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