[AArch64] Avoid GET_MODE_NUNITS in v8.4 support
[official-gcc.git] / gcc / c-family / c-common.c
blob097d192c8691c9339cdd24599a77466277926046
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 that we have builtin functions, and main is an int. */
219 int flag_hosted = 1;
222 /* ObjC language option variables. */
225 /* Tells the compiler that this is a special run. Do not perform any
226 compiling, instead we are to test some platform dependent features
227 and output a C header file with appropriate definitions. */
229 int print_struct_values;
231 /* Tells the compiler what is the constant string class for ObjC. */
233 const char *constant_string_class_name;
236 /* C++ language option variables. */
238 /* The reference version of the ABI for -Wabi. */
240 int warn_abi_version = -1;
242 /* Nonzero means generate separate instantiation control files and
243 juggle them at link time. */
245 int flag_use_repository;
247 /* The C++ dialect being used. Default set in c_common_post_options. */
249 enum cxx_dialect cxx_dialect = cxx_unset;
251 /* Maximum template instantiation depth. This limit exists to limit the
252 time it takes to notice excessively recursive template instantiations.
254 The default is lower than the 1024 recommended by the C++0x standard
255 because G++ runs out of stack before 1024 with highly recursive template
256 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
258 int max_tinst_depth = 900;
260 /* The elements of `ridpointers' are identifier nodes for the reserved
261 type names and storage classes. It is indexed by a RID_... value. */
262 tree *ridpointers;
264 tree (*make_fname_decl) (location_t, tree, int);
266 /* Nonzero means don't warn about problems that occur when the code is
267 executed. */
268 int c_inhibit_evaluation_warnings;
270 /* Whether we are building a boolean conversion inside
271 convert_for_assignment, or some other late binary operation. If
272 build_binary_op is called for C (from code shared by C and C++) in
273 this case, then the operands have already been folded and the
274 result will not be folded again, so C_MAYBE_CONST_EXPR should not
275 be generated. */
276 bool in_late_binary_op;
278 /* Whether lexing has been completed, so subsequent preprocessor
279 errors should use the compiler's input_location. */
280 bool done_lexing = false;
282 /* Information about how a function name is generated. */
283 struct fname_var_t
285 tree *const decl; /* pointer to the VAR_DECL. */
286 const unsigned rid; /* RID number for the identifier. */
287 const int pretty; /* How pretty is it? */
290 /* The three ways of getting then name of the current function. */
292 const struct fname_var_t fname_vars[] =
294 /* C99 compliant __func__, must be first. */
295 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
296 /* GCC __FUNCTION__ compliant. */
297 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
298 /* GCC __PRETTY_FUNCTION__ compliant. */
299 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
300 {NULL, 0, 0},
303 /* Global visibility options. */
304 struct visibility_flags visibility_options;
306 static tree check_case_value (location_t, tree);
307 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
308 bool *);
311 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
312 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
314 /* Reserved words. The third field is a mask: keywords are disabled
315 if they match the mask.
317 Masks for languages:
318 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
319 C --std=c99: D_CXXONLY | D_OBJC
320 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
321 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
322 C++ --std=c++11: D_CONLY | D_OBJC
323 ObjC++ is like C++ except that D_OBJC is not set
325 If -fno-asm is used, D_ASM is added to the mask. If
326 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
327 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
328 In C with -Wc++-compat, we warn if D_CXXWARN is set.
330 Note the complication of the D_CXX_OBJC keywords. These are
331 reserved words such as 'class'. In C++, 'class' is a reserved
332 word. In Objective-C++ it is too. In Objective-C, it is a
333 reserved word too, but only if it follows an '@' sign.
335 const struct c_common_resword c_common_reswords[] =
337 { "_Alignas", RID_ALIGNAS, D_CONLY },
338 { "_Alignof", RID_ALIGNOF, D_CONLY },
339 { "_Atomic", RID_ATOMIC, D_CONLY },
340 { "_Bool", RID_BOOL, D_CONLY },
341 { "_Complex", RID_COMPLEX, 0 },
342 { "_Imaginary", RID_IMAGINARY, D_CONLY },
343 { "_Float16", RID_FLOAT16, D_CONLY },
344 { "_Float32", RID_FLOAT32, D_CONLY },
345 { "_Float64", RID_FLOAT64, D_CONLY },
346 { "_Float128", RID_FLOAT128, D_CONLY },
347 { "_Float32x", RID_FLOAT32X, D_CONLY },
348 { "_Float64x", RID_FLOAT64X, D_CONLY },
349 { "_Float128x", RID_FLOAT128X, D_CONLY },
350 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
351 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
352 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
353 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
354 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
355 { "_Sat", RID_SAT, D_CONLY | D_EXT },
356 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
357 { "_Noreturn", RID_NORETURN, D_CONLY },
358 { "_Generic", RID_GENERIC, D_CONLY },
359 { "_Thread_local", RID_THREAD, D_CONLY },
360 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
361 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
362 { "__alignof", RID_ALIGNOF, 0 },
363 { "__alignof__", RID_ALIGNOF, 0 },
364 { "__asm", RID_ASM, 0 },
365 { "__asm__", RID_ASM, 0 },
366 { "__attribute", RID_ATTRIBUTE, 0 },
367 { "__attribute__", RID_ATTRIBUTE, 0 },
368 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
369 { "__bases", RID_BASES, D_CXXONLY },
370 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
371 { "__builtin_call_with_static_chain",
372 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
373 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
374 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
375 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
376 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
377 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
378 { "__builtin_offsetof", RID_OFFSETOF, 0 },
379 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
380 { "__builtin_va_arg", RID_VA_ARG, 0 },
381 { "__complex", RID_COMPLEX, 0 },
382 { "__complex__", RID_COMPLEX, 0 },
383 { "__const", RID_CONST, 0 },
384 { "__const__", RID_CONST, 0 },
385 { "__decltype", RID_DECLTYPE, D_CXXONLY },
386 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
387 { "__extension__", RID_EXTENSION, 0 },
388 { "__func__", RID_C99_FUNCTION_NAME, 0 },
389 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
390 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
391 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
392 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
393 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
394 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
395 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
396 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
397 D_CXXONLY },
398 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
399 { "__imag", RID_IMAGPART, 0 },
400 { "__imag__", RID_IMAGPART, 0 },
401 { "__inline", RID_INLINE, 0 },
402 { "__inline__", RID_INLINE, 0 },
403 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
404 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
405 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
406 { "__is_class", RID_IS_CLASS, D_CXXONLY },
407 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
408 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
409 { "__is_final", RID_IS_FINAL, D_CXXONLY },
410 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
411 { "__is_pod", RID_IS_POD, D_CXXONLY },
412 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
413 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
414 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
415 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
416 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
417 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
418 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
419 { "__is_union", RID_IS_UNION, D_CXXONLY },
420 { "__label__", RID_LABEL, 0 },
421 { "__null", RID_NULL, 0 },
422 { "__real", RID_REALPART, 0 },
423 { "__real__", RID_REALPART, 0 },
424 { "__restrict", RID_RESTRICT, 0 },
425 { "__restrict__", RID_RESTRICT, 0 },
426 { "__signed", RID_SIGNED, 0 },
427 { "__signed__", RID_SIGNED, 0 },
428 { "__thread", RID_THREAD, 0 },
429 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
430 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
431 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
432 { "__typeof", RID_TYPEOF, 0 },
433 { "__typeof__", RID_TYPEOF, 0 },
434 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
435 { "__volatile", RID_VOLATILE, 0 },
436 { "__volatile__", RID_VOLATILE, 0 },
437 { "__GIMPLE", RID_GIMPLE, D_CONLY },
438 { "__PHI", RID_PHI, D_CONLY },
439 { "__RTL", RID_RTL, D_CONLY },
440 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
441 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
442 { "asm", RID_ASM, D_ASM },
443 { "auto", RID_AUTO, 0 },
444 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
445 { "break", RID_BREAK, 0 },
446 { "case", RID_CASE, 0 },
447 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
448 { "char", RID_CHAR, 0 },
449 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
450 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
451 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
452 { "const", RID_CONST, 0 },
453 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
454 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
455 { "continue", RID_CONTINUE, 0 },
456 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
457 { "default", RID_DEFAULT, 0 },
458 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
459 { "do", RID_DO, 0 },
460 { "double", RID_DOUBLE, 0 },
461 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
462 { "else", RID_ELSE, 0 },
463 { "enum", RID_ENUM, 0 },
464 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
465 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
466 { "extern", RID_EXTERN, 0 },
467 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
468 { "float", RID_FLOAT, 0 },
469 { "for", RID_FOR, 0 },
470 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
471 { "goto", RID_GOTO, 0 },
472 { "if", RID_IF, 0 },
473 { "inline", RID_INLINE, D_EXT89 },
474 { "int", RID_INT, 0 },
475 { "long", RID_LONG, 0 },
476 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
477 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
478 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
479 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
480 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
481 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
482 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
483 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
484 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
485 { "register", RID_REGISTER, 0 },
486 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
487 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
488 { "return", RID_RETURN, 0 },
489 { "short", RID_SHORT, 0 },
490 { "signed", RID_SIGNED, 0 },
491 { "sizeof", RID_SIZEOF, 0 },
492 { "static", RID_STATIC, 0 },
493 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
494 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
495 { "struct", RID_STRUCT, 0 },
496 { "switch", RID_SWITCH, 0 },
497 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
498 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
499 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
500 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
501 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
502 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
503 { "typedef", RID_TYPEDEF, 0 },
504 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
505 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
506 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
507 { "union", RID_UNION, 0 },
508 { "unsigned", RID_UNSIGNED, 0 },
509 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
510 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
511 { "void", RID_VOID, 0 },
512 { "volatile", RID_VOLATILE, 0 },
513 { "wchar_t", RID_WCHAR, D_CXXONLY },
514 { "while", RID_WHILE, 0 },
515 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
516 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
518 /* C++ transactional memory. */
519 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
520 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
521 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
522 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
524 /* Concepts-related keywords */
525 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
526 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
528 /* These Objective-C keywords are recognized only immediately after
529 an '@'. */
530 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
531 { "defs", RID_AT_DEFS, D_OBJC },
532 { "encode", RID_AT_ENCODE, D_OBJC },
533 { "end", RID_AT_END, D_OBJC },
534 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
535 { "interface", RID_AT_INTERFACE, D_OBJC },
536 { "protocol", RID_AT_PROTOCOL, D_OBJC },
537 { "selector", RID_AT_SELECTOR, D_OBJC },
538 { "finally", RID_AT_FINALLY, D_OBJC },
539 { "optional", RID_AT_OPTIONAL, D_OBJC },
540 { "required", RID_AT_REQUIRED, D_OBJC },
541 { "property", RID_AT_PROPERTY, D_OBJC },
542 { "package", RID_AT_PACKAGE, D_OBJC },
543 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
544 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
545 /* These are recognized only in protocol-qualifier context
546 (see above) */
547 { "bycopy", RID_BYCOPY, D_OBJC },
548 { "byref", RID_BYREF, D_OBJC },
549 { "in", RID_IN, D_OBJC },
550 { "inout", RID_INOUT, D_OBJC },
551 { "oneway", RID_ONEWAY, D_OBJC },
552 { "out", RID_OUT, D_OBJC },
553 /* These are recognized inside a property attribute list */
554 { "assign", RID_ASSIGN, D_OBJC },
555 { "copy", RID_COPY, D_OBJC },
556 { "getter", RID_GETTER, D_OBJC },
557 { "nonatomic", RID_NONATOMIC, D_OBJC },
558 { "readonly", RID_READONLY, D_OBJC },
559 { "readwrite", RID_READWRITE, D_OBJC },
560 { "retain", RID_RETAIN, D_OBJC },
561 { "setter", RID_SETTER, D_OBJC },
564 const unsigned int num_c_common_reswords =
565 sizeof c_common_reswords / sizeof (struct c_common_resword);
567 /* Return identifier for address space AS. */
569 const char *
570 c_addr_space_name (addr_space_t as)
572 int rid = RID_FIRST_ADDR_SPACE + as;
573 gcc_assert (ridpointers [rid]);
574 return IDENTIFIER_POINTER (ridpointers [rid]);
577 /* Push current bindings for the function name VAR_DECLS. */
579 void
580 start_fname_decls (void)
582 unsigned ix;
583 tree saved = NULL_TREE;
585 for (ix = 0; fname_vars[ix].decl; ix++)
587 tree decl = *fname_vars[ix].decl;
589 if (decl)
591 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
592 saved);
593 *fname_vars[ix].decl = NULL_TREE;
596 if (saved || saved_function_name_decls)
597 /* Normally they'll have been NULL, so only push if we've got a
598 stack, or they are non-NULL. */
599 saved_function_name_decls = tree_cons (saved, NULL_TREE,
600 saved_function_name_decls);
603 /* Finish up the current bindings, adding them into the current function's
604 statement tree. This must be done _before_ finish_stmt_tree is called.
605 If there is no current function, we must be at file scope and no statements
606 are involved. Pop the previous bindings. */
608 void
609 finish_fname_decls (void)
611 unsigned ix;
612 tree stmts = NULL_TREE;
613 tree stack = saved_function_name_decls;
615 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
616 append_to_statement_list (TREE_VALUE (stack), &stmts);
618 if (stmts)
620 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
622 if (TREE_CODE (*bodyp) == BIND_EXPR)
623 bodyp = &BIND_EXPR_BODY (*bodyp);
625 append_to_statement_list_force (*bodyp, &stmts);
626 *bodyp = stmts;
629 for (ix = 0; fname_vars[ix].decl; ix++)
630 *fname_vars[ix].decl = NULL_TREE;
632 if (stack)
634 /* We had saved values, restore them. */
635 tree saved;
637 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
639 tree decl = TREE_PURPOSE (saved);
640 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
642 *fname_vars[ix].decl = decl;
644 stack = TREE_CHAIN (stack);
646 saved_function_name_decls = stack;
649 /* Return the text name of the current function, suitably prettified
650 by PRETTY_P. Return string must be freed by caller. */
652 const char *
653 fname_as_string (int pretty_p)
655 const char *name = "top level";
656 char *namep;
657 int vrb = 2, len;
658 cpp_string cstr = { 0, 0 }, strname;
660 if (!pretty_p)
662 name = "";
663 vrb = 0;
666 if (current_function_decl)
667 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
669 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
671 namep = XNEWVEC (char, len);
672 snprintf (namep, len, "\"%s\"", name);
673 strname.text = (unsigned char *) namep;
674 strname.len = len - 1;
676 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
678 XDELETEVEC (namep);
679 return (const char *) cstr.text;
682 return namep;
685 /* Return the VAR_DECL for a const char array naming the current
686 function. If the VAR_DECL has not yet been created, create it
687 now. RID indicates how it should be formatted and IDENTIFIER_NODE
688 ID is its name (unfortunately C and C++ hold the RID values of
689 keywords in different places, so we can't derive RID from ID in
690 this language independent code. LOC is the location of the
691 function. */
693 tree
694 fname_decl (location_t loc, unsigned int rid, tree id)
696 unsigned ix;
697 tree decl = NULL_TREE;
699 for (ix = 0; fname_vars[ix].decl; ix++)
700 if (fname_vars[ix].rid == rid)
701 break;
703 decl = *fname_vars[ix].decl;
704 if (!decl)
706 /* If a tree is built here, it would normally have the lineno of
707 the current statement. Later this tree will be moved to the
708 beginning of the function and this line number will be wrong.
709 To avoid this problem set the lineno to 0 here; that prevents
710 it from appearing in the RTL. */
711 tree stmts;
712 location_t saved_location = input_location;
713 input_location = UNKNOWN_LOCATION;
715 stmts = push_stmt_list ();
716 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
717 stmts = pop_stmt_list (stmts);
718 if (!IS_EMPTY_STMT (stmts))
719 saved_function_name_decls
720 = tree_cons (decl, stmts, saved_function_name_decls);
721 *fname_vars[ix].decl = decl;
722 input_location = saved_location;
724 if (!ix && !current_function_decl)
725 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
727 return decl;
730 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
732 tree
733 fix_string_type (tree value)
735 int length = TREE_STRING_LENGTH (value);
736 int nchars;
737 tree e_type, i_type, a_type;
739 /* Compute the number of elements, for the array type. */
740 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
742 nchars = length;
743 e_type = char_type_node;
745 else if (TREE_TYPE (value) == char16_array_type_node)
747 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
748 e_type = char16_type_node;
750 else if (TREE_TYPE (value) == char32_array_type_node)
752 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
753 e_type = char32_type_node;
755 else
757 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
758 e_type = wchar_type_node;
761 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
762 limit in C++98 Annex B is very large (65536) and is not normative,
763 so we do not diagnose it (warn_overlength_strings is forced off
764 in c_common_post_options). */
765 if (warn_overlength_strings)
767 const int nchars_max = flag_isoc99 ? 4095 : 509;
768 const int relevant_std = flag_isoc99 ? 99 : 90;
769 if (nchars - 1 > nchars_max)
770 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
771 separate the %d from the 'C'. 'ISO' should not be
772 translated, but it may be moved after 'C%d' in languages
773 where modifiers follow nouns. */
774 pedwarn (input_location, OPT_Woverlength_strings,
775 "string length %qd is greater than the length %qd "
776 "ISO C%d compilers are required to support",
777 nchars - 1, nchars_max, relevant_std);
780 /* Create the array type for the string constant. The ISO C++
781 standard says that a string literal has type `const char[N]' or
782 `const wchar_t[N]'. We use the same logic when invoked as a C
783 front-end with -Wwrite-strings.
784 ??? We should change the type of an expression depending on the
785 state of a warning flag. We should just be warning -- see how
786 this is handled in the C++ front-end for the deprecated implicit
787 conversion from string literals to `char*' or `wchar_t*'.
789 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
790 array type being the unqualified version of that type.
791 Therefore, if we are constructing an array of const char, we must
792 construct the matching unqualified array type first. The C front
793 end does not require this, but it does no harm, so we do it
794 unconditionally. */
795 i_type = build_index_type (size_int (nchars - 1));
796 a_type = build_array_type (e_type, i_type);
797 if (c_dialect_cxx() || warn_write_strings)
798 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
800 TREE_TYPE (value) = a_type;
801 TREE_CONSTANT (value) = 1;
802 TREE_READONLY (value) = 1;
803 TREE_STATIC (value) = 1;
804 return value;
807 /* Given a string of type STRING_TYPE, determine what kind of string
808 token would give an equivalent execution encoding: CPP_STRING,
809 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
810 This may not be exactly the string token type that initially created
811 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
812 string type at this point.
814 This effectively reverses part of the logic in lex_string and
815 fix_string_type. */
817 static enum cpp_ttype
818 get_cpp_ttype_from_string_type (tree string_type)
820 gcc_assert (string_type);
821 if (TREE_CODE (string_type) == POINTER_TYPE)
822 string_type = TREE_TYPE (string_type);
824 if (TREE_CODE (string_type) != ARRAY_TYPE)
825 return CPP_OTHER;
827 tree element_type = TREE_TYPE (string_type);
828 if (TREE_CODE (element_type) != INTEGER_TYPE)
829 return CPP_OTHER;
831 int bits_per_character = TYPE_PRECISION (element_type);
832 switch (bits_per_character)
834 case 8:
835 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
836 case 16:
837 return CPP_STRING16;
838 case 32:
839 return CPP_STRING32;
842 return CPP_OTHER;
845 /* The global record of string concatentations, for use in
846 extracting locations within string literals. */
848 GTY(()) string_concat_db *g_string_concat_db;
850 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
852 const char *
853 c_get_substring_location (const substring_loc &substr_loc,
854 location_t *out_loc)
856 enum cpp_ttype tok_type
857 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
858 if (tok_type == CPP_OTHER)
859 return "unrecognized string type";
861 return get_source_location_for_substring (parse_in, g_string_concat_db,
862 substr_loc.get_fmt_string_loc (),
863 tok_type,
864 substr_loc.get_caret_idx (),
865 substr_loc.get_start_idx (),
866 substr_loc.get_end_idx (),
867 out_loc);
871 /* Fold X for consideration by one of the warning functions when checking
872 whether an expression has a constant value. */
874 tree
875 fold_for_warn (tree x)
877 if (c_dialect_cxx ())
878 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
879 else
880 /* The C front-end has already folded X appropriately. */
881 return x;
884 /* Return true iff T is a boolean promoted to int. */
886 bool
887 bool_promoted_to_int_p (tree t)
889 return (CONVERT_EXPR_P (t)
890 && TREE_TYPE (t) == integer_type_node
891 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
894 /* vector_targets_convertible_p is used for vector pointer types. The
895 callers perform various checks that the qualifiers are satisfactory,
896 while OTOH vector_targets_convertible_p ignores the number of elements
897 in the vectors. That's fine with vector pointers as we can consider,
898 say, a vector of 8 elements as two consecutive vectors of 4 elements,
899 and that does not require and conversion of the pointer values.
900 In contrast, vector_types_convertible_p and
901 vector_types_compatible_elements_p are used for vector value types. */
902 /* True if pointers to distinct types T1 and T2 can be converted to
903 each other without an explicit cast. Only returns true for opaque
904 vector types. */
905 bool
906 vector_targets_convertible_p (const_tree t1, const_tree t2)
908 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
909 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
910 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
911 return true;
913 return false;
916 /* vector_types_convertible_p is used for vector value types.
917 It could in principle call vector_targets_convertible_p as a subroutine,
918 but then the check for vector type would be duplicated with its callers,
919 and also the purpose of vector_targets_convertible_p would become
920 muddled.
921 Where vector_types_convertible_p returns true, a conversion might still be
922 needed to make the types match.
923 In contrast, vector_targets_convertible_p is used for vector pointer
924 values, and vector_types_compatible_elements_p is used specifically
925 in the context for binary operators, as a check if use is possible without
926 conversion. */
927 /* True if vector types T1 and T2 can be converted to each other
928 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
929 can only be converted with -flax-vector-conversions yet that is not
930 in effect, emit a note telling the user about that option if such
931 a note has not previously been emitted. */
932 bool
933 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
935 static bool emitted_lax_note = false;
936 bool convertible_lax;
938 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
939 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
940 return true;
942 convertible_lax =
943 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
944 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
945 || known_eq (TYPE_VECTOR_SUBPARTS (t1),
946 TYPE_VECTOR_SUBPARTS (t2)))
947 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
948 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
950 if (!convertible_lax || flag_lax_vector_conversions)
951 return convertible_lax;
953 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
954 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
955 return true;
957 if (emit_lax_note && !emitted_lax_note)
959 emitted_lax_note = true;
960 inform (input_location, "use -flax-vector-conversions to permit "
961 "conversions between vectors with differing "
962 "element types or numbers of subparts");
965 return false;
968 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
969 and have vector types, V0 has the same type as V1, and the number of
970 elements of V0, V1, MASK is the same.
972 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
973 called with two arguments. In this case implementation passes the
974 first argument twice in order to share the same tree code. This fact
975 could enable the mask-values being twice the vector length. This is
976 an implementation accident and this semantics is not guaranteed to
977 the user. */
978 tree
979 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
980 bool complain)
982 tree ret;
983 bool wrap = true;
984 bool maybe_const = false;
985 bool two_arguments = false;
987 if (v1 == NULL_TREE)
989 two_arguments = true;
990 v1 = v0;
993 if (v0 == error_mark_node || v1 == error_mark_node
994 || mask == error_mark_node)
995 return error_mark_node;
997 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
999 if (complain)
1000 error_at (loc, "__builtin_shuffle last argument must "
1001 "be an integer vector");
1002 return error_mark_node;
1005 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1006 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1008 if (complain)
1009 error_at (loc, "__builtin_shuffle arguments must be vectors");
1010 return error_mark_node;
1013 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1015 if (complain)
1016 error_at (loc, "__builtin_shuffle argument vectors must be of "
1017 "the same type");
1018 return error_mark_node;
1021 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1022 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1023 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1024 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1026 if (complain)
1027 error_at (loc, "__builtin_shuffle number of elements of the "
1028 "argument vector(s) and the mask vector should "
1029 "be the same");
1030 return error_mark_node;
1033 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1034 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1036 if (complain)
1037 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1038 "must have the same size as inner type of the mask");
1039 return error_mark_node;
1042 if (!c_dialect_cxx ())
1044 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1045 v0 = c_fully_fold (v0, false, &maybe_const);
1046 wrap &= maybe_const;
1048 if (two_arguments)
1049 v1 = v0 = save_expr (v0);
1050 else
1052 v1 = c_fully_fold (v1, false, &maybe_const);
1053 wrap &= maybe_const;
1056 mask = c_fully_fold (mask, false, &maybe_const);
1057 wrap &= maybe_const;
1059 else if (two_arguments)
1060 v1 = v0 = save_expr (v0);
1062 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1064 if (!c_dialect_cxx () && !wrap)
1065 ret = c_wrap_maybe_const (ret, true);
1067 return ret;
1070 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1071 to integral type. */
1073 tree
1074 c_common_get_narrower (tree op, int *unsignedp_ptr)
1076 op = get_narrower (op, unsignedp_ptr);
1078 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1079 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1081 /* C++0x scoped enumerations don't implicitly convert to integral
1082 type; if we stripped an explicit conversion to a larger type we
1083 need to replace it so common_type will still work. */
1084 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1085 TYPE_UNSIGNED (TREE_TYPE (op)));
1086 op = fold_convert (type, op);
1088 return op;
1091 /* This is a helper function of build_binary_op.
1093 For certain operations if both args were extended from the same
1094 smaller type, do the arithmetic in that type and then extend.
1096 BITWISE indicates a bitwise operation.
1097 For them, this optimization is safe only if
1098 both args are zero-extended or both are sign-extended.
1099 Otherwise, we might change the result.
1100 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1101 but calculated in (unsigned short) it would be (unsigned short)-1.
1103 tree
1104 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1106 int unsigned0, unsigned1;
1107 tree arg0, arg1;
1108 int uns;
1109 tree type;
1111 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1112 excessive narrowing when we call get_narrower below. For
1113 example, suppose that OP0 is of unsigned int extended
1114 from signed char and that RESULT_TYPE is long long int.
1115 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1116 like
1118 (long long int) (unsigned int) signed_char
1120 which get_narrower would narrow down to
1122 (unsigned int) signed char
1124 If we do not cast OP0 first, get_narrower would return
1125 signed_char, which is inconsistent with the case of the
1126 explicit cast. */
1127 op0 = convert (result_type, op0);
1128 op1 = convert (result_type, op1);
1130 arg0 = c_common_get_narrower (op0, &unsigned0);
1131 arg1 = c_common_get_narrower (op1, &unsigned1);
1133 /* UNS is 1 if the operation to be done is an unsigned one. */
1134 uns = TYPE_UNSIGNED (result_type);
1136 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1137 but it *requires* conversion to FINAL_TYPE. */
1139 if ((TYPE_PRECISION (TREE_TYPE (op0))
1140 == TYPE_PRECISION (TREE_TYPE (arg0)))
1141 && TREE_TYPE (op0) != result_type)
1142 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1143 if ((TYPE_PRECISION (TREE_TYPE (op1))
1144 == TYPE_PRECISION (TREE_TYPE (arg1)))
1145 && TREE_TYPE (op1) != result_type)
1146 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1148 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1150 /* For bitwise operations, signedness of nominal type
1151 does not matter. Consider only how operands were extended. */
1152 if (bitwise)
1153 uns = unsigned0;
1155 /* Note that in all three cases below we refrain from optimizing
1156 an unsigned operation on sign-extended args.
1157 That would not be valid. */
1159 /* Both args variable: if both extended in same way
1160 from same width, do it in that width.
1161 Do it unsigned if args were zero-extended. */
1162 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1163 < TYPE_PRECISION (result_type))
1164 && (TYPE_PRECISION (TREE_TYPE (arg1))
1165 == TYPE_PRECISION (TREE_TYPE (arg0)))
1166 && unsigned0 == unsigned1
1167 && (unsigned0 || !uns))
1168 return c_common_signed_or_unsigned_type
1169 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1171 else if (TREE_CODE (arg0) == INTEGER_CST
1172 && (unsigned1 || !uns)
1173 && (TYPE_PRECISION (TREE_TYPE (arg1))
1174 < TYPE_PRECISION (result_type))
1175 && (type
1176 = c_common_signed_or_unsigned_type (unsigned1,
1177 TREE_TYPE (arg1)))
1178 && !POINTER_TYPE_P (type)
1179 && int_fits_type_p (arg0, type))
1180 return type;
1182 else if (TREE_CODE (arg1) == INTEGER_CST
1183 && (unsigned0 || !uns)
1184 && (TYPE_PRECISION (TREE_TYPE (arg0))
1185 < TYPE_PRECISION (result_type))
1186 && (type
1187 = c_common_signed_or_unsigned_type (unsigned0,
1188 TREE_TYPE (arg0)))
1189 && !POINTER_TYPE_P (type)
1190 && int_fits_type_p (arg1, type))
1191 return type;
1193 return result_type;
1196 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1197 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1199 static bool
1200 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1202 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1203 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1204 REAL_VALUE_TYPE real_low_bound =
1205 real_value_from_int_cst (0, type_low_bound);
1206 REAL_VALUE_TYPE real_high_bound =
1207 real_value_from_int_cst (0, type_high_bound);
1209 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1210 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1213 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1214 to the complex/real/integer type TYPE. Function returns non-zero when:
1215 * EXPR is a constant which cannot be exactly converted to TYPE.
1216 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1217 for EXPR type and TYPE being both integers or both real, or both
1218 complex.
1219 * EXPR is not a constant of complex type and TYPE is a real or
1220 an integer.
1221 * EXPR is not a constant of real type and TYPE is an integer.
1222 * EXPR is not a constant of integer type which cannot be
1223 exactly converted to real type.
1225 Function allows conversions between types of different signedness and
1226 can return SAFE_CONVERSION (zero) in that case. Function can produce
1227 signedness warnings if PRODUCE_WARNS is true.
1229 RESULT, when non-null is the result of the conversion. When constant
1230 it is included in the text of diagnostics.
1232 Function allows conversions from complex constants to non-complex types,
1233 provided that imaginary part is zero and real part can be safely converted
1234 to TYPE. */
1236 enum conversion_safety
1237 unsafe_conversion_p (location_t loc, tree type, tree expr, tree result,
1238 bool produce_warns)
1240 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1241 tree expr_type = TREE_TYPE (expr);
1243 bool cstresult = (result
1244 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1246 loc = expansion_point_location_if_in_system_header (loc);
1248 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1250 /* If type is complex, we are interested in compatibility with
1251 underlying type. */
1252 if (TREE_CODE (type) == COMPLEX_TYPE)
1253 type = TREE_TYPE (type);
1255 /* Warn for real constant that is not an exact integer converted
1256 to integer type. */
1257 if (TREE_CODE (expr_type) == REAL_TYPE
1258 && TREE_CODE (type) == INTEGER_TYPE)
1260 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1261 give_warning = UNSAFE_REAL;
1263 /* Warn for an integer constant that does not fit into integer type. */
1264 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1265 && TREE_CODE (type) == INTEGER_TYPE
1266 && !int_fits_type_p (expr, type))
1268 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1269 && tree_int_cst_sgn (expr) < 0)
1271 if (produce_warns)
1273 if (cstresult)
1274 warning_at (loc, OPT_Wsign_conversion,
1275 "unsigned conversion from %qT to %qT "
1276 "changes value from %qE to %qE",
1277 expr_type, type, expr, result);
1278 else
1279 warning_at (loc, OPT_Wsign_conversion,
1280 "unsigned conversion from %qT to %qT "
1281 "changes the value of %qE",
1282 expr_type, type, expr);
1285 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1287 if (cstresult)
1288 warning_at (loc, OPT_Wsign_conversion,
1289 "signed conversion from %qT to %qT changes "
1290 "value from %qE to %qE",
1291 expr_type, type, expr, result);
1292 else
1293 warning_at (loc, OPT_Wsign_conversion,
1294 "signed conversion from %qT to %qT changes "
1295 "the value of %qE",
1296 expr_type, type, expr);
1298 else
1299 give_warning = UNSAFE_OTHER;
1301 else if (TREE_CODE (type) == REAL_TYPE)
1303 /* Warn for an integer constant that does not fit into real type. */
1304 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1306 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1307 if (!exact_real_truncate (TYPE_MODE (type), &a))
1308 give_warning = UNSAFE_REAL;
1310 /* Warn for a real constant that does not fit into a smaller
1311 real type. */
1312 else if (TREE_CODE (expr_type) == REAL_TYPE
1313 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1315 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1316 if (!exact_real_truncate (TYPE_MODE (type), &a))
1317 give_warning = UNSAFE_REAL;
1322 else if (TREE_CODE (expr) == COMPLEX_CST)
1324 tree imag_part = TREE_IMAGPART (expr);
1325 /* Conversion from complex constant with zero imaginary part,
1326 perform check for conversion of real part. */
1327 if ((TREE_CODE (imag_part) == REAL_CST
1328 && real_zerop (imag_part))
1329 || (TREE_CODE (imag_part) == INTEGER_CST
1330 && integer_zerop (imag_part)))
1331 /* Note: in this branch we use recursive call to unsafe_conversion_p
1332 with different type of EXPR, but it is still safe, because when EXPR
1333 is a constant, it's type is not used in text of generated warnings
1334 (otherwise they could sound misleading). */
1335 return unsafe_conversion_p (loc, type, TREE_REALPART (expr), result,
1336 produce_warns);
1337 /* Conversion from complex constant with non-zero imaginary part. */
1338 else
1340 /* Conversion to complex type.
1341 Perform checks for both real and imaginary parts. */
1342 if (TREE_CODE (type) == COMPLEX_TYPE)
1344 /* Unfortunately, produce_warns must be false in two subsequent
1345 calls of unsafe_conversion_p, because otherwise we could
1346 produce strange "double" warnings, if both real and imaginary
1347 parts have conversion problems related to signedness.
1349 For example:
1350 int32_t _Complex a = 0x80000000 + 0x80000000i;
1352 Possible solution: add a separate function for checking
1353 constants and combine result of two calls appropriately. */
1354 enum conversion_safety re_safety =
1355 unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1356 result, false);
1357 enum conversion_safety im_safety =
1358 unsafe_conversion_p (loc, type, imag_part, result, false);
1360 /* Merge the results into appropriate single warning. */
1362 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1363 if (re_safety == im_safety)
1364 give_warning = re_safety;
1365 else if (!re_safety && im_safety)
1366 give_warning = im_safety;
1367 else if (re_safety && !im_safety)
1368 give_warning = re_safety;
1369 else
1370 give_warning = UNSAFE_OTHER;
1372 /* Warn about conversion from complex to real or integer type. */
1373 else
1374 give_warning = UNSAFE_IMAGINARY;
1378 /* Checks for remaining case: EXPR is not constant. */
1379 else
1381 /* Warn for real types converted to integer types. */
1382 if (TREE_CODE (expr_type) == REAL_TYPE
1383 && TREE_CODE (type) == INTEGER_TYPE)
1384 give_warning = UNSAFE_REAL;
1386 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1387 && TREE_CODE (type) == INTEGER_TYPE)
1389 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1390 expr = get_unwidened (expr, 0);
1391 expr_type = TREE_TYPE (expr);
1393 /* Don't warn for short y; short x = ((int)y & 0xff); */
1394 if (TREE_CODE (expr) == BIT_AND_EXPR
1395 || TREE_CODE (expr) == BIT_IOR_EXPR
1396 || TREE_CODE (expr) == BIT_XOR_EXPR)
1398 /* If both args were extended from a shortest type,
1399 use that type if that is safe. */
1400 expr_type = shorten_binary_op (expr_type,
1401 TREE_OPERAND (expr, 0),
1402 TREE_OPERAND (expr, 1),
1403 /* bitwise */1);
1405 if (TREE_CODE (expr) == BIT_AND_EXPR)
1407 tree op0 = TREE_OPERAND (expr, 0);
1408 tree op1 = TREE_OPERAND (expr, 1);
1409 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1410 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1412 /* If one of the operands is a non-negative constant
1413 that fits in the target type, then the type of the
1414 other operand does not matter. */
1415 if ((TREE_CODE (op0) == INTEGER_CST
1416 && int_fits_type_p (op0, c_common_signed_type (type))
1417 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1418 || (TREE_CODE (op1) == INTEGER_CST
1419 && int_fits_type_p (op1, c_common_signed_type (type))
1420 && int_fits_type_p (op1,
1421 c_common_unsigned_type (type))))
1422 return SAFE_CONVERSION;
1423 /* If constant is unsigned and fits in the target
1424 type, then the result will also fit. */
1425 else if ((TREE_CODE (op0) == INTEGER_CST
1426 && unsigned0
1427 && int_fits_type_p (op0, type))
1428 || (TREE_CODE (op1) == INTEGER_CST
1429 && unsigned1
1430 && int_fits_type_p (op1, type)))
1431 return SAFE_CONVERSION;
1434 /* Warn for integer types converted to smaller integer types. */
1435 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1436 give_warning = UNSAFE_OTHER;
1438 /* When they are the same width but different signedness,
1439 then the value may change. */
1440 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1441 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1442 /* Even when converted to a bigger type, if the type is
1443 unsigned but expr is signed, then negative values
1444 will be changed. */
1445 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1446 && produce_warns)
1447 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1448 "may change the sign of the result",
1449 type, expr_type);
1452 /* Warn for integer types converted to real types if and only if
1453 all the range of values of the integer type cannot be
1454 represented by the real type. */
1455 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1456 && TREE_CODE (type) == REAL_TYPE)
1458 /* Don't warn about char y = 0xff; float x = (int) y; */
1459 expr = get_unwidened (expr, 0);
1460 expr_type = TREE_TYPE (expr);
1462 if (!int_safely_convertible_to_real_p (expr_type, type))
1463 give_warning = UNSAFE_OTHER;
1466 /* Warn for real types converted to smaller real types. */
1467 else if (TREE_CODE (expr_type) == REAL_TYPE
1468 && TREE_CODE (type) == REAL_TYPE
1469 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1470 give_warning = UNSAFE_REAL;
1472 /* Check conversion between two complex types. */
1473 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1474 && TREE_CODE (type) == COMPLEX_TYPE)
1476 /* Extract underlying types (i.e., type of real and imaginary
1477 parts) of expr_type and type. */
1478 tree from_type = TREE_TYPE (expr_type);
1479 tree to_type = TREE_TYPE (type);
1481 /* Warn for real types converted to integer types. */
1482 if (TREE_CODE (from_type) == REAL_TYPE
1483 && TREE_CODE (to_type) == INTEGER_TYPE)
1484 give_warning = UNSAFE_REAL;
1486 /* Warn for real types converted to smaller real types. */
1487 else if (TREE_CODE (from_type) == REAL_TYPE
1488 && TREE_CODE (to_type) == REAL_TYPE
1489 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1490 give_warning = UNSAFE_REAL;
1492 /* Check conversion for complex integer types. Here implementation
1493 is simpler than for real-domain integers because it does not
1494 involve sophisticated cases, such as bitmasks, casts, etc. */
1495 else if (TREE_CODE (from_type) == INTEGER_TYPE
1496 && TREE_CODE (to_type) == INTEGER_TYPE)
1498 /* Warn for integer types converted to smaller integer types. */
1499 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1500 give_warning = UNSAFE_OTHER;
1502 /* Check for different signedness, see case for real-domain
1503 integers (above) for a more detailed comment. */
1504 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1505 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1506 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1507 && produce_warns)
1508 warning_at (loc, OPT_Wsign_conversion,
1509 "conversion to %qT from %qT "
1510 "may change the sign of the result",
1511 type, expr_type);
1513 else if (TREE_CODE (from_type) == INTEGER_TYPE
1514 && TREE_CODE (to_type) == REAL_TYPE
1515 && !int_safely_convertible_to_real_p (from_type, to_type))
1516 give_warning = UNSAFE_OTHER;
1519 /* Warn for complex types converted to real or integer types. */
1520 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1521 && TREE_CODE (type) != COMPLEX_TYPE)
1522 give_warning = UNSAFE_IMAGINARY;
1525 return give_warning;
1529 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1530 Invoke this function on every expression that is converted implicitly,
1531 i.e. because of language rules and not because of an explicit cast. */
1533 tree
1534 convert_and_check (location_t loc, tree type, tree expr)
1536 tree result;
1537 tree expr_for_warning;
1539 /* Convert from a value with possible excess precision rather than
1540 via the semantic type, but do not warn about values not fitting
1541 exactly in the semantic type. */
1542 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1544 tree orig_type = TREE_TYPE (expr);
1545 expr = TREE_OPERAND (expr, 0);
1546 expr_for_warning = convert (orig_type, expr);
1547 if (orig_type == type)
1548 return expr_for_warning;
1550 else
1551 expr_for_warning = expr;
1553 if (TREE_TYPE (expr) == type)
1554 return expr;
1556 result = convert (type, expr);
1558 if (c_inhibit_evaluation_warnings == 0
1559 && !TREE_OVERFLOW_P (expr)
1560 && result != error_mark_node)
1561 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1563 return result;
1566 /* A node in a list that describes references to variables (EXPR), which are
1567 either read accesses if WRITER is zero, or write accesses, in which case
1568 WRITER is the parent of EXPR. */
1569 struct tlist
1571 struct tlist *next;
1572 tree expr, writer;
1575 /* Used to implement a cache the results of a call to verify_tree. We only
1576 use this for SAVE_EXPRs. */
1577 struct tlist_cache
1579 struct tlist_cache *next;
1580 struct tlist *cache_before_sp;
1581 struct tlist *cache_after_sp;
1582 tree expr;
1585 /* Obstack to use when allocating tlist structures, and corresponding
1586 firstobj. */
1587 static struct obstack tlist_obstack;
1588 static char *tlist_firstobj = 0;
1590 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1591 warnings. */
1592 static struct tlist *warned_ids;
1593 /* SAVE_EXPRs need special treatment. We process them only once and then
1594 cache the results. */
1595 static struct tlist_cache *save_expr_cache;
1597 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1598 static void merge_tlist (struct tlist **, struct tlist *, int);
1599 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1600 static bool warning_candidate_p (tree);
1601 static bool candidate_equal_p (const_tree, const_tree);
1602 static void warn_for_collisions (struct tlist *);
1603 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1604 static struct tlist *new_tlist (struct tlist *, tree, tree);
1606 /* Create a new struct tlist and fill in its fields. */
1607 static struct tlist *
1608 new_tlist (struct tlist *next, tree t, tree writer)
1610 struct tlist *l;
1611 l = XOBNEW (&tlist_obstack, struct tlist);
1612 l->next = next;
1613 l->expr = t;
1614 l->writer = writer;
1615 return l;
1618 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1619 is nonnull, we ignore any node we find which has a writer equal to it. */
1621 static void
1622 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1624 while (add)
1626 struct tlist *next = add->next;
1627 if (!copy)
1628 add->next = *to;
1629 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1630 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1631 add = next;
1635 /* Merge the nodes of ADD into TO. This merging process is done so that for
1636 each variable that already exists in TO, no new node is added; however if
1637 there is a write access recorded in ADD, and an occurrence on TO is only
1638 a read access, then the occurrence in TO will be modified to record the
1639 write. */
1641 static void
1642 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1644 struct tlist **end = to;
1646 while (*end)
1647 end = &(*end)->next;
1649 while (add)
1651 int found = 0;
1652 struct tlist *tmp2;
1653 struct tlist *next = add->next;
1655 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1656 if (candidate_equal_p (tmp2->expr, add->expr))
1658 found = 1;
1659 if (!tmp2->writer)
1660 tmp2->writer = add->writer;
1662 if (!found)
1664 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1665 end = &(*end)->next;
1666 *end = 0;
1668 add = next;
1672 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1673 references in list LIST conflict with it, excluding reads if ONLY writers
1674 is nonzero. */
1676 static void
1677 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1678 int only_writes)
1680 struct tlist *tmp;
1682 /* Avoid duplicate warnings. */
1683 for (tmp = warned_ids; tmp; tmp = tmp->next)
1684 if (candidate_equal_p (tmp->expr, written))
1685 return;
1687 while (list)
1689 if (candidate_equal_p (list->expr, written)
1690 && !candidate_equal_p (list->writer, writer)
1691 && (!only_writes || list->writer))
1693 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1694 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1695 OPT_Wsequence_point, "operation on %qE may be undefined",
1696 list->expr);
1698 list = list->next;
1702 /* Given a list LIST of references to variables, find whether any of these
1703 can cause conflicts due to missing sequence points. */
1705 static void
1706 warn_for_collisions (struct tlist *list)
1708 struct tlist *tmp;
1710 for (tmp = list; tmp; tmp = tmp->next)
1712 if (tmp->writer)
1713 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1717 /* Return nonzero if X is a tree that can be verified by the sequence point
1718 warnings. */
1720 static bool
1721 warning_candidate_p (tree x)
1723 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1724 return false;
1726 if (TREE_CODE (x) == BLOCK)
1727 return false;
1729 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1730 (lvalue_p) crash on TRY/CATCH. */
1731 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1732 return false;
1734 if (!lvalue_p (x))
1735 return false;
1737 /* No point to track non-const calls, they will never satisfy
1738 operand_equal_p. */
1739 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1740 return false;
1742 if (TREE_CODE (x) == STRING_CST)
1743 return false;
1745 return true;
1748 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1749 static bool
1750 candidate_equal_p (const_tree x, const_tree y)
1752 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1755 /* Walk the tree X, and record accesses to variables. If X is written by the
1756 parent tree, WRITER is the parent.
1757 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1758 expression or its only operand forces a sequence point, then everything up
1759 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1760 in PNO_SP.
1761 Once we return, we will have emitted warnings if any subexpression before
1762 such a sequence point could be undefined. On a higher level, however, the
1763 sequence point may not be relevant, and we'll merge the two lists.
1765 Example: (b++, a) + b;
1766 The call that processes the COMPOUND_EXPR will store the increment of B
1767 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1768 processes the PLUS_EXPR will need to merge the two lists so that
1769 eventually, all accesses end up on the same list (and we'll warn about the
1770 unordered subexpressions b++ and b.
1772 A note on merging. If we modify the former example so that our expression
1773 becomes
1774 (b++, b) + a
1775 care must be taken not simply to add all three expressions into the final
1776 PNO_SP list. The function merge_tlist takes care of that by merging the
1777 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1778 way, so that no more than one access to B is recorded. */
1780 static void
1781 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1782 tree writer)
1784 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1785 enum tree_code code;
1786 enum tree_code_class cl;
1788 /* X may be NULL if it is the operand of an empty statement expression
1789 ({ }). */
1790 if (x == NULL)
1791 return;
1793 restart:
1794 code = TREE_CODE (x);
1795 cl = TREE_CODE_CLASS (code);
1797 if (warning_candidate_p (x))
1798 *pno_sp = new_tlist (*pno_sp, x, writer);
1800 switch (code)
1802 case CONSTRUCTOR:
1803 case SIZEOF_EXPR:
1804 return;
1806 case COMPOUND_EXPR:
1807 case TRUTH_ANDIF_EXPR:
1808 case TRUTH_ORIF_EXPR:
1809 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1810 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1811 warn_for_collisions (tmp_nosp);
1812 merge_tlist (pbefore_sp, tmp_before, 0);
1813 merge_tlist (pbefore_sp, tmp_nosp, 0);
1814 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1815 warn_for_collisions (tmp_list2);
1816 merge_tlist (pbefore_sp, tmp_list3, 0);
1817 merge_tlist (pno_sp, tmp_list2, 0);
1818 return;
1820 case COND_EXPR:
1821 tmp_before = tmp_list2 = 0;
1822 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1823 warn_for_collisions (tmp_list2);
1824 merge_tlist (pbefore_sp, tmp_before, 0);
1825 merge_tlist (pbefore_sp, tmp_list2, 0);
1827 tmp_list3 = tmp_nosp = 0;
1828 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1829 warn_for_collisions (tmp_nosp);
1830 merge_tlist (pbefore_sp, tmp_list3, 0);
1832 tmp_list3 = tmp_list2 = 0;
1833 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1834 warn_for_collisions (tmp_list2);
1835 merge_tlist (pbefore_sp, tmp_list3, 0);
1836 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1837 two first, to avoid warning for (a ? b++ : b++). */
1838 merge_tlist (&tmp_nosp, tmp_list2, 0);
1839 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1840 return;
1842 case PREDECREMENT_EXPR:
1843 case PREINCREMENT_EXPR:
1844 case POSTDECREMENT_EXPR:
1845 case POSTINCREMENT_EXPR:
1846 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1847 return;
1849 case MODIFY_EXPR:
1850 tmp_before = tmp_nosp = tmp_list3 = 0;
1851 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1852 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1853 /* Expressions inside the LHS are not ordered wrt. the sequence points
1854 in the RHS. Example:
1855 *a = (a++, 2)
1856 Despite the fact that the modification of "a" is in the before_sp
1857 list (tmp_before), it conflicts with the use of "a" in the LHS.
1858 We can handle this by adding the contents of tmp_list3
1859 to those of tmp_before, and redoing the collision warnings for that
1860 list. */
1861 add_tlist (&tmp_before, tmp_list3, x, 1);
1862 warn_for_collisions (tmp_before);
1863 /* Exclude the LHS itself here; we first have to merge it into the
1864 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1865 didn't exclude the LHS, we'd get it twice, once as a read and once
1866 as a write. */
1867 add_tlist (pno_sp, tmp_list3, x, 0);
1868 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1870 merge_tlist (pbefore_sp, tmp_before, 0);
1871 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1872 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1873 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1874 return;
1876 case CALL_EXPR:
1877 /* We need to warn about conflicts among arguments and conflicts between
1878 args and the function address. Side effects of the function address,
1879 however, are not ordered by the sequence point of the call. */
1881 call_expr_arg_iterator iter;
1882 tree arg;
1883 tmp_before = tmp_nosp = 0;
1884 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1885 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1887 tmp_list2 = tmp_list3 = 0;
1888 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1889 merge_tlist (&tmp_list3, tmp_list2, 0);
1890 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1892 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1893 warn_for_collisions (tmp_before);
1894 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1895 return;
1898 case TREE_LIST:
1899 /* Scan all the list, e.g. indices of multi dimensional array. */
1900 while (x)
1902 tmp_before = tmp_nosp = 0;
1903 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1904 merge_tlist (&tmp_nosp, tmp_before, 0);
1905 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1906 x = TREE_CHAIN (x);
1908 return;
1910 case SAVE_EXPR:
1912 struct tlist_cache *t;
1913 for (t = save_expr_cache; t; t = t->next)
1914 if (candidate_equal_p (t->expr, x))
1915 break;
1917 if (!t)
1919 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1920 t->next = save_expr_cache;
1921 t->expr = x;
1922 save_expr_cache = t;
1924 tmp_before = tmp_nosp = 0;
1925 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1926 warn_for_collisions (tmp_nosp);
1928 tmp_list3 = 0;
1929 merge_tlist (&tmp_list3, tmp_nosp, 0);
1930 t->cache_before_sp = tmp_before;
1931 t->cache_after_sp = tmp_list3;
1933 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1934 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1935 return;
1938 case ADDR_EXPR:
1939 x = TREE_OPERAND (x, 0);
1940 if (DECL_P (x))
1941 return;
1942 writer = 0;
1943 goto restart;
1945 default:
1946 /* For other expressions, simply recurse on their operands.
1947 Manual tail recursion for unary expressions.
1948 Other non-expressions need not be processed. */
1949 if (cl == tcc_unary)
1951 x = TREE_OPERAND (x, 0);
1952 writer = 0;
1953 goto restart;
1955 else if (IS_EXPR_CODE_CLASS (cl))
1957 int lp;
1958 int max = TREE_OPERAND_LENGTH (x);
1959 for (lp = 0; lp < max; lp++)
1961 tmp_before = tmp_nosp = 0;
1962 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1963 merge_tlist (&tmp_nosp, tmp_before, 0);
1964 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1967 return;
1971 /* Try to warn for undefined behavior in EXPR due to missing sequence
1972 points. */
1974 DEBUG_FUNCTION void
1975 verify_sequence_points (tree expr)
1977 struct tlist *before_sp = 0, *after_sp = 0;
1979 warned_ids = 0;
1980 save_expr_cache = 0;
1981 if (tlist_firstobj == 0)
1983 gcc_obstack_init (&tlist_obstack);
1984 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1987 verify_tree (expr, &before_sp, &after_sp, 0);
1988 warn_for_collisions (after_sp);
1989 obstack_free (&tlist_obstack, tlist_firstobj);
1992 /* Validate the expression after `case' and apply default promotions. */
1994 static tree
1995 check_case_value (location_t loc, tree value)
1997 if (value == NULL_TREE)
1998 return value;
2000 if (TREE_CODE (value) == INTEGER_CST)
2001 /* Promote char or short to int. */
2002 value = perform_integral_promotions (value);
2003 else if (value != error_mark_node)
2005 error_at (loc, "case label does not reduce to an integer constant");
2006 value = error_mark_node;
2009 constant_expression_warning (value);
2011 return value;
2014 /* See if the case values LOW and HIGH are in the range of the original
2015 type (i.e. before the default conversion to int) of the switch testing
2016 expression.
2017 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2018 the type before promoting it. CASE_LOW_P is a pointer to the lower
2019 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2020 if the case is not a case range.
2021 The caller has to make sure that we are not called with NULL for
2022 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
2023 was a case value that doesn't fit into the range of the ORIG_TYPE.
2024 Returns true if the case label is in range of ORIG_TYPE (saturated or
2025 untouched) or false if the label is out of range. */
2027 static bool
2028 check_case_bounds (location_t loc, tree type, tree orig_type,
2029 tree *case_low_p, tree *case_high_p,
2030 bool *outside_range_p)
2032 tree min_value, max_value;
2033 tree case_low = *case_low_p;
2034 tree case_high = case_high_p ? *case_high_p : case_low;
2036 /* If there was a problem with the original type, do nothing. */
2037 if (orig_type == error_mark_node)
2038 return true;
2040 min_value = TYPE_MIN_VALUE (orig_type);
2041 max_value = TYPE_MAX_VALUE (orig_type);
2043 /* We'll really need integer constants here. */
2044 case_low = fold (case_low);
2045 case_high = fold (case_high);
2047 /* Case label is less than minimum for type. */
2048 if (tree_int_cst_compare (case_low, min_value) < 0
2049 && tree_int_cst_compare (case_high, min_value) < 0)
2051 warning_at (loc, 0, "case label value is less than minimum value "
2052 "for type");
2053 *outside_range_p = true;
2054 return false;
2057 /* Case value is greater than maximum for type. */
2058 if (tree_int_cst_compare (case_low, max_value) > 0
2059 && tree_int_cst_compare (case_high, max_value) > 0)
2061 warning_at (loc, 0, "case label value exceeds maximum value for type");
2062 *outside_range_p = true;
2063 return false;
2066 /* Saturate lower case label value to minimum. */
2067 if (tree_int_cst_compare (case_high, min_value) >= 0
2068 && tree_int_cst_compare (case_low, min_value) < 0)
2070 warning_at (loc, 0, "lower value in case label range"
2071 " less than minimum value for type");
2072 *outside_range_p = true;
2073 case_low = min_value;
2076 /* Saturate upper case label value to maximum. */
2077 if (tree_int_cst_compare (case_low, max_value) <= 0
2078 && tree_int_cst_compare (case_high, max_value) > 0)
2080 warning_at (loc, 0, "upper value in case label range"
2081 " exceeds maximum value for type");
2082 *outside_range_p = true;
2083 case_high = max_value;
2086 if (*case_low_p != case_low)
2087 *case_low_p = convert (type, case_low);
2088 if (case_high_p && *case_high_p != case_high)
2089 *case_high_p = convert (type, case_high);
2091 return true;
2094 /* Return an integer type with BITS bits of precision,
2095 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2097 tree
2098 c_common_type_for_size (unsigned int bits, int unsignedp)
2100 int i;
2102 if (bits == TYPE_PRECISION (integer_type_node))
2103 return unsignedp ? unsigned_type_node : integer_type_node;
2105 if (bits == TYPE_PRECISION (signed_char_type_node))
2106 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2108 if (bits == TYPE_PRECISION (short_integer_type_node))
2109 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2111 if (bits == TYPE_PRECISION (long_integer_type_node))
2112 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2114 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2115 return (unsignedp ? long_long_unsigned_type_node
2116 : long_long_integer_type_node);
2118 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2119 if (int_n_enabled_p[i]
2120 && bits == int_n_data[i].bitsize)
2121 return (unsignedp ? int_n_trees[i].unsigned_type
2122 : int_n_trees[i].signed_type);
2124 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2125 return (unsignedp ? widest_unsigned_literal_type_node
2126 : widest_integer_literal_type_node);
2128 if (bits <= TYPE_PRECISION (intQI_type_node))
2129 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2131 if (bits <= TYPE_PRECISION (intHI_type_node))
2132 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2134 if (bits <= TYPE_PRECISION (intSI_type_node))
2135 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2137 if (bits <= TYPE_PRECISION (intDI_type_node))
2138 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2140 return NULL_TREE;
2143 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2144 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2145 and saturating if SATP is nonzero, otherwise not saturating. */
2147 tree
2148 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2149 int unsignedp, int satp)
2151 enum mode_class mclass;
2152 if (ibit == 0)
2153 mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2154 else
2155 mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2157 opt_scalar_mode opt_mode;
2158 scalar_mode mode;
2159 FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2161 mode = opt_mode.require ();
2162 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2163 break;
2166 if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2168 sorry ("GCC cannot support operators with integer types and "
2169 "fixed-point types that have too many integral and "
2170 "fractional bits together");
2171 return NULL_TREE;
2174 return c_common_type_for_mode (mode, satp);
2177 /* Used for communication between c_common_type_for_mode and
2178 c_register_builtin_type. */
2179 tree registered_builtin_types;
2181 /* Return a data type that has machine mode MODE.
2182 If the mode is an integer,
2183 then UNSIGNEDP selects between signed and unsigned types.
2184 If the mode is a fixed-point mode,
2185 then UNSIGNEDP selects between saturating and nonsaturating types. */
2187 tree
2188 c_common_type_for_mode (machine_mode mode, int unsignedp)
2190 tree t;
2191 int i;
2193 if (mode == TYPE_MODE (integer_type_node))
2194 return unsignedp ? unsigned_type_node : integer_type_node;
2196 if (mode == TYPE_MODE (signed_char_type_node))
2197 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2199 if (mode == TYPE_MODE (short_integer_type_node))
2200 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2202 if (mode == TYPE_MODE (long_integer_type_node))
2203 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2205 if (mode == TYPE_MODE (long_long_integer_type_node))
2206 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2208 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2209 if (int_n_enabled_p[i]
2210 && mode == int_n_data[i].m)
2211 return (unsignedp ? int_n_trees[i].unsigned_type
2212 : int_n_trees[i].signed_type);
2214 if (mode == QImode)
2215 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2217 if (mode == HImode)
2218 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2220 if (mode == SImode)
2221 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2223 if (mode == DImode)
2224 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2226 #if HOST_BITS_PER_WIDE_INT >= 64
2227 if (mode == TYPE_MODE (intTI_type_node))
2228 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2229 #endif
2231 if (mode == TYPE_MODE (float_type_node))
2232 return float_type_node;
2234 if (mode == TYPE_MODE (double_type_node))
2235 return double_type_node;
2237 if (mode == TYPE_MODE (long_double_type_node))
2238 return long_double_type_node;
2240 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2241 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2242 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2243 return FLOATN_NX_TYPE_NODE (i);
2245 if (mode == TYPE_MODE (void_type_node))
2246 return void_type_node;
2248 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2249 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2251 unsigned int precision
2252 = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2253 return (unsignedp
2254 ? make_unsigned_type (precision)
2255 : make_signed_type (precision));
2258 if (COMPLEX_MODE_P (mode))
2260 machine_mode inner_mode;
2261 tree inner_type;
2263 if (mode == TYPE_MODE (complex_float_type_node))
2264 return complex_float_type_node;
2265 if (mode == TYPE_MODE (complex_double_type_node))
2266 return complex_double_type_node;
2267 if (mode == TYPE_MODE (complex_long_double_type_node))
2268 return complex_long_double_type_node;
2270 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2271 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2272 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2273 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2275 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2276 return complex_integer_type_node;
2278 inner_mode = GET_MODE_INNER (mode);
2279 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2280 if (inner_type != NULL_TREE)
2281 return build_complex_type (inner_type);
2283 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2284 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2286 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
2287 GET_MODE_NUNITS (mode));
2288 tree bool_type = build_nonstandard_boolean_type (elem_bits);
2289 return build_vector_type_for_mode (bool_type, mode);
2291 else if (VECTOR_MODE_P (mode)
2292 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2294 machine_mode inner_mode = GET_MODE_INNER (mode);
2295 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2296 if (inner_type != NULL_TREE)
2297 return build_vector_type_for_mode (inner_type, mode);
2300 if (mode == TYPE_MODE (dfloat32_type_node))
2301 return dfloat32_type_node;
2302 if (mode == TYPE_MODE (dfloat64_type_node))
2303 return dfloat64_type_node;
2304 if (mode == TYPE_MODE (dfloat128_type_node))
2305 return dfloat128_type_node;
2307 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2309 if (mode == TYPE_MODE (short_fract_type_node))
2310 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2311 if (mode == TYPE_MODE (fract_type_node))
2312 return unsignedp ? sat_fract_type_node : fract_type_node;
2313 if (mode == TYPE_MODE (long_fract_type_node))
2314 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2315 if (mode == TYPE_MODE (long_long_fract_type_node))
2316 return unsignedp ? sat_long_long_fract_type_node
2317 : long_long_fract_type_node;
2319 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2320 return unsignedp ? sat_unsigned_short_fract_type_node
2321 : unsigned_short_fract_type_node;
2322 if (mode == TYPE_MODE (unsigned_fract_type_node))
2323 return unsignedp ? sat_unsigned_fract_type_node
2324 : unsigned_fract_type_node;
2325 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2326 return unsignedp ? sat_unsigned_long_fract_type_node
2327 : unsigned_long_fract_type_node;
2328 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2329 return unsignedp ? sat_unsigned_long_long_fract_type_node
2330 : unsigned_long_long_fract_type_node;
2332 if (mode == TYPE_MODE (short_accum_type_node))
2333 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2334 if (mode == TYPE_MODE (accum_type_node))
2335 return unsignedp ? sat_accum_type_node : accum_type_node;
2336 if (mode == TYPE_MODE (long_accum_type_node))
2337 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2338 if (mode == TYPE_MODE (long_long_accum_type_node))
2339 return unsignedp ? sat_long_long_accum_type_node
2340 : long_long_accum_type_node;
2342 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2343 return unsignedp ? sat_unsigned_short_accum_type_node
2344 : unsigned_short_accum_type_node;
2345 if (mode == TYPE_MODE (unsigned_accum_type_node))
2346 return unsignedp ? sat_unsigned_accum_type_node
2347 : unsigned_accum_type_node;
2348 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2349 return unsignedp ? sat_unsigned_long_accum_type_node
2350 : unsigned_long_accum_type_node;
2351 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2352 return unsignedp ? sat_unsigned_long_long_accum_type_node
2353 : unsigned_long_long_accum_type_node;
2355 if (mode == QQmode)
2356 return unsignedp ? sat_qq_type_node : qq_type_node;
2357 if (mode == HQmode)
2358 return unsignedp ? sat_hq_type_node : hq_type_node;
2359 if (mode == SQmode)
2360 return unsignedp ? sat_sq_type_node : sq_type_node;
2361 if (mode == DQmode)
2362 return unsignedp ? sat_dq_type_node : dq_type_node;
2363 if (mode == TQmode)
2364 return unsignedp ? sat_tq_type_node : tq_type_node;
2366 if (mode == UQQmode)
2367 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2368 if (mode == UHQmode)
2369 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2370 if (mode == USQmode)
2371 return unsignedp ? sat_usq_type_node : usq_type_node;
2372 if (mode == UDQmode)
2373 return unsignedp ? sat_udq_type_node : udq_type_node;
2374 if (mode == UTQmode)
2375 return unsignedp ? sat_utq_type_node : utq_type_node;
2377 if (mode == HAmode)
2378 return unsignedp ? sat_ha_type_node : ha_type_node;
2379 if (mode == SAmode)
2380 return unsignedp ? sat_sa_type_node : sa_type_node;
2381 if (mode == DAmode)
2382 return unsignedp ? sat_da_type_node : da_type_node;
2383 if (mode == TAmode)
2384 return unsignedp ? sat_ta_type_node : ta_type_node;
2386 if (mode == UHAmode)
2387 return unsignedp ? sat_uha_type_node : uha_type_node;
2388 if (mode == USAmode)
2389 return unsignedp ? sat_usa_type_node : usa_type_node;
2390 if (mode == UDAmode)
2391 return unsignedp ? sat_uda_type_node : uda_type_node;
2392 if (mode == UTAmode)
2393 return unsignedp ? sat_uta_type_node : uta_type_node;
2396 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2397 if (TYPE_MODE (TREE_VALUE (t)) == mode
2398 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2399 return TREE_VALUE (t);
2401 return NULL_TREE;
2404 tree
2405 c_common_unsigned_type (tree type)
2407 return c_common_signed_or_unsigned_type (1, type);
2410 /* Return a signed type the same as TYPE in other respects. */
2412 tree
2413 c_common_signed_type (tree type)
2415 return c_common_signed_or_unsigned_type (0, type);
2418 /* Return a type the same as TYPE except unsigned or
2419 signed according to UNSIGNEDP. */
2421 tree
2422 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2424 tree type1;
2425 int i;
2427 /* This block of code emulates the behavior of the old
2428 c_common_unsigned_type. In particular, it returns
2429 long_unsigned_type_node if passed a long, even when a int would
2430 have the same size. This is necessary for warnings to work
2431 correctly in archs where sizeof(int) == sizeof(long) */
2433 type1 = TYPE_MAIN_VARIANT (type);
2434 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2435 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2436 if (type1 == integer_type_node || type1 == unsigned_type_node)
2437 return unsignedp ? unsigned_type_node : integer_type_node;
2438 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2439 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2440 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2441 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2442 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2443 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2445 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2446 if (int_n_enabled_p[i]
2447 && (type1 == int_n_trees[i].unsigned_type
2448 || type1 == int_n_trees[i].signed_type))
2449 return (unsignedp ? int_n_trees[i].unsigned_type
2450 : int_n_trees[i].signed_type);
2452 #if HOST_BITS_PER_WIDE_INT >= 64
2453 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2454 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2455 #endif
2456 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2457 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2458 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2459 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2460 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2461 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2462 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2463 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2465 #define C_COMMON_FIXED_TYPES(NAME) \
2466 if (type1 == short_ ## NAME ## _type_node \
2467 || type1 == unsigned_short_ ## NAME ## _type_node) \
2468 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2469 : short_ ## NAME ## _type_node; \
2470 if (type1 == NAME ## _type_node \
2471 || type1 == unsigned_ ## NAME ## _type_node) \
2472 return unsignedp ? unsigned_ ## NAME ## _type_node \
2473 : NAME ## _type_node; \
2474 if (type1 == long_ ## NAME ## _type_node \
2475 || type1 == unsigned_long_ ## NAME ## _type_node) \
2476 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2477 : long_ ## NAME ## _type_node; \
2478 if (type1 == long_long_ ## NAME ## _type_node \
2479 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2480 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2481 : long_long_ ## NAME ## _type_node;
2483 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2484 if (type1 == NAME ## _type_node \
2485 || type1 == u ## NAME ## _type_node) \
2486 return unsignedp ? u ## NAME ## _type_node \
2487 : NAME ## _type_node;
2489 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2490 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2491 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2492 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2493 : sat_ ## short_ ## NAME ## _type_node; \
2494 if (type1 == sat_ ## NAME ## _type_node \
2495 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2496 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2497 : sat_ ## NAME ## _type_node; \
2498 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2499 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2500 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2501 : sat_ ## long_ ## NAME ## _type_node; \
2502 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2503 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2504 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2505 : sat_ ## long_long_ ## NAME ## _type_node;
2507 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2508 if (type1 == sat_ ## NAME ## _type_node \
2509 || type1 == sat_ ## u ## NAME ## _type_node) \
2510 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2511 : sat_ ## NAME ## _type_node;
2513 C_COMMON_FIXED_TYPES (fract);
2514 C_COMMON_FIXED_TYPES_SAT (fract);
2515 C_COMMON_FIXED_TYPES (accum);
2516 C_COMMON_FIXED_TYPES_SAT (accum);
2518 C_COMMON_FIXED_MODE_TYPES (qq);
2519 C_COMMON_FIXED_MODE_TYPES (hq);
2520 C_COMMON_FIXED_MODE_TYPES (sq);
2521 C_COMMON_FIXED_MODE_TYPES (dq);
2522 C_COMMON_FIXED_MODE_TYPES (tq);
2523 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2524 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2525 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2526 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2527 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2528 C_COMMON_FIXED_MODE_TYPES (ha);
2529 C_COMMON_FIXED_MODE_TYPES (sa);
2530 C_COMMON_FIXED_MODE_TYPES (da);
2531 C_COMMON_FIXED_MODE_TYPES (ta);
2532 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2533 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2534 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2535 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2537 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2538 the precision; they have precision set to match their range, but
2539 may use a wider mode to match an ABI. If we change modes, we may
2540 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2541 the precision as well, so as to yield correct results for
2542 bit-field types. C++ does not have these separate bit-field
2543 types, and producing a signed or unsigned variant of an
2544 ENUMERAL_TYPE may cause other problems as well. */
2546 if (!INTEGRAL_TYPE_P (type)
2547 || TYPE_UNSIGNED (type) == unsignedp)
2548 return type;
2550 #define TYPE_OK(node) \
2551 (TYPE_MODE (type) == TYPE_MODE (node) \
2552 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2553 if (TYPE_OK (signed_char_type_node))
2554 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2555 if (TYPE_OK (integer_type_node))
2556 return unsignedp ? unsigned_type_node : integer_type_node;
2557 if (TYPE_OK (short_integer_type_node))
2558 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2559 if (TYPE_OK (long_integer_type_node))
2560 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2561 if (TYPE_OK (long_long_integer_type_node))
2562 return (unsignedp ? long_long_unsigned_type_node
2563 : long_long_integer_type_node);
2565 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2566 if (int_n_enabled_p[i]
2567 && TYPE_MODE (type) == int_n_data[i].m
2568 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2569 return (unsignedp ? int_n_trees[i].unsigned_type
2570 : int_n_trees[i].signed_type);
2572 #if HOST_BITS_PER_WIDE_INT >= 64
2573 if (TYPE_OK (intTI_type_node))
2574 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2575 #endif
2576 if (TYPE_OK (intDI_type_node))
2577 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2578 if (TYPE_OK (intSI_type_node))
2579 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2580 if (TYPE_OK (intHI_type_node))
2581 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2582 if (TYPE_OK (intQI_type_node))
2583 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2584 #undef TYPE_OK
2586 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2589 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2591 tree
2592 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2594 int i;
2596 /* Extended integer types of the same width as a standard type have
2597 lesser rank, so those of the same width as int promote to int or
2598 unsigned int and are valid for printf formats expecting int or
2599 unsigned int. To avoid such special cases, avoid creating
2600 extended integer types for bit-fields if a standard integer type
2601 is available. */
2602 if (width == TYPE_PRECISION (integer_type_node))
2603 return unsignedp ? unsigned_type_node : integer_type_node;
2604 if (width == TYPE_PRECISION (signed_char_type_node))
2605 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2606 if (width == TYPE_PRECISION (short_integer_type_node))
2607 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2608 if (width == TYPE_PRECISION (long_integer_type_node))
2609 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2610 if (width == TYPE_PRECISION (long_long_integer_type_node))
2611 return (unsignedp ? long_long_unsigned_type_node
2612 : long_long_integer_type_node);
2613 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2614 if (int_n_enabled_p[i]
2615 && width == int_n_data[i].bitsize)
2616 return (unsignedp ? int_n_trees[i].unsigned_type
2617 : int_n_trees[i].signed_type);
2618 return build_nonstandard_integer_type (width, unsignedp);
2621 /* The C version of the register_builtin_type langhook. */
2623 void
2624 c_register_builtin_type (tree type, const char* name)
2626 tree decl;
2628 decl = build_decl (UNKNOWN_LOCATION,
2629 TYPE_DECL, get_identifier (name), type);
2630 DECL_ARTIFICIAL (decl) = 1;
2631 if (!TYPE_NAME (type))
2632 TYPE_NAME (type) = decl;
2633 lang_hooks.decls.pushdecl (decl);
2635 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2638 /* Print an error message for invalid operands to arith operation
2639 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2640 RICHLOC is a rich location for the message, containing either
2641 three separate locations for each of the operator and operands
2643 lhs op rhs
2644 ~~~ ^~ ~~~
2646 (C FE), or one location ranging over all over them
2648 lhs op rhs
2649 ~~~~^~~~~~
2651 (C++ FE). */
2653 void
2654 binary_op_error (rich_location *richloc, enum tree_code code,
2655 tree type0, tree type1)
2657 const char *opname;
2659 switch (code)
2661 case PLUS_EXPR:
2662 opname = "+"; break;
2663 case MINUS_EXPR:
2664 opname = "-"; break;
2665 case MULT_EXPR:
2666 opname = "*"; break;
2667 case MAX_EXPR:
2668 opname = "max"; break;
2669 case MIN_EXPR:
2670 opname = "min"; break;
2671 case EQ_EXPR:
2672 opname = "=="; break;
2673 case NE_EXPR:
2674 opname = "!="; break;
2675 case LE_EXPR:
2676 opname = "<="; break;
2677 case GE_EXPR:
2678 opname = ">="; break;
2679 case LT_EXPR:
2680 opname = "<"; break;
2681 case GT_EXPR:
2682 opname = ">"; break;
2683 case LSHIFT_EXPR:
2684 opname = "<<"; break;
2685 case RSHIFT_EXPR:
2686 opname = ">>"; break;
2687 case TRUNC_MOD_EXPR:
2688 case FLOOR_MOD_EXPR:
2689 opname = "%"; break;
2690 case TRUNC_DIV_EXPR:
2691 case FLOOR_DIV_EXPR:
2692 opname = "/"; break;
2693 case BIT_AND_EXPR:
2694 opname = "&"; break;
2695 case BIT_IOR_EXPR:
2696 opname = "|"; break;
2697 case TRUTH_ANDIF_EXPR:
2698 opname = "&&"; break;
2699 case TRUTH_ORIF_EXPR:
2700 opname = "||"; break;
2701 case BIT_XOR_EXPR:
2702 opname = "^"; break;
2703 default:
2704 gcc_unreachable ();
2706 error_at (richloc,
2707 "invalid operands to binary %s (have %qT and %qT)",
2708 opname, type0, type1);
2711 /* Given an expression as a tree, return its original type. Do this
2712 by stripping any conversion that preserves the sign and precision. */
2713 static tree
2714 expr_original_type (tree expr)
2716 STRIP_SIGN_NOPS (expr);
2717 return TREE_TYPE (expr);
2720 /* Subroutine of build_binary_op, used for comparison operations.
2721 See if the operands have both been converted from subword integer types
2722 and, if so, perhaps change them both back to their original type.
2723 This function is also responsible for converting the two operands
2724 to the proper common type for comparison.
2726 The arguments of this function are all pointers to local variables
2727 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2728 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2730 LOC is the location of the comparison.
2732 If this function returns non-NULL_TREE, it means that the comparison has
2733 a constant value. What this function returns is an expression for
2734 that value. */
2736 tree
2737 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2738 tree *restype_ptr, enum tree_code *rescode_ptr)
2740 tree type;
2741 tree op0 = *op0_ptr;
2742 tree op1 = *op1_ptr;
2743 int unsignedp0, unsignedp1;
2744 int real1, real2;
2745 tree primop0, primop1;
2746 enum tree_code code = *rescode_ptr;
2748 /* Throw away any conversions to wider types
2749 already present in the operands. */
2751 primop0 = c_common_get_narrower (op0, &unsignedp0);
2752 primop1 = c_common_get_narrower (op1, &unsignedp1);
2754 /* If primopN is first sign-extended from primopN's precision to opN's
2755 precision, then zero-extended from opN's precision to
2756 *restype_ptr precision, shortenings might be invalid. */
2757 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2758 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2759 && !unsignedp0
2760 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2761 primop0 = op0;
2762 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2763 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2764 && !unsignedp1
2765 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2766 primop1 = op1;
2768 /* Handle the case that OP0 does not *contain* a conversion
2769 but it *requires* conversion to FINAL_TYPE. */
2771 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2772 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2773 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2774 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2776 /* If one of the operands must be floated, we cannot optimize. */
2777 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2778 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2780 /* If first arg is constant, swap the args (changing operation
2781 so value is preserved), for canonicalization. Don't do this if
2782 the second arg is 0. */
2784 if (TREE_CONSTANT (primop0)
2785 && !integer_zerop (primop1) && !real_zerop (primop1)
2786 && !fixed_zerop (primop1))
2788 std::swap (primop0, primop1);
2789 std::swap (op0, op1);
2790 *op0_ptr = op0;
2791 *op1_ptr = op1;
2792 std::swap (unsignedp0, unsignedp1);
2793 std::swap (real1, real2);
2795 switch (code)
2797 case LT_EXPR:
2798 code = GT_EXPR;
2799 break;
2800 case GT_EXPR:
2801 code = LT_EXPR;
2802 break;
2803 case LE_EXPR:
2804 code = GE_EXPR;
2805 break;
2806 case GE_EXPR:
2807 code = LE_EXPR;
2808 break;
2809 default:
2810 break;
2812 *rescode_ptr = code;
2815 /* If comparing an integer against a constant more bits wide,
2816 maybe we can deduce a value of 1 or 0 independent of the data.
2817 Or else truncate the constant now
2818 rather than extend the variable at run time.
2820 This is only interesting if the constant is the wider arg.
2821 Also, it is not safe if the constant is unsigned and the
2822 variable arg is signed, since in this case the variable
2823 would be sign-extended and then regarded as unsigned.
2824 Our technique fails in this case because the lowest/highest
2825 possible unsigned results don't follow naturally from the
2826 lowest/highest possible values of the variable operand.
2827 For just EQ_EXPR and NE_EXPR there is another technique that
2828 could be used: see if the constant can be faithfully represented
2829 in the other operand's type, by truncating it and reextending it
2830 and see if that preserves the constant's value. */
2832 if (!real1 && !real2
2833 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2834 && TREE_CODE (primop1) == INTEGER_CST
2835 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2837 int min_gt, max_gt, min_lt, max_lt;
2838 tree maxval, minval;
2839 /* 1 if comparison is nominally unsigned. */
2840 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2841 tree val;
2843 type = c_common_signed_or_unsigned_type (unsignedp0,
2844 TREE_TYPE (primop0));
2846 maxval = TYPE_MAX_VALUE (type);
2847 minval = TYPE_MIN_VALUE (type);
2849 if (unsignedp && !unsignedp0)
2850 *restype_ptr = c_common_signed_type (*restype_ptr);
2852 if (TREE_TYPE (primop1) != *restype_ptr)
2854 /* Convert primop1 to target type, but do not introduce
2855 additional overflow. We know primop1 is an int_cst. */
2856 primop1 = force_fit_type (*restype_ptr,
2857 wi::to_wide
2858 (primop1,
2859 TYPE_PRECISION (*restype_ptr)),
2860 0, TREE_OVERFLOW (primop1));
2862 if (type != *restype_ptr)
2864 minval = convert (*restype_ptr, minval);
2865 maxval = convert (*restype_ptr, maxval);
2868 min_gt = tree_int_cst_lt (primop1, minval);
2869 max_gt = tree_int_cst_lt (primop1, maxval);
2870 min_lt = tree_int_cst_lt (minval, primop1);
2871 max_lt = tree_int_cst_lt (maxval, primop1);
2873 val = 0;
2874 /* This used to be a switch, but Genix compiler can't handle that. */
2875 if (code == NE_EXPR)
2877 if (max_lt || min_gt)
2878 val = truthvalue_true_node;
2880 else if (code == EQ_EXPR)
2882 if (max_lt || min_gt)
2883 val = truthvalue_false_node;
2885 else if (code == LT_EXPR)
2887 if (max_lt)
2888 val = truthvalue_true_node;
2889 if (!min_lt)
2890 val = truthvalue_false_node;
2892 else if (code == GT_EXPR)
2894 if (min_gt)
2895 val = truthvalue_true_node;
2896 if (!max_gt)
2897 val = truthvalue_false_node;
2899 else if (code == LE_EXPR)
2901 if (!max_gt)
2902 val = truthvalue_true_node;
2903 if (min_gt)
2904 val = truthvalue_false_node;
2906 else if (code == GE_EXPR)
2908 if (!min_lt)
2909 val = truthvalue_true_node;
2910 if (max_lt)
2911 val = truthvalue_false_node;
2914 /* If primop0 was sign-extended and unsigned comparison specd,
2915 we did a signed comparison above using the signed type bounds.
2916 But the comparison we output must be unsigned.
2918 Also, for inequalities, VAL is no good; but if the signed
2919 comparison had *any* fixed result, it follows that the
2920 unsigned comparison just tests the sign in reverse
2921 (positive values are LE, negative ones GE).
2922 So we can generate an unsigned comparison
2923 against an extreme value of the signed type. */
2925 if (unsignedp && !unsignedp0)
2927 if (val != 0)
2928 switch (code)
2930 case LT_EXPR:
2931 case GE_EXPR:
2932 primop1 = TYPE_MIN_VALUE (type);
2933 val = 0;
2934 break;
2936 case LE_EXPR:
2937 case GT_EXPR:
2938 primop1 = TYPE_MAX_VALUE (type);
2939 val = 0;
2940 break;
2942 default:
2943 break;
2945 type = c_common_unsigned_type (type);
2948 if (TREE_CODE (primop0) != INTEGER_CST
2949 /* Don't warn if it's from a (non-system) macro. */
2950 && !(from_macro_expansion_at
2951 (expansion_point_location_if_in_system_header
2952 (EXPR_LOCATION (primop0)))))
2954 if (val == truthvalue_false_node)
2955 warning_at (loc, OPT_Wtype_limits,
2956 "comparison is always false due to limited range of data type");
2957 if (val == truthvalue_true_node)
2958 warning_at (loc, OPT_Wtype_limits,
2959 "comparison is always true due to limited range of data type");
2962 if (val != 0)
2964 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2965 if (TREE_SIDE_EFFECTS (primop0))
2966 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2967 return val;
2970 /* Value is not predetermined, but do the comparison
2971 in the type of the operand that is not constant.
2972 TYPE is already properly set. */
2975 /* If either arg is decimal float and the other is float, find the
2976 proper common type to use for comparison. */
2977 else if (real1 && real2
2978 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2979 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2980 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2982 /* If either arg is decimal float and the other is float, fail. */
2983 else if (real1 && real2
2984 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2985 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2986 return NULL_TREE;
2988 else if (real1 && real2
2989 && (TYPE_PRECISION (TREE_TYPE (primop0))
2990 == TYPE_PRECISION (TREE_TYPE (primop1))))
2991 type = TREE_TYPE (primop0);
2993 /* If args' natural types are both narrower than nominal type
2994 and both extend in the same manner, compare them
2995 in the type of the wider arg.
2996 Otherwise must actually extend both to the nominal
2997 common type lest different ways of extending
2998 alter the result.
2999 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3001 else if (unsignedp0 == unsignedp1 && real1 == real2
3002 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3003 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3005 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3006 type = c_common_signed_or_unsigned_type (unsignedp0
3007 || TYPE_UNSIGNED (*restype_ptr),
3008 type);
3009 /* Make sure shorter operand is extended the right way
3010 to match the longer operand. */
3011 primop0
3012 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3013 TREE_TYPE (primop0)),
3014 primop0);
3015 primop1
3016 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3017 TREE_TYPE (primop1)),
3018 primop1);
3020 else
3022 /* Here we must do the comparison on the nominal type
3023 using the args exactly as we received them. */
3024 type = *restype_ptr;
3025 primop0 = op0;
3026 primop1 = op1;
3028 if (!real1 && !real2 && integer_zerop (primop1)
3029 && TYPE_UNSIGNED (*restype_ptr))
3031 tree value = NULL_TREE;
3032 /* All unsigned values are >= 0, so we warn. However,
3033 if OP0 is a constant that is >= 0, the signedness of
3034 the comparison isn't an issue, so suppress the
3035 warning. */
3036 bool warn =
3037 warn_type_limits && !in_system_header_at (loc)
3038 && !(TREE_CODE (primop0) == INTEGER_CST
3039 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3040 primop0)))
3041 /* Do not warn for enumeration types. */
3042 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3044 switch (code)
3046 case GE_EXPR:
3047 if (warn)
3048 warning_at (loc, OPT_Wtype_limits,
3049 "comparison of unsigned expression >= 0 is always true");
3050 value = truthvalue_true_node;
3051 break;
3053 case LT_EXPR:
3054 if (warn)
3055 warning_at (loc, OPT_Wtype_limits,
3056 "comparison of unsigned expression < 0 is always false");
3057 value = truthvalue_false_node;
3058 break;
3060 default:
3061 break;
3064 if (value != NULL_TREE)
3066 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3067 if (TREE_SIDE_EFFECTS (primop0))
3068 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3069 primop0, value);
3070 return value;
3075 *op0_ptr = convert (type, primop0);
3076 *op1_ptr = convert (type, primop1);
3078 *restype_ptr = truthvalue_type_node;
3080 return NULL_TREE;
3083 /* Return a tree for the sum or difference (RESULTCODE says which)
3084 of pointer PTROP and integer INTOP. */
3086 tree
3087 pointer_int_sum (location_t loc, enum tree_code resultcode,
3088 tree ptrop, tree intop, bool complain)
3090 tree size_exp, ret;
3092 /* The result is a pointer of the same type that is being added. */
3093 tree result_type = TREE_TYPE (ptrop);
3095 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3097 if (complain && warn_pointer_arith)
3098 pedwarn (loc, OPT_Wpointer_arith,
3099 "pointer of type %<void *%> used in arithmetic");
3100 else if (!complain)
3101 return error_mark_node;
3102 size_exp = integer_one_node;
3104 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3106 if (complain && warn_pointer_arith)
3107 pedwarn (loc, OPT_Wpointer_arith,
3108 "pointer to a function used in arithmetic");
3109 else if (!complain)
3110 return error_mark_node;
3111 size_exp = integer_one_node;
3113 else
3114 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3116 /* We are manipulating pointer values, so we don't need to warn
3117 about relying on undefined signed overflow. We disable the
3118 warning here because we use integer types so fold won't know that
3119 they are really pointers. */
3120 fold_defer_overflow_warnings ();
3122 /* If what we are about to multiply by the size of the elements
3123 contains a constant term, apply distributive law
3124 and multiply that constant term separately.
3125 This helps produce common subexpressions. */
3126 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3127 && !TREE_CONSTANT (intop)
3128 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3129 && TREE_CONSTANT (size_exp)
3130 /* If the constant comes from pointer subtraction,
3131 skip this optimization--it would cause an error. */
3132 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3133 /* If the constant is unsigned, and smaller than the pointer size,
3134 then we must skip this optimization. This is because it could cause
3135 an overflow error if the constant is negative but INTOP is not. */
3136 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3137 || (TYPE_PRECISION (TREE_TYPE (intop))
3138 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3140 enum tree_code subcode = resultcode;
3141 tree int_type = TREE_TYPE (intop);
3142 if (TREE_CODE (intop) == MINUS_EXPR)
3143 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3144 /* Convert both subexpression types to the type of intop,
3145 because weird cases involving pointer arithmetic
3146 can result in a sum or difference with different type args. */
3147 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3148 subcode, ptrop,
3149 convert (int_type, TREE_OPERAND (intop, 1)),
3150 true);
3151 intop = convert (int_type, TREE_OPERAND (intop, 0));
3154 /* Convert the integer argument to a type the same size as sizetype
3155 so the multiply won't overflow spuriously. */
3156 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3157 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3158 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3159 TYPE_UNSIGNED (sizetype)), intop);
3161 /* Replace the integer argument with a suitable product by the object size.
3162 Do this multiplication as signed, then convert to the appropriate type
3163 for the pointer operation and disregard an overflow that occurred only
3164 because of the sign-extension change in the latter conversion. */
3166 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3167 convert (TREE_TYPE (intop), size_exp));
3168 intop = convert (sizetype, t);
3169 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3170 intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3173 /* Create the sum or difference. */
3174 if (resultcode == MINUS_EXPR)
3175 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3177 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3179 fold_undefer_and_ignore_overflow_warnings ();
3181 return ret;
3184 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3185 and if NON_CONST is known not to be permitted in an evaluated part
3186 of a constant expression. */
3188 tree
3189 c_wrap_maybe_const (tree expr, bool non_const)
3191 bool nowarning = TREE_NO_WARNING (expr);
3192 location_t loc = EXPR_LOCATION (expr);
3194 /* This should never be called for C++. */
3195 if (c_dialect_cxx ())
3196 gcc_unreachable ();
3198 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3199 STRIP_TYPE_NOPS (expr);
3200 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3201 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3202 if (nowarning)
3203 TREE_NO_WARNING (expr) = 1;
3204 protected_set_expr_location (expr, loc);
3206 return expr;
3209 /* Return whether EXPR is a declaration whose address can never be
3210 NULL. */
3212 bool
3213 decl_with_nonnull_addr_p (const_tree expr)
3215 return (DECL_P (expr)
3216 && (TREE_CODE (expr) == PARM_DECL
3217 || TREE_CODE (expr) == LABEL_DECL
3218 || !DECL_WEAK (expr)));
3221 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3222 or for an `if' or `while' statement or ?..: exp. It should already
3223 have been validated to be of suitable type; otherwise, a bad
3224 diagnostic may result.
3226 The EXPR is located at LOCATION.
3228 This preparation consists of taking the ordinary
3229 representation of an expression expr and producing a valid tree
3230 boolean expression describing whether expr is nonzero. We could
3231 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3232 but we optimize comparisons, &&, ||, and !.
3234 The resulting type should always be `truthvalue_type_node'. */
3236 tree
3237 c_common_truthvalue_conversion (location_t location, tree expr)
3239 switch (TREE_CODE (expr))
3241 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3242 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3243 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3244 case ORDERED_EXPR: case UNORDERED_EXPR:
3245 if (TREE_TYPE (expr) == truthvalue_type_node)
3246 return expr;
3247 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3248 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3249 goto ret;
3251 case TRUTH_ANDIF_EXPR:
3252 case TRUTH_ORIF_EXPR:
3253 case TRUTH_AND_EXPR:
3254 case TRUTH_OR_EXPR:
3255 case TRUTH_XOR_EXPR:
3256 if (TREE_TYPE (expr) == truthvalue_type_node)
3257 return expr;
3258 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3259 c_common_truthvalue_conversion (location,
3260 TREE_OPERAND (expr, 0)),
3261 c_common_truthvalue_conversion (location,
3262 TREE_OPERAND (expr, 1)));
3263 goto ret;
3265 case TRUTH_NOT_EXPR:
3266 if (TREE_TYPE (expr) == truthvalue_type_node)
3267 return expr;
3268 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3269 c_common_truthvalue_conversion (location,
3270 TREE_OPERAND (expr, 0)));
3271 goto ret;
3273 case ERROR_MARK:
3274 return expr;
3276 case INTEGER_CST:
3277 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3278 && !integer_zerop (expr)
3279 && !integer_onep (expr))
3280 warning_at (location, OPT_Wint_in_bool_context,
3281 "enum constant in boolean context");
3282 return integer_zerop (expr) ? truthvalue_false_node
3283 : truthvalue_true_node;
3285 case REAL_CST:
3286 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3287 ? truthvalue_true_node
3288 : truthvalue_false_node;
3290 case FIXED_CST:
3291 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3292 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3293 ? truthvalue_true_node
3294 : truthvalue_false_node;
3296 case FUNCTION_DECL:
3297 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3298 /* Fall through. */
3300 case ADDR_EXPR:
3302 tree inner = TREE_OPERAND (expr, 0);
3303 if (decl_with_nonnull_addr_p (inner))
3305 /* Common Ada/Pascal programmer's mistake. */
3306 warning_at (location,
3307 OPT_Waddress,
3308 "the address of %qD will always evaluate as %<true%>",
3309 inner);
3310 return truthvalue_true_node;
3312 break;
3315 case COMPLEX_EXPR:
3316 expr = build_binary_op (EXPR_LOCATION (expr),
3317 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3318 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3319 c_common_truthvalue_conversion (location,
3320 TREE_OPERAND (expr, 0)),
3321 c_common_truthvalue_conversion (location,
3322 TREE_OPERAND (expr, 1)),
3323 false);
3324 goto ret;
3326 case NEGATE_EXPR:
3327 case ABS_EXPR:
3328 case FLOAT_EXPR:
3329 case EXCESS_PRECISION_EXPR:
3330 /* These don't change whether an object is nonzero or zero. */
3331 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3333 case LROTATE_EXPR:
3334 case RROTATE_EXPR:
3335 /* These don't change whether an object is zero or nonzero, but
3336 we can't ignore them if their second arg has side-effects. */
3337 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3339 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3340 TREE_OPERAND (expr, 1),
3341 c_common_truthvalue_conversion
3342 (location, TREE_OPERAND (expr, 0)));
3343 goto ret;
3345 else
3346 return c_common_truthvalue_conversion (location,
3347 TREE_OPERAND (expr, 0));
3349 case MULT_EXPR:
3350 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3351 "%<*%> in boolean context, suggest %<&&%> instead");
3352 break;
3354 case LSHIFT_EXPR:
3355 /* We will only warn on signed shifts here, because the majority of
3356 false positive warnings happen in code where unsigned arithmetic
3357 was used in anticipation of a possible overflow.
3358 Furthermore, if we see an unsigned type here we know that the
3359 result of the shift is not subject to integer promotion rules. */
3360 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3361 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3362 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3363 "%<<<%> in boolean context, did you mean %<<%> ?");
3364 break;
3366 case COND_EXPR:
3367 if (warn_int_in_bool_context
3368 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3370 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3371 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3372 if (TREE_CODE (val1) == INTEGER_CST
3373 && TREE_CODE (val2) == INTEGER_CST
3374 && !integer_zerop (val1)
3375 && !integer_zerop (val2)
3376 && (!integer_onep (val1)
3377 || !integer_onep (val2)))
3378 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3379 "?: using integer constants in boolean context, "
3380 "the expression will always evaluate to %<true%>");
3381 else if ((TREE_CODE (val1) == INTEGER_CST
3382 && !integer_zerop (val1)
3383 && !integer_onep (val1))
3384 || (TREE_CODE (val2) == INTEGER_CST
3385 && !integer_zerop (val2)
3386 && !integer_onep (val2)))
3387 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3388 "?: using integer constants in boolean context");
3390 /* Distribute the conversion into the arms of a COND_EXPR. */
3391 if (c_dialect_cxx ())
3392 /* Avoid premature folding. */
3393 break;
3394 else
3396 int w = warn_int_in_bool_context;
3397 warn_int_in_bool_context = 0;
3398 /* Folding will happen later for C. */
3399 expr = build3 (COND_EXPR, truthvalue_type_node,
3400 TREE_OPERAND (expr, 0),
3401 c_common_truthvalue_conversion (location,
3402 TREE_OPERAND (expr, 1)),
3403 c_common_truthvalue_conversion (location,
3404 TREE_OPERAND (expr, 2)));
3405 warn_int_in_bool_context = w;
3406 goto ret;
3409 CASE_CONVERT:
3411 tree totype = TREE_TYPE (expr);
3412 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3414 if (POINTER_TYPE_P (totype)
3415 && !c_inhibit_evaluation_warnings
3416 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3418 tree inner = expr;
3419 STRIP_NOPS (inner);
3421 if (DECL_P (inner))
3422 warning_at (location,
3423 OPT_Waddress,
3424 "the compiler can assume that the address of "
3425 "%qD will always evaluate to %<true%>",
3426 inner);
3429 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3430 since that affects how `default_conversion' will behave. */
3431 if (TREE_CODE (totype) == REFERENCE_TYPE
3432 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3433 break;
3434 /* Don't strip a conversion from C++0x scoped enum, since they
3435 don't implicitly convert to other types. */
3436 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3437 && ENUM_IS_SCOPED (fromtype))
3438 break;
3439 /* If this isn't narrowing the argument, we can ignore it. */
3440 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3441 return c_common_truthvalue_conversion (location,
3442 TREE_OPERAND (expr, 0));
3444 break;
3446 case MODIFY_EXPR:
3447 if (!TREE_NO_WARNING (expr)
3448 && warn_parentheses)
3450 warning_at (location, OPT_Wparentheses,
3451 "suggest parentheses around assignment used as "
3452 "truth value");
3453 TREE_NO_WARNING (expr) = 1;
3455 break;
3457 default:
3458 break;
3461 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3463 tree t = save_expr (expr);
3464 expr = (build_binary_op
3465 (EXPR_LOCATION (expr),
3466 (TREE_SIDE_EFFECTS (expr)
3467 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3468 c_common_truthvalue_conversion
3469 (location,
3470 build_unary_op (location, REALPART_EXPR, t, false)),
3471 c_common_truthvalue_conversion
3472 (location,
3473 build_unary_op (location, IMAGPART_EXPR, t, false)),
3474 false));
3475 goto ret;
3478 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3480 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3481 FCONST0 (TYPE_MODE
3482 (TREE_TYPE (expr))));
3483 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3485 else
3486 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3488 ret:
3489 protected_set_expr_location (expr, location);
3490 return expr;
3493 static void def_builtin_1 (enum built_in_function fncode,
3494 const char *name,
3495 enum built_in_class fnclass,
3496 tree fntype, tree libtype,
3497 bool both_p, bool fallback_p, bool nonansi_p,
3498 tree fnattrs, bool implicit_p);
3501 /* Apply the TYPE_QUALS to the new DECL. */
3503 void
3504 c_apply_type_quals_to_decl (int type_quals, tree decl)
3506 tree type = TREE_TYPE (decl);
3508 if (type == error_mark_node)
3509 return;
3511 if ((type_quals & TYPE_QUAL_CONST)
3512 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3513 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3514 constructor can produce constant init, so rely on cp_finish_decl to
3515 clear TREE_READONLY if the variable has non-constant init. */
3516 TREE_READONLY (decl) = 1;
3517 if (type_quals & TYPE_QUAL_VOLATILE)
3519 TREE_SIDE_EFFECTS (decl) = 1;
3520 TREE_THIS_VOLATILE (decl) = 1;
3522 if (type_quals & TYPE_QUAL_RESTRICT)
3524 while (type && TREE_CODE (type) == ARRAY_TYPE)
3525 /* Allow 'restrict' on arrays of pointers.
3526 FIXME currently we just ignore it. */
3527 type = TREE_TYPE (type);
3528 if (!type
3529 || !POINTER_TYPE_P (type)
3530 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3531 error ("invalid use of %<restrict%>");
3535 /* Return the typed-based alias set for T, which may be an expression
3536 or a type. Return -1 if we don't do anything special. */
3538 alias_set_type
3539 c_common_get_alias_set (tree t)
3541 /* For VLAs, use the alias set of the element type rather than the
3542 default of alias set 0 for types compared structurally. */
3543 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3545 if (TREE_CODE (t) == ARRAY_TYPE)
3546 return get_alias_set (TREE_TYPE (t));
3547 return -1;
3550 /* That's all the expressions we handle specially. */
3551 if (!TYPE_P (t))
3552 return -1;
3554 /* The C standard guarantees that any object may be accessed via an
3555 lvalue that has character type. */
3556 if (t == char_type_node
3557 || t == signed_char_type_node
3558 || t == unsigned_char_type_node)
3559 return 0;
3561 /* The C standard specifically allows aliasing between signed and
3562 unsigned variants of the same type. We treat the signed
3563 variant as canonical. */
3564 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3566 tree t1 = c_common_signed_type (t);
3568 /* t1 == t can happen for boolean nodes which are always unsigned. */
3569 if (t1 != t)
3570 return get_alias_set (t1);
3573 return -1;
3576 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3577 the IS_SIZEOF parameter indicates which operator is being applied.
3578 The COMPLAIN flag controls whether we should diagnose possibly
3579 ill-formed constructs or not. LOC is the location of the SIZEOF or
3580 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3581 a type in any context should be returned, rather than the normal
3582 alignment for that type. */
3584 tree
3585 c_sizeof_or_alignof_type (location_t loc,
3586 tree type, bool is_sizeof, bool min_alignof,
3587 int complain)
3589 const char *op_name;
3590 tree value = NULL;
3591 enum tree_code type_code = TREE_CODE (type);
3593 op_name = is_sizeof ? "sizeof" : "__alignof__";
3595 if (type_code == FUNCTION_TYPE)
3597 if (is_sizeof)
3599 if (complain && warn_pointer_arith)
3600 pedwarn (loc, OPT_Wpointer_arith,
3601 "invalid application of %<sizeof%> to a function type");
3602 else if (!complain)
3603 return error_mark_node;
3604 value = size_one_node;
3606 else
3608 if (complain)
3610 if (c_dialect_cxx ())
3611 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3612 "%<alignof%> applied to a function type");
3613 else
3614 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3615 "%<_Alignof%> applied to a function type");
3617 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3620 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3622 if (type_code == VOID_TYPE
3623 && complain && warn_pointer_arith)
3624 pedwarn (loc, OPT_Wpointer_arith,
3625 "invalid application of %qs to a void type", op_name);
3626 else if (!complain)
3627 return error_mark_node;
3628 value = size_one_node;
3630 else if (!COMPLETE_TYPE_P (type)
3631 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3633 if (complain)
3634 error_at (loc, "invalid application of %qs to incomplete type %qT",
3635 op_name, type);
3636 return error_mark_node;
3638 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3639 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3641 if (complain)
3642 error_at (loc, "invalid application of %qs to array type %qT of "
3643 "incomplete element type", op_name, type);
3644 return error_mark_node;
3646 else
3648 if (is_sizeof)
3649 /* Convert in case a char is more than one unit. */
3650 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3651 size_int (TYPE_PRECISION (char_type_node)
3652 / BITS_PER_UNIT));
3653 else if (min_alignof)
3654 value = size_int (min_align_of_type (type));
3655 else
3656 value = size_int (TYPE_ALIGN_UNIT (type));
3659 /* VALUE will have the middle-end integer type sizetype.
3660 However, we should really return a value of type `size_t',
3661 which is just a typedef for an ordinary integer type. */
3662 value = fold_convert_loc (loc, size_type_node, value);
3664 return value;
3667 /* Implement the __alignof keyword: Return the minimum required
3668 alignment of EXPR, measured in bytes. For VAR_DECLs,
3669 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3670 from an "aligned" __attribute__ specification). LOC is the
3671 location of the ALIGNOF operator. */
3673 tree
3674 c_alignof_expr (location_t loc, tree expr)
3676 tree t;
3678 if (VAR_OR_FUNCTION_DECL_P (expr))
3679 t = size_int (DECL_ALIGN_UNIT (expr));
3681 else if (TREE_CODE (expr) == COMPONENT_REF
3682 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3684 error_at (loc, "%<__alignof%> applied to a bit-field");
3685 t = size_one_node;
3687 else if (TREE_CODE (expr) == COMPONENT_REF
3688 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3689 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3691 else if (INDIRECT_REF_P (expr))
3693 tree t = TREE_OPERAND (expr, 0);
3694 tree best = t;
3695 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3697 while (CONVERT_EXPR_P (t)
3698 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3700 int thisalign;
3702 t = TREE_OPERAND (t, 0);
3703 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3704 if (thisalign > bestalign)
3705 best = t, bestalign = thisalign;
3707 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3709 else
3710 return c_alignof (loc, TREE_TYPE (expr));
3712 return fold_convert_loc (loc, size_type_node, t);
3715 /* Handle C and C++ default attributes. */
3717 enum built_in_attribute
3719 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3720 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3721 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3722 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3723 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3724 #include "builtin-attrs.def"
3725 #undef DEF_ATTR_NULL_TREE
3726 #undef DEF_ATTR_INT
3727 #undef DEF_ATTR_STRING
3728 #undef DEF_ATTR_IDENT
3729 #undef DEF_ATTR_TREE_LIST
3730 ATTR_LAST
3733 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3735 static void c_init_attributes (void);
3737 enum c_builtin_type
3739 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3740 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3741 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3742 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3743 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3744 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3745 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3746 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3747 ARG6) NAME,
3748 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3749 ARG6, ARG7) NAME,
3750 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3751 ARG6, ARG7, ARG8) NAME,
3752 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3753 ARG6, ARG7, ARG8, ARG9) NAME,
3754 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3755 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3756 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3757 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3758 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3759 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3760 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3761 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3762 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3763 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3764 NAME,
3765 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3766 ARG6) NAME,
3767 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3768 ARG6, ARG7) NAME,
3769 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3770 #include "builtin-types.def"
3771 #undef DEF_PRIMITIVE_TYPE
3772 #undef DEF_FUNCTION_TYPE_0
3773 #undef DEF_FUNCTION_TYPE_1
3774 #undef DEF_FUNCTION_TYPE_2
3775 #undef DEF_FUNCTION_TYPE_3
3776 #undef DEF_FUNCTION_TYPE_4
3777 #undef DEF_FUNCTION_TYPE_5
3778 #undef DEF_FUNCTION_TYPE_6
3779 #undef DEF_FUNCTION_TYPE_7
3780 #undef DEF_FUNCTION_TYPE_8
3781 #undef DEF_FUNCTION_TYPE_9
3782 #undef DEF_FUNCTION_TYPE_10
3783 #undef DEF_FUNCTION_TYPE_11
3784 #undef DEF_FUNCTION_TYPE_VAR_0
3785 #undef DEF_FUNCTION_TYPE_VAR_1
3786 #undef DEF_FUNCTION_TYPE_VAR_2
3787 #undef DEF_FUNCTION_TYPE_VAR_3
3788 #undef DEF_FUNCTION_TYPE_VAR_4
3789 #undef DEF_FUNCTION_TYPE_VAR_5
3790 #undef DEF_FUNCTION_TYPE_VAR_6
3791 #undef DEF_FUNCTION_TYPE_VAR_7
3792 #undef DEF_POINTER_TYPE
3793 BT_LAST
3796 typedef enum c_builtin_type builtin_type;
3798 /* A temporary array for c_common_nodes_and_builtins. Used in
3799 communication with def_fn_type. */
3800 static tree builtin_types[(int) BT_LAST + 1];
3802 /* A helper function for c_common_nodes_and_builtins. Build function type
3803 for DEF with return type RET and N arguments. If VAR is true, then the
3804 function should be variadic after those N arguments.
3806 Takes special care not to ICE if any of the types involved are
3807 error_mark_node, which indicates that said type is not in fact available
3808 (see builtin_type_for_size). In which case the function type as a whole
3809 should be error_mark_node. */
3811 static void
3812 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3814 tree t;
3815 tree *args = XALLOCAVEC (tree, n);
3816 va_list list;
3817 int i;
3819 va_start (list, n);
3820 for (i = 0; i < n; ++i)
3822 builtin_type a = (builtin_type) va_arg (list, int);
3823 t = builtin_types[a];
3824 if (t == error_mark_node)
3825 goto egress;
3826 args[i] = t;
3829 t = builtin_types[ret];
3830 if (t == error_mark_node)
3831 goto egress;
3832 if (var)
3833 t = build_varargs_function_type_array (t, n, args);
3834 else
3835 t = build_function_type_array (t, n, args);
3837 egress:
3838 builtin_types[def] = t;
3839 va_end (list);
3842 /* Build builtin functions common to both C and C++ language
3843 frontends. */
3845 static void
3846 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3848 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3849 builtin_types[ENUM] = VALUE;
3850 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3851 def_fn_type (ENUM, RETURN, 0, 0);
3852 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3853 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3854 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3855 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3856 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3857 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3858 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3859 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3860 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3861 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3862 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3863 ARG6) \
3864 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3865 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3866 ARG6, ARG7) \
3867 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3868 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3869 ARG6, ARG7, ARG8) \
3870 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3871 ARG7, ARG8);
3872 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3873 ARG6, ARG7, ARG8, ARG9) \
3874 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3875 ARG7, ARG8, ARG9);
3876 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3877 ARG6, ARG7, ARG8, ARG9, ARG10) \
3878 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3879 ARG7, ARG8, ARG9, ARG10);
3880 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3881 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3882 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3883 ARG7, ARG8, ARG9, ARG10, ARG11);
3884 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3885 def_fn_type (ENUM, RETURN, 1, 0);
3886 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3887 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3888 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3889 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3890 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3891 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3892 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3893 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3894 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3895 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3896 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3897 ARG6) \
3898 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3899 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3900 ARG6, ARG7) \
3901 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3902 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3903 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3905 #include "builtin-types.def"
3907 #undef DEF_PRIMITIVE_TYPE
3908 #undef DEF_FUNCTION_TYPE_0
3909 #undef DEF_FUNCTION_TYPE_1
3910 #undef DEF_FUNCTION_TYPE_2
3911 #undef DEF_FUNCTION_TYPE_3
3912 #undef DEF_FUNCTION_TYPE_4
3913 #undef DEF_FUNCTION_TYPE_5
3914 #undef DEF_FUNCTION_TYPE_6
3915 #undef DEF_FUNCTION_TYPE_7
3916 #undef DEF_FUNCTION_TYPE_8
3917 #undef DEF_FUNCTION_TYPE_9
3918 #undef DEF_FUNCTION_TYPE_10
3919 #undef DEF_FUNCTION_TYPE_11
3920 #undef DEF_FUNCTION_TYPE_VAR_0
3921 #undef DEF_FUNCTION_TYPE_VAR_1
3922 #undef DEF_FUNCTION_TYPE_VAR_2
3923 #undef DEF_FUNCTION_TYPE_VAR_3
3924 #undef DEF_FUNCTION_TYPE_VAR_4
3925 #undef DEF_FUNCTION_TYPE_VAR_5
3926 #undef DEF_FUNCTION_TYPE_VAR_6
3927 #undef DEF_FUNCTION_TYPE_VAR_7
3928 #undef DEF_POINTER_TYPE
3929 builtin_types[(int) BT_LAST] = NULL_TREE;
3931 c_init_attributes ();
3933 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3934 NONANSI_P, ATTRS, IMPLICIT, COND) \
3935 if (NAME && COND) \
3936 def_builtin_1 (ENUM, NAME, CLASS, \
3937 builtin_types[(int) TYPE], \
3938 builtin_types[(int) LIBTYPE], \
3939 BOTH_P, FALLBACK_P, NONANSI_P, \
3940 built_in_attributes[(int) ATTRS], IMPLICIT);
3941 #include "builtins.def"
3943 targetm.init_builtins ();
3945 build_common_builtin_nodes ();
3948 /* Like get_identifier, but avoid warnings about null arguments when
3949 the argument may be NULL for targets where GCC lacks stdint.h type
3950 information. */
3952 static inline tree
3953 c_get_ident (const char *id)
3955 return get_identifier (id);
3958 /* Build tree nodes and builtin functions common to both C and C++ language
3959 frontends. */
3961 void
3962 c_common_nodes_and_builtins (void)
3964 int char16_type_size;
3965 int char32_type_size;
3966 int wchar_type_size;
3967 tree array_domain_type;
3968 tree va_list_ref_type_node;
3969 tree va_list_arg_type_node;
3970 int i;
3972 build_common_tree_nodes (flag_signed_char);
3974 /* Define `int' and `char' first so that dbx will output them first. */
3975 record_builtin_type (RID_INT, NULL, integer_type_node);
3976 record_builtin_type (RID_CHAR, "char", char_type_node);
3978 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3979 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3980 but not C. Are the conditionals here needed? */
3981 if (c_dialect_cxx ())
3982 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3983 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3984 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3985 record_builtin_type (RID_MAX, "long unsigned int",
3986 long_unsigned_type_node);
3988 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3990 char name[25];
3992 sprintf (name, "__int%d", int_n_data[i].bitsize);
3993 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
3994 int_n_trees[i].signed_type);
3995 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
3996 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
3999 if (c_dialect_cxx ())
4000 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4001 record_builtin_type (RID_MAX, "long long int",
4002 long_long_integer_type_node);
4003 record_builtin_type (RID_MAX, "long long unsigned int",
4004 long_long_unsigned_type_node);
4005 if (c_dialect_cxx ())
4006 record_builtin_type (RID_MAX, "long long unsigned",
4007 long_long_unsigned_type_node);
4008 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4009 record_builtin_type (RID_MAX, "short unsigned int",
4010 short_unsigned_type_node);
4011 if (c_dialect_cxx ())
4012 record_builtin_type (RID_MAX, "unsigned short",
4013 short_unsigned_type_node);
4015 /* Define both `signed char' and `unsigned char'. */
4016 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4017 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4019 /* These are types that c_common_type_for_size and
4020 c_common_type_for_mode use. */
4021 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4022 TYPE_DECL, NULL_TREE,
4023 intQI_type_node));
4024 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4025 TYPE_DECL, NULL_TREE,
4026 intHI_type_node));
4027 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4028 TYPE_DECL, NULL_TREE,
4029 intSI_type_node));
4030 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4031 TYPE_DECL, NULL_TREE,
4032 intDI_type_node));
4033 #if HOST_BITS_PER_WIDE_INT >= 64
4034 /* Note that this is different than the __int128 type that's part of
4035 the generic __intN support. */
4036 if (targetm.scalar_mode_supported_p (TImode))
4037 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4038 TYPE_DECL,
4039 get_identifier ("__int128_t"),
4040 intTI_type_node));
4041 #endif
4042 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4043 TYPE_DECL, NULL_TREE,
4044 unsigned_intQI_type_node));
4045 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4046 TYPE_DECL, NULL_TREE,
4047 unsigned_intHI_type_node));
4048 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4049 TYPE_DECL, NULL_TREE,
4050 unsigned_intSI_type_node));
4051 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4052 TYPE_DECL, NULL_TREE,
4053 unsigned_intDI_type_node));
4054 #if HOST_BITS_PER_WIDE_INT >= 64
4055 if (targetm.scalar_mode_supported_p (TImode))
4056 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4057 TYPE_DECL,
4058 get_identifier ("__uint128_t"),
4059 unsigned_intTI_type_node));
4060 #endif
4062 /* Create the widest literal types. */
4063 if (targetm.scalar_mode_supported_p (TImode))
4065 widest_integer_literal_type_node = intTI_type_node;
4066 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4068 else
4070 widest_integer_literal_type_node = intDI_type_node;
4071 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4074 signed_size_type_node = c_common_signed_type (size_type_node);
4076 pid_type_node =
4077 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4079 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4080 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4081 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4083 if (!c_dialect_cxx ())
4084 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4085 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4086 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4087 FLOATN_NX_TYPE_NODE (i));
4089 /* Only supported decimal floating point extension if the target
4090 actually supports underlying modes. */
4091 if (targetm.scalar_mode_supported_p (SDmode)
4092 && targetm.scalar_mode_supported_p (DDmode)
4093 && targetm.scalar_mode_supported_p (TDmode))
4095 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4096 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4097 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4100 if (targetm.fixed_point_supported_p ())
4102 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4103 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4104 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4105 record_builtin_type (RID_MAX, "long long _Fract",
4106 long_long_fract_type_node);
4107 record_builtin_type (RID_MAX, "unsigned short _Fract",
4108 unsigned_short_fract_type_node);
4109 record_builtin_type (RID_MAX, "unsigned _Fract",
4110 unsigned_fract_type_node);
4111 record_builtin_type (RID_MAX, "unsigned long _Fract",
4112 unsigned_long_fract_type_node);
4113 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4114 unsigned_long_long_fract_type_node);
4115 record_builtin_type (RID_MAX, "_Sat short _Fract",
4116 sat_short_fract_type_node);
4117 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4118 record_builtin_type (RID_MAX, "_Sat long _Fract",
4119 sat_long_fract_type_node);
4120 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4121 sat_long_long_fract_type_node);
4122 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4123 sat_unsigned_short_fract_type_node);
4124 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4125 sat_unsigned_fract_type_node);
4126 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4127 sat_unsigned_long_fract_type_node);
4128 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4129 sat_unsigned_long_long_fract_type_node);
4130 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4131 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4132 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4133 record_builtin_type (RID_MAX, "long long _Accum",
4134 long_long_accum_type_node);
4135 record_builtin_type (RID_MAX, "unsigned short _Accum",
4136 unsigned_short_accum_type_node);
4137 record_builtin_type (RID_MAX, "unsigned _Accum",
4138 unsigned_accum_type_node);
4139 record_builtin_type (RID_MAX, "unsigned long _Accum",
4140 unsigned_long_accum_type_node);
4141 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4142 unsigned_long_long_accum_type_node);
4143 record_builtin_type (RID_MAX, "_Sat short _Accum",
4144 sat_short_accum_type_node);
4145 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4146 record_builtin_type (RID_MAX, "_Sat long _Accum",
4147 sat_long_accum_type_node);
4148 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4149 sat_long_long_accum_type_node);
4150 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4151 sat_unsigned_short_accum_type_node);
4152 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4153 sat_unsigned_accum_type_node);
4154 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4155 sat_unsigned_long_accum_type_node);
4156 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4157 sat_unsigned_long_long_accum_type_node);
4161 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4162 TYPE_DECL,
4163 get_identifier ("complex int"),
4164 complex_integer_type_node));
4165 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4166 TYPE_DECL,
4167 get_identifier ("complex float"),
4168 complex_float_type_node));
4169 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4170 TYPE_DECL,
4171 get_identifier ("complex double"),
4172 complex_double_type_node));
4173 lang_hooks.decls.pushdecl
4174 (build_decl (UNKNOWN_LOCATION,
4175 TYPE_DECL, get_identifier ("complex long double"),
4176 complex_long_double_type_node));
4178 if (!c_dialect_cxx ())
4179 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4180 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4182 char buf[30];
4183 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4184 floatn_nx_types[i].extended ? "x" : "");
4185 lang_hooks.decls.pushdecl
4186 (build_decl (UNKNOWN_LOCATION,
4187 TYPE_DECL,
4188 get_identifier (buf),
4189 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4192 if (c_dialect_cxx ())
4194 /* For C++, make fileptr_type_node a distinct void * type until
4195 FILE type is defined. Likewise for const struct tm*. */
4196 for (unsigned i = 0;
4197 i < sizeof (builtin_structptr_types)
4198 / sizeof (builtin_structptr_type);
4199 ++i)
4200 builtin_structptr_types[i].node =
4201 build_variant_type_copy (builtin_structptr_types[i].base);
4205 record_builtin_type (RID_VOID, NULL, void_type_node);
4207 /* Set the TYPE_NAME for any variants that were built before
4208 record_builtin_type gave names to the built-in types. */
4210 tree void_name = TYPE_NAME (void_type_node);
4211 TYPE_NAME (void_type_node) = NULL_TREE;
4212 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4213 = void_name;
4214 TYPE_NAME (void_type_node) = void_name;
4217 void_list_node = build_void_list_node ();
4219 /* Make a type to be the domain of a few array types
4220 whose domains don't really matter.
4221 200 is small enough that it always fits in size_t
4222 and large enough that it can hold most function names for the
4223 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4224 array_domain_type = build_index_type (size_int (200));
4226 /* Make a type for arrays of characters.
4227 With luck nothing will ever really depend on the length of this
4228 array type. */
4229 char_array_type_node
4230 = build_array_type (char_type_node, array_domain_type);
4232 string_type_node = build_pointer_type (char_type_node);
4233 const_string_type_node
4234 = build_pointer_type (build_qualified_type
4235 (char_type_node, TYPE_QUAL_CONST));
4237 /* This is special for C++ so functions can be overloaded. */
4238 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4239 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4240 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4241 underlying_wchar_type_node = wchar_type_node;
4242 if (c_dialect_cxx ())
4244 if (TYPE_UNSIGNED (wchar_type_node))
4245 wchar_type_node = make_unsigned_type (wchar_type_size);
4246 else
4247 wchar_type_node = make_signed_type (wchar_type_size);
4248 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4251 /* This is for wide string constants. */
4252 wchar_array_type_node
4253 = build_array_type (wchar_type_node, array_domain_type);
4255 /* Define 'char16_t'. */
4256 char16_type_node = get_identifier (CHAR16_TYPE);
4257 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4258 char16_type_size = TYPE_PRECISION (char16_type_node);
4259 if (c_dialect_cxx ())
4261 char16_type_node = make_unsigned_type (char16_type_size);
4263 if (cxx_dialect >= cxx11)
4264 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4267 /* This is for UTF-16 string constants. */
4268 char16_array_type_node
4269 = build_array_type (char16_type_node, array_domain_type);
4271 /* Define 'char32_t'. */
4272 char32_type_node = get_identifier (CHAR32_TYPE);
4273 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4274 char32_type_size = TYPE_PRECISION (char32_type_node);
4275 if (c_dialect_cxx ())
4277 char32_type_node = make_unsigned_type (char32_type_size);
4279 if (cxx_dialect >= cxx11)
4280 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4283 /* This is for UTF-32 string constants. */
4284 char32_array_type_node
4285 = build_array_type (char32_type_node, array_domain_type);
4287 wint_type_node =
4288 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4290 intmax_type_node =
4291 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4292 uintmax_type_node =
4293 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4295 if (SIG_ATOMIC_TYPE)
4296 sig_atomic_type_node =
4297 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4298 if (INT8_TYPE)
4299 int8_type_node =
4300 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4301 if (INT16_TYPE)
4302 int16_type_node =
4303 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4304 if (INT32_TYPE)
4305 int32_type_node =
4306 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4307 if (INT64_TYPE)
4308 int64_type_node =
4309 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4310 if (UINT8_TYPE)
4311 uint8_type_node =
4312 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4313 if (UINT16_TYPE)
4314 c_uint16_type_node = uint16_type_node =
4315 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4316 if (UINT32_TYPE)
4317 c_uint32_type_node = uint32_type_node =
4318 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4319 if (UINT64_TYPE)
4320 c_uint64_type_node = uint64_type_node =
4321 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4322 if (INT_LEAST8_TYPE)
4323 int_least8_type_node =
4324 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4325 if (INT_LEAST16_TYPE)
4326 int_least16_type_node =
4327 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4328 if (INT_LEAST32_TYPE)
4329 int_least32_type_node =
4330 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4331 if (INT_LEAST64_TYPE)
4332 int_least64_type_node =
4333 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4334 if (UINT_LEAST8_TYPE)
4335 uint_least8_type_node =
4336 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4337 if (UINT_LEAST16_TYPE)
4338 uint_least16_type_node =
4339 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4340 if (UINT_LEAST32_TYPE)
4341 uint_least32_type_node =
4342 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4343 if (UINT_LEAST64_TYPE)
4344 uint_least64_type_node =
4345 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4346 if (INT_FAST8_TYPE)
4347 int_fast8_type_node =
4348 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4349 if (INT_FAST16_TYPE)
4350 int_fast16_type_node =
4351 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4352 if (INT_FAST32_TYPE)
4353 int_fast32_type_node =
4354 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4355 if (INT_FAST64_TYPE)
4356 int_fast64_type_node =
4357 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4358 if (UINT_FAST8_TYPE)
4359 uint_fast8_type_node =
4360 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4361 if (UINT_FAST16_TYPE)
4362 uint_fast16_type_node =
4363 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4364 if (UINT_FAST32_TYPE)
4365 uint_fast32_type_node =
4366 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4367 if (UINT_FAST64_TYPE)
4368 uint_fast64_type_node =
4369 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4370 if (INTPTR_TYPE)
4371 intptr_type_node =
4372 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4373 if (UINTPTR_TYPE)
4374 uintptr_type_node =
4375 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4377 default_function_type
4378 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4379 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4381 lang_hooks.decls.pushdecl
4382 (build_decl (UNKNOWN_LOCATION,
4383 TYPE_DECL, get_identifier ("__builtin_va_list"),
4384 va_list_type_node));
4385 if (targetm.enum_va_list_p)
4387 int l;
4388 const char *pname;
4389 tree ptype;
4391 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4393 lang_hooks.decls.pushdecl
4394 (build_decl (UNKNOWN_LOCATION,
4395 TYPE_DECL, get_identifier (pname),
4396 ptype));
4401 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4403 va_list_arg_type_node = va_list_ref_type_node =
4404 build_pointer_type (TREE_TYPE (va_list_type_node));
4406 else
4408 va_list_arg_type_node = va_list_type_node;
4409 va_list_ref_type_node = build_reference_type (va_list_type_node);
4412 if (!flag_preprocess_only)
4413 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4415 main_identifier_node = get_identifier ("main");
4417 /* Create the built-in __null node. It is important that this is
4418 not shared. */
4419 null_node = make_int_cst (1, 1);
4420 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4422 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4423 memset (builtin_types, 0, sizeof (builtin_types));
4426 /* The number of named compound-literals generated thus far. */
4427 static GTY(()) int compound_literal_number;
4429 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4431 void
4432 set_compound_literal_name (tree decl)
4434 char *name;
4435 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4436 compound_literal_number);
4437 compound_literal_number++;
4438 DECL_NAME (decl) = get_identifier (name);
4441 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4442 TYPE and operand OP. */
4444 static tree
4445 build_va_arg_1 (location_t loc, tree type, tree op)
4447 tree expr = build1 (VA_ARG_EXPR, type, op);
4448 SET_EXPR_LOCATION (expr, loc);
4449 return expr;
4452 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4453 va_arg (EXPR, TYPE) at source location LOC. */
4455 tree
4456 build_va_arg (location_t loc, tree expr, tree type)
4458 tree va_type = TREE_TYPE (expr);
4459 tree canon_va_type = (va_type == error_mark_node
4460 ? error_mark_node
4461 : targetm.canonical_va_list_type (va_type));
4463 if (va_type == error_mark_node
4464 || canon_va_type == NULL_TREE)
4466 if (canon_va_type == NULL_TREE)
4467 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4469 /* Let's handle things neutrallly, if expr:
4470 - has undeclared type, or
4471 - is not an va_list type. */
4472 return build_va_arg_1 (loc, type, error_mark_node);
4475 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4477 /* Case 1: Not an array type. */
4479 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4480 type. */
4481 mark_addressable (expr);
4482 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4484 return build_va_arg_1 (loc, type, expr);
4487 /* Case 2: Array type.
4489 Background:
4491 For contrast, let's start with the simple case (case 1). If
4492 canon_va_type is not an array type, but say a char *, then when
4493 passing-by-value a va_list, the type of the va_list param decl is
4494 the same as for another va_list decl (all ap's are char *):
4496 f2_1 (char * ap)
4497 D.1815 = VA_ARG (&ap, 0B, 1);
4498 return D.1815;
4500 f2 (int i)
4501 char * ap.0;
4502 char * ap;
4503 __builtin_va_start (&ap, 0);
4504 ap.0 = ap;
4505 res = f2_1 (ap.0);
4506 __builtin_va_end (&ap);
4507 D.1812 = res;
4508 return D.1812;
4510 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4511 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4512 the same as for another va_list decl (case 2a, struct ap[1]).
4514 f2_1 (struct * ap)
4515 D.1844 = VA_ARG (ap, 0B, 0);
4516 return D.1844;
4518 f2 (int i)
4519 struct ap[1];
4520 __builtin_va_start (&ap, 0);
4521 res = f2_1 (&ap);
4522 __builtin_va_end (&ap);
4523 D.1841 = res;
4524 return D.1841;
4526 Case 2b is different because:
4527 - on the callee side, the parm decl has declared type va_list, but
4528 grokdeclarator changes the type of the parm decl to a pointer to the
4529 array elem type.
4530 - on the caller side, the pass-by-value uses &ap.
4532 We unify these two cases (case 2a: va_list is array type,
4533 case 2b: va_list is pointer to array elem type), by adding '&' for the
4534 array type case, such that we have a pointer to array elem in both
4535 cases. */
4537 if (TREE_CODE (va_type) == ARRAY_TYPE)
4539 /* Case 2a: va_list is array type. */
4541 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4542 elem type. */
4543 mark_addressable (expr);
4544 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4545 expr);
4547 /* Verify that &ap is still recognized as having va_list type. */
4548 tree canon_expr_type
4549 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4550 gcc_assert (canon_expr_type != NULL_TREE);
4552 else
4554 /* Case 2b: va_list is pointer to array elem type. */
4555 gcc_assert (POINTER_TYPE_P (va_type));
4557 /* Comparison as in std_canonical_va_list_type. */
4558 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4559 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4561 /* Don't take the address. We've already got '&ap'. */
4565 return build_va_arg_1 (loc, type, expr);
4569 /* Linked list of disabled built-in functions. */
4571 struct disabled_builtin
4573 const char *name;
4574 struct disabled_builtin *next;
4576 static disabled_builtin *disabled_builtins = NULL;
4578 static bool builtin_function_disabled_p (const char *);
4580 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4581 begins with "__builtin_", give an error. */
4583 void
4584 disable_builtin_function (const char *name)
4586 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4587 error ("cannot disable built-in function %qs", name);
4588 else
4590 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4591 new_disabled_builtin->name = name;
4592 new_disabled_builtin->next = disabled_builtins;
4593 disabled_builtins = new_disabled_builtin;
4598 /* Return true if the built-in function NAME has been disabled, false
4599 otherwise. */
4601 static bool
4602 builtin_function_disabled_p (const char *name)
4604 disabled_builtin *p;
4605 for (p = disabled_builtins; p != NULL; p = p->next)
4607 if (strcmp (name, p->name) == 0)
4608 return true;
4610 return false;
4614 /* Worker for DEF_BUILTIN.
4615 Possibly define a builtin function with one or two names.
4616 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4617 nonansi_p and flag_no_nonansi_builtin. */
4619 static void
4620 def_builtin_1 (enum built_in_function fncode,
4621 const char *name,
4622 enum built_in_class fnclass,
4623 tree fntype, tree libtype,
4624 bool both_p, bool fallback_p, bool nonansi_p,
4625 tree fnattrs, bool implicit_p)
4627 tree decl;
4628 const char *libname;
4630 if (fntype == error_mark_node)
4631 return;
4633 gcc_assert ((!both_p && !fallback_p)
4634 || !strncmp (name, "__builtin_",
4635 strlen ("__builtin_")));
4637 libname = name + strlen ("__builtin_");
4638 decl = add_builtin_function (name, fntype, fncode, fnclass,
4639 (fallback_p ? libname : NULL),
4640 fnattrs);
4642 set_builtin_decl (fncode, decl, implicit_p);
4644 if (both_p
4645 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4646 && !(nonansi_p && flag_no_nonansi_builtin))
4647 add_builtin_function (libname, libtype, fncode, fnclass,
4648 NULL, fnattrs);
4651 /* Nonzero if the type T promotes to int. This is (nearly) the
4652 integral promotions defined in ISO C99 6.3.1.1/2. */
4654 bool
4655 c_promoting_integer_type_p (const_tree t)
4657 switch (TREE_CODE (t))
4659 case INTEGER_TYPE:
4660 return (TYPE_MAIN_VARIANT (t) == char_type_node
4661 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4662 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4663 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4664 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4665 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4667 case ENUMERAL_TYPE:
4668 /* ??? Technically all enumerations not larger than an int
4669 promote to an int. But this is used along code paths
4670 that only want to notice a size change. */
4671 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4673 case BOOLEAN_TYPE:
4674 return true;
4676 default:
4677 return false;
4681 /* Return 1 if PARMS specifies a fixed number of parameters
4682 and none of their types is affected by default promotions. */
4684 bool
4685 self_promoting_args_p (const_tree parms)
4687 const_tree t;
4688 for (t = parms; t; t = TREE_CHAIN (t))
4690 tree type = TREE_VALUE (t);
4692 if (type == error_mark_node)
4693 continue;
4695 if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
4696 return false;
4698 if (type == NULL_TREE)
4699 return false;
4701 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4702 return false;
4704 if (c_promoting_integer_type_p (type))
4705 return false;
4707 return true;
4710 /* Recursively remove any '*' or '&' operator from TYPE. */
4711 tree
4712 strip_pointer_operator (tree t)
4714 while (POINTER_TYPE_P (t))
4715 t = TREE_TYPE (t);
4716 return t;
4719 /* Recursively remove pointer or array type from TYPE. */
4720 tree
4721 strip_pointer_or_array_types (tree t)
4723 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4724 t = TREE_TYPE (t);
4725 return t;
4728 /* Used to compare case labels. K1 and K2 are actually tree nodes
4729 representing case labels, or NULL_TREE for a `default' label.
4730 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4731 K2, and 0 if K1 and K2 are equal. */
4734 case_compare (splay_tree_key k1, splay_tree_key k2)
4736 /* Consider a NULL key (such as arises with a `default' label) to be
4737 smaller than anything else. */
4738 if (!k1)
4739 return k2 ? -1 : 0;
4740 else if (!k2)
4741 return k1 ? 1 : 0;
4743 return tree_int_cst_compare ((tree) k1, (tree) k2);
4746 /* Process a case label, located at LOC, for the range LOW_VALUE
4747 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4748 then this case label is actually a `default' label. If only
4749 HIGH_VALUE is NULL_TREE, then case label was declared using the
4750 usual C/C++ syntax, rather than the GNU case range extension.
4751 CASES is a tree containing all the case ranges processed so far;
4752 COND is the condition for the switch-statement itself.
4753 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4754 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4755 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4757 tree
4758 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4759 tree low_value, tree high_value, bool *outside_range_p)
4761 tree type;
4762 tree label;
4763 tree case_label;
4764 splay_tree_node node;
4766 /* Create the LABEL_DECL itself. */
4767 label = create_artificial_label (loc);
4769 /* If there was an error processing the switch condition, bail now
4770 before we get more confused. */
4771 if (!cond || cond == error_mark_node)
4772 goto error_out;
4774 if ((low_value && TREE_TYPE (low_value)
4775 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4776 || (high_value && TREE_TYPE (high_value)
4777 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4779 error_at (loc, "pointers are not permitted as case values");
4780 goto error_out;
4783 /* Case ranges are a GNU extension. */
4784 if (high_value)
4785 pedwarn (loc, OPT_Wpedantic,
4786 "range expressions in switch statements are non-standard");
4788 type = TREE_TYPE (cond);
4789 if (low_value)
4791 low_value = check_case_value (loc, low_value);
4792 low_value = convert_and_check (loc, type, low_value);
4793 if (low_value == error_mark_node)
4794 goto error_out;
4796 if (high_value)
4798 high_value = check_case_value (loc, high_value);
4799 high_value = convert_and_check (loc, type, high_value);
4800 if (high_value == error_mark_node)
4801 goto error_out;
4804 if (low_value && high_value)
4806 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4807 really a case range, even though it was written that way.
4808 Remove the HIGH_VALUE to simplify later processing. */
4809 if (tree_int_cst_equal (low_value, high_value))
4810 high_value = NULL_TREE;
4811 else if (!tree_int_cst_lt (low_value, high_value))
4812 warning_at (loc, 0, "empty range specified");
4815 /* See if the case is in range of the type of the original testing
4816 expression. If both low_value and high_value are out of range,
4817 don't insert the case label and return NULL_TREE. */
4818 if (low_value
4819 && !check_case_bounds (loc, type, orig_type,
4820 &low_value, high_value ? &high_value : NULL,
4821 outside_range_p))
4822 return NULL_TREE;
4824 /* Look up the LOW_VALUE in the table of case labels we already
4825 have. */
4826 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4827 /* If there was not an exact match, check for overlapping ranges.
4828 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4829 that's a `default' label and the only overlap is an exact match. */
4830 if (!node && (low_value || high_value))
4832 splay_tree_node low_bound;
4833 splay_tree_node high_bound;
4835 /* Even though there wasn't an exact match, there might be an
4836 overlap between this case range and another case range.
4837 Since we've (inductively) not allowed any overlapping case
4838 ranges, we simply need to find the greatest low case label
4839 that is smaller that LOW_VALUE, and the smallest low case
4840 label that is greater than LOW_VALUE. If there is an overlap
4841 it will occur in one of these two ranges. */
4842 low_bound = splay_tree_predecessor (cases,
4843 (splay_tree_key) low_value);
4844 high_bound = splay_tree_successor (cases,
4845 (splay_tree_key) low_value);
4847 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4848 the LOW_VALUE, so there is no need to check unless the
4849 LOW_BOUND is in fact itself a case range. */
4850 if (low_bound
4851 && CASE_HIGH ((tree) low_bound->value)
4852 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4853 low_value) >= 0)
4854 node = low_bound;
4855 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4856 range is bigger than the low end of the current range, so we
4857 are only interested if the current range is a real range, and
4858 not an ordinary case label. */
4859 else if (high_bound
4860 && high_value
4861 && (tree_int_cst_compare ((tree) high_bound->key,
4862 high_value)
4863 <= 0))
4864 node = high_bound;
4866 /* If there was an overlap, issue an error. */
4867 if (node)
4869 tree duplicate = CASE_LABEL ((tree) node->value);
4871 if (high_value)
4873 error_at (loc, "duplicate (or overlapping) case value");
4874 inform (DECL_SOURCE_LOCATION (duplicate),
4875 "this is the first entry overlapping that value");
4877 else if (low_value)
4879 error_at (loc, "duplicate case value") ;
4880 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4882 else
4884 error_at (loc, "multiple default labels in one switch");
4885 inform (DECL_SOURCE_LOCATION (duplicate),
4886 "this is the first default label");
4888 goto error_out;
4891 /* Add a CASE_LABEL to the statement-tree. */
4892 case_label = add_stmt (build_case_label (low_value, high_value, label));
4893 /* Register this case label in the splay tree. */
4894 splay_tree_insert (cases,
4895 (splay_tree_key) low_value,
4896 (splay_tree_value) case_label);
4898 return case_label;
4900 error_out:
4901 /* Add a label so that the back-end doesn't think that the beginning of
4902 the switch is unreachable. Note that we do not add a case label, as
4903 that just leads to duplicates and thence to failure later on. */
4904 if (!cases->root)
4906 tree t = create_artificial_label (loc);
4907 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4909 return error_mark_node;
4912 /* Subroutine of c_switch_covers_all_cases_p, called via
4913 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4914 ARGS[0] is initially NULL and after the first iteration is the
4915 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4916 type. */
4918 static int
4919 c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
4921 tree label = (tree) node->value;
4922 tree *args = (tree *) data;
4924 /* If there is a default case, we shouldn't have called this. */
4925 gcc_assert (CASE_LOW (label));
4927 if (args[0] == NULL_TREE)
4929 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
4930 return 1;
4932 else if (wi::add (wi::to_widest (args[0]), 1)
4933 != wi::to_widest (CASE_LOW (label)))
4934 return 1;
4935 if (CASE_HIGH (label))
4936 args[0] = CASE_HIGH (label);
4937 else
4938 args[0] = CASE_LOW (label);
4939 return 0;
4942 /* Return true if switch with CASES and switch condition with type
4943 covers all possible values in the case labels. */
4945 bool
4946 c_switch_covers_all_cases_p (splay_tree cases, tree type)
4948 /* If there is default:, this is always the case. */
4949 splay_tree_node default_node
4950 = splay_tree_lookup (cases, (splay_tree_key) NULL);
4951 if (default_node)
4952 return true;
4954 if (!INTEGRAL_TYPE_P (type))
4955 return false;
4957 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
4958 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
4959 return false;
4961 /* If there are no cases at all, or if the highest case label
4962 is smaller than TYPE_MAX_VALUE, return false. */
4963 if (args[0] == NULL_TREE
4964 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
4965 return false;
4967 return true;
4970 /* Finish an expression taking the address of LABEL (an
4971 IDENTIFIER_NODE). Returns an expression for the address.
4973 LOC is the location for the expression returned. */
4975 tree
4976 finish_label_address_expr (tree label, location_t loc)
4978 tree result;
4980 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
4982 if (label == error_mark_node)
4983 return error_mark_node;
4985 label = lookup_label (label);
4986 if (label == NULL_TREE)
4987 result = null_pointer_node;
4988 else
4990 TREE_USED (label) = 1;
4991 result = build1 (ADDR_EXPR, ptr_type_node, label);
4992 /* The current function is not necessarily uninlinable.
4993 Computed gotos are incompatible with inlining, but the value
4994 here could be used only in a diagnostic, for example. */
4995 protected_set_expr_location (result, loc);
4998 return result;
5002 /* Given a boolean expression ARG, return a tree representing an increment
5003 or decrement (as indicated by CODE) of ARG. The front end must check for
5004 invalid cases (e.g., decrement in C++). */
5005 tree
5006 boolean_increment (enum tree_code code, tree arg)
5008 tree val;
5009 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5011 arg = stabilize_reference (arg);
5012 switch (code)
5014 case PREINCREMENT_EXPR:
5015 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5016 break;
5017 case POSTINCREMENT_EXPR:
5018 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5019 arg = save_expr (arg);
5020 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5021 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5022 break;
5023 case PREDECREMENT_EXPR:
5024 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5025 invert_truthvalue_loc (input_location, arg));
5026 break;
5027 case POSTDECREMENT_EXPR:
5028 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5029 invert_truthvalue_loc (input_location, arg));
5030 arg = save_expr (arg);
5031 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5032 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5033 break;
5034 default:
5035 gcc_unreachable ();
5037 TREE_SIDE_EFFECTS (val) = 1;
5038 return val;
5041 /* Built-in macros for stddef.h and stdint.h, that require macros
5042 defined in this file. */
5043 void
5044 c_stddef_cpp_builtins(void)
5046 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5047 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5048 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5049 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5050 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5051 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5052 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5053 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5054 if (SIG_ATOMIC_TYPE)
5055 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5056 if (INT8_TYPE)
5057 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5058 if (INT16_TYPE)
5059 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5060 if (INT32_TYPE)
5061 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5062 if (INT64_TYPE)
5063 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5064 if (UINT8_TYPE)
5065 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5066 if (UINT16_TYPE)
5067 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5068 if (UINT32_TYPE)
5069 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5070 if (UINT64_TYPE)
5071 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5072 if (INT_LEAST8_TYPE)
5073 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5074 if (INT_LEAST16_TYPE)
5075 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5076 if (INT_LEAST32_TYPE)
5077 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5078 if (INT_LEAST64_TYPE)
5079 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5080 if (UINT_LEAST8_TYPE)
5081 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5082 if (UINT_LEAST16_TYPE)
5083 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5084 if (UINT_LEAST32_TYPE)
5085 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5086 if (UINT_LEAST64_TYPE)
5087 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5088 if (INT_FAST8_TYPE)
5089 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5090 if (INT_FAST16_TYPE)
5091 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5092 if (INT_FAST32_TYPE)
5093 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5094 if (INT_FAST64_TYPE)
5095 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5096 if (UINT_FAST8_TYPE)
5097 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5098 if (UINT_FAST16_TYPE)
5099 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5100 if (UINT_FAST32_TYPE)
5101 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5102 if (UINT_FAST64_TYPE)
5103 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5104 if (INTPTR_TYPE)
5105 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5106 if (UINTPTR_TYPE)
5107 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5110 static void
5111 c_init_attributes (void)
5113 /* Fill in the built_in_attributes array. */
5114 #define DEF_ATTR_NULL_TREE(ENUM) \
5115 built_in_attributes[(int) ENUM] = NULL_TREE;
5116 #define DEF_ATTR_INT(ENUM, VALUE) \
5117 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5118 #define DEF_ATTR_STRING(ENUM, VALUE) \
5119 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5120 #define DEF_ATTR_IDENT(ENUM, STRING) \
5121 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5122 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5123 built_in_attributes[(int) ENUM] \
5124 = tree_cons (built_in_attributes[(int) PURPOSE], \
5125 built_in_attributes[(int) VALUE], \
5126 built_in_attributes[(int) CHAIN]);
5127 #include "builtin-attrs.def"
5128 #undef DEF_ATTR_NULL_TREE
5129 #undef DEF_ATTR_INT
5130 #undef DEF_ATTR_IDENT
5131 #undef DEF_ATTR_TREE_LIST
5134 /* Check whether ALIGN is a valid user-specified alignment. If so,
5135 return its base-2 log; if not, output an error and return -1. If
5136 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
5137 no error. */
5139 check_user_alignment (const_tree align, bool allow_zero)
5141 int i;
5143 if (error_operand_p (align))
5144 return -1;
5145 if (TREE_CODE (align) != INTEGER_CST
5146 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5148 error ("requested alignment is not an integer constant");
5149 return -1;
5151 else if (allow_zero && integer_zerop (align))
5152 return -1;
5153 else if (tree_int_cst_sgn (align) == -1
5154 || (i = tree_log2 (align)) == -1)
5156 error ("requested alignment is not a positive power of 2");
5157 return -1;
5159 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5161 error ("requested alignment is too large");
5162 return -1;
5164 return i;
5167 /* Determine the ELF symbol visibility for DECL, which is either a
5168 variable or a function. It is an error to use this function if a
5169 definition of DECL is not available in this translation unit.
5170 Returns true if the final visibility has been determined by this
5171 function; false if the caller is free to make additional
5172 modifications. */
5174 bool
5175 c_determine_visibility (tree decl)
5177 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5179 /* If the user explicitly specified the visibility with an
5180 attribute, honor that. DECL_VISIBILITY will have been set during
5181 the processing of the attribute. We check for an explicit
5182 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5183 to distinguish the use of an attribute from the use of a "#pragma
5184 GCC visibility push(...)"; in the latter case we still want other
5185 considerations to be able to overrule the #pragma. */
5186 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5187 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5188 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5189 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5190 return true;
5192 /* Set default visibility to whatever the user supplied with
5193 visibility_specified depending on #pragma GCC visibility. */
5194 if (!DECL_VISIBILITY_SPECIFIED (decl))
5196 if (visibility_options.inpragma
5197 || DECL_VISIBILITY (decl) != default_visibility)
5199 DECL_VISIBILITY (decl) = default_visibility;
5200 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5201 /* If visibility changed and DECL already has DECL_RTL, ensure
5202 symbol flags are updated. */
5203 if (((VAR_P (decl) && TREE_STATIC (decl))
5204 || TREE_CODE (decl) == FUNCTION_DECL)
5205 && DECL_RTL_SET_P (decl))
5206 make_decl_rtl (decl);
5209 return false;
5212 /* Data to communicate through check_function_arguments_recurse between
5213 check_function_nonnull and check_nonnull_arg. */
5215 struct nonnull_arg_ctx
5217 location_t loc;
5218 bool warned_p;
5221 /* Check the argument list of a function call for null in argument slots
5222 that are marked as requiring a non-null pointer argument. The NARGS
5223 arguments are passed in the array ARGARRAY. Return true if we have
5224 warned. */
5226 static bool
5227 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5229 tree a;
5230 int i;
5232 attrs = lookup_attribute ("nonnull", attrs);
5233 if (attrs == NULL_TREE)
5234 return false;
5236 a = attrs;
5237 /* See if any of the nonnull attributes has no arguments. If so,
5238 then every pointer argument is checked (in which case the check
5239 for pointer type is done in check_nonnull_arg). */
5240 if (TREE_VALUE (a) != NULL_TREE)
5242 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5243 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5245 struct nonnull_arg_ctx ctx = { loc, false };
5246 if (a != NULL_TREE)
5247 for (i = 0; i < nargs; i++)
5248 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5249 i + 1);
5250 else
5252 /* Walk the argument list. If we encounter an argument number we
5253 should check for non-null, do it. */
5254 for (i = 0; i < nargs; i++)
5256 for (a = attrs; ; a = TREE_CHAIN (a))
5258 a = lookup_attribute ("nonnull", a);
5259 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5260 break;
5263 if (a != NULL_TREE)
5264 check_function_arguments_recurse (check_nonnull_arg, &ctx,
5265 argarray[i], i + 1);
5268 return ctx.warned_p;
5271 /* Check that the Nth argument of a function call (counting backwards
5272 from the end) is a (pointer)0. The NARGS arguments are passed in the
5273 array ARGARRAY. */
5275 static void
5276 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5278 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5280 if (attr)
5282 int len = 0;
5283 int pos = 0;
5284 tree sentinel;
5285 function_args_iterator iter;
5286 tree t;
5288 /* Skip over the named arguments. */
5289 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5291 if (len == nargs)
5292 break;
5293 len++;
5296 if (TREE_VALUE (attr))
5298 tree p = TREE_VALUE (TREE_VALUE (attr));
5299 pos = TREE_INT_CST_LOW (p);
5302 /* The sentinel must be one of the varargs, i.e.
5303 in position >= the number of fixed arguments. */
5304 if ((nargs - 1 - pos) < len)
5306 warning (OPT_Wformat_,
5307 "not enough variable arguments to fit a sentinel");
5308 return;
5311 /* Validate the sentinel. */
5312 sentinel = argarray[nargs - 1 - pos];
5313 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5314 || !integer_zerop (sentinel))
5315 /* Although __null (in C++) is only an integer we allow it
5316 nevertheless, as we are guaranteed that it's exactly
5317 as wide as a pointer, and we don't want to force
5318 users to cast the NULL they have written there.
5319 We warn with -Wstrict-null-sentinel, though. */
5320 && (warn_strict_null_sentinel || null_node != sentinel))
5321 warning (OPT_Wformat_, "missing sentinel in function call");
5325 /* Check that the same argument isn't passed to restrict arguments
5326 and other arguments. */
5328 static void
5329 check_function_restrict (const_tree fndecl, const_tree fntype,
5330 int nargs, tree *argarray)
5332 int i;
5333 tree parms = TYPE_ARG_TYPES (fntype);
5335 if (fndecl
5336 && TREE_CODE (fndecl) == FUNCTION_DECL)
5338 /* Skip checking built-ins here. They are checked in more
5339 detail elsewhere. */
5340 if (DECL_BUILT_IN (fndecl)
5341 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
5342 return;
5344 if (DECL_ARGUMENTS (fndecl))
5345 parms = DECL_ARGUMENTS (fndecl);
5348 for (i = 0; i < nargs; i++)
5349 TREE_VISITED (argarray[i]) = 0;
5351 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5353 tree type;
5354 if (TREE_CODE (parms) == PARM_DECL)
5356 type = TREE_TYPE (parms);
5357 parms = DECL_CHAIN (parms);
5359 else
5361 type = TREE_VALUE (parms);
5362 parms = TREE_CHAIN (parms);
5364 if (POINTER_TYPE_P (type)
5365 && TYPE_RESTRICT (type)
5366 && !TYPE_READONLY (TREE_TYPE (type)))
5367 warn_for_restrict (i, argarray, nargs);
5370 for (i = 0; i < nargs; i++)
5371 TREE_VISITED (argarray[i]) = 0;
5374 /* Helper for check_function_nonnull; given a list of operands which
5375 must be non-null in ARGS, determine if operand PARAM_NUM should be
5376 checked. */
5378 static bool
5379 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5381 unsigned HOST_WIDE_INT arg_num = 0;
5383 for (; args; args = TREE_CHAIN (args))
5385 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5387 gcc_assert (found);
5389 if (arg_num == param_num)
5390 return true;
5392 return false;
5395 /* Check that the function argument PARAM (which is operand number
5396 PARAM_NUM) is non-null. This is called by check_function_nonnull
5397 via check_function_arguments_recurse. */
5399 static void
5400 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5402 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5404 /* Just skip checking the argument if it's not a pointer. This can
5405 happen if the "nonnull" attribute was given without an operand
5406 list (which means to check every pointer argument). */
5408 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5409 return;
5411 /* When not optimizing diagnose the simple cases of null arguments.
5412 When optimization is enabled defer the checking until expansion
5413 when more cases can be detected. */
5414 if (integer_zerop (param))
5416 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5417 "required (argument %lu)", (unsigned long) param_num);
5418 pctx->warned_p = true;
5422 /* Helper for nonnull attribute handling; fetch the operand number
5423 from the attribute argument list. */
5425 bool
5426 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5428 /* Verify the arg number is a small constant. */
5429 if (tree_fits_uhwi_p (arg_num_expr))
5431 *valp = tree_to_uhwi (arg_num_expr);
5432 return true;
5434 else
5435 return false;
5438 /* Arguments being collected for optimization. */
5439 typedef const char *const_char_p; /* For DEF_VEC_P. */
5440 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5443 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5444 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5445 false for #pragma GCC optimize. */
5447 bool
5448 parse_optimize_options (tree args, bool attr_p)
5450 bool ret = true;
5451 unsigned opt_argc;
5452 unsigned i;
5453 const char **opt_argv;
5454 struct cl_decoded_option *decoded_options;
5455 unsigned int decoded_options_count;
5456 tree ap;
5458 /* Build up argv vector. Just in case the string is stored away, use garbage
5459 collected strings. */
5460 vec_safe_truncate (optimize_args, 0);
5461 vec_safe_push (optimize_args, (const char *) NULL);
5463 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5465 tree value = TREE_VALUE (ap);
5467 if (TREE_CODE (value) == INTEGER_CST)
5469 char buffer[20];
5470 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5471 vec_safe_push (optimize_args, ggc_strdup (buffer));
5474 else if (TREE_CODE (value) == STRING_CST)
5476 /* Split string into multiple substrings. */
5477 size_t len = TREE_STRING_LENGTH (value);
5478 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5479 char *end = p + len;
5480 char *comma;
5481 char *next_p = p;
5483 while (next_p != NULL)
5485 size_t len2;
5486 char *q, *r;
5488 p = next_p;
5489 comma = strchr (p, ',');
5490 if (comma)
5492 len2 = comma - p;
5493 *comma = '\0';
5494 next_p = comma+1;
5496 else
5498 len2 = end - p;
5499 next_p = NULL;
5502 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5504 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5505 options. */
5506 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5508 ret = false;
5509 if (attr_p)
5510 warning (OPT_Wattributes,
5511 "bad option %qs to attribute %<optimize%>", p);
5512 else
5513 warning (OPT_Wpragmas,
5514 "bad option %qs to pragma %<optimize%>", p);
5515 continue;
5518 if (*p != '-')
5520 *r++ = '-';
5522 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5523 itself is -Os, and any other switch begins with a -f. */
5524 if ((*p >= '0' && *p <= '9')
5525 || (p[0] == 's' && p[1] == '\0'))
5526 *r++ = 'O';
5527 else if (*p != 'O')
5528 *r++ = 'f';
5531 memcpy (r, p, len2);
5532 r[len2] = '\0';
5533 vec_safe_push (optimize_args, (const char *) q);
5539 opt_argc = optimize_args->length ();
5540 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5542 for (i = 1; i < opt_argc; i++)
5543 opt_argv[i] = (*optimize_args)[i];
5545 /* Now parse the options. */
5546 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5547 &decoded_options,
5548 &decoded_options_count);
5549 /* Drop non-Optimization options. */
5550 unsigned j = 1;
5551 for (i = 1; i < decoded_options_count; ++i)
5553 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5555 ret = false;
5556 if (attr_p)
5557 warning (OPT_Wattributes,
5558 "bad option %qs to attribute %<optimize%>",
5559 decoded_options[i].orig_option_with_args_text);
5560 else
5561 warning (OPT_Wpragmas,
5562 "bad option %qs to pragma %<optimize%>",
5563 decoded_options[i].orig_option_with_args_text);
5564 continue;
5566 if (i != j)
5567 decoded_options[j] = decoded_options[i];
5568 j++;
5570 decoded_options_count = j;
5571 /* And apply them. */
5572 decode_options (&global_options, &global_options_set,
5573 decoded_options, decoded_options_count,
5574 input_location, global_dc, NULL);
5576 targetm.override_options_after_change();
5578 optimize_args->truncate (0);
5579 return ret;
5582 /* Check whether ATTR is a valid attribute fallthrough. */
5584 bool
5585 attribute_fallthrough_p (tree attr)
5587 if (attr == error_mark_node)
5588 return false;
5589 tree t = lookup_attribute ("fallthrough", attr);
5590 if (t == NULL_TREE)
5591 return false;
5592 /* This attribute shall appear at most once in each attribute-list. */
5593 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5594 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5595 "times");
5596 /* No attribute-argument-clause shall be present. */
5597 else if (TREE_VALUE (t) != NULL_TREE)
5598 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5599 "a parameter");
5600 /* Warn if other attributes are found. */
5601 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5603 tree name = get_attribute_name (t);
5604 if (!is_attribute_p ("fallthrough", name))
5605 warning (OPT_Wattributes, "%qE attribute ignored", name);
5607 return true;
5611 /* Check for valid arguments being passed to a function with FNTYPE.
5612 There are NARGS arguments in the array ARGARRAY. LOC should be used for
5613 diagnostics. Return true if -Wnonnull warning has been diagnosed. */
5614 bool
5615 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5616 int nargs, tree *argarray, vec<location_t> *arglocs)
5618 bool warned_p = false;
5620 /* Check for null being passed in a pointer argument that must be
5621 non-null. We also need to do this if format checking is enabled. */
5623 if (warn_nonnull)
5624 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5625 nargs, argarray);
5627 /* Check for errors in format strings. */
5629 if (warn_format || warn_suggest_attribute_format)
5630 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray, arglocs);
5632 if (warn_format)
5633 check_function_sentinel (fntype, nargs, argarray);
5635 if (warn_restrict)
5636 check_function_restrict (fndecl, fntype, nargs, argarray);
5637 return warned_p;
5640 /* Generic argument checking recursion routine. PARAM is the argument to
5641 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5642 once the argument is resolved. CTX is context for the callback. */
5643 void
5644 check_function_arguments_recurse (void (*callback)
5645 (void *, tree, unsigned HOST_WIDE_INT),
5646 void *ctx, tree param,
5647 unsigned HOST_WIDE_INT param_num)
5649 if (CONVERT_EXPR_P (param)
5650 && (TYPE_PRECISION (TREE_TYPE (param))
5651 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5653 /* Strip coercion. */
5654 check_function_arguments_recurse (callback, ctx,
5655 TREE_OPERAND (param, 0), param_num);
5656 return;
5659 if (TREE_CODE (param) == CALL_EXPR)
5661 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5662 tree attrs;
5663 bool found_format_arg = false;
5665 /* See if this is a call to a known internationalization function
5666 that modifies a format arg. Such a function may have multiple
5667 format_arg attributes (for example, ngettext). */
5669 for (attrs = TYPE_ATTRIBUTES (type);
5670 attrs;
5671 attrs = TREE_CHAIN (attrs))
5672 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5674 tree inner_arg;
5675 tree format_num_expr;
5676 int format_num;
5677 int i;
5678 call_expr_arg_iterator iter;
5680 /* Extract the argument number, which was previously checked
5681 to be valid. */
5682 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5684 format_num = tree_to_uhwi (format_num_expr);
5686 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5687 inner_arg != NULL_TREE;
5688 inner_arg = next_call_expr_arg (&iter), i++)
5689 if (i == format_num)
5691 check_function_arguments_recurse (callback, ctx,
5692 inner_arg, param_num);
5693 found_format_arg = true;
5694 break;
5698 /* If we found a format_arg attribute and did a recursive check,
5699 we are done with checking this argument. Otherwise, we continue
5700 and this will be considered a non-literal. */
5701 if (found_format_arg)
5702 return;
5705 if (TREE_CODE (param) == COND_EXPR)
5707 /* Simplify to avoid warning for an impossible case. */
5708 param = fold_for_warn (param);
5709 if (TREE_CODE (param) == COND_EXPR)
5711 /* Check both halves of the conditional expression. */
5712 check_function_arguments_recurse (callback, ctx,
5713 TREE_OPERAND (param, 1),
5714 param_num);
5715 check_function_arguments_recurse (callback, ctx,
5716 TREE_OPERAND (param, 2),
5717 param_num);
5718 return;
5722 (*callback) (ctx, param, param_num);
5725 /* Checks for a builtin function FNDECL that the number of arguments
5726 NARGS against the required number REQUIRED and issues an error if
5727 there is a mismatch. Returns true if the number of arguments is
5728 correct, otherwise false. LOC is the location of FNDECL. */
5730 static bool
5731 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5732 int required)
5734 if (nargs < required)
5736 error_at (loc, "too few arguments to function %qE", fndecl);
5737 return false;
5739 else if (nargs > required)
5741 error_at (loc, "too many arguments to function %qE", fndecl);
5742 return false;
5744 return true;
5747 /* Helper macro for check_builtin_function_arguments. */
5748 #define ARG_LOCATION(N) \
5749 (arg_loc.is_empty () \
5750 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5751 : expansion_point_location (arg_loc[(N)]))
5753 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5754 Returns false if there was an error, otherwise true. LOC is the
5755 location of the function; ARG_LOC is a vector of locations of the
5756 arguments. */
5758 bool
5759 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5760 tree fndecl, int nargs, tree *args)
5762 if (!DECL_BUILT_IN (fndecl)
5763 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5764 return true;
5766 switch (DECL_FUNCTION_CODE (fndecl))
5768 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5769 if (!tree_fits_uhwi_p (args[2]))
5771 error_at (ARG_LOCATION (2),
5772 "third argument to function %qE must be a constant integer",
5773 fndecl);
5774 return false;
5776 /* fall through */
5778 case BUILT_IN_ALLOCA_WITH_ALIGN:
5780 /* Get the requested alignment (in bits) if it's a constant
5781 integer expression. */
5782 unsigned HOST_WIDE_INT align
5783 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5785 /* Determine if the requested alignment is a power of 2. */
5786 if ((align & (align - 1)))
5787 align = 0;
5789 /* The maximum alignment in bits corresponding to the same
5790 maximum in bytes enforced in check_user_alignment(). */
5791 unsigned maxalign = (UINT_MAX >> 1) + 1;
5793 /* Reject invalid alignments. */
5794 if (align < BITS_PER_UNIT || maxalign < align)
5796 error_at (ARG_LOCATION (1),
5797 "second argument to function %qE must be a constant "
5798 "integer power of 2 between %qi and %qu bits",
5799 fndecl, BITS_PER_UNIT, maxalign);
5800 return false;
5802 return true;
5805 case BUILT_IN_CONSTANT_P:
5806 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5808 case BUILT_IN_ISFINITE:
5809 case BUILT_IN_ISINF:
5810 case BUILT_IN_ISINF_SIGN:
5811 case BUILT_IN_ISNAN:
5812 case BUILT_IN_ISNORMAL:
5813 case BUILT_IN_SIGNBIT:
5814 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5816 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5818 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5819 "call to function %qE", fndecl);
5820 return false;
5822 return true;
5824 return false;
5826 case BUILT_IN_ISGREATER:
5827 case BUILT_IN_ISGREATEREQUAL:
5828 case BUILT_IN_ISLESS:
5829 case BUILT_IN_ISLESSEQUAL:
5830 case BUILT_IN_ISLESSGREATER:
5831 case BUILT_IN_ISUNORDERED:
5832 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5834 enum tree_code code0, code1;
5835 code0 = TREE_CODE (TREE_TYPE (args[0]));
5836 code1 = TREE_CODE (TREE_TYPE (args[1]));
5837 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5838 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5839 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5841 error_at (loc, "non-floating-point arguments in call to "
5842 "function %qE", fndecl);
5843 return false;
5845 return true;
5847 return false;
5849 case BUILT_IN_FPCLASSIFY:
5850 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5852 for (unsigned int i = 0; i < 5; i++)
5853 if (TREE_CODE (args[i]) != INTEGER_CST)
5855 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5856 "call to function %qE", i + 1, fndecl);
5857 return false;
5860 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5862 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5863 "call to function %qE", fndecl);
5864 return false;
5866 return true;
5868 return false;
5870 case BUILT_IN_ASSUME_ALIGNED:
5871 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5873 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5875 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5876 "function %qE", fndecl);
5877 return false;
5879 return true;
5881 return false;
5883 case BUILT_IN_ADD_OVERFLOW:
5884 case BUILT_IN_SUB_OVERFLOW:
5885 case BUILT_IN_MUL_OVERFLOW:
5886 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5888 unsigned i;
5889 for (i = 0; i < 2; i++)
5890 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5892 error_at (ARG_LOCATION (i), "argument %u in call to function "
5893 "%qE does not have integral type", i + 1, fndecl);
5894 return false;
5896 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5897 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5899 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5900 "does not have pointer to integral type", fndecl);
5901 return false;
5903 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5905 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5906 "has pointer to enumerated type", fndecl);
5907 return false;
5909 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5911 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5912 "has pointer to boolean type", fndecl);
5913 return false;
5915 return true;
5917 return false;
5919 case BUILT_IN_ADD_OVERFLOW_P:
5920 case BUILT_IN_SUB_OVERFLOW_P:
5921 case BUILT_IN_MUL_OVERFLOW_P:
5922 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5924 unsigned i;
5925 for (i = 0; i < 3; i++)
5926 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5928 error_at (ARG_LOCATION (i), "argument %u in call to function "
5929 "%qE does not have integral type", i + 1, fndecl);
5930 return false;
5932 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5934 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5935 "%qE has enumerated type", fndecl);
5936 return false;
5938 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5940 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5941 "%qE has boolean type", fndecl);
5942 return false;
5944 return true;
5946 return false;
5948 default:
5949 return true;
5953 /* Subroutine of c_parse_error.
5954 Return the result of concatenating LHS and RHS. RHS is really
5955 a string literal, its first character is indicated by RHS_START and
5956 RHS_SIZE is its length (including the terminating NUL character).
5958 The caller is responsible for deleting the returned pointer. */
5960 static char *
5961 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5963 const size_t lhs_size = strlen (lhs);
5964 char *result = XNEWVEC (char, lhs_size + rhs_size);
5965 memcpy (result, lhs, lhs_size);
5966 memcpy (result + lhs_size, rhs_start, rhs_size);
5967 return result;
5970 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
5971 before TOKEN, which had the associated VALUE. */
5973 void
5974 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
5975 tree value, unsigned char token_flags,
5976 rich_location *richloc)
5978 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
5980 char *message = NULL;
5982 if (token_type == CPP_EOF)
5983 message = catenate_messages (gmsgid, " at end of input");
5984 else if (token_type == CPP_CHAR
5985 || token_type == CPP_WCHAR
5986 || token_type == CPP_CHAR16
5987 || token_type == CPP_CHAR32
5988 || token_type == CPP_UTF8CHAR)
5990 unsigned int val = TREE_INT_CST_LOW (value);
5991 const char *prefix;
5993 switch (token_type)
5995 default:
5996 prefix = "";
5997 break;
5998 case CPP_WCHAR:
5999 prefix = "L";
6000 break;
6001 case CPP_CHAR16:
6002 prefix = "u";
6003 break;
6004 case CPP_CHAR32:
6005 prefix = "U";
6006 break;
6007 case CPP_UTF8CHAR:
6008 prefix = "u8";
6009 break;
6012 if (val <= UCHAR_MAX && ISGRAPH (val))
6013 message = catenate_messages (gmsgid, " before %s'%c'");
6014 else
6015 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6017 error_at (richloc, message, prefix, val);
6018 free (message);
6019 message = NULL;
6021 else if (token_type == CPP_CHAR_USERDEF
6022 || token_type == CPP_WCHAR_USERDEF
6023 || token_type == CPP_CHAR16_USERDEF
6024 || token_type == CPP_CHAR32_USERDEF
6025 || token_type == CPP_UTF8CHAR_USERDEF)
6026 message = catenate_messages (gmsgid,
6027 " before user-defined character literal");
6028 else if (token_type == CPP_STRING_USERDEF
6029 || token_type == CPP_WSTRING_USERDEF
6030 || token_type == CPP_STRING16_USERDEF
6031 || token_type == CPP_STRING32_USERDEF
6032 || token_type == CPP_UTF8STRING_USERDEF)
6033 message = catenate_messages (gmsgid, " before user-defined string literal");
6034 else if (token_type == CPP_STRING
6035 || token_type == CPP_WSTRING
6036 || token_type == CPP_STRING16
6037 || token_type == CPP_STRING32
6038 || token_type == CPP_UTF8STRING)
6039 message = catenate_messages (gmsgid, " before string constant");
6040 else if (token_type == CPP_NUMBER)
6041 message = catenate_messages (gmsgid, " before numeric constant");
6042 else if (token_type == CPP_NAME)
6044 message = catenate_messages (gmsgid, " before %qE");
6045 error_at (richloc, message, value);
6046 free (message);
6047 message = NULL;
6049 else if (token_type == CPP_PRAGMA)
6050 message = catenate_messages (gmsgid, " before %<#pragma%>");
6051 else if (token_type == CPP_PRAGMA_EOL)
6052 message = catenate_messages (gmsgid, " before end of line");
6053 else if (token_type == CPP_DECLTYPE)
6054 message = catenate_messages (gmsgid, " before %<decltype%>");
6055 else if (token_type < N_TTYPES)
6057 message = catenate_messages (gmsgid, " before %qs token");
6058 error_at (richloc, message, cpp_type2name (token_type, token_flags));
6059 free (message);
6060 message = NULL;
6062 else
6063 error_at (richloc, gmsgid);
6065 if (message)
6067 error_at (richloc, message);
6068 free (message);
6070 #undef catenate_messages
6073 /* Return the gcc option code associated with the reason for a cpp
6074 message, or 0 if none. */
6076 static int
6077 c_option_controlling_cpp_error (int reason)
6079 const struct cpp_reason_option_codes_t *entry;
6081 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6083 if (entry->reason == reason)
6084 return entry->option_code;
6086 return 0;
6089 /* Callback from cpp_error for PFILE to print diagnostics from the
6090 preprocessor. The diagnostic is of type LEVEL, with REASON set
6091 to the reason code if LEVEL is represents a warning, at location
6092 RICHLOC unless this is after lexing and the compiler's location
6093 should be used instead; MSG is the translated message and AP
6094 the arguments. Returns true if a diagnostic was emitted, false
6095 otherwise. */
6097 bool
6098 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6099 rich_location *richloc,
6100 const char *msg, va_list *ap)
6102 diagnostic_info diagnostic;
6103 diagnostic_t dlevel;
6104 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6105 bool ret;
6107 switch (level)
6109 case CPP_DL_WARNING_SYSHDR:
6110 if (flag_no_output)
6111 return false;
6112 global_dc->dc_warn_system_headers = 1;
6113 /* Fall through. */
6114 case CPP_DL_WARNING:
6115 if (flag_no_output)
6116 return false;
6117 dlevel = DK_WARNING;
6118 break;
6119 case CPP_DL_PEDWARN:
6120 if (flag_no_output && !flag_pedantic_errors)
6121 return false;
6122 dlevel = DK_PEDWARN;
6123 break;
6124 case CPP_DL_ERROR:
6125 dlevel = DK_ERROR;
6126 break;
6127 case CPP_DL_ICE:
6128 dlevel = DK_ICE;
6129 break;
6130 case CPP_DL_NOTE:
6131 dlevel = DK_NOTE;
6132 break;
6133 case CPP_DL_FATAL:
6134 dlevel = DK_FATAL;
6135 break;
6136 default:
6137 gcc_unreachable ();
6139 if (done_lexing)
6140 richloc->set_range (line_table, 0, input_location, true);
6141 diagnostic_set_info_translated (&diagnostic, msg, ap,
6142 richloc, dlevel);
6143 diagnostic_override_option_index (&diagnostic,
6144 c_option_controlling_cpp_error (reason));
6145 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
6146 if (level == CPP_DL_WARNING_SYSHDR)
6147 global_dc->dc_warn_system_headers = save_warn_system_headers;
6148 return ret;
6151 /* Convert a character from the host to the target execution character
6152 set. cpplib handles this, mostly. */
6154 HOST_WIDE_INT
6155 c_common_to_target_charset (HOST_WIDE_INT c)
6157 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6158 zero-extended under -fno-signed-char. cpplib insists that characters
6159 and character constants are always unsigned. Hence we must convert
6160 back and forth. */
6161 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6163 uc = cpp_host_to_exec_charset (parse_in, uc);
6165 if (flag_signed_char)
6166 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6167 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6168 else
6169 return uc;
6172 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6173 references with an INDIRECT_REF of a constant at the bottom; much like the
6174 traditional rendering of offsetof as a macro. Return the folded result. */
6176 tree
6177 fold_offsetof_1 (tree expr, enum tree_code ctx)
6179 tree base, off, t;
6180 tree_code code = TREE_CODE (expr);
6181 switch (code)
6183 case ERROR_MARK:
6184 return expr;
6186 case VAR_DECL:
6187 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6188 return error_mark_node;
6190 case CALL_EXPR:
6191 case TARGET_EXPR:
6192 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6193 return error_mark_node;
6195 case NOP_EXPR:
6196 case INDIRECT_REF:
6197 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6199 error ("cannot apply %<offsetof%> to a non constant address");
6200 return error_mark_node;
6202 return TREE_OPERAND (expr, 0);
6204 case COMPONENT_REF:
6205 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6206 if (base == error_mark_node)
6207 return base;
6209 t = TREE_OPERAND (expr, 1);
6210 if (DECL_C_BIT_FIELD (t))
6212 error ("attempt to take address of bit-field structure "
6213 "member %qD", t);
6214 return error_mark_node;
6216 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6217 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6218 / BITS_PER_UNIT));
6219 break;
6221 case ARRAY_REF:
6222 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6223 if (base == error_mark_node)
6224 return base;
6226 t = TREE_OPERAND (expr, 1);
6228 /* Check if the offset goes beyond the upper bound of the array. */
6229 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6231 tree upbound = array_ref_up_bound (expr);
6232 if (upbound != NULL_TREE
6233 && TREE_CODE (upbound) == INTEGER_CST
6234 && !tree_int_cst_equal (upbound,
6235 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6237 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6238 upbound = size_binop (PLUS_EXPR, upbound,
6239 build_int_cst (TREE_TYPE (upbound), 1));
6240 if (tree_int_cst_lt (upbound, t))
6242 tree v;
6244 for (v = TREE_OPERAND (expr, 0);
6245 TREE_CODE (v) == COMPONENT_REF;
6246 v = TREE_OPERAND (v, 0))
6247 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6248 == RECORD_TYPE)
6250 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6251 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6252 if (TREE_CODE (fld_chain) == FIELD_DECL)
6253 break;
6255 if (fld_chain)
6256 break;
6258 /* Don't warn if the array might be considered a poor
6259 man's flexible array member with a very permissive
6260 definition thereof. */
6261 if (TREE_CODE (v) == ARRAY_REF
6262 || TREE_CODE (v) == COMPONENT_REF)
6263 warning (OPT_Warray_bounds,
6264 "index %E denotes an offset "
6265 "greater than size of %qT",
6266 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6271 t = convert (sizetype, t);
6272 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6273 break;
6275 case COMPOUND_EXPR:
6276 /* Handle static members of volatile structs. */
6277 t = TREE_OPERAND (expr, 1);
6278 gcc_assert (VAR_P (t));
6279 return fold_offsetof_1 (t);
6281 default:
6282 gcc_unreachable ();
6285 return fold_build_pointer_plus (base, off);
6288 /* Likewise, but convert it to the return type of offsetof. */
6290 tree
6291 fold_offsetof (tree expr)
6293 return convert (size_type_node, fold_offsetof_1 (expr));
6297 /* *PTYPE is an incomplete array. Complete it with a domain based on
6298 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6299 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6300 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6303 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6305 tree maxindex, type, main_type, elt, unqual_elt;
6306 int failure = 0, quals;
6307 bool overflow_p = false;
6309 maxindex = size_zero_node;
6310 if (initial_value)
6312 if (TREE_CODE (initial_value) == STRING_CST)
6314 int eltsize
6315 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6316 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6318 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6320 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6322 if (vec_safe_is_empty (v))
6324 if (pedantic)
6325 failure = 3;
6326 maxindex = ssize_int (-1);
6328 else
6330 tree curindex;
6331 unsigned HOST_WIDE_INT cnt;
6332 constructor_elt *ce;
6333 bool fold_p = false;
6335 if ((*v)[0].index)
6336 maxindex = (*v)[0].index, fold_p = true;
6338 curindex = maxindex;
6340 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6342 bool curfold_p = false;
6343 if (ce->index)
6344 curindex = ce->index, curfold_p = true;
6345 else
6347 if (fold_p)
6349 /* Since we treat size types now as ordinary
6350 unsigned types, we need an explicit overflow
6351 check. */
6352 tree orig = curindex;
6353 curindex = fold_convert (sizetype, curindex);
6354 overflow_p |= tree_int_cst_lt (curindex, orig);
6356 curindex = size_binop (PLUS_EXPR, curindex,
6357 size_one_node);
6359 if (tree_int_cst_lt (maxindex, curindex))
6360 maxindex = curindex, fold_p = curfold_p;
6362 if (fold_p)
6364 tree orig = maxindex;
6365 maxindex = fold_convert (sizetype, maxindex);
6366 overflow_p |= tree_int_cst_lt (maxindex, orig);
6370 else
6372 /* Make an error message unless that happened already. */
6373 if (initial_value != error_mark_node)
6374 failure = 1;
6377 else
6379 failure = 2;
6380 if (!do_default)
6381 return failure;
6384 type = *ptype;
6385 elt = TREE_TYPE (type);
6386 quals = TYPE_QUALS (strip_array_types (elt));
6387 if (quals == 0)
6388 unqual_elt = elt;
6389 else
6390 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6392 /* Using build_distinct_type_copy and modifying things afterward instead
6393 of using build_array_type to create a new type preserves all of the
6394 TYPE_LANG_FLAG_? bits that the front end may have set. */
6395 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6396 TREE_TYPE (main_type) = unqual_elt;
6397 TYPE_DOMAIN (main_type)
6398 = build_range_type (TREE_TYPE (maxindex),
6399 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6400 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
6401 layout_type (main_type);
6403 /* Make sure we have the canonical MAIN_TYPE. */
6404 hashval_t hashcode = type_hash_canon_hash (main_type);
6405 main_type = type_hash_canon (hashcode, main_type);
6407 /* Fix the canonical type. */
6408 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6409 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6410 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6411 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6412 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6413 != TYPE_DOMAIN (main_type)))
6414 TYPE_CANONICAL (main_type)
6415 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6416 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6417 TYPE_TYPELESS_STORAGE (main_type));
6418 else
6419 TYPE_CANONICAL (main_type) = main_type;
6421 if (quals == 0)
6422 type = main_type;
6423 else
6424 type = c_build_qualified_type (main_type, quals);
6426 if (COMPLETE_TYPE_P (type)
6427 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6428 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6430 error ("size of array is too large");
6431 /* If we proceed with the array type as it is, we'll eventually
6432 crash in tree_to_[su]hwi(). */
6433 type = error_mark_node;
6436 *ptype = type;
6437 return failure;
6440 /* Like c_mark_addressable but don't check register qualifier. */
6441 void
6442 c_common_mark_addressable_vec (tree t)
6444 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6445 t = C_MAYBE_CONST_EXPR_EXPR (t);
6446 while (handled_component_p (t))
6447 t = TREE_OPERAND (t, 0);
6448 if (!VAR_P (t)
6449 && TREE_CODE (t) != PARM_DECL
6450 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6451 return;
6452 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6453 TREE_ADDRESSABLE (t) = 1;
6458 /* Used to help initialize the builtin-types.def table. When a type of
6459 the correct size doesn't exist, use error_mark_node instead of NULL.
6460 The later results in segfaults even when a decl using the type doesn't
6461 get invoked. */
6463 tree
6464 builtin_type_for_size (int size, bool unsignedp)
6466 tree type = c_common_type_for_size (size, unsignedp);
6467 return type ? type : error_mark_node;
6470 /* A helper function for resolve_overloaded_builtin in resolving the
6471 overloaded __sync_ builtins. Returns a positive power of 2 if the
6472 first operand of PARAMS is a pointer to a supported data type.
6473 Returns 0 if an error is encountered.
6474 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6475 built-ins. */
6477 static int
6478 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6480 /* Type of the argument. */
6481 tree argtype;
6482 /* Type the argument points to. */
6483 tree type;
6484 int size;
6486 if (vec_safe_is_empty (params))
6488 error ("too few arguments to function %qE", function);
6489 return 0;
6492 argtype = type = TREE_TYPE ((*params)[0]);
6493 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6495 /* Force array-to-pointer decay for C++. */
6496 (*params)[0] = default_conversion ((*params)[0]);
6497 type = TREE_TYPE ((*params)[0]);
6499 if (TREE_CODE (type) != POINTER_TYPE)
6500 goto incompatible;
6502 type = TREE_TYPE (type);
6503 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6504 goto incompatible;
6506 if (!COMPLETE_TYPE_P (type))
6507 goto incompatible;
6509 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6510 goto incompatible;
6512 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6513 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6514 return size;
6516 incompatible:
6517 /* Issue the diagnostic only if the argument is valid, otherwise
6518 it would be redundant at best and could be misleading. */
6519 if (argtype != error_mark_node)
6520 error ("operand type %qT is incompatible with argument %d of %qE",
6521 argtype, 1, function);
6522 return 0;
6525 /* A helper function for resolve_overloaded_builtin. Adds casts to
6526 PARAMS to make arguments match up with those of FUNCTION. Drops
6527 the variadic arguments at the end. Returns false if some error
6528 was encountered; true on success. */
6530 static bool
6531 sync_resolve_params (location_t loc, tree orig_function, tree function,
6532 vec<tree, va_gc> *params, bool orig_format)
6534 function_args_iterator iter;
6535 tree ptype;
6536 unsigned int parmnum;
6538 function_args_iter_init (&iter, TREE_TYPE (function));
6539 /* We've declared the implementation functions to use "volatile void *"
6540 as the pointer parameter, so we shouldn't get any complaints from the
6541 call to check_function_arguments what ever type the user used. */
6542 function_args_iter_next (&iter);
6543 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6544 ptype = TYPE_MAIN_VARIANT (ptype);
6546 /* For the rest of the values, we need to cast these to FTYPE, so that we
6547 don't get warnings for passing pointer types, etc. */
6548 parmnum = 0;
6549 while (1)
6551 tree val, arg_type;
6553 arg_type = function_args_iter_cond (&iter);
6554 /* XXX void_type_node belies the abstraction. */
6555 if (arg_type == void_type_node)
6556 break;
6558 ++parmnum;
6559 if (params->length () <= parmnum)
6561 error_at (loc, "too few arguments to function %qE", orig_function);
6562 return false;
6565 /* Only convert parameters if arg_type is unsigned integer type with
6566 new format sync routines, i.e. don't attempt to convert pointer
6567 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6568 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6569 kinds). */
6570 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6572 /* Ideally for the first conversion we'd use convert_for_assignment
6573 so that we get warnings for anything that doesn't match the pointer
6574 type. This isn't portable across the C and C++ front ends atm. */
6575 val = (*params)[parmnum];
6576 val = convert (ptype, val);
6577 val = convert (arg_type, val);
6578 (*params)[parmnum] = val;
6581 function_args_iter_next (&iter);
6584 /* __atomic routines are not variadic. */
6585 if (!orig_format && params->length () != parmnum + 1)
6587 error_at (loc, "too many arguments to function %qE", orig_function);
6588 return false;
6591 /* The definition of these primitives is variadic, with the remaining
6592 being "an optional list of variables protected by the memory barrier".
6593 No clue what that's supposed to mean, precisely, but we consider all
6594 call-clobbered variables to be protected so we're safe. */
6595 params->truncate (parmnum + 1);
6597 return true;
6600 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6601 RESULT to make it match the type of the first pointer argument in
6602 PARAMS. */
6604 static tree
6605 sync_resolve_return (tree first_param, tree result, bool orig_format)
6607 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6608 tree rtype = TREE_TYPE (result);
6609 ptype = TYPE_MAIN_VARIANT (ptype);
6611 /* New format doesn't require casting unless the types are the same size. */
6612 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6613 return convert (ptype, result);
6614 else
6615 return result;
6618 /* This function verifies the PARAMS to generic atomic FUNCTION.
6619 It returns the size if all the parameters are the same size, otherwise
6620 0 is returned if the parameters are invalid. */
6622 static int
6623 get_atomic_generic_size (location_t loc, tree function,
6624 vec<tree, va_gc> *params)
6626 unsigned int n_param;
6627 unsigned int n_model;
6628 unsigned int x;
6629 int size_0;
6630 tree type_0;
6632 /* Determine the parameter makeup. */
6633 switch (DECL_FUNCTION_CODE (function))
6635 case BUILT_IN_ATOMIC_EXCHANGE:
6636 n_param = 4;
6637 n_model = 1;
6638 break;
6639 case BUILT_IN_ATOMIC_LOAD:
6640 case BUILT_IN_ATOMIC_STORE:
6641 n_param = 3;
6642 n_model = 1;
6643 break;
6644 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6645 n_param = 6;
6646 n_model = 2;
6647 break;
6648 default:
6649 gcc_unreachable ();
6652 if (vec_safe_length (params) != n_param)
6654 error_at (loc, "incorrect number of arguments to function %qE", function);
6655 return 0;
6658 /* Get type of first parameter, and determine its size. */
6659 type_0 = TREE_TYPE ((*params)[0]);
6660 if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
6662 /* Force array-to-pointer decay for C++. */
6663 (*params)[0] = default_conversion ((*params)[0]);
6664 type_0 = TREE_TYPE ((*params)[0]);
6666 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6668 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6669 function);
6670 return 0;
6673 /* Types must be compile time constant sizes. */
6674 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6676 error_at (loc,
6677 "argument 1 of %qE must be a pointer to a constant size type",
6678 function);
6679 return 0;
6682 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6684 /* Zero size objects are not allowed. */
6685 if (size_0 == 0)
6687 error_at (loc,
6688 "argument 1 of %qE must be a pointer to a nonzero size object",
6689 function);
6690 return 0;
6693 /* Check each other parameter is a pointer and the same size. */
6694 for (x = 0; x < n_param - n_model; x++)
6696 int size;
6697 tree type = TREE_TYPE ((*params)[x]);
6698 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6699 if (n_param == 6 && x == 3)
6700 continue;
6701 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6703 /* Force array-to-pointer decay for C++. */
6704 (*params)[x] = default_conversion ((*params)[x]);
6705 type = TREE_TYPE ((*params)[x]);
6707 if (!POINTER_TYPE_P (type))
6709 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6710 function);
6711 return 0;
6713 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6714 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6715 != INTEGER_CST)
6717 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6718 "size type", x + 1, function);
6719 return 0;
6721 else if (FUNCTION_POINTER_TYPE_P (type))
6723 error_at (loc, "argument %d of %qE must not be a pointer to a "
6724 "function", x + 1, function);
6725 return 0;
6727 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6728 size = type_size ? tree_to_uhwi (type_size) : 0;
6729 if (size != size_0)
6731 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6732 function);
6733 return 0;
6737 /* Check memory model parameters for validity. */
6738 for (x = n_param - n_model ; x < n_param; x++)
6740 tree p = (*params)[x];
6741 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6743 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6744 function);
6745 return 0;
6747 p = fold_for_warn (p);
6748 if (TREE_CODE (p) == INTEGER_CST)
6750 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6751 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6752 bits will be checked later during expansion in target specific
6753 way. */
6754 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6755 warning_at (loc, OPT_Winvalid_memory_model,
6756 "invalid memory model argument %d of %qE", x + 1,
6757 function);
6761 return size_0;
6765 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6766 at the beginning of the parameter list PARAMS representing the size of the
6767 objects. This is to match the library ABI requirement. LOC is the location
6768 of the function call.
6769 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6770 returned to allow the external call to be constructed. */
6772 static tree
6773 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6774 vec<tree, va_gc> *params)
6776 tree size_node;
6778 /* Insert a SIZE_T parameter as the first param. If there isn't
6779 enough space, allocate a new vector and recursively re-build with that. */
6780 if (!params->space (1))
6782 unsigned int z, len;
6783 vec<tree, va_gc> *v;
6784 tree f;
6786 len = params->length ();
6787 vec_alloc (v, len + 1);
6788 v->quick_push (build_int_cst (size_type_node, n));
6789 for (z = 0; z < len; z++)
6790 v->quick_push ((*params)[z]);
6791 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6792 vec_free (v);
6793 return f;
6796 /* Add the size parameter and leave as a function call for processing. */
6797 size_node = build_int_cst (size_type_node, n);
6798 params->quick_insert (0, size_node);
6799 return NULL_TREE;
6803 /* Return whether atomic operations for naturally aligned N-byte
6804 arguments are supported, whether inline or through libatomic. */
6805 static bool
6806 atomic_size_supported_p (int n)
6808 switch (n)
6810 case 1:
6811 case 2:
6812 case 4:
6813 case 8:
6814 return true;
6816 case 16:
6817 return targetm.scalar_mode_supported_p (TImode);
6819 default:
6820 return false;
6824 /* This will process an __atomic_exchange function call, determine whether it
6825 needs to be mapped to the _N variation, or turned into a library call.
6826 LOC is the location of the builtin call.
6827 FUNCTION is the DECL that has been invoked;
6828 PARAMS is the argument list for the call. The return value is non-null
6829 TRUE is returned if it is translated into the proper format for a call to the
6830 external library, and NEW_RETURN is set the tree for that function.
6831 FALSE is returned if processing for the _N variation is required, and
6832 NEW_RETURN is set to the return value the result is copied into. */
6833 static bool
6834 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6835 vec<tree, va_gc> *params, tree *new_return)
6837 tree p0, p1, p2, p3;
6838 tree I_type, I_type_ptr;
6839 int n = get_atomic_generic_size (loc, function, params);
6841 /* Size of 0 is an error condition. */
6842 if (n == 0)
6844 *new_return = error_mark_node;
6845 return true;
6848 /* If not a lock-free size, change to the library generic format. */
6849 if (!atomic_size_supported_p (n))
6851 *new_return = add_atomic_size_parameter (n, loc, function, params);
6852 return true;
6855 /* Otherwise there is a lockfree match, transform the call from:
6856 void fn(T* mem, T* desired, T* return, model)
6857 into
6858 *return = (T) (fn (In* mem, (In) *desired, model)) */
6860 p0 = (*params)[0];
6861 p1 = (*params)[1];
6862 p2 = (*params)[2];
6863 p3 = (*params)[3];
6865 /* Create pointer to appropriate size. */
6866 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6867 I_type_ptr = build_pointer_type (I_type);
6869 /* Convert object pointer to required type. */
6870 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6871 (*params)[0] = p0;
6872 /* Convert new value to required type, and dereference it. */
6873 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6874 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6875 (*params)[1] = p1;
6877 /* Move memory model to the 3rd position, and end param list. */
6878 (*params)[2] = p3;
6879 params->truncate (3);
6881 /* Convert return pointer and dereference it for later assignment. */
6882 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6884 return false;
6888 /* This will process an __atomic_compare_exchange function call, determine
6889 whether it needs to be mapped to the _N variation, or turned into a lib call.
6890 LOC is the location of the builtin call.
6891 FUNCTION is the DECL that has been invoked;
6892 PARAMS is the argument list for the call. The return value is non-null
6893 TRUE is returned if it is translated into the proper format for a call to the
6894 external library, and NEW_RETURN is set the tree for that function.
6895 FALSE is returned if processing for the _N variation is required. */
6897 static bool
6898 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6899 vec<tree, va_gc> *params,
6900 tree *new_return)
6902 tree p0, p1, p2;
6903 tree I_type, I_type_ptr;
6904 int n = get_atomic_generic_size (loc, function, params);
6906 /* Size of 0 is an error condition. */
6907 if (n == 0)
6909 *new_return = error_mark_node;
6910 return true;
6913 /* If not a lock-free size, change to the library generic format. */
6914 if (!atomic_size_supported_p (n))
6916 /* The library generic format does not have the weak parameter, so
6917 remove it from the param list. Since a parameter has been removed,
6918 we can be sure that there is room for the SIZE_T parameter, meaning
6919 there will not be a recursive rebuilding of the parameter list, so
6920 there is no danger this will be done twice. */
6921 if (n > 0)
6923 (*params)[3] = (*params)[4];
6924 (*params)[4] = (*params)[5];
6925 params->truncate (5);
6927 *new_return = add_atomic_size_parameter (n, loc, function, params);
6928 return true;
6931 /* Otherwise, there is a match, so the call needs to be transformed from:
6932 bool fn(T* mem, T* desired, T* return, weak, success, failure)
6933 into
6934 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
6936 p0 = (*params)[0];
6937 p1 = (*params)[1];
6938 p2 = (*params)[2];
6940 /* Create pointer to appropriate size. */
6941 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6942 I_type_ptr = build_pointer_type (I_type);
6944 /* Convert object pointer to required type. */
6945 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6946 (*params)[0] = p0;
6948 /* Convert expected pointer to required type. */
6949 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
6950 (*params)[1] = p1;
6952 /* Convert desired value to required type, and dereference it. */
6953 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6954 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
6955 (*params)[2] = p2;
6957 /* The rest of the parameters are fine. NULL means no special return value
6958 processing.*/
6959 *new_return = NULL;
6960 return false;
6964 /* This will process an __atomic_load function call, determine whether it
6965 needs to be mapped to the _N variation, or turned into a library call.
6966 LOC is the location of the builtin call.
6967 FUNCTION is the DECL that has been invoked;
6968 PARAMS is the argument list for the call. The return value is non-null
6969 TRUE is returned if it is translated into the proper format for a call to the
6970 external library, and NEW_RETURN is set the tree for that function.
6971 FALSE is returned if processing for the _N variation is required, and
6972 NEW_RETURN is set to the return value the result is copied into. */
6974 static bool
6975 resolve_overloaded_atomic_load (location_t loc, tree function,
6976 vec<tree, va_gc> *params, tree *new_return)
6978 tree p0, p1, p2;
6979 tree I_type, I_type_ptr;
6980 int n = get_atomic_generic_size (loc, function, params);
6982 /* Size of 0 is an error condition. */
6983 if (n == 0)
6985 *new_return = error_mark_node;
6986 return true;
6989 /* If not a lock-free size, change to the library generic format. */
6990 if (!atomic_size_supported_p (n))
6992 *new_return = add_atomic_size_parameter (n, loc, function, params);
6993 return true;
6996 /* Otherwise, there is a match, so the call needs to be transformed from:
6997 void fn(T* mem, T* return, model)
6998 into
6999 *return = (T) (fn ((In *) mem, model)) */
7001 p0 = (*params)[0];
7002 p1 = (*params)[1];
7003 p2 = (*params)[2];
7005 /* Create pointer to appropriate size. */
7006 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7007 I_type_ptr = build_pointer_type (I_type);
7009 /* Convert object pointer to required type. */
7010 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7011 (*params)[0] = p0;
7013 /* Move memory model to the 2nd position, and end param list. */
7014 (*params)[1] = p2;
7015 params->truncate (2);
7017 /* Convert return pointer and dereference it for later assignment. */
7018 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7020 return false;
7024 /* This will process an __atomic_store function call, determine whether it
7025 needs to be mapped to the _N variation, or turned into a library call.
7026 LOC is the location of the builtin call.
7027 FUNCTION is the DECL that has been invoked;
7028 PARAMS is the argument list for the call. The return value is non-null
7029 TRUE is returned if it is translated into the proper format for a call to the
7030 external library, and NEW_RETURN is set the tree for that function.
7031 FALSE is returned if processing for the _N variation is required, and
7032 NEW_RETURN is set to the return value the result is copied into. */
7034 static bool
7035 resolve_overloaded_atomic_store (location_t loc, tree function,
7036 vec<tree, va_gc> *params, tree *new_return)
7038 tree p0, p1;
7039 tree I_type, I_type_ptr;
7040 int n = get_atomic_generic_size (loc, function, params);
7042 /* Size of 0 is an error condition. */
7043 if (n == 0)
7045 *new_return = error_mark_node;
7046 return true;
7049 /* If not a lock-free size, change to the library generic format. */
7050 if (!atomic_size_supported_p (n))
7052 *new_return = add_atomic_size_parameter (n, loc, function, params);
7053 return true;
7056 /* Otherwise, there is a match, so the call needs to be transformed from:
7057 void fn(T* mem, T* value, model)
7058 into
7059 fn ((In *) mem, (In) *value, model) */
7061 p0 = (*params)[0];
7062 p1 = (*params)[1];
7064 /* Create pointer to appropriate size. */
7065 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7066 I_type_ptr = build_pointer_type (I_type);
7068 /* Convert object pointer to required type. */
7069 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7070 (*params)[0] = p0;
7072 /* Convert new value to required type, and dereference it. */
7073 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7074 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7075 (*params)[1] = p1;
7077 /* The memory model is in the right spot already. Return is void. */
7078 *new_return = NULL_TREE;
7080 return false;
7084 /* Some builtin functions are placeholders for other expressions. This
7085 function should be called immediately after parsing the call expression
7086 before surrounding code has committed to the type of the expression.
7088 LOC is the location of the builtin call.
7090 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7091 PARAMS is the argument list for the call. The return value is non-null
7092 when expansion is complete, and null if normal processing should
7093 continue. */
7095 tree
7096 resolve_overloaded_builtin (location_t loc, tree function,
7097 vec<tree, va_gc> *params)
7099 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7101 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7102 Those are not valid to call with a pointer to _Bool (or C++ bool)
7103 and so must be rejected. */
7104 bool fetch_op = true;
7105 bool orig_format = true;
7106 tree new_return = NULL_TREE;
7108 switch (DECL_BUILT_IN_CLASS (function))
7110 case BUILT_IN_NORMAL:
7111 break;
7112 case BUILT_IN_MD:
7113 if (targetm.resolve_overloaded_builtin)
7114 return targetm.resolve_overloaded_builtin (loc, function, params);
7115 else
7116 return NULL_TREE;
7117 default:
7118 return NULL_TREE;
7121 /* Handle BUILT_IN_NORMAL here. */
7122 switch (orig_code)
7124 case BUILT_IN_ATOMIC_EXCHANGE:
7125 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7126 case BUILT_IN_ATOMIC_LOAD:
7127 case BUILT_IN_ATOMIC_STORE:
7129 /* Handle these 4 together so that they can fall through to the next
7130 case if the call is transformed to an _N variant. */
7131 switch (orig_code)
7133 case BUILT_IN_ATOMIC_EXCHANGE:
7135 if (resolve_overloaded_atomic_exchange (loc, function, params,
7136 &new_return))
7137 return new_return;
7138 /* Change to the _N variant. */
7139 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7140 break;
7143 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7145 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7146 params,
7147 &new_return))
7148 return new_return;
7149 /* Change to the _N variant. */
7150 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7151 break;
7153 case BUILT_IN_ATOMIC_LOAD:
7155 if (resolve_overloaded_atomic_load (loc, function, params,
7156 &new_return))
7157 return new_return;
7158 /* Change to the _N variant. */
7159 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7160 break;
7162 case BUILT_IN_ATOMIC_STORE:
7164 if (resolve_overloaded_atomic_store (loc, function, params,
7165 &new_return))
7166 return new_return;
7167 /* Change to the _N variant. */
7168 orig_code = BUILT_IN_ATOMIC_STORE_N;
7169 break;
7171 default:
7172 gcc_unreachable ();
7175 /* FALLTHRU */
7176 case BUILT_IN_ATOMIC_EXCHANGE_N:
7177 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7178 case BUILT_IN_ATOMIC_LOAD_N:
7179 case BUILT_IN_ATOMIC_STORE_N:
7180 fetch_op = false;
7181 /* FALLTHRU */
7182 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7183 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7184 case BUILT_IN_ATOMIC_AND_FETCH_N:
7185 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7186 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7187 case BUILT_IN_ATOMIC_OR_FETCH_N:
7188 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7189 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7190 case BUILT_IN_ATOMIC_FETCH_AND_N:
7191 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7192 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7193 case BUILT_IN_ATOMIC_FETCH_OR_N:
7194 orig_format = false;
7195 /* FALLTHRU */
7196 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7197 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7198 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7199 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7200 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7201 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7202 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7203 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7204 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7205 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7206 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7207 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7208 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7209 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7210 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7211 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7213 /* The following are not _FETCH_OPs and must be accepted with
7214 pointers to _Bool (or C++ bool). */
7215 if (fetch_op)
7216 fetch_op =
7217 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7218 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7219 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7220 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7222 int n = sync_resolve_size (function, params, fetch_op);
7223 tree new_function, first_param, result;
7224 enum built_in_function fncode;
7226 if (n == 0)
7227 return error_mark_node;
7229 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7230 new_function = builtin_decl_explicit (fncode);
7231 if (!sync_resolve_params (loc, function, new_function, params,
7232 orig_format))
7233 return error_mark_node;
7235 first_param = (*params)[0];
7236 result = build_function_call_vec (loc, vNULL, new_function, params,
7237 NULL);
7238 if (result == error_mark_node)
7239 return result;
7240 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7241 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7242 && orig_code != BUILT_IN_ATOMIC_STORE_N
7243 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7244 result = sync_resolve_return (first_param, result, orig_format);
7246 if (fetch_op)
7247 /* Prevent -Wunused-value warning. */
7248 TREE_USED (result) = true;
7250 /* If new_return is set, assign function to that expr and cast the
7251 result to void since the generic interface returned void. */
7252 if (new_return)
7254 /* Cast function result from I{1,2,4,8,16} to the required type. */
7255 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7256 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7257 result);
7258 TREE_SIDE_EFFECTS (result) = 1;
7259 protected_set_expr_location (result, loc);
7260 result = convert (void_type_node, result);
7262 return result;
7265 default:
7266 return NULL_TREE;
7270 /* vector_types_compatible_elements_p is used in type checks of vectors
7271 values used as operands of binary operators. Where it returns true, and
7272 the other checks of the caller succeed (being vector types in he first
7273 place, and matching number of elements), we can just treat the types
7274 as essentially the same.
7275 Contrast with vector_targets_convertible_p, which is used for vector
7276 pointer types, and vector_types_convertible_p, which will allow
7277 language-specific matches under the control of flag_lax_vector_conversions,
7278 and might still require a conversion. */
7279 /* True if vector types T1 and T2 can be inputs to the same binary
7280 operator without conversion.
7281 We don't check the overall vector size here because some of our callers
7282 want to give different error messages when the vectors are compatible
7283 except for the element count. */
7285 bool
7286 vector_types_compatible_elements_p (tree t1, tree t2)
7288 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7289 t1 = TREE_TYPE (t1);
7290 t2 = TREE_TYPE (t2);
7292 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7294 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7295 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7296 || c2 == FIXED_POINT_TYPE));
7298 t1 = c_common_signed_type (t1);
7299 t2 = c_common_signed_type (t2);
7300 /* Equality works here because c_common_signed_type uses
7301 TYPE_MAIN_VARIANT. */
7302 if (t1 == t2)
7303 return true;
7304 if (opaque && c1 == c2
7305 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7306 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7307 return true;
7308 return false;
7311 /* Check for missing format attributes on function pointers. LTYPE is
7312 the new type or left-hand side type. RTYPE is the old type or
7313 right-hand side type. Returns TRUE if LTYPE is missing the desired
7314 attribute. */
7316 bool
7317 check_missing_format_attribute (tree ltype, tree rtype)
7319 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7320 tree ra;
7322 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7323 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7324 break;
7325 if (ra)
7327 tree la;
7328 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7329 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7330 break;
7331 return !la;
7333 else
7334 return false;
7337 /* Setup a TYPE_DECL node as a typedef representation.
7339 X is a TYPE_DECL for a typedef statement. Create a brand new
7340 ..._TYPE node (which will be just a variant of the existing
7341 ..._TYPE node with identical properties) and then install X
7342 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7344 The whole point here is to end up with a situation where each
7345 and every ..._TYPE node the compiler creates will be uniquely
7346 associated with AT MOST one node representing a typedef name.
7347 This way, even though the compiler substitutes corresponding
7348 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7349 early on, later parts of the compiler can always do the reverse
7350 translation and get back the corresponding typedef name. For
7351 example, given:
7353 typedef struct S MY_TYPE;
7354 MY_TYPE object;
7356 Later parts of the compiler might only know that `object' was of
7357 type `struct S' if it were not for code just below. With this
7358 code however, later parts of the compiler see something like:
7360 struct S' == struct S
7361 typedef struct S' MY_TYPE;
7362 struct S' object;
7364 And they can then deduce (from the node for type struct S') that
7365 the original object declaration was:
7367 MY_TYPE object;
7369 Being able to do this is important for proper support of protoize,
7370 and also for generating precise symbolic debugging information
7371 which takes full account of the programmer's (typedef) vocabulary.
7373 Obviously, we don't want to generate a duplicate ..._TYPE node if
7374 the TYPE_DECL node that we are now processing really represents a
7375 standard built-in type. */
7377 void
7378 set_underlying_type (tree x)
7380 if (x == error_mark_node)
7381 return;
7382 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7384 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7385 TYPE_NAME (TREE_TYPE (x)) = x;
7387 else if (TREE_TYPE (x) != error_mark_node
7388 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7390 tree tt = TREE_TYPE (x);
7391 DECL_ORIGINAL_TYPE (x) = tt;
7392 tt = build_variant_type_copy (tt);
7393 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7394 TYPE_NAME (tt) = x;
7396 /* Mark the type as used only when its type decl is decorated
7397 with attribute unused. */
7398 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7399 TREE_USED (tt) = 1;
7401 TREE_TYPE (x) = tt;
7405 /* Record the types used by the current global variable declaration
7406 being parsed, so that we can decide later to emit their debug info.
7407 Those types are in types_used_by_cur_var_decl, and we are going to
7408 store them in the types_used_by_vars_hash hash table.
7409 DECL is the declaration of the global variable that has been parsed. */
7411 void
7412 record_types_used_by_current_var_decl (tree decl)
7414 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7416 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7418 tree type = types_used_by_cur_var_decl->pop ();
7419 types_used_by_var_decl_insert (type, decl);
7423 /* The C and C++ parsers both use vectors to hold function arguments.
7424 For efficiency, we keep a cache of unused vectors. This is the
7425 cache. */
7427 typedef vec<tree, va_gc> *tree_gc_vec;
7428 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7430 /* Return a new vector from the cache. If the cache is empty,
7431 allocate a new vector. These vectors are GC'ed, so it is OK if the
7432 pointer is not released.. */
7434 vec<tree, va_gc> *
7435 make_tree_vector (void)
7437 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7438 return tree_vector_cache->pop ();
7439 else
7441 /* Passing 0 to vec::alloc returns NULL, and our callers require
7442 that we always return a non-NULL value. The vector code uses
7443 4 when growing a NULL vector, so we do too. */
7444 vec<tree, va_gc> *v;
7445 vec_alloc (v, 4);
7446 return v;
7450 /* Release a vector of trees back to the cache. */
7452 void
7453 release_tree_vector (vec<tree, va_gc> *vec)
7455 if (vec != NULL)
7457 vec->truncate (0);
7458 vec_safe_push (tree_vector_cache, vec);
7462 /* Get a new tree vector holding a single tree. */
7464 vec<tree, va_gc> *
7465 make_tree_vector_single (tree t)
7467 vec<tree, va_gc> *ret = make_tree_vector ();
7468 ret->quick_push (t);
7469 return ret;
7472 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7474 vec<tree, va_gc> *
7475 make_tree_vector_from_list (tree list)
7477 vec<tree, va_gc> *ret = make_tree_vector ();
7478 for (; list; list = TREE_CHAIN (list))
7479 vec_safe_push (ret, TREE_VALUE (list));
7480 return ret;
7483 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7485 vec<tree, va_gc> *
7486 make_tree_vector_from_ctor (tree ctor)
7488 vec<tree,va_gc> *ret = make_tree_vector ();
7489 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7490 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7491 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7492 return ret;
7495 /* Get a new tree vector which is a copy of an existing one. */
7497 vec<tree, va_gc> *
7498 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7500 vec<tree, va_gc> *ret;
7501 unsigned int ix;
7502 tree t;
7504 ret = make_tree_vector ();
7505 vec_safe_reserve (ret, vec_safe_length (orig));
7506 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7507 ret->quick_push (t);
7508 return ret;
7511 /* Return true if KEYWORD starts a type specifier. */
7513 bool
7514 keyword_begins_type_specifier (enum rid keyword)
7516 switch (keyword)
7518 case RID_AUTO_TYPE:
7519 case RID_INT:
7520 case RID_CHAR:
7521 case RID_FLOAT:
7522 case RID_DOUBLE:
7523 case RID_VOID:
7524 case RID_UNSIGNED:
7525 case RID_LONG:
7526 case RID_SHORT:
7527 case RID_SIGNED:
7528 CASE_RID_FLOATN_NX:
7529 case RID_DFLOAT32:
7530 case RID_DFLOAT64:
7531 case RID_DFLOAT128:
7532 case RID_FRACT:
7533 case RID_ACCUM:
7534 case RID_BOOL:
7535 case RID_WCHAR:
7536 case RID_CHAR16:
7537 case RID_CHAR32:
7538 case RID_SAT:
7539 case RID_COMPLEX:
7540 case RID_TYPEOF:
7541 case RID_STRUCT:
7542 case RID_CLASS:
7543 case RID_UNION:
7544 case RID_ENUM:
7545 return true;
7546 default:
7547 if (keyword >= RID_FIRST_INT_N
7548 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7549 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7550 return true;
7551 return false;
7555 /* Return true if KEYWORD names a type qualifier. */
7557 bool
7558 keyword_is_type_qualifier (enum rid keyword)
7560 switch (keyword)
7562 case RID_CONST:
7563 case RID_VOLATILE:
7564 case RID_RESTRICT:
7565 case RID_ATOMIC:
7566 return true;
7567 default:
7568 return false;
7572 /* Return true if KEYWORD names a storage class specifier.
7574 RID_TYPEDEF is not included in this list despite `typedef' being
7575 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7576 such for syntactic convenience only. */
7578 bool
7579 keyword_is_storage_class_specifier (enum rid keyword)
7581 switch (keyword)
7583 case RID_STATIC:
7584 case RID_EXTERN:
7585 case RID_REGISTER:
7586 case RID_AUTO:
7587 case RID_MUTABLE:
7588 case RID_THREAD:
7589 return true;
7590 default:
7591 return false;
7595 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7597 static bool
7598 keyword_is_function_specifier (enum rid keyword)
7600 switch (keyword)
7602 case RID_INLINE:
7603 case RID_NORETURN:
7604 case RID_VIRTUAL:
7605 case RID_EXPLICIT:
7606 return true;
7607 default:
7608 return false;
7612 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7613 declaration-specifier (C99 6.7). */
7615 bool
7616 keyword_is_decl_specifier (enum rid keyword)
7618 if (keyword_is_storage_class_specifier (keyword)
7619 || keyword_is_type_qualifier (keyword)
7620 || keyword_is_function_specifier (keyword))
7621 return true;
7623 switch (keyword)
7625 case RID_TYPEDEF:
7626 case RID_FRIEND:
7627 case RID_CONSTEXPR:
7628 return true;
7629 default:
7630 return false;
7634 /* Initialize language-specific-bits of tree_contains_struct. */
7636 void
7637 c_common_init_ts (void)
7639 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7640 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7643 /* Build a user-defined numeric literal out of an integer constant type VALUE
7644 with identifier SUFFIX. */
7646 tree
7647 build_userdef_literal (tree suffix_id, tree value,
7648 enum overflow_type overflow, tree num_string)
7650 tree literal = make_node (USERDEF_LITERAL);
7651 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7652 USERDEF_LITERAL_VALUE (literal) = value;
7653 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7654 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7655 return literal;
7658 /* For vector[index], convert the vector to an array of the underlying type.
7659 Return true if the resulting ARRAY_REF should not be an lvalue. */
7661 bool
7662 convert_vector_to_array_for_subscript (location_t loc,
7663 tree *vecp, tree index)
7665 bool ret = false;
7666 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7668 tree type = TREE_TYPE (*vecp);
7670 ret = !lvalue_p (*vecp);
7672 if (TREE_CODE (index) == INTEGER_CST)
7673 if (!tree_fits_uhwi_p (index)
7674 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7675 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7677 /* We are building an ARRAY_REF so mark the vector as addressable
7678 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7679 for function parameters. */
7680 c_common_mark_addressable_vec (*vecp);
7682 *vecp = build1 (VIEW_CONVERT_EXPR,
7683 build_array_type_nelts (TREE_TYPE (type),
7684 TYPE_VECTOR_SUBPARTS (type)),
7685 *vecp);
7687 return ret;
7690 /* Determine which of the operands, if any, is a scalar that needs to be
7691 converted to a vector, for the range of operations. */
7692 enum stv_conv
7693 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7694 bool complain)
7696 tree type0 = TREE_TYPE (op0);
7697 tree type1 = TREE_TYPE (op1);
7698 bool integer_only_op = false;
7699 enum stv_conv ret = stv_firstarg;
7701 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7702 switch (code)
7704 /* Most GENERIC binary expressions require homogeneous arguments.
7705 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7706 argument that is a vector and a second one that is a scalar, so
7707 we never return stv_secondarg for them. */
7708 case RSHIFT_EXPR:
7709 case LSHIFT_EXPR:
7710 if (TREE_CODE (type0) == INTEGER_TYPE
7711 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7713 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7714 NULL_TREE, false))
7716 if (complain)
7717 error_at (loc, "conversion of scalar %qT to vector %qT "
7718 "involves truncation", type0, type1);
7719 return stv_error;
7721 else
7722 return stv_firstarg;
7724 break;
7726 case BIT_IOR_EXPR:
7727 case BIT_XOR_EXPR:
7728 case BIT_AND_EXPR:
7729 integer_only_op = true;
7730 /* fall through */
7732 case VEC_COND_EXPR:
7734 case PLUS_EXPR:
7735 case MINUS_EXPR:
7736 case MULT_EXPR:
7737 case TRUNC_DIV_EXPR:
7738 case CEIL_DIV_EXPR:
7739 case FLOOR_DIV_EXPR:
7740 case ROUND_DIV_EXPR:
7741 case EXACT_DIV_EXPR:
7742 case TRUNC_MOD_EXPR:
7743 case FLOOR_MOD_EXPR:
7744 case RDIV_EXPR:
7745 case EQ_EXPR:
7746 case NE_EXPR:
7747 case LE_EXPR:
7748 case GE_EXPR:
7749 case LT_EXPR:
7750 case GT_EXPR:
7751 /* What about UNLT_EXPR? */
7752 if (VECTOR_TYPE_P (type0))
7754 ret = stv_secondarg;
7755 std::swap (type0, type1);
7756 std::swap (op0, op1);
7759 if (TREE_CODE (type0) == INTEGER_TYPE
7760 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7762 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7763 NULL_TREE, false))
7765 if (complain)
7766 error_at (loc, "conversion of scalar %qT to vector %qT "
7767 "involves truncation", type0, type1);
7768 return stv_error;
7770 return ret;
7772 else if (!integer_only_op
7773 /* Allow integer --> real conversion if safe. */
7774 && (TREE_CODE (type0) == REAL_TYPE
7775 || TREE_CODE (type0) == INTEGER_TYPE)
7776 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7778 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7779 NULL_TREE, false))
7781 if (complain)
7782 error_at (loc, "conversion of scalar %qT to vector %qT "
7783 "involves truncation", type0, type1);
7784 return stv_error;
7786 return ret;
7788 default:
7789 break;
7792 return stv_nothing;
7795 /* Return the alignment of std::max_align_t.
7797 [support.types.layout] The type max_align_t is a POD type whose alignment
7798 requirement is at least as great as that of every scalar type, and whose
7799 alignment requirement is supported in every context. */
7801 unsigned
7802 max_align_t_align ()
7804 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7805 TYPE_ALIGN (long_double_type_node));
7806 if (float128_type_node != NULL_TREE)
7807 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7808 return max_align;
7811 /* Return true iff ALIGN is an integral constant that is a fundamental
7812 alignment, as defined by [basic.align] in the c++-11
7813 specifications.
7815 That is:
7817 [A fundamental alignment is represented by an alignment less than or
7818 equal to the greatest alignment supported by the implementation
7819 in all contexts, which is equal to alignof(max_align_t)]. */
7821 bool
7822 cxx_fundamental_alignment_p (unsigned align)
7824 return (align <= max_align_t_align ());
7827 /* Return true if T is a pointer to a zero-sized aggregate. */
7829 bool
7830 pointer_to_zero_sized_aggr_p (tree t)
7832 if (!POINTER_TYPE_P (t))
7833 return false;
7834 t = TREE_TYPE (t);
7835 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7838 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7839 with no library fallback or for an ADDR_EXPR whose operand is such type
7840 issues an error pointing to the location LOC.
7841 Returns true when the expression has been diagnosed and false
7842 otherwise. */
7844 bool
7845 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7847 if (TREE_CODE (expr) == ADDR_EXPR)
7848 expr = TREE_OPERAND (expr, 0);
7850 STRIP_ANY_LOCATION_WRAPPER (expr);
7852 if (TREE_TYPE (expr)
7853 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7854 && TREE_CODE (expr) == FUNCTION_DECL
7855 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7856 false positives for user-declared built-ins such as abs or
7857 strlen, and for C++ operators new and delete.
7858 The c_decl_implicit() test avoids false positives for implicitly
7859 declared built-ins with library fallbacks (such as abs). */
7860 && DECL_BUILT_IN (expr)
7861 && DECL_IS_BUILTIN (expr)
7862 && !c_decl_implicit (expr)
7863 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7865 if (loc == UNKNOWN_LOCATION)
7866 loc = EXPR_LOC_OR_LOC (expr, input_location);
7868 /* Reject arguments that are built-in functions with
7869 no library fallback. */
7870 error_at (loc, "built-in function %qE must be directly called", expr);
7872 return true;
7875 return false;
7878 /* Check if array size calculations overflow or if the array covers more
7879 than half of the address space. Return true if the size of the array
7880 is valid, false otherwise. TYPE is the type of the array and NAME is
7881 the name of the array, or NULL_TREE for unnamed arrays. */
7883 bool
7884 valid_array_size_p (location_t loc, tree type, tree name)
7886 if (type != error_mark_node
7887 && COMPLETE_TYPE_P (type)
7888 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7889 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7891 if (name)
7892 error_at (loc, "size of array %qE is too large", name);
7893 else
7894 error_at (loc, "size of unnamed array is too large");
7895 return false;
7897 return true;
7900 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7901 timestamp to replace embedded current dates to get reproducible
7902 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
7904 time_t
7905 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7907 char *source_date_epoch;
7908 int64_t epoch;
7909 char *endptr;
7911 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7912 if (!source_date_epoch)
7913 return (time_t) -1;
7915 errno = 0;
7916 #if defined(INT64_T_IS_LONG)
7917 epoch = strtol (source_date_epoch, &endptr, 10);
7918 #else
7919 epoch = strtoll (source_date_epoch, &endptr, 10);
7920 #endif
7921 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7922 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7924 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
7925 "expand to a non-negative integer less than or equal to %wd",
7926 MAX_SOURCE_DATE_EPOCH);
7927 return (time_t) -1;
7930 return (time_t) epoch;
7933 /* Callback for libcpp for offering spelling suggestions for misspelled
7934 directives. GOAL is an unrecognized string; CANDIDATES is a
7935 NULL-terminated array of candidate strings. Return the closest
7936 match to GOAL within CANDIDATES, or NULL if none are good
7937 suggestions. */
7939 const char *
7940 cb_get_suggestion (cpp_reader *, const char *goal,
7941 const char *const *candidates)
7943 best_match<const char *, const char *> bm (goal);
7944 while (*candidates)
7945 bm.consider (*candidates++);
7946 return bm.get_best_meaningful_candidate ();
7949 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
7950 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
7951 by C TS 18661-3 for interchange types that are computed in their
7952 native precision are larger than the C11 values for evaluating in the
7953 precision of float/double/long double. If either mode is
7954 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
7956 enum flt_eval_method
7957 excess_precision_mode_join (enum flt_eval_method x,
7958 enum flt_eval_method y)
7960 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
7961 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
7962 return FLT_EVAL_METHOD_UNPREDICTABLE;
7964 /* GCC only supports one interchange type right now, _Float16. If
7965 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
7966 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
7967 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7968 return y;
7969 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7970 return x;
7972 /* Other values for flt_eval_method are directly comparable, and we want
7973 the maximum. */
7974 return MAX (x, y);
7977 /* Return the value that should be set for FLT_EVAL_METHOD in the
7978 context of ISO/IEC TS 18861-3.
7980 This relates to the effective excess precision seen by the user,
7981 which is the join point of the precision the target requests for
7982 -fexcess-precision={standard,fast} and the implicit excess precision
7983 the target uses. */
7985 static enum flt_eval_method
7986 c_ts18661_flt_eval_method (void)
7988 enum flt_eval_method implicit
7989 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
7991 enum excess_precision_type flag_type
7992 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
7993 ? EXCESS_PRECISION_TYPE_STANDARD
7994 : EXCESS_PRECISION_TYPE_FAST);
7996 enum flt_eval_method requested
7997 = targetm.c.excess_precision (flag_type);
7999 return excess_precision_mode_join (implicit, requested);
8002 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8003 those that were permitted by C11. That is to say, eliminates
8004 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8006 static enum flt_eval_method
8007 c_c11_flt_eval_method (void)
8009 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8010 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8013 /* Return the value that should be set for FLT_EVAL_METHOD.
8014 MAYBE_C11_ONLY_P is TRUE if we should check
8015 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8016 values we can return to those from C99/C11, and FALSE otherwise.
8017 See the comments on c_ts18661_flt_eval_method for what value we choose
8018 to set here. */
8021 c_flt_eval_method (bool maybe_c11_only_p)
8023 if (maybe_c11_only_p
8024 && flag_permitted_flt_eval_methods
8025 == PERMITTED_FLT_EVAL_METHODS_C11)
8026 return c_c11_flt_eval_method ();
8027 else
8028 return c_ts18661_flt_eval_method ();
8031 /* An enum for get_missing_token_insertion_kind for describing the best
8032 place to insert a missing token, if there is one. */
8034 enum missing_token_insertion_kind
8036 MTIK_IMPOSSIBLE,
8037 MTIK_INSERT_BEFORE_NEXT,
8038 MTIK_INSERT_AFTER_PREV
8041 /* Given a missing token of TYPE, determine if it is reasonable to
8042 emit a fix-it hint suggesting the insertion of the token, and,
8043 if so, where the token should be inserted relative to other tokens.
8045 It only makes sense to do this for values of TYPE that are symbols.
8047 Some symbols should go before the next token, e.g. in:
8048 if flag)
8049 we want to insert the missing '(' immediately before "flag",
8050 giving:
8051 if (flag)
8052 rather than:
8053 if( flag)
8054 These use MTIK_INSERT_BEFORE_NEXT.
8056 Other symbols should go after the previous token, e.g. in:
8057 if (flag
8058 do_something ();
8059 we want to insert the missing ')' immediately after the "flag",
8060 giving:
8061 if (flag)
8062 do_something ();
8063 rather than:
8064 if (flag
8065 )do_something ();
8066 These use MTIK_INSERT_AFTER_PREV. */
8068 static enum missing_token_insertion_kind
8069 get_missing_token_insertion_kind (enum cpp_ttype type)
8071 switch (type)
8073 /* Insert missing "opening" brackets immediately
8074 before the next token. */
8075 case CPP_OPEN_SQUARE:
8076 case CPP_OPEN_PAREN:
8077 return MTIK_INSERT_BEFORE_NEXT;
8079 /* Insert other missing symbols immediately after
8080 the previous token. */
8081 case CPP_CLOSE_PAREN:
8082 case CPP_CLOSE_SQUARE:
8083 case CPP_SEMICOLON:
8084 case CPP_COMMA:
8085 case CPP_COLON:
8086 return MTIK_INSERT_AFTER_PREV;
8088 /* Other kinds of token don't get fix-it hints. */
8089 default:
8090 return MTIK_IMPOSSIBLE;
8094 /* Given RICHLOC, a location for a diagnostic describing a missing token
8095 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8096 insertion of the token.
8098 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8099 it will either be:
8100 (a) immediately after PREV_TOKEN_LOC, or
8102 (b) immediately before the primary location within RICHLOC (taken to
8103 be that of the token following where the token was expected).
8105 If we manage to add a fix-it hint, then the location of the
8106 fix-it hint is likely to be more useful as the primary location
8107 of the diagnostic than that of the following token, so we swap
8108 these locations.
8110 For example, given this bogus code:
8111 123456789012345678901234567890
8112 1 | int missing_semicolon (void)
8113 2 | {
8114 3 | return 42
8115 4 | }
8117 we will emit:
8119 "expected ';' before '}'"
8121 RICHLOC's primary location is at the closing brace, so before "swapping"
8122 we would emit the error at line 4 column 1:
8124 123456789012345678901234567890
8125 3 | return 42 |< fix-it hint emitted for this line
8126 | ; |
8127 4 | } |< "expected ';' before '}'" emitted at this line
8128 | ^ |
8130 It's more useful for the location of the diagnostic to be at the
8131 fix-it hint, so we swap the locations, so the primary location
8132 is at the fix-it hint, with the old primary location inserted
8133 as a secondary location, giving this, with the error at line 3
8134 column 12:
8136 123456789012345678901234567890
8137 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8138 | ^ | with fix-it hint
8139 4 | ; |
8140 | } |< secondary range emitted here
8141 | ~ |. */
8143 void
8144 maybe_suggest_missing_token_insertion (rich_location *richloc,
8145 enum cpp_ttype token_type,
8146 location_t prev_token_loc)
8148 gcc_assert (richloc);
8150 enum missing_token_insertion_kind mtik
8151 = get_missing_token_insertion_kind (token_type);
8153 switch (mtik)
8155 default:
8156 gcc_unreachable ();
8157 break;
8159 case MTIK_IMPOSSIBLE:
8160 return;
8162 case MTIK_INSERT_BEFORE_NEXT:
8163 /* Attempt to add the fix-it hint before the primary location
8164 of RICHLOC. */
8165 richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
8166 break;
8168 case MTIK_INSERT_AFTER_PREV:
8169 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8170 richloc->add_fixit_insert_after (prev_token_loc,
8171 cpp_type2name (token_type, 0));
8172 break;
8175 /* If we were successful, use the fix-it hint's location as the
8176 primary location within RICHLOC, adding the old primary location
8177 back as a secondary location. */
8178 if (!richloc->seen_impossible_fixit_p ())
8180 fixit_hint *hint = richloc->get_last_fixit_hint ();
8181 location_t hint_loc = hint->get_start_loc ();
8182 location_t old_loc = richloc->get_loc ();
8184 richloc->set_range (line_table, 0, hint_loc, true);
8185 richloc->add_range (old_loc, false);
8189 #if CHECKING_P
8191 namespace selftest {
8193 /* Verify that fold_for_warn on error_mark_node is safe. */
8195 static void
8196 test_fold_for_warn ()
8198 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8201 /* Run all of the selftests within this file. */
8203 static void
8204 c_common_c_tests ()
8206 test_fold_for_warn ();
8209 /* Run all of the tests within c-family. */
8211 void
8212 c_family_tests (void)
8214 c_common_c_tests ();
8215 c_format_c_tests ();
8216 c_pretty_print_c_tests ();
8217 c_spellcheck_cc_tests ();
8220 } // namespace selftest
8222 #endif /* #if CHECKING_P */
8224 /* Attempt to locate a suitable location within FILE for a
8225 #include directive to be inserted before. FILE should
8226 be a string from libcpp (pointer equality is used).
8227 LOC is the location of the relevant diagnostic.
8229 Attempt to return the location within FILE immediately
8230 after the last #include within that file, or the start of
8231 that file if it has no #include directives.
8233 Return UNKNOWN_LOCATION if no suitable location is found,
8234 or if an error occurs. */
8236 static location_t
8237 try_to_locate_new_include_insertion_point (const char *file, location_t loc)
8239 /* Locate the last ordinary map within FILE that ended with a #include. */
8240 const line_map_ordinary *last_include_ord_map = NULL;
8242 /* ...and the next ordinary map within FILE after that one. */
8243 const line_map_ordinary *last_ord_map_after_include = NULL;
8245 /* ...and the first ordinary map within FILE. */
8246 const line_map_ordinary *first_ord_map_in_file = NULL;
8248 /* Get ordinary map containing LOC (or its expansion). */
8249 const line_map_ordinary *ord_map_for_loc = NULL;
8250 loc = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
8251 &ord_map_for_loc);
8252 gcc_assert (ord_map_for_loc);
8254 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8256 const line_map_ordinary *ord_map
8257 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8259 const line_map_ordinary *from = INCLUDED_FROM (line_table, ord_map);
8260 if (from)
8261 if (from->to_file == file)
8263 last_include_ord_map = from;
8264 last_ord_map_after_include = NULL;
8267 if (ord_map->to_file == file)
8269 if (!first_ord_map_in_file)
8270 first_ord_map_in_file = ord_map;
8271 if (last_include_ord_map && !last_ord_map_after_include)
8272 last_ord_map_after_include = ord_map;
8275 /* Stop searching when reaching the ord_map containing LOC,
8276 as it makes no sense to provide fix-it hints that appear
8277 after the diagnostic in question. */
8278 if (ord_map == ord_map_for_loc)
8279 break;
8282 /* Determine where to insert the #include. */
8283 const line_map_ordinary *ord_map_for_insertion;
8285 /* We want the next ordmap in the file after the last one that's a
8286 #include, but failing that, the start of the file. */
8287 if (last_ord_map_after_include)
8288 ord_map_for_insertion = last_ord_map_after_include;
8289 else
8290 ord_map_for_insertion = first_ord_map_in_file;
8292 if (!ord_map_for_insertion)
8293 return UNKNOWN_LOCATION;
8295 /* The "start_location" is column 0, meaning "the whole line".
8296 rich_location and edit_context can't cope with this, so use
8297 column 1 instead. */
8298 location_t col_0 = ord_map_for_insertion->start_location;
8299 return linemap_position_for_loc_and_offset (line_table, col_0, 1);
8302 /* A map from filenames to sets of headers added to them, for
8303 ensuring idempotency within maybe_add_include_fixit. */
8305 /* The values within the map. We need string comparison as there's
8306 no guarantee that two different diagnostics that are recommending
8307 adding e.g. "<stdio.h>" are using the same buffer. */
8309 typedef hash_set <const char *, nofree_string_hash> per_file_includes_t;
8311 /* The map itself. We don't need string comparison for the filename keys,
8312 as they come from libcpp. */
8314 typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
8315 static added_includes_t *added_includes;
8317 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8318 in a suitable location within the file of RICHLOC's primary
8319 location.
8321 This function is idempotent: a header will be added at most once to
8322 any given file. */
8324 void
8325 maybe_add_include_fixit (rich_location *richloc, const char *header)
8327 location_t loc = richloc->get_loc ();
8328 const char *file = LOCATION_FILE (loc);
8329 if (!file)
8330 return;
8332 /* Idempotency: don't add the same header more than once to a given file. */
8333 if (!added_includes)
8334 added_includes = new added_includes_t ();
8335 per_file_includes_t *&set = added_includes->get_or_insert (file);
8336 if (set)
8337 if (set->contains (header))
8338 /* ...then we've already added HEADER to that file. */
8339 return;
8340 if (!set)
8341 set = new per_file_includes_t ();
8342 set->add (header);
8344 /* Attempt to locate a suitable place for the new directive. */
8345 location_t include_insert_loc
8346 = try_to_locate_new_include_insertion_point (file, loc);
8347 if (include_insert_loc == UNKNOWN_LOCATION)
8348 return;
8350 char *text = xasprintf ("#include %s\n", header);
8351 richloc->add_fixit_insert_before (include_insert_loc, text);
8352 free (text);
8355 #include "gt-c-family-c-common.h"