2017-03-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-common.c
blob885ea6d2d70e22c900f3299635b179b3488142ad
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 /* Check that the same argument isn't passed to restrict arguments
5368 and other arguments. */
5370 static void
5371 check_function_restrict (const_tree fndecl, const_tree fntype,
5372 int nargs, tree *argarray)
5374 int i;
5375 tree parms;
5377 if (fndecl
5378 && TREE_CODE (fndecl) == FUNCTION_DECL
5379 && DECL_ARGUMENTS (fndecl))
5380 parms = DECL_ARGUMENTS (fndecl);
5381 else
5382 parms = TYPE_ARG_TYPES (fntype);
5384 for (i = 0; i < nargs; i++)
5385 TREE_VISITED (argarray[i]) = 0;
5387 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5389 tree type;
5390 if (TREE_CODE (parms) == PARM_DECL)
5392 type = TREE_TYPE (parms);
5393 parms = DECL_CHAIN (parms);
5395 else
5397 type = TREE_VALUE (parms);
5398 parms = TREE_CHAIN (parms);
5400 if (POINTER_TYPE_P (type)
5401 && TYPE_RESTRICT (type)
5402 && !TYPE_READONLY (TREE_TYPE (type)))
5403 warn_for_restrict (i, argarray, nargs);
5406 for (i = 0; i < nargs; i++)
5407 TREE_VISITED (argarray[i]) = 0;
5410 /* Helper for check_function_nonnull; given a list of operands which
5411 must be non-null in ARGS, determine if operand PARAM_NUM should be
5412 checked. */
5414 static bool
5415 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5417 unsigned HOST_WIDE_INT arg_num = 0;
5419 for (; args; args = TREE_CHAIN (args))
5421 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5423 gcc_assert (found);
5425 if (arg_num == param_num)
5426 return true;
5428 return false;
5431 /* Check that the function argument PARAM (which is operand number
5432 PARAM_NUM) is non-null. This is called by check_function_nonnull
5433 via check_function_arguments_recurse. */
5435 static void
5436 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5438 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5440 /* Just skip checking the argument if it's not a pointer. This can
5441 happen if the "nonnull" attribute was given without an operand
5442 list (which means to check every pointer argument). */
5444 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5445 return;
5447 /* When not optimizing diagnose the simple cases of null arguments.
5448 When optimization is enabled defer the checking until expansion
5449 when more cases can be detected. */
5450 if (integer_zerop (param))
5452 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5453 "required (argument %lu)", (unsigned long) param_num);
5454 pctx->warned_p = true;
5458 /* Helper for nonnull attribute handling; fetch the operand number
5459 from the attribute argument list. */
5461 bool
5462 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5464 /* Verify the arg number is a small constant. */
5465 if (tree_fits_uhwi_p (arg_num_expr))
5467 *valp = TREE_INT_CST_LOW (arg_num_expr);
5468 return true;
5470 else
5471 return false;
5474 /* Arguments being collected for optimization. */
5475 typedef const char *const_char_p; /* For DEF_VEC_P. */
5476 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5479 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5480 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5481 false for #pragma GCC optimize. */
5483 bool
5484 parse_optimize_options (tree args, bool attr_p)
5486 bool ret = true;
5487 unsigned opt_argc;
5488 unsigned i;
5489 const char **opt_argv;
5490 struct cl_decoded_option *decoded_options;
5491 unsigned int decoded_options_count;
5492 tree ap;
5494 /* Build up argv vector. Just in case the string is stored away, use garbage
5495 collected strings. */
5496 vec_safe_truncate (optimize_args, 0);
5497 vec_safe_push (optimize_args, (const char *) NULL);
5499 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5501 tree value = TREE_VALUE (ap);
5503 if (TREE_CODE (value) == INTEGER_CST)
5505 char buffer[20];
5506 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5507 vec_safe_push (optimize_args, ggc_strdup (buffer));
5510 else if (TREE_CODE (value) == STRING_CST)
5512 /* Split string into multiple substrings. */
5513 size_t len = TREE_STRING_LENGTH (value);
5514 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5515 char *end = p + len;
5516 char *comma;
5517 char *next_p = p;
5519 while (next_p != NULL)
5521 size_t len2;
5522 char *q, *r;
5524 p = next_p;
5525 comma = strchr (p, ',');
5526 if (comma)
5528 len2 = comma - p;
5529 *comma = '\0';
5530 next_p = comma+1;
5532 else
5534 len2 = end - p;
5535 next_p = NULL;
5538 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5540 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5541 options. */
5542 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5544 ret = false;
5545 if (attr_p)
5546 warning (OPT_Wattributes,
5547 "bad option %qs to attribute %<optimize%>", p);
5548 else
5549 warning (OPT_Wpragmas,
5550 "bad option %qs to pragma %<optimize%>", p);
5551 continue;
5554 if (*p != '-')
5556 *r++ = '-';
5558 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5559 itself is -Os, and any other switch begins with a -f. */
5560 if ((*p >= '0' && *p <= '9')
5561 || (p[0] == 's' && p[1] == '\0'))
5562 *r++ = 'O';
5563 else if (*p != 'O')
5564 *r++ = 'f';
5567 memcpy (r, p, len2);
5568 r[len2] = '\0';
5569 vec_safe_push (optimize_args, (const char *) q);
5575 opt_argc = optimize_args->length ();
5576 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5578 for (i = 1; i < opt_argc; i++)
5579 opt_argv[i] = (*optimize_args)[i];
5581 /* Now parse the options. */
5582 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5583 &decoded_options,
5584 &decoded_options_count);
5585 /* Drop non-Optimization options. */
5586 unsigned j = 1;
5587 for (i = 1; i < decoded_options_count; ++i)
5589 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5591 ret = false;
5592 if (attr_p)
5593 warning (OPT_Wattributes,
5594 "bad option %qs to attribute %<optimize%>",
5595 decoded_options[i].orig_option_with_args_text);
5596 else
5597 warning (OPT_Wpragmas,
5598 "bad option %qs to pragma %<optimize%>",
5599 decoded_options[i].orig_option_with_args_text);
5600 continue;
5602 if (i != j)
5603 decoded_options[j] = decoded_options[i];
5604 j++;
5606 decoded_options_count = j;
5607 /* And apply them. */
5608 decode_options (&global_options, &global_options_set,
5609 decoded_options, decoded_options_count,
5610 input_location, global_dc);
5612 targetm.override_options_after_change();
5614 optimize_args->truncate (0);
5615 return ret;
5618 /* Check whether ATTR is a valid attribute fallthrough. */
5620 bool
5621 attribute_fallthrough_p (tree attr)
5623 if (attr == error_mark_node)
5624 return false;
5625 tree t = lookup_attribute ("fallthrough", attr);
5626 if (t == NULL_TREE)
5627 return false;
5628 /* This attribute shall appear at most once in each attribute-list. */
5629 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5630 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5631 "times");
5632 /* No attribute-argument-clause shall be present. */
5633 else if (TREE_VALUE (t) != NULL_TREE)
5634 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5635 "a parameter");
5636 /* Warn if other attributes are found. */
5637 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5639 tree name = get_attribute_name (t);
5640 if (!is_attribute_p ("fallthrough", name))
5641 warning (OPT_Wattributes, "%qE attribute ignored", name);
5643 return true;
5647 /* Check for valid arguments being passed to a function with FNTYPE.
5648 There are NARGS arguments in the array ARGARRAY. LOC should be used for
5649 diagnostics. Return true if -Wnonnull warning has been diagnosed. */
5650 bool
5651 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5652 int nargs, tree *argarray)
5654 bool warned_p = false;
5656 /* Check for null being passed in a pointer argument that must be
5657 non-null. We also need to do this if format checking is enabled. */
5659 if (warn_nonnull)
5660 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5661 nargs, argarray);
5663 /* Check for errors in format strings. */
5665 if (warn_format || warn_suggest_attribute_format)
5666 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
5668 if (warn_format)
5669 check_function_sentinel (fntype, nargs, argarray);
5671 if (warn_restrict)
5672 check_function_restrict (fndecl, fntype, nargs, argarray);
5673 return warned_p;
5676 /* Generic argument checking recursion routine. PARAM is the argument to
5677 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5678 once the argument is resolved. CTX is context for the callback. */
5679 void
5680 check_function_arguments_recurse (void (*callback)
5681 (void *, tree, unsigned HOST_WIDE_INT),
5682 void *ctx, tree param,
5683 unsigned HOST_WIDE_INT param_num)
5685 if (CONVERT_EXPR_P (param)
5686 && (TYPE_PRECISION (TREE_TYPE (param))
5687 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5689 /* Strip coercion. */
5690 check_function_arguments_recurse (callback, ctx,
5691 TREE_OPERAND (param, 0), param_num);
5692 return;
5695 if (TREE_CODE (param) == CALL_EXPR)
5697 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5698 tree attrs;
5699 bool found_format_arg = false;
5701 /* See if this is a call to a known internationalization function
5702 that modifies a format arg. Such a function may have multiple
5703 format_arg attributes (for example, ngettext). */
5705 for (attrs = TYPE_ATTRIBUTES (type);
5706 attrs;
5707 attrs = TREE_CHAIN (attrs))
5708 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5710 tree inner_arg;
5711 tree format_num_expr;
5712 int format_num;
5713 int i;
5714 call_expr_arg_iterator iter;
5716 /* Extract the argument number, which was previously checked
5717 to be valid. */
5718 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5720 format_num = tree_to_uhwi (format_num_expr);
5722 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5723 inner_arg != 0;
5724 inner_arg = next_call_expr_arg (&iter), i++)
5725 if (i == format_num)
5727 check_function_arguments_recurse (callback, ctx,
5728 inner_arg, param_num);
5729 found_format_arg = true;
5730 break;
5734 /* If we found a format_arg attribute and did a recursive check,
5735 we are done with checking this argument. Otherwise, we continue
5736 and this will be considered a non-literal. */
5737 if (found_format_arg)
5738 return;
5741 if (TREE_CODE (param) == COND_EXPR)
5743 /* Simplify to avoid warning for an impossible case. */
5744 param = fold_for_warn (param);
5745 if (TREE_CODE (param) == COND_EXPR)
5747 /* Check both halves of the conditional expression. */
5748 check_function_arguments_recurse (callback, ctx,
5749 TREE_OPERAND (param, 1),
5750 param_num);
5751 check_function_arguments_recurse (callback, ctx,
5752 TREE_OPERAND (param, 2),
5753 param_num);
5754 return;
5758 (*callback) (ctx, param, param_num);
5761 /* Checks for a builtin function FNDECL that the number of arguments
5762 NARGS against the required number REQUIRED and issues an error if
5763 there is a mismatch. Returns true if the number of arguments is
5764 correct, otherwise false. LOC is the location of FNDECL. */
5766 static bool
5767 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5768 int required)
5770 if (nargs < required)
5772 error_at (loc, "too few arguments to function %qE", fndecl);
5773 return false;
5775 else if (nargs > required)
5777 error_at (loc, "too many arguments to function %qE", fndecl);
5778 return false;
5780 return true;
5783 /* Helper macro for check_builtin_function_arguments. */
5784 #define ARG_LOCATION(N) \
5785 (arg_loc.is_empty () \
5786 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5787 : expansion_point_location (arg_loc[(N)]))
5789 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5790 Returns false if there was an error, otherwise true. LOC is the
5791 location of the function; ARG_LOC is a vector of locations of the
5792 arguments. */
5794 bool
5795 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5796 tree fndecl, int nargs, tree *args)
5798 if (!DECL_BUILT_IN (fndecl)
5799 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5800 return true;
5802 switch (DECL_FUNCTION_CODE (fndecl))
5804 case BUILT_IN_ALLOCA_WITH_ALIGN:
5806 /* Get the requested alignment (in bits) if it's a constant
5807 integer expression. */
5808 unsigned HOST_WIDE_INT align
5809 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5811 /* Determine if the requested alignment is a power of 2. */
5812 if ((align & (align - 1)))
5813 align = 0;
5815 /* The maximum alignment in bits corresponding to the same
5816 maximum in bytes enforced in check_user_alignment(). */
5817 unsigned maxalign = (UINT_MAX >> 1) + 1;
5819 /* Reject invalid alignments. */
5820 if (align < BITS_PER_UNIT || maxalign < align)
5822 error_at (ARG_LOCATION (1),
5823 "second argument to function %qE must be a constant "
5824 "integer power of 2 between %qi and %qu bits",
5825 fndecl, BITS_PER_UNIT, maxalign);
5826 return false;
5828 return true;
5831 case BUILT_IN_CONSTANT_P:
5832 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5834 case BUILT_IN_ISFINITE:
5835 case BUILT_IN_ISINF:
5836 case BUILT_IN_ISINF_SIGN:
5837 case BUILT_IN_ISNAN:
5838 case BUILT_IN_ISNORMAL:
5839 case BUILT_IN_SIGNBIT:
5840 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5842 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5844 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5845 "call to function %qE", fndecl);
5846 return false;
5848 return true;
5850 return false;
5852 case BUILT_IN_ISGREATER:
5853 case BUILT_IN_ISGREATEREQUAL:
5854 case BUILT_IN_ISLESS:
5855 case BUILT_IN_ISLESSEQUAL:
5856 case BUILT_IN_ISLESSGREATER:
5857 case BUILT_IN_ISUNORDERED:
5858 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5860 enum tree_code code0, code1;
5861 code0 = TREE_CODE (TREE_TYPE (args[0]));
5862 code1 = TREE_CODE (TREE_TYPE (args[1]));
5863 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5864 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5865 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5867 error_at (loc, "non-floating-point arguments in call to "
5868 "function %qE", fndecl);
5869 return false;
5871 return true;
5873 return false;
5875 case BUILT_IN_FPCLASSIFY:
5876 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5878 for (unsigned int i = 0; i < 5; i++)
5879 if (TREE_CODE (args[i]) != INTEGER_CST)
5881 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5882 "call to function %qE", i + 1, fndecl);
5883 return false;
5886 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5888 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5889 "call to function %qE", fndecl);
5890 return false;
5892 return true;
5894 return false;
5896 case BUILT_IN_ASSUME_ALIGNED:
5897 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5899 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5901 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5902 "function %qE", fndecl);
5903 return false;
5905 return true;
5907 return false;
5909 case BUILT_IN_ADD_OVERFLOW:
5910 case BUILT_IN_SUB_OVERFLOW:
5911 case BUILT_IN_MUL_OVERFLOW:
5912 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5914 unsigned i;
5915 for (i = 0; i < 2; i++)
5916 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5918 error_at (ARG_LOCATION (i), "argument %u in call to function "
5919 "%qE does not have integral type", i + 1, fndecl);
5920 return false;
5922 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5923 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5925 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5926 "does not have pointer to integral type", fndecl);
5927 return false;
5929 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5931 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5932 "has pointer to enumerated type", fndecl);
5933 return false;
5935 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5937 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5938 "has pointer to boolean type", fndecl);
5939 return false;
5941 return true;
5943 return false;
5945 case BUILT_IN_ADD_OVERFLOW_P:
5946 case BUILT_IN_SUB_OVERFLOW_P:
5947 case BUILT_IN_MUL_OVERFLOW_P:
5948 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5950 unsigned i;
5951 for (i = 0; i < 3; i++)
5952 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5954 error_at (ARG_LOCATION (i), "argument %u in call to function "
5955 "%qE does not have integral type", i + 1, fndecl);
5956 return false;
5958 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5960 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5961 "%qE has enumerated type", fndecl);
5962 return false;
5964 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5966 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5967 "%qE has boolean type", fndecl);
5968 return false;
5970 return true;
5972 return false;
5974 default:
5975 return true;
5979 /* Function to help qsort sort FIELD_DECLs by name order. */
5982 field_decl_cmp (const void *x_p, const void *y_p)
5984 const tree *const x = (const tree *const) x_p;
5985 const tree *const y = (const tree *const) y_p;
5987 if (DECL_NAME (*x) == DECL_NAME (*y))
5988 /* A nontype is "greater" than a type. */
5989 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5990 if (DECL_NAME (*x) == NULL_TREE)
5991 return -1;
5992 if (DECL_NAME (*y) == NULL_TREE)
5993 return 1;
5994 if (DECL_NAME (*x) < DECL_NAME (*y))
5995 return -1;
5996 return 1;
5999 static struct {
6000 gt_pointer_operator new_value;
6001 void *cookie;
6002 } resort_data;
6004 /* This routine compares two fields like field_decl_cmp but using the
6005 pointer operator in resort_data. */
6007 static int
6008 resort_field_decl_cmp (const void *x_p, const void *y_p)
6010 const tree *const x = (const tree *const) x_p;
6011 const tree *const y = (const tree *const) y_p;
6013 if (DECL_NAME (*x) == DECL_NAME (*y))
6014 /* A nontype is "greater" than a type. */
6015 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
6016 if (DECL_NAME (*x) == NULL_TREE)
6017 return -1;
6018 if (DECL_NAME (*y) == NULL_TREE)
6019 return 1;
6021 tree d1 = DECL_NAME (*x);
6022 tree d2 = DECL_NAME (*y);
6023 resort_data.new_value (&d1, resort_data.cookie);
6024 resort_data.new_value (&d2, resort_data.cookie);
6025 if (d1 < d2)
6026 return -1;
6028 return 1;
6031 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
6033 void
6034 resort_sorted_fields (void *obj,
6035 void * ARG_UNUSED (orig_obj),
6036 gt_pointer_operator new_value,
6037 void *cookie)
6039 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
6040 resort_data.new_value = new_value;
6041 resort_data.cookie = cookie;
6042 qsort (&sf->elts[0], sf->len, sizeof (tree),
6043 resort_field_decl_cmp);
6046 /* Subroutine of c_parse_error.
6047 Return the result of concatenating LHS and RHS. RHS is really
6048 a string literal, its first character is indicated by RHS_START and
6049 RHS_SIZE is its length (including the terminating NUL character).
6051 The caller is responsible for deleting the returned pointer. */
6053 static char *
6054 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6056 const int lhs_size = strlen (lhs);
6057 char *result = XNEWVEC (char, lhs_size + rhs_size);
6058 strncpy (result, lhs, lhs_size);
6059 strncpy (result + lhs_size, rhs_start, rhs_size);
6060 return result;
6063 /* Issue the error given by GMSGID, indicating that it occurred before
6064 TOKEN, which had the associated VALUE. */
6066 void
6067 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6068 tree value, unsigned char token_flags)
6070 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6072 char *message = NULL;
6074 if (token_type == CPP_EOF)
6075 message = catenate_messages (gmsgid, " at end of input");
6076 else if (token_type == CPP_CHAR
6077 || token_type == CPP_WCHAR
6078 || token_type == CPP_CHAR16
6079 || token_type == CPP_CHAR32
6080 || token_type == CPP_UTF8CHAR)
6082 unsigned int val = TREE_INT_CST_LOW (value);
6083 const char *prefix;
6085 switch (token_type)
6087 default:
6088 prefix = "";
6089 break;
6090 case CPP_WCHAR:
6091 prefix = "L";
6092 break;
6093 case CPP_CHAR16:
6094 prefix = "u";
6095 break;
6096 case CPP_CHAR32:
6097 prefix = "U";
6098 break;
6099 case CPP_UTF8CHAR:
6100 prefix = "u8";
6101 break;
6104 if (val <= UCHAR_MAX && ISGRAPH (val))
6105 message = catenate_messages (gmsgid, " before %s'%c'");
6106 else
6107 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6109 error (message, prefix, val);
6110 free (message);
6111 message = NULL;
6113 else if (token_type == CPP_CHAR_USERDEF
6114 || token_type == CPP_WCHAR_USERDEF
6115 || token_type == CPP_CHAR16_USERDEF
6116 || token_type == CPP_CHAR32_USERDEF
6117 || token_type == CPP_UTF8CHAR_USERDEF)
6118 message = catenate_messages (gmsgid,
6119 " before user-defined character literal");
6120 else if (token_type == CPP_STRING_USERDEF
6121 || token_type == CPP_WSTRING_USERDEF
6122 || token_type == CPP_STRING16_USERDEF
6123 || token_type == CPP_STRING32_USERDEF
6124 || token_type == CPP_UTF8STRING_USERDEF)
6125 message = catenate_messages (gmsgid, " before user-defined string literal");
6126 else if (token_type == CPP_STRING
6127 || token_type == CPP_WSTRING
6128 || token_type == CPP_STRING16
6129 || token_type == CPP_STRING32
6130 || token_type == CPP_UTF8STRING)
6131 message = catenate_messages (gmsgid, " before string constant");
6132 else if (token_type == CPP_NUMBER)
6133 message = catenate_messages (gmsgid, " before numeric constant");
6134 else if (token_type == CPP_NAME)
6136 message = catenate_messages (gmsgid, " before %qE");
6137 error (message, value);
6138 free (message);
6139 message = NULL;
6141 else if (token_type == CPP_PRAGMA)
6142 message = catenate_messages (gmsgid, " before %<#pragma%>");
6143 else if (token_type == CPP_PRAGMA_EOL)
6144 message = catenate_messages (gmsgid, " before end of line");
6145 else if (token_type == CPP_DECLTYPE)
6146 message = catenate_messages (gmsgid, " before %<decltype%>");
6147 else if (token_type < N_TTYPES)
6149 message = catenate_messages (gmsgid, " before %qs token");
6150 error (message, cpp_type2name (token_type, token_flags));
6151 free (message);
6152 message = NULL;
6154 else
6155 error (gmsgid);
6157 if (message)
6159 error (message);
6160 free (message);
6162 #undef catenate_messages
6165 /* Return the gcc option code associated with the reason for a cpp
6166 message, or 0 if none. */
6168 static int
6169 c_option_controlling_cpp_error (int reason)
6171 const struct cpp_reason_option_codes_t *entry;
6173 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6175 if (entry->reason == reason)
6176 return entry->option_code;
6178 return 0;
6181 /* Callback from cpp_error for PFILE to print diagnostics from the
6182 preprocessor. The diagnostic is of type LEVEL, with REASON set
6183 to the reason code if LEVEL is represents a warning, at location
6184 RICHLOC unless this is after lexing and the compiler's location
6185 should be used instead; MSG is the translated message and AP
6186 the arguments. Returns true if a diagnostic was emitted, false
6187 otherwise. */
6189 bool
6190 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6191 rich_location *richloc,
6192 const char *msg, va_list *ap)
6194 diagnostic_info diagnostic;
6195 diagnostic_t dlevel;
6196 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6197 bool ret;
6199 switch (level)
6201 case CPP_DL_WARNING_SYSHDR:
6202 if (flag_no_output)
6203 return false;
6204 global_dc->dc_warn_system_headers = 1;
6205 /* Fall through. */
6206 case CPP_DL_WARNING:
6207 if (flag_no_output)
6208 return false;
6209 dlevel = DK_WARNING;
6210 break;
6211 case CPP_DL_PEDWARN:
6212 if (flag_no_output && !flag_pedantic_errors)
6213 return false;
6214 dlevel = DK_PEDWARN;
6215 break;
6216 case CPP_DL_ERROR:
6217 dlevel = DK_ERROR;
6218 break;
6219 case CPP_DL_ICE:
6220 dlevel = DK_ICE;
6221 break;
6222 case CPP_DL_NOTE:
6223 dlevel = DK_NOTE;
6224 break;
6225 case CPP_DL_FATAL:
6226 dlevel = DK_FATAL;
6227 break;
6228 default:
6229 gcc_unreachable ();
6231 if (done_lexing)
6232 richloc->set_range (line_table, 0, input_location, true);
6233 diagnostic_set_info_translated (&diagnostic, msg, ap,
6234 richloc, dlevel);
6235 diagnostic_override_option_index (&diagnostic,
6236 c_option_controlling_cpp_error (reason));
6237 ret = report_diagnostic (&diagnostic);
6238 if (level == CPP_DL_WARNING_SYSHDR)
6239 global_dc->dc_warn_system_headers = save_warn_system_headers;
6240 return ret;
6243 /* Convert a character from the host to the target execution character
6244 set. cpplib handles this, mostly. */
6246 HOST_WIDE_INT
6247 c_common_to_target_charset (HOST_WIDE_INT c)
6249 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6250 zero-extended under -fno-signed-char. cpplib insists that characters
6251 and character constants are always unsigned. Hence we must convert
6252 back and forth. */
6253 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6255 uc = cpp_host_to_exec_charset (parse_in, uc);
6257 if (flag_signed_char)
6258 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6259 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6260 else
6261 return uc;
6264 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6265 references with an INDIRECT_REF of a constant at the bottom; much like the
6266 traditional rendering of offsetof as a macro. Return the folded result. */
6268 tree
6269 fold_offsetof_1 (tree expr, enum tree_code ctx)
6271 tree base, off, t;
6272 tree_code code = TREE_CODE (expr);
6273 switch (code)
6275 case ERROR_MARK:
6276 return expr;
6278 case VAR_DECL:
6279 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6280 return error_mark_node;
6282 case CALL_EXPR:
6283 case TARGET_EXPR:
6284 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6285 return error_mark_node;
6287 case NOP_EXPR:
6288 case INDIRECT_REF:
6289 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6291 error ("cannot apply %<offsetof%> to a non constant address");
6292 return error_mark_node;
6294 return TREE_OPERAND (expr, 0);
6296 case COMPONENT_REF:
6297 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6298 if (base == error_mark_node)
6299 return base;
6301 t = TREE_OPERAND (expr, 1);
6302 if (DECL_C_BIT_FIELD (t))
6304 error ("attempt to take address of bit-field structure "
6305 "member %qD", t);
6306 return error_mark_node;
6308 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6309 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6310 / BITS_PER_UNIT));
6311 break;
6313 case ARRAY_REF:
6314 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6315 if (base == error_mark_node)
6316 return base;
6318 t = TREE_OPERAND (expr, 1);
6320 /* Check if the offset goes beyond the upper bound of the array. */
6321 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6323 tree upbound = array_ref_up_bound (expr);
6324 if (upbound != NULL_TREE
6325 && TREE_CODE (upbound) == INTEGER_CST
6326 && !tree_int_cst_equal (upbound,
6327 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6329 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6330 upbound = size_binop (PLUS_EXPR, upbound,
6331 build_int_cst (TREE_TYPE (upbound), 1));
6332 if (tree_int_cst_lt (upbound, t))
6334 tree v;
6336 for (v = TREE_OPERAND (expr, 0);
6337 TREE_CODE (v) == COMPONENT_REF;
6338 v = TREE_OPERAND (v, 0))
6339 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6340 == RECORD_TYPE)
6342 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6343 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6344 if (TREE_CODE (fld_chain) == FIELD_DECL)
6345 break;
6347 if (fld_chain)
6348 break;
6350 /* Don't warn if the array might be considered a poor
6351 man's flexible array member with a very permissive
6352 definition thereof. */
6353 if (TREE_CODE (v) == ARRAY_REF
6354 || TREE_CODE (v) == COMPONENT_REF)
6355 warning (OPT_Warray_bounds,
6356 "index %E denotes an offset "
6357 "greater than size of %qT",
6358 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6363 t = convert (sizetype, t);
6364 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6365 break;
6367 case COMPOUND_EXPR:
6368 /* Handle static members of volatile structs. */
6369 t = TREE_OPERAND (expr, 1);
6370 gcc_assert (VAR_P (t));
6371 return fold_offsetof_1 (t);
6373 default:
6374 gcc_unreachable ();
6377 return fold_build_pointer_plus (base, off);
6380 /* Likewise, but convert it to the return type of offsetof. */
6382 tree
6383 fold_offsetof (tree expr)
6385 return convert (size_type_node, fold_offsetof_1 (expr));
6389 /* *PTYPE is an incomplete array. Complete it with a domain based on
6390 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6391 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6392 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6395 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6397 tree maxindex, type, main_type, elt, unqual_elt;
6398 int failure = 0, quals;
6399 hashval_t hashcode = 0;
6400 bool overflow_p = false;
6402 maxindex = size_zero_node;
6403 if (initial_value)
6405 if (TREE_CODE (initial_value) == STRING_CST)
6407 int eltsize
6408 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6409 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6411 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6413 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6415 if (vec_safe_is_empty (v))
6417 if (pedantic)
6418 failure = 3;
6419 maxindex = ssize_int (-1);
6421 else
6423 tree curindex;
6424 unsigned HOST_WIDE_INT cnt;
6425 constructor_elt *ce;
6426 bool fold_p = false;
6428 if ((*v)[0].index)
6429 maxindex = (*v)[0].index, fold_p = true;
6431 curindex = maxindex;
6433 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6435 bool curfold_p = false;
6436 if (ce->index)
6437 curindex = ce->index, curfold_p = true;
6438 else
6440 if (fold_p)
6442 /* Since we treat size types now as ordinary
6443 unsigned types, we need an explicit overflow
6444 check. */
6445 tree orig = curindex;
6446 curindex = fold_convert (sizetype, curindex);
6447 overflow_p |= tree_int_cst_lt (curindex, orig);
6449 curindex = size_binop (PLUS_EXPR, curindex,
6450 size_one_node);
6452 if (tree_int_cst_lt (maxindex, curindex))
6453 maxindex = curindex, fold_p = curfold_p;
6455 if (fold_p)
6457 tree orig = maxindex;
6458 maxindex = fold_convert (sizetype, maxindex);
6459 overflow_p |= tree_int_cst_lt (maxindex, orig);
6463 else
6465 /* Make an error message unless that happened already. */
6466 if (initial_value != error_mark_node)
6467 failure = 1;
6470 else
6472 failure = 2;
6473 if (!do_default)
6474 return failure;
6477 type = *ptype;
6478 elt = TREE_TYPE (type);
6479 quals = TYPE_QUALS (strip_array_types (elt));
6480 if (quals == 0)
6481 unqual_elt = elt;
6482 else
6483 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6485 /* Using build_distinct_type_copy and modifying things afterward instead
6486 of using build_array_type to create a new type preserves all of the
6487 TYPE_LANG_FLAG_? bits that the front end may have set. */
6488 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6489 TREE_TYPE (main_type) = unqual_elt;
6490 TYPE_DOMAIN (main_type)
6491 = build_range_type (TREE_TYPE (maxindex),
6492 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6493 layout_type (main_type);
6495 /* Make sure we have the canonical MAIN_TYPE. */
6496 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
6497 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6498 hashcode);
6499 main_type = type_hash_canon (hashcode, main_type);
6501 /* Fix the canonical type. */
6502 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6503 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6504 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6505 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6506 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6507 != TYPE_DOMAIN (main_type)))
6508 TYPE_CANONICAL (main_type)
6509 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6510 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
6511 else
6512 TYPE_CANONICAL (main_type) = main_type;
6514 if (quals == 0)
6515 type = main_type;
6516 else
6517 type = c_build_qualified_type (main_type, quals);
6519 if (COMPLETE_TYPE_P (type)
6520 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6521 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6523 error ("size of array is too large");
6524 /* If we proceed with the array type as it is, we'll eventually
6525 crash in tree_to_[su]hwi(). */
6526 type = error_mark_node;
6529 *ptype = type;
6530 return failure;
6533 /* Like c_mark_addressable but don't check register qualifier. */
6534 void
6535 c_common_mark_addressable_vec (tree t)
6537 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6538 t = C_MAYBE_CONST_EXPR_EXPR (t);
6539 while (handled_component_p (t))
6540 t = TREE_OPERAND (t, 0);
6541 if (!VAR_P (t)
6542 && TREE_CODE (t) != PARM_DECL
6543 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6544 return;
6545 TREE_ADDRESSABLE (t) = 1;
6550 /* Used to help initialize the builtin-types.def table. When a type of
6551 the correct size doesn't exist, use error_mark_node instead of NULL.
6552 The later results in segfaults even when a decl using the type doesn't
6553 get invoked. */
6555 tree
6556 builtin_type_for_size (int size, bool unsignedp)
6558 tree type = c_common_type_for_size (size, unsignedp);
6559 return type ? type : error_mark_node;
6562 /* A helper function for resolve_overloaded_builtin in resolving the
6563 overloaded __sync_ builtins. Returns a positive power of 2 if the
6564 first operand of PARAMS is a pointer to a supported data type.
6565 Returns 0 if an error is encountered.
6566 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6567 built-ins. */
6569 static int
6570 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6572 /* Type of the argument. */
6573 tree argtype;
6574 /* Type the argument points to. */
6575 tree type;
6576 int size;
6578 if (vec_safe_is_empty (params))
6580 error ("too few arguments to function %qE", function);
6581 return 0;
6584 argtype = type = TREE_TYPE ((*params)[0]);
6585 if (TREE_CODE (type) == ARRAY_TYPE)
6587 /* Force array-to-pointer decay for C++. */
6588 gcc_assert (c_dialect_cxx());
6589 (*params)[0] = default_conversion ((*params)[0]);
6590 type = TREE_TYPE ((*params)[0]);
6592 if (TREE_CODE (type) != POINTER_TYPE)
6593 goto incompatible;
6595 type = TREE_TYPE (type);
6596 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6597 goto incompatible;
6599 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6600 goto incompatible;
6602 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6603 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6604 return size;
6606 incompatible:
6607 /* Issue the diagnostic only if the argument is valid, otherwise
6608 it would be redundant at best and could be misleading. */
6609 if (argtype != error_mark_node)
6610 error ("operand type %qT is incompatible with argument %d of %qE",
6611 argtype, 1, function);
6612 return 0;
6615 /* A helper function for resolve_overloaded_builtin. Adds casts to
6616 PARAMS to make arguments match up with those of FUNCTION. Drops
6617 the variadic arguments at the end. Returns false if some error
6618 was encountered; true on success. */
6620 static bool
6621 sync_resolve_params (location_t loc, tree orig_function, tree function,
6622 vec<tree, va_gc> *params, bool orig_format)
6624 function_args_iterator iter;
6625 tree ptype;
6626 unsigned int parmnum;
6628 function_args_iter_init (&iter, TREE_TYPE (function));
6629 /* We've declared the implementation functions to use "volatile void *"
6630 as the pointer parameter, so we shouldn't get any complaints from the
6631 call to check_function_arguments what ever type the user used. */
6632 function_args_iter_next (&iter);
6633 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6634 ptype = TYPE_MAIN_VARIANT (ptype);
6636 /* For the rest of the values, we need to cast these to FTYPE, so that we
6637 don't get warnings for passing pointer types, etc. */
6638 parmnum = 0;
6639 while (1)
6641 tree val, arg_type;
6643 arg_type = function_args_iter_cond (&iter);
6644 /* XXX void_type_node belies the abstraction. */
6645 if (arg_type == void_type_node)
6646 break;
6648 ++parmnum;
6649 if (params->length () <= parmnum)
6651 error_at (loc, "too few arguments to function %qE", orig_function);
6652 return false;
6655 /* Only convert parameters if arg_type is unsigned integer type with
6656 new format sync routines, i.e. don't attempt to convert pointer
6657 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6658 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6659 kinds). */
6660 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6662 /* Ideally for the first conversion we'd use convert_for_assignment
6663 so that we get warnings for anything that doesn't match the pointer
6664 type. This isn't portable across the C and C++ front ends atm. */
6665 val = (*params)[parmnum];
6666 val = convert (ptype, val);
6667 val = convert (arg_type, val);
6668 (*params)[parmnum] = val;
6671 function_args_iter_next (&iter);
6674 /* __atomic routines are not variadic. */
6675 if (!orig_format && params->length () != parmnum + 1)
6677 error_at (loc, "too many arguments to function %qE", orig_function);
6678 return false;
6681 /* The definition of these primitives is variadic, with the remaining
6682 being "an optional list of variables protected by the memory barrier".
6683 No clue what that's supposed to mean, precisely, but we consider all
6684 call-clobbered variables to be protected so we're safe. */
6685 params->truncate (parmnum + 1);
6687 return true;
6690 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6691 RESULT to make it match the type of the first pointer argument in
6692 PARAMS. */
6694 static tree
6695 sync_resolve_return (tree first_param, tree result, bool orig_format)
6697 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6698 tree rtype = TREE_TYPE (result);
6699 ptype = TYPE_MAIN_VARIANT (ptype);
6701 /* New format doesn't require casting unless the types are the same size. */
6702 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6703 return convert (ptype, result);
6704 else
6705 return result;
6708 /* This function verifies the PARAMS to generic atomic FUNCTION.
6709 It returns the size if all the parameters are the same size, otherwise
6710 0 is returned if the parameters are invalid. */
6712 static int
6713 get_atomic_generic_size (location_t loc, tree function,
6714 vec<tree, va_gc> *params)
6716 unsigned int n_param;
6717 unsigned int n_model;
6718 unsigned int x;
6719 int size_0;
6720 tree type_0;
6722 /* Determine the parameter makeup. */
6723 switch (DECL_FUNCTION_CODE (function))
6725 case BUILT_IN_ATOMIC_EXCHANGE:
6726 n_param = 4;
6727 n_model = 1;
6728 break;
6729 case BUILT_IN_ATOMIC_LOAD:
6730 case BUILT_IN_ATOMIC_STORE:
6731 n_param = 3;
6732 n_model = 1;
6733 break;
6734 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6735 n_param = 6;
6736 n_model = 2;
6737 break;
6738 default:
6739 gcc_unreachable ();
6742 if (vec_safe_length (params) != n_param)
6744 error_at (loc, "incorrect number of arguments to function %qE", function);
6745 return 0;
6748 /* Get type of first parameter, and determine its size. */
6749 type_0 = TREE_TYPE ((*params)[0]);
6750 if (TREE_CODE (type_0) == ARRAY_TYPE)
6752 /* Force array-to-pointer decay for C++. */
6753 gcc_assert (c_dialect_cxx());
6754 (*params)[0] = default_conversion ((*params)[0]);
6755 type_0 = TREE_TYPE ((*params)[0]);
6757 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6759 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6760 function);
6761 return 0;
6764 /* Types must be compile time constant sizes. */
6765 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6767 error_at (loc,
6768 "argument 1 of %qE must be a pointer to a constant size type",
6769 function);
6770 return 0;
6773 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6775 /* Zero size objects are not allowed. */
6776 if (size_0 == 0)
6778 error_at (loc,
6779 "argument 1 of %qE must be a pointer to a nonzero size object",
6780 function);
6781 return 0;
6784 /* Check each other parameter is a pointer and the same size. */
6785 for (x = 0; x < n_param - n_model; x++)
6787 int size;
6788 tree type = TREE_TYPE ((*params)[x]);
6789 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6790 if (n_param == 6 && x == 3)
6791 continue;
6792 if (!POINTER_TYPE_P (type))
6794 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6795 function);
6796 return 0;
6798 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6799 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6800 != INTEGER_CST)
6802 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6803 "size type", x + 1, function);
6804 return 0;
6806 else if (FUNCTION_POINTER_TYPE_P (type))
6808 error_at (loc, "argument %d of %qE must not be a pointer to a "
6809 "function", x + 1, function);
6810 return 0;
6812 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6813 size = type_size ? tree_to_uhwi (type_size) : 0;
6814 if (size != size_0)
6816 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6817 function);
6818 return 0;
6822 /* Check memory model parameters for validity. */
6823 for (x = n_param - n_model ; x < n_param; x++)
6825 tree p = (*params)[x];
6826 if (TREE_CODE (p) == INTEGER_CST)
6828 int i = tree_to_uhwi (p);
6829 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
6831 warning_at (loc, OPT_Winvalid_memory_model,
6832 "invalid memory model argument %d of %qE", x + 1,
6833 function);
6836 else
6837 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6839 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6840 function);
6841 return 0;
6845 return size_0;
6849 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6850 at the beginning of the parameter list PARAMS representing the size of the
6851 objects. This is to match the library ABI requirement. LOC is the location
6852 of the function call.
6853 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6854 returned to allow the external call to be constructed. */
6856 static tree
6857 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6858 vec<tree, va_gc> *params)
6860 tree size_node;
6862 /* Insert a SIZE_T parameter as the first param. If there isn't
6863 enough space, allocate a new vector and recursively re-build with that. */
6864 if (!params->space (1))
6866 unsigned int z, len;
6867 vec<tree, va_gc> *v;
6868 tree f;
6870 len = params->length ();
6871 vec_alloc (v, len + 1);
6872 v->quick_push (build_int_cst (size_type_node, n));
6873 for (z = 0; z < len; z++)
6874 v->quick_push ((*params)[z]);
6875 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6876 vec_free (v);
6877 return f;
6880 /* Add the size parameter and leave as a function call for processing. */
6881 size_node = build_int_cst (size_type_node, n);
6882 params->quick_insert (0, size_node);
6883 return NULL_TREE;
6887 /* Return whether atomic operations for naturally aligned N-byte
6888 arguments are supported, whether inline or through libatomic. */
6889 static bool
6890 atomic_size_supported_p (int n)
6892 switch (n)
6894 case 1:
6895 case 2:
6896 case 4:
6897 case 8:
6898 return true;
6900 case 16:
6901 return targetm.scalar_mode_supported_p (TImode);
6903 default:
6904 return false;
6908 /* This will process an __atomic_exchange function call, determine whether it
6909 needs to be mapped to the _N variation, or turned into a library call.
6910 LOC is the location of the builtin call.
6911 FUNCTION is the DECL that has been invoked;
6912 PARAMS is the argument list for the call. The return value is non-null
6913 TRUE is returned if it is translated into the proper format for a call to the
6914 external library, and NEW_RETURN is set the tree for that function.
6915 FALSE is returned if processing for the _N variation is required, and
6916 NEW_RETURN is set to the return value the result is copied into. */
6917 static bool
6918 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6919 vec<tree, va_gc> *params, tree *new_return)
6921 tree p0, p1, p2, p3;
6922 tree I_type, I_type_ptr;
6923 int n = get_atomic_generic_size (loc, function, params);
6925 /* Size of 0 is an error condition. */
6926 if (n == 0)
6928 *new_return = error_mark_node;
6929 return true;
6932 /* If not a lock-free size, change to the library generic format. */
6933 if (!atomic_size_supported_p (n))
6935 *new_return = add_atomic_size_parameter (n, loc, function, params);
6936 return true;
6939 /* Otherwise there is a lockfree match, transform the call from:
6940 void fn(T* mem, T* desired, T* return, model)
6941 into
6942 *return = (T) (fn (In* mem, (In) *desired, model)) */
6944 p0 = (*params)[0];
6945 p1 = (*params)[1];
6946 p2 = (*params)[2];
6947 p3 = (*params)[3];
6949 /* Create pointer to appropriate size. */
6950 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6951 I_type_ptr = build_pointer_type (I_type);
6953 /* Convert object pointer to required type. */
6954 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6955 (*params)[0] = p0;
6956 /* Convert new value to required type, and dereference it. */
6957 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6958 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6959 (*params)[1] = p1;
6961 /* Move memory model to the 3rd position, and end param list. */
6962 (*params)[2] = p3;
6963 params->truncate (3);
6965 /* Convert return pointer and dereference it for later assignment. */
6966 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6968 return false;
6972 /* This will process an __atomic_compare_exchange function call, determine
6973 whether it needs to be mapped to the _N variation, or turned into a lib call.
6974 LOC is the location of the builtin call.
6975 FUNCTION is the DECL that has been invoked;
6976 PARAMS is the argument list for the call. The return value is non-null
6977 TRUE is returned if it is translated into the proper format for a call to the
6978 external library, and NEW_RETURN is set the tree for that function.
6979 FALSE is returned if processing for the _N variation is required. */
6981 static bool
6982 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6983 vec<tree, va_gc> *params,
6984 tree *new_return)
6986 tree p0, p1, p2;
6987 tree I_type, I_type_ptr;
6988 int n = get_atomic_generic_size (loc, function, params);
6990 /* Size of 0 is an error condition. */
6991 if (n == 0)
6993 *new_return = error_mark_node;
6994 return true;
6997 /* If not a lock-free size, change to the library generic format. */
6998 if (!atomic_size_supported_p (n))
7000 /* The library generic format does not have the weak parameter, so
7001 remove it from the param list. Since a parameter has been removed,
7002 we can be sure that there is room for the SIZE_T parameter, meaning
7003 there will not be a recursive rebuilding of the parameter list, so
7004 there is no danger this will be done twice. */
7005 if (n > 0)
7007 (*params)[3] = (*params)[4];
7008 (*params)[4] = (*params)[5];
7009 params->truncate (5);
7011 *new_return = add_atomic_size_parameter (n, loc, function, params);
7012 return true;
7015 /* Otherwise, there is a match, so the call needs to be transformed from:
7016 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7017 into
7018 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7020 p0 = (*params)[0];
7021 p1 = (*params)[1];
7022 p2 = (*params)[2];
7024 /* Create pointer to appropriate size. */
7025 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7026 I_type_ptr = build_pointer_type (I_type);
7028 /* Convert object pointer to required type. */
7029 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7030 (*params)[0] = p0;
7032 /* Convert expected pointer to required type. */
7033 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
7034 (*params)[1] = p1;
7036 /* Convert desired value to required type, and dereference it. */
7037 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7038 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
7039 (*params)[2] = p2;
7041 /* The rest of the parameters are fine. NULL means no special return value
7042 processing.*/
7043 *new_return = NULL;
7044 return false;
7048 /* This will process an __atomic_load function call, determine whether it
7049 needs to be mapped to the _N variation, or turned into a library call.
7050 LOC is the location of the builtin call.
7051 FUNCTION is the DECL that has been invoked;
7052 PARAMS is the argument list for the call. The return value is non-null
7053 TRUE is returned if it is translated into the proper format for a call to the
7054 external library, and NEW_RETURN is set the tree for that function.
7055 FALSE is returned if processing for the _N variation is required, and
7056 NEW_RETURN is set to the return value the result is copied into. */
7058 static bool
7059 resolve_overloaded_atomic_load (location_t loc, tree function,
7060 vec<tree, va_gc> *params, tree *new_return)
7062 tree p0, p1, p2;
7063 tree I_type, I_type_ptr;
7064 int n = get_atomic_generic_size (loc, function, params);
7066 /* Size of 0 is an error condition. */
7067 if (n == 0)
7069 *new_return = error_mark_node;
7070 return true;
7073 /* If not a lock-free size, change to the library generic format. */
7074 if (!atomic_size_supported_p (n))
7076 *new_return = add_atomic_size_parameter (n, loc, function, params);
7077 return true;
7080 /* Otherwise, there is a match, so the call needs to be transformed from:
7081 void fn(T* mem, T* return, model)
7082 into
7083 *return = (T) (fn ((In *) mem, model)) */
7085 p0 = (*params)[0];
7086 p1 = (*params)[1];
7087 p2 = (*params)[2];
7089 /* Create pointer to appropriate size. */
7090 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7091 I_type_ptr = build_pointer_type (I_type);
7093 /* Convert object pointer to required type. */
7094 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7095 (*params)[0] = p0;
7097 /* Move memory model to the 2nd position, and end param list. */
7098 (*params)[1] = p2;
7099 params->truncate (2);
7101 /* Convert return pointer and dereference it for later assignment. */
7102 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7104 return false;
7108 /* This will process an __atomic_store function call, determine whether it
7109 needs to be mapped to the _N variation, or turned into a library call.
7110 LOC is the location of the builtin call.
7111 FUNCTION is the DECL that has been invoked;
7112 PARAMS is the argument list for the call. The return value is non-null
7113 TRUE is returned if it is translated into the proper format for a call to the
7114 external library, and NEW_RETURN is set the tree for that function.
7115 FALSE is returned if processing for the _N variation is required, and
7116 NEW_RETURN is set to the return value the result is copied into. */
7118 static bool
7119 resolve_overloaded_atomic_store (location_t loc, tree function,
7120 vec<tree, va_gc> *params, tree *new_return)
7122 tree p0, p1;
7123 tree I_type, I_type_ptr;
7124 int n = get_atomic_generic_size (loc, function, params);
7126 /* Size of 0 is an error condition. */
7127 if (n == 0)
7129 *new_return = error_mark_node;
7130 return true;
7133 /* If not a lock-free size, change to the library generic format. */
7134 if (!atomic_size_supported_p (n))
7136 *new_return = add_atomic_size_parameter (n, loc, function, params);
7137 return true;
7140 /* Otherwise, there is a match, so the call needs to be transformed from:
7141 void fn(T* mem, T* value, model)
7142 into
7143 fn ((In *) mem, (In) *value, model) */
7145 p0 = (*params)[0];
7146 p1 = (*params)[1];
7148 /* Create pointer to appropriate size. */
7149 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7150 I_type_ptr = build_pointer_type (I_type);
7152 /* Convert object pointer to required type. */
7153 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7154 (*params)[0] = p0;
7156 /* Convert new value to required type, and dereference it. */
7157 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7158 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7159 (*params)[1] = p1;
7161 /* The memory model is in the right spot already. Return is void. */
7162 *new_return = NULL_TREE;
7164 return false;
7168 /* Some builtin functions are placeholders for other expressions. This
7169 function should be called immediately after parsing the call expression
7170 before surrounding code has committed to the type of the expression.
7172 LOC is the location of the builtin call.
7174 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7175 PARAMS is the argument list for the call. The return value is non-null
7176 when expansion is complete, and null if normal processing should
7177 continue. */
7179 tree
7180 resolve_overloaded_builtin (location_t loc, tree function,
7181 vec<tree, va_gc> *params)
7183 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7185 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7186 Those are not valid to call with a pointer to _Bool (or C++ bool)
7187 and so must be rejected. */
7188 bool fetch_op = true;
7189 bool orig_format = true;
7190 tree new_return = NULL_TREE;
7192 switch (DECL_BUILT_IN_CLASS (function))
7194 case BUILT_IN_NORMAL:
7195 break;
7196 case BUILT_IN_MD:
7197 if (targetm.resolve_overloaded_builtin)
7198 return targetm.resolve_overloaded_builtin (loc, function, params);
7199 else
7200 return NULL_TREE;
7201 default:
7202 return NULL_TREE;
7205 /* Handle BUILT_IN_NORMAL here. */
7206 switch (orig_code)
7208 case BUILT_IN_ATOMIC_EXCHANGE:
7209 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7210 case BUILT_IN_ATOMIC_LOAD:
7211 case BUILT_IN_ATOMIC_STORE:
7213 /* Handle these 4 together so that they can fall through to the next
7214 case if the call is transformed to an _N variant. */
7215 switch (orig_code)
7217 case BUILT_IN_ATOMIC_EXCHANGE:
7219 if (resolve_overloaded_atomic_exchange (loc, function, params,
7220 &new_return))
7221 return new_return;
7222 /* Change to the _N variant. */
7223 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7224 break;
7227 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7229 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7230 params,
7231 &new_return))
7232 return new_return;
7233 /* Change to the _N variant. */
7234 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7235 break;
7237 case BUILT_IN_ATOMIC_LOAD:
7239 if (resolve_overloaded_atomic_load (loc, function, params,
7240 &new_return))
7241 return new_return;
7242 /* Change to the _N variant. */
7243 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7244 break;
7246 case BUILT_IN_ATOMIC_STORE:
7248 if (resolve_overloaded_atomic_store (loc, function, params,
7249 &new_return))
7250 return new_return;
7251 /* Change to the _N variant. */
7252 orig_code = BUILT_IN_ATOMIC_STORE_N;
7253 break;
7255 default:
7256 gcc_unreachable ();
7259 /* FALLTHRU */
7260 case BUILT_IN_ATOMIC_EXCHANGE_N:
7261 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7262 case BUILT_IN_ATOMIC_LOAD_N:
7263 case BUILT_IN_ATOMIC_STORE_N:
7264 fetch_op = false;
7265 /* FALLTHRU */
7266 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7267 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7268 case BUILT_IN_ATOMIC_AND_FETCH_N:
7269 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7270 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7271 case BUILT_IN_ATOMIC_OR_FETCH_N:
7272 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7273 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7274 case BUILT_IN_ATOMIC_FETCH_AND_N:
7275 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7276 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7277 case BUILT_IN_ATOMIC_FETCH_OR_N:
7278 orig_format = false;
7279 /* FALLTHRU */
7280 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7281 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7282 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7283 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7284 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7285 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7286 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7287 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7288 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7289 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7290 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7291 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7292 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7293 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7294 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7295 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7297 /* The following are not _FETCH_OPs and must be accepted with
7298 pointers to _Bool (or C++ bool). */
7299 if (fetch_op)
7300 fetch_op =
7301 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7302 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7303 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7304 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7306 int n = sync_resolve_size (function, params, fetch_op);
7307 tree new_function, first_param, result;
7308 enum built_in_function fncode;
7310 if (n == 0)
7311 return error_mark_node;
7313 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7314 new_function = builtin_decl_explicit (fncode);
7315 if (!sync_resolve_params (loc, function, new_function, params,
7316 orig_format))
7317 return error_mark_node;
7319 first_param = (*params)[0];
7320 result = build_function_call_vec (loc, vNULL, new_function, params,
7321 NULL);
7322 if (result == error_mark_node)
7323 return result;
7324 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7325 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7326 && orig_code != BUILT_IN_ATOMIC_STORE_N
7327 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7328 result = sync_resolve_return (first_param, result, orig_format);
7330 if (fetch_op)
7331 /* Prevent -Wunused-value warning. */
7332 TREE_USED (result) = true;
7334 /* If new_return is set, assign function to that expr and cast the
7335 result to void since the generic interface returned void. */
7336 if (new_return)
7338 /* Cast function result from I{1,2,4,8,16} to the required type. */
7339 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7340 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7341 result);
7342 TREE_SIDE_EFFECTS (result) = 1;
7343 protected_set_expr_location (result, loc);
7344 result = convert (void_type_node, result);
7346 return result;
7349 default:
7350 return NULL_TREE;
7354 /* vector_types_compatible_elements_p is used in type checks of vectors
7355 values used as operands of binary operators. Where it returns true, and
7356 the other checks of the caller succeed (being vector types in he first
7357 place, and matching number of elements), we can just treat the types
7358 as essentially the same.
7359 Contrast with vector_targets_convertible_p, which is used for vector
7360 pointer types, and vector_types_convertible_p, which will allow
7361 language-specific matches under the control of flag_lax_vector_conversions,
7362 and might still require a conversion. */
7363 /* True if vector types T1 and T2 can be inputs to the same binary
7364 operator without conversion.
7365 We don't check the overall vector size here because some of our callers
7366 want to give different error messages when the vectors are compatible
7367 except for the element count. */
7369 bool
7370 vector_types_compatible_elements_p (tree t1, tree t2)
7372 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7373 t1 = TREE_TYPE (t1);
7374 t2 = TREE_TYPE (t2);
7376 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7378 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7379 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7380 || c2 == FIXED_POINT_TYPE));
7382 t1 = c_common_signed_type (t1);
7383 t2 = c_common_signed_type (t2);
7384 /* Equality works here because c_common_signed_type uses
7385 TYPE_MAIN_VARIANT. */
7386 if (t1 == t2)
7387 return true;
7388 if (opaque && c1 == c2
7389 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7390 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7391 return true;
7392 return false;
7395 /* Check for missing format attributes on function pointers. LTYPE is
7396 the new type or left-hand side type. RTYPE is the old type or
7397 right-hand side type. Returns TRUE if LTYPE is missing the desired
7398 attribute. */
7400 bool
7401 check_missing_format_attribute (tree ltype, tree rtype)
7403 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7404 tree ra;
7406 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7407 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7408 break;
7409 if (ra)
7411 tree la;
7412 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7413 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7414 break;
7415 return !la;
7417 else
7418 return false;
7421 /* Setup a TYPE_DECL node as a typedef representation.
7423 X is a TYPE_DECL for a typedef statement. Create a brand new
7424 ..._TYPE node (which will be just a variant of the existing
7425 ..._TYPE node with identical properties) and then install X
7426 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7428 The whole point here is to end up with a situation where each
7429 and every ..._TYPE node the compiler creates will be uniquely
7430 associated with AT MOST one node representing a typedef name.
7431 This way, even though the compiler substitutes corresponding
7432 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7433 early on, later parts of the compiler can always do the reverse
7434 translation and get back the corresponding typedef name. For
7435 example, given:
7437 typedef struct S MY_TYPE;
7438 MY_TYPE object;
7440 Later parts of the compiler might only know that `object' was of
7441 type `struct S' if it were not for code just below. With this
7442 code however, later parts of the compiler see something like:
7444 struct S' == struct S
7445 typedef struct S' MY_TYPE;
7446 struct S' object;
7448 And they can then deduce (from the node for type struct S') that
7449 the original object declaration was:
7451 MY_TYPE object;
7453 Being able to do this is important for proper support of protoize,
7454 and also for generating precise symbolic debugging information
7455 which takes full account of the programmer's (typedef) vocabulary.
7457 Obviously, we don't want to generate a duplicate ..._TYPE node if
7458 the TYPE_DECL node that we are now processing really represents a
7459 standard built-in type. */
7461 void
7462 set_underlying_type (tree x)
7464 if (x == error_mark_node)
7465 return;
7466 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7468 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7469 TYPE_NAME (TREE_TYPE (x)) = x;
7471 else if (TREE_TYPE (x) != error_mark_node
7472 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7474 tree tt = TREE_TYPE (x);
7475 DECL_ORIGINAL_TYPE (x) = tt;
7476 tt = build_variant_type_copy (tt);
7477 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7478 TYPE_NAME (tt) = x;
7479 TREE_USED (tt) = TREE_USED (x);
7480 TREE_TYPE (x) = tt;
7484 /* Record the types used by the current global variable declaration
7485 being parsed, so that we can decide later to emit their debug info.
7486 Those types are in types_used_by_cur_var_decl, and we are going to
7487 store them in the types_used_by_vars_hash hash table.
7488 DECL is the declaration of the global variable that has been parsed. */
7490 void
7491 record_types_used_by_current_var_decl (tree decl)
7493 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7495 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7497 tree type = types_used_by_cur_var_decl->pop ();
7498 types_used_by_var_decl_insert (type, decl);
7502 /* The C and C++ parsers both use vectors to hold function arguments.
7503 For efficiency, we keep a cache of unused vectors. This is the
7504 cache. */
7506 typedef vec<tree, va_gc> *tree_gc_vec;
7507 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7509 /* Return a new vector from the cache. If the cache is empty,
7510 allocate a new vector. These vectors are GC'ed, so it is OK if the
7511 pointer is not released.. */
7513 vec<tree, va_gc> *
7514 make_tree_vector (void)
7516 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7517 return tree_vector_cache->pop ();
7518 else
7520 /* Passing 0 to vec::alloc returns NULL, and our callers require
7521 that we always return a non-NULL value. The vector code uses
7522 4 when growing a NULL vector, so we do too. */
7523 vec<tree, va_gc> *v;
7524 vec_alloc (v, 4);
7525 return v;
7529 /* Release a vector of trees back to the cache. */
7531 void
7532 release_tree_vector (vec<tree, va_gc> *vec)
7534 if (vec != NULL)
7536 vec->truncate (0);
7537 vec_safe_push (tree_vector_cache, vec);
7541 /* Get a new tree vector holding a single tree. */
7543 vec<tree, va_gc> *
7544 make_tree_vector_single (tree t)
7546 vec<tree, va_gc> *ret = make_tree_vector ();
7547 ret->quick_push (t);
7548 return ret;
7551 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7553 vec<tree, va_gc> *
7554 make_tree_vector_from_list (tree list)
7556 vec<tree, va_gc> *ret = make_tree_vector ();
7557 for (; list; list = TREE_CHAIN (list))
7558 vec_safe_push (ret, TREE_VALUE (list));
7559 return ret;
7562 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7564 vec<tree, va_gc> *
7565 make_tree_vector_from_ctor (tree ctor)
7567 vec<tree,va_gc> *ret = make_tree_vector ();
7568 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7569 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7570 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7571 return ret;
7574 /* Get a new tree vector which is a copy of an existing one. */
7576 vec<tree, va_gc> *
7577 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7579 vec<tree, va_gc> *ret;
7580 unsigned int ix;
7581 tree t;
7583 ret = make_tree_vector ();
7584 vec_safe_reserve (ret, vec_safe_length (orig));
7585 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7586 ret->quick_push (t);
7587 return ret;
7590 /* Return true if KEYWORD starts a type specifier. */
7592 bool
7593 keyword_begins_type_specifier (enum rid keyword)
7595 switch (keyword)
7597 case RID_AUTO_TYPE:
7598 case RID_INT:
7599 case RID_CHAR:
7600 case RID_FLOAT:
7601 case RID_DOUBLE:
7602 case RID_VOID:
7603 case RID_UNSIGNED:
7604 case RID_LONG:
7605 case RID_SHORT:
7606 case RID_SIGNED:
7607 CASE_RID_FLOATN_NX:
7608 case RID_DFLOAT32:
7609 case RID_DFLOAT64:
7610 case RID_DFLOAT128:
7611 case RID_FRACT:
7612 case RID_ACCUM:
7613 case RID_BOOL:
7614 case RID_WCHAR:
7615 case RID_CHAR16:
7616 case RID_CHAR32:
7617 case RID_SAT:
7618 case RID_COMPLEX:
7619 case RID_TYPEOF:
7620 case RID_STRUCT:
7621 case RID_CLASS:
7622 case RID_UNION:
7623 case RID_ENUM:
7624 return true;
7625 default:
7626 if (keyword >= RID_FIRST_INT_N
7627 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7628 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7629 return true;
7630 return false;
7634 /* Return true if KEYWORD names a type qualifier. */
7636 bool
7637 keyword_is_type_qualifier (enum rid keyword)
7639 switch (keyword)
7641 case RID_CONST:
7642 case RID_VOLATILE:
7643 case RID_RESTRICT:
7644 case RID_ATOMIC:
7645 return true;
7646 default:
7647 return false;
7651 /* Return true if KEYWORD names a storage class specifier.
7653 RID_TYPEDEF is not included in this list despite `typedef' being
7654 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7655 such for syntactic convenience only. */
7657 bool
7658 keyword_is_storage_class_specifier (enum rid keyword)
7660 switch (keyword)
7662 case RID_STATIC:
7663 case RID_EXTERN:
7664 case RID_REGISTER:
7665 case RID_AUTO:
7666 case RID_MUTABLE:
7667 case RID_THREAD:
7668 return true;
7669 default:
7670 return false;
7674 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7676 static bool
7677 keyword_is_function_specifier (enum rid keyword)
7679 switch (keyword)
7681 case RID_INLINE:
7682 case RID_NORETURN:
7683 case RID_VIRTUAL:
7684 case RID_EXPLICIT:
7685 return true;
7686 default:
7687 return false;
7691 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7692 declaration-specifier (C99 6.7). */
7694 bool
7695 keyword_is_decl_specifier (enum rid keyword)
7697 if (keyword_is_storage_class_specifier (keyword)
7698 || keyword_is_type_qualifier (keyword)
7699 || keyword_is_function_specifier (keyword))
7700 return true;
7702 switch (keyword)
7704 case RID_TYPEDEF:
7705 case RID_FRIEND:
7706 case RID_CONSTEXPR:
7707 return true;
7708 default:
7709 return false;
7713 /* Initialize language-specific-bits of tree_contains_struct. */
7715 void
7716 c_common_init_ts (void)
7718 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7719 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7720 MARK_TS_TYPED (ARRAY_NOTATION_REF);
7723 /* Build a user-defined numeric literal out of an integer constant type VALUE
7724 with identifier SUFFIX. */
7726 tree
7727 build_userdef_literal (tree suffix_id, tree value,
7728 enum overflow_type overflow, tree num_string)
7730 tree literal = make_node (USERDEF_LITERAL);
7731 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7732 USERDEF_LITERAL_VALUE (literal) = value;
7733 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7734 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7735 return literal;
7738 /* For vector[index], convert the vector to an array of the underlying type.
7739 Return true if the resulting ARRAY_REF should not be an lvalue. */
7741 bool
7742 convert_vector_to_array_for_subscript (location_t loc,
7743 tree *vecp, tree index)
7745 bool ret = false;
7746 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7748 tree type = TREE_TYPE (*vecp);
7750 ret = !lvalue_p (*vecp);
7752 if (TREE_CODE (index) == INTEGER_CST)
7753 if (!tree_fits_uhwi_p (index)
7754 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7755 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7757 /* We are building an ARRAY_REF so mark the vector as addressable
7758 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7759 for function parameters. */
7760 c_common_mark_addressable_vec (*vecp);
7762 *vecp = build1 (VIEW_CONVERT_EXPR,
7763 build_array_type_nelts (TREE_TYPE (type),
7764 TYPE_VECTOR_SUBPARTS (type)),
7765 *vecp);
7767 return ret;
7770 /* Determine which of the operands, if any, is a scalar that needs to be
7771 converted to a vector, for the range of operations. */
7772 enum stv_conv
7773 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7774 bool complain)
7776 tree type0 = TREE_TYPE (op0);
7777 tree type1 = TREE_TYPE (op1);
7778 bool integer_only_op = false;
7779 enum stv_conv ret = stv_firstarg;
7781 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7782 switch (code)
7784 /* Most GENERIC binary expressions require homogeneous arguments.
7785 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7786 argument that is a vector and a second one that is a scalar, so
7787 we never return stv_secondarg for them. */
7788 case RSHIFT_EXPR:
7789 case LSHIFT_EXPR:
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 else
7801 return stv_firstarg;
7803 break;
7805 case BIT_IOR_EXPR:
7806 case BIT_XOR_EXPR:
7807 case BIT_AND_EXPR:
7808 integer_only_op = true;
7809 /* fall through */
7811 case VEC_COND_EXPR:
7813 case PLUS_EXPR:
7814 case MINUS_EXPR:
7815 case MULT_EXPR:
7816 case TRUNC_DIV_EXPR:
7817 case CEIL_DIV_EXPR:
7818 case FLOOR_DIV_EXPR:
7819 case ROUND_DIV_EXPR:
7820 case EXACT_DIV_EXPR:
7821 case TRUNC_MOD_EXPR:
7822 case FLOOR_MOD_EXPR:
7823 case RDIV_EXPR:
7824 case EQ_EXPR:
7825 case NE_EXPR:
7826 case LE_EXPR:
7827 case GE_EXPR:
7828 case LT_EXPR:
7829 case GT_EXPR:
7830 /* What about UNLT_EXPR? */
7831 if (VECTOR_TYPE_P (type0))
7833 ret = stv_secondarg;
7834 std::swap (type0, type1);
7835 std::swap (op0, op1);
7838 if (TREE_CODE (type0) == INTEGER_TYPE
7839 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7841 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7843 if (complain)
7844 error_at (loc, "conversion of scalar %qT to vector %qT "
7845 "involves truncation", type0, type1);
7846 return stv_error;
7848 return ret;
7850 else if (!integer_only_op
7851 /* Allow integer --> real conversion if safe. */
7852 && (TREE_CODE (type0) == REAL_TYPE
7853 || TREE_CODE (type0) == INTEGER_TYPE)
7854 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7856 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7858 if (complain)
7859 error_at (loc, "conversion of scalar %qT to vector %qT "
7860 "involves truncation", type0, type1);
7861 return stv_error;
7863 return ret;
7865 default:
7866 break;
7869 return stv_nothing;
7872 /* Return the alignment of std::max_align_t.
7874 [support.types.layout] The type max_align_t is a POD type whose alignment
7875 requirement is at least as great as that of every scalar type, and whose
7876 alignment requirement is supported in every context. */
7878 unsigned
7879 max_align_t_align ()
7881 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7882 TYPE_ALIGN (long_double_type_node));
7883 if (float128_type_node != NULL_TREE)
7884 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7885 return max_align;
7888 /* Return true iff ALIGN is an integral constant that is a fundamental
7889 alignment, as defined by [basic.align] in the c++-11
7890 specifications.
7892 That is:
7894 [A fundamental alignment is represented by an alignment less than or
7895 equal to the greatest alignment supported by the implementation
7896 in all contexts, which is equal to alignof(max_align_t)]. */
7898 bool
7899 cxx_fundamental_alignment_p (unsigned align)
7901 return (align <= max_align_t_align ());
7904 /* Return true if T is a pointer to a zero-sized aggregate. */
7906 bool
7907 pointer_to_zero_sized_aggr_p (tree t)
7909 if (!POINTER_TYPE_P (t))
7910 return false;
7911 t = TREE_TYPE (t);
7912 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7915 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7916 with no library fallback or for an ADDR_EXPR whose operand is such type
7917 issues an error pointing to the location LOC.
7918 Returns true when the expression has been diagnosed and false
7919 otherwise. */
7921 bool
7922 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7924 if (TREE_CODE (expr) == ADDR_EXPR)
7925 expr = TREE_OPERAND (expr, 0);
7927 if (TREE_TYPE (expr)
7928 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7929 && TREE_CODE (expr) == FUNCTION_DECL
7930 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7931 false positives for user-declared built-ins such as abs or
7932 strlen, and for C++ operators new and delete.
7933 The c_decl_implicit() test avoids false positives for implicitly
7934 declared built-ins with library fallbacks (such as abs). */
7935 && DECL_BUILT_IN (expr)
7936 && DECL_IS_BUILTIN (expr)
7937 && !c_decl_implicit (expr)
7938 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7940 if (loc == UNKNOWN_LOCATION)
7941 loc = EXPR_LOC_OR_LOC (expr, input_location);
7943 /* Reject arguments that are built-in functions with
7944 no library fallback. */
7945 error_at (loc, "built-in function %qE must be directly called", expr);
7947 return true;
7950 return false;
7953 /* Check if array size calculations overflow or if the array covers more
7954 than half of the address space. Return true if the size of the array
7955 is valid, false otherwise. TYPE is the type of the array and NAME is
7956 the name of the array, or NULL_TREE for unnamed arrays. */
7958 bool
7959 valid_array_size_p (location_t loc, tree type, tree name)
7961 if (type != error_mark_node
7962 && COMPLETE_TYPE_P (type)
7963 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7964 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7966 if (name)
7967 error_at (loc, "size of array %qE is too large", name);
7968 else
7969 error_at (loc, "size of unnamed array is too large");
7970 return false;
7972 return true;
7975 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7976 timestamp to replace embedded current dates to get reproducible
7977 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
7979 time_t
7980 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7982 char *source_date_epoch;
7983 int64_t epoch;
7984 char *endptr;
7986 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7987 if (!source_date_epoch)
7988 return (time_t) -1;
7990 errno = 0;
7991 #if defined(INT64_T_IS_LONG)
7992 epoch = strtol (source_date_epoch, &endptr, 10);
7993 #else
7994 epoch = strtoll (source_date_epoch, &endptr, 10);
7995 #endif
7996 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7997 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7999 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
8000 "expand to a non-negative integer less than or equal to %wd",
8001 MAX_SOURCE_DATE_EPOCH);
8002 return (time_t) -1;
8005 return (time_t) epoch;
8008 /* Callback for libcpp for offering spelling suggestions for misspelled
8009 directives. GOAL is an unrecognized string; CANDIDATES is a
8010 NULL-terminated array of candidate strings. Return the closest
8011 match to GOAL within CANDIDATES, or NULL if none are good
8012 suggestions. */
8014 const char *
8015 cb_get_suggestion (cpp_reader *, const char *goal,
8016 const char *const *candidates)
8018 best_match<const char *, const char *> bm (goal);
8019 while (*candidates)
8020 bm.consider (*candidates++);
8021 return bm.get_best_meaningful_candidate ();
8024 /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8025 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8026 by C TS 18661-3 for interchange types that are computed in their
8027 native precision are larger than the C11 values for evaluating in the
8028 precision of float/double/long double. If either mode is
8029 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8031 enum flt_eval_method
8032 excess_precision_mode_join (enum flt_eval_method x,
8033 enum flt_eval_method y)
8035 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8036 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8037 return FLT_EVAL_METHOD_UNPREDICTABLE;
8039 /* GCC only supports one interchange type right now, _Float16. If
8040 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8041 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8042 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8043 return y;
8044 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8045 return x;
8047 /* Other values for flt_eval_method are directly comparable, and we want
8048 the maximum. */
8049 return MAX (x, y);
8052 /* Return the value that should be set for FLT_EVAL_METHOD in the
8053 context of ISO/IEC TS 18861-3.
8055 This relates to the effective excess precision seen by the user,
8056 which is the join point of the precision the target requests for
8057 -fexcess-precision={standard,fast} and the implicit excess precision
8058 the target uses. */
8060 static enum flt_eval_method
8061 c_ts18661_flt_eval_method (void)
8063 enum flt_eval_method implicit
8064 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8066 enum excess_precision_type flag_type
8067 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8068 ? EXCESS_PRECISION_TYPE_STANDARD
8069 : EXCESS_PRECISION_TYPE_FAST);
8071 enum flt_eval_method requested
8072 = targetm.c.excess_precision (flag_type);
8074 return excess_precision_mode_join (implicit, requested);
8077 /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8078 those that were permitted by C11. That is to say, eliminates
8079 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8081 static enum flt_eval_method
8082 c_c11_flt_eval_method (void)
8084 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8085 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8088 /* Return the value that should be set for FLT_EVAL_METHOD.
8089 MAYBE_C11_ONLY_P is TRUE if we should check
8090 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8091 values we can return to those from C99/C11, and FALSE otherwise.
8092 See the comments on c_ts18661_flt_eval_method for what value we choose
8093 to set here. */
8096 c_flt_eval_method (bool maybe_c11_only_p)
8098 if (maybe_c11_only_p
8099 && flag_permitted_flt_eval_methods
8100 == PERMITTED_FLT_EVAL_METHODS_C11)
8101 return c_c11_flt_eval_method ();
8102 else
8103 return c_ts18661_flt_eval_method ();
8106 #include "gt-c-family-c-common.h"