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