1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
24 #include "coretypes.h"
30 #include "gimple-expr.h"
32 #include "stringpool.h"
34 #include "diagnostic.h"
36 #include "stor-layout.h"
40 #include "trans-mem.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 #include "substring-locations.h"
50 #include "spellcheck.h"
51 #include "c-spellcheck.h"
54 cpp_reader
*parse_in
; /* Declared in c-pragma.h. */
56 /* Mode used to build pointers (VOIDmode means ptr_mode). */
58 machine_mode c_default_pointer_mode
= VOIDmode
;
60 /* The following symbols are subsumed in the c_global_trees array, and
61 listed here individually for documentation purposes.
63 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
65 tree short_integer_type_node;
66 tree long_integer_type_node;
67 tree long_long_integer_type_node;
69 tree short_unsigned_type_node;
70 tree long_unsigned_type_node;
71 tree long_long_unsigned_type_node;
73 tree truthvalue_type_node;
74 tree truthvalue_false_node;
75 tree truthvalue_true_node;
77 tree ptrdiff_type_node;
79 tree unsigned_char_type_node;
80 tree signed_char_type_node;
84 tree char16_type_node;
85 tree char32_type_node;
88 tree double_type_node;
89 tree long_double_type_node;
91 tree complex_integer_type_node;
92 tree complex_float_type_node;
93 tree complex_double_type_node;
94 tree complex_long_double_type_node;
96 tree dfloat32_type_node;
97 tree dfloat64_type_node;
98 tree_dfloat128_type_node;
100 tree intQI_type_node;
101 tree intHI_type_node;
102 tree intSI_type_node;
103 tree intDI_type_node;
104 tree intTI_type_node;
106 tree unsigned_intQI_type_node;
107 tree unsigned_intHI_type_node;
108 tree unsigned_intSI_type_node;
109 tree unsigned_intDI_type_node;
110 tree unsigned_intTI_type_node;
112 tree widest_integer_literal_type_node;
113 tree widest_unsigned_literal_type_node;
115 Nodes for types `void *' and `const void *'.
117 tree ptr_type_node, const_ptr_type_node;
119 Nodes for types `char *' and `const char *'.
121 tree string_type_node, const_string_type_node;
123 Type `char[SOMENUMBER]'.
124 Used when an array of char is needed and the size is irrelevant.
126 tree char_array_type_node;
128 Type `wchar_t[SOMENUMBER]' or something like it.
129 Used when a wide string literal is created.
131 tree wchar_array_type_node;
133 Type `char8_t[SOMENUMBER]' or something like it.
134 Used when a UTF-8 string literal is created.
136 tree char8_array_type_node;
138 Type `char16_t[SOMENUMBER]' or something like it.
139 Used when a UTF-16 string literal is created.
141 tree char16_array_type_node;
143 Type `char32_t[SOMENUMBER]' or something like it.
144 Used when a UTF-32 string literal is created.
146 tree char32_array_type_node;
148 Type `int ()' -- used for implicit declaration of functions.
150 tree default_function_type;
152 A VOID_TYPE node, packaged in a TREE_LIST.
156 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
157 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
158 VAR_DECLS, but C++ does.)
160 tree function_name_decl_node;
161 tree pretty_function_name_decl_node;
162 tree c99_function_name_decl_node;
164 Stack of nested function name VAR_DECLs.
166 tree saved_function_name_decls;
170 tree c_global_trees
[CTI_MAX
];
172 /* Switches common to the C front ends. */
174 /* Nonzero means don't output line number information. */
176 char flag_no_line_commands
;
178 /* Nonzero causes -E output not to be done, but directives such as
179 #define that have side effects are still obeyed. */
183 /* Nonzero means dump macros in some fashion. */
185 char flag_dump_macros
;
187 /* Nonzero means pass #include lines through to the output. */
189 char flag_dump_includes
;
191 /* Nonzero means process PCH files while preprocessing. */
193 bool flag_pch_preprocess
;
195 /* The file name to which we should write a precompiled header, or
196 NULL if no header will be written in this compile. */
198 const char *pch_file
;
200 /* Nonzero if an ISO standard was selected. It rejects macros in the
204 /* C/ObjC language option variables. */
207 /* Nonzero means allow type mismatches in conditional expressions;
208 just make their values `void'. */
210 int flag_cond_mismatch
;
212 /* Nonzero means enable C89 Amendment 1 features. */
216 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
220 /* Nonzero means use the ISO C11 dialect of C. */
224 /* Nonzero means use the ISO C2X dialect of C. */
228 /* Nonzero means that we have builtin functions, and main is an int. */
233 /* ObjC language option variables. */
236 /* Tells the compiler that this is a special run. Do not perform any
237 compiling, instead we are to test some platform dependent features
238 and output a C header file with appropriate definitions. */
240 int print_struct_values
;
242 /* Tells the compiler what is the constant string class for ObjC. */
244 const char *constant_string_class_name
;
247 /* C++ language option variables. */
249 /* The reference version of the ABI for -Wabi. */
251 int warn_abi_version
= -1;
253 /* The C++ dialect being used. Default set in c_common_post_options. */
255 enum cxx_dialect cxx_dialect
= cxx_unset
;
257 /* Maximum template instantiation depth. This limit exists to limit the
258 time it takes to notice excessively recursive template instantiations.
260 The default is lower than the 1024 recommended by the C++0x standard
261 because G++ runs out of stack before 1024 with highly recursive template
262 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
264 int max_tinst_depth
= 900;
266 /* The elements of `ridpointers' are identifier nodes for the reserved
267 type names and storage classes. It is indexed by a RID_... value. */
270 tree (*make_fname_decl
) (location_t
, tree
, int);
272 /* Nonzero means don't warn about problems that occur when the code is
274 int c_inhibit_evaluation_warnings
;
276 /* Whether we are building a boolean conversion inside
277 convert_for_assignment, or some other late binary operation. If
278 build_binary_op is called for C (from code shared by C and C++) in
279 this case, then the operands have already been folded and the
280 result will not be folded again, so C_MAYBE_CONST_EXPR should not
282 bool in_late_binary_op
;
284 /* Whether lexing has been completed, so subsequent preprocessor
285 errors should use the compiler's input_location. */
286 bool done_lexing
= false;
288 /* Information about how a function name is generated. */
291 tree
*const decl
; /* pointer to the VAR_DECL. */
292 const unsigned rid
; /* RID number for the identifier. */
293 const int pretty
; /* How pretty is it? */
296 /* The three ways of getting then name of the current function. */
298 const struct fname_var_t fname_vars
[] =
300 /* C99 compliant __func__, must be first. */
301 {&c99_function_name_decl_node
, RID_C99_FUNCTION_NAME
, 0},
302 /* GCC __FUNCTION__ compliant. */
303 {&function_name_decl_node
, RID_FUNCTION_NAME
, 0},
304 /* GCC __PRETTY_FUNCTION__ compliant. */
305 {&pretty_function_name_decl_node
, RID_PRETTY_FUNCTION_NAME
, 1},
309 /* Global visibility options. */
310 struct visibility_flags visibility_options
;
312 static tree
check_case_value (location_t
, tree
);
315 static void check_nonnull_arg (void *, tree
, unsigned HOST_WIDE_INT
);
316 static bool nonnull_check_p (tree
, unsigned HOST_WIDE_INT
);
318 /* Reserved words. The third field is a mask: keywords are disabled
319 if they match the mask.
322 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
323 C --std=c99: D_CXXONLY | D_OBJC
324 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
325 C++ --std=c++98: D_CONLY | D_CXX11 | D_CXX20 | D_OBJC
326 C++ --std=c++11: D_CONLY | D_CXX20 | D_OBJC
327 C++ --std=c++2a: D_CONLY | D_OBJC
328 ObjC++ is like C++ except that D_OBJC is not set
330 If -fno-asm is used, D_ASM is added to the mask. If
331 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
332 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
333 In C with -Wc++-compat, we warn if D_CXXWARN is set.
335 Note the complication of the D_CXX_OBJC keywords. These are
336 reserved words such as 'class'. In C++, 'class' is a reserved
337 word. In Objective-C++ it is too. In Objective-C, it is a
338 reserved word too, but only if it follows an '@' sign.
340 const struct c_common_resword c_common_reswords
[] =
342 { "_Alignas", RID_ALIGNAS
, D_CONLY
},
343 { "_Alignof", RID_ALIGNOF
, D_CONLY
},
344 { "_Atomic", RID_ATOMIC
, D_CONLY
},
345 { "_Bool", RID_BOOL
, D_CONLY
},
346 { "_Complex", RID_COMPLEX
, 0 },
347 { "_Imaginary", RID_IMAGINARY
, D_CONLY
},
348 { "_Float16", RID_FLOAT16
, D_CONLY
},
349 { "_Float32", RID_FLOAT32
, D_CONLY
},
350 { "_Float64", RID_FLOAT64
, D_CONLY
},
351 { "_Float128", RID_FLOAT128
, D_CONLY
},
352 { "_Float32x", RID_FLOAT32X
, D_CONLY
},
353 { "_Float64x", RID_FLOAT64X
, D_CONLY
},
354 { "_Float128x", RID_FLOAT128X
, D_CONLY
},
355 { "_Decimal32", RID_DFLOAT32
, D_CONLY
},
356 { "_Decimal64", RID_DFLOAT64
, D_CONLY
},
357 { "_Decimal128", RID_DFLOAT128
, D_CONLY
},
358 { "_Fract", RID_FRACT
, D_CONLY
| D_EXT
},
359 { "_Accum", RID_ACCUM
, D_CONLY
| D_EXT
},
360 { "_Sat", RID_SAT
, D_CONLY
| D_EXT
},
361 { "_Static_assert", RID_STATIC_ASSERT
, D_CONLY
},
362 { "_Noreturn", RID_NORETURN
, D_CONLY
},
363 { "_Generic", RID_GENERIC
, D_CONLY
},
364 { "_Thread_local", RID_THREAD
, D_CONLY
},
365 { "__FUNCTION__", RID_FUNCTION_NAME
, 0 },
366 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME
, 0 },
367 { "__alignof", RID_ALIGNOF
, 0 },
368 { "__alignof__", RID_ALIGNOF
, 0 },
369 { "__asm", RID_ASM
, 0 },
370 { "__asm__", RID_ASM
, 0 },
371 { "__attribute", RID_ATTRIBUTE
, 0 },
372 { "__attribute__", RID_ATTRIBUTE
, 0 },
373 { "__auto_type", RID_AUTO_TYPE
, D_CONLY
},
374 { "__bases", RID_BASES
, D_CXXONLY
},
375 { "__builtin_addressof", RID_ADDRESSOF
, D_CXXONLY
},
376 { "__builtin_call_with_static_chain",
377 RID_BUILTIN_CALL_WITH_STATIC_CHAIN
, D_CONLY
},
378 { "__builtin_choose_expr", RID_CHOOSE_EXPR
, D_CONLY
},
379 { "__builtin_complex", RID_BUILTIN_COMPLEX
, D_CONLY
},
380 { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR
, 0 },
381 { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE
, 0 },
382 { "__builtin_launder", RID_BUILTIN_LAUNDER
, D_CXXONLY
},
383 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE
, 0 },
384 { "__builtin_tgmath", RID_BUILTIN_TGMATH
, D_CONLY
},
385 { "__builtin_offsetof", RID_OFFSETOF
, 0 },
386 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P
, D_CONLY
},
387 { "__builtin_va_arg", RID_VA_ARG
, 0 },
388 { "__complex", RID_COMPLEX
, 0 },
389 { "__complex__", RID_COMPLEX
, 0 },
390 { "__const", RID_CONST
, 0 },
391 { "__const__", RID_CONST
, 0 },
392 { "__constinit", RID_CONSTINIT
, D_CXXONLY
},
393 { "__decltype", RID_DECLTYPE
, D_CXXONLY
},
394 { "__direct_bases", RID_DIRECT_BASES
, D_CXXONLY
},
395 { "__extension__", RID_EXTENSION
, 0 },
396 { "__func__", RID_C99_FUNCTION_NAME
, 0 },
397 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN
, D_CXXONLY
},
398 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR
, D_CXXONLY
},
399 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY
, D_CXXONLY
},
400 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN
, D_CXXONLY
},
401 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR
, D_CXXONLY
},
402 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY
, D_CXXONLY
},
403 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR
, D_CXXONLY
},
404 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
,
406 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR
, D_CXXONLY
},
407 { "__imag", RID_IMAGPART
, 0 },
408 { "__imag__", RID_IMAGPART
, 0 },
409 { "__inline", RID_INLINE
, 0 },
410 { "__inline__", RID_INLINE
, 0 },
411 { "__is_abstract", RID_IS_ABSTRACT
, D_CXXONLY
},
412 { "__is_aggregate", RID_IS_AGGREGATE
, D_CXXONLY
},
413 { "__is_base_of", RID_IS_BASE_OF
, D_CXXONLY
},
414 { "__is_class", RID_IS_CLASS
, D_CXXONLY
},
415 { "__is_empty", RID_IS_EMPTY
, D_CXXONLY
},
416 { "__is_enum", RID_IS_ENUM
, D_CXXONLY
},
417 { "__is_final", RID_IS_FINAL
, D_CXXONLY
},
418 { "__is_literal_type", RID_IS_LITERAL_TYPE
, D_CXXONLY
},
419 { "__is_pod", RID_IS_POD
, D_CXXONLY
},
420 { "__is_polymorphic", RID_IS_POLYMORPHIC
, D_CXXONLY
},
421 { "__is_same", RID_IS_SAME_AS
, D_CXXONLY
},
422 { "__is_same_as", RID_IS_SAME_AS
, D_CXXONLY
},
423 { "__is_standard_layout", RID_IS_STD_LAYOUT
, D_CXXONLY
},
424 { "__is_trivial", RID_IS_TRIVIAL
, D_CXXONLY
},
425 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE
, D_CXXONLY
},
426 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE
, D_CXXONLY
},
427 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE
, D_CXXONLY
},
428 { "__is_union", RID_IS_UNION
, D_CXXONLY
},
429 { "__label__", RID_LABEL
, 0 },
430 { "__null", RID_NULL
, 0 },
431 { "__real", RID_REALPART
, 0 },
432 { "__real__", RID_REALPART
, 0 },
433 { "__restrict", RID_RESTRICT
, 0 },
434 { "__restrict__", RID_RESTRICT
, 0 },
435 { "__signed", RID_SIGNED
, 0 },
436 { "__signed__", RID_SIGNED
, 0 },
437 { "__thread", RID_THREAD
, 0 },
438 { "__transaction_atomic", RID_TRANSACTION_ATOMIC
, 0 },
439 { "__transaction_relaxed", RID_TRANSACTION_RELAXED
, 0 },
440 { "__transaction_cancel", RID_TRANSACTION_CANCEL
, 0 },
441 { "__typeof", RID_TYPEOF
, 0 },
442 { "__typeof__", RID_TYPEOF
, 0 },
443 { "__underlying_type", RID_UNDERLYING_TYPE
, D_CXXONLY
},
444 { "__volatile", RID_VOLATILE
, 0 },
445 { "__volatile__", RID_VOLATILE
, 0 },
446 { "__GIMPLE", RID_GIMPLE
, D_CONLY
},
447 { "__PHI", RID_PHI
, D_CONLY
},
448 { "__RTL", RID_RTL
, D_CONLY
},
449 { "alignas", RID_ALIGNAS
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
450 { "alignof", RID_ALIGNOF
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
451 { "asm", RID_ASM
, D_ASM
},
452 { "auto", RID_AUTO
, 0 },
453 { "bool", RID_BOOL
, D_CXXONLY
| D_CXXWARN
},
454 { "break", RID_BREAK
, 0 },
455 { "case", RID_CASE
, 0 },
456 { "catch", RID_CATCH
, D_CXX_OBJC
| D_CXXWARN
},
457 { "char", RID_CHAR
, 0 },
458 { "char8_t", RID_CHAR8
, D_CXX_CHAR8_T_FLAGS
| D_CXXWARN
},
459 { "char16_t", RID_CHAR16
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
460 { "char32_t", RID_CHAR32
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
461 { "class", RID_CLASS
, D_CXX_OBJC
| D_CXXWARN
},
462 { "const", RID_CONST
, 0 },
463 { "consteval", RID_CONSTEVAL
, D_CXXONLY
| D_CXX20
| D_CXXWARN
},
464 { "constexpr", RID_CONSTEXPR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
465 { "constinit", RID_CONSTINIT
, D_CXXONLY
| D_CXX20
| D_CXXWARN
},
466 { "const_cast", RID_CONSTCAST
, D_CXXONLY
| D_CXXWARN
},
467 { "continue", RID_CONTINUE
, 0 },
468 { "decltype", RID_DECLTYPE
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
469 { "default", RID_DEFAULT
, 0 },
470 { "delete", RID_DELETE
, D_CXXONLY
| D_CXXWARN
},
472 { "double", RID_DOUBLE
, 0 },
473 { "dynamic_cast", RID_DYNCAST
, D_CXXONLY
| D_CXXWARN
},
474 { "else", RID_ELSE
, 0 },
475 { "enum", RID_ENUM
, 0 },
476 { "explicit", RID_EXPLICIT
, D_CXXONLY
| D_CXXWARN
},
477 { "export", RID_EXPORT
, D_CXXONLY
| D_CXXWARN
},
478 { "extern", RID_EXTERN
, 0 },
479 { "false", RID_FALSE
, D_CXXONLY
| D_CXXWARN
},
480 { "float", RID_FLOAT
, 0 },
481 { "for", RID_FOR
, 0 },
482 { "friend", RID_FRIEND
, D_CXXONLY
| D_CXXWARN
},
483 { "goto", RID_GOTO
, 0 },
485 { "inline", RID_INLINE
, D_EXT89
},
486 { "int", RID_INT
, 0 },
487 { "long", RID_LONG
, 0 },
488 { "mutable", RID_MUTABLE
, D_CXXONLY
| D_CXXWARN
},
489 { "namespace", RID_NAMESPACE
, D_CXXONLY
| D_CXXWARN
},
490 { "new", RID_NEW
, D_CXXONLY
| D_CXXWARN
},
491 { "noexcept", RID_NOEXCEPT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
492 { "nullptr", RID_NULLPTR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
493 { "operator", RID_OPERATOR
, D_CXXONLY
| D_CXXWARN
},
494 { "private", RID_PRIVATE
, D_CXX_OBJC
| D_CXXWARN
},
495 { "protected", RID_PROTECTED
, D_CXX_OBJC
| D_CXXWARN
},
496 { "public", RID_PUBLIC
, D_CXX_OBJC
| D_CXXWARN
},
497 { "register", RID_REGISTER
, 0 },
498 { "reinterpret_cast", RID_REINTCAST
, D_CXXONLY
| D_CXXWARN
},
499 { "restrict", RID_RESTRICT
, D_CONLY
| D_C99
},
500 { "return", RID_RETURN
, 0 },
501 { "short", RID_SHORT
, 0 },
502 { "signed", RID_SIGNED
, 0 },
503 { "sizeof", RID_SIZEOF
, 0 },
504 { "static", RID_STATIC
, 0 },
505 { "static_assert", RID_STATIC_ASSERT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
506 { "static_cast", RID_STATCAST
, D_CXXONLY
| D_CXXWARN
},
507 { "struct", RID_STRUCT
, 0 },
508 { "switch", RID_SWITCH
, 0 },
509 { "template", RID_TEMPLATE
, D_CXXONLY
| D_CXXWARN
},
510 { "this", RID_THIS
, D_CXXONLY
| D_CXXWARN
},
511 { "thread_local", RID_THREAD
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
512 { "throw", RID_THROW
, D_CXX_OBJC
| D_CXXWARN
},
513 { "true", RID_TRUE
, D_CXXONLY
| D_CXXWARN
},
514 { "try", RID_TRY
, D_CXX_OBJC
| D_CXXWARN
},
515 { "typedef", RID_TYPEDEF
, 0 },
516 { "typename", RID_TYPENAME
, D_CXXONLY
| D_CXXWARN
},
517 { "typeid", RID_TYPEID
, D_CXXONLY
| D_CXXWARN
},
518 { "typeof", RID_TYPEOF
, D_ASM
| D_EXT
},
519 { "union", RID_UNION
, 0 },
520 { "unsigned", RID_UNSIGNED
, 0 },
521 { "using", RID_USING
, D_CXXONLY
| D_CXXWARN
},
522 { "virtual", RID_VIRTUAL
, D_CXXONLY
| D_CXXWARN
},
523 { "void", RID_VOID
, 0 },
524 { "volatile", RID_VOLATILE
, 0 },
525 { "wchar_t", RID_WCHAR
, D_CXXONLY
},
526 { "while", RID_WHILE
, 0 },
527 { "__is_assignable", RID_IS_ASSIGNABLE
, D_CXXONLY
},
528 { "__is_constructible", RID_IS_CONSTRUCTIBLE
, D_CXXONLY
},
530 /* C++ transactional memory. */
531 { "synchronized", RID_SYNCHRONIZED
, D_CXX_OBJC
| D_TRANSMEM
},
532 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT
, D_CXXONLY
| D_TRANSMEM
},
533 { "atomic_cancel", RID_ATOMIC_CANCEL
, D_CXXONLY
| D_TRANSMEM
},
534 { "atomic_commit", RID_TRANSACTION_ATOMIC
, D_CXXONLY
| D_TRANSMEM
},
536 /* Concepts-related keywords */
537 { "concept", RID_CONCEPT
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
538 { "requires", RID_REQUIRES
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
540 /* These Objective-C keywords are recognized only immediately after
542 { "compatibility_alias", RID_AT_ALIAS
, D_OBJC
},
543 { "defs", RID_AT_DEFS
, D_OBJC
},
544 { "encode", RID_AT_ENCODE
, D_OBJC
},
545 { "end", RID_AT_END
, D_OBJC
},
546 { "implementation", RID_AT_IMPLEMENTATION
, D_OBJC
},
547 { "interface", RID_AT_INTERFACE
, D_OBJC
},
548 { "protocol", RID_AT_PROTOCOL
, D_OBJC
},
549 { "selector", RID_AT_SELECTOR
, D_OBJC
},
550 { "finally", RID_AT_FINALLY
, D_OBJC
},
551 { "optional", RID_AT_OPTIONAL
, D_OBJC
},
552 { "required", RID_AT_REQUIRED
, D_OBJC
},
553 { "property", RID_AT_PROPERTY
, D_OBJC
},
554 { "package", RID_AT_PACKAGE
, D_OBJC
},
555 { "synthesize", RID_AT_SYNTHESIZE
, D_OBJC
},
556 { "dynamic", RID_AT_DYNAMIC
, D_OBJC
},
557 /* These are recognized only in protocol-qualifier context
559 { "bycopy", RID_BYCOPY
, D_OBJC
},
560 { "byref", RID_BYREF
, D_OBJC
},
561 { "in", RID_IN
, D_OBJC
},
562 { "inout", RID_INOUT
, D_OBJC
},
563 { "oneway", RID_ONEWAY
, D_OBJC
},
564 { "out", RID_OUT
, D_OBJC
},
565 /* These are recognized inside a property attribute list */
566 { "assign", RID_ASSIGN
, D_OBJC
},
567 { "copy", RID_COPY
, D_OBJC
},
568 { "getter", RID_GETTER
, D_OBJC
},
569 { "nonatomic", RID_NONATOMIC
, D_OBJC
},
570 { "readonly", RID_READONLY
, D_OBJC
},
571 { "readwrite", RID_READWRITE
, D_OBJC
},
572 { "retain", RID_RETAIN
, D_OBJC
},
573 { "setter", RID_SETTER
, D_OBJC
},
576 const unsigned int num_c_common_reswords
=
577 sizeof c_common_reswords
/ sizeof (struct c_common_resword
);
579 /* Return identifier for address space AS. */
582 c_addr_space_name (addr_space_t as
)
584 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
585 gcc_assert (ridpointers
[rid
]);
586 return IDENTIFIER_POINTER (ridpointers
[rid
]);
589 /* Push current bindings for the function name VAR_DECLS. */
592 start_fname_decls (void)
595 tree saved
= NULL_TREE
;
597 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
599 tree decl
= *fname_vars
[ix
].decl
;
603 saved
= tree_cons (decl
, build_int_cst (integer_type_node
, ix
),
605 *fname_vars
[ix
].decl
= NULL_TREE
;
608 if (saved
|| saved_function_name_decls
)
609 /* Normally they'll have been NULL, so only push if we've got a
610 stack, or they are non-NULL. */
611 saved_function_name_decls
= tree_cons (saved
, NULL_TREE
,
612 saved_function_name_decls
);
615 /* Finish up the current bindings, adding them into the current function's
616 statement tree. This must be done _before_ finish_stmt_tree is called.
617 If there is no current function, we must be at file scope and no statements
618 are involved. Pop the previous bindings. */
621 finish_fname_decls (void)
624 tree stmts
= NULL_TREE
;
625 tree stack
= saved_function_name_decls
;
627 for (; stack
&& TREE_VALUE (stack
); stack
= TREE_CHAIN (stack
))
628 append_to_statement_list (TREE_VALUE (stack
), &stmts
);
632 tree
*bodyp
= &DECL_SAVED_TREE (current_function_decl
);
634 if (TREE_CODE (*bodyp
) == BIND_EXPR
)
635 bodyp
= &BIND_EXPR_BODY (*bodyp
);
637 append_to_statement_list_force (*bodyp
, &stmts
);
641 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
642 *fname_vars
[ix
].decl
= NULL_TREE
;
646 /* We had saved values, restore them. */
649 for (saved
= TREE_PURPOSE (stack
); saved
; saved
= TREE_CHAIN (saved
))
651 tree decl
= TREE_PURPOSE (saved
);
652 unsigned ix
= TREE_INT_CST_LOW (TREE_VALUE (saved
));
654 *fname_vars
[ix
].decl
= decl
;
656 stack
= TREE_CHAIN (stack
);
658 saved_function_name_decls
= stack
;
661 /* Return the text name of the current function, suitably prettified
662 by PRETTY_P. Return string must be freed by caller. */
665 fname_as_string (int pretty_p
)
667 const char *name
= "top level";
670 cpp_string cstr
= { 0, 0 }, strname
;
678 if (current_function_decl
)
679 name
= lang_hooks
.decl_printable_name (current_function_decl
, vrb
);
681 len
= strlen (name
) + 3; /* Two for '"'s. One for NULL. */
683 namep
= XNEWVEC (char, len
);
684 snprintf (namep
, len
, "\"%s\"", name
);
685 strname
.text
= (unsigned char *) namep
;
686 strname
.len
= len
- 1;
688 if (cpp_interpret_string (parse_in
, &strname
, 1, &cstr
, CPP_STRING
))
691 return (const char *) cstr
.text
;
697 /* Return the VAR_DECL for a const char array naming the current
698 function. If the VAR_DECL has not yet been created, create it
699 now. RID indicates how it should be formatted and IDENTIFIER_NODE
700 ID is its name (unfortunately C and C++ hold the RID values of
701 keywords in different places, so we can't derive RID from ID in
702 this language independent code. LOC is the location of the
706 fname_decl (location_t loc
, unsigned int rid
, tree id
)
709 tree decl
= NULL_TREE
;
711 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
712 if (fname_vars
[ix
].rid
== rid
)
715 decl
= *fname_vars
[ix
].decl
;
718 /* If a tree is built here, it would normally have the lineno of
719 the current statement. Later this tree will be moved to the
720 beginning of the function and this line number will be wrong.
721 To avoid this problem set the lineno to 0 here; that prevents
722 it from appearing in the RTL. */
724 location_t saved_location
= input_location
;
725 input_location
= UNKNOWN_LOCATION
;
727 stmts
= push_stmt_list ();
728 decl
= (*make_fname_decl
) (loc
, id
, fname_vars
[ix
].pretty
);
729 stmts
= pop_stmt_list (stmts
);
730 if (!IS_EMPTY_STMT (stmts
))
731 saved_function_name_decls
732 = tree_cons (decl
, stmts
, saved_function_name_decls
);
733 *fname_vars
[ix
].decl
= decl
;
734 input_location
= saved_location
;
736 if (!ix
&& !current_function_decl
)
737 pedwarn (loc
, 0, "%qD is not defined outside of function scope", decl
);
742 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
745 fix_string_type (tree value
)
747 int length
= TREE_STRING_LENGTH (value
);
749 tree e_type
, i_type
, a_type
;
751 /* Compute the number of elements, for the array type. */
752 if (TREE_TYPE (value
) == char_array_type_node
|| !TREE_TYPE (value
))
755 e_type
= char_type_node
;
757 else if (flag_char8_t
&& TREE_TYPE (value
) == char8_array_type_node
)
759 charsz
= TYPE_PRECISION (char8_type_node
) / BITS_PER_UNIT
;
760 e_type
= char8_type_node
;
762 else if (TREE_TYPE (value
) == char16_array_type_node
)
764 charsz
= TYPE_PRECISION (char16_type_node
) / BITS_PER_UNIT
;
765 e_type
= char16_type_node
;
767 else if (TREE_TYPE (value
) == char32_array_type_node
)
769 charsz
= TYPE_PRECISION (char32_type_node
) / BITS_PER_UNIT
;
770 e_type
= char32_type_node
;
774 charsz
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
775 e_type
= wchar_type_node
;
778 /* This matters only for targets where ssizetype has smaller precision
780 if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype
)), length
))
782 error ("size of string literal is too large");
783 length
= tree_to_shwi (TYPE_MAX_VALUE (ssizetype
)) / charsz
* charsz
;
784 char *str
= CONST_CAST (char *, TREE_STRING_POINTER (value
));
785 memset (str
+ length
, '\0',
786 MIN (TREE_STRING_LENGTH (value
) - length
, charsz
));
787 TREE_STRING_LENGTH (value
) = length
;
789 nchars
= length
/ charsz
;
791 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
792 limit in C++98 Annex B is very large (65536) and is not normative,
793 so we do not diagnose it (warn_overlength_strings is forced off
794 in c_common_post_options). */
795 if (warn_overlength_strings
)
797 const int nchars_max
= flag_isoc99
? 4095 : 509;
798 const int relevant_std
= flag_isoc99
? 99 : 90;
799 if (nchars
- 1 > nchars_max
)
800 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
801 separate the %d from the 'C'. 'ISO' should not be
802 translated, but it may be moved after 'C%d' in languages
803 where modifiers follow nouns. */
804 pedwarn (input_location
, OPT_Woverlength_strings
,
805 "string length %qd is greater than the length %qd "
806 "ISO C%d compilers are required to support",
807 nchars
- 1, nchars_max
, relevant_std
);
810 /* Create the array type for the string constant. The ISO C++
811 standard says that a string literal has type `const char[N]' or
812 `const wchar_t[N]'. We use the same logic when invoked as a C
813 front-end with -Wwrite-strings.
814 ??? We should change the type of an expression depending on the
815 state of a warning flag. We should just be warning -- see how
816 this is handled in the C++ front-end for the deprecated implicit
817 conversion from string literals to `char*' or `wchar_t*'.
819 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
820 array type being the unqualified version of that type.
821 Therefore, if we are constructing an array of const char, we must
822 construct the matching unqualified array type first. The C front
823 end does not require this, but it does no harm, so we do it
825 i_type
= build_index_type (size_int (nchars
- 1));
826 a_type
= build_array_type (e_type
, i_type
);
827 if (c_dialect_cxx() || warn_write_strings
)
828 a_type
= c_build_qualified_type (a_type
, TYPE_QUAL_CONST
);
830 TREE_TYPE (value
) = a_type
;
831 TREE_CONSTANT (value
) = 1;
832 TREE_READONLY (value
) = 1;
833 TREE_STATIC (value
) = 1;
837 /* Given a string of type STRING_TYPE, determine what kind of string
838 token would give an equivalent execution encoding: CPP_STRING,
839 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
840 This may not be exactly the string token type that initially created
841 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
842 string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
845 This effectively reverses part of the logic in lex_string and
848 static enum cpp_ttype
849 get_cpp_ttype_from_string_type (tree string_type
)
851 gcc_assert (string_type
);
852 if (TREE_CODE (string_type
) == POINTER_TYPE
)
853 string_type
= TREE_TYPE (string_type
);
855 if (TREE_CODE (string_type
) != ARRAY_TYPE
)
858 tree element_type
= TREE_TYPE (string_type
);
859 if (TREE_CODE (element_type
) != INTEGER_TYPE
)
862 int bits_per_character
= TYPE_PRECISION (element_type
);
863 switch (bits_per_character
)
866 return CPP_STRING
; /* It could have also been CPP_UTF8STRING. */
876 /* The global record of string concatentations, for use in
877 extracting locations within string literals. */
879 GTY(()) string_concat_db
*g_string_concat_db
;
881 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
884 c_get_substring_location (const substring_loc
&substr_loc
,
887 enum cpp_ttype tok_type
888 = get_cpp_ttype_from_string_type (substr_loc
.get_string_type ());
889 if (tok_type
== CPP_OTHER
)
890 return "unrecognized string type";
892 return get_location_within_string (parse_in
, g_string_concat_db
,
893 substr_loc
.get_fmt_string_loc (),
895 substr_loc
.get_caret_idx (),
896 substr_loc
.get_start_idx (),
897 substr_loc
.get_end_idx (),
902 /* Return true iff T is a boolean promoted to int. */
905 bool_promoted_to_int_p (tree t
)
907 return (CONVERT_EXPR_P (t
)
908 && TREE_TYPE (t
) == integer_type_node
909 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == BOOLEAN_TYPE
);
912 /* vector_targets_convertible_p is used for vector pointer types. The
913 callers perform various checks that the qualifiers are satisfactory,
914 while OTOH vector_targets_convertible_p ignores the number of elements
915 in the vectors. That's fine with vector pointers as we can consider,
916 say, a vector of 8 elements as two consecutive vectors of 4 elements,
917 and that does not require and conversion of the pointer values.
918 In contrast, vector_types_convertible_p and
919 vector_types_compatible_elements_p are used for vector value types. */
920 /* True if pointers to distinct types T1 and T2 can be converted to
921 each other without an explicit cast. Only returns true for opaque
924 vector_targets_convertible_p (const_tree t1
, const_tree t2
)
926 if (VECTOR_TYPE_P (t1
) && VECTOR_TYPE_P (t2
)
927 && (TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
928 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
934 /* vector_types_convertible_p is used for vector value types.
935 It could in principle call vector_targets_convertible_p as a subroutine,
936 but then the check for vector type would be duplicated with its callers,
937 and also the purpose of vector_targets_convertible_p would become
939 Where vector_types_convertible_p returns true, a conversion might still be
940 needed to make the types match.
941 In contrast, vector_targets_convertible_p is used for vector pointer
942 values, and vector_types_compatible_elements_p is used specifically
943 in the context for binary operators, as a check if use is possible without
945 /* True if vector types T1 and T2 can be converted to each other
946 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
947 can only be converted with -flax-vector-conversions yet that is not
948 in effect, emit a note telling the user about that option if such
949 a note has not previously been emitted. */
951 vector_types_convertible_p (const_tree t1
, const_tree t2
, bool emit_lax_note
)
953 static bool emitted_lax_note
= false;
954 bool convertible_lax
;
956 if ((TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
957 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
961 (tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
))
962 && (TREE_CODE (TREE_TYPE (t1
)) != REAL_TYPE
963 || known_eq (TYPE_VECTOR_SUBPARTS (t1
),
964 TYPE_VECTOR_SUBPARTS (t2
)))
965 && (INTEGRAL_TYPE_P (TREE_TYPE (t1
))
966 == INTEGRAL_TYPE_P (TREE_TYPE (t2
))));
968 if (!convertible_lax
|| flag_lax_vector_conversions
)
969 return convertible_lax
;
971 if (known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
972 && lang_hooks
.types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
975 if (emit_lax_note
&& !emitted_lax_note
)
977 emitted_lax_note
= true;
978 inform (input_location
, "use %<-flax-vector-conversions%> to permit "
979 "conversions between vectors with differing "
980 "element types or numbers of subparts");
986 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
987 and have vector types, V0 has the same type as V1, and the number of
988 elements of V0, V1, MASK is the same.
990 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
991 called with two arguments. In this case implementation passes the
992 first argument twice in order to share the same tree code. This fact
993 could enable the mask-values being twice the vector length. This is
994 an implementation accident and this semantics is not guaranteed to
997 c_build_vec_perm_expr (location_t loc
, tree v0
, tree v1
, tree mask
,
1002 bool maybe_const
= false;
1003 bool two_arguments
= false;
1005 if (v1
== NULL_TREE
)
1007 two_arguments
= true;
1011 if (v0
== error_mark_node
|| v1
== error_mark_node
1012 || mask
== error_mark_node
)
1013 return error_mark_node
;
1015 if (!gnu_vector_type_p (TREE_TYPE (mask
))
1016 || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask
)))
1019 error_at (loc
, "%<__builtin_shuffle%> last argument must "
1020 "be an integer vector");
1021 return error_mark_node
;
1024 if (!gnu_vector_type_p (TREE_TYPE (v0
))
1025 || !gnu_vector_type_p (TREE_TYPE (v1
)))
1028 error_at (loc
, "%<__builtin_shuffle%> arguments must be vectors");
1029 return error_mark_node
;
1032 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0
)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1
)))
1035 error_at (loc
, "%<__builtin_shuffle%> argument vectors must be of "
1037 return error_mark_node
;
1040 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0
)),
1041 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
)))
1042 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1
)),
1043 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
))))
1046 error_at (loc
, "%<__builtin_shuffle%> number of elements of the "
1047 "argument vector(s) and the mask vector should "
1049 return error_mark_node
;
1052 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0
))))
1053 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask
)))))
1056 error_at (loc
, "%<__builtin_shuffle%> argument vector(s) inner type "
1057 "must have the same size as inner type of the mask");
1058 return error_mark_node
;
1061 if (!c_dialect_cxx ())
1063 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1064 v0
= c_fully_fold (v0
, false, &maybe_const
);
1065 wrap
&= maybe_const
;
1068 v1
= v0
= save_expr (v0
);
1071 v1
= c_fully_fold (v1
, false, &maybe_const
);
1072 wrap
&= maybe_const
;
1075 mask
= c_fully_fold (mask
, false, &maybe_const
);
1076 wrap
&= maybe_const
;
1078 else if (two_arguments
)
1079 v1
= v0
= save_expr (v0
);
1081 ret
= build3_loc (loc
, VEC_PERM_EXPR
, TREE_TYPE (v0
), v0
, v1
, mask
);
1083 if (!c_dialect_cxx () && !wrap
)
1084 ret
= c_wrap_maybe_const (ret
, true);
1089 /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1092 c_build_vec_convert (location_t loc1
, tree expr
, location_t loc2
, tree type
,
1095 if (error_operand_p (type
))
1096 return error_mark_node
;
1097 if (error_operand_p (expr
))
1098 return error_mark_node
;
1100 if (!gnu_vector_type_p (TREE_TYPE (expr
))
1101 || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1102 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr
))))
1105 error_at (loc1
, "%<__builtin_convertvector%> first argument must "
1106 "be an integer or floating vector");
1107 return error_mark_node
;
1110 if (!gnu_vector_type_p (type
)
1111 || (!VECTOR_INTEGER_TYPE_P (type
) && !VECTOR_FLOAT_TYPE_P (type
)))
1114 error_at (loc2
, "%<__builtin_convertvector%> second argument must "
1115 "be an integer or floating vector type");
1116 return error_mark_node
;
1119 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr
)),
1120 TYPE_VECTOR_SUBPARTS (type
)))
1123 error_at (loc1
, "%<__builtin_convertvector%> number of elements "
1124 "of the first argument vector and the second argument "
1125 "vector type should be the same");
1126 return error_mark_node
;
1129 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr
)))
1130 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
1131 || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1132 && VECTOR_INTEGER_TYPE_P (type
)
1133 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr
)))
1134 == TYPE_PRECISION (TREE_TYPE (type
)))))
1135 return build1_loc (loc1
, VIEW_CONVERT_EXPR
, type
, expr
);
1138 bool maybe_const
= false;
1140 if (!c_dialect_cxx ())
1142 /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1143 expr
= c_fully_fold (expr
, false, &maybe_const
);
1144 wrap
&= maybe_const
;
1147 ret
= build_call_expr_internal_loc (loc1
, IFN_VEC_CONVERT
, type
, 1, expr
);
1150 ret
= c_wrap_maybe_const (ret
, true);
1155 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1156 to integral type. */
1159 c_common_get_narrower (tree op
, int *unsignedp_ptr
)
1161 op
= get_narrower (op
, unsignedp_ptr
);
1163 if (TREE_CODE (TREE_TYPE (op
)) == ENUMERAL_TYPE
1164 && ENUM_IS_SCOPED (TREE_TYPE (op
)))
1166 /* C++0x scoped enumerations don't implicitly convert to integral
1167 type; if we stripped an explicit conversion to a larger type we
1168 need to replace it so common_type will still work. */
1169 tree type
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op
)),
1170 TYPE_UNSIGNED (TREE_TYPE (op
)));
1171 op
= fold_convert (type
, op
);
1176 /* This is a helper function of build_binary_op.
1178 For certain operations if both args were extended from the same
1179 smaller type, do the arithmetic in that type and then extend.
1181 BITWISE indicates a bitwise operation.
1182 For them, this optimization is safe only if
1183 both args are zero-extended or both are sign-extended.
1184 Otherwise, we might change the result.
1185 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1186 but calculated in (unsigned short) it would be (unsigned short)-1.
1189 shorten_binary_op (tree result_type
, tree op0
, tree op1
, bool bitwise
)
1191 int unsigned0
, unsigned1
;
1196 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1197 excessive narrowing when we call get_narrower below. For
1198 example, suppose that OP0 is of unsigned int extended
1199 from signed char and that RESULT_TYPE is long long int.
1200 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1203 (long long int) (unsigned int) signed_char
1205 which get_narrower would narrow down to
1207 (unsigned int) signed char
1209 If we do not cast OP0 first, get_narrower would return
1210 signed_char, which is inconsistent with the case of the
1212 op0
= convert (result_type
, op0
);
1213 op1
= convert (result_type
, op1
);
1215 arg0
= c_common_get_narrower (op0
, &unsigned0
);
1216 arg1
= c_common_get_narrower (op1
, &unsigned1
);
1218 /* UNS is 1 if the operation to be done is an unsigned one. */
1219 uns
= TYPE_UNSIGNED (result_type
);
1221 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1222 but it *requires* conversion to FINAL_TYPE. */
1224 if ((TYPE_PRECISION (TREE_TYPE (op0
))
1225 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1226 && TREE_TYPE (op0
) != result_type
)
1227 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1228 if ((TYPE_PRECISION (TREE_TYPE (op1
))
1229 == TYPE_PRECISION (TREE_TYPE (arg1
)))
1230 && TREE_TYPE (op1
) != result_type
)
1231 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1233 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1235 /* For bitwise operations, signedness of nominal type
1236 does not matter. Consider only how operands were extended. */
1240 /* Note that in all three cases below we refrain from optimizing
1241 an unsigned operation on sign-extended args.
1242 That would not be valid. */
1244 /* Both args variable: if both extended in same way
1245 from same width, do it in that width.
1246 Do it unsigned if args were zero-extended. */
1247 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
1248 < TYPE_PRECISION (result_type
))
1249 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1250 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1251 && unsigned0
== unsigned1
1252 && (unsigned0
|| !uns
))
1253 return c_common_signed_or_unsigned_type
1254 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
1256 else if (TREE_CODE (arg0
) == INTEGER_CST
1257 && (unsigned1
|| !uns
)
1258 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1259 < TYPE_PRECISION (result_type
))
1261 = c_common_signed_or_unsigned_type (unsigned1
,
1263 && !POINTER_TYPE_P (type
)
1264 && int_fits_type_p (arg0
, type
))
1267 else if (TREE_CODE (arg1
) == INTEGER_CST
1268 && (unsigned0
|| !uns
)
1269 && (TYPE_PRECISION (TREE_TYPE (arg0
))
1270 < TYPE_PRECISION (result_type
))
1272 = c_common_signed_or_unsigned_type (unsigned0
,
1274 && !POINTER_TYPE_P (type
)
1275 && int_fits_type_p (arg1
, type
))
1281 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1282 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1285 int_safely_convertible_to_real_p (const_tree from_type
, const_tree to_type
)
1287 tree type_low_bound
= TYPE_MIN_VALUE (from_type
);
1288 tree type_high_bound
= TYPE_MAX_VALUE (from_type
);
1289 REAL_VALUE_TYPE real_low_bound
=
1290 real_value_from_int_cst (0, type_low_bound
);
1291 REAL_VALUE_TYPE real_high_bound
=
1292 real_value_from_int_cst (0, type_high_bound
);
1294 return exact_real_truncate (TYPE_MODE (to_type
), &real_low_bound
)
1295 && exact_real_truncate (TYPE_MODE (to_type
), &real_high_bound
);
1298 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1299 to the complex/real/integer type TYPE. Function returns non-zero when:
1300 * EXPR is a constant which cannot be exactly converted to TYPE.
1301 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1302 for EXPR type and TYPE being both integers or both real, or both
1304 * EXPR is not a constant of complex type and TYPE is a real or
1306 * EXPR is not a constant of real type and TYPE is an integer.
1307 * EXPR is not a constant of integer type which cannot be
1308 exactly converted to real type.
1310 Function allows conversions between types of different signedness and
1311 can return SAFE_CONVERSION (zero) in that case. Function can produce
1312 signedness warnings if PRODUCE_WARNS is true.
1314 RESULT, when non-null is the result of the conversion. When constant
1315 it is included in the text of diagnostics.
1317 Function allows conversions from complex constants to non-complex types,
1318 provided that imaginary part is zero and real part can be safely converted
1321 enum conversion_safety
1322 unsafe_conversion_p (location_t loc
, tree type
, tree expr
, tree result
,
1325 enum conversion_safety give_warning
= SAFE_CONVERSION
; /* is 0 or false */
1326 tree expr_type
= TREE_TYPE (expr
);
1328 bool cstresult
= (result
1329 && TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
);
1331 loc
= expansion_point_location_if_in_system_header (loc
);
1333 expr
= fold_for_warn (expr
);
1335 if (TREE_CODE (expr
) == REAL_CST
|| TREE_CODE (expr
) == INTEGER_CST
)
1337 /* If type is complex, we are interested in compatibility with
1339 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1340 type
= TREE_TYPE (type
);
1342 /* Warn for real constant that is not an exact integer converted
1344 if (TREE_CODE (expr_type
) == REAL_TYPE
1345 && TREE_CODE (type
) == INTEGER_TYPE
)
1347 if (!real_isinteger (TREE_REAL_CST_PTR (expr
), TYPE_MODE (expr_type
)))
1348 give_warning
= UNSAFE_REAL
;
1350 /* Warn for an integer constant that does not fit into integer type. */
1351 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1352 && TREE_CODE (type
) == INTEGER_TYPE
1353 && !int_fits_type_p (expr
, type
))
1355 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)
1356 && tree_int_cst_sgn (expr
) < 0)
1361 warning_at (loc
, OPT_Wsign_conversion
,
1362 "unsigned conversion from %qT to %qT "
1363 "changes value from %qE to %qE",
1364 expr_type
, type
, expr
, result
);
1366 warning_at (loc
, OPT_Wsign_conversion
,
1367 "unsigned conversion from %qT to %qT "
1368 "changes the value of %qE",
1369 expr_type
, type
, expr
);
1372 else if (!TYPE_UNSIGNED (type
) && TYPE_UNSIGNED (expr_type
))
1375 warning_at (loc
, OPT_Wsign_conversion
,
1376 "signed conversion from %qT to %qT changes "
1377 "value from %qE to %qE",
1378 expr_type
, type
, expr
, result
);
1380 warning_at (loc
, OPT_Wsign_conversion
,
1381 "signed conversion from %qT to %qT changes "
1383 expr_type
, type
, expr
);
1386 give_warning
= UNSAFE_OTHER
;
1388 else if (TREE_CODE (type
) == REAL_TYPE
)
1390 /* Warn for an integer constant that does not fit into real type. */
1391 if (TREE_CODE (expr_type
) == INTEGER_TYPE
)
1393 REAL_VALUE_TYPE a
= real_value_from_int_cst (0, expr
);
1394 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1395 give_warning
= UNSAFE_REAL
;
1397 /* Warn for a real constant that does not fit into a smaller
1399 else if (TREE_CODE (expr_type
) == REAL_TYPE
1400 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1402 REAL_VALUE_TYPE a
= TREE_REAL_CST (expr
);
1403 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1404 give_warning
= UNSAFE_REAL
;
1409 else if (TREE_CODE (expr
) == COMPLEX_CST
)
1411 tree imag_part
= TREE_IMAGPART (expr
);
1412 /* Conversion from complex constant with zero imaginary part,
1413 perform check for conversion of real part. */
1414 if ((TREE_CODE (imag_part
) == REAL_CST
1415 && real_zerop (imag_part
))
1416 || (TREE_CODE (imag_part
) == INTEGER_CST
1417 && integer_zerop (imag_part
)))
1418 /* Note: in this branch we use recursive call to unsafe_conversion_p
1419 with different type of EXPR, but it is still safe, because when EXPR
1420 is a constant, it's type is not used in text of generated warnings
1421 (otherwise they could sound misleading). */
1422 return unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
), result
,
1424 /* Conversion from complex constant with non-zero imaginary part. */
1427 /* Conversion to complex type.
1428 Perform checks for both real and imaginary parts. */
1429 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1431 /* Unfortunately, produce_warns must be false in two subsequent
1432 calls of unsafe_conversion_p, because otherwise we could
1433 produce strange "double" warnings, if both real and imaginary
1434 parts have conversion problems related to signedness.
1437 int32_t _Complex a = 0x80000000 + 0x80000000i;
1439 Possible solution: add a separate function for checking
1440 constants and combine result of two calls appropriately. */
1441 enum conversion_safety re_safety
=
1442 unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
),
1444 enum conversion_safety im_safety
=
1445 unsafe_conversion_p (loc
, type
, imag_part
, result
, false);
1447 /* Merge the results into appropriate single warning. */
1449 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1450 if (re_safety
== im_safety
)
1451 give_warning
= re_safety
;
1452 else if (!re_safety
&& im_safety
)
1453 give_warning
= im_safety
;
1454 else if (re_safety
&& !im_safety
)
1455 give_warning
= re_safety
;
1457 give_warning
= UNSAFE_OTHER
;
1459 /* Warn about conversion from complex to real or integer type. */
1461 give_warning
= UNSAFE_IMAGINARY
;
1465 /* Checks for remaining case: EXPR is not constant. */
1468 /* Warn for real types converted to integer types. */
1469 if (TREE_CODE (expr_type
) == REAL_TYPE
1470 && TREE_CODE (type
) == INTEGER_TYPE
)
1471 give_warning
= UNSAFE_REAL
;
1473 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1474 && TREE_CODE (type
) == INTEGER_TYPE
)
1476 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1477 expr
= get_unwidened (expr
, 0);
1478 expr_type
= TREE_TYPE (expr
);
1480 /* Don't warn for short y; short x = ((int)y & 0xff); */
1481 if (TREE_CODE (expr
) == BIT_AND_EXPR
1482 || TREE_CODE (expr
) == BIT_IOR_EXPR
1483 || TREE_CODE (expr
) == BIT_XOR_EXPR
)
1485 /* If both args were extended from a shortest type,
1486 use that type if that is safe. */
1487 expr_type
= shorten_binary_op (expr_type
,
1488 TREE_OPERAND (expr
, 0),
1489 TREE_OPERAND (expr
, 1),
1492 if (TREE_CODE (expr
) == BIT_AND_EXPR
)
1494 tree op0
= TREE_OPERAND (expr
, 0);
1495 tree op1
= TREE_OPERAND (expr
, 1);
1496 bool unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1497 bool unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1499 /* If one of the operands is a non-negative constant
1500 that fits in the target type, then the type of the
1501 other operand does not matter. */
1502 if ((TREE_CODE (op0
) == INTEGER_CST
1503 && int_fits_type_p (op0
, c_common_signed_type (type
))
1504 && int_fits_type_p (op0
, c_common_unsigned_type (type
)))
1505 || (TREE_CODE (op1
) == INTEGER_CST
1506 && int_fits_type_p (op1
, c_common_signed_type (type
))
1507 && int_fits_type_p (op1
,
1508 c_common_unsigned_type (type
))))
1509 return SAFE_CONVERSION
;
1510 /* If constant is unsigned and fits in the target
1511 type, then the result will also fit. */
1512 else if ((TREE_CODE (op0
) == INTEGER_CST
1514 && int_fits_type_p (op0
, type
))
1515 || (TREE_CODE (op1
) == INTEGER_CST
1517 && int_fits_type_p (op1
, type
)))
1518 return SAFE_CONVERSION
;
1521 /* Warn for integer types converted to smaller integer types. */
1522 if (TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1523 give_warning
= UNSAFE_OTHER
;
1525 /* When they are the same width but different signedness,
1526 then the value may change. */
1527 else if (((TYPE_PRECISION (type
) == TYPE_PRECISION (expr_type
)
1528 && TYPE_UNSIGNED (expr_type
) != TYPE_UNSIGNED (type
))
1529 /* Even when converted to a bigger type, if the type is
1530 unsigned but expr is signed, then negative values
1532 || (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)))
1534 warning_at (loc
, OPT_Wsign_conversion
, "conversion to %qT from %qT "
1535 "may change the sign of the result",
1539 /* Warn for integer types converted to real types if and only if
1540 all the range of values of the integer type cannot be
1541 represented by the real type. */
1542 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1543 && TREE_CODE (type
) == REAL_TYPE
)
1545 /* Don't warn about char y = 0xff; float x = (int) y; */
1546 expr
= get_unwidened (expr
, 0);
1547 expr_type
= TREE_TYPE (expr
);
1549 if (!int_safely_convertible_to_real_p (expr_type
, type
))
1550 give_warning
= UNSAFE_OTHER
;
1553 /* Warn for real types converted to smaller real types. */
1554 else if (TREE_CODE (expr_type
) == REAL_TYPE
1555 && TREE_CODE (type
) == REAL_TYPE
1556 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1557 give_warning
= UNSAFE_REAL
;
1559 /* Check conversion between two complex types. */
1560 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1561 && TREE_CODE (type
) == COMPLEX_TYPE
)
1563 /* Extract underlying types (i.e., type of real and imaginary
1564 parts) of expr_type and type. */
1565 tree from_type
= TREE_TYPE (expr_type
);
1566 tree to_type
= TREE_TYPE (type
);
1568 /* Warn for real types converted to integer types. */
1569 if (TREE_CODE (from_type
) == REAL_TYPE
1570 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1571 give_warning
= UNSAFE_REAL
;
1573 /* Warn for real types converted to smaller real types. */
1574 else if (TREE_CODE (from_type
) == REAL_TYPE
1575 && TREE_CODE (to_type
) == REAL_TYPE
1576 && TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1577 give_warning
= UNSAFE_REAL
;
1579 /* Check conversion for complex integer types. Here implementation
1580 is simpler than for real-domain integers because it does not
1581 involve sophisticated cases, such as bitmasks, casts, etc. */
1582 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1583 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1585 /* Warn for integer types converted to smaller integer types. */
1586 if (TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1587 give_warning
= UNSAFE_OTHER
;
1589 /* Check for different signedness, see case for real-domain
1590 integers (above) for a more detailed comment. */
1591 else if (((TYPE_PRECISION (to_type
) == TYPE_PRECISION (from_type
)
1592 && TYPE_UNSIGNED (to_type
) != TYPE_UNSIGNED (from_type
))
1593 || (TYPE_UNSIGNED (to_type
) && !TYPE_UNSIGNED (from_type
)))
1595 warning_at (loc
, OPT_Wsign_conversion
,
1596 "conversion to %qT from %qT "
1597 "may change the sign of the result",
1600 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1601 && TREE_CODE (to_type
) == REAL_TYPE
1602 && !int_safely_convertible_to_real_p (from_type
, to_type
))
1603 give_warning
= UNSAFE_OTHER
;
1606 /* Warn for complex types converted to real or integer types. */
1607 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1608 && TREE_CODE (type
) != COMPLEX_TYPE
)
1609 give_warning
= UNSAFE_IMAGINARY
;
1612 return give_warning
;
1616 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1617 Invoke this function on every expression that is converted implicitly,
1618 i.e. because of language rules and not because of an explicit cast. */
1621 convert_and_check (location_t loc
, tree type
, tree expr
)
1624 tree expr_for_warning
;
1626 /* Convert from a value with possible excess precision rather than
1627 via the semantic type, but do not warn about values not fitting
1628 exactly in the semantic type. */
1629 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
1631 tree orig_type
= TREE_TYPE (expr
);
1632 expr
= TREE_OPERAND (expr
, 0);
1633 expr_for_warning
= convert (orig_type
, expr
);
1634 if (orig_type
== type
)
1635 return expr_for_warning
;
1638 expr_for_warning
= expr
;
1640 if (TREE_TYPE (expr
) == type
)
1643 result
= convert (type
, expr
);
1645 if (c_inhibit_evaluation_warnings
== 0
1646 && !TREE_OVERFLOW_P (expr
)
1647 && result
!= error_mark_node
)
1648 warnings_for_convert_and_check (loc
, type
, expr_for_warning
, result
);
1653 /* A node in a list that describes references to variables (EXPR), which are
1654 either read accesses if WRITER is zero, or write accesses, in which case
1655 WRITER is the parent of EXPR. */
1662 /* Used to implement a cache the results of a call to verify_tree. We only
1663 use this for SAVE_EXPRs. */
1666 struct tlist_cache
*next
;
1667 struct tlist
*cache_before_sp
;
1668 struct tlist
*cache_after_sp
;
1672 /* Obstack to use when allocating tlist structures, and corresponding
1674 static struct obstack tlist_obstack
;
1675 static char *tlist_firstobj
= 0;
1677 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1679 static struct tlist
*warned_ids
;
1680 /* SAVE_EXPRs need special treatment. We process them only once and then
1681 cache the results. */
1682 static struct tlist_cache
*save_expr_cache
;
1684 static void add_tlist (struct tlist
**, struct tlist
*, tree
, int);
1685 static void merge_tlist (struct tlist
**, struct tlist
*, int);
1686 static void verify_tree (tree
, struct tlist
**, struct tlist
**, tree
);
1687 static bool warning_candidate_p (tree
);
1688 static bool candidate_equal_p (const_tree
, const_tree
);
1689 static void warn_for_collisions (struct tlist
*);
1690 static void warn_for_collisions_1 (tree
, tree
, struct tlist
*, int);
1691 static struct tlist
*new_tlist (struct tlist
*, tree
, tree
);
1693 /* Create a new struct tlist and fill in its fields. */
1694 static struct tlist
*
1695 new_tlist (struct tlist
*next
, tree t
, tree writer
)
1698 l
= XOBNEW (&tlist_obstack
, struct tlist
);
1705 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1706 is nonnull, we ignore any node we find which has a writer equal to it. */
1709 add_tlist (struct tlist
**to
, struct tlist
*add
, tree exclude_writer
, int copy
)
1713 struct tlist
*next
= add
->next
;
1716 if (!exclude_writer
|| !candidate_equal_p (add
->writer
, exclude_writer
))
1717 *to
= copy
? new_tlist (*to
, add
->expr
, add
->writer
) : add
;
1722 /* Merge the nodes of ADD into TO. This merging process is done so that for
1723 each variable that already exists in TO, no new node is added; however if
1724 there is a write access recorded in ADD, and an occurrence on TO is only
1725 a read access, then the occurrence in TO will be modified to record the
1729 merge_tlist (struct tlist
**to
, struct tlist
*add
, int copy
)
1731 struct tlist
**end
= to
;
1734 end
= &(*end
)->next
;
1740 struct tlist
*next
= add
->next
;
1742 for (tmp2
= *to
; tmp2
; tmp2
= tmp2
->next
)
1743 if (candidate_equal_p (tmp2
->expr
, add
->expr
))
1747 tmp2
->writer
= add
->writer
;
1751 *end
= copy
? new_tlist (NULL
, add
->expr
, add
->writer
) : add
;
1752 end
= &(*end
)->next
;
1759 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1760 references in list LIST conflict with it, excluding reads if ONLY writers
1764 warn_for_collisions_1 (tree written
, tree writer
, struct tlist
*list
,
1769 /* Avoid duplicate warnings. */
1770 for (tmp
= warned_ids
; tmp
; tmp
= tmp
->next
)
1771 if (candidate_equal_p (tmp
->expr
, written
))
1776 if (candidate_equal_p (list
->expr
, written
)
1777 && !candidate_equal_p (list
->writer
, writer
)
1778 && (!only_writes
|| list
->writer
))
1780 warned_ids
= new_tlist (warned_ids
, written
, NULL_TREE
);
1781 warning_at (EXPR_LOC_OR_LOC (writer
, input_location
),
1782 OPT_Wsequence_point
, "operation on %qE may be undefined",
1789 /* Given a list LIST of references to variables, find whether any of these
1790 can cause conflicts due to missing sequence points. */
1793 warn_for_collisions (struct tlist
*list
)
1797 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
1800 warn_for_collisions_1 (tmp
->expr
, tmp
->writer
, list
, 0);
1804 /* Return nonzero if X is a tree that can be verified by the sequence point
1808 warning_candidate_p (tree x
)
1810 if (DECL_P (x
) && DECL_ARTIFICIAL (x
))
1813 if (TREE_CODE (x
) == BLOCK
)
1816 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1817 (lvalue_p) crash on TRY/CATCH. */
1818 if (TREE_TYPE (x
) == NULL_TREE
|| VOID_TYPE_P (TREE_TYPE (x
)))
1824 /* No point to track non-const calls, they will never satisfy
1826 if (TREE_CODE (x
) == CALL_EXPR
&& (call_expr_flags (x
) & ECF_CONST
) == 0)
1829 if (TREE_CODE (x
) == STRING_CST
)
1835 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1837 candidate_equal_p (const_tree x
, const_tree y
)
1839 return (x
== y
) || (x
&& y
&& operand_equal_p (x
, y
, 0));
1842 /* Walk the tree X, and record accesses to variables. If X is written by the
1843 parent tree, WRITER is the parent.
1844 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1845 expression or its only operand forces a sequence point, then everything up
1846 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1848 Once we return, we will have emitted warnings if any subexpression before
1849 such a sequence point could be undefined. On a higher level, however, the
1850 sequence point may not be relevant, and we'll merge the two lists.
1852 Example: (b++, a) + b;
1853 The call that processes the COMPOUND_EXPR will store the increment of B
1854 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1855 processes the PLUS_EXPR will need to merge the two lists so that
1856 eventually, all accesses end up on the same list (and we'll warn about the
1857 unordered subexpressions b++ and b.
1859 A note on merging. If we modify the former example so that our expression
1862 care must be taken not simply to add all three expressions into the final
1863 PNO_SP list. The function merge_tlist takes care of that by merging the
1864 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1865 way, so that no more than one access to B is recorded. */
1868 verify_tree (tree x
, struct tlist
**pbefore_sp
, struct tlist
**pno_sp
,
1871 struct tlist
*tmp_before
, *tmp_nosp
, *tmp_list2
, *tmp_list3
;
1872 enum tree_code code
;
1873 enum tree_code_class cl
;
1875 /* X may be NULL if it is the operand of an empty statement expression
1881 code
= TREE_CODE (x
);
1882 cl
= TREE_CODE_CLASS (code
);
1884 if (warning_candidate_p (x
))
1885 *pno_sp
= new_tlist (*pno_sp
, x
, writer
);
1894 case TRUTH_ANDIF_EXPR
:
1895 case TRUTH_ORIF_EXPR
:
1897 tmp_before
= tmp_nosp
= tmp_list2
= tmp_list3
= 0;
1898 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1899 warn_for_collisions (tmp_nosp
);
1900 merge_tlist (pbefore_sp
, tmp_before
, 0);
1901 merge_tlist (pbefore_sp
, tmp_nosp
, 0);
1902 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1903 warn_for_collisions (tmp_list2
);
1904 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1905 merge_tlist (pno_sp
, tmp_list2
, 0);
1909 tmp_before
= tmp_list2
= 0;
1910 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_list2
, NULL_TREE
);
1911 warn_for_collisions (tmp_list2
);
1912 merge_tlist (pbefore_sp
, tmp_before
, 0);
1913 merge_tlist (pbefore_sp
, tmp_list2
, 0);
1915 tmp_list3
= tmp_nosp
= 0;
1916 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_nosp
, NULL_TREE
);
1917 warn_for_collisions (tmp_nosp
);
1918 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1920 tmp_list3
= tmp_list2
= 0;
1921 verify_tree (TREE_OPERAND (x
, 2), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1922 warn_for_collisions (tmp_list2
);
1923 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1924 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1925 two first, to avoid warning for (a ? b++ : b++). */
1926 merge_tlist (&tmp_nosp
, tmp_list2
, 0);
1927 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
1930 case PREDECREMENT_EXPR
:
1931 case PREINCREMENT_EXPR
:
1932 case POSTDECREMENT_EXPR
:
1933 case POSTINCREMENT_EXPR
:
1934 verify_tree (TREE_OPERAND (x
, 0), pno_sp
, pno_sp
, x
);
1938 tmp_before
= tmp_nosp
= tmp_list3
= 0;
1939 verify_tree (TREE_OPERAND (x
, 1), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1940 verify_tree (TREE_OPERAND (x
, 0), &tmp_list3
, &tmp_list3
, x
);
1941 /* Expressions inside the LHS are not ordered wrt. the sequence points
1942 in the RHS. Example:
1944 Despite the fact that the modification of "a" is in the before_sp
1945 list (tmp_before), it conflicts with the use of "a" in the LHS.
1946 We can handle this by adding the contents of tmp_list3
1947 to those of tmp_before, and redoing the collision warnings for that
1949 add_tlist (&tmp_before
, tmp_list3
, x
, 1);
1950 warn_for_collisions (tmp_before
);
1951 /* Exclude the LHS itself here; we first have to merge it into the
1952 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1953 didn't exclude the LHS, we'd get it twice, once as a read and once
1955 add_tlist (pno_sp
, tmp_list3
, x
, 0);
1956 warn_for_collisions_1 (TREE_OPERAND (x
, 0), x
, tmp_nosp
, 1);
1958 merge_tlist (pbefore_sp
, tmp_before
, 0);
1959 if (warning_candidate_p (TREE_OPERAND (x
, 0)))
1960 merge_tlist (&tmp_nosp
, new_tlist (NULL
, TREE_OPERAND (x
, 0), x
), 0);
1961 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 1);
1965 /* We need to warn about conflicts among arguments and conflicts between
1966 args and the function address. Side effects of the function address,
1967 however, are not ordered by the sequence point of the call. */
1969 call_expr_arg_iterator iter
;
1971 tmp_before
= tmp_nosp
= 0;
1972 verify_tree (CALL_EXPR_FN (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1973 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, x
)
1975 tmp_list2
= tmp_list3
= 0;
1976 verify_tree (arg
, &tmp_list2
, &tmp_list3
, NULL_TREE
);
1977 merge_tlist (&tmp_list3
, tmp_list2
, 0);
1978 add_tlist (&tmp_before
, tmp_list3
, NULL_TREE
, 0);
1980 add_tlist (&tmp_before
, tmp_nosp
, NULL_TREE
, 0);
1981 warn_for_collisions (tmp_before
);
1982 add_tlist (pbefore_sp
, tmp_before
, NULL_TREE
, 0);
1987 /* Scan all the list, e.g. indices of multi dimensional array. */
1990 tmp_before
= tmp_nosp
= 0;
1991 verify_tree (TREE_VALUE (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1992 merge_tlist (&tmp_nosp
, tmp_before
, 0);
1993 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
2000 struct tlist_cache
*t
;
2001 for (t
= save_expr_cache
; t
; t
= t
->next
)
2002 if (candidate_equal_p (t
->expr
, x
))
2007 t
= XOBNEW (&tlist_obstack
, struct tlist_cache
);
2008 t
->next
= save_expr_cache
;
2010 save_expr_cache
= t
;
2012 tmp_before
= tmp_nosp
= 0;
2013 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
2014 warn_for_collisions (tmp_nosp
);
2017 merge_tlist (&tmp_list3
, tmp_nosp
, 0);
2018 t
->cache_before_sp
= tmp_before
;
2019 t
->cache_after_sp
= tmp_list3
;
2021 merge_tlist (pbefore_sp
, t
->cache_before_sp
, 1);
2022 add_tlist (pno_sp
, t
->cache_after_sp
, NULL_TREE
, 1);
2027 x
= TREE_OPERAND (x
, 0);
2033 case VIEW_CONVERT_EXPR
:
2034 if (location_wrapper_p (x
))
2036 x
= TREE_OPERAND (x
, 0);
2045 if (cxx_dialect
>= cxx17
)
2046 goto sequenced_binary
;
2051 /* For other expressions, simply recurse on their operands.
2052 Manual tail recursion for unary expressions.
2053 Other non-expressions need not be processed. */
2054 if (cl
== tcc_unary
)
2056 x
= TREE_OPERAND (x
, 0);
2060 else if (IS_EXPR_CODE_CLASS (cl
))
2063 int max
= TREE_OPERAND_LENGTH (x
);
2064 for (lp
= 0; lp
< max
; lp
++)
2066 tmp_before
= tmp_nosp
= 0;
2067 verify_tree (TREE_OPERAND (x
, lp
), &tmp_before
, &tmp_nosp
, 0);
2068 merge_tlist (&tmp_nosp
, tmp_before
, 0);
2069 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
2076 /* Try to warn for undefined behavior in EXPR due to missing sequence
2080 verify_sequence_points (tree expr
)
2082 struct tlist
*before_sp
= 0, *after_sp
= 0;
2085 save_expr_cache
= 0;
2086 if (tlist_firstobj
== 0)
2088 gcc_obstack_init (&tlist_obstack
);
2089 tlist_firstobj
= (char *) obstack_alloc (&tlist_obstack
, 0);
2092 verify_tree (expr
, &before_sp
, &after_sp
, 0);
2093 warn_for_collisions (after_sp
);
2094 obstack_free (&tlist_obstack
, tlist_firstobj
);
2097 /* Validate the expression after `case' and apply default promotions. */
2100 check_case_value (location_t loc
, tree value
)
2102 if (value
== NULL_TREE
)
2105 if (TREE_CODE (value
) == INTEGER_CST
)
2106 /* Promote char or short to int. */
2107 value
= perform_integral_promotions (value
);
2108 else if (value
!= error_mark_node
)
2110 error_at (loc
, "case label does not reduce to an integer constant");
2111 value
= error_mark_node
;
2114 constant_expression_warning (value
);
2119 /* Return an integer type with BITS bits of precision,
2120 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2123 c_common_type_for_size (unsigned int bits
, int unsignedp
)
2127 if (bits
== TYPE_PRECISION (integer_type_node
))
2128 return unsignedp
? unsigned_type_node
: integer_type_node
;
2130 if (bits
== TYPE_PRECISION (signed_char_type_node
))
2131 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2133 if (bits
== TYPE_PRECISION (short_integer_type_node
))
2134 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2136 if (bits
== TYPE_PRECISION (long_integer_type_node
))
2137 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2139 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
2140 return (unsignedp
? long_long_unsigned_type_node
2141 : long_long_integer_type_node
);
2143 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2144 if (int_n_enabled_p
[i
]
2145 && bits
== int_n_data
[i
].bitsize
)
2146 return (unsignedp
? int_n_trees
[i
].unsigned_type
2147 : int_n_trees
[i
].signed_type
);
2149 if (bits
== TYPE_PRECISION (widest_integer_literal_type_node
))
2150 return (unsignedp
? widest_unsigned_literal_type_node
2151 : widest_integer_literal_type_node
);
2153 if (bits
<= TYPE_PRECISION (intQI_type_node
))
2154 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2156 if (bits
<= TYPE_PRECISION (intHI_type_node
))
2157 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2159 if (bits
<= TYPE_PRECISION (intSI_type_node
))
2160 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2162 if (bits
<= TYPE_PRECISION (intDI_type_node
))
2163 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2168 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2169 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2170 and saturating if SATP is nonzero, otherwise not saturating. */
2173 c_common_fixed_point_type_for_size (unsigned int ibit
, unsigned int fbit
,
2174 int unsignedp
, int satp
)
2176 enum mode_class mclass
;
2178 mclass
= unsignedp
? MODE_UFRACT
: MODE_FRACT
;
2180 mclass
= unsignedp
? MODE_UACCUM
: MODE_ACCUM
;
2182 opt_scalar_mode opt_mode
;
2184 FOR_EACH_MODE_IN_CLASS (opt_mode
, mclass
)
2186 mode
= opt_mode
.require ();
2187 if (GET_MODE_IBIT (mode
) >= ibit
&& GET_MODE_FBIT (mode
) >= fbit
)
2191 if (!opt_mode
.exists (&mode
) || !targetm
.scalar_mode_supported_p (mode
))
2193 sorry ("GCC cannot support operators with integer types and "
2194 "fixed-point types that have too many integral and "
2195 "fractional bits together");
2199 return c_common_type_for_mode (mode
, satp
);
2202 /* Used for communication between c_common_type_for_mode and
2203 c_register_builtin_type. */
2204 tree registered_builtin_types
;
2206 /* Return a data type that has machine mode MODE.
2207 If the mode is an integer,
2208 then UNSIGNEDP selects between signed and unsigned types.
2209 If the mode is a fixed-point mode,
2210 then UNSIGNEDP selects between saturating and nonsaturating types. */
2213 c_common_type_for_mode (machine_mode mode
, int unsignedp
)
2218 if (mode
== TYPE_MODE (integer_type_node
))
2219 return unsignedp
? unsigned_type_node
: integer_type_node
;
2221 if (mode
== TYPE_MODE (signed_char_type_node
))
2222 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2224 if (mode
== TYPE_MODE (short_integer_type_node
))
2225 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2227 if (mode
== TYPE_MODE (long_integer_type_node
))
2228 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2230 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2231 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2233 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2234 if (int_n_enabled_p
[i
]
2235 && mode
== int_n_data
[i
].m
)
2236 return (unsignedp
? int_n_trees
[i
].unsigned_type
2237 : int_n_trees
[i
].signed_type
);
2240 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2243 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2246 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2249 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2251 #if HOST_BITS_PER_WIDE_INT >= 64
2252 if (mode
== TYPE_MODE (intTI_type_node
))
2253 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2256 if (mode
== TYPE_MODE (float_type_node
))
2257 return float_type_node
;
2259 if (mode
== TYPE_MODE (double_type_node
))
2260 return double_type_node
;
2262 if (mode
== TYPE_MODE (long_double_type_node
))
2263 return long_double_type_node
;
2265 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2266 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2267 && mode
== TYPE_MODE (FLOATN_NX_TYPE_NODE (i
)))
2268 return FLOATN_NX_TYPE_NODE (i
);
2270 if (mode
== TYPE_MODE (void_type_node
))
2271 return void_type_node
;
2273 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
))
2274 || mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2276 unsigned int precision
2277 = GET_MODE_PRECISION (as_a
<scalar_int_mode
> (mode
));
2279 ? make_unsigned_type (precision
)
2280 : make_signed_type (precision
));
2283 if (COMPLEX_MODE_P (mode
))
2285 machine_mode inner_mode
;
2288 if (mode
== TYPE_MODE (complex_float_type_node
))
2289 return complex_float_type_node
;
2290 if (mode
== TYPE_MODE (complex_double_type_node
))
2291 return complex_double_type_node
;
2292 if (mode
== TYPE_MODE (complex_long_double_type_node
))
2293 return complex_long_double_type_node
;
2295 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2296 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2297 && mode
== TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i
)))
2298 return COMPLEX_FLOATN_NX_TYPE_NODE (i
);
2300 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
2301 return complex_integer_type_node
;
2303 inner_mode
= GET_MODE_INNER (mode
);
2304 inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2305 if (inner_type
!= NULL_TREE
)
2306 return build_complex_type (inner_type
);
2308 else if (GET_MODE_CLASS (mode
) == MODE_VECTOR_BOOL
2309 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2311 unsigned int elem_bits
= vector_element_size (GET_MODE_BITSIZE (mode
),
2312 GET_MODE_NUNITS (mode
));
2313 tree bool_type
= build_nonstandard_boolean_type (elem_bits
);
2314 return build_vector_type_for_mode (bool_type
, mode
);
2316 else if (VECTOR_MODE_P (mode
)
2317 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2319 machine_mode inner_mode
= GET_MODE_INNER (mode
);
2320 tree inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2321 if (inner_type
!= NULL_TREE
)
2322 return build_vector_type_for_mode (inner_type
, mode
);
2325 if (dfloat32_type_node
!= NULL_TREE
2326 && mode
== TYPE_MODE (dfloat32_type_node
))
2327 return dfloat32_type_node
;
2328 if (dfloat64_type_node
!= NULL_TREE
2329 && mode
== TYPE_MODE (dfloat64_type_node
))
2330 return dfloat64_type_node
;
2331 if (dfloat128_type_node
!= NULL_TREE
2332 && mode
== TYPE_MODE (dfloat128_type_node
))
2333 return dfloat128_type_node
;
2335 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode
))
2337 if (mode
== TYPE_MODE (short_fract_type_node
))
2338 return unsignedp
? sat_short_fract_type_node
: short_fract_type_node
;
2339 if (mode
== TYPE_MODE (fract_type_node
))
2340 return unsignedp
? sat_fract_type_node
: fract_type_node
;
2341 if (mode
== TYPE_MODE (long_fract_type_node
))
2342 return unsignedp
? sat_long_fract_type_node
: long_fract_type_node
;
2343 if (mode
== TYPE_MODE (long_long_fract_type_node
))
2344 return unsignedp
? sat_long_long_fract_type_node
2345 : long_long_fract_type_node
;
2347 if (mode
== TYPE_MODE (unsigned_short_fract_type_node
))
2348 return unsignedp
? sat_unsigned_short_fract_type_node
2349 : unsigned_short_fract_type_node
;
2350 if (mode
== TYPE_MODE (unsigned_fract_type_node
))
2351 return unsignedp
? sat_unsigned_fract_type_node
2352 : unsigned_fract_type_node
;
2353 if (mode
== TYPE_MODE (unsigned_long_fract_type_node
))
2354 return unsignedp
? sat_unsigned_long_fract_type_node
2355 : unsigned_long_fract_type_node
;
2356 if (mode
== TYPE_MODE (unsigned_long_long_fract_type_node
))
2357 return unsignedp
? sat_unsigned_long_long_fract_type_node
2358 : unsigned_long_long_fract_type_node
;
2360 if (mode
== TYPE_MODE (short_accum_type_node
))
2361 return unsignedp
? sat_short_accum_type_node
: short_accum_type_node
;
2362 if (mode
== TYPE_MODE (accum_type_node
))
2363 return unsignedp
? sat_accum_type_node
: accum_type_node
;
2364 if (mode
== TYPE_MODE (long_accum_type_node
))
2365 return unsignedp
? sat_long_accum_type_node
: long_accum_type_node
;
2366 if (mode
== TYPE_MODE (long_long_accum_type_node
))
2367 return unsignedp
? sat_long_long_accum_type_node
2368 : long_long_accum_type_node
;
2370 if (mode
== TYPE_MODE (unsigned_short_accum_type_node
))
2371 return unsignedp
? sat_unsigned_short_accum_type_node
2372 : unsigned_short_accum_type_node
;
2373 if (mode
== TYPE_MODE (unsigned_accum_type_node
))
2374 return unsignedp
? sat_unsigned_accum_type_node
2375 : unsigned_accum_type_node
;
2376 if (mode
== TYPE_MODE (unsigned_long_accum_type_node
))
2377 return unsignedp
? sat_unsigned_long_accum_type_node
2378 : unsigned_long_accum_type_node
;
2379 if (mode
== TYPE_MODE (unsigned_long_long_accum_type_node
))
2380 return unsignedp
? sat_unsigned_long_long_accum_type_node
2381 : unsigned_long_long_accum_type_node
;
2384 return unsignedp
? sat_qq_type_node
: qq_type_node
;
2386 return unsignedp
? sat_hq_type_node
: hq_type_node
;
2388 return unsignedp
? sat_sq_type_node
: sq_type_node
;
2390 return unsignedp
? sat_dq_type_node
: dq_type_node
;
2392 return unsignedp
? sat_tq_type_node
: tq_type_node
;
2394 if (mode
== UQQmode
)
2395 return unsignedp
? sat_uqq_type_node
: uqq_type_node
;
2396 if (mode
== UHQmode
)
2397 return unsignedp
? sat_uhq_type_node
: uhq_type_node
;
2398 if (mode
== USQmode
)
2399 return unsignedp
? sat_usq_type_node
: usq_type_node
;
2400 if (mode
== UDQmode
)
2401 return unsignedp
? sat_udq_type_node
: udq_type_node
;
2402 if (mode
== UTQmode
)
2403 return unsignedp
? sat_utq_type_node
: utq_type_node
;
2406 return unsignedp
? sat_ha_type_node
: ha_type_node
;
2408 return unsignedp
? sat_sa_type_node
: sa_type_node
;
2410 return unsignedp
? sat_da_type_node
: da_type_node
;
2412 return unsignedp
? sat_ta_type_node
: ta_type_node
;
2414 if (mode
== UHAmode
)
2415 return unsignedp
? sat_uha_type_node
: uha_type_node
;
2416 if (mode
== USAmode
)
2417 return unsignedp
? sat_usa_type_node
: usa_type_node
;
2418 if (mode
== UDAmode
)
2419 return unsignedp
? sat_uda_type_node
: uda_type_node
;
2420 if (mode
== UTAmode
)
2421 return unsignedp
? sat_uta_type_node
: uta_type_node
;
2424 for (t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
2425 if (TYPE_MODE (TREE_VALUE (t
)) == mode
2426 && !!unsignedp
== !!TYPE_UNSIGNED (TREE_VALUE (t
)))
2427 return TREE_VALUE (t
);
2433 c_common_unsigned_type (tree type
)
2435 return c_common_signed_or_unsigned_type (1, type
);
2438 /* Return a signed type the same as TYPE in other respects. */
2441 c_common_signed_type (tree type
)
2443 return c_common_signed_or_unsigned_type (0, type
);
2446 /* Return a type the same as TYPE except unsigned or
2447 signed according to UNSIGNEDP. */
2450 c_common_signed_or_unsigned_type (int unsignedp
, tree type
)
2455 /* This block of code emulates the behavior of the old
2456 c_common_unsigned_type. In particular, it returns
2457 long_unsigned_type_node if passed a long, even when a int would
2458 have the same size. This is necessary for warnings to work
2459 correctly in archs where sizeof(int) == sizeof(long) */
2461 type1
= TYPE_MAIN_VARIANT (type
);
2462 if (type1
== signed_char_type_node
|| type1
== char_type_node
|| type1
== unsigned_char_type_node
)
2463 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2464 if (type1
== integer_type_node
|| type1
== unsigned_type_node
)
2465 return unsignedp
? unsigned_type_node
: integer_type_node
;
2466 if (type1
== short_integer_type_node
|| type1
== short_unsigned_type_node
)
2467 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2468 if (type1
== long_integer_type_node
|| type1
== long_unsigned_type_node
)
2469 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2470 if (type1
== long_long_integer_type_node
|| type1
== long_long_unsigned_type_node
)
2471 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2473 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2474 if (int_n_enabled_p
[i
]
2475 && (type1
== int_n_trees
[i
].unsigned_type
2476 || type1
== int_n_trees
[i
].signed_type
))
2477 return (unsignedp
? int_n_trees
[i
].unsigned_type
2478 : int_n_trees
[i
].signed_type
);
2480 #if HOST_BITS_PER_WIDE_INT >= 64
2481 if (type1
== intTI_type_node
|| type1
== unsigned_intTI_type_node
)
2482 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2484 if (type1
== intDI_type_node
|| type1
== unsigned_intDI_type_node
)
2485 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2486 if (type1
== intSI_type_node
|| type1
== unsigned_intSI_type_node
)
2487 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2488 if (type1
== intHI_type_node
|| type1
== unsigned_intHI_type_node
)
2489 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2490 if (type1
== intQI_type_node
|| type1
== unsigned_intQI_type_node
)
2491 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2493 #define C_COMMON_FIXED_TYPES(NAME) \
2494 if (type1 == short_ ## NAME ## _type_node \
2495 || type1 == unsigned_short_ ## NAME ## _type_node) \
2496 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2497 : short_ ## NAME ## _type_node; \
2498 if (type1 == NAME ## _type_node \
2499 || type1 == unsigned_ ## NAME ## _type_node) \
2500 return unsignedp ? unsigned_ ## NAME ## _type_node \
2501 : NAME ## _type_node; \
2502 if (type1 == long_ ## NAME ## _type_node \
2503 || type1 == unsigned_long_ ## NAME ## _type_node) \
2504 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2505 : long_ ## NAME ## _type_node; \
2506 if (type1 == long_long_ ## NAME ## _type_node \
2507 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2508 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2509 : long_long_ ## NAME ## _type_node;
2511 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2512 if (type1 == NAME ## _type_node \
2513 || type1 == u ## NAME ## _type_node) \
2514 return unsignedp ? u ## NAME ## _type_node \
2515 : NAME ## _type_node;
2517 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2518 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2519 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2520 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2521 : sat_ ## short_ ## NAME ## _type_node; \
2522 if (type1 == sat_ ## NAME ## _type_node \
2523 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2524 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2525 : sat_ ## NAME ## _type_node; \
2526 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2527 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2528 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2529 : sat_ ## long_ ## NAME ## _type_node; \
2530 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2531 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2532 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2533 : sat_ ## long_long_ ## NAME ## _type_node;
2535 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2536 if (type1 == sat_ ## NAME ## _type_node \
2537 || type1 == sat_ ## u ## NAME ## _type_node) \
2538 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2539 : sat_ ## NAME ## _type_node;
2541 C_COMMON_FIXED_TYPES (fract
);
2542 C_COMMON_FIXED_TYPES_SAT (fract
);
2543 C_COMMON_FIXED_TYPES (accum
);
2544 C_COMMON_FIXED_TYPES_SAT (accum
);
2546 C_COMMON_FIXED_MODE_TYPES (qq
);
2547 C_COMMON_FIXED_MODE_TYPES (hq
);
2548 C_COMMON_FIXED_MODE_TYPES (sq
);
2549 C_COMMON_FIXED_MODE_TYPES (dq
);
2550 C_COMMON_FIXED_MODE_TYPES (tq
);
2551 C_COMMON_FIXED_MODE_TYPES_SAT (qq
);
2552 C_COMMON_FIXED_MODE_TYPES_SAT (hq
);
2553 C_COMMON_FIXED_MODE_TYPES_SAT (sq
);
2554 C_COMMON_FIXED_MODE_TYPES_SAT (dq
);
2555 C_COMMON_FIXED_MODE_TYPES_SAT (tq
);
2556 C_COMMON_FIXED_MODE_TYPES (ha
);
2557 C_COMMON_FIXED_MODE_TYPES (sa
);
2558 C_COMMON_FIXED_MODE_TYPES (da
);
2559 C_COMMON_FIXED_MODE_TYPES (ta
);
2560 C_COMMON_FIXED_MODE_TYPES_SAT (ha
);
2561 C_COMMON_FIXED_MODE_TYPES_SAT (sa
);
2562 C_COMMON_FIXED_MODE_TYPES_SAT (da
);
2563 C_COMMON_FIXED_MODE_TYPES_SAT (ta
);
2565 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2566 the precision; they have precision set to match their range, but
2567 may use a wider mode to match an ABI. If we change modes, we may
2568 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2569 the precision as well, so as to yield correct results for
2570 bit-field types. C++ does not have these separate bit-field
2571 types, and producing a signed or unsigned variant of an
2572 ENUMERAL_TYPE may cause other problems as well. */
2574 if (!INTEGRAL_TYPE_P (type
)
2575 || TYPE_UNSIGNED (type
) == unsignedp
)
2578 #define TYPE_OK(node) \
2579 (TYPE_MODE (type) == TYPE_MODE (node) \
2580 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2581 if (TYPE_OK (signed_char_type_node
))
2582 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2583 if (TYPE_OK (integer_type_node
))
2584 return unsignedp
? unsigned_type_node
: integer_type_node
;
2585 if (TYPE_OK (short_integer_type_node
))
2586 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2587 if (TYPE_OK (long_integer_type_node
))
2588 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2589 if (TYPE_OK (long_long_integer_type_node
))
2590 return (unsignedp
? long_long_unsigned_type_node
2591 : long_long_integer_type_node
);
2593 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2594 if (int_n_enabled_p
[i
]
2595 && TYPE_MODE (type
) == int_n_data
[i
].m
2596 && TYPE_PRECISION (type
) == int_n_data
[i
].bitsize
)
2597 return (unsignedp
? int_n_trees
[i
].unsigned_type
2598 : int_n_trees
[i
].signed_type
);
2600 #if HOST_BITS_PER_WIDE_INT >= 64
2601 if (TYPE_OK (intTI_type_node
))
2602 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2604 if (TYPE_OK (intDI_type_node
))
2605 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2606 if (TYPE_OK (intSI_type_node
))
2607 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2608 if (TYPE_OK (intHI_type_node
))
2609 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2610 if (TYPE_OK (intQI_type_node
))
2611 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2614 return build_nonstandard_integer_type (TYPE_PRECISION (type
), unsignedp
);
2617 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2620 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width
, int unsignedp
)
2624 /* Extended integer types of the same width as a standard type have
2625 lesser rank, so those of the same width as int promote to int or
2626 unsigned int and are valid for printf formats expecting int or
2627 unsigned int. To avoid such special cases, avoid creating
2628 extended integer types for bit-fields if a standard integer type
2630 if (width
== TYPE_PRECISION (integer_type_node
))
2631 return unsignedp
? unsigned_type_node
: integer_type_node
;
2632 if (width
== TYPE_PRECISION (signed_char_type_node
))
2633 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2634 if (width
== TYPE_PRECISION (short_integer_type_node
))
2635 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2636 if (width
== TYPE_PRECISION (long_integer_type_node
))
2637 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2638 if (width
== TYPE_PRECISION (long_long_integer_type_node
))
2639 return (unsignedp
? long_long_unsigned_type_node
2640 : long_long_integer_type_node
);
2641 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2642 if (int_n_enabled_p
[i
]
2643 && width
== int_n_data
[i
].bitsize
)
2644 return (unsignedp
? int_n_trees
[i
].unsigned_type
2645 : int_n_trees
[i
].signed_type
);
2646 return build_nonstandard_integer_type (width
, unsignedp
);
2649 /* The C version of the register_builtin_type langhook. */
2652 c_register_builtin_type (tree type
, const char* name
)
2656 decl
= build_decl (UNKNOWN_LOCATION
,
2657 TYPE_DECL
, get_identifier (name
), type
);
2658 DECL_ARTIFICIAL (decl
) = 1;
2659 if (!TYPE_NAME (type
))
2660 TYPE_NAME (type
) = decl
;
2661 lang_hooks
.decls
.pushdecl (decl
);
2663 registered_builtin_types
= tree_cons (0, type
, registered_builtin_types
);
2666 /* Print an error message for invalid operands to arith operation
2667 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2668 RICHLOC is a rich location for the message, containing either
2669 three separate locations for each of the operator and operands
2674 (C FE), or one location ranging over all over them
2682 binary_op_error (rich_location
*richloc
, enum tree_code code
,
2683 tree type0
, tree type1
)
2690 opname
= "+"; break;
2692 opname
= "-"; break;
2694 opname
= "*"; break;
2696 opname
= "max"; break;
2698 opname
= "min"; break;
2700 opname
= "=="; break;
2702 opname
= "!="; break;
2704 opname
= "<="; break;
2706 opname
= ">="; break;
2708 opname
= "<"; break;
2710 opname
= ">"; break;
2712 opname
= "<<"; break;
2714 opname
= ">>"; break;
2715 case TRUNC_MOD_EXPR
:
2716 case FLOOR_MOD_EXPR
:
2717 opname
= "%"; break;
2718 case TRUNC_DIV_EXPR
:
2719 case FLOOR_DIV_EXPR
:
2720 opname
= "/"; break;
2722 opname
= "&"; break;
2724 opname
= "|"; break;
2725 case TRUTH_ANDIF_EXPR
:
2726 opname
= "&&"; break;
2727 case TRUTH_ORIF_EXPR
:
2728 opname
= "||"; break;
2730 opname
= "^"; break;
2735 "invalid operands to binary %s (have %qT and %qT)",
2736 opname
, type0
, type1
);
2739 /* Given an expression as a tree, return its original type. Do this
2740 by stripping any conversion that preserves the sign and precision. */
2742 expr_original_type (tree expr
)
2744 STRIP_SIGN_NOPS (expr
);
2745 return TREE_TYPE (expr
);
2748 /* Subroutine of build_binary_op, used for comparison operations.
2749 See if the operands have both been converted from subword integer types
2750 and, if so, perhaps change them both back to their original type.
2751 This function is also responsible for converting the two operands
2752 to the proper common type for comparison.
2754 The arguments of this function are all pointers to local variables
2755 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2756 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2758 LOC is the location of the comparison.
2760 If this function returns non-NULL_TREE, it means that the comparison has
2761 a constant value. What this function returns is an expression for
2765 shorten_compare (location_t loc
, tree
*op0_ptr
, tree
*op1_ptr
,
2766 tree
*restype_ptr
, enum tree_code
*rescode_ptr
)
2769 tree op0
= *op0_ptr
;
2770 tree op1
= *op1_ptr
;
2771 int unsignedp0
, unsignedp1
;
2773 tree primop0
, primop1
;
2774 enum tree_code code
= *rescode_ptr
;
2776 /* Throw away any conversions to wider types
2777 already present in the operands. */
2779 primop0
= c_common_get_narrower (op0
, &unsignedp0
);
2780 primop1
= c_common_get_narrower (op1
, &unsignedp1
);
2782 /* If primopN is first sign-extended from primopN's precision to opN's
2783 precision, then zero-extended from opN's precision to
2784 *restype_ptr precision, shortenings might be invalid. */
2785 if (TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (TREE_TYPE (op0
))
2786 && TYPE_PRECISION (TREE_TYPE (op0
)) < TYPE_PRECISION (*restype_ptr
)
2788 && TYPE_UNSIGNED (TREE_TYPE (op0
)))
2790 if (TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (TREE_TYPE (op1
))
2791 && TYPE_PRECISION (TREE_TYPE (op1
)) < TYPE_PRECISION (*restype_ptr
)
2793 && TYPE_UNSIGNED (TREE_TYPE (op1
)))
2796 /* Handle the case that OP0 does not *contain* a conversion
2797 but it *requires* conversion to FINAL_TYPE. */
2799 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2800 unsignedp0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2801 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2802 unsignedp1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2804 /* If one of the operands must be floated, we cannot optimize. */
2805 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2806 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2808 /* If first arg is constant, swap the args (changing operation
2809 so value is preserved), for canonicalization. Don't do this if
2810 the second arg is 0. */
2812 if (TREE_CONSTANT (primop0
)
2813 && !integer_zerop (primop1
) && !real_zerop (primop1
)
2814 && !fixed_zerop (primop1
))
2816 std::swap (primop0
, primop1
);
2817 std::swap (op0
, op1
);
2820 std::swap (unsignedp0
, unsignedp1
);
2821 std::swap (real1
, real2
);
2840 *rescode_ptr
= code
;
2843 /* If comparing an integer against a constant more bits wide,
2844 maybe we can deduce a value of 1 or 0 independent of the data.
2845 Or else truncate the constant now
2846 rather than extend the variable at run time.
2848 This is only interesting if the constant is the wider arg.
2849 Also, it is not safe if the constant is unsigned and the
2850 variable arg is signed, since in this case the variable
2851 would be sign-extended and then regarded as unsigned.
2852 Our technique fails in this case because the lowest/highest
2853 possible unsigned results don't follow naturally from the
2854 lowest/highest possible values of the variable operand.
2855 For just EQ_EXPR and NE_EXPR there is another technique that
2856 could be used: see if the constant can be faithfully represented
2857 in the other operand's type, by truncating it and reextending it
2858 and see if that preserves the constant's value. */
2860 if (!real1
&& !real2
2861 && TREE_CODE (TREE_TYPE (primop0
)) != FIXED_POINT_TYPE
2862 && TREE_CODE (primop1
) == INTEGER_CST
2863 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2865 int min_gt
, max_gt
, min_lt
, max_lt
;
2866 tree maxval
, minval
;
2867 /* 1 if comparison is nominally unsigned. */
2868 int unsignedp
= TYPE_UNSIGNED (*restype_ptr
);
2871 type
= c_common_signed_or_unsigned_type (unsignedp0
,
2872 TREE_TYPE (primop0
));
2874 maxval
= TYPE_MAX_VALUE (type
);
2875 minval
= TYPE_MIN_VALUE (type
);
2877 if (unsignedp
&& !unsignedp0
)
2878 *restype_ptr
= c_common_signed_type (*restype_ptr
);
2880 if (TREE_TYPE (primop1
) != *restype_ptr
)
2882 /* Convert primop1 to target type, but do not introduce
2883 additional overflow. We know primop1 is an int_cst. */
2884 primop1
= force_fit_type (*restype_ptr
,
2887 TYPE_PRECISION (*restype_ptr
)),
2888 0, TREE_OVERFLOW (primop1
));
2890 if (type
!= *restype_ptr
)
2892 minval
= convert (*restype_ptr
, minval
);
2893 maxval
= convert (*restype_ptr
, maxval
);
2896 min_gt
= tree_int_cst_lt (primop1
, minval
);
2897 max_gt
= tree_int_cst_lt (primop1
, maxval
);
2898 min_lt
= tree_int_cst_lt (minval
, primop1
);
2899 max_lt
= tree_int_cst_lt (maxval
, primop1
);
2902 /* This used to be a switch, but Genix compiler can't handle that. */
2903 if (code
== NE_EXPR
)
2905 if (max_lt
|| min_gt
)
2906 val
= truthvalue_true_node
;
2908 else if (code
== EQ_EXPR
)
2910 if (max_lt
|| min_gt
)
2911 val
= truthvalue_false_node
;
2913 else if (code
== LT_EXPR
)
2916 val
= truthvalue_true_node
;
2918 val
= truthvalue_false_node
;
2920 else if (code
== GT_EXPR
)
2923 val
= truthvalue_true_node
;
2925 val
= truthvalue_false_node
;
2927 else if (code
== LE_EXPR
)
2930 val
= truthvalue_true_node
;
2932 val
= truthvalue_false_node
;
2934 else if (code
== GE_EXPR
)
2937 val
= truthvalue_true_node
;
2939 val
= truthvalue_false_node
;
2942 /* If primop0 was sign-extended and unsigned comparison specd,
2943 we did a signed comparison above using the signed type bounds.
2944 But the comparison we output must be unsigned.
2946 Also, for inequalities, VAL is no good; but if the signed
2947 comparison had *any* fixed result, it follows that the
2948 unsigned comparison just tests the sign in reverse
2949 (positive values are LE, negative ones GE).
2950 So we can generate an unsigned comparison
2951 against an extreme value of the signed type. */
2953 if (unsignedp
&& !unsignedp0
)
2960 primop1
= TYPE_MIN_VALUE (type
);
2966 primop1
= TYPE_MAX_VALUE (type
);
2973 type
= c_common_unsigned_type (type
);
2976 if (TREE_CODE (primop0
) != INTEGER_CST
2977 /* Don't warn if it's from a (non-system) macro. */
2978 && !(from_macro_expansion_at
2979 (expansion_point_location_if_in_system_header
2980 (EXPR_LOCATION (primop0
)))))
2982 if (val
== truthvalue_false_node
)
2983 warning_at (loc
, OPT_Wtype_limits
,
2984 "comparison is always false due to limited range of data type");
2985 if (val
== truthvalue_true_node
)
2986 warning_at (loc
, OPT_Wtype_limits
,
2987 "comparison is always true due to limited range of data type");
2992 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2993 if (TREE_SIDE_EFFECTS (primop0
))
2994 return build2 (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2998 /* Value is not predetermined, but do the comparison
2999 in the type of the operand that is not constant.
3000 TYPE is already properly set. */
3003 /* If either arg is decimal float and the other is float, find the
3004 proper common type to use for comparison. */
3005 else if (real1
&& real2
3006 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
3007 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
))))
3008 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
3010 /* If either arg is decimal float and the other is float, fail. */
3011 else if (real1
&& real2
3012 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
3013 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
)))))
3016 else if (real1
&& real2
3017 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3018 == TYPE_PRECISION (TREE_TYPE (primop1
))))
3019 type
= TREE_TYPE (primop0
);
3021 /* If args' natural types are both narrower than nominal type
3022 and both extend in the same manner, compare them
3023 in the type of the wider arg.
3024 Otherwise must actually extend both to the nominal
3025 common type lest different ways of extending
3027 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3029 else if (unsignedp0
== unsignedp1
&& real1
== real2
3030 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
3031 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
3033 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
3034 type
= c_common_signed_or_unsigned_type (unsignedp0
3035 || TYPE_UNSIGNED (*restype_ptr
),
3037 /* Make sure shorter operand is extended the right way
3038 to match the longer operand. */
3040 = convert (c_common_signed_or_unsigned_type (unsignedp0
,
3041 TREE_TYPE (primop0
)),
3044 = convert (c_common_signed_or_unsigned_type (unsignedp1
,
3045 TREE_TYPE (primop1
)),
3050 /* Here we must do the comparison on the nominal type
3051 using the args exactly as we received them. */
3052 type
= *restype_ptr
;
3056 /* We want to fold unsigned comparisons of >= and < against zero.
3057 For these, we may also issue a warning if we have a non-constant
3058 compared against zero, where the zero was spelled as "0" (rather
3059 than merely folding to it).
3060 If we have at least one constant, then op1 is constant
3061 and we may have a non-constant expression as op0. */
3062 if (!real1
&& !real2
&& integer_zerop (primop1
)
3063 && TYPE_UNSIGNED (*restype_ptr
))
3065 tree value
= NULL_TREE
;
3066 /* All unsigned values are >= 0, so we warn. However,
3067 if OP0 is a constant that is >= 0, the signedness of
3068 the comparison isn't an issue, so suppress the
3070 tree folded_op0
= fold_for_warn (op0
);
3072 warn_type_limits
&& !in_system_header_at (loc
)
3073 && !(TREE_CODE (folded_op0
) == INTEGER_CST
3074 && !TREE_OVERFLOW (convert (c_common_signed_type (type
),
3076 /* Do not warn for enumeration types. */
3077 && (TREE_CODE (expr_original_type (folded_op0
)) != ENUMERAL_TYPE
);
3083 warning_at (loc
, OPT_Wtype_limits
,
3084 "comparison of unsigned expression in %<>= 0%> "
3086 value
= truthvalue_true_node
;
3091 warning_at (loc
, OPT_Wtype_limits
,
3092 "comparison of unsigned expression in %<< 0%> "
3094 value
= truthvalue_false_node
;
3101 if (value
!= NULL_TREE
)
3103 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3104 if (TREE_SIDE_EFFECTS (primop0
))
3105 return build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3112 *op0_ptr
= convert (type
, primop0
);
3113 *op1_ptr
= convert (type
, primop1
);
3115 *restype_ptr
= truthvalue_type_node
;
3120 /* Return a tree for the sum or difference (RESULTCODE says which)
3121 of pointer PTROP and integer INTOP. */
3124 pointer_int_sum (location_t loc
, enum tree_code resultcode
,
3125 tree ptrop
, tree intop
, bool complain
)
3129 /* The result is a pointer of the same type that is being added. */
3130 tree result_type
= TREE_TYPE (ptrop
);
3132 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3134 if (complain
&& warn_pointer_arith
)
3135 pedwarn (loc
, OPT_Wpointer_arith
,
3136 "pointer of type %<void *%> used in arithmetic");
3138 return error_mark_node
;
3139 size_exp
= integer_one_node
;
3141 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3143 if (complain
&& warn_pointer_arith
)
3144 pedwarn (loc
, OPT_Wpointer_arith
,
3145 "pointer to a function used in arithmetic");
3147 return error_mark_node
;
3148 size_exp
= integer_one_node
;
3150 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
3151 TREE_TYPE (result_type
)))
3152 size_exp
= integer_one_node
;
3154 size_exp
= size_in_bytes_loc (loc
, TREE_TYPE (result_type
));
3156 /* We are manipulating pointer values, so we don't need to warn
3157 about relying on undefined signed overflow. We disable the
3158 warning here because we use integer types so fold won't know that
3159 they are really pointers. */
3160 fold_defer_overflow_warnings ();
3162 /* If what we are about to multiply by the size of the elements
3163 contains a constant term, apply distributive law
3164 and multiply that constant term separately.
3165 This helps produce common subexpressions. */
3166 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3167 && !TREE_CONSTANT (intop
)
3168 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3169 && TREE_CONSTANT (size_exp
)
3170 /* If the constant comes from pointer subtraction,
3171 skip this optimization--it would cause an error. */
3172 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
3173 /* If the constant is unsigned, and smaller than the pointer size,
3174 then we must skip this optimization. This is because it could cause
3175 an overflow error if the constant is negative but INTOP is not. */
3176 && (!TYPE_UNSIGNED (TREE_TYPE (intop
))
3177 || (TYPE_PRECISION (TREE_TYPE (intop
))
3178 == TYPE_PRECISION (TREE_TYPE (ptrop
)))))
3180 enum tree_code subcode
= resultcode
;
3181 tree int_type
= TREE_TYPE (intop
);
3182 if (TREE_CODE (intop
) == MINUS_EXPR
)
3183 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3184 /* Convert both subexpression types to the type of intop,
3185 because weird cases involving pointer arithmetic
3186 can result in a sum or difference with different type args. */
3187 ptrop
= build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop
, 1)),
3189 convert (int_type
, TREE_OPERAND (intop
, 1)),
3191 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
3194 /* Convert the integer argument to a type the same size as sizetype
3195 so the multiply won't overflow spuriously. */
3196 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
)
3197 || TYPE_UNSIGNED (TREE_TYPE (intop
)) != TYPE_UNSIGNED (sizetype
))
3198 intop
= convert (c_common_type_for_size (TYPE_PRECISION (sizetype
),
3199 TYPE_UNSIGNED (sizetype
)), intop
);
3201 /* Replace the integer argument with a suitable product by the object size.
3202 Do this multiplication as signed, then convert to the appropriate type
3203 for the pointer operation and disregard an overflow that occurred only
3204 because of the sign-extension change in the latter conversion. */
3206 tree t
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (intop
), intop
,
3207 convert (TREE_TYPE (intop
), size_exp
));
3208 intop
= convert (sizetype
, t
);
3209 if (TREE_OVERFLOW_P (intop
) && !TREE_OVERFLOW (t
))
3210 intop
= wide_int_to_tree (TREE_TYPE (intop
), wi::to_wide (intop
));
3213 /* Create the sum or difference. */
3214 if (resultcode
== MINUS_EXPR
)
3215 intop
= fold_build1_loc (loc
, NEGATE_EXPR
, sizetype
, intop
);
3217 ret
= fold_build_pointer_plus_loc (loc
, ptrop
, intop
);
3219 fold_undefer_and_ignore_overflow_warnings ();
3224 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3225 and if NON_CONST is known not to be permitted in an evaluated part
3226 of a constant expression. */
3229 c_wrap_maybe_const (tree expr
, bool non_const
)
3231 bool nowarning
= TREE_NO_WARNING (expr
);
3232 location_t loc
= EXPR_LOCATION (expr
);
3234 /* This should never be called for C++. */
3235 if (c_dialect_cxx ())
3238 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3239 STRIP_TYPE_NOPS (expr
);
3240 expr
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL
, expr
);
3241 C_MAYBE_CONST_EXPR_NON_CONST (expr
) = non_const
;
3243 TREE_NO_WARNING (expr
) = 1;
3244 protected_set_expr_location (expr
, loc
);
3249 /* Return whether EXPR is a declaration whose address can never be
3253 decl_with_nonnull_addr_p (const_tree expr
)
3255 return (DECL_P (expr
)
3256 && (TREE_CODE (expr
) == PARM_DECL
3257 || TREE_CODE (expr
) == LABEL_DECL
3258 || !DECL_WEAK (expr
)));
3261 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3262 or for an `if' or `while' statement or ?..: exp. It should already
3263 have been validated to be of suitable type; otherwise, a bad
3264 diagnostic may result.
3266 The EXPR is located at LOCATION.
3268 This preparation consists of taking the ordinary
3269 representation of an expression expr and producing a valid tree
3270 boolean expression describing whether expr is nonzero. We could
3271 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3272 but we optimize comparisons, &&, ||, and !.
3274 The resulting type should always be `truthvalue_type_node'. */
3277 c_common_truthvalue_conversion (location_t location
, tree expr
)
3279 STRIP_ANY_LOCATION_WRAPPER (expr
);
3280 switch (TREE_CODE (expr
))
3282 case EQ_EXPR
: case NE_EXPR
: case UNEQ_EXPR
: case LTGT_EXPR
:
3283 case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
3284 case UNLE_EXPR
: case UNGE_EXPR
: case UNLT_EXPR
: case UNGT_EXPR
:
3285 case ORDERED_EXPR
: case UNORDERED_EXPR
:
3286 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3288 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3289 TREE_OPERAND (expr
, 0), TREE_OPERAND (expr
, 1));
3292 case TRUTH_ANDIF_EXPR
:
3293 case TRUTH_ORIF_EXPR
:
3294 case TRUTH_AND_EXPR
:
3296 case TRUTH_XOR_EXPR
:
3297 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3299 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3300 c_common_truthvalue_conversion (location
,
3301 TREE_OPERAND (expr
, 0)),
3302 c_common_truthvalue_conversion (location
,
3303 TREE_OPERAND (expr
, 1)));
3306 case TRUTH_NOT_EXPR
:
3307 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3309 expr
= build1 (TREE_CODE (expr
), truthvalue_type_node
,
3310 c_common_truthvalue_conversion (location
,
3311 TREE_OPERAND (expr
, 0)));
3318 if (TREE_CODE (TREE_TYPE (expr
)) == ENUMERAL_TYPE
3319 && !integer_zerop (expr
)
3320 && !integer_onep (expr
))
3321 warning_at (location
, OPT_Wint_in_bool_context
,
3322 "enum constant in boolean context");
3323 return integer_zerop (expr
) ? truthvalue_false_node
3324 : truthvalue_true_node
;
3327 return real_compare (NE_EXPR
, &TREE_REAL_CST (expr
), &dconst0
)
3328 ? truthvalue_true_node
3329 : truthvalue_false_node
;
3332 return fixed_compare (NE_EXPR
, &TREE_FIXED_CST (expr
),
3333 &FCONST0 (TYPE_MODE (TREE_TYPE (expr
))))
3334 ? truthvalue_true_node
3335 : truthvalue_false_node
;
3338 expr
= build_unary_op (location
, ADDR_EXPR
, expr
, false);
3343 tree inner
= TREE_OPERAND (expr
, 0);
3344 if (decl_with_nonnull_addr_p (inner
))
3346 /* Common Ada programmer's mistake. */
3347 warning_at (location
,
3349 "the address of %qD will always evaluate as %<true%>",
3351 return truthvalue_true_node
;
3357 expr
= build_binary_op (EXPR_LOCATION (expr
),
3358 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
3359 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3360 c_common_truthvalue_conversion (location
,
3361 TREE_OPERAND (expr
, 0)),
3362 c_common_truthvalue_conversion (location
,
3363 TREE_OPERAND (expr
, 1)),
3371 case EXCESS_PRECISION_EXPR
:
3372 /* These don't change whether an object is nonzero or zero. */
3373 return c_common_truthvalue_conversion (location
, TREE_OPERAND (expr
, 0));
3377 /* These don't change whether an object is zero or nonzero, but
3378 we can't ignore them if their second arg has side-effects. */
3379 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
3381 expr
= build2 (COMPOUND_EXPR
, truthvalue_type_node
,
3382 TREE_OPERAND (expr
, 1),
3383 c_common_truthvalue_conversion
3384 (location
, TREE_OPERAND (expr
, 0)));
3388 return c_common_truthvalue_conversion (location
,
3389 TREE_OPERAND (expr
, 0));
3392 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3393 "%<*%> in boolean context, suggest %<&&%> instead");
3397 /* We will only warn on signed shifts here, because the majority of
3398 false positive warnings happen in code where unsigned arithmetic
3399 was used in anticipation of a possible overflow.
3400 Furthermore, if we see an unsigned type here we know that the
3401 result of the shift is not subject to integer promotion rules. */
3402 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
3403 && !TYPE_UNSIGNED (TREE_TYPE (expr
)))
3404 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3405 "%<<<%> in boolean context, did you mean %<<%>?");
3409 if (warn_int_in_bool_context
3410 && !from_macro_definition_at (EXPR_LOCATION (expr
)))
3412 tree val1
= fold_for_warn (TREE_OPERAND (expr
, 1));
3413 tree val2
= fold_for_warn (TREE_OPERAND (expr
, 2));
3414 if (TREE_CODE (val1
) == INTEGER_CST
3415 && TREE_CODE (val2
) == INTEGER_CST
3416 && !integer_zerop (val1
)
3417 && !integer_zerop (val2
)
3418 && (!integer_onep (val1
)
3419 || !integer_onep (val2
)))
3420 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3421 "%<?:%> using integer constants in boolean context, "
3422 "the expression will always evaluate to %<true%>");
3423 else if ((TREE_CODE (val1
) == INTEGER_CST
3424 && !integer_zerop (val1
)
3425 && !integer_onep (val1
))
3426 || (TREE_CODE (val2
) == INTEGER_CST
3427 && !integer_zerop (val2
)
3428 && !integer_onep (val2
)))
3429 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3430 "%<?:%> using integer constants in boolean context");
3432 /* Distribute the conversion into the arms of a COND_EXPR. */
3433 if (c_dialect_cxx ())
3434 /* Avoid premature folding. */
3438 int w
= warn_int_in_bool_context
;
3439 warn_int_in_bool_context
= 0;
3440 /* Folding will happen later for C. */
3441 expr
= build3 (COND_EXPR
, truthvalue_type_node
,
3442 TREE_OPERAND (expr
, 0),
3443 c_common_truthvalue_conversion (location
,
3444 TREE_OPERAND (expr
, 1)),
3445 c_common_truthvalue_conversion (location
,
3446 TREE_OPERAND (expr
, 2)));
3447 warn_int_in_bool_context
= w
;
3453 tree totype
= TREE_TYPE (expr
);
3454 tree fromtype
= TREE_TYPE (TREE_OPERAND (expr
, 0));
3456 if (POINTER_TYPE_P (totype
)
3457 && !c_inhibit_evaluation_warnings
3458 && TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3464 warning_at (location
,
3466 "the compiler can assume that the address of "
3467 "%qD will always evaluate to %<true%>",
3471 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3472 since that affects how `default_conversion' will behave. */
3473 if (TREE_CODE (totype
) == REFERENCE_TYPE
3474 || TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3476 /* Don't strip a conversion from C++0x scoped enum, since they
3477 don't implicitly convert to other types. */
3478 if (TREE_CODE (fromtype
) == ENUMERAL_TYPE
3479 && ENUM_IS_SCOPED (fromtype
))
3481 /* If this isn't narrowing the argument, we can ignore it. */
3482 if (TYPE_PRECISION (totype
) >= TYPE_PRECISION (fromtype
))
3483 return c_common_truthvalue_conversion (location
,
3484 TREE_OPERAND (expr
, 0));
3489 if (!TREE_NO_WARNING (expr
)
3491 && warning_at (location
, OPT_Wparentheses
,
3492 "suggest parentheses around assignment used as "
3494 TREE_NO_WARNING (expr
) = 1;
3499 tree folded_expr
= fold_for_warn (expr
);
3500 if (folded_expr
!= expr
)
3501 return c_common_truthvalue_conversion (location
, folded_expr
);
3509 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
3511 tree t
= save_expr (expr
);
3512 expr
= (build_binary_op
3513 (EXPR_LOCATION (expr
),
3514 (TREE_SIDE_EFFECTS (expr
)
3515 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3516 c_common_truthvalue_conversion
3518 build_unary_op (location
, REALPART_EXPR
, t
, false)),
3519 c_common_truthvalue_conversion
3521 build_unary_op (location
, IMAGPART_EXPR
, t
, false)),
3526 if (TREE_CODE (TREE_TYPE (expr
)) == FIXED_POINT_TYPE
)
3528 tree fixed_zero_node
= build_fixed (TREE_TYPE (expr
),
3530 (TREE_TYPE (expr
))));
3531 return build_binary_op (location
, NE_EXPR
, expr
, fixed_zero_node
, true);
3534 return build_binary_op (location
, NE_EXPR
, expr
, integer_zero_node
, true);
3537 protected_set_expr_location (expr
, location
);
3541 static void def_builtin_1 (enum built_in_function fncode
,
3543 enum built_in_class fnclass
,
3544 tree fntype
, tree libtype
,
3545 bool both_p
, bool fallback_p
, bool nonansi_p
,
3546 tree fnattrs
, bool implicit_p
);
3549 /* Apply the TYPE_QUALS to the new DECL. */
3552 c_apply_type_quals_to_decl (int type_quals
, tree decl
)
3554 tree type
= TREE_TYPE (decl
);
3556 if (type
== error_mark_node
)
3559 if ((type_quals
& TYPE_QUAL_CONST
)
3560 || (type
&& TREE_CODE (type
) == REFERENCE_TYPE
))
3561 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3562 constructor can produce constant init, so rely on cp_finish_decl to
3563 clear TREE_READONLY if the variable has non-constant init. */
3564 TREE_READONLY (decl
) = 1;
3565 if (type_quals
& TYPE_QUAL_VOLATILE
)
3567 TREE_SIDE_EFFECTS (decl
) = 1;
3568 TREE_THIS_VOLATILE (decl
) = 1;
3570 if (type_quals
& TYPE_QUAL_RESTRICT
)
3572 while (type
&& TREE_CODE (type
) == ARRAY_TYPE
)
3573 /* Allow 'restrict' on arrays of pointers.
3574 FIXME currently we just ignore it. */
3575 type
= TREE_TYPE (type
);
3577 || !POINTER_TYPE_P (type
)
3578 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
)))
3579 error ("invalid use of %<restrict%>");
3583 /* Return the typed-based alias set for T, which may be an expression
3584 or a type. Return -1 if we don't do anything special. */
3587 c_common_get_alias_set (tree t
)
3589 /* For VLAs, use the alias set of the element type rather than the
3590 default of alias set 0 for types compared structurally. */
3591 if (TYPE_P (t
) && TYPE_STRUCTURAL_EQUALITY_P (t
))
3593 if (TREE_CODE (t
) == ARRAY_TYPE
)
3594 return get_alias_set (TREE_TYPE (t
));
3598 /* That's all the expressions we handle specially. */
3602 /* Unlike char, char8_t doesn't alias. */
3603 if (flag_char8_t
&& t
== char8_type_node
)
3606 /* The C standard guarantees that any object may be accessed via an
3607 lvalue that has narrow character type (except char8_t). */
3608 if (t
== char_type_node
3609 || t
== signed_char_type_node
3610 || t
== unsigned_char_type_node
)
3613 /* The C standard specifically allows aliasing between signed and
3614 unsigned variants of the same type. We treat the signed
3615 variant as canonical. */
3616 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_UNSIGNED (t
))
3618 tree t1
= c_common_signed_type (t
);
3620 /* t1 == t can happen for boolean nodes which are always unsigned. */
3622 return get_alias_set (t1
);
3628 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3629 the IS_SIZEOF parameter indicates which operator is being applied.
3630 The COMPLAIN flag controls whether we should diagnose possibly
3631 ill-formed constructs or not. LOC is the location of the SIZEOF or
3632 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3633 a type in any context should be returned, rather than the normal
3634 alignment for that type. */
3637 c_sizeof_or_alignof_type (location_t loc
,
3638 tree type
, bool is_sizeof
, bool min_alignof
,
3641 const char *op_name
;
3643 enum tree_code type_code
= TREE_CODE (type
);
3645 op_name
= is_sizeof
? "sizeof" : "__alignof__";
3647 if (type_code
== FUNCTION_TYPE
)
3651 if (complain
&& warn_pointer_arith
)
3652 pedwarn (loc
, OPT_Wpointer_arith
,
3653 "invalid application of %<sizeof%> to a function type");
3655 return error_mark_node
;
3656 value
= size_one_node
;
3662 if (c_dialect_cxx ())
3663 pedwarn (loc
, OPT_Wpedantic
, "ISO C++ does not permit "
3664 "%<alignof%> applied to a function type");
3666 pedwarn (loc
, OPT_Wpedantic
, "ISO C does not permit "
3667 "%<_Alignof%> applied to a function type");
3669 value
= size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
3672 else if (type_code
== VOID_TYPE
|| type_code
== ERROR_MARK
)
3674 if (type_code
== VOID_TYPE
3675 && complain
&& warn_pointer_arith
)
3676 pedwarn (loc
, OPT_Wpointer_arith
,
3677 "invalid application of %qs to a void type", op_name
);
3679 return error_mark_node
;
3680 value
= size_one_node
;
3682 else if (!COMPLETE_TYPE_P (type
)
3683 && (!c_dialect_cxx () || is_sizeof
|| type_code
!= ARRAY_TYPE
))
3686 error_at (loc
, "invalid application of %qs to incomplete type %qT",
3688 return error_mark_node
;
3690 else if (c_dialect_cxx () && type_code
== ARRAY_TYPE
3691 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
3694 error_at (loc
, "invalid application of %qs to array type %qT of "
3695 "incomplete element type", op_name
, type
);
3696 return error_mark_node
;
3698 else if (!verify_type_context (loc
, is_sizeof
? TCTX_SIZEOF
: TCTX_ALIGNOF
,
3702 return error_mark_node
;
3703 value
= size_one_node
;
3708 /* Convert in case a char is more than one unit. */
3709 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
3710 size_int (TYPE_PRECISION (char_type_node
)
3712 else if (min_alignof
)
3713 value
= size_int (min_align_of_type (type
));
3715 value
= size_int (TYPE_ALIGN_UNIT (type
));
3718 /* VALUE will have the middle-end integer type sizetype.
3719 However, we should really return a value of type `size_t',
3720 which is just a typedef for an ordinary integer type. */
3721 value
= fold_convert_loc (loc
, size_type_node
, value
);
3726 /* Implement the __alignof keyword: Return the minimum required
3727 alignment of EXPR, measured in bytes. For VAR_DECLs,
3728 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3729 from an "aligned" __attribute__ specification). LOC is the
3730 location of the ALIGNOF operator. */
3733 c_alignof_expr (location_t loc
, tree expr
)
3737 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (expr
)))
3740 else if (VAR_OR_FUNCTION_DECL_P (expr
))
3741 t
= size_int (DECL_ALIGN_UNIT (expr
));
3743 else if (TREE_CODE (expr
) == COMPONENT_REF
3744 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
3746 error_at (loc
, "%<__alignof%> applied to a bit-field");
3749 else if (TREE_CODE (expr
) == COMPONENT_REF
3750 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
3751 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr
, 1)));
3753 else if (INDIRECT_REF_P (expr
))
3755 tree t
= TREE_OPERAND (expr
, 0);
3757 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
3759 while (CONVERT_EXPR_P (t
)
3760 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
3764 t
= TREE_OPERAND (t
, 0);
3765 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
3766 if (thisalign
> bestalign
)
3767 best
= t
, bestalign
= thisalign
;
3769 return c_alignof (loc
, TREE_TYPE (TREE_TYPE (best
)));
3772 return c_alignof (loc
, TREE_TYPE (expr
));
3774 return fold_convert_loc (loc
, size_type_node
, t
);
3777 /* Handle C and C++ default attributes. */
3779 enum built_in_attribute
3781 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3782 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3783 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3784 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3785 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3786 #include "builtin-attrs.def"
3787 #undef DEF_ATTR_NULL_TREE
3789 #undef DEF_ATTR_STRING
3790 #undef DEF_ATTR_IDENT
3791 #undef DEF_ATTR_TREE_LIST
3795 static GTY(()) tree built_in_attributes
[(int) ATTR_LAST
];
3797 static void c_init_attributes (void);
3801 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3802 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3803 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3804 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3805 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3806 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3807 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3808 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3810 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3812 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3813 ARG6, ARG7, ARG8) NAME,
3814 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3815 ARG6, ARG7, ARG8, ARG9) NAME,
3816 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3817 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3818 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3819 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3820 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3821 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3822 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3823 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3824 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3825 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3827 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3829 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3831 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3832 #include "builtin-types.def"
3833 #undef DEF_PRIMITIVE_TYPE
3834 #undef DEF_FUNCTION_TYPE_0
3835 #undef DEF_FUNCTION_TYPE_1
3836 #undef DEF_FUNCTION_TYPE_2
3837 #undef DEF_FUNCTION_TYPE_3
3838 #undef DEF_FUNCTION_TYPE_4
3839 #undef DEF_FUNCTION_TYPE_5
3840 #undef DEF_FUNCTION_TYPE_6
3841 #undef DEF_FUNCTION_TYPE_7
3842 #undef DEF_FUNCTION_TYPE_8
3843 #undef DEF_FUNCTION_TYPE_9
3844 #undef DEF_FUNCTION_TYPE_10
3845 #undef DEF_FUNCTION_TYPE_11
3846 #undef DEF_FUNCTION_TYPE_VAR_0
3847 #undef DEF_FUNCTION_TYPE_VAR_1
3848 #undef DEF_FUNCTION_TYPE_VAR_2
3849 #undef DEF_FUNCTION_TYPE_VAR_3
3850 #undef DEF_FUNCTION_TYPE_VAR_4
3851 #undef DEF_FUNCTION_TYPE_VAR_5
3852 #undef DEF_FUNCTION_TYPE_VAR_6
3853 #undef DEF_FUNCTION_TYPE_VAR_7
3854 #undef DEF_POINTER_TYPE
3858 typedef enum c_builtin_type builtin_type
;
3860 /* A temporary array for c_common_nodes_and_builtins. Used in
3861 communication with def_fn_type. */
3862 static tree builtin_types
[(int) BT_LAST
+ 1];
3864 /* A helper function for c_common_nodes_and_builtins. Build function type
3865 for DEF with return type RET and N arguments. If VAR is true, then the
3866 function should be variadic after those N arguments.
3868 Takes special care not to ICE if any of the types involved are
3869 error_mark_node, which indicates that said type is not in fact available
3870 (see builtin_type_for_size). In which case the function type as a whole
3871 should be error_mark_node. */
3874 def_fn_type (builtin_type def
, builtin_type ret
, bool var
, int n
, ...)
3877 tree
*args
= XALLOCAVEC (tree
, n
);
3882 for (i
= 0; i
< n
; ++i
)
3884 builtin_type a
= (builtin_type
) va_arg (list
, int);
3885 t
= builtin_types
[a
];
3886 if (t
== error_mark_node
)
3891 t
= builtin_types
[ret
];
3892 if (t
== error_mark_node
)
3895 t
= build_varargs_function_type_array (t
, n
, args
);
3897 t
= build_function_type_array (t
, n
, args
);
3900 builtin_types
[def
] = t
;
3904 /* Build builtin functions common to both C and C++ language
3908 c_define_builtins (tree va_list_ref_type_node
, tree va_list_arg_type_node
)
3910 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3911 builtin_types[ENUM] = VALUE;
3912 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3913 def_fn_type (ENUM, RETURN, 0, 0);
3914 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3915 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3916 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3917 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3918 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3919 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3920 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3921 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3922 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3923 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3924 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3926 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3927 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3929 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3930 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3932 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3934 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3935 ARG6, ARG7, ARG8, ARG9) \
3936 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3938 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3939 ARG6, ARG7, ARG8, ARG9, ARG10) \
3940 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3941 ARG7, ARG8, ARG9, ARG10);
3942 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3943 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3944 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3945 ARG7, ARG8, ARG9, ARG10, ARG11);
3946 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3947 def_fn_type (ENUM, RETURN, 1, 0);
3948 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3949 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3950 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3951 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3952 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3953 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3954 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3955 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3956 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3957 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3958 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3960 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3961 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3963 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3964 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3965 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3967 #include "builtin-types.def"
3969 #undef DEF_PRIMITIVE_TYPE
3970 #undef DEF_FUNCTION_TYPE_0
3971 #undef DEF_FUNCTION_TYPE_1
3972 #undef DEF_FUNCTION_TYPE_2
3973 #undef DEF_FUNCTION_TYPE_3
3974 #undef DEF_FUNCTION_TYPE_4
3975 #undef DEF_FUNCTION_TYPE_5
3976 #undef DEF_FUNCTION_TYPE_6
3977 #undef DEF_FUNCTION_TYPE_7
3978 #undef DEF_FUNCTION_TYPE_8
3979 #undef DEF_FUNCTION_TYPE_9
3980 #undef DEF_FUNCTION_TYPE_10
3981 #undef DEF_FUNCTION_TYPE_11
3982 #undef DEF_FUNCTION_TYPE_VAR_0
3983 #undef DEF_FUNCTION_TYPE_VAR_1
3984 #undef DEF_FUNCTION_TYPE_VAR_2
3985 #undef DEF_FUNCTION_TYPE_VAR_3
3986 #undef DEF_FUNCTION_TYPE_VAR_4
3987 #undef DEF_FUNCTION_TYPE_VAR_5
3988 #undef DEF_FUNCTION_TYPE_VAR_6
3989 #undef DEF_FUNCTION_TYPE_VAR_7
3990 #undef DEF_POINTER_TYPE
3991 builtin_types
[(int) BT_LAST
] = NULL_TREE
;
3993 c_init_attributes ();
3995 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3996 NONANSI_P, ATTRS, IMPLICIT, COND) \
3998 def_builtin_1 (ENUM, NAME, CLASS, \
3999 builtin_types[(int) TYPE], \
4000 builtin_types[(int) LIBTYPE], \
4001 BOTH_P, FALLBACK_P, NONANSI_P, \
4002 built_in_attributes[(int) ATTRS], IMPLICIT);
4003 #include "builtins.def"
4005 targetm
.init_builtins ();
4007 build_common_builtin_nodes ();
4010 /* Like get_identifier, but avoid warnings about null arguments when
4011 the argument may be NULL for targets where GCC lacks stdint.h type
4015 c_get_ident (const char *id
)
4017 return get_identifier (id
);
4020 /* Build tree nodes and builtin functions common to both C and C++ language
4024 c_common_nodes_and_builtins (void)
4026 int char8_type_size
;
4027 int char16_type_size
;
4028 int char32_type_size
;
4029 int wchar_type_size
;
4030 tree array_domain_type
;
4031 tree va_list_ref_type_node
;
4032 tree va_list_arg_type_node
;
4035 build_common_tree_nodes (flag_signed_char
);
4037 /* Define `int' and `char' first so that dbx will output them first. */
4038 record_builtin_type (RID_INT
, NULL
, integer_type_node
);
4039 record_builtin_type (RID_CHAR
, "char", char_type_node
);
4041 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4042 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4043 but not C. Are the conditionals here needed? */
4044 if (c_dialect_cxx ())
4045 record_builtin_type (RID_SIGNED
, NULL
, integer_type_node
);
4046 record_builtin_type (RID_LONG
, "long int", long_integer_type_node
);
4047 record_builtin_type (RID_UNSIGNED
, "unsigned int", unsigned_type_node
);
4048 record_builtin_type (RID_MAX
, "long unsigned int",
4049 long_unsigned_type_node
);
4051 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
4055 sprintf (name
, "__int%d", int_n_data
[i
].bitsize
);
4056 record_builtin_type ((enum rid
)(RID_FIRST_INT_N
+ i
), name
,
4057 int_n_trees
[i
].signed_type
);
4058 sprintf (name
, "__int%d__", int_n_data
[i
].bitsize
);
4059 record_builtin_type ((enum rid
)(RID_FIRST_INT_N
+ i
), name
,
4060 int_n_trees
[i
].signed_type
);
4062 sprintf (name
, "__int%d unsigned", int_n_data
[i
].bitsize
);
4063 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
4064 sprintf (name
, "__int%d__ unsigned", int_n_data
[i
].bitsize
);
4065 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
4068 if (c_dialect_cxx ())
4069 record_builtin_type (RID_MAX
, "unsigned long", long_unsigned_type_node
);
4070 record_builtin_type (RID_MAX
, "long long int",
4071 long_long_integer_type_node
);
4072 record_builtin_type (RID_MAX
, "long long unsigned int",
4073 long_long_unsigned_type_node
);
4074 if (c_dialect_cxx ())
4075 record_builtin_type (RID_MAX
, "long long unsigned",
4076 long_long_unsigned_type_node
);
4077 record_builtin_type (RID_SHORT
, "short int", short_integer_type_node
);
4078 record_builtin_type (RID_MAX
, "short unsigned int",
4079 short_unsigned_type_node
);
4080 if (c_dialect_cxx ())
4081 record_builtin_type (RID_MAX
, "unsigned short",
4082 short_unsigned_type_node
);
4084 /* Define both `signed char' and `unsigned char'. */
4085 record_builtin_type (RID_MAX
, "signed char", signed_char_type_node
);
4086 record_builtin_type (RID_MAX
, "unsigned char", unsigned_char_type_node
);
4088 /* These are types that c_common_type_for_size and
4089 c_common_type_for_mode use. */
4090 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4091 TYPE_DECL
, NULL_TREE
,
4093 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4094 TYPE_DECL
, NULL_TREE
,
4096 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4097 TYPE_DECL
, NULL_TREE
,
4099 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4100 TYPE_DECL
, NULL_TREE
,
4102 #if HOST_BITS_PER_WIDE_INT >= 64
4103 /* Note that this is different than the __int128 type that's part of
4104 the generic __intN support. */
4105 if (targetm
.scalar_mode_supported_p (TImode
))
4106 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4108 get_identifier ("__int128_t"),
4111 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4112 TYPE_DECL
, NULL_TREE
,
4113 unsigned_intQI_type_node
));
4114 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4115 TYPE_DECL
, NULL_TREE
,
4116 unsigned_intHI_type_node
));
4117 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4118 TYPE_DECL
, NULL_TREE
,
4119 unsigned_intSI_type_node
));
4120 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4121 TYPE_DECL
, NULL_TREE
,
4122 unsigned_intDI_type_node
));
4123 #if HOST_BITS_PER_WIDE_INT >= 64
4124 if (targetm
.scalar_mode_supported_p (TImode
))
4125 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4127 get_identifier ("__uint128_t"),
4128 unsigned_intTI_type_node
));
4131 /* Create the widest literal types. */
4132 if (targetm
.scalar_mode_supported_p (TImode
))
4134 widest_integer_literal_type_node
= intTI_type_node
;
4135 widest_unsigned_literal_type_node
= unsigned_intTI_type_node
;
4139 widest_integer_literal_type_node
= intDI_type_node
;
4140 widest_unsigned_literal_type_node
= unsigned_intDI_type_node
;
4143 signed_size_type_node
= c_common_signed_type (size_type_node
);
4146 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE
)));
4148 record_builtin_type (RID_FLOAT
, NULL
, float_type_node
);
4149 record_builtin_type (RID_DOUBLE
, NULL
, double_type_node
);
4150 record_builtin_type (RID_MAX
, "long double", long_double_type_node
);
4152 if (!c_dialect_cxx ())
4153 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4154 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4155 record_builtin_type ((enum rid
) (RID_FLOATN_NX_FIRST
+ i
), NULL
,
4156 FLOATN_NX_TYPE_NODE (i
));
4158 /* Only supported decimal floating point extension if the target
4159 actually supports underlying modes. */
4160 if (targetm
.scalar_mode_supported_p (SDmode
)
4161 && targetm
.scalar_mode_supported_p (DDmode
)
4162 && targetm
.scalar_mode_supported_p (TDmode
))
4164 record_builtin_type (RID_DFLOAT32
, NULL
, dfloat32_type_node
);
4165 record_builtin_type (RID_DFLOAT64
, NULL
, dfloat64_type_node
);
4166 record_builtin_type (RID_DFLOAT128
, NULL
, dfloat128_type_node
);
4169 if (targetm
.fixed_point_supported_p ())
4171 record_builtin_type (RID_MAX
, "short _Fract", short_fract_type_node
);
4172 record_builtin_type (RID_FRACT
, NULL
, fract_type_node
);
4173 record_builtin_type (RID_MAX
, "long _Fract", long_fract_type_node
);
4174 record_builtin_type (RID_MAX
, "long long _Fract",
4175 long_long_fract_type_node
);
4176 record_builtin_type (RID_MAX
, "unsigned short _Fract",
4177 unsigned_short_fract_type_node
);
4178 record_builtin_type (RID_MAX
, "unsigned _Fract",
4179 unsigned_fract_type_node
);
4180 record_builtin_type (RID_MAX
, "unsigned long _Fract",
4181 unsigned_long_fract_type_node
);
4182 record_builtin_type (RID_MAX
, "unsigned long long _Fract",
4183 unsigned_long_long_fract_type_node
);
4184 record_builtin_type (RID_MAX
, "_Sat short _Fract",
4185 sat_short_fract_type_node
);
4186 record_builtin_type (RID_MAX
, "_Sat _Fract", sat_fract_type_node
);
4187 record_builtin_type (RID_MAX
, "_Sat long _Fract",
4188 sat_long_fract_type_node
);
4189 record_builtin_type (RID_MAX
, "_Sat long long _Fract",
4190 sat_long_long_fract_type_node
);
4191 record_builtin_type (RID_MAX
, "_Sat unsigned short _Fract",
4192 sat_unsigned_short_fract_type_node
);
4193 record_builtin_type (RID_MAX
, "_Sat unsigned _Fract",
4194 sat_unsigned_fract_type_node
);
4195 record_builtin_type (RID_MAX
, "_Sat unsigned long _Fract",
4196 sat_unsigned_long_fract_type_node
);
4197 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Fract",
4198 sat_unsigned_long_long_fract_type_node
);
4199 record_builtin_type (RID_MAX
, "short _Accum", short_accum_type_node
);
4200 record_builtin_type (RID_ACCUM
, NULL
, accum_type_node
);
4201 record_builtin_type (RID_MAX
, "long _Accum", long_accum_type_node
);
4202 record_builtin_type (RID_MAX
, "long long _Accum",
4203 long_long_accum_type_node
);
4204 record_builtin_type (RID_MAX
, "unsigned short _Accum",
4205 unsigned_short_accum_type_node
);
4206 record_builtin_type (RID_MAX
, "unsigned _Accum",
4207 unsigned_accum_type_node
);
4208 record_builtin_type (RID_MAX
, "unsigned long _Accum",
4209 unsigned_long_accum_type_node
);
4210 record_builtin_type (RID_MAX
, "unsigned long long _Accum",
4211 unsigned_long_long_accum_type_node
);
4212 record_builtin_type (RID_MAX
, "_Sat short _Accum",
4213 sat_short_accum_type_node
);
4214 record_builtin_type (RID_MAX
, "_Sat _Accum", sat_accum_type_node
);
4215 record_builtin_type (RID_MAX
, "_Sat long _Accum",
4216 sat_long_accum_type_node
);
4217 record_builtin_type (RID_MAX
, "_Sat long long _Accum",
4218 sat_long_long_accum_type_node
);
4219 record_builtin_type (RID_MAX
, "_Sat unsigned short _Accum",
4220 sat_unsigned_short_accum_type_node
);
4221 record_builtin_type (RID_MAX
, "_Sat unsigned _Accum",
4222 sat_unsigned_accum_type_node
);
4223 record_builtin_type (RID_MAX
, "_Sat unsigned long _Accum",
4224 sat_unsigned_long_accum_type_node
);
4225 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Accum",
4226 sat_unsigned_long_long_accum_type_node
);
4230 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4232 get_identifier ("complex int"),
4233 complex_integer_type_node
));
4234 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4236 get_identifier ("complex float"),
4237 complex_float_type_node
));
4238 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4240 get_identifier ("complex double"),
4241 complex_double_type_node
));
4242 lang_hooks
.decls
.pushdecl
4243 (build_decl (UNKNOWN_LOCATION
,
4244 TYPE_DECL
, get_identifier ("complex long double"),
4245 complex_long_double_type_node
));
4247 if (!c_dialect_cxx ())
4248 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4249 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4252 sprintf (buf
, "complex _Float%d%s", floatn_nx_types
[i
].n
,
4253 floatn_nx_types
[i
].extended
? "x" : "");
4254 lang_hooks
.decls
.pushdecl
4255 (build_decl (UNKNOWN_LOCATION
,
4257 get_identifier (buf
),
4258 COMPLEX_FLOATN_NX_TYPE_NODE (i
)));
4261 /* Make fileptr_type_node a distinct void * type until
4262 FILE type is defined. Likewise for const struct tm*. */
4263 for (unsigned i
= 0;
4264 i
< sizeof (builtin_structptr_types
) / sizeof (builtin_structptr_type
);
4266 builtin_structptr_types
[i
].node
4267 = build_variant_type_copy (builtin_structptr_types
[i
].base
);
4269 record_builtin_type (RID_VOID
, NULL
, void_type_node
);
4271 /* Set the TYPE_NAME for any variants that were built before
4272 record_builtin_type gave names to the built-in types. */
4274 tree void_name
= TYPE_NAME (void_type_node
);
4275 TYPE_NAME (void_type_node
) = NULL_TREE
;
4276 TYPE_NAME (build_qualified_type (void_type_node
, TYPE_QUAL_CONST
))
4278 TYPE_NAME (void_type_node
) = void_name
;
4281 void_list_node
= build_void_list_node ();
4283 /* Make a type to be the domain of a few array types
4284 whose domains don't really matter.
4285 200 is small enough that it always fits in size_t
4286 and large enough that it can hold most function names for the
4287 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4288 array_domain_type
= build_index_type (size_int (200));
4290 /* Make a type for arrays of characters.
4291 With luck nothing will ever really depend on the length of this
4293 char_array_type_node
4294 = build_array_type (char_type_node
, array_domain_type
);
4296 string_type_node
= build_pointer_type (char_type_node
);
4297 const_string_type_node
4298 = build_pointer_type (build_qualified_type
4299 (char_type_node
, TYPE_QUAL_CONST
));
4301 /* This is special for C++ so functions can be overloaded. */
4302 wchar_type_node
= get_identifier (MODIFIED_WCHAR_TYPE
);
4303 wchar_type_node
= TREE_TYPE (identifier_global_value (wchar_type_node
));
4304 wchar_type_size
= TYPE_PRECISION (wchar_type_node
);
4305 underlying_wchar_type_node
= wchar_type_node
;
4306 if (c_dialect_cxx ())
4308 if (TYPE_UNSIGNED (wchar_type_node
))
4309 wchar_type_node
= make_unsigned_type (wchar_type_size
);
4311 wchar_type_node
= make_signed_type (wchar_type_size
);
4312 record_builtin_type (RID_WCHAR
, "wchar_t", wchar_type_node
);
4315 /* This is for wide string constants. */
4316 wchar_array_type_node
4317 = build_array_type (wchar_type_node
, array_domain_type
);
4319 /* Define 'char8_t'. */
4320 char8_type_node
= get_identifier (CHAR8_TYPE
);
4321 char8_type_node
= TREE_TYPE (identifier_global_value (char8_type_node
));
4322 char8_type_size
= TYPE_PRECISION (char8_type_node
);
4323 if (c_dialect_cxx ())
4325 char8_type_node
= make_unsigned_type (char8_type_size
);
4328 record_builtin_type (RID_CHAR8
, "char8_t", char8_type_node
);
4331 /* This is for UTF-8 string constants. */
4332 char8_array_type_node
4333 = build_array_type (char8_type_node
, array_domain_type
);
4335 /* Define 'char16_t'. */
4336 char16_type_node
= get_identifier (CHAR16_TYPE
);
4337 char16_type_node
= TREE_TYPE (identifier_global_value (char16_type_node
));
4338 char16_type_size
= TYPE_PRECISION (char16_type_node
);
4339 if (c_dialect_cxx ())
4341 char16_type_node
= make_unsigned_type (char16_type_size
);
4343 if (cxx_dialect
>= cxx11
)
4344 record_builtin_type (RID_CHAR16
, "char16_t", char16_type_node
);
4347 /* This is for UTF-16 string constants. */
4348 char16_array_type_node
4349 = build_array_type (char16_type_node
, array_domain_type
);
4351 /* Define 'char32_t'. */
4352 char32_type_node
= get_identifier (CHAR32_TYPE
);
4353 char32_type_node
= TREE_TYPE (identifier_global_value (char32_type_node
));
4354 char32_type_size
= TYPE_PRECISION (char32_type_node
);
4355 if (c_dialect_cxx ())
4357 char32_type_node
= make_unsigned_type (char32_type_size
);
4359 if (cxx_dialect
>= cxx11
)
4360 record_builtin_type (RID_CHAR32
, "char32_t", char32_type_node
);
4363 /* This is for UTF-32 string constants. */
4364 char32_array_type_node
4365 = build_array_type (char32_type_node
, array_domain_type
);
4368 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE
)));
4371 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE
)));
4373 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE
)));
4375 if (SIG_ATOMIC_TYPE
)
4376 sig_atomic_type_node
=
4377 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE
)));
4380 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE
)));
4383 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE
)));
4386 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE
)));
4389 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE
)));
4392 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE
)));
4394 c_uint16_type_node
= uint16_type_node
=
4395 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE
)));
4397 c_uint32_type_node
= uint32_type_node
=
4398 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE
)));
4400 c_uint64_type_node
= uint64_type_node
=
4401 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE
)));
4402 if (INT_LEAST8_TYPE
)
4403 int_least8_type_node
=
4404 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE
)));
4405 if (INT_LEAST16_TYPE
)
4406 int_least16_type_node
=
4407 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE
)));
4408 if (INT_LEAST32_TYPE
)
4409 int_least32_type_node
=
4410 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE
)));
4411 if (INT_LEAST64_TYPE
)
4412 int_least64_type_node
=
4413 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE
)));
4414 if (UINT_LEAST8_TYPE
)
4415 uint_least8_type_node
=
4416 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE
)));
4417 if (UINT_LEAST16_TYPE
)
4418 uint_least16_type_node
=
4419 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE
)));
4420 if (UINT_LEAST32_TYPE
)
4421 uint_least32_type_node
=
4422 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE
)));
4423 if (UINT_LEAST64_TYPE
)
4424 uint_least64_type_node
=
4425 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE
)));
4427 int_fast8_type_node
=
4428 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE
)));
4429 if (INT_FAST16_TYPE
)
4430 int_fast16_type_node
=
4431 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE
)));
4432 if (INT_FAST32_TYPE
)
4433 int_fast32_type_node
=
4434 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE
)));
4435 if (INT_FAST64_TYPE
)
4436 int_fast64_type_node
=
4437 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE
)));
4438 if (UINT_FAST8_TYPE
)
4439 uint_fast8_type_node
=
4440 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE
)));
4441 if (UINT_FAST16_TYPE
)
4442 uint_fast16_type_node
=
4443 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE
)));
4444 if (UINT_FAST32_TYPE
)
4445 uint_fast32_type_node
=
4446 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE
)));
4447 if (UINT_FAST64_TYPE
)
4448 uint_fast64_type_node
=
4449 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE
)));
4452 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE
)));
4455 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE
)));
4457 default_function_type
4458 = build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4459 unsigned_ptrdiff_type_node
= c_common_unsigned_type (ptrdiff_type_node
);
4461 lang_hooks
.decls
.pushdecl
4462 (build_decl (UNKNOWN_LOCATION
,
4463 TYPE_DECL
, get_identifier ("__builtin_va_list"),
4464 va_list_type_node
));
4465 if (targetm
.enum_va_list_p
)
4471 for (l
= 0; targetm
.enum_va_list_p (l
, &pname
, &ptype
); ++l
)
4473 lang_hooks
.decls
.pushdecl
4474 (build_decl (UNKNOWN_LOCATION
,
4475 TYPE_DECL
, get_identifier (pname
),
4481 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4483 va_list_arg_type_node
= va_list_ref_type_node
=
4484 build_pointer_type (TREE_TYPE (va_list_type_node
));
4488 va_list_arg_type_node
= va_list_type_node
;
4489 va_list_ref_type_node
= build_reference_type (va_list_type_node
);
4492 c_define_builtins (va_list_ref_type_node
, va_list_arg_type_node
);
4494 main_identifier_node
= get_identifier ("main");
4496 /* Create the built-in __null node. It is important that this is
4498 null_node
= make_int_cst (1, 1);
4499 TREE_TYPE (null_node
) = c_common_type_for_size (POINTER_SIZE
, 0);
4501 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4502 memset (builtin_types
, 0, sizeof (builtin_types
));
4505 /* The number of named compound-literals generated thus far. */
4506 static GTY(()) int compound_literal_number
;
4508 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4511 set_compound_literal_name (tree decl
)
4514 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
4515 compound_literal_number
);
4516 compound_literal_number
++;
4517 DECL_NAME (decl
) = get_identifier (name
);
4520 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4521 TYPE and operand OP. */
4524 build_va_arg_1 (location_t loc
, tree type
, tree op
)
4526 tree expr
= build1 (VA_ARG_EXPR
, type
, op
);
4527 SET_EXPR_LOCATION (expr
, loc
);
4531 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4532 va_arg (EXPR, TYPE) at source location LOC. */
4535 build_va_arg (location_t loc
, tree expr
, tree type
)
4537 tree va_type
= TREE_TYPE (expr
);
4538 tree canon_va_type
= (va_type
== error_mark_node
4540 : targetm
.canonical_va_list_type (va_type
));
4542 if (va_type
== error_mark_node
4543 || canon_va_type
== NULL_TREE
)
4545 if (canon_va_type
== NULL_TREE
)
4546 error_at (loc
, "first argument to %<va_arg%> not of type %<va_list%>");
4548 /* Let's handle things neutrallly, if expr:
4549 - has undeclared type, or
4550 - is not an va_list type. */
4551 return build_va_arg_1 (loc
, type
, error_mark_node
);
4554 if (TREE_CODE (canon_va_type
) != ARRAY_TYPE
)
4556 /* Case 1: Not an array type. */
4558 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4560 mark_addressable (expr
);
4561 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (expr
)), expr
);
4563 return build_va_arg_1 (loc
, type
, expr
);
4566 /* Case 2: Array type.
4570 For contrast, let's start with the simple case (case 1). If
4571 canon_va_type is not an array type, but say a char *, then when
4572 passing-by-value a va_list, the type of the va_list param decl is
4573 the same as for another va_list decl (all ap's are char *):
4576 D.1815 = VA_ARG (&ap, 0B, 1);
4582 __builtin_va_start (&ap, 0);
4585 __builtin_va_end (&ap);
4589 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4590 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4591 the same as for another va_list decl (case 2a, struct ap[1]).
4594 D.1844 = VA_ARG (ap, 0B, 0);
4599 __builtin_va_start (&ap, 0);
4601 __builtin_va_end (&ap);
4605 Case 2b is different because:
4606 - on the callee side, the parm decl has declared type va_list, but
4607 grokdeclarator changes the type of the parm decl to a pointer to the
4609 - on the caller side, the pass-by-value uses &ap.
4611 We unify these two cases (case 2a: va_list is array type,
4612 case 2b: va_list is pointer to array elem type), by adding '&' for the
4613 array type case, such that we have a pointer to array elem in both
4616 if (TREE_CODE (va_type
) == ARRAY_TYPE
)
4618 /* Case 2a: va_list is array type. */
4620 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4622 mark_addressable (expr
);
4623 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (canon_va_type
)),
4626 /* Verify that &ap is still recognized as having va_list type. */
4627 tree canon_expr_type
4628 = targetm
.canonical_va_list_type (TREE_TYPE (expr
));
4629 gcc_assert (canon_expr_type
!= NULL_TREE
);
4633 /* Case 2b: va_list is pointer to array elem type. */
4634 gcc_assert (POINTER_TYPE_P (va_type
));
4636 /* Comparison as in std_canonical_va_list_type. */
4637 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type
))
4638 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type
)));
4640 /* Don't take the address. We've already got '&ap'. */
4644 return build_va_arg_1 (loc
, type
, expr
);
4648 /* Linked list of disabled built-in functions. */
4650 struct disabled_builtin
4653 struct disabled_builtin
*next
;
4655 static disabled_builtin
*disabled_builtins
= NULL
;
4657 static bool builtin_function_disabled_p (const char *);
4659 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4660 begins with "__builtin_", give an error. */
4663 disable_builtin_function (const char *name
)
4665 if (strncmp (name
, "__builtin_", strlen ("__builtin_")) == 0)
4666 error ("cannot disable built-in function %qs", name
);
4669 disabled_builtin
*new_disabled_builtin
= XNEW (disabled_builtin
);
4670 new_disabled_builtin
->name
= name
;
4671 new_disabled_builtin
->next
= disabled_builtins
;
4672 disabled_builtins
= new_disabled_builtin
;
4677 /* Return true if the built-in function NAME has been disabled, false
4681 builtin_function_disabled_p (const char *name
)
4683 disabled_builtin
*p
;
4684 for (p
= disabled_builtins
; p
!= NULL
; p
= p
->next
)
4686 if (strcmp (name
, p
->name
) == 0)
4693 /* Worker for DEF_BUILTIN.
4694 Possibly define a builtin function with one or two names.
4695 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4696 nonansi_p and flag_no_nonansi_builtin. */
4699 def_builtin_1 (enum built_in_function fncode
,
4701 enum built_in_class fnclass
,
4702 tree fntype
, tree libtype
,
4703 bool both_p
, bool fallback_p
, bool nonansi_p
,
4704 tree fnattrs
, bool implicit_p
)
4707 const char *libname
;
4709 if (fntype
== error_mark_node
)
4712 gcc_assert ((!both_p
&& !fallback_p
)
4713 || !strncmp (name
, "__builtin_",
4714 strlen ("__builtin_")));
4716 libname
= name
+ strlen ("__builtin_");
4717 decl
= add_builtin_function (name
, fntype
, fncode
, fnclass
,
4718 (fallback_p
? libname
: NULL
),
4721 set_builtin_decl (fncode
, decl
, implicit_p
);
4724 && !flag_no_builtin
&& !builtin_function_disabled_p (libname
)
4725 && !(nonansi_p
&& flag_no_nonansi_builtin
))
4726 add_builtin_function (libname
, libtype
, fncode
, fnclass
,
4730 /* Nonzero if the type T promotes to int. This is (nearly) the
4731 integral promotions defined in ISO C99 6.3.1.1/2. */
4734 c_promoting_integer_type_p (const_tree t
)
4736 switch (TREE_CODE (t
))
4739 return (TYPE_MAIN_VARIANT (t
) == char_type_node
4740 || TYPE_MAIN_VARIANT (t
) == signed_char_type_node
4741 || TYPE_MAIN_VARIANT (t
) == unsigned_char_type_node
4742 || TYPE_MAIN_VARIANT (t
) == short_integer_type_node
4743 || TYPE_MAIN_VARIANT (t
) == short_unsigned_type_node
4744 || TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
));
4747 /* ??? Technically all enumerations not larger than an int
4748 promote to an int. But this is used along code paths
4749 that only want to notice a size change. */
4750 return TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
);
4760 /* Return 1 if PARMS specifies a fixed number of parameters
4761 and none of their types is affected by default promotions. */
4764 self_promoting_args_p (const_tree parms
)
4767 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
4769 tree type
= TREE_VALUE (t
);
4771 if (type
== error_mark_node
)
4774 if (TREE_CHAIN (t
) == NULL_TREE
&& type
!= void_type_node
)
4777 if (type
== NULL_TREE
)
4780 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
4783 if (c_promoting_integer_type_p (type
))
4789 /* Recursively remove any '*' or '&' operator from TYPE. */
4791 strip_pointer_operator (tree t
)
4793 while (POINTER_TYPE_P (t
))
4798 /* Recursively remove pointer or array type from TYPE. */
4800 strip_pointer_or_array_types (tree t
)
4802 while (TREE_CODE (t
) == ARRAY_TYPE
|| POINTER_TYPE_P (t
))
4807 /* Used to compare case labels. K1 and K2 are actually tree nodes
4808 representing case labels, or NULL_TREE for a `default' label.
4809 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4810 K2, and 0 if K1 and K2 are equal. */
4813 case_compare (splay_tree_key k1
, splay_tree_key k2
)
4815 /* Consider a NULL key (such as arises with a `default' label) to be
4816 smaller than anything else. */
4822 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
4825 /* Process a case label, located at LOC, for the range LOW_VALUE
4826 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4827 then this case label is actually a `default' label. If only
4828 HIGH_VALUE is NULL_TREE, then case label was declared using the
4829 usual C/C++ syntax, rather than the GNU case range extension.
4830 CASES is a tree containing all the case ranges processed so far;
4831 COND is the condition for the switch-statement itself.
4832 Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
4833 CASE_LABEL_EXPR is created. */
4836 c_add_case_label (location_t loc
, splay_tree cases
, tree cond
,
4837 tree low_value
, tree high_value
)
4842 splay_tree_node node
;
4844 /* Create the LABEL_DECL itself. */
4845 label
= create_artificial_label (loc
);
4847 /* If there was an error processing the switch condition, bail now
4848 before we get more confused. */
4849 if (!cond
|| cond
== error_mark_node
)
4852 if ((low_value
&& TREE_TYPE (low_value
)
4853 && POINTER_TYPE_P (TREE_TYPE (low_value
)))
4854 || (high_value
&& TREE_TYPE (high_value
)
4855 && POINTER_TYPE_P (TREE_TYPE (high_value
))))
4857 error_at (loc
, "pointers are not permitted as case values");
4861 /* Case ranges are a GNU extension. */
4863 pedwarn (loc
, OPT_Wpedantic
,
4864 "range expressions in switch statements are non-standard");
4866 type
= TREE_TYPE (cond
);
4869 low_value
= check_case_value (loc
, low_value
);
4870 low_value
= convert_and_check (loc
, type
, low_value
);
4871 low_value
= fold (low_value
);
4872 if (low_value
== error_mark_node
)
4877 high_value
= check_case_value (loc
, high_value
);
4878 high_value
= convert_and_check (loc
, type
, high_value
);
4879 high_value
= fold (high_value
);
4880 if (high_value
== error_mark_node
)
4884 if (low_value
&& high_value
)
4886 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4887 really a case range, even though it was written that way.
4888 Remove the HIGH_VALUE to simplify later processing. */
4889 if (tree_int_cst_equal (low_value
, high_value
))
4890 high_value
= NULL_TREE
;
4891 else if (!tree_int_cst_lt (low_value
, high_value
))
4892 warning_at (loc
, 0, "empty range specified");
4895 /* Look up the LOW_VALUE in the table of case labels we already
4897 node
= splay_tree_lookup (cases
, (splay_tree_key
) low_value
);
4898 /* If there was not an exact match, check for overlapping ranges.
4899 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4900 that's a `default' label and the only overlap is an exact match. */
4901 if (!node
&& (low_value
|| high_value
))
4903 splay_tree_node low_bound
;
4904 splay_tree_node high_bound
;
4906 /* Even though there wasn't an exact match, there might be an
4907 overlap between this case range and another case range.
4908 Since we've (inductively) not allowed any overlapping case
4909 ranges, we simply need to find the greatest low case label
4910 that is smaller that LOW_VALUE, and the smallest low case
4911 label that is greater than LOW_VALUE. If there is an overlap
4912 it will occur in one of these two ranges. */
4913 low_bound
= splay_tree_predecessor (cases
,
4914 (splay_tree_key
) low_value
);
4915 high_bound
= splay_tree_successor (cases
,
4916 (splay_tree_key
) low_value
);
4918 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4919 the LOW_VALUE, so there is no need to check unless the
4920 LOW_BOUND is in fact itself a case range. */
4922 && CASE_HIGH ((tree
) low_bound
->value
)
4923 && tree_int_cst_compare (CASE_HIGH ((tree
) low_bound
->value
),
4926 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4927 range is bigger than the low end of the current range, so we
4928 are only interested if the current range is a real range, and
4929 not an ordinary case label. */
4932 && (tree_int_cst_compare ((tree
) high_bound
->key
,
4937 /* If there was an overlap, issue an error. */
4940 tree duplicate
= CASE_LABEL ((tree
) node
->value
);
4944 error_at (loc
, "duplicate (or overlapping) case value");
4945 inform (DECL_SOURCE_LOCATION (duplicate
),
4946 "this is the first entry overlapping that value");
4950 error_at (loc
, "duplicate case value") ;
4951 inform (DECL_SOURCE_LOCATION (duplicate
), "previously used here");
4955 error_at (loc
, "multiple default labels in one switch");
4956 inform (DECL_SOURCE_LOCATION (duplicate
),
4957 "this is the first default label");
4962 /* Add a CASE_LABEL to the statement-tree. */
4963 case_label
= add_stmt (build_case_label (low_value
, high_value
, label
));
4964 /* Register this case label in the splay tree. */
4965 splay_tree_insert (cases
,
4966 (splay_tree_key
) low_value
,
4967 (splay_tree_value
) case_label
);
4972 /* Add a label so that the back-end doesn't think that the beginning of
4973 the switch is unreachable. Note that we do not add a case label, as
4974 that just leads to duplicates and thence to failure later on. */
4977 tree t
= create_artificial_label (loc
);
4978 add_stmt (build_stmt (loc
, LABEL_EXPR
, t
));
4980 return error_mark_node
;
4983 /* Subroutine of c_switch_covers_all_cases_p, called via
4984 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4985 ARGS[0] is initially NULL and after the first iteration is the
4986 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4990 c_switch_covers_all_cases_p_1 (splay_tree_node node
, void *data
)
4992 tree label
= (tree
) node
->value
;
4993 tree
*args
= (tree
*) data
;
4995 /* If there is a default case, we shouldn't have called this. */
4996 gcc_assert (CASE_LOW (label
));
4998 if (args
[0] == NULL_TREE
)
5000 if (wi::to_widest (args
[1]) < wi::to_widest (CASE_LOW (label
)))
5003 else if (wi::add (wi::to_widest (args
[0]), 1)
5004 != wi::to_widest (CASE_LOW (label
)))
5006 if (CASE_HIGH (label
))
5007 args
[0] = CASE_HIGH (label
);
5009 args
[0] = CASE_LOW (label
);
5013 /* Return true if switch with CASES and switch condition with type
5014 covers all possible values in the case labels. */
5017 c_switch_covers_all_cases_p (splay_tree cases
, tree type
)
5019 /* If there is default:, this is always the case. */
5020 splay_tree_node default_node
5021 = splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
5025 if (!INTEGRAL_TYPE_P (type
))
5028 tree args
[2] = { NULL_TREE
, TYPE_MIN_VALUE (type
) };
5029 if (splay_tree_foreach (cases
, c_switch_covers_all_cases_p_1
, args
))
5032 /* If there are no cases at all, or if the highest case label
5033 is smaller than TYPE_MAX_VALUE, return false. */
5034 if (args
[0] == NULL_TREE
5035 || wi::to_widest (args
[0]) < wi::to_widest (TYPE_MAX_VALUE (type
)))
5041 /* Finish an expression taking the address of LABEL (an
5042 IDENTIFIER_NODE). Returns an expression for the address.
5044 LOC is the location for the expression returned. */
5047 finish_label_address_expr (tree label
, location_t loc
)
5051 pedwarn (input_location
, OPT_Wpedantic
, "taking the address of a label is non-standard");
5053 if (label
== error_mark_node
)
5054 return error_mark_node
;
5056 label
= lookup_label (label
);
5057 if (label
== NULL_TREE
)
5058 result
= null_pointer_node
;
5061 TREE_USED (label
) = 1;
5062 result
= build1 (ADDR_EXPR
, ptr_type_node
, label
);
5063 /* The current function is not necessarily uninlinable.
5064 Computed gotos are incompatible with inlining, but the value
5065 here could be used only in a diagnostic, for example. */
5066 protected_set_expr_location (result
, loc
);
5073 /* Given a boolean expression ARG, return a tree representing an increment
5074 or decrement (as indicated by CODE) of ARG. The front end must check for
5075 invalid cases (e.g., decrement in C++). */
5077 boolean_increment (enum tree_code code
, tree arg
)
5080 tree true_res
= build_int_cst (TREE_TYPE (arg
), 1);
5082 arg
= stabilize_reference (arg
);
5085 case PREINCREMENT_EXPR
:
5086 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5088 case POSTINCREMENT_EXPR
:
5089 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5090 arg
= save_expr (arg
);
5091 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5092 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5094 case PREDECREMENT_EXPR
:
5095 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5096 invert_truthvalue_loc (input_location
, arg
));
5098 case POSTDECREMENT_EXPR
:
5099 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5100 invert_truthvalue_loc (input_location
, arg
));
5101 arg
= save_expr (arg
);
5102 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5103 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5108 TREE_SIDE_EFFECTS (val
) = 1;
5112 /* Built-in macros for stddef.h and stdint.h, that require macros
5113 defined in this file. */
5115 c_stddef_cpp_builtins(void)
5117 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE
, 0);
5118 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
, 0);
5119 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE
, 0);
5120 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE
, 0);
5121 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE
, 0);
5122 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE
, 0);
5124 builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE
, 0);
5125 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE
, 0);
5126 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE
, 0);
5127 if (SIG_ATOMIC_TYPE
)
5128 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE
, 0);
5130 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE
, 0);
5132 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE
, 0);
5134 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE
, 0);
5136 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE
, 0);
5138 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE
, 0);
5140 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE
, 0);
5142 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE
, 0);
5144 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE
, 0);
5145 if (INT_LEAST8_TYPE
)
5146 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE
, 0);
5147 if (INT_LEAST16_TYPE
)
5148 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE
, 0);
5149 if (INT_LEAST32_TYPE
)
5150 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE
, 0);
5151 if (INT_LEAST64_TYPE
)
5152 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE
, 0);
5153 if (UINT_LEAST8_TYPE
)
5154 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE
, 0);
5155 if (UINT_LEAST16_TYPE
)
5156 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE
, 0);
5157 if (UINT_LEAST32_TYPE
)
5158 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE
, 0);
5159 if (UINT_LEAST64_TYPE
)
5160 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE
, 0);
5162 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE
, 0);
5163 if (INT_FAST16_TYPE
)
5164 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE
, 0);
5165 if (INT_FAST32_TYPE
)
5166 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE
, 0);
5167 if (INT_FAST64_TYPE
)
5168 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE
, 0);
5169 if (UINT_FAST8_TYPE
)
5170 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE
, 0);
5171 if (UINT_FAST16_TYPE
)
5172 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE
, 0);
5173 if (UINT_FAST32_TYPE
)
5174 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE
, 0);
5175 if (UINT_FAST64_TYPE
)
5176 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE
, 0);
5178 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE
, 0);
5180 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE
, 0);
5181 /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5182 Expose it as __SIZETYPE__. */
5184 builtin_define_with_value ("__SIZETYPE__", SIZETYPE
, 0);
5188 c_init_attributes (void)
5190 /* Fill in the built_in_attributes array. */
5191 #define DEF_ATTR_NULL_TREE(ENUM) \
5192 built_in_attributes[(int) ENUM] = NULL_TREE;
5193 #define DEF_ATTR_INT(ENUM, VALUE) \
5194 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5195 #define DEF_ATTR_STRING(ENUM, VALUE) \
5196 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5197 #define DEF_ATTR_IDENT(ENUM, STRING) \
5198 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5199 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5200 built_in_attributes[(int) ENUM] \
5201 = tree_cons (built_in_attributes[(int) PURPOSE], \
5202 built_in_attributes[(int) VALUE], \
5203 built_in_attributes[(int) CHAIN]);
5204 #include "builtin-attrs.def"
5205 #undef DEF_ATTR_NULL_TREE
5207 #undef DEF_ATTR_IDENT
5208 #undef DEF_ATTR_TREE_LIST
5211 /* Check whether the byte alignment ALIGN is a valid user-specified
5212 alignment less than the supported maximum. If so, return ALIGN's
5213 base-2 log; if not, output an error and return -1. If OBJFILE
5214 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5215 converted to bits. Otherwise, consider valid only alignments
5216 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5217 Zero is not considered a valid argument (and results in -1 on
5218 return) but it only triggers a warning when WARN_ZERO is set. */
5221 check_user_alignment (const_tree align
, bool objfile
, bool warn_zero
)
5223 if (error_operand_p (align
))
5226 if (TREE_CODE (align
) != INTEGER_CST
5227 || !INTEGRAL_TYPE_P (TREE_TYPE (align
)))
5229 error ("requested alignment is not an integer constant");
5233 if (integer_zerop (align
))
5236 warning (OPT_Wattributes
,
5237 "requested alignment %qE is not a positive power of 2",
5242 /* Log2 of the byte alignment ALIGN. */
5244 if (tree_int_cst_sgn (align
) == -1
5245 || (log2align
= tree_log2 (align
)) == -1)
5247 error ("requested alignment %qE is not a positive power of 2",
5254 unsigned maxalign
= MAX_OFILE_ALIGNMENT
/ BITS_PER_UNIT
;
5255 if (!tree_fits_uhwi_p (align
) || tree_to_uhwi (align
) > maxalign
)
5257 error ("requested alignment %qE exceeds object file maximum %u",
5263 if (log2align
>= HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
)
5265 error ("requested alignment %qE exceeds maximum %u",
5266 align
, 1U << (HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
- 1));
5273 /* Determine the ELF symbol visibility for DECL, which is either a
5274 variable or a function. It is an error to use this function if a
5275 definition of DECL is not available in this translation unit.
5276 Returns true if the final visibility has been determined by this
5277 function; false if the caller is free to make additional
5281 c_determine_visibility (tree decl
)
5283 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
5285 /* If the user explicitly specified the visibility with an
5286 attribute, honor that. DECL_VISIBILITY will have been set during
5287 the processing of the attribute. We check for an explicit
5288 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5289 to distinguish the use of an attribute from the use of a "#pragma
5290 GCC visibility push(...)"; in the latter case we still want other
5291 considerations to be able to overrule the #pragma. */
5292 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl
))
5293 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5294 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
))
5295 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))))
5298 /* Set default visibility to whatever the user supplied with
5299 visibility_specified depending on #pragma GCC visibility. */
5300 if (!DECL_VISIBILITY_SPECIFIED (decl
))
5302 if (visibility_options
.inpragma
5303 || DECL_VISIBILITY (decl
) != default_visibility
)
5305 DECL_VISIBILITY (decl
) = default_visibility
;
5306 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
5307 /* If visibility changed and DECL already has DECL_RTL, ensure
5308 symbol flags are updated. */
5309 if (((VAR_P (decl
) && TREE_STATIC (decl
))
5310 || TREE_CODE (decl
) == FUNCTION_DECL
)
5311 && DECL_RTL_SET_P (decl
))
5312 make_decl_rtl (decl
);
5318 /* Data to communicate through check_function_arguments_recurse between
5319 check_function_nonnull and check_nonnull_arg. */
5321 struct nonnull_arg_ctx
5327 /* Check the argument list of a function call for null in argument slots
5328 that are marked as requiring a non-null pointer argument. The NARGS
5329 arguments are passed in the array ARGARRAY. Return true if we have
5333 check_function_nonnull (location_t loc
, tree attrs
, int nargs
, tree
*argarray
)
5338 attrs
= lookup_attribute ("nonnull", attrs
);
5339 if (attrs
== NULL_TREE
)
5343 /* See if any of the nonnull attributes has no arguments. If so,
5344 then every pointer argument is checked (in which case the check
5345 for pointer type is done in check_nonnull_arg). */
5346 if (TREE_VALUE (a
) != NULL_TREE
)
5348 a
= lookup_attribute ("nonnull", TREE_CHAIN (a
));
5349 while (a
!= NULL_TREE
&& TREE_VALUE (a
) != NULL_TREE
);
5351 struct nonnull_arg_ctx ctx
= { loc
, false };
5353 for (i
= 0; i
< nargs
; i
++)
5354 check_function_arguments_recurse (check_nonnull_arg
, &ctx
, argarray
[i
],
5358 /* Walk the argument list. If we encounter an argument number we
5359 should check for non-null, do it. */
5360 for (i
= 0; i
< nargs
; i
++)
5362 for (a
= attrs
; ; a
= TREE_CHAIN (a
))
5364 a
= lookup_attribute ("nonnull", a
);
5365 if (a
== NULL_TREE
|| nonnull_check_p (TREE_VALUE (a
), i
+ 1))
5370 check_function_arguments_recurse (check_nonnull_arg
, &ctx
,
5371 argarray
[i
], i
+ 1);
5374 return ctx
.warned_p
;
5377 /* Check that the Nth argument of a function call (counting backwards
5378 from the end) is a (pointer)0. The NARGS arguments are passed in the
5382 check_function_sentinel (const_tree fntype
, int nargs
, tree
*argarray
)
5384 tree attr
= lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype
));
5391 function_args_iterator iter
;
5394 /* Skip over the named arguments. */
5395 FOREACH_FUNCTION_ARGS (fntype
, t
, iter
)
5402 if (TREE_VALUE (attr
))
5404 tree p
= TREE_VALUE (TREE_VALUE (attr
));
5405 pos
= TREE_INT_CST_LOW (p
);
5408 /* The sentinel must be one of the varargs, i.e.
5409 in position >= the number of fixed arguments. */
5410 if ((nargs
- 1 - pos
) < len
)
5412 warning (OPT_Wformat_
,
5413 "not enough variable arguments to fit a sentinel");
5417 /* Validate the sentinel. */
5418 sentinel
= fold_for_warn (argarray
[nargs
- 1 - pos
]);
5419 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel
))
5420 || !integer_zerop (sentinel
))
5421 /* Although __null (in C++) is only an integer we allow it
5422 nevertheless, as we are guaranteed that it's exactly
5423 as wide as a pointer, and we don't want to force
5424 users to cast the NULL they have written there.
5425 We warn with -Wstrict-null-sentinel, though. */
5426 && (warn_strict_null_sentinel
|| null_node
!= sentinel
))
5427 warning (OPT_Wformat_
, "missing sentinel in function call");
5431 /* Check that the same argument isn't passed to two or more
5432 restrict-qualified formal and issue a -Wrestrict warning
5433 if it is. Return true if a warning has been issued. */
5436 check_function_restrict (const_tree fndecl
, const_tree fntype
,
5437 int nargs
, tree
*unfolded_argarray
)
5440 tree parms
= TYPE_ARG_TYPES (fntype
);
5442 /* Call fold_for_warn on all of the arguments. */
5443 auto_vec
<tree
> argarray (nargs
);
5444 for (i
= 0; i
< nargs
; i
++)
5445 argarray
.quick_push (fold_for_warn (unfolded_argarray
[i
]));
5448 && TREE_CODE (fndecl
) == FUNCTION_DECL
)
5450 /* Avoid diagnosing calls built-ins with a zero size/bound
5451 here. They are checked in more detail elsewhere. */
5452 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
5454 && TREE_CODE (argarray
[2]) == INTEGER_CST
5455 && integer_zerop (argarray
[2]))
5458 if (DECL_ARGUMENTS (fndecl
))
5459 parms
= DECL_ARGUMENTS (fndecl
);
5462 for (i
= 0; i
< nargs
; i
++)
5463 TREE_VISITED (argarray
[i
]) = 0;
5465 bool warned
= false;
5467 for (i
= 0; i
< nargs
&& parms
&& parms
!= void_list_node
; i
++)
5470 if (TREE_CODE (parms
) == PARM_DECL
)
5472 type
= TREE_TYPE (parms
);
5473 parms
= DECL_CHAIN (parms
);
5477 type
= TREE_VALUE (parms
);
5478 parms
= TREE_CHAIN (parms
);
5480 if (POINTER_TYPE_P (type
)
5481 && TYPE_RESTRICT (type
)
5482 && !TYPE_READONLY (TREE_TYPE (type
)))
5483 warned
|= warn_for_restrict (i
, argarray
.address (), nargs
);
5486 for (i
= 0; i
< nargs
; i
++)
5487 TREE_VISITED (argarray
[i
]) = 0;
5492 /* Helper for check_function_nonnull; given a list of operands which
5493 must be non-null in ARGS, determine if operand PARAM_NUM should be
5497 nonnull_check_p (tree args
, unsigned HOST_WIDE_INT param_num
)
5499 unsigned HOST_WIDE_INT arg_num
= 0;
5501 for (; args
; args
= TREE_CHAIN (args
))
5503 bool found
= get_attribute_operand (TREE_VALUE (args
), &arg_num
);
5507 if (arg_num
== param_num
)
5513 /* Check that the function argument PARAM (which is operand number
5514 PARAM_NUM) is non-null. This is called by check_function_nonnull
5515 via check_function_arguments_recurse. */
5518 check_nonnull_arg (void *ctx
, tree param
, unsigned HOST_WIDE_INT param_num
)
5520 struct nonnull_arg_ctx
*pctx
= (struct nonnull_arg_ctx
*) ctx
;
5522 /* Just skip checking the argument if it's not a pointer. This can
5523 happen if the "nonnull" attribute was given without an operand
5524 list (which means to check every pointer argument). */
5526 if (TREE_CODE (TREE_TYPE (param
)) != POINTER_TYPE
)
5529 /* Diagnose the simple cases of null arguments. */
5530 if (integer_zerop (fold_for_warn (param
)))
5532 warning_at (pctx
->loc
, OPT_Wnonnull
, "null argument where non-null "
5533 "required (argument %lu)", (unsigned long) param_num
);
5534 pctx
->warned_p
= true;
5538 /* Helper for attribute handling; fetch the operand number from
5539 the attribute argument list. */
5542 get_attribute_operand (tree arg_num_expr
, unsigned HOST_WIDE_INT
*valp
)
5544 /* Verify the arg number is a small constant. */
5545 if (tree_fits_uhwi_p (arg_num_expr
))
5547 *valp
= tree_to_uhwi (arg_num_expr
);
5554 /* Arguments being collected for optimization. */
5555 typedef const char *const_char_p
; /* For DEF_VEC_P. */
5556 static GTY(()) vec
<const_char_p
, va_gc
> *optimize_args
;
5559 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5560 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5561 false for #pragma GCC optimize. */
5564 parse_optimize_options (tree args
, bool attr_p
)
5569 const char **opt_argv
;
5570 struct cl_decoded_option
*decoded_options
;
5571 unsigned int decoded_options_count
;
5574 /* Build up argv vector. Just in case the string is stored away, use garbage
5575 collected strings. */
5576 vec_safe_truncate (optimize_args
, 0);
5577 vec_safe_push (optimize_args
, (const char *) NULL
);
5579 for (ap
= args
; ap
!= NULL_TREE
; ap
= TREE_CHAIN (ap
))
5581 tree value
= TREE_VALUE (ap
);
5583 if (TREE_CODE (value
) == INTEGER_CST
)
5586 sprintf (buffer
, "-O%ld", (long) TREE_INT_CST_LOW (value
));
5587 vec_safe_push (optimize_args
, ggc_strdup (buffer
));
5590 else if (TREE_CODE (value
) == STRING_CST
)
5592 /* Split string into multiple substrings. */
5593 size_t len
= TREE_STRING_LENGTH (value
);
5594 char *p
= ASTRDUP (TREE_STRING_POINTER (value
));
5595 char *end
= p
+ len
;
5599 while (next_p
!= NULL
)
5605 comma
= strchr (p
, ',');
5618 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5620 if (*p
== '-' && p
[1] != 'O' && p
[1] != 'f')
5624 warning (OPT_Wattributes
,
5625 "bad option %qs to attribute %<optimize%>", p
);
5627 warning (OPT_Wpragmas
,
5628 "bad option %qs to pragma %<optimize%>", p
);
5632 /* Can't use GC memory here, see PR88007. */
5633 r
= q
= XOBNEWVEC (&opts_obstack
, char, len2
+ 3);
5639 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5640 itself is -Os, and any other switch begins with a -f. */
5641 if ((*p
>= '0' && *p
<= '9')
5642 || (p
[0] == 's' && p
[1] == '\0'))
5648 memcpy (r
, p
, len2
);
5650 vec_safe_push (optimize_args
, (const char *) q
);
5656 opt_argc
= optimize_args
->length ();
5657 opt_argv
= (const char **) alloca (sizeof (char *) * (opt_argc
+ 1));
5659 for (i
= 1; i
< opt_argc
; i
++)
5660 opt_argv
[i
] = (*optimize_args
)[i
];
5662 /* Now parse the options. */
5663 decode_cmdline_options_to_array_default_mask (opt_argc
, opt_argv
,
5665 &decoded_options_count
);
5666 /* Drop non-Optimization options. */
5668 for (i
= 1; i
< decoded_options_count
; ++i
)
5670 if (! (cl_options
[decoded_options
[i
].opt_index
].flags
& CL_OPTIMIZATION
))
5674 warning (OPT_Wattributes
,
5675 "bad option %qs to attribute %<optimize%>",
5676 decoded_options
[i
].orig_option_with_args_text
);
5678 warning (OPT_Wpragmas
,
5679 "bad option %qs to pragma %<optimize%>",
5680 decoded_options
[i
].orig_option_with_args_text
);
5684 decoded_options
[j
] = decoded_options
[i
];
5687 decoded_options_count
= j
;
5688 /* And apply them. */
5689 decode_options (&global_options
, &global_options_set
,
5690 decoded_options
, decoded_options_count
,
5691 input_location
, global_dc
, NULL
);
5693 targetm
.override_options_after_change();
5695 optimize_args
->truncate (0);
5699 /* Check whether ATTR is a valid attribute fallthrough. */
5702 attribute_fallthrough_p (tree attr
)
5704 if (attr
== error_mark_node
)
5706 tree t
= lookup_attribute ("fallthrough", attr
);
5709 /* This attribute shall appear at most once in each attribute-list. */
5710 if (lookup_attribute ("fallthrough", TREE_CHAIN (t
)))
5711 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified multiple "
5713 /* No attribute-argument-clause shall be present. */
5714 else if (TREE_VALUE (t
) != NULL_TREE
)
5715 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified with "
5717 /* Warn if other attributes are found. */
5718 for (t
= attr
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
5720 tree name
= get_attribute_name (t
);
5721 if (!is_attribute_p ("fallthrough", name
))
5723 if (!c_dialect_cxx () && get_attribute_namespace (t
) == NULL_TREE
)
5724 /* The specifications of standard attributes in C mean
5725 this is a constraint violation. */
5726 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
5727 get_attribute_name (t
));
5729 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
5736 /* Check for valid arguments being passed to a function with FNTYPE.
5737 There are NARGS arguments in the array ARGARRAY. LOC should be used
5738 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5741 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5742 to preserve location wrappers); checks that require folded arguments
5743 should call fold_for_warn on them. */
5746 check_function_arguments (location_t loc
, const_tree fndecl
, const_tree fntype
,
5747 int nargs
, tree
*argarray
, vec
<location_t
> *arglocs
)
5749 bool warned_p
= false;
5751 /* Check for null being passed in a pointer argument that must be
5752 non-null. We also need to do this if format checking is enabled. */
5755 warned_p
= check_function_nonnull (loc
, TYPE_ATTRIBUTES (fntype
),
5758 /* Check for errors in format strings. */
5760 if (warn_format
|| warn_suggest_attribute_format
)
5761 check_function_format (fntype
, TYPE_ATTRIBUTES (fntype
), nargs
, argarray
,
5765 check_function_sentinel (fntype
, nargs
, argarray
);
5768 warned_p
|= check_function_restrict (fndecl
, fntype
, nargs
, argarray
);
5772 /* Generic argument checking recursion routine. PARAM is the argument to
5773 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5774 once the argument is resolved. CTX is context for the callback. */
5776 check_function_arguments_recurse (void (*callback
)
5777 (void *, tree
, unsigned HOST_WIDE_INT
),
5778 void *ctx
, tree param
,
5779 unsigned HOST_WIDE_INT param_num
)
5781 if (CONVERT_EXPR_P (param
)
5782 && (TYPE_PRECISION (TREE_TYPE (param
))
5783 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param
, 0)))))
5785 /* Strip coercion. */
5786 check_function_arguments_recurse (callback
, ctx
,
5787 TREE_OPERAND (param
, 0), param_num
);
5791 if (TREE_CODE (param
) == CALL_EXPR
)
5793 tree type
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param
)));
5795 bool found_format_arg
= false;
5797 /* See if this is a call to a known internationalization function
5798 that modifies a format arg. Such a function may have multiple
5799 format_arg attributes (for example, ngettext). */
5801 for (attrs
= TYPE_ATTRIBUTES (type
);
5803 attrs
= TREE_CHAIN (attrs
))
5804 if (is_attribute_p ("format_arg", get_attribute_name (attrs
)))
5807 tree format_num_expr
;
5810 call_expr_arg_iterator iter
;
5812 /* Extract the argument number, which was previously checked
5814 format_num_expr
= TREE_VALUE (TREE_VALUE (attrs
));
5816 format_num
= tree_to_uhwi (format_num_expr
);
5818 for (inner_arg
= first_call_expr_arg (param
, &iter
), i
= 1;
5819 inner_arg
!= NULL_TREE
;
5820 inner_arg
= next_call_expr_arg (&iter
), i
++)
5821 if (i
== format_num
)
5823 check_function_arguments_recurse (callback
, ctx
,
5824 inner_arg
, param_num
);
5825 found_format_arg
= true;
5830 /* If we found a format_arg attribute and did a recursive check,
5831 we are done with checking this argument. Otherwise, we continue
5832 and this will be considered a non-literal. */
5833 if (found_format_arg
)
5837 if (TREE_CODE (param
) == COND_EXPR
)
5839 /* Simplify to avoid warning for an impossible case. */
5840 param
= fold_for_warn (param
);
5841 if (TREE_CODE (param
) == COND_EXPR
)
5843 /* Check both halves of the conditional expression. */
5844 check_function_arguments_recurse (callback
, ctx
,
5845 TREE_OPERAND (param
, 1),
5847 check_function_arguments_recurse (callback
, ctx
,
5848 TREE_OPERAND (param
, 2),
5854 (*callback
) (ctx
, param
, param_num
);
5857 /* Checks for a builtin function FNDECL that the number of arguments
5858 NARGS against the required number REQUIRED and issues an error if
5859 there is a mismatch. Returns true if the number of arguments is
5860 correct, otherwise false. LOC is the location of FNDECL. */
5863 builtin_function_validate_nargs (location_t loc
, tree fndecl
, int nargs
,
5866 if (nargs
< required
)
5868 error_at (loc
, "too few arguments to function %qE", fndecl
);
5871 else if (nargs
> required
)
5873 error_at (loc
, "too many arguments to function %qE", fndecl
);
5879 /* Helper macro for check_builtin_function_arguments. */
5880 #define ARG_LOCATION(N) \
5881 (arg_loc.is_empty () \
5882 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5883 : expansion_point_location (arg_loc[(N)]))
5885 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5886 Returns false if there was an error, otherwise true. LOC is the
5887 location of the function; ARG_LOC is a vector of locations of the
5888 arguments. If FNDECL is the result of resolving an overloaded
5889 target built-in, ORIG_FNDECL is the original function decl,
5890 otherwise it is null. */
5893 check_builtin_function_arguments (location_t loc
, vec
<location_t
> arg_loc
,
5894 tree fndecl
, tree orig_fndecl
,
5895 int nargs
, tree
*args
)
5897 if (!fndecl_built_in_p (fndecl
))
5900 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5901 return (!targetm
.check_builtin_call
5902 || targetm
.check_builtin_call (loc
, arg_loc
, fndecl
,
5903 orig_fndecl
, nargs
, args
));
5905 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_FRONTEND
)
5908 gcc_assert (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
);
5909 switch (DECL_FUNCTION_CODE (fndecl
))
5911 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
:
5912 if (!tree_fits_uhwi_p (args
[2]))
5914 error_at (ARG_LOCATION (2),
5915 "third argument to function %qE must be a constant integer",
5921 case BUILT_IN_ALLOCA_WITH_ALIGN
:
5923 /* Get the requested alignment (in bits) if it's a constant
5924 integer expression. */
5925 unsigned HOST_WIDE_INT align
5926 = tree_fits_uhwi_p (args
[1]) ? tree_to_uhwi (args
[1]) : 0;
5928 /* Determine if the requested alignment is a power of 2. */
5929 if ((align
& (align
- 1)))
5932 /* The maximum alignment in bits corresponding to the same
5933 maximum in bytes enforced in check_user_alignment(). */
5934 unsigned maxalign
= (UINT_MAX
>> 1) + 1;
5936 /* Reject invalid alignments. */
5937 if (align
< BITS_PER_UNIT
|| maxalign
< align
)
5939 error_at (ARG_LOCATION (1),
5940 "second argument to function %qE must be a constant "
5941 "integer power of 2 between %qi and %qu bits",
5942 fndecl
, BITS_PER_UNIT
, maxalign
);
5948 case BUILT_IN_CONSTANT_P
:
5949 return builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1);
5951 case BUILT_IN_ISFINITE
:
5952 case BUILT_IN_ISINF
:
5953 case BUILT_IN_ISINF_SIGN
:
5954 case BUILT_IN_ISNAN
:
5955 case BUILT_IN_ISNORMAL
:
5956 case BUILT_IN_SIGNBIT
:
5957 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1))
5959 if (TREE_CODE (TREE_TYPE (args
[0])) != REAL_TYPE
)
5961 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5962 "call to function %qE", fndecl
);
5969 case BUILT_IN_ISGREATER
:
5970 case BUILT_IN_ISGREATEREQUAL
:
5971 case BUILT_IN_ISLESS
:
5972 case BUILT_IN_ISLESSEQUAL
:
5973 case BUILT_IN_ISLESSGREATER
:
5974 case BUILT_IN_ISUNORDERED
:
5975 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2))
5977 enum tree_code code0
, code1
;
5978 code0
= TREE_CODE (TREE_TYPE (args
[0]));
5979 code1
= TREE_CODE (TREE_TYPE (args
[1]));
5980 if (!((code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
5981 || (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
5982 || (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)))
5984 error_at (loc
, "non-floating-point arguments in call to "
5985 "function %qE", fndecl
);
5992 case BUILT_IN_FPCLASSIFY
:
5993 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 6))
5995 for (unsigned int i
= 0; i
< 5; i
++)
5996 if (TREE_CODE (args
[i
]) != INTEGER_CST
)
5998 error_at (ARG_LOCATION (i
), "non-const integer argument %u in "
5999 "call to function %qE", i
+ 1, fndecl
);
6003 if (TREE_CODE (TREE_TYPE (args
[5])) != REAL_TYPE
)
6005 error_at (ARG_LOCATION (5), "non-floating-point argument in "
6006 "call to function %qE", fndecl
);
6013 case BUILT_IN_ASSUME_ALIGNED
:
6014 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2 + (nargs
> 2)))
6016 if (nargs
>= 3 && TREE_CODE (TREE_TYPE (args
[2])) != INTEGER_TYPE
)
6018 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
6019 "function %qE", fndecl
);
6026 case BUILT_IN_ADD_OVERFLOW
:
6027 case BUILT_IN_SUB_OVERFLOW
:
6028 case BUILT_IN_MUL_OVERFLOW
:
6029 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
6032 for (i
= 0; i
< 2; i
++)
6033 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
6035 error_at (ARG_LOCATION (i
), "argument %u in call to function "
6036 "%qE does not have integral type", i
+ 1, fndecl
);
6039 if (TREE_CODE (TREE_TYPE (args
[2])) != POINTER_TYPE
6040 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args
[2]))))
6042 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6043 "does not have pointer to integral type", fndecl
);
6046 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == ENUMERAL_TYPE
)
6048 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6049 "has pointer to enumerated type", fndecl
);
6052 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == BOOLEAN_TYPE
)
6054 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6055 "has pointer to boolean type", fndecl
);
6058 else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args
[2]))))
6060 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6061 "has pointer to %<const%> type (%qT)", fndecl
,
6062 TREE_TYPE (args
[2]));
6069 case BUILT_IN_ADD_OVERFLOW_P
:
6070 case BUILT_IN_SUB_OVERFLOW_P
:
6071 case BUILT_IN_MUL_OVERFLOW_P
:
6072 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
6075 for (i
= 0; i
< 3; i
++)
6076 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
6078 error_at (ARG_LOCATION (i
), "argument %u in call to function "
6079 "%qE does not have integral type", i
+ 1, fndecl
);
6082 if (TREE_CODE (TREE_TYPE (args
[2])) == ENUMERAL_TYPE
)
6084 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6085 "%qE has enumerated type", fndecl
);
6088 else if (TREE_CODE (TREE_TYPE (args
[2])) == BOOLEAN_TYPE
)
6090 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6091 "%qE has boolean type", fndecl
);
6103 /* Subroutine of c_parse_error.
6104 Return the result of concatenating LHS and RHS. RHS is really
6105 a string literal, its first character is indicated by RHS_START and
6106 RHS_SIZE is its length (including the terminating NUL character).
6108 The caller is responsible for deleting the returned pointer. */
6111 catenate_strings (const char *lhs
, const char *rhs_start
, int rhs_size
)
6113 const size_t lhs_size
= strlen (lhs
);
6114 char *result
= XNEWVEC (char, lhs_size
+ rhs_size
);
6115 memcpy (result
, lhs
, lhs_size
);
6116 memcpy (result
+ lhs_size
, rhs_start
, rhs_size
);
6120 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6121 before TOKEN, which had the associated VALUE. */
6124 c_parse_error (const char *gmsgid
, enum cpp_ttype token_type
,
6125 tree value
, unsigned char token_flags
,
6126 rich_location
*richloc
)
6128 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6130 char *message
= NULL
;
6132 if (token_type
== CPP_EOF
)
6133 message
= catenate_messages (gmsgid
, " at end of input");
6134 else if (token_type
== CPP_CHAR
6135 || token_type
== CPP_WCHAR
6136 || token_type
== CPP_CHAR16
6137 || token_type
== CPP_CHAR32
6138 || token_type
== CPP_UTF8CHAR
)
6140 unsigned int val
= TREE_INT_CST_LOW (value
);
6162 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
6163 message
= catenate_messages (gmsgid
, " before %s'%c'");
6165 message
= catenate_messages (gmsgid
, " before %s'\\x%x'");
6167 error_at (richloc
, message
, prefix
, val
);
6171 else if (token_type
== CPP_CHAR_USERDEF
6172 || token_type
== CPP_WCHAR_USERDEF
6173 || token_type
== CPP_CHAR16_USERDEF
6174 || token_type
== CPP_CHAR32_USERDEF
6175 || token_type
== CPP_UTF8CHAR_USERDEF
)
6176 message
= catenate_messages (gmsgid
,
6177 " before user-defined character literal");
6178 else if (token_type
== CPP_STRING_USERDEF
6179 || token_type
== CPP_WSTRING_USERDEF
6180 || token_type
== CPP_STRING16_USERDEF
6181 || token_type
== CPP_STRING32_USERDEF
6182 || token_type
== CPP_UTF8STRING_USERDEF
)
6183 message
= catenate_messages (gmsgid
, " before user-defined string literal");
6184 else if (token_type
== CPP_STRING
6185 || token_type
== CPP_WSTRING
6186 || token_type
== CPP_STRING16
6187 || token_type
== CPP_STRING32
6188 || token_type
== CPP_UTF8STRING
)
6189 message
= catenate_messages (gmsgid
, " before string constant");
6190 else if (token_type
== CPP_NUMBER
)
6191 message
= catenate_messages (gmsgid
, " before numeric constant");
6192 else if (token_type
== CPP_NAME
)
6194 message
= catenate_messages (gmsgid
, " before %qE");
6195 error_at (richloc
, message
, value
);
6199 else if (token_type
== CPP_PRAGMA
)
6200 message
= catenate_messages (gmsgid
, " before %<#pragma%>");
6201 else if (token_type
== CPP_PRAGMA_EOL
)
6202 message
= catenate_messages (gmsgid
, " before end of line");
6203 else if (token_type
== CPP_DECLTYPE
)
6204 message
= catenate_messages (gmsgid
, " before %<decltype%>");
6205 else if (token_type
< N_TTYPES
)
6207 message
= catenate_messages (gmsgid
, " before %qs token");
6208 error_at (richloc
, message
, cpp_type2name (token_type
, token_flags
));
6213 error_at (richloc
, gmsgid
);
6217 error_at (richloc
, message
);
6220 #undef catenate_messages
6223 /* Return the gcc option code associated with the reason for a cpp
6224 message, or 0 if none. */
6227 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason
)
6229 const struct cpp_reason_option_codes_t
*entry
;
6231 for (entry
= cpp_reason_option_codes
; entry
->reason
!= CPP_W_NONE
; entry
++)
6233 if (entry
->reason
== reason
)
6234 return entry
->option_code
;
6239 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6240 preprocessor. The diagnostic is of type LEVEL, with REASON set
6241 to the reason code if LEVEL is represents a warning, at location
6242 RICHLOC unless this is after lexing and the compiler's location
6243 should be used instead; MSG is the translated message and AP
6244 the arguments. Returns true if a diagnostic was emitted, false
6248 c_cpp_diagnostic (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
6249 enum cpp_diagnostic_level level
,
6250 enum cpp_warning_reason reason
,
6251 rich_location
*richloc
,
6252 const char *msg
, va_list *ap
)
6254 diagnostic_info diagnostic
;
6255 diagnostic_t dlevel
;
6256 bool save_warn_system_headers
= global_dc
->dc_warn_system_headers
;
6261 case CPP_DL_WARNING_SYSHDR
:
6264 global_dc
->dc_warn_system_headers
= 1;
6266 case CPP_DL_WARNING
:
6269 dlevel
= DK_WARNING
;
6271 case CPP_DL_PEDWARN
:
6272 if (flag_no_output
&& !flag_pedantic_errors
)
6274 dlevel
= DK_PEDWARN
;
6292 richloc
->set_range (0, input_location
, SHOW_RANGE_WITH_CARET
);
6293 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
6295 diagnostic_override_option_index
6297 c_option_controlling_cpp_diagnostic (reason
));
6298 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
6299 if (level
== CPP_DL_WARNING_SYSHDR
)
6300 global_dc
->dc_warn_system_headers
= save_warn_system_headers
;
6304 /* Convert a character from the host to the target execution character
6305 set. cpplib handles this, mostly. */
6308 c_common_to_target_charset (HOST_WIDE_INT c
)
6310 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6311 zero-extended under -fno-signed-char. cpplib insists that characters
6312 and character constants are always unsigned. Hence we must convert
6314 cppchar_t uc
= ((cppchar_t
)c
) & ((((cppchar_t
)1) << CHAR_BIT
)-1);
6316 uc
= cpp_host_to_exec_charset (parse_in
, uc
);
6318 if (flag_signed_char
)
6319 return ((HOST_WIDE_INT
)uc
) << (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
)
6320 >> (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
);
6325 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6326 references with an INDIRECT_REF of a constant at the bottom; much like the
6327 traditional rendering of offsetof as a macro. TYPE is the desired type of
6328 the whole expression. Return the folded result. */
6331 fold_offsetof (tree expr
, tree type
, enum tree_code ctx
)
6334 tree_code code
= TREE_CODE (expr
);
6341 error ("cannot apply %<offsetof%> to static data member %qD", expr
);
6342 return error_mark_node
;
6346 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6347 return error_mark_node
;
6351 if (!TREE_CONSTANT (TREE_OPERAND (expr
, 0)))
6353 error ("cannot apply %<offsetof%> to a non constant address");
6354 return error_mark_node
;
6356 return convert (type
, TREE_OPERAND (expr
, 0));
6359 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6360 if (base
== error_mark_node
)
6363 t
= TREE_OPERAND (expr
, 1);
6364 if (DECL_C_BIT_FIELD (t
))
6366 error ("attempt to take address of bit-field structure "
6368 return error_mark_node
;
6370 off
= size_binop_loc (input_location
, PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
6371 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
6376 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6377 if (base
== error_mark_node
)
6380 t
= TREE_OPERAND (expr
, 1);
6381 STRIP_ANY_LOCATION_WRAPPER (t
);
6383 /* Check if the offset goes beyond the upper bound of the array. */
6384 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) >= 0)
6386 tree upbound
= array_ref_up_bound (expr
);
6387 if (upbound
!= NULL_TREE
6388 && TREE_CODE (upbound
) == INTEGER_CST
6389 && !tree_int_cst_equal (upbound
,
6390 TYPE_MAX_VALUE (TREE_TYPE (upbound
))))
6392 if (ctx
!= ARRAY_REF
&& ctx
!= COMPONENT_REF
)
6393 upbound
= size_binop (PLUS_EXPR
, upbound
,
6394 build_int_cst (TREE_TYPE (upbound
), 1));
6395 if (tree_int_cst_lt (upbound
, t
))
6399 for (v
= TREE_OPERAND (expr
, 0);
6400 TREE_CODE (v
) == COMPONENT_REF
;
6401 v
= TREE_OPERAND (v
, 0))
6402 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
6405 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
6406 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
6407 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
6413 /* Don't warn if the array might be considered a poor
6414 man's flexible array member with a very permissive
6415 definition thereof. */
6416 if (TREE_CODE (v
) == ARRAY_REF
6417 || TREE_CODE (v
) == COMPONENT_REF
)
6418 warning (OPT_Warray_bounds
,
6419 "index %E denotes an offset "
6420 "greater than size of %qT",
6421 t
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
6426 t
= convert (sizetype
, t
);
6427 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
6431 /* Handle static members of volatile structs. */
6432 t
= TREE_OPERAND (expr
, 1);
6433 gcc_checking_assert (VAR_P (get_base_address (t
)));
6434 return fold_offsetof (t
, type
);
6440 if (!POINTER_TYPE_P (type
))
6441 return size_binop (PLUS_EXPR
, base
, convert (type
, off
));
6442 return fold_build_pointer_plus (base
, off
);
6445 /* *PTYPE is an incomplete array. Complete it with a domain based on
6446 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6447 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6448 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6451 complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
6453 tree maxindex
, type
, main_type
, elt
, unqual_elt
;
6454 int failure
= 0, quals
;
6455 bool overflow_p
= false;
6457 maxindex
= size_zero_node
;
6460 STRIP_ANY_LOCATION_WRAPPER (initial_value
);
6462 if (TREE_CODE (initial_value
) == STRING_CST
)
6465 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value
)));
6466 maxindex
= size_int (TREE_STRING_LENGTH (initial_value
)/eltsize
- 1);
6468 else if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
6470 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
6472 if (vec_safe_is_empty (v
))
6476 maxindex
= ssize_int (-1);
6481 unsigned HOST_WIDE_INT cnt
;
6482 constructor_elt
*ce
;
6483 bool fold_p
= false;
6486 maxindex
= (*v
)[0].index
, fold_p
= true;
6488 curindex
= maxindex
;
6490 for (cnt
= 1; vec_safe_iterate (v
, cnt
, &ce
); cnt
++)
6492 bool curfold_p
= false;
6494 curindex
= ce
->index
, curfold_p
= true;
6499 /* Since we treat size types now as ordinary
6500 unsigned types, we need an explicit overflow
6502 tree orig
= curindex
;
6503 curindex
= fold_convert (sizetype
, curindex
);
6504 overflow_p
|= tree_int_cst_lt (curindex
, orig
);
6506 curindex
= size_binop (PLUS_EXPR
, curindex
,
6509 if (tree_int_cst_lt (maxindex
, curindex
))
6510 maxindex
= curindex
, fold_p
= curfold_p
;
6514 tree orig
= maxindex
;
6515 maxindex
= fold_convert (sizetype
, maxindex
);
6516 overflow_p
|= tree_int_cst_lt (maxindex
, orig
);
6522 /* Make an error message unless that happened already. */
6523 if (initial_value
!= error_mark_node
)
6535 elt
= TREE_TYPE (type
);
6536 quals
= TYPE_QUALS (strip_array_types (elt
));
6540 unqual_elt
= c_build_qualified_type (elt
, KEEP_QUAL_ADDR_SPACE (quals
));
6542 /* Using build_distinct_type_copy and modifying things afterward instead
6543 of using build_array_type to create a new type preserves all of the
6544 TYPE_LANG_FLAG_? bits that the front end may have set. */
6545 main_type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6546 TREE_TYPE (main_type
) = unqual_elt
;
6547 TYPE_DOMAIN (main_type
)
6548 = build_range_type (TREE_TYPE (maxindex
),
6549 build_int_cst (TREE_TYPE (maxindex
), 0), maxindex
);
6550 TYPE_TYPELESS_STORAGE (main_type
) = TYPE_TYPELESS_STORAGE (type
);
6551 layout_type (main_type
);
6553 /* Make sure we have the canonical MAIN_TYPE. */
6554 hashval_t hashcode
= type_hash_canon_hash (main_type
);
6555 main_type
= type_hash_canon (hashcode
, main_type
);
6557 /* Fix the canonical type. */
6558 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type
))
6559 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type
)))
6560 SET_TYPE_STRUCTURAL_EQUALITY (main_type
);
6561 else if (TYPE_CANONICAL (TREE_TYPE (main_type
)) != TREE_TYPE (main_type
)
6562 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type
))
6563 != TYPE_DOMAIN (main_type
)))
6564 TYPE_CANONICAL (main_type
)
6565 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type
)),
6566 TYPE_CANONICAL (TYPE_DOMAIN (main_type
)),
6567 TYPE_TYPELESS_STORAGE (main_type
));
6569 TYPE_CANONICAL (main_type
) = main_type
;
6574 type
= c_build_qualified_type (main_type
, quals
);
6576 if (COMPLETE_TYPE_P (type
)
6577 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
6578 && (overflow_p
|| TREE_OVERFLOW (TYPE_SIZE_UNIT (type
))))
6580 error ("size of array is too large");
6581 /* If we proceed with the array type as it is, we'll eventually
6582 crash in tree_to_[su]hwi(). */
6583 type
= error_mark_node
;
6590 /* INIT is an constructor of a structure with a flexible array member.
6591 Complete the flexible array member with a domain based on it's value. */
6593 complete_flexible_array_elts (tree init
)
6597 if (init
== NULL_TREE
|| TREE_CODE (init
) != CONSTRUCTOR
)
6600 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
6603 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
6604 type
= TREE_TYPE (elt
);
6605 if (TREE_CODE (type
) == ARRAY_TYPE
6606 && TYPE_SIZE (type
) == NULL_TREE
)
6607 complete_array_type (&TREE_TYPE (elt
), elt
, false);
6609 complete_flexible_array_elts (elt
);
6612 /* Like c_mark_addressable but don't check register qualifier. */
6614 c_common_mark_addressable_vec (tree t
)
6616 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
6617 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
6618 while (handled_component_p (t
))
6619 t
= TREE_OPERAND (t
, 0);
6621 && TREE_CODE (t
) != PARM_DECL
6622 && TREE_CODE (t
) != COMPOUND_LITERAL_EXPR
)
6624 if (!VAR_P (t
) || !DECL_HARD_REGISTER (t
))
6625 TREE_ADDRESSABLE (t
) = 1;
6626 if (TREE_CODE (t
) == COMPOUND_LITERAL_EXPR
)
6627 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t
)) = 1;
6632 /* Used to help initialize the builtin-types.def table. When a type of
6633 the correct size doesn't exist, use error_mark_node instead of NULL.
6634 The later results in segfaults even when a decl using the type doesn't
6638 builtin_type_for_size (int size
, bool unsignedp
)
6640 tree type
= c_common_type_for_size (size
, unsignedp
);
6641 return type
? type
: error_mark_node
;
6644 /* Work out the size of the first argument of a call to
6645 __builtin_speculation_safe_value. Only pointers and integral types
6646 are permitted. Return -1 if the argument type is not supported or
6647 the size is too large; 0 if the argument type is a pointer or the
6648 size if it is integral. */
6649 static enum built_in_function
6650 speculation_safe_value_resolve_call (tree function
, vec
<tree
, va_gc
> *params
)
6652 /* Type of the argument. */
6656 if (vec_safe_is_empty (params
))
6658 error ("too few arguments to function %qE", function
);
6659 return BUILT_IN_NONE
;
6662 type
= TREE_TYPE ((*params
)[0]);
6663 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6665 /* Force array-to-pointer decay for C++. */
6666 (*params
)[0] = default_conversion ((*params
)[0]);
6667 type
= TREE_TYPE ((*params
)[0]);
6670 if (POINTER_TYPE_P (type
))
6671 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR
;
6673 if (!INTEGRAL_TYPE_P (type
))
6676 if (!COMPLETE_TYPE_P (type
))
6679 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6680 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6681 return ((enum built_in_function
)
6682 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1
+ exact_log2 (size
)));
6685 /* Issue the diagnostic only if the argument is valid, otherwise
6686 it would be redundant at best and could be misleading. */
6687 if (type
!= error_mark_node
)
6688 error ("operand type %qT is incompatible with argument %d of %qE",
6691 return BUILT_IN_NONE
;
6694 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6695 the prototype for FUNCTION. The first argument is mandatory, a second
6696 argument, if present, must be type compatible with the first. */
6698 speculation_safe_value_resolve_params (location_t loc
, tree orig_function
,
6699 vec
<tree
, va_gc
> *params
)
6703 if (params
->length () == 0)
6705 error_at (loc
, "too few arguments to function %qE", orig_function
);
6709 else if (params
->length () > 2)
6711 error_at (loc
, "too many arguments to function %qE", orig_function
);
6716 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
)
6717 val
= default_conversion (val
);
6718 if (!(TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
6719 || TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
))
6722 "expecting argument of type pointer or of type integer "
6728 if (params
->length () == 2)
6730 tree val2
= (*params
)[1];
6731 if (TREE_CODE (TREE_TYPE (val2
)) == ARRAY_TYPE
)
6732 val2
= default_conversion (val2
);
6733 if (!(TREE_TYPE (val
) == TREE_TYPE (val2
)
6734 || useless_type_conversion_p (TREE_TYPE (val
), TREE_TYPE (val2
))))
6736 error_at (loc
, "both arguments must be compatible");
6739 (*params
)[1] = val2
;
6745 /* Cast the result of the builtin back to the type of the first argument,
6746 preserving any qualifiers that it might have. */
6748 speculation_safe_value_resolve_return (tree first_param
, tree result
)
6750 tree ptype
= TREE_TYPE (first_param
);
6751 tree rtype
= TREE_TYPE (result
);
6752 ptype
= TYPE_MAIN_VARIANT (ptype
);
6754 if (tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6755 return convert (ptype
, result
);
6760 /* A helper function for resolve_overloaded_builtin in resolving the
6761 overloaded __sync_ builtins. Returns a positive power of 2 if the
6762 first operand of PARAMS is a pointer to a supported data type.
6763 Returns 0 if an error is encountered.
6764 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6768 sync_resolve_size (tree function
, vec
<tree
, va_gc
> *params
, bool fetch
)
6770 /* Type of the argument. */
6772 /* Type the argument points to. */
6776 if (vec_safe_is_empty (params
))
6778 error ("too few arguments to function %qE", function
);
6782 argtype
= type
= TREE_TYPE ((*params
)[0]);
6783 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6785 /* Force array-to-pointer decay for C++. */
6786 (*params
)[0] = default_conversion ((*params
)[0]);
6787 type
= TREE_TYPE ((*params
)[0]);
6789 if (TREE_CODE (type
) != POINTER_TYPE
)
6792 type
= TREE_TYPE (type
);
6793 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
6796 if (!COMPLETE_TYPE_P (type
))
6799 if (fetch
&& TREE_CODE (type
) == BOOLEAN_TYPE
)
6802 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6803 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6807 /* Issue the diagnostic only if the argument is valid, otherwise
6808 it would be redundant at best and could be misleading. */
6809 if (argtype
!= error_mark_node
)
6810 error ("operand type %qT is incompatible with argument %d of %qE",
6811 argtype
, 1, function
);
6815 /* A helper function for resolve_overloaded_builtin. Adds casts to
6816 PARAMS to make arguments match up with those of FUNCTION. Drops
6817 the variadic arguments at the end. Returns false if some error
6818 was encountered; true on success. */
6821 sync_resolve_params (location_t loc
, tree orig_function
, tree function
,
6822 vec
<tree
, va_gc
> *params
, bool orig_format
)
6824 function_args_iterator iter
;
6826 unsigned int parmnum
;
6828 function_args_iter_init (&iter
, TREE_TYPE (function
));
6829 /* We've declared the implementation functions to use "volatile void *"
6830 as the pointer parameter, so we shouldn't get any complaints from the
6831 call to check_function_arguments what ever type the user used. */
6832 function_args_iter_next (&iter
);
6833 ptype
= TREE_TYPE (TREE_TYPE ((*params
)[0]));
6834 ptype
= TYPE_MAIN_VARIANT (ptype
);
6836 /* For the rest of the values, we need to cast these to FTYPE, so that we
6837 don't get warnings for passing pointer types, etc. */
6843 arg_type
= function_args_iter_cond (&iter
);
6844 /* XXX void_type_node belies the abstraction. */
6845 if (arg_type
== void_type_node
)
6849 if (params
->length () <= parmnum
)
6851 error_at (loc
, "too few arguments to function %qE", orig_function
);
6855 /* Only convert parameters if arg_type is unsigned integer type with
6856 new format sync routines, i.e. don't attempt to convert pointer
6857 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6858 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6860 if (TREE_CODE (arg_type
) == INTEGER_TYPE
&& TYPE_UNSIGNED (arg_type
))
6862 /* Ideally for the first conversion we'd use convert_for_assignment
6863 so that we get warnings for anything that doesn't match the pointer
6864 type. This isn't portable across the C and C++ front ends atm. */
6865 val
= (*params
)[parmnum
];
6866 val
= convert (ptype
, val
);
6867 val
= convert (arg_type
, val
);
6868 (*params
)[parmnum
] = val
;
6871 function_args_iter_next (&iter
);
6874 /* __atomic routines are not variadic. */
6875 if (!orig_format
&& params
->length () != parmnum
+ 1)
6877 error_at (loc
, "too many arguments to function %qE", orig_function
);
6881 /* The definition of these primitives is variadic, with the remaining
6882 being "an optional list of variables protected by the memory barrier".
6883 No clue what that's supposed to mean, precisely, but we consider all
6884 call-clobbered variables to be protected so we're safe. */
6885 params
->truncate (parmnum
+ 1);
6890 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6891 RESULT to make it match the type of the first pointer argument in
6895 sync_resolve_return (tree first_param
, tree result
, bool orig_format
)
6897 tree ptype
= TREE_TYPE (TREE_TYPE (first_param
));
6898 tree rtype
= TREE_TYPE (result
);
6899 ptype
= TYPE_MAIN_VARIANT (ptype
);
6901 /* New format doesn't require casting unless the types are the same size. */
6902 if (orig_format
|| tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6903 return convert (ptype
, result
);
6908 /* This function verifies the PARAMS to generic atomic FUNCTION.
6909 It returns the size if all the parameters are the same size, otherwise
6910 0 is returned if the parameters are invalid. */
6913 get_atomic_generic_size (location_t loc
, tree function
,
6914 vec
<tree
, va_gc
> *params
)
6916 unsigned int n_param
;
6917 unsigned int n_model
;
6922 /* Determine the parameter makeup. */
6923 switch (DECL_FUNCTION_CODE (function
))
6925 case BUILT_IN_ATOMIC_EXCHANGE
:
6929 case BUILT_IN_ATOMIC_LOAD
:
6930 case BUILT_IN_ATOMIC_STORE
:
6934 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
6942 if (vec_safe_length (params
) != n_param
)
6944 error_at (loc
, "incorrect number of arguments to function %qE", function
);
6948 /* Get type of first parameter, and determine its size. */
6949 type_0
= TREE_TYPE ((*params
)[0]);
6950 if (TREE_CODE (type_0
) == ARRAY_TYPE
&& c_dialect_cxx ())
6952 /* Force array-to-pointer decay for C++. */
6953 (*params
)[0] = default_conversion ((*params
)[0]);
6954 type_0
= TREE_TYPE ((*params
)[0]);
6956 if (TREE_CODE (type_0
) != POINTER_TYPE
|| VOID_TYPE_P (TREE_TYPE (type_0
)))
6958 error_at (loc
, "argument 1 of %qE must be a non-void pointer type",
6963 /* Types must be compile time constant sizes. */
6964 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0
)))) != INTEGER_CST
)
6967 "argument 1 of %qE must be a pointer to a constant size type",
6972 size_0
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0
)));
6974 /* Zero size objects are not allowed. */
6978 "argument 1 of %qE must be a pointer to a nonzero size object",
6983 /* Check each other parameter is a pointer and the same size. */
6984 for (x
= 0; x
< n_param
- n_model
; x
++)
6987 tree type
= TREE_TYPE ((*params
)[x
]);
6988 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6989 if (n_param
== 6 && x
== 3)
6991 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6993 /* Force array-to-pointer decay for C++. */
6994 (*params
)[x
] = default_conversion ((*params
)[x
]);
6995 type
= TREE_TYPE ((*params
)[x
]);
6997 if (!POINTER_TYPE_P (type
))
6999 error_at (loc
, "argument %d of %qE must be a pointer type", x
+ 1,
7003 else if (TYPE_SIZE_UNIT (TREE_TYPE (type
))
7004 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type
))))
7007 error_at (loc
, "argument %d of %qE must be a pointer to a constant "
7008 "size type", x
+ 1, function
);
7011 else if (FUNCTION_POINTER_TYPE_P (type
))
7013 error_at (loc
, "argument %d of %qE must not be a pointer to a "
7014 "function", x
+ 1, function
);
7017 tree type_size
= TYPE_SIZE_UNIT (TREE_TYPE (type
));
7018 size
= type_size
? tree_to_uhwi (type_size
) : 0;
7021 error_at (loc
, "size mismatch in argument %d of %qE", x
+ 1,
7027 /* Check memory model parameters for validity. */
7028 for (x
= n_param
- n_model
; x
< n_param
; x
++)
7030 tree p
= (*params
)[x
];
7031 if (!INTEGRAL_TYPE_P (TREE_TYPE (p
)))
7033 error_at (loc
, "non-integer memory model argument %d of %qE", x
+ 1,
7037 p
= fold_for_warn (p
);
7038 if (TREE_CODE (p
) == INTEGER_CST
)
7040 /* memmodel_base masks the low 16 bits, thus ignore any bits above
7041 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7042 bits will be checked later during expansion in target specific
7044 if (memmodel_base (TREE_INT_CST_LOW (p
)) >= MEMMODEL_LAST
)
7045 warning_at (loc
, OPT_Winvalid_memory_model
,
7046 "invalid memory model argument %d of %qE", x
+ 1,
7055 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
7056 at the beginning of the parameter list PARAMS representing the size of the
7057 objects. This is to match the library ABI requirement. LOC is the location
7058 of the function call.
7059 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
7060 returned to allow the external call to be constructed. */
7063 add_atomic_size_parameter (unsigned n
, location_t loc
, tree function
,
7064 vec
<tree
, va_gc
> *params
)
7068 /* Insert a SIZE_T parameter as the first param. If there isn't
7069 enough space, allocate a new vector and recursively re-build with that. */
7070 if (!params
->space (1))
7072 unsigned int z
, len
;
7073 vec
<tree
, va_gc
> *v
;
7076 len
= params
->length ();
7077 vec_alloc (v
, len
+ 1);
7078 v
->quick_push (build_int_cst (size_type_node
, n
));
7079 for (z
= 0; z
< len
; z
++)
7080 v
->quick_push ((*params
)[z
]);
7081 f
= build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
7086 /* Add the size parameter and leave as a function call for processing. */
7087 size_node
= build_int_cst (size_type_node
, n
);
7088 params
->quick_insert (0, size_node
);
7093 /* Return whether atomic operations for naturally aligned N-byte
7094 arguments are supported, whether inline or through libatomic. */
7096 atomic_size_supported_p (int n
)
7107 return targetm
.scalar_mode_supported_p (TImode
);
7114 /* This will process an __atomic_exchange function call, determine whether it
7115 needs to be mapped to the _N variation, or turned into a library call.
7116 LOC is the location of the builtin call.
7117 FUNCTION is the DECL that has been invoked;
7118 PARAMS is the argument list for the call. The return value is non-null
7119 TRUE is returned if it is translated into the proper format for a call to the
7120 external library, and NEW_RETURN is set the tree for that function.
7121 FALSE is returned if processing for the _N variation is required, and
7122 NEW_RETURN is set to the return value the result is copied into. */
7124 resolve_overloaded_atomic_exchange (location_t loc
, tree function
,
7125 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7127 tree p0
, p1
, p2
, p3
;
7128 tree I_type
, I_type_ptr
;
7129 int n
= get_atomic_generic_size (loc
, function
, params
);
7131 /* Size of 0 is an error condition. */
7134 *new_return
= error_mark_node
;
7138 /* If not a lock-free size, change to the library generic format. */
7139 if (!atomic_size_supported_p (n
))
7141 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7145 /* Otherwise there is a lockfree match, transform the call from:
7146 void fn(T* mem, T* desired, T* return, model)
7148 *return = (T) (fn (In* mem, (In) *desired, model)) */
7155 /* Create pointer to appropriate size. */
7156 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7157 I_type_ptr
= build_pointer_type (I_type
);
7159 /* Convert object pointer to required type. */
7160 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7162 /* Convert new value to required type, and dereference it. */
7163 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7164 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7167 /* Move memory model to the 3rd position, and end param list. */
7169 params
->truncate (3);
7171 /* Convert return pointer and dereference it for later assignment. */
7172 *new_return
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7178 /* This will process an __atomic_compare_exchange function call, determine
7179 whether it needs to be mapped to the _N variation, or turned into a lib call.
7180 LOC is the location of the builtin call.
7181 FUNCTION is the DECL that has been invoked;
7182 PARAMS is the argument list for the call. The return value is non-null
7183 TRUE is returned if it is translated into the proper format for a call to the
7184 external library, and NEW_RETURN is set the tree for that function.
7185 FALSE is returned if processing for the _N variation is required. */
7188 resolve_overloaded_atomic_compare_exchange (location_t loc
, tree function
,
7189 vec
<tree
, va_gc
> *params
,
7193 tree I_type
, I_type_ptr
;
7194 int n
= get_atomic_generic_size (loc
, function
, params
);
7196 /* Size of 0 is an error condition. */
7199 *new_return
= error_mark_node
;
7203 /* If not a lock-free size, change to the library generic format. */
7204 if (!atomic_size_supported_p (n
))
7206 /* The library generic format does not have the weak parameter, so
7207 remove it from the param list. Since a parameter has been removed,
7208 we can be sure that there is room for the SIZE_T parameter, meaning
7209 there will not be a recursive rebuilding of the parameter list, so
7210 there is no danger this will be done twice. */
7213 (*params
)[3] = (*params
)[4];
7214 (*params
)[4] = (*params
)[5];
7215 params
->truncate (5);
7217 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7221 /* Otherwise, there is a match, so the call needs to be transformed from:
7222 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7224 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7230 /* Create pointer to appropriate size. */
7231 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7232 I_type_ptr
= build_pointer_type (I_type
);
7234 /* Convert object pointer to required type. */
7235 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7238 /* Convert expected pointer to required type. */
7239 p1
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p1
);
7242 /* Convert desired value to required type, and dereference it. */
7243 p2
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7244 p2
= build1 (VIEW_CONVERT_EXPR
, I_type
, p2
);
7247 /* The rest of the parameters are fine. NULL means no special return value
7254 /* This will process an __atomic_load function call, determine whether it
7255 needs to be mapped to the _N variation, or turned into a library call.
7256 LOC is the location of the builtin call.
7257 FUNCTION is the DECL that has been invoked;
7258 PARAMS is the argument list for the call. The return value is non-null
7259 TRUE is returned if it is translated into the proper format for a call to the
7260 external library, and NEW_RETURN is set the tree for that function.
7261 FALSE is returned if processing for the _N variation is required, and
7262 NEW_RETURN is set to the return value the result is copied into. */
7265 resolve_overloaded_atomic_load (location_t loc
, tree function
,
7266 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7269 tree I_type
, I_type_ptr
;
7270 int n
= get_atomic_generic_size (loc
, function
, params
);
7272 /* Size of 0 is an error condition. */
7275 *new_return
= error_mark_node
;
7279 /* If not a lock-free size, change to the library generic format. */
7280 if (!atomic_size_supported_p (n
))
7282 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7286 /* Otherwise, there is a match, so the call needs to be transformed from:
7287 void fn(T* mem, T* return, model)
7289 *return = (T) (fn ((In *) mem, model)) */
7295 /* Create pointer to appropriate size. */
7296 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7297 I_type_ptr
= build_pointer_type (I_type
);
7299 /* Convert object pointer to required type. */
7300 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7303 /* Move memory model to the 2nd position, and end param list. */
7305 params
->truncate (2);
7307 /* Convert return pointer and dereference it for later assignment. */
7308 *new_return
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7314 /* This will process an __atomic_store function call, determine whether it
7315 needs to be mapped to the _N variation, or turned into a library call.
7316 LOC is the location of the builtin call.
7317 FUNCTION is the DECL that has been invoked;
7318 PARAMS is the argument list for the call. The return value is non-null
7319 TRUE is returned if it is translated into the proper format for a call to the
7320 external library, and NEW_RETURN is set the tree for that function.
7321 FALSE is returned if processing for the _N variation is required, and
7322 NEW_RETURN is set to the return value the result is copied into. */
7325 resolve_overloaded_atomic_store (location_t loc
, tree function
,
7326 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7329 tree I_type
, I_type_ptr
;
7330 int n
= get_atomic_generic_size (loc
, function
, params
);
7332 /* Size of 0 is an error condition. */
7335 *new_return
= error_mark_node
;
7339 /* If not a lock-free size, change to the library generic format. */
7340 if (!atomic_size_supported_p (n
))
7342 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7346 /* Otherwise, there is a match, so the call needs to be transformed from:
7347 void fn(T* mem, T* value, model)
7349 fn ((In *) mem, (In) *value, model) */
7354 /* Create pointer to appropriate size. */
7355 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7356 I_type_ptr
= build_pointer_type (I_type
);
7358 /* Convert object pointer to required type. */
7359 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7362 /* Convert new value to required type, and dereference it. */
7363 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7364 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7367 /* The memory model is in the right spot already. Return is void. */
7368 *new_return
= NULL_TREE
;
7374 /* Some builtin functions are placeholders for other expressions. This
7375 function should be called immediately after parsing the call expression
7376 before surrounding code has committed to the type of the expression.
7378 LOC is the location of the builtin call.
7380 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7381 PARAMS is the argument list for the call. The return value is non-null
7382 when expansion is complete, and null if normal processing should
7386 resolve_overloaded_builtin (location_t loc
, tree function
,
7387 vec
<tree
, va_gc
> *params
)
7389 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7390 Those are not valid to call with a pointer to _Bool (or C++ bool)
7391 and so must be rejected. */
7392 bool fetch_op
= true;
7393 bool orig_format
= true;
7394 tree new_return
= NULL_TREE
;
7396 switch (DECL_BUILT_IN_CLASS (function
))
7398 case BUILT_IN_NORMAL
:
7401 if (targetm
.resolve_overloaded_builtin
)
7402 return targetm
.resolve_overloaded_builtin (loc
, function
, params
);
7409 /* Handle BUILT_IN_NORMAL here. */
7410 enum built_in_function orig_code
= DECL_FUNCTION_CODE (function
);
7413 case BUILT_IN_SPECULATION_SAFE_VALUE_N
:
7415 tree new_function
, first_param
, result
;
7416 enum built_in_function fncode
7417 = speculation_safe_value_resolve_call (function
, params
);;
7419 first_param
= (*params
)[0];
7420 if (fncode
== BUILT_IN_NONE
7421 || !speculation_safe_value_resolve_params (loc
, function
, params
))
7422 return error_mark_node
;
7424 if (targetm
.have_speculation_safe_value (true))
7426 new_function
= builtin_decl_explicit (fncode
);
7427 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7430 if (result
== error_mark_node
)
7433 return speculation_safe_value_resolve_return (first_param
, result
);
7437 /* This target doesn't have, or doesn't need, active mitigation
7438 against incorrect speculative execution. Simply return the
7439 first parameter to the builtin. */
7440 if (!targetm
.have_speculation_safe_value (false))
7441 /* The user has invoked __builtin_speculation_safe_value
7442 even though __HAVE_SPECULATION_SAFE_VALUE is not
7443 defined: emit a warning. */
7444 warning_at (input_location
, 0,
7445 "this target does not define a speculation barrier; "
7446 "your program will still execute correctly, "
7447 "but incorrect speculation may not be be "
7450 /* If the optional second argument is present, handle any side
7452 if (params
->length () == 2
7453 && TREE_SIDE_EFFECTS ((*params
)[1]))
7454 return build2 (COMPOUND_EXPR
, TREE_TYPE (first_param
),
7455 (*params
)[1], first_param
);
7461 case BUILT_IN_ATOMIC_EXCHANGE
:
7462 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7463 case BUILT_IN_ATOMIC_LOAD
:
7464 case BUILT_IN_ATOMIC_STORE
:
7466 /* Handle these 4 together so that they can fall through to the next
7467 case if the call is transformed to an _N variant. */
7470 case BUILT_IN_ATOMIC_EXCHANGE
:
7472 if (resolve_overloaded_atomic_exchange (loc
, function
, params
,
7475 /* Change to the _N variant. */
7476 orig_code
= BUILT_IN_ATOMIC_EXCHANGE_N
;
7480 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7482 if (resolve_overloaded_atomic_compare_exchange (loc
, function
,
7486 /* Change to the _N variant. */
7487 orig_code
= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
;
7490 case BUILT_IN_ATOMIC_LOAD
:
7492 if (resolve_overloaded_atomic_load (loc
, function
, params
,
7495 /* Change to the _N variant. */
7496 orig_code
= BUILT_IN_ATOMIC_LOAD_N
;
7499 case BUILT_IN_ATOMIC_STORE
:
7501 if (resolve_overloaded_atomic_store (loc
, function
, params
,
7504 /* Change to the _N variant. */
7505 orig_code
= BUILT_IN_ATOMIC_STORE_N
;
7513 case BUILT_IN_ATOMIC_EXCHANGE_N
:
7514 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
7515 case BUILT_IN_ATOMIC_LOAD_N
:
7516 case BUILT_IN_ATOMIC_STORE_N
:
7519 case BUILT_IN_ATOMIC_ADD_FETCH_N
:
7520 case BUILT_IN_ATOMIC_SUB_FETCH_N
:
7521 case BUILT_IN_ATOMIC_AND_FETCH_N
:
7522 case BUILT_IN_ATOMIC_NAND_FETCH_N
:
7523 case BUILT_IN_ATOMIC_XOR_FETCH_N
:
7524 case BUILT_IN_ATOMIC_OR_FETCH_N
:
7525 case BUILT_IN_ATOMIC_FETCH_ADD_N
:
7526 case BUILT_IN_ATOMIC_FETCH_SUB_N
:
7527 case BUILT_IN_ATOMIC_FETCH_AND_N
:
7528 case BUILT_IN_ATOMIC_FETCH_NAND_N
:
7529 case BUILT_IN_ATOMIC_FETCH_XOR_N
:
7530 case BUILT_IN_ATOMIC_FETCH_OR_N
:
7531 orig_format
= false;
7533 case BUILT_IN_SYNC_FETCH_AND_ADD_N
:
7534 case BUILT_IN_SYNC_FETCH_AND_SUB_N
:
7535 case BUILT_IN_SYNC_FETCH_AND_OR_N
:
7536 case BUILT_IN_SYNC_FETCH_AND_AND_N
:
7537 case BUILT_IN_SYNC_FETCH_AND_XOR_N
:
7538 case BUILT_IN_SYNC_FETCH_AND_NAND_N
:
7539 case BUILT_IN_SYNC_ADD_AND_FETCH_N
:
7540 case BUILT_IN_SYNC_SUB_AND_FETCH_N
:
7541 case BUILT_IN_SYNC_OR_AND_FETCH_N
:
7542 case BUILT_IN_SYNC_AND_AND_FETCH_N
:
7543 case BUILT_IN_SYNC_XOR_AND_FETCH_N
:
7544 case BUILT_IN_SYNC_NAND_AND_FETCH_N
:
7545 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
7546 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
:
7547 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
:
7548 case BUILT_IN_SYNC_LOCK_RELEASE_N
:
7550 /* The following are not _FETCH_OPs and must be accepted with
7551 pointers to _Bool (or C++ bool). */
7554 (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7555 && orig_code
!= BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7556 && orig_code
!= BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7557 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
);
7559 int n
= sync_resolve_size (function
, params
, fetch_op
);
7560 tree new_function
, first_param
, result
;
7561 enum built_in_function fncode
;
7564 return error_mark_node
;
7566 fncode
= (enum built_in_function
)((int)orig_code
+ exact_log2 (n
) + 1);
7567 new_function
= builtin_decl_explicit (fncode
);
7568 if (!sync_resolve_params (loc
, function
, new_function
, params
,
7570 return error_mark_node
;
7572 first_param
= (*params
)[0];
7573 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7575 if (result
== error_mark_node
)
7577 if (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7578 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
7579 && orig_code
!= BUILT_IN_ATOMIC_STORE_N
7580 && orig_code
!= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
)
7581 result
= sync_resolve_return (first_param
, result
, orig_format
);
7584 /* Prevent -Wunused-value warning. */
7585 TREE_USED (result
) = true;
7587 /* If new_return is set, assign function to that expr and cast the
7588 result to void since the generic interface returned void. */
7591 /* Cast function result from I{1,2,4,8,16} to the required type. */
7592 result
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (new_return
), result
);
7593 result
= build2 (MODIFY_EXPR
, TREE_TYPE (new_return
), new_return
,
7595 TREE_SIDE_EFFECTS (result
) = 1;
7596 protected_set_expr_location (result
, loc
);
7597 result
= convert (void_type_node
, result
);
7607 /* vector_types_compatible_elements_p is used in type checks of vectors
7608 values used as operands of binary operators. Where it returns true, and
7609 the other checks of the caller succeed (being vector types in he first
7610 place, and matching number of elements), we can just treat the types
7611 as essentially the same.
7612 Contrast with vector_targets_convertible_p, which is used for vector
7613 pointer types, and vector_types_convertible_p, which will allow
7614 language-specific matches under the control of flag_lax_vector_conversions,
7615 and might still require a conversion. */
7616 /* True if vector types T1 and T2 can be inputs to the same binary
7617 operator without conversion.
7618 We don't check the overall vector size here because some of our callers
7619 want to give different error messages when the vectors are compatible
7620 except for the element count. */
7623 vector_types_compatible_elements_p (tree t1
, tree t2
)
7625 bool opaque
= TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
);
7626 t1
= TREE_TYPE (t1
);
7627 t2
= TREE_TYPE (t2
);
7629 enum tree_code c1
= TREE_CODE (t1
), c2
= TREE_CODE (t2
);
7631 gcc_assert ((INTEGRAL_TYPE_P (t1
)
7633 || c1
== FIXED_POINT_TYPE
)
7634 && (INTEGRAL_TYPE_P (t2
)
7636 || c2
== FIXED_POINT_TYPE
));
7638 t1
= c_common_signed_type (t1
);
7639 t2
= c_common_signed_type (t2
);
7640 /* Equality works here because c_common_signed_type uses
7641 TYPE_MAIN_VARIANT. */
7644 if (opaque
&& c1
== c2
7645 && (INTEGRAL_TYPE_P (t1
) || c1
== REAL_TYPE
)
7646 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
))
7651 /* Check for missing format attributes on function pointers. LTYPE is
7652 the new type or left-hand side type. RTYPE is the old type or
7653 right-hand side type. Returns TRUE if LTYPE is missing the desired
7657 check_missing_format_attribute (tree ltype
, tree rtype
)
7659 tree
const ttr
= TREE_TYPE (rtype
), ttl
= TREE_TYPE (ltype
);
7662 for (ra
= TYPE_ATTRIBUTES (ttr
); ra
; ra
= TREE_CHAIN (ra
))
7663 if (is_attribute_p ("format", get_attribute_name (ra
)))
7668 for (la
= TYPE_ATTRIBUTES (ttl
); la
; la
= TREE_CHAIN (la
))
7669 if (is_attribute_p ("format", get_attribute_name (la
)))
7677 /* Setup a TYPE_DECL node as a typedef representation.
7679 X is a TYPE_DECL for a typedef statement. Create a brand new
7680 ..._TYPE node (which will be just a variant of the existing
7681 ..._TYPE node with identical properties) and then install X
7682 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7684 The whole point here is to end up with a situation where each
7685 and every ..._TYPE node the compiler creates will be uniquely
7686 associated with AT MOST one node representing a typedef name.
7687 This way, even though the compiler substitutes corresponding
7688 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7689 early on, later parts of the compiler can always do the reverse
7690 translation and get back the corresponding typedef name. For
7693 typedef struct S MY_TYPE;
7696 Later parts of the compiler might only know that `object' was of
7697 type `struct S' if it were not for code just below. With this
7698 code however, later parts of the compiler see something like:
7700 struct S' == struct S
7701 typedef struct S' MY_TYPE;
7704 And they can then deduce (from the node for type struct S') that
7705 the original object declaration was:
7709 Being able to do this is important for proper support of protoize,
7710 and also for generating precise symbolic debugging information
7711 which takes full account of the programmer's (typedef) vocabulary.
7713 Obviously, we don't want to generate a duplicate ..._TYPE node if
7714 the TYPE_DECL node that we are now processing really represents a
7715 standard built-in type. */
7718 set_underlying_type (tree x
)
7720 if (x
== error_mark_node
)
7722 if (DECL_IS_BUILTIN (x
) && TREE_CODE (TREE_TYPE (x
)) != ARRAY_TYPE
)
7724 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
7725 TYPE_NAME (TREE_TYPE (x
)) = x
;
7727 else if (TREE_TYPE (x
) != error_mark_node
7728 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
7730 tree tt
= TREE_TYPE (x
);
7731 DECL_ORIGINAL_TYPE (x
) = tt
;
7732 tt
= build_variant_type_copy (tt
);
7733 TYPE_STUB_DECL (tt
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
7736 /* Mark the type as used only when its type decl is decorated
7737 with attribute unused. */
7738 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x
)))
7745 /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
7746 the user in diagnostics, false if it would be better to use TYPE itself.
7747 TYPE is known to satisfy typedef_variant_p. */
7750 user_facing_original_type_p (const_tree type
)
7752 gcc_assert (typedef_variant_p (type
));
7753 tree decl
= TYPE_NAME (type
);
7755 /* Look through any typedef in "user" code. */
7756 if (!DECL_IN_SYSTEM_HEADER (decl
) && !DECL_IS_BUILTIN (decl
))
7759 /* If the original type is also named and is in the user namespace,
7760 assume it too is a user-facing type. */
7761 tree orig_type
= DECL_ORIGINAL_TYPE (decl
);
7762 if (tree orig_id
= TYPE_IDENTIFIER (orig_type
))
7763 if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id
)))
7766 switch (TREE_CODE (orig_type
))
7768 /* Don't look through to an anonymous vector type, since the syntax
7769 we use for them in diagnostics isn't real C or C++ syntax.
7770 And if ORIG_TYPE is named but in the implementation namespace,
7771 TYPE is likely to be more meaningful to the user. */
7775 /* Don't expose anonymous tag types that are presumably meant to be
7776 known by their typedef name. Also don't expose tags that are in
7777 the implementation namespace, such as:
7779 typedef struct __foo foo; */
7785 /* Look through to anything else. */
7791 /* Record the types used by the current global variable declaration
7792 being parsed, so that we can decide later to emit their debug info.
7793 Those types are in types_used_by_cur_var_decl, and we are going to
7794 store them in the types_used_by_vars_hash hash table.
7795 DECL is the declaration of the global variable that has been parsed. */
7798 record_types_used_by_current_var_decl (tree decl
)
7800 gcc_assert (decl
&& DECL_P (decl
) && TREE_STATIC (decl
));
7802 while (types_used_by_cur_var_decl
&& !types_used_by_cur_var_decl
->is_empty ())
7804 tree type
= types_used_by_cur_var_decl
->pop ();
7805 types_used_by_var_decl_insert (type
, decl
);
7809 /* The C and C++ parsers both use vectors to hold function arguments.
7810 For efficiency, we keep a cache of unused vectors. This is the
7813 typedef vec
<tree
, va_gc
> *tree_gc_vec
;
7814 static GTY((deletable
)) vec
<tree_gc_vec
, va_gc
> *tree_vector_cache
;
7816 /* Return a new vector from the cache. If the cache is empty,
7817 allocate a new vector. These vectors are GC'ed, so it is OK if the
7818 pointer is not released.. */
7821 make_tree_vector (void)
7823 if (tree_vector_cache
&& !tree_vector_cache
->is_empty ())
7824 return tree_vector_cache
->pop ();
7827 /* Passing 0 to vec::alloc returns NULL, and our callers require
7828 that we always return a non-NULL value. The vector code uses
7829 4 when growing a NULL vector, so we do too. */
7830 vec
<tree
, va_gc
> *v
;
7836 /* Release a vector of trees back to the cache. */
7839 release_tree_vector (vec
<tree
, va_gc
> *vec
)
7844 vec_safe_push (tree_vector_cache
, vec
);
7848 /* Get a new tree vector holding a single tree. */
7851 make_tree_vector_single (tree t
)
7853 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7854 ret
->quick_push (t
);
7858 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7861 make_tree_vector_from_list (tree list
)
7863 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7864 for (; list
; list
= TREE_CHAIN (list
))
7865 vec_safe_push (ret
, TREE_VALUE (list
));
7869 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7872 make_tree_vector_from_ctor (tree ctor
)
7874 vec
<tree
,va_gc
> *ret
= make_tree_vector ();
7875 vec_safe_reserve (ret
, CONSTRUCTOR_NELTS (ctor
));
7876 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (ctor
); ++i
)
7877 ret
->quick_push (CONSTRUCTOR_ELT (ctor
, i
)->value
);
7881 /* Get a new tree vector which is a copy of an existing one. */
7884 make_tree_vector_copy (const vec
<tree
, va_gc
> *orig
)
7886 vec
<tree
, va_gc
> *ret
;
7890 ret
= make_tree_vector ();
7891 vec_safe_reserve (ret
, vec_safe_length (orig
));
7892 FOR_EACH_VEC_SAFE_ELT (orig
, ix
, t
)
7893 ret
->quick_push (t
);
7897 /* Return true if KEYWORD starts a type specifier. */
7900 keyword_begins_type_specifier (enum rid keyword
)
7934 if (keyword
>= RID_FIRST_INT_N
7935 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
7936 && int_n_enabled_p
[keyword
-RID_FIRST_INT_N
])
7942 /* Return true if KEYWORD names a type qualifier. */
7945 keyword_is_type_qualifier (enum rid keyword
)
7959 /* Return true if KEYWORD names a storage class specifier.
7961 RID_TYPEDEF is not included in this list despite `typedef' being
7962 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7963 such for syntactic convenience only. */
7966 keyword_is_storage_class_specifier (enum rid keyword
)
7982 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7985 keyword_is_function_specifier (enum rid keyword
)
7999 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
8000 declaration-specifier (C99 6.7). */
8003 keyword_is_decl_specifier (enum rid keyword
)
8005 if (keyword_is_storage_class_specifier (keyword
)
8006 || keyword_is_type_qualifier (keyword
)
8007 || keyword_is_function_specifier (keyword
))
8022 /* Initialize language-specific-bits of tree_contains_struct. */
8025 c_common_init_ts (void)
8027 MARK_TS_EXP (SIZEOF_EXPR
);
8028 MARK_TS_EXP (C_MAYBE_CONST_EXPR
);
8029 MARK_TS_EXP (EXCESS_PRECISION_EXPR
);
8032 /* Build a user-defined numeric literal out of an integer constant type VALUE
8033 with identifier SUFFIX. */
8036 build_userdef_literal (tree suffix_id
, tree value
,
8037 enum overflow_type overflow
, tree num_string
)
8039 tree literal
= make_node (USERDEF_LITERAL
);
8040 USERDEF_LITERAL_SUFFIX_ID (literal
) = suffix_id
;
8041 USERDEF_LITERAL_VALUE (literal
) = value
;
8042 USERDEF_LITERAL_OVERFLOW (literal
) = overflow
;
8043 USERDEF_LITERAL_NUM_STRING (literal
) = num_string
;
8047 /* For vector[index], convert the vector to an array of the underlying type.
8048 Return true if the resulting ARRAY_REF should not be an lvalue. */
8051 convert_vector_to_array_for_subscript (location_t loc
,
8052 tree
*vecp
, tree index
)
8055 if (gnu_vector_type_p (TREE_TYPE (*vecp
)))
8057 tree type
= TREE_TYPE (*vecp
);
8059 ret
= !lvalue_p (*vecp
);
8061 index
= fold_for_warn (index
);
8062 if (TREE_CODE (index
) == INTEGER_CST
)
8063 if (!tree_fits_uhwi_p (index
)
8064 || maybe_ge (tree_to_uhwi (index
), TYPE_VECTOR_SUBPARTS (type
)))
8065 warning_at (loc
, OPT_Warray_bounds
, "index value is out of bound");
8067 /* We are building an ARRAY_REF so mark the vector as addressable
8068 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
8069 for function parameters. */
8070 c_common_mark_addressable_vec (*vecp
);
8072 *vecp
= build1 (VIEW_CONVERT_EXPR
,
8073 build_array_type_nelts (TREE_TYPE (type
),
8074 TYPE_VECTOR_SUBPARTS (type
)),
8080 /* Determine which of the operands, if any, is a scalar that needs to be
8081 converted to a vector, for the range of operations. */
8083 scalar_to_vector (location_t loc
, enum tree_code code
, tree op0
, tree op1
,
8086 tree type0
= TREE_TYPE (op0
);
8087 tree type1
= TREE_TYPE (op1
);
8088 bool integer_only_op
= false;
8089 enum stv_conv ret
= stv_firstarg
;
8091 gcc_assert (gnu_vector_type_p (type0
) || gnu_vector_type_p (type1
));
8094 /* Most GENERIC binary expressions require homogeneous arguments.
8095 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
8096 argument that is a vector and a second one that is a scalar, so
8097 we never return stv_secondarg for them. */
8100 if (TREE_CODE (type0
) == INTEGER_TYPE
8101 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
8103 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
8107 error_at (loc
, "conversion of scalar %qT to vector %qT "
8108 "involves truncation", type0
, type1
);
8112 return stv_firstarg
;
8119 integer_only_op
= true;
8127 case TRUNC_DIV_EXPR
:
8129 case FLOOR_DIV_EXPR
:
8130 case ROUND_DIV_EXPR
:
8131 case EXACT_DIV_EXPR
:
8132 case TRUNC_MOD_EXPR
:
8133 case FLOOR_MOD_EXPR
:
8141 /* What about UNLT_EXPR? */
8142 if (gnu_vector_type_p (type0
))
8144 ret
= stv_secondarg
;
8145 std::swap (type0
, type1
);
8146 std::swap (op0
, op1
);
8149 if (TREE_CODE (type0
) == INTEGER_TYPE
8150 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
8152 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
8156 error_at (loc
, "conversion of scalar %qT to vector %qT "
8157 "involves truncation", type0
, type1
);
8162 else if (!integer_only_op
8163 /* Allow integer --> real conversion if safe. */
8164 && (TREE_CODE (type0
) == REAL_TYPE
8165 || TREE_CODE (type0
) == INTEGER_TYPE
)
8166 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1
)))
8168 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
8172 error_at (loc
, "conversion of scalar %qT to vector %qT "
8173 "involves truncation", type0
, type1
);
8185 /* Return the alignment of std::max_align_t.
8187 [support.types.layout] The type max_align_t is a POD type whose alignment
8188 requirement is at least as great as that of every scalar type, and whose
8189 alignment requirement is supported in every context. */
8192 max_align_t_align ()
8194 unsigned int max_align
= MAX (TYPE_ALIGN (long_long_integer_type_node
),
8195 TYPE_ALIGN (long_double_type_node
));
8196 if (float128_type_node
!= NULL_TREE
)
8197 max_align
= MAX (max_align
, TYPE_ALIGN (float128_type_node
));
8201 /* Return true iff ALIGN is an integral constant that is a fundamental
8202 alignment, as defined by [basic.align] in the c++-11
8207 [A fundamental alignment is represented by an alignment less than or
8208 equal to the greatest alignment supported by the implementation
8209 in all contexts, which is equal to alignof(max_align_t)]. */
8212 cxx_fundamental_alignment_p (unsigned align
)
8214 return (align
<= max_align_t_align ());
8217 /* Return true if T is a pointer to a zero-sized aggregate. */
8220 pointer_to_zero_sized_aggr_p (tree t
)
8222 if (!POINTER_TYPE_P (t
))
8225 return (TYPE_SIZE (t
) && integer_zerop (TYPE_SIZE (t
)));
8228 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8229 with no library fallback or for an ADDR_EXPR whose operand is such type
8230 issues an error pointing to the location LOC.
8231 Returns true when the expression has been diagnosed and false
8235 reject_gcc_builtin (const_tree expr
, location_t loc
/* = UNKNOWN_LOCATION */)
8237 if (TREE_CODE (expr
) == ADDR_EXPR
)
8238 expr
= TREE_OPERAND (expr
, 0);
8240 STRIP_ANY_LOCATION_WRAPPER (expr
);
8242 if (TREE_TYPE (expr
)
8243 && TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
8244 && TREE_CODE (expr
) == FUNCTION_DECL
8245 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8246 false positives for user-declared built-ins such as abs or
8247 strlen, and for C++ operators new and delete.
8248 The c_decl_implicit() test avoids false positives for implicitly
8249 declared built-ins with library fallbacks (such as abs). */
8250 && fndecl_built_in_p (expr
)
8251 && DECL_IS_BUILTIN (expr
)
8252 && !c_decl_implicit (expr
)
8253 && !DECL_ASSEMBLER_NAME_SET_P (expr
))
8255 if (loc
== UNKNOWN_LOCATION
)
8256 loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
8258 /* Reject arguments that are built-in functions with
8259 no library fallback. */
8260 error_at (loc
, "built-in function %qE must be directly called", expr
);
8268 /* Issue an ERROR for an invalid SIZE of array NAME which is null
8269 for unnamed arrays. */
8272 invalid_array_size_error (location_t loc
, cst_size_error error
,
8273 const_tree size
, const_tree name
)
8275 tree maxsize
= max_object_size ();
8278 case cst_size_not_constant
:
8280 error_at (loc
, "size of array %qE is not a constant expression",
8283 error_at (loc
, "size of array is not a constant expression");
8285 case cst_size_negative
:
8287 error_at (loc
, "size %qE of array %qE is negative",
8290 error_at (loc
, "size %qE of array is negative",
8293 case cst_size_too_big
:
8295 error_at (loc
, "size %qE of array %qE exceeds maximum "
8296 "object size %qE", size
, name
, maxsize
);
8298 error_at (loc
, "size %qE of array exceeds maximum "
8299 "object size %qE", size
, maxsize
);
8301 case cst_size_overflow
:
8303 error_at (loc
, "size of array %qE exceeds maximum "
8304 "object size %qE", name
, maxsize
);
8306 error_at (loc
, "size of array exceeds maximum "
8307 "object size %qE", maxsize
);
8314 /* Check if array size calculations overflow or if the array covers more
8315 than half of the address space. Return true if the size of the array
8316 is valid, false otherwise. T is either the type of the array or its
8317 size, and NAME is the name of the array, or null for unnamed arrays. */
8320 valid_array_size_p (location_t loc
, const_tree t
, tree name
, bool complain
)
8322 if (t
== error_mark_node
)
8328 if (!COMPLETE_TYPE_P (t
))
8330 size
= TYPE_SIZE_UNIT (t
);
8335 if (TREE_CODE (size
) != INTEGER_CST
)
8338 cst_size_error error
;
8339 if (valid_constant_size_p (size
, &error
))
8345 if (TREE_CODE (TREE_TYPE (size
)) == ENUMERAL_TYPE
)
8346 /* Show the value of the enumerator rather than its name. */
8347 size
= convert (ssizetype
, const_cast<tree
> (size
));
8349 invalid_array_size_error (loc
, error
, size
, name
);
8353 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8354 timestamp to replace embedded current dates to get reproducible
8355 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
8358 cb_get_source_date_epoch (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
8360 char *source_date_epoch
;
8364 source_date_epoch
= getenv ("SOURCE_DATE_EPOCH");
8365 if (!source_date_epoch
)
8369 #if defined(INT64_T_IS_LONG)
8370 epoch
= strtol (source_date_epoch
, &endptr
, 10);
8372 epoch
= strtoll (source_date_epoch
, &endptr
, 10);
8374 if (errno
!= 0 || endptr
== source_date_epoch
|| *endptr
!= '\0'
8375 || epoch
< 0 || epoch
> MAX_SOURCE_DATE_EPOCH
)
8377 error_at (input_location
, "environment variable %qs must "
8378 "expand to a non-negative integer less than or equal to %wd",
8379 "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH
);
8383 return (time_t) epoch
;
8386 /* Callback for libcpp for offering spelling suggestions for misspelled
8387 directives. GOAL is an unrecognized string; CANDIDATES is a
8388 NULL-terminated array of candidate strings. Return the closest
8389 match to GOAL within CANDIDATES, or NULL if none are good
8393 cb_get_suggestion (cpp_reader
*, const char *goal
,
8394 const char *const *candidates
)
8396 best_match
<const char *, const char *> bm (goal
);
8398 bm
.consider (*candidates
++);
8399 return bm
.get_best_meaningful_candidate ();
8402 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8403 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8404 by C TS 18661-3 for interchange types that are computed in their
8405 native precision are larger than the C11 values for evaluating in the
8406 precision of float/double/long double. If either mode is
8407 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8409 enum flt_eval_method
8410 excess_precision_mode_join (enum flt_eval_method x
,
8411 enum flt_eval_method y
)
8413 if (x
== FLT_EVAL_METHOD_UNPREDICTABLE
8414 || y
== FLT_EVAL_METHOD_UNPREDICTABLE
)
8415 return FLT_EVAL_METHOD_UNPREDICTABLE
;
8417 /* GCC only supports one interchange type right now, _Float16. If
8418 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8419 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8420 if (x
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8422 if (y
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8425 /* Other values for flt_eval_method are directly comparable, and we want
8430 /* Return the value that should be set for FLT_EVAL_METHOD in the
8431 context of ISO/IEC TS 18861-3.
8433 This relates to the effective excess precision seen by the user,
8434 which is the join point of the precision the target requests for
8435 -fexcess-precision={standard,fast} and the implicit excess precision
8438 static enum flt_eval_method
8439 c_ts18661_flt_eval_method (void)
8441 enum flt_eval_method implicit
8442 = targetm
.c
.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT
);
8444 enum excess_precision_type flag_type
8445 = (flag_excess_precision
== EXCESS_PRECISION_STANDARD
8446 ? EXCESS_PRECISION_TYPE_STANDARD
8447 : EXCESS_PRECISION_TYPE_FAST
);
8449 enum flt_eval_method requested
8450 = targetm
.c
.excess_precision (flag_type
);
8452 return excess_precision_mode_join (implicit
, requested
);
8455 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8456 those that were permitted by C11. That is to say, eliminates
8457 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8459 static enum flt_eval_method
8460 c_c11_flt_eval_method (void)
8462 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8463 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT
);
8466 /* Return the value that should be set for FLT_EVAL_METHOD.
8467 MAYBE_C11_ONLY_P is TRUE if we should check
8468 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8469 values we can return to those from C99/C11, and FALSE otherwise.
8470 See the comments on c_ts18661_flt_eval_method for what value we choose
8474 c_flt_eval_method (bool maybe_c11_only_p
)
8476 if (maybe_c11_only_p
8477 && flag_permitted_flt_eval_methods
8478 == PERMITTED_FLT_EVAL_METHODS_C11
)
8479 return c_c11_flt_eval_method ();
8481 return c_ts18661_flt_eval_method ();
8484 /* An enum for get_missing_token_insertion_kind for describing the best
8485 place to insert a missing token, if there is one. */
8487 enum missing_token_insertion_kind
8490 MTIK_INSERT_BEFORE_NEXT
,
8491 MTIK_INSERT_AFTER_PREV
8494 /* Given a missing token of TYPE, determine if it is reasonable to
8495 emit a fix-it hint suggesting the insertion of the token, and,
8496 if so, where the token should be inserted relative to other tokens.
8498 It only makes sense to do this for values of TYPE that are symbols.
8500 Some symbols should go before the next token, e.g. in:
8502 we want to insert the missing '(' immediately before "flag",
8507 These use MTIK_INSERT_BEFORE_NEXT.
8509 Other symbols should go after the previous token, e.g. in:
8512 we want to insert the missing ')' immediately after the "flag",
8519 These use MTIK_INSERT_AFTER_PREV. */
8521 static enum missing_token_insertion_kind
8522 get_missing_token_insertion_kind (enum cpp_ttype type
)
8526 /* Insert missing "opening" brackets immediately
8527 before the next token. */
8528 case CPP_OPEN_SQUARE
:
8529 case CPP_OPEN_PAREN
:
8530 return MTIK_INSERT_BEFORE_NEXT
;
8532 /* Insert other missing symbols immediately after
8533 the previous token. */
8534 case CPP_CLOSE_PAREN
:
8535 case CPP_CLOSE_SQUARE
:
8539 return MTIK_INSERT_AFTER_PREV
;
8541 /* Other kinds of token don't get fix-it hints. */
8543 return MTIK_IMPOSSIBLE
;
8547 /* Given RICHLOC, a location for a diagnostic describing a missing token
8548 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8549 insertion of the token.
8551 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8553 (a) immediately after PREV_TOKEN_LOC, or
8555 (b) immediately before the primary location within RICHLOC (taken to
8556 be that of the token following where the token was expected).
8558 If we manage to add a fix-it hint, then the location of the
8559 fix-it hint is likely to be more useful as the primary location
8560 of the diagnostic than that of the following token, so we swap
8563 For example, given this bogus code:
8564 123456789012345678901234567890
8565 1 | int missing_semicolon (void)
8572 "expected ';' before '}'"
8574 RICHLOC's primary location is at the closing brace, so before "swapping"
8575 we would emit the error at line 4 column 1:
8577 123456789012345678901234567890
8578 3 | return 42 |< fix-it hint emitted for this line
8580 4 | } |< "expected ';' before '}'" emitted at this line
8583 It's more useful for the location of the diagnostic to be at the
8584 fix-it hint, so we swap the locations, so the primary location
8585 is at the fix-it hint, with the old primary location inserted
8586 as a secondary location, giving this, with the error at line 3
8589 123456789012345678901234567890
8590 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8591 | ^ | with fix-it hint
8593 | } |< secondary range emitted here
8597 maybe_suggest_missing_token_insertion (rich_location
*richloc
,
8598 enum cpp_ttype token_type
,
8599 location_t prev_token_loc
)
8601 gcc_assert (richloc
);
8603 enum missing_token_insertion_kind mtik
8604 = get_missing_token_insertion_kind (token_type
);
8612 case MTIK_IMPOSSIBLE
:
8615 case MTIK_INSERT_BEFORE_NEXT
:
8616 /* Attempt to add the fix-it hint before the primary location
8618 richloc
->add_fixit_insert_before (cpp_type2name (token_type
, 0));
8621 case MTIK_INSERT_AFTER_PREV
:
8622 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8623 richloc
->add_fixit_insert_after (prev_token_loc
,
8624 cpp_type2name (token_type
, 0));
8628 /* If we were successful, use the fix-it hint's location as the
8629 primary location within RICHLOC, adding the old primary location
8630 back as a secondary location. */
8631 if (!richloc
->seen_impossible_fixit_p ())
8633 fixit_hint
*hint
= richloc
->get_last_fixit_hint ();
8634 location_t hint_loc
= hint
->get_start_loc ();
8635 location_t old_loc
= richloc
->get_loc ();
8637 richloc
->set_range (0, hint_loc
, SHOW_RANGE_WITH_CARET
);
8638 richloc
->add_range (old_loc
);
8644 namespace selftest
{
8646 /* Verify that fold_for_warn on error_mark_node is safe. */
8649 test_fold_for_warn ()
8651 ASSERT_EQ (error_mark_node
, fold_for_warn (error_mark_node
));
8654 /* Run all of the selftests within this file. */
8659 test_fold_for_warn ();
8662 /* Run all of the tests within c-family. */
8665 c_family_tests (void)
8667 c_common_c_tests ();
8668 c_format_c_tests ();
8669 c_indentation_c_tests ();
8670 c_pretty_print_c_tests ();
8671 c_spellcheck_cc_tests ();
8674 } // namespace selftest
8676 #endif /* #if CHECKING_P */
8678 /* Attempt to locate a suitable location within FILE for a
8679 #include directive to be inserted before. FILE should
8680 be a string from libcpp (pointer equality is used).
8681 LOC is the location of the relevant diagnostic.
8683 Attempt to return the location within FILE immediately
8684 after the last #include within that file, or the start of
8685 that file if it has no #include directives.
8687 Return UNKNOWN_LOCATION if no suitable location is found,
8688 or if an error occurs. */
8691 try_to_locate_new_include_insertion_point (const char *file
, location_t loc
)
8693 /* Locate the last ordinary map within FILE that ended with a #include. */
8694 const line_map_ordinary
*last_include_ord_map
= NULL
;
8696 /* ...and the next ordinary map within FILE after that one. */
8697 const line_map_ordinary
*last_ord_map_after_include
= NULL
;
8699 /* ...and the first ordinary map within FILE. */
8700 const line_map_ordinary
*first_ord_map_in_file
= NULL
;
8702 /* Get ordinary map containing LOC (or its expansion). */
8703 const line_map_ordinary
*ord_map_for_loc
= NULL
;
8704 linemap_resolve_location (line_table
, loc
, LRK_MACRO_EXPANSION_POINT
,
8706 gcc_assert (ord_map_for_loc
);
8708 for (unsigned int i
= 0; i
< LINEMAPS_ORDINARY_USED (line_table
); i
++)
8710 const line_map_ordinary
*ord_map
8711 = LINEMAPS_ORDINARY_MAP_AT (line_table
, i
);
8713 if (const line_map_ordinary
*from
8714 = linemap_included_from_linemap (line_table
, ord_map
))
8715 if (from
->to_file
== file
)
8717 last_include_ord_map
= from
;
8718 last_ord_map_after_include
= NULL
;
8721 if (ord_map
->to_file
== file
)
8723 if (!first_ord_map_in_file
)
8724 first_ord_map_in_file
= ord_map
;
8725 if (last_include_ord_map
&& !last_ord_map_after_include
)
8726 last_ord_map_after_include
= ord_map
;
8729 /* Stop searching when reaching the ord_map containing LOC,
8730 as it makes no sense to provide fix-it hints that appear
8731 after the diagnostic in question. */
8732 if (ord_map
== ord_map_for_loc
)
8736 /* Determine where to insert the #include. */
8737 const line_map_ordinary
*ord_map_for_insertion
;
8739 /* We want the next ordmap in the file after the last one that's a
8740 #include, but failing that, the start of the file. */
8741 if (last_ord_map_after_include
)
8742 ord_map_for_insertion
= last_ord_map_after_include
;
8744 ord_map_for_insertion
= first_ord_map_in_file
;
8746 if (!ord_map_for_insertion
)
8747 return UNKNOWN_LOCATION
;
8749 /* The "start_location" is column 0, meaning "the whole line".
8750 rich_location and edit_context can't cope with this, so use
8751 column 1 instead. */
8752 location_t col_0
= ord_map_for_insertion
->start_location
;
8753 return linemap_position_for_loc_and_offset (line_table
, col_0
, 1);
8756 /* A map from filenames to sets of headers added to them, for
8757 ensuring idempotency within maybe_add_include_fixit. */
8759 /* The values within the map. We need string comparison as there's
8760 no guarantee that two different diagnostics that are recommending
8761 adding e.g. "<stdio.h>" are using the same buffer. */
8763 typedef hash_set
<const char *, false, nofree_string_hash
> per_file_includes_t
;
8765 /* The map itself. We don't need string comparison for the filename keys,
8766 as they come from libcpp. */
8768 typedef hash_map
<const char *, per_file_includes_t
*> added_includes_t
;
8769 static added_includes_t
*added_includes
;
8771 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8772 in a suitable location within the file of RICHLOC's primary
8775 This function is idempotent: a header will be added at most once to
8778 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8779 printed, then RICHLOC's primary location will be replaced by that of
8780 the fix-it hint (for use by "inform" notes where the location of the
8781 issue has already been reported). */
8784 maybe_add_include_fixit (rich_location
*richloc
, const char *header
,
8785 bool override_location
)
8787 location_t loc
= richloc
->get_loc ();
8788 const char *file
= LOCATION_FILE (loc
);
8792 /* Idempotency: don't add the same header more than once to a given file. */
8793 if (!added_includes
)
8794 added_includes
= new added_includes_t ();
8795 per_file_includes_t
*&set
= added_includes
->get_or_insert (file
);
8797 if (set
->contains (header
))
8798 /* ...then we've already added HEADER to that file. */
8801 set
= new per_file_includes_t ();
8804 /* Attempt to locate a suitable place for the new directive. */
8805 location_t include_insert_loc
8806 = try_to_locate_new_include_insertion_point (file
, loc
);
8807 if (include_insert_loc
== UNKNOWN_LOCATION
)
8810 char *text
= xasprintf ("#include %s\n", header
);
8811 richloc
->add_fixit_insert_before (include_insert_loc
, text
);
8814 if (override_location
&& global_dc
->show_caret
)
8816 /* Replace the primary location with that of the insertion point for the
8819 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8820 caret for the insertion point (or colorize it).
8822 Hence we print e.g.:
8824 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8825 73 | # include <debug/vector>
8826 +++ |+#include <vector>
8831 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8832 73 | # include <debug/vector>
8833 +++ |+#include <vector>
8837 avoiding the caret on the first column of line 74. */
8838 richloc
->set_range (0, include_insert_loc
, SHOW_LINES_WITHOUT_RANGE
);
8842 /* Attempt to convert a braced array initializer list CTOR for array
8843 TYPE into a STRING_CST for convenience and efficiency. Return
8844 the converted string on success or the original ctor on failure. */
8847 braced_list_to_string (tree type
, tree ctor
, bool member
)
8849 /* Ignore non-members with unknown size like arrays with unspecified
8851 tree typesize
= TYPE_SIZE_UNIT (type
);
8852 if (!member
&& !tree_fits_uhwi_p (typesize
))
8855 /* If the array has an explicit bound, use it to constrain the size
8856 of the string. If it doesn't, be sure to create a string that's
8857 as long as implied by the index of the last zero specified via
8858 a designator, as in:
8859 const char a[] = { [7] = 0 }; */
8860 unsigned HOST_WIDE_INT maxelts
;
8863 maxelts
= tree_to_uhwi (typesize
);
8864 maxelts
/= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type
)));
8867 maxelts
= HOST_WIDE_INT_M1U
;
8869 /* Avoid converting initializers for zero-length arrays (but do
8870 create them for flexible array members). */
8874 unsigned HOST_WIDE_INT nelts
= CONSTRUCTOR_NELTS (ctor
);
8877 str
.reserve (nelts
+ 1);
8879 unsigned HOST_WIDE_INT i
;
8882 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, index
, value
)
8884 unsigned HOST_WIDE_INT idx
= i
;
8887 if (!tree_fits_uhwi_p (index
))
8889 idx
= tree_to_uhwi (index
);
8892 /* auto_vec is limited to UINT_MAX elements. */
8896 /* Avoid non-constant initializers. */
8897 if (!tree_fits_shwi_p (value
))
8900 /* Skip over embedded nuls except the last one (initializer
8901 elements are in ascending order of indices). */
8902 HOST_WIDE_INT val
= tree_to_shwi (value
);
8903 if (!val
&& i
+ 1 < nelts
)
8906 if (idx
< str
.length())
8909 /* Bail if the CTOR has a block of more than 256 embedded nuls
8910 due to implicitly initialized elements. */
8911 unsigned nchars
= (idx
- str
.length ()) + 1;
8918 str
.quick_grow_cleared (idx
);
8924 str
.safe_insert (idx
, val
);
8927 /* Append a nul string termination. */
8928 if (maxelts
!= HOST_WIDE_INT_M1U
&& str
.length () < maxelts
)
8931 /* Build a STRING_CST with the same type as the array. */
8932 tree res
= build_string (str
.length (), str
.begin ());
8933 TREE_TYPE (res
) = type
;
8937 /* Implementation of the two-argument braced_lists_to_string withe
8938 the same arguments plus MEMBER which is set for struct members
8939 to allow initializers for flexible member arrays. */
8942 braced_lists_to_strings (tree type
, tree ctor
, bool member
)
8944 if (TREE_CODE (ctor
) != CONSTRUCTOR
)
8947 tree_code code
= TREE_CODE (type
);
8950 if (code
== ARRAY_TYPE
)
8951 ttp
= TREE_TYPE (type
);
8952 else if (code
== RECORD_TYPE
)
8954 ttp
= TREE_TYPE (ctor
);
8955 if (TREE_CODE (ttp
) == ARRAY_TYPE
)
8958 ttp
= TREE_TYPE (ttp
);
8964 if ((TREE_CODE (ttp
) == ARRAY_TYPE
|| TREE_CODE (ttp
) == INTEGER_TYPE
)
8965 && TYPE_STRING_FLAG (ttp
))
8966 return braced_list_to_string (type
, ctor
, member
);
8968 code
= TREE_CODE (ttp
);
8969 if (code
== ARRAY_TYPE
|| RECORD_OR_UNION_TYPE_P (ttp
))
8971 bool rec
= RECORD_OR_UNION_TYPE_P (ttp
);
8973 /* Handle array of arrays or struct member initializers. */
8975 unsigned HOST_WIDE_INT idx
;
8976 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, val
)
8978 val
= braced_lists_to_strings (ttp
, val
, rec
);
8979 CONSTRUCTOR_ELT (ctor
, idx
)->value
= val
;
8986 /* Attempt to convert a CTOR containing braced array initializer lists
8987 for array TYPE into one containing STRING_CSTs, for convenience and
8988 efficiency. Recurse for arrays of arrays and member initializers.
8989 Return the converted CTOR or STRING_CST on success or the original
8993 braced_lists_to_strings (tree type
, tree ctor
)
8995 return braced_lists_to_strings (type
, ctor
, false);
8998 #include "gt-c-family-c-common.h"