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