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