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