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"
55 cpp_reader
*parse_in
; /* Declared in c-pragma.h. */
57 /* Mode used to build pointers (VOIDmode means ptr_mode). */
59 machine_mode c_default_pointer_mode
= VOIDmode
;
61 /* The following symbols are subsumed in the c_global_trees array, and
62 listed here individually for documentation purposes.
64 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
66 tree short_integer_type_node;
67 tree long_integer_type_node;
68 tree long_long_integer_type_node;
70 tree short_unsigned_type_node;
71 tree long_unsigned_type_node;
72 tree long_long_unsigned_type_node;
74 tree truthvalue_type_node;
75 tree truthvalue_false_node;
76 tree truthvalue_true_node;
78 tree ptrdiff_type_node;
80 tree unsigned_char_type_node;
81 tree signed_char_type_node;
85 tree char16_type_node;
86 tree char32_type_node;
89 tree double_type_node;
90 tree long_double_type_node;
92 tree complex_integer_type_node;
93 tree complex_float_type_node;
94 tree complex_double_type_node;
95 tree complex_long_double_type_node;
97 tree dfloat32_type_node;
98 tree dfloat64_type_node;
99 tree_dfloat128_type_node;
101 tree intQI_type_node;
102 tree intHI_type_node;
103 tree intSI_type_node;
104 tree intDI_type_node;
105 tree intTI_type_node;
107 tree unsigned_intQI_type_node;
108 tree unsigned_intHI_type_node;
109 tree unsigned_intSI_type_node;
110 tree unsigned_intDI_type_node;
111 tree unsigned_intTI_type_node;
113 tree widest_integer_literal_type_node;
114 tree widest_unsigned_literal_type_node;
116 Nodes for types `void *' and `const void *'.
118 tree ptr_type_node, const_ptr_type_node;
120 Nodes for types `char *' and `const char *'.
122 tree string_type_node, const_string_type_node;
124 Type `char[SOMENUMBER]'.
125 Used when an array of char is needed and the size is irrelevant.
127 tree char_array_type_node;
129 Type `wchar_t[SOMENUMBER]' or something like it.
130 Used when a wide string literal is created.
132 tree wchar_array_type_node;
134 Type `char8_t[SOMENUMBER]' or something like it.
135 Used when a UTF-8 string literal is created.
137 tree char8_array_type_node;
139 Type `char16_t[SOMENUMBER]' or something like it.
140 Used when a UTF-16 string literal is created.
142 tree char16_array_type_node;
144 Type `char32_t[SOMENUMBER]' or something like it.
145 Used when a UTF-32 string literal is created.
147 tree char32_array_type_node;
149 Type `int ()' -- used for implicit declaration of functions.
151 tree default_function_type;
153 A VOID_TYPE node, packaged in a TREE_LIST.
157 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
158 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
159 VAR_DECLS, but C++ does.)
161 tree function_name_decl_node;
162 tree pretty_function_name_decl_node;
163 tree c99_function_name_decl_node;
165 Stack of nested function name VAR_DECLs.
167 tree saved_function_name_decls;
171 tree c_global_trees
[CTI_MAX
];
173 /* Switches common to the C front ends. */
175 /* Nonzero means don't output line number information. */
177 char flag_no_line_commands
;
179 /* Nonzero causes -E output not to be done, but directives such as
180 #define that have side effects are still obeyed. */
184 /* Nonzero means dump macros in some fashion. */
186 char flag_dump_macros
;
188 /* Nonzero means pass #include lines through to the output. */
190 char flag_dump_includes
;
192 /* Nonzero means process PCH files while preprocessing. */
194 bool flag_pch_preprocess
;
196 /* The file name to which we should write a precompiled header, or
197 NULL if no header will be written in this compile. */
199 const char *pch_file
;
201 /* Nonzero if an ISO standard was selected. It rejects macros in the
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch
;
213 /* Nonzero means enable C89 Amendment 1 features. */
217 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
221 /* Nonzero means use the ISO C11 dialect of C. */
225 /* Nonzero means use the ISO C2X dialect of C. */
229 /* Nonzero means that we have builtin functions, and main is an int. */
234 /* ObjC language option variables. */
237 /* Tells the compiler that this is a special run. Do not perform any
238 compiling, instead we are to test some platform dependent features
239 and output a C header file with appropriate definitions. */
241 int print_struct_values
;
243 /* Tells the compiler what is the constant string class for ObjC. */
245 const char *constant_string_class_name
;
248 /* C++ language option variables. */
250 /* The reference version of the ABI for -Wabi. */
252 int warn_abi_version
= -1;
254 /* The C++ dialect being used. Default set in c_common_post_options. */
256 enum cxx_dialect cxx_dialect
= cxx_unset
;
258 /* Maximum template instantiation depth. This limit exists to limit the
259 time it takes to notice excessively recursive template instantiations.
261 The default is lower than the 1024 recommended by the C++0x standard
262 because G++ runs out of stack before 1024 with highly recursive template
263 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
265 int max_tinst_depth
= 900;
267 /* The elements of `ridpointers' are identifier nodes for the reserved
268 type names and storage classes. It is indexed by a RID_... value. */
271 tree (*make_fname_decl
) (location_t
, tree
, int);
273 /* Nonzero means don't warn about problems that occur when the code is
275 int c_inhibit_evaluation_warnings
;
277 /* Whether we are building a boolean conversion inside
278 convert_for_assignment, or some other late binary operation. If
279 build_binary_op is called for C (from code shared by C and C++) in
280 this case, then the operands have already been folded and the
281 result will not be folded again, so C_MAYBE_CONST_EXPR should not
283 bool in_late_binary_op
;
285 /* Whether lexing has been completed, so subsequent preprocessor
286 errors should use the compiler's input_location. */
287 bool done_lexing
= false;
289 /* Information about how a function name is generated. */
292 tree
*const decl
; /* pointer to the VAR_DECL. */
293 const unsigned rid
; /* RID number for the identifier. */
294 const int pretty
; /* How pretty is it? */
297 /* The three ways of getting then name of the current function. */
299 const struct fname_var_t fname_vars
[] =
301 /* C99 compliant __func__, must be first. */
302 {&c99_function_name_decl_node
, RID_C99_FUNCTION_NAME
, 0},
303 /* GCC __FUNCTION__ compliant. */
304 {&function_name_decl_node
, RID_FUNCTION_NAME
, 0},
305 /* GCC __PRETTY_FUNCTION__ compliant. */
306 {&pretty_function_name_decl_node
, RID_PRETTY_FUNCTION_NAME
, 1},
310 /* Global visibility options. */
311 struct visibility_flags visibility_options
;
313 static tree
check_case_value (location_t
, tree
);
316 static void check_nonnull_arg (void *, tree
, unsigned HOST_WIDE_INT
);
317 static bool nonnull_check_p (tree
, unsigned HOST_WIDE_INT
);
319 /* Reserved words. The third field is a mask: keywords are disabled
320 if they match the mask.
323 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
324 C --std=c99: D_CXXONLY | D_OBJC
325 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
326 C++ --std=c++98: D_CONLY | D_CXX11 | D_CXX20 | D_OBJC
327 C++ --std=c++11: D_CONLY | D_CXX20 | D_OBJC
328 C++ --std=c++20: D_CONLY | D_OBJC
329 ObjC++ is like C++ except that D_OBJC is not set
331 If -fno-asm is used, D_ASM is added to the mask. If
332 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
333 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
334 In C with -Wc++-compat, we warn if D_CXXWARN is set.
336 Note the complication of the D_CXX_OBJC keywords. These are
337 reserved words such as 'class'. In C++, 'class' is a reserved
338 word. In Objective-C++ it is too. In Objective-C, it is a
339 reserved word too, but only if it follows an '@' sign.
341 const struct c_common_resword c_common_reswords
[] =
343 { "_Alignas", RID_ALIGNAS
, D_CONLY
},
344 { "_Alignof", RID_ALIGNOF
, D_CONLY
},
345 { "_Atomic", RID_ATOMIC
, D_CONLY
},
346 { "_Bool", RID_BOOL
, D_CONLY
},
347 { "_Complex", RID_COMPLEX
, 0 },
348 { "_Imaginary", RID_IMAGINARY
, D_CONLY
},
349 { "_Float16", RID_FLOAT16
, D_CONLY
},
350 { "_Float32", RID_FLOAT32
, D_CONLY
},
351 { "_Float64", RID_FLOAT64
, D_CONLY
},
352 { "_Float128", RID_FLOAT128
, D_CONLY
},
353 { "_Float32x", RID_FLOAT32X
, D_CONLY
},
354 { "_Float64x", RID_FLOAT64X
, D_CONLY
},
355 { "_Float128x", RID_FLOAT128X
, D_CONLY
},
356 { "_Decimal32", RID_DFLOAT32
, D_CONLY
},
357 { "_Decimal64", RID_DFLOAT64
, D_CONLY
},
358 { "_Decimal128", RID_DFLOAT128
, D_CONLY
},
359 { "_Fract", RID_FRACT
, D_CONLY
| D_EXT
},
360 { "_Accum", RID_ACCUM
, D_CONLY
| D_EXT
},
361 { "_Sat", RID_SAT
, D_CONLY
| D_EXT
},
362 { "_Static_assert", RID_STATIC_ASSERT
, D_CONLY
},
363 { "_Noreturn", RID_NORETURN
, D_CONLY
},
364 { "_Generic", RID_GENERIC
, D_CONLY
},
365 { "_Thread_local", RID_THREAD
, D_CONLY
},
366 { "__FUNCTION__", RID_FUNCTION_NAME
, 0 },
367 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME
, 0 },
368 { "__alignof", RID_ALIGNOF
, 0 },
369 { "__alignof__", RID_ALIGNOF
, 0 },
370 { "__asm", RID_ASM
, 0 },
371 { "__asm__", RID_ASM
, 0 },
372 { "__attribute", RID_ATTRIBUTE
, 0 },
373 { "__attribute__", RID_ATTRIBUTE
, 0 },
374 { "__auto_type", RID_AUTO_TYPE
, D_CONLY
},
375 { "__bases", RID_BASES
, D_CXXONLY
},
376 { "__builtin_addressof", RID_ADDRESSOF
, D_CXXONLY
},
377 { "__builtin_call_with_static_chain",
378 RID_BUILTIN_CALL_WITH_STATIC_CHAIN
, D_CONLY
},
379 { "__builtin_choose_expr", RID_CHOOSE_EXPR
, D_CONLY
},
380 { "__builtin_complex", RID_BUILTIN_COMPLEX
, D_CONLY
},
381 { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR
, 0 },
382 { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE
, 0 },
383 { "__builtin_launder", RID_BUILTIN_LAUNDER
, D_CXXONLY
},
384 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE
, 0 },
385 { "__builtin_tgmath", RID_BUILTIN_TGMATH
, D_CONLY
},
386 { "__builtin_offsetof", RID_OFFSETOF
, 0 },
387 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P
, D_CONLY
},
388 { "__builtin_va_arg", RID_VA_ARG
, 0 },
389 { "__complex", RID_COMPLEX
, 0 },
390 { "__complex__", RID_COMPLEX
, 0 },
391 { "__const", RID_CONST
, 0 },
392 { "__const__", RID_CONST
, 0 },
393 { "__constinit", RID_CONSTINIT
, D_CXXONLY
},
394 { "__decltype", RID_DECLTYPE
, D_CXXONLY
},
395 { "__direct_bases", RID_DIRECT_BASES
, D_CXXONLY
},
396 { "__extension__", RID_EXTENSION
, 0 },
397 { "__func__", RID_C99_FUNCTION_NAME
, 0 },
398 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN
, D_CXXONLY
},
399 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR
, D_CXXONLY
},
400 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY
, D_CXXONLY
},
401 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN
, D_CXXONLY
},
402 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR
, D_CXXONLY
},
403 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY
, D_CXXONLY
},
404 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR
, D_CXXONLY
},
405 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
,
407 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR
, D_CXXONLY
},
408 { "__imag", RID_IMAGPART
, 0 },
409 { "__imag__", RID_IMAGPART
, 0 },
410 { "__inline", RID_INLINE
, 0 },
411 { "__inline__", RID_INLINE
, 0 },
412 { "__is_abstract", RID_IS_ABSTRACT
, D_CXXONLY
},
413 { "__is_aggregate", RID_IS_AGGREGATE
, D_CXXONLY
},
414 { "__is_base_of", RID_IS_BASE_OF
, D_CXXONLY
},
415 { "__is_class", RID_IS_CLASS
, D_CXXONLY
},
416 { "__is_empty", RID_IS_EMPTY
, D_CXXONLY
},
417 { "__is_enum", RID_IS_ENUM
, D_CXXONLY
},
418 { "__is_final", RID_IS_FINAL
, D_CXXONLY
},
419 { "__is_literal_type", RID_IS_LITERAL_TYPE
, D_CXXONLY
},
420 { "__is_pod", RID_IS_POD
, D_CXXONLY
},
421 { "__is_polymorphic", RID_IS_POLYMORPHIC
, D_CXXONLY
},
422 { "__is_same", RID_IS_SAME_AS
, D_CXXONLY
},
423 { "__is_same_as", RID_IS_SAME_AS
, D_CXXONLY
},
424 { "__is_standard_layout", RID_IS_STD_LAYOUT
, D_CXXONLY
},
425 { "__is_trivial", RID_IS_TRIVIAL
, D_CXXONLY
},
426 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE
, D_CXXONLY
},
427 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE
, D_CXXONLY
},
428 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE
, D_CXXONLY
},
429 { "__is_union", RID_IS_UNION
, D_CXXONLY
},
430 { "__label__", RID_LABEL
, 0 },
431 { "__null", RID_NULL
, 0 },
432 { "__real", RID_REALPART
, 0 },
433 { "__real__", RID_REALPART
, 0 },
434 { "__restrict", RID_RESTRICT
, 0 },
435 { "__restrict__", RID_RESTRICT
, 0 },
436 { "__signed", RID_SIGNED
, 0 },
437 { "__signed__", RID_SIGNED
, 0 },
438 { "__thread", RID_THREAD
, 0 },
439 { "__transaction_atomic", RID_TRANSACTION_ATOMIC
, 0 },
440 { "__transaction_relaxed", RID_TRANSACTION_RELAXED
, 0 },
441 { "__transaction_cancel", RID_TRANSACTION_CANCEL
, 0 },
442 { "__typeof", RID_TYPEOF
, 0 },
443 { "__typeof__", RID_TYPEOF
, 0 },
444 { "__underlying_type", RID_UNDERLYING_TYPE
, D_CXXONLY
},
445 { "__volatile", RID_VOLATILE
, 0 },
446 { "__volatile__", RID_VOLATILE
, 0 },
447 { "__GIMPLE", RID_GIMPLE
, D_CONLY
},
448 { "__PHI", RID_PHI
, D_CONLY
},
449 { "__RTL", RID_RTL
, D_CONLY
},
450 { "alignas", RID_ALIGNAS
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
451 { "alignof", RID_ALIGNOF
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
452 { "asm", RID_ASM
, D_ASM
},
453 { "auto", RID_AUTO
, 0 },
454 { "bool", RID_BOOL
, D_CXXONLY
| D_CXXWARN
},
455 { "break", RID_BREAK
, 0 },
456 { "case", RID_CASE
, 0 },
457 { "catch", RID_CATCH
, D_CXX_OBJC
| D_CXXWARN
},
458 { "char", RID_CHAR
, 0 },
459 { "char8_t", RID_CHAR8
, D_CXX_CHAR8_T_FLAGS
| D_CXXWARN
},
460 { "char16_t", RID_CHAR16
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
461 { "char32_t", RID_CHAR32
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
462 { "class", RID_CLASS
, D_CXX_OBJC
| D_CXXWARN
},
463 { "const", RID_CONST
, 0 },
464 { "consteval", RID_CONSTEVAL
, D_CXXONLY
| D_CXX20
| D_CXXWARN
},
465 { "constexpr", RID_CONSTEXPR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
466 { "constinit", RID_CONSTINIT
, D_CXXONLY
| D_CXX20
| D_CXXWARN
},
467 { "const_cast", RID_CONSTCAST
, D_CXXONLY
| D_CXXWARN
},
468 { "continue", RID_CONTINUE
, 0 },
469 { "decltype", RID_DECLTYPE
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
470 { "default", RID_DEFAULT
, 0 },
471 { "delete", RID_DELETE
, D_CXXONLY
| D_CXXWARN
},
473 { "double", RID_DOUBLE
, 0 },
474 { "dynamic_cast", RID_DYNCAST
, D_CXXONLY
| D_CXXWARN
},
475 { "else", RID_ELSE
, 0 },
476 { "enum", RID_ENUM
, 0 },
477 { "explicit", RID_EXPLICIT
, D_CXXONLY
| D_CXXWARN
},
478 { "export", RID_EXPORT
, D_CXXONLY
| D_CXXWARN
},
479 { "extern", RID_EXTERN
, 0 },
480 { "false", RID_FALSE
, D_CXXONLY
| D_CXXWARN
},
481 { "float", RID_FLOAT
, 0 },
482 { "for", RID_FOR
, 0 },
483 { "friend", RID_FRIEND
, D_CXXONLY
| D_CXXWARN
},
484 { "goto", RID_GOTO
, 0 },
486 { "inline", RID_INLINE
, D_EXT89
},
487 { "int", RID_INT
, 0 },
488 { "long", RID_LONG
, 0 },
489 { "mutable", RID_MUTABLE
, D_CXXONLY
| D_CXXWARN
},
490 { "namespace", RID_NAMESPACE
, D_CXXONLY
| D_CXXWARN
},
491 { "new", RID_NEW
, D_CXXONLY
| D_CXXWARN
},
492 { "noexcept", RID_NOEXCEPT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
493 { "nullptr", RID_NULLPTR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
494 { "operator", RID_OPERATOR
, D_CXXONLY
| D_CXXWARN
},
495 { "private", RID_PRIVATE
, D_CXX_OBJC
| D_CXXWARN
},
496 { "protected", RID_PROTECTED
, D_CXX_OBJC
| D_CXXWARN
},
497 { "public", RID_PUBLIC
, D_CXX_OBJC
| D_CXXWARN
},
498 { "register", RID_REGISTER
, 0 },
499 { "reinterpret_cast", RID_REINTCAST
, D_CXXONLY
| D_CXXWARN
},
500 { "restrict", RID_RESTRICT
, D_CONLY
| D_C99
},
501 { "return", RID_RETURN
, 0 },
502 { "short", RID_SHORT
, 0 },
503 { "signed", RID_SIGNED
, 0 },
504 { "sizeof", RID_SIZEOF
, 0 },
505 { "static", RID_STATIC
, 0 },
506 { "static_assert", RID_STATIC_ASSERT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
507 { "static_cast", RID_STATCAST
, D_CXXONLY
| D_CXXWARN
},
508 { "struct", RID_STRUCT
, 0 },
509 { "switch", RID_SWITCH
, 0 },
510 { "template", RID_TEMPLATE
, D_CXXONLY
| D_CXXWARN
},
511 { "this", RID_THIS
, D_CXXONLY
| D_CXXWARN
},
512 { "thread_local", RID_THREAD
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
513 { "throw", RID_THROW
, D_CXX_OBJC
| D_CXXWARN
},
514 { "true", RID_TRUE
, D_CXXONLY
| D_CXXWARN
},
515 { "try", RID_TRY
, D_CXX_OBJC
| D_CXXWARN
},
516 { "typedef", RID_TYPEDEF
, 0 },
517 { "typename", RID_TYPENAME
, D_CXXONLY
| D_CXXWARN
},
518 { "typeid", RID_TYPEID
, D_CXXONLY
| D_CXXWARN
},
519 { "typeof", RID_TYPEOF
, D_ASM
| D_EXT
},
520 { "union", RID_UNION
, 0 },
521 { "unsigned", RID_UNSIGNED
, 0 },
522 { "using", RID_USING
, D_CXXONLY
| D_CXXWARN
},
523 { "virtual", RID_VIRTUAL
, D_CXXONLY
| D_CXXWARN
},
524 { "void", RID_VOID
, 0 },
525 { "volatile", RID_VOLATILE
, 0 },
526 { "wchar_t", RID_WCHAR
, D_CXXONLY
},
527 { "while", RID_WHILE
, 0 },
528 { "__is_assignable", RID_IS_ASSIGNABLE
, D_CXXONLY
},
529 { "__is_constructible", RID_IS_CONSTRUCTIBLE
, D_CXXONLY
},
531 /* C++ transactional memory. */
532 { "synchronized", RID_SYNCHRONIZED
, D_CXX_OBJC
| D_TRANSMEM
},
533 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT
, D_CXXONLY
| D_TRANSMEM
},
534 { "atomic_cancel", RID_ATOMIC_CANCEL
, D_CXXONLY
| D_TRANSMEM
},
535 { "atomic_commit", RID_TRANSACTION_ATOMIC
, D_CXXONLY
| D_TRANSMEM
},
537 /* Concepts-related keywords */
538 { "concept", RID_CONCEPT
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
539 { "requires", RID_REQUIRES
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
541 /* Coroutines-related keywords */
542 { "co_await", RID_CO_AWAIT
, D_CXX_COROUTINES_FLAGS
| D_CXXWARN
},
543 { "co_yield", RID_CO_YIELD
, D_CXX_COROUTINES_FLAGS
| D_CXXWARN
},
544 { "co_return", RID_CO_RETURN
, D_CXX_COROUTINES_FLAGS
| D_CXXWARN
},
546 /* These Objective-C keywords are recognized only immediately after
548 { "compatibility_alias", RID_AT_ALIAS
, D_OBJC
},
549 { "defs", RID_AT_DEFS
, D_OBJC
},
550 { "encode", RID_AT_ENCODE
, D_OBJC
},
551 { "end", RID_AT_END
, D_OBJC
},
552 { "implementation", RID_AT_IMPLEMENTATION
, D_OBJC
},
553 { "interface", RID_AT_INTERFACE
, D_OBJC
},
554 { "protocol", RID_AT_PROTOCOL
, D_OBJC
},
555 { "selector", RID_AT_SELECTOR
, D_OBJC
},
556 { "finally", RID_AT_FINALLY
, D_OBJC
},
557 { "optional", RID_AT_OPTIONAL
, D_OBJC
},
558 { "required", RID_AT_REQUIRED
, D_OBJC
},
559 { "property", RID_AT_PROPERTY
, D_OBJC
},
560 { "package", RID_AT_PACKAGE
, D_OBJC
},
561 { "synthesize", RID_AT_SYNTHESIZE
, D_OBJC
},
562 { "dynamic", RID_AT_DYNAMIC
, D_OBJC
},
563 /* These are recognized only in protocol-qualifier context
565 { "bycopy", RID_BYCOPY
, D_OBJC
},
566 { "byref", RID_BYREF
, D_OBJC
},
567 { "in", RID_IN
, D_OBJC
},
568 { "inout", RID_INOUT
, D_OBJC
},
569 { "oneway", RID_ONEWAY
, D_OBJC
},
570 { "out", RID_OUT
, D_OBJC
},
571 /* These are recognized inside a property attribute list */
572 { "assign", RID_ASSIGN
, D_OBJC
},
573 { "copy", RID_COPY
, D_OBJC
},
574 { "getter", RID_GETTER
, D_OBJC
},
575 { "nonatomic", RID_NONATOMIC
, D_OBJC
},
576 { "readonly", RID_READONLY
, D_OBJC
},
577 { "readwrite", RID_READWRITE
, D_OBJC
},
578 { "retain", RID_RETAIN
, D_OBJC
},
579 { "setter", RID_SETTER
, D_OBJC
},
582 const unsigned int num_c_common_reswords
=
583 sizeof c_common_reswords
/ sizeof (struct c_common_resword
);
585 /* Return identifier for address space AS. */
588 c_addr_space_name (addr_space_t as
)
590 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
591 gcc_assert (ridpointers
[rid
]);
592 return IDENTIFIER_POINTER (ridpointers
[rid
]);
595 /* Push current bindings for the function name VAR_DECLS. */
598 start_fname_decls (void)
601 tree saved
= NULL_TREE
;
603 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
605 tree decl
= *fname_vars
[ix
].decl
;
609 saved
= tree_cons (decl
, build_int_cst (integer_type_node
, ix
),
611 *fname_vars
[ix
].decl
= NULL_TREE
;
614 if (saved
|| saved_function_name_decls
)
615 /* Normally they'll have been NULL, so only push if we've got a
616 stack, or they are non-NULL. */
617 saved_function_name_decls
= tree_cons (saved
, NULL_TREE
,
618 saved_function_name_decls
);
621 /* Finish up the current bindings, adding them into the current function's
622 statement tree. This must be done _before_ finish_stmt_tree is called.
623 If there is no current function, we must be at file scope and no statements
624 are involved. Pop the previous bindings. */
627 finish_fname_decls (void)
630 tree stmts
= NULL_TREE
;
631 tree stack
= saved_function_name_decls
;
633 for (; stack
&& TREE_VALUE (stack
); stack
= TREE_CHAIN (stack
))
634 append_to_statement_list (TREE_VALUE (stack
), &stmts
);
638 tree
*bodyp
= &DECL_SAVED_TREE (current_function_decl
);
640 if (TREE_CODE (*bodyp
) == BIND_EXPR
)
641 bodyp
= &BIND_EXPR_BODY (*bodyp
);
643 append_to_statement_list_force (*bodyp
, &stmts
);
647 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
648 *fname_vars
[ix
].decl
= NULL_TREE
;
652 /* We had saved values, restore them. */
655 for (saved
= TREE_PURPOSE (stack
); saved
; saved
= TREE_CHAIN (saved
))
657 tree decl
= TREE_PURPOSE (saved
);
658 unsigned ix
= TREE_INT_CST_LOW (TREE_VALUE (saved
));
660 *fname_vars
[ix
].decl
= decl
;
662 stack
= TREE_CHAIN (stack
);
664 saved_function_name_decls
= stack
;
667 /* Return the text name of the current function, suitably prettified
668 by PRETTY_P. Return string must be freed by caller. */
671 fname_as_string (int pretty_p
)
673 const char *name
= "top level";
676 cpp_string cstr
= { 0, 0 }, strname
;
684 if (current_function_decl
)
685 name
= lang_hooks
.decl_printable_name (current_function_decl
, vrb
);
687 len
= strlen (name
) + 3; /* Two for '"'s. One for NULL. */
689 namep
= XNEWVEC (char, len
);
690 snprintf (namep
, len
, "\"%s\"", name
);
691 strname
.text
= (unsigned char *) namep
;
692 strname
.len
= len
- 1;
694 if (cpp_interpret_string (parse_in
, &strname
, 1, &cstr
, CPP_STRING
))
697 return (const char *) cstr
.text
;
703 /* Return the VAR_DECL for a const char array naming the current
704 function. If the VAR_DECL has not yet been created, create it
705 now. RID indicates how it should be formatted and IDENTIFIER_NODE
706 ID is its name (unfortunately C and C++ hold the RID values of
707 keywords in different places, so we can't derive RID from ID in
708 this language independent code. LOC is the location of the
712 fname_decl (location_t loc
, unsigned int rid
, tree id
)
715 tree decl
= NULL_TREE
;
717 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
718 if (fname_vars
[ix
].rid
== rid
)
721 decl
= *fname_vars
[ix
].decl
;
724 /* If a tree is built here, it would normally have the lineno of
725 the current statement. Later this tree will be moved to the
726 beginning of the function and this line number will be wrong.
727 To avoid this problem set the lineno to 0 here; that prevents
728 it from appearing in the RTL. */
730 location_t saved_location
= input_location
;
731 input_location
= UNKNOWN_LOCATION
;
733 stmts
= push_stmt_list ();
734 decl
= (*make_fname_decl
) (loc
, id
, fname_vars
[ix
].pretty
);
735 stmts
= pop_stmt_list (stmts
);
736 if (!IS_EMPTY_STMT (stmts
))
737 saved_function_name_decls
738 = tree_cons (decl
, stmts
, saved_function_name_decls
);
739 *fname_vars
[ix
].decl
= decl
;
740 input_location
= saved_location
;
742 if (!ix
&& !current_function_decl
)
743 pedwarn (loc
, 0, "%qD is not defined outside of function scope", decl
);
748 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
751 fix_string_type (tree value
)
753 int length
= TREE_STRING_LENGTH (value
);
755 tree e_type
, i_type
, a_type
;
757 /* Compute the number of elements, for the array type. */
758 if (TREE_TYPE (value
) == char_array_type_node
|| !TREE_TYPE (value
))
761 e_type
= char_type_node
;
763 else if (flag_char8_t
&& TREE_TYPE (value
) == char8_array_type_node
)
765 charsz
= TYPE_PRECISION (char8_type_node
) / BITS_PER_UNIT
;
766 e_type
= char8_type_node
;
768 else if (TREE_TYPE (value
) == char16_array_type_node
)
770 charsz
= TYPE_PRECISION (char16_type_node
) / BITS_PER_UNIT
;
771 e_type
= char16_type_node
;
773 else if (TREE_TYPE (value
) == char32_array_type_node
)
775 charsz
= TYPE_PRECISION (char32_type_node
) / BITS_PER_UNIT
;
776 e_type
= char32_type_node
;
780 charsz
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
781 e_type
= wchar_type_node
;
784 /* This matters only for targets where ssizetype has smaller precision
786 if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype
)), length
))
788 error ("size of string literal is too large");
789 length
= tree_to_shwi (TYPE_MAX_VALUE (ssizetype
)) / charsz
* charsz
;
790 char *str
= CONST_CAST (char *, TREE_STRING_POINTER (value
));
791 memset (str
+ length
, '\0',
792 MIN (TREE_STRING_LENGTH (value
) - length
, charsz
));
793 TREE_STRING_LENGTH (value
) = length
;
795 nchars
= length
/ charsz
;
797 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
798 limit in C++98 Annex B is very large (65536) and is not normative,
799 so we do not diagnose it (warn_overlength_strings is forced off
800 in c_common_post_options). */
801 if (warn_overlength_strings
)
803 const int nchars_max
= flag_isoc99
? 4095 : 509;
804 const int relevant_std
= flag_isoc99
? 99 : 90;
805 if (nchars
- 1 > nchars_max
)
806 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
807 separate the %d from the 'C'. 'ISO' should not be
808 translated, but it may be moved after 'C%d' in languages
809 where modifiers follow nouns. */
810 pedwarn (input_location
, OPT_Woverlength_strings
,
811 "string length %qd is greater than the length %qd "
812 "ISO C%d compilers are required to support",
813 nchars
- 1, nchars_max
, relevant_std
);
816 /* Create the array type for the string constant. The ISO C++
817 standard says that a string literal has type `const char[N]' or
818 `const wchar_t[N]'. We use the same logic when invoked as a C
819 front-end with -Wwrite-strings.
820 ??? We should change the type of an expression depending on the
821 state of a warning flag. We should just be warning -- see how
822 this is handled in the C++ front-end for the deprecated implicit
823 conversion from string literals to `char*' or `wchar_t*'.
825 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
826 array type being the unqualified version of that type.
827 Therefore, if we are constructing an array of const char, we must
828 construct the matching unqualified array type first. The C front
829 end does not require this, but it does no harm, so we do it
831 i_type
= build_index_type (size_int (nchars
- 1));
832 a_type
= build_array_type (e_type
, i_type
);
833 if (c_dialect_cxx() || warn_write_strings
)
834 a_type
= c_build_qualified_type (a_type
, TYPE_QUAL_CONST
);
836 TREE_TYPE (value
) = a_type
;
837 TREE_CONSTANT (value
) = 1;
838 TREE_READONLY (value
) = 1;
839 TREE_STATIC (value
) = 1;
843 /* Given a string of type STRING_TYPE, determine what kind of string
844 token would give an equivalent execution encoding: CPP_STRING,
845 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
846 This may not be exactly the string token type that initially created
847 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
848 string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
851 This effectively reverses part of the logic in lex_string and
854 static enum cpp_ttype
855 get_cpp_ttype_from_string_type (tree string_type
)
857 gcc_assert (string_type
);
858 if (TREE_CODE (string_type
) == POINTER_TYPE
)
859 string_type
= TREE_TYPE (string_type
);
861 if (TREE_CODE (string_type
) != ARRAY_TYPE
)
864 tree element_type
= TREE_TYPE (string_type
);
865 if (TREE_CODE (element_type
) != INTEGER_TYPE
)
868 int bits_per_character
= TYPE_PRECISION (element_type
);
869 switch (bits_per_character
)
872 return CPP_STRING
; /* It could have also been CPP_UTF8STRING. */
882 /* The global record of string concatentations, for use in
883 extracting locations within string literals. */
885 GTY(()) string_concat_db
*g_string_concat_db
;
887 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
890 c_get_substring_location (const substring_loc
&substr_loc
,
893 enum cpp_ttype tok_type
894 = get_cpp_ttype_from_string_type (substr_loc
.get_string_type ());
895 if (tok_type
== CPP_OTHER
)
896 return "unrecognized string type";
898 return get_location_within_string (parse_in
, g_string_concat_db
,
899 substr_loc
.get_fmt_string_loc (),
901 substr_loc
.get_caret_idx (),
902 substr_loc
.get_start_idx (),
903 substr_loc
.get_end_idx (),
908 /* Return true iff T is a boolean promoted to int. */
911 bool_promoted_to_int_p (tree t
)
913 return (CONVERT_EXPR_P (t
)
914 && TREE_TYPE (t
) == integer_type_node
915 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == BOOLEAN_TYPE
);
918 /* vector_targets_convertible_p is used for vector pointer types. The
919 callers perform various checks that the qualifiers are satisfactory,
920 while OTOH vector_targets_convertible_p ignores the number of elements
921 in the vectors. That's fine with vector pointers as we can consider,
922 say, a vector of 8 elements as two consecutive vectors of 4 elements,
923 and that does not require and conversion of the pointer values.
924 In contrast, vector_types_convertible_p and
925 vector_types_compatible_elements_p are used for vector value types. */
926 /* True if pointers to distinct types T1 and T2 can be converted to
927 each other without an explicit cast. Only returns true for opaque
930 vector_targets_convertible_p (const_tree t1
, const_tree t2
)
932 if (VECTOR_TYPE_P (t1
) && VECTOR_TYPE_P (t2
)
933 && (TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
934 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
940 /* vector_types_convertible_p is used for vector value types.
941 It could in principle call vector_targets_convertible_p as a subroutine,
942 but then the check for vector type would be duplicated with its callers,
943 and also the purpose of vector_targets_convertible_p would become
945 Where vector_types_convertible_p returns true, a conversion might still be
946 needed to make the types match.
947 In contrast, vector_targets_convertible_p is used for vector pointer
948 values, and vector_types_compatible_elements_p is used specifically
949 in the context for binary operators, as a check if use is possible without
951 /* True if vector types T1 and T2 can be converted to each other
952 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
953 can only be converted with -flax-vector-conversions yet that is not
954 in effect, emit a note telling the user about that option if such
955 a note has not previously been emitted. */
957 vector_types_convertible_p (const_tree t1
, const_tree t2
, bool emit_lax_note
)
959 static bool emitted_lax_note
= false;
960 bool convertible_lax
;
962 if ((TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
963 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
967 (tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
))
968 && (TREE_CODE (TREE_TYPE (t1
)) != REAL_TYPE
969 || known_eq (TYPE_VECTOR_SUBPARTS (t1
),
970 TYPE_VECTOR_SUBPARTS (t2
)))
971 && (INTEGRAL_TYPE_P (TREE_TYPE (t1
))
972 == INTEGRAL_TYPE_P (TREE_TYPE (t2
))));
974 if (!convertible_lax
|| flag_lax_vector_conversions
)
975 return convertible_lax
;
977 if (known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
978 && lang_hooks
.types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
981 if (emit_lax_note
&& !emitted_lax_note
)
983 emitted_lax_note
= true;
984 inform (input_location
, "use %<-flax-vector-conversions%> to permit "
985 "conversions between vectors with differing "
986 "element types or numbers of subparts");
992 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
993 and have vector types, V0 has the same type as V1, and the number of
994 elements of V0, V1, MASK is the same.
996 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
997 called with two arguments. In this case implementation passes the
998 first argument twice in order to share the same tree code. This fact
999 could enable the mask-values being twice the vector length. This is
1000 an implementation accident and this semantics is not guaranteed to
1003 c_build_vec_perm_expr (location_t loc
, tree v0
, tree v1
, tree mask
,
1008 bool maybe_const
= false;
1009 bool two_arguments
= false;
1011 if (v1
== NULL_TREE
)
1013 two_arguments
= true;
1017 if (v0
== error_mark_node
|| v1
== error_mark_node
1018 || mask
== error_mark_node
)
1019 return error_mark_node
;
1021 if (!gnu_vector_type_p (TREE_TYPE (mask
))
1022 || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask
)))
1025 error_at (loc
, "%<__builtin_shuffle%> last argument must "
1026 "be an integer vector");
1027 return error_mark_node
;
1030 if (!gnu_vector_type_p (TREE_TYPE (v0
))
1031 || !gnu_vector_type_p (TREE_TYPE (v1
)))
1034 error_at (loc
, "%<__builtin_shuffle%> arguments must be vectors");
1035 return error_mark_node
;
1038 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0
)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1
)))
1041 error_at (loc
, "%<__builtin_shuffle%> argument vectors must be of "
1043 return error_mark_node
;
1046 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0
)),
1047 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
)))
1048 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1
)),
1049 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
))))
1052 error_at (loc
, "%<__builtin_shuffle%> number of elements of the "
1053 "argument vector(s) and the mask vector should "
1055 return error_mark_node
;
1058 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0
))))
1059 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask
)))))
1062 error_at (loc
, "%<__builtin_shuffle%> argument vector(s) inner type "
1063 "must have the same size as inner type of the mask");
1064 return error_mark_node
;
1067 if (!c_dialect_cxx ())
1069 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1070 v0
= c_fully_fold (v0
, false, &maybe_const
);
1071 wrap
&= maybe_const
;
1074 v1
= v0
= save_expr (v0
);
1077 v1
= c_fully_fold (v1
, false, &maybe_const
);
1078 wrap
&= maybe_const
;
1081 mask
= c_fully_fold (mask
, false, &maybe_const
);
1082 wrap
&= maybe_const
;
1084 else if (two_arguments
)
1085 v1
= v0
= save_expr (v0
);
1087 ret
= build3_loc (loc
, VEC_PERM_EXPR
, TREE_TYPE (v0
), v0
, v1
, mask
);
1089 if (!c_dialect_cxx () && !wrap
)
1090 ret
= c_wrap_maybe_const (ret
, true);
1095 /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1098 c_build_vec_convert (location_t loc1
, tree expr
, location_t loc2
, tree type
,
1101 if (error_operand_p (type
))
1102 return error_mark_node
;
1103 if (error_operand_p (expr
))
1104 return error_mark_node
;
1106 if (!gnu_vector_type_p (TREE_TYPE (expr
))
1107 || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1108 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr
))))
1111 error_at (loc1
, "%<__builtin_convertvector%> first argument must "
1112 "be an integer or floating vector");
1113 return error_mark_node
;
1116 if (!gnu_vector_type_p (type
)
1117 || (!VECTOR_INTEGER_TYPE_P (type
) && !VECTOR_FLOAT_TYPE_P (type
)))
1120 error_at (loc2
, "%<__builtin_convertvector%> second argument must "
1121 "be an integer or floating vector type");
1122 return error_mark_node
;
1125 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr
)),
1126 TYPE_VECTOR_SUBPARTS (type
)))
1129 error_at (loc1
, "%<__builtin_convertvector%> number of elements "
1130 "of the first argument vector and the second argument "
1131 "vector type should be the same");
1132 return error_mark_node
;
1135 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr
)))
1136 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
1137 || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1138 && VECTOR_INTEGER_TYPE_P (type
)
1139 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr
)))
1140 == TYPE_PRECISION (TREE_TYPE (type
)))))
1141 return build1_loc (loc1
, VIEW_CONVERT_EXPR
, type
, expr
);
1144 bool maybe_const
= false;
1146 if (!c_dialect_cxx ())
1148 /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1149 expr
= c_fully_fold (expr
, false, &maybe_const
);
1150 wrap
&= maybe_const
;
1153 ret
= build_call_expr_internal_loc (loc1
, IFN_VEC_CONVERT
, type
, 1, expr
);
1156 ret
= c_wrap_maybe_const (ret
, true);
1161 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1162 to integral type. */
1165 c_common_get_narrower (tree op
, int *unsignedp_ptr
)
1167 op
= get_narrower (op
, unsignedp_ptr
);
1169 if (TREE_CODE (TREE_TYPE (op
)) == ENUMERAL_TYPE
1170 && ENUM_IS_SCOPED (TREE_TYPE (op
)))
1172 /* C++0x scoped enumerations don't implicitly convert to integral
1173 type; if we stripped an explicit conversion to a larger type we
1174 need to replace it so common_type will still work. */
1175 tree type
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op
)),
1176 TYPE_UNSIGNED (TREE_TYPE (op
)));
1177 op
= fold_convert (type
, op
);
1182 /* This is a helper function of build_binary_op.
1184 For certain operations if both args were extended from the same
1185 smaller type, do the arithmetic in that type and then extend.
1187 BITWISE indicates a bitwise operation.
1188 For them, this optimization is safe only if
1189 both args are zero-extended or both are sign-extended.
1190 Otherwise, we might change the result.
1191 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1192 but calculated in (unsigned short) it would be (unsigned short)-1.
1195 shorten_binary_op (tree result_type
, tree op0
, tree op1
, bool bitwise
)
1197 int unsigned0
, unsigned1
;
1202 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1203 excessive narrowing when we call get_narrower below. For
1204 example, suppose that OP0 is of unsigned int extended
1205 from signed char and that RESULT_TYPE is long long int.
1206 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1209 (long long int) (unsigned int) signed_char
1211 which get_narrower would narrow down to
1213 (unsigned int) signed char
1215 If we do not cast OP0 first, get_narrower would return
1216 signed_char, which is inconsistent with the case of the
1218 op0
= convert (result_type
, op0
);
1219 op1
= convert (result_type
, op1
);
1221 arg0
= c_common_get_narrower (op0
, &unsigned0
);
1222 arg1
= c_common_get_narrower (op1
, &unsigned1
);
1224 /* UNS is 1 if the operation to be done is an unsigned one. */
1225 uns
= TYPE_UNSIGNED (result_type
);
1227 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1228 but it *requires* conversion to FINAL_TYPE. */
1230 if ((TYPE_PRECISION (TREE_TYPE (op0
))
1231 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1232 && TREE_TYPE (op0
) != result_type
)
1233 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1234 if ((TYPE_PRECISION (TREE_TYPE (op1
))
1235 == TYPE_PRECISION (TREE_TYPE (arg1
)))
1236 && TREE_TYPE (op1
) != result_type
)
1237 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1239 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1241 /* For bitwise operations, signedness of nominal type
1242 does not matter. Consider only how operands were extended. */
1246 /* Note that in all three cases below we refrain from optimizing
1247 an unsigned operation on sign-extended args.
1248 That would not be valid. */
1250 /* Both args variable: if both extended in same way
1251 from same width, do it in that width.
1252 Do it unsigned if args were zero-extended. */
1253 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
1254 < TYPE_PRECISION (result_type
))
1255 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1256 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1257 && unsigned0
== unsigned1
1258 && (unsigned0
|| !uns
))
1259 return c_common_signed_or_unsigned_type
1260 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
1262 else if (TREE_CODE (arg0
) == INTEGER_CST
1263 && (unsigned1
|| !uns
)
1264 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1265 < TYPE_PRECISION (result_type
))
1267 = c_common_signed_or_unsigned_type (unsigned1
,
1269 && !POINTER_TYPE_P (type
)
1270 && int_fits_type_p (arg0
, type
))
1273 else if (TREE_CODE (arg1
) == INTEGER_CST
1274 && (unsigned0
|| !uns
)
1275 && (TYPE_PRECISION (TREE_TYPE (arg0
))
1276 < TYPE_PRECISION (result_type
))
1278 = c_common_signed_or_unsigned_type (unsigned0
,
1280 && !POINTER_TYPE_P (type
)
1281 && int_fits_type_p (arg1
, type
))
1287 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1288 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1291 int_safely_convertible_to_real_p (const_tree from_type
, const_tree to_type
)
1293 tree type_low_bound
= TYPE_MIN_VALUE (from_type
);
1294 tree type_high_bound
= TYPE_MAX_VALUE (from_type
);
1295 REAL_VALUE_TYPE real_low_bound
=
1296 real_value_from_int_cst (0, type_low_bound
);
1297 REAL_VALUE_TYPE real_high_bound
=
1298 real_value_from_int_cst (0, type_high_bound
);
1300 return exact_real_truncate (TYPE_MODE (to_type
), &real_low_bound
)
1301 && exact_real_truncate (TYPE_MODE (to_type
), &real_high_bound
);
1304 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1305 to the complex/real/integer type TYPE. Function returns non-zero when:
1306 * EXPR is a constant which cannot be exactly converted to TYPE.
1307 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1308 for EXPR type and TYPE being both integers or both real, or both
1310 * EXPR is not a constant of complex type and TYPE is a real or
1312 * EXPR is not a constant of real type and TYPE is an integer.
1313 * EXPR is not a constant of integer type which cannot be
1314 exactly converted to real type.
1316 Function allows conversions between types of different signedness if
1317 CHECK_SIGN is false and can return SAFE_CONVERSION (zero) in that
1318 case. Function can return UNSAFE_SIGN if CHECK_SIGN is true.
1320 RESULT, when non-null is the result of the conversion. When constant
1321 it is included in the text of diagnostics.
1323 Function allows conversions from complex constants to non-complex types,
1324 provided that imaginary part is zero and real part can be safely converted
1327 enum conversion_safety
1328 unsafe_conversion_p (tree type
, tree expr
, tree result
, bool check_sign
)
1330 enum conversion_safety give_warning
= SAFE_CONVERSION
; /* is 0 or false */
1331 tree expr_type
= TREE_TYPE (expr
);
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)
1359 give_warning
= UNSAFE_SIGN
;
1361 else if (!TYPE_UNSIGNED (type
) && TYPE_UNSIGNED (expr_type
))
1364 give_warning
= UNSAFE_SIGN
;
1367 give_warning
= UNSAFE_OTHER
;
1369 else if (TREE_CODE (type
) == REAL_TYPE
)
1371 /* Warn for an integer constant that does not fit into real type. */
1372 if (TREE_CODE (expr_type
) == INTEGER_TYPE
)
1374 REAL_VALUE_TYPE a
= real_value_from_int_cst (0, expr
);
1375 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1376 give_warning
= UNSAFE_REAL
;
1378 /* Warn for a real constant that does not fit into a smaller
1380 else if (TREE_CODE (expr_type
) == REAL_TYPE
1381 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1383 REAL_VALUE_TYPE a
= TREE_REAL_CST (expr
);
1384 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1385 give_warning
= UNSAFE_REAL
;
1390 else if (TREE_CODE (expr
) == COMPLEX_CST
)
1392 tree imag_part
= TREE_IMAGPART (expr
);
1393 /* Conversion from complex constant with zero imaginary part,
1394 perform check for conversion of real part. */
1395 if ((TREE_CODE (imag_part
) == REAL_CST
1396 && real_zerop (imag_part
))
1397 || (TREE_CODE (imag_part
) == INTEGER_CST
1398 && integer_zerop (imag_part
)))
1399 /* Note: in this branch we use recursive call to unsafe_conversion_p
1400 with different type of EXPR, but it is still safe, because when EXPR
1401 is a constant, it's type is not used in text of generated warnings
1402 (otherwise they could sound misleading). */
1403 return unsafe_conversion_p (type
, TREE_REALPART (expr
), result
,
1405 /* Conversion from complex constant with non-zero imaginary part. */
1408 /* Conversion to complex type.
1409 Perform checks for both real and imaginary parts. */
1410 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1412 enum conversion_safety re_safety
=
1413 unsafe_conversion_p (type
, TREE_REALPART (expr
),
1414 result
, check_sign
);
1415 enum conversion_safety im_safety
=
1416 unsafe_conversion_p (type
, imag_part
, result
, check_sign
);
1418 /* Merge the results into appropriate single warning. */
1420 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1421 if (re_safety
== im_safety
)
1422 give_warning
= re_safety
;
1423 else if (!re_safety
&& im_safety
)
1424 give_warning
= im_safety
;
1425 else if (re_safety
&& !im_safety
)
1426 give_warning
= re_safety
;
1428 give_warning
= UNSAFE_OTHER
;
1430 /* Warn about conversion from complex to real or integer type. */
1432 give_warning
= UNSAFE_IMAGINARY
;
1436 /* Checks for remaining case: EXPR is not constant. */
1439 /* Warn for real types converted to integer types. */
1440 if (TREE_CODE (expr_type
) == REAL_TYPE
1441 && TREE_CODE (type
) == INTEGER_TYPE
)
1442 give_warning
= UNSAFE_REAL
;
1444 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1445 && TREE_CODE (type
) == INTEGER_TYPE
)
1447 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1448 expr
= get_unwidened (expr
, 0);
1449 expr_type
= TREE_TYPE (expr
);
1451 /* Don't warn for short y; short x = ((int)y & 0xff); */
1452 if (TREE_CODE (expr
) == BIT_AND_EXPR
1453 || TREE_CODE (expr
) == BIT_IOR_EXPR
1454 || TREE_CODE (expr
) == BIT_XOR_EXPR
)
1456 /* If both args were extended from a shortest type,
1457 use that type if that is safe. */
1458 expr_type
= shorten_binary_op (expr_type
,
1459 TREE_OPERAND (expr
, 0),
1460 TREE_OPERAND (expr
, 1),
1463 if (TREE_CODE (expr
) == BIT_AND_EXPR
)
1465 tree op0
= TREE_OPERAND (expr
, 0);
1466 tree op1
= TREE_OPERAND (expr
, 1);
1467 bool unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1468 bool unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1470 /* If one of the operands is a non-negative constant
1471 that fits in the target type, then the type of the
1472 other operand does not matter. */
1473 if ((TREE_CODE (op0
) == INTEGER_CST
1474 && int_fits_type_p (op0
, c_common_signed_type (type
))
1475 && int_fits_type_p (op0
, c_common_unsigned_type (type
)))
1476 || (TREE_CODE (op1
) == INTEGER_CST
1477 && int_fits_type_p (op1
, c_common_signed_type (type
))
1478 && int_fits_type_p (op1
,
1479 c_common_unsigned_type (type
))))
1480 return SAFE_CONVERSION
;
1481 /* If constant is unsigned and fits in the target
1482 type, then the result will also fit. */
1483 else if ((TREE_CODE (op0
) == INTEGER_CST
1485 && int_fits_type_p (op0
, type
))
1486 || (TREE_CODE (op1
) == INTEGER_CST
1488 && int_fits_type_p (op1
, type
)))
1489 return SAFE_CONVERSION
;
1492 /* Warn for integer types converted to smaller integer types. */
1493 if (TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1494 give_warning
= UNSAFE_OTHER
;
1496 /* When they are the same width but different signedness,
1497 then the value may change. */
1498 else if (((TYPE_PRECISION (type
) == TYPE_PRECISION (expr_type
)
1499 && TYPE_UNSIGNED (expr_type
) != TYPE_UNSIGNED (type
))
1500 /* Even when converted to a bigger type, if the type is
1501 unsigned but expr is signed, then negative values
1503 || (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)))
1505 give_warning
= UNSAFE_SIGN
;
1508 /* Warn for integer types converted to real types if and only if
1509 all the range of values of the integer type cannot be
1510 represented by the real type. */
1511 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1512 && TREE_CODE (type
) == REAL_TYPE
)
1514 /* Don't warn about char y = 0xff; float x = (int) y; */
1515 expr
= get_unwidened (expr
, 0);
1516 expr_type
= TREE_TYPE (expr
);
1518 if (!int_safely_convertible_to_real_p (expr_type
, type
))
1519 give_warning
= UNSAFE_OTHER
;
1522 /* Warn for real types converted to smaller real types. */
1523 else if (TREE_CODE (expr_type
) == REAL_TYPE
1524 && TREE_CODE (type
) == REAL_TYPE
1525 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1526 give_warning
= UNSAFE_REAL
;
1528 /* Check conversion between two complex types. */
1529 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1530 && TREE_CODE (type
) == COMPLEX_TYPE
)
1532 /* Extract underlying types (i.e., type of real and imaginary
1533 parts) of expr_type and type. */
1534 tree from_type
= TREE_TYPE (expr_type
);
1535 tree to_type
= TREE_TYPE (type
);
1537 /* Warn for real types converted to integer types. */
1538 if (TREE_CODE (from_type
) == REAL_TYPE
1539 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1540 give_warning
= UNSAFE_REAL
;
1542 /* Warn for real types converted to smaller real types. */
1543 else if (TREE_CODE (from_type
) == REAL_TYPE
1544 && TREE_CODE (to_type
) == REAL_TYPE
1545 && TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1546 give_warning
= UNSAFE_REAL
;
1548 /* Check conversion for complex integer types. Here implementation
1549 is simpler than for real-domain integers because it does not
1550 involve sophisticated cases, such as bitmasks, casts, etc. */
1551 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1552 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1554 /* Warn for integer types converted to smaller integer types. */
1555 if (TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1556 give_warning
= UNSAFE_OTHER
;
1558 /* Check for different signedness, see case for real-domain
1559 integers (above) for a more detailed comment. */
1560 else if (((TYPE_PRECISION (to_type
) == TYPE_PRECISION (from_type
)
1561 && TYPE_UNSIGNED (to_type
) != TYPE_UNSIGNED (from_type
))
1562 || (TYPE_UNSIGNED (to_type
) && !TYPE_UNSIGNED (from_type
)))
1564 give_warning
= UNSAFE_SIGN
;
1566 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1567 && TREE_CODE (to_type
) == REAL_TYPE
1568 && !int_safely_convertible_to_real_p (from_type
, to_type
))
1569 give_warning
= UNSAFE_OTHER
;
1572 /* Warn for complex types converted to real or integer types. */
1573 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1574 && TREE_CODE (type
) != COMPLEX_TYPE
)
1575 give_warning
= UNSAFE_IMAGINARY
;
1578 return give_warning
;
1582 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1583 Invoke this function on every expression that is converted implicitly,
1584 i.e. because of language rules and not because of an explicit cast. */
1587 convert_and_check (location_t loc
, tree type
, tree expr
)
1590 tree expr_for_warning
;
1592 /* Convert from a value with possible excess precision rather than
1593 via the semantic type, but do not warn about values not fitting
1594 exactly in the semantic type. */
1595 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
1597 tree orig_type
= TREE_TYPE (expr
);
1598 expr
= TREE_OPERAND (expr
, 0);
1599 expr_for_warning
= convert (orig_type
, expr
);
1600 if (orig_type
== type
)
1601 return expr_for_warning
;
1604 expr_for_warning
= expr
;
1606 if (TREE_TYPE (expr
) == type
)
1609 result
= convert (type
, expr
);
1611 if (c_inhibit_evaluation_warnings
== 0
1612 && !TREE_OVERFLOW_P (expr
)
1613 && result
!= error_mark_node
)
1614 warnings_for_convert_and_check (loc
, type
, expr_for_warning
, result
);
1619 /* A node in a list that describes references to variables (EXPR), which are
1620 either read accesses if WRITER is zero, or write accesses, in which case
1621 WRITER is the parent of EXPR. */
1628 /* Used to implement a cache the results of a call to verify_tree. We only
1629 use this for SAVE_EXPRs. */
1632 struct tlist_cache
*next
;
1633 struct tlist
*cache_before_sp
;
1634 struct tlist
*cache_after_sp
;
1638 /* Obstack to use when allocating tlist structures, and corresponding
1640 static struct obstack tlist_obstack
;
1641 static char *tlist_firstobj
= 0;
1643 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1645 static struct tlist
*warned_ids
;
1646 /* SAVE_EXPRs need special treatment. We process them only once and then
1647 cache the results. */
1648 static struct tlist_cache
*save_expr_cache
;
1650 static void add_tlist (struct tlist
**, struct tlist
*, tree
, int);
1651 static void merge_tlist (struct tlist
**, struct tlist
*, int);
1652 static void verify_tree (tree
, struct tlist
**, struct tlist
**, tree
);
1653 static bool warning_candidate_p (tree
);
1654 static bool candidate_equal_p (const_tree
, const_tree
);
1655 static void warn_for_collisions (struct tlist
*);
1656 static void warn_for_collisions_1 (tree
, tree
, struct tlist
*, int);
1657 static struct tlist
*new_tlist (struct tlist
*, tree
, tree
);
1659 /* Create a new struct tlist and fill in its fields. */
1660 static struct tlist
*
1661 new_tlist (struct tlist
*next
, tree t
, tree writer
)
1664 l
= XOBNEW (&tlist_obstack
, struct tlist
);
1671 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1672 is nonnull, we ignore any node we find which has a writer equal to it. */
1675 add_tlist (struct tlist
**to
, struct tlist
*add
, tree exclude_writer
, int copy
)
1679 struct tlist
*next
= add
->next
;
1682 if (!exclude_writer
|| !candidate_equal_p (add
->writer
, exclude_writer
))
1683 *to
= copy
? new_tlist (*to
, add
->expr
, add
->writer
) : add
;
1688 /* Merge the nodes of ADD into TO. This merging process is done so that for
1689 each variable that already exists in TO, no new node is added; however if
1690 there is a write access recorded in ADD, and an occurrence on TO is only
1691 a read access, then the occurrence in TO will be modified to record the
1695 merge_tlist (struct tlist
**to
, struct tlist
*add
, int copy
)
1697 struct tlist
**end
= to
;
1700 end
= &(*end
)->next
;
1706 struct tlist
*next
= add
->next
;
1708 for (tmp2
= *to
; tmp2
; tmp2
= tmp2
->next
)
1709 if (candidate_equal_p (tmp2
->expr
, add
->expr
))
1713 tmp2
->writer
= add
->writer
;
1717 *end
= copy
? new_tlist (NULL
, add
->expr
, add
->writer
) : add
;
1718 end
= &(*end
)->next
;
1725 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1726 references in list LIST conflict with it, excluding reads if ONLY writers
1730 warn_for_collisions_1 (tree written
, tree writer
, struct tlist
*list
,
1735 /* Avoid duplicate warnings. */
1736 for (tmp
= warned_ids
; tmp
; tmp
= tmp
->next
)
1737 if (candidate_equal_p (tmp
->expr
, written
))
1742 if (candidate_equal_p (list
->expr
, written
)
1743 && !candidate_equal_p (list
->writer
, writer
)
1744 && (!only_writes
|| list
->writer
))
1746 warned_ids
= new_tlist (warned_ids
, written
, NULL_TREE
);
1747 warning_at (EXPR_LOC_OR_LOC (writer
, input_location
),
1748 OPT_Wsequence_point
, "operation on %qE may be undefined",
1755 /* Given a list LIST of references to variables, find whether any of these
1756 can cause conflicts due to missing sequence points. */
1759 warn_for_collisions (struct tlist
*list
)
1763 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
1766 warn_for_collisions_1 (tmp
->expr
, tmp
->writer
, list
, 0);
1770 /* Return nonzero if X is a tree that can be verified by the sequence point
1774 warning_candidate_p (tree x
)
1776 if (DECL_P (x
) && DECL_ARTIFICIAL (x
))
1779 if (TREE_CODE (x
) == BLOCK
)
1782 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1783 (lvalue_p) crash on TRY/CATCH. */
1784 if (TREE_TYPE (x
) == NULL_TREE
|| VOID_TYPE_P (TREE_TYPE (x
)))
1790 /* No point to track non-const calls, they will never satisfy
1792 if (TREE_CODE (x
) == CALL_EXPR
&& (call_expr_flags (x
) & ECF_CONST
) == 0)
1795 if (TREE_CODE (x
) == STRING_CST
)
1801 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1803 candidate_equal_p (const_tree x
, const_tree y
)
1805 return (x
== y
) || (x
&& y
&& operand_equal_p (x
, y
, 0));
1808 /* Walk the tree X, and record accesses to variables. If X is written by the
1809 parent tree, WRITER is the parent.
1810 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1811 expression or its only operand forces a sequence point, then everything up
1812 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1814 Once we return, we will have emitted warnings if any subexpression before
1815 such a sequence point could be undefined. On a higher level, however, the
1816 sequence point may not be relevant, and we'll merge the two lists.
1818 Example: (b++, a) + b;
1819 The call that processes the COMPOUND_EXPR will store the increment of B
1820 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1821 processes the PLUS_EXPR will need to merge the two lists so that
1822 eventually, all accesses end up on the same list (and we'll warn about the
1823 unordered subexpressions b++ and b.
1825 A note on merging. If we modify the former example so that our expression
1828 care must be taken not simply to add all three expressions into the final
1829 PNO_SP list. The function merge_tlist takes care of that by merging the
1830 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1831 way, so that no more than one access to B is recorded. */
1834 verify_tree (tree x
, struct tlist
**pbefore_sp
, struct tlist
**pno_sp
,
1837 struct tlist
*tmp_before
, *tmp_nosp
, *tmp_list2
, *tmp_list3
;
1838 enum tree_code code
;
1839 enum tree_code_class cl
;
1841 /* X may be NULL if it is the operand of an empty statement expression
1847 code
= TREE_CODE (x
);
1848 cl
= TREE_CODE_CLASS (code
);
1850 if (warning_candidate_p (x
))
1851 *pno_sp
= new_tlist (*pno_sp
, x
, writer
);
1860 case TRUTH_ANDIF_EXPR
:
1861 case TRUTH_ORIF_EXPR
:
1863 tmp_before
= tmp_nosp
= tmp_list2
= tmp_list3
= 0;
1864 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1865 warn_for_collisions (tmp_nosp
);
1866 merge_tlist (pbefore_sp
, tmp_before
, 0);
1867 merge_tlist (pbefore_sp
, tmp_nosp
, 0);
1868 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1869 warn_for_collisions (tmp_list2
);
1870 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1871 merge_tlist (pno_sp
, tmp_list2
, 0);
1875 tmp_before
= tmp_list2
= 0;
1876 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_list2
, NULL_TREE
);
1877 warn_for_collisions (tmp_list2
);
1878 merge_tlist (pbefore_sp
, tmp_before
, 0);
1879 merge_tlist (pbefore_sp
, tmp_list2
, 0);
1881 tmp_list3
= tmp_nosp
= 0;
1882 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_nosp
, NULL_TREE
);
1883 warn_for_collisions (tmp_nosp
);
1884 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1886 tmp_list3
= tmp_list2
= 0;
1887 verify_tree (TREE_OPERAND (x
, 2), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1888 warn_for_collisions (tmp_list2
);
1889 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1890 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1891 two first, to avoid warning for (a ? b++ : b++). */
1892 merge_tlist (&tmp_nosp
, tmp_list2
, 0);
1893 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
1896 case PREDECREMENT_EXPR
:
1897 case PREINCREMENT_EXPR
:
1898 case POSTDECREMENT_EXPR
:
1899 case POSTINCREMENT_EXPR
:
1900 verify_tree (TREE_OPERAND (x
, 0), pno_sp
, pno_sp
, x
);
1904 tmp_before
= tmp_nosp
= tmp_list3
= 0;
1905 verify_tree (TREE_OPERAND (x
, 1), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1906 verify_tree (TREE_OPERAND (x
, 0), &tmp_list3
, &tmp_list3
, x
);
1907 /* Expressions inside the LHS are not ordered wrt. the sequence points
1908 in the RHS. Example:
1910 Despite the fact that the modification of "a" is in the before_sp
1911 list (tmp_before), it conflicts with the use of "a" in the LHS.
1912 We can handle this by adding the contents of tmp_list3
1913 to those of tmp_before, and redoing the collision warnings for that
1915 add_tlist (&tmp_before
, tmp_list3
, x
, 1);
1916 warn_for_collisions (tmp_before
);
1917 /* Exclude the LHS itself here; we first have to merge it into the
1918 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1919 didn't exclude the LHS, we'd get it twice, once as a read and once
1921 add_tlist (pno_sp
, tmp_list3
, x
, 0);
1922 warn_for_collisions_1 (TREE_OPERAND (x
, 0), x
, tmp_nosp
, 1);
1924 merge_tlist (pbefore_sp
, tmp_before
, 0);
1925 if (warning_candidate_p (TREE_OPERAND (x
, 0)))
1926 merge_tlist (&tmp_nosp
, new_tlist (NULL
, TREE_OPERAND (x
, 0), x
), 0);
1927 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 1);
1931 /* We need to warn about conflicts among arguments and conflicts between
1932 args and the function address. Side effects of the function address,
1933 however, are not ordered by the sequence point of the call. */
1935 call_expr_arg_iterator iter
;
1937 tmp_before
= tmp_nosp
= 0;
1938 verify_tree (CALL_EXPR_FN (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1939 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, x
)
1941 tmp_list2
= tmp_list3
= 0;
1942 verify_tree (arg
, &tmp_list2
, &tmp_list3
, NULL_TREE
);
1943 merge_tlist (&tmp_list3
, tmp_list2
, 0);
1944 add_tlist (&tmp_before
, tmp_list3
, NULL_TREE
, 0);
1946 add_tlist (&tmp_before
, tmp_nosp
, NULL_TREE
, 0);
1947 warn_for_collisions (tmp_before
);
1948 add_tlist (pbefore_sp
, tmp_before
, NULL_TREE
, 0);
1953 /* Scan all the list, e.g. indices of multi dimensional array. */
1956 tmp_before
= tmp_nosp
= 0;
1957 verify_tree (TREE_VALUE (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1958 merge_tlist (&tmp_nosp
, tmp_before
, 0);
1959 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
1966 struct tlist_cache
*t
;
1967 for (t
= save_expr_cache
; t
; t
= t
->next
)
1968 if (candidate_equal_p (t
->expr
, x
))
1973 t
= XOBNEW (&tlist_obstack
, struct tlist_cache
);
1974 t
->next
= save_expr_cache
;
1976 save_expr_cache
= t
;
1978 tmp_before
= tmp_nosp
= 0;
1979 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1980 warn_for_collisions (tmp_nosp
);
1983 merge_tlist (&tmp_list3
, tmp_nosp
, 0);
1984 t
->cache_before_sp
= tmp_before
;
1985 t
->cache_after_sp
= tmp_list3
;
1987 merge_tlist (pbefore_sp
, t
->cache_before_sp
, 1);
1988 add_tlist (pno_sp
, t
->cache_after_sp
, NULL_TREE
, 1);
1993 x
= TREE_OPERAND (x
, 0);
1999 case VIEW_CONVERT_EXPR
:
2000 if (location_wrapper_p (x
))
2002 x
= TREE_OPERAND (x
, 0);
2011 if (cxx_dialect
>= cxx17
)
2012 goto sequenced_binary
;
2017 /* For other expressions, simply recurse on their operands.
2018 Manual tail recursion for unary expressions.
2019 Other non-expressions need not be processed. */
2020 if (cl
== tcc_unary
)
2022 x
= TREE_OPERAND (x
, 0);
2026 else if (IS_EXPR_CODE_CLASS (cl
))
2029 int max
= TREE_OPERAND_LENGTH (x
);
2030 for (lp
= 0; lp
< max
; lp
++)
2032 tmp_before
= tmp_nosp
= 0;
2033 verify_tree (TREE_OPERAND (x
, lp
), &tmp_before
, &tmp_nosp
, 0);
2034 merge_tlist (&tmp_nosp
, tmp_before
, 0);
2035 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
2042 /* Try to warn for undefined behavior in EXPR due to missing sequence
2046 verify_sequence_points (tree expr
)
2048 struct tlist
*before_sp
= 0, *after_sp
= 0;
2051 save_expr_cache
= 0;
2052 if (tlist_firstobj
== 0)
2054 gcc_obstack_init (&tlist_obstack
);
2055 tlist_firstobj
= (char *) obstack_alloc (&tlist_obstack
, 0);
2058 verify_tree (expr
, &before_sp
, &after_sp
, 0);
2059 warn_for_collisions (after_sp
);
2060 obstack_free (&tlist_obstack
, tlist_firstobj
);
2063 /* Validate the expression after `case' and apply default promotions. */
2066 check_case_value (location_t loc
, tree value
)
2068 if (value
== NULL_TREE
)
2071 if (TREE_CODE (value
) == INTEGER_CST
)
2072 /* Promote char or short to int. */
2073 value
= perform_integral_promotions (value
);
2074 else if (value
!= error_mark_node
)
2076 error_at (loc
, "case label does not reduce to an integer constant");
2077 value
= error_mark_node
;
2080 constant_expression_warning (value
);
2085 /* Return an integer type with BITS bits of precision,
2086 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2089 c_common_type_for_size (unsigned int bits
, int unsignedp
)
2093 if (bits
== TYPE_PRECISION (integer_type_node
))
2094 return unsignedp
? unsigned_type_node
: integer_type_node
;
2096 if (bits
== TYPE_PRECISION (signed_char_type_node
))
2097 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2099 if (bits
== TYPE_PRECISION (short_integer_type_node
))
2100 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2102 if (bits
== TYPE_PRECISION (long_integer_type_node
))
2103 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2105 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
2106 return (unsignedp
? long_long_unsigned_type_node
2107 : long_long_integer_type_node
);
2109 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2110 if (int_n_enabled_p
[i
]
2111 && bits
== int_n_data
[i
].bitsize
)
2112 return (unsignedp
? int_n_trees
[i
].unsigned_type
2113 : int_n_trees
[i
].signed_type
);
2115 if (bits
== TYPE_PRECISION (widest_integer_literal_type_node
))
2116 return (unsignedp
? widest_unsigned_literal_type_node
2117 : widest_integer_literal_type_node
);
2119 if (bits
<= TYPE_PRECISION (intQI_type_node
))
2120 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2122 if (bits
<= TYPE_PRECISION (intHI_type_node
))
2123 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2125 if (bits
<= TYPE_PRECISION (intSI_type_node
))
2126 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2128 if (bits
<= TYPE_PRECISION (intDI_type_node
))
2129 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2134 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2135 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2136 and saturating if SATP is nonzero, otherwise not saturating. */
2139 c_common_fixed_point_type_for_size (unsigned int ibit
, unsigned int fbit
,
2140 int unsignedp
, int satp
)
2142 enum mode_class mclass
;
2144 mclass
= unsignedp
? MODE_UFRACT
: MODE_FRACT
;
2146 mclass
= unsignedp
? MODE_UACCUM
: MODE_ACCUM
;
2148 opt_scalar_mode opt_mode
;
2150 FOR_EACH_MODE_IN_CLASS (opt_mode
, mclass
)
2152 mode
= opt_mode
.require ();
2153 if (GET_MODE_IBIT (mode
) >= ibit
&& GET_MODE_FBIT (mode
) >= fbit
)
2157 if (!opt_mode
.exists (&mode
) || !targetm
.scalar_mode_supported_p (mode
))
2159 sorry ("GCC cannot support operators with integer types and "
2160 "fixed-point types that have too many integral and "
2161 "fractional bits together");
2165 return c_common_type_for_mode (mode
, satp
);
2168 /* Used for communication between c_common_type_for_mode and
2169 c_register_builtin_type. */
2170 tree registered_builtin_types
;
2172 /* Return a data type that has machine mode MODE.
2173 If the mode is an integer,
2174 then UNSIGNEDP selects between signed and unsigned types.
2175 If the mode is a fixed-point mode,
2176 then UNSIGNEDP selects between saturating and nonsaturating types. */
2179 c_common_type_for_mode (machine_mode mode
, int unsignedp
)
2184 if (mode
== TYPE_MODE (integer_type_node
))
2185 return unsignedp
? unsigned_type_node
: integer_type_node
;
2187 if (mode
== TYPE_MODE (signed_char_type_node
))
2188 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2190 if (mode
== TYPE_MODE (short_integer_type_node
))
2191 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2193 if (mode
== TYPE_MODE (long_integer_type_node
))
2194 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2196 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2197 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2199 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2200 if (int_n_enabled_p
[i
]
2201 && mode
== int_n_data
[i
].m
)
2202 return (unsignedp
? int_n_trees
[i
].unsigned_type
2203 : int_n_trees
[i
].signed_type
);
2206 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2209 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2212 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2215 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2217 #if HOST_BITS_PER_WIDE_INT >= 64
2218 if (mode
== TYPE_MODE (intTI_type_node
))
2219 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2222 if (mode
== TYPE_MODE (float_type_node
))
2223 return float_type_node
;
2225 if (mode
== TYPE_MODE (double_type_node
))
2226 return double_type_node
;
2228 if (mode
== TYPE_MODE (long_double_type_node
))
2229 return long_double_type_node
;
2231 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2232 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2233 && mode
== TYPE_MODE (FLOATN_NX_TYPE_NODE (i
)))
2234 return FLOATN_NX_TYPE_NODE (i
);
2236 if (mode
== TYPE_MODE (void_type_node
))
2237 return void_type_node
;
2239 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
))
2240 || mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2242 unsigned int precision
2243 = GET_MODE_PRECISION (as_a
<scalar_int_mode
> (mode
));
2245 ? make_unsigned_type (precision
)
2246 : make_signed_type (precision
));
2249 if (COMPLEX_MODE_P (mode
))
2251 machine_mode inner_mode
;
2254 if (mode
== TYPE_MODE (complex_float_type_node
))
2255 return complex_float_type_node
;
2256 if (mode
== TYPE_MODE (complex_double_type_node
))
2257 return complex_double_type_node
;
2258 if (mode
== TYPE_MODE (complex_long_double_type_node
))
2259 return complex_long_double_type_node
;
2261 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2262 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2263 && mode
== TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i
)))
2264 return COMPLEX_FLOATN_NX_TYPE_NODE (i
);
2266 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
2267 return complex_integer_type_node
;
2269 inner_mode
= GET_MODE_INNER (mode
);
2270 inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2271 if (inner_type
!= NULL_TREE
)
2272 return build_complex_type (inner_type
);
2274 else if (GET_MODE_CLASS (mode
) == MODE_VECTOR_BOOL
2275 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2277 unsigned int elem_bits
= vector_element_size (GET_MODE_BITSIZE (mode
),
2278 GET_MODE_NUNITS (mode
));
2279 tree bool_type
= build_nonstandard_boolean_type (elem_bits
);
2280 return build_vector_type_for_mode (bool_type
, mode
);
2282 else if (VECTOR_MODE_P (mode
)
2283 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2285 machine_mode inner_mode
= GET_MODE_INNER (mode
);
2286 tree inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2287 if (inner_type
!= NULL_TREE
)
2288 return build_vector_type_for_mode (inner_type
, mode
);
2291 if (dfloat32_type_node
!= NULL_TREE
2292 && mode
== TYPE_MODE (dfloat32_type_node
))
2293 return dfloat32_type_node
;
2294 if (dfloat64_type_node
!= NULL_TREE
2295 && mode
== TYPE_MODE (dfloat64_type_node
))
2296 return dfloat64_type_node
;
2297 if (dfloat128_type_node
!= NULL_TREE
2298 && mode
== TYPE_MODE (dfloat128_type_node
))
2299 return dfloat128_type_node
;
2301 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode
))
2303 if (mode
== TYPE_MODE (short_fract_type_node
))
2304 return unsignedp
? sat_short_fract_type_node
: short_fract_type_node
;
2305 if (mode
== TYPE_MODE (fract_type_node
))
2306 return unsignedp
? sat_fract_type_node
: fract_type_node
;
2307 if (mode
== TYPE_MODE (long_fract_type_node
))
2308 return unsignedp
? sat_long_fract_type_node
: long_fract_type_node
;
2309 if (mode
== TYPE_MODE (long_long_fract_type_node
))
2310 return unsignedp
? sat_long_long_fract_type_node
2311 : long_long_fract_type_node
;
2313 if (mode
== TYPE_MODE (unsigned_short_fract_type_node
))
2314 return unsignedp
? sat_unsigned_short_fract_type_node
2315 : unsigned_short_fract_type_node
;
2316 if (mode
== TYPE_MODE (unsigned_fract_type_node
))
2317 return unsignedp
? sat_unsigned_fract_type_node
2318 : unsigned_fract_type_node
;
2319 if (mode
== TYPE_MODE (unsigned_long_fract_type_node
))
2320 return unsignedp
? sat_unsigned_long_fract_type_node
2321 : unsigned_long_fract_type_node
;
2322 if (mode
== TYPE_MODE (unsigned_long_long_fract_type_node
))
2323 return unsignedp
? sat_unsigned_long_long_fract_type_node
2324 : unsigned_long_long_fract_type_node
;
2326 if (mode
== TYPE_MODE (short_accum_type_node
))
2327 return unsignedp
? sat_short_accum_type_node
: short_accum_type_node
;
2328 if (mode
== TYPE_MODE (accum_type_node
))
2329 return unsignedp
? sat_accum_type_node
: accum_type_node
;
2330 if (mode
== TYPE_MODE (long_accum_type_node
))
2331 return unsignedp
? sat_long_accum_type_node
: long_accum_type_node
;
2332 if (mode
== TYPE_MODE (long_long_accum_type_node
))
2333 return unsignedp
? sat_long_long_accum_type_node
2334 : long_long_accum_type_node
;
2336 if (mode
== TYPE_MODE (unsigned_short_accum_type_node
))
2337 return unsignedp
? sat_unsigned_short_accum_type_node
2338 : unsigned_short_accum_type_node
;
2339 if (mode
== TYPE_MODE (unsigned_accum_type_node
))
2340 return unsignedp
? sat_unsigned_accum_type_node
2341 : unsigned_accum_type_node
;
2342 if (mode
== TYPE_MODE (unsigned_long_accum_type_node
))
2343 return unsignedp
? sat_unsigned_long_accum_type_node
2344 : unsigned_long_accum_type_node
;
2345 if (mode
== TYPE_MODE (unsigned_long_long_accum_type_node
))
2346 return unsignedp
? sat_unsigned_long_long_accum_type_node
2347 : unsigned_long_long_accum_type_node
;
2350 return unsignedp
? sat_qq_type_node
: qq_type_node
;
2352 return unsignedp
? sat_hq_type_node
: hq_type_node
;
2354 return unsignedp
? sat_sq_type_node
: sq_type_node
;
2356 return unsignedp
? sat_dq_type_node
: dq_type_node
;
2358 return unsignedp
? sat_tq_type_node
: tq_type_node
;
2360 if (mode
== UQQmode
)
2361 return unsignedp
? sat_uqq_type_node
: uqq_type_node
;
2362 if (mode
== UHQmode
)
2363 return unsignedp
? sat_uhq_type_node
: uhq_type_node
;
2364 if (mode
== USQmode
)
2365 return unsignedp
? sat_usq_type_node
: usq_type_node
;
2366 if (mode
== UDQmode
)
2367 return unsignedp
? sat_udq_type_node
: udq_type_node
;
2368 if (mode
== UTQmode
)
2369 return unsignedp
? sat_utq_type_node
: utq_type_node
;
2372 return unsignedp
? sat_ha_type_node
: ha_type_node
;
2374 return unsignedp
? sat_sa_type_node
: sa_type_node
;
2376 return unsignedp
? sat_da_type_node
: da_type_node
;
2378 return unsignedp
? sat_ta_type_node
: ta_type_node
;
2380 if (mode
== UHAmode
)
2381 return unsignedp
? sat_uha_type_node
: uha_type_node
;
2382 if (mode
== USAmode
)
2383 return unsignedp
? sat_usa_type_node
: usa_type_node
;
2384 if (mode
== UDAmode
)
2385 return unsignedp
? sat_uda_type_node
: uda_type_node
;
2386 if (mode
== UTAmode
)
2387 return unsignedp
? sat_uta_type_node
: uta_type_node
;
2390 for (t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
2392 tree type
= TREE_VALUE (t
);
2393 if (TYPE_MODE (type
) == mode
2394 && VECTOR_TYPE_P (type
) == VECTOR_MODE_P (mode
)
2395 && !!unsignedp
== !!TYPE_UNSIGNED (type
))
2402 c_common_unsigned_type (tree type
)
2404 return c_common_signed_or_unsigned_type (1, type
);
2407 /* Return a signed type the same as TYPE in other respects. */
2410 c_common_signed_type (tree type
)
2412 return c_common_signed_or_unsigned_type (0, type
);
2415 /* Return a type the same as TYPE except unsigned or
2416 signed according to UNSIGNEDP. */
2419 c_common_signed_or_unsigned_type (int unsignedp
, tree type
)
2424 /* This block of code emulates the behavior of the old
2425 c_common_unsigned_type. In particular, it returns
2426 long_unsigned_type_node if passed a long, even when a int would
2427 have the same size. This is necessary for warnings to work
2428 correctly in archs where sizeof(int) == sizeof(long) */
2430 type1
= TYPE_MAIN_VARIANT (type
);
2431 if (type1
== signed_char_type_node
|| type1
== char_type_node
|| type1
== unsigned_char_type_node
)
2432 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2433 if (type1
== integer_type_node
|| type1
== unsigned_type_node
)
2434 return unsignedp
? unsigned_type_node
: integer_type_node
;
2435 if (type1
== short_integer_type_node
|| type1
== short_unsigned_type_node
)
2436 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2437 if (type1
== long_integer_type_node
|| type1
== long_unsigned_type_node
)
2438 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2439 if (type1
== long_long_integer_type_node
|| type1
== long_long_unsigned_type_node
)
2440 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2442 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2443 if (int_n_enabled_p
[i
]
2444 && (type1
== int_n_trees
[i
].unsigned_type
2445 || type1
== int_n_trees
[i
].signed_type
))
2446 return (unsignedp
? int_n_trees
[i
].unsigned_type
2447 : int_n_trees
[i
].signed_type
);
2449 #if HOST_BITS_PER_WIDE_INT >= 64
2450 if (type1
== intTI_type_node
|| type1
== unsigned_intTI_type_node
)
2451 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2453 if (type1
== intDI_type_node
|| type1
== unsigned_intDI_type_node
)
2454 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2455 if (type1
== intSI_type_node
|| type1
== unsigned_intSI_type_node
)
2456 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2457 if (type1
== intHI_type_node
|| type1
== unsigned_intHI_type_node
)
2458 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2459 if (type1
== intQI_type_node
|| type1
== unsigned_intQI_type_node
)
2460 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2462 #define C_COMMON_FIXED_TYPES(NAME) \
2463 if (type1 == short_ ## NAME ## _type_node \
2464 || type1 == unsigned_short_ ## NAME ## _type_node) \
2465 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2466 : short_ ## NAME ## _type_node; \
2467 if (type1 == NAME ## _type_node \
2468 || type1 == unsigned_ ## NAME ## _type_node) \
2469 return unsignedp ? unsigned_ ## NAME ## _type_node \
2470 : NAME ## _type_node; \
2471 if (type1 == long_ ## NAME ## _type_node \
2472 || type1 == unsigned_long_ ## NAME ## _type_node) \
2473 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2474 : long_ ## NAME ## _type_node; \
2475 if (type1 == long_long_ ## NAME ## _type_node \
2476 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2477 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2478 : long_long_ ## NAME ## _type_node;
2480 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2481 if (type1 == NAME ## _type_node \
2482 || type1 == u ## NAME ## _type_node) \
2483 return unsignedp ? u ## NAME ## _type_node \
2484 : NAME ## _type_node;
2486 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2487 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2488 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2489 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2490 : sat_ ## short_ ## NAME ## _type_node; \
2491 if (type1 == sat_ ## NAME ## _type_node \
2492 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2493 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2494 : sat_ ## NAME ## _type_node; \
2495 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2496 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2497 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2498 : sat_ ## long_ ## NAME ## _type_node; \
2499 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2500 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2501 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2502 : sat_ ## long_long_ ## NAME ## _type_node;
2504 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2505 if (type1 == sat_ ## NAME ## _type_node \
2506 || type1 == sat_ ## u ## NAME ## _type_node) \
2507 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2508 : sat_ ## NAME ## _type_node;
2510 C_COMMON_FIXED_TYPES (fract
);
2511 C_COMMON_FIXED_TYPES_SAT (fract
);
2512 C_COMMON_FIXED_TYPES (accum
);
2513 C_COMMON_FIXED_TYPES_SAT (accum
);
2515 C_COMMON_FIXED_MODE_TYPES (qq
);
2516 C_COMMON_FIXED_MODE_TYPES (hq
);
2517 C_COMMON_FIXED_MODE_TYPES (sq
);
2518 C_COMMON_FIXED_MODE_TYPES (dq
);
2519 C_COMMON_FIXED_MODE_TYPES (tq
);
2520 C_COMMON_FIXED_MODE_TYPES_SAT (qq
);
2521 C_COMMON_FIXED_MODE_TYPES_SAT (hq
);
2522 C_COMMON_FIXED_MODE_TYPES_SAT (sq
);
2523 C_COMMON_FIXED_MODE_TYPES_SAT (dq
);
2524 C_COMMON_FIXED_MODE_TYPES_SAT (tq
);
2525 C_COMMON_FIXED_MODE_TYPES (ha
);
2526 C_COMMON_FIXED_MODE_TYPES (sa
);
2527 C_COMMON_FIXED_MODE_TYPES (da
);
2528 C_COMMON_FIXED_MODE_TYPES (ta
);
2529 C_COMMON_FIXED_MODE_TYPES_SAT (ha
);
2530 C_COMMON_FIXED_MODE_TYPES_SAT (sa
);
2531 C_COMMON_FIXED_MODE_TYPES_SAT (da
);
2532 C_COMMON_FIXED_MODE_TYPES_SAT (ta
);
2534 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2535 the precision; they have precision set to match their range, but
2536 may use a wider mode to match an ABI. If we change modes, we may
2537 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2538 the precision as well, so as to yield correct results for
2539 bit-field types. C++ does not have these separate bit-field
2540 types, and producing a signed or unsigned variant of an
2541 ENUMERAL_TYPE may cause other problems as well. */
2543 if (!INTEGRAL_TYPE_P (type
)
2544 || TYPE_UNSIGNED (type
) == unsignedp
)
2547 #define TYPE_OK(node) \
2548 (TYPE_MODE (type) == TYPE_MODE (node) \
2549 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2550 if (TYPE_OK (signed_char_type_node
))
2551 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2552 if (TYPE_OK (integer_type_node
))
2553 return unsignedp
? unsigned_type_node
: integer_type_node
;
2554 if (TYPE_OK (short_integer_type_node
))
2555 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2556 if (TYPE_OK (long_integer_type_node
))
2557 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2558 if (TYPE_OK (long_long_integer_type_node
))
2559 return (unsignedp
? long_long_unsigned_type_node
2560 : long_long_integer_type_node
);
2562 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2563 if (int_n_enabled_p
[i
]
2564 && TYPE_MODE (type
) == int_n_data
[i
].m
2565 && TYPE_PRECISION (type
) == int_n_data
[i
].bitsize
)
2566 return (unsignedp
? int_n_trees
[i
].unsigned_type
2567 : int_n_trees
[i
].signed_type
);
2569 #if HOST_BITS_PER_WIDE_INT >= 64
2570 if (TYPE_OK (intTI_type_node
))
2571 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2573 if (TYPE_OK (intDI_type_node
))
2574 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2575 if (TYPE_OK (intSI_type_node
))
2576 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2577 if (TYPE_OK (intHI_type_node
))
2578 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2579 if (TYPE_OK (intQI_type_node
))
2580 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2583 return build_nonstandard_integer_type (TYPE_PRECISION (type
), unsignedp
);
2586 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2589 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width
, int unsignedp
)
2593 /* Extended integer types of the same width as a standard type have
2594 lesser rank, so those of the same width as int promote to int or
2595 unsigned int and are valid for printf formats expecting int or
2596 unsigned int. To avoid such special cases, avoid creating
2597 extended integer types for bit-fields if a standard integer type
2599 if (width
== TYPE_PRECISION (integer_type_node
))
2600 return unsignedp
? unsigned_type_node
: integer_type_node
;
2601 if (width
== TYPE_PRECISION (signed_char_type_node
))
2602 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2603 if (width
== TYPE_PRECISION (short_integer_type_node
))
2604 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2605 if (width
== TYPE_PRECISION (long_integer_type_node
))
2606 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2607 if (width
== TYPE_PRECISION (long_long_integer_type_node
))
2608 return (unsignedp
? long_long_unsigned_type_node
2609 : long_long_integer_type_node
);
2610 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2611 if (int_n_enabled_p
[i
]
2612 && width
== int_n_data
[i
].bitsize
)
2613 return (unsignedp
? int_n_trees
[i
].unsigned_type
2614 : int_n_trees
[i
].signed_type
);
2615 return build_nonstandard_integer_type (width
, unsignedp
);
2618 /* The C version of the register_builtin_type langhook. */
2621 c_register_builtin_type (tree type
, const char* name
)
2625 decl
= build_decl (UNKNOWN_LOCATION
,
2626 TYPE_DECL
, get_identifier (name
), type
);
2627 DECL_ARTIFICIAL (decl
) = 1;
2628 if (!TYPE_NAME (type
))
2629 TYPE_NAME (type
) = decl
;
2630 lang_hooks
.decls
.pushdecl (decl
);
2632 registered_builtin_types
= tree_cons (0, type
, registered_builtin_types
);
2635 /* Print an error message for invalid operands to arith operation
2636 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2637 RICHLOC is a rich location for the message, containing either
2638 three separate locations for each of the operator and operands
2643 (C FE), or one location ranging over all over them
2651 binary_op_error (rich_location
*richloc
, enum tree_code code
,
2652 tree type0
, tree type1
)
2659 opname
= "+"; break;
2661 opname
= "-"; break;
2663 opname
= "*"; break;
2665 opname
= "max"; break;
2667 opname
= "min"; break;
2669 opname
= "=="; break;
2671 opname
= "!="; break;
2673 opname
= "<="; break;
2675 opname
= ">="; break;
2677 opname
= "<"; break;
2679 opname
= ">"; break;
2681 opname
= "<<"; break;
2683 opname
= ">>"; break;
2684 case TRUNC_MOD_EXPR
:
2685 case FLOOR_MOD_EXPR
:
2686 opname
= "%"; break;
2687 case TRUNC_DIV_EXPR
:
2688 case FLOOR_DIV_EXPR
:
2689 opname
= "/"; break;
2691 opname
= "&"; break;
2693 opname
= "|"; break;
2694 case TRUTH_ANDIF_EXPR
:
2695 opname
= "&&"; break;
2696 case TRUTH_ORIF_EXPR
:
2697 opname
= "||"; break;
2699 opname
= "^"; break;
2704 "invalid operands to binary %s (have %qT and %qT)",
2705 opname
, type0
, type1
);
2708 /* Given an expression as a tree, return its original type. Do this
2709 by stripping any conversion that preserves the sign and precision. */
2711 expr_original_type (tree expr
)
2713 STRIP_SIGN_NOPS (expr
);
2714 return TREE_TYPE (expr
);
2717 /* Subroutine of build_binary_op, used for comparison operations.
2718 See if the operands have both been converted from subword integer types
2719 and, if so, perhaps change them both back to their original type.
2720 This function is also responsible for converting the two operands
2721 to the proper common type for comparison.
2723 The arguments of this function are all pointers to local variables
2724 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2725 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2727 LOC is the location of the comparison.
2729 If this function returns non-NULL_TREE, it means that the comparison has
2730 a constant value. What this function returns is an expression for
2734 shorten_compare (location_t loc
, tree
*op0_ptr
, tree
*op1_ptr
,
2735 tree
*restype_ptr
, enum tree_code
*rescode_ptr
)
2738 tree op0
= *op0_ptr
;
2739 tree op1
= *op1_ptr
;
2740 int unsignedp0
, unsignedp1
;
2742 tree primop0
, primop1
;
2743 enum tree_code code
= *rescode_ptr
;
2745 /* Throw away any conversions to wider types
2746 already present in the operands. */
2748 primop0
= c_common_get_narrower (op0
, &unsignedp0
);
2749 primop1
= c_common_get_narrower (op1
, &unsignedp1
);
2751 /* If primopN is first sign-extended from primopN's precision to opN's
2752 precision, then zero-extended from opN's precision to
2753 *restype_ptr precision, shortenings might be invalid. */
2754 if (TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (TREE_TYPE (op0
))
2755 && TYPE_PRECISION (TREE_TYPE (op0
)) < TYPE_PRECISION (*restype_ptr
)
2757 && TYPE_UNSIGNED (TREE_TYPE (op0
)))
2759 if (TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (TREE_TYPE (op1
))
2760 && TYPE_PRECISION (TREE_TYPE (op1
)) < TYPE_PRECISION (*restype_ptr
)
2762 && TYPE_UNSIGNED (TREE_TYPE (op1
)))
2765 /* Handle the case that OP0 does not *contain* a conversion
2766 but it *requires* conversion to FINAL_TYPE. */
2768 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2769 unsignedp0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2770 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2771 unsignedp1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2773 /* If one of the operands must be floated, we cannot optimize. */
2774 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2775 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2777 /* If first arg is constant, swap the args (changing operation
2778 so value is preserved), for canonicalization. Don't do this if
2779 the second arg is 0. */
2781 if (TREE_CONSTANT (primop0
)
2782 && !integer_zerop (primop1
) && !real_zerop (primop1
)
2783 && !fixed_zerop (primop1
))
2785 std::swap (primop0
, primop1
);
2786 std::swap (op0
, op1
);
2789 std::swap (unsignedp0
, unsignedp1
);
2790 std::swap (real1
, real2
);
2809 *rescode_ptr
= code
;
2812 /* If comparing an integer against a constant more bits wide,
2813 maybe we can deduce a value of 1 or 0 independent of the data.
2814 Or else truncate the constant now
2815 rather than extend the variable at run time.
2817 This is only interesting if the constant is the wider arg.
2818 Also, it is not safe if the constant is unsigned and the
2819 variable arg is signed, since in this case the variable
2820 would be sign-extended and then regarded as unsigned.
2821 Our technique fails in this case because the lowest/highest
2822 possible unsigned results don't follow naturally from the
2823 lowest/highest possible values of the variable operand.
2824 For just EQ_EXPR and NE_EXPR there is another technique that
2825 could be used: see if the constant can be faithfully represented
2826 in the other operand's type, by truncating it and reextending it
2827 and see if that preserves the constant's value. */
2829 if (!real1
&& !real2
2830 && TREE_CODE (TREE_TYPE (primop0
)) != FIXED_POINT_TYPE
2831 && TREE_CODE (primop1
) == INTEGER_CST
2832 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2834 int min_gt
, max_gt
, min_lt
, max_lt
;
2835 tree maxval
, minval
;
2836 /* 1 if comparison is nominally unsigned. */
2837 int unsignedp
= TYPE_UNSIGNED (*restype_ptr
);
2840 type
= c_common_signed_or_unsigned_type (unsignedp0
,
2841 TREE_TYPE (primop0
));
2843 maxval
= TYPE_MAX_VALUE (type
);
2844 minval
= TYPE_MIN_VALUE (type
);
2846 if (unsignedp
&& !unsignedp0
)
2847 *restype_ptr
= c_common_signed_type (*restype_ptr
);
2849 if (TREE_TYPE (primop1
) != *restype_ptr
)
2851 /* Convert primop1 to target type, but do not introduce
2852 additional overflow. We know primop1 is an int_cst. */
2853 primop1
= force_fit_type (*restype_ptr
,
2856 TYPE_PRECISION (*restype_ptr
)),
2857 0, TREE_OVERFLOW (primop1
));
2859 if (type
!= *restype_ptr
)
2861 minval
= convert (*restype_ptr
, minval
);
2862 maxval
= convert (*restype_ptr
, maxval
);
2865 min_gt
= tree_int_cst_lt (primop1
, minval
);
2866 max_gt
= tree_int_cst_lt (primop1
, maxval
);
2867 min_lt
= tree_int_cst_lt (minval
, primop1
);
2868 max_lt
= tree_int_cst_lt (maxval
, primop1
);
2871 /* This used to be a switch, but Genix compiler can't handle that. */
2872 if (code
== NE_EXPR
)
2874 if (max_lt
|| min_gt
)
2875 val
= truthvalue_true_node
;
2877 else if (code
== EQ_EXPR
)
2879 if (max_lt
|| min_gt
)
2880 val
= truthvalue_false_node
;
2882 else if (code
== LT_EXPR
)
2885 val
= truthvalue_true_node
;
2887 val
= truthvalue_false_node
;
2889 else if (code
== GT_EXPR
)
2892 val
= truthvalue_true_node
;
2894 val
= truthvalue_false_node
;
2896 else if (code
== LE_EXPR
)
2899 val
= truthvalue_true_node
;
2901 val
= truthvalue_false_node
;
2903 else if (code
== GE_EXPR
)
2906 val
= truthvalue_true_node
;
2908 val
= truthvalue_false_node
;
2911 /* If primop0 was sign-extended and unsigned comparison specd,
2912 we did a signed comparison above using the signed type bounds.
2913 But the comparison we output must be unsigned.
2915 Also, for inequalities, VAL is no good; but if the signed
2916 comparison had *any* fixed result, it follows that the
2917 unsigned comparison just tests the sign in reverse
2918 (positive values are LE, negative ones GE).
2919 So we can generate an unsigned comparison
2920 against an extreme value of the signed type. */
2922 if (unsignedp
&& !unsignedp0
)
2929 primop1
= TYPE_MIN_VALUE (type
);
2935 primop1
= TYPE_MAX_VALUE (type
);
2942 type
= c_common_unsigned_type (type
);
2945 if (TREE_CODE (primop0
) != INTEGER_CST
2946 /* Don't warn if it's from a (non-system) macro. */
2947 && !(from_macro_expansion_at
2948 (expansion_point_location_if_in_system_header
2949 (EXPR_LOCATION (primop0
)))))
2951 if (val
== truthvalue_false_node
)
2952 warning_at (loc
, OPT_Wtype_limits
,
2953 "comparison is always false due to limited range of data type");
2954 if (val
== truthvalue_true_node
)
2955 warning_at (loc
, OPT_Wtype_limits
,
2956 "comparison is always true due to limited range of data type");
2961 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2962 if (TREE_SIDE_EFFECTS (primop0
))
2963 return build2 (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2967 /* Value is not predetermined, but do the comparison
2968 in the type of the operand that is not constant.
2969 TYPE is already properly set. */
2972 /* If either arg is decimal float and the other is float, find the
2973 proper common type to use for comparison. */
2974 else if (real1
&& real2
2975 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
2976 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
))))
2977 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2979 /* If either arg is decimal float and the other is float, fail. */
2980 else if (real1
&& real2
2981 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
2982 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
)))))
2985 else if (real1
&& real2
2986 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2987 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2988 type
= TREE_TYPE (primop0
);
2990 /* If args' natural types are both narrower than nominal type
2991 and both extend in the same manner, compare them
2992 in the type of the wider arg.
2993 Otherwise must actually extend both to the nominal
2994 common type lest different ways of extending
2996 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2998 else if (unsignedp0
== unsignedp1
&& real1
== real2
2999 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
3000 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
3002 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
3003 type
= c_common_signed_or_unsigned_type (unsignedp0
3004 || TYPE_UNSIGNED (*restype_ptr
),
3006 /* Make sure shorter operand is extended the right way
3007 to match the longer operand. */
3009 = convert (c_common_signed_or_unsigned_type (unsignedp0
,
3010 TREE_TYPE (primop0
)),
3013 = convert (c_common_signed_or_unsigned_type (unsignedp1
,
3014 TREE_TYPE (primop1
)),
3019 /* Here we must do the comparison on the nominal type
3020 using the args exactly as we received them. */
3021 type
= *restype_ptr
;
3025 /* We want to fold unsigned comparisons of >= and < against zero.
3026 For these, we may also issue a warning if we have a non-constant
3027 compared against zero, where the zero was spelled as "0" (rather
3028 than merely folding to it).
3029 If we have at least one constant, then op1 is constant
3030 and we may have a non-constant expression as op0. */
3031 if (!real1
&& !real2
&& integer_zerop (primop1
)
3032 && TYPE_UNSIGNED (*restype_ptr
))
3034 tree value
= NULL_TREE
;
3035 /* All unsigned values are >= 0, so we warn. However,
3036 if OP0 is a constant that is >= 0, the signedness of
3037 the comparison isn't an issue, so suppress the
3039 tree folded_op0
= fold_for_warn (op0
);
3041 warn_type_limits
&& !in_system_header_at (loc
)
3042 && !(TREE_CODE (folded_op0
) == INTEGER_CST
3043 && !TREE_OVERFLOW (convert (c_common_signed_type (type
),
3045 /* Do not warn for enumeration types. */
3046 && (TREE_CODE (expr_original_type (folded_op0
)) != ENUMERAL_TYPE
);
3052 warning_at (loc
, OPT_Wtype_limits
,
3053 "comparison of unsigned expression in %<>= 0%> "
3055 value
= truthvalue_true_node
;
3060 warning_at (loc
, OPT_Wtype_limits
,
3061 "comparison of unsigned expression in %<< 0%> "
3063 value
= truthvalue_false_node
;
3070 if (value
!= NULL_TREE
)
3072 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3073 if (TREE_SIDE_EFFECTS (primop0
))
3074 return build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3081 *op0_ptr
= convert (type
, primop0
);
3082 *op1_ptr
= convert (type
, primop1
);
3084 *restype_ptr
= truthvalue_type_node
;
3089 /* Return a tree for the sum or difference (RESULTCODE says which)
3090 of pointer PTROP and integer INTOP. */
3093 pointer_int_sum (location_t loc
, enum tree_code resultcode
,
3094 tree ptrop
, tree intop
, bool complain
)
3098 /* The result is a pointer of the same type that is being added. */
3099 tree result_type
= TREE_TYPE (ptrop
);
3101 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3103 if (complain
&& warn_pointer_arith
)
3104 pedwarn (loc
, OPT_Wpointer_arith
,
3105 "pointer of type %<void *%> used in arithmetic");
3107 return error_mark_node
;
3108 size_exp
= integer_one_node
;
3110 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3112 if (complain
&& warn_pointer_arith
)
3113 pedwarn (loc
, OPT_Wpointer_arith
,
3114 "pointer to a function used in arithmetic");
3116 return error_mark_node
;
3117 size_exp
= integer_one_node
;
3119 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
3120 TREE_TYPE (result_type
)))
3121 size_exp
= integer_one_node
;
3123 size_exp
= size_in_bytes_loc (loc
, TREE_TYPE (result_type
));
3125 /* We are manipulating pointer values, so we don't need to warn
3126 about relying on undefined signed overflow. We disable the
3127 warning here because we use integer types so fold won't know that
3128 they are really pointers. */
3129 fold_defer_overflow_warnings ();
3131 /* If what we are about to multiply by the size of the elements
3132 contains a constant term, apply distributive law
3133 and multiply that constant term separately.
3134 This helps produce common subexpressions. */
3135 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3136 && !TREE_CONSTANT (intop
)
3137 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3138 && TREE_CONSTANT (size_exp
)
3139 /* If the constant comes from pointer subtraction,
3140 skip this optimization--it would cause an error. */
3141 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
3142 /* If the constant is unsigned, and smaller than the pointer size,
3143 then we must skip this optimization. This is because it could cause
3144 an overflow error if the constant is negative but INTOP is not. */
3145 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (intop
))
3146 || (TYPE_PRECISION (TREE_TYPE (intop
))
3147 == TYPE_PRECISION (TREE_TYPE (ptrop
)))))
3149 enum tree_code subcode
= resultcode
;
3150 tree int_type
= TREE_TYPE (intop
);
3151 if (TREE_CODE (intop
) == MINUS_EXPR
)
3152 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3153 /* Convert both subexpression types to the type of intop,
3154 because weird cases involving pointer arithmetic
3155 can result in a sum or difference with different type args. */
3156 ptrop
= build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop
, 1)),
3158 convert (int_type
, TREE_OPERAND (intop
, 1)),
3160 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
3163 /* Convert the integer argument to a type the same size as sizetype
3164 so the multiply won't overflow spuriously. */
3165 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
)
3166 || TYPE_UNSIGNED (TREE_TYPE (intop
)) != TYPE_UNSIGNED (sizetype
))
3167 intop
= convert (c_common_type_for_size (TYPE_PRECISION (sizetype
),
3168 TYPE_UNSIGNED (sizetype
)), intop
);
3170 /* Replace the integer argument with a suitable product by the object size.
3171 Do this multiplication as signed, then convert to the appropriate type
3172 for the pointer operation and disregard an overflow that occurred only
3173 because of the sign-extension change in the latter conversion. */
3175 tree t
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (intop
), intop
,
3176 convert (TREE_TYPE (intop
), size_exp
));
3177 intop
= convert (sizetype
, t
);
3178 if (TREE_OVERFLOW_P (intop
) && !TREE_OVERFLOW (t
))
3179 intop
= wide_int_to_tree (TREE_TYPE (intop
), wi::to_wide (intop
));
3182 /* Create the sum or difference. */
3183 if (resultcode
== MINUS_EXPR
)
3184 intop
= fold_build1_loc (loc
, NEGATE_EXPR
, sizetype
, intop
);
3186 ret
= fold_build_pointer_plus_loc (loc
, ptrop
, intop
);
3188 fold_undefer_and_ignore_overflow_warnings ();
3193 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3194 and if NON_CONST is known not to be permitted in an evaluated part
3195 of a constant expression. */
3198 c_wrap_maybe_const (tree expr
, bool non_const
)
3200 bool nowarning
= TREE_NO_WARNING (expr
);
3201 location_t loc
= EXPR_LOCATION (expr
);
3203 /* This should never be called for C++. */
3204 if (c_dialect_cxx ())
3207 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3208 STRIP_TYPE_NOPS (expr
);
3209 expr
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL
, expr
);
3210 C_MAYBE_CONST_EXPR_NON_CONST (expr
) = non_const
;
3212 TREE_NO_WARNING (expr
) = 1;
3213 protected_set_expr_location (expr
, loc
);
3218 /* Return whether EXPR is a declaration whose address can never be
3222 decl_with_nonnull_addr_p (const_tree expr
)
3224 return (DECL_P (expr
)
3225 && (TREE_CODE (expr
) == PARM_DECL
3226 || TREE_CODE (expr
) == LABEL_DECL
3227 || !DECL_WEAK (expr
)));
3230 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3231 or for an `if' or `while' statement or ?..: exp. It should already
3232 have been validated to be of suitable type; otherwise, a bad
3233 diagnostic may result.
3235 The EXPR is located at LOCATION.
3237 This preparation consists of taking the ordinary
3238 representation of an expression expr and producing a valid tree
3239 boolean expression describing whether expr is nonzero. We could
3240 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3241 but we optimize comparisons, &&, ||, and !.
3243 The resulting type should always be `truthvalue_type_node'. */
3246 c_common_truthvalue_conversion (location_t location
, tree expr
)
3248 STRIP_ANY_LOCATION_WRAPPER (expr
);
3249 switch (TREE_CODE (expr
))
3251 case EQ_EXPR
: case NE_EXPR
: case UNEQ_EXPR
: case LTGT_EXPR
:
3252 case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
3253 case UNLE_EXPR
: case UNGE_EXPR
: case UNLT_EXPR
: case UNGT_EXPR
:
3254 case ORDERED_EXPR
: case UNORDERED_EXPR
:
3255 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3257 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3258 TREE_OPERAND (expr
, 0), TREE_OPERAND (expr
, 1));
3261 case TRUTH_ANDIF_EXPR
:
3262 case TRUTH_ORIF_EXPR
:
3263 case TRUTH_AND_EXPR
:
3265 case TRUTH_XOR_EXPR
:
3266 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3268 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3269 c_common_truthvalue_conversion (location
,
3270 TREE_OPERAND (expr
, 0)),
3271 c_common_truthvalue_conversion (location
,
3272 TREE_OPERAND (expr
, 1)));
3275 case TRUTH_NOT_EXPR
:
3276 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3278 expr
= build1 (TREE_CODE (expr
), truthvalue_type_node
,
3279 c_common_truthvalue_conversion (location
,
3280 TREE_OPERAND (expr
, 0)));
3287 if (TREE_CODE (TREE_TYPE (expr
)) == ENUMERAL_TYPE
3288 && !integer_zerop (expr
)
3289 && !integer_onep (expr
))
3290 warning_at (location
, OPT_Wint_in_bool_context
,
3291 "enum constant in boolean context");
3292 return integer_zerop (expr
) ? truthvalue_false_node
3293 : truthvalue_true_node
;
3296 return real_compare (NE_EXPR
, &TREE_REAL_CST (expr
), &dconst0
)
3297 ? truthvalue_true_node
3298 : truthvalue_false_node
;
3301 return fixed_compare (NE_EXPR
, &TREE_FIXED_CST (expr
),
3302 &FCONST0 (TYPE_MODE (TREE_TYPE (expr
))))
3303 ? truthvalue_true_node
3304 : truthvalue_false_node
;
3307 expr
= build_unary_op (location
, ADDR_EXPR
, expr
, false);
3312 tree inner
= TREE_OPERAND (expr
, 0);
3313 if (decl_with_nonnull_addr_p (inner
))
3315 /* Common Ada programmer's mistake. */
3316 warning_at (location
,
3318 "the address of %qD will always evaluate as %<true%>",
3320 return truthvalue_true_node
;
3326 expr
= build_binary_op (EXPR_LOCATION (expr
),
3327 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
3328 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3329 c_common_truthvalue_conversion (location
,
3330 TREE_OPERAND (expr
, 0)),
3331 c_common_truthvalue_conversion (location
,
3332 TREE_OPERAND (expr
, 1)),
3340 case EXCESS_PRECISION_EXPR
:
3341 /* These don't change whether an object is nonzero or zero. */
3342 return c_common_truthvalue_conversion (location
, TREE_OPERAND (expr
, 0));
3346 /* These don't change whether an object is zero or nonzero, but
3347 we can't ignore them if their second arg has side-effects. */
3348 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
3350 expr
= build2 (COMPOUND_EXPR
, truthvalue_type_node
,
3351 TREE_OPERAND (expr
, 1),
3352 c_common_truthvalue_conversion
3353 (location
, TREE_OPERAND (expr
, 0)));
3357 return c_common_truthvalue_conversion (location
,
3358 TREE_OPERAND (expr
, 0));
3361 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3362 "%<*%> in boolean context, suggest %<&&%> instead");
3366 /* We will only warn on signed shifts here, because the majority of
3367 false positive warnings happen in code where unsigned arithmetic
3368 was used in anticipation of a possible overflow.
3369 Furthermore, if we see an unsigned type here we know that the
3370 result of the shift is not subject to integer promotion rules. */
3371 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
3372 && !TYPE_UNSIGNED (TREE_TYPE (expr
)))
3373 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3374 "%<<<%> in boolean context, did you mean %<<%>?");
3378 if (warn_int_in_bool_context
3379 && !from_macro_definition_at (EXPR_LOCATION (expr
)))
3381 tree val1
= fold_for_warn (TREE_OPERAND (expr
, 1));
3382 tree val2
= fold_for_warn (TREE_OPERAND (expr
, 2));
3383 if (TREE_CODE (val1
) == INTEGER_CST
3384 && TREE_CODE (val2
) == INTEGER_CST
3385 && !integer_zerop (val1
)
3386 && !integer_zerop (val2
)
3387 && (!integer_onep (val1
)
3388 || !integer_onep (val2
)))
3389 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3390 "%<?:%> using integer constants in boolean context, "
3391 "the expression will always evaluate to %<true%>");
3392 else if ((TREE_CODE (val1
) == INTEGER_CST
3393 && !integer_zerop (val1
)
3394 && !integer_onep (val1
))
3395 || (TREE_CODE (val2
) == INTEGER_CST
3396 && !integer_zerop (val2
)
3397 && !integer_onep (val2
)))
3398 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3399 "%<?:%> using integer constants in boolean context");
3401 /* Distribute the conversion into the arms of a COND_EXPR. */
3402 if (c_dialect_cxx ())
3403 /* Avoid premature folding. */
3407 int w
= warn_int_in_bool_context
;
3408 warn_int_in_bool_context
= 0;
3409 /* Folding will happen later for C. */
3410 expr
= build3 (COND_EXPR
, truthvalue_type_node
,
3411 TREE_OPERAND (expr
, 0),
3412 c_common_truthvalue_conversion (location
,
3413 TREE_OPERAND (expr
, 1)),
3414 c_common_truthvalue_conversion (location
,
3415 TREE_OPERAND (expr
, 2)));
3416 warn_int_in_bool_context
= w
;
3422 tree totype
= TREE_TYPE (expr
);
3423 tree fromtype
= TREE_TYPE (TREE_OPERAND (expr
, 0));
3425 if (POINTER_TYPE_P (totype
)
3426 && !c_inhibit_evaluation_warnings
3427 && TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3433 warning_at (location
,
3435 "the compiler can assume that the address of "
3436 "%qD will always evaluate to %<true%>",
3440 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3441 since that affects how `default_conversion' will behave. */
3442 if (TREE_CODE (totype
) == REFERENCE_TYPE
3443 || TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3445 /* Don't strip a conversion from C++0x scoped enum, since they
3446 don't implicitly convert to other types. */
3447 if (TREE_CODE (fromtype
) == ENUMERAL_TYPE
3448 && ENUM_IS_SCOPED (fromtype
))
3450 /* If this isn't narrowing the argument, we can ignore it. */
3451 if (TYPE_PRECISION (totype
) >= TYPE_PRECISION (fromtype
))
3452 return c_common_truthvalue_conversion (location
,
3453 TREE_OPERAND (expr
, 0));
3458 if (!TREE_NO_WARNING (expr
)
3460 && warning_at (location
, OPT_Wparentheses
,
3461 "suggest parentheses around assignment used as "
3463 TREE_NO_WARNING (expr
) = 1;
3468 tree folded_expr
= fold_for_warn (expr
);
3469 if (folded_expr
!= expr
)
3470 return c_common_truthvalue_conversion (location
, folded_expr
);
3478 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
3480 tree t
= save_expr (expr
);
3481 expr
= (build_binary_op
3482 (EXPR_LOCATION (expr
),
3483 (TREE_SIDE_EFFECTS (expr
)
3484 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3485 c_common_truthvalue_conversion
3487 build_unary_op (location
, REALPART_EXPR
, t
, false)),
3488 c_common_truthvalue_conversion
3490 build_unary_op (location
, IMAGPART_EXPR
, t
, false)),
3495 if (TREE_CODE (TREE_TYPE (expr
)) == FIXED_POINT_TYPE
)
3497 tree fixed_zero_node
= build_fixed (TREE_TYPE (expr
),
3499 (TREE_TYPE (expr
))));
3500 return build_binary_op (location
, NE_EXPR
, expr
, fixed_zero_node
, true);
3503 return build_binary_op (location
, NE_EXPR
, expr
, integer_zero_node
, true);
3506 protected_set_expr_location (expr
, location
);
3510 static void def_builtin_1 (enum built_in_function fncode
,
3512 enum built_in_class fnclass
,
3513 tree fntype
, tree libtype
,
3514 bool both_p
, bool fallback_p
, bool nonansi_p
,
3515 tree fnattrs
, bool implicit_p
);
3518 /* Apply the TYPE_QUALS to the new DECL. */
3521 c_apply_type_quals_to_decl (int type_quals
, tree decl
)
3523 tree type
= TREE_TYPE (decl
);
3525 if (type
== error_mark_node
)
3528 if ((type_quals
& TYPE_QUAL_CONST
)
3529 || (type
&& TREE_CODE (type
) == REFERENCE_TYPE
))
3530 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3531 constructor can produce constant init, so rely on cp_finish_decl to
3532 clear TREE_READONLY if the variable has non-constant init. */
3533 TREE_READONLY (decl
) = 1;
3534 if (type_quals
& TYPE_QUAL_VOLATILE
)
3536 TREE_SIDE_EFFECTS (decl
) = 1;
3537 TREE_THIS_VOLATILE (decl
) = 1;
3539 if (type_quals
& TYPE_QUAL_RESTRICT
)
3541 while (type
&& TREE_CODE (type
) == ARRAY_TYPE
)
3542 /* Allow 'restrict' on arrays of pointers.
3543 FIXME currently we just ignore it. */
3544 type
= TREE_TYPE (type
);
3546 || !POINTER_TYPE_P (type
)
3547 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
)))
3548 error ("invalid use of %<restrict%>");
3552 /* Return the typed-based alias set for T, which may be an expression
3553 or a type. Return -1 if we don't do anything special. */
3556 c_common_get_alias_set (tree t
)
3558 /* For VLAs, use the alias set of the element type rather than the
3559 default of alias set 0 for types compared structurally. */
3560 if (TYPE_P (t
) && TYPE_STRUCTURAL_EQUALITY_P (t
))
3562 if (TREE_CODE (t
) == ARRAY_TYPE
)
3563 return get_alias_set (TREE_TYPE (t
));
3567 /* That's all the expressions we handle specially. */
3571 /* Unlike char, char8_t doesn't alias. */
3572 if (flag_char8_t
&& t
== char8_type_node
)
3575 /* The C standard guarantees that any object may be accessed via an
3576 lvalue that has narrow character type (except char8_t). */
3577 if (t
== char_type_node
3578 || t
== signed_char_type_node
3579 || t
== unsigned_char_type_node
)
3582 /* The C standard specifically allows aliasing between signed and
3583 unsigned variants of the same type. We treat the signed
3584 variant as canonical. */
3585 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_UNSIGNED (t
))
3587 tree t1
= c_common_signed_type (t
);
3589 /* t1 == t can happen for boolean nodes which are always unsigned. */
3591 return get_alias_set (t1
);
3597 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3598 the IS_SIZEOF parameter indicates which operator is being applied.
3599 The COMPLAIN flag controls whether we should diagnose possibly
3600 ill-formed constructs or not. LOC is the location of the SIZEOF or
3601 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3602 a type in any context should be returned, rather than the normal
3603 alignment for that type. */
3606 c_sizeof_or_alignof_type (location_t loc
,
3607 tree type
, bool is_sizeof
, bool min_alignof
,
3610 const char *op_name
;
3612 enum tree_code type_code
= TREE_CODE (type
);
3614 op_name
= is_sizeof
? "sizeof" : "__alignof__";
3616 if (type_code
== FUNCTION_TYPE
)
3620 if (complain
&& warn_pointer_arith
)
3621 pedwarn (loc
, OPT_Wpointer_arith
,
3622 "invalid application of %<sizeof%> to a function type");
3624 return error_mark_node
;
3625 value
= size_one_node
;
3631 if (c_dialect_cxx ())
3632 pedwarn (loc
, OPT_Wpedantic
, "ISO C++ does not permit "
3633 "%<alignof%> applied to a function type");
3635 pedwarn (loc
, OPT_Wpedantic
, "ISO C does not permit "
3636 "%<_Alignof%> applied to a function type");
3638 value
= size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
3641 else if (type_code
== VOID_TYPE
|| type_code
== ERROR_MARK
)
3643 if (type_code
== VOID_TYPE
3644 && complain
&& warn_pointer_arith
)
3645 pedwarn (loc
, OPT_Wpointer_arith
,
3646 "invalid application of %qs to a void type", op_name
);
3648 return error_mark_node
;
3649 value
= size_one_node
;
3651 else if (!COMPLETE_TYPE_P (type
)
3652 && (!c_dialect_cxx () || is_sizeof
|| type_code
!= ARRAY_TYPE
))
3655 error_at (loc
, "invalid application of %qs to incomplete type %qT",
3657 return error_mark_node
;
3659 else if (c_dialect_cxx () && type_code
== ARRAY_TYPE
3660 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
3663 error_at (loc
, "invalid application of %qs to array type %qT of "
3664 "incomplete element type", op_name
, type
);
3665 return error_mark_node
;
3667 else if (!verify_type_context (loc
, is_sizeof
? TCTX_SIZEOF
: TCTX_ALIGNOF
,
3671 return error_mark_node
;
3672 value
= size_one_node
;
3677 /* Convert in case a char is more than one unit. */
3678 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
3679 size_int (TYPE_PRECISION (char_type_node
)
3681 else if (min_alignof
)
3682 value
= size_int (min_align_of_type (type
));
3684 value
= size_int (TYPE_ALIGN_UNIT (type
));
3687 /* VALUE will have the middle-end integer type sizetype.
3688 However, we should really return a value of type `size_t',
3689 which is just a typedef for an ordinary integer type. */
3690 value
= fold_convert_loc (loc
, size_type_node
, value
);
3695 /* Implement the __alignof keyword: Return the minimum required
3696 alignment of EXPR, measured in bytes. For VAR_DECLs,
3697 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3698 from an "aligned" __attribute__ specification). LOC is the
3699 location of the ALIGNOF operator. */
3702 c_alignof_expr (location_t loc
, tree expr
)
3706 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (expr
)))
3709 else if (VAR_OR_FUNCTION_DECL_P (expr
))
3710 t
= size_int (DECL_ALIGN_UNIT (expr
));
3712 else if (TREE_CODE (expr
) == COMPONENT_REF
3713 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
3715 error_at (loc
, "%<__alignof%> applied to a bit-field");
3718 else if (TREE_CODE (expr
) == COMPONENT_REF
3719 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
3720 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr
, 1)));
3722 else if (INDIRECT_REF_P (expr
))
3724 tree t
= TREE_OPERAND (expr
, 0);
3726 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
3728 while (CONVERT_EXPR_P (t
)
3729 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
3733 t
= TREE_OPERAND (t
, 0);
3734 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
3735 if (thisalign
> bestalign
)
3736 best
= t
, bestalign
= thisalign
;
3738 return c_alignof (loc
, TREE_TYPE (TREE_TYPE (best
)));
3741 return c_alignof (loc
, TREE_TYPE (expr
));
3743 return fold_convert_loc (loc
, size_type_node
, t
);
3746 /* Handle C and C++ default attributes. */
3748 enum built_in_attribute
3750 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3751 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3752 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3753 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3754 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3755 #include "builtin-attrs.def"
3756 #undef DEF_ATTR_NULL_TREE
3758 #undef DEF_ATTR_STRING
3759 #undef DEF_ATTR_IDENT
3760 #undef DEF_ATTR_TREE_LIST
3764 static GTY(()) tree built_in_attributes
[(int) ATTR_LAST
];
3766 static void c_init_attributes (void);
3770 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3771 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3772 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3773 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3774 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3775 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3776 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3777 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3779 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3781 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3782 ARG6, ARG7, ARG8) NAME,
3783 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3784 ARG6, ARG7, ARG8, ARG9) NAME,
3785 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3786 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3787 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3788 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3789 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3790 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3791 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3792 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3793 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3794 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3796 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3798 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3800 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3801 #include "builtin-types.def"
3802 #undef DEF_PRIMITIVE_TYPE
3803 #undef DEF_FUNCTION_TYPE_0
3804 #undef DEF_FUNCTION_TYPE_1
3805 #undef DEF_FUNCTION_TYPE_2
3806 #undef DEF_FUNCTION_TYPE_3
3807 #undef DEF_FUNCTION_TYPE_4
3808 #undef DEF_FUNCTION_TYPE_5
3809 #undef DEF_FUNCTION_TYPE_6
3810 #undef DEF_FUNCTION_TYPE_7
3811 #undef DEF_FUNCTION_TYPE_8
3812 #undef DEF_FUNCTION_TYPE_9
3813 #undef DEF_FUNCTION_TYPE_10
3814 #undef DEF_FUNCTION_TYPE_11
3815 #undef DEF_FUNCTION_TYPE_VAR_0
3816 #undef DEF_FUNCTION_TYPE_VAR_1
3817 #undef DEF_FUNCTION_TYPE_VAR_2
3818 #undef DEF_FUNCTION_TYPE_VAR_3
3819 #undef DEF_FUNCTION_TYPE_VAR_4
3820 #undef DEF_FUNCTION_TYPE_VAR_5
3821 #undef DEF_FUNCTION_TYPE_VAR_6
3822 #undef DEF_FUNCTION_TYPE_VAR_7
3823 #undef DEF_POINTER_TYPE
3827 typedef enum c_builtin_type builtin_type
;
3829 /* A temporary array for c_common_nodes_and_builtins. Used in
3830 communication with def_fn_type. */
3831 static tree builtin_types
[(int) BT_LAST
+ 1];
3833 /* A helper function for c_common_nodes_and_builtins. Build function type
3834 for DEF with return type RET and N arguments. If VAR is true, then the
3835 function should be variadic after those N arguments.
3837 Takes special care not to ICE if any of the types involved are
3838 error_mark_node, which indicates that said type is not in fact available
3839 (see builtin_type_for_size). In which case the function type as a whole
3840 should be error_mark_node. */
3843 def_fn_type (builtin_type def
, builtin_type ret
, bool var
, int n
, ...)
3846 tree
*args
= XALLOCAVEC (tree
, n
);
3851 for (i
= 0; i
< n
; ++i
)
3853 builtin_type a
= (builtin_type
) va_arg (list
, int);
3854 t
= builtin_types
[a
];
3855 if (t
== error_mark_node
)
3860 t
= builtin_types
[ret
];
3861 if (t
== error_mark_node
)
3864 t
= build_varargs_function_type_array (t
, n
, args
);
3866 t
= build_function_type_array (t
, n
, args
);
3869 builtin_types
[def
] = t
;
3873 /* Build builtin functions common to both C and C++ language
3877 c_define_builtins (tree va_list_ref_type_node
, tree va_list_arg_type_node
)
3879 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3880 builtin_types[ENUM] = VALUE;
3881 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3882 def_fn_type (ENUM, RETURN, 0, 0);
3883 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3884 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3885 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3886 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3887 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3888 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3889 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3890 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3891 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3892 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3893 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3895 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3896 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3898 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3899 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3901 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3903 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3904 ARG6, ARG7, ARG8, ARG9) \
3905 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3907 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3908 ARG6, ARG7, ARG8, ARG9, ARG10) \
3909 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3910 ARG7, ARG8, ARG9, ARG10);
3911 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3912 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3913 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3914 ARG7, ARG8, ARG9, ARG10, ARG11);
3915 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3916 def_fn_type (ENUM, RETURN, 1, 0);
3917 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3918 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3919 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3920 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3921 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3922 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3923 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3924 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3925 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3926 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3927 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3929 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3930 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3932 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3933 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3934 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3936 #include "builtin-types.def"
3938 #undef DEF_PRIMITIVE_TYPE
3939 #undef DEF_FUNCTION_TYPE_0
3940 #undef DEF_FUNCTION_TYPE_1
3941 #undef DEF_FUNCTION_TYPE_2
3942 #undef DEF_FUNCTION_TYPE_3
3943 #undef DEF_FUNCTION_TYPE_4
3944 #undef DEF_FUNCTION_TYPE_5
3945 #undef DEF_FUNCTION_TYPE_6
3946 #undef DEF_FUNCTION_TYPE_7
3947 #undef DEF_FUNCTION_TYPE_8
3948 #undef DEF_FUNCTION_TYPE_9
3949 #undef DEF_FUNCTION_TYPE_10
3950 #undef DEF_FUNCTION_TYPE_11
3951 #undef DEF_FUNCTION_TYPE_VAR_0
3952 #undef DEF_FUNCTION_TYPE_VAR_1
3953 #undef DEF_FUNCTION_TYPE_VAR_2
3954 #undef DEF_FUNCTION_TYPE_VAR_3
3955 #undef DEF_FUNCTION_TYPE_VAR_4
3956 #undef DEF_FUNCTION_TYPE_VAR_5
3957 #undef DEF_FUNCTION_TYPE_VAR_6
3958 #undef DEF_FUNCTION_TYPE_VAR_7
3959 #undef DEF_POINTER_TYPE
3960 builtin_types
[(int) BT_LAST
] = NULL_TREE
;
3962 c_init_attributes ();
3964 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3965 NONANSI_P, ATTRS, IMPLICIT, COND) \
3967 def_builtin_1 (ENUM, NAME, CLASS, \
3968 builtin_types[(int) TYPE], \
3969 builtin_types[(int) LIBTYPE], \
3970 BOTH_P, FALLBACK_P, NONANSI_P, \
3971 built_in_attributes[(int) ATTRS], IMPLICIT);
3972 #include "builtins.def"
3974 targetm
.init_builtins ();
3976 build_common_builtin_nodes ();
3979 /* Like get_identifier, but avoid warnings about null arguments when
3980 the argument may be NULL for targets where GCC lacks stdint.h type
3984 c_get_ident (const char *id
)
3986 return get_identifier (id
);
3989 /* Build tree nodes and builtin functions common to both C and C++ language
3993 c_common_nodes_and_builtins (void)
3995 int char8_type_size
;
3996 int char16_type_size
;
3997 int char32_type_size
;
3998 int wchar_type_size
;
3999 tree array_domain_type
;
4000 tree va_list_ref_type_node
;
4001 tree va_list_arg_type_node
;
4004 build_common_tree_nodes (flag_signed_char
);
4006 /* Define `int' and `char' first so that dbx will output them first. */
4007 record_builtin_type (RID_INT
, NULL
, integer_type_node
);
4008 record_builtin_type (RID_CHAR
, "char", char_type_node
);
4010 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4011 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4012 but not C. Are the conditionals here needed? */
4013 if (c_dialect_cxx ())
4014 record_builtin_type (RID_SIGNED
, NULL
, integer_type_node
);
4015 record_builtin_type (RID_LONG
, "long int", long_integer_type_node
);
4016 record_builtin_type (RID_UNSIGNED
, "unsigned int", unsigned_type_node
);
4017 record_builtin_type (RID_MAX
, "long unsigned int",
4018 long_unsigned_type_node
);
4020 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
4024 sprintf (name
, "__int%d", int_n_data
[i
].bitsize
);
4025 record_builtin_type ((enum rid
)(RID_FIRST_INT_N
+ i
), name
,
4026 int_n_trees
[i
].signed_type
);
4027 sprintf (name
, "__int%d__", int_n_data
[i
].bitsize
);
4028 record_builtin_type ((enum rid
)(RID_FIRST_INT_N
+ i
), name
,
4029 int_n_trees
[i
].signed_type
);
4031 sprintf (name
, "__int%d unsigned", int_n_data
[i
].bitsize
);
4032 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
4033 sprintf (name
, "__int%d__ unsigned", int_n_data
[i
].bitsize
);
4034 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
4037 if (c_dialect_cxx ())
4038 record_builtin_type (RID_MAX
, "unsigned long", long_unsigned_type_node
);
4039 record_builtin_type (RID_MAX
, "long long int",
4040 long_long_integer_type_node
);
4041 record_builtin_type (RID_MAX
, "long long unsigned int",
4042 long_long_unsigned_type_node
);
4043 if (c_dialect_cxx ())
4044 record_builtin_type (RID_MAX
, "long long unsigned",
4045 long_long_unsigned_type_node
);
4046 record_builtin_type (RID_SHORT
, "short int", short_integer_type_node
);
4047 record_builtin_type (RID_MAX
, "short unsigned int",
4048 short_unsigned_type_node
);
4049 if (c_dialect_cxx ())
4050 record_builtin_type (RID_MAX
, "unsigned short",
4051 short_unsigned_type_node
);
4053 /* Define both `signed char' and `unsigned char'. */
4054 record_builtin_type (RID_MAX
, "signed char", signed_char_type_node
);
4055 record_builtin_type (RID_MAX
, "unsigned char", unsigned_char_type_node
);
4057 /* These are types that c_common_type_for_size and
4058 c_common_type_for_mode use. */
4059 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4060 TYPE_DECL
, NULL_TREE
,
4062 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4063 TYPE_DECL
, NULL_TREE
,
4065 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4066 TYPE_DECL
, NULL_TREE
,
4068 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4069 TYPE_DECL
, NULL_TREE
,
4071 #if HOST_BITS_PER_WIDE_INT >= 64
4072 /* Note that this is different than the __int128 type that's part of
4073 the generic __intN support. */
4074 if (targetm
.scalar_mode_supported_p (TImode
))
4075 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4077 get_identifier ("__int128_t"),
4080 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4081 TYPE_DECL
, NULL_TREE
,
4082 unsigned_intQI_type_node
));
4083 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4084 TYPE_DECL
, NULL_TREE
,
4085 unsigned_intHI_type_node
));
4086 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4087 TYPE_DECL
, NULL_TREE
,
4088 unsigned_intSI_type_node
));
4089 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4090 TYPE_DECL
, NULL_TREE
,
4091 unsigned_intDI_type_node
));
4092 #if HOST_BITS_PER_WIDE_INT >= 64
4093 if (targetm
.scalar_mode_supported_p (TImode
))
4094 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4096 get_identifier ("__uint128_t"),
4097 unsigned_intTI_type_node
));
4100 /* Create the widest literal types. */
4101 if (targetm
.scalar_mode_supported_p (TImode
))
4103 widest_integer_literal_type_node
= intTI_type_node
;
4104 widest_unsigned_literal_type_node
= unsigned_intTI_type_node
;
4108 widest_integer_literal_type_node
= intDI_type_node
;
4109 widest_unsigned_literal_type_node
= unsigned_intDI_type_node
;
4112 signed_size_type_node
= c_common_signed_type (size_type_node
);
4115 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE
)));
4117 record_builtin_type (RID_FLOAT
, NULL
, float_type_node
);
4118 record_builtin_type (RID_DOUBLE
, NULL
, double_type_node
);
4119 record_builtin_type (RID_MAX
, "long double", long_double_type_node
);
4121 if (!c_dialect_cxx ())
4122 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4123 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4124 record_builtin_type ((enum rid
) (RID_FLOATN_NX_FIRST
+ i
), NULL
,
4125 FLOATN_NX_TYPE_NODE (i
));
4127 /* Only supported decimal floating point extension if the target
4128 actually supports underlying modes. */
4129 if (targetm
.scalar_mode_supported_p (SDmode
)
4130 && targetm
.scalar_mode_supported_p (DDmode
)
4131 && targetm
.scalar_mode_supported_p (TDmode
))
4133 record_builtin_type (RID_DFLOAT32
, NULL
, dfloat32_type_node
);
4134 record_builtin_type (RID_DFLOAT64
, NULL
, dfloat64_type_node
);
4135 record_builtin_type (RID_DFLOAT128
, NULL
, dfloat128_type_node
);
4138 if (targetm
.fixed_point_supported_p ())
4140 record_builtin_type (RID_MAX
, "short _Fract", short_fract_type_node
);
4141 record_builtin_type (RID_FRACT
, NULL
, fract_type_node
);
4142 record_builtin_type (RID_MAX
, "long _Fract", long_fract_type_node
);
4143 record_builtin_type (RID_MAX
, "long long _Fract",
4144 long_long_fract_type_node
);
4145 record_builtin_type (RID_MAX
, "unsigned short _Fract",
4146 unsigned_short_fract_type_node
);
4147 record_builtin_type (RID_MAX
, "unsigned _Fract",
4148 unsigned_fract_type_node
);
4149 record_builtin_type (RID_MAX
, "unsigned long _Fract",
4150 unsigned_long_fract_type_node
);
4151 record_builtin_type (RID_MAX
, "unsigned long long _Fract",
4152 unsigned_long_long_fract_type_node
);
4153 record_builtin_type (RID_MAX
, "_Sat short _Fract",
4154 sat_short_fract_type_node
);
4155 record_builtin_type (RID_MAX
, "_Sat _Fract", sat_fract_type_node
);
4156 record_builtin_type (RID_MAX
, "_Sat long _Fract",
4157 sat_long_fract_type_node
);
4158 record_builtin_type (RID_MAX
, "_Sat long long _Fract",
4159 sat_long_long_fract_type_node
);
4160 record_builtin_type (RID_MAX
, "_Sat unsigned short _Fract",
4161 sat_unsigned_short_fract_type_node
);
4162 record_builtin_type (RID_MAX
, "_Sat unsigned _Fract",
4163 sat_unsigned_fract_type_node
);
4164 record_builtin_type (RID_MAX
, "_Sat unsigned long _Fract",
4165 sat_unsigned_long_fract_type_node
);
4166 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Fract",
4167 sat_unsigned_long_long_fract_type_node
);
4168 record_builtin_type (RID_MAX
, "short _Accum", short_accum_type_node
);
4169 record_builtin_type (RID_ACCUM
, NULL
, accum_type_node
);
4170 record_builtin_type (RID_MAX
, "long _Accum", long_accum_type_node
);
4171 record_builtin_type (RID_MAX
, "long long _Accum",
4172 long_long_accum_type_node
);
4173 record_builtin_type (RID_MAX
, "unsigned short _Accum",
4174 unsigned_short_accum_type_node
);
4175 record_builtin_type (RID_MAX
, "unsigned _Accum",
4176 unsigned_accum_type_node
);
4177 record_builtin_type (RID_MAX
, "unsigned long _Accum",
4178 unsigned_long_accum_type_node
);
4179 record_builtin_type (RID_MAX
, "unsigned long long _Accum",
4180 unsigned_long_long_accum_type_node
);
4181 record_builtin_type (RID_MAX
, "_Sat short _Accum",
4182 sat_short_accum_type_node
);
4183 record_builtin_type (RID_MAX
, "_Sat _Accum", sat_accum_type_node
);
4184 record_builtin_type (RID_MAX
, "_Sat long _Accum",
4185 sat_long_accum_type_node
);
4186 record_builtin_type (RID_MAX
, "_Sat long long _Accum",
4187 sat_long_long_accum_type_node
);
4188 record_builtin_type (RID_MAX
, "_Sat unsigned short _Accum",
4189 sat_unsigned_short_accum_type_node
);
4190 record_builtin_type (RID_MAX
, "_Sat unsigned _Accum",
4191 sat_unsigned_accum_type_node
);
4192 record_builtin_type (RID_MAX
, "_Sat unsigned long _Accum",
4193 sat_unsigned_long_accum_type_node
);
4194 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Accum",
4195 sat_unsigned_long_long_accum_type_node
);
4199 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4201 get_identifier ("complex int"),
4202 complex_integer_type_node
));
4203 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4205 get_identifier ("complex float"),
4206 complex_float_type_node
));
4207 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4209 get_identifier ("complex double"),
4210 complex_double_type_node
));
4211 lang_hooks
.decls
.pushdecl
4212 (build_decl (UNKNOWN_LOCATION
,
4213 TYPE_DECL
, get_identifier ("complex long double"),
4214 complex_long_double_type_node
));
4216 if (!c_dialect_cxx ())
4217 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4218 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4221 sprintf (buf
, "complex _Float%d%s", floatn_nx_types
[i
].n
,
4222 floatn_nx_types
[i
].extended
? "x" : "");
4223 lang_hooks
.decls
.pushdecl
4224 (build_decl (UNKNOWN_LOCATION
,
4226 get_identifier (buf
),
4227 COMPLEX_FLOATN_NX_TYPE_NODE (i
)));
4230 /* Make fileptr_type_node a distinct void * type until
4231 FILE type is defined. Likewise for const struct tm*. */
4232 for (unsigned i
= 0;
4233 i
< sizeof (builtin_structptr_types
) / sizeof (builtin_structptr_type
);
4235 builtin_structptr_types
[i
].node
4236 = build_variant_type_copy (builtin_structptr_types
[i
].base
);
4238 record_builtin_type (RID_VOID
, NULL
, void_type_node
);
4240 /* Set the TYPE_NAME for any variants that were built before
4241 record_builtin_type gave names to the built-in types. */
4243 tree void_name
= TYPE_NAME (void_type_node
);
4244 TYPE_NAME (void_type_node
) = NULL_TREE
;
4245 TYPE_NAME (build_qualified_type (void_type_node
, TYPE_QUAL_CONST
))
4247 TYPE_NAME (void_type_node
) = void_name
;
4250 void_list_node
= build_void_list_node ();
4252 /* Make a type to be the domain of a few array types
4253 whose domains don't really matter.
4254 200 is small enough that it always fits in size_t
4255 and large enough that it can hold most function names for the
4256 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4257 array_domain_type
= build_index_type (size_int (200));
4259 /* Make a type for arrays of characters.
4260 With luck nothing will ever really depend on the length of this
4262 char_array_type_node
4263 = build_array_type (char_type_node
, array_domain_type
);
4265 string_type_node
= build_pointer_type (char_type_node
);
4266 const_string_type_node
4267 = build_pointer_type (build_qualified_type
4268 (char_type_node
, TYPE_QUAL_CONST
));
4270 /* This is special for C++ so functions can be overloaded. */
4271 wchar_type_node
= get_identifier (MODIFIED_WCHAR_TYPE
);
4272 wchar_type_node
= TREE_TYPE (identifier_global_value (wchar_type_node
));
4273 wchar_type_size
= TYPE_PRECISION (wchar_type_node
);
4274 underlying_wchar_type_node
= wchar_type_node
;
4275 if (c_dialect_cxx ())
4277 if (TYPE_UNSIGNED (wchar_type_node
))
4278 wchar_type_node
= make_unsigned_type (wchar_type_size
);
4280 wchar_type_node
= make_signed_type (wchar_type_size
);
4281 record_builtin_type (RID_WCHAR
, "wchar_t", wchar_type_node
);
4284 /* This is for wide string constants. */
4285 wchar_array_type_node
4286 = build_array_type (wchar_type_node
, array_domain_type
);
4288 /* Define 'char8_t'. */
4289 char8_type_node
= get_identifier (CHAR8_TYPE
);
4290 char8_type_node
= TREE_TYPE (identifier_global_value (char8_type_node
));
4291 char8_type_size
= TYPE_PRECISION (char8_type_node
);
4292 if (c_dialect_cxx ())
4294 char8_type_node
= make_unsigned_type (char8_type_size
);
4297 record_builtin_type (RID_CHAR8
, "char8_t", char8_type_node
);
4300 /* This is for UTF-8 string constants. */
4301 char8_array_type_node
4302 = build_array_type (char8_type_node
, array_domain_type
);
4304 /* Define 'char16_t'. */
4305 char16_type_node
= get_identifier (CHAR16_TYPE
);
4306 char16_type_node
= TREE_TYPE (identifier_global_value (char16_type_node
));
4307 char16_type_size
= TYPE_PRECISION (char16_type_node
);
4308 if (c_dialect_cxx ())
4310 char16_type_node
= make_unsigned_type (char16_type_size
);
4312 if (cxx_dialect
>= cxx11
)
4313 record_builtin_type (RID_CHAR16
, "char16_t", char16_type_node
);
4316 /* This is for UTF-16 string constants. */
4317 char16_array_type_node
4318 = build_array_type (char16_type_node
, array_domain_type
);
4320 /* Define 'char32_t'. */
4321 char32_type_node
= get_identifier (CHAR32_TYPE
);
4322 char32_type_node
= TREE_TYPE (identifier_global_value (char32_type_node
));
4323 char32_type_size
= TYPE_PRECISION (char32_type_node
);
4324 if (c_dialect_cxx ())
4326 char32_type_node
= make_unsigned_type (char32_type_size
);
4328 if (cxx_dialect
>= cxx11
)
4329 record_builtin_type (RID_CHAR32
, "char32_t", char32_type_node
);
4332 /* This is for UTF-32 string constants. */
4333 char32_array_type_node
4334 = build_array_type (char32_type_node
, array_domain_type
);
4337 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE
)));
4340 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE
)));
4342 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE
)));
4344 if (SIG_ATOMIC_TYPE
)
4345 sig_atomic_type_node
=
4346 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE
)));
4349 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE
)));
4352 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE
)));
4355 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE
)));
4358 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE
)));
4361 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE
)));
4363 c_uint16_type_node
= uint16_type_node
=
4364 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE
)));
4366 c_uint32_type_node
= uint32_type_node
=
4367 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE
)));
4369 c_uint64_type_node
= uint64_type_node
=
4370 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE
)));
4371 if (INT_LEAST8_TYPE
)
4372 int_least8_type_node
=
4373 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE
)));
4374 if (INT_LEAST16_TYPE
)
4375 int_least16_type_node
=
4376 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE
)));
4377 if (INT_LEAST32_TYPE
)
4378 int_least32_type_node
=
4379 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE
)));
4380 if (INT_LEAST64_TYPE
)
4381 int_least64_type_node
=
4382 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE
)));
4383 if (UINT_LEAST8_TYPE
)
4384 uint_least8_type_node
=
4385 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE
)));
4386 if (UINT_LEAST16_TYPE
)
4387 uint_least16_type_node
=
4388 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE
)));
4389 if (UINT_LEAST32_TYPE
)
4390 uint_least32_type_node
=
4391 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE
)));
4392 if (UINT_LEAST64_TYPE
)
4393 uint_least64_type_node
=
4394 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE
)));
4396 int_fast8_type_node
=
4397 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE
)));
4398 if (INT_FAST16_TYPE
)
4399 int_fast16_type_node
=
4400 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE
)));
4401 if (INT_FAST32_TYPE
)
4402 int_fast32_type_node
=
4403 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE
)));
4404 if (INT_FAST64_TYPE
)
4405 int_fast64_type_node
=
4406 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE
)));
4407 if (UINT_FAST8_TYPE
)
4408 uint_fast8_type_node
=
4409 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE
)));
4410 if (UINT_FAST16_TYPE
)
4411 uint_fast16_type_node
=
4412 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE
)));
4413 if (UINT_FAST32_TYPE
)
4414 uint_fast32_type_node
=
4415 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE
)));
4416 if (UINT_FAST64_TYPE
)
4417 uint_fast64_type_node
=
4418 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE
)));
4421 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE
)));
4424 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE
)));
4426 default_function_type
4427 = build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4428 unsigned_ptrdiff_type_node
= c_common_unsigned_type (ptrdiff_type_node
);
4430 lang_hooks
.decls
.pushdecl
4431 (build_decl (UNKNOWN_LOCATION
,
4432 TYPE_DECL
, get_identifier ("__builtin_va_list"),
4433 va_list_type_node
));
4434 if (targetm
.enum_va_list_p
)
4440 for (l
= 0; targetm
.enum_va_list_p (l
, &pname
, &ptype
); ++l
)
4442 lang_hooks
.decls
.pushdecl
4443 (build_decl (UNKNOWN_LOCATION
,
4444 TYPE_DECL
, get_identifier (pname
),
4450 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4452 va_list_arg_type_node
= va_list_ref_type_node
=
4453 build_pointer_type (TREE_TYPE (va_list_type_node
));
4457 va_list_arg_type_node
= va_list_type_node
;
4458 va_list_ref_type_node
= build_reference_type (va_list_type_node
);
4461 c_define_builtins (va_list_ref_type_node
, va_list_arg_type_node
);
4463 main_identifier_node
= get_identifier ("main");
4465 /* Create the built-in __null node. It is important that this is
4467 null_node
= make_int_cst (1, 1);
4468 TREE_TYPE (null_node
) = c_common_type_for_size (POINTER_SIZE
, 0);
4470 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4471 memset (builtin_types
, 0, sizeof (builtin_types
));
4474 /* The number of named compound-literals generated thus far. */
4475 static GTY(()) int compound_literal_number
;
4477 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4480 set_compound_literal_name (tree decl
)
4483 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
4484 compound_literal_number
);
4485 compound_literal_number
++;
4486 DECL_NAME (decl
) = get_identifier (name
);
4489 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4490 TYPE and operand OP. */
4493 build_va_arg_1 (location_t loc
, tree type
, tree op
)
4495 tree expr
= build1 (VA_ARG_EXPR
, type
, op
);
4496 SET_EXPR_LOCATION (expr
, loc
);
4500 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4501 va_arg (EXPR, TYPE) at source location LOC. */
4504 build_va_arg (location_t loc
, tree expr
, tree type
)
4506 tree va_type
= TREE_TYPE (expr
);
4507 tree canon_va_type
= (va_type
== error_mark_node
4509 : targetm
.canonical_va_list_type (va_type
));
4511 if (va_type
== error_mark_node
4512 || canon_va_type
== NULL_TREE
)
4514 if (canon_va_type
== NULL_TREE
)
4515 error_at (loc
, "first argument to %<va_arg%> not of type %<va_list%>");
4517 /* Let's handle things neutrallly, if expr:
4518 - has undeclared type, or
4519 - is not an va_list type. */
4520 return build_va_arg_1 (loc
, type
, error_mark_node
);
4523 if (TREE_CODE (canon_va_type
) != ARRAY_TYPE
)
4525 /* Case 1: Not an array type. */
4527 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4529 mark_addressable (expr
);
4530 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (expr
)), expr
);
4532 return build_va_arg_1 (loc
, type
, expr
);
4535 /* Case 2: Array type.
4539 For contrast, let's start with the simple case (case 1). If
4540 canon_va_type is not an array type, but say a char *, then when
4541 passing-by-value a va_list, the type of the va_list param decl is
4542 the same as for another va_list decl (all ap's are char *):
4545 D.1815 = VA_ARG (&ap, 0B, 1);
4551 __builtin_va_start (&ap, 0);
4554 __builtin_va_end (&ap);
4558 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4559 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4560 the same as for another va_list decl (case 2a, struct ap[1]).
4563 D.1844 = VA_ARG (ap, 0B, 0);
4568 __builtin_va_start (&ap, 0);
4570 __builtin_va_end (&ap);
4574 Case 2b is different because:
4575 - on the callee side, the parm decl has declared type va_list, but
4576 grokdeclarator changes the type of the parm decl to a pointer to the
4578 - on the caller side, the pass-by-value uses &ap.
4580 We unify these two cases (case 2a: va_list is array type,
4581 case 2b: va_list is pointer to array elem type), by adding '&' for the
4582 array type case, such that we have a pointer to array elem in both
4585 if (TREE_CODE (va_type
) == ARRAY_TYPE
)
4587 /* Case 2a: va_list is array type. */
4589 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4591 mark_addressable (expr
);
4592 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (canon_va_type
)),
4595 /* Verify that &ap is still recognized as having va_list type. */
4596 tree canon_expr_type
4597 = targetm
.canonical_va_list_type (TREE_TYPE (expr
));
4598 gcc_assert (canon_expr_type
!= NULL_TREE
);
4602 /* Case 2b: va_list is pointer to array elem type. */
4603 gcc_assert (POINTER_TYPE_P (va_type
));
4605 /* Comparison as in std_canonical_va_list_type. */
4606 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type
))
4607 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type
)));
4609 /* Don't take the address. We've already got '&ap'. */
4613 return build_va_arg_1 (loc
, type
, expr
);
4617 /* Linked list of disabled built-in functions. */
4619 struct disabled_builtin
4622 struct disabled_builtin
*next
;
4624 static disabled_builtin
*disabled_builtins
= NULL
;
4626 static bool builtin_function_disabled_p (const char *);
4628 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4629 begins with "__builtin_", give an error. */
4632 disable_builtin_function (const char *name
)
4634 if (strncmp (name
, "__builtin_", strlen ("__builtin_")) == 0)
4635 error ("cannot disable built-in function %qs", name
);
4638 disabled_builtin
*new_disabled_builtin
= XNEW (disabled_builtin
);
4639 new_disabled_builtin
->name
= name
;
4640 new_disabled_builtin
->next
= disabled_builtins
;
4641 disabled_builtins
= new_disabled_builtin
;
4646 /* Return true if the built-in function NAME has been disabled, false
4650 builtin_function_disabled_p (const char *name
)
4652 disabled_builtin
*p
;
4653 for (p
= disabled_builtins
; p
!= NULL
; p
= p
->next
)
4655 if (strcmp (name
, p
->name
) == 0)
4662 /* Worker for DEF_BUILTIN.
4663 Possibly define a builtin function with one or two names.
4664 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4665 nonansi_p and flag_no_nonansi_builtin. */
4668 def_builtin_1 (enum built_in_function fncode
,
4670 enum built_in_class fnclass
,
4671 tree fntype
, tree libtype
,
4672 bool both_p
, bool fallback_p
, bool nonansi_p
,
4673 tree fnattrs
, bool implicit_p
)
4676 const char *libname
;
4678 if (fntype
== error_mark_node
)
4681 gcc_assert ((!both_p
&& !fallback_p
)
4682 || !strncmp (name
, "__builtin_",
4683 strlen ("__builtin_")));
4685 libname
= name
+ strlen ("__builtin_");
4686 decl
= add_builtin_function (name
, fntype
, fncode
, fnclass
,
4687 (fallback_p
? libname
: NULL
),
4690 set_builtin_decl (fncode
, decl
, implicit_p
);
4693 && !flag_no_builtin
&& !builtin_function_disabled_p (libname
)
4694 && !(nonansi_p
&& flag_no_nonansi_builtin
))
4695 add_builtin_function (libname
, libtype
, fncode
, fnclass
,
4699 /* Nonzero if the type T promotes to int. This is (nearly) the
4700 integral promotions defined in ISO C99 6.3.1.1/2. */
4703 c_promoting_integer_type_p (const_tree t
)
4705 switch (TREE_CODE (t
))
4708 return (TYPE_MAIN_VARIANT (t
) == char_type_node
4709 || TYPE_MAIN_VARIANT (t
) == signed_char_type_node
4710 || TYPE_MAIN_VARIANT (t
) == unsigned_char_type_node
4711 || TYPE_MAIN_VARIANT (t
) == short_integer_type_node
4712 || TYPE_MAIN_VARIANT (t
) == short_unsigned_type_node
4713 || TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
));
4716 /* ??? Technically all enumerations not larger than an int
4717 promote to an int. But this is used along code paths
4718 that only want to notice a size change. */
4719 return TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
);
4729 /* Return 1 if PARMS specifies a fixed number of parameters
4730 and none of their types is affected by default promotions. */
4733 self_promoting_args_p (const_tree parms
)
4736 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
4738 tree type
= TREE_VALUE (t
);
4740 if (type
== error_mark_node
)
4743 if (TREE_CHAIN (t
) == NULL_TREE
&& type
!= void_type_node
)
4746 if (type
== NULL_TREE
)
4749 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
4752 if (c_promoting_integer_type_p (type
))
4758 /* Recursively remove any '*' or '&' operator from TYPE. */
4760 strip_pointer_operator (tree t
)
4762 while (POINTER_TYPE_P (t
))
4767 /* Recursively remove pointer or array type from TYPE. */
4769 strip_pointer_or_array_types (tree t
)
4771 while (TREE_CODE (t
) == ARRAY_TYPE
|| POINTER_TYPE_P (t
))
4776 /* Used to compare case labels. K1 and K2 are actually tree nodes
4777 representing case labels, or NULL_TREE for a `default' label.
4778 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4779 K2, and 0 if K1 and K2 are equal. */
4782 case_compare (splay_tree_key k1
, splay_tree_key k2
)
4784 /* Consider a NULL key (such as arises with a `default' label) to be
4785 smaller than anything else. */
4791 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
4794 /* Process a case label, located at LOC, for the range LOW_VALUE
4795 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4796 then this case label is actually a `default' label. If only
4797 HIGH_VALUE is NULL_TREE, then case label was declared using the
4798 usual C/C++ syntax, rather than the GNU case range extension.
4799 CASES is a tree containing all the case ranges processed so far;
4800 COND is the condition for the switch-statement itself.
4801 Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
4802 CASE_LABEL_EXPR is created. */
4805 c_add_case_label (location_t loc
, splay_tree cases
, tree cond
,
4806 tree low_value
, tree high_value
)
4811 splay_tree_node node
;
4813 /* Create the LABEL_DECL itself. */
4814 label
= create_artificial_label (loc
);
4816 /* If there was an error processing the switch condition, bail now
4817 before we get more confused. */
4818 if (!cond
|| cond
== error_mark_node
)
4821 if ((low_value
&& TREE_TYPE (low_value
)
4822 && POINTER_TYPE_P (TREE_TYPE (low_value
)))
4823 || (high_value
&& TREE_TYPE (high_value
)
4824 && POINTER_TYPE_P (TREE_TYPE (high_value
))))
4826 error_at (loc
, "pointers are not permitted as case values");
4830 /* Case ranges are a GNU extension. */
4832 pedwarn (loc
, OPT_Wpedantic
,
4833 "range expressions in switch statements are non-standard");
4835 type
= TREE_TYPE (cond
);
4838 low_value
= check_case_value (loc
, low_value
);
4839 low_value
= convert_and_check (loc
, type
, low_value
);
4840 low_value
= fold (low_value
);
4841 if (low_value
== error_mark_node
)
4846 high_value
= check_case_value (loc
, high_value
);
4847 high_value
= convert_and_check (loc
, type
, high_value
);
4848 high_value
= fold (high_value
);
4849 if (high_value
== error_mark_node
)
4853 if (low_value
&& high_value
)
4855 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4856 really a case range, even though it was written that way.
4857 Remove the HIGH_VALUE to simplify later processing. */
4858 if (tree_int_cst_equal (low_value
, high_value
))
4859 high_value
= NULL_TREE
;
4860 else if (!tree_int_cst_lt (low_value
, high_value
))
4861 warning_at (loc
, 0, "empty range specified");
4864 /* Look up the LOW_VALUE in the table of case labels we already
4866 node
= splay_tree_lookup (cases
, (splay_tree_key
) low_value
);
4867 /* If there was not an exact match, check for overlapping ranges.
4868 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4869 that's a `default' label and the only overlap is an exact match. */
4870 if (!node
&& (low_value
|| high_value
))
4872 splay_tree_node low_bound
;
4873 splay_tree_node high_bound
;
4875 /* Even though there wasn't an exact match, there might be an
4876 overlap between this case range and another case range.
4877 Since we've (inductively) not allowed any overlapping case
4878 ranges, we simply need to find the greatest low case label
4879 that is smaller that LOW_VALUE, and the smallest low case
4880 label that is greater than LOW_VALUE. If there is an overlap
4881 it will occur in one of these two ranges. */
4882 low_bound
= splay_tree_predecessor (cases
,
4883 (splay_tree_key
) low_value
);
4884 high_bound
= splay_tree_successor (cases
,
4885 (splay_tree_key
) low_value
);
4887 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4888 the LOW_VALUE, so there is no need to check unless the
4889 LOW_BOUND is in fact itself a case range. */
4891 && CASE_HIGH ((tree
) low_bound
->value
)
4892 && tree_int_cst_compare (CASE_HIGH ((tree
) low_bound
->value
),
4895 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4896 range is bigger than the low end of the current range, so we
4897 are only interested if the current range is a real range, and
4898 not an ordinary case label. */
4901 && (tree_int_cst_compare ((tree
) high_bound
->key
,
4906 /* If there was an overlap, issue an error. */
4909 tree duplicate
= CASE_LABEL ((tree
) node
->value
);
4913 error_at (loc
, "duplicate (or overlapping) case value");
4914 inform (DECL_SOURCE_LOCATION (duplicate
),
4915 "this is the first entry overlapping that value");
4919 error_at (loc
, "duplicate case value") ;
4920 inform (DECL_SOURCE_LOCATION (duplicate
), "previously used here");
4924 error_at (loc
, "multiple default labels in one switch");
4925 inform (DECL_SOURCE_LOCATION (duplicate
),
4926 "this is the first default label");
4931 /* Add a CASE_LABEL to the statement-tree. */
4932 case_label
= add_stmt (build_case_label (low_value
, high_value
, label
));
4933 /* Register this case label in the splay tree. */
4934 splay_tree_insert (cases
,
4935 (splay_tree_key
) low_value
,
4936 (splay_tree_value
) case_label
);
4941 /* Add a label so that the back-end doesn't think that the beginning of
4942 the switch is unreachable. Note that we do not add a case label, as
4943 that just leads to duplicates and thence to failure later on. */
4946 tree t
= create_artificial_label (loc
);
4947 add_stmt (build_stmt (loc
, LABEL_EXPR
, t
));
4949 return error_mark_node
;
4952 /* Subroutine of c_switch_covers_all_cases_p, called via
4953 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4954 ARGS[0] is initially NULL and after the first iteration is the
4955 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4959 c_switch_covers_all_cases_p_1 (splay_tree_node node
, void *data
)
4961 tree label
= (tree
) node
->value
;
4962 tree
*args
= (tree
*) data
;
4964 /* If there is a default case, we shouldn't have called this. */
4965 gcc_assert (CASE_LOW (label
));
4967 if (args
[0] == NULL_TREE
)
4969 if (wi::to_widest (args
[1]) < wi::to_widest (CASE_LOW (label
)))
4972 else if (wi::add (wi::to_widest (args
[0]), 1)
4973 != wi::to_widest (CASE_LOW (label
)))
4975 if (CASE_HIGH (label
))
4976 args
[0] = CASE_HIGH (label
);
4978 args
[0] = CASE_LOW (label
);
4982 /* Return true if switch with CASES and switch condition with type
4983 covers all possible values in the case labels. */
4986 c_switch_covers_all_cases_p (splay_tree cases
, tree type
)
4988 /* If there is default:, this is always the case. */
4989 splay_tree_node default_node
4990 = splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
4994 if (!INTEGRAL_TYPE_P (type
))
4997 tree args
[2] = { NULL_TREE
, TYPE_MIN_VALUE (type
) };
4998 if (splay_tree_foreach (cases
, c_switch_covers_all_cases_p_1
, args
))
5001 /* If there are no cases at all, or if the highest case label
5002 is smaller than TYPE_MAX_VALUE, return false. */
5003 if (args
[0] == NULL_TREE
5004 || wi::to_widest (args
[0]) < wi::to_widest (TYPE_MAX_VALUE (type
)))
5010 /* Finish an expression taking the address of LABEL (an
5011 IDENTIFIER_NODE). Returns an expression for the address.
5013 LOC is the location for the expression returned. */
5016 finish_label_address_expr (tree label
, location_t loc
)
5020 pedwarn (input_location
, OPT_Wpedantic
, "taking the address of a label is non-standard");
5022 if (label
== error_mark_node
)
5023 return error_mark_node
;
5025 label
= lookup_label (label
);
5026 if (label
== NULL_TREE
)
5027 result
= null_pointer_node
;
5030 TREE_USED (label
) = 1;
5031 result
= build1 (ADDR_EXPR
, ptr_type_node
, label
);
5032 /* The current function is not necessarily uninlinable.
5033 Computed gotos are incompatible with inlining, but the value
5034 here could be used only in a diagnostic, for example. */
5035 protected_set_expr_location (result
, loc
);
5042 /* Given a boolean expression ARG, return a tree representing an increment
5043 or decrement (as indicated by CODE) of ARG. The front end must check for
5044 invalid cases (e.g., decrement in C++). */
5046 boolean_increment (enum tree_code code
, tree arg
)
5049 tree true_res
= build_int_cst (TREE_TYPE (arg
), 1);
5051 arg
= stabilize_reference (arg
);
5054 case PREINCREMENT_EXPR
:
5055 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5057 case POSTINCREMENT_EXPR
:
5058 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5059 arg
= save_expr (arg
);
5060 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5061 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5063 case PREDECREMENT_EXPR
:
5064 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5065 invert_truthvalue_loc (input_location
, arg
));
5067 case POSTDECREMENT_EXPR
:
5068 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5069 invert_truthvalue_loc (input_location
, arg
));
5070 arg
= save_expr (arg
);
5071 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5072 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5077 TREE_SIDE_EFFECTS (val
) = 1;
5081 /* Built-in macros for stddef.h and stdint.h, that require macros
5082 defined in this file. */
5084 c_stddef_cpp_builtins(void)
5086 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE
, 0);
5087 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
, 0);
5088 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE
, 0);
5089 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE
, 0);
5090 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE
, 0);
5091 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE
, 0);
5093 builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE
, 0);
5094 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE
, 0);
5095 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE
, 0);
5096 if (SIG_ATOMIC_TYPE
)
5097 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE
, 0);
5099 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE
, 0);
5101 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE
, 0);
5103 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE
, 0);
5105 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE
, 0);
5107 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE
, 0);
5109 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE
, 0);
5111 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE
, 0);
5113 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE
, 0);
5114 if (INT_LEAST8_TYPE
)
5115 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE
, 0);
5116 if (INT_LEAST16_TYPE
)
5117 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE
, 0);
5118 if (INT_LEAST32_TYPE
)
5119 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE
, 0);
5120 if (INT_LEAST64_TYPE
)
5121 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE
, 0);
5122 if (UINT_LEAST8_TYPE
)
5123 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE
, 0);
5124 if (UINT_LEAST16_TYPE
)
5125 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE
, 0);
5126 if (UINT_LEAST32_TYPE
)
5127 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE
, 0);
5128 if (UINT_LEAST64_TYPE
)
5129 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE
, 0);
5131 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE
, 0);
5132 if (INT_FAST16_TYPE
)
5133 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE
, 0);
5134 if (INT_FAST32_TYPE
)
5135 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE
, 0);
5136 if (INT_FAST64_TYPE
)
5137 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE
, 0);
5138 if (UINT_FAST8_TYPE
)
5139 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE
, 0);
5140 if (UINT_FAST16_TYPE
)
5141 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE
, 0);
5142 if (UINT_FAST32_TYPE
)
5143 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE
, 0);
5144 if (UINT_FAST64_TYPE
)
5145 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE
, 0);
5147 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE
, 0);
5149 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE
, 0);
5150 /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5151 Expose it as __SIZETYPE__. */
5153 builtin_define_with_value ("__SIZETYPE__", SIZETYPE
, 0);
5157 c_init_attributes (void)
5159 /* Fill in the built_in_attributes array. */
5160 #define DEF_ATTR_NULL_TREE(ENUM) \
5161 built_in_attributes[(int) ENUM] = NULL_TREE;
5162 #define DEF_ATTR_INT(ENUM, VALUE) \
5163 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5164 #define DEF_ATTR_STRING(ENUM, VALUE) \
5165 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5166 #define DEF_ATTR_IDENT(ENUM, STRING) \
5167 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5168 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5169 built_in_attributes[(int) ENUM] \
5170 = tree_cons (built_in_attributes[(int) PURPOSE], \
5171 built_in_attributes[(int) VALUE], \
5172 built_in_attributes[(int) CHAIN]);
5173 #include "builtin-attrs.def"
5174 #undef DEF_ATTR_NULL_TREE
5176 #undef DEF_ATTR_IDENT
5177 #undef DEF_ATTR_TREE_LIST
5180 /* Check whether the byte alignment ALIGN is a valid user-specified
5181 alignment less than the supported maximum. If so, return ALIGN's
5182 base-2 log; if not, output an error and return -1. If OBJFILE
5183 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5184 converted to bits. Otherwise, consider valid only alignments
5185 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5186 Zero is not considered a valid argument (and results in -1 on
5187 return) but it only triggers a warning when WARN_ZERO is set. */
5190 check_user_alignment (const_tree align
, bool objfile
, bool warn_zero
)
5192 if (error_operand_p (align
))
5195 if (TREE_CODE (align
) != INTEGER_CST
5196 || !INTEGRAL_TYPE_P (TREE_TYPE (align
)))
5198 error ("requested alignment is not an integer constant");
5202 if (integer_zerop (align
))
5205 warning (OPT_Wattributes
,
5206 "requested alignment %qE is not a positive power of 2",
5211 /* Log2 of the byte alignment ALIGN. */
5213 if (tree_int_cst_sgn (align
) == -1
5214 || (log2align
= tree_log2 (align
)) == -1)
5216 error ("requested alignment %qE is not a positive power of 2",
5223 unsigned maxalign
= MAX_OFILE_ALIGNMENT
/ BITS_PER_UNIT
;
5224 if (!tree_fits_uhwi_p (align
) || tree_to_uhwi (align
) > maxalign
)
5226 error ("requested alignment %qE exceeds object file maximum %u",
5232 if (log2align
>= HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
)
5234 error ("requested alignment %qE exceeds maximum %u",
5235 align
, 1U << (HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
- 1));
5242 /* Determine the ELF symbol visibility for DECL, which is either a
5243 variable or a function. It is an error to use this function if a
5244 definition of DECL is not available in this translation unit.
5245 Returns true if the final visibility has been determined by this
5246 function; false if the caller is free to make additional
5250 c_determine_visibility (tree decl
)
5252 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
5254 /* If the user explicitly specified the visibility with an
5255 attribute, honor that. DECL_VISIBILITY will have been set during
5256 the processing of the attribute. We check for an explicit
5257 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5258 to distinguish the use of an attribute from the use of a "#pragma
5259 GCC visibility push(...)"; in the latter case we still want other
5260 considerations to be able to overrule the #pragma. */
5261 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl
))
5262 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5263 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
))
5264 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))))
5267 /* Set default visibility to whatever the user supplied with
5268 visibility_specified depending on #pragma GCC visibility. */
5269 if (!DECL_VISIBILITY_SPECIFIED (decl
))
5271 if (visibility_options
.inpragma
5272 || DECL_VISIBILITY (decl
) != default_visibility
)
5274 DECL_VISIBILITY (decl
) = default_visibility
;
5275 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
5276 /* If visibility changed and DECL already has DECL_RTL, ensure
5277 symbol flags are updated. */
5278 if (((VAR_P (decl
) && TREE_STATIC (decl
))
5279 || TREE_CODE (decl
) == FUNCTION_DECL
)
5280 && DECL_RTL_SET_P (decl
))
5281 make_decl_rtl (decl
);
5287 /* Data to communicate through check_function_arguments_recurse between
5288 check_function_nonnull and check_nonnull_arg. */
5290 struct nonnull_arg_ctx
5292 /* Location of the call. */
5294 /* The function whose arguments are being checked and its type (used
5295 for calls through function pointers). */
5296 const_tree fndecl
, fntype
;
5297 /* True if a warning has been issued. */
5301 /* Check the argument list of a function call to CTX.FNDECL of CTX.FNTYPE
5302 for null in argument slots that are marked as requiring a non-null
5303 pointer argument. The NARGS arguments are passed in the array ARGARRAY.
5304 Return true if we have warned. */
5307 check_function_nonnull (nonnull_arg_ctx
&ctx
, int nargs
, tree
*argarray
)
5310 if (TREE_CODE (ctx
.fntype
) == METHOD_TYPE
)
5312 bool closure
= false;
5315 /* For certain lambda expressions the C++ front end emits calls
5316 that pass a null this pointer as an argument named __closure
5317 to the member operator() of empty function. Detect those
5318 and avoid checking them, but proceed to check the remaining
5320 tree arg0
= DECL_ARGUMENTS (ctx
.fndecl
);
5321 if (tree arg0name
= DECL_NAME (arg0
))
5322 closure
= id_equal (arg0name
, "__closure");
5325 /* In calls to C++ non-static member functions check the this
5326 pointer regardless of whether the function is declared with
5327 attribute nonnull. */
5330 check_function_arguments_recurse (check_nonnull_arg
, &ctx
, argarray
[0],
5334 tree attrs
= lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx
.fntype
));
5335 if (attrs
== NULL_TREE
)
5336 return ctx
.warned_p
;
5339 /* See if any of the nonnull attributes has no arguments. If so,
5340 then every pointer argument is checked (in which case the check
5341 for pointer type is done in check_nonnull_arg). */
5342 if (TREE_VALUE (a
) != NULL_TREE
)
5344 a
= lookup_attribute ("nonnull", TREE_CHAIN (a
));
5345 while (a
!= NULL_TREE
&& TREE_VALUE (a
) != NULL_TREE
);
5348 for (int i
= firstarg
; i
< nargs
; i
++)
5349 check_function_arguments_recurse (check_nonnull_arg
, &ctx
, argarray
[i
],
5353 /* Walk the argument list. If we encounter an argument number we
5354 should check for non-null, do it. */
5355 for (int i
= firstarg
; i
< nargs
; i
++)
5357 for (a
= attrs
; ; a
= TREE_CHAIN (a
))
5359 a
= lookup_attribute ("nonnull", a
);
5360 if (a
== NULL_TREE
|| nonnull_check_p (TREE_VALUE (a
), i
+ 1))
5365 check_function_arguments_recurse (check_nonnull_arg
, &ctx
,
5366 argarray
[i
], i
+ 1);
5369 return ctx
.warned_p
;
5372 /* Check that the Nth argument of a function call (counting backwards
5373 from the end) is a (pointer)0. The NARGS arguments are passed in the
5377 check_function_sentinel (const_tree fntype
, int nargs
, tree
*argarray
)
5379 tree attr
= lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype
));
5386 function_args_iterator iter
;
5389 /* Skip over the named arguments. */
5390 FOREACH_FUNCTION_ARGS (fntype
, t
, iter
)
5397 if (TREE_VALUE (attr
))
5399 tree p
= TREE_VALUE (TREE_VALUE (attr
));
5400 pos
= TREE_INT_CST_LOW (p
);
5403 /* The sentinel must be one of the varargs, i.e.
5404 in position >= the number of fixed arguments. */
5405 if ((nargs
- 1 - pos
) < len
)
5407 warning (OPT_Wformat_
,
5408 "not enough variable arguments to fit a sentinel");
5412 /* Validate the sentinel. */
5413 sentinel
= fold_for_warn (argarray
[nargs
- 1 - pos
]);
5414 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel
))
5415 || !integer_zerop (sentinel
))
5416 /* Although __null (in C++) is only an integer we allow it
5417 nevertheless, as we are guaranteed that it's exactly
5418 as wide as a pointer, and we don't want to force
5419 users to cast the NULL they have written there.
5420 We warn with -Wstrict-null-sentinel, though. */
5421 && (warn_strict_null_sentinel
|| null_node
!= sentinel
))
5422 warning (OPT_Wformat_
, "missing sentinel in function call");
5426 /* Check that the same argument isn't passed to two or more
5427 restrict-qualified formal and issue a -Wrestrict warning
5428 if it is. Return true if a warning has been issued. */
5431 check_function_restrict (const_tree fndecl
, const_tree fntype
,
5432 int nargs
, tree
*unfolded_argarray
)
5435 tree parms
= TYPE_ARG_TYPES (fntype
);
5437 /* Call fold_for_warn on all of the arguments. */
5438 auto_vec
<tree
> argarray (nargs
);
5439 for (i
= 0; i
< nargs
; i
++)
5440 argarray
.quick_push (fold_for_warn (unfolded_argarray
[i
]));
5443 && TREE_CODE (fndecl
) == FUNCTION_DECL
)
5445 /* Avoid diagnosing calls built-ins with a zero size/bound
5446 here. They are checked in more detail elsewhere. */
5447 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
5449 && TREE_CODE (argarray
[2]) == INTEGER_CST
5450 && integer_zerop (argarray
[2]))
5453 if (DECL_ARGUMENTS (fndecl
))
5454 parms
= DECL_ARGUMENTS (fndecl
);
5457 for (i
= 0; i
< nargs
; i
++)
5458 TREE_VISITED (argarray
[i
]) = 0;
5460 bool warned
= false;
5462 for (i
= 0; i
< nargs
&& parms
&& parms
!= void_list_node
; i
++)
5465 if (TREE_CODE (parms
) == PARM_DECL
)
5467 type
= TREE_TYPE (parms
);
5468 parms
= DECL_CHAIN (parms
);
5472 type
= TREE_VALUE (parms
);
5473 parms
= TREE_CHAIN (parms
);
5475 if (POINTER_TYPE_P (type
)
5476 && TYPE_RESTRICT (type
)
5477 && !TYPE_READONLY (TREE_TYPE (type
)))
5478 warned
|= warn_for_restrict (i
, argarray
.address (), nargs
);
5481 for (i
= 0; i
< nargs
; i
++)
5482 TREE_VISITED (argarray
[i
]) = 0;
5487 /* Helper for check_function_nonnull; given a list of operands which
5488 must be non-null in ARGS, determine if operand PARAM_NUM should be
5492 nonnull_check_p (tree args
, unsigned HOST_WIDE_INT param_num
)
5494 unsigned HOST_WIDE_INT arg_num
= 0;
5496 for (; args
; args
= TREE_CHAIN (args
))
5498 bool found
= get_attribute_operand (TREE_VALUE (args
), &arg_num
);
5502 if (arg_num
== param_num
)
5508 /* Check that the function argument PARAM (which is operand number
5509 PARAM_NUM) is non-null. This is called by check_function_nonnull
5510 via check_function_arguments_recurse. */
5513 check_nonnull_arg (void *ctx
, tree param
, unsigned HOST_WIDE_INT param_num
)
5515 struct nonnull_arg_ctx
*pctx
= (struct nonnull_arg_ctx
*) ctx
;
5517 /* Just skip checking the argument if it's not a pointer. This can
5518 happen if the "nonnull" attribute was given without an operand
5519 list (which means to check every pointer argument). */
5521 tree paramtype
= TREE_TYPE (param
);
5522 if (TREE_CODE (paramtype
) != POINTER_TYPE
5523 && TREE_CODE (paramtype
) != NULLPTR_TYPE
)
5526 /* Diagnose the simple cases of null arguments. */
5527 if (!integer_zerop (fold_for_warn (param
)))
5530 auto_diagnostic_group adg
;
5532 const location_t loc
= EXPR_LOC_OR_LOC (param
, pctx
->loc
);
5534 if (TREE_CODE (pctx
->fntype
) == METHOD_TYPE
)
5540 warned
= warning_at (loc
, OPT_Wnonnull
,
5541 "%qs pointer null", "this");
5542 if (warned
&& pctx
->fndecl
)
5543 inform (DECL_SOURCE_LOCATION (pctx
->fndecl
),
5544 "in a call to non-static member function %qD",
5549 warned
= warning_at (loc
, OPT_Wnonnull
,
5550 "argument %u null where non-null expected",
5551 (unsigned) param_num
);
5552 if (warned
&& pctx
->fndecl
)
5553 inform (DECL_SOURCE_LOCATION (pctx
->fndecl
),
5554 "in a call to function %qD declared %qs",
5555 pctx
->fndecl
, "nonnull");
5559 pctx
->warned_p
= true;
5562 /* Helper for attribute handling; fetch the operand number from
5563 the attribute argument list. */
5566 get_attribute_operand (tree arg_num_expr
, unsigned HOST_WIDE_INT
*valp
)
5568 /* Verify the arg number is a small constant. */
5569 if (tree_fits_uhwi_p (arg_num_expr
))
5571 *valp
= tree_to_uhwi (arg_num_expr
);
5578 /* Arguments being collected for optimization. */
5579 typedef const char *const_char_p
; /* For DEF_VEC_P. */
5580 static GTY(()) vec
<const_char_p
, va_gc
> *optimize_args
;
5583 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5584 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5585 false for #pragma GCC optimize. */
5588 parse_optimize_options (tree args
, bool attr_p
)
5593 const char **opt_argv
;
5594 struct cl_decoded_option
*decoded_options
;
5595 unsigned int decoded_options_count
;
5598 /* Build up argv vector. Just in case the string is stored away, use garbage
5599 collected strings. */
5600 vec_safe_truncate (optimize_args
, 0);
5601 vec_safe_push (optimize_args
, (const char *) NULL
);
5603 for (ap
= args
; ap
!= NULL_TREE
; ap
= TREE_CHAIN (ap
))
5605 tree value
= TREE_VALUE (ap
);
5607 if (TREE_CODE (value
) == INTEGER_CST
)
5610 sprintf (buffer
, "-O%ld", (long) TREE_INT_CST_LOW (value
));
5611 vec_safe_push (optimize_args
, ggc_strdup (buffer
));
5614 else if (TREE_CODE (value
) == STRING_CST
)
5616 /* Split string into multiple substrings. */
5617 size_t len
= TREE_STRING_LENGTH (value
);
5618 char *p
= ASTRDUP (TREE_STRING_POINTER (value
));
5619 char *end
= p
+ len
;
5623 while (next_p
!= NULL
)
5629 comma
= strchr (p
, ',');
5642 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5644 if (*p
== '-' && p
[1] != 'O' && p
[1] != 'f')
5648 warning (OPT_Wattributes
,
5649 "bad option %qs to attribute %<optimize%>", p
);
5651 warning (OPT_Wpragmas
,
5652 "bad option %qs to pragma %<optimize%>", p
);
5656 /* Can't use GC memory here, see PR88007. */
5657 r
= q
= XOBNEWVEC (&opts_obstack
, char, len2
+ 3);
5663 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5664 itself is -Os, and any other switch begins with a -f. */
5665 if ((*p
>= '0' && *p
<= '9')
5666 || (p
[0] == 's' && p
[1] == '\0'))
5672 memcpy (r
, p
, len2
);
5674 vec_safe_push (optimize_args
, (const char *) q
);
5680 opt_argc
= optimize_args
->length ();
5681 opt_argv
= (const char **) alloca (sizeof (char *) * (opt_argc
+ 1));
5683 for (i
= 1; i
< opt_argc
; i
++)
5684 opt_argv
[i
] = (*optimize_args
)[i
];
5686 /* Now parse the options. */
5687 decode_cmdline_options_to_array_default_mask (opt_argc
, opt_argv
,
5689 &decoded_options_count
);
5690 /* Drop non-Optimization options. */
5692 for (i
= 1; i
< decoded_options_count
; ++i
)
5694 if (! (cl_options
[decoded_options
[i
].opt_index
].flags
& CL_OPTIMIZATION
))
5698 warning (OPT_Wattributes
,
5699 "bad option %qs to attribute %<optimize%>",
5700 decoded_options
[i
].orig_option_with_args_text
);
5702 warning (OPT_Wpragmas
,
5703 "bad option %qs to pragma %<optimize%>",
5704 decoded_options
[i
].orig_option_with_args_text
);
5708 decoded_options
[j
] = decoded_options
[i
];
5711 decoded_options_count
= j
;
5712 /* And apply them. */
5713 decode_options (&global_options
, &global_options_set
,
5714 decoded_options
, decoded_options_count
,
5715 input_location
, global_dc
, NULL
);
5717 targetm
.override_options_after_change();
5719 optimize_args
->truncate (0);
5723 /* Check whether ATTR is a valid attribute fallthrough. */
5726 attribute_fallthrough_p (tree attr
)
5728 if (attr
== error_mark_node
)
5730 tree t
= lookup_attribute ("fallthrough", attr
);
5733 /* This attribute shall appear at most once in each attribute-list. */
5734 if (lookup_attribute ("fallthrough", TREE_CHAIN (t
)))
5735 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified multiple "
5737 /* No attribute-argument-clause shall be present. */
5738 else if (TREE_VALUE (t
) != NULL_TREE
)
5739 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified with "
5741 /* Warn if other attributes are found. */
5742 for (t
= attr
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
5744 tree name
= get_attribute_name (t
);
5745 if (!is_attribute_p ("fallthrough", name
))
5747 if (!c_dialect_cxx () && get_attribute_namespace (t
) == NULL_TREE
)
5748 /* The specifications of standard attributes in C mean
5749 this is a constraint violation. */
5750 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored",
5751 get_attribute_name (t
));
5753 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
5760 /* Check for valid arguments being passed to a function with FNTYPE.
5761 There are NARGS arguments in the array ARGARRAY. LOC should be used
5762 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5765 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5766 to preserve location wrappers); checks that require folded arguments
5767 should call fold_for_warn on them. */
5770 check_function_arguments (location_t loc
, const_tree fndecl
, const_tree fntype
,
5771 int nargs
, tree
*argarray
, vec
<location_t
> *arglocs
)
5773 bool warned_p
= false;
5775 /* Check for null being passed in a pointer argument that must be
5776 non-null. In C++, this includes the this pointer. We also need
5777 to do this if format checking is enabled. */
5780 nonnull_arg_ctx ctx
= { loc
, fndecl
, fntype
, false };
5781 warned_p
= check_function_nonnull (ctx
, nargs
, argarray
);
5784 /* Check for errors in format strings. */
5786 if (warn_format
|| warn_suggest_attribute_format
)
5787 check_function_format (fntype
, TYPE_ATTRIBUTES (fntype
), nargs
, argarray
,
5791 check_function_sentinel (fntype
, nargs
, argarray
);
5793 if (fndecl
&& fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
5795 switch (DECL_FUNCTION_CODE (fndecl
))
5797 case BUILT_IN_SPRINTF
:
5798 case BUILT_IN_SPRINTF_CHK
:
5799 case BUILT_IN_SNPRINTF
:
5800 case BUILT_IN_SNPRINTF_CHK
:
5801 /* Let the sprintf pass handle these. */
5809 /* check_function_restrict sets the DECL_READ_P for arguments
5810 so it must be called unconditionally. */
5811 warned_p
|= check_function_restrict (fndecl
, fntype
, nargs
, argarray
);
5816 /* Generic argument checking recursion routine. PARAM is the argument to
5817 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5818 once the argument is resolved. CTX is context for the callback. */
5820 check_function_arguments_recurse (void (*callback
)
5821 (void *, tree
, unsigned HOST_WIDE_INT
),
5822 void *ctx
, tree param
,
5823 unsigned HOST_WIDE_INT param_num
)
5825 if (TREE_NO_WARNING (param
))
5828 if (CONVERT_EXPR_P (param
)
5829 && (TYPE_PRECISION (TREE_TYPE (param
))
5830 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param
, 0)))))
5832 /* Strip coercion. */
5833 check_function_arguments_recurse (callback
, ctx
,
5834 TREE_OPERAND (param
, 0), param_num
);
5838 if (TREE_CODE (param
) == CALL_EXPR
&& CALL_EXPR_FN (param
))
5840 tree type
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param
)));
5842 bool found_format_arg
= false;
5844 /* See if this is a call to a known internationalization function
5845 that modifies a format arg. Such a function may have multiple
5846 format_arg attributes (for example, ngettext). */
5848 for (attrs
= TYPE_ATTRIBUTES (type
);
5850 attrs
= TREE_CHAIN (attrs
))
5851 if (is_attribute_p ("format_arg", get_attribute_name (attrs
)))
5854 tree format_num_expr
;
5857 call_expr_arg_iterator iter
;
5859 /* Extract the argument number, which was previously checked
5861 format_num_expr
= TREE_VALUE (TREE_VALUE (attrs
));
5863 format_num
= tree_to_uhwi (format_num_expr
);
5865 for (inner_arg
= first_call_expr_arg (param
, &iter
), i
= 1;
5866 inner_arg
!= NULL_TREE
;
5867 inner_arg
= next_call_expr_arg (&iter
), i
++)
5868 if (i
== format_num
)
5870 check_function_arguments_recurse (callback
, ctx
,
5871 inner_arg
, param_num
);
5872 found_format_arg
= true;
5877 /* If we found a format_arg attribute and did a recursive check,
5878 we are done with checking this argument. Otherwise, we continue
5879 and this will be considered a non-literal. */
5880 if (found_format_arg
)
5884 if (TREE_CODE (param
) == COND_EXPR
)
5886 /* Simplify to avoid warning for an impossible case. */
5887 param
= fold_for_warn (param
);
5888 if (TREE_CODE (param
) == COND_EXPR
)
5890 /* Check both halves of the conditional expression. */
5891 check_function_arguments_recurse (callback
, ctx
,
5892 TREE_OPERAND (param
, 1),
5894 check_function_arguments_recurse (callback
, ctx
,
5895 TREE_OPERAND (param
, 2),
5901 (*callback
) (ctx
, param
, param_num
);
5904 /* Checks for a builtin function FNDECL that the number of arguments
5905 NARGS against the required number REQUIRED and issues an error if
5906 there is a mismatch. Returns true if the number of arguments is
5907 correct, otherwise false. LOC is the location of FNDECL. */
5910 builtin_function_validate_nargs (location_t loc
, tree fndecl
, int nargs
,
5913 if (nargs
< required
)
5915 error_at (loc
, "too few arguments to function %qE", fndecl
);
5918 else if (nargs
> required
)
5920 error_at (loc
, "too many arguments to function %qE", fndecl
);
5926 /* Helper macro for check_builtin_function_arguments. */
5927 #define ARG_LOCATION(N) \
5928 (arg_loc.is_empty () \
5929 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5930 : expansion_point_location (arg_loc[(N)]))
5932 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5933 Returns false if there was an error, otherwise true. LOC is the
5934 location of the function; ARG_LOC is a vector of locations of the
5935 arguments. If FNDECL is the result of resolving an overloaded
5936 target built-in, ORIG_FNDECL is the original function decl,
5937 otherwise it is null. */
5940 check_builtin_function_arguments (location_t loc
, vec
<location_t
> arg_loc
,
5941 tree fndecl
, tree orig_fndecl
,
5942 int nargs
, tree
*args
)
5944 if (!fndecl_built_in_p (fndecl
))
5947 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5948 return (!targetm
.check_builtin_call
5949 || targetm
.check_builtin_call (loc
, arg_loc
, fndecl
,
5950 orig_fndecl
, nargs
, args
));
5952 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_FRONTEND
)
5955 gcc_assert (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
);
5956 switch (DECL_FUNCTION_CODE (fndecl
))
5958 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
:
5959 if (!tree_fits_uhwi_p (args
[2]))
5961 error_at (ARG_LOCATION (2),
5962 "third argument to function %qE must be a constant integer",
5968 case BUILT_IN_ALLOCA_WITH_ALIGN
:
5970 /* Get the requested alignment (in bits) if it's a constant
5971 integer expression. */
5972 unsigned HOST_WIDE_INT align
5973 = tree_fits_uhwi_p (args
[1]) ? tree_to_uhwi (args
[1]) : 0;
5975 /* Determine if the requested alignment is a power of 2. */
5976 if ((align
& (align
- 1)))
5979 /* The maximum alignment in bits corresponding to the same
5980 maximum in bytes enforced in check_user_alignment(). */
5981 unsigned maxalign
= (UINT_MAX
>> 1) + 1;
5983 /* Reject invalid alignments. */
5984 if (align
< BITS_PER_UNIT
|| maxalign
< align
)
5986 error_at (ARG_LOCATION (1),
5987 "second argument to function %qE must be a constant "
5988 "integer power of 2 between %qi and %qu bits",
5989 fndecl
, BITS_PER_UNIT
, maxalign
);
5995 case BUILT_IN_CONSTANT_P
:
5996 return builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1);
5998 case BUILT_IN_ISFINITE
:
5999 case BUILT_IN_ISINF
:
6000 case BUILT_IN_ISINF_SIGN
:
6001 case BUILT_IN_ISNAN
:
6002 case BUILT_IN_ISNORMAL
:
6003 case BUILT_IN_SIGNBIT
:
6004 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1))
6006 if (TREE_CODE (TREE_TYPE (args
[0])) != REAL_TYPE
)
6008 error_at (ARG_LOCATION (0), "non-floating-point argument in "
6009 "call to function %qE", fndecl
);
6016 case BUILT_IN_ISGREATER
:
6017 case BUILT_IN_ISGREATEREQUAL
:
6018 case BUILT_IN_ISLESS
:
6019 case BUILT_IN_ISLESSEQUAL
:
6020 case BUILT_IN_ISLESSGREATER
:
6021 case BUILT_IN_ISUNORDERED
:
6022 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2))
6024 enum tree_code code0
, code1
;
6025 code0
= TREE_CODE (TREE_TYPE (args
[0]));
6026 code1
= TREE_CODE (TREE_TYPE (args
[1]));
6027 if (!((code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
6028 || (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
6029 || (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)))
6031 error_at (loc
, "non-floating-point arguments in call to "
6032 "function %qE", fndecl
);
6039 case BUILT_IN_FPCLASSIFY
:
6040 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 6))
6042 for (unsigned int i
= 0; i
< 5; i
++)
6043 if (TREE_CODE (args
[i
]) != INTEGER_CST
)
6045 error_at (ARG_LOCATION (i
), "non-const integer argument %u in "
6046 "call to function %qE", i
+ 1, fndecl
);
6050 if (TREE_CODE (TREE_TYPE (args
[5])) != REAL_TYPE
)
6052 error_at (ARG_LOCATION (5), "non-floating-point argument in "
6053 "call to function %qE", fndecl
);
6060 case BUILT_IN_ASSUME_ALIGNED
:
6061 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2 + (nargs
> 2)))
6063 if (nargs
>= 3 && TREE_CODE (TREE_TYPE (args
[2])) != INTEGER_TYPE
)
6065 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
6066 "function %qE", fndecl
);
6073 case BUILT_IN_ADD_OVERFLOW
:
6074 case BUILT_IN_SUB_OVERFLOW
:
6075 case BUILT_IN_MUL_OVERFLOW
:
6076 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
6079 for (i
= 0; i
< 2; i
++)
6080 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
6082 error_at (ARG_LOCATION (i
), "argument %u in call to function "
6083 "%qE does not have integral type", i
+ 1, fndecl
);
6086 if (TREE_CODE (TREE_TYPE (args
[2])) != POINTER_TYPE
6087 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args
[2]))))
6089 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6090 "does not have pointer to integral type", fndecl
);
6093 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == ENUMERAL_TYPE
)
6095 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6096 "has pointer to enumerated type", fndecl
);
6099 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == BOOLEAN_TYPE
)
6101 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6102 "has pointer to boolean type", fndecl
);
6105 else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args
[2]))))
6107 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6108 "has pointer to %<const%> type (%qT)", fndecl
,
6109 TREE_TYPE (args
[2]));
6116 case BUILT_IN_ADD_OVERFLOW_P
:
6117 case BUILT_IN_SUB_OVERFLOW_P
:
6118 case BUILT_IN_MUL_OVERFLOW_P
:
6119 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
6122 for (i
= 0; i
< 3; i
++)
6123 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
6125 error_at (ARG_LOCATION (i
), "argument %u in call to function "
6126 "%qE does not have integral type", i
+ 1, fndecl
);
6129 if (TREE_CODE (TREE_TYPE (args
[2])) == ENUMERAL_TYPE
)
6131 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6132 "%qE has enumerated type", fndecl
);
6135 else if (TREE_CODE (TREE_TYPE (args
[2])) == BOOLEAN_TYPE
)
6137 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6138 "%qE has boolean type", fndecl
);
6150 /* Subroutine of c_parse_error.
6151 Return the result of concatenating LHS and RHS. RHS is really
6152 a string literal, its first character is indicated by RHS_START and
6153 RHS_SIZE is its length (including the terminating NUL character).
6155 The caller is responsible for deleting the returned pointer. */
6158 catenate_strings (const char *lhs
, const char *rhs_start
, int rhs_size
)
6160 const size_t lhs_size
= strlen (lhs
);
6161 char *result
= XNEWVEC (char, lhs_size
+ rhs_size
);
6162 memcpy (result
, lhs
, lhs_size
);
6163 memcpy (result
+ lhs_size
, rhs_start
, rhs_size
);
6167 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6168 before TOKEN, which had the associated VALUE. */
6171 c_parse_error (const char *gmsgid
, enum cpp_ttype token_type
,
6172 tree value
, unsigned char token_flags
,
6173 rich_location
*richloc
)
6175 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6177 char *message
= NULL
;
6179 if (token_type
== CPP_EOF
)
6180 message
= catenate_messages (gmsgid
, " at end of input");
6181 else if (token_type
== CPP_CHAR
6182 || token_type
== CPP_WCHAR
6183 || token_type
== CPP_CHAR16
6184 || token_type
== CPP_CHAR32
6185 || token_type
== CPP_UTF8CHAR
)
6187 unsigned int val
= TREE_INT_CST_LOW (value
);
6209 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
6210 message
= catenate_messages (gmsgid
, " before %s'%c'");
6212 message
= catenate_messages (gmsgid
, " before %s'\\x%x'");
6214 error_at (richloc
, message
, prefix
, val
);
6218 else if (token_type
== CPP_CHAR_USERDEF
6219 || token_type
== CPP_WCHAR_USERDEF
6220 || token_type
== CPP_CHAR16_USERDEF
6221 || token_type
== CPP_CHAR32_USERDEF
6222 || token_type
== CPP_UTF8CHAR_USERDEF
)
6223 message
= catenate_messages (gmsgid
,
6224 " before user-defined character literal");
6225 else if (token_type
== CPP_STRING_USERDEF
6226 || token_type
== CPP_WSTRING_USERDEF
6227 || token_type
== CPP_STRING16_USERDEF
6228 || token_type
== CPP_STRING32_USERDEF
6229 || token_type
== CPP_UTF8STRING_USERDEF
)
6230 message
= catenate_messages (gmsgid
, " before user-defined string literal");
6231 else if (token_type
== CPP_STRING
6232 || token_type
== CPP_WSTRING
6233 || token_type
== CPP_STRING16
6234 || token_type
== CPP_STRING32
6235 || token_type
== CPP_UTF8STRING
)
6236 message
= catenate_messages (gmsgid
, " before string constant");
6237 else if (token_type
== CPP_NUMBER
)
6238 message
= catenate_messages (gmsgid
, " before numeric constant");
6239 else if (token_type
== CPP_NAME
)
6241 message
= catenate_messages (gmsgid
, " before %qE");
6242 error_at (richloc
, message
, value
);
6246 else if (token_type
== CPP_PRAGMA
)
6247 message
= catenate_messages (gmsgid
, " before %<#pragma%>");
6248 else if (token_type
== CPP_PRAGMA_EOL
)
6249 message
= catenate_messages (gmsgid
, " before end of line");
6250 else if (token_type
== CPP_DECLTYPE
)
6251 message
= catenate_messages (gmsgid
, " before %<decltype%>");
6252 else if (token_type
< N_TTYPES
)
6254 message
= catenate_messages (gmsgid
, " before %qs token");
6255 error_at (richloc
, message
, cpp_type2name (token_type
, token_flags
));
6260 error_at (richloc
, gmsgid
);
6264 error_at (richloc
, message
);
6267 #undef catenate_messages
6270 /* Return the gcc option code associated with the reason for a cpp
6271 message, or 0 if none. */
6274 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason
)
6276 const struct cpp_reason_option_codes_t
*entry
;
6278 for (entry
= cpp_reason_option_codes
; entry
->reason
!= CPP_W_NONE
; entry
++)
6280 if (entry
->reason
== reason
)
6281 return entry
->option_code
;
6286 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6287 preprocessor. The diagnostic is of type LEVEL, with REASON set
6288 to the reason code if LEVEL is represents a warning, at location
6289 RICHLOC unless this is after lexing and the compiler's location
6290 should be used instead; MSG is the translated message and AP
6291 the arguments. Returns true if a diagnostic was emitted, false
6295 c_cpp_diagnostic (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
6296 enum cpp_diagnostic_level level
,
6297 enum cpp_warning_reason reason
,
6298 rich_location
*richloc
,
6299 const char *msg
, va_list *ap
)
6301 diagnostic_info diagnostic
;
6302 diagnostic_t dlevel
;
6303 bool save_warn_system_headers
= global_dc
->dc_warn_system_headers
;
6308 case CPP_DL_WARNING_SYSHDR
:
6311 global_dc
->dc_warn_system_headers
= 1;
6313 case CPP_DL_WARNING
:
6316 dlevel
= DK_WARNING
;
6318 case CPP_DL_PEDWARN
:
6319 if (flag_no_output
&& !flag_pedantic_errors
)
6321 dlevel
= DK_PEDWARN
;
6339 richloc
->set_range (0, input_location
, SHOW_RANGE_WITH_CARET
);
6340 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
6342 diagnostic_override_option_index
6344 c_option_controlling_cpp_diagnostic (reason
));
6345 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
6346 if (level
== CPP_DL_WARNING_SYSHDR
)
6347 global_dc
->dc_warn_system_headers
= save_warn_system_headers
;
6351 /* Convert a character from the host to the target execution character
6352 set. cpplib handles this, mostly. */
6355 c_common_to_target_charset (HOST_WIDE_INT c
)
6357 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6358 zero-extended under -fno-signed-char. cpplib insists that characters
6359 and character constants are always unsigned. Hence we must convert
6361 cppchar_t uc
= ((cppchar_t
)c
) & ((((cppchar_t
)1) << CHAR_BIT
)-1);
6363 uc
= cpp_host_to_exec_charset (parse_in
, uc
);
6365 if (flag_signed_char
)
6366 return ((HOST_WIDE_INT
)uc
) << (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
)
6367 >> (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
);
6372 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6373 references with an INDIRECT_REF of a constant at the bottom; much like the
6374 traditional rendering of offsetof as a macro. TYPE is the desired type of
6375 the whole expression. Return the folded result. */
6378 fold_offsetof (tree expr
, tree type
, enum tree_code ctx
)
6381 tree_code code
= TREE_CODE (expr
);
6388 error ("cannot apply %<offsetof%> to static data member %qD", expr
);
6389 return error_mark_node
;
6393 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6394 return error_mark_node
;
6398 if (!TREE_CONSTANT (TREE_OPERAND (expr
, 0)))
6400 error ("cannot apply %<offsetof%> to a non constant address");
6401 return error_mark_node
;
6403 return convert (type
, TREE_OPERAND (expr
, 0));
6406 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6407 if (base
== error_mark_node
)
6410 t
= TREE_OPERAND (expr
, 1);
6411 if (DECL_C_BIT_FIELD (t
))
6413 error ("attempt to take address of bit-field structure "
6415 return error_mark_node
;
6417 off
= size_binop_loc (input_location
, PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
6418 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
6423 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6424 if (base
== error_mark_node
)
6427 t
= TREE_OPERAND (expr
, 1);
6428 STRIP_ANY_LOCATION_WRAPPER (t
);
6430 /* Check if the offset goes beyond the upper bound of the array. */
6431 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) >= 0)
6433 tree upbound
= array_ref_up_bound (expr
);
6434 if (upbound
!= NULL_TREE
6435 && TREE_CODE (upbound
) == INTEGER_CST
6436 && !tree_int_cst_equal (upbound
,
6437 TYPE_MAX_VALUE (TREE_TYPE (upbound
))))
6439 if (ctx
!= ARRAY_REF
&& ctx
!= COMPONENT_REF
)
6440 upbound
= size_binop (PLUS_EXPR
, upbound
,
6441 build_int_cst (TREE_TYPE (upbound
), 1));
6442 if (tree_int_cst_lt (upbound
, t
))
6446 for (v
= TREE_OPERAND (expr
, 0);
6447 TREE_CODE (v
) == COMPONENT_REF
;
6448 v
= TREE_OPERAND (v
, 0))
6449 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
6452 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
6453 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
6454 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
6460 /* Don't warn if the array might be considered a poor
6461 man's flexible array member with a very permissive
6462 definition thereof. */
6463 if (TREE_CODE (v
) == ARRAY_REF
6464 || TREE_CODE (v
) == COMPONENT_REF
)
6465 warning (OPT_Warray_bounds
,
6466 "index %E denotes an offset "
6467 "greater than size of %qT",
6468 t
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
6473 t
= convert (sizetype
, t
);
6474 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
6478 /* Handle static members of volatile structs. */
6479 t
= TREE_OPERAND (expr
, 1);
6480 gcc_checking_assert (VAR_P (get_base_address (t
)));
6481 return fold_offsetof (t
, type
);
6487 if (!POINTER_TYPE_P (type
))
6488 return size_binop (PLUS_EXPR
, base
, convert (type
, off
));
6489 return fold_build_pointer_plus (base
, off
);
6492 /* *PTYPE is an incomplete array. Complete it with a domain based on
6493 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6494 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6495 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6498 complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
6500 tree maxindex
, type
, main_type
, elt
, unqual_elt
;
6501 int failure
= 0, quals
;
6502 bool overflow_p
= false;
6504 maxindex
= size_zero_node
;
6507 STRIP_ANY_LOCATION_WRAPPER (initial_value
);
6509 if (TREE_CODE (initial_value
) == STRING_CST
)
6512 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value
)));
6513 maxindex
= size_int (TREE_STRING_LENGTH (initial_value
)/eltsize
- 1);
6515 else if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
6517 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
6519 if (vec_safe_is_empty (v
))
6523 maxindex
= ssize_int (-1);
6528 unsigned HOST_WIDE_INT cnt
;
6529 constructor_elt
*ce
;
6530 bool fold_p
= false;
6533 maxindex
= (*v
)[0].index
, fold_p
= true;
6535 curindex
= maxindex
;
6537 for (cnt
= 1; vec_safe_iterate (v
, cnt
, &ce
); cnt
++)
6539 bool curfold_p
= false;
6541 curindex
= ce
->index
, curfold_p
= true;
6546 /* Since we treat size types now as ordinary
6547 unsigned types, we need an explicit overflow
6549 tree orig
= curindex
;
6550 curindex
= fold_convert (sizetype
, curindex
);
6551 overflow_p
|= tree_int_cst_lt (curindex
, orig
);
6553 curindex
= size_binop (PLUS_EXPR
, curindex
,
6556 if (tree_int_cst_lt (maxindex
, curindex
))
6557 maxindex
= curindex
, fold_p
= curfold_p
;
6561 tree orig
= maxindex
;
6562 maxindex
= fold_convert (sizetype
, maxindex
);
6563 overflow_p
|= tree_int_cst_lt (maxindex
, orig
);
6569 /* Make an error message unless that happened already. */
6570 if (initial_value
!= error_mark_node
)
6582 elt
= TREE_TYPE (type
);
6583 quals
= TYPE_QUALS (strip_array_types (elt
));
6587 unqual_elt
= c_build_qualified_type (elt
, KEEP_QUAL_ADDR_SPACE (quals
));
6589 /* Using build_distinct_type_copy and modifying things afterward instead
6590 of using build_array_type to create a new type preserves all of the
6591 TYPE_LANG_FLAG_? bits that the front end may have set. */
6592 main_type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6593 TREE_TYPE (main_type
) = unqual_elt
;
6594 TYPE_DOMAIN (main_type
)
6595 = build_range_type (TREE_TYPE (maxindex
),
6596 build_int_cst (TREE_TYPE (maxindex
), 0), maxindex
);
6597 TYPE_TYPELESS_STORAGE (main_type
) = TYPE_TYPELESS_STORAGE (type
);
6598 layout_type (main_type
);
6600 /* Make sure we have the canonical MAIN_TYPE. */
6601 hashval_t hashcode
= type_hash_canon_hash (main_type
);
6602 main_type
= type_hash_canon (hashcode
, main_type
);
6604 /* Fix the canonical type. */
6605 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type
))
6606 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type
)))
6607 SET_TYPE_STRUCTURAL_EQUALITY (main_type
);
6608 else if (TYPE_CANONICAL (TREE_TYPE (main_type
)) != TREE_TYPE (main_type
)
6609 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type
))
6610 != TYPE_DOMAIN (main_type
)))
6611 TYPE_CANONICAL (main_type
)
6612 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type
)),
6613 TYPE_CANONICAL (TYPE_DOMAIN (main_type
)),
6614 TYPE_TYPELESS_STORAGE (main_type
));
6616 TYPE_CANONICAL (main_type
) = main_type
;
6621 type
= c_build_qualified_type (main_type
, quals
);
6623 if (COMPLETE_TYPE_P (type
)
6624 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
6625 && (overflow_p
|| TREE_OVERFLOW (TYPE_SIZE_UNIT (type
))))
6627 error ("size of array is too large");
6628 /* If we proceed with the array type as it is, we'll eventually
6629 crash in tree_to_[su]hwi(). */
6630 type
= error_mark_node
;
6637 /* INIT is an constructor of a structure with a flexible array member.
6638 Complete the flexible array member with a domain based on it's value. */
6640 complete_flexible_array_elts (tree init
)
6644 if (init
== NULL_TREE
|| TREE_CODE (init
) != CONSTRUCTOR
)
6647 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
6650 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
6651 type
= TREE_TYPE (elt
);
6652 if (TREE_CODE (type
) == ARRAY_TYPE
6653 && TYPE_SIZE (type
) == NULL_TREE
)
6654 complete_array_type (&TREE_TYPE (elt
), elt
, false);
6656 complete_flexible_array_elts (elt
);
6659 /* Like c_mark_addressable but don't check register qualifier. */
6661 c_common_mark_addressable_vec (tree t
)
6663 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
6664 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
6665 while (handled_component_p (t
))
6666 t
= TREE_OPERAND (t
, 0);
6668 && TREE_CODE (t
) != PARM_DECL
6669 && TREE_CODE (t
) != COMPOUND_LITERAL_EXPR
)
6671 if (!VAR_P (t
) || !DECL_HARD_REGISTER (t
))
6672 TREE_ADDRESSABLE (t
) = 1;
6673 if (TREE_CODE (t
) == COMPOUND_LITERAL_EXPR
)
6674 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t
)) = 1;
6679 /* Used to help initialize the builtin-types.def table. When a type of
6680 the correct size doesn't exist, use error_mark_node instead of NULL.
6681 The later results in segfaults even when a decl using the type doesn't
6685 builtin_type_for_size (int size
, bool unsignedp
)
6687 tree type
= c_common_type_for_size (size
, unsignedp
);
6688 return type
? type
: error_mark_node
;
6691 /* Work out the size of the first argument of a call to
6692 __builtin_speculation_safe_value. Only pointers and integral types
6693 are permitted. Return -1 if the argument type is not supported or
6694 the size is too large; 0 if the argument type is a pointer or the
6695 size if it is integral. */
6696 static enum built_in_function
6697 speculation_safe_value_resolve_call (tree function
, vec
<tree
, va_gc
> *params
)
6699 /* Type of the argument. */
6703 if (vec_safe_is_empty (params
))
6705 error ("too few arguments to function %qE", function
);
6706 return BUILT_IN_NONE
;
6709 type
= TREE_TYPE ((*params
)[0]);
6710 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6712 /* Force array-to-pointer decay for C++. */
6713 (*params
)[0] = default_conversion ((*params
)[0]);
6714 type
= TREE_TYPE ((*params
)[0]);
6717 if (POINTER_TYPE_P (type
))
6718 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR
;
6720 if (!INTEGRAL_TYPE_P (type
))
6723 if (!COMPLETE_TYPE_P (type
))
6726 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6727 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6728 return ((enum built_in_function
)
6729 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1
+ exact_log2 (size
)));
6732 /* Issue the diagnostic only if the argument is valid, otherwise
6733 it would be redundant at best and could be misleading. */
6734 if (type
!= error_mark_node
)
6735 error ("operand type %qT is incompatible with argument %d of %qE",
6738 return BUILT_IN_NONE
;
6741 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6742 the prototype for FUNCTION. The first argument is mandatory, a second
6743 argument, if present, must be type compatible with the first. */
6745 speculation_safe_value_resolve_params (location_t loc
, tree orig_function
,
6746 vec
<tree
, va_gc
> *params
)
6750 if (params
->length () == 0)
6752 error_at (loc
, "too few arguments to function %qE", orig_function
);
6756 else if (params
->length () > 2)
6758 error_at (loc
, "too many arguments to function %qE", orig_function
);
6763 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
)
6764 val
= default_conversion (val
);
6765 if (!(TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
6766 || TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
))
6769 "expecting argument of type pointer or of type integer "
6775 if (params
->length () == 2)
6777 tree val2
= (*params
)[1];
6778 if (TREE_CODE (TREE_TYPE (val2
)) == ARRAY_TYPE
)
6779 val2
= default_conversion (val2
);
6780 if (error_operand_p (val2
))
6782 if (!(TREE_TYPE (val
) == TREE_TYPE (val2
)
6783 || useless_type_conversion_p (TREE_TYPE (val
), TREE_TYPE (val2
))))
6785 error_at (loc
, "both arguments must be compatible");
6788 (*params
)[1] = val2
;
6794 /* Cast the result of the builtin back to the type of the first argument,
6795 preserving any qualifiers that it might have. */
6797 speculation_safe_value_resolve_return (tree first_param
, tree result
)
6799 tree ptype
= TREE_TYPE (first_param
);
6800 tree rtype
= TREE_TYPE (result
);
6801 ptype
= TYPE_MAIN_VARIANT (ptype
);
6803 if (tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6804 return convert (ptype
, result
);
6809 /* A helper function for resolve_overloaded_builtin in resolving the
6810 overloaded __sync_ builtins. Returns a positive power of 2 if the
6811 first operand of PARAMS is a pointer to a supported data type.
6812 Returns 0 if an error is encountered.
6813 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6817 sync_resolve_size (tree function
, vec
<tree
, va_gc
> *params
, bool fetch
)
6819 /* Type of the argument. */
6821 /* Type the argument points to. */
6825 if (vec_safe_is_empty (params
))
6827 error ("too few arguments to function %qE", function
);
6831 argtype
= type
= TREE_TYPE ((*params
)[0]);
6832 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6834 /* Force array-to-pointer decay for C++. */
6835 (*params
)[0] = default_conversion ((*params
)[0]);
6836 type
= TREE_TYPE ((*params
)[0]);
6838 if (TREE_CODE (type
) != POINTER_TYPE
)
6841 type
= TREE_TYPE (type
);
6842 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
6845 if (!COMPLETE_TYPE_P (type
))
6848 if (fetch
&& TREE_CODE (type
) == BOOLEAN_TYPE
)
6851 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6852 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6856 /* Issue the diagnostic only if the argument is valid, otherwise
6857 it would be redundant at best and could be misleading. */
6858 if (argtype
!= error_mark_node
)
6859 error ("operand type %qT is incompatible with argument %d of %qE",
6860 argtype
, 1, function
);
6864 /* A helper function for resolve_overloaded_builtin. Adds casts to
6865 PARAMS to make arguments match up with those of FUNCTION. Drops
6866 the variadic arguments at the end. Returns false if some error
6867 was encountered; true on success. */
6870 sync_resolve_params (location_t loc
, tree orig_function
, tree function
,
6871 vec
<tree
, va_gc
> *params
, bool orig_format
)
6873 function_args_iterator iter
;
6875 unsigned int parmnum
;
6877 function_args_iter_init (&iter
, TREE_TYPE (function
));
6878 /* We've declared the implementation functions to use "volatile void *"
6879 as the pointer parameter, so we shouldn't get any complaints from the
6880 call to check_function_arguments what ever type the user used. */
6881 function_args_iter_next (&iter
);
6882 ptype
= TREE_TYPE (TREE_TYPE ((*params
)[0]));
6883 ptype
= TYPE_MAIN_VARIANT (ptype
);
6885 /* For the rest of the values, we need to cast these to FTYPE, so that we
6886 don't get warnings for passing pointer types, etc. */
6892 arg_type
= function_args_iter_cond (&iter
);
6893 /* XXX void_type_node belies the abstraction. */
6894 if (arg_type
== void_type_node
)
6898 if (params
->length () <= parmnum
)
6900 error_at (loc
, "too few arguments to function %qE", orig_function
);
6904 /* Only convert parameters if arg_type is unsigned integer type with
6905 new format sync routines, i.e. don't attempt to convert pointer
6906 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6907 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6909 if (TREE_CODE (arg_type
) == INTEGER_TYPE
&& TYPE_UNSIGNED (arg_type
))
6911 /* Ideally for the first conversion we'd use convert_for_assignment
6912 so that we get warnings for anything that doesn't match the pointer
6913 type. This isn't portable across the C and C++ front ends atm. */
6914 val
= (*params
)[parmnum
];
6915 val
= convert (ptype
, val
);
6916 val
= convert (arg_type
, val
);
6917 (*params
)[parmnum
] = val
;
6920 function_args_iter_next (&iter
);
6923 /* __atomic routines are not variadic. */
6924 if (!orig_format
&& params
->length () != parmnum
+ 1)
6926 error_at (loc
, "too many arguments to function %qE", orig_function
);
6930 /* The definition of these primitives is variadic, with the remaining
6931 being "an optional list of variables protected by the memory barrier".
6932 No clue what that's supposed to mean, precisely, but we consider all
6933 call-clobbered variables to be protected so we're safe. */
6934 params
->truncate (parmnum
+ 1);
6939 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6940 RESULT to make it match the type of the first pointer argument in
6944 sync_resolve_return (tree first_param
, tree result
, bool orig_format
)
6946 tree ptype
= TREE_TYPE (TREE_TYPE (first_param
));
6947 tree rtype
= TREE_TYPE (result
);
6948 ptype
= TYPE_MAIN_VARIANT (ptype
);
6950 /* New format doesn't require casting unless the types are the same size. */
6951 if (orig_format
|| tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6952 return convert (ptype
, result
);
6957 /* This function verifies the PARAMS to generic atomic FUNCTION.
6958 It returns the size if all the parameters are the same size, otherwise
6959 0 is returned if the parameters are invalid. */
6962 get_atomic_generic_size (location_t loc
, tree function
,
6963 vec
<tree
, va_gc
> *params
)
6965 unsigned int n_param
;
6966 unsigned int n_model
;
6967 unsigned int outputs
= 0; // bitset of output parameters
6972 /* Determine the parameter makeup. */
6973 switch (DECL_FUNCTION_CODE (function
))
6975 case BUILT_IN_ATOMIC_EXCHANGE
:
6980 case BUILT_IN_ATOMIC_LOAD
:
6985 case BUILT_IN_ATOMIC_STORE
:
6990 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
6999 if (vec_safe_length (params
) != n_param
)
7001 error_at (loc
, "incorrect number of arguments to function %qE", function
);
7005 /* Get type of first parameter, and determine its size. */
7006 type_0
= TREE_TYPE ((*params
)[0]);
7007 if (TREE_CODE (type_0
) == ARRAY_TYPE
&& c_dialect_cxx ())
7009 /* Force array-to-pointer decay for C++. */
7010 (*params
)[0] = default_conversion ((*params
)[0]);
7011 type_0
= TREE_TYPE ((*params
)[0]);
7013 if (TREE_CODE (type_0
) != POINTER_TYPE
|| VOID_TYPE_P (TREE_TYPE (type_0
)))
7015 error_at (loc
, "argument 1 of %qE must be a non-void pointer type",
7020 if (!COMPLETE_TYPE_P (TREE_TYPE (type_0
)))
7022 error_at (loc
, "argument 1 of %qE must be a pointer to a complete type",
7027 /* Types must be compile time constant sizes. */
7028 if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0
)))))
7031 "argument 1 of %qE must be a pointer to a constant size type",
7036 size_0
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0
)));
7038 /* Zero size objects are not allowed. */
7042 "argument 1 of %qE must be a pointer to a nonzero size object",
7047 /* Check each other parameter is a pointer and the same size. */
7048 for (x
= 0; x
< n_param
- n_model
; x
++)
7051 tree type
= TREE_TYPE ((*params
)[x
]);
7052 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
7053 if (n_param
== 6 && x
== 3)
7055 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
7057 /* Force array-to-pointer decay for C++. */
7058 (*params
)[x
] = default_conversion ((*params
)[x
]);
7059 type
= TREE_TYPE ((*params
)[x
]);
7061 if (!POINTER_TYPE_P (type
))
7063 error_at (loc
, "argument %d of %qE must be a pointer type", x
+ 1,
7067 else if (TYPE_SIZE_UNIT (TREE_TYPE (type
))
7068 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type
))))
7071 error_at (loc
, "argument %d of %qE must be a pointer to a constant "
7072 "size type", x
+ 1, function
);
7075 else if (FUNCTION_POINTER_TYPE_P (type
))
7077 error_at (loc
, "argument %d of %qE must not be a pointer to a "
7078 "function", x
+ 1, function
);
7081 tree type_size
= TYPE_SIZE_UNIT (TREE_TYPE (type
));
7082 size
= type_size
? tree_to_uhwi (type_size
) : 0;
7085 error_at (loc
, "size mismatch in argument %d of %qE", x
+ 1,
7091 auto_diagnostic_group d
;
7092 int quals
= TYPE_QUALS (TREE_TYPE (type
));
7093 /* Must not write to an argument of a const-qualified type. */
7094 if (outputs
& (1 << x
) && quals
& TYPE_QUAL_CONST
)
7096 if (c_dialect_cxx ())
7098 error_at (loc
, "argument %d of %qE must not be a pointer to "
7099 "a %<const%> type", x
+ 1, function
);
7103 pedwarn (loc
, OPT_Wincompatible_pointer_types
, "argument %d "
7104 "of %qE discards %<const%> qualifier", x
+ 1,
7107 /* Only the first argument is allowed to be volatile. */
7108 if (x
> 0 && quals
& TYPE_QUAL_VOLATILE
)
7110 if (c_dialect_cxx ())
7112 error_at (loc
, "argument %d of %qE must not be a pointer to "
7113 "a %<volatile%> type", x
+ 1, function
);
7117 pedwarn (loc
, OPT_Wincompatible_pointer_types
, "argument %d "
7118 "of %qE discards %<volatile%> qualifier", x
+ 1,
7124 /* Check memory model parameters for validity. */
7125 for (x
= n_param
- n_model
; x
< n_param
; x
++)
7127 tree p
= (*params
)[x
];
7128 if (!INTEGRAL_TYPE_P (TREE_TYPE (p
)))
7130 error_at (loc
, "non-integer memory model argument %d of %qE", x
+ 1,
7134 p
= fold_for_warn (p
);
7135 if (TREE_CODE (p
) == INTEGER_CST
)
7137 /* memmodel_base masks the low 16 bits, thus ignore any bits above
7138 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7139 bits will be checked later during expansion in target specific
7141 if (memmodel_base (TREE_INT_CST_LOW (p
)) >= MEMMODEL_LAST
)
7142 warning_at (loc
, OPT_Winvalid_memory_model
,
7143 "invalid memory model argument %d of %qE", x
+ 1,
7152 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
7153 at the beginning of the parameter list PARAMS representing the size of the
7154 objects. This is to match the library ABI requirement. LOC is the location
7155 of the function call.
7156 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
7157 returned to allow the external call to be constructed. */
7160 add_atomic_size_parameter (unsigned n
, location_t loc
, tree function
,
7161 vec
<tree
, va_gc
> *params
)
7165 /* Insert a SIZE_T parameter as the first param. If there isn't
7166 enough space, allocate a new vector and recursively re-build with that. */
7167 if (!params
->space (1))
7169 unsigned int z
, len
;
7170 vec
<tree
, va_gc
> *v
;
7173 len
= params
->length ();
7174 vec_alloc (v
, len
+ 1);
7175 v
->quick_push (build_int_cst (size_type_node
, n
));
7176 for (z
= 0; z
< len
; z
++)
7177 v
->quick_push ((*params
)[z
]);
7178 f
= build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
7183 /* Add the size parameter and leave as a function call for processing. */
7184 size_node
= build_int_cst (size_type_node
, n
);
7185 params
->quick_insert (0, size_node
);
7190 /* Return whether atomic operations for naturally aligned N-byte
7191 arguments are supported, whether inline or through libatomic. */
7193 atomic_size_supported_p (int n
)
7204 return targetm
.scalar_mode_supported_p (TImode
);
7211 /* This will process an __atomic_exchange function call, determine whether it
7212 needs to be mapped to the _N variation, or turned into a library call.
7213 LOC is the location of the builtin call.
7214 FUNCTION is the DECL that has been invoked;
7215 PARAMS is the argument list for the call. The return value is non-null
7216 TRUE is returned if it is translated into the proper format for a call to the
7217 external library, and NEW_RETURN is set the tree for that function.
7218 FALSE is returned if processing for the _N variation is required, and
7219 NEW_RETURN is set to the return value the result is copied into. */
7221 resolve_overloaded_atomic_exchange (location_t loc
, tree function
,
7222 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7224 tree p0
, p1
, p2
, p3
;
7225 tree I_type
, I_type_ptr
;
7226 int n
= get_atomic_generic_size (loc
, function
, params
);
7228 /* Size of 0 is an error condition. */
7231 *new_return
= error_mark_node
;
7235 /* If not a lock-free size, change to the library generic format. */
7236 if (!atomic_size_supported_p (n
))
7238 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7242 /* Otherwise there is a lockfree match, transform the call from:
7243 void fn(T* mem, T* desired, T* return, model)
7245 *return = (T) (fn (In* mem, (In) *desired, model)) */
7252 /* Create pointer to appropriate size. */
7253 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7254 I_type_ptr
= build_pointer_type (I_type
);
7256 /* Convert object pointer to required type. */
7257 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7259 /* Convert new value to required type, and dereference it. */
7260 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7261 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7264 /* Move memory model to the 3rd position, and end param list. */
7266 params
->truncate (3);
7268 /* Convert return pointer and dereference it for later assignment. */
7269 *new_return
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7275 /* This will process an __atomic_compare_exchange function call, determine
7276 whether it needs to be mapped to the _N variation, or turned into a lib call.
7277 LOC is the location of the builtin call.
7278 FUNCTION is the DECL that has been invoked;
7279 PARAMS is the argument list for the call. The return value is non-null
7280 TRUE is returned if it is translated into the proper format for a call to the
7281 external library, and NEW_RETURN is set the tree for that function.
7282 FALSE is returned if processing for the _N variation is required. */
7285 resolve_overloaded_atomic_compare_exchange (location_t loc
, tree function
,
7286 vec
<tree
, va_gc
> *params
,
7290 tree I_type
, I_type_ptr
;
7291 int n
= get_atomic_generic_size (loc
, function
, params
);
7293 /* Size of 0 is an error condition. */
7296 *new_return
= error_mark_node
;
7300 /* If not a lock-free size, change to the library generic format. */
7301 if (!atomic_size_supported_p (n
))
7303 /* The library generic format does not have the weak parameter, so
7304 remove it from the param list. Since a parameter has been removed,
7305 we can be sure that there is room for the SIZE_T parameter, meaning
7306 there will not be a recursive rebuilding of the parameter list, so
7307 there is no danger this will be done twice. */
7310 (*params
)[3] = (*params
)[4];
7311 (*params
)[4] = (*params
)[5];
7312 params
->truncate (5);
7314 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7318 /* Otherwise, there is a match, so the call needs to be transformed from:
7319 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7321 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7327 /* Create pointer to appropriate size. */
7328 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7329 I_type_ptr
= build_pointer_type (I_type
);
7331 /* Convert object pointer to required type. */
7332 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7335 /* Convert expected pointer to required type. */
7336 p1
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p1
);
7339 /* Convert desired value to required type, and dereference it. */
7340 p2
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7341 p2
= build1 (VIEW_CONVERT_EXPR
, I_type
, p2
);
7344 /* The rest of the parameters are fine. NULL means no special return value
7351 /* This will process an __atomic_load function call, determine whether it
7352 needs to be mapped to the _N variation, or turned into a library call.
7353 LOC is the location of the builtin call.
7354 FUNCTION is the DECL that has been invoked;
7355 PARAMS is the argument list for the call. The return value is non-null
7356 TRUE is returned if it is translated into the proper format for a call to the
7357 external library, and NEW_RETURN is set the tree for that function.
7358 FALSE is returned if processing for the _N variation is required, and
7359 NEW_RETURN is set to the return value the result is copied into. */
7362 resolve_overloaded_atomic_load (location_t loc
, tree function
,
7363 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7366 tree I_type
, I_type_ptr
;
7367 int n
= get_atomic_generic_size (loc
, function
, params
);
7369 /* Size of 0 is an error condition. */
7372 *new_return
= error_mark_node
;
7376 /* If not a lock-free size, change to the library generic format. */
7377 if (!atomic_size_supported_p (n
))
7379 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7383 /* Otherwise, there is a match, so the call needs to be transformed from:
7384 void fn(T* mem, T* return, model)
7386 *return = (T) (fn ((In *) mem, model)) */
7392 /* Create pointer to appropriate size. */
7393 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7394 I_type_ptr
= build_pointer_type (I_type
);
7396 /* Convert object pointer to required type. */
7397 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7400 /* Move memory model to the 2nd position, and end param list. */
7402 params
->truncate (2);
7404 /* Convert return pointer and dereference it for later assignment. */
7405 *new_return
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7411 /* This will process an __atomic_store function call, determine whether it
7412 needs to be mapped to the _N variation, or turned into a library call.
7413 LOC is the location of the builtin call.
7414 FUNCTION is the DECL that has been invoked;
7415 PARAMS is the argument list for the call. The return value is non-null
7416 TRUE is returned if it is translated into the proper format for a call to the
7417 external library, and NEW_RETURN is set the tree for that function.
7418 FALSE is returned if processing for the _N variation is required, and
7419 NEW_RETURN is set to the return value the result is copied into. */
7422 resolve_overloaded_atomic_store (location_t loc
, tree function
,
7423 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7426 tree I_type
, I_type_ptr
;
7427 int n
= get_atomic_generic_size (loc
, function
, params
);
7429 /* Size of 0 is an error condition. */
7432 *new_return
= error_mark_node
;
7436 /* If not a lock-free size, change to the library generic format. */
7437 if (!atomic_size_supported_p (n
))
7439 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7443 /* Otherwise, there is a match, so the call needs to be transformed from:
7444 void fn(T* mem, T* value, model)
7446 fn ((In *) mem, (In) *value, model) */
7451 /* Create pointer to appropriate size. */
7452 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7453 I_type_ptr
= build_pointer_type (I_type
);
7455 /* Convert object pointer to required type. */
7456 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7459 /* Convert new value to required type, and dereference it. */
7460 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7461 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7464 /* The memory model is in the right spot already. Return is void. */
7465 *new_return
= NULL_TREE
;
7471 /* Some builtin functions are placeholders for other expressions. This
7472 function should be called immediately after parsing the call expression
7473 before surrounding code has committed to the type of the expression.
7475 LOC is the location of the builtin call.
7477 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7478 PARAMS is the argument list for the call. The return value is non-null
7479 when expansion is complete, and null if normal processing should
7483 resolve_overloaded_builtin (location_t loc
, tree function
,
7484 vec
<tree
, va_gc
> *params
)
7486 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7487 Those are not valid to call with a pointer to _Bool (or C++ bool)
7488 and so must be rejected. */
7489 bool fetch_op
= true;
7490 bool orig_format
= true;
7491 tree new_return
= NULL_TREE
;
7493 switch (DECL_BUILT_IN_CLASS (function
))
7495 case BUILT_IN_NORMAL
:
7498 if (targetm
.resolve_overloaded_builtin
)
7499 return targetm
.resolve_overloaded_builtin (loc
, function
, params
);
7506 /* Handle BUILT_IN_NORMAL here. */
7507 enum built_in_function orig_code
= DECL_FUNCTION_CODE (function
);
7510 case BUILT_IN_SPECULATION_SAFE_VALUE_N
:
7512 tree new_function
, first_param
, result
;
7513 enum built_in_function fncode
7514 = speculation_safe_value_resolve_call (function
, params
);
7516 if (fncode
== BUILT_IN_NONE
)
7517 return error_mark_node
;
7519 first_param
= (*params
)[0];
7520 if (!speculation_safe_value_resolve_params (loc
, function
, params
))
7521 return error_mark_node
;
7523 if (targetm
.have_speculation_safe_value (true))
7525 new_function
= builtin_decl_explicit (fncode
);
7526 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7529 if (result
== error_mark_node
)
7532 return speculation_safe_value_resolve_return (first_param
, result
);
7536 /* This target doesn't have, or doesn't need, active mitigation
7537 against incorrect speculative execution. Simply return the
7538 first parameter to the builtin. */
7539 if (!targetm
.have_speculation_safe_value (false))
7540 /* The user has invoked __builtin_speculation_safe_value
7541 even though __HAVE_SPECULATION_SAFE_VALUE is not
7542 defined: emit a warning. */
7543 warning_at (input_location
, 0,
7544 "this target does not define a speculation barrier; "
7545 "your program will still execute correctly, "
7546 "but incorrect speculation may not be "
7549 /* If the optional second argument is present, handle any side
7551 if (params
->length () == 2
7552 && TREE_SIDE_EFFECTS ((*params
)[1]))
7553 return build2 (COMPOUND_EXPR
, TREE_TYPE (first_param
),
7554 (*params
)[1], first_param
);
7560 case BUILT_IN_ATOMIC_EXCHANGE
:
7561 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7562 case BUILT_IN_ATOMIC_LOAD
:
7563 case BUILT_IN_ATOMIC_STORE
:
7565 /* Handle these 4 together so that they can fall through to the next
7566 case if the call is transformed to an _N variant. */
7569 case BUILT_IN_ATOMIC_EXCHANGE
:
7571 if (resolve_overloaded_atomic_exchange (loc
, function
, params
,
7574 /* Change to the _N variant. */
7575 orig_code
= BUILT_IN_ATOMIC_EXCHANGE_N
;
7579 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7581 if (resolve_overloaded_atomic_compare_exchange (loc
, function
,
7585 /* Change to the _N variant. */
7586 orig_code
= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
;
7589 case BUILT_IN_ATOMIC_LOAD
:
7591 if (resolve_overloaded_atomic_load (loc
, function
, params
,
7594 /* Change to the _N variant. */
7595 orig_code
= BUILT_IN_ATOMIC_LOAD_N
;
7598 case BUILT_IN_ATOMIC_STORE
:
7600 if (resolve_overloaded_atomic_store (loc
, function
, params
,
7603 /* Change to the _N variant. */
7604 orig_code
= BUILT_IN_ATOMIC_STORE_N
;
7612 case BUILT_IN_ATOMIC_EXCHANGE_N
:
7613 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
7614 case BUILT_IN_ATOMIC_LOAD_N
:
7615 case BUILT_IN_ATOMIC_STORE_N
:
7618 case BUILT_IN_ATOMIC_ADD_FETCH_N
:
7619 case BUILT_IN_ATOMIC_SUB_FETCH_N
:
7620 case BUILT_IN_ATOMIC_AND_FETCH_N
:
7621 case BUILT_IN_ATOMIC_NAND_FETCH_N
:
7622 case BUILT_IN_ATOMIC_XOR_FETCH_N
:
7623 case BUILT_IN_ATOMIC_OR_FETCH_N
:
7624 case BUILT_IN_ATOMIC_FETCH_ADD_N
:
7625 case BUILT_IN_ATOMIC_FETCH_SUB_N
:
7626 case BUILT_IN_ATOMIC_FETCH_AND_N
:
7627 case BUILT_IN_ATOMIC_FETCH_NAND_N
:
7628 case BUILT_IN_ATOMIC_FETCH_XOR_N
:
7629 case BUILT_IN_ATOMIC_FETCH_OR_N
:
7630 orig_format
= false;
7632 case BUILT_IN_SYNC_FETCH_AND_ADD_N
:
7633 case BUILT_IN_SYNC_FETCH_AND_SUB_N
:
7634 case BUILT_IN_SYNC_FETCH_AND_OR_N
:
7635 case BUILT_IN_SYNC_FETCH_AND_AND_N
:
7636 case BUILT_IN_SYNC_FETCH_AND_XOR_N
:
7637 case BUILT_IN_SYNC_FETCH_AND_NAND_N
:
7638 case BUILT_IN_SYNC_ADD_AND_FETCH_N
:
7639 case BUILT_IN_SYNC_SUB_AND_FETCH_N
:
7640 case BUILT_IN_SYNC_OR_AND_FETCH_N
:
7641 case BUILT_IN_SYNC_AND_AND_FETCH_N
:
7642 case BUILT_IN_SYNC_XOR_AND_FETCH_N
:
7643 case BUILT_IN_SYNC_NAND_AND_FETCH_N
:
7644 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
7645 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
:
7646 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
:
7647 case BUILT_IN_SYNC_LOCK_RELEASE_N
:
7649 /* The following are not _FETCH_OPs and must be accepted with
7650 pointers to _Bool (or C++ bool). */
7653 (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7654 && orig_code
!= BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7655 && orig_code
!= BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7656 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
);
7658 int n
= sync_resolve_size (function
, params
, fetch_op
);
7659 tree new_function
, first_param
, result
;
7660 enum built_in_function fncode
;
7663 return error_mark_node
;
7665 fncode
= (enum built_in_function
)((int)orig_code
+ exact_log2 (n
) + 1);
7666 new_function
= builtin_decl_explicit (fncode
);
7667 if (!sync_resolve_params (loc
, function
, new_function
, params
,
7669 return error_mark_node
;
7671 first_param
= (*params
)[0];
7672 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7674 if (result
== error_mark_node
)
7676 if (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7677 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
7678 && orig_code
!= BUILT_IN_ATOMIC_STORE_N
7679 && orig_code
!= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
)
7680 result
= sync_resolve_return (first_param
, result
, orig_format
);
7683 /* Prevent -Wunused-value warning. */
7684 TREE_USED (result
) = true;
7686 /* If new_return is set, assign function to that expr and cast the
7687 result to void since the generic interface returned void. */
7690 /* Cast function result from I{1,2,4,8,16} to the required type. */
7691 result
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (new_return
), result
);
7692 result
= build2 (MODIFY_EXPR
, TREE_TYPE (new_return
), new_return
,
7694 TREE_SIDE_EFFECTS (result
) = 1;
7695 protected_set_expr_location (result
, loc
);
7696 result
= convert (void_type_node
, result
);
7706 /* vector_types_compatible_elements_p is used in type checks of vectors
7707 values used as operands of binary operators. Where it returns true, and
7708 the other checks of the caller succeed (being vector types in he first
7709 place, and matching number of elements), we can just treat the types
7710 as essentially the same.
7711 Contrast with vector_targets_convertible_p, which is used for vector
7712 pointer types, and vector_types_convertible_p, which will allow
7713 language-specific matches under the control of flag_lax_vector_conversions,
7714 and might still require a conversion. */
7715 /* True if vector types T1 and T2 can be inputs to the same binary
7716 operator without conversion.
7717 We don't check the overall vector size here because some of our callers
7718 want to give different error messages when the vectors are compatible
7719 except for the element count. */
7722 vector_types_compatible_elements_p (tree t1
, tree t2
)
7724 bool opaque
= TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
);
7725 t1
= TREE_TYPE (t1
);
7726 t2
= TREE_TYPE (t2
);
7728 enum tree_code c1
= TREE_CODE (t1
), c2
= TREE_CODE (t2
);
7730 gcc_assert ((INTEGRAL_TYPE_P (t1
)
7732 || c1
== FIXED_POINT_TYPE
)
7733 && (INTEGRAL_TYPE_P (t2
)
7735 || c2
== FIXED_POINT_TYPE
));
7737 t1
= c_common_signed_type (t1
);
7738 t2
= c_common_signed_type (t2
);
7739 /* Equality works here because c_common_signed_type uses
7740 TYPE_MAIN_VARIANT. */
7743 if (opaque
&& c1
== c2
7744 && (INTEGRAL_TYPE_P (t1
) || c1
== REAL_TYPE
)
7745 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
))
7750 /* Check for missing format attributes on function pointers. LTYPE is
7751 the new type or left-hand side type. RTYPE is the old type or
7752 right-hand side type. Returns TRUE if LTYPE is missing the desired
7756 check_missing_format_attribute (tree ltype
, tree rtype
)
7758 tree
const ttr
= TREE_TYPE (rtype
), ttl
= TREE_TYPE (ltype
);
7761 for (ra
= TYPE_ATTRIBUTES (ttr
); ra
; ra
= TREE_CHAIN (ra
))
7762 if (is_attribute_p ("format", get_attribute_name (ra
)))
7767 for (la
= TYPE_ATTRIBUTES (ttl
); la
; la
= TREE_CHAIN (la
))
7768 if (is_attribute_p ("format", get_attribute_name (la
)))
7776 /* Setup a TYPE_DECL node as a typedef representation.
7778 X is a TYPE_DECL for a typedef statement. Create a brand new
7779 ..._TYPE node (which will be just a variant of the existing
7780 ..._TYPE node with identical properties) and then install X
7781 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7783 The whole point here is to end up with a situation where each
7784 and every ..._TYPE node the compiler creates will be uniquely
7785 associated with AT MOST one node representing a typedef name.
7786 This way, even though the compiler substitutes corresponding
7787 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7788 early on, later parts of the compiler can always do the reverse
7789 translation and get back the corresponding typedef name. For
7792 typedef struct S MY_TYPE;
7795 Later parts of the compiler might only know that `object' was of
7796 type `struct S' if it were not for code just below. With this
7797 code however, later parts of the compiler see something like:
7799 struct S' == struct S
7800 typedef struct S' MY_TYPE;
7803 And they can then deduce (from the node for type struct S') that
7804 the original object declaration was:
7808 Being able to do this is important for proper support of protoize,
7809 and also for generating precise symbolic debugging information
7810 which takes full account of the programmer's (typedef) vocabulary.
7812 Obviously, we don't want to generate a duplicate ..._TYPE node if
7813 the TYPE_DECL node that we are now processing really represents a
7814 standard built-in type. */
7817 set_underlying_type (tree x
)
7819 if (x
== error_mark_node
)
7821 if (DECL_IS_BUILTIN (x
) && TREE_CODE (TREE_TYPE (x
)) != ARRAY_TYPE
)
7823 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
7824 TYPE_NAME (TREE_TYPE (x
)) = x
;
7826 else if (TREE_TYPE (x
) != error_mark_node
7827 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
7829 tree tt
= TREE_TYPE (x
);
7830 DECL_ORIGINAL_TYPE (x
) = tt
;
7831 tt
= build_variant_type_copy (tt
);
7832 TYPE_STUB_DECL (tt
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
7835 /* Mark the type as used only when its type decl is decorated
7836 with attribute unused. */
7837 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x
)))
7844 /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
7845 the user in diagnostics, false if it would be better to use TYPE itself.
7846 TYPE is known to satisfy typedef_variant_p. */
7849 user_facing_original_type_p (const_tree type
)
7851 gcc_assert (typedef_variant_p (type
));
7852 tree decl
= TYPE_NAME (type
);
7854 /* Look through any typedef in "user" code. */
7855 if (!DECL_IN_SYSTEM_HEADER (decl
) && !DECL_IS_BUILTIN (decl
))
7858 /* If the original type is also named and is in the user namespace,
7859 assume it too is a user-facing type. */
7860 tree orig_type
= DECL_ORIGINAL_TYPE (decl
);
7861 if (tree orig_id
= TYPE_IDENTIFIER (orig_type
))
7862 if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id
)))
7865 switch (TREE_CODE (orig_type
))
7867 /* Don't look through to an anonymous vector type, since the syntax
7868 we use for them in diagnostics isn't real C or C++ syntax.
7869 And if ORIG_TYPE is named but in the implementation namespace,
7870 TYPE is likely to be more meaningful to the user. */
7874 /* Don't expose anonymous tag types that are presumably meant to be
7875 known by their typedef name. Also don't expose tags that are in
7876 the implementation namespace, such as:
7878 typedef struct __foo foo; */
7884 /* Look through to anything else. */
7890 /* Record the types used by the current global variable declaration
7891 being parsed, so that we can decide later to emit their debug info.
7892 Those types are in types_used_by_cur_var_decl, and we are going to
7893 store them in the types_used_by_vars_hash hash table.
7894 DECL is the declaration of the global variable that has been parsed. */
7897 record_types_used_by_current_var_decl (tree decl
)
7899 gcc_assert (decl
&& DECL_P (decl
) && TREE_STATIC (decl
));
7901 while (types_used_by_cur_var_decl
&& !types_used_by_cur_var_decl
->is_empty ())
7903 tree type
= types_used_by_cur_var_decl
->pop ();
7904 types_used_by_var_decl_insert (type
, decl
);
7908 /* The C and C++ parsers both use vectors to hold function arguments.
7909 For efficiency, we keep a cache of unused vectors. This is the
7912 typedef vec
<tree
, va_gc
> *tree_gc_vec
;
7913 static GTY((deletable
)) vec
<tree_gc_vec
, va_gc
> *tree_vector_cache
;
7915 /* Return a new vector from the cache. If the cache is empty,
7916 allocate a new vector. These vectors are GC'ed, so it is OK if the
7917 pointer is not released.. */
7920 make_tree_vector (void)
7922 if (tree_vector_cache
&& !tree_vector_cache
->is_empty ())
7923 return tree_vector_cache
->pop ();
7926 /* Passing 0 to vec::alloc returns NULL, and our callers require
7927 that we always return a non-NULL value. The vector code uses
7928 4 when growing a NULL vector, so we do too. */
7929 vec
<tree
, va_gc
> *v
;
7935 /* Release a vector of trees back to the cache. */
7938 release_tree_vector (vec
<tree
, va_gc
> *vec
)
7943 vec_safe_push (tree_vector_cache
, vec
);
7947 /* Get a new tree vector holding a single tree. */
7950 make_tree_vector_single (tree t
)
7952 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7953 ret
->quick_push (t
);
7957 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7960 make_tree_vector_from_list (tree list
)
7962 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7963 for (; list
; list
= TREE_CHAIN (list
))
7964 vec_safe_push (ret
, TREE_VALUE (list
));
7968 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7971 make_tree_vector_from_ctor (tree ctor
)
7973 vec
<tree
,va_gc
> *ret
= make_tree_vector ();
7974 vec_safe_reserve (ret
, CONSTRUCTOR_NELTS (ctor
));
7975 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (ctor
); ++i
)
7976 ret
->quick_push (CONSTRUCTOR_ELT (ctor
, i
)->value
);
7980 /* Get a new tree vector which is a copy of an existing one. */
7983 make_tree_vector_copy (const vec
<tree
, va_gc
> *orig
)
7985 vec
<tree
, va_gc
> *ret
;
7989 ret
= make_tree_vector ();
7990 vec_safe_reserve (ret
, vec_safe_length (orig
));
7991 FOR_EACH_VEC_SAFE_ELT (orig
, ix
, t
)
7992 ret
->quick_push (t
);
7996 /* Return true if KEYWORD starts a type specifier. */
7999 keyword_begins_type_specifier (enum rid keyword
)
8033 if (keyword
>= RID_FIRST_INT_N
8034 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
8035 && int_n_enabled_p
[keyword
-RID_FIRST_INT_N
])
8041 /* Return true if KEYWORD names a type qualifier. */
8044 keyword_is_type_qualifier (enum rid keyword
)
8058 /* Return true if KEYWORD names a storage class specifier.
8060 RID_TYPEDEF is not included in this list despite `typedef' being
8061 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
8062 such for syntactic convenience only. */
8065 keyword_is_storage_class_specifier (enum rid keyword
)
8081 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
8084 keyword_is_function_specifier (enum rid keyword
)
8098 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
8099 declaration-specifier (C99 6.7). */
8102 keyword_is_decl_specifier (enum rid keyword
)
8104 if (keyword_is_storage_class_specifier (keyword
)
8105 || keyword_is_type_qualifier (keyword
)
8106 || keyword_is_function_specifier (keyword
))
8121 /* Initialize language-specific-bits of tree_contains_struct. */
8124 c_common_init_ts (void)
8126 MARK_TS_EXP (SIZEOF_EXPR
);
8127 MARK_TS_EXP (C_MAYBE_CONST_EXPR
);
8128 MARK_TS_EXP (EXCESS_PRECISION_EXPR
);
8131 /* Build a user-defined numeric literal out of an integer constant type VALUE
8132 with identifier SUFFIX. */
8135 build_userdef_literal (tree suffix_id
, tree value
,
8136 enum overflow_type overflow
, tree num_string
)
8138 tree literal
= make_node (USERDEF_LITERAL
);
8139 USERDEF_LITERAL_SUFFIX_ID (literal
) = suffix_id
;
8140 USERDEF_LITERAL_VALUE (literal
) = value
;
8141 USERDEF_LITERAL_OVERFLOW (literal
) = overflow
;
8142 USERDEF_LITERAL_NUM_STRING (literal
) = num_string
;
8146 /* For vector[index], convert the vector to an array of the underlying type.
8147 Return true if the resulting ARRAY_REF should not be an lvalue. */
8150 convert_vector_to_array_for_subscript (location_t loc
,
8151 tree
*vecp
, tree index
)
8154 if (gnu_vector_type_p (TREE_TYPE (*vecp
)))
8156 tree type
= TREE_TYPE (*vecp
);
8158 ret
= !lvalue_p (*vecp
);
8160 index
= fold_for_warn (index
);
8161 if (TREE_CODE (index
) == INTEGER_CST
)
8162 if (!tree_fits_uhwi_p (index
)
8163 || maybe_ge (tree_to_uhwi (index
), TYPE_VECTOR_SUBPARTS (type
)))
8164 warning_at (loc
, OPT_Warray_bounds
, "index value is out of bound");
8166 /* We are building an ARRAY_REF so mark the vector as addressable
8167 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
8168 for function parameters. */
8169 c_common_mark_addressable_vec (*vecp
);
8171 *vecp
= build1 (VIEW_CONVERT_EXPR
,
8172 build_array_type_nelts (TREE_TYPE (type
),
8173 TYPE_VECTOR_SUBPARTS (type
)),
8179 /* Determine which of the operands, if any, is a scalar that needs to be
8180 converted to a vector, for the range of operations. */
8182 scalar_to_vector (location_t loc
, enum tree_code code
, tree op0
, tree op1
,
8185 tree type0
= TREE_TYPE (op0
);
8186 tree type1
= TREE_TYPE (op1
);
8187 bool integer_only_op
= false;
8188 enum stv_conv ret
= stv_firstarg
;
8190 gcc_assert (gnu_vector_type_p (type0
) || gnu_vector_type_p (type1
));
8193 /* Most GENERIC binary expressions require homogeneous arguments.
8194 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
8195 argument that is a vector and a second one that is a scalar, so
8196 we never return stv_secondarg for them. */
8199 if (TREE_CODE (type0
) == INTEGER_TYPE
8200 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
8202 if (unsafe_conversion_p (TREE_TYPE (type1
), op0
,
8206 error_at (loc
, "conversion of scalar %qT to vector %qT "
8207 "involves truncation", type0
, type1
);
8211 return stv_firstarg
;
8218 integer_only_op
= true;
8226 case TRUNC_DIV_EXPR
:
8228 case FLOOR_DIV_EXPR
:
8229 case ROUND_DIV_EXPR
:
8230 case EXACT_DIV_EXPR
:
8231 case TRUNC_MOD_EXPR
:
8232 case FLOOR_MOD_EXPR
:
8240 /* What about UNLT_EXPR? */
8241 if (gnu_vector_type_p (type0
))
8243 ret
= stv_secondarg
;
8244 std::swap (type0
, type1
);
8245 std::swap (op0
, op1
);
8248 if (TREE_CODE (type0
) == INTEGER_TYPE
8249 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
8251 if (unsafe_conversion_p (TREE_TYPE (type1
), op0
,
8255 error_at (loc
, "conversion of scalar %qT to vector %qT "
8256 "involves truncation", type0
, type1
);
8261 else if (!integer_only_op
8262 /* Allow integer --> real conversion if safe. */
8263 && (TREE_CODE (type0
) == REAL_TYPE
8264 || TREE_CODE (type0
) == INTEGER_TYPE
)
8265 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1
)))
8267 if (unsafe_conversion_p (TREE_TYPE (type1
), op0
,
8271 error_at (loc
, "conversion of scalar %qT to vector %qT "
8272 "involves truncation", type0
, type1
);
8284 /* Return the alignment of std::max_align_t.
8286 [support.types.layout] The type max_align_t is a POD type whose alignment
8287 requirement is at least as great as that of every scalar type, and whose
8288 alignment requirement is supported in every context. */
8291 max_align_t_align ()
8293 unsigned int max_align
= MAX (TYPE_ALIGN (long_long_integer_type_node
),
8294 TYPE_ALIGN (long_double_type_node
));
8295 if (float128_type_node
!= NULL_TREE
)
8296 max_align
= MAX (max_align
, TYPE_ALIGN (float128_type_node
));
8300 /* Return true iff ALIGN is an integral constant that is a fundamental
8301 alignment, as defined by [basic.align] in the c++-11
8306 [A fundamental alignment is represented by an alignment less than or
8307 equal to the greatest alignment supported by the implementation
8308 in all contexts, which is equal to alignof(max_align_t)]. */
8311 cxx_fundamental_alignment_p (unsigned align
)
8313 return (align
<= max_align_t_align ());
8316 /* Return true if T is a pointer to a zero-sized aggregate. */
8319 pointer_to_zero_sized_aggr_p (tree t
)
8321 if (!POINTER_TYPE_P (t
))
8324 return (TYPE_SIZE (t
) && integer_zerop (TYPE_SIZE (t
)));
8327 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8328 with no library fallback or for an ADDR_EXPR whose operand is such type
8329 issues an error pointing to the location LOC.
8330 Returns true when the expression has been diagnosed and false
8334 reject_gcc_builtin (const_tree expr
, location_t loc
/* = UNKNOWN_LOCATION */)
8336 if (TREE_CODE (expr
) == ADDR_EXPR
)
8337 expr
= TREE_OPERAND (expr
, 0);
8339 STRIP_ANY_LOCATION_WRAPPER (expr
);
8341 if (TREE_TYPE (expr
)
8342 && TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
8343 && TREE_CODE (expr
) == FUNCTION_DECL
8344 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8345 false positives for user-declared built-ins such as abs or
8346 strlen, and for C++ operators new and delete.
8347 The c_decl_implicit() test avoids false positives for implicitly
8348 declared built-ins with library fallbacks (such as abs). */
8349 && fndecl_built_in_p (expr
)
8350 && DECL_IS_BUILTIN (expr
)
8351 && !c_decl_implicit (expr
)
8352 && !DECL_ASSEMBLER_NAME_SET_P (expr
))
8354 if (loc
== UNKNOWN_LOCATION
)
8355 loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
8357 /* Reject arguments that are built-in functions with
8358 no library fallback. */
8359 error_at (loc
, "built-in function %qE must be directly called", expr
);
8367 /* Issue an ERROR for an invalid SIZE of array NAME which is null
8368 for unnamed arrays. */
8371 invalid_array_size_error (location_t loc
, cst_size_error error
,
8372 const_tree size
, const_tree name
)
8374 tree maxsize
= max_object_size ();
8377 case cst_size_not_constant
:
8379 error_at (loc
, "size of array %qE is not a constant expression",
8382 error_at (loc
, "size of array is not a constant expression");
8384 case cst_size_negative
:
8386 error_at (loc
, "size %qE of array %qE is negative",
8389 error_at (loc
, "size %qE of array is negative",
8392 case cst_size_too_big
:
8394 error_at (loc
, "size %qE of array %qE exceeds maximum "
8395 "object size %qE", size
, name
, maxsize
);
8397 error_at (loc
, "size %qE of array exceeds maximum "
8398 "object size %qE", size
, maxsize
);
8400 case cst_size_overflow
:
8402 error_at (loc
, "size of array %qE exceeds maximum "
8403 "object size %qE", name
, maxsize
);
8405 error_at (loc
, "size of array exceeds maximum "
8406 "object size %qE", maxsize
);
8413 /* Check if array size calculations overflow or if the array covers more
8414 than half of the address space. Return true if the size of the array
8415 is valid, false otherwise. T is either the type of the array or its
8416 size, and NAME is the name of the array, or null for unnamed arrays. */
8419 valid_array_size_p (location_t loc
, const_tree t
, tree name
, bool complain
)
8421 if (t
== error_mark_node
)
8427 if (!COMPLETE_TYPE_P (t
))
8429 size
= TYPE_SIZE_UNIT (t
);
8434 if (TREE_CODE (size
) != INTEGER_CST
)
8437 cst_size_error error
;
8438 if (valid_constant_size_p (size
, &error
))
8444 if (TREE_CODE (TREE_TYPE (size
)) == ENUMERAL_TYPE
)
8445 /* Show the value of the enumerator rather than its name. */
8446 size
= convert (ssizetype
, const_cast<tree
> (size
));
8448 invalid_array_size_error (loc
, error
, size
, name
);
8452 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8453 timestamp to replace embedded current dates to get reproducible
8454 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
8457 cb_get_source_date_epoch (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
8459 char *source_date_epoch
;
8463 source_date_epoch
= getenv ("SOURCE_DATE_EPOCH");
8464 if (!source_date_epoch
)
8468 #if defined(INT64_T_IS_LONG)
8469 epoch
= strtol (source_date_epoch
, &endptr
, 10);
8471 epoch
= strtoll (source_date_epoch
, &endptr
, 10);
8473 if (errno
!= 0 || endptr
== source_date_epoch
|| *endptr
!= '\0'
8474 || epoch
< 0 || epoch
> MAX_SOURCE_DATE_EPOCH
)
8476 error_at (input_location
, "environment variable %qs must "
8477 "expand to a non-negative integer less than or equal to %wd",
8478 "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH
);
8482 return (time_t) epoch
;
8485 /* Callback for libcpp for offering spelling suggestions for misspelled
8486 directives. GOAL is an unrecognized string; CANDIDATES is a
8487 NULL-terminated array of candidate strings. Return the closest
8488 match to GOAL within CANDIDATES, or NULL if none are good
8492 cb_get_suggestion (cpp_reader
*, const char *goal
,
8493 const char *const *candidates
)
8495 best_match
<const char *, const char *> bm (goal
);
8497 bm
.consider (*candidates
++);
8498 return bm
.get_best_meaningful_candidate ();
8501 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8502 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8503 by C TS 18661-3 for interchange types that are computed in their
8504 native precision are larger than the C11 values for evaluating in the
8505 precision of float/double/long double. If either mode is
8506 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8508 enum flt_eval_method
8509 excess_precision_mode_join (enum flt_eval_method x
,
8510 enum flt_eval_method y
)
8512 if (x
== FLT_EVAL_METHOD_UNPREDICTABLE
8513 || y
== FLT_EVAL_METHOD_UNPREDICTABLE
)
8514 return FLT_EVAL_METHOD_UNPREDICTABLE
;
8516 /* GCC only supports one interchange type right now, _Float16. If
8517 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8518 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8519 if (x
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8521 if (y
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8524 /* Other values for flt_eval_method are directly comparable, and we want
8529 /* Return the value that should be set for FLT_EVAL_METHOD in the
8530 context of ISO/IEC TS 18861-3.
8532 This relates to the effective excess precision seen by the user,
8533 which is the join point of the precision the target requests for
8534 -fexcess-precision={standard,fast} and the implicit excess precision
8537 static enum flt_eval_method
8538 c_ts18661_flt_eval_method (void)
8540 enum flt_eval_method implicit
8541 = targetm
.c
.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT
);
8543 enum excess_precision_type flag_type
8544 = (flag_excess_precision
== EXCESS_PRECISION_STANDARD
8545 ? EXCESS_PRECISION_TYPE_STANDARD
8546 : EXCESS_PRECISION_TYPE_FAST
);
8548 enum flt_eval_method requested
8549 = targetm
.c
.excess_precision (flag_type
);
8551 return excess_precision_mode_join (implicit
, requested
);
8554 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8555 those that were permitted by C11. That is to say, eliminates
8556 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8558 static enum flt_eval_method
8559 c_c11_flt_eval_method (void)
8561 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8562 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT
);
8565 /* Return the value that should be set for FLT_EVAL_METHOD.
8566 MAYBE_C11_ONLY_P is TRUE if we should check
8567 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8568 values we can return to those from C99/C11, and FALSE otherwise.
8569 See the comments on c_ts18661_flt_eval_method for what value we choose
8573 c_flt_eval_method (bool maybe_c11_only_p
)
8575 if (maybe_c11_only_p
8576 && flag_permitted_flt_eval_methods
8577 == PERMITTED_FLT_EVAL_METHODS_C11
)
8578 return c_c11_flt_eval_method ();
8580 return c_ts18661_flt_eval_method ();
8583 /* An enum for get_missing_token_insertion_kind for describing the best
8584 place to insert a missing token, if there is one. */
8586 enum missing_token_insertion_kind
8589 MTIK_INSERT_BEFORE_NEXT
,
8590 MTIK_INSERT_AFTER_PREV
8593 /* Given a missing token of TYPE, determine if it is reasonable to
8594 emit a fix-it hint suggesting the insertion of the token, and,
8595 if so, where the token should be inserted relative to other tokens.
8597 It only makes sense to do this for values of TYPE that are symbols.
8599 Some symbols should go before the next token, e.g. in:
8601 we want to insert the missing '(' immediately before "flag",
8606 These use MTIK_INSERT_BEFORE_NEXT.
8608 Other symbols should go after the previous token, e.g. in:
8611 we want to insert the missing ')' immediately after the "flag",
8618 These use MTIK_INSERT_AFTER_PREV. */
8620 static enum missing_token_insertion_kind
8621 get_missing_token_insertion_kind (enum cpp_ttype type
)
8625 /* Insert missing "opening" brackets immediately
8626 before the next token. */
8627 case CPP_OPEN_SQUARE
:
8628 case CPP_OPEN_PAREN
:
8629 return MTIK_INSERT_BEFORE_NEXT
;
8631 /* Insert other missing symbols immediately after
8632 the previous token. */
8633 case CPP_CLOSE_PAREN
:
8634 case CPP_CLOSE_SQUARE
:
8638 return MTIK_INSERT_AFTER_PREV
;
8640 /* Other kinds of token don't get fix-it hints. */
8642 return MTIK_IMPOSSIBLE
;
8646 /* Given RICHLOC, a location for a diagnostic describing a missing token
8647 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8648 insertion of the token.
8650 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8652 (a) immediately after PREV_TOKEN_LOC, or
8654 (b) immediately before the primary location within RICHLOC (taken to
8655 be that of the token following where the token was expected).
8657 If we manage to add a fix-it hint, then the location of the
8658 fix-it hint is likely to be more useful as the primary location
8659 of the diagnostic than that of the following token, so we swap
8662 For example, given this bogus code:
8663 123456789012345678901234567890
8664 1 | int missing_semicolon (void)
8671 "expected ';' before '}'"
8673 RICHLOC's primary location is at the closing brace, so before "swapping"
8674 we would emit the error at line 4 column 1:
8676 123456789012345678901234567890
8677 3 | return 42 |< fix-it hint emitted for this line
8679 4 | } |< "expected ';' before '}'" emitted at this line
8682 It's more useful for the location of the diagnostic to be at the
8683 fix-it hint, so we swap the locations, so the primary location
8684 is at the fix-it hint, with the old primary location inserted
8685 as a secondary location, giving this, with the error at line 3
8688 123456789012345678901234567890
8689 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8690 | ^ | with fix-it hint
8692 | } |< secondary range emitted here
8696 maybe_suggest_missing_token_insertion (rich_location
*richloc
,
8697 enum cpp_ttype token_type
,
8698 location_t prev_token_loc
)
8700 gcc_assert (richloc
);
8702 enum missing_token_insertion_kind mtik
8703 = get_missing_token_insertion_kind (token_type
);
8711 case MTIK_IMPOSSIBLE
:
8714 case MTIK_INSERT_BEFORE_NEXT
:
8715 /* Attempt to add the fix-it hint before the primary location
8717 richloc
->add_fixit_insert_before (cpp_type2name (token_type
, 0));
8720 case MTIK_INSERT_AFTER_PREV
:
8721 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8722 richloc
->add_fixit_insert_after (prev_token_loc
,
8723 cpp_type2name (token_type
, 0));
8727 /* If we were successful, use the fix-it hint's location as the
8728 primary location within RICHLOC, adding the old primary location
8729 back as a secondary location. */
8730 if (!richloc
->seen_impossible_fixit_p ())
8732 fixit_hint
*hint
= richloc
->get_last_fixit_hint ();
8733 location_t hint_loc
= hint
->get_start_loc ();
8734 location_t old_loc
= richloc
->get_loc ();
8736 richloc
->set_range (0, hint_loc
, SHOW_RANGE_WITH_CARET
);
8737 richloc
->add_range (old_loc
);
8743 namespace selftest
{
8745 /* Verify that fold_for_warn on error_mark_node is safe. */
8748 test_fold_for_warn ()
8750 ASSERT_EQ (error_mark_node
, fold_for_warn (error_mark_node
));
8753 /* Run all of the selftests within this file. */
8758 test_fold_for_warn ();
8761 /* Run all of the tests within c-family. */
8764 c_family_tests (void)
8766 c_common_c_tests ();
8767 c_format_c_tests ();
8768 c_indentation_c_tests ();
8769 c_pretty_print_c_tests ();
8770 c_spellcheck_cc_tests ();
8773 } // namespace selftest
8775 #endif /* #if CHECKING_P */
8777 /* Attempt to locate a suitable location within FILE for a
8778 #include directive to be inserted before.
8779 LOC is the location of the relevant diagnostic.
8781 Attempt to return the location within FILE immediately
8782 after the last #include within that file, or the start of
8783 that file if it has no #include directives.
8785 Return UNKNOWN_LOCATION if no suitable location is found,
8786 or if an error occurs. */
8789 try_to_locate_new_include_insertion_point (const char *file
, location_t loc
)
8791 /* Locate the last ordinary map within FILE that ended with a #include. */
8792 const line_map_ordinary
*last_include_ord_map
= NULL
;
8794 /* ...and the next ordinary map within FILE after that one. */
8795 const line_map_ordinary
*last_ord_map_after_include
= NULL
;
8797 /* ...and the first ordinary map within FILE. */
8798 const line_map_ordinary
*first_ord_map_in_file
= NULL
;
8800 /* Get ordinary map containing LOC (or its expansion). */
8801 const line_map_ordinary
*ord_map_for_loc
= NULL
;
8802 linemap_resolve_location (line_table
, loc
, LRK_MACRO_EXPANSION_POINT
,
8804 gcc_assert (ord_map_for_loc
);
8806 for (unsigned int i
= 0; i
< LINEMAPS_ORDINARY_USED (line_table
); i
++)
8808 const line_map_ordinary
*ord_map
8809 = LINEMAPS_ORDINARY_MAP_AT (line_table
, i
);
8811 if (const line_map_ordinary
*from
8812 = linemap_included_from_linemap (line_table
, ord_map
))
8813 /* We cannot use pointer equality, because with preprocessed
8814 input all filename strings are unique. */
8815 if (0 == strcmp (from
->to_file
, file
))
8817 last_include_ord_map
= from
;
8818 last_ord_map_after_include
= NULL
;
8821 /* Likewise, use strcmp, and reject any line-zero introductory
8823 if (ord_map
->to_line
&& 0 == strcmp (ord_map
->to_file
, file
))
8825 if (!first_ord_map_in_file
)
8826 first_ord_map_in_file
= ord_map
;
8827 if (last_include_ord_map
&& !last_ord_map_after_include
)
8828 last_ord_map_after_include
= ord_map
;
8831 /* Stop searching when reaching the ord_map containing LOC,
8832 as it makes no sense to provide fix-it hints that appear
8833 after the diagnostic in question. */
8834 if (ord_map
== ord_map_for_loc
)
8838 /* Determine where to insert the #include. */
8839 const line_map_ordinary
*ord_map_for_insertion
;
8841 /* We want the next ordmap in the file after the last one that's a
8842 #include, but failing that, the start of the file. */
8843 if (last_ord_map_after_include
)
8844 ord_map_for_insertion
= last_ord_map_after_include
;
8846 ord_map_for_insertion
= first_ord_map_in_file
;
8848 if (!ord_map_for_insertion
)
8849 return UNKNOWN_LOCATION
;
8851 /* The "start_location" is column 0, meaning "the whole line".
8852 rich_location and edit_context can't cope with this, so use
8853 column 1 instead. */
8854 location_t col_0
= ord_map_for_insertion
->start_location
;
8855 return linemap_position_for_loc_and_offset (line_table
, col_0
, 1);
8858 /* A map from filenames to sets of headers added to them, for
8859 ensuring idempotency within maybe_add_include_fixit. */
8861 /* The values within the map. We need string comparison as there's
8862 no guarantee that two different diagnostics that are recommending
8863 adding e.g. "<stdio.h>" are using the same buffer. */
8865 typedef hash_set
<const char *, false, nofree_string_hash
> per_file_includes_t
;
8867 /* The map itself. We don't need string comparison for the filename keys,
8868 as they come from libcpp. */
8870 typedef hash_map
<const char *, per_file_includes_t
*> added_includes_t
;
8871 static added_includes_t
*added_includes
;
8873 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8874 in a suitable location within the file of RICHLOC's primary
8877 This function is idempotent: a header will be added at most once to
8880 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8881 printed, then RICHLOC's primary location will be replaced by that of
8882 the fix-it hint (for use by "inform" notes where the location of the
8883 issue has already been reported). */
8886 maybe_add_include_fixit (rich_location
*richloc
, const char *header
,
8887 bool override_location
)
8889 location_t loc
= richloc
->get_loc ();
8890 const char *file
= LOCATION_FILE (loc
);
8894 /* Idempotency: don't add the same header more than once to a given file. */
8895 if (!added_includes
)
8896 added_includes
= new added_includes_t ();
8897 per_file_includes_t
*&set
= added_includes
->get_or_insert (file
);
8899 if (set
->contains (header
))
8900 /* ...then we've already added HEADER to that file. */
8903 set
= new per_file_includes_t ();
8906 /* Attempt to locate a suitable place for the new directive. */
8907 location_t include_insert_loc
8908 = try_to_locate_new_include_insertion_point (file
, loc
);
8909 if (include_insert_loc
== UNKNOWN_LOCATION
)
8912 char *text
= xasprintf ("#include %s\n", header
);
8913 richloc
->add_fixit_insert_before (include_insert_loc
, text
);
8916 if (override_location
&& global_dc
->show_caret
)
8918 /* Replace the primary location with that of the insertion point for the
8921 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8922 caret for the insertion point (or colorize it).
8924 Hence we print e.g.:
8926 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8927 73 | # include <debug/vector>
8928 +++ |+#include <vector>
8933 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8934 73 | # include <debug/vector>
8935 +++ |+#include <vector>
8939 avoiding the caret on the first column of line 74. */
8940 richloc
->set_range (0, include_insert_loc
, SHOW_LINES_WITHOUT_RANGE
);
8944 /* Attempt to convert a braced array initializer list CTOR for array
8945 TYPE into a STRING_CST for convenience and efficiency. Return
8946 the converted string on success or the original ctor on failure. */
8949 braced_list_to_string (tree type
, tree ctor
, bool member
)
8951 /* Ignore non-members with unknown size like arrays with unspecified
8953 tree typesize
= TYPE_SIZE_UNIT (type
);
8954 if (!member
&& !tree_fits_uhwi_p (typesize
))
8957 /* If the array has an explicit bound, use it to constrain the size
8958 of the string. If it doesn't, be sure to create a string that's
8959 as long as implied by the index of the last zero specified via
8960 a designator, as in:
8961 const char a[] = { [7] = 0 }; */
8962 unsigned HOST_WIDE_INT maxelts
;
8965 maxelts
= tree_to_uhwi (typesize
);
8966 maxelts
/= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type
)));
8969 maxelts
= HOST_WIDE_INT_M1U
;
8971 /* Avoid converting initializers for zero-length arrays (but do
8972 create them for flexible array members). */
8976 unsigned HOST_WIDE_INT nelts
= CONSTRUCTOR_NELTS (ctor
);
8979 str
.reserve (nelts
+ 1);
8981 unsigned HOST_WIDE_INT i
;
8984 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, index
, value
)
8986 unsigned HOST_WIDE_INT idx
= i
;
8989 if (!tree_fits_uhwi_p (index
))
8991 idx
= tree_to_uhwi (index
);
8994 /* auto_vec is limited to UINT_MAX elements. */
8998 /* Avoid non-constant initializers. */
8999 if (!tree_fits_shwi_p (value
))
9002 /* Skip over embedded nuls except the last one (initializer
9003 elements are in ascending order of indices). */
9004 HOST_WIDE_INT val
= tree_to_shwi (value
);
9005 if (!val
&& i
+ 1 < nelts
)
9008 if (idx
< str
.length())
9011 /* Bail if the CTOR has a block of more than 256 embedded nuls
9012 due to implicitly initialized elements. */
9013 unsigned nchars
= (idx
- str
.length ()) + 1;
9020 str
.quick_grow_cleared (idx
);
9026 str
.safe_insert (idx
, val
);
9029 /* Append a nul string termination. */
9030 if (maxelts
!= HOST_WIDE_INT_M1U
&& str
.length () < maxelts
)
9033 /* Build a STRING_CST with the same type as the array. */
9034 tree res
= build_string (str
.length (), str
.begin ());
9035 TREE_TYPE (res
) = type
;
9039 /* Implementation of the two-argument braced_lists_to_string withe
9040 the same arguments plus MEMBER which is set for struct members
9041 to allow initializers for flexible member arrays. */
9044 braced_lists_to_strings (tree type
, tree ctor
, bool member
)
9046 if (TREE_CODE (ctor
) != CONSTRUCTOR
)
9049 tree_code code
= TREE_CODE (type
);
9052 if (code
== ARRAY_TYPE
)
9053 ttp
= TREE_TYPE (type
);
9054 else if (code
== RECORD_TYPE
)
9056 ttp
= TREE_TYPE (ctor
);
9057 if (TREE_CODE (ttp
) == ARRAY_TYPE
)
9060 ttp
= TREE_TYPE (ttp
);
9066 if ((TREE_CODE (ttp
) == ARRAY_TYPE
|| TREE_CODE (ttp
) == INTEGER_TYPE
)
9067 && TYPE_STRING_FLAG (ttp
))
9068 return braced_list_to_string (type
, ctor
, member
);
9070 code
= TREE_CODE (ttp
);
9071 if (code
== ARRAY_TYPE
|| RECORD_OR_UNION_TYPE_P (ttp
))
9073 bool rec
= RECORD_OR_UNION_TYPE_P (ttp
);
9075 /* Handle array of arrays or struct member initializers. */
9077 unsigned HOST_WIDE_INT idx
;
9078 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, val
)
9080 val
= braced_lists_to_strings (ttp
, val
, rec
);
9081 CONSTRUCTOR_ELT (ctor
, idx
)->value
= val
;
9088 /* Attempt to convert a CTOR containing braced array initializer lists
9089 for array TYPE into one containing STRING_CSTs, for convenience and
9090 efficiency. Recurse for arrays of arrays and member initializers.
9091 Return the converted CTOR or STRING_CST on success or the original
9095 braced_lists_to_strings (tree type
, tree ctor
)
9097 return braced_lists_to_strings (type
, ctor
, false);
9101 /* Emit debug for functions before finalizing early debug. */
9104 c_common_finalize_early_debug (void)
9106 /* Emit early debug for reachable functions, and by consequence,
9107 locally scoped symbols. Also emit debug for extern declared
9108 functions that are still reachable at this point. */
9109 struct cgraph_node
*cnode
;
9110 FOR_EACH_FUNCTION (cnode
)
9111 if (!cnode
->alias
&& !cnode
->thunk
.thunk_p
9112 && (cnode
->has_gimple_body_p () || !DECL_IS_BUILTIN (cnode
->decl
)))
9113 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
9116 #include "gt-c-family-c-common.h"