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