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