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