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/>. */
24 #include "coretypes.h"
37 #include "langhooks.h"
38 #include "tree-inline.h"
40 #include "diagnostic.h"
41 #include "tree-iterator.h"
43 #include "tree-mudflap.h"
46 #include "target-def.h"
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
51 /* The following symbols are subsumed in the c_global_trees array, and
52 listed here individually for documentation purposes.
54 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
56 tree short_integer_type_node;
57 tree long_integer_type_node;
58 tree long_long_integer_type_node;
59 tree int128_integer_type_node;
61 tree short_unsigned_type_node;
62 tree long_unsigned_type_node;
63 tree long_long_unsigned_type_node;
64 tree int128_unsigned_type_node;
66 tree truthvalue_type_node;
67 tree truthvalue_false_node;
68 tree truthvalue_true_node;
70 tree ptrdiff_type_node;
72 tree unsigned_char_type_node;
73 tree signed_char_type_node;
76 tree char16_type_node;
77 tree char32_type_node;
80 tree double_type_node;
81 tree long_double_type_node;
83 tree complex_integer_type_node;
84 tree complex_float_type_node;
85 tree complex_double_type_node;
86 tree complex_long_double_type_node;
88 tree dfloat32_type_node;
89 tree dfloat64_type_node;
90 tree_dfloat128_type_node;
98 tree unsigned_intQI_type_node;
99 tree unsigned_intHI_type_node;
100 tree unsigned_intSI_type_node;
101 tree unsigned_intDI_type_node;
102 tree unsigned_intTI_type_node;
104 tree widest_integer_literal_type_node;
105 tree widest_unsigned_literal_type_node;
107 Nodes for types `void *' and `const void *'.
109 tree ptr_type_node, const_ptr_type_node;
111 Nodes for types `char *' and `const char *'.
113 tree string_type_node, const_string_type_node;
115 Type `char[SOMENUMBER]'.
116 Used when an array of char is needed and the size is irrelevant.
118 tree char_array_type_node;
120 Type `int[SOMENUMBER]' or something like it.
121 Used when an array of int needed and the size is irrelevant.
123 tree int_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
196 /* Warn about #pragma directives that are not recognized. */
198 int warn_unknown_pragmas; /* Tri state variable. */
200 /* Warn about format/argument anomalies in calls to formatted I/O functions
201 (*printf, *scanf, strftime, strfmon, etc.). */
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
217 /* Nonzero means use the ISO C99 (or C1X) dialect of C. */
221 /* Nonzero means use the ISO C1X dialect of C. */
225 /* Nonzero means that we have builtin functions, and main is an int. */
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice infinite template instantiations; the default
258 value of 1024 is likely to be in the next C++ standard. */
260 int max_tinst_depth = 1024;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
272 int c_inhibit_evaluation_warnings;
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
278 /* Information about how a function name is generated. */
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
286 /* The three ways of getting then name of the current function. */
288 const struct fname_var_t fname_vars[] =
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
299 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
300 static tree check_case_value (tree);
301 static bool check_case_bounds (tree, tree, tree *, tree *);
303 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
304 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
305 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_always_inline_attribute (tree *, tree, tree, int,
314 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
322 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_transparent_union_attribute (tree *, tree, tree,
325 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
331 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
332 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
335 static tree handle_visibility_attribute (tree *, tree, tree, int,
337 static tree handle_tls_model_attribute (tree *, tree, tree, int,
339 static tree handle_no_instrument_function_attribute (tree *, tree,
341 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
345 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_deprecated_attribute (tree *, tree, tree, int,
349 static tree handle_vector_size_attribute (tree *, tree, tree, int,
351 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
356 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
364 static void check_function_nonnull (tree, int, tree *);
365 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
366 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
367 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
368 static int resort_field_decl_cmp (const void *, const void *);
370 /* Reserved words. The third field is a mask: keywords are disabled
371 if they match the mask.
374 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
375 C --std=c99: D_CXXONLY | D_OBJC
376 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
377 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
378 C++ --std=c0x: D_CONLY | D_OBJC
379 ObjC++ is like C++ except that D_OBJC is not set
381 If -fno-asm is used, D_ASM is added to the mask. If
382 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
383 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
384 In C with -Wc++-compat, we warn if D_CXXWARN is set.
386 Note the complication of the D_CXX_OBJC keywords. These are
387 reserved words such as 'class'. In C++, 'class' is a reserved
388 word. In Objective-C++ it is too. In Objective-C, it is a
389 reserved word too, but only if it follows an '@' sign.
391 const struct c_common_resword c_common_reswords[] =
393 { "_Bool", RID_BOOL, D_CONLY },
394 { "_Complex", RID_COMPLEX, 0 },
395 { "_Imaginary", RID_IMAGINARY, D_CONLY },
396 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
397 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
398 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
399 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
400 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
401 { "_Sat", RID_SAT, D_CONLY | D_EXT },
402 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
403 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
404 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
405 { "__alignof", RID_ALIGNOF, 0 },
406 { "__alignof__", RID_ALIGNOF, 0 },
407 { "__asm", RID_ASM, 0 },
408 { "__asm__", RID_ASM, 0 },
409 { "__attribute", RID_ATTRIBUTE, 0 },
410 { "__attribute__", RID_ATTRIBUTE, 0 },
411 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
412 { "__builtin_offsetof", RID_OFFSETOF, 0 },
413 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
414 { "__builtin_va_arg", RID_VA_ARG, 0 },
415 { "__complex", RID_COMPLEX, 0 },
416 { "__complex__", RID_COMPLEX, 0 },
417 { "__const", RID_CONST, 0 },
418 { "__const__", RID_CONST, 0 },
419 { "__decltype", RID_DECLTYPE, D_CXXONLY },
420 { "__extension__", RID_EXTENSION, 0 },
421 { "__func__", RID_C99_FUNCTION_NAME, 0 },
422 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
423 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
424 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
425 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
426 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
427 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
428 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
429 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
430 { "__int128", RID_INT128, 0 },
431 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
432 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
433 { "__is_class", RID_IS_CLASS, D_CXXONLY },
434 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
435 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
436 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
437 { "__is_pod", RID_IS_POD, D_CXXONLY },
438 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
439 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
440 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
441 { "__is_union", RID_IS_UNION, D_CXXONLY },
442 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
443 { "__imag", RID_IMAGPART, 0 },
444 { "__imag__", RID_IMAGPART, 0 },
445 { "__inline", RID_INLINE, 0 },
446 { "__inline__", RID_INLINE, 0 },
447 { "__label__", RID_LABEL, 0 },
448 { "__null", RID_NULL, 0 },
449 { "__real", RID_REALPART, 0 },
450 { "__real__", RID_REALPART, 0 },
451 { "__restrict", RID_RESTRICT, 0 },
452 { "__restrict__", RID_RESTRICT, 0 },
453 { "__signed", RID_SIGNED, 0 },
454 { "__signed__", RID_SIGNED, 0 },
455 { "__thread", RID_THREAD, 0 },
456 { "__typeof", RID_TYPEOF, 0 },
457 { "__typeof__", RID_TYPEOF, 0 },
458 { "__volatile", RID_VOLATILE, 0 },
459 { "__volatile__", RID_VOLATILE, 0 },
460 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
461 { "asm", RID_ASM, D_ASM },
462 { "auto", RID_AUTO, 0 },
463 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
464 { "break", RID_BREAK, 0 },
465 { "case", RID_CASE, 0 },
466 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
467 { "char", RID_CHAR, 0 },
468 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
469 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
470 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
471 { "const", RID_CONST, 0 },
472 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
473 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
474 { "continue", RID_CONTINUE, 0 },
475 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
476 { "default", RID_DEFAULT, 0 },
477 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
479 { "double", RID_DOUBLE, 0 },
480 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
481 { "else", RID_ELSE, 0 },
482 { "enum", RID_ENUM, 0 },
483 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
484 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
485 { "extern", RID_EXTERN, 0 },
486 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
487 { "float", RID_FLOAT, 0 },
488 { "for", RID_FOR, 0 },
489 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
490 { "goto", RID_GOTO, 0 },
492 { "inline", RID_INLINE, D_EXT89 },
493 { "int", RID_INT, 0 },
494 { "long", RID_LONG, 0 },
495 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
496 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
497 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
498 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
500 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
501 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
502 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
503 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
504 { "register", RID_REGISTER, 0 },
505 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
506 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
507 { "return", RID_RETURN, 0 },
508 { "short", RID_SHORT, 0 },
509 { "signed", RID_SIGNED, 0 },
510 { "sizeof", RID_SIZEOF, 0 },
511 { "static", RID_STATIC, 0 },
512 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
513 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
514 { "struct", RID_STRUCT, 0 },
515 { "switch", RID_SWITCH, 0 },
516 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
517 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
518 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
519 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
520 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
521 { "typedef", RID_TYPEDEF, 0 },
522 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
523 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
524 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
525 { "union", RID_UNION, 0 },
526 { "unsigned", RID_UNSIGNED, 0 },
527 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
528 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
529 { "void", RID_VOID, 0 },
530 { "volatile", RID_VOLATILE, 0 },
531 { "wchar_t", RID_WCHAR, D_CXXONLY },
532 { "while", RID_WHILE, 0 },
533 /* These Objective-C keywords are recognized only immediately after
535 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
536 { "defs", RID_AT_DEFS, D_OBJC },
537 { "encode", RID_AT_ENCODE, D_OBJC },
538 { "end", RID_AT_END, D_OBJC },
539 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
540 { "interface", RID_AT_INTERFACE, D_OBJC },
541 { "protocol", RID_AT_PROTOCOL, D_OBJC },
542 { "selector", RID_AT_SELECTOR, D_OBJC },
543 { "finally", RID_AT_FINALLY, D_OBJC },
544 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
545 { "optional", RID_AT_OPTIONAL, D_OBJC },
546 { "required", RID_AT_REQUIRED, D_OBJC },
547 { "property", RID_AT_PROPERTY, D_OBJC },
548 { "package", RID_AT_PACKAGE, D_OBJC },
549 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
550 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
551 /* These are recognized only in protocol-qualifier context
553 { "bycopy", RID_BYCOPY, D_OBJC },
554 { "byref", RID_BYREF, D_OBJC },
555 { "in", RID_IN, D_OBJC },
556 { "inout", RID_INOUT, D_OBJC },
557 { "oneway", RID_ONEWAY, D_OBJC },
558 { "out", RID_OUT, D_OBJC },
559 /* These are recognized inside a property attribute list */
560 { "assign", RID_ASSIGN, D_OBJC },
561 { "copy", RID_COPY, D_OBJC },
562 { "getter", RID_GETTER, D_OBJC },
563 { "nonatomic", RID_NONATOMIC, D_OBJC },
564 { "readonly", RID_READONLY, D_OBJC },
565 { "readwrite", RID_READWRITE, D_OBJC },
566 { "retain", RID_RETAIN, D_OBJC },
567 { "setter", RID_SETTER, D_OBJC },
570 const unsigned int num_c_common_reswords =
571 sizeof c_common_reswords / sizeof (struct c_common_resword);
573 /* Table of machine-independent attributes common to all C-like languages. */
574 const struct attribute_spec c_common_attribute_table[] =
576 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
577 { "packed", 0, 0, false, false, false,
578 handle_packed_attribute },
579 { "nocommon", 0, 0, true, false, false,
580 handle_nocommon_attribute },
581 { "common", 0, 0, true, false, false,
582 handle_common_attribute },
583 /* FIXME: logically, noreturn attributes should be listed as
584 "false, true, true" and apply to function types. But implementing this
585 would require all the places in the compiler that use TREE_THIS_VOLATILE
586 on a decl to identify non-returning functions to be located and fixed
587 to check the function type instead. */
588 { "noreturn", 0, 0, true, false, false,
589 handle_noreturn_attribute },
590 { "volatile", 0, 0, true, false, false,
591 handle_noreturn_attribute },
592 { "noinline", 0, 0, true, false, false,
593 handle_noinline_attribute },
594 { "noclone", 0, 0, true, false, false,
595 handle_noclone_attribute },
596 { "leaf", 0, 0, true, false, false,
597 handle_leaf_attribute },
598 { "always_inline", 0, 0, true, false, false,
599 handle_always_inline_attribute },
600 { "gnu_inline", 0, 0, true, false, false,
601 handle_gnu_inline_attribute },
602 { "artificial", 0, 0, true, false, false,
603 handle_artificial_attribute },
604 { "flatten", 0, 0, true, false, false,
605 handle_flatten_attribute },
606 { "used", 0, 0, true, false, false,
607 handle_used_attribute },
608 { "unused", 0, 0, false, false, false,
609 handle_unused_attribute },
610 { "externally_visible", 0, 0, true, false, false,
611 handle_externally_visible_attribute },
612 /* The same comments as for noreturn attributes apply to const ones. */
613 { "const", 0, 0, true, false, false,
614 handle_const_attribute },
615 { "transparent_union", 0, 0, false, false, false,
616 handle_transparent_union_attribute },
617 { "constructor", 0, 1, true, false, false,
618 handle_constructor_attribute },
619 { "destructor", 0, 1, true, false, false,
620 handle_destructor_attribute },
621 { "mode", 1, 1, false, true, false,
622 handle_mode_attribute },
623 { "section", 1, 1, true, false, false,
624 handle_section_attribute },
625 { "aligned", 0, 1, false, false, false,
626 handle_aligned_attribute },
627 { "weak", 0, 0, true, false, false,
628 handle_weak_attribute },
629 { "ifunc", 1, 1, true, false, false,
630 handle_ifunc_attribute },
631 { "alias", 1, 1, true, false, false,
632 handle_alias_attribute },
633 { "weakref", 0, 1, true, false, false,
634 handle_weakref_attribute },
635 { "no_instrument_function", 0, 0, true, false, false,
636 handle_no_instrument_function_attribute },
637 { "malloc", 0, 0, true, false, false,
638 handle_malloc_attribute },
639 { "returns_twice", 0, 0, true, false, false,
640 handle_returns_twice_attribute },
641 { "no_stack_limit", 0, 0, true, false, false,
642 handle_no_limit_stack_attribute },
643 { "pure", 0, 0, true, false, false,
644 handle_pure_attribute },
645 /* For internal use (marking of builtins) only. The name contains space
646 to prevent its usage in source code. */
647 { "no vops", 0, 0, true, false, false,
648 handle_novops_attribute },
649 { "deprecated", 0, 1, false, false, false,
650 handle_deprecated_attribute },
651 { "vector_size", 1, 1, false, true, false,
652 handle_vector_size_attribute },
653 { "visibility", 1, 1, false, false, false,
654 handle_visibility_attribute },
655 { "tls_model", 1, 1, true, false, false,
656 handle_tls_model_attribute },
657 { "nonnull", 0, -1, false, true, true,
658 handle_nonnull_attribute },
659 { "nothrow", 0, 0, true, false, false,
660 handle_nothrow_attribute },
661 { "may_alias", 0, 0, false, true, false, NULL },
662 { "cleanup", 1, 1, true, false, false,
663 handle_cleanup_attribute },
664 { "warn_unused_result", 0, 0, false, true, true,
665 handle_warn_unused_result_attribute },
666 { "sentinel", 0, 1, false, true, true,
667 handle_sentinel_attribute },
668 /* For internal use (marking of builtins) only. The name contains space
669 to prevent its usage in source code. */
670 { "type generic", 0, 0, false, true, true,
671 handle_type_generic_attribute },
672 { "alloc_size", 1, 2, false, true, true,
673 handle_alloc_size_attribute },
674 { "cold", 0, 0, true, false, false,
675 handle_cold_attribute },
676 { "hot", 0, 0, true, false, false,
677 handle_hot_attribute },
678 { "warning", 1, 1, true, false, false,
679 handle_error_attribute },
680 { "error", 1, 1, true, false, false,
681 handle_error_attribute },
682 { "target", 1, -1, true, false, false,
683 handle_target_attribute },
684 { "optimize", 1, -1, true, false, false,
685 handle_optimize_attribute },
686 { "no_split_stack", 0, 0, true, false, false,
687 handle_no_split_stack_attribute },
688 /* For internal use (marking of builtins and runtime functions) only.
689 The name contains space to prevent its usage in source code. */
690 { "fn spec", 1, 1, false, true, true,
691 handle_fnspec_attribute },
692 { NULL, 0, 0, false, false, false, NULL }
695 /* Give the specifications for the format attributes, used by C and all
698 const struct attribute_spec c_common_format_attribute_table[] =
700 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
701 { "format", 3, 3, false, true, true,
702 handle_format_attribute },
703 { "format_arg", 1, 1, false, true, true,
704 handle_format_arg_attribute },
705 { NULL, 0, 0, false, false, false, NULL }
708 /* Return identifier for address space AS. */
711 c_addr_space_name (addr_space_t as)
713 int rid = RID_FIRST_ADDR_SPACE + as;
714 gcc_assert (ridpointers [rid]);
715 return IDENTIFIER_POINTER (ridpointers [rid]);
718 /* Push current bindings for the function name VAR_DECLS. */
721 start_fname_decls (void)
724 tree saved = NULL_TREE;
726 for (ix = 0; fname_vars[ix].decl; ix++)
728 tree decl = *fname_vars[ix].decl;
732 saved = tree_cons (decl, build_int_cst (NULL_TREE, ix), saved);
733 *fname_vars[ix].decl = NULL_TREE;
736 if (saved || saved_function_name_decls)
737 /* Normally they'll have been NULL, so only push if we've got a
738 stack, or they are non-NULL. */
739 saved_function_name_decls = tree_cons (saved, NULL_TREE,
740 saved_function_name_decls);
743 /* Finish up the current bindings, adding them into the current function's
744 statement tree. This must be done _before_ finish_stmt_tree is called.
745 If there is no current function, we must be at file scope and no statements
746 are involved. Pop the previous bindings. */
749 finish_fname_decls (void)
752 tree stmts = NULL_TREE;
753 tree stack = saved_function_name_decls;
755 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
756 append_to_statement_list (TREE_VALUE (stack), &stmts);
760 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
762 if (TREE_CODE (*bodyp) == BIND_EXPR)
763 bodyp = &BIND_EXPR_BODY (*bodyp);
765 append_to_statement_list_force (*bodyp, &stmts);
769 for (ix = 0; fname_vars[ix].decl; ix++)
770 *fname_vars[ix].decl = NULL_TREE;
774 /* We had saved values, restore them. */
777 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
779 tree decl = TREE_PURPOSE (saved);
780 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
782 *fname_vars[ix].decl = decl;
784 stack = TREE_CHAIN (stack);
786 saved_function_name_decls = stack;
789 /* Return the text name of the current function, suitably prettified
790 by PRETTY_P. Return string must be freed by caller. */
793 fname_as_string (int pretty_p)
795 const char *name = "top level";
798 cpp_string cstr = { 0, 0 }, strname;
806 if (current_function_decl)
807 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
809 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
811 namep = XNEWVEC (char, len);
812 snprintf (namep, len, "\"%s\"", name);
813 strname.text = (unsigned char *) namep;
814 strname.len = len - 1;
816 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
819 return (const char *) cstr.text;
825 /* Return the VAR_DECL for a const char array naming the current
826 function. If the VAR_DECL has not yet been created, create it
827 now. RID indicates how it should be formatted and IDENTIFIER_NODE
828 ID is its name (unfortunately C and C++ hold the RID values of
829 keywords in different places, so we can't derive RID from ID in
830 this language independent code. LOC is the location of the
834 fname_decl (location_t loc, unsigned int rid, tree id)
837 tree decl = NULL_TREE;
839 for (ix = 0; fname_vars[ix].decl; ix++)
840 if (fname_vars[ix].rid == rid)
843 decl = *fname_vars[ix].decl;
846 /* If a tree is built here, it would normally have the lineno of
847 the current statement. Later this tree will be moved to the
848 beginning of the function and this line number will be wrong.
849 To avoid this problem set the lineno to 0 here; that prevents
850 it from appearing in the RTL. */
852 location_t saved_location = input_location;
853 input_location = UNKNOWN_LOCATION;
855 stmts = push_stmt_list ();
856 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
857 stmts = pop_stmt_list (stmts);
858 if (!IS_EMPTY_STMT (stmts))
859 saved_function_name_decls
860 = tree_cons (decl, stmts, saved_function_name_decls);
861 *fname_vars[ix].decl = decl;
862 input_location = saved_location;
864 if (!ix && !current_function_decl)
865 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
870 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
873 fix_string_type (tree value)
875 int length = TREE_STRING_LENGTH (value);
877 tree e_type, i_type, a_type;
879 /* Compute the number of elements, for the array type. */
880 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
883 e_type = char_type_node;
885 else if (TREE_TYPE (value) == char16_array_type_node)
887 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
888 e_type = char16_type_node;
890 else if (TREE_TYPE (value) == char32_array_type_node)
892 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
893 e_type = char32_type_node;
897 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
898 e_type = wchar_type_node;
901 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
902 limit in C++98 Annex B is very large (65536) and is not normative,
903 so we do not diagnose it (warn_overlength_strings is forced off
904 in c_common_post_options). */
905 if (warn_overlength_strings)
907 const int nchars_max = flag_isoc99 ? 4095 : 509;
908 const int relevant_std = flag_isoc99 ? 99 : 90;
909 if (nchars - 1 > nchars_max)
910 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
911 separate the %d from the 'C'. 'ISO' should not be
912 translated, but it may be moved after 'C%d' in languages
913 where modifiers follow nouns. */
914 pedwarn (input_location, OPT_Woverlength_strings,
915 "string length %qd is greater than the length %qd "
916 "ISO C%d compilers are required to support",
917 nchars - 1, nchars_max, relevant_std);
920 /* Create the array type for the string constant. The ISO C++
921 standard says that a string literal has type `const char[N]' or
922 `const wchar_t[N]'. We use the same logic when invoked as a C
923 front-end with -Wwrite-strings.
924 ??? We should change the type of an expression depending on the
925 state of a warning flag. We should just be warning -- see how
926 this is handled in the C++ front-end for the deprecated implicit
927 conversion from string literals to `char*' or `wchar_t*'.
929 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
930 array type being the unqualified version of that type.
931 Therefore, if we are constructing an array of const char, we must
932 construct the matching unqualified array type first. The C front
933 end does not require this, but it does no harm, so we do it
935 i_type = build_index_type (build_int_cst (NULL_TREE, nchars - 1));
936 a_type = build_array_type (e_type, i_type);
937 if (c_dialect_cxx() || warn_write_strings)
938 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
940 TREE_TYPE (value) = a_type;
941 TREE_CONSTANT (value) = 1;
942 TREE_READONLY (value) = 1;
943 TREE_STATIC (value) = 1;
947 /* Fully fold EXPR, an expression that was not folded (beyond integer
948 constant expressions and null pointer constants) when being built
949 up. If IN_INIT, this is in a static initializer and certain
950 changes are made to the folding done. Clear *MAYBE_CONST if
951 MAYBE_CONST is not NULL and EXPR is definitely not a constant
952 expression because it contains an evaluated operator (in C99) or an
953 operator outside of sizeof returning an integer constant (in C90)
954 not permitted in constant expressions, or because it contains an
955 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
956 set to true by callers before calling this function.) Return the
957 folded expression. Function arguments have already been folded
958 before calling this function, as have the contents of SAVE_EXPR,
959 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
960 C_MAYBE_CONST_EXPR. */
963 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
966 tree eptype = NULL_TREE;
968 bool maybe_const_itself = true;
969 location_t loc = EXPR_LOCATION (expr);
971 /* This function is not relevant to C++ because C++ folds while
972 parsing, and may need changes to be correct for C++ when C++
973 stops folding while parsing. */
974 if (c_dialect_cxx ())
978 maybe_const = &dummy;
979 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
981 eptype = TREE_TYPE (expr);
982 expr = TREE_OPERAND (expr, 0);
984 ret = c_fully_fold_internal (expr, in_init, maybe_const,
985 &maybe_const_itself);
987 ret = fold_convert_loc (loc, eptype, ret);
988 *maybe_const &= maybe_const_itself;
992 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
993 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
994 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
995 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
996 both evaluated and unevaluated subexpressions while
997 *MAYBE_CONST_ITSELF is carried from only evaluated
1001 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1002 bool *maybe_const_itself)
1005 enum tree_code code = TREE_CODE (expr);
1006 enum tree_code_class kind = TREE_CODE_CLASS (code);
1007 location_t loc = EXPR_LOCATION (expr);
1008 tree op0, op1, op2, op3;
1009 tree orig_op0, orig_op1, orig_op2;
1010 bool op0_const = true, op1_const = true, op2_const = true;
1011 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1012 bool nowarning = TREE_NO_WARNING (expr);
1015 /* This function is not relevant to C++ because C++ folds while
1016 parsing, and may need changes to be correct for C++ when C++
1017 stops folding while parsing. */
1018 if (c_dialect_cxx ())
1021 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1022 anything else not counted as an expression cannot usefully be
1023 folded further at this point. */
1024 if (!IS_EXPR_CODE_CLASS (kind)
1025 || kind == tcc_statement
1026 || code == SAVE_EXPR)
1029 /* Operands of variable-length expressions (function calls) have
1030 already been folded, as have __builtin_* function calls, and such
1031 expressions cannot occur in constant expressions. */
1032 if (kind == tcc_vl_exp)
1034 *maybe_const_operands = false;
1039 if (code == C_MAYBE_CONST_EXPR)
1041 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1042 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1043 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1044 *maybe_const_operands = false;
1045 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1046 *maybe_const_itself = false;
1047 if (pre && !in_init)
1048 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1054 /* Assignment, increment, decrement, function call and comma
1055 operators, and statement expressions, cannot occur in constant
1056 expressions if evaluated / outside of sizeof. (Function calls
1057 were handled above, though VA_ARG_EXPR is treated like a function
1058 call here, and statement expressions are handled through
1059 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1063 case PREDECREMENT_EXPR:
1064 case PREINCREMENT_EXPR:
1065 case POSTDECREMENT_EXPR:
1066 case POSTINCREMENT_EXPR:
1068 *maybe_const_operands = false;
1075 *maybe_const_operands = false;
1083 /* Fold individual tree codes as appropriate. */
1086 case COMPOUND_LITERAL_EXPR:
1087 /* Any non-constancy will have been marked in a containing
1088 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1092 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1093 op1 = TREE_OPERAND (expr, 1);
1094 op2 = TREE_OPERAND (expr, 2);
1095 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1096 maybe_const_itself);
1097 STRIP_TYPE_NOPS (op0);
1098 if (op0 != orig_op0)
1099 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1102 TREE_READONLY (ret) = TREE_READONLY (expr);
1103 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1108 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1109 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1110 op2 = TREE_OPERAND (expr, 2);
1111 op3 = TREE_OPERAND (expr, 3);
1112 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1113 maybe_const_itself);
1114 STRIP_TYPE_NOPS (op0);
1115 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1116 maybe_const_itself);
1117 STRIP_TYPE_NOPS (op1);
1118 op1 = decl_constant_value_for_optimization (op1);
1119 if (op0 != orig_op0 || op1 != orig_op1)
1120 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1123 TREE_READONLY (ret) = TREE_READONLY (expr);
1124 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1125 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1132 case PREDECREMENT_EXPR:
1133 case PREINCREMENT_EXPR:
1134 case POSTDECREMENT_EXPR:
1135 case POSTINCREMENT_EXPR:
1139 case POINTER_PLUS_EXPR:
1140 case TRUNC_DIV_EXPR:
1142 case FLOOR_DIV_EXPR:
1143 case TRUNC_MOD_EXPR:
1145 case EXACT_DIV_EXPR:
1158 case TRUTH_AND_EXPR:
1160 case TRUTH_XOR_EXPR:
1161 case UNORDERED_EXPR:
1168 /* Binary operations evaluating both arguments (increment and
1169 decrement are binary internally in GCC). */
1170 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1171 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1172 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1173 maybe_const_itself);
1174 STRIP_TYPE_NOPS (op0);
1175 if (code != MODIFY_EXPR
1176 && code != PREDECREMENT_EXPR
1177 && code != PREINCREMENT_EXPR
1178 && code != POSTDECREMENT_EXPR
1179 && code != POSTINCREMENT_EXPR)
1180 op0 = decl_constant_value_for_optimization (op0);
1181 /* The RHS of a MODIFY_EXPR was fully folded when building that
1182 expression for the sake of conversion warnings. */
1183 if (code != MODIFY_EXPR)
1184 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1185 maybe_const_itself);
1186 STRIP_TYPE_NOPS (op1);
1187 op1 = decl_constant_value_for_optimization (op1);
1188 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1190 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1191 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1194 if (TREE_OVERFLOW_P (ret)
1195 && !TREE_OVERFLOW_P (op0)
1196 && !TREE_OVERFLOW_P (op1))
1197 overflow_warning (EXPR_LOCATION (expr), ret);
1201 case FIX_TRUNC_EXPR:
1204 case NON_LVALUE_EXPR:
1207 case TRUTH_NOT_EXPR:
1212 /* Unary operations. */
1213 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1214 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1215 maybe_const_itself);
1216 STRIP_TYPE_NOPS (op0);
1217 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1218 op0 = decl_constant_value_for_optimization (op0);
1219 if (op0 != orig_op0 || in_init)
1221 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1222 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1225 if (code == INDIRECT_REF
1227 && TREE_CODE (ret) == INDIRECT_REF)
1229 TREE_READONLY (ret) = TREE_READONLY (expr);
1230 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1231 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1235 case FIX_TRUNC_EXPR:
1238 /* Don't warn about explicit conversions. We will already
1239 have warned about suspect implicit conversions. */
1243 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1244 overflow_warning (EXPR_LOCATION (expr), ret);
1249 case TRUTH_ANDIF_EXPR:
1250 case TRUTH_ORIF_EXPR:
1251 /* Binary operations not necessarily evaluating both
1253 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1254 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1255 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1256 STRIP_TYPE_NOPS (op0);
1258 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1259 ? truthvalue_false_node
1260 : truthvalue_true_node));
1261 c_inhibit_evaluation_warnings += unused_p;
1262 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1263 STRIP_TYPE_NOPS (op1);
1264 c_inhibit_evaluation_warnings -= unused_p;
1266 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1268 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1269 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1272 *maybe_const_operands &= op0_const;
1273 *maybe_const_itself &= op0_const_self;
1277 && (code == TRUTH_ANDIF_EXPR
1278 ? op0 == truthvalue_false_node
1279 : op0 == truthvalue_true_node)))
1280 *maybe_const_operands &= op1_const;
1283 && (code == TRUTH_ANDIF_EXPR
1284 ? op0 == truthvalue_false_node
1285 : op0 == truthvalue_true_node)))
1286 *maybe_const_itself &= op1_const_self;
1290 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1291 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1292 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1293 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1295 STRIP_TYPE_NOPS (op0);
1296 c_inhibit_evaluation_warnings += (op0 == truthvalue_false_node);
1297 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1298 STRIP_TYPE_NOPS (op1);
1299 c_inhibit_evaluation_warnings -= (op0 == truthvalue_false_node);
1301 c_inhibit_evaluation_warnings += (op0 == truthvalue_true_node);
1302 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1303 STRIP_TYPE_NOPS (op2);
1304 c_inhibit_evaluation_warnings -= (op0 == truthvalue_true_node);
1306 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1307 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1310 *maybe_const_operands &= op0_const;
1311 *maybe_const_itself &= op0_const_self;
1315 && op0 == truthvalue_false_node))
1316 *maybe_const_operands &= op1_const;
1319 && op0 == truthvalue_false_node))
1320 *maybe_const_itself &= op1_const_self;
1324 && op0 == truthvalue_true_node))
1325 *maybe_const_operands &= op2_const;
1328 && op0 == truthvalue_true_node))
1329 *maybe_const_itself &= op2_const_self;
1332 case EXCESS_PRECISION_EXPR:
1333 /* Each case where an operand with excess precision may be
1334 encountered must remove the EXCESS_PRECISION_EXPR around
1335 inner operands and possibly put one around the whole
1336 expression or possibly convert to the semantic type (which
1337 c_fully_fold does); we cannot tell at this stage which is
1338 appropriate in any particular case. */
1342 /* Various codes may appear through folding built-in functions
1343 and their arguments. */
1348 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1349 have been done by this point, so remove them again. */
1350 nowarning |= TREE_NO_WARNING (ret);
1351 STRIP_TYPE_NOPS (ret);
1352 if (nowarning && !TREE_NO_WARNING (ret))
1354 if (!CAN_HAVE_LOCATION_P (ret))
1355 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1356 TREE_NO_WARNING (ret) = 1;
1359 protected_set_expr_location (ret, loc);
1363 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1364 return EXP. Otherwise, return either EXP or its known constant
1365 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1366 Is the BLKmode test appropriate? */
1369 decl_constant_value_for_optimization (tree exp)
1373 /* This function is only used by C, for c_fully_fold and other
1374 optimization, and may not be correct for C++. */
1375 if (c_dialect_cxx ())
1379 || TREE_CODE (exp) != VAR_DECL
1380 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1381 || DECL_MODE (exp) == BLKmode)
1384 ret = decl_constant_value (exp);
1385 /* Avoid unwanted tree sharing between the initializer and current
1386 function's body where the tree can be modified e.g. by the
1388 if (ret != exp && TREE_STATIC (exp))
1389 ret = unshare_expr (ret);
1393 /* Print a warning if a constant expression had overflow in folding.
1394 Invoke this function on every expression that the language
1395 requires to be a constant expression.
1396 Note the ANSI C standard says it is erroneous for a
1397 constant expression to overflow. */
1400 constant_expression_warning (tree value)
1402 if (warn_overflow && pedantic
1403 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1404 || TREE_CODE (value) == FIXED_CST
1405 || TREE_CODE (value) == VECTOR_CST
1406 || TREE_CODE (value) == COMPLEX_CST)
1407 && TREE_OVERFLOW (value))
1408 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1411 /* The same as above but print an unconditional error. */
1413 constant_expression_error (tree value)
1415 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1416 || TREE_CODE (value) == FIXED_CST
1417 || TREE_CODE (value) == VECTOR_CST
1418 || TREE_CODE (value) == COMPLEX_CST)
1419 && TREE_OVERFLOW (value))
1420 error ("overflow in constant expression");
1423 /* Print a warning if an expression had overflow in folding and its
1426 Invoke this function on every expression that
1427 (1) appears in the source code, and
1428 (2) is a constant expression that overflowed, and
1429 (3) is not already checked by convert_and_check;
1430 however, do not invoke this function on operands of explicit casts
1431 or when the expression is the result of an operator and any operand
1432 already overflowed. */
1435 overflow_warning (location_t loc, tree value)
1437 if (c_inhibit_evaluation_warnings != 0)
1440 switch (TREE_CODE (value))
1443 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1447 warning_at (loc, OPT_Woverflow,
1448 "floating point overflow in expression");
1452 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1456 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1460 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1461 warning_at (loc, OPT_Woverflow,
1462 "complex integer overflow in expression");
1463 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1464 warning_at (loc, OPT_Woverflow,
1465 "complex floating point overflow in expression");
1473 /* Warn about uses of logical || / && operator in a context where it
1474 is likely that the bitwise equivalent was intended by the
1475 programmer. We have seen an expression in which CODE is a binary
1476 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1477 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1479 warn_logical_operator (location_t location, enum tree_code code, tree type,
1480 enum tree_code code_left, tree op_left,
1481 enum tree_code ARG_UNUSED (code_right), tree op_right)
1483 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1484 int in0_p, in1_p, in_p;
1485 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1486 bool strict_overflow_p = false;
1488 if (code != TRUTH_ANDIF_EXPR
1489 && code != TRUTH_AND_EXPR
1490 && code != TRUTH_ORIF_EXPR
1491 && code != TRUTH_OR_EXPR)
1494 /* Warn if &&/|| are being used in a context where it is
1495 likely that the bitwise equivalent was intended by the
1496 programmer. That is, an expression such as op && MASK
1497 where op should not be any boolean expression, nor a
1498 constant, and mask seems to be a non-boolean integer constant. */
1499 if (!truth_value_p (code_left)
1500 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1501 && !CONSTANT_CLASS_P (op_left)
1502 && !TREE_NO_WARNING (op_left)
1503 && TREE_CODE (op_right) == INTEGER_CST
1504 && !integer_zerop (op_right)
1505 && !integer_onep (op_right))
1508 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1509 " applied to non-boolean constant");
1511 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1512 " applied to non-boolean constant");
1513 TREE_NO_WARNING (op_left) = true;
1517 /* We do not warn for constants because they are typical of macro
1518 expansions that test for features. */
1519 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1522 /* This warning only makes sense with logical operands. */
1523 if (!(truth_value_p (TREE_CODE (op_left))
1524 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1525 || !(truth_value_p (TREE_CODE (op_right))
1526 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1529 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1530 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1532 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1533 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1535 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1536 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1538 /* If this is an OR operation, invert both sides; we will invert
1539 again at the end. */
1541 in0_p = !in0_p, in1_p = !in1_p;
1543 /* If both expressions are the same, if we can merge the ranges, and we
1544 can build the range test, return it or it inverted. */
1545 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1546 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1548 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1549 type, lhs, in_p, low, high)))
1551 if (TREE_CODE (tem) != INTEGER_CST)
1555 warning_at (location, OPT_Wlogical_op,
1557 "of collectively exhaustive tests is always true");
1559 warning_at (location, OPT_Wlogical_op,
1561 "of mutually exclusive tests is always false");
1566 /* Print a warning about casts that might indicate violation
1567 of strict aliasing rules if -Wstrict-aliasing is used and
1568 strict aliasing mode is in effect. OTYPE is the original
1569 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1572 strict_aliasing_warning (tree otype, tree type, tree expr)
1574 /* Strip pointer conversion chains and get to the correct original type. */
1576 otype = TREE_TYPE (expr);
1578 if (!(flag_strict_aliasing
1579 && POINTER_TYPE_P (type)
1580 && POINTER_TYPE_P (otype)
1581 && !VOID_TYPE_P (TREE_TYPE (type)))
1582 /* If the type we are casting to is a ref-all pointer
1583 dereferencing it is always valid. */
1584 || TYPE_REF_CAN_ALIAS_ALL (type))
1587 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1588 && (DECL_P (TREE_OPERAND (expr, 0))
1589 || handled_component_p (TREE_OPERAND (expr, 0))))
1591 /* Casting the address of an object to non void pointer. Warn
1592 if the cast breaks type based aliasing. */
1593 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1595 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1596 "might break strict-aliasing rules");
1601 /* warn_strict_aliasing >= 3. This includes the default (3).
1602 Only warn if the cast is dereferenced immediately. */
1603 alias_set_type set1 =
1604 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1605 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1607 if (set1 != set2 && set2 != 0
1608 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1610 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1611 "pointer will break strict-aliasing rules");
1614 else if (warn_strict_aliasing == 2
1615 && !alias_sets_must_conflict_p (set1, set2))
1617 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1618 "pointer might break strict-aliasing rules");
1624 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1626 /* At this level, warn for any conversions, even if an address is
1627 not taken in the same statement. This will likely produce many
1628 false positives, but could be useful to pinpoint problems that
1629 are not revealed at higher levels. */
1630 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1631 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1632 if (!COMPLETE_TYPE_P (type)
1633 || !alias_sets_must_conflict_p (set1, set2))
1635 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1636 "pointer might break strict-aliasing rules");
1644 /* Warn for unlikely, improbable, or stupid DECL declarations
1648 check_main_parameter_types (tree decl)
1653 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl)); args;
1654 args = TREE_CHAIN (args))
1656 tree type = args ? TREE_VALUE (args) : 0;
1658 if (type == void_type_node || type == error_mark_node )
1665 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1666 pedwarn (input_location, OPT_Wmain, "first argument of %q+D should be %<int%>",
1671 if (TREE_CODE (type) != POINTER_TYPE
1672 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1673 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1675 pedwarn (input_location, OPT_Wmain, "second argument of %q+D should be %<char **%>",
1680 if (TREE_CODE (type) != POINTER_TYPE
1681 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1682 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1684 pedwarn (input_location, OPT_Wmain, "third argument of %q+D should probably be "
1685 "%<char **%>", decl);
1690 /* It is intentional that this message does not mention the third
1691 argument because it's only mentioned in an appendix of the
1693 if (argct > 0 && (argct < 2 || argct > 3))
1694 pedwarn (input_location, OPT_Wmain, "%q+D takes only zero or two arguments", decl);
1697 /* True if pointers to distinct types T1 and T2 can be converted to
1698 each other without an explicit cast. Only returns true for opaque
1701 vector_targets_convertible_p (const_tree t1, const_tree t2)
1703 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1704 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1705 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1711 /* True if vector types T1 and T2 can be converted to each other
1712 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1713 can only be converted with -flax-vector-conversions yet that is not
1714 in effect, emit a note telling the user about that option if such
1715 a note has not previously been emitted. */
1717 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1719 static bool emitted_lax_note = false;
1720 bool convertible_lax;
1722 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1723 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1727 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1728 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1729 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1730 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1731 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1733 if (!convertible_lax || flag_lax_vector_conversions)
1734 return convertible_lax;
1736 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1737 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1740 if (emit_lax_note && !emitted_lax_note)
1742 emitted_lax_note = true;
1743 inform (input_location, "use -flax-vector-conversions to permit "
1744 "conversions between vectors with differing "
1745 "element types or numbers of subparts");
1751 /* This is a helper function of build_binary_op.
1753 For certain operations if both args were extended from the same
1754 smaller type, do the arithmetic in that type and then extend.
1756 BITWISE indicates a bitwise operation.
1757 For them, this optimization is safe only if
1758 both args are zero-extended or both are sign-extended.
1759 Otherwise, we might change the result.
1760 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1761 but calculated in (unsigned short) it would be (unsigned short)-1.
1763 tree shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1765 int unsigned0, unsigned1;
1770 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1771 excessive narrowing when we call get_narrower below. For
1772 example, suppose that OP0 is of unsigned int extended
1773 from signed char and that RESULT_TYPE is long long int.
1774 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1777 (long long int) (unsigned int) signed_char
1779 which get_narrower would narrow down to
1781 (unsigned int) signed char
1783 If we do not cast OP0 first, get_narrower would return
1784 signed_char, which is inconsistent with the case of the
1786 op0 = convert (result_type, op0);
1787 op1 = convert (result_type, op1);
1789 arg0 = get_narrower (op0, &unsigned0);
1790 arg1 = get_narrower (op1, &unsigned1);
1792 /* UNS is 1 if the operation to be done is an unsigned one. */
1793 uns = TYPE_UNSIGNED (result_type);
1795 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1796 but it *requires* conversion to FINAL_TYPE. */
1798 if ((TYPE_PRECISION (TREE_TYPE (op0))
1799 == TYPE_PRECISION (TREE_TYPE (arg0)))
1800 && TREE_TYPE (op0) != result_type)
1801 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1802 if ((TYPE_PRECISION (TREE_TYPE (op1))
1803 == TYPE_PRECISION (TREE_TYPE (arg1)))
1804 && TREE_TYPE (op1) != result_type)
1805 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1807 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1809 /* For bitwise operations, signedness of nominal type
1810 does not matter. Consider only how operands were extended. */
1814 /* Note that in all three cases below we refrain from optimizing
1815 an unsigned operation on sign-extended args.
1816 That would not be valid. */
1818 /* Both args variable: if both extended in same way
1819 from same width, do it in that width.
1820 Do it unsigned if args were zero-extended. */
1821 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1822 < TYPE_PRECISION (result_type))
1823 && (TYPE_PRECISION (TREE_TYPE (arg1))
1824 == TYPE_PRECISION (TREE_TYPE (arg0)))
1825 && unsigned0 == unsigned1
1826 && (unsigned0 || !uns))
1827 return c_common_signed_or_unsigned_type
1828 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1830 else if (TREE_CODE (arg0) == INTEGER_CST
1831 && (unsigned1 || !uns)
1832 && (TYPE_PRECISION (TREE_TYPE (arg1))
1833 < TYPE_PRECISION (result_type))
1835 = c_common_signed_or_unsigned_type (unsigned1,
1837 && !POINTER_TYPE_P (type)
1838 && int_fits_type_p (arg0, type))
1841 else if (TREE_CODE (arg1) == INTEGER_CST
1842 && (unsigned0 || !uns)
1843 && (TYPE_PRECISION (TREE_TYPE (arg0))
1844 < TYPE_PRECISION (result_type))
1846 = c_common_signed_or_unsigned_type (unsigned0,
1848 && !POINTER_TYPE_P (type)
1849 && int_fits_type_p (arg1, type))
1855 /* Warns if the conversion of EXPR to TYPE may alter a value.
1856 This is a helper function for warnings_for_convert_and_check. */
1859 conversion_warning (tree type, tree expr)
1861 bool give_warning = false;
1864 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
1865 tree expr_type = TREE_TYPE (expr);
1866 location_t loc = EXPR_LOC_OR_HERE (expr);
1868 if (!warn_conversion && !warn_sign_conversion)
1871 /* If any operand is artificial, then this expression was generated
1872 by the compiler and we do not warn. */
1873 for (i = 0; i < expr_num_operands; i++)
1875 tree op = TREE_OPERAND (expr, i);
1876 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
1880 switch (TREE_CODE (expr))
1888 case TRUTH_ANDIF_EXPR:
1889 case TRUTH_ORIF_EXPR:
1890 case TRUTH_AND_EXPR:
1892 case TRUTH_XOR_EXPR:
1893 case TRUTH_NOT_EXPR:
1894 /* Conversion from boolean to a signed:1 bit-field (which only
1895 can hold the values 0 and -1) doesn't lose information - but
1896 it does change the value. */
1897 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1898 warning_at (loc, OPT_Wconversion,
1899 "conversion to %qT from boolean expression", type);
1905 /* Warn for real constant that is not an exact integer converted
1907 if (TREE_CODE (expr_type) == REAL_TYPE
1908 && TREE_CODE (type) == INTEGER_TYPE)
1910 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1911 give_warning = true;
1913 /* Warn for an integer constant that does not fit into integer type. */
1914 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1915 && TREE_CODE (type) == INTEGER_TYPE
1916 && !int_fits_type_p (expr, type))
1918 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1919 && tree_int_cst_sgn (expr) < 0)
1920 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1921 " implicitly converted to unsigned type");
1922 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1923 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1924 " constant value to negative integer");
1926 give_warning = true;
1928 else if (TREE_CODE (type) == REAL_TYPE)
1930 /* Warn for an integer constant that does not fit into real type. */
1931 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1933 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1934 if (!exact_real_truncate (TYPE_MODE (type), &a))
1935 give_warning = true;
1937 /* Warn for a real constant that does not fit into a smaller
1939 else if (TREE_CODE (expr_type) == REAL_TYPE
1940 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1942 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1943 if (!exact_real_truncate (TYPE_MODE (type), &a))
1944 give_warning = true;
1949 warning_at (loc, OPT_Wconversion,
1950 "conversion to %qT alters %qT constant value",
1957 /* In case of COND_EXPR, if both operands are constants or
1958 COND_EXPR, then we do not care about the type of COND_EXPR,
1959 only about the conversion of each operand. */
1960 tree op1 = TREE_OPERAND (expr, 1);
1961 tree op2 = TREE_OPERAND (expr, 2);
1963 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
1964 || TREE_CODE (op1) == COND_EXPR)
1965 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
1966 || TREE_CODE (op2) == COND_EXPR))
1968 conversion_warning (type, op1);
1969 conversion_warning (type, op2);
1975 default: /* 'expr' is not a constant. */
1977 /* Warn for real types converted to integer types. */
1978 if (TREE_CODE (expr_type) == REAL_TYPE
1979 && TREE_CODE (type) == INTEGER_TYPE)
1980 give_warning = true;
1982 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1983 && TREE_CODE (type) == INTEGER_TYPE)
1985 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1986 expr = get_unwidened (expr, 0);
1987 expr_type = TREE_TYPE (expr);
1989 /* Don't warn for short y; short x = ((int)y & 0xff); */
1990 if (TREE_CODE (expr) == BIT_AND_EXPR
1991 || TREE_CODE (expr) == BIT_IOR_EXPR
1992 || TREE_CODE (expr) == BIT_XOR_EXPR)
1994 /* If both args were extended from a shortest type,
1995 use that type if that is safe. */
1996 expr_type = shorten_binary_op (expr_type,
1997 TREE_OPERAND (expr, 0),
1998 TREE_OPERAND (expr, 1),
2001 if (TREE_CODE (expr) == BIT_AND_EXPR)
2003 tree op0 = TREE_OPERAND (expr, 0);
2004 tree op1 = TREE_OPERAND (expr, 1);
2005 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2006 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2008 /* If one of the operands is a non-negative constant
2009 that fits in the target type, then the type of the
2010 other operand does not matter. */
2011 if ((TREE_CODE (op0) == INTEGER_CST
2012 && int_fits_type_p (op0, c_common_signed_type (type))
2013 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2014 || (TREE_CODE (op1) == INTEGER_CST
2015 && int_fits_type_p (op1, c_common_signed_type (type))
2016 && int_fits_type_p (op1,
2017 c_common_unsigned_type (type))))
2019 /* If constant is unsigned and fits in the target
2020 type, then the result will also fit. */
2021 else if ((TREE_CODE (op0) == INTEGER_CST
2023 && int_fits_type_p (op0, type))
2024 || (TREE_CODE (op1) == INTEGER_CST
2026 && int_fits_type_p (op1, type)))
2030 /* Warn for integer types converted to smaller integer types. */
2031 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2032 give_warning = true;
2034 /* When they are the same width but different signedness,
2035 then the value may change. */
2036 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2037 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2038 /* Even when converted to a bigger type, if the type is
2039 unsigned but expr is signed, then negative values
2041 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2042 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2043 "may change the sign of the result",
2047 /* Warn for integer types converted to real types if and only if
2048 all the range of values of the integer type cannot be
2049 represented by the real type. */
2050 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2051 && TREE_CODE (type) == REAL_TYPE)
2053 tree type_low_bound, type_high_bound;
2054 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2056 /* Don't warn about char y = 0xff; float x = (int) y; */
2057 expr = get_unwidened (expr, 0);
2058 expr_type = TREE_TYPE (expr);
2060 type_low_bound = TYPE_MIN_VALUE (expr_type);
2061 type_high_bound = TYPE_MAX_VALUE (expr_type);
2062 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2063 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2065 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2066 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2067 give_warning = true;
2070 /* Warn for real types converted to smaller real types. */
2071 else if (TREE_CODE (expr_type) == REAL_TYPE
2072 && TREE_CODE (type) == REAL_TYPE
2073 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2074 give_warning = true;
2078 warning_at (loc, OPT_Wconversion,
2079 "conversion to %qT from %qT may alter its value",
2084 /* Produce warnings after a conversion. RESULT is the result of
2085 converting EXPR to TYPE. This is a helper function for
2086 convert_and_check and cp_convert_and_check. */
2089 warnings_for_convert_and_check (tree type, tree expr, tree result)
2091 if (TREE_CODE (expr) == INTEGER_CST
2092 && (TREE_CODE (type) == INTEGER_TYPE
2093 || TREE_CODE (type) == ENUMERAL_TYPE)
2094 && !int_fits_type_p (expr, type))
2096 /* Do not diagnose overflow in a constant expression merely
2097 because a conversion overflowed. */
2098 if (TREE_OVERFLOW (result))
2099 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2101 if (TYPE_UNSIGNED (type))
2103 /* This detects cases like converting -129 or 256 to
2105 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2106 warning (OPT_Woverflow,
2107 "large integer implicitly truncated to unsigned type");
2109 conversion_warning (type, expr);
2111 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2112 warning (OPT_Woverflow,
2113 "overflow in implicit constant conversion");
2114 /* No warning for converting 0x80000000 to int. */
2116 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2117 || TYPE_PRECISION (TREE_TYPE (expr))
2118 != TYPE_PRECISION (type)))
2119 warning (OPT_Woverflow,
2120 "overflow in implicit constant conversion");
2123 conversion_warning (type, expr);
2125 else if ((TREE_CODE (result) == INTEGER_CST
2126 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2127 warning (OPT_Woverflow,
2128 "overflow in implicit constant conversion");
2130 conversion_warning (type, expr);
2134 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2135 Invoke this function on every expression that is converted implicitly,
2136 i.e. because of language rules and not because of an explicit cast. */
2139 convert_and_check (tree type, tree expr)
2142 tree expr_for_warning;
2144 /* Convert from a value with possible excess precision rather than
2145 via the semantic type, but do not warn about values not fitting
2146 exactly in the semantic type. */
2147 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2149 tree orig_type = TREE_TYPE (expr);
2150 expr = TREE_OPERAND (expr, 0);
2151 expr_for_warning = convert (orig_type, expr);
2152 if (orig_type == type)
2153 return expr_for_warning;
2156 expr_for_warning = expr;
2158 if (TREE_TYPE (expr) == type)
2161 result = convert (type, expr);
2163 if (c_inhibit_evaluation_warnings == 0
2164 && !TREE_OVERFLOW_P (expr)
2165 && result != error_mark_node)
2166 warnings_for_convert_and_check (type, expr_for_warning, result);
2171 /* A node in a list that describes references to variables (EXPR), which are
2172 either read accesses if WRITER is zero, or write accesses, in which case
2173 WRITER is the parent of EXPR. */
2180 /* Used to implement a cache the results of a call to verify_tree. We only
2181 use this for SAVE_EXPRs. */
2184 struct tlist_cache *next;
2185 struct tlist *cache_before_sp;
2186 struct tlist *cache_after_sp;
2190 /* Obstack to use when allocating tlist structures, and corresponding
2192 static struct obstack tlist_obstack;
2193 static char *tlist_firstobj = 0;
2195 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2197 static struct tlist *warned_ids;
2198 /* SAVE_EXPRs need special treatment. We process them only once and then
2199 cache the results. */
2200 static struct tlist_cache *save_expr_cache;
2202 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2203 static void merge_tlist (struct tlist **, struct tlist *, int);
2204 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2205 static int warning_candidate_p (tree);
2206 static bool candidate_equal_p (const_tree, const_tree);
2207 static void warn_for_collisions (struct tlist *);
2208 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2209 static struct tlist *new_tlist (struct tlist *, tree, tree);
2211 /* Create a new struct tlist and fill in its fields. */
2212 static struct tlist *
2213 new_tlist (struct tlist *next, tree t, tree writer)
2216 l = XOBNEW (&tlist_obstack, struct tlist);
2223 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2224 is nonnull, we ignore any node we find which has a writer equal to it. */
2227 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2231 struct tlist *next = add->next;
2234 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2235 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2240 /* Merge the nodes of ADD into TO. This merging process is done so that for
2241 each variable that already exists in TO, no new node is added; however if
2242 there is a write access recorded in ADD, and an occurrence on TO is only
2243 a read access, then the occurrence in TO will be modified to record the
2247 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2249 struct tlist **end = to;
2252 end = &(*end)->next;
2258 struct tlist *next = add->next;
2260 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2261 if (candidate_equal_p (tmp2->expr, add->expr))
2265 tmp2->writer = add->writer;
2269 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2270 end = &(*end)->next;
2277 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2278 references in list LIST conflict with it, excluding reads if ONLY writers
2282 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2287 /* Avoid duplicate warnings. */
2288 for (tmp = warned_ids; tmp; tmp = tmp->next)
2289 if (candidate_equal_p (tmp->expr, written))
2294 if (candidate_equal_p (list->expr, written)
2295 && !candidate_equal_p (list->writer, writer)
2296 && (!only_writes || list->writer))
2298 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2299 warning_at (EXPR_LOC_OR_HERE (writer),
2300 OPT_Wsequence_point, "operation on %qE may be undefined",
2307 /* Given a list LIST of references to variables, find whether any of these
2308 can cause conflicts due to missing sequence points. */
2311 warn_for_collisions (struct tlist *list)
2315 for (tmp = list; tmp; tmp = tmp->next)
2318 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2322 /* Return nonzero if X is a tree that can be verified by the sequence point
2325 warning_candidate_p (tree x)
2327 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2330 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2331 (lvalue_p) crash on TRY/CATCH. */
2332 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2338 /* No point to track non-const calls, they will never satisfy
2340 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2343 if (TREE_CODE (x) == STRING_CST)
2349 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2351 candidate_equal_p (const_tree x, const_tree y)
2353 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2356 /* Walk the tree X, and record accesses to variables. If X is written by the
2357 parent tree, WRITER is the parent.
2358 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2359 expression or its only operand forces a sequence point, then everything up
2360 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2362 Once we return, we will have emitted warnings if any subexpression before
2363 such a sequence point could be undefined. On a higher level, however, the
2364 sequence point may not be relevant, and we'll merge the two lists.
2366 Example: (b++, a) + b;
2367 The call that processes the COMPOUND_EXPR will store the increment of B
2368 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2369 processes the PLUS_EXPR will need to merge the two lists so that
2370 eventually, all accesses end up on the same list (and we'll warn about the
2371 unordered subexpressions b++ and b.
2373 A note on merging. If we modify the former example so that our expression
2376 care must be taken not simply to add all three expressions into the final
2377 PNO_SP list. The function merge_tlist takes care of that by merging the
2378 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2379 way, so that no more than one access to B is recorded. */
2382 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2385 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2386 enum tree_code code;
2387 enum tree_code_class cl;
2389 /* X may be NULL if it is the operand of an empty statement expression
2395 code = TREE_CODE (x);
2396 cl = TREE_CODE_CLASS (code);
2398 if (warning_candidate_p (x))
2399 *pno_sp = new_tlist (*pno_sp, x, writer);
2407 case TRUTH_ANDIF_EXPR:
2408 case TRUTH_ORIF_EXPR:
2409 tmp_before = tmp_nosp = tmp_list3 = 0;
2410 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2411 warn_for_collisions (tmp_nosp);
2412 merge_tlist (pbefore_sp, tmp_before, 0);
2413 merge_tlist (pbefore_sp, tmp_nosp, 0);
2414 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2415 merge_tlist (pbefore_sp, tmp_list3, 0);
2419 tmp_before = tmp_list2 = 0;
2420 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2421 warn_for_collisions (tmp_list2);
2422 merge_tlist (pbefore_sp, tmp_before, 0);
2423 merge_tlist (pbefore_sp, tmp_list2, 1);
2425 tmp_list3 = tmp_nosp = 0;
2426 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2427 warn_for_collisions (tmp_nosp);
2428 merge_tlist (pbefore_sp, tmp_list3, 0);
2430 tmp_list3 = tmp_list2 = 0;
2431 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2432 warn_for_collisions (tmp_list2);
2433 merge_tlist (pbefore_sp, tmp_list3, 0);
2434 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2435 two first, to avoid warning for (a ? b++ : b++). */
2436 merge_tlist (&tmp_nosp, tmp_list2, 0);
2437 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2440 case PREDECREMENT_EXPR:
2441 case PREINCREMENT_EXPR:
2442 case POSTDECREMENT_EXPR:
2443 case POSTINCREMENT_EXPR:
2444 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2448 tmp_before = tmp_nosp = tmp_list3 = 0;
2449 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2450 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2451 /* Expressions inside the LHS are not ordered wrt. the sequence points
2452 in the RHS. Example:
2454 Despite the fact that the modification of "a" is in the before_sp
2455 list (tmp_before), it conflicts with the use of "a" in the LHS.
2456 We can handle this by adding the contents of tmp_list3
2457 to those of tmp_before, and redoing the collision warnings for that
2459 add_tlist (&tmp_before, tmp_list3, x, 1);
2460 warn_for_collisions (tmp_before);
2461 /* Exclude the LHS itself here; we first have to merge it into the
2462 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2463 didn't exclude the LHS, we'd get it twice, once as a read and once
2465 add_tlist (pno_sp, tmp_list3, x, 0);
2466 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2468 merge_tlist (pbefore_sp, tmp_before, 0);
2469 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2470 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2471 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2475 /* We need to warn about conflicts among arguments and conflicts between
2476 args and the function address. Side effects of the function address,
2477 however, are not ordered by the sequence point of the call. */
2479 call_expr_arg_iterator iter;
2481 tmp_before = tmp_nosp = 0;
2482 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2483 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2485 tmp_list2 = tmp_list3 = 0;
2486 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2487 merge_tlist (&tmp_list3, tmp_list2, 0);
2488 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2490 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2491 warn_for_collisions (tmp_before);
2492 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2497 /* Scan all the list, e.g. indices of multi dimensional array. */
2500 tmp_before = tmp_nosp = 0;
2501 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2502 merge_tlist (&tmp_nosp, tmp_before, 0);
2503 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2510 struct tlist_cache *t;
2511 for (t = save_expr_cache; t; t = t->next)
2512 if (candidate_equal_p (t->expr, x))
2517 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2518 t->next = save_expr_cache;
2520 save_expr_cache = t;
2522 tmp_before = tmp_nosp = 0;
2523 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2524 warn_for_collisions (tmp_nosp);
2529 struct tlist *t = tmp_nosp;
2531 merge_tlist (&tmp_list3, t, 0);
2533 t->cache_before_sp = tmp_before;
2534 t->cache_after_sp = tmp_list3;
2536 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2537 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2542 x = TREE_OPERAND (x, 0);
2549 /* For other expressions, simply recurse on their operands.
2550 Manual tail recursion for unary expressions.
2551 Other non-expressions need not be processed. */
2552 if (cl == tcc_unary)
2554 x = TREE_OPERAND (x, 0);
2558 else if (IS_EXPR_CODE_CLASS (cl))
2561 int max = TREE_OPERAND_LENGTH (x);
2562 for (lp = 0; lp < max; lp++)
2564 tmp_before = tmp_nosp = 0;
2565 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2566 merge_tlist (&tmp_nosp, tmp_before, 0);
2567 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2574 /* Try to warn for undefined behavior in EXPR due to missing sequence
2578 verify_sequence_points (tree expr)
2580 struct tlist *before_sp = 0, *after_sp = 0;
2583 save_expr_cache = 0;
2584 if (tlist_firstobj == 0)
2586 gcc_obstack_init (&tlist_obstack);
2587 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2590 verify_tree (expr, &before_sp, &after_sp, 0);
2591 warn_for_collisions (after_sp);
2592 obstack_free (&tlist_obstack, tlist_firstobj);
2595 /* Validate the expression after `case' and apply default promotions. */
2598 check_case_value (tree value)
2600 if (value == NULL_TREE)
2603 if (TREE_CODE (value) == INTEGER_CST)
2604 /* Promote char or short to int. */
2605 value = perform_integral_promotions (value);
2606 else if (value != error_mark_node)
2608 error ("case label does not reduce to an integer constant");
2609 value = error_mark_node;
2612 constant_expression_warning (value);
2617 /* See if the case values LOW and HIGH are in the range of the original
2618 type (i.e. before the default conversion to int) of the switch testing
2620 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2621 the type before promoting it. CASE_LOW_P is a pointer to the lower
2622 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2623 if the case is not a case range.
2624 The caller has to make sure that we are not called with NULL for
2625 CASE_LOW_P (i.e. the default case).
2626 Returns true if the case label is in range of ORIG_TYPE (saturated or
2627 untouched) or false if the label is out of range. */
2630 check_case_bounds (tree type, tree orig_type,
2631 tree *case_low_p, tree *case_high_p)
2633 tree min_value, max_value;
2634 tree case_low = *case_low_p;
2635 tree case_high = case_high_p ? *case_high_p : case_low;
2637 /* If there was a problem with the original type, do nothing. */
2638 if (orig_type == error_mark_node)
2641 min_value = TYPE_MIN_VALUE (orig_type);
2642 max_value = TYPE_MAX_VALUE (orig_type);
2644 /* Case label is less than minimum for type. */
2645 if (tree_int_cst_compare (case_low, min_value) < 0
2646 && tree_int_cst_compare (case_high, min_value) < 0)
2648 warning (0, "case label value is less than minimum value for type");
2652 /* Case value is greater than maximum for type. */
2653 if (tree_int_cst_compare (case_low, max_value) > 0
2654 && tree_int_cst_compare (case_high, max_value) > 0)
2656 warning (0, "case label value exceeds maximum value for type");
2660 /* Saturate lower case label value to minimum. */
2661 if (tree_int_cst_compare (case_high, min_value) >= 0
2662 && tree_int_cst_compare (case_low, min_value) < 0)
2664 warning (0, "lower value in case label range"
2665 " less than minimum value for type");
2666 case_low = min_value;
2669 /* Saturate upper case label value to maximum. */
2670 if (tree_int_cst_compare (case_low, max_value) <= 0
2671 && tree_int_cst_compare (case_high, max_value) > 0)
2673 warning (0, "upper value in case label range"
2674 " exceeds maximum value for type");
2675 case_high = max_value;
2678 if (*case_low_p != case_low)
2679 *case_low_p = convert (type, case_low);
2680 if (case_high_p && *case_high_p != case_high)
2681 *case_high_p = convert (type, case_high);
2686 /* Return an integer type with BITS bits of precision,
2687 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2690 c_common_type_for_size (unsigned int bits, int unsignedp)
2692 if (bits == TYPE_PRECISION (integer_type_node))
2693 return unsignedp ? unsigned_type_node : integer_type_node;
2695 if (bits == TYPE_PRECISION (signed_char_type_node))
2696 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2698 if (bits == TYPE_PRECISION (short_integer_type_node))
2699 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2701 if (bits == TYPE_PRECISION (long_integer_type_node))
2702 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2704 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2705 return (unsignedp ? long_long_unsigned_type_node
2706 : long_long_integer_type_node);
2708 if (int128_integer_type_node
2709 && bits == TYPE_PRECISION (int128_integer_type_node))
2710 return (unsignedp ? int128_unsigned_type_node
2711 : int128_integer_type_node);
2713 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2714 return (unsignedp ? widest_unsigned_literal_type_node
2715 : widest_integer_literal_type_node);
2717 if (bits <= TYPE_PRECISION (intQI_type_node))
2718 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2720 if (bits <= TYPE_PRECISION (intHI_type_node))
2721 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2723 if (bits <= TYPE_PRECISION (intSI_type_node))
2724 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2726 if (bits <= TYPE_PRECISION (intDI_type_node))
2727 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2732 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2733 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2734 and saturating if SATP is nonzero, otherwise not saturating. */
2737 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2738 int unsignedp, int satp)
2740 enum machine_mode mode;
2742 mode = unsignedp ? UQQmode : QQmode;
2744 mode = unsignedp ? UHAmode : HAmode;
2746 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2747 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2750 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2752 sorry ("GCC cannot support operators with integer types and "
2753 "fixed-point types that have too many integral and "
2754 "fractional bits together");
2758 return c_common_type_for_mode (mode, satp);
2761 /* Used for communication between c_common_type_for_mode and
2762 c_register_builtin_type. */
2763 static GTY(()) tree registered_builtin_types;
2765 /* Return a data type that has machine mode MODE.
2766 If the mode is an integer,
2767 then UNSIGNEDP selects between signed and unsigned types.
2768 If the mode is a fixed-point mode,
2769 then UNSIGNEDP selects between saturating and nonsaturating types. */
2772 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2776 if (mode == TYPE_MODE (integer_type_node))
2777 return unsignedp ? unsigned_type_node : integer_type_node;
2779 if (mode == TYPE_MODE (signed_char_type_node))
2780 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2782 if (mode == TYPE_MODE (short_integer_type_node))
2783 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2785 if (mode == TYPE_MODE (long_integer_type_node))
2786 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2788 if (mode == TYPE_MODE (long_long_integer_type_node))
2789 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2791 if (int128_integer_type_node
2792 && mode == TYPE_MODE (int128_integer_type_node))
2793 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2795 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2796 return unsignedp ? widest_unsigned_literal_type_node
2797 : widest_integer_literal_type_node;
2800 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2803 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2806 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2809 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2811 #if HOST_BITS_PER_WIDE_INT >= 64
2812 if (mode == TYPE_MODE (intTI_type_node))
2813 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2816 if (mode == TYPE_MODE (float_type_node))
2817 return float_type_node;
2819 if (mode == TYPE_MODE (double_type_node))
2820 return double_type_node;
2822 if (mode == TYPE_MODE (long_double_type_node))
2823 return long_double_type_node;
2825 if (mode == TYPE_MODE (void_type_node))
2826 return void_type_node;
2828 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2830 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2831 : make_signed_type (GET_MODE_PRECISION (mode)));
2833 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2835 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2836 : make_signed_type (GET_MODE_PRECISION (mode)));
2838 if (COMPLEX_MODE_P (mode))
2840 enum machine_mode inner_mode;
2843 if (mode == TYPE_MODE (complex_float_type_node))
2844 return complex_float_type_node;
2845 if (mode == TYPE_MODE (complex_double_type_node))
2846 return complex_double_type_node;
2847 if (mode == TYPE_MODE (complex_long_double_type_node))
2848 return complex_long_double_type_node;
2850 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2851 return complex_integer_type_node;
2853 inner_mode = GET_MODE_INNER (mode);
2854 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2855 if (inner_type != NULL_TREE)
2856 return build_complex_type (inner_type);
2858 else if (VECTOR_MODE_P (mode))
2860 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2861 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2862 if (inner_type != NULL_TREE)
2863 return build_vector_type_for_mode (inner_type, mode);
2866 if (mode == TYPE_MODE (dfloat32_type_node))
2867 return dfloat32_type_node;
2868 if (mode == TYPE_MODE (dfloat64_type_node))
2869 return dfloat64_type_node;
2870 if (mode == TYPE_MODE (dfloat128_type_node))
2871 return dfloat128_type_node;
2873 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2875 if (mode == TYPE_MODE (short_fract_type_node))
2876 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2877 if (mode == TYPE_MODE (fract_type_node))
2878 return unsignedp ? sat_fract_type_node : fract_type_node;
2879 if (mode == TYPE_MODE (long_fract_type_node))
2880 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2881 if (mode == TYPE_MODE (long_long_fract_type_node))
2882 return unsignedp ? sat_long_long_fract_type_node
2883 : long_long_fract_type_node;
2885 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2886 return unsignedp ? sat_unsigned_short_fract_type_node
2887 : unsigned_short_fract_type_node;
2888 if (mode == TYPE_MODE (unsigned_fract_type_node))
2889 return unsignedp ? sat_unsigned_fract_type_node
2890 : unsigned_fract_type_node;
2891 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2892 return unsignedp ? sat_unsigned_long_fract_type_node
2893 : unsigned_long_fract_type_node;
2894 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2895 return unsignedp ? sat_unsigned_long_long_fract_type_node
2896 : unsigned_long_long_fract_type_node;
2898 if (mode == TYPE_MODE (short_accum_type_node))
2899 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2900 if (mode == TYPE_MODE (accum_type_node))
2901 return unsignedp ? sat_accum_type_node : accum_type_node;
2902 if (mode == TYPE_MODE (long_accum_type_node))
2903 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2904 if (mode == TYPE_MODE (long_long_accum_type_node))
2905 return unsignedp ? sat_long_long_accum_type_node
2906 : long_long_accum_type_node;
2908 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2909 return unsignedp ? sat_unsigned_short_accum_type_node
2910 : unsigned_short_accum_type_node;
2911 if (mode == TYPE_MODE (unsigned_accum_type_node))
2912 return unsignedp ? sat_unsigned_accum_type_node
2913 : unsigned_accum_type_node;
2914 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2915 return unsignedp ? sat_unsigned_long_accum_type_node
2916 : unsigned_long_accum_type_node;
2917 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2918 return unsignedp ? sat_unsigned_long_long_accum_type_node
2919 : unsigned_long_long_accum_type_node;
2922 return unsignedp ? sat_qq_type_node : qq_type_node;
2924 return unsignedp ? sat_hq_type_node : hq_type_node;
2926 return unsignedp ? sat_sq_type_node : sq_type_node;
2928 return unsignedp ? sat_dq_type_node : dq_type_node;
2930 return unsignedp ? sat_tq_type_node : tq_type_node;
2932 if (mode == UQQmode)
2933 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2934 if (mode == UHQmode)
2935 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2936 if (mode == USQmode)
2937 return unsignedp ? sat_usq_type_node : usq_type_node;
2938 if (mode == UDQmode)
2939 return unsignedp ? sat_udq_type_node : udq_type_node;
2940 if (mode == UTQmode)
2941 return unsignedp ? sat_utq_type_node : utq_type_node;
2944 return unsignedp ? sat_ha_type_node : ha_type_node;
2946 return unsignedp ? sat_sa_type_node : sa_type_node;
2948 return unsignedp ? sat_da_type_node : da_type_node;
2950 return unsignedp ? sat_ta_type_node : ta_type_node;
2952 if (mode == UHAmode)
2953 return unsignedp ? sat_uha_type_node : uha_type_node;
2954 if (mode == USAmode)
2955 return unsignedp ? sat_usa_type_node : usa_type_node;
2956 if (mode == UDAmode)
2957 return unsignedp ? sat_uda_type_node : uda_type_node;
2958 if (mode == UTAmode)
2959 return unsignedp ? sat_uta_type_node : uta_type_node;
2962 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2963 if (TYPE_MODE (TREE_VALUE (t)) == mode)
2964 return TREE_VALUE (t);
2970 c_common_unsigned_type (tree type)
2972 return c_common_signed_or_unsigned_type (1, type);
2975 /* Return a signed type the same as TYPE in other respects. */
2978 c_common_signed_type (tree type)
2980 return c_common_signed_or_unsigned_type (0, type);
2983 /* Return a type the same as TYPE except unsigned or
2984 signed according to UNSIGNEDP. */
2987 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2991 /* This block of code emulates the behavior of the old
2992 c_common_unsigned_type. In particular, it returns
2993 long_unsigned_type_node if passed a long, even when a int would
2994 have the same size. This is necessary for warnings to work
2995 correctly in archs where sizeof(int) == sizeof(long) */
2997 type1 = TYPE_MAIN_VARIANT (type);
2998 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2999 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3000 if (type1 == integer_type_node || type1 == unsigned_type_node)
3001 return unsignedp ? unsigned_type_node : integer_type_node;
3002 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3003 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3004 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3005 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3006 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3007 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3008 if (int128_integer_type_node
3009 && (type1 == int128_integer_type_node
3010 || type1 == int128_unsigned_type_node))
3011 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3012 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3013 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3014 #if HOST_BITS_PER_WIDE_INT >= 64
3015 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3016 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3018 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3019 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3020 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3021 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3022 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3023 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3024 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3025 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3027 #define C_COMMON_FIXED_TYPES(NAME) \
3028 if (type1 == short_ ## NAME ## _type_node \
3029 || type1 == unsigned_short_ ## NAME ## _type_node) \
3030 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3031 : short_ ## NAME ## _type_node; \
3032 if (type1 == NAME ## _type_node \
3033 || type1 == unsigned_ ## NAME ## _type_node) \
3034 return unsignedp ? unsigned_ ## NAME ## _type_node \
3035 : NAME ## _type_node; \
3036 if (type1 == long_ ## NAME ## _type_node \
3037 || type1 == unsigned_long_ ## NAME ## _type_node) \
3038 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3039 : long_ ## NAME ## _type_node; \
3040 if (type1 == long_long_ ## NAME ## _type_node \
3041 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3042 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3043 : long_long_ ## NAME ## _type_node;
3045 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3046 if (type1 == NAME ## _type_node \
3047 || type1 == u ## NAME ## _type_node) \
3048 return unsignedp ? u ## NAME ## _type_node \
3049 : NAME ## _type_node;
3051 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3052 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3053 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3054 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3055 : sat_ ## short_ ## NAME ## _type_node; \
3056 if (type1 == sat_ ## NAME ## _type_node \
3057 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3058 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3059 : sat_ ## NAME ## _type_node; \
3060 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3061 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3062 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3063 : sat_ ## long_ ## NAME ## _type_node; \
3064 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3065 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3066 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3067 : sat_ ## long_long_ ## NAME ## _type_node;
3069 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3070 if (type1 == sat_ ## NAME ## _type_node \
3071 || type1 == sat_ ## u ## NAME ## _type_node) \
3072 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3073 : sat_ ## NAME ## _type_node;
3075 C_COMMON_FIXED_TYPES (fract);
3076 C_COMMON_FIXED_TYPES_SAT (fract);
3077 C_COMMON_FIXED_TYPES (accum);
3078 C_COMMON_FIXED_TYPES_SAT (accum);
3080 C_COMMON_FIXED_MODE_TYPES (qq);
3081 C_COMMON_FIXED_MODE_TYPES (hq);
3082 C_COMMON_FIXED_MODE_TYPES (sq);
3083 C_COMMON_FIXED_MODE_TYPES (dq);
3084 C_COMMON_FIXED_MODE_TYPES (tq);
3085 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3086 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3087 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3088 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3089 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3090 C_COMMON_FIXED_MODE_TYPES (ha);
3091 C_COMMON_FIXED_MODE_TYPES (sa);
3092 C_COMMON_FIXED_MODE_TYPES (da);
3093 C_COMMON_FIXED_MODE_TYPES (ta);
3094 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3095 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3096 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3097 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3099 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3100 the precision; they have precision set to match their range, but
3101 may use a wider mode to match an ABI. If we change modes, we may
3102 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3103 the precision as well, so as to yield correct results for
3104 bit-field types. C++ does not have these separate bit-field
3105 types, and producing a signed or unsigned variant of an
3106 ENUMERAL_TYPE may cause other problems as well. */
3108 if (!INTEGRAL_TYPE_P (type)
3109 || TYPE_UNSIGNED (type) == unsignedp)
3112 #define TYPE_OK(node) \
3113 (TYPE_MODE (type) == TYPE_MODE (node) \
3114 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3115 if (TYPE_OK (signed_char_type_node))
3116 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3117 if (TYPE_OK (integer_type_node))
3118 return unsignedp ? unsigned_type_node : integer_type_node;
3119 if (TYPE_OK (short_integer_type_node))
3120 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3121 if (TYPE_OK (long_integer_type_node))
3122 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3123 if (TYPE_OK (long_long_integer_type_node))
3124 return (unsignedp ? long_long_unsigned_type_node
3125 : long_long_integer_type_node);
3126 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3127 return (unsignedp ? int128_unsigned_type_node
3128 : int128_integer_type_node);
3129 if (TYPE_OK (widest_integer_literal_type_node))
3130 return (unsignedp ? widest_unsigned_literal_type_node
3131 : widest_integer_literal_type_node);
3133 #if HOST_BITS_PER_WIDE_INT >= 64
3134 if (TYPE_OK (intTI_type_node))
3135 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3137 if (TYPE_OK (intDI_type_node))
3138 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3139 if (TYPE_OK (intSI_type_node))
3140 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3141 if (TYPE_OK (intHI_type_node))
3142 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3143 if (TYPE_OK (intQI_type_node))
3144 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3147 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3150 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3153 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3155 /* Extended integer types of the same width as a standard type have
3156 lesser rank, so those of the same width as int promote to int or
3157 unsigned int and are valid for printf formats expecting int or
3158 unsigned int. To avoid such special cases, avoid creating
3159 extended integer types for bit-fields if a standard integer type
3161 if (width == TYPE_PRECISION (integer_type_node))
3162 return unsignedp ? unsigned_type_node : integer_type_node;
3163 if (width == TYPE_PRECISION (signed_char_type_node))
3164 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3165 if (width == TYPE_PRECISION (short_integer_type_node))
3166 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3167 if (width == TYPE_PRECISION (long_integer_type_node))
3168 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3169 if (width == TYPE_PRECISION (long_long_integer_type_node))
3170 return (unsignedp ? long_long_unsigned_type_node
3171 : long_long_integer_type_node);
3172 if (int128_integer_type_node
3173 && width == TYPE_PRECISION (int128_integer_type_node))
3174 return (unsignedp ? int128_unsigned_type_node
3175 : int128_integer_type_node);
3176 return build_nonstandard_integer_type (width, unsignedp);
3179 /* The C version of the register_builtin_type langhook. */
3182 c_register_builtin_type (tree type, const char* name)
3186 decl = build_decl (UNKNOWN_LOCATION,
3187 TYPE_DECL, get_identifier (name), type);
3188 DECL_ARTIFICIAL (decl) = 1;
3189 if (!TYPE_NAME (type))
3190 TYPE_NAME (type) = decl;
3193 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3196 /* Print an error message for invalid operands to arith operation
3197 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3198 LOCATION is the location of the message. */
3201 binary_op_error (location_t location, enum tree_code code,
3202 tree type0, tree type1)
3209 opname = "+"; break;
3211 opname = "-"; break;
3213 opname = "*"; break;
3215 opname = "max"; break;
3217 opname = "min"; break;
3219 opname = "=="; break;
3221 opname = "!="; break;
3223 opname = "<="; break;
3225 opname = ">="; break;
3227 opname = "<"; break;
3229 opname = ">"; break;
3231 opname = "<<"; break;
3233 opname = ">>"; break;
3234 case TRUNC_MOD_EXPR:
3235 case FLOOR_MOD_EXPR:
3236 opname = "%"; break;
3237 case TRUNC_DIV_EXPR:
3238 case FLOOR_DIV_EXPR:
3239 opname = "/"; break;
3241 opname = "&"; break;
3243 opname = "|"; break;
3244 case TRUTH_ANDIF_EXPR:
3245 opname = "&&"; break;
3246 case TRUTH_ORIF_EXPR:
3247 opname = "||"; break;
3249 opname = "^"; break;
3254 "invalid operands to binary %s (have %qT and %qT)", opname,
3258 /* Subroutine of build_binary_op, used for comparison operations.
3259 See if the operands have both been converted from subword integer types
3260 and, if so, perhaps change them both back to their original type.
3261 This function is also responsible for converting the two operands
3262 to the proper common type for comparison.
3264 The arguments of this function are all pointers to local variables
3265 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3266 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3268 If this function returns nonzero, it means that the comparison has
3269 a constant value. What this function returns is an expression for
3273 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3274 enum tree_code *rescode_ptr)
3277 tree op0 = *op0_ptr;
3278 tree op1 = *op1_ptr;
3279 int unsignedp0, unsignedp1;
3281 tree primop0, primop1;
3282 enum tree_code code = *rescode_ptr;
3284 /* Throw away any conversions to wider types
3285 already present in the operands. */
3287 primop0 = get_narrower (op0, &unsignedp0);
3288 primop1 = get_narrower (op1, &unsignedp1);
3290 /* Handle the case that OP0 does not *contain* a conversion
3291 but it *requires* conversion to FINAL_TYPE. */
3293 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3294 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3295 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3296 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3298 /* If one of the operands must be floated, we cannot optimize. */
3299 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3300 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3302 /* If first arg is constant, swap the args (changing operation
3303 so value is preserved), for canonicalization. Don't do this if
3304 the second arg is 0. */
3306 if (TREE_CONSTANT (primop0)
3307 && !integer_zerop (primop1) && !real_zerop (primop1)
3308 && !fixed_zerop (primop1))
3311 int temi = unsignedp0;
3319 unsignedp0 = unsignedp1;
3342 *rescode_ptr = code;
3345 /* If comparing an integer against a constant more bits wide,
3346 maybe we can deduce a value of 1 or 0 independent of the data.
3347 Or else truncate the constant now
3348 rather than extend the variable at run time.
3350 This is only interesting if the constant is the wider arg.
3351 Also, it is not safe if the constant is unsigned and the
3352 variable arg is signed, since in this case the variable
3353 would be sign-extended and then regarded as unsigned.
3354 Our technique fails in this case because the lowest/highest
3355 possible unsigned results don't follow naturally from the
3356 lowest/highest possible values of the variable operand.
3357 For just EQ_EXPR and NE_EXPR there is another technique that
3358 could be used: see if the constant can be faithfully represented
3359 in the other operand's type, by truncating it and reextending it
3360 and see if that preserves the constant's value. */
3362 if (!real1 && !real2
3363 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3364 && TREE_CODE (primop1) == INTEGER_CST
3365 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3367 int min_gt, max_gt, min_lt, max_lt;
3368 tree maxval, minval;
3369 /* 1 if comparison is nominally unsigned. */
3370 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3373 type = c_common_signed_or_unsigned_type (unsignedp0,
3374 TREE_TYPE (primop0));
3376 maxval = TYPE_MAX_VALUE (type);
3377 minval = TYPE_MIN_VALUE (type);
3379 if (unsignedp && !unsignedp0)
3380 *restype_ptr = c_common_signed_type (*restype_ptr);
3382 if (TREE_TYPE (primop1) != *restype_ptr)
3384 /* Convert primop1 to target type, but do not introduce
3385 additional overflow. We know primop1 is an int_cst. */
3386 primop1 = force_fit_type_double (*restype_ptr,
3387 tree_to_double_int (primop1),
3388 0, TREE_OVERFLOW (primop1));
3390 if (type != *restype_ptr)
3392 minval = convert (*restype_ptr, minval);
3393 maxval = convert (*restype_ptr, maxval);
3396 if (unsignedp && unsignedp0)
3398 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3399 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3400 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3401 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3405 min_gt = INT_CST_LT (primop1, minval);
3406 max_gt = INT_CST_LT (primop1, maxval);
3407 min_lt = INT_CST_LT (minval, primop1);
3408 max_lt = INT_CST_LT (maxval, primop1);
3412 /* This used to be a switch, but Genix compiler can't handle that. */
3413 if (code == NE_EXPR)
3415 if (max_lt || min_gt)
3416 val = truthvalue_true_node;
3418 else if (code == EQ_EXPR)
3420 if (max_lt || min_gt)
3421 val = truthvalue_false_node;
3423 else if (code == LT_EXPR)
3426 val = truthvalue_true_node;
3428 val = truthvalue_false_node;
3430 else if (code == GT_EXPR)
3433 val = truthvalue_true_node;
3435 val = truthvalue_false_node;
3437 else if (code == LE_EXPR)
3440 val = truthvalue_true_node;
3442 val = truthvalue_false_node;
3444 else if (code == GE_EXPR)
3447 val = truthvalue_true_node;
3449 val = truthvalue_false_node;
3452 /* If primop0 was sign-extended and unsigned comparison specd,
3453 we did a signed comparison above using the signed type bounds.
3454 But the comparison we output must be unsigned.
3456 Also, for inequalities, VAL is no good; but if the signed
3457 comparison had *any* fixed result, it follows that the
3458 unsigned comparison just tests the sign in reverse
3459 (positive values are LE, negative ones GE).
3460 So we can generate an unsigned comparison
3461 against an extreme value of the signed type. */
3463 if (unsignedp && !unsignedp0)
3470 primop1 = TYPE_MIN_VALUE (type);
3476 primop1 = TYPE_MAX_VALUE (type);
3483 type = c_common_unsigned_type (type);
3486 if (TREE_CODE (primop0) != INTEGER_CST)
3488 if (val == truthvalue_false_node)
3489 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3490 if (val == truthvalue_true_node)
3491 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3496 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3497 if (TREE_SIDE_EFFECTS (primop0))
3498 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3502 /* Value is not predetermined, but do the comparison
3503 in the type of the operand that is not constant.
3504 TYPE is already properly set. */
3507 /* If either arg is decimal float and the other is float, find the
3508 proper common type to use for comparison. */
3509 else if (real1 && real2
3510 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3511 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3512 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3514 else if (real1 && real2
3515 && (TYPE_PRECISION (TREE_TYPE (primop0))
3516 == TYPE_PRECISION (TREE_TYPE (primop1))))
3517 type = TREE_TYPE (primop0);
3519 /* If args' natural types are both narrower than nominal type
3520 and both extend in the same manner, compare them
3521 in the type of the wider arg.
3522 Otherwise must actually extend both to the nominal
3523 common type lest different ways of extending
3525 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3527 else if (unsignedp0 == unsignedp1 && real1 == real2
3528 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3529 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3531 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3532 type = c_common_signed_or_unsigned_type (unsignedp0
3533 || TYPE_UNSIGNED (*restype_ptr),
3535 /* Make sure shorter operand is extended the right way
3536 to match the longer operand. */
3538 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3539 TREE_TYPE (primop0)),
3542 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3543 TREE_TYPE (primop1)),
3548 /* Here we must do the comparison on the nominal type
3549 using the args exactly as we received them. */
3550 type = *restype_ptr;
3554 if (!real1 && !real2 && integer_zerop (primop1)
3555 && TYPE_UNSIGNED (*restype_ptr))
3561 /* All unsigned values are >= 0, so we warn. However,
3562 if OP0 is a constant that is >= 0, the signedness of
3563 the comparison isn't an issue, so suppress the
3565 if (warn_type_limits && !in_system_header
3566 && !(TREE_CODE (primop0) == INTEGER_CST
3567 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3569 warning (OPT_Wtype_limits,
3570 "comparison of unsigned expression >= 0 is always true");
3571 value = truthvalue_true_node;
3575 if (warn_type_limits && !in_system_header
3576 && !(TREE_CODE (primop0) == INTEGER_CST
3577 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3579 warning (OPT_Wtype_limits,
3580 "comparison of unsigned expression < 0 is always false");
3581 value = truthvalue_false_node;
3590 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3591 if (TREE_SIDE_EFFECTS (primop0))
3592 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3599 *op0_ptr = convert (type, primop0);
3600 *op1_ptr = convert (type, primop1);
3602 *restype_ptr = truthvalue_type_node;
3607 /* Return a tree for the sum or difference (RESULTCODE says which)
3608 of pointer PTROP and integer INTOP. */
3611 pointer_int_sum (location_t loc, enum tree_code resultcode,
3612 tree ptrop, tree intop)
3616 /* The result is a pointer of the same type that is being added. */
3617 tree result_type = TREE_TYPE (ptrop);
3619 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3621 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3622 "pointer of type %<void *%> used in arithmetic");
3623 size_exp = integer_one_node;
3625 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3627 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3628 "pointer to a function used in arithmetic");
3629 size_exp = integer_one_node;
3631 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3633 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3634 "pointer to member function used in arithmetic");
3635 size_exp = integer_one_node;
3638 size_exp = size_in_bytes (TREE_TYPE (result_type));
3640 /* We are manipulating pointer values, so we don't need to warn
3641 about relying on undefined signed overflow. We disable the
3642 warning here because we use integer types so fold won't know that
3643 they are really pointers. */
3644 fold_defer_overflow_warnings ();
3646 /* If what we are about to multiply by the size of the elements
3647 contains a constant term, apply distributive law
3648 and multiply that constant term separately.
3649 This helps produce common subexpressions. */
3650 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3651 && !TREE_CONSTANT (intop)
3652 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3653 && TREE_CONSTANT (size_exp)
3654 /* If the constant comes from pointer subtraction,
3655 skip this optimization--it would cause an error. */
3656 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3657 /* If the constant is unsigned, and smaller than the pointer size,
3658 then we must skip this optimization. This is because it could cause
3659 an overflow error if the constant is negative but INTOP is not. */
3660 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3661 || (TYPE_PRECISION (TREE_TYPE (intop))
3662 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3664 enum tree_code subcode = resultcode;
3665 tree int_type = TREE_TYPE (intop);
3666 if (TREE_CODE (intop) == MINUS_EXPR)
3667 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3668 /* Convert both subexpression types to the type of intop,
3669 because weird cases involving pointer arithmetic
3670 can result in a sum or difference with different type args. */
3671 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3673 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3674 intop = convert (int_type, TREE_OPERAND (intop, 0));
3677 /* Convert the integer argument to a type the same size as sizetype
3678 so the multiply won't overflow spuriously. */
3679 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3680 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3681 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3682 TYPE_UNSIGNED (sizetype)), intop);
3684 /* Replace the integer argument with a suitable product by the object size.
3685 Do this multiplication as signed, then convert to the appropriate type
3686 for the pointer operation and disregard an overflow that occured only
3687 because of the sign-extension change in the latter conversion. */
3689 tree t = build_binary_op (loc,
3691 convert (TREE_TYPE (intop), size_exp), 1);
3692 intop = convert (sizetype, t);
3693 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3694 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3695 TREE_INT_CST_HIGH (intop));
3698 /* Create the sum or difference. */
3699 if (resultcode == MINUS_EXPR)
3700 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3702 ret = fold_build2_loc (loc, POINTER_PLUS_EXPR, result_type, ptrop, intop);
3704 fold_undefer_and_ignore_overflow_warnings ();
3709 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3710 and if NON_CONST is known not to be permitted in an evaluated part
3711 of a constant expression. */
3714 c_wrap_maybe_const (tree expr, bool non_const)
3716 bool nowarning = TREE_NO_WARNING (expr);
3717 location_t loc = EXPR_LOCATION (expr);
3719 /* This should never be called for C++. */
3720 if (c_dialect_cxx ())
3723 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3724 STRIP_TYPE_NOPS (expr);
3725 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3726 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3728 TREE_NO_WARNING (expr) = 1;
3729 protected_set_expr_location (expr, loc);
3734 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3735 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3736 around the SAVE_EXPR if needed so that c_fully_fold does not need
3737 to look inside SAVE_EXPRs. */
3740 c_save_expr (tree expr)
3742 bool maybe_const = true;
3743 if (c_dialect_cxx ())
3744 return save_expr (expr);
3745 expr = c_fully_fold (expr, false, &maybe_const);
3746 expr = save_expr (expr);
3748 expr = c_wrap_maybe_const (expr, true);
3752 /* Return whether EXPR is a declaration whose address can never be
3756 decl_with_nonnull_addr_p (const_tree expr)
3758 return (DECL_P (expr)
3759 && (TREE_CODE (expr) == PARM_DECL
3760 || TREE_CODE (expr) == LABEL_DECL
3761 || !DECL_WEAK (expr)));
3764 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3765 or for an `if' or `while' statement or ?..: exp. It should already
3766 have been validated to be of suitable type; otherwise, a bad
3767 diagnostic may result.
3769 The EXPR is located at LOCATION.
3771 This preparation consists of taking the ordinary
3772 representation of an expression expr and producing a valid tree
3773 boolean expression describing whether expr is nonzero. We could
3774 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3775 but we optimize comparisons, &&, ||, and !.
3777 The resulting type should always be `truthvalue_type_node'. */
3780 c_common_truthvalue_conversion (location_t location, tree expr)
3782 switch (TREE_CODE (expr))
3784 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3785 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3786 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3787 case ORDERED_EXPR: case UNORDERED_EXPR:
3788 if (TREE_TYPE (expr) == truthvalue_type_node)
3790 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3791 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3794 case TRUTH_ANDIF_EXPR:
3795 case TRUTH_ORIF_EXPR:
3796 case TRUTH_AND_EXPR:
3798 case TRUTH_XOR_EXPR:
3799 if (TREE_TYPE (expr) == truthvalue_type_node)
3801 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3802 c_common_truthvalue_conversion (location,
3803 TREE_OPERAND (expr, 0)),
3804 c_common_truthvalue_conversion (location,
3805 TREE_OPERAND (expr, 1)));
3808 case TRUTH_NOT_EXPR:
3809 if (TREE_TYPE (expr) == truthvalue_type_node)
3811 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3812 c_common_truthvalue_conversion (location,
3813 TREE_OPERAND (expr, 0)));
3820 return integer_zerop (expr) ? truthvalue_false_node
3821 : truthvalue_true_node;
3824 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3825 ? truthvalue_true_node
3826 : truthvalue_false_node;
3829 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3830 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3831 ? truthvalue_true_node
3832 : truthvalue_false_node;
3835 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3840 tree inner = TREE_OPERAND (expr, 0);
3841 if (decl_with_nonnull_addr_p (inner))
3843 /* Common Ada/Pascal programmer's mistake. */
3844 warning_at (location,
3846 "the address of %qD will always evaluate as %<true%>",
3848 return truthvalue_true_node;
3854 expr = build_binary_op (EXPR_LOCATION (expr),
3855 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3856 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3857 c_common_truthvalue_conversion (location,
3858 TREE_OPERAND (expr, 0)),
3859 c_common_truthvalue_conversion (location,
3860 TREE_OPERAND (expr, 1)),