[ARM] Fix typo in comment in arm_expand_prologue
[official-gcc.git] / gcc / c-family / c-common.c
blob5f4488a908064cfa92797c6a670d3778d6da11bf
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2017 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"
52 cpp_reader *parse_in; /* Declared in c-pragma.h. */
54 /* Mode used to build pointers (VOIDmode means ptr_mode). */
56 machine_mode c_default_pointer_mode = VOIDmode;
58 /* The following symbols are subsumed in the c_global_trees array, and
59 listed here individually for documentation purposes.
61 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
63 tree short_integer_type_node;
64 tree long_integer_type_node;
65 tree long_long_integer_type_node;
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
75 tree ptrdiff_type_node;
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
81 tree char16_type_node;
82 tree char32_type_node;
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
88 tree complex_integer_type_node;
89 tree complex_float_type_node;
90 tree complex_double_type_node;
91 tree complex_long_double_type_node;
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
97 tree intQI_type_node;
98 tree intHI_type_node;
99 tree intSI_type_node;
100 tree intDI_type_node;
101 tree intTI_type_node;
103 tree unsigned_intQI_type_node;
104 tree unsigned_intHI_type_node;
105 tree unsigned_intSI_type_node;
106 tree unsigned_intDI_type_node;
107 tree unsigned_intTI_type_node;
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
112 Nodes for types `void *' and `const void *'.
114 tree ptr_type_node, const_ptr_type_node;
116 Nodes for types `char *' and `const char *'.
118 tree string_type_node, const_string_type_node;
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
123 tree char_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
206 int flag_isoc94;
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 int flag_isoc99;
212 /* Nonzero means use the ISO C11 dialect of C. */
214 int flag_isoc11;
216 /* Nonzero means that we have builtin functions, and main is an int. */
218 int flag_hosted = 1;
221 /* ObjC language option variables. */
224 /* Tells the compiler that this is a special run. Do not perform any
225 compiling, instead we are to test some platform dependent features
226 and output a C header file with appropriate definitions. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
235 /* C++ language option variables. */
237 /* The reference version of the ABI for -Wabi. */
239 int warn_abi_version = -1;
241 /* Nonzero means generate separate instantiation control files and
242 juggle them at link time. */
244 int flag_use_repository;
246 /* The C++ dialect being used. Default set in c_common_post_options. */
248 enum cxx_dialect cxx_dialect = cxx_unset;
250 /* Maximum template instantiation depth. This limit exists to limit the
251 time it takes to notice excessively recursive template instantiations.
253 The default is lower than the 1024 recommended by the C++0x standard
254 because G++ runs out of stack before 1024 with highly recursive template
255 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
257 int max_tinst_depth = 900;
259 /* The elements of `ridpointers' are identifier nodes for the reserved
260 type names and storage classes. It is indexed by a RID_... value. */
261 tree *ridpointers;
263 tree (*make_fname_decl) (location_t, tree, int);
265 /* Nonzero means don't warn about problems that occur when the code is
266 executed. */
267 int c_inhibit_evaluation_warnings;
269 /* Whether we are building a boolean conversion inside
270 convert_for_assignment, or some other late binary operation. If
271 build_binary_op is called for C (from code shared by C and C++) in
272 this case, then the operands have already been folded and the
273 result will not be folded again, so C_MAYBE_CONST_EXPR should not
274 be generated. */
275 bool in_late_binary_op;
277 /* Whether lexing has been completed, so subsequent preprocessor
278 errors should use the compiler's input_location. */
279 bool done_lexing = false;
281 /* Information about how a function name is generated. */
282 struct fname_var_t
284 tree *const decl; /* pointer to the VAR_DECL. */
285 const unsigned rid; /* RID number for the identifier. */
286 const int pretty; /* How pretty is it? */
289 /* The three ways of getting then name of the current function. */
291 const struct fname_var_t fname_vars[] =
293 /* C99 compliant __func__, must be first. */
294 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
295 /* GCC __FUNCTION__ compliant. */
296 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
297 /* GCC __PRETTY_FUNCTION__ compliant. */
298 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
299 {NULL, 0, 0},
302 /* Global visibility options. */
303 struct visibility_flags visibility_options;
305 static tree check_case_value (location_t, tree);
306 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
307 bool *);
310 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
311 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
312 static int resort_field_decl_cmp (const void *, const void *);
314 /* Reserved words. The third field is a mask: keywords are disabled
315 if they match the mask.
317 Masks for languages:
318 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
319 C --std=c99: D_CXXONLY | D_OBJC
320 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
321 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
322 C++ --std=c++11: D_CONLY | D_OBJC
323 ObjC++ is like C++ except that D_OBJC is not set
325 If -fno-asm is used, D_ASM is added to the mask. If
326 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
327 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
328 In C with -Wc++-compat, we warn if D_CXXWARN is set.
330 Note the complication of the D_CXX_OBJC keywords. These are
331 reserved words such as 'class'. In C++, 'class' is a reserved
332 word. In Objective-C++ it is too. In Objective-C, it is a
333 reserved word too, but only if it follows an '@' sign.
335 const struct c_common_resword c_common_reswords[] =
337 { "_Alignas", RID_ALIGNAS, D_CONLY },
338 { "_Alignof", RID_ALIGNOF, D_CONLY },
339 { "_Atomic", RID_ATOMIC, D_CONLY },
340 { "_Bool", RID_BOOL, D_CONLY },
341 { "_Complex", RID_COMPLEX, 0 },
342 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
343 { "_Cilk_sync", RID_CILK_SYNC, 0 },
344 { "_Cilk_for", RID_CILK_FOR, 0 },
345 { "_Imaginary", RID_IMAGINARY, D_CONLY },
346 { "_Float16", RID_FLOAT16, D_CONLY },
347 { "_Float32", RID_FLOAT32, D_CONLY },
348 { "_Float64", RID_FLOAT64, D_CONLY },
349 { "_Float128", RID_FLOAT128, D_CONLY },
350 { "_Float32x", RID_FLOAT32X, D_CONLY },
351 { "_Float64x", RID_FLOAT64X, D_CONLY },
352 { "_Float128x", RID_FLOAT128X, D_CONLY },
353 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
354 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
355 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
356 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
357 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
358 { "_Sat", RID_SAT, D_CONLY | D_EXT },
359 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
360 { "_Noreturn", RID_NORETURN, D_CONLY },
361 { "_Generic", RID_GENERIC, D_CONLY },
362 { "_Thread_local", RID_THREAD, D_CONLY },
363 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
364 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
365 { "__alignof", RID_ALIGNOF, 0 },
366 { "__alignof__", RID_ALIGNOF, 0 },
367 { "__asm", RID_ASM, 0 },
368 { "__asm__", RID_ASM, 0 },
369 { "__attribute", RID_ATTRIBUTE, 0 },
370 { "__attribute__", RID_ATTRIBUTE, 0 },
371 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
372 { "__bases", RID_BASES, D_CXXONLY },
373 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
374 { "__builtin_call_with_static_chain",
375 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
376 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
377 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
378 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
379 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
380 { "__builtin_offsetof", RID_OFFSETOF, 0 },
381 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
382 { "__builtin_va_arg", RID_VA_ARG, 0 },
383 { "__complex", RID_COMPLEX, 0 },
384 { "__complex__", RID_COMPLEX, 0 },
385 { "__const", RID_CONST, 0 },
386 { "__const__", RID_CONST, 0 },
387 { "__decltype", RID_DECLTYPE, D_CXXONLY },
388 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
389 { "__extension__", RID_EXTENSION, 0 },
390 { "__func__", RID_C99_FUNCTION_NAME, 0 },
391 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
392 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
393 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
394 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
395 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
396 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
397 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
398 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
399 D_CXXONLY },
400 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
401 { "__imag", RID_IMAGPART, 0 },
402 { "__imag__", RID_IMAGPART, 0 },
403 { "__inline", RID_INLINE, 0 },
404 { "__inline__", RID_INLINE, 0 },
405 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
406 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
407 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
408 { "__is_class", RID_IS_CLASS, D_CXXONLY },
409 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
410 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
411 { "__is_final", RID_IS_FINAL, D_CXXONLY },
412 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
413 { "__is_pod", RID_IS_POD, D_CXXONLY },
414 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
415 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
416 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
417 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
418 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
419 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
420 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
421 { "__is_union", RID_IS_UNION, D_CXXONLY },
422 { "__label__", RID_LABEL, 0 },
423 { "__null", RID_NULL, 0 },
424 { "__real", RID_REALPART, 0 },
425 { "__real__", RID_REALPART, 0 },
426 { "__restrict", RID_RESTRICT, 0 },
427 { "__restrict__", RID_RESTRICT, 0 },
428 { "__signed", RID_SIGNED, 0 },
429 { "__signed__", RID_SIGNED, 0 },
430 { "__thread", RID_THREAD, 0 },
431 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
432 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
433 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
434 { "__typeof", RID_TYPEOF, 0 },
435 { "__typeof__", RID_TYPEOF, 0 },
436 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
437 { "__volatile", RID_VOLATILE, 0 },
438 { "__volatile__", RID_VOLATILE, 0 },
439 { "__GIMPLE", RID_GIMPLE, D_CONLY },
440 { "__PHI", RID_PHI, D_CONLY },
441 { "__RTL", RID_RTL, D_CONLY },
442 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
443 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
444 { "asm", RID_ASM, D_ASM },
445 { "auto", RID_AUTO, 0 },
446 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
447 { "break", RID_BREAK, 0 },
448 { "case", RID_CASE, 0 },
449 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
450 { "char", RID_CHAR, 0 },
451 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
452 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
453 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
454 { "const", RID_CONST, 0 },
455 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
456 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
457 { "continue", RID_CONTINUE, 0 },
458 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
459 { "default", RID_DEFAULT, 0 },
460 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
461 { "do", RID_DO, 0 },
462 { "double", RID_DOUBLE, 0 },
463 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
464 { "else", RID_ELSE, 0 },
465 { "enum", RID_ENUM, 0 },
466 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
467 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
468 { "extern", RID_EXTERN, 0 },
469 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
470 { "float", RID_FLOAT, 0 },
471 { "for", RID_FOR, 0 },
472 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
473 { "goto", RID_GOTO, 0 },
474 { "if", RID_IF, 0 },
475 { "inline", RID_INLINE, D_EXT89 },
476 { "int", RID_INT, 0 },
477 { "long", RID_LONG, 0 },
478 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
479 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
480 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
481 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
482 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
483 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
484 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
485 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
486 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
487 { "register", RID_REGISTER, 0 },
488 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
489 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
490 { "return", RID_RETURN, 0 },
491 { "short", RID_SHORT, 0 },
492 { "signed", RID_SIGNED, 0 },
493 { "sizeof", RID_SIZEOF, 0 },
494 { "static", RID_STATIC, 0 },
495 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
496 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
497 { "struct", RID_STRUCT, 0 },
498 { "switch", RID_SWITCH, 0 },
499 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
500 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
501 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
502 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
503 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
504 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
505 { "typedef", RID_TYPEDEF, 0 },
506 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
507 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
508 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
509 { "union", RID_UNION, 0 },
510 { "unsigned", RID_UNSIGNED, 0 },
511 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
512 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
513 { "void", RID_VOID, 0 },
514 { "volatile", RID_VOLATILE, 0 },
515 { "wchar_t", RID_WCHAR, D_CXXONLY },
516 { "while", RID_WHILE, 0 },
517 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
518 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
520 /* C++ transactional memory. */
521 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
522 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
523 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
524 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
526 /* Concepts-related keywords */
527 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
528 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
530 /* These Objective-C keywords are recognized only immediately after
531 an '@'. */
532 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
533 { "defs", RID_AT_DEFS, D_OBJC },
534 { "encode", RID_AT_ENCODE, D_OBJC },
535 { "end", RID_AT_END, D_OBJC },
536 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
537 { "interface", RID_AT_INTERFACE, D_OBJC },
538 { "protocol", RID_AT_PROTOCOL, D_OBJC },
539 { "selector", RID_AT_SELECTOR, D_OBJC },
540 { "finally", RID_AT_FINALLY, D_OBJC },
541 { "optional", RID_AT_OPTIONAL, D_OBJC },
542 { "required", RID_AT_REQUIRED, D_OBJC },
543 { "property", RID_AT_PROPERTY, D_OBJC },
544 { "package", RID_AT_PACKAGE, D_OBJC },
545 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
546 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
547 /* These are recognized only in protocol-qualifier context
548 (see above) */
549 { "bycopy", RID_BYCOPY, D_OBJC },
550 { "byref", RID_BYREF, D_OBJC },
551 { "in", RID_IN, D_OBJC },
552 { "inout", RID_INOUT, D_OBJC },
553 { "oneway", RID_ONEWAY, D_OBJC },
554 { "out", RID_OUT, D_OBJC },
555 /* These are recognized inside a property attribute list */
556 { "assign", RID_ASSIGN, D_OBJC },
557 { "copy", RID_COPY, D_OBJC },
558 { "getter", RID_GETTER, D_OBJC },
559 { "nonatomic", RID_NONATOMIC, D_OBJC },
560 { "readonly", RID_READONLY, D_OBJC },
561 { "readwrite", RID_READWRITE, D_OBJC },
562 { "retain", RID_RETAIN, D_OBJC },
563 { "setter", RID_SETTER, D_OBJC },
566 const unsigned int num_c_common_reswords =
567 sizeof c_common_reswords / sizeof (struct c_common_resword);
569 /* Return identifier for address space AS. */
571 const char *
572 c_addr_space_name (addr_space_t as)
574 int rid = RID_FIRST_ADDR_SPACE + as;
575 gcc_assert (ridpointers [rid]);
576 return IDENTIFIER_POINTER (ridpointers [rid]);
579 /* Push current bindings for the function name VAR_DECLS. */
581 void
582 start_fname_decls (void)
584 unsigned ix;
585 tree saved = NULL_TREE;
587 for (ix = 0; fname_vars[ix].decl; ix++)
589 tree decl = *fname_vars[ix].decl;
591 if (decl)
593 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
594 saved);
595 *fname_vars[ix].decl = NULL_TREE;
598 if (saved || saved_function_name_decls)
599 /* Normally they'll have been NULL, so only push if we've got a
600 stack, or they are non-NULL. */
601 saved_function_name_decls = tree_cons (saved, NULL_TREE,
602 saved_function_name_decls);
605 /* Finish up the current bindings, adding them into the current function's
606 statement tree. This must be done _before_ finish_stmt_tree is called.
607 If there is no current function, we must be at file scope and no statements
608 are involved. Pop the previous bindings. */
610 void
611 finish_fname_decls (void)
613 unsigned ix;
614 tree stmts = NULL_TREE;
615 tree stack = saved_function_name_decls;
617 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
618 append_to_statement_list (TREE_VALUE (stack), &stmts);
620 if (stmts)
622 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
624 if (TREE_CODE (*bodyp) == BIND_EXPR)
625 bodyp = &BIND_EXPR_BODY (*bodyp);
627 append_to_statement_list_force (*bodyp, &stmts);
628 *bodyp = stmts;
631 for (ix = 0; fname_vars[ix].decl; ix++)
632 *fname_vars[ix].decl = NULL_TREE;
634 if (stack)
636 /* We had saved values, restore them. */
637 tree saved;
639 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
641 tree decl = TREE_PURPOSE (saved);
642 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
644 *fname_vars[ix].decl = decl;
646 stack = TREE_CHAIN (stack);
648 saved_function_name_decls = stack;
651 /* Return the text name of the current function, suitably prettified
652 by PRETTY_P. Return string must be freed by caller. */
654 const char *
655 fname_as_string (int pretty_p)
657 const char *name = "top level";
658 char *namep;
659 int vrb = 2, len;
660 cpp_string cstr = { 0, 0 }, strname;
662 if (!pretty_p)
664 name = "";
665 vrb = 0;
668 if (current_function_decl)
669 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
671 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
673 namep = XNEWVEC (char, len);
674 snprintf (namep, len, "\"%s\"", name);
675 strname.text = (unsigned char *) namep;
676 strname.len = len - 1;
678 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
680 XDELETEVEC (namep);
681 return (const char *) cstr.text;
684 return namep;
687 /* Return the VAR_DECL for a const char array naming the current
688 function. If the VAR_DECL has not yet been created, create it
689 now. RID indicates how it should be formatted and IDENTIFIER_NODE
690 ID is its name (unfortunately C and C++ hold the RID values of
691 keywords in different places, so we can't derive RID from ID in
692 this language independent code. LOC is the location of the
693 function. */
695 tree
696 fname_decl (location_t loc, unsigned int rid, tree id)
698 unsigned ix;
699 tree decl = NULL_TREE;
701 for (ix = 0; fname_vars[ix].decl; ix++)
702 if (fname_vars[ix].rid == rid)
703 break;
705 decl = *fname_vars[ix].decl;
706 if (!decl)
708 /* If a tree is built here, it would normally have the lineno of
709 the current statement. Later this tree will be moved to the
710 beginning of the function and this line number will be wrong.
711 To avoid this problem set the lineno to 0 here; that prevents
712 it from appearing in the RTL. */
713 tree stmts;
714 location_t saved_location = input_location;
715 input_location = UNKNOWN_LOCATION;
717 stmts = push_stmt_list ();
718 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
719 stmts = pop_stmt_list (stmts);
720 if (!IS_EMPTY_STMT (stmts))
721 saved_function_name_decls
722 = tree_cons (decl, stmts, saved_function_name_decls);
723 *fname_vars[ix].decl = decl;
724 input_location = saved_location;
726 if (!ix && !current_function_decl)
727 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
729 return decl;
732 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
734 tree
735 fix_string_type (tree value)
737 int length = TREE_STRING_LENGTH (value);
738 int nchars;
739 tree e_type, i_type, a_type;
741 /* Compute the number of elements, for the array type. */
742 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
744 nchars = length;
745 e_type = char_type_node;
747 else if (TREE_TYPE (value) == char16_array_type_node)
749 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
750 e_type = char16_type_node;
752 else if (TREE_TYPE (value) == char32_array_type_node)
754 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
755 e_type = char32_type_node;
757 else
759 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
760 e_type = wchar_type_node;
763 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
764 limit in C++98 Annex B is very large (65536) and is not normative,
765 so we do not diagnose it (warn_overlength_strings is forced off
766 in c_common_post_options). */
767 if (warn_overlength_strings)
769 const int nchars_max = flag_isoc99 ? 4095 : 509;
770 const int relevant_std = flag_isoc99 ? 99 : 90;
771 if (nchars - 1 > nchars_max)
772 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
773 separate the %d from the 'C'. 'ISO' should not be
774 translated, but it may be moved after 'C%d' in languages
775 where modifiers follow nouns. */
776 pedwarn (input_location, OPT_Woverlength_strings,
777 "string length %qd is greater than the length %qd "
778 "ISO C%d compilers are required to support",
779 nchars - 1, nchars_max, relevant_std);
782 /* Create the array type for the string constant. The ISO C++
783 standard says that a string literal has type `const char[N]' or
784 `const wchar_t[N]'. We use the same logic when invoked as a C
785 front-end with -Wwrite-strings.
786 ??? We should change the type of an expression depending on the
787 state of a warning flag. We should just be warning -- see how
788 this is handled in the C++ front-end for the deprecated implicit
789 conversion from string literals to `char*' or `wchar_t*'.
791 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
792 array type being the unqualified version of that type.
793 Therefore, if we are constructing an array of const char, we must
794 construct the matching unqualified array type first. The C front
795 end does not require this, but it does no harm, so we do it
796 unconditionally. */
797 i_type = build_index_type (size_int (nchars - 1));
798 a_type = build_array_type (e_type, i_type);
799 if (c_dialect_cxx() || warn_write_strings)
800 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
802 TREE_TYPE (value) = a_type;
803 TREE_CONSTANT (value) = 1;
804 TREE_READONLY (value) = 1;
805 TREE_STATIC (value) = 1;
806 return value;
809 /* Given a string of type STRING_TYPE, determine what kind of string
810 token would give an equivalent execution encoding: CPP_STRING,
811 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
812 This may not be exactly the string token type that initially created
813 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
814 string type at this point.
816 This effectively reverses part of the logic in lex_string and
817 fix_string_type. */
819 static enum cpp_ttype
820 get_cpp_ttype_from_string_type (tree string_type)
822 gcc_assert (string_type);
823 if (TREE_CODE (string_type) == POINTER_TYPE)
824 string_type = TREE_TYPE (string_type);
826 if (TREE_CODE (string_type) != ARRAY_TYPE)
827 return CPP_OTHER;
829 tree element_type = TREE_TYPE (string_type);
830 if (TREE_CODE (element_type) != INTEGER_TYPE)
831 return CPP_OTHER;
833 int bits_per_character = TYPE_PRECISION (element_type);
834 switch (bits_per_character)
836 case 8:
837 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
838 case 16:
839 return CPP_STRING16;
840 case 32:
841 return CPP_STRING32;
844 return CPP_OTHER;
847 /* The global record of string concatentations, for use in
848 extracting locations within string literals. */
850 GTY(()) string_concat_db *g_string_concat_db;
852 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
854 const char *
855 c_get_substring_location (const substring_loc &substr_loc,
856 location_t *out_loc)
858 enum cpp_ttype tok_type
859 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
860 if (tok_type == CPP_OTHER)
861 return "unrecognized string type";
863 return get_source_location_for_substring (parse_in, g_string_concat_db,
864 substr_loc.get_fmt_string_loc (),
865 tok_type,
866 substr_loc.get_caret_idx (),
867 substr_loc.get_start_idx (),
868 substr_loc.get_end_idx (),
869 out_loc);
873 /* Fold X for consideration by one of the warning functions when checking
874 whether an expression has a constant value. */
876 tree
877 fold_for_warn (tree x)
879 if (c_dialect_cxx ())
880 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
881 else
882 /* The C front-end has already folded X appropriately. */
883 return x;
886 /* Return true iff T is a boolean promoted to int. */
888 bool
889 bool_promoted_to_int_p (tree t)
891 return (CONVERT_EXPR_P (t)
892 && TREE_TYPE (t) == integer_type_node
893 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
896 /* vector_targets_convertible_p is used for vector pointer types. The
897 callers perform various checks that the qualifiers are satisfactory,
898 while OTOH vector_targets_convertible_p ignores the number of elements
899 in the vectors. That's fine with vector pointers as we can consider,
900 say, a vector of 8 elements as two consecutive vectors of 4 elements,
901 and that does not require and conversion of the pointer values.
902 In contrast, vector_types_convertible_p and
903 vector_types_compatible_elements_p are used for vector value types. */
904 /* True if pointers to distinct types T1 and T2 can be converted to
905 each other without an explicit cast. Only returns true for opaque
906 vector types. */
907 bool
908 vector_targets_convertible_p (const_tree t1, const_tree t2)
910 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
911 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
912 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
913 return true;
915 return false;
918 /* vector_types_convertible_p is used for vector value types.
919 It could in principle call vector_targets_convertible_p as a subroutine,
920 but then the check for vector type would be duplicated with its callers,
921 and also the purpose of vector_targets_convertible_p would become
922 muddled.
923 Where vector_types_convertible_p returns true, a conversion might still be
924 needed to make the types match.
925 In contrast, vector_targets_convertible_p is used for vector pointer
926 values, and vector_types_compatible_elements_p is used specifically
927 in the context for binary operators, as a check if use is possible without
928 conversion. */
929 /* True if vector types T1 and T2 can be converted to each other
930 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
931 can only be converted with -flax-vector-conversions yet that is not
932 in effect, emit a note telling the user about that option if such
933 a note has not previously been emitted. */
934 bool
935 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
937 static bool emitted_lax_note = false;
938 bool convertible_lax;
940 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
941 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
942 return true;
944 convertible_lax =
945 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
946 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
947 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
948 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
949 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
951 if (!convertible_lax || flag_lax_vector_conversions)
952 return convertible_lax;
954 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
955 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
956 return true;
958 if (emit_lax_note && !emitted_lax_note)
960 emitted_lax_note = true;
961 inform (input_location, "use -flax-vector-conversions to permit "
962 "conversions between vectors with differing "
963 "element types or numbers of subparts");
966 return false;
969 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
970 and have vector types, V0 has the same type as V1, and the number of
971 elements of V0, V1, MASK is the same.
973 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
974 called with two arguments. In this case implementation passes the
975 first argument twice in order to share the same tree code. This fact
976 could enable the mask-values being twice the vector length. This is
977 an implementation accident and this semantics is not guaranteed to
978 the user. */
979 tree
980 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
981 bool complain)
983 tree ret;
984 bool wrap = true;
985 bool maybe_const = false;
986 bool two_arguments = false;
988 if (v1 == NULL_TREE)
990 two_arguments = true;
991 v1 = v0;
994 if (v0 == error_mark_node || v1 == error_mark_node
995 || mask == error_mark_node)
996 return error_mark_node;
998 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1000 if (complain)
1001 error_at (loc, "__builtin_shuffle last argument must "
1002 "be an integer vector");
1003 return error_mark_node;
1006 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1007 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1009 if (complain)
1010 error_at (loc, "__builtin_shuffle arguments must be vectors");
1011 return error_mark_node;
1014 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1016 if (complain)
1017 error_at (loc, "__builtin_shuffle argument vectors must be of "
1018 "the same type");
1019 return error_mark_node;
1022 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
1023 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
1024 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
1025 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1027 if (complain)
1028 error_at (loc, "__builtin_shuffle number of elements of the "
1029 "argument vector(s) and the mask vector should "
1030 "be the same");
1031 return error_mark_node;
1034 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1035 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1037 if (complain)
1038 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1039 "must have the same size as inner type of the mask");
1040 return error_mark_node;
1043 if (!c_dialect_cxx ())
1045 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1046 v0 = c_fully_fold (v0, false, &maybe_const);
1047 wrap &= maybe_const;
1049 if (two_arguments)
1050 v1 = v0 = save_expr (v0);
1051 else
1053 v1 = c_fully_fold (v1, false, &maybe_const);
1054 wrap &= maybe_const;
1057 mask = c_fully_fold (mask, false, &maybe_const);
1058 wrap &= maybe_const;
1060 else if (two_arguments)
1061 v1 = v0 = save_expr (v0);
1063 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1065 if (!c_dialect_cxx () && !wrap)
1066 ret = c_wrap_maybe_const (ret, true);
1068 return ret;
1071 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1072 to integral type. */
1074 tree
1075 c_common_get_narrower (tree op, int *unsignedp_ptr)
1077 op = get_narrower (op, unsignedp_ptr);
1079 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1080 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1082 /* C++0x scoped enumerations don't implicitly convert to integral
1083 type; if we stripped an explicit conversion to a larger type we
1084 need to replace it so common_type will still work. */
1085 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1086 TYPE_UNSIGNED (TREE_TYPE (op)));
1087 op = fold_convert (type, op);
1089 return op;
1092 /* This is a helper function of build_binary_op.
1094 For certain operations if both args were extended from the same
1095 smaller type, do the arithmetic in that type and then extend.
1097 BITWISE indicates a bitwise operation.
1098 For them, this optimization is safe only if
1099 both args are zero-extended or both are sign-extended.
1100 Otherwise, we might change the result.
1101 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1102 but calculated in (unsigned short) it would be (unsigned short)-1.
1104 tree
1105 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1107 int unsigned0, unsigned1;
1108 tree arg0, arg1;
1109 int uns;
1110 tree type;
1112 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1113 excessive narrowing when we call get_narrower below. For
1114 example, suppose that OP0 is of unsigned int extended
1115 from signed char and that RESULT_TYPE is long long int.
1116 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1117 like
1119 (long long int) (unsigned int) signed_char
1121 which get_narrower would narrow down to
1123 (unsigned int) signed char
1125 If we do not cast OP0 first, get_narrower would return
1126 signed_char, which is inconsistent with the case of the
1127 explicit cast. */
1128 op0 = convert (result_type, op0);
1129 op1 = convert (result_type, op1);
1131 arg0 = c_common_get_narrower (op0, &unsigned0);
1132 arg1 = c_common_get_narrower (op1, &unsigned1);
1134 /* UNS is 1 if the operation to be done is an unsigned one. */
1135 uns = TYPE_UNSIGNED (result_type);
1137 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1138 but it *requires* conversion to FINAL_TYPE. */
1140 if ((TYPE_PRECISION (TREE_TYPE (op0))
1141 == TYPE_PRECISION (TREE_TYPE (arg0)))
1142 && TREE_TYPE (op0) != result_type)
1143 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1144 if ((TYPE_PRECISION (TREE_TYPE (op1))
1145 == TYPE_PRECISION (TREE_TYPE (arg1)))
1146 && TREE_TYPE (op1) != result_type)
1147 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1149 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1151 /* For bitwise operations, signedness of nominal type
1152 does not matter. Consider only how operands were extended. */
1153 if (bitwise)
1154 uns = unsigned0;
1156 /* Note that in all three cases below we refrain from optimizing
1157 an unsigned operation on sign-extended args.
1158 That would not be valid. */
1160 /* Both args variable: if both extended in same way
1161 from same width, do it in that width.
1162 Do it unsigned if args were zero-extended. */
1163 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1164 < TYPE_PRECISION (result_type))
1165 && (TYPE_PRECISION (TREE_TYPE (arg1))
1166 == TYPE_PRECISION (TREE_TYPE (arg0)))
1167 && unsigned0 == unsigned1
1168 && (unsigned0 || !uns))
1169 return c_common_signed_or_unsigned_type
1170 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1172 else if (TREE_CODE (arg0) == INTEGER_CST
1173 && (unsigned1 || !uns)
1174 && (TYPE_PRECISION (TREE_TYPE (arg1))
1175 < TYPE_PRECISION (result_type))
1176 && (type
1177 = c_common_signed_or_unsigned_type (unsigned1,
1178 TREE_TYPE (arg1)))
1179 && !POINTER_TYPE_P (type)
1180 && int_fits_type_p (arg0, type))
1181 return type;
1183 else if (TREE_CODE (arg1) == INTEGER_CST
1184 && (unsigned0 || !uns)
1185 && (TYPE_PRECISION (TREE_TYPE (arg0))
1186 < TYPE_PRECISION (result_type))
1187 && (type
1188 = c_common_signed_or_unsigned_type (unsigned0,
1189 TREE_TYPE (arg0)))
1190 && !POINTER_TYPE_P (type)
1191 && int_fits_type_p (arg1, type))
1192 return type;
1194 return result_type;
1197 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1198 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1200 static bool
1201 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1203 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1204 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1205 REAL_VALUE_TYPE real_low_bound =
1206 real_value_from_int_cst (0, type_low_bound);
1207 REAL_VALUE_TYPE real_high_bound =
1208 real_value_from_int_cst (0, type_high_bound);
1210 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1211 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1214 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1215 to the complex/real/integer type TYPE. Function returns non-zero when:
1216 * EXPR is a constant which cannot be exactly converted to TYPE.
1217 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1218 for EXPR type and TYPE being both integers or both real, or both
1219 complex.
1220 * EXPR is not a constant of complex type and TYPE is a real or
1221 an integer.
1222 * EXPR is not a constant of real type and TYPE is an integer.
1223 * EXPR is not a constant of integer type which cannot be
1224 exactly converted to real type.
1226 Function allows conversions between types of different signedness and
1227 can return SAFE_CONVERSION (zero) in that case. Function can produce
1228 signedness warnings if PRODUCE_WARNS is true.
1230 Function allows conversions from complex constants to non-complex types,
1231 provided that imaginary part is zero and real part can be safely converted
1232 to TYPE. */
1234 enum conversion_safety
1235 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
1237 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1238 tree expr_type = TREE_TYPE (expr);
1239 loc = expansion_point_location_if_in_system_header (loc);
1241 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1243 /* If type is complex, we are interested in compatibility with
1244 underlying type. */
1245 if (TREE_CODE (type) == COMPLEX_TYPE)
1246 type = TREE_TYPE (type);
1248 /* Warn for real constant that is not an exact integer converted
1249 to integer type. */
1250 if (TREE_CODE (expr_type) == REAL_TYPE
1251 && TREE_CODE (type) == INTEGER_TYPE)
1253 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1254 give_warning = UNSAFE_REAL;
1256 /* Warn for an integer constant that does not fit into integer type. */
1257 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1258 && TREE_CODE (type) == INTEGER_TYPE
1259 && !int_fits_type_p (expr, type))
1261 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1262 && tree_int_cst_sgn (expr) < 0)
1264 if (produce_warns)
1265 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1266 " implicitly converted to unsigned type");
1268 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1270 if (produce_warns)
1271 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1272 " constant value to negative integer");
1274 else
1275 give_warning = UNSAFE_OTHER;
1277 else if (TREE_CODE (type) == REAL_TYPE)
1279 /* Warn for an integer constant that does not fit into real type. */
1280 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1282 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1283 if (!exact_real_truncate (TYPE_MODE (type), &a))
1284 give_warning = UNSAFE_REAL;
1286 /* Warn for a real constant that does not fit into a smaller
1287 real type. */
1288 else if (TREE_CODE (expr_type) == REAL_TYPE
1289 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1291 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1292 if (!exact_real_truncate (TYPE_MODE (type), &a))
1293 give_warning = UNSAFE_REAL;
1298 else if (TREE_CODE (expr) == COMPLEX_CST)
1300 tree imag_part = TREE_IMAGPART (expr);
1301 /* Conversion from complex constant with zero imaginary part,
1302 perform check for conversion of real part. */
1303 if ((TREE_CODE (imag_part) == REAL_CST
1304 && real_zerop (imag_part))
1305 || (TREE_CODE (imag_part) == INTEGER_CST
1306 && integer_zerop (imag_part)))
1307 /* Note: in this branch we use recursive call to unsafe_conversion_p
1308 with different type of EXPR, but it is still safe, because when EXPR
1309 is a constant, it's type is not used in text of generated warnings
1310 (otherwise they could sound misleading). */
1311 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1312 produce_warns);
1313 /* Conversion from complex constant with non-zero imaginary part. */
1314 else
1316 /* Conversion to complex type.
1317 Perform checks for both real and imaginary parts. */
1318 if (TREE_CODE (type) == COMPLEX_TYPE)
1320 /* Unfortunately, produce_warns must be false in two subsequent
1321 calls of unsafe_conversion_p, because otherwise we could
1322 produce strange "double" warnings, if both real and imaginary
1323 parts have conversion problems related to signedness.
1325 For example:
1326 int32_t _Complex a = 0x80000000 + 0x80000000i;
1328 Possible solution: add a separate function for checking
1329 constants and combine result of two calls appropriately. */
1330 enum conversion_safety re_safety =
1331 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
1332 enum conversion_safety im_safety =
1333 unsafe_conversion_p (loc, type, imag_part, false);
1335 /* Merge the results into appropriate single warning. */
1337 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1338 if (re_safety == im_safety)
1339 give_warning = re_safety;
1340 else if (!re_safety && im_safety)
1341 give_warning = im_safety;
1342 else if (re_safety && !im_safety)
1343 give_warning = re_safety;
1344 else
1345 give_warning = UNSAFE_OTHER;
1347 /* Warn about conversion from complex to real or integer type. */
1348 else
1349 give_warning = UNSAFE_IMAGINARY;
1353 /* Checks for remaining case: EXPR is not constant. */
1354 else
1356 /* Warn for real types converted to integer types. */
1357 if (TREE_CODE (expr_type) == REAL_TYPE
1358 && TREE_CODE (type) == INTEGER_TYPE)
1359 give_warning = UNSAFE_REAL;
1361 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1362 && TREE_CODE (type) == INTEGER_TYPE)
1364 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1365 expr = get_unwidened (expr, 0);
1366 expr_type = TREE_TYPE (expr);
1368 /* Don't warn for short y; short x = ((int)y & 0xff); */
1369 if (TREE_CODE (expr) == BIT_AND_EXPR
1370 || TREE_CODE (expr) == BIT_IOR_EXPR
1371 || TREE_CODE (expr) == BIT_XOR_EXPR)
1373 /* If both args were extended from a shortest type,
1374 use that type if that is safe. */
1375 expr_type = shorten_binary_op (expr_type,
1376 TREE_OPERAND (expr, 0),
1377 TREE_OPERAND (expr, 1),
1378 /* bitwise */1);
1380 if (TREE_CODE (expr) == BIT_AND_EXPR)
1382 tree op0 = TREE_OPERAND (expr, 0);
1383 tree op1 = TREE_OPERAND (expr, 1);
1384 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1385 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1387 /* If one of the operands is a non-negative constant
1388 that fits in the target type, then the type of the
1389 other operand does not matter. */
1390 if ((TREE_CODE (op0) == INTEGER_CST
1391 && int_fits_type_p (op0, c_common_signed_type (type))
1392 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1393 || (TREE_CODE (op1) == INTEGER_CST
1394 && int_fits_type_p (op1, c_common_signed_type (type))
1395 && int_fits_type_p (op1,
1396 c_common_unsigned_type (type))))
1397 return SAFE_CONVERSION;
1398 /* If constant is unsigned and fits in the target
1399 type, then the result will also fit. */
1400 else if ((TREE_CODE (op0) == INTEGER_CST
1401 && unsigned0
1402 && int_fits_type_p (op0, type))
1403 || (TREE_CODE (op1) == INTEGER_CST
1404 && unsigned1
1405 && int_fits_type_p (op1, type)))
1406 return SAFE_CONVERSION;
1409 /* Warn for integer types converted to smaller integer types. */
1410 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1411 give_warning = UNSAFE_OTHER;
1413 /* When they are the same width but different signedness,
1414 then the value may change. */
1415 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1416 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1417 /* Even when converted to a bigger type, if the type is
1418 unsigned but expr is signed, then negative values
1419 will be changed. */
1420 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1421 && produce_warns)
1422 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1423 "may change the sign of the result",
1424 type, expr_type);
1427 /* Warn for integer types converted to real types if and only if
1428 all the range of values of the integer type cannot be
1429 represented by the real type. */
1430 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1431 && TREE_CODE (type) == REAL_TYPE)
1433 /* Don't warn about char y = 0xff; float x = (int) y; */
1434 expr = get_unwidened (expr, 0);
1435 expr_type = TREE_TYPE (expr);
1437 if (!int_safely_convertible_to_real_p (expr_type, type))
1438 give_warning = UNSAFE_OTHER;
1441 /* Warn for real types converted to smaller real types. */
1442 else if (TREE_CODE (expr_type) == REAL_TYPE
1443 && TREE_CODE (type) == REAL_TYPE
1444 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1445 give_warning = UNSAFE_REAL;
1447 /* Check conversion between two complex types. */
1448 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1449 && TREE_CODE (type) == COMPLEX_TYPE)
1451 /* Extract underlying types (i.e., type of real and imaginary
1452 parts) of expr_type and type. */
1453 tree from_type = TREE_TYPE (expr_type);
1454 tree to_type = TREE_TYPE (type);
1456 /* Warn for real types converted to integer types. */
1457 if (TREE_CODE (from_type) == REAL_TYPE
1458 && TREE_CODE (to_type) == INTEGER_TYPE)
1459 give_warning = UNSAFE_REAL;
1461 /* Warn for real types converted to smaller real types. */
1462 else if (TREE_CODE (from_type) == REAL_TYPE
1463 && TREE_CODE (to_type) == REAL_TYPE
1464 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1465 give_warning = UNSAFE_REAL;
1467 /* Check conversion for complex integer types. Here implementation
1468 is simpler than for real-domain integers because it does not
1469 involve sophisticated cases, such as bitmasks, casts, etc. */
1470 else if (TREE_CODE (from_type) == INTEGER_TYPE
1471 && TREE_CODE (to_type) == INTEGER_TYPE)
1473 /* Warn for integer types converted to smaller integer types. */
1474 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1475 give_warning = UNSAFE_OTHER;
1477 /* Check for different signedness, see case for real-domain
1478 integers (above) for a more detailed comment. */
1479 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1480 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1481 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1482 && produce_warns)
1483 warning_at (loc, OPT_Wsign_conversion,
1484 "conversion to %qT from %qT "
1485 "may change the sign of the result",
1486 type, expr_type);
1488 else if (TREE_CODE (from_type) == INTEGER_TYPE
1489 && TREE_CODE (to_type) == REAL_TYPE
1490 && !int_safely_convertible_to_real_p (from_type, to_type))
1491 give_warning = UNSAFE_OTHER;
1494 /* Warn for complex types converted to real or integer types. */
1495 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1496 && TREE_CODE (type) != COMPLEX_TYPE)
1497 give_warning = UNSAFE_IMAGINARY;
1500 return give_warning;
1504 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1505 Invoke this function on every expression that is converted implicitly,
1506 i.e. because of language rules and not because of an explicit cast. */
1508 tree
1509 convert_and_check (location_t loc, tree type, tree expr)
1511 tree result;
1512 tree expr_for_warning;
1514 /* Convert from a value with possible excess precision rather than
1515 via the semantic type, but do not warn about values not fitting
1516 exactly in the semantic type. */
1517 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1519 tree orig_type = TREE_TYPE (expr);
1520 expr = TREE_OPERAND (expr, 0);
1521 expr_for_warning = convert (orig_type, expr);
1522 if (orig_type == type)
1523 return expr_for_warning;
1525 else
1526 expr_for_warning = expr;
1528 if (TREE_TYPE (expr) == type)
1529 return expr;
1531 result = convert (type, expr);
1533 if (c_inhibit_evaluation_warnings == 0
1534 && !TREE_OVERFLOW_P (expr)
1535 && result != error_mark_node)
1536 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1538 return result;
1541 /* A node in a list that describes references to variables (EXPR), which are
1542 either read accesses if WRITER is zero, or write accesses, in which case
1543 WRITER is the parent of EXPR. */
1544 struct tlist
1546 struct tlist *next;
1547 tree expr, writer;
1550 /* Used to implement a cache the results of a call to verify_tree. We only
1551 use this for SAVE_EXPRs. */
1552 struct tlist_cache
1554 struct tlist_cache *next;
1555 struct tlist *cache_before_sp;
1556 struct tlist *cache_after_sp;
1557 tree expr;
1560 /* Obstack to use when allocating tlist structures, and corresponding
1561 firstobj. */
1562 static struct obstack tlist_obstack;
1563 static char *tlist_firstobj = 0;
1565 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1566 warnings. */
1567 static struct tlist *warned_ids;
1568 /* SAVE_EXPRs need special treatment. We process them only once and then
1569 cache the results. */
1570 static struct tlist_cache *save_expr_cache;
1572 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1573 static void merge_tlist (struct tlist **, struct tlist *, int);
1574 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1575 static bool warning_candidate_p (tree);
1576 static bool candidate_equal_p (const_tree, const_tree);
1577 static void warn_for_collisions (struct tlist *);
1578 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1579 static struct tlist *new_tlist (struct tlist *, tree, tree);
1581 /* Create a new struct tlist and fill in its fields. */
1582 static struct tlist *
1583 new_tlist (struct tlist *next, tree t, tree writer)
1585 struct tlist *l;
1586 l = XOBNEW (&tlist_obstack, struct tlist);
1587 l->next = next;
1588 l->expr = t;
1589 l->writer = writer;
1590 return l;
1593 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1594 is nonnull, we ignore any node we find which has a writer equal to it. */
1596 static void
1597 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1599 while (add)
1601 struct tlist *next = add->next;
1602 if (!copy)
1603 add->next = *to;
1604 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1605 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1606 add = next;
1610 /* Merge the nodes of ADD into TO. This merging process is done so that for
1611 each variable that already exists in TO, no new node is added; however if
1612 there is a write access recorded in ADD, and an occurrence on TO is only
1613 a read access, then the occurrence in TO will be modified to record the
1614 write. */
1616 static void
1617 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1619 struct tlist **end = to;
1621 while (*end)
1622 end = &(*end)->next;
1624 while (add)
1626 int found = 0;
1627 struct tlist *tmp2;
1628 struct tlist *next = add->next;
1630 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1631 if (candidate_equal_p (tmp2->expr, add->expr))
1633 found = 1;
1634 if (!tmp2->writer)
1635 tmp2->writer = add->writer;
1637 if (!found)
1639 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1640 end = &(*end)->next;
1641 *end = 0;
1643 add = next;
1647 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1648 references in list LIST conflict with it, excluding reads if ONLY writers
1649 is nonzero. */
1651 static void
1652 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1653 int only_writes)
1655 struct tlist *tmp;
1657 /* Avoid duplicate warnings. */
1658 for (tmp = warned_ids; tmp; tmp = tmp->next)
1659 if (candidate_equal_p (tmp->expr, written))
1660 return;
1662 while (list)
1664 if (candidate_equal_p (list->expr, written)
1665 && !candidate_equal_p (list->writer, writer)
1666 && (!only_writes || list->writer))
1668 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1669 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1670 OPT_Wsequence_point, "operation on %qE may be undefined",
1671 list->expr);
1673 list = list->next;
1677 /* Given a list LIST of references to variables, find whether any of these
1678 can cause conflicts due to missing sequence points. */
1680 static void
1681 warn_for_collisions (struct tlist *list)
1683 struct tlist *tmp;
1685 for (tmp = list; tmp; tmp = tmp->next)
1687 if (tmp->writer)
1688 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1692 /* Return nonzero if X is a tree that can be verified by the sequence point
1693 warnings. */
1695 static bool
1696 warning_candidate_p (tree x)
1698 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1699 return false;
1701 if (TREE_CODE (x) == BLOCK)
1702 return false;
1704 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1705 (lvalue_p) crash on TRY/CATCH. */
1706 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1707 return false;
1709 if (!lvalue_p (x))
1710 return false;
1712 /* No point to track non-const calls, they will never satisfy
1713 operand_equal_p. */
1714 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1715 return false;
1717 if (TREE_CODE (x) == STRING_CST)
1718 return false;
1720 return true;
1723 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1724 static bool
1725 candidate_equal_p (const_tree x, const_tree y)
1727 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1730 /* Walk the tree X, and record accesses to variables. If X is written by the
1731 parent tree, WRITER is the parent.
1732 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1733 expression or its only operand forces a sequence point, then everything up
1734 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1735 in PNO_SP.
1736 Once we return, we will have emitted warnings if any subexpression before
1737 such a sequence point could be undefined. On a higher level, however, the
1738 sequence point may not be relevant, and we'll merge the two lists.
1740 Example: (b++, a) + b;
1741 The call that processes the COMPOUND_EXPR will store the increment of B
1742 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1743 processes the PLUS_EXPR will need to merge the two lists so that
1744 eventually, all accesses end up on the same list (and we'll warn about the
1745 unordered subexpressions b++ and b.
1747 A note on merging. If we modify the former example so that our expression
1748 becomes
1749 (b++, b) + a
1750 care must be taken not simply to add all three expressions into the final
1751 PNO_SP list. The function merge_tlist takes care of that by merging the
1752 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1753 way, so that no more than one access to B is recorded. */
1755 static void
1756 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1757 tree writer)
1759 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1760 enum tree_code code;
1761 enum tree_code_class cl;
1763 /* X may be NULL if it is the operand of an empty statement expression
1764 ({ }). */
1765 if (x == NULL)
1766 return;
1768 restart:
1769 code = TREE_CODE (x);
1770 cl = TREE_CODE_CLASS (code);
1772 if (warning_candidate_p (x))
1773 *pno_sp = new_tlist (*pno_sp, x, writer);
1775 switch (code)
1777 case CONSTRUCTOR:
1778 case SIZEOF_EXPR:
1779 return;
1781 case COMPOUND_EXPR:
1782 case TRUTH_ANDIF_EXPR:
1783 case TRUTH_ORIF_EXPR:
1784 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1785 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1786 warn_for_collisions (tmp_nosp);
1787 merge_tlist (pbefore_sp, tmp_before, 0);
1788 merge_tlist (pbefore_sp, tmp_nosp, 0);
1789 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1790 warn_for_collisions (tmp_list2);
1791 merge_tlist (pbefore_sp, tmp_list3, 0);
1792 merge_tlist (pno_sp, tmp_list2, 0);
1793 return;
1795 case COND_EXPR:
1796 tmp_before = tmp_list2 = 0;
1797 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1798 warn_for_collisions (tmp_list2);
1799 merge_tlist (pbefore_sp, tmp_before, 0);
1800 merge_tlist (pbefore_sp, tmp_list2, 0);
1802 tmp_list3 = tmp_nosp = 0;
1803 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1804 warn_for_collisions (tmp_nosp);
1805 merge_tlist (pbefore_sp, tmp_list3, 0);
1807 tmp_list3 = tmp_list2 = 0;
1808 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1809 warn_for_collisions (tmp_list2);
1810 merge_tlist (pbefore_sp, tmp_list3, 0);
1811 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1812 two first, to avoid warning for (a ? b++ : b++). */
1813 merge_tlist (&tmp_nosp, tmp_list2, 0);
1814 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1815 return;
1817 case PREDECREMENT_EXPR:
1818 case PREINCREMENT_EXPR:
1819 case POSTDECREMENT_EXPR:
1820 case POSTINCREMENT_EXPR:
1821 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1822 return;
1824 case MODIFY_EXPR:
1825 tmp_before = tmp_nosp = tmp_list3 = 0;
1826 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1827 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1828 /* Expressions inside the LHS are not ordered wrt. the sequence points
1829 in the RHS. Example:
1830 *a = (a++, 2)
1831 Despite the fact that the modification of "a" is in the before_sp
1832 list (tmp_before), it conflicts with the use of "a" in the LHS.
1833 We can handle this by adding the contents of tmp_list3
1834 to those of tmp_before, and redoing the collision warnings for that
1835 list. */
1836 add_tlist (&tmp_before, tmp_list3, x, 1);
1837 warn_for_collisions (tmp_before);
1838 /* Exclude the LHS itself here; we first have to merge it into the
1839 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1840 didn't exclude the LHS, we'd get it twice, once as a read and once
1841 as a write. */
1842 add_tlist (pno_sp, tmp_list3, x, 0);
1843 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1845 merge_tlist (pbefore_sp, tmp_before, 0);
1846 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1847 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1848 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1849 return;
1851 case CALL_EXPR:
1852 /* We need to warn about conflicts among arguments and conflicts between
1853 args and the function address. Side effects of the function address,
1854 however, are not ordered by the sequence point of the call. */
1856 call_expr_arg_iterator iter;
1857 tree arg;
1858 tmp_before = tmp_nosp = 0;
1859 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1860 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1862 tmp_list2 = tmp_list3 = 0;
1863 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1864 merge_tlist (&tmp_list3, tmp_list2, 0);
1865 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1867 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1868 warn_for_collisions (tmp_before);
1869 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1870 return;
1873 case TREE_LIST:
1874 /* Scan all the list, e.g. indices of multi dimensional array. */
1875 while (x)
1877 tmp_before = tmp_nosp = 0;
1878 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1879 merge_tlist (&tmp_nosp, tmp_before, 0);
1880 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1881 x = TREE_CHAIN (x);
1883 return;
1885 case SAVE_EXPR:
1887 struct tlist_cache *t;
1888 for (t = save_expr_cache; t; t = t->next)
1889 if (candidate_equal_p (t->expr, x))
1890 break;
1892 if (!t)
1894 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1895 t->next = save_expr_cache;
1896 t->expr = x;
1897 save_expr_cache = t;
1899 tmp_before = tmp_nosp = 0;
1900 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1901 warn_for_collisions (tmp_nosp);
1903 tmp_list3 = 0;
1904 merge_tlist (&tmp_list3, tmp_nosp, 0);
1905 t->cache_before_sp = tmp_before;
1906 t->cache_after_sp = tmp_list3;
1908 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1909 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1910 return;
1913 case ADDR_EXPR:
1914 x = TREE_OPERAND (x, 0);
1915 if (DECL_P (x))
1916 return;
1917 writer = 0;
1918 goto restart;
1920 default:
1921 /* For other expressions, simply recurse on their operands.
1922 Manual tail recursion for unary expressions.
1923 Other non-expressions need not be processed. */
1924 if (cl == tcc_unary)
1926 x = TREE_OPERAND (x, 0);
1927 writer = 0;
1928 goto restart;
1930 else if (IS_EXPR_CODE_CLASS (cl))
1932 int lp;
1933 int max = TREE_OPERAND_LENGTH (x);
1934 for (lp = 0; lp < max; lp++)
1936 tmp_before = tmp_nosp = 0;
1937 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1938 merge_tlist (&tmp_nosp, tmp_before, 0);
1939 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1942 return;
1946 /* Try to warn for undefined behavior in EXPR due to missing sequence
1947 points. */
1949 DEBUG_FUNCTION void
1950 verify_sequence_points (tree expr)
1952 struct tlist *before_sp = 0, *after_sp = 0;
1954 warned_ids = 0;
1955 save_expr_cache = 0;
1956 if (tlist_firstobj == 0)
1958 gcc_obstack_init (&tlist_obstack);
1959 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1962 verify_tree (expr, &before_sp, &after_sp, 0);
1963 warn_for_collisions (after_sp);
1964 obstack_free (&tlist_obstack, tlist_firstobj);
1967 /* Validate the expression after `case' and apply default promotions. */
1969 static tree
1970 check_case_value (location_t loc, tree value)
1972 if (value == NULL_TREE)
1973 return value;
1975 if (TREE_CODE (value) == INTEGER_CST)
1976 /* Promote char or short to int. */
1977 value = perform_integral_promotions (value);
1978 else if (value != error_mark_node)
1980 error_at (loc, "case label does not reduce to an integer constant");
1981 value = error_mark_node;
1984 constant_expression_warning (value);
1986 return value;
1989 /* See if the case values LOW and HIGH are in the range of the original
1990 type (i.e. before the default conversion to int) of the switch testing
1991 expression.
1992 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
1993 the type before promoting it. CASE_LOW_P is a pointer to the lower
1994 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
1995 if the case is not a case range.
1996 The caller has to make sure that we are not called with NULL for
1997 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
1998 was a case value that doesn't fit into the range of the ORIG_TYPE.
1999 Returns true if the case label is in range of ORIG_TYPE (saturated or
2000 untouched) or false if the label is out of range. */
2002 static bool
2003 check_case_bounds (location_t loc, tree type, tree orig_type,
2004 tree *case_low_p, tree *case_high_p,
2005 bool *outside_range_p)
2007 tree min_value, max_value;
2008 tree case_low = *case_low_p;
2009 tree case_high = case_high_p ? *case_high_p : case_low;
2011 /* If there was a problem with the original type, do nothing. */
2012 if (orig_type == error_mark_node)
2013 return true;
2015 min_value = TYPE_MIN_VALUE (orig_type);
2016 max_value = TYPE_MAX_VALUE (orig_type);
2018 /* We'll really need integer constants here. */
2019 case_low = fold (case_low);
2020 case_high = fold (case_high);
2022 /* Case label is less than minimum for type. */
2023 if (tree_int_cst_compare (case_low, min_value) < 0
2024 && tree_int_cst_compare (case_high, min_value) < 0)
2026 warning_at (loc, 0, "case label value is less than minimum value "
2027 "for type");
2028 *outside_range_p = true;
2029 return false;
2032 /* Case value is greater than maximum for type. */
2033 if (tree_int_cst_compare (case_low, max_value) > 0
2034 && tree_int_cst_compare (case_high, max_value) > 0)
2036 warning_at (loc, 0, "case label value exceeds maximum value for type");
2037 *outside_range_p = true;
2038 return false;
2041 /* Saturate lower case label value to minimum. */
2042 if (tree_int_cst_compare (case_high, min_value) >= 0
2043 && tree_int_cst_compare (case_low, min_value) < 0)
2045 warning_at (loc, 0, "lower value in case label range"
2046 " less than minimum value for type");
2047 *outside_range_p = true;
2048 case_low = min_value;
2051 /* Saturate upper case label value to maximum. */
2052 if (tree_int_cst_compare (case_low, max_value) <= 0
2053 && tree_int_cst_compare (case_high, max_value) > 0)
2055 warning_at (loc, 0, "upper value in case label range"
2056 " exceeds maximum value for type");
2057 *outside_range_p = true;
2058 case_high = max_value;
2061 if (*case_low_p != case_low)
2062 *case_low_p = convert (type, case_low);
2063 if (case_high_p && *case_high_p != case_high)
2064 *case_high_p = convert (type, case_high);
2066 return true;
2069 /* Return an integer type with BITS bits of precision,
2070 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2072 tree
2073 c_common_type_for_size (unsigned int bits, int unsignedp)
2075 int i;
2077 if (bits == TYPE_PRECISION (integer_type_node))
2078 return unsignedp ? unsigned_type_node : integer_type_node;
2080 if (bits == TYPE_PRECISION (signed_char_type_node))
2081 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2083 if (bits == TYPE_PRECISION (short_integer_type_node))
2084 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2086 if (bits == TYPE_PRECISION (long_integer_type_node))
2087 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2089 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2090 return (unsignedp ? long_long_unsigned_type_node
2091 : long_long_integer_type_node);
2093 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2094 if (int_n_enabled_p[i]
2095 && bits == int_n_data[i].bitsize)
2096 return (unsignedp ? int_n_trees[i].unsigned_type
2097 : int_n_trees[i].signed_type);
2099 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2100 return (unsignedp ? widest_unsigned_literal_type_node
2101 : widest_integer_literal_type_node);
2103 if (bits <= TYPE_PRECISION (intQI_type_node))
2104 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2106 if (bits <= TYPE_PRECISION (intHI_type_node))
2107 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2109 if (bits <= TYPE_PRECISION (intSI_type_node))
2110 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2112 if (bits <= TYPE_PRECISION (intDI_type_node))
2113 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2115 return NULL_TREE;
2118 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2119 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2120 and saturating if SATP is nonzero, otherwise not saturating. */
2122 tree
2123 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2124 int unsignedp, int satp)
2126 machine_mode mode;
2127 if (ibit == 0)
2128 mode = unsignedp ? UQQmode : QQmode;
2129 else
2130 mode = unsignedp ? UHAmode : HAmode;
2132 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2133 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2134 break;
2136 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2138 sorry ("GCC cannot support operators with integer types and "
2139 "fixed-point types that have too many integral and "
2140 "fractional bits together");
2141 return NULL_TREE;
2144 return c_common_type_for_mode (mode, satp);
2147 /* Used for communication between c_common_type_for_mode and
2148 c_register_builtin_type. */
2149 tree registered_builtin_types;
2151 /* Return a data type that has machine mode MODE.
2152 If the mode is an integer,
2153 then UNSIGNEDP selects between signed and unsigned types.
2154 If the mode is a fixed-point mode,
2155 then UNSIGNEDP selects between saturating and nonsaturating types. */
2157 tree
2158 c_common_type_for_mode (machine_mode mode, int unsignedp)
2160 tree t;
2161 int i;
2163 if (mode == TYPE_MODE (integer_type_node))
2164 return unsignedp ? unsigned_type_node : integer_type_node;
2166 if (mode == TYPE_MODE (signed_char_type_node))
2167 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2169 if (mode == TYPE_MODE (short_integer_type_node))
2170 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2172 if (mode == TYPE_MODE (long_integer_type_node))
2173 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2175 if (mode == TYPE_MODE (long_long_integer_type_node))
2176 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2178 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2179 if (int_n_enabled_p[i]
2180 && mode == int_n_data[i].m)
2181 return (unsignedp ? int_n_trees[i].unsigned_type
2182 : int_n_trees[i].signed_type);
2184 if (mode == QImode)
2185 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2187 if (mode == HImode)
2188 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2190 if (mode == SImode)
2191 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2193 if (mode == DImode)
2194 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2196 #if HOST_BITS_PER_WIDE_INT >= 64
2197 if (mode == TYPE_MODE (intTI_type_node))
2198 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2199 #endif
2201 if (mode == TYPE_MODE (float_type_node))
2202 return float_type_node;
2204 if (mode == TYPE_MODE (double_type_node))
2205 return double_type_node;
2207 if (mode == TYPE_MODE (long_double_type_node))
2208 return long_double_type_node;
2210 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2211 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2212 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2213 return FLOATN_NX_TYPE_NODE (i);
2215 if (mode == TYPE_MODE (void_type_node))
2216 return void_type_node;
2218 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2219 return (unsignedp
2220 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2221 : make_signed_type (GET_MODE_PRECISION (mode)));
2223 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2224 return (unsignedp
2225 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2226 : make_signed_type (GET_MODE_PRECISION (mode)));
2228 if (COMPLEX_MODE_P (mode))
2230 machine_mode inner_mode;
2231 tree inner_type;
2233 if (mode == TYPE_MODE (complex_float_type_node))
2234 return complex_float_type_node;
2235 if (mode == TYPE_MODE (complex_double_type_node))
2236 return complex_double_type_node;
2237 if (mode == TYPE_MODE (complex_long_double_type_node))
2238 return complex_long_double_type_node;
2240 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2241 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2242 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2243 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2245 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2246 return complex_integer_type_node;
2248 inner_mode = GET_MODE_INNER (mode);
2249 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2250 if (inner_type != NULL_TREE)
2251 return build_complex_type (inner_type);
2253 else if (VECTOR_MODE_P (mode))
2255 machine_mode inner_mode = GET_MODE_INNER (mode);
2256 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2257 if (inner_type != NULL_TREE)
2258 return build_vector_type_for_mode (inner_type, mode);
2261 if (mode == TYPE_MODE (dfloat32_type_node))
2262 return dfloat32_type_node;
2263 if (mode == TYPE_MODE (dfloat64_type_node))
2264 return dfloat64_type_node;
2265 if (mode == TYPE_MODE (dfloat128_type_node))
2266 return dfloat128_type_node;
2268 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2270 if (mode == TYPE_MODE (short_fract_type_node))
2271 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2272 if (mode == TYPE_MODE (fract_type_node))
2273 return unsignedp ? sat_fract_type_node : fract_type_node;
2274 if (mode == TYPE_MODE (long_fract_type_node))
2275 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2276 if (mode == TYPE_MODE (long_long_fract_type_node))
2277 return unsignedp ? sat_long_long_fract_type_node
2278 : long_long_fract_type_node;
2280 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2281 return unsignedp ? sat_unsigned_short_fract_type_node
2282 : unsigned_short_fract_type_node;
2283 if (mode == TYPE_MODE (unsigned_fract_type_node))
2284 return unsignedp ? sat_unsigned_fract_type_node
2285 : unsigned_fract_type_node;
2286 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2287 return unsignedp ? sat_unsigned_long_fract_type_node
2288 : unsigned_long_fract_type_node;
2289 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2290 return unsignedp ? sat_unsigned_long_long_fract_type_node
2291 : unsigned_long_long_fract_type_node;
2293 if (mode == TYPE_MODE (short_accum_type_node))
2294 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2295 if (mode == TYPE_MODE (accum_type_node))
2296 return unsignedp ? sat_accum_type_node : accum_type_node;
2297 if (mode == TYPE_MODE (long_accum_type_node))
2298 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2299 if (mode == TYPE_MODE (long_long_accum_type_node))
2300 return unsignedp ? sat_long_long_accum_type_node
2301 : long_long_accum_type_node;
2303 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2304 return unsignedp ? sat_unsigned_short_accum_type_node
2305 : unsigned_short_accum_type_node;
2306 if (mode == TYPE_MODE (unsigned_accum_type_node))
2307 return unsignedp ? sat_unsigned_accum_type_node
2308 : unsigned_accum_type_node;
2309 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2310 return unsignedp ? sat_unsigned_long_accum_type_node
2311 : unsigned_long_accum_type_node;
2312 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2313 return unsignedp ? sat_unsigned_long_long_accum_type_node
2314 : unsigned_long_long_accum_type_node;
2316 if (mode == QQmode)
2317 return unsignedp ? sat_qq_type_node : qq_type_node;
2318 if (mode == HQmode)
2319 return unsignedp ? sat_hq_type_node : hq_type_node;
2320 if (mode == SQmode)
2321 return unsignedp ? sat_sq_type_node : sq_type_node;
2322 if (mode == DQmode)
2323 return unsignedp ? sat_dq_type_node : dq_type_node;
2324 if (mode == TQmode)
2325 return unsignedp ? sat_tq_type_node : tq_type_node;
2327 if (mode == UQQmode)
2328 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2329 if (mode == UHQmode)
2330 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2331 if (mode == USQmode)
2332 return unsignedp ? sat_usq_type_node : usq_type_node;
2333 if (mode == UDQmode)
2334 return unsignedp ? sat_udq_type_node : udq_type_node;
2335 if (mode == UTQmode)
2336 return unsignedp ? sat_utq_type_node : utq_type_node;
2338 if (mode == HAmode)
2339 return unsignedp ? sat_ha_type_node : ha_type_node;
2340 if (mode == SAmode)
2341 return unsignedp ? sat_sa_type_node : sa_type_node;
2342 if (mode == DAmode)
2343 return unsignedp ? sat_da_type_node : da_type_node;
2344 if (mode == TAmode)
2345 return unsignedp ? sat_ta_type_node : ta_type_node;
2347 if (mode == UHAmode)
2348 return unsignedp ? sat_uha_type_node : uha_type_node;
2349 if (mode == USAmode)
2350 return unsignedp ? sat_usa_type_node : usa_type_node;
2351 if (mode == UDAmode)
2352 return unsignedp ? sat_uda_type_node : uda_type_node;
2353 if (mode == UTAmode)
2354 return unsignedp ? sat_uta_type_node : uta_type_node;
2357 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2358 if (TYPE_MODE (TREE_VALUE (t)) == mode
2359 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2360 return TREE_VALUE (t);
2362 return NULL_TREE;
2365 tree
2366 c_common_unsigned_type (tree type)
2368 return c_common_signed_or_unsigned_type (1, type);
2371 /* Return a signed type the same as TYPE in other respects. */
2373 tree
2374 c_common_signed_type (tree type)
2376 return c_common_signed_or_unsigned_type (0, type);
2379 /* Return a type the same as TYPE except unsigned or
2380 signed according to UNSIGNEDP. */
2382 tree
2383 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2385 tree type1;
2386 int i;
2388 /* This block of code emulates the behavior of the old
2389 c_common_unsigned_type. In particular, it returns
2390 long_unsigned_type_node if passed a long, even when a int would
2391 have the same size. This is necessary for warnings to work
2392 correctly in archs where sizeof(int) == sizeof(long) */
2394 type1 = TYPE_MAIN_VARIANT (type);
2395 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2396 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2397 if (type1 == integer_type_node || type1 == unsigned_type_node)
2398 return unsignedp ? unsigned_type_node : integer_type_node;
2399 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2400 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2401 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2402 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2403 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2404 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2406 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2407 if (int_n_enabled_p[i]
2408 && (type1 == int_n_trees[i].unsigned_type
2409 || type1 == int_n_trees[i].signed_type))
2410 return (unsignedp ? int_n_trees[i].unsigned_type
2411 : int_n_trees[i].signed_type);
2413 #if HOST_BITS_PER_WIDE_INT >= 64
2414 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2415 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2416 #endif
2417 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2418 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2419 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2420 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2421 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2422 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2423 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2424 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2426 #define C_COMMON_FIXED_TYPES(NAME) \
2427 if (type1 == short_ ## NAME ## _type_node \
2428 || type1 == unsigned_short_ ## NAME ## _type_node) \
2429 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2430 : short_ ## NAME ## _type_node; \
2431 if (type1 == NAME ## _type_node \
2432 || type1 == unsigned_ ## NAME ## _type_node) \
2433 return unsignedp ? unsigned_ ## NAME ## _type_node \
2434 : NAME ## _type_node; \
2435 if (type1 == long_ ## NAME ## _type_node \
2436 || type1 == unsigned_long_ ## NAME ## _type_node) \
2437 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2438 : long_ ## NAME ## _type_node; \
2439 if (type1 == long_long_ ## NAME ## _type_node \
2440 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2441 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2442 : long_long_ ## NAME ## _type_node;
2444 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2445 if (type1 == NAME ## _type_node \
2446 || type1 == u ## NAME ## _type_node) \
2447 return unsignedp ? u ## NAME ## _type_node \
2448 : NAME ## _type_node;
2450 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2451 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2452 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2453 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2454 : sat_ ## short_ ## NAME ## _type_node; \
2455 if (type1 == sat_ ## NAME ## _type_node \
2456 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2457 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2458 : sat_ ## NAME ## _type_node; \
2459 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2460 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2461 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2462 : sat_ ## long_ ## NAME ## _type_node; \
2463 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2464 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2465 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2466 : sat_ ## long_long_ ## NAME ## _type_node;
2468 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2469 if (type1 == sat_ ## NAME ## _type_node \
2470 || type1 == sat_ ## u ## NAME ## _type_node) \
2471 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2472 : sat_ ## NAME ## _type_node;
2474 C_COMMON_FIXED_TYPES (fract);
2475 C_COMMON_FIXED_TYPES_SAT (fract);
2476 C_COMMON_FIXED_TYPES (accum);
2477 C_COMMON_FIXED_TYPES_SAT (accum);
2479 C_COMMON_FIXED_MODE_TYPES (qq);
2480 C_COMMON_FIXED_MODE_TYPES (hq);
2481 C_COMMON_FIXED_MODE_TYPES (sq);
2482 C_COMMON_FIXED_MODE_TYPES (dq);
2483 C_COMMON_FIXED_MODE_TYPES (tq);
2484 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2485 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2486 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2487 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2488 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2489 C_COMMON_FIXED_MODE_TYPES (ha);
2490 C_COMMON_FIXED_MODE_TYPES (sa);
2491 C_COMMON_FIXED_MODE_TYPES (da);
2492 C_COMMON_FIXED_MODE_TYPES (ta);
2493 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2494 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2495 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2496 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2498 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2499 the precision; they have precision set to match their range, but
2500 may use a wider mode to match an ABI. If we change modes, we may
2501 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2502 the precision as well, so as to yield correct results for
2503 bit-field types. C++ does not have these separate bit-field
2504 types, and producing a signed or unsigned variant of an
2505 ENUMERAL_TYPE may cause other problems as well. */
2507 if (!INTEGRAL_TYPE_P (type)
2508 || TYPE_UNSIGNED (type) == unsignedp)
2509 return type;
2511 #define TYPE_OK(node) \
2512 (TYPE_MODE (type) == TYPE_MODE (node) \
2513 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2514 if (TYPE_OK (signed_char_type_node))
2515 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2516 if (TYPE_OK (integer_type_node))
2517 return unsignedp ? unsigned_type_node : integer_type_node;
2518 if (TYPE_OK (short_integer_type_node))
2519 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2520 if (TYPE_OK (long_integer_type_node))
2521 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2522 if (TYPE_OK (long_long_integer_type_node))
2523 return (unsignedp ? long_long_unsigned_type_node
2524 : long_long_integer_type_node);
2526 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2527 if (int_n_enabled_p[i]
2528 && TYPE_MODE (type) == int_n_data[i].m
2529 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2530 return (unsignedp ? int_n_trees[i].unsigned_type
2531 : int_n_trees[i].signed_type);
2533 #if HOST_BITS_PER_WIDE_INT >= 64
2534 if (TYPE_OK (intTI_type_node))
2535 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2536 #endif
2537 if (TYPE_OK (intDI_type_node))
2538 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2539 if (TYPE_OK (intSI_type_node))
2540 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2541 if (TYPE_OK (intHI_type_node))
2542 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2543 if (TYPE_OK (intQI_type_node))
2544 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2545 #undef TYPE_OK
2547 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2550 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2552 tree
2553 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2555 int i;
2557 /* Extended integer types of the same width as a standard type have
2558 lesser rank, so those of the same width as int promote to int or
2559 unsigned int and are valid for printf formats expecting int or
2560 unsigned int. To avoid such special cases, avoid creating
2561 extended integer types for bit-fields if a standard integer type
2562 is available. */
2563 if (width == TYPE_PRECISION (integer_type_node))
2564 return unsignedp ? unsigned_type_node : integer_type_node;
2565 if (width == TYPE_PRECISION (signed_char_type_node))
2566 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2567 if (width == TYPE_PRECISION (short_integer_type_node))
2568 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2569 if (width == TYPE_PRECISION (long_integer_type_node))
2570 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2571 if (width == TYPE_PRECISION (long_long_integer_type_node))
2572 return (unsignedp ? long_long_unsigned_type_node
2573 : long_long_integer_type_node);
2574 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2575 if (int_n_enabled_p[i]
2576 && width == int_n_data[i].bitsize)
2577 return (unsignedp ? int_n_trees[i].unsigned_type
2578 : int_n_trees[i].signed_type);
2579 return build_nonstandard_integer_type (width, unsignedp);
2582 /* The C version of the register_builtin_type langhook. */
2584 void
2585 c_register_builtin_type (tree type, const char* name)
2587 tree decl;
2589 decl = build_decl (UNKNOWN_LOCATION,
2590 TYPE_DECL, get_identifier (name), type);
2591 DECL_ARTIFICIAL (decl) = 1;
2592 if (!TYPE_NAME (type))
2593 TYPE_NAME (type) = decl;
2594 lang_hooks.decls.pushdecl (decl);
2596 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2599 /* Print an error message for invalid operands to arith operation
2600 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2601 RICHLOC is a rich location for the message, containing either
2602 three separate locations for each of the operator and operands
2604 lhs op rhs
2605 ~~~ ^~ ~~~
2607 (C FE), or one location ranging over all over them
2609 lhs op rhs
2610 ~~~~^~~~~~
2612 (C++ FE). */
2614 void
2615 binary_op_error (rich_location *richloc, enum tree_code code,
2616 tree type0, tree type1)
2618 const char *opname;
2620 switch (code)
2622 case PLUS_EXPR:
2623 opname = "+"; break;
2624 case MINUS_EXPR:
2625 opname = "-"; break;
2626 case MULT_EXPR:
2627 opname = "*"; break;
2628 case MAX_EXPR:
2629 opname = "max"; break;
2630 case MIN_EXPR:
2631 opname = "min"; break;
2632 case EQ_EXPR:
2633 opname = "=="; break;
2634 case NE_EXPR:
2635 opname = "!="; break;
2636 case LE_EXPR:
2637 opname = "<="; break;
2638 case GE_EXPR:
2639 opname = ">="; break;
2640 case LT_EXPR:
2641 opname = "<"; break;
2642 case GT_EXPR:
2643 opname = ">"; break;
2644 case LSHIFT_EXPR:
2645 opname = "<<"; break;
2646 case RSHIFT_EXPR:
2647 opname = ">>"; break;
2648 case TRUNC_MOD_EXPR:
2649 case FLOOR_MOD_EXPR:
2650 opname = "%"; break;
2651 case TRUNC_DIV_EXPR:
2652 case FLOOR_DIV_EXPR:
2653 opname = "/"; break;
2654 case BIT_AND_EXPR:
2655 opname = "&"; break;
2656 case BIT_IOR_EXPR:
2657 opname = "|"; break;
2658 case TRUTH_ANDIF_EXPR:
2659 opname = "&&"; break;
2660 case TRUTH_ORIF_EXPR:
2661 opname = "||"; break;
2662 case BIT_XOR_EXPR:
2663 opname = "^"; break;
2664 default:
2665 gcc_unreachable ();
2667 error_at_rich_loc (richloc,
2668 "invalid operands to binary %s (have %qT and %qT)",
2669 opname, type0, type1);
2672 /* Given an expression as a tree, return its original type. Do this
2673 by stripping any conversion that preserves the sign and precision. */
2674 static tree
2675 expr_original_type (tree expr)
2677 STRIP_SIGN_NOPS (expr);
2678 return TREE_TYPE (expr);
2681 /* Subroutine of build_binary_op, used for comparison operations.
2682 See if the operands have both been converted from subword integer types
2683 and, if so, perhaps change them both back to their original type.
2684 This function is also responsible for converting the two operands
2685 to the proper common type for comparison.
2687 The arguments of this function are all pointers to local variables
2688 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2689 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2691 LOC is the location of the comparison.
2693 If this function returns non-NULL_TREE, it means that the comparison has
2694 a constant value. What this function returns is an expression for
2695 that value. */
2697 tree
2698 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2699 tree *restype_ptr, enum tree_code *rescode_ptr)
2701 tree type;
2702 tree op0 = *op0_ptr;
2703 tree op1 = *op1_ptr;
2704 int unsignedp0, unsignedp1;
2705 int real1, real2;
2706 tree primop0, primop1;
2707 enum tree_code code = *rescode_ptr;
2709 /* Throw away any conversions to wider types
2710 already present in the operands. */
2712 primop0 = c_common_get_narrower (op0, &unsignedp0);
2713 primop1 = c_common_get_narrower (op1, &unsignedp1);
2715 /* If primopN is first sign-extended from primopN's precision to opN's
2716 precision, then zero-extended from opN's precision to
2717 *restype_ptr precision, shortenings might be invalid. */
2718 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2719 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2720 && !unsignedp0
2721 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2722 primop0 = op0;
2723 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2724 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2725 && !unsignedp1
2726 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2727 primop1 = op1;
2729 /* Handle the case that OP0 does not *contain* a conversion
2730 but it *requires* conversion to FINAL_TYPE. */
2732 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2733 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2734 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2735 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2737 /* If one of the operands must be floated, we cannot optimize. */
2738 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2739 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2741 /* If first arg is constant, swap the args (changing operation
2742 so value is preserved), for canonicalization. Don't do this if
2743 the second arg is 0. */
2745 if (TREE_CONSTANT (primop0)
2746 && !integer_zerop (primop1) && !real_zerop (primop1)
2747 && !fixed_zerop (primop1))
2749 std::swap (primop0, primop1);
2750 std::swap (op0, op1);
2751 *op0_ptr = op0;
2752 *op1_ptr = op1;
2753 std::swap (unsignedp0, unsignedp1);
2754 std::swap (real1, real2);
2756 switch (code)
2758 case LT_EXPR:
2759 code = GT_EXPR;
2760 break;
2761 case GT_EXPR:
2762 code = LT_EXPR;
2763 break;
2764 case LE_EXPR:
2765 code = GE_EXPR;
2766 break;
2767 case GE_EXPR:
2768 code = LE_EXPR;
2769 break;
2770 default:
2771 break;
2773 *rescode_ptr = code;
2776 /* If comparing an integer against a constant more bits wide,
2777 maybe we can deduce a value of 1 or 0 independent of the data.
2778 Or else truncate the constant now
2779 rather than extend the variable at run time.
2781 This is only interesting if the constant is the wider arg.
2782 Also, it is not safe if the constant is unsigned and the
2783 variable arg is signed, since in this case the variable
2784 would be sign-extended and then regarded as unsigned.
2785 Our technique fails in this case because the lowest/highest
2786 possible unsigned results don't follow naturally from the
2787 lowest/highest possible values of the variable operand.
2788 For just EQ_EXPR and NE_EXPR there is another technique that
2789 could be used: see if the constant can be faithfully represented
2790 in the other operand's type, by truncating it and reextending it
2791 and see if that preserves the constant's value. */
2793 if (!real1 && !real2
2794 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2795 && TREE_CODE (primop1) == INTEGER_CST
2796 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2798 int min_gt, max_gt, min_lt, max_lt;
2799 tree maxval, minval;
2800 /* 1 if comparison is nominally unsigned. */
2801 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2802 tree val;
2804 type = c_common_signed_or_unsigned_type (unsignedp0,
2805 TREE_TYPE (primop0));
2807 maxval = TYPE_MAX_VALUE (type);
2808 minval = TYPE_MIN_VALUE (type);
2810 if (unsignedp && !unsignedp0)
2811 *restype_ptr = c_common_signed_type (*restype_ptr);
2813 if (TREE_TYPE (primop1) != *restype_ptr)
2815 /* Convert primop1 to target type, but do not introduce
2816 additional overflow. We know primop1 is an int_cst. */
2817 primop1 = force_fit_type (*restype_ptr,
2818 wi::to_wide
2819 (primop1,
2820 TYPE_PRECISION (*restype_ptr)),
2821 0, TREE_OVERFLOW (primop1));
2823 if (type != *restype_ptr)
2825 minval = convert (*restype_ptr, minval);
2826 maxval = convert (*restype_ptr, maxval);
2829 min_gt = tree_int_cst_lt (primop1, minval);
2830 max_gt = tree_int_cst_lt (primop1, maxval);
2831 min_lt = tree_int_cst_lt (minval, primop1);
2832 max_lt = tree_int_cst_lt (maxval, primop1);
2834 val = 0;
2835 /* This used to be a switch, but Genix compiler can't handle that. */
2836 if (code == NE_EXPR)
2838 if (max_lt || min_gt)
2839 val = truthvalue_true_node;
2841 else if (code == EQ_EXPR)
2843 if (max_lt || min_gt)
2844 val = truthvalue_false_node;
2846 else if (code == LT_EXPR)
2848 if (max_lt)
2849 val = truthvalue_true_node;
2850 if (!min_lt)
2851 val = truthvalue_false_node;
2853 else if (code == GT_EXPR)
2855 if (min_gt)
2856 val = truthvalue_true_node;
2857 if (!max_gt)
2858 val = truthvalue_false_node;
2860 else if (code == LE_EXPR)
2862 if (!max_gt)
2863 val = truthvalue_true_node;
2864 if (min_gt)
2865 val = truthvalue_false_node;
2867 else if (code == GE_EXPR)
2869 if (!min_lt)
2870 val = truthvalue_true_node;
2871 if (max_lt)
2872 val = truthvalue_false_node;
2875 /* If primop0 was sign-extended and unsigned comparison specd,
2876 we did a signed comparison above using the signed type bounds.
2877 But the comparison we output must be unsigned.
2879 Also, for inequalities, VAL is no good; but if the signed
2880 comparison had *any* fixed result, it follows that the
2881 unsigned comparison just tests the sign in reverse
2882 (positive values are LE, negative ones GE).
2883 So we can generate an unsigned comparison
2884 against an extreme value of the signed type. */
2886 if (unsignedp && !unsignedp0)
2888 if (val != 0)
2889 switch (code)
2891 case LT_EXPR:
2892 case GE_EXPR:
2893 primop1 = TYPE_MIN_VALUE (type);
2894 val = 0;
2895 break;
2897 case LE_EXPR:
2898 case GT_EXPR:
2899 primop1 = TYPE_MAX_VALUE (type);
2900 val = 0;
2901 break;
2903 default:
2904 break;
2906 type = c_common_unsigned_type (type);
2909 if (TREE_CODE (primop0) != INTEGER_CST
2910 /* Don't warn if it's from a (non-system) macro. */
2911 && !(from_macro_expansion_at
2912 (expansion_point_location_if_in_system_header
2913 (EXPR_LOCATION (primop0)))))
2915 if (val == truthvalue_false_node)
2916 warning_at (loc, OPT_Wtype_limits,
2917 "comparison is always false due to limited range of data type");
2918 if (val == truthvalue_true_node)
2919 warning_at (loc, OPT_Wtype_limits,
2920 "comparison is always true due to limited range of data type");
2923 if (val != 0)
2925 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2926 if (TREE_SIDE_EFFECTS (primop0))
2927 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2928 return val;
2931 /* Value is not predetermined, but do the comparison
2932 in the type of the operand that is not constant.
2933 TYPE is already properly set. */
2936 /* If either arg is decimal float and the other is float, find the
2937 proper common type to use for comparison. */
2938 else if (real1 && real2
2939 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2940 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2941 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2943 /* If either arg is decimal float and the other is float, fail. */
2944 else if (real1 && real2
2945 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2946 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2947 return NULL_TREE;
2949 else if (real1 && real2
2950 && (TYPE_PRECISION (TREE_TYPE (primop0))
2951 == TYPE_PRECISION (TREE_TYPE (primop1))))
2952 type = TREE_TYPE (primop0);
2954 /* If args' natural types are both narrower than nominal type
2955 and both extend in the same manner, compare them
2956 in the type of the wider arg.
2957 Otherwise must actually extend both to the nominal
2958 common type lest different ways of extending
2959 alter the result.
2960 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2962 else if (unsignedp0 == unsignedp1 && real1 == real2
2963 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2964 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2966 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2967 type = c_common_signed_or_unsigned_type (unsignedp0
2968 || TYPE_UNSIGNED (*restype_ptr),
2969 type);
2970 /* Make sure shorter operand is extended the right way
2971 to match the longer operand. */
2972 primop0
2973 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2974 TREE_TYPE (primop0)),
2975 primop0);
2976 primop1
2977 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2978 TREE_TYPE (primop1)),
2979 primop1);
2981 else
2983 /* Here we must do the comparison on the nominal type
2984 using the args exactly as we received them. */
2985 type = *restype_ptr;
2986 primop0 = op0;
2987 primop1 = op1;
2989 if (!real1 && !real2 && integer_zerop (primop1)
2990 && TYPE_UNSIGNED (*restype_ptr))
2992 tree value = NULL_TREE;
2993 /* All unsigned values are >= 0, so we warn. However,
2994 if OP0 is a constant that is >= 0, the signedness of
2995 the comparison isn't an issue, so suppress the
2996 warning. */
2997 bool warn =
2998 warn_type_limits && !in_system_header_at (loc)
2999 && !(TREE_CODE (primop0) == INTEGER_CST
3000 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3001 primop0)))
3002 /* Do not warn for enumeration types. */
3003 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3005 switch (code)
3007 case GE_EXPR:
3008 if (warn)
3009 warning_at (loc, OPT_Wtype_limits,
3010 "comparison of unsigned expression >= 0 is always true");
3011 value = truthvalue_true_node;
3012 break;
3014 case LT_EXPR:
3015 if (warn)
3016 warning_at (loc, OPT_Wtype_limits,
3017 "comparison of unsigned expression < 0 is always false");
3018 value = truthvalue_false_node;
3019 break;
3021 default:
3022 break;
3025 if (value != NULL_TREE)
3027 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3028 if (TREE_SIDE_EFFECTS (primop0))
3029 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3030 primop0, value);
3031 return value;
3036 *op0_ptr = convert (type, primop0);
3037 *op1_ptr = convert (type, primop1);
3039 *restype_ptr = truthvalue_type_node;
3041 return NULL_TREE;
3044 /* Return a tree for the sum or difference (RESULTCODE says which)
3045 of pointer PTROP and integer INTOP. */
3047 tree
3048 pointer_int_sum (location_t loc, enum tree_code resultcode,
3049 tree ptrop, tree intop, bool complain)
3051 tree size_exp, ret;
3053 /* The result is a pointer of the same type that is being added. */
3054 tree result_type = TREE_TYPE (ptrop);
3056 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3058 if (complain && warn_pointer_arith)
3059 pedwarn (loc, OPT_Wpointer_arith,
3060 "pointer of type %<void *%> used in arithmetic");
3061 else if (!complain)
3062 return error_mark_node;
3063 size_exp = integer_one_node;
3065 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3067 if (complain && warn_pointer_arith)
3068 pedwarn (loc, OPT_Wpointer_arith,
3069 "pointer to a function used in arithmetic");
3070 else if (!complain)
3071 return error_mark_node;
3072 size_exp = integer_one_node;
3074 else
3075 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3077 /* We are manipulating pointer values, so we don't need to warn
3078 about relying on undefined signed overflow. We disable the
3079 warning here because we use integer types so fold won't know that
3080 they are really pointers. */
3081 fold_defer_overflow_warnings ();
3083 /* If what we are about to multiply by the size of the elements
3084 contains a constant term, apply distributive law
3085 and multiply that constant term separately.
3086 This helps produce common subexpressions. */
3087 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3088 && !TREE_CONSTANT (intop)
3089 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3090 && TREE_CONSTANT (size_exp)
3091 /* If the constant comes from pointer subtraction,
3092 skip this optimization--it would cause an error. */
3093 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3094 /* If the constant is unsigned, and smaller than the pointer size,
3095 then we must skip this optimization. This is because it could cause
3096 an overflow error if the constant is negative but INTOP is not. */
3097 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3098 || (TYPE_PRECISION (TREE_TYPE (intop))
3099 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3101 enum tree_code subcode = resultcode;
3102 tree int_type = TREE_TYPE (intop);
3103 if (TREE_CODE (intop) == MINUS_EXPR)
3104 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3105 /* Convert both subexpression types to the type of intop,
3106 because weird cases involving pointer arithmetic
3107 can result in a sum or difference with different type args. */
3108 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3109 subcode, ptrop,
3110 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3111 intop = convert (int_type, TREE_OPERAND (intop, 0));
3114 /* Convert the integer argument to a type the same size as sizetype
3115 so the multiply won't overflow spuriously. */
3116 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3117 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3118 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3119 TYPE_UNSIGNED (sizetype)), intop);
3121 /* Replace the integer argument with a suitable product by the object size.
3122 Do this multiplication as signed, then convert to the appropriate type
3123 for the pointer operation and disregard an overflow that occurred only
3124 because of the sign-extension change in the latter conversion. */
3126 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3127 convert (TREE_TYPE (intop), size_exp));
3128 intop = convert (sizetype, t);
3129 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3130 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
3133 /* Create the sum or difference. */
3134 if (resultcode == MINUS_EXPR)
3135 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3137 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3139 fold_undefer_and_ignore_overflow_warnings ();
3141 return ret;
3144 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3145 and if NON_CONST is known not to be permitted in an evaluated part
3146 of a constant expression. */
3148 tree
3149 c_wrap_maybe_const (tree expr, bool non_const)
3151 bool nowarning = TREE_NO_WARNING (expr);
3152 location_t loc = EXPR_LOCATION (expr);
3154 /* This should never be called for C++. */
3155 if (c_dialect_cxx ())
3156 gcc_unreachable ();
3158 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3159 STRIP_TYPE_NOPS (expr);
3160 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3161 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3162 if (nowarning)
3163 TREE_NO_WARNING (expr) = 1;
3164 protected_set_expr_location (expr, loc);
3166 return expr;
3169 /* Return whether EXPR is a declaration whose address can never be
3170 NULL. */
3172 bool
3173 decl_with_nonnull_addr_p (const_tree expr)
3175 return (DECL_P (expr)
3176 && (TREE_CODE (expr) == PARM_DECL
3177 || TREE_CODE (expr) == LABEL_DECL
3178 || !DECL_WEAK (expr)));
3181 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3182 or for an `if' or `while' statement or ?..: exp. It should already
3183 have been validated to be of suitable type; otherwise, a bad
3184 diagnostic may result.
3186 The EXPR is located at LOCATION.
3188 This preparation consists of taking the ordinary
3189 representation of an expression expr and producing a valid tree
3190 boolean expression describing whether expr is nonzero. We could
3191 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3192 but we optimize comparisons, &&, ||, and !.
3194 The resulting type should always be `truthvalue_type_node'. */
3196 tree
3197 c_common_truthvalue_conversion (location_t location, tree expr)
3199 switch (TREE_CODE (expr))
3201 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3202 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3203 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3204 case ORDERED_EXPR: case UNORDERED_EXPR:
3205 if (TREE_TYPE (expr) == truthvalue_type_node)
3206 return expr;
3207 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3208 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3209 goto ret;
3211 case TRUTH_ANDIF_EXPR:
3212 case TRUTH_ORIF_EXPR:
3213 case TRUTH_AND_EXPR:
3214 case TRUTH_OR_EXPR:
3215 case TRUTH_XOR_EXPR:
3216 if (TREE_TYPE (expr) == truthvalue_type_node)
3217 return expr;
3218 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3219 c_common_truthvalue_conversion (location,
3220 TREE_OPERAND (expr, 0)),
3221 c_common_truthvalue_conversion (location,
3222 TREE_OPERAND (expr, 1)));
3223 goto ret;
3225 case TRUTH_NOT_EXPR:
3226 if (TREE_TYPE (expr) == truthvalue_type_node)
3227 return expr;
3228 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3229 c_common_truthvalue_conversion (location,
3230 TREE_OPERAND (expr, 0)));
3231 goto ret;
3233 case ERROR_MARK:
3234 return expr;
3236 case INTEGER_CST:
3237 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3238 && !integer_zerop (expr)
3239 && !integer_onep (expr))
3240 warning_at (location, OPT_Wint_in_bool_context,
3241 "enum constant in boolean context");
3242 return integer_zerop (expr) ? truthvalue_false_node
3243 : truthvalue_true_node;
3245 case REAL_CST:
3246 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3247 ? truthvalue_true_node
3248 : truthvalue_false_node;
3250 case FIXED_CST:
3251 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3252 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3253 ? truthvalue_true_node
3254 : truthvalue_false_node;
3256 case FUNCTION_DECL:
3257 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3258 /* Fall through. */
3260 case ADDR_EXPR:
3262 tree inner = TREE_OPERAND (expr, 0);
3263 if (decl_with_nonnull_addr_p (inner))
3265 /* Common Ada/Pascal programmer's mistake. */
3266 warning_at (location,
3267 OPT_Waddress,
3268 "the address of %qD will always evaluate as %<true%>",
3269 inner);
3270 return truthvalue_true_node;
3272 break;
3275 case COMPLEX_EXPR:
3276 expr = build_binary_op (EXPR_LOCATION (expr),
3277 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3278 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3279 c_common_truthvalue_conversion (location,
3280 TREE_OPERAND (expr, 0)),
3281 c_common_truthvalue_conversion (location,
3282 TREE_OPERAND (expr, 1)),
3284 goto ret;
3286 case NEGATE_EXPR:
3287 case ABS_EXPR:
3288 case FLOAT_EXPR:
3289 case EXCESS_PRECISION_EXPR:
3290 /* These don't change whether an object is nonzero or zero. */
3291 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3293 case LROTATE_EXPR:
3294 case RROTATE_EXPR:
3295 /* These don't change whether an object is zero or nonzero, but
3296 we can't ignore them if their second arg has side-effects. */
3297 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3299 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3300 TREE_OPERAND (expr, 1),
3301 c_common_truthvalue_conversion
3302 (location, TREE_OPERAND (expr, 0)));
3303 goto ret;
3305 else
3306 return c_common_truthvalue_conversion (location,
3307 TREE_OPERAND (expr, 0));
3309 case MULT_EXPR:
3310 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3311 "%<*%> in boolean context, suggest %<&&%> instead");
3312 break;
3314 case LSHIFT_EXPR:
3315 /* We will only warn on signed shifts here, because the majority of
3316 false positive warnings happen in code where unsigned arithmetic
3317 was used in anticipation of a possible overflow.
3318 Furthermore, if we see an unsigned type here we know that the
3319 result of the shift is not subject to integer promotion rules. */
3320 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3321 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3322 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3323 "%<<<%> in boolean context, did you mean %<<%> ?");
3324 break;
3326 case COND_EXPR:
3327 if (warn_int_in_bool_context
3328 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3330 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3331 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3332 if (TREE_CODE (val1) == INTEGER_CST
3333 && TREE_CODE (val2) == INTEGER_CST
3334 && !integer_zerop (val1)
3335 && !integer_zerop (val2)
3336 && (!integer_onep (val1)
3337 || !integer_onep (val2)))
3338 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3339 "?: using integer constants in boolean context, "
3340 "the expression will always evaluate to %<true%>");
3341 else if ((TREE_CODE (val1) == INTEGER_CST
3342 && !integer_zerop (val1)
3343 && !integer_onep (val1))
3344 || (TREE_CODE (val2) == INTEGER_CST
3345 && !integer_zerop (val2)
3346 && !integer_onep (val2)))
3347 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3348 "?: using integer constants in boolean context");
3350 /* Distribute the conversion into the arms of a COND_EXPR. */
3351 if (c_dialect_cxx ())
3352 /* Avoid premature folding. */
3353 break;
3354 else
3356 int w = warn_int_in_bool_context;
3357 warn_int_in_bool_context = 0;
3358 /* Folding will happen later for C. */
3359 expr = build3 (COND_EXPR, truthvalue_type_node,
3360 TREE_OPERAND (expr, 0),
3361 c_common_truthvalue_conversion (location,
3362 TREE_OPERAND (expr, 1)),
3363 c_common_truthvalue_conversion (location,
3364 TREE_OPERAND (expr, 2)));
3365 warn_int_in_bool_context = w;
3366 goto ret;
3369 CASE_CONVERT:
3371 tree totype = TREE_TYPE (expr);
3372 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3374 if (POINTER_TYPE_P (totype)
3375 && !c_inhibit_evaluation_warnings
3376 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3378 tree inner = expr;
3379 STRIP_NOPS (inner);
3381 if (DECL_P (inner))
3382 warning_at (location,
3383 OPT_Waddress,
3384 "the compiler can assume that the address of "
3385 "%qD will always evaluate to %<true%>",
3386 inner);
3389 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3390 since that affects how `default_conversion' will behave. */
3391 if (TREE_CODE (totype) == REFERENCE_TYPE
3392 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3393 break;
3394 /* Don't strip a conversion from C++0x scoped enum, since they
3395 don't implicitly convert to other types. */
3396 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3397 && ENUM_IS_SCOPED (fromtype))
3398 break;
3399 /* If this isn't narrowing the argument, we can ignore it. */
3400 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3401 return c_common_truthvalue_conversion (location,
3402 TREE_OPERAND (expr, 0));
3404 break;
3406 case MODIFY_EXPR:
3407 if (!TREE_NO_WARNING (expr)
3408 && warn_parentheses)
3410 warning_at (location, OPT_Wparentheses,
3411 "suggest parentheses around assignment used as "
3412 "truth value");
3413 TREE_NO_WARNING (expr) = 1;
3415 break;
3417 default:
3418 break;
3421 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3423 tree t = save_expr (expr);
3424 expr = (build_binary_op
3425 (EXPR_LOCATION (expr),
3426 (TREE_SIDE_EFFECTS (expr)
3427 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3428 c_common_truthvalue_conversion
3429 (location,
3430 build_unary_op (location, REALPART_EXPR, t, false)),
3431 c_common_truthvalue_conversion
3432 (location,
3433 build_unary_op (location, IMAGPART_EXPR, t, false)),
3434 0));
3435 goto ret;
3438 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3440 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3441 FCONST0 (TYPE_MODE
3442 (TREE_TYPE (expr))));
3443 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3445 else
3446 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3448 ret:
3449 protected_set_expr_location (expr, location);
3450 return expr;
3453 static void def_builtin_1 (enum built_in_function fncode,
3454 const char *name,
3455 enum built_in_class fnclass,
3456 tree fntype, tree libtype,
3457 bool both_p, bool fallback_p, bool nonansi_p,
3458 tree fnattrs, bool implicit_p);
3461 /* Apply the TYPE_QUALS to the new DECL. */
3463 void
3464 c_apply_type_quals_to_decl (int type_quals, tree decl)
3466 tree type = TREE_TYPE (decl);
3468 if (type == error_mark_node)
3469 return;
3471 if ((type_quals & TYPE_QUAL_CONST)
3472 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3473 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3474 constructor can produce constant init, so rely on cp_finish_decl to
3475 clear TREE_READONLY if the variable has non-constant init. */
3476 TREE_READONLY (decl) = 1;
3477 if (type_quals & TYPE_QUAL_VOLATILE)
3479 TREE_SIDE_EFFECTS (decl) = 1;
3480 TREE_THIS_VOLATILE (decl) = 1;
3482 if (type_quals & TYPE_QUAL_RESTRICT)
3484 while (type && TREE_CODE (type) == ARRAY_TYPE)
3485 /* Allow 'restrict' on arrays of pointers.
3486 FIXME currently we just ignore it. */
3487 type = TREE_TYPE (type);
3488 if (!type
3489 || !POINTER_TYPE_P (type)
3490 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3491 error ("invalid use of %<restrict%>");
3495 /* Return the typed-based alias set for T, which may be an expression
3496 or a type. Return -1 if we don't do anything special. */
3498 alias_set_type
3499 c_common_get_alias_set (tree t)
3501 /* For VLAs, use the alias set of the element type rather than the
3502 default of alias set 0 for types compared structurally. */
3503 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3505 if (TREE_CODE (t) == ARRAY_TYPE)
3506 return get_alias_set (TREE_TYPE (t));
3507 return -1;
3510 /* That's all the expressions we handle specially. */
3511 if (!TYPE_P (t))
3512 return -1;
3514 /* The C standard guarantees that any object may be accessed via an
3515 lvalue that has character type. */
3516 if (t == char_type_node
3517 || t == signed_char_type_node
3518 || t == unsigned_char_type_node)
3519 return 0;
3521 /* The C standard specifically allows aliasing between signed and
3522 unsigned variants of the same type. We treat the signed
3523 variant as canonical. */
3524 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3526 tree t1 = c_common_signed_type (t);
3528 /* t1 == t can happen for boolean nodes which are always unsigned. */
3529 if (t1 != t)
3530 return get_alias_set (t1);
3533 return -1;
3536 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3537 the IS_SIZEOF parameter indicates which operator is being applied.
3538 The COMPLAIN flag controls whether we should diagnose possibly
3539 ill-formed constructs or not. LOC is the location of the SIZEOF or
3540 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3541 a type in any context should be returned, rather than the normal
3542 alignment for that type. */
3544 tree
3545 c_sizeof_or_alignof_type (location_t loc,
3546 tree type, bool is_sizeof, bool min_alignof,
3547 int complain)
3549 const char *op_name;
3550 tree value = NULL;
3551 enum tree_code type_code = TREE_CODE (type);
3553 op_name = is_sizeof ? "sizeof" : "__alignof__";
3555 if (type_code == FUNCTION_TYPE)
3557 if (is_sizeof)
3559 if (complain && warn_pointer_arith)
3560 pedwarn (loc, OPT_Wpointer_arith,
3561 "invalid application of %<sizeof%> to a function type");
3562 else if (!complain)
3563 return error_mark_node;
3564 value = size_one_node;
3566 else
3568 if (complain)
3570 if (c_dialect_cxx ())
3571 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3572 "%<alignof%> applied to a function type");
3573 else
3574 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3575 "%<_Alignof%> applied to a function type");
3577 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3580 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3582 if (type_code == VOID_TYPE
3583 && complain && warn_pointer_arith)
3584 pedwarn (loc, OPT_Wpointer_arith,
3585 "invalid application of %qs to a void type", op_name);
3586 else if (!complain)
3587 return error_mark_node;
3588 value = size_one_node;
3590 else if (!COMPLETE_TYPE_P (type)
3591 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3593 if (complain)
3594 error_at (loc, "invalid application of %qs to incomplete type %qT",
3595 op_name, type);
3596 return error_mark_node;
3598 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3599 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3601 if (complain)
3602 error_at (loc, "invalid application of %qs to array type %qT of "
3603 "incomplete element type", op_name, type);
3604 return error_mark_node;
3606 else
3608 if (is_sizeof)
3609 /* Convert in case a char is more than one unit. */
3610 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3611 size_int (TYPE_PRECISION (char_type_node)
3612 / BITS_PER_UNIT));
3613 else if (min_alignof)
3614 value = size_int (min_align_of_type (type));
3615 else
3616 value = size_int (TYPE_ALIGN_UNIT (type));
3619 /* VALUE will have the middle-end integer type sizetype.
3620 However, we should really return a value of type `size_t',
3621 which is just a typedef for an ordinary integer type. */
3622 value = fold_convert_loc (loc, size_type_node, value);
3624 return value;
3627 /* Implement the __alignof keyword: Return the minimum required
3628 alignment of EXPR, measured in bytes. For VAR_DECLs,
3629 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3630 from an "aligned" __attribute__ specification). LOC is the
3631 location of the ALIGNOF operator. */
3633 tree
3634 c_alignof_expr (location_t loc, tree expr)
3636 tree t;
3638 if (VAR_OR_FUNCTION_DECL_P (expr))
3639 t = size_int (DECL_ALIGN_UNIT (expr));
3641 else if (TREE_CODE (expr) == COMPONENT_REF
3642 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3644 error_at (loc, "%<__alignof%> applied to a bit-field");
3645 t = size_one_node;
3647 else if (TREE_CODE (expr) == COMPONENT_REF
3648 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3649 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3651 else if (INDIRECT_REF_P (expr))
3653 tree t = TREE_OPERAND (expr, 0);
3654 tree best = t;
3655 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3657 while (CONVERT_EXPR_P (t)
3658 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3660 int thisalign;
3662 t = TREE_OPERAND (t, 0);
3663 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3664 if (thisalign > bestalign)
3665 best = t, bestalign = thisalign;
3667 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3669 else
3670 return c_alignof (loc, TREE_TYPE (expr));
3672 return fold_convert_loc (loc, size_type_node, t);
3675 /* Handle C and C++ default attributes. */
3677 enum built_in_attribute
3679 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3680 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3681 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3682 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3683 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3684 #include "builtin-attrs.def"
3685 #undef DEF_ATTR_NULL_TREE
3686 #undef DEF_ATTR_INT
3687 #undef DEF_ATTR_STRING
3688 #undef DEF_ATTR_IDENT
3689 #undef DEF_ATTR_TREE_LIST
3690 ATTR_LAST
3693 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3695 static void c_init_attributes (void);
3697 enum c_builtin_type
3699 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3700 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3701 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3702 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3703 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3704 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3705 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3706 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3707 ARG6) NAME,
3708 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3709 ARG6, ARG7) NAME,
3710 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3711 ARG6, ARG7, ARG8) NAME,
3712 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3713 ARG6, ARG7, ARG8, ARG9) NAME,
3714 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3715 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3716 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3717 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3718 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3719 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3720 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3721 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3722 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3723 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3724 NAME,
3725 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3726 ARG6) NAME,
3727 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3728 ARG6, ARG7) NAME,
3729 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3730 #include "builtin-types.def"
3731 #undef DEF_PRIMITIVE_TYPE
3732 #undef DEF_FUNCTION_TYPE_0
3733 #undef DEF_FUNCTION_TYPE_1
3734 #undef DEF_FUNCTION_TYPE_2
3735 #undef DEF_FUNCTION_TYPE_3
3736 #undef DEF_FUNCTION_TYPE_4
3737 #undef DEF_FUNCTION_TYPE_5
3738 #undef DEF_FUNCTION_TYPE_6
3739 #undef DEF_FUNCTION_TYPE_7
3740 #undef DEF_FUNCTION_TYPE_8
3741 #undef DEF_FUNCTION_TYPE_9
3742 #undef DEF_FUNCTION_TYPE_10
3743 #undef DEF_FUNCTION_TYPE_11
3744 #undef DEF_FUNCTION_TYPE_VAR_0
3745 #undef DEF_FUNCTION_TYPE_VAR_1
3746 #undef DEF_FUNCTION_TYPE_VAR_2
3747 #undef DEF_FUNCTION_TYPE_VAR_3
3748 #undef DEF_FUNCTION_TYPE_VAR_4
3749 #undef DEF_FUNCTION_TYPE_VAR_5
3750 #undef DEF_FUNCTION_TYPE_VAR_6
3751 #undef DEF_FUNCTION_TYPE_VAR_7
3752 #undef DEF_POINTER_TYPE
3753 BT_LAST
3756 typedef enum c_builtin_type builtin_type;
3758 /* A temporary array for c_common_nodes_and_builtins. Used in
3759 communication with def_fn_type. */
3760 static tree builtin_types[(int) BT_LAST + 1];
3762 /* A helper function for c_common_nodes_and_builtins. Build function type
3763 for DEF with return type RET and N arguments. If VAR is true, then the
3764 function should be variadic after those N arguments.
3766 Takes special care not to ICE if any of the types involved are
3767 error_mark_node, which indicates that said type is not in fact available
3768 (see builtin_type_for_size). In which case the function type as a whole
3769 should be error_mark_node. */
3771 static void
3772 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3774 tree t;
3775 tree *args = XALLOCAVEC (tree, n);
3776 va_list list;
3777 int i;
3779 va_start (list, n);
3780 for (i = 0; i < n; ++i)
3782 builtin_type a = (builtin_type) va_arg (list, int);
3783 t = builtin_types[a];
3784 if (t == error_mark_node)
3785 goto egress;
3786 args[i] = t;
3789 t = builtin_types[ret];
3790 if (t == error_mark_node)
3791 goto egress;
3792 if (var)
3793 t = build_varargs_function_type_array (t, n, args);
3794 else
3795 t = build_function_type_array (t, n, args);
3797 egress:
3798 builtin_types[def] = t;
3799 va_end (list);
3802 /* Build builtin functions common to both C and C++ language
3803 frontends. */
3805 static void
3806 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3808 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3809 builtin_types[ENUM] = VALUE;
3810 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3811 def_fn_type (ENUM, RETURN, 0, 0);
3812 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3813 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3814 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3815 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3816 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3817 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3818 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3819 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3820 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3821 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3822 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3823 ARG6) \
3824 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3825 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3826 ARG6, ARG7) \
3827 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3828 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3829 ARG6, ARG7, ARG8) \
3830 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3831 ARG7, ARG8);
3832 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3833 ARG6, ARG7, ARG8, ARG9) \
3834 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3835 ARG7, ARG8, ARG9);
3836 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3837 ARG6, ARG7, ARG8, ARG9, ARG10) \
3838 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3839 ARG7, ARG8, ARG9, ARG10);
3840 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3841 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3842 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3843 ARG7, ARG8, ARG9, ARG10, ARG11);
3844 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3845 def_fn_type (ENUM, RETURN, 1, 0);
3846 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3847 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3848 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3849 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3850 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3851 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3852 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3853 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3854 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3855 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3856 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3857 ARG6) \
3858 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3859 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3860 ARG6, ARG7) \
3861 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3862 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3863 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3865 #include "builtin-types.def"
3867 #undef DEF_PRIMITIVE_TYPE
3868 #undef DEF_FUNCTION_TYPE_0
3869 #undef DEF_FUNCTION_TYPE_1
3870 #undef DEF_FUNCTION_TYPE_2
3871 #undef DEF_FUNCTION_TYPE_3
3872 #undef DEF_FUNCTION_TYPE_4
3873 #undef DEF_FUNCTION_TYPE_5
3874 #undef DEF_FUNCTION_TYPE_6
3875 #undef DEF_FUNCTION_TYPE_7
3876 #undef DEF_FUNCTION_TYPE_8
3877 #undef DEF_FUNCTION_TYPE_9
3878 #undef DEF_FUNCTION_TYPE_10
3879 #undef DEF_FUNCTION_TYPE_11
3880 #undef DEF_FUNCTION_TYPE_VAR_0
3881 #undef DEF_FUNCTION_TYPE_VAR_1
3882 #undef DEF_FUNCTION_TYPE_VAR_2
3883 #undef DEF_FUNCTION_TYPE_VAR_3
3884 #undef DEF_FUNCTION_TYPE_VAR_4
3885 #undef DEF_FUNCTION_TYPE_VAR_5
3886 #undef DEF_FUNCTION_TYPE_VAR_6
3887 #undef DEF_FUNCTION_TYPE_VAR_7
3888 #undef DEF_POINTER_TYPE
3889 builtin_types[(int) BT_LAST] = NULL_TREE;
3891 c_init_attributes ();
3893 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3894 NONANSI_P, ATTRS, IMPLICIT, COND) \
3895 if (NAME && COND) \
3896 def_builtin_1 (ENUM, NAME, CLASS, \
3897 builtin_types[(int) TYPE], \
3898 builtin_types[(int) LIBTYPE], \
3899 BOTH_P, FALLBACK_P, NONANSI_P, \
3900 built_in_attributes[(int) ATTRS], IMPLICIT);
3901 #include "builtins.def"
3903 targetm.init_builtins ();
3905 build_common_builtin_nodes ();
3907 if (flag_cilkplus)
3908 cilk_init_builtins ();
3911 /* Like get_identifier, but avoid warnings about null arguments when
3912 the argument may be NULL for targets where GCC lacks stdint.h type
3913 information. */
3915 static inline tree
3916 c_get_ident (const char *id)
3918 return get_identifier (id);
3921 /* Build tree nodes and builtin functions common to both C and C++ language
3922 frontends. */
3924 void
3925 c_common_nodes_and_builtins (void)
3927 int char16_type_size;
3928 int char32_type_size;
3929 int wchar_type_size;
3930 tree array_domain_type;
3931 tree va_list_ref_type_node;
3932 tree va_list_arg_type_node;
3933 int i;
3935 build_common_tree_nodes (flag_signed_char);
3937 /* Define `int' and `char' first so that dbx will output them first. */
3938 record_builtin_type (RID_INT, NULL, integer_type_node);
3939 record_builtin_type (RID_CHAR, "char", char_type_node);
3941 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3942 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3943 but not C. Are the conditionals here needed? */
3944 if (c_dialect_cxx ())
3945 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3946 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3947 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3948 record_builtin_type (RID_MAX, "long unsigned int",
3949 long_unsigned_type_node);
3951 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3953 char name[25];
3955 sprintf (name, "__int%d", int_n_data[i].bitsize);
3956 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
3957 int_n_trees[i].signed_type);
3958 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
3959 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
3962 if (c_dialect_cxx ())
3963 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3964 record_builtin_type (RID_MAX, "long long int",
3965 long_long_integer_type_node);
3966 record_builtin_type (RID_MAX, "long long unsigned int",
3967 long_long_unsigned_type_node);
3968 if (c_dialect_cxx ())
3969 record_builtin_type (RID_MAX, "long long unsigned",
3970 long_long_unsigned_type_node);
3971 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
3972 record_builtin_type (RID_MAX, "short unsigned int",
3973 short_unsigned_type_node);
3974 if (c_dialect_cxx ())
3975 record_builtin_type (RID_MAX, "unsigned short",
3976 short_unsigned_type_node);
3978 /* Define both `signed char' and `unsigned char'. */
3979 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
3980 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
3982 /* These are types that c_common_type_for_size and
3983 c_common_type_for_mode use. */
3984 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
3985 TYPE_DECL, NULL_TREE,
3986 intQI_type_node));
3987 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
3988 TYPE_DECL, NULL_TREE,
3989 intHI_type_node));
3990 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
3991 TYPE_DECL, NULL_TREE,
3992 intSI_type_node));
3993 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
3994 TYPE_DECL, NULL_TREE,
3995 intDI_type_node));
3996 #if HOST_BITS_PER_WIDE_INT >= 64
3997 /* Note that this is different than the __int128 type that's part of
3998 the generic __intN support. */
3999 if (targetm.scalar_mode_supported_p (TImode))
4000 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4001 TYPE_DECL,
4002 get_identifier ("__int128_t"),
4003 intTI_type_node));
4004 #endif
4005 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4006 TYPE_DECL, NULL_TREE,
4007 unsigned_intQI_type_node));
4008 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4009 TYPE_DECL, NULL_TREE,
4010 unsigned_intHI_type_node));
4011 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4012 TYPE_DECL, NULL_TREE,
4013 unsigned_intSI_type_node));
4014 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4015 TYPE_DECL, NULL_TREE,
4016 unsigned_intDI_type_node));
4017 #if HOST_BITS_PER_WIDE_INT >= 64
4018 if (targetm.scalar_mode_supported_p (TImode))
4019 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4020 TYPE_DECL,
4021 get_identifier ("__uint128_t"),
4022 unsigned_intTI_type_node));
4023 #endif
4025 /* Create the widest literal types. */
4026 if (targetm.scalar_mode_supported_p (TImode))
4028 widest_integer_literal_type_node = intTI_type_node;
4029 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4031 else
4033 widest_integer_literal_type_node = intDI_type_node;
4034 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4037 signed_size_type_node = c_common_signed_type (size_type_node);
4039 pid_type_node =
4040 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4042 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4043 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4044 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4046 if (!c_dialect_cxx ())
4047 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4048 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4049 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4050 FLOATN_NX_TYPE_NODE (i));
4052 /* Only supported decimal floating point extension if the target
4053 actually supports underlying modes. */
4054 if (targetm.scalar_mode_supported_p (SDmode)
4055 && targetm.scalar_mode_supported_p (DDmode)
4056 && targetm.scalar_mode_supported_p (TDmode))
4058 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4059 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4060 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4063 if (targetm.fixed_point_supported_p ())
4065 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4066 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4067 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4068 record_builtin_type (RID_MAX, "long long _Fract",
4069 long_long_fract_type_node);
4070 record_builtin_type (RID_MAX, "unsigned short _Fract",
4071 unsigned_short_fract_type_node);
4072 record_builtin_type (RID_MAX, "unsigned _Fract",
4073 unsigned_fract_type_node);
4074 record_builtin_type (RID_MAX, "unsigned long _Fract",
4075 unsigned_long_fract_type_node);
4076 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4077 unsigned_long_long_fract_type_node);
4078 record_builtin_type (RID_MAX, "_Sat short _Fract",
4079 sat_short_fract_type_node);
4080 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4081 record_builtin_type (RID_MAX, "_Sat long _Fract",
4082 sat_long_fract_type_node);
4083 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4084 sat_long_long_fract_type_node);
4085 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4086 sat_unsigned_short_fract_type_node);
4087 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4088 sat_unsigned_fract_type_node);
4089 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4090 sat_unsigned_long_fract_type_node);
4091 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4092 sat_unsigned_long_long_fract_type_node);
4093 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4094 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4095 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4096 record_builtin_type (RID_MAX, "long long _Accum",
4097 long_long_accum_type_node);
4098 record_builtin_type (RID_MAX, "unsigned short _Accum",
4099 unsigned_short_accum_type_node);
4100 record_builtin_type (RID_MAX, "unsigned _Accum",
4101 unsigned_accum_type_node);
4102 record_builtin_type (RID_MAX, "unsigned long _Accum",
4103 unsigned_long_accum_type_node);
4104 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4105 unsigned_long_long_accum_type_node);
4106 record_builtin_type (RID_MAX, "_Sat short _Accum",
4107 sat_short_accum_type_node);
4108 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4109 record_builtin_type (RID_MAX, "_Sat long _Accum",
4110 sat_long_accum_type_node);
4111 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4112 sat_long_long_accum_type_node);
4113 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4114 sat_unsigned_short_accum_type_node);
4115 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4116 sat_unsigned_accum_type_node);
4117 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4118 sat_unsigned_long_accum_type_node);
4119 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4120 sat_unsigned_long_long_accum_type_node);
4124 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4125 TYPE_DECL,
4126 get_identifier ("complex int"),
4127 complex_integer_type_node));
4128 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4129 TYPE_DECL,
4130 get_identifier ("complex float"),
4131 complex_float_type_node));
4132 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4133 TYPE_DECL,
4134 get_identifier ("complex double"),
4135 complex_double_type_node));
4136 lang_hooks.decls.pushdecl
4137 (build_decl (UNKNOWN_LOCATION,
4138 TYPE_DECL, get_identifier ("complex long double"),
4139 complex_long_double_type_node));
4141 if (!c_dialect_cxx ())
4142 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4143 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4145 char buf[30];
4146 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4147 floatn_nx_types[i].extended ? "x" : "");
4148 lang_hooks.decls.pushdecl
4149 (build_decl (UNKNOWN_LOCATION,
4150 TYPE_DECL,
4151 get_identifier (buf),
4152 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4155 if (c_dialect_cxx ())
4157 /* For C++, make fileptr_type_node a distinct void * type until
4158 FILE type is defined. */
4159 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4160 /* Likewise for const struct tm*. */
4161 const_tm_ptr_type_node = build_variant_type_copy (const_ptr_type_node);
4164 record_builtin_type (RID_VOID, NULL, void_type_node);
4166 /* Set the TYPE_NAME for any variants that were built before
4167 record_builtin_type gave names to the built-in types. */
4169 tree void_name = TYPE_NAME (void_type_node);
4170 TYPE_NAME (void_type_node) = NULL_TREE;
4171 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4172 = void_name;
4173 TYPE_NAME (void_type_node) = void_name;
4176 void_list_node = build_void_list_node ();
4178 /* Make a type to be the domain of a few array types
4179 whose domains don't really matter.
4180 200 is small enough that it always fits in size_t
4181 and large enough that it can hold most function names for the
4182 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4183 array_domain_type = build_index_type (size_int (200));
4185 /* Make a type for arrays of characters.
4186 With luck nothing will ever really depend on the length of this
4187 array type. */
4188 char_array_type_node
4189 = build_array_type (char_type_node, array_domain_type);
4191 string_type_node = build_pointer_type (char_type_node);
4192 const_string_type_node
4193 = build_pointer_type (build_qualified_type
4194 (char_type_node, TYPE_QUAL_CONST));
4196 /* This is special for C++ so functions can be overloaded. */
4197 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4198 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4199 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4200 underlying_wchar_type_node = wchar_type_node;
4201 if (c_dialect_cxx ())
4203 if (TYPE_UNSIGNED (wchar_type_node))
4204 wchar_type_node = make_unsigned_type (wchar_type_size);
4205 else
4206 wchar_type_node = make_signed_type (wchar_type_size);
4207 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4210 /* This is for wide string constants. */
4211 wchar_array_type_node
4212 = build_array_type (wchar_type_node, array_domain_type);
4214 /* Define 'char16_t'. */
4215 char16_type_node = get_identifier (CHAR16_TYPE);
4216 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4217 char16_type_size = TYPE_PRECISION (char16_type_node);
4218 if (c_dialect_cxx ())
4220 char16_type_node = make_unsigned_type (char16_type_size);
4222 if (cxx_dialect >= cxx11)
4223 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4226 /* This is for UTF-16 string constants. */
4227 char16_array_type_node
4228 = build_array_type (char16_type_node, array_domain_type);
4230 /* Define 'char32_t'. */
4231 char32_type_node = get_identifier (CHAR32_TYPE);
4232 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4233 char32_type_size = TYPE_PRECISION (char32_type_node);
4234 if (c_dialect_cxx ())
4236 char32_type_node = make_unsigned_type (char32_type_size);
4238 if (cxx_dialect >= cxx11)
4239 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4242 /* This is for UTF-32 string constants. */
4243 char32_array_type_node
4244 = build_array_type (char32_type_node, array_domain_type);
4246 wint_type_node =
4247 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4249 intmax_type_node =
4250 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4251 uintmax_type_node =
4252 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4254 if (SIG_ATOMIC_TYPE)
4255 sig_atomic_type_node =
4256 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4257 if (INT8_TYPE)
4258 int8_type_node =
4259 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4260 if (INT16_TYPE)
4261 int16_type_node =
4262 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4263 if (INT32_TYPE)
4264 int32_type_node =
4265 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4266 if (INT64_TYPE)
4267 int64_type_node =
4268 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4269 if (UINT8_TYPE)
4270 uint8_type_node =
4271 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4272 if (UINT16_TYPE)
4273 c_uint16_type_node = uint16_type_node =
4274 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4275 if (UINT32_TYPE)
4276 c_uint32_type_node = uint32_type_node =
4277 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4278 if (UINT64_TYPE)
4279 c_uint64_type_node = uint64_type_node =
4280 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4281 if (INT_LEAST8_TYPE)
4282 int_least8_type_node =
4283 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4284 if (INT_LEAST16_TYPE)
4285 int_least16_type_node =
4286 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4287 if (INT_LEAST32_TYPE)
4288 int_least32_type_node =
4289 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4290 if (INT_LEAST64_TYPE)
4291 int_least64_type_node =
4292 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4293 if (UINT_LEAST8_TYPE)
4294 uint_least8_type_node =
4295 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4296 if (UINT_LEAST16_TYPE)
4297 uint_least16_type_node =
4298 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4299 if (UINT_LEAST32_TYPE)
4300 uint_least32_type_node =
4301 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4302 if (UINT_LEAST64_TYPE)
4303 uint_least64_type_node =
4304 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4305 if (INT_FAST8_TYPE)
4306 int_fast8_type_node =
4307 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4308 if (INT_FAST16_TYPE)
4309 int_fast16_type_node =
4310 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4311 if (INT_FAST32_TYPE)
4312 int_fast32_type_node =
4313 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4314 if (INT_FAST64_TYPE)
4315 int_fast64_type_node =
4316 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4317 if (UINT_FAST8_TYPE)
4318 uint_fast8_type_node =
4319 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4320 if (UINT_FAST16_TYPE)
4321 uint_fast16_type_node =
4322 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4323 if (UINT_FAST32_TYPE)
4324 uint_fast32_type_node =
4325 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4326 if (UINT_FAST64_TYPE)
4327 uint_fast64_type_node =
4328 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4329 if (INTPTR_TYPE)
4330 intptr_type_node =
4331 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4332 if (UINTPTR_TYPE)
4333 uintptr_type_node =
4334 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4336 default_function_type
4337 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4338 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4340 lang_hooks.decls.pushdecl
4341 (build_decl (UNKNOWN_LOCATION,
4342 TYPE_DECL, get_identifier ("__builtin_va_list"),
4343 va_list_type_node));
4344 if (targetm.enum_va_list_p)
4346 int l;
4347 const char *pname;
4348 tree ptype;
4350 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4352 lang_hooks.decls.pushdecl
4353 (build_decl (UNKNOWN_LOCATION,
4354 TYPE_DECL, get_identifier (pname),
4355 ptype));
4360 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4362 va_list_arg_type_node = va_list_ref_type_node =
4363 build_pointer_type (TREE_TYPE (va_list_type_node));
4365 else
4367 va_list_arg_type_node = va_list_type_node;
4368 va_list_ref_type_node = build_reference_type (va_list_type_node);
4371 if (!flag_preprocess_only)
4372 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4374 main_identifier_node = get_identifier ("main");
4376 /* Create the built-in __null node. It is important that this is
4377 not shared. */
4378 null_node = make_int_cst (1, 1);
4379 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4381 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4382 memset (builtin_types, 0, sizeof (builtin_types));
4385 /* The number of named compound-literals generated thus far. */
4386 static GTY(()) int compound_literal_number;
4388 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4390 void
4391 set_compound_literal_name (tree decl)
4393 char *name;
4394 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4395 compound_literal_number);
4396 compound_literal_number++;
4397 DECL_NAME (decl) = get_identifier (name);
4400 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4401 TYPE and operand OP. */
4403 static tree
4404 build_va_arg_1 (location_t loc, tree type, tree op)
4406 tree expr = build1 (VA_ARG_EXPR, type, op);
4407 SET_EXPR_LOCATION (expr, loc);
4408 return expr;
4411 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4412 va_arg (EXPR, TYPE) at source location LOC. */
4414 tree
4415 build_va_arg (location_t loc, tree expr, tree type)
4417 tree va_type = TREE_TYPE (expr);
4418 tree canon_va_type = (va_type == error_mark_node
4419 ? error_mark_node
4420 : targetm.canonical_va_list_type (va_type));
4422 if (va_type == error_mark_node
4423 || canon_va_type == NULL_TREE)
4425 if (canon_va_type == NULL_TREE)
4426 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4428 /* Let's handle things neutrallly, if expr:
4429 - has undeclared type, or
4430 - is not an va_list type. */
4431 return build_va_arg_1 (loc, type, error_mark_node);
4434 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4436 /* Case 1: Not an array type. */
4438 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4439 type. */
4440 mark_addressable (expr);
4441 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4443 return build_va_arg_1 (loc, type, expr);
4446 /* Case 2: Array type.
4448 Background:
4450 For contrast, let's start with the simple case (case 1). If
4451 canon_va_type is not an array type, but say a char *, then when
4452 passing-by-value a va_list, the type of the va_list param decl is
4453 the same as for another va_list decl (all ap's are char *):
4455 f2_1 (char * ap)
4456 D.1815 = VA_ARG (&ap, 0B, 1);
4457 return D.1815;
4459 f2 (int i)
4460 char * ap.0;
4461 char * ap;
4462 __builtin_va_start (&ap, 0);
4463 ap.0 = ap;
4464 res = f2_1 (ap.0);
4465 __builtin_va_end (&ap);
4466 D.1812 = res;
4467 return D.1812;
4469 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4470 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4471 the same as for another va_list decl (case 2a, struct ap[1]).
4473 f2_1 (struct * ap)
4474 D.1844 = VA_ARG (ap, 0B, 0);
4475 return D.1844;
4477 f2 (int i)
4478 struct ap[1];
4479 __builtin_va_start (&ap, 0);
4480 res = f2_1 (&ap);
4481 __builtin_va_end (&ap);
4482 D.1841 = res;
4483 return D.1841;
4485 Case 2b is different because:
4486 - on the callee side, the parm decl has declared type va_list, but
4487 grokdeclarator changes the type of the parm decl to a pointer to the
4488 array elem type.
4489 - on the caller side, the pass-by-value uses &ap.
4491 We unify these two cases (case 2a: va_list is array type,
4492 case 2b: va_list is pointer to array elem type), by adding '&' for the
4493 array type case, such that we have a pointer to array elem in both
4494 cases. */
4496 if (TREE_CODE (va_type) == ARRAY_TYPE)
4498 /* Case 2a: va_list is array type. */
4500 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4501 elem type. */
4502 mark_addressable (expr);
4503 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4504 expr);
4506 /* Verify that &ap is still recognized as having va_list type. */
4507 tree canon_expr_type
4508 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4509 gcc_assert (canon_expr_type != NULL_TREE);
4511 else
4513 /* Case 2b: va_list is pointer to array elem type. */
4514 gcc_assert (POINTER_TYPE_P (va_type));
4516 /* Comparison as in std_canonical_va_list_type. */
4517 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4518 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4520 /* Don't take the address. We've already got '&ap'. */
4524 return build_va_arg_1 (loc, type, expr);
4528 /* Linked list of disabled built-in functions. */
4530 struct disabled_builtin
4532 const char *name;
4533 struct disabled_builtin *next;
4535 static disabled_builtin *disabled_builtins = NULL;
4537 static bool builtin_function_disabled_p (const char *);
4539 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4540 begins with "__builtin_", give an error. */
4542 void
4543 disable_builtin_function (const char *name)
4545 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4546 error ("cannot disable built-in function %qs", name);
4547 else
4549 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4550 new_disabled_builtin->name = name;
4551 new_disabled_builtin->next = disabled_builtins;
4552 disabled_builtins = new_disabled_builtin;
4557 /* Return true if the built-in function NAME has been disabled, false
4558 otherwise. */
4560 static bool
4561 builtin_function_disabled_p (const char *name)
4563 disabled_builtin *p;
4564 for (p = disabled_builtins; p != NULL; p = p->next)
4566 if (strcmp (name, p->name) == 0)
4567 return true;
4569 return false;
4573 /* Worker for DEF_BUILTIN.
4574 Possibly define a builtin function with one or two names.
4575 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4576 nonansi_p and flag_no_nonansi_builtin. */
4578 static void
4579 def_builtin_1 (enum built_in_function fncode,
4580 const char *name,
4581 enum built_in_class fnclass,
4582 tree fntype, tree libtype,
4583 bool both_p, bool fallback_p, bool nonansi_p,
4584 tree fnattrs, bool implicit_p)
4586 tree decl;
4587 const char *libname;
4589 if (fntype == error_mark_node)
4590 return;
4592 gcc_assert ((!both_p && !fallback_p)
4593 || !strncmp (name, "__builtin_",
4594 strlen ("__builtin_")));
4596 libname = name + strlen ("__builtin_");
4597 decl = add_builtin_function (name, fntype, fncode, fnclass,
4598 (fallback_p ? libname : NULL),
4599 fnattrs);
4601 set_builtin_decl (fncode, decl, implicit_p);
4603 if (both_p
4604 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4605 && !(nonansi_p && flag_no_nonansi_builtin))
4606 add_builtin_function (libname, libtype, fncode, fnclass,
4607 NULL, fnattrs);
4610 /* Nonzero if the type T promotes to int. This is (nearly) the
4611 integral promotions defined in ISO C99 6.3.1.1/2. */
4613 bool
4614 c_promoting_integer_type_p (const_tree t)
4616 switch (TREE_CODE (t))
4618 case INTEGER_TYPE:
4619 return (TYPE_MAIN_VARIANT (t) == char_type_node
4620 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4621 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4622 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4623 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4624 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4626 case ENUMERAL_TYPE:
4627 /* ??? Technically all enumerations not larger than an int
4628 promote to an int. But this is used along code paths
4629 that only want to notice a size change. */
4630 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4632 case BOOLEAN_TYPE:
4633 return true;
4635 default:
4636 return false;
4640 /* Return 1 if PARMS specifies a fixed number of parameters
4641 and none of their types is affected by default promotions. */
4643 bool
4644 self_promoting_args_p (const_tree parms)
4646 const_tree t;
4647 for (t = parms; t; t = TREE_CHAIN (t))
4649 tree type = TREE_VALUE (t);
4651 if (type == error_mark_node)
4652 continue;
4654 if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
4655 return false;
4657 if (type == NULL_TREE)
4658 return false;
4660 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4661 return false;
4663 if (c_promoting_integer_type_p (type))
4664 return false;
4666 return true;
4669 /* Recursively remove any '*' or '&' operator from TYPE. */
4670 tree
4671 strip_pointer_operator (tree t)
4673 while (POINTER_TYPE_P (t))
4674 t = TREE_TYPE (t);
4675 return t;
4678 /* Recursively remove pointer or array type from TYPE. */
4679 tree
4680 strip_pointer_or_array_types (tree t)
4682 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4683 t = TREE_TYPE (t);
4684 return t;
4687 /* Used to compare case labels. K1 and K2 are actually tree nodes
4688 representing case labels, or NULL_TREE for a `default' label.
4689 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4690 K2, and 0 if K1 and K2 are equal. */
4693 case_compare (splay_tree_key k1, splay_tree_key k2)
4695 /* Consider a NULL key (such as arises with a `default' label) to be
4696 smaller than anything else. */
4697 if (!k1)
4698 return k2 ? -1 : 0;
4699 else if (!k2)
4700 return k1 ? 1 : 0;
4702 return tree_int_cst_compare ((tree) k1, (tree) k2);
4705 /* Process a case label, located at LOC, for the range LOW_VALUE
4706 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4707 then this case label is actually a `default' label. If only
4708 HIGH_VALUE is NULL_TREE, then case label was declared using the
4709 usual C/C++ syntax, rather than the GNU case range extension.
4710 CASES is a tree containing all the case ranges processed so far;
4711 COND is the condition for the switch-statement itself.
4712 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4713 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4714 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4716 tree
4717 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4718 tree low_value, tree high_value, bool *outside_range_p)
4720 tree type;
4721 tree label;
4722 tree case_label;
4723 splay_tree_node node;
4725 /* Create the LABEL_DECL itself. */
4726 label = create_artificial_label (loc);
4728 /* If there was an error processing the switch condition, bail now
4729 before we get more confused. */
4730 if (!cond || cond == error_mark_node)
4731 goto error_out;
4733 if ((low_value && TREE_TYPE (low_value)
4734 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4735 || (high_value && TREE_TYPE (high_value)
4736 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4738 error_at (loc, "pointers are not permitted as case values");
4739 goto error_out;
4742 /* Case ranges are a GNU extension. */
4743 if (high_value)
4744 pedwarn (loc, OPT_Wpedantic,
4745 "range expressions in switch statements are non-standard");
4747 type = TREE_TYPE (cond);
4748 if (low_value)
4750 low_value = check_case_value (loc, low_value);
4751 low_value = convert_and_check (loc, type, low_value);
4752 if (low_value == error_mark_node)
4753 goto error_out;
4755 if (high_value)
4757 high_value = check_case_value (loc, high_value);
4758 high_value = convert_and_check (loc, type, high_value);
4759 if (high_value == error_mark_node)
4760 goto error_out;
4763 if (low_value && high_value)
4765 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4766 really a case range, even though it was written that way.
4767 Remove the HIGH_VALUE to simplify later processing. */
4768 if (tree_int_cst_equal (low_value, high_value))
4769 high_value = NULL_TREE;
4770 else if (!tree_int_cst_lt (low_value, high_value))
4771 warning_at (loc, 0, "empty range specified");
4774 /* See if the case is in range of the type of the original testing
4775 expression. If both low_value and high_value are out of range,
4776 don't insert the case label and return NULL_TREE. */
4777 if (low_value
4778 && !check_case_bounds (loc, type, orig_type,
4779 &low_value, high_value ? &high_value : NULL,
4780 outside_range_p))
4781 return NULL_TREE;
4783 /* Look up the LOW_VALUE in the table of case labels we already
4784 have. */
4785 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4786 /* If there was not an exact match, check for overlapping ranges.
4787 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4788 that's a `default' label and the only overlap is an exact match. */
4789 if (!node && (low_value || high_value))
4791 splay_tree_node low_bound;
4792 splay_tree_node high_bound;
4794 /* Even though there wasn't an exact match, there might be an
4795 overlap between this case range and another case range.
4796 Since we've (inductively) not allowed any overlapping case
4797 ranges, we simply need to find the greatest low case label
4798 that is smaller that LOW_VALUE, and the smallest low case
4799 label that is greater than LOW_VALUE. If there is an overlap
4800 it will occur in one of these two ranges. */
4801 low_bound = splay_tree_predecessor (cases,
4802 (splay_tree_key) low_value);
4803 high_bound = splay_tree_successor (cases,
4804 (splay_tree_key) low_value);
4806 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4807 the LOW_VALUE, so there is no need to check unless the
4808 LOW_BOUND is in fact itself a case range. */
4809 if (low_bound
4810 && CASE_HIGH ((tree) low_bound->value)
4811 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4812 low_value) >= 0)
4813 node = low_bound;
4814 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4815 range is bigger than the low end of the current range, so we
4816 are only interested if the current range is a real range, and
4817 not an ordinary case label. */
4818 else if (high_bound
4819 && high_value
4820 && (tree_int_cst_compare ((tree) high_bound->key,
4821 high_value)
4822 <= 0))
4823 node = high_bound;
4825 /* If there was an overlap, issue an error. */
4826 if (node)
4828 tree duplicate = CASE_LABEL ((tree) node->value);
4830 if (high_value)
4832 error_at (loc, "duplicate (or overlapping) case value");
4833 inform (DECL_SOURCE_LOCATION (duplicate),
4834 "this is the first entry overlapping that value");
4836 else if (low_value)
4838 error_at (loc, "duplicate case value") ;
4839 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4841 else
4843 error_at (loc, "multiple default labels in one switch");
4844 inform (DECL_SOURCE_LOCATION (duplicate),
4845 "this is the first default label");
4847 goto error_out;
4850 /* Add a CASE_LABEL to the statement-tree. */
4851 case_label = add_stmt (build_case_label (low_value, high_value, label));
4852 /* Register this case label in the splay tree. */
4853 splay_tree_insert (cases,
4854 (splay_tree_key) low_value,
4855 (splay_tree_value) case_label);
4857 return case_label;
4859 error_out:
4860 /* Add a label so that the back-end doesn't think that the beginning of
4861 the switch is unreachable. Note that we do not add a case label, as
4862 that just leads to duplicates and thence to failure later on. */
4863 if (!cases->root)
4865 tree t = create_artificial_label (loc);
4866 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4868 return error_mark_node;
4871 /* Finish an expression taking the address of LABEL (an
4872 IDENTIFIER_NODE). Returns an expression for the address.
4874 LOC is the location for the expression returned. */
4876 tree
4877 finish_label_address_expr (tree label, location_t loc)
4879 tree result;
4881 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
4883 if (label == error_mark_node)
4884 return error_mark_node;
4886 label = lookup_label (label);
4887 if (label == NULL_TREE)
4888 result = null_pointer_node;
4889 else
4891 TREE_USED (label) = 1;
4892 result = build1 (ADDR_EXPR, ptr_type_node, label);
4893 /* The current function is not necessarily uninlinable.
4894 Computed gotos are incompatible with inlining, but the value
4895 here could be used only in a diagnostic, for example. */
4896 protected_set_expr_location (result, loc);
4899 return result;
4903 /* Given a boolean expression ARG, return a tree representing an increment
4904 or decrement (as indicated by CODE) of ARG. The front end must check for
4905 invalid cases (e.g., decrement in C++). */
4906 tree
4907 boolean_increment (enum tree_code code, tree arg)
4909 tree val;
4910 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
4912 arg = stabilize_reference (arg);
4913 switch (code)
4915 case PREINCREMENT_EXPR:
4916 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4917 break;
4918 case POSTINCREMENT_EXPR:
4919 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4920 arg = save_expr (arg);
4921 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4922 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4923 break;
4924 case PREDECREMENT_EXPR:
4925 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
4926 invert_truthvalue_loc (input_location, arg));
4927 break;
4928 case POSTDECREMENT_EXPR:
4929 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
4930 invert_truthvalue_loc (input_location, arg));
4931 arg = save_expr (arg);
4932 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4933 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4934 break;
4935 default:
4936 gcc_unreachable ();
4938 TREE_SIDE_EFFECTS (val) = 1;
4939 return val;
4942 /* Built-in macros for stddef.h and stdint.h, that require macros
4943 defined in this file. */
4944 void
4945 c_stddef_cpp_builtins(void)
4947 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4948 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4949 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4950 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
4951 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
4952 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
4953 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
4954 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
4955 if (SIG_ATOMIC_TYPE)
4956 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
4957 if (INT8_TYPE)
4958 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
4959 if (INT16_TYPE)
4960 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
4961 if (INT32_TYPE)
4962 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
4963 if (INT64_TYPE)
4964 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
4965 if (UINT8_TYPE)
4966 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
4967 if (UINT16_TYPE)
4968 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
4969 if (UINT32_TYPE)
4970 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
4971 if (UINT64_TYPE)
4972 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
4973 if (INT_LEAST8_TYPE)
4974 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
4975 if (INT_LEAST16_TYPE)
4976 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
4977 if (INT_LEAST32_TYPE)
4978 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
4979 if (INT_LEAST64_TYPE)
4980 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
4981 if (UINT_LEAST8_TYPE)
4982 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
4983 if (UINT_LEAST16_TYPE)
4984 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
4985 if (UINT_LEAST32_TYPE)
4986 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
4987 if (UINT_LEAST64_TYPE)
4988 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
4989 if (INT_FAST8_TYPE)
4990 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
4991 if (INT_FAST16_TYPE)
4992 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
4993 if (INT_FAST32_TYPE)
4994 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
4995 if (INT_FAST64_TYPE)
4996 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
4997 if (UINT_FAST8_TYPE)
4998 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
4999 if (UINT_FAST16_TYPE)
5000 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5001 if (UINT_FAST32_TYPE)
5002 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5003 if (UINT_FAST64_TYPE)
5004 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5005 if (INTPTR_TYPE)
5006 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5007 if (UINTPTR_TYPE)
5008 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5011 static void
5012 c_init_attributes (void)
5014 /* Fill in the built_in_attributes array. */
5015 #define DEF_ATTR_NULL_TREE(ENUM) \
5016 built_in_attributes[(int) ENUM] = NULL_TREE;
5017 #define DEF_ATTR_INT(ENUM, VALUE) \
5018 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5019 #define DEF_ATTR_STRING(ENUM, VALUE) \
5020 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5021 #define DEF_ATTR_IDENT(ENUM, STRING) \
5022 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5023 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5024 built_in_attributes[(int) ENUM] \
5025 = tree_cons (built_in_attributes[(int) PURPOSE], \
5026 built_in_attributes[(int) VALUE], \
5027 built_in_attributes[(int) CHAIN]);
5028 #include "builtin-attrs.def"
5029 #undef DEF_ATTR_NULL_TREE
5030 #undef DEF_ATTR_INT
5031 #undef DEF_ATTR_IDENT
5032 #undef DEF_ATTR_TREE_LIST
5035 /* Check whether ALIGN is a valid user-specified alignment. If so,
5036 return its base-2 log; if not, output an error and return -1. If
5037 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
5038 no error. */
5040 check_user_alignment (const_tree align, bool allow_zero)
5042 int i;
5044 if (error_operand_p (align))
5045 return -1;
5046 if (TREE_CODE (align) != INTEGER_CST
5047 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5049 error ("requested alignment is not an integer constant");
5050 return -1;
5052 else if (allow_zero && integer_zerop (align))
5053 return -1;
5054 else if (tree_int_cst_sgn (align) == -1
5055 || (i = tree_log2 (align)) == -1)
5057 error ("requested alignment is not a positive power of 2");
5058 return -1;
5060 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5062 error ("requested alignment is too large");
5063 return -1;
5065 return i;
5068 /* Determine the ELF symbol visibility for DECL, which is either a
5069 variable or a function. It is an error to use this function if a
5070 definition of DECL is not available in this translation unit.
5071 Returns true if the final visibility has been determined by this
5072 function; false if the caller is free to make additional
5073 modifications. */
5075 bool
5076 c_determine_visibility (tree decl)
5078 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5080 /* If the user explicitly specified the visibility with an
5081 attribute, honor that. DECL_VISIBILITY will have been set during
5082 the processing of the attribute. We check for an explicit
5083 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5084 to distinguish the use of an attribute from the use of a "#pragma
5085 GCC visibility push(...)"; in the latter case we still want other
5086 considerations to be able to overrule the #pragma. */
5087 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5088 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5089 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5090 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5091 return true;
5093 /* Set default visibility to whatever the user supplied with
5094 visibility_specified depending on #pragma GCC visibility. */
5095 if (!DECL_VISIBILITY_SPECIFIED (decl))
5097 if (visibility_options.inpragma
5098 || DECL_VISIBILITY (decl) != default_visibility)
5100 DECL_VISIBILITY (decl) = default_visibility;
5101 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5102 /* If visibility changed and DECL already has DECL_RTL, ensure
5103 symbol flags are updated. */
5104 if (((VAR_P (decl) && TREE_STATIC (decl))
5105 || TREE_CODE (decl) == FUNCTION_DECL)
5106 && DECL_RTL_SET_P (decl))
5107 make_decl_rtl (decl);
5110 return false;
5113 /* Data to communicate through check_function_arguments_recurse between
5114 check_function_nonnull and check_nonnull_arg. */
5116 struct nonnull_arg_ctx
5118 location_t loc;
5119 bool warned_p;
5122 /* Check the argument list of a function call for null in argument slots
5123 that are marked as requiring a non-null pointer argument. The NARGS
5124 arguments are passed in the array ARGARRAY. Return true if we have
5125 warned. */
5127 static bool
5128 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5130 tree a;
5131 int i;
5133 attrs = lookup_attribute ("nonnull", attrs);
5134 if (attrs == NULL_TREE)
5135 return false;
5137 a = attrs;
5138 /* See if any of the nonnull attributes has no arguments. If so,
5139 then every pointer argument is checked (in which case the check
5140 for pointer type is done in check_nonnull_arg). */
5141 if (TREE_VALUE (a) != NULL_TREE)
5143 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5144 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5146 struct nonnull_arg_ctx ctx = { loc, false };
5147 if (a != NULL_TREE)
5148 for (i = 0; i < nargs; i++)
5149 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5150 i + 1);
5151 else
5153 /* Walk the argument list. If we encounter an argument number we
5154 should check for non-null, do it. */
5155 for (i = 0; i < nargs; i++)
5157 for (a = attrs; ; a = TREE_CHAIN (a))
5159 a = lookup_attribute ("nonnull", a);
5160 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5161 break;
5164 if (a != NULL_TREE)
5165 check_function_arguments_recurse (check_nonnull_arg, &ctx,
5166 argarray[i], i + 1);
5169 return ctx.warned_p;
5172 /* Check that the Nth argument of a function call (counting backwards
5173 from the end) is a (pointer)0. The NARGS arguments are passed in the
5174 array ARGARRAY. */
5176 static void
5177 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5179 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5181 if (attr)
5183 int len = 0;
5184 int pos = 0;
5185 tree sentinel;
5186 function_args_iterator iter;
5187 tree t;
5189 /* Skip over the named arguments. */
5190 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5192 if (len == nargs)
5193 break;
5194 len++;
5197 if (TREE_VALUE (attr))
5199 tree p = TREE_VALUE (TREE_VALUE (attr));
5200 pos = TREE_INT_CST_LOW (p);
5203 /* The sentinel must be one of the varargs, i.e.
5204 in position >= the number of fixed arguments. */
5205 if ((nargs - 1 - pos) < len)
5207 warning (OPT_Wformat_,
5208 "not enough variable arguments to fit a sentinel");
5209 return;
5212 /* Validate the sentinel. */
5213 sentinel = argarray[nargs - 1 - pos];
5214 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5215 || !integer_zerop (sentinel))
5216 /* Although __null (in C++) is only an integer we allow it
5217 nevertheless, as we are guaranteed that it's exactly
5218 as wide as a pointer, and we don't want to force
5219 users to cast the NULL they have written there.
5220 We warn with -Wstrict-null-sentinel, though. */
5221 && (warn_strict_null_sentinel || null_node != sentinel))
5222 warning (OPT_Wformat_, "missing sentinel in function call");
5226 /* Check that the same argument isn't passed to restrict arguments
5227 and other arguments. */
5229 static void
5230 check_function_restrict (const_tree fndecl, const_tree fntype,
5231 int nargs, tree *argarray)
5233 int i;
5234 tree parms;
5236 if (fndecl
5237 && TREE_CODE (fndecl) == FUNCTION_DECL
5238 && DECL_ARGUMENTS (fndecl))
5239 parms = DECL_ARGUMENTS (fndecl);
5240 else
5241 parms = TYPE_ARG_TYPES (fntype);
5243 for (i = 0; i < nargs; i++)
5244 TREE_VISITED (argarray[i]) = 0;
5246 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5248 tree type;
5249 if (TREE_CODE (parms) == PARM_DECL)
5251 type = TREE_TYPE (parms);
5252 parms = DECL_CHAIN (parms);
5254 else
5256 type = TREE_VALUE (parms);
5257 parms = TREE_CHAIN (parms);
5259 if (POINTER_TYPE_P (type)
5260 && TYPE_RESTRICT (type)
5261 && !TYPE_READONLY (TREE_TYPE (type)))
5262 warn_for_restrict (i, argarray, nargs);
5265 for (i = 0; i < nargs; i++)
5266 TREE_VISITED (argarray[i]) = 0;
5269 /* Helper for check_function_nonnull; given a list of operands which
5270 must be non-null in ARGS, determine if operand PARAM_NUM should be
5271 checked. */
5273 static bool
5274 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5276 unsigned HOST_WIDE_INT arg_num = 0;
5278 for (; args; args = TREE_CHAIN (args))
5280 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5282 gcc_assert (found);
5284 if (arg_num == param_num)
5285 return true;
5287 return false;
5290 /* Check that the function argument PARAM (which is operand number
5291 PARAM_NUM) is non-null. This is called by check_function_nonnull
5292 via check_function_arguments_recurse. */
5294 static void
5295 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5297 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5299 /* Just skip checking the argument if it's not a pointer. This can
5300 happen if the "nonnull" attribute was given without an operand
5301 list (which means to check every pointer argument). */
5303 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5304 return;
5306 /* When not optimizing diagnose the simple cases of null arguments.
5307 When optimization is enabled defer the checking until expansion
5308 when more cases can be detected. */
5309 if (integer_zerop (param))
5311 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5312 "required (argument %lu)", (unsigned long) param_num);
5313 pctx->warned_p = true;
5317 /* Helper for nonnull attribute handling; fetch the operand number
5318 from the attribute argument list. */
5320 bool
5321 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5323 /* Verify the arg number is a small constant. */
5324 if (tree_fits_uhwi_p (arg_num_expr))
5326 *valp = TREE_INT_CST_LOW (arg_num_expr);
5327 return true;
5329 else
5330 return false;
5333 /* Arguments being collected for optimization. */
5334 typedef const char *const_char_p; /* For DEF_VEC_P. */
5335 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5338 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5339 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5340 false for #pragma GCC optimize. */
5342 bool
5343 parse_optimize_options (tree args, bool attr_p)
5345 bool ret = true;
5346 unsigned opt_argc;
5347 unsigned i;
5348 const char **opt_argv;
5349 struct cl_decoded_option *decoded_options;
5350 unsigned int decoded_options_count;
5351 tree ap;
5353 /* Build up argv vector. Just in case the string is stored away, use garbage
5354 collected strings. */
5355 vec_safe_truncate (optimize_args, 0);
5356 vec_safe_push (optimize_args, (const char *) NULL);
5358 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5360 tree value = TREE_VALUE (ap);
5362 if (TREE_CODE (value) == INTEGER_CST)
5364 char buffer[20];
5365 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5366 vec_safe_push (optimize_args, ggc_strdup (buffer));
5369 else if (TREE_CODE (value) == STRING_CST)
5371 /* Split string into multiple substrings. */
5372 size_t len = TREE_STRING_LENGTH (value);
5373 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5374 char *end = p + len;
5375 char *comma;
5376 char *next_p = p;
5378 while (next_p != NULL)
5380 size_t len2;
5381 char *q, *r;
5383 p = next_p;
5384 comma = strchr (p, ',');
5385 if (comma)
5387 len2 = comma - p;
5388 *comma = '\0';
5389 next_p = comma+1;
5391 else
5393 len2 = end - p;
5394 next_p = NULL;
5397 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5399 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5400 options. */
5401 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5403 ret = false;
5404 if (attr_p)
5405 warning (OPT_Wattributes,
5406 "bad option %qs to attribute %<optimize%>", p);
5407 else
5408 warning (OPT_Wpragmas,
5409 "bad option %qs to pragma %<optimize%>", p);
5410 continue;
5413 if (*p != '-')
5415 *r++ = '-';
5417 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5418 itself is -Os, and any other switch begins with a -f. */
5419 if ((*p >= '0' && *p <= '9')
5420 || (p[0] == 's' && p[1] == '\0'))
5421 *r++ = 'O';
5422 else if (*p != 'O')
5423 *r++ = 'f';
5426 memcpy (r, p, len2);
5427 r[len2] = '\0';
5428 vec_safe_push (optimize_args, (const char *) q);
5434 opt_argc = optimize_args->length ();
5435 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5437 for (i = 1; i < opt_argc; i++)
5438 opt_argv[i] = (*optimize_args)[i];
5440 /* Now parse the options. */
5441 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5442 &decoded_options,
5443 &decoded_options_count);
5444 /* Drop non-Optimization options. */
5445 unsigned j = 1;
5446 for (i = 1; i < decoded_options_count; ++i)
5448 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5450 ret = false;
5451 if (attr_p)
5452 warning (OPT_Wattributes,
5453 "bad option %qs to attribute %<optimize%>",
5454 decoded_options[i].orig_option_with_args_text);
5455 else
5456 warning (OPT_Wpragmas,
5457 "bad option %qs to pragma %<optimize%>",
5458 decoded_options[i].orig_option_with_args_text);
5459 continue;
5461 if (i != j)
5462 decoded_options[j] = decoded_options[i];
5463 j++;
5465 decoded_options_count = j;
5466 /* And apply them. */
5467 decode_options (&global_options, &global_options_set,
5468 decoded_options, decoded_options_count,
5469 input_location, global_dc);
5471 targetm.override_options_after_change();
5473 optimize_args->truncate (0);
5474 return ret;
5477 /* Check whether ATTR is a valid attribute fallthrough. */
5479 bool
5480 attribute_fallthrough_p (tree attr)
5482 if (attr == error_mark_node)
5483 return false;
5484 tree t = lookup_attribute ("fallthrough", attr);
5485 if (t == NULL_TREE)
5486 return false;
5487 /* This attribute shall appear at most once in each attribute-list. */
5488 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5489 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5490 "times");
5491 /* No attribute-argument-clause shall be present. */
5492 else if (TREE_VALUE (t) != NULL_TREE)
5493 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5494 "a parameter");
5495 /* Warn if other attributes are found. */
5496 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5498 tree name = get_attribute_name (t);
5499 if (!is_attribute_p ("fallthrough", name))
5500 warning (OPT_Wattributes, "%qE attribute ignored", name);
5502 return true;
5506 /* Check for valid arguments being passed to a function with FNTYPE.
5507 There are NARGS arguments in the array ARGARRAY. LOC should be used for
5508 diagnostics. Return true if -Wnonnull warning has been diagnosed. */
5509 bool
5510 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5511 int nargs, tree *argarray)
5513 bool warned_p = false;
5515 /* Check for null being passed in a pointer argument that must be
5516 non-null. We also need to do this if format checking is enabled. */
5518 if (warn_nonnull)
5519 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5520 nargs, argarray);
5522 /* Check for errors in format strings. */
5524 if (warn_format || warn_suggest_attribute_format)
5525 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
5527 if (warn_format)
5528 check_function_sentinel (fntype, nargs, argarray);
5530 if (warn_restrict)
5531 check_function_restrict (fndecl, fntype, nargs, argarray);
5532 return warned_p;
5535 /* Generic argument checking recursion routine. PARAM is the argument to
5536 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5537 once the argument is resolved. CTX is context for the callback. */
5538 void
5539 check_function_arguments_recurse (void (*callback)
5540 (void *, tree, unsigned HOST_WIDE_INT),
5541 void *ctx, tree param,
5542 unsigned HOST_WIDE_INT param_num)
5544 if (CONVERT_EXPR_P (param)
5545 && (TYPE_PRECISION (TREE_TYPE (param))
5546 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5548 /* Strip coercion. */
5549 check_function_arguments_recurse (callback, ctx,
5550 TREE_OPERAND (param, 0), param_num);
5551 return;
5554 if (TREE_CODE (param) == CALL_EXPR)
5556 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5557 tree attrs;
5558 bool found_format_arg = false;
5560 /* See if this is a call to a known internationalization function
5561 that modifies a format arg. Such a function may have multiple
5562 format_arg attributes (for example, ngettext). */
5564 for (attrs = TYPE_ATTRIBUTES (type);
5565 attrs;
5566 attrs = TREE_CHAIN (attrs))
5567 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5569 tree inner_arg;
5570 tree format_num_expr;
5571 int format_num;
5572 int i;
5573 call_expr_arg_iterator iter;
5575 /* Extract the argument number, which was previously checked
5576 to be valid. */
5577 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5579 format_num = tree_to_uhwi (format_num_expr);
5581 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5582 inner_arg != NULL_TREE;
5583 inner_arg = next_call_expr_arg (&iter), i++)
5584 if (i == format_num)
5586 check_function_arguments_recurse (callback, ctx,
5587 inner_arg, param_num);
5588 found_format_arg = true;
5589 break;
5593 /* If we found a format_arg attribute and did a recursive check,
5594 we are done with checking this argument. Otherwise, we continue
5595 and this will be considered a non-literal. */
5596 if (found_format_arg)
5597 return;
5600 if (TREE_CODE (param) == COND_EXPR)
5602 /* Simplify to avoid warning for an impossible case. */
5603 param = fold_for_warn (param);
5604 if (TREE_CODE (param) == COND_EXPR)
5606 /* Check both halves of the conditional expression. */
5607 check_function_arguments_recurse (callback, ctx,
5608 TREE_OPERAND (param, 1),
5609 param_num);
5610 check_function_arguments_recurse (callback, ctx,
5611 TREE_OPERAND (param, 2),
5612 param_num);
5613 return;
5617 (*callback) (ctx, param, param_num);
5620 /* Checks for a builtin function FNDECL that the number of arguments
5621 NARGS against the required number REQUIRED and issues an error if
5622 there is a mismatch. Returns true if the number of arguments is
5623 correct, otherwise false. LOC is the location of FNDECL. */
5625 static bool
5626 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5627 int required)
5629 if (nargs < required)
5631 error_at (loc, "too few arguments to function %qE", fndecl);
5632 return false;
5634 else if (nargs > required)
5636 error_at (loc, "too many arguments to function %qE", fndecl);
5637 return false;
5639 return true;
5642 /* Helper macro for check_builtin_function_arguments. */
5643 #define ARG_LOCATION(N) \
5644 (arg_loc.is_empty () \
5645 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5646 : expansion_point_location (arg_loc[(N)]))
5648 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5649 Returns false if there was an error, otherwise true. LOC is the
5650 location of the function; ARG_LOC is a vector of locations of the
5651 arguments. */
5653 bool
5654 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5655 tree fndecl, int nargs, tree *args)
5657 if (!DECL_BUILT_IN (fndecl)
5658 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5659 return true;
5661 switch (DECL_FUNCTION_CODE (fndecl))
5663 case BUILT_IN_ALLOCA_WITH_ALIGN:
5665 /* Get the requested alignment (in bits) if it's a constant
5666 integer expression. */
5667 unsigned HOST_WIDE_INT align
5668 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5670 /* Determine if the requested alignment is a power of 2. */
5671 if ((align & (align - 1)))
5672 align = 0;
5674 /* The maximum alignment in bits corresponding to the same
5675 maximum in bytes enforced in check_user_alignment(). */
5676 unsigned maxalign = (UINT_MAX >> 1) + 1;
5678 /* Reject invalid alignments. */
5679 if (align < BITS_PER_UNIT || maxalign < align)
5681 error_at (ARG_LOCATION (1),
5682 "second argument to function %qE must be a constant "
5683 "integer power of 2 between %qi and %qu bits",
5684 fndecl, BITS_PER_UNIT, maxalign);
5685 return false;
5687 return true;
5690 case BUILT_IN_CONSTANT_P:
5691 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5693 case BUILT_IN_ISFINITE:
5694 case BUILT_IN_ISINF:
5695 case BUILT_IN_ISINF_SIGN:
5696 case BUILT_IN_ISNAN:
5697 case BUILT_IN_ISNORMAL:
5698 case BUILT_IN_SIGNBIT:
5699 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5701 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5703 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5704 "call to function %qE", fndecl);
5705 return false;
5707 return true;
5709 return false;
5711 case BUILT_IN_ISGREATER:
5712 case BUILT_IN_ISGREATEREQUAL:
5713 case BUILT_IN_ISLESS:
5714 case BUILT_IN_ISLESSEQUAL:
5715 case BUILT_IN_ISLESSGREATER:
5716 case BUILT_IN_ISUNORDERED:
5717 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5719 enum tree_code code0, code1;
5720 code0 = TREE_CODE (TREE_TYPE (args[0]));
5721 code1 = TREE_CODE (TREE_TYPE (args[1]));
5722 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5723 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5724 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5726 error_at (loc, "non-floating-point arguments in call to "
5727 "function %qE", fndecl);
5728 return false;
5730 return true;
5732 return false;
5734 case BUILT_IN_FPCLASSIFY:
5735 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5737 for (unsigned int i = 0; i < 5; i++)
5738 if (TREE_CODE (args[i]) != INTEGER_CST)
5740 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5741 "call to function %qE", i + 1, fndecl);
5742 return false;
5745 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5747 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5748 "call to function %qE", fndecl);
5749 return false;
5751 return true;
5753 return false;
5755 case BUILT_IN_ASSUME_ALIGNED:
5756 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5758 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5760 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5761 "function %qE", fndecl);
5762 return false;
5764 return true;
5766 return false;
5768 case BUILT_IN_ADD_OVERFLOW:
5769 case BUILT_IN_SUB_OVERFLOW:
5770 case BUILT_IN_MUL_OVERFLOW:
5771 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5773 unsigned i;
5774 for (i = 0; i < 2; i++)
5775 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5777 error_at (ARG_LOCATION (i), "argument %u in call to function "
5778 "%qE does not have integral type", i + 1, fndecl);
5779 return false;
5781 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5782 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5784 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5785 "does not have pointer to integral type", fndecl);
5786 return false;
5788 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5790 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5791 "has pointer to enumerated type", fndecl);
5792 return false;
5794 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5796 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5797 "has pointer to boolean type", fndecl);
5798 return false;
5800 return true;
5802 return false;
5804 case BUILT_IN_ADD_OVERFLOW_P:
5805 case BUILT_IN_SUB_OVERFLOW_P:
5806 case BUILT_IN_MUL_OVERFLOW_P:
5807 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5809 unsigned i;
5810 for (i = 0; i < 3; i++)
5811 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5813 error_at (ARG_LOCATION (i), "argument %u in call to function "
5814 "%qE does not have integral type", i + 1, fndecl);
5815 return false;
5817 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5819 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5820 "%qE has enumerated type", fndecl);
5821 return false;
5823 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5825 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5826 "%qE has boolean type", fndecl);
5827 return false;
5829 return true;
5831 return false;
5833 default:
5834 return true;
5838 /* Function to help qsort sort FIELD_DECLs by name order. */
5841 field_decl_cmp (const void *x_p, const void *y_p)
5843 const tree *const x = (const tree *const) x_p;
5844 const tree *const y = (const tree *const) y_p;
5846 if (DECL_NAME (*x) == DECL_NAME (*y))
5847 /* A nontype is "greater" than a type. */
5848 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5849 if (DECL_NAME (*x) == NULL_TREE)
5850 return -1;
5851 if (DECL_NAME (*y) == NULL_TREE)
5852 return 1;
5853 if (DECL_NAME (*x) < DECL_NAME (*y))
5854 return -1;
5855 return 1;
5858 static struct {
5859 gt_pointer_operator new_value;
5860 void *cookie;
5861 } resort_data;
5863 /* This routine compares two fields like field_decl_cmp but using the
5864 pointer operator in resort_data. */
5866 static int
5867 resort_field_decl_cmp (const void *x_p, const void *y_p)
5869 const tree *const x = (const tree *const) x_p;
5870 const tree *const y = (const tree *const) y_p;
5872 if (DECL_NAME (*x) == DECL_NAME (*y))
5873 /* A nontype is "greater" than a type. */
5874 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5875 if (DECL_NAME (*x) == NULL_TREE)
5876 return -1;
5877 if (DECL_NAME (*y) == NULL_TREE)
5878 return 1;
5880 tree d1 = DECL_NAME (*x);
5881 tree d2 = DECL_NAME (*y);
5882 resort_data.new_value (&d1, resort_data.cookie);
5883 resort_data.new_value (&d2, resort_data.cookie);
5884 if (d1 < d2)
5885 return -1;
5887 return 1;
5890 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
5892 void
5893 resort_sorted_fields (void *obj,
5894 void * ARG_UNUSED (orig_obj),
5895 gt_pointer_operator new_value,
5896 void *cookie)
5898 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
5899 resort_data.new_value = new_value;
5900 resort_data.cookie = cookie;
5901 qsort (&sf->elts[0], sf->len, sizeof (tree),
5902 resort_field_decl_cmp);
5905 /* Subroutine of c_parse_error.
5906 Return the result of concatenating LHS and RHS. RHS is really
5907 a string literal, its first character is indicated by RHS_START and
5908 RHS_SIZE is its length (including the terminating NUL character).
5910 The caller is responsible for deleting the returned pointer. */
5912 static char *
5913 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5915 const int lhs_size = strlen (lhs);
5916 char *result = XNEWVEC (char, lhs_size + rhs_size);
5917 strncpy (result, lhs, lhs_size);
5918 strncpy (result + lhs_size, rhs_start, rhs_size);
5919 return result;
5922 /* Issue the error given by GMSGID, indicating that it occurred before
5923 TOKEN, which had the associated VALUE. */
5925 void
5926 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
5927 tree value, unsigned char token_flags)
5929 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
5931 char *message = NULL;
5933 if (token_type == CPP_EOF)
5934 message = catenate_messages (gmsgid, " at end of input");
5935 else if (token_type == CPP_CHAR
5936 || token_type == CPP_WCHAR
5937 || token_type == CPP_CHAR16
5938 || token_type == CPP_CHAR32
5939 || token_type == CPP_UTF8CHAR)
5941 unsigned int val = TREE_INT_CST_LOW (value);
5942 const char *prefix;
5944 switch (token_type)
5946 default:
5947 prefix = "";
5948 break;
5949 case CPP_WCHAR:
5950 prefix = "L";
5951 break;
5952 case CPP_CHAR16:
5953 prefix = "u";
5954 break;
5955 case CPP_CHAR32:
5956 prefix = "U";
5957 break;
5958 case CPP_UTF8CHAR:
5959 prefix = "u8";
5960 break;
5963 if (val <= UCHAR_MAX && ISGRAPH (val))
5964 message = catenate_messages (gmsgid, " before %s'%c'");
5965 else
5966 message = catenate_messages (gmsgid, " before %s'\\x%x'");
5968 error (message, prefix, val);
5969 free (message);
5970 message = NULL;
5972 else if (token_type == CPP_CHAR_USERDEF
5973 || token_type == CPP_WCHAR_USERDEF
5974 || token_type == CPP_CHAR16_USERDEF
5975 || token_type == CPP_CHAR32_USERDEF
5976 || token_type == CPP_UTF8CHAR_USERDEF)
5977 message = catenate_messages (gmsgid,
5978 " before user-defined character literal");
5979 else if (token_type == CPP_STRING_USERDEF
5980 || token_type == CPP_WSTRING_USERDEF
5981 || token_type == CPP_STRING16_USERDEF
5982 || token_type == CPP_STRING32_USERDEF
5983 || token_type == CPP_UTF8STRING_USERDEF)
5984 message = catenate_messages (gmsgid, " before user-defined string literal");
5985 else if (token_type == CPP_STRING
5986 || token_type == CPP_WSTRING
5987 || token_type == CPP_STRING16
5988 || token_type == CPP_STRING32
5989 || token_type == CPP_UTF8STRING)
5990 message = catenate_messages (gmsgid, " before string constant");
5991 else if (token_type == CPP_NUMBER)
5992 message = catenate_messages (gmsgid, " before numeric constant");
5993 else if (token_type == CPP_NAME)
5995 message = catenate_messages (gmsgid, " before %qE");
5996 error (message, value);
5997 free (message);
5998 message = NULL;
6000 else if (token_type == CPP_PRAGMA)
6001 message = catenate_messages (gmsgid, " before %<#pragma%>");
6002 else if (token_type == CPP_PRAGMA_EOL)
6003 message = catenate_messages (gmsgid, " before end of line");
6004 else if (token_type == CPP_DECLTYPE)
6005 message = catenate_messages (gmsgid, " before %<decltype%>");
6006 else if (token_type < N_TTYPES)
6008 message = catenate_messages (gmsgid, " before %qs token");
6009 error (message, cpp_type2name (token_type, token_flags));
6010 free (message);
6011 message = NULL;
6013 else
6014 error (gmsgid);
6016 if (message)
6018 error (message);
6019 free (message);
6021 #undef catenate_messages
6024 /* Return the gcc option code associated with the reason for a cpp
6025 message, or 0 if none. */
6027 static int
6028 c_option_controlling_cpp_error (int reason)
6030 const struct cpp_reason_option_codes_t *entry;
6032 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6034 if (entry->reason == reason)
6035 return entry->option_code;
6037 return 0;
6040 /* Callback from cpp_error for PFILE to print diagnostics from the
6041 preprocessor. The diagnostic is of type LEVEL, with REASON set
6042 to the reason code if LEVEL is represents a warning, at location
6043 RICHLOC unless this is after lexing and the compiler's location
6044 should be used instead; MSG is the translated message and AP
6045 the arguments. Returns true if a diagnostic was emitted, false
6046 otherwise. */
6048 bool
6049 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6050 rich_location *richloc,
6051 const char *msg, va_list *ap)
6053 diagnostic_info diagnostic;
6054 diagnostic_t dlevel;
6055 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6056 bool ret;
6058 switch (level)
6060 case CPP_DL_WARNING_SYSHDR:
6061 if (flag_no_output)
6062 return false;
6063 global_dc->dc_warn_system_headers = 1;
6064 /* Fall through. */
6065 case CPP_DL_WARNING:
6066 if (flag_no_output)
6067 return false;
6068 dlevel = DK_WARNING;
6069 break;
6070 case CPP_DL_PEDWARN:
6071 if (flag_no_output && !flag_pedantic_errors)
6072 return false;
6073 dlevel = DK_PEDWARN;
6074 break;
6075 case CPP_DL_ERROR:
6076 dlevel = DK_ERROR;
6077 break;
6078 case CPP_DL_ICE:
6079 dlevel = DK_ICE;
6080 break;
6081 case CPP_DL_NOTE:
6082 dlevel = DK_NOTE;
6083 break;
6084 case CPP_DL_FATAL:
6085 dlevel = DK_FATAL;
6086 break;
6087 default:
6088 gcc_unreachable ();
6090 if (done_lexing)
6091 richloc->set_range (line_table, 0, input_location, true);
6092 diagnostic_set_info_translated (&diagnostic, msg, ap,
6093 richloc, dlevel);
6094 diagnostic_override_option_index (&diagnostic,
6095 c_option_controlling_cpp_error (reason));
6096 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
6097 if (level == CPP_DL_WARNING_SYSHDR)
6098 global_dc->dc_warn_system_headers = save_warn_system_headers;
6099 return ret;
6102 /* Convert a character from the host to the target execution character
6103 set. cpplib handles this, mostly. */
6105 HOST_WIDE_INT
6106 c_common_to_target_charset (HOST_WIDE_INT c)
6108 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6109 zero-extended under -fno-signed-char. cpplib insists that characters
6110 and character constants are always unsigned. Hence we must convert
6111 back and forth. */
6112 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6114 uc = cpp_host_to_exec_charset (parse_in, uc);
6116 if (flag_signed_char)
6117 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6118 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6119 else
6120 return uc;
6123 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6124 references with an INDIRECT_REF of a constant at the bottom; much like the
6125 traditional rendering of offsetof as a macro. Return the folded result. */
6127 tree
6128 fold_offsetof_1 (tree expr, enum tree_code ctx)
6130 tree base, off, t;
6131 tree_code code = TREE_CODE (expr);
6132 switch (code)
6134 case ERROR_MARK:
6135 return expr;
6137 case VAR_DECL:
6138 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6139 return error_mark_node;
6141 case CALL_EXPR:
6142 case TARGET_EXPR:
6143 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6144 return error_mark_node;
6146 case NOP_EXPR:
6147 case INDIRECT_REF:
6148 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6150 error ("cannot apply %<offsetof%> to a non constant address");
6151 return error_mark_node;
6153 return TREE_OPERAND (expr, 0);
6155 case COMPONENT_REF:
6156 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6157 if (base == error_mark_node)
6158 return base;
6160 t = TREE_OPERAND (expr, 1);
6161 if (DECL_C_BIT_FIELD (t))
6163 error ("attempt to take address of bit-field structure "
6164 "member %qD", t);
6165 return error_mark_node;
6167 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6168 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6169 / BITS_PER_UNIT));
6170 break;
6172 case ARRAY_REF:
6173 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6174 if (base == error_mark_node)
6175 return base;
6177 t = TREE_OPERAND (expr, 1);
6179 /* Check if the offset goes beyond the upper bound of the array. */
6180 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6182 tree upbound = array_ref_up_bound (expr);
6183 if (upbound != NULL_TREE
6184 && TREE_CODE (upbound) == INTEGER_CST
6185 && !tree_int_cst_equal (upbound,
6186 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6188 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6189 upbound = size_binop (PLUS_EXPR, upbound,
6190 build_int_cst (TREE_TYPE (upbound), 1));
6191 if (tree_int_cst_lt (upbound, t))
6193 tree v;
6195 for (v = TREE_OPERAND (expr, 0);
6196 TREE_CODE (v) == COMPONENT_REF;
6197 v = TREE_OPERAND (v, 0))
6198 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6199 == RECORD_TYPE)
6201 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6202 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6203 if (TREE_CODE (fld_chain) == FIELD_DECL)
6204 break;
6206 if (fld_chain)
6207 break;
6209 /* Don't warn if the array might be considered a poor
6210 man's flexible array member with a very permissive
6211 definition thereof. */
6212 if (TREE_CODE (v) == ARRAY_REF
6213 || TREE_CODE (v) == COMPONENT_REF)
6214 warning (OPT_Warray_bounds,
6215 "index %E denotes an offset "
6216 "greater than size of %qT",
6217 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6222 t = convert (sizetype, t);
6223 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6224 break;
6226 case COMPOUND_EXPR:
6227 /* Handle static members of volatile structs. */
6228 t = TREE_OPERAND (expr, 1);
6229 gcc_assert (VAR_P (t));
6230 return fold_offsetof_1 (t);
6232 default:
6233 gcc_unreachable ();
6236 return fold_build_pointer_plus (base, off);
6239 /* Likewise, but convert it to the return type of offsetof. */
6241 tree
6242 fold_offsetof (tree expr)
6244 return convert (size_type_node, fold_offsetof_1 (expr));
6248 /* *PTYPE is an incomplete array. Complete it with a domain based on
6249 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6250 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6251 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6254 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6256 tree maxindex, type, main_type, elt, unqual_elt;
6257 int failure = 0, quals;
6258 bool overflow_p = false;
6260 maxindex = size_zero_node;
6261 if (initial_value)
6263 if (TREE_CODE (initial_value) == STRING_CST)
6265 int eltsize
6266 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6267 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6269 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6271 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6273 if (vec_safe_is_empty (v))
6275 if (pedantic)
6276 failure = 3;
6277 maxindex = ssize_int (-1);
6279 else
6281 tree curindex;
6282 unsigned HOST_WIDE_INT cnt;
6283 constructor_elt *ce;
6284 bool fold_p = false;
6286 if ((*v)[0].index)
6287 maxindex = (*v)[0].index, fold_p = true;
6289 curindex = maxindex;
6291 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6293 bool curfold_p = false;
6294 if (ce->index)
6295 curindex = ce->index, curfold_p = true;
6296 else
6298 if (fold_p)
6300 /* Since we treat size types now as ordinary
6301 unsigned types, we need an explicit overflow
6302 check. */
6303 tree orig = curindex;
6304 curindex = fold_convert (sizetype, curindex);
6305 overflow_p |= tree_int_cst_lt (curindex, orig);
6307 curindex = size_binop (PLUS_EXPR, curindex,
6308 size_one_node);
6310 if (tree_int_cst_lt (maxindex, curindex))
6311 maxindex = curindex, fold_p = curfold_p;
6313 if (fold_p)
6315 tree orig = maxindex;
6316 maxindex = fold_convert (sizetype, maxindex);
6317 overflow_p |= tree_int_cst_lt (maxindex, orig);
6321 else
6323 /* Make an error message unless that happened already. */
6324 if (initial_value != error_mark_node)
6325 failure = 1;
6328 else
6330 failure = 2;
6331 if (!do_default)
6332 return failure;
6335 type = *ptype;
6336 elt = TREE_TYPE (type);
6337 quals = TYPE_QUALS (strip_array_types (elt));
6338 if (quals == 0)
6339 unqual_elt = elt;
6340 else
6341 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6343 /* Using build_distinct_type_copy and modifying things afterward instead
6344 of using build_array_type to create a new type preserves all of the
6345 TYPE_LANG_FLAG_? bits that the front end may have set. */
6346 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6347 TREE_TYPE (main_type) = unqual_elt;
6348 TYPE_DOMAIN (main_type)
6349 = build_range_type (TREE_TYPE (maxindex),
6350 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6351 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
6352 layout_type (main_type);
6354 /* Make sure we have the canonical MAIN_TYPE. */
6355 hashval_t hashcode = type_hash_canon_hash (main_type);
6356 main_type = type_hash_canon (hashcode, main_type);
6358 /* Fix the canonical type. */
6359 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6360 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6361 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6362 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6363 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6364 != TYPE_DOMAIN (main_type)))
6365 TYPE_CANONICAL (main_type)
6366 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6367 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6368 TYPE_TYPELESS_STORAGE (main_type));
6369 else
6370 TYPE_CANONICAL (main_type) = main_type;
6372 if (quals == 0)
6373 type = main_type;
6374 else
6375 type = c_build_qualified_type (main_type, quals);
6377 if (COMPLETE_TYPE_P (type)
6378 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6379 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6381 error ("size of array is too large");
6382 /* If we proceed with the array type as it is, we'll eventually
6383 crash in tree_to_[su]hwi(). */
6384 type = error_mark_node;
6387 *ptype = type;
6388 return failure;
6391 /* Like c_mark_addressable but don't check register qualifier. */
6392 void
6393 c_common_mark_addressable_vec (tree t)
6395 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6396 t = C_MAYBE_CONST_EXPR_EXPR (t);
6397 while (handled_component_p (t))
6398 t = TREE_OPERAND (t, 0);
6399 if (!VAR_P (t)
6400 && TREE_CODE (t) != PARM_DECL
6401 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6402 return;
6403 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6404 TREE_ADDRESSABLE (t) = 1;
6409 /* Used to help initialize the builtin-types.def table. When a type of
6410 the correct size doesn't exist, use error_mark_node instead of NULL.
6411 The later results in segfaults even when a decl using the type doesn't
6412 get invoked. */
6414 tree
6415 builtin_type_for_size (int size, bool unsignedp)
6417 tree type = c_common_type_for_size (size, unsignedp);
6418 return type ? type : error_mark_node;
6421 /* A helper function for resolve_overloaded_builtin in resolving the
6422 overloaded __sync_ builtins. Returns a positive power of 2 if the
6423 first operand of PARAMS is a pointer to a supported data type.
6424 Returns 0 if an error is encountered.
6425 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6426 built-ins. */
6428 static int
6429 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6431 /* Type of the argument. */
6432 tree argtype;
6433 /* Type the argument points to. */
6434 tree type;
6435 int size;
6437 if (vec_safe_is_empty (params))
6439 error ("too few arguments to function %qE", function);
6440 return 0;
6443 argtype = type = TREE_TYPE ((*params)[0]);
6444 if (TREE_CODE (type) == ARRAY_TYPE)
6446 /* Force array-to-pointer decay for C++. */
6447 gcc_assert (c_dialect_cxx());
6448 (*params)[0] = default_conversion ((*params)[0]);
6449 type = TREE_TYPE ((*params)[0]);
6451 if (TREE_CODE (type) != POINTER_TYPE)
6452 goto incompatible;
6454 type = TREE_TYPE (type);
6455 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6456 goto incompatible;
6458 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6459 goto incompatible;
6461 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6462 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6463 return size;
6465 incompatible:
6466 /* Issue the diagnostic only if the argument is valid, otherwise
6467 it would be redundant at best and could be misleading. */
6468 if (argtype != error_mark_node)
6469 error ("operand type %qT is incompatible with argument %d of %qE",
6470 argtype, 1, function);
6471 return 0;
6474 /* A helper function for resolve_overloaded_builtin. Adds casts to
6475 PARAMS to make arguments match up with those of FUNCTION. Drops
6476 the variadic arguments at the end. Returns false if some error
6477 was encountered; true on success. */
6479 static bool
6480 sync_resolve_params (location_t loc, tree orig_function, tree function,
6481 vec<tree, va_gc> *params, bool orig_format)
6483 function_args_iterator iter;
6484 tree ptype;
6485 unsigned int parmnum;
6487 function_args_iter_init (&iter, TREE_TYPE (function));
6488 /* We've declared the implementation functions to use "volatile void *"
6489 as the pointer parameter, so we shouldn't get any complaints from the
6490 call to check_function_arguments what ever type the user used. */
6491 function_args_iter_next (&iter);
6492 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6493 ptype = TYPE_MAIN_VARIANT (ptype);
6495 /* For the rest of the values, we need to cast these to FTYPE, so that we
6496 don't get warnings for passing pointer types, etc. */
6497 parmnum = 0;
6498 while (1)
6500 tree val, arg_type;
6502 arg_type = function_args_iter_cond (&iter);
6503 /* XXX void_type_node belies the abstraction. */
6504 if (arg_type == void_type_node)
6505 break;
6507 ++parmnum;
6508 if (params->length () <= parmnum)
6510 error_at (loc, "too few arguments to function %qE", orig_function);
6511 return false;
6514 /* Only convert parameters if arg_type is unsigned integer type with
6515 new format sync routines, i.e. don't attempt to convert pointer
6516 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6517 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6518 kinds). */
6519 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6521 /* Ideally for the first conversion we'd use convert_for_assignment
6522 so that we get warnings for anything that doesn't match the pointer
6523 type. This isn't portable across the C and C++ front ends atm. */
6524 val = (*params)[parmnum];
6525 val = convert (ptype, val);
6526 val = convert (arg_type, val);
6527 (*params)[parmnum] = val;
6530 function_args_iter_next (&iter);
6533 /* __atomic routines are not variadic. */
6534 if (!orig_format && params->length () != parmnum + 1)
6536 error_at (loc, "too many arguments to function %qE", orig_function);
6537 return false;
6540 /* The definition of these primitives is variadic, with the remaining
6541 being "an optional list of variables protected by the memory barrier".
6542 No clue what that's supposed to mean, precisely, but we consider all
6543 call-clobbered variables to be protected so we're safe. */
6544 params->truncate (parmnum + 1);
6546 return true;
6549 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6550 RESULT to make it match the type of the first pointer argument in
6551 PARAMS. */
6553 static tree
6554 sync_resolve_return (tree first_param, tree result, bool orig_format)
6556 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6557 tree rtype = TREE_TYPE (result);
6558 ptype = TYPE_MAIN_VARIANT (ptype);
6560 /* New format doesn't require casting unless the types are the same size. */
6561 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6562 return convert (ptype, result);
6563 else
6564 return result;
6567 /* This function verifies the PARAMS to generic atomic FUNCTION.
6568 It returns the size if all the parameters are the same size, otherwise
6569 0 is returned if the parameters are invalid. */
6571 static int
6572 get_atomic_generic_size (location_t loc, tree function,
6573 vec<tree, va_gc> *params)
6575 unsigned int n_param;
6576 unsigned int n_model;
6577 unsigned int x;
6578 int size_0;
6579 tree type_0;
6581 /* Determine the parameter makeup. */
6582 switch (DECL_FUNCTION_CODE (function))
6584 case BUILT_IN_ATOMIC_EXCHANGE:
6585 n_param = 4;
6586 n_model = 1;
6587 break;
6588 case BUILT_IN_ATOMIC_LOAD:
6589 case BUILT_IN_ATOMIC_STORE:
6590 n_param = 3;
6591 n_model = 1;
6592 break;
6593 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6594 n_param = 6;
6595 n_model = 2;
6596 break;
6597 default:
6598 gcc_unreachable ();
6601 if (vec_safe_length (params) != n_param)
6603 error_at (loc, "incorrect number of arguments to function %qE", function);
6604 return 0;
6607 /* Get type of first parameter, and determine its size. */
6608 type_0 = TREE_TYPE ((*params)[0]);
6609 if (TREE_CODE (type_0) == ARRAY_TYPE)
6611 /* Force array-to-pointer decay for C++. */
6612 gcc_assert (c_dialect_cxx());
6613 (*params)[0] = default_conversion ((*params)[0]);
6614 type_0 = TREE_TYPE ((*params)[0]);
6616 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6618 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6619 function);
6620 return 0;
6623 /* Types must be compile time constant sizes. */
6624 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6626 error_at (loc,
6627 "argument 1 of %qE must be a pointer to a constant size type",
6628 function);
6629 return 0;
6632 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6634 /* Zero size objects are not allowed. */
6635 if (size_0 == 0)
6637 error_at (loc,
6638 "argument 1 of %qE must be a pointer to a nonzero size object",
6639 function);
6640 return 0;
6643 /* Check each other parameter is a pointer and the same size. */
6644 for (x = 0; x < n_param - n_model; x++)
6646 int size;
6647 tree type = TREE_TYPE ((*params)[x]);
6648 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6649 if (n_param == 6 && x == 3)
6650 continue;
6651 if (!POINTER_TYPE_P (type))
6653 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6654 function);
6655 return 0;
6657 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6658 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6659 != INTEGER_CST)
6661 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6662 "size type", x + 1, function);
6663 return 0;
6665 else if (FUNCTION_POINTER_TYPE_P (type))
6667 error_at (loc, "argument %d of %qE must not be a pointer to a "
6668 "function", x + 1, function);
6669 return 0;
6671 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6672 size = type_size ? tree_to_uhwi (type_size) : 0;
6673 if (size != size_0)
6675 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6676 function);
6677 return 0;
6681 /* Check memory model parameters for validity. */
6682 for (x = n_param - n_model ; x < n_param; x++)
6684 tree p = (*params)[x];
6685 if (TREE_CODE (p) == INTEGER_CST)
6687 int i = tree_to_uhwi (p);
6688 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
6690 warning_at (loc, OPT_Winvalid_memory_model,
6691 "invalid memory model argument %d of %qE", x + 1,
6692 function);
6695 else
6696 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6698 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6699 function);
6700 return 0;
6704 return size_0;
6708 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6709 at the beginning of the parameter list PARAMS representing the size of the
6710 objects. This is to match the library ABI requirement. LOC is the location
6711 of the function call.
6712 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6713 returned to allow the external call to be constructed. */
6715 static tree
6716 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6717 vec<tree, va_gc> *params)
6719 tree size_node;
6721 /* Insert a SIZE_T parameter as the first param. If there isn't
6722 enough space, allocate a new vector and recursively re-build with that. */
6723 if (!params->space (1))
6725 unsigned int z, len;
6726 vec<tree, va_gc> *v;
6727 tree f;
6729 len = params->length ();
6730 vec_alloc (v, len + 1);
6731 v->quick_push (build_int_cst (size_type_node, n));
6732 for (z = 0; z < len; z++)
6733 v->quick_push ((*params)[z]);
6734 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6735 vec_free (v);
6736 return f;
6739 /* Add the size parameter and leave as a function call for processing. */
6740 size_node = build_int_cst (size_type_node, n);
6741 params->quick_insert (0, size_node);
6742 return NULL_TREE;
6746 /* Return whether atomic operations for naturally aligned N-byte
6747 arguments are supported, whether inline or through libatomic. */
6748 static bool
6749 atomic_size_supported_p (int n)
6751 switch (n)
6753 case 1:
6754 case 2:
6755 case 4:
6756 case 8:
6757 return true;
6759 case 16:
6760 return targetm.scalar_mode_supported_p (TImode);
6762 default:
6763 return false;
6767 /* This will process an __atomic_exchange function call, determine whether it
6768 needs to be mapped to the _N variation, or turned into a library call.
6769 LOC is the location of the builtin call.
6770 FUNCTION is the DECL that has been invoked;
6771 PARAMS is the argument list for the call. The return value is non-null
6772 TRUE is returned if it is translated into the proper format for a call to the
6773 external library, and NEW_RETURN is set the tree for that function.
6774 FALSE is returned if processing for the _N variation is required, and
6775 NEW_RETURN is set to the return value the result is copied into. */
6776 static bool
6777 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6778 vec<tree, va_gc> *params, tree *new_return)
6780 tree p0, p1, p2, p3;
6781 tree I_type, I_type_ptr;
6782 int n = get_atomic_generic_size (loc, function, params);
6784 /* Size of 0 is an error condition. */
6785 if (n == 0)
6787 *new_return = error_mark_node;
6788 return true;
6791 /* If not a lock-free size, change to the library generic format. */
6792 if (!atomic_size_supported_p (n))
6794 *new_return = add_atomic_size_parameter (n, loc, function, params);
6795 return true;
6798 /* Otherwise there is a lockfree match, transform the call from:
6799 void fn(T* mem, T* desired, T* return, model)
6800 into
6801 *return = (T) (fn (In* mem, (In) *desired, model)) */
6803 p0 = (*params)[0];
6804 p1 = (*params)[1];
6805 p2 = (*params)[2];
6806 p3 = (*params)[3];
6808 /* Create pointer to appropriate size. */
6809 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6810 I_type_ptr = build_pointer_type (I_type);
6812 /* Convert object pointer to required type. */
6813 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6814 (*params)[0] = p0;
6815 /* Convert new value to required type, and dereference it. */
6816 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6817 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6818 (*params)[1] = p1;
6820 /* Move memory model to the 3rd position, and end param list. */
6821 (*params)[2] = p3;
6822 params->truncate (3);
6824 /* Convert return pointer and dereference it for later assignment. */
6825 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6827 return false;
6831 /* This will process an __atomic_compare_exchange function call, determine
6832 whether it needs to be mapped to the _N variation, or turned into a lib call.
6833 LOC is the location of the builtin call.
6834 FUNCTION is the DECL that has been invoked;
6835 PARAMS is the argument list for the call. The return value is non-null
6836 TRUE is returned if it is translated into the proper format for a call to the
6837 external library, and NEW_RETURN is set the tree for that function.
6838 FALSE is returned if processing for the _N variation is required. */
6840 static bool
6841 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6842 vec<tree, va_gc> *params,
6843 tree *new_return)
6845 tree p0, p1, p2;
6846 tree I_type, I_type_ptr;
6847 int n = get_atomic_generic_size (loc, function, params);
6849 /* Size of 0 is an error condition. */
6850 if (n == 0)
6852 *new_return = error_mark_node;
6853 return true;
6856 /* If not a lock-free size, change to the library generic format. */
6857 if (!atomic_size_supported_p (n))
6859 /* The library generic format does not have the weak parameter, so
6860 remove it from the param list. Since a parameter has been removed,
6861 we can be sure that there is room for the SIZE_T parameter, meaning
6862 there will not be a recursive rebuilding of the parameter list, so
6863 there is no danger this will be done twice. */
6864 if (n > 0)
6866 (*params)[3] = (*params)[4];
6867 (*params)[4] = (*params)[5];
6868 params->truncate (5);
6870 *new_return = add_atomic_size_parameter (n, loc, function, params);
6871 return true;
6874 /* Otherwise, there is a match, so the call needs to be transformed from:
6875 bool fn(T* mem, T* desired, T* return, weak, success, failure)
6876 into
6877 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
6879 p0 = (*params)[0];
6880 p1 = (*params)[1];
6881 p2 = (*params)[2];
6883 /* Create pointer to appropriate size. */
6884 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6885 I_type_ptr = build_pointer_type (I_type);
6887 /* Convert object pointer to required type. */
6888 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6889 (*params)[0] = p0;
6891 /* Convert expected pointer to required type. */
6892 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
6893 (*params)[1] = p1;
6895 /* Convert desired value to required type, and dereference it. */
6896 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6897 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
6898 (*params)[2] = p2;
6900 /* The rest of the parameters are fine. NULL means no special return value
6901 processing.*/
6902 *new_return = NULL;
6903 return false;
6907 /* This will process an __atomic_load function call, determine whether it
6908 needs to be mapped to the _N variation, or turned into a library call.
6909 LOC is the location of the builtin call.
6910 FUNCTION is the DECL that has been invoked;
6911 PARAMS is the argument list for the call. The return value is non-null
6912 TRUE is returned if it is translated into the proper format for a call to the
6913 external library, and NEW_RETURN is set the tree for that function.
6914 FALSE is returned if processing for the _N variation is required, and
6915 NEW_RETURN is set to the return value the result is copied into. */
6917 static bool
6918 resolve_overloaded_atomic_load (location_t loc, tree function,
6919 vec<tree, va_gc> *params, tree *new_return)
6921 tree p0, p1, p2;
6922 tree I_type, I_type_ptr;
6923 int n = get_atomic_generic_size (loc, function, params);
6925 /* Size of 0 is an error condition. */
6926 if (n == 0)
6928 *new_return = error_mark_node;
6929 return true;
6932 /* If not a lock-free size, change to the library generic format. */
6933 if (!atomic_size_supported_p (n))
6935 *new_return = add_atomic_size_parameter (n, loc, function, params);
6936 return true;
6939 /* Otherwise, there is a match, so the call needs to be transformed from:
6940 void fn(T* mem, T* return, model)
6941 into
6942 *return = (T) (fn ((In *) mem, model)) */
6944 p0 = (*params)[0];
6945 p1 = (*params)[1];
6946 p2 = (*params)[2];
6948 /* Create pointer to appropriate size. */
6949 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6950 I_type_ptr = build_pointer_type (I_type);
6952 /* Convert object pointer to required type. */
6953 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6954 (*params)[0] = p0;
6956 /* Move memory model to the 2nd position, and end param list. */
6957 (*params)[1] = p2;
6958 params->truncate (2);
6960 /* Convert return pointer and dereference it for later assignment. */
6961 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6963 return false;
6967 /* This will process an __atomic_store function call, determine whether it
6968 needs to be mapped to the _N variation, or turned into a library call.
6969 LOC is the location of the builtin call.
6970 FUNCTION is the DECL that has been invoked;
6971 PARAMS is the argument list for the call. The return value is non-null
6972 TRUE is returned if it is translated into the proper format for a call to the
6973 external library, and NEW_RETURN is set the tree for that function.
6974 FALSE is returned if processing for the _N variation is required, and
6975 NEW_RETURN is set to the return value the result is copied into. */
6977 static bool
6978 resolve_overloaded_atomic_store (location_t loc, tree function,
6979 vec<tree, va_gc> *params, tree *new_return)
6981 tree p0, p1;
6982 tree I_type, I_type_ptr;
6983 int n = get_atomic_generic_size (loc, function, params);
6985 /* Size of 0 is an error condition. */
6986 if (n == 0)
6988 *new_return = error_mark_node;
6989 return true;
6992 /* If not a lock-free size, change to the library generic format. */
6993 if (!atomic_size_supported_p (n))
6995 *new_return = add_atomic_size_parameter (n, loc, function, params);
6996 return true;
6999 /* Otherwise, there is a match, so the call needs to be transformed from:
7000 void fn(T* mem, T* value, model)
7001 into
7002 fn ((In *) mem, (In) *value, model) */
7004 p0 = (*params)[0];
7005 p1 = (*params)[1];
7007 /* Create pointer to appropriate size. */
7008 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7009 I_type_ptr = build_pointer_type (I_type);
7011 /* Convert object pointer to required type. */
7012 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7013 (*params)[0] = p0;
7015 /* Convert new value to required type, and dereference it. */
7016 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7017 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7018 (*params)[1] = p1;
7020 /* The memory model is in the right spot already. Return is void. */
7021 *new_return = NULL_TREE;
7023 return false;
7027 /* Some builtin functions are placeholders for other expressions. This
7028 function should be called immediately after parsing the call expression
7029 before surrounding code has committed to the type of the expression.
7031 LOC is the location of the builtin call.
7033 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7034 PARAMS is the argument list for the call. The return value is non-null
7035 when expansion is complete, and null if normal processing should
7036 continue. */
7038 tree
7039 resolve_overloaded_builtin (location_t loc, tree function,
7040 vec<tree, va_gc> *params)
7042 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7044 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7045 Those are not valid to call with a pointer to _Bool (or C++ bool)
7046 and so must be rejected. */
7047 bool fetch_op = true;
7048 bool orig_format = true;
7049 tree new_return = NULL_TREE;
7051 switch (DECL_BUILT_IN_CLASS (function))
7053 case BUILT_IN_NORMAL:
7054 break;
7055 case BUILT_IN_MD:
7056 if (targetm.resolve_overloaded_builtin)
7057 return targetm.resolve_overloaded_builtin (loc, function, params);
7058 else
7059 return NULL_TREE;
7060 default:
7061 return NULL_TREE;
7064 /* Handle BUILT_IN_NORMAL here. */
7065 switch (orig_code)
7067 case BUILT_IN_ATOMIC_EXCHANGE:
7068 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7069 case BUILT_IN_ATOMIC_LOAD:
7070 case BUILT_IN_ATOMIC_STORE:
7072 /* Handle these 4 together so that they can fall through to the next
7073 case if the call is transformed to an _N variant. */
7074 switch (orig_code)
7076 case BUILT_IN_ATOMIC_EXCHANGE:
7078 if (resolve_overloaded_atomic_exchange (loc, function, params,
7079 &new_return))
7080 return new_return;
7081 /* Change to the _N variant. */
7082 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7083 break;
7086 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7088 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7089 params,
7090 &new_return))
7091 return new_return;
7092 /* Change to the _N variant. */
7093 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7094 break;
7096 case BUILT_IN_ATOMIC_LOAD:
7098 if (resolve_overloaded_atomic_load (loc, function, params,
7099 &new_return))
7100 return new_return;
7101 /* Change to the _N variant. */
7102 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7103 break;
7105 case BUILT_IN_ATOMIC_STORE:
7107 if (resolve_overloaded_atomic_store (loc, function, params,
7108 &new_return))
7109 return new_return;
7110 /* Change to the _N variant. */
7111 orig_code = BUILT_IN_ATOMIC_STORE_N;
7112 break;
7114 default:
7115 gcc_unreachable ();
7118 /* FALLTHRU */
7119 case BUILT_IN_ATOMIC_EXCHANGE_N:
7120 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7121 case BUILT_IN_ATOMIC_LOAD_N:
7122 case BUILT_IN_ATOMIC_STORE_N:
7123 fetch_op = false;
7124 /* FALLTHRU */
7125 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7126 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7127 case BUILT_IN_ATOMIC_AND_FETCH_N:
7128 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7129 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7130 case BUILT_IN_ATOMIC_OR_FETCH_N:
7131 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7132 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7133 case BUILT_IN_ATOMIC_FETCH_AND_N:
7134 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7135 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7136 case BUILT_IN_ATOMIC_FETCH_OR_N:
7137 orig_format = false;
7138 /* FALLTHRU */
7139 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7140 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7141 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7142 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7143 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7144 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7145 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7146 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7147 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7148 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7149 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7150 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7151 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7152 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7153 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7154 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7156 /* The following are not _FETCH_OPs and must be accepted with
7157 pointers to _Bool (or C++ bool). */
7158 if (fetch_op)
7159 fetch_op =
7160 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7161 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7162 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7163 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7165 int n = sync_resolve_size (function, params, fetch_op);
7166 tree new_function, first_param, result;
7167 enum built_in_function fncode;
7169 if (n == 0)
7170 return error_mark_node;
7172 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7173 new_function = builtin_decl_explicit (fncode);
7174 if (!sync_resolve_params (loc, function, new_function, params,
7175 orig_format))
7176 return error_mark_node;
7178 first_param = (*params)[0];
7179 result = build_function_call_vec (loc, vNULL, new_function, params,
7180 NULL);
7181 if (result == error_mark_node)
7182 return result;
7183 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7184 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7185 && orig_code != BUILT_IN_ATOMIC_STORE_N
7186 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7187 result = sync_resolve_return (first_param, result, orig_format);
7189 if (fetch_op)
7190 /* Prevent -Wunused-value warning. */
7191 TREE_USED (result) = true;
7193 /* If new_return is set, assign function to that expr and cast the
7194 result to void since the generic interface returned void. */
7195 if (new_return)
7197 /* Cast function result from I{1,2,4,8,16} to the required type. */
7198 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7199 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7200 result);
7201 TREE_SIDE_EFFECTS (result) = 1;
7202 protected_set_expr_location (result, loc);
7203 result = convert (void_type_node, result);
7205 return result;
7208 default:
7209 return NULL_TREE;
7213 /* vector_types_compatible_elements_p is used in type checks of vectors
7214 values used as operands of binary operators. Where it returns true, and
7215 the other checks of the caller succeed (being vector types in he first
7216 place, and matching number of elements), we can just treat the types
7217 as essentially the same.
7218 Contrast with vector_targets_convertible_p, which is used for vector
7219 pointer types, and vector_types_convertible_p, which will allow
7220 language-specific matches under the control of flag_lax_vector_conversions,
7221 and might still require a conversion. */
7222 /* True if vector types T1 and T2 can be inputs to the same binary
7223 operator without conversion.
7224 We don't check the overall vector size here because some of our callers
7225 want to give different error messages when the vectors are compatible
7226 except for the element count. */
7228 bool
7229 vector_types_compatible_elements_p (tree t1, tree t2)
7231 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7232 t1 = TREE_TYPE (t1);
7233 t2 = TREE_TYPE (t2);
7235 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7237 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7238 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7239 || c2 == FIXED_POINT_TYPE));
7241 t1 = c_common_signed_type (t1);
7242 t2 = c_common_signed_type (t2);
7243 /* Equality works here because c_common_signed_type uses
7244 TYPE_MAIN_VARIANT. */
7245 if (t1 == t2)
7246 return true;
7247 if (opaque && c1 == c2
7248 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7249 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7250 return true;
7251 return false;
7254 /* Check for missing format attributes on function pointers. LTYPE is
7255 the new type or left-hand side type. RTYPE is the old type or
7256 right-hand side type. Returns TRUE if LTYPE is missing the desired
7257 attribute. */
7259 bool
7260 check_missing_format_attribute (tree ltype, tree rtype)
7262 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7263 tree ra;
7265 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7266 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7267 break;
7268 if (ra)
7270 tree la;
7271 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7272 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7273 break;
7274 return !la;
7276 else
7277 return false;
7280 /* Setup a TYPE_DECL node as a typedef representation.
7282 X is a TYPE_DECL for a typedef statement. Create a brand new
7283 ..._TYPE node (which will be just a variant of the existing
7284 ..._TYPE node with identical properties) and then install X
7285 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7287 The whole point here is to end up with a situation where each
7288 and every ..._TYPE node the compiler creates will be uniquely
7289 associated with AT MOST one node representing a typedef name.
7290 This way, even though the compiler substitutes corresponding
7291 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7292 early on, later parts of the compiler can always do the reverse
7293 translation and get back the corresponding typedef name. For
7294 example, given:
7296 typedef struct S MY_TYPE;
7297 MY_TYPE object;
7299 Later parts of the compiler might only know that `object' was of
7300 type `struct S' if it were not for code just below. With this
7301 code however, later parts of the compiler see something like:
7303 struct S' == struct S
7304 typedef struct S' MY_TYPE;
7305 struct S' object;
7307 And they can then deduce (from the node for type struct S') that
7308 the original object declaration was:
7310 MY_TYPE object;
7312 Being able to do this is important for proper support of protoize,
7313 and also for generating precise symbolic debugging information
7314 which takes full account of the programmer's (typedef) vocabulary.
7316 Obviously, we don't want to generate a duplicate ..._TYPE node if
7317 the TYPE_DECL node that we are now processing really represents a
7318 standard built-in type. */
7320 void
7321 set_underlying_type (tree x)
7323 if (x == error_mark_node)
7324 return;
7325 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7327 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7328 TYPE_NAME (TREE_TYPE (x)) = x;
7330 else if (TREE_TYPE (x) != error_mark_node
7331 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7333 tree tt = TREE_TYPE (x);
7334 DECL_ORIGINAL_TYPE (x) = tt;
7335 tt = build_variant_type_copy (tt);
7336 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7337 TYPE_NAME (tt) = x;
7339 /* Mark the type as used only when its type decl is decorated
7340 with attribute unused. */
7341 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7342 TREE_USED (tt) = 1;
7344 TREE_TYPE (x) = tt;
7348 /* Record the types used by the current global variable declaration
7349 being parsed, so that we can decide later to emit their debug info.
7350 Those types are in types_used_by_cur_var_decl, and we are going to
7351 store them in the types_used_by_vars_hash hash table.
7352 DECL is the declaration of the global variable that has been parsed. */
7354 void
7355 record_types_used_by_current_var_decl (tree decl)
7357 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7359 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7361 tree type = types_used_by_cur_var_decl->pop ();
7362 types_used_by_var_decl_insert (type, decl);
7366 /* The C and C++ parsers both use vectors to hold function arguments.
7367 For efficiency, we keep a cache of unused vectors. This is the
7368 cache. */
7370 typedef vec<tree, va_gc> *tree_gc_vec;
7371 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7373 /* Return a new vector from the cache. If the cache is empty,
7374 allocate a new vector. These vectors are GC'ed, so it is OK if the
7375 pointer is not released.. */
7377 vec<tree, va_gc> *
7378 make_tree_vector (void)
7380 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7381 return tree_vector_cache->pop ();
7382 else
7384 /* Passing 0 to vec::alloc returns NULL, and our callers require
7385 that we always return a non-NULL value. The vector code uses
7386 4 when growing a NULL vector, so we do too. */
7387 vec<tree, va_gc> *v;
7388 vec_alloc (v, 4);
7389 return v;
7393 /* Release a vector of trees back to the cache. */
7395 void
7396 release_tree_vector (vec<tree, va_gc> *vec)
7398 if (vec != NULL)
7400 vec->truncate (0);
7401 vec_safe_push (tree_vector_cache, vec);
7405 /* Get a new tree vector holding a single tree. */
7407 vec<tree, va_gc> *
7408 make_tree_vector_single (tree t)
7410 vec<tree, va_gc> *ret = make_tree_vector ();
7411 ret->quick_push (t);
7412 return ret;
7415 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7417 vec<tree, va_gc> *
7418 make_tree_vector_from_list (tree list)
7420 vec<tree, va_gc> *ret = make_tree_vector ();
7421 for (; list; list = TREE_CHAIN (list))
7422 vec_safe_push (ret, TREE_VALUE (list));
7423 return ret;
7426 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7428 vec<tree, va_gc> *
7429 make_tree_vector_from_ctor (tree ctor)
7431 vec<tree,va_gc> *ret = make_tree_vector ();
7432 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7433 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7434 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7435 return ret;
7438 /* Get a new tree vector which is a copy of an existing one. */
7440 vec<tree, va_gc> *
7441 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7443 vec<tree, va_gc> *ret;
7444 unsigned int ix;
7445 tree t;
7447 ret = make_tree_vector ();
7448 vec_safe_reserve (ret, vec_safe_length (orig));
7449 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7450 ret->quick_push (t);
7451 return ret;
7454 /* Return true if KEYWORD starts a type specifier. */
7456 bool
7457 keyword_begins_type_specifier (enum rid keyword)
7459 switch (keyword)
7461 case RID_AUTO_TYPE:
7462 case RID_INT:
7463 case RID_CHAR:
7464 case RID_FLOAT:
7465 case RID_DOUBLE:
7466 case RID_VOID:
7467 case RID_UNSIGNED:
7468 case RID_LONG:
7469 case RID_SHORT:
7470 case RID_SIGNED:
7471 CASE_RID_FLOATN_NX:
7472 case RID_DFLOAT32:
7473 case RID_DFLOAT64:
7474 case RID_DFLOAT128:
7475 case RID_FRACT:
7476 case RID_ACCUM:
7477 case RID_BOOL:
7478 case RID_WCHAR:
7479 case RID_CHAR16:
7480 case RID_CHAR32:
7481 case RID_SAT:
7482 case RID_COMPLEX:
7483 case RID_TYPEOF:
7484 case RID_STRUCT:
7485 case RID_CLASS:
7486 case RID_UNION:
7487 case RID_ENUM:
7488 return true;
7489 default:
7490 if (keyword >= RID_FIRST_INT_N
7491 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7492 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7493 return true;
7494 return false;
7498 /* Return true if KEYWORD names a type qualifier. */
7500 bool
7501 keyword_is_type_qualifier (enum rid keyword)
7503 switch (keyword)
7505 case RID_CONST:
7506 case RID_VOLATILE:
7507 case RID_RESTRICT:
7508 case RID_ATOMIC:
7509 return true;
7510 default:
7511 return false;
7515 /* Return true if KEYWORD names a storage class specifier.
7517 RID_TYPEDEF is not included in this list despite `typedef' being
7518 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7519 such for syntactic convenience only. */
7521 bool
7522 keyword_is_storage_class_specifier (enum rid keyword)
7524 switch (keyword)
7526 case RID_STATIC:
7527 case RID_EXTERN:
7528 case RID_REGISTER:
7529 case RID_AUTO:
7530 case RID_MUTABLE:
7531 case RID_THREAD:
7532 return true;
7533 default:
7534 return false;
7538 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7540 static bool
7541 keyword_is_function_specifier (enum rid keyword)
7543 switch (keyword)
7545 case RID_INLINE:
7546 case RID_NORETURN:
7547 case RID_VIRTUAL:
7548 case RID_EXPLICIT:
7549 return true;
7550 default:
7551 return false;
7555 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7556 declaration-specifier (C99 6.7). */
7558 bool
7559 keyword_is_decl_specifier (enum rid keyword)
7561 if (keyword_is_storage_class_specifier (keyword)
7562 || keyword_is_type_qualifier (keyword)
7563 || keyword_is_function_specifier (keyword))
7564 return true;
7566 switch (keyword)
7568 case RID_TYPEDEF:
7569 case RID_FRIEND:
7570 case RID_CONSTEXPR:
7571 return true;
7572 default:
7573 return false;
7577 /* Initialize language-specific-bits of tree_contains_struct. */
7579 void
7580 c_common_init_ts (void)
7582 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7583 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7584 MARK_TS_TYPED (ARRAY_NOTATION_REF);
7587 /* Build a user-defined numeric literal out of an integer constant type VALUE
7588 with identifier SUFFIX. */
7590 tree
7591 build_userdef_literal (tree suffix_id, tree value,
7592 enum overflow_type overflow, tree num_string)
7594 tree literal = make_node (USERDEF_LITERAL);
7595 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7596 USERDEF_LITERAL_VALUE (literal) = value;
7597 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7598 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7599 return literal;
7602 /* For vector[index], convert the vector to an array of the underlying type.
7603 Return true if the resulting ARRAY_REF should not be an lvalue. */
7605 bool
7606 convert_vector_to_array_for_subscript (location_t loc,
7607 tree *vecp, tree index)
7609 bool ret = false;
7610 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7612 tree type = TREE_TYPE (*vecp);
7614 ret = !lvalue_p (*vecp);
7616 if (TREE_CODE (index) == INTEGER_CST)
7617 if (!tree_fits_uhwi_p (index)
7618 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7619 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7621 /* We are building an ARRAY_REF so mark the vector as addressable
7622 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7623 for function parameters. */
7624 c_common_mark_addressable_vec (*vecp);
7626 *vecp = build1 (VIEW_CONVERT_EXPR,
7627 build_array_type_nelts (TREE_TYPE (type),
7628 TYPE_VECTOR_SUBPARTS (type)),
7629 *vecp);
7631 return ret;
7634 /* Determine which of the operands, if any, is a scalar that needs to be
7635 converted to a vector, for the range of operations. */
7636 enum stv_conv
7637 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7638 bool complain)
7640 tree type0 = TREE_TYPE (op0);
7641 tree type1 = TREE_TYPE (op1);
7642 bool integer_only_op = false;
7643 enum stv_conv ret = stv_firstarg;
7645 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7646 switch (code)
7648 /* Most GENERIC binary expressions require homogeneous arguments.
7649 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7650 argument that is a vector and a second one that is a scalar, so
7651 we never return stv_secondarg for them. */
7652 case RSHIFT_EXPR:
7653 case LSHIFT_EXPR:
7654 if (TREE_CODE (type0) == INTEGER_TYPE
7655 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7657 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7659 if (complain)
7660 error_at (loc, "conversion of scalar %qT to vector %qT "
7661 "involves truncation", type0, type1);
7662 return stv_error;
7664 else
7665 return stv_firstarg;
7667 break;
7669 case BIT_IOR_EXPR:
7670 case BIT_XOR_EXPR:
7671 case BIT_AND_EXPR:
7672 integer_only_op = true;
7673 /* fall through */
7675 case VEC_COND_EXPR:
7677 case PLUS_EXPR:
7678 case MINUS_EXPR:
7679 case MULT_EXPR:
7680 case TRUNC_DIV_EXPR:
7681 case CEIL_DIV_EXPR:
7682 case FLOOR_DIV_EXPR:
7683 case ROUND_DIV_EXPR:
7684 case EXACT_DIV_EXPR:
7685 case TRUNC_MOD_EXPR:
7686 case FLOOR_MOD_EXPR:
7687 case RDIV_EXPR:
7688 case EQ_EXPR:
7689 case NE_EXPR:
7690 case LE_EXPR:
7691 case GE_EXPR:
7692 case LT_EXPR:
7693 case GT_EXPR:
7694 /* What about UNLT_EXPR? */
7695 if (VECTOR_TYPE_P (type0))
7697 ret = stv_secondarg;
7698 std::swap (type0, type1);
7699 std::swap (op0, op1);
7702 if (TREE_CODE (type0) == INTEGER_TYPE
7703 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7705 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7707 if (complain)
7708 error_at (loc, "conversion of scalar %qT to vector %qT "
7709 "involves truncation", type0, type1);
7710 return stv_error;
7712 return ret;
7714 else if (!integer_only_op
7715 /* Allow integer --> real conversion if safe. */
7716 && (TREE_CODE (type0) == REAL_TYPE
7717 || TREE_CODE (type0) == INTEGER_TYPE)
7718 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7720 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7722 if (complain)
7723 error_at (loc, "conversion of scalar %qT to vector %qT "
7724 "involves truncation", type0, type1);
7725 return stv_error;
7727 return ret;
7729 default:
7730 break;
7733 return stv_nothing;
7736 /* Return the alignment of std::max_align_t.
7738 [support.types.layout] The type max_align_t is a POD type whose alignment
7739 requirement is at least as great as that of every scalar type, and whose
7740 alignment requirement is supported in every context. */
7742 unsigned
7743 max_align_t_align ()
7745 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7746 TYPE_ALIGN (long_double_type_node));
7747 if (float128_type_node != NULL_TREE)
7748 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7749 return max_align;
7752 /* Return true iff ALIGN is an integral constant that is a fundamental
7753 alignment, as defined by [basic.align] in the c++-11
7754 specifications.
7756 That is:
7758 [A fundamental alignment is represented by an alignment less than or
7759 equal to the greatest alignment supported by the implementation
7760 in all contexts, which is equal to alignof(max_align_t)]. */
7762 bool
7763 cxx_fundamental_alignment_p (unsigned align)
7765 return (align <= max_align_t_align ());
7768 /* Return true if T is a pointer to a zero-sized aggregate. */
7770 bool
7771 pointer_to_zero_sized_aggr_p (tree t)
7773 if (!POINTER_TYPE_P (t))
7774 return false;
7775 t = TREE_TYPE (t);
7776 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7779 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7780 with no library fallback or for an ADDR_EXPR whose operand is such type
7781 issues an error pointing to the location LOC.
7782 Returns true when the expression has been diagnosed and false
7783 otherwise. */
7785 bool
7786 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7788 if (TREE_CODE (expr) == ADDR_EXPR)
7789 expr = TREE_OPERAND (expr, 0);
7791 if (TREE_TYPE (expr)
7792 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7793 && TREE_CODE (expr) == FUNCTION_DECL
7794 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7795 false positives for user-declared built-ins such as abs or
7796 strlen, and for C++ operators new and delete.
7797 The c_decl_implicit() test avoids false positives for implicitly
7798 declared built-ins with library fallbacks (such as abs). */
7799 && DECL_BUILT_IN (expr)
7800 && DECL_IS_BUILTIN (expr)
7801 && !c_decl_implicit (expr)
7802 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7804 if (loc == UNKNOWN_LOCATION)
7805 loc = EXPR_LOC_OR_LOC (expr, input_location);
7807 /* Reject arguments that are built-in functions with
7808 no library fallback. */
7809 error_at (loc, "built-in function %qE must be directly called", expr);
7811 return true;
7814 return false;
7817 /* Check if array size calculations overflow or if the array covers more
7818 than half of the address space. Return true if the size of the array
7819 is valid, false otherwise. TYPE is the type of the array and NAME is
7820 the name of the array, or NULL_TREE for unnamed arrays. */
7822 bool
7823 valid_array_size_p (location_t loc, tree type, tree name)
7825 if (type != error_mark_node
7826 && COMPLETE_TYPE_P (type)
7827 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7828 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7830 if (name)
7831 error_at (loc, "size of array %qE is too large", name);
7832 else
7833 error_at (loc, "size of unnamed array is too large");
7834 return false;
7836 return true;
7839 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7840 timestamp to replace embedded current dates to get reproducible
7841 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
7843 time_t
7844 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7846 char *source_date_epoch;
7847 int64_t epoch;
7848 char *endptr;
7850 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7851 if (!source_date_epoch)
7852 return (time_t) -1;
7854 errno = 0;
7855 #if defined(INT64_T_IS_LONG)
7856 epoch = strtol (source_date_epoch, &endptr, 10);
7857 #else
7858 epoch = strtoll (source_date_epoch, &endptr, 10);
7859 #endif
7860 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7861 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7863 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
7864 "expand to a non-negative integer less than or equal to %wd",
7865 MAX_SOURCE_DATE_EPOCH);
7866 return (time_t) -1;
7869 return (time_t) epoch;
7872 /* Callback for libcpp for offering spelling suggestions for misspelled
7873 directives. GOAL is an unrecognized string; CANDIDATES is a
7874 NULL-terminated array of candidate strings. Return the closest
7875 match to GOAL within CANDIDATES, or NULL if none are good
7876 suggestions. */
7878 const char *
7879 cb_get_suggestion (cpp_reader *, const char *goal,
7880 const char *const *candidates)
7882 best_match<const char *, const char *> bm (goal);
7883 while (*candidates)
7884 bm.consider (*candidates++);
7885 return bm.get_best_meaningful_candidate ();
7888 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
7889 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
7890 by C TS 18661-3 for interchange types that are computed in their
7891 native precision are larger than the C11 values for evaluating in the
7892 precision of float/double/long double. If either mode is
7893 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
7895 enum flt_eval_method
7896 excess_precision_mode_join (enum flt_eval_method x,
7897 enum flt_eval_method y)
7899 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
7900 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
7901 return FLT_EVAL_METHOD_UNPREDICTABLE;
7903 /* GCC only supports one interchange type right now, _Float16. If
7904 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
7905 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
7906 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7907 return y;
7908 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7909 return x;
7911 /* Other values for flt_eval_method are directly comparable, and we want
7912 the maximum. */
7913 return MAX (x, y);
7916 /* Return the value that should be set for FLT_EVAL_METHOD in the
7917 context of ISO/IEC TS 18861-3.
7919 This relates to the effective excess precision seen by the user,
7920 which is the join point of the precision the target requests for
7921 -fexcess-precision={standard,fast} and the implicit excess precision
7922 the target uses. */
7924 static enum flt_eval_method
7925 c_ts18661_flt_eval_method (void)
7927 enum flt_eval_method implicit
7928 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
7930 enum excess_precision_type flag_type
7931 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
7932 ? EXCESS_PRECISION_TYPE_STANDARD
7933 : EXCESS_PRECISION_TYPE_FAST);
7935 enum flt_eval_method requested
7936 = targetm.c.excess_precision (flag_type);
7938 return excess_precision_mode_join (implicit, requested);
7941 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
7942 those that were permitted by C11. That is to say, eliminates
7943 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
7945 static enum flt_eval_method
7946 c_c11_flt_eval_method (void)
7948 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
7949 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
7952 /* Return the value that should be set for FLT_EVAL_METHOD.
7953 MAYBE_C11_ONLY_P is TRUE if we should check
7954 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
7955 values we can return to those from C99/C11, and FALSE otherwise.
7956 See the comments on c_ts18661_flt_eval_method for what value we choose
7957 to set here. */
7960 c_flt_eval_method (bool maybe_c11_only_p)
7962 if (maybe_c11_only_p
7963 && flag_permitted_flt_eval_methods
7964 == PERMITTED_FLT_EVAL_METHODS_C11)
7965 return c_c11_flt_eval_method ();
7966 else
7967 return c_ts18661_flt_eval_method ();
7970 #include "gt-c-family-c-common.h"