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