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