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