PR rtl-optimization/88470
[official-gcc.git] / gcc / c-family / c-common.c
blob4c903650650d2a42f044e7a99012ee4040f0e4c4
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2018 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
9 version.
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
14 for more details.
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
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "c-common.h"
30 #include "gimple-expr.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "trans-mem.h"
41 #include "c-objc.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "toplev.h"
46 #include "tree-iterator.h"
47 #include "opts.h"
48 #include "gimplify.h"
49 #include "substring-locations.h"
50 #include "spellcheck.h"
51 #include "selftest.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;
80 tree wchar_type_node;
82 tree char16_type_node;
83 tree char32_type_node;
85 tree float_type_node;
86 tree double_type_node;
87 tree long_double_type_node;
89 tree complex_integer_type_node;
90 tree complex_float_type_node;
91 tree complex_double_type_node;
92 tree complex_long_double_type_node;
94 tree dfloat32_type_node;
95 tree dfloat64_type_node;
96 tree_dfloat128_type_node;
98 tree intQI_type_node;
99 tree intHI_type_node;
100 tree intSI_type_node;
101 tree intDI_type_node;
102 tree intTI_type_node;
104 tree unsigned_intQI_type_node;
105 tree unsigned_intHI_type_node;
106 tree unsigned_intSI_type_node;
107 tree unsigned_intDI_type_node;
108 tree unsigned_intTI_type_node;
110 tree widest_integer_literal_type_node;
111 tree widest_unsigned_literal_type_node;
113 Nodes for types `void *' and `const void *'.
115 tree ptr_type_node, const_ptr_type_node;
117 Nodes for types `char *' and `const char *'.
119 tree string_type_node, const_string_type_node;
121 Type `char[SOMENUMBER]'.
122 Used when an array of char is needed and the size is irrelevant.
124 tree char_array_type_node;
126 Type `wchar_t[SOMENUMBER]' or something like it.
127 Used when a wide string literal is created.
129 tree wchar_array_type_node;
131 Type `char16_t[SOMENUMBER]' or something like it.
132 Used when a UTF-16 string literal is created.
134 tree char16_array_type_node;
136 Type `char32_t[SOMENUMBER]' or something like it.
137 Used when a UTF-32 string literal is created.
139 tree char32_array_type_node;
141 Type `int ()' -- used for implicit declaration of functions.
143 tree default_function_type;
145 A VOID_TYPE node, packaged in a TREE_LIST.
147 tree void_list_node;
149 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
150 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
151 VAR_DECLS, but C++ does.)
153 tree function_name_decl_node;
154 tree pretty_function_name_decl_node;
155 tree c99_function_name_decl_node;
157 Stack of nested function name VAR_DECLs.
159 tree saved_function_name_decls;
163 tree c_global_trees[CTI_MAX];
165 /* Switches common to the C front ends. */
167 /* Nonzero means don't output line number information. */
169 char flag_no_line_commands;
171 /* Nonzero causes -E output not to be done, but directives such as
172 #define that have side effects are still obeyed. */
174 char flag_no_output;
176 /* Nonzero means dump macros in some fashion. */
178 char flag_dump_macros;
180 /* Nonzero means pass #include lines through to the output. */
182 char flag_dump_includes;
184 /* Nonzero means process PCH files while preprocessing. */
186 bool flag_pch_preprocess;
188 /* The file name to which we should write a precompiled header, or
189 NULL if no header will be written in this compile. */
191 const char *pch_file;
193 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 user's namespace. */
195 int flag_iso;
197 /* C/ObjC language option variables. */
200 /* Nonzero means allow type mismatches in conditional expressions;
201 just make their values `void'. */
203 int flag_cond_mismatch;
205 /* Nonzero means enable C89 Amendment 1 features. */
207 int flag_isoc94;
209 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
211 int flag_isoc99;
213 /* Nonzero means use the ISO C11 dialect of C. */
215 int flag_isoc11;
217 /* Nonzero means use the ISO C2X dialect of C. */
219 int flag_isoc2x;
221 /* Nonzero means that we have builtin functions, and main is an int. */
223 int flag_hosted = 1;
226 /* ObjC language option variables. */
229 /* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
233 int print_struct_values;
235 /* Tells the compiler what is the constant string class for ObjC. */
237 const char *constant_string_class_name;
240 /* C++ language option variables. */
242 /* The reference version of the ABI for -Wabi. */
244 int warn_abi_version = -1;
246 /* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
249 int flag_use_repository;
251 /* The C++ dialect being used. Default set in c_common_post_options. */
253 enum cxx_dialect cxx_dialect = cxx_unset;
255 /* Maximum template instantiation depth. This limit exists to limit the
256 time it takes to notice excessively recursive template instantiations.
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
262 int max_tinst_depth = 900;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266 tree *ridpointers;
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272 int c_inhibit_evaluation_warnings;
274 /* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280 bool in_late_binary_op;
282 /* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284 bool done_lexing = false;
286 /* Information about how a function name is generated. */
287 struct fname_var_t
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
294 /* The three ways of getting then name of the current function. */
296 const struct fname_var_t fname_vars[] =
298 /* C99 compliant __func__, must be first. */
299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
300 /* GCC __FUNCTION__ compliant. */
301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
302 /* GCC __PRETTY_FUNCTION__ compliant. */
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
307 /* Global visibility options. */
308 struct visibility_flags visibility_options;
310 static tree check_case_value (location_t, tree);
311 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
312 bool *);
315 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
316 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
318 /* Reserved words. The third field is a mask: keywords are disabled
319 if they match the mask.
321 Masks for languages:
322 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
323 C --std=c99: D_CXXONLY | D_OBJC
324 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
325 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
326 C++ --std=c++11: D_CONLY | D_OBJC
327 ObjC++ is like C++ except that D_OBJC is not set
329 If -fno-asm is used, D_ASM is added to the mask. If
330 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
331 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
332 In C with -Wc++-compat, we warn if D_CXXWARN is set.
334 Note the complication of the D_CXX_OBJC keywords. These are
335 reserved words such as 'class'. In C++, 'class' is a reserved
336 word. In Objective-C++ it is too. In Objective-C, it is a
337 reserved word too, but only if it follows an '@' sign.
339 const struct c_common_resword c_common_reswords[] =
341 { "_Alignas", RID_ALIGNAS, D_CONLY },
342 { "_Alignof", RID_ALIGNOF, D_CONLY },
343 { "_Atomic", RID_ATOMIC, D_CONLY },
344 { "_Bool", RID_BOOL, D_CONLY },
345 { "_Complex", RID_COMPLEX, 0 },
346 { "_Imaginary", RID_IMAGINARY, D_CONLY },
347 { "_Float16", RID_FLOAT16, D_CONLY },
348 { "_Float32", RID_FLOAT32, D_CONLY },
349 { "_Float64", RID_FLOAT64, D_CONLY },
350 { "_Float128", RID_FLOAT128, D_CONLY },
351 { "_Float32x", RID_FLOAT32X, D_CONLY },
352 { "_Float64x", RID_FLOAT64X, D_CONLY },
353 { "_Float128x", RID_FLOAT128X, D_CONLY },
354 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
355 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
356 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
357 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
358 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
359 { "_Sat", RID_SAT, D_CONLY | D_EXT },
360 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
361 { "_Noreturn", RID_NORETURN, D_CONLY },
362 { "_Generic", RID_GENERIC, D_CONLY },
363 { "_Thread_local", RID_THREAD, D_CONLY },
364 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
365 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
366 { "__alignof", RID_ALIGNOF, 0 },
367 { "__alignof__", RID_ALIGNOF, 0 },
368 { "__asm", RID_ASM, 0 },
369 { "__asm__", RID_ASM, 0 },
370 { "__attribute", RID_ATTRIBUTE, 0 },
371 { "__attribute__", RID_ATTRIBUTE, 0 },
372 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
373 { "__bases", RID_BASES, D_CXXONLY },
374 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
375 { "__builtin_call_with_static_chain",
376 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
377 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
378 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
379 { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
380 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
381 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
382 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
383 { "__builtin_offsetof", RID_OFFSETOF, 0 },
384 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
385 { "__builtin_va_arg", RID_VA_ARG, 0 },
386 { "__complex", RID_COMPLEX, 0 },
387 { "__complex__", RID_COMPLEX, 0 },
388 { "__const", RID_CONST, 0 },
389 { "__const__", RID_CONST, 0 },
390 { "__decltype", RID_DECLTYPE, D_CXXONLY },
391 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
392 { "__extension__", RID_EXTENSION, 0 },
393 { "__func__", RID_C99_FUNCTION_NAME, 0 },
394 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
395 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
396 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
397 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
398 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
399 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
400 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
401 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
402 D_CXXONLY },
403 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
404 { "__imag", RID_IMAGPART, 0 },
405 { "__imag__", RID_IMAGPART, 0 },
406 { "__inline", RID_INLINE, 0 },
407 { "__inline__", RID_INLINE, 0 },
408 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
409 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
410 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
411 { "__is_class", RID_IS_CLASS, D_CXXONLY },
412 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
413 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
414 { "__is_final", RID_IS_FINAL, D_CXXONLY },
415 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
416 { "__is_pod", RID_IS_POD, D_CXXONLY },
417 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
418 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
419 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
420 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
421 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
422 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
423 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
424 { "__is_union", RID_IS_UNION, D_CXXONLY },
425 { "__label__", RID_LABEL, 0 },
426 { "__null", RID_NULL, 0 },
427 { "__real", RID_REALPART, 0 },
428 { "__real__", RID_REALPART, 0 },
429 { "__restrict", RID_RESTRICT, 0 },
430 { "__restrict__", RID_RESTRICT, 0 },
431 { "__signed", RID_SIGNED, 0 },
432 { "__signed__", RID_SIGNED, 0 },
433 { "__thread", RID_THREAD, 0 },
434 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
435 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
436 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
437 { "__typeof", RID_TYPEOF, 0 },
438 { "__typeof__", RID_TYPEOF, 0 },
439 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
440 { "__volatile", RID_VOLATILE, 0 },
441 { "__volatile__", RID_VOLATILE, 0 },
442 { "__GIMPLE", RID_GIMPLE, D_CONLY },
443 { "__PHI", RID_PHI, D_CONLY },
444 { "__RTL", RID_RTL, D_CONLY },
445 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
446 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
447 { "asm", RID_ASM, D_ASM },
448 { "auto", RID_AUTO, 0 },
449 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
450 { "break", RID_BREAK, 0 },
451 { "case", RID_CASE, 0 },
452 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
453 { "char", RID_CHAR, 0 },
454 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
455 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
456 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
457 { "const", RID_CONST, 0 },
458 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
459 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
460 { "continue", RID_CONTINUE, 0 },
461 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
462 { "default", RID_DEFAULT, 0 },
463 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
464 { "do", RID_DO, 0 },
465 { "double", RID_DOUBLE, 0 },
466 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
467 { "else", RID_ELSE, 0 },
468 { "enum", RID_ENUM, 0 },
469 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
470 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
471 { "extern", RID_EXTERN, 0 },
472 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
473 { "float", RID_FLOAT, 0 },
474 { "for", RID_FOR, 0 },
475 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
476 { "goto", RID_GOTO, 0 },
477 { "if", RID_IF, 0 },
478 { "inline", RID_INLINE, D_EXT89 },
479 { "int", RID_INT, 0 },
480 { "long", RID_LONG, 0 },
481 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
482 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
483 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
484 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
485 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
486 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
487 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
488 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
489 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
490 { "register", RID_REGISTER, 0 },
491 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
492 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
493 { "return", RID_RETURN, 0 },
494 { "short", RID_SHORT, 0 },
495 { "signed", RID_SIGNED, 0 },
496 { "sizeof", RID_SIZEOF, 0 },
497 { "static", RID_STATIC, 0 },
498 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
499 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
500 { "struct", RID_STRUCT, 0 },
501 { "switch", RID_SWITCH, 0 },
502 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
503 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
504 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
505 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
506 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
507 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
508 { "typedef", RID_TYPEDEF, 0 },
509 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
510 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
511 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
512 { "union", RID_UNION, 0 },
513 { "unsigned", RID_UNSIGNED, 0 },
514 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
515 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
516 { "void", RID_VOID, 0 },
517 { "volatile", RID_VOLATILE, 0 },
518 { "wchar_t", RID_WCHAR, D_CXXONLY },
519 { "while", RID_WHILE, 0 },
520 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
521 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
523 /* C++ transactional memory. */
524 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
525 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
526 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
527 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
529 /* Concepts-related keywords */
530 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
531 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
533 /* These Objective-C keywords are recognized only immediately after
534 an '@'. */
535 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
536 { "defs", RID_AT_DEFS, D_OBJC },
537 { "encode", RID_AT_ENCODE, D_OBJC },
538 { "end", RID_AT_END, D_OBJC },
539 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
540 { "interface", RID_AT_INTERFACE, D_OBJC },
541 { "protocol", RID_AT_PROTOCOL, D_OBJC },
542 { "selector", RID_AT_SELECTOR, D_OBJC },
543 { "finally", RID_AT_FINALLY, D_OBJC },
544 { "optional", RID_AT_OPTIONAL, D_OBJC },
545 { "required", RID_AT_REQUIRED, D_OBJC },
546 { "property", RID_AT_PROPERTY, D_OBJC },
547 { "package", RID_AT_PACKAGE, D_OBJC },
548 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
549 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
550 /* These are recognized only in protocol-qualifier context
551 (see above) */
552 { "bycopy", RID_BYCOPY, D_OBJC },
553 { "byref", RID_BYREF, D_OBJC },
554 { "in", RID_IN, D_OBJC },
555 { "inout", RID_INOUT, D_OBJC },
556 { "oneway", RID_ONEWAY, D_OBJC },
557 { "out", RID_OUT, D_OBJC },
558 /* These are recognized inside a property attribute list */
559 { "assign", RID_ASSIGN, D_OBJC },
560 { "copy", RID_COPY, D_OBJC },
561 { "getter", RID_GETTER, D_OBJC },
562 { "nonatomic", RID_NONATOMIC, D_OBJC },
563 { "readonly", RID_READONLY, D_OBJC },
564 { "readwrite", RID_READWRITE, D_OBJC },
565 { "retain", RID_RETAIN, D_OBJC },
566 { "setter", RID_SETTER, D_OBJC },
569 const unsigned int num_c_common_reswords =
570 sizeof c_common_reswords / sizeof (struct c_common_resword);
572 /* Return identifier for address space AS. */
574 const char *
575 c_addr_space_name (addr_space_t as)
577 int rid = RID_FIRST_ADDR_SPACE + as;
578 gcc_assert (ridpointers [rid]);
579 return IDENTIFIER_POINTER (ridpointers [rid]);
582 /* Push current bindings for the function name VAR_DECLS. */
584 void
585 start_fname_decls (void)
587 unsigned ix;
588 tree saved = NULL_TREE;
590 for (ix = 0; fname_vars[ix].decl; ix++)
592 tree decl = *fname_vars[ix].decl;
594 if (decl)
596 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
597 saved);
598 *fname_vars[ix].decl = NULL_TREE;
601 if (saved || saved_function_name_decls)
602 /* Normally they'll have been NULL, so only push if we've got a
603 stack, or they are non-NULL. */
604 saved_function_name_decls = tree_cons (saved, NULL_TREE,
605 saved_function_name_decls);
608 /* Finish up the current bindings, adding them into the current function's
609 statement tree. This must be done _before_ finish_stmt_tree is called.
610 If there is no current function, we must be at file scope and no statements
611 are involved. Pop the previous bindings. */
613 void
614 finish_fname_decls (void)
616 unsigned ix;
617 tree stmts = NULL_TREE;
618 tree stack = saved_function_name_decls;
620 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
621 append_to_statement_list (TREE_VALUE (stack), &stmts);
623 if (stmts)
625 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
627 if (TREE_CODE (*bodyp) == BIND_EXPR)
628 bodyp = &BIND_EXPR_BODY (*bodyp);
630 append_to_statement_list_force (*bodyp, &stmts);
631 *bodyp = stmts;
634 for (ix = 0; fname_vars[ix].decl; ix++)
635 *fname_vars[ix].decl = NULL_TREE;
637 if (stack)
639 /* We had saved values, restore them. */
640 tree saved;
642 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
644 tree decl = TREE_PURPOSE (saved);
645 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
647 *fname_vars[ix].decl = decl;
649 stack = TREE_CHAIN (stack);
651 saved_function_name_decls = stack;
654 /* Return the text name of the current function, suitably prettified
655 by PRETTY_P. Return string must be freed by caller. */
657 const char *
658 fname_as_string (int pretty_p)
660 const char *name = "top level";
661 char *namep;
662 int vrb = 2, len;
663 cpp_string cstr = { 0, 0 }, strname;
665 if (!pretty_p)
667 name = "";
668 vrb = 0;
671 if (current_function_decl)
672 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
674 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
676 namep = XNEWVEC (char, len);
677 snprintf (namep, len, "\"%s\"", name);
678 strname.text = (unsigned char *) namep;
679 strname.len = len - 1;
681 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
683 XDELETEVEC (namep);
684 return (const char *) cstr.text;
687 return namep;
690 /* Return the VAR_DECL for a const char array naming the current
691 function. If the VAR_DECL has not yet been created, create it
692 now. RID indicates how it should be formatted and IDENTIFIER_NODE
693 ID is its name (unfortunately C and C++ hold the RID values of
694 keywords in different places, so we can't derive RID from ID in
695 this language independent code. LOC is the location of the
696 function. */
698 tree
699 fname_decl (location_t loc, unsigned int rid, tree id)
701 unsigned ix;
702 tree decl = NULL_TREE;
704 for (ix = 0; fname_vars[ix].decl; ix++)
705 if (fname_vars[ix].rid == rid)
706 break;
708 decl = *fname_vars[ix].decl;
709 if (!decl)
711 /* If a tree is built here, it would normally have the lineno of
712 the current statement. Later this tree will be moved to the
713 beginning of the function and this line number will be wrong.
714 To avoid this problem set the lineno to 0 here; that prevents
715 it from appearing in the RTL. */
716 tree stmts;
717 location_t saved_location = input_location;
718 input_location = UNKNOWN_LOCATION;
720 stmts = push_stmt_list ();
721 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
722 stmts = pop_stmt_list (stmts);
723 if (!IS_EMPTY_STMT (stmts))
724 saved_function_name_decls
725 = tree_cons (decl, stmts, saved_function_name_decls);
726 *fname_vars[ix].decl = decl;
727 input_location = saved_location;
729 if (!ix && !current_function_decl)
730 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
732 return decl;
735 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
737 tree
738 fix_string_type (tree value)
740 int length = TREE_STRING_LENGTH (value);
741 int nchars, charsz;
742 tree e_type, i_type, a_type;
744 /* Compute the number of elements, for the array type. */
745 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
747 charsz = 1;
748 e_type = char_type_node;
750 else if (TREE_TYPE (value) == char16_array_type_node)
752 charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
753 e_type = char16_type_node;
755 else if (TREE_TYPE (value) == char32_array_type_node)
757 charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
758 e_type = char32_type_node;
760 else
762 charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
763 e_type = wchar_type_node;
766 /* This matters only for targets where ssizetype has smaller precision
767 than 32 bits. */
768 if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
770 error ("size of string literal is too large");
771 length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
772 char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
773 memset (str + length, '\0',
774 MIN (TREE_STRING_LENGTH (value) - length, charsz));
775 TREE_STRING_LENGTH (value) = length;
777 nchars = length / charsz;
779 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
780 limit in C++98 Annex B is very large (65536) and is not normative,
781 so we do not diagnose it (warn_overlength_strings is forced off
782 in c_common_post_options). */
783 if (warn_overlength_strings)
785 const int nchars_max = flag_isoc99 ? 4095 : 509;
786 const int relevant_std = flag_isoc99 ? 99 : 90;
787 if (nchars - 1 > nchars_max)
788 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
789 separate the %d from the 'C'. 'ISO' should not be
790 translated, but it may be moved after 'C%d' in languages
791 where modifiers follow nouns. */
792 pedwarn (input_location, OPT_Woverlength_strings,
793 "string length %qd is greater than the length %qd "
794 "ISO C%d compilers are required to support",
795 nchars - 1, nchars_max, relevant_std);
798 /* Create the array type for the string constant. The ISO C++
799 standard says that a string literal has type `const char[N]' or
800 `const wchar_t[N]'. We use the same logic when invoked as a C
801 front-end with -Wwrite-strings.
802 ??? We should change the type of an expression depending on the
803 state of a warning flag. We should just be warning -- see how
804 this is handled in the C++ front-end for the deprecated implicit
805 conversion from string literals to `char*' or `wchar_t*'.
807 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
808 array type being the unqualified version of that type.
809 Therefore, if we are constructing an array of const char, we must
810 construct the matching unqualified array type first. The C front
811 end does not require this, but it does no harm, so we do it
812 unconditionally. */
813 i_type = build_index_type (size_int (nchars - 1));
814 a_type = build_array_type (e_type, i_type);
815 if (c_dialect_cxx() || warn_write_strings)
816 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
818 TREE_TYPE (value) = a_type;
819 TREE_CONSTANT (value) = 1;
820 TREE_READONLY (value) = 1;
821 TREE_STATIC (value) = 1;
822 return value;
825 /* Given a string of type STRING_TYPE, determine what kind of string
826 token would give an equivalent execution encoding: CPP_STRING,
827 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
828 This may not be exactly the string token type that initially created
829 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
830 string type at this point.
832 This effectively reverses part of the logic in lex_string and
833 fix_string_type. */
835 static enum cpp_ttype
836 get_cpp_ttype_from_string_type (tree string_type)
838 gcc_assert (string_type);
839 if (TREE_CODE (string_type) == POINTER_TYPE)
840 string_type = TREE_TYPE (string_type);
842 if (TREE_CODE (string_type) != ARRAY_TYPE)
843 return CPP_OTHER;
845 tree element_type = TREE_TYPE (string_type);
846 if (TREE_CODE (element_type) != INTEGER_TYPE)
847 return CPP_OTHER;
849 int bits_per_character = TYPE_PRECISION (element_type);
850 switch (bits_per_character)
852 case 8:
853 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
854 case 16:
855 return CPP_STRING16;
856 case 32:
857 return CPP_STRING32;
860 return CPP_OTHER;
863 /* The global record of string concatentations, for use in
864 extracting locations within string literals. */
866 GTY(()) string_concat_db *g_string_concat_db;
868 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
870 const char *
871 c_get_substring_location (const substring_loc &substr_loc,
872 location_t *out_loc)
874 enum cpp_ttype tok_type
875 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
876 if (tok_type == CPP_OTHER)
877 return "unrecognized string type";
879 return get_location_within_string (parse_in, g_string_concat_db,
880 substr_loc.get_fmt_string_loc (),
881 tok_type,
882 substr_loc.get_caret_idx (),
883 substr_loc.get_start_idx (),
884 substr_loc.get_end_idx (),
885 out_loc);
889 /* Return true iff T is a boolean promoted to int. */
891 bool
892 bool_promoted_to_int_p (tree t)
894 return (CONVERT_EXPR_P (t)
895 && TREE_TYPE (t) == integer_type_node
896 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
899 /* vector_targets_convertible_p is used for vector pointer types. The
900 callers perform various checks that the qualifiers are satisfactory,
901 while OTOH vector_targets_convertible_p ignores the number of elements
902 in the vectors. That's fine with vector pointers as we can consider,
903 say, a vector of 8 elements as two consecutive vectors of 4 elements,
904 and that does not require and conversion of the pointer values.
905 In contrast, vector_types_convertible_p and
906 vector_types_compatible_elements_p are used for vector value types. */
907 /* True if pointers to distinct types T1 and T2 can be converted to
908 each other without an explicit cast. Only returns true for opaque
909 vector types. */
910 bool
911 vector_targets_convertible_p (const_tree t1, const_tree t2)
913 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
914 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
915 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
916 return true;
918 return false;
921 /* vector_types_convertible_p is used for vector value types.
922 It could in principle call vector_targets_convertible_p as a subroutine,
923 but then the check for vector type would be duplicated with its callers,
924 and also the purpose of vector_targets_convertible_p would become
925 muddled.
926 Where vector_types_convertible_p returns true, a conversion might still be
927 needed to make the types match.
928 In contrast, vector_targets_convertible_p is used for vector pointer
929 values, and vector_types_compatible_elements_p is used specifically
930 in the context for binary operators, as a check if use is possible without
931 conversion. */
932 /* True if vector types T1 and T2 can be converted to each other
933 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
934 can only be converted with -flax-vector-conversions yet that is not
935 in effect, emit a note telling the user about that option if such
936 a note has not previously been emitted. */
937 bool
938 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
940 static bool emitted_lax_note = false;
941 bool convertible_lax;
943 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
944 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
945 return true;
947 convertible_lax =
948 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
949 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
950 || known_eq (TYPE_VECTOR_SUBPARTS (t1),
951 TYPE_VECTOR_SUBPARTS (t2)))
952 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
953 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
955 if (!convertible_lax || flag_lax_vector_conversions)
956 return convertible_lax;
958 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
959 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
960 return true;
962 if (emit_lax_note && !emitted_lax_note)
964 emitted_lax_note = true;
965 inform (input_location, "use -flax-vector-conversions to permit "
966 "conversions between vectors with differing "
967 "element types or numbers of subparts");
970 return false;
973 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
974 and have vector types, V0 has the same type as V1, and the number of
975 elements of V0, V1, MASK is the same.
977 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
978 called with two arguments. In this case implementation passes the
979 first argument twice in order to share the same tree code. This fact
980 could enable the mask-values being twice the vector length. This is
981 an implementation accident and this semantics is not guaranteed to
982 the user. */
983 tree
984 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
985 bool complain)
987 tree ret;
988 bool wrap = true;
989 bool maybe_const = false;
990 bool two_arguments = false;
992 if (v1 == NULL_TREE)
994 two_arguments = true;
995 v1 = v0;
998 if (v0 == error_mark_node || v1 == error_mark_node
999 || mask == error_mark_node)
1000 return error_mark_node;
1002 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1004 if (complain)
1005 error_at (loc, "__builtin_shuffle last argument must "
1006 "be an integer vector");
1007 return error_mark_node;
1010 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1011 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1013 if (complain)
1014 error_at (loc, "__builtin_shuffle arguments must be vectors");
1015 return error_mark_node;
1018 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1020 if (complain)
1021 error_at (loc, "__builtin_shuffle argument vectors must be of "
1022 "the same type");
1023 return error_mark_node;
1026 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1027 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1028 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1029 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1031 if (complain)
1032 error_at (loc, "__builtin_shuffle number of elements of the "
1033 "argument vector(s) and the mask vector should "
1034 "be the same");
1035 return error_mark_node;
1038 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1039 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1041 if (complain)
1042 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1043 "must have the same size as inner type of the mask");
1044 return error_mark_node;
1047 if (!c_dialect_cxx ())
1049 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1050 v0 = c_fully_fold (v0, false, &maybe_const);
1051 wrap &= maybe_const;
1053 if (two_arguments)
1054 v1 = v0 = save_expr (v0);
1055 else
1057 v1 = c_fully_fold (v1, false, &maybe_const);
1058 wrap &= maybe_const;
1061 mask = c_fully_fold (mask, false, &maybe_const);
1062 wrap &= maybe_const;
1064 else if (two_arguments)
1065 v1 = v0 = save_expr (v0);
1067 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1069 if (!c_dialect_cxx () && !wrap)
1070 ret = c_wrap_maybe_const (ret, true);
1072 return ret;
1075 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1076 to integral type. */
1078 tree
1079 c_common_get_narrower (tree op, int *unsignedp_ptr)
1081 op = get_narrower (op, unsignedp_ptr);
1083 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1084 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1086 /* C++0x scoped enumerations don't implicitly convert to integral
1087 type; if we stripped an explicit conversion to a larger type we
1088 need to replace it so common_type will still work. */
1089 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1090 TYPE_UNSIGNED (TREE_TYPE (op)));
1091 op = fold_convert (type, op);
1093 return op;
1096 /* This is a helper function of build_binary_op.
1098 For certain operations if both args were extended from the same
1099 smaller type, do the arithmetic in that type and then extend.
1101 BITWISE indicates a bitwise operation.
1102 For them, this optimization is safe only if
1103 both args are zero-extended or both are sign-extended.
1104 Otherwise, we might change the result.
1105 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1106 but calculated in (unsigned short) it would be (unsigned short)-1.
1108 tree
1109 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1111 int unsigned0, unsigned1;
1112 tree arg0, arg1;
1113 int uns;
1114 tree type;
1116 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1117 excessive narrowing when we call get_narrower below. For
1118 example, suppose that OP0 is of unsigned int extended
1119 from signed char and that RESULT_TYPE is long long int.
1120 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1121 like
1123 (long long int) (unsigned int) signed_char
1125 which get_narrower would narrow down to
1127 (unsigned int) signed char
1129 If we do not cast OP0 first, get_narrower would return
1130 signed_char, which is inconsistent with the case of the
1131 explicit cast. */
1132 op0 = convert (result_type, op0);
1133 op1 = convert (result_type, op1);
1135 arg0 = c_common_get_narrower (op0, &unsigned0);
1136 arg1 = c_common_get_narrower (op1, &unsigned1);
1138 /* UNS is 1 if the operation to be done is an unsigned one. */
1139 uns = TYPE_UNSIGNED (result_type);
1141 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1142 but it *requires* conversion to FINAL_TYPE. */
1144 if ((TYPE_PRECISION (TREE_TYPE (op0))
1145 == TYPE_PRECISION (TREE_TYPE (arg0)))
1146 && TREE_TYPE (op0) != result_type)
1147 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1148 if ((TYPE_PRECISION (TREE_TYPE (op1))
1149 == TYPE_PRECISION (TREE_TYPE (arg1)))
1150 && TREE_TYPE (op1) != result_type)
1151 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1153 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1155 /* For bitwise operations, signedness of nominal type
1156 does not matter. Consider only how operands were extended. */
1157 if (bitwise)
1158 uns = unsigned0;
1160 /* Note that in all three cases below we refrain from optimizing
1161 an unsigned operation on sign-extended args.
1162 That would not be valid. */
1164 /* Both args variable: if both extended in same way
1165 from same width, do it in that width.
1166 Do it unsigned if args were zero-extended. */
1167 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1168 < TYPE_PRECISION (result_type))
1169 && (TYPE_PRECISION (TREE_TYPE (arg1))
1170 == TYPE_PRECISION (TREE_TYPE (arg0)))
1171 && unsigned0 == unsigned1
1172 && (unsigned0 || !uns))
1173 return c_common_signed_or_unsigned_type
1174 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1176 else if (TREE_CODE (arg0) == INTEGER_CST
1177 && (unsigned1 || !uns)
1178 && (TYPE_PRECISION (TREE_TYPE (arg1))
1179 < TYPE_PRECISION (result_type))
1180 && (type
1181 = c_common_signed_or_unsigned_type (unsigned1,
1182 TREE_TYPE (arg1)))
1183 && !POINTER_TYPE_P (type)
1184 && int_fits_type_p (arg0, type))
1185 return type;
1187 else if (TREE_CODE (arg1) == INTEGER_CST
1188 && (unsigned0 || !uns)
1189 && (TYPE_PRECISION (TREE_TYPE (arg0))
1190 < TYPE_PRECISION (result_type))
1191 && (type
1192 = c_common_signed_or_unsigned_type (unsigned0,
1193 TREE_TYPE (arg0)))
1194 && !POINTER_TYPE_P (type)
1195 && int_fits_type_p (arg1, type))
1196 return type;
1198 return result_type;
1201 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1202 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1204 static bool
1205 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1207 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1208 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1209 REAL_VALUE_TYPE real_low_bound =
1210 real_value_from_int_cst (0, type_low_bound);
1211 REAL_VALUE_TYPE real_high_bound =
1212 real_value_from_int_cst (0, type_high_bound);
1214 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1215 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1218 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1219 to the complex/real/integer type TYPE. Function returns non-zero when:
1220 * EXPR is a constant which cannot be exactly converted to TYPE.
1221 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1222 for EXPR type and TYPE being both integers or both real, or both
1223 complex.
1224 * EXPR is not a constant of complex type and TYPE is a real or
1225 an integer.
1226 * EXPR is not a constant of real type and TYPE is an integer.
1227 * EXPR is not a constant of integer type which cannot be
1228 exactly converted to real type.
1230 Function allows conversions between types of different signedness and
1231 can return SAFE_CONVERSION (zero) in that case. Function can produce
1232 signedness warnings if PRODUCE_WARNS is true.
1234 RESULT, when non-null is the result of the conversion. When constant
1235 it is included in the text of diagnostics.
1237 Function allows conversions from complex constants to non-complex types,
1238 provided that imaginary part is zero and real part can be safely converted
1239 to TYPE. */
1241 enum conversion_safety
1242 unsafe_conversion_p (location_t loc, tree type, tree expr, tree result,
1243 bool produce_warns)
1245 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1246 tree expr_type = TREE_TYPE (expr);
1248 bool cstresult = (result
1249 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1251 loc = expansion_point_location_if_in_system_header (loc);
1253 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1255 /* If type is complex, we are interested in compatibility with
1256 underlying type. */
1257 if (TREE_CODE (type) == COMPLEX_TYPE)
1258 type = TREE_TYPE (type);
1260 /* Warn for real constant that is not an exact integer converted
1261 to integer type. */
1262 if (TREE_CODE (expr_type) == REAL_TYPE
1263 && TREE_CODE (type) == INTEGER_TYPE)
1265 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1266 give_warning = UNSAFE_REAL;
1268 /* Warn for an integer constant that does not fit into integer type. */
1269 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1270 && TREE_CODE (type) == INTEGER_TYPE
1271 && !int_fits_type_p (expr, type))
1273 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1274 && tree_int_cst_sgn (expr) < 0)
1276 if (produce_warns)
1278 if (cstresult)
1279 warning_at (loc, OPT_Wsign_conversion,
1280 "unsigned conversion from %qT to %qT "
1281 "changes value from %qE to %qE",
1282 expr_type, type, expr, result);
1283 else
1284 warning_at (loc, OPT_Wsign_conversion,
1285 "unsigned conversion from %qT to %qT "
1286 "changes the value of %qE",
1287 expr_type, type, expr);
1290 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1292 if (cstresult)
1293 warning_at (loc, OPT_Wsign_conversion,
1294 "signed conversion from %qT to %qT changes "
1295 "value from %qE to %qE",
1296 expr_type, type, expr, result);
1297 else
1298 warning_at (loc, OPT_Wsign_conversion,
1299 "signed conversion from %qT to %qT changes "
1300 "the value of %qE",
1301 expr_type, type, expr);
1303 else
1304 give_warning = UNSAFE_OTHER;
1306 else if (TREE_CODE (type) == REAL_TYPE)
1308 /* Warn for an integer constant that does not fit into real type. */
1309 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1311 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1312 if (!exact_real_truncate (TYPE_MODE (type), &a))
1313 give_warning = UNSAFE_REAL;
1315 /* Warn for a real constant that does not fit into a smaller
1316 real type. */
1317 else if (TREE_CODE (expr_type) == REAL_TYPE
1318 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1320 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1321 if (!exact_real_truncate (TYPE_MODE (type), &a))
1322 give_warning = UNSAFE_REAL;
1327 else if (TREE_CODE (expr) == COMPLEX_CST)
1329 tree imag_part = TREE_IMAGPART (expr);
1330 /* Conversion from complex constant with zero imaginary part,
1331 perform check for conversion of real part. */
1332 if ((TREE_CODE (imag_part) == REAL_CST
1333 && real_zerop (imag_part))
1334 || (TREE_CODE (imag_part) == INTEGER_CST
1335 && integer_zerop (imag_part)))
1336 /* Note: in this branch we use recursive call to unsafe_conversion_p
1337 with different type of EXPR, but it is still safe, because when EXPR
1338 is a constant, it's type is not used in text of generated warnings
1339 (otherwise they could sound misleading). */
1340 return unsafe_conversion_p (loc, type, TREE_REALPART (expr), result,
1341 produce_warns);
1342 /* Conversion from complex constant with non-zero imaginary part. */
1343 else
1345 /* Conversion to complex type.
1346 Perform checks for both real and imaginary parts. */
1347 if (TREE_CODE (type) == COMPLEX_TYPE)
1349 /* Unfortunately, produce_warns must be false in two subsequent
1350 calls of unsafe_conversion_p, because otherwise we could
1351 produce strange "double" warnings, if both real and imaginary
1352 parts have conversion problems related to signedness.
1354 For example:
1355 int32_t _Complex a = 0x80000000 + 0x80000000i;
1357 Possible solution: add a separate function for checking
1358 constants and combine result of two calls appropriately. */
1359 enum conversion_safety re_safety =
1360 unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1361 result, false);
1362 enum conversion_safety im_safety =
1363 unsafe_conversion_p (loc, type, imag_part, result, false);
1365 /* Merge the results into appropriate single warning. */
1367 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1368 if (re_safety == im_safety)
1369 give_warning = re_safety;
1370 else if (!re_safety && im_safety)
1371 give_warning = im_safety;
1372 else if (re_safety && !im_safety)
1373 give_warning = re_safety;
1374 else
1375 give_warning = UNSAFE_OTHER;
1377 /* Warn about conversion from complex to real or integer type. */
1378 else
1379 give_warning = UNSAFE_IMAGINARY;
1383 /* Checks for remaining case: EXPR is not constant. */
1384 else
1386 /* Warn for real types converted to integer types. */
1387 if (TREE_CODE (expr_type) == REAL_TYPE
1388 && TREE_CODE (type) == INTEGER_TYPE)
1389 give_warning = UNSAFE_REAL;
1391 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1392 && TREE_CODE (type) == INTEGER_TYPE)
1394 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1395 expr = get_unwidened (expr, 0);
1396 expr_type = TREE_TYPE (expr);
1398 /* Don't warn for short y; short x = ((int)y & 0xff); */
1399 if (TREE_CODE (expr) == BIT_AND_EXPR
1400 || TREE_CODE (expr) == BIT_IOR_EXPR
1401 || TREE_CODE (expr) == BIT_XOR_EXPR)
1403 /* If both args were extended from a shortest type,
1404 use that type if that is safe. */
1405 expr_type = shorten_binary_op (expr_type,
1406 TREE_OPERAND (expr, 0),
1407 TREE_OPERAND (expr, 1),
1408 /* bitwise */1);
1410 if (TREE_CODE (expr) == BIT_AND_EXPR)
1412 tree op0 = TREE_OPERAND (expr, 0);
1413 tree op1 = TREE_OPERAND (expr, 1);
1414 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1415 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1417 /* If one of the operands is a non-negative constant
1418 that fits in the target type, then the type of the
1419 other operand does not matter. */
1420 if ((TREE_CODE (op0) == INTEGER_CST
1421 && int_fits_type_p (op0, c_common_signed_type (type))
1422 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1423 || (TREE_CODE (op1) == INTEGER_CST
1424 && int_fits_type_p (op1, c_common_signed_type (type))
1425 && int_fits_type_p (op1,
1426 c_common_unsigned_type (type))))
1427 return SAFE_CONVERSION;
1428 /* If constant is unsigned and fits in the target
1429 type, then the result will also fit. */
1430 else if ((TREE_CODE (op0) == INTEGER_CST
1431 && unsigned0
1432 && int_fits_type_p (op0, type))
1433 || (TREE_CODE (op1) == INTEGER_CST
1434 && unsigned1
1435 && int_fits_type_p (op1, type)))
1436 return SAFE_CONVERSION;
1439 /* Warn for integer types converted to smaller integer types. */
1440 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1441 give_warning = UNSAFE_OTHER;
1443 /* When they are the same width but different signedness,
1444 then the value may change. */
1445 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1446 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1447 /* Even when converted to a bigger type, if the type is
1448 unsigned but expr is signed, then negative values
1449 will be changed. */
1450 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1451 && produce_warns)
1452 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1453 "may change the sign of the result",
1454 type, expr_type);
1457 /* Warn for integer types converted to real types if and only if
1458 all the range of values of the integer type cannot be
1459 represented by the real type. */
1460 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1461 && TREE_CODE (type) == REAL_TYPE)
1463 /* Don't warn about char y = 0xff; float x = (int) y; */
1464 expr = get_unwidened (expr, 0);
1465 expr_type = TREE_TYPE (expr);
1467 if (!int_safely_convertible_to_real_p (expr_type, type))
1468 give_warning = UNSAFE_OTHER;
1471 /* Warn for real types converted to smaller real types. */
1472 else if (TREE_CODE (expr_type) == REAL_TYPE
1473 && TREE_CODE (type) == REAL_TYPE
1474 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1475 give_warning = UNSAFE_REAL;
1477 /* Check conversion between two complex types. */
1478 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1479 && TREE_CODE (type) == COMPLEX_TYPE)
1481 /* Extract underlying types (i.e., type of real and imaginary
1482 parts) of expr_type and type. */
1483 tree from_type = TREE_TYPE (expr_type);
1484 tree to_type = TREE_TYPE (type);
1486 /* Warn for real types converted to integer types. */
1487 if (TREE_CODE (from_type) == REAL_TYPE
1488 && TREE_CODE (to_type) == INTEGER_TYPE)
1489 give_warning = UNSAFE_REAL;
1491 /* Warn for real types converted to smaller real types. */
1492 else if (TREE_CODE (from_type) == REAL_TYPE
1493 && TREE_CODE (to_type) == REAL_TYPE
1494 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1495 give_warning = UNSAFE_REAL;
1497 /* Check conversion for complex integer types. Here implementation
1498 is simpler than for real-domain integers because it does not
1499 involve sophisticated cases, such as bitmasks, casts, etc. */
1500 else if (TREE_CODE (from_type) == INTEGER_TYPE
1501 && TREE_CODE (to_type) == INTEGER_TYPE)
1503 /* Warn for integer types converted to smaller integer types. */
1504 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1505 give_warning = UNSAFE_OTHER;
1507 /* Check for different signedness, see case for real-domain
1508 integers (above) for a more detailed comment. */
1509 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1510 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1511 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1512 && produce_warns)
1513 warning_at (loc, OPT_Wsign_conversion,
1514 "conversion to %qT from %qT "
1515 "may change the sign of the result",
1516 type, expr_type);
1518 else if (TREE_CODE (from_type) == INTEGER_TYPE
1519 && TREE_CODE (to_type) == REAL_TYPE
1520 && !int_safely_convertible_to_real_p (from_type, to_type))
1521 give_warning = UNSAFE_OTHER;
1524 /* Warn for complex types converted to real or integer types. */
1525 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1526 && TREE_CODE (type) != COMPLEX_TYPE)
1527 give_warning = UNSAFE_IMAGINARY;
1530 return give_warning;
1534 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1535 Invoke this function on every expression that is converted implicitly,
1536 i.e. because of language rules and not because of an explicit cast. */
1538 tree
1539 convert_and_check (location_t loc, tree type, tree expr)
1541 tree result;
1542 tree expr_for_warning;
1544 /* Convert from a value with possible excess precision rather than
1545 via the semantic type, but do not warn about values not fitting
1546 exactly in the semantic type. */
1547 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1549 tree orig_type = TREE_TYPE (expr);
1550 expr = TREE_OPERAND (expr, 0);
1551 expr_for_warning = convert (orig_type, expr);
1552 if (orig_type == type)
1553 return expr_for_warning;
1555 else
1556 expr_for_warning = expr;
1558 if (TREE_TYPE (expr) == type)
1559 return expr;
1561 result = convert (type, expr);
1563 if (c_inhibit_evaluation_warnings == 0
1564 && !TREE_OVERFLOW_P (expr)
1565 && result != error_mark_node)
1566 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1568 return result;
1571 /* A node in a list that describes references to variables (EXPR), which are
1572 either read accesses if WRITER is zero, or write accesses, in which case
1573 WRITER is the parent of EXPR. */
1574 struct tlist
1576 struct tlist *next;
1577 tree expr, writer;
1580 /* Used to implement a cache the results of a call to verify_tree. We only
1581 use this for SAVE_EXPRs. */
1582 struct tlist_cache
1584 struct tlist_cache *next;
1585 struct tlist *cache_before_sp;
1586 struct tlist *cache_after_sp;
1587 tree expr;
1590 /* Obstack to use when allocating tlist structures, and corresponding
1591 firstobj. */
1592 static struct obstack tlist_obstack;
1593 static char *tlist_firstobj = 0;
1595 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1596 warnings. */
1597 static struct tlist *warned_ids;
1598 /* SAVE_EXPRs need special treatment. We process them only once and then
1599 cache the results. */
1600 static struct tlist_cache *save_expr_cache;
1602 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1603 static void merge_tlist (struct tlist **, struct tlist *, int);
1604 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1605 static bool warning_candidate_p (tree);
1606 static bool candidate_equal_p (const_tree, const_tree);
1607 static void warn_for_collisions (struct tlist *);
1608 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1609 static struct tlist *new_tlist (struct tlist *, tree, tree);
1611 /* Create a new struct tlist and fill in its fields. */
1612 static struct tlist *
1613 new_tlist (struct tlist *next, tree t, tree writer)
1615 struct tlist *l;
1616 l = XOBNEW (&tlist_obstack, struct tlist);
1617 l->next = next;
1618 l->expr = t;
1619 l->writer = writer;
1620 return l;
1623 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1624 is nonnull, we ignore any node we find which has a writer equal to it. */
1626 static void
1627 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1629 while (add)
1631 struct tlist *next = add->next;
1632 if (!copy)
1633 add->next = *to;
1634 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1635 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1636 add = next;
1640 /* Merge the nodes of ADD into TO. This merging process is done so that for
1641 each variable that already exists in TO, no new node is added; however if
1642 there is a write access recorded in ADD, and an occurrence on TO is only
1643 a read access, then the occurrence in TO will be modified to record the
1644 write. */
1646 static void
1647 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1649 struct tlist **end = to;
1651 while (*end)
1652 end = &(*end)->next;
1654 while (add)
1656 int found = 0;
1657 struct tlist *tmp2;
1658 struct tlist *next = add->next;
1660 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1661 if (candidate_equal_p (tmp2->expr, add->expr))
1663 found = 1;
1664 if (!tmp2->writer)
1665 tmp2->writer = add->writer;
1667 if (!found)
1669 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1670 end = &(*end)->next;
1671 *end = 0;
1673 add = next;
1677 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1678 references in list LIST conflict with it, excluding reads if ONLY writers
1679 is nonzero. */
1681 static void
1682 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1683 int only_writes)
1685 struct tlist *tmp;
1687 /* Avoid duplicate warnings. */
1688 for (tmp = warned_ids; tmp; tmp = tmp->next)
1689 if (candidate_equal_p (tmp->expr, written))
1690 return;
1692 while (list)
1694 if (candidate_equal_p (list->expr, written)
1695 && !candidate_equal_p (list->writer, writer)
1696 && (!only_writes || list->writer))
1698 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1699 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1700 OPT_Wsequence_point, "operation on %qE may be undefined",
1701 list->expr);
1703 list = list->next;
1707 /* Given a list LIST of references to variables, find whether any of these
1708 can cause conflicts due to missing sequence points. */
1710 static void
1711 warn_for_collisions (struct tlist *list)
1713 struct tlist *tmp;
1715 for (tmp = list; tmp; tmp = tmp->next)
1717 if (tmp->writer)
1718 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1722 /* Return nonzero if X is a tree that can be verified by the sequence point
1723 warnings. */
1725 static bool
1726 warning_candidate_p (tree x)
1728 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1729 return false;
1731 if (TREE_CODE (x) == BLOCK)
1732 return false;
1734 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1735 (lvalue_p) crash on TRY/CATCH. */
1736 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1737 return false;
1739 if (!lvalue_p (x))
1740 return false;
1742 /* No point to track non-const calls, they will never satisfy
1743 operand_equal_p. */
1744 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1745 return false;
1747 if (TREE_CODE (x) == STRING_CST)
1748 return false;
1750 return true;
1753 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1754 static bool
1755 candidate_equal_p (const_tree x, const_tree y)
1757 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1760 /* Walk the tree X, and record accesses to variables. If X is written by the
1761 parent tree, WRITER is the parent.
1762 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1763 expression or its only operand forces a sequence point, then everything up
1764 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1765 in PNO_SP.
1766 Once we return, we will have emitted warnings if any subexpression before
1767 such a sequence point could be undefined. On a higher level, however, the
1768 sequence point may not be relevant, and we'll merge the two lists.
1770 Example: (b++, a) + b;
1771 The call that processes the COMPOUND_EXPR will store the increment of B
1772 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1773 processes the PLUS_EXPR will need to merge the two lists so that
1774 eventually, all accesses end up on the same list (and we'll warn about the
1775 unordered subexpressions b++ and b.
1777 A note on merging. If we modify the former example so that our expression
1778 becomes
1779 (b++, b) + a
1780 care must be taken not simply to add all three expressions into the final
1781 PNO_SP list. The function merge_tlist takes care of that by merging the
1782 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1783 way, so that no more than one access to B is recorded. */
1785 static void
1786 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1787 tree writer)
1789 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1790 enum tree_code code;
1791 enum tree_code_class cl;
1793 /* X may be NULL if it is the operand of an empty statement expression
1794 ({ }). */
1795 if (x == NULL)
1796 return;
1798 restart:
1799 code = TREE_CODE (x);
1800 cl = TREE_CODE_CLASS (code);
1802 if (warning_candidate_p (x))
1803 *pno_sp = new_tlist (*pno_sp, x, writer);
1805 switch (code)
1807 case CONSTRUCTOR:
1808 case SIZEOF_EXPR:
1809 return;
1811 case COMPOUND_EXPR:
1812 case TRUTH_ANDIF_EXPR:
1813 case TRUTH_ORIF_EXPR:
1814 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1815 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1816 warn_for_collisions (tmp_nosp);
1817 merge_tlist (pbefore_sp, tmp_before, 0);
1818 merge_tlist (pbefore_sp, tmp_nosp, 0);
1819 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1820 warn_for_collisions (tmp_list2);
1821 merge_tlist (pbefore_sp, tmp_list3, 0);
1822 merge_tlist (pno_sp, tmp_list2, 0);
1823 return;
1825 case COND_EXPR:
1826 tmp_before = tmp_list2 = 0;
1827 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1828 warn_for_collisions (tmp_list2);
1829 merge_tlist (pbefore_sp, tmp_before, 0);
1830 merge_tlist (pbefore_sp, tmp_list2, 0);
1832 tmp_list3 = tmp_nosp = 0;
1833 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1834 warn_for_collisions (tmp_nosp);
1835 merge_tlist (pbefore_sp, tmp_list3, 0);
1837 tmp_list3 = tmp_list2 = 0;
1838 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1839 warn_for_collisions (tmp_list2);
1840 merge_tlist (pbefore_sp, tmp_list3, 0);
1841 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1842 two first, to avoid warning for (a ? b++ : b++). */
1843 merge_tlist (&tmp_nosp, tmp_list2, 0);
1844 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1845 return;
1847 case PREDECREMENT_EXPR:
1848 case PREINCREMENT_EXPR:
1849 case POSTDECREMENT_EXPR:
1850 case POSTINCREMENT_EXPR:
1851 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1852 return;
1854 case MODIFY_EXPR:
1855 tmp_before = tmp_nosp = tmp_list3 = 0;
1856 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1857 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1858 /* Expressions inside the LHS are not ordered wrt. the sequence points
1859 in the RHS. Example:
1860 *a = (a++, 2)
1861 Despite the fact that the modification of "a" is in the before_sp
1862 list (tmp_before), it conflicts with the use of "a" in the LHS.
1863 We can handle this by adding the contents of tmp_list3
1864 to those of tmp_before, and redoing the collision warnings for that
1865 list. */
1866 add_tlist (&tmp_before, tmp_list3, x, 1);
1867 warn_for_collisions (tmp_before);
1868 /* Exclude the LHS itself here; we first have to merge it into the
1869 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1870 didn't exclude the LHS, we'd get it twice, once as a read and once
1871 as a write. */
1872 add_tlist (pno_sp, tmp_list3, x, 0);
1873 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1875 merge_tlist (pbefore_sp, tmp_before, 0);
1876 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1877 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1878 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1879 return;
1881 case CALL_EXPR:
1882 /* We need to warn about conflicts among arguments and conflicts between
1883 args and the function address. Side effects of the function address,
1884 however, are not ordered by the sequence point of the call. */
1886 call_expr_arg_iterator iter;
1887 tree arg;
1888 tmp_before = tmp_nosp = 0;
1889 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1890 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1892 tmp_list2 = tmp_list3 = 0;
1893 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1894 merge_tlist (&tmp_list3, tmp_list2, 0);
1895 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1897 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1898 warn_for_collisions (tmp_before);
1899 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1900 return;
1903 case TREE_LIST:
1904 /* Scan all the list, e.g. indices of multi dimensional array. */
1905 while (x)
1907 tmp_before = tmp_nosp = 0;
1908 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1909 merge_tlist (&tmp_nosp, tmp_before, 0);
1910 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1911 x = TREE_CHAIN (x);
1913 return;
1915 case SAVE_EXPR:
1917 struct tlist_cache *t;
1918 for (t = save_expr_cache; t; t = t->next)
1919 if (candidate_equal_p (t->expr, x))
1920 break;
1922 if (!t)
1924 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1925 t->next = save_expr_cache;
1926 t->expr = x;
1927 save_expr_cache = t;
1929 tmp_before = tmp_nosp = 0;
1930 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1931 warn_for_collisions (tmp_nosp);
1933 tmp_list3 = 0;
1934 merge_tlist (&tmp_list3, tmp_nosp, 0);
1935 t->cache_before_sp = tmp_before;
1936 t->cache_after_sp = tmp_list3;
1938 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1939 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1940 return;
1943 case ADDR_EXPR:
1944 x = TREE_OPERAND (x, 0);
1945 if (DECL_P (x))
1946 return;
1947 writer = 0;
1948 goto restart;
1950 default:
1951 /* For other expressions, simply recurse on their operands.
1952 Manual tail recursion for unary expressions.
1953 Other non-expressions need not be processed. */
1954 if (cl == tcc_unary)
1956 x = TREE_OPERAND (x, 0);
1957 writer = 0;
1958 goto restart;
1960 else if (IS_EXPR_CODE_CLASS (cl))
1962 int lp;
1963 int max = TREE_OPERAND_LENGTH (x);
1964 for (lp = 0; lp < max; lp++)
1966 tmp_before = tmp_nosp = 0;
1967 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1968 merge_tlist (&tmp_nosp, tmp_before, 0);
1969 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1972 return;
1976 /* Try to warn for undefined behavior in EXPR due to missing sequence
1977 points. */
1979 DEBUG_FUNCTION void
1980 verify_sequence_points (tree expr)
1982 struct tlist *before_sp = 0, *after_sp = 0;
1984 warned_ids = 0;
1985 save_expr_cache = 0;
1986 if (tlist_firstobj == 0)
1988 gcc_obstack_init (&tlist_obstack);
1989 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1992 verify_tree (expr, &before_sp, &after_sp, 0);
1993 warn_for_collisions (after_sp);
1994 obstack_free (&tlist_obstack, tlist_firstobj);
1997 /* Validate the expression after `case' and apply default promotions. */
1999 static tree
2000 check_case_value (location_t loc, tree value)
2002 if (value == NULL_TREE)
2003 return value;
2005 if (TREE_CODE (value) == INTEGER_CST)
2006 /* Promote char or short to int. */
2007 value = perform_integral_promotions (value);
2008 else if (value != error_mark_node)
2010 error_at (loc, "case label does not reduce to an integer constant");
2011 value = error_mark_node;
2014 constant_expression_warning (value);
2016 return value;
2019 /* See if the case values LOW and HIGH are in the range of the original
2020 type (i.e. before the default conversion to int) of the switch testing
2021 expression.
2022 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2023 the type before promoting it. CASE_LOW_P is a pointer to the lower
2024 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2025 if the case is not a case range.
2026 The caller has to make sure that we are not called with NULL for
2027 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
2028 was a case value that doesn't fit into the range of the ORIG_TYPE.
2029 Returns true if the case label is in range of ORIG_TYPE (saturated or
2030 untouched) or false if the label is out of range. */
2032 static bool
2033 check_case_bounds (location_t loc, tree type, tree orig_type,
2034 tree *case_low_p, tree *case_high_p,
2035 bool *outside_range_p)
2037 tree min_value, max_value;
2038 tree case_low = *case_low_p;
2039 tree case_high = case_high_p ? *case_high_p : case_low;
2041 /* If there was a problem with the original type, do nothing. */
2042 if (orig_type == error_mark_node)
2043 return true;
2045 min_value = TYPE_MIN_VALUE (orig_type);
2046 max_value = TYPE_MAX_VALUE (orig_type);
2048 /* We'll really need integer constants here. */
2049 case_low = fold (case_low);
2050 case_high = fold (case_high);
2052 /* Case label is less than minimum for type. */
2053 if (tree_int_cst_compare (case_low, min_value) < 0
2054 && tree_int_cst_compare (case_high, min_value) < 0)
2056 warning_at (loc, 0, "case label value is less than minimum value "
2057 "for type");
2058 *outside_range_p = true;
2059 return false;
2062 /* Case value is greater than maximum for type. */
2063 if (tree_int_cst_compare (case_low, max_value) > 0
2064 && tree_int_cst_compare (case_high, max_value) > 0)
2066 warning_at (loc, 0, "case label value exceeds maximum value for type");
2067 *outside_range_p = true;
2068 return false;
2071 /* Saturate lower case label value to minimum. */
2072 if (tree_int_cst_compare (case_high, min_value) >= 0
2073 && tree_int_cst_compare (case_low, min_value) < 0)
2075 warning_at (loc, 0, "lower value in case label range"
2076 " less than minimum value for type");
2077 *outside_range_p = true;
2078 case_low = min_value;
2081 /* Saturate upper case label value to maximum. */
2082 if (tree_int_cst_compare (case_low, max_value) <= 0
2083 && tree_int_cst_compare (case_high, max_value) > 0)
2085 warning_at (loc, 0, "upper value in case label range"
2086 " exceeds maximum value for type");
2087 *outside_range_p = true;
2088 case_high = max_value;
2091 if (*case_low_p != case_low)
2092 *case_low_p = convert (type, case_low);
2093 if (case_high_p && *case_high_p != case_high)
2094 *case_high_p = convert (type, case_high);
2096 return true;
2099 /* Return an integer type with BITS bits of precision,
2100 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2102 tree
2103 c_common_type_for_size (unsigned int bits, int unsignedp)
2105 int i;
2107 if (bits == TYPE_PRECISION (integer_type_node))
2108 return unsignedp ? unsigned_type_node : integer_type_node;
2110 if (bits == TYPE_PRECISION (signed_char_type_node))
2111 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2113 if (bits == TYPE_PRECISION (short_integer_type_node))
2114 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2116 if (bits == TYPE_PRECISION (long_integer_type_node))
2117 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2119 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2120 return (unsignedp ? long_long_unsigned_type_node
2121 : long_long_integer_type_node);
2123 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2124 if (int_n_enabled_p[i]
2125 && bits == int_n_data[i].bitsize)
2126 return (unsignedp ? int_n_trees[i].unsigned_type
2127 : int_n_trees[i].signed_type);
2129 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2130 return (unsignedp ? widest_unsigned_literal_type_node
2131 : widest_integer_literal_type_node);
2133 if (bits <= TYPE_PRECISION (intQI_type_node))
2134 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2136 if (bits <= TYPE_PRECISION (intHI_type_node))
2137 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2139 if (bits <= TYPE_PRECISION (intSI_type_node))
2140 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2142 if (bits <= TYPE_PRECISION (intDI_type_node))
2143 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2145 return NULL_TREE;
2148 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2149 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2150 and saturating if SATP is nonzero, otherwise not saturating. */
2152 tree
2153 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2154 int unsignedp, int satp)
2156 enum mode_class mclass;
2157 if (ibit == 0)
2158 mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2159 else
2160 mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2162 opt_scalar_mode opt_mode;
2163 scalar_mode mode;
2164 FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2166 mode = opt_mode.require ();
2167 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2168 break;
2171 if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2173 sorry ("GCC cannot support operators with integer types and "
2174 "fixed-point types that have too many integral and "
2175 "fractional bits together");
2176 return NULL_TREE;
2179 return c_common_type_for_mode (mode, satp);
2182 /* Used for communication between c_common_type_for_mode and
2183 c_register_builtin_type. */
2184 tree registered_builtin_types;
2186 /* Return a data type that has machine mode MODE.
2187 If the mode is an integer,
2188 then UNSIGNEDP selects between signed and unsigned types.
2189 If the mode is a fixed-point mode,
2190 then UNSIGNEDP selects between saturating and nonsaturating types. */
2192 tree
2193 c_common_type_for_mode (machine_mode mode, int unsignedp)
2195 tree t;
2196 int i;
2198 if (mode == TYPE_MODE (integer_type_node))
2199 return unsignedp ? unsigned_type_node : integer_type_node;
2201 if (mode == TYPE_MODE (signed_char_type_node))
2202 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2204 if (mode == TYPE_MODE (short_integer_type_node))
2205 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2207 if (mode == TYPE_MODE (long_integer_type_node))
2208 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2210 if (mode == TYPE_MODE (long_long_integer_type_node))
2211 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2213 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2214 if (int_n_enabled_p[i]
2215 && mode == int_n_data[i].m)
2216 return (unsignedp ? int_n_trees[i].unsigned_type
2217 : int_n_trees[i].signed_type);
2219 if (mode == QImode)
2220 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2222 if (mode == HImode)
2223 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2225 if (mode == SImode)
2226 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2228 if (mode == DImode)
2229 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2231 #if HOST_BITS_PER_WIDE_INT >= 64
2232 if (mode == TYPE_MODE (intTI_type_node))
2233 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2234 #endif
2236 if (mode == TYPE_MODE (float_type_node))
2237 return float_type_node;
2239 if (mode == TYPE_MODE (double_type_node))
2240 return double_type_node;
2242 if (mode == TYPE_MODE (long_double_type_node))
2243 return long_double_type_node;
2245 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2246 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2247 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2248 return FLOATN_NX_TYPE_NODE (i);
2250 if (mode == TYPE_MODE (void_type_node))
2251 return void_type_node;
2253 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2254 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2256 unsigned int precision
2257 = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2258 return (unsignedp
2259 ? make_unsigned_type (precision)
2260 : make_signed_type (precision));
2263 if (COMPLEX_MODE_P (mode))
2265 machine_mode inner_mode;
2266 tree inner_type;
2268 if (mode == TYPE_MODE (complex_float_type_node))
2269 return complex_float_type_node;
2270 if (mode == TYPE_MODE (complex_double_type_node))
2271 return complex_double_type_node;
2272 if (mode == TYPE_MODE (complex_long_double_type_node))
2273 return complex_long_double_type_node;
2275 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2276 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2277 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2278 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2280 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2281 return complex_integer_type_node;
2283 inner_mode = GET_MODE_INNER (mode);
2284 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2285 if (inner_type != NULL_TREE)
2286 return build_complex_type (inner_type);
2288 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2289 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2291 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
2292 GET_MODE_NUNITS (mode));
2293 tree bool_type = build_nonstandard_boolean_type (elem_bits);
2294 return build_vector_type_for_mode (bool_type, mode);
2296 else if (VECTOR_MODE_P (mode)
2297 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2299 machine_mode inner_mode = GET_MODE_INNER (mode);
2300 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2301 if (inner_type != NULL_TREE)
2302 return build_vector_type_for_mode (inner_type, mode);
2305 if (mode == TYPE_MODE (dfloat32_type_node))
2306 return dfloat32_type_node;
2307 if (mode == TYPE_MODE (dfloat64_type_node))
2308 return dfloat64_type_node;
2309 if (mode == TYPE_MODE (dfloat128_type_node))
2310 return dfloat128_type_node;
2312 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2314 if (mode == TYPE_MODE (short_fract_type_node))
2315 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2316 if (mode == TYPE_MODE (fract_type_node))
2317 return unsignedp ? sat_fract_type_node : fract_type_node;
2318 if (mode == TYPE_MODE (long_fract_type_node))
2319 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2320 if (mode == TYPE_MODE (long_long_fract_type_node))
2321 return unsignedp ? sat_long_long_fract_type_node
2322 : long_long_fract_type_node;
2324 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2325 return unsignedp ? sat_unsigned_short_fract_type_node
2326 : unsigned_short_fract_type_node;
2327 if (mode == TYPE_MODE (unsigned_fract_type_node))
2328 return unsignedp ? sat_unsigned_fract_type_node
2329 : unsigned_fract_type_node;
2330 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2331 return unsignedp ? sat_unsigned_long_fract_type_node
2332 : unsigned_long_fract_type_node;
2333 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2334 return unsignedp ? sat_unsigned_long_long_fract_type_node
2335 : unsigned_long_long_fract_type_node;
2337 if (mode == TYPE_MODE (short_accum_type_node))
2338 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2339 if (mode == TYPE_MODE (accum_type_node))
2340 return unsignedp ? sat_accum_type_node : accum_type_node;
2341 if (mode == TYPE_MODE (long_accum_type_node))
2342 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2343 if (mode == TYPE_MODE (long_long_accum_type_node))
2344 return unsignedp ? sat_long_long_accum_type_node
2345 : long_long_accum_type_node;
2347 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2348 return unsignedp ? sat_unsigned_short_accum_type_node
2349 : unsigned_short_accum_type_node;
2350 if (mode == TYPE_MODE (unsigned_accum_type_node))
2351 return unsignedp ? sat_unsigned_accum_type_node
2352 : unsigned_accum_type_node;
2353 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2354 return unsignedp ? sat_unsigned_long_accum_type_node
2355 : unsigned_long_accum_type_node;
2356 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2357 return unsignedp ? sat_unsigned_long_long_accum_type_node
2358 : unsigned_long_long_accum_type_node;
2360 if (mode == QQmode)
2361 return unsignedp ? sat_qq_type_node : qq_type_node;
2362 if (mode == HQmode)
2363 return unsignedp ? sat_hq_type_node : hq_type_node;
2364 if (mode == SQmode)
2365 return unsignedp ? sat_sq_type_node : sq_type_node;
2366 if (mode == DQmode)
2367 return unsignedp ? sat_dq_type_node : dq_type_node;
2368 if (mode == TQmode)
2369 return unsignedp ? sat_tq_type_node : tq_type_node;
2371 if (mode == UQQmode)
2372 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2373 if (mode == UHQmode)
2374 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2375 if (mode == USQmode)
2376 return unsignedp ? sat_usq_type_node : usq_type_node;
2377 if (mode == UDQmode)
2378 return unsignedp ? sat_udq_type_node : udq_type_node;
2379 if (mode == UTQmode)
2380 return unsignedp ? sat_utq_type_node : utq_type_node;
2382 if (mode == HAmode)
2383 return unsignedp ? sat_ha_type_node : ha_type_node;
2384 if (mode == SAmode)
2385 return unsignedp ? sat_sa_type_node : sa_type_node;
2386 if (mode == DAmode)
2387 return unsignedp ? sat_da_type_node : da_type_node;
2388 if (mode == TAmode)
2389 return unsignedp ? sat_ta_type_node : ta_type_node;
2391 if (mode == UHAmode)
2392 return unsignedp ? sat_uha_type_node : uha_type_node;
2393 if (mode == USAmode)
2394 return unsignedp ? sat_usa_type_node : usa_type_node;
2395 if (mode == UDAmode)
2396 return unsignedp ? sat_uda_type_node : uda_type_node;
2397 if (mode == UTAmode)
2398 return unsignedp ? sat_uta_type_node : uta_type_node;
2401 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2402 if (TYPE_MODE (TREE_VALUE (t)) == mode
2403 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2404 return TREE_VALUE (t);
2406 return NULL_TREE;
2409 tree
2410 c_common_unsigned_type (tree type)
2412 return c_common_signed_or_unsigned_type (1, type);
2415 /* Return a signed type the same as TYPE in other respects. */
2417 tree
2418 c_common_signed_type (tree type)
2420 return c_common_signed_or_unsigned_type (0, type);
2423 /* Return a type the same as TYPE except unsigned or
2424 signed according to UNSIGNEDP. */
2426 tree
2427 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2429 tree type1;
2430 int i;
2432 /* This block of code emulates the behavior of the old
2433 c_common_unsigned_type. In particular, it returns
2434 long_unsigned_type_node if passed a long, even when a int would
2435 have the same size. This is necessary for warnings to work
2436 correctly in archs where sizeof(int) == sizeof(long) */
2438 type1 = TYPE_MAIN_VARIANT (type);
2439 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2440 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2441 if (type1 == integer_type_node || type1 == unsigned_type_node)
2442 return unsignedp ? unsigned_type_node : integer_type_node;
2443 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2444 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2445 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2446 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2447 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2448 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2450 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2451 if (int_n_enabled_p[i]
2452 && (type1 == int_n_trees[i].unsigned_type
2453 || type1 == int_n_trees[i].signed_type))
2454 return (unsignedp ? int_n_trees[i].unsigned_type
2455 : int_n_trees[i].signed_type);
2457 #if HOST_BITS_PER_WIDE_INT >= 64
2458 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2459 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2460 #endif
2461 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2462 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2463 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2464 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2465 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2466 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2467 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2468 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2470 #define C_COMMON_FIXED_TYPES(NAME) \
2471 if (type1 == short_ ## NAME ## _type_node \
2472 || type1 == unsigned_short_ ## NAME ## _type_node) \
2473 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2474 : short_ ## NAME ## _type_node; \
2475 if (type1 == NAME ## _type_node \
2476 || type1 == unsigned_ ## NAME ## _type_node) \
2477 return unsignedp ? unsigned_ ## NAME ## _type_node \
2478 : NAME ## _type_node; \
2479 if (type1 == long_ ## NAME ## _type_node \
2480 || type1 == unsigned_long_ ## NAME ## _type_node) \
2481 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2482 : long_ ## NAME ## _type_node; \
2483 if (type1 == long_long_ ## NAME ## _type_node \
2484 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2485 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2486 : long_long_ ## NAME ## _type_node;
2488 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2489 if (type1 == NAME ## _type_node \
2490 || type1 == u ## NAME ## _type_node) \
2491 return unsignedp ? u ## NAME ## _type_node \
2492 : NAME ## _type_node;
2494 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2495 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2496 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2497 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2498 : sat_ ## short_ ## NAME ## _type_node; \
2499 if (type1 == sat_ ## NAME ## _type_node \
2500 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2501 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2502 : sat_ ## NAME ## _type_node; \
2503 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2504 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2505 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2506 : sat_ ## long_ ## NAME ## _type_node; \
2507 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2508 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2509 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2510 : sat_ ## long_long_ ## NAME ## _type_node;
2512 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2513 if (type1 == sat_ ## NAME ## _type_node \
2514 || type1 == sat_ ## u ## NAME ## _type_node) \
2515 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2516 : sat_ ## NAME ## _type_node;
2518 C_COMMON_FIXED_TYPES (fract);
2519 C_COMMON_FIXED_TYPES_SAT (fract);
2520 C_COMMON_FIXED_TYPES (accum);
2521 C_COMMON_FIXED_TYPES_SAT (accum);
2523 C_COMMON_FIXED_MODE_TYPES (qq);
2524 C_COMMON_FIXED_MODE_TYPES (hq);
2525 C_COMMON_FIXED_MODE_TYPES (sq);
2526 C_COMMON_FIXED_MODE_TYPES (dq);
2527 C_COMMON_FIXED_MODE_TYPES (tq);
2528 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2529 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2530 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2531 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2532 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2533 C_COMMON_FIXED_MODE_TYPES (ha);
2534 C_COMMON_FIXED_MODE_TYPES (sa);
2535 C_COMMON_FIXED_MODE_TYPES (da);
2536 C_COMMON_FIXED_MODE_TYPES (ta);
2537 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2538 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2539 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2540 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2542 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2543 the precision; they have precision set to match their range, but
2544 may use a wider mode to match an ABI. If we change modes, we may
2545 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2546 the precision as well, so as to yield correct results for
2547 bit-field types. C++ does not have these separate bit-field
2548 types, and producing a signed or unsigned variant of an
2549 ENUMERAL_TYPE may cause other problems as well. */
2551 if (!INTEGRAL_TYPE_P (type)
2552 || TYPE_UNSIGNED (type) == unsignedp)
2553 return type;
2555 #define TYPE_OK(node) \
2556 (TYPE_MODE (type) == TYPE_MODE (node) \
2557 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2558 if (TYPE_OK (signed_char_type_node))
2559 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2560 if (TYPE_OK (integer_type_node))
2561 return unsignedp ? unsigned_type_node : integer_type_node;
2562 if (TYPE_OK (short_integer_type_node))
2563 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2564 if (TYPE_OK (long_integer_type_node))
2565 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2566 if (TYPE_OK (long_long_integer_type_node))
2567 return (unsignedp ? long_long_unsigned_type_node
2568 : long_long_integer_type_node);
2570 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2571 if (int_n_enabled_p[i]
2572 && TYPE_MODE (type) == int_n_data[i].m
2573 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2574 return (unsignedp ? int_n_trees[i].unsigned_type
2575 : int_n_trees[i].signed_type);
2577 #if HOST_BITS_PER_WIDE_INT >= 64
2578 if (TYPE_OK (intTI_type_node))
2579 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2580 #endif
2581 if (TYPE_OK (intDI_type_node))
2582 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2583 if (TYPE_OK (intSI_type_node))
2584 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2585 if (TYPE_OK (intHI_type_node))
2586 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2587 if (TYPE_OK (intQI_type_node))
2588 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2589 #undef TYPE_OK
2591 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2594 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2596 tree
2597 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2599 int i;
2601 /* Extended integer types of the same width as a standard type have
2602 lesser rank, so those of the same width as int promote to int or
2603 unsigned int and are valid for printf formats expecting int or
2604 unsigned int. To avoid such special cases, avoid creating
2605 extended integer types for bit-fields if a standard integer type
2606 is available. */
2607 if (width == TYPE_PRECISION (integer_type_node))
2608 return unsignedp ? unsigned_type_node : integer_type_node;
2609 if (width == TYPE_PRECISION (signed_char_type_node))
2610 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2611 if (width == TYPE_PRECISION (short_integer_type_node))
2612 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2613 if (width == TYPE_PRECISION (long_integer_type_node))
2614 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2615 if (width == TYPE_PRECISION (long_long_integer_type_node))
2616 return (unsignedp ? long_long_unsigned_type_node
2617 : long_long_integer_type_node);
2618 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2619 if (int_n_enabled_p[i]
2620 && width == int_n_data[i].bitsize)
2621 return (unsignedp ? int_n_trees[i].unsigned_type
2622 : int_n_trees[i].signed_type);
2623 return build_nonstandard_integer_type (width, unsignedp);
2626 /* The C version of the register_builtin_type langhook. */
2628 void
2629 c_register_builtin_type (tree type, const char* name)
2631 tree decl;
2633 decl = build_decl (UNKNOWN_LOCATION,
2634 TYPE_DECL, get_identifier (name), type);
2635 DECL_ARTIFICIAL (decl) = 1;
2636 if (!TYPE_NAME (type))
2637 TYPE_NAME (type) = decl;
2638 lang_hooks.decls.pushdecl (decl);
2640 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2643 /* Print an error message for invalid operands to arith operation
2644 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2645 RICHLOC is a rich location for the message, containing either
2646 three separate locations for each of the operator and operands
2648 lhs op rhs
2649 ~~~ ^~ ~~~
2651 (C FE), or one location ranging over all over them
2653 lhs op rhs
2654 ~~~~^~~~~~
2656 (C++ FE). */
2658 void
2659 binary_op_error (rich_location *richloc, enum tree_code code,
2660 tree type0, tree type1)
2662 const char *opname;
2664 switch (code)
2666 case PLUS_EXPR:
2667 opname = "+"; break;
2668 case MINUS_EXPR:
2669 opname = "-"; break;
2670 case MULT_EXPR:
2671 opname = "*"; break;
2672 case MAX_EXPR:
2673 opname = "max"; break;
2674 case MIN_EXPR:
2675 opname = "min"; break;
2676 case EQ_EXPR:
2677 opname = "=="; break;
2678 case NE_EXPR:
2679 opname = "!="; break;
2680 case LE_EXPR:
2681 opname = "<="; break;
2682 case GE_EXPR:
2683 opname = ">="; break;
2684 case LT_EXPR:
2685 opname = "<"; break;
2686 case GT_EXPR:
2687 opname = ">"; break;
2688 case LSHIFT_EXPR:
2689 opname = "<<"; break;
2690 case RSHIFT_EXPR:
2691 opname = ">>"; break;
2692 case TRUNC_MOD_EXPR:
2693 case FLOOR_MOD_EXPR:
2694 opname = "%"; break;
2695 case TRUNC_DIV_EXPR:
2696 case FLOOR_DIV_EXPR:
2697 opname = "/"; break;
2698 case BIT_AND_EXPR:
2699 opname = "&"; break;
2700 case BIT_IOR_EXPR:
2701 opname = "|"; break;
2702 case TRUTH_ANDIF_EXPR:
2703 opname = "&&"; break;
2704 case TRUTH_ORIF_EXPR:
2705 opname = "||"; break;
2706 case BIT_XOR_EXPR:
2707 opname = "^"; break;
2708 default:
2709 gcc_unreachable ();
2711 error_at (richloc,
2712 "invalid operands to binary %s (have %qT and %qT)",
2713 opname, type0, type1);
2716 /* Given an expression as a tree, return its original type. Do this
2717 by stripping any conversion that preserves the sign and precision. */
2718 static tree
2719 expr_original_type (tree expr)
2721 STRIP_SIGN_NOPS (expr);
2722 return TREE_TYPE (expr);
2725 /* Subroutine of build_binary_op, used for comparison operations.
2726 See if the operands have both been converted from subword integer types
2727 and, if so, perhaps change them both back to their original type.
2728 This function is also responsible for converting the two operands
2729 to the proper common type for comparison.
2731 The arguments of this function are all pointers to local variables
2732 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2733 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2735 LOC is the location of the comparison.
2737 If this function returns non-NULL_TREE, it means that the comparison has
2738 a constant value. What this function returns is an expression for
2739 that value. */
2741 tree
2742 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2743 tree *restype_ptr, enum tree_code *rescode_ptr)
2745 tree type;
2746 tree op0 = *op0_ptr;
2747 tree op1 = *op1_ptr;
2748 int unsignedp0, unsignedp1;
2749 int real1, real2;
2750 tree primop0, primop1;
2751 enum tree_code code = *rescode_ptr;
2753 /* Throw away any conversions to wider types
2754 already present in the operands. */
2756 primop0 = c_common_get_narrower (op0, &unsignedp0);
2757 primop1 = c_common_get_narrower (op1, &unsignedp1);
2759 /* If primopN is first sign-extended from primopN's precision to opN's
2760 precision, then zero-extended from opN's precision to
2761 *restype_ptr precision, shortenings might be invalid. */
2762 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2763 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2764 && !unsignedp0
2765 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2766 primop0 = op0;
2767 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2768 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2769 && !unsignedp1
2770 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2771 primop1 = op1;
2773 /* Handle the case that OP0 does not *contain* a conversion
2774 but it *requires* conversion to FINAL_TYPE. */
2776 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2777 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2778 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2779 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2781 /* If one of the operands must be floated, we cannot optimize. */
2782 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2783 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2785 /* If first arg is constant, swap the args (changing operation
2786 so value is preserved), for canonicalization. Don't do this if
2787 the second arg is 0. */
2789 if (TREE_CONSTANT (primop0)
2790 && !integer_zerop (primop1) && !real_zerop (primop1)
2791 && !fixed_zerop (primop1))
2793 std::swap (primop0, primop1);
2794 std::swap (op0, op1);
2795 *op0_ptr = op0;
2796 *op1_ptr = op1;
2797 std::swap (unsignedp0, unsignedp1);
2798 std::swap (real1, real2);
2800 switch (code)
2802 case LT_EXPR:
2803 code = GT_EXPR;
2804 break;
2805 case GT_EXPR:
2806 code = LT_EXPR;
2807 break;
2808 case LE_EXPR:
2809 code = GE_EXPR;
2810 break;
2811 case GE_EXPR:
2812 code = LE_EXPR;
2813 break;
2814 default:
2815 break;
2817 *rescode_ptr = code;
2820 /* If comparing an integer against a constant more bits wide,
2821 maybe we can deduce a value of 1 or 0 independent of the data.
2822 Or else truncate the constant now
2823 rather than extend the variable at run time.
2825 This is only interesting if the constant is the wider arg.
2826 Also, it is not safe if the constant is unsigned and the
2827 variable arg is signed, since in this case the variable
2828 would be sign-extended and then regarded as unsigned.
2829 Our technique fails in this case because the lowest/highest
2830 possible unsigned results don't follow naturally from the
2831 lowest/highest possible values of the variable operand.
2832 For just EQ_EXPR and NE_EXPR there is another technique that
2833 could be used: see if the constant can be faithfully represented
2834 in the other operand's type, by truncating it and reextending it
2835 and see if that preserves the constant's value. */
2837 if (!real1 && !real2
2838 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2839 && TREE_CODE (primop1) == INTEGER_CST
2840 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2842 int min_gt, max_gt, min_lt, max_lt;
2843 tree maxval, minval;
2844 /* 1 if comparison is nominally unsigned. */
2845 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2846 tree val;
2848 type = c_common_signed_or_unsigned_type (unsignedp0,
2849 TREE_TYPE (primop0));
2851 maxval = TYPE_MAX_VALUE (type);
2852 minval = TYPE_MIN_VALUE (type);
2854 if (unsignedp && !unsignedp0)
2855 *restype_ptr = c_common_signed_type (*restype_ptr);
2857 if (TREE_TYPE (primop1) != *restype_ptr)
2859 /* Convert primop1 to target type, but do not introduce
2860 additional overflow. We know primop1 is an int_cst. */
2861 primop1 = force_fit_type (*restype_ptr,
2862 wi::to_wide
2863 (primop1,
2864 TYPE_PRECISION (*restype_ptr)),
2865 0, TREE_OVERFLOW (primop1));
2867 if (type != *restype_ptr)
2869 minval = convert (*restype_ptr, minval);
2870 maxval = convert (*restype_ptr, maxval);
2873 min_gt = tree_int_cst_lt (primop1, minval);
2874 max_gt = tree_int_cst_lt (primop1, maxval);
2875 min_lt = tree_int_cst_lt (minval, primop1);
2876 max_lt = tree_int_cst_lt (maxval, primop1);
2878 val = 0;
2879 /* This used to be a switch, but Genix compiler can't handle that. */
2880 if (code == NE_EXPR)
2882 if (max_lt || min_gt)
2883 val = truthvalue_true_node;
2885 else if (code == EQ_EXPR)
2887 if (max_lt || min_gt)
2888 val = truthvalue_false_node;
2890 else if (code == LT_EXPR)
2892 if (max_lt)
2893 val = truthvalue_true_node;
2894 if (!min_lt)
2895 val = truthvalue_false_node;
2897 else if (code == GT_EXPR)
2899 if (min_gt)
2900 val = truthvalue_true_node;
2901 if (!max_gt)
2902 val = truthvalue_false_node;
2904 else if (code == LE_EXPR)
2906 if (!max_gt)
2907 val = truthvalue_true_node;
2908 if (min_gt)
2909 val = truthvalue_false_node;
2911 else if (code == GE_EXPR)
2913 if (!min_lt)
2914 val = truthvalue_true_node;
2915 if (max_lt)
2916 val = truthvalue_false_node;
2919 /* If primop0 was sign-extended and unsigned comparison specd,
2920 we did a signed comparison above using the signed type bounds.
2921 But the comparison we output must be unsigned.
2923 Also, for inequalities, VAL is no good; but if the signed
2924 comparison had *any* fixed result, it follows that the
2925 unsigned comparison just tests the sign in reverse
2926 (positive values are LE, negative ones GE).
2927 So we can generate an unsigned comparison
2928 against an extreme value of the signed type. */
2930 if (unsignedp && !unsignedp0)
2932 if (val != 0)
2933 switch (code)
2935 case LT_EXPR:
2936 case GE_EXPR:
2937 primop1 = TYPE_MIN_VALUE (type);
2938 val = 0;
2939 break;
2941 case LE_EXPR:
2942 case GT_EXPR:
2943 primop1 = TYPE_MAX_VALUE (type);
2944 val = 0;
2945 break;
2947 default:
2948 break;
2950 type = c_common_unsigned_type (type);
2953 if (TREE_CODE (primop0) != INTEGER_CST
2954 /* Don't warn if it's from a (non-system) macro. */
2955 && !(from_macro_expansion_at
2956 (expansion_point_location_if_in_system_header
2957 (EXPR_LOCATION (primop0)))))
2959 if (val == truthvalue_false_node)
2960 warning_at (loc, OPT_Wtype_limits,
2961 "comparison is always false due to limited range of data type");
2962 if (val == truthvalue_true_node)
2963 warning_at (loc, OPT_Wtype_limits,
2964 "comparison is always true due to limited range of data type");
2967 if (val != 0)
2969 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2970 if (TREE_SIDE_EFFECTS (primop0))
2971 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2972 return val;
2975 /* Value is not predetermined, but do the comparison
2976 in the type of the operand that is not constant.
2977 TYPE is already properly set. */
2980 /* If either arg is decimal float and the other is float, find the
2981 proper common type to use for comparison. */
2982 else if (real1 && real2
2983 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2984 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2985 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2987 /* If either arg is decimal float and the other is float, fail. */
2988 else if (real1 && real2
2989 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2990 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2991 return NULL_TREE;
2993 else if (real1 && real2
2994 && (TYPE_PRECISION (TREE_TYPE (primop0))
2995 == TYPE_PRECISION (TREE_TYPE (primop1))))
2996 type = TREE_TYPE (primop0);
2998 /* If args' natural types are both narrower than nominal type
2999 and both extend in the same manner, compare them
3000 in the type of the wider arg.
3001 Otherwise must actually extend both to the nominal
3002 common type lest different ways of extending
3003 alter the result.
3004 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3006 else if (unsignedp0 == unsignedp1 && real1 == real2
3007 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3008 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3010 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3011 type = c_common_signed_or_unsigned_type (unsignedp0
3012 || TYPE_UNSIGNED (*restype_ptr),
3013 type);
3014 /* Make sure shorter operand is extended the right way
3015 to match the longer operand. */
3016 primop0
3017 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3018 TREE_TYPE (primop0)),
3019 primop0);
3020 primop1
3021 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3022 TREE_TYPE (primop1)),
3023 primop1);
3025 else
3027 /* Here we must do the comparison on the nominal type
3028 using the args exactly as we received them. */
3029 type = *restype_ptr;
3030 primop0 = op0;
3031 primop1 = op1;
3033 if (!real1 && !real2 && integer_zerop (primop1)
3034 && TYPE_UNSIGNED (*restype_ptr))
3036 tree value = NULL_TREE;
3037 /* All unsigned values are >= 0, so we warn. However,
3038 if OP0 is a constant that is >= 0, the signedness of
3039 the comparison isn't an issue, so suppress the
3040 warning. */
3041 bool warn =
3042 warn_type_limits && !in_system_header_at (loc)
3043 && !(TREE_CODE (primop0) == INTEGER_CST
3044 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3045 primop0)))
3046 /* Do not warn for enumeration types. */
3047 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3049 switch (code)
3051 case GE_EXPR:
3052 if (warn)
3053 warning_at (loc, OPT_Wtype_limits,
3054 "comparison of unsigned expression >= 0 is always true");
3055 value = truthvalue_true_node;
3056 break;
3058 case LT_EXPR:
3059 if (warn)
3060 warning_at (loc, OPT_Wtype_limits,
3061 "comparison of unsigned expression < 0 is always false");
3062 value = truthvalue_false_node;
3063 break;
3065 default:
3066 break;
3069 if (value != NULL_TREE)
3071 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3072 if (TREE_SIDE_EFFECTS (primop0))
3073 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3074 primop0, value);
3075 return value;
3080 *op0_ptr = convert (type, primop0);
3081 *op1_ptr = convert (type, primop1);
3083 *restype_ptr = truthvalue_type_node;
3085 return NULL_TREE;
3088 /* Return a tree for the sum or difference (RESULTCODE says which)
3089 of pointer PTROP and integer INTOP. */
3091 tree
3092 pointer_int_sum (location_t loc, enum tree_code resultcode,
3093 tree ptrop, tree intop, bool complain)
3095 tree size_exp, ret;
3097 /* The result is a pointer of the same type that is being added. */
3098 tree result_type = TREE_TYPE (ptrop);
3100 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3102 if (complain && warn_pointer_arith)
3103 pedwarn (loc, OPT_Wpointer_arith,
3104 "pointer of type %<void *%> used in arithmetic");
3105 else if (!complain)
3106 return error_mark_node;
3107 size_exp = integer_one_node;
3109 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3111 if (complain && warn_pointer_arith)
3112 pedwarn (loc, OPT_Wpointer_arith,
3113 "pointer to a function used in arithmetic");
3114 else if (!complain)
3115 return error_mark_node;
3116 size_exp = integer_one_node;
3118 else
3119 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3121 /* We are manipulating pointer values, so we don't need to warn
3122 about relying on undefined signed overflow. We disable the
3123 warning here because we use integer types so fold won't know that
3124 they are really pointers. */
3125 fold_defer_overflow_warnings ();
3127 /* If what we are about to multiply by the size of the elements
3128 contains a constant term, apply distributive law
3129 and multiply that constant term separately.
3130 This helps produce common subexpressions. */
3131 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3132 && !TREE_CONSTANT (intop)
3133 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3134 && TREE_CONSTANT (size_exp)
3135 /* If the constant comes from pointer subtraction,
3136 skip this optimization--it would cause an error. */
3137 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3138 /* If the constant is unsigned, and smaller than the pointer size,
3139 then we must skip this optimization. This is because it could cause
3140 an overflow error if the constant is negative but INTOP is not. */
3141 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3142 || (TYPE_PRECISION (TREE_TYPE (intop))
3143 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3145 enum tree_code subcode = resultcode;
3146 tree int_type = TREE_TYPE (intop);
3147 if (TREE_CODE (intop) == MINUS_EXPR)
3148 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3149 /* Convert both subexpression types to the type of intop,
3150 because weird cases involving pointer arithmetic
3151 can result in a sum or difference with different type args. */
3152 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3153 subcode, ptrop,
3154 convert (int_type, TREE_OPERAND (intop, 1)),
3155 true);
3156 intop = convert (int_type, TREE_OPERAND (intop, 0));
3159 /* Convert the integer argument to a type the same size as sizetype
3160 so the multiply won't overflow spuriously. */
3161 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3162 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3163 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3164 TYPE_UNSIGNED (sizetype)), intop);
3166 /* Replace the integer argument with a suitable product by the object size.
3167 Do this multiplication as signed, then convert to the appropriate type
3168 for the pointer operation and disregard an overflow that occurred only
3169 because of the sign-extension change in the latter conversion. */
3171 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3172 convert (TREE_TYPE (intop), size_exp));
3173 intop = convert (sizetype, t);
3174 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3175 intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3178 /* Create the sum or difference. */
3179 if (resultcode == MINUS_EXPR)
3180 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3182 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3184 fold_undefer_and_ignore_overflow_warnings ();
3186 return ret;
3189 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3190 and if NON_CONST is known not to be permitted in an evaluated part
3191 of a constant expression. */
3193 tree
3194 c_wrap_maybe_const (tree expr, bool non_const)
3196 bool nowarning = TREE_NO_WARNING (expr);
3197 location_t loc = EXPR_LOCATION (expr);
3199 /* This should never be called for C++. */
3200 if (c_dialect_cxx ())
3201 gcc_unreachable ();
3203 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3204 STRIP_TYPE_NOPS (expr);
3205 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3206 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3207 if (nowarning)
3208 TREE_NO_WARNING (expr) = 1;
3209 protected_set_expr_location (expr, loc);
3211 return expr;
3214 /* Return whether EXPR is a declaration whose address can never be
3215 NULL. */
3217 bool
3218 decl_with_nonnull_addr_p (const_tree expr)
3220 return (DECL_P (expr)
3221 && (TREE_CODE (expr) == PARM_DECL
3222 || TREE_CODE (expr) == LABEL_DECL
3223 || !DECL_WEAK (expr)));
3226 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3227 or for an `if' or `while' statement or ?..: exp. It should already
3228 have been validated to be of suitable type; otherwise, a bad
3229 diagnostic may result.
3231 The EXPR is located at LOCATION.
3233 This preparation consists of taking the ordinary
3234 representation of an expression expr and producing a valid tree
3235 boolean expression describing whether expr is nonzero. We could
3236 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3237 but we optimize comparisons, &&, ||, and !.
3239 The resulting type should always be `truthvalue_type_node'. */
3241 tree
3242 c_common_truthvalue_conversion (location_t location, tree expr)
3244 switch (TREE_CODE (expr))
3246 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3247 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3248 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3249 case ORDERED_EXPR: case UNORDERED_EXPR:
3250 if (TREE_TYPE (expr) == truthvalue_type_node)
3251 return expr;
3252 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3253 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3254 goto ret;
3256 case TRUTH_ANDIF_EXPR:
3257 case TRUTH_ORIF_EXPR:
3258 case TRUTH_AND_EXPR:
3259 case TRUTH_OR_EXPR:
3260 case TRUTH_XOR_EXPR:
3261 if (TREE_TYPE (expr) == truthvalue_type_node)
3262 return expr;
3263 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3264 c_common_truthvalue_conversion (location,
3265 TREE_OPERAND (expr, 0)),
3266 c_common_truthvalue_conversion (location,
3267 TREE_OPERAND (expr, 1)));
3268 goto ret;
3270 case TRUTH_NOT_EXPR:
3271 if (TREE_TYPE (expr) == truthvalue_type_node)
3272 return expr;
3273 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3274 c_common_truthvalue_conversion (location,
3275 TREE_OPERAND (expr, 0)));
3276 goto ret;
3278 case ERROR_MARK:
3279 return expr;
3281 case INTEGER_CST:
3282 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3283 && !integer_zerop (expr)
3284 && !integer_onep (expr))
3285 warning_at (location, OPT_Wint_in_bool_context,
3286 "enum constant in boolean context");
3287 return integer_zerop (expr) ? truthvalue_false_node
3288 : truthvalue_true_node;
3290 case REAL_CST:
3291 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3292 ? truthvalue_true_node
3293 : truthvalue_false_node;
3295 case FIXED_CST:
3296 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3297 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3298 ? truthvalue_true_node
3299 : truthvalue_false_node;
3301 case FUNCTION_DECL:
3302 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3303 /* Fall through. */
3305 case ADDR_EXPR:
3307 tree inner = TREE_OPERAND (expr, 0);
3308 if (decl_with_nonnull_addr_p (inner))
3310 /* Common Ada programmer's mistake. */
3311 warning_at (location,
3312 OPT_Waddress,
3313 "the address of %qD will always evaluate as %<true%>",
3314 inner);
3315 return truthvalue_true_node;
3317 break;
3320 case COMPLEX_EXPR:
3321 expr = build_binary_op (EXPR_LOCATION (expr),
3322 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3323 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3324 c_common_truthvalue_conversion (location,
3325 TREE_OPERAND (expr, 0)),
3326 c_common_truthvalue_conversion (location,
3327 TREE_OPERAND (expr, 1)),
3328 false);
3329 goto ret;
3331 case NEGATE_EXPR:
3332 case ABS_EXPR:
3333 case ABSU_EXPR:
3334 case FLOAT_EXPR:
3335 case EXCESS_PRECISION_EXPR:
3336 /* These don't change whether an object is nonzero or zero. */
3337 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3339 case LROTATE_EXPR:
3340 case RROTATE_EXPR:
3341 /* These don't change whether an object is zero or nonzero, but
3342 we can't ignore them if their second arg has side-effects. */
3343 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3345 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3346 TREE_OPERAND (expr, 1),
3347 c_common_truthvalue_conversion
3348 (location, TREE_OPERAND (expr, 0)));
3349 goto ret;
3351 else
3352 return c_common_truthvalue_conversion (location,
3353 TREE_OPERAND (expr, 0));
3355 case MULT_EXPR:
3356 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3357 "%<*%> in boolean context, suggest %<&&%> instead");
3358 break;
3360 case LSHIFT_EXPR:
3361 /* We will only warn on signed shifts here, because the majority of
3362 false positive warnings happen in code where unsigned arithmetic
3363 was used in anticipation of a possible overflow.
3364 Furthermore, if we see an unsigned type here we know that the
3365 result of the shift is not subject to integer promotion rules. */
3366 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3367 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3368 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3369 "%<<<%> in boolean context, did you mean %<<%> ?");
3370 break;
3372 case COND_EXPR:
3373 if (warn_int_in_bool_context
3374 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3376 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3377 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3378 if (TREE_CODE (val1) == INTEGER_CST
3379 && TREE_CODE (val2) == INTEGER_CST
3380 && !integer_zerop (val1)
3381 && !integer_zerop (val2)
3382 && (!integer_onep (val1)
3383 || !integer_onep (val2)))
3384 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3385 "?: using integer constants in boolean context, "
3386 "the expression will always evaluate to %<true%>");
3387 else if ((TREE_CODE (val1) == INTEGER_CST
3388 && !integer_zerop (val1)
3389 && !integer_onep (val1))
3390 || (TREE_CODE (val2) == INTEGER_CST
3391 && !integer_zerop (val2)
3392 && !integer_onep (val2)))
3393 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3394 "?: using integer constants in boolean context");
3396 /* Distribute the conversion into the arms of a COND_EXPR. */
3397 if (c_dialect_cxx ())
3398 /* Avoid premature folding. */
3399 break;
3400 else
3402 int w = warn_int_in_bool_context;
3403 warn_int_in_bool_context = 0;
3404 /* Folding will happen later for C. */
3405 expr = build3 (COND_EXPR, truthvalue_type_node,
3406 TREE_OPERAND (expr, 0),
3407 c_common_truthvalue_conversion (location,
3408 TREE_OPERAND (expr, 1)),
3409 c_common_truthvalue_conversion (location,
3410 TREE_OPERAND (expr, 2)));
3411 warn_int_in_bool_context = w;
3412 goto ret;
3415 CASE_CONVERT:
3417 tree totype = TREE_TYPE (expr);
3418 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3420 if (POINTER_TYPE_P (totype)
3421 && !c_inhibit_evaluation_warnings
3422 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3424 tree inner = expr;
3425 STRIP_NOPS (inner);
3427 if (DECL_P (inner))
3428 warning_at (location,
3429 OPT_Waddress,
3430 "the compiler can assume that the address of "
3431 "%qD will always evaluate to %<true%>",
3432 inner);
3435 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3436 since that affects how `default_conversion' will behave. */
3437 if (TREE_CODE (totype) == REFERENCE_TYPE
3438 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3439 break;
3440 /* Don't strip a conversion from C++0x scoped enum, since they
3441 don't implicitly convert to other types. */
3442 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3443 && ENUM_IS_SCOPED (fromtype))
3444 break;
3445 /* If this isn't narrowing the argument, we can ignore it. */
3446 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3447 return c_common_truthvalue_conversion (location,
3448 TREE_OPERAND (expr, 0));
3450 break;
3452 case MODIFY_EXPR:
3453 if (!TREE_NO_WARNING (expr)
3454 && warn_parentheses)
3456 warning_at (location, OPT_Wparentheses,
3457 "suggest parentheses around assignment used as "
3458 "truth value");
3459 TREE_NO_WARNING (expr) = 1;
3461 break;
3463 default:
3464 break;
3467 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3469 tree t = save_expr (expr);
3470 expr = (build_binary_op
3471 (EXPR_LOCATION (expr),
3472 (TREE_SIDE_EFFECTS (expr)
3473 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3474 c_common_truthvalue_conversion
3475 (location,
3476 build_unary_op (location, REALPART_EXPR, t, false)),
3477 c_common_truthvalue_conversion
3478 (location,
3479 build_unary_op (location, IMAGPART_EXPR, t, false)),
3480 false));
3481 goto ret;
3484 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3486 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3487 FCONST0 (TYPE_MODE
3488 (TREE_TYPE (expr))));
3489 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3491 else
3492 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3494 ret:
3495 protected_set_expr_location (expr, location);
3496 return expr;
3499 static void def_builtin_1 (enum built_in_function fncode,
3500 const char *name,
3501 enum built_in_class fnclass,
3502 tree fntype, tree libtype,
3503 bool both_p, bool fallback_p, bool nonansi_p,
3504 tree fnattrs, bool implicit_p);
3507 /* Apply the TYPE_QUALS to the new DECL. */
3509 void
3510 c_apply_type_quals_to_decl (int type_quals, tree decl)
3512 tree type = TREE_TYPE (decl);
3514 if (type == error_mark_node)
3515 return;
3517 if ((type_quals & TYPE_QUAL_CONST)
3518 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3519 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3520 constructor can produce constant init, so rely on cp_finish_decl to
3521 clear TREE_READONLY if the variable has non-constant init. */
3522 TREE_READONLY (decl) = 1;
3523 if (type_quals & TYPE_QUAL_VOLATILE)
3525 TREE_SIDE_EFFECTS (decl) = 1;
3526 TREE_THIS_VOLATILE (decl) = 1;
3528 if (type_quals & TYPE_QUAL_RESTRICT)
3530 while (type && TREE_CODE (type) == ARRAY_TYPE)
3531 /* Allow 'restrict' on arrays of pointers.
3532 FIXME currently we just ignore it. */
3533 type = TREE_TYPE (type);
3534 if (!type
3535 || !POINTER_TYPE_P (type)
3536 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3537 error ("invalid use of %<restrict%>");
3541 /* Return the typed-based alias set for T, which may be an expression
3542 or a type. Return -1 if we don't do anything special. */
3544 alias_set_type
3545 c_common_get_alias_set (tree t)
3547 /* For VLAs, use the alias set of the element type rather than the
3548 default of alias set 0 for types compared structurally. */
3549 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3551 if (TREE_CODE (t) == ARRAY_TYPE)
3552 return get_alias_set (TREE_TYPE (t));
3553 return -1;
3556 /* That's all the expressions we handle specially. */
3557 if (!TYPE_P (t))
3558 return -1;
3560 /* The C standard guarantees that any object may be accessed via an
3561 lvalue that has character type. */
3562 if (t == char_type_node
3563 || t == signed_char_type_node
3564 || t == unsigned_char_type_node)
3565 return 0;
3567 /* The C standard specifically allows aliasing between signed and
3568 unsigned variants of the same type. We treat the signed
3569 variant as canonical. */
3570 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3572 tree t1 = c_common_signed_type (t);
3574 /* t1 == t can happen for boolean nodes which are always unsigned. */
3575 if (t1 != t)
3576 return get_alias_set (t1);
3579 return -1;
3582 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3583 the IS_SIZEOF parameter indicates which operator is being applied.
3584 The COMPLAIN flag controls whether we should diagnose possibly
3585 ill-formed constructs or not. LOC is the location of the SIZEOF or
3586 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3587 a type in any context should be returned, rather than the normal
3588 alignment for that type. */
3590 tree
3591 c_sizeof_or_alignof_type (location_t loc,
3592 tree type, bool is_sizeof, bool min_alignof,
3593 int complain)
3595 const char *op_name;
3596 tree value = NULL;
3597 enum tree_code type_code = TREE_CODE (type);
3599 op_name = is_sizeof ? "sizeof" : "__alignof__";
3601 if (type_code == FUNCTION_TYPE)
3603 if (is_sizeof)
3605 if (complain && warn_pointer_arith)
3606 pedwarn (loc, OPT_Wpointer_arith,
3607 "invalid application of %<sizeof%> to a function type");
3608 else if (!complain)
3609 return error_mark_node;
3610 value = size_one_node;
3612 else
3614 if (complain)
3616 if (c_dialect_cxx ())
3617 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3618 "%<alignof%> applied to a function type");
3619 else
3620 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3621 "%<_Alignof%> applied to a function type");
3623 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3626 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3628 if (type_code == VOID_TYPE
3629 && complain && warn_pointer_arith)
3630 pedwarn (loc, OPT_Wpointer_arith,
3631 "invalid application of %qs to a void type", op_name);
3632 else if (!complain)
3633 return error_mark_node;
3634 value = size_one_node;
3636 else if (!COMPLETE_TYPE_P (type)
3637 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3639 if (complain)
3640 error_at (loc, "invalid application of %qs to incomplete type %qT",
3641 op_name, type);
3642 return error_mark_node;
3644 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3645 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3647 if (complain)
3648 error_at (loc, "invalid application of %qs to array type %qT of "
3649 "incomplete element type", op_name, type);
3650 return error_mark_node;
3652 else
3654 if (is_sizeof)
3655 /* Convert in case a char is more than one unit. */
3656 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3657 size_int (TYPE_PRECISION (char_type_node)
3658 / BITS_PER_UNIT));
3659 else if (min_alignof)
3660 value = size_int (min_align_of_type (type));
3661 else
3662 value = size_int (TYPE_ALIGN_UNIT (type));
3665 /* VALUE will have the middle-end integer type sizetype.
3666 However, we should really return a value of type `size_t',
3667 which is just a typedef for an ordinary integer type. */
3668 value = fold_convert_loc (loc, size_type_node, value);
3670 return value;
3673 /* Implement the __alignof keyword: Return the minimum required
3674 alignment of EXPR, measured in bytes. For VAR_DECLs,
3675 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3676 from an "aligned" __attribute__ specification). LOC is the
3677 location of the ALIGNOF operator. */
3679 tree
3680 c_alignof_expr (location_t loc, tree expr)
3682 tree t;
3684 if (VAR_OR_FUNCTION_DECL_P (expr))
3685 t = size_int (DECL_ALIGN_UNIT (expr));
3687 else if (TREE_CODE (expr) == COMPONENT_REF
3688 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3690 error_at (loc, "%<__alignof%> applied to a bit-field");
3691 t = size_one_node;
3693 else if (TREE_CODE (expr) == COMPONENT_REF
3694 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3695 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3697 else if (INDIRECT_REF_P (expr))
3699 tree t = TREE_OPERAND (expr, 0);
3700 tree best = t;
3701 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3703 while (CONVERT_EXPR_P (t)
3704 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3706 int thisalign;
3708 t = TREE_OPERAND (t, 0);
3709 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3710 if (thisalign > bestalign)
3711 best = t, bestalign = thisalign;
3713 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3715 else
3716 return c_alignof (loc, TREE_TYPE (expr));
3718 return fold_convert_loc (loc, size_type_node, t);
3721 /* Handle C and C++ default attributes. */
3723 enum built_in_attribute
3725 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3726 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3727 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3728 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3729 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3730 #include "builtin-attrs.def"
3731 #undef DEF_ATTR_NULL_TREE
3732 #undef DEF_ATTR_INT
3733 #undef DEF_ATTR_STRING
3734 #undef DEF_ATTR_IDENT
3735 #undef DEF_ATTR_TREE_LIST
3736 ATTR_LAST
3739 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3741 static void c_init_attributes (void);
3743 enum c_builtin_type
3745 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3746 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3747 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3748 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3749 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3750 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3751 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3752 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3753 ARG6) NAME,
3754 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3755 ARG6, ARG7) NAME,
3756 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3757 ARG6, ARG7, ARG8) NAME,
3758 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3759 ARG6, ARG7, ARG8, ARG9) NAME,
3760 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3761 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3762 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3763 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3764 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3765 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3766 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3767 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3768 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3769 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3770 NAME,
3771 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3772 ARG6) NAME,
3773 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3774 ARG6, ARG7) NAME,
3775 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3776 #include "builtin-types.def"
3777 #undef DEF_PRIMITIVE_TYPE
3778 #undef DEF_FUNCTION_TYPE_0
3779 #undef DEF_FUNCTION_TYPE_1
3780 #undef DEF_FUNCTION_TYPE_2
3781 #undef DEF_FUNCTION_TYPE_3
3782 #undef DEF_FUNCTION_TYPE_4
3783 #undef DEF_FUNCTION_TYPE_5
3784 #undef DEF_FUNCTION_TYPE_6
3785 #undef DEF_FUNCTION_TYPE_7
3786 #undef DEF_FUNCTION_TYPE_8
3787 #undef DEF_FUNCTION_TYPE_9
3788 #undef DEF_FUNCTION_TYPE_10
3789 #undef DEF_FUNCTION_TYPE_11
3790 #undef DEF_FUNCTION_TYPE_VAR_0
3791 #undef DEF_FUNCTION_TYPE_VAR_1
3792 #undef DEF_FUNCTION_TYPE_VAR_2
3793 #undef DEF_FUNCTION_TYPE_VAR_3
3794 #undef DEF_FUNCTION_TYPE_VAR_4
3795 #undef DEF_FUNCTION_TYPE_VAR_5
3796 #undef DEF_FUNCTION_TYPE_VAR_6
3797 #undef DEF_FUNCTION_TYPE_VAR_7
3798 #undef DEF_POINTER_TYPE
3799 BT_LAST
3802 typedef enum c_builtin_type builtin_type;
3804 /* A temporary array for c_common_nodes_and_builtins. Used in
3805 communication with def_fn_type. */
3806 static tree builtin_types[(int) BT_LAST + 1];
3808 /* A helper function for c_common_nodes_and_builtins. Build function type
3809 for DEF with return type RET and N arguments. If VAR is true, then the
3810 function should be variadic after those N arguments.
3812 Takes special care not to ICE if any of the types involved are
3813 error_mark_node, which indicates that said type is not in fact available
3814 (see builtin_type_for_size). In which case the function type as a whole
3815 should be error_mark_node. */
3817 static void
3818 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3820 tree t;
3821 tree *args = XALLOCAVEC (tree, n);
3822 va_list list;
3823 int i;
3825 va_start (list, n);
3826 for (i = 0; i < n; ++i)
3828 builtin_type a = (builtin_type) va_arg (list, int);
3829 t = builtin_types[a];
3830 if (t == error_mark_node)
3831 goto egress;
3832 args[i] = t;
3835 t = builtin_types[ret];
3836 if (t == error_mark_node)
3837 goto egress;
3838 if (var)
3839 t = build_varargs_function_type_array (t, n, args);
3840 else
3841 t = build_function_type_array (t, n, args);
3843 egress:
3844 builtin_types[def] = t;
3845 va_end (list);
3848 /* Build builtin functions common to both C and C++ language
3849 frontends. */
3851 static void
3852 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3854 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3855 builtin_types[ENUM] = VALUE;
3856 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3857 def_fn_type (ENUM, RETURN, 0, 0);
3858 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3859 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3860 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3861 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3862 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3863 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3864 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3865 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3866 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3867 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3868 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3869 ARG6) \
3870 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3871 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3872 ARG6, ARG7) \
3873 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3874 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3875 ARG6, ARG7, ARG8) \
3876 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3877 ARG7, ARG8);
3878 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3879 ARG6, ARG7, ARG8, ARG9) \
3880 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3881 ARG7, ARG8, ARG9);
3882 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3883 ARG6, ARG7, ARG8, ARG9, ARG10) \
3884 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3885 ARG7, ARG8, ARG9, ARG10);
3886 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3887 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3888 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3889 ARG7, ARG8, ARG9, ARG10, ARG11);
3890 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3891 def_fn_type (ENUM, RETURN, 1, 0);
3892 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3893 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3894 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3895 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3896 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3897 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3898 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3899 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3900 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3901 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3902 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3903 ARG6) \
3904 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3905 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3906 ARG6, ARG7) \
3907 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3908 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3909 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3911 #include "builtin-types.def"
3913 #undef DEF_PRIMITIVE_TYPE
3914 #undef DEF_FUNCTION_TYPE_0
3915 #undef DEF_FUNCTION_TYPE_1
3916 #undef DEF_FUNCTION_TYPE_2
3917 #undef DEF_FUNCTION_TYPE_3
3918 #undef DEF_FUNCTION_TYPE_4
3919 #undef DEF_FUNCTION_TYPE_5
3920 #undef DEF_FUNCTION_TYPE_6
3921 #undef DEF_FUNCTION_TYPE_7
3922 #undef DEF_FUNCTION_TYPE_8
3923 #undef DEF_FUNCTION_TYPE_9
3924 #undef DEF_FUNCTION_TYPE_10
3925 #undef DEF_FUNCTION_TYPE_11
3926 #undef DEF_FUNCTION_TYPE_VAR_0
3927 #undef DEF_FUNCTION_TYPE_VAR_1
3928 #undef DEF_FUNCTION_TYPE_VAR_2
3929 #undef DEF_FUNCTION_TYPE_VAR_3
3930 #undef DEF_FUNCTION_TYPE_VAR_4
3931 #undef DEF_FUNCTION_TYPE_VAR_5
3932 #undef DEF_FUNCTION_TYPE_VAR_6
3933 #undef DEF_FUNCTION_TYPE_VAR_7
3934 #undef DEF_POINTER_TYPE
3935 builtin_types[(int) BT_LAST] = NULL_TREE;
3937 c_init_attributes ();
3939 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3940 NONANSI_P, ATTRS, IMPLICIT, COND) \
3941 if (NAME && COND) \
3942 def_builtin_1 (ENUM, NAME, CLASS, \
3943 builtin_types[(int) TYPE], \
3944 builtin_types[(int) LIBTYPE], \
3945 BOTH_P, FALLBACK_P, NONANSI_P, \
3946 built_in_attributes[(int) ATTRS], IMPLICIT);
3947 #include "builtins.def"
3949 targetm.init_builtins ();
3951 build_common_builtin_nodes ();
3954 /* Like get_identifier, but avoid warnings about null arguments when
3955 the argument may be NULL for targets where GCC lacks stdint.h type
3956 information. */
3958 static inline tree
3959 c_get_ident (const char *id)
3961 return get_identifier (id);
3964 /* Build tree nodes and builtin functions common to both C and C++ language
3965 frontends. */
3967 void
3968 c_common_nodes_and_builtins (void)
3970 int char16_type_size;
3971 int char32_type_size;
3972 int wchar_type_size;
3973 tree array_domain_type;
3974 tree va_list_ref_type_node;
3975 tree va_list_arg_type_node;
3976 int i;
3978 build_common_tree_nodes (flag_signed_char);
3980 /* Define `int' and `char' first so that dbx will output them first. */
3981 record_builtin_type (RID_INT, NULL, integer_type_node);
3982 record_builtin_type (RID_CHAR, "char", char_type_node);
3984 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3985 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3986 but not C. Are the conditionals here needed? */
3987 if (c_dialect_cxx ())
3988 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3989 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3990 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3991 record_builtin_type (RID_MAX, "long unsigned int",
3992 long_unsigned_type_node);
3994 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3996 char name[25];
3998 sprintf (name, "__int%d", int_n_data[i].bitsize);
3999 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4000 int_n_trees[i].signed_type);
4001 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4002 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4005 if (c_dialect_cxx ())
4006 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4007 record_builtin_type (RID_MAX, "long long int",
4008 long_long_integer_type_node);
4009 record_builtin_type (RID_MAX, "long long unsigned int",
4010 long_long_unsigned_type_node);
4011 if (c_dialect_cxx ())
4012 record_builtin_type (RID_MAX, "long long unsigned",
4013 long_long_unsigned_type_node);
4014 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4015 record_builtin_type (RID_MAX, "short unsigned int",
4016 short_unsigned_type_node);
4017 if (c_dialect_cxx ())
4018 record_builtin_type (RID_MAX, "unsigned short",
4019 short_unsigned_type_node);
4021 /* Define both `signed char' and `unsigned char'. */
4022 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4023 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4025 /* These are types that c_common_type_for_size and
4026 c_common_type_for_mode use. */
4027 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4028 TYPE_DECL, NULL_TREE,
4029 intQI_type_node));
4030 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4031 TYPE_DECL, NULL_TREE,
4032 intHI_type_node));
4033 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4034 TYPE_DECL, NULL_TREE,
4035 intSI_type_node));
4036 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4037 TYPE_DECL, NULL_TREE,
4038 intDI_type_node));
4039 #if HOST_BITS_PER_WIDE_INT >= 64
4040 /* Note that this is different than the __int128 type that's part of
4041 the generic __intN support. */
4042 if (targetm.scalar_mode_supported_p (TImode))
4043 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4044 TYPE_DECL,
4045 get_identifier ("__int128_t"),
4046 intTI_type_node));
4047 #endif
4048 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4049 TYPE_DECL, NULL_TREE,
4050 unsigned_intQI_type_node));
4051 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4052 TYPE_DECL, NULL_TREE,
4053 unsigned_intHI_type_node));
4054 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4055 TYPE_DECL, NULL_TREE,
4056 unsigned_intSI_type_node));
4057 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4058 TYPE_DECL, NULL_TREE,
4059 unsigned_intDI_type_node));
4060 #if HOST_BITS_PER_WIDE_INT >= 64
4061 if (targetm.scalar_mode_supported_p (TImode))
4062 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4063 TYPE_DECL,
4064 get_identifier ("__uint128_t"),
4065 unsigned_intTI_type_node));
4066 #endif
4068 /* Create the widest literal types. */
4069 if (targetm.scalar_mode_supported_p (TImode))
4071 widest_integer_literal_type_node = intTI_type_node;
4072 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4074 else
4076 widest_integer_literal_type_node = intDI_type_node;
4077 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4080 signed_size_type_node = c_common_signed_type (size_type_node);
4082 pid_type_node =
4083 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4085 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4086 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4087 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4089 if (!c_dialect_cxx ())
4090 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4091 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4092 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4093 FLOATN_NX_TYPE_NODE (i));
4095 /* Only supported decimal floating point extension if the target
4096 actually supports underlying modes. */
4097 if (targetm.scalar_mode_supported_p (SDmode)
4098 && targetm.scalar_mode_supported_p (DDmode)
4099 && targetm.scalar_mode_supported_p (TDmode))
4101 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4102 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4103 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4106 if (targetm.fixed_point_supported_p ())
4108 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4109 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4110 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4111 record_builtin_type (RID_MAX, "long long _Fract",
4112 long_long_fract_type_node);
4113 record_builtin_type (RID_MAX, "unsigned short _Fract",
4114 unsigned_short_fract_type_node);
4115 record_builtin_type (RID_MAX, "unsigned _Fract",
4116 unsigned_fract_type_node);
4117 record_builtin_type (RID_MAX, "unsigned long _Fract",
4118 unsigned_long_fract_type_node);
4119 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4120 unsigned_long_long_fract_type_node);
4121 record_builtin_type (RID_MAX, "_Sat short _Fract",
4122 sat_short_fract_type_node);
4123 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4124 record_builtin_type (RID_MAX, "_Sat long _Fract",
4125 sat_long_fract_type_node);
4126 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4127 sat_long_long_fract_type_node);
4128 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4129 sat_unsigned_short_fract_type_node);
4130 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4131 sat_unsigned_fract_type_node);
4132 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4133 sat_unsigned_long_fract_type_node);
4134 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4135 sat_unsigned_long_long_fract_type_node);
4136 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4137 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4138 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4139 record_builtin_type (RID_MAX, "long long _Accum",
4140 long_long_accum_type_node);
4141 record_builtin_type (RID_MAX, "unsigned short _Accum",
4142 unsigned_short_accum_type_node);
4143 record_builtin_type (RID_MAX, "unsigned _Accum",
4144 unsigned_accum_type_node);
4145 record_builtin_type (RID_MAX, "unsigned long _Accum",
4146 unsigned_long_accum_type_node);
4147 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4148 unsigned_long_long_accum_type_node);
4149 record_builtin_type (RID_MAX, "_Sat short _Accum",
4150 sat_short_accum_type_node);
4151 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4152 record_builtin_type (RID_MAX, "_Sat long _Accum",
4153 sat_long_accum_type_node);
4154 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4155 sat_long_long_accum_type_node);
4156 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4157 sat_unsigned_short_accum_type_node);
4158 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4159 sat_unsigned_accum_type_node);
4160 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4161 sat_unsigned_long_accum_type_node);
4162 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4163 sat_unsigned_long_long_accum_type_node);
4167 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4168 TYPE_DECL,
4169 get_identifier ("complex int"),
4170 complex_integer_type_node));
4171 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4172 TYPE_DECL,
4173 get_identifier ("complex float"),
4174 complex_float_type_node));
4175 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4176 TYPE_DECL,
4177 get_identifier ("complex double"),
4178 complex_double_type_node));
4179 lang_hooks.decls.pushdecl
4180 (build_decl (UNKNOWN_LOCATION,
4181 TYPE_DECL, get_identifier ("complex long double"),
4182 complex_long_double_type_node));
4184 if (!c_dialect_cxx ())
4185 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4186 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4188 char buf[30];
4189 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4190 floatn_nx_types[i].extended ? "x" : "");
4191 lang_hooks.decls.pushdecl
4192 (build_decl (UNKNOWN_LOCATION,
4193 TYPE_DECL,
4194 get_identifier (buf),
4195 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4198 if (c_dialect_cxx ())
4200 /* For C++, make fileptr_type_node a distinct void * type until
4201 FILE type is defined. Likewise for const struct tm*. */
4202 for (unsigned i = 0;
4203 i < sizeof (builtin_structptr_types)
4204 / sizeof (builtin_structptr_type);
4205 ++i)
4206 builtin_structptr_types[i].node =
4207 build_variant_type_copy (builtin_structptr_types[i].base);
4211 record_builtin_type (RID_VOID, NULL, void_type_node);
4213 /* Set the TYPE_NAME for any variants that were built before
4214 record_builtin_type gave names to the built-in types. */
4216 tree void_name = TYPE_NAME (void_type_node);
4217 TYPE_NAME (void_type_node) = NULL_TREE;
4218 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4219 = void_name;
4220 TYPE_NAME (void_type_node) = void_name;
4223 void_list_node = build_void_list_node ();
4225 /* Make a type to be the domain of a few array types
4226 whose domains don't really matter.
4227 200 is small enough that it always fits in size_t
4228 and large enough that it can hold most function names for the
4229 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4230 array_domain_type = build_index_type (size_int (200));
4232 /* Make a type for arrays of characters.
4233 With luck nothing will ever really depend on the length of this
4234 array type. */
4235 char_array_type_node
4236 = build_array_type (char_type_node, array_domain_type);
4238 string_type_node = build_pointer_type (char_type_node);
4239 const_string_type_node
4240 = build_pointer_type (build_qualified_type
4241 (char_type_node, TYPE_QUAL_CONST));
4243 /* This is special for C++ so functions can be overloaded. */
4244 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4245 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4246 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4247 underlying_wchar_type_node = wchar_type_node;
4248 if (c_dialect_cxx ())
4250 if (TYPE_UNSIGNED (wchar_type_node))
4251 wchar_type_node = make_unsigned_type (wchar_type_size);
4252 else
4253 wchar_type_node = make_signed_type (wchar_type_size);
4254 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4257 /* This is for wide string constants. */
4258 wchar_array_type_node
4259 = build_array_type (wchar_type_node, array_domain_type);
4261 /* Define 'char16_t'. */
4262 char16_type_node = get_identifier (CHAR16_TYPE);
4263 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4264 char16_type_size = TYPE_PRECISION (char16_type_node);
4265 if (c_dialect_cxx ())
4267 char16_type_node = make_unsigned_type (char16_type_size);
4269 if (cxx_dialect >= cxx11)
4270 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4273 /* This is for UTF-16 string constants. */
4274 char16_array_type_node
4275 = build_array_type (char16_type_node, array_domain_type);
4277 /* Define 'char32_t'. */
4278 char32_type_node = get_identifier (CHAR32_TYPE);
4279 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4280 char32_type_size = TYPE_PRECISION (char32_type_node);
4281 if (c_dialect_cxx ())
4283 char32_type_node = make_unsigned_type (char32_type_size);
4285 if (cxx_dialect >= cxx11)
4286 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4289 /* This is for UTF-32 string constants. */
4290 char32_array_type_node
4291 = build_array_type (char32_type_node, array_domain_type);
4293 wint_type_node =
4294 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4296 intmax_type_node =
4297 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4298 uintmax_type_node =
4299 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4301 if (SIG_ATOMIC_TYPE)
4302 sig_atomic_type_node =
4303 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4304 if (INT8_TYPE)
4305 int8_type_node =
4306 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4307 if (INT16_TYPE)
4308 int16_type_node =
4309 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4310 if (INT32_TYPE)
4311 int32_type_node =
4312 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4313 if (INT64_TYPE)
4314 int64_type_node =
4315 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4316 if (UINT8_TYPE)
4317 uint8_type_node =
4318 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4319 if (UINT16_TYPE)
4320 c_uint16_type_node = uint16_type_node =
4321 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4322 if (UINT32_TYPE)
4323 c_uint32_type_node = uint32_type_node =
4324 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4325 if (UINT64_TYPE)
4326 c_uint64_type_node = uint64_type_node =
4327 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4328 if (INT_LEAST8_TYPE)
4329 int_least8_type_node =
4330 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4331 if (INT_LEAST16_TYPE)
4332 int_least16_type_node =
4333 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4334 if (INT_LEAST32_TYPE)
4335 int_least32_type_node =
4336 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4337 if (INT_LEAST64_TYPE)
4338 int_least64_type_node =
4339 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4340 if (UINT_LEAST8_TYPE)
4341 uint_least8_type_node =
4342 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4343 if (UINT_LEAST16_TYPE)
4344 uint_least16_type_node =
4345 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4346 if (UINT_LEAST32_TYPE)
4347 uint_least32_type_node =
4348 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4349 if (UINT_LEAST64_TYPE)
4350 uint_least64_type_node =
4351 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4352 if (INT_FAST8_TYPE)
4353 int_fast8_type_node =
4354 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4355 if (INT_FAST16_TYPE)
4356 int_fast16_type_node =
4357 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4358 if (INT_FAST32_TYPE)
4359 int_fast32_type_node =
4360 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4361 if (INT_FAST64_TYPE)
4362 int_fast64_type_node =
4363 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4364 if (UINT_FAST8_TYPE)
4365 uint_fast8_type_node =
4366 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4367 if (UINT_FAST16_TYPE)
4368 uint_fast16_type_node =
4369 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4370 if (UINT_FAST32_TYPE)
4371 uint_fast32_type_node =
4372 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4373 if (UINT_FAST64_TYPE)
4374 uint_fast64_type_node =
4375 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4376 if (INTPTR_TYPE)
4377 intptr_type_node =
4378 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4379 if (UINTPTR_TYPE)
4380 uintptr_type_node =
4381 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4383 default_function_type
4384 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4385 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4387 lang_hooks.decls.pushdecl
4388 (build_decl (UNKNOWN_LOCATION,
4389 TYPE_DECL, get_identifier ("__builtin_va_list"),
4390 va_list_type_node));
4391 if (targetm.enum_va_list_p)
4393 int l;
4394 const char *pname;
4395 tree ptype;
4397 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4399 lang_hooks.decls.pushdecl
4400 (build_decl (UNKNOWN_LOCATION,
4401 TYPE_DECL, get_identifier (pname),
4402 ptype));
4407 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4409 va_list_arg_type_node = va_list_ref_type_node =
4410 build_pointer_type (TREE_TYPE (va_list_type_node));
4412 else
4414 va_list_arg_type_node = va_list_type_node;
4415 va_list_ref_type_node = build_reference_type (va_list_type_node);
4418 if (!flag_preprocess_only)
4419 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4421 main_identifier_node = get_identifier ("main");
4423 /* Create the built-in __null node. It is important that this is
4424 not shared. */
4425 null_node = make_int_cst (1, 1);
4426 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4428 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4429 memset (builtin_types, 0, sizeof (builtin_types));
4432 /* The number of named compound-literals generated thus far. */
4433 static GTY(()) int compound_literal_number;
4435 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4437 void
4438 set_compound_literal_name (tree decl)
4440 char *name;
4441 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4442 compound_literal_number);
4443 compound_literal_number++;
4444 DECL_NAME (decl) = get_identifier (name);
4447 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4448 TYPE and operand OP. */
4450 static tree
4451 build_va_arg_1 (location_t loc, tree type, tree op)
4453 tree expr = build1 (VA_ARG_EXPR, type, op);
4454 SET_EXPR_LOCATION (expr, loc);
4455 return expr;
4458 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4459 va_arg (EXPR, TYPE) at source location LOC. */
4461 tree
4462 build_va_arg (location_t loc, tree expr, tree type)
4464 tree va_type = TREE_TYPE (expr);
4465 tree canon_va_type = (va_type == error_mark_node
4466 ? error_mark_node
4467 : targetm.canonical_va_list_type (va_type));
4469 if (va_type == error_mark_node
4470 || canon_va_type == NULL_TREE)
4472 if (canon_va_type == NULL_TREE)
4473 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4475 /* Let's handle things neutrallly, if expr:
4476 - has undeclared type, or
4477 - is not an va_list type. */
4478 return build_va_arg_1 (loc, type, error_mark_node);
4481 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4483 /* Case 1: Not an array type. */
4485 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4486 type. */
4487 mark_addressable (expr);
4488 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4490 return build_va_arg_1 (loc, type, expr);
4493 /* Case 2: Array type.
4495 Background:
4497 For contrast, let's start with the simple case (case 1). If
4498 canon_va_type is not an array type, but say a char *, then when
4499 passing-by-value a va_list, the type of the va_list param decl is
4500 the same as for another va_list decl (all ap's are char *):
4502 f2_1 (char * ap)
4503 D.1815 = VA_ARG (&ap, 0B, 1);
4504 return D.1815;
4506 f2 (int i)
4507 char * ap.0;
4508 char * ap;
4509 __builtin_va_start (&ap, 0);
4510 ap.0 = ap;
4511 res = f2_1 (ap.0);
4512 __builtin_va_end (&ap);
4513 D.1812 = res;
4514 return D.1812;
4516 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4517 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4518 the same as for another va_list decl (case 2a, struct ap[1]).
4520 f2_1 (struct * ap)
4521 D.1844 = VA_ARG (ap, 0B, 0);
4522 return D.1844;
4524 f2 (int i)
4525 struct ap[1];
4526 __builtin_va_start (&ap, 0);
4527 res = f2_1 (&ap);
4528 __builtin_va_end (&ap);
4529 D.1841 = res;
4530 return D.1841;
4532 Case 2b is different because:
4533 - on the callee side, the parm decl has declared type va_list, but
4534 grokdeclarator changes the type of the parm decl to a pointer to the
4535 array elem type.
4536 - on the caller side, the pass-by-value uses &ap.
4538 We unify these two cases (case 2a: va_list is array type,
4539 case 2b: va_list is pointer to array elem type), by adding '&' for the
4540 array type case, such that we have a pointer to array elem in both
4541 cases. */
4543 if (TREE_CODE (va_type) == ARRAY_TYPE)
4545 /* Case 2a: va_list is array type. */
4547 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4548 elem type. */
4549 mark_addressable (expr);
4550 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4551 expr);
4553 /* Verify that &ap is still recognized as having va_list type. */
4554 tree canon_expr_type
4555 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4556 gcc_assert (canon_expr_type != NULL_TREE);
4558 else
4560 /* Case 2b: va_list is pointer to array elem type. */
4561 gcc_assert (POINTER_TYPE_P (va_type));
4563 /* Comparison as in std_canonical_va_list_type. */
4564 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4565 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4567 /* Don't take the address. We've already got '&ap'. */
4571 return build_va_arg_1 (loc, type, expr);
4575 /* Linked list of disabled built-in functions. */
4577 struct disabled_builtin
4579 const char *name;
4580 struct disabled_builtin *next;
4582 static disabled_builtin *disabled_builtins = NULL;
4584 static bool builtin_function_disabled_p (const char *);
4586 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4587 begins with "__builtin_", give an error. */
4589 void
4590 disable_builtin_function (const char *name)
4592 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4593 error ("cannot disable built-in function %qs", name);
4594 else
4596 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4597 new_disabled_builtin->name = name;
4598 new_disabled_builtin->next = disabled_builtins;
4599 disabled_builtins = new_disabled_builtin;
4604 /* Return true if the built-in function NAME has been disabled, false
4605 otherwise. */
4607 static bool
4608 builtin_function_disabled_p (const char *name)
4610 disabled_builtin *p;
4611 for (p = disabled_builtins; p != NULL; p = p->next)
4613 if (strcmp (name, p->name) == 0)
4614 return true;
4616 return false;
4620 /* Worker for DEF_BUILTIN.
4621 Possibly define a builtin function with one or two names.
4622 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4623 nonansi_p and flag_no_nonansi_builtin. */
4625 static void
4626 def_builtin_1 (enum built_in_function fncode,
4627 const char *name,
4628 enum built_in_class fnclass,
4629 tree fntype, tree libtype,
4630 bool both_p, bool fallback_p, bool nonansi_p,
4631 tree fnattrs, bool implicit_p)
4633 tree decl;
4634 const char *libname;
4636 if (fntype == error_mark_node)
4637 return;
4639 gcc_assert ((!both_p && !fallback_p)
4640 || !strncmp (name, "__builtin_",
4641 strlen ("__builtin_")));
4643 libname = name + strlen ("__builtin_");
4644 decl = add_builtin_function (name, fntype, fncode, fnclass,
4645 (fallback_p ? libname : NULL),
4646 fnattrs);
4648 set_builtin_decl (fncode, decl, implicit_p);
4650 if (both_p
4651 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4652 && !(nonansi_p && flag_no_nonansi_builtin))
4653 add_builtin_function (libname, libtype, fncode, fnclass,
4654 NULL, fnattrs);
4657 /* Nonzero if the type T promotes to int. This is (nearly) the
4658 integral promotions defined in ISO C99 6.3.1.1/2. */
4660 bool
4661 c_promoting_integer_type_p (const_tree t)
4663 switch (TREE_CODE (t))
4665 case INTEGER_TYPE:
4666 return (TYPE_MAIN_VARIANT (t) == char_type_node
4667 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4668 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4669 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4670 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4671 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4673 case ENUMERAL_TYPE:
4674 /* ??? Technically all enumerations not larger than an int
4675 promote to an int. But this is used along code paths
4676 that only want to notice a size change. */
4677 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4679 case BOOLEAN_TYPE:
4680 return true;
4682 default:
4683 return false;
4687 /* Return 1 if PARMS specifies a fixed number of parameters
4688 and none of their types is affected by default promotions. */
4690 bool
4691 self_promoting_args_p (const_tree parms)
4693 const_tree t;
4694 for (t = parms; t; t = TREE_CHAIN (t))
4696 tree type = TREE_VALUE (t);
4698 if (type == error_mark_node)
4699 continue;
4701 if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
4702 return false;
4704 if (type == NULL_TREE)
4705 return false;
4707 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4708 return false;
4710 if (c_promoting_integer_type_p (type))
4711 return false;
4713 return true;
4716 /* Recursively remove any '*' or '&' operator from TYPE. */
4717 tree
4718 strip_pointer_operator (tree t)
4720 while (POINTER_TYPE_P (t))
4721 t = TREE_TYPE (t);
4722 return t;
4725 /* Recursively remove pointer or array type from TYPE. */
4726 tree
4727 strip_pointer_or_array_types (tree t)
4729 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4730 t = TREE_TYPE (t);
4731 return t;
4734 /* Used to compare case labels. K1 and K2 are actually tree nodes
4735 representing case labels, or NULL_TREE for a `default' label.
4736 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4737 K2, and 0 if K1 and K2 are equal. */
4740 case_compare (splay_tree_key k1, splay_tree_key k2)
4742 /* Consider a NULL key (such as arises with a `default' label) to be
4743 smaller than anything else. */
4744 if (!k1)
4745 return k2 ? -1 : 0;
4746 else if (!k2)
4747 return k1 ? 1 : 0;
4749 return tree_int_cst_compare ((tree) k1, (tree) k2);
4752 /* Process a case label, located at LOC, for the range LOW_VALUE
4753 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4754 then this case label is actually a `default' label. If only
4755 HIGH_VALUE is NULL_TREE, then case label was declared using the
4756 usual C/C++ syntax, rather than the GNU case range extension.
4757 CASES is a tree containing all the case ranges processed so far;
4758 COND is the condition for the switch-statement itself.
4759 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4760 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4761 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4763 tree
4764 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4765 tree low_value, tree high_value, bool *outside_range_p)
4767 tree type;
4768 tree label;
4769 tree case_label;
4770 splay_tree_node node;
4772 /* Create the LABEL_DECL itself. */
4773 label = create_artificial_label (loc);
4775 /* If there was an error processing the switch condition, bail now
4776 before we get more confused. */
4777 if (!cond || cond == error_mark_node)
4778 goto error_out;
4780 if ((low_value && TREE_TYPE (low_value)
4781 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4782 || (high_value && TREE_TYPE (high_value)
4783 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4785 error_at (loc, "pointers are not permitted as case values");
4786 goto error_out;
4789 /* Case ranges are a GNU extension. */
4790 if (high_value)
4791 pedwarn (loc, OPT_Wpedantic,
4792 "range expressions in switch statements are non-standard");
4794 type = TREE_TYPE (cond);
4795 if (low_value)
4797 low_value = check_case_value (loc, low_value);
4798 low_value = convert_and_check (loc, type, low_value);
4799 if (low_value == error_mark_node)
4800 goto error_out;
4802 if (high_value)
4804 high_value = check_case_value (loc, high_value);
4805 high_value = convert_and_check (loc, type, high_value);
4806 if (high_value == error_mark_node)
4807 goto error_out;
4810 if (low_value && high_value)
4812 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4813 really a case range, even though it was written that way.
4814 Remove the HIGH_VALUE to simplify later processing. */
4815 if (tree_int_cst_equal (low_value, high_value))
4816 high_value = NULL_TREE;
4817 else if (!tree_int_cst_lt (low_value, high_value))
4818 warning_at (loc, 0, "empty range specified");
4821 /* See if the case is in range of the type of the original testing
4822 expression. If both low_value and high_value are out of range,
4823 don't insert the case label and return NULL_TREE. */
4824 if (low_value
4825 && !check_case_bounds (loc, type, orig_type,
4826 &low_value, high_value ? &high_value : NULL,
4827 outside_range_p))
4828 return NULL_TREE;
4830 /* Look up the LOW_VALUE in the table of case labels we already
4831 have. */
4832 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4833 /* If there was not an exact match, check for overlapping ranges.
4834 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4835 that's a `default' label and the only overlap is an exact match. */
4836 if (!node && (low_value || high_value))
4838 splay_tree_node low_bound;
4839 splay_tree_node high_bound;
4841 /* Even though there wasn't an exact match, there might be an
4842 overlap between this case range and another case range.
4843 Since we've (inductively) not allowed any overlapping case
4844 ranges, we simply need to find the greatest low case label
4845 that is smaller that LOW_VALUE, and the smallest low case
4846 label that is greater than LOW_VALUE. If there is an overlap
4847 it will occur in one of these two ranges. */
4848 low_bound = splay_tree_predecessor (cases,
4849 (splay_tree_key) low_value);
4850 high_bound = splay_tree_successor (cases,
4851 (splay_tree_key) low_value);
4853 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4854 the LOW_VALUE, so there is no need to check unless the
4855 LOW_BOUND is in fact itself a case range. */
4856 if (low_bound
4857 && CASE_HIGH ((tree) low_bound->value)
4858 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4859 low_value) >= 0)
4860 node = low_bound;
4861 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4862 range is bigger than the low end of the current range, so we
4863 are only interested if the current range is a real range, and
4864 not an ordinary case label. */
4865 else if (high_bound
4866 && high_value
4867 && (tree_int_cst_compare ((tree) high_bound->key,
4868 high_value)
4869 <= 0))
4870 node = high_bound;
4872 /* If there was an overlap, issue an error. */
4873 if (node)
4875 tree duplicate = CASE_LABEL ((tree) node->value);
4877 if (high_value)
4879 error_at (loc, "duplicate (or overlapping) case value");
4880 inform (DECL_SOURCE_LOCATION (duplicate),
4881 "this is the first entry overlapping that value");
4883 else if (low_value)
4885 error_at (loc, "duplicate case value") ;
4886 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4888 else
4890 error_at (loc, "multiple default labels in one switch");
4891 inform (DECL_SOURCE_LOCATION (duplicate),
4892 "this is the first default label");
4894 goto error_out;
4897 /* Add a CASE_LABEL to the statement-tree. */
4898 case_label = add_stmt (build_case_label (low_value, high_value, label));
4899 /* Register this case label in the splay tree. */
4900 splay_tree_insert (cases,
4901 (splay_tree_key) low_value,
4902 (splay_tree_value) case_label);
4904 return case_label;
4906 error_out:
4907 /* Add a label so that the back-end doesn't think that the beginning of
4908 the switch is unreachable. Note that we do not add a case label, as
4909 that just leads to duplicates and thence to failure later on. */
4910 if (!cases->root)
4912 tree t = create_artificial_label (loc);
4913 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4915 return error_mark_node;
4918 /* Subroutine of c_switch_covers_all_cases_p, called via
4919 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4920 ARGS[0] is initially NULL and after the first iteration is the
4921 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4922 type. */
4924 static int
4925 c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
4927 tree label = (tree) node->value;
4928 tree *args = (tree *) data;
4930 /* If there is a default case, we shouldn't have called this. */
4931 gcc_assert (CASE_LOW (label));
4933 if (args[0] == NULL_TREE)
4935 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
4936 return 1;
4938 else if (wi::add (wi::to_widest (args[0]), 1)
4939 != wi::to_widest (CASE_LOW (label)))
4940 return 1;
4941 if (CASE_HIGH (label))
4942 args[0] = CASE_HIGH (label);
4943 else
4944 args[0] = CASE_LOW (label);
4945 return 0;
4948 /* Return true if switch with CASES and switch condition with type
4949 covers all possible values in the case labels. */
4951 bool
4952 c_switch_covers_all_cases_p (splay_tree cases, tree type)
4954 /* If there is default:, this is always the case. */
4955 splay_tree_node default_node
4956 = splay_tree_lookup (cases, (splay_tree_key) NULL);
4957 if (default_node)
4958 return true;
4960 if (!INTEGRAL_TYPE_P (type))
4961 return false;
4963 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
4964 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
4965 return false;
4967 /* If there are no cases at all, or if the highest case label
4968 is smaller than TYPE_MAX_VALUE, return false. */
4969 if (args[0] == NULL_TREE
4970 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
4971 return false;
4973 return true;
4976 /* Finish an expression taking the address of LABEL (an
4977 IDENTIFIER_NODE). Returns an expression for the address.
4979 LOC is the location for the expression returned. */
4981 tree
4982 finish_label_address_expr (tree label, location_t loc)
4984 tree result;
4986 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
4988 if (label == error_mark_node)
4989 return error_mark_node;
4991 label = lookup_label (label);
4992 if (label == NULL_TREE)
4993 result = null_pointer_node;
4994 else
4996 TREE_USED (label) = 1;
4997 result = build1 (ADDR_EXPR, ptr_type_node, label);
4998 /* The current function is not necessarily uninlinable.
4999 Computed gotos are incompatible with inlining, but the value
5000 here could be used only in a diagnostic, for example. */
5001 protected_set_expr_location (result, loc);
5004 return result;
5008 /* Given a boolean expression ARG, return a tree representing an increment
5009 or decrement (as indicated by CODE) of ARG. The front end must check for
5010 invalid cases (e.g., decrement in C++). */
5011 tree
5012 boolean_increment (enum tree_code code, tree arg)
5014 tree val;
5015 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5017 arg = stabilize_reference (arg);
5018 switch (code)
5020 case PREINCREMENT_EXPR:
5021 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5022 break;
5023 case POSTINCREMENT_EXPR:
5024 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5025 arg = save_expr (arg);
5026 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5027 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5028 break;
5029 case PREDECREMENT_EXPR:
5030 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5031 invert_truthvalue_loc (input_location, arg));
5032 break;
5033 case POSTDECREMENT_EXPR:
5034 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5035 invert_truthvalue_loc (input_location, arg));
5036 arg = save_expr (arg);
5037 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5038 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5039 break;
5040 default:
5041 gcc_unreachable ();
5043 TREE_SIDE_EFFECTS (val) = 1;
5044 return val;
5047 /* Built-in macros for stddef.h and stdint.h, that require macros
5048 defined in this file. */
5049 void
5050 c_stddef_cpp_builtins(void)
5052 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5053 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5054 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5055 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5056 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5057 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5058 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5059 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5060 if (SIG_ATOMIC_TYPE)
5061 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5062 if (INT8_TYPE)
5063 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5064 if (INT16_TYPE)
5065 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5066 if (INT32_TYPE)
5067 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5068 if (INT64_TYPE)
5069 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5070 if (UINT8_TYPE)
5071 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5072 if (UINT16_TYPE)
5073 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5074 if (UINT32_TYPE)
5075 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5076 if (UINT64_TYPE)
5077 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5078 if (INT_LEAST8_TYPE)
5079 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5080 if (INT_LEAST16_TYPE)
5081 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5082 if (INT_LEAST32_TYPE)
5083 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5084 if (INT_LEAST64_TYPE)
5085 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5086 if (UINT_LEAST8_TYPE)
5087 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5088 if (UINT_LEAST16_TYPE)
5089 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5090 if (UINT_LEAST32_TYPE)
5091 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5092 if (UINT_LEAST64_TYPE)
5093 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5094 if (INT_FAST8_TYPE)
5095 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5096 if (INT_FAST16_TYPE)
5097 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5098 if (INT_FAST32_TYPE)
5099 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5100 if (INT_FAST64_TYPE)
5101 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5102 if (UINT_FAST8_TYPE)
5103 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5104 if (UINT_FAST16_TYPE)
5105 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5106 if (UINT_FAST32_TYPE)
5107 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5108 if (UINT_FAST64_TYPE)
5109 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5110 if (INTPTR_TYPE)
5111 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5112 if (UINTPTR_TYPE)
5113 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5116 static void
5117 c_init_attributes (void)
5119 /* Fill in the built_in_attributes array. */
5120 #define DEF_ATTR_NULL_TREE(ENUM) \
5121 built_in_attributes[(int) ENUM] = NULL_TREE;
5122 #define DEF_ATTR_INT(ENUM, VALUE) \
5123 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5124 #define DEF_ATTR_STRING(ENUM, VALUE) \
5125 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5126 #define DEF_ATTR_IDENT(ENUM, STRING) \
5127 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5128 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5129 built_in_attributes[(int) ENUM] \
5130 = tree_cons (built_in_attributes[(int) PURPOSE], \
5131 built_in_attributes[(int) VALUE], \
5132 built_in_attributes[(int) CHAIN]);
5133 #include "builtin-attrs.def"
5134 #undef DEF_ATTR_NULL_TREE
5135 #undef DEF_ATTR_INT
5136 #undef DEF_ATTR_IDENT
5137 #undef DEF_ATTR_TREE_LIST
5140 /* Check whether the byte alignment ALIGN is a valid user-specified
5141 alignment less than the supported maximum. If so, return ALIGN's
5142 base-2 log; if not, output an error and return -1. If OBJFILE
5143 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5144 converted to bits. Otherwise, consider valid only alignments
5145 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5146 Zero is not considered a valid argument (and results in -1 on
5147 return) but it only triggers a warning when WARN_ZERO is set. */
5150 check_user_alignment (const_tree align, bool objfile, bool warn_zero)
5152 if (error_operand_p (align))
5153 return -1;
5155 if (TREE_CODE (align) != INTEGER_CST
5156 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5158 error ("requested alignment is not an integer constant");
5159 return -1;
5162 if (integer_zerop (align))
5164 if (warn_zero)
5165 warning (OPT_Wattributes,
5166 "requested alignment %qE is not a positive power of 2",
5167 align);
5168 return -1;
5171 int log2bitalign;
5172 if (tree_int_cst_sgn (align) == -1
5173 || (log2bitalign = tree_log2 (align)) == -1)
5175 error ("requested alignment %qE is not a positive power of 2",
5176 align);
5177 return -1;
5180 if (objfile)
5182 unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5183 if (tree_to_shwi (align) > maxalign)
5185 error ("requested alignment %qE exceeds object file maximum %u",
5186 align, maxalign);
5187 return -1;
5191 if (log2bitalign >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5193 error ("requested alignment %qE exceeds maximum %u",
5194 align, 1U << (HOST_BITS_PER_INT - 1));
5195 return -1;
5198 return log2bitalign;
5201 /* Determine the ELF symbol visibility for DECL, which is either a
5202 variable or a function. It is an error to use this function if a
5203 definition of DECL is not available in this translation unit.
5204 Returns true if the final visibility has been determined by this
5205 function; false if the caller is free to make additional
5206 modifications. */
5208 bool
5209 c_determine_visibility (tree decl)
5211 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5213 /* If the user explicitly specified the visibility with an
5214 attribute, honor that. DECL_VISIBILITY will have been set during
5215 the processing of the attribute. We check for an explicit
5216 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5217 to distinguish the use of an attribute from the use of a "#pragma
5218 GCC visibility push(...)"; in the latter case we still want other
5219 considerations to be able to overrule the #pragma. */
5220 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5221 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5222 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5223 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5224 return true;
5226 /* Set default visibility to whatever the user supplied with
5227 visibility_specified depending on #pragma GCC visibility. */
5228 if (!DECL_VISIBILITY_SPECIFIED (decl))
5230 if (visibility_options.inpragma
5231 || DECL_VISIBILITY (decl) != default_visibility)
5233 DECL_VISIBILITY (decl) = default_visibility;
5234 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5235 /* If visibility changed and DECL already has DECL_RTL, ensure
5236 symbol flags are updated. */
5237 if (((VAR_P (decl) && TREE_STATIC (decl))
5238 || TREE_CODE (decl) == FUNCTION_DECL)
5239 && DECL_RTL_SET_P (decl))
5240 make_decl_rtl (decl);
5243 return false;
5246 /* Data to communicate through check_function_arguments_recurse between
5247 check_function_nonnull and check_nonnull_arg. */
5249 struct nonnull_arg_ctx
5251 location_t loc;
5252 bool warned_p;
5255 /* Check the argument list of a function call for null in argument slots
5256 that are marked as requiring a non-null pointer argument. The NARGS
5257 arguments are passed in the array ARGARRAY. Return true if we have
5258 warned. */
5260 static bool
5261 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5263 tree a;
5264 int i;
5266 attrs = lookup_attribute ("nonnull", attrs);
5267 if (attrs == NULL_TREE)
5268 return false;
5270 a = attrs;
5271 /* See if any of the nonnull attributes has no arguments. If so,
5272 then every pointer argument is checked (in which case the check
5273 for pointer type is done in check_nonnull_arg). */
5274 if (TREE_VALUE (a) != NULL_TREE)
5276 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5277 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5279 struct nonnull_arg_ctx ctx = { loc, false };
5280 if (a != NULL_TREE)
5281 for (i = 0; i < nargs; i++)
5282 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5283 i + 1);
5284 else
5286 /* Walk the argument list. If we encounter an argument number we
5287 should check for non-null, do it. */
5288 for (i = 0; i < nargs; i++)
5290 for (a = attrs; ; a = TREE_CHAIN (a))
5292 a = lookup_attribute ("nonnull", a);
5293 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5294 break;
5297 if (a != NULL_TREE)
5298 check_function_arguments_recurse (check_nonnull_arg, &ctx,
5299 argarray[i], i + 1);
5302 return ctx.warned_p;
5305 /* Check that the Nth argument of a function call (counting backwards
5306 from the end) is a (pointer)0. The NARGS arguments are passed in the
5307 array ARGARRAY. */
5309 static void
5310 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5312 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5314 if (attr)
5316 int len = 0;
5317 int pos = 0;
5318 tree sentinel;
5319 function_args_iterator iter;
5320 tree t;
5322 /* Skip over the named arguments. */
5323 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5325 if (len == nargs)
5326 break;
5327 len++;
5330 if (TREE_VALUE (attr))
5332 tree p = TREE_VALUE (TREE_VALUE (attr));
5333 pos = TREE_INT_CST_LOW (p);
5336 /* The sentinel must be one of the varargs, i.e.
5337 in position >= the number of fixed arguments. */
5338 if ((nargs - 1 - pos) < len)
5340 warning (OPT_Wformat_,
5341 "not enough variable arguments to fit a sentinel");
5342 return;
5345 /* Validate the sentinel. */
5346 sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
5347 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5348 || !integer_zerop (sentinel))
5349 /* Although __null (in C++) is only an integer we allow it
5350 nevertheless, as we are guaranteed that it's exactly
5351 as wide as a pointer, and we don't want to force
5352 users to cast the NULL they have written there.
5353 We warn with -Wstrict-null-sentinel, though. */
5354 && (warn_strict_null_sentinel || null_node != sentinel))
5355 warning (OPT_Wformat_, "missing sentinel in function call");
5359 /* Check that the same argument isn't passed to two or more
5360 restrict-qualified formal and issue a -Wrestrict warning
5361 if it is. Return true if a warning has been issued. */
5363 static bool
5364 check_function_restrict (const_tree fndecl, const_tree fntype,
5365 int nargs, tree *unfolded_argarray)
5367 int i;
5368 tree parms = TYPE_ARG_TYPES (fntype);
5370 /* Call fold_for_warn on all of the arguments. */
5371 auto_vec<tree> argarray (nargs);
5372 for (i = 0; i < nargs; i++)
5373 argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5375 if (fndecl
5376 && TREE_CODE (fndecl) == FUNCTION_DECL)
5378 /* Avoid diagnosing calls built-ins with a zero size/bound
5379 here. They are checked in more detail elsewhere. */
5380 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5381 && nargs == 3
5382 && TREE_CODE (argarray[2]) == INTEGER_CST
5383 && integer_zerop (argarray[2]))
5384 return false;
5386 if (DECL_ARGUMENTS (fndecl))
5387 parms = DECL_ARGUMENTS (fndecl);
5390 for (i = 0; i < nargs; i++)
5391 TREE_VISITED (argarray[i]) = 0;
5393 bool warned = false;
5395 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5397 tree type;
5398 if (TREE_CODE (parms) == PARM_DECL)
5400 type = TREE_TYPE (parms);
5401 parms = DECL_CHAIN (parms);
5403 else
5405 type = TREE_VALUE (parms);
5406 parms = TREE_CHAIN (parms);
5408 if (POINTER_TYPE_P (type)
5409 && TYPE_RESTRICT (type)
5410 && !TYPE_READONLY (TREE_TYPE (type)))
5411 warned |= warn_for_restrict (i, argarray.address (), nargs);
5414 for (i = 0; i < nargs; i++)
5415 TREE_VISITED (argarray[i]) = 0;
5417 return warned;
5420 /* Helper for check_function_nonnull; given a list of operands which
5421 must be non-null in ARGS, determine if operand PARAM_NUM should be
5422 checked. */
5424 static bool
5425 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5427 unsigned HOST_WIDE_INT arg_num = 0;
5429 for (; args; args = TREE_CHAIN (args))
5431 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5433 gcc_assert (found);
5435 if (arg_num == param_num)
5436 return true;
5438 return false;
5441 /* Check that the function argument PARAM (which is operand number
5442 PARAM_NUM) is non-null. This is called by check_function_nonnull
5443 via check_function_arguments_recurse. */
5445 static void
5446 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5448 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5450 /* Just skip checking the argument if it's not a pointer. This can
5451 happen if the "nonnull" attribute was given without an operand
5452 list (which means to check every pointer argument). */
5454 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5455 return;
5457 /* Diagnose the simple cases of null arguments. */
5458 if (integer_zerop (fold_for_warn (param)))
5460 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5461 "required (argument %lu)", (unsigned long) param_num);
5462 pctx->warned_p = true;
5466 /* Helper for nonnull attribute handling; fetch the operand number
5467 from the attribute argument list. */
5469 bool
5470 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5472 /* Verify the arg number is a small constant. */
5473 if (tree_fits_uhwi_p (arg_num_expr))
5475 *valp = tree_to_uhwi (arg_num_expr);
5476 return true;
5478 else
5479 return false;
5482 /* Arguments being collected for optimization. */
5483 typedef const char *const_char_p; /* For DEF_VEC_P. */
5484 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5487 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5488 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5489 false for #pragma GCC optimize. */
5491 bool
5492 parse_optimize_options (tree args, bool attr_p)
5494 bool ret = true;
5495 unsigned opt_argc;
5496 unsigned i;
5497 const char **opt_argv;
5498 struct cl_decoded_option *decoded_options;
5499 unsigned int decoded_options_count;
5500 tree ap;
5502 /* Build up argv vector. Just in case the string is stored away, use garbage
5503 collected strings. */
5504 vec_safe_truncate (optimize_args, 0);
5505 vec_safe_push (optimize_args, (const char *) NULL);
5507 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5509 tree value = TREE_VALUE (ap);
5511 if (TREE_CODE (value) == INTEGER_CST)
5513 char buffer[20];
5514 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5515 vec_safe_push (optimize_args, ggc_strdup (buffer));
5518 else if (TREE_CODE (value) == STRING_CST)
5520 /* Split string into multiple substrings. */
5521 size_t len = TREE_STRING_LENGTH (value);
5522 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5523 char *end = p + len;
5524 char *comma;
5525 char *next_p = p;
5527 while (next_p != NULL)
5529 size_t len2;
5530 char *q, *r;
5532 p = next_p;
5533 comma = strchr (p, ',');
5534 if (comma)
5536 len2 = comma - p;
5537 *comma = '\0';
5538 next_p = comma+1;
5540 else
5542 len2 = end - p;
5543 next_p = NULL;
5546 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5547 options. */
5548 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5550 ret = false;
5551 if (attr_p)
5552 warning (OPT_Wattributes,
5553 "bad option %qs to attribute %<optimize%>", p);
5554 else
5555 warning (OPT_Wpragmas,
5556 "bad option %qs to pragma %<optimize%>", p);
5557 continue;
5560 /* Can't use GC memory here, see PR88007. */
5561 r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
5563 if (*p != '-')
5565 *r++ = '-';
5567 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5568 itself is -Os, and any other switch begins with a -f. */
5569 if ((*p >= '0' && *p <= '9')
5570 || (p[0] == 's' && p[1] == '\0'))
5571 *r++ = 'O';
5572 else if (*p != 'O')
5573 *r++ = 'f';
5576 memcpy (r, p, len2);
5577 r[len2] = '\0';
5578 vec_safe_push (optimize_args, (const char *) q);
5584 opt_argc = optimize_args->length ();
5585 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5587 for (i = 1; i < opt_argc; i++)
5588 opt_argv[i] = (*optimize_args)[i];
5590 /* Now parse the options. */
5591 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5592 &decoded_options,
5593 &decoded_options_count);
5594 /* Drop non-Optimization options. */
5595 unsigned j = 1;
5596 for (i = 1; i < decoded_options_count; ++i)
5598 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5600 ret = false;
5601 if (attr_p)
5602 warning (OPT_Wattributes,
5603 "bad option %qs to attribute %<optimize%>",
5604 decoded_options[i].orig_option_with_args_text);
5605 else
5606 warning (OPT_Wpragmas,
5607 "bad option %qs to pragma %<optimize%>",
5608 decoded_options[i].orig_option_with_args_text);
5609 continue;
5611 if (i != j)
5612 decoded_options[j] = decoded_options[i];
5613 j++;
5615 decoded_options_count = j;
5616 /* And apply them. */
5617 decode_options (&global_options, &global_options_set,
5618 decoded_options, decoded_options_count,
5619 input_location, global_dc, NULL);
5621 targetm.override_options_after_change();
5623 optimize_args->truncate (0);
5624 return ret;
5627 /* Check whether ATTR is a valid attribute fallthrough. */
5629 bool
5630 attribute_fallthrough_p (tree attr)
5632 if (attr == error_mark_node)
5633 return false;
5634 tree t = lookup_attribute ("fallthrough", attr);
5635 if (t == NULL_TREE)
5636 return false;
5637 /* This attribute shall appear at most once in each attribute-list. */
5638 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5639 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5640 "times");
5641 /* No attribute-argument-clause shall be present. */
5642 else if (TREE_VALUE (t) != NULL_TREE)
5643 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5644 "a parameter");
5645 /* Warn if other attributes are found. */
5646 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5648 tree name = get_attribute_name (t);
5649 if (!is_attribute_p ("fallthrough", name))
5650 warning (OPT_Wattributes, "%qE attribute ignored", name);
5652 return true;
5656 /* Check for valid arguments being passed to a function with FNTYPE.
5657 There are NARGS arguments in the array ARGARRAY. LOC should be used
5658 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5659 been issued.
5661 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5662 to preserve location wrappers); checks that require folded arguments
5663 should call fold_for_warn on them. */
5665 bool
5666 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5667 int nargs, tree *argarray, vec<location_t> *arglocs)
5669 bool warned_p = false;
5671 /* Check for null being passed in a pointer argument that must be
5672 non-null. We also need to do this if format checking is enabled. */
5674 if (warn_nonnull)
5675 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5676 nargs, argarray);
5678 /* Check for errors in format strings. */
5680 if (warn_format || warn_suggest_attribute_format)
5681 check_function_format (fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray,
5682 arglocs);
5684 if (warn_format)
5685 check_function_sentinel (fntype, nargs, argarray);
5687 if (warn_restrict)
5688 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
5689 return warned_p;
5692 /* Generic argument checking recursion routine. PARAM is the argument to
5693 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5694 once the argument is resolved. CTX is context for the callback. */
5695 void
5696 check_function_arguments_recurse (void (*callback)
5697 (void *, tree, unsigned HOST_WIDE_INT),
5698 void *ctx, tree param,
5699 unsigned HOST_WIDE_INT param_num)
5701 if (CONVERT_EXPR_P (param)
5702 && (TYPE_PRECISION (TREE_TYPE (param))
5703 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5705 /* Strip coercion. */
5706 check_function_arguments_recurse (callback, ctx,
5707 TREE_OPERAND (param, 0), param_num);
5708 return;
5711 if (TREE_CODE (param) == CALL_EXPR)
5713 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5714 tree attrs;
5715 bool found_format_arg = false;
5717 /* See if this is a call to a known internationalization function
5718 that modifies a format arg. Such a function may have multiple
5719 format_arg attributes (for example, ngettext). */
5721 for (attrs = TYPE_ATTRIBUTES (type);
5722 attrs;
5723 attrs = TREE_CHAIN (attrs))
5724 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5726 tree inner_arg;
5727 tree format_num_expr;
5728 int format_num;
5729 int i;
5730 call_expr_arg_iterator iter;
5732 /* Extract the argument number, which was previously checked
5733 to be valid. */
5734 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5736 format_num = tree_to_uhwi (format_num_expr);
5738 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5739 inner_arg != NULL_TREE;
5740 inner_arg = next_call_expr_arg (&iter), i++)
5741 if (i == format_num)
5743 check_function_arguments_recurse (callback, ctx,
5744 inner_arg, param_num);
5745 found_format_arg = true;
5746 break;
5750 /* If we found a format_arg attribute and did a recursive check,
5751 we are done with checking this argument. Otherwise, we continue
5752 and this will be considered a non-literal. */
5753 if (found_format_arg)
5754 return;
5757 if (TREE_CODE (param) == COND_EXPR)
5759 /* Simplify to avoid warning for an impossible case. */
5760 param = fold_for_warn (param);
5761 if (TREE_CODE (param) == COND_EXPR)
5763 /* Check both halves of the conditional expression. */
5764 check_function_arguments_recurse (callback, ctx,
5765 TREE_OPERAND (param, 1),
5766 param_num);
5767 check_function_arguments_recurse (callback, ctx,
5768 TREE_OPERAND (param, 2),
5769 param_num);
5770 return;
5774 (*callback) (ctx, param, param_num);
5777 /* Checks for a builtin function FNDECL that the number of arguments
5778 NARGS against the required number REQUIRED and issues an error if
5779 there is a mismatch. Returns true if the number of arguments is
5780 correct, otherwise false. LOC is the location of FNDECL. */
5782 static bool
5783 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5784 int required)
5786 if (nargs < required)
5788 error_at (loc, "too few arguments to function %qE", fndecl);
5789 return false;
5791 else if (nargs > required)
5793 error_at (loc, "too many arguments to function %qE", fndecl);
5794 return false;
5796 return true;
5799 /* Helper macro for check_builtin_function_arguments. */
5800 #define ARG_LOCATION(N) \
5801 (arg_loc.is_empty () \
5802 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5803 : expansion_point_location (arg_loc[(N)]))
5805 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5806 Returns false if there was an error, otherwise true. LOC is the
5807 location of the function; ARG_LOC is a vector of locations of the
5808 arguments. */
5810 bool
5811 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5812 tree fndecl, int nargs, tree *args)
5814 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
5815 return true;
5817 switch (DECL_FUNCTION_CODE (fndecl))
5819 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5820 if (!tree_fits_uhwi_p (args[2]))
5822 error_at (ARG_LOCATION (2),
5823 "third argument to function %qE must be a constant integer",
5824 fndecl);
5825 return false;
5827 /* fall through */
5829 case BUILT_IN_ALLOCA_WITH_ALIGN:
5831 /* Get the requested alignment (in bits) if it's a constant
5832 integer expression. */
5833 unsigned HOST_WIDE_INT align
5834 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5836 /* Determine if the requested alignment is a power of 2. */
5837 if ((align & (align - 1)))
5838 align = 0;
5840 /* The maximum alignment in bits corresponding to the same
5841 maximum in bytes enforced in check_user_alignment(). */
5842 unsigned maxalign = (UINT_MAX >> 1) + 1;
5844 /* Reject invalid alignments. */
5845 if (align < BITS_PER_UNIT || maxalign < align)
5847 error_at (ARG_LOCATION (1),
5848 "second argument to function %qE must be a constant "
5849 "integer power of 2 between %qi and %qu bits",
5850 fndecl, BITS_PER_UNIT, maxalign);
5851 return false;
5853 return true;
5856 case BUILT_IN_CONSTANT_P:
5857 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5859 case BUILT_IN_ISFINITE:
5860 case BUILT_IN_ISINF:
5861 case BUILT_IN_ISINF_SIGN:
5862 case BUILT_IN_ISNAN:
5863 case BUILT_IN_ISNORMAL:
5864 case BUILT_IN_SIGNBIT:
5865 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5867 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5869 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5870 "call to function %qE", fndecl);
5871 return false;
5873 return true;
5875 return false;
5877 case BUILT_IN_ISGREATER:
5878 case BUILT_IN_ISGREATEREQUAL:
5879 case BUILT_IN_ISLESS:
5880 case BUILT_IN_ISLESSEQUAL:
5881 case BUILT_IN_ISLESSGREATER:
5882 case BUILT_IN_ISUNORDERED:
5883 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5885 enum tree_code code0, code1;
5886 code0 = TREE_CODE (TREE_TYPE (args[0]));
5887 code1 = TREE_CODE (TREE_TYPE (args[1]));
5888 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5889 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5890 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5892 error_at (loc, "non-floating-point arguments in call to "
5893 "function %qE", fndecl);
5894 return false;
5896 return true;
5898 return false;
5900 case BUILT_IN_FPCLASSIFY:
5901 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5903 for (unsigned int i = 0; i < 5; i++)
5904 if (TREE_CODE (args[i]) != INTEGER_CST)
5906 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5907 "call to function %qE", i + 1, fndecl);
5908 return false;
5911 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5913 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5914 "call to function %qE", fndecl);
5915 return false;
5917 return true;
5919 return false;
5921 case BUILT_IN_ASSUME_ALIGNED:
5922 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5924 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5926 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5927 "function %qE", fndecl);
5928 return false;
5930 return true;
5932 return false;
5934 case BUILT_IN_ADD_OVERFLOW:
5935 case BUILT_IN_SUB_OVERFLOW:
5936 case BUILT_IN_MUL_OVERFLOW:
5937 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5939 unsigned i;
5940 for (i = 0; i < 2; i++)
5941 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5943 error_at (ARG_LOCATION (i), "argument %u in call to function "
5944 "%qE does not have integral type", i + 1, fndecl);
5945 return false;
5947 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5948 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5950 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5951 "does not have pointer to integral type", fndecl);
5952 return false;
5954 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5956 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5957 "has pointer to enumerated type", fndecl);
5958 return false;
5960 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5962 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5963 "has pointer to boolean type", fndecl);
5964 return false;
5966 return true;
5968 return false;
5970 case BUILT_IN_ADD_OVERFLOW_P:
5971 case BUILT_IN_SUB_OVERFLOW_P:
5972 case BUILT_IN_MUL_OVERFLOW_P:
5973 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5975 unsigned i;
5976 for (i = 0; i < 3; i++)
5977 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5979 error_at (ARG_LOCATION (i), "argument %u in call to function "
5980 "%qE does not have integral type", i + 1, fndecl);
5981 return false;
5983 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5985 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5986 "%qE has enumerated type", fndecl);
5987 return false;
5989 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5991 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5992 "%qE has boolean type", fndecl);
5993 return false;
5995 return true;
5997 return false;
5999 default:
6000 return true;
6004 /* Subroutine of c_parse_error.
6005 Return the result of concatenating LHS and RHS. RHS is really
6006 a string literal, its first character is indicated by RHS_START and
6007 RHS_SIZE is its length (including the terminating NUL character).
6009 The caller is responsible for deleting the returned pointer. */
6011 static char *
6012 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6014 const size_t lhs_size = strlen (lhs);
6015 char *result = XNEWVEC (char, lhs_size + rhs_size);
6016 memcpy (result, lhs, lhs_size);
6017 memcpy (result + lhs_size, rhs_start, rhs_size);
6018 return result;
6021 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6022 before TOKEN, which had the associated VALUE. */
6024 void
6025 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6026 tree value, unsigned char token_flags,
6027 rich_location *richloc)
6029 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6031 char *message = NULL;
6033 if (token_type == CPP_EOF)
6034 message = catenate_messages (gmsgid, " at end of input");
6035 else if (token_type == CPP_CHAR
6036 || token_type == CPP_WCHAR
6037 || token_type == CPP_CHAR16
6038 || token_type == CPP_CHAR32
6039 || token_type == CPP_UTF8CHAR)
6041 unsigned int val = TREE_INT_CST_LOW (value);
6042 const char *prefix;
6044 switch (token_type)
6046 default:
6047 prefix = "";
6048 break;
6049 case CPP_WCHAR:
6050 prefix = "L";
6051 break;
6052 case CPP_CHAR16:
6053 prefix = "u";
6054 break;
6055 case CPP_CHAR32:
6056 prefix = "U";
6057 break;
6058 case CPP_UTF8CHAR:
6059 prefix = "u8";
6060 break;
6063 if (val <= UCHAR_MAX && ISGRAPH (val))
6064 message = catenate_messages (gmsgid, " before %s'%c'");
6065 else
6066 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6068 error_at (richloc, message, prefix, val);
6069 free (message);
6070 message = NULL;
6072 else if (token_type == CPP_CHAR_USERDEF
6073 || token_type == CPP_WCHAR_USERDEF
6074 || token_type == CPP_CHAR16_USERDEF
6075 || token_type == CPP_CHAR32_USERDEF
6076 || token_type == CPP_UTF8CHAR_USERDEF)
6077 message = catenate_messages (gmsgid,
6078 " before user-defined character literal");
6079 else if (token_type == CPP_STRING_USERDEF
6080 || token_type == CPP_WSTRING_USERDEF
6081 || token_type == CPP_STRING16_USERDEF
6082 || token_type == CPP_STRING32_USERDEF
6083 || token_type == CPP_UTF8STRING_USERDEF)
6084 message = catenate_messages (gmsgid, " before user-defined string literal");
6085 else if (token_type == CPP_STRING
6086 || token_type == CPP_WSTRING
6087 || token_type == CPP_STRING16
6088 || token_type == CPP_STRING32
6089 || token_type == CPP_UTF8STRING)
6090 message = catenate_messages (gmsgid, " before string constant");
6091 else if (token_type == CPP_NUMBER)
6092 message = catenate_messages (gmsgid, " before numeric constant");
6093 else if (token_type == CPP_NAME)
6095 message = catenate_messages (gmsgid, " before %qE");
6096 error_at (richloc, message, value);
6097 free (message);
6098 message = NULL;
6100 else if (token_type == CPP_PRAGMA)
6101 message = catenate_messages (gmsgid, " before %<#pragma%>");
6102 else if (token_type == CPP_PRAGMA_EOL)
6103 message = catenate_messages (gmsgid, " before end of line");
6104 else if (token_type == CPP_DECLTYPE)
6105 message = catenate_messages (gmsgid, " before %<decltype%>");
6106 else if (token_type < N_TTYPES)
6108 message = catenate_messages (gmsgid, " before %qs token");
6109 error_at (richloc, message, cpp_type2name (token_type, token_flags));
6110 free (message);
6111 message = NULL;
6113 else
6114 error_at (richloc, gmsgid);
6116 if (message)
6118 error_at (richloc, message);
6119 free (message);
6121 #undef catenate_messages
6124 /* Return the gcc option code associated with the reason for a cpp
6125 message, or 0 if none. */
6127 static int
6128 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
6130 const struct cpp_reason_option_codes_t *entry;
6132 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6134 if (entry->reason == reason)
6135 return entry->option_code;
6137 return 0;
6140 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6141 preprocessor. The diagnostic is of type LEVEL, with REASON set
6142 to the reason code if LEVEL is represents a warning, at location
6143 RICHLOC unless this is after lexing and the compiler's location
6144 should be used instead; MSG is the translated message and AP
6145 the arguments. Returns true if a diagnostic was emitted, false
6146 otherwise. */
6148 bool
6149 c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
6150 enum cpp_diagnostic_level level,
6151 enum cpp_warning_reason reason,
6152 rich_location *richloc,
6153 const char *msg, va_list *ap)
6155 diagnostic_info diagnostic;
6156 diagnostic_t dlevel;
6157 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6158 bool ret;
6160 switch (level)
6162 case CPP_DL_WARNING_SYSHDR:
6163 if (flag_no_output)
6164 return false;
6165 global_dc->dc_warn_system_headers = 1;
6166 /* Fall through. */
6167 case CPP_DL_WARNING:
6168 if (flag_no_output)
6169 return false;
6170 dlevel = DK_WARNING;
6171 break;
6172 case CPP_DL_PEDWARN:
6173 if (flag_no_output && !flag_pedantic_errors)
6174 return false;
6175 dlevel = DK_PEDWARN;
6176 break;
6177 case CPP_DL_ERROR:
6178 dlevel = DK_ERROR;
6179 break;
6180 case CPP_DL_ICE:
6181 dlevel = DK_ICE;
6182 break;
6183 case CPP_DL_NOTE:
6184 dlevel = DK_NOTE;
6185 break;
6186 case CPP_DL_FATAL:
6187 dlevel = DK_FATAL;
6188 break;
6189 default:
6190 gcc_unreachable ();
6192 if (done_lexing)
6193 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
6194 diagnostic_set_info_translated (&diagnostic, msg, ap,
6195 richloc, dlevel);
6196 diagnostic_override_option_index
6197 (&diagnostic,
6198 c_option_controlling_cpp_diagnostic (reason));
6199 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
6200 if (level == CPP_DL_WARNING_SYSHDR)
6201 global_dc->dc_warn_system_headers = save_warn_system_headers;
6202 return ret;
6205 /* Convert a character from the host to the target execution character
6206 set. cpplib handles this, mostly. */
6208 HOST_WIDE_INT
6209 c_common_to_target_charset (HOST_WIDE_INT c)
6211 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6212 zero-extended under -fno-signed-char. cpplib insists that characters
6213 and character constants are always unsigned. Hence we must convert
6214 back and forth. */
6215 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6217 uc = cpp_host_to_exec_charset (parse_in, uc);
6219 if (flag_signed_char)
6220 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6221 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6222 else
6223 return uc;
6226 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6227 references with an INDIRECT_REF of a constant at the bottom; much like the
6228 traditional rendering of offsetof as a macro. TYPE is the desired type of
6229 the whole expression. Return the folded result. */
6231 tree
6232 fold_offsetof (tree expr, tree type, enum tree_code ctx)
6234 tree base, off, t;
6235 tree_code code = TREE_CODE (expr);
6236 switch (code)
6238 case ERROR_MARK:
6239 return expr;
6241 case VAR_DECL:
6242 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6243 return error_mark_node;
6245 case CALL_EXPR:
6246 case TARGET_EXPR:
6247 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6248 return error_mark_node;
6250 case NOP_EXPR:
6251 case INDIRECT_REF:
6252 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6254 error ("cannot apply %<offsetof%> to a non constant address");
6255 return error_mark_node;
6257 return convert (type, TREE_OPERAND (expr, 0));
6259 case COMPONENT_REF:
6260 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6261 if (base == error_mark_node)
6262 return base;
6264 t = TREE_OPERAND (expr, 1);
6265 if (DECL_C_BIT_FIELD (t))
6267 error ("attempt to take address of bit-field structure "
6268 "member %qD", t);
6269 return error_mark_node;
6271 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6272 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6273 / BITS_PER_UNIT));
6274 break;
6276 case ARRAY_REF:
6277 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6278 if (base == error_mark_node)
6279 return base;
6281 t = TREE_OPERAND (expr, 1);
6283 /* Check if the offset goes beyond the upper bound of the array. */
6284 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6286 tree upbound = array_ref_up_bound (expr);
6287 if (upbound != NULL_TREE
6288 && TREE_CODE (upbound) == INTEGER_CST
6289 && !tree_int_cst_equal (upbound,
6290 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6292 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6293 upbound = size_binop (PLUS_EXPR, upbound,
6294 build_int_cst (TREE_TYPE (upbound), 1));
6295 if (tree_int_cst_lt (upbound, t))
6297 tree v;
6299 for (v = TREE_OPERAND (expr, 0);
6300 TREE_CODE (v) == COMPONENT_REF;
6301 v = TREE_OPERAND (v, 0))
6302 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6303 == RECORD_TYPE)
6305 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6306 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6307 if (TREE_CODE (fld_chain) == FIELD_DECL)
6308 break;
6310 if (fld_chain)
6311 break;
6313 /* Don't warn if the array might be considered a poor
6314 man's flexible array member with a very permissive
6315 definition thereof. */
6316 if (TREE_CODE (v) == ARRAY_REF
6317 || TREE_CODE (v) == COMPONENT_REF)
6318 warning (OPT_Warray_bounds,
6319 "index %E denotes an offset "
6320 "greater than size of %qT",
6321 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6326 t = convert (sizetype, t);
6327 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6328 break;
6330 case COMPOUND_EXPR:
6331 /* Handle static members of volatile structs. */
6332 t = TREE_OPERAND (expr, 1);
6333 gcc_checking_assert (VAR_P (get_base_address (t)));
6334 return fold_offsetof (t, type);
6336 default:
6337 gcc_unreachable ();
6340 if (!POINTER_TYPE_P (type))
6341 return size_binop (PLUS_EXPR, base, convert (type, off));
6342 return fold_build_pointer_plus (base, off);
6345 /* *PTYPE is an incomplete array. Complete it with a domain based on
6346 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6347 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6348 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6351 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6353 tree maxindex, type, main_type, elt, unqual_elt;
6354 int failure = 0, quals;
6355 bool overflow_p = false;
6357 maxindex = size_zero_node;
6358 if (initial_value)
6360 if (TREE_CODE (initial_value) == STRING_CST)
6362 int eltsize
6363 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6364 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6366 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6368 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6370 if (vec_safe_is_empty (v))
6372 if (pedantic)
6373 failure = 3;
6374 maxindex = ssize_int (-1);
6376 else
6378 tree curindex;
6379 unsigned HOST_WIDE_INT cnt;
6380 constructor_elt *ce;
6381 bool fold_p = false;
6383 if ((*v)[0].index)
6384 maxindex = (*v)[0].index, fold_p = true;
6386 curindex = maxindex;
6388 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6390 bool curfold_p = false;
6391 if (ce->index)
6392 curindex = ce->index, curfold_p = true;
6393 else
6395 if (fold_p)
6397 /* Since we treat size types now as ordinary
6398 unsigned types, we need an explicit overflow
6399 check. */
6400 tree orig = curindex;
6401 curindex = fold_convert (sizetype, curindex);
6402 overflow_p |= tree_int_cst_lt (curindex, orig);
6404 curindex = size_binop (PLUS_EXPR, curindex,
6405 size_one_node);
6407 if (tree_int_cst_lt (maxindex, curindex))
6408 maxindex = curindex, fold_p = curfold_p;
6410 if (fold_p)
6412 tree orig = maxindex;
6413 maxindex = fold_convert (sizetype, maxindex);
6414 overflow_p |= tree_int_cst_lt (maxindex, orig);
6418 else
6420 /* Make an error message unless that happened already. */
6421 if (initial_value != error_mark_node)
6422 failure = 1;
6425 else
6427 failure = 2;
6428 if (!do_default)
6429 return failure;
6432 type = *ptype;
6433 elt = TREE_TYPE (type);
6434 quals = TYPE_QUALS (strip_array_types (elt));
6435 if (quals == 0)
6436 unqual_elt = elt;
6437 else
6438 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6440 /* Using build_distinct_type_copy and modifying things afterward instead
6441 of using build_array_type to create a new type preserves all of the
6442 TYPE_LANG_FLAG_? bits that the front end may have set. */
6443 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6444 TREE_TYPE (main_type) = unqual_elt;
6445 TYPE_DOMAIN (main_type)
6446 = build_range_type (TREE_TYPE (maxindex),
6447 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6448 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
6449 layout_type (main_type);
6451 /* Make sure we have the canonical MAIN_TYPE. */
6452 hashval_t hashcode = type_hash_canon_hash (main_type);
6453 main_type = type_hash_canon (hashcode, main_type);
6455 /* Fix the canonical type. */
6456 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6457 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6458 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6459 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6460 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6461 != TYPE_DOMAIN (main_type)))
6462 TYPE_CANONICAL (main_type)
6463 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6464 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6465 TYPE_TYPELESS_STORAGE (main_type));
6466 else
6467 TYPE_CANONICAL (main_type) = main_type;
6469 if (quals == 0)
6470 type = main_type;
6471 else
6472 type = c_build_qualified_type (main_type, quals);
6474 if (COMPLETE_TYPE_P (type)
6475 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6476 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6478 error ("size of array is too large");
6479 /* If we proceed with the array type as it is, we'll eventually
6480 crash in tree_to_[su]hwi(). */
6481 type = error_mark_node;
6484 *ptype = type;
6485 return failure;
6488 /* INIT is an constructor of a structure with a flexible array member.
6489 Complete the flexible array member with a domain based on it's value. */
6490 void
6491 complete_flexible_array_elts (tree init)
6493 tree elt, type;
6495 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
6496 return;
6498 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
6499 return;
6501 elt = CONSTRUCTOR_ELTS (init)->last ().value;
6502 type = TREE_TYPE (elt);
6503 if (TREE_CODE (type) == ARRAY_TYPE
6504 && TYPE_SIZE (type) == NULL_TREE)
6505 complete_array_type (&TREE_TYPE (elt), elt, false);
6506 else
6507 complete_flexible_array_elts (elt);
6510 /* Like c_mark_addressable but don't check register qualifier. */
6511 void
6512 c_common_mark_addressable_vec (tree t)
6514 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6515 t = C_MAYBE_CONST_EXPR_EXPR (t);
6516 while (handled_component_p (t))
6517 t = TREE_OPERAND (t, 0);
6518 if (!VAR_P (t)
6519 && TREE_CODE (t) != PARM_DECL
6520 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6521 return;
6522 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6523 TREE_ADDRESSABLE (t) = 1;
6528 /* Used to help initialize the builtin-types.def table. When a type of
6529 the correct size doesn't exist, use error_mark_node instead of NULL.
6530 The later results in segfaults even when a decl using the type doesn't
6531 get invoked. */
6533 tree
6534 builtin_type_for_size (int size, bool unsignedp)
6536 tree type = c_common_type_for_size (size, unsignedp);
6537 return type ? type : error_mark_node;
6540 /* Work out the size of the first argument of a call to
6541 __builtin_speculation_safe_value. Only pointers and integral types
6542 are permitted. Return -1 if the argument type is not supported or
6543 the size is too large; 0 if the argument type is a pointer or the
6544 size if it is integral. */
6545 static enum built_in_function
6546 speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params)
6548 /* Type of the argument. */
6549 tree type;
6550 int size;
6552 if (vec_safe_is_empty (params))
6554 error ("too few arguments to function %qE", function);
6555 return BUILT_IN_NONE;
6558 type = TREE_TYPE ((*params)[0]);
6559 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6561 /* Force array-to-pointer decay for C++. */
6562 (*params)[0] = default_conversion ((*params)[0]);
6563 type = TREE_TYPE ((*params)[0]);
6566 if (POINTER_TYPE_P (type))
6567 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
6569 if (!INTEGRAL_TYPE_P (type))
6570 goto incompatible;
6572 if (!COMPLETE_TYPE_P (type))
6573 goto incompatible;
6575 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6576 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6577 return ((enum built_in_function)
6578 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
6580 incompatible:
6581 /* Issue the diagnostic only if the argument is valid, otherwise
6582 it would be redundant at best and could be misleading. */
6583 if (type != error_mark_node)
6584 error ("operand type %qT is incompatible with argument %d of %qE",
6585 type, 1, function);
6587 return BUILT_IN_NONE;
6590 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6591 the prototype for FUNCTION. The first argument is mandatory, a second
6592 argument, if present, must be type compatible with the first. */
6593 static bool
6594 speculation_safe_value_resolve_params (location_t loc, tree orig_function,
6595 vec<tree, va_gc> *params)
6597 tree val;
6599 if (params->length () == 0)
6601 error_at (loc, "too few arguments to function %qE", orig_function);
6602 return false;
6605 else if (params->length () > 2)
6607 error_at (loc, "too many arguments to function %qE", orig_function);
6608 return false;
6611 val = (*params)[0];
6612 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
6613 val = default_conversion (val);
6614 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
6615 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
6617 error_at (loc,
6618 "expecting argument of type pointer or of type integer "
6619 "for argument 1");
6620 return false;
6622 (*params)[0] = val;
6624 if (params->length () == 2)
6626 tree val2 = (*params)[1];
6627 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
6628 val2 = default_conversion (val2);
6629 if (!(TREE_TYPE (val) == TREE_TYPE (val2)
6630 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
6632 error_at (loc, "both arguments must be compatible");
6633 return false;
6635 (*params)[1] = val2;
6638 return true;
6641 /* Cast the result of the builtin back to the type of the first argument,
6642 preserving any qualifiers that it might have. */
6643 static tree
6644 speculation_safe_value_resolve_return (tree first_param, tree result)
6646 tree ptype = TREE_TYPE (first_param);
6647 tree rtype = TREE_TYPE (result);
6648 ptype = TYPE_MAIN_VARIANT (ptype);
6650 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6651 return convert (ptype, result);
6653 return result;
6656 /* A helper function for resolve_overloaded_builtin in resolving the
6657 overloaded __sync_ builtins. Returns a positive power of 2 if the
6658 first operand of PARAMS is a pointer to a supported data type.
6659 Returns 0 if an error is encountered.
6660 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6661 built-ins. */
6663 static int
6664 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6666 /* Type of the argument. */
6667 tree argtype;
6668 /* Type the argument points to. */
6669 tree type;
6670 int size;
6672 if (vec_safe_is_empty (params))
6674 error ("too few arguments to function %qE", function);
6675 return 0;
6678 argtype = type = TREE_TYPE ((*params)[0]);
6679 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6681 /* Force array-to-pointer decay for C++. */
6682 (*params)[0] = default_conversion ((*params)[0]);
6683 type = TREE_TYPE ((*params)[0]);
6685 if (TREE_CODE (type) != POINTER_TYPE)
6686 goto incompatible;
6688 type = TREE_TYPE (type);
6689 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6690 goto incompatible;
6692 if (!COMPLETE_TYPE_P (type))
6693 goto incompatible;
6695 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6696 goto incompatible;
6698 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6699 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6700 return size;
6702 incompatible:
6703 /* Issue the diagnostic only if the argument is valid, otherwise
6704 it would be redundant at best and could be misleading. */
6705 if (argtype != error_mark_node)
6706 error ("operand type %qT is incompatible with argument %d of %qE",
6707 argtype, 1, function);
6708 return 0;
6711 /* A helper function for resolve_overloaded_builtin. Adds casts to
6712 PARAMS to make arguments match up with those of FUNCTION. Drops
6713 the variadic arguments at the end. Returns false if some error
6714 was encountered; true on success. */
6716 static bool
6717 sync_resolve_params (location_t loc, tree orig_function, tree function,
6718 vec<tree, va_gc> *params, bool orig_format)
6720 function_args_iterator iter;
6721 tree ptype;
6722 unsigned int parmnum;
6724 function_args_iter_init (&iter, TREE_TYPE (function));
6725 /* We've declared the implementation functions to use "volatile void *"
6726 as the pointer parameter, so we shouldn't get any complaints from the
6727 call to check_function_arguments what ever type the user used. */
6728 function_args_iter_next (&iter);
6729 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6730 ptype = TYPE_MAIN_VARIANT (ptype);
6732 /* For the rest of the values, we need to cast these to FTYPE, so that we
6733 don't get warnings for passing pointer types, etc. */
6734 parmnum = 0;
6735 while (1)
6737 tree val, arg_type;
6739 arg_type = function_args_iter_cond (&iter);
6740 /* XXX void_type_node belies the abstraction. */
6741 if (arg_type == void_type_node)
6742 break;
6744 ++parmnum;
6745 if (params->length () <= parmnum)
6747 error_at (loc, "too few arguments to function %qE", orig_function);
6748 return false;
6751 /* Only convert parameters if arg_type is unsigned integer type with
6752 new format sync routines, i.e. don't attempt to convert pointer
6753 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6754 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6755 kinds). */
6756 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6758 /* Ideally for the first conversion we'd use convert_for_assignment
6759 so that we get warnings for anything that doesn't match the pointer
6760 type. This isn't portable across the C and C++ front ends atm. */
6761 val = (*params)[parmnum];
6762 val = convert (ptype, val);
6763 val = convert (arg_type, val);
6764 (*params)[parmnum] = val;
6767 function_args_iter_next (&iter);
6770 /* __atomic routines are not variadic. */
6771 if (!orig_format && params->length () != parmnum + 1)
6773 error_at (loc, "too many arguments to function %qE", orig_function);
6774 return false;
6777 /* The definition of these primitives is variadic, with the remaining
6778 being "an optional list of variables protected by the memory barrier".
6779 No clue what that's supposed to mean, precisely, but we consider all
6780 call-clobbered variables to be protected so we're safe. */
6781 params->truncate (parmnum + 1);
6783 return true;
6786 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6787 RESULT to make it match the type of the first pointer argument in
6788 PARAMS. */
6790 static tree
6791 sync_resolve_return (tree first_param, tree result, bool orig_format)
6793 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6794 tree rtype = TREE_TYPE (result);
6795 ptype = TYPE_MAIN_VARIANT (ptype);
6797 /* New format doesn't require casting unless the types are the same size. */
6798 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6799 return convert (ptype, result);
6800 else
6801 return result;
6804 /* This function verifies the PARAMS to generic atomic FUNCTION.
6805 It returns the size if all the parameters are the same size, otherwise
6806 0 is returned if the parameters are invalid. */
6808 static int
6809 get_atomic_generic_size (location_t loc, tree function,
6810 vec<tree, va_gc> *params)
6812 unsigned int n_param;
6813 unsigned int n_model;
6814 unsigned int x;
6815 int size_0;
6816 tree type_0;
6818 /* Determine the parameter makeup. */
6819 switch (DECL_FUNCTION_CODE (function))
6821 case BUILT_IN_ATOMIC_EXCHANGE:
6822 n_param = 4;
6823 n_model = 1;
6824 break;
6825 case BUILT_IN_ATOMIC_LOAD:
6826 case BUILT_IN_ATOMIC_STORE:
6827 n_param = 3;
6828 n_model = 1;
6829 break;
6830 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6831 n_param = 6;
6832 n_model = 2;
6833 break;
6834 default:
6835 gcc_unreachable ();
6838 if (vec_safe_length (params) != n_param)
6840 error_at (loc, "incorrect number of arguments to function %qE", function);
6841 return 0;
6844 /* Get type of first parameter, and determine its size. */
6845 type_0 = TREE_TYPE ((*params)[0]);
6846 if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
6848 /* Force array-to-pointer decay for C++. */
6849 (*params)[0] = default_conversion ((*params)[0]);
6850 type_0 = TREE_TYPE ((*params)[0]);
6852 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6854 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6855 function);
6856 return 0;
6859 /* Types must be compile time constant sizes. */
6860 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6862 error_at (loc,
6863 "argument 1 of %qE must be a pointer to a constant size type",
6864 function);
6865 return 0;
6868 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6870 /* Zero size objects are not allowed. */
6871 if (size_0 == 0)
6873 error_at (loc,
6874 "argument 1 of %qE must be a pointer to a nonzero size object",
6875 function);
6876 return 0;
6879 /* Check each other parameter is a pointer and the same size. */
6880 for (x = 0; x < n_param - n_model; x++)
6882 int size;
6883 tree type = TREE_TYPE ((*params)[x]);
6884 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6885 if (n_param == 6 && x == 3)
6886 continue;
6887 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6889 /* Force array-to-pointer decay for C++. */
6890 (*params)[x] = default_conversion ((*params)[x]);
6891 type = TREE_TYPE ((*params)[x]);
6893 if (!POINTER_TYPE_P (type))
6895 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6896 function);
6897 return 0;
6899 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6900 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6901 != INTEGER_CST)
6903 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6904 "size type", x + 1, function);
6905 return 0;
6907 else if (FUNCTION_POINTER_TYPE_P (type))
6909 error_at (loc, "argument %d of %qE must not be a pointer to a "
6910 "function", x + 1, function);
6911 return 0;
6913 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6914 size = type_size ? tree_to_uhwi (type_size) : 0;
6915 if (size != size_0)
6917 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6918 function);
6919 return 0;
6923 /* Check memory model parameters for validity. */
6924 for (x = n_param - n_model ; x < n_param; x++)
6926 tree p = (*params)[x];
6927 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6929 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6930 function);
6931 return 0;
6933 p = fold_for_warn (p);
6934 if (TREE_CODE (p) == INTEGER_CST)
6936 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6937 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6938 bits will be checked later during expansion in target specific
6939 way. */
6940 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6941 warning_at (loc, OPT_Winvalid_memory_model,
6942 "invalid memory model argument %d of %qE", x + 1,
6943 function);
6947 return size_0;
6951 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6952 at the beginning of the parameter list PARAMS representing the size of the
6953 objects. This is to match the library ABI requirement. LOC is the location
6954 of the function call.
6955 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6956 returned to allow the external call to be constructed. */
6958 static tree
6959 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6960 vec<tree, va_gc> *params)
6962 tree size_node;
6964 /* Insert a SIZE_T parameter as the first param. If there isn't
6965 enough space, allocate a new vector and recursively re-build with that. */
6966 if (!params->space (1))
6968 unsigned int z, len;
6969 vec<tree, va_gc> *v;
6970 tree f;
6972 len = params->length ();
6973 vec_alloc (v, len + 1);
6974 v->quick_push (build_int_cst (size_type_node, n));
6975 for (z = 0; z < len; z++)
6976 v->quick_push ((*params)[z]);
6977 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6978 vec_free (v);
6979 return f;
6982 /* Add the size parameter and leave as a function call for processing. */
6983 size_node = build_int_cst (size_type_node, n);
6984 params->quick_insert (0, size_node);
6985 return NULL_TREE;
6989 /* Return whether atomic operations for naturally aligned N-byte
6990 arguments are supported, whether inline or through libatomic. */
6991 static bool
6992 atomic_size_supported_p (int n)
6994 switch (n)
6996 case 1:
6997 case 2:
6998 case 4:
6999 case 8:
7000 return true;
7002 case 16:
7003 return targetm.scalar_mode_supported_p (TImode);
7005 default:
7006 return false;
7010 /* This will process an __atomic_exchange function call, determine whether it
7011 needs to be mapped to the _N variation, or turned into a library call.
7012 LOC is the location of the builtin call.
7013 FUNCTION is the DECL that has been invoked;
7014 PARAMS is the argument list for the call. The return value is non-null
7015 TRUE is returned if it is translated into the proper format for a call to the
7016 external library, and NEW_RETURN is set the tree for that function.
7017 FALSE is returned if processing for the _N variation is required, and
7018 NEW_RETURN is set to the return value the result is copied into. */
7019 static bool
7020 resolve_overloaded_atomic_exchange (location_t loc, tree function,
7021 vec<tree, va_gc> *params, tree *new_return)
7023 tree p0, p1, p2, p3;
7024 tree I_type, I_type_ptr;
7025 int n = get_atomic_generic_size (loc, function, params);
7027 /* Size of 0 is an error condition. */
7028 if (n == 0)
7030 *new_return = error_mark_node;
7031 return true;
7034 /* If not a lock-free size, change to the library generic format. */
7035 if (!atomic_size_supported_p (n))
7037 *new_return = add_atomic_size_parameter (n, loc, function, params);
7038 return true;
7041 /* Otherwise there is a lockfree match, transform the call from:
7042 void fn(T* mem, T* desired, T* return, model)
7043 into
7044 *return = (T) (fn (In* mem, (In) *desired, model)) */
7046 p0 = (*params)[0];
7047 p1 = (*params)[1];
7048 p2 = (*params)[2];
7049 p3 = (*params)[3];
7051 /* Create pointer to appropriate size. */
7052 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7053 I_type_ptr = build_pointer_type (I_type);
7055 /* Convert object pointer to required type. */
7056 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7057 (*params)[0] = p0;
7058 /* Convert new value to required type, and dereference it. */
7059 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7060 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7061 (*params)[1] = p1;
7063 /* Move memory model to the 3rd position, and end param list. */
7064 (*params)[2] = p3;
7065 params->truncate (3);
7067 /* Convert return pointer and dereference it for later assignment. */
7068 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7070 return false;
7074 /* This will process an __atomic_compare_exchange function call, determine
7075 whether it needs to be mapped to the _N variation, or turned into a lib call.
7076 LOC is the location of the builtin call.
7077 FUNCTION is the DECL that has been invoked;
7078 PARAMS is the argument list for the call. The return value is non-null
7079 TRUE is returned if it is translated into the proper format for a call to the
7080 external library, and NEW_RETURN is set the tree for that function.
7081 FALSE is returned if processing for the _N variation is required. */
7083 static bool
7084 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
7085 vec<tree, va_gc> *params,
7086 tree *new_return)
7088 tree p0, p1, p2;
7089 tree I_type, I_type_ptr;
7090 int n = get_atomic_generic_size (loc, function, params);
7092 /* Size of 0 is an error condition. */
7093 if (n == 0)
7095 *new_return = error_mark_node;
7096 return true;
7099 /* If not a lock-free size, change to the library generic format. */
7100 if (!atomic_size_supported_p (n))
7102 /* The library generic format does not have the weak parameter, so
7103 remove it from the param list. Since a parameter has been removed,
7104 we can be sure that there is room for the SIZE_T parameter, meaning
7105 there will not be a recursive rebuilding of the parameter list, so
7106 there is no danger this will be done twice. */
7107 if (n > 0)
7109 (*params)[3] = (*params)[4];
7110 (*params)[4] = (*params)[5];
7111 params->truncate (5);
7113 *new_return = add_atomic_size_parameter (n, loc, function, params);
7114 return true;
7117 /* Otherwise, there is a match, so the call needs to be transformed from:
7118 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7119 into
7120 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7122 p0 = (*params)[0];
7123 p1 = (*params)[1];
7124 p2 = (*params)[2];
7126 /* Create pointer to appropriate size. */
7127 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7128 I_type_ptr = build_pointer_type (I_type);
7130 /* Convert object pointer to required type. */
7131 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7132 (*params)[0] = p0;
7134 /* Convert expected pointer to required type. */
7135 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
7136 (*params)[1] = p1;
7138 /* Convert desired value to required type, and dereference it. */
7139 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7140 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
7141 (*params)[2] = p2;
7143 /* The rest of the parameters are fine. NULL means no special return value
7144 processing.*/
7145 *new_return = NULL;
7146 return false;
7150 /* This will process an __atomic_load function call, determine whether it
7151 needs to be mapped to the _N variation, or turned into a library call.
7152 LOC is the location of the builtin call.
7153 FUNCTION is the DECL that has been invoked;
7154 PARAMS is the argument list for the call. The return value is non-null
7155 TRUE is returned if it is translated into the proper format for a call to the
7156 external library, and NEW_RETURN is set the tree for that function.
7157 FALSE is returned if processing for the _N variation is required, and
7158 NEW_RETURN is set to the return value the result is copied into. */
7160 static bool
7161 resolve_overloaded_atomic_load (location_t loc, tree function,
7162 vec<tree, va_gc> *params, tree *new_return)
7164 tree p0, p1, p2;
7165 tree I_type, I_type_ptr;
7166 int n = get_atomic_generic_size (loc, function, params);
7168 /* Size of 0 is an error condition. */
7169 if (n == 0)
7171 *new_return = error_mark_node;
7172 return true;
7175 /* If not a lock-free size, change to the library generic format. */
7176 if (!atomic_size_supported_p (n))
7178 *new_return = add_atomic_size_parameter (n, loc, function, params);
7179 return true;
7182 /* Otherwise, there is a match, so the call needs to be transformed from:
7183 void fn(T* mem, T* return, model)
7184 into
7185 *return = (T) (fn ((In *) mem, model)) */
7187 p0 = (*params)[0];
7188 p1 = (*params)[1];
7189 p2 = (*params)[2];
7191 /* Create pointer to appropriate size. */
7192 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7193 I_type_ptr = build_pointer_type (I_type);
7195 /* Convert object pointer to required type. */
7196 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7197 (*params)[0] = p0;
7199 /* Move memory model to the 2nd position, and end param list. */
7200 (*params)[1] = p2;
7201 params->truncate (2);
7203 /* Convert return pointer and dereference it for later assignment. */
7204 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7206 return false;
7210 /* This will process an __atomic_store function call, determine whether it
7211 needs to be mapped to the _N variation, or turned into a library call.
7212 LOC is the location of the builtin call.
7213 FUNCTION is the DECL that has been invoked;
7214 PARAMS is the argument list for the call. The return value is non-null
7215 TRUE is returned if it is translated into the proper format for a call to the
7216 external library, and NEW_RETURN is set the tree for that function.
7217 FALSE is returned if processing for the _N variation is required, and
7218 NEW_RETURN is set to the return value the result is copied into. */
7220 static bool
7221 resolve_overloaded_atomic_store (location_t loc, tree function,
7222 vec<tree, va_gc> *params, tree *new_return)
7224 tree p0, p1;
7225 tree I_type, I_type_ptr;
7226 int n = get_atomic_generic_size (loc, function, params);
7228 /* Size of 0 is an error condition. */
7229 if (n == 0)
7231 *new_return = error_mark_node;
7232 return true;
7235 /* If not a lock-free size, change to the library generic format. */
7236 if (!atomic_size_supported_p (n))
7238 *new_return = add_atomic_size_parameter (n, loc, function, params);
7239 return true;
7242 /* Otherwise, there is a match, so the call needs to be transformed from:
7243 void fn(T* mem, T* value, model)
7244 into
7245 fn ((In *) mem, (In) *value, model) */
7247 p0 = (*params)[0];
7248 p1 = (*params)[1];
7250 /* Create pointer to appropriate size. */
7251 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7252 I_type_ptr = build_pointer_type (I_type);
7254 /* Convert object pointer to required type. */
7255 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7256 (*params)[0] = p0;
7258 /* Convert new value to required type, and dereference it. */
7259 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7260 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7261 (*params)[1] = p1;
7263 /* The memory model is in the right spot already. Return is void. */
7264 *new_return = NULL_TREE;
7266 return false;
7270 /* Some builtin functions are placeholders for other expressions. This
7271 function should be called immediately after parsing the call expression
7272 before surrounding code has committed to the type of the expression.
7274 LOC is the location of the builtin call.
7276 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7277 PARAMS is the argument list for the call. The return value is non-null
7278 when expansion is complete, and null if normal processing should
7279 continue. */
7281 tree
7282 resolve_overloaded_builtin (location_t loc, tree function,
7283 vec<tree, va_gc> *params)
7285 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7287 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7288 Those are not valid to call with a pointer to _Bool (or C++ bool)
7289 and so must be rejected. */
7290 bool fetch_op = true;
7291 bool orig_format = true;
7292 tree new_return = NULL_TREE;
7294 switch (DECL_BUILT_IN_CLASS (function))
7296 case BUILT_IN_NORMAL:
7297 break;
7298 case BUILT_IN_MD:
7299 if (targetm.resolve_overloaded_builtin)
7300 return targetm.resolve_overloaded_builtin (loc, function, params);
7301 else
7302 return NULL_TREE;
7303 default:
7304 return NULL_TREE;
7307 /* Handle BUILT_IN_NORMAL here. */
7308 switch (orig_code)
7310 case BUILT_IN_SPECULATION_SAFE_VALUE_N:
7312 tree new_function, first_param, result;
7313 enum built_in_function fncode
7314 = speculation_safe_value_resolve_call (function, params);;
7316 first_param = (*params)[0];
7317 if (fncode == BUILT_IN_NONE
7318 || !speculation_safe_value_resolve_params (loc, function, params))
7319 return error_mark_node;
7321 if (targetm.have_speculation_safe_value (true))
7323 new_function = builtin_decl_explicit (fncode);
7324 result = build_function_call_vec (loc, vNULL, new_function, params,
7325 NULL);
7327 if (result == error_mark_node)
7328 return result;
7330 return speculation_safe_value_resolve_return (first_param, result);
7332 else
7334 /* This target doesn't have, or doesn't need, active mitigation
7335 against incorrect speculative execution. Simply return the
7336 first parameter to the builtin. */
7337 if (!targetm.have_speculation_safe_value (false))
7338 /* The user has invoked __builtin_speculation_safe_value
7339 even though __HAVE_SPECULATION_SAFE_VALUE is not
7340 defined: emit a warning. */
7341 warning_at (input_location, 0,
7342 "this target does not define a speculation barrier; "
7343 "your program will still execute correctly, "
7344 "but incorrect speculation may not be be "
7345 "restricted");
7347 /* If the optional second argument is present, handle any side
7348 effects now. */
7349 if (params->length () == 2
7350 && TREE_SIDE_EFFECTS ((*params)[1]))
7351 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
7352 (*params)[1], first_param);
7354 return first_param;
7358 case BUILT_IN_ATOMIC_EXCHANGE:
7359 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7360 case BUILT_IN_ATOMIC_LOAD:
7361 case BUILT_IN_ATOMIC_STORE:
7363 /* Handle these 4 together so that they can fall through to the next
7364 case if the call is transformed to an _N variant. */
7365 switch (orig_code)
7367 case BUILT_IN_ATOMIC_EXCHANGE:
7369 if (resolve_overloaded_atomic_exchange (loc, function, params,
7370 &new_return))
7371 return new_return;
7372 /* Change to the _N variant. */
7373 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7374 break;
7377 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7379 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7380 params,
7381 &new_return))
7382 return new_return;
7383 /* Change to the _N variant. */
7384 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7385 break;
7387 case BUILT_IN_ATOMIC_LOAD:
7389 if (resolve_overloaded_atomic_load (loc, function, params,
7390 &new_return))
7391 return new_return;
7392 /* Change to the _N variant. */
7393 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7394 break;
7396 case BUILT_IN_ATOMIC_STORE:
7398 if (resolve_overloaded_atomic_store (loc, function, params,
7399 &new_return))
7400 return new_return;
7401 /* Change to the _N variant. */
7402 orig_code = BUILT_IN_ATOMIC_STORE_N;
7403 break;
7405 default:
7406 gcc_unreachable ();
7409 /* FALLTHRU */
7410 case BUILT_IN_ATOMIC_EXCHANGE_N:
7411 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7412 case BUILT_IN_ATOMIC_LOAD_N:
7413 case BUILT_IN_ATOMIC_STORE_N:
7414 fetch_op = false;
7415 /* FALLTHRU */
7416 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7417 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7418 case BUILT_IN_ATOMIC_AND_FETCH_N:
7419 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7420 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7421 case BUILT_IN_ATOMIC_OR_FETCH_N:
7422 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7423 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7424 case BUILT_IN_ATOMIC_FETCH_AND_N:
7425 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7426 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7427 case BUILT_IN_ATOMIC_FETCH_OR_N:
7428 orig_format = false;
7429 /* FALLTHRU */
7430 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7431 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7432 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7433 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7434 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7435 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7436 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7437 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7438 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7439 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7440 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7441 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7442 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7443 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7444 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7445 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7447 /* The following are not _FETCH_OPs and must be accepted with
7448 pointers to _Bool (or C++ bool). */
7449 if (fetch_op)
7450 fetch_op =
7451 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7452 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7453 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7454 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7456 int n = sync_resolve_size (function, params, fetch_op);
7457 tree new_function, first_param, result;
7458 enum built_in_function fncode;
7460 if (n == 0)
7461 return error_mark_node;
7463 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7464 new_function = builtin_decl_explicit (fncode);
7465 if (!sync_resolve_params (loc, function, new_function, params,
7466 orig_format))
7467 return error_mark_node;
7469 first_param = (*params)[0];
7470 result = build_function_call_vec (loc, vNULL, new_function, params,
7471 NULL);
7472 if (result == error_mark_node)
7473 return result;
7474 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7475 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7476 && orig_code != BUILT_IN_ATOMIC_STORE_N
7477 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7478 result = sync_resolve_return (first_param, result, orig_format);
7480 if (fetch_op)
7481 /* Prevent -Wunused-value warning. */
7482 TREE_USED (result) = true;
7484 /* If new_return is set, assign function to that expr and cast the
7485 result to void since the generic interface returned void. */
7486 if (new_return)
7488 /* Cast function result from I{1,2,4,8,16} to the required type. */
7489 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7490 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7491 result);
7492 TREE_SIDE_EFFECTS (result) = 1;
7493 protected_set_expr_location (result, loc);
7494 result = convert (void_type_node, result);
7496 return result;
7499 default:
7500 return NULL_TREE;
7504 /* vector_types_compatible_elements_p is used in type checks of vectors
7505 values used as operands of binary operators. Where it returns true, and
7506 the other checks of the caller succeed (being vector types in he first
7507 place, and matching number of elements), we can just treat the types
7508 as essentially the same.
7509 Contrast with vector_targets_convertible_p, which is used for vector
7510 pointer types, and vector_types_convertible_p, which will allow
7511 language-specific matches under the control of flag_lax_vector_conversions,
7512 and might still require a conversion. */
7513 /* True if vector types T1 and T2 can be inputs to the same binary
7514 operator without conversion.
7515 We don't check the overall vector size here because some of our callers
7516 want to give different error messages when the vectors are compatible
7517 except for the element count. */
7519 bool
7520 vector_types_compatible_elements_p (tree t1, tree t2)
7522 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7523 t1 = TREE_TYPE (t1);
7524 t2 = TREE_TYPE (t2);
7526 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7528 gcc_assert ((INTEGRAL_TYPE_P (t1)
7529 || c1 == REAL_TYPE
7530 || c1 == FIXED_POINT_TYPE)
7531 && (INTEGRAL_TYPE_P (t2)
7532 || c2 == REAL_TYPE
7533 || c2 == FIXED_POINT_TYPE));
7535 t1 = c_common_signed_type (t1);
7536 t2 = c_common_signed_type (t2);
7537 /* Equality works here because c_common_signed_type uses
7538 TYPE_MAIN_VARIANT. */
7539 if (t1 == t2)
7540 return true;
7541 if (opaque && c1 == c2
7542 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
7543 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7544 return true;
7545 return false;
7548 /* Check for missing format attributes on function pointers. LTYPE is
7549 the new type or left-hand side type. RTYPE is the old type or
7550 right-hand side type. Returns TRUE if LTYPE is missing the desired
7551 attribute. */
7553 bool
7554 check_missing_format_attribute (tree ltype, tree rtype)
7556 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7557 tree ra;
7559 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7560 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7561 break;
7562 if (ra)
7564 tree la;
7565 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7566 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7567 break;
7568 return !la;
7570 else
7571 return false;
7574 /* Setup a TYPE_DECL node as a typedef representation.
7576 X is a TYPE_DECL for a typedef statement. Create a brand new
7577 ..._TYPE node (which will be just a variant of the existing
7578 ..._TYPE node with identical properties) and then install X
7579 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7581 The whole point here is to end up with a situation where each
7582 and every ..._TYPE node the compiler creates will be uniquely
7583 associated with AT MOST one node representing a typedef name.
7584 This way, even though the compiler substitutes corresponding
7585 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7586 early on, later parts of the compiler can always do the reverse
7587 translation and get back the corresponding typedef name. For
7588 example, given:
7590 typedef struct S MY_TYPE;
7591 MY_TYPE object;
7593 Later parts of the compiler might only know that `object' was of
7594 type `struct S' if it were not for code just below. With this
7595 code however, later parts of the compiler see something like:
7597 struct S' == struct S
7598 typedef struct S' MY_TYPE;
7599 struct S' object;
7601 And they can then deduce (from the node for type struct S') that
7602 the original object declaration was:
7604 MY_TYPE object;
7606 Being able to do this is important for proper support of protoize,
7607 and also for generating precise symbolic debugging information
7608 which takes full account of the programmer's (typedef) vocabulary.
7610 Obviously, we don't want to generate a duplicate ..._TYPE node if
7611 the TYPE_DECL node that we are now processing really represents a
7612 standard built-in type. */
7614 void
7615 set_underlying_type (tree x)
7617 if (x == error_mark_node)
7618 return;
7619 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7621 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7622 TYPE_NAME (TREE_TYPE (x)) = x;
7624 else if (TREE_TYPE (x) != error_mark_node
7625 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7627 tree tt = TREE_TYPE (x);
7628 DECL_ORIGINAL_TYPE (x) = tt;
7629 tt = build_variant_type_copy (tt);
7630 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7631 TYPE_NAME (tt) = x;
7633 /* Mark the type as used only when its type decl is decorated
7634 with attribute unused. */
7635 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7636 TREE_USED (tt) = 1;
7638 TREE_TYPE (x) = tt;
7642 /* Record the types used by the current global variable declaration
7643 being parsed, so that we can decide later to emit their debug info.
7644 Those types are in types_used_by_cur_var_decl, and we are going to
7645 store them in the types_used_by_vars_hash hash table.
7646 DECL is the declaration of the global variable that has been parsed. */
7648 void
7649 record_types_used_by_current_var_decl (tree decl)
7651 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7653 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7655 tree type = types_used_by_cur_var_decl->pop ();
7656 types_used_by_var_decl_insert (type, decl);
7660 /* The C and C++ parsers both use vectors to hold function arguments.
7661 For efficiency, we keep a cache of unused vectors. This is the
7662 cache. */
7664 typedef vec<tree, va_gc> *tree_gc_vec;
7665 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7667 /* Return a new vector from the cache. If the cache is empty,
7668 allocate a new vector. These vectors are GC'ed, so it is OK if the
7669 pointer is not released.. */
7671 vec<tree, va_gc> *
7672 make_tree_vector (void)
7674 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7675 return tree_vector_cache->pop ();
7676 else
7678 /* Passing 0 to vec::alloc returns NULL, and our callers require
7679 that we always return a non-NULL value. The vector code uses
7680 4 when growing a NULL vector, so we do too. */
7681 vec<tree, va_gc> *v;
7682 vec_alloc (v, 4);
7683 return v;
7687 /* Release a vector of trees back to the cache. */
7689 void
7690 release_tree_vector (vec<tree, va_gc> *vec)
7692 if (vec != NULL)
7694 vec->truncate (0);
7695 vec_safe_push (tree_vector_cache, vec);
7699 /* Get a new tree vector holding a single tree. */
7701 vec<tree, va_gc> *
7702 make_tree_vector_single (tree t)
7704 vec<tree, va_gc> *ret = make_tree_vector ();
7705 ret->quick_push (t);
7706 return ret;
7709 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7711 vec<tree, va_gc> *
7712 make_tree_vector_from_list (tree list)
7714 vec<tree, va_gc> *ret = make_tree_vector ();
7715 for (; list; list = TREE_CHAIN (list))
7716 vec_safe_push (ret, TREE_VALUE (list));
7717 return ret;
7720 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7722 vec<tree, va_gc> *
7723 make_tree_vector_from_ctor (tree ctor)
7725 vec<tree,va_gc> *ret = make_tree_vector ();
7726 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7727 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7728 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7729 return ret;
7732 /* Get a new tree vector which is a copy of an existing one. */
7734 vec<tree, va_gc> *
7735 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7737 vec<tree, va_gc> *ret;
7738 unsigned int ix;
7739 tree t;
7741 ret = make_tree_vector ();
7742 vec_safe_reserve (ret, vec_safe_length (orig));
7743 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7744 ret->quick_push (t);
7745 return ret;
7748 /* Return true if KEYWORD starts a type specifier. */
7750 bool
7751 keyword_begins_type_specifier (enum rid keyword)
7753 switch (keyword)
7755 case RID_AUTO_TYPE:
7756 case RID_INT:
7757 case RID_CHAR:
7758 case RID_FLOAT:
7759 case RID_DOUBLE:
7760 case RID_VOID:
7761 case RID_UNSIGNED:
7762 case RID_LONG:
7763 case RID_SHORT:
7764 case RID_SIGNED:
7765 CASE_RID_FLOATN_NX:
7766 case RID_DFLOAT32:
7767 case RID_DFLOAT64:
7768 case RID_DFLOAT128:
7769 case RID_FRACT:
7770 case RID_ACCUM:
7771 case RID_BOOL:
7772 case RID_WCHAR:
7773 case RID_CHAR16:
7774 case RID_CHAR32:
7775 case RID_SAT:
7776 case RID_COMPLEX:
7777 case RID_TYPEOF:
7778 case RID_STRUCT:
7779 case RID_CLASS:
7780 case RID_UNION:
7781 case RID_ENUM:
7782 return true;
7783 default:
7784 if (keyword >= RID_FIRST_INT_N
7785 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7786 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7787 return true;
7788 return false;
7792 /* Return true if KEYWORD names a type qualifier. */
7794 bool
7795 keyword_is_type_qualifier (enum rid keyword)
7797 switch (keyword)
7799 case RID_CONST:
7800 case RID_VOLATILE:
7801 case RID_RESTRICT:
7802 case RID_ATOMIC:
7803 return true;
7804 default:
7805 return false;
7809 /* Return true if KEYWORD names a storage class specifier.
7811 RID_TYPEDEF is not included in this list despite `typedef' being
7812 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7813 such for syntactic convenience only. */
7815 bool
7816 keyword_is_storage_class_specifier (enum rid keyword)
7818 switch (keyword)
7820 case RID_STATIC:
7821 case RID_EXTERN:
7822 case RID_REGISTER:
7823 case RID_AUTO:
7824 case RID_MUTABLE:
7825 case RID_THREAD:
7826 return true;
7827 default:
7828 return false;
7832 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7834 static bool
7835 keyword_is_function_specifier (enum rid keyword)
7837 switch (keyword)
7839 case RID_INLINE:
7840 case RID_NORETURN:
7841 case RID_VIRTUAL:
7842 case RID_EXPLICIT:
7843 return true;
7844 default:
7845 return false;
7849 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7850 declaration-specifier (C99 6.7). */
7852 bool
7853 keyword_is_decl_specifier (enum rid keyword)
7855 if (keyword_is_storage_class_specifier (keyword)
7856 || keyword_is_type_qualifier (keyword)
7857 || keyword_is_function_specifier (keyword))
7858 return true;
7860 switch (keyword)
7862 case RID_TYPEDEF:
7863 case RID_FRIEND:
7864 case RID_CONSTEXPR:
7865 return true;
7866 default:
7867 return false;
7871 /* Initialize language-specific-bits of tree_contains_struct. */
7873 void
7874 c_common_init_ts (void)
7876 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7877 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7880 /* Build a user-defined numeric literal out of an integer constant type VALUE
7881 with identifier SUFFIX. */
7883 tree
7884 build_userdef_literal (tree suffix_id, tree value,
7885 enum overflow_type overflow, tree num_string)
7887 tree literal = make_node (USERDEF_LITERAL);
7888 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7889 USERDEF_LITERAL_VALUE (literal) = value;
7890 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7891 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7892 return literal;
7895 /* For vector[index], convert the vector to an array of the underlying type.
7896 Return true if the resulting ARRAY_REF should not be an lvalue. */
7898 bool
7899 convert_vector_to_array_for_subscript (location_t loc,
7900 tree *vecp, tree index)
7902 bool ret = false;
7903 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7905 tree type = TREE_TYPE (*vecp);
7907 ret = !lvalue_p (*vecp);
7909 if (TREE_CODE (index) == INTEGER_CST)
7910 if (!tree_fits_uhwi_p (index)
7911 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7912 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7914 /* We are building an ARRAY_REF so mark the vector as addressable
7915 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7916 for function parameters. */
7917 c_common_mark_addressable_vec (*vecp);
7919 *vecp = build1 (VIEW_CONVERT_EXPR,
7920 build_array_type_nelts (TREE_TYPE (type),
7921 TYPE_VECTOR_SUBPARTS (type)),
7922 *vecp);
7924 return ret;
7927 /* Determine which of the operands, if any, is a scalar that needs to be
7928 converted to a vector, for the range of operations. */
7929 enum stv_conv
7930 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7931 bool complain)
7933 tree type0 = TREE_TYPE (op0);
7934 tree type1 = TREE_TYPE (op1);
7935 bool integer_only_op = false;
7936 enum stv_conv ret = stv_firstarg;
7938 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7939 switch (code)
7941 /* Most GENERIC binary expressions require homogeneous arguments.
7942 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7943 argument that is a vector and a second one that is a scalar, so
7944 we never return stv_secondarg for them. */
7945 case RSHIFT_EXPR:
7946 case LSHIFT_EXPR:
7947 if (TREE_CODE (type0) == INTEGER_TYPE
7948 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7950 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7951 NULL_TREE, false))
7953 if (complain)
7954 error_at (loc, "conversion of scalar %qT to vector %qT "
7955 "involves truncation", type0, type1);
7956 return stv_error;
7958 else
7959 return stv_firstarg;
7961 break;
7963 case BIT_IOR_EXPR:
7964 case BIT_XOR_EXPR:
7965 case BIT_AND_EXPR:
7966 integer_only_op = true;
7967 /* fall through */
7969 case VEC_COND_EXPR:
7971 case PLUS_EXPR:
7972 case MINUS_EXPR:
7973 case MULT_EXPR:
7974 case TRUNC_DIV_EXPR:
7975 case CEIL_DIV_EXPR:
7976 case FLOOR_DIV_EXPR:
7977 case ROUND_DIV_EXPR:
7978 case EXACT_DIV_EXPR:
7979 case TRUNC_MOD_EXPR:
7980 case FLOOR_MOD_EXPR:
7981 case RDIV_EXPR:
7982 case EQ_EXPR:
7983 case NE_EXPR:
7984 case LE_EXPR:
7985 case GE_EXPR:
7986 case LT_EXPR:
7987 case GT_EXPR:
7988 /* What about UNLT_EXPR? */
7989 if (VECTOR_TYPE_P (type0))
7991 ret = stv_secondarg;
7992 std::swap (type0, type1);
7993 std::swap (op0, op1);
7996 if (TREE_CODE (type0) == INTEGER_TYPE
7997 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7999 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
8000 NULL_TREE, false))
8002 if (complain)
8003 error_at (loc, "conversion of scalar %qT to vector %qT "
8004 "involves truncation", type0, type1);
8005 return stv_error;
8007 return ret;
8009 else if (!integer_only_op
8010 /* Allow integer --> real conversion if safe. */
8011 && (TREE_CODE (type0) == REAL_TYPE
8012 || TREE_CODE (type0) == INTEGER_TYPE)
8013 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
8015 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
8016 NULL_TREE, false))
8018 if (complain)
8019 error_at (loc, "conversion of scalar %qT to vector %qT "
8020 "involves truncation", type0, type1);
8021 return stv_error;
8023 return ret;
8025 default:
8026 break;
8029 return stv_nothing;
8032 /* Return the alignment of std::max_align_t.
8034 [support.types.layout] The type max_align_t is a POD type whose alignment
8035 requirement is at least as great as that of every scalar type, and whose
8036 alignment requirement is supported in every context. */
8038 unsigned
8039 max_align_t_align ()
8041 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
8042 TYPE_ALIGN (long_double_type_node));
8043 if (float128_type_node != NULL_TREE)
8044 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
8045 return max_align;
8048 /* Return true iff ALIGN is an integral constant that is a fundamental
8049 alignment, as defined by [basic.align] in the c++-11
8050 specifications.
8052 That is:
8054 [A fundamental alignment is represented by an alignment less than or
8055 equal to the greatest alignment supported by the implementation
8056 in all contexts, which is equal to alignof(max_align_t)]. */
8058 bool
8059 cxx_fundamental_alignment_p (unsigned align)
8061 return (align <= max_align_t_align ());
8064 /* Return true if T is a pointer to a zero-sized aggregate. */
8066 bool
8067 pointer_to_zero_sized_aggr_p (tree t)
8069 if (!POINTER_TYPE_P (t))
8070 return false;
8071 t = TREE_TYPE (t);
8072 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
8075 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8076 with no library fallback or for an ADDR_EXPR whose operand is such type
8077 issues an error pointing to the location LOC.
8078 Returns true when the expression has been diagnosed and false
8079 otherwise. */
8081 bool
8082 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
8084 if (TREE_CODE (expr) == ADDR_EXPR)
8085 expr = TREE_OPERAND (expr, 0);
8087 STRIP_ANY_LOCATION_WRAPPER (expr);
8089 if (TREE_TYPE (expr)
8090 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
8091 && TREE_CODE (expr) == FUNCTION_DECL
8092 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8093 false positives for user-declared built-ins such as abs or
8094 strlen, and for C++ operators new and delete.
8095 The c_decl_implicit() test avoids false positives for implicitly
8096 declared built-ins with library fallbacks (such as abs). */
8097 && fndecl_built_in_p (expr)
8098 && DECL_IS_BUILTIN (expr)
8099 && !c_decl_implicit (expr)
8100 && !DECL_ASSEMBLER_NAME_SET_P (expr))
8102 if (loc == UNKNOWN_LOCATION)
8103 loc = EXPR_LOC_OR_LOC (expr, input_location);
8105 /* Reject arguments that are built-in functions with
8106 no library fallback. */
8107 error_at (loc, "built-in function %qE must be directly called", expr);
8109 return true;
8112 return false;
8115 /* Check if array size calculations overflow or if the array covers more
8116 than half of the address space. Return true if the size of the array
8117 is valid, false otherwise. TYPE is the type of the array and NAME is
8118 the name of the array, or NULL_TREE for unnamed arrays. */
8120 bool
8121 valid_array_size_p (location_t loc, tree type, tree name, bool complain)
8123 if (type != error_mark_node
8124 && COMPLETE_TYPE_P (type)
8125 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8126 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
8128 if (complain)
8130 if (name)
8131 error_at (loc, "size of array %qE is too large", name);
8132 else
8133 error_at (loc, "size of unnamed array is too large");
8135 return false;
8137 return true;
8140 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8141 timestamp to replace embedded current dates to get reproducible
8142 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
8144 time_t
8145 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
8147 char *source_date_epoch;
8148 int64_t epoch;
8149 char *endptr;
8151 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
8152 if (!source_date_epoch)
8153 return (time_t) -1;
8155 errno = 0;
8156 #if defined(INT64_T_IS_LONG)
8157 epoch = strtol (source_date_epoch, &endptr, 10);
8158 #else
8159 epoch = strtoll (source_date_epoch, &endptr, 10);
8160 #endif
8161 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
8162 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
8164 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
8165 "expand to a non-negative integer less than or equal to %wd",
8166 MAX_SOURCE_DATE_EPOCH);
8167 return (time_t) -1;
8170 return (time_t) epoch;
8173 /* Callback for libcpp for offering spelling suggestions for misspelled
8174 directives. GOAL is an unrecognized string; CANDIDATES is a
8175 NULL-terminated array of candidate strings. Return the closest
8176 match to GOAL within CANDIDATES, or NULL if none are good
8177 suggestions. */
8179 const char *
8180 cb_get_suggestion (cpp_reader *, const char *goal,
8181 const char *const *candidates)
8183 best_match<const char *, const char *> bm (goal);
8184 while (*candidates)
8185 bm.consider (*candidates++);
8186 return bm.get_best_meaningful_candidate ();
8189 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8190 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8191 by C TS 18661-3 for interchange types that are computed in their
8192 native precision are larger than the C11 values for evaluating in the
8193 precision of float/double/long double. If either mode is
8194 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8196 enum flt_eval_method
8197 excess_precision_mode_join (enum flt_eval_method x,
8198 enum flt_eval_method y)
8200 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8201 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8202 return FLT_EVAL_METHOD_UNPREDICTABLE;
8204 /* GCC only supports one interchange type right now, _Float16. If
8205 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8206 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8207 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8208 return y;
8209 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8210 return x;
8212 /* Other values for flt_eval_method are directly comparable, and we want
8213 the maximum. */
8214 return MAX (x, y);
8217 /* Return the value that should be set for FLT_EVAL_METHOD in the
8218 context of ISO/IEC TS 18861-3.
8220 This relates to the effective excess precision seen by the user,
8221 which is the join point of the precision the target requests for
8222 -fexcess-precision={standard,fast} and the implicit excess precision
8223 the target uses. */
8225 static enum flt_eval_method
8226 c_ts18661_flt_eval_method (void)
8228 enum flt_eval_method implicit
8229 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8231 enum excess_precision_type flag_type
8232 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8233 ? EXCESS_PRECISION_TYPE_STANDARD
8234 : EXCESS_PRECISION_TYPE_FAST);
8236 enum flt_eval_method requested
8237 = targetm.c.excess_precision (flag_type);
8239 return excess_precision_mode_join (implicit, requested);
8242 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8243 those that were permitted by C11. That is to say, eliminates
8244 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8246 static enum flt_eval_method
8247 c_c11_flt_eval_method (void)
8249 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8250 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8253 /* Return the value that should be set for FLT_EVAL_METHOD.
8254 MAYBE_C11_ONLY_P is TRUE if we should check
8255 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8256 values we can return to those from C99/C11, and FALSE otherwise.
8257 See the comments on c_ts18661_flt_eval_method for what value we choose
8258 to set here. */
8261 c_flt_eval_method (bool maybe_c11_only_p)
8263 if (maybe_c11_only_p
8264 && flag_permitted_flt_eval_methods
8265 == PERMITTED_FLT_EVAL_METHODS_C11)
8266 return c_c11_flt_eval_method ();
8267 else
8268 return c_ts18661_flt_eval_method ();
8271 /* An enum for get_missing_token_insertion_kind for describing the best
8272 place to insert a missing token, if there is one. */
8274 enum missing_token_insertion_kind
8276 MTIK_IMPOSSIBLE,
8277 MTIK_INSERT_BEFORE_NEXT,
8278 MTIK_INSERT_AFTER_PREV
8281 /* Given a missing token of TYPE, determine if it is reasonable to
8282 emit a fix-it hint suggesting the insertion of the token, and,
8283 if so, where the token should be inserted relative to other tokens.
8285 It only makes sense to do this for values of TYPE that are symbols.
8287 Some symbols should go before the next token, e.g. in:
8288 if flag)
8289 we want to insert the missing '(' immediately before "flag",
8290 giving:
8291 if (flag)
8292 rather than:
8293 if( flag)
8294 These use MTIK_INSERT_BEFORE_NEXT.
8296 Other symbols should go after the previous token, e.g. in:
8297 if (flag
8298 do_something ();
8299 we want to insert the missing ')' immediately after the "flag",
8300 giving:
8301 if (flag)
8302 do_something ();
8303 rather than:
8304 if (flag
8305 )do_something ();
8306 These use MTIK_INSERT_AFTER_PREV. */
8308 static enum missing_token_insertion_kind
8309 get_missing_token_insertion_kind (enum cpp_ttype type)
8311 switch (type)
8313 /* Insert missing "opening" brackets immediately
8314 before the next token. */
8315 case CPP_OPEN_SQUARE:
8316 case CPP_OPEN_PAREN:
8317 return MTIK_INSERT_BEFORE_NEXT;
8319 /* Insert other missing symbols immediately after
8320 the previous token. */
8321 case CPP_CLOSE_PAREN:
8322 case CPP_CLOSE_SQUARE:
8323 case CPP_SEMICOLON:
8324 case CPP_COMMA:
8325 case CPP_COLON:
8326 return MTIK_INSERT_AFTER_PREV;
8328 /* Other kinds of token don't get fix-it hints. */
8329 default:
8330 return MTIK_IMPOSSIBLE;
8334 /* Given RICHLOC, a location for a diagnostic describing a missing token
8335 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8336 insertion of the token.
8338 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8339 it will either be:
8340 (a) immediately after PREV_TOKEN_LOC, or
8342 (b) immediately before the primary location within RICHLOC (taken to
8343 be that of the token following where the token was expected).
8345 If we manage to add a fix-it hint, then the location of the
8346 fix-it hint is likely to be more useful as the primary location
8347 of the diagnostic than that of the following token, so we swap
8348 these locations.
8350 For example, given this bogus code:
8351 123456789012345678901234567890
8352 1 | int missing_semicolon (void)
8353 2 | {
8354 3 | return 42
8355 4 | }
8357 we will emit:
8359 "expected ';' before '}'"
8361 RICHLOC's primary location is at the closing brace, so before "swapping"
8362 we would emit the error at line 4 column 1:
8364 123456789012345678901234567890
8365 3 | return 42 |< fix-it hint emitted for this line
8366 | ; |
8367 4 | } |< "expected ';' before '}'" emitted at this line
8368 | ^ |
8370 It's more useful for the location of the diagnostic to be at the
8371 fix-it hint, so we swap the locations, so the primary location
8372 is at the fix-it hint, with the old primary location inserted
8373 as a secondary location, giving this, with the error at line 3
8374 column 12:
8376 123456789012345678901234567890
8377 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8378 | ^ | with fix-it hint
8379 4 | ; |
8380 | } |< secondary range emitted here
8381 | ~ |. */
8383 void
8384 maybe_suggest_missing_token_insertion (rich_location *richloc,
8385 enum cpp_ttype token_type,
8386 location_t prev_token_loc)
8388 gcc_assert (richloc);
8390 enum missing_token_insertion_kind mtik
8391 = get_missing_token_insertion_kind (token_type);
8393 switch (mtik)
8395 default:
8396 gcc_unreachable ();
8397 break;
8399 case MTIK_IMPOSSIBLE:
8400 return;
8402 case MTIK_INSERT_BEFORE_NEXT:
8403 /* Attempt to add the fix-it hint before the primary location
8404 of RICHLOC. */
8405 richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
8406 break;
8408 case MTIK_INSERT_AFTER_PREV:
8409 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8410 richloc->add_fixit_insert_after (prev_token_loc,
8411 cpp_type2name (token_type, 0));
8412 break;
8415 /* If we were successful, use the fix-it hint's location as the
8416 primary location within RICHLOC, adding the old primary location
8417 back as a secondary location. */
8418 if (!richloc->seen_impossible_fixit_p ())
8420 fixit_hint *hint = richloc->get_last_fixit_hint ();
8421 location_t hint_loc = hint->get_start_loc ();
8422 location_t old_loc = richloc->get_loc ();
8424 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
8425 richloc->add_range (old_loc);
8429 #if CHECKING_P
8431 namespace selftest {
8433 /* Verify that fold_for_warn on error_mark_node is safe. */
8435 static void
8436 test_fold_for_warn ()
8438 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8441 /* Run all of the selftests within this file. */
8443 static void
8444 c_common_c_tests ()
8446 test_fold_for_warn ();
8449 /* Run all of the tests within c-family. */
8451 void
8452 c_family_tests (void)
8454 c_common_c_tests ();
8455 c_format_c_tests ();
8456 c_indentation_c_tests ();
8457 c_pretty_print_c_tests ();
8458 c_spellcheck_cc_tests ();
8461 } // namespace selftest
8463 #endif /* #if CHECKING_P */
8465 /* Attempt to locate a suitable location within FILE for a
8466 #include directive to be inserted before. FILE should
8467 be a string from libcpp (pointer equality is used).
8468 LOC is the location of the relevant diagnostic.
8470 Attempt to return the location within FILE immediately
8471 after the last #include within that file, or the start of
8472 that file if it has no #include directives.
8474 Return UNKNOWN_LOCATION if no suitable location is found,
8475 or if an error occurs. */
8477 static location_t
8478 try_to_locate_new_include_insertion_point (const char *file, location_t loc)
8480 /* Locate the last ordinary map within FILE that ended with a #include. */
8481 const line_map_ordinary *last_include_ord_map = NULL;
8483 /* ...and the next ordinary map within FILE after that one. */
8484 const line_map_ordinary *last_ord_map_after_include = NULL;
8486 /* ...and the first ordinary map within FILE. */
8487 const line_map_ordinary *first_ord_map_in_file = NULL;
8489 /* Get ordinary map containing LOC (or its expansion). */
8490 const line_map_ordinary *ord_map_for_loc = NULL;
8491 loc = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
8492 &ord_map_for_loc);
8493 gcc_assert (ord_map_for_loc);
8495 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8497 const line_map_ordinary *ord_map
8498 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8500 if (const line_map_ordinary *from
8501 = linemap_included_from_linemap (line_table, ord_map))
8502 if (from->to_file == file)
8504 last_include_ord_map = from;
8505 last_ord_map_after_include = NULL;
8508 if (ord_map->to_file == file)
8510 if (!first_ord_map_in_file)
8511 first_ord_map_in_file = ord_map;
8512 if (last_include_ord_map && !last_ord_map_after_include)
8513 last_ord_map_after_include = ord_map;
8516 /* Stop searching when reaching the ord_map containing LOC,
8517 as it makes no sense to provide fix-it hints that appear
8518 after the diagnostic in question. */
8519 if (ord_map == ord_map_for_loc)
8520 break;
8523 /* Determine where to insert the #include. */
8524 const line_map_ordinary *ord_map_for_insertion;
8526 /* We want the next ordmap in the file after the last one that's a
8527 #include, but failing that, the start of the file. */
8528 if (last_ord_map_after_include)
8529 ord_map_for_insertion = last_ord_map_after_include;
8530 else
8531 ord_map_for_insertion = first_ord_map_in_file;
8533 if (!ord_map_for_insertion)
8534 return UNKNOWN_LOCATION;
8536 /* The "start_location" is column 0, meaning "the whole line".
8537 rich_location and edit_context can't cope with this, so use
8538 column 1 instead. */
8539 location_t col_0 = ord_map_for_insertion->start_location;
8540 return linemap_position_for_loc_and_offset (line_table, col_0, 1);
8543 /* A map from filenames to sets of headers added to them, for
8544 ensuring idempotency within maybe_add_include_fixit. */
8546 /* The values within the map. We need string comparison as there's
8547 no guarantee that two different diagnostics that are recommending
8548 adding e.g. "<stdio.h>" are using the same buffer. */
8550 typedef hash_set <const char *, nofree_string_hash> per_file_includes_t;
8552 /* The map itself. We don't need string comparison for the filename keys,
8553 as they come from libcpp. */
8555 typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
8556 static added_includes_t *added_includes;
8558 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8559 in a suitable location within the file of RICHLOC's primary
8560 location.
8562 This function is idempotent: a header will be added at most once to
8563 any given file.
8565 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8566 printed, then RICHLOC's primary location will be replaced by that of
8567 the fix-it hint (for use by "inform" notes where the location of the
8568 issue has already been reported). */
8570 void
8571 maybe_add_include_fixit (rich_location *richloc, const char *header,
8572 bool override_location)
8574 location_t loc = richloc->get_loc ();
8575 const char *file = LOCATION_FILE (loc);
8576 if (!file)
8577 return;
8579 /* Idempotency: don't add the same header more than once to a given file. */
8580 if (!added_includes)
8581 added_includes = new added_includes_t ();
8582 per_file_includes_t *&set = added_includes->get_or_insert (file);
8583 if (set)
8584 if (set->contains (header))
8585 /* ...then we've already added HEADER to that file. */
8586 return;
8587 if (!set)
8588 set = new per_file_includes_t ();
8589 set->add (header);
8591 /* Attempt to locate a suitable place for the new directive. */
8592 location_t include_insert_loc
8593 = try_to_locate_new_include_insertion_point (file, loc);
8594 if (include_insert_loc == UNKNOWN_LOCATION)
8595 return;
8597 char *text = xasprintf ("#include %s\n", header);
8598 richloc->add_fixit_insert_before (include_insert_loc, text);
8599 free (text);
8601 if (override_location && global_dc->show_caret)
8603 /* Replace the primary location with that of the insertion point for the
8604 fix-it hint.
8606 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8607 caret for the insertion point (or colorize it).
8609 Hence we print e.g.:
8611 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8612 73 | # include <debug/vector>
8613 +++ |+#include <vector>
8614 74 | #endif
8616 rather than:
8618 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8619 73 | # include <debug/vector>
8620 +++ |+#include <vector>
8621 74 | #endif
8624 avoiding the caret on the first column of line 74. */
8625 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
8629 /* Attempt to convert a braced array initializer list CTOR for array
8630 TYPE into a STRING_CST for convenience and efficiency. Return
8631 the converted string on success or the original ctor on failure. */
8633 tree
8634 braced_list_to_string (tree type, tree ctor)
8636 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
8637 return ctor;
8639 /* If the array has an explicit bound, use it to constrain the size
8640 of the string. If it doesn't, be sure to create a string that's
8641 as long as implied by the index of the last zero specified via
8642 a designator, as in:
8643 const char a[] = { [7] = 0 }; */
8644 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8645 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8647 /* Avoid converting initializers for zero-length arrays. */
8648 if (!maxelts)
8649 return ctor;
8651 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
8653 auto_vec<char> str;
8654 str.reserve (nelts + 1);
8656 unsigned HOST_WIDE_INT i;
8657 tree index, value;
8659 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
8661 unsigned HOST_WIDE_INT idx = i;
8662 if (index)
8664 if (!tree_fits_uhwi_p (index))
8665 return ctor;
8666 idx = tree_to_uhwi (index);
8669 /* auto_vec is limited to UINT_MAX elements. */
8670 if (idx > UINT_MAX)
8671 return ctor;
8673 /* Avoid non-constant initializers. */
8674 if (!tree_fits_shwi_p (value))
8675 return ctor;
8677 /* Skip over embedded nuls except the last one (initializer
8678 elements are in ascending order of indices). */
8679 HOST_WIDE_INT val = tree_to_shwi (value);
8680 if (!val && i + 1 < nelts)
8681 continue;
8683 if (idx < str.length())
8684 return ctor;
8686 /* Bail if the CTOR has a block of more than 256 embedded nuls
8687 due to implicitly initialized elements. */
8688 unsigned nchars = (idx - str.length ()) + 1;
8689 if (nchars > 256)
8690 return ctor;
8692 if (nchars > 1)
8694 str.reserve (idx);
8695 str.quick_grow_cleared (idx);
8698 if (idx >= maxelts)
8699 return ctor;
8701 str.safe_insert (idx, val);
8704 /* Append a nul string termination. */
8705 if (str.length () < maxelts)
8706 str.safe_push (0);
8708 /* Build a STRING_CST with the same type as the array. */
8709 tree res = build_string (str.length (), str.begin ());
8710 TREE_TYPE (res) = type;
8711 return res;
8714 #include "gt-c-family-c-common.h"