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