1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2019 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"
53 cpp_reader
*parse_in
; /* Declared in c-pragma.h. */
55 /* Mode used to build pointers (VOIDmode means ptr_mode). */
57 machine_mode c_default_pointer_mode
= VOIDmode
;
59 /* The following symbols are subsumed in the c_global_trees array, and
60 listed here individually for documentation purposes.
62 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
64 tree short_integer_type_node;
65 tree long_integer_type_node;
66 tree long_long_integer_type_node;
68 tree short_unsigned_type_node;
69 tree long_unsigned_type_node;
70 tree long_long_unsigned_type_node;
72 tree truthvalue_type_node;
73 tree truthvalue_false_node;
74 tree truthvalue_true_node;
76 tree ptrdiff_type_node;
78 tree unsigned_char_type_node;
79 tree signed_char_type_node;
83 tree char16_type_node;
84 tree char32_type_node;
87 tree double_type_node;
88 tree long_double_type_node;
90 tree complex_integer_type_node;
91 tree complex_float_type_node;
92 tree complex_double_type_node;
93 tree complex_long_double_type_node;
95 tree dfloat32_type_node;
96 tree dfloat64_type_node;
97 tree_dfloat128_type_node;
100 tree intHI_type_node;
101 tree intSI_type_node;
102 tree intDI_type_node;
103 tree intTI_type_node;
105 tree unsigned_intQI_type_node;
106 tree unsigned_intHI_type_node;
107 tree unsigned_intSI_type_node;
108 tree unsigned_intDI_type_node;
109 tree unsigned_intTI_type_node;
111 tree widest_integer_literal_type_node;
112 tree widest_unsigned_literal_type_node;
114 Nodes for types `void *' and `const void *'.
116 tree ptr_type_node, const_ptr_type_node;
118 Nodes for types `char *' and `const char *'.
120 tree string_type_node, const_string_type_node;
122 Type `char[SOMENUMBER]'.
123 Used when an array of char is needed and the size is irrelevant.
125 tree char_array_type_node;
127 Type `wchar_t[SOMENUMBER]' or something like it.
128 Used when a wide string literal is created.
130 tree wchar_array_type_node;
132 Type `char8_t[SOMENUMBER]' or something like it.
133 Used when a UTF-8 string literal is created.
135 tree char8_array_type_node;
137 Type `char16_t[SOMENUMBER]' or something like it.
138 Used when a UTF-16 string literal is created.
140 tree char16_array_type_node;
142 Type `char32_t[SOMENUMBER]' or something like it.
143 Used when a UTF-32 string literal is created.
145 tree char32_array_type_node;
147 Type `int ()' -- used for implicit declaration of functions.
149 tree default_function_type;
151 A VOID_TYPE node, packaged in a TREE_LIST.
155 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
156 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
157 VAR_DECLS, but C++ does.)
159 tree function_name_decl_node;
160 tree pretty_function_name_decl_node;
161 tree c99_function_name_decl_node;
163 Stack of nested function name VAR_DECLs.
165 tree saved_function_name_decls;
169 tree c_global_trees
[CTI_MAX
];
171 /* Switches common to the C front ends. */
173 /* Nonzero means don't output line number information. */
175 char flag_no_line_commands
;
177 /* Nonzero causes -E output not to be done, but directives such as
178 #define that have side effects are still obeyed. */
182 /* Nonzero means dump macros in some fashion. */
184 char flag_dump_macros
;
186 /* Nonzero means pass #include lines through to the output. */
188 char flag_dump_includes
;
190 /* Nonzero means process PCH files while preprocessing. */
192 bool flag_pch_preprocess
;
194 /* The file name to which we should write a precompiled header, or
195 NULL if no header will be written in this compile. */
197 const char *pch_file
;
199 /* Nonzero if an ISO standard was selected. It rejects macros in the
203 /* C/ObjC language option variables. */
206 /* Nonzero means allow type mismatches in conditional expressions;
207 just make their values `void'. */
209 int flag_cond_mismatch
;
211 /* Nonzero means enable C89 Amendment 1 features. */
215 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
219 /* Nonzero means use the ISO C11 dialect of C. */
223 /* Nonzero means use the ISO C2X dialect of C. */
227 /* Nonzero means that we have builtin functions, and main is an int. */
232 /* ObjC language option variables. */
235 /* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
239 int print_struct_values
;
241 /* Tells the compiler what is the constant string class for ObjC. */
243 const char *constant_string_class_name
;
246 /* C++ language option variables. */
248 /* The reference version of the ABI for -Wabi. */
250 int warn_abi_version
= -1;
252 /* Nonzero means generate separate instantiation control files and
253 juggle them at link time. */
255 int flag_use_repository
;
257 /* The C++ dialect being used. Default set in c_common_post_options. */
259 enum cxx_dialect cxx_dialect
= cxx_unset
;
261 /* Maximum template instantiation depth. This limit exists to limit the
262 time it takes to notice excessively recursive template instantiations.
264 The default is lower than the 1024 recommended by the C++0x standard
265 because G++ runs out of stack before 1024 with highly recursive template
266 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
268 int max_tinst_depth
= 900;
270 /* The elements of `ridpointers' are identifier nodes for the reserved
271 type names and storage classes. It is indexed by a RID_... value. */
274 tree (*make_fname_decl
) (location_t
, tree
, int);
276 /* Nonzero means don't warn about problems that occur when the code is
278 int c_inhibit_evaluation_warnings
;
280 /* Whether we are building a boolean conversion inside
281 convert_for_assignment, or some other late binary operation. If
282 build_binary_op is called for C (from code shared by C and C++) in
283 this case, then the operands have already been folded and the
284 result will not be folded again, so C_MAYBE_CONST_EXPR should not
286 bool in_late_binary_op
;
288 /* Whether lexing has been completed, so subsequent preprocessor
289 errors should use the compiler's input_location. */
290 bool done_lexing
= false;
292 /* Information about how a function name is generated. */
295 tree
*const decl
; /* pointer to the VAR_DECL. */
296 const unsigned rid
; /* RID number for the identifier. */
297 const int pretty
; /* How pretty is it? */
300 /* The three ways of getting then name of the current function. */
302 const struct fname_var_t fname_vars
[] =
304 /* C99 compliant __func__, must be first. */
305 {&c99_function_name_decl_node
, RID_C99_FUNCTION_NAME
, 0},
306 /* GCC __FUNCTION__ compliant. */
307 {&function_name_decl_node
, RID_FUNCTION_NAME
, 0},
308 /* GCC __PRETTY_FUNCTION__ compliant. */
309 {&pretty_function_name_decl_node
, RID_PRETTY_FUNCTION_NAME
, 1},
313 /* Global visibility options. */
314 struct visibility_flags visibility_options
;
316 static tree
check_case_value (location_t
, tree
);
319 static void check_nonnull_arg (void *, tree
, unsigned HOST_WIDE_INT
);
320 static bool nonnull_check_p (tree
, unsigned HOST_WIDE_INT
);
322 /* Reserved words. The third field is a mask: keywords are disabled
323 if they match the mask.
326 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
327 C --std=c99: D_CXXONLY | D_OBJC
328 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
329 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
330 C++ --std=c++11: D_CONLY | D_OBJC
331 ObjC++ is like C++ except that D_OBJC is not set
333 If -fno-asm is used, D_ASM is added to the mask. If
334 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
335 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
336 In C with -Wc++-compat, we warn if D_CXXWARN is set.
338 Note the complication of the D_CXX_OBJC keywords. These are
339 reserved words such as 'class'. In C++, 'class' is a reserved
340 word. In Objective-C++ it is too. In Objective-C, it is a
341 reserved word too, but only if it follows an '@' sign.
343 const struct c_common_resword c_common_reswords
[] =
345 { "_Alignas", RID_ALIGNAS
, D_CONLY
},
346 { "_Alignof", RID_ALIGNOF
, D_CONLY
},
347 { "_Atomic", RID_ATOMIC
, D_CONLY
},
348 { "_Bool", RID_BOOL
, D_CONLY
},
349 { "_Complex", RID_COMPLEX
, 0 },
350 { "_Imaginary", RID_IMAGINARY
, D_CONLY
},
351 { "_Float16", RID_FLOAT16
, D_CONLY
},
352 { "_Float32", RID_FLOAT32
, D_CONLY
},
353 { "_Float64", RID_FLOAT64
, D_CONLY
},
354 { "_Float128", RID_FLOAT128
, D_CONLY
},
355 { "_Float32x", RID_FLOAT32X
, D_CONLY
},
356 { "_Float64x", RID_FLOAT64X
, D_CONLY
},
357 { "_Float128x", RID_FLOAT128X
, D_CONLY
},
358 { "_Decimal32", RID_DFLOAT32
, D_CONLY
| D_EXT
},
359 { "_Decimal64", RID_DFLOAT64
, D_CONLY
| D_EXT
},
360 { "_Decimal128", RID_DFLOAT128
, D_CONLY
| D_EXT
},
361 { "_Fract", RID_FRACT
, D_CONLY
| D_EXT
},
362 { "_Accum", RID_ACCUM
, D_CONLY
| D_EXT
},
363 { "_Sat", RID_SAT
, D_CONLY
| D_EXT
},
364 { "_Static_assert", RID_STATIC_ASSERT
, D_CONLY
},
365 { "_Noreturn", RID_NORETURN
, D_CONLY
},
366 { "_Generic", RID_GENERIC
, D_CONLY
},
367 { "_Thread_local", RID_THREAD
, D_CONLY
},
368 { "__FUNCTION__", RID_FUNCTION_NAME
, 0 },
369 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME
, 0 },
370 { "__alignof", RID_ALIGNOF
, 0 },
371 { "__alignof__", RID_ALIGNOF
, 0 },
372 { "__asm", RID_ASM
, 0 },
373 { "__asm__", RID_ASM
, 0 },
374 { "__attribute", RID_ATTRIBUTE
, 0 },
375 { "__attribute__", RID_ATTRIBUTE
, 0 },
376 { "__auto_type", RID_AUTO_TYPE
, D_CONLY
},
377 { "__bases", RID_BASES
, D_CXXONLY
},
378 { "__builtin_addressof", RID_ADDRESSOF
, D_CXXONLY
},
379 { "__builtin_call_with_static_chain",
380 RID_BUILTIN_CALL_WITH_STATIC_CHAIN
, D_CONLY
},
381 { "__builtin_choose_expr", RID_CHOOSE_EXPR
, D_CONLY
},
382 { "__builtin_complex", RID_BUILTIN_COMPLEX
, D_CONLY
},
383 { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR
, 0 },
384 { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE
, 0 },
385 { "__builtin_launder", RID_BUILTIN_LAUNDER
, D_CXXONLY
},
386 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE
, 0 },
387 { "__builtin_tgmath", RID_BUILTIN_TGMATH
, D_CONLY
},
388 { "__builtin_offsetof", RID_OFFSETOF
, 0 },
389 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P
, D_CONLY
},
390 { "__builtin_va_arg", RID_VA_ARG
, 0 },
391 { "__complex", RID_COMPLEX
, 0 },
392 { "__complex__", RID_COMPLEX
, 0 },
393 { "__const", RID_CONST
, 0 },
394 { "__const__", RID_CONST
, 0 },
395 { "__decltype", RID_DECLTYPE
, D_CXXONLY
},
396 { "__direct_bases", RID_DIRECT_BASES
, D_CXXONLY
},
397 { "__extension__", RID_EXTENSION
, 0 },
398 { "__func__", RID_C99_FUNCTION_NAME
, 0 },
399 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN
, D_CXXONLY
},
400 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR
, D_CXXONLY
},
401 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY
, D_CXXONLY
},
402 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN
, D_CXXONLY
},
403 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR
, D_CXXONLY
},
404 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY
, D_CXXONLY
},
405 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR
, D_CXXONLY
},
406 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
,
408 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR
, D_CXXONLY
},
409 { "__imag", RID_IMAGPART
, 0 },
410 { "__imag__", RID_IMAGPART
, 0 },
411 { "__inline", RID_INLINE
, 0 },
412 { "__inline__", RID_INLINE
, 0 },
413 { "__is_abstract", RID_IS_ABSTRACT
, D_CXXONLY
},
414 { "__is_aggregate", RID_IS_AGGREGATE
, D_CXXONLY
},
415 { "__is_base_of", RID_IS_BASE_OF
, D_CXXONLY
},
416 { "__is_class", RID_IS_CLASS
, D_CXXONLY
},
417 { "__is_empty", RID_IS_EMPTY
, D_CXXONLY
},
418 { "__is_enum", RID_IS_ENUM
, D_CXXONLY
},
419 { "__is_final", RID_IS_FINAL
, D_CXXONLY
},
420 { "__is_literal_type", RID_IS_LITERAL_TYPE
, D_CXXONLY
},
421 { "__is_pod", RID_IS_POD
, D_CXXONLY
},
422 { "__is_polymorphic", RID_IS_POLYMORPHIC
, 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 { "constexpr", RID_CONSTEXPR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
465 { "const_cast", RID_CONSTCAST
, D_CXXONLY
| D_CXXWARN
},
466 { "continue", RID_CONTINUE
, 0 },
467 { "decltype", RID_DECLTYPE
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
468 { "default", RID_DEFAULT
, 0 },
469 { "delete", RID_DELETE
, D_CXXONLY
| D_CXXWARN
},
471 { "double", RID_DOUBLE
, 0 },
472 { "dynamic_cast", RID_DYNCAST
, D_CXXONLY
| D_CXXWARN
},
473 { "else", RID_ELSE
, 0 },
474 { "enum", RID_ENUM
, 0 },
475 { "explicit", RID_EXPLICIT
, D_CXXONLY
| D_CXXWARN
},
476 { "export", RID_EXPORT
, D_CXXONLY
| D_CXXWARN
},
477 { "extern", RID_EXTERN
, 0 },
478 { "false", RID_FALSE
, D_CXXONLY
| D_CXXWARN
},
479 { "float", RID_FLOAT
, 0 },
480 { "for", RID_FOR
, 0 },
481 { "friend", RID_FRIEND
, D_CXXONLY
| D_CXXWARN
},
482 { "goto", RID_GOTO
, 0 },
484 { "inline", RID_INLINE
, D_EXT89
},
485 { "int", RID_INT
, 0 },
486 { "long", RID_LONG
, 0 },
487 { "mutable", RID_MUTABLE
, D_CXXONLY
| D_CXXWARN
},
488 { "namespace", RID_NAMESPACE
, D_CXXONLY
| D_CXXWARN
},
489 { "new", RID_NEW
, D_CXXONLY
| D_CXXWARN
},
490 { "noexcept", RID_NOEXCEPT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
491 { "nullptr", RID_NULLPTR
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
492 { "operator", RID_OPERATOR
, D_CXXONLY
| D_CXXWARN
},
493 { "private", RID_PRIVATE
, D_CXX_OBJC
| D_CXXWARN
},
494 { "protected", RID_PROTECTED
, D_CXX_OBJC
| D_CXXWARN
},
495 { "public", RID_PUBLIC
, D_CXX_OBJC
| D_CXXWARN
},
496 { "register", RID_REGISTER
, 0 },
497 { "reinterpret_cast", RID_REINTCAST
, D_CXXONLY
| D_CXXWARN
},
498 { "restrict", RID_RESTRICT
, D_CONLY
| D_C99
},
499 { "return", RID_RETURN
, 0 },
500 { "short", RID_SHORT
, 0 },
501 { "signed", RID_SIGNED
, 0 },
502 { "sizeof", RID_SIZEOF
, 0 },
503 { "static", RID_STATIC
, 0 },
504 { "static_assert", RID_STATIC_ASSERT
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
505 { "static_cast", RID_STATCAST
, D_CXXONLY
| D_CXXWARN
},
506 { "struct", RID_STRUCT
, 0 },
507 { "switch", RID_SWITCH
, 0 },
508 { "template", RID_TEMPLATE
, D_CXXONLY
| D_CXXWARN
},
509 { "this", RID_THIS
, D_CXXONLY
| D_CXXWARN
},
510 { "thread_local", RID_THREAD
, D_CXXONLY
| D_CXX11
| D_CXXWARN
},
511 { "throw", RID_THROW
, D_CXX_OBJC
| D_CXXWARN
},
512 { "true", RID_TRUE
, D_CXXONLY
| D_CXXWARN
},
513 { "try", RID_TRY
, D_CXX_OBJC
| D_CXXWARN
},
514 { "typedef", RID_TYPEDEF
, 0 },
515 { "typename", RID_TYPENAME
, D_CXXONLY
| D_CXXWARN
},
516 { "typeid", RID_TYPEID
, D_CXXONLY
| D_CXXWARN
},
517 { "typeof", RID_TYPEOF
, D_ASM
| D_EXT
},
518 { "union", RID_UNION
, 0 },
519 { "unsigned", RID_UNSIGNED
, 0 },
520 { "using", RID_USING
, D_CXXONLY
| D_CXXWARN
},
521 { "virtual", RID_VIRTUAL
, D_CXXONLY
| D_CXXWARN
},
522 { "void", RID_VOID
, 0 },
523 { "volatile", RID_VOLATILE
, 0 },
524 { "wchar_t", RID_WCHAR
, D_CXXONLY
},
525 { "while", RID_WHILE
, 0 },
526 { "__is_assignable", RID_IS_ASSIGNABLE
, D_CXXONLY
},
527 { "__is_constructible", RID_IS_CONSTRUCTIBLE
, D_CXXONLY
},
529 /* C++ transactional memory. */
530 { "synchronized", RID_SYNCHRONIZED
, D_CXX_OBJC
| D_TRANSMEM
},
531 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT
, D_CXXONLY
| D_TRANSMEM
},
532 { "atomic_cancel", RID_ATOMIC_CANCEL
, D_CXXONLY
| D_TRANSMEM
},
533 { "atomic_commit", RID_TRANSACTION_ATOMIC
, D_CXXONLY
| D_TRANSMEM
},
535 /* Concepts-related keywords */
536 { "concept", RID_CONCEPT
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
537 { "requires", RID_REQUIRES
, D_CXX_CONCEPTS_FLAGS
| D_CXXWARN
},
539 /* These Objective-C keywords are recognized only immediately after
541 { "compatibility_alias", RID_AT_ALIAS
, D_OBJC
},
542 { "defs", RID_AT_DEFS
, D_OBJC
},
543 { "encode", RID_AT_ENCODE
, D_OBJC
},
544 { "end", RID_AT_END
, D_OBJC
},
545 { "implementation", RID_AT_IMPLEMENTATION
, D_OBJC
},
546 { "interface", RID_AT_INTERFACE
, D_OBJC
},
547 { "protocol", RID_AT_PROTOCOL
, D_OBJC
},
548 { "selector", RID_AT_SELECTOR
, D_OBJC
},
549 { "finally", RID_AT_FINALLY
, D_OBJC
},
550 { "optional", RID_AT_OPTIONAL
, D_OBJC
},
551 { "required", RID_AT_REQUIRED
, D_OBJC
},
552 { "property", RID_AT_PROPERTY
, D_OBJC
},
553 { "package", RID_AT_PACKAGE
, D_OBJC
},
554 { "synthesize", RID_AT_SYNTHESIZE
, D_OBJC
},
555 { "dynamic", RID_AT_DYNAMIC
, D_OBJC
},
556 /* These are recognized only in protocol-qualifier context
558 { "bycopy", RID_BYCOPY
, D_OBJC
},
559 { "byref", RID_BYREF
, D_OBJC
},
560 { "in", RID_IN
, D_OBJC
},
561 { "inout", RID_INOUT
, D_OBJC
},
562 { "oneway", RID_ONEWAY
, D_OBJC
},
563 { "out", RID_OUT
, D_OBJC
},
564 /* These are recognized inside a property attribute list */
565 { "assign", RID_ASSIGN
, D_OBJC
},
566 { "copy", RID_COPY
, D_OBJC
},
567 { "getter", RID_GETTER
, D_OBJC
},
568 { "nonatomic", RID_NONATOMIC
, D_OBJC
},
569 { "readonly", RID_READONLY
, D_OBJC
},
570 { "readwrite", RID_READWRITE
, D_OBJC
},
571 { "retain", RID_RETAIN
, D_OBJC
},
572 { "setter", RID_SETTER
, D_OBJC
},
575 const unsigned int num_c_common_reswords
=
576 sizeof c_common_reswords
/ sizeof (struct c_common_resword
);
578 /* Return identifier for address space AS. */
581 c_addr_space_name (addr_space_t as
)
583 int rid
= RID_FIRST_ADDR_SPACE
+ as
;
584 gcc_assert (ridpointers
[rid
]);
585 return IDENTIFIER_POINTER (ridpointers
[rid
]);
588 /* Push current bindings for the function name VAR_DECLS. */
591 start_fname_decls (void)
594 tree saved
= NULL_TREE
;
596 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
598 tree decl
= *fname_vars
[ix
].decl
;
602 saved
= tree_cons (decl
, build_int_cst (integer_type_node
, ix
),
604 *fname_vars
[ix
].decl
= NULL_TREE
;
607 if (saved
|| saved_function_name_decls
)
608 /* Normally they'll have been NULL, so only push if we've got a
609 stack, or they are non-NULL. */
610 saved_function_name_decls
= tree_cons (saved
, NULL_TREE
,
611 saved_function_name_decls
);
614 /* Finish up the current bindings, adding them into the current function's
615 statement tree. This must be done _before_ finish_stmt_tree is called.
616 If there is no current function, we must be at file scope and no statements
617 are involved. Pop the previous bindings. */
620 finish_fname_decls (void)
623 tree stmts
= NULL_TREE
;
624 tree stack
= saved_function_name_decls
;
626 for (; stack
&& TREE_VALUE (stack
); stack
= TREE_CHAIN (stack
))
627 append_to_statement_list (TREE_VALUE (stack
), &stmts
);
631 tree
*bodyp
= &DECL_SAVED_TREE (current_function_decl
);
633 if (TREE_CODE (*bodyp
) == BIND_EXPR
)
634 bodyp
= &BIND_EXPR_BODY (*bodyp
);
636 append_to_statement_list_force (*bodyp
, &stmts
);
640 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
641 *fname_vars
[ix
].decl
= NULL_TREE
;
645 /* We had saved values, restore them. */
648 for (saved
= TREE_PURPOSE (stack
); saved
; saved
= TREE_CHAIN (saved
))
650 tree decl
= TREE_PURPOSE (saved
);
651 unsigned ix
= TREE_INT_CST_LOW (TREE_VALUE (saved
));
653 *fname_vars
[ix
].decl
= decl
;
655 stack
= TREE_CHAIN (stack
);
657 saved_function_name_decls
= stack
;
660 /* Return the text name of the current function, suitably prettified
661 by PRETTY_P. Return string must be freed by caller. */
664 fname_as_string (int pretty_p
)
666 const char *name
= "top level";
669 cpp_string cstr
= { 0, 0 }, strname
;
677 if (current_function_decl
)
678 name
= lang_hooks
.decl_printable_name (current_function_decl
, vrb
);
680 len
= strlen (name
) + 3; /* Two for '"'s. One for NULL. */
682 namep
= XNEWVEC (char, len
);
683 snprintf (namep
, len
, "\"%s\"", name
);
684 strname
.text
= (unsigned char *) namep
;
685 strname
.len
= len
- 1;
687 if (cpp_interpret_string (parse_in
, &strname
, 1, &cstr
, CPP_STRING
))
690 return (const char *) cstr
.text
;
696 /* Return the VAR_DECL for a const char array naming the current
697 function. If the VAR_DECL has not yet been created, create it
698 now. RID indicates how it should be formatted and IDENTIFIER_NODE
699 ID is its name (unfortunately C and C++ hold the RID values of
700 keywords in different places, so we can't derive RID from ID in
701 this language independent code. LOC is the location of the
705 fname_decl (location_t loc
, unsigned int rid
, tree id
)
708 tree decl
= NULL_TREE
;
710 for (ix
= 0; fname_vars
[ix
].decl
; ix
++)
711 if (fname_vars
[ix
].rid
== rid
)
714 decl
= *fname_vars
[ix
].decl
;
717 /* If a tree is built here, it would normally have the lineno of
718 the current statement. Later this tree will be moved to the
719 beginning of the function and this line number will be wrong.
720 To avoid this problem set the lineno to 0 here; that prevents
721 it from appearing in the RTL. */
723 location_t saved_location
= input_location
;
724 input_location
= UNKNOWN_LOCATION
;
726 stmts
= push_stmt_list ();
727 decl
= (*make_fname_decl
) (loc
, id
, fname_vars
[ix
].pretty
);
728 stmts
= pop_stmt_list (stmts
);
729 if (!IS_EMPTY_STMT (stmts
))
730 saved_function_name_decls
731 = tree_cons (decl
, stmts
, saved_function_name_decls
);
732 *fname_vars
[ix
].decl
= decl
;
733 input_location
= saved_location
;
735 if (!ix
&& !current_function_decl
)
736 pedwarn (loc
, 0, "%qD is not defined outside of function scope", decl
);
741 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
744 fix_string_type (tree value
)
746 int length
= TREE_STRING_LENGTH (value
);
748 tree e_type
, i_type
, a_type
;
750 /* Compute the number of elements, for the array type. */
751 if (TREE_TYPE (value
) == char_array_type_node
|| !TREE_TYPE (value
))
754 e_type
= char_type_node
;
756 else if (flag_char8_t
&& TREE_TYPE (value
) == char8_array_type_node
)
758 charsz
= TYPE_PRECISION (char8_type_node
) / BITS_PER_UNIT
;
759 e_type
= char8_type_node
;
761 else if (TREE_TYPE (value
) == char16_array_type_node
)
763 charsz
= TYPE_PRECISION (char16_type_node
) / BITS_PER_UNIT
;
764 e_type
= char16_type_node
;
766 else if (TREE_TYPE (value
) == char32_array_type_node
)
768 charsz
= TYPE_PRECISION (char32_type_node
) / BITS_PER_UNIT
;
769 e_type
= char32_type_node
;
773 charsz
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
774 e_type
= wchar_type_node
;
777 /* This matters only for targets where ssizetype has smaller precision
779 if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype
)), length
))
781 error ("size of string literal is too large");
782 length
= tree_to_shwi (TYPE_MAX_VALUE (ssizetype
)) / charsz
* charsz
;
783 char *str
= CONST_CAST (char *, TREE_STRING_POINTER (value
));
784 memset (str
+ length
, '\0',
785 MIN (TREE_STRING_LENGTH (value
) - length
, charsz
));
786 TREE_STRING_LENGTH (value
) = length
;
788 nchars
= length
/ charsz
;
790 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
791 limit in C++98 Annex B is very large (65536) and is not normative,
792 so we do not diagnose it (warn_overlength_strings is forced off
793 in c_common_post_options). */
794 if (warn_overlength_strings
)
796 const int nchars_max
= flag_isoc99
? 4095 : 509;
797 const int relevant_std
= flag_isoc99
? 99 : 90;
798 if (nchars
- 1 > nchars_max
)
799 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
800 separate the %d from the 'C'. 'ISO' should not be
801 translated, but it may be moved after 'C%d' in languages
802 where modifiers follow nouns. */
803 pedwarn (input_location
, OPT_Woverlength_strings
,
804 "string length %qd is greater than the length %qd "
805 "ISO C%d compilers are required to support",
806 nchars
- 1, nchars_max
, relevant_std
);
809 /* Create the array type for the string constant. The ISO C++
810 standard says that a string literal has type `const char[N]' or
811 `const wchar_t[N]'. We use the same logic when invoked as a C
812 front-end with -Wwrite-strings.
813 ??? We should change the type of an expression depending on the
814 state of a warning flag. We should just be warning -- see how
815 this is handled in the C++ front-end for the deprecated implicit
816 conversion from string literals to `char*' or `wchar_t*'.
818 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
819 array type being the unqualified version of that type.
820 Therefore, if we are constructing an array of const char, we must
821 construct the matching unqualified array type first. The C front
822 end does not require this, but it does no harm, so we do it
824 i_type
= build_index_type (size_int (nchars
- 1));
825 a_type
= build_array_type (e_type
, i_type
);
826 if (c_dialect_cxx() || warn_write_strings
)
827 a_type
= c_build_qualified_type (a_type
, TYPE_QUAL_CONST
);
829 TREE_TYPE (value
) = a_type
;
830 TREE_CONSTANT (value
) = 1;
831 TREE_READONLY (value
) = 1;
832 TREE_STATIC (value
) = 1;
836 /* Given a string of type STRING_TYPE, determine what kind of string
837 token would give an equivalent execution encoding: CPP_STRING,
838 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
839 This may not be exactly the string token type that initially created
840 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
841 string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
844 This effectively reverses part of the logic in lex_string and
847 static enum cpp_ttype
848 get_cpp_ttype_from_string_type (tree string_type
)
850 gcc_assert (string_type
);
851 if (TREE_CODE (string_type
) == POINTER_TYPE
)
852 string_type
= TREE_TYPE (string_type
);
854 if (TREE_CODE (string_type
) != ARRAY_TYPE
)
857 tree element_type
= TREE_TYPE (string_type
);
858 if (TREE_CODE (element_type
) != INTEGER_TYPE
)
861 int bits_per_character
= TYPE_PRECISION (element_type
);
862 switch (bits_per_character
)
865 return CPP_STRING
; /* It could have also been CPP_UTF8STRING. */
875 /* The global record of string concatentations, for use in
876 extracting locations within string literals. */
878 GTY(()) string_concat_db
*g_string_concat_db
;
880 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
883 c_get_substring_location (const substring_loc
&substr_loc
,
886 enum cpp_ttype tok_type
887 = get_cpp_ttype_from_string_type (substr_loc
.get_string_type ());
888 if (tok_type
== CPP_OTHER
)
889 return "unrecognized string type";
891 return get_location_within_string (parse_in
, g_string_concat_db
,
892 substr_loc
.get_fmt_string_loc (),
894 substr_loc
.get_caret_idx (),
895 substr_loc
.get_start_idx (),
896 substr_loc
.get_end_idx (),
901 /* Return true iff T is a boolean promoted to int. */
904 bool_promoted_to_int_p (tree t
)
906 return (CONVERT_EXPR_P (t
)
907 && TREE_TYPE (t
) == integer_type_node
908 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == BOOLEAN_TYPE
);
911 /* vector_targets_convertible_p is used for vector pointer types. The
912 callers perform various checks that the qualifiers are satisfactory,
913 while OTOH vector_targets_convertible_p ignores the number of elements
914 in the vectors. That's fine with vector pointers as we can consider,
915 say, a vector of 8 elements as two consecutive vectors of 4 elements,
916 and that does not require and conversion of the pointer values.
917 In contrast, vector_types_convertible_p and
918 vector_types_compatible_elements_p are used for vector value types. */
919 /* True if pointers to distinct types T1 and T2 can be converted to
920 each other without an explicit cast. Only returns true for opaque
923 vector_targets_convertible_p (const_tree t1
, const_tree t2
)
925 if (VECTOR_TYPE_P (t1
) && VECTOR_TYPE_P (t2
)
926 && (TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
927 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
933 /* vector_types_convertible_p is used for vector value types.
934 It could in principle call vector_targets_convertible_p as a subroutine,
935 but then the check for vector type would be duplicated with its callers,
936 and also the purpose of vector_targets_convertible_p would become
938 Where vector_types_convertible_p returns true, a conversion might still be
939 needed to make the types match.
940 In contrast, vector_targets_convertible_p is used for vector pointer
941 values, and vector_types_compatible_elements_p is used specifically
942 in the context for binary operators, as a check if use is possible without
944 /* True if vector types T1 and T2 can be converted to each other
945 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
946 can only be converted with -flax-vector-conversions yet that is not
947 in effect, emit a note telling the user about that option if such
948 a note has not previously been emitted. */
950 vector_types_convertible_p (const_tree t1
, const_tree t2
, bool emit_lax_note
)
952 static bool emitted_lax_note
= false;
953 bool convertible_lax
;
955 if ((TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
))
956 && tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
)))
960 (tree_int_cst_equal (TYPE_SIZE (t1
), TYPE_SIZE (t2
))
961 && (TREE_CODE (TREE_TYPE (t1
)) != REAL_TYPE
962 || known_eq (TYPE_VECTOR_SUBPARTS (t1
),
963 TYPE_VECTOR_SUBPARTS (t2
)))
964 && (INTEGRAL_TYPE_P (TREE_TYPE (t1
))
965 == INTEGRAL_TYPE_P (TREE_TYPE (t2
))));
967 if (!convertible_lax
|| flag_lax_vector_conversions
)
968 return convertible_lax
;
970 if (known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
971 && lang_hooks
.types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
974 if (emit_lax_note
&& !emitted_lax_note
)
976 emitted_lax_note
= true;
977 inform (input_location
, "use %<-flax-vector-conversions%> to permit "
978 "conversions between vectors with differing "
979 "element types or numbers of subparts");
985 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
986 and have vector types, V0 has the same type as V1, and the number of
987 elements of V0, V1, MASK is the same.
989 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
990 called with two arguments. In this case implementation passes the
991 first argument twice in order to share the same tree code. This fact
992 could enable the mask-values being twice the vector length. This is
993 an implementation accident and this semantics is not guaranteed to
996 c_build_vec_perm_expr (location_t loc
, tree v0
, tree v1
, tree mask
,
1001 bool maybe_const
= false;
1002 bool two_arguments
= false;
1004 if (v1
== NULL_TREE
)
1006 two_arguments
= true;
1010 if (v0
== error_mark_node
|| v1
== error_mark_node
1011 || mask
== error_mark_node
)
1012 return error_mark_node
;
1014 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask
)))
1017 error_at (loc
, "%<__builtin_shuffle%> last argument must "
1018 "be an integer vector");
1019 return error_mark_node
;
1022 if (!VECTOR_TYPE_P (TREE_TYPE (v0
))
1023 || !VECTOR_TYPE_P (TREE_TYPE (v1
)))
1026 error_at (loc
, "%<__builtin_shuffle%> arguments must be vectors");
1027 return error_mark_node
;
1030 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0
)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1
)))
1033 error_at (loc
, "%<__builtin_shuffle%> argument vectors must be of "
1035 return error_mark_node
;
1038 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0
)),
1039 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
)))
1040 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1
)),
1041 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask
))))
1044 error_at (loc
, "%<__builtin_shuffle%> number of elements of the "
1045 "argument vector(s) and the mask vector should "
1047 return error_mark_node
;
1050 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0
))))
1051 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask
)))))
1054 error_at (loc
, "%<__builtin_shuffle%> argument vector(s) inner type "
1055 "must have the same size as inner type of the mask");
1056 return error_mark_node
;
1059 if (!c_dialect_cxx ())
1061 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1062 v0
= c_fully_fold (v0
, false, &maybe_const
);
1063 wrap
&= maybe_const
;
1066 v1
= v0
= save_expr (v0
);
1069 v1
= c_fully_fold (v1
, false, &maybe_const
);
1070 wrap
&= maybe_const
;
1073 mask
= c_fully_fold (mask
, false, &maybe_const
);
1074 wrap
&= maybe_const
;
1076 else if (two_arguments
)
1077 v1
= v0
= save_expr (v0
);
1079 ret
= build3_loc (loc
, VEC_PERM_EXPR
, TREE_TYPE (v0
), v0
, v1
, mask
);
1081 if (!c_dialect_cxx () && !wrap
)
1082 ret
= c_wrap_maybe_const (ret
, true);
1087 /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1090 c_build_vec_convert (location_t loc1
, tree expr
, location_t loc2
, tree type
,
1093 if (error_operand_p (type
))
1094 return error_mark_node
;
1095 if (error_operand_p (expr
))
1096 return error_mark_node
;
1098 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1099 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr
)))
1102 error_at (loc1
, "%<__builtin_convertvector%> first argument must "
1103 "be an integer or floating vector");
1104 return error_mark_node
;
1107 if (!VECTOR_INTEGER_TYPE_P (type
) && !VECTOR_FLOAT_TYPE_P (type
))
1110 error_at (loc2
, "%<__builtin_convertvector%> second argument must "
1111 "be an integer or floating vector type");
1112 return error_mark_node
;
1115 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr
)),
1116 TYPE_VECTOR_SUBPARTS (type
)))
1119 error_at (loc1
, "%<__builtin_convertvector%> number of elements "
1120 "of the first argument vector and the second argument "
1121 "vector type should be the same");
1122 return error_mark_node
;
1125 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr
)))
1126 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
1127 || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr
))
1128 && VECTOR_INTEGER_TYPE_P (type
)
1129 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr
)))
1130 == TYPE_PRECISION (TREE_TYPE (type
)))))
1131 return build1_loc (loc1
, VIEW_CONVERT_EXPR
, type
, expr
);
1134 bool maybe_const
= false;
1136 if (!c_dialect_cxx ())
1138 /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1139 expr
= c_fully_fold (expr
, false, &maybe_const
);
1140 wrap
&= maybe_const
;
1143 ret
= build_call_expr_internal_loc (loc1
, IFN_VEC_CONVERT
, type
, 1, expr
);
1146 ret
= c_wrap_maybe_const (ret
, true);
1151 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1152 to integral type. */
1155 c_common_get_narrower (tree op
, int *unsignedp_ptr
)
1157 op
= get_narrower (op
, unsignedp_ptr
);
1159 if (TREE_CODE (TREE_TYPE (op
)) == ENUMERAL_TYPE
1160 && ENUM_IS_SCOPED (TREE_TYPE (op
)))
1162 /* C++0x scoped enumerations don't implicitly convert to integral
1163 type; if we stripped an explicit conversion to a larger type we
1164 need to replace it so common_type will still work. */
1165 tree type
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op
)),
1166 TYPE_UNSIGNED (TREE_TYPE (op
)));
1167 op
= fold_convert (type
, op
);
1172 /* This is a helper function of build_binary_op.
1174 For certain operations if both args were extended from the same
1175 smaller type, do the arithmetic in that type and then extend.
1177 BITWISE indicates a bitwise operation.
1178 For them, this optimization is safe only if
1179 both args are zero-extended or both are sign-extended.
1180 Otherwise, we might change the result.
1181 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1182 but calculated in (unsigned short) it would be (unsigned short)-1.
1185 shorten_binary_op (tree result_type
, tree op0
, tree op1
, bool bitwise
)
1187 int unsigned0
, unsigned1
;
1192 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1193 excessive narrowing when we call get_narrower below. For
1194 example, suppose that OP0 is of unsigned int extended
1195 from signed char and that RESULT_TYPE is long long int.
1196 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1199 (long long int) (unsigned int) signed_char
1201 which get_narrower would narrow down to
1203 (unsigned int) signed char
1205 If we do not cast OP0 first, get_narrower would return
1206 signed_char, which is inconsistent with the case of the
1208 op0
= convert (result_type
, op0
);
1209 op1
= convert (result_type
, op1
);
1211 arg0
= c_common_get_narrower (op0
, &unsigned0
);
1212 arg1
= c_common_get_narrower (op1
, &unsigned1
);
1214 /* UNS is 1 if the operation to be done is an unsigned one. */
1215 uns
= TYPE_UNSIGNED (result_type
);
1217 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1218 but it *requires* conversion to FINAL_TYPE. */
1220 if ((TYPE_PRECISION (TREE_TYPE (op0
))
1221 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1222 && TREE_TYPE (op0
) != result_type
)
1223 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1224 if ((TYPE_PRECISION (TREE_TYPE (op1
))
1225 == TYPE_PRECISION (TREE_TYPE (arg1
)))
1226 && TREE_TYPE (op1
) != result_type
)
1227 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1229 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1231 /* For bitwise operations, signedness of nominal type
1232 does not matter. Consider only how operands were extended. */
1236 /* Note that in all three cases below we refrain from optimizing
1237 an unsigned operation on sign-extended args.
1238 That would not be valid. */
1240 /* Both args variable: if both extended in same way
1241 from same width, do it in that width.
1242 Do it unsigned if args were zero-extended. */
1243 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
1244 < TYPE_PRECISION (result_type
))
1245 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1246 == TYPE_PRECISION (TREE_TYPE (arg0
)))
1247 && unsigned0
== unsigned1
1248 && (unsigned0
|| !uns
))
1249 return c_common_signed_or_unsigned_type
1250 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
1252 else if (TREE_CODE (arg0
) == INTEGER_CST
1253 && (unsigned1
|| !uns
)
1254 && (TYPE_PRECISION (TREE_TYPE (arg1
))
1255 < TYPE_PRECISION (result_type
))
1257 = c_common_signed_or_unsigned_type (unsigned1
,
1259 && !POINTER_TYPE_P (type
)
1260 && int_fits_type_p (arg0
, type
))
1263 else if (TREE_CODE (arg1
) == INTEGER_CST
1264 && (unsigned0
|| !uns
)
1265 && (TYPE_PRECISION (TREE_TYPE (arg0
))
1266 < TYPE_PRECISION (result_type
))
1268 = c_common_signed_or_unsigned_type (unsigned0
,
1270 && !POINTER_TYPE_P (type
)
1271 && int_fits_type_p (arg1
, type
))
1277 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1278 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1281 int_safely_convertible_to_real_p (const_tree from_type
, const_tree to_type
)
1283 tree type_low_bound
= TYPE_MIN_VALUE (from_type
);
1284 tree type_high_bound
= TYPE_MAX_VALUE (from_type
);
1285 REAL_VALUE_TYPE real_low_bound
=
1286 real_value_from_int_cst (0, type_low_bound
);
1287 REAL_VALUE_TYPE real_high_bound
=
1288 real_value_from_int_cst (0, type_high_bound
);
1290 return exact_real_truncate (TYPE_MODE (to_type
), &real_low_bound
)
1291 && exact_real_truncate (TYPE_MODE (to_type
), &real_high_bound
);
1294 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1295 to the complex/real/integer type TYPE. Function returns non-zero when:
1296 * EXPR is a constant which cannot be exactly converted to TYPE.
1297 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1298 for EXPR type and TYPE being both integers or both real, or both
1300 * EXPR is not a constant of complex type and TYPE is a real or
1302 * EXPR is not a constant of real type and TYPE is an integer.
1303 * EXPR is not a constant of integer type which cannot be
1304 exactly converted to real type.
1306 Function allows conversions between types of different signedness and
1307 can return SAFE_CONVERSION (zero) in that case. Function can produce
1308 signedness warnings if PRODUCE_WARNS is true.
1310 RESULT, when non-null is the result of the conversion. When constant
1311 it is included in the text of diagnostics.
1313 Function allows conversions from complex constants to non-complex types,
1314 provided that imaginary part is zero and real part can be safely converted
1317 enum conversion_safety
1318 unsafe_conversion_p (location_t loc
, tree type
, tree expr
, tree result
,
1321 enum conversion_safety give_warning
= SAFE_CONVERSION
; /* is 0 or false */
1322 tree expr_type
= TREE_TYPE (expr
);
1324 bool cstresult
= (result
1325 && TREE_CODE_CLASS (TREE_CODE (result
)) == tcc_constant
);
1327 loc
= expansion_point_location_if_in_system_header (loc
);
1329 expr
= fold_for_warn (expr
);
1331 if (TREE_CODE (expr
) == REAL_CST
|| TREE_CODE (expr
) == INTEGER_CST
)
1333 /* If type is complex, we are interested in compatibility with
1335 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1336 type
= TREE_TYPE (type
);
1338 /* Warn for real constant that is not an exact integer converted
1340 if (TREE_CODE (expr_type
) == REAL_TYPE
1341 && TREE_CODE (type
) == INTEGER_TYPE
)
1343 if (!real_isinteger (TREE_REAL_CST_PTR (expr
), TYPE_MODE (expr_type
)))
1344 give_warning
= UNSAFE_REAL
;
1346 /* Warn for an integer constant that does not fit into integer type. */
1347 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1348 && TREE_CODE (type
) == INTEGER_TYPE
1349 && !int_fits_type_p (expr
, type
))
1351 if (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)
1352 && tree_int_cst_sgn (expr
) < 0)
1357 warning_at (loc
, OPT_Wsign_conversion
,
1358 "unsigned conversion from %qT to %qT "
1359 "changes value from %qE to %qE",
1360 expr_type
, type
, expr
, result
);
1362 warning_at (loc
, OPT_Wsign_conversion
,
1363 "unsigned conversion from %qT to %qT "
1364 "changes the value of %qE",
1365 expr_type
, type
, expr
);
1368 else if (!TYPE_UNSIGNED (type
) && TYPE_UNSIGNED (expr_type
))
1371 warning_at (loc
, OPT_Wsign_conversion
,
1372 "signed conversion from %qT to %qT changes "
1373 "value from %qE to %qE",
1374 expr_type
, type
, expr
, result
);
1376 warning_at (loc
, OPT_Wsign_conversion
,
1377 "signed conversion from %qT to %qT changes "
1379 expr_type
, type
, expr
);
1382 give_warning
= UNSAFE_OTHER
;
1384 else if (TREE_CODE (type
) == REAL_TYPE
)
1386 /* Warn for an integer constant that does not fit into real type. */
1387 if (TREE_CODE (expr_type
) == INTEGER_TYPE
)
1389 REAL_VALUE_TYPE a
= real_value_from_int_cst (0, expr
);
1390 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1391 give_warning
= UNSAFE_REAL
;
1393 /* Warn for a real constant that does not fit into a smaller
1395 else if (TREE_CODE (expr_type
) == REAL_TYPE
1396 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1398 REAL_VALUE_TYPE a
= TREE_REAL_CST (expr
);
1399 if (!exact_real_truncate (TYPE_MODE (type
), &a
))
1400 give_warning
= UNSAFE_REAL
;
1405 else if (TREE_CODE (expr
) == COMPLEX_CST
)
1407 tree imag_part
= TREE_IMAGPART (expr
);
1408 /* Conversion from complex constant with zero imaginary part,
1409 perform check for conversion of real part. */
1410 if ((TREE_CODE (imag_part
) == REAL_CST
1411 && real_zerop (imag_part
))
1412 || (TREE_CODE (imag_part
) == INTEGER_CST
1413 && integer_zerop (imag_part
)))
1414 /* Note: in this branch we use recursive call to unsafe_conversion_p
1415 with different type of EXPR, but it is still safe, because when EXPR
1416 is a constant, it's type is not used in text of generated warnings
1417 (otherwise they could sound misleading). */
1418 return unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
), result
,
1420 /* Conversion from complex constant with non-zero imaginary part. */
1423 /* Conversion to complex type.
1424 Perform checks for both real and imaginary parts. */
1425 if (TREE_CODE (type
) == COMPLEX_TYPE
)
1427 /* Unfortunately, produce_warns must be false in two subsequent
1428 calls of unsafe_conversion_p, because otherwise we could
1429 produce strange "double" warnings, if both real and imaginary
1430 parts have conversion problems related to signedness.
1433 int32_t _Complex a = 0x80000000 + 0x80000000i;
1435 Possible solution: add a separate function for checking
1436 constants and combine result of two calls appropriately. */
1437 enum conversion_safety re_safety
=
1438 unsafe_conversion_p (loc
, type
, TREE_REALPART (expr
),
1440 enum conversion_safety im_safety
=
1441 unsafe_conversion_p (loc
, type
, imag_part
, result
, false);
1443 /* Merge the results into appropriate single warning. */
1445 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1446 if (re_safety
== im_safety
)
1447 give_warning
= re_safety
;
1448 else if (!re_safety
&& im_safety
)
1449 give_warning
= im_safety
;
1450 else if (re_safety
&& !im_safety
)
1451 give_warning
= re_safety
;
1453 give_warning
= UNSAFE_OTHER
;
1455 /* Warn about conversion from complex to real or integer type. */
1457 give_warning
= UNSAFE_IMAGINARY
;
1461 /* Checks for remaining case: EXPR is not constant. */
1464 /* Warn for real types converted to integer types. */
1465 if (TREE_CODE (expr_type
) == REAL_TYPE
1466 && TREE_CODE (type
) == INTEGER_TYPE
)
1467 give_warning
= UNSAFE_REAL
;
1469 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1470 && TREE_CODE (type
) == INTEGER_TYPE
)
1472 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1473 expr
= get_unwidened (expr
, 0);
1474 expr_type
= TREE_TYPE (expr
);
1476 /* Don't warn for short y; short x = ((int)y & 0xff); */
1477 if (TREE_CODE (expr
) == BIT_AND_EXPR
1478 || TREE_CODE (expr
) == BIT_IOR_EXPR
1479 || TREE_CODE (expr
) == BIT_XOR_EXPR
)
1481 /* If both args were extended from a shortest type,
1482 use that type if that is safe. */
1483 expr_type
= shorten_binary_op (expr_type
,
1484 TREE_OPERAND (expr
, 0),
1485 TREE_OPERAND (expr
, 1),
1488 if (TREE_CODE (expr
) == BIT_AND_EXPR
)
1490 tree op0
= TREE_OPERAND (expr
, 0);
1491 tree op1
= TREE_OPERAND (expr
, 1);
1492 bool unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
1493 bool unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
1495 /* If one of the operands is a non-negative constant
1496 that fits in the target type, then the type of the
1497 other operand does not matter. */
1498 if ((TREE_CODE (op0
) == INTEGER_CST
1499 && int_fits_type_p (op0
, c_common_signed_type (type
))
1500 && int_fits_type_p (op0
, c_common_unsigned_type (type
)))
1501 || (TREE_CODE (op1
) == INTEGER_CST
1502 && int_fits_type_p (op1
, c_common_signed_type (type
))
1503 && int_fits_type_p (op1
,
1504 c_common_unsigned_type (type
))))
1505 return SAFE_CONVERSION
;
1506 /* If constant is unsigned and fits in the target
1507 type, then the result will also fit. */
1508 else if ((TREE_CODE (op0
) == INTEGER_CST
1510 && int_fits_type_p (op0
, type
))
1511 || (TREE_CODE (op1
) == INTEGER_CST
1513 && int_fits_type_p (op1
, type
)))
1514 return SAFE_CONVERSION
;
1517 /* Warn for integer types converted to smaller integer types. */
1518 if (TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1519 give_warning
= UNSAFE_OTHER
;
1521 /* When they are the same width but different signedness,
1522 then the value may change. */
1523 else if (((TYPE_PRECISION (type
) == TYPE_PRECISION (expr_type
)
1524 && TYPE_UNSIGNED (expr_type
) != TYPE_UNSIGNED (type
))
1525 /* Even when converted to a bigger type, if the type is
1526 unsigned but expr is signed, then negative values
1528 || (TYPE_UNSIGNED (type
) && !TYPE_UNSIGNED (expr_type
)))
1530 warning_at (loc
, OPT_Wsign_conversion
, "conversion to %qT from %qT "
1531 "may change the sign of the result",
1535 /* Warn for integer types converted to real types if and only if
1536 all the range of values of the integer type cannot be
1537 represented by the real type. */
1538 else if (TREE_CODE (expr_type
) == INTEGER_TYPE
1539 && TREE_CODE (type
) == REAL_TYPE
)
1541 /* Don't warn about char y = 0xff; float x = (int) y; */
1542 expr
= get_unwidened (expr
, 0);
1543 expr_type
= TREE_TYPE (expr
);
1545 if (!int_safely_convertible_to_real_p (expr_type
, type
))
1546 give_warning
= UNSAFE_OTHER
;
1549 /* Warn for real types converted to smaller real types. */
1550 else if (TREE_CODE (expr_type
) == REAL_TYPE
1551 && TREE_CODE (type
) == REAL_TYPE
1552 && TYPE_PRECISION (type
) < TYPE_PRECISION (expr_type
))
1553 give_warning
= UNSAFE_REAL
;
1555 /* Check conversion between two complex types. */
1556 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1557 && TREE_CODE (type
) == COMPLEX_TYPE
)
1559 /* Extract underlying types (i.e., type of real and imaginary
1560 parts) of expr_type and type. */
1561 tree from_type
= TREE_TYPE (expr_type
);
1562 tree to_type
= TREE_TYPE (type
);
1564 /* Warn for real types converted to integer types. */
1565 if (TREE_CODE (from_type
) == REAL_TYPE
1566 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1567 give_warning
= UNSAFE_REAL
;
1569 /* Warn for real types converted to smaller real types. */
1570 else if (TREE_CODE (from_type
) == REAL_TYPE
1571 && TREE_CODE (to_type
) == REAL_TYPE
1572 && TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1573 give_warning
= UNSAFE_REAL
;
1575 /* Check conversion for complex integer types. Here implementation
1576 is simpler than for real-domain integers because it does not
1577 involve sophisticated cases, such as bitmasks, casts, etc. */
1578 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1579 && TREE_CODE (to_type
) == INTEGER_TYPE
)
1581 /* Warn for integer types converted to smaller integer types. */
1582 if (TYPE_PRECISION (to_type
) < TYPE_PRECISION (from_type
))
1583 give_warning
= UNSAFE_OTHER
;
1585 /* Check for different signedness, see case for real-domain
1586 integers (above) for a more detailed comment. */
1587 else if (((TYPE_PRECISION (to_type
) == TYPE_PRECISION (from_type
)
1588 && TYPE_UNSIGNED (to_type
) != TYPE_UNSIGNED (from_type
))
1589 || (TYPE_UNSIGNED (to_type
) && !TYPE_UNSIGNED (from_type
)))
1591 warning_at (loc
, OPT_Wsign_conversion
,
1592 "conversion to %qT from %qT "
1593 "may change the sign of the result",
1596 else if (TREE_CODE (from_type
) == INTEGER_TYPE
1597 && TREE_CODE (to_type
) == REAL_TYPE
1598 && !int_safely_convertible_to_real_p (from_type
, to_type
))
1599 give_warning
= UNSAFE_OTHER
;
1602 /* Warn for complex types converted to real or integer types. */
1603 else if (TREE_CODE (expr_type
) == COMPLEX_TYPE
1604 && TREE_CODE (type
) != COMPLEX_TYPE
)
1605 give_warning
= UNSAFE_IMAGINARY
;
1608 return give_warning
;
1612 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1613 Invoke this function on every expression that is converted implicitly,
1614 i.e. because of language rules and not because of an explicit cast. */
1617 convert_and_check (location_t loc
, tree type
, tree expr
)
1620 tree expr_for_warning
;
1622 /* Convert from a value with possible excess precision rather than
1623 via the semantic type, but do not warn about values not fitting
1624 exactly in the semantic type. */
1625 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
1627 tree orig_type
= TREE_TYPE (expr
);
1628 expr
= TREE_OPERAND (expr
, 0);
1629 expr_for_warning
= convert (orig_type
, expr
);
1630 if (orig_type
== type
)
1631 return expr_for_warning
;
1634 expr_for_warning
= expr
;
1636 if (TREE_TYPE (expr
) == type
)
1639 result
= convert (type
, expr
);
1641 if (c_inhibit_evaluation_warnings
== 0
1642 && !TREE_OVERFLOW_P (expr
)
1643 && result
!= error_mark_node
)
1644 warnings_for_convert_and_check (loc
, type
, expr_for_warning
, result
);
1649 /* A node in a list that describes references to variables (EXPR), which are
1650 either read accesses if WRITER is zero, or write accesses, in which case
1651 WRITER is the parent of EXPR. */
1658 /* Used to implement a cache the results of a call to verify_tree. We only
1659 use this for SAVE_EXPRs. */
1662 struct tlist_cache
*next
;
1663 struct tlist
*cache_before_sp
;
1664 struct tlist
*cache_after_sp
;
1668 /* Obstack to use when allocating tlist structures, and corresponding
1670 static struct obstack tlist_obstack
;
1671 static char *tlist_firstobj
= 0;
1673 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1675 static struct tlist
*warned_ids
;
1676 /* SAVE_EXPRs need special treatment. We process them only once and then
1677 cache the results. */
1678 static struct tlist_cache
*save_expr_cache
;
1680 static void add_tlist (struct tlist
**, struct tlist
*, tree
, int);
1681 static void merge_tlist (struct tlist
**, struct tlist
*, int);
1682 static void verify_tree (tree
, struct tlist
**, struct tlist
**, tree
);
1683 static bool warning_candidate_p (tree
);
1684 static bool candidate_equal_p (const_tree
, const_tree
);
1685 static void warn_for_collisions (struct tlist
*);
1686 static void warn_for_collisions_1 (tree
, tree
, struct tlist
*, int);
1687 static struct tlist
*new_tlist (struct tlist
*, tree
, tree
);
1689 /* Create a new struct tlist and fill in its fields. */
1690 static struct tlist
*
1691 new_tlist (struct tlist
*next
, tree t
, tree writer
)
1694 l
= XOBNEW (&tlist_obstack
, struct tlist
);
1701 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1702 is nonnull, we ignore any node we find which has a writer equal to it. */
1705 add_tlist (struct tlist
**to
, struct tlist
*add
, tree exclude_writer
, int copy
)
1709 struct tlist
*next
= add
->next
;
1712 if (!exclude_writer
|| !candidate_equal_p (add
->writer
, exclude_writer
))
1713 *to
= copy
? new_tlist (*to
, add
->expr
, add
->writer
) : add
;
1718 /* Merge the nodes of ADD into TO. This merging process is done so that for
1719 each variable that already exists in TO, no new node is added; however if
1720 there is a write access recorded in ADD, and an occurrence on TO is only
1721 a read access, then the occurrence in TO will be modified to record the
1725 merge_tlist (struct tlist
**to
, struct tlist
*add
, int copy
)
1727 struct tlist
**end
= to
;
1730 end
= &(*end
)->next
;
1736 struct tlist
*next
= add
->next
;
1738 for (tmp2
= *to
; tmp2
; tmp2
= tmp2
->next
)
1739 if (candidate_equal_p (tmp2
->expr
, add
->expr
))
1743 tmp2
->writer
= add
->writer
;
1747 *end
= copy
? new_tlist (NULL
, add
->expr
, add
->writer
) : add
;
1748 end
= &(*end
)->next
;
1755 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1756 references in list LIST conflict with it, excluding reads if ONLY writers
1760 warn_for_collisions_1 (tree written
, tree writer
, struct tlist
*list
,
1765 /* Avoid duplicate warnings. */
1766 for (tmp
= warned_ids
; tmp
; tmp
= tmp
->next
)
1767 if (candidate_equal_p (tmp
->expr
, written
))
1772 if (candidate_equal_p (list
->expr
, written
)
1773 && !candidate_equal_p (list
->writer
, writer
)
1774 && (!only_writes
|| list
->writer
))
1776 warned_ids
= new_tlist (warned_ids
, written
, NULL_TREE
);
1777 warning_at (EXPR_LOC_OR_LOC (writer
, input_location
),
1778 OPT_Wsequence_point
, "operation on %qE may be undefined",
1785 /* Given a list LIST of references to variables, find whether any of these
1786 can cause conflicts due to missing sequence points. */
1789 warn_for_collisions (struct tlist
*list
)
1793 for (tmp
= list
; tmp
; tmp
= tmp
->next
)
1796 warn_for_collisions_1 (tmp
->expr
, tmp
->writer
, list
, 0);
1800 /* Return nonzero if X is a tree that can be verified by the sequence point
1804 warning_candidate_p (tree x
)
1806 if (DECL_P (x
) && DECL_ARTIFICIAL (x
))
1809 if (TREE_CODE (x
) == BLOCK
)
1812 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1813 (lvalue_p) crash on TRY/CATCH. */
1814 if (TREE_TYPE (x
) == NULL_TREE
|| VOID_TYPE_P (TREE_TYPE (x
)))
1820 /* No point to track non-const calls, they will never satisfy
1822 if (TREE_CODE (x
) == CALL_EXPR
&& (call_expr_flags (x
) & ECF_CONST
) == 0)
1825 if (TREE_CODE (x
) == STRING_CST
)
1831 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1833 candidate_equal_p (const_tree x
, const_tree y
)
1835 return (x
== y
) || (x
&& y
&& operand_equal_p (x
, y
, 0));
1838 /* Walk the tree X, and record accesses to variables. If X is written by the
1839 parent tree, WRITER is the parent.
1840 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1841 expression or its only operand forces a sequence point, then everything up
1842 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1844 Once we return, we will have emitted warnings if any subexpression before
1845 such a sequence point could be undefined. On a higher level, however, the
1846 sequence point may not be relevant, and we'll merge the two lists.
1848 Example: (b++, a) + b;
1849 The call that processes the COMPOUND_EXPR will store the increment of B
1850 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1851 processes the PLUS_EXPR will need to merge the two lists so that
1852 eventually, all accesses end up on the same list (and we'll warn about the
1853 unordered subexpressions b++ and b.
1855 A note on merging. If we modify the former example so that our expression
1858 care must be taken not simply to add all three expressions into the final
1859 PNO_SP list. The function merge_tlist takes care of that by merging the
1860 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1861 way, so that no more than one access to B is recorded. */
1864 verify_tree (tree x
, struct tlist
**pbefore_sp
, struct tlist
**pno_sp
,
1867 struct tlist
*tmp_before
, *tmp_nosp
, *tmp_list2
, *tmp_list3
;
1868 enum tree_code code
;
1869 enum tree_code_class cl
;
1871 /* X may be NULL if it is the operand of an empty statement expression
1877 code
= TREE_CODE (x
);
1878 cl
= TREE_CODE_CLASS (code
);
1880 if (warning_candidate_p (x
))
1881 *pno_sp
= new_tlist (*pno_sp
, x
, writer
);
1890 case TRUTH_ANDIF_EXPR
:
1891 case TRUTH_ORIF_EXPR
:
1892 tmp_before
= tmp_nosp
= tmp_list2
= tmp_list3
= 0;
1893 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1894 warn_for_collisions (tmp_nosp
);
1895 merge_tlist (pbefore_sp
, tmp_before
, 0);
1896 merge_tlist (pbefore_sp
, tmp_nosp
, 0);
1897 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1898 warn_for_collisions (tmp_list2
);
1899 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1900 merge_tlist (pno_sp
, tmp_list2
, 0);
1904 tmp_before
= tmp_list2
= 0;
1905 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_list2
, NULL_TREE
);
1906 warn_for_collisions (tmp_list2
);
1907 merge_tlist (pbefore_sp
, tmp_before
, 0);
1908 merge_tlist (pbefore_sp
, tmp_list2
, 0);
1910 tmp_list3
= tmp_nosp
= 0;
1911 verify_tree (TREE_OPERAND (x
, 1), &tmp_list3
, &tmp_nosp
, NULL_TREE
);
1912 warn_for_collisions (tmp_nosp
);
1913 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1915 tmp_list3
= tmp_list2
= 0;
1916 verify_tree (TREE_OPERAND (x
, 2), &tmp_list3
, &tmp_list2
, NULL_TREE
);
1917 warn_for_collisions (tmp_list2
);
1918 merge_tlist (pbefore_sp
, tmp_list3
, 0);
1919 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1920 two first, to avoid warning for (a ? b++ : b++). */
1921 merge_tlist (&tmp_nosp
, tmp_list2
, 0);
1922 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
1925 case PREDECREMENT_EXPR
:
1926 case PREINCREMENT_EXPR
:
1927 case POSTDECREMENT_EXPR
:
1928 case POSTINCREMENT_EXPR
:
1929 verify_tree (TREE_OPERAND (x
, 0), pno_sp
, pno_sp
, x
);
1933 tmp_before
= tmp_nosp
= tmp_list3
= 0;
1934 verify_tree (TREE_OPERAND (x
, 1), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1935 verify_tree (TREE_OPERAND (x
, 0), &tmp_list3
, &tmp_list3
, x
);
1936 /* Expressions inside the LHS are not ordered wrt. the sequence points
1937 in the RHS. Example:
1939 Despite the fact that the modification of "a" is in the before_sp
1940 list (tmp_before), it conflicts with the use of "a" in the LHS.
1941 We can handle this by adding the contents of tmp_list3
1942 to those of tmp_before, and redoing the collision warnings for that
1944 add_tlist (&tmp_before
, tmp_list3
, x
, 1);
1945 warn_for_collisions (tmp_before
);
1946 /* Exclude the LHS itself here; we first have to merge it into the
1947 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1948 didn't exclude the LHS, we'd get it twice, once as a read and once
1950 add_tlist (pno_sp
, tmp_list3
, x
, 0);
1951 warn_for_collisions_1 (TREE_OPERAND (x
, 0), x
, tmp_nosp
, 1);
1953 merge_tlist (pbefore_sp
, tmp_before
, 0);
1954 if (warning_candidate_p (TREE_OPERAND (x
, 0)))
1955 merge_tlist (&tmp_nosp
, new_tlist (NULL
, TREE_OPERAND (x
, 0), x
), 0);
1956 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 1);
1960 /* We need to warn about conflicts among arguments and conflicts between
1961 args and the function address. Side effects of the function address,
1962 however, are not ordered by the sequence point of the call. */
1964 call_expr_arg_iterator iter
;
1966 tmp_before
= tmp_nosp
= 0;
1967 verify_tree (CALL_EXPR_FN (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1968 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, x
)
1970 tmp_list2
= tmp_list3
= 0;
1971 verify_tree (arg
, &tmp_list2
, &tmp_list3
, NULL_TREE
);
1972 merge_tlist (&tmp_list3
, tmp_list2
, 0);
1973 add_tlist (&tmp_before
, tmp_list3
, NULL_TREE
, 0);
1975 add_tlist (&tmp_before
, tmp_nosp
, NULL_TREE
, 0);
1976 warn_for_collisions (tmp_before
);
1977 add_tlist (pbefore_sp
, tmp_before
, NULL_TREE
, 0);
1982 /* Scan all the list, e.g. indices of multi dimensional array. */
1985 tmp_before
= tmp_nosp
= 0;
1986 verify_tree (TREE_VALUE (x
), &tmp_before
, &tmp_nosp
, NULL_TREE
);
1987 merge_tlist (&tmp_nosp
, tmp_before
, 0);
1988 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
1995 struct tlist_cache
*t
;
1996 for (t
= save_expr_cache
; t
; t
= t
->next
)
1997 if (candidate_equal_p (t
->expr
, x
))
2002 t
= XOBNEW (&tlist_obstack
, struct tlist_cache
);
2003 t
->next
= save_expr_cache
;
2005 save_expr_cache
= t
;
2007 tmp_before
= tmp_nosp
= 0;
2008 verify_tree (TREE_OPERAND (x
, 0), &tmp_before
, &tmp_nosp
, NULL_TREE
);
2009 warn_for_collisions (tmp_nosp
);
2012 merge_tlist (&tmp_list3
, tmp_nosp
, 0);
2013 t
->cache_before_sp
= tmp_before
;
2014 t
->cache_after_sp
= tmp_list3
;
2016 merge_tlist (pbefore_sp
, t
->cache_before_sp
, 1);
2017 add_tlist (pno_sp
, t
->cache_after_sp
, NULL_TREE
, 1);
2022 x
= TREE_OPERAND (x
, 0);
2028 case VIEW_CONVERT_EXPR
:
2029 if (location_wrapper_p (x
))
2031 x
= TREE_OPERAND (x
, 0);
2036 /* For other expressions, simply recurse on their operands.
2037 Manual tail recursion for unary expressions.
2038 Other non-expressions need not be processed. */
2039 if (cl
== tcc_unary
)
2041 x
= TREE_OPERAND (x
, 0);
2045 else if (IS_EXPR_CODE_CLASS (cl
))
2048 int max
= TREE_OPERAND_LENGTH (x
);
2049 for (lp
= 0; lp
< max
; lp
++)
2051 tmp_before
= tmp_nosp
= 0;
2052 verify_tree (TREE_OPERAND (x
, lp
), &tmp_before
, &tmp_nosp
, 0);
2053 merge_tlist (&tmp_nosp
, tmp_before
, 0);
2054 add_tlist (pno_sp
, tmp_nosp
, NULL_TREE
, 0);
2061 /* Try to warn for undefined behavior in EXPR due to missing sequence
2065 verify_sequence_points (tree expr
)
2067 struct tlist
*before_sp
= 0, *after_sp
= 0;
2070 save_expr_cache
= 0;
2071 if (tlist_firstobj
== 0)
2073 gcc_obstack_init (&tlist_obstack
);
2074 tlist_firstobj
= (char *) obstack_alloc (&tlist_obstack
, 0);
2077 verify_tree (expr
, &before_sp
, &after_sp
, 0);
2078 warn_for_collisions (after_sp
);
2079 obstack_free (&tlist_obstack
, tlist_firstobj
);
2082 /* Validate the expression after `case' and apply default promotions. */
2085 check_case_value (location_t loc
, tree value
)
2087 if (value
== NULL_TREE
)
2090 if (TREE_CODE (value
) == INTEGER_CST
)
2091 /* Promote char or short to int. */
2092 value
= perform_integral_promotions (value
);
2093 else if (value
!= error_mark_node
)
2095 error_at (loc
, "case label does not reduce to an integer constant");
2096 value
= error_mark_node
;
2099 constant_expression_warning (value
);
2104 /* Return an integer type with BITS bits of precision,
2105 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2108 c_common_type_for_size (unsigned int bits
, int unsignedp
)
2112 if (bits
== TYPE_PRECISION (integer_type_node
))
2113 return unsignedp
? unsigned_type_node
: integer_type_node
;
2115 if (bits
== TYPE_PRECISION (signed_char_type_node
))
2116 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2118 if (bits
== TYPE_PRECISION (short_integer_type_node
))
2119 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2121 if (bits
== TYPE_PRECISION (long_integer_type_node
))
2122 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2124 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
2125 return (unsignedp
? long_long_unsigned_type_node
2126 : long_long_integer_type_node
);
2128 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2129 if (int_n_enabled_p
[i
]
2130 && bits
== int_n_data
[i
].bitsize
)
2131 return (unsignedp
? int_n_trees
[i
].unsigned_type
2132 : int_n_trees
[i
].signed_type
);
2134 if (bits
== TYPE_PRECISION (widest_integer_literal_type_node
))
2135 return (unsignedp
? widest_unsigned_literal_type_node
2136 : widest_integer_literal_type_node
);
2138 if (bits
<= TYPE_PRECISION (intQI_type_node
))
2139 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2141 if (bits
<= TYPE_PRECISION (intHI_type_node
))
2142 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2144 if (bits
<= TYPE_PRECISION (intSI_type_node
))
2145 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2147 if (bits
<= TYPE_PRECISION (intDI_type_node
))
2148 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2153 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2154 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2155 and saturating if SATP is nonzero, otherwise not saturating. */
2158 c_common_fixed_point_type_for_size (unsigned int ibit
, unsigned int fbit
,
2159 int unsignedp
, int satp
)
2161 enum mode_class mclass
;
2163 mclass
= unsignedp
? MODE_UFRACT
: MODE_FRACT
;
2165 mclass
= unsignedp
? MODE_UACCUM
: MODE_ACCUM
;
2167 opt_scalar_mode opt_mode
;
2169 FOR_EACH_MODE_IN_CLASS (opt_mode
, mclass
)
2171 mode
= opt_mode
.require ();
2172 if (GET_MODE_IBIT (mode
) >= ibit
&& GET_MODE_FBIT (mode
) >= fbit
)
2176 if (!opt_mode
.exists (&mode
) || !targetm
.scalar_mode_supported_p (mode
))
2178 sorry ("GCC cannot support operators with integer types and "
2179 "fixed-point types that have too many integral and "
2180 "fractional bits together");
2184 return c_common_type_for_mode (mode
, satp
);
2187 /* Used for communication between c_common_type_for_mode and
2188 c_register_builtin_type. */
2189 tree registered_builtin_types
;
2191 /* Return a data type that has machine mode MODE.
2192 If the mode is an integer,
2193 then UNSIGNEDP selects between signed and unsigned types.
2194 If the mode is a fixed-point mode,
2195 then UNSIGNEDP selects between saturating and nonsaturating types. */
2198 c_common_type_for_mode (machine_mode mode
, int unsignedp
)
2203 if (mode
== TYPE_MODE (integer_type_node
))
2204 return unsignedp
? unsigned_type_node
: integer_type_node
;
2206 if (mode
== TYPE_MODE (signed_char_type_node
))
2207 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2209 if (mode
== TYPE_MODE (short_integer_type_node
))
2210 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2212 if (mode
== TYPE_MODE (long_integer_type_node
))
2213 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2215 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2216 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2218 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2219 if (int_n_enabled_p
[i
]
2220 && mode
== int_n_data
[i
].m
)
2221 return (unsignedp
? int_n_trees
[i
].unsigned_type
2222 : int_n_trees
[i
].signed_type
);
2225 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2228 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2231 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2234 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2236 #if HOST_BITS_PER_WIDE_INT >= 64
2237 if (mode
== TYPE_MODE (intTI_type_node
))
2238 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2241 if (mode
== TYPE_MODE (float_type_node
))
2242 return float_type_node
;
2244 if (mode
== TYPE_MODE (double_type_node
))
2245 return double_type_node
;
2247 if (mode
== TYPE_MODE (long_double_type_node
))
2248 return long_double_type_node
;
2250 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2251 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2252 && mode
== TYPE_MODE (FLOATN_NX_TYPE_NODE (i
)))
2253 return FLOATN_NX_TYPE_NODE (i
);
2255 if (mode
== TYPE_MODE (void_type_node
))
2256 return void_type_node
;
2258 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
))
2259 || mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2261 unsigned int precision
2262 = GET_MODE_PRECISION (as_a
<scalar_int_mode
> (mode
));
2264 ? make_unsigned_type (precision
)
2265 : make_signed_type (precision
));
2268 if (COMPLEX_MODE_P (mode
))
2270 machine_mode inner_mode
;
2273 if (mode
== TYPE_MODE (complex_float_type_node
))
2274 return complex_float_type_node
;
2275 if (mode
== TYPE_MODE (complex_double_type_node
))
2276 return complex_double_type_node
;
2277 if (mode
== TYPE_MODE (complex_long_double_type_node
))
2278 return complex_long_double_type_node
;
2280 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
2281 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
2282 && mode
== TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i
)))
2283 return COMPLEX_FLOATN_NX_TYPE_NODE (i
);
2285 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
2286 return complex_integer_type_node
;
2288 inner_mode
= GET_MODE_INNER (mode
);
2289 inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2290 if (inner_type
!= NULL_TREE
)
2291 return build_complex_type (inner_type
);
2293 else if (GET_MODE_CLASS (mode
) == MODE_VECTOR_BOOL
2294 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2296 unsigned int elem_bits
= vector_element_size (GET_MODE_BITSIZE (mode
),
2297 GET_MODE_NUNITS (mode
));
2298 tree bool_type
= build_nonstandard_boolean_type (elem_bits
);
2299 return build_vector_type_for_mode (bool_type
, mode
);
2301 else if (VECTOR_MODE_P (mode
)
2302 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
2304 machine_mode inner_mode
= GET_MODE_INNER (mode
);
2305 tree inner_type
= c_common_type_for_mode (inner_mode
, unsignedp
);
2306 if (inner_type
!= NULL_TREE
)
2307 return build_vector_type_for_mode (inner_type
, mode
);
2310 if (mode
== TYPE_MODE (dfloat32_type_node
))
2311 return dfloat32_type_node
;
2312 if (mode
== TYPE_MODE (dfloat64_type_node
))
2313 return dfloat64_type_node
;
2314 if (mode
== TYPE_MODE (dfloat128_type_node
))
2315 return dfloat128_type_node
;
2317 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode
))
2319 if (mode
== TYPE_MODE (short_fract_type_node
))
2320 return unsignedp
? sat_short_fract_type_node
: short_fract_type_node
;
2321 if (mode
== TYPE_MODE (fract_type_node
))
2322 return unsignedp
? sat_fract_type_node
: fract_type_node
;
2323 if (mode
== TYPE_MODE (long_fract_type_node
))
2324 return unsignedp
? sat_long_fract_type_node
: long_fract_type_node
;
2325 if (mode
== TYPE_MODE (long_long_fract_type_node
))
2326 return unsignedp
? sat_long_long_fract_type_node
2327 : long_long_fract_type_node
;
2329 if (mode
== TYPE_MODE (unsigned_short_fract_type_node
))
2330 return unsignedp
? sat_unsigned_short_fract_type_node
2331 : unsigned_short_fract_type_node
;
2332 if (mode
== TYPE_MODE (unsigned_fract_type_node
))
2333 return unsignedp
? sat_unsigned_fract_type_node
2334 : unsigned_fract_type_node
;
2335 if (mode
== TYPE_MODE (unsigned_long_fract_type_node
))
2336 return unsignedp
? sat_unsigned_long_fract_type_node
2337 : unsigned_long_fract_type_node
;
2338 if (mode
== TYPE_MODE (unsigned_long_long_fract_type_node
))
2339 return unsignedp
? sat_unsigned_long_long_fract_type_node
2340 : unsigned_long_long_fract_type_node
;
2342 if (mode
== TYPE_MODE (short_accum_type_node
))
2343 return unsignedp
? sat_short_accum_type_node
: short_accum_type_node
;
2344 if (mode
== TYPE_MODE (accum_type_node
))
2345 return unsignedp
? sat_accum_type_node
: accum_type_node
;
2346 if (mode
== TYPE_MODE (long_accum_type_node
))
2347 return unsignedp
? sat_long_accum_type_node
: long_accum_type_node
;
2348 if (mode
== TYPE_MODE (long_long_accum_type_node
))
2349 return unsignedp
? sat_long_long_accum_type_node
2350 : long_long_accum_type_node
;
2352 if (mode
== TYPE_MODE (unsigned_short_accum_type_node
))
2353 return unsignedp
? sat_unsigned_short_accum_type_node
2354 : unsigned_short_accum_type_node
;
2355 if (mode
== TYPE_MODE (unsigned_accum_type_node
))
2356 return unsignedp
? sat_unsigned_accum_type_node
2357 : unsigned_accum_type_node
;
2358 if (mode
== TYPE_MODE (unsigned_long_accum_type_node
))
2359 return unsignedp
? sat_unsigned_long_accum_type_node
2360 : unsigned_long_accum_type_node
;
2361 if (mode
== TYPE_MODE (unsigned_long_long_accum_type_node
))
2362 return unsignedp
? sat_unsigned_long_long_accum_type_node
2363 : unsigned_long_long_accum_type_node
;
2366 return unsignedp
? sat_qq_type_node
: qq_type_node
;
2368 return unsignedp
? sat_hq_type_node
: hq_type_node
;
2370 return unsignedp
? sat_sq_type_node
: sq_type_node
;
2372 return unsignedp
? sat_dq_type_node
: dq_type_node
;
2374 return unsignedp
? sat_tq_type_node
: tq_type_node
;
2376 if (mode
== UQQmode
)
2377 return unsignedp
? sat_uqq_type_node
: uqq_type_node
;
2378 if (mode
== UHQmode
)
2379 return unsignedp
? sat_uhq_type_node
: uhq_type_node
;
2380 if (mode
== USQmode
)
2381 return unsignedp
? sat_usq_type_node
: usq_type_node
;
2382 if (mode
== UDQmode
)
2383 return unsignedp
? sat_udq_type_node
: udq_type_node
;
2384 if (mode
== UTQmode
)
2385 return unsignedp
? sat_utq_type_node
: utq_type_node
;
2388 return unsignedp
? sat_ha_type_node
: ha_type_node
;
2390 return unsignedp
? sat_sa_type_node
: sa_type_node
;
2392 return unsignedp
? sat_da_type_node
: da_type_node
;
2394 return unsignedp
? sat_ta_type_node
: ta_type_node
;
2396 if (mode
== UHAmode
)
2397 return unsignedp
? sat_uha_type_node
: uha_type_node
;
2398 if (mode
== USAmode
)
2399 return unsignedp
? sat_usa_type_node
: usa_type_node
;
2400 if (mode
== UDAmode
)
2401 return unsignedp
? sat_uda_type_node
: uda_type_node
;
2402 if (mode
== UTAmode
)
2403 return unsignedp
? sat_uta_type_node
: uta_type_node
;
2406 for (t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
2407 if (TYPE_MODE (TREE_VALUE (t
)) == mode
2408 && !!unsignedp
== !!TYPE_UNSIGNED (TREE_VALUE (t
)))
2409 return TREE_VALUE (t
);
2415 c_common_unsigned_type (tree type
)
2417 return c_common_signed_or_unsigned_type (1, type
);
2420 /* Return a signed type the same as TYPE in other respects. */
2423 c_common_signed_type (tree type
)
2425 return c_common_signed_or_unsigned_type (0, type
);
2428 /* Return a type the same as TYPE except unsigned or
2429 signed according to UNSIGNEDP. */
2432 c_common_signed_or_unsigned_type (int unsignedp
, tree type
)
2437 /* This block of code emulates the behavior of the old
2438 c_common_unsigned_type. In particular, it returns
2439 long_unsigned_type_node if passed a long, even when a int would
2440 have the same size. This is necessary for warnings to work
2441 correctly in archs where sizeof(int) == sizeof(long) */
2443 type1
= TYPE_MAIN_VARIANT (type
);
2444 if (type1
== signed_char_type_node
|| type1
== char_type_node
|| type1
== unsigned_char_type_node
)
2445 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2446 if (type1
== integer_type_node
|| type1
== unsigned_type_node
)
2447 return unsignedp
? unsigned_type_node
: integer_type_node
;
2448 if (type1
== short_integer_type_node
|| type1
== short_unsigned_type_node
)
2449 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2450 if (type1
== long_integer_type_node
|| type1
== long_unsigned_type_node
)
2451 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2452 if (type1
== long_long_integer_type_node
|| type1
== long_long_unsigned_type_node
)
2453 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2455 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2456 if (int_n_enabled_p
[i
]
2457 && (type1
== int_n_trees
[i
].unsigned_type
2458 || type1
== int_n_trees
[i
].signed_type
))
2459 return (unsignedp
? int_n_trees
[i
].unsigned_type
2460 : int_n_trees
[i
].signed_type
);
2462 #if HOST_BITS_PER_WIDE_INT >= 64
2463 if (type1
== intTI_type_node
|| type1
== unsigned_intTI_type_node
)
2464 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2466 if (type1
== intDI_type_node
|| type1
== unsigned_intDI_type_node
)
2467 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2468 if (type1
== intSI_type_node
|| type1
== unsigned_intSI_type_node
)
2469 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2470 if (type1
== intHI_type_node
|| type1
== unsigned_intHI_type_node
)
2471 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2472 if (type1
== intQI_type_node
|| type1
== unsigned_intQI_type_node
)
2473 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2475 #define C_COMMON_FIXED_TYPES(NAME) \
2476 if (type1 == short_ ## NAME ## _type_node \
2477 || type1 == unsigned_short_ ## NAME ## _type_node) \
2478 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2479 : short_ ## NAME ## _type_node; \
2480 if (type1 == NAME ## _type_node \
2481 || type1 == unsigned_ ## NAME ## _type_node) \
2482 return unsignedp ? unsigned_ ## NAME ## _type_node \
2483 : NAME ## _type_node; \
2484 if (type1 == long_ ## NAME ## _type_node \
2485 || type1 == unsigned_long_ ## NAME ## _type_node) \
2486 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2487 : long_ ## NAME ## _type_node; \
2488 if (type1 == long_long_ ## NAME ## _type_node \
2489 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2490 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2491 : long_long_ ## NAME ## _type_node;
2493 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2494 if (type1 == NAME ## _type_node \
2495 || type1 == u ## NAME ## _type_node) \
2496 return unsignedp ? u ## NAME ## _type_node \
2497 : NAME ## _type_node;
2499 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2500 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2501 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2502 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2503 : sat_ ## short_ ## NAME ## _type_node; \
2504 if (type1 == sat_ ## NAME ## _type_node \
2505 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2506 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2507 : sat_ ## NAME ## _type_node; \
2508 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2509 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2510 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2511 : sat_ ## long_ ## NAME ## _type_node; \
2512 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2513 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2514 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2515 : sat_ ## long_long_ ## NAME ## _type_node;
2517 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2518 if (type1 == sat_ ## NAME ## _type_node \
2519 || type1 == sat_ ## u ## NAME ## _type_node) \
2520 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2521 : sat_ ## NAME ## _type_node;
2523 C_COMMON_FIXED_TYPES (fract
);
2524 C_COMMON_FIXED_TYPES_SAT (fract
);
2525 C_COMMON_FIXED_TYPES (accum
);
2526 C_COMMON_FIXED_TYPES_SAT (accum
);
2528 C_COMMON_FIXED_MODE_TYPES (qq
);
2529 C_COMMON_FIXED_MODE_TYPES (hq
);
2530 C_COMMON_FIXED_MODE_TYPES (sq
);
2531 C_COMMON_FIXED_MODE_TYPES (dq
);
2532 C_COMMON_FIXED_MODE_TYPES (tq
);
2533 C_COMMON_FIXED_MODE_TYPES_SAT (qq
);
2534 C_COMMON_FIXED_MODE_TYPES_SAT (hq
);
2535 C_COMMON_FIXED_MODE_TYPES_SAT (sq
);
2536 C_COMMON_FIXED_MODE_TYPES_SAT (dq
);
2537 C_COMMON_FIXED_MODE_TYPES_SAT (tq
);
2538 C_COMMON_FIXED_MODE_TYPES (ha
);
2539 C_COMMON_FIXED_MODE_TYPES (sa
);
2540 C_COMMON_FIXED_MODE_TYPES (da
);
2541 C_COMMON_FIXED_MODE_TYPES (ta
);
2542 C_COMMON_FIXED_MODE_TYPES_SAT (ha
);
2543 C_COMMON_FIXED_MODE_TYPES_SAT (sa
);
2544 C_COMMON_FIXED_MODE_TYPES_SAT (da
);
2545 C_COMMON_FIXED_MODE_TYPES_SAT (ta
);
2547 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2548 the precision; they have precision set to match their range, but
2549 may use a wider mode to match an ABI. If we change modes, we may
2550 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2551 the precision as well, so as to yield correct results for
2552 bit-field types. C++ does not have these separate bit-field
2553 types, and producing a signed or unsigned variant of an
2554 ENUMERAL_TYPE may cause other problems as well. */
2556 if (!INTEGRAL_TYPE_P (type
)
2557 || TYPE_UNSIGNED (type
) == unsignedp
)
2560 #define TYPE_OK(node) \
2561 (TYPE_MODE (type) == TYPE_MODE (node) \
2562 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2563 if (TYPE_OK (signed_char_type_node
))
2564 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2565 if (TYPE_OK (integer_type_node
))
2566 return unsignedp
? unsigned_type_node
: integer_type_node
;
2567 if (TYPE_OK (short_integer_type_node
))
2568 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2569 if (TYPE_OK (long_integer_type_node
))
2570 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2571 if (TYPE_OK (long_long_integer_type_node
))
2572 return (unsignedp
? long_long_unsigned_type_node
2573 : long_long_integer_type_node
);
2575 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2576 if (int_n_enabled_p
[i
]
2577 && TYPE_MODE (type
) == int_n_data
[i
].m
2578 && TYPE_PRECISION (type
) == int_n_data
[i
].bitsize
)
2579 return (unsignedp
? int_n_trees
[i
].unsigned_type
2580 : int_n_trees
[i
].signed_type
);
2582 #if HOST_BITS_PER_WIDE_INT >= 64
2583 if (TYPE_OK (intTI_type_node
))
2584 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2586 if (TYPE_OK (intDI_type_node
))
2587 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2588 if (TYPE_OK (intSI_type_node
))
2589 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2590 if (TYPE_OK (intHI_type_node
))
2591 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2592 if (TYPE_OK (intQI_type_node
))
2593 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2596 return build_nonstandard_integer_type (TYPE_PRECISION (type
), unsignedp
);
2599 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2602 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width
, int unsignedp
)
2606 /* Extended integer types of the same width as a standard type have
2607 lesser rank, so those of the same width as int promote to int or
2608 unsigned int and are valid for printf formats expecting int or
2609 unsigned int. To avoid such special cases, avoid creating
2610 extended integer types for bit-fields if a standard integer type
2612 if (width
== TYPE_PRECISION (integer_type_node
))
2613 return unsignedp
? unsigned_type_node
: integer_type_node
;
2614 if (width
== TYPE_PRECISION (signed_char_type_node
))
2615 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2616 if (width
== TYPE_PRECISION (short_integer_type_node
))
2617 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2618 if (width
== TYPE_PRECISION (long_integer_type_node
))
2619 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2620 if (width
== TYPE_PRECISION (long_long_integer_type_node
))
2621 return (unsignedp
? long_long_unsigned_type_node
2622 : long_long_integer_type_node
);
2623 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2624 if (int_n_enabled_p
[i
]
2625 && width
== int_n_data
[i
].bitsize
)
2626 return (unsignedp
? int_n_trees
[i
].unsigned_type
2627 : int_n_trees
[i
].signed_type
);
2628 return build_nonstandard_integer_type (width
, unsignedp
);
2631 /* The C version of the register_builtin_type langhook. */
2634 c_register_builtin_type (tree type
, const char* name
)
2638 decl
= build_decl (UNKNOWN_LOCATION
,
2639 TYPE_DECL
, get_identifier (name
), type
);
2640 DECL_ARTIFICIAL (decl
) = 1;
2641 if (!TYPE_NAME (type
))
2642 TYPE_NAME (type
) = decl
;
2643 lang_hooks
.decls
.pushdecl (decl
);
2645 registered_builtin_types
= tree_cons (0, type
, registered_builtin_types
);
2648 /* Print an error message for invalid operands to arith operation
2649 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2650 RICHLOC is a rich location for the message, containing either
2651 three separate locations for each of the operator and operands
2656 (C FE), or one location ranging over all over them
2664 binary_op_error (rich_location
*richloc
, enum tree_code code
,
2665 tree type0
, tree type1
)
2672 opname
= "+"; break;
2674 opname
= "-"; break;
2676 opname
= "*"; break;
2678 opname
= "max"; break;
2680 opname
= "min"; break;
2682 opname
= "=="; break;
2684 opname
= "!="; break;
2686 opname
= "<="; break;
2688 opname
= ">="; break;
2690 opname
= "<"; break;
2692 opname
= ">"; break;
2694 opname
= "<<"; break;
2696 opname
= ">>"; break;
2697 case TRUNC_MOD_EXPR
:
2698 case FLOOR_MOD_EXPR
:
2699 opname
= "%"; break;
2700 case TRUNC_DIV_EXPR
:
2701 case FLOOR_DIV_EXPR
:
2702 opname
= "/"; break;
2704 opname
= "&"; break;
2706 opname
= "|"; break;
2707 case TRUTH_ANDIF_EXPR
:
2708 opname
= "&&"; break;
2709 case TRUTH_ORIF_EXPR
:
2710 opname
= "||"; break;
2712 opname
= "^"; break;
2717 "invalid operands to binary %s (have %qT and %qT)",
2718 opname
, type0
, type1
);
2721 /* Given an expression as a tree, return its original type. Do this
2722 by stripping any conversion that preserves the sign and precision. */
2724 expr_original_type (tree expr
)
2726 STRIP_SIGN_NOPS (expr
);
2727 return TREE_TYPE (expr
);
2730 /* Subroutine of build_binary_op, used for comparison operations.
2731 See if the operands have both been converted from subword integer types
2732 and, if so, perhaps change them both back to their original type.
2733 This function is also responsible for converting the two operands
2734 to the proper common type for comparison.
2736 The arguments of this function are all pointers to local variables
2737 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2738 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2740 LOC is the location of the comparison.
2742 If this function returns non-NULL_TREE, it means that the comparison has
2743 a constant value. What this function returns is an expression for
2747 shorten_compare (location_t loc
, tree
*op0_ptr
, tree
*op1_ptr
,
2748 tree
*restype_ptr
, enum tree_code
*rescode_ptr
)
2751 tree op0
= *op0_ptr
;
2752 tree op1
= *op1_ptr
;
2753 int unsignedp0
, unsignedp1
;
2755 tree primop0
, primop1
;
2756 enum tree_code code
= *rescode_ptr
;
2758 /* Throw away any conversions to wider types
2759 already present in the operands. */
2761 primop0
= c_common_get_narrower (op0
, &unsignedp0
);
2762 primop1
= c_common_get_narrower (op1
, &unsignedp1
);
2764 /* If primopN is first sign-extended from primopN's precision to opN's
2765 precision, then zero-extended from opN's precision to
2766 *restype_ptr precision, shortenings might be invalid. */
2767 if (TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (TREE_TYPE (op0
))
2768 && TYPE_PRECISION (TREE_TYPE (op0
)) < TYPE_PRECISION (*restype_ptr
)
2770 && TYPE_UNSIGNED (TREE_TYPE (op0
)))
2772 if (TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (TREE_TYPE (op1
))
2773 && TYPE_PRECISION (TREE_TYPE (op1
)) < TYPE_PRECISION (*restype_ptr
)
2775 && TYPE_UNSIGNED (TREE_TYPE (op1
)))
2778 /* Handle the case that OP0 does not *contain* a conversion
2779 but it *requires* conversion to FINAL_TYPE. */
2781 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2782 unsignedp0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
2783 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2784 unsignedp1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
2786 /* If one of the operands must be floated, we cannot optimize. */
2787 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2788 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2790 /* If first arg is constant, swap the args (changing operation
2791 so value is preserved), for canonicalization. Don't do this if
2792 the second arg is 0. */
2794 if (TREE_CONSTANT (primop0
)
2795 && !integer_zerop (primop1
) && !real_zerop (primop1
)
2796 && !fixed_zerop (primop1
))
2798 std::swap (primop0
, primop1
);
2799 std::swap (op0
, op1
);
2802 std::swap (unsignedp0
, unsignedp1
);
2803 std::swap (real1
, real2
);
2822 *rescode_ptr
= code
;
2825 /* If comparing an integer against a constant more bits wide,
2826 maybe we can deduce a value of 1 or 0 independent of the data.
2827 Or else truncate the constant now
2828 rather than extend the variable at run time.
2830 This is only interesting if the constant is the wider arg.
2831 Also, it is not safe if the constant is unsigned and the
2832 variable arg is signed, since in this case the variable
2833 would be sign-extended and then regarded as unsigned.
2834 Our technique fails in this case because the lowest/highest
2835 possible unsigned results don't follow naturally from the
2836 lowest/highest possible values of the variable operand.
2837 For just EQ_EXPR and NE_EXPR there is another technique that
2838 could be used: see if the constant can be faithfully represented
2839 in the other operand's type, by truncating it and reextending it
2840 and see if that preserves the constant's value. */
2842 if (!real1
&& !real2
2843 && TREE_CODE (TREE_TYPE (primop0
)) != FIXED_POINT_TYPE
2844 && TREE_CODE (primop1
) == INTEGER_CST
2845 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2847 int min_gt
, max_gt
, min_lt
, max_lt
;
2848 tree maxval
, minval
;
2849 /* 1 if comparison is nominally unsigned. */
2850 int unsignedp
= TYPE_UNSIGNED (*restype_ptr
);
2853 type
= c_common_signed_or_unsigned_type (unsignedp0
,
2854 TREE_TYPE (primop0
));
2856 maxval
= TYPE_MAX_VALUE (type
);
2857 minval
= TYPE_MIN_VALUE (type
);
2859 if (unsignedp
&& !unsignedp0
)
2860 *restype_ptr
= c_common_signed_type (*restype_ptr
);
2862 if (TREE_TYPE (primop1
) != *restype_ptr
)
2864 /* Convert primop1 to target type, but do not introduce
2865 additional overflow. We know primop1 is an int_cst. */
2866 primop1
= force_fit_type (*restype_ptr
,
2869 TYPE_PRECISION (*restype_ptr
)),
2870 0, TREE_OVERFLOW (primop1
));
2872 if (type
!= *restype_ptr
)
2874 minval
= convert (*restype_ptr
, minval
);
2875 maxval
= convert (*restype_ptr
, maxval
);
2878 min_gt
= tree_int_cst_lt (primop1
, minval
);
2879 max_gt
= tree_int_cst_lt (primop1
, maxval
);
2880 min_lt
= tree_int_cst_lt (minval
, primop1
);
2881 max_lt
= tree_int_cst_lt (maxval
, primop1
);
2884 /* This used to be a switch, but Genix compiler can't handle that. */
2885 if (code
== NE_EXPR
)
2887 if (max_lt
|| min_gt
)
2888 val
= truthvalue_true_node
;
2890 else if (code
== EQ_EXPR
)
2892 if (max_lt
|| min_gt
)
2893 val
= truthvalue_false_node
;
2895 else if (code
== LT_EXPR
)
2898 val
= truthvalue_true_node
;
2900 val
= truthvalue_false_node
;
2902 else if (code
== GT_EXPR
)
2905 val
= truthvalue_true_node
;
2907 val
= truthvalue_false_node
;
2909 else if (code
== LE_EXPR
)
2912 val
= truthvalue_true_node
;
2914 val
= truthvalue_false_node
;
2916 else if (code
== GE_EXPR
)
2919 val
= truthvalue_true_node
;
2921 val
= truthvalue_false_node
;
2924 /* If primop0 was sign-extended and unsigned comparison specd,
2925 we did a signed comparison above using the signed type bounds.
2926 But the comparison we output must be unsigned.
2928 Also, for inequalities, VAL is no good; but if the signed
2929 comparison had *any* fixed result, it follows that the
2930 unsigned comparison just tests the sign in reverse
2931 (positive values are LE, negative ones GE).
2932 So we can generate an unsigned comparison
2933 against an extreme value of the signed type. */
2935 if (unsignedp
&& !unsignedp0
)
2942 primop1
= TYPE_MIN_VALUE (type
);
2948 primop1
= TYPE_MAX_VALUE (type
);
2955 type
= c_common_unsigned_type (type
);
2958 if (TREE_CODE (primop0
) != INTEGER_CST
2959 /* Don't warn if it's from a (non-system) macro. */
2960 && !(from_macro_expansion_at
2961 (expansion_point_location_if_in_system_header
2962 (EXPR_LOCATION (primop0
)))))
2964 if (val
== truthvalue_false_node
)
2965 warning_at (loc
, OPT_Wtype_limits
,
2966 "comparison is always false due to limited range of data type");
2967 if (val
== truthvalue_true_node
)
2968 warning_at (loc
, OPT_Wtype_limits
,
2969 "comparison is always true due to limited range of data type");
2974 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2975 if (TREE_SIDE_EFFECTS (primop0
))
2976 return build2 (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2980 /* Value is not predetermined, but do the comparison
2981 in the type of the operand that is not constant.
2982 TYPE is already properly set. */
2985 /* If either arg is decimal float and the other is float, find the
2986 proper common type to use for comparison. */
2987 else if (real1
&& real2
2988 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
2989 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
))))
2990 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2992 /* If either arg is decimal float and the other is float, fail. */
2993 else if (real1
&& real2
2994 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0
)))
2995 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1
)))))
2998 else if (real1
&& real2
2999 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3000 == TYPE_PRECISION (TREE_TYPE (primop1
))))
3001 type
= TREE_TYPE (primop0
);
3003 /* If args' natural types are both narrower than nominal type
3004 and both extend in the same manner, compare them
3005 in the type of the wider arg.
3006 Otherwise must actually extend both to the nominal
3007 common type lest different ways of extending
3009 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3011 else if (unsignedp0
== unsignedp1
&& real1
== real2
3012 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
3013 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
3015 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
3016 type
= c_common_signed_or_unsigned_type (unsignedp0
3017 || TYPE_UNSIGNED (*restype_ptr
),
3019 /* Make sure shorter operand is extended the right way
3020 to match the longer operand. */
3022 = convert (c_common_signed_or_unsigned_type (unsignedp0
,
3023 TREE_TYPE (primop0
)),
3026 = convert (c_common_signed_or_unsigned_type (unsignedp1
,
3027 TREE_TYPE (primop1
)),
3032 /* Here we must do the comparison on the nominal type
3033 using the args exactly as we received them. */
3034 type
= *restype_ptr
;
3038 /* We want to fold unsigned comparisons of >= and < against zero.
3039 For these, we may also issue a warning if we have a non-constant
3040 compared against zero, where the zero was spelled as "0" (rather
3041 than merely folding to it).
3042 If we have at least one constant, then op1 is constant
3043 and we may have a non-constant expression as op0. */
3044 if (!real1
&& !real2
&& integer_zerop (primop1
)
3045 && TYPE_UNSIGNED (*restype_ptr
))
3047 tree value
= NULL_TREE
;
3048 /* All unsigned values are >= 0, so we warn. However,
3049 if OP0 is a constant that is >= 0, the signedness of
3050 the comparison isn't an issue, so suppress the
3052 tree folded_op0
= fold_for_warn (op0
);
3054 warn_type_limits
&& !in_system_header_at (loc
)
3055 && !(TREE_CODE (folded_op0
) == INTEGER_CST
3056 && !TREE_OVERFLOW (convert (c_common_signed_type (type
),
3058 /* Do not warn for enumeration types. */
3059 && (TREE_CODE (expr_original_type (folded_op0
)) != ENUMERAL_TYPE
);
3065 warning_at (loc
, OPT_Wtype_limits
,
3066 "comparison of unsigned expression in %<>= 0%> "
3068 value
= truthvalue_true_node
;
3073 warning_at (loc
, OPT_Wtype_limits
,
3074 "comparison of unsigned expression in %<< 0%> "
3076 value
= truthvalue_false_node
;
3083 if (value
!= NULL_TREE
)
3085 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3086 if (TREE_SIDE_EFFECTS (primop0
))
3087 return build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3094 *op0_ptr
= convert (type
, primop0
);
3095 *op1_ptr
= convert (type
, primop1
);
3097 *restype_ptr
= truthvalue_type_node
;
3102 /* Return a tree for the sum or difference (RESULTCODE says which)
3103 of pointer PTROP and integer INTOP. */
3106 pointer_int_sum (location_t loc
, enum tree_code resultcode
,
3107 tree ptrop
, tree intop
, bool complain
)
3111 /* The result is a pointer of the same type that is being added. */
3112 tree result_type
= TREE_TYPE (ptrop
);
3114 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3116 if (complain
&& warn_pointer_arith
)
3117 pedwarn (loc
, OPT_Wpointer_arith
,
3118 "pointer of type %<void *%> used in arithmetic");
3120 return error_mark_node
;
3121 size_exp
= integer_one_node
;
3123 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3125 if (complain
&& warn_pointer_arith
)
3126 pedwarn (loc
, OPT_Wpointer_arith
,
3127 "pointer to a function used in arithmetic");
3129 return error_mark_node
;
3130 size_exp
= integer_one_node
;
3133 size_exp
= size_in_bytes_loc (loc
, TREE_TYPE (result_type
));
3135 /* We are manipulating pointer values, so we don't need to warn
3136 about relying on undefined signed overflow. We disable the
3137 warning here because we use integer types so fold won't know that
3138 they are really pointers. */
3139 fold_defer_overflow_warnings ();
3141 /* If what we are about to multiply by the size of the elements
3142 contains a constant term, apply distributive law
3143 and multiply that constant term separately.
3144 This helps produce common subexpressions. */
3145 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3146 && !TREE_CONSTANT (intop
)
3147 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3148 && TREE_CONSTANT (size_exp
)
3149 /* If the constant comes from pointer subtraction,
3150 skip this optimization--it would cause an error. */
3151 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
3152 /* If the constant is unsigned, and smaller than the pointer size,
3153 then we must skip this optimization. This is because it could cause
3154 an overflow error if the constant is negative but INTOP is not. */
3155 && (!TYPE_UNSIGNED (TREE_TYPE (intop
))
3156 || (TYPE_PRECISION (TREE_TYPE (intop
))
3157 == TYPE_PRECISION (TREE_TYPE (ptrop
)))))
3159 enum tree_code subcode
= resultcode
;
3160 tree int_type
= TREE_TYPE (intop
);
3161 if (TREE_CODE (intop
) == MINUS_EXPR
)
3162 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3163 /* Convert both subexpression types to the type of intop,
3164 because weird cases involving pointer arithmetic
3165 can result in a sum or difference with different type args. */
3166 ptrop
= build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop
, 1)),
3168 convert (int_type
, TREE_OPERAND (intop
, 1)),
3170 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
3173 /* Convert the integer argument to a type the same size as sizetype
3174 so the multiply won't overflow spuriously. */
3175 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
)
3176 || TYPE_UNSIGNED (TREE_TYPE (intop
)) != TYPE_UNSIGNED (sizetype
))
3177 intop
= convert (c_common_type_for_size (TYPE_PRECISION (sizetype
),
3178 TYPE_UNSIGNED (sizetype
)), intop
);
3180 /* Replace the integer argument with a suitable product by the object size.
3181 Do this multiplication as signed, then convert to the appropriate type
3182 for the pointer operation and disregard an overflow that occurred only
3183 because of the sign-extension change in the latter conversion. */
3185 tree t
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (intop
), intop
,
3186 convert (TREE_TYPE (intop
), size_exp
));
3187 intop
= convert (sizetype
, t
);
3188 if (TREE_OVERFLOW_P (intop
) && !TREE_OVERFLOW (t
))
3189 intop
= wide_int_to_tree (TREE_TYPE (intop
), wi::to_wide (intop
));
3192 /* Create the sum or difference. */
3193 if (resultcode
== MINUS_EXPR
)
3194 intop
= fold_build1_loc (loc
, NEGATE_EXPR
, sizetype
, intop
);
3196 ret
= fold_build_pointer_plus_loc (loc
, ptrop
, intop
);
3198 fold_undefer_and_ignore_overflow_warnings ();
3203 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3204 and if NON_CONST is known not to be permitted in an evaluated part
3205 of a constant expression. */
3208 c_wrap_maybe_const (tree expr
, bool non_const
)
3210 bool nowarning
= TREE_NO_WARNING (expr
);
3211 location_t loc
= EXPR_LOCATION (expr
);
3213 /* This should never be called for C++. */
3214 if (c_dialect_cxx ())
3217 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3218 STRIP_TYPE_NOPS (expr
);
3219 expr
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL
, expr
);
3220 C_MAYBE_CONST_EXPR_NON_CONST (expr
) = non_const
;
3222 TREE_NO_WARNING (expr
) = 1;
3223 protected_set_expr_location (expr
, loc
);
3228 /* Return whether EXPR is a declaration whose address can never be
3232 decl_with_nonnull_addr_p (const_tree expr
)
3234 return (DECL_P (expr
)
3235 && (TREE_CODE (expr
) == PARM_DECL
3236 || TREE_CODE (expr
) == LABEL_DECL
3237 || !DECL_WEAK (expr
)));
3240 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3241 or for an `if' or `while' statement or ?..: exp. It should already
3242 have been validated to be of suitable type; otherwise, a bad
3243 diagnostic may result.
3245 The EXPR is located at LOCATION.
3247 This preparation consists of taking the ordinary
3248 representation of an expression expr and producing a valid tree
3249 boolean expression describing whether expr is nonzero. We could
3250 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3251 but we optimize comparisons, &&, ||, and !.
3253 The resulting type should always be `truthvalue_type_node'. */
3256 c_common_truthvalue_conversion (location_t location
, tree expr
)
3258 STRIP_ANY_LOCATION_WRAPPER (expr
);
3259 switch (TREE_CODE (expr
))
3261 case EQ_EXPR
: case NE_EXPR
: case UNEQ_EXPR
: case LTGT_EXPR
:
3262 case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
3263 case UNLE_EXPR
: case UNGE_EXPR
: case UNLT_EXPR
: case UNGT_EXPR
:
3264 case ORDERED_EXPR
: case UNORDERED_EXPR
:
3265 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3267 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3268 TREE_OPERAND (expr
, 0), TREE_OPERAND (expr
, 1));
3271 case TRUTH_ANDIF_EXPR
:
3272 case TRUTH_ORIF_EXPR
:
3273 case TRUTH_AND_EXPR
:
3275 case TRUTH_XOR_EXPR
:
3276 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3278 expr
= build2 (TREE_CODE (expr
), truthvalue_type_node
,
3279 c_common_truthvalue_conversion (location
,
3280 TREE_OPERAND (expr
, 0)),
3281 c_common_truthvalue_conversion (location
,
3282 TREE_OPERAND (expr
, 1)));
3285 case TRUTH_NOT_EXPR
:
3286 if (TREE_TYPE (expr
) == truthvalue_type_node
)
3288 expr
= build1 (TREE_CODE (expr
), truthvalue_type_node
,
3289 c_common_truthvalue_conversion (location
,
3290 TREE_OPERAND (expr
, 0)));
3297 if (TREE_CODE (TREE_TYPE (expr
)) == ENUMERAL_TYPE
3298 && !integer_zerop (expr
)
3299 && !integer_onep (expr
))
3300 warning_at (location
, OPT_Wint_in_bool_context
,
3301 "enum constant in boolean context");
3302 return integer_zerop (expr
) ? truthvalue_false_node
3303 : truthvalue_true_node
;
3306 return real_compare (NE_EXPR
, &TREE_REAL_CST (expr
), &dconst0
)
3307 ? truthvalue_true_node
3308 : truthvalue_false_node
;
3311 return fixed_compare (NE_EXPR
, &TREE_FIXED_CST (expr
),
3312 &FCONST0 (TYPE_MODE (TREE_TYPE (expr
))))
3313 ? truthvalue_true_node
3314 : truthvalue_false_node
;
3317 expr
= build_unary_op (location
, ADDR_EXPR
, expr
, false);
3322 tree inner
= TREE_OPERAND (expr
, 0);
3323 if (decl_with_nonnull_addr_p (inner
))
3325 /* Common Ada programmer's mistake. */
3326 warning_at (location
,
3328 "the address of %qD will always evaluate as %<true%>",
3330 return truthvalue_true_node
;
3336 expr
= build_binary_op (EXPR_LOCATION (expr
),
3337 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
3338 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3339 c_common_truthvalue_conversion (location
,
3340 TREE_OPERAND (expr
, 0)),
3341 c_common_truthvalue_conversion (location
,
3342 TREE_OPERAND (expr
, 1)),
3350 case EXCESS_PRECISION_EXPR
:
3351 /* These don't change whether an object is nonzero or zero. */
3352 return c_common_truthvalue_conversion (location
, TREE_OPERAND (expr
, 0));
3356 /* These don't change whether an object is zero or nonzero, but
3357 we can't ignore them if their second arg has side-effects. */
3358 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
3360 expr
= build2 (COMPOUND_EXPR
, truthvalue_type_node
,
3361 TREE_OPERAND (expr
, 1),
3362 c_common_truthvalue_conversion
3363 (location
, TREE_OPERAND (expr
, 0)));
3367 return c_common_truthvalue_conversion (location
,
3368 TREE_OPERAND (expr
, 0));
3371 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3372 "%<*%> in boolean context, suggest %<&&%> instead");
3376 /* We will only warn on signed shifts here, because the majority of
3377 false positive warnings happen in code where unsigned arithmetic
3378 was used in anticipation of a possible overflow.
3379 Furthermore, if we see an unsigned type here we know that the
3380 result of the shift is not subject to integer promotion rules. */
3381 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
3382 && !TYPE_UNSIGNED (TREE_TYPE (expr
)))
3383 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3384 "%<<<%> in boolean context, did you mean %<<%>?");
3388 if (warn_int_in_bool_context
3389 && !from_macro_definition_at (EXPR_LOCATION (expr
)))
3391 tree val1
= fold_for_warn (TREE_OPERAND (expr
, 1));
3392 tree val2
= fold_for_warn (TREE_OPERAND (expr
, 2));
3393 if (TREE_CODE (val1
) == INTEGER_CST
3394 && TREE_CODE (val2
) == INTEGER_CST
3395 && !integer_zerop (val1
)
3396 && !integer_zerop (val2
)
3397 && (!integer_onep (val1
)
3398 || !integer_onep (val2
)))
3399 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3400 "%<?:%> using integer constants in boolean context, "
3401 "the expression will always evaluate to %<true%>");
3402 else if ((TREE_CODE (val1
) == INTEGER_CST
3403 && !integer_zerop (val1
)
3404 && !integer_onep (val1
))
3405 || (TREE_CODE (val2
) == INTEGER_CST
3406 && !integer_zerop (val2
)
3407 && !integer_onep (val2
)))
3408 warning_at (EXPR_LOCATION (expr
), OPT_Wint_in_bool_context
,
3409 "%<?:%> using integer constants in boolean context");
3411 /* Distribute the conversion into the arms of a COND_EXPR. */
3412 if (c_dialect_cxx ())
3413 /* Avoid premature folding. */
3417 int w
= warn_int_in_bool_context
;
3418 warn_int_in_bool_context
= 0;
3419 /* Folding will happen later for C. */
3420 expr
= build3 (COND_EXPR
, truthvalue_type_node
,
3421 TREE_OPERAND (expr
, 0),
3422 c_common_truthvalue_conversion (location
,
3423 TREE_OPERAND (expr
, 1)),
3424 c_common_truthvalue_conversion (location
,
3425 TREE_OPERAND (expr
, 2)));
3426 warn_int_in_bool_context
= w
;
3432 tree totype
= TREE_TYPE (expr
);
3433 tree fromtype
= TREE_TYPE (TREE_OPERAND (expr
, 0));
3435 if (POINTER_TYPE_P (totype
)
3436 && !c_inhibit_evaluation_warnings
3437 && TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3443 warning_at (location
,
3445 "the compiler can assume that the address of "
3446 "%qD will always evaluate to %<true%>",
3450 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3451 since that affects how `default_conversion' will behave. */
3452 if (TREE_CODE (totype
) == REFERENCE_TYPE
3453 || TREE_CODE (fromtype
) == REFERENCE_TYPE
)
3455 /* Don't strip a conversion from C++0x scoped enum, since they
3456 don't implicitly convert to other types. */
3457 if (TREE_CODE (fromtype
) == ENUMERAL_TYPE
3458 && ENUM_IS_SCOPED (fromtype
))
3460 /* If this isn't narrowing the argument, we can ignore it. */
3461 if (TYPE_PRECISION (totype
) >= TYPE_PRECISION (fromtype
))
3462 return c_common_truthvalue_conversion (location
,
3463 TREE_OPERAND (expr
, 0));
3468 if (!TREE_NO_WARNING (expr
)
3470 && warning_at (location
, OPT_Wparentheses
,
3471 "suggest parentheses around assignment used as "
3473 TREE_NO_WARNING (expr
) = 1;
3478 tree folded_expr
= fold_for_warn (expr
);
3479 if (folded_expr
!= expr
)
3480 return c_common_truthvalue_conversion (location
, folded_expr
);
3488 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
3490 tree t
= save_expr (expr
);
3491 expr
= (build_binary_op
3492 (EXPR_LOCATION (expr
),
3493 (TREE_SIDE_EFFECTS (expr
)
3494 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
3495 c_common_truthvalue_conversion
3497 build_unary_op (location
, REALPART_EXPR
, t
, false)),
3498 c_common_truthvalue_conversion
3500 build_unary_op (location
, IMAGPART_EXPR
, t
, false)),
3505 if (TREE_CODE (TREE_TYPE (expr
)) == FIXED_POINT_TYPE
)
3507 tree fixed_zero_node
= build_fixed (TREE_TYPE (expr
),
3509 (TREE_TYPE (expr
))));
3510 return build_binary_op (location
, NE_EXPR
, expr
, fixed_zero_node
, true);
3513 return build_binary_op (location
, NE_EXPR
, expr
, integer_zero_node
, true);
3516 protected_set_expr_location (expr
, location
);
3520 static void def_builtin_1 (enum built_in_function fncode
,
3522 enum built_in_class fnclass
,
3523 tree fntype
, tree libtype
,
3524 bool both_p
, bool fallback_p
, bool nonansi_p
,
3525 tree fnattrs
, bool implicit_p
);
3528 /* Apply the TYPE_QUALS to the new DECL. */
3531 c_apply_type_quals_to_decl (int type_quals
, tree decl
)
3533 tree type
= TREE_TYPE (decl
);
3535 if (type
== error_mark_node
)
3538 if ((type_quals
& TYPE_QUAL_CONST
)
3539 || (type
&& TREE_CODE (type
) == REFERENCE_TYPE
))
3540 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3541 constructor can produce constant init, so rely on cp_finish_decl to
3542 clear TREE_READONLY if the variable has non-constant init. */
3543 TREE_READONLY (decl
) = 1;
3544 if (type_quals
& TYPE_QUAL_VOLATILE
)
3546 TREE_SIDE_EFFECTS (decl
) = 1;
3547 TREE_THIS_VOLATILE (decl
) = 1;
3549 if (type_quals
& TYPE_QUAL_RESTRICT
)
3551 while (type
&& TREE_CODE (type
) == ARRAY_TYPE
)
3552 /* Allow 'restrict' on arrays of pointers.
3553 FIXME currently we just ignore it. */
3554 type
= TREE_TYPE (type
);
3556 || !POINTER_TYPE_P (type
)
3557 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
)))
3558 error ("invalid use of %<restrict%>");
3562 /* Return the typed-based alias set for T, which may be an expression
3563 or a type. Return -1 if we don't do anything special. */
3566 c_common_get_alias_set (tree t
)
3568 /* For VLAs, use the alias set of the element type rather than the
3569 default of alias set 0 for types compared structurally. */
3570 if (TYPE_P (t
) && TYPE_STRUCTURAL_EQUALITY_P (t
))
3572 if (TREE_CODE (t
) == ARRAY_TYPE
)
3573 return get_alias_set (TREE_TYPE (t
));
3577 /* That's all the expressions we handle specially. */
3581 /* Unlike char, char8_t doesn't alias. */
3582 if (flag_char8_t
&& t
== char8_type_node
)
3585 /* The C standard guarantees that any object may be accessed via an
3586 lvalue that has narrow character type (except char8_t). */
3587 if (t
== char_type_node
3588 || t
== signed_char_type_node
3589 || t
== unsigned_char_type_node
)
3592 /* The C standard specifically allows aliasing between signed and
3593 unsigned variants of the same type. We treat the signed
3594 variant as canonical. */
3595 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_UNSIGNED (t
))
3597 tree t1
= c_common_signed_type (t
);
3599 /* t1 == t can happen for boolean nodes which are always unsigned. */
3601 return get_alias_set (t1
);
3607 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3608 the IS_SIZEOF parameter indicates which operator is being applied.
3609 The COMPLAIN flag controls whether we should diagnose possibly
3610 ill-formed constructs or not. LOC is the location of the SIZEOF or
3611 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3612 a type in any context should be returned, rather than the normal
3613 alignment for that type. */
3616 c_sizeof_or_alignof_type (location_t loc
,
3617 tree type
, bool is_sizeof
, bool min_alignof
,
3620 const char *op_name
;
3622 enum tree_code type_code
= TREE_CODE (type
);
3624 op_name
= is_sizeof
? "sizeof" : "__alignof__";
3626 if (type_code
== FUNCTION_TYPE
)
3630 if (complain
&& warn_pointer_arith
)
3631 pedwarn (loc
, OPT_Wpointer_arith
,
3632 "invalid application of %<sizeof%> to a function type");
3634 return error_mark_node
;
3635 value
= size_one_node
;
3641 if (c_dialect_cxx ())
3642 pedwarn (loc
, OPT_Wpedantic
, "ISO C++ does not permit "
3643 "%<alignof%> applied to a function type");
3645 pedwarn (loc
, OPT_Wpedantic
, "ISO C does not permit "
3646 "%<_Alignof%> applied to a function type");
3648 value
= size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
3651 else if (type_code
== VOID_TYPE
|| type_code
== ERROR_MARK
)
3653 if (type_code
== VOID_TYPE
3654 && complain
&& warn_pointer_arith
)
3655 pedwarn (loc
, OPT_Wpointer_arith
,
3656 "invalid application of %qs to a void type", op_name
);
3658 return error_mark_node
;
3659 value
= size_one_node
;
3661 else if (!COMPLETE_TYPE_P (type
)
3662 && (!c_dialect_cxx () || is_sizeof
|| type_code
!= ARRAY_TYPE
))
3665 error_at (loc
, "invalid application of %qs to incomplete type %qT",
3667 return error_mark_node
;
3669 else if (c_dialect_cxx () && type_code
== ARRAY_TYPE
3670 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
3673 error_at (loc
, "invalid application of %qs to array type %qT of "
3674 "incomplete element type", op_name
, type
);
3675 return error_mark_node
;
3680 /* Convert in case a char is more than one unit. */
3681 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
3682 size_int (TYPE_PRECISION (char_type_node
)
3684 else if (min_alignof
)
3685 value
= size_int (min_align_of_type (type
));
3687 value
= size_int (TYPE_ALIGN_UNIT (type
));
3690 /* VALUE will have the middle-end integer type sizetype.
3691 However, we should really return a value of type `size_t',
3692 which is just a typedef for an ordinary integer type. */
3693 value
= fold_convert_loc (loc
, size_type_node
, value
);
3698 /* Implement the __alignof keyword: Return the minimum required
3699 alignment of EXPR, measured in bytes. For VAR_DECLs,
3700 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3701 from an "aligned" __attribute__ specification). LOC is the
3702 location of the ALIGNOF operator. */
3705 c_alignof_expr (location_t loc
, tree expr
)
3709 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 unsigned", int_n_data
[i
].bitsize
);
4028 record_builtin_type (RID_MAX
, name
, int_n_trees
[i
].unsigned_type
);
4031 if (c_dialect_cxx ())
4032 record_builtin_type (RID_MAX
, "unsigned long", long_unsigned_type_node
);
4033 record_builtin_type (RID_MAX
, "long long int",
4034 long_long_integer_type_node
);
4035 record_builtin_type (RID_MAX
, "long long unsigned int",
4036 long_long_unsigned_type_node
);
4037 if (c_dialect_cxx ())
4038 record_builtin_type (RID_MAX
, "long long unsigned",
4039 long_long_unsigned_type_node
);
4040 record_builtin_type (RID_SHORT
, "short int", short_integer_type_node
);
4041 record_builtin_type (RID_MAX
, "short unsigned int",
4042 short_unsigned_type_node
);
4043 if (c_dialect_cxx ())
4044 record_builtin_type (RID_MAX
, "unsigned short",
4045 short_unsigned_type_node
);
4047 /* Define both `signed char' and `unsigned char'. */
4048 record_builtin_type (RID_MAX
, "signed char", signed_char_type_node
);
4049 record_builtin_type (RID_MAX
, "unsigned char", unsigned_char_type_node
);
4051 /* These are types that c_common_type_for_size and
4052 c_common_type_for_mode use. */
4053 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4054 TYPE_DECL
, NULL_TREE
,
4056 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4057 TYPE_DECL
, NULL_TREE
,
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 #if HOST_BITS_PER_WIDE_INT >= 64
4066 /* Note that this is different than the __int128 type that's part of
4067 the generic __intN support. */
4068 if (targetm
.scalar_mode_supported_p (TImode
))
4069 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4071 get_identifier ("__int128_t"),
4074 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4075 TYPE_DECL
, NULL_TREE
,
4076 unsigned_intQI_type_node
));
4077 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4078 TYPE_DECL
, NULL_TREE
,
4079 unsigned_intHI_type_node
));
4080 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4081 TYPE_DECL
, NULL_TREE
,
4082 unsigned_intSI_type_node
));
4083 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4084 TYPE_DECL
, NULL_TREE
,
4085 unsigned_intDI_type_node
));
4086 #if HOST_BITS_PER_WIDE_INT >= 64
4087 if (targetm
.scalar_mode_supported_p (TImode
))
4088 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4090 get_identifier ("__uint128_t"),
4091 unsigned_intTI_type_node
));
4094 /* Create the widest literal types. */
4095 if (targetm
.scalar_mode_supported_p (TImode
))
4097 widest_integer_literal_type_node
= intTI_type_node
;
4098 widest_unsigned_literal_type_node
= unsigned_intTI_type_node
;
4102 widest_integer_literal_type_node
= intDI_type_node
;
4103 widest_unsigned_literal_type_node
= unsigned_intDI_type_node
;
4106 signed_size_type_node
= c_common_signed_type (size_type_node
);
4109 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE
)));
4111 record_builtin_type (RID_FLOAT
, NULL
, float_type_node
);
4112 record_builtin_type (RID_DOUBLE
, NULL
, double_type_node
);
4113 record_builtin_type (RID_MAX
, "long double", long_double_type_node
);
4115 if (!c_dialect_cxx ())
4116 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4117 if (FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4118 record_builtin_type ((enum rid
) (RID_FLOATN_NX_FIRST
+ i
), NULL
,
4119 FLOATN_NX_TYPE_NODE (i
));
4121 /* Only supported decimal floating point extension if the target
4122 actually supports underlying modes. */
4123 if (targetm
.scalar_mode_supported_p (SDmode
)
4124 && targetm
.scalar_mode_supported_p (DDmode
)
4125 && targetm
.scalar_mode_supported_p (TDmode
))
4127 record_builtin_type (RID_DFLOAT32
, NULL
, dfloat32_type_node
);
4128 record_builtin_type (RID_DFLOAT64
, NULL
, dfloat64_type_node
);
4129 record_builtin_type (RID_DFLOAT128
, NULL
, dfloat128_type_node
);
4132 if (targetm
.fixed_point_supported_p ())
4134 record_builtin_type (RID_MAX
, "short _Fract", short_fract_type_node
);
4135 record_builtin_type (RID_FRACT
, NULL
, fract_type_node
);
4136 record_builtin_type (RID_MAX
, "long _Fract", long_fract_type_node
);
4137 record_builtin_type (RID_MAX
, "long long _Fract",
4138 long_long_fract_type_node
);
4139 record_builtin_type (RID_MAX
, "unsigned short _Fract",
4140 unsigned_short_fract_type_node
);
4141 record_builtin_type (RID_MAX
, "unsigned _Fract",
4142 unsigned_fract_type_node
);
4143 record_builtin_type (RID_MAX
, "unsigned long _Fract",
4144 unsigned_long_fract_type_node
);
4145 record_builtin_type (RID_MAX
, "unsigned long long _Fract",
4146 unsigned_long_long_fract_type_node
);
4147 record_builtin_type (RID_MAX
, "_Sat short _Fract",
4148 sat_short_fract_type_node
);
4149 record_builtin_type (RID_MAX
, "_Sat _Fract", sat_fract_type_node
);
4150 record_builtin_type (RID_MAX
, "_Sat long _Fract",
4151 sat_long_fract_type_node
);
4152 record_builtin_type (RID_MAX
, "_Sat long long _Fract",
4153 sat_long_long_fract_type_node
);
4154 record_builtin_type (RID_MAX
, "_Sat unsigned short _Fract",
4155 sat_unsigned_short_fract_type_node
);
4156 record_builtin_type (RID_MAX
, "_Sat unsigned _Fract",
4157 sat_unsigned_fract_type_node
);
4158 record_builtin_type (RID_MAX
, "_Sat unsigned long _Fract",
4159 sat_unsigned_long_fract_type_node
);
4160 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Fract",
4161 sat_unsigned_long_long_fract_type_node
);
4162 record_builtin_type (RID_MAX
, "short _Accum", short_accum_type_node
);
4163 record_builtin_type (RID_ACCUM
, NULL
, accum_type_node
);
4164 record_builtin_type (RID_MAX
, "long _Accum", long_accum_type_node
);
4165 record_builtin_type (RID_MAX
, "long long _Accum",
4166 long_long_accum_type_node
);
4167 record_builtin_type (RID_MAX
, "unsigned short _Accum",
4168 unsigned_short_accum_type_node
);
4169 record_builtin_type (RID_MAX
, "unsigned _Accum",
4170 unsigned_accum_type_node
);
4171 record_builtin_type (RID_MAX
, "unsigned long _Accum",
4172 unsigned_long_accum_type_node
);
4173 record_builtin_type (RID_MAX
, "unsigned long long _Accum",
4174 unsigned_long_long_accum_type_node
);
4175 record_builtin_type (RID_MAX
, "_Sat short _Accum",
4176 sat_short_accum_type_node
);
4177 record_builtin_type (RID_MAX
, "_Sat _Accum", sat_accum_type_node
);
4178 record_builtin_type (RID_MAX
, "_Sat long _Accum",
4179 sat_long_accum_type_node
);
4180 record_builtin_type (RID_MAX
, "_Sat long long _Accum",
4181 sat_long_long_accum_type_node
);
4182 record_builtin_type (RID_MAX
, "_Sat unsigned short _Accum",
4183 sat_unsigned_short_accum_type_node
);
4184 record_builtin_type (RID_MAX
, "_Sat unsigned _Accum",
4185 sat_unsigned_accum_type_node
);
4186 record_builtin_type (RID_MAX
, "_Sat unsigned long _Accum",
4187 sat_unsigned_long_accum_type_node
);
4188 record_builtin_type (RID_MAX
, "_Sat unsigned long long _Accum",
4189 sat_unsigned_long_long_accum_type_node
);
4193 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4195 get_identifier ("complex int"),
4196 complex_integer_type_node
));
4197 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4199 get_identifier ("complex float"),
4200 complex_float_type_node
));
4201 lang_hooks
.decls
.pushdecl (build_decl (UNKNOWN_LOCATION
,
4203 get_identifier ("complex double"),
4204 complex_double_type_node
));
4205 lang_hooks
.decls
.pushdecl
4206 (build_decl (UNKNOWN_LOCATION
,
4207 TYPE_DECL
, get_identifier ("complex long double"),
4208 complex_long_double_type_node
));
4210 if (!c_dialect_cxx ())
4211 for (i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
4212 if (COMPLEX_FLOATN_NX_TYPE_NODE (i
) != NULL_TREE
)
4215 sprintf (buf
, "complex _Float%d%s", floatn_nx_types
[i
].n
,
4216 floatn_nx_types
[i
].extended
? "x" : "");
4217 lang_hooks
.decls
.pushdecl
4218 (build_decl (UNKNOWN_LOCATION
,
4220 get_identifier (buf
),
4221 COMPLEX_FLOATN_NX_TYPE_NODE (i
)));
4224 /* Make fileptr_type_node a distinct void * type until
4225 FILE type is defined. Likewise for const struct tm*. */
4226 for (unsigned i
= 0;
4227 i
< sizeof (builtin_structptr_types
) / sizeof (builtin_structptr_type
);
4229 builtin_structptr_types
[i
].node
4230 = build_variant_type_copy (builtin_structptr_types
[i
].base
);
4232 record_builtin_type (RID_VOID
, NULL
, void_type_node
);
4234 /* Set the TYPE_NAME for any variants that were built before
4235 record_builtin_type gave names to the built-in types. */
4237 tree void_name
= TYPE_NAME (void_type_node
);
4238 TYPE_NAME (void_type_node
) = NULL_TREE
;
4239 TYPE_NAME (build_qualified_type (void_type_node
, TYPE_QUAL_CONST
))
4241 TYPE_NAME (void_type_node
) = void_name
;
4244 void_list_node
= build_void_list_node ();
4246 /* Make a type to be the domain of a few array types
4247 whose domains don't really matter.
4248 200 is small enough that it always fits in size_t
4249 and large enough that it can hold most function names for the
4250 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4251 array_domain_type
= build_index_type (size_int (200));
4253 /* Make a type for arrays of characters.
4254 With luck nothing will ever really depend on the length of this
4256 char_array_type_node
4257 = build_array_type (char_type_node
, array_domain_type
);
4259 string_type_node
= build_pointer_type (char_type_node
);
4260 const_string_type_node
4261 = build_pointer_type (build_qualified_type
4262 (char_type_node
, TYPE_QUAL_CONST
));
4264 /* This is special for C++ so functions can be overloaded. */
4265 wchar_type_node
= get_identifier (MODIFIED_WCHAR_TYPE
);
4266 wchar_type_node
= TREE_TYPE (identifier_global_value (wchar_type_node
));
4267 wchar_type_size
= TYPE_PRECISION (wchar_type_node
);
4268 underlying_wchar_type_node
= wchar_type_node
;
4269 if (c_dialect_cxx ())
4271 if (TYPE_UNSIGNED (wchar_type_node
))
4272 wchar_type_node
= make_unsigned_type (wchar_type_size
);
4274 wchar_type_node
= make_signed_type (wchar_type_size
);
4275 record_builtin_type (RID_WCHAR
, "wchar_t", wchar_type_node
);
4278 /* This is for wide string constants. */
4279 wchar_array_type_node
4280 = build_array_type (wchar_type_node
, array_domain_type
);
4282 /* Define 'char8_t'. */
4283 char8_type_node
= get_identifier (CHAR8_TYPE
);
4284 char8_type_node
= TREE_TYPE (identifier_global_value (char8_type_node
));
4285 char8_type_size
= TYPE_PRECISION (char8_type_node
);
4286 if (c_dialect_cxx ())
4288 char8_type_node
= make_unsigned_type (char8_type_size
);
4291 record_builtin_type (RID_CHAR8
, "char8_t", char8_type_node
);
4294 /* This is for UTF-8 string constants. */
4295 char8_array_type_node
4296 = build_array_type (char8_type_node
, array_domain_type
);
4298 /* Define 'char16_t'. */
4299 char16_type_node
= get_identifier (CHAR16_TYPE
);
4300 char16_type_node
= TREE_TYPE (identifier_global_value (char16_type_node
));
4301 char16_type_size
= TYPE_PRECISION (char16_type_node
);
4302 if (c_dialect_cxx ())
4304 char16_type_node
= make_unsigned_type (char16_type_size
);
4306 if (cxx_dialect
>= cxx11
)
4307 record_builtin_type (RID_CHAR16
, "char16_t", char16_type_node
);
4310 /* This is for UTF-16 string constants. */
4311 char16_array_type_node
4312 = build_array_type (char16_type_node
, array_domain_type
);
4314 /* Define 'char32_t'. */
4315 char32_type_node
= get_identifier (CHAR32_TYPE
);
4316 char32_type_node
= TREE_TYPE (identifier_global_value (char32_type_node
));
4317 char32_type_size
= TYPE_PRECISION (char32_type_node
);
4318 if (c_dialect_cxx ())
4320 char32_type_node
= make_unsigned_type (char32_type_size
);
4322 if (cxx_dialect
>= cxx11
)
4323 record_builtin_type (RID_CHAR32
, "char32_t", char32_type_node
);
4326 /* This is for UTF-32 string constants. */
4327 char32_array_type_node
4328 = build_array_type (char32_type_node
, array_domain_type
);
4331 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE
)));
4334 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE
)));
4336 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE
)));
4338 if (SIG_ATOMIC_TYPE
)
4339 sig_atomic_type_node
=
4340 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE
)));
4343 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE
)));
4346 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE
)));
4349 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE
)));
4352 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE
)));
4355 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE
)));
4357 c_uint16_type_node
= uint16_type_node
=
4358 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE
)));
4360 c_uint32_type_node
= uint32_type_node
=
4361 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE
)));
4363 c_uint64_type_node
= uint64_type_node
=
4364 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE
)));
4365 if (INT_LEAST8_TYPE
)
4366 int_least8_type_node
=
4367 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE
)));
4368 if (INT_LEAST16_TYPE
)
4369 int_least16_type_node
=
4370 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE
)));
4371 if (INT_LEAST32_TYPE
)
4372 int_least32_type_node
=
4373 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE
)));
4374 if (INT_LEAST64_TYPE
)
4375 int_least64_type_node
=
4376 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE
)));
4377 if (UINT_LEAST8_TYPE
)
4378 uint_least8_type_node
=
4379 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE
)));
4380 if (UINT_LEAST16_TYPE
)
4381 uint_least16_type_node
=
4382 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE
)));
4383 if (UINT_LEAST32_TYPE
)
4384 uint_least32_type_node
=
4385 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE
)));
4386 if (UINT_LEAST64_TYPE
)
4387 uint_least64_type_node
=
4388 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE
)));
4390 int_fast8_type_node
=
4391 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE
)));
4392 if (INT_FAST16_TYPE
)
4393 int_fast16_type_node
=
4394 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE
)));
4395 if (INT_FAST32_TYPE
)
4396 int_fast32_type_node
=
4397 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE
)));
4398 if (INT_FAST64_TYPE
)
4399 int_fast64_type_node
=
4400 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE
)));
4401 if (UINT_FAST8_TYPE
)
4402 uint_fast8_type_node
=
4403 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE
)));
4404 if (UINT_FAST16_TYPE
)
4405 uint_fast16_type_node
=
4406 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE
)));
4407 if (UINT_FAST32_TYPE
)
4408 uint_fast32_type_node
=
4409 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE
)));
4410 if (UINT_FAST64_TYPE
)
4411 uint_fast64_type_node
=
4412 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE
)));
4415 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE
)));
4418 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE
)));
4420 default_function_type
4421 = build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4422 unsigned_ptrdiff_type_node
= c_common_unsigned_type (ptrdiff_type_node
);
4424 lang_hooks
.decls
.pushdecl
4425 (build_decl (UNKNOWN_LOCATION
,
4426 TYPE_DECL
, get_identifier ("__builtin_va_list"),
4427 va_list_type_node
));
4428 if (targetm
.enum_va_list_p
)
4434 for (l
= 0; targetm
.enum_va_list_p (l
, &pname
, &ptype
); ++l
)
4436 lang_hooks
.decls
.pushdecl
4437 (build_decl (UNKNOWN_LOCATION
,
4438 TYPE_DECL
, get_identifier (pname
),
4444 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
4446 va_list_arg_type_node
= va_list_ref_type_node
=
4447 build_pointer_type (TREE_TYPE (va_list_type_node
));
4451 va_list_arg_type_node
= va_list_type_node
;
4452 va_list_ref_type_node
= build_reference_type (va_list_type_node
);
4455 if (!flag_preprocess_only
)
4456 c_define_builtins (va_list_ref_type_node
, va_list_arg_type_node
);
4458 main_identifier_node
= get_identifier ("main");
4460 /* Create the built-in __null node. It is important that this is
4462 null_node
= make_int_cst (1, 1);
4463 TREE_TYPE (null_node
) = c_common_type_for_size (POINTER_SIZE
, 0);
4465 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4466 memset (builtin_types
, 0, sizeof (builtin_types
));
4469 /* The number of named compound-literals generated thus far. */
4470 static GTY(()) int compound_literal_number
;
4472 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4475 set_compound_literal_name (tree decl
)
4478 ASM_FORMAT_PRIVATE_NAME (name
, "__compound_literal",
4479 compound_literal_number
);
4480 compound_literal_number
++;
4481 DECL_NAME (decl
) = get_identifier (name
);
4484 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4485 TYPE and operand OP. */
4488 build_va_arg_1 (location_t loc
, tree type
, tree op
)
4490 tree expr
= build1 (VA_ARG_EXPR
, type
, op
);
4491 SET_EXPR_LOCATION (expr
, loc
);
4495 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4496 va_arg (EXPR, TYPE) at source location LOC. */
4499 build_va_arg (location_t loc
, tree expr
, tree type
)
4501 tree va_type
= TREE_TYPE (expr
);
4502 tree canon_va_type
= (va_type
== error_mark_node
4504 : targetm
.canonical_va_list_type (va_type
));
4506 if (va_type
== error_mark_node
4507 || canon_va_type
== NULL_TREE
)
4509 if (canon_va_type
== NULL_TREE
)
4510 error_at (loc
, "first argument to %<va_arg%> not of type %<va_list%>");
4512 /* Let's handle things neutrallly, if expr:
4513 - has undeclared type, or
4514 - is not an va_list type. */
4515 return build_va_arg_1 (loc
, type
, error_mark_node
);
4518 if (TREE_CODE (canon_va_type
) != ARRAY_TYPE
)
4520 /* Case 1: Not an array type. */
4522 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4524 mark_addressable (expr
);
4525 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (expr
)), expr
);
4527 return build_va_arg_1 (loc
, type
, expr
);
4530 /* Case 2: Array type.
4534 For contrast, let's start with the simple case (case 1). If
4535 canon_va_type is not an array type, but say a char *, then when
4536 passing-by-value a va_list, the type of the va_list param decl is
4537 the same as for another va_list decl (all ap's are char *):
4540 D.1815 = VA_ARG (&ap, 0B, 1);
4546 __builtin_va_start (&ap, 0);
4549 __builtin_va_end (&ap);
4553 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4554 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4555 the same as for another va_list decl (case 2a, struct ap[1]).
4558 D.1844 = VA_ARG (ap, 0B, 0);
4563 __builtin_va_start (&ap, 0);
4565 __builtin_va_end (&ap);
4569 Case 2b is different because:
4570 - on the callee side, the parm decl has declared type va_list, but
4571 grokdeclarator changes the type of the parm decl to a pointer to the
4573 - on the caller side, the pass-by-value uses &ap.
4575 We unify these two cases (case 2a: va_list is array type,
4576 case 2b: va_list is pointer to array elem type), by adding '&' for the
4577 array type case, such that we have a pointer to array elem in both
4580 if (TREE_CODE (va_type
) == ARRAY_TYPE
)
4582 /* Case 2a: va_list is array type. */
4584 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4586 mark_addressable (expr
);
4587 expr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (canon_va_type
)),
4590 /* Verify that &ap is still recognized as having va_list type. */
4591 tree canon_expr_type
4592 = targetm
.canonical_va_list_type (TREE_TYPE (expr
));
4593 gcc_assert (canon_expr_type
!= NULL_TREE
);
4597 /* Case 2b: va_list is pointer to array elem type. */
4598 gcc_assert (POINTER_TYPE_P (va_type
));
4600 /* Comparison as in std_canonical_va_list_type. */
4601 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type
))
4602 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type
)));
4604 /* Don't take the address. We've already got '&ap'. */
4608 return build_va_arg_1 (loc
, type
, expr
);
4612 /* Linked list of disabled built-in functions. */
4614 struct disabled_builtin
4617 struct disabled_builtin
*next
;
4619 static disabled_builtin
*disabled_builtins
= NULL
;
4621 static bool builtin_function_disabled_p (const char *);
4623 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4624 begins with "__builtin_", give an error. */
4627 disable_builtin_function (const char *name
)
4629 if (strncmp (name
, "__builtin_", strlen ("__builtin_")) == 0)
4630 error ("cannot disable built-in function %qs", name
);
4633 disabled_builtin
*new_disabled_builtin
= XNEW (disabled_builtin
);
4634 new_disabled_builtin
->name
= name
;
4635 new_disabled_builtin
->next
= disabled_builtins
;
4636 disabled_builtins
= new_disabled_builtin
;
4641 /* Return true if the built-in function NAME has been disabled, false
4645 builtin_function_disabled_p (const char *name
)
4647 disabled_builtin
*p
;
4648 for (p
= disabled_builtins
; p
!= NULL
; p
= p
->next
)
4650 if (strcmp (name
, p
->name
) == 0)
4657 /* Worker for DEF_BUILTIN.
4658 Possibly define a builtin function with one or two names.
4659 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4660 nonansi_p and flag_no_nonansi_builtin. */
4663 def_builtin_1 (enum built_in_function fncode
,
4665 enum built_in_class fnclass
,
4666 tree fntype
, tree libtype
,
4667 bool both_p
, bool fallback_p
, bool nonansi_p
,
4668 tree fnattrs
, bool implicit_p
)
4671 const char *libname
;
4673 if (fntype
== error_mark_node
)
4676 gcc_assert ((!both_p
&& !fallback_p
)
4677 || !strncmp (name
, "__builtin_",
4678 strlen ("__builtin_")));
4680 libname
= name
+ strlen ("__builtin_");
4681 decl
= add_builtin_function (name
, fntype
, fncode
, fnclass
,
4682 (fallback_p
? libname
: NULL
),
4685 set_builtin_decl (fncode
, decl
, implicit_p
);
4688 && !flag_no_builtin
&& !builtin_function_disabled_p (libname
)
4689 && !(nonansi_p
&& flag_no_nonansi_builtin
))
4690 add_builtin_function (libname
, libtype
, fncode
, fnclass
,
4694 /* Nonzero if the type T promotes to int. This is (nearly) the
4695 integral promotions defined in ISO C99 6.3.1.1/2. */
4698 c_promoting_integer_type_p (const_tree t
)
4700 switch (TREE_CODE (t
))
4703 return (TYPE_MAIN_VARIANT (t
) == char_type_node
4704 || TYPE_MAIN_VARIANT (t
) == signed_char_type_node
4705 || TYPE_MAIN_VARIANT (t
) == unsigned_char_type_node
4706 || TYPE_MAIN_VARIANT (t
) == short_integer_type_node
4707 || TYPE_MAIN_VARIANT (t
) == short_unsigned_type_node
4708 || TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
));
4711 /* ??? Technically all enumerations not larger than an int
4712 promote to an int. But this is used along code paths
4713 that only want to notice a size change. */
4714 return TYPE_PRECISION (t
) < TYPE_PRECISION (integer_type_node
);
4724 /* Return 1 if PARMS specifies a fixed number of parameters
4725 and none of their types is affected by default promotions. */
4728 self_promoting_args_p (const_tree parms
)
4731 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
4733 tree type
= TREE_VALUE (t
);
4735 if (type
== error_mark_node
)
4738 if (TREE_CHAIN (t
) == NULL_TREE
&& type
!= void_type_node
)
4741 if (type
== NULL_TREE
)
4744 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
4747 if (c_promoting_integer_type_p (type
))
4753 /* Recursively remove any '*' or '&' operator from TYPE. */
4755 strip_pointer_operator (tree t
)
4757 while (POINTER_TYPE_P (t
))
4762 /* Recursively remove pointer or array type from TYPE. */
4764 strip_pointer_or_array_types (tree t
)
4766 while (TREE_CODE (t
) == ARRAY_TYPE
|| POINTER_TYPE_P (t
))
4771 /* Used to compare case labels. K1 and K2 are actually tree nodes
4772 representing case labels, or NULL_TREE for a `default' label.
4773 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4774 K2, and 0 if K1 and K2 are equal. */
4777 case_compare (splay_tree_key k1
, splay_tree_key k2
)
4779 /* Consider a NULL key (such as arises with a `default' label) to be
4780 smaller than anything else. */
4786 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
4789 /* Process a case label, located at LOC, for the range LOW_VALUE
4790 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4791 then this case label is actually a `default' label. If only
4792 HIGH_VALUE is NULL_TREE, then case label was declared using the
4793 usual C/C++ syntax, rather than the GNU case range extension.
4794 CASES is a tree containing all the case ranges processed so far;
4795 COND is the condition for the switch-statement itself.
4796 Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
4797 CASE_LABEL_EXPR is created. */
4800 c_add_case_label (location_t loc
, splay_tree cases
, tree cond
,
4801 tree low_value
, tree high_value
)
4806 splay_tree_node node
;
4808 /* Create the LABEL_DECL itself. */
4809 label
= create_artificial_label (loc
);
4811 /* If there was an error processing the switch condition, bail now
4812 before we get more confused. */
4813 if (!cond
|| cond
== error_mark_node
)
4816 if ((low_value
&& TREE_TYPE (low_value
)
4817 && POINTER_TYPE_P (TREE_TYPE (low_value
)))
4818 || (high_value
&& TREE_TYPE (high_value
)
4819 && POINTER_TYPE_P (TREE_TYPE (high_value
))))
4821 error_at (loc
, "pointers are not permitted as case values");
4825 /* Case ranges are a GNU extension. */
4827 pedwarn (loc
, OPT_Wpedantic
,
4828 "range expressions in switch statements are non-standard");
4830 type
= TREE_TYPE (cond
);
4833 low_value
= check_case_value (loc
, low_value
);
4834 low_value
= convert_and_check (loc
, type
, low_value
);
4835 low_value
= fold (low_value
);
4836 if (low_value
== error_mark_node
)
4841 high_value
= check_case_value (loc
, high_value
);
4842 high_value
= convert_and_check (loc
, type
, high_value
);
4843 high_value
= fold (high_value
);
4844 if (high_value
== error_mark_node
)
4848 if (low_value
&& high_value
)
4850 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4851 really a case range, even though it was written that way.
4852 Remove the HIGH_VALUE to simplify later processing. */
4853 if (tree_int_cst_equal (low_value
, high_value
))
4854 high_value
= NULL_TREE
;
4855 else if (!tree_int_cst_lt (low_value
, high_value
))
4856 warning_at (loc
, 0, "empty range specified");
4859 /* Look up the LOW_VALUE in the table of case labels we already
4861 node
= splay_tree_lookup (cases
, (splay_tree_key
) low_value
);
4862 /* If there was not an exact match, check for overlapping ranges.
4863 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4864 that's a `default' label and the only overlap is an exact match. */
4865 if (!node
&& (low_value
|| high_value
))
4867 splay_tree_node low_bound
;
4868 splay_tree_node high_bound
;
4870 /* Even though there wasn't an exact match, there might be an
4871 overlap between this case range and another case range.
4872 Since we've (inductively) not allowed any overlapping case
4873 ranges, we simply need to find the greatest low case label
4874 that is smaller that LOW_VALUE, and the smallest low case
4875 label that is greater than LOW_VALUE. If there is an overlap
4876 it will occur in one of these two ranges. */
4877 low_bound
= splay_tree_predecessor (cases
,
4878 (splay_tree_key
) low_value
);
4879 high_bound
= splay_tree_successor (cases
,
4880 (splay_tree_key
) low_value
);
4882 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4883 the LOW_VALUE, so there is no need to check unless the
4884 LOW_BOUND is in fact itself a case range. */
4886 && CASE_HIGH ((tree
) low_bound
->value
)
4887 && tree_int_cst_compare (CASE_HIGH ((tree
) low_bound
->value
),
4890 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4891 range is bigger than the low end of the current range, so we
4892 are only interested if the current range is a real range, and
4893 not an ordinary case label. */
4896 && (tree_int_cst_compare ((tree
) high_bound
->key
,
4901 /* If there was an overlap, issue an error. */
4904 tree duplicate
= CASE_LABEL ((tree
) node
->value
);
4908 error_at (loc
, "duplicate (or overlapping) case value");
4909 inform (DECL_SOURCE_LOCATION (duplicate
),
4910 "this is the first entry overlapping that value");
4914 error_at (loc
, "duplicate case value") ;
4915 inform (DECL_SOURCE_LOCATION (duplicate
), "previously used here");
4919 error_at (loc
, "multiple default labels in one switch");
4920 inform (DECL_SOURCE_LOCATION (duplicate
),
4921 "this is the first default label");
4926 /* Add a CASE_LABEL to the statement-tree. */
4927 case_label
= add_stmt (build_case_label (low_value
, high_value
, label
));
4928 /* Register this case label in the splay tree. */
4929 splay_tree_insert (cases
,
4930 (splay_tree_key
) low_value
,
4931 (splay_tree_value
) case_label
);
4936 /* Add a label so that the back-end doesn't think that the beginning of
4937 the switch is unreachable. Note that we do not add a case label, as
4938 that just leads to duplicates and thence to failure later on. */
4941 tree t
= create_artificial_label (loc
);
4942 add_stmt (build_stmt (loc
, LABEL_EXPR
, t
));
4944 return error_mark_node
;
4947 /* Subroutine of c_switch_covers_all_cases_p, called via
4948 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4949 ARGS[0] is initially NULL and after the first iteration is the
4950 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4954 c_switch_covers_all_cases_p_1 (splay_tree_node node
, void *data
)
4956 tree label
= (tree
) node
->value
;
4957 tree
*args
= (tree
*) data
;
4959 /* If there is a default case, we shouldn't have called this. */
4960 gcc_assert (CASE_LOW (label
));
4962 if (args
[0] == NULL_TREE
)
4964 if (wi::to_widest (args
[1]) < wi::to_widest (CASE_LOW (label
)))
4967 else if (wi::add (wi::to_widest (args
[0]), 1)
4968 != wi::to_widest (CASE_LOW (label
)))
4970 if (CASE_HIGH (label
))
4971 args
[0] = CASE_HIGH (label
);
4973 args
[0] = CASE_LOW (label
);
4977 /* Return true if switch with CASES and switch condition with type
4978 covers all possible values in the case labels. */
4981 c_switch_covers_all_cases_p (splay_tree cases
, tree type
)
4983 /* If there is default:, this is always the case. */
4984 splay_tree_node default_node
4985 = splay_tree_lookup (cases
, (splay_tree_key
) NULL
);
4989 if (!INTEGRAL_TYPE_P (type
))
4992 tree args
[2] = { NULL_TREE
, TYPE_MIN_VALUE (type
) };
4993 if (splay_tree_foreach (cases
, c_switch_covers_all_cases_p_1
, args
))
4996 /* If there are no cases at all, or if the highest case label
4997 is smaller than TYPE_MAX_VALUE, return false. */
4998 if (args
[0] == NULL_TREE
4999 || wi::to_widest (args
[0]) < wi::to_widest (TYPE_MAX_VALUE (type
)))
5005 /* Finish an expression taking the address of LABEL (an
5006 IDENTIFIER_NODE). Returns an expression for the address.
5008 LOC is the location for the expression returned. */
5011 finish_label_address_expr (tree label
, location_t loc
)
5015 pedwarn (input_location
, OPT_Wpedantic
, "taking the address of a label is non-standard");
5017 if (label
== error_mark_node
)
5018 return error_mark_node
;
5020 label
= lookup_label (label
);
5021 if (label
== NULL_TREE
)
5022 result
= null_pointer_node
;
5025 TREE_USED (label
) = 1;
5026 result
= build1 (ADDR_EXPR
, ptr_type_node
, label
);
5027 /* The current function is not necessarily uninlinable.
5028 Computed gotos are incompatible with inlining, but the value
5029 here could be used only in a diagnostic, for example. */
5030 protected_set_expr_location (result
, loc
);
5037 /* Given a boolean expression ARG, return a tree representing an increment
5038 or decrement (as indicated by CODE) of ARG. The front end must check for
5039 invalid cases (e.g., decrement in C++). */
5041 boolean_increment (enum tree_code code
, tree arg
)
5044 tree true_res
= build_int_cst (TREE_TYPE (arg
), 1);
5046 arg
= stabilize_reference (arg
);
5049 case PREINCREMENT_EXPR
:
5050 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5052 case POSTINCREMENT_EXPR
:
5053 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
, true_res
);
5054 arg
= save_expr (arg
);
5055 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5056 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5058 case PREDECREMENT_EXPR
:
5059 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5060 invert_truthvalue_loc (input_location
, arg
));
5062 case POSTDECREMENT_EXPR
:
5063 val
= build2 (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
5064 invert_truthvalue_loc (input_location
, arg
));
5065 arg
= save_expr (arg
);
5066 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
5067 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
5072 TREE_SIDE_EFFECTS (val
) = 1;
5076 /* Built-in macros for stddef.h and stdint.h, that require macros
5077 defined in this file. */
5079 c_stddef_cpp_builtins(void)
5081 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE
, 0);
5082 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
, 0);
5083 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE
, 0);
5084 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE
, 0);
5085 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE
, 0);
5086 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE
, 0);
5088 builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE
, 0);
5089 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE
, 0);
5090 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE
, 0);
5091 if (SIG_ATOMIC_TYPE
)
5092 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE
, 0);
5094 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE
, 0);
5096 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE
, 0);
5098 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE
, 0);
5100 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE
, 0);
5102 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE
, 0);
5104 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE
, 0);
5106 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE
, 0);
5108 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE
, 0);
5109 if (INT_LEAST8_TYPE
)
5110 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE
, 0);
5111 if (INT_LEAST16_TYPE
)
5112 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE
, 0);
5113 if (INT_LEAST32_TYPE
)
5114 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE
, 0);
5115 if (INT_LEAST64_TYPE
)
5116 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE
, 0);
5117 if (UINT_LEAST8_TYPE
)
5118 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE
, 0);
5119 if (UINT_LEAST16_TYPE
)
5120 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE
, 0);
5121 if (UINT_LEAST32_TYPE
)
5122 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE
, 0);
5123 if (UINT_LEAST64_TYPE
)
5124 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE
, 0);
5126 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE
, 0);
5127 if (INT_FAST16_TYPE
)
5128 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE
, 0);
5129 if (INT_FAST32_TYPE
)
5130 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE
, 0);
5131 if (INT_FAST64_TYPE
)
5132 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE
, 0);
5133 if (UINT_FAST8_TYPE
)
5134 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE
, 0);
5135 if (UINT_FAST16_TYPE
)
5136 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE
, 0);
5137 if (UINT_FAST32_TYPE
)
5138 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE
, 0);
5139 if (UINT_FAST64_TYPE
)
5140 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE
, 0);
5142 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE
, 0);
5144 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE
, 0);
5148 c_init_attributes (void)
5150 /* Fill in the built_in_attributes array. */
5151 #define DEF_ATTR_NULL_TREE(ENUM) \
5152 built_in_attributes[(int) ENUM] = NULL_TREE;
5153 #define DEF_ATTR_INT(ENUM, VALUE) \
5154 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5155 #define DEF_ATTR_STRING(ENUM, VALUE) \
5156 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5157 #define DEF_ATTR_IDENT(ENUM, STRING) \
5158 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5159 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5160 built_in_attributes[(int) ENUM] \
5161 = tree_cons (built_in_attributes[(int) PURPOSE], \
5162 built_in_attributes[(int) VALUE], \
5163 built_in_attributes[(int) CHAIN]);
5164 #include "builtin-attrs.def"
5165 #undef DEF_ATTR_NULL_TREE
5167 #undef DEF_ATTR_IDENT
5168 #undef DEF_ATTR_TREE_LIST
5171 /* Check whether the byte alignment ALIGN is a valid user-specified
5172 alignment less than the supported maximum. If so, return ALIGN's
5173 base-2 log; if not, output an error and return -1. If OBJFILE
5174 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5175 converted to bits. Otherwise, consider valid only alignments
5176 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5177 Zero is not considered a valid argument (and results in -1 on
5178 return) but it only triggers a warning when WARN_ZERO is set. */
5181 check_user_alignment (const_tree align
, bool objfile
, bool warn_zero
)
5183 if (error_operand_p (align
))
5186 if (TREE_CODE (align
) != INTEGER_CST
5187 || !INTEGRAL_TYPE_P (TREE_TYPE (align
)))
5189 error ("requested alignment is not an integer constant");
5193 if (integer_zerop (align
))
5196 warning (OPT_Wattributes
,
5197 "requested alignment %qE is not a positive power of 2",
5202 /* Log2 of the byte alignment ALIGN. */
5204 if (tree_int_cst_sgn (align
) == -1
5205 || (log2align
= tree_log2 (align
)) == -1)
5207 error ("requested alignment %qE is not a positive power of 2",
5214 unsigned maxalign
= MAX_OFILE_ALIGNMENT
/ BITS_PER_UNIT
;
5215 if (!tree_fits_uhwi_p (align
) || tree_to_uhwi (align
) > maxalign
)
5217 error ("requested alignment %qE exceeds object file maximum %u",
5223 if (log2align
>= HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
)
5225 error ("requested alignment %qE exceeds maximum %u",
5226 align
, 1U << (HOST_BITS_PER_INT
- LOG2_BITS_PER_UNIT
- 1));
5233 /* Determine the ELF symbol visibility for DECL, which is either a
5234 variable or a function. It is an error to use this function if a
5235 definition of DECL is not available in this translation unit.
5236 Returns true if the final visibility has been determined by this
5237 function; false if the caller is free to make additional
5241 c_determine_visibility (tree decl
)
5243 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl
));
5245 /* If the user explicitly specified the visibility with an
5246 attribute, honor that. DECL_VISIBILITY will have been set during
5247 the processing of the attribute. We check for an explicit
5248 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5249 to distinguish the use of an attribute from the use of a "#pragma
5250 GCC visibility push(...)"; in the latter case we still want other
5251 considerations to be able to overrule the #pragma. */
5252 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl
))
5253 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5254 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
))
5255 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))))
5258 /* Set default visibility to whatever the user supplied with
5259 visibility_specified depending on #pragma GCC visibility. */
5260 if (!DECL_VISIBILITY_SPECIFIED (decl
))
5262 if (visibility_options
.inpragma
5263 || DECL_VISIBILITY (decl
) != default_visibility
)
5265 DECL_VISIBILITY (decl
) = default_visibility
;
5266 DECL_VISIBILITY_SPECIFIED (decl
) = visibility_options
.inpragma
;
5267 /* If visibility changed and DECL already has DECL_RTL, ensure
5268 symbol flags are updated. */
5269 if (((VAR_P (decl
) && TREE_STATIC (decl
))
5270 || TREE_CODE (decl
) == FUNCTION_DECL
)
5271 && DECL_RTL_SET_P (decl
))
5272 make_decl_rtl (decl
);
5278 /* Data to communicate through check_function_arguments_recurse between
5279 check_function_nonnull and check_nonnull_arg. */
5281 struct nonnull_arg_ctx
5287 /* Check the argument list of a function call for null in argument slots
5288 that are marked as requiring a non-null pointer argument. The NARGS
5289 arguments are passed in the array ARGARRAY. Return true if we have
5293 check_function_nonnull (location_t loc
, tree attrs
, int nargs
, tree
*argarray
)
5298 attrs
= lookup_attribute ("nonnull", attrs
);
5299 if (attrs
== NULL_TREE
)
5303 /* See if any of the nonnull attributes has no arguments. If so,
5304 then every pointer argument is checked (in which case the check
5305 for pointer type is done in check_nonnull_arg). */
5306 if (TREE_VALUE (a
) != NULL_TREE
)
5308 a
= lookup_attribute ("nonnull", TREE_CHAIN (a
));
5309 while (a
!= NULL_TREE
&& TREE_VALUE (a
) != NULL_TREE
);
5311 struct nonnull_arg_ctx ctx
= { loc
, false };
5313 for (i
= 0; i
< nargs
; i
++)
5314 check_function_arguments_recurse (check_nonnull_arg
, &ctx
, argarray
[i
],
5318 /* Walk the argument list. If we encounter an argument number we
5319 should check for non-null, do it. */
5320 for (i
= 0; i
< nargs
; i
++)
5322 for (a
= attrs
; ; a
= TREE_CHAIN (a
))
5324 a
= lookup_attribute ("nonnull", a
);
5325 if (a
== NULL_TREE
|| nonnull_check_p (TREE_VALUE (a
), i
+ 1))
5330 check_function_arguments_recurse (check_nonnull_arg
, &ctx
,
5331 argarray
[i
], i
+ 1);
5334 return ctx
.warned_p
;
5337 /* Check that the Nth argument of a function call (counting backwards
5338 from the end) is a (pointer)0. The NARGS arguments are passed in the
5342 check_function_sentinel (const_tree fntype
, int nargs
, tree
*argarray
)
5344 tree attr
= lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype
));
5351 function_args_iterator iter
;
5354 /* Skip over the named arguments. */
5355 FOREACH_FUNCTION_ARGS (fntype
, t
, iter
)
5362 if (TREE_VALUE (attr
))
5364 tree p
= TREE_VALUE (TREE_VALUE (attr
));
5365 pos
= TREE_INT_CST_LOW (p
);
5368 /* The sentinel must be one of the varargs, i.e.
5369 in position >= the number of fixed arguments. */
5370 if ((nargs
- 1 - pos
) < len
)
5372 warning (OPT_Wformat_
,
5373 "not enough variable arguments to fit a sentinel");
5377 /* Validate the sentinel. */
5378 sentinel
= fold_for_warn (argarray
[nargs
- 1 - pos
]);
5379 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel
))
5380 || !integer_zerop (sentinel
))
5381 /* Although __null (in C++) is only an integer we allow it
5382 nevertheless, as we are guaranteed that it's exactly
5383 as wide as a pointer, and we don't want to force
5384 users to cast the NULL they have written there.
5385 We warn with -Wstrict-null-sentinel, though. */
5386 && (warn_strict_null_sentinel
|| null_node
!= sentinel
))
5387 warning (OPT_Wformat_
, "missing sentinel in function call");
5391 /* Check that the same argument isn't passed to two or more
5392 restrict-qualified formal and issue a -Wrestrict warning
5393 if it is. Return true if a warning has been issued. */
5396 check_function_restrict (const_tree fndecl
, const_tree fntype
,
5397 int nargs
, tree
*unfolded_argarray
)
5400 tree parms
= TYPE_ARG_TYPES (fntype
);
5402 /* Call fold_for_warn on all of the arguments. */
5403 auto_vec
<tree
> argarray (nargs
);
5404 for (i
= 0; i
< nargs
; i
++)
5405 argarray
.quick_push (fold_for_warn (unfolded_argarray
[i
]));
5408 && TREE_CODE (fndecl
) == FUNCTION_DECL
)
5410 /* Avoid diagnosing calls built-ins with a zero size/bound
5411 here. They are checked in more detail elsewhere. */
5412 if (fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
5414 && TREE_CODE (argarray
[2]) == INTEGER_CST
5415 && integer_zerop (argarray
[2]))
5418 if (DECL_ARGUMENTS (fndecl
))
5419 parms
= DECL_ARGUMENTS (fndecl
);
5422 for (i
= 0; i
< nargs
; i
++)
5423 TREE_VISITED (argarray
[i
]) = 0;
5425 bool warned
= false;
5427 for (i
= 0; i
< nargs
&& parms
&& parms
!= void_list_node
; i
++)
5430 if (TREE_CODE (parms
) == PARM_DECL
)
5432 type
= TREE_TYPE (parms
);
5433 parms
= DECL_CHAIN (parms
);
5437 type
= TREE_VALUE (parms
);
5438 parms
= TREE_CHAIN (parms
);
5440 if (POINTER_TYPE_P (type
)
5441 && TYPE_RESTRICT (type
)
5442 && !TYPE_READONLY (TREE_TYPE (type
)))
5443 warned
|= warn_for_restrict (i
, argarray
.address (), nargs
);
5446 for (i
= 0; i
< nargs
; i
++)
5447 TREE_VISITED (argarray
[i
]) = 0;
5452 /* Helper for check_function_nonnull; given a list of operands which
5453 must be non-null in ARGS, determine if operand PARAM_NUM should be
5457 nonnull_check_p (tree args
, unsigned HOST_WIDE_INT param_num
)
5459 unsigned HOST_WIDE_INT arg_num
= 0;
5461 for (; args
; args
= TREE_CHAIN (args
))
5463 bool found
= get_nonnull_operand (TREE_VALUE (args
), &arg_num
);
5467 if (arg_num
== param_num
)
5473 /* Check that the function argument PARAM (which is operand number
5474 PARAM_NUM) is non-null. This is called by check_function_nonnull
5475 via check_function_arguments_recurse. */
5478 check_nonnull_arg (void *ctx
, tree param
, unsigned HOST_WIDE_INT param_num
)
5480 struct nonnull_arg_ctx
*pctx
= (struct nonnull_arg_ctx
*) ctx
;
5482 /* Just skip checking the argument if it's not a pointer. This can
5483 happen if the "nonnull" attribute was given without an operand
5484 list (which means to check every pointer argument). */
5486 if (TREE_CODE (TREE_TYPE (param
)) != POINTER_TYPE
)
5489 /* Diagnose the simple cases of null arguments. */
5490 if (integer_zerop (fold_for_warn (param
)))
5492 warning_at (pctx
->loc
, OPT_Wnonnull
, "null argument where non-null "
5493 "required (argument %lu)", (unsigned long) param_num
);
5494 pctx
->warned_p
= true;
5498 /* Helper for nonnull attribute handling; fetch the operand number
5499 from the attribute argument list. */
5502 get_nonnull_operand (tree arg_num_expr
, unsigned HOST_WIDE_INT
*valp
)
5504 /* Verify the arg number is a small constant. */
5505 if (tree_fits_uhwi_p (arg_num_expr
))
5507 *valp
= tree_to_uhwi (arg_num_expr
);
5514 /* Arguments being collected for optimization. */
5515 typedef const char *const_char_p
; /* For DEF_VEC_P. */
5516 static GTY(()) vec
<const_char_p
, va_gc
> *optimize_args
;
5519 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5520 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5521 false for #pragma GCC optimize. */
5524 parse_optimize_options (tree args
, bool attr_p
)
5529 const char **opt_argv
;
5530 struct cl_decoded_option
*decoded_options
;
5531 unsigned int decoded_options_count
;
5534 /* Build up argv vector. Just in case the string is stored away, use garbage
5535 collected strings. */
5536 vec_safe_truncate (optimize_args
, 0);
5537 vec_safe_push (optimize_args
, (const char *) NULL
);
5539 for (ap
= args
; ap
!= NULL_TREE
; ap
= TREE_CHAIN (ap
))
5541 tree value
= TREE_VALUE (ap
);
5543 if (TREE_CODE (value
) == INTEGER_CST
)
5546 sprintf (buffer
, "-O%ld", (long) TREE_INT_CST_LOW (value
));
5547 vec_safe_push (optimize_args
, ggc_strdup (buffer
));
5550 else if (TREE_CODE (value
) == STRING_CST
)
5552 /* Split string into multiple substrings. */
5553 size_t len
= TREE_STRING_LENGTH (value
);
5554 char *p
= ASTRDUP (TREE_STRING_POINTER (value
));
5555 char *end
= p
+ len
;
5559 while (next_p
!= NULL
)
5565 comma
= strchr (p
, ',');
5578 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5580 if (*p
== '-' && p
[1] != 'O' && p
[1] != 'f')
5584 warning (OPT_Wattributes
,
5585 "bad option %qs to attribute %<optimize%>", p
);
5587 warning (OPT_Wpragmas
,
5588 "bad option %qs to pragma %<optimize%>", p
);
5592 /* Can't use GC memory here, see PR88007. */
5593 r
= q
= XOBNEWVEC (&opts_obstack
, char, len2
+ 3);
5599 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5600 itself is -Os, and any other switch begins with a -f. */
5601 if ((*p
>= '0' && *p
<= '9')
5602 || (p
[0] == 's' && p
[1] == '\0'))
5608 memcpy (r
, p
, len2
);
5610 vec_safe_push (optimize_args
, (const char *) q
);
5616 opt_argc
= optimize_args
->length ();
5617 opt_argv
= (const char **) alloca (sizeof (char *) * (opt_argc
+ 1));
5619 for (i
= 1; i
< opt_argc
; i
++)
5620 opt_argv
[i
] = (*optimize_args
)[i
];
5622 /* Now parse the options. */
5623 decode_cmdline_options_to_array_default_mask (opt_argc
, opt_argv
,
5625 &decoded_options_count
);
5626 /* Drop non-Optimization options. */
5628 for (i
= 1; i
< decoded_options_count
; ++i
)
5630 if (! (cl_options
[decoded_options
[i
].opt_index
].flags
& CL_OPTIMIZATION
))
5634 warning (OPT_Wattributes
,
5635 "bad option %qs to attribute %<optimize%>",
5636 decoded_options
[i
].orig_option_with_args_text
);
5638 warning (OPT_Wpragmas
,
5639 "bad option %qs to pragma %<optimize%>",
5640 decoded_options
[i
].orig_option_with_args_text
);
5644 decoded_options
[j
] = decoded_options
[i
];
5647 decoded_options_count
= j
;
5648 /* And apply them. */
5649 decode_options (&global_options
, &global_options_set
,
5650 decoded_options
, decoded_options_count
,
5651 input_location
, global_dc
, NULL
);
5653 targetm
.override_options_after_change();
5655 optimize_args
->truncate (0);
5659 /* Check whether ATTR is a valid attribute fallthrough. */
5662 attribute_fallthrough_p (tree attr
)
5664 if (attr
== error_mark_node
)
5666 tree t
= lookup_attribute ("fallthrough", attr
);
5669 /* This attribute shall appear at most once in each attribute-list. */
5670 if (lookup_attribute ("fallthrough", TREE_CHAIN (t
)))
5671 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified multiple "
5673 /* No attribute-argument-clause shall be present. */
5674 else if (TREE_VALUE (t
) != NULL_TREE
)
5675 warning (OPT_Wattributes
, "%<fallthrough%> attribute specified with "
5677 /* Warn if other attributes are found. */
5678 for (t
= attr
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
5680 tree name
= get_attribute_name (t
);
5681 if (!is_attribute_p ("fallthrough", name
))
5682 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
5688 /* Check for valid arguments being passed to a function with FNTYPE.
5689 There are NARGS arguments in the array ARGARRAY. LOC should be used
5690 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5693 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5694 to preserve location wrappers); checks that require folded arguments
5695 should call fold_for_warn on them. */
5698 check_function_arguments (location_t loc
, const_tree fndecl
, const_tree fntype
,
5699 int nargs
, tree
*argarray
, vec
<location_t
> *arglocs
)
5701 bool warned_p
= false;
5703 /* Check for null being passed in a pointer argument that must be
5704 non-null. We also need to do this if format checking is enabled. */
5707 warned_p
= check_function_nonnull (loc
, TYPE_ATTRIBUTES (fntype
),
5710 /* Check for errors in format strings. */
5712 if (warn_format
|| warn_suggest_attribute_format
)
5713 check_function_format (fntype
, TYPE_ATTRIBUTES (fntype
), nargs
, argarray
,
5717 check_function_sentinel (fntype
, nargs
, argarray
);
5720 warned_p
|= check_function_restrict (fndecl
, fntype
, nargs
, argarray
);
5724 /* Generic argument checking recursion routine. PARAM is the argument to
5725 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5726 once the argument is resolved. CTX is context for the callback. */
5728 check_function_arguments_recurse (void (*callback
)
5729 (void *, tree
, unsigned HOST_WIDE_INT
),
5730 void *ctx
, tree param
,
5731 unsigned HOST_WIDE_INT param_num
)
5733 if (CONVERT_EXPR_P (param
)
5734 && (TYPE_PRECISION (TREE_TYPE (param
))
5735 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param
, 0)))))
5737 /* Strip coercion. */
5738 check_function_arguments_recurse (callback
, ctx
,
5739 TREE_OPERAND (param
, 0), param_num
);
5743 if (TREE_CODE (param
) == CALL_EXPR
)
5745 tree type
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param
)));
5747 bool found_format_arg
= false;
5749 /* See if this is a call to a known internationalization function
5750 that modifies a format arg. Such a function may have multiple
5751 format_arg attributes (for example, ngettext). */
5753 for (attrs
= TYPE_ATTRIBUTES (type
);
5755 attrs
= TREE_CHAIN (attrs
))
5756 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs
)))
5759 tree format_num_expr
;
5762 call_expr_arg_iterator iter
;
5764 /* Extract the argument number, which was previously checked
5766 format_num_expr
= TREE_VALUE (TREE_VALUE (attrs
));
5768 format_num
= tree_to_uhwi (format_num_expr
);
5770 for (inner_arg
= first_call_expr_arg (param
, &iter
), i
= 1;
5771 inner_arg
!= NULL_TREE
;
5772 inner_arg
= next_call_expr_arg (&iter
), i
++)
5773 if (i
== format_num
)
5775 check_function_arguments_recurse (callback
, ctx
,
5776 inner_arg
, param_num
);
5777 found_format_arg
= true;
5782 /* If we found a format_arg attribute and did a recursive check,
5783 we are done with checking this argument. Otherwise, we continue
5784 and this will be considered a non-literal. */
5785 if (found_format_arg
)
5789 if (TREE_CODE (param
) == COND_EXPR
)
5791 /* Simplify to avoid warning for an impossible case. */
5792 param
= fold_for_warn (param
);
5793 if (TREE_CODE (param
) == COND_EXPR
)
5795 /* Check both halves of the conditional expression. */
5796 check_function_arguments_recurse (callback
, ctx
,
5797 TREE_OPERAND (param
, 1),
5799 check_function_arguments_recurse (callback
, ctx
,
5800 TREE_OPERAND (param
, 2),
5806 (*callback
) (ctx
, param
, param_num
);
5809 /* Checks for a builtin function FNDECL that the number of arguments
5810 NARGS against the required number REQUIRED and issues an error if
5811 there is a mismatch. Returns true if the number of arguments is
5812 correct, otherwise false. LOC is the location of FNDECL. */
5815 builtin_function_validate_nargs (location_t loc
, tree fndecl
, int nargs
,
5818 if (nargs
< required
)
5820 error_at (loc
, "too few arguments to function %qE", fndecl
);
5823 else if (nargs
> required
)
5825 error_at (loc
, "too many arguments to function %qE", fndecl
);
5831 /* Helper macro for check_builtin_function_arguments. */
5832 #define ARG_LOCATION(N) \
5833 (arg_loc.is_empty () \
5834 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5835 : expansion_point_location (arg_loc[(N)]))
5837 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5838 Returns false if there was an error, otherwise true. LOC is the
5839 location of the function; ARG_LOC is a vector of locations of the
5843 check_builtin_function_arguments (location_t loc
, vec
<location_t
> arg_loc
,
5844 tree fndecl
, int nargs
, tree
*args
)
5846 if (!fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
5849 switch (DECL_FUNCTION_CODE (fndecl
))
5851 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
:
5852 if (!tree_fits_uhwi_p (args
[2]))
5854 error_at (ARG_LOCATION (2),
5855 "third argument to function %qE must be a constant integer",
5861 case BUILT_IN_ALLOCA_WITH_ALIGN
:
5863 /* Get the requested alignment (in bits) if it's a constant
5864 integer expression. */
5865 unsigned HOST_WIDE_INT align
5866 = tree_fits_uhwi_p (args
[1]) ? tree_to_uhwi (args
[1]) : 0;
5868 /* Determine if the requested alignment is a power of 2. */
5869 if ((align
& (align
- 1)))
5872 /* The maximum alignment in bits corresponding to the same
5873 maximum in bytes enforced in check_user_alignment(). */
5874 unsigned maxalign
= (UINT_MAX
>> 1) + 1;
5876 /* Reject invalid alignments. */
5877 if (align
< BITS_PER_UNIT
|| maxalign
< align
)
5879 error_at (ARG_LOCATION (1),
5880 "second argument to function %qE must be a constant "
5881 "integer power of 2 between %qi and %qu bits",
5882 fndecl
, BITS_PER_UNIT
, maxalign
);
5888 case BUILT_IN_CONSTANT_P
:
5889 return builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1);
5891 case BUILT_IN_ISFINITE
:
5892 case BUILT_IN_ISINF
:
5893 case BUILT_IN_ISINF_SIGN
:
5894 case BUILT_IN_ISNAN
:
5895 case BUILT_IN_ISNORMAL
:
5896 case BUILT_IN_SIGNBIT
:
5897 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 1))
5899 if (TREE_CODE (TREE_TYPE (args
[0])) != REAL_TYPE
)
5901 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5902 "call to function %qE", fndecl
);
5909 case BUILT_IN_ISGREATER
:
5910 case BUILT_IN_ISGREATEREQUAL
:
5911 case BUILT_IN_ISLESS
:
5912 case BUILT_IN_ISLESSEQUAL
:
5913 case BUILT_IN_ISLESSGREATER
:
5914 case BUILT_IN_ISUNORDERED
:
5915 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2))
5917 enum tree_code code0
, code1
;
5918 code0
= TREE_CODE (TREE_TYPE (args
[0]));
5919 code1
= TREE_CODE (TREE_TYPE (args
[1]));
5920 if (!((code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
5921 || (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
5922 || (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)))
5924 error_at (loc
, "non-floating-point arguments in call to "
5925 "function %qE", fndecl
);
5932 case BUILT_IN_FPCLASSIFY
:
5933 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 6))
5935 for (unsigned int i
= 0; i
< 5; i
++)
5936 if (TREE_CODE (args
[i
]) != INTEGER_CST
)
5938 error_at (ARG_LOCATION (i
), "non-const integer argument %u in "
5939 "call to function %qE", i
+ 1, fndecl
);
5943 if (TREE_CODE (TREE_TYPE (args
[5])) != REAL_TYPE
)
5945 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5946 "call to function %qE", fndecl
);
5953 case BUILT_IN_ASSUME_ALIGNED
:
5954 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 2 + (nargs
> 2)))
5956 if (nargs
>= 3 && TREE_CODE (TREE_TYPE (args
[2])) != INTEGER_TYPE
)
5958 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5959 "function %qE", fndecl
);
5966 case BUILT_IN_ADD_OVERFLOW
:
5967 case BUILT_IN_SUB_OVERFLOW
:
5968 case BUILT_IN_MUL_OVERFLOW
:
5969 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
5972 for (i
= 0; i
< 2; i
++)
5973 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
5975 error_at (ARG_LOCATION (i
), "argument %u in call to function "
5976 "%qE does not have integral type", i
+ 1, fndecl
);
5979 if (TREE_CODE (TREE_TYPE (args
[2])) != POINTER_TYPE
5980 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args
[2]))))
5982 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5983 "does not have pointer to integral type", fndecl
);
5986 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == ENUMERAL_TYPE
)
5988 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5989 "has pointer to enumerated type", fndecl
);
5992 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args
[2]))) == BOOLEAN_TYPE
)
5994 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5995 "has pointer to boolean type", fndecl
);
6002 case BUILT_IN_ADD_OVERFLOW_P
:
6003 case BUILT_IN_SUB_OVERFLOW_P
:
6004 case BUILT_IN_MUL_OVERFLOW_P
:
6005 if (builtin_function_validate_nargs (loc
, fndecl
, nargs
, 3))
6008 for (i
= 0; i
< 3; i
++)
6009 if (!INTEGRAL_TYPE_P (TREE_TYPE (args
[i
])))
6011 error_at (ARG_LOCATION (i
), "argument %u in call to function "
6012 "%qE does not have integral type", i
+ 1, fndecl
);
6015 if (TREE_CODE (TREE_TYPE (args
[2])) == ENUMERAL_TYPE
)
6017 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6018 "%qE has enumerated type", fndecl
);
6021 else if (TREE_CODE (TREE_TYPE (args
[2])) == BOOLEAN_TYPE
)
6023 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6024 "%qE has boolean type", fndecl
);
6036 /* Subroutine of c_parse_error.
6037 Return the result of concatenating LHS and RHS. RHS is really
6038 a string literal, its first character is indicated by RHS_START and
6039 RHS_SIZE is its length (including the terminating NUL character).
6041 The caller is responsible for deleting the returned pointer. */
6044 catenate_strings (const char *lhs
, const char *rhs_start
, int rhs_size
)
6046 const size_t lhs_size
= strlen (lhs
);
6047 char *result
= XNEWVEC (char, lhs_size
+ rhs_size
);
6048 memcpy (result
, lhs
, lhs_size
);
6049 memcpy (result
+ lhs_size
, rhs_start
, rhs_size
);
6053 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6054 before TOKEN, which had the associated VALUE. */
6057 c_parse_error (const char *gmsgid
, enum cpp_ttype token_type
,
6058 tree value
, unsigned char token_flags
,
6059 rich_location
*richloc
)
6061 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6063 char *message
= NULL
;
6065 if (token_type
== CPP_EOF
)
6066 message
= catenate_messages (gmsgid
, " at end of input");
6067 else if (token_type
== CPP_CHAR
6068 || token_type
== CPP_WCHAR
6069 || token_type
== CPP_CHAR16
6070 || token_type
== CPP_CHAR32
6071 || token_type
== CPP_UTF8CHAR
)
6073 unsigned int val
= TREE_INT_CST_LOW (value
);
6095 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
6096 message
= catenate_messages (gmsgid
, " before %s'%c'");
6098 message
= catenate_messages (gmsgid
, " before %s'\\x%x'");
6100 error_at (richloc
, message
, prefix
, val
);
6104 else if (token_type
== CPP_CHAR_USERDEF
6105 || token_type
== CPP_WCHAR_USERDEF
6106 || token_type
== CPP_CHAR16_USERDEF
6107 || token_type
== CPP_CHAR32_USERDEF
6108 || token_type
== CPP_UTF8CHAR_USERDEF
)
6109 message
= catenate_messages (gmsgid
,
6110 " before user-defined character literal");
6111 else if (token_type
== CPP_STRING_USERDEF
6112 || token_type
== CPP_WSTRING_USERDEF
6113 || token_type
== CPP_STRING16_USERDEF
6114 || token_type
== CPP_STRING32_USERDEF
6115 || token_type
== CPP_UTF8STRING_USERDEF
)
6116 message
= catenate_messages (gmsgid
, " before user-defined string literal");
6117 else if (token_type
== CPP_STRING
6118 || token_type
== CPP_WSTRING
6119 || token_type
== CPP_STRING16
6120 || token_type
== CPP_STRING32
6121 || token_type
== CPP_UTF8STRING
)
6122 message
= catenate_messages (gmsgid
, " before string constant");
6123 else if (token_type
== CPP_NUMBER
)
6124 message
= catenate_messages (gmsgid
, " before numeric constant");
6125 else if (token_type
== CPP_NAME
)
6127 message
= catenate_messages (gmsgid
, " before %qE");
6128 error_at (richloc
, message
, value
);
6132 else if (token_type
== CPP_PRAGMA
)
6133 message
= catenate_messages (gmsgid
, " before %<#pragma%>");
6134 else if (token_type
== CPP_PRAGMA_EOL
)
6135 message
= catenate_messages (gmsgid
, " before end of line");
6136 else if (token_type
== CPP_DECLTYPE
)
6137 message
= catenate_messages (gmsgid
, " before %<decltype%>");
6138 else if (token_type
< N_TTYPES
)
6140 message
= catenate_messages (gmsgid
, " before %qs token");
6141 error_at (richloc
, message
, cpp_type2name (token_type
, token_flags
));
6146 error_at (richloc
, gmsgid
);
6150 error_at (richloc
, message
);
6153 #undef catenate_messages
6156 /* Return the gcc option code associated with the reason for a cpp
6157 message, or 0 if none. */
6160 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason
)
6162 const struct cpp_reason_option_codes_t
*entry
;
6164 for (entry
= cpp_reason_option_codes
; entry
->reason
!= CPP_W_NONE
; entry
++)
6166 if (entry
->reason
== reason
)
6167 return entry
->option_code
;
6172 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6173 preprocessor. The diagnostic is of type LEVEL, with REASON set
6174 to the reason code if LEVEL is represents a warning, at location
6175 RICHLOC unless this is after lexing and the compiler's location
6176 should be used instead; MSG is the translated message and AP
6177 the arguments. Returns true if a diagnostic was emitted, false
6181 c_cpp_diagnostic (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
6182 enum cpp_diagnostic_level level
,
6183 enum cpp_warning_reason reason
,
6184 rich_location
*richloc
,
6185 const char *msg
, va_list *ap
)
6187 diagnostic_info diagnostic
;
6188 diagnostic_t dlevel
;
6189 bool save_warn_system_headers
= global_dc
->dc_warn_system_headers
;
6194 case CPP_DL_WARNING_SYSHDR
:
6197 global_dc
->dc_warn_system_headers
= 1;
6199 case CPP_DL_WARNING
:
6202 dlevel
= DK_WARNING
;
6204 case CPP_DL_PEDWARN
:
6205 if (flag_no_output
&& !flag_pedantic_errors
)
6207 dlevel
= DK_PEDWARN
;
6225 richloc
->set_range (0, input_location
, SHOW_RANGE_WITH_CARET
);
6226 diagnostic_set_info_translated (&diagnostic
, msg
, ap
,
6228 diagnostic_override_option_index
6230 c_option_controlling_cpp_diagnostic (reason
));
6231 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
6232 if (level
== CPP_DL_WARNING_SYSHDR
)
6233 global_dc
->dc_warn_system_headers
= save_warn_system_headers
;
6237 /* Convert a character from the host to the target execution character
6238 set. cpplib handles this, mostly. */
6241 c_common_to_target_charset (HOST_WIDE_INT c
)
6243 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6244 zero-extended under -fno-signed-char. cpplib insists that characters
6245 and character constants are always unsigned. Hence we must convert
6247 cppchar_t uc
= ((cppchar_t
)c
) & ((((cppchar_t
)1) << CHAR_BIT
)-1);
6249 uc
= cpp_host_to_exec_charset (parse_in
, uc
);
6251 if (flag_signed_char
)
6252 return ((HOST_WIDE_INT
)uc
) << (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
)
6253 >> (HOST_BITS_PER_WIDE_INT
- CHAR_TYPE_SIZE
);
6258 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6259 references with an INDIRECT_REF of a constant at the bottom; much like the
6260 traditional rendering of offsetof as a macro. TYPE is the desired type of
6261 the whole expression. Return the folded result. */
6264 fold_offsetof (tree expr
, tree type
, enum tree_code ctx
)
6267 tree_code code
= TREE_CODE (expr
);
6274 error ("cannot apply %<offsetof%> to static data member %qD", expr
);
6275 return error_mark_node
;
6279 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6280 return error_mark_node
;
6284 if (!TREE_CONSTANT (TREE_OPERAND (expr
, 0)))
6286 error ("cannot apply %<offsetof%> to a non constant address");
6287 return error_mark_node
;
6289 return convert (type
, TREE_OPERAND (expr
, 0));
6292 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6293 if (base
== error_mark_node
)
6296 t
= TREE_OPERAND (expr
, 1);
6297 if (DECL_C_BIT_FIELD (t
))
6299 error ("attempt to take address of bit-field structure "
6301 return error_mark_node
;
6303 off
= size_binop_loc (input_location
, PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
6304 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
6309 base
= fold_offsetof (TREE_OPERAND (expr
, 0), type
, code
);
6310 if (base
== error_mark_node
)
6313 t
= TREE_OPERAND (expr
, 1);
6314 STRIP_ANY_LOCATION_WRAPPER (t
);
6316 /* Check if the offset goes beyond the upper bound of the array. */
6317 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) >= 0)
6319 tree upbound
= array_ref_up_bound (expr
);
6320 if (upbound
!= NULL_TREE
6321 && TREE_CODE (upbound
) == INTEGER_CST
6322 && !tree_int_cst_equal (upbound
,
6323 TYPE_MAX_VALUE (TREE_TYPE (upbound
))))
6325 if (ctx
!= ARRAY_REF
&& ctx
!= COMPONENT_REF
)
6326 upbound
= size_binop (PLUS_EXPR
, upbound
,
6327 build_int_cst (TREE_TYPE (upbound
), 1));
6328 if (tree_int_cst_lt (upbound
, t
))
6332 for (v
= TREE_OPERAND (expr
, 0);
6333 TREE_CODE (v
) == COMPONENT_REF
;
6334 v
= TREE_OPERAND (v
, 0))
6335 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
6338 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
6339 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
6340 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
6346 /* Don't warn if the array might be considered a poor
6347 man's flexible array member with a very permissive
6348 definition thereof. */
6349 if (TREE_CODE (v
) == ARRAY_REF
6350 || TREE_CODE (v
) == COMPONENT_REF
)
6351 warning (OPT_Warray_bounds
,
6352 "index %E denotes an offset "
6353 "greater than size of %qT",
6354 t
, TREE_TYPE (TREE_OPERAND (expr
, 0)));
6359 t
= convert (sizetype
, t
);
6360 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
6364 /* Handle static members of volatile structs. */
6365 t
= TREE_OPERAND (expr
, 1);
6366 gcc_checking_assert (VAR_P (get_base_address (t
)));
6367 return fold_offsetof (t
, type
);
6373 if (!POINTER_TYPE_P (type
))
6374 return size_binop (PLUS_EXPR
, base
, convert (type
, off
));
6375 return fold_build_pointer_plus (base
, off
);
6378 /* *PTYPE is an incomplete array. Complete it with a domain based on
6379 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6380 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6381 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6384 complete_array_type (tree
*ptype
, tree initial_value
, bool do_default
)
6386 tree maxindex
, type
, main_type
, elt
, unqual_elt
;
6387 int failure
= 0, quals
;
6388 bool overflow_p
= false;
6390 maxindex
= size_zero_node
;
6393 STRIP_ANY_LOCATION_WRAPPER (initial_value
);
6395 if (TREE_CODE (initial_value
) == STRING_CST
)
6398 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value
)));
6399 maxindex
= size_int (TREE_STRING_LENGTH (initial_value
)/eltsize
- 1);
6401 else if (TREE_CODE (initial_value
) == CONSTRUCTOR
)
6403 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (initial_value
);
6405 if (vec_safe_is_empty (v
))
6409 maxindex
= ssize_int (-1);
6414 unsigned HOST_WIDE_INT cnt
;
6415 constructor_elt
*ce
;
6416 bool fold_p
= false;
6419 maxindex
= (*v
)[0].index
, fold_p
= true;
6421 curindex
= maxindex
;
6423 for (cnt
= 1; vec_safe_iterate (v
, cnt
, &ce
); cnt
++)
6425 bool curfold_p
= false;
6427 curindex
= ce
->index
, curfold_p
= true;
6432 /* Since we treat size types now as ordinary
6433 unsigned types, we need an explicit overflow
6435 tree orig
= curindex
;
6436 curindex
= fold_convert (sizetype
, curindex
);
6437 overflow_p
|= tree_int_cst_lt (curindex
, orig
);
6439 curindex
= size_binop (PLUS_EXPR
, curindex
,
6442 if (tree_int_cst_lt (maxindex
, curindex
))
6443 maxindex
= curindex
, fold_p
= curfold_p
;
6447 tree orig
= maxindex
;
6448 maxindex
= fold_convert (sizetype
, maxindex
);
6449 overflow_p
|= tree_int_cst_lt (maxindex
, orig
);
6455 /* Make an error message unless that happened already. */
6456 if (initial_value
!= error_mark_node
)
6468 elt
= TREE_TYPE (type
);
6469 quals
= TYPE_QUALS (strip_array_types (elt
));
6473 unqual_elt
= c_build_qualified_type (elt
, KEEP_QUAL_ADDR_SPACE (quals
));
6475 /* Using build_distinct_type_copy and modifying things afterward instead
6476 of using build_array_type to create a new type preserves all of the
6477 TYPE_LANG_FLAG_? bits that the front end may have set. */
6478 main_type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6479 TREE_TYPE (main_type
) = unqual_elt
;
6480 TYPE_DOMAIN (main_type
)
6481 = build_range_type (TREE_TYPE (maxindex
),
6482 build_int_cst (TREE_TYPE (maxindex
), 0), maxindex
);
6483 TYPE_TYPELESS_STORAGE (main_type
) = TYPE_TYPELESS_STORAGE (type
);
6484 layout_type (main_type
);
6486 /* Make sure we have the canonical MAIN_TYPE. */
6487 hashval_t hashcode
= type_hash_canon_hash (main_type
);
6488 main_type
= type_hash_canon (hashcode
, main_type
);
6490 /* Fix the canonical type. */
6491 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type
))
6492 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type
)))
6493 SET_TYPE_STRUCTURAL_EQUALITY (main_type
);
6494 else if (TYPE_CANONICAL (TREE_TYPE (main_type
)) != TREE_TYPE (main_type
)
6495 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type
))
6496 != TYPE_DOMAIN (main_type
)))
6497 TYPE_CANONICAL (main_type
)
6498 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type
)),
6499 TYPE_CANONICAL (TYPE_DOMAIN (main_type
)),
6500 TYPE_TYPELESS_STORAGE (main_type
));
6502 TYPE_CANONICAL (main_type
) = main_type
;
6507 type
= c_build_qualified_type (main_type
, quals
);
6509 if (COMPLETE_TYPE_P (type
)
6510 && TREE_CODE (TYPE_SIZE_UNIT (type
)) == INTEGER_CST
6511 && (overflow_p
|| TREE_OVERFLOW (TYPE_SIZE_UNIT (type
))))
6513 error ("size of array is too large");
6514 /* If we proceed with the array type as it is, we'll eventually
6515 crash in tree_to_[su]hwi(). */
6516 type
= error_mark_node
;
6523 /* INIT is an constructor of a structure with a flexible array member.
6524 Complete the flexible array member with a domain based on it's value. */
6526 complete_flexible_array_elts (tree init
)
6530 if (init
== NULL_TREE
|| TREE_CODE (init
) != CONSTRUCTOR
)
6533 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
6536 elt
= CONSTRUCTOR_ELTS (init
)->last ().value
;
6537 type
= TREE_TYPE (elt
);
6538 if (TREE_CODE (type
) == ARRAY_TYPE
6539 && TYPE_SIZE (type
) == NULL_TREE
)
6540 complete_array_type (&TREE_TYPE (elt
), elt
, false);
6542 complete_flexible_array_elts (elt
);
6545 /* Like c_mark_addressable but don't check register qualifier. */
6547 c_common_mark_addressable_vec (tree t
)
6549 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
6550 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
6551 while (handled_component_p (t
))
6552 t
= TREE_OPERAND (t
, 0);
6554 && TREE_CODE (t
) != PARM_DECL
6555 && TREE_CODE (t
) != COMPOUND_LITERAL_EXPR
)
6557 if (!VAR_P (t
) || !DECL_HARD_REGISTER (t
))
6558 TREE_ADDRESSABLE (t
) = 1;
6559 if (TREE_CODE (t
) == COMPOUND_LITERAL_EXPR
)
6560 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t
)) = 1;
6565 /* Used to help initialize the builtin-types.def table. When a type of
6566 the correct size doesn't exist, use error_mark_node instead of NULL.
6567 The later results in segfaults even when a decl using the type doesn't
6571 builtin_type_for_size (int size
, bool unsignedp
)
6573 tree type
= c_common_type_for_size (size
, unsignedp
);
6574 return type
? type
: error_mark_node
;
6577 /* Work out the size of the first argument of a call to
6578 __builtin_speculation_safe_value. Only pointers and integral types
6579 are permitted. Return -1 if the argument type is not supported or
6580 the size is too large; 0 if the argument type is a pointer or the
6581 size if it is integral. */
6582 static enum built_in_function
6583 speculation_safe_value_resolve_call (tree function
, vec
<tree
, va_gc
> *params
)
6585 /* Type of the argument. */
6589 if (vec_safe_is_empty (params
))
6591 error ("too few arguments to function %qE", function
);
6592 return BUILT_IN_NONE
;
6595 type
= TREE_TYPE ((*params
)[0]);
6596 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6598 /* Force array-to-pointer decay for C++. */
6599 (*params
)[0] = default_conversion ((*params
)[0]);
6600 type
= TREE_TYPE ((*params
)[0]);
6603 if (POINTER_TYPE_P (type
))
6604 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR
;
6606 if (!INTEGRAL_TYPE_P (type
))
6609 if (!COMPLETE_TYPE_P (type
))
6612 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6613 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6614 return ((enum built_in_function
)
6615 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1
+ exact_log2 (size
)));
6618 /* Issue the diagnostic only if the argument is valid, otherwise
6619 it would be redundant at best and could be misleading. */
6620 if (type
!= error_mark_node
)
6621 error ("operand type %qT is incompatible with argument %d of %qE",
6624 return BUILT_IN_NONE
;
6627 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6628 the prototype for FUNCTION. The first argument is mandatory, a second
6629 argument, if present, must be type compatible with the first. */
6631 speculation_safe_value_resolve_params (location_t loc
, tree orig_function
,
6632 vec
<tree
, va_gc
> *params
)
6636 if (params
->length () == 0)
6638 error_at (loc
, "too few arguments to function %qE", orig_function
);
6642 else if (params
->length () > 2)
6644 error_at (loc
, "too many arguments to function %qE", orig_function
);
6649 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
)
6650 val
= default_conversion (val
);
6651 if (!(TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
6652 || TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
))
6655 "expecting argument of type pointer or of type integer "
6661 if (params
->length () == 2)
6663 tree val2
= (*params
)[1];
6664 if (TREE_CODE (TREE_TYPE (val2
)) == ARRAY_TYPE
)
6665 val2
= default_conversion (val2
);
6666 if (!(TREE_TYPE (val
) == TREE_TYPE (val2
)
6667 || useless_type_conversion_p (TREE_TYPE (val
), TREE_TYPE (val2
))))
6669 error_at (loc
, "both arguments must be compatible");
6672 (*params
)[1] = val2
;
6678 /* Cast the result of the builtin back to the type of the first argument,
6679 preserving any qualifiers that it might have. */
6681 speculation_safe_value_resolve_return (tree first_param
, tree result
)
6683 tree ptype
= TREE_TYPE (first_param
);
6684 tree rtype
= TREE_TYPE (result
);
6685 ptype
= TYPE_MAIN_VARIANT (ptype
);
6687 if (tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6688 return convert (ptype
, result
);
6693 /* A helper function for resolve_overloaded_builtin in resolving the
6694 overloaded __sync_ builtins. Returns a positive power of 2 if the
6695 first operand of PARAMS is a pointer to a supported data type.
6696 Returns 0 if an error is encountered.
6697 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6701 sync_resolve_size (tree function
, vec
<tree
, va_gc
> *params
, bool fetch
)
6703 /* Type of the argument. */
6705 /* Type the argument points to. */
6709 if (vec_safe_is_empty (params
))
6711 error ("too few arguments to function %qE", function
);
6715 argtype
= type
= TREE_TYPE ((*params
)[0]);
6716 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6718 /* Force array-to-pointer decay for C++. */
6719 (*params
)[0] = default_conversion ((*params
)[0]);
6720 type
= TREE_TYPE ((*params
)[0]);
6722 if (TREE_CODE (type
) != POINTER_TYPE
)
6725 type
= TREE_TYPE (type
);
6726 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
6729 if (!COMPLETE_TYPE_P (type
))
6732 if (fetch
&& TREE_CODE (type
) == BOOLEAN_TYPE
)
6735 size
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
6736 if (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16)
6740 /* Issue the diagnostic only if the argument is valid, otherwise
6741 it would be redundant at best and could be misleading. */
6742 if (argtype
!= error_mark_node
)
6743 error ("operand type %qT is incompatible with argument %d of %qE",
6744 argtype
, 1, function
);
6748 /* A helper function for resolve_overloaded_builtin. Adds casts to
6749 PARAMS to make arguments match up with those of FUNCTION. Drops
6750 the variadic arguments at the end. Returns false if some error
6751 was encountered; true on success. */
6754 sync_resolve_params (location_t loc
, tree orig_function
, tree function
,
6755 vec
<tree
, va_gc
> *params
, bool orig_format
)
6757 function_args_iterator iter
;
6759 unsigned int parmnum
;
6761 function_args_iter_init (&iter
, TREE_TYPE (function
));
6762 /* We've declared the implementation functions to use "volatile void *"
6763 as the pointer parameter, so we shouldn't get any complaints from the
6764 call to check_function_arguments what ever type the user used. */
6765 function_args_iter_next (&iter
);
6766 ptype
= TREE_TYPE (TREE_TYPE ((*params
)[0]));
6767 ptype
= TYPE_MAIN_VARIANT (ptype
);
6769 /* For the rest of the values, we need to cast these to FTYPE, so that we
6770 don't get warnings for passing pointer types, etc. */
6776 arg_type
= function_args_iter_cond (&iter
);
6777 /* XXX void_type_node belies the abstraction. */
6778 if (arg_type
== void_type_node
)
6782 if (params
->length () <= parmnum
)
6784 error_at (loc
, "too few arguments to function %qE", orig_function
);
6788 /* Only convert parameters if arg_type is unsigned integer type with
6789 new format sync routines, i.e. don't attempt to convert pointer
6790 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6791 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6793 if (TREE_CODE (arg_type
) == INTEGER_TYPE
&& TYPE_UNSIGNED (arg_type
))
6795 /* Ideally for the first conversion we'd use convert_for_assignment
6796 so that we get warnings for anything that doesn't match the pointer
6797 type. This isn't portable across the C and C++ front ends atm. */
6798 val
= (*params
)[parmnum
];
6799 val
= convert (ptype
, val
);
6800 val
= convert (arg_type
, val
);
6801 (*params
)[parmnum
] = val
;
6804 function_args_iter_next (&iter
);
6807 /* __atomic routines are not variadic. */
6808 if (!orig_format
&& params
->length () != parmnum
+ 1)
6810 error_at (loc
, "too many arguments to function %qE", orig_function
);
6814 /* The definition of these primitives is variadic, with the remaining
6815 being "an optional list of variables protected by the memory barrier".
6816 No clue what that's supposed to mean, precisely, but we consider all
6817 call-clobbered variables to be protected so we're safe. */
6818 params
->truncate (parmnum
+ 1);
6823 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6824 RESULT to make it match the type of the first pointer argument in
6828 sync_resolve_return (tree first_param
, tree result
, bool orig_format
)
6830 tree ptype
= TREE_TYPE (TREE_TYPE (first_param
));
6831 tree rtype
= TREE_TYPE (result
);
6832 ptype
= TYPE_MAIN_VARIANT (ptype
);
6834 /* New format doesn't require casting unless the types are the same size. */
6835 if (orig_format
|| tree_int_cst_equal (TYPE_SIZE (ptype
), TYPE_SIZE (rtype
)))
6836 return convert (ptype
, result
);
6841 /* This function verifies the PARAMS to generic atomic FUNCTION.
6842 It returns the size if all the parameters are the same size, otherwise
6843 0 is returned if the parameters are invalid. */
6846 get_atomic_generic_size (location_t loc
, tree function
,
6847 vec
<tree
, va_gc
> *params
)
6849 unsigned int n_param
;
6850 unsigned int n_model
;
6855 /* Determine the parameter makeup. */
6856 switch (DECL_FUNCTION_CODE (function
))
6858 case BUILT_IN_ATOMIC_EXCHANGE
:
6862 case BUILT_IN_ATOMIC_LOAD
:
6863 case BUILT_IN_ATOMIC_STORE
:
6867 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
6875 if (vec_safe_length (params
) != n_param
)
6877 error_at (loc
, "incorrect number of arguments to function %qE", function
);
6881 /* Get type of first parameter, and determine its size. */
6882 type_0
= TREE_TYPE ((*params
)[0]);
6883 if (TREE_CODE (type_0
) == ARRAY_TYPE
&& c_dialect_cxx ())
6885 /* Force array-to-pointer decay for C++. */
6886 (*params
)[0] = default_conversion ((*params
)[0]);
6887 type_0
= TREE_TYPE ((*params
)[0]);
6889 if (TREE_CODE (type_0
) != POINTER_TYPE
|| VOID_TYPE_P (TREE_TYPE (type_0
)))
6891 error_at (loc
, "argument 1 of %qE must be a non-void pointer type",
6896 /* Types must be compile time constant sizes. */
6897 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0
)))) != INTEGER_CST
)
6900 "argument 1 of %qE must be a pointer to a constant size type",
6905 size_0
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0
)));
6907 /* Zero size objects are not allowed. */
6911 "argument 1 of %qE must be a pointer to a nonzero size object",
6916 /* Check each other parameter is a pointer and the same size. */
6917 for (x
= 0; x
< n_param
- n_model
; x
++)
6920 tree type
= TREE_TYPE ((*params
)[x
]);
6921 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6922 if (n_param
== 6 && x
== 3)
6924 if (TREE_CODE (type
) == ARRAY_TYPE
&& c_dialect_cxx ())
6926 /* Force array-to-pointer decay for C++. */
6927 (*params
)[x
] = default_conversion ((*params
)[x
]);
6928 type
= TREE_TYPE ((*params
)[x
]);
6930 if (!POINTER_TYPE_P (type
))
6932 error_at (loc
, "argument %d of %qE must be a pointer type", x
+ 1,
6936 else if (TYPE_SIZE_UNIT (TREE_TYPE (type
))
6937 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type
))))
6940 error_at (loc
, "argument %d of %qE must be a pointer to a constant "
6941 "size type", x
+ 1, function
);
6944 else if (FUNCTION_POINTER_TYPE_P (type
))
6946 error_at (loc
, "argument %d of %qE must not be a pointer to a "
6947 "function", x
+ 1, function
);
6950 tree type_size
= TYPE_SIZE_UNIT (TREE_TYPE (type
));
6951 size
= type_size
? tree_to_uhwi (type_size
) : 0;
6954 error_at (loc
, "size mismatch in argument %d of %qE", x
+ 1,
6960 /* Check memory model parameters for validity. */
6961 for (x
= n_param
- n_model
; x
< n_param
; x
++)
6963 tree p
= (*params
)[x
];
6964 if (!INTEGRAL_TYPE_P (TREE_TYPE (p
)))
6966 error_at (loc
, "non-integer memory model argument %d of %qE", x
+ 1,
6970 p
= fold_for_warn (p
);
6971 if (TREE_CODE (p
) == INTEGER_CST
)
6973 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6974 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6975 bits will be checked later during expansion in target specific
6977 if (memmodel_base (TREE_INT_CST_LOW (p
)) >= MEMMODEL_LAST
)
6978 warning_at (loc
, OPT_Winvalid_memory_model
,
6979 "invalid memory model argument %d of %qE", x
+ 1,
6988 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6989 at the beginning of the parameter list PARAMS representing the size of the
6990 objects. This is to match the library ABI requirement. LOC is the location
6991 of the function call.
6992 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6993 returned to allow the external call to be constructed. */
6996 add_atomic_size_parameter (unsigned n
, location_t loc
, tree function
,
6997 vec
<tree
, va_gc
> *params
)
7001 /* Insert a SIZE_T parameter as the first param. If there isn't
7002 enough space, allocate a new vector and recursively re-build with that. */
7003 if (!params
->space (1))
7005 unsigned int z
, len
;
7006 vec
<tree
, va_gc
> *v
;
7009 len
= params
->length ();
7010 vec_alloc (v
, len
+ 1);
7011 v
->quick_push (build_int_cst (size_type_node
, n
));
7012 for (z
= 0; z
< len
; z
++)
7013 v
->quick_push ((*params
)[z
]);
7014 f
= build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
7019 /* Add the size parameter and leave as a function call for processing. */
7020 size_node
= build_int_cst (size_type_node
, n
);
7021 params
->quick_insert (0, size_node
);
7026 /* Return whether atomic operations for naturally aligned N-byte
7027 arguments are supported, whether inline or through libatomic. */
7029 atomic_size_supported_p (int n
)
7040 return targetm
.scalar_mode_supported_p (TImode
);
7047 /* This will process an __atomic_exchange function call, determine whether it
7048 needs to be mapped to the _N variation, or turned into a library call.
7049 LOC is the location of the builtin call.
7050 FUNCTION is the DECL that has been invoked;
7051 PARAMS is the argument list for the call. The return value is non-null
7052 TRUE is returned if it is translated into the proper format for a call to the
7053 external library, and NEW_RETURN is set the tree for that function.
7054 FALSE is returned if processing for the _N variation is required, and
7055 NEW_RETURN is set to the return value the result is copied into. */
7057 resolve_overloaded_atomic_exchange (location_t loc
, tree function
,
7058 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7060 tree p0
, p1
, p2
, p3
;
7061 tree I_type
, I_type_ptr
;
7062 int n
= get_atomic_generic_size (loc
, function
, params
);
7064 /* Size of 0 is an error condition. */
7067 *new_return
= error_mark_node
;
7071 /* If not a lock-free size, change to the library generic format. */
7072 if (!atomic_size_supported_p (n
))
7074 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7078 /* Otherwise there is a lockfree match, transform the call from:
7079 void fn(T* mem, T* desired, T* return, model)
7081 *return = (T) (fn (In* mem, (In) *desired, model)) */
7088 /* Create pointer to appropriate size. */
7089 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7090 I_type_ptr
= build_pointer_type (I_type
);
7092 /* Convert object pointer to required type. */
7093 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7095 /* Convert new value to required type, and dereference it. */
7096 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7097 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7100 /* Move memory model to the 3rd position, and end param list. */
7102 params
->truncate (3);
7104 /* Convert return pointer and dereference it for later assignment. */
7105 *new_return
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7111 /* This will process an __atomic_compare_exchange function call, determine
7112 whether it needs to be mapped to the _N variation, or turned into a lib call.
7113 LOC is the location of the builtin call.
7114 FUNCTION is the DECL that has been invoked;
7115 PARAMS is the argument list for the call. The return value is non-null
7116 TRUE is returned if it is translated into the proper format for a call to the
7117 external library, and NEW_RETURN is set the tree for that function.
7118 FALSE is returned if processing for the _N variation is required. */
7121 resolve_overloaded_atomic_compare_exchange (location_t loc
, tree function
,
7122 vec
<tree
, va_gc
> *params
,
7126 tree I_type
, I_type_ptr
;
7127 int n
= get_atomic_generic_size (loc
, function
, params
);
7129 /* Size of 0 is an error condition. */
7132 *new_return
= error_mark_node
;
7136 /* If not a lock-free size, change to the library generic format. */
7137 if (!atomic_size_supported_p (n
))
7139 /* The library generic format does not have the weak parameter, so
7140 remove it from the param list. Since a parameter has been removed,
7141 we can be sure that there is room for the SIZE_T parameter, meaning
7142 there will not be a recursive rebuilding of the parameter list, so
7143 there is no danger this will be done twice. */
7146 (*params
)[3] = (*params
)[4];
7147 (*params
)[4] = (*params
)[5];
7148 params
->truncate (5);
7150 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7154 /* Otherwise, there is a match, so the call needs to be transformed from:
7155 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7157 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7163 /* Create pointer to appropriate size. */
7164 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7165 I_type_ptr
= build_pointer_type (I_type
);
7167 /* Convert object pointer to required type. */
7168 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7171 /* Convert expected pointer to required type. */
7172 p1
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p1
);
7175 /* Convert desired value to required type, and dereference it. */
7176 p2
= build_indirect_ref (loc
, p2
, RO_UNARY_STAR
);
7177 p2
= build1 (VIEW_CONVERT_EXPR
, I_type
, p2
);
7180 /* The rest of the parameters are fine. NULL means no special return value
7187 /* This will process an __atomic_load function call, determine whether it
7188 needs to be mapped to the _N variation, or turned into a library call.
7189 LOC is the location of the builtin call.
7190 FUNCTION is the DECL that has been invoked;
7191 PARAMS is the argument list for the call. The return value is non-null
7192 TRUE is returned if it is translated into the proper format for a call to the
7193 external library, and NEW_RETURN is set the tree for that function.
7194 FALSE is returned if processing for the _N variation is required, and
7195 NEW_RETURN is set to the return value the result is copied into. */
7198 resolve_overloaded_atomic_load (location_t loc
, tree function
,
7199 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7202 tree I_type
, I_type_ptr
;
7203 int n
= get_atomic_generic_size (loc
, function
, params
);
7205 /* Size of 0 is an error condition. */
7208 *new_return
= error_mark_node
;
7212 /* If not a lock-free size, change to the library generic format. */
7213 if (!atomic_size_supported_p (n
))
7215 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7219 /* Otherwise, there is a match, so the call needs to be transformed from:
7220 void fn(T* mem, T* return, model)
7222 *return = (T) (fn ((In *) mem, model)) */
7228 /* Create pointer to appropriate size. */
7229 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7230 I_type_ptr
= build_pointer_type (I_type
);
7232 /* Convert object pointer to required type. */
7233 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7236 /* Move memory model to the 2nd position, and end param list. */
7238 params
->truncate (2);
7240 /* Convert return pointer and dereference it for later assignment. */
7241 *new_return
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7247 /* This will process an __atomic_store function call, determine whether it
7248 needs to be mapped to the _N variation, or turned into a library call.
7249 LOC is the location of the builtin call.
7250 FUNCTION is the DECL that has been invoked;
7251 PARAMS is the argument list for the call. The return value is non-null
7252 TRUE is returned if it is translated into the proper format for a call to the
7253 external library, and NEW_RETURN is set the tree for that function.
7254 FALSE is returned if processing for the _N variation is required, and
7255 NEW_RETURN is set to the return value the result is copied into. */
7258 resolve_overloaded_atomic_store (location_t loc
, tree function
,
7259 vec
<tree
, va_gc
> *params
, tree
*new_return
)
7262 tree I_type
, I_type_ptr
;
7263 int n
= get_atomic_generic_size (loc
, function
, params
);
7265 /* Size of 0 is an error condition. */
7268 *new_return
= error_mark_node
;
7272 /* If not a lock-free size, change to the library generic format. */
7273 if (!atomic_size_supported_p (n
))
7275 *new_return
= add_atomic_size_parameter (n
, loc
, function
, params
);
7279 /* Otherwise, there is a match, so the call needs to be transformed from:
7280 void fn(T* mem, T* value, model)
7282 fn ((In *) mem, (In) *value, model) */
7287 /* Create pointer to appropriate size. */
7288 I_type
= builtin_type_for_size (BITS_PER_UNIT
* n
, 1);
7289 I_type_ptr
= build_pointer_type (I_type
);
7291 /* Convert object pointer to required type. */
7292 p0
= build1 (VIEW_CONVERT_EXPR
, I_type_ptr
, p0
);
7295 /* Convert new value to required type, and dereference it. */
7296 p1
= build_indirect_ref (loc
, p1
, RO_UNARY_STAR
);
7297 p1
= build1 (VIEW_CONVERT_EXPR
, I_type
, p1
);
7300 /* The memory model is in the right spot already. Return is void. */
7301 *new_return
= NULL_TREE
;
7307 /* Some builtin functions are placeholders for other expressions. This
7308 function should be called immediately after parsing the call expression
7309 before surrounding code has committed to the type of the expression.
7311 LOC is the location of the builtin call.
7313 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7314 PARAMS is the argument list for the call. The return value is non-null
7315 when expansion is complete, and null if normal processing should
7319 resolve_overloaded_builtin (location_t loc
, tree function
,
7320 vec
<tree
, va_gc
> *params
)
7322 enum built_in_function orig_code
= DECL_FUNCTION_CODE (function
);
7324 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7325 Those are not valid to call with a pointer to _Bool (or C++ bool)
7326 and so must be rejected. */
7327 bool fetch_op
= true;
7328 bool orig_format
= true;
7329 tree new_return
= NULL_TREE
;
7331 switch (DECL_BUILT_IN_CLASS (function
))
7333 case BUILT_IN_NORMAL
:
7336 if (targetm
.resolve_overloaded_builtin
)
7337 return targetm
.resolve_overloaded_builtin (loc
, function
, params
);
7344 /* Handle BUILT_IN_NORMAL here. */
7347 case BUILT_IN_SPECULATION_SAFE_VALUE_N
:
7349 tree new_function
, first_param
, result
;
7350 enum built_in_function fncode
7351 = speculation_safe_value_resolve_call (function
, params
);;
7353 first_param
= (*params
)[0];
7354 if (fncode
== BUILT_IN_NONE
7355 || !speculation_safe_value_resolve_params (loc
, function
, params
))
7356 return error_mark_node
;
7358 if (targetm
.have_speculation_safe_value (true))
7360 new_function
= builtin_decl_explicit (fncode
);
7361 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7364 if (result
== error_mark_node
)
7367 return speculation_safe_value_resolve_return (first_param
, result
);
7371 /* This target doesn't have, or doesn't need, active mitigation
7372 against incorrect speculative execution. Simply return the
7373 first parameter to the builtin. */
7374 if (!targetm
.have_speculation_safe_value (false))
7375 /* The user has invoked __builtin_speculation_safe_value
7376 even though __HAVE_SPECULATION_SAFE_VALUE is not
7377 defined: emit a warning. */
7378 warning_at (input_location
, 0,
7379 "this target does not define a speculation barrier; "
7380 "your program will still execute correctly, "
7381 "but incorrect speculation may not be be "
7384 /* If the optional second argument is present, handle any side
7386 if (params
->length () == 2
7387 && TREE_SIDE_EFFECTS ((*params
)[1]))
7388 return build2 (COMPOUND_EXPR
, TREE_TYPE (first_param
),
7389 (*params
)[1], first_param
);
7395 case BUILT_IN_ATOMIC_EXCHANGE
:
7396 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7397 case BUILT_IN_ATOMIC_LOAD
:
7398 case BUILT_IN_ATOMIC_STORE
:
7400 /* Handle these 4 together so that they can fall through to the next
7401 case if the call is transformed to an _N variant. */
7404 case BUILT_IN_ATOMIC_EXCHANGE
:
7406 if (resolve_overloaded_atomic_exchange (loc
, function
, params
,
7409 /* Change to the _N variant. */
7410 orig_code
= BUILT_IN_ATOMIC_EXCHANGE_N
;
7414 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE
:
7416 if (resolve_overloaded_atomic_compare_exchange (loc
, function
,
7420 /* Change to the _N variant. */
7421 orig_code
= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
;
7424 case BUILT_IN_ATOMIC_LOAD
:
7426 if (resolve_overloaded_atomic_load (loc
, function
, params
,
7429 /* Change to the _N variant. */
7430 orig_code
= BUILT_IN_ATOMIC_LOAD_N
;
7433 case BUILT_IN_ATOMIC_STORE
:
7435 if (resolve_overloaded_atomic_store (loc
, function
, params
,
7438 /* Change to the _N variant. */
7439 orig_code
= BUILT_IN_ATOMIC_STORE_N
;
7447 case BUILT_IN_ATOMIC_EXCHANGE_N
:
7448 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
:
7449 case BUILT_IN_ATOMIC_LOAD_N
:
7450 case BUILT_IN_ATOMIC_STORE_N
:
7453 case BUILT_IN_ATOMIC_ADD_FETCH_N
:
7454 case BUILT_IN_ATOMIC_SUB_FETCH_N
:
7455 case BUILT_IN_ATOMIC_AND_FETCH_N
:
7456 case BUILT_IN_ATOMIC_NAND_FETCH_N
:
7457 case BUILT_IN_ATOMIC_XOR_FETCH_N
:
7458 case BUILT_IN_ATOMIC_OR_FETCH_N
:
7459 case BUILT_IN_ATOMIC_FETCH_ADD_N
:
7460 case BUILT_IN_ATOMIC_FETCH_SUB_N
:
7461 case BUILT_IN_ATOMIC_FETCH_AND_N
:
7462 case BUILT_IN_ATOMIC_FETCH_NAND_N
:
7463 case BUILT_IN_ATOMIC_FETCH_XOR_N
:
7464 case BUILT_IN_ATOMIC_FETCH_OR_N
:
7465 orig_format
= false;
7467 case BUILT_IN_SYNC_FETCH_AND_ADD_N
:
7468 case BUILT_IN_SYNC_FETCH_AND_SUB_N
:
7469 case BUILT_IN_SYNC_FETCH_AND_OR_N
:
7470 case BUILT_IN_SYNC_FETCH_AND_AND_N
:
7471 case BUILT_IN_SYNC_FETCH_AND_XOR_N
:
7472 case BUILT_IN_SYNC_FETCH_AND_NAND_N
:
7473 case BUILT_IN_SYNC_ADD_AND_FETCH_N
:
7474 case BUILT_IN_SYNC_SUB_AND_FETCH_N
:
7475 case BUILT_IN_SYNC_OR_AND_FETCH_N
:
7476 case BUILT_IN_SYNC_AND_AND_FETCH_N
:
7477 case BUILT_IN_SYNC_XOR_AND_FETCH_N
:
7478 case BUILT_IN_SYNC_NAND_AND_FETCH_N
:
7479 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
:
7480 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
:
7481 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
:
7482 case BUILT_IN_SYNC_LOCK_RELEASE_N
:
7484 /* The following are not _FETCH_OPs and must be accepted with
7485 pointers to _Bool (or C++ bool). */
7488 (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7489 && orig_code
!= BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7490 && orig_code
!= BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7491 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
);
7493 int n
= sync_resolve_size (function
, params
, fetch_op
);
7494 tree new_function
, first_param
, result
;
7495 enum built_in_function fncode
;
7498 return error_mark_node
;
7500 fncode
= (enum built_in_function
)((int)orig_code
+ exact_log2 (n
) + 1);
7501 new_function
= builtin_decl_explicit (fncode
);
7502 if (!sync_resolve_params (loc
, function
, new_function
, params
,
7504 return error_mark_node
;
7506 first_param
= (*params
)[0];
7507 result
= build_function_call_vec (loc
, vNULL
, new_function
, params
,
7509 if (result
== error_mark_node
)
7511 if (orig_code
!= BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7512 && orig_code
!= BUILT_IN_SYNC_LOCK_RELEASE_N
7513 && orig_code
!= BUILT_IN_ATOMIC_STORE_N
7514 && orig_code
!= BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N
)
7515 result
= sync_resolve_return (first_param
, result
, orig_format
);
7518 /* Prevent -Wunused-value warning. */
7519 TREE_USED (result
) = true;
7521 /* If new_return is set, assign function to that expr and cast the
7522 result to void since the generic interface returned void. */
7525 /* Cast function result from I{1,2,4,8,16} to the required type. */
7526 result
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (new_return
), result
);
7527 result
= build2 (MODIFY_EXPR
, TREE_TYPE (new_return
), new_return
,
7529 TREE_SIDE_EFFECTS (result
) = 1;
7530 protected_set_expr_location (result
, loc
);
7531 result
= convert (void_type_node
, result
);
7541 /* vector_types_compatible_elements_p is used in type checks of vectors
7542 values used as operands of binary operators. Where it returns true, and
7543 the other checks of the caller succeed (being vector types in he first
7544 place, and matching number of elements), we can just treat the types
7545 as essentially the same.
7546 Contrast with vector_targets_convertible_p, which is used for vector
7547 pointer types, and vector_types_convertible_p, which will allow
7548 language-specific matches under the control of flag_lax_vector_conversions,
7549 and might still require a conversion. */
7550 /* True if vector types T1 and T2 can be inputs to the same binary
7551 operator without conversion.
7552 We don't check the overall vector size here because some of our callers
7553 want to give different error messages when the vectors are compatible
7554 except for the element count. */
7557 vector_types_compatible_elements_p (tree t1
, tree t2
)
7559 bool opaque
= TYPE_VECTOR_OPAQUE (t1
) || TYPE_VECTOR_OPAQUE (t2
);
7560 t1
= TREE_TYPE (t1
);
7561 t2
= TREE_TYPE (t2
);
7563 enum tree_code c1
= TREE_CODE (t1
), c2
= TREE_CODE (t2
);
7565 gcc_assert ((INTEGRAL_TYPE_P (t1
)
7567 || c1
== FIXED_POINT_TYPE
)
7568 && (INTEGRAL_TYPE_P (t2
)
7570 || c2
== FIXED_POINT_TYPE
));
7572 t1
= c_common_signed_type (t1
);
7573 t2
= c_common_signed_type (t2
);
7574 /* Equality works here because c_common_signed_type uses
7575 TYPE_MAIN_VARIANT. */
7578 if (opaque
&& c1
== c2
7579 && (INTEGRAL_TYPE_P (t1
) || c1
== REAL_TYPE
)
7580 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
))
7585 /* Check for missing format attributes on function pointers. LTYPE is
7586 the new type or left-hand side type. RTYPE is the old type or
7587 right-hand side type. Returns TRUE if LTYPE is missing the desired
7591 check_missing_format_attribute (tree ltype
, tree rtype
)
7593 tree
const ttr
= TREE_TYPE (rtype
), ttl
= TREE_TYPE (ltype
);
7596 for (ra
= TYPE_ATTRIBUTES (ttr
); ra
; ra
= TREE_CHAIN (ra
))
7597 if (is_attribute_p ("format", TREE_PURPOSE (ra
)))
7602 for (la
= TYPE_ATTRIBUTES (ttl
); la
; la
= TREE_CHAIN (la
))
7603 if (is_attribute_p ("format", TREE_PURPOSE (la
)))
7611 /* Setup a TYPE_DECL node as a typedef representation.
7613 X is a TYPE_DECL for a typedef statement. Create a brand new
7614 ..._TYPE node (which will be just a variant of the existing
7615 ..._TYPE node with identical properties) and then install X
7616 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7618 The whole point here is to end up with a situation where each
7619 and every ..._TYPE node the compiler creates will be uniquely
7620 associated with AT MOST one node representing a typedef name.
7621 This way, even though the compiler substitutes corresponding
7622 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7623 early on, later parts of the compiler can always do the reverse
7624 translation and get back the corresponding typedef name. For
7627 typedef struct S MY_TYPE;
7630 Later parts of the compiler might only know that `object' was of
7631 type `struct S' if it were not for code just below. With this
7632 code however, later parts of the compiler see something like:
7634 struct S' == struct S
7635 typedef struct S' MY_TYPE;
7638 And they can then deduce (from the node for type struct S') that
7639 the original object declaration was:
7643 Being able to do this is important for proper support of protoize,
7644 and also for generating precise symbolic debugging information
7645 which takes full account of the programmer's (typedef) vocabulary.
7647 Obviously, we don't want to generate a duplicate ..._TYPE node if
7648 the TYPE_DECL node that we are now processing really represents a
7649 standard built-in type. */
7652 set_underlying_type (tree x
)
7654 if (x
== error_mark_node
)
7656 if (DECL_IS_BUILTIN (x
) && TREE_CODE (TREE_TYPE (x
)) != ARRAY_TYPE
)
7658 if (TYPE_NAME (TREE_TYPE (x
)) == 0)
7659 TYPE_NAME (TREE_TYPE (x
)) = x
;
7661 else if (TREE_TYPE (x
) != error_mark_node
7662 && DECL_ORIGINAL_TYPE (x
) == NULL_TREE
)
7664 tree tt
= TREE_TYPE (x
);
7665 DECL_ORIGINAL_TYPE (x
) = tt
;
7666 tt
= build_variant_type_copy (tt
);
7667 TYPE_STUB_DECL (tt
) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x
));
7670 /* Mark the type as used only when its type decl is decorated
7671 with attribute unused. */
7672 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x
)))
7679 /* Record the types used by the current global variable declaration
7680 being parsed, so that we can decide later to emit their debug info.
7681 Those types are in types_used_by_cur_var_decl, and we are going to
7682 store them in the types_used_by_vars_hash hash table.
7683 DECL is the declaration of the global variable that has been parsed. */
7686 record_types_used_by_current_var_decl (tree decl
)
7688 gcc_assert (decl
&& DECL_P (decl
) && TREE_STATIC (decl
));
7690 while (types_used_by_cur_var_decl
&& !types_used_by_cur_var_decl
->is_empty ())
7692 tree type
= types_used_by_cur_var_decl
->pop ();
7693 types_used_by_var_decl_insert (type
, decl
);
7697 /* The C and C++ parsers both use vectors to hold function arguments.
7698 For efficiency, we keep a cache of unused vectors. This is the
7701 typedef vec
<tree
, va_gc
> *tree_gc_vec
;
7702 static GTY((deletable
)) vec
<tree_gc_vec
, va_gc
> *tree_vector_cache
;
7704 /* Return a new vector from the cache. If the cache is empty,
7705 allocate a new vector. These vectors are GC'ed, so it is OK if the
7706 pointer is not released.. */
7709 make_tree_vector (void)
7711 if (tree_vector_cache
&& !tree_vector_cache
->is_empty ())
7712 return tree_vector_cache
->pop ();
7715 /* Passing 0 to vec::alloc returns NULL, and our callers require
7716 that we always return a non-NULL value. The vector code uses
7717 4 when growing a NULL vector, so we do too. */
7718 vec
<tree
, va_gc
> *v
;
7724 /* Release a vector of trees back to the cache. */
7727 release_tree_vector (vec
<tree
, va_gc
> *vec
)
7732 vec_safe_push (tree_vector_cache
, vec
);
7736 /* Get a new tree vector holding a single tree. */
7739 make_tree_vector_single (tree t
)
7741 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7742 ret
->quick_push (t
);
7746 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7749 make_tree_vector_from_list (tree list
)
7751 vec
<tree
, va_gc
> *ret
= make_tree_vector ();
7752 for (; list
; list
= TREE_CHAIN (list
))
7753 vec_safe_push (ret
, TREE_VALUE (list
));
7757 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7760 make_tree_vector_from_ctor (tree ctor
)
7762 vec
<tree
,va_gc
> *ret
= make_tree_vector ();
7763 vec_safe_reserve (ret
, CONSTRUCTOR_NELTS (ctor
));
7764 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (ctor
); ++i
)
7765 ret
->quick_push (CONSTRUCTOR_ELT (ctor
, i
)->value
);
7769 /* Get a new tree vector which is a copy of an existing one. */
7772 make_tree_vector_copy (const vec
<tree
, va_gc
> *orig
)
7774 vec
<tree
, va_gc
> *ret
;
7778 ret
= make_tree_vector ();
7779 vec_safe_reserve (ret
, vec_safe_length (orig
));
7780 FOR_EACH_VEC_SAFE_ELT (orig
, ix
, t
)
7781 ret
->quick_push (t
);
7785 /* Return true if KEYWORD starts a type specifier. */
7788 keyword_begins_type_specifier (enum rid keyword
)
7822 if (keyword
>= RID_FIRST_INT_N
7823 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
7824 && int_n_enabled_p
[keyword
-RID_FIRST_INT_N
])
7830 /* Return true if KEYWORD names a type qualifier. */
7833 keyword_is_type_qualifier (enum rid keyword
)
7847 /* Return true if KEYWORD names a storage class specifier.
7849 RID_TYPEDEF is not included in this list despite `typedef' being
7850 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7851 such for syntactic convenience only. */
7854 keyword_is_storage_class_specifier (enum rid keyword
)
7870 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7873 keyword_is_function_specifier (enum rid keyword
)
7887 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7888 declaration-specifier (C99 6.7). */
7891 keyword_is_decl_specifier (enum rid keyword
)
7893 if (keyword_is_storage_class_specifier (keyword
)
7894 || keyword_is_type_qualifier (keyword
)
7895 || keyword_is_function_specifier (keyword
))
7909 /* Initialize language-specific-bits of tree_contains_struct. */
7912 c_common_init_ts (void)
7914 MARK_TS_EXP (SIZEOF_EXPR
);
7915 MARK_TS_EXP (C_MAYBE_CONST_EXPR
);
7916 MARK_TS_EXP (EXCESS_PRECISION_EXPR
);
7919 /* Build a user-defined numeric literal out of an integer constant type VALUE
7920 with identifier SUFFIX. */
7923 build_userdef_literal (tree suffix_id
, tree value
,
7924 enum overflow_type overflow
, tree num_string
)
7926 tree literal
= make_node (USERDEF_LITERAL
);
7927 USERDEF_LITERAL_SUFFIX_ID (literal
) = suffix_id
;
7928 USERDEF_LITERAL_VALUE (literal
) = value
;
7929 USERDEF_LITERAL_OVERFLOW (literal
) = overflow
;
7930 USERDEF_LITERAL_NUM_STRING (literal
) = num_string
;
7934 /* For vector[index], convert the vector to an array of the underlying type.
7935 Return true if the resulting ARRAY_REF should not be an lvalue. */
7938 convert_vector_to_array_for_subscript (location_t loc
,
7939 tree
*vecp
, tree index
)
7942 if (VECTOR_TYPE_P (TREE_TYPE (*vecp
)))
7944 tree type
= TREE_TYPE (*vecp
);
7946 ret
= !lvalue_p (*vecp
);
7948 index
= fold_for_warn (index
);
7949 if (TREE_CODE (index
) == INTEGER_CST
)
7950 if (!tree_fits_uhwi_p (index
)
7951 || maybe_ge (tree_to_uhwi (index
), TYPE_VECTOR_SUBPARTS (type
)))
7952 warning_at (loc
, OPT_Warray_bounds
, "index value is out of bound");
7954 /* We are building an ARRAY_REF so mark the vector as addressable
7955 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7956 for function parameters. */
7957 c_common_mark_addressable_vec (*vecp
);
7959 *vecp
= build1 (VIEW_CONVERT_EXPR
,
7960 build_array_type_nelts (TREE_TYPE (type
),
7961 TYPE_VECTOR_SUBPARTS (type
)),
7967 /* Determine which of the operands, if any, is a scalar that needs to be
7968 converted to a vector, for the range of operations. */
7970 scalar_to_vector (location_t loc
, enum tree_code code
, tree op0
, tree op1
,
7973 tree type0
= TREE_TYPE (op0
);
7974 tree type1
= TREE_TYPE (op1
);
7975 bool integer_only_op
= false;
7976 enum stv_conv ret
= stv_firstarg
;
7978 gcc_assert (VECTOR_TYPE_P (type0
) || VECTOR_TYPE_P (type1
));
7981 /* Most GENERIC binary expressions require homogeneous arguments.
7982 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7983 argument that is a vector and a second one that is a scalar, so
7984 we never return stv_secondarg for them. */
7987 if (TREE_CODE (type0
) == INTEGER_TYPE
7988 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
7990 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
7994 error_at (loc
, "conversion of scalar %qT to vector %qT "
7995 "involves truncation", type0
, type1
);
7999 return stv_firstarg
;
8006 integer_only_op
= true;
8014 case TRUNC_DIV_EXPR
:
8016 case FLOOR_DIV_EXPR
:
8017 case ROUND_DIV_EXPR
:
8018 case EXACT_DIV_EXPR
:
8019 case TRUNC_MOD_EXPR
:
8020 case FLOOR_MOD_EXPR
:
8028 /* What about UNLT_EXPR? */
8029 if (VECTOR_TYPE_P (type0
))
8031 ret
= stv_secondarg
;
8032 std::swap (type0
, type1
);
8033 std::swap (op0
, op1
);
8036 if (TREE_CODE (type0
) == INTEGER_TYPE
8037 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
8039 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
8043 error_at (loc
, "conversion of scalar %qT to vector %qT "
8044 "involves truncation", type0
, type1
);
8049 else if (!integer_only_op
8050 /* Allow integer --> real conversion if safe. */
8051 && (TREE_CODE (type0
) == REAL_TYPE
8052 || TREE_CODE (type0
) == INTEGER_TYPE
)
8053 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1
)))
8055 if (unsafe_conversion_p (loc
, TREE_TYPE (type1
), op0
,
8059 error_at (loc
, "conversion of scalar %qT to vector %qT "
8060 "involves truncation", type0
, type1
);
8072 /* Return the alignment of std::max_align_t.
8074 [support.types.layout] The type max_align_t is a POD type whose alignment
8075 requirement is at least as great as that of every scalar type, and whose
8076 alignment requirement is supported in every context. */
8079 max_align_t_align ()
8081 unsigned int max_align
= MAX (TYPE_ALIGN (long_long_integer_type_node
),
8082 TYPE_ALIGN (long_double_type_node
));
8083 if (float128_type_node
!= NULL_TREE
)
8084 max_align
= MAX (max_align
, TYPE_ALIGN (float128_type_node
));
8088 /* Return true iff ALIGN is an integral constant that is a fundamental
8089 alignment, as defined by [basic.align] in the c++-11
8094 [A fundamental alignment is represented by an alignment less than or
8095 equal to the greatest alignment supported by the implementation
8096 in all contexts, which is equal to alignof(max_align_t)]. */
8099 cxx_fundamental_alignment_p (unsigned align
)
8101 return (align
<= max_align_t_align ());
8104 /* Return true if T is a pointer to a zero-sized aggregate. */
8107 pointer_to_zero_sized_aggr_p (tree t
)
8109 if (!POINTER_TYPE_P (t
))
8112 return (TYPE_SIZE (t
) && integer_zerop (TYPE_SIZE (t
)));
8115 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8116 with no library fallback or for an ADDR_EXPR whose operand is such type
8117 issues an error pointing to the location LOC.
8118 Returns true when the expression has been diagnosed and false
8122 reject_gcc_builtin (const_tree expr
, location_t loc
/* = UNKNOWN_LOCATION */)
8124 if (TREE_CODE (expr
) == ADDR_EXPR
)
8125 expr
= TREE_OPERAND (expr
, 0);
8127 STRIP_ANY_LOCATION_WRAPPER (expr
);
8129 if (TREE_TYPE (expr
)
8130 && TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
8131 && TREE_CODE (expr
) == FUNCTION_DECL
8132 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8133 false positives for user-declared built-ins such as abs or
8134 strlen, and for C++ operators new and delete.
8135 The c_decl_implicit() test avoids false positives for implicitly
8136 declared built-ins with library fallbacks (such as abs). */
8137 && fndecl_built_in_p (expr
)
8138 && DECL_IS_BUILTIN (expr
)
8139 && !c_decl_implicit (expr
)
8140 && !DECL_ASSEMBLER_NAME_SET_P (expr
))
8142 if (loc
== UNKNOWN_LOCATION
)
8143 loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
8145 /* Reject arguments that are built-in functions with
8146 no library fallback. */
8147 error_at (loc
, "built-in function %qE must be directly called", expr
);
8155 /* Issue an ERROR for an invalid SIZE of array NAME which is null
8156 for unnamed arrays. */
8159 invalid_array_size_error (location_t loc
, cst_size_error error
,
8160 const_tree size
, const_tree name
)
8162 tree maxsize
= max_object_size ();
8165 case cst_size_not_constant
:
8167 error_at (loc
, "size of array %qE is not a constant expression",
8170 error_at (loc
, "size of array is not a constant expression");
8172 case cst_size_negative
:
8174 error_at (loc
, "size %qE of array %qE is negative",
8177 error_at (loc
, "size %qE of array is negative",
8180 case cst_size_too_big
:
8182 error_at (loc
, "size %qE of array %qE exceeds maximum "
8183 "object size %qE", size
, name
, maxsize
);
8185 error_at (loc
, "size %qE of array exceeds maximum "
8186 "object size %qE", size
, maxsize
);
8188 case cst_size_overflow
:
8190 error_at (loc
, "size of array %qE exceeds maximum "
8191 "object size %qE", name
, maxsize
);
8193 error_at (loc
, "size of array exceeds maximum "
8194 "object size %qE", maxsize
);
8201 /* Check if array size calculations overflow or if the array covers more
8202 than half of the address space. Return true if the size of the array
8203 is valid, false otherwise. T is either the type of the array or its
8204 size, and NAME is the name of the array, or null for unnamed arrays. */
8207 valid_array_size_p (location_t loc
, const_tree t
, tree name
, bool complain
)
8209 if (t
== error_mark_node
)
8215 if (!COMPLETE_TYPE_P (t
))
8217 size
= TYPE_SIZE_UNIT (t
);
8222 if (TREE_CODE (size
) != INTEGER_CST
)
8225 cst_size_error error
;
8226 if (valid_constant_size_p (size
, &error
))
8232 if (TREE_CODE (TREE_TYPE (size
)) == ENUMERAL_TYPE
)
8233 /* Show the value of the enumerator rather than its name. */
8234 size
= convert (ssizetype
, const_cast<tree
> (size
));
8236 invalid_array_size_error (loc
, error
, size
, name
);
8240 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8241 timestamp to replace embedded current dates to get reproducible
8242 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
8245 cb_get_source_date_epoch (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
8247 char *source_date_epoch
;
8251 source_date_epoch
= getenv ("SOURCE_DATE_EPOCH");
8252 if (!source_date_epoch
)
8256 #if defined(INT64_T_IS_LONG)
8257 epoch
= strtol (source_date_epoch
, &endptr
, 10);
8259 epoch
= strtoll (source_date_epoch
, &endptr
, 10);
8261 if (errno
!= 0 || endptr
== source_date_epoch
|| *endptr
!= '\0'
8262 || epoch
< 0 || epoch
> MAX_SOURCE_DATE_EPOCH
)
8264 error_at (input_location
, "environment variable %qs must "
8265 "expand to a non-negative integer less than or equal to %wd",
8266 "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH
);
8270 return (time_t) epoch
;
8273 /* Callback for libcpp for offering spelling suggestions for misspelled
8274 directives. GOAL is an unrecognized string; CANDIDATES is a
8275 NULL-terminated array of candidate strings. Return the closest
8276 match to GOAL within CANDIDATES, or NULL if none are good
8280 cb_get_suggestion (cpp_reader
*, const char *goal
,
8281 const char *const *candidates
)
8283 best_match
<const char *, const char *> bm (goal
);
8285 bm
.consider (*candidates
++);
8286 return bm
.get_best_meaningful_candidate ();
8289 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8290 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8291 by C TS 18661-3 for interchange types that are computed in their
8292 native precision are larger than the C11 values for evaluating in the
8293 precision of float/double/long double. If either mode is
8294 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8296 enum flt_eval_method
8297 excess_precision_mode_join (enum flt_eval_method x
,
8298 enum flt_eval_method y
)
8300 if (x
== FLT_EVAL_METHOD_UNPREDICTABLE
8301 || y
== FLT_EVAL_METHOD_UNPREDICTABLE
)
8302 return FLT_EVAL_METHOD_UNPREDICTABLE
;
8304 /* GCC only supports one interchange type right now, _Float16. If
8305 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8306 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8307 if (x
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8309 if (y
== FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16
)
8312 /* Other values for flt_eval_method are directly comparable, and we want
8317 /* Return the value that should be set for FLT_EVAL_METHOD in the
8318 context of ISO/IEC TS 18861-3.
8320 This relates to the effective excess precision seen by the user,
8321 which is the join point of the precision the target requests for
8322 -fexcess-precision={standard,fast} and the implicit excess precision
8325 static enum flt_eval_method
8326 c_ts18661_flt_eval_method (void)
8328 enum flt_eval_method implicit
8329 = targetm
.c
.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT
);
8331 enum excess_precision_type flag_type
8332 = (flag_excess_precision_cmdline
== EXCESS_PRECISION_STANDARD
8333 ? EXCESS_PRECISION_TYPE_STANDARD
8334 : EXCESS_PRECISION_TYPE_FAST
);
8336 enum flt_eval_method requested
8337 = targetm
.c
.excess_precision (flag_type
);
8339 return excess_precision_mode_join (implicit
, requested
);
8342 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8343 those that were permitted by C11. That is to say, eliminates
8344 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8346 static enum flt_eval_method
8347 c_c11_flt_eval_method (void)
8349 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8350 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT
);
8353 /* Return the value that should be set for FLT_EVAL_METHOD.
8354 MAYBE_C11_ONLY_P is TRUE if we should check
8355 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8356 values we can return to those from C99/C11, and FALSE otherwise.
8357 See the comments on c_ts18661_flt_eval_method for what value we choose
8361 c_flt_eval_method (bool maybe_c11_only_p
)
8363 if (maybe_c11_only_p
8364 && flag_permitted_flt_eval_methods
8365 == PERMITTED_FLT_EVAL_METHODS_C11
)
8366 return c_c11_flt_eval_method ();
8368 return c_ts18661_flt_eval_method ();
8371 /* An enum for get_missing_token_insertion_kind for describing the best
8372 place to insert a missing token, if there is one. */
8374 enum missing_token_insertion_kind
8377 MTIK_INSERT_BEFORE_NEXT
,
8378 MTIK_INSERT_AFTER_PREV
8381 /* Given a missing token of TYPE, determine if it is reasonable to
8382 emit a fix-it hint suggesting the insertion of the token, and,
8383 if so, where the token should be inserted relative to other tokens.
8385 It only makes sense to do this for values of TYPE that are symbols.
8387 Some symbols should go before the next token, e.g. in:
8389 we want to insert the missing '(' immediately before "flag",
8394 These use MTIK_INSERT_BEFORE_NEXT.
8396 Other symbols should go after the previous token, e.g. in:
8399 we want to insert the missing ')' immediately after the "flag",
8406 These use MTIK_INSERT_AFTER_PREV. */
8408 static enum missing_token_insertion_kind
8409 get_missing_token_insertion_kind (enum cpp_ttype type
)
8413 /* Insert missing "opening" brackets immediately
8414 before the next token. */
8415 case CPP_OPEN_SQUARE
:
8416 case CPP_OPEN_PAREN
:
8417 return MTIK_INSERT_BEFORE_NEXT
;
8419 /* Insert other missing symbols immediately after
8420 the previous token. */
8421 case CPP_CLOSE_PAREN
:
8422 case CPP_CLOSE_SQUARE
:
8426 return MTIK_INSERT_AFTER_PREV
;
8428 /* Other kinds of token don't get fix-it hints. */
8430 return MTIK_IMPOSSIBLE
;
8434 /* Given RICHLOC, a location for a diagnostic describing a missing token
8435 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8436 insertion of the token.
8438 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8440 (a) immediately after PREV_TOKEN_LOC, or
8442 (b) immediately before the primary location within RICHLOC (taken to
8443 be that of the token following where the token was expected).
8445 If we manage to add a fix-it hint, then the location of the
8446 fix-it hint is likely to be more useful as the primary location
8447 of the diagnostic than that of the following token, so we swap
8450 For example, given this bogus code:
8451 123456789012345678901234567890
8452 1 | int missing_semicolon (void)
8459 "expected ';' before '}'"
8461 RICHLOC's primary location is at the closing brace, so before "swapping"
8462 we would emit the error at line 4 column 1:
8464 123456789012345678901234567890
8465 3 | return 42 |< fix-it hint emitted for this line
8467 4 | } |< "expected ';' before '}'" emitted at this line
8470 It's more useful for the location of the diagnostic to be at the
8471 fix-it hint, so we swap the locations, so the primary location
8472 is at the fix-it hint, with the old primary location inserted
8473 as a secondary location, giving this, with the error at line 3
8476 123456789012345678901234567890
8477 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8478 | ^ | with fix-it hint
8480 | } |< secondary range emitted here
8484 maybe_suggest_missing_token_insertion (rich_location
*richloc
,
8485 enum cpp_ttype token_type
,
8486 location_t prev_token_loc
)
8488 gcc_assert (richloc
);
8490 enum missing_token_insertion_kind mtik
8491 = get_missing_token_insertion_kind (token_type
);
8499 case MTIK_IMPOSSIBLE
:
8502 case MTIK_INSERT_BEFORE_NEXT
:
8503 /* Attempt to add the fix-it hint before the primary location
8505 richloc
->add_fixit_insert_before (cpp_type2name (token_type
, 0));
8508 case MTIK_INSERT_AFTER_PREV
:
8509 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8510 richloc
->add_fixit_insert_after (prev_token_loc
,
8511 cpp_type2name (token_type
, 0));
8515 /* If we were successful, use the fix-it hint's location as the
8516 primary location within RICHLOC, adding the old primary location
8517 back as a secondary location. */
8518 if (!richloc
->seen_impossible_fixit_p ())
8520 fixit_hint
*hint
= richloc
->get_last_fixit_hint ();
8521 location_t hint_loc
= hint
->get_start_loc ();
8522 location_t old_loc
= richloc
->get_loc ();
8524 richloc
->set_range (0, hint_loc
, SHOW_RANGE_WITH_CARET
);
8525 richloc
->add_range (old_loc
);
8531 namespace selftest
{
8533 /* Verify that fold_for_warn on error_mark_node is safe. */
8536 test_fold_for_warn ()
8538 ASSERT_EQ (error_mark_node
, fold_for_warn (error_mark_node
));
8541 /* Run all of the selftests within this file. */
8546 test_fold_for_warn ();
8549 /* Run all of the tests within c-family. */
8552 c_family_tests (void)
8554 c_common_c_tests ();
8555 c_format_c_tests ();
8556 c_indentation_c_tests ();
8557 c_pretty_print_c_tests ();
8558 c_spellcheck_cc_tests ();
8561 } // namespace selftest
8563 #endif /* #if CHECKING_P */
8565 /* Attempt to locate a suitable location within FILE for a
8566 #include directive to be inserted before. FILE should
8567 be a string from libcpp (pointer equality is used).
8568 LOC is the location of the relevant diagnostic.
8570 Attempt to return the location within FILE immediately
8571 after the last #include within that file, or the start of
8572 that file if it has no #include directives.
8574 Return UNKNOWN_LOCATION if no suitable location is found,
8575 or if an error occurs. */
8578 try_to_locate_new_include_insertion_point (const char *file
, location_t loc
)
8580 /* Locate the last ordinary map within FILE that ended with a #include. */
8581 const line_map_ordinary
*last_include_ord_map
= NULL
;
8583 /* ...and the next ordinary map within FILE after that one. */
8584 const line_map_ordinary
*last_ord_map_after_include
= NULL
;
8586 /* ...and the first ordinary map within FILE. */
8587 const line_map_ordinary
*first_ord_map_in_file
= NULL
;
8589 /* Get ordinary map containing LOC (or its expansion). */
8590 const line_map_ordinary
*ord_map_for_loc
= NULL
;
8591 loc
= linemap_resolve_location (line_table
, loc
, LRK_MACRO_EXPANSION_POINT
,
8593 gcc_assert (ord_map_for_loc
);
8595 for (unsigned int i
= 0; i
< LINEMAPS_ORDINARY_USED (line_table
); i
++)
8597 const line_map_ordinary
*ord_map
8598 = LINEMAPS_ORDINARY_MAP_AT (line_table
, i
);
8600 if (const line_map_ordinary
*from
8601 = linemap_included_from_linemap (line_table
, ord_map
))
8602 if (from
->to_file
== file
)
8604 last_include_ord_map
= from
;
8605 last_ord_map_after_include
= NULL
;
8608 if (ord_map
->to_file
== file
)
8610 if (!first_ord_map_in_file
)
8611 first_ord_map_in_file
= ord_map
;
8612 if (last_include_ord_map
&& !last_ord_map_after_include
)
8613 last_ord_map_after_include
= ord_map
;
8616 /* Stop searching when reaching the ord_map containing LOC,
8617 as it makes no sense to provide fix-it hints that appear
8618 after the diagnostic in question. */
8619 if (ord_map
== ord_map_for_loc
)
8623 /* Determine where to insert the #include. */
8624 const line_map_ordinary
*ord_map_for_insertion
;
8626 /* We want the next ordmap in the file after the last one that's a
8627 #include, but failing that, the start of the file. */
8628 if (last_ord_map_after_include
)
8629 ord_map_for_insertion
= last_ord_map_after_include
;
8631 ord_map_for_insertion
= first_ord_map_in_file
;
8633 if (!ord_map_for_insertion
)
8634 return UNKNOWN_LOCATION
;
8636 /* The "start_location" is column 0, meaning "the whole line".
8637 rich_location and edit_context can't cope with this, so use
8638 column 1 instead. */
8639 location_t col_0
= ord_map_for_insertion
->start_location
;
8640 return linemap_position_for_loc_and_offset (line_table
, col_0
, 1);
8643 /* A map from filenames to sets of headers added to them, for
8644 ensuring idempotency within maybe_add_include_fixit. */
8646 /* The values within the map. We need string comparison as there's
8647 no guarantee that two different diagnostics that are recommending
8648 adding e.g. "<stdio.h>" are using the same buffer. */
8650 typedef hash_set
<const char *, false, nofree_string_hash
> per_file_includes_t
;
8652 /* The map itself. We don't need string comparison for the filename keys,
8653 as they come from libcpp. */
8655 typedef hash_map
<const char *, per_file_includes_t
*> added_includes_t
;
8656 static added_includes_t
*added_includes
;
8658 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8659 in a suitable location within the file of RICHLOC's primary
8662 This function is idempotent: a header will be added at most once to
8665 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8666 printed, then RICHLOC's primary location will be replaced by that of
8667 the fix-it hint (for use by "inform" notes where the location of the
8668 issue has already been reported). */
8671 maybe_add_include_fixit (rich_location
*richloc
, const char *header
,
8672 bool override_location
)
8674 location_t loc
= richloc
->get_loc ();
8675 const char *file
= LOCATION_FILE (loc
);
8679 /* Idempotency: don't add the same header more than once to a given file. */
8680 if (!added_includes
)
8681 added_includes
= new added_includes_t ();
8682 per_file_includes_t
*&set
= added_includes
->get_or_insert (file
);
8684 if (set
->contains (header
))
8685 /* ...then we've already added HEADER to that file. */
8688 set
= new per_file_includes_t ();
8691 /* Attempt to locate a suitable place for the new directive. */
8692 location_t include_insert_loc
8693 = try_to_locate_new_include_insertion_point (file
, loc
);
8694 if (include_insert_loc
== UNKNOWN_LOCATION
)
8697 char *text
= xasprintf ("#include %s\n", header
);
8698 richloc
->add_fixit_insert_before (include_insert_loc
, text
);
8701 if (override_location
&& global_dc
->show_caret
)
8703 /* Replace the primary location with that of the insertion point for the
8706 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8707 caret for the insertion point (or colorize it).
8709 Hence we print e.g.:
8711 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8712 73 | # include <debug/vector>
8713 +++ |+#include <vector>
8718 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8719 73 | # include <debug/vector>
8720 +++ |+#include <vector>
8724 avoiding the caret on the first column of line 74. */
8725 richloc
->set_range (0, include_insert_loc
, SHOW_LINES_WITHOUT_RANGE
);
8729 /* Attempt to convert a braced array initializer list CTOR for array
8730 TYPE into a STRING_CST for convenience and efficiency. Return
8731 the converted string on success or the original ctor on failure. */
8734 braced_list_to_string (tree type
, tree ctor
)
8736 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
8739 /* If the array has an explicit bound, use it to constrain the size
8740 of the string. If it doesn't, be sure to create a string that's
8741 as long as implied by the index of the last zero specified via
8742 a designator, as in:
8743 const char a[] = { [7] = 0 }; */
8744 unsigned HOST_WIDE_INT maxelts
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
8745 maxelts
/= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type
)));
8747 /* Avoid converting initializers for zero-length arrays. */
8751 unsigned HOST_WIDE_INT nelts
= CONSTRUCTOR_NELTS (ctor
);
8754 str
.reserve (nelts
+ 1);
8756 unsigned HOST_WIDE_INT i
;
8759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, index
, value
)
8761 unsigned HOST_WIDE_INT idx
= i
;
8764 if (!tree_fits_uhwi_p (index
))
8766 idx
= tree_to_uhwi (index
);
8769 /* auto_vec is limited to UINT_MAX elements. */
8773 /* Avoid non-constant initializers. */
8774 if (!tree_fits_shwi_p (value
))
8777 /* Skip over embedded nuls except the last one (initializer
8778 elements are in ascending order of indices). */
8779 HOST_WIDE_INT val
= tree_to_shwi (value
);
8780 if (!val
&& i
+ 1 < nelts
)
8783 if (idx
< str
.length())
8786 /* Bail if the CTOR has a block of more than 256 embedded nuls
8787 due to implicitly initialized elements. */
8788 unsigned nchars
= (idx
- str
.length ()) + 1;
8795 str
.quick_grow_cleared (idx
);
8801 str
.safe_insert (idx
, val
);
8804 /* Append a nul string termination. */
8805 if (str
.length () < maxelts
)
8808 /* Build a STRING_CST with the same type as the array. */
8809 tree res
= build_string (str
.length (), str
.begin ());
8810 TREE_TYPE (res
) = type
;
8814 /* Attempt to convert a CTOR containing braced array initializer lists
8815 for array TYPE into one containing STRING_CSTs, for convenience and
8816 efficiency. Recurse for arrays of arrays and member initializers.
8817 Return the converted CTOR or STRING_CST on success or the original
8821 braced_lists_to_strings (tree type
, tree ctor
)
8823 if (TREE_CODE (ctor
) != CONSTRUCTOR
)
8826 tree_code code
= TREE_CODE (type
);
8829 if (code
== ARRAY_TYPE
)
8830 ttp
= TREE_TYPE (type
);
8831 else if (code
== RECORD_TYPE
)
8833 ttp
= TREE_TYPE (ctor
);
8834 if (TREE_CODE (ttp
) == ARRAY_TYPE
)
8837 ttp
= TREE_TYPE (ttp
);
8843 if (TYPE_STRING_FLAG (ttp
))
8844 return braced_list_to_string (type
, ctor
);
8846 code
= TREE_CODE (ttp
);
8847 if (code
== ARRAY_TYPE
|| code
== RECORD_TYPE
)
8849 /* Handle array of arrays or struct member initializers. */
8851 unsigned HOST_WIDE_INT idx
;
8852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, val
)
8854 val
= braced_lists_to_strings (ttp
, val
);
8855 CONSTRUCTOR_ELT (ctor
, idx
)->value
= val
;
8862 #include "gt-c-family-c-common.h"