]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
* config/rl78/rl78.c (rl78_select_section): Select the correct
[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
d875b9d2 259/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 260
d875b9d2 261enum cxx_dialect cxx_dialect = cxx_unset;
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{
c37be9ec 5921 tree va_type = TREE_TYPE (expr);
5922 tree canon_va_type = (va_type == error_mark_node
5923 ? NULL_TREE
5924 : targetm.canonical_va_list_type (va_type));
5925
5926 if (canon_va_type != NULL)
5927 {
5928 /* When the va_arg ap argument is a parm decl with declared type va_list,
5929 and the va_list type is an array, then grokdeclarator changes the type
5930 of the parm decl to the corresponding pointer type. We know that that
5931 pointer is constant, so there's no need to modify it, so there's no
5932 need to pass it around using an address operator, so there's no need to
5933 mark it addressable. */
5934 if (!(TREE_CODE (canon_va_type) == ARRAY_TYPE
5935 && TREE_CODE (va_type) != ARRAY_TYPE))
5936 /* In gimplify_va_arg_expr we take the address of the ap argument, mark
5937 it addressable now. */
5938 mark_addressable (expr);
5939 }
f7fec1c7 5940
e60a6f7b 5941 expr = build1 (VA_ARG_EXPR, type, expr);
5942 SET_EXPR_LOCATION (expr, loc);
5943 return expr;
a66c9326 5944}
0d4238dc 5945
5946
dd878098 5947/* Linked list of disabled built-in functions. */
5948
5949typedef struct disabled_builtin
5950{
5951 const char *name;
5952 struct disabled_builtin *next;
5953} disabled_builtin;
5954static disabled_builtin *disabled_builtins = NULL;
5955
1cae46be 5956static bool builtin_function_disabled_p (const char *);
dd878098 5957
5958/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5959 begins with "__builtin_", give an error. */
5960
5961void
1cae46be 5962disable_builtin_function (const char *name)
dd878098 5963{
5964 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5965 error ("cannot disable built-in function %qs", name);
dd878098 5966 else
5967 {
e85905e5 5968 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5969 new_disabled_builtin->name = name;
5970 new_disabled_builtin->next = disabled_builtins;
5971 disabled_builtins = new_disabled_builtin;
dd878098 5972 }
5973}
5974
5975
5976/* Return true if the built-in function NAME has been disabled, false
5977 otherwise. */
5978
5979static bool
1cae46be 5980builtin_function_disabled_p (const char *name)
dd878098 5981{
5982 disabled_builtin *p;
5983 for (p = disabled_builtins; p != NULL; p = p->next)
5984 {
5985 if (strcmp (name, p->name) == 0)
5986 return true;
5987 }
5988 return false;
5989}
5990
5991
3237155d 5992/* Worker for DEF_BUILTIN.
5993 Possibly define a builtin function with one or two names.
5994 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5995 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 5996
3237155d 5997static void
5998def_builtin_1 (enum built_in_function fncode,
5999 const char *name,
6000 enum built_in_class fnclass,
6001 tree fntype, tree libtype,
6002 bool both_p, bool fallback_p, bool nonansi_p,
6003 tree fnattrs, bool implicit_p)
0d4238dc 6004{
3237155d 6005 tree decl;
6006 const char *libname;
6007
27213ba3 6008 if (fntype == error_mark_node)
6009 return;
6010
3237155d 6011 gcc_assert ((!both_p && !fallback_p)
6012 || !strncmp (name, "__builtin_",
6013 strlen ("__builtin_")));
6014
6015 libname = name + strlen ("__builtin_");
54be5d7e 6016 decl = add_builtin_function (name, fntype, fncode, fnclass,
6017 (fallback_p ? libname : NULL),
6018 fnattrs);
b9a16870 6019
6020 set_builtin_decl (fncode, decl, implicit_p);
6021
3237155d 6022 if (both_p
6023 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6024 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6025 add_builtin_function (libname, libtype, fncode, fnclass,
6026 NULL, fnattrs);
0d4238dc 6027}
e94026da 6028\f
d7aeef06 6029/* Nonzero if the type T promotes to int. This is (nearly) the
6030 integral promotions defined in ISO C99 6.3.1.1/2. */
6031
6032bool
9f627b1a 6033c_promoting_integer_type_p (const_tree t)
d7aeef06 6034{
6035 switch (TREE_CODE (t))
6036 {
6037 case INTEGER_TYPE:
6038 return (TYPE_MAIN_VARIANT (t) == char_type_node
6039 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6040 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6041 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6042 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6043 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6044
6045 case ENUMERAL_TYPE:
6046 /* ??? Technically all enumerations not larger than an int
6047 promote to an int. But this is used along code paths
6048 that only want to notice a size change. */
6049 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6050
6051 case BOOLEAN_TYPE:
6052 return 1;
6053
6054 default:
6055 return 0;
6056 }
6057}
6058
e94026da 6059/* Return 1 if PARMS specifies a fixed number of parameters
6060 and none of their types is affected by default promotions. */
6061
6062int
9f627b1a 6063self_promoting_args_p (const_tree parms)
e94026da 6064{
9f627b1a 6065 const_tree t;
e94026da 6066 for (t = parms; t; t = TREE_CHAIN (t))
6067 {
19cb6b50 6068 tree type = TREE_VALUE (t);
43f74bc4 6069
e1d8e198 6070 if (type == error_mark_node)
6071 continue;
6072
e94026da 6073 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6074 return 0;
6075
6076 if (type == 0)
6077 return 0;
6078
6079 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6080 return 0;
6081
d7aeef06 6082 if (c_promoting_integer_type_p (type))
e94026da 6083 return 0;
6084 }
6085 return 1;
6086}
605fb01e 6087
c10de5e7 6088/* Recursively remove any '*' or '&' operator from TYPE. */
6089tree
6090strip_pointer_operator (tree t)
6091{
6092 while (POINTER_TYPE_P (t))
6093 t = TREE_TYPE (t);
6094 return t;
6095}
6096
57a0ed23 6097/* Recursively remove pointer or array type from TYPE. */
6098tree
6099strip_pointer_or_array_types (tree t)
6100{
6101 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6102 t = TREE_TYPE (t);
6103 return t;
6104}
6105
e41f0d80 6106/* Used to compare case labels. K1 and K2 are actually tree nodes
6107 representing case labels, or NULL_TREE for a `default' label.
6108 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6109 K2, and 0 if K1 and K2 are equal. */
6110
6111int
1cae46be 6112case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6113{
6114 /* Consider a NULL key (such as arises with a `default' label) to be
6115 smaller than anything else. */
6116 if (!k1)
6117 return k2 ? -1 : 0;
6118 else if (!k2)
6119 return k1 ? 1 : 0;
6120
6121 return tree_int_cst_compare ((tree) k1, (tree) k2);
6122}
6123
e60a6f7b 6124/* Process a case label, located at LOC, for the range LOW_VALUE
6125 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6126 then this case label is actually a `default' label. If only
6127 HIGH_VALUE is NULL_TREE, then case label was declared using the
6128 usual C/C++ syntax, rather than the GNU case range extension.
6129 CASES is a tree containing all the case ranges processed so far;
6130 COND is the condition for the switch-statement itself. Returns the
6131 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6132 is created. */
e41f0d80 6133
6134tree
e60a6f7b 6135c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
2ca392fd 6136 tree low_value, tree high_value)
e41f0d80 6137{
6138 tree type;
6139 tree label;
6140 tree case_label;
6141 splay_tree_node node;
6142
6143 /* Create the LABEL_DECL itself. */
e60a6f7b 6144 label = create_artificial_label (loc);
e41f0d80 6145
6146 /* If there was an error processing the switch condition, bail now
6147 before we get more confused. */
6148 if (!cond || cond == error_mark_node)
4ee9c684 6149 goto error_out;
e41f0d80 6150
1cae46be 6151 if ((low_value && TREE_TYPE (low_value)
6152 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6153 || (high_value && TREE_TYPE (high_value)
6154 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6155 {
e60a6f7b 6156 error_at (loc, "pointers are not permitted as case values");
b96dc121 6157 goto error_out;
6158 }
e41f0d80 6159
6160 /* Case ranges are a GNU extension. */
8864917d 6161 if (high_value)
29438999 6162 pedwarn (loc, OPT_Wpedantic,
8864917d 6163 "range expressions in switch statements are non-standard");
e41f0d80 6164
6165 type = TREE_TYPE (cond);
6166 if (low_value)
6167 {
2d2f6a15 6168 low_value = check_case_value (loc, low_value);
22a75734 6169 low_value = convert_and_check (loc, type, low_value);
96722196 6170 if (low_value == error_mark_node)
6171 goto error_out;
e41f0d80 6172 }
6173 if (high_value)
6174 {
2d2f6a15 6175 high_value = check_case_value (loc, high_value);
22a75734 6176 high_value = convert_and_check (loc, type, high_value);
96722196 6177 if (high_value == error_mark_node)
6178 goto error_out;
e41f0d80 6179 }
6180
96722196 6181 if (low_value && high_value)
6182 {
6183 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6184 really a case range, even though it was written that way.
6185 Remove the HIGH_VALUE to simplify later processing. */
96722196 6186 if (tree_int_cst_equal (low_value, high_value))
6187 high_value = NULL_TREE;
6188 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6189 warning_at (loc, 0, "empty range specified");
96722196 6190 }
e41f0d80 6191
2ca392fd 6192 /* See if the case is in range of the type of the original testing
6193 expression. If both low_value and high_value are out of range,
6194 don't insert the case label and return NULL_TREE. */
6195 if (low_value
f61a9bc2 6196 && !check_case_bounds (loc, type, orig_type,
84166705 6197 &low_value, high_value ? &high_value : NULL))
2ca392fd 6198 return NULL_TREE;
6199
e41f0d80 6200 /* Look up the LOW_VALUE in the table of case labels we already
6201 have. */
6202 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6203 /* If there was not an exact match, check for overlapping ranges.
6204 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6205 that's a `default' label and the only overlap is an exact match. */
6206 if (!node && (low_value || high_value))
6207 {
6208 splay_tree_node low_bound;
6209 splay_tree_node high_bound;
6210
6211 /* Even though there wasn't an exact match, there might be an
6212 overlap between this case range and another case range.
6213 Since we've (inductively) not allowed any overlapping case
6214 ranges, we simply need to find the greatest low case label
6215 that is smaller that LOW_VALUE, and the smallest low case
6216 label that is greater than LOW_VALUE. If there is an overlap
6217 it will occur in one of these two ranges. */
6218 low_bound = splay_tree_predecessor (cases,
6219 (splay_tree_key) low_value);
6220 high_bound = splay_tree_successor (cases,
6221 (splay_tree_key) low_value);
6222
6223 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6224 the LOW_VALUE, so there is no need to check unless the
6225 LOW_BOUND is in fact itself a case range. */
6226 if (low_bound
6227 && CASE_HIGH ((tree) low_bound->value)
6228 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6229 low_value) >= 0)
6230 node = low_bound;
6231 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6232 range is bigger than the low end of the current range, so we
6233 are only interested if the current range is a real range, and
6234 not an ordinary case label. */
1cae46be 6235 else if (high_bound
e41f0d80 6236 && high_value
6237 && (tree_int_cst_compare ((tree) high_bound->key,
6238 high_value)
6239 <= 0))
6240 node = high_bound;
6241 }
6242 /* If there was an overlap, issue an error. */
6243 if (node)
6244 {
eaae3b75 6245 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6246
6247 if (high_value)
6248 {
e60a6f7b 6249 error_at (loc, "duplicate (or overlapping) case value");
6250 error_at (DECL_SOURCE_LOCATION (duplicate),
6251 "this is the first entry overlapping that value");
e41f0d80 6252 }
6253 else if (low_value)
6254 {
e60a6f7b 6255 error_at (loc, "duplicate case value") ;
6256 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6257 }
6258 else
6259 {
e60a6f7b 6260 error_at (loc, "multiple default labels in one switch");
6261 error_at (DECL_SOURCE_LOCATION (duplicate),
6262 "this is the first default label");
e41f0d80 6263 }
4ee9c684 6264 goto error_out;
e41f0d80 6265 }
6266
6267 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6268 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6269 /* Register this case label in the splay tree. */
1cae46be 6270 splay_tree_insert (cases,
e41f0d80 6271 (splay_tree_key) low_value,
6272 (splay_tree_value) case_label);
6273
6274 return case_label;
4ee9c684 6275
6276 error_out:
daf6dff5 6277 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6278 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6279 that just leads to duplicates and thence to failure later on. */
4ee9c684 6280 if (!cases->root)
6281 {
e60a6f7b 6282 tree t = create_artificial_label (loc);
6283 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6284 }
6285 return error_mark_node;
6286}
6287
6288/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6289 Used to verify that case values match up with enumerator values. */
6290
6291static void
6292match_case_to_enum_1 (tree key, tree type, tree label)
6293{
e913b5cd 6294 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6295
dd76621f 6296 if (tree_fits_uhwi_p (key))
6297 print_dec (key, buf, UNSIGNED);
6298 else if (tree_fits_shwi_p (key))
6299 print_dec (key, buf, SIGNED);
4ee9c684 6300 else
e913b5cd 6301 print_hex (key, buf);
4ee9c684 6302
6303 if (TYPE_NAME (type) == 0)
712d2297 6304 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6305 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6306 "case value %qs not in enumerated type",
6307 buf);
4ee9c684 6308 else
712d2297 6309 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6310 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6311 "case value %qs not in enumerated type %qT",
6312 buf, type);
4ee9c684 6313}
6314
359d87c6 6315/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6316 Used to verify that case values match up with enumerator values. */
6317
4ee9c684 6318static int
6319match_case_to_enum (splay_tree_node node, void *data)
6320{
6321 tree label = (tree) node->value;
4fd61bc6 6322 tree type = (tree) data;
4ee9c684 6323
6324 /* Skip default case. */
6325 if (!CASE_LOW (label))
6326 return 0;
6327
359d87c6 6328 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6329 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6330 if (!CASE_LOW_SEEN (label))
4ee9c684 6331 match_case_to_enum_1 (CASE_LOW (label), type, label);
6332 else
359d87c6 6333 CASE_LOW_SEEN (label) = 0;
4ee9c684 6334
359d87c6 6335 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6336 not set, that means that CASE_HIGH did not appear when we did our
6337 enum->case scan. Reset our scratch bit after. */
4ee9c684 6338 if (CASE_HIGH (label))
6339 {
359d87c6 6340 if (!CASE_HIGH_SEEN (label))
6341 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6342 else
6343 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6344 }
6345
6346 return 0;
6347}
6348
e7911019 6349/* Handle -Wswitch*. Called from the front end after parsing the
6350 switch construct. */
6351/* ??? Should probably be somewhere generic, since other languages
6352 besides C and C++ would want this. At the moment, however, C/C++
6353 are the only tree-ssa languages that support enumerations at all,
6354 so the point is moot. */
4ee9c684 6355
e7911019 6356void
6357c_do_switch_warnings (splay_tree cases, location_t switch_location,
6358 tree type, tree cond)
4ee9c684 6359{
b27ac6b5 6360 splay_tree_node default_node;
359d87c6 6361 splay_tree_node node;
6362 tree chain;
4ee9c684 6363
6364 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6365 return;
6366
4ee9c684 6367 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6368 if (!default_node)
5fb6a912 6369 warning_at (switch_location, OPT_Wswitch_default,
6370 "switch missing default case");
4ee9c684 6371
359d87c6 6372 /* From here on, we only care about about enumerated types. */
6373 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6374 return;
6375
561017b5 6376 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6377 if (!warn_switch_enum && !warn_switch)
359d87c6 6378 return;
6379
561017b5 6380 /* Check the cases. Warn about case values which are not members of
6381 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6382 there is no default case, check that exactly all enumeration
6383 literals are covered by the cases. */
6384
359d87c6 6385 /* Clearing COND if it is not an integer constant simplifies
6386 the tests inside the loop below. */
6387 if (TREE_CODE (cond) != INTEGER_CST)
6388 cond = NULL_TREE;
6389
6390 /* The time complexity here is O(N*lg(N)) worst case, but for the
6391 common case of monotonically increasing enumerators, it is
6392 O(N), since the nature of the splay tree will keep the next
6393 element adjacent to the root at all times. */
4ee9c684 6394
359d87c6 6395 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6396 {
6397 tree value = TREE_VALUE (chain);
3f00a6c0 6398 if (TREE_CODE (value) == CONST_DECL)
6399 value = DECL_INITIAL (value);
359d87c6 6400 node = splay_tree_lookup (cases, (splay_tree_key) value);
6401 if (node)
4ee9c684 6402 {
359d87c6 6403 /* Mark the CASE_LOW part of the case entry as seen. */
6404 tree label = (tree) node->value;
6405 CASE_LOW_SEEN (label) = 1;
6406 continue;
6407 }
6408
6409 /* Even though there wasn't an exact match, there might be a
f0b5f617 6410 case range which includes the enumerator's value. */
359d87c6 6411 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6412 if (node && CASE_HIGH ((tree) node->value))
6413 {
6414 tree label = (tree) node->value;
6415 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6416 if (cmp >= 0)
4ee9c684 6417 {
359d87c6 6418 /* If we match the upper bound exactly, mark the CASE_HIGH
6419 part of the case entry as seen. */
6420 if (cmp == 0)
6421 CASE_HIGH_SEEN (label) = 1;
6422 continue;
4ee9c684 6423 }
6424 }
6425
359d87c6 6426 /* We've now determined that this enumerated literal isn't
6427 handled by the case labels of the switch statement. */
4ee9c684 6428
359d87c6 6429 /* If the switch expression is a constant, we only really care
6430 about whether that constant is handled by the switch. */
6431 if (cond && tree_int_cst_compare (cond, value))
6432 continue;
4ee9c684 6433
6cbbbc89 6434 /* If there is a default_node, the only relevant option is
561017b5 6435 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6436 to warn using -Wswitch because -Wswitch is enabled by -Wall
6437 while -Wswitch-enum is explicit. */
561017b5 6438 warning_at (switch_location,
6439 (default_node || !warn_switch
6440 ? OPT_Wswitch_enum
6441 : OPT_Wswitch),
6442 "enumeration value %qE not handled in switch",
6443 TREE_PURPOSE (chain));
4ee9c684 6444 }
359d87c6 6445
6446 /* Warn if there are case expressions that don't correspond to
6447 enumerators. This can occur since C and C++ don't enforce
6448 type-checking of assignments to enumeration variables.
6449
6450 The time complexity here is now always O(N) worst case, since
6451 we should have marked both the lower bound and upper bound of
6452 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6453 above. This scan also resets those fields. */
6cbbbc89 6454
359d87c6 6455 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6456}
6457
9dd48740 6458/* Finish an expression taking the address of LABEL (an
dda49785 6459 IDENTIFIER_NODE). Returns an expression for the address.
6460
6461 LOC is the location for the expression returned. */
d0a47c8d 6462
1cae46be 6463tree
dda49785 6464finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6465{
6466 tree result;
6467
29438999 6468 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6469
9dd48740 6470 if (label == error_mark_node)
6471 return error_mark_node;
6472
d0a47c8d 6473 label = lookup_label (label);
6474 if (label == NULL_TREE)
6475 result = null_pointer_node;
6476 else
6477 {
6478 TREE_USED (label) = 1;
6479 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6480 /* The current function is not necessarily uninlinable.
d0a47c8d 6481 Computed gotos are incompatible with inlining, but the value
6482 here could be used only in a diagnostic, for example. */
dda49785 6483 protected_set_expr_location (result, loc);
d0a47c8d 6484 }
6485
6486 return result;
6487}
4f9a1c9b 6488\f
6489
6490/* Given a boolean expression ARG, return a tree representing an increment
6491 or decrement (as indicated by CODE) of ARG. The front end must check for
6492 invalid cases (e.g., decrement in C++). */
6493tree
1cae46be 6494boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6495{
6496 tree val;
69db191c 6497 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6498
4f9a1c9b 6499 arg = stabilize_reference (arg);
6500 switch (code)
6501 {
6502 case PREINCREMENT_EXPR:
14ae0310 6503 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6504 break;
6505 case POSTINCREMENT_EXPR:
14ae0310 6506 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6507 arg = save_expr (arg);
14ae0310 6508 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6509 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6510 break;
6511 case PREDECREMENT_EXPR:
14ae0310 6512 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6513 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6514 break;
6515 case POSTDECREMENT_EXPR:
14ae0310 6516 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6517 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6518 arg = save_expr (arg);
14ae0310 6519 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6520 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6521 break;
6522 default:
231bd014 6523 gcc_unreachable ();
4f9a1c9b 6524 }
6525 TREE_SIDE_EFFECTS (val) = 1;
6526 return val;
6527}
76a6e674 6528\f
f3449a3c 6529/* Built-in macros for stddef.h and stdint.h, that require macros
6530 defined in this file. */
79cf3ec1 6531void
1cae46be 6532c_stddef_cpp_builtins(void)
1ed9d5f5 6533{
63994318 6534 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6535 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6536 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6537 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6538 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6539 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6540 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6541 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6542 if (SIG_ATOMIC_TYPE)
6543 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6544 if (INT8_TYPE)
6545 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6546 if (INT16_TYPE)
6547 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6548 if (INT32_TYPE)
6549 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6550 if (INT64_TYPE)
6551 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6552 if (UINT8_TYPE)
6553 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6554 if (UINT16_TYPE)
6555 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6556 if (UINT32_TYPE)
6557 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6558 if (UINT64_TYPE)
6559 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6560 if (INT_LEAST8_TYPE)
6561 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6562 if (INT_LEAST16_TYPE)
6563 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6564 if (INT_LEAST32_TYPE)
6565 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6566 if (INT_LEAST64_TYPE)
6567 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6568 if (UINT_LEAST8_TYPE)
6569 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6570 if (UINT_LEAST16_TYPE)
6571 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6572 if (UINT_LEAST32_TYPE)
6573 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6574 if (UINT_LEAST64_TYPE)
6575 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6576 if (INT_FAST8_TYPE)
6577 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6578 if (INT_FAST16_TYPE)
6579 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6580 if (INT_FAST32_TYPE)
6581 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6582 if (INT_FAST64_TYPE)
6583 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6584 if (UINT_FAST8_TYPE)
6585 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6586 if (UINT_FAST16_TYPE)
6587 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6588 if (UINT_FAST32_TYPE)
6589 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6590 if (UINT_FAST64_TYPE)
6591 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6592 if (INTPTR_TYPE)
6593 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6594 if (UINTPTR_TYPE)
6595 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6596}
6597
7d3b509a 6598static void
1cae46be 6599c_init_attributes (void)
7d3b509a 6600{
6601 /* Fill in the built_in_attributes array. */
7c446c95 6602#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6603 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6604#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6605 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6606#define DEF_ATTR_STRING(ENUM, VALUE) \
6607 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6608#define DEF_ATTR_IDENT(ENUM, STRING) \
6609 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6610#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6611 built_in_attributes[(int) ENUM] \
6612 = tree_cons (built_in_attributes[(int) PURPOSE], \
6613 built_in_attributes[(int) VALUE], \
6614 built_in_attributes[(int) CHAIN]);
7d3b509a 6615#include "builtin-attrs.def"
6616#undef DEF_ATTR_NULL_TREE
6617#undef DEF_ATTR_INT
6618#undef DEF_ATTR_IDENT
6619#undef DEF_ATTR_TREE_LIST
76a6e674 6620}
5f3cead1 6621
33199a81 6622/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6623 identifier as an argument, so the front end shouldn't look it up. */
6624
6625bool
47b19b94 6626attribute_takes_identifier_p (const_tree attr_id)
33199a81 6627{
9bf1c74e 6628 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6629 if (spec == NULL)
6630 /* Unknown attribute that we'll end up ignoring, return true so we
6631 don't complain about an identifier argument. */
6632 return true;
6633 else if (!strcmp ("mode", spec->name)
6634 || !strcmp ("format", spec->name)
6635 || !strcmp ("cleanup", spec->name))
47b19b94 6636 return true;
6637 else
6638 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6639}
6640
f8e93a2e 6641/* Attribute handlers common to C front ends. */
6642
6643/* Handle a "packed" attribute; arguments as in
6644 struct attribute_spec.handler. */
6645
6646static tree
9a03a746 6647handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6648 int flags, bool *no_add_attrs)
f8e93a2e 6649{
f40175cb 6650 if (TYPE_P (*node))
f8e93a2e 6651 {
6652 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6653 *node = build_variant_type_copy (*node);
f40175cb 6654 TYPE_PACKED (*node) = 1;
f8e93a2e 6655 }
6656 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6657 {
9fd767c5 6658 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6659 /* Still pack bitfields. */
6660 && ! DECL_INITIAL (*node))
c2ab04f9 6661 warning (OPT_Wattributes,
6662 "%qE attribute ignored for field of type %qT",
6663 name, TREE_TYPE (*node));
6664 else
6665 DECL_PACKED (*node) = 1;
6666 }
f8e93a2e 6667 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6668 used for DECL_REGISTER. It wouldn't mean anything anyway.
6669 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6670 that changes what the typedef is typing. */
f8e93a2e 6671 else
6672 {
9b2d6d13 6673 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6674 *no_add_attrs = true;
6675 }
6676
6677 return NULL_TREE;
6678}
6679
6680/* Handle a "nocommon" attribute; arguments as in
6681 struct attribute_spec.handler. */
6682
6683static tree
1cae46be 6684handle_nocommon_attribute (tree *node, tree name,
9a03a746 6685 tree ARG_UNUSED (args),
6686 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6687{
6688 if (TREE_CODE (*node) == VAR_DECL)
6689 DECL_COMMON (*node) = 0;
6690 else
6691 {
9b2d6d13 6692 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6693 *no_add_attrs = true;
6694 }
6695
6696 return NULL_TREE;
6697}
6698
6699/* Handle a "common" attribute; arguments as in
6700 struct attribute_spec.handler. */
6701
6702static tree
9a03a746 6703handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6704 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6705{
6706 if (TREE_CODE (*node) == VAR_DECL)
6707 DECL_COMMON (*node) = 1;
6708 else
6709 {
9b2d6d13 6710 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6711 *no_add_attrs = true;
6712 }
6713
6714 return NULL_TREE;
6715}
6716
6717/* Handle a "noreturn" attribute; arguments as in
6718 struct attribute_spec.handler. */
6719
6720static tree
9a03a746 6721handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6722 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6723{
6724 tree type = TREE_TYPE (*node);
6725
6726 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6727 if (TREE_CODE (*node) == FUNCTION_DECL
6728 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6729 TREE_THIS_VOLATILE (*node) = 1;
6730 else if (TREE_CODE (type) == POINTER_TYPE
6731 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6732 TREE_TYPE (*node)
9d4eeb52 6733 = (build_qualified_type
6734 (build_pointer_type
6735 (build_type_variant (TREE_TYPE (type),
6736 TYPE_READONLY (TREE_TYPE (type)), 1)),
6737 TYPE_QUALS (type)));
f8e93a2e 6738 else
6739 {
9b2d6d13 6740 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6741 *no_add_attrs = true;
6742 }
6743
6744 return NULL_TREE;
6745}
6746
5de92639 6747/* Handle a "hot" and attribute; arguments as in
6748 struct attribute_spec.handler. */
6749
6750static tree
6751handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 6752 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 6753{
758a38ab 6754 if (TREE_CODE (*node) == FUNCTION_DECL
6755 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6756 {
6757 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6758 {
4a026b48 6759 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6760 "with attribute %qs", name, "cold");
5de92639 6761 *no_add_attrs = true;
6762 }
24470055 6763 /* Most of the rest of the hot processing is done later with
6764 lookup_attribute. */
5de92639 6765 }
6766 else
6767 {
6768 warning (OPT_Wattributes, "%qE attribute ignored", name);
6769 *no_add_attrs = true;
6770 }
6771
6772 return NULL_TREE;
6773}
758a38ab 6774
5de92639 6775/* Handle a "cold" and attribute; arguments as in
6776 struct attribute_spec.handler. */
6777
6778static tree
6779handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6780 int ARG_UNUSED (flags), bool *no_add_attrs)
6781{
758a38ab 6782 if (TREE_CODE (*node) == FUNCTION_DECL
6783 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6784 {
6785 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6786 {
4a026b48 6787 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6788 "with attribute %qs", name, "hot");
5de92639 6789 *no_add_attrs = true;
6790 }
24470055 6791 /* Most of the rest of the cold processing is done later with
6792 lookup_attribute. */
5de92639 6793 }
6794 else
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_sanitize_address" attribute; arguments as in
d413ffdd 6804 struct attribute_spec.handler. */
6805
6806static tree
a9196da9 6807handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6808 bool *no_add_attrs)
d413ffdd 6809{
6810 if (TREE_CODE (*node) != FUNCTION_DECL)
6811 {
6812 warning (OPT_Wattributes, "%qE attribute ignored", name);
6813 *no_add_attrs = true;
6814 }
6815
6816 return NULL_TREE;
6817}
6818
a9196da9 6819/* Handle a "no_address_safety_analysis" attribute; arguments as in
6820 struct attribute_spec.handler. */
6821
6822static tree
6823handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6824 bool *no_add_attrs)
6825{
6826 if (TREE_CODE (*node) != FUNCTION_DECL)
6827 warning (OPT_Wattributes, "%qE attribute ignored", name);
6828 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6829 DECL_ATTRIBUTES (*node)
6830 = tree_cons (get_identifier ("no_sanitize_address"),
6831 NULL_TREE, DECL_ATTRIBUTES (*node));
6832 *no_add_attrs = true;
6833 return NULL_TREE;
6834}
6835
05f893e1 6836/* Handle a "no_sanitize_undefined" attribute; arguments as in
6837 struct attribute_spec.handler. */
6838
6839static tree
6840handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6841 bool *no_add_attrs)
6842{
6843 if (TREE_CODE (*node) != FUNCTION_DECL)
6844 {
6845 warning (OPT_Wattributes, "%qE attribute ignored", name);
6846 *no_add_attrs = true;
6847 }
6848
6849 return NULL_TREE;
6850}
6851
947aa916 6852/* Handle a "stack_protect" attribute; arguments as in
6853 struct attribute_spec.handler. */
6854static tree
6855handle_stack_protect_attribute (tree *node, tree name, tree, int,
6856 bool *no_add_attrs)
6857{
6858 if (TREE_CODE (*node) != FUNCTION_DECL)
6859 {
6860 warning (OPT_Wattributes, "%qE attribute ignored", name);
6861 *no_add_attrs = true;
6862 }
6863 else
6864 DECL_ATTRIBUTES (*node)
6865 = tree_cons (get_identifier ("stack_protect"),
6866 NULL_TREE, DECL_ATTRIBUTES (*node));
6867
6868 return NULL_TREE;
6869}
6870
f8e93a2e 6871/* Handle a "noinline" attribute; arguments as in
6872 struct attribute_spec.handler. */
6873
6874static tree
1cae46be 6875handle_noinline_attribute (tree *node, tree name,
9a03a746 6876 tree ARG_UNUSED (args),
6877 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6878{
6879 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 6880 {
6881 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6882 {
6883 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6884 "with attribute %qs", name, "always_inline");
6885 *no_add_attrs = true;
6886 }
6887 else
6888 DECL_UNINLINABLE (*node) = 1;
6889 }
f8e93a2e 6890 else
6891 {
9b2d6d13 6892 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6893 *no_add_attrs = true;
6894 }
6895
6896 return NULL_TREE;
6897}
6898
bdb1f0d1 6899/* Handle a "noclone" attribute; arguments as in
6900 struct attribute_spec.handler. */
6901
6902static tree
6903handle_noclone_attribute (tree *node, tree name,
6904 tree ARG_UNUSED (args),
6905 int ARG_UNUSED (flags), bool *no_add_attrs)
6906{
6907 if (TREE_CODE (*node) != FUNCTION_DECL)
6908 {
6909 warning (OPT_Wattributes, "%qE attribute ignored", name);
6910 *no_add_attrs = true;
6911 }
6912
6913 return NULL_TREE;
6914}
6915
85fbea97 6916/* Handle a "no_icf" attribute; arguments as in
6917 struct attribute_spec.handler. */
6918
6919static tree
6920handle_noicf_attribute (tree *node, tree name,
6921 tree ARG_UNUSED (args),
6922 int ARG_UNUSED (flags), bool *no_add_attrs)
6923{
6924 if (TREE_CODE (*node) != FUNCTION_DECL)
6925 {
6926 warning (OPT_Wattributes, "%qE attribute ignored", name);
6927 *no_add_attrs = true;
6928 }
6929
6930 return NULL_TREE;
6931}
6932
6933
f8e93a2e 6934/* Handle a "always_inline" attribute; arguments as in
6935 struct attribute_spec.handler. */
6936
6937static tree
1cae46be 6938handle_always_inline_attribute (tree *node, tree name,
9a03a746 6939 tree ARG_UNUSED (args),
6940 int ARG_UNUSED (flags),
09347743 6941 bool *no_add_attrs)
f8e93a2e 6942{
6943 if (TREE_CODE (*node) == FUNCTION_DECL)
6944 {
4a026b48 6945 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6946 {
6947 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6948 "with %qs attribute", name, "noinline");
6949 *no_add_attrs = true;
6950 }
6951 else
6952 /* Set the attribute and mark it for disregarding inline
6953 limits. */
6954 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 6955 }
6956 else
6957 {
9b2d6d13 6958 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6959 *no_add_attrs = true;
541e4101 6960 }
6961
6962 return NULL_TREE;
6963}
6964
6965/* Handle a "gnu_inline" attribute; arguments as in
6966 struct attribute_spec.handler. */
6967
6968static tree
6969handle_gnu_inline_attribute (tree *node, tree name,
6970 tree ARG_UNUSED (args),
6971 int ARG_UNUSED (flags),
6972 bool *no_add_attrs)
6973{
6974 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6975 {
6976 /* Do nothing else, just set the attribute. We'll get at
6977 it later with lookup_attribute. */
6978 }
6979 else
6980 {
6981 warning (OPT_Wattributes, "%qE attribute ignored", name);
6982 *no_add_attrs = true;
7bd95dfd 6983 }
6984
6985 return NULL_TREE;
6986}
6987
6988/* Handle a "leaf" attribute; arguments as in
6989 struct attribute_spec.handler. */
6990
6991static tree
6992handle_leaf_attribute (tree *node, tree name,
6993 tree ARG_UNUSED (args),
6994 int ARG_UNUSED (flags), bool *no_add_attrs)
6995{
6996 if (TREE_CODE (*node) != FUNCTION_DECL)
6997 {
6998 warning (OPT_Wattributes, "%qE attribute ignored", name);
6999 *no_add_attrs = true;
7000 }
7001 if (!TREE_PUBLIC (*node))
7002 {
7003 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7004 *no_add_attrs = true;
f8e93a2e 7005 }
7006
7007 return NULL_TREE;
7008}
7009
1b16fc45 7010/* Handle an "artificial" attribute; arguments as in
7011 struct attribute_spec.handler. */
7012
7013static tree
7014handle_artificial_attribute (tree *node, tree name,
7015 tree ARG_UNUSED (args),
7016 int ARG_UNUSED (flags),
7017 bool *no_add_attrs)
7018{
7019 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7020 {
7021 /* Do nothing else, just set the attribute. We'll get at
7022 it later with lookup_attribute. */
7023 }
7024 else
7025 {
7026 warning (OPT_Wattributes, "%qE attribute ignored", name);
7027 *no_add_attrs = true;
7028 }
7029
7030 return NULL_TREE;
7031}
7032
0cdd9887 7033/* Handle a "flatten" attribute; arguments as in
7034 struct attribute_spec.handler. */
7035
7036static tree
7037handle_flatten_attribute (tree *node, tree name,
a0c938f0 7038 tree args ATTRIBUTE_UNUSED,
7039 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7040{
7041 if (TREE_CODE (*node) == FUNCTION_DECL)
7042 /* Do nothing else, just set the attribute. We'll get at
7043 it later with lookup_attribute. */
7044 ;
7045 else
7046 {
7047 warning (OPT_Wattributes, "%qE attribute ignored", name);
7048 *no_add_attrs = true;
7049 }
7050
7051 return NULL_TREE;
7052}
7053
10fc867f 7054/* Handle a "warning" or "error" attribute; arguments as in
7055 struct attribute_spec.handler. */
7056
7057static tree
7058handle_error_attribute (tree *node, tree name, tree args,
7059 int ARG_UNUSED (flags), bool *no_add_attrs)
7060{
7061 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7062 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7063 /* Do nothing else, just set the attribute. We'll get at
7064 it later with lookup_attribute. */
7065 ;
7066 else
7067 {
7068 warning (OPT_Wattributes, "%qE attribute ignored", name);
7069 *no_add_attrs = true;
7070 }
7071
7072 return NULL_TREE;
7073}
0cdd9887 7074
f8e93a2e 7075/* Handle a "used" attribute; arguments as in
7076 struct attribute_spec.handler. */
7077
7078static tree
9a03a746 7079handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7080 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7081{
d0a31bd8 7082 tree node = *pnode;
7083
7084 if (TREE_CODE (node) == FUNCTION_DECL
a4e3ffad 7085 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7086 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7087 {
f54ed8bc 7088 TREE_USED (node) = 1;
9423c9b7 7089 DECL_PRESERVE_P (node) = 1;
abc6c64f 7090 if (TREE_CODE (node) == VAR_DECL)
7091 DECL_READ_P (node) = 1;
f54ed8bc 7092 }
f8e93a2e 7093 else
7094 {
9b2d6d13 7095 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7096 *no_add_attrs = true;
7097 }
7098
7099 return NULL_TREE;
7100}
7101
7102/* Handle a "unused" attribute; arguments as in
7103 struct attribute_spec.handler. */
7104
7105static tree
9a03a746 7106handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7107 int flags, bool *no_add_attrs)
f8e93a2e 7108{
7109 if (DECL_P (*node))
7110 {
7111 tree decl = *node;
7112
7113 if (TREE_CODE (decl) == PARM_DECL
7114 || TREE_CODE (decl) == VAR_DECL
7115 || TREE_CODE (decl) == FUNCTION_DECL
7116 || TREE_CODE (decl) == LABEL_DECL
7117 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7118 {
7119 TREE_USED (decl) = 1;
7120 if (TREE_CODE (decl) == VAR_DECL
7121 || TREE_CODE (decl) == PARM_DECL)
7122 DECL_READ_P (decl) = 1;
7123 }
f8e93a2e 7124 else
7125 {
9b2d6d13 7126 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7127 *no_add_attrs = true;
7128 }
7129 }
7130 else
7131 {
7132 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7133 *node = build_variant_type_copy (*node);
f8e93a2e 7134 TREE_USED (*node) = 1;
7135 }
7136
7137 return NULL_TREE;
7138}
7139
62eec3b4 7140/* Handle a "externally_visible" attribute; arguments as in
7141 struct attribute_spec.handler. */
7142
7143static tree
7144handle_externally_visible_attribute (tree *pnode, tree name,
7145 tree ARG_UNUSED (args),
7146 int ARG_UNUSED (flags),
7147 bool *no_add_attrs)
7148{
7149 tree node = *pnode;
7150
ba12ea31 7151 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
62eec3b4 7152 {
ba12ea31 7153 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7154 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7155 {
7156 warning (OPT_Wattributes,
7157 "%qE attribute have effect only on public objects", name);
7158 *no_add_attrs = true;
7159 }
62eec3b4 7160 }
62eec3b4 7161 else
7162 {
7163 warning (OPT_Wattributes, "%qE attribute ignored", name);
7164 *no_add_attrs = true;
7165 }
7166
7167 return NULL_TREE;
7168}
7169
6b722052 7170/* Handle the "no_reorder" attribute. Arguments as in
7171 struct attribute_spec.handler. */
7172
7173static tree
7174handle_no_reorder_attribute (tree *pnode,
7175 tree name,
7176 tree,
7177 int,
7178 bool *no_add_attrs)
7179{
7180 tree node = *pnode;
7181
7182 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7183 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7184 {
7185 warning (OPT_Wattributes,
7186 "%qE attribute only affects top level objects",
7187 name);
7188 *no_add_attrs = true;
7189 }
7190
7191 return NULL_TREE;
7192}
7193
f8e93a2e 7194/* Handle a "const" attribute; arguments as in
7195 struct attribute_spec.handler. */
7196
7197static tree
9a03a746 7198handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7199 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7200{
7201 tree type = TREE_TYPE (*node);
7202
7203 /* See FIXME comment on noreturn in c_common_attribute_table. */
7204 if (TREE_CODE (*node) == FUNCTION_DECL)
7205 TREE_READONLY (*node) = 1;
7206 else if (TREE_CODE (type) == POINTER_TYPE
7207 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7208 TREE_TYPE (*node)
9d4eeb52 7209 = (build_qualified_type
7210 (build_pointer_type
7211 (build_type_variant (TREE_TYPE (type), 1,
7212 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7213 TYPE_QUALS (type)));
f8e93a2e 7214 else
7215 {
9b2d6d13 7216 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7217 *no_add_attrs = true;
7218 }
7219
7220 return NULL_TREE;
7221}
7222
7223/* Handle a "transparent_union" attribute; arguments as in
7224 struct attribute_spec.handler. */
7225
7226static tree
1cae46be 7227handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7228 tree ARG_UNUSED (args), int flags,
09347743 7229 bool *no_add_attrs)
f8e93a2e 7230{
881eb642 7231 tree type;
03908818 7232
7233 *no_add_attrs = true;
f8e93a2e 7234
ffcdbf9c 7235
7236 if (TREE_CODE (*node) == TYPE_DECL
7237 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7238 node = &TREE_TYPE (*node);
7239 type = *node;
f8e93a2e 7240
03908818 7241 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7242 {
fca86134 7243 /* Make sure that the first field will work for a transparent union.
7244 If the type isn't complete yet, leave the check to the code in
7245 finish_struct. */
7246 if (TYPE_SIZE (type))
7247 {
7248 tree first = first_field (type);
7249 if (first == NULL_TREE
7250 || DECL_ARTIFICIAL (first)
7251 || TYPE_MODE (type) != DECL_MODE (first))
7252 goto ignored;
7253 }
7254
f8e93a2e 7255 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7256 {
fca86134 7257 /* If the type isn't complete yet, setting the flag
7258 on a variant wouldn't ever be checked. */
7259 if (!TYPE_SIZE (type))
7260 goto ignored;
7261
7262 /* build_duplicate_type doesn't work for C++. */
7263 if (c_dialect_cxx ())
03908818 7264 goto ignored;
7265
7266 /* A type variant isn't good enough, since we don't a cast
7267 to such a type removed as a no-op. */
7268 *node = type = build_duplicate_type (type);
7269 }
7270
8df5a43d 7271 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7272 return NULL_TREE;
f8e93a2e 7273 }
7274
03908818 7275 ignored:
7276 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7277 return NULL_TREE;
7278}
7279
9af7fd5b 7280/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7281 get the requested priority for a constructor or destructor,
7282 possibly issuing diagnostics for invalid or reserved
7283 priorities. */
7284
7285static priority_type
7286get_priority (tree args, bool is_destructor)
7287{
7288 HOST_WIDE_INT pri;
6c181a06 7289 tree arg;
9af7fd5b 7290
7291 if (!args)
7292 return DEFAULT_INIT_PRIORITY;
48e1416a 7293
28fbc04f 7294 if (!SUPPORTS_INIT_PRIORITY)
7295 {
7296 if (is_destructor)
7297 error ("destructor priorities are not supported");
7298 else
7299 error ("constructor priorities are not supported");
7300 return DEFAULT_INIT_PRIORITY;
7301 }
7302
6c181a06 7303 arg = TREE_VALUE (args);
253e1cae 7304 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7305 goto invalid;
7306 if (arg == error_mark_node)
7307 return DEFAULT_INIT_PRIORITY;
c28ddc97 7308 arg = default_conversion (arg);
e913b5cd 7309 if (!tree_fits_shwi_p (arg)
6c181a06 7310 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7311 goto invalid;
7312
e913b5cd 7313 pri = tree_to_shwi (arg);
9af7fd5b 7314 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7315 goto invalid;
7316
7317 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7318 {
7319 if (is_destructor)
7320 warning (0,
7321 "destructor priorities from 0 to %d are reserved "
48e1416a 7322 "for the implementation",
9af7fd5b 7323 MAX_RESERVED_INIT_PRIORITY);
7324 else
7325 warning (0,
7326 "constructor priorities from 0 to %d are reserved "
48e1416a 7327 "for the implementation",
9af7fd5b 7328 MAX_RESERVED_INIT_PRIORITY);
7329 }
7330 return pri;
7331
7332 invalid:
7333 if (is_destructor)
7334 error ("destructor priorities must be integers from 0 to %d inclusive",
7335 MAX_INIT_PRIORITY);
7336 else
7337 error ("constructor priorities must be integers from 0 to %d inclusive",
7338 MAX_INIT_PRIORITY);
7339 return DEFAULT_INIT_PRIORITY;
7340}
7341
f8e93a2e 7342/* Handle a "constructor" attribute; arguments as in
7343 struct attribute_spec.handler. */
7344
7345static tree
9af7fd5b 7346handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7347 int ARG_UNUSED (flags),
09347743 7348 bool *no_add_attrs)
f8e93a2e 7349{
7350 tree decl = *node;
7351 tree type = TREE_TYPE (decl);
7352
7353 if (TREE_CODE (decl) == FUNCTION_DECL
7354 && TREE_CODE (type) == FUNCTION_TYPE
7355 && decl_function_context (decl) == 0)
7356 {
9af7fd5b 7357 priority_type priority;
f8e93a2e 7358 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7359 priority = get_priority (args, /*is_destructor=*/false);
7360 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7361 TREE_USED (decl) = 1;
7362 }
7363 else
7364 {
9b2d6d13 7365 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7366 *no_add_attrs = true;
7367 }
7368
7369 return NULL_TREE;
7370}
7371
7372/* Handle a "destructor" attribute; arguments as in
7373 struct attribute_spec.handler. */
7374
7375static tree
9af7fd5b 7376handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7377 int ARG_UNUSED (flags),
09347743 7378 bool *no_add_attrs)
f8e93a2e 7379{
7380 tree decl = *node;
7381 tree type = TREE_TYPE (decl);
7382
7383 if (TREE_CODE (decl) == FUNCTION_DECL
7384 && TREE_CODE (type) == FUNCTION_TYPE
7385 && decl_function_context (decl) == 0)
7386 {
9af7fd5b 7387 priority_type priority;
f8e93a2e 7388 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7389 priority = get_priority (args, /*is_destructor=*/true);
7390 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7391 TREE_USED (decl) = 1;
7392 }
7393 else
7394 {
9b2d6d13 7395 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7396 *no_add_attrs = true;
7397 }
7398
7399 return NULL_TREE;
7400}
7401
1c58e3f1 7402/* Nonzero if the mode is a valid vector mode for this architecture.
7403 This returns nonzero even if there is no hardware support for the
7404 vector mode, but we can emulate with narrower modes. */
7405
7406static int
3754d046 7407vector_mode_valid_p (machine_mode mode)
1c58e3f1 7408{
7409 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7410 machine_mode innermode;
1c58e3f1 7411
7412 /* Doh! What's going on? */
7413 if (mclass != MODE_VECTOR_INT
7414 && mclass != MODE_VECTOR_FLOAT
7415 && mclass != MODE_VECTOR_FRACT
7416 && mclass != MODE_VECTOR_UFRACT
7417 && mclass != MODE_VECTOR_ACCUM
7418 && mclass != MODE_VECTOR_UACCUM)
7419 return 0;
7420
7421 /* Hardware support. Woo hoo! */
7422 if (targetm.vector_mode_supported_p (mode))
7423 return 1;
7424
7425 innermode = GET_MODE_INNER (mode);
7426
7427 /* We should probably return 1 if requesting V4DI and we have no DI,
7428 but we have V2DI, but this is probably very unlikely. */
7429
7430 /* If we have support for the inner mode, we can safely emulate it.
7431 We may not have V2DI, but me can emulate with a pair of DIs. */
7432 return targetm.scalar_mode_supported_p (innermode);
7433}
7434
7435
f8e93a2e 7436/* Handle a "mode" attribute; arguments as in
7437 struct attribute_spec.handler. */
7438
7439static tree
9a03a746 7440handle_mode_attribute (tree *node, tree name, tree args,
7441 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7442{
7443 tree type = *node;
d1dd9ac0 7444 tree ident = TREE_VALUE (args);
f8e93a2e 7445
7446 *no_add_attrs = true;
ab2c1de8 7447
d1dd9ac0 7448 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7449 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7450 else
7451 {
7452 int j;
d1dd9ac0 7453 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7454 int len = strlen (p);
3754d046 7455 machine_mode mode = VOIDmode;
f8e93a2e 7456 tree typefm;
b2aef146 7457 bool valid_mode;
f8e93a2e 7458
7459 if (len > 4 && p[0] == '_' && p[1] == '_'
7460 && p[len - 1] == '_' && p[len - 2] == '_')
7461 {
4fd61bc6 7462 char *newp = (char *) alloca (len - 1);
f8e93a2e 7463
7464 strcpy (newp, &p[2]);
7465 newp[len - 4] = '\0';
7466 p = newp;
7467 }
7468
7469 /* Change this type to have a type with the specified mode.
7470 First check for the special modes. */
84166705 7471 if (!strcmp (p, "byte"))
f8e93a2e 7472 mode = byte_mode;
7473 else if (!strcmp (p, "word"))
7474 mode = word_mode;
84166705 7475 else if (!strcmp (p, "pointer"))
f8e93a2e 7476 mode = ptr_mode;
0ef89dfd 7477 else if (!strcmp (p, "libgcc_cmp_return"))
7478 mode = targetm.libgcc_cmp_return_mode ();
7479 else if (!strcmp (p, "libgcc_shift_count"))
7480 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7481 else if (!strcmp (p, "unwind_word"))
7482 mode = targetm.unwind_word_mode ();
f8e93a2e 7483 else
7484 for (j = 0; j < NUM_MACHINE_MODES; j++)
7485 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7486 {
3754d046 7487 mode = (machine_mode) j;
743a6f47 7488 break;
7489 }
f8e93a2e 7490
7491 if (mode == VOIDmode)
4917c376 7492 {
d1dd9ac0 7493 error ("unknown machine mode %qE", ident);
4917c376 7494 return NULL_TREE;
7495 }
7496
b2aef146 7497 valid_mode = false;
7498 switch (GET_MODE_CLASS (mode))
4917c376 7499 {
b2aef146 7500 case MODE_INT:
7501 case MODE_PARTIAL_INT:
7502 case MODE_FLOAT:
c4503c0a 7503 case MODE_DECIMAL_FLOAT:
9421ebb9 7504 case MODE_FRACT:
7505 case MODE_UFRACT:
7506 case MODE_ACCUM:
7507 case MODE_UACCUM:
b2aef146 7508 valid_mode = targetm.scalar_mode_supported_p (mode);
7509 break;
7510
7511 case MODE_COMPLEX_INT:
7512 case MODE_COMPLEX_FLOAT:
7513 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7514 break;
7515
7516 case MODE_VECTOR_INT:
7517 case MODE_VECTOR_FLOAT:
9421ebb9 7518 case MODE_VECTOR_FRACT:
7519 case MODE_VECTOR_UFRACT:
7520 case MODE_VECTOR_ACCUM:
7521 case MODE_VECTOR_UACCUM:
9b2d6d13 7522 warning (OPT_Wattributes, "specifying vector types with "
7523 "__attribute__ ((mode)) is deprecated");
7524 warning (OPT_Wattributes,
7525 "use __attribute__ ((vector_size)) instead");
b2aef146 7526 valid_mode = vector_mode_valid_p (mode);
7527 break;
4917c376 7528
b2aef146 7529 default:
7530 break;
7531 }
7532 if (!valid_mode)
7533 {
1e5fcbe2 7534 error ("unable to emulate %qs", p);
b2aef146 7535 return NULL_TREE;
7536 }
4917c376 7537
b2aef146 7538 if (POINTER_TYPE_P (type))
ead34f59 7539 {
6d5d708e 7540 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7541 tree (*fn)(tree, machine_mode, bool);
b2aef146 7542
6d5d708e 7543 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7544 {
1e5fcbe2 7545 error ("invalid pointer mode %qs", p);
ead34f59 7546 return NULL_TREE;
7547 }
7548
a0c938f0 7549 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7550 fn = build_pointer_type_for_mode;
805e22b2 7551 else
b2aef146 7552 fn = build_reference_type_for_mode;
7553 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7554 }
b2aef146 7555 else
9421ebb9 7556 {
7557 /* For fixed-point modes, we need to test if the signness of type
7558 and the machine mode are consistent. */
7559 if (ALL_FIXED_POINT_MODE_P (mode)
7560 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7561 {
bf776685 7562 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7563 return NULL_TREE;
7564 }
7565 /* For fixed-point modes, we need to pass saturating info. */
7566 typefm = lang_hooks.types.type_for_mode (mode,
7567 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7568 : TYPE_UNSIGNED (type));
7569 }
3a648ab9 7570
b2aef146 7571 if (typefm == NULL_TREE)
7572 {
743a6f47 7573 error ("no data type for mode %qs", p);
b2aef146 7574 return NULL_TREE;
7575 }
3a648ab9 7576 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7577 {
7578 /* For enumeral types, copy the precision from the integer
7579 type returned above. If not an INTEGER_TYPE, we can't use
7580 this mode for this type. */
7581 if (TREE_CODE (typefm) != INTEGER_TYPE)
7582 {
743a6f47 7583 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7584 return NULL_TREE;
7585 }
7586
10080eac 7587 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7588 {
7589 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7590 typefm = type;
7591 }
7592 else
7593 {
7594 /* We cannot build a type variant, as there's code that assumes
7595 that TYPE_MAIN_VARIANT has the same mode. This includes the
7596 debug generators. Instead, create a subrange type. This
7597 results in all of the enumeral values being emitted only once
7598 in the original, and the subtype gets them by reference. */
7599 if (TYPE_UNSIGNED (type))
7600 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7601 else
7602 typefm = make_signed_type (TYPE_PRECISION (typefm));
7603 TREE_TYPE (typefm) = type;
7604 }
3a648ab9 7605 }
4bf450a1 7606 else if (VECTOR_MODE_P (mode)
7607 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7608 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7609 {
7610 error ("mode %qs applied to inappropriate type", p);
7611 return NULL_TREE;
7612 }
7613
b2aef146 7614 *node = typefm;
f8e93a2e 7615 }
7616
7617 return NULL_TREE;
7618}
7619
7620/* Handle a "section" attribute; arguments as in
7621 struct attribute_spec.handler. */
7622
7623static tree
9a03a746 7624handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7625 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7626{
7627 tree decl = *node;
7628
9866562d 7629 if (!targetm_common.have_named_sections)
f8e93a2e 7630 {
9866562d 7631 error_at (DECL_SOURCE_LOCATION (*node),
7632 "section attributes are not supported for this target");
7633 goto fail;
7634 }
065efcb1 7635
9866562d 7636 user_defined_section_attribute = true;
f8e93a2e 7637
9866562d 7638 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
7639 {
7640 error ("section attribute not allowed for %q+D", *node);
7641 goto fail;
f8e93a2e 7642 }
9866562d 7643
7644 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7645 {
9866562d 7646 error ("section attribute argument not a string constant");
7647 goto fail;
f8e93a2e 7648 }
7649
9866562d 7650 if (TREE_CODE (decl) == VAR_DECL
7651 && current_function_decl != NULL_TREE
7652 && !TREE_STATIC (decl))
7653 {
7654 error_at (DECL_SOURCE_LOCATION (decl),
7655 "section attribute cannot be specified for local variables");
7656 goto fail;
7657 }
7658
7659 /* The decl may have already been given a section attribute
7660 from a previous declaration. Ensure they match. */
7661 if (DECL_SECTION_NAME (decl) != NULL
7662 && strcmp (DECL_SECTION_NAME (decl),
7663 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7664 {
7665 error ("section of %q+D conflicts with previous declaration", *node);
7666 goto fail;
7667 }
7668
7669 if (TREE_CODE (decl) == VAR_DECL
7670 && !targetm.have_tls && targetm.emutls.tmpl_section
7671 && DECL_THREAD_LOCAL_P (decl))
7672 {
7673 error ("section of %q+D cannot be overridden", *node);
7674 goto fail;
7675 }
7676
7677 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7678 return NULL_TREE;
7679
7680fail:
7681 *no_add_attrs = true;
f8e93a2e 7682 return NULL_TREE;
7683}
7684
83e25171 7685/* Check whether ALIGN is a valid user-specified alignment. If so,
7686 return its base-2 log; if not, output an error and return -1. If
7687 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7688 no error. */
7689int
7690check_user_alignment (const_tree align, bool allow_zero)
7691{
7692 int i;
7693
3e5a8b00 7694 if (error_operand_p (align))
7695 return -1;
5abaa10a 7696 if (TREE_CODE (align) != INTEGER_CST
7697 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7698 {
7699 error ("requested alignment is not an integer constant");
7700 return -1;
7701 }
7702 else if (allow_zero && integer_zerop (align))
7703 return -1;
1a087624 7704 else if (tree_int_cst_sgn (align) == -1
7705 || (i = tree_log2 (align)) == -1)
83e25171 7706 {
1a087624 7707 error ("requested alignment is not a positive power of 2");
83e25171 7708 return -1;
7709 }
7710 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7711 {
7712 error ("requested alignment is too large");
7713 return -1;
7714 }
7715 return i;
7716}
7717
ffcdbf9c 7718/*
7719 If in c++-11, check if the c++-11 alignment constraint with respect
7720 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7721 c++-11 mode, does nothing.
7722
7723 [dcl.align]2/ says:
7724
7725 [* if the constant expression evaluates to a fundamental alignment,
7726 the alignment requirement of the declared entity shall be the
7727 specified fundamental alignment.
7728
7729 * if the constant expression evaluates to an extended alignment
7730 and the implementation supports that alignment in the context
7731 of the declaration, the alignment of the declared entity shall
7732 be that alignment
7733
7734 * if the constant expression evaluates to an extended alignment
7735 and the implementation does not support that alignment in the
7736 context of the declaration, the program is ill-formed]. */
7737
7738static bool
7739check_cxx_fundamental_alignment_constraints (tree node,
7740 unsigned align_log,
7741 int flags)
7742{
7743 bool alignment_too_large_p = false;
7744 unsigned requested_alignment = 1U << align_log;
7745 unsigned max_align = 0;
7746
7747 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7748 || (node == NULL_TREE || node == error_mark_node))
7749 return true;
7750
7751 if (cxx_fundamental_alignment_p (requested_alignment))
7752 return true;
7753
7754 if (DECL_P (node))
7755 {
7756 if (TREE_STATIC (node))
7757 {
7758 /* For file scope variables and static members, the target
7759 supports alignments that are at most
7760 MAX_OFILE_ALIGNMENT. */
7761 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7762 alignment_too_large_p = true;
7763 }
7764 else
7765 {
7766#ifdef BIGGEST_FIELD_ALIGNMENT
7767#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7768#else
7769#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7770#endif
7771 /* For non-static members, the target supports either
7772 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7773 if it is defined or BIGGEST_ALIGNMENT. */
7774 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7775 if (TREE_CODE (node) == FIELD_DECL
7776 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7777 alignment_too_large_p = true;
7778#undef MAX_TARGET_FIELD_ALIGNMENT
7779 /* For stack variables, the target supports at most
7780 MAX_STACK_ALIGNMENT. */
7781 else if (decl_function_context (node) != NULL
7782 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7783 alignment_too_large_p = true;
7784 }
7785 }
7786 else if (TYPE_P (node))
7787 {
7788 /* Let's be liberal for types. */
7789 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7790 alignment_too_large_p = true;
7791 }
7792
7793 if (alignment_too_large_p)
7794 pedwarn (input_location, OPT_Wattributes,
7795 "requested alignment %d is larger than %d",
7796 requested_alignment, max_align);
7797
7798 return !alignment_too_large_p;
7799}
7800
f8e93a2e 7801/* Handle a "aligned" attribute; arguments as in
7802 struct attribute_spec.handler. */
7803
7804static tree
9a03a746 7805handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 7806 int flags, bool *no_add_attrs)
f8e93a2e 7807{
7808 tree decl = NULL_TREE;
7809 tree *type = NULL;
7810 int is_type = 0;
caf62483 7811 tree align_expr;
f8e93a2e 7812 int i;
7813
caf62483 7814 if (args)
7815 {
7816 align_expr = TREE_VALUE (args);
3e5a8b00 7817 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7818 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 7819 align_expr = default_conversion (align_expr);
7820 }
7821 else
7822 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7823
f8e93a2e 7824 if (DECL_P (*node))
7825 {
7826 decl = *node;
7827 type = &TREE_TYPE (decl);
7828 is_type = TREE_CODE (*node) == TYPE_DECL;
7829 }
7830 else if (TYPE_P (*node))
7831 type = node, is_type = 1;
7832
ffcdbf9c 7833 if ((i = check_user_alignment (align_expr, false)) == -1
7834 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 7835 *no_add_attrs = true;
f8e93a2e 7836 else if (is_type)
7837 {
2ec3af9c 7838 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7839 /* OK, modify the type in place. */;
f8e93a2e 7840 /* If we have a TYPE_DECL, then copy the type, so that we
7841 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 7842 else if (decl && TREE_TYPE (decl) != error_mark_node
7843 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 7844 {
7845 tree tt = TREE_TYPE (decl);
e086912e 7846 *type = build_variant_type_copy (*type);
f8e93a2e 7847 DECL_ORIGINAL_TYPE (decl) = tt;
7848 TYPE_NAME (*type) = decl;
7849 TREE_USED (*type) = TREE_USED (decl);
7850 TREE_TYPE (decl) = *type;
7851 }
2ec3af9c 7852 else
e086912e 7853 *type = build_variant_type_copy (*type);
f8e93a2e 7854
7cfdc2f0 7855 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7856 TYPE_USER_ALIGN (*type) = 1;
7857 }
097b5c8b 7858 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7859 && TREE_CODE (decl) != FIELD_DECL)
7860 {
3cf8b391 7861 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 7862 *no_add_attrs = true;
7863 }
ffcdbf9c 7864 else if (DECL_USER_ALIGN (decl)
7865 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7866 /* C++-11 [dcl.align/4]:
7867
7868 When multiple alignment-specifiers are specified for an
7869 entity, the alignment requirement shall be set to the
7870 strictest specified alignment.
7871
7872 This formally comes from the c++11 specification but we are
7873 doing it for the GNU attribute syntax as well. */
7874 *no_add_attrs = true;
097b5c8b 7875 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 7876 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 7877 {
7878 if (DECL_USER_ALIGN (decl))
7879 error ("alignment for %q+D was previously specified as %d "
7880 "and may not be decreased", decl,
7881 DECL_ALIGN (decl) / BITS_PER_UNIT);
7882 else
7883 error ("alignment for %q+D must be at least %d", decl,
7884 DECL_ALIGN (decl) / BITS_PER_UNIT);
7885 *no_add_attrs = true;
7886 }
f8e93a2e 7887 else
7888 {
7cfdc2f0 7889 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7890 DECL_USER_ALIGN (decl) = 1;
7891 }
7892
7893 return NULL_TREE;
7894}
7895
7896/* Handle a "weak" attribute; arguments as in
7897 struct attribute_spec.handler. */
7898
7899static tree
f948b309 7900handle_weak_attribute (tree *node, tree name,
9a03a746 7901 tree ARG_UNUSED (args),
7902 int ARG_UNUSED (flags),
7903 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 7904{
f948b309 7905 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 7906 && DECL_DECLARED_INLINE_P (*node))
7907 {
0725e25c 7908 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 7909 *no_add_attrs = true;
7910 }
85c0a25c 7911 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7912 {
7913 error ("indirect function %q+D cannot be declared weak", *node);
7914 *no_add_attrs = true;
7915 return NULL_TREE;
7916 }
059a60f3 7917 else if (TREE_CODE (*node) == FUNCTION_DECL
7918 || TREE_CODE (*node) == VAR_DECL)
c11b875d 7919 {
7920 struct symtab_node *n = symtab_node::get (*node);
7921 if (n && n->refuse_visibility_changes)
7922 error ("%+D declared weak after being used", *node);
7923 declare_weak (*node);
7924 }
f948b309 7925 else
7926 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7927
7928 return NULL_TREE;
7929}
7930
85c0a25c 7931/* Handle an "alias" or "ifunc" attribute; arguments as in
7932 struct attribute_spec.handler, except that IS_ALIAS tells us
7933 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 7934
7935static tree
85c0a25c 7936handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7937 bool *no_add_attrs)
f8e93a2e 7938{
7939 tree decl = *node;
7940
85c0a25c 7941 if (TREE_CODE (decl) != FUNCTION_DECL
7942 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
9e830260 7943 {
7944 warning (OPT_Wattributes, "%qE attribute ignored", name);
7945 *no_add_attrs = true;
7946 }
7947 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 7948 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7949 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7950 /* A static variable declaration is always a tentative definition,
7951 but the alias is a non-tentative definition which overrides. */
48e1416a 7952 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7953 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 7954 {
85c0a25c 7955 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 7956 *no_add_attrs = true;
85c0a25c 7957 return NULL_TREE;
f8e93a2e 7958 }
85c0a25c 7959 else if (!is_alias
7960 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7961 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7962 {
7963 error ("weak %q+D cannot be defined %qE", decl, name);
7964 *no_add_attrs = true;
7965 return NULL_TREE;
7966 }
8c42f0d9 7967
7968 /* Note that the very first time we process a nested declaration,
7969 decl_function_context will not be set. Indeed, *would* never
7970 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7971 we do below. After such frobbery, pushdecl would set the context.
7972 In any case, this is never what we want. */
7973 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 7974 {
7975 tree id;
7976
7977 id = TREE_VALUE (args);
7978 if (TREE_CODE (id) != STRING_CST)
7979 {
85c0a25c 7980 error ("attribute %qE argument not a string", name);
f8e93a2e 7981 *no_add_attrs = true;
7982 return NULL_TREE;
7983 }
7984 id = get_identifier (TREE_STRING_POINTER (id));
7985 /* This counts as a use of the object pointed to. */
7986 TREE_USED (id) = 1;
7987
7988 if (TREE_CODE (decl) == FUNCTION_DECL)
7989 DECL_INITIAL (decl) = error_mark_node;
7990 else
f2526cce 7991 TREE_STATIC (decl) = 1;
85c0a25c 7992
7993 if (!is_alias)
7994 /* ifuncs are also aliases, so set that attribute too. */
7995 DECL_ATTRIBUTES (decl)
7996 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 7997 }
7998 else
7999 {
9b2d6d13 8000 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8001 *no_add_attrs = true;
8002 }
8003
8e857c41 8004 if (decl_in_symtab_p (*node))
8005 {
8006 struct symtab_node *n = symtab_node::get (decl);
8007 if (n && n->refuse_visibility_changes)
8008 {
8009 if (is_alias)
8010 error ("%+D declared alias after being used", decl);
8011 else
8012 error ("%+D declared ifunc after being used", decl);
8013 }
8014 }
8015
8016
f8e93a2e 8017 return NULL_TREE;
8018}
8019
85c0a25c 8020/* Handle an "alias" or "ifunc" attribute; arguments as in
8021 struct attribute_spec.handler. */
8022
8023static tree
8024handle_ifunc_attribute (tree *node, tree name, tree args,
8025 int ARG_UNUSED (flags), bool *no_add_attrs)
8026{
8027 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8028}
8029
8030/* Handle an "alias" or "ifunc" attribute; arguments as in
8031 struct attribute_spec.handler. */
8032
8033static tree
8034handle_alias_attribute (tree *node, tree name, tree args,
8035 int ARG_UNUSED (flags), bool *no_add_attrs)
8036{
8037 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8038}
8039
f4a30bd7 8040/* Handle a "weakref" attribute; arguments as in struct
8041 attribute_spec.handler. */
8042
8043static tree
8044handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8045 int flags, bool *no_add_attrs)
8046{
8047 tree attr = NULL_TREE;
8048
83852912 8049 /* We must ignore the attribute when it is associated with
8050 local-scoped decls, since attribute alias is ignored and many
8051 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8052 if (decl_function_context (*node)
8053 || current_function_decl
8054 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
83852912 8055 {
8056 warning (OPT_Wattributes, "%qE attribute ignored", name);
8057 *no_add_attrs = true;
8058 return NULL_TREE;
8059 }
8060
85c0a25c 8061 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8062 {
8063 error ("indirect function %q+D cannot be declared weakref", *node);
8064 *no_add_attrs = true;
8065 return NULL_TREE;
8066 }
8067
f4a30bd7 8068 /* The idea here is that `weakref("name")' mutates into `weakref,
8069 alias("name")', and weakref without arguments, in turn,
8070 implicitly adds weak. */
8071
8072 if (args)
8073 {
8074 attr = tree_cons (get_identifier ("alias"), args, attr);
8075 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8076
8077 *no_add_attrs = true;
0a3ecdc1 8078
8079 decl_attributes (node, attr, flags);
f4a30bd7 8080 }
8081 else
8082 {
8083 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8084 error_at (DECL_SOURCE_LOCATION (*node),
8085 "weakref attribute must appear before alias attribute");
f4a30bd7 8086
0a3ecdc1 8087 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8088 and that isn't supported; and because it wants to add it to
8089 the list of weak decls, which isn't helpful. */
8090 DECL_WEAK (*node) = 1;
f4a30bd7 8091 }
8092
8e857c41 8093 if (decl_in_symtab_p (*node))
8094 {
8095 struct symtab_node *n = symtab_node::get (*node);
8096 if (n && n->refuse_visibility_changes)
8097 error ("%+D declared weakref after being used", *node);
8098 }
8099
f4a30bd7 8100 return NULL_TREE;
8101}
8102
f8e93a2e 8103/* Handle an "visibility" attribute; arguments as in
8104 struct attribute_spec.handler. */
8105
8106static tree
1cae46be 8107handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8108 int ARG_UNUSED (flags),
4a2849cb 8109 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8110{
8111 tree decl = *node;
9c40570a 8112 tree id = TREE_VALUE (args);
4a2849cb 8113 enum symbol_visibility vis;
f8e93a2e 8114
b212f378 8115 if (TYPE_P (*node))
8116 {
4a2849cb 8117 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8118 /* OK */;
8119 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8120 {
8121 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8122 name);
8123 return NULL_TREE;
8124 }
8125 else if (TYPE_FIELDS (*node))
8126 {
8127 error ("%qE attribute ignored because %qT is already defined",
8128 name, *node);
8129 return NULL_TREE;
8130 }
b212f378 8131 }
84166705 8132 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8133 {
9b2d6d13 8134 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8135 return NULL_TREE;
f8e93a2e 8136 }
f8e93a2e 8137
9c40570a 8138 if (TREE_CODE (id) != STRING_CST)
8139 {
07e3a3d2 8140 error ("visibility argument not a string");
9c40570a 8141 return NULL_TREE;
f8e93a2e 8142 }
b27ac6b5 8143
b212f378 8144 /* If this is a type, set the visibility on the type decl. */
8145 if (TYPE_P (decl))
8146 {
8147 decl = TYPE_NAME (decl);
84166705 8148 if (!decl)
a0c938f0 8149 return NULL_TREE;
e147aab3 8150 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8151 {
9b2d6d13 8152 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8153 name);
8154 return NULL_TREE;
8155 }
b212f378 8156 }
f8e93a2e 8157
9c40570a 8158 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8159 vis = VISIBILITY_DEFAULT;
9c40570a 8160 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8161 vis = VISIBILITY_INTERNAL;
9c40570a 8162 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8163 vis = VISIBILITY_HIDDEN;
9c40570a 8164 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8165 vis = VISIBILITY_PROTECTED;
9c40570a 8166 else
4a2849cb 8167 {
8168 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8169 vis = VISIBILITY_DEFAULT;
8170 }
8171
8172 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8173 && vis != DECL_VISIBILITY (decl))
8174 {
8175 tree attributes = (TYPE_P (*node)
8176 ? TYPE_ATTRIBUTES (*node)
8177 : DECL_ATTRIBUTES (decl));
8178 if (lookup_attribute ("visibility", attributes))
8179 error ("%qD redeclared with different visibility", decl);
8180 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8181 && lookup_attribute ("dllimport", attributes))
8182 error ("%qD was declared %qs which implies default visibility",
8183 decl, "dllimport");
8184 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8185 && lookup_attribute ("dllexport", attributes))
8186 error ("%qD was declared %qs which implies default visibility",
8187 decl, "dllexport");
8188 }
4a2849cb 8189
8190 DECL_VISIBILITY (decl) = vis;
b212f378 8191 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8192
4a2849cb 8193 /* Go ahead and attach the attribute to the node as well. This is needed
8194 so we can determine whether we have VISIBILITY_DEFAULT because the
8195 visibility was not specified, or because it was explicitly overridden
8196 from the containing scope. */
9c40570a 8197
f8e93a2e 8198 return NULL_TREE;
8199}
8200
3aa0c315 8201/* Determine the ELF symbol visibility for DECL, which is either a
8202 variable or a function. It is an error to use this function if a
8203 definition of DECL is not available in this translation unit.
8204 Returns true if the final visibility has been determined by this
8205 function; false if the caller is free to make additional
8206 modifications. */
8207
8208bool
8209c_determine_visibility (tree decl)
8210{
1d8fc210 8211 gcc_assert (TREE_CODE (decl) == VAR_DECL
8212 || TREE_CODE (decl) == FUNCTION_DECL);
3aa0c315 8213
8214 /* If the user explicitly specified the visibility with an
8215 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8216 the processing of the attribute. We check for an explicit
8217 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8218 to distinguish the use of an attribute from the use of a "#pragma
8219 GCC visibility push(...)"; in the latter case we still want other
8220 considerations to be able to overrule the #pragma. */
8221 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8222 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8223 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8224 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8225 return true;
8226
4a2849cb 8227 /* Set default visibility to whatever the user supplied with
8228 visibility_specified depending on #pragma GCC visibility. */
8229 if (!DECL_VISIBILITY_SPECIFIED (decl))
8230 {
2d9d8740 8231 if (visibility_options.inpragma
8232 || DECL_VISIBILITY (decl) != default_visibility)
8233 {
8234 DECL_VISIBILITY (decl) = default_visibility;
8235 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8236 /* If visibility changed and DECL already has DECL_RTL, ensure
8237 symbol flags are updated. */
8238 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8239 || TREE_CODE (decl) == FUNCTION_DECL)
8240 && DECL_RTL_SET_P (decl))
8241 make_decl_rtl (decl);
8242 }
4a2849cb 8243 }
3aa0c315 8244 return false;
8245}
8246
24dfead4 8247/* Handle an "tls_model" attribute; arguments as in
8248 struct attribute_spec.handler. */
8249
8250static tree
1cae46be 8251handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8252 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8253{
1b53eb20 8254 tree id;
24dfead4 8255 tree decl = *node;
1b53eb20 8256 enum tls_model kind;
24dfead4 8257
1b53eb20 8258 *no_add_attrs = true;
8259
c34f8a78 8260 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8261 {
9b2d6d13 8262 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8263 return NULL_TREE;
24dfead4 8264 }
24dfead4 8265
1b53eb20 8266 kind = DECL_TLS_MODEL (decl);
8267 id = TREE_VALUE (args);
8268 if (TREE_CODE (id) != STRING_CST)
8269 {
8270 error ("tls_model argument not a string");
8271 return NULL_TREE;
24dfead4 8272 }
8273
1b53eb20 8274 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8275 kind = TLS_MODEL_LOCAL_EXEC;
8276 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8277 kind = TLS_MODEL_INITIAL_EXEC;
8278 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8279 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8280 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8281 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8282 else
8283 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8284
5e68df57 8285 set_decl_tls_model (decl, kind);
24dfead4 8286 return NULL_TREE;
8287}
8288
f8e93a2e 8289/* Handle a "no_instrument_function" attribute; arguments as in
8290 struct attribute_spec.handler. */
8291
8292static tree
1cae46be 8293handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8294 tree ARG_UNUSED (args),
8295 int ARG_UNUSED (flags),
09347743 8296 bool *no_add_attrs)
f8e93a2e 8297{
8298 tree decl = *node;
8299
8300 if (TREE_CODE (decl) != FUNCTION_DECL)
8301 {
712d2297 8302 error_at (DECL_SOURCE_LOCATION (decl),
8303 "%qE attribute applies only to functions", name);
f8e93a2e 8304 *no_add_attrs = true;
8305 }
f8e93a2e 8306 else
8307 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8308
8309 return NULL_TREE;
8310}
8311
8312/* Handle a "malloc" attribute; arguments as in
8313 struct attribute_spec.handler. */
8314
8315static tree
9a03a746 8316handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8317 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8318{
a5147fca 8319 if (TREE_CODE (*node) == FUNCTION_DECL
8320 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8321 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8322 else
8323 {
9b2d6d13 8324 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8325 *no_add_attrs = true;
8326 }
8327
8328 return NULL_TREE;
8329}
8330
4a29c97c 8331/* Handle a "alloc_size" attribute; arguments as in
8332 struct attribute_spec.handler. */
8333
8334static tree
8335handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8336 int ARG_UNUSED (flags), bool *no_add_attrs)
8337{
2802826e 8338 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8339 for (; args; args = TREE_CHAIN (args))
8340 {
8341 tree position = TREE_VALUE (args);
caf62483 8342 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8343 && TREE_CODE (position) != FUNCTION_DECL)
8344 position = default_conversion (position);
4a29c97c 8345
237e78b1 8346 if (!tree_fits_uhwi_p (position)
8347 || !arg_count
8348 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8349 {
48e1416a 8350 warning (OPT_Wattributes,
4a29c97c 8351 "alloc_size parameter outside range");
8352 *no_add_attrs = true;
8353 return NULL_TREE;
8354 }
8355 }
8356 return NULL_TREE;
8357}
8358
237e78b1 8359/* Handle a "alloc_align" attribute; arguments as in
8360 struct attribute_spec.handler. */
8361
8362static tree
8363handle_alloc_align_attribute (tree *node, tree, tree args, int,
8364 bool *no_add_attrs)
8365{
8366 unsigned arg_count = type_num_arguments (*node);
8367 tree position = TREE_VALUE (args);
8368 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8369 position = default_conversion (position);
8370
8371 if (!tree_fits_uhwi_p (position)
8372 || !arg_count
8373 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8374 {
8375 warning (OPT_Wattributes,
8376 "alloc_align parameter outside range");
8377 *no_add_attrs = true;
8378 return NULL_TREE;
8379 }
8380 return NULL_TREE;
8381}
8382
8383/* Handle a "assume_aligned" attribute; arguments as in
8384 struct attribute_spec.handler. */
8385
8386static tree
8387handle_assume_aligned_attribute (tree *, tree, tree args, int,
8388 bool *no_add_attrs)
8389{
8390 for (; args; args = TREE_CHAIN (args))
8391 {
8392 tree position = TREE_VALUE (args);
8393 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8394 && TREE_CODE (position) != FUNCTION_DECL)
8395 position = default_conversion (position);
8396
8397 if (TREE_CODE (position) != INTEGER_CST)
8398 {
8399 warning (OPT_Wattributes,
8400 "assume_aligned parameter not integer constant");
8401 *no_add_attrs = true;
8402 return NULL_TREE;
8403 }
8404 }
8405 return NULL_TREE;
8406}
8407
8ce86007 8408/* Handle a "fn spec" attribute; arguments as in
8409 struct attribute_spec.handler. */
8410
8411static tree
8412handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8413 tree args, int ARG_UNUSED (flags),
8414 bool *no_add_attrs ATTRIBUTE_UNUSED)
8415{
8416 gcc_assert (args
8417 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8418 && !TREE_CHAIN (args));
8419 return NULL_TREE;
8420}
8421
058a1b7a 8422/* Handle a "bnd_variable_size" attribute; arguments as in
8423 struct attribute_spec.handler. */
8424
8425static tree
8426handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8427 int ARG_UNUSED (flags), bool *no_add_attrs)
8428{
8429 if (TREE_CODE (*node) != FIELD_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_legacy" attribute; arguments as in
8439 struct attribute_spec.handler. */
8440
8441static tree
8442handle_bnd_legacy (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
8454/* Handle a "bnd_instrument" attribute; arguments as in
8455 struct attribute_spec.handler. */
8456
8457static tree
8458handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8459 int ARG_UNUSED (flags), bool *no_add_attrs)
8460{
8461 if (TREE_CODE (*node) != FUNCTION_DECL)
8462 {
8463 warning (OPT_Wattributes, "%qE attribute ignored", name);
8464 *no_add_attrs = true;
8465 }
8466
8467 return NULL_TREE;
8468}
8469
a96c3cc1 8470/* Handle a "warn_unused" attribute; arguments as in
8471 struct attribute_spec.handler. */
8472
8473static tree
8474handle_warn_unused_attribute (tree *node, tree name,
8475 tree args ATTRIBUTE_UNUSED,
8476 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8477{
8478 if (TYPE_P (*node))
8479 /* Do nothing else, just set the attribute. We'll get at
8480 it later with lookup_attribute. */
8481 ;
8482 else
8483 {
8484 warning (OPT_Wattributes, "%qE attribute ignored", name);
8485 *no_add_attrs = true;
8486 }
8487
8488 return NULL_TREE;
8489}
8490
bc7bff74 8491/* Handle an "omp declare simd" attribute; arguments as in
8492 struct attribute_spec.handler. */
8493
8494static tree
8495handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8496{
8497 return NULL_TREE;
8498}
8499
8500/* Handle an "omp declare target" attribute; arguments as in
8501 struct attribute_spec.handler. */
8502
8503static tree
8504handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8505{
8506 return NULL_TREE;
8507}
8508
26d1c5ff 8509/* Handle a "returns_twice" attribute; arguments as in
8510 struct attribute_spec.handler. */
8511
8512static tree
8513handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8514 int ARG_UNUSED (flags), bool *no_add_attrs)
8515{
8516 if (TREE_CODE (*node) == FUNCTION_DECL)
8517 DECL_IS_RETURNS_TWICE (*node) = 1;
8518 else
8519 {
9b2d6d13 8520 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8521 *no_add_attrs = true;
8522 }
8523
8524 return NULL_TREE;
8525}
8526
f8e93a2e 8527/* Handle a "no_limit_stack" attribute; arguments as in
8528 struct attribute_spec.handler. */
8529
8530static tree
1cae46be 8531handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8532 tree ARG_UNUSED (args),
8533 int ARG_UNUSED (flags),
09347743 8534 bool *no_add_attrs)
f8e93a2e 8535{
8536 tree decl = *node;
8537
8538 if (TREE_CODE (decl) != FUNCTION_DECL)
8539 {
712d2297 8540 error_at (DECL_SOURCE_LOCATION (decl),
8541 "%qE attribute applies only to functions", name);
f8e93a2e 8542 *no_add_attrs = true;
8543 }
8544 else if (DECL_INITIAL (decl))
8545 {
712d2297 8546 error_at (DECL_SOURCE_LOCATION (decl),
8547 "can%'t set %qE attribute after definition", name);
f8e93a2e 8548 *no_add_attrs = true;
8549 }
8550 else
8551 DECL_NO_LIMIT_STACK (decl) = 1;
8552
8553 return NULL_TREE;
8554}
8555
8556/* Handle a "pure" attribute; arguments as in
8557 struct attribute_spec.handler. */
8558
8559static tree
9a03a746 8560handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8561 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8562{
8563 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8564 DECL_PURE_P (*node) = 1;
f8e93a2e 8565 /* ??? TODO: Support types. */
8566 else
8567 {
9b2d6d13 8568 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8569 *no_add_attrs = true;
8570 }
8571
8572 return NULL_TREE;
8573}
8574
4c0315d0 8575/* Digest an attribute list destined for a transactional memory statement.
8576 ALLOWED is the set of attributes that are allowed for this statement;
8577 return the attribute we parsed. Multiple attributes are never allowed. */
8578
8579int
8580parse_tm_stmt_attr (tree attrs, int allowed)
8581{
8582 tree a_seen = NULL;
8583 int m_seen = 0;
8584
8585 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8586 {
8587 tree a = TREE_PURPOSE (attrs);
8588 int m = 0;
8589
8590 if (is_attribute_p ("outer", a))
8591 m = TM_STMT_ATTR_OUTER;
8592
8593 if ((m & allowed) == 0)
8594 {
8595 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8596 continue;
8597 }
8598
8599 if (m_seen == 0)
8600 {
8601 a_seen = a;
8602 m_seen = m;
8603 }
8604 else if (m_seen == m)
8605 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8606 else
8607 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8608 }
8609
8610 return m_seen;
8611}
8612
8613/* Transform a TM attribute name into a maskable integer and back.
8614 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8615 to how the lack of an attribute is treated. */
8616
8617int
8618tm_attr_to_mask (tree attr)
8619{
8620 if (attr == NULL)
8621 return 0;
8622 if (is_attribute_p ("transaction_safe", attr))
8623 return TM_ATTR_SAFE;
8624 if (is_attribute_p ("transaction_callable", attr))
8625 return TM_ATTR_CALLABLE;
8626 if (is_attribute_p ("transaction_pure", attr))
8627 return TM_ATTR_PURE;
8628 if (is_attribute_p ("transaction_unsafe", attr))
8629 return TM_ATTR_IRREVOCABLE;
8630 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8631 return TM_ATTR_MAY_CANCEL_OUTER;
8632 return 0;
8633}
8634
8635tree
8636tm_mask_to_attr (int mask)
8637{
8638 const char *str;
8639 switch (mask)
8640 {
8641 case TM_ATTR_SAFE:
8642 str = "transaction_safe";
8643 break;
8644 case TM_ATTR_CALLABLE:
8645 str = "transaction_callable";
8646 break;
8647 case TM_ATTR_PURE:
8648 str = "transaction_pure";
8649 break;
8650 case TM_ATTR_IRREVOCABLE:
8651 str = "transaction_unsafe";
8652 break;
8653 case TM_ATTR_MAY_CANCEL_OUTER:
8654 str = "transaction_may_cancel_outer";
8655 break;
8656 default:
8657 gcc_unreachable ();
8658 }
8659 return get_identifier (str);
8660}
8661
8662/* Return the first TM attribute seen in LIST. */
8663
8664tree
8665find_tm_attribute (tree list)
8666{
8667 for (; list ; list = TREE_CHAIN (list))
8668 {
8669 tree name = TREE_PURPOSE (list);
8670 if (tm_attr_to_mask (name) != 0)
8671 return name;
8672 }
8673 return NULL_TREE;
8674}
8675
8676/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8677 Here we accept only function types, and verify that none of the other
8678 function TM attributes are also applied. */
8679/* ??? We need to accept class types for C++, but not C. This greatly
8680 complicates this function, since we can no longer rely on the extra
8681 processing given by function_type_required. */
8682
8683static tree
8684handle_tm_attribute (tree *node, tree name, tree args,
8685 int flags, bool *no_add_attrs)
8686{
8687 /* Only one path adds the attribute; others don't. */
8688 *no_add_attrs = true;
8689
8690 switch (TREE_CODE (*node))
8691 {
8692 case RECORD_TYPE:
8693 case UNION_TYPE:
8694 /* Only tm_callable and tm_safe apply to classes. */
8695 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8696 goto ignored;
8697 /* FALLTHRU */
8698
8699 case FUNCTION_TYPE:
8700 case METHOD_TYPE:
8701 {
8702 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8703 if (old_name == name)
8704 ;
8705 else if (old_name != NULL_TREE)
8706 error ("type was previously declared %qE", old_name);
8707 else
8708 *no_add_attrs = false;
8709 }
8710 break;
8711
8712 case POINTER_TYPE:
8713 {
8714 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8715 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8716 {
8717 tree fn_tmp = TREE_TYPE (*node);
8718 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8719 *node = build_pointer_type (fn_tmp);
8720 break;
8721 }
8722 }
8723 /* FALLTHRU */
8724
8725 default:
8726 /* If a function is next, pass it on to be tried next. */
8727 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8728 return tree_cons (name, args, NULL);
8729
8730 ignored:
8731 warning (OPT_Wattributes, "%qE attribute ignored", name);
8732 break;
8733 }
8734
8735 return NULL_TREE;
8736}
8737
8738/* Handle the TM_WRAP attribute; arguments as in
8739 struct attribute_spec.handler. */
8740
8741static tree
8742handle_tm_wrap_attribute (tree *node, tree name, tree args,
8743 int ARG_UNUSED (flags), bool *no_add_attrs)
8744{
8745 tree decl = *node;
8746
8747 /* We don't need the attribute even on success, since we
8748 record the entry in an external table. */
8749 *no_add_attrs = true;
8750
8751 if (TREE_CODE (decl) != FUNCTION_DECL)
8752 warning (OPT_Wattributes, "%qE attribute ignored", name);
8753 else
8754 {
8755 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 8756 if (error_operand_p (wrap_decl))
8757 ;
8758 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8759 && TREE_CODE (wrap_decl) != VAR_DECL
8760 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
4c0315d0 8761 error ("%qE argument not an identifier", name);
8762 else
8763 {
8764 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8765 wrap_decl = lookup_name (wrap_decl);
8766 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8767 {
8768 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8769 TREE_TYPE (wrap_decl)))
8770 record_tm_replacement (wrap_decl, decl);
8771 else
8772 error ("%qD is not compatible with %qD", wrap_decl, decl);
8773 }
8774 else
cdf34fca 8775 error ("%qE argument is not a function", name);
4c0315d0 8776 }
8777 }
8778
8779 return NULL_TREE;
8780}
8781
8782/* Ignore the given attribute. Used when this attribute may be usefully
8783 overridden by the target, but is not used generically. */
8784
8785static tree
8786ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8787 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8788 bool *no_add_attrs)
8789{
8790 *no_add_attrs = true;
8791 return NULL_TREE;
8792}
8793
fc09b200 8794/* Handle a "no vops" attribute; arguments as in
8795 struct attribute_spec.handler. */
8796
8797static tree
8798handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8799 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8800 bool *ARG_UNUSED (no_add_attrs))
8801{
8802 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8803 DECL_IS_NOVOPS (*node) = 1;
8804 return NULL_TREE;
8805}
8806
f8e93a2e 8807/* Handle a "deprecated" attribute; arguments as in
8808 struct attribute_spec.handler. */
1cae46be 8809
f8e93a2e 8810static tree
1cae46be 8811handle_deprecated_attribute (tree *node, tree name,
45c4e798 8812 tree args, int flags,
09347743 8813 bool *no_add_attrs)
f8e93a2e 8814{
8815 tree type = NULL_TREE;
8816 int warn = 0;
782858b8 8817 tree what = NULL_TREE;
1cae46be 8818
45c4e798 8819 if (!args)
8820 *no_add_attrs = true;
8821 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8822 {
8823 error ("deprecated message is not a string");
8824 *no_add_attrs = true;
8825 }
8826
f8e93a2e 8827 if (DECL_P (*node))
8828 {
8829 tree decl = *node;
8830 type = TREE_TYPE (decl);
1cae46be 8831
f8e93a2e 8832 if (TREE_CODE (decl) == TYPE_DECL
8833 || TREE_CODE (decl) == PARM_DECL
8834 || TREE_CODE (decl) == VAR_DECL
8835 || TREE_CODE (decl) == FUNCTION_DECL
40c8d1dd 8836 || TREE_CODE (decl) == FIELD_DECL
8837 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 8838 TREE_DEPRECATED (decl) = 1;
8839 else
8840 warn = 1;
8841 }
8842 else if (TYPE_P (*node))
8843 {
8844 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 8845 *node = build_variant_type_copy (*node);
f8e93a2e 8846 TREE_DEPRECATED (*node) = 1;
8847 type = *node;
8848 }
8849 else
8850 warn = 1;
1cae46be 8851
f8e93a2e 8852 if (warn)
8853 {
8854 *no_add_attrs = true;
8855 if (type && TYPE_NAME (type))
8856 {
8857 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 8858 what = TYPE_NAME (*node);
f8e93a2e 8859 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8860 && DECL_NAME (TYPE_NAME (type)))
782858b8 8861 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 8862 }
8863 if (what)
9b2d6d13 8864 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 8865 else
9b2d6d13 8866 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8867 }
8868
8869 return NULL_TREE;
8870}
8871
f8e93a2e 8872/* Handle a "vector_size" attribute; arguments as in
8873 struct attribute_spec.handler. */
8874
8875static tree
1cae46be 8876handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 8877 int ARG_UNUSED (flags),
09347743 8878 bool *no_add_attrs)
f8e93a2e 8879{
8880 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 8881 machine_mode orig_mode;
4917c376 8882 tree type = *node, new_type, size;
f8e93a2e 8883
8884 *no_add_attrs = true;
8885
4917c376 8886 size = TREE_VALUE (args);
3e5a8b00 8887 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8888 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 8889 size = default_conversion (size);
4917c376 8890
e913b5cd 8891 if (!tree_fits_uhwi_p (size))
f8e93a2e 8892 {
9b2d6d13 8893 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8894 return NULL_TREE;
8895 }
8896
8897 /* Get the vector size (in bytes). */
e913b5cd 8898 vecsize = tree_to_uhwi (size);
f8e93a2e 8899
8900 /* We need to provide for vector pointers, vector arrays, and
8901 functions returning vectors. For example:
8902
8903 __attribute__((vector_size(16))) short *foo;
8904
8905 In this case, the mode is SI, but the type being modified is
8906 HI, so we need to look further. */
8907
8908 while (POINTER_TYPE_P (type)
8909 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 8910 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 8911 || TREE_CODE (type) == ARRAY_TYPE
8912 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 8913 type = TREE_TYPE (type);
8914
8915 /* Get the mode of the type being modified. */
8916 orig_mode = TYPE_MODE (type);
8917
2cb0e5d9 8918 if ((!INTEGRAL_TYPE_P (type)
8919 && !SCALAR_FLOAT_TYPE_P (type)
8920 && !FIXED_POINT_TYPE_P (type))
cee7491d 8921 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 8922 && GET_MODE_CLASS (orig_mode) != MODE_INT
8923 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 8924 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 8925 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 8926 {
782858b8 8927 error ("invalid vector type for attribute %qE", name);
f8e93a2e 8928 return NULL_TREE;
8929 }
8930
e913b5cd 8931 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 8932 {
8933 error ("vector size not an integral multiple of component size");
8934 return NULL;
8935 }
8936
8937 if (vecsize == 0)
8938 {
8939 error ("zero vector size");
8940 return NULL;
8941 }
8942
f8e93a2e 8943 /* Calculate how many units fit in the vector. */
e913b5cd 8944 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 8945 if (nunits & (nunits - 1))
f8e93a2e 8946 {
83e2a11b 8947 error ("number of components of the vector not a power of two");
f8e93a2e 8948 return NULL_TREE;
8949 }
8950
83e2a11b 8951 new_type = build_vector_type (type, nunits);
f8e93a2e 8952
8953 /* Build back pointers if needed. */
d991e6e8 8954 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 8955
8956 return NULL_TREE;
8957}
8958
dbf6c367 8959/* Handle the "nonnull" attribute. */
8960static tree
9a03a746 8961handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8962 tree args, int ARG_UNUSED (flags),
09347743 8963 bool *no_add_attrs)
dbf6c367 8964{
8965 tree type = *node;
8966 unsigned HOST_WIDE_INT attr_arg_num;
8967
8968 /* If no arguments are specified, all pointer arguments should be
d716ce75 8969 non-null. Verify a full prototype is given so that the arguments
dbf6c367 8970 will have the correct types when we actually check them later. */
84166705 8971 if (!args)
dbf6c367 8972 {
a36cf284 8973 if (!prototype_p (type))
dbf6c367 8974 {
8975 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 8976 *no_add_attrs = true;
dbf6c367 8977 }
8978 return NULL_TREE;
8979 }
8980
8981 /* Argument list specified. Verify that each argument number references
8982 a pointer argument. */
caf62483 8983 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 8984 {
4ee9c684 8985 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 8986
caf62483 8987 tree arg = TREE_VALUE (args);
8988 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8989 && TREE_CODE (arg) != FUNCTION_DECL)
8990 arg = default_conversion (arg);
8991
8992 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 8993 {
07e3a3d2 8994 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 8995 (unsigned long) attr_arg_num);
8996 *no_add_attrs = true;
8997 return NULL_TREE;
8998 }
8999
d0af78c5 9000 if (prototype_p (type))
dbf6c367 9001 {
d0af78c5 9002 function_args_iterator iter;
9003 tree argument;
9004
9005 function_args_iter_init (&iter, type);
9006 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9007 {
d0af78c5 9008 argument = function_args_iter_cond (&iter);
9009 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9010 break;
dbf6c367 9011 }
9012
84166705 9013 if (!argument
d0af78c5 9014 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9015 {
07e3a3d2 9016 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9017 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9018 *no_add_attrs = true;
9019 return NULL_TREE;
9020 }
9021
d0af78c5 9022 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9023 {
07e3a3d2 9024 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9025 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9026 *no_add_attrs = true;
9027 return NULL_TREE;
9028 }
9029 }
9030 }
9031
9032 return NULL_TREE;
9033}
9034
9035/* Check the argument list of a function call for null in argument slots
d01f58f9 9036 that are marked as requiring a non-null pointer argument. The NARGS
9037 arguments are passed in the array ARGARRAY.
9038*/
dbf6c367 9039
9040static void
d01f58f9 9041check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9042{
9ca77b08 9043 tree a;
d01f58f9 9044 int i;
dbf6c367 9045
9ca77b08 9046 attrs = lookup_attribute ("nonnull", attrs);
9047 if (attrs == NULL_TREE)
9048 return;
9049
9050 a = attrs;
9051 /* See if any of the nonnull attributes has no arguments. If so,
9052 then every pointer argument is checked (in which case the check
9053 for pointer type is done in check_nonnull_arg). */
9054 if (TREE_VALUE (a) != NULL_TREE)
9055 do
9056 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9057 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9058
9059 if (a != NULL_TREE)
9060 for (i = 0; i < nargs; i++)
9061 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9062 i + 1);
9063 else
dbf6c367 9064 {
9ca77b08 9065 /* Walk the argument list. If we encounter an argument number we
9066 should check for non-null, do it. */
9067 for (i = 0; i < nargs; i++)
dbf6c367 9068 {
9ca77b08 9069 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9070 {
9ca77b08 9071 a = lookup_attribute ("nonnull", a);
9072 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9073 break;
4ee9c684 9074 }
9ca77b08 9075
9076 if (a != NULL_TREE)
9077 check_function_arguments_recurse (check_nonnull_arg, NULL,
9078 argarray[i], i + 1);
dbf6c367 9079 }
9080 }
9081}
9082
50ca527f 9083/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9084 from the end) is a (pointer)0. The NARGS arguments are passed in the
9085 array ARGARRAY. */
bf6c8de0 9086
9087static void
774e9d58 9088check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9089{
774e9d58 9090 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9091
9092 if (attr)
9093 {
d01f58f9 9094 int len = 0;
9095 int pos = 0;
9096 tree sentinel;
774e9d58 9097 function_args_iterator iter;
9098 tree t;
a0c938f0 9099
d01f58f9 9100 /* Skip over the named arguments. */
774e9d58 9101 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9102 {
774e9d58 9103 if (len == nargs)
9104 break;
d01f58f9 9105 len++;
9106 }
50ca527f 9107
d01f58f9 9108 if (TREE_VALUE (attr))
9109 {
9110 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9111 pos = TREE_INT_CST_LOW (p);
d01f58f9 9112 }
50ca527f 9113
d01f58f9 9114 /* The sentinel must be one of the varargs, i.e.
9115 in position >= the number of fixed arguments. */
9116 if ((nargs - 1 - pos) < len)
9117 {
77a357e3 9118 warning (OPT_Wformat_,
d01f58f9 9119 "not enough variable arguments to fit a sentinel");
9120 return;
bf6c8de0 9121 }
d01f58f9 9122
9123 /* Validate the sentinel. */
9124 sentinel = argarray[nargs - 1 - pos];
9125 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9126 || !integer_zerop (sentinel))
9127 /* Although __null (in C++) is only an integer we allow it
9128 nevertheless, as we are guaranteed that it's exactly
9129 as wide as a pointer, and we don't want to force
9130 users to cast the NULL they have written there.
9131 We warn with -Wstrict-null-sentinel, though. */
9132 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9133 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9134 }
9135}
9136
dbf6c367 9137/* Helper for check_function_nonnull; given a list of operands which
9138 must be non-null in ARGS, determine if operand PARAM_NUM should be
9139 checked. */
9140
9141static bool
1cae46be 9142nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9143{
4ee9c684 9144 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9145
9146 for (; args; args = TREE_CHAIN (args))
9147 {
231bd014 9148 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9149
9150 gcc_assert (found);
dbf6c367 9151
9152 if (arg_num == param_num)
9153 return true;
9154 }
9155 return false;
9156}
9157
9158/* Check that the function argument PARAM (which is operand number
9159 PARAM_NUM) is non-null. This is called by check_function_nonnull
9160 via check_function_arguments_recurse. */
9161
9162static void
9a03a746 9163check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9164 unsigned HOST_WIDE_INT param_num)
dbf6c367 9165{
9166 /* Just skip checking the argument if it's not a pointer. This can
9167 happen if the "nonnull" attribute was given without an operand
9168 list (which means to check every pointer argument). */
9169
9170 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9171 return;
9172
9173 if (integer_zerop (param))
155b601b 9174 warning (OPT_Wnonnull, "null argument where non-null required "
9175 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9176}
9177
9178/* Helper for nonnull attribute handling; fetch the operand number
9179 from the attribute argument list. */
9180
9181static bool
1cae46be 9182get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9183{
e913b5cd 9184 /* Verify the arg number is a small constant. */
e1d65c9f 9185 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9186 {
f9ae6f95 9187 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9188 return true;
9189 }
9190 else
dbf6c367 9191 return false;
dbf6c367 9192}
fa987697 9193
9194/* Handle a "nothrow" attribute; arguments as in
9195 struct attribute_spec.handler. */
9196
9197static tree
9a03a746 9198handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9199 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9200{
9201 if (TREE_CODE (*node) == FUNCTION_DECL)
9202 TREE_NOTHROW (*node) = 1;
9203 /* ??? TODO: Support types. */
9204 else
9205 {
9b2d6d13 9206 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9207 *no_add_attrs = true;
9208 }
9209
9210 return NULL_TREE;
9211}
7acb29a3 9212
9213/* Handle a "cleanup" attribute; arguments as in
9214 struct attribute_spec.handler. */
9215
9216static tree
1cae46be 9217handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9218 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9219{
9220 tree decl = *node;
9221 tree cleanup_id, cleanup_decl;
9222
9223 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9224 for global destructors in C++. This requires infrastructure that
9225 we don't have generically at the moment. It's also not a feature
9226 we'd be missing too much, since we do have attribute constructor. */
9227 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9228 {
9b2d6d13 9229 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9230 *no_add_attrs = true;
9231 return NULL_TREE;
9232 }
9233
9234 /* Verify that the argument is a function in scope. */
9235 /* ??? We could support pointers to functions here as well, if
9236 that was considered desirable. */
9237 cleanup_id = TREE_VALUE (args);
9238 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9239 {
07e3a3d2 9240 error ("cleanup argument not an identifier");
7acb29a3 9241 *no_add_attrs = true;
9242 return NULL_TREE;
9243 }
d1c41717 9244 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9245 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9246 {
07e3a3d2 9247 error ("cleanup argument not a function");
7acb29a3 9248 *no_add_attrs = true;
9249 return NULL_TREE;
9250 }
9251
1cae46be 9252 /* That the function has proper type is checked with the
7acb29a3 9253 eventual call to build_function_call. */
9254
9255 return NULL_TREE;
9256}
8a8cdb8d 9257
9258/* Handle a "warn_unused_result" attribute. No special handling. */
9259
9260static tree
9261handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9262 tree ARG_UNUSED (args),
9263 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9264{
9265 /* Ignore the attribute for functions not returning any value. */
9266 if (VOID_TYPE_P (TREE_TYPE (*node)))
9267 {
9b2d6d13 9268 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9269 *no_add_attrs = true;
9270 }
9271
9272 return NULL_TREE;
9273}
bf6c8de0 9274
9275/* Handle a "sentinel" attribute. */
9276
9277static tree
50ca527f 9278handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9279 int ARG_UNUSED (flags), bool *no_add_attrs)
9280{
a36cf284 9281 if (!prototype_p (*node))
bf6c8de0 9282 {
9b2d6d13 9283 warning (OPT_Wattributes,
9284 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9285 *no_add_attrs = true;
bf6c8de0 9286 }
50ca527f 9287 else
9288 {
c33080b9 9289 if (!stdarg_p (*node))
a0c938f0 9290 {
9b2d6d13 9291 warning (OPT_Wattributes,
9292 "%qE attribute only applies to variadic functions", name);
50ca527f 9293 *no_add_attrs = true;
9294 }
9295 }
a0c938f0 9296
50ca527f 9297 if (args)
bf6c8de0 9298 {
50ca527f 9299 tree position = TREE_VALUE (args);
3e5a8b00 9300 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9301 && TREE_CODE (position) != FUNCTION_DECL)
9302 position = default_conversion (position);
50ca527f 9303
3e5a8b00 9304 if (TREE_CODE (position) != INTEGER_CST
9305 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9306 {
48e1416a 9307 warning (OPT_Wattributes,
01b54db5 9308 "requested position is not an integer constant");
50ca527f 9309 *no_add_attrs = true;
9310 }
9311 else
a0c938f0 9312 {
50ca527f 9313 if (tree_int_cst_lt (position, integer_zero_node))
9314 {
01b54db5 9315 warning (OPT_Wattributes,
9316 "requested position is less than zero");
50ca527f 9317 *no_add_attrs = true;
9318 }
9319 }
bf6c8de0 9320 }
a0c938f0 9321
bf6c8de0 9322 return NULL_TREE;
9323}
b5c26b42 9324
9325/* Handle a "type_generic" attribute. */
9326
9327static tree
9328handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9329 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9330 bool * ARG_UNUSED (no_add_attrs))
9331{
19fbe3a4 9332 /* Ensure we have a function type. */
9333 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9334
19fbe3a4 9335 /* Ensure we have a variadic function. */
c33080b9 9336 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9337
9338 return NULL_TREE;
9339}
46f8e3b0 9340
24470055 9341/* Handle a "target" attribute. */
46f8e3b0 9342
9343static tree
24470055 9344handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9345 bool *no_add_attrs)
9346{
9347 /* Ensure we have a function type. */
9348 if (TREE_CODE (*node) != FUNCTION_DECL)
9349 {
9350 warning (OPT_Wattributes, "%qE attribute ignored", name);
9351 *no_add_attrs = true;
9352 }
46f8e3b0 9353 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9354 flags))
46f8e3b0 9355 *no_add_attrs = true;
9356
9357 return NULL_TREE;
9358}
9359
9360/* Arguments being collected for optimization. */
9361typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9362static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9363
9364
9365/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9366 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9367 false for #pragma GCC optimize. */
9368
9369bool
9370parse_optimize_options (tree args, bool attr_p)
9371{
9372 bool ret = true;
9373 unsigned opt_argc;
9374 unsigned i;
2becf397 9375 int saved_flag_strict_aliasing;
46f8e3b0 9376 const char **opt_argv;
615ef0bb 9377 struct cl_decoded_option *decoded_options;
9378 unsigned int decoded_options_count;
46f8e3b0 9379 tree ap;
9380
9381 /* Build up argv vector. Just in case the string is stored away, use garbage
9382 collected strings. */
f1f41a6c 9383 vec_safe_truncate (optimize_args, 0);
9384 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9385
9386 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9387 {
9388 tree value = TREE_VALUE (ap);
9389
9390 if (TREE_CODE (value) == INTEGER_CST)
9391 {
9392 char buffer[20];
f9ae6f95 9393 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9394 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9395 }
9396
9397 else if (TREE_CODE (value) == STRING_CST)
9398 {
9399 /* Split string into multiple substrings. */
9400 size_t len = TREE_STRING_LENGTH (value);
9401 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9402 char *end = p + len;
9403 char *comma;
9404 char *next_p = p;
9405
9406 while (next_p != NULL)
9407 {
9408 size_t len2;
9409 char *q, *r;
9410
9411 p = next_p;
9412 comma = strchr (p, ',');
9413 if (comma)
9414 {
9415 len2 = comma - p;
9416 *comma = '\0';
9417 next_p = comma+1;
9418 }
9419 else
9420 {
9421 len2 = end - p;
9422 next_p = NULL;
9423 }
9424
ba72912a 9425 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9426
9427 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9428 options. */
9429 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9430 {
9431 ret = false;
9432 if (attr_p)
9433 warning (OPT_Wattributes,
ebd7c4c1 9434 "bad option %s to optimize attribute", p);
46f8e3b0 9435 else
9436 warning (OPT_Wpragmas,
e44b0a1f 9437 "bad option %s to pragma attribute", p);
46f8e3b0 9438 continue;
9439 }
9440
9441 if (*p != '-')
9442 {
9443 *r++ = '-';
9444
9445 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9446 itself is -Os, and any other switch begins with a -f. */
9447 if ((*p >= '0' && *p <= '9')
9448 || (p[0] == 's' && p[1] == '\0'))
9449 *r++ = 'O';
9450 else if (*p != 'O')
9451 *r++ = 'f';
9452 }
9453
9454 memcpy (r, p, len2);
9455 r[len2] = '\0';
f1f41a6c 9456 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9457 }
9458
9459 }
9460 }
9461
f1f41a6c 9462 opt_argc = optimize_args->length ();
46f8e3b0 9463 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9464
9465 for (i = 1; i < opt_argc; i++)
f1f41a6c 9466 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9467
2becf397 9468 saved_flag_strict_aliasing = flag_strict_aliasing;
9469
46f8e3b0 9470 /* Now parse the options. */
f3f006ad 9471 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9472 &decoded_options,
9473 &decoded_options_count);
9474 decode_options (&global_options, &global_options_set,
3c6c0e40 9475 decoded_options, decoded_options_count,
9476 input_location, global_dc);
46f8e3b0 9477
4bec06b3 9478 targetm.override_options_after_change();
9479
2becf397 9480 /* Don't allow changing -fstrict-aliasing. */
9481 flag_strict_aliasing = saved_flag_strict_aliasing;
9482
f1f41a6c 9483 optimize_args->truncate (0);
46f8e3b0 9484 return ret;
9485}
9486
9487/* For handling "optimize" attribute. arguments as in
9488 struct attribute_spec.handler. */
9489
9490static tree
9491handle_optimize_attribute (tree *node, tree name, tree args,
9492 int ARG_UNUSED (flags), bool *no_add_attrs)
9493{
9494 /* Ensure we have a function type. */
9495 if (TREE_CODE (*node) != FUNCTION_DECL)
9496 {
9497 warning (OPT_Wattributes, "%qE attribute ignored", name);
9498 *no_add_attrs = true;
9499 }
9500 else
9501 {
9502 struct cl_optimization cur_opts;
9503 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9504
9505 /* Save current options. */
2c5d2e39 9506 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9507
9508 /* If we previously had some optimization options, use them as the
9509 default. */
9510 if (old_opts)
2c5d2e39 9511 cl_optimization_restore (&global_options,
9512 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9513
9514 /* Parse options, and update the vector. */
9515 parse_optimize_options (args, true);
9516 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9517 = build_optimization_node (&global_options);
46f8e3b0 9518
9519 /* Restore current options. */
2c5d2e39 9520 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9521 }
9522
9523 return NULL_TREE;
9524}
48b14f50 9525
9526/* Handle a "no_split_stack" attribute. */
9527
9528static tree
9529handle_no_split_stack_attribute (tree *node, tree name,
9530 tree ARG_UNUSED (args),
9531 int ARG_UNUSED (flags),
9532 bool *no_add_attrs)
9533{
9534 tree decl = *node;
9535
9536 if (TREE_CODE (decl) != FUNCTION_DECL)
9537 {
9538 error_at (DECL_SOURCE_LOCATION (decl),
9539 "%qE attribute applies only to functions", name);
9540 *no_add_attrs = true;
9541 }
9542 else if (DECL_INITIAL (decl))
9543 {
9544 error_at (DECL_SOURCE_LOCATION (decl),
9545 "can%'t set %qE attribute after definition", name);
9546 *no_add_attrs = true;
9547 }
9548
9549 return NULL_TREE;
9550}
d7dcba40 9551
9552/* Handle a "returns_nonnull" attribute; arguments as in
9553 struct attribute_spec.handler. */
9554
9555static tree
9556handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9557 bool *no_add_attrs)
9558{
9559 // Even without a prototype we still have a return type we can check.
9560 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9561 {
9562 error ("returns_nonnull attribute on a function not returning a pointer");
9563 *no_add_attrs = true;
9564 }
9565 return NULL_TREE;
9566}
9567
74691f46 9568/* Handle a "designated_init" attribute; arguments as in
9569 struct attribute_spec.handler. */
9570
9571static tree
9572handle_designated_init_attribute (tree *node, tree name, tree, int,
9573 bool *no_add_attrs)
9574{
9575 if (TREE_CODE (*node) != RECORD_TYPE)
9576 {
9577 error ("%qE attribute is only valid on %<struct%> type", name);
9578 *no_add_attrs = true;
9579 }
9580 return NULL_TREE;
9581}
9582
dbf6c367 9583\f
774e9d58 9584/* Check for valid arguments being passed to a function with FNTYPE.
9585 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 9586void
774e9d58 9587check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 9588{
9589 /* Check for null being passed in a pointer argument that must be
9590 non-null. We also need to do this if format checking is enabled. */
9591
9592 if (warn_nonnull)
774e9d58 9593 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9594
9595 /* Check for errors in format strings. */
9596
068bea1e 9597 if (warn_format || warn_suggest_attribute_format)
774e9d58 9598 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9599
9600 if (warn_format)
774e9d58 9601 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9602}
9603
9604/* Generic argument checking recursion routine. PARAM is the argument to
9605 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9606 once the argument is resolved. CTX is context for the callback. */
9607void
1cae46be 9608check_function_arguments_recurse (void (*callback)
9609 (void *, tree, unsigned HOST_WIDE_INT),
9610 void *ctx, tree param,
9611 unsigned HOST_WIDE_INT param_num)
dbf6c367 9612{
72dd6141 9613 if (CONVERT_EXPR_P (param)
c44afe23 9614 && (TYPE_PRECISION (TREE_TYPE (param))
9615 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9616 {
9617 /* Strip coercion. */
9618 check_function_arguments_recurse (callback, ctx,
4ee9c684 9619 TREE_OPERAND (param, 0), param_num);
dbf6c367 9620 return;
9621 }
9622
9623 if (TREE_CODE (param) == CALL_EXPR)
9624 {
c2f47e15 9625 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9626 tree attrs;
9627 bool found_format_arg = false;
9628
9629 /* See if this is a call to a known internationalization function
9630 that modifies a format arg. Such a function may have multiple
9631 format_arg attributes (for example, ngettext). */
9632
9633 for (attrs = TYPE_ATTRIBUTES (type);
9634 attrs;
9635 attrs = TREE_CHAIN (attrs))
9636 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9637 {
c2f47e15 9638 tree inner_arg;
dbf6c367 9639 tree format_num_expr;
9640 int format_num;
9641 int i;
c2f47e15 9642 call_expr_arg_iterator iter;
dbf6c367 9643
9644 /* Extract the argument number, which was previously checked
9645 to be valid. */
9646 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9647
e913b5cd 9648 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9649
c2f47e15 9650 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9651 inner_arg != 0;
9652 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9653 if (i == format_num)
9654 {
9655 check_function_arguments_recurse (callback, ctx,
c2f47e15 9656 inner_arg, param_num);
dbf6c367 9657 found_format_arg = true;
9658 break;
9659 }
9660 }
9661
9662 /* If we found a format_arg attribute and did a recursive check,
9663 we are done with checking this argument. Otherwise, we continue
9664 and this will be considered a non-literal. */
9665 if (found_format_arg)
9666 return;
9667 }
9668
9669 if (TREE_CODE (param) == COND_EXPR)
9670 {
9671 /* Check both halves of the conditional expression. */
9672 check_function_arguments_recurse (callback, ctx,
4ee9c684 9673 TREE_OPERAND (param, 1), param_num);
dbf6c367 9674 check_function_arguments_recurse (callback, ctx,
4ee9c684 9675 TREE_OPERAND (param, 2), param_num);
dbf6c367 9676 return;
9677 }
9678
9679 (*callback) (ctx, param, param_num);
9680}
1f3233d1 9681
60cce472 9682/* Checks for a builtin function FNDECL that the number of arguments
9683 NARGS against the required number REQUIRED and issues an error if
9684 there is a mismatch. Returns true if the number of arguments is
9685 correct, otherwise false. */
d43cee80 9686
9687static bool
60cce472 9688builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9689{
9690 if (nargs < required)
9691 {
60cce472 9692 error_at (input_location,
9693 "not enough arguments to function %qE", fndecl);
d43cee80 9694 return false;
9695 }
9696 else if (nargs > required)
9697 {
60cce472 9698 error_at (input_location,
9699 "too many arguments to function %qE", fndecl);
d43cee80 9700 return false;
9701 }
9702 return true;
9703}
9704
9705/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9706 Returns false if there was an error, otherwise true. */
9707
9708bool
9709check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9710{
9711 if (!DECL_BUILT_IN (fndecl)
9712 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9713 return true;
9714
9715 switch (DECL_FUNCTION_CODE (fndecl))
9716 {
9717 case BUILT_IN_CONSTANT_P:
60cce472 9718 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 9719
9720 case BUILT_IN_ISFINITE:
9721 case BUILT_IN_ISINF:
c319d56a 9722 case BUILT_IN_ISINF_SIGN:
d43cee80 9723 case BUILT_IN_ISNAN:
9724 case BUILT_IN_ISNORMAL:
60cce472 9725 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 9726 {
9727 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9728 {
9729 error ("non-floating-point argument in call to "
9730 "function %qE", fndecl);
9731 return false;
9732 }
9733 return true;
9734 }
9735 return false;
9736
9737 case BUILT_IN_ISGREATER:
9738 case BUILT_IN_ISGREATEREQUAL:
9739 case BUILT_IN_ISLESS:
9740 case BUILT_IN_ISLESSEQUAL:
9741 case BUILT_IN_ISLESSGREATER:
9742 case BUILT_IN_ISUNORDERED:
60cce472 9743 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 9744 {
9745 enum tree_code code0, code1;
9746 code0 = TREE_CODE (TREE_TYPE (args[0]));
9747 code1 = TREE_CODE (TREE_TYPE (args[1]));
9748 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9749 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9750 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9751 {
9752 error ("non-floating-point arguments in call to "
9753 "function %qE", fndecl);
9754 return false;
9755 }
9756 return true;
9757 }
9758 return false;
9759
19fbe3a4 9760 case BUILT_IN_FPCLASSIFY:
60cce472 9761 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 9762 {
9763 unsigned i;
48e1416a 9764
19fbe3a4 9765 for (i=0; i<5; i++)
9766 if (TREE_CODE (args[i]) != INTEGER_CST)
9767 {
9768 error ("non-const integer argument %u in call to function %qE",
9769 i+1, fndecl);
9770 return false;
9771 }
9772
9773 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9774 {
9775 error ("non-floating-point argument in call to function %qE",
9776 fndecl);
9777 return false;
9778 }
9779 return true;
9780 }
9781 return false;
9782
fca0886c 9783 case BUILT_IN_ASSUME_ALIGNED:
9784 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9785 {
9786 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9787 {
9788 error ("non-integer argument 3 in call to function %qE", fndecl);
9789 return false;
9790 }
9791 return true;
9792 }
9793 return false;
9794
0c93c8a9 9795 case BUILT_IN_ADD_OVERFLOW:
9796 case BUILT_IN_SUB_OVERFLOW:
9797 case BUILT_IN_MUL_OVERFLOW:
9798 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9799 {
9800 unsigned i;
9801 for (i = 0; i < 2; i++)
9802 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9803 {
9804 error ("argument %u in call to function %qE does not have "
9805 "integral type", i + 1, fndecl);
9806 return false;
9807 }
9808 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9809 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9810 {
9811 error ("argument 3 in call to function %qE does not have "
9812 "pointer to integer type", fndecl);
9813 return false;
9814 }
9815 return true;
9816 }
9817 return false;
9818
d43cee80 9819 default:
9820 return true;
9821 }
9822}
9823
860251be 9824/* Function to help qsort sort FIELD_DECLs by name order. */
9825
9826int
9827field_decl_cmp (const void *x_p, const void *y_p)
9828{
4fd61bc6 9829 const tree *const x = (const tree *const) x_p;
9830 const tree *const y = (const tree *const) y_p;
9831
860251be 9832 if (DECL_NAME (*x) == DECL_NAME (*y))
9833 /* A nontype is "greater" than a type. */
9834 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9835 if (DECL_NAME (*x) == NULL_TREE)
9836 return -1;
9837 if (DECL_NAME (*y) == NULL_TREE)
9838 return 1;
9839 if (DECL_NAME (*x) < DECL_NAME (*y))
9840 return -1;
9841 return 1;
9842}
9843
9844static struct {
9845 gt_pointer_operator new_value;
9846 void *cookie;
9847} resort_data;
9848
9849/* This routine compares two fields like field_decl_cmp but using the
9850pointer operator in resort_data. */
9851
9852static int
9853resort_field_decl_cmp (const void *x_p, const void *y_p)
9854{
4fd61bc6 9855 const tree *const x = (const tree *const) x_p;
9856 const tree *const y = (const tree *const) y_p;
860251be 9857
9858 if (DECL_NAME (*x) == DECL_NAME (*y))
9859 /* A nontype is "greater" than a type. */
9860 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9861 if (DECL_NAME (*x) == NULL_TREE)
9862 return -1;
9863 if (DECL_NAME (*y) == NULL_TREE)
9864 return 1;
9865 {
9866 tree d1 = DECL_NAME (*x);
9867 tree d2 = DECL_NAME (*y);
9868 resort_data.new_value (&d1, resort_data.cookie);
9869 resort_data.new_value (&d2, resort_data.cookie);
9870 if (d1 < d2)
9871 return -1;
9872 }
9873 return 1;
9874}
9875
9876/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9877
9878void
9879resort_sorted_fields (void *obj,
9a03a746 9880 void * ARG_UNUSED (orig_obj),
4ee9c684 9881 gt_pointer_operator new_value,
9882 void *cookie)
860251be 9883{
9a03a746 9884 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 9885 resort_data.new_value = new_value;
9886 resort_data.cookie = cookie;
9887 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 9888 resort_field_decl_cmp);
860251be 9889}
9890
209c9752 9891/* Subroutine of c_parse_error.
9892 Return the result of concatenating LHS and RHS. RHS is really
9893 a string literal, its first character is indicated by RHS_START and
cfee01e3 9894 RHS_SIZE is its length (including the terminating NUL character).
209c9752 9895
9896 The caller is responsible for deleting the returned pointer. */
9897
9898static char *
9899catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9900{
9901 const int lhs_size = strlen (lhs);
9902 char *result = XNEWVEC (char, lhs_size + rhs_size);
9903 strncpy (result, lhs, lhs_size);
9904 strncpy (result + lhs_size, rhs_start, rhs_size);
9905 return result;
9906}
9907
380c6697 9908/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 9909 TOKEN, which had the associated VALUE. */
9910
9911void
48e1416a 9912c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 9913 tree value, unsigned char token_flags)
92b128ed 9914{
209c9752 9915#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9916
9917 char *message = NULL;
92b128ed 9918
ba99525e 9919 if (token_type == CPP_EOF)
380c6697 9920 message = catenate_messages (gmsgid, " at end of input");
48e1416a 9921 else if (token_type == CPP_CHAR
9922 || token_type == CPP_WCHAR
ba99525e 9923 || token_type == CPP_CHAR16
9924 || token_type == CPP_CHAR32)
92b128ed 9925 {
f9ae6f95 9926 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 9927 const char *prefix;
9928
ba99525e 9929 switch (token_type)
924bbf02 9930 {
9931 default:
9932 prefix = "";
9933 break;
9934 case CPP_WCHAR:
9935 prefix = "L";
9936 break;
9937 case CPP_CHAR16:
9938 prefix = "u";
9939 break;
9940 case CPP_CHAR32:
9941 prefix = "U";
9942 break;
9943 }
9944
92b128ed 9945 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 9946 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 9947 else
a0c938f0 9948 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 9949
924bbf02 9950 error (message, prefix, val);
209c9752 9951 free (message);
9952 message = NULL;
92b128ed 9953 }
1898176c 9954 else if (token_type == CPP_CHAR_USERDEF
9955 || token_type == CPP_WCHAR_USERDEF
9956 || token_type == CPP_CHAR16_USERDEF
9957 || token_type == CPP_CHAR32_USERDEF)
9958 message = catenate_messages (gmsgid,
9959 " before user-defined character literal");
9960 else if (token_type == CPP_STRING_USERDEF
9961 || token_type == CPP_WSTRING_USERDEF
9962 || token_type == CPP_STRING16_USERDEF
9963 || token_type == CPP_STRING32_USERDEF
9964 || token_type == CPP_UTF8STRING_USERDEF)
9965 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 9966 else if (token_type == CPP_STRING
9967 || token_type == CPP_WSTRING
ba99525e 9968 || token_type == CPP_STRING16
538ba11a 9969 || token_type == CPP_STRING32
9970 || token_type == CPP_UTF8STRING)
380c6697 9971 message = catenate_messages (gmsgid, " before string constant");
ba99525e 9972 else if (token_type == CPP_NUMBER)
380c6697 9973 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 9974 else if (token_type == CPP_NAME)
209c9752 9975 {
380c6697 9976 message = catenate_messages (gmsgid, " before %qE");
782858b8 9977 error (message, value);
209c9752 9978 free (message);
9979 message = NULL;
9980 }
ba99525e 9981 else if (token_type == CPP_PRAGMA)
b75b98aa 9982 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 9983 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 9984 message = catenate_messages (gmsgid, " before end of line");
07b8f133 9985 else if (token_type == CPP_DECLTYPE)
9986 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 9987 else if (token_type < N_TTYPES)
209c9752 9988 {
380c6697 9989 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 9990 error (message, cpp_type2name (token_type, token_flags));
209c9752 9991 free (message);
9992 message = NULL;
9993 }
92b128ed 9994 else
380c6697 9995 error (gmsgid);
209c9752 9996
9997 if (message)
9998 {
9999 error (message);
10000 free (message);
10001 }
a0c938f0 10002#undef catenate_messages
92b128ed 10003}
10004
3a79f5da 10005/* Return the gcc option code associated with the reason for a cpp
10006 message, or 0 if none. */
10007
10008static int
10009c_option_controlling_cpp_error (int reason)
10010{
7ff8db31 10011 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10012
7ff8db31 10013 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10014 {
10015 if (entry->reason == reason)
10016 return entry->option_code;
10017 }
10018 return 0;
10019}
10020
7f5f3953 10021/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10022 preprocessor. The diagnostic is of type LEVEL, with REASON set
10023 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10024 LOCATION unless this is after lexing and the compiler's location
10025 should be used instead, with column number possibly overridden by
10026 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10027 the arguments. Returns true if a diagnostic was emitted, false
10028 otherwise. */
10029
10030bool
3a79f5da 10031c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10032 location_t location, unsigned int column_override,
10033 const char *msg, va_list *ap)
10034{
10035 diagnostic_info diagnostic;
10036 diagnostic_t dlevel;
5ae82d58 10037 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10038 bool ret;
10039
10040 switch (level)
10041 {
10042 case CPP_DL_WARNING_SYSHDR:
10043 if (flag_no_output)
10044 return false;
5ae82d58 10045 global_dc->dc_warn_system_headers = 1;
7f5f3953 10046 /* Fall through. */
10047 case CPP_DL_WARNING:
10048 if (flag_no_output)
10049 return false;
10050 dlevel = DK_WARNING;
10051 break;
10052 case CPP_DL_PEDWARN:
10053 if (flag_no_output && !flag_pedantic_errors)
10054 return false;
10055 dlevel = DK_PEDWARN;
10056 break;
10057 case CPP_DL_ERROR:
10058 dlevel = DK_ERROR;
10059 break;
10060 case CPP_DL_ICE:
10061 dlevel = DK_ICE;
10062 break;
10063 case CPP_DL_NOTE:
10064 dlevel = DK_NOTE;
10065 break;
ff903809 10066 case CPP_DL_FATAL:
10067 dlevel = DK_FATAL;
10068 break;
7f5f3953 10069 default:
10070 gcc_unreachable ();
10071 }
10072 if (done_lexing)
10073 location = input_location;
10074 diagnostic_set_info_translated (&diagnostic, msg, ap,
10075 location, dlevel);
10076 if (column_override)
10077 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10078 diagnostic_override_option_index (&diagnostic,
10079 c_option_controlling_cpp_error (reason));
7f5f3953 10080 ret = report_diagnostic (&diagnostic);
10081 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10082 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10083 return ret;
10084}
10085
624d37a6 10086/* Convert a character from the host to the target execution character
10087 set. cpplib handles this, mostly. */
10088
10089HOST_WIDE_INT
10090c_common_to_target_charset (HOST_WIDE_INT c)
10091{
10092 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10093 zero-extended under -fno-signed-char. cpplib insists that characters
10094 and character constants are always unsigned. Hence we must convert
10095 back and forth. */
10096 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10097
10098 uc = cpp_host_to_exec_charset (parse_in, uc);
10099
10100 if (flag_signed_char)
10101 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10102 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10103 else
10104 return uc;
10105}
10106
7549df0d 10107/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10108 references with an INDIRECT_REF of a constant at the bottom; much like the
10109 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10110
7549df0d 10111tree
10112fold_offsetof_1 (tree expr)
af28855b 10113{
af28855b 10114 tree base, off, t;
10115
10116 switch (TREE_CODE (expr))
10117 {
10118 case ERROR_MARK:
10119 return expr;
10120
6b11d2e3 10121 case VAR_DECL:
10122 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10123 return error_mark_node;
10124
d897f7c2 10125 case CALL_EXPR:
cf1a89a3 10126 case TARGET_EXPR:
d897f7c2 10127 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10128 return error_mark_node;
10129
d897f7c2 10130 case NOP_EXPR:
10131 case INDIRECT_REF:
7549df0d 10132 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10133 {
10134 error ("cannot apply %<offsetof%> to a non constant address");
10135 return error_mark_node;
10136 }
7549df0d 10137 return TREE_OPERAND (expr, 0);
d897f7c2 10138
af28855b 10139 case COMPONENT_REF:
7549df0d 10140 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10141 if (base == error_mark_node)
10142 return base;
10143
10144 t = TREE_OPERAND (expr, 1);
10145 if (DECL_C_BIT_FIELD (t))
10146 {
10147 error ("attempt to take address of bit-field structure "
782858b8 10148 "member %qD", t);
af28855b 10149 return error_mark_node;
10150 }
389dd41b 10151 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10152 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10153 / BITS_PER_UNIT));
af28855b 10154 break;
10155
10156 case ARRAY_REF:
7549df0d 10157 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10158 if (base == error_mark_node)
10159 return base;
10160
10161 t = TREE_OPERAND (expr, 1);
64ed018c 10162
10163 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10164 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10165 {
10166 tree upbound = array_ref_up_bound (expr);
10167 if (upbound != NULL_TREE
10168 && TREE_CODE (upbound) == INTEGER_CST
10169 && !tree_int_cst_equal (upbound,
10170 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10171 {
10172 upbound = size_binop (PLUS_EXPR, upbound,
10173 build_int_cst (TREE_TYPE (upbound), 1));
10174 if (tree_int_cst_lt (upbound, t))
10175 {
10176 tree v;
10177
10178 for (v = TREE_OPERAND (expr, 0);
10179 TREE_CODE (v) == COMPONENT_REF;
10180 v = TREE_OPERAND (v, 0))
10181 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10182 == RECORD_TYPE)
10183 {
1767a056 10184 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10185 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10186 if (TREE_CODE (fld_chain) == FIELD_DECL)
10187 break;
10188
10189 if (fld_chain)
10190 break;
10191 }
10192 /* Don't warn if the array might be considered a poor
10193 man's flexible array member with a very permissive
10194 definition thereof. */
10195 if (TREE_CODE (v) == ARRAY_REF
10196 || TREE_CODE (v) == COMPONENT_REF)
10197 warning (OPT_Warray_bounds,
10198 "index %E denotes an offset "
10199 "greater than size of %qT",
10200 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10201 }
10202 }
10203 }
7549df0d 10204
10205 t = convert (sizetype, t);
10206 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10207 break;
10208
ede90cc2 10209 case COMPOUND_EXPR:
10210 /* Handle static members of volatile structs. */
10211 t = TREE_OPERAND (expr, 1);
10212 gcc_assert (TREE_CODE (t) == VAR_DECL);
7549df0d 10213 return fold_offsetof_1 (t);
ede90cc2 10214
af28855b 10215 default:
231bd014 10216 gcc_unreachable ();
af28855b 10217 }
10218
7549df0d 10219 return fold_build_pointer_plus (base, off);
af28855b 10220}
10221
7549df0d 10222/* Likewise, but convert it to the return type of offsetof. */
10223
af28855b 10224tree
7549df0d 10225fold_offsetof (tree expr)
af28855b 10226{
7549df0d 10227 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10228}
10229
b9bdfa0b 10230/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10231 expression, because B will always be true. */
10232
10233void
10234warn_for_omitted_condop (location_t location, tree cond)
10235{
10236 if (truth_value_p (TREE_CODE (cond)))
10237 warning_at (location, OPT_Wparentheses,
10238 "the omitted middle operand in ?: will always be %<true%>, "
10239 "suggest explicit middle operand");
10240}
10241
a1f90215 10242/* Give an error for storing into ARG, which is 'const'. USE indicates
10243 how ARG was being used. */
10244
10245void
f2697631 10246readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10247{
10248 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10249 || use == lv_asm);
10250 /* Using this macro rather than (for example) arrays of messages
10251 ensures that all the format strings are checked at compile
10252 time. */
10253#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10254 : (use == lv_increment ? (I) \
10255 : (use == lv_decrement ? (D) : (AS))))
10256 if (TREE_CODE (arg) == COMPONENT_REF)
10257 {
10258 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10259 error_at (loc, READONLY_MSG (G_("assignment of member "
10260 "%qD in read-only object"),
10261 G_("increment of member "
10262 "%qD in read-only object"),
10263 G_("decrement of member "
10264 "%qD in read-only object"),
10265 G_("member %qD in read-only object "
10266 "used as %<asm%> output")),
10267 TREE_OPERAND (arg, 1));
a1f90215 10268 else
f2697631 10269 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10270 G_("increment of read-only member %qD"),
10271 G_("decrement of read-only member %qD"),
10272 G_("read-only member %qD used as %<asm%> output")),
10273 TREE_OPERAND (arg, 1));
a1f90215 10274 }
10275 else if (TREE_CODE (arg) == VAR_DECL)
f2697631 10276 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10277 G_("increment of read-only variable %qD"),
10278 G_("decrement of read-only variable %qD"),
10279 G_("read-only variable %qD used as %<asm%> output")),
10280 arg);
a1f90215 10281 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10282 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10283 G_("increment of read-only parameter %qD"),
10284 G_("decrement of read-only parameter %qD"),
10285 G_("read-only parameter %qD use as %<asm%> output")),
10286 arg);
a1f90215 10287 else if (TREE_CODE (arg) == RESULT_DECL)
10288 {
10289 gcc_assert (c_dialect_cxx ());
f2697631 10290 error_at (loc, READONLY_MSG (G_("assignment of "
10291 "read-only named return value %qD"),
10292 G_("increment of "
10293 "read-only named return value %qD"),
10294 G_("decrement of "
10295 "read-only named return value %qD"),
10296 G_("read-only named return value %qD "
10297 "used as %<asm%>output")),
10298 arg);
a1f90215 10299 }
10300 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10301 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10302 G_("increment of function %qD"),
10303 G_("decrement of function %qD"),
10304 G_("function %qD used as %<asm%> output")),
10305 arg);
a1f90215 10306 else
f2697631 10307 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10308 G_("increment of read-only location %qE"),
10309 G_("decrement of read-only location %qE"),
10310 G_("read-only location %qE used as %<asm%> output")),
10311 arg);
a1f90215 10312}
10313
e35976b1 10314/* Print an error message for an invalid lvalue. USE says
fdd84b77 10315 how the lvalue is being used and so selects the error message. LOC
10316 is the location for the error. */
ab6bb714 10317
e35976b1 10318void
fdd84b77 10319lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10320{
e35976b1 10321 switch (use)
ab6bb714 10322 {
e35976b1 10323 case lv_assign:
fdd84b77 10324 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10325 break;
10326 case lv_increment:
fdd84b77 10327 error_at (loc, "lvalue required as increment operand");
e35976b1 10328 break;
10329 case lv_decrement:
fdd84b77 10330 error_at (loc, "lvalue required as decrement operand");
e35976b1 10331 break;
10332 case lv_addressof:
fdd84b77 10333 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10334 break;
10335 case lv_asm:
fdd84b77 10336 error_at (loc, "lvalue required in asm statement");
e35976b1 10337 break;
10338 default:
10339 gcc_unreachable ();
ab6bb714 10340 }
ab6bb714 10341}
b1bbc8e5 10342
10343/* Print an error message for an invalid indirection of type TYPE.
10344 ERRSTRING is the name of the operator for the indirection. */
10345
10346void
10347invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10348{
10349 switch (errstring)
10350 {
10351 case RO_NULL:
10352 gcc_assert (c_dialect_cxx ());
10353 error_at (loc, "invalid type argument (have %qT)", type);
10354 break;
10355 case RO_ARRAY_INDEXING:
10356 error_at (loc,
10357 "invalid type argument of array indexing (have %qT)",
10358 type);
10359 break;
10360 case RO_UNARY_STAR:
10361 error_at (loc,
10362 "invalid type argument of unary %<*%> (have %qT)",
10363 type);
10364 break;
10365 case RO_ARROW:
10366 error_at (loc,
10367 "invalid type argument of %<->%> (have %qT)",
10368 type);
10369 break;
7354a89b 10370 case RO_ARROW_STAR:
10371 error_at (loc,
10372 "invalid type argument of %<->*%> (have %qT)",
10373 type);
10374 break;
b1bbc8e5 10375 case RO_IMPLICIT_CONVERSION:
10376 error_at (loc,
10377 "invalid type argument of implicit conversion (have %qT)",
10378 type);
10379 break;
10380 default:
10381 gcc_unreachable ();
10382 }
10383}
c271bdb2 10384\f
10385/* *PTYPE is an incomplete array. Complete it with a domain based on
10386 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10387 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10388 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10389
10390int
10391complete_array_type (tree *ptype, tree initial_value, bool do_default)
10392{
10393 tree maxindex, type, main_type, elt, unqual_elt;
10394 int failure = 0, quals;
6753bca0 10395 hashval_t hashcode = 0;
f5298614 10396 bool overflow_p = false;
c271bdb2 10397
10398 maxindex = size_zero_node;
10399 if (initial_value)
10400 {
10401 if (TREE_CODE (initial_value) == STRING_CST)
10402 {
10403 int eltsize
10404 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10405 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10406 }
10407 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10408 {
f1f41a6c 10409 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10410
f1f41a6c 10411 if (vec_safe_is_empty (v))
c271bdb2 10412 {
10413 if (pedantic)
10414 failure = 3;
7542c3b4 10415 maxindex = ssize_int (-1);
c271bdb2 10416 }
10417 else
10418 {
10419 tree curindex;
c75b4594 10420 unsigned HOST_WIDE_INT cnt;
10421 constructor_elt *ce;
cee43f7e 10422 bool fold_p = false;
c271bdb2 10423
f1f41a6c 10424 if ((*v)[0].index)
f5298614 10425 maxindex = (*v)[0].index, fold_p = true;
10426
c271bdb2 10427 curindex = maxindex;
10428
f1f41a6c 10429 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10430 {
cee43f7e 10431 bool curfold_p = false;
c75b4594 10432 if (ce->index)
cee43f7e 10433 curindex = ce->index, curfold_p = true;
c271bdb2 10434 else
cee43f7e 10435 {
10436 if (fold_p)
f5298614 10437 {
10438 /* Since we treat size types now as ordinary
10439 unsigned types, we need an explicit overflow
10440 check. */
10441 tree orig = curindex;
10442 curindex = fold_convert (sizetype, curindex);
10443 overflow_p |= tree_int_cst_lt (curindex, orig);
10444 }
389dd41b 10445 curindex = size_binop (PLUS_EXPR, curindex,
10446 size_one_node);
cee43f7e 10447 }
c271bdb2 10448 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10449 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10450 }
f5298614 10451 if (fold_p)
10452 {
10453 tree orig = maxindex;
10454 maxindex = fold_convert (sizetype, maxindex);
10455 overflow_p |= tree_int_cst_lt (maxindex, orig);
10456 }
c271bdb2 10457 }
10458 }
10459 else
10460 {
10461 /* Make an error message unless that happened already. */
10462 if (initial_value != error_mark_node)
10463 failure = 1;
10464 }
10465 }
10466 else
10467 {
10468 failure = 2;
10469 if (!do_default)
10470 return failure;
10471 }
10472
10473 type = *ptype;
10474 elt = TREE_TYPE (type);
10475 quals = TYPE_QUALS (strip_array_types (elt));
10476 if (quals == 0)
10477 unqual_elt = elt;
10478 else
6d5d708e 10479 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10480
10481 /* Using build_distinct_type_copy and modifying things afterward instead
10482 of using build_array_type to create a new type preserves all of the
10483 TYPE_LANG_FLAG_? bits that the front end may have set. */
10484 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10485 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10486 TYPE_DOMAIN (main_type)
10487 = build_range_type (TREE_TYPE (maxindex),
10488 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10489 layout_type (main_type);
10490
6753bca0 10491 /* Make sure we have the canonical MAIN_TYPE. */
10492 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10493 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10494 hashcode);
10495 main_type = type_hash_canon (hashcode, main_type);
10496
796735dc 10497 /* Fix the canonical type. */
10498 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10499 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10500 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10501 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10502 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10503 != TYPE_DOMAIN (main_type)))
48e1416a 10504 TYPE_CANONICAL (main_type)
796735dc 10505 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10506 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10507 else
10508 TYPE_CANONICAL (main_type) = main_type;
10509
c271bdb2 10510 if (quals == 0)
10511 type = main_type;
10512 else
10513 type = c_build_qualified_type (main_type, quals);
10514
4f5b8f2a 10515 if (COMPLETE_TYPE_P (type)
10516 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10517 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10518 {
10519 error ("size of array is too large");
10520 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10521 crash in tree_to_[su]hwi(). */
4f5b8f2a 10522 type = error_mark_node;
10523 }
10524
c271bdb2 10525 *ptype = type;
10526 return failure;
10527}
ab6bb714 10528
93426222 10529/* Like c_mark_addressable but don't check register qualifier. */
10530void
10531c_common_mark_addressable_vec (tree t)
10532{
10533 while (handled_component_p (t))
10534 t = TREE_OPERAND (t, 0);
10535 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10536 return;
10537 TREE_ADDRESSABLE (t) = 1;
10538}
10539
10540
b6a5fc45 10541\f
10542/* Used to help initialize the builtin-types.def table. When a type of
10543 the correct size doesn't exist, use error_mark_node instead of NULL.
10544 The later results in segfaults even when a decl using the type doesn't
10545 get invoked. */
10546
10547tree
10548builtin_type_for_size (int size, bool unsignedp)
10549{
a51edb4c 10550 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10551 return type ? type : error_mark_node;
10552}
10553
10554/* A helper function for resolve_overloaded_builtin in resolving the
10555 overloaded __sync_ builtins. Returns a positive power of 2 if the
10556 first operand of PARAMS is a pointer to a supported data type.
10557 Returns 0 if an error is encountered. */
10558
10559static int
f1f41a6c 10560sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 10561{
10562 tree type;
10563 int size;
10564
f1f41a6c 10565 if (!params)
b6a5fc45 10566 {
10567 error ("too few arguments to function %qE", function);
10568 return 0;
10569 }
10570
f1f41a6c 10571 type = TREE_TYPE ((*params)[0]);
0d284870 10572 if (TREE_CODE (type) == ARRAY_TYPE)
10573 {
10574 /* Force array-to-pointer decay for C++. */
10575 gcc_assert (c_dialect_cxx());
10576 (*params)[0] = default_conversion ((*params)[0]);
10577 type = TREE_TYPE ((*params)[0]);
10578 }
b6a5fc45 10579 if (TREE_CODE (type) != POINTER_TYPE)
10580 goto incompatible;
10581
10582 type = TREE_TYPE (type);
10583 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10584 goto incompatible;
10585
e913b5cd 10586 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10587 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10588 return size;
10589
10590 incompatible:
10591 error ("incompatible type for argument %d of %qE", 1, function);
10592 return 0;
10593}
10594
a0c938f0 10595/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10596 PARAMS to make arguments match up with those of FUNCTION. Drops
10597 the variadic arguments at the end. Returns false if some error
10598 was encountered; true on success. */
10599
10600static bool
1cd6e20d 10601sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10602 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10603{
d0af78c5 10604 function_args_iterator iter;
b6a5fc45 10605 tree ptype;
b9c74b4d 10606 unsigned int parmnum;
b6a5fc45 10607
d0af78c5 10608 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10609 /* We've declared the implementation functions to use "volatile void *"
10610 as the pointer parameter, so we shouldn't get any complaints from the
10611 call to check_function_arguments what ever type the user used. */
d0af78c5 10612 function_args_iter_next (&iter);
f1f41a6c 10613 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10614 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10615
10616 /* For the rest of the values, we need to cast these to FTYPE, so that we
10617 don't get warnings for passing pointer types, etc. */
b9c74b4d 10618 parmnum = 0;
d0af78c5 10619 while (1)
b6a5fc45 10620 {
d0af78c5 10621 tree val, arg_type;
10622
10623 arg_type = function_args_iter_cond (&iter);
10624 /* XXX void_type_node belies the abstraction. */
10625 if (arg_type == void_type_node)
10626 break;
b6a5fc45 10627
b9c74b4d 10628 ++parmnum;
f1f41a6c 10629 if (params->length () <= parmnum)
b6a5fc45 10630 {
1cd6e20d 10631 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10632 return false;
10633 }
10634
0f6a7cb7 10635 /* Only convert parameters if arg_type is unsigned integer type with
10636 new format sync routines, i.e. don't attempt to convert pointer
10637 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10638 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10639 kinds). */
10640 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10641 {
10642 /* Ideally for the first conversion we'd use convert_for_assignment
10643 so that we get warnings for anything that doesn't match the pointer
10644 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10645 val = (*params)[parmnum];
1cd6e20d 10646 val = convert (ptype, val);
10647 val = convert (arg_type, val);
f1f41a6c 10648 (*params)[parmnum] = val;
1cd6e20d 10649 }
b6a5fc45 10650
d0af78c5 10651 function_args_iter_next (&iter);
b6a5fc45 10652 }
10653
1cd6e20d 10654 /* __atomic routines are not variadic. */
f1f41a6c 10655 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10656 {
10657 error_at (loc, "too many arguments to function %qE", orig_function);
10658 return false;
10659 }
10660
b6a5fc45 10661 /* The definition of these primitives is variadic, with the remaining
10662 being "an optional list of variables protected by the memory barrier".
10663 No clue what that's supposed to mean, precisely, but we consider all
10664 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10665 params->truncate (parmnum + 1);
b6a5fc45 10666
10667 return true;
10668}
10669
a0c938f0 10670/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10671 RESULT to make it match the type of the first pointer argument in
10672 PARAMS. */
10673
10674static tree
1cd6e20d 10675sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10676{
b9c74b4d 10677 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10678 tree rtype = TREE_TYPE (result);
10080eac 10679 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10680
10681 /* New format doesn't require casting unless the types are the same size. */
10682 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10683 return convert (ptype, result);
10684 else
10685 return result;
10686}
10687
10688/* This function verifies the PARAMS to generic atomic FUNCTION.
10689 It returns the size if all the parameters are the same size, otherwise
10690 0 is returned if the parameters are invalid. */
10691
10692static int
f1f41a6c 10693get_atomic_generic_size (location_t loc, tree function,
10694 vec<tree, va_gc> *params)
1cd6e20d 10695{
10696 unsigned int n_param;
10697 unsigned int n_model;
10698 unsigned int x;
10699 int size_0;
10700 tree type_0;
10701
10702 /* Determine the parameter makeup. */
10703 switch (DECL_FUNCTION_CODE (function))
10704 {
10705 case BUILT_IN_ATOMIC_EXCHANGE:
10706 n_param = 4;
10707 n_model = 1;
10708 break;
10709 case BUILT_IN_ATOMIC_LOAD:
10710 case BUILT_IN_ATOMIC_STORE:
10711 n_param = 3;
10712 n_model = 1;
10713 break;
10714 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10715 n_param = 6;
10716 n_model = 2;
10717 break;
10718 default:
1d581089 10719 gcc_unreachable ();
1cd6e20d 10720 }
10721
f1f41a6c 10722 if (vec_safe_length (params) != n_param)
1cd6e20d 10723 {
10724 error_at (loc, "incorrect number of arguments to function %qE", function);
10725 return 0;
10726 }
10727
10728 /* Get type of first parameter, and determine its size. */
f1f41a6c 10729 type_0 = TREE_TYPE ((*params)[0]);
0d284870 10730 if (TREE_CODE (type_0) == ARRAY_TYPE)
10731 {
10732 /* Force array-to-pointer decay for C++. */
10733 gcc_assert (c_dialect_cxx());
10734 (*params)[0] = default_conversion ((*params)[0]);
10735 type_0 = TREE_TYPE ((*params)[0]);
10736 }
1d581089 10737 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10738 {
10739 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10740 function);
10741 return 0;
10742 }
10743
10744 /* Types must be compile time constant sizes. */
10745 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 10746 {
1d581089 10747 error_at (loc,
10748 "argument 1 of %qE must be a pointer to a constant size type",
10749 function);
1cd6e20d 10750 return 0;
10751 }
1d581089 10752
e913b5cd 10753 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 10754
1d581089 10755 /* Zero size objects are not allowed. */
10756 if (size_0 == 0)
10757 {
10758 error_at (loc,
10759 "argument 1 of %qE must be a pointer to a nonzero size object",
10760 function);
10761 return 0;
10762 }
10763
1cd6e20d 10764 /* Check each other parameter is a pointer and the same size. */
10765 for (x = 0; x < n_param - n_model; x++)
10766 {
10767 int size;
f1f41a6c 10768 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 10769 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 10770 if (n_param == 6 && x == 3)
10771 continue;
10772 if (!POINTER_TYPE_P (type))
10773 {
10774 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10775 function);
10776 return 0;
10777 }
1f6be080 10778 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10779 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 10780 if (size != size_0)
10781 {
10782 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10783 function);
10784 return 0;
10785 }
10786 }
10787
10788 /* Check memory model parameters for validity. */
10789 for (x = n_param - n_model ; x < n_param; x++)
10790 {
f1f41a6c 10791 tree p = (*params)[x];
1cd6e20d 10792 if (TREE_CODE (p) == INTEGER_CST)
10793 {
e913b5cd 10794 int i = tree_to_uhwi (p);
a372f7ca 10795 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 10796 {
10797 warning_at (loc, OPT_Winvalid_memory_model,
10798 "invalid memory model argument %d of %qE", x + 1,
10799 function);
1cd6e20d 10800 }
10801 }
10802 else
10803 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10804 {
10805 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10806 function);
10807 return 0;
10808 }
10809 }
10810
10811 return size_0;
10812}
10813
10814
10815/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10816 at the beginning of the parameter list PARAMS representing the size of the
10817 objects. This is to match the library ABI requirement. LOC is the location
10818 of the function call.
10819 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10820 returned to allow the external call to be constructed. */
10821
10822static tree
10823add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 10824 vec<tree, va_gc> *params)
1cd6e20d 10825{
10826 tree size_node;
10827
10828 /* Insert a SIZE_T parameter as the first param. If there isn't
10829 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 10830 if (!params->space (1))
1cd6e20d 10831 {
10832 unsigned int z, len;
f1f41a6c 10833 vec<tree, va_gc> *v;
1cd6e20d 10834 tree f;
10835
f1f41a6c 10836 len = params->length ();
10837 vec_alloc (v, len + 1);
5a672e62 10838 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 10839 for (z = 0; z < len; z++)
f1f41a6c 10840 v->quick_push ((*params)[z]);
ec761d5a 10841 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 10842 vec_free (v);
1cd6e20d 10843 return f;
10844 }
10845
10846 /* Add the size parameter and leave as a function call for processing. */
10847 size_node = build_int_cst (size_type_node, n);
f1f41a6c 10848 params->quick_insert (0, size_node);
1cd6e20d 10849 return NULL_TREE;
10850}
10851
10852
a056826c 10853/* Return whether atomic operations for naturally aligned N-byte
10854 arguments are supported, whether inline or through libatomic. */
10855static bool
10856atomic_size_supported_p (int n)
10857{
10858 switch (n)
10859 {
10860 case 1:
10861 case 2:
10862 case 4:
10863 case 8:
10864 return true;
10865
10866 case 16:
10867 return targetm.scalar_mode_supported_p (TImode);
10868
10869 default:
10870 return false;
10871 }
10872}
10873
1cd6e20d 10874/* This will process an __atomic_exchange function call, determine whether it
10875 needs to be mapped to the _N variation, or turned into a library call.
10876 LOC is the location of the builtin call.
10877 FUNCTION is the DECL that has been invoked;
10878 PARAMS is the argument list for the call. The return value is non-null
10879 TRUE is returned if it is translated into the proper format for a call to the
10880 external library, and NEW_RETURN is set the tree for that function.
10881 FALSE is returned if processing for the _N variation is required, and
10882 NEW_RETURN is set to the the return value the result is copied into. */
10883static bool
10884resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 10885 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 10886{
10887 tree p0, p1, p2, p3;
10888 tree I_type, I_type_ptr;
10889 int n = get_atomic_generic_size (loc, function, params);
10890
1d581089 10891 /* Size of 0 is an error condition. */
10892 if (n == 0)
10893 {
10894 *new_return = error_mark_node;
10895 return true;
10896 }
10897
1cd6e20d 10898 /* If not a lock-free size, change to the library generic format. */
a056826c 10899 if (!atomic_size_supported_p (n))
1cd6e20d 10900 {
10901 *new_return = add_atomic_size_parameter (n, loc, function, params);
10902 return true;
10903 }
10904
10905 /* Otherwise there is a lockfree match, transform the call from:
10906 void fn(T* mem, T* desired, T* return, model)
10907 into
10908 *return = (T) (fn (In* mem, (In) *desired, model)) */
10909
f1f41a6c 10910 p0 = (*params)[0];
10911 p1 = (*params)[1];
10912 p2 = (*params)[2];
10913 p3 = (*params)[3];
1cd6e20d 10914
10915 /* Create pointer to appropriate size. */
10916 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10917 I_type_ptr = build_pointer_type (I_type);
10918
10919 /* Convert object pointer to required type. */
10920 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10921 (*params)[0] = p0;
1cd6e20d 10922 /* Convert new value to required type, and dereference it. */
10923 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10924 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 10925 (*params)[1] = p1;
1cd6e20d 10926
10927 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 10928 (*params)[2] = p3;
10929 params->truncate (3);
1cd6e20d 10930
10931 /* Convert return pointer and dereference it for later assignment. */
10932 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10933
10934 return false;
b6a5fc45 10935}
10936
1cd6e20d 10937
10938/* This will process an __atomic_compare_exchange function call, determine
10939 whether it needs to be mapped to the _N variation, or turned into a lib call.
10940 LOC is the location of the builtin call.
10941 FUNCTION is the DECL that has been invoked;
10942 PARAMS is the argument list for the call. The return value is non-null
10943 TRUE is returned if it is translated into the proper format for a call to the
10944 external library, and NEW_RETURN is set the tree for that function.
10945 FALSE is returned if processing for the _N variation is required. */
10946
10947static bool
10948resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 10949 vec<tree, va_gc> *params,
1cd6e20d 10950 tree *new_return)
10951{
10952 tree p0, p1, p2;
10953 tree I_type, I_type_ptr;
10954 int n = get_atomic_generic_size (loc, function, params);
10955
1d581089 10956 /* Size of 0 is an error condition. */
10957 if (n == 0)
10958 {
10959 *new_return = error_mark_node;
10960 return true;
10961 }
10962
1cd6e20d 10963 /* If not a lock-free size, change to the library generic format. */
a056826c 10964 if (!atomic_size_supported_p (n))
1cd6e20d 10965 {
10966 /* The library generic format does not have the weak parameter, so
10967 remove it from the param list. Since a parameter has been removed,
10968 we can be sure that there is room for the SIZE_T parameter, meaning
10969 there will not be a recursive rebuilding of the parameter list, so
10970 there is no danger this will be done twice. */
10971 if (n > 0)
10972 {
f1f41a6c 10973 (*params)[3] = (*params)[4];
10974 (*params)[4] = (*params)[5];
10975 params->truncate (5);
1cd6e20d 10976 }
10977 *new_return = add_atomic_size_parameter (n, loc, function, params);
10978 return true;
10979 }
10980
10981 /* Otherwise, there is a match, so the call needs to be transformed from:
10982 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10983 into
10984 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 10985
f1f41a6c 10986 p0 = (*params)[0];
10987 p1 = (*params)[1];
10988 p2 = (*params)[2];
1cd6e20d 10989
10990 /* Create pointer to appropriate size. */
10991 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10992 I_type_ptr = build_pointer_type (I_type);
10993
10994 /* Convert object pointer to required type. */
10995 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 10996 (*params)[0] = p0;
1cd6e20d 10997
10998 /* Convert expected pointer to required type. */
10999 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11000 (*params)[1] = p1;
1cd6e20d 11001
11002 /* Convert desired value to required type, and dereference it. */
11003 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11004 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11005 (*params)[2] = p2;
1cd6e20d 11006
11007 /* The rest of the parameters are fine. NULL means no special return value
11008 processing.*/
11009 *new_return = NULL;
11010 return false;
11011}
11012
11013
11014/* This will process an __atomic_load function call, determine whether it
11015 needs to be mapped to the _N variation, or turned into a library call.
11016 LOC is the location of the builtin call.
11017 FUNCTION is the DECL that has been invoked;
11018 PARAMS is the argument list for the call. The return value is non-null
11019 TRUE is returned if it is translated into the proper format for a call to the
11020 external library, and NEW_RETURN is set the tree for that function.
11021 FALSE is returned if processing for the _N variation is required, and
11022 NEW_RETURN is set to the the return value the result is copied into. */
11023
11024static bool
11025resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11026 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11027{
11028 tree p0, p1, p2;
11029 tree I_type, I_type_ptr;
11030 int n = get_atomic_generic_size (loc, function, params);
11031
1d581089 11032 /* Size of 0 is an error condition. */
11033 if (n == 0)
11034 {
11035 *new_return = error_mark_node;
11036 return true;
11037 }
11038
1cd6e20d 11039 /* If not a lock-free size, change to the library generic format. */
a056826c 11040 if (!atomic_size_supported_p (n))
1cd6e20d 11041 {
11042 *new_return = add_atomic_size_parameter (n, loc, function, params);
11043 return true;
11044 }
11045
11046 /* Otherwise, there is a match, so the call needs to be transformed from:
11047 void fn(T* mem, T* return, model)
11048 into
11049 *return = (T) (fn ((In *) mem, model)) */
11050
f1f41a6c 11051 p0 = (*params)[0];
11052 p1 = (*params)[1];
11053 p2 = (*params)[2];
1cd6e20d 11054
11055 /* Create pointer to appropriate size. */
11056 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11057 I_type_ptr = build_pointer_type (I_type);
11058
11059 /* Convert object pointer to required type. */
11060 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11061 (*params)[0] = p0;
1cd6e20d 11062
11063 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11064 (*params)[1] = p2;
11065 params->truncate (2);
1cd6e20d 11066
11067 /* Convert return pointer and dereference it for later assignment. */
11068 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11069
11070 return false;
11071}
11072
11073
11074/* This will process an __atomic_store function call, determine whether it
11075 needs to be mapped to the _N variation, or turned into a library call.
11076 LOC is the location of the builtin call.
11077 FUNCTION is the DECL that has been invoked;
11078 PARAMS is the argument list for the call. The return value is non-null
11079 TRUE is returned if it is translated into the proper format for a call to the
11080 external library, and NEW_RETURN is set the tree for that function.
11081 FALSE is returned if processing for the _N variation is required, and
11082 NEW_RETURN is set to the the return value the result is copied into. */
11083
11084static bool
11085resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11086 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11087{
11088 tree p0, p1;
11089 tree I_type, I_type_ptr;
11090 int n = get_atomic_generic_size (loc, function, params);
11091
1d581089 11092 /* Size of 0 is an error condition. */
11093 if (n == 0)
11094 {
11095 *new_return = error_mark_node;
11096 return true;
11097 }
11098
1cd6e20d 11099 /* If not a lock-free size, change to the library generic format. */
a056826c 11100 if (!atomic_size_supported_p (n))
1cd6e20d 11101 {
11102 *new_return = add_atomic_size_parameter (n, loc, function, params);
11103 return true;
11104 }
11105
11106 /* Otherwise, there is a match, so the call needs to be transformed from:
11107 void fn(T* mem, T* value, model)
11108 into
11109 fn ((In *) mem, (In) *value, model) */
11110
f1f41a6c 11111 p0 = (*params)[0];
11112 p1 = (*params)[1];
1cd6e20d 11113
11114 /* Create pointer to appropriate size. */
11115 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11116 I_type_ptr = build_pointer_type (I_type);
11117
11118 /* Convert object pointer to required type. */
11119 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11120 (*params)[0] = p0;
1cd6e20d 11121
11122 /* Convert new value to required type, and dereference it. */
11123 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11124 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11125 (*params)[1] = p1;
1cd6e20d 11126
11127 /* The memory model is in the right spot already. Return is void. */
11128 *new_return = NULL_TREE;
11129
11130 return false;
11131}
11132
11133
b6a5fc45 11134/* Some builtin functions are placeholders for other expressions. This
11135 function should be called immediately after parsing the call expression
11136 before surrounding code has committed to the type of the expression.
11137
e60a6f7b 11138 LOC is the location of the builtin call.
11139
b6a5fc45 11140 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11141 PARAMS is the argument list for the call. The return value is non-null
11142 when expansion is complete, and null if normal processing should
11143 continue. */
11144
11145tree
f1f41a6c 11146resolve_overloaded_builtin (location_t loc, tree function,
11147 vec<tree, va_gc> *params)
b6a5fc45 11148{
11149 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11150 bool orig_format = true;
11151 tree new_return = NULL_TREE;
11152
65441f6f 11153 switch (DECL_BUILT_IN_CLASS (function))
11154 {
11155 case BUILT_IN_NORMAL:
11156 break;
11157 case BUILT_IN_MD:
11158 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11159 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11160 else
a0c938f0 11161 return NULL_TREE;
65441f6f 11162 default:
11163 return NULL_TREE;
11164 }
a0c938f0 11165
65441f6f 11166 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11167 switch (orig_code)
11168 {
1cd6e20d 11169 case BUILT_IN_ATOMIC_EXCHANGE:
11170 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11171 case BUILT_IN_ATOMIC_LOAD:
11172 case BUILT_IN_ATOMIC_STORE:
11173 {
11174 /* Handle these 4 together so that they can fall through to the next
11175 case if the call is transformed to an _N variant. */
11176 switch (orig_code)
11177 {
11178 case BUILT_IN_ATOMIC_EXCHANGE:
11179 {
11180 if (resolve_overloaded_atomic_exchange (loc, function, params,
11181 &new_return))
11182 return new_return;
11183 /* Change to the _N variant. */
11184 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11185 break;
11186 }
11187
11188 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11189 {
11190 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11191 params,
11192 &new_return))
11193 return new_return;
11194 /* Change to the _N variant. */
11195 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11196 break;
11197 }
11198 case BUILT_IN_ATOMIC_LOAD:
11199 {
11200 if (resolve_overloaded_atomic_load (loc, function, params,
11201 &new_return))
11202 return new_return;
11203 /* Change to the _N variant. */
11204 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11205 break;
11206 }
11207 case BUILT_IN_ATOMIC_STORE:
11208 {
11209 if (resolve_overloaded_atomic_store (loc, function, params,
11210 &new_return))
11211 return new_return;
11212 /* Change to the _N variant. */
11213 orig_code = BUILT_IN_ATOMIC_STORE_N;
11214 break;
11215 }
11216 default:
11217 gcc_unreachable ();
11218 }
11219 /* Fallthrough to the normal processing. */
11220 }
11221 case BUILT_IN_ATOMIC_EXCHANGE_N:
11222 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11223 case BUILT_IN_ATOMIC_LOAD_N:
11224 case BUILT_IN_ATOMIC_STORE_N:
11225 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11226 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11227 case BUILT_IN_ATOMIC_AND_FETCH_N:
11228 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11229 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11230 case BUILT_IN_ATOMIC_OR_FETCH_N:
11231 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11232 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11233 case BUILT_IN_ATOMIC_FETCH_AND_N:
11234 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11235 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11236 case BUILT_IN_ATOMIC_FETCH_OR_N:
11237 {
11238 orig_format = false;
11239 /* Fallthru for parameter processing. */
11240 }
2797f13a 11241 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11242 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11243 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11244 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11245 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11246 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11247 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11248 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11249 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11250 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11251 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11252 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11253 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11254 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11255 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11256 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11257 {
11258 int n = sync_resolve_size (function, params);
b9c74b4d 11259 tree new_function, first_param, result;
b9a16870 11260 enum built_in_function fncode;
b6a5fc45 11261
11262 if (n == 0)
11263 return error_mark_node;
11264
b9a16870 11265 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11266 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11267 if (!sync_resolve_params (loc, function, new_function, params,
11268 orig_format))
b6a5fc45 11269 return error_mark_node;
11270
f1f41a6c 11271 first_param = (*params)[0];
ec761d5a 11272 result = build_function_call_vec (loc, vNULL, new_function, params,
11273 NULL);
1cd6e20d 11274 if (result == error_mark_node)
11275 return result;
2797f13a 11276 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11277 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11278 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11279 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11280
1cd6e20d 11281 /* If new_return is set, assign function to that expr and cast the
11282 result to void since the generic interface returned void. */
11283 if (new_return)
11284 {
11285 /* Cast function result from I{1,2,4,8,16} to the required type. */
11286 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11287 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11288 result);
11289 TREE_SIDE_EFFECTS (result) = 1;
11290 protected_set_expr_location (result, loc);
11291 result = convert (void_type_node, result);
11292 }
b6a5fc45 11293 return result;
11294 }
11295
11296 default:
65441f6f 11297 return NULL_TREE;
b6a5fc45 11298 }
11299}
11300
73437615 11301/* vector_types_compatible_elements_p is used in type checks of vectors
11302 values used as operands of binary operators. Where it returns true, and
11303 the other checks of the caller succeed (being vector types in he first
11304 place, and matching number of elements), we can just treat the types
11305 as essentially the same.
11306 Contrast with vector_targets_convertible_p, which is used for vector
11307 pointer types, and vector_types_convertible_p, which will allow
11308 language-specific matches under the control of flag_lax_vector_conversions,
11309 and might still require a conversion. */
11310/* True if vector types T1 and T2 can be inputs to the same binary
11311 operator without conversion.
11312 We don't check the overall vector size here because some of our callers
11313 want to give different error messages when the vectors are compatible
11314 except for the element count. */
11315
491255f5 11316bool
73437615 11317vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11318{
73437615 11319 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11320 t1 = TREE_TYPE (t1);
11321 t2 = TREE_TYPE (t2);
11322
491255f5 11323 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11324
9421ebb9 11325 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11326 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11327 || c2 == FIXED_POINT_TYPE));
491255f5 11328
73437615 11329 t1 = c_common_signed_type (t1);
11330 t2 = c_common_signed_type (t2);
491255f5 11331 /* Equality works here because c_common_signed_type uses
11332 TYPE_MAIN_VARIANT. */
73437615 11333 if (t1 == t2)
11334 return true;
11335 if (opaque && c1 == c2
11336 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11337 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11338 return true;
11339 return false;
491255f5 11340}
11341
be7350e7 11342/* Check for missing format attributes on function pointers. LTYPE is
11343 the new type or left-hand side type. RTYPE is the old type or
11344 right-hand side type. Returns TRUE if LTYPE is missing the desired
11345 attribute. */
11346
11347bool
11348check_missing_format_attribute (tree ltype, tree rtype)
11349{
11350 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11351 tree ra;
11352
11353 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11354 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11355 break;
11356 if (ra)
11357 {
11358 tree la;
11359 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11360 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11361 break;
11362 return !la;
11363 }
11364 else
11365 return false;
11366}
11367
2840aae4 11368/* Subscripting with type char is likely to lose on a machine where
11369 chars are signed. So warn on any machine, but optionally. Don't
11370 warn for unsigned char since that type is safe. Don't warn for
11371 signed char because anyone who uses that must have done so
11372 deliberately. Furthermore, we reduce the false positive load by
11373 warning only for non-constant value of type char. */
11374
11375void
92b63884 11376warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11377{
11378 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11379 && TREE_CODE (index) != INTEGER_CST)
92b63884 11380 warning_at (loc, OPT_Wchar_subscripts,
11381 "array subscript has type %<char%>");
2840aae4 11382}
11383
e534436e 11384/* Implement -Wparentheses for the unexpected C precedence rules, to
11385 cover cases like x + y << z which readers are likely to
11386 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11387 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11388 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11389 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11390 expression was not formed using a binary or unary operator, or it
11391 was enclosed in parentheses. */
e534436e 11392
11393void
b0e7825e 11394warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11395 enum tree_code code_left, tree arg_left,
82012ffe 11396 enum tree_code code_right, tree arg_right)
e534436e 11397{
11398 if (!warn_parentheses)
11399 return;
11400
82012ffe 11401 /* This macro tests that the expression ARG with original tree code
11402 CODE appears to be a boolean expression. or the result of folding a
11403 boolean expression. */
11404#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11405 (truth_value_p (TREE_CODE (ARG)) \
11406 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11407 /* Folding may create 0 or 1 integers from other expressions. */ \
11408 || ((CODE) != INTEGER_CST \
11409 && (integer_onep (ARG) || integer_zerop (ARG))))
11410
48e1416a 11411 switch (code)
e534436e 11412 {
82012ffe 11413 case LSHIFT_EXPR:
b0e7825e 11414 if (code_left == PLUS_EXPR)
11415 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11416 "suggest parentheses around %<+%> inside %<<<%>");
11417 else if (code_right == PLUS_EXPR)
11418 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11419 "suggest parentheses around %<+%> inside %<<<%>");
11420 else if (code_left == MINUS_EXPR)
11421 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11422 "suggest parentheses around %<-%> inside %<<<%>");
11423 else if (code_right == MINUS_EXPR)
11424 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11425 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11426 return;
e534436e 11427
82012ffe 11428 case RSHIFT_EXPR:
b0e7825e 11429 if (code_left == PLUS_EXPR)
11430 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11431 "suggest parentheses around %<+%> inside %<>>%>");
11432 else if (code_right == PLUS_EXPR)
11433 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11434 "suggest parentheses around %<+%> inside %<>>%>");
11435 else if (code_left == MINUS_EXPR)
11436 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11437 "suggest parentheses around %<-%> inside %<>>%>");
11438 else if (code_right == MINUS_EXPR)
11439 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11440 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11441 return;
e534436e 11442
82012ffe 11443 case TRUTH_ORIF_EXPR:
b0e7825e 11444 if (code_left == TRUTH_ANDIF_EXPR)
11445 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11446 "suggest parentheses around %<&&%> within %<||%>");
11447 else if (code_right == TRUTH_ANDIF_EXPR)
11448 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11449 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11450 return;
11451
11452 case BIT_IOR_EXPR:
e534436e 11453 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11454 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11455 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11456 "suggest parentheses around arithmetic in operand of %<|%>");
11457 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11458 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11459 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11460 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11461 /* Check cases like x|y==z */
b0e7825e 11462 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11463 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11464 "suggest parentheses around comparison in operand of %<|%>");
11465 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11466 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11467 "suggest parentheses around comparison in operand of %<|%>");
11468 /* Check cases like !x | y */
11469 else if (code_left == TRUTH_NOT_EXPR
11470 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11471 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11472 "suggest parentheses around operand of "
11473 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11474 return;
e534436e 11475
82012ffe 11476 case BIT_XOR_EXPR:
e534436e 11477 if (code_left == BIT_AND_EXPR
b0e7825e 11478 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11479 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11480 "suggest parentheses around arithmetic in operand of %<^%>");
11481 else if (code_right == BIT_AND_EXPR
11482 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11483 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11484 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11485 /* Check cases like x^y==z */
b0e7825e 11486 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11487 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11488 "suggest parentheses around comparison in operand of %<^%>");
11489 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11490 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11491 "suggest parentheses around comparison in operand of %<^%>");
11492 return;
e534436e 11493
82012ffe 11494 case BIT_AND_EXPR:
b0e7825e 11495 if (code_left == PLUS_EXPR)
11496 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11497 "suggest parentheses around %<+%> in operand of %<&%>");
11498 else if (code_right == PLUS_EXPR)
11499 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11500 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11501 else if (code_left == MINUS_EXPR)
11502 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11503 "suggest parentheses around %<-%> in operand of %<&%>");
11504 else if (code_right == MINUS_EXPR)
11505 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11506 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11507 /* Check cases like x&y==z */
b0e7825e 11508 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11509 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11510 "suggest parentheses around comparison in operand of %<&%>");
11511 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11512 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11513 "suggest parentheses around comparison in operand of %<&%>");
11514 /* Check cases like !x & y */
11515 else if (code_left == TRUTH_NOT_EXPR
11516 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11517 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11518 "suggest parentheses around operand of "
11519 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11520 return;
e534436e 11521
82012ffe 11522 case EQ_EXPR:
b0e7825e 11523 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11524 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11525 "suggest parentheses around comparison in operand of %<==%>");
11526 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11527 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11528 "suggest parentheses around comparison in operand of %<==%>");
11529 return;
11530 case NE_EXPR:
b0e7825e 11531 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11532 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11533 "suggest parentheses around comparison in operand of %<!=%>");
11534 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11535 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11536 "suggest parentheses around comparison in operand of %<!=%>");
11537 return;
11538
11539 default:
b0e7825e 11540 if (TREE_CODE_CLASS (code) == tcc_comparison)
11541 {
11542 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11543 && code_left != NE_EXPR && code_left != EQ_EXPR
11544 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11545 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11546 "comparisons like %<X<=Y<=Z%> do not "
11547 "have their mathematical meaning");
11548 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11549 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11550 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11551 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11552 "comparisons like %<X<=Y<=Z%> do not "
11553 "have their mathematical meaning");
11554 }
82012ffe 11555 return;
6ce0c450 11556 }
82012ffe 11557#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11558}
11559
92fccaaa 11560/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11561
11562void
11563warn_for_unused_label (tree label)
11564{
11565 if (!TREE_USED (label))
11566 {
11567 if (DECL_INITIAL (label))
11568 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11569 else
11570 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11571 }
11572}
2840aae4 11573
b6889cb0 11574/* Warn for division by zero according to the value of DIVISOR. LOC
11575 is the location of the division operator. */
f092582b 11576
11577void
b6889cb0 11578warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11579{
9421ebb9 11580 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11581 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11582 floating-point type, since we consider 0.0/0.0 a valid way of
11583 generating a NaN. */
48d94ede 11584 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11585 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11586 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11587}
11588
13869a99 11589/* Subroutine of build_binary_op. Give warnings for comparisons
11590 between signed and unsigned quantities that may fail. Do the
11591 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11592 so that casts will be considered, but default promotions won't
8e70fb09 11593 be.
11594
11595 LOCATION is the location of the comparison operator.
13869a99 11596
11597 The arguments of this function map directly to local variables
11598 of build_binary_op. */
11599
48e1416a 11600void
8e70fb09 11601warn_for_sign_compare (location_t location,
48e1416a 11602 tree orig_op0, tree orig_op1,
11603 tree op0, tree op1,
13869a99 11604 tree result_type, enum tree_code resultcode)
11605{
11606 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11607 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11608 int unsignedp0, unsignedp1;
48e1416a 11609
13869a99 11610 /* In C++, check for comparison of different enum types. */
11611 if (c_dialect_cxx()
11612 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11613 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11614 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11615 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11616 {
8e70fb09 11617 warning_at (location,
11618 OPT_Wsign_compare, "comparison between types %qT and %qT",
11619 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11620 }
11621
11622 /* Do not warn if the comparison is being done in a signed type,
11623 since the signed type will only be chosen if it can represent
11624 all the values of the unsigned type. */
11625 if (!TYPE_UNSIGNED (result_type))
11626 /* OK */;
11627 /* Do not warn if both operands are unsigned. */
11628 else if (op0_signed == op1_signed)
11629 /* OK */;
11630 else
11631 {
895b662f 11632 tree sop, uop, base_type;
13869a99 11633 bool ovf;
895b662f 11634
13869a99 11635 if (op0_signed)
11636 sop = orig_op0, uop = orig_op1;
48e1416a 11637 else
13869a99 11638 sop = orig_op1, uop = orig_op0;
11639
48e1416a 11640 STRIP_TYPE_NOPS (sop);
13869a99 11641 STRIP_TYPE_NOPS (uop);
895b662f 11642 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11643 ? TREE_TYPE (result_type) : result_type);
13869a99 11644
11645 /* Do not warn if the signed quantity is an unsuffixed integer
11646 literal (or some static constant expression involving such
11647 literals or a conditional expression involving such literals)
11648 and it is non-negative. */
11649 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11650 /* OK */;
11651 /* Do not warn if the comparison is an equality operation, the
11652 unsigned quantity is an integral constant, and it would fit
11653 in the result if the result were signed. */
11654 else if (TREE_CODE (uop) == INTEGER_CST
11655 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11656 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11657 /* OK */;
11658 /* In C, do not warn if the unsigned quantity is an enumeration
11659 constant and its maximum value would fit in the result if the
11660 result were signed. */
11661 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11662 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11663 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11664 c_common_signed_type (base_type)))
13869a99 11665 /* OK */;
48e1416a 11666 else
8e70fb09 11667 warning_at (location,
48e1416a 11668 OPT_Wsign_compare,
8e70fb09 11669 "comparison between signed and unsigned integer expressions");
13869a99 11670 }
48e1416a 11671
13869a99 11672 /* Warn if two unsigned values are being compared in a size larger
11673 than their original size, and one (and only one) is the result of
11674 a `~' operator. This comparison will always fail.
48e1416a 11675
13869a99 11676 Also warn if one operand is a constant, and the constant does not
11677 have all bits set that are set in the ~ operand when it is
11678 extended. */
11679
7f506bca 11680 op0 = c_common_get_narrower (op0, &unsignedp0);
11681 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11682
13869a99 11683 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11684 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11685 {
11686 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11687 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11688 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11689 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11690
e913b5cd 11691 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11692 {
11693 tree primop;
11694 HOST_WIDE_INT constant, mask;
11695 int unsignedp;
11696 unsigned int bits;
48e1416a 11697
e913b5cd 11698 if (tree_fits_shwi_p (op0))
13869a99 11699 {
11700 primop = op1;
11701 unsignedp = unsignedp1;
e913b5cd 11702 constant = tree_to_shwi (op0);
13869a99 11703 }
11704 else
11705 {
11706 primop = op0;
11707 unsignedp = unsignedp0;
e913b5cd 11708 constant = tree_to_shwi (op1);
13869a99 11709 }
48e1416a 11710
13869a99 11711 bits = TYPE_PRECISION (TREE_TYPE (primop));
11712 if (bits < TYPE_PRECISION (result_type)
11713 && bits < HOST_BITS_PER_LONG && unsignedp)
11714 {
11715 mask = (~ (HOST_WIDE_INT) 0) << bits;
11716 if ((mask & constant) != mask)
11717 {
11718 if (constant == 0)
76fdceeb 11719 warning_at (location, OPT_Wsign_compare,
11720 "promoted ~unsigned is always non-zero");
13869a99 11721 else
48e1416a 11722 warning_at (location, OPT_Wsign_compare,
8e70fb09 11723 "comparison of promoted ~unsigned with constant");
13869a99 11724 }
11725 }
11726 }
11727 else if (unsignedp0 && unsignedp1
11728 && (TYPE_PRECISION (TREE_TYPE (op0))
11729 < TYPE_PRECISION (result_type))
11730 && (TYPE_PRECISION (TREE_TYPE (op1))
11731 < TYPE_PRECISION (result_type)))
8e70fb09 11732 warning_at (location, OPT_Wsign_compare,
13869a99 11733 "comparison of promoted ~unsigned with unsigned");
11734 }
11735}
11736
5ba33bf4 11737/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11738 type via c_common_type. If -Wdouble-promotion is in use, and the
11739 conditions for warning have been met, issue a warning. GMSGID is
11740 the warning message. It must have two %T specifiers for the type
11741 that was converted (generally "float") and the type to which it was
11742 converted (generally "double), respectively. LOC is the location
11743 to which the awrning should refer. */
11744
11745void
11746do_warn_double_promotion (tree result_type, tree type1, tree type2,
11747 const char *gmsgid, location_t loc)
11748{
11749 tree source_type;
11750
11751 if (!warn_double_promotion)
11752 return;
11753 /* If the conversion will not occur at run-time, there is no need to
11754 warn about it. */
11755 if (c_inhibit_evaluation_warnings)
11756 return;
11757 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11758 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11759 return;
11760 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11761 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11762 source_type = type1;
11763 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11764 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11765 source_type = type2;
11766 else
11767 return;
11768 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11769}
11770
41771881 11771/* Setup a TYPE_DECL node as a typedef representation.
11772
11773 X is a TYPE_DECL for a typedef statement. Create a brand new
11774 ..._TYPE node (which will be just a variant of the existing
11775 ..._TYPE node with identical properties) and then install X
11776 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11777
11778 The whole point here is to end up with a situation where each
11779 and every ..._TYPE node the compiler creates will be uniquely
11780 associated with AT MOST one node representing a typedef name.
11781 This way, even though the compiler substitutes corresponding
11782 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11783 early on, later parts of the compiler can always do the reverse
11784 translation and get back the corresponding typedef name. For
11785 example, given:
ab2c1de8 11786
41771881 11787 typedef struct S MY_TYPE;
11788 MY_TYPE object;
11789
11790 Later parts of the compiler might only know that `object' was of
11791 type `struct S' if it were not for code just below. With this
11792 code however, later parts of the compiler see something like:
11793
11794 struct S' == struct S
11795 typedef struct S' MY_TYPE;
11796 struct S' object;
11797
11798 And they can then deduce (from the node for type struct S') that
11799 the original object declaration was:
11800
11801 MY_TYPE object;
11802
11803 Being able to do this is important for proper support of protoize,
11804 and also for generating precise symbolic debugging information
11805 which takes full account of the programmer's (typedef) vocabulary.
11806
11807 Obviously, we don't want to generate a duplicate ..._TYPE node if
11808 the TYPE_DECL node that we are now processing really represents a
11809 standard built-in type. */
11810
11811void
11812set_underlying_type (tree x)
11813{
11814 if (x == error_mark_node)
11815 return;
11816 if (DECL_IS_BUILTIN (x))
11817 {
11818 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11819 TYPE_NAME (TREE_TYPE (x)) = x;
11820 }
11821 else if (TREE_TYPE (x) != error_mark_node
11822 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11823 {
11824 tree tt = TREE_TYPE (x);
11825 DECL_ORIGINAL_TYPE (x) = tt;
11826 tt = build_variant_type_copy (tt);
11827 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11828 TYPE_NAME (tt) = x;
11829 TREE_USED (tt) = TREE_USED (x);
11830 TREE_TYPE (x) = tt;
11831 }
11832}
11833
1a4c44c5 11834/* Record the types used by the current global variable declaration
11835 being parsed, so that we can decide later to emit their debug info.
11836 Those types are in types_used_by_cur_var_decl, and we are going to
11837 store them in the types_used_by_vars_hash hash table.
11838 DECL is the declaration of the global variable that has been parsed. */
11839
11840void
11841record_types_used_by_current_var_decl (tree decl)
11842{
11843 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11844
f1f41a6c 11845 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 11846 {
f1f41a6c 11847 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 11848 types_used_by_var_decl_insert (type, decl);
1a4c44c5 11849 }
11850}
11851
a4e3ffad 11852/* If DECL is a typedef that is declared in the current function,
11853 record it for the purpose of -Wunused-local-typedefs. */
11854
11855void
11856record_locally_defined_typedef (tree decl)
11857{
11858 struct c_language_function *l;
11859
11860 if (!warn_unused_local_typedefs
11861 || cfun == NULL
11862 /* if this is not a locally defined typedef then we are not
11863 interested. */
11864 || !is_typedef_decl (decl)
11865 || !decl_function_context (decl))
11866 return;
11867
11868 l = (struct c_language_function *) cfun->language;
f1f41a6c 11869 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 11870}
11871
11872/* If T is a TYPE_DECL declared locally, mark it as used. */
11873
11874void
11875maybe_record_typedef_use (tree t)
11876{
11877 if (!is_typedef_decl (t))
11878 return;
11879
11880 TREE_USED (t) = true;
11881}
11882
11883/* Warn if there are some unused locally defined typedefs in the
11884 current function. */
11885
11886void
11887maybe_warn_unused_local_typedefs (void)
11888{
11889 int i;
11890 tree decl;
11891 /* The number of times we have emitted -Wunused-local-typedefs
11892 warnings. If this is different from errorcount, that means some
11893 unrelated errors have been issued. In which case, we'll avoid
11894 emitting "unused-local-typedefs" warnings. */
11895 static int unused_local_typedefs_warn_count;
11896 struct c_language_function *l;
11897
11898 if (cfun == NULL)
11899 return;
11900
11901 if ((l = (struct c_language_function *) cfun->language) == NULL)
11902 return;
11903
11904 if (warn_unused_local_typedefs
11905 && errorcount == unused_local_typedefs_warn_count)
11906 {
f1f41a6c 11907 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 11908 if (!TREE_USED (decl))
11909 warning_at (DECL_SOURCE_LOCATION (decl),
11910 OPT_Wunused_local_typedefs,
11911 "typedef %qD locally defined but not used", decl);
11912 unused_local_typedefs_warn_count = errorcount;
11913 }
11914
f1f41a6c 11915 vec_free (l->local_typedefs);
a4e3ffad 11916}
11917
78bf4156 11918/* Warn about boolean expression compared with an integer value different
11919 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11920 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11921 are the operands of the comparison. The caller must ensure that
11922 either operand is a boolean expression. */
11923
11924void
11925maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11926 tree op1)
11927{
11928 if (TREE_CODE_CLASS (code) != tcc_comparison)
11929 return;
11930
11931 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11932 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11933 if (!cst)
11934 return;
11935
11936 if (!integer_zerop (cst) && !integer_onep (cst))
11937 {
a720ab1c 11938 int sign = (TREE_CODE (op0) == INTEGER_CST
11939 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 11940 if (code == EQ_EXPR
11941 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11942 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11943 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11944 "with boolean expression is always false", cst);
11945 else
11946 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11947 "with boolean expression is always true", cst);
11948 }
a720ab1c 11949 else if (integer_zerop (cst) || integer_onep (cst))
11950 {
14744a16 11951 /* If the non-constant operand isn't of a boolean type, we
11952 don't want to warn here. */
11953 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
11954 /* Handle booleans promoted to integers. */
11955 if (CONVERT_EXPR_P (noncst)
11956 && TREE_TYPE (noncst) == integer_type_node
11957 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
11958 /* Warn. */;
11959 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
11960 && !truth_value_p (TREE_CODE (noncst)))
11961 return;
a720ab1c 11962 /* Do some magic to get the right diagnostics. */
11963 bool flag = TREE_CODE (op0) == INTEGER_CST;
11964 flag = integer_zerop (cst) ? flag : !flag;
11965 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
11966 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11967 "with boolean expression is always true", cst);
11968 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
11969 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11970 "with boolean expression is always false", cst);
11971 }
78bf4156 11972}
11973
f352a3fb 11974/* The C and C++ parsers both use vectors to hold function arguments.
11975 For efficiency, we keep a cache of unused vectors. This is the
11976 cache. */
11977
f1f41a6c 11978typedef vec<tree, va_gc> *tree_gc_vec;
11979static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 11980
11981/* Return a new vector from the cache. If the cache is empty,
11982 allocate a new vector. These vectors are GC'ed, so it is OK if the
11983 pointer is not released.. */
11984
f1f41a6c 11985vec<tree, va_gc> *
f352a3fb 11986make_tree_vector (void)
11987{
f1f41a6c 11988 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11989 return tree_vector_cache->pop ();
f352a3fb 11990 else
11991 {
f1f41a6c 11992 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 11993 that we always return a non-NULL value. The vector code uses
11994 4 when growing a NULL vector, so we do too. */
f1f41a6c 11995 vec<tree, va_gc> *v;
11996 vec_alloc (v, 4);
11997 return v;
f352a3fb 11998 }
11999}
12000
12001/* Release a vector of trees back to the cache. */
12002
12003void
f1f41a6c 12004release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12005{
12006 if (vec != NULL)
12007 {
f1f41a6c 12008 vec->truncate (0);
12009 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12010 }
12011}
12012
12013/* Get a new tree vector holding a single tree. */
12014
f1f41a6c 12015vec<tree, va_gc> *
f352a3fb 12016make_tree_vector_single (tree t)
12017{
f1f41a6c 12018 vec<tree, va_gc> *ret = make_tree_vector ();
12019 ret->quick_push (t);
f352a3fb 12020 return ret;
12021}
12022
c66c81be 12023/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12024
f1f41a6c 12025vec<tree, va_gc> *
c66c81be 12026make_tree_vector_from_list (tree list)
12027{
f1f41a6c 12028 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12029 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12030 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12031 return ret;
12032}
12033
f352a3fb 12034/* Get a new tree vector which is a copy of an existing one. */
12035
f1f41a6c 12036vec<tree, va_gc> *
12037make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12038{
f1f41a6c 12039 vec<tree, va_gc> *ret;
f352a3fb 12040 unsigned int ix;
12041 tree t;
12042
12043 ret = make_tree_vector ();
f1f41a6c 12044 vec_safe_reserve (ret, vec_safe_length (orig));
12045 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12046 ret->quick_push (t);
f352a3fb 12047 return ret;
12048}
12049
a9ffdd35 12050/* Return true if KEYWORD starts a type specifier. */
12051
12052bool
12053keyword_begins_type_specifier (enum rid keyword)
12054{
12055 switch (keyword)
12056 {
4fba5eb9 12057 case RID_AUTO_TYPE:
a9ffdd35 12058 case RID_INT:
12059 case RID_CHAR:
12060 case RID_FLOAT:
12061 case RID_DOUBLE:
12062 case RID_VOID:
a9ffdd35 12063 case RID_UNSIGNED:
12064 case RID_LONG:
12065 case RID_SHORT:
12066 case RID_SIGNED:
12067 case RID_DFLOAT32:
12068 case RID_DFLOAT64:
12069 case RID_DFLOAT128:
12070 case RID_FRACT:
12071 case RID_ACCUM:
12072 case RID_BOOL:
12073 case RID_WCHAR:
12074 case RID_CHAR16:
12075 case RID_CHAR32:
12076 case RID_SAT:
12077 case RID_COMPLEX:
12078 case RID_TYPEOF:
12079 case RID_STRUCT:
12080 case RID_CLASS:
12081 case RID_UNION:
12082 case RID_ENUM:
12083 return true;
12084 default:
9f75f026 12085 if (keyword >= RID_FIRST_INT_N
12086 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12087 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12088 return true;
a9ffdd35 12089 return false;
12090 }
12091}
12092
12093/* Return true if KEYWORD names a type qualifier. */
12094
12095bool
12096keyword_is_type_qualifier (enum rid keyword)
12097{
12098 switch (keyword)
12099 {
12100 case RID_CONST:
12101 case RID_VOLATILE:
12102 case RID_RESTRICT:
b560fabd 12103 case RID_ATOMIC:
a9ffdd35 12104 return true;
12105 default:
12106 return false;
12107 }
12108}
12109
12110/* Return true if KEYWORD names a storage class specifier.
12111
12112 RID_TYPEDEF is not included in this list despite `typedef' being
12113 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12114 such for syntactic convenience only. */
12115
12116bool
12117keyword_is_storage_class_specifier (enum rid keyword)
12118{
12119 switch (keyword)
12120 {
12121 case RID_STATIC:
12122 case RID_EXTERN:
12123 case RID_REGISTER:
12124 case RID_AUTO:
12125 case RID_MUTABLE:
12126 case RID_THREAD:
12127 return true;
12128 default:
12129 return false;
12130 }
12131}
12132
fad3f658 12133/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12134
12135static bool
12136keyword_is_function_specifier (enum rid keyword)
12137{
12138 switch (keyword)
12139 {
12140 case RID_INLINE:
985c6e3a 12141 case RID_NORETURN:
fad3f658 12142 case RID_VIRTUAL:
12143 case RID_EXPLICIT:
12144 return true;
12145 default:
12146 return false;
12147 }
12148}
12149
12150/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12151 declaration-specifier (C99 6.7). */
12152
12153bool
12154keyword_is_decl_specifier (enum rid keyword)
12155{
12156 if (keyword_is_storage_class_specifier (keyword)
12157 || keyword_is_type_qualifier (keyword)
12158 || keyword_is_function_specifier (keyword))
12159 return true;
12160
12161 switch (keyword)
12162 {
12163 case RID_TYPEDEF:
12164 case RID_FRIEND:
12165 case RID_CONSTEXPR:
12166 return true;
12167 default:
12168 return false;
12169 }
12170}
12171
9b88d08d 12172/* Initialize language-specific-bits of tree_contains_struct. */
12173
12174void
12175c_common_init_ts (void)
12176{
12177 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12178 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12179 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12180}
12181
244db24d 12182/* Build a user-defined numeric literal out of an integer constant type VALUE
12183 with identifier SUFFIX. */
12184
12185tree
324ca377 12186build_userdef_literal (tree suffix_id, tree value,
12187 enum overflow_type overflow, tree num_string)
244db24d 12188{
12189 tree literal = make_node (USERDEF_LITERAL);
12190 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12191 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12192 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12193 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12194 return literal;
12195}
12196
7059d45d 12197/* For vector[index], convert the vector to a
c61ef207 12198 pointer of the underlying type. Return true if the resulting
12199 ARRAY_REF should not be an lvalue. */
12200
12201bool
7059d45d 12202convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12203 tree *vecp, tree index)
7059d45d 12204{
c61ef207 12205 bool ret = false;
7059d45d 12206 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12207 {
12208 tree type = TREE_TYPE (*vecp);
12209 tree type1;
12210
c61ef207 12211 ret = !lvalue_p (*vecp);
7059d45d 12212 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12213 if (!tree_fits_uhwi_p (index)
aa59f000 12214 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12215 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12216
c61ef207 12217 if (ret)
12218 {
f9e245b2 12219 tree tmp = create_tmp_var_raw (type);
c61ef207 12220 DECL_SOURCE_LOCATION (tmp) = loc;
12221 *vecp = c_save_expr (*vecp);
12222 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12223 {
12224 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12225 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12226 *vecp
12227 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12228 *vecp, NULL_TREE, NULL_TREE),
12229 non_const);
12230 }
12231 else
12232 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12233 NULL_TREE, NULL_TREE);
12234 SET_EXPR_LOCATION (*vecp, loc);
12235 c_common_mark_addressable_vec (tmp);
12236 }
12237 else
12238 c_common_mark_addressable_vec (*vecp);
7059d45d 12239 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12240 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12241 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12242 if (!ref_all
12243 && !DECL_P (*vecp))
12244 {
12245 /* If the original vector isn't declared may_alias and it
12246 isn't a bare vector look if the subscripting would
12247 alias the vector we subscript, and if not, force ref-all. */
12248 alias_set_type vecset = get_alias_set (*vecp);
12249 alias_set_type sset = get_alias_set (type);
12250 if (!alias_sets_must_conflict_p (sset, vecset)
12251 && !alias_set_subset_of (sset, vecset))
12252 ref_all = true;
12253 }
12254 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12255 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12256 *vecp = convert (type, *vecp);
12257 }
c61ef207 12258 return ret;
7059d45d 12259}
12260
41ed701a 12261/* Determine which of the operands, if any, is a scalar that needs to be
12262 converted to a vector, for the range of operations. */
12263enum stv_conv
12264scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12265 bool complain)
12266{
12267 tree type0 = TREE_TYPE (op0);
12268 tree type1 = TREE_TYPE (op1);
12269 bool integer_only_op = false;
12270 enum stv_conv ret = stv_firstarg;
12271
12272 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12273 || TREE_CODE (type1) == VECTOR_TYPE);
12274 switch (code)
12275 {
12276 /* Most GENERIC binary expressions require homogeneous arguments.
12277 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12278 argument that is a vector and a second one that is a scalar, so
12279 we never return stv_secondarg for them. */
12280 case RSHIFT_EXPR:
12281 case LSHIFT_EXPR:
12282 if (TREE_CODE (type0) == INTEGER_TYPE
12283 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12284 {
22a75734 12285 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12286 {
12287 if (complain)
12288 error_at (loc, "conversion of scalar %qT to vector %qT "
12289 "involves truncation", type0, type1);
12290 return stv_error;
12291 }
12292 else
12293 return stv_firstarg;
12294 }
12295 break;
12296
12297 case BIT_IOR_EXPR:
12298 case BIT_XOR_EXPR:
12299 case BIT_AND_EXPR:
12300 integer_only_op = true;
12301 /* ... fall through ... */
12302
7b463b19 12303 case VEC_COND_EXPR:
12304
41ed701a 12305 case PLUS_EXPR:
12306 case MINUS_EXPR:
12307 case MULT_EXPR:
12308 case TRUNC_DIV_EXPR:
12309 case CEIL_DIV_EXPR:
12310 case FLOOR_DIV_EXPR:
12311 case ROUND_DIV_EXPR:
12312 case EXACT_DIV_EXPR:
12313 case TRUNC_MOD_EXPR:
12314 case FLOOR_MOD_EXPR:
12315 case RDIV_EXPR:
12316 case EQ_EXPR:
12317 case NE_EXPR:
12318 case LE_EXPR:
12319 case GE_EXPR:
12320 case LT_EXPR:
12321 case GT_EXPR:
12322 /* What about UNLT_EXPR? */
12323 if (TREE_CODE (type0) == VECTOR_TYPE)
12324 {
12325 tree tmp;
12326 ret = stv_secondarg;
12327 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12328 tmp = type0; type0 = type1; type1 = tmp;
12329 tmp = op0; op0 = op1; op1 = tmp;
12330 }
12331
12332 if (TREE_CODE (type0) == INTEGER_TYPE
12333 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12334 {
22a75734 12335 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12336 {
12337 if (complain)
12338 error_at (loc, "conversion of scalar %qT to vector %qT "
12339 "involves truncation", type0, type1);
12340 return stv_error;
12341 }
12342 return ret;
12343 }
12344 else if (!integer_only_op
12345 /* Allow integer --> real conversion if safe. */
12346 && (TREE_CODE (type0) == REAL_TYPE
12347 || TREE_CODE (type0) == INTEGER_TYPE)
12348 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12349 {
22a75734 12350 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12351 {
12352 if (complain)
12353 error_at (loc, "conversion of scalar %qT to vector %qT "
12354 "involves truncation", type0, type1);
12355 return stv_error;
12356 }
12357 return ret;
12358 }
12359 default:
12360 break;
12361 }
12362
12363 return stv_nothing;
12364}
12365
ffcdbf9c 12366/* Return true iff ALIGN is an integral constant that is a fundamental
12367 alignment, as defined by [basic.align] in the c++-11
12368 specifications.
12369
12370 That is:
12371
12372 [A fundamental alignment is represented by an alignment less than or
12373 equal to the greatest alignment supported by the implementation
12374 in all contexts, which is equal to
12375 alignof(max_align_t)]. */
12376
12377bool
12378cxx_fundamental_alignment_p (unsigned align)
12379{
12380 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12381 TYPE_ALIGN (long_double_type_node)));
12382}
12383
46da3601 12384/* Return true if T is a pointer to a zero-sized aggregate. */
12385
12386bool
12387pointer_to_zero_sized_aggr_p (tree t)
12388{
12389 if (!POINTER_TYPE_P (t))
12390 return false;
12391 t = TREE_TYPE (t);
12392 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12393}
12394
7bedc3a0 12395#include "gt-c-family-c-common.h"