Add "__RTL" to cc1
[official-gcc.git] / gcc / c-family / c-common.c
blob62b762bd388b9548acd642e73622213f4e0de265
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_base_of", RID_IS_BASE_OF, D_CXXONLY },
407 { "__is_class", RID_IS_CLASS, D_CXXONLY },
408 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
409 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
410 { "__is_final", RID_IS_FINAL, D_CXXONLY },
411 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
412 { "__is_pod", RID_IS_POD, D_CXXONLY },
413 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
414 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
415 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
416 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
417 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
418 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
419 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
420 { "__is_union", RID_IS_UNION, D_CXXONLY },
421 { "__label__", RID_LABEL, 0 },
422 { "__null", RID_NULL, 0 },
423 { "__real", RID_REALPART, 0 },
424 { "__real__", RID_REALPART, 0 },
425 { "__restrict", RID_RESTRICT, 0 },
426 { "__restrict__", RID_RESTRICT, 0 },
427 { "__signed", RID_SIGNED, 0 },
428 { "__signed__", RID_SIGNED, 0 },
429 { "__thread", RID_THREAD, 0 },
430 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
431 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
432 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
433 { "__typeof", RID_TYPEOF, 0 },
434 { "__typeof__", RID_TYPEOF, 0 },
435 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
436 { "__volatile", RID_VOLATILE, 0 },
437 { "__volatile__", RID_VOLATILE, 0 },
438 { "__GIMPLE", RID_GIMPLE, D_CONLY },
439 { "__PHI", RID_PHI, D_CONLY },
440 { "__RTL", RID_RTL, D_CONLY },
441 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
442 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
443 { "asm", RID_ASM, D_ASM },
444 { "auto", RID_AUTO, 0 },
445 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
446 { "break", RID_BREAK, 0 },
447 { "case", RID_CASE, 0 },
448 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
449 { "char", RID_CHAR, 0 },
450 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
451 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
452 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
453 { "const", RID_CONST, 0 },
454 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
455 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
456 { "continue", RID_CONTINUE, 0 },
457 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
458 { "default", RID_DEFAULT, 0 },
459 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
460 { "do", RID_DO, 0 },
461 { "double", RID_DOUBLE, 0 },
462 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
463 { "else", RID_ELSE, 0 },
464 { "enum", RID_ENUM, 0 },
465 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
466 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
467 { "extern", RID_EXTERN, 0 },
468 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
469 { "float", RID_FLOAT, 0 },
470 { "for", RID_FOR, 0 },
471 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
472 { "goto", RID_GOTO, 0 },
473 { "if", RID_IF, 0 },
474 { "inline", RID_INLINE, D_EXT89 },
475 { "int", RID_INT, 0 },
476 { "long", RID_LONG, 0 },
477 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
478 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
479 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
480 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
481 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
482 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
483 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
484 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
485 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
486 { "register", RID_REGISTER, 0 },
487 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
488 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
489 { "return", RID_RETURN, 0 },
490 { "short", RID_SHORT, 0 },
491 { "signed", RID_SIGNED, 0 },
492 { "sizeof", RID_SIZEOF, 0 },
493 { "static", RID_STATIC, 0 },
494 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
495 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
496 { "struct", RID_STRUCT, 0 },
497 { "switch", RID_SWITCH, 0 },
498 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
499 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
500 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
501 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
502 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
503 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
504 { "typedef", RID_TYPEDEF, 0 },
505 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
506 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
507 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
508 { "union", RID_UNION, 0 },
509 { "unsigned", RID_UNSIGNED, 0 },
510 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
511 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
512 { "void", RID_VOID, 0 },
513 { "volatile", RID_VOLATILE, 0 },
514 { "wchar_t", RID_WCHAR, D_CXXONLY },
515 { "while", RID_WHILE, 0 },
517 /* C++ transactional memory. */
518 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
519 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
520 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
521 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
523 /* Concepts-related keywords */
524 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
525 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
527 /* These Objective-C keywords are recognized only immediately after
528 an '@'. */
529 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
530 { "defs", RID_AT_DEFS, D_OBJC },
531 { "encode", RID_AT_ENCODE, D_OBJC },
532 { "end", RID_AT_END, D_OBJC },
533 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
534 { "interface", RID_AT_INTERFACE, D_OBJC },
535 { "protocol", RID_AT_PROTOCOL, D_OBJC },
536 { "selector", RID_AT_SELECTOR, D_OBJC },
537 { "finally", RID_AT_FINALLY, D_OBJC },
538 { "optional", RID_AT_OPTIONAL, D_OBJC },
539 { "required", RID_AT_REQUIRED, D_OBJC },
540 { "property", RID_AT_PROPERTY, D_OBJC },
541 { "package", RID_AT_PACKAGE, D_OBJC },
542 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
543 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
544 /* These are recognized only in protocol-qualifier context
545 (see above) */
546 { "bycopy", RID_BYCOPY, D_OBJC },
547 { "byref", RID_BYREF, D_OBJC },
548 { "in", RID_IN, D_OBJC },
549 { "inout", RID_INOUT, D_OBJC },
550 { "oneway", RID_ONEWAY, D_OBJC },
551 { "out", RID_OUT, D_OBJC },
552 /* These are recognized inside a property attribute list */
553 { "assign", RID_ASSIGN, D_OBJC },
554 { "copy", RID_COPY, D_OBJC },
555 { "getter", RID_GETTER, D_OBJC },
556 { "nonatomic", RID_NONATOMIC, D_OBJC },
557 { "readonly", RID_READONLY, D_OBJC },
558 { "readwrite", RID_READWRITE, D_OBJC },
559 { "retain", RID_RETAIN, D_OBJC },
560 { "setter", RID_SETTER, D_OBJC },
563 const unsigned int num_c_common_reswords =
564 sizeof c_common_reswords / sizeof (struct c_common_resword);
566 /* Return identifier for address space AS. */
568 const char *
569 c_addr_space_name (addr_space_t as)
571 int rid = RID_FIRST_ADDR_SPACE + as;
572 gcc_assert (ridpointers [rid]);
573 return IDENTIFIER_POINTER (ridpointers [rid]);
576 /* Push current bindings for the function name VAR_DECLS. */
578 void
579 start_fname_decls (void)
581 unsigned ix;
582 tree saved = NULL_TREE;
584 for (ix = 0; fname_vars[ix].decl; ix++)
586 tree decl = *fname_vars[ix].decl;
588 if (decl)
590 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
591 saved);
592 *fname_vars[ix].decl = NULL_TREE;
595 if (saved || saved_function_name_decls)
596 /* Normally they'll have been NULL, so only push if we've got a
597 stack, or they are non-NULL. */
598 saved_function_name_decls = tree_cons (saved, NULL_TREE,
599 saved_function_name_decls);
602 /* Finish up the current bindings, adding them into the current function's
603 statement tree. This must be done _before_ finish_stmt_tree is called.
604 If there is no current function, we must be at file scope and no statements
605 are involved. Pop the previous bindings. */
607 void
608 finish_fname_decls (void)
610 unsigned ix;
611 tree stmts = NULL_TREE;
612 tree stack = saved_function_name_decls;
614 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
615 append_to_statement_list (TREE_VALUE (stack), &stmts);
617 if (stmts)
619 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
621 if (TREE_CODE (*bodyp) == BIND_EXPR)
622 bodyp = &BIND_EXPR_BODY (*bodyp);
624 append_to_statement_list_force (*bodyp, &stmts);
625 *bodyp = stmts;
628 for (ix = 0; fname_vars[ix].decl; ix++)
629 *fname_vars[ix].decl = NULL_TREE;
631 if (stack)
633 /* We had saved values, restore them. */
634 tree saved;
636 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
638 tree decl = TREE_PURPOSE (saved);
639 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
641 *fname_vars[ix].decl = decl;
643 stack = TREE_CHAIN (stack);
645 saved_function_name_decls = stack;
648 /* Return the text name of the current function, suitably prettified
649 by PRETTY_P. Return string must be freed by caller. */
651 const char *
652 fname_as_string (int pretty_p)
654 const char *name = "top level";
655 char *namep;
656 int vrb = 2, len;
657 cpp_string cstr = { 0, 0 }, strname;
659 if (!pretty_p)
661 name = "";
662 vrb = 0;
665 if (current_function_decl)
666 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
668 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
670 namep = XNEWVEC (char, len);
671 snprintf (namep, len, "\"%s\"", name);
672 strname.text = (unsigned char *) namep;
673 strname.len = len - 1;
675 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
677 XDELETEVEC (namep);
678 return (const char *) cstr.text;
681 return namep;
684 /* Return the VAR_DECL for a const char array naming the current
685 function. If the VAR_DECL has not yet been created, create it
686 now. RID indicates how it should be formatted and IDENTIFIER_NODE
687 ID is its name (unfortunately C and C++ hold the RID values of
688 keywords in different places, so we can't derive RID from ID in
689 this language independent code. LOC is the location of the
690 function. */
692 tree
693 fname_decl (location_t loc, unsigned int rid, tree id)
695 unsigned ix;
696 tree decl = NULL_TREE;
698 for (ix = 0; fname_vars[ix].decl; ix++)
699 if (fname_vars[ix].rid == rid)
700 break;
702 decl = *fname_vars[ix].decl;
703 if (!decl)
705 /* If a tree is built here, it would normally have the lineno of
706 the current statement. Later this tree will be moved to the
707 beginning of the function and this line number will be wrong.
708 To avoid this problem set the lineno to 0 here; that prevents
709 it from appearing in the RTL. */
710 tree stmts;
711 location_t saved_location = input_location;
712 input_location = UNKNOWN_LOCATION;
714 stmts = push_stmt_list ();
715 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
716 stmts = pop_stmt_list (stmts);
717 if (!IS_EMPTY_STMT (stmts))
718 saved_function_name_decls
719 = tree_cons (decl, stmts, saved_function_name_decls);
720 *fname_vars[ix].decl = decl;
721 input_location = saved_location;
723 if (!ix && !current_function_decl)
724 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
726 return decl;
729 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
731 tree
732 fix_string_type (tree value)
734 int length = TREE_STRING_LENGTH (value);
735 int nchars;
736 tree e_type, i_type, a_type;
738 /* Compute the number of elements, for the array type. */
739 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
741 nchars = length;
742 e_type = char_type_node;
744 else if (TREE_TYPE (value) == char16_array_type_node)
746 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
747 e_type = char16_type_node;
749 else if (TREE_TYPE (value) == char32_array_type_node)
751 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
752 e_type = char32_type_node;
754 else
756 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
757 e_type = wchar_type_node;
760 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
761 limit in C++98 Annex B is very large (65536) and is not normative,
762 so we do not diagnose it (warn_overlength_strings is forced off
763 in c_common_post_options). */
764 if (warn_overlength_strings)
766 const int nchars_max = flag_isoc99 ? 4095 : 509;
767 const int relevant_std = flag_isoc99 ? 99 : 90;
768 if (nchars - 1 > nchars_max)
769 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
770 separate the %d from the 'C'. 'ISO' should not be
771 translated, but it may be moved after 'C%d' in languages
772 where modifiers follow nouns. */
773 pedwarn (input_location, OPT_Woverlength_strings,
774 "string length %qd is greater than the length %qd "
775 "ISO C%d compilers are required to support",
776 nchars - 1, nchars_max, relevant_std);
779 /* Create the array type for the string constant. The ISO C++
780 standard says that a string literal has type `const char[N]' or
781 `const wchar_t[N]'. We use the same logic when invoked as a C
782 front-end with -Wwrite-strings.
783 ??? We should change the type of an expression depending on the
784 state of a warning flag. We should just be warning -- see how
785 this is handled in the C++ front-end for the deprecated implicit
786 conversion from string literals to `char*' or `wchar_t*'.
788 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
789 array type being the unqualified version of that type.
790 Therefore, if we are constructing an array of const char, we must
791 construct the matching unqualified array type first. The C front
792 end does not require this, but it does no harm, so we do it
793 unconditionally. */
794 i_type = build_index_type (size_int (nchars - 1));
795 a_type = build_array_type (e_type, i_type);
796 if (c_dialect_cxx() || warn_write_strings)
797 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
799 TREE_TYPE (value) = a_type;
800 TREE_CONSTANT (value) = 1;
801 TREE_READONLY (value) = 1;
802 TREE_STATIC (value) = 1;
803 return value;
806 /* Given a string of type STRING_TYPE, determine what kind of string
807 token would give an equivalent execution encoding: CPP_STRING,
808 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
809 This may not be exactly the string token type that initially created
810 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
811 string type at this point.
813 This effectively reverses part of the logic in lex_string and
814 fix_string_type. */
816 static enum cpp_ttype
817 get_cpp_ttype_from_string_type (tree string_type)
819 gcc_assert (string_type);
820 if (TREE_CODE (string_type) == POINTER_TYPE)
821 string_type = TREE_TYPE (string_type);
823 if (TREE_CODE (string_type) != ARRAY_TYPE)
824 return CPP_OTHER;
826 tree element_type = TREE_TYPE (string_type);
827 if (TREE_CODE (element_type) != INTEGER_TYPE)
828 return CPP_OTHER;
830 int bits_per_character = TYPE_PRECISION (element_type);
831 switch (bits_per_character)
833 case 8:
834 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
835 case 16:
836 return CPP_STRING16;
837 case 32:
838 return CPP_STRING32;
841 return CPP_OTHER;
844 /* The global record of string concatentations, for use in
845 extracting locations within string literals. */
847 GTY(()) string_concat_db *g_string_concat_db;
849 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
851 const char *
852 c_get_substring_location (const substring_loc &substr_loc,
853 location_t *out_loc)
855 enum cpp_ttype tok_type
856 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
857 if (tok_type == CPP_OTHER)
858 return "unrecognized string type";
860 return get_source_location_for_substring (parse_in, g_string_concat_db,
861 substr_loc.get_fmt_string_loc (),
862 tok_type,
863 substr_loc.get_caret_idx (),
864 substr_loc.get_start_idx (),
865 substr_loc.get_end_idx (),
866 out_loc);
870 /* Fold X for consideration by one of the warning functions when checking
871 whether an expression has a constant value. */
873 tree
874 fold_for_warn (tree x)
876 if (c_dialect_cxx ())
877 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
878 else
879 /* The C front-end has already folded X appropriately. */
880 return x;
883 /* Return true iff T is a boolean promoted to int. */
885 bool
886 bool_promoted_to_int_p (tree t)
888 return (CONVERT_EXPR_P (t)
889 && TREE_TYPE (t) == integer_type_node
890 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
893 /* vector_targets_convertible_p is used for vector pointer types. The
894 callers perform various checks that the qualifiers are satisfactory,
895 while OTOH vector_targets_convertible_p ignores the number of elements
896 in the vectors. That's fine with vector pointers as we can consider,
897 say, a vector of 8 elements as two consecutive vectors of 4 elements,
898 and that does not require and conversion of the pointer values.
899 In contrast, vector_types_convertible_p and
900 vector_types_compatible_elements_p are used for vector value types. */
901 /* True if pointers to distinct types T1 and T2 can be converted to
902 each other without an explicit cast. Only returns true for opaque
903 vector types. */
904 bool
905 vector_targets_convertible_p (const_tree t1, const_tree t2)
907 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
908 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
909 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
910 return true;
912 return false;
915 /* vector_types_convertible_p is used for vector value types.
916 It could in principle call vector_targets_convertible_p as a subroutine,
917 but then the check for vector type would be duplicated with its callers,
918 and also the purpose of vector_targets_convertible_p would become
919 muddled.
920 Where vector_types_convertible_p returns true, a conversion might still be
921 needed to make the types match.
922 In contrast, vector_targets_convertible_p is used for vector pointer
923 values, and vector_types_compatible_elements_p is used specifically
924 in the context for binary operators, as a check if use is possible without
925 conversion. */
926 /* True if vector types T1 and T2 can be converted to each other
927 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
928 can only be converted with -flax-vector-conversions yet that is not
929 in effect, emit a note telling the user about that option if such
930 a note has not previously been emitted. */
931 bool
932 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
934 static bool emitted_lax_note = false;
935 bool convertible_lax;
937 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
938 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
939 return true;
941 convertible_lax =
942 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
943 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
944 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
945 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
946 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
948 if (!convertible_lax || flag_lax_vector_conversions)
949 return convertible_lax;
951 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
952 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
953 return true;
955 if (emit_lax_note && !emitted_lax_note)
957 emitted_lax_note = true;
958 inform (input_location, "use -flax-vector-conversions to permit "
959 "conversions between vectors with differing "
960 "element types or numbers of subparts");
963 return false;
966 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
967 and have vector types, V0 has the same type as V1, and the number of
968 elements of V0, V1, MASK is the same.
970 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
971 called with two arguments. In this case implementation passes the
972 first argument twice in order to share the same tree code. This fact
973 could enable the mask-values being twice the vector length. This is
974 an implementation accident and this semantics is not guaranteed to
975 the user. */
976 tree
977 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
978 bool complain)
980 tree ret;
981 bool wrap = true;
982 bool maybe_const = false;
983 bool two_arguments = false;
985 if (v1 == NULL_TREE)
987 two_arguments = true;
988 v1 = v0;
991 if (v0 == error_mark_node || v1 == error_mark_node
992 || mask == error_mark_node)
993 return error_mark_node;
995 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
997 if (complain)
998 error_at (loc, "__builtin_shuffle last argument must "
999 "be an integer vector");
1000 return error_mark_node;
1003 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1004 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1006 if (complain)
1007 error_at (loc, "__builtin_shuffle arguments must be vectors");
1008 return error_mark_node;
1011 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1013 if (complain)
1014 error_at (loc, "__builtin_shuffle argument vectors must be of "
1015 "the same type");
1016 return error_mark_node;
1019 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
1020 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
1021 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
1022 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1024 if (complain)
1025 error_at (loc, "__builtin_shuffle number of elements of the "
1026 "argument vector(s) and the mask vector should "
1027 "be the same");
1028 return error_mark_node;
1031 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1032 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1034 if (complain)
1035 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1036 "must have the same size as inner type of the mask");
1037 return error_mark_node;
1040 if (!c_dialect_cxx ())
1042 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1043 v0 = c_fully_fold (v0, false, &maybe_const);
1044 wrap &= maybe_const;
1046 if (two_arguments)
1047 v1 = v0 = save_expr (v0);
1048 else
1050 v1 = c_fully_fold (v1, false, &maybe_const);
1051 wrap &= maybe_const;
1054 mask = c_fully_fold (mask, false, &maybe_const);
1055 wrap &= maybe_const;
1057 else if (two_arguments)
1058 v1 = v0 = save_expr (v0);
1060 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1062 if (!c_dialect_cxx () && !wrap)
1063 ret = c_wrap_maybe_const (ret, true);
1065 return ret;
1068 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1069 to integral type. */
1071 tree
1072 c_common_get_narrower (tree op, int *unsignedp_ptr)
1074 op = get_narrower (op, unsignedp_ptr);
1076 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1077 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1079 /* C++0x scoped enumerations don't implicitly convert to integral
1080 type; if we stripped an explicit conversion to a larger type we
1081 need to replace it so common_type will still work. */
1082 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1083 TYPE_UNSIGNED (TREE_TYPE (op)));
1084 op = fold_convert (type, op);
1086 return op;
1089 /* This is a helper function of build_binary_op.
1091 For certain operations if both args were extended from the same
1092 smaller type, do the arithmetic in that type and then extend.
1094 BITWISE indicates a bitwise operation.
1095 For them, this optimization is safe only if
1096 both args are zero-extended or both are sign-extended.
1097 Otherwise, we might change the result.
1098 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1099 but calculated in (unsigned short) it would be (unsigned short)-1.
1101 tree
1102 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1104 int unsigned0, unsigned1;
1105 tree arg0, arg1;
1106 int uns;
1107 tree type;
1109 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1110 excessive narrowing when we call get_narrower below. For
1111 example, suppose that OP0 is of unsigned int extended
1112 from signed char and that RESULT_TYPE is long long int.
1113 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1114 like
1116 (long long int) (unsigned int) signed_char
1118 which get_narrower would narrow down to
1120 (unsigned int) signed char
1122 If we do not cast OP0 first, get_narrower would return
1123 signed_char, which is inconsistent with the case of the
1124 explicit cast. */
1125 op0 = convert (result_type, op0);
1126 op1 = convert (result_type, op1);
1128 arg0 = c_common_get_narrower (op0, &unsigned0);
1129 arg1 = c_common_get_narrower (op1, &unsigned1);
1131 /* UNS is 1 if the operation to be done is an unsigned one. */
1132 uns = TYPE_UNSIGNED (result_type);
1134 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1135 but it *requires* conversion to FINAL_TYPE. */
1137 if ((TYPE_PRECISION (TREE_TYPE (op0))
1138 == TYPE_PRECISION (TREE_TYPE (arg0)))
1139 && TREE_TYPE (op0) != result_type)
1140 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1141 if ((TYPE_PRECISION (TREE_TYPE (op1))
1142 == TYPE_PRECISION (TREE_TYPE (arg1)))
1143 && TREE_TYPE (op1) != result_type)
1144 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1146 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1148 /* For bitwise operations, signedness of nominal type
1149 does not matter. Consider only how operands were extended. */
1150 if (bitwise)
1151 uns = unsigned0;
1153 /* Note that in all three cases below we refrain from optimizing
1154 an unsigned operation on sign-extended args.
1155 That would not be valid. */
1157 /* Both args variable: if both extended in same way
1158 from same width, do it in that width.
1159 Do it unsigned if args were zero-extended. */
1160 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1161 < TYPE_PRECISION (result_type))
1162 && (TYPE_PRECISION (TREE_TYPE (arg1))
1163 == TYPE_PRECISION (TREE_TYPE (arg0)))
1164 && unsigned0 == unsigned1
1165 && (unsigned0 || !uns))
1166 return c_common_signed_or_unsigned_type
1167 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1169 else if (TREE_CODE (arg0) == INTEGER_CST
1170 && (unsigned1 || !uns)
1171 && (TYPE_PRECISION (TREE_TYPE (arg1))
1172 < TYPE_PRECISION (result_type))
1173 && (type
1174 = c_common_signed_or_unsigned_type (unsigned1,
1175 TREE_TYPE (arg1)))
1176 && !POINTER_TYPE_P (type)
1177 && int_fits_type_p (arg0, type))
1178 return type;
1180 else if (TREE_CODE (arg1) == INTEGER_CST
1181 && (unsigned0 || !uns)
1182 && (TYPE_PRECISION (TREE_TYPE (arg0))
1183 < TYPE_PRECISION (result_type))
1184 && (type
1185 = c_common_signed_or_unsigned_type (unsigned0,
1186 TREE_TYPE (arg0)))
1187 && !POINTER_TYPE_P (type)
1188 && int_fits_type_p (arg1, type))
1189 return type;
1191 return result_type;
1194 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1195 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1197 static bool
1198 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1200 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1201 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1202 REAL_VALUE_TYPE real_low_bound =
1203 real_value_from_int_cst (0, type_low_bound);
1204 REAL_VALUE_TYPE real_high_bound =
1205 real_value_from_int_cst (0, type_high_bound);
1207 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1208 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1211 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1212 to the complex/real/integer type TYPE. Function returns non-zero when:
1213 * EXPR is a constant which cannot be exactly converted to TYPE.
1214 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1215 for EXPR type and TYPE being both integers or both real, or both
1216 complex.
1217 * EXPR is not a constant of complex type and TYPE is a real or
1218 an integer.
1219 * EXPR is not a constant of real type and TYPE is an integer.
1220 * EXPR is not a constant of integer type which cannot be
1221 exactly converted to real type.
1223 Function allows conversions between types of different signedness and
1224 can return SAFE_CONVERSION (zero) in that case. Function can produce
1225 signedness warnings if PRODUCE_WARNS is true.
1227 Function allows conversions from complex constants to non-complex types,
1228 provided that imaginary part is zero and real part can be safely converted
1229 to TYPE. */
1231 enum conversion_safety
1232 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
1234 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1235 tree expr_type = TREE_TYPE (expr);
1236 loc = expansion_point_location_if_in_system_header (loc);
1238 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1240 /* If type is complex, we are interested in compatibility with
1241 underlying type. */
1242 if (TREE_CODE (type) == COMPLEX_TYPE)
1243 type = TREE_TYPE (type);
1245 /* Warn for real constant that is not an exact integer converted
1246 to integer type. */
1247 if (TREE_CODE (expr_type) == REAL_TYPE
1248 && TREE_CODE (type) == INTEGER_TYPE)
1250 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1251 give_warning = UNSAFE_REAL;
1253 /* Warn for an integer constant that does not fit into integer type. */
1254 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1255 && TREE_CODE (type) == INTEGER_TYPE
1256 && !int_fits_type_p (expr, type))
1258 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1259 && tree_int_cst_sgn (expr) < 0)
1261 if (produce_warns)
1262 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1263 " implicitly converted to unsigned type");
1265 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1267 if (produce_warns)
1268 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1269 " constant value to negative integer");
1271 else
1272 give_warning = UNSAFE_OTHER;
1274 else if (TREE_CODE (type) == REAL_TYPE)
1276 /* Warn for an integer constant that does not fit into real type. */
1277 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1279 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1280 if (!exact_real_truncate (TYPE_MODE (type), &a))
1281 give_warning = UNSAFE_REAL;
1283 /* Warn for a real constant that does not fit into a smaller
1284 real type. */
1285 else if (TREE_CODE (expr_type) == REAL_TYPE
1286 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1288 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1289 if (!exact_real_truncate (TYPE_MODE (type), &a))
1290 give_warning = UNSAFE_REAL;
1295 else if (TREE_CODE (expr) == COMPLEX_CST)
1297 tree imag_part = TREE_IMAGPART (expr);
1298 /* Conversion from complex constant with zero imaginary part,
1299 perform check for conversion of real part. */
1300 if ((TREE_CODE (imag_part) == REAL_CST
1301 && real_zerop (imag_part))
1302 || (TREE_CODE (imag_part) == INTEGER_CST
1303 && integer_zerop (imag_part)))
1304 /* Note: in this branch we use recursive call to unsafe_conversion_p
1305 with different type of EXPR, but it is still safe, because when EXPR
1306 is a constant, it's type is not used in text of generated warnings
1307 (otherwise they could sound misleading). */
1308 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1309 produce_warns);
1310 /* Conversion from complex constant with non-zero imaginary part. */
1311 else
1313 /* Conversion to complex type.
1314 Perform checks for both real and imaginary parts. */
1315 if (TREE_CODE (type) == COMPLEX_TYPE)
1317 /* Unfortunately, produce_warns must be false in two subsequent
1318 calls of unsafe_conversion_p, because otherwise we could
1319 produce strange "double" warnings, if both real and imaginary
1320 parts have conversion problems related to signedness.
1322 For example:
1323 int32_t _Complex a = 0x80000000 + 0x80000000i;
1325 Possible solution: add a separate function for checking
1326 constants and combine result of two calls appropriately. */
1327 enum conversion_safety re_safety =
1328 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
1329 enum conversion_safety im_safety =
1330 unsafe_conversion_p (loc, type, imag_part, false);
1332 /* Merge the results into appropriate single warning. */
1334 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1335 if (re_safety == im_safety)
1336 give_warning = re_safety;
1337 else if (!re_safety && im_safety)
1338 give_warning = im_safety;
1339 else if (re_safety && !im_safety)
1340 give_warning = re_safety;
1341 else
1342 give_warning = UNSAFE_OTHER;
1344 /* Warn about conversion from complex to real or integer type. */
1345 else
1346 give_warning = UNSAFE_IMAGINARY;
1350 /* Checks for remaining case: EXPR is not constant. */
1351 else
1353 /* Warn for real types converted to integer types. */
1354 if (TREE_CODE (expr_type) == REAL_TYPE
1355 && TREE_CODE (type) == INTEGER_TYPE)
1356 give_warning = UNSAFE_REAL;
1358 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1359 && TREE_CODE (type) == INTEGER_TYPE)
1361 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1362 expr = get_unwidened (expr, 0);
1363 expr_type = TREE_TYPE (expr);
1365 /* Don't warn for short y; short x = ((int)y & 0xff); */
1366 if (TREE_CODE (expr) == BIT_AND_EXPR
1367 || TREE_CODE (expr) == BIT_IOR_EXPR
1368 || TREE_CODE (expr) == BIT_XOR_EXPR)
1370 /* If both args were extended from a shortest type,
1371 use that type if that is safe. */
1372 expr_type = shorten_binary_op (expr_type,
1373 TREE_OPERAND (expr, 0),
1374 TREE_OPERAND (expr, 1),
1375 /* bitwise */1);
1377 if (TREE_CODE (expr) == BIT_AND_EXPR)
1379 tree op0 = TREE_OPERAND (expr, 0);
1380 tree op1 = TREE_OPERAND (expr, 1);
1381 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1382 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1384 /* If one of the operands is a non-negative constant
1385 that fits in the target type, then the type of the
1386 other operand does not matter. */
1387 if ((TREE_CODE (op0) == INTEGER_CST
1388 && int_fits_type_p (op0, c_common_signed_type (type))
1389 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1390 || (TREE_CODE (op1) == INTEGER_CST
1391 && int_fits_type_p (op1, c_common_signed_type (type))
1392 && int_fits_type_p (op1,
1393 c_common_unsigned_type (type))))
1394 return SAFE_CONVERSION;
1395 /* If constant is unsigned and fits in the target
1396 type, then the result will also fit. */
1397 else if ((TREE_CODE (op0) == INTEGER_CST
1398 && unsigned0
1399 && int_fits_type_p (op0, type))
1400 || (TREE_CODE (op1) == INTEGER_CST
1401 && unsigned1
1402 && int_fits_type_p (op1, type)))
1403 return SAFE_CONVERSION;
1406 /* Warn for integer types converted to smaller integer types. */
1407 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1408 give_warning = UNSAFE_OTHER;
1410 /* When they are the same width but different signedness,
1411 then the value may change. */
1412 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1413 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1414 /* Even when converted to a bigger type, if the type is
1415 unsigned but expr is signed, then negative values
1416 will be changed. */
1417 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1418 && produce_warns)
1419 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1420 "may change the sign of the result",
1421 type, expr_type);
1424 /* Warn for integer types converted to real types if and only if
1425 all the range of values of the integer type cannot be
1426 represented by the real type. */
1427 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1428 && TREE_CODE (type) == REAL_TYPE)
1430 /* Don't warn about char y = 0xff; float x = (int) y; */
1431 expr = get_unwidened (expr, 0);
1432 expr_type = TREE_TYPE (expr);
1434 if (!int_safely_convertible_to_real_p (expr_type, type))
1435 give_warning = UNSAFE_OTHER;
1438 /* Warn for real types converted to smaller real types. */
1439 else if (TREE_CODE (expr_type) == REAL_TYPE
1440 && TREE_CODE (type) == REAL_TYPE
1441 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1442 give_warning = UNSAFE_REAL;
1444 /* Check conversion between two complex types. */
1445 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1446 && TREE_CODE (type) == COMPLEX_TYPE)
1448 /* Extract underlying types (i.e., type of real and imaginary
1449 parts) of expr_type and type. */
1450 tree from_type = TREE_TYPE (expr_type);
1451 tree to_type = TREE_TYPE (type);
1453 /* Warn for real types converted to integer types. */
1454 if (TREE_CODE (from_type) == REAL_TYPE
1455 && TREE_CODE (to_type) == INTEGER_TYPE)
1456 give_warning = UNSAFE_REAL;
1458 /* Warn for real types converted to smaller real types. */
1459 else if (TREE_CODE (from_type) == REAL_TYPE
1460 && TREE_CODE (to_type) == REAL_TYPE
1461 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1462 give_warning = UNSAFE_REAL;
1464 /* Check conversion for complex integer types. Here implementation
1465 is simpler than for real-domain integers because it does not
1466 involve sophisticated cases, such as bitmasks, casts, etc. */
1467 else if (TREE_CODE (from_type) == INTEGER_TYPE
1468 && TREE_CODE (to_type) == INTEGER_TYPE)
1470 /* Warn for integer types converted to smaller integer types. */
1471 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1472 give_warning = UNSAFE_OTHER;
1474 /* Check for different signedness, see case for real-domain
1475 integers (above) for a more detailed comment. */
1476 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1477 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1478 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1479 && produce_warns)
1480 warning_at (loc, OPT_Wsign_conversion,
1481 "conversion to %qT from %qT "
1482 "may change the sign of the result",
1483 type, expr_type);
1485 else if (TREE_CODE (from_type) == INTEGER_TYPE
1486 && TREE_CODE (to_type) == REAL_TYPE
1487 && !int_safely_convertible_to_real_p (from_type, to_type))
1488 give_warning = UNSAFE_OTHER;
1491 /* Warn for complex types converted to real or integer types. */
1492 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1493 && TREE_CODE (type) != COMPLEX_TYPE)
1494 give_warning = UNSAFE_IMAGINARY;
1497 return give_warning;
1501 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1502 Invoke this function on every expression that is converted implicitly,
1503 i.e. because of language rules and not because of an explicit cast. */
1505 tree
1506 convert_and_check (location_t loc, tree type, tree expr)
1508 tree result;
1509 tree expr_for_warning;
1511 /* Convert from a value with possible excess precision rather than
1512 via the semantic type, but do not warn about values not fitting
1513 exactly in the semantic type. */
1514 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1516 tree orig_type = TREE_TYPE (expr);
1517 expr = TREE_OPERAND (expr, 0);
1518 expr_for_warning = convert (orig_type, expr);
1519 if (orig_type == type)
1520 return expr_for_warning;
1522 else
1523 expr_for_warning = expr;
1525 if (TREE_TYPE (expr) == type)
1526 return expr;
1528 result = convert (type, expr);
1530 if (c_inhibit_evaluation_warnings == 0
1531 && !TREE_OVERFLOW_P (expr)
1532 && result != error_mark_node)
1533 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1535 return result;
1538 /* A node in a list that describes references to variables (EXPR), which are
1539 either read accesses if WRITER is zero, or write accesses, in which case
1540 WRITER is the parent of EXPR. */
1541 struct tlist
1543 struct tlist *next;
1544 tree expr, writer;
1547 /* Used to implement a cache the results of a call to verify_tree. We only
1548 use this for SAVE_EXPRs. */
1549 struct tlist_cache
1551 struct tlist_cache *next;
1552 struct tlist *cache_before_sp;
1553 struct tlist *cache_after_sp;
1554 tree expr;
1557 /* Obstack to use when allocating tlist structures, and corresponding
1558 firstobj. */
1559 static struct obstack tlist_obstack;
1560 static char *tlist_firstobj = 0;
1562 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1563 warnings. */
1564 static struct tlist *warned_ids;
1565 /* SAVE_EXPRs need special treatment. We process them only once and then
1566 cache the results. */
1567 static struct tlist_cache *save_expr_cache;
1569 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1570 static void merge_tlist (struct tlist **, struct tlist *, int);
1571 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1572 static bool warning_candidate_p (tree);
1573 static bool candidate_equal_p (const_tree, const_tree);
1574 static void warn_for_collisions (struct tlist *);
1575 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1576 static struct tlist *new_tlist (struct tlist *, tree, tree);
1578 /* Create a new struct tlist and fill in its fields. */
1579 static struct tlist *
1580 new_tlist (struct tlist *next, tree t, tree writer)
1582 struct tlist *l;
1583 l = XOBNEW (&tlist_obstack, struct tlist);
1584 l->next = next;
1585 l->expr = t;
1586 l->writer = writer;
1587 return l;
1590 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1591 is nonnull, we ignore any node we find which has a writer equal to it. */
1593 static void
1594 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1596 while (add)
1598 struct tlist *next = add->next;
1599 if (!copy)
1600 add->next = *to;
1601 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1602 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1603 add = next;
1607 /* Merge the nodes of ADD into TO. This merging process is done so that for
1608 each variable that already exists in TO, no new node is added; however if
1609 there is a write access recorded in ADD, and an occurrence on TO is only
1610 a read access, then the occurrence in TO will be modified to record the
1611 write. */
1613 static void
1614 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1616 struct tlist **end = to;
1618 while (*end)
1619 end = &(*end)->next;
1621 while (add)
1623 int found = 0;
1624 struct tlist *tmp2;
1625 struct tlist *next = add->next;
1627 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1628 if (candidate_equal_p (tmp2->expr, add->expr))
1630 found = 1;
1631 if (!tmp2->writer)
1632 tmp2->writer = add->writer;
1634 if (!found)
1636 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1637 end = &(*end)->next;
1638 *end = 0;
1640 add = next;
1644 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1645 references in list LIST conflict with it, excluding reads if ONLY writers
1646 is nonzero. */
1648 static void
1649 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1650 int only_writes)
1652 struct tlist *tmp;
1654 /* Avoid duplicate warnings. */
1655 for (tmp = warned_ids; tmp; tmp = tmp->next)
1656 if (candidate_equal_p (tmp->expr, written))
1657 return;
1659 while (list)
1661 if (candidate_equal_p (list->expr, written)
1662 && !candidate_equal_p (list->writer, writer)
1663 && (!only_writes || list->writer))
1665 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1666 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1667 OPT_Wsequence_point, "operation on %qE may be undefined",
1668 list->expr);
1670 list = list->next;
1674 /* Given a list LIST of references to variables, find whether any of these
1675 can cause conflicts due to missing sequence points. */
1677 static void
1678 warn_for_collisions (struct tlist *list)
1680 struct tlist *tmp;
1682 for (tmp = list; tmp; tmp = tmp->next)
1684 if (tmp->writer)
1685 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1689 /* Return nonzero if X is a tree that can be verified by the sequence point
1690 warnings. */
1692 static bool
1693 warning_candidate_p (tree x)
1695 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1696 return false;
1698 if (TREE_CODE (x) == BLOCK)
1699 return false;
1701 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1702 (lvalue_p) crash on TRY/CATCH. */
1703 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1704 return false;
1706 if (!lvalue_p (x))
1707 return false;
1709 /* No point to track non-const calls, they will never satisfy
1710 operand_equal_p. */
1711 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1712 return false;
1714 if (TREE_CODE (x) == STRING_CST)
1715 return false;
1717 return true;
1720 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1721 static bool
1722 candidate_equal_p (const_tree x, const_tree y)
1724 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1727 /* Walk the tree X, and record accesses to variables. If X is written by the
1728 parent tree, WRITER is the parent.
1729 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1730 expression or its only operand forces a sequence point, then everything up
1731 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1732 in PNO_SP.
1733 Once we return, we will have emitted warnings if any subexpression before
1734 such a sequence point could be undefined. On a higher level, however, the
1735 sequence point may not be relevant, and we'll merge the two lists.
1737 Example: (b++, a) + b;
1738 The call that processes the COMPOUND_EXPR will store the increment of B
1739 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1740 processes the PLUS_EXPR will need to merge the two lists so that
1741 eventually, all accesses end up on the same list (and we'll warn about the
1742 unordered subexpressions b++ and b.
1744 A note on merging. If we modify the former example so that our expression
1745 becomes
1746 (b++, b) + a
1747 care must be taken not simply to add all three expressions into the final
1748 PNO_SP list. The function merge_tlist takes care of that by merging the
1749 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1750 way, so that no more than one access to B is recorded. */
1752 static void
1753 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1754 tree writer)
1756 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1757 enum tree_code code;
1758 enum tree_code_class cl;
1760 /* X may be NULL if it is the operand of an empty statement expression
1761 ({ }). */
1762 if (x == NULL)
1763 return;
1765 restart:
1766 code = TREE_CODE (x);
1767 cl = TREE_CODE_CLASS (code);
1769 if (warning_candidate_p (x))
1770 *pno_sp = new_tlist (*pno_sp, x, writer);
1772 switch (code)
1774 case CONSTRUCTOR:
1775 case SIZEOF_EXPR:
1776 return;
1778 case COMPOUND_EXPR:
1779 case TRUTH_ANDIF_EXPR:
1780 case TRUTH_ORIF_EXPR:
1781 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1782 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1783 warn_for_collisions (tmp_nosp);
1784 merge_tlist (pbefore_sp, tmp_before, 0);
1785 merge_tlist (pbefore_sp, tmp_nosp, 0);
1786 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1787 warn_for_collisions (tmp_list2);
1788 merge_tlist (pbefore_sp, tmp_list3, 0);
1789 merge_tlist (pno_sp, tmp_list2, 0);
1790 return;
1792 case COND_EXPR:
1793 tmp_before = tmp_list2 = 0;
1794 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1795 warn_for_collisions (tmp_list2);
1796 merge_tlist (pbefore_sp, tmp_before, 0);
1797 merge_tlist (pbefore_sp, tmp_list2, 0);
1799 tmp_list3 = tmp_nosp = 0;
1800 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1801 warn_for_collisions (tmp_nosp);
1802 merge_tlist (pbefore_sp, tmp_list3, 0);
1804 tmp_list3 = tmp_list2 = 0;
1805 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1806 warn_for_collisions (tmp_list2);
1807 merge_tlist (pbefore_sp, tmp_list3, 0);
1808 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1809 two first, to avoid warning for (a ? b++ : b++). */
1810 merge_tlist (&tmp_nosp, tmp_list2, 0);
1811 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1812 return;
1814 case PREDECREMENT_EXPR:
1815 case PREINCREMENT_EXPR:
1816 case POSTDECREMENT_EXPR:
1817 case POSTINCREMENT_EXPR:
1818 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1819 return;
1821 case MODIFY_EXPR:
1822 tmp_before = tmp_nosp = tmp_list3 = 0;
1823 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1824 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1825 /* Expressions inside the LHS are not ordered wrt. the sequence points
1826 in the RHS. Example:
1827 *a = (a++, 2)
1828 Despite the fact that the modification of "a" is in the before_sp
1829 list (tmp_before), it conflicts with the use of "a" in the LHS.
1830 We can handle this by adding the contents of tmp_list3
1831 to those of tmp_before, and redoing the collision warnings for that
1832 list. */
1833 add_tlist (&tmp_before, tmp_list3, x, 1);
1834 warn_for_collisions (tmp_before);
1835 /* Exclude the LHS itself here; we first have to merge it into the
1836 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1837 didn't exclude the LHS, we'd get it twice, once as a read and once
1838 as a write. */
1839 add_tlist (pno_sp, tmp_list3, x, 0);
1840 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1842 merge_tlist (pbefore_sp, tmp_before, 0);
1843 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1844 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1845 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1846 return;
1848 case CALL_EXPR:
1849 /* We need to warn about conflicts among arguments and conflicts between
1850 args and the function address. Side effects of the function address,
1851 however, are not ordered by the sequence point of the call. */
1853 call_expr_arg_iterator iter;
1854 tree arg;
1855 tmp_before = tmp_nosp = 0;
1856 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1857 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1859 tmp_list2 = tmp_list3 = 0;
1860 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1861 merge_tlist (&tmp_list3, tmp_list2, 0);
1862 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1864 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1865 warn_for_collisions (tmp_before);
1866 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1867 return;
1870 case TREE_LIST:
1871 /* Scan all the list, e.g. indices of multi dimensional array. */
1872 while (x)
1874 tmp_before = tmp_nosp = 0;
1875 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1876 merge_tlist (&tmp_nosp, tmp_before, 0);
1877 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1878 x = TREE_CHAIN (x);
1880 return;
1882 case SAVE_EXPR:
1884 struct tlist_cache *t;
1885 for (t = save_expr_cache; t; t = t->next)
1886 if (candidate_equal_p (t->expr, x))
1887 break;
1889 if (!t)
1891 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1892 t->next = save_expr_cache;
1893 t->expr = x;
1894 save_expr_cache = t;
1896 tmp_before = tmp_nosp = 0;
1897 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1898 warn_for_collisions (tmp_nosp);
1900 tmp_list3 = 0;
1901 merge_tlist (&tmp_list3, tmp_nosp, 0);
1902 t->cache_before_sp = tmp_before;
1903 t->cache_after_sp = tmp_list3;
1905 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1906 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1907 return;
1910 case ADDR_EXPR:
1911 x = TREE_OPERAND (x, 0);
1912 if (DECL_P (x))
1913 return;
1914 writer = 0;
1915 goto restart;
1917 default:
1918 /* For other expressions, simply recurse on their operands.
1919 Manual tail recursion for unary expressions.
1920 Other non-expressions need not be processed. */
1921 if (cl == tcc_unary)
1923 x = TREE_OPERAND (x, 0);
1924 writer = 0;
1925 goto restart;
1927 else if (IS_EXPR_CODE_CLASS (cl))
1929 int lp;
1930 int max = TREE_OPERAND_LENGTH (x);
1931 for (lp = 0; lp < max; lp++)
1933 tmp_before = tmp_nosp = 0;
1934 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1935 merge_tlist (&tmp_nosp, tmp_before, 0);
1936 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1939 return;
1943 /* Try to warn for undefined behavior in EXPR due to missing sequence
1944 points. */
1946 DEBUG_FUNCTION void
1947 verify_sequence_points (tree expr)
1949 struct tlist *before_sp = 0, *after_sp = 0;
1951 warned_ids = 0;
1952 save_expr_cache = 0;
1953 if (tlist_firstobj == 0)
1955 gcc_obstack_init (&tlist_obstack);
1956 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1959 verify_tree (expr, &before_sp, &after_sp, 0);
1960 warn_for_collisions (after_sp);
1961 obstack_free (&tlist_obstack, tlist_firstobj);
1964 /* Validate the expression after `case' and apply default promotions. */
1966 static tree
1967 check_case_value (location_t loc, tree value)
1969 if (value == NULL_TREE)
1970 return value;
1972 if (TREE_CODE (value) == INTEGER_CST)
1973 /* Promote char or short to int. */
1974 value = perform_integral_promotions (value);
1975 else if (value != error_mark_node)
1977 error_at (loc, "case label does not reduce to an integer constant");
1978 value = error_mark_node;
1981 constant_expression_warning (value);
1983 return value;
1986 /* See if the case values LOW and HIGH are in the range of the original
1987 type (i.e. before the default conversion to int) of the switch testing
1988 expression.
1989 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
1990 the type before promoting it. CASE_LOW_P is a pointer to the lower
1991 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
1992 if the case is not a case range.
1993 The caller has to make sure that we are not called with NULL for
1994 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
1995 was a case value that doesn't fit into the range of the ORIG_TYPE.
1996 Returns true if the case label is in range of ORIG_TYPE (saturated or
1997 untouched) or false if the label is out of range. */
1999 static bool
2000 check_case_bounds (location_t loc, tree type, tree orig_type,
2001 tree *case_low_p, tree *case_high_p,
2002 bool *outside_range_p)
2004 tree min_value, max_value;
2005 tree case_low = *case_low_p;
2006 tree case_high = case_high_p ? *case_high_p : case_low;
2008 /* If there was a problem with the original type, do nothing. */
2009 if (orig_type == error_mark_node)
2010 return true;
2012 min_value = TYPE_MIN_VALUE (orig_type);
2013 max_value = TYPE_MAX_VALUE (orig_type);
2015 /* We'll really need integer constants here. */
2016 case_low = fold (case_low);
2017 case_high = fold (case_high);
2019 /* Case label is less than minimum for type. */
2020 if (tree_int_cst_compare (case_low, min_value) < 0
2021 && tree_int_cst_compare (case_high, min_value) < 0)
2023 warning_at (loc, 0, "case label value is less than minimum value "
2024 "for type");
2025 *outside_range_p = true;
2026 return false;
2029 /* Case value is greater than maximum for type. */
2030 if (tree_int_cst_compare (case_low, max_value) > 0
2031 && tree_int_cst_compare (case_high, max_value) > 0)
2033 warning_at (loc, 0, "case label value exceeds maximum value for type");
2034 *outside_range_p = true;
2035 return false;
2038 /* Saturate lower case label value to minimum. */
2039 if (tree_int_cst_compare (case_high, min_value) >= 0
2040 && tree_int_cst_compare (case_low, min_value) < 0)
2042 warning_at (loc, 0, "lower value in case label range"
2043 " less than minimum value for type");
2044 *outside_range_p = true;
2045 case_low = min_value;
2048 /* Saturate upper case label value to maximum. */
2049 if (tree_int_cst_compare (case_low, max_value) <= 0
2050 && tree_int_cst_compare (case_high, max_value) > 0)
2052 warning_at (loc, 0, "upper value in case label range"
2053 " exceeds maximum value for type");
2054 *outside_range_p = true;
2055 case_high = max_value;
2058 if (*case_low_p != case_low)
2059 *case_low_p = convert (type, case_low);
2060 if (case_high_p && *case_high_p != case_high)
2061 *case_high_p = convert (type, case_high);
2063 return true;
2066 /* Return an integer type with BITS bits of precision,
2067 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2069 tree
2070 c_common_type_for_size (unsigned int bits, int unsignedp)
2072 int i;
2074 if (bits == TYPE_PRECISION (integer_type_node))
2075 return unsignedp ? unsigned_type_node : integer_type_node;
2077 if (bits == TYPE_PRECISION (signed_char_type_node))
2078 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2080 if (bits == TYPE_PRECISION (short_integer_type_node))
2081 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2083 if (bits == TYPE_PRECISION (long_integer_type_node))
2084 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2086 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2087 return (unsignedp ? long_long_unsigned_type_node
2088 : long_long_integer_type_node);
2090 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2091 if (int_n_enabled_p[i]
2092 && bits == int_n_data[i].bitsize)
2093 return (unsignedp ? int_n_trees[i].unsigned_type
2094 : int_n_trees[i].signed_type);
2096 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2097 return (unsignedp ? widest_unsigned_literal_type_node
2098 : widest_integer_literal_type_node);
2100 if (bits <= TYPE_PRECISION (intQI_type_node))
2101 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2103 if (bits <= TYPE_PRECISION (intHI_type_node))
2104 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2106 if (bits <= TYPE_PRECISION (intSI_type_node))
2107 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2109 if (bits <= TYPE_PRECISION (intDI_type_node))
2110 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2112 return 0;
2115 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2116 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2117 and saturating if SATP is nonzero, otherwise not saturating. */
2119 tree
2120 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2121 int unsignedp, int satp)
2123 machine_mode mode;
2124 if (ibit == 0)
2125 mode = unsignedp ? UQQmode : QQmode;
2126 else
2127 mode = unsignedp ? UHAmode : HAmode;
2129 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2130 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2131 break;
2133 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2135 sorry ("GCC cannot support operators with integer types and "
2136 "fixed-point types that have too many integral and "
2137 "fractional bits together");
2138 return 0;
2141 return c_common_type_for_mode (mode, satp);
2144 /* Used for communication between c_common_type_for_mode and
2145 c_register_builtin_type. */
2146 tree registered_builtin_types;
2148 /* Return a data type that has machine mode MODE.
2149 If the mode is an integer,
2150 then UNSIGNEDP selects between signed and unsigned types.
2151 If the mode is a fixed-point mode,
2152 then UNSIGNEDP selects between saturating and nonsaturating types. */
2154 tree
2155 c_common_type_for_mode (machine_mode mode, int unsignedp)
2157 tree t;
2158 int i;
2160 if (mode == TYPE_MODE (integer_type_node))
2161 return unsignedp ? unsigned_type_node : integer_type_node;
2163 if (mode == TYPE_MODE (signed_char_type_node))
2164 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2166 if (mode == TYPE_MODE (short_integer_type_node))
2167 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2169 if (mode == TYPE_MODE (long_integer_type_node))
2170 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2172 if (mode == TYPE_MODE (long_long_integer_type_node))
2173 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2175 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2176 if (int_n_enabled_p[i]
2177 && mode == int_n_data[i].m)
2178 return (unsignedp ? int_n_trees[i].unsigned_type
2179 : int_n_trees[i].signed_type);
2181 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2182 return unsignedp ? widest_unsigned_literal_type_node
2183 : widest_integer_literal_type_node;
2185 if (mode == QImode)
2186 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2188 if (mode == HImode)
2189 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2191 if (mode == SImode)
2192 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2194 if (mode == DImode)
2195 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2197 #if HOST_BITS_PER_WIDE_INT >= 64
2198 if (mode == TYPE_MODE (intTI_type_node))
2199 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2200 #endif
2202 if (mode == TYPE_MODE (float_type_node))
2203 return float_type_node;
2205 if (mode == TYPE_MODE (double_type_node))
2206 return double_type_node;
2208 if (mode == TYPE_MODE (long_double_type_node))
2209 return long_double_type_node;
2211 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2212 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2213 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2214 return FLOATN_NX_TYPE_NODE (i);
2216 if (mode == TYPE_MODE (void_type_node))
2217 return void_type_node;
2219 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2220 return (unsignedp
2221 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2222 : make_signed_type (GET_MODE_PRECISION (mode)));
2224 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2225 return (unsignedp
2226 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2227 : make_signed_type (GET_MODE_PRECISION (mode)));
2229 if (COMPLEX_MODE_P (mode))
2231 machine_mode inner_mode;
2232 tree inner_type;
2234 if (mode == TYPE_MODE (complex_float_type_node))
2235 return complex_float_type_node;
2236 if (mode == TYPE_MODE (complex_double_type_node))
2237 return complex_double_type_node;
2238 if (mode == TYPE_MODE (complex_long_double_type_node))
2239 return complex_long_double_type_node;
2241 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2242 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2243 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2244 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2246 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2247 return complex_integer_type_node;
2249 inner_mode = GET_MODE_INNER (mode);
2250 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2251 if (inner_type != NULL_TREE)
2252 return build_complex_type (inner_type);
2254 else if (VECTOR_MODE_P (mode))
2256 machine_mode inner_mode = GET_MODE_INNER (mode);
2257 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2258 if (inner_type != NULL_TREE)
2259 return build_vector_type_for_mode (inner_type, mode);
2262 if (mode == TYPE_MODE (dfloat32_type_node))
2263 return dfloat32_type_node;
2264 if (mode == TYPE_MODE (dfloat64_type_node))
2265 return dfloat64_type_node;
2266 if (mode == TYPE_MODE (dfloat128_type_node))
2267 return dfloat128_type_node;
2269 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2271 if (mode == TYPE_MODE (short_fract_type_node))
2272 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2273 if (mode == TYPE_MODE (fract_type_node))
2274 return unsignedp ? sat_fract_type_node : fract_type_node;
2275 if (mode == TYPE_MODE (long_fract_type_node))
2276 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2277 if (mode == TYPE_MODE (long_long_fract_type_node))
2278 return unsignedp ? sat_long_long_fract_type_node
2279 : long_long_fract_type_node;
2281 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2282 return unsignedp ? sat_unsigned_short_fract_type_node
2283 : unsigned_short_fract_type_node;
2284 if (mode == TYPE_MODE (unsigned_fract_type_node))
2285 return unsignedp ? sat_unsigned_fract_type_node
2286 : unsigned_fract_type_node;
2287 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2288 return unsignedp ? sat_unsigned_long_fract_type_node
2289 : unsigned_long_fract_type_node;
2290 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2291 return unsignedp ? sat_unsigned_long_long_fract_type_node
2292 : unsigned_long_long_fract_type_node;
2294 if (mode == TYPE_MODE (short_accum_type_node))
2295 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2296 if (mode == TYPE_MODE (accum_type_node))
2297 return unsignedp ? sat_accum_type_node : accum_type_node;
2298 if (mode == TYPE_MODE (long_accum_type_node))
2299 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2300 if (mode == TYPE_MODE (long_long_accum_type_node))
2301 return unsignedp ? sat_long_long_accum_type_node
2302 : long_long_accum_type_node;
2304 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2305 return unsignedp ? sat_unsigned_short_accum_type_node
2306 : unsigned_short_accum_type_node;
2307 if (mode == TYPE_MODE (unsigned_accum_type_node))
2308 return unsignedp ? sat_unsigned_accum_type_node
2309 : unsigned_accum_type_node;
2310 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2311 return unsignedp ? sat_unsigned_long_accum_type_node
2312 : unsigned_long_accum_type_node;
2313 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2314 return unsignedp ? sat_unsigned_long_long_accum_type_node
2315 : unsigned_long_long_accum_type_node;
2317 if (mode == QQmode)
2318 return unsignedp ? sat_qq_type_node : qq_type_node;
2319 if (mode == HQmode)
2320 return unsignedp ? sat_hq_type_node : hq_type_node;
2321 if (mode == SQmode)
2322 return unsignedp ? sat_sq_type_node : sq_type_node;
2323 if (mode == DQmode)
2324 return unsignedp ? sat_dq_type_node : dq_type_node;
2325 if (mode == TQmode)
2326 return unsignedp ? sat_tq_type_node : tq_type_node;
2328 if (mode == UQQmode)
2329 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2330 if (mode == UHQmode)
2331 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2332 if (mode == USQmode)
2333 return unsignedp ? sat_usq_type_node : usq_type_node;
2334 if (mode == UDQmode)
2335 return unsignedp ? sat_udq_type_node : udq_type_node;
2336 if (mode == UTQmode)
2337 return unsignedp ? sat_utq_type_node : utq_type_node;
2339 if (mode == HAmode)
2340 return unsignedp ? sat_ha_type_node : ha_type_node;
2341 if (mode == SAmode)
2342 return unsignedp ? sat_sa_type_node : sa_type_node;
2343 if (mode == DAmode)
2344 return unsignedp ? sat_da_type_node : da_type_node;
2345 if (mode == TAmode)
2346 return unsignedp ? sat_ta_type_node : ta_type_node;
2348 if (mode == UHAmode)
2349 return unsignedp ? sat_uha_type_node : uha_type_node;
2350 if (mode == USAmode)
2351 return unsignedp ? sat_usa_type_node : usa_type_node;
2352 if (mode == UDAmode)
2353 return unsignedp ? sat_uda_type_node : uda_type_node;
2354 if (mode == UTAmode)
2355 return unsignedp ? sat_uta_type_node : uta_type_node;
2358 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2359 if (TYPE_MODE (TREE_VALUE (t)) == mode
2360 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2361 return TREE_VALUE (t);
2363 return 0;
2366 tree
2367 c_common_unsigned_type (tree type)
2369 return c_common_signed_or_unsigned_type (1, type);
2372 /* Return a signed type the same as TYPE in other respects. */
2374 tree
2375 c_common_signed_type (tree type)
2377 return c_common_signed_or_unsigned_type (0, type);
2380 /* Return a type the same as TYPE except unsigned or
2381 signed according to UNSIGNEDP. */
2383 tree
2384 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2386 tree type1;
2387 int i;
2389 /* This block of code emulates the behavior of the old
2390 c_common_unsigned_type. In particular, it returns
2391 long_unsigned_type_node if passed a long, even when a int would
2392 have the same size. This is necessary for warnings to work
2393 correctly in archs where sizeof(int) == sizeof(long) */
2395 type1 = TYPE_MAIN_VARIANT (type);
2396 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2397 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2398 if (type1 == integer_type_node || type1 == unsigned_type_node)
2399 return unsignedp ? unsigned_type_node : integer_type_node;
2400 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2401 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2402 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2403 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2404 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2405 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2407 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2408 if (int_n_enabled_p[i]
2409 && (type1 == int_n_trees[i].unsigned_type
2410 || type1 == int_n_trees[i].signed_type))
2411 return (unsignedp ? int_n_trees[i].unsigned_type
2412 : int_n_trees[i].signed_type);
2414 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
2415 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
2416 #if HOST_BITS_PER_WIDE_INT >= 64
2417 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2418 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2419 #endif
2420 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2421 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2422 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2423 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2424 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2425 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2426 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2427 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2429 #define C_COMMON_FIXED_TYPES(NAME) \
2430 if (type1 == short_ ## NAME ## _type_node \
2431 || type1 == unsigned_short_ ## NAME ## _type_node) \
2432 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2433 : short_ ## NAME ## _type_node; \
2434 if (type1 == NAME ## _type_node \
2435 || type1 == unsigned_ ## NAME ## _type_node) \
2436 return unsignedp ? unsigned_ ## NAME ## _type_node \
2437 : NAME ## _type_node; \
2438 if (type1 == long_ ## NAME ## _type_node \
2439 || type1 == unsigned_long_ ## NAME ## _type_node) \
2440 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2441 : long_ ## NAME ## _type_node; \
2442 if (type1 == long_long_ ## NAME ## _type_node \
2443 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2444 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2445 : long_long_ ## NAME ## _type_node;
2447 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2448 if (type1 == NAME ## _type_node \
2449 || type1 == u ## NAME ## _type_node) \
2450 return unsignedp ? u ## NAME ## _type_node \
2451 : NAME ## _type_node;
2453 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2454 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2455 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2456 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2457 : sat_ ## short_ ## NAME ## _type_node; \
2458 if (type1 == sat_ ## NAME ## _type_node \
2459 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2460 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2461 : sat_ ## NAME ## _type_node; \
2462 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2463 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2464 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2465 : sat_ ## long_ ## NAME ## _type_node; \
2466 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2467 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2468 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2469 : sat_ ## long_long_ ## NAME ## _type_node;
2471 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2472 if (type1 == sat_ ## NAME ## _type_node \
2473 || type1 == sat_ ## u ## NAME ## _type_node) \
2474 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2475 : sat_ ## NAME ## _type_node;
2477 C_COMMON_FIXED_TYPES (fract);
2478 C_COMMON_FIXED_TYPES_SAT (fract);
2479 C_COMMON_FIXED_TYPES (accum);
2480 C_COMMON_FIXED_TYPES_SAT (accum);
2482 C_COMMON_FIXED_MODE_TYPES (qq);
2483 C_COMMON_FIXED_MODE_TYPES (hq);
2484 C_COMMON_FIXED_MODE_TYPES (sq);
2485 C_COMMON_FIXED_MODE_TYPES (dq);
2486 C_COMMON_FIXED_MODE_TYPES (tq);
2487 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2488 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2489 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2490 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2491 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2492 C_COMMON_FIXED_MODE_TYPES (ha);
2493 C_COMMON_FIXED_MODE_TYPES (sa);
2494 C_COMMON_FIXED_MODE_TYPES (da);
2495 C_COMMON_FIXED_MODE_TYPES (ta);
2496 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2497 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2498 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2499 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2501 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2502 the precision; they have precision set to match their range, but
2503 may use a wider mode to match an ABI. If we change modes, we may
2504 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2505 the precision as well, so as to yield correct results for
2506 bit-field types. C++ does not have these separate bit-field
2507 types, and producing a signed or unsigned variant of an
2508 ENUMERAL_TYPE may cause other problems as well. */
2510 if (!INTEGRAL_TYPE_P (type)
2511 || TYPE_UNSIGNED (type) == unsignedp)
2512 return type;
2514 #define TYPE_OK(node) \
2515 (TYPE_MODE (type) == TYPE_MODE (node) \
2516 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2517 if (TYPE_OK (signed_char_type_node))
2518 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2519 if (TYPE_OK (integer_type_node))
2520 return unsignedp ? unsigned_type_node : integer_type_node;
2521 if (TYPE_OK (short_integer_type_node))
2522 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2523 if (TYPE_OK (long_integer_type_node))
2524 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2525 if (TYPE_OK (long_long_integer_type_node))
2526 return (unsignedp ? long_long_unsigned_type_node
2527 : long_long_integer_type_node);
2529 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2530 if (int_n_enabled_p[i]
2531 && TYPE_MODE (type) == int_n_data[i].m
2532 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2533 return (unsignedp ? int_n_trees[i].unsigned_type
2534 : int_n_trees[i].signed_type);
2536 if (TYPE_OK (widest_integer_literal_type_node))
2537 return (unsignedp ? widest_unsigned_literal_type_node
2538 : widest_integer_literal_type_node);
2540 #if HOST_BITS_PER_WIDE_INT >= 64
2541 if (TYPE_OK (intTI_type_node))
2542 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2543 #endif
2544 if (TYPE_OK (intDI_type_node))
2545 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2546 if (TYPE_OK (intSI_type_node))
2547 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2548 if (TYPE_OK (intHI_type_node))
2549 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2550 if (TYPE_OK (intQI_type_node))
2551 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2552 #undef TYPE_OK
2554 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2557 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2559 tree
2560 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2562 int i;
2564 /* Extended integer types of the same width as a standard type have
2565 lesser rank, so those of the same width as int promote to int or
2566 unsigned int and are valid for printf formats expecting int or
2567 unsigned int. To avoid such special cases, avoid creating
2568 extended integer types for bit-fields if a standard integer type
2569 is available. */
2570 if (width == TYPE_PRECISION (integer_type_node))
2571 return unsignedp ? unsigned_type_node : integer_type_node;
2572 if (width == TYPE_PRECISION (signed_char_type_node))
2573 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2574 if (width == TYPE_PRECISION (short_integer_type_node))
2575 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2576 if (width == TYPE_PRECISION (long_integer_type_node))
2577 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2578 if (width == TYPE_PRECISION (long_long_integer_type_node))
2579 return (unsignedp ? long_long_unsigned_type_node
2580 : long_long_integer_type_node);
2581 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2582 if (int_n_enabled_p[i]
2583 && width == int_n_data[i].bitsize)
2584 return (unsignedp ? int_n_trees[i].unsigned_type
2585 : int_n_trees[i].signed_type);
2586 return build_nonstandard_integer_type (width, unsignedp);
2589 /* The C version of the register_builtin_type langhook. */
2591 void
2592 c_register_builtin_type (tree type, const char* name)
2594 tree decl;
2596 decl = build_decl (UNKNOWN_LOCATION,
2597 TYPE_DECL, get_identifier (name), type);
2598 DECL_ARTIFICIAL (decl) = 1;
2599 if (!TYPE_NAME (type))
2600 TYPE_NAME (type) = decl;
2601 pushdecl (decl);
2603 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2606 /* Print an error message for invalid operands to arith operation
2607 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2608 RICHLOC is a rich location for the message, containing either
2609 three separate locations for each of the operator and operands
2611 lhs op rhs
2612 ~~~ ^~ ~~~
2614 (C FE), or one location ranging over all over them
2616 lhs op rhs
2617 ~~~~^~~~~~
2619 (C++ FE). */
2621 void
2622 binary_op_error (rich_location *richloc, enum tree_code code,
2623 tree type0, tree type1)
2625 const char *opname;
2627 switch (code)
2629 case PLUS_EXPR:
2630 opname = "+"; break;
2631 case MINUS_EXPR:
2632 opname = "-"; break;
2633 case MULT_EXPR:
2634 opname = "*"; break;
2635 case MAX_EXPR:
2636 opname = "max"; break;
2637 case MIN_EXPR:
2638 opname = "min"; break;
2639 case EQ_EXPR:
2640 opname = "=="; break;
2641 case NE_EXPR:
2642 opname = "!="; break;
2643 case LE_EXPR:
2644 opname = "<="; break;
2645 case GE_EXPR:
2646 opname = ">="; break;
2647 case LT_EXPR:
2648 opname = "<"; break;
2649 case GT_EXPR:
2650 opname = ">"; break;
2651 case LSHIFT_EXPR:
2652 opname = "<<"; break;
2653 case RSHIFT_EXPR:
2654 opname = ">>"; break;
2655 case TRUNC_MOD_EXPR:
2656 case FLOOR_MOD_EXPR:
2657 opname = "%"; break;
2658 case TRUNC_DIV_EXPR:
2659 case FLOOR_DIV_EXPR:
2660 opname = "/"; break;
2661 case BIT_AND_EXPR:
2662 opname = "&"; break;
2663 case BIT_IOR_EXPR:
2664 opname = "|"; break;
2665 case TRUTH_ANDIF_EXPR:
2666 opname = "&&"; break;
2667 case TRUTH_ORIF_EXPR:
2668 opname = "||"; break;
2669 case BIT_XOR_EXPR:
2670 opname = "^"; break;
2671 default:
2672 gcc_unreachable ();
2674 error_at_rich_loc (richloc,
2675 "invalid operands to binary %s (have %qT and %qT)",
2676 opname, type0, type1);
2679 /* Given an expression as a tree, return its original type. Do this
2680 by stripping any conversion that preserves the sign and precision. */
2681 static tree
2682 expr_original_type (tree expr)
2684 STRIP_SIGN_NOPS (expr);
2685 return TREE_TYPE (expr);
2688 /* Subroutine of build_binary_op, used for comparison operations.
2689 See if the operands have both been converted from subword integer types
2690 and, if so, perhaps change them both back to their original type.
2691 This function is also responsible for converting the two operands
2692 to the proper common type for comparison.
2694 The arguments of this function are all pointers to local variables
2695 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2696 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2698 LOC is the location of the comparison.
2700 If this function returns nonzero, it means that the comparison has
2701 a constant value. What this function returns is an expression for
2702 that value. */
2704 tree
2705 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2706 tree *restype_ptr, enum tree_code *rescode_ptr)
2708 tree type;
2709 tree op0 = *op0_ptr;
2710 tree op1 = *op1_ptr;
2711 int unsignedp0, unsignedp1;
2712 int real1, real2;
2713 tree primop0, primop1;
2714 enum tree_code code = *rescode_ptr;
2716 /* Throw away any conversions to wider types
2717 already present in the operands. */
2719 primop0 = c_common_get_narrower (op0, &unsignedp0);
2720 primop1 = c_common_get_narrower (op1, &unsignedp1);
2722 /* If primopN is first sign-extended from primopN's precision to opN's
2723 precision, then zero-extended from opN's precision to
2724 *restype_ptr precision, shortenings might be invalid. */
2725 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2726 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2727 && !unsignedp0
2728 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2729 primop0 = op0;
2730 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2731 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2732 && !unsignedp1
2733 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2734 primop1 = op1;
2736 /* Handle the case that OP0 does not *contain* a conversion
2737 but it *requires* conversion to FINAL_TYPE. */
2739 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2740 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2741 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2742 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2744 /* If one of the operands must be floated, we cannot optimize. */
2745 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2746 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2748 /* If first arg is constant, swap the args (changing operation
2749 so value is preserved), for canonicalization. Don't do this if
2750 the second arg is 0. */
2752 if (TREE_CONSTANT (primop0)
2753 && !integer_zerop (primop1) && !real_zerop (primop1)
2754 && !fixed_zerop (primop1))
2756 std::swap (primop0, primop1);
2757 std::swap (op0, op1);
2758 *op0_ptr = op0;
2759 *op1_ptr = op1;
2760 std::swap (unsignedp0, unsignedp1);
2761 std::swap (real1, real2);
2763 switch (code)
2765 case LT_EXPR:
2766 code = GT_EXPR;
2767 break;
2768 case GT_EXPR:
2769 code = LT_EXPR;
2770 break;
2771 case LE_EXPR:
2772 code = GE_EXPR;
2773 break;
2774 case GE_EXPR:
2775 code = LE_EXPR;
2776 break;
2777 default:
2778 break;
2780 *rescode_ptr = code;
2783 /* If comparing an integer against a constant more bits wide,
2784 maybe we can deduce a value of 1 or 0 independent of the data.
2785 Or else truncate the constant now
2786 rather than extend the variable at run time.
2788 This is only interesting if the constant is the wider arg.
2789 Also, it is not safe if the constant is unsigned and the
2790 variable arg is signed, since in this case the variable
2791 would be sign-extended and then regarded as unsigned.
2792 Our technique fails in this case because the lowest/highest
2793 possible unsigned results don't follow naturally from the
2794 lowest/highest possible values of the variable operand.
2795 For just EQ_EXPR and NE_EXPR there is another technique that
2796 could be used: see if the constant can be faithfully represented
2797 in the other operand's type, by truncating it and reextending it
2798 and see if that preserves the constant's value. */
2800 if (!real1 && !real2
2801 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2802 && TREE_CODE (primop1) == INTEGER_CST
2803 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2805 int min_gt, max_gt, min_lt, max_lt;
2806 tree maxval, minval;
2807 /* 1 if comparison is nominally unsigned. */
2808 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2809 tree val;
2811 type = c_common_signed_or_unsigned_type (unsignedp0,
2812 TREE_TYPE (primop0));
2814 maxval = TYPE_MAX_VALUE (type);
2815 minval = TYPE_MIN_VALUE (type);
2817 if (unsignedp && !unsignedp0)
2818 *restype_ptr = c_common_signed_type (*restype_ptr);
2820 if (TREE_TYPE (primop1) != *restype_ptr)
2822 /* Convert primop1 to target type, but do not introduce
2823 additional overflow. We know primop1 is an int_cst. */
2824 primop1 = force_fit_type (*restype_ptr,
2825 wi::to_wide
2826 (primop1,
2827 TYPE_PRECISION (*restype_ptr)),
2828 0, TREE_OVERFLOW (primop1));
2830 if (type != *restype_ptr)
2832 minval = convert (*restype_ptr, minval);
2833 maxval = convert (*restype_ptr, maxval);
2836 min_gt = tree_int_cst_lt (primop1, minval);
2837 max_gt = tree_int_cst_lt (primop1, maxval);
2838 min_lt = tree_int_cst_lt (minval, primop1);
2839 max_lt = tree_int_cst_lt (maxval, primop1);
2841 val = 0;
2842 /* This used to be a switch, but Genix compiler can't handle that. */
2843 if (code == NE_EXPR)
2845 if (max_lt || min_gt)
2846 val = truthvalue_true_node;
2848 else if (code == EQ_EXPR)
2850 if (max_lt || min_gt)
2851 val = truthvalue_false_node;
2853 else if (code == LT_EXPR)
2855 if (max_lt)
2856 val = truthvalue_true_node;
2857 if (!min_lt)
2858 val = truthvalue_false_node;
2860 else if (code == GT_EXPR)
2862 if (min_gt)
2863 val = truthvalue_true_node;
2864 if (!max_gt)
2865 val = truthvalue_false_node;
2867 else if (code == LE_EXPR)
2869 if (!max_gt)
2870 val = truthvalue_true_node;
2871 if (min_gt)
2872 val = truthvalue_false_node;
2874 else if (code == GE_EXPR)
2876 if (!min_lt)
2877 val = truthvalue_true_node;
2878 if (max_lt)
2879 val = truthvalue_false_node;
2882 /* If primop0 was sign-extended and unsigned comparison specd,
2883 we did a signed comparison above using the signed type bounds.
2884 But the comparison we output must be unsigned.
2886 Also, for inequalities, VAL is no good; but if the signed
2887 comparison had *any* fixed result, it follows that the
2888 unsigned comparison just tests the sign in reverse
2889 (positive values are LE, negative ones GE).
2890 So we can generate an unsigned comparison
2891 against an extreme value of the signed type. */
2893 if (unsignedp && !unsignedp0)
2895 if (val != 0)
2896 switch (code)
2898 case LT_EXPR:
2899 case GE_EXPR:
2900 primop1 = TYPE_MIN_VALUE (type);
2901 val = 0;
2902 break;
2904 case LE_EXPR:
2905 case GT_EXPR:
2906 primop1 = TYPE_MAX_VALUE (type);
2907 val = 0;
2908 break;
2910 default:
2911 break;
2913 type = c_common_unsigned_type (type);
2916 if (TREE_CODE (primop0) != INTEGER_CST
2917 /* Don't warn if it's from a (non-system) macro. */
2918 && !(from_macro_expansion_at
2919 (expansion_point_location_if_in_system_header
2920 (EXPR_LOCATION (primop0)))))
2922 if (val == truthvalue_false_node)
2923 warning_at (loc, OPT_Wtype_limits,
2924 "comparison is always false due to limited range of data type");
2925 if (val == truthvalue_true_node)
2926 warning_at (loc, OPT_Wtype_limits,
2927 "comparison is always true due to limited range of data type");
2930 if (val != 0)
2932 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2933 if (TREE_SIDE_EFFECTS (primop0))
2934 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2935 return val;
2938 /* Value is not predetermined, but do the comparison
2939 in the type of the operand that is not constant.
2940 TYPE is already properly set. */
2943 /* If either arg is decimal float and the other is float, find the
2944 proper common type to use for comparison. */
2945 else if (real1 && real2
2946 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2947 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2948 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2950 /* If either arg is decimal float and the other is float, fail. */
2951 else if (real1 && real2
2952 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2953 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2954 return 0;
2956 else if (real1 && real2
2957 && (TYPE_PRECISION (TREE_TYPE (primop0))
2958 == TYPE_PRECISION (TREE_TYPE (primop1))))
2959 type = TREE_TYPE (primop0);
2961 /* If args' natural types are both narrower than nominal type
2962 and both extend in the same manner, compare them
2963 in the type of the wider arg.
2964 Otherwise must actually extend both to the nominal
2965 common type lest different ways of extending
2966 alter the result.
2967 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2969 else if (unsignedp0 == unsignedp1 && real1 == real2
2970 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2971 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2973 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2974 type = c_common_signed_or_unsigned_type (unsignedp0
2975 || TYPE_UNSIGNED (*restype_ptr),
2976 type);
2977 /* Make sure shorter operand is extended the right way
2978 to match the longer operand. */
2979 primop0
2980 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2981 TREE_TYPE (primop0)),
2982 primop0);
2983 primop1
2984 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2985 TREE_TYPE (primop1)),
2986 primop1);
2988 else
2990 /* Here we must do the comparison on the nominal type
2991 using the args exactly as we received them. */
2992 type = *restype_ptr;
2993 primop0 = op0;
2994 primop1 = op1;
2996 if (!real1 && !real2 && integer_zerop (primop1)
2997 && TYPE_UNSIGNED (*restype_ptr))
2999 tree value = 0;
3000 /* All unsigned values are >= 0, so we warn. However,
3001 if OP0 is a constant that is >= 0, the signedness of
3002 the comparison isn't an issue, so suppress the
3003 warning. */
3004 bool warn =
3005 warn_type_limits && !in_system_header_at (loc)
3006 && !(TREE_CODE (primop0) == INTEGER_CST
3007 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3008 primop0)))
3009 /* Do not warn for enumeration types. */
3010 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3012 switch (code)
3014 case GE_EXPR:
3015 if (warn)
3016 warning_at (loc, OPT_Wtype_limits,
3017 "comparison of unsigned expression >= 0 is always true");
3018 value = truthvalue_true_node;
3019 break;
3021 case LT_EXPR:
3022 if (warn)
3023 warning_at (loc, OPT_Wtype_limits,
3024 "comparison of unsigned expression < 0 is always false");
3025 value = truthvalue_false_node;
3026 break;
3028 default:
3029 break;
3032 if (value != 0)
3034 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3035 if (TREE_SIDE_EFFECTS (primop0))
3036 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3037 primop0, value);
3038 return value;
3043 *op0_ptr = convert (type, primop0);
3044 *op1_ptr = convert (type, primop1);
3046 *restype_ptr = truthvalue_type_node;
3048 return 0;
3051 /* Return a tree for the sum or difference (RESULTCODE says which)
3052 of pointer PTROP and integer INTOP. */
3054 tree
3055 pointer_int_sum (location_t loc, enum tree_code resultcode,
3056 tree ptrop, tree intop, bool complain)
3058 tree size_exp, ret;
3060 /* The result is a pointer of the same type that is being added. */
3061 tree result_type = TREE_TYPE (ptrop);
3063 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3065 if (complain && warn_pointer_arith)
3066 pedwarn (loc, OPT_Wpointer_arith,
3067 "pointer of type %<void *%> used in arithmetic");
3068 else if (!complain)
3069 return error_mark_node;
3070 size_exp = integer_one_node;
3072 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3074 if (complain && warn_pointer_arith)
3075 pedwarn (loc, OPT_Wpointer_arith,
3076 "pointer to a function used in arithmetic");
3077 else if (!complain)
3078 return error_mark_node;
3079 size_exp = integer_one_node;
3081 else
3082 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3084 /* We are manipulating pointer values, so we don't need to warn
3085 about relying on undefined signed overflow. We disable the
3086 warning here because we use integer types so fold won't know that
3087 they are really pointers. */
3088 fold_defer_overflow_warnings ();
3090 /* If what we are about to multiply by the size of the elements
3091 contains a constant term, apply distributive law
3092 and multiply that constant term separately.
3093 This helps produce common subexpressions. */
3094 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3095 && !TREE_CONSTANT (intop)
3096 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3097 && TREE_CONSTANT (size_exp)
3098 /* If the constant comes from pointer subtraction,
3099 skip this optimization--it would cause an error. */
3100 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3101 /* If the constant is unsigned, and smaller than the pointer size,
3102 then we must skip this optimization. This is because it could cause
3103 an overflow error if the constant is negative but INTOP is not. */
3104 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3105 || (TYPE_PRECISION (TREE_TYPE (intop))
3106 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3108 enum tree_code subcode = resultcode;
3109 tree int_type = TREE_TYPE (intop);
3110 if (TREE_CODE (intop) == MINUS_EXPR)
3111 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3112 /* Convert both subexpression types to the type of intop,
3113 because weird cases involving pointer arithmetic
3114 can result in a sum or difference with different type args. */
3115 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3116 subcode, ptrop,
3117 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3118 intop = convert (int_type, TREE_OPERAND (intop, 0));
3121 /* Convert the integer argument to a type the same size as sizetype
3122 so the multiply won't overflow spuriously. */
3123 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3124 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3125 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3126 TYPE_UNSIGNED (sizetype)), intop);
3128 /* Replace the integer argument with a suitable product by the object size.
3129 Do this multiplication as signed, then convert to the appropriate type
3130 for the pointer operation and disregard an overflow that occurred only
3131 because of the sign-extension change in the latter conversion. */
3133 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3134 convert (TREE_TYPE (intop), size_exp));
3135 intop = convert (sizetype, t);
3136 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3137 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
3140 /* Create the sum or difference. */
3141 if (resultcode == MINUS_EXPR)
3142 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3144 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3146 fold_undefer_and_ignore_overflow_warnings ();
3148 return ret;
3151 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3152 and if NON_CONST is known not to be permitted in an evaluated part
3153 of a constant expression. */
3155 tree
3156 c_wrap_maybe_const (tree expr, bool non_const)
3158 bool nowarning = TREE_NO_WARNING (expr);
3159 location_t loc = EXPR_LOCATION (expr);
3161 /* This should never be called for C++. */
3162 if (c_dialect_cxx ())
3163 gcc_unreachable ();
3165 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3166 STRIP_TYPE_NOPS (expr);
3167 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3168 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3169 if (nowarning)
3170 TREE_NO_WARNING (expr) = 1;
3171 protected_set_expr_location (expr, loc);
3173 return expr;
3176 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3177 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3178 around the SAVE_EXPR if needed so that c_fully_fold does not need
3179 to look inside SAVE_EXPRs. */
3181 tree
3182 c_save_expr (tree expr)
3184 bool maybe_const = true;
3185 if (c_dialect_cxx ())
3186 return save_expr (expr);
3187 expr = c_fully_fold (expr, false, &maybe_const);
3188 expr = save_expr (expr);
3189 if (!maybe_const)
3190 expr = c_wrap_maybe_const (expr, true);
3191 return expr;
3194 /* Return whether EXPR is a declaration whose address can never be
3195 NULL. */
3197 bool
3198 decl_with_nonnull_addr_p (const_tree expr)
3200 return (DECL_P (expr)
3201 && (TREE_CODE (expr) == PARM_DECL
3202 || TREE_CODE (expr) == LABEL_DECL
3203 || !DECL_WEAK (expr)));
3206 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3207 or for an `if' or `while' statement or ?..: exp. It should already
3208 have been validated to be of suitable type; otherwise, a bad
3209 diagnostic may result.
3211 The EXPR is located at LOCATION.
3213 This preparation consists of taking the ordinary
3214 representation of an expression expr and producing a valid tree
3215 boolean expression describing whether expr is nonzero. We could
3216 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3217 but we optimize comparisons, &&, ||, and !.
3219 The resulting type should always be `truthvalue_type_node'. */
3221 tree
3222 c_common_truthvalue_conversion (location_t location, tree expr)
3224 switch (TREE_CODE (expr))
3226 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3227 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3228 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3229 case ORDERED_EXPR: case UNORDERED_EXPR:
3230 if (TREE_TYPE (expr) == truthvalue_type_node)
3231 return expr;
3232 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3233 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3234 goto ret;
3236 case TRUTH_ANDIF_EXPR:
3237 case TRUTH_ORIF_EXPR:
3238 case TRUTH_AND_EXPR:
3239 case TRUTH_OR_EXPR:
3240 case TRUTH_XOR_EXPR:
3241 if (TREE_TYPE (expr) == truthvalue_type_node)
3242 return expr;
3243 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3244 c_common_truthvalue_conversion (location,
3245 TREE_OPERAND (expr, 0)),
3246 c_common_truthvalue_conversion (location,
3247 TREE_OPERAND (expr, 1)));
3248 goto ret;
3250 case TRUTH_NOT_EXPR:
3251 if (TREE_TYPE (expr) == truthvalue_type_node)
3252 return expr;
3253 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3254 c_common_truthvalue_conversion (location,
3255 TREE_OPERAND (expr, 0)));
3256 goto ret;
3258 case ERROR_MARK:
3259 return expr;
3261 case INTEGER_CST:
3262 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3263 && !integer_zerop (expr)
3264 && !integer_onep (expr))
3265 warning_at (location, OPT_Wint_in_bool_context,
3266 "enum constant in boolean context");
3267 return integer_zerop (expr) ? truthvalue_false_node
3268 : truthvalue_true_node;
3270 case REAL_CST:
3271 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3272 ? truthvalue_true_node
3273 : truthvalue_false_node;
3275 case FIXED_CST:
3276 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3277 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3278 ? truthvalue_true_node
3279 : truthvalue_false_node;
3281 case FUNCTION_DECL:
3282 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3283 /* Fall through. */
3285 case ADDR_EXPR:
3287 tree inner = TREE_OPERAND (expr, 0);
3288 if (decl_with_nonnull_addr_p (inner))
3290 /* Common Ada/Pascal programmer's mistake. */
3291 warning_at (location,
3292 OPT_Waddress,
3293 "the address of %qD will always evaluate as %<true%>",
3294 inner);
3295 return truthvalue_true_node;
3297 break;
3300 case COMPLEX_EXPR:
3301 expr = build_binary_op (EXPR_LOCATION (expr),
3302 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3303 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3304 c_common_truthvalue_conversion (location,
3305 TREE_OPERAND (expr, 0)),
3306 c_common_truthvalue_conversion (location,
3307 TREE_OPERAND (expr, 1)),
3309 goto ret;
3311 case NEGATE_EXPR:
3312 case ABS_EXPR:
3313 case FLOAT_EXPR:
3314 case EXCESS_PRECISION_EXPR:
3315 /* These don't change whether an object is nonzero or zero. */
3316 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3318 case LROTATE_EXPR:
3319 case RROTATE_EXPR:
3320 /* These don't change whether an object is zero or nonzero, but
3321 we can't ignore them if their second arg has side-effects. */
3322 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3324 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3325 TREE_OPERAND (expr, 1),
3326 c_common_truthvalue_conversion
3327 (location, TREE_OPERAND (expr, 0)));
3328 goto ret;
3330 else
3331 return c_common_truthvalue_conversion (location,
3332 TREE_OPERAND (expr, 0));
3334 case MULT_EXPR:
3335 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3336 "%<*%> in boolean context, suggest %<&&%> instead");
3337 break;
3339 case LSHIFT_EXPR:
3340 /* We will only warn on signed shifts here, because the majority of
3341 false positive warnings happen in code where unsigned arithmetic
3342 was used in anticipation of a possible overflow.
3343 Furthermore, if we see an unsigned type here we know that the
3344 result of the shift is not subject to integer promotion rules. */
3345 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3346 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3347 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3348 "%<<<%> in boolean context, did you mean %<<%> ?");
3349 break;
3351 case COND_EXPR:
3352 if (warn_int_in_bool_context
3353 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3355 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3356 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3357 if (TREE_CODE (val1) == INTEGER_CST
3358 && TREE_CODE (val2) == INTEGER_CST
3359 && !integer_zerop (val1)
3360 && !integer_zerop (val2)
3361 && (!integer_onep (val1)
3362 || !integer_onep (val2)))
3363 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3364 "?: using integer constants in boolean context, "
3365 "the expression will always evaluate to %<true%>");
3366 else if ((TREE_CODE (val1) == INTEGER_CST
3367 && !integer_zerop (val1)
3368 && !integer_onep (val1))
3369 || (TREE_CODE (val2) == INTEGER_CST
3370 && !integer_zerop (val2)
3371 && !integer_onep (val2)))
3372 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3373 "?: using integer constants in boolean context");
3375 /* Distribute the conversion into the arms of a COND_EXPR. */
3376 if (c_dialect_cxx ())
3377 /* Avoid premature folding. */
3378 break;
3379 else
3381 int w = warn_int_in_bool_context;
3382 warn_int_in_bool_context = 0;
3383 /* Folding will happen later for C. */
3384 expr = build3 (COND_EXPR, truthvalue_type_node,
3385 TREE_OPERAND (expr, 0),
3386 c_common_truthvalue_conversion (location,
3387 TREE_OPERAND (expr, 1)),
3388 c_common_truthvalue_conversion (location,
3389 TREE_OPERAND (expr, 2)));
3390 warn_int_in_bool_context = w;
3391 goto ret;
3394 CASE_CONVERT:
3396 tree totype = TREE_TYPE (expr);
3397 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3399 if (POINTER_TYPE_P (totype)
3400 && !c_inhibit_evaluation_warnings
3401 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3403 tree inner = expr;
3404 STRIP_NOPS (inner);
3406 if (DECL_P (inner))
3407 warning_at (location,
3408 OPT_Waddress,
3409 "the compiler can assume that the address of "
3410 "%qD will always evaluate to %<true%>",
3411 inner);
3414 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3415 since that affects how `default_conversion' will behave. */
3416 if (TREE_CODE (totype) == REFERENCE_TYPE
3417 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3418 break;
3419 /* Don't strip a conversion from C++0x scoped enum, since they
3420 don't implicitly convert to other types. */
3421 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3422 && ENUM_IS_SCOPED (fromtype))
3423 break;
3424 /* If this isn't narrowing the argument, we can ignore it. */
3425 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3426 return c_common_truthvalue_conversion (location,
3427 TREE_OPERAND (expr, 0));
3429 break;
3431 case MODIFY_EXPR:
3432 if (!TREE_NO_WARNING (expr)
3433 && warn_parentheses)
3435 warning_at (location, OPT_Wparentheses,
3436 "suggest parentheses around assignment used as "
3437 "truth value");
3438 TREE_NO_WARNING (expr) = 1;
3440 break;
3442 default:
3443 break;
3446 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3448 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
3449 expr = (build_binary_op
3450 (EXPR_LOCATION (expr),
3451 (TREE_SIDE_EFFECTS (expr)
3452 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3453 c_common_truthvalue_conversion
3454 (location,
3455 build_unary_op (location, REALPART_EXPR, t, false)),
3456 c_common_truthvalue_conversion
3457 (location,
3458 build_unary_op (location, IMAGPART_EXPR, t, false)),
3459 0));
3460 goto ret;
3463 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3465 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3466 FCONST0 (TYPE_MODE
3467 (TREE_TYPE (expr))));
3468 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3470 else
3471 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3473 ret:
3474 protected_set_expr_location (expr, location);
3475 return expr;
3478 static void def_builtin_1 (enum built_in_function fncode,
3479 const char *name,
3480 enum built_in_class fnclass,
3481 tree fntype, tree libtype,
3482 bool both_p, bool fallback_p, bool nonansi_p,
3483 tree fnattrs, bool implicit_p);
3486 /* Apply the TYPE_QUALS to the new DECL. */
3488 void
3489 c_apply_type_quals_to_decl (int type_quals, tree decl)
3491 tree type = TREE_TYPE (decl);
3493 if (type == error_mark_node)
3494 return;
3496 if ((type_quals & TYPE_QUAL_CONST)
3497 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3498 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3499 constructor can produce constant init, so rely on cp_finish_decl to
3500 clear TREE_READONLY if the variable has non-constant init. */
3501 TREE_READONLY (decl) = 1;
3502 if (type_quals & TYPE_QUAL_VOLATILE)
3504 TREE_SIDE_EFFECTS (decl) = 1;
3505 TREE_THIS_VOLATILE (decl) = 1;
3507 if (type_quals & TYPE_QUAL_RESTRICT)
3509 while (type && TREE_CODE (type) == ARRAY_TYPE)
3510 /* Allow 'restrict' on arrays of pointers.
3511 FIXME currently we just ignore it. */
3512 type = TREE_TYPE (type);
3513 if (!type
3514 || !POINTER_TYPE_P (type)
3515 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3516 error ("invalid use of %<restrict%>");
3520 struct c_type_hasher : ggc_ptr_hash<tree_node>
3522 static hashval_t hash (tree);
3523 static bool equal (tree, tree);
3526 /* Hash function for the problem of multiple type definitions in
3527 different files. This must hash all types that will compare
3528 equal via comptypes to the same value. In practice it hashes
3529 on some of the simple stuff and leaves the details to comptypes. */
3531 hashval_t
3532 c_type_hasher::hash (tree t)
3534 int n_elements;
3535 int shift, size;
3536 tree t2;
3537 switch (TREE_CODE (t))
3539 /* For pointers, hash on pointee type plus some swizzling. */
3540 case POINTER_TYPE:
3541 return hash (TREE_TYPE (t)) ^ 0x3003003;
3542 /* Hash on number of elements and total size. */
3543 case ENUMERAL_TYPE:
3544 shift = 3;
3545 t2 = TYPE_VALUES (t);
3546 break;
3547 case RECORD_TYPE:
3548 shift = 0;
3549 t2 = TYPE_FIELDS (t);
3550 break;
3551 case QUAL_UNION_TYPE:
3552 shift = 1;
3553 t2 = TYPE_FIELDS (t);
3554 break;
3555 case UNION_TYPE:
3556 shift = 2;
3557 t2 = TYPE_FIELDS (t);
3558 break;
3559 default:
3560 gcc_unreachable ();
3562 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
3563 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
3564 n_elements = list_length (t2);
3565 /* We might have a VLA here. */
3566 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
3567 size = 0;
3568 else
3569 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
3570 return ((size << 24) | (n_elements << shift));
3573 bool
3574 c_type_hasher::equal (tree t1, tree t2)
3576 return lang_hooks.types_compatible_p (t1, t2);
3579 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
3581 /* Return the typed-based alias set for T, which may be an expression
3582 or a type. Return -1 if we don't do anything special. */
3584 alias_set_type
3585 c_common_get_alias_set (tree t)
3587 /* For VLAs, use the alias set of the element type rather than the
3588 default of alias set 0 for types compared structurally. */
3589 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3591 if (TREE_CODE (t) == ARRAY_TYPE)
3592 return get_alias_set (TREE_TYPE (t));
3593 return -1;
3596 /* That's all the expressions we handle specially. */
3597 if (!TYPE_P (t))
3598 return -1;
3600 /* The C standard guarantees that any object may be accessed via an
3601 lvalue that has character type. */
3602 if (t == char_type_node
3603 || t == signed_char_type_node
3604 || t == unsigned_char_type_node)
3605 return 0;
3607 /* The C standard specifically allows aliasing between signed and
3608 unsigned variants of the same type. We treat the signed
3609 variant as canonical. */
3610 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3612 tree t1 = c_common_signed_type (t);
3614 /* t1 == t can happen for boolean nodes which are always unsigned. */
3615 if (t1 != t)
3616 return get_alias_set (t1);
3619 /* Handle the case of multiple type nodes referring to "the same" type,
3620 which occurs with IMA. These share an alias set. FIXME: Currently only
3621 C90 is handled. (In C99 type compatibility is not transitive, which
3622 complicates things mightily. The alias set splay trees can theoretically
3623 represent this, but insertion is tricky when you consider all the
3624 different orders things might arrive in.) */
3626 if (c_language != clk_c || flag_isoc99)
3627 return -1;
3629 /* Save time if there's only one input file. */
3630 if (num_in_fnames == 1)
3631 return -1;
3633 /* Pointers need special handling if they point to any type that
3634 needs special handling (below). */
3635 if (TREE_CODE (t) == POINTER_TYPE)
3637 tree t2;
3638 /* Find bottom type under any nested POINTERs. */
3639 for (t2 = TREE_TYPE (t);
3640 TREE_CODE (t2) == POINTER_TYPE;
3641 t2 = TREE_TYPE (t2))
3643 if (!RECORD_OR_UNION_TYPE_P (t2)
3644 && TREE_CODE (t2) != ENUMERAL_TYPE)
3645 return -1;
3646 if (TYPE_SIZE (t2) == 0)
3647 return -1;
3649 /* These are the only cases that need special handling. */
3650 if (!RECORD_OR_UNION_TYPE_P (t)
3651 && TREE_CODE (t) != ENUMERAL_TYPE
3652 && TREE_CODE (t) != POINTER_TYPE)
3653 return -1;
3654 /* Undefined? */
3655 if (TYPE_SIZE (t) == 0)
3656 return -1;
3658 /* Look up t in hash table. Only one of the compatible types within each
3659 alias set is recorded in the table. */
3660 if (!type_hash_table)
3661 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
3662 tree *slot = type_hash_table->find_slot (t, INSERT);
3663 if (*slot != NULL)
3665 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
3666 return TYPE_ALIAS_SET ((tree)*slot);
3668 else
3669 /* Our caller will assign and record (in t) a new alias set; all we need
3670 to do is remember t in the hash table. */
3671 *slot = t;
3673 return -1;
3676 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3677 the IS_SIZEOF parameter indicates which operator is being applied.
3678 The COMPLAIN flag controls whether we should diagnose possibly
3679 ill-formed constructs or not. LOC is the location of the SIZEOF or
3680 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3681 a type in any context should be returned, rather than the normal
3682 alignment for that type. */
3684 tree
3685 c_sizeof_or_alignof_type (location_t loc,
3686 tree type, bool is_sizeof, bool min_alignof,
3687 int complain)
3689 const char *op_name;
3690 tree value = NULL;
3691 enum tree_code type_code = TREE_CODE (type);
3693 op_name = is_sizeof ? "sizeof" : "__alignof__";
3695 if (type_code == FUNCTION_TYPE)
3697 if (is_sizeof)
3699 if (complain && warn_pointer_arith)
3700 pedwarn (loc, OPT_Wpointer_arith,
3701 "invalid application of %<sizeof%> to a function type");
3702 else if (!complain)
3703 return error_mark_node;
3704 value = size_one_node;
3706 else
3708 if (complain)
3710 if (c_dialect_cxx ())
3711 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3712 "%<alignof%> applied to a function type");
3713 else
3714 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3715 "%<_Alignof%> applied to a function type");
3717 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3720 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3722 if (type_code == VOID_TYPE
3723 && complain && warn_pointer_arith)
3724 pedwarn (loc, OPT_Wpointer_arith,
3725 "invalid application of %qs to a void type", op_name);
3726 else if (!complain)
3727 return error_mark_node;
3728 value = size_one_node;
3730 else if (!COMPLETE_TYPE_P (type)
3731 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3733 if (complain)
3734 error_at (loc, "invalid application of %qs to incomplete type %qT",
3735 op_name, type);
3736 return error_mark_node;
3738 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3739 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3741 if (complain)
3742 error_at (loc, "invalid application of %qs to array type %qT of "
3743 "incomplete element type", op_name, type);
3744 return error_mark_node;
3746 else
3748 if (is_sizeof)
3749 /* Convert in case a char is more than one unit. */
3750 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3751 size_int (TYPE_PRECISION (char_type_node)
3752 / BITS_PER_UNIT));
3753 else if (min_alignof)
3754 value = size_int (min_align_of_type (type));
3755 else
3756 value = size_int (TYPE_ALIGN_UNIT (type));
3759 /* VALUE will have the middle-end integer type sizetype.
3760 However, we should really return a value of type `size_t',
3761 which is just a typedef for an ordinary integer type. */
3762 value = fold_convert_loc (loc, size_type_node, value);
3764 return value;
3767 /* Implement the __alignof keyword: Return the minimum required
3768 alignment of EXPR, measured in bytes. For VAR_DECLs,
3769 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3770 from an "aligned" __attribute__ specification). LOC is the
3771 location of the ALIGNOF operator. */
3773 tree
3774 c_alignof_expr (location_t loc, tree expr)
3776 tree t;
3778 if (VAR_OR_FUNCTION_DECL_P (expr))
3779 t = size_int (DECL_ALIGN_UNIT (expr));
3781 else if (TREE_CODE (expr) == COMPONENT_REF
3782 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3784 error_at (loc, "%<__alignof%> applied to a bit-field");
3785 t = size_one_node;
3787 else if (TREE_CODE (expr) == COMPONENT_REF
3788 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3789 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3791 else if (INDIRECT_REF_P (expr))
3793 tree t = TREE_OPERAND (expr, 0);
3794 tree best = t;
3795 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3797 while (CONVERT_EXPR_P (t)
3798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3800 int thisalign;
3802 t = TREE_OPERAND (t, 0);
3803 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3804 if (thisalign > bestalign)
3805 best = t, bestalign = thisalign;
3807 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3809 else
3810 return c_alignof (loc, TREE_TYPE (expr));
3812 return fold_convert_loc (loc, size_type_node, t);
3815 /* Handle C and C++ default attributes. */
3817 enum built_in_attribute
3819 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3820 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3821 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3822 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3823 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3824 #include "builtin-attrs.def"
3825 #undef DEF_ATTR_NULL_TREE
3826 #undef DEF_ATTR_INT
3827 #undef DEF_ATTR_STRING
3828 #undef DEF_ATTR_IDENT
3829 #undef DEF_ATTR_TREE_LIST
3830 ATTR_LAST
3833 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3835 static void c_init_attributes (void);
3837 enum c_builtin_type
3839 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3840 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3841 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3842 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3843 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3844 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3845 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3846 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3847 ARG6) NAME,
3848 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3849 ARG6, ARG7) NAME,
3850 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3851 ARG6, ARG7, ARG8) NAME,
3852 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3853 ARG6, ARG7, ARG8, ARG9) NAME,
3854 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3855 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3856 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3857 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3858 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3859 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3860 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3861 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3862 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3863 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3864 NAME,
3865 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3866 ARG6) NAME,
3867 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3868 ARG6, ARG7) NAME,
3869 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3870 #include "builtin-types.def"
3871 #undef DEF_PRIMITIVE_TYPE
3872 #undef DEF_FUNCTION_TYPE_0
3873 #undef DEF_FUNCTION_TYPE_1
3874 #undef DEF_FUNCTION_TYPE_2
3875 #undef DEF_FUNCTION_TYPE_3
3876 #undef DEF_FUNCTION_TYPE_4
3877 #undef DEF_FUNCTION_TYPE_5
3878 #undef DEF_FUNCTION_TYPE_6
3879 #undef DEF_FUNCTION_TYPE_7
3880 #undef DEF_FUNCTION_TYPE_8
3881 #undef DEF_FUNCTION_TYPE_9
3882 #undef DEF_FUNCTION_TYPE_10
3883 #undef DEF_FUNCTION_TYPE_11
3884 #undef DEF_FUNCTION_TYPE_VAR_0
3885 #undef DEF_FUNCTION_TYPE_VAR_1
3886 #undef DEF_FUNCTION_TYPE_VAR_2
3887 #undef DEF_FUNCTION_TYPE_VAR_3
3888 #undef DEF_FUNCTION_TYPE_VAR_4
3889 #undef DEF_FUNCTION_TYPE_VAR_5
3890 #undef DEF_FUNCTION_TYPE_VAR_6
3891 #undef DEF_FUNCTION_TYPE_VAR_7
3892 #undef DEF_POINTER_TYPE
3893 BT_LAST
3896 typedef enum c_builtin_type builtin_type;
3898 /* A temporary array for c_common_nodes_and_builtins. Used in
3899 communication with def_fn_type. */
3900 static tree builtin_types[(int) BT_LAST + 1];
3902 /* A helper function for c_common_nodes_and_builtins. Build function type
3903 for DEF with return type RET and N arguments. If VAR is true, then the
3904 function should be variadic after those N arguments.
3906 Takes special care not to ICE if any of the types involved are
3907 error_mark_node, which indicates that said type is not in fact available
3908 (see builtin_type_for_size). In which case the function type as a whole
3909 should be error_mark_node. */
3911 static void
3912 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3914 tree t;
3915 tree *args = XALLOCAVEC (tree, n);
3916 va_list list;
3917 int i;
3919 va_start (list, n);
3920 for (i = 0; i < n; ++i)
3922 builtin_type a = (builtin_type) va_arg (list, int);
3923 t = builtin_types[a];
3924 if (t == error_mark_node)
3925 goto egress;
3926 args[i] = t;
3929 t = builtin_types[ret];
3930 if (t == error_mark_node)
3931 goto egress;
3932 if (var)
3933 t = build_varargs_function_type_array (t, n, args);
3934 else
3935 t = build_function_type_array (t, n, args);
3937 egress:
3938 builtin_types[def] = t;
3939 va_end (list);
3942 /* Build builtin functions common to both C and C++ language
3943 frontends. */
3945 static void
3946 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3948 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3949 builtin_types[ENUM] = VALUE;
3950 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3951 def_fn_type (ENUM, RETURN, 0, 0);
3952 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3953 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3954 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3955 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3956 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3957 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3958 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3959 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3960 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3961 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3962 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3963 ARG6) \
3964 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3965 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3966 ARG6, ARG7) \
3967 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3968 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3969 ARG6, ARG7, ARG8) \
3970 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3971 ARG7, ARG8);
3972 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3973 ARG6, ARG7, ARG8, ARG9) \
3974 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3975 ARG7, ARG8, ARG9);
3976 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3977 ARG6, ARG7, ARG8, ARG9, ARG10) \
3978 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3979 ARG7, ARG8, ARG9, ARG10);
3980 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3981 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3982 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3983 ARG7, ARG8, ARG9, ARG10, ARG11);
3984 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3985 def_fn_type (ENUM, RETURN, 1, 0);
3986 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3987 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3988 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3989 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3990 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3991 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3992 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3993 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3994 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3995 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3996 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3997 ARG6) \
3998 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3999 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4000 ARG6, ARG7) \
4001 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4002 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4003 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4005 #include "builtin-types.def"
4007 #undef DEF_PRIMITIVE_TYPE
4008 #undef DEF_FUNCTION_TYPE_0
4009 #undef DEF_FUNCTION_TYPE_1
4010 #undef DEF_FUNCTION_TYPE_2
4011 #undef DEF_FUNCTION_TYPE_3
4012 #undef DEF_FUNCTION_TYPE_4
4013 #undef DEF_FUNCTION_TYPE_5
4014 #undef DEF_FUNCTION_TYPE_6
4015 #undef DEF_FUNCTION_TYPE_7
4016 #undef DEF_FUNCTION_TYPE_8
4017 #undef DEF_FUNCTION_TYPE_9
4018 #undef DEF_FUNCTION_TYPE_10
4019 #undef DEF_FUNCTION_TYPE_11
4020 #undef DEF_FUNCTION_TYPE_VAR_0
4021 #undef DEF_FUNCTION_TYPE_VAR_1
4022 #undef DEF_FUNCTION_TYPE_VAR_2
4023 #undef DEF_FUNCTION_TYPE_VAR_3
4024 #undef DEF_FUNCTION_TYPE_VAR_4
4025 #undef DEF_FUNCTION_TYPE_VAR_5
4026 #undef DEF_FUNCTION_TYPE_VAR_6
4027 #undef DEF_FUNCTION_TYPE_VAR_7
4028 #undef DEF_POINTER_TYPE
4029 builtin_types[(int) BT_LAST] = NULL_TREE;
4031 c_init_attributes ();
4033 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4034 NONANSI_P, ATTRS, IMPLICIT, COND) \
4035 if (NAME && COND) \
4036 def_builtin_1 (ENUM, NAME, CLASS, \
4037 builtin_types[(int) TYPE], \
4038 builtin_types[(int) LIBTYPE], \
4039 BOTH_P, FALLBACK_P, NONANSI_P, \
4040 built_in_attributes[(int) ATTRS], IMPLICIT);
4041 #include "builtins.def"
4043 targetm.init_builtins ();
4045 build_common_builtin_nodes ();
4047 if (flag_cilkplus)
4048 cilk_init_builtins ();
4051 /* Like get_identifier, but avoid warnings about null arguments when
4052 the argument may be NULL for targets where GCC lacks stdint.h type
4053 information. */
4055 static inline tree
4056 c_get_ident (const char *id)
4058 return get_identifier (id);
4061 /* Build tree nodes and builtin functions common to both C and C++ language
4062 frontends. */
4064 void
4065 c_common_nodes_and_builtins (void)
4067 int char16_type_size;
4068 int char32_type_size;
4069 int wchar_type_size;
4070 tree array_domain_type;
4071 tree va_list_ref_type_node;
4072 tree va_list_arg_type_node;
4073 int i;
4075 build_common_tree_nodes (flag_signed_char);
4077 /* Define `int' and `char' first so that dbx will output them first. */
4078 record_builtin_type (RID_INT, NULL, integer_type_node);
4079 record_builtin_type (RID_CHAR, "char", char_type_node);
4081 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4082 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4083 but not C. Are the conditionals here needed? */
4084 if (c_dialect_cxx ())
4085 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4086 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4087 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4088 record_builtin_type (RID_MAX, "long unsigned int",
4089 long_unsigned_type_node);
4091 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4093 char name[25];
4095 sprintf (name, "__int%d", int_n_data[i].bitsize);
4096 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4097 int_n_trees[i].signed_type);
4098 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4099 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4102 if (c_dialect_cxx ())
4103 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4104 record_builtin_type (RID_MAX, "long long int",
4105 long_long_integer_type_node);
4106 record_builtin_type (RID_MAX, "long long unsigned int",
4107 long_long_unsigned_type_node);
4108 if (c_dialect_cxx ())
4109 record_builtin_type (RID_MAX, "long long unsigned",
4110 long_long_unsigned_type_node);
4111 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4112 record_builtin_type (RID_MAX, "short unsigned int",
4113 short_unsigned_type_node);
4114 if (c_dialect_cxx ())
4115 record_builtin_type (RID_MAX, "unsigned short",
4116 short_unsigned_type_node);
4118 /* Define both `signed char' and `unsigned char'. */
4119 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4120 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4122 /* These are types that c_common_type_for_size and
4123 c_common_type_for_mode use. */
4124 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4125 TYPE_DECL, NULL_TREE,
4126 intQI_type_node));
4127 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4128 TYPE_DECL, NULL_TREE,
4129 intHI_type_node));
4130 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4131 TYPE_DECL, NULL_TREE,
4132 intSI_type_node));
4133 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4134 TYPE_DECL, NULL_TREE,
4135 intDI_type_node));
4136 #if HOST_BITS_PER_WIDE_INT >= 64
4137 /* Note that this is different than the __int128 type that's part of
4138 the generic __intN support. */
4139 if (targetm.scalar_mode_supported_p (TImode))
4140 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4141 TYPE_DECL,
4142 get_identifier ("__int128_t"),
4143 intTI_type_node));
4144 #endif
4145 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4146 TYPE_DECL, NULL_TREE,
4147 unsigned_intQI_type_node));
4148 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4149 TYPE_DECL, NULL_TREE,
4150 unsigned_intHI_type_node));
4151 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4152 TYPE_DECL, NULL_TREE,
4153 unsigned_intSI_type_node));
4154 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4155 TYPE_DECL, NULL_TREE,
4156 unsigned_intDI_type_node));
4157 #if HOST_BITS_PER_WIDE_INT >= 64
4158 if (targetm.scalar_mode_supported_p (TImode))
4159 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4160 TYPE_DECL,
4161 get_identifier ("__uint128_t"),
4162 unsigned_intTI_type_node));
4163 #endif
4165 /* Create the widest literal types. */
4166 widest_integer_literal_type_node
4167 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4168 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4169 TYPE_DECL, NULL_TREE,
4170 widest_integer_literal_type_node));
4172 widest_unsigned_literal_type_node
4173 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4174 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4175 TYPE_DECL, NULL_TREE,
4176 widest_unsigned_literal_type_node));
4178 signed_size_type_node = c_common_signed_type (size_type_node);
4180 pid_type_node =
4181 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4183 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4184 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4185 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4187 if (!c_dialect_cxx ())
4188 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4189 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4190 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4191 FLOATN_NX_TYPE_NODE (i));
4193 /* Only supported decimal floating point extension if the target
4194 actually supports underlying modes. */
4195 if (targetm.scalar_mode_supported_p (SDmode)
4196 && targetm.scalar_mode_supported_p (DDmode)
4197 && targetm.scalar_mode_supported_p (TDmode))
4199 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4200 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4201 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4204 if (targetm.fixed_point_supported_p ())
4206 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4207 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4208 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4209 record_builtin_type (RID_MAX, "long long _Fract",
4210 long_long_fract_type_node);
4211 record_builtin_type (RID_MAX, "unsigned short _Fract",
4212 unsigned_short_fract_type_node);
4213 record_builtin_type (RID_MAX, "unsigned _Fract",
4214 unsigned_fract_type_node);
4215 record_builtin_type (RID_MAX, "unsigned long _Fract",
4216 unsigned_long_fract_type_node);
4217 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4218 unsigned_long_long_fract_type_node);
4219 record_builtin_type (RID_MAX, "_Sat short _Fract",
4220 sat_short_fract_type_node);
4221 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4222 record_builtin_type (RID_MAX, "_Sat long _Fract",
4223 sat_long_fract_type_node);
4224 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4225 sat_long_long_fract_type_node);
4226 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4227 sat_unsigned_short_fract_type_node);
4228 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4229 sat_unsigned_fract_type_node);
4230 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4231 sat_unsigned_long_fract_type_node);
4232 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4233 sat_unsigned_long_long_fract_type_node);
4234 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4235 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4236 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4237 record_builtin_type (RID_MAX, "long long _Accum",
4238 long_long_accum_type_node);
4239 record_builtin_type (RID_MAX, "unsigned short _Accum",
4240 unsigned_short_accum_type_node);
4241 record_builtin_type (RID_MAX, "unsigned _Accum",
4242 unsigned_accum_type_node);
4243 record_builtin_type (RID_MAX, "unsigned long _Accum",
4244 unsigned_long_accum_type_node);
4245 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4246 unsigned_long_long_accum_type_node);
4247 record_builtin_type (RID_MAX, "_Sat short _Accum",
4248 sat_short_accum_type_node);
4249 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4250 record_builtin_type (RID_MAX, "_Sat long _Accum",
4251 sat_long_accum_type_node);
4252 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4253 sat_long_long_accum_type_node);
4254 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4255 sat_unsigned_short_accum_type_node);
4256 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4257 sat_unsigned_accum_type_node);
4258 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4259 sat_unsigned_long_accum_type_node);
4260 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4261 sat_unsigned_long_long_accum_type_node);
4265 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4266 TYPE_DECL,
4267 get_identifier ("complex int"),
4268 complex_integer_type_node));
4269 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4270 TYPE_DECL,
4271 get_identifier ("complex float"),
4272 complex_float_type_node));
4273 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4274 TYPE_DECL,
4275 get_identifier ("complex double"),
4276 complex_double_type_node));
4277 lang_hooks.decls.pushdecl
4278 (build_decl (UNKNOWN_LOCATION,
4279 TYPE_DECL, get_identifier ("complex long double"),
4280 complex_long_double_type_node));
4282 if (!c_dialect_cxx ())
4283 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4284 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4286 char buf[30];
4287 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4288 floatn_nx_types[i].extended ? "x" : "");
4289 lang_hooks.decls.pushdecl
4290 (build_decl (UNKNOWN_LOCATION,
4291 TYPE_DECL,
4292 get_identifier (buf),
4293 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4296 if (c_dialect_cxx ())
4298 /* For C++, make fileptr_type_node a distinct void * type until
4299 FILE type is defined. */
4300 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4301 /* Likewise for const struct tm*. */
4302 const_tm_ptr_type_node = build_variant_type_copy (const_ptr_type_node);
4305 record_builtin_type (RID_VOID, NULL, void_type_node);
4307 /* Set the TYPE_NAME for any variants that were built before
4308 record_builtin_type gave names to the built-in types. */
4310 tree void_name = TYPE_NAME (void_type_node);
4311 TYPE_NAME (void_type_node) = NULL_TREE;
4312 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4313 = void_name;
4314 TYPE_NAME (void_type_node) = void_name;
4317 void_list_node = build_void_list_node ();
4319 /* Make a type to be the domain of a few array types
4320 whose domains don't really matter.
4321 200 is small enough that it always fits in size_t
4322 and large enough that it can hold most function names for the
4323 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4324 array_domain_type = build_index_type (size_int (200));
4326 /* Make a type for arrays of characters.
4327 With luck nothing will ever really depend on the length of this
4328 array type. */
4329 char_array_type_node
4330 = build_array_type (char_type_node, array_domain_type);
4332 string_type_node = build_pointer_type (char_type_node);
4333 const_string_type_node
4334 = build_pointer_type (build_qualified_type
4335 (char_type_node, TYPE_QUAL_CONST));
4337 /* This is special for C++ so functions can be overloaded. */
4338 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4339 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4340 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4341 underlying_wchar_type_node = wchar_type_node;
4342 if (c_dialect_cxx ())
4344 if (TYPE_UNSIGNED (wchar_type_node))
4345 wchar_type_node = make_unsigned_type (wchar_type_size);
4346 else
4347 wchar_type_node = make_signed_type (wchar_type_size);
4348 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4351 /* This is for wide string constants. */
4352 wchar_array_type_node
4353 = build_array_type (wchar_type_node, array_domain_type);
4355 /* Define 'char16_t'. */
4356 char16_type_node = get_identifier (CHAR16_TYPE);
4357 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4358 char16_type_size = TYPE_PRECISION (char16_type_node);
4359 if (c_dialect_cxx ())
4361 char16_type_node = make_unsigned_type (char16_type_size);
4363 if (cxx_dialect >= cxx11)
4364 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4367 /* This is for UTF-16 string constants. */
4368 char16_array_type_node
4369 = build_array_type (char16_type_node, array_domain_type);
4371 /* Define 'char32_t'. */
4372 char32_type_node = get_identifier (CHAR32_TYPE);
4373 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4374 char32_type_size = TYPE_PRECISION (char32_type_node);
4375 if (c_dialect_cxx ())
4377 char32_type_node = make_unsigned_type (char32_type_size);
4379 if (cxx_dialect >= cxx11)
4380 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4383 /* This is for UTF-32 string constants. */
4384 char32_array_type_node
4385 = build_array_type (char32_type_node, array_domain_type);
4387 wint_type_node =
4388 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4390 intmax_type_node =
4391 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4392 uintmax_type_node =
4393 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4395 if (SIG_ATOMIC_TYPE)
4396 sig_atomic_type_node =
4397 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4398 if (INT8_TYPE)
4399 int8_type_node =
4400 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4401 if (INT16_TYPE)
4402 int16_type_node =
4403 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4404 if (INT32_TYPE)
4405 int32_type_node =
4406 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4407 if (INT64_TYPE)
4408 int64_type_node =
4409 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4410 if (UINT8_TYPE)
4411 uint8_type_node =
4412 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4413 if (UINT16_TYPE)
4414 c_uint16_type_node = uint16_type_node =
4415 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4416 if (UINT32_TYPE)
4417 c_uint32_type_node = uint32_type_node =
4418 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4419 if (UINT64_TYPE)
4420 c_uint64_type_node = uint64_type_node =
4421 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4422 if (INT_LEAST8_TYPE)
4423 int_least8_type_node =
4424 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4425 if (INT_LEAST16_TYPE)
4426 int_least16_type_node =
4427 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4428 if (INT_LEAST32_TYPE)
4429 int_least32_type_node =
4430 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4431 if (INT_LEAST64_TYPE)
4432 int_least64_type_node =
4433 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4434 if (UINT_LEAST8_TYPE)
4435 uint_least8_type_node =
4436 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4437 if (UINT_LEAST16_TYPE)
4438 uint_least16_type_node =
4439 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4440 if (UINT_LEAST32_TYPE)
4441 uint_least32_type_node =
4442 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4443 if (UINT_LEAST64_TYPE)
4444 uint_least64_type_node =
4445 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4446 if (INT_FAST8_TYPE)
4447 int_fast8_type_node =
4448 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4449 if (INT_FAST16_TYPE)
4450 int_fast16_type_node =
4451 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4452 if (INT_FAST32_TYPE)
4453 int_fast32_type_node =
4454 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4455 if (INT_FAST64_TYPE)
4456 int_fast64_type_node =
4457 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4458 if (UINT_FAST8_TYPE)
4459 uint_fast8_type_node =
4460 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4461 if (UINT_FAST16_TYPE)
4462 uint_fast16_type_node =
4463 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4464 if (UINT_FAST32_TYPE)
4465 uint_fast32_type_node =
4466 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4467 if (UINT_FAST64_TYPE)
4468 uint_fast64_type_node =
4469 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4470 if (INTPTR_TYPE)
4471 intptr_type_node =
4472 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4473 if (UINTPTR_TYPE)
4474 uintptr_type_node =
4475 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4477 default_function_type
4478 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4479 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4481 lang_hooks.decls.pushdecl
4482 (build_decl (UNKNOWN_LOCATION,
4483 TYPE_DECL, get_identifier ("__builtin_va_list"),
4484 va_list_type_node));
4485 if (targetm.enum_va_list_p)
4487 int l;
4488 const char *pname;
4489 tree ptype;
4491 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4493 lang_hooks.decls.pushdecl
4494 (build_decl (UNKNOWN_LOCATION,
4495 TYPE_DECL, get_identifier (pname),
4496 ptype));
4501 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4503 va_list_arg_type_node = va_list_ref_type_node =
4504 build_pointer_type (TREE_TYPE (va_list_type_node));
4506 else
4508 va_list_arg_type_node = va_list_type_node;
4509 va_list_ref_type_node = build_reference_type (va_list_type_node);
4512 if (!flag_preprocess_only)
4513 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4515 main_identifier_node = get_identifier ("main");
4517 /* Create the built-in __null node. It is important that this is
4518 not shared. */
4519 null_node = make_int_cst (1, 1);
4520 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4522 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4523 memset (builtin_types, 0, sizeof (builtin_types));
4526 /* The number of named compound-literals generated thus far. */
4527 static GTY(()) int compound_literal_number;
4529 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4531 void
4532 set_compound_literal_name (tree decl)
4534 char *name;
4535 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4536 compound_literal_number);
4537 compound_literal_number++;
4538 DECL_NAME (decl) = get_identifier (name);
4541 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4542 TYPE and operand OP. */
4544 static tree
4545 build_va_arg_1 (location_t loc, tree type, tree op)
4547 tree expr = build1 (VA_ARG_EXPR, type, op);
4548 SET_EXPR_LOCATION (expr, loc);
4549 return expr;
4552 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4553 va_arg (EXPR, TYPE) at source location LOC. */
4555 tree
4556 build_va_arg (location_t loc, tree expr, tree type)
4558 tree va_type = TREE_TYPE (expr);
4559 tree canon_va_type = (va_type == error_mark_node
4560 ? error_mark_node
4561 : targetm.canonical_va_list_type (va_type));
4563 if (va_type == error_mark_node
4564 || canon_va_type == NULL_TREE)
4566 if (canon_va_type == NULL_TREE)
4567 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4569 /* Let's handle things neutrallly, if expr:
4570 - has undeclared type, or
4571 - is not an va_list type. */
4572 return build_va_arg_1 (loc, type, error_mark_node);
4575 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4577 /* Case 1: Not an array type. */
4579 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4580 type. */
4581 mark_addressable (expr);
4582 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4584 return build_va_arg_1 (loc, type, expr);
4587 /* Case 2: Array type.
4589 Background:
4591 For contrast, let's start with the simple case (case 1). If
4592 canon_va_type is not an array type, but say a char *, then when
4593 passing-by-value a va_list, the type of the va_list param decl is
4594 the same as for another va_list decl (all ap's are char *):
4596 f2_1 (char * ap)
4597 D.1815 = VA_ARG (&ap, 0B, 1);
4598 return D.1815;
4600 f2 (int i)
4601 char * ap.0;
4602 char * ap;
4603 __builtin_va_start (&ap, 0);
4604 ap.0 = ap;
4605 res = f2_1 (ap.0);
4606 __builtin_va_end (&ap);
4607 D.1812 = res;
4608 return D.1812;
4610 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4611 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4612 the same as for another va_list decl (case 2a, struct ap[1]).
4614 f2_1 (struct * ap)
4615 D.1844 = VA_ARG (ap, 0B, 0);
4616 return D.1844;
4618 f2 (int i)
4619 struct ap[1];
4620 __builtin_va_start (&ap, 0);
4621 res = f2_1 (&ap);
4622 __builtin_va_end (&ap);
4623 D.1841 = res;
4624 return D.1841;
4626 Case 2b is different because:
4627 - on the callee side, the parm decl has declared type va_list, but
4628 grokdeclarator changes the type of the parm decl to a pointer to the
4629 array elem type.
4630 - on the caller side, the pass-by-value uses &ap.
4632 We unify these two cases (case 2a: va_list is array type,
4633 case 2b: va_list is pointer to array elem type), by adding '&' for the
4634 array type case, such that we have a pointer to array elem in both
4635 cases. */
4637 if (TREE_CODE (va_type) == ARRAY_TYPE)
4639 /* Case 2a: va_list is array type. */
4641 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4642 elem type. */
4643 mark_addressable (expr);
4644 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4645 expr);
4647 /* Verify that &ap is still recognized as having va_list type. */
4648 tree canon_expr_type
4649 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4650 gcc_assert (canon_expr_type != NULL_TREE);
4652 else
4654 /* Case 2b: va_list is pointer to array elem type. */
4655 gcc_assert (POINTER_TYPE_P (va_type));
4657 /* Comparison as in std_canonical_va_list_type. */
4658 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4659 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4661 /* Don't take the address. We've already got '&ap'. */
4665 return build_va_arg_1 (loc, type, expr);
4669 /* Linked list of disabled built-in functions. */
4671 struct disabled_builtin
4673 const char *name;
4674 struct disabled_builtin *next;
4676 static disabled_builtin *disabled_builtins = NULL;
4678 static bool builtin_function_disabled_p (const char *);
4680 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4681 begins with "__builtin_", give an error. */
4683 void
4684 disable_builtin_function (const char *name)
4686 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4687 error ("cannot disable built-in function %qs", name);
4688 else
4690 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4691 new_disabled_builtin->name = name;
4692 new_disabled_builtin->next = disabled_builtins;
4693 disabled_builtins = new_disabled_builtin;
4698 /* Return true if the built-in function NAME has been disabled, false
4699 otherwise. */
4701 static bool
4702 builtin_function_disabled_p (const char *name)
4704 disabled_builtin *p;
4705 for (p = disabled_builtins; p != NULL; p = p->next)
4707 if (strcmp (name, p->name) == 0)
4708 return true;
4710 return false;
4714 /* Worker for DEF_BUILTIN.
4715 Possibly define a builtin function with one or two names.
4716 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4717 nonansi_p and flag_no_nonansi_builtin. */
4719 static void
4720 def_builtin_1 (enum built_in_function fncode,
4721 const char *name,
4722 enum built_in_class fnclass,
4723 tree fntype, tree libtype,
4724 bool both_p, bool fallback_p, bool nonansi_p,
4725 tree fnattrs, bool implicit_p)
4727 tree decl;
4728 const char *libname;
4730 if (fntype == error_mark_node)
4731 return;
4733 gcc_assert ((!both_p && !fallback_p)
4734 || !strncmp (name, "__builtin_",
4735 strlen ("__builtin_")));
4737 libname = name + strlen ("__builtin_");
4738 decl = add_builtin_function (name, fntype, fncode, fnclass,
4739 (fallback_p ? libname : NULL),
4740 fnattrs);
4742 set_builtin_decl (fncode, decl, implicit_p);
4744 if (both_p
4745 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4746 && !(nonansi_p && flag_no_nonansi_builtin))
4747 add_builtin_function (libname, libtype, fncode, fnclass,
4748 NULL, fnattrs);
4751 /* Nonzero if the type T promotes to int. This is (nearly) the
4752 integral promotions defined in ISO C99 6.3.1.1/2. */
4754 bool
4755 c_promoting_integer_type_p (const_tree t)
4757 switch (TREE_CODE (t))
4759 case INTEGER_TYPE:
4760 return (TYPE_MAIN_VARIANT (t) == char_type_node
4761 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4762 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4763 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4764 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4765 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4767 case ENUMERAL_TYPE:
4768 /* ??? Technically all enumerations not larger than an int
4769 promote to an int. But this is used along code paths
4770 that only want to notice a size change. */
4771 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4773 case BOOLEAN_TYPE:
4774 return 1;
4776 default:
4777 return 0;
4781 /* Return 1 if PARMS specifies a fixed number of parameters
4782 and none of their types is affected by default promotions. */
4785 self_promoting_args_p (const_tree parms)
4787 const_tree t;
4788 for (t = parms; t; t = TREE_CHAIN (t))
4790 tree type = TREE_VALUE (t);
4792 if (type == error_mark_node)
4793 continue;
4795 if (TREE_CHAIN (t) == 0 && type != void_type_node)
4796 return 0;
4798 if (type == 0)
4799 return 0;
4801 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4802 return 0;
4804 if (c_promoting_integer_type_p (type))
4805 return 0;
4807 return 1;
4810 /* Recursively remove any '*' or '&' operator from TYPE. */
4811 tree
4812 strip_pointer_operator (tree t)
4814 while (POINTER_TYPE_P (t))
4815 t = TREE_TYPE (t);
4816 return t;
4819 /* Recursively remove pointer or array type from TYPE. */
4820 tree
4821 strip_pointer_or_array_types (tree t)
4823 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4824 t = TREE_TYPE (t);
4825 return t;
4828 /* Used to compare case labels. K1 and K2 are actually tree nodes
4829 representing case labels, or NULL_TREE for a `default' label.
4830 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4831 K2, and 0 if K1 and K2 are equal. */
4834 case_compare (splay_tree_key k1, splay_tree_key k2)
4836 /* Consider a NULL key (such as arises with a `default' label) to be
4837 smaller than anything else. */
4838 if (!k1)
4839 return k2 ? -1 : 0;
4840 else if (!k2)
4841 return k1 ? 1 : 0;
4843 return tree_int_cst_compare ((tree) k1, (tree) k2);
4846 /* Process a case label, located at LOC, for the range LOW_VALUE
4847 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4848 then this case label is actually a `default' label. If only
4849 HIGH_VALUE is NULL_TREE, then case label was declared using the
4850 usual C/C++ syntax, rather than the GNU case range extension.
4851 CASES is a tree containing all the case ranges processed so far;
4852 COND is the condition for the switch-statement itself.
4853 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4854 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4855 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4857 tree
4858 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4859 tree low_value, tree high_value, bool *outside_range_p)
4861 tree type;
4862 tree label;
4863 tree case_label;
4864 splay_tree_node node;
4866 /* Create the LABEL_DECL itself. */
4867 label = create_artificial_label (loc);
4869 /* If there was an error processing the switch condition, bail now
4870 before we get more confused. */
4871 if (!cond || cond == error_mark_node)
4872 goto error_out;
4874 if ((low_value && TREE_TYPE (low_value)
4875 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4876 || (high_value && TREE_TYPE (high_value)
4877 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4879 error_at (loc, "pointers are not permitted as case values");
4880 goto error_out;
4883 /* Case ranges are a GNU extension. */
4884 if (high_value)
4885 pedwarn (loc, OPT_Wpedantic,
4886 "range expressions in switch statements are non-standard");
4888 type = TREE_TYPE (cond);
4889 if (low_value)
4891 low_value = check_case_value (loc, low_value);
4892 low_value = convert_and_check (loc, type, low_value);
4893 if (low_value == error_mark_node)
4894 goto error_out;
4896 if (high_value)
4898 high_value = check_case_value (loc, high_value);
4899 high_value = convert_and_check (loc, type, high_value);
4900 if (high_value == error_mark_node)
4901 goto error_out;
4904 if (low_value && high_value)
4906 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4907 really a case range, even though it was written that way.
4908 Remove the HIGH_VALUE to simplify later processing. */
4909 if (tree_int_cst_equal (low_value, high_value))
4910 high_value = NULL_TREE;
4911 else if (!tree_int_cst_lt (low_value, high_value))
4912 warning_at (loc, 0, "empty range specified");
4915 /* See if the case is in range of the type of the original testing
4916 expression. If both low_value and high_value are out of range,
4917 don't insert the case label and return NULL_TREE. */
4918 if (low_value
4919 && !check_case_bounds (loc, type, orig_type,
4920 &low_value, high_value ? &high_value : NULL,
4921 outside_range_p))
4922 return NULL_TREE;
4924 /* Look up the LOW_VALUE in the table of case labels we already
4925 have. */
4926 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4927 /* If there was not an exact match, check for overlapping ranges.
4928 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4929 that's a `default' label and the only overlap is an exact match. */
4930 if (!node && (low_value || high_value))
4932 splay_tree_node low_bound;
4933 splay_tree_node high_bound;
4935 /* Even though there wasn't an exact match, there might be an
4936 overlap between this case range and another case range.
4937 Since we've (inductively) not allowed any overlapping case
4938 ranges, we simply need to find the greatest low case label
4939 that is smaller that LOW_VALUE, and the smallest low case
4940 label that is greater than LOW_VALUE. If there is an overlap
4941 it will occur in one of these two ranges. */
4942 low_bound = splay_tree_predecessor (cases,
4943 (splay_tree_key) low_value);
4944 high_bound = splay_tree_successor (cases,
4945 (splay_tree_key) low_value);
4947 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4948 the LOW_VALUE, so there is no need to check unless the
4949 LOW_BOUND is in fact itself a case range. */
4950 if (low_bound
4951 && CASE_HIGH ((tree) low_bound->value)
4952 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4953 low_value) >= 0)
4954 node = low_bound;
4955 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4956 range is bigger than the low end of the current range, so we
4957 are only interested if the current range is a real range, and
4958 not an ordinary case label. */
4959 else if (high_bound
4960 && high_value
4961 && (tree_int_cst_compare ((tree) high_bound->key,
4962 high_value)
4963 <= 0))
4964 node = high_bound;
4966 /* If there was an overlap, issue an error. */
4967 if (node)
4969 tree duplicate = CASE_LABEL ((tree) node->value);
4971 if (high_value)
4973 error_at (loc, "duplicate (or overlapping) case value");
4974 inform (DECL_SOURCE_LOCATION (duplicate),
4975 "this is the first entry overlapping that value");
4977 else if (low_value)
4979 error_at (loc, "duplicate case value") ;
4980 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4982 else
4984 error_at (loc, "multiple default labels in one switch");
4985 inform (DECL_SOURCE_LOCATION (duplicate),
4986 "this is the first default label");
4988 goto error_out;
4991 /* Add a CASE_LABEL to the statement-tree. */
4992 case_label = add_stmt (build_case_label (low_value, high_value, label));
4993 /* Register this case label in the splay tree. */
4994 splay_tree_insert (cases,
4995 (splay_tree_key) low_value,
4996 (splay_tree_value) case_label);
4998 return case_label;
5000 error_out:
5001 /* Add a label so that the back-end doesn't think that the beginning of
5002 the switch is unreachable. Note that we do not add a case label, as
5003 that just leads to duplicates and thence to failure later on. */
5004 if (!cases->root)
5006 tree t = create_artificial_label (loc);
5007 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5009 return error_mark_node;
5012 /* Finish an expression taking the address of LABEL (an
5013 IDENTIFIER_NODE). Returns an expression for the address.
5015 LOC is the location for the expression returned. */
5017 tree
5018 finish_label_address_expr (tree label, location_t loc)
5020 tree result;
5022 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5024 if (label == error_mark_node)
5025 return error_mark_node;
5027 label = lookup_label (label);
5028 if (label == NULL_TREE)
5029 result = null_pointer_node;
5030 else
5032 TREE_USED (label) = 1;
5033 result = build1 (ADDR_EXPR, ptr_type_node, label);
5034 /* The current function is not necessarily uninlinable.
5035 Computed gotos are incompatible with inlining, but the value
5036 here could be used only in a diagnostic, for example. */
5037 protected_set_expr_location (result, loc);
5040 return result;
5044 /* Given a boolean expression ARG, return a tree representing an increment
5045 or decrement (as indicated by CODE) of ARG. The front end must check for
5046 invalid cases (e.g., decrement in C++). */
5047 tree
5048 boolean_increment (enum tree_code code, tree arg)
5050 tree val;
5051 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5053 arg = stabilize_reference (arg);
5054 switch (code)
5056 case PREINCREMENT_EXPR:
5057 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5058 break;
5059 case POSTINCREMENT_EXPR:
5060 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5061 arg = save_expr (arg);
5062 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5063 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5064 break;
5065 case PREDECREMENT_EXPR:
5066 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5067 invert_truthvalue_loc (input_location, arg));
5068 break;
5069 case POSTDECREMENT_EXPR:
5070 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5071 invert_truthvalue_loc (input_location, arg));
5072 arg = save_expr (arg);
5073 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5074 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5075 break;
5076 default:
5077 gcc_unreachable ();
5079 TREE_SIDE_EFFECTS (val) = 1;
5080 return val;
5083 /* Built-in macros for stddef.h and stdint.h, that require macros
5084 defined in this file. */
5085 void
5086 c_stddef_cpp_builtins(void)
5088 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5089 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5090 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5091 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5092 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5093 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5094 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5095 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5096 if (SIG_ATOMIC_TYPE)
5097 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5098 if (INT8_TYPE)
5099 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5100 if (INT16_TYPE)
5101 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5102 if (INT32_TYPE)
5103 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5104 if (INT64_TYPE)
5105 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5106 if (UINT8_TYPE)
5107 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5108 if (UINT16_TYPE)
5109 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5110 if (UINT32_TYPE)
5111 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5112 if (UINT64_TYPE)
5113 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5114 if (INT_LEAST8_TYPE)
5115 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5116 if (INT_LEAST16_TYPE)
5117 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5118 if (INT_LEAST32_TYPE)
5119 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5120 if (INT_LEAST64_TYPE)
5121 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5122 if (UINT_LEAST8_TYPE)
5123 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5124 if (UINT_LEAST16_TYPE)
5125 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5126 if (UINT_LEAST32_TYPE)
5127 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5128 if (UINT_LEAST64_TYPE)
5129 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5130 if (INT_FAST8_TYPE)
5131 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5132 if (INT_FAST16_TYPE)
5133 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5134 if (INT_FAST32_TYPE)
5135 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5136 if (INT_FAST64_TYPE)
5137 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5138 if (UINT_FAST8_TYPE)
5139 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5140 if (UINT_FAST16_TYPE)
5141 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5142 if (UINT_FAST32_TYPE)
5143 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5144 if (UINT_FAST64_TYPE)
5145 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5146 if (INTPTR_TYPE)
5147 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5148 if (UINTPTR_TYPE)
5149 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5152 static void
5153 c_init_attributes (void)
5155 /* Fill in the built_in_attributes array. */
5156 #define DEF_ATTR_NULL_TREE(ENUM) \
5157 built_in_attributes[(int) ENUM] = NULL_TREE;
5158 #define DEF_ATTR_INT(ENUM, VALUE) \
5159 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5160 #define DEF_ATTR_STRING(ENUM, VALUE) \
5161 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5162 #define DEF_ATTR_IDENT(ENUM, STRING) \
5163 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5164 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5165 built_in_attributes[(int) ENUM] \
5166 = tree_cons (built_in_attributes[(int) PURPOSE], \
5167 built_in_attributes[(int) VALUE], \
5168 built_in_attributes[(int) CHAIN]);
5169 #include "builtin-attrs.def"
5170 #undef DEF_ATTR_NULL_TREE
5171 #undef DEF_ATTR_INT
5172 #undef DEF_ATTR_IDENT
5173 #undef DEF_ATTR_TREE_LIST
5176 /* Check whether ALIGN is a valid user-specified alignment. If so,
5177 return its base-2 log; if not, output an error and return -1. If
5178 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
5179 no error. */
5181 check_user_alignment (const_tree align, bool allow_zero)
5183 int i;
5185 if (error_operand_p (align))
5186 return -1;
5187 if (TREE_CODE (align) != INTEGER_CST
5188 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5190 error ("requested alignment is not an integer constant");
5191 return -1;
5193 else if (allow_zero && integer_zerop (align))
5194 return -1;
5195 else if (tree_int_cst_sgn (align) == -1
5196 || (i = tree_log2 (align)) == -1)
5198 error ("requested alignment is not a positive power of 2");
5199 return -1;
5201 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5203 error ("requested alignment is too large");
5204 return -1;
5206 return i;
5209 /* Determine the ELF symbol visibility for DECL, which is either a
5210 variable or a function. It is an error to use this function if a
5211 definition of DECL is not available in this translation unit.
5212 Returns true if the final visibility has been determined by this
5213 function; false if the caller is free to make additional
5214 modifications. */
5216 bool
5217 c_determine_visibility (tree decl)
5219 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5221 /* If the user explicitly specified the visibility with an
5222 attribute, honor that. DECL_VISIBILITY will have been set during
5223 the processing of the attribute. We check for an explicit
5224 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5225 to distinguish the use of an attribute from the use of a "#pragma
5226 GCC visibility push(...)"; in the latter case we still want other
5227 considerations to be able to overrule the #pragma. */
5228 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5229 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5230 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5231 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5232 return true;
5234 /* Set default visibility to whatever the user supplied with
5235 visibility_specified depending on #pragma GCC visibility. */
5236 if (!DECL_VISIBILITY_SPECIFIED (decl))
5238 if (visibility_options.inpragma
5239 || DECL_VISIBILITY (decl) != default_visibility)
5241 DECL_VISIBILITY (decl) = default_visibility;
5242 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5243 /* If visibility changed and DECL already has DECL_RTL, ensure
5244 symbol flags are updated. */
5245 if (((VAR_P (decl) && TREE_STATIC (decl))
5246 || TREE_CODE (decl) == FUNCTION_DECL)
5247 && DECL_RTL_SET_P (decl))
5248 make_decl_rtl (decl);
5251 return false;
5254 /* Data to communicate through check_function_arguments_recurse between
5255 check_function_nonnull and check_nonnull_arg. */
5257 struct nonnull_arg_ctx
5259 location_t loc;
5260 bool warned_p;
5263 /* Check the argument list of a function call for null in argument slots
5264 that are marked as requiring a non-null pointer argument. The NARGS
5265 arguments are passed in the array ARGARRAY. Return true if we have
5266 warned. */
5268 static bool
5269 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5271 tree a;
5272 int i;
5274 attrs = lookup_attribute ("nonnull", attrs);
5275 if (attrs == NULL_TREE)
5276 return false;
5278 a = attrs;
5279 /* See if any of the nonnull attributes has no arguments. If so,
5280 then every pointer argument is checked (in which case the check
5281 for pointer type is done in check_nonnull_arg). */
5282 if (TREE_VALUE (a) != NULL_TREE)
5284 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5285 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5287 struct nonnull_arg_ctx ctx = { loc, false };
5288 if (a != NULL_TREE)
5289 for (i = 0; i < nargs; i++)
5290 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5291 i + 1);
5292 else
5294 /* Walk the argument list. If we encounter an argument number we
5295 should check for non-null, do it. */
5296 for (i = 0; i < nargs; i++)
5298 for (a = attrs; ; a = TREE_CHAIN (a))
5300 a = lookup_attribute ("nonnull", a);
5301 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5302 break;
5305 if (a != NULL_TREE)
5306 check_function_arguments_recurse (check_nonnull_arg, &ctx,
5307 argarray[i], i + 1);
5310 return ctx.warned_p;
5313 /* Check that the Nth argument of a function call (counting backwards
5314 from the end) is a (pointer)0. The NARGS arguments are passed in the
5315 array ARGARRAY. */
5317 static void
5318 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5320 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5322 if (attr)
5324 int len = 0;
5325 int pos = 0;
5326 tree sentinel;
5327 function_args_iterator iter;
5328 tree t;
5330 /* Skip over the named arguments. */
5331 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5333 if (len == nargs)
5334 break;
5335 len++;
5338 if (TREE_VALUE (attr))
5340 tree p = TREE_VALUE (TREE_VALUE (attr));
5341 pos = TREE_INT_CST_LOW (p);
5344 /* The sentinel must be one of the varargs, i.e.
5345 in position >= the number of fixed arguments. */
5346 if ((nargs - 1 - pos) < len)
5348 warning (OPT_Wformat_,
5349 "not enough variable arguments to fit a sentinel");
5350 return;
5353 /* Validate the sentinel. */
5354 sentinel = argarray[nargs - 1 - pos];
5355 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5356 || !integer_zerop (sentinel))
5357 /* Although __null (in C++) is only an integer we allow it
5358 nevertheless, as we are guaranteed that it's exactly
5359 as wide as a pointer, and we don't want to force
5360 users to cast the NULL they have written there.
5361 We warn with -Wstrict-null-sentinel, though. */
5362 && (warn_strict_null_sentinel || null_node != sentinel))
5363 warning (OPT_Wformat_, "missing sentinel in function call");
5367 /* Helper for check_function_nonnull; given a list of operands which
5368 must be non-null in ARGS, determine if operand PARAM_NUM should be
5369 checked. */
5371 static bool
5372 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5374 unsigned HOST_WIDE_INT arg_num = 0;
5376 for (; args; args = TREE_CHAIN (args))
5378 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5380 gcc_assert (found);
5382 if (arg_num == param_num)
5383 return true;
5385 return false;
5388 /* Check that the function argument PARAM (which is operand number
5389 PARAM_NUM) is non-null. This is called by check_function_nonnull
5390 via check_function_arguments_recurse. */
5392 static void
5393 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5395 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5397 /* Just skip checking the argument if it's not a pointer. This can
5398 happen if the "nonnull" attribute was given without an operand
5399 list (which means to check every pointer argument). */
5401 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5402 return;
5404 /* When not optimizing diagnose the simple cases of null arguments.
5405 When optimization is enabled defer the checking until expansion
5406 when more cases can be detected. */
5407 if (integer_zerop (param))
5409 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5410 "required (argument %lu)", (unsigned long) param_num);
5411 pctx->warned_p = true;
5415 /* Helper for nonnull attribute handling; fetch the operand number
5416 from the attribute argument list. */
5418 bool
5419 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5421 /* Verify the arg number is a small constant. */
5422 if (tree_fits_uhwi_p (arg_num_expr))
5424 *valp = TREE_INT_CST_LOW (arg_num_expr);
5425 return true;
5427 else
5428 return false;
5431 /* Arguments being collected for optimization. */
5432 typedef const char *const_char_p; /* For DEF_VEC_P. */
5433 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5436 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5437 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5438 false for #pragma GCC optimize. */
5440 bool
5441 parse_optimize_options (tree args, bool attr_p)
5443 bool ret = true;
5444 unsigned opt_argc;
5445 unsigned i;
5446 const char **opt_argv;
5447 struct cl_decoded_option *decoded_options;
5448 unsigned int decoded_options_count;
5449 tree ap;
5451 /* Build up argv vector. Just in case the string is stored away, use garbage
5452 collected strings. */
5453 vec_safe_truncate (optimize_args, 0);
5454 vec_safe_push (optimize_args, (const char *) NULL);
5456 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5458 tree value = TREE_VALUE (ap);
5460 if (TREE_CODE (value) == INTEGER_CST)
5462 char buffer[20];
5463 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5464 vec_safe_push (optimize_args, ggc_strdup (buffer));
5467 else if (TREE_CODE (value) == STRING_CST)
5469 /* Split string into multiple substrings. */
5470 size_t len = TREE_STRING_LENGTH (value);
5471 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5472 char *end = p + len;
5473 char *comma;
5474 char *next_p = p;
5476 while (next_p != NULL)
5478 size_t len2;
5479 char *q, *r;
5481 p = next_p;
5482 comma = strchr (p, ',');
5483 if (comma)
5485 len2 = comma - p;
5486 *comma = '\0';
5487 next_p = comma+1;
5489 else
5491 len2 = end - p;
5492 next_p = NULL;
5495 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5497 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5498 options. */
5499 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5501 ret = false;
5502 if (attr_p)
5503 warning (OPT_Wattributes,
5504 "bad option %qs to attribute %<optimize%>", p);
5505 else
5506 warning (OPT_Wpragmas,
5507 "bad option %qs to pragma %<optimize%>", p);
5508 continue;
5511 if (*p != '-')
5513 *r++ = '-';
5515 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5516 itself is -Os, and any other switch begins with a -f. */
5517 if ((*p >= '0' && *p <= '9')
5518 || (p[0] == 's' && p[1] == '\0'))
5519 *r++ = 'O';
5520 else if (*p != 'O')
5521 *r++ = 'f';
5524 memcpy (r, p, len2);
5525 r[len2] = '\0';
5526 vec_safe_push (optimize_args, (const char *) q);
5532 opt_argc = optimize_args->length ();
5533 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5535 for (i = 1; i < opt_argc; i++)
5536 opt_argv[i] = (*optimize_args)[i];
5538 /* Now parse the options. */
5539 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5540 &decoded_options,
5541 &decoded_options_count);
5542 /* Drop non-Optimization options. */
5543 unsigned j = 1;
5544 for (i = 1; i < decoded_options_count; ++i)
5546 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5548 ret = false;
5549 if (attr_p)
5550 warning (OPT_Wattributes,
5551 "bad option %qs to attribute %<optimize%>",
5552 decoded_options[i].orig_option_with_args_text);
5553 else
5554 warning (OPT_Wpragmas,
5555 "bad option %qs to pragma %<optimize%>",
5556 decoded_options[i].orig_option_with_args_text);
5557 continue;
5559 if (i != j)
5560 decoded_options[j] = decoded_options[i];
5561 j++;
5563 decoded_options_count = j;
5564 /* And apply them. */
5565 decode_options (&global_options, &global_options_set,
5566 decoded_options, decoded_options_count,
5567 input_location, global_dc);
5569 targetm.override_options_after_change();
5571 optimize_args->truncate (0);
5572 return ret;
5575 /* Check whether ATTR is a valid attribute fallthrough. */
5577 bool
5578 attribute_fallthrough_p (tree attr)
5580 if (attr == error_mark_node)
5581 return false;
5582 tree t = lookup_attribute ("fallthrough", attr);
5583 if (t == NULL_TREE)
5584 return false;
5585 /* This attribute shall appear at most once in each attribute-list. */
5586 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5587 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5588 "times");
5589 /* No attribute-argument-clause shall be present. */
5590 else if (TREE_VALUE (t) != NULL_TREE)
5591 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5592 "a parameter");
5593 /* Warn if other attributes are found. */
5594 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5596 tree name = get_attribute_name (t);
5597 if (!is_attribute_p ("fallthrough", name))
5598 warning (OPT_Wattributes, "%qE attribute ignored", name);
5600 return true;
5604 /* Check for valid arguments being passed to a function with FNTYPE.
5605 There are NARGS arguments in the array ARGARRAY. LOC should be used for
5606 diagnostics. Return true if -Wnonnull warning has been diagnosed. */
5607 bool
5608 check_function_arguments (location_t loc, const_tree fntype, int nargs,
5609 tree *argarray)
5611 bool warned_p = false;
5613 /* Check for null being passed in a pointer argument that must be
5614 non-null. We also need to do this if format checking is enabled. */
5616 if (warn_nonnull)
5617 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5618 nargs, argarray);
5620 /* Check for errors in format strings. */
5622 if (warn_format || warn_suggest_attribute_format)
5623 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
5625 if (warn_format)
5626 check_function_sentinel (fntype, nargs, argarray);
5627 return warned_p;
5630 /* Generic argument checking recursion routine. PARAM is the argument to
5631 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5632 once the argument is resolved. CTX is context for the callback. */
5633 void
5634 check_function_arguments_recurse (void (*callback)
5635 (void *, tree, unsigned HOST_WIDE_INT),
5636 void *ctx, tree param,
5637 unsigned HOST_WIDE_INT param_num)
5639 if (CONVERT_EXPR_P (param)
5640 && (TYPE_PRECISION (TREE_TYPE (param))
5641 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5643 /* Strip coercion. */
5644 check_function_arguments_recurse (callback, ctx,
5645 TREE_OPERAND (param, 0), param_num);
5646 return;
5649 if (TREE_CODE (param) == CALL_EXPR)
5651 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5652 tree attrs;
5653 bool found_format_arg = false;
5655 /* See if this is a call to a known internationalization function
5656 that modifies a format arg. Such a function may have multiple
5657 format_arg attributes (for example, ngettext). */
5659 for (attrs = TYPE_ATTRIBUTES (type);
5660 attrs;
5661 attrs = TREE_CHAIN (attrs))
5662 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5664 tree inner_arg;
5665 tree format_num_expr;
5666 int format_num;
5667 int i;
5668 call_expr_arg_iterator iter;
5670 /* Extract the argument number, which was previously checked
5671 to be valid. */
5672 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5674 format_num = tree_to_uhwi (format_num_expr);
5676 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5677 inner_arg != 0;
5678 inner_arg = next_call_expr_arg (&iter), i++)
5679 if (i == format_num)
5681 check_function_arguments_recurse (callback, ctx,
5682 inner_arg, param_num);
5683 found_format_arg = true;
5684 break;
5688 /* If we found a format_arg attribute and did a recursive check,
5689 we are done with checking this argument. Otherwise, we continue
5690 and this will be considered a non-literal. */
5691 if (found_format_arg)
5692 return;
5695 if (TREE_CODE (param) == COND_EXPR)
5697 /* Simplify to avoid warning for an impossible case. */
5698 param = fold_for_warn (param);
5699 if (TREE_CODE (param) == COND_EXPR)
5701 /* Check both halves of the conditional expression. */
5702 check_function_arguments_recurse (callback, ctx,
5703 TREE_OPERAND (param, 1),
5704 param_num);
5705 check_function_arguments_recurse (callback, ctx,
5706 TREE_OPERAND (param, 2),
5707 param_num);
5708 return;
5712 (*callback) (ctx, param, param_num);
5715 /* Checks for a builtin function FNDECL that the number of arguments
5716 NARGS against the required number REQUIRED and issues an error if
5717 there is a mismatch. Returns true if the number of arguments is
5718 correct, otherwise false. LOC is the location of FNDECL. */
5720 static bool
5721 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5722 int required)
5724 if (nargs < required)
5726 error_at (loc, "too few arguments to function %qE", fndecl);
5727 return false;
5729 else if (nargs > required)
5731 error_at (loc, "too many arguments to function %qE", fndecl);
5732 return false;
5734 return true;
5737 /* Helper macro for check_builtin_function_arguments. */
5738 #define ARG_LOCATION(N) \
5739 (arg_loc.is_empty () \
5740 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5741 : expansion_point_location (arg_loc[(N)]))
5743 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5744 Returns false if there was an error, otherwise true. LOC is the
5745 location of the function; ARG_LOC is a vector of locations of the
5746 arguments. */
5748 bool
5749 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5750 tree fndecl, int nargs, tree *args)
5752 if (!DECL_BUILT_IN (fndecl)
5753 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5754 return true;
5756 switch (DECL_FUNCTION_CODE (fndecl))
5758 case BUILT_IN_ALLOCA_WITH_ALIGN:
5760 /* Get the requested alignment (in bits) if it's a constant
5761 integer expression. */
5762 unsigned HOST_WIDE_INT align
5763 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5765 /* Determine if the requested alignment is a power of 2. */
5766 if ((align & (align - 1)))
5767 align = 0;
5769 /* The maximum alignment in bits corresponding to the same
5770 maximum in bytes enforced in check_user_alignment(). */
5771 unsigned maxalign = (UINT_MAX >> 1) + 1;
5773 /* Reject invalid alignments. */
5774 if (align < BITS_PER_UNIT || maxalign < align)
5776 error_at (ARG_LOCATION (1),
5777 "second argument to function %qE must be a constant "
5778 "integer power of 2 between %qi and %qu bits",
5779 fndecl, BITS_PER_UNIT, maxalign);
5780 return false;
5782 return true;
5785 case BUILT_IN_CONSTANT_P:
5786 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5788 case BUILT_IN_ISFINITE:
5789 case BUILT_IN_ISINF:
5790 case BUILT_IN_ISINF_SIGN:
5791 case BUILT_IN_ISNAN:
5792 case BUILT_IN_ISNORMAL:
5793 case BUILT_IN_SIGNBIT:
5794 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5796 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5798 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5799 "call to function %qE", fndecl);
5800 return false;
5802 return true;
5804 return false;
5806 case BUILT_IN_ISGREATER:
5807 case BUILT_IN_ISGREATEREQUAL:
5808 case BUILT_IN_ISLESS:
5809 case BUILT_IN_ISLESSEQUAL:
5810 case BUILT_IN_ISLESSGREATER:
5811 case BUILT_IN_ISUNORDERED:
5812 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5814 enum tree_code code0, code1;
5815 code0 = TREE_CODE (TREE_TYPE (args[0]));
5816 code1 = TREE_CODE (TREE_TYPE (args[1]));
5817 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5818 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5819 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5821 error_at (loc, "non-floating-point arguments in call to "
5822 "function %qE", fndecl);
5823 return false;
5825 return true;
5827 return false;
5829 case BUILT_IN_FPCLASSIFY:
5830 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5832 for (unsigned int i = 0; i < 5; i++)
5833 if (TREE_CODE (args[i]) != INTEGER_CST)
5835 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5836 "call to function %qE", i + 1, fndecl);
5837 return false;
5840 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5842 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5843 "call to function %qE", fndecl);
5844 return false;
5846 return true;
5848 return false;
5850 case BUILT_IN_ASSUME_ALIGNED:
5851 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5853 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5855 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5856 "function %qE", fndecl);
5857 return false;
5859 return true;
5861 return false;
5863 case BUILT_IN_ADD_OVERFLOW:
5864 case BUILT_IN_SUB_OVERFLOW:
5865 case BUILT_IN_MUL_OVERFLOW:
5866 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5868 unsigned i;
5869 for (i = 0; i < 2; i++)
5870 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5872 error_at (ARG_LOCATION (i), "argument %u in call to function "
5873 "%qE does not have integral type", i + 1, fndecl);
5874 return false;
5876 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5877 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5879 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5880 "does not have pointer to integral type", fndecl);
5881 return false;
5883 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5885 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5886 "has pointer to enumerated type", fndecl);
5887 return false;
5889 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5891 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5892 "has pointer to boolean type", fndecl);
5893 return false;
5895 return true;
5897 return false;
5899 case BUILT_IN_ADD_OVERFLOW_P:
5900 case BUILT_IN_SUB_OVERFLOW_P:
5901 case BUILT_IN_MUL_OVERFLOW_P:
5902 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5904 unsigned i;
5905 for (i = 0; i < 3; i++)
5906 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5908 error_at (ARG_LOCATION (i), "argument %u in call to function "
5909 "%qE does not have integral type", i + 1, fndecl);
5910 return false;
5912 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5914 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5915 "%qE has enumerated type", fndecl);
5916 return false;
5918 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5920 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5921 "%qE has boolean type", fndecl);
5922 return false;
5924 return true;
5926 return false;
5928 default:
5929 return true;
5933 /* Function to help qsort sort FIELD_DECLs by name order. */
5936 field_decl_cmp (const void *x_p, const void *y_p)
5938 const tree *const x = (const tree *const) x_p;
5939 const tree *const y = (const tree *const) y_p;
5941 if (DECL_NAME (*x) == DECL_NAME (*y))
5942 /* A nontype is "greater" than a type. */
5943 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5944 if (DECL_NAME (*x) == NULL_TREE)
5945 return -1;
5946 if (DECL_NAME (*y) == NULL_TREE)
5947 return 1;
5948 if (DECL_NAME (*x) < DECL_NAME (*y))
5949 return -1;
5950 return 1;
5953 static struct {
5954 gt_pointer_operator new_value;
5955 void *cookie;
5956 } resort_data;
5958 /* This routine compares two fields like field_decl_cmp but using the
5959 pointer operator in resort_data. */
5961 static int
5962 resort_field_decl_cmp (const void *x_p, const void *y_p)
5964 const tree *const x = (const tree *const) x_p;
5965 const tree *const y = (const tree *const) y_p;
5967 if (DECL_NAME (*x) == DECL_NAME (*y))
5968 /* A nontype is "greater" than a type. */
5969 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5970 if (DECL_NAME (*x) == NULL_TREE)
5971 return -1;
5972 if (DECL_NAME (*y) == NULL_TREE)
5973 return 1;
5975 tree d1 = DECL_NAME (*x);
5976 tree d2 = DECL_NAME (*y);
5977 resort_data.new_value (&d1, resort_data.cookie);
5978 resort_data.new_value (&d2, resort_data.cookie);
5979 if (d1 < d2)
5980 return -1;
5982 return 1;
5985 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
5987 void
5988 resort_sorted_fields (void *obj,
5989 void * ARG_UNUSED (orig_obj),
5990 gt_pointer_operator new_value,
5991 void *cookie)
5993 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
5994 resort_data.new_value = new_value;
5995 resort_data.cookie = cookie;
5996 qsort (&sf->elts[0], sf->len, sizeof (tree),
5997 resort_field_decl_cmp);
6000 /* Subroutine of c_parse_error.
6001 Return the result of concatenating LHS and RHS. RHS is really
6002 a string literal, its first character is indicated by RHS_START and
6003 RHS_SIZE is its length (including the terminating NUL character).
6005 The caller is responsible for deleting the returned pointer. */
6007 static char *
6008 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6010 const int lhs_size = strlen (lhs);
6011 char *result = XNEWVEC (char, lhs_size + rhs_size);
6012 strncpy (result, lhs, lhs_size);
6013 strncpy (result + lhs_size, rhs_start, rhs_size);
6014 return result;
6017 /* Issue the error given by GMSGID, indicating that it occurred before
6018 TOKEN, which had the associated VALUE. */
6020 void
6021 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6022 tree value, unsigned char token_flags)
6024 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6026 char *message = NULL;
6028 if (token_type == CPP_EOF)
6029 message = catenate_messages (gmsgid, " at end of input");
6030 else if (token_type == CPP_CHAR
6031 || token_type == CPP_WCHAR
6032 || token_type == CPP_CHAR16
6033 || token_type == CPP_CHAR32
6034 || token_type == CPP_UTF8CHAR)
6036 unsigned int val = TREE_INT_CST_LOW (value);
6037 const char *prefix;
6039 switch (token_type)
6041 default:
6042 prefix = "";
6043 break;
6044 case CPP_WCHAR:
6045 prefix = "L";
6046 break;
6047 case CPP_CHAR16:
6048 prefix = "u";
6049 break;
6050 case CPP_CHAR32:
6051 prefix = "U";
6052 break;
6053 case CPP_UTF8CHAR:
6054 prefix = "u8";
6055 break;
6058 if (val <= UCHAR_MAX && ISGRAPH (val))
6059 message = catenate_messages (gmsgid, " before %s'%c'");
6060 else
6061 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6063 error (message, prefix, val);
6064 free (message);
6065 message = NULL;
6067 else if (token_type == CPP_CHAR_USERDEF
6068 || token_type == CPP_WCHAR_USERDEF
6069 || token_type == CPP_CHAR16_USERDEF
6070 || token_type == CPP_CHAR32_USERDEF
6071 || token_type == CPP_UTF8CHAR_USERDEF)
6072 message = catenate_messages (gmsgid,
6073 " before user-defined character literal");
6074 else if (token_type == CPP_STRING_USERDEF
6075 || token_type == CPP_WSTRING_USERDEF
6076 || token_type == CPP_STRING16_USERDEF
6077 || token_type == CPP_STRING32_USERDEF
6078 || token_type == CPP_UTF8STRING_USERDEF)
6079 message = catenate_messages (gmsgid, " before user-defined string literal");
6080 else if (token_type == CPP_STRING
6081 || token_type == CPP_WSTRING
6082 || token_type == CPP_STRING16
6083 || token_type == CPP_STRING32
6084 || token_type == CPP_UTF8STRING)
6085 message = catenate_messages (gmsgid, " before string constant");
6086 else if (token_type == CPP_NUMBER)
6087 message = catenate_messages (gmsgid, " before numeric constant");
6088 else if (token_type == CPP_NAME)
6090 message = catenate_messages (gmsgid, " before %qE");
6091 error (message, value);
6092 free (message);
6093 message = NULL;
6095 else if (token_type == CPP_PRAGMA)
6096 message = catenate_messages (gmsgid, " before %<#pragma%>");
6097 else if (token_type == CPP_PRAGMA_EOL)
6098 message = catenate_messages (gmsgid, " before end of line");
6099 else if (token_type == CPP_DECLTYPE)
6100 message = catenate_messages (gmsgid, " before %<decltype%>");
6101 else if (token_type < N_TTYPES)
6103 message = catenate_messages (gmsgid, " before %qs token");
6104 error (message, cpp_type2name (token_type, token_flags));
6105 free (message);
6106 message = NULL;
6108 else
6109 error (gmsgid);
6111 if (message)
6113 error (message);
6114 free (message);
6116 #undef catenate_messages
6119 /* Return the gcc option code associated with the reason for a cpp
6120 message, or 0 if none. */
6122 static int
6123 c_option_controlling_cpp_error (int reason)
6125 const struct cpp_reason_option_codes_t *entry;
6127 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6129 if (entry->reason == reason)
6130 return entry->option_code;
6132 return 0;
6135 /* Callback from cpp_error for PFILE to print diagnostics from the
6136 preprocessor. The diagnostic is of type LEVEL, with REASON set
6137 to the reason code if LEVEL is represents a warning, at location
6138 RICHLOC unless this is after lexing and the compiler's location
6139 should be used instead; MSG is the translated message and AP
6140 the arguments. Returns true if a diagnostic was emitted, false
6141 otherwise. */
6143 bool
6144 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6145 rich_location *richloc,
6146 const char *msg, va_list *ap)
6148 diagnostic_info diagnostic;
6149 diagnostic_t dlevel;
6150 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6151 bool ret;
6153 switch (level)
6155 case CPP_DL_WARNING_SYSHDR:
6156 if (flag_no_output)
6157 return false;
6158 global_dc->dc_warn_system_headers = 1;
6159 /* Fall through. */
6160 case CPP_DL_WARNING:
6161 if (flag_no_output)
6162 return false;
6163 dlevel = DK_WARNING;
6164 break;
6165 case CPP_DL_PEDWARN:
6166 if (flag_no_output && !flag_pedantic_errors)
6167 return false;
6168 dlevel = DK_PEDWARN;
6169 break;
6170 case CPP_DL_ERROR:
6171 dlevel = DK_ERROR;
6172 break;
6173 case CPP_DL_ICE:
6174 dlevel = DK_ICE;
6175 break;
6176 case CPP_DL_NOTE:
6177 dlevel = DK_NOTE;
6178 break;
6179 case CPP_DL_FATAL:
6180 dlevel = DK_FATAL;
6181 break;
6182 default:
6183 gcc_unreachable ();
6185 if (done_lexing)
6186 richloc->set_range (line_table, 0, input_location, true);
6187 diagnostic_set_info_translated (&diagnostic, msg, ap,
6188 richloc, dlevel);
6189 diagnostic_override_option_index (&diagnostic,
6190 c_option_controlling_cpp_error (reason));
6191 ret = report_diagnostic (&diagnostic);
6192 if (level == CPP_DL_WARNING_SYSHDR)
6193 global_dc->dc_warn_system_headers = save_warn_system_headers;
6194 return ret;
6197 /* Convert a character from the host to the target execution character
6198 set. cpplib handles this, mostly. */
6200 HOST_WIDE_INT
6201 c_common_to_target_charset (HOST_WIDE_INT c)
6203 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6204 zero-extended under -fno-signed-char. cpplib insists that characters
6205 and character constants are always unsigned. Hence we must convert
6206 back and forth. */
6207 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6209 uc = cpp_host_to_exec_charset (parse_in, uc);
6211 if (flag_signed_char)
6212 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6213 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6214 else
6215 return uc;
6218 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6219 references with an INDIRECT_REF of a constant at the bottom; much like the
6220 traditional rendering of offsetof as a macro. Return the folded result. */
6222 tree
6223 fold_offsetof_1 (tree expr, enum tree_code ctx)
6225 tree base, off, t;
6226 tree_code code = TREE_CODE (expr);
6227 switch (code)
6229 case ERROR_MARK:
6230 return expr;
6232 case VAR_DECL:
6233 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6234 return error_mark_node;
6236 case CALL_EXPR:
6237 case TARGET_EXPR:
6238 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6239 return error_mark_node;
6241 case NOP_EXPR:
6242 case INDIRECT_REF:
6243 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6245 error ("cannot apply %<offsetof%> to a non constant address");
6246 return error_mark_node;
6248 return TREE_OPERAND (expr, 0);
6250 case COMPONENT_REF:
6251 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6252 if (base == error_mark_node)
6253 return base;
6255 t = TREE_OPERAND (expr, 1);
6256 if (DECL_C_BIT_FIELD (t))
6258 error ("attempt to take address of bit-field structure "
6259 "member %qD", t);
6260 return error_mark_node;
6262 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6263 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6264 / BITS_PER_UNIT));
6265 break;
6267 case ARRAY_REF:
6268 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6269 if (base == error_mark_node)
6270 return base;
6272 t = TREE_OPERAND (expr, 1);
6274 /* Check if the offset goes beyond the upper bound of the array. */
6275 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6277 tree upbound = array_ref_up_bound (expr);
6278 if (upbound != NULL_TREE
6279 && TREE_CODE (upbound) == INTEGER_CST
6280 && !tree_int_cst_equal (upbound,
6281 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6283 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6284 upbound = size_binop (PLUS_EXPR, upbound,
6285 build_int_cst (TREE_TYPE (upbound), 1));
6286 if (tree_int_cst_lt (upbound, t))
6288 tree v;
6290 for (v = TREE_OPERAND (expr, 0);
6291 TREE_CODE (v) == COMPONENT_REF;
6292 v = TREE_OPERAND (v, 0))
6293 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6294 == RECORD_TYPE)
6296 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6297 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6298 if (TREE_CODE (fld_chain) == FIELD_DECL)
6299 break;
6301 if (fld_chain)
6302 break;
6304 /* Don't warn if the array might be considered a poor
6305 man's flexible array member with a very permissive
6306 definition thereof. */
6307 if (TREE_CODE (v) == ARRAY_REF
6308 || TREE_CODE (v) == COMPONENT_REF)
6309 warning (OPT_Warray_bounds,
6310 "index %E denotes an offset "
6311 "greater than size of %qT",
6312 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6317 t = convert (sizetype, t);
6318 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6319 break;
6321 case COMPOUND_EXPR:
6322 /* Handle static members of volatile structs. */
6323 t = TREE_OPERAND (expr, 1);
6324 gcc_assert (VAR_P (t));
6325 return fold_offsetof_1 (t);
6327 default:
6328 gcc_unreachable ();
6331 return fold_build_pointer_plus (base, off);
6334 /* Likewise, but convert it to the return type of offsetof. */
6336 tree
6337 fold_offsetof (tree expr)
6339 return convert (size_type_node, fold_offsetof_1 (expr));
6343 /* *PTYPE is an incomplete array. Complete it with a domain based on
6344 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6345 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6346 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6349 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6351 tree maxindex, type, main_type, elt, unqual_elt;
6352 int failure = 0, quals;
6353 hashval_t hashcode = 0;
6354 bool overflow_p = false;
6356 maxindex = size_zero_node;
6357 if (initial_value)
6359 if (TREE_CODE (initial_value) == STRING_CST)
6361 int eltsize
6362 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6363 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6365 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6367 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6369 if (vec_safe_is_empty (v))
6371 if (pedantic)
6372 failure = 3;
6373 maxindex = ssize_int (-1);
6375 else
6377 tree curindex;
6378 unsigned HOST_WIDE_INT cnt;
6379 constructor_elt *ce;
6380 bool fold_p = false;
6382 if ((*v)[0].index)
6383 maxindex = (*v)[0].index, fold_p = true;
6385 curindex = maxindex;
6387 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6389 bool curfold_p = false;
6390 if (ce->index)
6391 curindex = ce->index, curfold_p = true;
6392 else
6394 if (fold_p)
6396 /* Since we treat size types now as ordinary
6397 unsigned types, we need an explicit overflow
6398 check. */
6399 tree orig = curindex;
6400 curindex = fold_convert (sizetype, curindex);
6401 overflow_p |= tree_int_cst_lt (curindex, orig);
6403 curindex = size_binop (PLUS_EXPR, curindex,
6404 size_one_node);
6406 if (tree_int_cst_lt (maxindex, curindex))
6407 maxindex = curindex, fold_p = curfold_p;
6409 if (fold_p)
6411 tree orig = maxindex;
6412 maxindex = fold_convert (sizetype, maxindex);
6413 overflow_p |= tree_int_cst_lt (maxindex, orig);
6417 else
6419 /* Make an error message unless that happened already. */
6420 if (initial_value != error_mark_node)
6421 failure = 1;
6424 else
6426 failure = 2;
6427 if (!do_default)
6428 return failure;
6431 type = *ptype;
6432 elt = TREE_TYPE (type);
6433 quals = TYPE_QUALS (strip_array_types (elt));
6434 if (quals == 0)
6435 unqual_elt = elt;
6436 else
6437 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6439 /* Using build_distinct_type_copy and modifying things afterward instead
6440 of using build_array_type to create a new type preserves all of the
6441 TYPE_LANG_FLAG_? bits that the front end may have set. */
6442 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6443 TREE_TYPE (main_type) = unqual_elt;
6444 TYPE_DOMAIN (main_type)
6445 = build_range_type (TREE_TYPE (maxindex),
6446 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6447 layout_type (main_type);
6449 /* Make sure we have the canonical MAIN_TYPE. */
6450 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
6451 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6452 hashcode);
6453 main_type = type_hash_canon (hashcode, main_type);
6455 /* Fix the canonical type. */
6456 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6457 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6458 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6459 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6460 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6461 != TYPE_DOMAIN (main_type)))
6462 TYPE_CANONICAL (main_type)
6463 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6464 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
6465 else
6466 TYPE_CANONICAL (main_type) = main_type;
6468 if (quals == 0)
6469 type = main_type;
6470 else
6471 type = c_build_qualified_type (main_type, quals);
6473 if (COMPLETE_TYPE_P (type)
6474 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6475 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6477 error ("size of array is too large");
6478 /* If we proceed with the array type as it is, we'll eventually
6479 crash in tree_to_[su]hwi(). */
6480 type = error_mark_node;
6483 *ptype = type;
6484 return failure;
6487 /* Like c_mark_addressable but don't check register qualifier. */
6488 void
6489 c_common_mark_addressable_vec (tree t)
6491 while (handled_component_p (t))
6492 t = TREE_OPERAND (t, 0);
6493 if (!VAR_P (t)
6494 && TREE_CODE (t) != PARM_DECL
6495 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6496 return;
6497 TREE_ADDRESSABLE (t) = 1;
6502 /* Used to help initialize the builtin-types.def table. When a type of
6503 the correct size doesn't exist, use error_mark_node instead of NULL.
6504 The later results in segfaults even when a decl using the type doesn't
6505 get invoked. */
6507 tree
6508 builtin_type_for_size (int size, bool unsignedp)
6510 tree type = c_common_type_for_size (size, unsignedp);
6511 return type ? type : error_mark_node;
6514 /* A helper function for resolve_overloaded_builtin in resolving the
6515 overloaded __sync_ builtins. Returns a positive power of 2 if the
6516 first operand of PARAMS is a pointer to a supported data type.
6517 Returns 0 if an error is encountered.
6518 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6519 built-ins. */
6521 static int
6522 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6524 /* Type of the argument. */
6525 tree argtype;
6526 /* Type the argument points to. */
6527 tree type;
6528 int size;
6530 if (vec_safe_is_empty (params))
6532 error ("too few arguments to function %qE", function);
6533 return 0;
6536 argtype = type = TREE_TYPE ((*params)[0]);
6537 if (TREE_CODE (type) == ARRAY_TYPE)
6539 /* Force array-to-pointer decay for C++. */
6540 gcc_assert (c_dialect_cxx());
6541 (*params)[0] = default_conversion ((*params)[0]);
6542 type = TREE_TYPE ((*params)[0]);
6544 if (TREE_CODE (type) != POINTER_TYPE)
6545 goto incompatible;
6547 type = TREE_TYPE (type);
6548 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6549 goto incompatible;
6551 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6552 goto incompatible;
6554 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6555 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6556 return size;
6558 incompatible:
6559 /* Issue the diagnostic only if the argument is valid, otherwise
6560 it would be redundant at best and could be misleading. */
6561 if (argtype != error_mark_node)
6562 error ("operand type %qT is incompatible with argument %d of %qE",
6563 argtype, 1, function);
6564 return 0;
6567 /* A helper function for resolve_overloaded_builtin. Adds casts to
6568 PARAMS to make arguments match up with those of FUNCTION. Drops
6569 the variadic arguments at the end. Returns false if some error
6570 was encountered; true on success. */
6572 static bool
6573 sync_resolve_params (location_t loc, tree orig_function, tree function,
6574 vec<tree, va_gc> *params, bool orig_format)
6576 function_args_iterator iter;
6577 tree ptype;
6578 unsigned int parmnum;
6580 function_args_iter_init (&iter, TREE_TYPE (function));
6581 /* We've declared the implementation functions to use "volatile void *"
6582 as the pointer parameter, so we shouldn't get any complaints from the
6583 call to check_function_arguments what ever type the user used. */
6584 function_args_iter_next (&iter);
6585 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6586 ptype = TYPE_MAIN_VARIANT (ptype);
6588 /* For the rest of the values, we need to cast these to FTYPE, so that we
6589 don't get warnings for passing pointer types, etc. */
6590 parmnum = 0;
6591 while (1)
6593 tree val, arg_type;
6595 arg_type = function_args_iter_cond (&iter);
6596 /* XXX void_type_node belies the abstraction. */
6597 if (arg_type == void_type_node)
6598 break;
6600 ++parmnum;
6601 if (params->length () <= parmnum)
6603 error_at (loc, "too few arguments to function %qE", orig_function);
6604 return false;
6607 /* Only convert parameters if arg_type is unsigned integer type with
6608 new format sync routines, i.e. don't attempt to convert pointer
6609 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6610 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6611 kinds). */
6612 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6614 /* Ideally for the first conversion we'd use convert_for_assignment
6615 so that we get warnings for anything that doesn't match the pointer
6616 type. This isn't portable across the C and C++ front ends atm. */
6617 val = (*params)[parmnum];
6618 val = convert (ptype, val);
6619 val = convert (arg_type, val);
6620 (*params)[parmnum] = val;
6623 function_args_iter_next (&iter);
6626 /* __atomic routines are not variadic. */
6627 if (!orig_format && params->length () != parmnum + 1)
6629 error_at (loc, "too many arguments to function %qE", orig_function);
6630 return false;
6633 /* The definition of these primitives is variadic, with the remaining
6634 being "an optional list of variables protected by the memory barrier".
6635 No clue what that's supposed to mean, precisely, but we consider all
6636 call-clobbered variables to be protected so we're safe. */
6637 params->truncate (parmnum + 1);
6639 return true;
6642 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6643 RESULT to make it match the type of the first pointer argument in
6644 PARAMS. */
6646 static tree
6647 sync_resolve_return (tree first_param, tree result, bool orig_format)
6649 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6650 tree rtype = TREE_TYPE (result);
6651 ptype = TYPE_MAIN_VARIANT (ptype);
6653 /* New format doesn't require casting unless the types are the same size. */
6654 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6655 return convert (ptype, result);
6656 else
6657 return result;
6660 /* This function verifies the PARAMS to generic atomic FUNCTION.
6661 It returns the size if all the parameters are the same size, otherwise
6662 0 is returned if the parameters are invalid. */
6664 static int
6665 get_atomic_generic_size (location_t loc, tree function,
6666 vec<tree, va_gc> *params)
6668 unsigned int n_param;
6669 unsigned int n_model;
6670 unsigned int x;
6671 int size_0;
6672 tree type_0;
6674 /* Determine the parameter makeup. */
6675 switch (DECL_FUNCTION_CODE (function))
6677 case BUILT_IN_ATOMIC_EXCHANGE:
6678 n_param = 4;
6679 n_model = 1;
6680 break;
6681 case BUILT_IN_ATOMIC_LOAD:
6682 case BUILT_IN_ATOMIC_STORE:
6683 n_param = 3;
6684 n_model = 1;
6685 break;
6686 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6687 n_param = 6;
6688 n_model = 2;
6689 break;
6690 default:
6691 gcc_unreachable ();
6694 if (vec_safe_length (params) != n_param)
6696 error_at (loc, "incorrect number of arguments to function %qE", function);
6697 return 0;
6700 /* Get type of first parameter, and determine its size. */
6701 type_0 = TREE_TYPE ((*params)[0]);
6702 if (TREE_CODE (type_0) == ARRAY_TYPE)
6704 /* Force array-to-pointer decay for C++. */
6705 gcc_assert (c_dialect_cxx());
6706 (*params)[0] = default_conversion ((*params)[0]);
6707 type_0 = TREE_TYPE ((*params)[0]);
6709 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6711 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6712 function);
6713 return 0;
6716 /* Types must be compile time constant sizes. */
6717 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6719 error_at (loc,
6720 "argument 1 of %qE must be a pointer to a constant size type",
6721 function);
6722 return 0;
6725 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6727 /* Zero size objects are not allowed. */
6728 if (size_0 == 0)
6730 error_at (loc,
6731 "argument 1 of %qE must be a pointer to a nonzero size object",
6732 function);
6733 return 0;
6736 /* Check each other parameter is a pointer and the same size. */
6737 for (x = 0; x < n_param - n_model; x++)
6739 int size;
6740 tree type = TREE_TYPE ((*params)[x]);
6741 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6742 if (n_param == 6 && x == 3)
6743 continue;
6744 if (!POINTER_TYPE_P (type))
6746 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6747 function);
6748 return 0;
6750 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6751 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6752 != INTEGER_CST)
6754 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6755 "size type", x + 1, function);
6756 return 0;
6758 else if (FUNCTION_POINTER_TYPE_P (type))
6760 error_at (loc, "argument %d of %qE must not be a pointer to a "
6761 "function", x + 1, function);
6762 return 0;
6764 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6765 size = type_size ? tree_to_uhwi (type_size) : 0;
6766 if (size != size_0)
6768 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6769 function);
6770 return 0;
6774 /* Check memory model parameters for validity. */
6775 for (x = n_param - n_model ; x < n_param; x++)
6777 tree p = (*params)[x];
6778 if (TREE_CODE (p) == INTEGER_CST)
6780 int i = tree_to_uhwi (p);
6781 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
6783 warning_at (loc, OPT_Winvalid_memory_model,
6784 "invalid memory model argument %d of %qE", x + 1,
6785 function);
6788 else
6789 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6791 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6792 function);
6793 return 0;
6797 return size_0;
6801 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6802 at the beginning of the parameter list PARAMS representing the size of the
6803 objects. This is to match the library ABI requirement. LOC is the location
6804 of the function call.
6805 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6806 returned to allow the external call to be constructed. */
6808 static tree
6809 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6810 vec<tree, va_gc> *params)
6812 tree size_node;
6814 /* Insert a SIZE_T parameter as the first param. If there isn't
6815 enough space, allocate a new vector and recursively re-build with that. */
6816 if (!params->space (1))
6818 unsigned int z, len;
6819 vec<tree, va_gc> *v;
6820 tree f;
6822 len = params->length ();
6823 vec_alloc (v, len + 1);
6824 v->quick_push (build_int_cst (size_type_node, n));
6825 for (z = 0; z < len; z++)
6826 v->quick_push ((*params)[z]);
6827 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6828 vec_free (v);
6829 return f;
6832 /* Add the size parameter and leave as a function call for processing. */
6833 size_node = build_int_cst (size_type_node, n);
6834 params->quick_insert (0, size_node);
6835 return NULL_TREE;
6839 /* Return whether atomic operations for naturally aligned N-byte
6840 arguments are supported, whether inline or through libatomic. */
6841 static bool
6842 atomic_size_supported_p (int n)
6844 switch (n)
6846 case 1:
6847 case 2:
6848 case 4:
6849 case 8:
6850 return true;
6852 case 16:
6853 return targetm.scalar_mode_supported_p (TImode);
6855 default:
6856 return false;
6860 /* This will process an __atomic_exchange function call, determine whether it
6861 needs to be mapped to the _N variation, or turned into a library call.
6862 LOC is the location of the builtin call.
6863 FUNCTION is the DECL that has been invoked;
6864 PARAMS is the argument list for the call. The return value is non-null
6865 TRUE is returned if it is translated into the proper format for a call to the
6866 external library, and NEW_RETURN is set the tree for that function.
6867 FALSE is returned if processing for the _N variation is required, and
6868 NEW_RETURN is set to the return value the result is copied into. */
6869 static bool
6870 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6871 vec<tree, va_gc> *params, tree *new_return)
6873 tree p0, p1, p2, p3;
6874 tree I_type, I_type_ptr;
6875 int n = get_atomic_generic_size (loc, function, params);
6877 /* Size of 0 is an error condition. */
6878 if (n == 0)
6880 *new_return = error_mark_node;
6881 return true;
6884 /* If not a lock-free size, change to the library generic format. */
6885 if (!atomic_size_supported_p (n))
6887 *new_return = add_atomic_size_parameter (n, loc, function, params);
6888 return true;
6891 /* Otherwise there is a lockfree match, transform the call from:
6892 void fn(T* mem, T* desired, T* return, model)
6893 into
6894 *return = (T) (fn (In* mem, (In) *desired, model)) */
6896 p0 = (*params)[0];
6897 p1 = (*params)[1];
6898 p2 = (*params)[2];
6899 p3 = (*params)[3];
6901 /* Create pointer to appropriate size. */
6902 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6903 I_type_ptr = build_pointer_type (I_type);
6905 /* Convert object pointer to required type. */
6906 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6907 (*params)[0] = p0;
6908 /* Convert new value to required type, and dereference it. */
6909 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6910 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6911 (*params)[1] = p1;
6913 /* Move memory model to the 3rd position, and end param list. */
6914 (*params)[2] = p3;
6915 params->truncate (3);
6917 /* Convert return pointer and dereference it for later assignment. */
6918 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6920 return false;
6924 /* This will process an __atomic_compare_exchange function call, determine
6925 whether it needs to be mapped to the _N variation, or turned into a lib call.
6926 LOC is the location of the builtin call.
6927 FUNCTION is the DECL that has been invoked;
6928 PARAMS is the argument list for the call. The return value is non-null
6929 TRUE is returned if it is translated into the proper format for a call to the
6930 external library, and NEW_RETURN is set the tree for that function.
6931 FALSE is returned if processing for the _N variation is required. */
6933 static bool
6934 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6935 vec<tree, va_gc> *params,
6936 tree *new_return)
6938 tree p0, p1, p2;
6939 tree I_type, I_type_ptr;
6940 int n = get_atomic_generic_size (loc, function, params);
6942 /* Size of 0 is an error condition. */
6943 if (n == 0)
6945 *new_return = error_mark_node;
6946 return true;
6949 /* If not a lock-free size, change to the library generic format. */
6950 if (!atomic_size_supported_p (n))
6952 /* The library generic format does not have the weak parameter, so
6953 remove it from the param list. Since a parameter has been removed,
6954 we can be sure that there is room for the SIZE_T parameter, meaning
6955 there will not be a recursive rebuilding of the parameter list, so
6956 there is no danger this will be done twice. */
6957 if (n > 0)
6959 (*params)[3] = (*params)[4];
6960 (*params)[4] = (*params)[5];
6961 params->truncate (5);
6963 *new_return = add_atomic_size_parameter (n, loc, function, params);
6964 return true;
6967 /* Otherwise, there is a match, so the call needs to be transformed from:
6968 bool fn(T* mem, T* desired, T* return, weak, success, failure)
6969 into
6970 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
6972 p0 = (*params)[0];
6973 p1 = (*params)[1];
6974 p2 = (*params)[2];
6976 /* Create pointer to appropriate size. */
6977 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6978 I_type_ptr = build_pointer_type (I_type);
6980 /* Convert object pointer to required type. */
6981 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6982 (*params)[0] = p0;
6984 /* Convert expected pointer to required type. */
6985 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
6986 (*params)[1] = p1;
6988 /* Convert desired value to required type, and dereference it. */
6989 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6990 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
6991 (*params)[2] = p2;
6993 /* The rest of the parameters are fine. NULL means no special return value
6994 processing.*/
6995 *new_return = NULL;
6996 return false;
7000 /* This will process an __atomic_load function call, determine whether it
7001 needs to be mapped to the _N variation, or turned into a library call.
7002 LOC is the location of the builtin call.
7003 FUNCTION is the DECL that has been invoked;
7004 PARAMS is the argument list for the call. The return value is non-null
7005 TRUE is returned if it is translated into the proper format for a call to the
7006 external library, and NEW_RETURN is set the tree for that function.
7007 FALSE is returned if processing for the _N variation is required, and
7008 NEW_RETURN is set to the return value the result is copied into. */
7010 static bool
7011 resolve_overloaded_atomic_load (location_t loc, tree function,
7012 vec<tree, va_gc> *params, tree *new_return)
7014 tree p0, p1, p2;
7015 tree I_type, I_type_ptr;
7016 int n = get_atomic_generic_size (loc, function, params);
7018 /* Size of 0 is an error condition. */
7019 if (n == 0)
7021 *new_return = error_mark_node;
7022 return true;
7025 /* If not a lock-free size, change to the library generic format. */
7026 if (!atomic_size_supported_p (n))
7028 *new_return = add_atomic_size_parameter (n, loc, function, params);
7029 return true;
7032 /* Otherwise, there is a match, so the call needs to be transformed from:
7033 void fn(T* mem, T* return, model)
7034 into
7035 *return = (T) (fn ((In *) mem, model)) */
7037 p0 = (*params)[0];
7038 p1 = (*params)[1];
7039 p2 = (*params)[2];
7041 /* Create pointer to appropriate size. */
7042 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7043 I_type_ptr = build_pointer_type (I_type);
7045 /* Convert object pointer to required type. */
7046 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7047 (*params)[0] = p0;
7049 /* Move memory model to the 2nd position, and end param list. */
7050 (*params)[1] = p2;
7051 params->truncate (2);
7053 /* Convert return pointer and dereference it for later assignment. */
7054 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7056 return false;
7060 /* This will process an __atomic_store function call, determine whether it
7061 needs to be mapped to the _N variation, or turned into a library call.
7062 LOC is the location of the builtin call.
7063 FUNCTION is the DECL that has been invoked;
7064 PARAMS is the argument list for the call. The return value is non-null
7065 TRUE is returned if it is translated into the proper format for a call to the
7066 external library, and NEW_RETURN is set the tree for that function.
7067 FALSE is returned if processing for the _N variation is required, and
7068 NEW_RETURN is set to the return value the result is copied into. */
7070 static bool
7071 resolve_overloaded_atomic_store (location_t loc, tree function,
7072 vec<tree, va_gc> *params, tree *new_return)
7074 tree p0, p1;
7075 tree I_type, I_type_ptr;
7076 int n = get_atomic_generic_size (loc, function, params);
7078 /* Size of 0 is an error condition. */
7079 if (n == 0)
7081 *new_return = error_mark_node;
7082 return true;
7085 /* If not a lock-free size, change to the library generic format. */
7086 if (!atomic_size_supported_p (n))
7088 *new_return = add_atomic_size_parameter (n, loc, function, params);
7089 return true;
7092 /* Otherwise, there is a match, so the call needs to be transformed from:
7093 void fn(T* mem, T* value, model)
7094 into
7095 fn ((In *) mem, (In) *value, model) */
7097 p0 = (*params)[0];
7098 p1 = (*params)[1];
7100 /* Create pointer to appropriate size. */
7101 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7102 I_type_ptr = build_pointer_type (I_type);
7104 /* Convert object pointer to required type. */
7105 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7106 (*params)[0] = p0;
7108 /* Convert new value to required type, and dereference it. */
7109 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7110 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7111 (*params)[1] = p1;
7113 /* The memory model is in the right spot already. Return is void. */
7114 *new_return = NULL_TREE;
7116 return false;
7120 /* Some builtin functions are placeholders for other expressions. This
7121 function should be called immediately after parsing the call expression
7122 before surrounding code has committed to the type of the expression.
7124 LOC is the location of the builtin call.
7126 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7127 PARAMS is the argument list for the call. The return value is non-null
7128 when expansion is complete, and null if normal processing should
7129 continue. */
7131 tree
7132 resolve_overloaded_builtin (location_t loc, tree function,
7133 vec<tree, va_gc> *params)
7135 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7137 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7138 Those are not valid to call with a pointer to _Bool (or C++ bool)
7139 and so must be rejected. */
7140 bool fetch_op = true;
7141 bool orig_format = true;
7142 tree new_return = NULL_TREE;
7144 switch (DECL_BUILT_IN_CLASS (function))
7146 case BUILT_IN_NORMAL:
7147 break;
7148 case BUILT_IN_MD:
7149 if (targetm.resolve_overloaded_builtin)
7150 return targetm.resolve_overloaded_builtin (loc, function, params);
7151 else
7152 return NULL_TREE;
7153 default:
7154 return NULL_TREE;
7157 /* Handle BUILT_IN_NORMAL here. */
7158 switch (orig_code)
7160 case BUILT_IN_ATOMIC_EXCHANGE:
7161 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7162 case BUILT_IN_ATOMIC_LOAD:
7163 case BUILT_IN_ATOMIC_STORE:
7165 /* Handle these 4 together so that they can fall through to the next
7166 case if the call is transformed to an _N variant. */
7167 switch (orig_code)
7169 case BUILT_IN_ATOMIC_EXCHANGE:
7171 if (resolve_overloaded_atomic_exchange (loc, function, params,
7172 &new_return))
7173 return new_return;
7174 /* Change to the _N variant. */
7175 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7176 break;
7179 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7181 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7182 params,
7183 &new_return))
7184 return new_return;
7185 /* Change to the _N variant. */
7186 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7187 break;
7189 case BUILT_IN_ATOMIC_LOAD:
7191 if (resolve_overloaded_atomic_load (loc, function, params,
7192 &new_return))
7193 return new_return;
7194 /* Change to the _N variant. */
7195 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7196 break;
7198 case BUILT_IN_ATOMIC_STORE:
7200 if (resolve_overloaded_atomic_store (loc, function, params,
7201 &new_return))
7202 return new_return;
7203 /* Change to the _N variant. */
7204 orig_code = BUILT_IN_ATOMIC_STORE_N;
7205 break;
7207 default:
7208 gcc_unreachable ();
7211 /* FALLTHRU */
7212 case BUILT_IN_ATOMIC_EXCHANGE_N:
7213 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7214 case BUILT_IN_ATOMIC_LOAD_N:
7215 case BUILT_IN_ATOMIC_STORE_N:
7216 fetch_op = false;
7217 /* FALLTHRU */
7218 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7219 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7220 case BUILT_IN_ATOMIC_AND_FETCH_N:
7221 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7222 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7223 case BUILT_IN_ATOMIC_OR_FETCH_N:
7224 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7225 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7226 case BUILT_IN_ATOMIC_FETCH_AND_N:
7227 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7228 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7229 case BUILT_IN_ATOMIC_FETCH_OR_N:
7230 orig_format = false;
7231 /* FALLTHRU */
7232 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7233 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7234 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7235 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7236 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7237 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7238 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7239 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7240 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7241 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7242 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7243 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7244 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7245 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7246 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7247 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7249 /* The following are not _FETCH_OPs and must be accepted with
7250 pointers to _Bool (or C++ bool). */
7251 if (fetch_op)
7252 fetch_op =
7253 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7254 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7255 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7256 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7258 int n = sync_resolve_size (function, params, fetch_op);
7259 tree new_function, first_param, result;
7260 enum built_in_function fncode;
7262 if (n == 0)
7263 return error_mark_node;
7265 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7266 new_function = builtin_decl_explicit (fncode);
7267 if (!sync_resolve_params (loc, function, new_function, params,
7268 orig_format))
7269 return error_mark_node;
7271 first_param = (*params)[0];
7272 result = build_function_call_vec (loc, vNULL, new_function, params,
7273 NULL);
7274 if (result == error_mark_node)
7275 return result;
7276 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7277 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7278 && orig_code != BUILT_IN_ATOMIC_STORE_N
7279 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7280 result = sync_resolve_return (first_param, result, orig_format);
7282 if (fetch_op)
7283 /* Prevent -Wunused-value warning. */
7284 TREE_USED (result) = true;
7286 /* If new_return is set, assign function to that expr and cast the
7287 result to void since the generic interface returned void. */
7288 if (new_return)
7290 /* Cast function result from I{1,2,4,8,16} to the required type. */
7291 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7292 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7293 result);
7294 TREE_SIDE_EFFECTS (result) = 1;
7295 protected_set_expr_location (result, loc);
7296 result = convert (void_type_node, result);
7298 return result;
7301 default:
7302 return NULL_TREE;
7306 /* vector_types_compatible_elements_p is used in type checks of vectors
7307 values used as operands of binary operators. Where it returns true, and
7308 the other checks of the caller succeed (being vector types in he first
7309 place, and matching number of elements), we can just treat the types
7310 as essentially the same.
7311 Contrast with vector_targets_convertible_p, which is used for vector
7312 pointer types, and vector_types_convertible_p, which will allow
7313 language-specific matches under the control of flag_lax_vector_conversions,
7314 and might still require a conversion. */
7315 /* True if vector types T1 and T2 can be inputs to the same binary
7316 operator without conversion.
7317 We don't check the overall vector size here because some of our callers
7318 want to give different error messages when the vectors are compatible
7319 except for the element count. */
7321 bool
7322 vector_types_compatible_elements_p (tree t1, tree t2)
7324 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7325 t1 = TREE_TYPE (t1);
7326 t2 = TREE_TYPE (t2);
7328 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7330 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7331 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7332 || c2 == FIXED_POINT_TYPE));
7334 t1 = c_common_signed_type (t1);
7335 t2 = c_common_signed_type (t2);
7336 /* Equality works here because c_common_signed_type uses
7337 TYPE_MAIN_VARIANT. */
7338 if (t1 == t2)
7339 return true;
7340 if (opaque && c1 == c2
7341 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7342 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7343 return true;
7344 return false;
7347 /* Check for missing format attributes on function pointers. LTYPE is
7348 the new type or left-hand side type. RTYPE is the old type or
7349 right-hand side type. Returns TRUE if LTYPE is missing the desired
7350 attribute. */
7352 bool
7353 check_missing_format_attribute (tree ltype, tree rtype)
7355 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7356 tree ra;
7358 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7359 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7360 break;
7361 if (ra)
7363 tree la;
7364 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7365 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7366 break;
7367 return !la;
7369 else
7370 return false;
7373 /* Setup a TYPE_DECL node as a typedef representation.
7375 X is a TYPE_DECL for a typedef statement. Create a brand new
7376 ..._TYPE node (which will be just a variant of the existing
7377 ..._TYPE node with identical properties) and then install X
7378 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7380 The whole point here is to end up with a situation where each
7381 and every ..._TYPE node the compiler creates will be uniquely
7382 associated with AT MOST one node representing a typedef name.
7383 This way, even though the compiler substitutes corresponding
7384 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7385 early on, later parts of the compiler can always do the reverse
7386 translation and get back the corresponding typedef name. For
7387 example, given:
7389 typedef struct S MY_TYPE;
7390 MY_TYPE object;
7392 Later parts of the compiler might only know that `object' was of
7393 type `struct S' if it were not for code just below. With this
7394 code however, later parts of the compiler see something like:
7396 struct S' == struct S
7397 typedef struct S' MY_TYPE;
7398 struct S' object;
7400 And they can then deduce (from the node for type struct S') that
7401 the original object declaration was:
7403 MY_TYPE object;
7405 Being able to do this is important for proper support of protoize,
7406 and also for generating precise symbolic debugging information
7407 which takes full account of the programmer's (typedef) vocabulary.
7409 Obviously, we don't want to generate a duplicate ..._TYPE node if
7410 the TYPE_DECL node that we are now processing really represents a
7411 standard built-in type. */
7413 void
7414 set_underlying_type (tree x)
7416 if (x == error_mark_node)
7417 return;
7418 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7420 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7421 TYPE_NAME (TREE_TYPE (x)) = x;
7423 else if (TREE_TYPE (x) != error_mark_node
7424 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7426 tree tt = TREE_TYPE (x);
7427 DECL_ORIGINAL_TYPE (x) = tt;
7428 tt = build_variant_type_copy (tt);
7429 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7430 TYPE_NAME (tt) = x;
7431 TREE_USED (tt) = TREE_USED (x);
7432 TREE_TYPE (x) = tt;
7436 /* Record the types used by the current global variable declaration
7437 being parsed, so that we can decide later to emit their debug info.
7438 Those types are in types_used_by_cur_var_decl, and we are going to
7439 store them in the types_used_by_vars_hash hash table.
7440 DECL is the declaration of the global variable that has been parsed. */
7442 void
7443 record_types_used_by_current_var_decl (tree decl)
7445 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7447 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7449 tree type = types_used_by_cur_var_decl->pop ();
7450 types_used_by_var_decl_insert (type, decl);
7454 /* The C and C++ parsers both use vectors to hold function arguments.
7455 For efficiency, we keep a cache of unused vectors. This is the
7456 cache. */
7458 typedef vec<tree, va_gc> *tree_gc_vec;
7459 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7461 /* Return a new vector from the cache. If the cache is empty,
7462 allocate a new vector. These vectors are GC'ed, so it is OK if the
7463 pointer is not released.. */
7465 vec<tree, va_gc> *
7466 make_tree_vector (void)
7468 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7469 return tree_vector_cache->pop ();
7470 else
7472 /* Passing 0 to vec::alloc returns NULL, and our callers require
7473 that we always return a non-NULL value. The vector code uses
7474 4 when growing a NULL vector, so we do too. */
7475 vec<tree, va_gc> *v;
7476 vec_alloc (v, 4);
7477 return v;
7481 /* Release a vector of trees back to the cache. */
7483 void
7484 release_tree_vector (vec<tree, va_gc> *vec)
7486 if (vec != NULL)
7488 vec->truncate (0);
7489 vec_safe_push (tree_vector_cache, vec);
7493 /* Get a new tree vector holding a single tree. */
7495 vec<tree, va_gc> *
7496 make_tree_vector_single (tree t)
7498 vec<tree, va_gc> *ret = make_tree_vector ();
7499 ret->quick_push (t);
7500 return ret;
7503 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7505 vec<tree, va_gc> *
7506 make_tree_vector_from_list (tree list)
7508 vec<tree, va_gc> *ret = make_tree_vector ();
7509 for (; list; list = TREE_CHAIN (list))
7510 vec_safe_push (ret, TREE_VALUE (list));
7511 return ret;
7514 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7516 vec<tree, va_gc> *
7517 make_tree_vector_from_ctor (tree ctor)
7519 vec<tree,va_gc> *ret = make_tree_vector ();
7520 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7521 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7522 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7523 return ret;
7526 /* Get a new tree vector which is a copy of an existing one. */
7528 vec<tree, va_gc> *
7529 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7531 vec<tree, va_gc> *ret;
7532 unsigned int ix;
7533 tree t;
7535 ret = make_tree_vector ();
7536 vec_safe_reserve (ret, vec_safe_length (orig));
7537 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7538 ret->quick_push (t);
7539 return ret;
7542 /* Return true if KEYWORD starts a type specifier. */
7544 bool
7545 keyword_begins_type_specifier (enum rid keyword)
7547 switch (keyword)
7549 case RID_AUTO_TYPE:
7550 case RID_INT:
7551 case RID_CHAR:
7552 case RID_FLOAT:
7553 case RID_DOUBLE:
7554 case RID_VOID:
7555 case RID_UNSIGNED:
7556 case RID_LONG:
7557 case RID_SHORT:
7558 case RID_SIGNED:
7559 CASE_RID_FLOATN_NX:
7560 case RID_DFLOAT32:
7561 case RID_DFLOAT64:
7562 case RID_DFLOAT128:
7563 case RID_FRACT:
7564 case RID_ACCUM:
7565 case RID_BOOL:
7566 case RID_WCHAR:
7567 case RID_CHAR16:
7568 case RID_CHAR32:
7569 case RID_SAT:
7570 case RID_COMPLEX:
7571 case RID_TYPEOF:
7572 case RID_STRUCT:
7573 case RID_CLASS:
7574 case RID_UNION:
7575 case RID_ENUM:
7576 return true;
7577 default:
7578 if (keyword >= RID_FIRST_INT_N
7579 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7580 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7581 return true;
7582 return false;
7586 /* Return true if KEYWORD names a type qualifier. */
7588 bool
7589 keyword_is_type_qualifier (enum rid keyword)
7591 switch (keyword)
7593 case RID_CONST:
7594 case RID_VOLATILE:
7595 case RID_RESTRICT:
7596 case RID_ATOMIC:
7597 return true;
7598 default:
7599 return false;
7603 /* Return true if KEYWORD names a storage class specifier.
7605 RID_TYPEDEF is not included in this list despite `typedef' being
7606 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7607 such for syntactic convenience only. */
7609 bool
7610 keyword_is_storage_class_specifier (enum rid keyword)
7612 switch (keyword)
7614 case RID_STATIC:
7615 case RID_EXTERN:
7616 case RID_REGISTER:
7617 case RID_AUTO:
7618 case RID_MUTABLE:
7619 case RID_THREAD:
7620 return true;
7621 default:
7622 return false;
7626 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7628 static bool
7629 keyword_is_function_specifier (enum rid keyword)
7631 switch (keyword)
7633 case RID_INLINE:
7634 case RID_NORETURN:
7635 case RID_VIRTUAL:
7636 case RID_EXPLICIT:
7637 return true;
7638 default:
7639 return false;
7643 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7644 declaration-specifier (C99 6.7). */
7646 bool
7647 keyword_is_decl_specifier (enum rid keyword)
7649 if (keyword_is_storage_class_specifier (keyword)
7650 || keyword_is_type_qualifier (keyword)
7651 || keyword_is_function_specifier (keyword))
7652 return true;
7654 switch (keyword)
7656 case RID_TYPEDEF:
7657 case RID_FRIEND:
7658 case RID_CONSTEXPR:
7659 return true;
7660 default:
7661 return false;
7665 /* Initialize language-specific-bits of tree_contains_struct. */
7667 void
7668 c_common_init_ts (void)
7670 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7671 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7672 MARK_TS_TYPED (ARRAY_NOTATION_REF);
7675 /* Build a user-defined numeric literal out of an integer constant type VALUE
7676 with identifier SUFFIX. */
7678 tree
7679 build_userdef_literal (tree suffix_id, tree value,
7680 enum overflow_type overflow, tree num_string)
7682 tree literal = make_node (USERDEF_LITERAL);
7683 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7684 USERDEF_LITERAL_VALUE (literal) = value;
7685 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7686 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7687 return literal;
7690 /* For vector[index], convert the vector to an array of the underlying type.
7691 Return true if the resulting ARRAY_REF should not be an lvalue. */
7693 bool
7694 convert_vector_to_array_for_subscript (location_t loc,
7695 tree *vecp, tree index)
7697 bool ret = false;
7698 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7700 tree type = TREE_TYPE (*vecp);
7702 ret = !lvalue_p (*vecp);
7704 if (TREE_CODE (index) == INTEGER_CST)
7705 if (!tree_fits_uhwi_p (index)
7706 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7707 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7709 /* We are building an ARRAY_REF so mark the vector as addressable
7710 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7711 for function parameters. */
7712 c_common_mark_addressable_vec (*vecp);
7714 *vecp = build1 (VIEW_CONVERT_EXPR,
7715 build_array_type_nelts (TREE_TYPE (type),
7716 TYPE_VECTOR_SUBPARTS (type)),
7717 *vecp);
7719 return ret;
7722 /* Determine which of the operands, if any, is a scalar that needs to be
7723 converted to a vector, for the range of operations. */
7724 enum stv_conv
7725 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7726 bool complain)
7728 tree type0 = TREE_TYPE (op0);
7729 tree type1 = TREE_TYPE (op1);
7730 bool integer_only_op = false;
7731 enum stv_conv ret = stv_firstarg;
7733 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7734 switch (code)
7736 /* Most GENERIC binary expressions require homogeneous arguments.
7737 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7738 argument that is a vector and a second one that is a scalar, so
7739 we never return stv_secondarg for them. */
7740 case RSHIFT_EXPR:
7741 case LSHIFT_EXPR:
7742 if (TREE_CODE (type0) == INTEGER_TYPE
7743 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7745 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7747 if (complain)
7748 error_at (loc, "conversion of scalar %qT to vector %qT "
7749 "involves truncation", type0, type1);
7750 return stv_error;
7752 else
7753 return stv_firstarg;
7755 break;
7757 case BIT_IOR_EXPR:
7758 case BIT_XOR_EXPR:
7759 case BIT_AND_EXPR:
7760 integer_only_op = true;
7761 /* fall through */
7763 case VEC_COND_EXPR:
7765 case PLUS_EXPR:
7766 case MINUS_EXPR:
7767 case MULT_EXPR:
7768 case TRUNC_DIV_EXPR:
7769 case CEIL_DIV_EXPR:
7770 case FLOOR_DIV_EXPR:
7771 case ROUND_DIV_EXPR:
7772 case EXACT_DIV_EXPR:
7773 case TRUNC_MOD_EXPR:
7774 case FLOOR_MOD_EXPR:
7775 case RDIV_EXPR:
7776 case EQ_EXPR:
7777 case NE_EXPR:
7778 case LE_EXPR:
7779 case GE_EXPR:
7780 case LT_EXPR:
7781 case GT_EXPR:
7782 /* What about UNLT_EXPR? */
7783 if (VECTOR_TYPE_P (type0))
7785 ret = stv_secondarg;
7786 std::swap (type0, type1);
7787 std::swap (op0, op1);
7790 if (TREE_CODE (type0) == INTEGER_TYPE
7791 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7793 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7795 if (complain)
7796 error_at (loc, "conversion of scalar %qT to vector %qT "
7797 "involves truncation", type0, type1);
7798 return stv_error;
7800 return ret;
7802 else if (!integer_only_op
7803 /* Allow integer --> real conversion if safe. */
7804 && (TREE_CODE (type0) == REAL_TYPE
7805 || TREE_CODE (type0) == INTEGER_TYPE)
7806 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7808 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7810 if (complain)
7811 error_at (loc, "conversion of scalar %qT to vector %qT "
7812 "involves truncation", type0, type1);
7813 return stv_error;
7815 return ret;
7817 default:
7818 break;
7821 return stv_nothing;
7824 /* Return the alignment of std::max_align_t.
7826 [support.types.layout] The type max_align_t is a POD type whose alignment
7827 requirement is at least as great as that of every scalar type, and whose
7828 alignment requirement is supported in every context. */
7830 unsigned
7831 max_align_t_align ()
7833 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7834 TYPE_ALIGN (long_double_type_node));
7835 if (float128_type_node != NULL_TREE)
7836 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7837 return max_align;
7840 /* Return true iff ALIGN is an integral constant that is a fundamental
7841 alignment, as defined by [basic.align] in the c++-11
7842 specifications.
7844 That is:
7846 [A fundamental alignment is represented by an alignment less than or
7847 equal to the greatest alignment supported by the implementation
7848 in all contexts, which is equal to alignof(max_align_t)]. */
7850 bool
7851 cxx_fundamental_alignment_p (unsigned align)
7853 return (align <= max_align_t_align ());
7856 /* Return true if T is a pointer to a zero-sized aggregate. */
7858 bool
7859 pointer_to_zero_sized_aggr_p (tree t)
7861 if (!POINTER_TYPE_P (t))
7862 return false;
7863 t = TREE_TYPE (t);
7864 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7867 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7868 with no library fallback or for an ADDR_EXPR whose operand is such type
7869 issues an error pointing to the location LOC.
7870 Returns true when the expression has been diagnosed and false
7871 otherwise. */
7873 bool
7874 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7876 if (TREE_CODE (expr) == ADDR_EXPR)
7877 expr = TREE_OPERAND (expr, 0);
7879 if (TREE_TYPE (expr)
7880 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7881 && TREE_CODE (expr) == FUNCTION_DECL
7882 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7883 false positives for user-declared built-ins such as abs or
7884 strlen, and for C++ operators new and delete.
7885 The c_decl_implicit() test avoids false positives for implicitly
7886 declared built-ins with library fallbacks (such as abs). */
7887 && DECL_BUILT_IN (expr)
7888 && DECL_IS_BUILTIN (expr)
7889 && !c_decl_implicit (expr)
7890 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7892 if (loc == UNKNOWN_LOCATION)
7893 loc = EXPR_LOC_OR_LOC (expr, input_location);
7895 /* Reject arguments that are built-in functions with
7896 no library fallback. */
7897 error_at (loc, "built-in function %qE must be directly called", expr);
7899 return true;
7902 return false;
7905 /* Check if array size calculations overflow or if the array covers more
7906 than half of the address space. Return true if the size of the array
7907 is valid, false otherwise. TYPE is the type of the array and NAME is
7908 the name of the array, or NULL_TREE for unnamed arrays. */
7910 bool
7911 valid_array_size_p (location_t loc, tree type, tree name)
7913 if (type != error_mark_node
7914 && COMPLETE_TYPE_P (type)
7915 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7916 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7918 if (name)
7919 error_at (loc, "size of array %qE is too large", name);
7920 else
7921 error_at (loc, "size of unnamed array is too large");
7922 return false;
7924 return true;
7927 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7928 timestamp to replace embedded current dates to get reproducible
7929 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
7931 time_t
7932 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7934 char *source_date_epoch;
7935 int64_t epoch;
7936 char *endptr;
7938 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7939 if (!source_date_epoch)
7940 return (time_t) -1;
7942 errno = 0;
7943 #if defined(INT64_T_IS_LONG)
7944 epoch = strtol (source_date_epoch, &endptr, 10);
7945 #else
7946 epoch = strtoll (source_date_epoch, &endptr, 10);
7947 #endif
7948 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7949 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7951 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
7952 "expand to a non-negative integer less than or equal to %wd",
7953 MAX_SOURCE_DATE_EPOCH);
7954 return (time_t) -1;
7957 return (time_t) epoch;
7960 /* Callback for libcpp for offering spelling suggestions for misspelled
7961 directives. GOAL is an unrecognized string; CANDIDATES is a
7962 NULL-terminated array of candidate strings. Return the closest
7963 match to GOAL within CANDIDATES, or NULL if none are good
7964 suggestions. */
7966 const char *
7967 cb_get_suggestion (cpp_reader *, const char *goal,
7968 const char *const *candidates)
7970 best_match<const char *, const char *> bm (goal);
7971 while (*candidates)
7972 bm.consider (*candidates++);
7973 return bm.get_best_meaningful_candidate ();
7976 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
7977 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
7978 by C TS 18661-3 for interchange types that are computed in their
7979 native precision are larger than the C11 values for evaluating in the
7980 precision of float/double/long double. If either mode is
7981 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
7983 enum flt_eval_method
7984 excess_precision_mode_join (enum flt_eval_method x,
7985 enum flt_eval_method y)
7987 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
7988 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
7989 return FLT_EVAL_METHOD_UNPREDICTABLE;
7991 /* GCC only supports one interchange type right now, _Float16. If
7992 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
7993 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
7994 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7995 return y;
7996 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
7997 return x;
7999 /* Other values for flt_eval_method are directly comparable, and we want
8000 the maximum. */
8001 return MAX (x, y);
8004 /* Return the value that should be set for FLT_EVAL_METHOD in the
8005 context of ISO/IEC TS 18861-3.
8007 This relates to the effective excess precision seen by the user,
8008 which is the join point of the precision the target requests for
8009 -fexcess-precision={standard,fast} and the implicit excess precision
8010 the target uses. */
8012 static enum flt_eval_method
8013 c_ts18661_flt_eval_method (void)
8015 enum flt_eval_method implicit
8016 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8018 enum excess_precision_type flag_type
8019 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8020 ? EXCESS_PRECISION_TYPE_STANDARD
8021 : EXCESS_PRECISION_TYPE_FAST);
8023 enum flt_eval_method requested
8024 = targetm.c.excess_precision (flag_type);
8026 return excess_precision_mode_join (implicit, requested);
8029 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8030 those that were permitted by C11. That is to say, eliminates
8031 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8033 static enum flt_eval_method
8034 c_c11_flt_eval_method (void)
8036 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8037 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8040 /* Return the value that should be set for FLT_EVAL_METHOD.
8041 MAYBE_C11_ONLY_P is TRUE if we should check
8042 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8043 values we can return to those from C99/C11, and FALSE otherwise.
8044 See the comments on c_ts18661_flt_eval_method for what value we choose
8045 to set here. */
8048 c_flt_eval_method (bool maybe_c11_only_p)
8050 if (maybe_c11_only_p
8051 && flag_permitted_flt_eval_methods
8052 == PERMITTED_FLT_EVAL_METHODS_C11)
8053 return c_c11_flt_eval_method ();
8054 else
8055 return c_ts18661_flt_eval_method ();
8058 #include "gt-c-family-c-common.h"