Eliminate source_location in favor of location_t
[official-gcc.git] / gcc / c-family / c-common.c
blob9c2f18609acb48b4cff92c5c4a655f7cb64aba5f
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "c-common.h"
30 #include "gimple-expr.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "trans-mem.h"
41 #include "c-objc.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "toplev.h"
46 #include "tree-iterator.h"
47 #include "opts.h"
48 #include "gimplify.h"
49 #include "substring-locations.h"
50 #include "spellcheck.h"
51 #include "selftest.h"
53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
55 /* Mode used to build pointers (VOIDmode means ptr_mode). */
57 machine_mode c_default_pointer_mode = VOIDmode;
59 /* The following symbols are subsumed in the c_global_trees array, and
60 listed here individually for documentation purposes.
62 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
64 tree short_integer_type_node;
65 tree long_integer_type_node;
66 tree long_long_integer_type_node;
68 tree short_unsigned_type_node;
69 tree long_unsigned_type_node;
70 tree long_long_unsigned_type_node;
72 tree truthvalue_type_node;
73 tree truthvalue_false_node;
74 tree truthvalue_true_node;
76 tree ptrdiff_type_node;
78 tree unsigned_char_type_node;
79 tree signed_char_type_node;
80 tree wchar_type_node;
82 tree char16_type_node;
83 tree char32_type_node;
85 tree float_type_node;
86 tree double_type_node;
87 tree long_double_type_node;
89 tree complex_integer_type_node;
90 tree complex_float_type_node;
91 tree complex_double_type_node;
92 tree complex_long_double_type_node;
94 tree dfloat32_type_node;
95 tree dfloat64_type_node;
96 tree_dfloat128_type_node;
98 tree intQI_type_node;
99 tree intHI_type_node;
100 tree intSI_type_node;
101 tree intDI_type_node;
102 tree intTI_type_node;
104 tree unsigned_intQI_type_node;
105 tree unsigned_intHI_type_node;
106 tree unsigned_intSI_type_node;
107 tree unsigned_intDI_type_node;
108 tree unsigned_intTI_type_node;
110 tree widest_integer_literal_type_node;
111 tree widest_unsigned_literal_type_node;
113 Nodes for types `void *' and `const void *'.
115 tree ptr_type_node, const_ptr_type_node;
117 Nodes for types `char *' and `const char *'.
119 tree string_type_node, const_string_type_node;
121 Type `char[SOMENUMBER]'.
122 Used when an array of char is needed and the size is irrelevant.
124 tree char_array_type_node;
126 Type `wchar_t[SOMENUMBER]' or something like it.
127 Used when a wide string literal is created.
129 tree wchar_array_type_node;
131 Type `char16_t[SOMENUMBER]' or something like it.
132 Used when a UTF-16 string literal is created.
134 tree char16_array_type_node;
136 Type `char32_t[SOMENUMBER]' or something like it.
137 Used when a UTF-32 string literal is created.
139 tree char32_array_type_node;
141 Type `int ()' -- used for implicit declaration of functions.
143 tree default_function_type;
145 A VOID_TYPE node, packaged in a TREE_LIST.
147 tree void_list_node;
149 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
150 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
151 VAR_DECLS, but C++ does.)
153 tree function_name_decl_node;
154 tree pretty_function_name_decl_node;
155 tree c99_function_name_decl_node;
157 Stack of nested function name VAR_DECLs.
159 tree saved_function_name_decls;
163 tree c_global_trees[CTI_MAX];
165 /* Switches common to the C front ends. */
167 /* Nonzero means don't output line number information. */
169 char flag_no_line_commands;
171 /* Nonzero causes -E output not to be done, but directives such as
172 #define that have side effects are still obeyed. */
174 char flag_no_output;
176 /* Nonzero means dump macros in some fashion. */
178 char flag_dump_macros;
180 /* Nonzero means pass #include lines through to the output. */
182 char flag_dump_includes;
184 /* Nonzero means process PCH files while preprocessing. */
186 bool flag_pch_preprocess;
188 /* The file name to which we should write a precompiled header, or
189 NULL if no header will be written in this compile. */
191 const char *pch_file;
193 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 user's namespace. */
195 int flag_iso;
197 /* C/ObjC language option variables. */
200 /* Nonzero means allow type mismatches in conditional expressions;
201 just make their values `void'. */
203 int flag_cond_mismatch;
205 /* Nonzero means enable C89 Amendment 1 features. */
207 int flag_isoc94;
209 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
211 int flag_isoc99;
213 /* Nonzero means use the ISO C11 dialect of C. */
215 int flag_isoc11;
217 /* Nonzero means use the ISO C2X dialect of C. */
219 int flag_isoc2x;
221 /* Nonzero means that we have builtin functions, and main is an int. */
223 int flag_hosted = 1;
226 /* ObjC language option variables. */
229 /* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
233 int print_struct_values;
235 /* Tells the compiler what is the constant string class for ObjC. */
237 const char *constant_string_class_name;
240 /* C++ language option variables. */
242 /* The reference version of the ABI for -Wabi. */
244 int warn_abi_version = -1;
246 /* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
249 int flag_use_repository;
251 /* The C++ dialect being used. Default set in c_common_post_options. */
253 enum cxx_dialect cxx_dialect = cxx_unset;
255 /* Maximum template instantiation depth. This limit exists to limit the
256 time it takes to notice excessively recursive template instantiations.
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
262 int max_tinst_depth = 900;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266 tree *ridpointers;
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272 int c_inhibit_evaluation_warnings;
274 /* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280 bool in_late_binary_op;
282 /* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284 bool done_lexing = false;
286 /* Information about how a function name is generated. */
287 struct fname_var_t
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
294 /* The three ways of getting then name of the current function. */
296 const struct fname_var_t fname_vars[] =
298 /* C99 compliant __func__, must be first. */
299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
300 /* GCC __FUNCTION__ compliant. */
301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
302 /* GCC __PRETTY_FUNCTION__ compliant. */
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
307 /* Global visibility options. */
308 struct visibility_flags visibility_options;
310 static tree check_case_value (location_t, tree);
311 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
312 bool *);
315 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
316 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
318 /* Reserved words. The third field is a mask: keywords are disabled
319 if they match the mask.
321 Masks for languages:
322 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
323 C --std=c99: D_CXXONLY | D_OBJC
324 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
325 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
326 C++ --std=c++11: D_CONLY | D_OBJC
327 ObjC++ is like C++ except that D_OBJC is not set
329 If -fno-asm is used, D_ASM is added to the mask. If
330 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
331 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
332 In C with -Wc++-compat, we warn if D_CXXWARN is set.
334 Note the complication of the D_CXX_OBJC keywords. These are
335 reserved words such as 'class'. In C++, 'class' is a reserved
336 word. In Objective-C++ it is too. In Objective-C, it is a
337 reserved word too, but only if it follows an '@' sign.
339 const struct c_common_resword c_common_reswords[] =
341 { "_Alignas", RID_ALIGNAS, D_CONLY },
342 { "_Alignof", RID_ALIGNOF, D_CONLY },
343 { "_Atomic", RID_ATOMIC, D_CONLY },
344 { "_Bool", RID_BOOL, D_CONLY },
345 { "_Complex", RID_COMPLEX, 0 },
346 { "_Imaginary", RID_IMAGINARY, D_CONLY },
347 { "_Float16", RID_FLOAT16, D_CONLY },
348 { "_Float32", RID_FLOAT32, D_CONLY },
349 { "_Float64", RID_FLOAT64, D_CONLY },
350 { "_Float128", RID_FLOAT128, D_CONLY },
351 { "_Float32x", RID_FLOAT32X, D_CONLY },
352 { "_Float64x", RID_FLOAT64X, D_CONLY },
353 { "_Float128x", RID_FLOAT128X, D_CONLY },
354 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
355 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
356 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
357 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
358 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
359 { "_Sat", RID_SAT, D_CONLY | D_EXT },
360 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
361 { "_Noreturn", RID_NORETURN, D_CONLY },
362 { "_Generic", RID_GENERIC, D_CONLY },
363 { "_Thread_local", RID_THREAD, D_CONLY },
364 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
365 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
366 { "__alignof", RID_ALIGNOF, 0 },
367 { "__alignof__", RID_ALIGNOF, 0 },
368 { "__asm", RID_ASM, 0 },
369 { "__asm__", RID_ASM, 0 },
370 { "__attribute", RID_ATTRIBUTE, 0 },
371 { "__attribute__", RID_ATTRIBUTE, 0 },
372 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
373 { "__bases", RID_BASES, D_CXXONLY },
374 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
375 { "__builtin_call_with_static_chain",
376 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
377 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
378 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
379 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
380 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
381 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
382 { "__builtin_offsetof", RID_OFFSETOF, 0 },
383 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
384 { "__builtin_va_arg", RID_VA_ARG, 0 },
385 { "__complex", RID_COMPLEX, 0 },
386 { "__complex__", RID_COMPLEX, 0 },
387 { "__const", RID_CONST, 0 },
388 { "__const__", RID_CONST, 0 },
389 { "__decltype", RID_DECLTYPE, D_CXXONLY },
390 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
391 { "__extension__", RID_EXTENSION, 0 },
392 { "__func__", RID_C99_FUNCTION_NAME, 0 },
393 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
394 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
395 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
396 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
397 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
398 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
399 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
400 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
401 D_CXXONLY },
402 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
403 { "__imag", RID_IMAGPART, 0 },
404 { "__imag__", RID_IMAGPART, 0 },
405 { "__inline", RID_INLINE, 0 },
406 { "__inline__", RID_INLINE, 0 },
407 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
408 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
409 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
410 { "__is_class", RID_IS_CLASS, D_CXXONLY },
411 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
412 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
413 { "__is_final", RID_IS_FINAL, D_CXXONLY },
414 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
415 { "__is_pod", RID_IS_POD, D_CXXONLY },
416 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
417 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
418 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
419 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
420 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
421 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
422 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
423 { "__is_union", RID_IS_UNION, D_CXXONLY },
424 { "__label__", RID_LABEL, 0 },
425 { "__null", RID_NULL, 0 },
426 { "__real", RID_REALPART, 0 },
427 { "__real__", RID_REALPART, 0 },
428 { "__restrict", RID_RESTRICT, 0 },
429 { "__restrict__", RID_RESTRICT, 0 },
430 { "__signed", RID_SIGNED, 0 },
431 { "__signed__", RID_SIGNED, 0 },
432 { "__thread", RID_THREAD, 0 },
433 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
434 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
435 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
436 { "__typeof", RID_TYPEOF, 0 },
437 { "__typeof__", RID_TYPEOF, 0 },
438 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
439 { "__volatile", RID_VOLATILE, 0 },
440 { "__volatile__", RID_VOLATILE, 0 },
441 { "__GIMPLE", RID_GIMPLE, D_CONLY },
442 { "__PHI", RID_PHI, D_CONLY },
443 { "__RTL", RID_RTL, D_CONLY },
444 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
445 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
446 { "asm", RID_ASM, D_ASM },
447 { "auto", RID_AUTO, 0 },
448 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
449 { "break", RID_BREAK, 0 },
450 { "case", RID_CASE, 0 },
451 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
452 { "char", RID_CHAR, 0 },
453 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
454 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
455 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
456 { "const", RID_CONST, 0 },
457 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
458 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
459 { "continue", RID_CONTINUE, 0 },
460 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
461 { "default", RID_DEFAULT, 0 },
462 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
463 { "do", RID_DO, 0 },
464 { "double", RID_DOUBLE, 0 },
465 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
466 { "else", RID_ELSE, 0 },
467 { "enum", RID_ENUM, 0 },
468 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
469 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
470 { "extern", RID_EXTERN, 0 },
471 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
472 { "float", RID_FLOAT, 0 },
473 { "for", RID_FOR, 0 },
474 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
475 { "goto", RID_GOTO, 0 },
476 { "if", RID_IF, 0 },
477 { "inline", RID_INLINE, D_EXT89 },
478 { "int", RID_INT, 0 },
479 { "long", RID_LONG, 0 },
480 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
481 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
482 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
483 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
484 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
485 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
486 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
487 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
488 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
489 { "register", RID_REGISTER, 0 },
490 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
491 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
492 { "return", RID_RETURN, 0 },
493 { "short", RID_SHORT, 0 },
494 { "signed", RID_SIGNED, 0 },
495 { "sizeof", RID_SIZEOF, 0 },
496 { "static", RID_STATIC, 0 },
497 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
498 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
499 { "struct", RID_STRUCT, 0 },
500 { "switch", RID_SWITCH, 0 },
501 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
502 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
503 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
504 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
505 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
506 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
507 { "typedef", RID_TYPEDEF, 0 },
508 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
509 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
510 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
511 { "union", RID_UNION, 0 },
512 { "unsigned", RID_UNSIGNED, 0 },
513 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
514 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
515 { "void", RID_VOID, 0 },
516 { "volatile", RID_VOLATILE, 0 },
517 { "wchar_t", RID_WCHAR, D_CXXONLY },
518 { "while", RID_WHILE, 0 },
519 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
520 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
522 /* C++ transactional memory. */
523 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
524 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
525 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
526 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
528 /* Concepts-related keywords */
529 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
530 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
532 /* These Objective-C keywords are recognized only immediately after
533 an '@'. */
534 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
535 { "defs", RID_AT_DEFS, D_OBJC },
536 { "encode", RID_AT_ENCODE, D_OBJC },
537 { "end", RID_AT_END, D_OBJC },
538 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
539 { "interface", RID_AT_INTERFACE, D_OBJC },
540 { "protocol", RID_AT_PROTOCOL, D_OBJC },
541 { "selector", RID_AT_SELECTOR, D_OBJC },
542 { "finally", RID_AT_FINALLY, D_OBJC },
543 { "optional", RID_AT_OPTIONAL, D_OBJC },
544 { "required", RID_AT_REQUIRED, D_OBJC },
545 { "property", RID_AT_PROPERTY, D_OBJC },
546 { "package", RID_AT_PACKAGE, D_OBJC },
547 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
548 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
549 /* These are recognized only in protocol-qualifier context
550 (see above) */
551 { "bycopy", RID_BYCOPY, D_OBJC },
552 { "byref", RID_BYREF, D_OBJC },
553 { "in", RID_IN, D_OBJC },
554 { "inout", RID_INOUT, D_OBJC },
555 { "oneway", RID_ONEWAY, D_OBJC },
556 { "out", RID_OUT, D_OBJC },
557 /* These are recognized inside a property attribute list */
558 { "assign", RID_ASSIGN, D_OBJC },
559 { "copy", RID_COPY, D_OBJC },
560 { "getter", RID_GETTER, D_OBJC },
561 { "nonatomic", RID_NONATOMIC, D_OBJC },
562 { "readonly", RID_READONLY, D_OBJC },
563 { "readwrite", RID_READWRITE, D_OBJC },
564 { "retain", RID_RETAIN, D_OBJC },
565 { "setter", RID_SETTER, D_OBJC },
568 const unsigned int num_c_common_reswords =
569 sizeof c_common_reswords / sizeof (struct c_common_resword);
571 /* Return identifier for address space AS. */
573 const char *
574 c_addr_space_name (addr_space_t as)
576 int rid = RID_FIRST_ADDR_SPACE + as;
577 gcc_assert (ridpointers [rid]);
578 return IDENTIFIER_POINTER (ridpointers [rid]);
581 /* Push current bindings for the function name VAR_DECLS. */
583 void
584 start_fname_decls (void)
586 unsigned ix;
587 tree saved = NULL_TREE;
589 for (ix = 0; fname_vars[ix].decl; ix++)
591 tree decl = *fname_vars[ix].decl;
593 if (decl)
595 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
596 saved);
597 *fname_vars[ix].decl = NULL_TREE;
600 if (saved || saved_function_name_decls)
601 /* Normally they'll have been NULL, so only push if we've got a
602 stack, or they are non-NULL. */
603 saved_function_name_decls = tree_cons (saved, NULL_TREE,
604 saved_function_name_decls);
607 /* Finish up the current bindings, adding them into the current function's
608 statement tree. This must be done _before_ finish_stmt_tree is called.
609 If there is no current function, we must be at file scope and no statements
610 are involved. Pop the previous bindings. */
612 void
613 finish_fname_decls (void)
615 unsigned ix;
616 tree stmts = NULL_TREE;
617 tree stack = saved_function_name_decls;
619 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
620 append_to_statement_list (TREE_VALUE (stack), &stmts);
622 if (stmts)
624 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
626 if (TREE_CODE (*bodyp) == BIND_EXPR)
627 bodyp = &BIND_EXPR_BODY (*bodyp);
629 append_to_statement_list_force (*bodyp, &stmts);
630 *bodyp = stmts;
633 for (ix = 0; fname_vars[ix].decl; ix++)
634 *fname_vars[ix].decl = NULL_TREE;
636 if (stack)
638 /* We had saved values, restore them. */
639 tree saved;
641 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
643 tree decl = TREE_PURPOSE (saved);
644 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
646 *fname_vars[ix].decl = decl;
648 stack = TREE_CHAIN (stack);
650 saved_function_name_decls = stack;
653 /* Return the text name of the current function, suitably prettified
654 by PRETTY_P. Return string must be freed by caller. */
656 const char *
657 fname_as_string (int pretty_p)
659 const char *name = "top level";
660 char *namep;
661 int vrb = 2, len;
662 cpp_string cstr = { 0, 0 }, strname;
664 if (!pretty_p)
666 name = "";
667 vrb = 0;
670 if (current_function_decl)
671 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
673 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
675 namep = XNEWVEC (char, len);
676 snprintf (namep, len, "\"%s\"", name);
677 strname.text = (unsigned char *) namep;
678 strname.len = len - 1;
680 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
682 XDELETEVEC (namep);
683 return (const char *) cstr.text;
686 return namep;
689 /* Return the VAR_DECL for a const char array naming the current
690 function. If the VAR_DECL has not yet been created, create it
691 now. RID indicates how it should be formatted and IDENTIFIER_NODE
692 ID is its name (unfortunately C and C++ hold the RID values of
693 keywords in different places, so we can't derive RID from ID in
694 this language independent code. LOC is the location of the
695 function. */
697 tree
698 fname_decl (location_t loc, unsigned int rid, tree id)
700 unsigned ix;
701 tree decl = NULL_TREE;
703 for (ix = 0; fname_vars[ix].decl; ix++)
704 if (fname_vars[ix].rid == rid)
705 break;
707 decl = *fname_vars[ix].decl;
708 if (!decl)
710 /* If a tree is built here, it would normally have the lineno of
711 the current statement. Later this tree will be moved to the
712 beginning of the function and this line number will be wrong.
713 To avoid this problem set the lineno to 0 here; that prevents
714 it from appearing in the RTL. */
715 tree stmts;
716 location_t saved_location = input_location;
717 input_location = UNKNOWN_LOCATION;
719 stmts = push_stmt_list ();
720 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
721 stmts = pop_stmt_list (stmts);
722 if (!IS_EMPTY_STMT (stmts))
723 saved_function_name_decls
724 = tree_cons (decl, stmts, saved_function_name_decls);
725 *fname_vars[ix].decl = decl;
726 input_location = saved_location;
728 if (!ix && !current_function_decl)
729 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
731 return decl;
734 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
736 tree
737 fix_string_type (tree value)
739 int length = TREE_STRING_LENGTH (value);
740 int nchars;
741 tree e_type, i_type, a_type;
743 /* Compute the number of elements, for the array type. */
744 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
746 nchars = length;
747 e_type = char_type_node;
749 else if (TREE_TYPE (value) == char16_array_type_node)
751 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
752 e_type = char16_type_node;
754 else if (TREE_TYPE (value) == char32_array_type_node)
756 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
757 e_type = char32_type_node;
759 else
761 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
762 e_type = wchar_type_node;
765 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
766 limit in C++98 Annex B is very large (65536) and is not normative,
767 so we do not diagnose it (warn_overlength_strings is forced off
768 in c_common_post_options). */
769 if (warn_overlength_strings)
771 const int nchars_max = flag_isoc99 ? 4095 : 509;
772 const int relevant_std = flag_isoc99 ? 99 : 90;
773 if (nchars - 1 > nchars_max)
774 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
775 separate the %d from the 'C'. 'ISO' should not be
776 translated, but it may be moved after 'C%d' in languages
777 where modifiers follow nouns. */
778 pedwarn (input_location, OPT_Woverlength_strings,
779 "string length %qd is greater than the length %qd "
780 "ISO C%d compilers are required to support",
781 nchars - 1, nchars_max, relevant_std);
784 /* Create the array type for the string constant. The ISO C++
785 standard says that a string literal has type `const char[N]' or
786 `const wchar_t[N]'. We use the same logic when invoked as a C
787 front-end with -Wwrite-strings.
788 ??? We should change the type of an expression depending on the
789 state of a warning flag. We should just be warning -- see how
790 this is handled in the C++ front-end for the deprecated implicit
791 conversion from string literals to `char*' or `wchar_t*'.
793 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
794 array type being the unqualified version of that type.
795 Therefore, if we are constructing an array of const char, we must
796 construct the matching unqualified array type first. The C front
797 end does not require this, but it does no harm, so we do it
798 unconditionally. */
799 i_type = build_index_type (size_int (nchars - 1));
800 a_type = build_array_type (e_type, i_type);
801 if (c_dialect_cxx() || warn_write_strings)
802 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
804 TREE_TYPE (value) = a_type;
805 TREE_CONSTANT (value) = 1;
806 TREE_READONLY (value) = 1;
807 TREE_STATIC (value) = 1;
808 return value;
811 /* Given a string of type STRING_TYPE, determine what kind of string
812 token would give an equivalent execution encoding: CPP_STRING,
813 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
814 This may not be exactly the string token type that initially created
815 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
816 string type at this point.
818 This effectively reverses part of the logic in lex_string and
819 fix_string_type. */
821 static enum cpp_ttype
822 get_cpp_ttype_from_string_type (tree string_type)
824 gcc_assert (string_type);
825 if (TREE_CODE (string_type) == POINTER_TYPE)
826 string_type = TREE_TYPE (string_type);
828 if (TREE_CODE (string_type) != ARRAY_TYPE)
829 return CPP_OTHER;
831 tree element_type = TREE_TYPE (string_type);
832 if (TREE_CODE (element_type) != INTEGER_TYPE)
833 return CPP_OTHER;
835 int bits_per_character = TYPE_PRECISION (element_type);
836 switch (bits_per_character)
838 case 8:
839 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
840 case 16:
841 return CPP_STRING16;
842 case 32:
843 return CPP_STRING32;
846 return CPP_OTHER;
849 /* The global record of string concatentations, for use in
850 extracting locations within string literals. */
852 GTY(()) string_concat_db *g_string_concat_db;
854 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
856 const char *
857 c_get_substring_location (const substring_loc &substr_loc,
858 location_t *out_loc)
860 enum cpp_ttype tok_type
861 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
862 if (tok_type == CPP_OTHER)
863 return "unrecognized string type";
865 return get_location_within_string (parse_in, g_string_concat_db,
866 substr_loc.get_fmt_string_loc (),
867 tok_type,
868 substr_loc.get_caret_idx (),
869 substr_loc.get_start_idx (),
870 substr_loc.get_end_idx (),
871 out_loc);
875 /* Return true iff T is a boolean promoted to int. */
877 bool
878 bool_promoted_to_int_p (tree t)
880 return (CONVERT_EXPR_P (t)
881 && TREE_TYPE (t) == integer_type_node
882 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
885 /* vector_targets_convertible_p is used for vector pointer types. The
886 callers perform various checks that the qualifiers are satisfactory,
887 while OTOH vector_targets_convertible_p ignores the number of elements
888 in the vectors. That's fine with vector pointers as we can consider,
889 say, a vector of 8 elements as two consecutive vectors of 4 elements,
890 and that does not require and conversion of the pointer values.
891 In contrast, vector_types_convertible_p and
892 vector_types_compatible_elements_p are used for vector value types. */
893 /* True if pointers to distinct types T1 and T2 can be converted to
894 each other without an explicit cast. Only returns true for opaque
895 vector types. */
896 bool
897 vector_targets_convertible_p (const_tree t1, const_tree t2)
899 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
900 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
901 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
902 return true;
904 return false;
907 /* vector_types_convertible_p is used for vector value types.
908 It could in principle call vector_targets_convertible_p as a subroutine,
909 but then the check for vector type would be duplicated with its callers,
910 and also the purpose of vector_targets_convertible_p would become
911 muddled.
912 Where vector_types_convertible_p returns true, a conversion might still be
913 needed to make the types match.
914 In contrast, vector_targets_convertible_p is used for vector pointer
915 values, and vector_types_compatible_elements_p is used specifically
916 in the context for binary operators, as a check if use is possible without
917 conversion. */
918 /* True if vector types T1 and T2 can be converted to each other
919 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
920 can only be converted with -flax-vector-conversions yet that is not
921 in effect, emit a note telling the user about that option if such
922 a note has not previously been emitted. */
923 bool
924 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
926 static bool emitted_lax_note = false;
927 bool convertible_lax;
929 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
930 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
931 return true;
933 convertible_lax =
934 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
935 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
936 || known_eq (TYPE_VECTOR_SUBPARTS (t1),
937 TYPE_VECTOR_SUBPARTS (t2)))
938 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
939 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
941 if (!convertible_lax || flag_lax_vector_conversions)
942 return convertible_lax;
944 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
945 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
946 return true;
948 if (emit_lax_note && !emitted_lax_note)
950 emitted_lax_note = true;
951 inform (input_location, "use -flax-vector-conversions to permit "
952 "conversions between vectors with differing "
953 "element types or numbers of subparts");
956 return false;
959 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
960 and have vector types, V0 has the same type as V1, and the number of
961 elements of V0, V1, MASK is the same.
963 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
964 called with two arguments. In this case implementation passes the
965 first argument twice in order to share the same tree code. This fact
966 could enable the mask-values being twice the vector length. This is
967 an implementation accident and this semantics is not guaranteed to
968 the user. */
969 tree
970 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
971 bool complain)
973 tree ret;
974 bool wrap = true;
975 bool maybe_const = false;
976 bool two_arguments = false;
978 if (v1 == NULL_TREE)
980 two_arguments = true;
981 v1 = v0;
984 if (v0 == error_mark_node || v1 == error_mark_node
985 || mask == error_mark_node)
986 return error_mark_node;
988 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
990 if (complain)
991 error_at (loc, "__builtin_shuffle last argument must "
992 "be an integer vector");
993 return error_mark_node;
996 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
997 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
999 if (complain)
1000 error_at (loc, "__builtin_shuffle arguments must be vectors");
1001 return error_mark_node;
1004 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1006 if (complain)
1007 error_at (loc, "__builtin_shuffle argument vectors must be of "
1008 "the same type");
1009 return error_mark_node;
1012 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1013 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1014 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1015 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1017 if (complain)
1018 error_at (loc, "__builtin_shuffle number of elements of the "
1019 "argument vector(s) and the mask vector should "
1020 "be the same");
1021 return error_mark_node;
1024 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1025 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1027 if (complain)
1028 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1029 "must have the same size as inner type of the mask");
1030 return error_mark_node;
1033 if (!c_dialect_cxx ())
1035 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1036 v0 = c_fully_fold (v0, false, &maybe_const);
1037 wrap &= maybe_const;
1039 if (two_arguments)
1040 v1 = v0 = save_expr (v0);
1041 else
1043 v1 = c_fully_fold (v1, false, &maybe_const);
1044 wrap &= maybe_const;
1047 mask = c_fully_fold (mask, false, &maybe_const);
1048 wrap &= maybe_const;
1050 else if (two_arguments)
1051 v1 = v0 = save_expr (v0);
1053 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1055 if (!c_dialect_cxx () && !wrap)
1056 ret = c_wrap_maybe_const (ret, true);
1058 return ret;
1061 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1062 to integral type. */
1064 tree
1065 c_common_get_narrower (tree op, int *unsignedp_ptr)
1067 op = get_narrower (op, unsignedp_ptr);
1069 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1070 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1072 /* C++0x scoped enumerations don't implicitly convert to integral
1073 type; if we stripped an explicit conversion to a larger type we
1074 need to replace it so common_type will still work. */
1075 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1076 TYPE_UNSIGNED (TREE_TYPE (op)));
1077 op = fold_convert (type, op);
1079 return op;
1082 /* This is a helper function of build_binary_op.
1084 For certain operations if both args were extended from the same
1085 smaller type, do the arithmetic in that type and then extend.
1087 BITWISE indicates a bitwise operation.
1088 For them, this optimization is safe only if
1089 both args are zero-extended or both are sign-extended.
1090 Otherwise, we might change the result.
1091 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1092 but calculated in (unsigned short) it would be (unsigned short)-1.
1094 tree
1095 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1097 int unsigned0, unsigned1;
1098 tree arg0, arg1;
1099 int uns;
1100 tree type;
1102 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1103 excessive narrowing when we call get_narrower below. For
1104 example, suppose that OP0 is of unsigned int extended
1105 from signed char and that RESULT_TYPE is long long int.
1106 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1107 like
1109 (long long int) (unsigned int) signed_char
1111 which get_narrower would narrow down to
1113 (unsigned int) signed char
1115 If we do not cast OP0 first, get_narrower would return
1116 signed_char, which is inconsistent with the case of the
1117 explicit cast. */
1118 op0 = convert (result_type, op0);
1119 op1 = convert (result_type, op1);
1121 arg0 = c_common_get_narrower (op0, &unsigned0);
1122 arg1 = c_common_get_narrower (op1, &unsigned1);
1124 /* UNS is 1 if the operation to be done is an unsigned one. */
1125 uns = TYPE_UNSIGNED (result_type);
1127 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1128 but it *requires* conversion to FINAL_TYPE. */
1130 if ((TYPE_PRECISION (TREE_TYPE (op0))
1131 == TYPE_PRECISION (TREE_TYPE (arg0)))
1132 && TREE_TYPE (op0) != result_type)
1133 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1134 if ((TYPE_PRECISION (TREE_TYPE (op1))
1135 == TYPE_PRECISION (TREE_TYPE (arg1)))
1136 && TREE_TYPE (op1) != result_type)
1137 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1139 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1141 /* For bitwise operations, signedness of nominal type
1142 does not matter. Consider only how operands were extended. */
1143 if (bitwise)
1144 uns = unsigned0;
1146 /* Note that in all three cases below we refrain from optimizing
1147 an unsigned operation on sign-extended args.
1148 That would not be valid. */
1150 /* Both args variable: if both extended in same way
1151 from same width, do it in that width.
1152 Do it unsigned if args were zero-extended. */
1153 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1154 < TYPE_PRECISION (result_type))
1155 && (TYPE_PRECISION (TREE_TYPE (arg1))
1156 == TYPE_PRECISION (TREE_TYPE (arg0)))
1157 && unsigned0 == unsigned1
1158 && (unsigned0 || !uns))
1159 return c_common_signed_or_unsigned_type
1160 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1162 else if (TREE_CODE (arg0) == INTEGER_CST
1163 && (unsigned1 || !uns)
1164 && (TYPE_PRECISION (TREE_TYPE (arg1))
1165 < TYPE_PRECISION (result_type))
1166 && (type
1167 = c_common_signed_or_unsigned_type (unsigned1,
1168 TREE_TYPE (arg1)))
1169 && !POINTER_TYPE_P (type)
1170 && int_fits_type_p (arg0, type))
1171 return type;
1173 else if (TREE_CODE (arg1) == INTEGER_CST
1174 && (unsigned0 || !uns)
1175 && (TYPE_PRECISION (TREE_TYPE (arg0))
1176 < TYPE_PRECISION (result_type))
1177 && (type
1178 = c_common_signed_or_unsigned_type (unsigned0,
1179 TREE_TYPE (arg0)))
1180 && !POINTER_TYPE_P (type)
1181 && int_fits_type_p (arg1, type))
1182 return type;
1184 return result_type;
1187 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1188 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1190 static bool
1191 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1193 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1194 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1195 REAL_VALUE_TYPE real_low_bound =
1196 real_value_from_int_cst (0, type_low_bound);
1197 REAL_VALUE_TYPE real_high_bound =
1198 real_value_from_int_cst (0, type_high_bound);
1200 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1201 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1204 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1205 to the complex/real/integer type TYPE. Function returns non-zero when:
1206 * EXPR is a constant which cannot be exactly converted to TYPE.
1207 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1208 for EXPR type and TYPE being both integers or both real, or both
1209 complex.
1210 * EXPR is not a constant of complex type and TYPE is a real or
1211 an integer.
1212 * EXPR is not a constant of real type and TYPE is an integer.
1213 * EXPR is not a constant of integer type which cannot be
1214 exactly converted to real type.
1216 Function allows conversions between types of different signedness and
1217 can return SAFE_CONVERSION (zero) in that case. Function can produce
1218 signedness warnings if PRODUCE_WARNS is true.
1220 RESULT, when non-null is the result of the conversion. When constant
1221 it is included in the text of diagnostics.
1223 Function allows conversions from complex constants to non-complex types,
1224 provided that imaginary part is zero and real part can be safely converted
1225 to TYPE. */
1227 enum conversion_safety
1228 unsafe_conversion_p (location_t loc, tree type, tree expr, tree result,
1229 bool produce_warns)
1231 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1232 tree expr_type = TREE_TYPE (expr);
1234 bool cstresult = (result
1235 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1237 loc = expansion_point_location_if_in_system_header (loc);
1239 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1241 /* If type is complex, we are interested in compatibility with
1242 underlying type. */
1243 if (TREE_CODE (type) == COMPLEX_TYPE)
1244 type = TREE_TYPE (type);
1246 /* Warn for real constant that is not an exact integer converted
1247 to integer type. */
1248 if (TREE_CODE (expr_type) == REAL_TYPE
1249 && TREE_CODE (type) == INTEGER_TYPE)
1251 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1252 give_warning = UNSAFE_REAL;
1254 /* Warn for an integer constant that does not fit into integer type. */
1255 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1256 && TREE_CODE (type) == INTEGER_TYPE
1257 && !int_fits_type_p (expr, type))
1259 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1260 && tree_int_cst_sgn (expr) < 0)
1262 if (produce_warns)
1264 if (cstresult)
1265 warning_at (loc, OPT_Wsign_conversion,
1266 "unsigned conversion from %qT to %qT "
1267 "changes value from %qE to %qE",
1268 expr_type, type, expr, result);
1269 else
1270 warning_at (loc, OPT_Wsign_conversion,
1271 "unsigned conversion from %qT to %qT "
1272 "changes the value of %qE",
1273 expr_type, type, expr);
1276 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1278 if (cstresult)
1279 warning_at (loc, OPT_Wsign_conversion,
1280 "signed conversion from %qT to %qT changes "
1281 "value from %qE to %qE",
1282 expr_type, type, expr, result);
1283 else
1284 warning_at (loc, OPT_Wsign_conversion,
1285 "signed conversion from %qT to %qT changes "
1286 "the value of %qE",
1287 expr_type, type, expr);
1289 else
1290 give_warning = UNSAFE_OTHER;
1292 else if (TREE_CODE (type) == REAL_TYPE)
1294 /* Warn for an integer constant that does not fit into real type. */
1295 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1297 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1298 if (!exact_real_truncate (TYPE_MODE (type), &a))
1299 give_warning = UNSAFE_REAL;
1301 /* Warn for a real constant that does not fit into a smaller
1302 real type. */
1303 else if (TREE_CODE (expr_type) == REAL_TYPE
1304 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1306 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1307 if (!exact_real_truncate (TYPE_MODE (type), &a))
1308 give_warning = UNSAFE_REAL;
1313 else if (TREE_CODE (expr) == COMPLEX_CST)
1315 tree imag_part = TREE_IMAGPART (expr);
1316 /* Conversion from complex constant with zero imaginary part,
1317 perform check for conversion of real part. */
1318 if ((TREE_CODE (imag_part) == REAL_CST
1319 && real_zerop (imag_part))
1320 || (TREE_CODE (imag_part) == INTEGER_CST
1321 && integer_zerop (imag_part)))
1322 /* Note: in this branch we use recursive call to unsafe_conversion_p
1323 with different type of EXPR, but it is still safe, because when EXPR
1324 is a constant, it's type is not used in text of generated warnings
1325 (otherwise they could sound misleading). */
1326 return unsafe_conversion_p (loc, type, TREE_REALPART (expr), result,
1327 produce_warns);
1328 /* Conversion from complex constant with non-zero imaginary part. */
1329 else
1331 /* Conversion to complex type.
1332 Perform checks for both real and imaginary parts. */
1333 if (TREE_CODE (type) == COMPLEX_TYPE)
1335 /* Unfortunately, produce_warns must be false in two subsequent
1336 calls of unsafe_conversion_p, because otherwise we could
1337 produce strange "double" warnings, if both real and imaginary
1338 parts have conversion problems related to signedness.
1340 For example:
1341 int32_t _Complex a = 0x80000000 + 0x80000000i;
1343 Possible solution: add a separate function for checking
1344 constants and combine result of two calls appropriately. */
1345 enum conversion_safety re_safety =
1346 unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1347 result, false);
1348 enum conversion_safety im_safety =
1349 unsafe_conversion_p (loc, type, imag_part, result, false);
1351 /* Merge the results into appropriate single warning. */
1353 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1354 if (re_safety == im_safety)
1355 give_warning = re_safety;
1356 else if (!re_safety && im_safety)
1357 give_warning = im_safety;
1358 else if (re_safety && !im_safety)
1359 give_warning = re_safety;
1360 else
1361 give_warning = UNSAFE_OTHER;
1363 /* Warn about conversion from complex to real or integer type. */
1364 else
1365 give_warning = UNSAFE_IMAGINARY;
1369 /* Checks for remaining case: EXPR is not constant. */
1370 else
1372 /* Warn for real types converted to integer types. */
1373 if (TREE_CODE (expr_type) == REAL_TYPE
1374 && TREE_CODE (type) == INTEGER_TYPE)
1375 give_warning = UNSAFE_REAL;
1377 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1378 && TREE_CODE (type) == INTEGER_TYPE)
1380 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1381 expr = get_unwidened (expr, 0);
1382 expr_type = TREE_TYPE (expr);
1384 /* Don't warn for short y; short x = ((int)y & 0xff); */
1385 if (TREE_CODE (expr) == BIT_AND_EXPR
1386 || TREE_CODE (expr) == BIT_IOR_EXPR
1387 || TREE_CODE (expr) == BIT_XOR_EXPR)
1389 /* If both args were extended from a shortest type,
1390 use that type if that is safe. */
1391 expr_type = shorten_binary_op (expr_type,
1392 TREE_OPERAND (expr, 0),
1393 TREE_OPERAND (expr, 1),
1394 /* bitwise */1);
1396 if (TREE_CODE (expr) == BIT_AND_EXPR)
1398 tree op0 = TREE_OPERAND (expr, 0);
1399 tree op1 = TREE_OPERAND (expr, 1);
1400 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1401 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1403 /* If one of the operands is a non-negative constant
1404 that fits in the target type, then the type of the
1405 other operand does not matter. */
1406 if ((TREE_CODE (op0) == INTEGER_CST
1407 && int_fits_type_p (op0, c_common_signed_type (type))
1408 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1409 || (TREE_CODE (op1) == INTEGER_CST
1410 && int_fits_type_p (op1, c_common_signed_type (type))
1411 && int_fits_type_p (op1,
1412 c_common_unsigned_type (type))))
1413 return SAFE_CONVERSION;
1414 /* If constant is unsigned and fits in the target
1415 type, then the result will also fit. */
1416 else if ((TREE_CODE (op0) == INTEGER_CST
1417 && unsigned0
1418 && int_fits_type_p (op0, type))
1419 || (TREE_CODE (op1) == INTEGER_CST
1420 && unsigned1
1421 && int_fits_type_p (op1, type)))
1422 return SAFE_CONVERSION;
1425 /* Warn for integer types converted to smaller integer types. */
1426 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1427 give_warning = UNSAFE_OTHER;
1429 /* When they are the same width but different signedness,
1430 then the value may change. */
1431 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1432 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1433 /* Even when converted to a bigger type, if the type is
1434 unsigned but expr is signed, then negative values
1435 will be changed. */
1436 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1437 && produce_warns)
1438 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1439 "may change the sign of the result",
1440 type, expr_type);
1443 /* Warn for integer types converted to real types if and only if
1444 all the range of values of the integer type cannot be
1445 represented by the real type. */
1446 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1447 && TREE_CODE (type) == REAL_TYPE)
1449 /* Don't warn about char y = 0xff; float x = (int) y; */
1450 expr = get_unwidened (expr, 0);
1451 expr_type = TREE_TYPE (expr);
1453 if (!int_safely_convertible_to_real_p (expr_type, type))
1454 give_warning = UNSAFE_OTHER;
1457 /* Warn for real types converted to smaller real types. */
1458 else if (TREE_CODE (expr_type) == REAL_TYPE
1459 && TREE_CODE (type) == REAL_TYPE
1460 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1461 give_warning = UNSAFE_REAL;
1463 /* Check conversion between two complex types. */
1464 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1465 && TREE_CODE (type) == COMPLEX_TYPE)
1467 /* Extract underlying types (i.e., type of real and imaginary
1468 parts) of expr_type and type. */
1469 tree from_type = TREE_TYPE (expr_type);
1470 tree to_type = TREE_TYPE (type);
1472 /* Warn for real types converted to integer types. */
1473 if (TREE_CODE (from_type) == REAL_TYPE
1474 && TREE_CODE (to_type) == INTEGER_TYPE)
1475 give_warning = UNSAFE_REAL;
1477 /* Warn for real types converted to smaller real types. */
1478 else if (TREE_CODE (from_type) == REAL_TYPE
1479 && TREE_CODE (to_type) == REAL_TYPE
1480 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1481 give_warning = UNSAFE_REAL;
1483 /* Check conversion for complex integer types. Here implementation
1484 is simpler than for real-domain integers because it does not
1485 involve sophisticated cases, such as bitmasks, casts, etc. */
1486 else if (TREE_CODE (from_type) == INTEGER_TYPE
1487 && TREE_CODE (to_type) == INTEGER_TYPE)
1489 /* Warn for integer types converted to smaller integer types. */
1490 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1491 give_warning = UNSAFE_OTHER;
1493 /* Check for different signedness, see case for real-domain
1494 integers (above) for a more detailed comment. */
1495 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1496 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1497 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1498 && produce_warns)
1499 warning_at (loc, OPT_Wsign_conversion,
1500 "conversion to %qT from %qT "
1501 "may change the sign of the result",
1502 type, expr_type);
1504 else if (TREE_CODE (from_type) == INTEGER_TYPE
1505 && TREE_CODE (to_type) == REAL_TYPE
1506 && !int_safely_convertible_to_real_p (from_type, to_type))
1507 give_warning = UNSAFE_OTHER;
1510 /* Warn for complex types converted to real or integer types. */
1511 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1512 && TREE_CODE (type) != COMPLEX_TYPE)
1513 give_warning = UNSAFE_IMAGINARY;
1516 return give_warning;
1520 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1521 Invoke this function on every expression that is converted implicitly,
1522 i.e. because of language rules and not because of an explicit cast. */
1524 tree
1525 convert_and_check (location_t loc, tree type, tree expr)
1527 tree result;
1528 tree expr_for_warning;
1530 /* Convert from a value with possible excess precision rather than
1531 via the semantic type, but do not warn about values not fitting
1532 exactly in the semantic type. */
1533 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1535 tree orig_type = TREE_TYPE (expr);
1536 expr = TREE_OPERAND (expr, 0);
1537 expr_for_warning = convert (orig_type, expr);
1538 if (orig_type == type)
1539 return expr_for_warning;
1541 else
1542 expr_for_warning = expr;
1544 if (TREE_TYPE (expr) == type)
1545 return expr;
1547 result = convert (type, expr);
1549 if (c_inhibit_evaluation_warnings == 0
1550 && !TREE_OVERFLOW_P (expr)
1551 && result != error_mark_node)
1552 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1554 return result;
1557 /* A node in a list that describes references to variables (EXPR), which are
1558 either read accesses if WRITER is zero, or write accesses, in which case
1559 WRITER is the parent of EXPR. */
1560 struct tlist
1562 struct tlist *next;
1563 tree expr, writer;
1566 /* Used to implement a cache the results of a call to verify_tree. We only
1567 use this for SAVE_EXPRs. */
1568 struct tlist_cache
1570 struct tlist_cache *next;
1571 struct tlist *cache_before_sp;
1572 struct tlist *cache_after_sp;
1573 tree expr;
1576 /* Obstack to use when allocating tlist structures, and corresponding
1577 firstobj. */
1578 static struct obstack tlist_obstack;
1579 static char *tlist_firstobj = 0;
1581 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1582 warnings. */
1583 static struct tlist *warned_ids;
1584 /* SAVE_EXPRs need special treatment. We process them only once and then
1585 cache the results. */
1586 static struct tlist_cache *save_expr_cache;
1588 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1589 static void merge_tlist (struct tlist **, struct tlist *, int);
1590 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1591 static bool warning_candidate_p (tree);
1592 static bool candidate_equal_p (const_tree, const_tree);
1593 static void warn_for_collisions (struct tlist *);
1594 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1595 static struct tlist *new_tlist (struct tlist *, tree, tree);
1597 /* Create a new struct tlist and fill in its fields. */
1598 static struct tlist *
1599 new_tlist (struct tlist *next, tree t, tree writer)
1601 struct tlist *l;
1602 l = XOBNEW (&tlist_obstack, struct tlist);
1603 l->next = next;
1604 l->expr = t;
1605 l->writer = writer;
1606 return l;
1609 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1610 is nonnull, we ignore any node we find which has a writer equal to it. */
1612 static void
1613 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1615 while (add)
1617 struct tlist *next = add->next;
1618 if (!copy)
1619 add->next = *to;
1620 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1621 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1622 add = next;
1626 /* Merge the nodes of ADD into TO. This merging process is done so that for
1627 each variable that already exists in TO, no new node is added; however if
1628 there is a write access recorded in ADD, and an occurrence on TO is only
1629 a read access, then the occurrence in TO will be modified to record the
1630 write. */
1632 static void
1633 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1635 struct tlist **end = to;
1637 while (*end)
1638 end = &(*end)->next;
1640 while (add)
1642 int found = 0;
1643 struct tlist *tmp2;
1644 struct tlist *next = add->next;
1646 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1647 if (candidate_equal_p (tmp2->expr, add->expr))
1649 found = 1;
1650 if (!tmp2->writer)
1651 tmp2->writer = add->writer;
1653 if (!found)
1655 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1656 end = &(*end)->next;
1657 *end = 0;
1659 add = next;
1663 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1664 references in list LIST conflict with it, excluding reads if ONLY writers
1665 is nonzero. */
1667 static void
1668 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1669 int only_writes)
1671 struct tlist *tmp;
1673 /* Avoid duplicate warnings. */
1674 for (tmp = warned_ids; tmp; tmp = tmp->next)
1675 if (candidate_equal_p (tmp->expr, written))
1676 return;
1678 while (list)
1680 if (candidate_equal_p (list->expr, written)
1681 && !candidate_equal_p (list->writer, writer)
1682 && (!only_writes || list->writer))
1684 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1685 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1686 OPT_Wsequence_point, "operation on %qE may be undefined",
1687 list->expr);
1689 list = list->next;
1693 /* Given a list LIST of references to variables, find whether any of these
1694 can cause conflicts due to missing sequence points. */
1696 static void
1697 warn_for_collisions (struct tlist *list)
1699 struct tlist *tmp;
1701 for (tmp = list; tmp; tmp = tmp->next)
1703 if (tmp->writer)
1704 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1708 /* Return nonzero if X is a tree that can be verified by the sequence point
1709 warnings. */
1711 static bool
1712 warning_candidate_p (tree x)
1714 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1715 return false;
1717 if (TREE_CODE (x) == BLOCK)
1718 return false;
1720 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1721 (lvalue_p) crash on TRY/CATCH. */
1722 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1723 return false;
1725 if (!lvalue_p (x))
1726 return false;
1728 /* No point to track non-const calls, they will never satisfy
1729 operand_equal_p. */
1730 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1731 return false;
1733 if (TREE_CODE (x) == STRING_CST)
1734 return false;
1736 return true;
1739 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1740 static bool
1741 candidate_equal_p (const_tree x, const_tree y)
1743 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1746 /* Walk the tree X, and record accesses to variables. If X is written by the
1747 parent tree, WRITER is the parent.
1748 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1749 expression or its only operand forces a sequence point, then everything up
1750 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1751 in PNO_SP.
1752 Once we return, we will have emitted warnings if any subexpression before
1753 such a sequence point could be undefined. On a higher level, however, the
1754 sequence point may not be relevant, and we'll merge the two lists.
1756 Example: (b++, a) + b;
1757 The call that processes the COMPOUND_EXPR will store the increment of B
1758 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1759 processes the PLUS_EXPR will need to merge the two lists so that
1760 eventually, all accesses end up on the same list (and we'll warn about the
1761 unordered subexpressions b++ and b.
1763 A note on merging. If we modify the former example so that our expression
1764 becomes
1765 (b++, b) + a
1766 care must be taken not simply to add all three expressions into the final
1767 PNO_SP list. The function merge_tlist takes care of that by merging the
1768 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1769 way, so that no more than one access to B is recorded. */
1771 static void
1772 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1773 tree writer)
1775 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1776 enum tree_code code;
1777 enum tree_code_class cl;
1779 /* X may be NULL if it is the operand of an empty statement expression
1780 ({ }). */
1781 if (x == NULL)
1782 return;
1784 restart:
1785 code = TREE_CODE (x);
1786 cl = TREE_CODE_CLASS (code);
1788 if (warning_candidate_p (x))
1789 *pno_sp = new_tlist (*pno_sp, x, writer);
1791 switch (code)
1793 case CONSTRUCTOR:
1794 case SIZEOF_EXPR:
1795 return;
1797 case COMPOUND_EXPR:
1798 case TRUTH_ANDIF_EXPR:
1799 case TRUTH_ORIF_EXPR:
1800 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1801 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1802 warn_for_collisions (tmp_nosp);
1803 merge_tlist (pbefore_sp, tmp_before, 0);
1804 merge_tlist (pbefore_sp, tmp_nosp, 0);
1805 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1806 warn_for_collisions (tmp_list2);
1807 merge_tlist (pbefore_sp, tmp_list3, 0);
1808 merge_tlist (pno_sp, tmp_list2, 0);
1809 return;
1811 case COND_EXPR:
1812 tmp_before = tmp_list2 = 0;
1813 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1814 warn_for_collisions (tmp_list2);
1815 merge_tlist (pbefore_sp, tmp_before, 0);
1816 merge_tlist (pbefore_sp, tmp_list2, 0);
1818 tmp_list3 = tmp_nosp = 0;
1819 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1820 warn_for_collisions (tmp_nosp);
1821 merge_tlist (pbefore_sp, tmp_list3, 0);
1823 tmp_list3 = tmp_list2 = 0;
1824 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1825 warn_for_collisions (tmp_list2);
1826 merge_tlist (pbefore_sp, tmp_list3, 0);
1827 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1828 two first, to avoid warning for (a ? b++ : b++). */
1829 merge_tlist (&tmp_nosp, tmp_list2, 0);
1830 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1831 return;
1833 case PREDECREMENT_EXPR:
1834 case PREINCREMENT_EXPR:
1835 case POSTDECREMENT_EXPR:
1836 case POSTINCREMENT_EXPR:
1837 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1838 return;
1840 case MODIFY_EXPR:
1841 tmp_before = tmp_nosp = tmp_list3 = 0;
1842 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1843 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1844 /* Expressions inside the LHS are not ordered wrt. the sequence points
1845 in the RHS. Example:
1846 *a = (a++, 2)
1847 Despite the fact that the modification of "a" is in the before_sp
1848 list (tmp_before), it conflicts with the use of "a" in the LHS.
1849 We can handle this by adding the contents of tmp_list3
1850 to those of tmp_before, and redoing the collision warnings for that
1851 list. */
1852 add_tlist (&tmp_before, tmp_list3, x, 1);
1853 warn_for_collisions (tmp_before);
1854 /* Exclude the LHS itself here; we first have to merge it into the
1855 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1856 didn't exclude the LHS, we'd get it twice, once as a read and once
1857 as a write. */
1858 add_tlist (pno_sp, tmp_list3, x, 0);
1859 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1861 merge_tlist (pbefore_sp, tmp_before, 0);
1862 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1863 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1864 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1865 return;
1867 case CALL_EXPR:
1868 /* We need to warn about conflicts among arguments and conflicts between
1869 args and the function address. Side effects of the function address,
1870 however, are not ordered by the sequence point of the call. */
1872 call_expr_arg_iterator iter;
1873 tree arg;
1874 tmp_before = tmp_nosp = 0;
1875 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1876 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1878 tmp_list2 = tmp_list3 = 0;
1879 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1880 merge_tlist (&tmp_list3, tmp_list2, 0);
1881 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1883 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1884 warn_for_collisions (tmp_before);
1885 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1886 return;
1889 case TREE_LIST:
1890 /* Scan all the list, e.g. indices of multi dimensional array. */
1891 while (x)
1893 tmp_before = tmp_nosp = 0;
1894 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1895 merge_tlist (&tmp_nosp, tmp_before, 0);
1896 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1897 x = TREE_CHAIN (x);
1899 return;
1901 case SAVE_EXPR:
1903 struct tlist_cache *t;
1904 for (t = save_expr_cache; t; t = t->next)
1905 if (candidate_equal_p (t->expr, x))
1906 break;
1908 if (!t)
1910 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1911 t->next = save_expr_cache;
1912 t->expr = x;
1913 save_expr_cache = t;
1915 tmp_before = tmp_nosp = 0;
1916 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1917 warn_for_collisions (tmp_nosp);
1919 tmp_list3 = 0;
1920 merge_tlist (&tmp_list3, tmp_nosp, 0);
1921 t->cache_before_sp = tmp_before;
1922 t->cache_after_sp = tmp_list3;
1924 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1925 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1926 return;
1929 case ADDR_EXPR:
1930 x = TREE_OPERAND (x, 0);
1931 if (DECL_P (x))
1932 return;
1933 writer = 0;
1934 goto restart;
1936 default:
1937 /* For other expressions, simply recurse on their operands.
1938 Manual tail recursion for unary expressions.
1939 Other non-expressions need not be processed. */
1940 if (cl == tcc_unary)
1942 x = TREE_OPERAND (x, 0);
1943 writer = 0;
1944 goto restart;
1946 else if (IS_EXPR_CODE_CLASS (cl))
1948 int lp;
1949 int max = TREE_OPERAND_LENGTH (x);
1950 for (lp = 0; lp < max; lp++)
1952 tmp_before = tmp_nosp = 0;
1953 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1954 merge_tlist (&tmp_nosp, tmp_before, 0);
1955 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1958 return;
1962 /* Try to warn for undefined behavior in EXPR due to missing sequence
1963 points. */
1965 DEBUG_FUNCTION void
1966 verify_sequence_points (tree expr)
1968 struct tlist *before_sp = 0, *after_sp = 0;
1970 warned_ids = 0;
1971 save_expr_cache = 0;
1972 if (tlist_firstobj == 0)
1974 gcc_obstack_init (&tlist_obstack);
1975 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1978 verify_tree (expr, &before_sp, &after_sp, 0);
1979 warn_for_collisions (after_sp);
1980 obstack_free (&tlist_obstack, tlist_firstobj);
1983 /* Validate the expression after `case' and apply default promotions. */
1985 static tree
1986 check_case_value (location_t loc, tree value)
1988 if (value == NULL_TREE)
1989 return value;
1991 if (TREE_CODE (value) == INTEGER_CST)
1992 /* Promote char or short to int. */
1993 value = perform_integral_promotions (value);
1994 else if (value != error_mark_node)
1996 error_at (loc, "case label does not reduce to an integer constant");
1997 value = error_mark_node;
2000 constant_expression_warning (value);
2002 return value;
2005 /* See if the case values LOW and HIGH are in the range of the original
2006 type (i.e. before the default conversion to int) of the switch testing
2007 expression.
2008 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2009 the type before promoting it. CASE_LOW_P is a pointer to the lower
2010 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2011 if the case is not a case range.
2012 The caller has to make sure that we are not called with NULL for
2013 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
2014 was a case value that doesn't fit into the range of the ORIG_TYPE.
2015 Returns true if the case label is in range of ORIG_TYPE (saturated or
2016 untouched) or false if the label is out of range. */
2018 static bool
2019 check_case_bounds (location_t loc, tree type, tree orig_type,
2020 tree *case_low_p, tree *case_high_p,
2021 bool *outside_range_p)
2023 tree min_value, max_value;
2024 tree case_low = *case_low_p;
2025 tree case_high = case_high_p ? *case_high_p : case_low;
2027 /* If there was a problem with the original type, do nothing. */
2028 if (orig_type == error_mark_node)
2029 return true;
2031 min_value = TYPE_MIN_VALUE (orig_type);
2032 max_value = TYPE_MAX_VALUE (orig_type);
2034 /* We'll really need integer constants here. */
2035 case_low = fold (case_low);
2036 case_high = fold (case_high);
2038 /* Case label is less than minimum for type. */
2039 if (tree_int_cst_compare (case_low, min_value) < 0
2040 && tree_int_cst_compare (case_high, min_value) < 0)
2042 warning_at (loc, 0, "case label value is less than minimum value "
2043 "for type");
2044 *outside_range_p = true;
2045 return false;
2048 /* Case value is greater than maximum for type. */
2049 if (tree_int_cst_compare (case_low, max_value) > 0
2050 && tree_int_cst_compare (case_high, max_value) > 0)
2052 warning_at (loc, 0, "case label value exceeds maximum value for type");
2053 *outside_range_p = true;
2054 return false;
2057 /* Saturate lower case label value to minimum. */
2058 if (tree_int_cst_compare (case_high, min_value) >= 0
2059 && tree_int_cst_compare (case_low, min_value) < 0)
2061 warning_at (loc, 0, "lower value in case label range"
2062 " less than minimum value for type");
2063 *outside_range_p = true;
2064 case_low = min_value;
2067 /* Saturate upper case label value to maximum. */
2068 if (tree_int_cst_compare (case_low, max_value) <= 0
2069 && tree_int_cst_compare (case_high, max_value) > 0)
2071 warning_at (loc, 0, "upper value in case label range"
2072 " exceeds maximum value for type");
2073 *outside_range_p = true;
2074 case_high = max_value;
2077 if (*case_low_p != case_low)
2078 *case_low_p = convert (type, case_low);
2079 if (case_high_p && *case_high_p != case_high)
2080 *case_high_p = convert (type, case_high);
2082 return true;
2085 /* Return an integer type with BITS bits of precision,
2086 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2088 tree
2089 c_common_type_for_size (unsigned int bits, int unsignedp)
2091 int i;
2093 if (bits == TYPE_PRECISION (integer_type_node))
2094 return unsignedp ? unsigned_type_node : integer_type_node;
2096 if (bits == TYPE_PRECISION (signed_char_type_node))
2097 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2099 if (bits == TYPE_PRECISION (short_integer_type_node))
2100 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2102 if (bits == TYPE_PRECISION (long_integer_type_node))
2103 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2105 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2106 return (unsignedp ? long_long_unsigned_type_node
2107 : long_long_integer_type_node);
2109 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2110 if (int_n_enabled_p[i]
2111 && bits == int_n_data[i].bitsize)
2112 return (unsignedp ? int_n_trees[i].unsigned_type
2113 : int_n_trees[i].signed_type);
2115 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2116 return (unsignedp ? widest_unsigned_literal_type_node
2117 : widest_integer_literal_type_node);
2119 if (bits <= TYPE_PRECISION (intQI_type_node))
2120 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2122 if (bits <= TYPE_PRECISION (intHI_type_node))
2123 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2125 if (bits <= TYPE_PRECISION (intSI_type_node))
2126 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2128 if (bits <= TYPE_PRECISION (intDI_type_node))
2129 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2131 return NULL_TREE;
2134 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2135 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2136 and saturating if SATP is nonzero, otherwise not saturating. */
2138 tree
2139 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2140 int unsignedp, int satp)
2142 enum mode_class mclass;
2143 if (ibit == 0)
2144 mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2145 else
2146 mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2148 opt_scalar_mode opt_mode;
2149 scalar_mode mode;
2150 FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2152 mode = opt_mode.require ();
2153 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2154 break;
2157 if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2159 sorry ("GCC cannot support operators with integer types and "
2160 "fixed-point types that have too many integral and "
2161 "fractional bits together");
2162 return NULL_TREE;
2165 return c_common_type_for_mode (mode, satp);
2168 /* Used for communication between c_common_type_for_mode and
2169 c_register_builtin_type. */
2170 tree registered_builtin_types;
2172 /* Return a data type that has machine mode MODE.
2173 If the mode is an integer,
2174 then UNSIGNEDP selects between signed and unsigned types.
2175 If the mode is a fixed-point mode,
2176 then UNSIGNEDP selects between saturating and nonsaturating types. */
2178 tree
2179 c_common_type_for_mode (machine_mode mode, int unsignedp)
2181 tree t;
2182 int i;
2184 if (mode == TYPE_MODE (integer_type_node))
2185 return unsignedp ? unsigned_type_node : integer_type_node;
2187 if (mode == TYPE_MODE (signed_char_type_node))
2188 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2190 if (mode == TYPE_MODE (short_integer_type_node))
2191 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2193 if (mode == TYPE_MODE (long_integer_type_node))
2194 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2196 if (mode == TYPE_MODE (long_long_integer_type_node))
2197 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2199 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2200 if (int_n_enabled_p[i]
2201 && mode == int_n_data[i].m)
2202 return (unsignedp ? int_n_trees[i].unsigned_type
2203 : int_n_trees[i].signed_type);
2205 if (mode == QImode)
2206 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2208 if (mode == HImode)
2209 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2211 if (mode == SImode)
2212 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2214 if (mode == DImode)
2215 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2217 #if HOST_BITS_PER_WIDE_INT >= 64
2218 if (mode == TYPE_MODE (intTI_type_node))
2219 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2220 #endif
2222 if (mode == TYPE_MODE (float_type_node))
2223 return float_type_node;
2225 if (mode == TYPE_MODE (double_type_node))
2226 return double_type_node;
2228 if (mode == TYPE_MODE (long_double_type_node))
2229 return long_double_type_node;
2231 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2232 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2233 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2234 return FLOATN_NX_TYPE_NODE (i);
2236 if (mode == TYPE_MODE (void_type_node))
2237 return void_type_node;
2239 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2240 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2242 unsigned int precision
2243 = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2244 return (unsignedp
2245 ? make_unsigned_type (precision)
2246 : make_signed_type (precision));
2249 if (COMPLEX_MODE_P (mode))
2251 machine_mode inner_mode;
2252 tree inner_type;
2254 if (mode == TYPE_MODE (complex_float_type_node))
2255 return complex_float_type_node;
2256 if (mode == TYPE_MODE (complex_double_type_node))
2257 return complex_double_type_node;
2258 if (mode == TYPE_MODE (complex_long_double_type_node))
2259 return complex_long_double_type_node;
2261 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2262 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2263 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2264 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2266 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2267 return complex_integer_type_node;
2269 inner_mode = GET_MODE_INNER (mode);
2270 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2271 if (inner_type != NULL_TREE)
2272 return build_complex_type (inner_type);
2274 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2275 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2277 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
2278 GET_MODE_NUNITS (mode));
2279 tree bool_type = build_nonstandard_boolean_type (elem_bits);
2280 return build_vector_type_for_mode (bool_type, mode);
2282 else if (VECTOR_MODE_P (mode)
2283 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2285 machine_mode inner_mode = GET_MODE_INNER (mode);
2286 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2287 if (inner_type != NULL_TREE)
2288 return build_vector_type_for_mode (inner_type, mode);
2291 if (mode == TYPE_MODE (dfloat32_type_node))
2292 return dfloat32_type_node;
2293 if (mode == TYPE_MODE (dfloat64_type_node))
2294 return dfloat64_type_node;
2295 if (mode == TYPE_MODE (dfloat128_type_node))
2296 return dfloat128_type_node;
2298 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2300 if (mode == TYPE_MODE (short_fract_type_node))
2301 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2302 if (mode == TYPE_MODE (fract_type_node))
2303 return unsignedp ? sat_fract_type_node : fract_type_node;
2304 if (mode == TYPE_MODE (long_fract_type_node))
2305 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2306 if (mode == TYPE_MODE (long_long_fract_type_node))
2307 return unsignedp ? sat_long_long_fract_type_node
2308 : long_long_fract_type_node;
2310 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2311 return unsignedp ? sat_unsigned_short_fract_type_node
2312 : unsigned_short_fract_type_node;
2313 if (mode == TYPE_MODE (unsigned_fract_type_node))
2314 return unsignedp ? sat_unsigned_fract_type_node
2315 : unsigned_fract_type_node;
2316 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2317 return unsignedp ? sat_unsigned_long_fract_type_node
2318 : unsigned_long_fract_type_node;
2319 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2320 return unsignedp ? sat_unsigned_long_long_fract_type_node
2321 : unsigned_long_long_fract_type_node;
2323 if (mode == TYPE_MODE (short_accum_type_node))
2324 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2325 if (mode == TYPE_MODE (accum_type_node))
2326 return unsignedp ? sat_accum_type_node : accum_type_node;
2327 if (mode == TYPE_MODE (long_accum_type_node))
2328 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2329 if (mode == TYPE_MODE (long_long_accum_type_node))
2330 return unsignedp ? sat_long_long_accum_type_node
2331 : long_long_accum_type_node;
2333 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2334 return unsignedp ? sat_unsigned_short_accum_type_node
2335 : unsigned_short_accum_type_node;
2336 if (mode == TYPE_MODE (unsigned_accum_type_node))
2337 return unsignedp ? sat_unsigned_accum_type_node
2338 : unsigned_accum_type_node;
2339 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2340 return unsignedp ? sat_unsigned_long_accum_type_node
2341 : unsigned_long_accum_type_node;
2342 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2343 return unsignedp ? sat_unsigned_long_long_accum_type_node
2344 : unsigned_long_long_accum_type_node;
2346 if (mode == QQmode)
2347 return unsignedp ? sat_qq_type_node : qq_type_node;
2348 if (mode == HQmode)
2349 return unsignedp ? sat_hq_type_node : hq_type_node;
2350 if (mode == SQmode)
2351 return unsignedp ? sat_sq_type_node : sq_type_node;
2352 if (mode == DQmode)
2353 return unsignedp ? sat_dq_type_node : dq_type_node;
2354 if (mode == TQmode)
2355 return unsignedp ? sat_tq_type_node : tq_type_node;
2357 if (mode == UQQmode)
2358 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2359 if (mode == UHQmode)
2360 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2361 if (mode == USQmode)
2362 return unsignedp ? sat_usq_type_node : usq_type_node;
2363 if (mode == UDQmode)
2364 return unsignedp ? sat_udq_type_node : udq_type_node;
2365 if (mode == UTQmode)
2366 return unsignedp ? sat_utq_type_node : utq_type_node;
2368 if (mode == HAmode)
2369 return unsignedp ? sat_ha_type_node : ha_type_node;
2370 if (mode == SAmode)
2371 return unsignedp ? sat_sa_type_node : sa_type_node;
2372 if (mode == DAmode)
2373 return unsignedp ? sat_da_type_node : da_type_node;
2374 if (mode == TAmode)
2375 return unsignedp ? sat_ta_type_node : ta_type_node;
2377 if (mode == UHAmode)
2378 return unsignedp ? sat_uha_type_node : uha_type_node;
2379 if (mode == USAmode)
2380 return unsignedp ? sat_usa_type_node : usa_type_node;
2381 if (mode == UDAmode)
2382 return unsignedp ? sat_uda_type_node : uda_type_node;
2383 if (mode == UTAmode)
2384 return unsignedp ? sat_uta_type_node : uta_type_node;
2387 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2388 if (TYPE_MODE (TREE_VALUE (t)) == mode
2389 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2390 return TREE_VALUE (t);
2392 return NULL_TREE;
2395 tree
2396 c_common_unsigned_type (tree type)
2398 return c_common_signed_or_unsigned_type (1, type);
2401 /* Return a signed type the same as TYPE in other respects. */
2403 tree
2404 c_common_signed_type (tree type)
2406 return c_common_signed_or_unsigned_type (0, type);
2409 /* Return a type the same as TYPE except unsigned or
2410 signed according to UNSIGNEDP. */
2412 tree
2413 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2415 tree type1;
2416 int i;
2418 /* This block of code emulates the behavior of the old
2419 c_common_unsigned_type. In particular, it returns
2420 long_unsigned_type_node if passed a long, even when a int would
2421 have the same size. This is necessary for warnings to work
2422 correctly in archs where sizeof(int) == sizeof(long) */
2424 type1 = TYPE_MAIN_VARIANT (type);
2425 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2426 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2427 if (type1 == integer_type_node || type1 == unsigned_type_node)
2428 return unsignedp ? unsigned_type_node : integer_type_node;
2429 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2430 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2431 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2432 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2433 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2434 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2436 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2437 if (int_n_enabled_p[i]
2438 && (type1 == int_n_trees[i].unsigned_type
2439 || type1 == int_n_trees[i].signed_type))
2440 return (unsignedp ? int_n_trees[i].unsigned_type
2441 : int_n_trees[i].signed_type);
2443 #if HOST_BITS_PER_WIDE_INT >= 64
2444 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2445 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2446 #endif
2447 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2448 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2449 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2450 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2451 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2452 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2453 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2454 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2456 #define C_COMMON_FIXED_TYPES(NAME) \
2457 if (type1 == short_ ## NAME ## _type_node \
2458 || type1 == unsigned_short_ ## NAME ## _type_node) \
2459 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2460 : short_ ## NAME ## _type_node; \
2461 if (type1 == NAME ## _type_node \
2462 || type1 == unsigned_ ## NAME ## _type_node) \
2463 return unsignedp ? unsigned_ ## NAME ## _type_node \
2464 : NAME ## _type_node; \
2465 if (type1 == long_ ## NAME ## _type_node \
2466 || type1 == unsigned_long_ ## NAME ## _type_node) \
2467 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2468 : long_ ## NAME ## _type_node; \
2469 if (type1 == long_long_ ## NAME ## _type_node \
2470 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2471 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2472 : long_long_ ## NAME ## _type_node;
2474 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2475 if (type1 == NAME ## _type_node \
2476 || type1 == u ## NAME ## _type_node) \
2477 return unsignedp ? u ## NAME ## _type_node \
2478 : NAME ## _type_node;
2480 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2481 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2482 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2483 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2484 : sat_ ## short_ ## NAME ## _type_node; \
2485 if (type1 == sat_ ## NAME ## _type_node \
2486 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2487 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2488 : sat_ ## NAME ## _type_node; \
2489 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2490 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2491 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2492 : sat_ ## long_ ## NAME ## _type_node; \
2493 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2494 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2495 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2496 : sat_ ## long_long_ ## NAME ## _type_node;
2498 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2499 if (type1 == sat_ ## NAME ## _type_node \
2500 || type1 == sat_ ## u ## NAME ## _type_node) \
2501 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2502 : sat_ ## NAME ## _type_node;
2504 C_COMMON_FIXED_TYPES (fract);
2505 C_COMMON_FIXED_TYPES_SAT (fract);
2506 C_COMMON_FIXED_TYPES (accum);
2507 C_COMMON_FIXED_TYPES_SAT (accum);
2509 C_COMMON_FIXED_MODE_TYPES (qq);
2510 C_COMMON_FIXED_MODE_TYPES (hq);
2511 C_COMMON_FIXED_MODE_TYPES (sq);
2512 C_COMMON_FIXED_MODE_TYPES (dq);
2513 C_COMMON_FIXED_MODE_TYPES (tq);
2514 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2515 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2516 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2517 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2518 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2519 C_COMMON_FIXED_MODE_TYPES (ha);
2520 C_COMMON_FIXED_MODE_TYPES (sa);
2521 C_COMMON_FIXED_MODE_TYPES (da);
2522 C_COMMON_FIXED_MODE_TYPES (ta);
2523 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2524 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2525 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2526 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2528 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2529 the precision; they have precision set to match their range, but
2530 may use a wider mode to match an ABI. If we change modes, we may
2531 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2532 the precision as well, so as to yield correct results for
2533 bit-field types. C++ does not have these separate bit-field
2534 types, and producing a signed or unsigned variant of an
2535 ENUMERAL_TYPE may cause other problems as well. */
2537 if (!INTEGRAL_TYPE_P (type)
2538 || TYPE_UNSIGNED (type) == unsignedp)
2539 return type;
2541 #define TYPE_OK(node) \
2542 (TYPE_MODE (type) == TYPE_MODE (node) \
2543 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2544 if (TYPE_OK (signed_char_type_node))
2545 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2546 if (TYPE_OK (integer_type_node))
2547 return unsignedp ? unsigned_type_node : integer_type_node;
2548 if (TYPE_OK (short_integer_type_node))
2549 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2550 if (TYPE_OK (long_integer_type_node))
2551 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2552 if (TYPE_OK (long_long_integer_type_node))
2553 return (unsignedp ? long_long_unsigned_type_node
2554 : long_long_integer_type_node);
2556 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2557 if (int_n_enabled_p[i]
2558 && TYPE_MODE (type) == int_n_data[i].m
2559 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2560 return (unsignedp ? int_n_trees[i].unsigned_type
2561 : int_n_trees[i].signed_type);
2563 #if HOST_BITS_PER_WIDE_INT >= 64
2564 if (TYPE_OK (intTI_type_node))
2565 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2566 #endif
2567 if (TYPE_OK (intDI_type_node))
2568 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2569 if (TYPE_OK (intSI_type_node))
2570 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2571 if (TYPE_OK (intHI_type_node))
2572 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2573 if (TYPE_OK (intQI_type_node))
2574 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2575 #undef TYPE_OK
2577 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2580 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2582 tree
2583 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2585 int i;
2587 /* Extended integer types of the same width as a standard type have
2588 lesser rank, so those of the same width as int promote to int or
2589 unsigned int and are valid for printf formats expecting int or
2590 unsigned int. To avoid such special cases, avoid creating
2591 extended integer types for bit-fields if a standard integer type
2592 is available. */
2593 if (width == TYPE_PRECISION (integer_type_node))
2594 return unsignedp ? unsigned_type_node : integer_type_node;
2595 if (width == TYPE_PRECISION (signed_char_type_node))
2596 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2597 if (width == TYPE_PRECISION (short_integer_type_node))
2598 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2599 if (width == TYPE_PRECISION (long_integer_type_node))
2600 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2601 if (width == TYPE_PRECISION (long_long_integer_type_node))
2602 return (unsignedp ? long_long_unsigned_type_node
2603 : long_long_integer_type_node);
2604 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2605 if (int_n_enabled_p[i]
2606 && width == int_n_data[i].bitsize)
2607 return (unsignedp ? int_n_trees[i].unsigned_type
2608 : int_n_trees[i].signed_type);
2609 return build_nonstandard_integer_type (width, unsignedp);
2612 /* The C version of the register_builtin_type langhook. */
2614 void
2615 c_register_builtin_type (tree type, const char* name)
2617 tree decl;
2619 decl = build_decl (UNKNOWN_LOCATION,
2620 TYPE_DECL, get_identifier (name), type);
2621 DECL_ARTIFICIAL (decl) = 1;
2622 if (!TYPE_NAME (type))
2623 TYPE_NAME (type) = decl;
2624 lang_hooks.decls.pushdecl (decl);
2626 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2629 /* Print an error message for invalid operands to arith operation
2630 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2631 RICHLOC is a rich location for the message, containing either
2632 three separate locations for each of the operator and operands
2634 lhs op rhs
2635 ~~~ ^~ ~~~
2637 (C FE), or one location ranging over all over them
2639 lhs op rhs
2640 ~~~~^~~~~~
2642 (C++ FE). */
2644 void
2645 binary_op_error (rich_location *richloc, enum tree_code code,
2646 tree type0, tree type1)
2648 const char *opname;
2650 switch (code)
2652 case PLUS_EXPR:
2653 opname = "+"; break;
2654 case MINUS_EXPR:
2655 opname = "-"; break;
2656 case MULT_EXPR:
2657 opname = "*"; break;
2658 case MAX_EXPR:
2659 opname = "max"; break;
2660 case MIN_EXPR:
2661 opname = "min"; break;
2662 case EQ_EXPR:
2663 opname = "=="; break;
2664 case NE_EXPR:
2665 opname = "!="; break;
2666 case LE_EXPR:
2667 opname = "<="; break;
2668 case GE_EXPR:
2669 opname = ">="; break;
2670 case LT_EXPR:
2671 opname = "<"; break;
2672 case GT_EXPR:
2673 opname = ">"; break;
2674 case LSHIFT_EXPR:
2675 opname = "<<"; break;
2676 case RSHIFT_EXPR:
2677 opname = ">>"; break;
2678 case TRUNC_MOD_EXPR:
2679 case FLOOR_MOD_EXPR:
2680 opname = "%"; break;
2681 case TRUNC_DIV_EXPR:
2682 case FLOOR_DIV_EXPR:
2683 opname = "/"; break;
2684 case BIT_AND_EXPR:
2685 opname = "&"; break;
2686 case BIT_IOR_EXPR:
2687 opname = "|"; break;
2688 case TRUTH_ANDIF_EXPR:
2689 opname = "&&"; break;
2690 case TRUTH_ORIF_EXPR:
2691 opname = "||"; break;
2692 case BIT_XOR_EXPR:
2693 opname = "^"; break;
2694 default:
2695 gcc_unreachable ();
2697 error_at (richloc,
2698 "invalid operands to binary %s (have %qT and %qT)",
2699 opname, type0, type1);
2702 /* Given an expression as a tree, return its original type. Do this
2703 by stripping any conversion that preserves the sign and precision. */
2704 static tree
2705 expr_original_type (tree expr)
2707 STRIP_SIGN_NOPS (expr);
2708 return TREE_TYPE (expr);
2711 /* Subroutine of build_binary_op, used for comparison operations.
2712 See if the operands have both been converted from subword integer types
2713 and, if so, perhaps change them both back to their original type.
2714 This function is also responsible for converting the two operands
2715 to the proper common type for comparison.
2717 The arguments of this function are all pointers to local variables
2718 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2719 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2721 LOC is the location of the comparison.
2723 If this function returns non-NULL_TREE, it means that the comparison has
2724 a constant value. What this function returns is an expression for
2725 that value. */
2727 tree
2728 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2729 tree *restype_ptr, enum tree_code *rescode_ptr)
2731 tree type;
2732 tree op0 = *op0_ptr;
2733 tree op1 = *op1_ptr;
2734 int unsignedp0, unsignedp1;
2735 int real1, real2;
2736 tree primop0, primop1;
2737 enum tree_code code = *rescode_ptr;
2739 /* Throw away any conversions to wider types
2740 already present in the operands. */
2742 primop0 = c_common_get_narrower (op0, &unsignedp0);
2743 primop1 = c_common_get_narrower (op1, &unsignedp1);
2745 /* If primopN is first sign-extended from primopN's precision to opN's
2746 precision, then zero-extended from opN's precision to
2747 *restype_ptr precision, shortenings might be invalid. */
2748 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2749 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2750 && !unsignedp0
2751 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2752 primop0 = op0;
2753 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2754 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2755 && !unsignedp1
2756 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2757 primop1 = op1;
2759 /* Handle the case that OP0 does not *contain* a conversion
2760 but it *requires* conversion to FINAL_TYPE. */
2762 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2763 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2764 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2765 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2767 /* If one of the operands must be floated, we cannot optimize. */
2768 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2769 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2771 /* If first arg is constant, swap the args (changing operation
2772 so value is preserved), for canonicalization. Don't do this if
2773 the second arg is 0. */
2775 if (TREE_CONSTANT (primop0)
2776 && !integer_zerop (primop1) && !real_zerop (primop1)
2777 && !fixed_zerop (primop1))
2779 std::swap (primop0, primop1);
2780 std::swap (op0, op1);
2781 *op0_ptr = op0;
2782 *op1_ptr = op1;
2783 std::swap (unsignedp0, unsignedp1);
2784 std::swap (real1, real2);
2786 switch (code)
2788 case LT_EXPR:
2789 code = GT_EXPR;
2790 break;
2791 case GT_EXPR:
2792 code = LT_EXPR;
2793 break;
2794 case LE_EXPR:
2795 code = GE_EXPR;
2796 break;
2797 case GE_EXPR:
2798 code = LE_EXPR;
2799 break;
2800 default:
2801 break;
2803 *rescode_ptr = code;
2806 /* If comparing an integer against a constant more bits wide,
2807 maybe we can deduce a value of 1 or 0 independent of the data.
2808 Or else truncate the constant now
2809 rather than extend the variable at run time.
2811 This is only interesting if the constant is the wider arg.
2812 Also, it is not safe if the constant is unsigned and the
2813 variable arg is signed, since in this case the variable
2814 would be sign-extended and then regarded as unsigned.
2815 Our technique fails in this case because the lowest/highest
2816 possible unsigned results don't follow naturally from the
2817 lowest/highest possible values of the variable operand.
2818 For just EQ_EXPR and NE_EXPR there is another technique that
2819 could be used: see if the constant can be faithfully represented
2820 in the other operand's type, by truncating it and reextending it
2821 and see if that preserves the constant's value. */
2823 if (!real1 && !real2
2824 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2825 && TREE_CODE (primop1) == INTEGER_CST
2826 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2828 int min_gt, max_gt, min_lt, max_lt;
2829 tree maxval, minval;
2830 /* 1 if comparison is nominally unsigned. */
2831 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2832 tree val;
2834 type = c_common_signed_or_unsigned_type (unsignedp0,
2835 TREE_TYPE (primop0));
2837 maxval = TYPE_MAX_VALUE (type);
2838 minval = TYPE_MIN_VALUE (type);
2840 if (unsignedp && !unsignedp0)
2841 *restype_ptr = c_common_signed_type (*restype_ptr);
2843 if (TREE_TYPE (primop1) != *restype_ptr)
2845 /* Convert primop1 to target type, but do not introduce
2846 additional overflow. We know primop1 is an int_cst. */
2847 primop1 = force_fit_type (*restype_ptr,
2848 wi::to_wide
2849 (primop1,
2850 TYPE_PRECISION (*restype_ptr)),
2851 0, TREE_OVERFLOW (primop1));
2853 if (type != *restype_ptr)
2855 minval = convert (*restype_ptr, minval);
2856 maxval = convert (*restype_ptr, maxval);
2859 min_gt = tree_int_cst_lt (primop1, minval);
2860 max_gt = tree_int_cst_lt (primop1, maxval);
2861 min_lt = tree_int_cst_lt (minval, primop1);
2862 max_lt = tree_int_cst_lt (maxval, primop1);
2864 val = 0;
2865 /* This used to be a switch, but Genix compiler can't handle that. */
2866 if (code == NE_EXPR)
2868 if (max_lt || min_gt)
2869 val = truthvalue_true_node;
2871 else if (code == EQ_EXPR)
2873 if (max_lt || min_gt)
2874 val = truthvalue_false_node;
2876 else if (code == LT_EXPR)
2878 if (max_lt)
2879 val = truthvalue_true_node;
2880 if (!min_lt)
2881 val = truthvalue_false_node;
2883 else if (code == GT_EXPR)
2885 if (min_gt)
2886 val = truthvalue_true_node;
2887 if (!max_gt)
2888 val = truthvalue_false_node;
2890 else if (code == LE_EXPR)
2892 if (!max_gt)
2893 val = truthvalue_true_node;
2894 if (min_gt)
2895 val = truthvalue_false_node;
2897 else if (code == GE_EXPR)
2899 if (!min_lt)
2900 val = truthvalue_true_node;
2901 if (max_lt)
2902 val = truthvalue_false_node;
2905 /* If primop0 was sign-extended and unsigned comparison specd,
2906 we did a signed comparison above using the signed type bounds.
2907 But the comparison we output must be unsigned.
2909 Also, for inequalities, VAL is no good; but if the signed
2910 comparison had *any* fixed result, it follows that the
2911 unsigned comparison just tests the sign in reverse
2912 (positive values are LE, negative ones GE).
2913 So we can generate an unsigned comparison
2914 against an extreme value of the signed type. */
2916 if (unsignedp && !unsignedp0)
2918 if (val != 0)
2919 switch (code)
2921 case LT_EXPR:
2922 case GE_EXPR:
2923 primop1 = TYPE_MIN_VALUE (type);
2924 val = 0;
2925 break;
2927 case LE_EXPR:
2928 case GT_EXPR:
2929 primop1 = TYPE_MAX_VALUE (type);
2930 val = 0;
2931 break;
2933 default:
2934 break;
2936 type = c_common_unsigned_type (type);
2939 if (TREE_CODE (primop0) != INTEGER_CST
2940 /* Don't warn if it's from a (non-system) macro. */
2941 && !(from_macro_expansion_at
2942 (expansion_point_location_if_in_system_header
2943 (EXPR_LOCATION (primop0)))))
2945 if (val == truthvalue_false_node)
2946 warning_at (loc, OPT_Wtype_limits,
2947 "comparison is always false due to limited range of data type");
2948 if (val == truthvalue_true_node)
2949 warning_at (loc, OPT_Wtype_limits,
2950 "comparison is always true due to limited range of data type");
2953 if (val != 0)
2955 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2956 if (TREE_SIDE_EFFECTS (primop0))
2957 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2958 return val;
2961 /* Value is not predetermined, but do the comparison
2962 in the type of the operand that is not constant.
2963 TYPE is already properly set. */
2966 /* If either arg is decimal float and the other is float, find the
2967 proper common type to use for comparison. */
2968 else if (real1 && real2
2969 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2970 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2971 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2973 /* If either arg is decimal float and the other is float, fail. */
2974 else if (real1 && real2
2975 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2976 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2977 return NULL_TREE;
2979 else if (real1 && real2
2980 && (TYPE_PRECISION (TREE_TYPE (primop0))
2981 == TYPE_PRECISION (TREE_TYPE (primop1))))
2982 type = TREE_TYPE (primop0);
2984 /* If args' natural types are both narrower than nominal type
2985 and both extend in the same manner, compare them
2986 in the type of the wider arg.
2987 Otherwise must actually extend both to the nominal
2988 common type lest different ways of extending
2989 alter the result.
2990 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2992 else if (unsignedp0 == unsignedp1 && real1 == real2
2993 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2994 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2996 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2997 type = c_common_signed_or_unsigned_type (unsignedp0
2998 || TYPE_UNSIGNED (*restype_ptr),
2999 type);
3000 /* Make sure shorter operand is extended the right way
3001 to match the longer operand. */
3002 primop0
3003 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3004 TREE_TYPE (primop0)),
3005 primop0);
3006 primop1
3007 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3008 TREE_TYPE (primop1)),
3009 primop1);
3011 else
3013 /* Here we must do the comparison on the nominal type
3014 using the args exactly as we received them. */
3015 type = *restype_ptr;
3016 primop0 = op0;
3017 primop1 = op1;
3019 if (!real1 && !real2 && integer_zerop (primop1)
3020 && TYPE_UNSIGNED (*restype_ptr))
3022 tree value = NULL_TREE;
3023 /* All unsigned values are >= 0, so we warn. However,
3024 if OP0 is a constant that is >= 0, the signedness of
3025 the comparison isn't an issue, so suppress the
3026 warning. */
3027 bool warn =
3028 warn_type_limits && !in_system_header_at (loc)
3029 && !(TREE_CODE (primop0) == INTEGER_CST
3030 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3031 primop0)))
3032 /* Do not warn for enumeration types. */
3033 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3035 switch (code)
3037 case GE_EXPR:
3038 if (warn)
3039 warning_at (loc, OPT_Wtype_limits,
3040 "comparison of unsigned expression >= 0 is always true");
3041 value = truthvalue_true_node;
3042 break;
3044 case LT_EXPR:
3045 if (warn)
3046 warning_at (loc, OPT_Wtype_limits,
3047 "comparison of unsigned expression < 0 is always false");
3048 value = truthvalue_false_node;
3049 break;
3051 default:
3052 break;
3055 if (value != NULL_TREE)
3057 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3058 if (TREE_SIDE_EFFECTS (primop0))
3059 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3060 primop0, value);
3061 return value;
3066 *op0_ptr = convert (type, primop0);
3067 *op1_ptr = convert (type, primop1);
3069 *restype_ptr = truthvalue_type_node;
3071 return NULL_TREE;
3074 /* Return a tree for the sum or difference (RESULTCODE says which)
3075 of pointer PTROP and integer INTOP. */
3077 tree
3078 pointer_int_sum (location_t loc, enum tree_code resultcode,
3079 tree ptrop, tree intop, bool complain)
3081 tree size_exp, ret;
3083 /* The result is a pointer of the same type that is being added. */
3084 tree result_type = TREE_TYPE (ptrop);
3086 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3088 if (complain && warn_pointer_arith)
3089 pedwarn (loc, OPT_Wpointer_arith,
3090 "pointer of type %<void *%> used in arithmetic");
3091 else if (!complain)
3092 return error_mark_node;
3093 size_exp = integer_one_node;
3095 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3097 if (complain && warn_pointer_arith)
3098 pedwarn (loc, OPT_Wpointer_arith,
3099 "pointer to a function used in arithmetic");
3100 else if (!complain)
3101 return error_mark_node;
3102 size_exp = integer_one_node;
3104 else
3105 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3107 /* We are manipulating pointer values, so we don't need to warn
3108 about relying on undefined signed overflow. We disable the
3109 warning here because we use integer types so fold won't know that
3110 they are really pointers. */
3111 fold_defer_overflow_warnings ();
3113 /* If what we are about to multiply by the size of the elements
3114 contains a constant term, apply distributive law
3115 and multiply that constant term separately.
3116 This helps produce common subexpressions. */
3117 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3118 && !TREE_CONSTANT (intop)
3119 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3120 && TREE_CONSTANT (size_exp)
3121 /* If the constant comes from pointer subtraction,
3122 skip this optimization--it would cause an error. */
3123 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3124 /* If the constant is unsigned, and smaller than the pointer size,
3125 then we must skip this optimization. This is because it could cause
3126 an overflow error if the constant is negative but INTOP is not. */
3127 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3128 || (TYPE_PRECISION (TREE_TYPE (intop))
3129 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3131 enum tree_code subcode = resultcode;
3132 tree int_type = TREE_TYPE (intop);
3133 if (TREE_CODE (intop) == MINUS_EXPR)
3134 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3135 /* Convert both subexpression types to the type of intop,
3136 because weird cases involving pointer arithmetic
3137 can result in a sum or difference with different type args. */
3138 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3139 subcode, ptrop,
3140 convert (int_type, TREE_OPERAND (intop, 1)),
3141 true);
3142 intop = convert (int_type, TREE_OPERAND (intop, 0));
3145 /* Convert the integer argument to a type the same size as sizetype
3146 so the multiply won't overflow spuriously. */
3147 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3148 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3149 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3150 TYPE_UNSIGNED (sizetype)), intop);
3152 /* Replace the integer argument with a suitable product by the object size.
3153 Do this multiplication as signed, then convert to the appropriate type
3154 for the pointer operation and disregard an overflow that occurred only
3155 because of the sign-extension change in the latter conversion. */
3157 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3158 convert (TREE_TYPE (intop), size_exp));
3159 intop = convert (sizetype, t);
3160 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3161 intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3164 /* Create the sum or difference. */
3165 if (resultcode == MINUS_EXPR)
3166 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3168 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3170 fold_undefer_and_ignore_overflow_warnings ();
3172 return ret;
3175 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3176 and if NON_CONST is known not to be permitted in an evaluated part
3177 of a constant expression. */
3179 tree
3180 c_wrap_maybe_const (tree expr, bool non_const)
3182 bool nowarning = TREE_NO_WARNING (expr);
3183 location_t loc = EXPR_LOCATION (expr);
3185 /* This should never be called for C++. */
3186 if (c_dialect_cxx ())
3187 gcc_unreachable ();
3189 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3190 STRIP_TYPE_NOPS (expr);
3191 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3192 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3193 if (nowarning)
3194 TREE_NO_WARNING (expr) = 1;
3195 protected_set_expr_location (expr, loc);
3197 return expr;
3200 /* Return whether EXPR is a declaration whose address can never be
3201 NULL. */
3203 bool
3204 decl_with_nonnull_addr_p (const_tree expr)
3206 return (DECL_P (expr)
3207 && (TREE_CODE (expr) == PARM_DECL
3208 || TREE_CODE (expr) == LABEL_DECL
3209 || !DECL_WEAK (expr)));
3212 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3213 or for an `if' or `while' statement or ?..: exp. It should already
3214 have been validated to be of suitable type; otherwise, a bad
3215 diagnostic may result.
3217 The EXPR is located at LOCATION.
3219 This preparation consists of taking the ordinary
3220 representation of an expression expr and producing a valid tree
3221 boolean expression describing whether expr is nonzero. We could
3222 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3223 but we optimize comparisons, &&, ||, and !.
3225 The resulting type should always be `truthvalue_type_node'. */
3227 tree
3228 c_common_truthvalue_conversion (location_t location, tree expr)
3230 switch (TREE_CODE (expr))
3232 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3233 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3234 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3235 case ORDERED_EXPR: case UNORDERED_EXPR:
3236 if (TREE_TYPE (expr) == truthvalue_type_node)
3237 return expr;
3238 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3239 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3240 goto ret;
3242 case TRUTH_ANDIF_EXPR:
3243 case TRUTH_ORIF_EXPR:
3244 case TRUTH_AND_EXPR:
3245 case TRUTH_OR_EXPR:
3246 case TRUTH_XOR_EXPR:
3247 if (TREE_TYPE (expr) == truthvalue_type_node)
3248 return expr;
3249 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3250 c_common_truthvalue_conversion (location,
3251 TREE_OPERAND (expr, 0)),
3252 c_common_truthvalue_conversion (location,
3253 TREE_OPERAND (expr, 1)));
3254 goto ret;
3256 case TRUTH_NOT_EXPR:
3257 if (TREE_TYPE (expr) == truthvalue_type_node)
3258 return expr;
3259 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3260 c_common_truthvalue_conversion (location,
3261 TREE_OPERAND (expr, 0)));
3262 goto ret;
3264 case ERROR_MARK:
3265 return expr;
3267 case INTEGER_CST:
3268 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3269 && !integer_zerop (expr)
3270 && !integer_onep (expr))
3271 warning_at (location, OPT_Wint_in_bool_context,
3272 "enum constant in boolean context");
3273 return integer_zerop (expr) ? truthvalue_false_node
3274 : truthvalue_true_node;
3276 case REAL_CST:
3277 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3278 ? truthvalue_true_node
3279 : truthvalue_false_node;
3281 case FIXED_CST:
3282 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3283 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3284 ? truthvalue_true_node
3285 : truthvalue_false_node;
3287 case FUNCTION_DECL:
3288 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3289 /* Fall through. */
3291 case ADDR_EXPR:
3293 tree inner = TREE_OPERAND (expr, 0);
3294 if (decl_with_nonnull_addr_p (inner))
3296 /* Common Ada programmer's mistake. */
3297 warning_at (location,
3298 OPT_Waddress,
3299 "the address of %qD will always evaluate as %<true%>",
3300 inner);
3301 return truthvalue_true_node;
3303 break;
3306 case COMPLEX_EXPR:
3307 expr = build_binary_op (EXPR_LOCATION (expr),
3308 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3309 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3310 c_common_truthvalue_conversion (location,
3311 TREE_OPERAND (expr, 0)),
3312 c_common_truthvalue_conversion (location,
3313 TREE_OPERAND (expr, 1)),
3314 false);
3315 goto ret;
3317 case NEGATE_EXPR:
3318 case ABS_EXPR:
3319 case ABSU_EXPR:
3320 case FLOAT_EXPR:
3321 case EXCESS_PRECISION_EXPR:
3322 /* These don't change whether an object is nonzero or zero. */
3323 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3325 case LROTATE_EXPR:
3326 case RROTATE_EXPR:
3327 /* These don't change whether an object is zero or nonzero, but
3328 we can't ignore them if their second arg has side-effects. */
3329 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3331 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3332 TREE_OPERAND (expr, 1),
3333 c_common_truthvalue_conversion
3334 (location, TREE_OPERAND (expr, 0)));
3335 goto ret;
3337 else
3338 return c_common_truthvalue_conversion (location,
3339 TREE_OPERAND (expr, 0));
3341 case MULT_EXPR:
3342 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3343 "%<*%> in boolean context, suggest %<&&%> instead");
3344 break;
3346 case LSHIFT_EXPR:
3347 /* We will only warn on signed shifts here, because the majority of
3348 false positive warnings happen in code where unsigned arithmetic
3349 was used in anticipation of a possible overflow.
3350 Furthermore, if we see an unsigned type here we know that the
3351 result of the shift is not subject to integer promotion rules. */
3352 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3353 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3354 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3355 "%<<<%> in boolean context, did you mean %<<%> ?");
3356 break;
3358 case COND_EXPR:
3359 if (warn_int_in_bool_context
3360 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3362 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3363 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3364 if (TREE_CODE (val1) == INTEGER_CST
3365 && TREE_CODE (val2) == INTEGER_CST
3366 && !integer_zerop (val1)
3367 && !integer_zerop (val2)
3368 && (!integer_onep (val1)
3369 || !integer_onep (val2)))
3370 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3371 "?: using integer constants in boolean context, "
3372 "the expression will always evaluate to %<true%>");
3373 else if ((TREE_CODE (val1) == INTEGER_CST
3374 && !integer_zerop (val1)
3375 && !integer_onep (val1))
3376 || (TREE_CODE (val2) == INTEGER_CST
3377 && !integer_zerop (val2)
3378 && !integer_onep (val2)))
3379 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3380 "?: using integer constants in boolean context");
3382 /* Distribute the conversion into the arms of a COND_EXPR. */
3383 if (c_dialect_cxx ())
3384 /* Avoid premature folding. */
3385 break;
3386 else
3388 int w = warn_int_in_bool_context;
3389 warn_int_in_bool_context = 0;
3390 /* Folding will happen later for C. */
3391 expr = build3 (COND_EXPR, truthvalue_type_node,
3392 TREE_OPERAND (expr, 0),
3393 c_common_truthvalue_conversion (location,
3394 TREE_OPERAND (expr, 1)),
3395 c_common_truthvalue_conversion (location,
3396 TREE_OPERAND (expr, 2)));
3397 warn_int_in_bool_context = w;
3398 goto ret;
3401 CASE_CONVERT:
3403 tree totype = TREE_TYPE (expr);
3404 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3406 if (POINTER_TYPE_P (totype)
3407 && !c_inhibit_evaluation_warnings
3408 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3410 tree inner = expr;
3411 STRIP_NOPS (inner);
3413 if (DECL_P (inner))
3414 warning_at (location,
3415 OPT_Waddress,
3416 "the compiler can assume that the address of "
3417 "%qD will always evaluate to %<true%>",
3418 inner);
3421 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3422 since that affects how `default_conversion' will behave. */
3423 if (TREE_CODE (totype) == REFERENCE_TYPE
3424 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3425 break;
3426 /* Don't strip a conversion from C++0x scoped enum, since they
3427 don't implicitly convert to other types. */
3428 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3429 && ENUM_IS_SCOPED (fromtype))
3430 break;
3431 /* If this isn't narrowing the argument, we can ignore it. */
3432 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3433 return c_common_truthvalue_conversion (location,
3434 TREE_OPERAND (expr, 0));
3436 break;
3438 case MODIFY_EXPR:
3439 if (!TREE_NO_WARNING (expr)
3440 && warn_parentheses)
3442 warning_at (location, OPT_Wparentheses,
3443 "suggest parentheses around assignment used as "
3444 "truth value");
3445 TREE_NO_WARNING (expr) = 1;
3447 break;
3449 default:
3450 break;
3453 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3455 tree t = save_expr (expr);
3456 expr = (build_binary_op
3457 (EXPR_LOCATION (expr),
3458 (TREE_SIDE_EFFECTS (expr)
3459 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3460 c_common_truthvalue_conversion
3461 (location,
3462 build_unary_op (location, REALPART_EXPR, t, false)),
3463 c_common_truthvalue_conversion
3464 (location,
3465 build_unary_op (location, IMAGPART_EXPR, t, false)),
3466 false));
3467 goto ret;
3470 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3472 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3473 FCONST0 (TYPE_MODE
3474 (TREE_TYPE (expr))));
3475 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3477 else
3478 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3480 ret:
3481 protected_set_expr_location (expr, location);
3482 return expr;
3485 static void def_builtin_1 (enum built_in_function fncode,
3486 const char *name,
3487 enum built_in_class fnclass,
3488 tree fntype, tree libtype,
3489 bool both_p, bool fallback_p, bool nonansi_p,
3490 tree fnattrs, bool implicit_p);
3493 /* Apply the TYPE_QUALS to the new DECL. */
3495 void
3496 c_apply_type_quals_to_decl (int type_quals, tree decl)
3498 tree type = TREE_TYPE (decl);
3500 if (type == error_mark_node)
3501 return;
3503 if ((type_quals & TYPE_QUAL_CONST)
3504 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3505 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3506 constructor can produce constant init, so rely on cp_finish_decl to
3507 clear TREE_READONLY if the variable has non-constant init. */
3508 TREE_READONLY (decl) = 1;
3509 if (type_quals & TYPE_QUAL_VOLATILE)
3511 TREE_SIDE_EFFECTS (decl) = 1;
3512 TREE_THIS_VOLATILE (decl) = 1;
3514 if (type_quals & TYPE_QUAL_RESTRICT)
3516 while (type && TREE_CODE (type) == ARRAY_TYPE)
3517 /* Allow 'restrict' on arrays of pointers.
3518 FIXME currently we just ignore it. */
3519 type = TREE_TYPE (type);
3520 if (!type
3521 || !POINTER_TYPE_P (type)
3522 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3523 error ("invalid use of %<restrict%>");
3527 /* Return the typed-based alias set for T, which may be an expression
3528 or a type. Return -1 if we don't do anything special. */
3530 alias_set_type
3531 c_common_get_alias_set (tree t)
3533 /* For VLAs, use the alias set of the element type rather than the
3534 default of alias set 0 for types compared structurally. */
3535 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3537 if (TREE_CODE (t) == ARRAY_TYPE)
3538 return get_alias_set (TREE_TYPE (t));
3539 return -1;
3542 /* That's all the expressions we handle specially. */
3543 if (!TYPE_P (t))
3544 return -1;
3546 /* The C standard guarantees that any object may be accessed via an
3547 lvalue that has character type. */
3548 if (t == char_type_node
3549 || t == signed_char_type_node
3550 || t == unsigned_char_type_node)
3551 return 0;
3553 /* The C standard specifically allows aliasing between signed and
3554 unsigned variants of the same type. We treat the signed
3555 variant as canonical. */
3556 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3558 tree t1 = c_common_signed_type (t);
3560 /* t1 == t can happen for boolean nodes which are always unsigned. */
3561 if (t1 != t)
3562 return get_alias_set (t1);
3565 return -1;
3568 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3569 the IS_SIZEOF parameter indicates which operator is being applied.
3570 The COMPLAIN flag controls whether we should diagnose possibly
3571 ill-formed constructs or not. LOC is the location of the SIZEOF or
3572 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3573 a type in any context should be returned, rather than the normal
3574 alignment for that type. */
3576 tree
3577 c_sizeof_or_alignof_type (location_t loc,
3578 tree type, bool is_sizeof, bool min_alignof,
3579 int complain)
3581 const char *op_name;
3582 tree value = NULL;
3583 enum tree_code type_code = TREE_CODE (type);
3585 op_name = is_sizeof ? "sizeof" : "__alignof__";
3587 if (type_code == FUNCTION_TYPE)
3589 if (is_sizeof)
3591 if (complain && warn_pointer_arith)
3592 pedwarn (loc, OPT_Wpointer_arith,
3593 "invalid application of %<sizeof%> to a function type");
3594 else if (!complain)
3595 return error_mark_node;
3596 value = size_one_node;
3598 else
3600 if (complain)
3602 if (c_dialect_cxx ())
3603 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3604 "%<alignof%> applied to a function type");
3605 else
3606 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3607 "%<_Alignof%> applied to a function type");
3609 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3612 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3614 if (type_code == VOID_TYPE
3615 && complain && warn_pointer_arith)
3616 pedwarn (loc, OPT_Wpointer_arith,
3617 "invalid application of %qs to a void type", op_name);
3618 else if (!complain)
3619 return error_mark_node;
3620 value = size_one_node;
3622 else if (!COMPLETE_TYPE_P (type)
3623 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3625 if (complain)
3626 error_at (loc, "invalid application of %qs to incomplete type %qT",
3627 op_name, type);
3628 return error_mark_node;
3630 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3631 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3633 if (complain)
3634 error_at (loc, "invalid application of %qs to array type %qT of "
3635 "incomplete element type", op_name, type);
3636 return error_mark_node;
3638 else
3640 if (is_sizeof)
3641 /* Convert in case a char is more than one unit. */
3642 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3643 size_int (TYPE_PRECISION (char_type_node)
3644 / BITS_PER_UNIT));
3645 else if (min_alignof)
3646 value = size_int (min_align_of_type (type));
3647 else
3648 value = size_int (TYPE_ALIGN_UNIT (type));
3651 /* VALUE will have the middle-end integer type sizetype.
3652 However, we should really return a value of type `size_t',
3653 which is just a typedef for an ordinary integer type. */
3654 value = fold_convert_loc (loc, size_type_node, value);
3656 return value;
3659 /* Implement the __alignof keyword: Return the minimum required
3660 alignment of EXPR, measured in bytes. For VAR_DECLs,
3661 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3662 from an "aligned" __attribute__ specification). LOC is the
3663 location of the ALIGNOF operator. */
3665 tree
3666 c_alignof_expr (location_t loc, tree expr)
3668 tree t;
3670 if (VAR_OR_FUNCTION_DECL_P (expr))
3671 t = size_int (DECL_ALIGN_UNIT (expr));
3673 else if (TREE_CODE (expr) == COMPONENT_REF
3674 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3676 error_at (loc, "%<__alignof%> applied to a bit-field");
3677 t = size_one_node;
3679 else if (TREE_CODE (expr) == COMPONENT_REF
3680 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3681 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3683 else if (INDIRECT_REF_P (expr))
3685 tree t = TREE_OPERAND (expr, 0);
3686 tree best = t;
3687 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3689 while (CONVERT_EXPR_P (t)
3690 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3692 int thisalign;
3694 t = TREE_OPERAND (t, 0);
3695 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3696 if (thisalign > bestalign)
3697 best = t, bestalign = thisalign;
3699 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3701 else
3702 return c_alignof (loc, TREE_TYPE (expr));
3704 return fold_convert_loc (loc, size_type_node, t);
3707 /* Handle C and C++ default attributes. */
3709 enum built_in_attribute
3711 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3712 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3713 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3714 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3715 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3716 #include "builtin-attrs.def"
3717 #undef DEF_ATTR_NULL_TREE
3718 #undef DEF_ATTR_INT
3719 #undef DEF_ATTR_STRING
3720 #undef DEF_ATTR_IDENT
3721 #undef DEF_ATTR_TREE_LIST
3722 ATTR_LAST
3725 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3727 static void c_init_attributes (void);
3729 enum c_builtin_type
3731 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3732 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3733 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3734 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3735 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3736 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3737 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3738 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3739 ARG6) NAME,
3740 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3741 ARG6, ARG7) NAME,
3742 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3743 ARG6, ARG7, ARG8) NAME,
3744 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3745 ARG6, ARG7, ARG8, ARG9) NAME,
3746 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3747 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3748 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3749 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3750 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3751 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3752 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3753 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3754 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3755 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3756 NAME,
3757 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3758 ARG6) NAME,
3759 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3760 ARG6, ARG7) NAME,
3761 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3762 #include "builtin-types.def"
3763 #undef DEF_PRIMITIVE_TYPE
3764 #undef DEF_FUNCTION_TYPE_0
3765 #undef DEF_FUNCTION_TYPE_1
3766 #undef DEF_FUNCTION_TYPE_2
3767 #undef DEF_FUNCTION_TYPE_3
3768 #undef DEF_FUNCTION_TYPE_4
3769 #undef DEF_FUNCTION_TYPE_5
3770 #undef DEF_FUNCTION_TYPE_6
3771 #undef DEF_FUNCTION_TYPE_7
3772 #undef DEF_FUNCTION_TYPE_8
3773 #undef DEF_FUNCTION_TYPE_9
3774 #undef DEF_FUNCTION_TYPE_10
3775 #undef DEF_FUNCTION_TYPE_11
3776 #undef DEF_FUNCTION_TYPE_VAR_0
3777 #undef DEF_FUNCTION_TYPE_VAR_1
3778 #undef DEF_FUNCTION_TYPE_VAR_2
3779 #undef DEF_FUNCTION_TYPE_VAR_3
3780 #undef DEF_FUNCTION_TYPE_VAR_4
3781 #undef DEF_FUNCTION_TYPE_VAR_5
3782 #undef DEF_FUNCTION_TYPE_VAR_6
3783 #undef DEF_FUNCTION_TYPE_VAR_7
3784 #undef DEF_POINTER_TYPE
3785 BT_LAST
3788 typedef enum c_builtin_type builtin_type;
3790 /* A temporary array for c_common_nodes_and_builtins. Used in
3791 communication with def_fn_type. */
3792 static tree builtin_types[(int) BT_LAST + 1];
3794 /* A helper function for c_common_nodes_and_builtins. Build function type
3795 for DEF with return type RET and N arguments. If VAR is true, then the
3796 function should be variadic after those N arguments.
3798 Takes special care not to ICE if any of the types involved are
3799 error_mark_node, which indicates that said type is not in fact available
3800 (see builtin_type_for_size). In which case the function type as a whole
3801 should be error_mark_node. */
3803 static void
3804 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3806 tree t;
3807 tree *args = XALLOCAVEC (tree, n);
3808 va_list list;
3809 int i;
3811 va_start (list, n);
3812 for (i = 0; i < n; ++i)
3814 builtin_type a = (builtin_type) va_arg (list, int);
3815 t = builtin_types[a];
3816 if (t == error_mark_node)
3817 goto egress;
3818 args[i] = t;
3821 t = builtin_types[ret];
3822 if (t == error_mark_node)
3823 goto egress;
3824 if (var)
3825 t = build_varargs_function_type_array (t, n, args);
3826 else
3827 t = build_function_type_array (t, n, args);
3829 egress:
3830 builtin_types[def] = t;
3831 va_end (list);
3834 /* Build builtin functions common to both C and C++ language
3835 frontends. */
3837 static void
3838 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3840 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3841 builtin_types[ENUM] = VALUE;
3842 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3843 def_fn_type (ENUM, RETURN, 0, 0);
3844 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3845 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3846 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3847 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3848 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3849 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3850 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3851 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3852 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3853 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3854 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3855 ARG6) \
3856 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3857 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3858 ARG6, ARG7) \
3859 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3860 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3861 ARG6, ARG7, ARG8) \
3862 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3863 ARG7, ARG8);
3864 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3865 ARG6, ARG7, ARG8, ARG9) \
3866 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3867 ARG7, ARG8, ARG9);
3868 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3869 ARG6, ARG7, ARG8, ARG9, ARG10) \
3870 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3871 ARG7, ARG8, ARG9, ARG10);
3872 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3873 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3874 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3875 ARG7, ARG8, ARG9, ARG10, ARG11);
3876 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3877 def_fn_type (ENUM, RETURN, 1, 0);
3878 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3879 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3880 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3881 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3882 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3883 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3884 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3885 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3886 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3887 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3888 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3889 ARG6) \
3890 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3891 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3892 ARG6, ARG7) \
3893 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3894 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3895 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3897 #include "builtin-types.def"
3899 #undef DEF_PRIMITIVE_TYPE
3900 #undef DEF_FUNCTION_TYPE_0
3901 #undef DEF_FUNCTION_TYPE_1
3902 #undef DEF_FUNCTION_TYPE_2
3903 #undef DEF_FUNCTION_TYPE_3
3904 #undef DEF_FUNCTION_TYPE_4
3905 #undef DEF_FUNCTION_TYPE_5
3906 #undef DEF_FUNCTION_TYPE_6
3907 #undef DEF_FUNCTION_TYPE_7
3908 #undef DEF_FUNCTION_TYPE_8
3909 #undef DEF_FUNCTION_TYPE_9
3910 #undef DEF_FUNCTION_TYPE_10
3911 #undef DEF_FUNCTION_TYPE_11
3912 #undef DEF_FUNCTION_TYPE_VAR_0
3913 #undef DEF_FUNCTION_TYPE_VAR_1
3914 #undef DEF_FUNCTION_TYPE_VAR_2
3915 #undef DEF_FUNCTION_TYPE_VAR_3
3916 #undef DEF_FUNCTION_TYPE_VAR_4
3917 #undef DEF_FUNCTION_TYPE_VAR_5
3918 #undef DEF_FUNCTION_TYPE_VAR_6
3919 #undef DEF_FUNCTION_TYPE_VAR_7
3920 #undef DEF_POINTER_TYPE
3921 builtin_types[(int) BT_LAST] = NULL_TREE;
3923 c_init_attributes ();
3925 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3926 NONANSI_P, ATTRS, IMPLICIT, COND) \
3927 if (NAME && COND) \
3928 def_builtin_1 (ENUM, NAME, CLASS, \
3929 builtin_types[(int) TYPE], \
3930 builtin_types[(int) LIBTYPE], \
3931 BOTH_P, FALLBACK_P, NONANSI_P, \
3932 built_in_attributes[(int) ATTRS], IMPLICIT);
3933 #include "builtins.def"
3935 targetm.init_builtins ();
3937 build_common_builtin_nodes ();
3940 /* Like get_identifier, but avoid warnings about null arguments when
3941 the argument may be NULL for targets where GCC lacks stdint.h type
3942 information. */
3944 static inline tree
3945 c_get_ident (const char *id)
3947 return get_identifier (id);
3950 /* Build tree nodes and builtin functions common to both C and C++ language
3951 frontends. */
3953 void
3954 c_common_nodes_and_builtins (void)
3956 int char16_type_size;
3957 int char32_type_size;
3958 int wchar_type_size;
3959 tree array_domain_type;
3960 tree va_list_ref_type_node;
3961 tree va_list_arg_type_node;
3962 int i;
3964 build_common_tree_nodes (flag_signed_char);
3966 /* Define `int' and `char' first so that dbx will output them first. */
3967 record_builtin_type (RID_INT, NULL, integer_type_node);
3968 record_builtin_type (RID_CHAR, "char", char_type_node);
3970 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3971 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3972 but not C. Are the conditionals here needed? */
3973 if (c_dialect_cxx ())
3974 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3975 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3976 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3977 record_builtin_type (RID_MAX, "long unsigned int",
3978 long_unsigned_type_node);
3980 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3982 char name[25];
3984 sprintf (name, "__int%d", int_n_data[i].bitsize);
3985 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
3986 int_n_trees[i].signed_type);
3987 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
3988 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
3991 if (c_dialect_cxx ())
3992 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3993 record_builtin_type (RID_MAX, "long long int",
3994 long_long_integer_type_node);
3995 record_builtin_type (RID_MAX, "long long unsigned int",
3996 long_long_unsigned_type_node);
3997 if (c_dialect_cxx ())
3998 record_builtin_type (RID_MAX, "long long unsigned",
3999 long_long_unsigned_type_node);
4000 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4001 record_builtin_type (RID_MAX, "short unsigned int",
4002 short_unsigned_type_node);
4003 if (c_dialect_cxx ())
4004 record_builtin_type (RID_MAX, "unsigned short",
4005 short_unsigned_type_node);
4007 /* Define both `signed char' and `unsigned char'. */
4008 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4009 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4011 /* These are types that c_common_type_for_size and
4012 c_common_type_for_mode use. */
4013 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4014 TYPE_DECL, NULL_TREE,
4015 intQI_type_node));
4016 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4017 TYPE_DECL, NULL_TREE,
4018 intHI_type_node));
4019 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4020 TYPE_DECL, NULL_TREE,
4021 intSI_type_node));
4022 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4023 TYPE_DECL, NULL_TREE,
4024 intDI_type_node));
4025 #if HOST_BITS_PER_WIDE_INT >= 64
4026 /* Note that this is different than the __int128 type that's part of
4027 the generic __intN support. */
4028 if (targetm.scalar_mode_supported_p (TImode))
4029 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4030 TYPE_DECL,
4031 get_identifier ("__int128_t"),
4032 intTI_type_node));
4033 #endif
4034 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4035 TYPE_DECL, NULL_TREE,
4036 unsigned_intQI_type_node));
4037 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4038 TYPE_DECL, NULL_TREE,
4039 unsigned_intHI_type_node));
4040 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4041 TYPE_DECL, NULL_TREE,
4042 unsigned_intSI_type_node));
4043 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4044 TYPE_DECL, NULL_TREE,
4045 unsigned_intDI_type_node));
4046 #if HOST_BITS_PER_WIDE_INT >= 64
4047 if (targetm.scalar_mode_supported_p (TImode))
4048 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4049 TYPE_DECL,
4050 get_identifier ("__uint128_t"),
4051 unsigned_intTI_type_node));
4052 #endif
4054 /* Create the widest literal types. */
4055 if (targetm.scalar_mode_supported_p (TImode))
4057 widest_integer_literal_type_node = intTI_type_node;
4058 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4060 else
4062 widest_integer_literal_type_node = intDI_type_node;
4063 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4066 signed_size_type_node = c_common_signed_type (size_type_node);
4068 pid_type_node =
4069 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4071 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4072 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4073 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4075 if (!c_dialect_cxx ())
4076 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4077 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4078 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4079 FLOATN_NX_TYPE_NODE (i));
4081 /* Only supported decimal floating point extension if the target
4082 actually supports underlying modes. */
4083 if (targetm.scalar_mode_supported_p (SDmode)
4084 && targetm.scalar_mode_supported_p (DDmode)
4085 && targetm.scalar_mode_supported_p (TDmode))
4087 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4088 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4089 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4092 if (targetm.fixed_point_supported_p ())
4094 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4095 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4096 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4097 record_builtin_type (RID_MAX, "long long _Fract",
4098 long_long_fract_type_node);
4099 record_builtin_type (RID_MAX, "unsigned short _Fract",
4100 unsigned_short_fract_type_node);
4101 record_builtin_type (RID_MAX, "unsigned _Fract",
4102 unsigned_fract_type_node);
4103 record_builtin_type (RID_MAX, "unsigned long _Fract",
4104 unsigned_long_fract_type_node);
4105 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4106 unsigned_long_long_fract_type_node);
4107 record_builtin_type (RID_MAX, "_Sat short _Fract",
4108 sat_short_fract_type_node);
4109 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4110 record_builtin_type (RID_MAX, "_Sat long _Fract",
4111 sat_long_fract_type_node);
4112 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4113 sat_long_long_fract_type_node);
4114 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4115 sat_unsigned_short_fract_type_node);
4116 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4117 sat_unsigned_fract_type_node);
4118 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4119 sat_unsigned_long_fract_type_node);
4120 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4121 sat_unsigned_long_long_fract_type_node);
4122 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4123 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4124 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4125 record_builtin_type (RID_MAX, "long long _Accum",
4126 long_long_accum_type_node);
4127 record_builtin_type (RID_MAX, "unsigned short _Accum",
4128 unsigned_short_accum_type_node);
4129 record_builtin_type (RID_MAX, "unsigned _Accum",
4130 unsigned_accum_type_node);
4131 record_builtin_type (RID_MAX, "unsigned long _Accum",
4132 unsigned_long_accum_type_node);
4133 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4134 unsigned_long_long_accum_type_node);
4135 record_builtin_type (RID_MAX, "_Sat short _Accum",
4136 sat_short_accum_type_node);
4137 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4138 record_builtin_type (RID_MAX, "_Sat long _Accum",
4139 sat_long_accum_type_node);
4140 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4141 sat_long_long_accum_type_node);
4142 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4143 sat_unsigned_short_accum_type_node);
4144 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4145 sat_unsigned_accum_type_node);
4146 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4147 sat_unsigned_long_accum_type_node);
4148 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4149 sat_unsigned_long_long_accum_type_node);
4153 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4154 TYPE_DECL,
4155 get_identifier ("complex int"),
4156 complex_integer_type_node));
4157 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4158 TYPE_DECL,
4159 get_identifier ("complex float"),
4160 complex_float_type_node));
4161 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4162 TYPE_DECL,
4163 get_identifier ("complex double"),
4164 complex_double_type_node));
4165 lang_hooks.decls.pushdecl
4166 (build_decl (UNKNOWN_LOCATION,
4167 TYPE_DECL, get_identifier ("complex long double"),
4168 complex_long_double_type_node));
4170 if (!c_dialect_cxx ())
4171 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4172 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4174 char buf[30];
4175 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4176 floatn_nx_types[i].extended ? "x" : "");
4177 lang_hooks.decls.pushdecl
4178 (build_decl (UNKNOWN_LOCATION,
4179 TYPE_DECL,
4180 get_identifier (buf),
4181 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4184 if (c_dialect_cxx ())
4186 /* For C++, make fileptr_type_node a distinct void * type until
4187 FILE type is defined. Likewise for const struct tm*. */
4188 for (unsigned i = 0;
4189 i < sizeof (builtin_structptr_types)
4190 / sizeof (builtin_structptr_type);
4191 ++i)
4192 builtin_structptr_types[i].node =
4193 build_variant_type_copy (builtin_structptr_types[i].base);
4197 record_builtin_type (RID_VOID, NULL, void_type_node);
4199 /* Set the TYPE_NAME for any variants that were built before
4200 record_builtin_type gave names to the built-in types. */
4202 tree void_name = TYPE_NAME (void_type_node);
4203 TYPE_NAME (void_type_node) = NULL_TREE;
4204 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4205 = void_name;
4206 TYPE_NAME (void_type_node) = void_name;
4209 void_list_node = build_void_list_node ();
4211 /* Make a type to be the domain of a few array types
4212 whose domains don't really matter.
4213 200 is small enough that it always fits in size_t
4214 and large enough that it can hold most function names for the
4215 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4216 array_domain_type = build_index_type (size_int (200));
4218 /* Make a type for arrays of characters.
4219 With luck nothing will ever really depend on the length of this
4220 array type. */
4221 char_array_type_node
4222 = build_array_type (char_type_node, array_domain_type);
4224 string_type_node = build_pointer_type (char_type_node);
4225 const_string_type_node
4226 = build_pointer_type (build_qualified_type
4227 (char_type_node, TYPE_QUAL_CONST));
4229 /* This is special for C++ so functions can be overloaded. */
4230 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4231 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4232 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4233 underlying_wchar_type_node = wchar_type_node;
4234 if (c_dialect_cxx ())
4236 if (TYPE_UNSIGNED (wchar_type_node))
4237 wchar_type_node = make_unsigned_type (wchar_type_size);
4238 else
4239 wchar_type_node = make_signed_type (wchar_type_size);
4240 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4243 /* This is for wide string constants. */
4244 wchar_array_type_node
4245 = build_array_type (wchar_type_node, array_domain_type);
4247 /* Define 'char16_t'. */
4248 char16_type_node = get_identifier (CHAR16_TYPE);
4249 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4250 char16_type_size = TYPE_PRECISION (char16_type_node);
4251 if (c_dialect_cxx ())
4253 char16_type_node = make_unsigned_type (char16_type_size);
4255 if (cxx_dialect >= cxx11)
4256 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4259 /* This is for UTF-16 string constants. */
4260 char16_array_type_node
4261 = build_array_type (char16_type_node, array_domain_type);
4263 /* Define 'char32_t'. */
4264 char32_type_node = get_identifier (CHAR32_TYPE);
4265 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4266 char32_type_size = TYPE_PRECISION (char32_type_node);
4267 if (c_dialect_cxx ())
4269 char32_type_node = make_unsigned_type (char32_type_size);
4271 if (cxx_dialect >= cxx11)
4272 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4275 /* This is for UTF-32 string constants. */
4276 char32_array_type_node
4277 = build_array_type (char32_type_node, array_domain_type);
4279 wint_type_node =
4280 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4282 intmax_type_node =
4283 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4284 uintmax_type_node =
4285 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4287 if (SIG_ATOMIC_TYPE)
4288 sig_atomic_type_node =
4289 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4290 if (INT8_TYPE)
4291 int8_type_node =
4292 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4293 if (INT16_TYPE)
4294 int16_type_node =
4295 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4296 if (INT32_TYPE)
4297 int32_type_node =
4298 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4299 if (INT64_TYPE)
4300 int64_type_node =
4301 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4302 if (UINT8_TYPE)
4303 uint8_type_node =
4304 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4305 if (UINT16_TYPE)
4306 c_uint16_type_node = uint16_type_node =
4307 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4308 if (UINT32_TYPE)
4309 c_uint32_type_node = uint32_type_node =
4310 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4311 if (UINT64_TYPE)
4312 c_uint64_type_node = uint64_type_node =
4313 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4314 if (INT_LEAST8_TYPE)
4315 int_least8_type_node =
4316 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4317 if (INT_LEAST16_TYPE)
4318 int_least16_type_node =
4319 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4320 if (INT_LEAST32_TYPE)
4321 int_least32_type_node =
4322 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4323 if (INT_LEAST64_TYPE)
4324 int_least64_type_node =
4325 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4326 if (UINT_LEAST8_TYPE)
4327 uint_least8_type_node =
4328 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4329 if (UINT_LEAST16_TYPE)
4330 uint_least16_type_node =
4331 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4332 if (UINT_LEAST32_TYPE)
4333 uint_least32_type_node =
4334 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4335 if (UINT_LEAST64_TYPE)
4336 uint_least64_type_node =
4337 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4338 if (INT_FAST8_TYPE)
4339 int_fast8_type_node =
4340 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4341 if (INT_FAST16_TYPE)
4342 int_fast16_type_node =
4343 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4344 if (INT_FAST32_TYPE)
4345 int_fast32_type_node =
4346 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4347 if (INT_FAST64_TYPE)
4348 int_fast64_type_node =
4349 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4350 if (UINT_FAST8_TYPE)
4351 uint_fast8_type_node =
4352 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4353 if (UINT_FAST16_TYPE)
4354 uint_fast16_type_node =
4355 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4356 if (UINT_FAST32_TYPE)
4357 uint_fast32_type_node =
4358 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4359 if (UINT_FAST64_TYPE)
4360 uint_fast64_type_node =
4361 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4362 if (INTPTR_TYPE)
4363 intptr_type_node =
4364 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4365 if (UINTPTR_TYPE)
4366 uintptr_type_node =
4367 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4369 default_function_type
4370 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4371 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4373 lang_hooks.decls.pushdecl
4374 (build_decl (UNKNOWN_LOCATION,
4375 TYPE_DECL, get_identifier ("__builtin_va_list"),
4376 va_list_type_node));
4377 if (targetm.enum_va_list_p)
4379 int l;
4380 const char *pname;
4381 tree ptype;
4383 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4385 lang_hooks.decls.pushdecl
4386 (build_decl (UNKNOWN_LOCATION,
4387 TYPE_DECL, get_identifier (pname),
4388 ptype));
4393 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4395 va_list_arg_type_node = va_list_ref_type_node =
4396 build_pointer_type (TREE_TYPE (va_list_type_node));
4398 else
4400 va_list_arg_type_node = va_list_type_node;
4401 va_list_ref_type_node = build_reference_type (va_list_type_node);
4404 if (!flag_preprocess_only)
4405 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4407 main_identifier_node = get_identifier ("main");
4409 /* Create the built-in __null node. It is important that this is
4410 not shared. */
4411 null_node = make_int_cst (1, 1);
4412 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4414 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4415 memset (builtin_types, 0, sizeof (builtin_types));
4418 /* The number of named compound-literals generated thus far. */
4419 static GTY(()) int compound_literal_number;
4421 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4423 void
4424 set_compound_literal_name (tree decl)
4426 char *name;
4427 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4428 compound_literal_number);
4429 compound_literal_number++;
4430 DECL_NAME (decl) = get_identifier (name);
4433 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4434 TYPE and operand OP. */
4436 static tree
4437 build_va_arg_1 (location_t loc, tree type, tree op)
4439 tree expr = build1 (VA_ARG_EXPR, type, op);
4440 SET_EXPR_LOCATION (expr, loc);
4441 return expr;
4444 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4445 va_arg (EXPR, TYPE) at source location LOC. */
4447 tree
4448 build_va_arg (location_t loc, tree expr, tree type)
4450 tree va_type = TREE_TYPE (expr);
4451 tree canon_va_type = (va_type == error_mark_node
4452 ? error_mark_node
4453 : targetm.canonical_va_list_type (va_type));
4455 if (va_type == error_mark_node
4456 || canon_va_type == NULL_TREE)
4458 if (canon_va_type == NULL_TREE)
4459 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4461 /* Let's handle things neutrallly, if expr:
4462 - has undeclared type, or
4463 - is not an va_list type. */
4464 return build_va_arg_1 (loc, type, error_mark_node);
4467 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4469 /* Case 1: Not an array type. */
4471 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4472 type. */
4473 mark_addressable (expr);
4474 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4476 return build_va_arg_1 (loc, type, expr);
4479 /* Case 2: Array type.
4481 Background:
4483 For contrast, let's start with the simple case (case 1). If
4484 canon_va_type is not an array type, but say a char *, then when
4485 passing-by-value a va_list, the type of the va_list param decl is
4486 the same as for another va_list decl (all ap's are char *):
4488 f2_1 (char * ap)
4489 D.1815 = VA_ARG (&ap, 0B, 1);
4490 return D.1815;
4492 f2 (int i)
4493 char * ap.0;
4494 char * ap;
4495 __builtin_va_start (&ap, 0);
4496 ap.0 = ap;
4497 res = f2_1 (ap.0);
4498 __builtin_va_end (&ap);
4499 D.1812 = res;
4500 return D.1812;
4502 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4503 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4504 the same as for another va_list decl (case 2a, struct ap[1]).
4506 f2_1 (struct * ap)
4507 D.1844 = VA_ARG (ap, 0B, 0);
4508 return D.1844;
4510 f2 (int i)
4511 struct ap[1];
4512 __builtin_va_start (&ap, 0);
4513 res = f2_1 (&ap);
4514 __builtin_va_end (&ap);
4515 D.1841 = res;
4516 return D.1841;
4518 Case 2b is different because:
4519 - on the callee side, the parm decl has declared type va_list, but
4520 grokdeclarator changes the type of the parm decl to a pointer to the
4521 array elem type.
4522 - on the caller side, the pass-by-value uses &ap.
4524 We unify these two cases (case 2a: va_list is array type,
4525 case 2b: va_list is pointer to array elem type), by adding '&' for the
4526 array type case, such that we have a pointer to array elem in both
4527 cases. */
4529 if (TREE_CODE (va_type) == ARRAY_TYPE)
4531 /* Case 2a: va_list is array type. */
4533 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4534 elem type. */
4535 mark_addressable (expr);
4536 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4537 expr);
4539 /* Verify that &ap is still recognized as having va_list type. */
4540 tree canon_expr_type
4541 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4542 gcc_assert (canon_expr_type != NULL_TREE);
4544 else
4546 /* Case 2b: va_list is pointer to array elem type. */
4547 gcc_assert (POINTER_TYPE_P (va_type));
4549 /* Comparison as in std_canonical_va_list_type. */
4550 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4551 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4553 /* Don't take the address. We've already got '&ap'. */
4557 return build_va_arg_1 (loc, type, expr);
4561 /* Linked list of disabled built-in functions. */
4563 struct disabled_builtin
4565 const char *name;
4566 struct disabled_builtin *next;
4568 static disabled_builtin *disabled_builtins = NULL;
4570 static bool builtin_function_disabled_p (const char *);
4572 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4573 begins with "__builtin_", give an error. */
4575 void
4576 disable_builtin_function (const char *name)
4578 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4579 error ("cannot disable built-in function %qs", name);
4580 else
4582 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4583 new_disabled_builtin->name = name;
4584 new_disabled_builtin->next = disabled_builtins;
4585 disabled_builtins = new_disabled_builtin;
4590 /* Return true if the built-in function NAME has been disabled, false
4591 otherwise. */
4593 static bool
4594 builtin_function_disabled_p (const char *name)
4596 disabled_builtin *p;
4597 for (p = disabled_builtins; p != NULL; p = p->next)
4599 if (strcmp (name, p->name) == 0)
4600 return true;
4602 return false;
4606 /* Worker for DEF_BUILTIN.
4607 Possibly define a builtin function with one or two names.
4608 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4609 nonansi_p and flag_no_nonansi_builtin. */
4611 static void
4612 def_builtin_1 (enum built_in_function fncode,
4613 const char *name,
4614 enum built_in_class fnclass,
4615 tree fntype, tree libtype,
4616 bool both_p, bool fallback_p, bool nonansi_p,
4617 tree fnattrs, bool implicit_p)
4619 tree decl;
4620 const char *libname;
4622 if (fntype == error_mark_node)
4623 return;
4625 gcc_assert ((!both_p && !fallback_p)
4626 || !strncmp (name, "__builtin_",
4627 strlen ("__builtin_")));
4629 libname = name + strlen ("__builtin_");
4630 decl = add_builtin_function (name, fntype, fncode, fnclass,
4631 (fallback_p ? libname : NULL),
4632 fnattrs);
4634 set_builtin_decl (fncode, decl, implicit_p);
4636 if (both_p
4637 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4638 && !(nonansi_p && flag_no_nonansi_builtin))
4639 add_builtin_function (libname, libtype, fncode, fnclass,
4640 NULL, fnattrs);
4643 /* Nonzero if the type T promotes to int. This is (nearly) the
4644 integral promotions defined in ISO C99 6.3.1.1/2. */
4646 bool
4647 c_promoting_integer_type_p (const_tree t)
4649 switch (TREE_CODE (t))
4651 case INTEGER_TYPE:
4652 return (TYPE_MAIN_VARIANT (t) == char_type_node
4653 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4654 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4655 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4656 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4657 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4659 case ENUMERAL_TYPE:
4660 /* ??? Technically all enumerations not larger than an int
4661 promote to an int. But this is used along code paths
4662 that only want to notice a size change. */
4663 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4665 case BOOLEAN_TYPE:
4666 return true;
4668 default:
4669 return false;
4673 /* Return 1 if PARMS specifies a fixed number of parameters
4674 and none of their types is affected by default promotions. */
4676 bool
4677 self_promoting_args_p (const_tree parms)
4679 const_tree t;
4680 for (t = parms; t; t = TREE_CHAIN (t))
4682 tree type = TREE_VALUE (t);
4684 if (type == error_mark_node)
4685 continue;
4687 if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
4688 return false;
4690 if (type == NULL_TREE)
4691 return false;
4693 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4694 return false;
4696 if (c_promoting_integer_type_p (type))
4697 return false;
4699 return true;
4702 /* Recursively remove any '*' or '&' operator from TYPE. */
4703 tree
4704 strip_pointer_operator (tree t)
4706 while (POINTER_TYPE_P (t))
4707 t = TREE_TYPE (t);
4708 return t;
4711 /* Recursively remove pointer or array type from TYPE. */
4712 tree
4713 strip_pointer_or_array_types (tree t)
4715 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4716 t = TREE_TYPE (t);
4717 return t;
4720 /* Used to compare case labels. K1 and K2 are actually tree nodes
4721 representing case labels, or NULL_TREE for a `default' label.
4722 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4723 K2, and 0 if K1 and K2 are equal. */
4726 case_compare (splay_tree_key k1, splay_tree_key k2)
4728 /* Consider a NULL key (such as arises with a `default' label) to be
4729 smaller than anything else. */
4730 if (!k1)
4731 return k2 ? -1 : 0;
4732 else if (!k2)
4733 return k1 ? 1 : 0;
4735 return tree_int_cst_compare ((tree) k1, (tree) k2);
4738 /* Process a case label, located at LOC, for the range LOW_VALUE
4739 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4740 then this case label is actually a `default' label. If only
4741 HIGH_VALUE is NULL_TREE, then case label was declared using the
4742 usual C/C++ syntax, rather than the GNU case range extension.
4743 CASES is a tree containing all the case ranges processed so far;
4744 COND is the condition for the switch-statement itself.
4745 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4746 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4747 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4749 tree
4750 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4751 tree low_value, tree high_value, bool *outside_range_p)
4753 tree type;
4754 tree label;
4755 tree case_label;
4756 splay_tree_node node;
4758 /* Create the LABEL_DECL itself. */
4759 label = create_artificial_label (loc);
4761 /* If there was an error processing the switch condition, bail now
4762 before we get more confused. */
4763 if (!cond || cond == error_mark_node)
4764 goto error_out;
4766 if ((low_value && TREE_TYPE (low_value)
4767 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4768 || (high_value && TREE_TYPE (high_value)
4769 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4771 error_at (loc, "pointers are not permitted as case values");
4772 goto error_out;
4775 /* Case ranges are a GNU extension. */
4776 if (high_value)
4777 pedwarn (loc, OPT_Wpedantic,
4778 "range expressions in switch statements are non-standard");
4780 type = TREE_TYPE (cond);
4781 if (low_value)
4783 low_value = check_case_value (loc, low_value);
4784 low_value = convert_and_check (loc, type, low_value);
4785 if (low_value == error_mark_node)
4786 goto error_out;
4788 if (high_value)
4790 high_value = check_case_value (loc, high_value);
4791 high_value = convert_and_check (loc, type, high_value);
4792 if (high_value == error_mark_node)
4793 goto error_out;
4796 if (low_value && high_value)
4798 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4799 really a case range, even though it was written that way.
4800 Remove the HIGH_VALUE to simplify later processing. */
4801 if (tree_int_cst_equal (low_value, high_value))
4802 high_value = NULL_TREE;
4803 else if (!tree_int_cst_lt (low_value, high_value))
4804 warning_at (loc, 0, "empty range specified");
4807 /* See if the case is in range of the type of the original testing
4808 expression. If both low_value and high_value are out of range,
4809 don't insert the case label and return NULL_TREE. */
4810 if (low_value
4811 && !check_case_bounds (loc, type, orig_type,
4812 &low_value, high_value ? &high_value : NULL,
4813 outside_range_p))
4814 return NULL_TREE;
4816 /* Look up the LOW_VALUE in the table of case labels we already
4817 have. */
4818 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4819 /* If there was not an exact match, check for overlapping ranges.
4820 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4821 that's a `default' label and the only overlap is an exact match. */
4822 if (!node && (low_value || high_value))
4824 splay_tree_node low_bound;
4825 splay_tree_node high_bound;
4827 /* Even though there wasn't an exact match, there might be an
4828 overlap between this case range and another case range.
4829 Since we've (inductively) not allowed any overlapping case
4830 ranges, we simply need to find the greatest low case label
4831 that is smaller that LOW_VALUE, and the smallest low case
4832 label that is greater than LOW_VALUE. If there is an overlap
4833 it will occur in one of these two ranges. */
4834 low_bound = splay_tree_predecessor (cases,
4835 (splay_tree_key) low_value);
4836 high_bound = splay_tree_successor (cases,
4837 (splay_tree_key) low_value);
4839 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4840 the LOW_VALUE, so there is no need to check unless the
4841 LOW_BOUND is in fact itself a case range. */
4842 if (low_bound
4843 && CASE_HIGH ((tree) low_bound->value)
4844 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4845 low_value) >= 0)
4846 node = low_bound;
4847 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4848 range is bigger than the low end of the current range, so we
4849 are only interested if the current range is a real range, and
4850 not an ordinary case label. */
4851 else if (high_bound
4852 && high_value
4853 && (tree_int_cst_compare ((tree) high_bound->key,
4854 high_value)
4855 <= 0))
4856 node = high_bound;
4858 /* If there was an overlap, issue an error. */
4859 if (node)
4861 tree duplicate = CASE_LABEL ((tree) node->value);
4863 if (high_value)
4865 error_at (loc, "duplicate (or overlapping) case value");
4866 inform (DECL_SOURCE_LOCATION (duplicate),
4867 "this is the first entry overlapping that value");
4869 else if (low_value)
4871 error_at (loc, "duplicate case value") ;
4872 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4874 else
4876 error_at (loc, "multiple default labels in one switch");
4877 inform (DECL_SOURCE_LOCATION (duplicate),
4878 "this is the first default label");
4880 goto error_out;
4883 /* Add a CASE_LABEL to the statement-tree. */
4884 case_label = add_stmt (build_case_label (low_value, high_value, label));
4885 /* Register this case label in the splay tree. */
4886 splay_tree_insert (cases,
4887 (splay_tree_key) low_value,
4888 (splay_tree_value) case_label);
4890 return case_label;
4892 error_out:
4893 /* Add a label so that the back-end doesn't think that the beginning of
4894 the switch is unreachable. Note that we do not add a case label, as
4895 that just leads to duplicates and thence to failure later on. */
4896 if (!cases->root)
4898 tree t = create_artificial_label (loc);
4899 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4901 return error_mark_node;
4904 /* Subroutine of c_switch_covers_all_cases_p, called via
4905 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4906 ARGS[0] is initially NULL and after the first iteration is the
4907 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4908 type. */
4910 static int
4911 c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
4913 tree label = (tree) node->value;
4914 tree *args = (tree *) data;
4916 /* If there is a default case, we shouldn't have called this. */
4917 gcc_assert (CASE_LOW (label));
4919 if (args[0] == NULL_TREE)
4921 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
4922 return 1;
4924 else if (wi::add (wi::to_widest (args[0]), 1)
4925 != wi::to_widest (CASE_LOW (label)))
4926 return 1;
4927 if (CASE_HIGH (label))
4928 args[0] = CASE_HIGH (label);
4929 else
4930 args[0] = CASE_LOW (label);
4931 return 0;
4934 /* Return true if switch with CASES and switch condition with type
4935 covers all possible values in the case labels. */
4937 bool
4938 c_switch_covers_all_cases_p (splay_tree cases, tree type)
4940 /* If there is default:, this is always the case. */
4941 splay_tree_node default_node
4942 = splay_tree_lookup (cases, (splay_tree_key) NULL);
4943 if (default_node)
4944 return true;
4946 if (!INTEGRAL_TYPE_P (type))
4947 return false;
4949 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
4950 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
4951 return false;
4953 /* If there are no cases at all, or if the highest case label
4954 is smaller than TYPE_MAX_VALUE, return false. */
4955 if (args[0] == NULL_TREE
4956 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
4957 return false;
4959 return true;
4962 /* Finish an expression taking the address of LABEL (an
4963 IDENTIFIER_NODE). Returns an expression for the address.
4965 LOC is the location for the expression returned. */
4967 tree
4968 finish_label_address_expr (tree label, location_t loc)
4970 tree result;
4972 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
4974 if (label == error_mark_node)
4975 return error_mark_node;
4977 label = lookup_label (label);
4978 if (label == NULL_TREE)
4979 result = null_pointer_node;
4980 else
4982 TREE_USED (label) = 1;
4983 result = build1 (ADDR_EXPR, ptr_type_node, label);
4984 /* The current function is not necessarily uninlinable.
4985 Computed gotos are incompatible with inlining, but the value
4986 here could be used only in a diagnostic, for example. */
4987 protected_set_expr_location (result, loc);
4990 return result;
4994 /* Given a boolean expression ARG, return a tree representing an increment
4995 or decrement (as indicated by CODE) of ARG. The front end must check for
4996 invalid cases (e.g., decrement in C++). */
4997 tree
4998 boolean_increment (enum tree_code code, tree arg)
5000 tree val;
5001 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5003 arg = stabilize_reference (arg);
5004 switch (code)
5006 case PREINCREMENT_EXPR:
5007 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5008 break;
5009 case POSTINCREMENT_EXPR:
5010 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5011 arg = save_expr (arg);
5012 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5013 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5014 break;
5015 case PREDECREMENT_EXPR:
5016 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5017 invert_truthvalue_loc (input_location, arg));
5018 break;
5019 case POSTDECREMENT_EXPR:
5020 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5021 invert_truthvalue_loc (input_location, arg));
5022 arg = save_expr (arg);
5023 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5024 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5025 break;
5026 default:
5027 gcc_unreachable ();
5029 TREE_SIDE_EFFECTS (val) = 1;
5030 return val;
5033 /* Built-in macros for stddef.h and stdint.h, that require macros
5034 defined in this file. */
5035 void
5036 c_stddef_cpp_builtins(void)
5038 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5039 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5040 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5041 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5042 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5043 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5044 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5045 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5046 if (SIG_ATOMIC_TYPE)
5047 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5048 if (INT8_TYPE)
5049 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5050 if (INT16_TYPE)
5051 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5052 if (INT32_TYPE)
5053 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5054 if (INT64_TYPE)
5055 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5056 if (UINT8_TYPE)
5057 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5058 if (UINT16_TYPE)
5059 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5060 if (UINT32_TYPE)
5061 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5062 if (UINT64_TYPE)
5063 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5064 if (INT_LEAST8_TYPE)
5065 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5066 if (INT_LEAST16_TYPE)
5067 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5068 if (INT_LEAST32_TYPE)
5069 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5070 if (INT_LEAST64_TYPE)
5071 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5072 if (UINT_LEAST8_TYPE)
5073 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5074 if (UINT_LEAST16_TYPE)
5075 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5076 if (UINT_LEAST32_TYPE)
5077 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5078 if (UINT_LEAST64_TYPE)
5079 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5080 if (INT_FAST8_TYPE)
5081 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5082 if (INT_FAST16_TYPE)
5083 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5084 if (INT_FAST32_TYPE)
5085 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5086 if (INT_FAST64_TYPE)
5087 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5088 if (UINT_FAST8_TYPE)
5089 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5090 if (UINT_FAST16_TYPE)
5091 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5092 if (UINT_FAST32_TYPE)
5093 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5094 if (UINT_FAST64_TYPE)
5095 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5096 if (INTPTR_TYPE)
5097 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5098 if (UINTPTR_TYPE)
5099 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5102 static void
5103 c_init_attributes (void)
5105 /* Fill in the built_in_attributes array. */
5106 #define DEF_ATTR_NULL_TREE(ENUM) \
5107 built_in_attributes[(int) ENUM] = NULL_TREE;
5108 #define DEF_ATTR_INT(ENUM, VALUE) \
5109 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5110 #define DEF_ATTR_STRING(ENUM, VALUE) \
5111 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5112 #define DEF_ATTR_IDENT(ENUM, STRING) \
5113 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5114 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5115 built_in_attributes[(int) ENUM] \
5116 = tree_cons (built_in_attributes[(int) PURPOSE], \
5117 built_in_attributes[(int) VALUE], \
5118 built_in_attributes[(int) CHAIN]);
5119 #include "builtin-attrs.def"
5120 #undef DEF_ATTR_NULL_TREE
5121 #undef DEF_ATTR_INT
5122 #undef DEF_ATTR_IDENT
5123 #undef DEF_ATTR_TREE_LIST
5126 /* Check whether the byte alignment ALIGN is a valid user-specified
5127 alignment less than the supported maximum. If so, return ALIGN's
5128 base-2 log; if not, output an error and return -1. If OBJFILE
5129 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5130 converted to bits. Otherwise, consider valid only alignments
5131 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5132 If ALLOW_ZERO then 0 is valid and should result in
5133 a return of -1 with no error. */
5136 check_user_alignment (const_tree align, bool objfile, bool allow_zero)
5138 if (error_operand_p (align))
5139 return -1;
5141 if (TREE_CODE (align) != INTEGER_CST
5142 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5144 error ("requested alignment is not an integer constant");
5145 return -1;
5148 if (allow_zero && integer_zerop (align))
5149 return -1;
5151 int log2bitalign;
5152 if (tree_int_cst_sgn (align) == -1
5153 || (log2bitalign = tree_log2 (align)) == -1)
5155 error ("requested alignment %qE is not a positive power of 2",
5156 align);
5157 return -1;
5160 if (objfile)
5162 unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5163 if (tree_to_shwi (align) > maxalign)
5165 error ("requested alignment %qE exceeds object file maximum %u",
5166 align, maxalign);
5167 return -1;
5171 if (log2bitalign >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5173 error ("requested alignment %qE exceeds maximum %u",
5174 align, 1U << (HOST_BITS_PER_INT - 1));
5175 return -1;
5178 return log2bitalign;
5181 /* Determine the ELF symbol visibility for DECL, which is either a
5182 variable or a function. It is an error to use this function if a
5183 definition of DECL is not available in this translation unit.
5184 Returns true if the final visibility has been determined by this
5185 function; false if the caller is free to make additional
5186 modifications. */
5188 bool
5189 c_determine_visibility (tree decl)
5191 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5193 /* If the user explicitly specified the visibility with an
5194 attribute, honor that. DECL_VISIBILITY will have been set during
5195 the processing of the attribute. We check for an explicit
5196 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5197 to distinguish the use of an attribute from the use of a "#pragma
5198 GCC visibility push(...)"; in the latter case we still want other
5199 considerations to be able to overrule the #pragma. */
5200 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5201 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5202 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5203 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5204 return true;
5206 /* Set default visibility to whatever the user supplied with
5207 visibility_specified depending on #pragma GCC visibility. */
5208 if (!DECL_VISIBILITY_SPECIFIED (decl))
5210 if (visibility_options.inpragma
5211 || DECL_VISIBILITY (decl) != default_visibility)
5213 DECL_VISIBILITY (decl) = default_visibility;
5214 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5215 /* If visibility changed and DECL already has DECL_RTL, ensure
5216 symbol flags are updated. */
5217 if (((VAR_P (decl) && TREE_STATIC (decl))
5218 || TREE_CODE (decl) == FUNCTION_DECL)
5219 && DECL_RTL_SET_P (decl))
5220 make_decl_rtl (decl);
5223 return false;
5226 /* Data to communicate through check_function_arguments_recurse between
5227 check_function_nonnull and check_nonnull_arg. */
5229 struct nonnull_arg_ctx
5231 location_t loc;
5232 bool warned_p;
5235 /* Check the argument list of a function call for null in argument slots
5236 that are marked as requiring a non-null pointer argument. The NARGS
5237 arguments are passed in the array ARGARRAY. Return true if we have
5238 warned. */
5240 static bool
5241 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5243 tree a;
5244 int i;
5246 attrs = lookup_attribute ("nonnull", attrs);
5247 if (attrs == NULL_TREE)
5248 return false;
5250 a = attrs;
5251 /* See if any of the nonnull attributes has no arguments. If so,
5252 then every pointer argument is checked (in which case the check
5253 for pointer type is done in check_nonnull_arg). */
5254 if (TREE_VALUE (a) != NULL_TREE)
5256 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5257 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5259 struct nonnull_arg_ctx ctx = { loc, false };
5260 if (a != NULL_TREE)
5261 for (i = 0; i < nargs; i++)
5262 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5263 i + 1);
5264 else
5266 /* Walk the argument list. If we encounter an argument number we
5267 should check for non-null, do it. */
5268 for (i = 0; i < nargs; i++)
5270 for (a = attrs; ; a = TREE_CHAIN (a))
5272 a = lookup_attribute ("nonnull", a);
5273 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5274 break;
5277 if (a != NULL_TREE)
5278 check_function_arguments_recurse (check_nonnull_arg, &ctx,
5279 argarray[i], i + 1);
5282 return ctx.warned_p;
5285 /* Check that the Nth argument of a function call (counting backwards
5286 from the end) is a (pointer)0. The NARGS arguments are passed in the
5287 array ARGARRAY. */
5289 static void
5290 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5292 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5294 if (attr)
5296 int len = 0;
5297 int pos = 0;
5298 tree sentinel;
5299 function_args_iterator iter;
5300 tree t;
5302 /* Skip over the named arguments. */
5303 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5305 if (len == nargs)
5306 break;
5307 len++;
5310 if (TREE_VALUE (attr))
5312 tree p = TREE_VALUE (TREE_VALUE (attr));
5313 pos = TREE_INT_CST_LOW (p);
5316 /* The sentinel must be one of the varargs, i.e.
5317 in position >= the number of fixed arguments. */
5318 if ((nargs - 1 - pos) < len)
5320 warning (OPT_Wformat_,
5321 "not enough variable arguments to fit a sentinel");
5322 return;
5325 /* Validate the sentinel. */
5326 sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
5327 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5328 || !integer_zerop (sentinel))
5329 /* Although __null (in C++) is only an integer we allow it
5330 nevertheless, as we are guaranteed that it's exactly
5331 as wide as a pointer, and we don't want to force
5332 users to cast the NULL they have written there.
5333 We warn with -Wstrict-null-sentinel, though. */
5334 && (warn_strict_null_sentinel || null_node != sentinel))
5335 warning (OPT_Wformat_, "missing sentinel in function call");
5339 /* Check that the same argument isn't passed to two or more
5340 restrict-qualified formal and issue a -Wrestrict warning
5341 if it is. Return true if a warning has been issued. */
5343 static bool
5344 check_function_restrict (const_tree fndecl, const_tree fntype,
5345 int nargs, tree *unfolded_argarray)
5347 int i;
5348 tree parms = TYPE_ARG_TYPES (fntype);
5350 /* Call fold_for_warn on all of the arguments. */
5351 auto_vec<tree> argarray (nargs);
5352 for (i = 0; i < nargs; i++)
5353 argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5355 if (fndecl
5356 && TREE_CODE (fndecl) == FUNCTION_DECL)
5358 /* Avoid diagnosing calls built-ins with a zero size/bound
5359 here. They are checked in more detail elsewhere. */
5360 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5361 && nargs == 3
5362 && TREE_CODE (argarray[2]) == INTEGER_CST
5363 && integer_zerop (argarray[2]))
5364 return false;
5366 if (DECL_ARGUMENTS (fndecl))
5367 parms = DECL_ARGUMENTS (fndecl);
5370 for (i = 0; i < nargs; i++)
5371 TREE_VISITED (argarray[i]) = 0;
5373 bool warned = false;
5375 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5377 tree type;
5378 if (TREE_CODE (parms) == PARM_DECL)
5380 type = TREE_TYPE (parms);
5381 parms = DECL_CHAIN (parms);
5383 else
5385 type = TREE_VALUE (parms);
5386 parms = TREE_CHAIN (parms);
5388 if (POINTER_TYPE_P (type)
5389 && TYPE_RESTRICT (type)
5390 && !TYPE_READONLY (TREE_TYPE (type)))
5391 warned |= warn_for_restrict (i, argarray.address (), nargs);
5394 for (i = 0; i < nargs; i++)
5395 TREE_VISITED (argarray[i]) = 0;
5397 return warned;
5400 /* Helper for check_function_nonnull; given a list of operands which
5401 must be non-null in ARGS, determine if operand PARAM_NUM should be
5402 checked. */
5404 static bool
5405 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5407 unsigned HOST_WIDE_INT arg_num = 0;
5409 for (; args; args = TREE_CHAIN (args))
5411 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5413 gcc_assert (found);
5415 if (arg_num == param_num)
5416 return true;
5418 return false;
5421 /* Check that the function argument PARAM (which is operand number
5422 PARAM_NUM) is non-null. This is called by check_function_nonnull
5423 via check_function_arguments_recurse. */
5425 static void
5426 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5428 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5430 /* Just skip checking the argument if it's not a pointer. This can
5431 happen if the "nonnull" attribute was given without an operand
5432 list (which means to check every pointer argument). */
5434 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5435 return;
5437 /* Diagnose the simple cases of null arguments. */
5438 if (integer_zerop (fold_for_warn (param)))
5440 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5441 "required (argument %lu)", (unsigned long) param_num);
5442 pctx->warned_p = true;
5446 /* Helper for nonnull attribute handling; fetch the operand number
5447 from the attribute argument list. */
5449 bool
5450 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5452 /* Verify the arg number is a small constant. */
5453 if (tree_fits_uhwi_p (arg_num_expr))
5455 *valp = tree_to_uhwi (arg_num_expr);
5456 return true;
5458 else
5459 return false;
5462 /* Arguments being collected for optimization. */
5463 typedef const char *const_char_p; /* For DEF_VEC_P. */
5464 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5467 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5468 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5469 false for #pragma GCC optimize. */
5471 bool
5472 parse_optimize_options (tree args, bool attr_p)
5474 bool ret = true;
5475 unsigned opt_argc;
5476 unsigned i;
5477 const char **opt_argv;
5478 struct cl_decoded_option *decoded_options;
5479 unsigned int decoded_options_count;
5480 tree ap;
5482 /* Build up argv vector. Just in case the string is stored away, use garbage
5483 collected strings. */
5484 vec_safe_truncate (optimize_args, 0);
5485 vec_safe_push (optimize_args, (const char *) NULL);
5487 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5489 tree value = TREE_VALUE (ap);
5491 if (TREE_CODE (value) == INTEGER_CST)
5493 char buffer[20];
5494 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5495 vec_safe_push (optimize_args, ggc_strdup (buffer));
5498 else if (TREE_CODE (value) == STRING_CST)
5500 /* Split string into multiple substrings. */
5501 size_t len = TREE_STRING_LENGTH (value);
5502 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5503 char *end = p + len;
5504 char *comma;
5505 char *next_p = p;
5507 while (next_p != NULL)
5509 size_t len2;
5510 char *q, *r;
5512 p = next_p;
5513 comma = strchr (p, ',');
5514 if (comma)
5516 len2 = comma - p;
5517 *comma = '\0';
5518 next_p = comma+1;
5520 else
5522 len2 = end - p;
5523 next_p = NULL;
5526 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5528 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5529 options. */
5530 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5532 ret = false;
5533 if (attr_p)
5534 warning (OPT_Wattributes,
5535 "bad option %qs to attribute %<optimize%>", p);
5536 else
5537 warning (OPT_Wpragmas,
5538 "bad option %qs to pragma %<optimize%>", p);
5539 continue;
5542 if (*p != '-')
5544 *r++ = '-';
5546 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5547 itself is -Os, and any other switch begins with a -f. */
5548 if ((*p >= '0' && *p <= '9')
5549 || (p[0] == 's' && p[1] == '\0'))
5550 *r++ = 'O';
5551 else if (*p != 'O')
5552 *r++ = 'f';
5555 memcpy (r, p, len2);
5556 r[len2] = '\0';
5557 vec_safe_push (optimize_args, (const char *) q);
5563 opt_argc = optimize_args->length ();
5564 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5566 for (i = 1; i < opt_argc; i++)
5567 opt_argv[i] = (*optimize_args)[i];
5569 /* Now parse the options. */
5570 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5571 &decoded_options,
5572 &decoded_options_count);
5573 /* Drop non-Optimization options. */
5574 unsigned j = 1;
5575 for (i = 1; i < decoded_options_count; ++i)
5577 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5579 ret = false;
5580 if (attr_p)
5581 warning (OPT_Wattributes,
5582 "bad option %qs to attribute %<optimize%>",
5583 decoded_options[i].orig_option_with_args_text);
5584 else
5585 warning (OPT_Wpragmas,
5586 "bad option %qs to pragma %<optimize%>",
5587 decoded_options[i].orig_option_with_args_text);
5588 continue;
5590 if (i != j)
5591 decoded_options[j] = decoded_options[i];
5592 j++;
5594 decoded_options_count = j;
5595 /* And apply them. */
5596 decode_options (&global_options, &global_options_set,
5597 decoded_options, decoded_options_count,
5598 input_location, global_dc, NULL);
5600 targetm.override_options_after_change();
5602 optimize_args->truncate (0);
5603 return ret;
5606 /* Check whether ATTR is a valid attribute fallthrough. */
5608 bool
5609 attribute_fallthrough_p (tree attr)
5611 if (attr == error_mark_node)
5612 return false;
5613 tree t = lookup_attribute ("fallthrough", attr);
5614 if (t == NULL_TREE)
5615 return false;
5616 /* This attribute shall appear at most once in each attribute-list. */
5617 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5618 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5619 "times");
5620 /* No attribute-argument-clause shall be present. */
5621 else if (TREE_VALUE (t) != NULL_TREE)
5622 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5623 "a parameter");
5624 /* Warn if other attributes are found. */
5625 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5627 tree name = get_attribute_name (t);
5628 if (!is_attribute_p ("fallthrough", name))
5629 warning (OPT_Wattributes, "%qE attribute ignored", name);
5631 return true;
5635 /* Check for valid arguments being passed to a function with FNTYPE.
5636 There are NARGS arguments in the array ARGARRAY. LOC should be used
5637 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5638 been issued.
5640 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5641 to preserve location wrappers); checks that require folded arguments
5642 should call fold_for_warn on them. */
5644 bool
5645 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5646 int nargs, tree *argarray, vec<location_t> *arglocs)
5648 bool warned_p = false;
5650 /* Check for null being passed in a pointer argument that must be
5651 non-null. We also need to do this if format checking is enabled. */
5653 if (warn_nonnull)
5654 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5655 nargs, argarray);
5657 /* Check for errors in format strings. */
5659 if (warn_format || warn_suggest_attribute_format)
5660 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray, arglocs);
5662 if (warn_format)
5663 check_function_sentinel (fntype, nargs, argarray);
5665 if (warn_restrict)
5666 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
5667 return warned_p;
5670 /* Generic argument checking recursion routine. PARAM is the argument to
5671 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5672 once the argument is resolved. CTX is context for the callback. */
5673 void
5674 check_function_arguments_recurse (void (*callback)
5675 (void *, tree, unsigned HOST_WIDE_INT),
5676 void *ctx, tree param,
5677 unsigned HOST_WIDE_INT param_num)
5679 if (CONVERT_EXPR_P (param)
5680 && (TYPE_PRECISION (TREE_TYPE (param))
5681 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5683 /* Strip coercion. */
5684 check_function_arguments_recurse (callback, ctx,
5685 TREE_OPERAND (param, 0), param_num);
5686 return;
5689 if (TREE_CODE (param) == CALL_EXPR)
5691 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5692 tree attrs;
5693 bool found_format_arg = false;
5695 /* See if this is a call to a known internationalization function
5696 that modifies a format arg. Such a function may have multiple
5697 format_arg attributes (for example, ngettext). */
5699 for (attrs = TYPE_ATTRIBUTES (type);
5700 attrs;
5701 attrs = TREE_CHAIN (attrs))
5702 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5704 tree inner_arg;
5705 tree format_num_expr;
5706 int format_num;
5707 int i;
5708 call_expr_arg_iterator iter;
5710 /* Extract the argument number, which was previously checked
5711 to be valid. */
5712 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5714 format_num = tree_to_uhwi (format_num_expr);
5716 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5717 inner_arg != NULL_TREE;
5718 inner_arg = next_call_expr_arg (&iter), i++)
5719 if (i == format_num)
5721 check_function_arguments_recurse (callback, ctx,
5722 inner_arg, param_num);
5723 found_format_arg = true;
5724 break;
5728 /* If we found a format_arg attribute and did a recursive check,
5729 we are done with checking this argument. Otherwise, we continue
5730 and this will be considered a non-literal. */
5731 if (found_format_arg)
5732 return;
5735 if (TREE_CODE (param) == COND_EXPR)
5737 /* Simplify to avoid warning for an impossible case. */
5738 param = fold_for_warn (param);
5739 if (TREE_CODE (param) == COND_EXPR)
5741 /* Check both halves of the conditional expression. */
5742 check_function_arguments_recurse (callback, ctx,
5743 TREE_OPERAND (param, 1),
5744 param_num);
5745 check_function_arguments_recurse (callback, ctx,
5746 TREE_OPERAND (param, 2),
5747 param_num);
5748 return;
5752 (*callback) (ctx, param, param_num);
5755 /* Checks for a builtin function FNDECL that the number of arguments
5756 NARGS against the required number REQUIRED and issues an error if
5757 there is a mismatch. Returns true if the number of arguments is
5758 correct, otherwise false. LOC is the location of FNDECL. */
5760 static bool
5761 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5762 int required)
5764 if (nargs < required)
5766 error_at (loc, "too few arguments to function %qE", fndecl);
5767 return false;
5769 else if (nargs > required)
5771 error_at (loc, "too many arguments to function %qE", fndecl);
5772 return false;
5774 return true;
5777 /* Helper macro for check_builtin_function_arguments. */
5778 #define ARG_LOCATION(N) \
5779 (arg_loc.is_empty () \
5780 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5781 : expansion_point_location (arg_loc[(N)]))
5783 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5784 Returns false if there was an error, otherwise true. LOC is the
5785 location of the function; ARG_LOC is a vector of locations of the
5786 arguments. */
5788 bool
5789 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5790 tree fndecl, int nargs, tree *args)
5792 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
5793 return true;
5795 switch (DECL_FUNCTION_CODE (fndecl))
5797 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5798 if (!tree_fits_uhwi_p (args[2]))
5800 error_at (ARG_LOCATION (2),
5801 "third argument to function %qE must be a constant integer",
5802 fndecl);
5803 return false;
5805 /* fall through */
5807 case BUILT_IN_ALLOCA_WITH_ALIGN:
5809 /* Get the requested alignment (in bits) if it's a constant
5810 integer expression. */
5811 unsigned HOST_WIDE_INT align
5812 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5814 /* Determine if the requested alignment is a power of 2. */
5815 if ((align & (align - 1)))
5816 align = 0;
5818 /* The maximum alignment in bits corresponding to the same
5819 maximum in bytes enforced in check_user_alignment(). */
5820 unsigned maxalign = (UINT_MAX >> 1) + 1;
5822 /* Reject invalid alignments. */
5823 if (align < BITS_PER_UNIT || maxalign < align)
5825 error_at (ARG_LOCATION (1),
5826 "second argument to function %qE must be a constant "
5827 "integer power of 2 between %qi and %qu bits",
5828 fndecl, BITS_PER_UNIT, maxalign);
5829 return false;
5831 return true;
5834 case BUILT_IN_CONSTANT_P:
5835 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5837 case BUILT_IN_ISFINITE:
5838 case BUILT_IN_ISINF:
5839 case BUILT_IN_ISINF_SIGN:
5840 case BUILT_IN_ISNAN:
5841 case BUILT_IN_ISNORMAL:
5842 case BUILT_IN_SIGNBIT:
5843 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5845 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5847 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5848 "call to function %qE", fndecl);
5849 return false;
5851 return true;
5853 return false;
5855 case BUILT_IN_ISGREATER:
5856 case BUILT_IN_ISGREATEREQUAL:
5857 case BUILT_IN_ISLESS:
5858 case BUILT_IN_ISLESSEQUAL:
5859 case BUILT_IN_ISLESSGREATER:
5860 case BUILT_IN_ISUNORDERED:
5861 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5863 enum tree_code code0, code1;
5864 code0 = TREE_CODE (TREE_TYPE (args[0]));
5865 code1 = TREE_CODE (TREE_TYPE (args[1]));
5866 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5867 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5868 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5870 error_at (loc, "non-floating-point arguments in call to "
5871 "function %qE", fndecl);
5872 return false;
5874 return true;
5876 return false;
5878 case BUILT_IN_FPCLASSIFY:
5879 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5881 for (unsigned int i = 0; i < 5; i++)
5882 if (TREE_CODE (args[i]) != INTEGER_CST)
5884 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5885 "call to function %qE", i + 1, fndecl);
5886 return false;
5889 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5891 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5892 "call to function %qE", fndecl);
5893 return false;
5895 return true;
5897 return false;
5899 case BUILT_IN_ASSUME_ALIGNED:
5900 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5902 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5904 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5905 "function %qE", fndecl);
5906 return false;
5908 return true;
5910 return false;
5912 case BUILT_IN_ADD_OVERFLOW:
5913 case BUILT_IN_SUB_OVERFLOW:
5914 case BUILT_IN_MUL_OVERFLOW:
5915 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5917 unsigned i;
5918 for (i = 0; i < 2; i++)
5919 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5921 error_at (ARG_LOCATION (i), "argument %u in call to function "
5922 "%qE does not have integral type", i + 1, fndecl);
5923 return false;
5925 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5926 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5928 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5929 "does not have pointer to integral type", fndecl);
5930 return false;
5932 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5934 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5935 "has pointer to enumerated type", fndecl);
5936 return false;
5938 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5940 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5941 "has pointer to boolean type", fndecl);
5942 return false;
5944 return true;
5946 return false;
5948 case BUILT_IN_ADD_OVERFLOW_P:
5949 case BUILT_IN_SUB_OVERFLOW_P:
5950 case BUILT_IN_MUL_OVERFLOW_P:
5951 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5953 unsigned i;
5954 for (i = 0; i < 3; i++)
5955 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5957 error_at (ARG_LOCATION (i), "argument %u in call to function "
5958 "%qE does not have integral type", i + 1, fndecl);
5959 return false;
5961 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5963 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5964 "%qE has enumerated type", fndecl);
5965 return false;
5967 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5969 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5970 "%qE has boolean type", fndecl);
5971 return false;
5973 return true;
5975 return false;
5977 default:
5978 return true;
5982 /* Subroutine of c_parse_error.
5983 Return the result of concatenating LHS and RHS. RHS is really
5984 a string literal, its first character is indicated by RHS_START and
5985 RHS_SIZE is its length (including the terminating NUL character).
5987 The caller is responsible for deleting the returned pointer. */
5989 static char *
5990 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5992 const size_t lhs_size = strlen (lhs);
5993 char *result = XNEWVEC (char, lhs_size + rhs_size);
5994 memcpy (result, lhs, lhs_size);
5995 memcpy (result + lhs_size, rhs_start, rhs_size);
5996 return result;
5999 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6000 before TOKEN, which had the associated VALUE. */
6002 void
6003 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6004 tree value, unsigned char token_flags,
6005 rich_location *richloc)
6007 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6009 char *message = NULL;
6011 if (token_type == CPP_EOF)
6012 message = catenate_messages (gmsgid, " at end of input");
6013 else if (token_type == CPP_CHAR
6014 || token_type == CPP_WCHAR
6015 || token_type == CPP_CHAR16
6016 || token_type == CPP_CHAR32
6017 || token_type == CPP_UTF8CHAR)
6019 unsigned int val = TREE_INT_CST_LOW (value);
6020 const char *prefix;
6022 switch (token_type)
6024 default:
6025 prefix = "";
6026 break;
6027 case CPP_WCHAR:
6028 prefix = "L";
6029 break;
6030 case CPP_CHAR16:
6031 prefix = "u";
6032 break;
6033 case CPP_CHAR32:
6034 prefix = "U";
6035 break;
6036 case CPP_UTF8CHAR:
6037 prefix = "u8";
6038 break;
6041 if (val <= UCHAR_MAX && ISGRAPH (val))
6042 message = catenate_messages (gmsgid, " before %s'%c'");
6043 else
6044 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6046 error_at (richloc, message, prefix, val);
6047 free (message);
6048 message = NULL;
6050 else if (token_type == CPP_CHAR_USERDEF
6051 || token_type == CPP_WCHAR_USERDEF
6052 || token_type == CPP_CHAR16_USERDEF
6053 || token_type == CPP_CHAR32_USERDEF
6054 || token_type == CPP_UTF8CHAR_USERDEF)
6055 message = catenate_messages (gmsgid,
6056 " before user-defined character literal");
6057 else if (token_type == CPP_STRING_USERDEF
6058 || token_type == CPP_WSTRING_USERDEF
6059 || token_type == CPP_STRING16_USERDEF
6060 || token_type == CPP_STRING32_USERDEF
6061 || token_type == CPP_UTF8STRING_USERDEF)
6062 message = catenate_messages (gmsgid, " before user-defined string literal");
6063 else if (token_type == CPP_STRING
6064 || token_type == CPP_WSTRING
6065 || token_type == CPP_STRING16
6066 || token_type == CPP_STRING32
6067 || token_type == CPP_UTF8STRING)
6068 message = catenate_messages (gmsgid, " before string constant");
6069 else if (token_type == CPP_NUMBER)
6070 message = catenate_messages (gmsgid, " before numeric constant");
6071 else if (token_type == CPP_NAME)
6073 message = catenate_messages (gmsgid, " before %qE");
6074 error_at (richloc, message, value);
6075 free (message);
6076 message = NULL;
6078 else if (token_type == CPP_PRAGMA)
6079 message = catenate_messages (gmsgid, " before %<#pragma%>");
6080 else if (token_type == CPP_PRAGMA_EOL)
6081 message = catenate_messages (gmsgid, " before end of line");
6082 else if (token_type == CPP_DECLTYPE)
6083 message = catenate_messages (gmsgid, " before %<decltype%>");
6084 else if (token_type < N_TTYPES)
6086 message = catenate_messages (gmsgid, " before %qs token");
6087 error_at (richloc, message, cpp_type2name (token_type, token_flags));
6088 free (message);
6089 message = NULL;
6091 else
6092 error_at (richloc, gmsgid);
6094 if (message)
6096 error_at (richloc, message);
6097 free (message);
6099 #undef catenate_messages
6102 /* Return the gcc option code associated with the reason for a cpp
6103 message, or 0 if none. */
6105 static int
6106 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
6108 const struct cpp_reason_option_codes_t *entry;
6110 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6112 if (entry->reason == reason)
6113 return entry->option_code;
6115 return 0;
6118 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6119 preprocessor. The diagnostic is of type LEVEL, with REASON set
6120 to the reason code if LEVEL is represents a warning, at location
6121 RICHLOC unless this is after lexing and the compiler's location
6122 should be used instead; MSG is the translated message and AP
6123 the arguments. Returns true if a diagnostic was emitted, false
6124 otherwise. */
6126 bool
6127 c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
6128 enum cpp_diagnostic_level level,
6129 enum cpp_warning_reason reason,
6130 rich_location *richloc,
6131 const char *msg, va_list *ap)
6133 diagnostic_info diagnostic;
6134 diagnostic_t dlevel;
6135 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6136 bool ret;
6138 switch (level)
6140 case CPP_DL_WARNING_SYSHDR:
6141 if (flag_no_output)
6142 return false;
6143 global_dc->dc_warn_system_headers = 1;
6144 /* Fall through. */
6145 case CPP_DL_WARNING:
6146 if (flag_no_output)
6147 return false;
6148 dlevel = DK_WARNING;
6149 break;
6150 case CPP_DL_PEDWARN:
6151 if (flag_no_output && !flag_pedantic_errors)
6152 return false;
6153 dlevel = DK_PEDWARN;
6154 break;
6155 case CPP_DL_ERROR:
6156 dlevel = DK_ERROR;
6157 break;
6158 case CPP_DL_ICE:
6159 dlevel = DK_ICE;
6160 break;
6161 case CPP_DL_NOTE:
6162 dlevel = DK_NOTE;
6163 break;
6164 case CPP_DL_FATAL:
6165 dlevel = DK_FATAL;
6166 break;
6167 default:
6168 gcc_unreachable ();
6170 if (done_lexing)
6171 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
6172 diagnostic_set_info_translated (&diagnostic, msg, ap,
6173 richloc, dlevel);
6174 diagnostic_override_option_index
6175 (&diagnostic,
6176 c_option_controlling_cpp_diagnostic (reason));
6177 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
6178 if (level == CPP_DL_WARNING_SYSHDR)
6179 global_dc->dc_warn_system_headers = save_warn_system_headers;
6180 return ret;
6183 /* Convert a character from the host to the target execution character
6184 set. cpplib handles this, mostly. */
6186 HOST_WIDE_INT
6187 c_common_to_target_charset (HOST_WIDE_INT c)
6189 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6190 zero-extended under -fno-signed-char. cpplib insists that characters
6191 and character constants are always unsigned. Hence we must convert
6192 back and forth. */
6193 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6195 uc = cpp_host_to_exec_charset (parse_in, uc);
6197 if (flag_signed_char)
6198 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6199 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6200 else
6201 return uc;
6204 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6205 references with an INDIRECT_REF of a constant at the bottom; much like the
6206 traditional rendering of offsetof as a macro. TYPE is the desired type of
6207 the whole expression. Return the folded result. */
6209 tree
6210 fold_offsetof (tree expr, tree type, enum tree_code ctx)
6212 tree base, off, t;
6213 tree_code code = TREE_CODE (expr);
6214 switch (code)
6216 case ERROR_MARK:
6217 return expr;
6219 case VAR_DECL:
6220 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6221 return error_mark_node;
6223 case CALL_EXPR:
6224 case TARGET_EXPR:
6225 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6226 return error_mark_node;
6228 case NOP_EXPR:
6229 case INDIRECT_REF:
6230 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6232 error ("cannot apply %<offsetof%> to a non constant address");
6233 return error_mark_node;
6235 return convert (type, TREE_OPERAND (expr, 0));
6237 case COMPONENT_REF:
6238 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6239 if (base == error_mark_node)
6240 return base;
6242 t = TREE_OPERAND (expr, 1);
6243 if (DECL_C_BIT_FIELD (t))
6245 error ("attempt to take address of bit-field structure "
6246 "member %qD", t);
6247 return error_mark_node;
6249 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6250 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6251 / BITS_PER_UNIT));
6252 break;
6254 case ARRAY_REF:
6255 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6256 if (base == error_mark_node)
6257 return base;
6259 t = TREE_OPERAND (expr, 1);
6261 /* Check if the offset goes beyond the upper bound of the array. */
6262 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6264 tree upbound = array_ref_up_bound (expr);
6265 if (upbound != NULL_TREE
6266 && TREE_CODE (upbound) == INTEGER_CST
6267 && !tree_int_cst_equal (upbound,
6268 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6270 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6271 upbound = size_binop (PLUS_EXPR, upbound,
6272 build_int_cst (TREE_TYPE (upbound), 1));
6273 if (tree_int_cst_lt (upbound, t))
6275 tree v;
6277 for (v = TREE_OPERAND (expr, 0);
6278 TREE_CODE (v) == COMPONENT_REF;
6279 v = TREE_OPERAND (v, 0))
6280 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6281 == RECORD_TYPE)
6283 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6284 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6285 if (TREE_CODE (fld_chain) == FIELD_DECL)
6286 break;
6288 if (fld_chain)
6289 break;
6291 /* Don't warn if the array might be considered a poor
6292 man's flexible array member with a very permissive
6293 definition thereof. */
6294 if (TREE_CODE (v) == ARRAY_REF
6295 || TREE_CODE (v) == COMPONENT_REF)
6296 warning (OPT_Warray_bounds,
6297 "index %E denotes an offset "
6298 "greater than size of %qT",
6299 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6304 t = convert (sizetype, t);
6305 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6306 break;
6308 case COMPOUND_EXPR:
6309 /* Handle static members of volatile structs. */
6310 t = TREE_OPERAND (expr, 1);
6311 gcc_checking_assert (VAR_P (get_base_address (t)));
6312 return fold_offsetof (t, type);
6314 default:
6315 gcc_unreachable ();
6318 if (!POINTER_TYPE_P (type))
6319 return size_binop (PLUS_EXPR, base, convert (type, off));
6320 return fold_build_pointer_plus (base, off);
6323 /* *PTYPE is an incomplete array. Complete it with a domain based on
6324 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6325 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6326 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6329 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6331 tree maxindex, type, main_type, elt, unqual_elt;
6332 int failure = 0, quals;
6333 bool overflow_p = false;
6335 maxindex = size_zero_node;
6336 if (initial_value)
6338 if (TREE_CODE (initial_value) == STRING_CST)
6340 int eltsize
6341 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6342 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6344 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6346 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6348 if (vec_safe_is_empty (v))
6350 if (pedantic)
6351 failure = 3;
6352 maxindex = ssize_int (-1);
6354 else
6356 tree curindex;
6357 unsigned HOST_WIDE_INT cnt;
6358 constructor_elt *ce;
6359 bool fold_p = false;
6361 if ((*v)[0].index)
6362 maxindex = (*v)[0].index, fold_p = true;
6364 curindex = maxindex;
6366 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6368 bool curfold_p = false;
6369 if (ce->index)
6370 curindex = ce->index, curfold_p = true;
6371 else
6373 if (fold_p)
6375 /* Since we treat size types now as ordinary
6376 unsigned types, we need an explicit overflow
6377 check. */
6378 tree orig = curindex;
6379 curindex = fold_convert (sizetype, curindex);
6380 overflow_p |= tree_int_cst_lt (curindex, orig);
6382 curindex = size_binop (PLUS_EXPR, curindex,
6383 size_one_node);
6385 if (tree_int_cst_lt (maxindex, curindex))
6386 maxindex = curindex, fold_p = curfold_p;
6388 if (fold_p)
6390 tree orig = maxindex;
6391 maxindex = fold_convert (sizetype, maxindex);
6392 overflow_p |= tree_int_cst_lt (maxindex, orig);
6396 else
6398 /* Make an error message unless that happened already. */
6399 if (initial_value != error_mark_node)
6400 failure = 1;
6403 else
6405 failure = 2;
6406 if (!do_default)
6407 return failure;
6410 type = *ptype;
6411 elt = TREE_TYPE (type);
6412 quals = TYPE_QUALS (strip_array_types (elt));
6413 if (quals == 0)
6414 unqual_elt = elt;
6415 else
6416 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6418 /* Using build_distinct_type_copy and modifying things afterward instead
6419 of using build_array_type to create a new type preserves all of the
6420 TYPE_LANG_FLAG_? bits that the front end may have set. */
6421 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6422 TREE_TYPE (main_type) = unqual_elt;
6423 TYPE_DOMAIN (main_type)
6424 = build_range_type (TREE_TYPE (maxindex),
6425 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6426 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
6427 layout_type (main_type);
6429 /* Make sure we have the canonical MAIN_TYPE. */
6430 hashval_t hashcode = type_hash_canon_hash (main_type);
6431 main_type = type_hash_canon (hashcode, main_type);
6433 /* Fix the canonical type. */
6434 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6435 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6436 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6437 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6438 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6439 != TYPE_DOMAIN (main_type)))
6440 TYPE_CANONICAL (main_type)
6441 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6442 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6443 TYPE_TYPELESS_STORAGE (main_type));
6444 else
6445 TYPE_CANONICAL (main_type) = main_type;
6447 if (quals == 0)
6448 type = main_type;
6449 else
6450 type = c_build_qualified_type (main_type, quals);
6452 if (COMPLETE_TYPE_P (type)
6453 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6454 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6456 error ("size of array is too large");
6457 /* If we proceed with the array type as it is, we'll eventually
6458 crash in tree_to_[su]hwi(). */
6459 type = error_mark_node;
6462 *ptype = type;
6463 return failure;
6466 /* INIT is an constructor of a structure with a flexible array member.
6467 Complete the flexible array member with a domain based on it's value. */
6468 void
6469 complete_flexible_array_elts (tree init)
6471 tree elt, type;
6473 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
6474 return;
6476 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
6477 return;
6479 elt = CONSTRUCTOR_ELTS (init)->last ().value;
6480 type = TREE_TYPE (elt);
6481 if (TREE_CODE (type) == ARRAY_TYPE
6482 && TYPE_SIZE (type) == NULL_TREE)
6483 complete_array_type (&TREE_TYPE (elt), elt, false);
6484 else
6485 complete_flexible_array_elts (elt);
6488 /* Like c_mark_addressable but don't check register qualifier. */
6489 void
6490 c_common_mark_addressable_vec (tree t)
6492 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6493 t = C_MAYBE_CONST_EXPR_EXPR (t);
6494 while (handled_component_p (t))
6495 t = TREE_OPERAND (t, 0);
6496 if (!VAR_P (t)
6497 && TREE_CODE (t) != PARM_DECL
6498 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6499 return;
6500 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6501 TREE_ADDRESSABLE (t) = 1;
6506 /* Used to help initialize the builtin-types.def table. When a type of
6507 the correct size doesn't exist, use error_mark_node instead of NULL.
6508 The later results in segfaults even when a decl using the type doesn't
6509 get invoked. */
6511 tree
6512 builtin_type_for_size (int size, bool unsignedp)
6514 tree type = c_common_type_for_size (size, unsignedp);
6515 return type ? type : error_mark_node;
6518 /* Work out the size of the first argument of a call to
6519 __builtin_speculation_safe_value. Only pointers and integral types
6520 are permitted. Return -1 if the argument type is not supported or
6521 the size is too large; 0 if the argument type is a pointer or the
6522 size if it is integral. */
6523 static enum built_in_function
6524 speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params)
6526 /* Type of the argument. */
6527 tree type;
6528 int size;
6530 if (vec_safe_is_empty (params))
6532 error ("too few arguments to function %qE", function);
6533 return BUILT_IN_NONE;
6536 type = TREE_TYPE ((*params)[0]);
6537 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6539 /* Force array-to-pointer decay for C++. */
6540 (*params)[0] = default_conversion ((*params)[0]);
6541 type = TREE_TYPE ((*params)[0]);
6544 if (POINTER_TYPE_P (type))
6545 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
6547 if (!INTEGRAL_TYPE_P (type))
6548 goto incompatible;
6550 if (!COMPLETE_TYPE_P (type))
6551 goto incompatible;
6553 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6554 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6555 return ((enum built_in_function)
6556 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
6558 incompatible:
6559 /* Issue the diagnostic only if the argument is valid, otherwise
6560 it would be redundant at best and could be misleading. */
6561 if (type != error_mark_node)
6562 error ("operand type %qT is incompatible with argument %d of %qE",
6563 type, 1, function);
6565 return BUILT_IN_NONE;
6568 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6569 the prototype for FUNCTION. The first argument is mandatory, a second
6570 argument, if present, must be type compatible with the first. */
6571 static bool
6572 speculation_safe_value_resolve_params (location_t loc, tree orig_function,
6573 vec<tree, va_gc> *params)
6575 tree val;
6577 if (params->length () == 0)
6579 error_at (loc, "too few arguments to function %qE", orig_function);
6580 return false;
6583 else if (params->length () > 2)
6585 error_at (loc, "too many arguments to function %qE", orig_function);
6586 return false;
6589 val = (*params)[0];
6590 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
6591 val = default_conversion (val);
6592 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
6593 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
6595 error_at (loc,
6596 "expecting argument of type pointer or of type integer "
6597 "for argument 1");
6598 return false;
6600 (*params)[0] = val;
6602 if (params->length () == 2)
6604 tree val2 = (*params)[1];
6605 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
6606 val2 = default_conversion (val2);
6607 if (!(TREE_TYPE (val) == TREE_TYPE (val2)
6608 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
6610 error_at (loc, "both arguments must be compatible");
6611 return false;
6613 (*params)[1] = val2;
6616 return true;
6619 /* Cast the result of the builtin back to the type of the first argument,
6620 preserving any qualifiers that it might have. */
6621 static tree
6622 speculation_safe_value_resolve_return (tree first_param, tree result)
6624 tree ptype = TREE_TYPE (first_param);
6625 tree rtype = TREE_TYPE (result);
6626 ptype = TYPE_MAIN_VARIANT (ptype);
6628 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6629 return convert (ptype, result);
6631 return result;
6634 /* A helper function for resolve_overloaded_builtin in resolving the
6635 overloaded __sync_ builtins. Returns a positive power of 2 if the
6636 first operand of PARAMS is a pointer to a supported data type.
6637 Returns 0 if an error is encountered.
6638 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6639 built-ins. */
6641 static int
6642 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6644 /* Type of the argument. */
6645 tree argtype;
6646 /* Type the argument points to. */
6647 tree type;
6648 int size;
6650 if (vec_safe_is_empty (params))
6652 error ("too few arguments to function %qE", function);
6653 return 0;
6656 argtype = type = TREE_TYPE ((*params)[0]);
6657 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6659 /* Force array-to-pointer decay for C++. */
6660 (*params)[0] = default_conversion ((*params)[0]);
6661 type = TREE_TYPE ((*params)[0]);
6663 if (TREE_CODE (type) != POINTER_TYPE)
6664 goto incompatible;
6666 type = TREE_TYPE (type);
6667 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6668 goto incompatible;
6670 if (!COMPLETE_TYPE_P (type))
6671 goto incompatible;
6673 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6674 goto incompatible;
6676 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6677 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6678 return size;
6680 incompatible:
6681 /* Issue the diagnostic only if the argument is valid, otherwise
6682 it would be redundant at best and could be misleading. */
6683 if (argtype != error_mark_node)
6684 error ("operand type %qT is incompatible with argument %d of %qE",
6685 argtype, 1, function);
6686 return 0;
6689 /* A helper function for resolve_overloaded_builtin. Adds casts to
6690 PARAMS to make arguments match up with those of FUNCTION. Drops
6691 the variadic arguments at the end. Returns false if some error
6692 was encountered; true on success. */
6694 static bool
6695 sync_resolve_params (location_t loc, tree orig_function, tree function,
6696 vec<tree, va_gc> *params, bool orig_format)
6698 function_args_iterator iter;
6699 tree ptype;
6700 unsigned int parmnum;
6702 function_args_iter_init (&iter, TREE_TYPE (function));
6703 /* We've declared the implementation functions to use "volatile void *"
6704 as the pointer parameter, so we shouldn't get any complaints from the
6705 call to check_function_arguments what ever type the user used. */
6706 function_args_iter_next (&iter);
6707 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6708 ptype = TYPE_MAIN_VARIANT (ptype);
6710 /* For the rest of the values, we need to cast these to FTYPE, so that we
6711 don't get warnings for passing pointer types, etc. */
6712 parmnum = 0;
6713 while (1)
6715 tree val, arg_type;
6717 arg_type = function_args_iter_cond (&iter);
6718 /* XXX void_type_node belies the abstraction. */
6719 if (arg_type == void_type_node)
6720 break;
6722 ++parmnum;
6723 if (params->length () <= parmnum)
6725 error_at (loc, "too few arguments to function %qE", orig_function);
6726 return false;
6729 /* Only convert parameters if arg_type is unsigned integer type with
6730 new format sync routines, i.e. don't attempt to convert pointer
6731 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6732 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6733 kinds). */
6734 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6736 /* Ideally for the first conversion we'd use convert_for_assignment
6737 so that we get warnings for anything that doesn't match the pointer
6738 type. This isn't portable across the C and C++ front ends atm. */
6739 val = (*params)[parmnum];
6740 val = convert (ptype, val);
6741 val = convert (arg_type, val);
6742 (*params)[parmnum] = val;
6745 function_args_iter_next (&iter);
6748 /* __atomic routines are not variadic. */
6749 if (!orig_format && params->length () != parmnum + 1)
6751 error_at (loc, "too many arguments to function %qE", orig_function);
6752 return false;
6755 /* The definition of these primitives is variadic, with the remaining
6756 being "an optional list of variables protected by the memory barrier".
6757 No clue what that's supposed to mean, precisely, but we consider all
6758 call-clobbered variables to be protected so we're safe. */
6759 params->truncate (parmnum + 1);
6761 return true;
6764 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6765 RESULT to make it match the type of the first pointer argument in
6766 PARAMS. */
6768 static tree
6769 sync_resolve_return (tree first_param, tree result, bool orig_format)
6771 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6772 tree rtype = TREE_TYPE (result);
6773 ptype = TYPE_MAIN_VARIANT (ptype);
6775 /* New format doesn't require casting unless the types are the same size. */
6776 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6777 return convert (ptype, result);
6778 else
6779 return result;
6782 /* This function verifies the PARAMS to generic atomic FUNCTION.
6783 It returns the size if all the parameters are the same size, otherwise
6784 0 is returned if the parameters are invalid. */
6786 static int
6787 get_atomic_generic_size (location_t loc, tree function,
6788 vec<tree, va_gc> *params)
6790 unsigned int n_param;
6791 unsigned int n_model;
6792 unsigned int x;
6793 int size_0;
6794 tree type_0;
6796 /* Determine the parameter makeup. */
6797 switch (DECL_FUNCTION_CODE (function))
6799 case BUILT_IN_ATOMIC_EXCHANGE:
6800 n_param = 4;
6801 n_model = 1;
6802 break;
6803 case BUILT_IN_ATOMIC_LOAD:
6804 case BUILT_IN_ATOMIC_STORE:
6805 n_param = 3;
6806 n_model = 1;
6807 break;
6808 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6809 n_param = 6;
6810 n_model = 2;
6811 break;
6812 default:
6813 gcc_unreachable ();
6816 if (vec_safe_length (params) != n_param)
6818 error_at (loc, "incorrect number of arguments to function %qE", function);
6819 return 0;
6822 /* Get type of first parameter, and determine its size. */
6823 type_0 = TREE_TYPE ((*params)[0]);
6824 if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
6826 /* Force array-to-pointer decay for C++. */
6827 (*params)[0] = default_conversion ((*params)[0]);
6828 type_0 = TREE_TYPE ((*params)[0]);
6830 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6832 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6833 function);
6834 return 0;
6837 /* Types must be compile time constant sizes. */
6838 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6840 error_at (loc,
6841 "argument 1 of %qE must be a pointer to a constant size type",
6842 function);
6843 return 0;
6846 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6848 /* Zero size objects are not allowed. */
6849 if (size_0 == 0)
6851 error_at (loc,
6852 "argument 1 of %qE must be a pointer to a nonzero size object",
6853 function);
6854 return 0;
6857 /* Check each other parameter is a pointer and the same size. */
6858 for (x = 0; x < n_param - n_model; x++)
6860 int size;
6861 tree type = TREE_TYPE ((*params)[x]);
6862 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6863 if (n_param == 6 && x == 3)
6864 continue;
6865 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6867 /* Force array-to-pointer decay for C++. */
6868 (*params)[x] = default_conversion ((*params)[x]);
6869 type = TREE_TYPE ((*params)[x]);
6871 if (!POINTER_TYPE_P (type))
6873 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6874 function);
6875 return 0;
6877 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6878 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6879 != INTEGER_CST)
6881 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6882 "size type", x + 1, function);
6883 return 0;
6885 else if (FUNCTION_POINTER_TYPE_P (type))
6887 error_at (loc, "argument %d of %qE must not be a pointer to a "
6888 "function", x + 1, function);
6889 return 0;
6891 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6892 size = type_size ? tree_to_uhwi (type_size) : 0;
6893 if (size != size_0)
6895 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6896 function);
6897 return 0;
6901 /* Check memory model parameters for validity. */
6902 for (x = n_param - n_model ; x < n_param; x++)
6904 tree p = (*params)[x];
6905 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6907 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6908 function);
6909 return 0;
6911 p = fold_for_warn (p);
6912 if (TREE_CODE (p) == INTEGER_CST)
6914 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6915 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6916 bits will be checked later during expansion in target specific
6917 way. */
6918 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6919 warning_at (loc, OPT_Winvalid_memory_model,
6920 "invalid memory model argument %d of %qE", x + 1,
6921 function);
6925 return size_0;
6929 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6930 at the beginning of the parameter list PARAMS representing the size of the
6931 objects. This is to match the library ABI requirement. LOC is the location
6932 of the function call.
6933 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6934 returned to allow the external call to be constructed. */
6936 static tree
6937 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6938 vec<tree, va_gc> *params)
6940 tree size_node;
6942 /* Insert a SIZE_T parameter as the first param. If there isn't
6943 enough space, allocate a new vector and recursively re-build with that. */
6944 if (!params->space (1))
6946 unsigned int z, len;
6947 vec<tree, va_gc> *v;
6948 tree f;
6950 len = params->length ();
6951 vec_alloc (v, len + 1);
6952 v->quick_push (build_int_cst (size_type_node, n));
6953 for (z = 0; z < len; z++)
6954 v->quick_push ((*params)[z]);
6955 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6956 vec_free (v);
6957 return f;
6960 /* Add the size parameter and leave as a function call for processing. */
6961 size_node = build_int_cst (size_type_node, n);
6962 params->quick_insert (0, size_node);
6963 return NULL_TREE;
6967 /* Return whether atomic operations for naturally aligned N-byte
6968 arguments are supported, whether inline or through libatomic. */
6969 static bool
6970 atomic_size_supported_p (int n)
6972 switch (n)
6974 case 1:
6975 case 2:
6976 case 4:
6977 case 8:
6978 return true;
6980 case 16:
6981 return targetm.scalar_mode_supported_p (TImode);
6983 default:
6984 return false;
6988 /* This will process an __atomic_exchange function call, determine whether it
6989 needs to be mapped to the _N variation, or turned into a library call.
6990 LOC is the location of the builtin call.
6991 FUNCTION is the DECL that has been invoked;
6992 PARAMS is the argument list for the call. The return value is non-null
6993 TRUE is returned if it is translated into the proper format for a call to the
6994 external library, and NEW_RETURN is set the tree for that function.
6995 FALSE is returned if processing for the _N variation is required, and
6996 NEW_RETURN is set to the return value the result is copied into. */
6997 static bool
6998 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6999 vec<tree, va_gc> *params, tree *new_return)
7001 tree p0, p1, p2, p3;
7002 tree I_type, I_type_ptr;
7003 int n = get_atomic_generic_size (loc, function, params);
7005 /* Size of 0 is an error condition. */
7006 if (n == 0)
7008 *new_return = error_mark_node;
7009 return true;
7012 /* If not a lock-free size, change to the library generic format. */
7013 if (!atomic_size_supported_p (n))
7015 *new_return = add_atomic_size_parameter (n, loc, function, params);
7016 return true;
7019 /* Otherwise there is a lockfree match, transform the call from:
7020 void fn(T* mem, T* desired, T* return, model)
7021 into
7022 *return = (T) (fn (In* mem, (In) *desired, model)) */
7024 p0 = (*params)[0];
7025 p1 = (*params)[1];
7026 p2 = (*params)[2];
7027 p3 = (*params)[3];
7029 /* Create pointer to appropriate size. */
7030 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7031 I_type_ptr = build_pointer_type (I_type);
7033 /* Convert object pointer to required type. */
7034 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7035 (*params)[0] = p0;
7036 /* Convert new value to required type, and dereference it. */
7037 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7038 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7039 (*params)[1] = p1;
7041 /* Move memory model to the 3rd position, and end param list. */
7042 (*params)[2] = p3;
7043 params->truncate (3);
7045 /* Convert return pointer and dereference it for later assignment. */
7046 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7048 return false;
7052 /* This will process an __atomic_compare_exchange function call, determine
7053 whether it needs to be mapped to the _N variation, or turned into a lib call.
7054 LOC is the location of the builtin call.
7055 FUNCTION is the DECL that has been invoked;
7056 PARAMS is the argument list for the call. The return value is non-null
7057 TRUE is returned if it is translated into the proper format for a call to the
7058 external library, and NEW_RETURN is set the tree for that function.
7059 FALSE is returned if processing for the _N variation is required. */
7061 static bool
7062 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
7063 vec<tree, va_gc> *params,
7064 tree *new_return)
7066 tree p0, p1, p2;
7067 tree I_type, I_type_ptr;
7068 int n = get_atomic_generic_size (loc, function, params);
7070 /* Size of 0 is an error condition. */
7071 if (n == 0)
7073 *new_return = error_mark_node;
7074 return true;
7077 /* If not a lock-free size, change to the library generic format. */
7078 if (!atomic_size_supported_p (n))
7080 /* The library generic format does not have the weak parameter, so
7081 remove it from the param list. Since a parameter has been removed,
7082 we can be sure that there is room for the SIZE_T parameter, meaning
7083 there will not be a recursive rebuilding of the parameter list, so
7084 there is no danger this will be done twice. */
7085 if (n > 0)
7087 (*params)[3] = (*params)[4];
7088 (*params)[4] = (*params)[5];
7089 params->truncate (5);
7091 *new_return = add_atomic_size_parameter (n, loc, function, params);
7092 return true;
7095 /* Otherwise, there is a match, so the call needs to be transformed from:
7096 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7097 into
7098 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7100 p0 = (*params)[0];
7101 p1 = (*params)[1];
7102 p2 = (*params)[2];
7104 /* Create pointer to appropriate size. */
7105 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7106 I_type_ptr = build_pointer_type (I_type);
7108 /* Convert object pointer to required type. */
7109 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7110 (*params)[0] = p0;
7112 /* Convert expected pointer to required type. */
7113 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
7114 (*params)[1] = p1;
7116 /* Convert desired value to required type, and dereference it. */
7117 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7118 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
7119 (*params)[2] = p2;
7121 /* The rest of the parameters are fine. NULL means no special return value
7122 processing.*/
7123 *new_return = NULL;
7124 return false;
7128 /* This will process an __atomic_load function call, determine whether it
7129 needs to be mapped to the _N variation, or turned into a library call.
7130 LOC is the location of the builtin call.
7131 FUNCTION is the DECL that has been invoked;
7132 PARAMS is the argument list for the call. The return value is non-null
7133 TRUE is returned if it is translated into the proper format for a call to the
7134 external library, and NEW_RETURN is set the tree for that function.
7135 FALSE is returned if processing for the _N variation is required, and
7136 NEW_RETURN is set to the return value the result is copied into. */
7138 static bool
7139 resolve_overloaded_atomic_load (location_t loc, tree function,
7140 vec<tree, va_gc> *params, tree *new_return)
7142 tree p0, p1, p2;
7143 tree I_type, I_type_ptr;
7144 int n = get_atomic_generic_size (loc, function, params);
7146 /* Size of 0 is an error condition. */
7147 if (n == 0)
7149 *new_return = error_mark_node;
7150 return true;
7153 /* If not a lock-free size, change to the library generic format. */
7154 if (!atomic_size_supported_p (n))
7156 *new_return = add_atomic_size_parameter (n, loc, function, params);
7157 return true;
7160 /* Otherwise, there is a match, so the call needs to be transformed from:
7161 void fn(T* mem, T* return, model)
7162 into
7163 *return = (T) (fn ((In *) mem, model)) */
7165 p0 = (*params)[0];
7166 p1 = (*params)[1];
7167 p2 = (*params)[2];
7169 /* Create pointer to appropriate size. */
7170 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7171 I_type_ptr = build_pointer_type (I_type);
7173 /* Convert object pointer to required type. */
7174 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7175 (*params)[0] = p0;
7177 /* Move memory model to the 2nd position, and end param list. */
7178 (*params)[1] = p2;
7179 params->truncate (2);
7181 /* Convert return pointer and dereference it for later assignment. */
7182 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7184 return false;
7188 /* This will process an __atomic_store function call, determine whether it
7189 needs to be mapped to the _N variation, or turned into a library call.
7190 LOC is the location of the builtin call.
7191 FUNCTION is the DECL that has been invoked;
7192 PARAMS is the argument list for the call. The return value is non-null
7193 TRUE is returned if it is translated into the proper format for a call to the
7194 external library, and NEW_RETURN is set the tree for that function.
7195 FALSE is returned if processing for the _N variation is required, and
7196 NEW_RETURN is set to the return value the result is copied into. */
7198 static bool
7199 resolve_overloaded_atomic_store (location_t loc, tree function,
7200 vec<tree, va_gc> *params, tree *new_return)
7202 tree p0, p1;
7203 tree I_type, I_type_ptr;
7204 int n = get_atomic_generic_size (loc, function, params);
7206 /* Size of 0 is an error condition. */
7207 if (n == 0)
7209 *new_return = error_mark_node;
7210 return true;
7213 /* If not a lock-free size, change to the library generic format. */
7214 if (!atomic_size_supported_p (n))
7216 *new_return = add_atomic_size_parameter (n, loc, function, params);
7217 return true;
7220 /* Otherwise, there is a match, so the call needs to be transformed from:
7221 void fn(T* mem, T* value, model)
7222 into
7223 fn ((In *) mem, (In) *value, model) */
7225 p0 = (*params)[0];
7226 p1 = (*params)[1];
7228 /* Create pointer to appropriate size. */
7229 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7230 I_type_ptr = build_pointer_type (I_type);
7232 /* Convert object pointer to required type. */
7233 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7234 (*params)[0] = p0;
7236 /* Convert new value to required type, and dereference it. */
7237 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7238 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7239 (*params)[1] = p1;
7241 /* The memory model is in the right spot already. Return is void. */
7242 *new_return = NULL_TREE;
7244 return false;
7248 /* Some builtin functions are placeholders for other expressions. This
7249 function should be called immediately after parsing the call expression
7250 before surrounding code has committed to the type of the expression.
7252 LOC is the location of the builtin call.
7254 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7255 PARAMS is the argument list for the call. The return value is non-null
7256 when expansion is complete, and null if normal processing should
7257 continue. */
7259 tree
7260 resolve_overloaded_builtin (location_t loc, tree function,
7261 vec<tree, va_gc> *params)
7263 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7265 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7266 Those are not valid to call with a pointer to _Bool (or C++ bool)
7267 and so must be rejected. */
7268 bool fetch_op = true;
7269 bool orig_format = true;
7270 tree new_return = NULL_TREE;
7272 switch (DECL_BUILT_IN_CLASS (function))
7274 case BUILT_IN_NORMAL:
7275 break;
7276 case BUILT_IN_MD:
7277 if (targetm.resolve_overloaded_builtin)
7278 return targetm.resolve_overloaded_builtin (loc, function, params);
7279 else
7280 return NULL_TREE;
7281 default:
7282 return NULL_TREE;
7285 /* Handle BUILT_IN_NORMAL here. */
7286 switch (orig_code)
7288 case BUILT_IN_SPECULATION_SAFE_VALUE_N:
7290 tree new_function, first_param, result;
7291 enum built_in_function fncode
7292 = speculation_safe_value_resolve_call (function, params);;
7294 first_param = (*params)[0];
7295 if (fncode == BUILT_IN_NONE
7296 || !speculation_safe_value_resolve_params (loc, function, params))
7297 return error_mark_node;
7299 if (targetm.have_speculation_safe_value (true))
7301 new_function = builtin_decl_explicit (fncode);
7302 result = build_function_call_vec (loc, vNULL, new_function, params,
7303 NULL);
7305 if (result == error_mark_node)
7306 return result;
7308 return speculation_safe_value_resolve_return (first_param, result);
7310 else
7312 /* This target doesn't have, or doesn't need, active mitigation
7313 against incorrect speculative execution. Simply return the
7314 first parameter to the builtin. */
7315 if (!targetm.have_speculation_safe_value (false))
7316 /* The user has invoked __builtin_speculation_safe_value
7317 even though __HAVE_SPECULATION_SAFE_VALUE is not
7318 defined: emit a warning. */
7319 warning_at (input_location, 0,
7320 "this target does not define a speculation barrier; "
7321 "your program will still execute correctly, "
7322 "but incorrect speculation may not be be "
7323 "restricted");
7325 /* If the optional second argument is present, handle any side
7326 effects now. */
7327 if (params->length () == 2
7328 && TREE_SIDE_EFFECTS ((*params)[1]))
7329 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
7330 (*params)[1], first_param);
7332 return first_param;
7336 case BUILT_IN_ATOMIC_EXCHANGE:
7337 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7338 case BUILT_IN_ATOMIC_LOAD:
7339 case BUILT_IN_ATOMIC_STORE:
7341 /* Handle these 4 together so that they can fall through to the next
7342 case if the call is transformed to an _N variant. */
7343 switch (orig_code)
7345 case BUILT_IN_ATOMIC_EXCHANGE:
7347 if (resolve_overloaded_atomic_exchange (loc, function, params,
7348 &new_return))
7349 return new_return;
7350 /* Change to the _N variant. */
7351 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7352 break;
7355 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7357 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7358 params,
7359 &new_return))
7360 return new_return;
7361 /* Change to the _N variant. */
7362 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7363 break;
7365 case BUILT_IN_ATOMIC_LOAD:
7367 if (resolve_overloaded_atomic_load (loc, function, params,
7368 &new_return))
7369 return new_return;
7370 /* Change to the _N variant. */
7371 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7372 break;
7374 case BUILT_IN_ATOMIC_STORE:
7376 if (resolve_overloaded_atomic_store (loc, function, params,
7377 &new_return))
7378 return new_return;
7379 /* Change to the _N variant. */
7380 orig_code = BUILT_IN_ATOMIC_STORE_N;
7381 break;
7383 default:
7384 gcc_unreachable ();
7387 /* FALLTHRU */
7388 case BUILT_IN_ATOMIC_EXCHANGE_N:
7389 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7390 case BUILT_IN_ATOMIC_LOAD_N:
7391 case BUILT_IN_ATOMIC_STORE_N:
7392 fetch_op = false;
7393 /* FALLTHRU */
7394 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7395 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7396 case BUILT_IN_ATOMIC_AND_FETCH_N:
7397 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7398 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7399 case BUILT_IN_ATOMIC_OR_FETCH_N:
7400 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7401 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7402 case BUILT_IN_ATOMIC_FETCH_AND_N:
7403 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7404 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7405 case BUILT_IN_ATOMIC_FETCH_OR_N:
7406 orig_format = false;
7407 /* FALLTHRU */
7408 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7409 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7410 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7411 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7412 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7413 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7414 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7415 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7416 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7417 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7418 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7419 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7420 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7421 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7422 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7423 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7425 /* The following are not _FETCH_OPs and must be accepted with
7426 pointers to _Bool (or C++ bool). */
7427 if (fetch_op)
7428 fetch_op =
7429 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7430 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7431 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7432 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7434 int n = sync_resolve_size (function, params, fetch_op);
7435 tree new_function, first_param, result;
7436 enum built_in_function fncode;
7438 if (n == 0)
7439 return error_mark_node;
7441 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7442 new_function = builtin_decl_explicit (fncode);
7443 if (!sync_resolve_params (loc, function, new_function, params,
7444 orig_format))
7445 return error_mark_node;
7447 first_param = (*params)[0];
7448 result = build_function_call_vec (loc, vNULL, new_function, params,
7449 NULL);
7450 if (result == error_mark_node)
7451 return result;
7452 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7453 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7454 && orig_code != BUILT_IN_ATOMIC_STORE_N
7455 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7456 result = sync_resolve_return (first_param, result, orig_format);
7458 if (fetch_op)
7459 /* Prevent -Wunused-value warning. */
7460 TREE_USED (result) = true;
7462 /* If new_return is set, assign function to that expr and cast the
7463 result to void since the generic interface returned void. */
7464 if (new_return)
7466 /* Cast function result from I{1,2,4,8,16} to the required type. */
7467 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7468 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7469 result);
7470 TREE_SIDE_EFFECTS (result) = 1;
7471 protected_set_expr_location (result, loc);
7472 result = convert (void_type_node, result);
7474 return result;
7477 default:
7478 return NULL_TREE;
7482 /* vector_types_compatible_elements_p is used in type checks of vectors
7483 values used as operands of binary operators. Where it returns true, and
7484 the other checks of the caller succeed (being vector types in he first
7485 place, and matching number of elements), we can just treat the types
7486 as essentially the same.
7487 Contrast with vector_targets_convertible_p, which is used for vector
7488 pointer types, and vector_types_convertible_p, which will allow
7489 language-specific matches under the control of flag_lax_vector_conversions,
7490 and might still require a conversion. */
7491 /* True if vector types T1 and T2 can be inputs to the same binary
7492 operator without conversion.
7493 We don't check the overall vector size here because some of our callers
7494 want to give different error messages when the vectors are compatible
7495 except for the element count. */
7497 bool
7498 vector_types_compatible_elements_p (tree t1, tree t2)
7500 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7501 t1 = TREE_TYPE (t1);
7502 t2 = TREE_TYPE (t2);
7504 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7506 gcc_assert ((INTEGRAL_TYPE_P (t1)
7507 || c1 == REAL_TYPE
7508 || c1 == FIXED_POINT_TYPE)
7509 && (INTEGRAL_TYPE_P (t2)
7510 || c2 == REAL_TYPE
7511 || c2 == FIXED_POINT_TYPE));
7513 t1 = c_common_signed_type (t1);
7514 t2 = c_common_signed_type (t2);
7515 /* Equality works here because c_common_signed_type uses
7516 TYPE_MAIN_VARIANT. */
7517 if (t1 == t2)
7518 return true;
7519 if (opaque && c1 == c2
7520 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
7521 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7522 return true;
7523 return false;
7526 /* Check for missing format attributes on function pointers. LTYPE is
7527 the new type or left-hand side type. RTYPE is the old type or
7528 right-hand side type. Returns TRUE if LTYPE is missing the desired
7529 attribute. */
7531 bool
7532 check_missing_format_attribute (tree ltype, tree rtype)
7534 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7535 tree ra;
7537 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7538 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7539 break;
7540 if (ra)
7542 tree la;
7543 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7544 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7545 break;
7546 return !la;
7548 else
7549 return false;
7552 /* Setup a TYPE_DECL node as a typedef representation.
7554 X is a TYPE_DECL for a typedef statement. Create a brand new
7555 ..._TYPE node (which will be just a variant of the existing
7556 ..._TYPE node with identical properties) and then install X
7557 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7559 The whole point here is to end up with a situation where each
7560 and every ..._TYPE node the compiler creates will be uniquely
7561 associated with AT MOST one node representing a typedef name.
7562 This way, even though the compiler substitutes corresponding
7563 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7564 early on, later parts of the compiler can always do the reverse
7565 translation and get back the corresponding typedef name. For
7566 example, given:
7568 typedef struct S MY_TYPE;
7569 MY_TYPE object;
7571 Later parts of the compiler might only know that `object' was of
7572 type `struct S' if it were not for code just below. With this
7573 code however, later parts of the compiler see something like:
7575 struct S' == struct S
7576 typedef struct S' MY_TYPE;
7577 struct S' object;
7579 And they can then deduce (from the node for type struct S') that
7580 the original object declaration was:
7582 MY_TYPE object;
7584 Being able to do this is important for proper support of protoize,
7585 and also for generating precise symbolic debugging information
7586 which takes full account of the programmer's (typedef) vocabulary.
7588 Obviously, we don't want to generate a duplicate ..._TYPE node if
7589 the TYPE_DECL node that we are now processing really represents a
7590 standard built-in type. */
7592 void
7593 set_underlying_type (tree x)
7595 if (x == error_mark_node)
7596 return;
7597 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7599 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7600 TYPE_NAME (TREE_TYPE (x)) = x;
7602 else if (TREE_TYPE (x) != error_mark_node
7603 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7605 tree tt = TREE_TYPE (x);
7606 DECL_ORIGINAL_TYPE (x) = tt;
7607 tt = build_variant_type_copy (tt);
7608 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7609 TYPE_NAME (tt) = x;
7611 /* Mark the type as used only when its type decl is decorated
7612 with attribute unused. */
7613 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7614 TREE_USED (tt) = 1;
7616 TREE_TYPE (x) = tt;
7620 /* Record the types used by the current global variable declaration
7621 being parsed, so that we can decide later to emit their debug info.
7622 Those types are in types_used_by_cur_var_decl, and we are going to
7623 store them in the types_used_by_vars_hash hash table.
7624 DECL is the declaration of the global variable that has been parsed. */
7626 void
7627 record_types_used_by_current_var_decl (tree decl)
7629 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7631 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7633 tree type = types_used_by_cur_var_decl->pop ();
7634 types_used_by_var_decl_insert (type, decl);
7638 /* The C and C++ parsers both use vectors to hold function arguments.
7639 For efficiency, we keep a cache of unused vectors. This is the
7640 cache. */
7642 typedef vec<tree, va_gc> *tree_gc_vec;
7643 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7645 /* Return a new vector from the cache. If the cache is empty,
7646 allocate a new vector. These vectors are GC'ed, so it is OK if the
7647 pointer is not released.. */
7649 vec<tree, va_gc> *
7650 make_tree_vector (void)
7652 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7653 return tree_vector_cache->pop ();
7654 else
7656 /* Passing 0 to vec::alloc returns NULL, and our callers require
7657 that we always return a non-NULL value. The vector code uses
7658 4 when growing a NULL vector, so we do too. */
7659 vec<tree, va_gc> *v;
7660 vec_alloc (v, 4);
7661 return v;
7665 /* Release a vector of trees back to the cache. */
7667 void
7668 release_tree_vector (vec<tree, va_gc> *vec)
7670 if (vec != NULL)
7672 vec->truncate (0);
7673 vec_safe_push (tree_vector_cache, vec);
7677 /* Get a new tree vector holding a single tree. */
7679 vec<tree, va_gc> *
7680 make_tree_vector_single (tree t)
7682 vec<tree, va_gc> *ret = make_tree_vector ();
7683 ret->quick_push (t);
7684 return ret;
7687 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7689 vec<tree, va_gc> *
7690 make_tree_vector_from_list (tree list)
7692 vec<tree, va_gc> *ret = make_tree_vector ();
7693 for (; list; list = TREE_CHAIN (list))
7694 vec_safe_push (ret, TREE_VALUE (list));
7695 return ret;
7698 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7700 vec<tree, va_gc> *
7701 make_tree_vector_from_ctor (tree ctor)
7703 vec<tree,va_gc> *ret = make_tree_vector ();
7704 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7705 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7706 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7707 return ret;
7710 /* Get a new tree vector which is a copy of an existing one. */
7712 vec<tree, va_gc> *
7713 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7715 vec<tree, va_gc> *ret;
7716 unsigned int ix;
7717 tree t;
7719 ret = make_tree_vector ();
7720 vec_safe_reserve (ret, vec_safe_length (orig));
7721 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7722 ret->quick_push (t);
7723 return ret;
7726 /* Return true if KEYWORD starts a type specifier. */
7728 bool
7729 keyword_begins_type_specifier (enum rid keyword)
7731 switch (keyword)
7733 case RID_AUTO_TYPE:
7734 case RID_INT:
7735 case RID_CHAR:
7736 case RID_FLOAT:
7737 case RID_DOUBLE:
7738 case RID_VOID:
7739 case RID_UNSIGNED:
7740 case RID_LONG:
7741 case RID_SHORT:
7742 case RID_SIGNED:
7743 CASE_RID_FLOATN_NX:
7744 case RID_DFLOAT32:
7745 case RID_DFLOAT64:
7746 case RID_DFLOAT128:
7747 case RID_FRACT:
7748 case RID_ACCUM:
7749 case RID_BOOL:
7750 case RID_WCHAR:
7751 case RID_CHAR16:
7752 case RID_CHAR32:
7753 case RID_SAT:
7754 case RID_COMPLEX:
7755 case RID_TYPEOF:
7756 case RID_STRUCT:
7757 case RID_CLASS:
7758 case RID_UNION:
7759 case RID_ENUM:
7760 return true;
7761 default:
7762 if (keyword >= RID_FIRST_INT_N
7763 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7764 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7765 return true;
7766 return false;
7770 /* Return true if KEYWORD names a type qualifier. */
7772 bool
7773 keyword_is_type_qualifier (enum rid keyword)
7775 switch (keyword)
7777 case RID_CONST:
7778 case RID_VOLATILE:
7779 case RID_RESTRICT:
7780 case RID_ATOMIC:
7781 return true;
7782 default:
7783 return false;
7787 /* Return true if KEYWORD names a storage class specifier.
7789 RID_TYPEDEF is not included in this list despite `typedef' being
7790 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7791 such for syntactic convenience only. */
7793 bool
7794 keyword_is_storage_class_specifier (enum rid keyword)
7796 switch (keyword)
7798 case RID_STATIC:
7799 case RID_EXTERN:
7800 case RID_REGISTER:
7801 case RID_AUTO:
7802 case RID_MUTABLE:
7803 case RID_THREAD:
7804 return true;
7805 default:
7806 return false;
7810 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7812 static bool
7813 keyword_is_function_specifier (enum rid keyword)
7815 switch (keyword)
7817 case RID_INLINE:
7818 case RID_NORETURN:
7819 case RID_VIRTUAL:
7820 case RID_EXPLICIT:
7821 return true;
7822 default:
7823 return false;
7827 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7828 declaration-specifier (C99 6.7). */
7830 bool
7831 keyword_is_decl_specifier (enum rid keyword)
7833 if (keyword_is_storage_class_specifier (keyword)
7834 || keyword_is_type_qualifier (keyword)
7835 || keyword_is_function_specifier (keyword))
7836 return true;
7838 switch (keyword)
7840 case RID_TYPEDEF:
7841 case RID_FRIEND:
7842 case RID_CONSTEXPR:
7843 return true;
7844 default:
7845 return false;
7849 /* Initialize language-specific-bits of tree_contains_struct. */
7851 void
7852 c_common_init_ts (void)
7854 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7855 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7858 /* Build a user-defined numeric literal out of an integer constant type VALUE
7859 with identifier SUFFIX. */
7861 tree
7862 build_userdef_literal (tree suffix_id, tree value,
7863 enum overflow_type overflow, tree num_string)
7865 tree literal = make_node (USERDEF_LITERAL);
7866 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7867 USERDEF_LITERAL_VALUE (literal) = value;
7868 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7869 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7870 return literal;
7873 /* For vector[index], convert the vector to an array of the underlying type.
7874 Return true if the resulting ARRAY_REF should not be an lvalue. */
7876 bool
7877 convert_vector_to_array_for_subscript (location_t loc,
7878 tree *vecp, tree index)
7880 bool ret = false;
7881 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7883 tree type = TREE_TYPE (*vecp);
7885 ret = !lvalue_p (*vecp);
7887 if (TREE_CODE (index) == INTEGER_CST)
7888 if (!tree_fits_uhwi_p (index)
7889 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7890 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7892 /* We are building an ARRAY_REF so mark the vector as addressable
7893 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7894 for function parameters. */
7895 c_common_mark_addressable_vec (*vecp);
7897 *vecp = build1 (VIEW_CONVERT_EXPR,
7898 build_array_type_nelts (TREE_TYPE (type),
7899 TYPE_VECTOR_SUBPARTS (type)),
7900 *vecp);
7902 return ret;
7905 /* Determine which of the operands, if any, is a scalar that needs to be
7906 converted to a vector, for the range of operations. */
7907 enum stv_conv
7908 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7909 bool complain)
7911 tree type0 = TREE_TYPE (op0);
7912 tree type1 = TREE_TYPE (op1);
7913 bool integer_only_op = false;
7914 enum stv_conv ret = stv_firstarg;
7916 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7917 switch (code)
7919 /* Most GENERIC binary expressions require homogeneous arguments.
7920 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7921 argument that is a vector and a second one that is a scalar, so
7922 we never return stv_secondarg for them. */
7923 case RSHIFT_EXPR:
7924 case LSHIFT_EXPR:
7925 if (TREE_CODE (type0) == INTEGER_TYPE
7926 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7928 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7929 NULL_TREE, false))
7931 if (complain)
7932 error_at (loc, "conversion of scalar %qT to vector %qT "
7933 "involves truncation", type0, type1);
7934 return stv_error;
7936 else
7937 return stv_firstarg;
7939 break;
7941 case BIT_IOR_EXPR:
7942 case BIT_XOR_EXPR:
7943 case BIT_AND_EXPR:
7944 integer_only_op = true;
7945 /* fall through */
7947 case VEC_COND_EXPR:
7949 case PLUS_EXPR:
7950 case MINUS_EXPR:
7951 case MULT_EXPR:
7952 case TRUNC_DIV_EXPR:
7953 case CEIL_DIV_EXPR:
7954 case FLOOR_DIV_EXPR:
7955 case ROUND_DIV_EXPR:
7956 case EXACT_DIV_EXPR:
7957 case TRUNC_MOD_EXPR:
7958 case FLOOR_MOD_EXPR:
7959 case RDIV_EXPR:
7960 case EQ_EXPR:
7961 case NE_EXPR:
7962 case LE_EXPR:
7963 case GE_EXPR:
7964 case LT_EXPR:
7965 case GT_EXPR:
7966 /* What about UNLT_EXPR? */
7967 if (VECTOR_TYPE_P (type0))
7969 ret = stv_secondarg;
7970 std::swap (type0, type1);
7971 std::swap (op0, op1);
7974 if (TREE_CODE (type0) == INTEGER_TYPE
7975 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7977 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7978 NULL_TREE, false))
7980 if (complain)
7981 error_at (loc, "conversion of scalar %qT to vector %qT "
7982 "involves truncation", type0, type1);
7983 return stv_error;
7985 return ret;
7987 else if (!integer_only_op
7988 /* Allow integer --> real conversion if safe. */
7989 && (TREE_CODE (type0) == REAL_TYPE
7990 || TREE_CODE (type0) == INTEGER_TYPE)
7991 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7993 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7994 NULL_TREE, false))
7996 if (complain)
7997 error_at (loc, "conversion of scalar %qT to vector %qT "
7998 "involves truncation", type0, type1);
7999 return stv_error;
8001 return ret;
8003 default:
8004 break;
8007 return stv_nothing;
8010 /* Return the alignment of std::max_align_t.
8012 [support.types.layout] The type max_align_t is a POD type whose alignment
8013 requirement is at least as great as that of every scalar type, and whose
8014 alignment requirement is supported in every context. */
8016 unsigned
8017 max_align_t_align ()
8019 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
8020 TYPE_ALIGN (long_double_type_node));
8021 if (float128_type_node != NULL_TREE)
8022 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
8023 return max_align;
8026 /* Return true iff ALIGN is an integral constant that is a fundamental
8027 alignment, as defined by [basic.align] in the c++-11
8028 specifications.
8030 That is:
8032 [A fundamental alignment is represented by an alignment less than or
8033 equal to the greatest alignment supported by the implementation
8034 in all contexts, which is equal to alignof(max_align_t)]. */
8036 bool
8037 cxx_fundamental_alignment_p (unsigned align)
8039 return (align <= max_align_t_align ());
8042 /* Return true if T is a pointer to a zero-sized aggregate. */
8044 bool
8045 pointer_to_zero_sized_aggr_p (tree t)
8047 if (!POINTER_TYPE_P (t))
8048 return false;
8049 t = TREE_TYPE (t);
8050 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
8053 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8054 with no library fallback or for an ADDR_EXPR whose operand is such type
8055 issues an error pointing to the location LOC.
8056 Returns true when the expression has been diagnosed and false
8057 otherwise. */
8059 bool
8060 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
8062 if (TREE_CODE (expr) == ADDR_EXPR)
8063 expr = TREE_OPERAND (expr, 0);
8065 STRIP_ANY_LOCATION_WRAPPER (expr);
8067 if (TREE_TYPE (expr)
8068 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
8069 && TREE_CODE (expr) == FUNCTION_DECL
8070 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8071 false positives for user-declared built-ins such as abs or
8072 strlen, and for C++ operators new and delete.
8073 The c_decl_implicit() test avoids false positives for implicitly
8074 declared built-ins with library fallbacks (such as abs). */
8075 && fndecl_built_in_p (expr)
8076 && DECL_IS_BUILTIN (expr)
8077 && !c_decl_implicit (expr)
8078 && !DECL_ASSEMBLER_NAME_SET_P (expr))
8080 if (loc == UNKNOWN_LOCATION)
8081 loc = EXPR_LOC_OR_LOC (expr, input_location);
8083 /* Reject arguments that are built-in functions with
8084 no library fallback. */
8085 error_at (loc, "built-in function %qE must be directly called", expr);
8087 return true;
8090 return false;
8093 /* Check if array size calculations overflow or if the array covers more
8094 than half of the address space. Return true if the size of the array
8095 is valid, false otherwise. TYPE is the type of the array and NAME is
8096 the name of the array, or NULL_TREE for unnamed arrays. */
8098 bool
8099 valid_array_size_p (location_t loc, tree type, tree name, bool complain)
8101 if (type != error_mark_node
8102 && COMPLETE_TYPE_P (type)
8103 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8104 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
8106 if (complain)
8108 if (name)
8109 error_at (loc, "size of array %qE is too large", name);
8110 else
8111 error_at (loc, "size of unnamed array is too large");
8113 return false;
8115 return true;
8118 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8119 timestamp to replace embedded current dates to get reproducible
8120 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
8122 time_t
8123 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
8125 char *source_date_epoch;
8126 int64_t epoch;
8127 char *endptr;
8129 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
8130 if (!source_date_epoch)
8131 return (time_t) -1;
8133 errno = 0;
8134 #if defined(INT64_T_IS_LONG)
8135 epoch = strtol (source_date_epoch, &endptr, 10);
8136 #else
8137 epoch = strtoll (source_date_epoch, &endptr, 10);
8138 #endif
8139 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
8140 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
8142 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
8143 "expand to a non-negative integer less than or equal to %wd",
8144 MAX_SOURCE_DATE_EPOCH);
8145 return (time_t) -1;
8148 return (time_t) epoch;
8151 /* Callback for libcpp for offering spelling suggestions for misspelled
8152 directives. GOAL is an unrecognized string; CANDIDATES is a
8153 NULL-terminated array of candidate strings. Return the closest
8154 match to GOAL within CANDIDATES, or NULL if none are good
8155 suggestions. */
8157 const char *
8158 cb_get_suggestion (cpp_reader *, const char *goal,
8159 const char *const *candidates)
8161 best_match<const char *, const char *> bm (goal);
8162 while (*candidates)
8163 bm.consider (*candidates++);
8164 return bm.get_best_meaningful_candidate ();
8167 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8168 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8169 by C TS 18661-3 for interchange types that are computed in their
8170 native precision are larger than the C11 values for evaluating in the
8171 precision of float/double/long double. If either mode is
8172 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8174 enum flt_eval_method
8175 excess_precision_mode_join (enum flt_eval_method x,
8176 enum flt_eval_method y)
8178 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8179 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8180 return FLT_EVAL_METHOD_UNPREDICTABLE;
8182 /* GCC only supports one interchange type right now, _Float16. If
8183 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8184 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8185 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8186 return y;
8187 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8188 return x;
8190 /* Other values for flt_eval_method are directly comparable, and we want
8191 the maximum. */
8192 return MAX (x, y);
8195 /* Return the value that should be set for FLT_EVAL_METHOD in the
8196 context of ISO/IEC TS 18861-3.
8198 This relates to the effective excess precision seen by the user,
8199 which is the join point of the precision the target requests for
8200 -fexcess-precision={standard,fast} and the implicit excess precision
8201 the target uses. */
8203 static enum flt_eval_method
8204 c_ts18661_flt_eval_method (void)
8206 enum flt_eval_method implicit
8207 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8209 enum excess_precision_type flag_type
8210 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8211 ? EXCESS_PRECISION_TYPE_STANDARD
8212 : EXCESS_PRECISION_TYPE_FAST);
8214 enum flt_eval_method requested
8215 = targetm.c.excess_precision (flag_type);
8217 return excess_precision_mode_join (implicit, requested);
8220 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8221 those that were permitted by C11. That is to say, eliminates
8222 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8224 static enum flt_eval_method
8225 c_c11_flt_eval_method (void)
8227 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8228 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8231 /* Return the value that should be set for FLT_EVAL_METHOD.
8232 MAYBE_C11_ONLY_P is TRUE if we should check
8233 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8234 values we can return to those from C99/C11, and FALSE otherwise.
8235 See the comments on c_ts18661_flt_eval_method for what value we choose
8236 to set here. */
8239 c_flt_eval_method (bool maybe_c11_only_p)
8241 if (maybe_c11_only_p
8242 && flag_permitted_flt_eval_methods
8243 == PERMITTED_FLT_EVAL_METHODS_C11)
8244 return c_c11_flt_eval_method ();
8245 else
8246 return c_ts18661_flt_eval_method ();
8249 /* An enum for get_missing_token_insertion_kind for describing the best
8250 place to insert a missing token, if there is one. */
8252 enum missing_token_insertion_kind
8254 MTIK_IMPOSSIBLE,
8255 MTIK_INSERT_BEFORE_NEXT,
8256 MTIK_INSERT_AFTER_PREV
8259 /* Given a missing token of TYPE, determine if it is reasonable to
8260 emit a fix-it hint suggesting the insertion of the token, and,
8261 if so, where the token should be inserted relative to other tokens.
8263 It only makes sense to do this for values of TYPE that are symbols.
8265 Some symbols should go before the next token, e.g. in:
8266 if flag)
8267 we want to insert the missing '(' immediately before "flag",
8268 giving:
8269 if (flag)
8270 rather than:
8271 if( flag)
8272 These use MTIK_INSERT_BEFORE_NEXT.
8274 Other symbols should go after the previous token, e.g. in:
8275 if (flag
8276 do_something ();
8277 we want to insert the missing ')' immediately after the "flag",
8278 giving:
8279 if (flag)
8280 do_something ();
8281 rather than:
8282 if (flag
8283 )do_something ();
8284 These use MTIK_INSERT_AFTER_PREV. */
8286 static enum missing_token_insertion_kind
8287 get_missing_token_insertion_kind (enum cpp_ttype type)
8289 switch (type)
8291 /* Insert missing "opening" brackets immediately
8292 before the next token. */
8293 case CPP_OPEN_SQUARE:
8294 case CPP_OPEN_PAREN:
8295 return MTIK_INSERT_BEFORE_NEXT;
8297 /* Insert other missing symbols immediately after
8298 the previous token. */
8299 case CPP_CLOSE_PAREN:
8300 case CPP_CLOSE_SQUARE:
8301 case CPP_SEMICOLON:
8302 case CPP_COMMA:
8303 case CPP_COLON:
8304 return MTIK_INSERT_AFTER_PREV;
8306 /* Other kinds of token don't get fix-it hints. */
8307 default:
8308 return MTIK_IMPOSSIBLE;
8312 /* Given RICHLOC, a location for a diagnostic describing a missing token
8313 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8314 insertion of the token.
8316 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8317 it will either be:
8318 (a) immediately after PREV_TOKEN_LOC, or
8320 (b) immediately before the primary location within RICHLOC (taken to
8321 be that of the token following where the token was expected).
8323 If we manage to add a fix-it hint, then the location of the
8324 fix-it hint is likely to be more useful as the primary location
8325 of the diagnostic than that of the following token, so we swap
8326 these locations.
8328 For example, given this bogus code:
8329 123456789012345678901234567890
8330 1 | int missing_semicolon (void)
8331 2 | {
8332 3 | return 42
8333 4 | }
8335 we will emit:
8337 "expected ';' before '}'"
8339 RICHLOC's primary location is at the closing brace, so before "swapping"
8340 we would emit the error at line 4 column 1:
8342 123456789012345678901234567890
8343 3 | return 42 |< fix-it hint emitted for this line
8344 | ; |
8345 4 | } |< "expected ';' before '}'" emitted at this line
8346 | ^ |
8348 It's more useful for the location of the diagnostic to be at the
8349 fix-it hint, so we swap the locations, so the primary location
8350 is at the fix-it hint, with the old primary location inserted
8351 as a secondary location, giving this, with the error at line 3
8352 column 12:
8354 123456789012345678901234567890
8355 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8356 | ^ | with fix-it hint
8357 4 | ; |
8358 | } |< secondary range emitted here
8359 | ~ |. */
8361 void
8362 maybe_suggest_missing_token_insertion (rich_location *richloc,
8363 enum cpp_ttype token_type,
8364 location_t prev_token_loc)
8366 gcc_assert (richloc);
8368 enum missing_token_insertion_kind mtik
8369 = get_missing_token_insertion_kind (token_type);
8371 switch (mtik)
8373 default:
8374 gcc_unreachable ();
8375 break;
8377 case MTIK_IMPOSSIBLE:
8378 return;
8380 case MTIK_INSERT_BEFORE_NEXT:
8381 /* Attempt to add the fix-it hint before the primary location
8382 of RICHLOC. */
8383 richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
8384 break;
8386 case MTIK_INSERT_AFTER_PREV:
8387 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8388 richloc->add_fixit_insert_after (prev_token_loc,
8389 cpp_type2name (token_type, 0));
8390 break;
8393 /* If we were successful, use the fix-it hint's location as the
8394 primary location within RICHLOC, adding the old primary location
8395 back as a secondary location. */
8396 if (!richloc->seen_impossible_fixit_p ())
8398 fixit_hint *hint = richloc->get_last_fixit_hint ();
8399 location_t hint_loc = hint->get_start_loc ();
8400 location_t old_loc = richloc->get_loc ();
8402 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
8403 richloc->add_range (old_loc);
8407 #if CHECKING_P
8409 namespace selftest {
8411 /* Verify that fold_for_warn on error_mark_node is safe. */
8413 static void
8414 test_fold_for_warn ()
8416 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8419 /* Run all of the selftests within this file. */
8421 static void
8422 c_common_c_tests ()
8424 test_fold_for_warn ();
8427 /* Run all of the tests within c-family. */
8429 void
8430 c_family_tests (void)
8432 c_common_c_tests ();
8433 c_format_c_tests ();
8434 c_indentation_c_tests ();
8435 c_pretty_print_c_tests ();
8436 c_spellcheck_cc_tests ();
8439 } // namespace selftest
8441 #endif /* #if CHECKING_P */
8443 /* Attempt to locate a suitable location within FILE for a
8444 #include directive to be inserted before. FILE should
8445 be a string from libcpp (pointer equality is used).
8446 LOC is the location of the relevant diagnostic.
8448 Attempt to return the location within FILE immediately
8449 after the last #include within that file, or the start of
8450 that file if it has no #include directives.
8452 Return UNKNOWN_LOCATION if no suitable location is found,
8453 or if an error occurs. */
8455 static location_t
8456 try_to_locate_new_include_insertion_point (const char *file, location_t loc)
8458 /* Locate the last ordinary map within FILE that ended with a #include. */
8459 const line_map_ordinary *last_include_ord_map = NULL;
8461 /* ...and the next ordinary map within FILE after that one. */
8462 const line_map_ordinary *last_ord_map_after_include = NULL;
8464 /* ...and the first ordinary map within FILE. */
8465 const line_map_ordinary *first_ord_map_in_file = NULL;
8467 /* Get ordinary map containing LOC (or its expansion). */
8468 const line_map_ordinary *ord_map_for_loc = NULL;
8469 loc = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
8470 &ord_map_for_loc);
8471 gcc_assert (ord_map_for_loc);
8473 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8475 const line_map_ordinary *ord_map
8476 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8478 if (const line_map_ordinary *from
8479 = linemap_included_from_linemap (line_table, ord_map))
8480 if (from->to_file == file)
8482 last_include_ord_map = from;
8483 last_ord_map_after_include = NULL;
8486 if (ord_map->to_file == file)
8488 if (!first_ord_map_in_file)
8489 first_ord_map_in_file = ord_map;
8490 if (last_include_ord_map && !last_ord_map_after_include)
8491 last_ord_map_after_include = ord_map;
8494 /* Stop searching when reaching the ord_map containing LOC,
8495 as it makes no sense to provide fix-it hints that appear
8496 after the diagnostic in question. */
8497 if (ord_map == ord_map_for_loc)
8498 break;
8501 /* Determine where to insert the #include. */
8502 const line_map_ordinary *ord_map_for_insertion;
8504 /* We want the next ordmap in the file after the last one that's a
8505 #include, but failing that, the start of the file. */
8506 if (last_ord_map_after_include)
8507 ord_map_for_insertion = last_ord_map_after_include;
8508 else
8509 ord_map_for_insertion = first_ord_map_in_file;
8511 if (!ord_map_for_insertion)
8512 return UNKNOWN_LOCATION;
8514 /* The "start_location" is column 0, meaning "the whole line".
8515 rich_location and edit_context can't cope with this, so use
8516 column 1 instead. */
8517 location_t col_0 = ord_map_for_insertion->start_location;
8518 return linemap_position_for_loc_and_offset (line_table, col_0, 1);
8521 /* A map from filenames to sets of headers added to them, for
8522 ensuring idempotency within maybe_add_include_fixit. */
8524 /* The values within the map. We need string comparison as there's
8525 no guarantee that two different diagnostics that are recommending
8526 adding e.g. "<stdio.h>" are using the same buffer. */
8528 typedef hash_set <const char *, nofree_string_hash> per_file_includes_t;
8530 /* The map itself. We don't need string comparison for the filename keys,
8531 as they come from libcpp. */
8533 typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
8534 static added_includes_t *added_includes;
8536 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8537 in a suitable location within the file of RICHLOC's primary
8538 location.
8540 This function is idempotent: a header will be added at most once to
8541 any given file.
8543 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8544 printed, then RICHLOC's primary location will be replaced by that of
8545 the fix-it hint (for use by "inform" notes where the location of the
8546 issue has already been reported). */
8548 void
8549 maybe_add_include_fixit (rich_location *richloc, const char *header,
8550 bool override_location)
8552 location_t loc = richloc->get_loc ();
8553 const char *file = LOCATION_FILE (loc);
8554 if (!file)
8555 return;
8557 /* Idempotency: don't add the same header more than once to a given file. */
8558 if (!added_includes)
8559 added_includes = new added_includes_t ();
8560 per_file_includes_t *&set = added_includes->get_or_insert (file);
8561 if (set)
8562 if (set->contains (header))
8563 /* ...then we've already added HEADER to that file. */
8564 return;
8565 if (!set)
8566 set = new per_file_includes_t ();
8567 set->add (header);
8569 /* Attempt to locate a suitable place for the new directive. */
8570 location_t include_insert_loc
8571 = try_to_locate_new_include_insertion_point (file, loc);
8572 if (include_insert_loc == UNKNOWN_LOCATION)
8573 return;
8575 char *text = xasprintf ("#include %s\n", header);
8576 richloc->add_fixit_insert_before (include_insert_loc, text);
8577 free (text);
8579 if (override_location && global_dc->show_caret)
8581 /* Replace the primary location with that of the insertion point for the
8582 fix-it hint.
8584 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8585 caret for the insertion point (or colorize it).
8587 Hence we print e.g.:
8589 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8590 73 | # include <debug/vector>
8591 +++ |+#include <vector>
8592 74 | #endif
8594 rather than:
8596 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8597 73 | # include <debug/vector>
8598 +++ |+#include <vector>
8599 74 | #endif
8602 avoiding the caret on the first column of line 74. */
8603 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
8607 /* Attempt to convert a braced array initializer list CTOR for array
8608 TYPE into a STRING_CST for convenience and efficiency. Return
8609 the converted string on success or the original ctor on failure. */
8611 tree
8612 braced_list_to_string (tree type, tree ctor)
8614 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
8615 return ctor;
8617 /* If the array has an explicit bound, use it to constrain the size
8618 of the string. If it doesn't, be sure to create a string that's
8619 as long as implied by the index of the last zero specified via
8620 a designator, as in:
8621 const char a[] = { [7] = 0 }; */
8622 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8623 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8625 /* Avoid converting initializers for zero-length arrays. */
8626 if (!maxelts)
8627 return ctor;
8629 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
8631 auto_vec<char> str;
8632 str.reserve (nelts + 1);
8634 unsigned HOST_WIDE_INT i;
8635 tree index, value;
8637 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
8639 unsigned HOST_WIDE_INT idx = i;
8640 if (index)
8642 if (!tree_fits_uhwi_p (index))
8643 return ctor;
8644 idx = tree_to_uhwi (index);
8647 /* auto_vec is limited to UINT_MAX elements. */
8648 if (idx > UINT_MAX)
8649 return ctor;
8651 /* Avoid non-constant initializers. */
8652 if (!tree_fits_shwi_p (value))
8653 return ctor;
8655 /* Skip over embedded nuls except the last one (initializer
8656 elements are in ascending order of indices). */
8657 HOST_WIDE_INT val = tree_to_shwi (value);
8658 if (!val && i + 1 < nelts)
8659 continue;
8661 if (idx < str.length())
8662 return ctor;
8664 /* Bail if the CTOR has a block of more than 256 embedded nuls
8665 due to implicitly initialized elements. */
8666 unsigned nchars = (idx - str.length ()) + 1;
8667 if (nchars > 256)
8668 return ctor;
8670 if (nchars > 1)
8672 str.reserve (idx);
8673 str.quick_grow_cleared (idx);
8676 if (idx >= maxelts)
8677 return ctor;
8679 str.safe_insert (idx, val);
8682 /* Append a nul string termination. */
8683 if (str.length () < maxelts)
8684 str.safe_push (0);
8686 /* Build a STRING_CST with the same type as the array. */
8687 tree res = build_string (str.length (), str.begin ());
8688 TREE_TYPE (res) = type;
8689 return res;
8692 #include "gt-c-family-c-common.h"