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