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