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