1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* FIXME: Still need to include rtl.h here (via expr.h) in a front-end file.
23 Pretend this is a back-end file. */
24 #undef IN_GCC_FRONTEND
28 #include "coretypes.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
44 #include "diagnostic.h"
45 #include "tree-iterator.h"
47 #include "tree-mudflap.h"
50 #include "target-def.h"
53 #include "expr.h" /* For vector_mode_valid_p */
55 cpp_reader *parse_in; /* Declared in c-pragma.h. */
57 /* The following symbols are subsumed in the c_global_trees array, and
58 listed here individually for documentation purposes.
60 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
62 tree short_integer_type_node;
63 tree long_integer_type_node;
64 tree long_long_integer_type_node;
65 tree int128_integer_type_node;
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
70 tree int128_unsigned_type_node;
72 tree truthvalue_type_node;
73 tree truthvalue_false_node;
74 tree truthvalue_true_node;
76 tree ptrdiff_type_node;
78 tree unsigned_char_type_node;
79 tree signed_char_type_node;
82 tree char16_type_node;
83 tree char32_type_node;
86 tree double_type_node;
87 tree long_double_type_node;
89 tree complex_integer_type_node;
90 tree complex_float_type_node;
91 tree complex_double_type_node;
92 tree complex_long_double_type_node;
94 tree dfloat32_type_node;
95 tree dfloat64_type_node;
96 tree_dfloat128_type_node;
100 tree intSI_type_node;
101 tree intDI_type_node;
102 tree intTI_type_node;
104 tree unsigned_intQI_type_node;
105 tree unsigned_intHI_type_node;
106 tree unsigned_intSI_type_node;
107 tree unsigned_intDI_type_node;
108 tree unsigned_intTI_type_node;
110 tree widest_integer_literal_type_node;
111 tree widest_unsigned_literal_type_node;
113 Nodes for types `void *' and `const void *'.
115 tree ptr_type_node, const_ptr_type_node;
117 Nodes for types `char *' and `const char *'.
119 tree string_type_node, const_string_type_node;
121 Type `char[SOMENUMBER]'.
122 Used when an array of char is needed and the size is irrelevant.
124 tree char_array_type_node;
126 Type `int[SOMENUMBER]' or something like it.
127 Used when an array of int needed and the size is irrelevant.
129 tree int_array_type_node;
131 Type `wchar_t[SOMENUMBER]' or something like it.
132 Used when a wide string literal is created.
134 tree wchar_array_type_node;
136 Type `char16_t[SOMENUMBER]' or something like it.
137 Used when a UTF-16 string literal is created.
139 tree char16_array_type_node;
141 Type `char32_t[SOMENUMBER]' or something like it.
142 Used when a UTF-32 string literal is created.
144 tree char32_array_type_node;
146 Type `int ()' -- used for implicit declaration of functions.
148 tree default_function_type;
150 A VOID_TYPE node, packaged in a TREE_LIST.
154 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
155 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
156 VAR_DECLS, but C++ does.)
158 tree function_name_decl_node;
159 tree pretty_function_name_decl_node;
160 tree c99_function_name_decl_node;
162 Stack of nested function name VAR_DECLs.
164 tree saved_function_name_decls;
168 tree c_global_trees[CTI_MAX];
170 /* Switches common to the C front ends. */
172 /* Nonzero if preprocessing only. */
174 int flag_preprocess_only;
176 /* Nonzero means don't output line number information. */
178 char flag_no_line_commands;
180 /* Nonzero causes -E output not to be done, but directives such as
181 #define that have side effects are still obeyed. */
185 /* Nonzero means dump macros in some fashion. */
187 char flag_dump_macros;
189 /* Nonzero means pass #include lines through to the output. */
191 char flag_dump_includes;
193 /* Nonzero means process PCH files while preprocessing. */
195 bool flag_pch_preprocess;
197 /* The file name to which we should write a precompiled header, or
198 NULL if no header will be written in this compile. */
200 const char *pch_file;
202 /* Nonzero if an ISO standard was selected. It rejects macros in the
206 /* Nonzero if -undef was given. It suppresses target built-in macros
210 /* Nonzero means don't recognize the non-ANSI builtin functions. */
214 /* Nonzero means don't recognize the non-ANSI builtin functions.
217 int flag_no_nonansi_builtin;
219 /* Nonzero means give `double' the same size as `float'. */
221 int flag_short_double;
223 /* Nonzero means give `wchar_t' the same size as `short'. */
225 int flag_short_wchar;
227 /* Nonzero means allow implicit conversions between vectors with
228 differing numbers of subparts and/or differing element types. */
229 int flag_lax_vector_conversions;
231 /* Nonzero means allow Microsoft extensions without warnings or errors. */
232 int flag_ms_extensions;
234 /* Nonzero means don't recognize the keyword `asm'. */
238 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
240 int flag_signed_bitfields = 1;
242 /* Warn about #pragma directives that are not recognized. */
244 int warn_unknown_pragmas; /* Tri state variable. */
246 /* Warn about format/argument anomalies in calls to formatted I/O functions
247 (*printf, *scanf, strftime, strfmon, etc.). */
251 /* Warn about using __null (as NULL in C++) as sentinel. For code compiled
252 with GCC this doesn't matter as __null is guaranteed to have the right
255 int warn_strict_null_sentinel;
257 /* Zero means that faster, ...NonNil variants of objc_msgSend...
258 calls will be used in ObjC; passing nil receivers to such calls
259 will most likely result in crashes. */
260 int flag_nil_receivers = 1;
262 /* Nonzero means that code generation will be altered to support
263 "zero-link" execution. This currently affects ObjC only, but may
264 affect other languages in the future. */
265 int flag_zero_link = 0;
267 /* Nonzero means emit an '__OBJC, __image_info' for the current translation
268 unit. It will inform the ObjC runtime that class definition(s) herein
269 contained are to replace one(s) previously loaded. */
270 int flag_replace_objc_classes = 0;
272 /* C/ObjC language option variables. */
275 /* Nonzero means allow type mismatches in conditional expressions;
276 just make their values `void'. */
278 int flag_cond_mismatch;
280 /* Nonzero means enable C89 Amendment 1 features. */
284 /* Nonzero means use the ISO C99 (or C1X) dialect of C. */
288 /* Nonzero means use the ISO C1X dialect of C. */
292 /* Nonzero means that we have builtin functions, and main is an int. */
297 /* ObjC language option variables. */
300 /* Open and close the file for outputting class declarations, if
303 int flag_gen_declaration;
305 /* Tells the compiler that this is a special run. Do not perform any
306 compiling, instead we are to test some platform dependent features
307 and output a C header file with appropriate definitions. */
309 int print_struct_values;
311 /* Tells the compiler what is the constant string class for ObjC. */
313 const char *constant_string_class_name;
316 /* C++ language option variables. */
319 /* Nonzero means don't recognize any extension keywords. */
321 int flag_no_gnu_keywords;
323 /* Nonzero means do emit exported implementations of functions even if
324 they can be inlined. */
326 int flag_implement_inlines = 1;
328 /* Nonzero means that implicit instantiations will be emitted if needed. */
330 int flag_implicit_templates = 1;
332 /* Nonzero means that implicit instantiations of inline templates will be
333 emitted if needed, even if instantiations of non-inline templates
336 int flag_implicit_inline_templates = 1;
338 /* Nonzero means generate separate instantiation control files and
339 juggle them at link time. */
341 int flag_use_repository;
343 /* Nonzero if we want to issue diagnostics that the standard says are not
346 int flag_optional_diags = 1;
348 /* Nonzero means we should attempt to elide constructors when possible. */
350 int flag_elide_constructors = 1;
352 /* Nonzero means that member functions defined in class scope are
353 inline by default. */
355 int flag_default_inline = 1;
357 /* Controls whether compiler generates 'type descriptor' that give
358 run-time type information. */
362 /* Nonzero if we want to conserve space in the .o files. We do this
363 by putting uninitialized data and runtime initialized data into
364 .common instead of .data at the expense of not flagging multiple
367 int flag_conserve_space;
369 /* Nonzero if we want to obey access control semantics. */
371 int flag_access_control = 1;
373 /* Nonzero if we want to check the return value of new and avoid calling
374 constructors if it is a null pointer. */
378 /* The C++ dialect being used. C++98 is the default. */
380 enum cxx_dialect cxx_dialect = cxx98;
382 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
383 initialization variables.
384 0: Old rules, set by -fno-for-scope.
385 2: New ISO rules, set by -ffor-scope.
386 1: Try to implement new ISO rules, but with backup compatibility
387 (and warnings). This is the default, for now. */
389 int flag_new_for_scope = 1;
391 /* Nonzero if we want to emit defined symbols with common-like linkage as
392 weak symbols where possible, in order to conform to C++ semantics.
393 Otherwise, emit them as local symbols. */
397 /* 0 means we want the preprocessor to not emit line directives for
398 the current working directory. 1 means we want it to do it. -1
399 means we should decide depending on whether debugging information
400 is being emitted or not. */
402 int flag_working_directory = -1;
404 /* Nonzero to use __cxa_atexit, rather than atexit, to register
405 destructors for local statics and global objects. '2' means it has been
406 set nonzero as a default, not by a command-line flag. */
408 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
410 /* Nonzero to use __cxa_get_exception_ptr in C++ exception-handling
411 code. '2' means it has not been set explicitly on the command line. */
413 int flag_use_cxa_get_exception_ptr = 2;
415 /* Nonzero means to implement standard semantics for exception
416 specifications, calling unexpected if an exception is thrown that
417 doesn't match the specification. Zero means to treat them as
418 assertions and optimize accordingly, but not check them. */
420 int flag_enforce_eh_specs = 1;
422 /* Nonzero means to generate thread-safe code for initializing local
425 int flag_threadsafe_statics = 1;
427 /* Nonzero if we want to pretty-print template specializations as the
428 template signature followed by the arguments. */
430 int flag_pretty_templates = 1;
432 /* Maximum template instantiation depth. This limit exists to limit the
433 time it takes to notice infinite template instantiations; the default
434 value of 1024 is likely to be in the next C++ standard. */
436 int max_tinst_depth = 1024;
440 /* The elements of `ridpointers' are identifier nodes for the reserved
441 type names and storage classes. It is indexed by a RID_... value. */
444 tree (*make_fname_decl) (location_t, tree, int);
446 /* Nonzero means don't warn about problems that occur when the code is
448 int c_inhibit_evaluation_warnings;
450 /* Whether lexing has been completed, so subsequent preprocessor
451 errors should use the compiler's input_location. */
452 bool done_lexing = false;
454 /* Information about how a function name is generated. */
457 tree *const decl; /* pointer to the VAR_DECL. */
458 const unsigned rid; /* RID number for the identifier. */
459 const int pretty; /* How pretty is it? */
462 /* The three ways of getting then name of the current function. */
464 const struct fname_var_t fname_vars[] =
466 /* C99 compliant __func__, must be first. */
467 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
468 /* GCC __FUNCTION__ compliant. */
469 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
470 /* GCC __PRETTY_FUNCTION__ compliant. */
471 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
475 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
476 static tree check_case_value (tree);
477 static bool check_case_bounds (tree, tree, tree *, tree *);
479 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
480 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
481 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
482 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
483 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
484 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
485 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
486 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
487 static tree handle_always_inline_attribute (tree *, tree, tree, int,
489 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
490 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
491 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
492 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
493 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
494 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
495 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
497 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
498 static tree handle_transparent_union_attribute (tree *, tree, tree,
500 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
501 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
502 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
503 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
504 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
505 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
506 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
507 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
508 static tree handle_visibility_attribute (tree *, tree, tree, int,
510 static tree handle_tls_model_attribute (tree *, tree, tree, int,
512 static tree handle_no_instrument_function_attribute (tree *, tree,
514 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
515 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
516 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
518 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
519 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
520 static tree handle_deprecated_attribute (tree *, tree, tree, int,
522 static tree handle_vector_size_attribute (tree *, tree, tree, int,
524 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
525 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
526 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
527 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
529 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
530 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
531 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
532 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
533 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
534 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
536 static void check_function_nonnull (tree, int, tree *);
537 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
538 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
539 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
540 static int resort_field_decl_cmp (const void *, const void *);
542 /* Reserved words. The third field is a mask: keywords are disabled
543 if they match the mask.
546 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
547 C --std=c99: D_CXXONLY | D_OBJC
548 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
549 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
550 C++ --std=c0x: D_CONLY | D_OBJC
551 ObjC++ is like C++ except that D_OBJC is not set
553 If -fno-asm is used, D_ASM is added to the mask. If
554 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
555 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
556 In C with -Wc++-compat, we warn if D_CXXWARN is set. */
558 const struct c_common_resword c_common_reswords[] =
560 { "_Bool", RID_BOOL, D_CONLY },
561 { "_Complex", RID_COMPLEX, 0 },
562 { "_Imaginary", RID_IMAGINARY, D_CONLY },
563 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
564 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
565 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
566 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
567 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
568 { "_Sat", RID_SAT, D_CONLY | D_EXT },
569 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
570 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
571 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
572 { "__alignof", RID_ALIGNOF, 0 },
573 { "__alignof__", RID_ALIGNOF, 0 },
574 { "__asm", RID_ASM, 0 },
575 { "__asm__", RID_ASM, 0 },
576 { "__attribute", RID_ATTRIBUTE, 0 },
577 { "__attribute__", RID_ATTRIBUTE, 0 },
578 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
579 { "__builtin_offsetof", RID_OFFSETOF, 0 },
580 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
581 { "__builtin_va_arg", RID_VA_ARG, 0 },
582 { "__complex", RID_COMPLEX, 0 },
583 { "__complex__", RID_COMPLEX, 0 },
584 { "__const", RID_CONST, 0 },
585 { "__const__", RID_CONST, 0 },
586 { "__decltype", RID_DECLTYPE, D_CXXONLY },
587 { "__extension__", RID_EXTENSION, 0 },
588 { "__func__", RID_C99_FUNCTION_NAME, 0 },
589 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
590 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
591 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
592 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
593 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
594 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
595 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
596 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
597 { "__int128", RID_INT128, 0 },
598 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
599 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
600 { "__is_class", RID_IS_CLASS, D_CXXONLY },
601 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
602 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
603 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
604 { "__is_pod", RID_IS_POD, D_CXXONLY },
605 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
606 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
607 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
608 { "__is_union", RID_IS_UNION, D_CXXONLY },
609 { "__imag", RID_IMAGPART, 0 },
610 { "__imag__", RID_IMAGPART, 0 },
611 { "__inline", RID_INLINE, 0 },
612 { "__inline__", RID_INLINE, 0 },
613 { "__label__", RID_LABEL, 0 },
614 { "__null", RID_NULL, 0 },
615 { "__real", RID_REALPART, 0 },
616 { "__real__", RID_REALPART, 0 },
617 { "__restrict", RID_RESTRICT, 0 },
618 { "__restrict__", RID_RESTRICT, 0 },
619 { "__signed", RID_SIGNED, 0 },
620 { "__signed__", RID_SIGNED, 0 },
621 { "__thread", RID_THREAD, 0 },
622 { "__typeof", RID_TYPEOF, 0 },
623 { "__typeof__", RID_TYPEOF, 0 },
624 { "__volatile", RID_VOLATILE, 0 },
625 { "__volatile__", RID_VOLATILE, 0 },
626 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
627 { "asm", RID_ASM, D_ASM },
628 { "auto", RID_AUTO, 0 },
629 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
630 { "break", RID_BREAK, 0 },
631 { "case", RID_CASE, 0 },
632 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
633 { "char", RID_CHAR, 0 },
634 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
635 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
636 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
637 { "const", RID_CONST, 0 },
638 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
639 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
640 { "continue", RID_CONTINUE, 0 },
641 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
642 { "default", RID_DEFAULT, 0 },
643 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
645 { "double", RID_DOUBLE, 0 },
646 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
647 { "else", RID_ELSE, 0 },
648 { "enum", RID_ENUM, 0 },
649 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
650 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
651 { "extern", RID_EXTERN, 0 },
652 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
653 { "float", RID_FLOAT, 0 },
654 { "for", RID_FOR, 0 },
655 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
656 { "goto", RID_GOTO, 0 },
658 { "inline", RID_INLINE, D_EXT89 },
659 { "int", RID_INT, 0 },
660 { "long", RID_LONG, 0 },
661 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
662 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
663 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
664 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
665 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
666 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
667 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
668 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
669 { "register", RID_REGISTER, 0 },
670 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
671 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
672 { "return", RID_RETURN, 0 },
673 { "short", RID_SHORT, 0 },
674 { "signed", RID_SIGNED, 0 },
675 { "sizeof", RID_SIZEOF, 0 },
676 { "static", RID_STATIC, 0 },
677 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
678 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
679 { "struct", RID_STRUCT, 0 },
680 { "switch", RID_SWITCH, 0 },
681 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
682 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
683 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
684 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
685 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
686 { "typedef", RID_TYPEDEF, 0 },
687 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
688 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
689 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
690 { "union", RID_UNION, 0 },
691 { "unsigned", RID_UNSIGNED, 0 },
692 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
693 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
694 { "void", RID_VOID, 0 },
695 { "volatile", RID_VOLATILE, 0 },
696 { "wchar_t", RID_WCHAR, D_CXXONLY },
697 { "while", RID_WHILE, 0 },
698 /* These Objective-C keywords are recognized only immediately after
700 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
701 { "defs", RID_AT_DEFS, D_OBJC },
702 { "encode", RID_AT_ENCODE, D_OBJC },
703 { "end", RID_AT_END, D_OBJC },
704 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
705 { "interface", RID_AT_INTERFACE, D_OBJC },
706 { "protocol", RID_AT_PROTOCOL, D_OBJC },
707 { "selector", RID_AT_SELECTOR, D_OBJC },
708 { "finally", RID_AT_FINALLY, D_OBJC },
709 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
710 /* These are recognized only in protocol-qualifier context
712 { "bycopy", RID_BYCOPY, D_OBJC },
713 { "byref", RID_BYREF, D_OBJC },
714 { "in", RID_IN, D_OBJC },
715 { "inout", RID_INOUT, D_OBJC },
716 { "oneway", RID_ONEWAY, D_OBJC },
717 { "out", RID_OUT, D_OBJC },
720 const unsigned int num_c_common_reswords =
721 sizeof c_common_reswords / sizeof (struct c_common_resword);
723 /* Table of machine-independent attributes common to all C-like languages. */
724 const struct attribute_spec c_common_attribute_table[] =
726 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
727 { "packed", 0, 0, false, false, false,
728 handle_packed_attribute },
729 { "nocommon", 0, 0, true, false, false,
730 handle_nocommon_attribute },
731 { "common", 0, 0, true, false, false,
732 handle_common_attribute },
733 /* FIXME: logically, noreturn attributes should be listed as
734 "false, true, true" and apply to function types. But implementing this
735 would require all the places in the compiler that use TREE_THIS_VOLATILE
736 on a decl to identify non-returning functions to be located and fixed
737 to check the function type instead. */
738 { "noreturn", 0, 0, true, false, false,
739 handle_noreturn_attribute },
740 { "volatile", 0, 0, true, false, false,
741 handle_noreturn_attribute },
742 { "noinline", 0, 0, true, false, false,
743 handle_noinline_attribute },
744 { "noclone", 0, 0, true, false, false,
745 handle_noclone_attribute },
746 { "always_inline", 0, 0, true, false, false,
747 handle_always_inline_attribute },
748 { "gnu_inline", 0, 0, true, false, false,
749 handle_gnu_inline_attribute },
750 { "artificial", 0, 0, true, false, false,
751 handle_artificial_attribute },
752 { "flatten", 0, 0, true, false, false,
753 handle_flatten_attribute },
754 { "used", 0, 0, true, false, false,
755 handle_used_attribute },
756 { "unused", 0, 0, false, false, false,
757 handle_unused_attribute },
758 { "externally_visible", 0, 0, true, false, false,
759 handle_externally_visible_attribute },
760 /* The same comments as for noreturn attributes apply to const ones. */
761 { "const", 0, 0, true, false, false,
762 handle_const_attribute },
763 { "transparent_union", 0, 0, false, false, false,
764 handle_transparent_union_attribute },
765 { "constructor", 0, 1, true, false, false,
766 handle_constructor_attribute },
767 { "destructor", 0, 1, true, false, false,
768 handle_destructor_attribute },
769 { "mode", 1, 1, false, true, false,
770 handle_mode_attribute },
771 { "section", 1, 1, true, false, false,
772 handle_section_attribute },
773 { "aligned", 0, 1, false, false, false,
774 handle_aligned_attribute },
775 { "weak", 0, 0, true, false, false,
776 handle_weak_attribute },
777 { "alias", 1, 1, true, false, false,
778 handle_alias_attribute },
779 { "weakref", 0, 1, true, false, false,
780 handle_weakref_attribute },
781 { "no_instrument_function", 0, 0, true, false, false,
782 handle_no_instrument_function_attribute },
783 { "malloc", 0, 0, true, false, false,
784 handle_malloc_attribute },
785 { "returns_twice", 0, 0, true, false, false,
786 handle_returns_twice_attribute },
787 { "no_stack_limit", 0, 0, true, false, false,
788 handle_no_limit_stack_attribute },
789 { "pure", 0, 0, true, false, false,
790 handle_pure_attribute },
791 /* For internal use (marking of builtins) only. The name contains space
792 to prevent its usage in source code. */
793 { "no vops", 0, 0, true, false, false,
794 handle_novops_attribute },
795 { "deprecated", 0, 1, false, false, false,
796 handle_deprecated_attribute },
797 { "vector_size", 1, 1, false, true, false,
798 handle_vector_size_attribute },
799 { "visibility", 1, 1, false, false, false,
800 handle_visibility_attribute },
801 { "tls_model", 1, 1, true, false, false,
802 handle_tls_model_attribute },
803 { "nonnull", 0, -1, false, true, true,
804 handle_nonnull_attribute },
805 { "nothrow", 0, 0, true, false, false,
806 handle_nothrow_attribute },
807 { "may_alias", 0, 0, false, true, false, NULL },
808 { "cleanup", 1, 1, true, false, false,
809 handle_cleanup_attribute },
810 { "warn_unused_result", 0, 0, false, true, true,
811 handle_warn_unused_result_attribute },
812 { "sentinel", 0, 1, false, true, true,
813 handle_sentinel_attribute },
814 /* For internal use (marking of builtins) only. The name contains space
815 to prevent its usage in source code. */
816 { "type generic", 0, 0, false, true, true,
817 handle_type_generic_attribute },
818 { "alloc_size", 1, 2, false, true, true,
819 handle_alloc_size_attribute },
820 { "cold", 0, 0, true, false, false,
821 handle_cold_attribute },
822 { "hot", 0, 0, true, false, false,
823 handle_hot_attribute },
824 { "warning", 1, 1, true, false, false,
825 handle_error_attribute },
826 { "error", 1, 1, true, false, false,
827 handle_error_attribute },
828 { "target", 1, -1, true, false, false,
829 handle_target_attribute },
830 { "optimize", 1, -1, true, false, false,
831 handle_optimize_attribute },
832 /* For internal use (marking of builtins and runtime functions) only.
833 The name contains space to prevent its usage in source code. */
834 { "fn spec", 1, 1, false, true, true,
835 handle_fnspec_attribute },
836 { NULL, 0, 0, false, false, false, NULL }
839 /* Give the specifications for the format attributes, used by C and all
842 const struct attribute_spec c_common_format_attribute_table[] =
844 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
845 { "format", 3, 3, false, true, true,
846 handle_format_attribute },
847 { "format_arg", 1, 1, false, true, true,
848 handle_format_arg_attribute },
849 { NULL, 0, 0, false, false, false, NULL }
852 /* Return identifier for address space AS. */
855 c_addr_space_name (addr_space_t as)
857 int rid = RID_FIRST_ADDR_SPACE + as;
858 gcc_assert (ridpointers [rid]);
859 return IDENTIFIER_POINTER (ridpointers [rid]);
862 /* Push current bindings for the function name VAR_DECLS. */
865 start_fname_decls (void)
868 tree saved = NULL_TREE;
870 for (ix = 0; fname_vars[ix].decl; ix++)
872 tree decl = *fname_vars[ix].decl;
876 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
877 *fname_vars[ix].decl = NULL_TREE;
880 if (saved || saved_function_name_decls)
881 /* Normally they'll have been NULL, so only push if we've got a
882 stack, or they are non-NULL. */
883 saved_function_name_decls = tree_cons (saved, NULL_TREE,
884 saved_function_name_decls);
887 /* Finish up the current bindings, adding them into the current function's
888 statement tree. This must be done _before_ finish_stmt_tree is called.
889 If there is no current function, we must be at file scope and no statements
890 are involved. Pop the previous bindings. */
893 finish_fname_decls (void)
896 tree stmts = NULL_TREE;
897 tree stack = saved_function_name_decls;
899 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
900 append_to_statement_list (TREE_VALUE (stack), &stmts);
904 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
906 if (TREE_CODE (*bodyp) == BIND_EXPR)
907 bodyp = &BIND_EXPR_BODY (*bodyp);
909 append_to_statement_list_force (*bodyp, &stmts);
913 for (ix = 0; fname_vars[ix].decl; ix++)
914 *fname_vars[ix].decl = NULL_TREE;
918 /* We had saved values, restore them. */
921 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
923 tree decl = TREE_PURPOSE (saved);
924 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
926 *fname_vars[ix].decl = decl;
928 stack = TREE_CHAIN (stack);
930 saved_function_name_decls = stack;
933 /* Return the text name of the current function, suitably prettified
934 by PRETTY_P. Return string must be freed by caller. */
937 fname_as_string (int pretty_p)
939 const char *name = "top level";
942 cpp_string cstr = { 0, 0 }, strname;
950 if (current_function_decl)
951 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
953 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
955 namep = XNEWVEC (char, len);
956 snprintf (namep, len, "\"%s\"", name);
957 strname.text = (unsigned char *) namep;
958 strname.len = len - 1;
960 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
963 return (const char *) cstr.text;
969 /* Return the VAR_DECL for a const char array naming the current
970 function. If the VAR_DECL has not yet been created, create it
971 now. RID indicates how it should be formatted and IDENTIFIER_NODE
972 ID is its name (unfortunately C and C++ hold the RID values of
973 keywords in different places, so we can't derive RID from ID in
974 this language independent code. LOC is the location of the
978 fname_decl (location_t loc, unsigned int rid, tree id)
981 tree decl = NULL_TREE;
983 for (ix = 0; fname_vars[ix].decl; ix++)
984 if (fname_vars[ix].rid == rid)
987 decl = *fname_vars[ix].decl;
990 /* If a tree is built here, it would normally have the lineno of
991 the current statement. Later this tree will be moved to the
992 beginning of the function and this line number will be wrong.
993 To avoid this problem set the lineno to 0 here; that prevents
994 it from appearing in the RTL. */
996 location_t saved_location = input_location;
997 input_location = UNKNOWN_LOCATION;
999 stmts = push_stmt_list ();
1000 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1001 stmts = pop_stmt_list (stmts);
1002 if (!IS_EMPTY_STMT (stmts))
1003 saved_function_name_decls
1004 = tree_cons (decl, stmts, saved_function_name_decls);
1005 *fname_vars[ix].decl = decl;
1006 input_location = saved_location;
1008 if (!ix && !current_function_decl)
1009 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1014 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1017 fix_string_type (tree value)
1019 int length = TREE_STRING_LENGTH (value);
1021 tree e_type, i_type, a_type;
1023 /* Compute the number of elements, for the array type. */
1024 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1027 e_type = char_type_node;
1029 else if (TREE_TYPE (value) == char16_array_type_node)
1031 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1032 e_type = char16_type_node;
1034 else if (TREE_TYPE (value) == char32_array_type_node)
1036 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1037 e_type = char32_type_node;
1041 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1042 e_type = wchar_type_node;
1045 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1046 limit in C++98 Annex B is very large (65536) and is not normative,
1047 so we do not diagnose it (warn_overlength_strings is forced off
1048 in c_common_post_options). */
1049 if (warn_overlength_strings)
1051 const int nchars_max = flag_isoc99 ? 4095 : 509;
1052 const int relevant_std = flag_isoc99 ? 99 : 90;
1053 if (nchars - 1 > nchars_max)
1054 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1055 separate the %d from the 'C'. 'ISO' should not be
1056 translated, but it may be moved after 'C%d' in languages
1057 where modifiers follow nouns. */
1058 pedwarn (input_location, OPT_Woverlength_strings,
1059 "string length %qd is greater than the length %qd "
1060 "ISO C%d compilers are required to support",
1061 nchars - 1, nchars_max, relevant_std);
1064 /* Create the array type for the string constant. The ISO C++
1065 standard says that a string literal has type `const char[N]' or
1066 `const wchar_t[N]'. We use the same logic when invoked as a C
1067 front-end with -Wwrite-strings.
1068 ??? We should change the type of an expression depending on the
1069 state of a warning flag. We should just be warning -- see how
1070 this is handled in the C++ front-end for the deprecated implicit
1071 conversion from string literals to `char*' or `wchar_t*'.
1073 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1074 array type being the unqualified version of that type.
1075 Therefore, if we are constructing an array of const char, we must
1076 construct the matching unqualified array type first. The C front
1077 end does not require this, but it does no harm, so we do it
1079 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
1080 a_type = build_array_type (e_type, i_type);
1081 if (c_dialect_cxx() || warn_write_strings)
1082 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1084 TREE_TYPE (value) = a_type;
1085 TREE_CONSTANT (value) = 1;
1086 TREE_READONLY (value) = 1;
1087 TREE_STATIC (value) = 1;
1091 /* Fully fold EXPR, an expression that was not folded (beyond integer
1092 constant expressions and null pointer constants) when being built
1093 up. If IN_INIT, this is in a static initializer and certain
1094 changes are made to the folding done. Clear *MAYBE_CONST if
1095 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1096 expression because it contains an evaluated operator (in C99) or an
1097 operator outside of sizeof returning an integer constant (in C90)
1098 not permitted in constant expressions, or because it contains an
1099 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1100 set to true by callers before calling this function.) Return the
1101 folded expression. Function arguments have already been folded
1102 before calling this function, as have the contents of SAVE_EXPR,
1103 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1104 C_MAYBE_CONST_EXPR. */
1107 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1110 tree eptype = NULL_TREE;
1112 bool maybe_const_itself = true;
1113 location_t loc = EXPR_LOCATION (expr);
1115 /* This function is not relevant to C++ because C++ folds while
1116 parsing, and may need changes to be correct for C++ when C++
1117 stops folding while parsing. */
1118 if (c_dialect_cxx ())
1122 maybe_const = &dummy;
1123 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1125 eptype = TREE_TYPE (expr);
1126 expr = TREE_OPERAND (expr, 0);
1128 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1129 &maybe_const_itself);
1131 ret = fold_convert_loc (loc, eptype, ret);
1132 *maybe_const &= maybe_const_itself;
1136 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1137 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1138 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1139 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1140 both evaluated and unevaluated subexpressions while
1141 *MAYBE_CONST_ITSELF is carried from only evaluated
1145 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1146 bool *maybe_const_itself)
1149 enum tree_code code = TREE_CODE (expr);
1150 enum tree_code_class kind = TREE_CODE_CLASS (code);
1151 location_t loc = EXPR_LOCATION (expr);
1152 tree op0, op1, op2, op3;
1153 tree orig_op0, orig_op1, orig_op2;
1154 bool op0_const = true, op1_const = true, op2_const = true;
1155 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1156 bool nowarning = TREE_NO_WARNING (expr);
1159 /* This function is not relevant to C++ because C++ folds while
1160 parsing, and may need changes to be correct for C++ when C++
1161 stops folding while parsing. */
1162 if (c_dialect_cxx ())
1165 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1166 anything else not counted as an expression cannot usefully be
1167 folded further at this point. */
1168 if (!IS_EXPR_CODE_CLASS (kind)
1169 || kind == tcc_statement
1170 || code == SAVE_EXPR)
1173 /* Operands of variable-length expressions (function calls) have
1174 already been folded, as have __builtin_* function calls, and such
1175 expressions cannot occur in constant expressions. */
1176 if (kind == tcc_vl_exp)
1178 *maybe_const_operands = false;
1183 if (code == C_MAYBE_CONST_EXPR)
1185 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1186 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1187 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1188 *maybe_const_operands = false;
1189 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1190 *maybe_const_itself = false;
1191 if (pre && !in_init)
1192 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1198 /* Assignment, increment, decrement, function call and comma
1199 operators, and statement expressions, cannot occur in constant
1200 expressions if evaluated / outside of sizeof. (Function calls
1201 were handled above, though VA_ARG_EXPR is treated like a function
1202 call here, and statement expressions are handled through
1203 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1207 case PREDECREMENT_EXPR:
1208 case PREINCREMENT_EXPR:
1209 case POSTDECREMENT_EXPR:
1210 case POSTINCREMENT_EXPR:
1212 *maybe_const_operands = false;
1219 *maybe_const_operands = false;
1227 /* Fold individual tree codes as appropriate. */
1230 case COMPOUND_LITERAL_EXPR:
1231 /* Any non-constancy will have been marked in a containing
1232 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1236 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1237 op1 = TREE_OPERAND (expr, 1);
1238 op2 = TREE_OPERAND (expr, 2);
1239 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1240 maybe_const_itself);
1241 STRIP_TYPE_NOPS (op0);
1242 if (op0 != orig_op0)
1243 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1246 TREE_READONLY (ret) = TREE_READONLY (expr);
1247 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1252 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1253 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1254 op2 = TREE_OPERAND (expr, 2);
1255 op3 = TREE_OPERAND (expr, 3);
1256 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1257 maybe_const_itself);
1258 STRIP_TYPE_NOPS (op0);
1259 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1260 maybe_const_itself);
1261 STRIP_TYPE_NOPS (op1);
1262 op1 = decl_constant_value_for_optimization (op1);
1263 if (op0 != orig_op0 || op1 != orig_op1)
1264 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1267 TREE_READONLY (ret) = TREE_READONLY (expr);
1268 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1269 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1276 case PREDECREMENT_EXPR:
1277 case PREINCREMENT_EXPR:
1278 case POSTDECREMENT_EXPR:
1279 case POSTINCREMENT_EXPR:
1283 case POINTER_PLUS_EXPR:
1284 case TRUNC_DIV_EXPR:
1286 case FLOOR_DIV_EXPR:
1287 case TRUNC_MOD_EXPR:
1289 case EXACT_DIV_EXPR:
1302 case TRUTH_AND_EXPR:
1304 case TRUTH_XOR_EXPR:
1305 case UNORDERED_EXPR:
1312 /* Binary operations evaluating both arguments (increment and
1313 decrement are binary internally in GCC). */
1314 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1315 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1316 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1317 maybe_const_itself);
1318 STRIP_TYPE_NOPS (op0);
1319 if (code != MODIFY_EXPR
1320 && code != PREDECREMENT_EXPR
1321 && code != PREINCREMENT_EXPR
1322 && code != POSTDECREMENT_EXPR
1323 && code != POSTINCREMENT_EXPR)
1324 op0 = decl_constant_value_for_optimization (op0);
1325 /* The RHS of a MODIFY_EXPR was fully folded when building that
1326 expression for the sake of conversion warnings. */
1327 if (code != MODIFY_EXPR)
1328 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1329 maybe_const_itself);
1330 STRIP_TYPE_NOPS (op1);
1331 op1 = decl_constant_value_for_optimization (op1);
1332 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1334 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1335 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1338 if (TREE_OVERFLOW_P (ret)
1339 && !TREE_OVERFLOW_P (op0)
1340 && !TREE_OVERFLOW_P (op1))
1341 overflow_warning (EXPR_LOCATION (expr), ret);
1345 case FIX_TRUNC_EXPR:
1348 case NON_LVALUE_EXPR:
1351 case TRUTH_NOT_EXPR:
1356 /* Unary operations. */
1357 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1358 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1359 maybe_const_itself);
1360 STRIP_TYPE_NOPS (op0);
1361 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1362 op0 = decl_constant_value_for_optimization (op0);
1363 if (op0 != orig_op0 || in_init)
1365 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1366 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1369 if (code == INDIRECT_REF
1371 && TREE_CODE (ret) == INDIRECT_REF)
1373 TREE_READONLY (ret) = TREE_READONLY (expr);
1374 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1375 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1379 case FIX_TRUNC_EXPR:
1382 /* Don't warn about explicit conversions. We will already
1383 have warned about suspect implicit conversions. */
1387 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1388 overflow_warning (EXPR_LOCATION (expr), ret);
1393 case TRUTH_ANDIF_EXPR:
1394 case TRUTH_ORIF_EXPR:
1395 /* Binary operations not necessarily evaluating both
1397 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1398 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1399 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1400 STRIP_TYPE_NOPS (op0);
1402 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1403 ? truthvalue_false_node
1404 : truthvalue_true_node));
1405 c_inhibit_evaluation_warnings += unused_p;
1406 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1407 STRIP_TYPE_NOPS (op1);
1408 c_inhibit_evaluation_warnings -= unused_p;
1410 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1412 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1413 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1416 *maybe_const_operands &= op0_const;
1417 *maybe_const_itself &= op0_const_self;
1421 && (code == TRUTH_ANDIF_EXPR
1422 ? op0 == truthvalue_false_node
1423 : op0 == truthvalue_true_node)))
1424 *maybe_const_operands &= op1_const;
1427 && (code == TRUTH_ANDIF_EXPR
1428 ? op0 == truthvalue_false_node
1429 : op0 == truthvalue_true_node)))
1430 *maybe_const_itself &= op1_const_self;
1434 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1435 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1436 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1437 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1439 STRIP_TYPE_NOPS (op0);
1440 c_inhibit_evaluation_warnings += (op0 == truthvalue_false_node);
1441 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1442 STRIP_TYPE_NOPS (op1);
1443 c_inhibit_evaluation_warnings -= (op0 == truthvalue_false_node);
1445 c_inhibit_evaluation_warnings += (op0 == truthvalue_true_node);
1446 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1447 STRIP_TYPE_NOPS (op2);
1448 c_inhibit_evaluation_warnings -= (op0 == truthvalue_true_node);
1450 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1451 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1454 *maybe_const_operands &= op0_const;
1455 *maybe_const_itself &= op0_const_self;
1459 && op0 == truthvalue_false_node))
1460 *maybe_const_operands &= op1_const;
1463 && op0 == truthvalue_false_node))
1464 *maybe_const_itself &= op1_const_self;
1468 && op0 == truthvalue_true_node))
1469 *maybe_const_operands &= op2_const;
1472 && op0 == truthvalue_true_node))
1473 *maybe_const_itself &= op2_const_self;
1476 case EXCESS_PRECISION_EXPR:
1477 /* Each case where an operand with excess precision may be
1478 encountered must remove the EXCESS_PRECISION_EXPR around
1479 inner operands and possibly put one around the whole
1480 expression or possibly convert to the semantic type (which
1481 c_fully_fold does); we cannot tell at this stage which is
1482 appropriate in any particular case. */
1486 /* Various codes may appear through folding built-in functions
1487 and their arguments. */
1492 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1493 have been done by this point, so remove them again. */
1494 nowarning |= TREE_NO_WARNING (ret);
1495 STRIP_TYPE_NOPS (ret);
1496 if (nowarning && !TREE_NO_WARNING (ret))
1498 if (!CAN_HAVE_LOCATION_P (ret))
1499 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1500 TREE_NO_WARNING (ret) = 1;
1503 protected_set_expr_location (ret, loc);
1507 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1508 return EXP. Otherwise, return either EXP or its known constant
1509 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1510 Is the BLKmode test appropriate? */
1513 decl_constant_value_for_optimization (tree exp)
1517 /* This function is only used by C, for c_fully_fold and other
1518 optimization, and may not be correct for C++. */
1519 if (c_dialect_cxx ())
1523 || TREE_CODE (exp) != VAR_DECL
1524 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1525 || DECL_MODE (exp) == BLKmode)
1528 ret = decl_constant_value (exp);
1529 /* Avoid unwanted tree sharing between the initializer and current
1530 function's body where the tree can be modified e.g. by the
1532 if (ret != exp && TREE_STATIC (exp))
1533 ret = unshare_expr (ret);
1537 /* Print a warning if a constant expression had overflow in folding.
1538 Invoke this function on every expression that the language
1539 requires to be a constant expression.
1540 Note the ANSI C standard says it is erroneous for a
1541 constant expression to overflow. */
1544 constant_expression_warning (tree value)
1546 if (warn_overflow && pedantic
1547 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1548 || TREE_CODE (value) == FIXED_CST
1549 || TREE_CODE (value) == VECTOR_CST
1550 || TREE_CODE (value) == COMPLEX_CST)
1551 && TREE_OVERFLOW (value))
1552 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1555 /* The same as above but print an unconditional error. */
1557 constant_expression_error (tree value)
1559 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1560 || TREE_CODE (value) == FIXED_CST
1561 || TREE_CODE (value) == VECTOR_CST
1562 || TREE_CODE (value) == COMPLEX_CST)
1563 && TREE_OVERFLOW (value))
1564 error ("overflow in constant expression");
1567 /* Print a warning if an expression had overflow in folding and its
1570 Invoke this function on every expression that
1571 (1) appears in the source code, and
1572 (2) is a constant expression that overflowed, and
1573 (3) is not already checked by convert_and_check;
1574 however, do not invoke this function on operands of explicit casts
1575 or when the expression is the result of an operator and any operand
1576 already overflowed. */
1579 overflow_warning (location_t loc, tree value)
1581 if (c_inhibit_evaluation_warnings != 0)
1584 switch (TREE_CODE (value))
1587 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1591 warning_at (loc, OPT_Woverflow,
1592 "floating point overflow in expression");
1596 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1600 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1604 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1605 warning_at (loc, OPT_Woverflow,
1606 "complex integer overflow in expression");
1607 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1608 warning_at (loc, OPT_Woverflow,
1609 "complex floating point overflow in expression");
1617 /* Warn about uses of logical || / && operator in a context where it
1618 is likely that the bitwise equivalent was intended by the
1619 programmer. We have seen an expression in which CODE is a binary
1620 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1621 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1623 warn_logical_operator (location_t location, enum tree_code code, tree type,
1624 enum tree_code code_left, tree op_left,
1625 enum tree_code ARG_UNUSED (code_right), tree op_right)
1627 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1628 int in0_p, in1_p, in_p;
1629 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1630 bool strict_overflow_p = false;
1632 if (code != TRUTH_ANDIF_EXPR
1633 && code != TRUTH_AND_EXPR
1634 && code != TRUTH_ORIF_EXPR
1635 && code != TRUTH_OR_EXPR)
1638 /* Warn if &&/|| are being used in a context where it is
1639 likely that the bitwise equivalent was intended by the
1640 programmer. That is, an expression such as op && MASK
1641 where op should not be any boolean expression, nor a
1642 constant, and mask seems to be a non-boolean integer constant. */
1643 if (!truth_value_p (code_left)
1644 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1645 && !CONSTANT_CLASS_P (op_left)
1646 && !TREE_NO_WARNING (op_left)
1647 && TREE_CODE (op_right) == INTEGER_CST
1648 && !integer_zerop (op_right)
1649 && !integer_onep (op_right))
1652 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1653 " applied to non-boolean constant");
1655 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1656 " applied to non-boolean constant");
1657 TREE_NO_WARNING (op_left) = true;
1661 /* We do not warn for constants because they are typical of macro
1662 expansions that test for features. */
1663 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1666 /* This warning only makes sense with logical operands. */
1667 if (!(truth_value_p (TREE_CODE (op_left))
1668 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1669 || !(truth_value_p (TREE_CODE (op_right))
1670 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1673 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1674 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1676 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1677 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1679 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1680 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1682 /* If this is an OR operation, invert both sides; we will invert
1683 again at the end. */
1685 in0_p = !in0_p, in1_p = !in1_p;
1687 /* If both expressions are the same, if we can merge the ranges, and we
1688 can build the range test, return it or it inverted. */
1689 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1690 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1692 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1693 type, lhs, in_p, low, high)))
1695 if (TREE_CODE (tem) != INTEGER_CST)
1699 warning_at (location, OPT_Wlogical_op,
1701 "of collectively exhaustive tests is always true");
1703 warning_at (location, OPT_Wlogical_op,
1705 "of mutually exclusive tests is always false");
1710 /* Print a warning about casts that might indicate violation
1711 of strict aliasing rules if -Wstrict-aliasing is used and
1712 strict aliasing mode is in effect. OTYPE is the original
1713 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1716 strict_aliasing_warning (tree otype, tree type, tree expr)
1718 /* Strip pointer conversion chains and get to the correct original type. */
1720 otype = TREE_TYPE (expr);
1722 if (!(flag_strict_aliasing
1723 && POINTER_TYPE_P (type)
1724 && POINTER_TYPE_P (otype)
1725 && !VOID_TYPE_P (TREE_TYPE (type)))
1726 /* If the type we are casting to is a ref-all pointer
1727 dereferencing it is always valid. */
1728 || TYPE_REF_CAN_ALIAS_ALL (type))
1731 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1732 && (DECL_P (TREE_OPERAND (expr, 0))
1733 || handled_component_p (TREE_OPERAND (expr, 0))))
1735 /* Casting the address of an object to non void pointer. Warn
1736 if the cast breaks type based aliasing. */
1737 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1739 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1740 "might break strict-aliasing rules");
1745 /* warn_strict_aliasing >= 3. This includes the default (3).
1746 Only warn if the cast is dereferenced immediately. */
1747 alias_set_type set1 =
1748 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1749 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1751 if (set1 != set2 && set2 != 0
1752 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1754 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1755 "pointer will break strict-aliasing rules");
1758 else if (warn_strict_aliasing == 2
1759 && !alias_sets_must_conflict_p (set1, set2))
1761 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1762 "pointer might break strict-aliasing rules");
1768 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1770 /* At this level, warn for any conversions, even if an address is
1771 not taken in the same statement. This will likely produce many
1772 false positives, but could be useful to pinpoint problems that
1773 are not revealed at higher levels. */
1774 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1775 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1776 if (!COMPLETE_TYPE_P (type)
1777 || !alias_sets_must_conflict_p (set1, set2))
1779 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1780 "pointer might break strict-aliasing rules");
1788 /* Warn for unlikely, improbable, or stupid DECL declarations
1792 check_main_parameter_types (tree decl)
1797 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args;
1798 args = TREE_CHAIN (args))
1800 tree type = args ? TREE_VALUE (args) : 0;
1802 if (type == void_type_node || type == error_mark_node )
1809 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1810 pedwarn (input_location, OPT_Wmain, "first argument of %q+D should be %<int%>",
1815 if (TREE_CODE (type) != POINTER_TYPE
1816 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1817 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1819 pedwarn (input_location, OPT_Wmain, "second argument of %q+D should be %<char **%>",
1824 if (TREE_CODE (type) != POINTER_TYPE
1825 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1826 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1828 pedwarn (input_location, OPT_Wmain, "third argument of %q+D should probably be "
1829 "%<char **%>", decl);
1834 /* It is intentional that this message does not mention the third
1835 argument because it's only mentioned in an appendix of the
1837 if (argct > 0 && (argct < 2 || argct > 3))
1838 pedwarn (input_location, OPT_Wmain, "%q+D takes only zero or two arguments", decl);
1841 /* True if pointers to distinct types T1 and T2 can be converted to
1842 each other without an explicit cast. Only returns true for opaque
1845 vector_targets_convertible_p (const_tree t1, const_tree t2)
1847 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1848 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1849 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1855 /* True if vector types T1 and T2 can be converted to each other
1856 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1857 can only be converted with -flax-vector-conversions yet that is not
1858 in effect, emit a note telling the user about that option if such
1859 a note has not previously been emitted. */
1861 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1863 static bool emitted_lax_note = false;
1864 bool convertible_lax;
1866 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1867 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1871 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1872 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1873 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1874 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1875 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1877 if (!convertible_lax || flag_lax_vector_conversions)
1878 return convertible_lax;
1880 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1881 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1884 if (emit_lax_note && !emitted_lax_note)
1886 emitted_lax_note = true;
1887 inform (input_location, "use -flax-vector-conversions to permit "
1888 "conversions between vectors with differing "
1889 "element types or numbers of subparts");
1895 /* This is a helper function of build_binary_op.
1897 For certain operations if both args were extended from the same
1898 smaller type, do the arithmetic in that type and then extend.
1900 BITWISE indicates a bitwise operation.
1901 For them, this optimization is safe only if
1902 both args are zero-extended or both are sign-extended.
1903 Otherwise, we might change the result.
1904 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1905 but calculated in (unsigned short) it would be (unsigned short)-1.
1907 tree shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1909 int unsigned0, unsigned1;
1914 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1915 excessive narrowing when we call get_narrower below. For
1916 example, suppose that OP0 is of unsigned int extended
1917 from signed char and that RESULT_TYPE is long long int.
1918 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1921 (long long int) (unsigned int) signed_char
1923 which get_narrower would narrow down to
1925 (unsigned int) signed char
1927 If we do not cast OP0 first, get_narrower would return
1928 signed_char, which is inconsistent with the case of the
1930 op0 = convert (result_type, op0);
1931 op1 = convert (result_type, op1);
1933 arg0 = get_narrower (op0, &unsigned0);
1934 arg1 = get_narrower (op1, &unsigned1);
1936 /* UNS is 1 if the operation to be done is an unsigned one. */
1937 uns = TYPE_UNSIGNED (result_type);
1939 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1940 but it *requires* conversion to FINAL_TYPE. */
1942 if ((TYPE_PRECISION (TREE_TYPE (op0))
1943 == TYPE_PRECISION (TREE_TYPE (arg0)))
1944 && TREE_TYPE (op0) != result_type)
1945 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1946 if ((TYPE_PRECISION (TREE_TYPE (op1))
1947 == TYPE_PRECISION (TREE_TYPE (arg1)))
1948 && TREE_TYPE (op1) != result_type)
1949 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1951 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1953 /* For bitwise operations, signedness of nominal type
1954 does not matter. Consider only how operands were extended. */
1958 /* Note that in all three cases below we refrain from optimizing
1959 an unsigned operation on sign-extended args.
1960 That would not be valid. */
1962 /* Both args variable: if both extended in same way
1963 from same width, do it in that width.
1964 Do it unsigned if args were zero-extended. */
1965 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1966 < TYPE_PRECISION (result_type))
1967 && (TYPE_PRECISION (TREE_TYPE (arg1))
1968 == TYPE_PRECISION (TREE_TYPE (arg0)))
1969 && unsigned0 == unsigned1
1970 && (unsigned0 || !uns))
1971 return c_common_signed_or_unsigned_type
1972 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1974 else if (TREE_CODE (arg0) == INTEGER_CST
1975 && (unsigned1 || !uns)
1976 && (TYPE_PRECISION (TREE_TYPE (arg1))
1977 < TYPE_PRECISION (result_type))
1979 = c_common_signed_or_unsigned_type (unsigned1,
1981 && !POINTER_TYPE_P (type)
1982 && int_fits_type_p (arg0, type))
1985 else if (TREE_CODE (arg1) == INTEGER_CST
1986 && (unsigned0 || !uns)
1987 && (TYPE_PRECISION (TREE_TYPE (arg0))
1988 < TYPE_PRECISION (result_type))
1990 = c_common_signed_or_unsigned_type (unsigned0,
1992 && !POINTER_TYPE_P (type)
1993 && int_fits_type_p (arg1, type))
1999 /* Warns if the conversion of EXPR to TYPE may alter a value.
2000 This is a helper function for warnings_for_convert_and_check. */
2003 conversion_warning (tree type, tree expr)
2005 bool give_warning = false;
2008 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
2009 tree expr_type = TREE_TYPE (expr);
2011 if (!warn_conversion && !warn_sign_conversion)
2014 /* If any operand is artificial, then this expression was generated
2015 by the compiler and we do not warn. */
2016 for (i = 0; i < expr_num_operands; i++)
2018 tree op = TREE_OPERAND (expr, i);
2019 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
2023 switch (TREE_CODE (expr))
2031 case TRUTH_ANDIF_EXPR:
2032 case TRUTH_ORIF_EXPR:
2033 case TRUTH_AND_EXPR:
2035 case TRUTH_XOR_EXPR:
2036 case TRUTH_NOT_EXPR:
2037 /* Conversion from boolean to a signed:1 bit-field (which only
2038 can hold the values 0 and -1) doesn't lose information - but
2039 it does change the value. */
2040 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2041 warning (OPT_Wconversion,
2042 "conversion to %qT from boolean expression", type);
2048 /* Warn for real constant that is not an exact integer converted
2050 if (TREE_CODE (expr_type) == REAL_TYPE
2051 && TREE_CODE (type) == INTEGER_TYPE)
2053 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2054 give_warning = true;
2056 /* Warn for an integer constant that does not fit into integer type. */
2057 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2058 && TREE_CODE (type) == INTEGER_TYPE
2059 && !int_fits_type_p (expr, type))
2061 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2062 && tree_int_cst_sgn (expr) < 0)
2063 warning (OPT_Wsign_conversion,
2064 "negative integer implicitly converted to unsigned type");
2065 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2066 warning (OPT_Wsign_conversion, "conversion of unsigned constant "
2067 "value to negative integer");
2069 give_warning = true;
2071 else if (TREE_CODE (type) == REAL_TYPE)
2073 /* Warn for an integer constant that does not fit into real type. */
2074 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2076 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2077 if (!exact_real_truncate (TYPE_MODE (type), &a))
2078 give_warning = true;
2080 /* Warn for a real constant that does not fit into a smaller
2082 else if (TREE_CODE (expr_type) == REAL_TYPE
2083 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2085 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2086 if (!exact_real_truncate (TYPE_MODE (type), &a))
2087 give_warning = true;
2092 warning (OPT_Wconversion,
2093 "conversion to %qT alters %qT constant value",
2100 /* In case of COND_EXPR, if both operands are constants or
2101 COND_EXPR, then we do not care about the type of COND_EXPR,
2102 only about the conversion of each operand. */
2103 tree op1 = TREE_OPERAND (expr, 1);
2104 tree op2 = TREE_OPERAND (expr, 2);
2106 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2107 || TREE_CODE (op1) == COND_EXPR)
2108 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2109 || TREE_CODE (op2) == COND_EXPR))
2111 conversion_warning (type, op1);
2112 conversion_warning (type, op2);
2118 default: /* 'expr' is not a constant. */
2120 /* Warn for real types converted to integer types. */
2121 if (TREE_CODE (expr_type) == REAL_TYPE
2122 && TREE_CODE (type) == INTEGER_TYPE)
2123 give_warning = true;
2125 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2126 && TREE_CODE (type) == INTEGER_TYPE)
2128 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2129 expr = get_unwidened (expr, 0);
2130 expr_type = TREE_TYPE (expr);
2132 /* Don't warn for short y; short x = ((int)y & 0xff); */
2133 if (TREE_CODE (expr) == BIT_AND_EXPR
2134 || TREE_CODE (expr) == BIT_IOR_EXPR
2135 || TREE_CODE (expr) == BIT_XOR_EXPR)
2137 /* If both args were extended from a shortest type,
2138 use that type if that is safe. */
2139 expr_type = shorten_binary_op (expr_type,
2140 TREE_OPERAND (expr, 0),
2141 TREE_OPERAND (expr, 1),
2144 if (TREE_CODE (expr) == BIT_AND_EXPR)
2146 tree op0 = TREE_OPERAND (expr, 0);
2147 tree op1 = TREE_OPERAND (expr, 1);
2148 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2149 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2151 /* If one of the operands is a non-negative constant
2152 that fits in the target type, then the type of the
2153 other operand does not matter. */
2154 if ((TREE_CODE (op0) == INTEGER_CST
2155 && int_fits_type_p (op0, c_common_signed_type (type))
2156 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2157 || (TREE_CODE (op1) == INTEGER_CST
2158 && int_fits_type_p (op1, c_common_signed_type (type))
2159 && int_fits_type_p (op1,
2160 c_common_unsigned_type (type))))
2162 /* If constant is unsigned and fits in the target
2163 type, then the result will also fit. */
2164 else if ((TREE_CODE (op0) == INTEGER_CST
2166 && int_fits_type_p (op0, type))
2167 || (TREE_CODE (op1) == INTEGER_CST
2169 && int_fits_type_p (op1, type)))
2173 /* Warn for integer types converted to smaller integer types. */
2174 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2175 give_warning = true;
2177 /* When they are the same width but different signedness,
2178 then the value may change. */
2179 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2180 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2181 /* Even when converted to a bigger type, if the type is
2182 unsigned but expr is signed, then negative values
2184 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2185 warning (OPT_Wsign_conversion, "conversion to %qT from %qT "
2186 "may change the sign of the result",
2190 /* Warn for integer types converted to real types if and only if
2191 all the range of values of the integer type cannot be
2192 represented by the real type. */
2193 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2194 && TREE_CODE (type) == REAL_TYPE)
2196 tree type_low_bound, type_high_bound;
2197 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2199 /* Don't warn about char y = 0xff; float x = (int) y; */
2200 expr = get_unwidened (expr, 0);
2201 expr_type = TREE_TYPE (expr);
2203 type_low_bound = TYPE_MIN_VALUE (expr_type);
2204 type_high_bound = TYPE_MAX_VALUE (expr_type);
2205 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2206 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2208 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2209 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2210 give_warning = true;
2213 /* Warn for real types converted to smaller real types. */
2214 else if (TREE_CODE (expr_type) == REAL_TYPE
2215 && TREE_CODE (type) == REAL_TYPE
2216 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2217 give_warning = true;
2221 warning (OPT_Wconversion,
2222 "conversion to %qT from %qT may alter its value",
2227 /* Produce warnings after a conversion. RESULT is the result of
2228 converting EXPR to TYPE. This is a helper function for
2229 convert_and_check and cp_convert_and_check. */
2232 warnings_for_convert_and_check (tree type, tree expr, tree result)
2234 if (TREE_CODE (expr) == INTEGER_CST
2235 && (TREE_CODE (type) == INTEGER_TYPE
2236 || TREE_CODE (type) == ENUMERAL_TYPE)
2237 && !int_fits_type_p (expr, type))
2239 /* Do not diagnose overflow in a constant expression merely
2240 because a conversion overflowed. */
2241 if (TREE_OVERFLOW (result))
2242 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2244 if (TYPE_UNSIGNED (type))
2246 /* This detects cases like converting -129 or 256 to
2248 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2249 warning (OPT_Woverflow,
2250 "large integer implicitly truncated to unsigned type");
2252 conversion_warning (type, expr);
2254 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2255 warning (OPT_Woverflow,
2256 "overflow in implicit constant conversion");
2257 /* No warning for converting 0x80000000 to int. */
2259 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2260 || TYPE_PRECISION (TREE_TYPE (expr))
2261 != TYPE_PRECISION (type)))
2262 warning (OPT_Woverflow,
2263 "overflow in implicit constant conversion");
2266 conversion_warning (type, expr);
2268 else if ((TREE_CODE (result) == INTEGER_CST
2269 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2270 warning (OPT_Woverflow,
2271 "overflow in implicit constant conversion");
2273 conversion_warning (type, expr);
2277 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2278 Invoke this function on every expression that is converted implicitly,
2279 i.e. because of language rules and not because of an explicit cast. */
2282 convert_and_check (tree type, tree expr)
2285 tree expr_for_warning;
2287 /* Convert from a value with possible excess precision rather than
2288 via the semantic type, but do not warn about values not fitting
2289 exactly in the semantic type. */
2290 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2292 tree orig_type = TREE_TYPE (expr);
2293 expr = TREE_OPERAND (expr, 0);
2294 expr_for_warning = convert (orig_type, expr);
2295 if (orig_type == type)
2296 return expr_for_warning;
2299 expr_for_warning = expr;
2301 if (TREE_TYPE (expr) == type)
2304 result = convert (type, expr);
2306 if (c_inhibit_evaluation_warnings == 0
2307 && !TREE_OVERFLOW_P (expr)
2308 && result != error_mark_node)
2309 warnings_for_convert_and_check (type, expr_for_warning, result);
2314 /* A node in a list that describes references to variables (EXPR), which are
2315 either read accesses if WRITER is zero, or write accesses, in which case
2316 WRITER is the parent of EXPR. */
2323 /* Used to implement a cache the results of a call to verify_tree. We only
2324 use this for SAVE_EXPRs. */
2327 struct tlist_cache *next;
2328 struct tlist *cache_before_sp;
2329 struct tlist *cache_after_sp;
2333 /* Obstack to use when allocating tlist structures, and corresponding
2335 static struct obstack tlist_obstack;
2336 static char *tlist_firstobj = 0;
2338 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2340 static struct tlist *warned_ids;
2341 /* SAVE_EXPRs need special treatment. We process them only once and then
2342 cache the results. */
2343 static struct tlist_cache *save_expr_cache;
2345 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2346 static void merge_tlist (struct tlist **, struct tlist *, int);
2347 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2348 static int warning_candidate_p (tree);
2349 static bool candidate_equal_p (const_tree, const_tree);
2350 static void warn_for_collisions (struct tlist *);
2351 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2352 static struct tlist *new_tlist (struct tlist *, tree, tree);
2354 /* Create a new struct tlist and fill in its fields. */
2355 static struct tlist *
2356 new_tlist (struct tlist *next, tree t, tree writer)
2359 l = XOBNEW (&tlist_obstack, struct tlist);
2366 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2367 is nonnull, we ignore any node we find which has a writer equal to it. */
2370 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2374 struct tlist *next = add->next;
2377 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2378 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2383 /* Merge the nodes of ADD into TO. This merging process is done so that for
2384 each variable that already exists in TO, no new node is added; however if
2385 there is a write access recorded in ADD, and an occurrence on TO is only
2386 a read access, then the occurrence in TO will be modified to record the
2390 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2392 struct tlist **end = to;
2395 end = &(*end)->next;
2401 struct tlist *next = add->next;
2403 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2404 if (candidate_equal_p (tmp2->expr, add->expr))
2408 tmp2->writer = add->writer;
2412 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2413 end = &(*end)->next;
2420 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2421 references in list LIST conflict with it, excluding reads if ONLY writers
2425 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2430 /* Avoid duplicate warnings. */
2431 for (tmp = warned_ids; tmp; tmp = tmp->next)
2432 if (candidate_equal_p (tmp->expr, written))
2437 if (candidate_equal_p (list->expr, written)
2438 && !candidate_equal_p (list->writer, writer)
2439 && (!only_writes || list->writer))
2441 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2442 warning_at (EXPR_HAS_LOCATION (writer)
2443 ? EXPR_LOCATION (writer) : input_location,
2444 OPT_Wsequence_point, "operation on %qE may be undefined",
2451 /* Given a list LIST of references to variables, find whether any of these
2452 can cause conflicts due to missing sequence points. */
2455 warn_for_collisions (struct tlist *list)
2459 for (tmp = list; tmp; tmp = tmp->next)
2462 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2466 /* Return nonzero if X is a tree that can be verified by the sequence point
2469 warning_candidate_p (tree x)
2471 /* !VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2472 (lvalue_p) crash on TRY/CATCH. */
2473 return !(DECL_P (x) && DECL_ARTIFICIAL (x))
2474 && TREE_TYPE (x) && !VOID_TYPE_P (TREE_TYPE (x)) && lvalue_p (x);
2477 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2479 candidate_equal_p (const_tree x, const_tree y)
2481 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2484 /* Walk the tree X, and record accesses to variables. If X is written by the
2485 parent tree, WRITER is the parent.
2486 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2487 expression or its only operand forces a sequence point, then everything up
2488 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2490 Once we return, we will have emitted warnings if any subexpression before
2491 such a sequence point could be undefined. On a higher level, however, the
2492 sequence point may not be relevant, and we'll merge the two lists.
2494 Example: (b++, a) + b;
2495 The call that processes the COMPOUND_EXPR will store the increment of B
2496 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2497 processes the PLUS_EXPR will need to merge the two lists so that
2498 eventually, all accesses end up on the same list (and we'll warn about the
2499 unordered subexpressions b++ and b.
2501 A note on merging. If we modify the former example so that our expression
2504 care must be taken not simply to add all three expressions into the final
2505 PNO_SP list. The function merge_tlist takes care of that by merging the
2506 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2507 way, so that no more than one access to B is recorded. */
2510 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2513 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2514 enum tree_code code;
2515 enum tree_code_class cl;
2517 /* X may be NULL if it is the operand of an empty statement expression
2523 code = TREE_CODE (x);
2524 cl = TREE_CODE_CLASS (code);
2526 if (warning_candidate_p (x))
2527 *pno_sp = new_tlist (*pno_sp, x, writer);
2535 case TRUTH_ANDIF_EXPR:
2536 case TRUTH_ORIF_EXPR:
2537 tmp_before = tmp_nosp = tmp_list3 = 0;
2538 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2539 warn_for_collisions (tmp_nosp);
2540 merge_tlist (pbefore_sp, tmp_before, 0);
2541 merge_tlist (pbefore_sp, tmp_nosp, 0);
2542 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2543 merge_tlist (pbefore_sp, tmp_list3, 0);
2547 tmp_before = tmp_list2 = 0;
2548 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2549 warn_for_collisions (tmp_list2);
2550 merge_tlist (pbefore_sp, tmp_before, 0);
2551 merge_tlist (pbefore_sp, tmp_list2, 1);
2553 tmp_list3 = tmp_nosp = 0;
2554 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2555 warn_for_collisions (tmp_nosp);
2556 merge_tlist (pbefore_sp, tmp_list3, 0);
2558 tmp_list3 = tmp_list2 = 0;
2559 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2560 warn_for_collisions (tmp_list2);
2561 merge_tlist (pbefore_sp, tmp_list3, 0);
2562 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2563 two first, to avoid warning for (a ? b++ : b++). */
2564 merge_tlist (&tmp_nosp, tmp_list2, 0);
2565 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2568 case PREDECREMENT_EXPR:
2569 case PREINCREMENT_EXPR:
2570 case POSTDECREMENT_EXPR:
2571 case POSTINCREMENT_EXPR:
2572 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2576 tmp_before = tmp_nosp = tmp_list3 = 0;
2577 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2578 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2579 /* Expressions inside the LHS are not ordered wrt. the sequence points
2580 in the RHS. Example:
2582 Despite the fact that the modification of "a" is in the before_sp
2583 list (tmp_before), it conflicts with the use of "a" in the LHS.
2584 We can handle this by adding the contents of tmp_list3
2585 to those of tmp_before, and redoing the collision warnings for that
2587 add_tlist (&tmp_before, tmp_list3, x, 1);
2588 warn_for_collisions (tmp_before);
2589 /* Exclude the LHS itself here; we first have to merge it into the
2590 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2591 didn't exclude the LHS, we'd get it twice, once as a read and once
2593 add_tlist (pno_sp, tmp_list3, x, 0);
2594 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2596 merge_tlist (pbefore_sp, tmp_before, 0);
2597 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2598 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2599 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2603 /* We need to warn about conflicts among arguments and conflicts between
2604 args and the function address. Side effects of the function address,
2605 however, are not ordered by the sequence point of the call. */
2607 call_expr_arg_iterator iter;
2609 tmp_before = tmp_nosp = 0;
2610 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2611 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2613 tmp_list2 = tmp_list3 = 0;
2614 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2615 merge_tlist (&tmp_list3, tmp_list2, 0);
2616 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2618 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2619 warn_for_collisions (tmp_before);
2620 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2625 /* Scan all the list, e.g. indices of multi dimensional array. */
2628 tmp_before = tmp_nosp = 0;
2629 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2630 merge_tlist (&tmp_nosp, tmp_before, 0);
2631 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2638 struct tlist_cache *t;
2639 for (t = save_expr_cache; t; t = t->next)
2640 if (candidate_equal_p (t->expr, x))
2645 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2646 t->next = save_expr_cache;
2648 save_expr_cache = t;
2650 tmp_before = tmp_nosp = 0;
2651 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2652 warn_for_collisions (tmp_nosp);
2657 struct tlist *t = tmp_nosp;
2659 merge_tlist (&tmp_list3, t, 0);
2661 t->cache_before_sp = tmp_before;
2662 t->cache_after_sp = tmp_list3;
2664 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2665 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2670 x = TREE_OPERAND (x, 0);
2677 /* For other expressions, simply recurse on their operands.
2678 Manual tail recursion for unary expressions.
2679 Other non-expressions need not be processed. */
2680 if (cl == tcc_unary)
2682 x = TREE_OPERAND (x, 0);
2686 else if (IS_EXPR_CODE_CLASS (cl))
2689 int max = TREE_OPERAND_LENGTH (x);
2690 for (lp = 0; lp < max; lp++)
2692 tmp_before = tmp_nosp = 0;
2693 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2694 merge_tlist (&tmp_nosp, tmp_before, 0);
2695 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2702 /* Try to warn for undefined behavior in EXPR due to missing sequence
2706 verify_sequence_points (tree expr)
2708 struct tlist *before_sp = 0, *after_sp = 0;
2711 save_expr_cache = 0;
2712 if (tlist_firstobj == 0)
2714 gcc_obstack_init (&tlist_obstack);
2715 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2718 verify_tree (expr, &before_sp, &after_sp, 0);
2719 warn_for_collisions (after_sp);
2720 obstack_free (&tlist_obstack, tlist_firstobj);
2723 /* Validate the expression after `case' and apply default promotions. */
2726 check_case_value (tree value)
2728 if (value == NULL_TREE)
2731 /* ??? Can we ever get nops here for a valid case value? We
2733 STRIP_TYPE_NOPS (value);
2734 /* In C++, the following is allowed:
2737 switch (...) { case i: ... }
2739 So, we try to reduce the VALUE to a constant that way. */
2740 if (c_dialect_cxx ())
2742 value = decl_constant_value (value);
2743 STRIP_TYPE_NOPS (value);
2744 value = fold (value);
2747 if (TREE_CODE (value) == INTEGER_CST)
2748 /* Promote char or short to int. */
2749 value = perform_integral_promotions (value);
2750 else if (value != error_mark_node)
2752 error ("case label does not reduce to an integer constant");
2753 value = error_mark_node;
2756 constant_expression_warning (value);
2761 /* See if the case values LOW and HIGH are in the range of the original
2762 type (i.e. before the default conversion to int) of the switch testing
2764 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2765 the type before promoting it. CASE_LOW_P is a pointer to the lower
2766 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2767 if the case is not a case range.
2768 The caller has to make sure that we are not called with NULL for
2769 CASE_LOW_P (i.e. the default case).
2770 Returns true if the case label is in range of ORIG_TYPE (saturated or
2771 untouched) or false if the label is out of range. */
2774 check_case_bounds (tree type, tree orig_type,
2775 tree *case_low_p, tree *case_high_p)
2777 tree min_value, max_value;
2778 tree case_low = *case_low_p;
2779 tree case_high = case_high_p ? *case_high_p : case_low;
2781 /* If there was a problem with the original type, do nothing. */
2782 if (orig_type == error_mark_node)
2785 min_value = TYPE_MIN_VALUE (orig_type);
2786 max_value = TYPE_MAX_VALUE (orig_type);
2788 /* Case label is less than minimum for type. */
2789 if (tree_int_cst_compare (case_low, min_value) < 0
2790 && tree_int_cst_compare (case_high, min_value) < 0)
2792 warning (0, "case label value is less than minimum value for type");
2796 /* Case value is greater than maximum for type. */
2797 if (tree_int_cst_compare (case_low, max_value) > 0
2798 && tree_int_cst_compare (case_high, max_value) > 0)
2800 warning (0, "case label value exceeds maximum value for type");
2804 /* Saturate lower case label value to minimum. */
2805 if (tree_int_cst_compare (case_high, min_value) >= 0
2806 && tree_int_cst_compare (case_low, min_value) < 0)
2808 warning (0, "lower value in case label range"
2809 " less than minimum value for type");
2810 case_low = min_value;
2813 /* Saturate upper case label value to maximum. */
2814 if (tree_int_cst_compare (case_low, max_value) <= 0
2815 && tree_int_cst_compare (case_high, max_value) > 0)
2817 warning (0, "upper value in case label range"
2818 " exceeds maximum value for type");
2819 case_high = max_value;
2822 if (*case_low_p != case_low)
2823 *case_low_p = convert (type, case_low);
2824 if (case_high_p && *case_high_p != case_high)
2825 *case_high_p = convert (type, case_high);
2830 /* Return an integer type with BITS bits of precision,
2831 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2834 c_common_type_for_size (unsigned int bits, int unsignedp)
2836 if (bits == TYPE_PRECISION (integer_type_node))
2837 return unsignedp ? unsigned_type_node : integer_type_node;
2839 if (bits == TYPE_PRECISION (signed_char_type_node))
2840 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2842 if (bits == TYPE_PRECISION (short_integer_type_node))
2843 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2845 if (bits == TYPE_PRECISION (long_integer_type_node))
2846 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2848 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2849 return (unsignedp ? long_long_unsigned_type_node
2850 : long_long_integer_type_node);
2852 if (int128_integer_type_node
2853 && bits == TYPE_PRECISION (int128_integer_type_node))
2854 return (unsignedp ? int128_unsigned_type_node
2855 : int128_integer_type_node);
2857 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2858 return (unsignedp ? widest_unsigned_literal_type_node
2859 : widest_integer_literal_type_node);
2861 if (bits <= TYPE_PRECISION (intQI_type_node))
2862 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2864 if (bits <= TYPE_PRECISION (intHI_type_node))
2865 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2867 if (bits <= TYPE_PRECISION (intSI_type_node))
2868 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2870 if (bits <= TYPE_PRECISION (intDI_type_node))
2871 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2876 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2877 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2878 and saturating if SATP is nonzero, otherwise not saturating. */
2881 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2882 int unsignedp, int satp)
2884 enum machine_mode mode;
2886 mode = unsignedp ? UQQmode : QQmode;
2888 mode = unsignedp ? UHAmode : HAmode;
2890 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2891 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2894 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2896 sorry ("GCC cannot support operators with integer types and "
2897 "fixed-point types that have too many integral and "
2898 "fractional bits together");
2902 return c_common_type_for_mode (mode, satp);
2905 /* Used for communication between c_common_type_for_mode and
2906 c_register_builtin_type. */
2907 static GTY(()) tree registered_builtin_types;
2909 /* Return a data type that has machine mode MODE.
2910 If the mode is an integer,
2911 then UNSIGNEDP selects between signed and unsigned types.
2912 If the mode is a fixed-point mode,
2913 then UNSIGNEDP selects between saturating and nonsaturating types. */
2916 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2920 if (mode == TYPE_MODE (integer_type_node))
2921 return unsignedp ? unsigned_type_node : integer_type_node;
2923 if (mode == TYPE_MODE (signed_char_type_node))
2924 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2926 if (mode == TYPE_MODE (short_integer_type_node))
2927 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2929 if (mode == TYPE_MODE (long_integer_type_node))
2930 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2932 if (mode == TYPE_MODE (long_long_integer_type_node))
2933 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2935 if (int128_integer_type_node
2936 && mode == TYPE_MODE (int128_integer_type_node))
2937 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2939 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2940 return unsignedp ? widest_unsigned_literal_type_node
2941 : widest_integer_literal_type_node;
2944 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2947 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2950 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2953 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2955 #if HOST_BITS_PER_WIDE_INT >= 64
2956 if (mode == TYPE_MODE (intTI_type_node))
2957 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2960 if (mode == TYPE_MODE (float_type_node))
2961 return float_type_node;
2963 if (mode == TYPE_MODE (double_type_node))
2964 return double_type_node;
2966 if (mode == TYPE_MODE (long_double_type_node))
2967 return long_double_type_node;
2969 if (mode == TYPE_MODE (void_type_node))
2970 return void_type_node;
2972 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2974 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2975 : make_signed_type (GET_MODE_PRECISION (mode)));
2977 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2979 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2980 : make_signed_type (GET_MODE_PRECISION (mode)));
2982 if (COMPLEX_MODE_P (mode))
2984 enum machine_mode inner_mode;
2987 if (mode == TYPE_MODE (complex_float_type_node))
2988 return complex_float_type_node;
2989 if (mode == TYPE_MODE (complex_double_type_node))
2990 return complex_double_type_node;
2991 if (mode == TYPE_MODE (complex_long_double_type_node))
2992 return complex_long_double_type_node;
2994 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2995 return complex_integer_type_node;
2997 inner_mode = GET_MODE_INNER (mode);
2998 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2999 if (inner_type != NULL_TREE)
3000 return build_complex_type (inner_type);
3002 else if (VECTOR_MODE_P (mode))
3004 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3005 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3006 if (inner_type != NULL_TREE)
3007 return build_vector_type_for_mode (inner_type, mode);
3010 if (mode == TYPE_MODE (dfloat32_type_node))
3011 return dfloat32_type_node;
3012 if (mode == TYPE_MODE (dfloat64_type_node))
3013 return dfloat64_type_node;
3014 if (mode == TYPE_MODE (dfloat128_type_node))
3015 return dfloat128_type_node;
3017 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3019 if (mode == TYPE_MODE (short_fract_type_node))
3020 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3021 if (mode == TYPE_MODE (fract_type_node))
3022 return unsignedp ? sat_fract_type_node : fract_type_node;
3023 if (mode == TYPE_MODE (long_fract_type_node))
3024 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3025 if (mode == TYPE_MODE (long_long_fract_type_node))
3026 return unsignedp ? sat_long_long_fract_type_node
3027 : long_long_fract_type_node;
3029 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3030 return unsignedp ? sat_unsigned_short_fract_type_node
3031 : unsigned_short_fract_type_node;
3032 if (mode == TYPE_MODE (unsigned_fract_type_node))
3033 return unsignedp ? sat_unsigned_fract_type_node
3034 : unsigned_fract_type_node;
3035 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3036 return unsignedp ? sat_unsigned_long_fract_type_node
3037 : unsigned_long_fract_type_node;
3038 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3039 return unsignedp ? sat_unsigned_long_long_fract_type_node
3040 : unsigned_long_long_fract_type_node;
3042 if (mode == TYPE_MODE (short_accum_type_node))
3043 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3044 if (mode == TYPE_MODE (accum_type_node))
3045 return unsignedp ? sat_accum_type_node : accum_type_node;
3046 if (mode == TYPE_MODE (long_accum_type_node))
3047 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3048 if (mode == TYPE_MODE (long_long_accum_type_node))
3049 return unsignedp ? sat_long_long_accum_type_node
3050 : long_long_accum_type_node;
3052 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3053 return unsignedp ? sat_unsigned_short_accum_type_node
3054 : unsigned_short_accum_type_node;
3055 if (mode == TYPE_MODE (unsigned_accum_type_node))
3056 return unsignedp ? sat_unsigned_accum_type_node
3057 : unsigned_accum_type_node;
3058 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3059 return unsignedp ? sat_unsigned_long_accum_type_node
3060 : unsigned_long_accum_type_node;
3061 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3062 return unsignedp ? sat_unsigned_long_long_accum_type_node
3063 : unsigned_long_long_accum_type_node;
3066 return unsignedp ? sat_qq_type_node : qq_type_node;
3068 return unsignedp ? sat_hq_type_node : hq_type_node;
3070 return unsignedp ? sat_sq_type_node : sq_type_node;
3072 return unsignedp ? sat_dq_type_node : dq_type_node;
3074 return unsignedp ? sat_tq_type_node : tq_type_node;
3076 if (mode == UQQmode)
3077 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3078 if (mode == UHQmode)
3079 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3080 if (mode == USQmode)
3081 return unsignedp ? sat_usq_type_node : usq_type_node;
3082 if (mode == UDQmode)
3083 return unsignedp ? sat_udq_type_node : udq_type_node;
3084 if (mode == UTQmode)
3085 return unsignedp ? sat_utq_type_node : utq_type_node;
3088 return unsignedp ? sat_ha_type_node : ha_type_node;
3090 return unsignedp ? sat_sa_type_node : sa_type_node;
3092 return unsignedp ? sat_da_type_node : da_type_node;
3094 return unsignedp ? sat_ta_type_node : ta_type_node;
3096 if (mode == UHAmode)
3097 return unsignedp ? sat_uha_type_node : uha_type_node;
3098 if (mode == USAmode)
3099 return unsignedp ? sat_usa_type_node : usa_type_node;
3100 if (mode == UDAmode)
3101 return unsignedp ? sat_uda_type_node : uda_type_node;
3102 if (mode == UTAmode)
3103 return unsignedp ? sat_uta_type_node : uta_type_node;
3106 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3107 if (TYPE_MODE (TREE_VALUE (t)) == mode)
3108 return TREE_VALUE (t);
3114 c_common_unsigned_type (tree type)
3116 return c_common_signed_or_unsigned_type (1, type);
3119 /* Return a signed type the same as TYPE in other respects. */
3122 c_common_signed_type (tree type)
3124 return c_common_signed_or_unsigned_type (0, type);
3127 /* Return a type the same as TYPE except unsigned or
3128 signed according to UNSIGNEDP. */
3131 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3135 /* This block of code emulates the behavior of the old
3136 c_common_unsigned_type. In particular, it returns
3137 long_unsigned_type_node if passed a long, even when a int would
3138 have the same size. This is necessary for warnings to work
3139 correctly in archs where sizeof(int) == sizeof(long) */
3141 type1 = TYPE_MAIN_VARIANT (type);
3142 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3143 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3144 if (type1 == integer_type_node || type1 == unsigned_type_node)
3145 return unsignedp ? unsigned_type_node : integer_type_node;
3146 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3147 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3148 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3149 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3150 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3151 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3152 if (int128_integer_type_node
3153 && (type1 == int128_integer_type_node
3154 || type1 == int128_unsigned_type_node))
3155 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3156 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3157 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3158 #if HOST_BITS_PER_WIDE_INT >= 64
3159 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3160 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3162 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3163 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3164 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3165 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3166 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3167 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3168 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3169 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3171 #define C_COMMON_FIXED_TYPES(NAME) \
3172 if (type1 == short_ ## NAME ## _type_node \
3173 || type1 == unsigned_short_ ## NAME ## _type_node) \
3174 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3175 : short_ ## NAME ## _type_node; \
3176 if (type1 == NAME ## _type_node \
3177 || type1 == unsigned_ ## NAME ## _type_node) \
3178 return unsignedp ? unsigned_ ## NAME ## _type_node \
3179 : NAME ## _type_node; \
3180 if (type1 == long_ ## NAME ## _type_node \
3181 || type1 == unsigned_long_ ## NAME ## _type_node) \
3182 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3183 : long_ ## NAME ## _type_node; \
3184 if (type1 == long_long_ ## NAME ## _type_node \
3185 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3186 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3187 : long_long_ ## NAME ## _type_node;
3189 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3190 if (type1 == NAME ## _type_node \
3191 || type1 == u ## NAME ## _type_node) \
3192 return unsignedp ? u ## NAME ## _type_node \
3193 : NAME ## _type_node;
3195 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3196 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3197 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3198 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3199 : sat_ ## short_ ## NAME ## _type_node; \
3200 if (type1 == sat_ ## NAME ## _type_node \
3201 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3202 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3203 : sat_ ## NAME ## _type_node; \
3204 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3205 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3206 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3207 : sat_ ## long_ ## NAME ## _type_node; \
3208 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3209 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3210 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3211 : sat_ ## long_long_ ## NAME ## _type_node;
3213 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3214 if (type1 == sat_ ## NAME ## _type_node \
3215 || type1 == sat_ ## u ## NAME ## _type_node) \
3216 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3217 : sat_ ## NAME ## _type_node;
3219 C_COMMON_FIXED_TYPES (fract);
3220 C_COMMON_FIXED_TYPES_SAT (fract);
3221 C_COMMON_FIXED_TYPES (accum);
3222 C_COMMON_FIXED_TYPES_SAT (accum);
3224 C_COMMON_FIXED_MODE_TYPES (qq);
3225 C_COMMON_FIXED_MODE_TYPES (hq);
3226 C_COMMON_FIXED_MODE_TYPES (sq);
3227 C_COMMON_FIXED_MODE_TYPES (dq);
3228 C_COMMON_FIXED_MODE_TYPES (tq);
3229 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3230 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3231 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3232 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3233 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3234 C_COMMON_FIXED_MODE_TYPES (ha);
3235 C_COMMON_FIXED_MODE_TYPES (sa);
3236 C_COMMON_FIXED_MODE_TYPES (da);
3237 C_COMMON_FIXED_MODE_TYPES (ta);
3238 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3239 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3240 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3241 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3243 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3244 the precision; they have precision set to match their range, but
3245 may use a wider mode to match an ABI. If we change modes, we may
3246 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3247 the precision as well, so as to yield correct results for
3248 bit-field types. C++ does not have these separate bit-field
3249 types, and producing a signed or unsigned variant of an
3250 ENUMERAL_TYPE may cause other problems as well. */
3252 if (!INTEGRAL_TYPE_P (type)
3253 || TYPE_UNSIGNED (type) == unsignedp)
3256 #define TYPE_OK(node) \
3257 (TYPE_MODE (type) == TYPE_MODE (node) \
3258 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3259 if (TYPE_OK (signed_char_type_node))
3260 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3261 if (TYPE_OK (integer_type_node))
3262 return unsignedp ? unsigned_type_node : integer_type_node;
3263 if (TYPE_OK (short_integer_type_node))
3264 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3265 if (TYPE_OK (long_integer_type_node))
3266 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3267 if (TYPE_OK (long_long_integer_type_node))
3268 return (unsignedp ? long_long_unsigned_type_node
3269 : long_long_integer_type_node);
3270 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3271 return (unsignedp ? int128_unsigned_type_node
3272 : int128_integer_type_node);
3273 if (TYPE_OK (widest_integer_literal_type_node))
3274 return (unsignedp ? widest_unsigned_literal_type_node
3275 : widest_integer_literal_type_node);
3277 #if HOST_BITS_PER_WIDE_INT >= 64
3278 if (TYPE_OK (intTI_type_node))
3279 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3281 if (TYPE_OK (intDI_type_node))
3282 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3283 if (TYPE_OK (intSI_type_node))
3284 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3285 if (TYPE_OK (intHI_type_node))
3286 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3287 if (TYPE_OK (intQI_type_node))
3288 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3291 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3294 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3297 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3299 /* Extended integer types of the same width as a standard type have
3300 lesser rank, so those of the same width as int promote to int or
3301 unsigned int and are valid for printf formats expecting int or
3302 unsigned int. To avoid such special cases, avoid creating
3303 extended integer types for bit-fields if a standard integer type
3305 if (width == TYPE_PRECISION (integer_type_node))
3306 return unsignedp ? unsigned_type_node : integer_type_node;
3307 if (width == TYPE_PRECISION (signed_char_type_node))
3308 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3309 if (width == TYPE_PRECISION (short_integer_type_node))
3310 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3311 if (width == TYPE_PRECISION (long_integer_type_node))
3312 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3313 if (width == TYPE_PRECISION (long_long_integer_type_node))
3314 return (unsignedp ? long_long_unsigned_type_node
3315 : long_long_integer_type_node);
3316 if (int128_integer_type_node
3317 && width == TYPE_PRECISION (int128_integer_type_node))
3318 return (unsignedp ? int128_unsigned_type_node
3319 : int128_integer_type_node);
3320 return build_nonstandard_integer_type (width, unsignedp);
3323 /* The C version of the register_builtin_type langhook. */
3326 c_register_builtin_type (tree type, const char* name)
3330 decl = build_decl (UNKNOWN_LOCATION,
3331 TYPE_DECL, get_identifier (name), type);
3332 DECL_ARTIFICIAL (decl) = 1;
3333 if (!TYPE_NAME (type))
3334 TYPE_NAME (type) = decl;
3337 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3340 /* Print an error message for invalid operands to arith operation
3341 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3342 LOCATION is the location of the message. */
3345 binary_op_error (location_t location, enum tree_code code,
3346 tree type0, tree type1)
3353 opname = "+"; break;
3355 opname = "-"; break;
3357 opname = "*"; break;
3359 opname = "max"; break;
3361 opname = "min"; break;
3363 opname = "=="; break;
3365 opname = "!="; break;
3367 opname = "<="; break;
3369 opname = ">="; break;
3371 opname = "<"; break;
3373 opname = ">"; break;
3375 opname = "<<"; break;
3377 opname = ">>"; break;
3378 case TRUNC_MOD_EXPR:
3379 case FLOOR_MOD_EXPR:
3380 opname = "%"; break;
3381 case TRUNC_DIV_EXPR:
3382 case FLOOR_DIV_EXPR:
3383 opname = "/"; break;
3385 opname = "&"; break;
3387 opname = "|"; break;
3388 case TRUTH_ANDIF_EXPR:
3389 opname = "&&"; break;
3390 case TRUTH_ORIF_EXPR:
3391 opname = "||"; break;
3393 opname = "^"; break;
3398 "invalid operands to binary %s (have %qT and %qT)", opname,
3402 /* Subroutine of build_binary_op, used for comparison operations.
3403 See if the operands have both been converted from subword integer types
3404 and, if so, perhaps change them both back to their original type.
3405 This function is also responsible for converting the two operands
3406 to the proper common type for comparison.
3408 The arguments of this function are all pointers to local variables
3409 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3410 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3412 If this function returns nonzero, it means that the comparison has
3413 a constant value. What this function returns is an expression for
3417 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3418 enum tree_code *rescode_ptr)
3421 tree op0 = *op0_ptr;
3422 tree op1 = *op1_ptr;
3423 int unsignedp0, unsignedp1;
3425 tree primop0, primop1;
3426 enum tree_code code = *rescode_ptr;
3428 /* Throw away any conversions to wider types
3429 already present in the operands. */
3431 primop0 = get_narrower (op0, &unsignedp0);
3432 primop1 = get_narrower (op1, &unsignedp1);
3434 /* Handle the case that OP0 does not *contain* a conversion
3435 but it *requires* conversion to FINAL_TYPE. */
3437 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3438 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3439 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3440 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3442 /* If one of the operands must be floated, we cannot optimize. */
3443 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3444 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3446 /* If first arg is constant, swap the args (changing operation
3447 so value is preserved), for canonicalization. Don't do this if
3448 the second arg is 0. */
3450 if (TREE_CONSTANT (primop0)