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