]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
d353bf18 2 Copyright (C) 1992-2015 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
b20a8bb4 20#define GCC_C_COMMON_C
21
b0fc3e72 22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
b20a8bb4 25#include "input.h"
7ff8db31 26#include "c-common.h"
805e22b2 27#include "tm.h"
e48d0f41 28#include "intl.h"
b0fc3e72 29#include "tree.h"
9ed99284 30#include "fold-const.h"
31#include "stor-layout.h"
32#include "calls.h"
33#include "stringpool.h"
34#include "attribs.h"
35#include "varasm.h"
36#include "trans-mem.h"
b0fc3e72 37#include "flags.h"
a3fa7feb 38#include "c-pragma.h"
6c536c4f 39#include "c-objc.h"
d8c9779c 40#include "tm_p.h"
4e91a871 41#include "obstack.h"
a654e028 42#include "cpplib.h"
8ee295a7 43#include "target.h"
218e3e4e 44#include "common/common-target.h"
96554925 45#include "langhooks.h"
f3dde807 46#include "tree-inline.h"
69579044 47#include "toplev.h"
7f5f3953 48#include "diagnostic.h"
4ee9c684 49#include "tree-iterator.h"
50#include "hashtab.h"
e08bd2f4 51#include "opts.h"
1140c305 52#include "hash-map.h"
53#include "is-a.h"
54#include "plugin-api.h"
55#include "vec.h"
56#include "hash-set.h"
1140c305 57#include "hard-reg-set.h"
58#include "input.h"
59#include "function.h"
60#include "ipa-ref.h"
62eec3b4 61#include "cgraph.h"
b9fc964a 62#include "target-def.h"
a8783bee 63#include "gimplify.h"
e913b5cd 64#include "wide-int-print.h"
c61ef207 65#include "gimple-expr.h"
fd6f6435 66
90cc7820 67cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 68
de801c28 69/* Mode used to build pointers (VOIDmode means ptr_mode). */
70
71machine_mode c_default_pointer_mode = VOIDmode;
72
72040e7e 73/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 74 listed here individually for documentation purposes.
72040e7e 75
76 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
77
78 tree short_integer_type_node;
79 tree long_integer_type_node;
80 tree long_long_integer_type_node;
81
82 tree short_unsigned_type_node;
83 tree long_unsigned_type_node;
84 tree long_long_unsigned_type_node;
85
3c2239cf 86 tree truthvalue_type_node;
87 tree truthvalue_false_node;
88 tree truthvalue_true_node;
72040e7e 89
90 tree ptrdiff_type_node;
91
92 tree unsigned_char_type_node;
93 tree signed_char_type_node;
94 tree wchar_type_node;
72040e7e 95
924bbf02 96 tree char16_type_node;
97 tree char32_type_node;
98
72040e7e 99 tree float_type_node;
100 tree double_type_node;
101 tree long_double_type_node;
102
103 tree complex_integer_type_node;
104 tree complex_float_type_node;
105 tree complex_double_type_node;
106 tree complex_long_double_type_node;
107
c4503c0a 108 tree dfloat32_type_node;
109 tree dfloat64_type_node;
110 tree_dfloat128_type_node;
111
72040e7e 112 tree intQI_type_node;
113 tree intHI_type_node;
114 tree intSI_type_node;
115 tree intDI_type_node;
116 tree intTI_type_node;
117
118 tree unsigned_intQI_type_node;
119 tree unsigned_intHI_type_node;
120 tree unsigned_intSI_type_node;
121 tree unsigned_intDI_type_node;
122 tree unsigned_intTI_type_node;
123
124 tree widest_integer_literal_type_node;
125 tree widest_unsigned_literal_type_node;
126
127 Nodes for types `void *' and `const void *'.
128
129 tree ptr_type_node, const_ptr_type_node;
130
131 Nodes for types `char *' and `const char *'.
132
133 tree string_type_node, const_string_type_node;
134
135 Type `char[SOMENUMBER]'.
136 Used when an array of char is needed and the size is irrelevant.
137
138 tree char_array_type_node;
139
72040e7e 140 Type `wchar_t[SOMENUMBER]' or something like it.
141 Used when a wide string literal is created.
142
143 tree wchar_array_type_node;
144
924bbf02 145 Type `char16_t[SOMENUMBER]' or something like it.
146 Used when a UTF-16 string literal is created.
147
148 tree char16_array_type_node;
149
150 Type `char32_t[SOMENUMBER]' or something like it.
151 Used when a UTF-32 string literal is created.
152
153 tree char32_array_type_node;
154
72040e7e 155 Type `int ()' -- used for implicit declaration of functions.
156
157 tree default_function_type;
158
72040e7e 159 A VOID_TYPE node, packaged in a TREE_LIST.
160
161 tree void_list_node;
162
734c98be 163 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 164 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
165 VAR_DECLS, but C++ does.)
71d9fc9b 166
65b7f83f 167 tree function_name_decl_node;
734c98be 168 tree pretty_function_name_decl_node;
65b7f83f 169 tree c99_function_name_decl_node;
170
171 Stack of nested function name VAR_DECLs.
1cae46be 172
65b7f83f 173 tree saved_function_name_decls;
71d9fc9b 174
72040e7e 175*/
176
177tree c_global_trees[CTI_MAX];
573aba85 178\f
574a6990 179/* Switches common to the C front ends. */
180
f7070933 181/* Nonzero means don't output line number information. */
182
183char flag_no_line_commands;
184
185/* Nonzero causes -E output not to be done, but directives such as
186 #define that have side effects are still obeyed. */
187
188char flag_no_output;
189
190/* Nonzero means dump macros in some fashion. */
191
192char flag_dump_macros;
193
194/* Nonzero means pass #include lines through to the output. */
195
196char flag_dump_includes;
197
d718b525 198/* Nonzero means process PCH files while preprocessing. */
199
200bool flag_pch_preprocess;
201
573aba85 202/* The file name to which we should write a precompiled header, or
203 NULL if no header will be written in this compile. */
204
205const char *pch_file;
206
1ed9d5f5 207/* Nonzero if an ISO standard was selected. It rejects macros in the
208 user's namespace. */
209int flag_iso;
210
574a6990 211/* C/ObjC language option variables. */
212
213
574a6990 214/* Nonzero means allow type mismatches in conditional expressions;
215 just make their values `void'. */
216
217int flag_cond_mismatch;
218
219/* Nonzero means enable C89 Amendment 1 features. */
220
221int flag_isoc94;
222
32074525 223/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 224
225int flag_isoc99;
226
32074525 227/* Nonzero means use the ISO C11 dialect of C. */
39012afb 228
32074525 229int flag_isoc11;
39012afb 230
8b332087 231/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 232
233int flag_hosted = 1;
234
574a6990 235
236/* ObjC language option variables. */
237
238
574a6990 239/* Tells the compiler that this is a special run. Do not perform any
240 compiling, instead we are to test some platform dependent features
241 and output a C header file with appropriate definitions. */
242
243int print_struct_values;
244
f0b5f617 245/* Tells the compiler what is the constant string class for ObjC. */
574a6990 246
247const char *constant_string_class_name;
248
574a6990 249
250/* C++ language option variables. */
251
252
574a6990 253/* Nonzero means generate separate instantiation control files and
254 juggle them at link time. */
255
256int flag_use_repository;
257
d875b9d2 258/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 259
d875b9d2 260enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 261
9dcd0d49 262/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 263 time it takes to notice excessively recursive template instantiations.
574a6990 264
8ce59854 265 The default is lower than the 1024 recommended by the C++0x standard
266 because G++ runs out of stack before 1024 with highly recursive template
267 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
268
269int max_tinst_depth = 900;
574a6990 270
988fc1d1 271/* The elements of `ridpointers' are identifier nodes for the reserved
272 type names and storage classes. It is indexed by a RID_... value. */
273tree *ridpointers;
274
e60a6f7b 275tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 276
48d94ede 277/* Nonzero means don't warn about problems that occur when the code is
278 executed. */
279int c_inhibit_evaluation_warnings;
e78703c1 280
93be21c0 281/* Whether we are building a boolean conversion inside
282 convert_for_assignment, or some other late binary operation. If
283 build_binary_op is called for C (from code shared by C and C++) in
284 this case, then the operands have already been folded and the
285 result will not be folded again, so C_MAYBE_CONST_EXPR should not
286 be generated. */
287bool in_late_binary_op;
288
7f5f3953 289/* Whether lexing has been completed, so subsequent preprocessor
290 errors should use the compiler's input_location. */
291bool done_lexing = false;
292
2c0e001b 293/* Information about how a function name is generated. */
65b7f83f 294struct fname_var_t
295{
e99c3a1d 296 tree *const decl; /* pointer to the VAR_DECL. */
297 const unsigned rid; /* RID number for the identifier. */
298 const int pretty; /* How pretty is it? */
65b7f83f 299};
300
2c0e001b 301/* The three ways of getting then name of the current function. */
65b7f83f 302
303const struct fname_var_t fname_vars[] =
304{
2c0e001b 305 /* C99 compliant __func__, must be first. */
65b7f83f 306 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 307 /* GCC __FUNCTION__ compliant. */
65b7f83f 308 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 309 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 310 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
311 {NULL, 0, 0},
312};
313
79396169 314/* Global visibility options. */
315struct visibility_flags visibility_options;
316
aac24642 317static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 318static tree check_case_value (location_t, tree);
f61a9bc2 319static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
be43ff5a 320
1cae46be 321static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
322static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
323static tree handle_common_attribute (tree *, tree, tree, int, bool *);
324static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 325static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
326static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 327static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
328 int, bool *);
d413ffdd 329static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
330 int, bool *);
05f893e1 331static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
332 bool *);
947aa916 333static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 334static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 335static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 336static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 337static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 338static tree handle_always_inline_attribute (tree *, tree, tree, int,
339 bool *);
1b16fc45 340static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
341static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 342static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 343static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 344static tree handle_used_attribute (tree *, tree, tree, int, bool *);
345static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 346static tree handle_externally_visible_attribute (tree *, tree, tree, int,
347 bool *);
6b722052 348static tree handle_no_reorder_attribute (tree *, tree, tree, int,
349 bool *);
1cae46be 350static tree handle_const_attribute (tree *, tree, tree, int, bool *);
351static tree handle_transparent_union_attribute (tree *, tree, tree,
352 int, bool *);
353static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
354static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
355static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
356static tree handle_section_attribute (tree *, tree, tree, int, bool *);
357static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
358static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 359static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
360static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 361static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 362static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 363static tree handle_visibility_attribute (tree *, tree, tree, int,
364 bool *);
365static tree handle_tls_model_attribute (tree *, tree, tree, int,
366 bool *);
367static tree handle_no_instrument_function_attribute (tree *, tree,
368 tree, int, bool *);
369static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 370static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 371static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
372 bool *);
373static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 374static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
375static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 376static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 377static tree handle_deprecated_attribute (tree *, tree, tree, int,
378 bool *);
379static tree handle_vector_size_attribute (tree *, tree, tree, int,
380 bool *);
381static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
382static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
383static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 384static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
385 bool *);
bf6c8de0 386static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 387static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 388static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 389static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
390static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 391static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 392static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 393static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 394static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 395static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 396static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 397static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 398static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
399 bool *);
400static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
401 bool *);
74691f46 402static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 403static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
404static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
405static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 406
d01f58f9 407static void check_function_nonnull (tree, int, tree *);
1cae46be 408static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
409static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
410static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 411static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 412
5c6e5756 413/* Reserved words. The third field is a mask: keywords are disabled
414 if they match the mask.
415
416 Masks for languages:
417 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
418 C --std=c99: D_CXXONLY | D_OBJC
419 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
420 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
421 C++ --std=c0x: D_CONLY | D_OBJC
422 ObjC++ is like C++ except that D_OBJC is not set
423
424 If -fno-asm is used, D_ASM is added to the mask. If
425 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
426 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 427 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 428
b27e241e 429 Note the complication of the D_CXX_OBJC keywords. These are
430 reserved words such as 'class'. In C++, 'class' is a reserved
431 word. In Objective-C++ it is too. In Objective-C, it is a
432 reserved word too, but only if it follows an '@' sign.
433*/
5c6e5756 434const struct c_common_resword c_common_reswords[] =
435{
83e25171 436 { "_Alignas", RID_ALIGNAS, D_CONLY },
437 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 438 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 439 { "_Bool", RID_BOOL, D_CONLY },
440 { "_Complex", RID_COMPLEX, 0 },
d037099f 441 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
442 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 443 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 444 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 445 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
446 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
447 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
448 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
449 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
450 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 451 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 452 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 453 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 454 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 455 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
456 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
457 { "__alignof", RID_ALIGNOF, 0 },
458 { "__alignof__", RID_ALIGNOF, 0 },
459 { "__asm", RID_ASM, 0 },
460 { "__asm__", RID_ASM, 0 },
461 { "__attribute", RID_ATTRIBUTE, 0 },
462 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 463 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 464 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 465 { "__builtin_call_with_static_chain",
466 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 467 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 468 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 469 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 470 { "__builtin_offsetof", RID_OFFSETOF, 0 },
471 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
472 { "__builtin_va_arg", RID_VA_ARG, 0 },
473 { "__complex", RID_COMPLEX, 0 },
474 { "__complex__", RID_COMPLEX, 0 },
475 { "__const", RID_CONST, 0 },
476 { "__const__", RID_CONST, 0 },
477 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 478 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 479 { "__extension__", RID_EXTENSION, 0 },
480 { "__func__", RID_C99_FUNCTION_NAME, 0 },
481 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
482 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
483 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
484 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
485 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
486 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
487 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
488 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 489 { "__imag", RID_IMAGPART, 0 },
490 { "__imag__", RID_IMAGPART, 0 },
491 { "__inline", RID_INLINE, 0 },
492 { "__inline__", RID_INLINE, 0 },
5c6e5756 493 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
494 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
495 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 496 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
497 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 498 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 499 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 500 { "__is_pod", RID_IS_POD, D_CXXONLY },
501 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c1c67b4f 502 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
503 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 504 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
505 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 506 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 507 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 508 { "__label__", RID_LABEL, 0 },
509 { "__null", RID_NULL, 0 },
510 { "__real", RID_REALPART, 0 },
511 { "__real__", RID_REALPART, 0 },
512 { "__restrict", RID_RESTRICT, 0 },
513 { "__restrict__", RID_RESTRICT, 0 },
514 { "__signed", RID_SIGNED, 0 },
515 { "__signed__", RID_SIGNED, 0 },
516 { "__thread", RID_THREAD, 0 },
4c0315d0 517 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
518 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
519 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 520 { "__typeof", RID_TYPEOF, 0 },
521 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 522 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 523 { "__volatile", RID_VOLATILE, 0 },
524 { "__volatile__", RID_VOLATILE, 0 },
ffcdbf9c 525 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
e463efd7 526 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 527 { "asm", RID_ASM, D_ASM },
528 { "auto", RID_AUTO, 0 },
dbd982c9 529 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 530 { "break", RID_BREAK, 0 },
531 { "case", RID_CASE, 0 },
51030405 532 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 533 { "char", RID_CHAR, 0 },
51030405 534 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
535 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 537 { "const", RID_CONST, 0 },
17814aca 538 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 539 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
540 { "continue", RID_CONTINUE, 0 },
51030405 541 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 542 { "default", RID_DEFAULT, 0 },
51030405 543 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 544 { "do", RID_DO, 0 },
545 { "double", RID_DOUBLE, 0 },
546 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
547 { "else", RID_ELSE, 0 },
548 { "enum", RID_ENUM, 0 },
51030405 549 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
550 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 551 { "extern", RID_EXTERN, 0 },
51030405 552 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 553 { "float", RID_FLOAT, 0 },
554 { "for", RID_FOR, 0 },
51030405 555 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 556 { "goto", RID_GOTO, 0 },
557 { "if", RID_IF, 0 },
558 { "inline", RID_INLINE, D_EXT89 },
559 { "int", RID_INT, 0 },
560 { "long", RID_LONG, 0 },
561 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 562 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
563 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
98fe9664 564 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
6fe11077 565 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 566 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
567 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
568 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
569 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 570 { "register", RID_REGISTER, 0 },
571 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
572 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
573 { "return", RID_RETURN, 0 },
574 { "short", RID_SHORT, 0 },
575 { "signed", RID_SIGNED, 0 },
576 { "sizeof", RID_SIZEOF, 0 },
577 { "static", RID_STATIC, 0 },
578 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
579 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
580 { "struct", RID_STRUCT, 0 },
581 { "switch", RID_SWITCH, 0 },
51030405 582 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
583 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
3740094c 584 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 585 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
586 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
587 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 588 { "typedef", RID_TYPEDEF, 0 },
51030405 589 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
590 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 591 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
592 { "union", RID_UNION, 0 },
593 { "unsigned", RID_UNSIGNED, 0 },
51030405 594 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
595 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 596 { "void", RID_VOID, 0 },
597 { "volatile", RID_VOLATILE, 0 },
598 { "wchar_t", RID_WCHAR, D_CXXONLY },
599 { "while", RID_WHILE, 0 },
600 /* These Objective-C keywords are recognized only immediately after
601 an '@'. */
602 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
603 { "defs", RID_AT_DEFS, D_OBJC },
604 { "encode", RID_AT_ENCODE, D_OBJC },
605 { "end", RID_AT_END, D_OBJC },
606 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
607 { "interface", RID_AT_INTERFACE, D_OBJC },
608 { "protocol", RID_AT_PROTOCOL, D_OBJC },
609 { "selector", RID_AT_SELECTOR, D_OBJC },
610 { "finally", RID_AT_FINALLY, D_OBJC },
611 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
069761fb 612 { "optional", RID_AT_OPTIONAL, D_OBJC },
613 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 614 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 615 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 616 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
617 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 618 /* These are recognized only in protocol-qualifier context
619 (see above) */
620 { "bycopy", RID_BYCOPY, D_OBJC },
621 { "byref", RID_BYREF, D_OBJC },
622 { "in", RID_IN, D_OBJC },
623 { "inout", RID_INOUT, D_OBJC },
624 { "oneway", RID_ONEWAY, D_OBJC },
625 { "out", RID_OUT, D_OBJC },
86c110ac 626 /* These are recognized inside a property attribute list */
7590f0e5 627 { "assign", RID_ASSIGN, D_OBJC },
628 { "copy", RID_COPY, D_OBJC },
86c110ac 629 { "getter", RID_GETTER, D_OBJC },
7590f0e5 630 { "nonatomic", RID_NONATOMIC, D_OBJC },
631 { "readonly", RID_READONLY, D_OBJC },
632 { "readwrite", RID_READWRITE, D_OBJC },
633 { "retain", RID_RETAIN, D_OBJC },
86c110ac 634 { "setter", RID_SETTER, D_OBJC },
5c6e5756 635};
636
637const unsigned int num_c_common_reswords =
638 sizeof c_common_reswords / sizeof (struct c_common_resword);
639
058a1b7a 640/* Table of machine-independent attributes common to all C-like languages.
641
642 All attributes referencing arguments should be additionally processed
643 in chkp_copy_function_type_adding_bounds for correct instrumentation
644 by Pointer Bounds Checker.
645 Current list of processed common attributes: nonnull. */
f8e93a2e 646const struct attribute_spec c_common_attribute_table[] =
647{
ac86af5d 648 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
649 affects_type_identity } */
f8e93a2e 650 { "packed", 0, 0, false, false, false,
ac86af5d 651 handle_packed_attribute , false},
f8e93a2e 652 { "nocommon", 0, 0, true, false, false,
ac86af5d 653 handle_nocommon_attribute, false},
f8e93a2e 654 { "common", 0, 0, true, false, false,
ac86af5d 655 handle_common_attribute, false },
f8e93a2e 656 /* FIXME: logically, noreturn attributes should be listed as
657 "false, true, true" and apply to function types. But implementing this
658 would require all the places in the compiler that use TREE_THIS_VOLATILE
659 on a decl to identify non-returning functions to be located and fixed
660 to check the function type instead. */
661 { "noreturn", 0, 0, true, false, false,
ac86af5d 662 handle_noreturn_attribute, false },
f8e93a2e 663 { "volatile", 0, 0, true, false, false,
ac86af5d 664 handle_noreturn_attribute, false },
947aa916 665 { "stack_protect", 0, 0, true, false, false,
666 handle_stack_protect_attribute, false },
f8e93a2e 667 { "noinline", 0, 0, true, false, false,
ac86af5d 668 handle_noinline_attribute, false },
bdb1f0d1 669 { "noclone", 0, 0, true, false, false,
ac86af5d 670 handle_noclone_attribute, false },
85fbea97 671 { "no_icf", 0, 0, true, false, false,
672 handle_noicf_attribute, false },
7bd95dfd 673 { "leaf", 0, 0, true, false, false,
ac86af5d 674 handle_leaf_attribute, false },
f8e93a2e 675 { "always_inline", 0, 0, true, false, false,
ac86af5d 676 handle_always_inline_attribute, false },
541e4101 677 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 678 handle_gnu_inline_attribute, false },
1b16fc45 679 { "artificial", 0, 0, true, false, false,
ac86af5d 680 handle_artificial_attribute, false },
0cdd9887 681 { "flatten", 0, 0, true, false, false,
ac86af5d 682 handle_flatten_attribute, false },
f8e93a2e 683 { "used", 0, 0, true, false, false,
ac86af5d 684 handle_used_attribute, false },
f8e93a2e 685 { "unused", 0, 0, false, false, false,
ac86af5d 686 handle_unused_attribute, false },
62eec3b4 687 { "externally_visible", 0, 0, true, false, false,
ac86af5d 688 handle_externally_visible_attribute, false },
6b722052 689 { "no_reorder", 0, 0, true, false, false,
690 handle_no_reorder_attribute, false },
f8e93a2e 691 /* The same comments as for noreturn attributes apply to const ones. */
692 { "const", 0, 0, true, false, false,
ac86af5d 693 handle_const_attribute, false },
f8e93a2e 694 { "transparent_union", 0, 0, false, false, false,
ac86af5d 695 handle_transparent_union_attribute, false },
9af7fd5b 696 { "constructor", 0, 1, true, false, false,
ac86af5d 697 handle_constructor_attribute, false },
9af7fd5b 698 { "destructor", 0, 1, true, false, false,
ac86af5d 699 handle_destructor_attribute, false },
f8e93a2e 700 { "mode", 1, 1, false, true, false,
ac86af5d 701 handle_mode_attribute, false },
f8e93a2e 702 { "section", 1, 1, true, false, false,
ac86af5d 703 handle_section_attribute, false },
f8e93a2e 704 { "aligned", 0, 1, false, false, false,
ac86af5d 705 handle_aligned_attribute, false },
f8e93a2e 706 { "weak", 0, 0, true, false, false,
ac86af5d 707 handle_weak_attribute, false },
85c0a25c 708 { "ifunc", 1, 1, true, false, false,
ac86af5d 709 handle_ifunc_attribute, false },
f8e93a2e 710 { "alias", 1, 1, true, false, false,
ac86af5d 711 handle_alias_attribute, false },
f4a30bd7 712 { "weakref", 0, 1, true, false, false,
ac86af5d 713 handle_weakref_attribute, false },
f8e93a2e 714 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 715 handle_no_instrument_function_attribute,
716 false },
f8e93a2e 717 { "malloc", 0, 0, true, false, false,
ac86af5d 718 handle_malloc_attribute, false },
26d1c5ff 719 { "returns_twice", 0, 0, true, false, false,
ac86af5d 720 handle_returns_twice_attribute, false },
f8e93a2e 721 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 722 handle_no_limit_stack_attribute, false },
f8e93a2e 723 { "pure", 0, 0, true, false, false,
ac86af5d 724 handle_pure_attribute, false },
4c0315d0 725 { "transaction_callable", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_unsafe", 0, 0, false, true, false,
728 handle_tm_attribute, false },
729 { "transaction_safe", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_may_cancel_outer", 0, 0, false, true, false,
732 handle_tm_attribute, false },
733 /* ??? These two attributes didn't make the transition from the
734 Intel language document to the multi-vendor language document. */
735 { "transaction_pure", 0, 0, false, true, false,
736 handle_tm_attribute, false },
737 { "transaction_wrap", 1, 1, true, false, false,
738 handle_tm_wrap_attribute, false },
fc09b200 739 /* For internal use (marking of builtins) only. The name contains space
740 to prevent its usage in source code. */
741 { "no vops", 0, 0, true, false, false,
ac86af5d 742 handle_novops_attribute, false },
45c4e798 743 { "deprecated", 0, 1, false, false, false,
ac86af5d 744 handle_deprecated_attribute, false },
f8e93a2e 745 { "vector_size", 1, 1, false, true, false,
ac86af5d 746 handle_vector_size_attribute, false },
b212f378 747 { "visibility", 1, 1, false, false, false,
ac86af5d 748 handle_visibility_attribute, false },
24dfead4 749 { "tls_model", 1, 1, true, false, false,
ac86af5d 750 handle_tls_model_attribute, false },
dbf6c367 751 { "nonnull", 0, -1, false, true, true,
ac86af5d 752 handle_nonnull_attribute, false },
fa987697 753 { "nothrow", 0, 0, true, false, false,
ac86af5d 754 handle_nothrow_attribute, false },
755 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 756 { "cleanup", 1, 1, true, false, false,
ac86af5d 757 handle_cleanup_attribute, false },
8a8cdb8d 758 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 759 handle_warn_unused_result_attribute, false },
50ca527f 760 { "sentinel", 0, 1, false, true, true,
ac86af5d 761 handle_sentinel_attribute, false },
b5c26b42 762 /* For internal use (marking of builtins) only. The name contains space
763 to prevent its usage in source code. */
764 { "type generic", 0, 0, false, true, true,
ac86af5d 765 handle_type_generic_attribute, false },
4a29c97c 766 { "alloc_size", 1, 2, false, true, true,
ac86af5d 767 handle_alloc_size_attribute, false },
5de92639 768 { "cold", 0, 0, true, false, false,
ac86af5d 769 handle_cold_attribute, false },
5de92639 770 { "hot", 0, 0, true, false, false,
ac86af5d 771 handle_hot_attribute, false },
d413ffdd 772 { "no_address_safety_analysis",
773 0, 0, true, false, false,
774 handle_no_address_safety_analysis_attribute,
775 false },
a9196da9 776 { "no_sanitize_address", 0, 0, true, false, false,
777 handle_no_sanitize_address_attribute,
778 false },
d1e96383 779 { "no_sanitize_thread", 0, 0, true, false, false,
780 handle_no_sanitize_address_attribute,
781 false },
05f893e1 782 { "no_sanitize_undefined", 0, 0, true, false, false,
783 handle_no_sanitize_undefined_attribute,
784 false },
10fc867f 785 { "warning", 1, 1, true, false, false,
ac86af5d 786 handle_error_attribute, false },
10fc867f 787 { "error", 1, 1, true, false, false,
ac86af5d 788 handle_error_attribute, false },
24470055 789 { "target", 1, -1, true, false, false,
ac86af5d 790 handle_target_attribute, false },
46f8e3b0 791 { "optimize", 1, -1, true, false, false,
ac86af5d 792 handle_optimize_attribute, false },
4c0315d0 793 /* For internal use only. The leading '*' both prevents its usage in
794 source code and signals that it may be overridden by machine tables. */
795 { "*tm regparm", 0, 0, false, true, true,
796 ignore_attribute, false },
48b14f50 797 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 798 handle_no_split_stack_attribute, false },
8ce86007 799 /* For internal use (marking of builtins and runtime functions) only.
800 The name contains space to prevent its usage in source code. */
801 { "fn spec", 1, 1, false, true, true,
ac86af5d 802 handle_fnspec_attribute, false },
a96c3cc1 803 { "warn_unused", 0, 0, false, false, false,
804 handle_warn_unused_attribute, false },
d7dcba40 805 { "returns_nonnull", 0, 0, false, true, true,
806 handle_returns_nonnull_attribute, false },
bc7bff74 807 { "omp declare simd", 0, -1, true, false, false,
808 handle_omp_declare_simd_attribute, false },
74acc703 809 { "cilk simd function", 0, -1, true, false, false,
810 handle_omp_declare_simd_attribute, false },
bc7bff74 811 { "omp declare target", 0, 0, true, false, false,
812 handle_omp_declare_target_attribute, false },
237e78b1 813 { "alloc_align", 1, 1, false, true, true,
814 handle_alloc_align_attribute, false },
815 { "assume_aligned", 1, 2, false, true, true,
816 handle_assume_aligned_attribute, false },
74691f46 817 { "designated_init", 0, 0, false, true, false,
818 handle_designated_init_attribute, false },
058a1b7a 819 { "bnd_variable_size", 0, 0, true, false, false,
820 handle_bnd_variable_size_attribute, false },
821 { "bnd_legacy", 0, 0, true, false, false,
822 handle_bnd_legacy, false },
823 { "bnd_instrument", 0, 0, true, false, false,
824 handle_bnd_instrument, false },
ac86af5d 825 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 826};
827
828/* Give the specifications for the format attributes, used by C and all
058a1b7a 829 descendants.
f8e93a2e 830
058a1b7a 831 All attributes referencing arguments should be additionally processed
832 in chkp_copy_function_type_adding_bounds for correct instrumentation
833 by Pointer Bounds Checker.
834 Current list of processed format attributes: format, format_arg. */
f8e93a2e 835const struct attribute_spec c_common_format_attribute_table[] =
836{
ac86af5d 837 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
838 affects_type_identity } */
f8e93a2e 839 { "format", 3, 3, false, true, true,
ac86af5d 840 handle_format_attribute, false },
f8e93a2e 841 { "format_arg", 1, 1, false, true, true,
ac86af5d 842 handle_format_arg_attribute, false },
843 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 844};
845
6d5d708e 846/* Return identifier for address space AS. */
34208e18 847
6d5d708e 848const char *
849c_addr_space_name (addr_space_t as)
850{
34208e18 851 int rid = RID_FIRST_ADDR_SPACE + as;
852 gcc_assert (ridpointers [rid]);
853 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 854}
855
2c0e001b 856/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 857
858void
1cae46be 859start_fname_decls (void)
f4e3c278 860{
65b7f83f 861 unsigned ix;
862 tree saved = NULL_TREE;
1cae46be 863
65b7f83f 864 for (ix = 0; fname_vars[ix].decl; ix++)
865 {
866 tree decl = *fname_vars[ix].decl;
f4e3c278 867
65b7f83f 868 if (decl)
869 {
ceb7b692 870 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
871 saved);
65b7f83f 872 *fname_vars[ix].decl = NULL_TREE;
873 }
874 }
875 if (saved || saved_function_name_decls)
876 /* Normally they'll have been NULL, so only push if we've got a
877 stack, or they are non-NULL. */
878 saved_function_name_decls = tree_cons (saved, NULL_TREE,
879 saved_function_name_decls);
880}
881
2363ef00 882/* Finish up the current bindings, adding them into the current function's
883 statement tree. This must be done _before_ finish_stmt_tree is called.
884 If there is no current function, we must be at file scope and no statements
885 are involved. Pop the previous bindings. */
65b7f83f 886
887void
1cae46be 888finish_fname_decls (void)
65b7f83f 889{
890 unsigned ix;
2363ef00 891 tree stmts = NULL_TREE;
65b7f83f 892 tree stack = saved_function_name_decls;
893
894 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 895 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 896
2363ef00 897 if (stmts)
65b7f83f 898 {
2363ef00 899 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 900
2363ef00 901 if (TREE_CODE (*bodyp) == BIND_EXPR)
902 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 903
bc2b76e0 904 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 905 *bodyp = stmts;
65b7f83f 906 }
1cae46be 907
65b7f83f 908 for (ix = 0; fname_vars[ix].decl; ix++)
909 *fname_vars[ix].decl = NULL_TREE;
1cae46be 910
65b7f83f 911 if (stack)
f4e3c278 912 {
2c0e001b 913 /* We had saved values, restore them. */
65b7f83f 914 tree saved;
915
916 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
917 {
918 tree decl = TREE_PURPOSE (saved);
f9ae6f95 919 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 920
65b7f83f 921 *fname_vars[ix].decl = decl;
922 }
923 stack = TREE_CHAIN (stack);
f4e3c278 924 }
65b7f83f 925 saved_function_name_decls = stack;
926}
927
81010c97 928/* Return the text name of the current function, suitably prettified
5fc7fa69 929 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 930
931const char *
1cae46be 932fname_as_string (int pretty_p)
65b7f83f 933{
9ad4bb1e 934 const char *name = "top level";
5fc7fa69 935 char *namep;
8115b8be 936 int vrb = 2, len;
937 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 938
84166705 939 if (!pretty_p)
9ad4bb1e 940 {
941 name = "";
942 vrb = 0;
943 }
944
945 if (current_function_decl)
dc24ddbd 946 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 947
8115b8be 948 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 949
8115b8be 950 namep = XNEWVEC (char, len);
951 snprintf (namep, len, "\"%s\"", name);
952 strname.text = (unsigned char *) namep;
953 strname.len = len - 1;
5fc7fa69 954
924bbf02 955 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 956 {
957 XDELETEVEC (namep);
958 return (const char *) cstr.text;
5fc7fa69 959 }
5fc7fa69 960
961 return namep;
65b7f83f 962}
963
65b7f83f 964/* Return the VAR_DECL for a const char array naming the current
965 function. If the VAR_DECL has not yet been created, create it
966 now. RID indicates how it should be formatted and IDENTIFIER_NODE
967 ID is its name (unfortunately C and C++ hold the RID values of
968 keywords in different places, so we can't derive RID from ID in
e3b80d49 969 this language independent code. LOC is the location of the
970 function. */
65b7f83f 971
972tree
e3b80d49 973fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 974{
975 unsigned ix;
976 tree decl = NULL_TREE;
977
978 for (ix = 0; fname_vars[ix].decl; ix++)
979 if (fname_vars[ix].rid == rid)
980 break;
981
982 decl = *fname_vars[ix].decl;
983 if (!decl)
f4e3c278 984 {
2222b3c6 985 /* If a tree is built here, it would normally have the lineno of
986 the current statement. Later this tree will be moved to the
987 beginning of the function and this line number will be wrong.
988 To avoid this problem set the lineno to 0 here; that prevents
7299020b 989 it from appearing in the RTL. */
2363ef00 990 tree stmts;
9a6486a6 991 location_t saved_location = input_location;
9a6486a6 992 input_location = UNKNOWN_LOCATION;
1cae46be 993
2363ef00 994 stmts = push_stmt_list ();
e60a6f7b 995 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 996 stmts = pop_stmt_list (stmts);
997 if (!IS_EMPTY_STMT (stmts))
998 saved_function_name_decls
999 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1000 *fname_vars[ix].decl = decl;
9a6486a6 1001 input_location = saved_location;
f4e3c278 1002 }
65b7f83f 1003 if (!ix && !current_function_decl)
e3b80d49 1004 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1005
65b7f83f 1006 return decl;
f4e3c278 1007}
1008
070236f0 1009/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1010
1011tree
1cae46be 1012fix_string_type (tree value)
b0fc3e72 1013{
070236f0 1014 int length = TREE_STRING_LENGTH (value);
1015 int nchars;
00d26680 1016 tree e_type, i_type, a_type;
1017
73be5127 1018 /* Compute the number of elements, for the array type. */
924bbf02 1019 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1020 {
1021 nchars = length;
1022 e_type = char_type_node;
1023 }
1024 else if (TREE_TYPE (value) == char16_array_type_node)
1025 {
1026 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1027 e_type = char16_type_node;
1028 }
1029 else if (TREE_TYPE (value) == char32_array_type_node)
1030 {
1031 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1032 e_type = char32_type_node;
1033 }
1034 else
1035 {
1036 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1037 e_type = wchar_type_node;
1038 }
b0fc3e72 1039
1d752508 1040 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1041 limit in C++98 Annex B is very large (65536) and is not normative,
1042 so we do not diagnose it (warn_overlength_strings is forced off
1043 in c_common_post_options). */
1044 if (warn_overlength_strings)
1045 {
1046 const int nchars_max = flag_isoc99 ? 4095 : 509;
1047 const int relevant_std = flag_isoc99 ? 99 : 90;
1048 if (nchars - 1 > nchars_max)
1049 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1050 separate the %d from the 'C'. 'ISO' should not be
1051 translated, but it may be moved after 'C%d' in languages
1052 where modifiers follow nouns. */
21ca8540 1053 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1054 "string length %qd is greater than the length %qd "
1d752508 1055 "ISO C%d compilers are required to support",
1056 nchars - 1, nchars_max, relevant_std);
1057 }
82cfc7f7 1058
390be14e 1059 /* Create the array type for the string constant. The ISO C++
1060 standard says that a string literal has type `const char[N]' or
1061 `const wchar_t[N]'. We use the same logic when invoked as a C
1062 front-end with -Wwrite-strings.
1063 ??? We should change the type of an expression depending on the
1064 state of a warning flag. We should just be warning -- see how
1065 this is handled in the C++ front-end for the deprecated implicit
1066 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1067
1068 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1069 array type being the unqualified version of that type.
1070 Therefore, if we are constructing an array of const char, we must
1071 construct the matching unqualified array type first. The C front
1072 end does not require this, but it does no harm, so we do it
1073 unconditionally. */
ceb7b692 1074 i_type = build_index_type (size_int (nchars - 1));
00d26680 1075 a_type = build_array_type (e_type, i_type);
390be14e 1076 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1077 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1078
00d26680 1079 TREE_TYPE (value) = a_type;
b8e3b7ad 1080 TREE_CONSTANT (value) = 1;
a814bad5 1081 TREE_READONLY (value) = 1;
b0fc3e72 1082 TREE_STATIC (value) = 1;
1083 return value;
1084}
1085\f
fc501191 1086/* If DISABLE is true, stop issuing warnings. This is used when
1087 parsing code that we know will not be executed. This function may
1088 be called multiple times, and works as a stack. */
1089
1090static void
1091c_disable_warnings (bool disable)
1092{
1093 if (disable)
1094 {
1095 ++c_inhibit_evaluation_warnings;
1096 fold_defer_overflow_warnings ();
1097 }
1098}
1099
1100/* If ENABLE is true, reenable issuing warnings. */
1101
1102static void
1103c_enable_warnings (bool enable)
1104{
1105 if (enable)
1106 {
1107 --c_inhibit_evaluation_warnings;
1108 fold_undefer_and_ignore_overflow_warnings ();
1109 }
1110}
1111
a75b1c71 1112/* Fully fold EXPR, an expression that was not folded (beyond integer
1113 constant expressions and null pointer constants) when being built
1114 up. If IN_INIT, this is in a static initializer and certain
1115 changes are made to the folding done. Clear *MAYBE_CONST if
1116 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1117 expression because it contains an evaluated operator (in C99) or an
1118 operator outside of sizeof returning an integer constant (in C90)
1119 not permitted in constant expressions, or because it contains an
1120 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1121 set to true by callers before calling this function.) Return the
1122 folded expression. Function arguments have already been folded
1123 before calling this function, as have the contents of SAVE_EXPR,
1124 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1125 C_MAYBE_CONST_EXPR. */
1126
1127tree
1128c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1129{
1130 tree ret;
c6418a4e 1131 tree eptype = NULL_TREE;
a75b1c71 1132 bool dummy = true;
1133 bool maybe_const_itself = true;
389dd41b 1134 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1135
1136 /* This function is not relevant to C++ because C++ folds while
1137 parsing, and may need changes to be correct for C++ when C++
1138 stops folding while parsing. */
1139 if (c_dialect_cxx ())
1140 gcc_unreachable ();
1141
1142 if (!maybe_const)
1143 maybe_const = &dummy;
c6418a4e 1144 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1145 {
1146 eptype = TREE_TYPE (expr);
1147 expr = TREE_OPERAND (expr, 0);
1148 }
a75b1c71 1149 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1150 &maybe_const_itself, false);
c6418a4e 1151 if (eptype)
389dd41b 1152 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1153 *maybe_const &= maybe_const_itself;
1154 return ret;
1155}
1156
1157/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1158 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1159 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1160 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1161 both evaluated and unevaluated subexpressions while
1162 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1163 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1164 with integer constant operands, and if any of the operands doesn't
1165 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1166
1167static tree
1168c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1169 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1170{
1171 tree ret = expr;
1172 enum tree_code code = TREE_CODE (expr);
1173 enum tree_code_class kind = TREE_CODE_CLASS (code);
1174 location_t loc = EXPR_LOCATION (expr);
1175 tree op0, op1, op2, op3;
1176 tree orig_op0, orig_op1, orig_op2;
1177 bool op0_const = true, op1_const = true, op2_const = true;
1178 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1179 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1180 bool unused_p;
a75b1c71 1181
1182 /* This function is not relevant to C++ because C++ folds while
1183 parsing, and may need changes to be correct for C++ when C++
1184 stops folding while parsing. */
1185 if (c_dialect_cxx ())
1186 gcc_unreachable ();
1187
1188 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1189 anything else not counted as an expression cannot usefully be
1190 folded further at this point. */
1191 if (!IS_EXPR_CODE_CLASS (kind)
1192 || kind == tcc_statement
1193 || code == SAVE_EXPR)
1194 return expr;
1195
1196 /* Operands of variable-length expressions (function calls) have
1197 already been folded, as have __builtin_* function calls, and such
1198 expressions cannot occur in constant expressions. */
1199 if (kind == tcc_vl_exp)
1200 {
1201 *maybe_const_operands = false;
1202 ret = fold (expr);
1203 goto out;
1204 }
1205
1206 if (code == C_MAYBE_CONST_EXPR)
1207 {
1208 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1209 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1210 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1211 *maybe_const_operands = false;
1212 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1213 {
1214 *maybe_const_itself = false;
1215 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1216 maybe_const_itself, true);
1217 }
a75b1c71 1218 if (pre && !in_init)
1219 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1220 else
1221 ret = inner;
1222 goto out;
1223 }
1224
1225 /* Assignment, increment, decrement, function call and comma
1226 operators, and statement expressions, cannot occur in constant
1227 expressions if evaluated / outside of sizeof. (Function calls
1228 were handled above, though VA_ARG_EXPR is treated like a function
1229 call here, and statement expressions are handled through
1230 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1231 switch (code)
1232 {
1233 case MODIFY_EXPR:
1234 case PREDECREMENT_EXPR:
1235 case PREINCREMENT_EXPR:
1236 case POSTDECREMENT_EXPR:
1237 case POSTINCREMENT_EXPR:
1238 case COMPOUND_EXPR:
1239 *maybe_const_operands = false;
1240 break;
1241
1242 case VA_ARG_EXPR:
1243 case TARGET_EXPR:
1244 case BIND_EXPR:
1245 case OBJ_TYPE_REF:
1246 *maybe_const_operands = false;
1247 ret = fold (expr);
1248 goto out;
1249
1250 default:
1251 break;
1252 }
1253
1254 /* Fold individual tree codes as appropriate. */
1255 switch (code)
1256 {
1257 case COMPOUND_LITERAL_EXPR:
1258 /* Any non-constancy will have been marked in a containing
1259 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1260 goto out;
1261
1262 case COMPONENT_REF:
1263 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1264 op1 = TREE_OPERAND (expr, 1);
1265 op2 = TREE_OPERAND (expr, 2);
1266 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1267 maybe_const_itself, for_int_const);
f59e3889 1268 STRIP_TYPE_NOPS (op0);
a75b1c71 1269 if (op0 != orig_op0)
1270 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1271 if (ret != expr)
1272 {
1273 TREE_READONLY (ret) = TREE_READONLY (expr);
1274 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1275 }
1276 goto out;
1277
1278 case ARRAY_REF:
1279 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1280 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1281 op2 = TREE_OPERAND (expr, 2);
1282 op3 = TREE_OPERAND (expr, 3);
1283 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1284 maybe_const_itself, for_int_const);
f59e3889 1285 STRIP_TYPE_NOPS (op0);
a75b1c71 1286 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1287 maybe_const_itself, for_int_const);
f59e3889 1288 STRIP_TYPE_NOPS (op1);
a75b1c71 1289 op1 = decl_constant_value_for_optimization (op1);
1290 if (op0 != orig_op0 || op1 != orig_op1)
1291 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1292 if (ret != expr)
1293 {
1294 TREE_READONLY (ret) = TREE_READONLY (expr);
1295 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1296 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1297 }
1298 ret = fold (ret);
1299 goto out;
1300
1301 case COMPOUND_EXPR:
1302 case MODIFY_EXPR:
1303 case PREDECREMENT_EXPR:
1304 case PREINCREMENT_EXPR:
1305 case POSTDECREMENT_EXPR:
1306 case POSTINCREMENT_EXPR:
1307 case PLUS_EXPR:
1308 case MINUS_EXPR:
1309 case MULT_EXPR:
1310 case POINTER_PLUS_EXPR:
1311 case TRUNC_DIV_EXPR:
1312 case CEIL_DIV_EXPR:
1313 case FLOOR_DIV_EXPR:
1314 case TRUNC_MOD_EXPR:
1315 case RDIV_EXPR:
1316 case EXACT_DIV_EXPR:
1317 case LSHIFT_EXPR:
1318 case RSHIFT_EXPR:
1319 case BIT_IOR_EXPR:
1320 case BIT_XOR_EXPR:
1321 case BIT_AND_EXPR:
1322 case LT_EXPR:
1323 case LE_EXPR:
1324 case GT_EXPR:
1325 case GE_EXPR:
1326 case EQ_EXPR:
1327 case NE_EXPR:
1328 case COMPLEX_EXPR:
1329 case TRUTH_AND_EXPR:
1330 case TRUTH_OR_EXPR:
1331 case TRUTH_XOR_EXPR:
1332 case UNORDERED_EXPR:
1333 case ORDERED_EXPR:
1334 case UNLT_EXPR:
1335 case UNLE_EXPR:
1336 case UNGT_EXPR:
1337 case UNGE_EXPR:
1338 case UNEQ_EXPR:
1339 /* Binary operations evaluating both arguments (increment and
1340 decrement are binary internally in GCC). */
1341 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1342 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1343 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1344 maybe_const_itself, for_int_const);
f59e3889 1345 STRIP_TYPE_NOPS (op0);
a75b1c71 1346 if (code != MODIFY_EXPR
1347 && code != PREDECREMENT_EXPR
1348 && code != PREINCREMENT_EXPR
1349 && code != POSTDECREMENT_EXPR
1350 && code != POSTINCREMENT_EXPR)
1351 op0 = decl_constant_value_for_optimization (op0);
1352 /* The RHS of a MODIFY_EXPR was fully folded when building that
1353 expression for the sake of conversion warnings. */
1354 if (code != MODIFY_EXPR)
1355 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1356 maybe_const_itself, for_int_const);
f59e3889 1357 STRIP_TYPE_NOPS (op1);
a75b1c71 1358 op1 = decl_constant_value_for_optimization (op1);
aac24642 1359
1360 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1361 || TREE_CODE (op1) != INTEGER_CST))
1362 goto out;
1363
a75b1c71 1364 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1365 ret = in_init
389dd41b 1366 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1367 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1368 else
1369 ret = fold (expr);
672d914b 1370 if (TREE_OVERFLOW_P (ret)
1371 && !TREE_OVERFLOW_P (op0)
1372 && !TREE_OVERFLOW_P (op1))
1373 overflow_warning (EXPR_LOCATION (expr), ret);
cdc64059 1374 if (code == LSHIFT_EXPR
1375 && TREE_CODE (orig_op0) != INTEGER_CST
1376 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1377 && TREE_CODE (op0) == INTEGER_CST
1378 && c_inhibit_evaluation_warnings == 0
1379 && tree_int_cst_sgn (op0) < 0)
1380 warning_at (loc, OPT_Wshift_negative_value,
1381 "left shift of negative value");
7c834436 1382 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1383 && TREE_CODE (orig_op1) != INTEGER_CST
1384 && TREE_CODE (op1) == INTEGER_CST
1385 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1386 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1387 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1388 && c_inhibit_evaluation_warnings == 0)
1389 {
1390 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1391 warning_at (loc, OPT_Wshift_count_negative,
1392 (code == LSHIFT_EXPR
1393 ? G_("left shift count is negative")
1394 : G_("right shift count is negative")));
7c834436 1395 else if (compare_tree_int (op1,
1396 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1397 >= 0)
f11bdffb 1398 warning_at (loc, OPT_Wshift_count_overflow,
1399 (code == LSHIFT_EXPR
1400 ? G_("left shift count >= width of type")
1401 : G_("right shift count >= width of type")));
7c834436 1402 }
b57910fa 1403 if ((code == TRUNC_DIV_EXPR
1404 || code == CEIL_DIV_EXPR
1405 || code == FLOOR_DIV_EXPR
1406 || code == EXACT_DIV_EXPR
1407 || code == TRUNC_MOD_EXPR)
1408 && TREE_CODE (orig_op1) != INTEGER_CST
1409 && TREE_CODE (op1) == INTEGER_CST
1410 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1411 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1412 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1413 warn_for_div_by_zero (loc, op1);
a75b1c71 1414 goto out;
1415
1416 case INDIRECT_REF:
1417 case FIX_TRUNC_EXPR:
1418 case FLOAT_EXPR:
1419 CASE_CONVERT:
e66325ea 1420 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1421 case VIEW_CONVERT_EXPR:
a75b1c71 1422 case NON_LVALUE_EXPR:
1423 case NEGATE_EXPR:
1424 case BIT_NOT_EXPR:
1425 case TRUTH_NOT_EXPR:
1426 case ADDR_EXPR:
1427 case CONJ_EXPR:
1428 case REALPART_EXPR:
1429 case IMAGPART_EXPR:
1430 /* Unary operations. */
1431 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1432 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1433 maybe_const_itself, for_int_const);
f59e3889 1434 STRIP_TYPE_NOPS (op0);
a75b1c71 1435 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1436 op0 = decl_constant_value_for_optimization (op0);
aac24642 1437
1438 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1439 goto out;
1440
737a23cc 1441 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1442 not prepared to deal with them if they occur in initializers. */
1443 if (op0 != orig_op0
1444 && code == ADDR_EXPR
1445 && (op1 = get_base_address (op0)) != NULL_TREE
1446 && TREE_CODE (op1) == INDIRECT_REF
1447 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1448 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1449 else if (op0 != orig_op0 || in_init)
a75b1c71 1450 ret = in_init
389dd41b 1451 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1452 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1453 else
1454 ret = fold (expr);
1455 if (code == INDIRECT_REF
1456 && ret != expr
1457 && TREE_CODE (ret) == INDIRECT_REF)
1458 {
1459 TREE_READONLY (ret) = TREE_READONLY (expr);
1460 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1461 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1462 }
672d914b 1463 switch (code)
1464 {
1465 case FIX_TRUNC_EXPR:
1466 case FLOAT_EXPR:
1467 CASE_CONVERT:
1468 /* Don't warn about explicit conversions. We will already
1469 have warned about suspect implicit conversions. */
1470 break;
1471
1472 default:
1473 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1474 overflow_warning (EXPR_LOCATION (expr), ret);
1475 break;
1476 }
a75b1c71 1477 goto out;
1478
1479 case TRUTH_ANDIF_EXPR:
1480 case TRUTH_ORIF_EXPR:
1481 /* Binary operations not necessarily evaluating both
1482 arguments. */
1483 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1484 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1485 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1486 for_int_const);
f59e3889 1487 STRIP_TYPE_NOPS (op0);
672d914b 1488
1489 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1490 ? truthvalue_false_node
1491 : truthvalue_true_node));
fc501191 1492 c_disable_warnings (unused_p);
aac24642 1493 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1494 for_int_const);
f59e3889 1495 STRIP_TYPE_NOPS (op1);
fc501191 1496 c_enable_warnings (unused_p);
672d914b 1497
aac24642 1498 if (for_int_const
1499 && (TREE_CODE (op0) != INTEGER_CST
1500 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1501 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1502 goto out;
1503
a75b1c71 1504 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1505 ret = in_init
389dd41b 1506 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1507 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1508 else
1509 ret = fold (expr);
1510 *maybe_const_operands &= op0_const;
1511 *maybe_const_itself &= op0_const_self;
1512 if (!(flag_isoc99
1513 && op0_const
1514 && op0_const_self
1515 && (code == TRUTH_ANDIF_EXPR
1516 ? op0 == truthvalue_false_node
1517 : op0 == truthvalue_true_node)))
1518 *maybe_const_operands &= op1_const;
1519 if (!(op0_const
1520 && op0_const_self
1521 && (code == TRUTH_ANDIF_EXPR
1522 ? op0 == truthvalue_false_node
1523 : op0 == truthvalue_true_node)))
1524 *maybe_const_itself &= op1_const_self;
1525 goto out;
1526
1527 case COND_EXPR:
1528 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1529 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1530 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1531 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1532 for_int_const);
672d914b 1533
f59e3889 1534 STRIP_TYPE_NOPS (op0);
fc501191 1535 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1536 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1537 for_int_const);
f59e3889 1538 STRIP_TYPE_NOPS (op1);
fc501191 1539 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1540
fc501191 1541 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1542 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1543 for_int_const);
f59e3889 1544 STRIP_TYPE_NOPS (op2);
fc501191 1545 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1546
aac24642 1547 if (for_int_const
1548 && (TREE_CODE (op0) != INTEGER_CST
1549 /* Only the evaluated operand must be an INTEGER_CST. */
1550 || (op0 == truthvalue_true_node
1551 ? TREE_CODE (op1) != INTEGER_CST
1552 : TREE_CODE (op2) != INTEGER_CST)))
1553 goto out;
1554
a75b1c71 1555 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1556 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1557 else
1558 ret = fold (expr);
1559 *maybe_const_operands &= op0_const;
1560 *maybe_const_itself &= op0_const_self;
1561 if (!(flag_isoc99
1562 && op0_const
1563 && op0_const_self
1564 && op0 == truthvalue_false_node))
1565 *maybe_const_operands &= op1_const;
1566 if (!(op0_const
1567 && op0_const_self
1568 && op0 == truthvalue_false_node))
1569 *maybe_const_itself &= op1_const_self;
1570 if (!(flag_isoc99
1571 && op0_const
1572 && op0_const_self
1573 && op0 == truthvalue_true_node))
1574 *maybe_const_operands &= op2_const;
1575 if (!(op0_const
1576 && op0_const_self
1577 && op0 == truthvalue_true_node))
1578 *maybe_const_itself &= op2_const_self;
1579 goto out;
1580
c6418a4e 1581 case EXCESS_PRECISION_EXPR:
1582 /* Each case where an operand with excess precision may be
1583 encountered must remove the EXCESS_PRECISION_EXPR around
1584 inner operands and possibly put one around the whole
1585 expression or possibly convert to the semantic type (which
1586 c_fully_fold does); we cannot tell at this stage which is
1587 appropriate in any particular case. */
1588 gcc_unreachable ();
1589
a75b1c71 1590 default:
1591 /* Various codes may appear through folding built-in functions
1592 and their arguments. */
1593 goto out;
1594 }
1595
1596 out:
1597 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1598 have been done by this point, so remove them again. */
1599 nowarning |= TREE_NO_WARNING (ret);
1600 STRIP_TYPE_NOPS (ret);
1601 if (nowarning && !TREE_NO_WARNING (ret))
1602 {
1603 if (!CAN_HAVE_LOCATION_P (ret))
1604 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1605 TREE_NO_WARNING (ret) = 1;
1606 }
1607 if (ret != expr)
1608 protected_set_expr_location (ret, loc);
1609 return ret;
1610}
1611
1612/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1613 return EXP. Otherwise, return either EXP or its known constant
1614 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1615 Is the BLKmode test appropriate? */
1616
1617tree
1618decl_constant_value_for_optimization (tree exp)
1619{
1620 tree ret;
1621
1622 /* This function is only used by C, for c_fully_fold and other
1623 optimization, and may not be correct for C++. */
1624 if (c_dialect_cxx ())
1625 gcc_unreachable ();
1626
1627 if (!optimize
1628 || TREE_CODE (exp) != VAR_DECL
1629 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1630 || DECL_MODE (exp) == BLKmode)
1631 return exp;
1632
1633 ret = decl_constant_value (exp);
1634 /* Avoid unwanted tree sharing between the initializer and current
1635 function's body where the tree can be modified e.g. by the
1636 gimplifier. */
1637 if (ret != exp && TREE_STATIC (exp))
1638 ret = unshare_expr (ret);
1639 return ret;
1640}
1641
2a1736ed 1642/* Print a warning if a constant expression had overflow in folding.
1643 Invoke this function on every expression that the language
1644 requires to be a constant expression.
1645 Note the ANSI C standard says it is erroneous for a
1646 constant expression to overflow. */
b2806639 1647
1648void
1cae46be 1649constant_expression_warning (tree value)
07317e69 1650{
48e1416a 1651 if (warn_overflow && pedantic
07317e69 1652 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1653 || TREE_CODE (value) == FIXED_CST
1654 || TREE_CODE (value) == VECTOR_CST
1655 || TREE_CODE (value) == COMPLEX_CST)
1656 && TREE_OVERFLOW (value))
21ca8540 1657 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1658}
1659
1660/* The same as above but print an unconditional error. */
1661void
1662constant_expression_error (tree value)
b2806639 1663{
837e1122 1664 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1665 || TREE_CODE (value) == FIXED_CST
886cfd4f 1666 || TREE_CODE (value) == VECTOR_CST
837e1122 1667 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1668 && TREE_OVERFLOW (value))
1669 error ("overflow in constant expression");
2a1736ed 1670}
1671
f170d67f 1672/* Print a warning if an expression had overflow in folding and its
1673 operands hadn't.
1674
2a1736ed 1675 Invoke this function on every expression that
1676 (1) appears in the source code, and
f170d67f 1677 (2) is a constant expression that overflowed, and
2a1736ed 1678 (3) is not already checked by convert_and_check;
f170d67f 1679 however, do not invoke this function on operands of explicit casts
1680 or when the expression is the result of an operator and any operand
1681 already overflowed. */
2a1736ed 1682
1683void
e60a6f7b 1684overflow_warning (location_t loc, tree value)
2a1736ed 1685{
48d94ede 1686 if (c_inhibit_evaluation_warnings != 0)
1687 return;
f170d67f 1688
1689 switch (TREE_CODE (value))
886cfd4f 1690 {
f170d67f 1691 case INTEGER_CST:
e60a6f7b 1692 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1693 break;
48e1416a 1694
f170d67f 1695 case REAL_CST:
e60a6f7b 1696 warning_at (loc, OPT_Woverflow,
1697 "floating point overflow in expression");
f170d67f 1698 break;
48e1416a 1699
9421ebb9 1700 case FIXED_CST:
e60a6f7b 1701 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1702 break;
1703
f170d67f 1704 case VECTOR_CST:
e60a6f7b 1705 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1706 break;
48e1416a 1707
f170d67f 1708 case COMPLEX_CST:
1709 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1710 warning_at (loc, OPT_Woverflow,
1711 "complex integer overflow in expression");
f170d67f 1712 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1713 warning_at (loc, OPT_Woverflow,
1714 "complex floating point overflow in expression");
f170d67f 1715 break;
1716
1717 default:
1718 break;
886cfd4f 1719 }
2a1736ed 1720}
1721
03033af4 1722/* Warn about uses of logical || / && operator in a context where it
1723 is likely that the bitwise equivalent was intended by the
1724 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1725 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1726 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1727void
9c20c4fc 1728warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1729 enum tree_code code_left, tree op_left,
03033af4 1730 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1731{
9c20c4fc 1732 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1733 int in0_p, in1_p, in_p;
1734 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1735 bool strict_overflow_p = false;
1736
03033af4 1737 if (code != TRUTH_ANDIF_EXPR
1738 && code != TRUTH_AND_EXPR
1739 && code != TRUTH_ORIF_EXPR
1740 && code != TRUTH_OR_EXPR)
1741 return;
1742
439606a9 1743 /* We don't want to warn if either operand comes from a macro
1744 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1745 see PR61534. */
1746 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1747 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1748 return;
1749
03033af4 1750 /* Warn if &&/|| are being used in a context where it is
1751 likely that the bitwise equivalent was intended by the
1752 programmer. That is, an expression such as op && MASK
1753 where op should not be any boolean expression, nor a
1754 constant, and mask seems to be a non-boolean integer constant. */
1755 if (!truth_value_p (code_left)
1756 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1757 && !CONSTANT_CLASS_P (op_left)
1758 && !TREE_NO_WARNING (op_left)
1759 && TREE_CODE (op_right) == INTEGER_CST
1760 && !integer_zerop (op_right)
1761 && !integer_onep (op_right))
b13d1547 1762 {
9c20c4fc 1763 if (or_op)
03033af4 1764 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1765 " applied to non-boolean constant");
1766 else
1767 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1768 " applied to non-boolean constant");
1769 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1770 return;
1771 }
1772
1773 /* We do not warn for constants because they are typical of macro
1774 expansions that test for features. */
1775 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1776 return;
1777
1778 /* This warning only makes sense with logical operands. */
1779 if (!(truth_value_p (TREE_CODE (op_left))
1780 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1781 || !(truth_value_p (TREE_CODE (op_right))
1782 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1783 return;
1784
5e84569c 1785 /* The range computations only work with scalars. */
1786 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1787 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1788 return;
9c20c4fc 1789
686369e8 1790 /* We first test whether either side separately is trivially true
1791 (with OR) or trivially false (with AND). If so, do not warn.
1792 This is a common idiom for testing ranges of data types in
1793 portable code. */
1794 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1795 if (!lhs)
1796 return;
1797 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1798 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1799
686369e8 1800 /* If this is an OR operation, invert both sides; now, the result
1801 should be always false to get a warning. */
1802 if (or_op)
1803 in0_p = !in0_p;
1804
1805 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1806 if (tem && integer_zerop (tem))
686369e8 1807 return;
1808
1809 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1810 if (!rhs)
1811 return;
1812 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1813 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1814
686369e8 1815 /* If this is an OR operation, invert both sides; now, the result
1816 should be always false to get a warning. */
9c20c4fc 1817 if (or_op)
686369e8 1818 in1_p = !in1_p;
48e1416a 1819
686369e8 1820 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1821 if (tem && integer_zerop (tem))
686369e8 1822 return;
1823
1824 /* If both expressions have the same operand, if we can merge the
485f6b9c 1825 ranges, ... */
686369e8 1826 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1827 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1828 in1_p, low1, high1))
9c20c4fc 1829 {
485f6b9c 1830 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1831 /* ... and if the range test is always false, then warn. */
1832 if (tem && integer_zerop (tem))
1833 {
1834 if (or_op)
1835 warning_at (location, OPT_Wlogical_op,
1836 "logical %<or%> of collectively exhaustive tests is "
1837 "always true");
1838 else
1839 warning_at (location, OPT_Wlogical_op,
1840 "logical %<and%> of mutually exclusive tests is "
1841 "always false");
1842 }
1843 /* Or warn if the operands have exactly the same range, e.g.
1844 A > 0 && A > 0. */
1845 else if (low0 == low1 && high0 == high1)
1846 {
1847 if (or_op)
1848 warning_at (location, OPT_Wlogical_op,
1849 "logical %<or%> of equal expressions");
1850 else
1851 warning_at (location, OPT_Wlogical_op,
1852 "logical %<and%> of equal expressions");
1853 }
b13d1547 1854 }
1855}
1856
32dc1512 1857/* Warn about logical not used on the left hand side operand of a comparison.
1858 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1859 Do not warn if RHS is of a boolean type. */
32dc1512 1860
1861void
1862warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1863 tree rhs)
32dc1512 1864{
dc6229e8 1865 if (TREE_CODE_CLASS (code) != tcc_comparison
1866 || TREE_TYPE (rhs) == NULL_TREE
1867 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1868 return;
1869
16f958b3 1870 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1871 !(x == 0) or !(y != 0). */
1872 if ((code == EQ_EXPR || code == NE_EXPR)
1873 && integer_zerop (rhs))
1874 return;
1875
32dc1512 1876 warning_at (location, OPT_Wlogical_not_parentheses,
1877 "logical not is only applied to the left hand side of "
1878 "comparison");
1879}
b13d1547 1880
3d177e8c 1881/* Warn if EXP contains any computations whose results are not used.
1882 Return true if a warning is printed; false otherwise. LOCUS is the
1883 (potential) location of the expression. */
1884
1885bool
1886warn_if_unused_value (const_tree exp, location_t locus)
1887{
1888 restart:
1889 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1890 return false;
1891
1892 /* Don't warn about void constructs. This includes casting to void,
1893 void function calls, and statement expressions with a final cast
1894 to void. */
1895 if (VOID_TYPE_P (TREE_TYPE (exp)))
1896 return false;
1897
1898 if (EXPR_HAS_LOCATION (exp))
1899 locus = EXPR_LOCATION (exp);
1900
1901 switch (TREE_CODE (exp))
1902 {
1903 case PREINCREMENT_EXPR:
1904 case POSTINCREMENT_EXPR:
1905 case PREDECREMENT_EXPR:
1906 case POSTDECREMENT_EXPR:
1907 case MODIFY_EXPR:
1908 case INIT_EXPR:
1909 case TARGET_EXPR:
1910 case CALL_EXPR:
1911 case TRY_CATCH_EXPR:
1912 case WITH_CLEANUP_EXPR:
1913 case EXIT_EXPR:
1914 case VA_ARG_EXPR:
1915 return false;
1916
1917 case BIND_EXPR:
1918 /* For a binding, warn if no side effect within it. */
1919 exp = BIND_EXPR_BODY (exp);
1920 goto restart;
1921
1922 case SAVE_EXPR:
1923 case NON_LVALUE_EXPR:
d85dbdb3 1924 case NOP_EXPR:
3d177e8c 1925 exp = TREE_OPERAND (exp, 0);
1926 goto restart;
1927
1928 case TRUTH_ORIF_EXPR:
1929 case TRUTH_ANDIF_EXPR:
1930 /* In && or ||, warn if 2nd operand has no side effect. */
1931 exp = TREE_OPERAND (exp, 1);
1932 goto restart;
1933
1934 case COMPOUND_EXPR:
1935 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1936 return true;
1937 /* Let people do `(foo (), 0)' without a warning. */
1938 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1939 return false;
1940 exp = TREE_OPERAND (exp, 1);
1941 goto restart;
1942
1943 case COND_EXPR:
1944 /* If this is an expression with side effects, don't warn; this
1945 case commonly appears in macro expansions. */
1946 if (TREE_SIDE_EFFECTS (exp))
1947 return false;
1948 goto warn;
1949
1950 case INDIRECT_REF:
1951 /* Don't warn about automatic dereferencing of references, since
1952 the user cannot control it. */
1953 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1954 {
1955 exp = TREE_OPERAND (exp, 0);
1956 goto restart;
1957 }
1958 /* Fall through. */
1959
1960 default:
1961 /* Referencing a volatile value is a side effect, so don't warn. */
1962 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1963 && TREE_THIS_VOLATILE (exp))
1964 return false;
1965
1966 /* If this is an expression which has no operands, there is no value
1967 to be unused. There are no such language-independent codes,
1968 but front ends may define such. */
1969 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1970 return false;
1971
1972 warn:
1973 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1974 }
1975}
1976
1977
bcf22371 1978/* Print a warning about casts that might indicate violation
1979 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1980 strict aliasing mode is in effect. OTYPE is the original
1981 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1982
e6fa0ea6 1983bool
1e31ff37 1984strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1985{
45bb3afb 1986 /* Strip pointer conversion chains and get to the correct original type. */
1987 STRIP_NOPS (expr);
1988 otype = TREE_TYPE (expr);
1989
f06537f2 1990 if (!(flag_strict_aliasing
1991 && POINTER_TYPE_P (type)
1992 && POINTER_TYPE_P (otype)
1993 && !VOID_TYPE_P (TREE_TYPE (type)))
1994 /* If the type we are casting to is a ref-all pointer
1995 dereferencing it is always valid. */
1996 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1997 return false;
1998
1999 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 2000 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 2001 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 2002 {
2003 /* Casting the address of an object to non void pointer. Warn
2004 if the cast breaks type based aliasing. */
e6fa0ea6 2005 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2006 {
2007 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2008 "might break strict-aliasing rules");
2009 return true;
2010 }
bcf22371 2011 else
2012 {
48e1416a 2013 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2014 Only warn if the cast is dereferenced immediately. */
32c2fdea 2015 alias_set_type set1 =
e6fa0ea6 2016 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2017 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2018
62d823d0 2019 if (set1 != set2 && set2 != 0
2020 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2021 {
2022 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2023 "pointer will break strict-aliasing rules");
2024 return true;
2025 }
2026 else if (warn_strict_aliasing == 2
879f881c 2027 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2028 {
2029 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2030 "pointer might break strict-aliasing rules");
2031 return true;
2032 }
bcf22371 2033 }
2034 }
e6fa0ea6 2035 else
2036 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2037 {
2038 /* At this level, warn for any conversions, even if an address is
2039 not taken in the same statement. This will likely produce many
2040 false positives, but could be useful to pinpoint problems that
2041 are not revealed at higher levels. */
32c2fdea 2042 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2043 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2044 if (!COMPLETE_TYPE_P (type)
879f881c 2045 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2046 {
2047 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2048 "pointer might break strict-aliasing rules");
2049 return true;
2050 }
2051 }
2052
2053 return false;
bcf22371 2054}
2055
f003f5dc 2056/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2057 sizeof as last operand of certain builtins. */
2058
2059void
57f872a2 2060sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2061 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2062 bool (*comp_types) (tree, tree))
2063{
2064 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2065 bool strop = false, cmp = false;
2066 unsigned int idx = ~0;
2067 location_t loc;
f003f5dc 2068
2069 if (TREE_CODE (callee) != FUNCTION_DECL
2070 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2071 || vec_safe_length (params) <= 1)
f003f5dc 2072 return;
2073
f003f5dc 2074 switch (DECL_FUNCTION_CODE (callee))
2075 {
2076 case BUILT_IN_STRNCMP:
2077 case BUILT_IN_STRNCASECMP:
57f872a2 2078 cmp = true;
2079 /* FALLTHRU */
f003f5dc 2080 case BUILT_IN_STRNCPY:
57f872a2 2081 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2082 case BUILT_IN_STRNCAT:
57f872a2 2083 case BUILT_IN_STRNCAT_CHK:
2084 case BUILT_IN_STPNCPY:
2085 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2086 strop = true;
2087 /* FALLTHRU */
2088 case BUILT_IN_MEMCPY:
57f872a2 2089 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2090 case BUILT_IN_MEMMOVE:
57f872a2 2091 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2092 if (params->length () < 3)
57f872a2 2093 return;
f1f41a6c 2094 src = (*params)[1];
2095 dest = (*params)[0];
57f872a2 2096 idx = 2;
2097 break;
2098 case BUILT_IN_BCOPY:
f1f41a6c 2099 if (params->length () < 3)
57f872a2 2100 return;
f1f41a6c 2101 src = (*params)[0];
2102 dest = (*params)[1];
57f872a2 2103 idx = 2;
2104 break;
f003f5dc 2105 case BUILT_IN_MEMCMP:
57f872a2 2106 case BUILT_IN_BCMP:
f1f41a6c 2107 if (params->length () < 3)
f003f5dc 2108 return;
f1f41a6c 2109 src = (*params)[1];
2110 dest = (*params)[0];
57f872a2 2111 idx = 2;
2112 cmp = true;
f003f5dc 2113 break;
2114 case BUILT_IN_MEMSET:
57f872a2 2115 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2116 if (params->length () < 3)
f003f5dc 2117 return;
f1f41a6c 2118 dest = (*params)[0];
57f872a2 2119 idx = 2;
2120 break;
2121 case BUILT_IN_BZERO:
f1f41a6c 2122 dest = (*params)[0];
57f872a2 2123 idx = 1;
f003f5dc 2124 break;
2125 case BUILT_IN_STRNDUP:
f1f41a6c 2126 src = (*params)[0];
f003f5dc 2127 strop = true;
57f872a2 2128 idx = 1;
2129 break;
2130 case BUILT_IN_MEMCHR:
f1f41a6c 2131 if (params->length () < 3)
57f872a2 2132 return;
f1f41a6c 2133 src = (*params)[0];
57f872a2 2134 idx = 2;
2135 break;
2136 case BUILT_IN_SNPRINTF:
2137 case BUILT_IN_SNPRINTF_CHK:
2138 case BUILT_IN_VSNPRINTF:
2139 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2140 dest = (*params)[0];
57f872a2 2141 idx = 1;
2142 strop = true;
f003f5dc 2143 break;
2144 default:
2145 break;
2146 }
2147
57f872a2 2148 if (idx >= 3)
2149 return;
2150
2151 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2152 return;
2153
2154 type = TYPE_P (sizeof_arg[idx])
2155 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2156 if (!POINTER_TYPE_P (type))
2157 return;
2158
f003f5dc 2159 if (dest
2160 && (tem = tree_strip_nop_conversions (dest))
2161 && POINTER_TYPE_P (TREE_TYPE (tem))
2162 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2163 return;
2164
2165 if (src
2166 && (tem = tree_strip_nop_conversions (src))
2167 && POINTER_TYPE_P (TREE_TYPE (tem))
2168 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2169 return;
2170
57f872a2 2171 loc = sizeof_arg_loc[idx];
2172
2173 if (dest && !cmp)
f003f5dc 2174 {
57f872a2 2175 if (!TYPE_P (sizeof_arg[idx])
2176 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2177 && comp_types (TREE_TYPE (dest), type))
2178 {
57f872a2 2179 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2180 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2181 "argument to %<sizeof%> in %qD call is the same "
2182 "expression as the destination; did you mean to "
2183 "remove the addressof?", callee);
2184 else if ((TYPE_PRECISION (TREE_TYPE (type))
2185 == TYPE_PRECISION (char_type_node))
2186 || strop)
2187 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2188 "argument to %<sizeof%> in %qD call is the same "
2189 "expression as the destination; did you mean to "
2190 "provide an explicit length?", callee);
2191 else
2192 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2193 "argument to %<sizeof%> in %qD call is the same "
2194 "expression as the destination; did you mean to "
2195 "dereference it?", callee);
2196 return;
2197 }
2198
2199 if (POINTER_TYPE_P (TREE_TYPE (dest))
2200 && !strop
2201 && comp_types (TREE_TYPE (dest), type)
2202 && !VOID_TYPE_P (TREE_TYPE (type)))
2203 {
2204 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2205 "argument to %<sizeof%> in %qD call is the same "
2206 "pointer type %qT as the destination; expected %qT "
2207 "or an explicit length", callee, TREE_TYPE (dest),
2208 TREE_TYPE (TREE_TYPE (dest)));
2209 return;
2210 }
2211 }
2212
57f872a2 2213 if (src && !cmp)
f003f5dc 2214 {
57f872a2 2215 if (!TYPE_P (sizeof_arg[idx])
2216 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2217 && comp_types (TREE_TYPE (src), type))
2218 {
57f872a2 2219 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2220 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2221 "argument to %<sizeof%> in %qD call is the same "
2222 "expression as the source; did you mean to "
2223 "remove the addressof?", callee);
2224 else if ((TYPE_PRECISION (TREE_TYPE (type))
2225 == TYPE_PRECISION (char_type_node))
2226 || strop)
2227 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2228 "argument to %<sizeof%> in %qD call is the same "
2229 "expression as the source; did you mean to "
2230 "provide an explicit length?", callee);
2231 else
2232 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2233 "argument to %<sizeof%> in %qD call is the same "
2234 "expression as the source; did you mean to "
2235 "dereference it?", callee);
2236 return;
2237 }
2238
2239 if (POINTER_TYPE_P (TREE_TYPE (src))
2240 && !strop
2241 && comp_types (TREE_TYPE (src), type)
2242 && !VOID_TYPE_P (TREE_TYPE (type)))
2243 {
2244 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2245 "argument to %<sizeof%> in %qD call is the same "
2246 "pointer type %qT as the source; expected %qT "
2247 "or an explicit length", callee, TREE_TYPE (src),
2248 TREE_TYPE (TREE_TYPE (src)));
2249 return;
2250 }
2251 }
57f872a2 2252
2253 if (dest)
2254 {
2255 if (!TYPE_P (sizeof_arg[idx])
2256 && operand_equal_p (dest, sizeof_arg[idx], 0)
2257 && comp_types (TREE_TYPE (dest), type))
2258 {
2259 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2260 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2261 "argument to %<sizeof%> in %qD call is the same "
2262 "expression as the first source; did you mean to "
2263 "remove the addressof?", callee);
2264 else if ((TYPE_PRECISION (TREE_TYPE (type))
2265 == TYPE_PRECISION (char_type_node))
2266 || strop)
2267 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2268 "argument to %<sizeof%> in %qD call is the same "
2269 "expression as the first source; did you mean to "
2270 "provide an explicit length?", callee);
2271 else
2272 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2273 "argument to %<sizeof%> in %qD call is the same "
2274 "expression as the first source; did you mean to "
2275 "dereference it?", callee);
2276 return;
2277 }
2278
2279 if (POINTER_TYPE_P (TREE_TYPE (dest))
2280 && !strop
2281 && comp_types (TREE_TYPE (dest), type)
2282 && !VOID_TYPE_P (TREE_TYPE (type)))
2283 {
2284 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2285 "argument to %<sizeof%> in %qD call is the same "
2286 "pointer type %qT as the first source; expected %qT "
2287 "or an explicit length", callee, TREE_TYPE (dest),
2288 TREE_TYPE (TREE_TYPE (dest)));
2289 return;
2290 }
2291 }
2292
2293 if (src)
2294 {
2295 if (!TYPE_P (sizeof_arg[idx])
2296 && operand_equal_p (src, sizeof_arg[idx], 0)
2297 && comp_types (TREE_TYPE (src), type))
2298 {
2299 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2300 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2301 "argument to %<sizeof%> in %qD call is the same "
2302 "expression as the second source; did you mean to "
2303 "remove the addressof?", callee);
2304 else if ((TYPE_PRECISION (TREE_TYPE (type))
2305 == TYPE_PRECISION (char_type_node))
2306 || strop)
2307 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2308 "argument to %<sizeof%> in %qD call is the same "
2309 "expression as the second source; did you mean to "
2310 "provide an explicit length?", callee);
2311 else
2312 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2313 "argument to %<sizeof%> in %qD call is the same "
2314 "expression as the second source; did you mean to "
2315 "dereference it?", callee);
2316 return;
2317 }
2318
2319 if (POINTER_TYPE_P (TREE_TYPE (src))
2320 && !strop
2321 && comp_types (TREE_TYPE (src), type)
2322 && !VOID_TYPE_P (TREE_TYPE (type)))
2323 {
2324 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2325 "argument to %<sizeof%> in %qD call is the same "
2326 "pointer type %qT as the second source; expected %qT "
2327 "or an explicit length", callee, TREE_TYPE (src),
2328 TREE_TYPE (TREE_TYPE (src)));
2329 return;
2330 }
2331 }
2332
f003f5dc 2333}
2334
3f08e399 2335/* Warn for unlikely, improbable, or stupid DECL declarations
2336 of `main'. */
2337
2338void
2339check_main_parameter_types (tree decl)
2340{
d0af78c5 2341 function_args_iterator iter;
2342 tree type;
3f08e399 2343 int argct = 0;
2344
d0af78c5 2345 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2346 {
2347 /* XXX void_type_node belies the abstraction. */
2348 if (type == void_type_node || type == error_mark_node )
2349 break;
2350
2026249a 2351 tree t = type;
2352 if (TYPE_ATOMIC (t))
2353 pedwarn (input_location, OPT_Wmain,
2354 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2355 type, decl);
2356 while (POINTER_TYPE_P (t))
2357 {
2358 t = TREE_TYPE (t);
2359 if (TYPE_ATOMIC (t))
2360 pedwarn (input_location, OPT_Wmain,
2361 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2362 type, decl);
2363 }
2364
d0af78c5 2365 ++argct;
2366 switch (argct)
2367 {
2368 case 1:
2369 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2370 pedwarn (input_location, OPT_Wmain,
2371 "first argument of %q+D should be %<int%>", decl);
2372 break;
2373
2374 case 2:
2375 if (TREE_CODE (type) != POINTER_TYPE
2376 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2377 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2378 != char_type_node))
2379 pedwarn (input_location, OPT_Wmain,
2380 "second argument of %q+D should be %<char **%>", decl);
2381 break;
2382
2383 case 3:
2384 if (TREE_CODE (type) != POINTER_TYPE
2385 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2386 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2387 != char_type_node))
2388 pedwarn (input_location, OPT_Wmain,
2389 "third argument of %q+D should probably be "
2390 "%<char **%>", decl);
2391 break;
2392 }
2393 }
3f08e399 2394
2395 /* It is intentional that this message does not mention the third
2396 argument because it's only mentioned in an appendix of the
2397 standard. */
2398 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2399 pedwarn (input_location, OPT_Wmain,
2400 "%q+D takes only zero or two arguments", decl);
90e645fa 2401
2402 if (stdarg_p (TREE_TYPE (decl)))
2403 pedwarn (input_location, OPT_Wmain,
2404 "%q+D declared as variadic function", decl);
3f08e399 2405}
2406
73437615 2407/* vector_targets_convertible_p is used for vector pointer types. The
2408 callers perform various checks that the qualifiers are satisfactory,
2409 while OTOH vector_targets_convertible_p ignores the number of elements
2410 in the vectors. That's fine with vector pointers as we can consider,
2411 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2412 and that does not require and conversion of the pointer values.
2413 In contrast, vector_types_convertible_p and
2414 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2415/* True if pointers to distinct types T1 and T2 can be converted to
2416 each other without an explicit cast. Only returns true for opaque
2417 vector types. */
2418bool
2419vector_targets_convertible_p (const_tree t1, const_tree t2)
2420{
2421 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
8d125f7d 2422 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2423 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2424 return true;
2425
2426 return false;
2427}
2428
73437615 2429/* vector_types_convertible_p is used for vector value types.
2430 It could in principle call vector_targets_convertible_p as a subroutine,
2431 but then the check for vector type would be duplicated with its callers,
2432 and also the purpose of vector_targets_convertible_p would become
2433 muddled.
2434 Where vector_types_convertible_p returns true, a conversion might still be
2435 needed to make the types match.
2436 In contrast, vector_targets_convertible_p is used for vector pointer
2437 values, and vector_types_compatible_elements_p is used specifically
2438 in the context for binary operators, as a check if use is possible without
2439 conversion. */
546c4794 2440/* True if vector types T1 and T2 can be converted to each other
2441 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2442 can only be converted with -flax-vector-conversions yet that is not
2443 in effect, emit a note telling the user about that option if such
2444 a note has not previously been emitted. */
2445bool
9f627b1a 2446vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2447{
546c4794 2448 static bool emitted_lax_note = false;
ae6db8ab 2449 bool convertible_lax;
2450
8d125f7d 2451 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2452 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2453 return true;
2454
2455 convertible_lax =
2456 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2457 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2458 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2459 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2460 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2461
2462 if (!convertible_lax || flag_lax_vector_conversions)
2463 return convertible_lax;
2464
2465 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2466 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2467 return true;
2468
2469 if (emit_lax_note && !emitted_lax_note)
2470 {
2471 emitted_lax_note = true;
5bcc316e 2472 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2473 "conversions between vectors with differing "
2474 "element types or numbers of subparts");
2475 }
2476
2477 return false;
8b4b9810 2478}
2479
bf0cb017 2480/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2481 and have vector types, V0 has the same type as V1, and the number of
2482 elements of V0, V1, MASK is the same.
2483
2484 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2485 called with two arguments. In this case implementation passes the
2486 first argument twice in order to share the same tree code. This fact
2487 could enable the mask-values being twice the vector length. This is
2488 an implementation accident and this semantics is not guaranteed to
2489 the user. */
2490tree
68ea4406 2491c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2492 bool complain)
bf0cb017 2493{
2494 tree ret;
2495 bool wrap = true;
2496 bool maybe_const = false;
2497 bool two_arguments = false;
2498
2499 if (v1 == NULL_TREE)
2500 {
2501 two_arguments = true;
2502 v1 = v0;
2503 }
2504
2505 if (v0 == error_mark_node || v1 == error_mark_node
2506 || mask == error_mark_node)
2507 return error_mark_node;
2508
2509 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2510 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2511 {
68ea4406 2512 if (complain)
2513 error_at (loc, "__builtin_shuffle last argument must "
2514 "be an integer vector");
bf0cb017 2515 return error_mark_node;
2516 }
2517
2518 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2519 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2520 {
68ea4406 2521 if (complain)
2522 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2523 return error_mark_node;
2524 }
2525
2526 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2527 {
68ea4406 2528 if (complain)
2529 error_at (loc, "__builtin_shuffle argument vectors must be of "
2530 "the same type");
bf0cb017 2531 return error_mark_node;
2532 }
2533
2534 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2535 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2536 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2537 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2538 {
68ea4406 2539 if (complain)
2540 error_at (loc, "__builtin_shuffle number of elements of the "
2541 "argument vector(s) and the mask vector should "
2542 "be the same");
bf0cb017 2543 return error_mark_node;
2544 }
2545
2546 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2547 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2548 {
68ea4406 2549 if (complain)
2550 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2551 "must have the same size as inner type of the mask");
bf0cb017 2552 return error_mark_node;
2553 }
2554
2555 if (!c_dialect_cxx ())
2556 {
2557 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2558 v0 = c_fully_fold (v0, false, &maybe_const);
2559 wrap &= maybe_const;
2560
2561 if (two_arguments)
2562 v1 = v0 = save_expr (v0);
2563 else
2564 {
2565 v1 = c_fully_fold (v1, false, &maybe_const);
2566 wrap &= maybe_const;
2567 }
2568
2569 mask = c_fully_fold (mask, false, &maybe_const);
2570 wrap &= maybe_const;
2571 }
68ea4406 2572 else if (two_arguments)
2573 v1 = v0 = save_expr (v0);
bf0cb017 2574
2575 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2576
2577 if (!c_dialect_cxx () && !wrap)
2578 ret = c_wrap_maybe_const (ret, true);
2579
2580 return ret;
2581}
2582
7f506bca 2583/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2584 to integral type. */
2585
2586static tree
2587c_common_get_narrower (tree op, int *unsignedp_ptr)
2588{
2589 op = get_narrower (op, unsignedp_ptr);
2590
2591 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2592 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2593 {
2594 /* C++0x scoped enumerations don't implicitly convert to integral
2595 type; if we stripped an explicit conversion to a larger type we
2596 need to replace it so common_type will still work. */
a51edb4c 2597 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2598 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2599 op = fold_convert (type, op);
2600 }
2601 return op;
2602}
2603
2561cea2 2604/* This is a helper function of build_binary_op.
2605
2606 For certain operations if both args were extended from the same
2607 smaller type, do the arithmetic in that type and then extend.
2608
2609 BITWISE indicates a bitwise operation.
2610 For them, this optimization is safe only if
2611 both args are zero-extended or both are sign-extended.
2612 Otherwise, we might change the result.
2613 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2614 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2615*/
7f506bca 2616tree
2617shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2618{
2619 int unsigned0, unsigned1;
2620 tree arg0, arg1;
2621 int uns;
2622 tree type;
2623
2624 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2625 excessive narrowing when we call get_narrower below. For
2626 example, suppose that OP0 is of unsigned int extended
2627 from signed char and that RESULT_TYPE is long long int.
2628 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2629 like
48e1416a 2630
2561cea2 2631 (long long int) (unsigned int) signed_char
2632
2633 which get_narrower would narrow down to
48e1416a 2634
2561cea2 2635 (unsigned int) signed char
48e1416a 2636
2561cea2 2637 If we do not cast OP0 first, get_narrower would return
2638 signed_char, which is inconsistent with the case of the
2639 explicit cast. */
2640 op0 = convert (result_type, op0);
2641 op1 = convert (result_type, op1);
2642
7f506bca 2643 arg0 = c_common_get_narrower (op0, &unsigned0);
2644 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2645
2561cea2 2646 /* UNS is 1 if the operation to be done is an unsigned one. */
2647 uns = TYPE_UNSIGNED (result_type);
2648
2649 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2650 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2651
2561cea2 2652 if ((TYPE_PRECISION (TREE_TYPE (op0))
2653 == TYPE_PRECISION (TREE_TYPE (arg0)))
2654 && TREE_TYPE (op0) != result_type)
2655 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2656 if ((TYPE_PRECISION (TREE_TYPE (op1))
2657 == TYPE_PRECISION (TREE_TYPE (arg1)))
2658 && TREE_TYPE (op1) != result_type)
2659 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2660
2561cea2 2661 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2662
2561cea2 2663 /* For bitwise operations, signedness of nominal type
2664 does not matter. Consider only how operands were extended. */
2665 if (bitwise)
2666 uns = unsigned0;
48e1416a 2667
2561cea2 2668 /* Note that in all three cases below we refrain from optimizing
2669 an unsigned operation on sign-extended args.
2670 That would not be valid. */
48e1416a 2671
2561cea2 2672 /* Both args variable: if both extended in same way
2673 from same width, do it in that width.
2674 Do it unsigned if args were zero-extended. */
2675 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2676 < TYPE_PRECISION (result_type))
2677 && (TYPE_PRECISION (TREE_TYPE (arg1))
2678 == TYPE_PRECISION (TREE_TYPE (arg0)))
2679 && unsigned0 == unsigned1
2680 && (unsigned0 || !uns))
2681 return c_common_signed_or_unsigned_type
2682 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2683
2684 else if (TREE_CODE (arg0) == INTEGER_CST
2685 && (unsigned1 || !uns)
2686 && (TYPE_PRECISION (TREE_TYPE (arg1))
2687 < TYPE_PRECISION (result_type))
2688 && (type
2689 = c_common_signed_or_unsigned_type (unsigned1,
2690 TREE_TYPE (arg1)))
2691 && !POINTER_TYPE_P (type)
2692 && int_fits_type_p (arg0, type))
2693 return type;
2694
2695 else if (TREE_CODE (arg1) == INTEGER_CST
2696 && (unsigned0 || !uns)
2697 && (TYPE_PRECISION (TREE_TYPE (arg0))
2698 < TYPE_PRECISION (result_type))
2699 && (type
2700 = c_common_signed_or_unsigned_type (unsigned0,
2701 TREE_TYPE (arg0)))
2702 && !POINTER_TYPE_P (type)
2703 && int_fits_type_p (arg1, type))
2704 return type;
2705
2706 return result_type;
2707}
2708
e53013a8 2709/* Returns true iff any integer value of type FROM_TYPE can be represented as
2710 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2711
2712static bool
2713int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2714{
2715 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2716 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2717 REAL_VALUE_TYPE real_low_bound =
2718 real_value_from_int_cst (0, type_low_bound);
2719 REAL_VALUE_TYPE real_high_bound =
2720 real_value_from_int_cst (0, type_high_bound);
2721
2722 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2723 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2724}
2725
2726/* Checks if expression EXPR of complex/real/integer type cannot be converted
2727 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2728 * EXPR is a constant which cannot be exactly converted to TYPE.
2729 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2730 for EXPR type and TYPE being both integers or both real, or both
2731 complex.
2732 * EXPR is not a constant of complex type and TYPE is a real or
2733 an integer.
22a75734 2734 * EXPR is not a constant of real type and TYPE is an integer.
2735 * EXPR is not a constant of integer type which cannot be
2736 exactly converted to real type.
e53013a8 2737
7dfa155b 2738 Function allows conversions between types of different signedness and
ca9d7d74 2739 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2740 signedness warnings if PRODUCE_WARNS is true.
2741
2742 Function allows conversions from complex constants to non-complex types,
2743 provided that imaginary part is zero and real part can be safely converted
2744 to TYPE. */
22a75734 2745
ca9d7d74 2746enum conversion_safety
22a75734 2747unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2748{
ca9d7d74 2749 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2750 tree expr_type = TREE_TYPE (expr);
22a75734 2751 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2752
7dfa155b 2753 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2754 {
e53013a8 2755 /* If type is complex, we are interested in compatibility with
2756 underlying type. */
2757 if (TREE_CODE (type) == COMPLEX_TYPE)
2758 type = TREE_TYPE (type);
2759
d31d55f0 2760 /* Warn for real constant that is not an exact integer converted
7dfa155b 2761 to integer type. */
27259707 2762 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2763 && TREE_CODE (type) == INTEGER_TYPE)
2764 {
2765 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2766 give_warning = UNSAFE_REAL;
7dfa155b 2767 }
da1fb07b 2768 /* Warn for an integer constant that does not fit into integer type. */
27259707 2769 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2770 && TREE_CODE (type) == INTEGER_TYPE
2771 && !int_fits_type_p (expr, type))
2772 {
2773 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2774 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2775 {
2776 if (produce_warns)
2777 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2778 " implicitly converted to unsigned type");
2779 }
2780 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2781 {
2782 if (produce_warns)
2783 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2784 " constant value to negative integer");
2785 }
7ee0d227 2786 else
ca9d7d74 2787 give_warning = UNSAFE_OTHER;
7dfa155b 2788 }
d31d55f0 2789 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2790 {
2791 /* Warn for an integer constant that does not fit into real type. */
2792 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2793 {
2794 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2795 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2796 give_warning = UNSAFE_REAL;
7dfa155b 2797 }
2798 /* Warn for a real constant that does not fit into a smaller
2799 real type. */
2800 else if (TREE_CODE (expr_type) == REAL_TYPE
2801 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2802 {
2803 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2804 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2805 give_warning = UNSAFE_REAL;
7dfa155b 2806 }
2807 }
2808 }
e53013a8 2809
2810 else if (TREE_CODE (expr) == COMPLEX_CST)
2811 {
2812 tree imag_part = TREE_IMAGPART (expr);
2813 /* Conversion from complex constant with zero imaginary part,
2814 perform check for conversion of real part. */
2815 if ((TREE_CODE (imag_part) == REAL_CST
2816 && real_zerop (imag_part))
2817 || (TREE_CODE (imag_part) == INTEGER_CST
2818 && integer_zerop (imag_part)))
2819 /* Note: in this branch we use recursive call to unsafe_conversion_p
2820 with different type of EXPR, but it is still safe, because when EXPR
2821 is a constant, it's type is not used in text of generated warnings
2822 (otherwise they could sound misleading). */
2823 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2824 produce_warns);
2825 /* Conversion from complex constant with non-zero imaginary part. */
2826 else
2827 {
2828 /* Conversion to complex type.
2829 Perform checks for both real and imaginary parts. */
2830 if (TREE_CODE (type) == COMPLEX_TYPE)
2831 {
2832 /* Unfortunately, produce_warns must be false in two subsequent
2833 calls of unsafe_conversion_p, because otherwise we could
2834 produce strange "double" warnings, if both real and imaginary
2835 parts have conversion problems related to signedness.
2836
2837 For example:
2838 int32_t _Complex a = 0x80000000 + 0x80000000i;
2839
2840 Possible solution: add a separate function for checking
2841 constants and combine result of two calls appropriately. */
2842 enum conversion_safety re_safety =
2843 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2844 enum conversion_safety im_safety =
2845 unsafe_conversion_p (loc, type, imag_part, false);
2846
2847 /* Merge the results into appropriate single warning. */
2848
2849 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2850 if (re_safety == im_safety)
2851 give_warning = re_safety;
2852 else if (!re_safety && im_safety)
2853 give_warning = im_safety;
2854 else if (re_safety && !im_safety)
2855 give_warning = re_safety;
2856 else
2857 give_warning = UNSAFE_OTHER;
2858 }
2859 /* Warn about conversion from complex to real or integer type. */
2860 else
2861 give_warning = UNSAFE_IMAGINARY;
2862 }
2863 }
2864
2865 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2866 else
2867 {
d31d55f0 2868 /* Warn for real types converted to integer types. */
2561cea2 2869 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2870 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2871 give_warning = UNSAFE_REAL;
d31d55f0 2872
2561cea2 2873 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2874 && TREE_CODE (type) == INTEGER_TYPE)
2875 {
69609004 2876 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2877 expr = get_unwidened (expr, 0);
2561cea2 2878 expr_type = TREE_TYPE (expr);
69609004 2879
2561cea2 2880 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2881 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2882 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2883 || TREE_CODE (expr) == BIT_XOR_EXPR)
2884 {
27259707 2885 /* If both args were extended from a shortest type,
2886 use that type if that is safe. */
48e1416a 2887 expr_type = shorten_binary_op (expr_type,
2888 TREE_OPERAND (expr, 0),
2889 TREE_OPERAND (expr, 1),
2561cea2 2890 /* bitwise */1);
2891
2561cea2 2892 if (TREE_CODE (expr) == BIT_AND_EXPR)
2893 {
2894 tree op0 = TREE_OPERAND (expr, 0);
2895 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2896 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2897 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2898
2899 /* If one of the operands is a non-negative constant
2900 that fits in the target type, then the type of the
2901 other operand does not matter. */
2561cea2 2902 if ((TREE_CODE (op0) == INTEGER_CST
2903 && int_fits_type_p (op0, c_common_signed_type (type))
2904 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2905 || (TREE_CODE (op1) == INTEGER_CST
27259707 2906 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2907 && int_fits_type_p (op1,
27259707 2908 c_common_unsigned_type (type))))
ca9d7d74 2909 return SAFE_CONVERSION;
30de145b 2910 /* If constant is unsigned and fits in the target
2911 type, then the result will also fit. */
2912 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2913 && unsigned0
30de145b 2914 && int_fits_type_p (op0, type))
2915 || (TREE_CODE (op1) == INTEGER_CST
2916 && unsigned1
2917 && int_fits_type_p (op1, type)))
ca9d7d74 2918 return SAFE_CONVERSION;
2561cea2 2919 }
2920 }
7dfa155b 2921 /* Warn for integer types converted to smaller integer types. */
48e1416a 2922 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2923 give_warning = UNSAFE_OTHER;
7ee0d227 2924
2925 /* When they are the same width but different signedness,
2926 then the value may change. */
7dfa155b 2927 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2928 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2929 /* Even when converted to a bigger type, if the type is
2930 unsigned but expr is signed, then negative values
2931 will be changed. */
7dfa155b 2932 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2933 && produce_warns)
200dd99c 2934 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2935 "may change the sign of the result",
2936 type, expr_type);
7dfa155b 2937 }
d31d55f0 2938
2939 /* Warn for integer types converted to real types if and only if
7dfa155b 2940 all the range of values of the integer type cannot be
2941 represented by the real type. */
2561cea2 2942 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2943 && TREE_CODE (type) == REAL_TYPE)
2944 {
4c2cfa81 2945 /* Don't warn about char y = 0xff; float x = (int) y; */
2946 expr = get_unwidened (expr, 0);
2947 expr_type = TREE_TYPE (expr);
2948
e53013a8 2949 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2950 give_warning = UNSAFE_OTHER;
7dfa155b 2951 }
d31d55f0 2952
2953 /* Warn for real types converted to smaller real types. */
2561cea2 2954 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2955 && TREE_CODE (type) == REAL_TYPE
2956 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2957 give_warning = UNSAFE_REAL;
e53013a8 2958
2959 /* Check conversion between two complex types. */
2960 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2961 && TREE_CODE (type) == COMPLEX_TYPE)
2962 {
2963 /* Extract underlying types (i.e., type of real and imaginary
2964 parts) of expr_type and type. */
2965 tree from_type = TREE_TYPE (expr_type);
2966 tree to_type = TREE_TYPE (type);
2967
2968 /* Warn for real types converted to integer types. */
2969 if (TREE_CODE (from_type) == REAL_TYPE
2970 && TREE_CODE (to_type) == INTEGER_TYPE)
2971 give_warning = UNSAFE_REAL;
2972
2973 /* Warn for real types converted to smaller real types. */
2974 else if (TREE_CODE (from_type) == REAL_TYPE
2975 && TREE_CODE (to_type) == REAL_TYPE
2976 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2977 give_warning = UNSAFE_REAL;
2978
2979 /* Check conversion for complex integer types. Here implementation
2980 is simpler than for real-domain integers because it does not
2981 involve sophisticated cases, such as bitmasks, casts, etc. */
2982 else if (TREE_CODE (from_type) == INTEGER_TYPE
2983 && TREE_CODE (to_type) == INTEGER_TYPE)
2984 {
2985 /* Warn for integer types converted to smaller integer types. */
2986 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2987 give_warning = UNSAFE_OTHER;
2988
2989 /* Check for different signedness, see case for real-domain
2990 integers (above) for a more detailed comment. */
2991 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2992 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2993 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2994 && produce_warns)
2995 warning_at (loc, OPT_Wsign_conversion,
2996 "conversion to %qT from %qT "
2997 "may change the sign of the result",
2998 type, expr_type);
2999 }
3000 else if (TREE_CODE (from_type) == INTEGER_TYPE
3001 && TREE_CODE (to_type) == REAL_TYPE
3002 && !int_safely_convertible_to_real_p (from_type, to_type))
3003 give_warning = UNSAFE_OTHER;
3004 }
3005
3006 /* Warn for complex types converted to real or integer types. */
3007 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3008 && TREE_CODE (type) != COMPLEX_TYPE)
3009 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3010 }
3011
3012 return give_warning;
3013}
3014
3015/* Warns if the conversion of EXPR to TYPE may alter a value.
3016 This is a helper function for warnings_for_convert_and_check. */
3017
3018static void
22a75734 3019conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3020{
7dfa155b 3021 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3022 enum conversion_safety conversion_kind;
d31d55f0 3023
ca9d7d74 3024 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3025 return;
d31d55f0 3026
ec704957 3027 /* This may happen, because for LHS op= RHS we preevaluate
3028 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3029 means we could no longer see the code of the EXPR. */
3030 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3031 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3032 if (TREE_CODE (expr) == SAVE_EXPR)
3033 expr = TREE_OPERAND (expr, 0);
3034
7dfa155b 3035 switch (TREE_CODE (expr))
3036 {
3037 case EQ_EXPR:
3038 case NE_EXPR:
3039 case LE_EXPR:
3040 case GE_EXPR:
3041 case LT_EXPR:
3042 case GT_EXPR:
3043 case TRUTH_ANDIF_EXPR:
3044 case TRUTH_ORIF_EXPR:
3045 case TRUTH_AND_EXPR:
3046 case TRUTH_OR_EXPR:
3047 case TRUTH_XOR_EXPR:
3048 case TRUTH_NOT_EXPR:
3049 /* Conversion from boolean to a signed:1 bit-field (which only
3050 can hold the values 0 and -1) doesn't lose information - but
3051 it does change the value. */
3052 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3053 warning_at (loc, OPT_Wconversion,
3054 "conversion to %qT from boolean expression", type);
3055 return;
3056
3057 case REAL_CST:
3058 case INTEGER_CST:
e53013a8 3059 case COMPLEX_CST:
22a75734 3060 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3061 if (conversion_kind == UNSAFE_REAL)
3062 warning_at (loc, OPT_Wfloat_conversion,
3063 "conversion to %qT alters %qT constant value",
3064 type, expr_type);
3065 else if (conversion_kind)
7dfa155b 3066 warning_at (loc, OPT_Wconversion,
3067 "conversion to %qT alters %qT constant value",
3068 type, expr_type);
3069 return;
3070
3071 case COND_EXPR:
3072 {
0e4e775a 3073 /* In case of COND_EXPR, we do not care about the type of
3074 COND_EXPR, only about the conversion of each operand. */
3075 tree op1 = TREE_OPERAND (expr, 1);
3076 tree op2 = TREE_OPERAND (expr, 2);
3077
22a75734 3078 conversion_warning (loc, type, op1);
3079 conversion_warning (loc, type, op2);
0e4e775a 3080 return;
7dfa155b 3081 }
3082
3083 default: /* 'expr' is not a constant. */
22a75734 3084 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3085 if (conversion_kind == UNSAFE_REAL)
3086 warning_at (loc, OPT_Wfloat_conversion,
3087 "conversion to %qT from %qT may alter its value",
3088 type, expr_type);
e53013a8 3089 else if (conversion_kind == UNSAFE_IMAGINARY)
3090 warning_at (loc, OPT_Wconversion,
3091 "conversion to %qT from %qT discards imaginary component",
3092 type, expr_type);
ca9d7d74 3093 else if (conversion_kind)
7dfa155b 3094 warning_at (loc, OPT_Wconversion,
200dd99c 3095 "conversion to %qT from %qT may alter its value",
3096 type, expr_type);
d31d55f0 3097 }
3098}
3099
59dd8856 3100/* Produce warnings after a conversion. RESULT is the result of
3101 converting EXPR to TYPE. This is a helper function for
3102 convert_and_check and cp_convert_and_check. */
2a1736ed 3103
59dd8856 3104void
22a75734 3105warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3106 tree result)
2a1736ed 3107{
22a75734 3108 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3109
da1fb07b 3110 if (TREE_CODE (expr) == INTEGER_CST
3111 && (TREE_CODE (type) == INTEGER_TYPE
3112 || TREE_CODE (type) == ENUMERAL_TYPE)
3113 && !int_fits_type_p (expr, type))
3114 {
d31d55f0 3115 /* Do not diagnose overflow in a constant expression merely
3116 because a conversion overflowed. */
da1fb07b 3117 if (TREE_OVERFLOW (result))
eddad94a 3118 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3119
da1fb07b 3120 if (TYPE_UNSIGNED (type))
d31d55f0 3121 {
da1fb07b 3122 /* This detects cases like converting -129 or 256 to
3123 unsigned char. */
3124 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3125 warning_at (loc, OPT_Woverflow,
3126 "large integer implicitly truncated to unsigned type");
7ee0d227 3127 else
22a75734 3128 conversion_warning (loc, type, expr);
da1fb07b 3129 }
48e1416a 3130 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3131 warning_at (loc, OPT_Woverflow,
e0913805 3132 "overflow in implicit constant conversion");
3133 /* No warning for converting 0x80000000 to int. */
3134 else if (pedantic
3135 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3136 || TYPE_PRECISION (TREE_TYPE (expr))
3137 != TYPE_PRECISION (type)))
61f69bc9 3138 warning_at (loc, OPT_Woverflow,
3139 "overflow in implicit constant conversion");
e0913805 3140
7ee0d227 3141 else
22a75734 3142 conversion_warning (loc, type, expr);
2a1736ed 3143 }
9421ebb9 3144 else if ((TREE_CODE (result) == INTEGER_CST
3145 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3146 warning_at (loc, OPT_Woverflow,
3147 "overflow in implicit constant conversion");
7ee0d227 3148 else
22a75734 3149 conversion_warning (loc, type, expr);
59dd8856 3150}
3151
3152
3153/* Convert EXPR to TYPE, warning about conversion problems with constants.
3154 Invoke this function on every expression that is converted implicitly,
3155 i.e. because of language rules and not because of an explicit cast. */
3156
3157tree
22a75734 3158convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3159{
3160 tree result;
c6418a4e 3161 tree expr_for_warning;
3162
3163 /* Convert from a value with possible excess precision rather than
3164 via the semantic type, but do not warn about values not fitting
3165 exactly in the semantic type. */
3166 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3167 {
3168 tree orig_type = TREE_TYPE (expr);
3169 expr = TREE_OPERAND (expr, 0);
3170 expr_for_warning = convert (orig_type, expr);
3171 if (orig_type == type)
3172 return expr_for_warning;
3173 }
3174 else
3175 expr_for_warning = expr;
59dd8856 3176
3177 if (TREE_TYPE (expr) == type)
3178 return expr;
48e1416a 3179
59dd8856 3180 result = convert (type, expr);
3181
48d94ede 3182 if (c_inhibit_evaluation_warnings == 0
3183 && !TREE_OVERFLOW_P (expr)
3184 && result != error_mark_node)
22a75734 3185 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3186
da1fb07b 3187 return result;
b2806639 3188}
3189\f
4e91a871 3190/* A node in a list that describes references to variables (EXPR), which are
3191 either read accesses if WRITER is zero, or write accesses, in which case
3192 WRITER is the parent of EXPR. */
3193struct tlist
3194{
3195 struct tlist *next;
3196 tree expr, writer;
3197};
3198
3199/* Used to implement a cache the results of a call to verify_tree. We only
3200 use this for SAVE_EXPRs. */
3201struct tlist_cache
3202{
3203 struct tlist_cache *next;
3204 struct tlist *cache_before_sp;
3205 struct tlist *cache_after_sp;
3206 tree expr;
481c6ce6 3207};
3208
4e91a871 3209/* Obstack to use when allocating tlist structures, and corresponding
3210 firstobj. */
3211static struct obstack tlist_obstack;
3212static char *tlist_firstobj = 0;
3213
3214/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3215 warnings. */
3216static struct tlist *warned_ids;
3217/* SAVE_EXPRs need special treatment. We process them only once and then
3218 cache the results. */
3219static struct tlist_cache *save_expr_cache;
3220
1cae46be 3221static void add_tlist (struct tlist **, struct tlist *, tree, int);
3222static void merge_tlist (struct tlist **, struct tlist *, int);
3223static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3224static int warning_candidate_p (tree);
79973b57 3225static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3226static void warn_for_collisions (struct tlist *);
3227static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3228static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3229
4e91a871 3230/* Create a new struct tlist and fill in its fields. */
3231static struct tlist *
1cae46be 3232new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3233{
3234 struct tlist *l;
9318f22c 3235 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3236 l->next = next;
3237 l->expr = t;
3238 l->writer = writer;
3239 return l;
3240}
3241
3242/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3243 is nonnull, we ignore any node we find which has a writer equal to it. */
3244
3245static void
1cae46be 3246add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3247{
3248 while (add)
3249 {
3250 struct tlist *next = add->next;
84166705 3251 if (!copy)
4e91a871 3252 add->next = *to;
79973b57 3253 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3254 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3255 add = next;
3256 }
3257}
3258
3259/* Merge the nodes of ADD into TO. This merging process is done so that for
3260 each variable that already exists in TO, no new node is added; however if
3261 there is a write access recorded in ADD, and an occurrence on TO is only
3262 a read access, then the occurrence in TO will be modified to record the
3263 write. */
481c6ce6 3264
3265static void
1cae46be 3266merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3267{
3268 struct tlist **end = to;
3269
3270 while (*end)
3271 end = &(*end)->next;
3272
3273 while (add)
3274 {
3275 int found = 0;
3276 struct tlist *tmp2;
3277 struct tlist *next = add->next;
3278
3279 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3280 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3281 {
3282 found = 1;
84166705 3283 if (!tmp2->writer)
4e91a871 3284 tmp2->writer = add->writer;
3285 }
84166705 3286 if (!found)
4e91a871 3287 {
312243bb 3288 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3289 end = &(*end)->next;
3290 *end = 0;
3291 }
3292 add = next;
3293 }
3294}
3295
3296/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3297 references in list LIST conflict with it, excluding reads if ONLY writers
3298 is nonzero. */
3299
3300static void
1cae46be 3301warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3302 int only_writes)
4e91a871 3303{
3304 struct tlist *tmp;
3305
3306 /* Avoid duplicate warnings. */
3307 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3308 if (candidate_equal_p (tmp->expr, written))
4e91a871 3309 return;
3310
3311 while (list)
3312 {
79973b57 3313 if (candidate_equal_p (list->expr, written)
3314 && !candidate_equal_p (list->writer, writer)
3315 && (!only_writes || list->writer))
4e91a871 3316 {
3317 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3318 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3319 OPT_Wsequence_point, "operation on %qE may be undefined",
3320 list->expr);
4e91a871 3321 }
3322 list = list->next;
3323 }
3324}
3325
3326/* Given a list LIST of references to variables, find whether any of these
3327 can cause conflicts due to missing sequence points. */
3328
3329static void
1cae46be 3330warn_for_collisions (struct tlist *list)
4e91a871 3331{
3332 struct tlist *tmp;
1cae46be 3333
4e91a871 3334 for (tmp = list; tmp; tmp = tmp->next)
3335 {
3336 if (tmp->writer)
3337 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3338 }
3339}
3340
734c98be 3341/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3342 warnings. */
3343static int
1cae46be 3344warning_candidate_p (tree x)
481c6ce6 3345{
6ef8d12f 3346 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3347 return 0;
3348
027fc6ef 3349 if (TREE_CODE (x) == BLOCK)
3350 return 0;
3351
6ef8d12f 3352 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3353 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3354 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3355 return 0;
3356
3357 if (!lvalue_p (x))
3358 return 0;
3359
3360 /* No point to track non-const calls, they will never satisfy
3361 operand_equal_p. */
3362 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3363 return 0;
3364
3365 if (TREE_CODE (x) == STRING_CST)
3366 return 0;
3367
3368 return 1;
79973b57 3369}
3370
3371/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3372static bool
3373candidate_equal_p (const_tree x, const_tree y)
3374{
3375 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3376}
481c6ce6 3377
4e91a871 3378/* Walk the tree X, and record accesses to variables. If X is written by the
3379 parent tree, WRITER is the parent.
3380 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3381 expression or its only operand forces a sequence point, then everything up
3382 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3383 in PNO_SP.
3384 Once we return, we will have emitted warnings if any subexpression before
3385 such a sequence point could be undefined. On a higher level, however, the
3386 sequence point may not be relevant, and we'll merge the two lists.
3387
3388 Example: (b++, a) + b;
3389 The call that processes the COMPOUND_EXPR will store the increment of B
3390 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3391 processes the PLUS_EXPR will need to merge the two lists so that
3392 eventually, all accesses end up on the same list (and we'll warn about the
3393 unordered subexpressions b++ and b.
3394
3395 A note on merging. If we modify the former example so that our expression
3396 becomes
3397 (b++, b) + a
3398 care must be taken not simply to add all three expressions into the final
3399 PNO_SP list. The function merge_tlist takes care of that by merging the
3400 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3401 way, so that no more than one access to B is recorded. */
481c6ce6 3402
4e91a871 3403static void
1cae46be 3404verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3405 tree writer)
4e91a871 3406{
3407 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3408 enum tree_code code;
ce45a448 3409 enum tree_code_class cl;
481c6ce6 3410
e5b75768 3411 /* X may be NULL if it is the operand of an empty statement expression
3412 ({ }). */
3413 if (x == NULL)
3414 return;
3415
4e91a871 3416 restart:
3417 code = TREE_CODE (x);
e916c70c 3418 cl = TREE_CODE_CLASS (code);
481c6ce6 3419
4e91a871 3420 if (warning_candidate_p (x))
79973b57 3421 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3422
3423 switch (code)
3424 {
67b28e3e 3425 case CONSTRUCTOR:
8e71dad2 3426 case SIZEOF_EXPR:
67b28e3e 3427 return;
3428
4e91a871 3429 case COMPOUND_EXPR:
3430 case TRUTH_ANDIF_EXPR:
3431 case TRUTH_ORIF_EXPR:
3432 tmp_before = tmp_nosp = tmp_list3 = 0;
3433 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3434 warn_for_collisions (tmp_nosp);
3435 merge_tlist (pbefore_sp, tmp_before, 0);
3436 merge_tlist (pbefore_sp, tmp_nosp, 0);
3437 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3438 merge_tlist (pbefore_sp, tmp_list3, 0);
3439 return;
3440
3441 case COND_EXPR:
3442 tmp_before = tmp_list2 = 0;
3443 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3444 warn_for_collisions (tmp_list2);
3445 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3446 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3447
3448 tmp_list3 = tmp_nosp = 0;
3449 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3450 warn_for_collisions (tmp_nosp);
3451 merge_tlist (pbefore_sp, tmp_list3, 0);
3452
3453 tmp_list3 = tmp_list2 = 0;
3454 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3455 warn_for_collisions (tmp_list2);
3456 merge_tlist (pbefore_sp, tmp_list3, 0);
3457 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3458 two first, to avoid warning for (a ? b++ : b++). */
3459 merge_tlist (&tmp_nosp, tmp_list2, 0);
3460 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3461 return;
3462
481c6ce6 3463 case PREDECREMENT_EXPR:
3464 case PREINCREMENT_EXPR:
3465 case POSTDECREMENT_EXPR:
3466 case POSTINCREMENT_EXPR:
4e91a871 3467 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3468 return;
3469
3470 case MODIFY_EXPR:
3471 tmp_before = tmp_nosp = tmp_list3 = 0;
3472 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3473 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3474 /* Expressions inside the LHS are not ordered wrt. the sequence points
3475 in the RHS. Example:
3476 *a = (a++, 2)
3477 Despite the fact that the modification of "a" is in the before_sp
3478 list (tmp_before), it conflicts with the use of "a" in the LHS.
3479 We can handle this by adding the contents of tmp_list3
3480 to those of tmp_before, and redoing the collision warnings for that
3481 list. */
3482 add_tlist (&tmp_before, tmp_list3, x, 1);
3483 warn_for_collisions (tmp_before);
3484 /* Exclude the LHS itself here; we first have to merge it into the
3485 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3486 didn't exclude the LHS, we'd get it twice, once as a read and once
3487 as a write. */
3488 add_tlist (pno_sp, tmp_list3, x, 0);
3489 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3490
3491 merge_tlist (pbefore_sp, tmp_before, 0);
3492 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3493 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3494 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3495 return;
481c6ce6 3496
3497 case CALL_EXPR:
4e91a871 3498 /* We need to warn about conflicts among arguments and conflicts between
3499 args and the function address. Side effects of the function address,
3500 however, are not ordered by the sequence point of the call. */
c2f47e15 3501 {
3502 call_expr_arg_iterator iter;
3503 tree arg;
48e1416a 3504 tmp_before = tmp_nosp = 0;
c2f47e15 3505 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3506 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3507 {
3508 tmp_list2 = tmp_list3 = 0;
3509 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3510 merge_tlist (&tmp_list3, tmp_list2, 0);
3511 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3512 }
3513 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3514 warn_for_collisions (tmp_before);
3515 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3516 return;
3517 }
481c6ce6 3518
3519 case TREE_LIST:
3520 /* Scan all the list, e.g. indices of multi dimensional array. */
3521 while (x)
3522 {
4e91a871 3523 tmp_before = tmp_nosp = 0;
3524 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3525 merge_tlist (&tmp_nosp, tmp_before, 0);
3526 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3527 x = TREE_CHAIN (x);
3528 }
4e91a871 3529 return;
481c6ce6 3530
4e91a871 3531 case SAVE_EXPR:
3532 {
3533 struct tlist_cache *t;
3534 for (t = save_expr_cache; t; t = t->next)
79973b57 3535 if (candidate_equal_p (t->expr, x))
4e91a871 3536 break;
481c6ce6 3537
84166705 3538 if (!t)
481c6ce6 3539 {
9318f22c 3540 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3541 t->next = save_expr_cache;
3542 t->expr = x;
3543 save_expr_cache = t;
3544
3545 tmp_before = tmp_nosp = 0;
3546 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3547 warn_for_collisions (tmp_nosp);
3548
3549 tmp_list3 = 0;
312243bb 3550 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3551 t->cache_before_sp = tmp_before;
3552 t->cache_after_sp = tmp_list3;
481c6ce6 3553 }
4e91a871 3554 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3555 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3556 return;
3557 }
481c6ce6 3558
012916cb 3559 case ADDR_EXPR:
3560 x = TREE_OPERAND (x, 0);
3561 if (DECL_P (x))
3562 return;
3563 writer = 0;
3564 goto restart;
3565
ce45a448 3566 default:
3567 /* For other expressions, simply recurse on their operands.
a0c938f0 3568 Manual tail recursion for unary expressions.
ce45a448 3569 Other non-expressions need not be processed. */
3570 if (cl == tcc_unary)
3571 {
ce45a448 3572 x = TREE_OPERAND (x, 0);
3573 writer = 0;
3574 goto restart;
3575 }
3576 else if (IS_EXPR_CODE_CLASS (cl))
3577 {
3578 int lp;
c2f47e15 3579 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3580 for (lp = 0; lp < max; lp++)
3581 {
3582 tmp_before = tmp_nosp = 0;
3583 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3584 merge_tlist (&tmp_nosp, tmp_before, 0);
3585 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3586 }
3587 }
3588 return;
481c6ce6 3589 }
481c6ce6 3590}
3591
974e2c0c 3592/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3593 points. */
3594
4b987fac 3595DEBUG_FUNCTION void
1cae46be 3596verify_sequence_points (tree expr)
481c6ce6 3597{
4e91a871 3598 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3599
4e91a871 3600 warned_ids = 0;
3601 save_expr_cache = 0;
3602 if (tlist_firstobj == 0)
481c6ce6 3603 {
4e91a871 3604 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3605 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3606 }
3607
4e91a871 3608 verify_tree (expr, &before_sp, &after_sp, 0);
3609 warn_for_collisions (after_sp);
3610 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3611}
b0fc3e72 3612\f
3613/* Validate the expression after `case' and apply default promotions. */
3614
2ca392fd 3615static tree
2d2f6a15 3616check_case_value (location_t loc, tree value)
b0fc3e72 3617{
3618 if (value == NULL_TREE)
3619 return value;
3620
b96dc121 3621 if (TREE_CODE (value) == INTEGER_CST)
3622 /* Promote char or short to int. */
3623 value = perform_integral_promotions (value);
3624 else if (value != error_mark_node)
b0fc3e72 3625 {
2d2f6a15 3626 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3627 value = error_mark_node;
3628 }
b0fc3e72 3629
6433f1c2 3630 constant_expression_warning (value);
3631
b0fc3e72 3632 return value;
3633}
3634\f
2ca392fd 3635/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3636 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3637 expression.
3638 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3639 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3640 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3641 if the case is not a case range.
3642 The caller has to make sure that we are not called with NULL for
5c9dae64 3643 CASE_LOW_P (i.e. the default case).
442e3cb9 3644 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3645 untouched) or false if the label is out of range. */
3646
3647static bool
f61a9bc2 3648check_case_bounds (location_t loc, tree type, tree orig_type,
2ca392fd 3649 tree *case_low_p, tree *case_high_p)
3650{
3651 tree min_value, max_value;
3652 tree case_low = *case_low_p;
3653 tree case_high = case_high_p ? *case_high_p : case_low;
3654
3655 /* If there was a problem with the original type, do nothing. */
3656 if (orig_type == error_mark_node)
3657 return true;
3658
3659 min_value = TYPE_MIN_VALUE (orig_type);
3660 max_value = TYPE_MAX_VALUE (orig_type);
3661
3662 /* Case label is less than minimum for type. */
3663 if (tree_int_cst_compare (case_low, min_value) < 0
3664 && tree_int_cst_compare (case_high, min_value) < 0)
3665 {
f61a9bc2 3666 warning_at (loc, 0, "case label value is less than minimum value "
3667 "for type");
2ca392fd 3668 return false;
3669 }
b27ac6b5 3670
2ca392fd 3671 /* Case value is greater than maximum for type. */
3672 if (tree_int_cst_compare (case_low, max_value) > 0
3673 && tree_int_cst_compare (case_high, max_value) > 0)
3674 {
f61a9bc2 3675 warning_at (loc, 0, "case label value exceeds maximum value for type");
2ca392fd 3676 return false;
3677 }
3678
3679 /* Saturate lower case label value to minimum. */
3680 if (tree_int_cst_compare (case_high, min_value) >= 0
3681 && tree_int_cst_compare (case_low, min_value) < 0)
3682 {
f61a9bc2 3683 warning_at (loc, 0, "lower value in case label range"
3684 " less than minimum value for type");
2ca392fd 3685 case_low = min_value;
3686 }
b27ac6b5 3687
2ca392fd 3688 /* Saturate upper case label value to maximum. */
3689 if (tree_int_cst_compare (case_low, max_value) <= 0
3690 && tree_int_cst_compare (case_high, max_value) > 0)
3691 {
f61a9bc2 3692 warning_at (loc, 0, "upper value in case label range"
3693 " exceeds maximum value for type");
2ca392fd 3694 case_high = max_value;
3695 }
3696
3697 if (*case_low_p != case_low)
3698 *case_low_p = convert (type, case_low);
3699 if (case_high_p && *case_high_p != case_high)
3700 *case_high_p = convert (type, case_high);
3701
3702 return true;
3703}
3704\f
b0fc3e72 3705/* Return an integer type with BITS bits of precision,
3706 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3707
3708tree
1cae46be 3709c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3710{
9f75f026 3711 int i;
3712
46375237 3713 if (bits == TYPE_PRECISION (integer_type_node))
3714 return unsignedp ? unsigned_type_node : integer_type_node;
3715
bacde65a 3716 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3717 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3718
bacde65a 3719 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3720 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3721
bacde65a 3722 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3723 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3724
bacde65a 3725 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3726 return (unsignedp ? long_long_unsigned_type_node
3727 : long_long_integer_type_node);
3728
9f75f026 3729 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3730 if (int_n_enabled_p[i]
3731 && bits == int_n_data[i].bitsize)
3732 return (unsignedp ? int_n_trees[i].unsigned_type
3733 : int_n_trees[i].signed_type);
6388cfe2 3734
f57fa2ea 3735 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3736 return (unsignedp ? widest_unsigned_literal_type_node
3737 : widest_integer_literal_type_node);
3738
bacde65a 3739 if (bits <= TYPE_PRECISION (intQI_type_node))
3740 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3741
3742 if (bits <= TYPE_PRECISION (intHI_type_node))
3743 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3744
3745 if (bits <= TYPE_PRECISION (intSI_type_node))
3746 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3747
3748 if (bits <= TYPE_PRECISION (intDI_type_node))
3749 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3750
b0fc3e72 3751 return 0;
3752}
3753
9421ebb9 3754/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3755 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3756 and saturating if SATP is nonzero, otherwise not saturating. */
3757
3758tree
3759c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3760 int unsignedp, int satp)
3761{
3754d046 3762 machine_mode mode;
9421ebb9 3763 if (ibit == 0)
3764 mode = unsignedp ? UQQmode : QQmode;
3765 else
3766 mode = unsignedp ? UHAmode : HAmode;
3767
3768 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3769 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3770 break;
3771
3772 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3773 {
3774 sorry ("GCC cannot support operators with integer types and "
3775 "fixed-point types that have too many integral and "
3776 "fractional bits together");
3777 return 0;
3778 }
3779
3780 return c_common_type_for_mode (mode, satp);
3781}
3782
5b247e9f 3783/* Used for communication between c_common_type_for_mode and
3784 c_register_builtin_type. */
c1917557 3785tree registered_builtin_types;
5b247e9f 3786
b0fc3e72 3787/* Return a data type that has machine mode MODE.
3788 If the mode is an integer,
9421ebb9 3789 then UNSIGNEDP selects between signed and unsigned types.
3790 If the mode is a fixed-point mode,
3791 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3792
3793tree
3754d046 3794c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3795{
5b247e9f 3796 tree t;
9f75f026 3797 int i;
5b247e9f 3798
46375237 3799 if (mode == TYPE_MODE (integer_type_node))
3800 return unsignedp ? unsigned_type_node : integer_type_node;
3801
b0fc3e72 3802 if (mode == TYPE_MODE (signed_char_type_node))
3803 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3804
3805 if (mode == TYPE_MODE (short_integer_type_node))
3806 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3807
b0fc3e72 3808 if (mode == TYPE_MODE (long_integer_type_node))
3809 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3810
3811 if (mode == TYPE_MODE (long_long_integer_type_node))
3812 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3813
9f75f026 3814 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3815 if (int_n_enabled_p[i]
3816 && mode == int_n_data[i].m)
3817 return (unsignedp ? int_n_trees[i].unsigned_type
3818 : int_n_trees[i].signed_type);
6388cfe2 3819
f57fa2ea 3820 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3821 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3822 : widest_integer_literal_type_node;
f57fa2ea 3823
88ae7f04 3824 if (mode == QImode)
bacde65a 3825 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3826
88ae7f04 3827 if (mode == HImode)
bacde65a 3828 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3829
88ae7f04 3830 if (mode == SImode)
bacde65a 3831 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3832
88ae7f04 3833 if (mode == DImode)
bacde65a 3834 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3835
cc1cc1c7 3836#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3837 if (mode == TYPE_MODE (intTI_type_node))
3838 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3839#endif
6274009c 3840
b0fc3e72 3841 if (mode == TYPE_MODE (float_type_node))
3842 return float_type_node;
3843
3844 if (mode == TYPE_MODE (double_type_node))
3845 return double_type_node;
3846
3847 if (mode == TYPE_MODE (long_double_type_node))
3848 return long_double_type_node;
3849
545c2bde 3850 if (mode == TYPE_MODE (void_type_node))
3851 return void_type_node;
b27ac6b5 3852
b0fc3e72 3853 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3854 return (unsignedp
3855 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3856 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3857
3858 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3859 return (unsignedp
3860 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3861 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3862
0dfc45b5 3863 if (COMPLEX_MODE_P (mode))
3864 {
3754d046 3865 machine_mode inner_mode;
0dfc45b5 3866 tree inner_type;
3867
3868 if (mode == TYPE_MODE (complex_float_type_node))
3869 return complex_float_type_node;
3870 if (mode == TYPE_MODE (complex_double_type_node))
3871 return complex_double_type_node;
3872 if (mode == TYPE_MODE (complex_long_double_type_node))
3873 return complex_long_double_type_node;
3874
3875 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3876 return complex_integer_type_node;
3877
3878 inner_mode = GET_MODE_INNER (mode);
3879 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3880 if (inner_type != NULL_TREE)
3881 return build_complex_type (inner_type);
3882 }
3883 else if (VECTOR_MODE_P (mode))
4917c376 3884 {
3754d046 3885 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3886 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3887 if (inner_type != NULL_TREE)
3888 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3889 }
e2ea7e3a 3890
c4503c0a 3891 if (mode == TYPE_MODE (dfloat32_type_node))
3892 return dfloat32_type_node;
3893 if (mode == TYPE_MODE (dfloat64_type_node))
3894 return dfloat64_type_node;
3895 if (mode == TYPE_MODE (dfloat128_type_node))
3896 return dfloat128_type_node;
3897
9421ebb9 3898 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3899 {
3900 if (mode == TYPE_MODE (short_fract_type_node))
3901 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3902 if (mode == TYPE_MODE (fract_type_node))
3903 return unsignedp ? sat_fract_type_node : fract_type_node;
3904 if (mode == TYPE_MODE (long_fract_type_node))
3905 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3906 if (mode == TYPE_MODE (long_long_fract_type_node))
3907 return unsignedp ? sat_long_long_fract_type_node
3908 : long_long_fract_type_node;
3909
3910 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3911 return unsignedp ? sat_unsigned_short_fract_type_node
3912 : unsigned_short_fract_type_node;
3913 if (mode == TYPE_MODE (unsigned_fract_type_node))
3914 return unsignedp ? sat_unsigned_fract_type_node
3915 : unsigned_fract_type_node;
3916 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3917 return unsignedp ? sat_unsigned_long_fract_type_node
3918 : unsigned_long_fract_type_node;
3919 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3920 return unsignedp ? sat_unsigned_long_long_fract_type_node
3921 : unsigned_long_long_fract_type_node;
3922
3923 if (mode == TYPE_MODE (short_accum_type_node))
3924 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3925 if (mode == TYPE_MODE (accum_type_node))
3926 return unsignedp ? sat_accum_type_node : accum_type_node;
3927 if (mode == TYPE_MODE (long_accum_type_node))
3928 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3929 if (mode == TYPE_MODE (long_long_accum_type_node))
3930 return unsignedp ? sat_long_long_accum_type_node
3931 : long_long_accum_type_node;
3932
3933 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3934 return unsignedp ? sat_unsigned_short_accum_type_node
3935 : unsigned_short_accum_type_node;
3936 if (mode == TYPE_MODE (unsigned_accum_type_node))
3937 return unsignedp ? sat_unsigned_accum_type_node
3938 : unsigned_accum_type_node;
3939 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3940 return unsignedp ? sat_unsigned_long_accum_type_node
3941 : unsigned_long_accum_type_node;
3942 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3943 return unsignedp ? sat_unsigned_long_long_accum_type_node
3944 : unsigned_long_long_accum_type_node;
3945
3946 if (mode == QQmode)
3947 return unsignedp ? sat_qq_type_node : qq_type_node;
3948 if (mode == HQmode)
3949 return unsignedp ? sat_hq_type_node : hq_type_node;
3950 if (mode == SQmode)
3951 return unsignedp ? sat_sq_type_node : sq_type_node;
3952 if (mode == DQmode)
3953 return unsignedp ? sat_dq_type_node : dq_type_node;
3954 if (mode == TQmode)
3955 return unsignedp ? sat_tq_type_node : tq_type_node;
3956
3957 if (mode == UQQmode)
3958 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3959 if (mode == UHQmode)
3960 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3961 if (mode == USQmode)
3962 return unsignedp ? sat_usq_type_node : usq_type_node;
3963 if (mode == UDQmode)
3964 return unsignedp ? sat_udq_type_node : udq_type_node;
3965 if (mode == UTQmode)
3966 return unsignedp ? sat_utq_type_node : utq_type_node;
3967
3968 if (mode == HAmode)
3969 return unsignedp ? sat_ha_type_node : ha_type_node;
3970 if (mode == SAmode)
3971 return unsignedp ? sat_sa_type_node : sa_type_node;
3972 if (mode == DAmode)
3973 return unsignedp ? sat_da_type_node : da_type_node;
3974 if (mode == TAmode)
3975 return unsignedp ? sat_ta_type_node : ta_type_node;
3976
3977 if (mode == UHAmode)
3978 return unsignedp ? sat_uha_type_node : uha_type_node;
3979 if (mode == USAmode)
3980 return unsignedp ? sat_usa_type_node : usa_type_node;
3981 if (mode == UDAmode)
3982 return unsignedp ? sat_uda_type_node : uda_type_node;
3983 if (mode == UTAmode)
3984 return unsignedp ? sat_uta_type_node : uta_type_node;
3985 }
3986
5b247e9f 3987 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3988 if (TYPE_MODE (TREE_VALUE (t)) == mode
3989 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3990 return TREE_VALUE (t);
3991
b0fc3e72 3992 return 0;
3993}
20d39783 3994
11773141 3995tree
3996c_common_unsigned_type (tree type)
3997{
3998 return c_common_signed_or_unsigned_type (1, type);
3999}
4000
20d39783 4001/* Return a signed type the same as TYPE in other respects. */
4002
4003tree
1cae46be 4004c_common_signed_type (tree type)
20d39783 4005{
4070745f 4006 return c_common_signed_or_unsigned_type (0, type);
20d39783 4007}
4008
4009/* Return a type the same as TYPE except unsigned or
4010 signed according to UNSIGNEDP. */
4011
4012tree
1cae46be 4013c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4014{
7a91101f 4015 tree type1;
9f75f026 4016 int i;
20d39783 4017
7a91101f 4018 /* This block of code emulates the behavior of the old
4019 c_common_unsigned_type. In particular, it returns
4020 long_unsigned_type_node if passed a long, even when a int would
4021 have the same size. This is necessary for warnings to work
4022 correctly in archs where sizeof(int) == sizeof(long) */
4023
4024 type1 = TYPE_MAIN_VARIANT (type);
4025 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4026 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4027 if (type1 == integer_type_node || type1 == unsigned_type_node)
4028 return unsignedp ? unsigned_type_node : integer_type_node;
4029 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4030 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4031 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4032 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4033 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4034 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4035
4036 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4037 if (int_n_enabled_p[i]
4038 && (type1 == int_n_trees[i].unsigned_type
4039 || type1 == int_n_trees[i].signed_type))
4040 return (unsignedp ? int_n_trees[i].unsigned_type
4041 : int_n_trees[i].signed_type);
4042
7a91101f 4043 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4044 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4045#if HOST_BITS_PER_WIDE_INT >= 64
4046 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4047 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4048#endif
4049 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4050 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4051 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4052 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4053 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4054 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4055 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4056 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4057
9f92e1a2 4058#define C_COMMON_FIXED_TYPES(NAME) \
4059 if (type1 == short_ ## NAME ## _type_node \
4060 || type1 == unsigned_short_ ## NAME ## _type_node) \
4061 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4062 : short_ ## NAME ## _type_node; \
4063 if (type1 == NAME ## _type_node \
4064 || type1 == unsigned_ ## NAME ## _type_node) \
4065 return unsignedp ? unsigned_ ## NAME ## _type_node \
4066 : NAME ## _type_node; \
4067 if (type1 == long_ ## NAME ## _type_node \
4068 || type1 == unsigned_long_ ## NAME ## _type_node) \
4069 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4070 : long_ ## NAME ## _type_node; \
4071 if (type1 == long_long_ ## NAME ## _type_node \
4072 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4073 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4074 : long_long_ ## NAME ## _type_node;
4075
4076#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4077 if (type1 == NAME ## _type_node \
4078 || type1 == u ## NAME ## _type_node) \
4079 return unsignedp ? u ## NAME ## _type_node \
4080 : NAME ## _type_node;
4081
4082#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4083 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4084 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4085 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4086 : sat_ ## short_ ## NAME ## _type_node; \
4087 if (type1 == sat_ ## NAME ## _type_node \
4088 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4089 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4090 : sat_ ## NAME ## _type_node; \
4091 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4092 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4093 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4094 : sat_ ## long_ ## NAME ## _type_node; \
4095 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4096 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4097 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4098 : sat_ ## long_long_ ## NAME ## _type_node;
4099
4100#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4101 if (type1 == sat_ ## NAME ## _type_node \
4102 || type1 == sat_ ## u ## NAME ## _type_node) \
4103 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4104 : sat_ ## NAME ## _type_node;
4105
4106 C_COMMON_FIXED_TYPES (fract);
4107 C_COMMON_FIXED_TYPES_SAT (fract);
4108 C_COMMON_FIXED_TYPES (accum);
4109 C_COMMON_FIXED_TYPES_SAT (accum);
4110
4111 C_COMMON_FIXED_MODE_TYPES (qq);
4112 C_COMMON_FIXED_MODE_TYPES (hq);
4113 C_COMMON_FIXED_MODE_TYPES (sq);
4114 C_COMMON_FIXED_MODE_TYPES (dq);
4115 C_COMMON_FIXED_MODE_TYPES (tq);
4116 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4117 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4118 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4119 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4120 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4121 C_COMMON_FIXED_MODE_TYPES (ha);
4122 C_COMMON_FIXED_MODE_TYPES (sa);
4123 C_COMMON_FIXED_MODE_TYPES (da);
4124 C_COMMON_FIXED_MODE_TYPES (ta);
4125 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4126 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4127 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4128 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4129
4f7f7efd 4130 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4131 the precision; they have precision set to match their range, but
4132 may use a wider mode to match an ABI. If we change modes, we may
4133 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4134 the precision as well, so as to yield correct results for
4135 bit-field types. C++ does not have these separate bit-field
4136 types, and producing a signed or unsigned variant of an
4137 ENUMERAL_TYPE may cause other problems as well. */
4138
ac265864 4139 if (!INTEGRAL_TYPE_P (type)
4140 || TYPE_UNSIGNED (type) == unsignedp)
4141 return type;
4142
4f7f7efd 4143#define TYPE_OK(node) \
4144 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4145 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4146 if (TYPE_OK (signed_char_type_node))
20d39783 4147 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4148 if (TYPE_OK (integer_type_node))
20d39783 4149 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4150 if (TYPE_OK (short_integer_type_node))
20d39783 4151 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4152 if (TYPE_OK (long_integer_type_node))
20d39783 4153 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4154 if (TYPE_OK (long_long_integer_type_node))
20d39783 4155 return (unsignedp ? long_long_unsigned_type_node
4156 : long_long_integer_type_node);
9f75f026 4157
4158 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4159 if (int_n_enabled_p[i]
4160 && TYPE_MODE (type) == int_n_data[i].m
4161 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4162 return (unsignedp ? int_n_trees[i].unsigned_type
4163 : int_n_trees[i].signed_type);
4164
4f7f7efd 4165 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4166 return (unsignedp ? widest_unsigned_literal_type_node
4167 : widest_integer_literal_type_node);
ef11801e 4168
4169#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4170 if (TYPE_OK (intTI_type_node))
ef11801e 4171 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4172#endif
4f7f7efd 4173 if (TYPE_OK (intDI_type_node))
ef11801e 4174 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4175 if (TYPE_OK (intSI_type_node))
ef11801e 4176 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4177 if (TYPE_OK (intHI_type_node))
ef11801e 4178 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4179 if (TYPE_OK (intQI_type_node))
ef11801e 4180 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4181#undef TYPE_OK
ef11801e 4182
0c4abe5b 4183 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4184}
b268e47e 4185
c0e47fd4 4186/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4187
4188tree
4189c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4190{
9f75f026 4191 int i;
4192
c0e47fd4 4193 /* Extended integer types of the same width as a standard type have
4194 lesser rank, so those of the same width as int promote to int or
4195 unsigned int and are valid for printf formats expecting int or
4196 unsigned int. To avoid such special cases, avoid creating
4197 extended integer types for bit-fields if a standard integer type
4198 is available. */
4199 if (width == TYPE_PRECISION (integer_type_node))
4200 return unsignedp ? unsigned_type_node : integer_type_node;
4201 if (width == TYPE_PRECISION (signed_char_type_node))
4202 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4203 if (width == TYPE_PRECISION (short_integer_type_node))
4204 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4205 if (width == TYPE_PRECISION (long_integer_type_node))
4206 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4207 if (width == TYPE_PRECISION (long_long_integer_type_node))
4208 return (unsignedp ? long_long_unsigned_type_node
4209 : long_long_integer_type_node);
9f75f026 4210 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4211 if (int_n_enabled_p[i]
4212 && width == int_n_data[i].bitsize)
4213 return (unsignedp ? int_n_trees[i].unsigned_type
4214 : int_n_trees[i].signed_type);
c0e47fd4 4215 return build_nonstandard_integer_type (width, unsignedp);
4216}
4217
b268e47e 4218/* The C version of the register_builtin_type langhook. */
4219
4220void
4221c_register_builtin_type (tree type, const char* name)
4222{
4223 tree decl;
4224
e60a6f7b 4225 decl = build_decl (UNKNOWN_LOCATION,
4226 TYPE_DECL, get_identifier (name), type);
b268e47e 4227 DECL_ARTIFICIAL (decl) = 1;
4228 if (!TYPE_NAME (type))
4229 TYPE_NAME (type) = decl;
4230 pushdecl (decl);
5b247e9f 4231
4232 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4233}
a9b9d10c 4234\f
aff9e656 4235/* Print an error message for invalid operands to arith operation
8e70fb09 4236 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4237 LOCATION is the location of the message. */
b0fc3e72 4238
4239void
8e70fb09 4240binary_op_error (location_t location, enum tree_code code,
4241 tree type0, tree type1)
b0fc3e72 4242{
19cb6b50 4243 const char *opname;
f03946e4 4244
b0fc3e72 4245 switch (code)
4246 {
b0fc3e72 4247 case PLUS_EXPR:
4248 opname = "+"; break;
4249 case MINUS_EXPR:
4250 opname = "-"; break;
4251 case MULT_EXPR:
4252 opname = "*"; break;
4253 case MAX_EXPR:
4254 opname = "max"; break;
4255 case MIN_EXPR:
4256 opname = "min"; break;
4257 case EQ_EXPR:
4258 opname = "=="; break;
4259 case NE_EXPR:
4260 opname = "!="; break;
4261 case LE_EXPR:
4262 opname = "<="; break;
4263 case GE_EXPR:
4264 opname = ">="; break;
4265 case LT_EXPR:
4266 opname = "<"; break;
4267 case GT_EXPR:
4268 opname = ">"; break;
4269 case LSHIFT_EXPR:
4270 opname = "<<"; break;
4271 case RSHIFT_EXPR:
4272 opname = ">>"; break;
4273 case TRUNC_MOD_EXPR:
66618a1e 4274 case FLOOR_MOD_EXPR:
b0fc3e72 4275 opname = "%"; break;
4276 case TRUNC_DIV_EXPR:
66618a1e 4277 case FLOOR_DIV_EXPR:
b0fc3e72 4278 opname = "/"; break;
4279 case BIT_AND_EXPR:
4280 opname = "&"; break;
4281 case BIT_IOR_EXPR:
4282 opname = "|"; break;
4283 case TRUTH_ANDIF_EXPR:
4284 opname = "&&"; break;
4285 case TRUTH_ORIF_EXPR:
4286 opname = "||"; break;
4287 case BIT_XOR_EXPR:
4288 opname = "^"; break;
31f820d2 4289 default:
315ba355 4290 gcc_unreachable ();
b0fc3e72 4291 }
8e70fb09 4292 error_at (location,
4293 "invalid operands to binary %s (have %qT and %qT)", opname,
4294 type0, type1);
b0fc3e72 4295}
4296\f
03fe1dc2 4297/* Given an expression as a tree, return its original type. Do this
4298 by stripping any conversion that preserves the sign and precision. */
4299static tree
4300expr_original_type (tree expr)
4301{
4302 STRIP_SIGN_NOPS (expr);
4303 return TREE_TYPE (expr);
4304}
4305
b0fc3e72 4306/* Subroutine of build_binary_op, used for comparison operations.
4307 See if the operands have both been converted from subword integer types
4308 and, if so, perhaps change them both back to their original type.
5b511807 4309 This function is also responsible for converting the two operands
4310 to the proper common type for comparison.
b0fc3e72 4311
4312 The arguments of this function are all pointers to local variables
4313 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4314 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4315
2623625f 4316 LOC is the location of the comparison.
4317
b0fc3e72 4318 If this function returns nonzero, it means that the comparison has
4319 a constant value. What this function returns is an expression for
4320 that value. */
4321
4322tree
2623625f 4323shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4324 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4325{
19cb6b50 4326 tree type;
b0fc3e72 4327 tree op0 = *op0_ptr;
4328 tree op1 = *op1_ptr;
4329 int unsignedp0, unsignedp1;
4330 int real1, real2;
4331 tree primop0, primop1;
4332 enum tree_code code = *rescode_ptr;
4333
4334 /* Throw away any conversions to wider types
4335 already present in the operands. */
4336
7f506bca 4337 primop0 = c_common_get_narrower (op0, &unsignedp0);
4338 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4339
119d06b2 4340 /* If primopN is first sign-extended from primopN's precision to opN's
4341 precision, then zero-extended from opN's precision to
4342 *restype_ptr precision, shortenings might be invalid. */
4343 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4344 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4345 && !unsignedp0
4346 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4347 primop0 = op0;
4348 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4349 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4350 && !unsignedp1
4351 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4352 primop1 = op1;
4353
b0fc3e72 4354 /* Handle the case that OP0 does not *contain* a conversion
4355 but it *requires* conversion to FINAL_TYPE. */
4356
4357 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4358 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4359 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4360 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4361
4362 /* If one of the operands must be floated, we cannot optimize. */
4363 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4364 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4365
b0fc3e72 4366 /* If first arg is constant, swap the args (changing operation
2bd278cc 4367 so value is preserved), for canonicalization. Don't do this if
4368 the second arg is 0. */
b0fc3e72 4369
2bd278cc 4370 if (TREE_CONSTANT (primop0)
9421ebb9 4371 && !integer_zerop (primop1) && !real_zerop (primop1)
4372 && !fixed_zerop (primop1))
b0fc3e72 4373 {
dfcf26a5 4374 std::swap (primop0, primop1);
4375 std::swap (op0, op1);
b0fc3e72 4376 *op0_ptr = op0;
4377 *op1_ptr = op1;
dfcf26a5 4378 std::swap (unsignedp0, unsignedp1);
4379 std::swap (real1, real2);
b0fc3e72 4380
4381 switch (code)
4382 {
4383 case LT_EXPR:
4384 code = GT_EXPR;
4385 break;
4386 case GT_EXPR:
4387 code = LT_EXPR;
4388 break;
4389 case LE_EXPR:
4390 code = GE_EXPR;
4391 break;
4392 case GE_EXPR:
4393 code = LE_EXPR;
4394 break;
31f820d2 4395 default:
4396 break;
b0fc3e72 4397 }
4398 *rescode_ptr = code;
4399 }
4400
4401 /* If comparing an integer against a constant more bits wide,
4402 maybe we can deduce a value of 1 or 0 independent of the data.
4403 Or else truncate the constant now
4404 rather than extend the variable at run time.
4405
4406 This is only interesting if the constant is the wider arg.
4407 Also, it is not safe if the constant is unsigned and the
4408 variable arg is signed, since in this case the variable
4409 would be sign-extended and then regarded as unsigned.
4410 Our technique fails in this case because the lowest/highest
4411 possible unsigned results don't follow naturally from the
4412 lowest/highest possible values of the variable operand.
4413 For just EQ_EXPR and NE_EXPR there is another technique that
4414 could be used: see if the constant can be faithfully represented
4415 in the other operand's type, by truncating it and reextending it
4416 and see if that preserves the constant's value. */
4417
4418 if (!real1 && !real2
9421ebb9 4419 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4420 && TREE_CODE (primop1) == INTEGER_CST
4421 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4422 {
4423 int min_gt, max_gt, min_lt, max_lt;
4424 tree maxval, minval;
4425 /* 1 if comparison is nominally unsigned. */
78a8ed03 4426 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4427 tree val;
4428
4070745f 4429 type = c_common_signed_or_unsigned_type (unsignedp0,
4430 TREE_TYPE (primop0));
cda09c61 4431
b0fc3e72 4432 maxval = TYPE_MAX_VALUE (type);
4433 minval = TYPE_MIN_VALUE (type);
4434
4435 if (unsignedp && !unsignedp0)
4070745f 4436 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4437
4438 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4439 {
9a5e8086 4440 /* Convert primop1 to target type, but do not introduce
4441 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4442 primop1 = force_fit_type (*restype_ptr,
796b6678 4443 wide_int::from
4444 (primop1,
4445 TYPE_PRECISION (*restype_ptr),
4446 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4447 0, TREE_OVERFLOW (primop1));
18dbec6f 4448 }
b0fc3e72 4449 if (type != *restype_ptr)
4450 {
4451 minval = convert (*restype_ptr, minval);
4452 maxval = convert (*restype_ptr, maxval);
4453 }
4454
d99d10ca 4455 min_gt = tree_int_cst_lt (primop1, minval);
4456 max_gt = tree_int_cst_lt (primop1, maxval);
4457 min_lt = tree_int_cst_lt (minval, primop1);
4458 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4459
4460 val = 0;
4461 /* This used to be a switch, but Genix compiler can't handle that. */
4462 if (code == NE_EXPR)
4463 {
4464 if (max_lt || min_gt)
3c2239cf 4465 val = truthvalue_true_node;
b0fc3e72 4466 }
4467 else if (code == EQ_EXPR)
4468 {
4469 if (max_lt || min_gt)
3c2239cf 4470 val = truthvalue_false_node;
b0fc3e72 4471 }
4472 else if (code == LT_EXPR)
4473 {
4474 if (max_lt)
3c2239cf 4475 val = truthvalue_true_node;
b0fc3e72 4476 if (!min_lt)
3c2239cf 4477 val = truthvalue_false_node;
b0fc3e72 4478 }
4479 else if (code == GT_EXPR)
4480 {
4481 if (min_gt)
3c2239cf 4482 val = truthvalue_true_node;
b0fc3e72 4483 if (!max_gt)
3c2239cf 4484 val = truthvalue_false_node;
b0fc3e72 4485 }
4486 else if (code == LE_EXPR)
4487 {
4488 if (!max_gt)
3c2239cf 4489 val = truthvalue_true_node;
b0fc3e72 4490 if (min_gt)
3c2239cf 4491 val = truthvalue_false_node;
b0fc3e72 4492 }
4493 else if (code == GE_EXPR)
4494 {
4495 if (!min_lt)
3c2239cf 4496 val = truthvalue_true_node;
b0fc3e72 4497 if (max_lt)
3c2239cf 4498 val = truthvalue_false_node;
b0fc3e72 4499 }
4500
4501 /* If primop0 was sign-extended and unsigned comparison specd,
4502 we did a signed comparison above using the signed type bounds.
4503 But the comparison we output must be unsigned.
4504
4505 Also, for inequalities, VAL is no good; but if the signed
4506 comparison had *any* fixed result, it follows that the
4507 unsigned comparison just tests the sign in reverse
4508 (positive values are LE, negative ones GE).
4509 So we can generate an unsigned comparison
4510 against an extreme value of the signed type. */
4511
4512 if (unsignedp && !unsignedp0)
4513 {
4514 if (val != 0)
4515 switch (code)
4516 {
4517 case LT_EXPR:
4518 case GE_EXPR:
4519 primop1 = TYPE_MIN_VALUE (type);
4520 val = 0;
4521 break;
4522
4523 case LE_EXPR:
4524 case GT_EXPR:
4525 primop1 = TYPE_MAX_VALUE (type);
4526 val = 0;
4527 break;
31f820d2 4528
4529 default:
4530 break;
b0fc3e72 4531 }
11773141 4532 type = c_common_unsigned_type (type);
b0fc3e72 4533 }
4534
734ec290 4535 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4536 {
3c2239cf 4537 if (val == truthvalue_false_node)
03fe1dc2 4538 warning_at (loc, OPT_Wtype_limits,
4539 "comparison is always false due to limited range of data type");
3c2239cf 4540 if (val == truthvalue_true_node)
03fe1dc2 4541 warning_at (loc, OPT_Wtype_limits,
4542 "comparison is always true due to limited range of data type");
b0fc3e72 4543 }
4544
4545 if (val != 0)
4546 {
4547 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4548 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4549 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4550 return val;
4551 }
4552
4553 /* Value is not predetermined, but do the comparison
4554 in the type of the operand that is not constant.
4555 TYPE is already properly set. */
4556 }
c4503c0a 4557
4558 /* If either arg is decimal float and the other is float, find the
4559 proper common type to use for comparison. */
7fd22aae 4560 else if (real1 && real2
4561 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4562 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4563 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4564
4565 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4566 else if (real1 && real2
4567 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4568 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4569 return 0;
c4503c0a 4570
b0fc3e72 4571 else if (real1 && real2
2203bd5c 4572 && (TYPE_PRECISION (TREE_TYPE (primop0))
4573 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4574 type = TREE_TYPE (primop0);
4575
4576 /* If args' natural types are both narrower than nominal type
4577 and both extend in the same manner, compare them
4578 in the type of the wider arg.
4579 Otherwise must actually extend both to the nominal
4580 common type lest different ways of extending
4581 alter the result.
4582 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4583
4584 else if (unsignedp0 == unsignedp1 && real1 == real2
4585 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4586 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4587 {
4588 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4589 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4590 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4591 type);
b0fc3e72 4592 /* Make sure shorter operand is extended the right way
4593 to match the longer operand. */
4070745f 4594 primop0
4595 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4596 TREE_TYPE (primop0)),
4597 primop0);
4598 primop1
4599 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4600 TREE_TYPE (primop1)),
4601 primop1);
b0fc3e72 4602 }
4603 else
4604 {
4605 /* Here we must do the comparison on the nominal type
4606 using the args exactly as we received them. */
4607 type = *restype_ptr;
4608 primop0 = op0;
4609 primop1 = op1;
4610
4611 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4612 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4613 {
4614 tree value = 0;
03fe1dc2 4615 /* All unsigned values are >= 0, so we warn. However,
4616 if OP0 is a constant that is >= 0, the signedness of
4617 the comparison isn't an issue, so suppress the
4618 warning. */
4619 bool warn =
3df42822 4620 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4621 && !(TREE_CODE (primop0) == INTEGER_CST
4622 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4623 primop0)))
4624 /* Do not warn for enumeration types. */
4625 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4626
b0fc3e72 4627 switch (code)
4628 {
4629 case GE_EXPR:
03fe1dc2 4630 if (warn)
4631 warning_at (loc, OPT_Wtype_limits,
4632 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4633 value = truthvalue_true_node;
b0fc3e72 4634 break;
4635
4636 case LT_EXPR:
03fe1dc2 4637 if (warn)
4638 warning_at (loc, OPT_Wtype_limits,
4639 "comparison of unsigned expression < 0 is always false");
3c2239cf 4640 value = truthvalue_false_node;
31f820d2 4641 break;
4642
4643 default:
4644 break;
b0fc3e72 4645 }
4646
4647 if (value != 0)
4648 {
4649 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4650 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4651 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4652 primop0, value);
b0fc3e72 4653 return value;
4654 }
4655 }
4656 }
4657
4658 *op0_ptr = convert (type, primop0);
4659 *op1_ptr = convert (type, primop1);
4660
3c2239cf 4661 *restype_ptr = truthvalue_type_node;
b0fc3e72 4662
4663 return 0;
4664}
4665\f
1c26100f 4666/* Return a tree for the sum or difference (RESULTCODE says which)
4667 of pointer PTROP and integer INTOP. */
4668
4669tree
389dd41b 4670pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4671 tree ptrop, tree intop, bool complain)
1c26100f 4672{
add6ee5e 4673 tree size_exp, ret;
1c26100f 4674
1c26100f 4675 /* The result is a pointer of the same type that is being added. */
1c26100f 4676 tree result_type = TREE_TYPE (ptrop);
4677
4678 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4679 {
42f9a786 4680 if (complain && warn_pointer_arith)
4681 pedwarn (loc, OPT_Wpointer_arith,
4682 "pointer of type %<void *%> used in arithmetic");
4683 else if (!complain)
4684 return error_mark_node;
1c26100f 4685 size_exp = integer_one_node;
4686 }
4687 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4688 {
42f9a786 4689 if (complain && warn_pointer_arith)
4690 pedwarn (loc, OPT_Wpointer_arith,
4691 "pointer to a function used in arithmetic");
4692 else if (!complain)
4693 return error_mark_node;
1c26100f 4694 size_exp = integer_one_node;
4695 }
1c26100f 4696 else
4697 size_exp = size_in_bytes (TREE_TYPE (result_type));
4698
add6ee5e 4699 /* We are manipulating pointer values, so we don't need to warn
4700 about relying on undefined signed overflow. We disable the
4701 warning here because we use integer types so fold won't know that
4702 they are really pointers. */
4703 fold_defer_overflow_warnings ();
4704
1c26100f 4705 /* If what we are about to multiply by the size of the elements
4706 contains a constant term, apply distributive law
4707 and multiply that constant term separately.
4708 This helps produce common subexpressions. */
1c26100f 4709 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4710 && !TREE_CONSTANT (intop)
1c26100f 4711 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4712 && TREE_CONSTANT (size_exp)
4713 /* If the constant comes from pointer subtraction,
4714 skip this optimization--it would cause an error. */
4715 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4716 /* If the constant is unsigned, and smaller than the pointer size,
4717 then we must skip this optimization. This is because it could cause
4718 an overflow error if the constant is negative but INTOP is not. */
84166705 4719 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4720 || (TYPE_PRECISION (TREE_TYPE (intop))
4721 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4722 {
4723 enum tree_code subcode = resultcode;
4724 tree int_type = TREE_TYPE (intop);
4725 if (TREE_CODE (intop) == MINUS_EXPR)
4726 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4727 /* Convert both subexpression types to the type of intop,
4728 because weird cases involving pointer arithmetic
4729 can result in a sum or difference with different type args. */
8e70fb09 4730 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4731 subcode, ptrop,
1c26100f 4732 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4733 intop = convert (int_type, TREE_OPERAND (intop, 0));
4734 }
4735
4736 /* Convert the integer argument to a type the same size as sizetype
4737 so the multiply won't overflow spuriously. */
1c26100f 4738 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4739 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4740 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4741 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4742
4743 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4744 Do this multiplication as signed, then convert to the appropriate type
8032877c 4745 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4746 because of the sign-extension change in the latter conversion. */
4747 {
4748 tree t = build_binary_op (loc,
4749 MULT_EXPR, intop,
4750 convert (TREE_TYPE (intop), size_exp), 1);
4751 intop = convert (sizetype, t);
4752 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4753 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4754 }
0de36bdb 4755
499e523f 4756 /* Create the sum or difference. */
0de36bdb 4757 if (resultcode == MINUS_EXPR)
389dd41b 4758 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4759
2cc66f2a 4760 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4761
4762 fold_undefer_and_ignore_overflow_warnings ();
4763
4764 return ret;
1c26100f 4765}
4766\f
f59e3889 4767/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4768 and if NON_CONST is known not to be permitted in an evaluated part
4769 of a constant expression. */
4770
4771tree
4772c_wrap_maybe_const (tree expr, bool non_const)
4773{
4774 bool nowarning = TREE_NO_WARNING (expr);
4775 location_t loc = EXPR_LOCATION (expr);
4776
4777 /* This should never be called for C++. */
4778 if (c_dialect_cxx ())
4779 gcc_unreachable ();
4780
4781 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4782 STRIP_TYPE_NOPS (expr);
4783 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4784 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4785 if (nowarning)
4786 TREE_NO_WARNING (expr) = 1;
4787 protected_set_expr_location (expr, loc);
4788
4789 return expr;
4790}
4791
a75b1c71 4792/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4793 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4794 around the SAVE_EXPR if needed so that c_fully_fold does not need
4795 to look inside SAVE_EXPRs. */
4796
4797tree
4798c_save_expr (tree expr)
4799{
4800 bool maybe_const = true;
4801 if (c_dialect_cxx ())
4802 return save_expr (expr);
4803 expr = c_fully_fold (expr, false, &maybe_const);
4804 expr = save_expr (expr);
4805 if (!maybe_const)
f59e3889 4806 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4807 return expr;
4808}
4809
6b68e71a 4810/* Return whether EXPR is a declaration whose address can never be
4811 NULL. */
4812
4813bool
9f627b1a 4814decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4815{
4816 return (DECL_P (expr)
4817 && (TREE_CODE (expr) == PARM_DECL
4818 || TREE_CODE (expr) == LABEL_DECL
4819 || !DECL_WEAK (expr)));
4820}
4821
b0fc3e72 4822/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4823 or for an `if' or `while' statement or ?..: exp. It should already
4824 have been validated to be of suitable type; otherwise, a bad
4825 diagnostic may result.
b0fc3e72 4826
8e70fb09 4827 The EXPR is located at LOCATION.
4828
b0fc3e72 4829 This preparation consists of taking the ordinary
4830 representation of an expression expr and producing a valid tree
4831 boolean expression describing whether expr is nonzero. We could
3c2239cf 4832 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4833 but we optimize comparisons, &&, ||, and !.
4834
3c2239cf 4835 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4836
4837tree
8e70fb09 4838c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4839{
b0fc3e72 4840 switch (TREE_CODE (expr))
4841 {
318a728f 4842 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4843 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4844 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4845 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4846 if (TREE_TYPE (expr) == truthvalue_type_node)
4847 return expr;
e60a6f7b 4848 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4849 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4850 goto ret;
ce04dcdc 4851
b0fc3e72 4852 case TRUTH_ANDIF_EXPR:
4853 case TRUTH_ORIF_EXPR:
4854 case TRUTH_AND_EXPR:
4855 case TRUTH_OR_EXPR:
31f6e93c 4856 case TRUTH_XOR_EXPR:
ce04dcdc 4857 if (TREE_TYPE (expr) == truthvalue_type_node)
4858 return expr;
e60a6f7b 4859 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4860 c_common_truthvalue_conversion (location,
e60a6f7b 4861 TREE_OPERAND (expr, 0)),
4862 c_common_truthvalue_conversion (location,
4863 TREE_OPERAND (expr, 1)));
4864 goto ret;
3e851b85 4865
f6e28f72 4866 case TRUTH_NOT_EXPR:
ce04dcdc 4867 if (TREE_TYPE (expr) == truthvalue_type_node)
4868 return expr;
e60a6f7b 4869 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4870 c_common_truthvalue_conversion (location,
4871 TREE_OPERAND (expr, 0)));
4872 goto ret;
f6e28f72 4873
b0fc3e72 4874 case ERROR_MARK:
4875 return expr;
ab2c1de8 4876
b0fc3e72 4877 case INTEGER_CST:
eddad94a 4878 return integer_zerop (expr) ? truthvalue_false_node
4879 : truthvalue_true_node;
b0fc3e72 4880
4881 case REAL_CST:
5000e21c 4882 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4883 ? truthvalue_true_node
4884 : truthvalue_false_node;
b0fc3e72 4885
9421ebb9 4886 case FIXED_CST:
4887 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4888 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4889 ? truthvalue_true_node
4890 : truthvalue_false_node;
4891
ce04dcdc 4892 case FUNCTION_DECL:
b6889cb0 4893 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4894 /* Fall through. */
4895
b0fc3e72 4896 case ADDR_EXPR:
ce871053 4897 {
ee7d4d6a 4898 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4899 if (decl_with_nonnull_addr_p (inner))
ce871053 4900 {
6b68e71a 4901 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4902 warning_at (location,
4903 OPT_Waddress,
4904 "the address of %qD will always evaluate as %<true%>",
4905 inner);
ce871053 4906 return truthvalue_true_node;
4907 }
d473d901 4908 break;
ce871053 4909 }
b0fc3e72 4910
2203bd5c 4911 case COMPLEX_EXPR:
e60a6f7b 4912 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4913 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4914 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4915 c_common_truthvalue_conversion (location,
4916 TREE_OPERAND (expr, 0)),
4917 c_common_truthvalue_conversion (location,
4918 TREE_OPERAND (expr, 1)),
2203bd5c 4919 0);
e60a6f7b 4920 goto ret;
2203bd5c 4921
b0fc3e72 4922 case NEGATE_EXPR:
4923 case ABS_EXPR:
4924 case FLOAT_EXPR:
c6418a4e 4925 case EXCESS_PRECISION_EXPR:
d10cfa8d 4926 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4927 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4928
4929 case LROTATE_EXPR:
4930 case RROTATE_EXPR:
d10cfa8d 4931 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4932 we can't ignore them if their second arg has side-effects. */
4933 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4934 {
4935 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4936 TREE_OPERAND (expr, 1),
48e1416a 4937 c_common_truthvalue_conversion
e60a6f7b 4938 (location, TREE_OPERAND (expr, 0)));
4939 goto ret;
4940 }
b0fc3e72 4941 else
8e70fb09 4942 return c_common_truthvalue_conversion (location,
4943 TREE_OPERAND (expr, 0));
73be5127 4944
b0fc3e72 4945 case COND_EXPR:
4946 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4947 if (c_dialect_cxx ())
e60a6f7b 4948 {
d0389adc 4949 tree op1 = TREE_OPERAND (expr, 1);
4950 tree op2 = TREE_OPERAND (expr, 2);
4951 /* In C++ one of the arms might have void type if it is throw. */
4952 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4953 op1 = c_common_truthvalue_conversion (location, op1);
4954 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4955 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4956 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4957 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4958 goto ret;
4959 }
a75b1c71 4960 else
e60a6f7b 4961 {
4962 /* Folding will happen later for C. */
4963 expr = build3 (COND_EXPR, truthvalue_type_node,
4964 TREE_OPERAND (expr, 0),
4965 c_common_truthvalue_conversion (location,
4966 TREE_OPERAND (expr, 1)),
4967 c_common_truthvalue_conversion (location,
4968 TREE_OPERAND (expr, 2)));
4969 goto ret;
4970 }
b0fc3e72 4971
72dd6141 4972 CASE_CONVERT:
f8913d47 4973 {
4974 tree totype = TREE_TYPE (expr);
4975 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4976
4977 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4978 since that affects how `default_conversion' will behave. */
4979 if (TREE_CODE (totype) == REFERENCE_TYPE
4980 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4981 break;
4982 /* Don't strip a conversion from C++0x scoped enum, since they
4983 don't implicitly convert to other types. */
4984 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4985 && ENUM_IS_SCOPED (fromtype))
4986 break;
4987 /* If this isn't narrowing the argument, we can ignore it. */
4988 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4989 return c_common_truthvalue_conversion (location,
4990 TREE_OPERAND (expr, 0));
4991 }
b0fc3e72 4992 break;
4993
16837b18 4994 case MODIFY_EXPR:
60a0513e 4995 if (!TREE_NO_WARNING (expr)
4996 && warn_parentheses)
4997 {
4998 warning (OPT_Wparentheses,
4999 "suggest parentheses around assignment used as truth value");
5000 TREE_NO_WARNING (expr) = 1;
5001 }
16837b18 5002 break;
73be5127 5003
31f820d2 5004 default:
5005 break;
b0fc3e72 5006 }
5007
2ba726d2 5008 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5009 {
93be21c0 5010 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5011 expr = (build_binary_op
8e70fb09 5012 (EXPR_LOCATION (expr),
5013 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5014 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5015 c_common_truthvalue_conversion
5016 (location,
5017 build_unary_op (location, REALPART_EXPR, t, 0)),
5018 c_common_truthvalue_conversion
5019 (location,
5020 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5021 0));
e60a6f7b 5022 goto ret;
a0748b7d 5023 }
2ba726d2 5024
9421ebb9 5025 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5026 {
5027 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5028 FCONST0 (TYPE_MODE
5029 (TREE_TYPE (expr))));
43158006 5030 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5031 }
e60a6f7b 5032 else
5033 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5034
e60a6f7b 5035 ret:
5036 protected_set_expr_location (expr, location);
5037 return expr;
b0fc3e72 5038}
5039\f
3237155d 5040static void def_builtin_1 (enum built_in_function fncode,
5041 const char *name,
5042 enum built_in_class fnclass,
5043 tree fntype, tree libtype,
5044 bool both_p, bool fallback_p, bool nonansi_p,
5045 tree fnattrs, bool implicit_p);
0d4238dc 5046
a5b1863e 5047
5048/* Apply the TYPE_QUALS to the new DECL. */
5049
5050void
1cae46be 5051c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5052{
adfb367f 5053 tree type = TREE_TYPE (decl);
b27ac6b5 5054
e4eabbe4 5055 if (type == error_mark_node)
5056 return;
adfb367f 5057
98a33d9f 5058 if ((type_quals & TYPE_QUAL_CONST)
5059 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5060 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5061 constructor can produce constant init, so rely on cp_finish_decl to
5062 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5063 TREE_READONLY (decl) = 1;
5064 if (type_quals & TYPE_QUAL_VOLATILE)
5065 {
5066 TREE_SIDE_EFFECTS (decl) = 1;
5067 TREE_THIS_VOLATILE (decl) = 1;
5068 }
d91a20bc 5069 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5070 {
adfb367f 5071 while (type && TREE_CODE (type) == ARRAY_TYPE)
5072 /* Allow 'restrict' on arrays of pointers.
5073 FIXME currently we just ignore it. */
5074 type = TREE_TYPE (type);
5075 if (!type
5076 || !POINTER_TYPE_P (type)
5077 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5078 error ("invalid use of %<restrict%>");
a5b1863e 5079 }
5080}
5081
2ef51f0e 5082struct c_type_hasher : ggc_hasher<tree>
5083{
5084 static hashval_t hash (tree);
5085 static bool equal (tree, tree);
5086};
5087
4ee9c684 5088/* Hash function for the problem of multiple type definitions in
5089 different files. This must hash all types that will compare
5090 equal via comptypes to the same value. In practice it hashes
0bed3869 5091 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5092
2ef51f0e 5093hashval_t
5094c_type_hasher::hash (tree t)
4ee9c684 5095{
ecf2703d 5096 int n_elements;
4ee9c684 5097 int shift, size;
4ee9c684 5098 tree t2;
5099 switch (TREE_CODE (t))
5100 {
fbf0afd1 5101 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5102 case POINTER_TYPE:
2ef51f0e 5103 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5104 /* Hash on number of elements and total size. */
5105 case ENUMERAL_TYPE:
5106 shift = 3;
5107 t2 = TYPE_VALUES (t);
5108 break;
5109 case RECORD_TYPE:
5110 shift = 0;
5111 t2 = TYPE_FIELDS (t);
5112 break;
5113 case QUAL_UNION_TYPE:
5114 shift = 1;
5115 t2 = TYPE_FIELDS (t);
5116 break;
5117 case UNION_TYPE:
5118 shift = 2;
5119 t2 = TYPE_FIELDS (t);
5120 break;
5121 default:
231bd014 5122 gcc_unreachable ();
4ee9c684 5123 }
ecf2703d 5124 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5125 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5126 n_elements = list_length (t2);
78c2e180 5127 /* We might have a VLA here. */
5128 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5129 size = 0;
5130 else
f9ae6f95 5131 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5132 return ((size << 24) | (n_elements << shift));
4ee9c684 5133}
5134
2ef51f0e 5135bool
5136c_type_hasher::equal (tree t1, tree t2)
5137{
5138 return lang_hooks.types_compatible_p (t1, t2);
5139}
5140
5141static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5142
b5ba9f3a 5143/* Return the typed-based alias set for T, which may be an expression
f7c44134 5144 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5145
32c2fdea 5146alias_set_type
1cae46be 5147c_common_get_alias_set (tree t)
b5ba9f3a 5148{
be4f2de7 5149 tree u;
1cae46be 5150
e58c17e7 5151 /* For VLAs, use the alias set of the element type rather than the
5152 default of alias set 0 for types compared structurally. */
5153 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5154 {
5155 if (TREE_CODE (t) == ARRAY_TYPE)
5156 return get_alias_set (TREE_TYPE (t));
5157 return -1;
5158 }
5159
be4f2de7 5160 /* Permit type-punning when accessing a union, provided the access
5161 is directly through the union. For example, this code does not
5162 permit taking the address of a union member and then storing
5163 through it. Even the type-punning allowed here is a GCC
5164 extension, albeit a common and useful one; the C standard says
5165 that such accesses have implementation-defined behavior. */
5166 for (u = t;
5167 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5168 u = TREE_OPERAND (u, 0))
5169 if (TREE_CODE (u) == COMPONENT_REF
5170 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5171 return 0;
1e2513d9 5172
9fcc3e54 5173 /* That's all the expressions we handle specially. */
84166705 5174 if (!TYPE_P (t))
9fcc3e54 5175 return -1;
5176
d716ce75 5177 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5178 lvalue that has character type. */
5179 if (t == char_type_node
5180 || t == signed_char_type_node
5181 || t == unsigned_char_type_node)
f7c44134 5182 return 0;
a5b1863e 5183
1607663f 5184 /* The C standard specifically allows aliasing between signed and
5185 unsigned variants of the same type. We treat the signed
5186 variant as canonical. */
78a8ed03 5187 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5188 {
4070745f 5189 tree t1 = c_common_signed_type (t);
1607663f 5190
a8868e19 5191 /* t1 == t can happen for boolean nodes which are always unsigned. */
5192 if (t1 != t)
5193 return get_alias_set (t1);
5194 }
1e2513d9 5195
4ee9c684 5196 /* Handle the case of multiple type nodes referring to "the same" type,
5197 which occurs with IMA. These share an alias set. FIXME: Currently only
5198 C90 is handled. (In C99 type compatibility is not transitive, which
5199 complicates things mightily. The alias set splay trees can theoretically
5200 represent this, but insertion is tricky when you consider all the
5201 different orders things might arrive in.) */
5202
5203 if (c_language != clk_c || flag_isoc99)
5204 return -1;
5205
0bed3869 5206 /* Save time if there's only one input file. */
e08bd2f4 5207 if (num_in_fnames == 1)
4ee9c684 5208 return -1;
5209
5210 /* Pointers need special handling if they point to any type that
5211 needs special handling (below). */
5212 if (TREE_CODE (t) == POINTER_TYPE)
5213 {
5214 tree t2;
5215 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5216 for (t2 = TREE_TYPE (t);
af592f67 5217 TREE_CODE (t2) == POINTER_TYPE;
5218 t2 = TREE_TYPE (t2))
5219 ;
b27ac6b5 5220 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 5221 && TREE_CODE (t2) != ENUMERAL_TYPE
5222 && TREE_CODE (t2) != QUAL_UNION_TYPE
5223 && TREE_CODE (t2) != UNION_TYPE)
5224 return -1;
4ee9c684 5225 if (TYPE_SIZE (t2) == 0)
af592f67 5226 return -1;
4ee9c684 5227 }
5228 /* These are the only cases that need special handling. */
b27ac6b5 5229 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 5230 && TREE_CODE (t) != ENUMERAL_TYPE
5231 && TREE_CODE (t) != QUAL_UNION_TYPE
5232 && TREE_CODE (t) != UNION_TYPE
5233 && TREE_CODE (t) != POINTER_TYPE)
5234 return -1;
5235 /* Undefined? */
5236 if (TYPE_SIZE (t) == 0)
5237 return -1;
5238
b27ac6b5 5239 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5240 alias set is recorded in the table. */
5241 if (!type_hash_table)
2ef51f0e 5242 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5243 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5244 if (*slot != NULL)
ad16cb2c 5245 {
5246 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5247 return TYPE_ALIAS_SET ((tree)*slot);
5248 }
4ee9c684 5249 else
5250 /* Our caller will assign and record (in t) a new alias set; all we need
5251 to do is remember t in the hash table. */
5252 *slot = t;
5253
f7c44134 5254 return -1;
b5ba9f3a 5255}
902b4e01 5256\f
e60a6f7b 5257/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5258 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5259 The COMPLAIN flag controls whether we should diagnose possibly
5260 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5261 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5262 a type in any context should be returned, rather than the normal
5263 alignment for that type. */
908c697e 5264
902b4e01 5265tree
e60a6f7b 5266c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5267 tree type, bool is_sizeof, bool min_alignof,
5268 int complain)
902b4e01 5269{
d4c4d95c 5270 const char *op_name;
5271 tree value = NULL;
5272 enum tree_code type_code = TREE_CODE (type);
1cae46be 5273
908c697e 5274 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5275
d4c4d95c 5276 if (type_code == FUNCTION_TYPE)
902b4e01 5277 {
908c697e 5278 if (is_sizeof)
d4c4d95c 5279 {
9205a6cc 5280 if (complain && warn_pointer_arith)
5281 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5282 "invalid application of %<sizeof%> to a function type");
ebd21de4 5283 else if (!complain)
5284 return error_mark_node;
d4c4d95c 5285 value = size_one_node;
5286 }
5287 else
83e25171 5288 {
5289 if (complain)
5290 {
5291 if (c_dialect_cxx ())
29438999 5292 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5293 "%<alignof%> applied to a function type");
5294 else
29438999 5295 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5296 "%<_Alignof%> applied to a function type");
5297 }
5298 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5299 }
d4c4d95c 5300 }
5301 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5302 {
1cae46be 5303 if (type_code == VOID_TYPE
9205a6cc 5304 && complain && warn_pointer_arith)
5305 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5306 "invalid application of %qs to a void type", op_name);
ebd21de4 5307 else if (!complain)
5308 return error_mark_node;
d4c4d95c 5309 value = size_one_node;
902b4e01 5310 }
3df19e1b 5311 else if (!COMPLETE_TYPE_P (type)
5312 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5313 {
d3a4d008 5314 if (complain)
3df19e1b 5315 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5316 op_name, type);
9c719c74 5317 return error_mark_node;
902b4e01 5318 }
3df19e1b 5319 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5320 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5321 {
5322 if (complain)
5323 error_at (loc, "invalid application of %qs to array type %qT of "
5324 "incomplete element type", op_name, type);
5325 return error_mark_node;
5326 }
902b4e01 5327 else
d4c4d95c 5328 {
908c697e 5329 if (is_sizeof)
d4c4d95c 5330 /* Convert in case a char is more than one unit. */
389dd41b 5331 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5332 size_int (TYPE_PRECISION (char_type_node)
5333 / BITS_PER_UNIT));
a179a7dc 5334 else if (min_alignof)
c2c4ae8d 5335 value = size_int (min_align_of_type (type));
d4c4d95c 5336 else
d37625c0 5337 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5338 }
902b4e01 5339
5a1fe2db 5340 /* VALUE will have the middle-end integer type sizetype.
5341 However, we should really return a value of type `size_t',
5342 which is just a typedef for an ordinary integer type. */
389dd41b 5343 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5344
d4c4d95c 5345 return value;
902b4e01 5346}
5347
5348/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5349 alignment of EXPR, measured in bytes. For VAR_DECLs,
5350 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5351 from an "aligned" __attribute__ specification). LOC is the
5352 location of the ALIGNOF operator. */
72040e7e 5353
902b4e01 5354tree
e60a6f7b 5355c_alignof_expr (location_t loc, tree expr)
902b4e01 5356{
5357 tree t;
5358
097b5c8b 5359 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5360 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5361
902b4e01 5362 else if (TREE_CODE (expr) == COMPONENT_REF
5363 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5364 {
e60a6f7b 5365 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5366 t = size_one_node;
5367 }
5368 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5369 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5370 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5371
902b4e01 5372 else if (TREE_CODE (expr) == INDIRECT_REF)
5373 {
5374 tree t = TREE_OPERAND (expr, 0);
5375 tree best = t;
5376 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5377
72dd6141 5378 while (CONVERT_EXPR_P (t)
7cc7e163 5379 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5380 {
5381 int thisalign;
5382
5383 t = TREE_OPERAND (t, 0);
5384 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5385 if (thisalign > bestalign)
5386 best = t, bestalign = thisalign;
5387 }
e60a6f7b 5388 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5389 }
5390 else
e60a6f7b 5391 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5392
389dd41b 5393 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5394}
5395\f
8fe4a266 5396/* Handle C and C++ default attributes. */
5397
5398enum built_in_attribute
5399{
5400#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5401#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5402#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5403#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5404#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5405#include "builtin-attrs.def"
5406#undef DEF_ATTR_NULL_TREE
5407#undef DEF_ATTR_INT
c8010b80 5408#undef DEF_ATTR_STRING
8fe4a266 5409#undef DEF_ATTR_IDENT
5410#undef DEF_ATTR_TREE_LIST
8fe4a266 5411 ATTR_LAST
5412};
5413
5414static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5415
1cae46be 5416static void c_init_attributes (void);
8fe4a266 5417
27213ba3 5418enum c_builtin_type
72040e7e 5419{
d2d4bdde 5420#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5421#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5422#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5423#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5424#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5425#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5426#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5427#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5428 ARG6) NAME,
5429#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5430 ARG6, ARG7) NAME,
5431#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5432 ARG6, ARG7, ARG8) NAME,
d2d4bdde 5433#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5434#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5435#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5436#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5437#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5438#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5439 NAME,
5440#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5441 ARG6, ARG7) NAME,
5442#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5443 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5444#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5445#include "builtin-types.def"
5446#undef DEF_PRIMITIVE_TYPE
5447#undef DEF_FUNCTION_TYPE_0
5448#undef DEF_FUNCTION_TYPE_1
5449#undef DEF_FUNCTION_TYPE_2
5450#undef DEF_FUNCTION_TYPE_3
5451#undef DEF_FUNCTION_TYPE_4
0a39fd54 5452#undef DEF_FUNCTION_TYPE_5
5453#undef DEF_FUNCTION_TYPE_6
27213ba3 5454#undef DEF_FUNCTION_TYPE_7
bc7bff74 5455#undef DEF_FUNCTION_TYPE_8
d2d4bdde 5456#undef DEF_FUNCTION_TYPE_VAR_0
5457#undef DEF_FUNCTION_TYPE_VAR_1
5458#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5459#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5460#undef DEF_FUNCTION_TYPE_VAR_4
5461#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5462#undef DEF_FUNCTION_TYPE_VAR_7
5463#undef DEF_FUNCTION_TYPE_VAR_11
d2d4bdde 5464#undef DEF_POINTER_TYPE
27213ba3 5465 BT_LAST
5466};
5467
5468typedef enum c_builtin_type builtin_type;
d2d4bdde 5469
27213ba3 5470/* A temporary array for c_common_nodes_and_builtins. Used in
5471 communication with def_fn_type. */
5472static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5473
27213ba3 5474/* A helper function for c_common_nodes_and_builtins. Build function type
5475 for DEF with return type RET and N arguments. If VAR is true, then the
5476 function should be variadic after those N arguments.
5477
5478 Takes special care not to ICE if any of the types involved are
5479 error_mark_node, which indicates that said type is not in fact available
5480 (see builtin_type_for_size). In which case the function type as a whole
5481 should be error_mark_node. */
5482
5483static void
5484def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5485{
3a939d12 5486 tree t;
5487 tree *args = XALLOCAVEC (tree, n);
27213ba3 5488 va_list list;
5489 int i;
5490
5491 va_start (list, n);
5492 for (i = 0; i < n; ++i)
5493 {
7d339f93 5494 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5495 t = builtin_types[a];
5496 if (t == error_mark_node)
5497 goto egress;
3a939d12 5498 args[i] = t;
27213ba3 5499 }
27213ba3 5500
27213ba3 5501 t = builtin_types[ret];
5502 if (t == error_mark_node)
5503 goto egress;
3a939d12 5504 if (var)
5505 t = build_varargs_function_type_array (t, n, args);
5506 else
5507 t = build_function_type_array (t, n, args);
27213ba3 5508
5509 egress:
5510 builtin_types[def] = t;
451c8e2f 5511 va_end (list);
27213ba3 5512}
5513
dce22712 5514/* Build builtin functions common to both C and C++ language
5515 frontends. */
5516
5517static void
5518c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5519{
5520#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5521 builtin_types[ENUM] = VALUE;
5522#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5523 def_fn_type (ENUM, RETURN, 0, 0);
5524#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5525 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5526#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5527 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5528#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5529 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5530#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5531 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5532#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5533 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5534#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5535 ARG6) \
5536 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5537#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5538 ARG6, ARG7) \
5539 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5540#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5541 ARG6, ARG7, ARG8) \
5542 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5543 ARG7, ARG8);
dce22712 5544#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5545 def_fn_type (ENUM, RETURN, 1, 0);
5546#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5547 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5548#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5549 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5550#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5551 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5552#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5553 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5554#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5555 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6349b8cc 5556#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5557 ARG6, ARG7) \
5558 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5559#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5560 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5561 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5562 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5563#define DEF_POINTER_TYPE(ENUM, TYPE) \
5564 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5565
5566#include "builtin-types.def"
5567
5568#undef DEF_PRIMITIVE_TYPE
3c77ca67 5569#undef DEF_FUNCTION_TYPE_0
dce22712 5570#undef DEF_FUNCTION_TYPE_1
5571#undef DEF_FUNCTION_TYPE_2
5572#undef DEF_FUNCTION_TYPE_3
5573#undef DEF_FUNCTION_TYPE_4
5574#undef DEF_FUNCTION_TYPE_5
5575#undef DEF_FUNCTION_TYPE_6
3c77ca67 5576#undef DEF_FUNCTION_TYPE_7
5577#undef DEF_FUNCTION_TYPE_8
dce22712 5578#undef DEF_FUNCTION_TYPE_VAR_0
5579#undef DEF_FUNCTION_TYPE_VAR_1
5580#undef DEF_FUNCTION_TYPE_VAR_2
5581#undef DEF_FUNCTION_TYPE_VAR_3
5582#undef DEF_FUNCTION_TYPE_VAR_4
5583#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5584#undef DEF_FUNCTION_TYPE_VAR_7
5585#undef DEF_FUNCTION_TYPE_VAR_11
dce22712 5586#undef DEF_POINTER_TYPE
5587 builtin_types[(int) BT_LAST] = NULL_TREE;
5588
5589 c_init_attributes ();
5590
5591#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5592 NONANSI_P, ATTRS, IMPLICIT, COND) \
5593 if (NAME && COND) \
5594 def_builtin_1 (ENUM, NAME, CLASS, \
5595 builtin_types[(int) TYPE], \
5596 builtin_types[(int) LIBTYPE], \
5597 BOTH_P, FALLBACK_P, NONANSI_P, \
5598 built_in_attributes[(int) ATTRS], IMPLICIT);
5599#include "builtins.def"
5600#undef DEF_BUILTIN
5601
87eb1c28 5602 targetm.init_builtins ();
5603
471eff36 5604 build_common_builtin_nodes ();
dce22712 5605
a89e6c15 5606 if (flag_cilkplus)
d037099f 5607 cilk_init_builtins ();
dce22712 5608}
5609
9e6687c8 5610/* Like get_identifier, but avoid warnings about null arguments when
5611 the argument may be NULL for targets where GCC lacks stdint.h type
5612 information. */
5613
5614static inline tree
5615c_get_ident (const char *id)
5616{
5617 return get_identifier (id);
5618}
5619
27213ba3 5620/* Build tree nodes and builtin functions common to both C and C++ language
5621 frontends. */
5622
5623void
5624c_common_nodes_and_builtins (void)
5625{
924bbf02 5626 int char16_type_size;
5627 int char32_type_size;
174fcc61 5628 int wchar_type_size;
5629 tree array_domain_type;
2d47cc32 5630 tree va_list_ref_type_node;
8a15c04a 5631 tree va_list_arg_type_node;
9f75f026 5632 int i;
a66c9326 5633
c38a75b7 5634 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5635
174fcc61 5636 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5637 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5638 record_builtin_type (RID_CHAR, "char", char_type_node);
5639
5640 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5641 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5642 but not C. Are the conditionals here needed? */
c0f19401 5643 if (c_dialect_cxx ())
d946ea19 5644 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5645 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5646 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5647 record_builtin_type (RID_MAX, "long unsigned int",
5648 long_unsigned_type_node);
9f75f026 5649
5650 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5651 {
9f75f026 5652 char name[25];
5653
5654 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5655 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5656 int_n_trees[i].signed_type);
5657 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5658 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5659 }
9f75f026 5660
c0f19401 5661 if (c_dialect_cxx ())
174fcc61 5662 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5663 record_builtin_type (RID_MAX, "long long int",
5664 long_long_integer_type_node);
5665 record_builtin_type (RID_MAX, "long long unsigned int",
5666 long_long_unsigned_type_node);
c0f19401 5667 if (c_dialect_cxx ())
174fcc61 5668 record_builtin_type (RID_MAX, "long long unsigned",
5669 long_long_unsigned_type_node);
5670 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5671 record_builtin_type (RID_MAX, "short unsigned int",
5672 short_unsigned_type_node);
c0f19401 5673 if (c_dialect_cxx ())
174fcc61 5674 record_builtin_type (RID_MAX, "unsigned short",
5675 short_unsigned_type_node);
5676
5677 /* Define both `signed char' and `unsigned char'. */
5678 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5679 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5680
771d21fa 5681 /* These are types that c_common_type_for_size and
5682 c_common_type_for_mode use. */
e60a6f7b 5683 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5684 TYPE_DECL, NULL_TREE,
dc24ddbd 5685 intQI_type_node));
e60a6f7b 5686 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5687 TYPE_DECL, NULL_TREE,
dc24ddbd 5688 intHI_type_node));
e60a6f7b 5689 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5690 TYPE_DECL, NULL_TREE,
dc24ddbd 5691 intSI_type_node));
e60a6f7b 5692 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5693 TYPE_DECL, NULL_TREE,
dc24ddbd 5694 intDI_type_node));
174fcc61 5695#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5696 /* Note that this is different than the __int128 type that's part of
5697 the generic __intN support. */
f1515a39 5698 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5699 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5700 TYPE_DECL,
f1515a39 5701 get_identifier ("__int128_t"),
5702 intTI_type_node));
174fcc61 5703#endif
e60a6f7b 5704 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5705 TYPE_DECL, NULL_TREE,
dc24ddbd 5706 unsigned_intQI_type_node));
e60a6f7b 5707 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5708 TYPE_DECL, NULL_TREE,
dc24ddbd 5709 unsigned_intHI_type_node));
e60a6f7b 5710 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5711 TYPE_DECL, NULL_TREE,
dc24ddbd 5712 unsigned_intSI_type_node));
e60a6f7b 5713 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5714 TYPE_DECL, NULL_TREE,
dc24ddbd 5715 unsigned_intDI_type_node));
174fcc61 5716#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5717 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5718 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5719 TYPE_DECL,
f1515a39 5720 get_identifier ("__uint128_t"),
5721 unsigned_intTI_type_node));
174fcc61 5722#endif
5723
5724 /* Create the widest literal types. */
5725 widest_integer_literal_type_node
5726 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5727 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5728 TYPE_DECL, NULL_TREE,
dc24ddbd 5729 widest_integer_literal_type_node));
174fcc61 5730
5731 widest_unsigned_literal_type_node
5732 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5733 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5734 TYPE_DECL, NULL_TREE,
dc24ddbd 5735 widest_unsigned_literal_type_node));
174fcc61 5736
654ef926 5737 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5738
73673831 5739 pid_type_node =
5740 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5741
d946ea19 5742 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5743 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5744 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5745
c4503c0a 5746 /* Only supported decimal floating point extension if the target
5747 actually supports underlying modes. */
48e1416a 5748 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5749 && targetm.scalar_mode_supported_p (DDmode)
5750 && targetm.scalar_mode_supported_p (TDmode))
5751 {
5752 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5753 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5754 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5755 }
5756
9421ebb9 5757 if (targetm.fixed_point_supported_p ())
5758 {
5759 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5760 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5761 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5762 record_builtin_type (RID_MAX, "long long _Fract",
5763 long_long_fract_type_node);
5764 record_builtin_type (RID_MAX, "unsigned short _Fract",
5765 unsigned_short_fract_type_node);
5766 record_builtin_type (RID_MAX, "unsigned _Fract",
5767 unsigned_fract_type_node);
5768 record_builtin_type (RID_MAX, "unsigned long _Fract",
5769 unsigned_long_fract_type_node);
5770 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5771 unsigned_long_long_fract_type_node);
5772 record_builtin_type (RID_MAX, "_Sat short _Fract",
5773 sat_short_fract_type_node);
5774 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5775 record_builtin_type (RID_MAX, "_Sat long _Fract",
5776 sat_long_fract_type_node);
5777 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5778 sat_long_long_fract_type_node);
5779 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5780 sat_unsigned_short_fract_type_node);
5781 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5782 sat_unsigned_fract_type_node);
5783 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5784 sat_unsigned_long_fract_type_node);
5785 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5786 sat_unsigned_long_long_fract_type_node);
5787 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5788 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5789 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5790 record_builtin_type (RID_MAX, "long long _Accum",
5791 long_long_accum_type_node);
5792 record_builtin_type (RID_MAX, "unsigned short _Accum",
5793 unsigned_short_accum_type_node);
5794 record_builtin_type (RID_MAX, "unsigned _Accum",
5795 unsigned_accum_type_node);
5796 record_builtin_type (RID_MAX, "unsigned long _Accum",
5797 unsigned_long_accum_type_node);
5798 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5799 unsigned_long_long_accum_type_node);
5800 record_builtin_type (RID_MAX, "_Sat short _Accum",
5801 sat_short_accum_type_node);
5802 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5803 record_builtin_type (RID_MAX, "_Sat long _Accum",
5804 sat_long_accum_type_node);
5805 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5806 sat_long_long_accum_type_node);
5807 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5808 sat_unsigned_short_accum_type_node);
5809 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5810 sat_unsigned_accum_type_node);
5811 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5812 sat_unsigned_long_accum_type_node);
5813 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5814 sat_unsigned_long_long_accum_type_node);
5815
5816 }
5817
e60a6f7b 5818 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5819 TYPE_DECL,
dc24ddbd 5820 get_identifier ("complex int"),
5821 complex_integer_type_node));
e60a6f7b 5822 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5823 TYPE_DECL,
dc24ddbd 5824 get_identifier ("complex float"),
5825 complex_float_type_node));
e60a6f7b 5826 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5827 TYPE_DECL,
dc24ddbd 5828 get_identifier ("complex double"),
5829 complex_double_type_node));
5830 lang_hooks.decls.pushdecl
e60a6f7b 5831 (build_decl (UNKNOWN_LOCATION,
5832 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5833 complex_long_double_type_node));
174fcc61 5834
e256d445 5835 if (c_dialect_cxx ())
5836 /* For C++, make fileptr_type_node a distinct void * type until
5837 FILE type is defined. */
e086912e 5838 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5839
d946ea19 5840 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5841
6753bca0 5842 /* Set the TYPE_NAME for any variants that were built before
5843 record_builtin_type gave names to the built-in types. */
5844 {
5845 tree void_name = TYPE_NAME (void_type_node);
5846 TYPE_NAME (void_type_node) = NULL_TREE;
5847 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5848 = void_name;
5849 TYPE_NAME (void_type_node) = void_name;
5850 }
5851
174fcc61 5852 void_list_node = build_void_list_node ();
5853
5854 /* Make a type to be the domain of a few array types
5855 whose domains don't really matter.
5856 200 is small enough that it always fits in size_t
5857 and large enough that it can hold most function names for the
5858 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5859 array_domain_type = build_index_type (size_int (200));
5860
5861 /* Make a type for arrays of characters.
5862 With luck nothing will ever really depend on the length of this
5863 array type. */
5864 char_array_type_node
5865 = build_array_type (char_type_node, array_domain_type);
5866
d2d4bdde 5867 string_type_node = build_pointer_type (char_type_node);
5868 const_string_type_node
5869 = build_pointer_type (build_qualified_type
5870 (char_type_node, TYPE_QUAL_CONST));
5871
174fcc61 5872 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5873 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5874 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5875 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5876 underlying_wchar_type_node = wchar_type_node;
c0f19401 5877 if (c_dialect_cxx ())
174fcc61 5878 {
78a8ed03 5879 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5880 wchar_type_node = make_unsigned_type (wchar_type_size);
5881 else
5882 wchar_type_node = make_signed_type (wchar_type_size);
5883 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5884 }
174fcc61 5885
5886 /* This is for wide string constants. */
5887 wchar_array_type_node
5888 = build_array_type (wchar_type_node, array_domain_type);
5889
924bbf02 5890 /* Define 'char16_t'. */
5891 char16_type_node = get_identifier (CHAR16_TYPE);
5892 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5893 char16_type_size = TYPE_PRECISION (char16_type_node);
5894 if (c_dialect_cxx ())
5895 {
5896 char16_type_node = make_unsigned_type (char16_type_size);
5897
60777f69 5898 if (cxx_dialect >= cxx11)
924bbf02 5899 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5900 }
5901
5902 /* This is for UTF-16 string constants. */
5903 char16_array_type_node
5904 = build_array_type (char16_type_node, array_domain_type);
5905
5906 /* Define 'char32_t'. */
5907 char32_type_node = get_identifier (CHAR32_TYPE);
5908 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5909 char32_type_size = TYPE_PRECISION (char32_type_node);
5910 if (c_dialect_cxx ())
5911 {
5912 char32_type_node = make_unsigned_type (char32_type_size);
5913
60777f69 5914 if (cxx_dialect >= cxx11)
924bbf02 5915 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5916 }
5917
5918 /* This is for UTF-32 string constants. */
5919 char32_array_type_node
5920 = build_array_type (char32_type_node, array_domain_type);
5921
6bf5ed8d 5922 wint_type_node =
5923 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5924
5925 intmax_type_node =
5926 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5927 uintmax_type_node =
5928 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5929
f3449a3c 5930 if (SIG_ATOMIC_TYPE)
5931 sig_atomic_type_node =
9e6687c8 5932 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5933 if (INT8_TYPE)
5934 int8_type_node =
9e6687c8 5935 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5936 if (INT16_TYPE)
5937 int16_type_node =
9e6687c8 5938 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5939 if (INT32_TYPE)
5940 int32_type_node =
9e6687c8 5941 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5942 if (INT64_TYPE)
5943 int64_type_node =
9e6687c8 5944 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5945 if (UINT8_TYPE)
5946 uint8_type_node =
9e6687c8 5947 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5948 if (UINT16_TYPE)
d1081017 5949 c_uint16_type_node = uint16_type_node =
9e6687c8 5950 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5951 if (UINT32_TYPE)
d1081017 5952 c_uint32_type_node = uint32_type_node =
9e6687c8 5953 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5954 if (UINT64_TYPE)
d1081017 5955 c_uint64_type_node = uint64_type_node =
9e6687c8 5956 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5957 if (INT_LEAST8_TYPE)
5958 int_least8_type_node =
9e6687c8 5959 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5960 if (INT_LEAST16_TYPE)
5961 int_least16_type_node =
9e6687c8 5962 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5963 if (INT_LEAST32_TYPE)
5964 int_least32_type_node =
9e6687c8 5965 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5966 if (INT_LEAST64_TYPE)
5967 int_least64_type_node =
9e6687c8 5968 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5969 if (UINT_LEAST8_TYPE)
5970 uint_least8_type_node =
9e6687c8 5971 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5972 if (UINT_LEAST16_TYPE)
5973 uint_least16_type_node =
9e6687c8 5974 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5975 if (UINT_LEAST32_TYPE)
5976 uint_least32_type_node =
9e6687c8 5977 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5978 if (UINT_LEAST64_TYPE)
5979 uint_least64_type_node =
9e6687c8 5980 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5981 if (INT_FAST8_TYPE)
5982 int_fast8_type_node =
9e6687c8 5983 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5984 if (INT_FAST16_TYPE)
5985 int_fast16_type_node =
9e6687c8 5986 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5987 if (INT_FAST32_TYPE)
5988 int_fast32_type_node =
9e6687c8 5989 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5990 if (INT_FAST64_TYPE)
5991 int_fast64_type_node =
9e6687c8 5992 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 5993 if (UINT_FAST8_TYPE)
5994 uint_fast8_type_node =
9e6687c8 5995 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 5996 if (UINT_FAST16_TYPE)
5997 uint_fast16_type_node =
9e6687c8 5998 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 5999 if (UINT_FAST32_TYPE)
6000 uint_fast32_type_node =
9e6687c8 6001 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6002 if (UINT_FAST64_TYPE)
6003 uint_fast64_type_node =
9e6687c8 6004 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6005 if (INTPTR_TYPE)
6006 intptr_type_node =
9e6687c8 6007 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6008 if (UINTPTR_TYPE)
6009 uintptr_type_node =
9e6687c8 6010 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6011
3a939d12 6012 default_function_type
6013 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6014 ptrdiff_type_node
6015 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6016 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6017
dc24ddbd 6018 lang_hooks.decls.pushdecl
e60a6f7b 6019 (build_decl (UNKNOWN_LOCATION,
6020 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6021 va_list_type_node));
202d6e5f 6022 if (targetm.enum_va_list_p)
acd6f472 6023 {
6024 int l;
6025 const char *pname;
6026 tree ptype;
5f57a8b1 6027
202d6e5f 6028 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6029 {
6030 lang_hooks.decls.pushdecl
6031 (build_decl (UNKNOWN_LOCATION,
6032 TYPE_DECL, get_identifier (pname),
6033 ptype));
ab2c1de8 6034
acd6f472 6035 }
6036 }
8a15c04a 6037
8a15c04a 6038 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6039 {
6040 va_list_arg_type_node = va_list_ref_type_node =
6041 build_pointer_type (TREE_TYPE (va_list_type_node));
6042 }
8a15c04a 6043 else
2d47cc32 6044 {
6045 va_list_arg_type_node = va_list_type_node;
6046 va_list_ref_type_node = build_reference_type (va_list_type_node);
6047 }
1cae46be 6048
dce22712 6049 if (!flag_preprocess_only)
6050 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6051
5c62f199 6052 main_identifier_node = get_identifier ("main");
ae84079f 6053
6054 /* Create the built-in __null node. It is important that this is
6055 not shared. */
271e739a 6056 null_node = make_int_cst (1, 1);
ae84079f 6057 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6058
6059 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6060 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6061}
a66c9326 6062
79b01846 6063/* The number of named compound-literals generated thus far. */
6064static GTY(()) int compound_literal_number;
6065
6066/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6067
6068void
6069set_compound_literal_name (tree decl)
6070{
6071 char *name;
6072 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6073 compound_literal_number);
6074 compound_literal_number++;
6075 DECL_NAME (decl) = get_identifier (name);
6076}
6077
2e474820 6078/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6079 TYPE and operand OP. */
6080
6081static tree
6082build_va_arg_1 (location_t loc, tree type, tree op)
6083{
6084 tree expr = build1 (VA_ARG_EXPR, type, op);
6085 SET_EXPR_LOCATION (expr, loc);
6086 return expr;
6087}
6088
6089/* Return a VA_ARG_EXPR corresponding to a source-level expression
6090 va_arg (EXPR, TYPE) at source location LOC. */
6091
a66c9326 6092tree
e60a6f7b 6093build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6094{
c37be9ec 6095 tree va_type = TREE_TYPE (expr);
6096 tree canon_va_type = (va_type == error_mark_node
6097 ? NULL_TREE
6098 : targetm.canonical_va_list_type (va_type));
6099
2e474820 6100 if (va_type == error_mark_node
6101 || canon_va_type == NULL_TREE)
c37be9ec 6102 {
2e474820 6103 /* Let's handle things neutrallly, if expr:
6104 - has undeclared type, or
6105 - is not an va_list type. */
6106 return build_va_arg_1 (loc, type, expr);
c37be9ec 6107 }
f7fec1c7 6108
2e474820 6109 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6110 {
6111 /* Case 1: Not an array type. */
6112
6113 /* Take the address, to get '&ap'. */
6114 mark_addressable (expr);
6115 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6116
6117 /* Verify that &ap is still recognized as having va_list type. */
6118 tree canon_expr_type
6119 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6120 gcc_assert (canon_expr_type != NULL_TREE);
6121
6122 return build_va_arg_1 (loc, type, expr);
6123 }
6124
6125 /* Case 2: Array type.
6126
6127 Background:
6128
6129 For contrast, let's start with the simple case (case 1). If
6130 canon_va_type is not an array type, but say a char *, then when
6131 passing-by-value a va_list, the type of the va_list param decl is
6132 the same as for another va_list decl (all ap's are char *):
6133
6134 f2_1 (char * ap)
6135 D.1815 = VA_ARG (&ap, 0B, 1);
6136 return D.1815;
6137
6138 f2 (int i)
6139 char * ap.0;
6140 char * ap;
6141 __builtin_va_start (&ap, 0);
6142 ap.0 = ap;
6143 res = f2_1 (ap.0);
6144 __builtin_va_end (&ap);
6145 D.1812 = res;
6146 return D.1812;
6147
6148 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6149 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6150 the same as for another va_list decl (case 2a, struct ap[1]).
6151
6152 f2_1 (struct * ap)
6153 D.1844 = VA_ARG (ap, 0B, 0);
6154 return D.1844;
6155
6156 f2 (int i)
6157 struct ap[1];
6158 __builtin_va_start (&ap, 0);
6159 res = f2_1 (&ap);
6160 __builtin_va_end (&ap);
6161 D.1841 = res;
6162 return D.1841;
6163
6164 Case 2b is different because:
6165 - on the callee side, the parm decl has declared type va_list, but
6166 grokdeclarator changes the type of the parm decl to a pointer to the
6167 array elem type.
6168 - on the caller side, the pass-by-value uses &ap.
6169
6170 We unify these two cases (case 2a: va_list is array type,
6171 case 2b: va_list is pointer to array elem type), by adding '&' for the
6172 array type case, such that we have a pointer to array elem in both
6173 cases. */
6174
6175 if (TREE_CODE (va_type) == ARRAY_TYPE)
6176 {
6177 /* Case 2a: va_list is array type. */
6178
6179 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6180 elem type. */
6181 mark_addressable (expr);
6182 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6183 expr);
6184
6185 /* Verify that &ap is still recognized as having va_list type. */
6186 tree canon_expr_type
6187 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6188 gcc_assert (canon_expr_type != NULL_TREE);
6189 }
6190 else
6191 {
6192 /* Case 2b: va_list is pointer to array elem type. */
6193 gcc_assert (POINTER_TYPE_P (va_type));
6194 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6195
6196 /* Don't take the address. We've already got '&ap'. */
6197 ;
6198 }
6199
6200 return build_va_arg_1 (loc, type, expr);
a66c9326 6201}
0d4238dc 6202
6203
dd878098 6204/* Linked list of disabled built-in functions. */
6205
6206typedef struct disabled_builtin
6207{
6208 const char *name;
6209 struct disabled_builtin *next;
6210} disabled_builtin;
6211static disabled_builtin *disabled_builtins = NULL;
6212
1cae46be 6213static bool builtin_function_disabled_p (const char *);
dd878098 6214
6215/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6216 begins with "__builtin_", give an error. */
6217
6218void
1cae46be 6219disable_builtin_function (const char *name)
dd878098 6220{
6221 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6222 error ("cannot disable built-in function %qs", name);
dd878098 6223 else
6224 {
e85905e5 6225 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6226 new_disabled_builtin->name = name;
6227 new_disabled_builtin->next = disabled_builtins;
6228 disabled_builtins = new_disabled_builtin;
dd878098 6229 }
6230}
6231
6232
6233/* Return true if the built-in function NAME has been disabled, false
6234 otherwise. */
6235
6236static bool
1cae46be 6237builtin_function_disabled_p (const char *name)
dd878098 6238{
6239 disabled_builtin *p;
6240 for (p = disabled_builtins; p != NULL; p = p->next)
6241 {
6242 if (strcmp (name, p->name) == 0)
6243 return true;
6244 }
6245 return false;
6246}
6247
6248
3237155d 6249/* Worker for DEF_BUILTIN.
6250 Possibly define a builtin function with one or two names.
6251 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6252 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6253
3237155d 6254static void
6255def_builtin_1 (enum built_in_function fncode,
6256 const char *name,
6257 enum built_in_class fnclass,
6258 tree fntype, tree libtype,
6259 bool both_p, bool fallback_p, bool nonansi_p,
6260 tree fnattrs, bool implicit_p)
0d4238dc 6261{
3237155d 6262 tree decl;
6263 const char *libname;
6264
27213ba3 6265 if (fntype == error_mark_node)
6266 return;
6267
3237155d 6268 gcc_assert ((!both_p && !fallback_p)
6269 || !strncmp (name, "__builtin_",
6270 strlen ("__builtin_")));
6271
6272 libname = name + strlen ("__builtin_");
54be5d7e 6273 decl = add_builtin_function (name, fntype, fncode, fnclass,
6274 (fallback_p ? libname : NULL),
6275 fnattrs);
b9a16870 6276
6277 set_builtin_decl (fncode, decl, implicit_p);
6278
3237155d 6279 if (both_p
6280 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6281 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6282 add_builtin_function (libname, libtype, fncode, fnclass,
6283 NULL, fnattrs);
0d4238dc 6284}
e94026da 6285\f
d7aeef06 6286/* Nonzero if the type T promotes to int. This is (nearly) the
6287 integral promotions defined in ISO C99 6.3.1.1/2. */
6288
6289bool
9f627b1a 6290c_promoting_integer_type_p (const_tree t)
d7aeef06 6291{
6292 switch (TREE_CODE (t))
6293 {
6294 case INTEGER_TYPE:
6295 return (TYPE_MAIN_VARIANT (t) == char_type_node
6296 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6297 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6298 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6299 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6300 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6301
6302 case ENUMERAL_TYPE:
6303 /* ??? Technically all enumerations not larger than an int
6304 promote to an int. But this is used along code paths
6305 that only want to notice a size change. */
6306 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6307
6308 case BOOLEAN_TYPE:
6309 return 1;
6310
6311 default:
6312 return 0;
6313 }
6314}
6315
e94026da 6316/* Return 1 if PARMS specifies a fixed number of parameters
6317 and none of their types is affected by default promotions. */
6318
6319int
9f627b1a 6320self_promoting_args_p (const_tree parms)
e94026da 6321{
9f627b1a 6322 const_tree t;
e94026da 6323 for (t = parms; t; t = TREE_CHAIN (t))
6324 {
19cb6b50 6325 tree type = TREE_VALUE (t);
43f74bc4 6326
e1d8e198 6327 if (type == error_mark_node)
6328 continue;
6329
e94026da 6330 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6331 return 0;
6332
6333 if (type == 0)
6334 return 0;
6335
6336 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6337 return 0;
6338
d7aeef06 6339 if (c_promoting_integer_type_p (type))
e94026da 6340 return 0;
6341 }
6342 return 1;
6343}
605fb01e 6344
c10de5e7 6345/* Recursively remove any '*' or '&' operator from TYPE. */
6346tree
6347strip_pointer_operator (tree t)
6348{
6349 while (POINTER_TYPE_P (t))
6350 t = TREE_TYPE (t);
6351 return t;
6352}
6353
57a0ed23 6354/* Recursively remove pointer or array type from TYPE. */
6355tree
6356strip_pointer_or_array_types (tree t)
6357{
6358 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6359 t = TREE_TYPE (t);
6360 return t;
6361}
6362
e41f0d80 6363/* Used to compare case labels. K1 and K2 are actually tree nodes
6364 representing case labels, or NULL_TREE for a `default' label.
6365 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6366 K2, and 0 if K1 and K2 are equal. */
6367
6368int
1cae46be 6369case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6370{
6371 /* Consider a NULL key (such as arises with a `default' label) to be
6372 smaller than anything else. */
6373 if (!k1)
6374 return k2 ? -1 : 0;
6375 else if (!k2)
6376 return k1 ? 1 : 0;
6377
6378 return tree_int_cst_compare ((tree) k1, (tree) k2);
6379}
6380
e60a6f7b 6381/* Process a case label, located at LOC, for the range LOW_VALUE
6382 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6383 then this case label is actually a `default' label. If only
6384 HIGH_VALUE is NULL_TREE, then case label was declared using the
6385 usual C/C++ syntax, rather than the GNU case range extension.
6386 CASES is a tree containing all the case ranges processed so far;
6387 COND is the condition for the switch-statement itself. Returns the
6388 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6389 is created. */
e41f0d80 6390
6391tree
e60a6f7b 6392c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
2ca392fd 6393 tree low_value, tree high_value)
e41f0d80 6394{
6395 tree type;
6396 tree label;
6397 tree case_label;
6398 splay_tree_node node;
6399
6400 /* Create the LABEL_DECL itself. */
e60a6f7b 6401 label = create_artificial_label (loc);
e41f0d80 6402
6403 /* If there was an error processing the switch condition, bail now
6404 before we get more confused. */
6405 if (!cond || cond == error_mark_node)
4ee9c684 6406 goto error_out;
e41f0d80 6407
1cae46be 6408 if ((low_value && TREE_TYPE (low_value)
6409 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6410 || (high_value && TREE_TYPE (high_value)
6411 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6412 {
e60a6f7b 6413 error_at (loc, "pointers are not permitted as case values");
b96dc121 6414 goto error_out;
6415 }
e41f0d80 6416
6417 /* Case ranges are a GNU extension. */
8864917d 6418 if (high_value)
29438999 6419 pedwarn (loc, OPT_Wpedantic,
8864917d 6420 "range expressions in switch statements are non-standard");
e41f0d80 6421
6422 type = TREE_TYPE (cond);
6423 if (low_value)
6424 {
2d2f6a15 6425 low_value = check_case_value (loc, low_value);
22a75734 6426 low_value = convert_and_check (loc, type, low_value);
96722196 6427 if (low_value == error_mark_node)
6428 goto error_out;
e41f0d80 6429 }
6430 if (high_value)
6431 {
2d2f6a15 6432 high_value = check_case_value (loc, high_value);
22a75734 6433 high_value = convert_and_check (loc, type, high_value);
96722196 6434 if (high_value == error_mark_node)
6435 goto error_out;
e41f0d80 6436 }
6437
96722196 6438 if (low_value && high_value)
6439 {
6440 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6441 really a case range, even though it was written that way.
6442 Remove the HIGH_VALUE to simplify later processing. */
96722196 6443 if (tree_int_cst_equal (low_value, high_value))
6444 high_value = NULL_TREE;
6445 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6446 warning_at (loc, 0, "empty range specified");
96722196 6447 }
e41f0d80 6448
2ca392fd 6449 /* See if the case is in range of the type of the original testing
6450 expression. If both low_value and high_value are out of range,
6451 don't insert the case label and return NULL_TREE. */
6452 if (low_value
f61a9bc2 6453 && !check_case_bounds (loc, type, orig_type,
84166705 6454 &low_value, high_value ? &high_value : NULL))
2ca392fd 6455 return NULL_TREE;
6456
e41f0d80 6457 /* Look up the LOW_VALUE in the table of case labels we already
6458 have. */
6459 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6460 /* If there was not an exact match, check for overlapping ranges.
6461 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6462 that's a `default' label and the only overlap is an exact match. */
6463 if (!node && (low_value || high_value))
6464 {
6465 splay_tree_node low_bound;
6466 splay_tree_node high_bound;
6467
6468 /* Even though there wasn't an exact match, there might be an
6469 overlap between this case range and another case range.
6470 Since we've (inductively) not allowed any overlapping case
6471 ranges, we simply need to find the greatest low case label
6472 that is smaller that LOW_VALUE, and the smallest low case
6473 label that is greater than LOW_VALUE. If there is an overlap
6474 it will occur in one of these two ranges. */
6475 low_bound = splay_tree_predecessor (cases,
6476 (splay_tree_key) low_value);
6477 high_bound = splay_tree_successor (cases,
6478 (splay_tree_key) low_value);
6479
6480 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6481 the LOW_VALUE, so there is no need to check unless the
6482 LOW_BOUND is in fact itself a case range. */
6483 if (low_bound
6484 && CASE_HIGH ((tree) low_bound->value)
6485 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6486 low_value) >= 0)
6487 node = low_bound;
6488 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6489 range is bigger than the low end of the current range, so we
6490 are only interested if the current range is a real range, and
6491 not an ordinary case label. */
1cae46be 6492 else if (high_bound
e41f0d80 6493 && high_value
6494 && (tree_int_cst_compare ((tree) high_bound->key,
6495 high_value)
6496 <= 0))
6497 node = high_bound;
6498 }
6499 /* If there was an overlap, issue an error. */
6500 if (node)
6501 {
eaae3b75 6502 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6503
6504 if (high_value)
6505 {
e60a6f7b 6506 error_at (loc, "duplicate (or overlapping) case value");
6507 error_at (DECL_SOURCE_LOCATION (duplicate),
6508 "this is the first entry overlapping that value");
e41f0d80 6509 }
6510 else if (low_value)
6511 {
e60a6f7b 6512 error_at (loc, "duplicate case value") ;
6513 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6514 }
6515 else
6516 {
e60a6f7b 6517 error_at (loc, "multiple default labels in one switch");
6518 error_at (DECL_SOURCE_LOCATION (duplicate),
6519 "this is the first default label");
e41f0d80 6520 }
4ee9c684 6521 goto error_out;
e41f0d80 6522 }
6523
6524 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6525 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6526 /* Register this case label in the splay tree. */
1cae46be 6527 splay_tree_insert (cases,
e41f0d80 6528 (splay_tree_key) low_value,
6529 (splay_tree_value) case_label);
6530
6531 return case_label;
4ee9c684 6532
6533 error_out:
daf6dff5 6534 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6535 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6536 that just leads to duplicates and thence to failure later on. */
4ee9c684 6537 if (!cases->root)
6538 {
e60a6f7b 6539 tree t = create_artificial_label (loc);
6540 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6541 }
6542 return error_mark_node;
6543}
6544
6545/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6546 Used to verify that case values match up with enumerator values. */
6547
6548static void
6549match_case_to_enum_1 (tree key, tree type, tree label)
6550{
e913b5cd 6551 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6552
dd76621f 6553 if (tree_fits_uhwi_p (key))
6554 print_dec (key, buf, UNSIGNED);
6555 else if (tree_fits_shwi_p (key))
6556 print_dec (key, buf, SIGNED);
4ee9c684 6557 else
e913b5cd 6558 print_hex (key, buf);
4ee9c684 6559
6560 if (TYPE_NAME (type) == 0)
712d2297 6561 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6562 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6563 "case value %qs not in enumerated type",
6564 buf);
4ee9c684 6565 else
712d2297 6566 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6567 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6568 "case value %qs not in enumerated type %qT",
6569 buf, type);
4ee9c684 6570}
6571
359d87c6 6572/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6573 Used to verify that case values match up with enumerator values. */
6574
4ee9c684 6575static int
6576match_case_to_enum (splay_tree_node node, void *data)
6577{
6578 tree label = (tree) node->value;
4fd61bc6 6579 tree type = (tree) data;
4ee9c684 6580
6581 /* Skip default case. */
6582 if (!CASE_LOW (label))
6583 return 0;
6584
359d87c6 6585 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6586 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6587 if (!CASE_LOW_SEEN (label))
4ee9c684 6588 match_case_to_enum_1 (CASE_LOW (label), type, label);
6589 else
359d87c6 6590 CASE_LOW_SEEN (label) = 0;
4ee9c684 6591
359d87c6 6592 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6593 not set, that means that CASE_HIGH did not appear when we did our
6594 enum->case scan. Reset our scratch bit after. */
4ee9c684 6595 if (CASE_HIGH (label))
6596 {
359d87c6 6597 if (!CASE_HIGH_SEEN (label))
6598 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6599 else
6600 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6601 }
6602
6603 return 0;
6604}
6605
e7911019 6606/* Handle -Wswitch*. Called from the front end after parsing the
6607 switch construct. */
6608/* ??? Should probably be somewhere generic, since other languages
6609 besides C and C++ would want this. At the moment, however, C/C++
6610 are the only tree-ssa languages that support enumerations at all,
6611 so the point is moot. */
4ee9c684 6612
e7911019 6613void
6614c_do_switch_warnings (splay_tree cases, location_t switch_location,
6615 tree type, tree cond)
4ee9c684 6616{
b27ac6b5 6617 splay_tree_node default_node;
359d87c6 6618 splay_tree_node node;
6619 tree chain;
4ee9c684 6620
6621 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6622 return;
6623
4ee9c684 6624 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6625 if (!default_node)
5fb6a912 6626 warning_at (switch_location, OPT_Wswitch_default,
6627 "switch missing default case");
4ee9c684 6628
359d87c6 6629 /* From here on, we only care about about enumerated types. */
6630 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6631 return;
6632
561017b5 6633 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6634 if (!warn_switch_enum && !warn_switch)
359d87c6 6635 return;
6636
561017b5 6637 /* Check the cases. Warn about case values which are not members of
6638 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6639 there is no default case, check that exactly all enumeration
6640 literals are covered by the cases. */
6641
359d87c6 6642 /* Clearing COND if it is not an integer constant simplifies
6643 the tests inside the loop below. */
6644 if (TREE_CODE (cond) != INTEGER_CST)
6645 cond = NULL_TREE;
6646
6647 /* The time complexity here is O(N*lg(N)) worst case, but for the
6648 common case of monotonically increasing enumerators, it is
6649 O(N), since the nature of the splay tree will keep the next
6650 element adjacent to the root at all times. */
4ee9c684 6651
359d87c6 6652 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6653 {
6654 tree value = TREE_VALUE (chain);
3f00a6c0 6655 if (TREE_CODE (value) == CONST_DECL)
6656 value = DECL_INITIAL (value);
359d87c6 6657 node = splay_tree_lookup (cases, (splay_tree_key) value);
6658 if (node)
4ee9c684 6659 {
359d87c6 6660 /* Mark the CASE_LOW part of the case entry as seen. */
6661 tree label = (tree) node->value;
6662 CASE_LOW_SEEN (label) = 1;
6663 continue;
6664 }
6665
6666 /* Even though there wasn't an exact match, there might be a
f0b5f617 6667 case range which includes the enumerator's value. */
359d87c6 6668 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6669 if (node && CASE_HIGH ((tree) node->value))
6670 {
6671 tree label = (tree) node->value;
6672 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6673 if (cmp >= 0)
4ee9c684 6674 {
359d87c6 6675 /* If we match the upper bound exactly, mark the CASE_HIGH
6676 part of the case entry as seen. */
6677 if (cmp == 0)
6678 CASE_HIGH_SEEN (label) = 1;
6679 continue;
4ee9c684 6680 }
6681 }
6682
359d87c6 6683 /* We've now determined that this enumerated literal isn't
6684 handled by the case labels of the switch statement. */
4ee9c684 6685
359d87c6 6686 /* If the switch expression is a constant, we only really care
6687 about whether that constant is handled by the switch. */
6688 if (cond && tree_int_cst_compare (cond, value))
6689 continue;
4ee9c684 6690
6cbbbc89 6691 /* If there is a default_node, the only relevant option is
561017b5 6692 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6693 to warn using -Wswitch because -Wswitch is enabled by -Wall
6694 while -Wswitch-enum is explicit. */
561017b5 6695 warning_at (switch_location,
6696 (default_node || !warn_switch
6697 ? OPT_Wswitch_enum
6698 : OPT_Wswitch),
6699 "enumeration value %qE not handled in switch",
6700 TREE_PURPOSE (chain));
4ee9c684 6701 }
359d87c6 6702
6703 /* Warn if there are case expressions that don't correspond to
6704 enumerators. This can occur since C and C++ don't enforce
6705 type-checking of assignments to enumeration variables.
6706
6707 The time complexity here is now always O(N) worst case, since
6708 we should have marked both the lower bound and upper bound of
6709 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6710 above. This scan also resets those fields. */
6cbbbc89 6711
359d87c6 6712 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6713}
6714
9dd48740 6715/* Finish an expression taking the address of LABEL (an
dda49785 6716 IDENTIFIER_NODE). Returns an expression for the address.
6717
6718 LOC is the location for the expression returned. */
d0a47c8d 6719
1cae46be 6720tree
dda49785 6721finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6722{
6723 tree result;
6724
29438999 6725 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6726
9dd48740 6727 if (label == error_mark_node)
6728 return error_mark_node;
6729
d0a47c8d 6730 label = lookup_label (label);
6731 if (label == NULL_TREE)
6732 result = null_pointer_node;
6733 else
6734 {
6735 TREE_USED (label) = 1;
6736 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6737 /* The current function is not necessarily uninlinable.
d0a47c8d 6738 Computed gotos are incompatible with inlining, but the value
6739 here could be used only in a diagnostic, for example. */
dda49785 6740 protected_set_expr_location (result, loc);
d0a47c8d 6741 }
6742
6743 return result;
6744}
4f9a1c9b 6745\f
6746
6747/* Given a boolean expression ARG, return a tree representing an increment
6748 or decrement (as indicated by CODE) of ARG. The front end must check for
6749 invalid cases (e.g., decrement in C++). */
6750tree
1cae46be 6751boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6752{
6753 tree val;
69db191c 6754 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6755
4f9a1c9b 6756 arg = stabilize_reference (arg);
6757 switch (code)
6758 {
6759 case PREINCREMENT_EXPR:
14ae0310 6760 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6761 break;
6762 case POSTINCREMENT_EXPR:
14ae0310 6763 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6764 arg = save_expr (arg);
14ae0310 6765 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6766 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6767 break;
6768 case PREDECREMENT_EXPR:
14ae0310 6769 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6770 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6771 break;
6772 case POSTDECREMENT_EXPR:
14ae0310 6773 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6774 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6775 arg = save_expr (arg);
14ae0310 6776 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6777 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6778 break;
6779 default:
231bd014 6780 gcc_unreachable ();
4f9a1c9b 6781 }
6782 TREE_SIDE_EFFECTS (val) = 1;
6783 return val;
6784}
76a6e674 6785\f
f3449a3c 6786/* Built-in macros for stddef.h and stdint.h, that require macros
6787 defined in this file. */
79cf3ec1 6788void
1cae46be 6789c_stddef_cpp_builtins(void)
1ed9d5f5 6790{
63994318 6791 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6792 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6793 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6794 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6795 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6796 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6797 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6798 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6799 if (SIG_ATOMIC_TYPE)
6800 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6801 if (INT8_TYPE)
6802 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6803 if (INT16_TYPE)
6804 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6805 if (INT32_TYPE)
6806 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6807 if (INT64_TYPE)
6808 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6809 if (UINT8_TYPE)
6810 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6811 if (UINT16_TYPE)
6812 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6813 if (UINT32_TYPE)
6814 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6815 if (UINT64_TYPE)
6816 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6817 if (INT_LEAST8_TYPE)
6818 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6819 if (INT_LEAST16_TYPE)
6820 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6821 if (INT_LEAST32_TYPE)
6822 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6823 if (INT_LEAST64_TYPE)
6824 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6825 if (UINT_LEAST8_TYPE)
6826 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6827 if (UINT_LEAST16_TYPE)
6828 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6829 if (UINT_LEAST32_TYPE)
6830 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6831 if (UINT_LEAST64_TYPE)
6832 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6833 if (INT_FAST8_TYPE)
6834 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6835 if (INT_FAST16_TYPE)
6836 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6837 if (INT_FAST32_TYPE)
6838 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6839 if (INT_FAST64_TYPE)
6840 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6841 if (UINT_FAST8_TYPE)
6842 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6843 if (UINT_FAST16_TYPE)
6844 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6845 if (UINT_FAST32_TYPE)
6846 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6847 if (UINT_FAST64_TYPE)
6848 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6849 if (INTPTR_TYPE)
6850 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6851 if (UINTPTR_TYPE)
6852 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6853}
6854
7d3b509a 6855static void
1cae46be 6856c_init_attributes (void)
7d3b509a 6857{
6858 /* Fill in the built_in_attributes array. */
7c446c95 6859#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6860 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6861#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6862 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6863#define DEF_ATTR_STRING(ENUM, VALUE) \
6864 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6865#define DEF_ATTR_IDENT(ENUM, STRING) \
6866 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6867#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6868 built_in_attributes[(int) ENUM] \
6869 = tree_cons (built_in_attributes[(int) PURPOSE], \
6870 built_in_attributes[(int) VALUE], \
6871 built_in_attributes[(int) CHAIN]);
7d3b509a 6872#include "builtin-attrs.def"
6873#undef DEF_ATTR_NULL_TREE
6874#undef DEF_ATTR_INT
6875#undef DEF_ATTR_IDENT
6876#undef DEF_ATTR_TREE_LIST
76a6e674 6877}
5f3cead1 6878
33199a81 6879/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6880 identifier as an argument, so the front end shouldn't look it up. */
6881
6882bool
47b19b94 6883attribute_takes_identifier_p (const_tree attr_id)
33199a81 6884{
9bf1c74e 6885 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6886 if (spec == NULL)
6887 /* Unknown attribute that we'll end up ignoring, return true so we
6888 don't complain about an identifier argument. */
6889 return true;
6890 else if (!strcmp ("mode", spec->name)
6891 || !strcmp ("format", spec->name)
6892 || !strcmp ("cleanup", spec->name))
47b19b94 6893 return true;
6894 else
6895 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6896}
6897
f8e93a2e 6898/* Attribute handlers common to C front ends. */
6899
6900/* Handle a "packed" attribute; arguments as in
6901 struct attribute_spec.handler. */
6902
6903static tree
9a03a746 6904handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6905 int flags, bool *no_add_attrs)
f8e93a2e 6906{
f40175cb 6907 if (TYPE_P (*node))
f8e93a2e 6908 {
6909 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6910 *node = build_variant_type_copy (*node);
f40175cb 6911 TYPE_PACKED (*node) = 1;
f8e93a2e 6912 }
6913 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6914 {
9fd767c5 6915 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6916 /* Still pack bitfields. */
6917 && ! DECL_INITIAL (*node))
c2ab04f9 6918 warning (OPT_Wattributes,
6919 "%qE attribute ignored for field of type %qT",
6920 name, TREE_TYPE (*node));
6921 else
6922 DECL_PACKED (*node) = 1;
6923 }
f8e93a2e 6924 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6925 used for DECL_REGISTER. It wouldn't mean anything anyway.
6926 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6927 that changes what the typedef is typing. */
f8e93a2e 6928 else
6929 {
9b2d6d13 6930 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6931 *no_add_attrs = true;
6932 }
6933
6934 return NULL_TREE;
6935}
6936
6937/* Handle a "nocommon" attribute; arguments as in
6938 struct attribute_spec.handler. */
6939
6940static tree
1cae46be 6941handle_nocommon_attribute (tree *node, tree name,
9a03a746 6942 tree ARG_UNUSED (args),
6943 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6944{
6945 if (TREE_CODE (*node) == VAR_DECL)
6946 DECL_COMMON (*node) = 0;
6947 else
6948 {
9b2d6d13 6949 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6950 *no_add_attrs = true;
6951 }
6952
6953 return NULL_TREE;
6954}
6955
6956/* Handle a "common" attribute; arguments as in
6957 struct attribute_spec.handler. */
6958
6959static tree
9a03a746 6960handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6961 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6962{
6963 if (TREE_CODE (*node) == VAR_DECL)
6964 DECL_COMMON (*node) = 1;
6965 else
6966 {
9b2d6d13 6967 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6968 *no_add_attrs = true;
6969 }
6970
6971 return NULL_TREE;
6972}
6973
6974/* Handle a "noreturn" attribute; arguments as in
6975 struct attribute_spec.handler. */
6976
6977static tree
9a03a746 6978handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6979 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6980{
6981 tree type = TREE_TYPE (*node);
6982
6983 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6984 if (TREE_CODE (*node) == FUNCTION_DECL
6985 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6986 TREE_THIS_VOLATILE (*node) = 1;
6987 else if (TREE_CODE (type) == POINTER_TYPE
6988 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6989 TREE_TYPE (*node)
9d4eeb52 6990 = (build_qualified_type
6991 (build_pointer_type
6992 (build_type_variant (TREE_TYPE (type),
6993 TYPE_READONLY (TREE_TYPE (type)), 1)),
6994 TYPE_QUALS (type)));
f8e93a2e 6995 else
6996 {
9b2d6d13 6997 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6998 *no_add_attrs = true;
6999 }
7000
7001 return NULL_TREE;
7002}
7003
5de92639 7004/* Handle a "hot" and attribute; arguments as in
7005 struct attribute_spec.handler. */
7006
7007static tree
7008handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7009 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7010{
758a38ab 7011 if (TREE_CODE (*node) == FUNCTION_DECL
7012 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7013 {
7014 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7015 {
4a026b48 7016 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7017 "with attribute %qs", name, "cold");
5de92639 7018 *no_add_attrs = true;
7019 }
24470055 7020 /* Most of the rest of the hot processing is done later with
7021 lookup_attribute. */
5de92639 7022 }
7023 else
7024 {
7025 warning (OPT_Wattributes, "%qE attribute ignored", name);
7026 *no_add_attrs = true;
7027 }
7028
7029 return NULL_TREE;
7030}
758a38ab 7031
5de92639 7032/* Handle a "cold" and attribute; arguments as in
7033 struct attribute_spec.handler. */
7034
7035static tree
7036handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7037 int ARG_UNUSED (flags), bool *no_add_attrs)
7038{
758a38ab 7039 if (TREE_CODE (*node) == FUNCTION_DECL
7040 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7041 {
7042 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7043 {
4a026b48 7044 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7045 "with attribute %qs", name, "hot");
5de92639 7046 *no_add_attrs = true;
7047 }
24470055 7048 /* Most of the rest of the cold processing is done later with
7049 lookup_attribute. */
5de92639 7050 }
7051 else
7052 {
7053 warning (OPT_Wattributes, "%qE attribute ignored", name);
7054 *no_add_attrs = true;
7055 }
7056
7057 return NULL_TREE;
7058}
7059
a9196da9 7060/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7061 struct attribute_spec.handler. */
7062
7063static tree
a9196da9 7064handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7065 bool *no_add_attrs)
d413ffdd 7066{
7067 if (TREE_CODE (*node) != FUNCTION_DECL)
7068 {
7069 warning (OPT_Wattributes, "%qE attribute ignored", name);
7070 *no_add_attrs = true;
7071 }
7072
7073 return NULL_TREE;
7074}
7075
a9196da9 7076/* Handle a "no_address_safety_analysis" attribute; arguments as in
7077 struct attribute_spec.handler. */
7078
7079static tree
7080handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7081 bool *no_add_attrs)
7082{
7083 if (TREE_CODE (*node) != FUNCTION_DECL)
7084 warning (OPT_Wattributes, "%qE attribute ignored", name);
7085 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7086 DECL_ATTRIBUTES (*node)
7087 = tree_cons (get_identifier ("no_sanitize_address"),
7088 NULL_TREE, DECL_ATTRIBUTES (*node));
7089 *no_add_attrs = true;
7090 return NULL_TREE;
7091}
7092
05f893e1 7093/* Handle a "no_sanitize_undefined" attribute; arguments as in
7094 struct attribute_spec.handler. */
7095
7096static tree
7097handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7098 bool *no_add_attrs)
7099{
7100 if (TREE_CODE (*node) != FUNCTION_DECL)
7101 {
7102 warning (OPT_Wattributes, "%qE attribute ignored", name);
7103 *no_add_attrs = true;
7104 }
7105
7106 return NULL_TREE;
7107}
7108
947aa916 7109/* Handle a "stack_protect" attribute; arguments as in
7110 struct attribute_spec.handler. */
7111static tree
7112handle_stack_protect_attribute (tree *node, tree name, tree, int,
7113 bool *no_add_attrs)
7114{
7115 if (TREE_CODE (*node) != FUNCTION_DECL)
7116 {
7117 warning (OPT_Wattributes, "%qE attribute ignored", name);
7118 *no_add_attrs = true;
7119 }
7120 else
7121 DECL_ATTRIBUTES (*node)
7122 = tree_cons (get_identifier ("stack_protect"),
7123 NULL_TREE, DECL_ATTRIBUTES (*node));
7124
7125 return NULL_TREE;
7126}
7127
f8e93a2e 7128/* Handle a "noinline" attribute; arguments as in
7129 struct attribute_spec.handler. */
7130
7131static tree
1cae46be 7132handle_noinline_attribute (tree *node, tree name,
9a03a746 7133 tree ARG_UNUSED (args),
7134 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7135{
7136 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7137 {
7138 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7139 {
7140 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7141 "with attribute %qs", name, "always_inline");
7142 *no_add_attrs = true;
7143 }
7144 else
7145 DECL_UNINLINABLE (*node) = 1;
7146 }
f8e93a2e 7147 else
7148 {
9b2d6d13 7149 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7150 *no_add_attrs = true;
7151 }
7152
7153 return NULL_TREE;
7154}
7155
bdb1f0d1 7156/* Handle a "noclone" attribute; arguments as in
7157 struct attribute_spec.handler. */
7158
7159static tree
7160handle_noclone_attribute (tree *node, tree name,
7161 tree ARG_UNUSED (args),
7162 int ARG_UNUSED (flags), bool *no_add_attrs)
7163{
7164 if (TREE_CODE (*node) != FUNCTION_DECL)
7165 {
7166 warning (OPT_Wattributes, "%qE attribute ignored", name);
7167 *no_add_attrs = true;
7168 }
7169
7170 return NULL_TREE;
7171}
7172
85fbea97 7173/* Handle a "no_icf" attribute; arguments as in
7174 struct attribute_spec.handler. */
7175
7176static tree
7177handle_noicf_attribute (tree *node, tree name,
7178 tree ARG_UNUSED (args),
7179 int ARG_UNUSED (flags), bool *no_add_attrs)
7180{
7181 if (TREE_CODE (*node) != FUNCTION_DECL)
7182 {
7183 warning (OPT_Wattributes, "%qE attribute ignored", name);
7184 *no_add_attrs = true;
7185 }
7186
7187 return NULL_TREE;
7188}
7189
7190
f8e93a2e 7191/* Handle a "always_inline" attribute; arguments as in
7192 struct attribute_spec.handler. */
7193
7194static tree
1cae46be 7195handle_always_inline_attribute (tree *node, tree name,
9a03a746 7196 tree ARG_UNUSED (args),
7197 int ARG_UNUSED (flags),
09347743 7198 bool *no_add_attrs)
f8e93a2e 7199{
7200 if (TREE_CODE (*node) == FUNCTION_DECL)
7201 {
4a026b48 7202 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7203 {
7204 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7205 "with %qs attribute", name, "noinline");
7206 *no_add_attrs = true;
7207 }
7208 else
7209 /* Set the attribute and mark it for disregarding inline
7210 limits. */
7211 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7212 }
7213 else
7214 {
9b2d6d13 7215 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7216 *no_add_attrs = true;
541e4101 7217 }
7218
7219 return NULL_TREE;
7220}
7221
7222/* Handle a "gnu_inline" attribute; arguments as in
7223 struct attribute_spec.handler. */
7224
7225static tree
7226handle_gnu_inline_attribute (tree *node, tree name,
7227 tree ARG_UNUSED (args),
7228 int ARG_UNUSED (flags),
7229 bool *no_add_attrs)
7230{
7231 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7232 {
7233 /* Do nothing else, just set the attribute. We'll get at
7234 it later with lookup_attribute. */
7235 }
7236 else
7237 {
7238 warning (OPT_Wattributes, "%qE attribute ignored", name);
7239 *no_add_attrs = true;
7bd95dfd 7240 }
7241
7242 return NULL_TREE;
7243}
7244
7245/* Handle a "leaf" attribute; arguments as in
7246 struct attribute_spec.handler. */
7247
7248static tree
7249handle_leaf_attribute (tree *node, tree name,
7250 tree ARG_UNUSED (args),
7251 int ARG_UNUSED (flags), bool *no_add_attrs)
7252{
7253 if (TREE_CODE (*node) != FUNCTION_DECL)
7254 {
7255 warning (OPT_Wattributes, "%qE attribute ignored", name);
7256 *no_add_attrs = true;
7257 }
7258 if (!TREE_PUBLIC (*node))
7259 {
7260 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7261 *no_add_attrs = true;
f8e93a2e 7262 }
7263
7264 return NULL_TREE;
7265}
7266
1b16fc45 7267/* Handle an "artificial" attribute; arguments as in
7268 struct attribute_spec.handler. */
7269
7270static tree
7271handle_artificial_attribute (tree *node, tree name,
7272 tree ARG_UNUSED (args),
7273 int ARG_UNUSED (flags),
7274 bool *no_add_attrs)
7275{
7276 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7277 {
7278 /* Do nothing else, just set the attribute. We'll get at
7279 it later with lookup_attribute. */
7280 }
7281 else
7282 {
7283 warning (OPT_Wattributes, "%qE attribute ignored", name);
7284 *no_add_attrs = true;
7285 }
7286
7287 return NULL_TREE;
7288}
7289
0cdd9887 7290/* Handle a "flatten" attribute; arguments as in
7291 struct attribute_spec.handler. */
7292
7293static tree
7294handle_flatten_attribute (tree *node, tree name,
a0c938f0 7295 tree args ATTRIBUTE_UNUSED,
7296 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7297{
7298 if (TREE_CODE (*node) == FUNCTION_DECL)
7299 /* Do nothing else, just set the attribute. We'll get at
7300 it later with lookup_attribute. */
7301 ;
7302 else
7303 {
7304 warning (OPT_Wattributes, "%qE attribute ignored", name);
7305 *no_add_attrs = true;
7306 }
7307
7308 return NULL_TREE;
7309}
7310
10fc867f 7311/* Handle a "warning" or "error" attribute; arguments as in
7312 struct attribute_spec.handler. */
7313
7314static tree
7315handle_error_attribute (tree *node, tree name, tree args,
7316 int ARG_UNUSED (flags), bool *no_add_attrs)
7317{
7318 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7319 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7320 /* Do nothing else, just set the attribute. We'll get at
7321 it later with lookup_attribute. */
7322 ;
7323 else
7324 {
7325 warning (OPT_Wattributes, "%qE attribute ignored", name);
7326 *no_add_attrs = true;
7327 }
7328
7329 return NULL_TREE;
7330}
0cdd9887 7331
f8e93a2e 7332/* Handle a "used" attribute; arguments as in
7333 struct attribute_spec.handler. */
7334
7335static tree
9a03a746 7336handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7337 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7338{
d0a31bd8 7339 tree node = *pnode;
7340
7341 if (TREE_CODE (node) == FUNCTION_DECL
a4e3ffad 7342 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7343 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7344 {
f54ed8bc 7345 TREE_USED (node) = 1;
9423c9b7 7346 DECL_PRESERVE_P (node) = 1;
abc6c64f 7347 if (TREE_CODE (node) == VAR_DECL)
7348 DECL_READ_P (node) = 1;
f54ed8bc 7349 }
f8e93a2e 7350 else
7351 {
9b2d6d13 7352 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7353 *no_add_attrs = true;
7354 }
7355
7356 return NULL_TREE;
7357}
7358
7359/* Handle a "unused" attribute; arguments as in
7360 struct attribute_spec.handler. */
7361
7362static tree
9a03a746 7363handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7364 int flags, bool *no_add_attrs)
f8e93a2e 7365{
7366 if (DECL_P (*node))
7367 {
7368 tree decl = *node;
7369
7370 if (TREE_CODE (decl) == PARM_DECL
7371 || TREE_CODE (decl) == VAR_DECL
7372 || TREE_CODE (decl) == FUNCTION_DECL
7373 || TREE_CODE (decl) == LABEL_DECL
7374 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7375 {
7376 TREE_USED (decl) = 1;
7377 if (TREE_CODE (decl) == VAR_DECL
7378 || TREE_CODE (decl) == PARM_DECL)
7379 DECL_READ_P (decl) = 1;
7380 }
f8e93a2e 7381 else
7382 {
9b2d6d13 7383 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7384 *no_add_attrs = true;
7385 }
7386 }
7387 else
7388 {
7389 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7390 *node = build_variant_type_copy (*node);
f8e93a2e 7391 TREE_USED (*node) = 1;
7392 }
7393
7394 return NULL_TREE;
7395}
7396
62eec3b4 7397/* Handle a "externally_visible" attribute; arguments as in
7398 struct attribute_spec.handler. */
7399
7400static tree
7401handle_externally_visible_attribute (tree *pnode, tree name,
7402 tree ARG_UNUSED (args),
7403 int ARG_UNUSED (flags),
7404 bool *no_add_attrs)
7405{
7406 tree node = *pnode;
7407
b443c459 7408 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7409 {
ba12ea31 7410 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7411 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7412 {
7413 warning (OPT_Wattributes,
7414 "%qE attribute have effect only on public objects", name);
7415 *no_add_attrs = true;
7416 }
62eec3b4 7417 }
62eec3b4 7418 else
7419 {
7420 warning (OPT_Wattributes, "%qE attribute ignored", name);
7421 *no_add_attrs = true;
7422 }
7423
7424 return NULL_TREE;
7425}
7426
6b722052 7427/* Handle the "no_reorder" attribute. Arguments as in
7428 struct attribute_spec.handler. */
7429
7430static tree
7431handle_no_reorder_attribute (tree *pnode,
7432 tree name,
7433 tree,
7434 int,
7435 bool *no_add_attrs)
7436{
7437 tree node = *pnode;
7438
b443c459 7439 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7440 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7441 {
7442 warning (OPT_Wattributes,
7443 "%qE attribute only affects top level objects",
7444 name);
7445 *no_add_attrs = true;
7446 }
7447
7448 return NULL_TREE;
7449}
7450
f8e93a2e 7451/* Handle a "const" attribute; arguments as in
7452 struct attribute_spec.handler. */
7453
7454static tree
9a03a746 7455handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7456 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7457{
7458 tree type = TREE_TYPE (*node);
7459
7460 /* See FIXME comment on noreturn in c_common_attribute_table. */
7461 if (TREE_CODE (*node) == FUNCTION_DECL)
7462 TREE_READONLY (*node) = 1;
7463 else if (TREE_CODE (type) == POINTER_TYPE
7464 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7465 TREE_TYPE (*node)
9d4eeb52 7466 = (build_qualified_type
7467 (build_pointer_type
7468 (build_type_variant (TREE_TYPE (type), 1,
7469 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7470 TYPE_QUALS (type)));
f8e93a2e 7471 else
7472 {
9b2d6d13 7473 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7474 *no_add_attrs = true;
7475 }
7476
7477 return NULL_TREE;
7478}
7479
7480/* Handle a "transparent_union" attribute; arguments as in
7481 struct attribute_spec.handler. */
7482
7483static tree
1cae46be 7484handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7485 tree ARG_UNUSED (args), int flags,
09347743 7486 bool *no_add_attrs)
f8e93a2e 7487{
881eb642 7488 tree type;
03908818 7489
7490 *no_add_attrs = true;
f8e93a2e 7491
ffcdbf9c 7492
7493 if (TREE_CODE (*node) == TYPE_DECL
7494 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7495 node = &TREE_TYPE (*node);
7496 type = *node;
f8e93a2e 7497
03908818 7498 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7499 {
fca86134 7500 /* Make sure that the first field will work for a transparent union.
7501 If the type isn't complete yet, leave the check to the code in
7502 finish_struct. */
7503 if (TYPE_SIZE (type))
7504 {
7505 tree first = first_field (type);
7506 if (first == NULL_TREE
7507 || DECL_ARTIFICIAL (first)
7508 || TYPE_MODE (type) != DECL_MODE (first))
7509 goto ignored;
7510 }
7511
f8e93a2e 7512 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7513 {
fca86134 7514 /* If the type isn't complete yet, setting the flag
7515 on a variant wouldn't ever be checked. */
7516 if (!TYPE_SIZE (type))
7517 goto ignored;
7518
7519 /* build_duplicate_type doesn't work for C++. */
7520 if (c_dialect_cxx ())
03908818 7521 goto ignored;
7522
7523 /* A type variant isn't good enough, since we don't a cast
7524 to such a type removed as a no-op. */
7525 *node = type = build_duplicate_type (type);
7526 }
7527
8df5a43d 7528 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7529 return NULL_TREE;
f8e93a2e 7530 }
7531
03908818 7532 ignored:
7533 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7534 return NULL_TREE;
7535}
7536
9af7fd5b 7537/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7538 get the requested priority for a constructor or destructor,
7539 possibly issuing diagnostics for invalid or reserved
7540 priorities. */
7541
7542static priority_type
7543get_priority (tree args, bool is_destructor)
7544{
7545 HOST_WIDE_INT pri;
6c181a06 7546 tree arg;
9af7fd5b 7547
7548 if (!args)
7549 return DEFAULT_INIT_PRIORITY;
48e1416a 7550
28fbc04f 7551 if (!SUPPORTS_INIT_PRIORITY)
7552 {
7553 if (is_destructor)
7554 error ("destructor priorities are not supported");
7555 else
7556 error ("constructor priorities are not supported");
7557 return DEFAULT_INIT_PRIORITY;
7558 }
7559
6c181a06 7560 arg = TREE_VALUE (args);
253e1cae 7561 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7562 goto invalid;
7563 if (arg == error_mark_node)
7564 return DEFAULT_INIT_PRIORITY;
c28ddc97 7565 arg = default_conversion (arg);
e913b5cd 7566 if (!tree_fits_shwi_p (arg)
6c181a06 7567 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7568 goto invalid;
7569
e913b5cd 7570 pri = tree_to_shwi (arg);
9af7fd5b 7571 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7572 goto invalid;
7573
7574 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7575 {
7576 if (is_destructor)
7577 warning (0,
7578 "destructor priorities from 0 to %d are reserved "
48e1416a 7579 "for the implementation",
9af7fd5b 7580 MAX_RESERVED_INIT_PRIORITY);
7581 else
7582 warning (0,
7583 "constructor priorities from 0 to %d are reserved "
48e1416a 7584 "for the implementation",
9af7fd5b 7585 MAX_RESERVED_INIT_PRIORITY);
7586 }
7587 return pri;
7588
7589 invalid:
7590 if (is_destructor)
7591 error ("destructor priorities must be integers from 0 to %d inclusive",
7592 MAX_INIT_PRIORITY);
7593 else
7594 error ("constructor priorities must be integers from 0 to %d inclusive",
7595 MAX_INIT_PRIORITY);
7596 return DEFAULT_INIT_PRIORITY;
7597}
7598
f8e93a2e 7599/* Handle a "constructor" attribute; arguments as in
7600 struct attribute_spec.handler. */
7601
7602static tree
9af7fd5b 7603handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7604 int ARG_UNUSED (flags),
09347743 7605 bool *no_add_attrs)
f8e93a2e 7606{
7607 tree decl = *node;
7608 tree type = TREE_TYPE (decl);
7609
7610 if (TREE_CODE (decl) == FUNCTION_DECL
7611 && TREE_CODE (type) == FUNCTION_TYPE
7612 && decl_function_context (decl) == 0)
7613 {
9af7fd5b 7614 priority_type priority;
f8e93a2e 7615 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7616 priority = get_priority (args, /*is_destructor=*/false);
7617 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7618 TREE_USED (decl) = 1;
7619 }
7620 else
7621 {
9b2d6d13 7622 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7623 *no_add_attrs = true;
7624 }
7625
7626 return NULL_TREE;
7627}
7628
7629/* Handle a "destructor" attribute; arguments as in
7630 struct attribute_spec.handler. */
7631
7632static tree
9af7fd5b 7633handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7634 int ARG_UNUSED (flags),
09347743 7635 bool *no_add_attrs)
f8e93a2e 7636{
7637 tree decl = *node;
7638 tree type = TREE_TYPE (decl);
7639
7640 if (TREE_CODE (decl) == FUNCTION_DECL
7641 && TREE_CODE (type) == FUNCTION_TYPE
7642 && decl_function_context (decl) == 0)
7643 {
9af7fd5b 7644 priority_type priority;
f8e93a2e 7645 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7646 priority = get_priority (args, /*is_destructor=*/true);
7647 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7648 TREE_USED (decl) = 1;
7649 }
7650 else
7651 {
9b2d6d13 7652 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7653 *no_add_attrs = true;
7654 }
7655
7656 return NULL_TREE;
7657}
7658
1c58e3f1 7659/* Nonzero if the mode is a valid vector mode for this architecture.
7660 This returns nonzero even if there is no hardware support for the
7661 vector mode, but we can emulate with narrower modes. */
7662
7663static int
3754d046 7664vector_mode_valid_p (machine_mode mode)
1c58e3f1 7665{
7666 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7667 machine_mode innermode;
1c58e3f1 7668
7669 /* Doh! What's going on? */
7670 if (mclass != MODE_VECTOR_INT
7671 && mclass != MODE_VECTOR_FLOAT
7672 && mclass != MODE_VECTOR_FRACT
7673 && mclass != MODE_VECTOR_UFRACT
7674 && mclass != MODE_VECTOR_ACCUM
7675 && mclass != MODE_VECTOR_UACCUM)
7676 return 0;
7677
7678 /* Hardware support. Woo hoo! */
7679 if (targetm.vector_mode_supported_p (mode))
7680 return 1;
7681
7682 innermode = GET_MODE_INNER (mode);
7683
7684 /* We should probably return 1 if requesting V4DI and we have no DI,
7685 but we have V2DI, but this is probably very unlikely. */
7686
7687 /* If we have support for the inner mode, we can safely emulate it.
7688 We may not have V2DI, but me can emulate with a pair of DIs. */
7689 return targetm.scalar_mode_supported_p (innermode);
7690}
7691
7692
f8e93a2e 7693/* Handle a "mode" attribute; arguments as in
7694 struct attribute_spec.handler. */
7695
7696static tree
9a03a746 7697handle_mode_attribute (tree *node, tree name, tree args,
7698 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7699{
7700 tree type = *node;
d1dd9ac0 7701 tree ident = TREE_VALUE (args);
f8e93a2e 7702
7703 *no_add_attrs = true;
ab2c1de8 7704
d1dd9ac0 7705 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7706 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7707 else
7708 {
7709 int j;
d1dd9ac0 7710 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7711 int len = strlen (p);
3754d046 7712 machine_mode mode = VOIDmode;
f8e93a2e 7713 tree typefm;
b2aef146 7714 bool valid_mode;
f8e93a2e 7715
7716 if (len > 4 && p[0] == '_' && p[1] == '_'
7717 && p[len - 1] == '_' && p[len - 2] == '_')
7718 {
4fd61bc6 7719 char *newp = (char *) alloca (len - 1);
f8e93a2e 7720
7721 strcpy (newp, &p[2]);
7722 newp[len - 4] = '\0';
7723 p = newp;
7724 }
7725
7726 /* Change this type to have a type with the specified mode.
7727 First check for the special modes. */
84166705 7728 if (!strcmp (p, "byte"))
f8e93a2e 7729 mode = byte_mode;
7730 else if (!strcmp (p, "word"))
7731 mode = word_mode;
84166705 7732 else if (!strcmp (p, "pointer"))
f8e93a2e 7733 mode = ptr_mode;
0ef89dfd 7734 else if (!strcmp (p, "libgcc_cmp_return"))
7735 mode = targetm.libgcc_cmp_return_mode ();
7736 else if (!strcmp (p, "libgcc_shift_count"))
7737 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7738 else if (!strcmp (p, "unwind_word"))
7739 mode = targetm.unwind_word_mode ();
f8e93a2e 7740 else
7741 for (j = 0; j < NUM_MACHINE_MODES; j++)
7742 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7743 {
3754d046 7744 mode = (machine_mode) j;
743a6f47 7745 break;
7746 }
f8e93a2e 7747
7748 if (mode == VOIDmode)
4917c376 7749 {
d1dd9ac0 7750 error ("unknown machine mode %qE", ident);
4917c376 7751 return NULL_TREE;
7752 }
7753
b2aef146 7754 valid_mode = false;
7755 switch (GET_MODE_CLASS (mode))
4917c376 7756 {
b2aef146 7757 case MODE_INT:
7758 case MODE_PARTIAL_INT:
7759 case MODE_FLOAT:
c4503c0a 7760 case MODE_DECIMAL_FLOAT:
9421ebb9 7761 case MODE_FRACT:
7762 case MODE_UFRACT:
7763 case MODE_ACCUM:
7764 case MODE_UACCUM:
b2aef146 7765 valid_mode = targetm.scalar_mode_supported_p (mode);
7766 break;
7767
7768 case MODE_COMPLEX_INT:
7769 case MODE_COMPLEX_FLOAT:
7770 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7771 break;
7772
7773 case MODE_VECTOR_INT:
7774 case MODE_VECTOR_FLOAT:
9421ebb9 7775 case MODE_VECTOR_FRACT:
7776 case MODE_VECTOR_UFRACT:
7777 case MODE_VECTOR_ACCUM:
7778 case MODE_VECTOR_UACCUM:
9b2d6d13 7779 warning (OPT_Wattributes, "specifying vector types with "
7780 "__attribute__ ((mode)) is deprecated");
7781 warning (OPT_Wattributes,
7782 "use __attribute__ ((vector_size)) instead");
b2aef146 7783 valid_mode = vector_mode_valid_p (mode);
7784 break;
4917c376 7785
b2aef146 7786 default:
7787 break;
7788 }
7789 if (!valid_mode)
7790 {
1e5fcbe2 7791 error ("unable to emulate %qs", p);
b2aef146 7792 return NULL_TREE;
7793 }
4917c376 7794
b2aef146 7795 if (POINTER_TYPE_P (type))
ead34f59 7796 {
6d5d708e 7797 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7798 tree (*fn)(tree, machine_mode, bool);
b2aef146 7799
6d5d708e 7800 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7801 {
1e5fcbe2 7802 error ("invalid pointer mode %qs", p);
ead34f59 7803 return NULL_TREE;
7804 }
7805
a0c938f0 7806 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7807 fn = build_pointer_type_for_mode;
805e22b2 7808 else
b2aef146 7809 fn = build_reference_type_for_mode;
7810 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7811 }
b2aef146 7812 else
9421ebb9 7813 {
7814 /* For fixed-point modes, we need to test if the signness of type
7815 and the machine mode are consistent. */
7816 if (ALL_FIXED_POINT_MODE_P (mode)
7817 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7818 {
bf776685 7819 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7820 return NULL_TREE;
7821 }
7822 /* For fixed-point modes, we need to pass saturating info. */
7823 typefm = lang_hooks.types.type_for_mode (mode,
7824 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7825 : TYPE_UNSIGNED (type));
7826 }
3a648ab9 7827
b2aef146 7828 if (typefm == NULL_TREE)
7829 {
743a6f47 7830 error ("no data type for mode %qs", p);
b2aef146 7831 return NULL_TREE;
7832 }
3a648ab9 7833 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7834 {
7835 /* For enumeral types, copy the precision from the integer
7836 type returned above. If not an INTEGER_TYPE, we can't use
7837 this mode for this type. */
7838 if (TREE_CODE (typefm) != INTEGER_TYPE)
7839 {
743a6f47 7840 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7841 return NULL_TREE;
7842 }
7843
10080eac 7844 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7845 {
7846 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7847 typefm = type;
7848 }
7849 else
7850 {
7851 /* We cannot build a type variant, as there's code that assumes
7852 that TYPE_MAIN_VARIANT has the same mode. This includes the
7853 debug generators. Instead, create a subrange type. This
7854 results in all of the enumeral values being emitted only once
7855 in the original, and the subtype gets them by reference. */
7856 if (TYPE_UNSIGNED (type))
7857 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7858 else
7859 typefm = make_signed_type (TYPE_PRECISION (typefm));
7860 TREE_TYPE (typefm) = type;
7861 }
3a648ab9 7862 }
4bf450a1 7863 else if (VECTOR_MODE_P (mode)
7864 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7865 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7866 {
7867 error ("mode %qs applied to inappropriate type", p);
7868 return NULL_TREE;
7869 }
7870
b2aef146 7871 *node = typefm;
f8e93a2e 7872 }
7873
7874 return NULL_TREE;
7875}
7876
7877/* Handle a "section" attribute; arguments as in
7878 struct attribute_spec.handler. */
7879
7880static tree
9a03a746 7881handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7882 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7883{
7884 tree decl = *node;
7885
9866562d 7886 if (!targetm_common.have_named_sections)
f8e93a2e 7887 {
9866562d 7888 error_at (DECL_SOURCE_LOCATION (*node),
7889 "section attributes are not supported for this target");
7890 goto fail;
7891 }
065efcb1 7892
9866562d 7893 user_defined_section_attribute = true;
f8e93a2e 7894
b443c459 7895 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7896 {
7897 error ("section attribute not allowed for %q+D", *node);
7898 goto fail;
f8e93a2e 7899 }
9866562d 7900
7901 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7902 {
9866562d 7903 error ("section attribute argument not a string constant");
7904 goto fail;
f8e93a2e 7905 }
7906
9866562d 7907 if (TREE_CODE (decl) == VAR_DECL
7908 && current_function_decl != NULL_TREE
7909 && !TREE_STATIC (decl))
7910 {
7911 error_at (DECL_SOURCE_LOCATION (decl),
7912 "section attribute cannot be specified for local variables");
7913 goto fail;
7914 }
7915
7916 /* The decl may have already been given a section attribute
7917 from a previous declaration. Ensure they match. */
7918 if (DECL_SECTION_NAME (decl) != NULL
7919 && strcmp (DECL_SECTION_NAME (decl),
7920 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7921 {
7922 error ("section of %q+D conflicts with previous declaration", *node);
7923 goto fail;
7924 }
7925
7926 if (TREE_CODE (decl) == VAR_DECL
7927 && !targetm.have_tls && targetm.emutls.tmpl_section
7928 && DECL_THREAD_LOCAL_P (decl))
7929 {
7930 error ("section of %q+D cannot be overridden", *node);
7931 goto fail;
7932 }
7933
7934 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7935 return NULL_TREE;
7936
7937fail:
7938 *no_add_attrs = true;
f8e93a2e 7939 return NULL_TREE;
7940}
7941
83e25171 7942/* Check whether ALIGN is a valid user-specified alignment. If so,
7943 return its base-2 log; if not, output an error and return -1. If
7944 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7945 no error. */
7946int
7947check_user_alignment (const_tree align, bool allow_zero)
7948{
7949 int i;
7950
3e5a8b00 7951 if (error_operand_p (align))
7952 return -1;
5abaa10a 7953 if (TREE_CODE (align) != INTEGER_CST
7954 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7955 {
7956 error ("requested alignment is not an integer constant");
7957 return -1;
7958 }
7959 else if (allow_zero && integer_zerop (align))
7960 return -1;
1a087624 7961 else if (tree_int_cst_sgn (align) == -1
7962 || (i = tree_log2 (align)) == -1)
83e25171 7963 {
1a087624 7964 error ("requested alignment is not a positive power of 2");
83e25171 7965 return -1;
7966 }
7967 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7968 {
7969 error ("requested alignment is too large");
7970 return -1;
7971 }
7972 return i;
7973}
7974
ffcdbf9c 7975/*
7976 If in c++-11, check if the c++-11 alignment constraint with respect
7977 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7978 c++-11 mode, does nothing.
7979
7980 [dcl.align]2/ says:
7981
7982 [* if the constant expression evaluates to a fundamental alignment,
7983 the alignment requirement of the declared entity shall be the
7984 specified fundamental alignment.
7985
7986 * if the constant expression evaluates to an extended alignment
7987 and the implementation supports that alignment in the context
7988 of the declaration, the alignment of the declared entity shall
7989 be that alignment
7990
7991 * if the constant expression evaluates to an extended alignment
7992 and the implementation does not support that alignment in the
7993 context of the declaration, the program is ill-formed]. */
7994
7995static bool
7996check_cxx_fundamental_alignment_constraints (tree node,
7997 unsigned align_log,
7998 int flags)
7999{
8000 bool alignment_too_large_p = false;
8001 unsigned requested_alignment = 1U << align_log;
8002 unsigned max_align = 0;
8003
8004 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8005 || (node == NULL_TREE || node == error_mark_node))
8006 return true;
8007
8008 if (cxx_fundamental_alignment_p (requested_alignment))
8009 return true;
8010
8011 if (DECL_P (node))
8012 {
8013 if (TREE_STATIC (node))
8014 {
8015 /* For file scope variables and static members, the target
8016 supports alignments that are at most
8017 MAX_OFILE_ALIGNMENT. */
8018 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8019 alignment_too_large_p = true;
8020 }
8021 else
8022 {
8023#ifdef BIGGEST_FIELD_ALIGNMENT
8024#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8025#else
8026#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8027#endif
8028 /* For non-static members, the target supports either
8029 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8030 if it is defined or BIGGEST_ALIGNMENT. */
8031 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8032 if (TREE_CODE (node) == FIELD_DECL
8033 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8034 alignment_too_large_p = true;
8035#undef MAX_TARGET_FIELD_ALIGNMENT
8036 /* For stack variables, the target supports at most
8037 MAX_STACK_ALIGNMENT. */
8038 else if (decl_function_context (node) != NULL
8039 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8040 alignment_too_large_p = true;
8041 }
8042 }
8043 else if (TYPE_P (node))
8044 {
8045 /* Let's be liberal for types. */
8046 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8047 alignment_too_large_p = true;
8048 }
8049
8050 if (alignment_too_large_p)
8051 pedwarn (input_location, OPT_Wattributes,
8052 "requested alignment %d is larger than %d",
8053 requested_alignment, max_align);
8054
8055 return !alignment_too_large_p;
8056}
8057
f8e93a2e 8058/* Handle a "aligned" attribute; arguments as in
8059 struct attribute_spec.handler. */
8060
8061static tree
9a03a746 8062handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8063 int flags, bool *no_add_attrs)
f8e93a2e 8064{
8065 tree decl = NULL_TREE;
8066 tree *type = NULL;
8067 int is_type = 0;
caf62483 8068 tree align_expr;
f8e93a2e 8069 int i;
8070
caf62483 8071 if (args)
8072 {
8073 align_expr = TREE_VALUE (args);
3e5a8b00 8074 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8075 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8076 align_expr = default_conversion (align_expr);
8077 }
8078 else
8079 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8080
f8e93a2e 8081 if (DECL_P (*node))
8082 {
8083 decl = *node;
8084 type = &TREE_TYPE (decl);
8085 is_type = TREE_CODE (*node) == TYPE_DECL;
8086 }
8087 else if (TYPE_P (*node))
8088 type = node, is_type = 1;
8089
ffcdbf9c 8090 if ((i = check_user_alignment (align_expr, false)) == -1
8091 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8092 *no_add_attrs = true;
f8e93a2e 8093 else if (is_type)
8094 {
2ec3af9c 8095 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8096 /* OK, modify the type in place. */;
f8e93a2e 8097 /* If we have a TYPE_DECL, then copy the type, so that we
8098 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8099 else if (decl && TREE_TYPE (decl) != error_mark_node
8100 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8101 {
8102 tree tt = TREE_TYPE (decl);
e086912e 8103 *type = build_variant_type_copy (*type);
f8e93a2e 8104 DECL_ORIGINAL_TYPE (decl) = tt;
8105 TYPE_NAME (*type) = decl;
8106 TREE_USED (*type) = TREE_USED (decl);
8107 TREE_TYPE (decl) = *type;
8108 }
2ec3af9c 8109 else
e086912e 8110 *type = build_variant_type_copy (*type);
f8e93a2e 8111
7cfdc2f0 8112 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8113 TYPE_USER_ALIGN (*type) = 1;
8114 }
097b5c8b 8115 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8116 && TREE_CODE (decl) != FIELD_DECL)
8117 {
3cf8b391 8118 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8119 *no_add_attrs = true;
8120 }
ffcdbf9c 8121 else if (DECL_USER_ALIGN (decl)
8122 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8123 /* C++-11 [dcl.align/4]:
8124
8125 When multiple alignment-specifiers are specified for an
8126 entity, the alignment requirement shall be set to the
8127 strictest specified alignment.
8128
8129 This formally comes from the c++11 specification but we are
8130 doing it for the GNU attribute syntax as well. */
8131 *no_add_attrs = true;
097b5c8b 8132 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8133 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8134 {
8135 if (DECL_USER_ALIGN (decl))
8136 error ("alignment for %q+D was previously specified as %d "
8137 "and may not be decreased", decl,
8138 DECL_ALIGN (decl) / BITS_PER_UNIT);
8139 else
8140 error ("alignment for %q+D must be at least %d", decl,
8141 DECL_ALIGN (decl) / BITS_PER_UNIT);
8142 *no_add_attrs = true;
8143 }
f8e93a2e 8144 else
8145 {
7cfdc2f0 8146 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8147 DECL_USER_ALIGN (decl) = 1;
8148 }
8149
8150 return NULL_TREE;
8151}
8152
8153/* Handle a "weak" attribute; arguments as in
8154 struct attribute_spec.handler. */
8155
8156static tree
f948b309 8157handle_weak_attribute (tree *node, tree name,
9a03a746 8158 tree ARG_UNUSED (args),
8159 int ARG_UNUSED (flags),
8160 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8161{
f948b309 8162 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8163 && DECL_DECLARED_INLINE_P (*node))
8164 {
0725e25c 8165 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8166 *no_add_attrs = true;
8167 }
85c0a25c 8168 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8169 {
8170 error ("indirect function %q+D cannot be declared weak", *node);
8171 *no_add_attrs = true;
8172 return NULL_TREE;
8173 }
b443c459 8174 else if (VAR_OR_FUNCTION_DECL_P (*node))
c11b875d 8175 {
8176 struct symtab_node *n = symtab_node::get (*node);
8177 if (n && n->refuse_visibility_changes)
8178 error ("%+D declared weak after being used", *node);
8179 declare_weak (*node);
8180 }
f948b309 8181 else
8182 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8183
8184 return NULL_TREE;
8185}
8186
85c0a25c 8187/* Handle an "alias" or "ifunc" attribute; arguments as in
8188 struct attribute_spec.handler, except that IS_ALIAS tells us
8189 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8190
8191static tree
85c0a25c 8192handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8193 bool *no_add_attrs)
f8e93a2e 8194{
8195 tree decl = *node;
8196
85c0a25c 8197 if (TREE_CODE (decl) != FUNCTION_DECL
8198 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
9e830260 8199 {
8200 warning (OPT_Wattributes, "%qE attribute ignored", name);
8201 *no_add_attrs = true;
8202 }
8203 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8204 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8205 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8206 /* A static variable declaration is always a tentative definition,
8207 but the alias is a non-tentative definition which overrides. */
48e1416a 8208 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8209 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8210 {
85c0a25c 8211 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8212 *no_add_attrs = true;
85c0a25c 8213 return NULL_TREE;
f8e93a2e 8214 }
85c0a25c 8215 else if (!is_alias
8216 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8217 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8218 {
8219 error ("weak %q+D cannot be defined %qE", decl, name);
8220 *no_add_attrs = true;
8221 return NULL_TREE;
8222 }
8c42f0d9 8223
8224 /* Note that the very first time we process a nested declaration,
8225 decl_function_context will not be set. Indeed, *would* never
8226 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8227 we do below. After such frobbery, pushdecl would set the context.
8228 In any case, this is never what we want. */
8229 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8230 {
8231 tree id;
8232
8233 id = TREE_VALUE (args);
8234 if (TREE_CODE (id) != STRING_CST)
8235 {
85c0a25c 8236 error ("attribute %qE argument not a string", name);
f8e93a2e 8237 *no_add_attrs = true;
8238 return NULL_TREE;
8239 }
8240 id = get_identifier (TREE_STRING_POINTER (id));
8241 /* This counts as a use of the object pointed to. */
8242 TREE_USED (id) = 1;
8243
8244 if (TREE_CODE (decl) == FUNCTION_DECL)
8245 DECL_INITIAL (decl) = error_mark_node;
8246 else
f2526cce 8247 TREE_STATIC (decl) = 1;
85c0a25c 8248
8249 if (!is_alias)
8250 /* ifuncs are also aliases, so set that attribute too. */
8251 DECL_ATTRIBUTES (decl)
8252 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8253 }
8254 else
8255 {
9b2d6d13 8256 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8257 *no_add_attrs = true;
8258 }
8259
8e857c41 8260 if (decl_in_symtab_p (*node))
8261 {
8262 struct symtab_node *n = symtab_node::get (decl);
8263 if (n && n->refuse_visibility_changes)
8264 {
8265 if (is_alias)
8266 error ("%+D declared alias after being used", decl);
8267 else
8268 error ("%+D declared ifunc after being used", decl);
8269 }
8270 }
8271
8272
f8e93a2e 8273 return NULL_TREE;
8274}
8275
85c0a25c 8276/* Handle an "alias" or "ifunc" attribute; arguments as in
8277 struct attribute_spec.handler. */
8278
8279static tree
8280handle_ifunc_attribute (tree *node, tree name, tree args,
8281 int ARG_UNUSED (flags), bool *no_add_attrs)
8282{
8283 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8284}
8285
8286/* Handle an "alias" or "ifunc" attribute; arguments as in
8287 struct attribute_spec.handler. */
8288
8289static tree
8290handle_alias_attribute (tree *node, tree name, tree args,
8291 int ARG_UNUSED (flags), bool *no_add_attrs)
8292{
8293 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8294}
8295
f4a30bd7 8296/* Handle a "weakref" attribute; arguments as in struct
8297 attribute_spec.handler. */
8298
8299static tree
8300handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8301 int flags, bool *no_add_attrs)
8302{
8303 tree attr = NULL_TREE;
8304
83852912 8305 /* We must ignore the attribute when it is associated with
8306 local-scoped decls, since attribute alias is ignored and many
8307 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8308 if (decl_function_context (*node)
8309 || current_function_decl
b443c459 8310 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8311 {
8312 warning (OPT_Wattributes, "%qE attribute ignored", name);
8313 *no_add_attrs = true;
8314 return NULL_TREE;
8315 }
8316
85c0a25c 8317 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8318 {
8319 error ("indirect function %q+D cannot be declared weakref", *node);
8320 *no_add_attrs = true;
8321 return NULL_TREE;
8322 }
8323
f4a30bd7 8324 /* The idea here is that `weakref("name")' mutates into `weakref,
8325 alias("name")', and weakref without arguments, in turn,
8326 implicitly adds weak. */
8327
8328 if (args)
8329 {
8330 attr = tree_cons (get_identifier ("alias"), args, attr);
8331 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8332
8333 *no_add_attrs = true;
0a3ecdc1 8334
8335 decl_attributes (node, attr, flags);
f4a30bd7 8336 }
8337 else
8338 {
8339 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8340 error_at (DECL_SOURCE_LOCATION (*node),
8341 "weakref attribute must appear before alias attribute");
f4a30bd7 8342
0a3ecdc1 8343 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8344 and that isn't supported; and because it wants to add it to
8345 the list of weak decls, which isn't helpful. */
8346 DECL_WEAK (*node) = 1;
f4a30bd7 8347 }
8348
8e857c41 8349 if (decl_in_symtab_p (*node))
8350 {
8351 struct symtab_node *n = symtab_node::get (*node);
8352 if (n && n->refuse_visibility_changes)
8353 error ("%+D declared weakref after being used", *node);
8354 }
8355
f4a30bd7 8356 return NULL_TREE;
8357}
8358
f8e93a2e 8359/* Handle an "visibility" attribute; arguments as in
8360 struct attribute_spec.handler. */
8361
8362static tree
1cae46be 8363handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8364 int ARG_UNUSED (flags),
4a2849cb 8365 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8366{
8367 tree decl = *node;
9c40570a 8368 tree id = TREE_VALUE (args);
4a2849cb 8369 enum symbol_visibility vis;
f8e93a2e 8370
b212f378 8371 if (TYPE_P (*node))
8372 {
4a2849cb 8373 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8374 /* OK */;
8375 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8376 {
8377 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8378 name);
8379 return NULL_TREE;
8380 }
8381 else if (TYPE_FIELDS (*node))
8382 {
8383 error ("%qE attribute ignored because %qT is already defined",
8384 name, *node);
8385 return NULL_TREE;
8386 }
b212f378 8387 }
84166705 8388 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8389 {
9b2d6d13 8390 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8391 return NULL_TREE;
f8e93a2e 8392 }
f8e93a2e 8393
9c40570a 8394 if (TREE_CODE (id) != STRING_CST)
8395 {
07e3a3d2 8396 error ("visibility argument not a string");
9c40570a 8397 return NULL_TREE;
f8e93a2e 8398 }
b27ac6b5 8399
b212f378 8400 /* If this is a type, set the visibility on the type decl. */
8401 if (TYPE_P (decl))
8402 {
8403 decl = TYPE_NAME (decl);
84166705 8404 if (!decl)
a0c938f0 8405 return NULL_TREE;
e147aab3 8406 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8407 {
9b2d6d13 8408 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8409 name);
8410 return NULL_TREE;
8411 }
b212f378 8412 }
f8e93a2e 8413
9c40570a 8414 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8415 vis = VISIBILITY_DEFAULT;
9c40570a 8416 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8417 vis = VISIBILITY_INTERNAL;
9c40570a 8418 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8419 vis = VISIBILITY_HIDDEN;
9c40570a 8420 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8421 vis = VISIBILITY_PROTECTED;
9c40570a 8422 else
4a2849cb 8423 {
8424 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8425 vis = VISIBILITY_DEFAULT;
8426 }
8427
8428 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8429 && vis != DECL_VISIBILITY (decl))
8430 {
8431 tree attributes = (TYPE_P (*node)
8432 ? TYPE_ATTRIBUTES (*node)
8433 : DECL_ATTRIBUTES (decl));
8434 if (lookup_attribute ("visibility", attributes))
8435 error ("%qD redeclared with different visibility", decl);
8436 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8437 && lookup_attribute ("dllimport", attributes))
8438 error ("%qD was declared %qs which implies default visibility",
8439 decl, "dllimport");
8440 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8441 && lookup_attribute ("dllexport", attributes))
8442 error ("%qD was declared %qs which implies default visibility",
8443 decl, "dllexport");
8444 }
4a2849cb 8445
8446 DECL_VISIBILITY (decl) = vis;
b212f378 8447 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8448
4a2849cb 8449 /* Go ahead and attach the attribute to the node as well. This is needed
8450 so we can determine whether we have VISIBILITY_DEFAULT because the
8451 visibility was not specified, or because it was explicitly overridden
8452 from the containing scope. */
9c40570a 8453
f8e93a2e 8454 return NULL_TREE;
8455}
8456
3aa0c315 8457/* Determine the ELF symbol visibility for DECL, which is either a
8458 variable or a function. It is an error to use this function if a
8459 definition of DECL is not available in this translation unit.
8460 Returns true if the final visibility has been determined by this
8461 function; false if the caller is free to make additional
8462 modifications. */
8463
8464bool
8465c_determine_visibility (tree decl)
8466{
b443c459 8467 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8468
8469 /* If the user explicitly specified the visibility with an
8470 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8471 the processing of the attribute. We check for an explicit
8472 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8473 to distinguish the use of an attribute from the use of a "#pragma
8474 GCC visibility push(...)"; in the latter case we still want other
8475 considerations to be able to overrule the #pragma. */
8476 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8477 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8478 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8479 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8480 return true;
8481
4a2849cb 8482 /* Set default visibility to whatever the user supplied with
8483 visibility_specified depending on #pragma GCC visibility. */
8484 if (!DECL_VISIBILITY_SPECIFIED (decl))
8485 {
2d9d8740 8486 if (visibility_options.inpragma
8487 || DECL_VISIBILITY (decl) != default_visibility)
8488 {
8489 DECL_VISIBILITY (decl) = default_visibility;
8490 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8491 /* If visibility changed and DECL already has DECL_RTL, ensure
8492 symbol flags are updated. */
8493 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8494 || TREE_CODE (decl) == FUNCTION_DECL)
8495 && DECL_RTL_SET_P (decl))
8496 make_decl_rtl (decl);
8497 }
4a2849cb 8498 }
3aa0c315 8499 return false;
8500}
8501
24dfead4 8502/* Handle an "tls_model" attribute; arguments as in
8503 struct attribute_spec.handler. */
8504
8505static tree
1cae46be 8506handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8507 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8508{
1b53eb20 8509 tree id;
24dfead4 8510 tree decl = *node;
1b53eb20 8511 enum tls_model kind;
24dfead4 8512
1b53eb20 8513 *no_add_attrs = true;
8514
c34f8a78 8515 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8516 {
9b2d6d13 8517 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8518 return NULL_TREE;
24dfead4 8519 }
24dfead4 8520
1b53eb20 8521 kind = DECL_TLS_MODEL (decl);
8522 id = TREE_VALUE (args);
8523 if (TREE_CODE (id) != STRING_CST)
8524 {
8525 error ("tls_model argument not a string");
8526 return NULL_TREE;
24dfead4 8527 }
8528
1b53eb20 8529 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8530 kind = TLS_MODEL_LOCAL_EXEC;
8531 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8532 kind = TLS_MODEL_INITIAL_EXEC;
8533 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8534 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8535 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8536 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8537 else
8538 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8539
5e68df57 8540 set_decl_tls_model (decl, kind);
24dfead4 8541 return NULL_TREE;
8542}
8543
f8e93a2e 8544/* Handle a "no_instrument_function" attribute; arguments as in
8545 struct attribute_spec.handler. */
8546
8547static tree
1cae46be 8548handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8549 tree ARG_UNUSED (args),
8550 int ARG_UNUSED (flags),
09347743 8551 bool *no_add_attrs)
f8e93a2e 8552{
8553 tree decl = *node;
8554
8555 if (TREE_CODE (decl) != FUNCTION_DECL)
8556 {
712d2297 8557 error_at (DECL_SOURCE_LOCATION (decl),
8558 "%qE attribute applies only to functions", name);
f8e93a2e 8559 *no_add_attrs = true;
8560 }
f8e93a2e 8561 else
8562 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8563
8564 return NULL_TREE;
8565}
8566
8567/* Handle a "malloc" attribute; arguments as in
8568 struct attribute_spec.handler. */
8569
8570static tree
9a03a746 8571handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8572 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8573{
a5147fca 8574 if (TREE_CODE (*node) == FUNCTION_DECL
8575 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8576 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8577 else
8578 {
9b2d6d13 8579 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8580 *no_add_attrs = true;
8581 }
8582
8583 return NULL_TREE;
8584}
8585
4a29c97c 8586/* Handle a "alloc_size" attribute; arguments as in
8587 struct attribute_spec.handler. */
8588
8589static tree
8590handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8591 int ARG_UNUSED (flags), bool *no_add_attrs)
8592{
2802826e 8593 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8594 for (; args; args = TREE_CHAIN (args))
8595 {
8596 tree position = TREE_VALUE (args);
caf62483 8597 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8598 && TREE_CODE (position) != FUNCTION_DECL)
8599 position = default_conversion (position);
4a29c97c 8600
237e78b1 8601 if (!tree_fits_uhwi_p (position)
8602 || !arg_count
8603 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8604 {
48e1416a 8605 warning (OPT_Wattributes,
4a29c97c 8606 "alloc_size parameter outside range");
8607 *no_add_attrs = true;
8608 return NULL_TREE;
8609 }
8610 }
8611 return NULL_TREE;
8612}
8613
237e78b1 8614/* Handle a "alloc_align" attribute; arguments as in
8615 struct attribute_spec.handler. */
8616
8617static tree
8618handle_alloc_align_attribute (tree *node, tree, tree args, int,
8619 bool *no_add_attrs)
8620{
8621 unsigned arg_count = type_num_arguments (*node);
8622 tree position = TREE_VALUE (args);
8623 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8624 position = default_conversion (position);
8625
8626 if (!tree_fits_uhwi_p (position)
8627 || !arg_count
8628 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8629 {
8630 warning (OPT_Wattributes,
8631 "alloc_align parameter outside range");
8632 *no_add_attrs = true;
8633 return NULL_TREE;
8634 }
8635 return NULL_TREE;
8636}
8637
8638/* Handle a "assume_aligned" attribute; arguments as in
8639 struct attribute_spec.handler. */
8640
8641static tree
8642handle_assume_aligned_attribute (tree *, tree, tree args, int,
8643 bool *no_add_attrs)
8644{
8645 for (; args; args = TREE_CHAIN (args))
8646 {
8647 tree position = TREE_VALUE (args);
8648 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8649 && TREE_CODE (position) != FUNCTION_DECL)
8650 position = default_conversion (position);
8651
8652 if (TREE_CODE (position) != INTEGER_CST)
8653 {
8654 warning (OPT_Wattributes,
8655 "assume_aligned parameter not integer constant");
8656 *no_add_attrs = true;
8657 return NULL_TREE;
8658 }
8659 }
8660 return NULL_TREE;
8661}
8662
8ce86007 8663/* Handle a "fn spec" attribute; arguments as in
8664 struct attribute_spec.handler. */
8665
8666static tree
8667handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8668 tree args, int ARG_UNUSED (flags),
8669 bool *no_add_attrs ATTRIBUTE_UNUSED)
8670{
8671 gcc_assert (args
8672 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8673 && !TREE_CHAIN (args));
8674 return NULL_TREE;
8675}
8676
058a1b7a 8677/* Handle a "bnd_variable_size" attribute; arguments as in
8678 struct attribute_spec.handler. */
8679
8680static tree
8681handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8682 int ARG_UNUSED (flags), bool *no_add_attrs)
8683{
8684 if (TREE_CODE (*node) != FIELD_DECL)
8685 {
8686 warning (OPT_Wattributes, "%qE attribute ignored", name);
8687 *no_add_attrs = true;
8688 }
8689
8690 return NULL_TREE;
8691}
8692
8693/* Handle a "bnd_legacy" attribute; arguments as in
8694 struct attribute_spec.handler. */
8695
8696static tree
8697handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8698 int ARG_UNUSED (flags), bool *no_add_attrs)
8699{
8700 if (TREE_CODE (*node) != FUNCTION_DECL)
8701 {
8702 warning (OPT_Wattributes, "%qE attribute ignored", name);
8703 *no_add_attrs = true;
8704 }
8705
8706 return NULL_TREE;
8707}
8708
8709/* Handle a "bnd_instrument" attribute; arguments as in
8710 struct attribute_spec.handler. */
8711
8712static tree
8713handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8714 int ARG_UNUSED (flags), bool *no_add_attrs)
8715{
8716 if (TREE_CODE (*node) != FUNCTION_DECL)
8717 {
8718 warning (OPT_Wattributes, "%qE attribute ignored", name);
8719 *no_add_attrs = true;
8720 }
8721
8722 return NULL_TREE;
8723}
8724
a96c3cc1 8725/* Handle a "warn_unused" attribute; arguments as in
8726 struct attribute_spec.handler. */
8727
8728static tree
8729handle_warn_unused_attribute (tree *node, tree name,
8730 tree args ATTRIBUTE_UNUSED,
8731 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8732{
8733 if (TYPE_P (*node))
8734 /* Do nothing else, just set the attribute. We'll get at
8735 it later with lookup_attribute. */
8736 ;
8737 else
8738 {
8739 warning (OPT_Wattributes, "%qE attribute ignored", name);
8740 *no_add_attrs = true;
8741 }
8742
8743 return NULL_TREE;
8744}
8745
bc7bff74 8746/* Handle an "omp declare simd" attribute; arguments as in
8747 struct attribute_spec.handler. */
8748
8749static tree
8750handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8751{
8752 return NULL_TREE;
8753}
8754
8755/* Handle an "omp declare target" attribute; arguments as in
8756 struct attribute_spec.handler. */
8757
8758static tree
8759handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8760{
8761 return NULL_TREE;
8762}
8763
26d1c5ff 8764/* Handle a "returns_twice" attribute; arguments as in
8765 struct attribute_spec.handler. */
8766
8767static tree
8768handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8769 int ARG_UNUSED (flags), bool *no_add_attrs)
8770{
8771 if (TREE_CODE (*node) == FUNCTION_DECL)
8772 DECL_IS_RETURNS_TWICE (*node) = 1;
8773 else
8774 {
9b2d6d13 8775 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8776 *no_add_attrs = true;
8777 }
8778
8779 return NULL_TREE;
8780}
8781
f8e93a2e 8782/* Handle a "no_limit_stack" attribute; arguments as in
8783 struct attribute_spec.handler. */
8784
8785static tree
1cae46be 8786handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8787 tree ARG_UNUSED (args),
8788 int ARG_UNUSED (flags),
09347743 8789 bool *no_add_attrs)
f8e93a2e 8790{
8791 tree decl = *node;
8792
8793 if (TREE_CODE (decl) != FUNCTION_DECL)
8794 {
712d2297 8795 error_at (DECL_SOURCE_LOCATION (decl),
8796 "%qE attribute applies only to functions", name);
f8e93a2e 8797 *no_add_attrs = true;
8798 }
8799 else if (DECL_INITIAL (decl))
8800 {
712d2297 8801 error_at (DECL_SOURCE_LOCATION (decl),
8802 "can%'t set %qE attribute after definition", name);
f8e93a2e 8803 *no_add_attrs = true;
8804 }
8805 else
8806 DECL_NO_LIMIT_STACK (decl) = 1;
8807
8808 return NULL_TREE;
8809}
8810
8811/* Handle a "pure" attribute; arguments as in
8812 struct attribute_spec.handler. */
8813
8814static tree
9a03a746 8815handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8816 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8817{
8818 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8819 DECL_PURE_P (*node) = 1;
f8e93a2e 8820 /* ??? TODO: Support types. */
8821 else
8822 {
9b2d6d13 8823 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8824 *no_add_attrs = true;
8825 }
8826
8827 return NULL_TREE;
8828}
8829
4c0315d0 8830/* Digest an attribute list destined for a transactional memory statement.
8831 ALLOWED is the set of attributes that are allowed for this statement;
8832 return the attribute we parsed. Multiple attributes are never allowed. */
8833
8834int
8835parse_tm_stmt_attr (tree attrs, int allowed)
8836{
8837 tree a_seen = NULL;
8838 int m_seen = 0;
8839
8840 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8841 {
8842 tree a = TREE_PURPOSE (attrs);
8843 int m = 0;
8844
8845 if (is_attribute_p ("outer", a))
8846 m = TM_STMT_ATTR_OUTER;
8847
8848 if ((m & allowed) == 0)
8849 {
8850 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8851 continue;
8852 }
8853
8854 if (m_seen == 0)
8855 {
8856 a_seen = a;
8857 m_seen = m;
8858 }
8859 else if (m_seen == m)
8860 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8861 else
8862 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8863 }
8864
8865 return m_seen;
8866}
8867
8868/* Transform a TM attribute name into a maskable integer and back.
8869 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8870 to how the lack of an attribute is treated. */
8871
8872int
8873tm_attr_to_mask (tree attr)
8874{
8875 if (attr == NULL)
8876 return 0;
8877 if (is_attribute_p ("transaction_safe", attr))
8878 return TM_ATTR_SAFE;
8879 if (is_attribute_p ("transaction_callable", attr))
8880 return TM_ATTR_CALLABLE;
8881 if (is_attribute_p ("transaction_pure", attr))
8882 return TM_ATTR_PURE;
8883 if (is_attribute_p ("transaction_unsafe", attr))
8884 return TM_ATTR_IRREVOCABLE;
8885 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8886 return TM_ATTR_MAY_CANCEL_OUTER;
8887 return 0;
8888}
8889
8890tree
8891tm_mask_to_attr (int mask)
8892{
8893 const char *str;
8894 switch (mask)
8895 {
8896 case TM_ATTR_SAFE:
8897 str = "transaction_safe";
8898 break;
8899 case TM_ATTR_CALLABLE:
8900 str = "transaction_callable";
8901 break;
8902 case TM_ATTR_PURE:
8903 str = "transaction_pure";
8904 break;
8905 case TM_ATTR_IRREVOCABLE:
8906 str = "transaction_unsafe";
8907 break;
8908 case TM_ATTR_MAY_CANCEL_OUTER:
8909 str = "transaction_may_cancel_outer";
8910 break;
8911 default:
8912 gcc_unreachable ();
8913 }
8914 return get_identifier (str);
8915}
8916
8917/* Return the first TM attribute seen in LIST. */
8918
8919tree
8920find_tm_attribute (tree list)
8921{
8922 for (; list ; list = TREE_CHAIN (list))
8923 {
8924 tree name = TREE_PURPOSE (list);
8925 if (tm_attr_to_mask (name) != 0)
8926 return name;
8927 }
8928 return NULL_TREE;
8929}
8930
8931/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8932 Here we accept only function types, and verify that none of the other
8933 function TM attributes are also applied. */
8934/* ??? We need to accept class types for C++, but not C. This greatly
8935 complicates this function, since we can no longer rely on the extra
8936 processing given by function_type_required. */
8937
8938static tree
8939handle_tm_attribute (tree *node, tree name, tree args,
8940 int flags, bool *no_add_attrs)
8941{
8942 /* Only one path adds the attribute; others don't. */
8943 *no_add_attrs = true;
8944
8945 switch (TREE_CODE (*node))
8946 {
8947 case RECORD_TYPE:
8948 case UNION_TYPE:
8949 /* Only tm_callable and tm_safe apply to classes. */
8950 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8951 goto ignored;
8952 /* FALLTHRU */
8953
8954 case FUNCTION_TYPE:
8955 case METHOD_TYPE:
8956 {
8957 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8958 if (old_name == name)
8959 ;
8960 else if (old_name != NULL_TREE)
8961 error ("type was previously declared %qE", old_name);
8962 else
8963 *no_add_attrs = false;
8964 }
8965 break;
8966
8967 case POINTER_TYPE:
8968 {
8969 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8970 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8971 {
8972 tree fn_tmp = TREE_TYPE (*node);
8973 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8974 *node = build_pointer_type (fn_tmp);
8975 break;
8976 }
8977 }
8978 /* FALLTHRU */
8979
8980 default:
8981 /* If a function is next, pass it on to be tried next. */
8982 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8983 return tree_cons (name, args, NULL);
8984
8985 ignored:
8986 warning (OPT_Wattributes, "%qE attribute ignored", name);
8987 break;
8988 }
8989
8990 return NULL_TREE;
8991}
8992
8993/* Handle the TM_WRAP attribute; arguments as in
8994 struct attribute_spec.handler. */
8995
8996static tree
8997handle_tm_wrap_attribute (tree *node, tree name, tree args,
8998 int ARG_UNUSED (flags), bool *no_add_attrs)
8999{
9000 tree decl = *node;
9001
9002 /* We don't need the attribute even on success, since we
9003 record the entry in an external table. */
9004 *no_add_attrs = true;
9005
9006 if (TREE_CODE (decl) != FUNCTION_DECL)
9007 warning (OPT_Wattributes, "%qE attribute ignored", name);
9008 else
9009 {
9010 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9011 if (error_operand_p (wrap_decl))
9012 ;
9013 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9014 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9015 error ("%qE argument not an identifier", name);
9016 else
9017 {
9018 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9019 wrap_decl = lookup_name (wrap_decl);
9020 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9021 {
9022 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9023 TREE_TYPE (wrap_decl)))
9024 record_tm_replacement (wrap_decl, decl);
9025 else
9026 error ("%qD is not compatible with %qD", wrap_decl, decl);
9027 }
9028 else
cdf34fca 9029 error ("%qE argument is not a function", name);
4c0315d0 9030 }
9031 }
9032
9033 return NULL_TREE;
9034}
9035
9036/* Ignore the given attribute. Used when this attribute may be usefully
9037 overridden by the target, but is not used generically. */
9038
9039static tree
9040ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9041 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9042 bool *no_add_attrs)
9043{
9044 *no_add_attrs = true;
9045 return NULL_TREE;
9046}
9047
fc09b200 9048/* Handle a "no vops" attribute; arguments as in
9049 struct attribute_spec.handler. */
9050
9051static tree
9052handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9053 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9054 bool *ARG_UNUSED (no_add_attrs))
9055{
9056 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9057 DECL_IS_NOVOPS (*node) = 1;
9058 return NULL_TREE;
9059}
9060
f8e93a2e 9061/* Handle a "deprecated" attribute; arguments as in
9062 struct attribute_spec.handler. */
1cae46be 9063
f8e93a2e 9064static tree
1cae46be 9065handle_deprecated_attribute (tree *node, tree name,
45c4e798 9066 tree args, int flags,
09347743 9067 bool *no_add_attrs)
f8e93a2e 9068{
9069 tree type = NULL_TREE;
9070 int warn = 0;
782858b8 9071 tree what = NULL_TREE;
1cae46be 9072
45c4e798 9073 if (!args)
9074 *no_add_attrs = true;
9075 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9076 {
9077 error ("deprecated message is not a string");
9078 *no_add_attrs = true;
9079 }
9080
f8e93a2e 9081 if (DECL_P (*node))
9082 {
9083 tree decl = *node;
9084 type = TREE_TYPE (decl);
1cae46be 9085
f8e93a2e 9086 if (TREE_CODE (decl) == TYPE_DECL
9087 || TREE_CODE (decl) == PARM_DECL
b443c459 9088 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9089 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9090 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9091 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9092 TREE_DEPRECATED (decl) = 1;
9093 else
9094 warn = 1;
9095 }
9096 else if (TYPE_P (*node))
9097 {
9098 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9099 *node = build_variant_type_copy (*node);
f8e93a2e 9100 TREE_DEPRECATED (*node) = 1;
9101 type = *node;
9102 }
9103 else
9104 warn = 1;
1cae46be 9105
f8e93a2e 9106 if (warn)
9107 {
9108 *no_add_attrs = true;
9109 if (type && TYPE_NAME (type))
9110 {
9111 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9112 what = TYPE_NAME (*node);
f8e93a2e 9113 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9114 && DECL_NAME (TYPE_NAME (type)))
782858b8 9115 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9116 }
9117 if (what)
9b2d6d13 9118 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9119 else
9b2d6d13 9120 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9121 }
9122
9123 return NULL_TREE;
9124}
9125
f8e93a2e 9126/* Handle a "vector_size" attribute; arguments as in
9127 struct attribute_spec.handler. */
9128
9129static tree
1cae46be 9130handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9131 int ARG_UNUSED (flags),
09347743 9132 bool *no_add_attrs)
f8e93a2e 9133{
9134 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9135 machine_mode orig_mode;
4917c376 9136 tree type = *node, new_type, size;
f8e93a2e 9137
9138 *no_add_attrs = true;
9139
4917c376 9140 size = TREE_VALUE (args);
3e5a8b00 9141 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9142 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9143 size = default_conversion (size);
4917c376 9144
e913b5cd 9145 if (!tree_fits_uhwi_p (size))
f8e93a2e 9146 {
9b2d6d13 9147 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9148 return NULL_TREE;
9149 }
9150
9151 /* Get the vector size (in bytes). */
e913b5cd 9152 vecsize = tree_to_uhwi (size);
f8e93a2e 9153
9154 /* We need to provide for vector pointers, vector arrays, and
9155 functions returning vectors. For example:
9156
9157 __attribute__((vector_size(16))) short *foo;
9158
9159 In this case, the mode is SI, but the type being modified is
9160 HI, so we need to look further. */
9161
9162 while (POINTER_TYPE_P (type)
9163 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9164 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9165 || TREE_CODE (type) == ARRAY_TYPE
9166 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9167 type = TREE_TYPE (type);
9168
9169 /* Get the mode of the type being modified. */
9170 orig_mode = TYPE_MODE (type);
9171
2cb0e5d9 9172 if ((!INTEGRAL_TYPE_P (type)
9173 && !SCALAR_FLOAT_TYPE_P (type)
9174 && !FIXED_POINT_TYPE_P (type))
cee7491d 9175 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9176 && GET_MODE_CLASS (orig_mode) != MODE_INT
9177 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9178 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9179 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9180 {
782858b8 9181 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9182 return NULL_TREE;
9183 }
9184
e913b5cd 9185 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9186 {
9187 error ("vector size not an integral multiple of component size");
9188 return NULL;
9189 }
9190
9191 if (vecsize == 0)
9192 {
9193 error ("zero vector size");
9194 return NULL;
9195 }
9196
f8e93a2e 9197 /* Calculate how many units fit in the vector. */
e913b5cd 9198 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9199 if (nunits & (nunits - 1))
f8e93a2e 9200 {
83e2a11b 9201 error ("number of components of the vector not a power of two");
f8e93a2e 9202 return NULL_TREE;
9203 }
9204
83e2a11b 9205 new_type = build_vector_type (type, nunits);
f8e93a2e 9206
9207 /* Build back pointers if needed. */
d991e6e8 9208 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9209
9210 return NULL_TREE;
9211}
9212
dbf6c367 9213/* Handle the "nonnull" attribute. */
9214static tree
9a03a746 9215handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9216 tree args, int ARG_UNUSED (flags),
09347743 9217 bool *no_add_attrs)
dbf6c367 9218{
9219 tree type = *node;
9220 unsigned HOST_WIDE_INT attr_arg_num;
9221
9222 /* If no arguments are specified, all pointer arguments should be
d716ce75 9223 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9224 will have the correct types when we actually check them later. */
84166705 9225 if (!args)
dbf6c367 9226 {
a36cf284 9227 if (!prototype_p (type))
dbf6c367 9228 {
9229 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9230 *no_add_attrs = true;
dbf6c367 9231 }
9232 return NULL_TREE;
9233 }
9234
9235 /* Argument list specified. Verify that each argument number references
9236 a pointer argument. */
caf62483 9237 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9238 {
4ee9c684 9239 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9240
caf62483 9241 tree arg = TREE_VALUE (args);
9242 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9243 && TREE_CODE (arg) != FUNCTION_DECL)
9244 arg = default_conversion (arg);
9245
9246 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9247 {
07e3a3d2 9248 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9249 (unsigned long) attr_arg_num);
9250 *no_add_attrs = true;
9251 return NULL_TREE;
9252 }
9253
d0af78c5 9254 if (prototype_p (type))
dbf6c367 9255 {
d0af78c5 9256 function_args_iterator iter;
9257 tree argument;
9258
9259 function_args_iter_init (&iter, type);
9260 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9261 {
d0af78c5 9262 argument = function_args_iter_cond (&iter);
9263 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9264 break;
dbf6c367 9265 }
9266
84166705 9267 if (!argument
d0af78c5 9268 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9269 {
07e3a3d2 9270 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9271 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9272 *no_add_attrs = true;
9273 return NULL_TREE;
9274 }
9275
d0af78c5 9276 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9277 {
07e3a3d2 9278 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9279 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9280 *no_add_attrs = true;
9281 return NULL_TREE;
9282 }
9283 }
9284 }
9285
9286 return NULL_TREE;
9287}
9288
9289/* Check the argument list of a function call for null in argument slots
d01f58f9 9290 that are marked as requiring a non-null pointer argument. The NARGS
9291 arguments are passed in the array ARGARRAY.
9292*/
dbf6c367 9293
9294static void
d01f58f9 9295check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9296{
9ca77b08 9297 tree a;
d01f58f9 9298 int i;
dbf6c367 9299
9ca77b08 9300 attrs = lookup_attribute ("nonnull", attrs);
9301 if (attrs == NULL_TREE)
9302 return;
9303
9304 a = attrs;
9305 /* See if any of the nonnull attributes has no arguments. If so,
9306 then every pointer argument is checked (in which case the check
9307 for pointer type is done in check_nonnull_arg). */
9308 if (TREE_VALUE (a) != NULL_TREE)
9309 do
9310 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9311 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9312
9313 if (a != NULL_TREE)
9314 for (i = 0; i < nargs; i++)
9315 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9316 i + 1);
9317 else
dbf6c367 9318 {
9ca77b08 9319 /* Walk the argument list. If we encounter an argument number we
9320 should check for non-null, do it. */
9321 for (i = 0; i < nargs; i++)
dbf6c367 9322 {
9ca77b08 9323 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9324 {
9ca77b08 9325 a = lookup_attribute ("nonnull", a);
9326 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9327 break;
4ee9c684 9328 }
9ca77b08 9329
9330 if (a != NULL_TREE)
9331 check_function_arguments_recurse (check_nonnull_arg, NULL,
9332 argarray[i], i + 1);
dbf6c367 9333 }
9334 }
9335}
9336
50ca527f 9337/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9338 from the end) is a (pointer)0. The NARGS arguments are passed in the
9339 array ARGARRAY. */
bf6c8de0 9340
9341static void
774e9d58 9342check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9343{
774e9d58 9344 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9345
9346 if (attr)
9347 {
d01f58f9 9348 int len = 0;
9349 int pos = 0;
9350 tree sentinel;
774e9d58 9351 function_args_iterator iter;
9352 tree t;
a0c938f0 9353
d01f58f9 9354 /* Skip over the named arguments. */
774e9d58 9355 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9356 {
774e9d58 9357 if (len == nargs)
9358 break;
d01f58f9 9359 len++;
9360 }
50ca527f 9361
d01f58f9 9362 if (TREE_VALUE (attr))
9363 {
9364 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9365 pos = TREE_INT_CST_LOW (p);
d01f58f9 9366 }
50ca527f 9367
d01f58f9 9368 /* The sentinel must be one of the varargs, i.e.
9369 in position >= the number of fixed arguments. */
9370 if ((nargs - 1 - pos) < len)
9371 {
77a357e3 9372 warning (OPT_Wformat_,
d01f58f9 9373 "not enough variable arguments to fit a sentinel");
9374 return;
bf6c8de0 9375 }
d01f58f9 9376
9377 /* Validate the sentinel. */
9378 sentinel = argarray[nargs - 1 - pos];
9379 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9380 || !integer_zerop (sentinel))
9381 /* Although __null (in C++) is only an integer we allow it
9382 nevertheless, as we are guaranteed that it's exactly
9383 as wide as a pointer, and we don't want to force
9384 users to cast the NULL they have written there.
9385 We warn with -Wstrict-null-sentinel, though. */
9386 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9387 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9388 }
9389}
9390
dbf6c367 9391/* Helper for check_function_nonnull; given a list of operands which
9392 must be non-null in ARGS, determine if operand PARAM_NUM should be
9393 checked. */
9394
9395static bool
1cae46be 9396nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9397{
4ee9c684 9398 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9399
9400 for (; args; args = TREE_CHAIN (args))
9401 {
231bd014 9402 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9403
9404 gcc_assert (found);
dbf6c367 9405
9406 if (arg_num == param_num)
9407 return true;
9408 }
9409 return false;
9410}
9411
9412/* Check that the function argument PARAM (which is operand number
9413 PARAM_NUM) is non-null. This is called by check_function_nonnull
9414 via check_function_arguments_recurse. */
9415
9416static void
9a03a746 9417check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9418 unsigned HOST_WIDE_INT param_num)
dbf6c367 9419{
9420 /* Just skip checking the argument if it's not a pointer. This can
9421 happen if the "nonnull" attribute was given without an operand
9422 list (which means to check every pointer argument). */
9423
9424 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9425 return;
9426
9427 if (integer_zerop (param))
155b601b 9428 warning (OPT_Wnonnull, "null argument where non-null required "
9429 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9430}
9431
9432/* Helper for nonnull attribute handling; fetch the operand number
9433 from the attribute argument list. */
9434
9435static bool
1cae46be 9436get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9437{
e913b5cd 9438 /* Verify the arg number is a small constant. */
e1d65c9f 9439 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9440 {
f9ae6f95 9441 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9442 return true;
9443 }
9444 else
dbf6c367 9445 return false;
dbf6c367 9446}
fa987697 9447
9448/* Handle a "nothrow" attribute; arguments as in
9449 struct attribute_spec.handler. */
9450
9451static tree
9a03a746 9452handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9453 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9454{
9455 if (TREE_CODE (*node) == FUNCTION_DECL)
9456 TREE_NOTHROW (*node) = 1;
9457 /* ??? TODO: Support types. */
9458 else
9459 {
9b2d6d13 9460 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9461 *no_add_attrs = true;
9462 }
9463
9464 return NULL_TREE;
9465}
7acb29a3 9466
9467/* Handle a "cleanup" attribute; arguments as in
9468 struct attribute_spec.handler. */
9469
9470static tree
1cae46be 9471handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9472 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9473{
9474 tree decl = *node;
9475 tree cleanup_id, cleanup_decl;
9476
9477 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9478 for global destructors in C++. This requires infrastructure that
9479 we don't have generically at the moment. It's also not a feature
9480 we'd be missing too much, since we do have attribute constructor. */
9481 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9482 {
9b2d6d13 9483 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9484 *no_add_attrs = true;
9485 return NULL_TREE;
9486 }
9487
9488 /* Verify that the argument is a function in scope. */
9489 /* ??? We could support pointers to functions here as well, if
9490 that was considered desirable. */
9491 cleanup_id = TREE_VALUE (args);
9492 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9493 {
07e3a3d2 9494 error ("cleanup argument not an identifier");
7acb29a3 9495 *no_add_attrs = true;
9496 return NULL_TREE;
9497 }
d1c41717 9498 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9499 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9500 {
07e3a3d2 9501 error ("cleanup argument not a function");
7acb29a3 9502 *no_add_attrs = true;
9503 return NULL_TREE;
9504 }
9505
1cae46be 9506 /* That the function has proper type is checked with the
7acb29a3 9507 eventual call to build_function_call. */
9508
9509 return NULL_TREE;
9510}
8a8cdb8d 9511
9512/* Handle a "warn_unused_result" attribute. No special handling. */
9513
9514static tree
9515handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9516 tree ARG_UNUSED (args),
9517 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9518{
9519 /* Ignore the attribute for functions not returning any value. */
9520 if (VOID_TYPE_P (TREE_TYPE (*node)))
9521 {
9b2d6d13 9522 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9523 *no_add_attrs = true;
9524 }
9525
9526 return NULL_TREE;
9527}
bf6c8de0 9528
9529/* Handle a "sentinel" attribute. */
9530
9531static tree
50ca527f 9532handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9533 int ARG_UNUSED (flags), bool *no_add_attrs)
9534{
a36cf284 9535 if (!prototype_p (*node))
bf6c8de0 9536 {
9b2d6d13 9537 warning (OPT_Wattributes,
9538 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9539 *no_add_attrs = true;
bf6c8de0 9540 }
50ca527f 9541 else
9542 {
c33080b9 9543 if (!stdarg_p (*node))
a0c938f0 9544 {
9b2d6d13 9545 warning (OPT_Wattributes,
9546 "%qE attribute only applies to variadic functions", name);
50ca527f 9547 *no_add_attrs = true;
9548 }
9549 }
a0c938f0 9550
50ca527f 9551 if (args)
bf6c8de0 9552 {
50ca527f 9553 tree position = TREE_VALUE (args);
3e5a8b00 9554 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9555 && TREE_CODE (position) != FUNCTION_DECL)
9556 position = default_conversion (position);
50ca527f 9557
3e5a8b00 9558 if (TREE_CODE (position) != INTEGER_CST
9559 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9560 {
48e1416a 9561 warning (OPT_Wattributes,
01b54db5 9562 "requested position is not an integer constant");
50ca527f 9563 *no_add_attrs = true;
9564 }
9565 else
a0c938f0 9566 {
50ca527f 9567 if (tree_int_cst_lt (position, integer_zero_node))
9568 {
01b54db5 9569 warning (OPT_Wattributes,
9570 "requested position is less than zero");
50ca527f 9571 *no_add_attrs = true;
9572 }
9573 }
bf6c8de0 9574 }
a0c938f0 9575
bf6c8de0 9576 return NULL_TREE;
9577}
b5c26b42 9578
9579/* Handle a "type_generic" attribute. */
9580
9581static tree
9582handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9583 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9584 bool * ARG_UNUSED (no_add_attrs))
9585{
19fbe3a4 9586 /* Ensure we have a function type. */
9587 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9588
19fbe3a4 9589 /* Ensure we have a variadic function. */
c33080b9 9590 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9591
9592 return NULL_TREE;
9593}
46f8e3b0 9594
24470055 9595/* Handle a "target" attribute. */
46f8e3b0 9596
9597static tree
24470055 9598handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9599 bool *no_add_attrs)
9600{
9601 /* Ensure we have a function type. */
9602 if (TREE_CODE (*node) != FUNCTION_DECL)
9603 {
9604 warning (OPT_Wattributes, "%qE attribute ignored", name);
9605 *no_add_attrs = true;
9606 }
46f8e3b0 9607 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9608 flags))
46f8e3b0 9609 *no_add_attrs = true;
9610
9611 return NULL_TREE;
9612}
9613
9614/* Arguments being collected for optimization. */
9615typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9616static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9617
9618
9619/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9620 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9621 false for #pragma GCC optimize. */
9622
9623bool
9624parse_optimize_options (tree args, bool attr_p)
9625{
9626 bool ret = true;
9627 unsigned opt_argc;
9628 unsigned i;
2becf397 9629 int saved_flag_strict_aliasing;
46f8e3b0 9630 const char **opt_argv;
615ef0bb 9631 struct cl_decoded_option *decoded_options;
9632 unsigned int decoded_options_count;
46f8e3b0 9633 tree ap;
9634
9635 /* Build up argv vector. Just in case the string is stored away, use garbage
9636 collected strings. */
f1f41a6c 9637 vec_safe_truncate (optimize_args, 0);
9638 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9639
9640 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9641 {
9642 tree value = TREE_VALUE (ap);
9643
9644 if (TREE_CODE (value) == INTEGER_CST)
9645 {
9646 char buffer[20];
f9ae6f95 9647 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9648 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9649 }
9650
9651 else if (TREE_CODE (value) == STRING_CST)
9652 {
9653 /* Split string into multiple substrings. */
9654 size_t len = TREE_STRING_LENGTH (value);
9655 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9656 char *end = p + len;
9657 char *comma;
9658 char *next_p = p;
9659
9660 while (next_p != NULL)
9661 {
9662 size_t len2;
9663 char *q, *r;
9664
9665 p = next_p;
9666 comma = strchr (p, ',');
9667 if (comma)
9668 {
9669 len2 = comma - p;
9670 *comma = '\0';
9671 next_p = comma+1;
9672 }
9673 else
9674 {
9675 len2 = end - p;
9676 next_p = NULL;
9677 }
9678
ba72912a 9679 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9680
9681 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9682 options. */
9683 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9684 {
9685 ret = false;
9686 if (attr_p)
9687 warning (OPT_Wattributes,
ebd7c4c1 9688 "bad option %s to optimize attribute", p);
46f8e3b0 9689 else
9690 warning (OPT_Wpragmas,
e44b0a1f 9691 "bad option %s to pragma attribute", p);
46f8e3b0 9692 continue;
9693 }
9694
9695 if (*p != '-')
9696 {
9697 *r++ = '-';
9698
9699 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9700 itself is -Os, and any other switch begins with a -f. */
9701 if ((*p >= '0' && *p <= '9')
9702 || (p[0] == 's' && p[1] == '\0'))
9703 *r++ = 'O';
9704 else if (*p != 'O')
9705 *r++ = 'f';
9706 }
9707
9708 memcpy (r, p, len2);
9709 r[len2] = '\0';
f1f41a6c 9710 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9711 }
9712
9713 }
9714 }
9715
f1f41a6c 9716 opt_argc = optimize_args->length ();
46f8e3b0 9717 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9718
9719 for (i = 1; i < opt_argc; i++)
f1f41a6c 9720 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9721
2becf397 9722 saved_flag_strict_aliasing = flag_strict_aliasing;
9723
46f8e3b0 9724 /* Now parse the options. */
f3f006ad 9725 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9726 &decoded_options,
9727 &decoded_options_count);
9728 decode_options (&global_options, &global_options_set,
3c6c0e40 9729 decoded_options, decoded_options_count,
9730 input_location, global_dc);
46f8e3b0 9731
4bec06b3 9732 targetm.override_options_after_change();
9733
2becf397 9734 /* Don't allow changing -fstrict-aliasing. */
9735 flag_strict_aliasing = saved_flag_strict_aliasing;
9736
f1f41a6c 9737 optimize_args->truncate (0);
46f8e3b0 9738 return ret;
9739}
9740
9741/* For handling "optimize" attribute. arguments as in
9742 struct attribute_spec.handler. */
9743
9744static tree
9745handle_optimize_attribute (tree *node, tree name, tree args,
9746 int ARG_UNUSED (flags), bool *no_add_attrs)
9747{
9748 /* Ensure we have a function type. */
9749 if (TREE_CODE (*node) != FUNCTION_DECL)
9750 {
9751 warning (OPT_Wattributes, "%qE attribute ignored", name);
9752 *no_add_attrs = true;
9753 }
9754 else
9755 {
9756 struct cl_optimization cur_opts;
9757 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9758
9759 /* Save current options. */
2c5d2e39 9760 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9761
9762 /* If we previously had some optimization options, use them as the
9763 default. */
9764 if (old_opts)
2c5d2e39 9765 cl_optimization_restore (&global_options,
9766 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9767
9768 /* Parse options, and update the vector. */
9769 parse_optimize_options (args, true);
9770 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9771 = build_optimization_node (&global_options);
46f8e3b0 9772
9773 /* Restore current options. */
2c5d2e39 9774 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9775 }
9776
9777 return NULL_TREE;
9778}
48b14f50 9779
9780/* Handle a "no_split_stack" attribute. */
9781
9782static tree
9783handle_no_split_stack_attribute (tree *node, tree name,
9784 tree ARG_UNUSED (args),
9785 int ARG_UNUSED (flags),
9786 bool *no_add_attrs)
9787{
9788 tree decl = *node;
9789
9790 if (TREE_CODE (decl) != FUNCTION_DECL)
9791 {
9792 error_at (DECL_SOURCE_LOCATION (decl),
9793 "%qE attribute applies only to functions", name);
9794 *no_add_attrs = true;
9795 }
9796 else if (DECL_INITIAL (decl))
9797 {
9798 error_at (DECL_SOURCE_LOCATION (decl),
9799 "can%'t set %qE attribute after definition", name);
9800 *no_add_attrs = true;
9801 }
9802
9803 return NULL_TREE;
9804}
d7dcba40 9805
9806/* Handle a "returns_nonnull" attribute; arguments as in
9807 struct attribute_spec.handler. */
9808
9809static tree
9810handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9811 bool *no_add_attrs)
9812{
9813 // Even without a prototype we still have a return type we can check.
9814 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9815 {
9816 error ("returns_nonnull attribute on a function not returning a pointer");
9817 *no_add_attrs = true;
9818 }
9819 return NULL_TREE;
9820}
9821
74691f46 9822/* Handle a "designated_init" attribute; arguments as in
9823 struct attribute_spec.handler. */
9824
9825static tree
9826handle_designated_init_attribute (tree *node, tree name, tree, int,
9827 bool *no_add_attrs)
9828{
9829 if (TREE_CODE (*node) != RECORD_TYPE)
9830 {
9831 error ("%qE attribute is only valid on %<struct%> type", name);
9832 *no_add_attrs = true;
9833 }
9834 return NULL_TREE;
9835}
9836
dbf6c367 9837\f
774e9d58 9838/* Check for valid arguments being passed to a function with FNTYPE.
9839 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 9840void
774e9d58 9841check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 9842{
9843 /* Check for null being passed in a pointer argument that must be
9844 non-null. We also need to do this if format checking is enabled. */
9845
9846 if (warn_nonnull)
774e9d58 9847 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9848
9849 /* Check for errors in format strings. */
9850
068bea1e 9851 if (warn_format || warn_suggest_attribute_format)
774e9d58 9852 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9853
9854 if (warn_format)
774e9d58 9855 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9856}
9857
9858/* Generic argument checking recursion routine. PARAM is the argument to
9859 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9860 once the argument is resolved. CTX is context for the callback. */
9861void
1cae46be 9862check_function_arguments_recurse (void (*callback)
9863 (void *, tree, unsigned HOST_WIDE_INT),
9864 void *ctx, tree param,
9865 unsigned HOST_WIDE_INT param_num)
dbf6c367 9866{
72dd6141 9867 if (CONVERT_EXPR_P (param)
c44afe23 9868 && (TYPE_PRECISION (TREE_TYPE (param))
9869 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9870 {
9871 /* Strip coercion. */
9872 check_function_arguments_recurse (callback, ctx,
4ee9c684 9873 TREE_OPERAND (param, 0), param_num);
dbf6c367 9874 return;
9875 }
9876
9877 if (TREE_CODE (param) == CALL_EXPR)
9878 {
c2f47e15 9879 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9880 tree attrs;
9881 bool found_format_arg = false;
9882
9883 /* See if this is a call to a known internationalization function
9884 that modifies a format arg. Such a function may have multiple
9885 format_arg attributes (for example, ngettext). */
9886
9887 for (attrs = TYPE_ATTRIBUTES (type);
9888 attrs;
9889 attrs = TREE_CHAIN (attrs))
9890 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9891 {
c2f47e15 9892 tree inner_arg;
dbf6c367 9893 tree format_num_expr;
9894 int format_num;
9895 int i;
c2f47e15 9896 call_expr_arg_iterator iter;
dbf6c367 9897
9898 /* Extract the argument number, which was previously checked
9899 to be valid. */
9900 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9901
e913b5cd 9902 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9903
c2f47e15 9904 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9905 inner_arg != 0;
9906 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9907 if (i == format_num)
9908 {
9909 check_function_arguments_recurse (callback, ctx,
c2f47e15 9910 inner_arg, param_num);
dbf6c367 9911 found_format_arg = true;
9912 break;
9913 }
9914 }
9915
9916 /* If we found a format_arg attribute and did a recursive check,
9917 we are done with checking this argument. Otherwise, we continue
9918 and this will be considered a non-literal. */
9919 if (found_format_arg)
9920 return;
9921 }
9922
9923 if (TREE_CODE (param) == COND_EXPR)
9924 {
9925 /* Check both halves of the conditional expression. */
9926 check_function_arguments_recurse (callback, ctx,
4ee9c684 9927 TREE_OPERAND (param, 1), param_num);
dbf6c367 9928 check_function_arguments_recurse (callback, ctx,
4ee9c684 9929 TREE_OPERAND (param, 2), param_num);
dbf6c367 9930 return;
9931 }
9932
9933 (*callback) (ctx, param, param_num);
9934}
1f3233d1 9935
60cce472 9936/* Checks for a builtin function FNDECL that the number of arguments
9937 NARGS against the required number REQUIRED and issues an error if
9938 there is a mismatch. Returns true if the number of arguments is
9939 correct, otherwise false. */
d43cee80 9940
9941static bool
60cce472 9942builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9943{
9944 if (nargs < required)
9945 {
60cce472 9946 error_at (input_location,
9947 "not enough arguments to function %qE", fndecl);
d43cee80 9948 return false;
9949 }
9950 else if (nargs > required)
9951 {
60cce472 9952 error_at (input_location,
9953 "too many arguments to function %qE", fndecl);
d43cee80 9954 return false;
9955 }
9956 return true;
9957}
9958
9959/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9960 Returns false if there was an error, otherwise true. */
9961
9962bool
9963check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9964{
9965 if (!DECL_BUILT_IN (fndecl)
9966 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9967 return true;
9968
9969 switch (DECL_FUNCTION_CODE (fndecl))
9970 {
9971 case BUILT_IN_CONSTANT_P:
60cce472 9972 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 9973
9974 case BUILT_IN_ISFINITE:
9975 case BUILT_IN_ISINF:
c319d56a 9976 case BUILT_IN_ISINF_SIGN:
d43cee80 9977 case BUILT_IN_ISNAN:
9978 case BUILT_IN_ISNORMAL:
60cce472 9979 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 9980 {
9981 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9982 {
9983 error ("non-floating-point argument in call to "
9984 "function %qE", fndecl);
9985 return false;
9986 }
9987 return true;
9988 }
9989 return false;
9990
9991 case BUILT_IN_ISGREATER:
9992 case BUILT_IN_ISGREATEREQUAL:
9993 case BUILT_IN_ISLESS:
9994 case BUILT_IN_ISLESSEQUAL:
9995 case BUILT_IN_ISLESSGREATER:
9996 case BUILT_IN_ISUNORDERED:
60cce472 9997 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 9998 {
9999 enum tree_code code0, code1;
10000 code0 = TREE_CODE (TREE_TYPE (args[0]));
10001 code1 = TREE_CODE (TREE_TYPE (args[1]));
10002 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10003 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10004 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10005 {
10006 error ("non-floating-point arguments in call to "
10007 "function %qE", fndecl);
10008 return false;
10009 }
10010 return true;
10011 }
10012 return false;
10013
19fbe3a4 10014 case BUILT_IN_FPCLASSIFY:
60cce472 10015 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10016 {
10017 unsigned i;
48e1416a 10018
19fbe3a4 10019 for (i=0; i<5; i++)
10020 if (TREE_CODE (args[i]) != INTEGER_CST)
10021 {
10022 error ("non-const integer argument %u in call to function %qE",
10023 i+1, fndecl);
10024 return false;
10025 }
10026
10027 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10028 {
10029 error ("non-floating-point argument in call to function %qE",
10030 fndecl);
10031 return false;
10032 }
10033 return true;
10034 }
10035 return false;
10036
fca0886c 10037 case BUILT_IN_ASSUME_ALIGNED:
10038 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10039 {
10040 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10041 {
10042 error ("non-integer argument 3 in call to function %qE", fndecl);
10043 return false;
10044 }
10045 return true;
10046 }
10047 return false;
10048
0c93c8a9 10049 case BUILT_IN_ADD_OVERFLOW:
10050 case BUILT_IN_SUB_OVERFLOW:
10051 case BUILT_IN_MUL_OVERFLOW:
10052 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10053 {
10054 unsigned i;
10055 for (i = 0; i < 2; i++)
10056 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10057 {
10058 error ("argument %u in call to function %qE does not have "
10059 "integral type", i + 1, fndecl);
10060 return false;
10061 }
10062 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10063 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10064 {
10065 error ("argument 3 in call to function %qE does not have "
10066 "pointer to integer type", fndecl);
10067 return false;
10068 }
10069 return true;
10070 }
10071 return false;
10072
d43cee80 10073 default:
10074 return true;
10075 }
10076}
10077
860251be 10078/* Function to help qsort sort FIELD_DECLs by name order. */
10079
10080int
10081field_decl_cmp (const void *x_p, const void *y_p)
10082{
4fd61bc6 10083 const tree *const x = (const tree *const) x_p;
10084 const tree *const y = (const tree *const) y_p;
10085
860251be 10086 if (DECL_NAME (*x) == DECL_NAME (*y))
10087 /* A nontype is "greater" than a type. */
10088 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10089 if (DECL_NAME (*x) == NULL_TREE)
10090 return -1;
10091 if (DECL_NAME (*y) == NULL_TREE)
10092 return 1;
10093 if (DECL_NAME (*x) < DECL_NAME (*y))
10094 return -1;
10095 return 1;
10096}
10097
10098static struct {
10099 gt_pointer_operator new_value;
10100 void *cookie;
10101} resort_data;
10102
10103/* This routine compares two fields like field_decl_cmp but using the
10104pointer operator in resort_data. */
10105
10106static int
10107resort_field_decl_cmp (const void *x_p, const void *y_p)
10108{
4fd61bc6 10109 const tree *const x = (const tree *const) x_p;
10110 const tree *const y = (const tree *const) y_p;
860251be 10111
10112 if (DECL_NAME (*x) == DECL_NAME (*y))
10113 /* A nontype is "greater" than a type. */
10114 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10115 if (DECL_NAME (*x) == NULL_TREE)
10116 return -1;
10117 if (DECL_NAME (*y) == NULL_TREE)
10118 return 1;
10119 {
10120 tree d1 = DECL_NAME (*x);
10121 tree d2 = DECL_NAME (*y);
10122 resort_data.new_value (&d1, resort_data.cookie);
10123 resort_data.new_value (&d2, resort_data.cookie);
10124 if (d1 < d2)
10125 return -1;
10126 }
10127 return 1;
10128}
10129
10130/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10131
10132void
10133resort_sorted_fields (void *obj,
9a03a746 10134 void * ARG_UNUSED (orig_obj),
4ee9c684 10135 gt_pointer_operator new_value,
10136 void *cookie)
860251be 10137{
9a03a746 10138 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10139 resort_data.new_value = new_value;
10140 resort_data.cookie = cookie;
10141 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10142 resort_field_decl_cmp);
860251be 10143}
10144
209c9752 10145/* Subroutine of c_parse_error.
10146 Return the result of concatenating LHS and RHS. RHS is really
10147 a string literal, its first character is indicated by RHS_START and
cfee01e3 10148 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10149
10150 The caller is responsible for deleting the returned pointer. */
10151
10152static char *
10153catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10154{
10155 const int lhs_size = strlen (lhs);
10156 char *result = XNEWVEC (char, lhs_size + rhs_size);
10157 strncpy (result, lhs, lhs_size);
10158 strncpy (result + lhs_size, rhs_start, rhs_size);
10159 return result;
10160}
10161
380c6697 10162/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10163 TOKEN, which had the associated VALUE. */
10164
10165void
48e1416a 10166c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10167 tree value, unsigned char token_flags)
92b128ed 10168{
209c9752 10169#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10170
10171 char *message = NULL;
92b128ed 10172
ba99525e 10173 if (token_type == CPP_EOF)
380c6697 10174 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10175 else if (token_type == CPP_CHAR
10176 || token_type == CPP_WCHAR
ba99525e 10177 || token_type == CPP_CHAR16
10178 || token_type == CPP_CHAR32)
92b128ed 10179 {
f9ae6f95 10180 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10181 const char *prefix;
10182
ba99525e 10183 switch (token_type)
924bbf02 10184 {
10185 default:
10186 prefix = "";
10187 break;
10188 case CPP_WCHAR:
10189 prefix = "L";
10190 break;
10191 case CPP_CHAR16:
10192 prefix = "u";
10193 break;
10194 case CPP_CHAR32:
10195 prefix = "U";
10196 break;
10197 }
10198
92b128ed 10199 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10200 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10201 else
a0c938f0 10202 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10203
924bbf02 10204 error (message, prefix, val);
209c9752 10205 free (message);
10206 message = NULL;
92b128ed 10207 }
1898176c 10208 else if (token_type == CPP_CHAR_USERDEF
10209 || token_type == CPP_WCHAR_USERDEF
10210 || token_type == CPP_CHAR16_USERDEF
10211 || token_type == CPP_CHAR32_USERDEF)
10212 message = catenate_messages (gmsgid,
10213 " before user-defined character literal");
10214 else if (token_type == CPP_STRING_USERDEF
10215 || token_type == CPP_WSTRING_USERDEF
10216 || token_type == CPP_STRING16_USERDEF
10217 || token_type == CPP_STRING32_USERDEF
10218 || token_type == CPP_UTF8STRING_USERDEF)
10219 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10220 else if (token_type == CPP_STRING
10221 || token_type == CPP_WSTRING
ba99525e 10222 || token_type == CPP_STRING16
538ba11a 10223 || token_type == CPP_STRING32
10224 || token_type == CPP_UTF8STRING)
380c6697 10225 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10226 else if (token_type == CPP_NUMBER)
380c6697 10227 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10228 else if (token_type == CPP_NAME)
209c9752 10229 {
380c6697 10230 message = catenate_messages (gmsgid, " before %qE");
782858b8 10231 error (message, value);
209c9752 10232 free (message);
10233 message = NULL;
10234 }
ba99525e 10235 else if (token_type == CPP_PRAGMA)
b75b98aa 10236 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10237 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10238 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10239 else if (token_type == CPP_DECLTYPE)
10240 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10241 else if (token_type < N_TTYPES)
209c9752 10242 {
380c6697 10243 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10244 error (message, cpp_type2name (token_type, token_flags));
209c9752 10245 free (message);
10246 message = NULL;
10247 }
92b128ed 10248 else
380c6697 10249 error (gmsgid);
209c9752 10250
10251 if (message)
10252 {
10253 error (message);
10254 free (message);
10255 }
a0c938f0 10256#undef catenate_messages
92b128ed 10257}
10258
3a79f5da 10259/* Return the gcc option code associated with the reason for a cpp
10260 message, or 0 if none. */
10261
10262static int
10263c_option_controlling_cpp_error (int reason)
10264{
7ff8db31 10265 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10266
7ff8db31 10267 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10268 {
10269 if (entry->reason == reason)
10270 return entry->option_code;
10271 }
10272 return 0;
10273}
10274
7f5f3953 10275/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10276 preprocessor. The diagnostic is of type LEVEL, with REASON set
10277 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10278 LOCATION unless this is after lexing and the compiler's location
10279 should be used instead, with column number possibly overridden by
10280 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10281 the arguments. Returns true if a diagnostic was emitted, false
10282 otherwise. */
10283
10284bool
3a79f5da 10285c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10286 location_t location, unsigned int column_override,
10287 const char *msg, va_list *ap)
10288{
10289 diagnostic_info diagnostic;
10290 diagnostic_t dlevel;
5ae82d58 10291 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10292 bool ret;
10293
10294 switch (level)
10295 {
10296 case CPP_DL_WARNING_SYSHDR:
10297 if (flag_no_output)
10298 return false;
5ae82d58 10299 global_dc->dc_warn_system_headers = 1;
7f5f3953 10300 /* Fall through. */
10301 case CPP_DL_WARNING:
10302 if (flag_no_output)
10303 return false;
10304 dlevel = DK_WARNING;
10305 break;
10306 case CPP_DL_PEDWARN:
10307 if (flag_no_output && !flag_pedantic_errors)
10308 return false;
10309 dlevel = DK_PEDWARN;
10310 break;
10311 case CPP_DL_ERROR:
10312 dlevel = DK_ERROR;
10313 break;
10314 case CPP_DL_ICE:
10315 dlevel = DK_ICE;
10316 break;
10317 case CPP_DL_NOTE:
10318 dlevel = DK_NOTE;
10319 break;
ff903809 10320 case CPP_DL_FATAL:
10321 dlevel = DK_FATAL;
10322 break;
7f5f3953 10323 default:
10324 gcc_unreachable ();
10325 }
10326 if (done_lexing)
10327 location = input_location;
10328 diagnostic_set_info_translated (&diagnostic, msg, ap,
10329 location, dlevel);
10330 if (column_override)
10331 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10332 diagnostic_override_option_index (&diagnostic,
10333 c_option_controlling_cpp_error (reason));
7f5f3953 10334 ret = report_diagnostic (&diagnostic);
10335 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10336 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10337 return ret;
10338}
10339
624d37a6 10340/* Convert a character from the host to the target execution character
10341 set. cpplib handles this, mostly. */
10342
10343HOST_WIDE_INT
10344c_common_to_target_charset (HOST_WIDE_INT c)
10345{
10346 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10347 zero-extended under -fno-signed-char. cpplib insists that characters
10348 and character constants are always unsigned. Hence we must convert
10349 back and forth. */
10350 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10351
10352 uc = cpp_host_to_exec_charset (parse_in, uc);
10353
10354 if (flag_signed_char)
10355 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10356 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10357 else
10358 return uc;
10359}
10360
7549df0d 10361/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10362 references with an INDIRECT_REF of a constant at the bottom; much like the
10363 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10364
7549df0d 10365tree
10366fold_offsetof_1 (tree expr)
af28855b 10367{
af28855b 10368 tree base, off, t;
10369
10370 switch (TREE_CODE (expr))
10371 {
10372 case ERROR_MARK:
10373 return expr;
10374
6b11d2e3 10375 case VAR_DECL:
10376 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10377 return error_mark_node;
10378
d897f7c2 10379 case CALL_EXPR:
cf1a89a3 10380 case TARGET_EXPR:
d897f7c2 10381 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10382 return error_mark_node;
10383
d897f7c2 10384 case NOP_EXPR:
10385 case INDIRECT_REF:
7549df0d 10386 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10387 {
10388 error ("cannot apply %<offsetof%> to a non constant address");
10389 return error_mark_node;
10390 }
7549df0d 10391 return TREE_OPERAND (expr, 0);
d897f7c2 10392
af28855b 10393 case COMPONENT_REF:
7549df0d 10394 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10395 if (base == error_mark_node)
10396 return base;
10397
10398 t = TREE_OPERAND (expr, 1);
10399 if (DECL_C_BIT_FIELD (t))
10400 {
10401 error ("attempt to take address of bit-field structure "
782858b8 10402 "member %qD", t);
af28855b 10403 return error_mark_node;
10404 }
389dd41b 10405 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10406 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10407 / BITS_PER_UNIT));
af28855b 10408 break;
10409
10410 case ARRAY_REF:
7549df0d 10411 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10412 if (base == error_mark_node)
10413 return base;
10414
10415 t = TREE_OPERAND (expr, 1);
64ed018c 10416
10417 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10418 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10419 {
10420 tree upbound = array_ref_up_bound (expr);
10421 if (upbound != NULL_TREE
10422 && TREE_CODE (upbound) == INTEGER_CST
10423 && !tree_int_cst_equal (upbound,
10424 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10425 {
10426 upbound = size_binop (PLUS_EXPR, upbound,
10427 build_int_cst (TREE_TYPE (upbound), 1));
10428 if (tree_int_cst_lt (upbound, t))
10429 {
10430 tree v;
10431
10432 for (v = TREE_OPERAND (expr, 0);
10433 TREE_CODE (v) == COMPONENT_REF;
10434 v = TREE_OPERAND (v, 0))
10435 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10436 == RECORD_TYPE)
10437 {
1767a056 10438 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10439 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10440 if (TREE_CODE (fld_chain) == FIELD_DECL)
10441 break;
10442
10443 if (fld_chain)
10444 break;
10445 }
10446 /* Don't warn if the array might be considered a poor
10447 man's flexible array member with a very permissive
10448 definition thereof. */
10449 if (TREE_CODE (v) == ARRAY_REF
10450 || TREE_CODE (v) == COMPONENT_REF)
10451 warning (OPT_Warray_bounds,
10452 "index %E denotes an offset "
10453 "greater than size of %qT",
10454 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10455 }
10456 }
10457 }
7549df0d 10458
10459 t = convert (sizetype, t);
10460 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10461 break;
10462
ede90cc2 10463 case COMPOUND_EXPR:
10464 /* Handle static members of volatile structs. */
10465 t = TREE_OPERAND (expr, 1);
10466 gcc_assert (TREE_CODE (t) == VAR_DECL);
7549df0d 10467 return fold_offsetof_1 (t);
ede90cc2 10468
af28855b 10469 default:
231bd014 10470 gcc_unreachable ();
af28855b 10471 }
10472
7549df0d 10473 return fold_build_pointer_plus (base, off);
af28855b 10474}
10475
7549df0d 10476/* Likewise, but convert it to the return type of offsetof. */
10477
af28855b 10478tree
7549df0d 10479fold_offsetof (tree expr)
af28855b 10480{
7549df0d 10481 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10482}
10483
b9bdfa0b 10484/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10485 expression, because B will always be true. */
10486
10487void
10488warn_for_omitted_condop (location_t location, tree cond)
10489{
10490 if (truth_value_p (TREE_CODE (cond)))
10491 warning_at (location, OPT_Wparentheses,
10492 "the omitted middle operand in ?: will always be %<true%>, "
10493 "suggest explicit middle operand");
10494}
10495
a1f90215 10496/* Give an error for storing into ARG, which is 'const'. USE indicates
10497 how ARG was being used. */
10498
10499void
f2697631 10500readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10501{
10502 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10503 || use == lv_asm);
10504 /* Using this macro rather than (for example) arrays of messages
10505 ensures that all the format strings are checked at compile
10506 time. */
10507#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10508 : (use == lv_increment ? (I) \
10509 : (use == lv_decrement ? (D) : (AS))))
10510 if (TREE_CODE (arg) == COMPONENT_REF)
10511 {
10512 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10513 error_at (loc, READONLY_MSG (G_("assignment of member "
10514 "%qD in read-only object"),
10515 G_("increment of member "
10516 "%qD in read-only object"),
10517 G_("decrement of member "
10518 "%qD in read-only object"),
10519 G_("member %qD in read-only object "
10520 "used as %<asm%> output")),
10521 TREE_OPERAND (arg, 1));
a1f90215 10522 else
f2697631 10523 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10524 G_("increment of read-only member %qD"),
10525 G_("decrement of read-only member %qD"),
10526 G_("read-only member %qD used as %<asm%> output")),
10527 TREE_OPERAND (arg, 1));
a1f90215 10528 }
10529 else if (TREE_CODE (arg) == VAR_DECL)
f2697631 10530 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10531 G_("increment of read-only variable %qD"),
10532 G_("decrement of read-only variable %qD"),
10533 G_("read-only variable %qD used as %<asm%> output")),
10534 arg);
a1f90215 10535 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10536 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10537 G_("increment of read-only parameter %qD"),
10538 G_("decrement of read-only parameter %qD"),
10539 G_("read-only parameter %qD use as %<asm%> output")),
10540 arg);
a1f90215 10541 else if (TREE_CODE (arg) == RESULT_DECL)
10542 {
10543 gcc_assert (c_dialect_cxx ());
f2697631 10544 error_at (loc, READONLY_MSG (G_("assignment of "
10545 "read-only named return value %qD"),
10546 G_("increment of "
10547 "read-only named return value %qD"),
10548 G_("decrement of "
10549 "read-only named return value %qD"),
10550 G_("read-only named return value %qD "
10551 "used as %<asm%>output")),
10552 arg);
a1f90215 10553 }
10554 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10555 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10556 G_("increment of function %qD"),
10557 G_("decrement of function %qD"),
10558 G_("function %qD used as %<asm%> output")),
10559 arg);
a1f90215 10560 else
f2697631 10561 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10562 G_("increment of read-only location %qE"),
10563 G_("decrement of read-only location %qE"),
10564 G_("read-only location %qE used as %<asm%> output")),
10565 arg);
a1f90215 10566}
10567
e35976b1 10568/* Print an error message for an invalid lvalue. USE says
fdd84b77 10569 how the lvalue is being used and so selects the error message. LOC
10570 is the location for the error. */
ab6bb714 10571
e35976b1 10572void
fdd84b77 10573lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10574{
e35976b1 10575 switch (use)
ab6bb714 10576 {
e35976b1 10577 case lv_assign:
fdd84b77 10578 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10579 break;
10580 case lv_increment:
fdd84b77 10581 error_at (loc, "lvalue required as increment operand");
e35976b1 10582 break;
10583 case lv_decrement:
fdd84b77 10584 error_at (loc, "lvalue required as decrement operand");
e35976b1 10585 break;
10586 case lv_addressof:
fdd84b77 10587 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10588 break;
10589 case lv_asm:
fdd84b77 10590 error_at (loc, "lvalue required in asm statement");
e35976b1 10591 break;
10592 default:
10593 gcc_unreachable ();
ab6bb714 10594 }
ab6bb714 10595}
b1bbc8e5 10596
10597/* Print an error message for an invalid indirection of type TYPE.
10598 ERRSTRING is the name of the operator for the indirection. */
10599
10600void
10601invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10602{
10603 switch (errstring)
10604 {
10605 case RO_NULL:
10606 gcc_assert (c_dialect_cxx ());
10607 error_at (loc, "invalid type argument (have %qT)", type);
10608 break;
10609 case RO_ARRAY_INDEXING:
10610 error_at (loc,
10611 "invalid type argument of array indexing (have %qT)",
10612 type);
10613 break;
10614 case RO_UNARY_STAR:
10615 error_at (loc,
10616 "invalid type argument of unary %<*%> (have %qT)",
10617 type);
10618 break;
10619 case RO_ARROW:
10620 error_at (loc,
10621 "invalid type argument of %<->%> (have %qT)",
10622 type);
10623 break;
7354a89b 10624 case RO_ARROW_STAR:
10625 error_at (loc,
10626 "invalid type argument of %<->*%> (have %qT)",
10627 type);
10628 break;
b1bbc8e5 10629 case RO_IMPLICIT_CONVERSION:
10630 error_at (loc,
10631 "invalid type argument of implicit conversion (have %qT)",
10632 type);
10633 break;
10634 default:
10635 gcc_unreachable ();
10636 }
10637}
c271bdb2 10638\f
10639/* *PTYPE is an incomplete array. Complete it with a domain based on
10640 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10641 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10642 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10643
10644int
10645complete_array_type (tree *ptype, tree initial_value, bool do_default)
10646{
10647 tree maxindex, type, main_type, elt, unqual_elt;
10648 int failure = 0, quals;
6753bca0 10649 hashval_t hashcode = 0;
f5298614 10650 bool overflow_p = false;
c271bdb2 10651
10652 maxindex = size_zero_node;
10653 if (initial_value)
10654 {
10655 if (TREE_CODE (initial_value) == STRING_CST)
10656 {
10657 int eltsize
10658 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10659 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10660 }
10661 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10662 {
f1f41a6c 10663 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10664
f1f41a6c 10665 if (vec_safe_is_empty (v))
c271bdb2 10666 {
10667 if (pedantic)
10668 failure = 3;
7542c3b4 10669 maxindex = ssize_int (-1);
c271bdb2 10670 }
10671 else
10672 {
10673 tree curindex;
c75b4594 10674 unsigned HOST_WIDE_INT cnt;
10675 constructor_elt *ce;
cee43f7e 10676 bool fold_p = false;
c271bdb2 10677
f1f41a6c 10678 if ((*v)[0].index)
f5298614 10679 maxindex = (*v)[0].index, fold_p = true;
10680
c271bdb2 10681 curindex = maxindex;
10682
f1f41a6c 10683 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10684 {
cee43f7e 10685 bool curfold_p = false;
c75b4594 10686 if (ce->index)
cee43f7e 10687 curindex = ce->index, curfold_p = true;
c271bdb2 10688 else
cee43f7e 10689 {
10690 if (fold_p)
f5298614 10691 {
10692 /* Since we treat size types now as ordinary
10693 unsigned types, we need an explicit overflow
10694 check. */
10695 tree orig = curindex;
10696 curindex = fold_convert (sizetype, curindex);
10697 overflow_p |= tree_int_cst_lt (curindex, orig);
10698 }
389dd41b 10699 curindex = size_binop (PLUS_EXPR, curindex,
10700 size_one_node);
cee43f7e 10701 }
c271bdb2 10702 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10703 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10704 }
f5298614 10705 if (fold_p)
10706 {
10707 tree orig = maxindex;
10708 maxindex = fold_convert (sizetype, maxindex);
10709 overflow_p |= tree_int_cst_lt (maxindex, orig);
10710 }
c271bdb2 10711 }
10712 }
10713 else
10714 {
10715 /* Make an error message unless that happened already. */
10716 if (initial_value != error_mark_node)
10717 failure = 1;
10718 }
10719 }
10720 else
10721 {
10722 failure = 2;
10723 if (!do_default)
10724 return failure;
10725 }
10726
10727 type = *ptype;
10728 elt = TREE_TYPE (type);
10729 quals = TYPE_QUALS (strip_array_types (elt));
10730 if (quals == 0)
10731 unqual_elt = elt;
10732 else
6d5d708e 10733 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10734
10735 /* Using build_distinct_type_copy and modifying things afterward instead
10736 of using build_array_type to create a new type preserves all of the
10737 TYPE_LANG_FLAG_? bits that the front end may have set. */
10738 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10739 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10740 TYPE_DOMAIN (main_type)
10741 = build_range_type (TREE_TYPE (maxindex),
10742 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10743 layout_type (main_type);
10744
6753bca0 10745 /* Make sure we have the canonical MAIN_TYPE. */
10746 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10747 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10748 hashcode);
10749 main_type = type_hash_canon (hashcode, main_type);
10750
796735dc 10751 /* Fix the canonical type. */
10752 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10753 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10754 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10755 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10756 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10757 != TYPE_DOMAIN (main_type)))
48e1416a 10758 TYPE_CANONICAL (main_type)
796735dc 10759 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10760 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10761 else
10762 TYPE_CANONICAL (main_type) = main_type;
10763
c271bdb2 10764 if (quals == 0)
10765 type = main_type;
10766 else
10767 type = c_build_qualified_type (main_type, quals);
10768
4f5b8f2a 10769 if (COMPLETE_TYPE_P (type)
10770 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10771 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10772 {
10773 error ("size of array is too large");
10774 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10775 crash in tree_to_[su]hwi(). */
4f5b8f2a 10776 type = error_mark_node;
10777 }
10778
c271bdb2 10779 *ptype = type;
10780 return failure;
10781}
ab6bb714 10782
93426222 10783/* Like c_mark_addressable but don't check register qualifier. */
10784void
10785c_common_mark_addressable_vec (tree t)
10786{
10787 while (handled_component_p (t))
10788 t = TREE_OPERAND (t, 0);
10789 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10790 return;
10791 TREE_ADDRESSABLE (t) = 1;
10792}
10793
10794
b6a5fc45 10795\f
10796/* Used to help initialize the builtin-types.def table. When a type of
10797 the correct size doesn't exist, use error_mark_node instead of NULL.
10798 The later results in segfaults even when a decl using the type doesn't
10799 get invoked. */
10800
10801tree
10802builtin_type_for_size (int size, bool unsignedp)
10803{
a51edb4c 10804 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10805 return type ? type : error_mark_node;
10806}
10807
10808/* A helper function for resolve_overloaded_builtin in resolving the
10809 overloaded __sync_ builtins. Returns a positive power of 2 if the
10810 first operand of PARAMS is a pointer to a supported data type.
10811 Returns 0 if an error is encountered. */
10812
10813static int
f1f41a6c 10814sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 10815{
10816 tree type;
10817 int size;
10818
f1f41a6c 10819 if (!params)
b6a5fc45 10820 {
10821 error ("too few arguments to function %qE", function);
10822 return 0;
10823 }
10824
f1f41a6c 10825 type = TREE_TYPE ((*params)[0]);
0d284870 10826 if (TREE_CODE (type) == ARRAY_TYPE)
10827 {
10828 /* Force array-to-pointer decay for C++. */
10829 gcc_assert (c_dialect_cxx());
10830 (*params)[0] = default_conversion ((*params)[0]);
10831 type = TREE_TYPE ((*params)[0]);
10832 }
b6a5fc45 10833 if (TREE_CODE (type) != POINTER_TYPE)
10834 goto incompatible;
10835
10836 type = TREE_TYPE (type);
10837 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10838 goto incompatible;
10839
e913b5cd 10840 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10841 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10842 return size;
10843
10844 incompatible:
10845 error ("incompatible type for argument %d of %qE", 1, function);
10846 return 0;
10847}
10848
a0c938f0 10849/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10850 PARAMS to make arguments match up with those of FUNCTION. Drops
10851 the variadic arguments at the end. Returns false if some error
10852 was encountered; true on success. */
10853
10854static bool
1cd6e20d 10855sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10856 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10857{
d0af78c5 10858 function_args_iterator iter;
b6a5fc45 10859 tree ptype;
b9c74b4d 10860 unsigned int parmnum;
b6a5fc45 10861
d0af78c5 10862 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10863 /* We've declared the implementation functions to use "volatile void *"
10864 as the pointer parameter, so we shouldn't get any complaints from the
10865 call to check_function_arguments what ever type the user used. */
d0af78c5 10866 function_args_iter_next (&iter);
f1f41a6c 10867 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10868 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10869
10870 /* For the rest of the values, we need to cast these to FTYPE, so that we
10871 don't get warnings for passing pointer types, etc. */
b9c74b4d 10872 parmnum = 0;
d0af78c5 10873 while (1)
b6a5fc45 10874 {
d0af78c5 10875 tree val, arg_type;
10876
10877 arg_type = function_args_iter_cond (&iter);
10878 /* XXX void_type_node belies the abstraction. */
10879 if (arg_type == void_type_node)
10880 break;
b6a5fc45 10881
b9c74b4d 10882 ++parmnum;
f1f41a6c 10883 if (params->length () <= parmnum)
b6a5fc45 10884 {
1cd6e20d 10885 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10886 return false;
10887 }
10888
0f6a7cb7 10889 /* Only convert parameters if arg_type is unsigned integer type with
10890 new format sync routines, i.e. don't attempt to convert pointer
10891 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10892 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10893 kinds). */
10894 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10895 {
10896 /* Ideally for the first conversion we'd use convert_for_assignment
10897 so that we get warnings for anything that doesn't match the pointer
10898 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10899 val = (*params)[parmnum];
1cd6e20d 10900 val = convert (ptype, val);
10901 val = convert (arg_type, val);
f1f41a6c 10902 (*params)[parmnum] = val;
1cd6e20d 10903 }
b6a5fc45 10904
d0af78c5 10905 function_args_iter_next (&iter);
b6a5fc45 10906 }
10907
1cd6e20d 10908 /* __atomic routines are not variadic. */
f1f41a6c 10909 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10910 {
10911 error_at (loc, "too many arguments to function %qE", orig_function);
10912 return false;
10913 }
10914
b6a5fc45 10915 /* The definition of these primitives is variadic, with the remaining
10916 being "an optional list of variables protected by the memory barrier".
10917 No clue what that's supposed to mean, precisely, but we consider all
10918 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10919 params->truncate (parmnum + 1);
b6a5fc45 10920
10921 return true;
10922}
10923
a0c938f0 10924/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10925 RESULT to make it match the type of the first pointer argument in
10926 PARAMS. */
10927
10928static tree
1cd6e20d 10929sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10930{
b9c74b4d 10931 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10932 tree rtype = TREE_TYPE (result);
10080eac 10933 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10934
10935 /* New format doesn't require casting unless the types are the same size. */
10936 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10937 return convert (ptype, result);
10938 else
10939 return result;
10940}
10941
10942/* This function verifies the PARAMS to generic atomic FUNCTION.
10943 It returns the size if all the parameters are the same size, otherwise
10944 0 is returned if the parameters are invalid. */
10945
10946static int
f1f41a6c 10947get_atomic_generic_size (location_t loc, tree function,
10948 vec<tree, va_gc> *params)
1cd6e20d 10949{
10950 unsigned int n_param;
10951 unsigned int n_model;
10952 unsigned int x;
10953 int size_0;
10954 tree type_0;
10955
10956 /* Determine the parameter makeup. */
10957 switch (DECL_FUNCTION_CODE (function))
10958 {
10959 case BUILT_IN_ATOMIC_EXCHANGE:
10960 n_param = 4;
10961 n_model = 1;
10962 break;
10963 case BUILT_IN_ATOMIC_LOAD:
10964 case BUILT_IN_ATOMIC_STORE:
10965 n_param = 3;
10966 n_model = 1;
10967 break;
10968 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10969 n_param = 6;
10970 n_model = 2;
10971 break;
10972 default:
1d581089 10973 gcc_unreachable ();
1cd6e20d 10974 }
10975
f1f41a6c 10976 if (vec_safe_length (params) != n_param)
1cd6e20d 10977 {
10978 error_at (loc, "incorrect number of arguments to function %qE", function);
10979 return 0;
10980 }
10981
10982 /* Get type of first parameter, and determine its size. */
f1f41a6c 10983 type_0 = TREE_TYPE ((*params)[0]);
0d284870 10984 if (TREE_CODE (type_0) == ARRAY_TYPE)
10985 {
10986 /* Force array-to-pointer decay for C++. */
10987 gcc_assert (c_dialect_cxx());
10988 (*params)[0] = default_conversion ((*params)[0]);
10989 type_0 = TREE_TYPE ((*params)[0]);
10990 }
1d581089 10991 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10992 {
10993 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10994 function);
10995 return 0;
10996 }
10997
10998 /* Types must be compile time constant sizes. */
10999 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11000 {
1d581089 11001 error_at (loc,
11002 "argument 1 of %qE must be a pointer to a constant size type",
11003 function);
1cd6e20d 11004 return 0;
11005 }
1d581089 11006
e913b5cd 11007 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11008
1d581089 11009 /* Zero size objects are not allowed. */
11010 if (size_0 == 0)
11011 {
11012 error_at (loc,
11013 "argument 1 of %qE must be a pointer to a nonzero size object",
11014 function);
11015 return 0;
11016 }
11017
1cd6e20d 11018 /* Check each other parameter is a pointer and the same size. */
11019 for (x = 0; x < n_param - n_model; x++)
11020 {
11021 int size;
f1f41a6c 11022 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11023 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11024 if (n_param == 6 && x == 3)
11025 continue;
11026 if (!POINTER_TYPE_P (type))
11027 {
11028 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11029 function);
11030 return 0;
11031 }
1f6be080 11032 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11033 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11034 if (size != size_0)
11035 {
11036 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11037 function);
11038 return 0;
11039 }
11040 }
11041
11042 /* Check memory model parameters for validity. */
11043 for (x = n_param - n_model ; x < n_param; x++)
11044 {
f1f41a6c 11045 tree p = (*params)[x];
1cd6e20d 11046 if (TREE_CODE (p) == INTEGER_CST)
11047 {
e913b5cd 11048 int i = tree_to_uhwi (p);
a372f7ca 11049 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11050 {
11051 warning_at (loc, OPT_Winvalid_memory_model,
11052 "invalid memory model argument %d of %qE", x + 1,
11053 function);
1cd6e20d 11054 }
11055 }
11056 else
11057 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11058 {
11059 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11060 function);
11061 return 0;
11062 }
11063 }
11064
11065 return size_0;
11066}
11067
11068
11069/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11070 at the beginning of the parameter list PARAMS representing the size of the
11071 objects. This is to match the library ABI requirement. LOC is the location
11072 of the function call.
11073 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11074 returned to allow the external call to be constructed. */
11075
11076static tree
11077add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11078 vec<tree, va_gc> *params)
1cd6e20d 11079{
11080 tree size_node;
11081
11082 /* Insert a SIZE_T parameter as the first param. If there isn't
11083 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11084 if (!params->space (1))
1cd6e20d 11085 {
11086 unsigned int z, len;
f1f41a6c 11087 vec<tree, va_gc> *v;
1cd6e20d 11088 tree f;
11089
f1f41a6c 11090 len = params->length ();
11091 vec_alloc (v, len + 1);
5a672e62 11092 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11093 for (z = 0; z < len; z++)
f1f41a6c 11094 v->quick_push ((*params)[z]);
ec761d5a 11095 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11096 vec_free (v);
1cd6e20d 11097 return f;
11098 }
11099
11100 /* Add the size parameter and leave as a function call for processing. */
11101 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11102 params->quick_insert (0, size_node);
1cd6e20d 11103 return NULL_TREE;
11104}
11105
11106
a056826c 11107/* Return whether atomic operations for naturally aligned N-byte
11108 arguments are supported, whether inline or through libatomic. */
11109static bool
11110atomic_size_supported_p (int n)
11111{
11112 switch (n)
11113 {
11114 case 1:
11115 case 2:
11116 case 4:
11117 case 8:
11118 return true;
11119
11120 case 16:
11121 return targetm.scalar_mode_supported_p (TImode);
11122
11123 default:
11124 return false;
11125 }
11126}
11127
1cd6e20d 11128/* This will process an __atomic_exchange function call, determine whether it
11129 needs to be mapped to the _N variation, or turned into a library call.
11130 LOC is the location of the builtin call.
11131 FUNCTION is the DECL that has been invoked;
11132 PARAMS is the argument list for the call. The return value is non-null
11133 TRUE is returned if it is translated into the proper format for a call to the
11134 external library, and NEW_RETURN is set the tree for that function.
11135 FALSE is returned if processing for the _N variation is required, and
11136 NEW_RETURN is set to the the return value the result is copied into. */
11137static bool
11138resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11139 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11140{
11141 tree p0, p1, p2, p3;
11142 tree I_type, I_type_ptr;
11143 int n = get_atomic_generic_size (loc, function, params);
11144
1d581089 11145 /* Size of 0 is an error condition. */
11146 if (n == 0)
11147 {
11148 *new_return = error_mark_node;
11149 return true;
11150 }
11151
1cd6e20d 11152 /* If not a lock-free size, change to the library generic format. */
a056826c 11153 if (!atomic_size_supported_p (n))
1cd6e20d 11154 {
11155 *new_return = add_atomic_size_parameter (n, loc, function, params);
11156 return true;
11157 }
11158
11159 /* Otherwise there is a lockfree match, transform the call from:
11160 void fn(T* mem, T* desired, T* return, model)
11161 into
11162 *return = (T) (fn (In* mem, (In) *desired, model)) */
11163
f1f41a6c 11164 p0 = (*params)[0];
11165 p1 = (*params)[1];
11166 p2 = (*params)[2];
11167 p3 = (*params)[3];
1cd6e20d 11168
11169 /* Create pointer to appropriate size. */
11170 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11171 I_type_ptr = build_pointer_type (I_type);
11172
11173 /* Convert object pointer to required type. */
11174 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11175 (*params)[0] = p0;
1cd6e20d 11176 /* Convert new value to required type, and dereference it. */
11177 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11178 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11179 (*params)[1] = p1;
1cd6e20d 11180
11181 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11182 (*params)[2] = p3;
11183 params->truncate (3);
1cd6e20d 11184
11185 /* Convert return pointer and dereference it for later assignment. */
11186 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11187
11188 return false;
b6a5fc45 11189}
11190
1cd6e20d 11191
11192/* This will process an __atomic_compare_exchange function call, determine
11193 whether it needs to be mapped to the _N variation, or turned into a lib call.
11194 LOC is the location of the builtin call.
11195 FUNCTION is the DECL that has been invoked;
11196 PARAMS is the argument list for the call. The return value is non-null
11197 TRUE is returned if it is translated into the proper format for a call to the
11198 external library, and NEW_RETURN is set the tree for that function.
11199 FALSE is returned if processing for the _N variation is required. */
11200
11201static bool
11202resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11203 vec<tree, va_gc> *params,
1cd6e20d 11204 tree *new_return)
11205{
11206 tree p0, p1, p2;
11207 tree I_type, I_type_ptr;
11208 int n = get_atomic_generic_size (loc, function, params);
11209
1d581089 11210 /* Size of 0 is an error condition. */
11211 if (n == 0)
11212 {
11213 *new_return = error_mark_node;
11214 return true;
11215 }
11216
1cd6e20d 11217 /* If not a lock-free size, change to the library generic format. */
a056826c 11218 if (!atomic_size_supported_p (n))
1cd6e20d 11219 {
11220 /* The library generic format does not have the weak parameter, so
11221 remove it from the param list. Since a parameter has been removed,
11222 we can be sure that there is room for the SIZE_T parameter, meaning
11223 there will not be a recursive rebuilding of the parameter list, so
11224 there is no danger this will be done twice. */
11225 if (n > 0)
11226 {
f1f41a6c 11227 (*params)[3] = (*params)[4];
11228 (*params)[4] = (*params)[5];
11229 params->truncate (5);
1cd6e20d 11230 }
11231 *new_return = add_atomic_size_parameter (n, loc, function, params);
11232 return true;
11233 }
11234
11235 /* Otherwise, there is a match, so the call needs to be transformed from:
11236 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11237 into
11238 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11239
f1f41a6c 11240 p0 = (*params)[0];
11241 p1 = (*params)[1];
11242 p2 = (*params)[2];
1cd6e20d 11243
11244 /* Create pointer to appropriate size. */
11245 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11246 I_type_ptr = build_pointer_type (I_type);
11247
11248 /* Convert object pointer to required type. */
11249 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11250 (*params)[0] = p0;
1cd6e20d 11251
11252 /* Convert expected pointer to required type. */
11253 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11254 (*params)[1] = p1;
1cd6e20d 11255
11256 /* Convert desired value to required type, and dereference it. */
11257 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11258 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11259 (*params)[2] = p2;
1cd6e20d 11260
11261 /* The rest of the parameters are fine. NULL means no special return value
11262 processing.*/
11263 *new_return = NULL;
11264 return false;
11265}
11266
11267
11268/* This will process an __atomic_load function call, determine whether it
11269 needs to be mapped to the _N variation, or turned into a library call.
11270 LOC is the location of the builtin call.
11271 FUNCTION is the DECL that has been invoked;
11272 PARAMS is the argument list for the call. The return value is non-null
11273 TRUE is returned if it is translated into the proper format for a call to the
11274 external library, and NEW_RETURN is set the tree for that function.
11275 FALSE is returned if processing for the _N variation is required, and
11276 NEW_RETURN is set to the the return value the result is copied into. */
11277
11278static bool
11279resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11280 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11281{
11282 tree p0, p1, p2;
11283 tree I_type, I_type_ptr;
11284 int n = get_atomic_generic_size (loc, function, params);
11285
1d581089 11286 /* Size of 0 is an error condition. */
11287 if (n == 0)
11288 {
11289 *new_return = error_mark_node;
11290 return true;
11291 }
11292
1cd6e20d 11293 /* If not a lock-free size, change to the library generic format. */
a056826c 11294 if (!atomic_size_supported_p (n))
1cd6e20d 11295 {
11296 *new_return = add_atomic_size_parameter (n, loc, function, params);
11297 return true;
11298 }
11299
11300 /* Otherwise, there is a match, so the call needs to be transformed from:
11301 void fn(T* mem, T* return, model)
11302 into
11303 *return = (T) (fn ((In *) mem, model)) */
11304
f1f41a6c 11305 p0 = (*params)[0];
11306 p1 = (*params)[1];
11307 p2 = (*params)[2];
1cd6e20d 11308
11309 /* Create pointer to appropriate size. */
11310 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11311 I_type_ptr = build_pointer_type (I_type);
11312
11313 /* Convert object pointer to required type. */
11314 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11315 (*params)[0] = p0;
1cd6e20d 11316
11317 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11318 (*params)[1] = p2;
11319 params->truncate (2);
1cd6e20d 11320
11321 /* Convert return pointer and dereference it for later assignment. */
11322 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11323
11324 return false;
11325}
11326
11327
11328/* This will process an __atomic_store function call, determine whether it
11329 needs to be mapped to the _N variation, or turned into a library call.
11330 LOC is the location of the builtin call.
11331 FUNCTION is the DECL that has been invoked;
11332 PARAMS is the argument list for the call. The return value is non-null
11333 TRUE is returned if it is translated into the proper format for a call to the
11334 external library, and NEW_RETURN is set the tree for that function.
11335 FALSE is returned if processing for the _N variation is required, and
11336 NEW_RETURN is set to the the return value the result is copied into. */
11337
11338static bool
11339resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11340 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11341{
11342 tree p0, p1;
11343 tree I_type, I_type_ptr;
11344 int n = get_atomic_generic_size (loc, function, params);
11345
1d581089 11346 /* Size of 0 is an error condition. */
11347 if (n == 0)
11348 {
11349 *new_return = error_mark_node;
11350 return true;
11351 }
11352
1cd6e20d 11353 /* If not a lock-free size, change to the library generic format. */
a056826c 11354 if (!atomic_size_supported_p (n))
1cd6e20d 11355 {
11356 *new_return = add_atomic_size_parameter (n, loc, function, params);
11357 return true;
11358 }
11359
11360 /* Otherwise, there is a match, so the call needs to be transformed from:
11361 void fn(T* mem, T* value, model)
11362 into
11363 fn ((In *) mem, (In) *value, model) */
11364
f1f41a6c 11365 p0 = (*params)[0];
11366 p1 = (*params)[1];
1cd6e20d 11367
11368 /* Create pointer to appropriate size. */
11369 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11370 I_type_ptr = build_pointer_type (I_type);
11371
11372 /* Convert object pointer to required type. */
11373 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11374 (*params)[0] = p0;
1cd6e20d 11375
11376 /* Convert new value to required type, and dereference it. */
11377 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11378 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11379 (*params)[1] = p1;
1cd6e20d 11380
11381 /* The memory model is in the right spot already. Return is void. */
11382 *new_return = NULL_TREE;
11383
11384 return false;
11385}
11386
11387
b6a5fc45 11388/* Some builtin functions are placeholders for other expressions. This
11389 function should be called immediately after parsing the call expression
11390 before surrounding code has committed to the type of the expression.
11391
e60a6f7b 11392 LOC is the location of the builtin call.
11393
b6a5fc45 11394 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11395 PARAMS is the argument list for the call. The return value is non-null
11396 when expansion is complete, and null if normal processing should
11397 continue. */
11398
11399tree
f1f41a6c 11400resolve_overloaded_builtin (location_t loc, tree function,
11401 vec<tree, va_gc> *params)
b6a5fc45 11402{
11403 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11404 bool orig_format = true;
11405 tree new_return = NULL_TREE;
11406
65441f6f 11407 switch (DECL_BUILT_IN_CLASS (function))
11408 {
11409 case BUILT_IN_NORMAL:
11410 break;
11411 case BUILT_IN_MD:
11412 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11413 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11414 else
a0c938f0 11415 return NULL_TREE;
65441f6f 11416 default:
11417 return NULL_TREE;
11418 }
a0c938f0 11419
65441f6f 11420 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11421 switch (orig_code)
11422 {
1cd6e20d 11423 case BUILT_IN_ATOMIC_EXCHANGE:
11424 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11425 case BUILT_IN_ATOMIC_LOAD:
11426 case BUILT_IN_ATOMIC_STORE:
11427 {
11428 /* Handle these 4 together so that they can fall through to the next
11429 case if the call is transformed to an _N variant. */
11430 switch (orig_code)
11431 {
11432 case BUILT_IN_ATOMIC_EXCHANGE:
11433 {
11434 if (resolve_overloaded_atomic_exchange (loc, function, params,
11435 &new_return))
11436 return new_return;
11437 /* Change to the _N variant. */
11438 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11439 break;
11440 }
11441
11442 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11443 {
11444 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11445 params,
11446 &new_return))
11447 return new_return;
11448 /* Change to the _N variant. */
11449 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11450 break;
11451 }
11452 case BUILT_IN_ATOMIC_LOAD:
11453 {
11454 if (resolve_overloaded_atomic_load (loc, function, params,
11455 &new_return))
11456 return new_return;
11457 /* Change to the _N variant. */
11458 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11459 break;
11460 }
11461 case BUILT_IN_ATOMIC_STORE:
11462 {
11463 if (resolve_overloaded_atomic_store (loc, function, params,
11464 &new_return))
11465 return new_return;
11466 /* Change to the _N variant. */
11467 orig_code = BUILT_IN_ATOMIC_STORE_N;
11468 break;
11469 }
11470 default:
11471 gcc_unreachable ();
11472 }
11473 /* Fallthrough to the normal processing. */
11474 }
11475 case BUILT_IN_ATOMIC_EXCHANGE_N:
11476 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11477 case BUILT_IN_ATOMIC_LOAD_N:
11478 case BUILT_IN_ATOMIC_STORE_N:
11479 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11480 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11481 case BUILT_IN_ATOMIC_AND_FETCH_N:
11482 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11483 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11484 case BUILT_IN_ATOMIC_OR_FETCH_N:
11485 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11486 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11487 case BUILT_IN_ATOMIC_FETCH_AND_N:
11488 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11489 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11490 case BUILT_IN_ATOMIC_FETCH_OR_N:
11491 {
11492 orig_format = false;
11493 /* Fallthru for parameter processing. */
11494 }
2797f13a 11495 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11496 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11497 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11498 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11499 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11500 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11501 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11502 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11503 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11504 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11505 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11506 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11507 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11508 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11509 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11510 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11511 {
11512 int n = sync_resolve_size (function, params);
b9c74b4d 11513 tree new_function, first_param, result;
b9a16870 11514 enum built_in_function fncode;
b6a5fc45 11515
11516 if (n == 0)
11517 return error_mark_node;
11518
b9a16870 11519 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11520 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11521 if (!sync_resolve_params (loc, function, new_function, params,
11522 orig_format))
b6a5fc45 11523 return error_mark_node;
11524
f1f41a6c 11525 first_param = (*params)[0];
ec761d5a 11526 result = build_function_call_vec (loc, vNULL, new_function, params,
11527 NULL);
1cd6e20d 11528 if (result == error_mark_node)
11529 return result;
2797f13a 11530 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11531 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11532 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11533 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11534
1cd6e20d 11535 /* If new_return is set, assign function to that expr and cast the
11536 result to void since the generic interface returned void. */
11537 if (new_return)
11538 {
11539 /* Cast function result from I{1,2,4,8,16} to the required type. */
11540 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11541 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11542 result);
11543 TREE_SIDE_EFFECTS (result) = 1;
11544 protected_set_expr_location (result, loc);
11545 result = convert (void_type_node, result);
11546 }
b6a5fc45 11547 return result;
11548 }
11549
11550 default:
65441f6f 11551 return NULL_TREE;
b6a5fc45 11552 }
11553}
11554
73437615 11555/* vector_types_compatible_elements_p is used in type checks of vectors
11556 values used as operands of binary operators. Where it returns true, and
11557 the other checks of the caller succeed (being vector types in he first
11558 place, and matching number of elements), we can just treat the types
11559 as essentially the same.
11560 Contrast with vector_targets_convertible_p, which is used for vector
11561 pointer types, and vector_types_convertible_p, which will allow
11562 language-specific matches under the control of flag_lax_vector_conversions,
11563 and might still require a conversion. */
11564/* True if vector types T1 and T2 can be inputs to the same binary
11565 operator without conversion.
11566 We don't check the overall vector size here because some of our callers
11567 want to give different error messages when the vectors are compatible
11568 except for the element count. */
11569
491255f5 11570bool
73437615 11571vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11572{
73437615 11573 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11574 t1 = TREE_TYPE (t1);
11575 t2 = TREE_TYPE (t2);
11576
491255f5 11577 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11578
9421ebb9 11579 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11580 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11581 || c2 == FIXED_POINT_TYPE));
491255f5 11582
73437615 11583 t1 = c_common_signed_type (t1);
11584 t2 = c_common_signed_type (t2);
491255f5 11585 /* Equality works here because c_common_signed_type uses
11586 TYPE_MAIN_VARIANT. */
73437615 11587 if (t1 == t2)
11588 return true;
11589 if (opaque && c1 == c2
11590 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11591 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11592 return true;
11593 return false;
491255f5 11594}
11595
be7350e7 11596/* Check for missing format attributes on function pointers. LTYPE is
11597 the new type or left-hand side type. RTYPE is the old type or
11598 right-hand side type. Returns TRUE if LTYPE is missing the desired
11599 attribute. */
11600
11601bool
11602check_missing_format_attribute (tree ltype, tree rtype)
11603{
11604 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11605 tree ra;
11606
11607 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11608 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11609 break;
11610 if (ra)
11611 {
11612 tree la;
11613 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11614 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11615 break;
11616 return !la;
11617 }
11618 else
11619 return false;
11620}
11621
2840aae4 11622/* Subscripting with type char is likely to lose on a machine where
11623 chars are signed. So warn on any machine, but optionally. Don't
11624 warn for unsigned char since that type is safe. Don't warn for
11625 signed char because anyone who uses that must have done so
11626 deliberately. Furthermore, we reduce the false positive load by
11627 warning only for non-constant value of type char. */
11628
11629void
92b63884 11630warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11631{
11632 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11633 && TREE_CODE (index) != INTEGER_CST)
92b63884 11634 warning_at (loc, OPT_Wchar_subscripts,
11635 "array subscript has type %<char%>");
2840aae4 11636}
11637
e534436e 11638/* Implement -Wparentheses for the unexpected C precedence rules, to
11639 cover cases like x + y << z which readers are likely to
11640 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11641 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11642 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11643 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11644 expression was not formed using a binary or unary operator, or it
11645 was enclosed in parentheses. */
e534436e 11646
11647void
b0e7825e 11648warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11649 enum tree_code code_left, tree arg_left,
82012ffe 11650 enum tree_code code_right, tree arg_right)
e534436e 11651{
11652 if (!warn_parentheses)
11653 return;
11654
82012ffe 11655 /* This macro tests that the expression ARG with original tree code
11656 CODE appears to be a boolean expression. or the result of folding a
11657 boolean expression. */
11658#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11659 (truth_value_p (TREE_CODE (ARG)) \
11660 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11661 /* Folding may create 0 or 1 integers from other expressions. */ \
11662 || ((CODE) != INTEGER_CST \
11663 && (integer_onep (ARG) || integer_zerop (ARG))))
11664
48e1416a 11665 switch (code)
e534436e 11666 {
82012ffe 11667 case LSHIFT_EXPR:
b0e7825e 11668 if (code_left == PLUS_EXPR)
11669 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11670 "suggest parentheses around %<+%> inside %<<<%>");
11671 else if (code_right == PLUS_EXPR)
11672 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11673 "suggest parentheses around %<+%> inside %<<<%>");
11674 else if (code_left == MINUS_EXPR)
11675 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11676 "suggest parentheses around %<-%> inside %<<<%>");
11677 else if (code_right == MINUS_EXPR)
11678 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11679 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11680 return;
e534436e 11681
82012ffe 11682 case RSHIFT_EXPR:
b0e7825e 11683 if (code_left == PLUS_EXPR)
11684 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11685 "suggest parentheses around %<+%> inside %<>>%>");
11686 else if (code_right == PLUS_EXPR)
11687 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11688 "suggest parentheses around %<+%> inside %<>>%>");
11689 else if (code_left == MINUS_EXPR)
11690 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11691 "suggest parentheses around %<-%> inside %<>>%>");
11692 else if (code_right == MINUS_EXPR)
11693 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11694 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11695 return;
e534436e 11696
82012ffe 11697 case TRUTH_ORIF_EXPR:
b0e7825e 11698 if (code_left == TRUTH_ANDIF_EXPR)
11699 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11700 "suggest parentheses around %<&&%> within %<||%>");
11701 else if (code_right == TRUTH_ANDIF_EXPR)
11702 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11703 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11704 return;
11705
11706 case BIT_IOR_EXPR:
e534436e 11707 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11708 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11709 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11710 "suggest parentheses around arithmetic in operand of %<|%>");
11711 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11712 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11714 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11715 /* Check cases like x|y==z */
b0e7825e 11716 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11717 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11718 "suggest parentheses around comparison in operand of %<|%>");
11719 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11720 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11721 "suggest parentheses around comparison in operand of %<|%>");
11722 /* Check cases like !x | y */
11723 else if (code_left == TRUTH_NOT_EXPR
11724 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11725 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11726 "suggest parentheses around operand of "
11727 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11728 return;
e534436e 11729
82012ffe 11730 case BIT_XOR_EXPR:
e534436e 11731 if (code_left == BIT_AND_EXPR
b0e7825e 11732 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11733 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11734 "suggest parentheses around arithmetic in operand of %<^%>");
11735 else if (code_right == BIT_AND_EXPR
11736 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11737 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11738 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11739 /* Check cases like x^y==z */
b0e7825e 11740 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11741 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11742 "suggest parentheses around comparison in operand of %<^%>");
11743 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11744 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11745 "suggest parentheses around comparison in operand of %<^%>");
11746 return;
e534436e 11747
82012ffe 11748 case BIT_AND_EXPR:
b0e7825e 11749 if (code_left == PLUS_EXPR)
11750 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11751 "suggest parentheses around %<+%> in operand of %<&%>");
11752 else if (code_right == PLUS_EXPR)
11753 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11754 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11755 else if (code_left == MINUS_EXPR)
11756 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11757 "suggest parentheses around %<-%> in operand of %<&%>");
11758 else if (code_right == MINUS_EXPR)
11759 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11760 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11761 /* Check cases like x&y==z */
b0e7825e 11762 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11763 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11764 "suggest parentheses around comparison in operand of %<&%>");
11765 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11766 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11767 "suggest parentheses around comparison in operand of %<&%>");
11768 /* Check cases like !x & y */
11769 else if (code_left == TRUTH_NOT_EXPR
11770 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11771 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11772 "suggest parentheses around operand of "
11773 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11774 return;
e534436e 11775
82012ffe 11776 case EQ_EXPR:
b0e7825e 11777 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11778 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11779 "suggest parentheses around comparison in operand of %<==%>");
11780 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11781 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11782 "suggest parentheses around comparison in operand of %<==%>");
11783 return;
11784 case NE_EXPR:
b0e7825e 11785 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11786 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11787 "suggest parentheses around comparison in operand of %<!=%>");
11788 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11789 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11790 "suggest parentheses around comparison in operand of %<!=%>");
11791 return;
11792
11793 default:
b0e7825e 11794 if (TREE_CODE_CLASS (code) == tcc_comparison)
11795 {
11796 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11797 && code_left != NE_EXPR && code_left != EQ_EXPR
11798 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11799 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11800 "comparisons like %<X<=Y<=Z%> do not "
11801 "have their mathematical meaning");
11802 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11803 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11804 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11805 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11806 "comparisons like %<X<=Y<=Z%> do not "
11807 "have their mathematical meaning");
11808 }
82012ffe 11809 return;
6ce0c450 11810 }
82012ffe 11811#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11812}
11813
92fccaaa 11814/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11815
11816void
11817warn_for_unused_label (tree label)
11818{
11819 if (!TREE_USED (label))
11820 {
11821 if (DECL_INITIAL (label))
11822 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11823 else
11824 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11825 }
11826}
2840aae4 11827
b6889cb0 11828/* Warn for division by zero according to the value of DIVISOR. LOC
11829 is the location of the division operator. */
f092582b 11830
11831void
b6889cb0 11832warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11833{
9421ebb9 11834 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11835 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11836 floating-point type, since we consider 0.0/0.0 a valid way of
11837 generating a NaN. */
48d94ede 11838 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11839 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11840 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11841}
11842
13869a99 11843/* Subroutine of build_binary_op. Give warnings for comparisons
11844 between signed and unsigned quantities that may fail. Do the
11845 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11846 so that casts will be considered, but default promotions won't
8e70fb09 11847 be.
11848
11849 LOCATION is the location of the comparison operator.
13869a99 11850
11851 The arguments of this function map directly to local variables
11852 of build_binary_op. */
11853
48e1416a 11854void
8e70fb09 11855warn_for_sign_compare (location_t location,
48e1416a 11856 tree orig_op0, tree orig_op1,
11857 tree op0, tree op1,
13869a99 11858 tree result_type, enum tree_code resultcode)
11859{
11860 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11861 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11862 int unsignedp0, unsignedp1;
48e1416a 11863
13869a99 11864 /* In C++, check for comparison of different enum types. */
11865 if (c_dialect_cxx()
11866 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11867 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11868 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11869 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11870 {
8e70fb09 11871 warning_at (location,
11872 OPT_Wsign_compare, "comparison between types %qT and %qT",
11873 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11874 }
11875
11876 /* Do not warn if the comparison is being done in a signed type,
11877 since the signed type will only be chosen if it can represent
11878 all the values of the unsigned type. */
11879 if (!TYPE_UNSIGNED (result_type))
11880 /* OK */;
11881 /* Do not warn if both operands are unsigned. */
11882 else if (op0_signed == op1_signed)
11883 /* OK */;
11884 else
11885 {
895b662f 11886 tree sop, uop, base_type;
13869a99 11887 bool ovf;
895b662f 11888
13869a99 11889 if (op0_signed)
11890 sop = orig_op0, uop = orig_op1;
48e1416a 11891 else
13869a99 11892 sop = orig_op1, uop = orig_op0;
11893
48e1416a 11894 STRIP_TYPE_NOPS (sop);
13869a99 11895 STRIP_TYPE_NOPS (uop);
895b662f 11896 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11897 ? TREE_TYPE (result_type) : result_type);
13869a99 11898
11899 /* Do not warn if the signed quantity is an unsuffixed integer
11900 literal (or some static constant expression involving such
11901 literals or a conditional expression involving such literals)
11902 and it is non-negative. */
11903 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11904 /* OK */;
11905 /* Do not warn if the comparison is an equality operation, the
11906 unsigned quantity is an integral constant, and it would fit
11907 in the result if the result were signed. */
11908 else if (TREE_CODE (uop) == INTEGER_CST
11909 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11910 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11911 /* OK */;
11912 /* In C, do not warn if the unsigned quantity is an enumeration
11913 constant and its maximum value would fit in the result if the
11914 result were signed. */
11915 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11916 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11917 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11918 c_common_signed_type (base_type)))
13869a99 11919 /* OK */;
48e1416a 11920 else
8e70fb09 11921 warning_at (location,
48e1416a 11922 OPT_Wsign_compare,
8e70fb09 11923 "comparison between signed and unsigned integer expressions");
13869a99 11924 }
48e1416a 11925
13869a99 11926 /* Warn if two unsigned values are being compared in a size larger
11927 than their original size, and one (and only one) is the result of
11928 a `~' operator. This comparison will always fail.
48e1416a 11929
13869a99 11930 Also warn if one operand is a constant, and the constant does not
11931 have all bits set that are set in the ~ operand when it is
11932 extended. */
11933
7f506bca 11934 op0 = c_common_get_narrower (op0, &unsignedp0);
11935 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11936
13869a99 11937 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11938 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11939 {
11940 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11941 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11942 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11943 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11944
e913b5cd 11945 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11946 {
11947 tree primop;
11948 HOST_WIDE_INT constant, mask;
11949 int unsignedp;
11950 unsigned int bits;
48e1416a 11951
e913b5cd 11952 if (tree_fits_shwi_p (op0))
13869a99 11953 {
11954 primop = op1;
11955 unsignedp = unsignedp1;
e913b5cd 11956 constant = tree_to_shwi (op0);
13869a99 11957 }
11958 else
11959 {
11960 primop = op0;
11961 unsignedp = unsignedp0;
e913b5cd 11962 constant = tree_to_shwi (op1);
13869a99 11963 }
48e1416a 11964
13869a99 11965 bits = TYPE_PRECISION (TREE_TYPE (primop));
11966 if (bits < TYPE_PRECISION (result_type)
11967 && bits < HOST_BITS_PER_LONG && unsignedp)
11968 {
11969 mask = (~ (HOST_WIDE_INT) 0) << bits;
11970 if ((mask & constant) != mask)
11971 {
11972 if (constant == 0)
76fdceeb 11973 warning_at (location, OPT_Wsign_compare,
11974 "promoted ~unsigned is always non-zero");
13869a99 11975 else
48e1416a 11976 warning_at (location, OPT_Wsign_compare,
8e70fb09 11977 "comparison of promoted ~unsigned with constant");
13869a99 11978 }
11979 }
11980 }
11981 else if (unsignedp0 && unsignedp1
11982 && (TYPE_PRECISION (TREE_TYPE (op0))
11983 < TYPE_PRECISION (result_type))
11984 && (TYPE_PRECISION (TREE_TYPE (op1))
11985 < TYPE_PRECISION (result_type)))
8e70fb09 11986 warning_at (location, OPT_Wsign_compare,
13869a99 11987 "comparison of promoted ~unsigned with unsigned");
11988 }
11989}
11990
5ba33bf4 11991/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11992 type via c_common_type. If -Wdouble-promotion is in use, and the
11993 conditions for warning have been met, issue a warning. GMSGID is
11994 the warning message. It must have two %T specifiers for the type
11995 that was converted (generally "float") and the type to which it was
11996 converted (generally "double), respectively. LOC is the location
11997 to which the awrning should refer. */
11998
11999void
12000do_warn_double_promotion (tree result_type, tree type1, tree type2,
12001 const char *gmsgid, location_t loc)
12002{
12003 tree source_type;
12004
12005 if (!warn_double_promotion)
12006 return;
12007 /* If the conversion will not occur at run-time, there is no need to
12008 warn about it. */
12009 if (c_inhibit_evaluation_warnings)
12010 return;
12011 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12012 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12013 return;
12014 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12015 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12016 source_type = type1;
12017 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12018 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12019 source_type = type2;
12020 else
12021 return;
12022 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12023}
12024
41771881 12025/* Setup a TYPE_DECL node as a typedef representation.
12026
12027 X is a TYPE_DECL for a typedef statement. Create a brand new
12028 ..._TYPE node (which will be just a variant of the existing
12029 ..._TYPE node with identical properties) and then install X
12030 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12031
12032 The whole point here is to end up with a situation where each
12033 and every ..._TYPE node the compiler creates will be uniquely
12034 associated with AT MOST one node representing a typedef name.
12035 This way, even though the compiler substitutes corresponding
12036 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12037 early on, later parts of the compiler can always do the reverse
12038 translation and get back the corresponding typedef name. For
12039 example, given:
ab2c1de8 12040
41771881 12041 typedef struct S MY_TYPE;
12042 MY_TYPE object;
12043
12044 Later parts of the compiler might only know that `object' was of
12045 type `struct S' if it were not for code just below. With this
12046 code however, later parts of the compiler see something like:
12047
12048 struct S' == struct S
12049 typedef struct S' MY_TYPE;
12050 struct S' object;
12051
12052 And they can then deduce (from the node for type struct S') that
12053 the original object declaration was:
12054
12055 MY_TYPE object;
12056
12057 Being able to do this is important for proper support of protoize,
12058 and also for generating precise symbolic debugging information
12059 which takes full account of the programmer's (typedef) vocabulary.
12060
12061 Obviously, we don't want to generate a duplicate ..._TYPE node if
12062 the TYPE_DECL node that we are now processing really represents a
12063 standard built-in type. */
12064
12065void
12066set_underlying_type (tree x)
12067{
12068 if (x == error_mark_node)
12069 return;
12070 if (DECL_IS_BUILTIN (x))
12071 {
12072 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12073 TYPE_NAME (TREE_TYPE (x)) = x;
12074 }
12075 else if (TREE_TYPE (x) != error_mark_node
12076 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12077 {
12078 tree tt = TREE_TYPE (x);
12079 DECL_ORIGINAL_TYPE (x) = tt;
12080 tt = build_variant_type_copy (tt);
12081 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12082 TYPE_NAME (tt) = x;
12083 TREE_USED (tt) = TREE_USED (x);
12084 TREE_TYPE (x) = tt;
12085 }
12086}
12087
1a4c44c5 12088/* Record the types used by the current global variable declaration
12089 being parsed, so that we can decide later to emit their debug info.
12090 Those types are in types_used_by_cur_var_decl, and we are going to
12091 store them in the types_used_by_vars_hash hash table.
12092 DECL is the declaration of the global variable that has been parsed. */
12093
12094void
12095record_types_used_by_current_var_decl (tree decl)
12096{
12097 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12098
f1f41a6c 12099 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12100 {
f1f41a6c 12101 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12102 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12103 }
12104}
12105
a4e3ffad 12106/* If DECL is a typedef that is declared in the current function,
12107 record it for the purpose of -Wunused-local-typedefs. */
12108
12109void
12110record_locally_defined_typedef (tree decl)
12111{
12112 struct c_language_function *l;
12113
12114 if (!warn_unused_local_typedefs
12115 || cfun == NULL
12116 /* if this is not a locally defined typedef then we are not
12117 interested. */
12118 || !is_typedef_decl (decl)
12119 || !decl_function_context (decl))
12120 return;
12121
12122 l = (struct c_language_function *) cfun->language;
f1f41a6c 12123 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12124}
12125
12126/* If T is a TYPE_DECL declared locally, mark it as used. */
12127
12128void
12129maybe_record_typedef_use (tree t)
12130{
12131 if (!is_typedef_decl (t))
12132 return;
12133
12134 TREE_USED (t) = true;
12135}
12136
12137/* Warn if there are some unused locally defined typedefs in the
12138 current function. */
12139
12140void
12141maybe_warn_unused_local_typedefs (void)
12142{
12143 int i;
12144 tree decl;
12145 /* The number of times we have emitted -Wunused-local-typedefs
12146 warnings. If this is different from errorcount, that means some
12147 unrelated errors have been issued. In which case, we'll avoid
12148 emitting "unused-local-typedefs" warnings. */
12149 static int unused_local_typedefs_warn_count;
12150 struct c_language_function *l;
12151
12152 if (cfun == NULL)
12153 return;
12154
12155 if ((l = (struct c_language_function *) cfun->language) == NULL)
12156 return;
12157
12158 if (warn_unused_local_typedefs
12159 && errorcount == unused_local_typedefs_warn_count)
12160 {
f1f41a6c 12161 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12162 if (!TREE_USED (decl))
12163 warning_at (DECL_SOURCE_LOCATION (decl),
12164 OPT_Wunused_local_typedefs,
12165 "typedef %qD locally defined but not used", decl);
12166 unused_local_typedefs_warn_count = errorcount;
12167 }
12168
f1f41a6c 12169 vec_free (l->local_typedefs);
a4e3ffad 12170}
12171
78bf4156 12172/* Warn about boolean expression compared with an integer value different
12173 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12174 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12175 are the operands of the comparison. The caller must ensure that
12176 either operand is a boolean expression. */
12177
12178void
12179maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12180 tree op1)
12181{
12182 if (TREE_CODE_CLASS (code) != tcc_comparison)
12183 return;
12184
12185 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12186 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12187 if (!cst)
12188 return;
12189
12190 if (!integer_zerop (cst) && !integer_onep (cst))
12191 {
a720ab1c 12192 int sign = (TREE_CODE (op0) == INTEGER_CST
12193 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12194 if (code == EQ_EXPR
12195 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12196 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12197 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12198 "with boolean expression is always false", cst);
12199 else
12200 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12201 "with boolean expression is always true", cst);
12202 }
a720ab1c 12203 else if (integer_zerop (cst) || integer_onep (cst))
12204 {
14744a16 12205 /* If the non-constant operand isn't of a boolean type, we
12206 don't want to warn here. */
12207 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12208 /* Handle booleans promoted to integers. */
12209 if (CONVERT_EXPR_P (noncst)
12210 && TREE_TYPE (noncst) == integer_type_node
12211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12212 /* Warn. */;
12213 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12214 && !truth_value_p (TREE_CODE (noncst)))
12215 return;
a720ab1c 12216 /* Do some magic to get the right diagnostics. */
12217 bool flag = TREE_CODE (op0) == INTEGER_CST;
12218 flag = integer_zerop (cst) ? flag : !flag;
12219 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12220 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12221 "with boolean expression is always true", cst);
12222 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12223 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12224 "with boolean expression is always false", cst);
12225 }
78bf4156 12226}
12227
f352a3fb 12228/* The C and C++ parsers both use vectors to hold function arguments.
12229 For efficiency, we keep a cache of unused vectors. This is the
12230 cache. */
12231
f1f41a6c 12232typedef vec<tree, va_gc> *tree_gc_vec;
12233static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12234
12235/* Return a new vector from the cache. If the cache is empty,
12236 allocate a new vector. These vectors are GC'ed, so it is OK if the
12237 pointer is not released.. */
12238
f1f41a6c 12239vec<tree, va_gc> *
f352a3fb 12240make_tree_vector (void)
12241{
f1f41a6c 12242 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12243 return tree_vector_cache->pop ();
f352a3fb 12244 else
12245 {
f1f41a6c 12246 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12247 that we always return a non-NULL value. The vector code uses
12248 4 when growing a NULL vector, so we do too. */
f1f41a6c 12249 vec<tree, va_gc> *v;
12250 vec_alloc (v, 4);
12251 return v;
f352a3fb 12252 }
12253}
12254
12255/* Release a vector of trees back to the cache. */
12256
12257void
f1f41a6c 12258release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12259{
12260 if (vec != NULL)
12261 {
f1f41a6c 12262 vec->truncate (0);
12263 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12264 }
12265}
12266
12267/* Get a new tree vector holding a single tree. */
12268
f1f41a6c 12269vec<tree, va_gc> *
f352a3fb 12270make_tree_vector_single (tree t)
12271{
f1f41a6c 12272 vec<tree, va_gc> *ret = make_tree_vector ();
12273 ret->quick_push (t);
f352a3fb 12274 return ret;
12275}
12276
c66c81be 12277/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12278
f1f41a6c 12279vec<tree, va_gc> *
c66c81be 12280make_tree_vector_from_list (tree list)
12281{
f1f41a6c 12282 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12283 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12284 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12285 return ret;
12286}
12287
f352a3fb 12288/* Get a new tree vector which is a copy of an existing one. */
12289
f1f41a6c 12290vec<tree, va_gc> *
12291make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12292{
f1f41a6c 12293 vec<tree, va_gc> *ret;
f352a3fb 12294 unsigned int ix;
12295 tree t;
12296
12297 ret = make_tree_vector ();
f1f41a6c 12298 vec_safe_reserve (ret, vec_safe_length (orig));
12299 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12300 ret->quick_push (t);
f352a3fb 12301 return ret;
12302}
12303
a9ffdd35 12304/* Return true if KEYWORD starts a type specifier. */
12305
12306bool
12307keyword_begins_type_specifier (enum rid keyword)
12308{
12309 switch (keyword)
12310 {
4fba5eb9 12311 case RID_AUTO_TYPE:
a9ffdd35 12312 case RID_INT:
12313 case RID_CHAR:
12314 case RID_FLOAT:
12315 case RID_DOUBLE:
12316 case RID_VOID:
a9ffdd35 12317 case RID_UNSIGNED:
12318 case RID_LONG:
12319 case RID_SHORT:
12320 case RID_SIGNED:
12321 case RID_DFLOAT32:
12322 case RID_DFLOAT64:
12323 case RID_DFLOAT128:
12324 case RID_FRACT:
12325 case RID_ACCUM:
12326 case RID_BOOL:
12327 case RID_WCHAR:
12328 case RID_CHAR16:
12329 case RID_CHAR32:
12330 case RID_SAT:
12331 case RID_COMPLEX:
12332 case RID_TYPEOF:
12333 case RID_STRUCT:
12334 case RID_CLASS:
12335 case RID_UNION:
12336 case RID_ENUM:
12337 return true;
12338 default:
9f75f026 12339 if (keyword >= RID_FIRST_INT_N
12340 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12341 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12342 return true;
a9ffdd35 12343 return false;
12344 }
12345}
12346
12347/* Return true if KEYWORD names a type qualifier. */
12348
12349bool
12350keyword_is_type_qualifier (enum rid keyword)
12351{
12352 switch (keyword)
12353 {
12354 case RID_CONST:
12355 case RID_VOLATILE:
12356 case RID_RESTRICT:
b560fabd 12357 case RID_ATOMIC:
a9ffdd35 12358 return true;
12359 default:
12360 return false;
12361 }
12362}
12363
12364/* Return true if KEYWORD names a storage class specifier.
12365
12366 RID_TYPEDEF is not included in this list despite `typedef' being
12367 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12368 such for syntactic convenience only. */
12369
12370bool
12371keyword_is_storage_class_specifier (enum rid keyword)
12372{
12373 switch (keyword)
12374 {
12375 case RID_STATIC:
12376 case RID_EXTERN:
12377 case RID_REGISTER:
12378 case RID_AUTO:
12379 case RID_MUTABLE:
12380 case RID_THREAD:
12381 return true;
12382 default:
12383 return false;
12384 }
12385}
12386
fad3f658 12387/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12388
12389static bool
12390keyword_is_function_specifier (enum rid keyword)
12391{
12392 switch (keyword)
12393 {
12394 case RID_INLINE:
985c6e3a 12395 case RID_NORETURN:
fad3f658 12396 case RID_VIRTUAL:
12397 case RID_EXPLICIT:
12398 return true;
12399 default:
12400 return false;
12401 }
12402}
12403
12404/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12405 declaration-specifier (C99 6.7). */
12406
12407bool
12408keyword_is_decl_specifier (enum rid keyword)
12409{
12410 if (keyword_is_storage_class_specifier (keyword)
12411 || keyword_is_type_qualifier (keyword)
12412 || keyword_is_function_specifier (keyword))
12413 return true;
12414
12415 switch (keyword)
12416 {
12417 case RID_TYPEDEF:
12418 case RID_FRIEND:
12419 case RID_CONSTEXPR:
12420 return true;
12421 default:
12422 return false;
12423 }
12424}
12425
9b88d08d 12426/* Initialize language-specific-bits of tree_contains_struct. */
12427
12428void
12429c_common_init_ts (void)
12430{
12431 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12432 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12433 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12434}
12435
244db24d 12436/* Build a user-defined numeric literal out of an integer constant type VALUE
12437 with identifier SUFFIX. */
12438
12439tree
324ca377 12440build_userdef_literal (tree suffix_id, tree value,
12441 enum overflow_type overflow, tree num_string)
244db24d 12442{
12443 tree literal = make_node (USERDEF_LITERAL);
12444 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12445 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12446 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12447 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12448 return literal;
12449}
12450
7059d45d 12451/* For vector[index], convert the vector to a
c61ef207 12452 pointer of the underlying type. Return true if the resulting
12453 ARRAY_REF should not be an lvalue. */
12454
12455bool
7059d45d 12456convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12457 tree *vecp, tree index)
7059d45d 12458{
c61ef207 12459 bool ret = false;
7059d45d 12460 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12461 {
12462 tree type = TREE_TYPE (*vecp);
12463 tree type1;
12464
c61ef207 12465 ret = !lvalue_p (*vecp);
7059d45d 12466 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12467 if (!tree_fits_uhwi_p (index)
aa59f000 12468 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12469 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12470
c61ef207 12471 if (ret)
12472 {
f9e245b2 12473 tree tmp = create_tmp_var_raw (type);
c61ef207 12474 DECL_SOURCE_LOCATION (tmp) = loc;
12475 *vecp = c_save_expr (*vecp);
12476 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12477 {
12478 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12479 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12480 *vecp
12481 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12482 *vecp, NULL_TREE, NULL_TREE),
12483 non_const);
12484 }
12485 else
12486 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12487 NULL_TREE, NULL_TREE);
12488 SET_EXPR_LOCATION (*vecp, loc);
12489 c_common_mark_addressable_vec (tmp);
12490 }
12491 else
12492 c_common_mark_addressable_vec (*vecp);
7059d45d 12493 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12494 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12495 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12496 if (!ref_all
12497 && !DECL_P (*vecp))
12498 {
12499 /* If the original vector isn't declared may_alias and it
12500 isn't a bare vector look if the subscripting would
12501 alias the vector we subscript, and if not, force ref-all. */
12502 alias_set_type vecset = get_alias_set (*vecp);
12503 alias_set_type sset = get_alias_set (type);
12504 if (!alias_sets_must_conflict_p (sset, vecset)
12505 && !alias_set_subset_of (sset, vecset))
12506 ref_all = true;
12507 }
12508 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12509 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12510 *vecp = convert (type, *vecp);
12511 }
c61ef207 12512 return ret;
7059d45d 12513}
12514
41ed701a 12515/* Determine which of the operands, if any, is a scalar that needs to be
12516 converted to a vector, for the range of operations. */
12517enum stv_conv
12518scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12519 bool complain)
12520{
12521 tree type0 = TREE_TYPE (op0);
12522 tree type1 = TREE_TYPE (op1);
12523 bool integer_only_op = false;
12524 enum stv_conv ret = stv_firstarg;
12525
12526 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12527 || TREE_CODE (type1) == VECTOR_TYPE);
12528 switch (code)
12529 {
12530 /* Most GENERIC binary expressions require homogeneous arguments.
12531 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12532 argument that is a vector and a second one that is a scalar, so
12533 we never return stv_secondarg for them. */
12534 case RSHIFT_EXPR:
12535 case LSHIFT_EXPR:
12536 if (TREE_CODE (type0) == INTEGER_TYPE
12537 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12538 {
22a75734 12539 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12540 {
12541 if (complain)
12542 error_at (loc, "conversion of scalar %qT to vector %qT "
12543 "involves truncation", type0, type1);
12544 return stv_error;
12545 }
12546 else
12547 return stv_firstarg;
12548 }
12549 break;
12550
12551 case BIT_IOR_EXPR:
12552 case BIT_XOR_EXPR:
12553 case BIT_AND_EXPR:
12554 integer_only_op = true;
12555 /* ... fall through ... */
12556
7b463b19 12557 case VEC_COND_EXPR:
12558
41ed701a 12559 case PLUS_EXPR:
12560 case MINUS_EXPR:
12561 case MULT_EXPR:
12562 case TRUNC_DIV_EXPR:
12563 case CEIL_DIV_EXPR:
12564 case FLOOR_DIV_EXPR:
12565 case ROUND_DIV_EXPR:
12566 case EXACT_DIV_EXPR:
12567 case TRUNC_MOD_EXPR:
12568 case FLOOR_MOD_EXPR:
12569 case RDIV_EXPR:
12570 case EQ_EXPR:
12571 case NE_EXPR:
12572 case LE_EXPR:
12573 case GE_EXPR:
12574 case LT_EXPR:
12575 case GT_EXPR:
12576 /* What about UNLT_EXPR? */
12577 if (TREE_CODE (type0) == VECTOR_TYPE)
12578 {
12579 tree tmp;
12580 ret = stv_secondarg;
12581 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12582 tmp = type0; type0 = type1; type1 = tmp;
12583 tmp = op0; op0 = op1; op1 = tmp;
12584 }
12585
12586 if (TREE_CODE (type0) == INTEGER_TYPE
12587 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12588 {
22a75734 12589 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12590 {
12591 if (complain)
12592 error_at (loc, "conversion of scalar %qT to vector %qT "
12593 "involves truncation", type0, type1);
12594 return stv_error;
12595 }
12596 return ret;
12597 }
12598 else if (!integer_only_op
12599 /* Allow integer --> real conversion if safe. */
12600 && (TREE_CODE (type0) == REAL_TYPE
12601 || TREE_CODE (type0) == INTEGER_TYPE)
12602 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12603 {
22a75734 12604 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12605 {
12606 if (complain)
12607 error_at (loc, "conversion of scalar %qT to vector %qT "
12608 "involves truncation", type0, type1);
12609 return stv_error;
12610 }
12611 return ret;
12612 }
12613 default:
12614 break;
12615 }
12616
12617 return stv_nothing;
12618}
12619
ffcdbf9c 12620/* Return true iff ALIGN is an integral constant that is a fundamental
12621 alignment, as defined by [basic.align] in the c++-11
12622 specifications.
12623
12624 That is:
12625
12626 [A fundamental alignment is represented by an alignment less than or
12627 equal to the greatest alignment supported by the implementation
12628 in all contexts, which is equal to
12629 alignof(max_align_t)]. */
12630
12631bool
12632cxx_fundamental_alignment_p (unsigned align)
12633{
12634 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12635 TYPE_ALIGN (long_double_type_node)));
12636}
12637
46da3601 12638/* Return true if T is a pointer to a zero-sized aggregate. */
12639
12640bool
12641pointer_to_zero_sized_aggr_p (tree t)
12642{
12643 if (!POINTER_TYPE_P (t))
12644 return false;
12645 t = TREE_TYPE (t);
12646 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12647}
12648
7bedc3a0 12649#include "gt-c-family-c-common.h"