* g++.dg/debug/dwarf2/ref-3.C: XFAIL AIX.
[official-gcc.git] / gcc / c-family / c-common.c
blob307862b9c7c4d694acb5a42c7491ee8e3ecef80d
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2016 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 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
439 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
440 { "asm", RID_ASM, D_ASM },
441 { "auto", RID_AUTO, 0 },
442 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
443 { "break", RID_BREAK, 0 },
444 { "case", RID_CASE, 0 },
445 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
446 { "char", RID_CHAR, 0 },
447 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
448 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
449 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
450 { "const", RID_CONST, 0 },
451 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
452 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
453 { "continue", RID_CONTINUE, 0 },
454 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
455 { "default", RID_DEFAULT, 0 },
456 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
457 { "do", RID_DO, 0 },
458 { "double", RID_DOUBLE, 0 },
459 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
460 { "else", RID_ELSE, 0 },
461 { "enum", RID_ENUM, 0 },
462 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
463 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
464 { "extern", RID_EXTERN, 0 },
465 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
466 { "float", RID_FLOAT, 0 },
467 { "for", RID_FOR, 0 },
468 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
469 { "goto", RID_GOTO, 0 },
470 { "if", RID_IF, 0 },
471 { "inline", RID_INLINE, D_EXT89 },
472 { "int", RID_INT, 0 },
473 { "long", RID_LONG, 0 },
474 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
475 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
476 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
477 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
478 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
479 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
480 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
481 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
482 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
483 { "register", RID_REGISTER, 0 },
484 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
485 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
486 { "return", RID_RETURN, 0 },
487 { "short", RID_SHORT, 0 },
488 { "signed", RID_SIGNED, 0 },
489 { "sizeof", RID_SIZEOF, 0 },
490 { "static", RID_STATIC, 0 },
491 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
492 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
493 { "struct", RID_STRUCT, 0 },
494 { "switch", RID_SWITCH, 0 },
495 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
496 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
497 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
498 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
499 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
500 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
501 { "typedef", RID_TYPEDEF, 0 },
502 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
503 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
504 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
505 { "union", RID_UNION, 0 },
506 { "unsigned", RID_UNSIGNED, 0 },
507 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
508 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
509 { "void", RID_VOID, 0 },
510 { "volatile", RID_VOLATILE, 0 },
511 { "wchar_t", RID_WCHAR, D_CXXONLY },
512 { "while", RID_WHILE, 0 },
514 /* C++ transactional memory. */
515 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
516 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
517 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
518 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
520 /* Concepts-related keywords */
521 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
522 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
524 /* These Objective-C keywords are recognized only immediately after
525 an '@'. */
526 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
527 { "defs", RID_AT_DEFS, D_OBJC },
528 { "encode", RID_AT_ENCODE, D_OBJC },
529 { "end", RID_AT_END, D_OBJC },
530 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
531 { "interface", RID_AT_INTERFACE, D_OBJC },
532 { "protocol", RID_AT_PROTOCOL, D_OBJC },
533 { "selector", RID_AT_SELECTOR, D_OBJC },
534 { "finally", RID_AT_FINALLY, D_OBJC },
535 { "optional", RID_AT_OPTIONAL, D_OBJC },
536 { "required", RID_AT_REQUIRED, D_OBJC },
537 { "property", RID_AT_PROPERTY, D_OBJC },
538 { "package", RID_AT_PACKAGE, D_OBJC },
539 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
540 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
541 /* These are recognized only in protocol-qualifier context
542 (see above) */
543 { "bycopy", RID_BYCOPY, D_OBJC },
544 { "byref", RID_BYREF, D_OBJC },
545 { "in", RID_IN, D_OBJC },
546 { "inout", RID_INOUT, D_OBJC },
547 { "oneway", RID_ONEWAY, D_OBJC },
548 { "out", RID_OUT, D_OBJC },
549 /* These are recognized inside a property attribute list */
550 { "assign", RID_ASSIGN, D_OBJC },
551 { "copy", RID_COPY, D_OBJC },
552 { "getter", RID_GETTER, D_OBJC },
553 { "nonatomic", RID_NONATOMIC, D_OBJC },
554 { "readonly", RID_READONLY, D_OBJC },
555 { "readwrite", RID_READWRITE, D_OBJC },
556 { "retain", RID_RETAIN, D_OBJC },
557 { "setter", RID_SETTER, D_OBJC },
560 const unsigned int num_c_common_reswords =
561 sizeof c_common_reswords / sizeof (struct c_common_resword);
563 /* Return identifier for address space AS. */
565 const char *
566 c_addr_space_name (addr_space_t as)
568 int rid = RID_FIRST_ADDR_SPACE + as;
569 gcc_assert (ridpointers [rid]);
570 return IDENTIFIER_POINTER (ridpointers [rid]);
573 /* Push current bindings for the function name VAR_DECLS. */
575 void
576 start_fname_decls (void)
578 unsigned ix;
579 tree saved = NULL_TREE;
581 for (ix = 0; fname_vars[ix].decl; ix++)
583 tree decl = *fname_vars[ix].decl;
585 if (decl)
587 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
588 saved);
589 *fname_vars[ix].decl = NULL_TREE;
592 if (saved || saved_function_name_decls)
593 /* Normally they'll have been NULL, so only push if we've got a
594 stack, or they are non-NULL. */
595 saved_function_name_decls = tree_cons (saved, NULL_TREE,
596 saved_function_name_decls);
599 /* Finish up the current bindings, adding them into the current function's
600 statement tree. This must be done _before_ finish_stmt_tree is called.
601 If there is no current function, we must be at file scope and no statements
602 are involved. Pop the previous bindings. */
604 void
605 finish_fname_decls (void)
607 unsigned ix;
608 tree stmts = NULL_TREE;
609 tree stack = saved_function_name_decls;
611 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
612 append_to_statement_list (TREE_VALUE (stack), &stmts);
614 if (stmts)
616 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
618 if (TREE_CODE (*bodyp) == BIND_EXPR)
619 bodyp = &BIND_EXPR_BODY (*bodyp);
621 append_to_statement_list_force (*bodyp, &stmts);
622 *bodyp = stmts;
625 for (ix = 0; fname_vars[ix].decl; ix++)
626 *fname_vars[ix].decl = NULL_TREE;
628 if (stack)
630 /* We had saved values, restore them. */
631 tree saved;
633 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
635 tree decl = TREE_PURPOSE (saved);
636 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
638 *fname_vars[ix].decl = decl;
640 stack = TREE_CHAIN (stack);
642 saved_function_name_decls = stack;
645 /* Return the text name of the current function, suitably prettified
646 by PRETTY_P. Return string must be freed by caller. */
648 const char *
649 fname_as_string (int pretty_p)
651 const char *name = "top level";
652 char *namep;
653 int vrb = 2, len;
654 cpp_string cstr = { 0, 0 }, strname;
656 if (!pretty_p)
658 name = "";
659 vrb = 0;
662 if (current_function_decl)
663 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
665 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
667 namep = XNEWVEC (char, len);
668 snprintf (namep, len, "\"%s\"", name);
669 strname.text = (unsigned char *) namep;
670 strname.len = len - 1;
672 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
674 XDELETEVEC (namep);
675 return (const char *) cstr.text;
678 return namep;
681 /* Return the VAR_DECL for a const char array naming the current
682 function. If the VAR_DECL has not yet been created, create it
683 now. RID indicates how it should be formatted and IDENTIFIER_NODE
684 ID is its name (unfortunately C and C++ hold the RID values of
685 keywords in different places, so we can't derive RID from ID in
686 this language independent code. LOC is the location of the
687 function. */
689 tree
690 fname_decl (location_t loc, unsigned int rid, tree id)
692 unsigned ix;
693 tree decl = NULL_TREE;
695 for (ix = 0; fname_vars[ix].decl; ix++)
696 if (fname_vars[ix].rid == rid)
697 break;
699 decl = *fname_vars[ix].decl;
700 if (!decl)
702 /* If a tree is built here, it would normally have the lineno of
703 the current statement. Later this tree will be moved to the
704 beginning of the function and this line number will be wrong.
705 To avoid this problem set the lineno to 0 here; that prevents
706 it from appearing in the RTL. */
707 tree stmts;
708 location_t saved_location = input_location;
709 input_location = UNKNOWN_LOCATION;
711 stmts = push_stmt_list ();
712 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
713 stmts = pop_stmt_list (stmts);
714 if (!IS_EMPTY_STMT (stmts))
715 saved_function_name_decls
716 = tree_cons (decl, stmts, saved_function_name_decls);
717 *fname_vars[ix].decl = decl;
718 input_location = saved_location;
720 if (!ix && !current_function_decl)
721 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
723 return decl;
726 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
728 tree
729 fix_string_type (tree value)
731 int length = TREE_STRING_LENGTH (value);
732 int nchars;
733 tree e_type, i_type, a_type;
735 /* Compute the number of elements, for the array type. */
736 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
738 nchars = length;
739 e_type = char_type_node;
741 else if (TREE_TYPE (value) == char16_array_type_node)
743 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
744 e_type = char16_type_node;
746 else if (TREE_TYPE (value) == char32_array_type_node)
748 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
749 e_type = char32_type_node;
751 else
753 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
754 e_type = wchar_type_node;
757 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
758 limit in C++98 Annex B is very large (65536) and is not normative,
759 so we do not diagnose it (warn_overlength_strings is forced off
760 in c_common_post_options). */
761 if (warn_overlength_strings)
763 const int nchars_max = flag_isoc99 ? 4095 : 509;
764 const int relevant_std = flag_isoc99 ? 99 : 90;
765 if (nchars - 1 > nchars_max)
766 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
767 separate the %d from the 'C'. 'ISO' should not be
768 translated, but it may be moved after 'C%d' in languages
769 where modifiers follow nouns. */
770 pedwarn (input_location, OPT_Woverlength_strings,
771 "string length %qd is greater than the length %qd "
772 "ISO C%d compilers are required to support",
773 nchars - 1, nchars_max, relevant_std);
776 /* Create the array type for the string constant. The ISO C++
777 standard says that a string literal has type `const char[N]' or
778 `const wchar_t[N]'. We use the same logic when invoked as a C
779 front-end with -Wwrite-strings.
780 ??? We should change the type of an expression depending on the
781 state of a warning flag. We should just be warning -- see how
782 this is handled in the C++ front-end for the deprecated implicit
783 conversion from string literals to `char*' or `wchar_t*'.
785 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
786 array type being the unqualified version of that type.
787 Therefore, if we are constructing an array of const char, we must
788 construct the matching unqualified array type first. The C front
789 end does not require this, but it does no harm, so we do it
790 unconditionally. */
791 i_type = build_index_type (size_int (nchars - 1));
792 a_type = build_array_type (e_type, i_type);
793 if (c_dialect_cxx() || warn_write_strings)
794 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
796 TREE_TYPE (value) = a_type;
797 TREE_CONSTANT (value) = 1;
798 TREE_READONLY (value) = 1;
799 TREE_STATIC (value) = 1;
800 return value;
803 /* Given a string of type STRING_TYPE, determine what kind of string
804 token would give an equivalent execution encoding: CPP_STRING,
805 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
806 This may not be exactly the string token type that initially created
807 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
808 string type at this point.
810 This effectively reverses part of the logic in lex_string and
811 fix_string_type. */
813 static enum cpp_ttype
814 get_cpp_ttype_from_string_type (tree string_type)
816 gcc_assert (string_type);
817 if (TREE_CODE (string_type) == POINTER_TYPE)
818 string_type = TREE_TYPE (string_type);
820 if (TREE_CODE (string_type) != ARRAY_TYPE)
821 return CPP_OTHER;
823 tree element_type = TREE_TYPE (string_type);
824 if (TREE_CODE (element_type) != INTEGER_TYPE)
825 return CPP_OTHER;
827 int bits_per_character = TYPE_PRECISION (element_type);
828 switch (bits_per_character)
830 case 8:
831 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
832 case 16:
833 return CPP_STRING16;
834 case 32:
835 return CPP_STRING32;
838 return CPP_OTHER;
841 /* The global record of string concatentations, for use in
842 extracting locations within string literals. */
844 GTY(()) string_concat_db *g_string_concat_db;
846 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
848 const char *
849 c_get_substring_location (const substring_loc &substr_loc,
850 location_t *out_loc)
852 enum cpp_ttype tok_type
853 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
854 if (tok_type == CPP_OTHER)
855 return "unrecognized string type";
857 return get_source_location_for_substring (parse_in, g_string_concat_db,
858 substr_loc.get_fmt_string_loc (),
859 tok_type,
860 substr_loc.get_caret_idx (),
861 substr_loc.get_start_idx (),
862 substr_loc.get_end_idx (),
863 out_loc);
867 /* Fold X for consideration by one of the warning functions when checking
868 whether an expression has a constant value. */
870 tree
871 fold_for_warn (tree x)
873 if (c_dialect_cxx ())
874 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
875 else
876 /* The C front-end has already folded X appropriately. */
877 return x;
880 /* Return true iff T is a boolean promoted to int. */
882 bool
883 bool_promoted_to_int_p (tree t)
885 return (CONVERT_EXPR_P (t)
886 && TREE_TYPE (t) == integer_type_node
887 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
890 /* vector_targets_convertible_p is used for vector pointer types. The
891 callers perform various checks that the qualifiers are satisfactory,
892 while OTOH vector_targets_convertible_p ignores the number of elements
893 in the vectors. That's fine with vector pointers as we can consider,
894 say, a vector of 8 elements as two consecutive vectors of 4 elements,
895 and that does not require and conversion of the pointer values.
896 In contrast, vector_types_convertible_p and
897 vector_types_compatible_elements_p are used for vector value types. */
898 /* True if pointers to distinct types T1 and T2 can be converted to
899 each other without an explicit cast. Only returns true for opaque
900 vector types. */
901 bool
902 vector_targets_convertible_p (const_tree t1, const_tree t2)
904 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
905 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
906 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
907 return true;
909 return false;
912 /* vector_types_convertible_p is used for vector value types.
913 It could in principle call vector_targets_convertible_p as a subroutine,
914 but then the check for vector type would be duplicated with its callers,
915 and also the purpose of vector_targets_convertible_p would become
916 muddled.
917 Where vector_types_convertible_p returns true, a conversion might still be
918 needed to make the types match.
919 In contrast, vector_targets_convertible_p is used for vector pointer
920 values, and vector_types_compatible_elements_p is used specifically
921 in the context for binary operators, as a check if use is possible without
922 conversion. */
923 /* True if vector types T1 and T2 can be converted to each other
924 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
925 can only be converted with -flax-vector-conversions yet that is not
926 in effect, emit a note telling the user about that option if such
927 a note has not previously been emitted. */
928 bool
929 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
931 static bool emitted_lax_note = false;
932 bool convertible_lax;
934 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
935 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
936 return true;
938 convertible_lax =
939 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
940 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
941 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
942 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
943 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
945 if (!convertible_lax || flag_lax_vector_conversions)
946 return convertible_lax;
948 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
949 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
950 return true;
952 if (emit_lax_note && !emitted_lax_note)
954 emitted_lax_note = true;
955 inform (input_location, "use -flax-vector-conversions to permit "
956 "conversions between vectors with differing "
957 "element types or numbers of subparts");
960 return false;
963 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
964 and have vector types, V0 has the same type as V1, and the number of
965 elements of V0, V1, MASK is the same.
967 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
968 called with two arguments. In this case implementation passes the
969 first argument twice in order to share the same tree code. This fact
970 could enable the mask-values being twice the vector length. This is
971 an implementation accident and this semantics is not guaranteed to
972 the user. */
973 tree
974 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
975 bool complain)
977 tree ret;
978 bool wrap = true;
979 bool maybe_const = false;
980 bool two_arguments = false;
982 if (v1 == NULL_TREE)
984 two_arguments = true;
985 v1 = v0;
988 if (v0 == error_mark_node || v1 == error_mark_node
989 || mask == error_mark_node)
990 return error_mark_node;
992 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
994 if (complain)
995 error_at (loc, "__builtin_shuffle last argument must "
996 "be an integer vector");
997 return error_mark_node;
1000 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1001 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1003 if (complain)
1004 error_at (loc, "__builtin_shuffle arguments must be vectors");
1005 return error_mark_node;
1008 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1010 if (complain)
1011 error_at (loc, "__builtin_shuffle argument vectors must be of "
1012 "the same type");
1013 return error_mark_node;
1016 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
1017 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
1018 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
1019 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1021 if (complain)
1022 error_at (loc, "__builtin_shuffle number of elements of the "
1023 "argument vector(s) and the mask vector should "
1024 "be the same");
1025 return error_mark_node;
1028 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1029 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1031 if (complain)
1032 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1033 "must have the same size as inner type of the mask");
1034 return error_mark_node;
1037 if (!c_dialect_cxx ())
1039 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1040 v0 = c_fully_fold (v0, false, &maybe_const);
1041 wrap &= maybe_const;
1043 if (two_arguments)
1044 v1 = v0 = save_expr (v0);
1045 else
1047 v1 = c_fully_fold (v1, false, &maybe_const);
1048 wrap &= maybe_const;
1051 mask = c_fully_fold (mask, false, &maybe_const);
1052 wrap &= maybe_const;
1054 else if (two_arguments)
1055 v1 = v0 = save_expr (v0);
1057 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1059 if (!c_dialect_cxx () && !wrap)
1060 ret = c_wrap_maybe_const (ret, true);
1062 return ret;
1065 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1066 to integral type. */
1068 tree
1069 c_common_get_narrower (tree op, int *unsignedp_ptr)
1071 op = get_narrower (op, unsignedp_ptr);
1073 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1074 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1076 /* C++0x scoped enumerations don't implicitly convert to integral
1077 type; if we stripped an explicit conversion to a larger type we
1078 need to replace it so common_type will still work. */
1079 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1080 TYPE_UNSIGNED (TREE_TYPE (op)));
1081 op = fold_convert (type, op);
1083 return op;
1086 /* This is a helper function of build_binary_op.
1088 For certain operations if both args were extended from the same
1089 smaller type, do the arithmetic in that type and then extend.
1091 BITWISE indicates a bitwise operation.
1092 For them, this optimization is safe only if
1093 both args are zero-extended or both are sign-extended.
1094 Otherwise, we might change the result.
1095 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1096 but calculated in (unsigned short) it would be (unsigned short)-1.
1098 tree
1099 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1101 int unsigned0, unsigned1;
1102 tree arg0, arg1;
1103 int uns;
1104 tree type;
1106 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1107 excessive narrowing when we call get_narrower below. For
1108 example, suppose that OP0 is of unsigned int extended
1109 from signed char and that RESULT_TYPE is long long int.
1110 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1111 like
1113 (long long int) (unsigned int) signed_char
1115 which get_narrower would narrow down to
1117 (unsigned int) signed char
1119 If we do not cast OP0 first, get_narrower would return
1120 signed_char, which is inconsistent with the case of the
1121 explicit cast. */
1122 op0 = convert (result_type, op0);
1123 op1 = convert (result_type, op1);
1125 arg0 = c_common_get_narrower (op0, &unsigned0);
1126 arg1 = c_common_get_narrower (op1, &unsigned1);
1128 /* UNS is 1 if the operation to be done is an unsigned one. */
1129 uns = TYPE_UNSIGNED (result_type);
1131 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1132 but it *requires* conversion to FINAL_TYPE. */
1134 if ((TYPE_PRECISION (TREE_TYPE (op0))
1135 == TYPE_PRECISION (TREE_TYPE (arg0)))
1136 && TREE_TYPE (op0) != result_type)
1137 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1138 if ((TYPE_PRECISION (TREE_TYPE (op1))
1139 == TYPE_PRECISION (TREE_TYPE (arg1)))
1140 && TREE_TYPE (op1) != result_type)
1141 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1143 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1145 /* For bitwise operations, signedness of nominal type
1146 does not matter. Consider only how operands were extended. */
1147 if (bitwise)
1148 uns = unsigned0;
1150 /* Note that in all three cases below we refrain from optimizing
1151 an unsigned operation on sign-extended args.
1152 That would not be valid. */
1154 /* Both args variable: if both extended in same way
1155 from same width, do it in that width.
1156 Do it unsigned if args were zero-extended. */
1157 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1158 < TYPE_PRECISION (result_type))
1159 && (TYPE_PRECISION (TREE_TYPE (arg1))
1160 == TYPE_PRECISION (TREE_TYPE (arg0)))
1161 && unsigned0 == unsigned1
1162 && (unsigned0 || !uns))
1163 return c_common_signed_or_unsigned_type
1164 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1166 else if (TREE_CODE (arg0) == INTEGER_CST
1167 && (unsigned1 || !uns)
1168 && (TYPE_PRECISION (TREE_TYPE (arg1))
1169 < TYPE_PRECISION (result_type))
1170 && (type
1171 = c_common_signed_or_unsigned_type (unsigned1,
1172 TREE_TYPE (arg1)))
1173 && !POINTER_TYPE_P (type)
1174 && int_fits_type_p (arg0, type))
1175 return type;
1177 else if (TREE_CODE (arg1) == INTEGER_CST
1178 && (unsigned0 || !uns)
1179 && (TYPE_PRECISION (TREE_TYPE (arg0))
1180 < TYPE_PRECISION (result_type))
1181 && (type
1182 = c_common_signed_or_unsigned_type (unsigned0,
1183 TREE_TYPE (arg0)))
1184 && !POINTER_TYPE_P (type)
1185 && int_fits_type_p (arg1, type))
1186 return type;
1188 return result_type;
1191 /* Returns true iff any integer value of type FROM_TYPE can be represented as
1192 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1194 static bool
1195 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1197 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1198 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1199 REAL_VALUE_TYPE real_low_bound =
1200 real_value_from_int_cst (0, type_low_bound);
1201 REAL_VALUE_TYPE real_high_bound =
1202 real_value_from_int_cst (0, type_high_bound);
1204 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1205 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1208 /* Checks if expression EXPR of complex/real/integer type cannot be converted
1209 to the complex/real/integer type TYPE. Function returns non-zero when:
1210 * EXPR is a constant which cannot be exactly converted to TYPE.
1211 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1212 for EXPR type and TYPE being both integers or both real, or both
1213 complex.
1214 * EXPR is not a constant of complex type and TYPE is a real or
1215 an integer.
1216 * EXPR is not a constant of real type and TYPE is an integer.
1217 * EXPR is not a constant of integer type which cannot be
1218 exactly converted to real type.
1220 Function allows conversions between types of different signedness and
1221 can return SAFE_CONVERSION (zero) in that case. Function can produce
1222 signedness warnings if PRODUCE_WARNS is true.
1224 Function allows conversions from complex constants to non-complex types,
1225 provided that imaginary part is zero and real part can be safely converted
1226 to TYPE. */
1228 enum conversion_safety
1229 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
1231 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1232 tree expr_type = TREE_TYPE (expr);
1233 loc = expansion_point_location_if_in_system_header (loc);
1235 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1237 /* If type is complex, we are interested in compatibility with
1238 underlying type. */
1239 if (TREE_CODE (type) == COMPLEX_TYPE)
1240 type = TREE_TYPE (type);
1242 /* Warn for real constant that is not an exact integer converted
1243 to integer type. */
1244 if (TREE_CODE (expr_type) == REAL_TYPE
1245 && TREE_CODE (type) == INTEGER_TYPE)
1247 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1248 give_warning = UNSAFE_REAL;
1250 /* Warn for an integer constant that does not fit into integer type. */
1251 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1252 && TREE_CODE (type) == INTEGER_TYPE
1253 && !int_fits_type_p (expr, type))
1255 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1256 && tree_int_cst_sgn (expr) < 0)
1258 if (produce_warns)
1259 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1260 " implicitly converted to unsigned type");
1262 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1264 if (produce_warns)
1265 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1266 " constant value to negative integer");
1268 else
1269 give_warning = UNSAFE_OTHER;
1271 else if (TREE_CODE (type) == REAL_TYPE)
1273 /* Warn for an integer constant that does not fit into real type. */
1274 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1276 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1277 if (!exact_real_truncate (TYPE_MODE (type), &a))
1278 give_warning = UNSAFE_REAL;
1280 /* Warn for a real constant that does not fit into a smaller
1281 real type. */
1282 else if (TREE_CODE (expr_type) == REAL_TYPE
1283 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1285 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1286 if (!exact_real_truncate (TYPE_MODE (type), &a))
1287 give_warning = UNSAFE_REAL;
1292 else if (TREE_CODE (expr) == COMPLEX_CST)
1294 tree imag_part = TREE_IMAGPART (expr);
1295 /* Conversion from complex constant with zero imaginary part,
1296 perform check for conversion of real part. */
1297 if ((TREE_CODE (imag_part) == REAL_CST
1298 && real_zerop (imag_part))
1299 || (TREE_CODE (imag_part) == INTEGER_CST
1300 && integer_zerop (imag_part)))
1301 /* Note: in this branch we use recursive call to unsafe_conversion_p
1302 with different type of EXPR, but it is still safe, because when EXPR
1303 is a constant, it's type is not used in text of generated warnings
1304 (otherwise they could sound misleading). */
1305 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1306 produce_warns);
1307 /* Conversion from complex constant with non-zero imaginary part. */
1308 else
1310 /* Conversion to complex type.
1311 Perform checks for both real and imaginary parts. */
1312 if (TREE_CODE (type) == COMPLEX_TYPE)
1314 /* Unfortunately, produce_warns must be false in two subsequent
1315 calls of unsafe_conversion_p, because otherwise we could
1316 produce strange "double" warnings, if both real and imaginary
1317 parts have conversion problems related to signedness.
1319 For example:
1320 int32_t _Complex a = 0x80000000 + 0x80000000i;
1322 Possible solution: add a separate function for checking
1323 constants and combine result of two calls appropriately. */
1324 enum conversion_safety re_safety =
1325 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
1326 enum conversion_safety im_safety =
1327 unsafe_conversion_p (loc, type, imag_part, false);
1329 /* Merge the results into appropriate single warning. */
1331 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1332 if (re_safety == im_safety)
1333 give_warning = re_safety;
1334 else if (!re_safety && im_safety)
1335 give_warning = im_safety;
1336 else if (re_safety && !im_safety)
1337 give_warning = re_safety;
1338 else
1339 give_warning = UNSAFE_OTHER;
1341 /* Warn about conversion from complex to real or integer type. */
1342 else
1343 give_warning = UNSAFE_IMAGINARY;
1347 /* Checks for remaining case: EXPR is not constant. */
1348 else
1350 /* Warn for real types converted to integer types. */
1351 if (TREE_CODE (expr_type) == REAL_TYPE
1352 && TREE_CODE (type) == INTEGER_TYPE)
1353 give_warning = UNSAFE_REAL;
1355 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1356 && TREE_CODE (type) == INTEGER_TYPE)
1358 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1359 expr = get_unwidened (expr, 0);
1360 expr_type = TREE_TYPE (expr);
1362 /* Don't warn for short y; short x = ((int)y & 0xff); */
1363 if (TREE_CODE (expr) == BIT_AND_EXPR
1364 || TREE_CODE (expr) == BIT_IOR_EXPR
1365 || TREE_CODE (expr) == BIT_XOR_EXPR)
1367 /* If both args were extended from a shortest type,
1368 use that type if that is safe. */
1369 expr_type = shorten_binary_op (expr_type,
1370 TREE_OPERAND (expr, 0),
1371 TREE_OPERAND (expr, 1),
1372 /* bitwise */1);
1374 if (TREE_CODE (expr) == BIT_AND_EXPR)
1376 tree op0 = TREE_OPERAND (expr, 0);
1377 tree op1 = TREE_OPERAND (expr, 1);
1378 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1379 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1381 /* If one of the operands is a non-negative constant
1382 that fits in the target type, then the type of the
1383 other operand does not matter. */
1384 if ((TREE_CODE (op0) == INTEGER_CST
1385 && int_fits_type_p (op0, c_common_signed_type (type))
1386 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1387 || (TREE_CODE (op1) == INTEGER_CST
1388 && int_fits_type_p (op1, c_common_signed_type (type))
1389 && int_fits_type_p (op1,
1390 c_common_unsigned_type (type))))
1391 return SAFE_CONVERSION;
1392 /* If constant is unsigned and fits in the target
1393 type, then the result will also fit. */
1394 else if ((TREE_CODE (op0) == INTEGER_CST
1395 && unsigned0
1396 && int_fits_type_p (op0, type))
1397 || (TREE_CODE (op1) == INTEGER_CST
1398 && unsigned1
1399 && int_fits_type_p (op1, type)))
1400 return SAFE_CONVERSION;
1403 /* Warn for integer types converted to smaller integer types. */
1404 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1405 give_warning = UNSAFE_OTHER;
1407 /* When they are the same width but different signedness,
1408 then the value may change. */
1409 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1410 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1411 /* Even when converted to a bigger type, if the type is
1412 unsigned but expr is signed, then negative values
1413 will be changed. */
1414 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1415 && produce_warns)
1416 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1417 "may change the sign of the result",
1418 type, expr_type);
1421 /* Warn for integer types converted to real types if and only if
1422 all the range of values of the integer type cannot be
1423 represented by the real type. */
1424 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1425 && TREE_CODE (type) == REAL_TYPE)
1427 /* Don't warn about char y = 0xff; float x = (int) y; */
1428 expr = get_unwidened (expr, 0);
1429 expr_type = TREE_TYPE (expr);
1431 if (!int_safely_convertible_to_real_p (expr_type, type))
1432 give_warning = UNSAFE_OTHER;
1435 /* Warn for real types converted to smaller real types. */
1436 else if (TREE_CODE (expr_type) == REAL_TYPE
1437 && TREE_CODE (type) == REAL_TYPE
1438 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1439 give_warning = UNSAFE_REAL;
1441 /* Check conversion between two complex types. */
1442 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1443 && TREE_CODE (type) == COMPLEX_TYPE)
1445 /* Extract underlying types (i.e., type of real and imaginary
1446 parts) of expr_type and type. */
1447 tree from_type = TREE_TYPE (expr_type);
1448 tree to_type = TREE_TYPE (type);
1450 /* Warn for real types converted to integer types. */
1451 if (TREE_CODE (from_type) == REAL_TYPE
1452 && TREE_CODE (to_type) == INTEGER_TYPE)
1453 give_warning = UNSAFE_REAL;
1455 /* Warn for real types converted to smaller real types. */
1456 else if (TREE_CODE (from_type) == REAL_TYPE
1457 && TREE_CODE (to_type) == REAL_TYPE
1458 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1459 give_warning = UNSAFE_REAL;
1461 /* Check conversion for complex integer types. Here implementation
1462 is simpler than for real-domain integers because it does not
1463 involve sophisticated cases, such as bitmasks, casts, etc. */
1464 else if (TREE_CODE (from_type) == INTEGER_TYPE
1465 && TREE_CODE (to_type) == INTEGER_TYPE)
1467 /* Warn for integer types converted to smaller integer types. */
1468 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1469 give_warning = UNSAFE_OTHER;
1471 /* Check for different signedness, see case for real-domain
1472 integers (above) for a more detailed comment. */
1473 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1474 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1475 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1476 && produce_warns)
1477 warning_at (loc, OPT_Wsign_conversion,
1478 "conversion to %qT from %qT "
1479 "may change the sign of the result",
1480 type, expr_type);
1482 else if (TREE_CODE (from_type) == INTEGER_TYPE
1483 && TREE_CODE (to_type) == REAL_TYPE
1484 && !int_safely_convertible_to_real_p (from_type, to_type))
1485 give_warning = UNSAFE_OTHER;
1488 /* Warn for complex types converted to real or integer types. */
1489 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1490 && TREE_CODE (type) != COMPLEX_TYPE)
1491 give_warning = UNSAFE_IMAGINARY;
1494 return give_warning;
1498 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1499 Invoke this function on every expression that is converted implicitly,
1500 i.e. because of language rules and not because of an explicit cast. */
1502 tree
1503 convert_and_check (location_t loc, tree type, tree expr)
1505 tree result;
1506 tree expr_for_warning;
1508 /* Convert from a value with possible excess precision rather than
1509 via the semantic type, but do not warn about values not fitting
1510 exactly in the semantic type. */
1511 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1513 tree orig_type = TREE_TYPE (expr);
1514 expr = TREE_OPERAND (expr, 0);
1515 expr_for_warning = convert (orig_type, expr);
1516 if (orig_type == type)
1517 return expr_for_warning;
1519 else
1520 expr_for_warning = expr;
1522 if (TREE_TYPE (expr) == type)
1523 return expr;
1525 result = convert (type, expr);
1527 if (c_inhibit_evaluation_warnings == 0
1528 && !TREE_OVERFLOW_P (expr)
1529 && result != error_mark_node)
1530 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1532 return result;
1535 /* A node in a list that describes references to variables (EXPR), which are
1536 either read accesses if WRITER is zero, or write accesses, in which case
1537 WRITER is the parent of EXPR. */
1538 struct tlist
1540 struct tlist *next;
1541 tree expr, writer;
1544 /* Used to implement a cache the results of a call to verify_tree. We only
1545 use this for SAVE_EXPRs. */
1546 struct tlist_cache
1548 struct tlist_cache *next;
1549 struct tlist *cache_before_sp;
1550 struct tlist *cache_after_sp;
1551 tree expr;
1554 /* Obstack to use when allocating tlist structures, and corresponding
1555 firstobj. */
1556 static struct obstack tlist_obstack;
1557 static char *tlist_firstobj = 0;
1559 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1560 warnings. */
1561 static struct tlist *warned_ids;
1562 /* SAVE_EXPRs need special treatment. We process them only once and then
1563 cache the results. */
1564 static struct tlist_cache *save_expr_cache;
1566 static void add_tlist (struct tlist **, struct tlist *, tree, int);
1567 static void merge_tlist (struct tlist **, struct tlist *, int);
1568 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1569 static bool warning_candidate_p (tree);
1570 static bool candidate_equal_p (const_tree, const_tree);
1571 static void warn_for_collisions (struct tlist *);
1572 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1573 static struct tlist *new_tlist (struct tlist *, tree, tree);
1575 /* Create a new struct tlist and fill in its fields. */
1576 static struct tlist *
1577 new_tlist (struct tlist *next, tree t, tree writer)
1579 struct tlist *l;
1580 l = XOBNEW (&tlist_obstack, struct tlist);
1581 l->next = next;
1582 l->expr = t;
1583 l->writer = writer;
1584 return l;
1587 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1588 is nonnull, we ignore any node we find which has a writer equal to it. */
1590 static void
1591 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1593 while (add)
1595 struct tlist *next = add->next;
1596 if (!copy)
1597 add->next = *to;
1598 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1599 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1600 add = next;
1604 /* Merge the nodes of ADD into TO. This merging process is done so that for
1605 each variable that already exists in TO, no new node is added; however if
1606 there is a write access recorded in ADD, and an occurrence on TO is only
1607 a read access, then the occurrence in TO will be modified to record the
1608 write. */
1610 static void
1611 merge_tlist (struct tlist **to, struct tlist *add, int copy)
1613 struct tlist **end = to;
1615 while (*end)
1616 end = &(*end)->next;
1618 while (add)
1620 int found = 0;
1621 struct tlist *tmp2;
1622 struct tlist *next = add->next;
1624 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1625 if (candidate_equal_p (tmp2->expr, add->expr))
1627 found = 1;
1628 if (!tmp2->writer)
1629 tmp2->writer = add->writer;
1631 if (!found)
1633 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1634 end = &(*end)->next;
1635 *end = 0;
1637 add = next;
1641 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1642 references in list LIST conflict with it, excluding reads if ONLY writers
1643 is nonzero. */
1645 static void
1646 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1647 int only_writes)
1649 struct tlist *tmp;
1651 /* Avoid duplicate warnings. */
1652 for (tmp = warned_ids; tmp; tmp = tmp->next)
1653 if (candidate_equal_p (tmp->expr, written))
1654 return;
1656 while (list)
1658 if (candidate_equal_p (list->expr, written)
1659 && !candidate_equal_p (list->writer, writer)
1660 && (!only_writes || list->writer))
1662 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1663 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1664 OPT_Wsequence_point, "operation on %qE may be undefined",
1665 list->expr);
1667 list = list->next;
1671 /* Given a list LIST of references to variables, find whether any of these
1672 can cause conflicts due to missing sequence points. */
1674 static void
1675 warn_for_collisions (struct tlist *list)
1677 struct tlist *tmp;
1679 for (tmp = list; tmp; tmp = tmp->next)
1681 if (tmp->writer)
1682 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1686 /* Return nonzero if X is a tree that can be verified by the sequence point
1687 warnings. */
1689 static bool
1690 warning_candidate_p (tree x)
1692 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1693 return false;
1695 if (TREE_CODE (x) == BLOCK)
1696 return false;
1698 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1699 (lvalue_p) crash on TRY/CATCH. */
1700 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1701 return false;
1703 if (!lvalue_p (x))
1704 return false;
1706 /* No point to track non-const calls, they will never satisfy
1707 operand_equal_p. */
1708 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1709 return false;
1711 if (TREE_CODE (x) == STRING_CST)
1712 return false;
1714 return true;
1717 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1718 static bool
1719 candidate_equal_p (const_tree x, const_tree y)
1721 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1724 /* Walk the tree X, and record accesses to variables. If X is written by the
1725 parent tree, WRITER is the parent.
1726 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1727 expression or its only operand forces a sequence point, then everything up
1728 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1729 in PNO_SP.
1730 Once we return, we will have emitted warnings if any subexpression before
1731 such a sequence point could be undefined. On a higher level, however, the
1732 sequence point may not be relevant, and we'll merge the two lists.
1734 Example: (b++, a) + b;
1735 The call that processes the COMPOUND_EXPR will store the increment of B
1736 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1737 processes the PLUS_EXPR will need to merge the two lists so that
1738 eventually, all accesses end up on the same list (and we'll warn about the
1739 unordered subexpressions b++ and b.
1741 A note on merging. If we modify the former example so that our expression
1742 becomes
1743 (b++, b) + a
1744 care must be taken not simply to add all three expressions into the final
1745 PNO_SP list. The function merge_tlist takes care of that by merging the
1746 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1747 way, so that no more than one access to B is recorded. */
1749 static void
1750 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1751 tree writer)
1753 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1754 enum tree_code code;
1755 enum tree_code_class cl;
1757 /* X may be NULL if it is the operand of an empty statement expression
1758 ({ }). */
1759 if (x == NULL)
1760 return;
1762 restart:
1763 code = TREE_CODE (x);
1764 cl = TREE_CODE_CLASS (code);
1766 if (warning_candidate_p (x))
1767 *pno_sp = new_tlist (*pno_sp, x, writer);
1769 switch (code)
1771 case CONSTRUCTOR:
1772 case SIZEOF_EXPR:
1773 return;
1775 case COMPOUND_EXPR:
1776 case TRUTH_ANDIF_EXPR:
1777 case TRUTH_ORIF_EXPR:
1778 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1779 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1780 warn_for_collisions (tmp_nosp);
1781 merge_tlist (pbefore_sp, tmp_before, 0);
1782 merge_tlist (pbefore_sp, tmp_nosp, 0);
1783 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1784 warn_for_collisions (tmp_list2);
1785 merge_tlist (pbefore_sp, tmp_list3, 0);
1786 merge_tlist (pno_sp, tmp_list2, 0);
1787 return;
1789 case COND_EXPR:
1790 tmp_before = tmp_list2 = 0;
1791 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1792 warn_for_collisions (tmp_list2);
1793 merge_tlist (pbefore_sp, tmp_before, 0);
1794 merge_tlist (pbefore_sp, tmp_list2, 0);
1796 tmp_list3 = tmp_nosp = 0;
1797 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1798 warn_for_collisions (tmp_nosp);
1799 merge_tlist (pbefore_sp, tmp_list3, 0);
1801 tmp_list3 = tmp_list2 = 0;
1802 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1803 warn_for_collisions (tmp_list2);
1804 merge_tlist (pbefore_sp, tmp_list3, 0);
1805 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1806 two first, to avoid warning for (a ? b++ : b++). */
1807 merge_tlist (&tmp_nosp, tmp_list2, 0);
1808 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1809 return;
1811 case PREDECREMENT_EXPR:
1812 case PREINCREMENT_EXPR:
1813 case POSTDECREMENT_EXPR:
1814 case POSTINCREMENT_EXPR:
1815 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1816 return;
1818 case MODIFY_EXPR:
1819 tmp_before = tmp_nosp = tmp_list3 = 0;
1820 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1821 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1822 /* Expressions inside the LHS are not ordered wrt. the sequence points
1823 in the RHS. Example:
1824 *a = (a++, 2)
1825 Despite the fact that the modification of "a" is in the before_sp
1826 list (tmp_before), it conflicts with the use of "a" in the LHS.
1827 We can handle this by adding the contents of tmp_list3
1828 to those of tmp_before, and redoing the collision warnings for that
1829 list. */
1830 add_tlist (&tmp_before, tmp_list3, x, 1);
1831 warn_for_collisions (tmp_before);
1832 /* Exclude the LHS itself here; we first have to merge it into the
1833 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1834 didn't exclude the LHS, we'd get it twice, once as a read and once
1835 as a write. */
1836 add_tlist (pno_sp, tmp_list3, x, 0);
1837 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1839 merge_tlist (pbefore_sp, tmp_before, 0);
1840 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1841 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1842 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1843 return;
1845 case CALL_EXPR:
1846 /* We need to warn about conflicts among arguments and conflicts between
1847 args and the function address. Side effects of the function address,
1848 however, are not ordered by the sequence point of the call. */
1850 call_expr_arg_iterator iter;
1851 tree arg;
1852 tmp_before = tmp_nosp = 0;
1853 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1854 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1856 tmp_list2 = tmp_list3 = 0;
1857 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1858 merge_tlist (&tmp_list3, tmp_list2, 0);
1859 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1861 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1862 warn_for_collisions (tmp_before);
1863 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1864 return;
1867 case TREE_LIST:
1868 /* Scan all the list, e.g. indices of multi dimensional array. */
1869 while (x)
1871 tmp_before = tmp_nosp = 0;
1872 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1873 merge_tlist (&tmp_nosp, tmp_before, 0);
1874 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1875 x = TREE_CHAIN (x);
1877 return;
1879 case SAVE_EXPR:
1881 struct tlist_cache *t;
1882 for (t = save_expr_cache; t; t = t->next)
1883 if (candidate_equal_p (t->expr, x))
1884 break;
1886 if (!t)
1888 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1889 t->next = save_expr_cache;
1890 t->expr = x;
1891 save_expr_cache = t;
1893 tmp_before = tmp_nosp = 0;
1894 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1895 warn_for_collisions (tmp_nosp);
1897 tmp_list3 = 0;
1898 merge_tlist (&tmp_list3, tmp_nosp, 0);
1899 t->cache_before_sp = tmp_before;
1900 t->cache_after_sp = tmp_list3;
1902 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1903 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1904 return;
1907 case ADDR_EXPR:
1908 x = TREE_OPERAND (x, 0);
1909 if (DECL_P (x))
1910 return;
1911 writer = 0;
1912 goto restart;
1914 default:
1915 /* For other expressions, simply recurse on their operands.
1916 Manual tail recursion for unary expressions.
1917 Other non-expressions need not be processed. */
1918 if (cl == tcc_unary)
1920 x = TREE_OPERAND (x, 0);
1921 writer = 0;
1922 goto restart;
1924 else if (IS_EXPR_CODE_CLASS (cl))
1926 int lp;
1927 int max = TREE_OPERAND_LENGTH (x);
1928 for (lp = 0; lp < max; lp++)
1930 tmp_before = tmp_nosp = 0;
1931 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1932 merge_tlist (&tmp_nosp, tmp_before, 0);
1933 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1936 return;
1940 /* Try to warn for undefined behavior in EXPR due to missing sequence
1941 points. */
1943 DEBUG_FUNCTION void
1944 verify_sequence_points (tree expr)
1946 struct tlist *before_sp = 0, *after_sp = 0;
1948 warned_ids = 0;
1949 save_expr_cache = 0;
1950 if (tlist_firstobj == 0)
1952 gcc_obstack_init (&tlist_obstack);
1953 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1956 verify_tree (expr, &before_sp, &after_sp, 0);
1957 warn_for_collisions (after_sp);
1958 obstack_free (&tlist_obstack, tlist_firstobj);
1961 /* Validate the expression after `case' and apply default promotions. */
1963 static tree
1964 check_case_value (location_t loc, tree value)
1966 if (value == NULL_TREE)
1967 return value;
1969 if (TREE_CODE (value) == INTEGER_CST)
1970 /* Promote char or short to int. */
1971 value = perform_integral_promotions (value);
1972 else if (value != error_mark_node)
1974 error_at (loc, "case label does not reduce to an integer constant");
1975 value = error_mark_node;
1978 constant_expression_warning (value);
1980 return value;
1983 /* See if the case values LOW and HIGH are in the range of the original
1984 type (i.e. before the default conversion to int) of the switch testing
1985 expression.
1986 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
1987 the type before promoting it. CASE_LOW_P is a pointer to the lower
1988 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
1989 if the case is not a case range.
1990 The caller has to make sure that we are not called with NULL for
1991 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
1992 was a case value that doesn't fit into the range of the ORIG_TYPE.
1993 Returns true if the case label is in range of ORIG_TYPE (saturated or
1994 untouched) or false if the label is out of range. */
1996 static bool
1997 check_case_bounds (location_t loc, tree type, tree orig_type,
1998 tree *case_low_p, tree *case_high_p,
1999 bool *outside_range_p)
2001 tree min_value, max_value;
2002 tree case_low = *case_low_p;
2003 tree case_high = case_high_p ? *case_high_p : case_low;
2005 /* If there was a problem with the original type, do nothing. */
2006 if (orig_type == error_mark_node)
2007 return true;
2009 min_value = TYPE_MIN_VALUE (orig_type);
2010 max_value = TYPE_MAX_VALUE (orig_type);
2012 /* We'll really need integer constants here. */
2013 case_low = fold (case_low);
2014 case_high = fold (case_high);
2016 /* Case label is less than minimum for type. */
2017 if (tree_int_cst_compare (case_low, min_value) < 0
2018 && tree_int_cst_compare (case_high, min_value) < 0)
2020 warning_at (loc, 0, "case label value is less than minimum value "
2021 "for type");
2022 *outside_range_p = true;
2023 return false;
2026 /* Case value is greater than maximum for type. */
2027 if (tree_int_cst_compare (case_low, max_value) > 0
2028 && tree_int_cst_compare (case_high, max_value) > 0)
2030 warning_at (loc, 0, "case label value exceeds maximum value for type");
2031 *outside_range_p = true;
2032 return false;
2035 /* Saturate lower case label value to minimum. */
2036 if (tree_int_cst_compare (case_high, min_value) >= 0
2037 && tree_int_cst_compare (case_low, min_value) < 0)
2039 warning_at (loc, 0, "lower value in case label range"
2040 " less than minimum value for type");
2041 *outside_range_p = true;
2042 case_low = min_value;
2045 /* Saturate upper case label value to maximum. */
2046 if (tree_int_cst_compare (case_low, max_value) <= 0
2047 && tree_int_cst_compare (case_high, max_value) > 0)
2049 warning_at (loc, 0, "upper value in case label range"
2050 " exceeds maximum value for type");
2051 *outside_range_p = true;
2052 case_high = max_value;
2055 if (*case_low_p != case_low)
2056 *case_low_p = convert (type, case_low);
2057 if (case_high_p && *case_high_p != case_high)
2058 *case_high_p = convert (type, case_high);
2060 return true;
2063 /* Return an integer type with BITS bits of precision,
2064 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2066 tree
2067 c_common_type_for_size (unsigned int bits, int unsignedp)
2069 int i;
2071 if (bits == TYPE_PRECISION (integer_type_node))
2072 return unsignedp ? unsigned_type_node : integer_type_node;
2074 if (bits == TYPE_PRECISION (signed_char_type_node))
2075 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2077 if (bits == TYPE_PRECISION (short_integer_type_node))
2078 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2080 if (bits == TYPE_PRECISION (long_integer_type_node))
2081 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2083 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2084 return (unsignedp ? long_long_unsigned_type_node
2085 : long_long_integer_type_node);
2087 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2088 if (int_n_enabled_p[i]
2089 && bits == int_n_data[i].bitsize)
2090 return (unsignedp ? int_n_trees[i].unsigned_type
2091 : int_n_trees[i].signed_type);
2093 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2094 return (unsignedp ? widest_unsigned_literal_type_node
2095 : widest_integer_literal_type_node);
2097 if (bits <= TYPE_PRECISION (intQI_type_node))
2098 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2100 if (bits <= TYPE_PRECISION (intHI_type_node))
2101 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2103 if (bits <= TYPE_PRECISION (intSI_type_node))
2104 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2106 if (bits <= TYPE_PRECISION (intDI_type_node))
2107 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2109 return 0;
2112 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2113 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2114 and saturating if SATP is nonzero, otherwise not saturating. */
2116 tree
2117 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2118 int unsignedp, int satp)
2120 machine_mode mode;
2121 if (ibit == 0)
2122 mode = unsignedp ? UQQmode : QQmode;
2123 else
2124 mode = unsignedp ? UHAmode : HAmode;
2126 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2127 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2128 break;
2130 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2132 sorry ("GCC cannot support operators with integer types and "
2133 "fixed-point types that have too many integral and "
2134 "fractional bits together");
2135 return 0;
2138 return c_common_type_for_mode (mode, satp);
2141 /* Used for communication between c_common_type_for_mode and
2142 c_register_builtin_type. */
2143 tree registered_builtin_types;
2145 /* Return a data type that has machine mode MODE.
2146 If the mode is an integer,
2147 then UNSIGNEDP selects between signed and unsigned types.
2148 If the mode is a fixed-point mode,
2149 then UNSIGNEDP selects between saturating and nonsaturating types. */
2151 tree
2152 c_common_type_for_mode (machine_mode mode, int unsignedp)
2154 tree t;
2155 int i;
2157 if (mode == TYPE_MODE (integer_type_node))
2158 return unsignedp ? unsigned_type_node : integer_type_node;
2160 if (mode == TYPE_MODE (signed_char_type_node))
2161 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2163 if (mode == TYPE_MODE (short_integer_type_node))
2164 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2166 if (mode == TYPE_MODE (long_integer_type_node))
2167 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2169 if (mode == TYPE_MODE (long_long_integer_type_node))
2170 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2172 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2173 if (int_n_enabled_p[i]
2174 && mode == int_n_data[i].m)
2175 return (unsignedp ? int_n_trees[i].unsigned_type
2176 : int_n_trees[i].signed_type);
2178 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2179 return unsignedp ? widest_unsigned_literal_type_node
2180 : widest_integer_literal_type_node;
2182 if (mode == QImode)
2183 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2185 if (mode == HImode)
2186 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2188 if (mode == SImode)
2189 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2191 if (mode == DImode)
2192 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2194 #if HOST_BITS_PER_WIDE_INT >= 64
2195 if (mode == TYPE_MODE (intTI_type_node))
2196 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2197 #endif
2199 if (mode == TYPE_MODE (float_type_node))
2200 return float_type_node;
2202 if (mode == TYPE_MODE (double_type_node))
2203 return double_type_node;
2205 if (mode == TYPE_MODE (long_double_type_node))
2206 return long_double_type_node;
2208 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2209 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2210 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2211 return FLOATN_NX_TYPE_NODE (i);
2213 if (mode == TYPE_MODE (void_type_node))
2214 return void_type_node;
2216 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2217 return (unsignedp
2218 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2219 : make_signed_type (GET_MODE_PRECISION (mode)));
2221 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2222 return (unsignedp
2223 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2224 : make_signed_type (GET_MODE_PRECISION (mode)));
2226 if (COMPLEX_MODE_P (mode))
2228 machine_mode inner_mode;
2229 tree inner_type;
2231 if (mode == TYPE_MODE (complex_float_type_node))
2232 return complex_float_type_node;
2233 if (mode == TYPE_MODE (complex_double_type_node))
2234 return complex_double_type_node;
2235 if (mode == TYPE_MODE (complex_long_double_type_node))
2236 return complex_long_double_type_node;
2238 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2239 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2240 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2241 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2243 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2244 return complex_integer_type_node;
2246 inner_mode = GET_MODE_INNER (mode);
2247 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2248 if (inner_type != NULL_TREE)
2249 return build_complex_type (inner_type);
2251 else if (VECTOR_MODE_P (mode))
2253 machine_mode inner_mode = GET_MODE_INNER (mode);
2254 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2255 if (inner_type != NULL_TREE)
2256 return build_vector_type_for_mode (inner_type, mode);
2259 if (mode == TYPE_MODE (dfloat32_type_node))
2260 return dfloat32_type_node;
2261 if (mode == TYPE_MODE (dfloat64_type_node))
2262 return dfloat64_type_node;
2263 if (mode == TYPE_MODE (dfloat128_type_node))
2264 return dfloat128_type_node;
2266 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2268 if (mode == TYPE_MODE (short_fract_type_node))
2269 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2270 if (mode == TYPE_MODE (fract_type_node))
2271 return unsignedp ? sat_fract_type_node : fract_type_node;
2272 if (mode == TYPE_MODE (long_fract_type_node))
2273 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2274 if (mode == TYPE_MODE (long_long_fract_type_node))
2275 return unsignedp ? sat_long_long_fract_type_node
2276 : long_long_fract_type_node;
2278 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2279 return unsignedp ? sat_unsigned_short_fract_type_node
2280 : unsigned_short_fract_type_node;
2281 if (mode == TYPE_MODE (unsigned_fract_type_node))
2282 return unsignedp ? sat_unsigned_fract_type_node
2283 : unsigned_fract_type_node;
2284 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2285 return unsignedp ? sat_unsigned_long_fract_type_node
2286 : unsigned_long_fract_type_node;
2287 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2288 return unsignedp ? sat_unsigned_long_long_fract_type_node
2289 : unsigned_long_long_fract_type_node;
2291 if (mode == TYPE_MODE (short_accum_type_node))
2292 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2293 if (mode == TYPE_MODE (accum_type_node))
2294 return unsignedp ? sat_accum_type_node : accum_type_node;
2295 if (mode == TYPE_MODE (long_accum_type_node))
2296 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2297 if (mode == TYPE_MODE (long_long_accum_type_node))
2298 return unsignedp ? sat_long_long_accum_type_node
2299 : long_long_accum_type_node;
2301 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2302 return unsignedp ? sat_unsigned_short_accum_type_node
2303 : unsigned_short_accum_type_node;
2304 if (mode == TYPE_MODE (unsigned_accum_type_node))
2305 return unsignedp ? sat_unsigned_accum_type_node
2306 : unsigned_accum_type_node;
2307 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2308 return unsignedp ? sat_unsigned_long_accum_type_node
2309 : unsigned_long_accum_type_node;
2310 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2311 return unsignedp ? sat_unsigned_long_long_accum_type_node
2312 : unsigned_long_long_accum_type_node;
2314 if (mode == QQmode)
2315 return unsignedp ? sat_qq_type_node : qq_type_node;
2316 if (mode == HQmode)
2317 return unsignedp ? sat_hq_type_node : hq_type_node;
2318 if (mode == SQmode)
2319 return unsignedp ? sat_sq_type_node : sq_type_node;
2320 if (mode == DQmode)
2321 return unsignedp ? sat_dq_type_node : dq_type_node;
2322 if (mode == TQmode)
2323 return unsignedp ? sat_tq_type_node : tq_type_node;
2325 if (mode == UQQmode)
2326 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2327 if (mode == UHQmode)
2328 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2329 if (mode == USQmode)
2330 return unsignedp ? sat_usq_type_node : usq_type_node;
2331 if (mode == UDQmode)
2332 return unsignedp ? sat_udq_type_node : udq_type_node;
2333 if (mode == UTQmode)
2334 return unsignedp ? sat_utq_type_node : utq_type_node;
2336 if (mode == HAmode)
2337 return unsignedp ? sat_ha_type_node : ha_type_node;
2338 if (mode == SAmode)
2339 return unsignedp ? sat_sa_type_node : sa_type_node;
2340 if (mode == DAmode)
2341 return unsignedp ? sat_da_type_node : da_type_node;
2342 if (mode == TAmode)
2343 return unsignedp ? sat_ta_type_node : ta_type_node;
2345 if (mode == UHAmode)
2346 return unsignedp ? sat_uha_type_node : uha_type_node;
2347 if (mode == USAmode)
2348 return unsignedp ? sat_usa_type_node : usa_type_node;
2349 if (mode == UDAmode)
2350 return unsignedp ? sat_uda_type_node : uda_type_node;
2351 if (mode == UTAmode)
2352 return unsignedp ? sat_uta_type_node : uta_type_node;
2355 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2356 if (TYPE_MODE (TREE_VALUE (t)) == mode
2357 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2358 return TREE_VALUE (t);
2360 return 0;
2363 tree
2364 c_common_unsigned_type (tree type)
2366 return c_common_signed_or_unsigned_type (1, type);
2369 /* Return a signed type the same as TYPE in other respects. */
2371 tree
2372 c_common_signed_type (tree type)
2374 return c_common_signed_or_unsigned_type (0, type);
2377 /* Return a type the same as TYPE except unsigned or
2378 signed according to UNSIGNEDP. */
2380 tree
2381 c_common_signed_or_unsigned_type (int unsignedp, tree type)
2383 tree type1;
2384 int i;
2386 /* This block of code emulates the behavior of the old
2387 c_common_unsigned_type. In particular, it returns
2388 long_unsigned_type_node if passed a long, even when a int would
2389 have the same size. This is necessary for warnings to work
2390 correctly in archs where sizeof(int) == sizeof(long) */
2392 type1 = TYPE_MAIN_VARIANT (type);
2393 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2394 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2395 if (type1 == integer_type_node || type1 == unsigned_type_node)
2396 return unsignedp ? unsigned_type_node : integer_type_node;
2397 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2398 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2399 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2400 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2401 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2402 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2404 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2405 if (int_n_enabled_p[i]
2406 && (type1 == int_n_trees[i].unsigned_type
2407 || type1 == int_n_trees[i].signed_type))
2408 return (unsignedp ? int_n_trees[i].unsigned_type
2409 : int_n_trees[i].signed_type);
2411 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
2412 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
2413 #if HOST_BITS_PER_WIDE_INT >= 64
2414 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2415 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2416 #endif
2417 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2418 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2419 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2420 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2421 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2422 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2423 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2424 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2426 #define C_COMMON_FIXED_TYPES(NAME) \
2427 if (type1 == short_ ## NAME ## _type_node \
2428 || type1 == unsigned_short_ ## NAME ## _type_node) \
2429 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2430 : short_ ## NAME ## _type_node; \
2431 if (type1 == NAME ## _type_node \
2432 || type1 == unsigned_ ## NAME ## _type_node) \
2433 return unsignedp ? unsigned_ ## NAME ## _type_node \
2434 : NAME ## _type_node; \
2435 if (type1 == long_ ## NAME ## _type_node \
2436 || type1 == unsigned_long_ ## NAME ## _type_node) \
2437 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2438 : long_ ## NAME ## _type_node; \
2439 if (type1 == long_long_ ## NAME ## _type_node \
2440 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2441 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2442 : long_long_ ## NAME ## _type_node;
2444 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2445 if (type1 == NAME ## _type_node \
2446 || type1 == u ## NAME ## _type_node) \
2447 return unsignedp ? u ## NAME ## _type_node \
2448 : NAME ## _type_node;
2450 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2451 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2452 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2453 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2454 : sat_ ## short_ ## NAME ## _type_node; \
2455 if (type1 == sat_ ## NAME ## _type_node \
2456 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2457 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2458 : sat_ ## NAME ## _type_node; \
2459 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2460 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2461 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2462 : sat_ ## long_ ## NAME ## _type_node; \
2463 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2464 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2465 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2466 : sat_ ## long_long_ ## NAME ## _type_node;
2468 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2469 if (type1 == sat_ ## NAME ## _type_node \
2470 || type1 == sat_ ## u ## NAME ## _type_node) \
2471 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2472 : sat_ ## NAME ## _type_node;
2474 C_COMMON_FIXED_TYPES (fract);
2475 C_COMMON_FIXED_TYPES_SAT (fract);
2476 C_COMMON_FIXED_TYPES (accum);
2477 C_COMMON_FIXED_TYPES_SAT (accum);
2479 C_COMMON_FIXED_MODE_TYPES (qq);
2480 C_COMMON_FIXED_MODE_TYPES (hq);
2481 C_COMMON_FIXED_MODE_TYPES (sq);
2482 C_COMMON_FIXED_MODE_TYPES (dq);
2483 C_COMMON_FIXED_MODE_TYPES (tq);
2484 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2485 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2486 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2487 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2488 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2489 C_COMMON_FIXED_MODE_TYPES (ha);
2490 C_COMMON_FIXED_MODE_TYPES (sa);
2491 C_COMMON_FIXED_MODE_TYPES (da);
2492 C_COMMON_FIXED_MODE_TYPES (ta);
2493 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2494 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2495 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2496 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2498 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2499 the precision; they have precision set to match their range, but
2500 may use a wider mode to match an ABI. If we change modes, we may
2501 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2502 the precision as well, so as to yield correct results for
2503 bit-field types. C++ does not have these separate bit-field
2504 types, and producing a signed or unsigned variant of an
2505 ENUMERAL_TYPE may cause other problems as well. */
2507 if (!INTEGRAL_TYPE_P (type)
2508 || TYPE_UNSIGNED (type) == unsignedp)
2509 return type;
2511 #define TYPE_OK(node) \
2512 (TYPE_MODE (type) == TYPE_MODE (node) \
2513 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2514 if (TYPE_OK (signed_char_type_node))
2515 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2516 if (TYPE_OK (integer_type_node))
2517 return unsignedp ? unsigned_type_node : integer_type_node;
2518 if (TYPE_OK (short_integer_type_node))
2519 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2520 if (TYPE_OK (long_integer_type_node))
2521 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2522 if (TYPE_OK (long_long_integer_type_node))
2523 return (unsignedp ? long_long_unsigned_type_node
2524 : long_long_integer_type_node);
2526 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2527 if (int_n_enabled_p[i]
2528 && TYPE_MODE (type) == int_n_data[i].m
2529 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2530 return (unsignedp ? int_n_trees[i].unsigned_type
2531 : int_n_trees[i].signed_type);
2533 if (TYPE_OK (widest_integer_literal_type_node))
2534 return (unsignedp ? widest_unsigned_literal_type_node
2535 : widest_integer_literal_type_node);
2537 #if HOST_BITS_PER_WIDE_INT >= 64
2538 if (TYPE_OK (intTI_type_node))
2539 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2540 #endif
2541 if (TYPE_OK (intDI_type_node))
2542 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2543 if (TYPE_OK (intSI_type_node))
2544 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2545 if (TYPE_OK (intHI_type_node))
2546 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2547 if (TYPE_OK (intQI_type_node))
2548 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2549 #undef TYPE_OK
2551 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2554 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2556 tree
2557 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2559 int i;
2561 /* Extended integer types of the same width as a standard type have
2562 lesser rank, so those of the same width as int promote to int or
2563 unsigned int and are valid for printf formats expecting int or
2564 unsigned int. To avoid such special cases, avoid creating
2565 extended integer types for bit-fields if a standard integer type
2566 is available. */
2567 if (width == TYPE_PRECISION (integer_type_node))
2568 return unsignedp ? unsigned_type_node : integer_type_node;
2569 if (width == TYPE_PRECISION (signed_char_type_node))
2570 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2571 if (width == TYPE_PRECISION (short_integer_type_node))
2572 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2573 if (width == TYPE_PRECISION (long_integer_type_node))
2574 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2575 if (width == TYPE_PRECISION (long_long_integer_type_node))
2576 return (unsignedp ? long_long_unsigned_type_node
2577 : long_long_integer_type_node);
2578 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2579 if (int_n_enabled_p[i]
2580 && width == int_n_data[i].bitsize)
2581 return (unsignedp ? int_n_trees[i].unsigned_type
2582 : int_n_trees[i].signed_type);
2583 return build_nonstandard_integer_type (width, unsignedp);
2586 /* The C version of the register_builtin_type langhook. */
2588 void
2589 c_register_builtin_type (tree type, const char* name)
2591 tree decl;
2593 decl = build_decl (UNKNOWN_LOCATION,
2594 TYPE_DECL, get_identifier (name), type);
2595 DECL_ARTIFICIAL (decl) = 1;
2596 if (!TYPE_NAME (type))
2597 TYPE_NAME (type) = decl;
2598 pushdecl (decl);
2600 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2603 /* Print an error message for invalid operands to arith operation
2604 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2605 RICHLOC is a rich location for the message, containing either
2606 three separate locations for each of the operator and operands
2608 lhs op rhs
2609 ~~~ ^~ ~~~
2611 (C FE), or one location ranging over all over them
2613 lhs op rhs
2614 ~~~~^~~~~~
2616 (C++ FE). */
2618 void
2619 binary_op_error (rich_location *richloc, enum tree_code code,
2620 tree type0, tree type1)
2622 const char *opname;
2624 switch (code)
2626 case PLUS_EXPR:
2627 opname = "+"; break;
2628 case MINUS_EXPR:
2629 opname = "-"; break;
2630 case MULT_EXPR:
2631 opname = "*"; break;
2632 case MAX_EXPR:
2633 opname = "max"; break;
2634 case MIN_EXPR:
2635 opname = "min"; break;
2636 case EQ_EXPR:
2637 opname = "=="; break;
2638 case NE_EXPR:
2639 opname = "!="; break;
2640 case LE_EXPR:
2641 opname = "<="; break;
2642 case GE_EXPR:
2643 opname = ">="; break;
2644 case LT_EXPR:
2645 opname = "<"; break;
2646 case GT_EXPR:
2647 opname = ">"; break;
2648 case LSHIFT_EXPR:
2649 opname = "<<"; break;
2650 case RSHIFT_EXPR:
2651 opname = ">>"; break;
2652 case TRUNC_MOD_EXPR:
2653 case FLOOR_MOD_EXPR:
2654 opname = "%"; break;
2655 case TRUNC_DIV_EXPR:
2656 case FLOOR_DIV_EXPR:
2657 opname = "/"; break;
2658 case BIT_AND_EXPR:
2659 opname = "&"; break;
2660 case BIT_IOR_EXPR:
2661 opname = "|"; break;
2662 case TRUTH_ANDIF_EXPR:
2663 opname = "&&"; break;
2664 case TRUTH_ORIF_EXPR:
2665 opname = "||"; break;
2666 case BIT_XOR_EXPR:
2667 opname = "^"; break;
2668 default:
2669 gcc_unreachable ();
2671 error_at_rich_loc (richloc,
2672 "invalid operands to binary %s (have %qT and %qT)",
2673 opname, type0, type1);
2676 /* Given an expression as a tree, return its original type. Do this
2677 by stripping any conversion that preserves the sign and precision. */
2678 static tree
2679 expr_original_type (tree expr)
2681 STRIP_SIGN_NOPS (expr);
2682 return TREE_TYPE (expr);
2685 /* Subroutine of build_binary_op, used for comparison operations.
2686 See if the operands have both been converted from subword integer types
2687 and, if so, perhaps change them both back to their original type.
2688 This function is also responsible for converting the two operands
2689 to the proper common type for comparison.
2691 The arguments of this function are all pointers to local variables
2692 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2693 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2695 LOC is the location of the comparison.
2697 If this function returns nonzero, it means that the comparison has
2698 a constant value. What this function returns is an expression for
2699 that value. */
2701 tree
2702 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2703 tree *restype_ptr, enum tree_code *rescode_ptr)
2705 tree type;
2706 tree op0 = *op0_ptr;
2707 tree op1 = *op1_ptr;
2708 int unsignedp0, unsignedp1;
2709 int real1, real2;
2710 tree primop0, primop1;
2711 enum tree_code code = *rescode_ptr;
2713 /* Throw away any conversions to wider types
2714 already present in the operands. */
2716 primop0 = c_common_get_narrower (op0, &unsignedp0);
2717 primop1 = c_common_get_narrower (op1, &unsignedp1);
2719 /* If primopN is first sign-extended from primopN's precision to opN's
2720 precision, then zero-extended from opN's precision to
2721 *restype_ptr precision, shortenings might be invalid. */
2722 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2723 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2724 && !unsignedp0
2725 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2726 primop0 = op0;
2727 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2728 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2729 && !unsignedp1
2730 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2731 primop1 = op1;
2733 /* Handle the case that OP0 does not *contain* a conversion
2734 but it *requires* conversion to FINAL_TYPE. */
2736 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2737 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2738 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2739 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2741 /* If one of the operands must be floated, we cannot optimize. */
2742 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2743 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2745 /* If first arg is constant, swap the args (changing operation
2746 so value is preserved), for canonicalization. Don't do this if
2747 the second arg is 0. */
2749 if (TREE_CONSTANT (primop0)
2750 && !integer_zerop (primop1) && !real_zerop (primop1)
2751 && !fixed_zerop (primop1))
2753 std::swap (primop0, primop1);
2754 std::swap (op0, op1);
2755 *op0_ptr = op0;
2756 *op1_ptr = op1;
2757 std::swap (unsignedp0, unsignedp1);
2758 std::swap (real1, real2);
2760 switch (code)
2762 case LT_EXPR:
2763 code = GT_EXPR;
2764 break;
2765 case GT_EXPR:
2766 code = LT_EXPR;
2767 break;
2768 case LE_EXPR:
2769 code = GE_EXPR;
2770 break;
2771 case GE_EXPR:
2772 code = LE_EXPR;
2773 break;
2774 default:
2775 break;
2777 *rescode_ptr = code;
2780 /* If comparing an integer against a constant more bits wide,
2781 maybe we can deduce a value of 1 or 0 independent of the data.
2782 Or else truncate the constant now
2783 rather than extend the variable at run time.
2785 This is only interesting if the constant is the wider arg.
2786 Also, it is not safe if the constant is unsigned and the
2787 variable arg is signed, since in this case the variable
2788 would be sign-extended and then regarded as unsigned.
2789 Our technique fails in this case because the lowest/highest
2790 possible unsigned results don't follow naturally from the
2791 lowest/highest possible values of the variable operand.
2792 For just EQ_EXPR and NE_EXPR there is another technique that
2793 could be used: see if the constant can be faithfully represented
2794 in the other operand's type, by truncating it and reextending it
2795 and see if that preserves the constant's value. */
2797 if (!real1 && !real2
2798 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2799 && TREE_CODE (primop1) == INTEGER_CST
2800 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2802 int min_gt, max_gt, min_lt, max_lt;
2803 tree maxval, minval;
2804 /* 1 if comparison is nominally unsigned. */
2805 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2806 tree val;
2808 type = c_common_signed_or_unsigned_type (unsignedp0,
2809 TREE_TYPE (primop0));
2811 maxval = TYPE_MAX_VALUE (type);
2812 minval = TYPE_MIN_VALUE (type);
2814 if (unsignedp && !unsignedp0)
2815 *restype_ptr = c_common_signed_type (*restype_ptr);
2817 if (TREE_TYPE (primop1) != *restype_ptr)
2819 /* Convert primop1 to target type, but do not introduce
2820 additional overflow. We know primop1 is an int_cst. */
2821 primop1 = force_fit_type (*restype_ptr,
2822 wi::to_wide
2823 (primop1,
2824 TYPE_PRECISION (*restype_ptr)),
2825 0, TREE_OVERFLOW (primop1));
2827 if (type != *restype_ptr)
2829 minval = convert (*restype_ptr, minval);
2830 maxval = convert (*restype_ptr, maxval);
2833 min_gt = tree_int_cst_lt (primop1, minval);
2834 max_gt = tree_int_cst_lt (primop1, maxval);
2835 min_lt = tree_int_cst_lt (minval, primop1);
2836 max_lt = tree_int_cst_lt (maxval, primop1);
2838 val = 0;
2839 /* This used to be a switch, but Genix compiler can't handle that. */
2840 if (code == NE_EXPR)
2842 if (max_lt || min_gt)
2843 val = truthvalue_true_node;
2845 else if (code == EQ_EXPR)
2847 if (max_lt || min_gt)
2848 val = truthvalue_false_node;
2850 else if (code == LT_EXPR)
2852 if (max_lt)
2853 val = truthvalue_true_node;
2854 if (!min_lt)
2855 val = truthvalue_false_node;
2857 else if (code == GT_EXPR)
2859 if (min_gt)
2860 val = truthvalue_true_node;
2861 if (!max_gt)
2862 val = truthvalue_false_node;
2864 else if (code == LE_EXPR)
2866 if (!max_gt)
2867 val = truthvalue_true_node;
2868 if (min_gt)
2869 val = truthvalue_false_node;
2871 else if (code == GE_EXPR)
2873 if (!min_lt)
2874 val = truthvalue_true_node;
2875 if (max_lt)
2876 val = truthvalue_false_node;
2879 /* If primop0 was sign-extended and unsigned comparison specd,
2880 we did a signed comparison above using the signed type bounds.
2881 But the comparison we output must be unsigned.
2883 Also, for inequalities, VAL is no good; but if the signed
2884 comparison had *any* fixed result, it follows that the
2885 unsigned comparison just tests the sign in reverse
2886 (positive values are LE, negative ones GE).
2887 So we can generate an unsigned comparison
2888 against an extreme value of the signed type. */
2890 if (unsignedp && !unsignedp0)
2892 if (val != 0)
2893 switch (code)
2895 case LT_EXPR:
2896 case GE_EXPR:
2897 primop1 = TYPE_MIN_VALUE (type);
2898 val = 0;
2899 break;
2901 case LE_EXPR:
2902 case GT_EXPR:
2903 primop1 = TYPE_MAX_VALUE (type);
2904 val = 0;
2905 break;
2907 default:
2908 break;
2910 type = c_common_unsigned_type (type);
2913 if (TREE_CODE (primop0) != INTEGER_CST
2914 /* Don't warn if it's from a (non-system) macro. */
2915 && !(from_macro_expansion_at
2916 (expansion_point_location_if_in_system_header
2917 (EXPR_LOCATION (primop0)))))
2919 if (val == truthvalue_false_node)
2920 warning_at (loc, OPT_Wtype_limits,
2921 "comparison is always false due to limited range of data type");
2922 if (val == truthvalue_true_node)
2923 warning_at (loc, OPT_Wtype_limits,
2924 "comparison is always true due to limited range of data type");
2927 if (val != 0)
2929 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2930 if (TREE_SIDE_EFFECTS (primop0))
2931 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2932 return val;
2935 /* Value is not predetermined, but do the comparison
2936 in the type of the operand that is not constant.
2937 TYPE is already properly set. */
2940 /* If either arg is decimal float and the other is float, find the
2941 proper common type to use for comparison. */
2942 else if (real1 && real2
2943 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2944 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2945 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2947 /* If either arg is decimal float and the other is float, fail. */
2948 else if (real1 && real2
2949 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2950 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2951 return 0;
2953 else if (real1 && real2
2954 && (TYPE_PRECISION (TREE_TYPE (primop0))
2955 == TYPE_PRECISION (TREE_TYPE (primop1))))
2956 type = TREE_TYPE (primop0);
2958 /* If args' natural types are both narrower than nominal type
2959 and both extend in the same manner, compare them
2960 in the type of the wider arg.
2961 Otherwise must actually extend both to the nominal
2962 common type lest different ways of extending
2963 alter the result.
2964 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2966 else if (unsignedp0 == unsignedp1 && real1 == real2
2967 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2968 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2970 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2971 type = c_common_signed_or_unsigned_type (unsignedp0
2972 || TYPE_UNSIGNED (*restype_ptr),
2973 type);
2974 /* Make sure shorter operand is extended the right way
2975 to match the longer operand. */
2976 primop0
2977 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2978 TREE_TYPE (primop0)),
2979 primop0);
2980 primop1
2981 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2982 TREE_TYPE (primop1)),
2983 primop1);
2985 else
2987 /* Here we must do the comparison on the nominal type
2988 using the args exactly as we received them. */
2989 type = *restype_ptr;
2990 primop0 = op0;
2991 primop1 = op1;
2993 if (!real1 && !real2 && integer_zerop (primop1)
2994 && TYPE_UNSIGNED (*restype_ptr))
2996 tree value = 0;
2997 /* All unsigned values are >= 0, so we warn. However,
2998 if OP0 is a constant that is >= 0, the signedness of
2999 the comparison isn't an issue, so suppress the
3000 warning. */
3001 bool warn =
3002 warn_type_limits && !in_system_header_at (loc)
3003 && !(TREE_CODE (primop0) == INTEGER_CST
3004 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3005 primop0)))
3006 /* Do not warn for enumeration types. */
3007 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3009 switch (code)
3011 case GE_EXPR:
3012 if (warn)
3013 warning_at (loc, OPT_Wtype_limits,
3014 "comparison of unsigned expression >= 0 is always true");
3015 value = truthvalue_true_node;
3016 break;
3018 case LT_EXPR:
3019 if (warn)
3020 warning_at (loc, OPT_Wtype_limits,
3021 "comparison of unsigned expression < 0 is always false");
3022 value = truthvalue_false_node;
3023 break;
3025 default:
3026 break;
3029 if (value != 0)
3031 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3032 if (TREE_SIDE_EFFECTS (primop0))
3033 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3034 primop0, value);
3035 return value;
3040 *op0_ptr = convert (type, primop0);
3041 *op1_ptr = convert (type, primop1);
3043 *restype_ptr = truthvalue_type_node;
3045 return 0;
3048 /* Return a tree for the sum or difference (RESULTCODE says which)
3049 of pointer PTROP and integer INTOP. */
3051 tree
3052 pointer_int_sum (location_t loc, enum tree_code resultcode,
3053 tree ptrop, tree intop, bool complain)
3055 tree size_exp, ret;
3057 /* The result is a pointer of the same type that is being added. */
3058 tree result_type = TREE_TYPE (ptrop);
3060 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3062 if (complain && warn_pointer_arith)
3063 pedwarn (loc, OPT_Wpointer_arith,
3064 "pointer of type %<void *%> used in arithmetic");
3065 else if (!complain)
3066 return error_mark_node;
3067 size_exp = integer_one_node;
3069 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3071 if (complain && warn_pointer_arith)
3072 pedwarn (loc, OPT_Wpointer_arith,
3073 "pointer to a function used in arithmetic");
3074 else if (!complain)
3075 return error_mark_node;
3076 size_exp = integer_one_node;
3078 else
3079 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3081 /* We are manipulating pointer values, so we don't need to warn
3082 about relying on undefined signed overflow. We disable the
3083 warning here because we use integer types so fold won't know that
3084 they are really pointers. */
3085 fold_defer_overflow_warnings ();
3087 /* If what we are about to multiply by the size of the elements
3088 contains a constant term, apply distributive law
3089 and multiply that constant term separately.
3090 This helps produce common subexpressions. */
3091 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3092 && !TREE_CONSTANT (intop)
3093 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3094 && TREE_CONSTANT (size_exp)
3095 /* If the constant comes from pointer subtraction,
3096 skip this optimization--it would cause an error. */
3097 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3098 /* If the constant is unsigned, and smaller than the pointer size,
3099 then we must skip this optimization. This is because it could cause
3100 an overflow error if the constant is negative but INTOP is not. */
3101 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3102 || (TYPE_PRECISION (TREE_TYPE (intop))
3103 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3105 enum tree_code subcode = resultcode;
3106 tree int_type = TREE_TYPE (intop);
3107 if (TREE_CODE (intop) == MINUS_EXPR)
3108 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3109 /* Convert both subexpression types to the type of intop,
3110 because weird cases involving pointer arithmetic
3111 can result in a sum or difference with different type args. */
3112 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3113 subcode, ptrop,
3114 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3115 intop = convert (int_type, TREE_OPERAND (intop, 0));
3118 /* Convert the integer argument to a type the same size as sizetype
3119 so the multiply won't overflow spuriously. */
3120 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3121 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3122 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3123 TYPE_UNSIGNED (sizetype)), intop);
3125 /* Replace the integer argument with a suitable product by the object size.
3126 Do this multiplication as signed, then convert to the appropriate type
3127 for the pointer operation and disregard an overflow that occurred only
3128 because of the sign-extension change in the latter conversion. */
3130 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3131 convert (TREE_TYPE (intop), size_exp));
3132 intop = convert (sizetype, t);
3133 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3134 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
3137 /* Create the sum or difference. */
3138 if (resultcode == MINUS_EXPR)
3139 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3141 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3143 fold_undefer_and_ignore_overflow_warnings ();
3145 return ret;
3148 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3149 and if NON_CONST is known not to be permitted in an evaluated part
3150 of a constant expression. */
3152 tree
3153 c_wrap_maybe_const (tree expr, bool non_const)
3155 bool nowarning = TREE_NO_WARNING (expr);
3156 location_t loc = EXPR_LOCATION (expr);
3158 /* This should never be called for C++. */
3159 if (c_dialect_cxx ())
3160 gcc_unreachable ();
3162 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3163 STRIP_TYPE_NOPS (expr);
3164 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3165 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3166 if (nowarning)
3167 TREE_NO_WARNING (expr) = 1;
3168 protected_set_expr_location (expr, loc);
3170 return expr;
3173 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3174 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3175 around the SAVE_EXPR if needed so that c_fully_fold does not need
3176 to look inside SAVE_EXPRs. */
3178 tree
3179 c_save_expr (tree expr)
3181 bool maybe_const = true;
3182 if (c_dialect_cxx ())
3183 return save_expr (expr);
3184 expr = c_fully_fold (expr, false, &maybe_const);
3185 expr = save_expr (expr);
3186 if (!maybe_const)
3187 expr = c_wrap_maybe_const (expr, true);
3188 return expr;
3191 /* Return whether EXPR is a declaration whose address can never be
3192 NULL. */
3194 bool
3195 decl_with_nonnull_addr_p (const_tree expr)
3197 return (DECL_P (expr)
3198 && (TREE_CODE (expr) == PARM_DECL
3199 || TREE_CODE (expr) == LABEL_DECL
3200 || !DECL_WEAK (expr)));
3203 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3204 or for an `if' or `while' statement or ?..: exp. It should already
3205 have been validated to be of suitable type; otherwise, a bad
3206 diagnostic may result.
3208 The EXPR is located at LOCATION.
3210 This preparation consists of taking the ordinary
3211 representation of an expression expr and producing a valid tree
3212 boolean expression describing whether expr is nonzero. We could
3213 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3214 but we optimize comparisons, &&, ||, and !.
3216 The resulting type should always be `truthvalue_type_node'. */
3218 tree
3219 c_common_truthvalue_conversion (location_t location, tree expr)
3221 switch (TREE_CODE (expr))
3223 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3224 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3225 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3226 case ORDERED_EXPR: case UNORDERED_EXPR:
3227 if (TREE_TYPE (expr) == truthvalue_type_node)
3228 return expr;
3229 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3230 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3231 goto ret;
3233 case TRUTH_ANDIF_EXPR:
3234 case TRUTH_ORIF_EXPR:
3235 case TRUTH_AND_EXPR:
3236 case TRUTH_OR_EXPR:
3237 case TRUTH_XOR_EXPR:
3238 if (TREE_TYPE (expr) == truthvalue_type_node)
3239 return expr;
3240 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3241 c_common_truthvalue_conversion (location,
3242 TREE_OPERAND (expr, 0)),
3243 c_common_truthvalue_conversion (location,
3244 TREE_OPERAND (expr, 1)));
3245 goto ret;
3247 case TRUTH_NOT_EXPR:
3248 if (TREE_TYPE (expr) == truthvalue_type_node)
3249 return expr;
3250 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3251 c_common_truthvalue_conversion (location,
3252 TREE_OPERAND (expr, 0)));
3253 goto ret;
3255 case ERROR_MARK:
3256 return expr;
3258 case INTEGER_CST:
3259 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3260 && !integer_zerop (expr)
3261 && !integer_onep (expr))
3262 warning_at (location, OPT_Wint_in_bool_context,
3263 "enum constant in boolean context");
3264 return integer_zerop (expr) ? truthvalue_false_node
3265 : truthvalue_true_node;
3267 case REAL_CST:
3268 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3269 ? truthvalue_true_node
3270 : truthvalue_false_node;
3272 case FIXED_CST:
3273 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3274 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3275 ? truthvalue_true_node
3276 : truthvalue_false_node;
3278 case FUNCTION_DECL:
3279 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3280 /* Fall through. */
3282 case ADDR_EXPR:
3284 tree inner = TREE_OPERAND (expr, 0);
3285 if (decl_with_nonnull_addr_p (inner))
3287 /* Common Ada/Pascal programmer's mistake. */
3288 warning_at (location,
3289 OPT_Waddress,
3290 "the address of %qD will always evaluate as %<true%>",
3291 inner);
3292 return truthvalue_true_node;
3294 break;
3297 case COMPLEX_EXPR:
3298 expr = build_binary_op (EXPR_LOCATION (expr),
3299 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3300 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3301 c_common_truthvalue_conversion (location,
3302 TREE_OPERAND (expr, 0)),
3303 c_common_truthvalue_conversion (location,
3304 TREE_OPERAND (expr, 1)),
3306 goto ret;
3308 case NEGATE_EXPR:
3309 case ABS_EXPR:
3310 case FLOAT_EXPR:
3311 case EXCESS_PRECISION_EXPR:
3312 /* These don't change whether an object is nonzero or zero. */
3313 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3315 case LROTATE_EXPR:
3316 case RROTATE_EXPR:
3317 /* These don't change whether an object is zero or nonzero, but
3318 we can't ignore them if their second arg has side-effects. */
3319 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3321 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3322 TREE_OPERAND (expr, 1),
3323 c_common_truthvalue_conversion
3324 (location, TREE_OPERAND (expr, 0)));
3325 goto ret;
3327 else
3328 return c_common_truthvalue_conversion (location,
3329 TREE_OPERAND (expr, 0));
3331 case MULT_EXPR:
3332 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3333 "%<*%> in boolean context, suggest %<&&%> instead");
3334 break;
3336 case LSHIFT_EXPR:
3337 /* We will only warn on signed shifts here, because the majority of
3338 false positive warnings happen in code where unsigned arithmetic
3339 was used in anticipation of a possible overflow.
3340 Furthermore, if we see an unsigned type here we know that the
3341 result of the shift is not subject to integer promotion rules. */
3342 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3343 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3344 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3345 "%<<<%> in boolean context, did you mean %<<%> ?");
3346 break;
3348 case COND_EXPR:
3349 if (warn_int_in_bool_context
3350 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3352 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3353 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3354 if (TREE_CODE (val1) == INTEGER_CST
3355 && TREE_CODE (val2) == INTEGER_CST
3356 && !integer_zerop (val1)
3357 && !integer_zerop (val2)
3358 && (!integer_onep (val1)
3359 || !integer_onep (val2)))
3360 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3361 "?: using integer constants in boolean context, "
3362 "the expression will always evaluate to %<true%>");
3363 else if ((TREE_CODE (val1) == INTEGER_CST
3364 && !integer_zerop (val1)
3365 && !integer_onep (val1))
3366 || (TREE_CODE (val2) == INTEGER_CST
3367 && !integer_zerop (val2)
3368 && !integer_onep (val2)))
3369 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3370 "?: using integer constants in boolean context");
3372 /* Distribute the conversion into the arms of a COND_EXPR. */
3373 if (c_dialect_cxx ())
3374 /* Avoid premature folding. */
3375 break;
3376 else
3378 int w = warn_int_in_bool_context;
3379 warn_int_in_bool_context = 0;
3380 /* Folding will happen later for C. */
3381 expr = build3 (COND_EXPR, truthvalue_type_node,
3382 TREE_OPERAND (expr, 0),
3383 c_common_truthvalue_conversion (location,
3384 TREE_OPERAND (expr, 1)),
3385 c_common_truthvalue_conversion (location,
3386 TREE_OPERAND (expr, 2)));
3387 warn_int_in_bool_context = w;
3388 goto ret;
3391 CASE_CONVERT:
3393 tree totype = TREE_TYPE (expr);
3394 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3396 if (POINTER_TYPE_P (totype)
3397 && !c_inhibit_evaluation_warnings
3398 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3400 tree inner = expr;
3401 STRIP_NOPS (inner);
3403 if (DECL_P (inner))
3404 warning_at (location,
3405 OPT_Waddress,
3406 "the compiler can assume that the address of "
3407 "%qD will always evaluate to %<true%>",
3408 inner);
3411 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3412 since that affects how `default_conversion' will behave. */
3413 if (TREE_CODE (totype) == REFERENCE_TYPE
3414 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3415 break;
3416 /* Don't strip a conversion from C++0x scoped enum, since they
3417 don't implicitly convert to other types. */
3418 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3419 && ENUM_IS_SCOPED (fromtype))
3420 break;
3421 /* If this isn't narrowing the argument, we can ignore it. */
3422 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3423 return c_common_truthvalue_conversion (location,
3424 TREE_OPERAND (expr, 0));
3426 break;
3428 case MODIFY_EXPR:
3429 if (!TREE_NO_WARNING (expr)
3430 && warn_parentheses)
3432 warning_at (location, OPT_Wparentheses,
3433 "suggest parentheses around assignment used as "
3434 "truth value");
3435 TREE_NO_WARNING (expr) = 1;
3437 break;
3439 default:
3440 break;
3443 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3445 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
3446 expr = (build_binary_op
3447 (EXPR_LOCATION (expr),
3448 (TREE_SIDE_EFFECTS (expr)
3449 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3450 c_common_truthvalue_conversion
3451 (location,
3452 build_unary_op (location, REALPART_EXPR, t, false)),
3453 c_common_truthvalue_conversion
3454 (location,
3455 build_unary_op (location, IMAGPART_EXPR, t, false)),
3456 0));
3457 goto ret;
3460 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3462 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3463 FCONST0 (TYPE_MODE
3464 (TREE_TYPE (expr))));
3465 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
3467 else
3468 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
3470 ret:
3471 protected_set_expr_location (expr, location);
3472 return expr;
3475 static void def_builtin_1 (enum built_in_function fncode,
3476 const char *name,
3477 enum built_in_class fnclass,
3478 tree fntype, tree libtype,
3479 bool both_p, bool fallback_p, bool nonansi_p,
3480 tree fnattrs, bool implicit_p);
3483 /* Apply the TYPE_QUALS to the new DECL. */
3485 void
3486 c_apply_type_quals_to_decl (int type_quals, tree decl)
3488 tree type = TREE_TYPE (decl);
3490 if (type == error_mark_node)
3491 return;
3493 if ((type_quals & TYPE_QUAL_CONST)
3494 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3495 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3496 constructor can produce constant init, so rely on cp_finish_decl to
3497 clear TREE_READONLY if the variable has non-constant init. */
3498 TREE_READONLY (decl) = 1;
3499 if (type_quals & TYPE_QUAL_VOLATILE)
3501 TREE_SIDE_EFFECTS (decl) = 1;
3502 TREE_THIS_VOLATILE (decl) = 1;
3504 if (type_quals & TYPE_QUAL_RESTRICT)
3506 while (type && TREE_CODE (type) == ARRAY_TYPE)
3507 /* Allow 'restrict' on arrays of pointers.
3508 FIXME currently we just ignore it. */
3509 type = TREE_TYPE (type);
3510 if (!type
3511 || !POINTER_TYPE_P (type)
3512 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3513 error ("invalid use of %<restrict%>");
3517 struct c_type_hasher : ggc_ptr_hash<tree_node>
3519 static hashval_t hash (tree);
3520 static bool equal (tree, tree);
3523 /* Hash function for the problem of multiple type definitions in
3524 different files. This must hash all types that will compare
3525 equal via comptypes to the same value. In practice it hashes
3526 on some of the simple stuff and leaves the details to comptypes. */
3528 hashval_t
3529 c_type_hasher::hash (tree t)
3531 int n_elements;
3532 int shift, size;
3533 tree t2;
3534 switch (TREE_CODE (t))
3536 /* For pointers, hash on pointee type plus some swizzling. */
3537 case POINTER_TYPE:
3538 return hash (TREE_TYPE (t)) ^ 0x3003003;
3539 /* Hash on number of elements and total size. */
3540 case ENUMERAL_TYPE:
3541 shift = 3;
3542 t2 = TYPE_VALUES (t);
3543 break;
3544 case RECORD_TYPE:
3545 shift = 0;
3546 t2 = TYPE_FIELDS (t);
3547 break;
3548 case QUAL_UNION_TYPE:
3549 shift = 1;
3550 t2 = TYPE_FIELDS (t);
3551 break;
3552 case UNION_TYPE:
3553 shift = 2;
3554 t2 = TYPE_FIELDS (t);
3555 break;
3556 default:
3557 gcc_unreachable ();
3559 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
3560 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
3561 n_elements = list_length (t2);
3562 /* We might have a VLA here. */
3563 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
3564 size = 0;
3565 else
3566 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
3567 return ((size << 24) | (n_elements << shift));
3570 bool
3571 c_type_hasher::equal (tree t1, tree t2)
3573 return lang_hooks.types_compatible_p (t1, t2);
3576 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
3578 /* Return the typed-based alias set for T, which may be an expression
3579 or a type. Return -1 if we don't do anything special. */
3581 alias_set_type
3582 c_common_get_alias_set (tree t)
3584 /* For VLAs, use the alias set of the element type rather than the
3585 default of alias set 0 for types compared structurally. */
3586 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3588 if (TREE_CODE (t) == ARRAY_TYPE)
3589 return get_alias_set (TREE_TYPE (t));
3590 return -1;
3593 /* That's all the expressions we handle specially. */
3594 if (!TYPE_P (t))
3595 return -1;
3597 /* The C standard guarantees that any object may be accessed via an
3598 lvalue that has character type. */
3599 if (t == char_type_node
3600 || t == signed_char_type_node
3601 || t == unsigned_char_type_node)
3602 return 0;
3604 /* The C standard specifically allows aliasing between signed and
3605 unsigned variants of the same type. We treat the signed
3606 variant as canonical. */
3607 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3609 tree t1 = c_common_signed_type (t);
3611 /* t1 == t can happen for boolean nodes which are always unsigned. */
3612 if (t1 != t)
3613 return get_alias_set (t1);
3616 /* Handle the case of multiple type nodes referring to "the same" type,
3617 which occurs with IMA. These share an alias set. FIXME: Currently only
3618 C90 is handled. (In C99 type compatibility is not transitive, which
3619 complicates things mightily. The alias set splay trees can theoretically
3620 represent this, but insertion is tricky when you consider all the
3621 different orders things might arrive in.) */
3623 if (c_language != clk_c || flag_isoc99)
3624 return -1;
3626 /* Save time if there's only one input file. */
3627 if (num_in_fnames == 1)
3628 return -1;
3630 /* Pointers need special handling if they point to any type that
3631 needs special handling (below). */
3632 if (TREE_CODE (t) == POINTER_TYPE)
3634 tree t2;
3635 /* Find bottom type under any nested POINTERs. */
3636 for (t2 = TREE_TYPE (t);
3637 TREE_CODE (t2) == POINTER_TYPE;
3638 t2 = TREE_TYPE (t2))
3640 if (!RECORD_OR_UNION_TYPE_P (t2)
3641 && TREE_CODE (t2) != ENUMERAL_TYPE)
3642 return -1;
3643 if (TYPE_SIZE (t2) == 0)
3644 return -1;
3646 /* These are the only cases that need special handling. */
3647 if (!RECORD_OR_UNION_TYPE_P (t)
3648 && TREE_CODE (t) != ENUMERAL_TYPE
3649 && TREE_CODE (t) != POINTER_TYPE)
3650 return -1;
3651 /* Undefined? */
3652 if (TYPE_SIZE (t) == 0)
3653 return -1;
3655 /* Look up t in hash table. Only one of the compatible types within each
3656 alias set is recorded in the table. */
3657 if (!type_hash_table)
3658 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
3659 tree *slot = type_hash_table->find_slot (t, INSERT);
3660 if (*slot != NULL)
3662 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
3663 return TYPE_ALIAS_SET ((tree)*slot);
3665 else
3666 /* Our caller will assign and record (in t) a new alias set; all we need
3667 to do is remember t in the hash table. */
3668 *slot = t;
3670 return -1;
3673 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3674 the IS_SIZEOF parameter indicates which operator is being applied.
3675 The COMPLAIN flag controls whether we should diagnose possibly
3676 ill-formed constructs or not. LOC is the location of the SIZEOF or
3677 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3678 a type in any context should be returned, rather than the normal
3679 alignment for that type. */
3681 tree
3682 c_sizeof_or_alignof_type (location_t loc,
3683 tree type, bool is_sizeof, bool min_alignof,
3684 int complain)
3686 const char *op_name;
3687 tree value = NULL;
3688 enum tree_code type_code = TREE_CODE (type);
3690 op_name = is_sizeof ? "sizeof" : "__alignof__";
3692 if (type_code == FUNCTION_TYPE)
3694 if (is_sizeof)
3696 if (complain && warn_pointer_arith)
3697 pedwarn (loc, OPT_Wpointer_arith,
3698 "invalid application of %<sizeof%> to a function type");
3699 else if (!complain)
3700 return error_mark_node;
3701 value = size_one_node;
3703 else
3705 if (complain)
3707 if (c_dialect_cxx ())
3708 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3709 "%<alignof%> applied to a function type");
3710 else
3711 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3712 "%<_Alignof%> applied to a function type");
3714 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3717 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3719 if (type_code == VOID_TYPE
3720 && complain && warn_pointer_arith)
3721 pedwarn (loc, OPT_Wpointer_arith,
3722 "invalid application of %qs to a void type", op_name);
3723 else if (!complain)
3724 return error_mark_node;
3725 value = size_one_node;
3727 else if (!COMPLETE_TYPE_P (type)
3728 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3730 if (complain)
3731 error_at (loc, "invalid application of %qs to incomplete type %qT",
3732 op_name, type);
3733 return error_mark_node;
3735 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3736 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3738 if (complain)
3739 error_at (loc, "invalid application of %qs to array type %qT of "
3740 "incomplete element type", op_name, type);
3741 return error_mark_node;
3743 else
3745 if (is_sizeof)
3746 /* Convert in case a char is more than one unit. */
3747 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3748 size_int (TYPE_PRECISION (char_type_node)
3749 / BITS_PER_UNIT));
3750 else if (min_alignof)
3751 value = size_int (min_align_of_type (type));
3752 else
3753 value = size_int (TYPE_ALIGN_UNIT (type));
3756 /* VALUE will have the middle-end integer type sizetype.
3757 However, we should really return a value of type `size_t',
3758 which is just a typedef for an ordinary integer type. */
3759 value = fold_convert_loc (loc, size_type_node, value);
3761 return value;
3764 /* Implement the __alignof keyword: Return the minimum required
3765 alignment of EXPR, measured in bytes. For VAR_DECLs,
3766 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3767 from an "aligned" __attribute__ specification). LOC is the
3768 location of the ALIGNOF operator. */
3770 tree
3771 c_alignof_expr (location_t loc, tree expr)
3773 tree t;
3775 if (VAR_OR_FUNCTION_DECL_P (expr))
3776 t = size_int (DECL_ALIGN_UNIT (expr));
3778 else if (TREE_CODE (expr) == COMPONENT_REF
3779 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3781 error_at (loc, "%<__alignof%> applied to a bit-field");
3782 t = size_one_node;
3784 else if (TREE_CODE (expr) == COMPONENT_REF
3785 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3786 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3788 else if (INDIRECT_REF_P (expr))
3790 tree t = TREE_OPERAND (expr, 0);
3791 tree best = t;
3792 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3794 while (CONVERT_EXPR_P (t)
3795 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3797 int thisalign;
3799 t = TREE_OPERAND (t, 0);
3800 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3801 if (thisalign > bestalign)
3802 best = t, bestalign = thisalign;
3804 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3806 else
3807 return c_alignof (loc, TREE_TYPE (expr));
3809 return fold_convert_loc (loc, size_type_node, t);
3812 /* Handle C and C++ default attributes. */
3814 enum built_in_attribute
3816 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3817 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3818 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3819 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3820 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3821 #include "builtin-attrs.def"
3822 #undef DEF_ATTR_NULL_TREE
3823 #undef DEF_ATTR_INT
3824 #undef DEF_ATTR_STRING
3825 #undef DEF_ATTR_IDENT
3826 #undef DEF_ATTR_TREE_LIST
3827 ATTR_LAST
3830 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3832 static void c_init_attributes (void);
3834 enum c_builtin_type
3836 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3837 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3838 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3839 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3840 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3841 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3842 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3843 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3844 ARG6) NAME,
3845 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3846 ARG6, ARG7) NAME,
3847 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3848 ARG6, ARG7, ARG8) NAME,
3849 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3850 ARG6, ARG7, ARG8, ARG9) NAME,
3851 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3852 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3853 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3854 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3855 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3856 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3857 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3858 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3859 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3860 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3861 NAME,
3862 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3863 ARG6) NAME,
3864 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3865 ARG6, ARG7) NAME,
3866 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3867 #include "builtin-types.def"
3868 #undef DEF_PRIMITIVE_TYPE
3869 #undef DEF_FUNCTION_TYPE_0
3870 #undef DEF_FUNCTION_TYPE_1
3871 #undef DEF_FUNCTION_TYPE_2
3872 #undef DEF_FUNCTION_TYPE_3
3873 #undef DEF_FUNCTION_TYPE_4
3874 #undef DEF_FUNCTION_TYPE_5
3875 #undef DEF_FUNCTION_TYPE_6
3876 #undef DEF_FUNCTION_TYPE_7
3877 #undef DEF_FUNCTION_TYPE_8
3878 #undef DEF_FUNCTION_TYPE_9
3879 #undef DEF_FUNCTION_TYPE_10
3880 #undef DEF_FUNCTION_TYPE_11
3881 #undef DEF_FUNCTION_TYPE_VAR_0
3882 #undef DEF_FUNCTION_TYPE_VAR_1
3883 #undef DEF_FUNCTION_TYPE_VAR_2
3884 #undef DEF_FUNCTION_TYPE_VAR_3
3885 #undef DEF_FUNCTION_TYPE_VAR_4
3886 #undef DEF_FUNCTION_TYPE_VAR_5
3887 #undef DEF_FUNCTION_TYPE_VAR_6
3888 #undef DEF_FUNCTION_TYPE_VAR_7
3889 #undef DEF_POINTER_TYPE
3890 BT_LAST
3893 typedef enum c_builtin_type builtin_type;
3895 /* A temporary array for c_common_nodes_and_builtins. Used in
3896 communication with def_fn_type. */
3897 static tree builtin_types[(int) BT_LAST + 1];
3899 /* A helper function for c_common_nodes_and_builtins. Build function type
3900 for DEF with return type RET and N arguments. If VAR is true, then the
3901 function should be variadic after those N arguments.
3903 Takes special care not to ICE if any of the types involved are
3904 error_mark_node, which indicates that said type is not in fact available
3905 (see builtin_type_for_size). In which case the function type as a whole
3906 should be error_mark_node. */
3908 static void
3909 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3911 tree t;
3912 tree *args = XALLOCAVEC (tree, n);
3913 va_list list;
3914 int i;
3916 va_start (list, n);
3917 for (i = 0; i < n; ++i)
3919 builtin_type a = (builtin_type) va_arg (list, int);
3920 t = builtin_types[a];
3921 if (t == error_mark_node)
3922 goto egress;
3923 args[i] = t;
3926 t = builtin_types[ret];
3927 if (t == error_mark_node)
3928 goto egress;
3929 if (var)
3930 t = build_varargs_function_type_array (t, n, args);
3931 else
3932 t = build_function_type_array (t, n, args);
3934 egress:
3935 builtin_types[def] = t;
3936 va_end (list);
3939 /* Build builtin functions common to both C and C++ language
3940 frontends. */
3942 static void
3943 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3945 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3946 builtin_types[ENUM] = VALUE;
3947 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3948 def_fn_type (ENUM, RETURN, 0, 0);
3949 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3950 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3951 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3952 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3953 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3954 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3955 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3956 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3957 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3958 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3959 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3960 ARG6) \
3961 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3962 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3963 ARG6, ARG7) \
3964 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3965 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3966 ARG6, ARG7, ARG8) \
3967 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3968 ARG7, ARG8);
3969 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3970 ARG6, ARG7, ARG8, ARG9) \
3971 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3972 ARG7, ARG8, ARG9);
3973 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3974 ARG6, ARG7, ARG8, ARG9, ARG10) \
3975 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3976 ARG7, ARG8, ARG9, ARG10);
3977 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3978 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3979 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3980 ARG7, ARG8, ARG9, ARG10, ARG11);
3981 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3982 def_fn_type (ENUM, RETURN, 1, 0);
3983 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3984 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3985 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3986 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3987 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3988 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3989 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3990 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3991 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3992 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3993 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3994 ARG6) \
3995 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3996 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3997 ARG6, ARG7) \
3998 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3999 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4000 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4002 #include "builtin-types.def"
4004 #undef DEF_PRIMITIVE_TYPE
4005 #undef DEF_FUNCTION_TYPE_0
4006 #undef DEF_FUNCTION_TYPE_1
4007 #undef DEF_FUNCTION_TYPE_2
4008 #undef DEF_FUNCTION_TYPE_3
4009 #undef DEF_FUNCTION_TYPE_4
4010 #undef DEF_FUNCTION_TYPE_5
4011 #undef DEF_FUNCTION_TYPE_6
4012 #undef DEF_FUNCTION_TYPE_7
4013 #undef DEF_FUNCTION_TYPE_8
4014 #undef DEF_FUNCTION_TYPE_9
4015 #undef DEF_FUNCTION_TYPE_10
4016 #undef DEF_FUNCTION_TYPE_11
4017 #undef DEF_FUNCTION_TYPE_VAR_0
4018 #undef DEF_FUNCTION_TYPE_VAR_1
4019 #undef DEF_FUNCTION_TYPE_VAR_2
4020 #undef DEF_FUNCTION_TYPE_VAR_3
4021 #undef DEF_FUNCTION_TYPE_VAR_4
4022 #undef DEF_FUNCTION_TYPE_VAR_5
4023 #undef DEF_FUNCTION_TYPE_VAR_6
4024 #undef DEF_FUNCTION_TYPE_VAR_7
4025 #undef DEF_POINTER_TYPE
4026 builtin_types[(int) BT_LAST] = NULL_TREE;
4028 c_init_attributes ();
4030 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4031 NONANSI_P, ATTRS, IMPLICIT, COND) \
4032 if (NAME && COND) \
4033 def_builtin_1 (ENUM, NAME, CLASS, \
4034 builtin_types[(int) TYPE], \
4035 builtin_types[(int) LIBTYPE], \
4036 BOTH_P, FALLBACK_P, NONANSI_P, \
4037 built_in_attributes[(int) ATTRS], IMPLICIT);
4038 #include "builtins.def"
4040 targetm.init_builtins ();
4042 build_common_builtin_nodes ();
4044 if (flag_cilkplus)
4045 cilk_init_builtins ();
4048 /* Like get_identifier, but avoid warnings about null arguments when
4049 the argument may be NULL for targets where GCC lacks stdint.h type
4050 information. */
4052 static inline tree
4053 c_get_ident (const char *id)
4055 return get_identifier (id);
4058 /* Build tree nodes and builtin functions common to both C and C++ language
4059 frontends. */
4061 void
4062 c_common_nodes_and_builtins (void)
4064 int char16_type_size;
4065 int char32_type_size;
4066 int wchar_type_size;
4067 tree array_domain_type;
4068 tree va_list_ref_type_node;
4069 tree va_list_arg_type_node;
4070 int i;
4072 build_common_tree_nodes (flag_signed_char);
4074 /* Define `int' and `char' first so that dbx will output them first. */
4075 record_builtin_type (RID_INT, NULL, integer_type_node);
4076 record_builtin_type (RID_CHAR, "char", char_type_node);
4078 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4079 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4080 but not C. Are the conditionals here needed? */
4081 if (c_dialect_cxx ())
4082 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4083 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4084 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4085 record_builtin_type (RID_MAX, "long unsigned int",
4086 long_unsigned_type_node);
4088 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4090 char name[25];
4092 sprintf (name, "__int%d", int_n_data[i].bitsize);
4093 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4094 int_n_trees[i].signed_type);
4095 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4096 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4099 if (c_dialect_cxx ())
4100 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4101 record_builtin_type (RID_MAX, "long long int",
4102 long_long_integer_type_node);
4103 record_builtin_type (RID_MAX, "long long unsigned int",
4104 long_long_unsigned_type_node);
4105 if (c_dialect_cxx ())
4106 record_builtin_type (RID_MAX, "long long unsigned",
4107 long_long_unsigned_type_node);
4108 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4109 record_builtin_type (RID_MAX, "short unsigned int",
4110 short_unsigned_type_node);
4111 if (c_dialect_cxx ())
4112 record_builtin_type (RID_MAX, "unsigned short",
4113 short_unsigned_type_node);
4115 /* Define both `signed char' and `unsigned char'. */
4116 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4117 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4119 /* These are types that c_common_type_for_size and
4120 c_common_type_for_mode use. */
4121 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4122 TYPE_DECL, NULL_TREE,
4123 intQI_type_node));
4124 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4125 TYPE_DECL, NULL_TREE,
4126 intHI_type_node));
4127 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4128 TYPE_DECL, NULL_TREE,
4129 intSI_type_node));
4130 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4131 TYPE_DECL, NULL_TREE,
4132 intDI_type_node));
4133 #if HOST_BITS_PER_WIDE_INT >= 64
4134 /* Note that this is different than the __int128 type that's part of
4135 the generic __intN support. */
4136 if (targetm.scalar_mode_supported_p (TImode))
4137 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4138 TYPE_DECL,
4139 get_identifier ("__int128_t"),
4140 intTI_type_node));
4141 #endif
4142 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4143 TYPE_DECL, NULL_TREE,
4144 unsigned_intQI_type_node));
4145 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4146 TYPE_DECL, NULL_TREE,
4147 unsigned_intHI_type_node));
4148 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4149 TYPE_DECL, NULL_TREE,
4150 unsigned_intSI_type_node));
4151 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4152 TYPE_DECL, NULL_TREE,
4153 unsigned_intDI_type_node));
4154 #if HOST_BITS_PER_WIDE_INT >= 64
4155 if (targetm.scalar_mode_supported_p (TImode))
4156 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4157 TYPE_DECL,
4158 get_identifier ("__uint128_t"),
4159 unsigned_intTI_type_node));
4160 #endif
4162 /* Create the widest literal types. */
4163 widest_integer_literal_type_node
4164 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4165 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4166 TYPE_DECL, NULL_TREE,
4167 widest_integer_literal_type_node));
4169 widest_unsigned_literal_type_node
4170 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4171 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4172 TYPE_DECL, NULL_TREE,
4173 widest_unsigned_literal_type_node));
4175 signed_size_type_node = c_common_signed_type (size_type_node);
4177 pid_type_node =
4178 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4180 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4181 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4182 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4184 if (!c_dialect_cxx ())
4185 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4186 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4187 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4188 FLOATN_NX_TYPE_NODE (i));
4190 /* Only supported decimal floating point extension if the target
4191 actually supports underlying modes. */
4192 if (targetm.scalar_mode_supported_p (SDmode)
4193 && targetm.scalar_mode_supported_p (DDmode)
4194 && targetm.scalar_mode_supported_p (TDmode))
4196 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4197 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4198 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4201 if (targetm.fixed_point_supported_p ())
4203 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4204 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4205 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4206 record_builtin_type (RID_MAX, "long long _Fract",
4207 long_long_fract_type_node);
4208 record_builtin_type (RID_MAX, "unsigned short _Fract",
4209 unsigned_short_fract_type_node);
4210 record_builtin_type (RID_MAX, "unsigned _Fract",
4211 unsigned_fract_type_node);
4212 record_builtin_type (RID_MAX, "unsigned long _Fract",
4213 unsigned_long_fract_type_node);
4214 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4215 unsigned_long_long_fract_type_node);
4216 record_builtin_type (RID_MAX, "_Sat short _Fract",
4217 sat_short_fract_type_node);
4218 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4219 record_builtin_type (RID_MAX, "_Sat long _Fract",
4220 sat_long_fract_type_node);
4221 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4222 sat_long_long_fract_type_node);
4223 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4224 sat_unsigned_short_fract_type_node);
4225 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4226 sat_unsigned_fract_type_node);
4227 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4228 sat_unsigned_long_fract_type_node);
4229 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4230 sat_unsigned_long_long_fract_type_node);
4231 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4232 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4233 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4234 record_builtin_type (RID_MAX, "long long _Accum",
4235 long_long_accum_type_node);
4236 record_builtin_type (RID_MAX, "unsigned short _Accum",
4237 unsigned_short_accum_type_node);
4238 record_builtin_type (RID_MAX, "unsigned _Accum",
4239 unsigned_accum_type_node);
4240 record_builtin_type (RID_MAX, "unsigned long _Accum",
4241 unsigned_long_accum_type_node);
4242 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4243 unsigned_long_long_accum_type_node);
4244 record_builtin_type (RID_MAX, "_Sat short _Accum",
4245 sat_short_accum_type_node);
4246 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4247 record_builtin_type (RID_MAX, "_Sat long _Accum",
4248 sat_long_accum_type_node);
4249 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4250 sat_long_long_accum_type_node);
4251 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4252 sat_unsigned_short_accum_type_node);
4253 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4254 sat_unsigned_accum_type_node);
4255 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4256 sat_unsigned_long_accum_type_node);
4257 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4258 sat_unsigned_long_long_accum_type_node);
4262 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4263 TYPE_DECL,
4264 get_identifier ("complex int"),
4265 complex_integer_type_node));
4266 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4267 TYPE_DECL,
4268 get_identifier ("complex float"),
4269 complex_float_type_node));
4270 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4271 TYPE_DECL,
4272 get_identifier ("complex double"),
4273 complex_double_type_node));
4274 lang_hooks.decls.pushdecl
4275 (build_decl (UNKNOWN_LOCATION,
4276 TYPE_DECL, get_identifier ("complex long double"),
4277 complex_long_double_type_node));
4279 if (!c_dialect_cxx ())
4280 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4281 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4283 char buf[30];
4284 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4285 floatn_nx_types[i].extended ? "x" : "");
4286 lang_hooks.decls.pushdecl
4287 (build_decl (UNKNOWN_LOCATION,
4288 TYPE_DECL,
4289 get_identifier (buf),
4290 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4293 if (c_dialect_cxx ())
4294 /* For C++, make fileptr_type_node a distinct void * type until
4295 FILE type is defined. */
4296 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4298 record_builtin_type (RID_VOID, NULL, void_type_node);
4300 /* Set the TYPE_NAME for any variants that were built before
4301 record_builtin_type gave names to the built-in types. */
4303 tree void_name = TYPE_NAME (void_type_node);
4304 TYPE_NAME (void_type_node) = NULL_TREE;
4305 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4306 = void_name;
4307 TYPE_NAME (void_type_node) = void_name;
4310 void_list_node = build_void_list_node ();
4312 /* Make a type to be the domain of a few array types
4313 whose domains don't really matter.
4314 200 is small enough that it always fits in size_t
4315 and large enough that it can hold most function names for the
4316 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4317 array_domain_type = build_index_type (size_int (200));
4319 /* Make a type for arrays of characters.
4320 With luck nothing will ever really depend on the length of this
4321 array type. */
4322 char_array_type_node
4323 = build_array_type (char_type_node, array_domain_type);
4325 string_type_node = build_pointer_type (char_type_node);
4326 const_string_type_node
4327 = build_pointer_type (build_qualified_type
4328 (char_type_node, TYPE_QUAL_CONST));
4330 /* This is special for C++ so functions can be overloaded. */
4331 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4332 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4333 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4334 underlying_wchar_type_node = wchar_type_node;
4335 if (c_dialect_cxx ())
4337 if (TYPE_UNSIGNED (wchar_type_node))
4338 wchar_type_node = make_unsigned_type (wchar_type_size);
4339 else
4340 wchar_type_node = make_signed_type (wchar_type_size);
4341 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4344 /* This is for wide string constants. */
4345 wchar_array_type_node
4346 = build_array_type (wchar_type_node, array_domain_type);
4348 /* Define 'char16_t'. */
4349 char16_type_node = get_identifier (CHAR16_TYPE);
4350 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4351 char16_type_size = TYPE_PRECISION (char16_type_node);
4352 if (c_dialect_cxx ())
4354 char16_type_node = make_unsigned_type (char16_type_size);
4356 if (cxx_dialect >= cxx11)
4357 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4360 /* This is for UTF-16 string constants. */
4361 char16_array_type_node
4362 = build_array_type (char16_type_node, array_domain_type);
4364 /* Define 'char32_t'. */
4365 char32_type_node = get_identifier (CHAR32_TYPE);
4366 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4367 char32_type_size = TYPE_PRECISION (char32_type_node);
4368 if (c_dialect_cxx ())
4370 char32_type_node = make_unsigned_type (char32_type_size);
4372 if (cxx_dialect >= cxx11)
4373 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4376 /* This is for UTF-32 string constants. */
4377 char32_array_type_node
4378 = build_array_type (char32_type_node, array_domain_type);
4380 wint_type_node =
4381 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4383 intmax_type_node =
4384 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4385 uintmax_type_node =
4386 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4388 if (SIG_ATOMIC_TYPE)
4389 sig_atomic_type_node =
4390 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4391 if (INT8_TYPE)
4392 int8_type_node =
4393 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4394 if (INT16_TYPE)
4395 int16_type_node =
4396 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4397 if (INT32_TYPE)
4398 int32_type_node =
4399 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4400 if (INT64_TYPE)
4401 int64_type_node =
4402 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4403 if (UINT8_TYPE)
4404 uint8_type_node =
4405 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4406 if (UINT16_TYPE)
4407 c_uint16_type_node = uint16_type_node =
4408 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4409 if (UINT32_TYPE)
4410 c_uint32_type_node = uint32_type_node =
4411 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4412 if (UINT64_TYPE)
4413 c_uint64_type_node = uint64_type_node =
4414 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4415 if (INT_LEAST8_TYPE)
4416 int_least8_type_node =
4417 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4418 if (INT_LEAST16_TYPE)
4419 int_least16_type_node =
4420 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4421 if (INT_LEAST32_TYPE)
4422 int_least32_type_node =
4423 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4424 if (INT_LEAST64_TYPE)
4425 int_least64_type_node =
4426 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4427 if (UINT_LEAST8_TYPE)
4428 uint_least8_type_node =
4429 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4430 if (UINT_LEAST16_TYPE)
4431 uint_least16_type_node =
4432 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4433 if (UINT_LEAST32_TYPE)
4434 uint_least32_type_node =
4435 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4436 if (UINT_LEAST64_TYPE)
4437 uint_least64_type_node =
4438 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4439 if (INT_FAST8_TYPE)
4440 int_fast8_type_node =
4441 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4442 if (INT_FAST16_TYPE)
4443 int_fast16_type_node =
4444 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4445 if (INT_FAST32_TYPE)
4446 int_fast32_type_node =
4447 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4448 if (INT_FAST64_TYPE)
4449 int_fast64_type_node =
4450 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4451 if (UINT_FAST8_TYPE)
4452 uint_fast8_type_node =
4453 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4454 if (UINT_FAST16_TYPE)
4455 uint_fast16_type_node =
4456 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4457 if (UINT_FAST32_TYPE)
4458 uint_fast32_type_node =
4459 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4460 if (UINT_FAST64_TYPE)
4461 uint_fast64_type_node =
4462 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4463 if (INTPTR_TYPE)
4464 intptr_type_node =
4465 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4466 if (UINTPTR_TYPE)
4467 uintptr_type_node =
4468 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4470 default_function_type
4471 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4472 ptrdiff_type_node
4473 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
4474 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4476 lang_hooks.decls.pushdecl
4477 (build_decl (UNKNOWN_LOCATION,
4478 TYPE_DECL, get_identifier ("__builtin_va_list"),
4479 va_list_type_node));
4480 if (targetm.enum_va_list_p)
4482 int l;
4483 const char *pname;
4484 tree ptype;
4486 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4488 lang_hooks.decls.pushdecl
4489 (build_decl (UNKNOWN_LOCATION,
4490 TYPE_DECL, get_identifier (pname),
4491 ptype));
4496 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4498 va_list_arg_type_node = va_list_ref_type_node =
4499 build_pointer_type (TREE_TYPE (va_list_type_node));
4501 else
4503 va_list_arg_type_node = va_list_type_node;
4504 va_list_ref_type_node = build_reference_type (va_list_type_node);
4507 if (!flag_preprocess_only)
4508 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4510 main_identifier_node = get_identifier ("main");
4512 /* Create the built-in __null node. It is important that this is
4513 not shared. */
4514 null_node = make_int_cst (1, 1);
4515 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4517 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4518 memset (builtin_types, 0, sizeof (builtin_types));
4521 /* The number of named compound-literals generated thus far. */
4522 static GTY(()) int compound_literal_number;
4524 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4526 void
4527 set_compound_literal_name (tree decl)
4529 char *name;
4530 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4531 compound_literal_number);
4532 compound_literal_number++;
4533 DECL_NAME (decl) = get_identifier (name);
4536 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4537 TYPE and operand OP. */
4539 static tree
4540 build_va_arg_1 (location_t loc, tree type, tree op)
4542 tree expr = build1 (VA_ARG_EXPR, type, op);
4543 SET_EXPR_LOCATION (expr, loc);
4544 return expr;
4547 /* Return a VA_ARG_EXPR corresponding to a source-level expression
4548 va_arg (EXPR, TYPE) at source location LOC. */
4550 tree
4551 build_va_arg (location_t loc, tree expr, tree type)
4553 tree va_type = TREE_TYPE (expr);
4554 tree canon_va_type = (va_type == error_mark_node
4555 ? error_mark_node
4556 : targetm.canonical_va_list_type (va_type));
4558 if (va_type == error_mark_node
4559 || canon_va_type == NULL_TREE)
4561 if (canon_va_type == NULL_TREE)
4562 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4564 /* Let's handle things neutrallly, if expr:
4565 - has undeclared type, or
4566 - is not an va_list type. */
4567 return build_va_arg_1 (loc, type, error_mark_node);
4570 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4572 /* Case 1: Not an array type. */
4574 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4575 type. */
4576 mark_addressable (expr);
4577 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4579 return build_va_arg_1 (loc, type, expr);
4582 /* Case 2: Array type.
4584 Background:
4586 For contrast, let's start with the simple case (case 1). If
4587 canon_va_type is not an array type, but say a char *, then when
4588 passing-by-value a va_list, the type of the va_list param decl is
4589 the same as for another va_list decl (all ap's are char *):
4591 f2_1 (char * ap)
4592 D.1815 = VA_ARG (&ap, 0B, 1);
4593 return D.1815;
4595 f2 (int i)
4596 char * ap.0;
4597 char * ap;
4598 __builtin_va_start (&ap, 0);
4599 ap.0 = ap;
4600 res = f2_1 (ap.0);
4601 __builtin_va_end (&ap);
4602 D.1812 = res;
4603 return D.1812;
4605 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4606 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4607 the same as for another va_list decl (case 2a, struct ap[1]).
4609 f2_1 (struct * ap)
4610 D.1844 = VA_ARG (ap, 0B, 0);
4611 return D.1844;
4613 f2 (int i)
4614 struct ap[1];
4615 __builtin_va_start (&ap, 0);
4616 res = f2_1 (&ap);
4617 __builtin_va_end (&ap);
4618 D.1841 = res;
4619 return D.1841;
4621 Case 2b is different because:
4622 - on the callee side, the parm decl has declared type va_list, but
4623 grokdeclarator changes the type of the parm decl to a pointer to the
4624 array elem type.
4625 - on the caller side, the pass-by-value uses &ap.
4627 We unify these two cases (case 2a: va_list is array type,
4628 case 2b: va_list is pointer to array elem type), by adding '&' for the
4629 array type case, such that we have a pointer to array elem in both
4630 cases. */
4632 if (TREE_CODE (va_type) == ARRAY_TYPE)
4634 /* Case 2a: va_list is array type. */
4636 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4637 elem type. */
4638 mark_addressable (expr);
4639 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4640 expr);
4642 /* Verify that &ap is still recognized as having va_list type. */
4643 tree canon_expr_type
4644 = targetm.canonical_va_list_type (TREE_TYPE (expr));
4645 gcc_assert (canon_expr_type != NULL_TREE);
4647 else
4649 /* Case 2b: va_list is pointer to array elem type. */
4650 gcc_assert (POINTER_TYPE_P (va_type));
4652 /* Comparison as in std_canonical_va_list_type. */
4653 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4654 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4656 /* Don't take the address. We've already got '&ap'. */
4660 return build_va_arg_1 (loc, type, expr);
4664 /* Linked list of disabled built-in functions. */
4666 struct disabled_builtin
4668 const char *name;
4669 struct disabled_builtin *next;
4671 static disabled_builtin *disabled_builtins = NULL;
4673 static bool builtin_function_disabled_p (const char *);
4675 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4676 begins with "__builtin_", give an error. */
4678 void
4679 disable_builtin_function (const char *name)
4681 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
4682 error ("cannot disable built-in function %qs", name);
4683 else
4685 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4686 new_disabled_builtin->name = name;
4687 new_disabled_builtin->next = disabled_builtins;
4688 disabled_builtins = new_disabled_builtin;
4693 /* Return true if the built-in function NAME has been disabled, false
4694 otherwise. */
4696 static bool
4697 builtin_function_disabled_p (const char *name)
4699 disabled_builtin *p;
4700 for (p = disabled_builtins; p != NULL; p = p->next)
4702 if (strcmp (name, p->name) == 0)
4703 return true;
4705 return false;
4709 /* Worker for DEF_BUILTIN.
4710 Possibly define a builtin function with one or two names.
4711 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4712 nonansi_p and flag_no_nonansi_builtin. */
4714 static void
4715 def_builtin_1 (enum built_in_function fncode,
4716 const char *name,
4717 enum built_in_class fnclass,
4718 tree fntype, tree libtype,
4719 bool both_p, bool fallback_p, bool nonansi_p,
4720 tree fnattrs, bool implicit_p)
4722 tree decl;
4723 const char *libname;
4725 if (fntype == error_mark_node)
4726 return;
4728 gcc_assert ((!both_p && !fallback_p)
4729 || !strncmp (name, "__builtin_",
4730 strlen ("__builtin_")));
4732 libname = name + strlen ("__builtin_");
4733 decl = add_builtin_function (name, fntype, fncode, fnclass,
4734 (fallback_p ? libname : NULL),
4735 fnattrs);
4737 set_builtin_decl (fncode, decl, implicit_p);
4739 if (both_p
4740 && !flag_no_builtin && !builtin_function_disabled_p (libname)
4741 && !(nonansi_p && flag_no_nonansi_builtin))
4742 add_builtin_function (libname, libtype, fncode, fnclass,
4743 NULL, fnattrs);
4746 /* Nonzero if the type T promotes to int. This is (nearly) the
4747 integral promotions defined in ISO C99 6.3.1.1/2. */
4749 bool
4750 c_promoting_integer_type_p (const_tree t)
4752 switch (TREE_CODE (t))
4754 case INTEGER_TYPE:
4755 return (TYPE_MAIN_VARIANT (t) == char_type_node
4756 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4757 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4758 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
4759 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4760 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
4762 case ENUMERAL_TYPE:
4763 /* ??? Technically all enumerations not larger than an int
4764 promote to an int. But this is used along code paths
4765 that only want to notice a size change. */
4766 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4768 case BOOLEAN_TYPE:
4769 return 1;
4771 default:
4772 return 0;
4776 /* Return 1 if PARMS specifies a fixed number of parameters
4777 and none of their types is affected by default promotions. */
4780 self_promoting_args_p (const_tree parms)
4782 const_tree t;
4783 for (t = parms; t; t = TREE_CHAIN (t))
4785 tree type = TREE_VALUE (t);
4787 if (type == error_mark_node)
4788 continue;
4790 if (TREE_CHAIN (t) == 0 && type != void_type_node)
4791 return 0;
4793 if (type == 0)
4794 return 0;
4796 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4797 return 0;
4799 if (c_promoting_integer_type_p (type))
4800 return 0;
4802 return 1;
4805 /* Recursively remove any '*' or '&' operator from TYPE. */
4806 tree
4807 strip_pointer_operator (tree t)
4809 while (POINTER_TYPE_P (t))
4810 t = TREE_TYPE (t);
4811 return t;
4814 /* Recursively remove pointer or array type from TYPE. */
4815 tree
4816 strip_pointer_or_array_types (tree t)
4818 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4819 t = TREE_TYPE (t);
4820 return t;
4823 /* Used to compare case labels. K1 and K2 are actually tree nodes
4824 representing case labels, or NULL_TREE for a `default' label.
4825 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4826 K2, and 0 if K1 and K2 are equal. */
4829 case_compare (splay_tree_key k1, splay_tree_key k2)
4831 /* Consider a NULL key (such as arises with a `default' label) to be
4832 smaller than anything else. */
4833 if (!k1)
4834 return k2 ? -1 : 0;
4835 else if (!k2)
4836 return k1 ? 1 : 0;
4838 return tree_int_cst_compare ((tree) k1, (tree) k2);
4841 /* Process a case label, located at LOC, for the range LOW_VALUE
4842 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4843 then this case label is actually a `default' label. If only
4844 HIGH_VALUE is NULL_TREE, then case label was declared using the
4845 usual C/C++ syntax, rather than the GNU case range extension.
4846 CASES is a tree containing all the case ranges processed so far;
4847 COND is the condition for the switch-statement itself.
4848 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4849 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4850 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
4852 tree
4853 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
4854 tree low_value, tree high_value, bool *outside_range_p)
4856 tree type;
4857 tree label;
4858 tree case_label;
4859 splay_tree_node node;
4861 /* Create the LABEL_DECL itself. */
4862 label = create_artificial_label (loc);
4864 /* If there was an error processing the switch condition, bail now
4865 before we get more confused. */
4866 if (!cond || cond == error_mark_node)
4867 goto error_out;
4869 if ((low_value && TREE_TYPE (low_value)
4870 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4871 || (high_value && TREE_TYPE (high_value)
4872 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4874 error_at (loc, "pointers are not permitted as case values");
4875 goto error_out;
4878 /* Case ranges are a GNU extension. */
4879 if (high_value)
4880 pedwarn (loc, OPT_Wpedantic,
4881 "range expressions in switch statements are non-standard");
4883 type = TREE_TYPE (cond);
4884 if (low_value)
4886 low_value = check_case_value (loc, low_value);
4887 low_value = convert_and_check (loc, type, low_value);
4888 if (low_value == error_mark_node)
4889 goto error_out;
4891 if (high_value)
4893 high_value = check_case_value (loc, high_value);
4894 high_value = convert_and_check (loc, type, high_value);
4895 if (high_value == error_mark_node)
4896 goto error_out;
4899 if (low_value && high_value)
4901 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4902 really a case range, even though it was written that way.
4903 Remove the HIGH_VALUE to simplify later processing. */
4904 if (tree_int_cst_equal (low_value, high_value))
4905 high_value = NULL_TREE;
4906 else if (!tree_int_cst_lt (low_value, high_value))
4907 warning_at (loc, 0, "empty range specified");
4910 /* See if the case is in range of the type of the original testing
4911 expression. If both low_value and high_value are out of range,
4912 don't insert the case label and return NULL_TREE. */
4913 if (low_value
4914 && !check_case_bounds (loc, type, orig_type,
4915 &low_value, high_value ? &high_value : NULL,
4916 outside_range_p))
4917 return NULL_TREE;
4919 /* Look up the LOW_VALUE in the table of case labels we already
4920 have. */
4921 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4922 /* If there was not an exact match, check for overlapping ranges.
4923 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4924 that's a `default' label and the only overlap is an exact match. */
4925 if (!node && (low_value || high_value))
4927 splay_tree_node low_bound;
4928 splay_tree_node high_bound;
4930 /* Even though there wasn't an exact match, there might be an
4931 overlap between this case range and another case range.
4932 Since we've (inductively) not allowed any overlapping case
4933 ranges, we simply need to find the greatest low case label
4934 that is smaller that LOW_VALUE, and the smallest low case
4935 label that is greater than LOW_VALUE. If there is an overlap
4936 it will occur in one of these two ranges. */
4937 low_bound = splay_tree_predecessor (cases,
4938 (splay_tree_key) low_value);
4939 high_bound = splay_tree_successor (cases,
4940 (splay_tree_key) low_value);
4942 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4943 the LOW_VALUE, so there is no need to check unless the
4944 LOW_BOUND is in fact itself a case range. */
4945 if (low_bound
4946 && CASE_HIGH ((tree) low_bound->value)
4947 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4948 low_value) >= 0)
4949 node = low_bound;
4950 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4951 range is bigger than the low end of the current range, so we
4952 are only interested if the current range is a real range, and
4953 not an ordinary case label. */
4954 else if (high_bound
4955 && high_value
4956 && (tree_int_cst_compare ((tree) high_bound->key,
4957 high_value)
4958 <= 0))
4959 node = high_bound;
4961 /* If there was an overlap, issue an error. */
4962 if (node)
4964 tree duplicate = CASE_LABEL ((tree) node->value);
4966 if (high_value)
4968 error_at (loc, "duplicate (or overlapping) case value");
4969 error_at (DECL_SOURCE_LOCATION (duplicate),
4970 "this is the first entry overlapping that value");
4972 else if (low_value)
4974 error_at (loc, "duplicate case value") ;
4975 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
4977 else
4979 error_at (loc, "multiple default labels in one switch");
4980 error_at (DECL_SOURCE_LOCATION (duplicate),
4981 "this is the first default label");
4983 goto error_out;
4986 /* Add a CASE_LABEL to the statement-tree. */
4987 case_label = add_stmt (build_case_label (low_value, high_value, label));
4988 /* Register this case label in the splay tree. */
4989 splay_tree_insert (cases,
4990 (splay_tree_key) low_value,
4991 (splay_tree_value) case_label);
4993 return case_label;
4995 error_out:
4996 /* Add a label so that the back-end doesn't think that the beginning of
4997 the switch is unreachable. Note that we do not add a case label, as
4998 that just leads to duplicates and thence to failure later on. */
4999 if (!cases->root)
5001 tree t = create_artificial_label (loc);
5002 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5004 return error_mark_node;
5007 /* Finish an expression taking the address of LABEL (an
5008 IDENTIFIER_NODE). Returns an expression for the address.
5010 LOC is the location for the expression returned. */
5012 tree
5013 finish_label_address_expr (tree label, location_t loc)
5015 tree result;
5017 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5019 if (label == error_mark_node)
5020 return error_mark_node;
5022 label = lookup_label (label);
5023 if (label == NULL_TREE)
5024 result = null_pointer_node;
5025 else
5027 TREE_USED (label) = 1;
5028 result = build1 (ADDR_EXPR, ptr_type_node, label);
5029 /* The current function is not necessarily uninlinable.
5030 Computed gotos are incompatible with inlining, but the value
5031 here could be used only in a diagnostic, for example. */
5032 protected_set_expr_location (result, loc);
5035 return result;
5039 /* Given a boolean expression ARG, return a tree representing an increment
5040 or decrement (as indicated by CODE) of ARG. The front end must check for
5041 invalid cases (e.g., decrement in C++). */
5042 tree
5043 boolean_increment (enum tree_code code, tree arg)
5045 tree val;
5046 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5048 arg = stabilize_reference (arg);
5049 switch (code)
5051 case PREINCREMENT_EXPR:
5052 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5053 break;
5054 case POSTINCREMENT_EXPR:
5055 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5056 arg = save_expr (arg);
5057 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5058 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5059 break;
5060 case PREDECREMENT_EXPR:
5061 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5062 invert_truthvalue_loc (input_location, arg));
5063 break;
5064 case POSTDECREMENT_EXPR:
5065 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5066 invert_truthvalue_loc (input_location, arg));
5067 arg = save_expr (arg);
5068 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5069 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5070 break;
5071 default:
5072 gcc_unreachable ();
5074 TREE_SIDE_EFFECTS (val) = 1;
5075 return val;
5078 /* Built-in macros for stddef.h and stdint.h, that require macros
5079 defined in this file. */
5080 void
5081 c_stddef_cpp_builtins(void)
5083 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5084 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5085 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5086 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5087 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5088 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5089 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5090 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5091 if (SIG_ATOMIC_TYPE)
5092 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5093 if (INT8_TYPE)
5094 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5095 if (INT16_TYPE)
5096 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5097 if (INT32_TYPE)
5098 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5099 if (INT64_TYPE)
5100 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5101 if (UINT8_TYPE)
5102 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5103 if (UINT16_TYPE)
5104 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5105 if (UINT32_TYPE)
5106 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5107 if (UINT64_TYPE)
5108 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5109 if (INT_LEAST8_TYPE)
5110 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5111 if (INT_LEAST16_TYPE)
5112 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5113 if (INT_LEAST32_TYPE)
5114 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5115 if (INT_LEAST64_TYPE)
5116 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5117 if (UINT_LEAST8_TYPE)
5118 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5119 if (UINT_LEAST16_TYPE)
5120 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5121 if (UINT_LEAST32_TYPE)
5122 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5123 if (UINT_LEAST64_TYPE)
5124 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5125 if (INT_FAST8_TYPE)
5126 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5127 if (INT_FAST16_TYPE)
5128 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5129 if (INT_FAST32_TYPE)
5130 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5131 if (INT_FAST64_TYPE)
5132 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5133 if (UINT_FAST8_TYPE)
5134 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5135 if (UINT_FAST16_TYPE)
5136 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5137 if (UINT_FAST32_TYPE)
5138 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5139 if (UINT_FAST64_TYPE)
5140 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5141 if (INTPTR_TYPE)
5142 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5143 if (UINTPTR_TYPE)
5144 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5147 static void
5148 c_init_attributes (void)
5150 /* Fill in the built_in_attributes array. */
5151 #define DEF_ATTR_NULL_TREE(ENUM) \
5152 built_in_attributes[(int) ENUM] = NULL_TREE;
5153 #define DEF_ATTR_INT(ENUM, VALUE) \
5154 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5155 #define DEF_ATTR_STRING(ENUM, VALUE) \
5156 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5157 #define DEF_ATTR_IDENT(ENUM, STRING) \
5158 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5159 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5160 built_in_attributes[(int) ENUM] \
5161 = tree_cons (built_in_attributes[(int) PURPOSE], \
5162 built_in_attributes[(int) VALUE], \
5163 built_in_attributes[(int) CHAIN]);
5164 #include "builtin-attrs.def"
5165 #undef DEF_ATTR_NULL_TREE
5166 #undef DEF_ATTR_INT
5167 #undef DEF_ATTR_IDENT
5168 #undef DEF_ATTR_TREE_LIST
5171 /* Check whether ALIGN is a valid user-specified alignment. If so,
5172 return its base-2 log; if not, output an error and return -1. If
5173 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
5174 no error. */
5176 check_user_alignment (const_tree align, bool allow_zero)
5178 int i;
5180 if (error_operand_p (align))
5181 return -1;
5182 if (TREE_CODE (align) != INTEGER_CST
5183 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5185 error ("requested alignment is not an integer constant");
5186 return -1;
5188 else if (allow_zero && integer_zerop (align))
5189 return -1;
5190 else if (tree_int_cst_sgn (align) == -1
5191 || (i = tree_log2 (align)) == -1)
5193 error ("requested alignment is not a positive power of 2");
5194 return -1;
5196 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5198 error ("requested alignment is too large");
5199 return -1;
5201 return i;
5204 /* Determine the ELF symbol visibility for DECL, which is either a
5205 variable or a function. It is an error to use this function if a
5206 definition of DECL is not available in this translation unit.
5207 Returns true if the final visibility has been determined by this
5208 function; false if the caller is free to make additional
5209 modifications. */
5211 bool
5212 c_determine_visibility (tree decl)
5214 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5216 /* If the user explicitly specified the visibility with an
5217 attribute, honor that. DECL_VISIBILITY will have been set during
5218 the processing of the attribute. We check for an explicit
5219 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5220 to distinguish the use of an attribute from the use of a "#pragma
5221 GCC visibility push(...)"; in the latter case we still want other
5222 considerations to be able to overrule the #pragma. */
5223 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5224 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5225 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5226 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5227 return true;
5229 /* Set default visibility to whatever the user supplied with
5230 visibility_specified depending on #pragma GCC visibility. */
5231 if (!DECL_VISIBILITY_SPECIFIED (decl))
5233 if (visibility_options.inpragma
5234 || DECL_VISIBILITY (decl) != default_visibility)
5236 DECL_VISIBILITY (decl) = default_visibility;
5237 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5238 /* If visibility changed and DECL already has DECL_RTL, ensure
5239 symbol flags are updated. */
5240 if (((VAR_P (decl) && TREE_STATIC (decl))
5241 || TREE_CODE (decl) == FUNCTION_DECL)
5242 && DECL_RTL_SET_P (decl))
5243 make_decl_rtl (decl);
5246 return false;
5249 /* Check the argument list of a function call for null in argument slots
5250 that are marked as requiring a non-null pointer argument. The NARGS
5251 arguments are passed in the array ARGARRAY. */
5253 static void
5254 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
5256 tree a;
5257 int i;
5259 attrs = lookup_attribute ("nonnull", attrs);
5260 if (attrs == NULL_TREE)
5261 return;
5263 a = attrs;
5264 /* See if any of the nonnull attributes has no arguments. If so,
5265 then every pointer argument is checked (in which case the check
5266 for pointer type is done in check_nonnull_arg). */
5267 if (TREE_VALUE (a) != NULL_TREE)
5269 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5270 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5272 if (a != NULL_TREE)
5273 for (i = 0; i < nargs; i++)
5274 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
5275 i + 1);
5276 else
5278 /* Walk the argument list. If we encounter an argument number we
5279 should check for non-null, do it. */
5280 for (i = 0; i < nargs; i++)
5282 for (a = attrs; ; a = TREE_CHAIN (a))
5284 a = lookup_attribute ("nonnull", a);
5285 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5286 break;
5289 if (a != NULL_TREE)
5290 check_function_arguments_recurse (check_nonnull_arg, &loc,
5291 argarray[i], i + 1);
5296 /* Check that the Nth argument of a function call (counting backwards
5297 from the end) is a (pointer)0. The NARGS arguments are passed in the
5298 array ARGARRAY. */
5300 static void
5301 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5303 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5305 if (attr)
5307 int len = 0;
5308 int pos = 0;
5309 tree sentinel;
5310 function_args_iterator iter;
5311 tree t;
5313 /* Skip over the named arguments. */
5314 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5316 if (len == nargs)
5317 break;
5318 len++;
5321 if (TREE_VALUE (attr))
5323 tree p = TREE_VALUE (TREE_VALUE (attr));
5324 pos = TREE_INT_CST_LOW (p);
5327 /* The sentinel must be one of the varargs, i.e.
5328 in position >= the number of fixed arguments. */
5329 if ((nargs - 1 - pos) < len)
5331 warning (OPT_Wformat_,
5332 "not enough variable arguments to fit a sentinel");
5333 return;
5336 /* Validate the sentinel. */
5337 sentinel = argarray[nargs - 1 - pos];
5338 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5339 || !integer_zerop (sentinel))
5340 /* Although __null (in C++) is only an integer we allow it
5341 nevertheless, as we are guaranteed that it's exactly
5342 as wide as a pointer, and we don't want to force
5343 users to cast the NULL they have written there.
5344 We warn with -Wstrict-null-sentinel, though. */
5345 && (warn_strict_null_sentinel || null_node != sentinel))
5346 warning (OPT_Wformat_, "missing sentinel in function call");
5350 /* Helper for check_function_nonnull; given a list of operands which
5351 must be non-null in ARGS, determine if operand PARAM_NUM should be
5352 checked. */
5354 static bool
5355 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5357 unsigned HOST_WIDE_INT arg_num = 0;
5359 for (; args; args = TREE_CHAIN (args))
5361 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
5363 gcc_assert (found);
5365 if (arg_num == param_num)
5366 return true;
5368 return false;
5371 /* Check that the function argument PARAM (which is operand number
5372 PARAM_NUM) is non-null. This is called by check_function_nonnull
5373 via check_function_arguments_recurse. */
5375 static void
5376 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5378 location_t *ploc = (location_t *) ctx;
5380 /* Just skip checking the argument if it's not a pointer. This can
5381 happen if the "nonnull" attribute was given without an operand
5382 list (which means to check every pointer argument). */
5384 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5385 return;
5387 if (integer_zerop (param))
5388 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
5389 "(argument %lu)", (unsigned long) param_num);
5392 /* Helper for nonnull attribute handling; fetch the operand number
5393 from the attribute argument list. */
5395 bool
5396 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5398 /* Verify the arg number is a small constant. */
5399 if (tree_fits_uhwi_p (arg_num_expr))
5401 *valp = TREE_INT_CST_LOW (arg_num_expr);
5402 return true;
5404 else
5405 return false;
5408 /* Arguments being collected for optimization. */
5409 typedef const char *const_char_p; /* For DEF_VEC_P. */
5410 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
5413 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
5414 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5415 false for #pragma GCC optimize. */
5417 bool
5418 parse_optimize_options (tree args, bool attr_p)
5420 bool ret = true;
5421 unsigned opt_argc;
5422 unsigned i;
5423 const char **opt_argv;
5424 struct cl_decoded_option *decoded_options;
5425 unsigned int decoded_options_count;
5426 tree ap;
5428 /* Build up argv vector. Just in case the string is stored away, use garbage
5429 collected strings. */
5430 vec_safe_truncate (optimize_args, 0);
5431 vec_safe_push (optimize_args, (const char *) NULL);
5433 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5435 tree value = TREE_VALUE (ap);
5437 if (TREE_CODE (value) == INTEGER_CST)
5439 char buffer[20];
5440 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
5441 vec_safe_push (optimize_args, ggc_strdup (buffer));
5444 else if (TREE_CODE (value) == STRING_CST)
5446 /* Split string into multiple substrings. */
5447 size_t len = TREE_STRING_LENGTH (value);
5448 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5449 char *end = p + len;
5450 char *comma;
5451 char *next_p = p;
5453 while (next_p != NULL)
5455 size_t len2;
5456 char *q, *r;
5458 p = next_p;
5459 comma = strchr (p, ',');
5460 if (comma)
5462 len2 = comma - p;
5463 *comma = '\0';
5464 next_p = comma+1;
5466 else
5468 len2 = end - p;
5469 next_p = NULL;
5472 r = q = (char *) ggc_alloc_atomic (len2 + 3);
5474 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5475 options. */
5476 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5478 ret = false;
5479 if (attr_p)
5480 warning (OPT_Wattributes,
5481 "bad option %qs to attribute %<optimize%>", p);
5482 else
5483 warning (OPT_Wpragmas,
5484 "bad option %qs to pragma %<optimize%>", p);
5485 continue;
5488 if (*p != '-')
5490 *r++ = '-';
5492 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5493 itself is -Os, and any other switch begins with a -f. */
5494 if ((*p >= '0' && *p <= '9')
5495 || (p[0] == 's' && p[1] == '\0'))
5496 *r++ = 'O';
5497 else if (*p != 'O')
5498 *r++ = 'f';
5501 memcpy (r, p, len2);
5502 r[len2] = '\0';
5503 vec_safe_push (optimize_args, (const char *) q);
5509 opt_argc = optimize_args->length ();
5510 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5512 for (i = 1; i < opt_argc; i++)
5513 opt_argv[i] = (*optimize_args)[i];
5515 /* Now parse the options. */
5516 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5517 &decoded_options,
5518 &decoded_options_count);
5519 /* Drop non-Optimization options. */
5520 unsigned j = 1;
5521 for (i = 1; i < decoded_options_count; ++i)
5523 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5525 ret = false;
5526 if (attr_p)
5527 warning (OPT_Wattributes,
5528 "bad option %qs to attribute %<optimize%>",
5529 decoded_options[i].orig_option_with_args_text);
5530 else
5531 warning (OPT_Wpragmas,
5532 "bad option %qs to pragma %<optimize%>",
5533 decoded_options[i].orig_option_with_args_text);
5534 continue;
5536 if (i != j)
5537 decoded_options[j] = decoded_options[i];
5538 j++;
5540 decoded_options_count = j;
5541 /* And apply them. */
5542 decode_options (&global_options, &global_options_set,
5543 decoded_options, decoded_options_count,
5544 input_location, global_dc);
5546 targetm.override_options_after_change();
5548 optimize_args->truncate (0);
5549 return ret;
5552 /* Check whether ATTR is a valid attribute fallthrough. */
5554 bool
5555 attribute_fallthrough_p (tree attr)
5557 tree t = lookup_attribute ("fallthrough", attr);
5558 if (t == NULL_TREE)
5559 return false;
5560 /* This attribute shall appear at most once in each attribute-list. */
5561 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5562 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5563 "times");
5564 /* No attribute-argument-clause shall be present. */
5565 else if (TREE_VALUE (t) != NULL_TREE)
5566 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5567 "a parameter");
5568 /* Warn if other attributes are found. */
5569 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5571 tree name = get_attribute_name (t);
5572 if (!is_attribute_p ("fallthrough", name))
5573 warning (OPT_Wattributes, "%qE attribute ignored", name);
5575 return true;
5579 /* Check for valid arguments being passed to a function with FNTYPE.
5580 There are NARGS arguments in the array ARGARRAY. LOC should be used for
5581 diagnostics. */
5582 void
5583 check_function_arguments (location_t loc, const_tree fntype, int nargs,
5584 tree *argarray)
5586 /* Check for null being passed in a pointer argument that must be
5587 non-null. We also need to do this if format checking is enabled. */
5589 if (warn_nonnull)
5590 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
5592 /* Check for errors in format strings. */
5594 if (warn_format || warn_suggest_attribute_format)
5595 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
5597 if (warn_format)
5598 check_function_sentinel (fntype, nargs, argarray);
5601 /* Generic argument checking recursion routine. PARAM is the argument to
5602 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5603 once the argument is resolved. CTX is context for the callback. */
5604 void
5605 check_function_arguments_recurse (void (*callback)
5606 (void *, tree, unsigned HOST_WIDE_INT),
5607 void *ctx, tree param,
5608 unsigned HOST_WIDE_INT param_num)
5610 if (CONVERT_EXPR_P (param)
5611 && (TYPE_PRECISION (TREE_TYPE (param))
5612 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
5614 /* Strip coercion. */
5615 check_function_arguments_recurse (callback, ctx,
5616 TREE_OPERAND (param, 0), param_num);
5617 return;
5620 if (TREE_CODE (param) == CALL_EXPR)
5622 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
5623 tree attrs;
5624 bool found_format_arg = false;
5626 /* See if this is a call to a known internationalization function
5627 that modifies a format arg. Such a function may have multiple
5628 format_arg attributes (for example, ngettext). */
5630 for (attrs = TYPE_ATTRIBUTES (type);
5631 attrs;
5632 attrs = TREE_CHAIN (attrs))
5633 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5635 tree inner_arg;
5636 tree format_num_expr;
5637 int format_num;
5638 int i;
5639 call_expr_arg_iterator iter;
5641 /* Extract the argument number, which was previously checked
5642 to be valid. */
5643 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5645 format_num = tree_to_uhwi (format_num_expr);
5647 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
5648 inner_arg != 0;
5649 inner_arg = next_call_expr_arg (&iter), i++)
5650 if (i == format_num)
5652 check_function_arguments_recurse (callback, ctx,
5653 inner_arg, param_num);
5654 found_format_arg = true;
5655 break;
5659 /* If we found a format_arg attribute and did a recursive check,
5660 we are done with checking this argument. Otherwise, we continue
5661 and this will be considered a non-literal. */
5662 if (found_format_arg)
5663 return;
5666 if (TREE_CODE (param) == COND_EXPR)
5668 /* Simplify to avoid warning for an impossible case. */
5669 param = fold_for_warn (param);
5670 if (TREE_CODE (param) == COND_EXPR)
5672 /* Check both halves of the conditional expression. */
5673 check_function_arguments_recurse (callback, ctx,
5674 TREE_OPERAND (param, 1),
5675 param_num);
5676 check_function_arguments_recurse (callback, ctx,
5677 TREE_OPERAND (param, 2),
5678 param_num);
5679 return;
5683 (*callback) (ctx, param, param_num);
5686 /* Checks for a builtin function FNDECL that the number of arguments
5687 NARGS against the required number REQUIRED and issues an error if
5688 there is a mismatch. Returns true if the number of arguments is
5689 correct, otherwise false. LOC is the location of FNDECL. */
5691 static bool
5692 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5693 int required)
5695 if (nargs < required)
5697 error_at (loc, "too few arguments to function %qE", fndecl);
5698 return false;
5700 else if (nargs > required)
5702 error_at (loc, "too many arguments to function %qE", fndecl);
5703 return false;
5705 return true;
5708 /* Helper macro for check_builtin_function_arguments. */
5709 #define ARG_LOCATION(N) \
5710 (arg_loc.is_empty () \
5711 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5712 : expansion_point_location (arg_loc[(N)]))
5714 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5715 Returns false if there was an error, otherwise true. LOC is the
5716 location of the function; ARG_LOC is a vector of locations of the
5717 arguments. */
5719 bool
5720 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5721 tree fndecl, int nargs, tree *args)
5723 if (!DECL_BUILT_IN (fndecl)
5724 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5725 return true;
5727 switch (DECL_FUNCTION_CODE (fndecl))
5729 case BUILT_IN_ALLOCA_WITH_ALIGN:
5731 /* Get the requested alignment (in bits) if it's a constant
5732 integer expression. */
5733 unsigned HOST_WIDE_INT align
5734 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5736 /* Determine if the requested alignment is a power of 2. */
5737 if ((align & (align - 1)))
5738 align = 0;
5740 /* The maximum alignment in bits corresponding to the same
5741 maximum in bytes enforced in check_user_alignment(). */
5742 unsigned maxalign = (UINT_MAX >> 1) + 1;
5744 /* Reject invalid alignments. */
5745 if (align < BITS_PER_UNIT || maxalign < align)
5747 error_at (ARG_LOCATION (1),
5748 "second argument to function %qE must be a constant "
5749 "integer power of 2 between %qi and %qu bits",
5750 fndecl, BITS_PER_UNIT, maxalign);
5751 return false;
5753 return true;
5756 case BUILT_IN_CONSTANT_P:
5757 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
5759 case BUILT_IN_ISFINITE:
5760 case BUILT_IN_ISINF:
5761 case BUILT_IN_ISINF_SIGN:
5762 case BUILT_IN_ISNAN:
5763 case BUILT_IN_ISNORMAL:
5764 case BUILT_IN_SIGNBIT:
5765 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
5767 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5769 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5770 "call to function %qE", fndecl);
5771 return false;
5773 return true;
5775 return false;
5777 case BUILT_IN_ISGREATER:
5778 case BUILT_IN_ISGREATEREQUAL:
5779 case BUILT_IN_ISLESS:
5780 case BUILT_IN_ISLESSEQUAL:
5781 case BUILT_IN_ISLESSGREATER:
5782 case BUILT_IN_ISUNORDERED:
5783 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
5785 enum tree_code code0, code1;
5786 code0 = TREE_CODE (TREE_TYPE (args[0]));
5787 code1 = TREE_CODE (TREE_TYPE (args[1]));
5788 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5789 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5790 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5792 error_at (loc, "non-floating-point arguments in call to "
5793 "function %qE", fndecl);
5794 return false;
5796 return true;
5798 return false;
5800 case BUILT_IN_FPCLASSIFY:
5801 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
5803 for (unsigned int i = 0; i < 5; i++)
5804 if (TREE_CODE (args[i]) != INTEGER_CST)
5806 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5807 "call to function %qE", i + 1, fndecl);
5808 return false;
5811 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5813 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5814 "call to function %qE", fndecl);
5815 return false;
5817 return true;
5819 return false;
5821 case BUILT_IN_ASSUME_ALIGNED:
5822 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
5824 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5826 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5827 "function %qE", fndecl);
5828 return false;
5830 return true;
5832 return false;
5834 case BUILT_IN_ADD_OVERFLOW:
5835 case BUILT_IN_SUB_OVERFLOW:
5836 case BUILT_IN_MUL_OVERFLOW:
5837 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5839 unsigned i;
5840 for (i = 0; i < 2; i++)
5841 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5843 error_at (ARG_LOCATION (i), "argument %u in call to function "
5844 "%qE does not have integral type", i + 1, fndecl);
5845 return false;
5847 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
5848 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
5850 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5851 "does not have pointer to integral type", fndecl);
5852 return false;
5854 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5856 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5857 "has pointer to enumerated type", fndecl);
5858 return false;
5860 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5862 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5863 "has pointer to boolean type", fndecl);
5864 return false;
5866 return true;
5868 return false;
5870 case BUILT_IN_ADD_OVERFLOW_P:
5871 case BUILT_IN_SUB_OVERFLOW_P:
5872 case BUILT_IN_MUL_OVERFLOW_P:
5873 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5875 unsigned i;
5876 for (i = 0; i < 3; i++)
5877 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5879 error_at (ARG_LOCATION (i), "argument %u in call to function "
5880 "%qE does not have integral type", i + 1, fndecl);
5881 return false;
5883 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5885 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5886 "%qE has enumerated type", fndecl);
5887 return false;
5889 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5891 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5892 "%qE has boolean type", fndecl);
5893 return false;
5895 return true;
5897 return false;
5899 default:
5900 return true;
5904 /* Function to help qsort sort FIELD_DECLs by name order. */
5907 field_decl_cmp (const void *x_p, const void *y_p)
5909 const tree *const x = (const tree *const) x_p;
5910 const tree *const y = (const tree *const) y_p;
5912 if (DECL_NAME (*x) == DECL_NAME (*y))
5913 /* A nontype is "greater" than a type. */
5914 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5915 if (DECL_NAME (*x) == NULL_TREE)
5916 return -1;
5917 if (DECL_NAME (*y) == NULL_TREE)
5918 return 1;
5919 if (DECL_NAME (*x) < DECL_NAME (*y))
5920 return -1;
5921 return 1;
5924 static struct {
5925 gt_pointer_operator new_value;
5926 void *cookie;
5927 } resort_data;
5929 /* This routine compares two fields like field_decl_cmp but using the
5930 pointer operator in resort_data. */
5932 static int
5933 resort_field_decl_cmp (const void *x_p, const void *y_p)
5935 const tree *const x = (const tree *const) x_p;
5936 const tree *const y = (const tree *const) y_p;
5938 if (DECL_NAME (*x) == DECL_NAME (*y))
5939 /* A nontype is "greater" than a type. */
5940 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
5941 if (DECL_NAME (*x) == NULL_TREE)
5942 return -1;
5943 if (DECL_NAME (*y) == NULL_TREE)
5944 return 1;
5946 tree d1 = DECL_NAME (*x);
5947 tree d2 = DECL_NAME (*y);
5948 resort_data.new_value (&d1, resort_data.cookie);
5949 resort_data.new_value (&d2, resort_data.cookie);
5950 if (d1 < d2)
5951 return -1;
5953 return 1;
5956 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
5958 void
5959 resort_sorted_fields (void *obj,
5960 void * ARG_UNUSED (orig_obj),
5961 gt_pointer_operator new_value,
5962 void *cookie)
5964 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
5965 resort_data.new_value = new_value;
5966 resort_data.cookie = cookie;
5967 qsort (&sf->elts[0], sf->len, sizeof (tree),
5968 resort_field_decl_cmp);
5971 /* Subroutine of c_parse_error.
5972 Return the result of concatenating LHS and RHS. RHS is really
5973 a string literal, its first character is indicated by RHS_START and
5974 RHS_SIZE is its length (including the terminating NUL character).
5976 The caller is responsible for deleting the returned pointer. */
5978 static char *
5979 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5981 const int lhs_size = strlen (lhs);
5982 char *result = XNEWVEC (char, lhs_size + rhs_size);
5983 strncpy (result, lhs, lhs_size);
5984 strncpy (result + lhs_size, rhs_start, rhs_size);
5985 return result;
5988 /* Issue the error given by GMSGID, indicating that it occurred before
5989 TOKEN, which had the associated VALUE. */
5991 void
5992 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
5993 tree value, unsigned char token_flags)
5995 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
5997 char *message = NULL;
5999 if (token_type == CPP_EOF)
6000 message = catenate_messages (gmsgid, " at end of input");
6001 else if (token_type == CPP_CHAR
6002 || token_type == CPP_WCHAR
6003 || token_type == CPP_CHAR16
6004 || token_type == CPP_CHAR32
6005 || token_type == CPP_UTF8CHAR)
6007 unsigned int val = TREE_INT_CST_LOW (value);
6008 const char *prefix;
6010 switch (token_type)
6012 default:
6013 prefix = "";
6014 break;
6015 case CPP_WCHAR:
6016 prefix = "L";
6017 break;
6018 case CPP_CHAR16:
6019 prefix = "u";
6020 break;
6021 case CPP_CHAR32:
6022 prefix = "U";
6023 break;
6024 case CPP_UTF8CHAR:
6025 prefix = "u8";
6026 break;
6029 if (val <= UCHAR_MAX && ISGRAPH (val))
6030 message = catenate_messages (gmsgid, " before %s'%c'");
6031 else
6032 message = catenate_messages (gmsgid, " before %s'\\x%x'");
6034 error (message, prefix, val);
6035 free (message);
6036 message = NULL;
6038 else if (token_type == CPP_CHAR_USERDEF
6039 || token_type == CPP_WCHAR_USERDEF
6040 || token_type == CPP_CHAR16_USERDEF
6041 || token_type == CPP_CHAR32_USERDEF
6042 || token_type == CPP_UTF8CHAR_USERDEF)
6043 message = catenate_messages (gmsgid,
6044 " before user-defined character literal");
6045 else if (token_type == CPP_STRING_USERDEF
6046 || token_type == CPP_WSTRING_USERDEF
6047 || token_type == CPP_STRING16_USERDEF
6048 || token_type == CPP_STRING32_USERDEF
6049 || token_type == CPP_UTF8STRING_USERDEF)
6050 message = catenate_messages (gmsgid, " before user-defined string literal");
6051 else if (token_type == CPP_STRING
6052 || token_type == CPP_WSTRING
6053 || token_type == CPP_STRING16
6054 || token_type == CPP_STRING32
6055 || token_type == CPP_UTF8STRING)
6056 message = catenate_messages (gmsgid, " before string constant");
6057 else if (token_type == CPP_NUMBER)
6058 message = catenate_messages (gmsgid, " before numeric constant");
6059 else if (token_type == CPP_NAME)
6061 message = catenate_messages (gmsgid, " before %qE");
6062 error (message, value);
6063 free (message);
6064 message = NULL;
6066 else if (token_type == CPP_PRAGMA)
6067 message = catenate_messages (gmsgid, " before %<#pragma%>");
6068 else if (token_type == CPP_PRAGMA_EOL)
6069 message = catenate_messages (gmsgid, " before end of line");
6070 else if (token_type == CPP_DECLTYPE)
6071 message = catenate_messages (gmsgid, " before %<decltype%>");
6072 else if (token_type < N_TTYPES)
6074 message = catenate_messages (gmsgid, " before %qs token");
6075 error (message, cpp_type2name (token_type, token_flags));
6076 free (message);
6077 message = NULL;
6079 else
6080 error (gmsgid);
6082 if (message)
6084 error (message);
6085 free (message);
6087 #undef catenate_messages
6090 /* Return the gcc option code associated with the reason for a cpp
6091 message, or 0 if none. */
6093 static int
6094 c_option_controlling_cpp_error (int reason)
6096 const struct cpp_reason_option_codes_t *entry;
6098 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6100 if (entry->reason == reason)
6101 return entry->option_code;
6103 return 0;
6106 /* Callback from cpp_error for PFILE to print diagnostics from the
6107 preprocessor. The diagnostic is of type LEVEL, with REASON set
6108 to the reason code if LEVEL is represents a warning, at location
6109 RICHLOC unless this is after lexing and the compiler's location
6110 should be used instead; MSG is the translated message and AP
6111 the arguments. Returns true if a diagnostic was emitted, false
6112 otherwise. */
6114 bool
6115 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
6116 rich_location *richloc,
6117 const char *msg, va_list *ap)
6119 diagnostic_info diagnostic;
6120 diagnostic_t dlevel;
6121 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6122 bool ret;
6124 switch (level)
6126 case CPP_DL_WARNING_SYSHDR:
6127 if (flag_no_output)
6128 return false;
6129 global_dc->dc_warn_system_headers = 1;
6130 /* Fall through. */
6131 case CPP_DL_WARNING:
6132 if (flag_no_output)
6133 return false;
6134 dlevel = DK_WARNING;
6135 break;
6136 case CPP_DL_PEDWARN:
6137 if (flag_no_output && !flag_pedantic_errors)
6138 return false;
6139 dlevel = DK_PEDWARN;
6140 break;
6141 case CPP_DL_ERROR:
6142 dlevel = DK_ERROR;
6143 break;
6144 case CPP_DL_ICE:
6145 dlevel = DK_ICE;
6146 break;
6147 case CPP_DL_NOTE:
6148 dlevel = DK_NOTE;
6149 break;
6150 case CPP_DL_FATAL:
6151 dlevel = DK_FATAL;
6152 break;
6153 default:
6154 gcc_unreachable ();
6156 if (done_lexing)
6157 richloc->set_range (line_table, 0, input_location, true);
6158 diagnostic_set_info_translated (&diagnostic, msg, ap,
6159 richloc, dlevel);
6160 diagnostic_override_option_index (&diagnostic,
6161 c_option_controlling_cpp_error (reason));
6162 ret = report_diagnostic (&diagnostic);
6163 if (level == CPP_DL_WARNING_SYSHDR)
6164 global_dc->dc_warn_system_headers = save_warn_system_headers;
6165 return ret;
6168 /* Convert a character from the host to the target execution character
6169 set. cpplib handles this, mostly. */
6171 HOST_WIDE_INT
6172 c_common_to_target_charset (HOST_WIDE_INT c)
6174 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6175 zero-extended under -fno-signed-char. cpplib insists that characters
6176 and character constants are always unsigned. Hence we must convert
6177 back and forth. */
6178 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6180 uc = cpp_host_to_exec_charset (parse_in, uc);
6182 if (flag_signed_char)
6183 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6184 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6185 else
6186 return uc;
6189 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6190 references with an INDIRECT_REF of a constant at the bottom; much like the
6191 traditional rendering of offsetof as a macro. Return the folded result. */
6193 tree
6194 fold_offsetof_1 (tree expr, enum tree_code ctx)
6196 tree base, off, t;
6197 tree_code code = TREE_CODE (expr);
6198 switch (code)
6200 case ERROR_MARK:
6201 return expr;
6203 case VAR_DECL:
6204 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6205 return error_mark_node;
6207 case CALL_EXPR:
6208 case TARGET_EXPR:
6209 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6210 return error_mark_node;
6212 case NOP_EXPR:
6213 case INDIRECT_REF:
6214 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6216 error ("cannot apply %<offsetof%> to a non constant address");
6217 return error_mark_node;
6219 return TREE_OPERAND (expr, 0);
6221 case COMPONENT_REF:
6222 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6223 if (base == error_mark_node)
6224 return base;
6226 t = TREE_OPERAND (expr, 1);
6227 if (DECL_C_BIT_FIELD (t))
6229 error ("attempt to take address of bit-field structure "
6230 "member %qD", t);
6231 return error_mark_node;
6233 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
6234 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6235 / BITS_PER_UNIT));
6236 break;
6238 case ARRAY_REF:
6239 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
6240 if (base == error_mark_node)
6241 return base;
6243 t = TREE_OPERAND (expr, 1);
6245 /* Check if the offset goes beyond the upper bound of the array. */
6246 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
6248 tree upbound = array_ref_up_bound (expr);
6249 if (upbound != NULL_TREE
6250 && TREE_CODE (upbound) == INTEGER_CST
6251 && !tree_int_cst_equal (upbound,
6252 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6254 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6255 upbound = size_binop (PLUS_EXPR, upbound,
6256 build_int_cst (TREE_TYPE (upbound), 1));
6257 if (tree_int_cst_lt (upbound, t))
6259 tree v;
6261 for (v = TREE_OPERAND (expr, 0);
6262 TREE_CODE (v) == COMPONENT_REF;
6263 v = TREE_OPERAND (v, 0))
6264 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6265 == RECORD_TYPE)
6267 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6268 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
6269 if (TREE_CODE (fld_chain) == FIELD_DECL)
6270 break;
6272 if (fld_chain)
6273 break;
6275 /* Don't warn if the array might be considered a poor
6276 man's flexible array member with a very permissive
6277 definition thereof. */
6278 if (TREE_CODE (v) == ARRAY_REF
6279 || TREE_CODE (v) == COMPONENT_REF)
6280 warning (OPT_Warray_bounds,
6281 "index %E denotes an offset "
6282 "greater than size of %qT",
6283 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6288 t = convert (sizetype, t);
6289 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
6290 break;
6292 case COMPOUND_EXPR:
6293 /* Handle static members of volatile structs. */
6294 t = TREE_OPERAND (expr, 1);
6295 gcc_assert (VAR_P (t));
6296 return fold_offsetof_1 (t);
6298 default:
6299 gcc_unreachable ();
6302 return fold_build_pointer_plus (base, off);
6305 /* Likewise, but convert it to the return type of offsetof. */
6307 tree
6308 fold_offsetof (tree expr)
6310 return convert (size_type_node, fold_offsetof_1 (expr));
6314 /* *PTYPE is an incomplete array. Complete it with a domain based on
6315 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6316 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6317 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6320 complete_array_type (tree *ptype, tree initial_value, bool do_default)
6322 tree maxindex, type, main_type, elt, unqual_elt;
6323 int failure = 0, quals;
6324 hashval_t hashcode = 0;
6325 bool overflow_p = false;
6327 maxindex = size_zero_node;
6328 if (initial_value)
6330 if (TREE_CODE (initial_value) == STRING_CST)
6332 int eltsize
6333 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6334 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6336 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6338 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
6340 if (vec_safe_is_empty (v))
6342 if (pedantic)
6343 failure = 3;
6344 maxindex = ssize_int (-1);
6346 else
6348 tree curindex;
6349 unsigned HOST_WIDE_INT cnt;
6350 constructor_elt *ce;
6351 bool fold_p = false;
6353 if ((*v)[0].index)
6354 maxindex = (*v)[0].index, fold_p = true;
6356 curindex = maxindex;
6358 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
6360 bool curfold_p = false;
6361 if (ce->index)
6362 curindex = ce->index, curfold_p = true;
6363 else
6365 if (fold_p)
6367 /* Since we treat size types now as ordinary
6368 unsigned types, we need an explicit overflow
6369 check. */
6370 tree orig = curindex;
6371 curindex = fold_convert (sizetype, curindex);
6372 overflow_p |= tree_int_cst_lt (curindex, orig);
6374 curindex = size_binop (PLUS_EXPR, curindex,
6375 size_one_node);
6377 if (tree_int_cst_lt (maxindex, curindex))
6378 maxindex = curindex, fold_p = curfold_p;
6380 if (fold_p)
6382 tree orig = maxindex;
6383 maxindex = fold_convert (sizetype, maxindex);
6384 overflow_p |= tree_int_cst_lt (maxindex, orig);
6388 else
6390 /* Make an error message unless that happened already. */
6391 if (initial_value != error_mark_node)
6392 failure = 1;
6395 else
6397 failure = 2;
6398 if (!do_default)
6399 return failure;
6402 type = *ptype;
6403 elt = TREE_TYPE (type);
6404 quals = TYPE_QUALS (strip_array_types (elt));
6405 if (quals == 0)
6406 unqual_elt = elt;
6407 else
6408 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
6410 /* Using build_distinct_type_copy and modifying things afterward instead
6411 of using build_array_type to create a new type preserves all of the
6412 TYPE_LANG_FLAG_? bits that the front end may have set. */
6413 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6414 TREE_TYPE (main_type) = unqual_elt;
6415 TYPE_DOMAIN (main_type)
6416 = build_range_type (TREE_TYPE (maxindex),
6417 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6418 layout_type (main_type);
6420 /* Make sure we have the canonical MAIN_TYPE. */
6421 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
6422 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6423 hashcode);
6424 main_type = type_hash_canon (hashcode, main_type);
6426 /* Fix the canonical type. */
6427 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6428 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6429 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6430 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6431 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6432 != TYPE_DOMAIN (main_type)))
6433 TYPE_CANONICAL (main_type)
6434 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6435 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
6436 else
6437 TYPE_CANONICAL (main_type) = main_type;
6439 if (quals == 0)
6440 type = main_type;
6441 else
6442 type = c_build_qualified_type (main_type, quals);
6444 if (COMPLETE_TYPE_P (type)
6445 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6446 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
6448 error ("size of array is too large");
6449 /* If we proceed with the array type as it is, we'll eventually
6450 crash in tree_to_[su]hwi(). */
6451 type = error_mark_node;
6454 *ptype = type;
6455 return failure;
6458 /* Like c_mark_addressable but don't check register qualifier. */
6459 void
6460 c_common_mark_addressable_vec (tree t)
6462 while (handled_component_p (t))
6463 t = TREE_OPERAND (t, 0);
6464 if (!VAR_P (t)
6465 && TREE_CODE (t) != PARM_DECL
6466 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
6467 return;
6468 TREE_ADDRESSABLE (t) = 1;
6473 /* Used to help initialize the builtin-types.def table. When a type of
6474 the correct size doesn't exist, use error_mark_node instead of NULL.
6475 The later results in segfaults even when a decl using the type doesn't
6476 get invoked. */
6478 tree
6479 builtin_type_for_size (int size, bool unsignedp)
6481 tree type = c_common_type_for_size (size, unsignedp);
6482 return type ? type : error_mark_node;
6485 /* A helper function for resolve_overloaded_builtin in resolving the
6486 overloaded __sync_ builtins. Returns a positive power of 2 if the
6487 first operand of PARAMS is a pointer to a supported data type.
6488 Returns 0 if an error is encountered.
6489 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6490 built-ins. */
6492 static int
6493 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
6495 /* Type of the argument. */
6496 tree argtype;
6497 /* Type the argument points to. */
6498 tree type;
6499 int size;
6501 if (vec_safe_is_empty (params))
6503 error ("too few arguments to function %qE", function);
6504 return 0;
6507 argtype = type = TREE_TYPE ((*params)[0]);
6508 if (TREE_CODE (type) == ARRAY_TYPE)
6510 /* Force array-to-pointer decay for C++. */
6511 gcc_assert (c_dialect_cxx());
6512 (*params)[0] = default_conversion ((*params)[0]);
6513 type = TREE_TYPE ((*params)[0]);
6515 if (TREE_CODE (type) != POINTER_TYPE)
6516 goto incompatible;
6518 type = TREE_TYPE (type);
6519 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6520 goto incompatible;
6522 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6523 goto incompatible;
6525 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6526 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6527 return size;
6529 incompatible:
6530 /* Issue the diagnostic only if the argument is valid, otherwise
6531 it would be redundant at best and could be misleading. */
6532 if (argtype != error_mark_node)
6533 error ("operand type %qT is incompatible with argument %d of %qE",
6534 argtype, 1, function);
6535 return 0;
6538 /* A helper function for resolve_overloaded_builtin. Adds casts to
6539 PARAMS to make arguments match up with those of FUNCTION. Drops
6540 the variadic arguments at the end. Returns false if some error
6541 was encountered; true on success. */
6543 static bool
6544 sync_resolve_params (location_t loc, tree orig_function, tree function,
6545 vec<tree, va_gc> *params, bool orig_format)
6547 function_args_iterator iter;
6548 tree ptype;
6549 unsigned int parmnum;
6551 function_args_iter_init (&iter, TREE_TYPE (function));
6552 /* We've declared the implementation functions to use "volatile void *"
6553 as the pointer parameter, so we shouldn't get any complaints from the
6554 call to check_function_arguments what ever type the user used. */
6555 function_args_iter_next (&iter);
6556 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
6557 ptype = TYPE_MAIN_VARIANT (ptype);
6559 /* For the rest of the values, we need to cast these to FTYPE, so that we
6560 don't get warnings for passing pointer types, etc. */
6561 parmnum = 0;
6562 while (1)
6564 tree val, arg_type;
6566 arg_type = function_args_iter_cond (&iter);
6567 /* XXX void_type_node belies the abstraction. */
6568 if (arg_type == void_type_node)
6569 break;
6571 ++parmnum;
6572 if (params->length () <= parmnum)
6574 error_at (loc, "too few arguments to function %qE", orig_function);
6575 return false;
6578 /* Only convert parameters if arg_type is unsigned integer type with
6579 new format sync routines, i.e. don't attempt to convert pointer
6580 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6581 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6582 kinds). */
6583 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
6585 /* Ideally for the first conversion we'd use convert_for_assignment
6586 so that we get warnings for anything that doesn't match the pointer
6587 type. This isn't portable across the C and C++ front ends atm. */
6588 val = (*params)[parmnum];
6589 val = convert (ptype, val);
6590 val = convert (arg_type, val);
6591 (*params)[parmnum] = val;
6594 function_args_iter_next (&iter);
6597 /* __atomic routines are not variadic. */
6598 if (!orig_format && params->length () != parmnum + 1)
6600 error_at (loc, "too many arguments to function %qE", orig_function);
6601 return false;
6604 /* The definition of these primitives is variadic, with the remaining
6605 being "an optional list of variables protected by the memory barrier".
6606 No clue what that's supposed to mean, precisely, but we consider all
6607 call-clobbered variables to be protected so we're safe. */
6608 params->truncate (parmnum + 1);
6610 return true;
6613 /* A helper function for resolve_overloaded_builtin. Adds a cast to
6614 RESULT to make it match the type of the first pointer argument in
6615 PARAMS. */
6617 static tree
6618 sync_resolve_return (tree first_param, tree result, bool orig_format)
6620 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
6621 tree rtype = TREE_TYPE (result);
6622 ptype = TYPE_MAIN_VARIANT (ptype);
6624 /* New format doesn't require casting unless the types are the same size. */
6625 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6626 return convert (ptype, result);
6627 else
6628 return result;
6631 /* This function verifies the PARAMS to generic atomic FUNCTION.
6632 It returns the size if all the parameters are the same size, otherwise
6633 0 is returned if the parameters are invalid. */
6635 static int
6636 get_atomic_generic_size (location_t loc, tree function,
6637 vec<tree, va_gc> *params)
6639 unsigned int n_param;
6640 unsigned int n_model;
6641 unsigned int x;
6642 int size_0;
6643 tree type_0;
6645 /* Determine the parameter makeup. */
6646 switch (DECL_FUNCTION_CODE (function))
6648 case BUILT_IN_ATOMIC_EXCHANGE:
6649 n_param = 4;
6650 n_model = 1;
6651 break;
6652 case BUILT_IN_ATOMIC_LOAD:
6653 case BUILT_IN_ATOMIC_STORE:
6654 n_param = 3;
6655 n_model = 1;
6656 break;
6657 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6658 n_param = 6;
6659 n_model = 2;
6660 break;
6661 default:
6662 gcc_unreachable ();
6665 if (vec_safe_length (params) != n_param)
6667 error_at (loc, "incorrect number of arguments to function %qE", function);
6668 return 0;
6671 /* Get type of first parameter, and determine its size. */
6672 type_0 = TREE_TYPE ((*params)[0]);
6673 if (TREE_CODE (type_0) == ARRAY_TYPE)
6675 /* Force array-to-pointer decay for C++. */
6676 gcc_assert (c_dialect_cxx());
6677 (*params)[0] = default_conversion ((*params)[0]);
6678 type_0 = TREE_TYPE ((*params)[0]);
6680 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6682 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6683 function);
6684 return 0;
6687 /* Types must be compile time constant sizes. */
6688 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
6690 error_at (loc,
6691 "argument 1 of %qE must be a pointer to a constant size type",
6692 function);
6693 return 0;
6696 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
6698 /* Zero size objects are not allowed. */
6699 if (size_0 == 0)
6701 error_at (loc,
6702 "argument 1 of %qE must be a pointer to a nonzero size object",
6703 function);
6704 return 0;
6707 /* Check each other parameter is a pointer and the same size. */
6708 for (x = 0; x < n_param - n_model; x++)
6710 int size;
6711 tree type = TREE_TYPE ((*params)[x]);
6712 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
6713 if (n_param == 6 && x == 3)
6714 continue;
6715 if (!POINTER_TYPE_P (type))
6717 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6718 function);
6719 return 0;
6721 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6722 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6723 != INTEGER_CST)
6725 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6726 "size type", x + 1, function);
6727 return 0;
6729 else if (FUNCTION_POINTER_TYPE_P (type))
6731 error_at (loc, "argument %d of %qE must not be a pointer to a "
6732 "function", x + 1, function);
6733 return 0;
6735 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6736 size = type_size ? tree_to_uhwi (type_size) : 0;
6737 if (size != size_0)
6739 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6740 function);
6741 return 0;
6745 /* Check memory model parameters for validity. */
6746 for (x = n_param - n_model ; x < n_param; x++)
6748 tree p = (*params)[x];
6749 if (TREE_CODE (p) == INTEGER_CST)
6751 int i = tree_to_uhwi (p);
6752 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
6754 warning_at (loc, OPT_Winvalid_memory_model,
6755 "invalid memory model argument %d of %qE", x + 1,
6756 function);
6759 else
6760 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6762 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6763 function);
6764 return 0;
6768 return size_0;
6772 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6773 at the beginning of the parameter list PARAMS representing the size of the
6774 objects. This is to match the library ABI requirement. LOC is the location
6775 of the function call.
6776 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6777 returned to allow the external call to be constructed. */
6779 static tree
6780 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
6781 vec<tree, va_gc> *params)
6783 tree size_node;
6785 /* Insert a SIZE_T parameter as the first param. If there isn't
6786 enough space, allocate a new vector and recursively re-build with that. */
6787 if (!params->space (1))
6789 unsigned int z, len;
6790 vec<tree, va_gc> *v;
6791 tree f;
6793 len = params->length ();
6794 vec_alloc (v, len + 1);
6795 v->quick_push (build_int_cst (size_type_node, n));
6796 for (z = 0; z < len; z++)
6797 v->quick_push ((*params)[z]);
6798 f = build_function_call_vec (loc, vNULL, function, v, NULL);
6799 vec_free (v);
6800 return f;
6803 /* Add the size parameter and leave as a function call for processing. */
6804 size_node = build_int_cst (size_type_node, n);
6805 params->quick_insert (0, size_node);
6806 return NULL_TREE;
6810 /* Return whether atomic operations for naturally aligned N-byte
6811 arguments are supported, whether inline or through libatomic. */
6812 static bool
6813 atomic_size_supported_p (int n)
6815 switch (n)
6817 case 1:
6818 case 2:
6819 case 4:
6820 case 8:
6821 return true;
6823 case 16:
6824 return targetm.scalar_mode_supported_p (TImode);
6826 default:
6827 return false;
6831 /* This will process an __atomic_exchange function call, determine whether it
6832 needs to be mapped to the _N variation, or turned into a library call.
6833 LOC is the location of the builtin call.
6834 FUNCTION is the DECL that has been invoked;
6835 PARAMS is the argument list for the call. The return value is non-null
6836 TRUE is returned if it is translated into the proper format for a call to the
6837 external library, and NEW_RETURN is set the tree for that function.
6838 FALSE is returned if processing for the _N variation is required, and
6839 NEW_RETURN is set to the return value the result is copied into. */
6840 static bool
6841 resolve_overloaded_atomic_exchange (location_t loc, tree function,
6842 vec<tree, va_gc> *params, tree *new_return)
6844 tree p0, p1, p2, p3;
6845 tree I_type, I_type_ptr;
6846 int n = get_atomic_generic_size (loc, function, params);
6848 /* Size of 0 is an error condition. */
6849 if (n == 0)
6851 *new_return = error_mark_node;
6852 return true;
6855 /* If not a lock-free size, change to the library generic format. */
6856 if (!atomic_size_supported_p (n))
6858 *new_return = add_atomic_size_parameter (n, loc, function, params);
6859 return true;
6862 /* Otherwise there is a lockfree match, transform the call from:
6863 void fn(T* mem, T* desired, T* return, model)
6864 into
6865 *return = (T) (fn (In* mem, (In) *desired, model)) */
6867 p0 = (*params)[0];
6868 p1 = (*params)[1];
6869 p2 = (*params)[2];
6870 p3 = (*params)[3];
6872 /* Create pointer to appropriate size. */
6873 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6874 I_type_ptr = build_pointer_type (I_type);
6876 /* Convert object pointer to required type. */
6877 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6878 (*params)[0] = p0;
6879 /* Convert new value to required type, and dereference it. */
6880 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
6881 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
6882 (*params)[1] = p1;
6884 /* Move memory model to the 3rd position, and end param list. */
6885 (*params)[2] = p3;
6886 params->truncate (3);
6888 /* Convert return pointer and dereference it for later assignment. */
6889 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6891 return false;
6895 /* This will process an __atomic_compare_exchange function call, determine
6896 whether it needs to be mapped to the _N variation, or turned into a lib call.
6897 LOC is the location of the builtin call.
6898 FUNCTION is the DECL that has been invoked;
6899 PARAMS is the argument list for the call. The return value is non-null
6900 TRUE is returned if it is translated into the proper format for a call to the
6901 external library, and NEW_RETURN is set the tree for that function.
6902 FALSE is returned if processing for the _N variation is required. */
6904 static bool
6905 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
6906 vec<tree, va_gc> *params,
6907 tree *new_return)
6909 tree p0, p1, p2;
6910 tree I_type, I_type_ptr;
6911 int n = get_atomic_generic_size (loc, function, params);
6913 /* Size of 0 is an error condition. */
6914 if (n == 0)
6916 *new_return = error_mark_node;
6917 return true;
6920 /* If not a lock-free size, change to the library generic format. */
6921 if (!atomic_size_supported_p (n))
6923 /* The library generic format does not have the weak parameter, so
6924 remove it from the param list. Since a parameter has been removed,
6925 we can be sure that there is room for the SIZE_T parameter, meaning
6926 there will not be a recursive rebuilding of the parameter list, so
6927 there is no danger this will be done twice. */
6928 if (n > 0)
6930 (*params)[3] = (*params)[4];
6931 (*params)[4] = (*params)[5];
6932 params->truncate (5);
6934 *new_return = add_atomic_size_parameter (n, loc, function, params);
6935 return true;
6938 /* Otherwise, there is a match, so the call needs to be transformed from:
6939 bool fn(T* mem, T* desired, T* return, weak, success, failure)
6940 into
6941 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
6943 p0 = (*params)[0];
6944 p1 = (*params)[1];
6945 p2 = (*params)[2];
6947 /* Create pointer to appropriate size. */
6948 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
6949 I_type_ptr = build_pointer_type (I_type);
6951 /* Convert object pointer to required type. */
6952 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
6953 (*params)[0] = p0;
6955 /* Convert expected pointer to required type. */
6956 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
6957 (*params)[1] = p1;
6959 /* Convert desired value to required type, and dereference it. */
6960 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
6961 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
6962 (*params)[2] = p2;
6964 /* The rest of the parameters are fine. NULL means no special return value
6965 processing.*/
6966 *new_return = NULL;
6967 return false;
6971 /* This will process an __atomic_load function call, determine whether it
6972 needs to be mapped to the _N variation, or turned into a library call.
6973 LOC is the location of the builtin call.
6974 FUNCTION is the DECL that has been invoked;
6975 PARAMS is the argument list for the call. The return value is non-null
6976 TRUE is returned if it is translated into the proper format for a call to the
6977 external library, and NEW_RETURN is set the tree for that function.
6978 FALSE is returned if processing for the _N variation is required, and
6979 NEW_RETURN is set to the return value the result is copied into. */
6981 static bool
6982 resolve_overloaded_atomic_load (location_t loc, tree function,
6983 vec<tree, va_gc> *params, tree *new_return)
6985 tree p0, p1, p2;
6986 tree I_type, I_type_ptr;
6987 int n = get_atomic_generic_size (loc, function, params);
6989 /* Size of 0 is an error condition. */
6990 if (n == 0)
6992 *new_return = error_mark_node;
6993 return true;
6996 /* If not a lock-free size, change to the library generic format. */
6997 if (!atomic_size_supported_p (n))
6999 *new_return = add_atomic_size_parameter (n, loc, function, params);
7000 return true;
7003 /* Otherwise, there is a match, so the call needs to be transformed from:
7004 void fn(T* mem, T* return, model)
7005 into
7006 *return = (T) (fn ((In *) mem, model)) */
7008 p0 = (*params)[0];
7009 p1 = (*params)[1];
7010 p2 = (*params)[2];
7012 /* Create pointer to appropriate size. */
7013 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7014 I_type_ptr = build_pointer_type (I_type);
7016 /* Convert object pointer to required type. */
7017 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7018 (*params)[0] = p0;
7020 /* Move memory model to the 2nd position, and end param list. */
7021 (*params)[1] = p2;
7022 params->truncate (2);
7024 /* Convert return pointer and dereference it for later assignment. */
7025 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7027 return false;
7031 /* This will process an __atomic_store function call, determine whether it
7032 needs to be mapped to the _N variation, or turned into a library call.
7033 LOC is the location of the builtin call.
7034 FUNCTION is the DECL that has been invoked;
7035 PARAMS is the argument list for the call. The return value is non-null
7036 TRUE is returned if it is translated into the proper format for a call to the
7037 external library, and NEW_RETURN is set the tree for that function.
7038 FALSE is returned if processing for the _N variation is required, and
7039 NEW_RETURN is set to the return value the result is copied into. */
7041 static bool
7042 resolve_overloaded_atomic_store (location_t loc, tree function,
7043 vec<tree, va_gc> *params, tree *new_return)
7045 tree p0, p1;
7046 tree I_type, I_type_ptr;
7047 int n = get_atomic_generic_size (loc, function, params);
7049 /* Size of 0 is an error condition. */
7050 if (n == 0)
7052 *new_return = error_mark_node;
7053 return true;
7056 /* If not a lock-free size, change to the library generic format. */
7057 if (!atomic_size_supported_p (n))
7059 *new_return = add_atomic_size_parameter (n, loc, function, params);
7060 return true;
7063 /* Otherwise, there is a match, so the call needs to be transformed from:
7064 void fn(T* mem, T* value, model)
7065 into
7066 fn ((In *) mem, (In) *value, model) */
7068 p0 = (*params)[0];
7069 p1 = (*params)[1];
7071 /* Create pointer to appropriate size. */
7072 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7073 I_type_ptr = build_pointer_type (I_type);
7075 /* Convert object pointer to required type. */
7076 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
7077 (*params)[0] = p0;
7079 /* Convert new value to required type, and dereference it. */
7080 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7081 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
7082 (*params)[1] = p1;
7084 /* The memory model is in the right spot already. Return is void. */
7085 *new_return = NULL_TREE;
7087 return false;
7091 /* Some builtin functions are placeholders for other expressions. This
7092 function should be called immediately after parsing the call expression
7093 before surrounding code has committed to the type of the expression.
7095 LOC is the location of the builtin call.
7097 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7098 PARAMS is the argument list for the call. The return value is non-null
7099 when expansion is complete, and null if normal processing should
7100 continue. */
7102 tree
7103 resolve_overloaded_builtin (location_t loc, tree function,
7104 vec<tree, va_gc> *params)
7106 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7108 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7109 Those are not valid to call with a pointer to _Bool (or C++ bool)
7110 and so must be rejected. */
7111 bool fetch_op = true;
7112 bool orig_format = true;
7113 tree new_return = NULL_TREE;
7115 switch (DECL_BUILT_IN_CLASS (function))
7117 case BUILT_IN_NORMAL:
7118 break;
7119 case BUILT_IN_MD:
7120 if (targetm.resolve_overloaded_builtin)
7121 return targetm.resolve_overloaded_builtin (loc, function, params);
7122 else
7123 return NULL_TREE;
7124 default:
7125 return NULL_TREE;
7128 /* Handle BUILT_IN_NORMAL here. */
7129 switch (orig_code)
7131 case BUILT_IN_ATOMIC_EXCHANGE:
7132 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7133 case BUILT_IN_ATOMIC_LOAD:
7134 case BUILT_IN_ATOMIC_STORE:
7136 /* Handle these 4 together so that they can fall through to the next
7137 case if the call is transformed to an _N variant. */
7138 switch (orig_code)
7140 case BUILT_IN_ATOMIC_EXCHANGE:
7142 if (resolve_overloaded_atomic_exchange (loc, function, params,
7143 &new_return))
7144 return new_return;
7145 /* Change to the _N variant. */
7146 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7147 break;
7150 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7152 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7153 params,
7154 &new_return))
7155 return new_return;
7156 /* Change to the _N variant. */
7157 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7158 break;
7160 case BUILT_IN_ATOMIC_LOAD:
7162 if (resolve_overloaded_atomic_load (loc, function, params,
7163 &new_return))
7164 return new_return;
7165 /* Change to the _N variant. */
7166 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7167 break;
7169 case BUILT_IN_ATOMIC_STORE:
7171 if (resolve_overloaded_atomic_store (loc, function, params,
7172 &new_return))
7173 return new_return;
7174 /* Change to the _N variant. */
7175 orig_code = BUILT_IN_ATOMIC_STORE_N;
7176 break;
7178 default:
7179 gcc_unreachable ();
7182 /* FALLTHRU */
7183 case BUILT_IN_ATOMIC_EXCHANGE_N:
7184 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7185 case BUILT_IN_ATOMIC_LOAD_N:
7186 case BUILT_IN_ATOMIC_STORE_N:
7187 fetch_op = false;
7188 /* FALLTHRU */
7189 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7190 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7191 case BUILT_IN_ATOMIC_AND_FETCH_N:
7192 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7193 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7194 case BUILT_IN_ATOMIC_OR_FETCH_N:
7195 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7196 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7197 case BUILT_IN_ATOMIC_FETCH_AND_N:
7198 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7199 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7200 case BUILT_IN_ATOMIC_FETCH_OR_N:
7201 orig_format = false;
7202 /* FALLTHRU */
7203 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7204 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7205 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7206 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7207 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7208 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7209 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7210 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7211 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7212 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7213 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7214 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7215 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7216 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7217 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7218 case BUILT_IN_SYNC_LOCK_RELEASE_N:
7220 /* The following are not _FETCH_OPs and must be accepted with
7221 pointers to _Bool (or C++ bool). */
7222 if (fetch_op)
7223 fetch_op =
7224 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7225 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7226 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7227 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7229 int n = sync_resolve_size (function, params, fetch_op);
7230 tree new_function, first_param, result;
7231 enum built_in_function fncode;
7233 if (n == 0)
7234 return error_mark_node;
7236 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7237 new_function = builtin_decl_explicit (fncode);
7238 if (!sync_resolve_params (loc, function, new_function, params,
7239 orig_format))
7240 return error_mark_node;
7242 first_param = (*params)[0];
7243 result = build_function_call_vec (loc, vNULL, new_function, params,
7244 NULL);
7245 if (result == error_mark_node)
7246 return result;
7247 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7248 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
7249 && orig_code != BUILT_IN_ATOMIC_STORE_N
7250 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
7251 result = sync_resolve_return (first_param, result, orig_format);
7253 if (fetch_op)
7254 /* Prevent -Wunused-value warning. */
7255 TREE_USED (result) = true;
7257 /* If new_return is set, assign function to that expr and cast the
7258 result to void since the generic interface returned void. */
7259 if (new_return)
7261 /* Cast function result from I{1,2,4,8,16} to the required type. */
7262 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7263 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7264 result);
7265 TREE_SIDE_EFFECTS (result) = 1;
7266 protected_set_expr_location (result, loc);
7267 result = convert (void_type_node, result);
7269 return result;
7272 default:
7273 return NULL_TREE;
7277 /* vector_types_compatible_elements_p is used in type checks of vectors
7278 values used as operands of binary operators. Where it returns true, and
7279 the other checks of the caller succeed (being vector types in he first
7280 place, and matching number of elements), we can just treat the types
7281 as essentially the same.
7282 Contrast with vector_targets_convertible_p, which is used for vector
7283 pointer types, and vector_types_convertible_p, which will allow
7284 language-specific matches under the control of flag_lax_vector_conversions,
7285 and might still require a conversion. */
7286 /* True if vector types T1 and T2 can be inputs to the same binary
7287 operator without conversion.
7288 We don't check the overall vector size here because some of our callers
7289 want to give different error messages when the vectors are compatible
7290 except for the element count. */
7292 bool
7293 vector_types_compatible_elements_p (tree t1, tree t2)
7295 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7296 t1 = TREE_TYPE (t1);
7297 t2 = TREE_TYPE (t2);
7299 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7301 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
7302 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
7303 || c2 == FIXED_POINT_TYPE));
7305 t1 = c_common_signed_type (t1);
7306 t2 = c_common_signed_type (t2);
7307 /* Equality works here because c_common_signed_type uses
7308 TYPE_MAIN_VARIANT. */
7309 if (t1 == t2)
7310 return true;
7311 if (opaque && c1 == c2
7312 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
7313 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7314 return true;
7315 return false;
7318 /* Check for missing format attributes on function pointers. LTYPE is
7319 the new type or left-hand side type. RTYPE is the old type or
7320 right-hand side type. Returns TRUE if LTYPE is missing the desired
7321 attribute. */
7323 bool
7324 check_missing_format_attribute (tree ltype, tree rtype)
7326 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7327 tree ra;
7329 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7330 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7331 break;
7332 if (ra)
7334 tree la;
7335 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7336 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7337 break;
7338 return !la;
7340 else
7341 return false;
7344 /* Setup a TYPE_DECL node as a typedef representation.
7346 X is a TYPE_DECL for a typedef statement. Create a brand new
7347 ..._TYPE node (which will be just a variant of the existing
7348 ..._TYPE node with identical properties) and then install X
7349 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7351 The whole point here is to end up with a situation where each
7352 and every ..._TYPE node the compiler creates will be uniquely
7353 associated with AT MOST one node representing a typedef name.
7354 This way, even though the compiler substitutes corresponding
7355 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7356 early on, later parts of the compiler can always do the reverse
7357 translation and get back the corresponding typedef name. For
7358 example, given:
7360 typedef struct S MY_TYPE;
7361 MY_TYPE object;
7363 Later parts of the compiler might only know that `object' was of
7364 type `struct S' if it were not for code just below. With this
7365 code however, later parts of the compiler see something like:
7367 struct S' == struct S
7368 typedef struct S' MY_TYPE;
7369 struct S' object;
7371 And they can then deduce (from the node for type struct S') that
7372 the original object declaration was:
7374 MY_TYPE object;
7376 Being able to do this is important for proper support of protoize,
7377 and also for generating precise symbolic debugging information
7378 which takes full account of the programmer's (typedef) vocabulary.
7380 Obviously, we don't want to generate a duplicate ..._TYPE node if
7381 the TYPE_DECL node that we are now processing really represents a
7382 standard built-in type. */
7384 void
7385 set_underlying_type (tree x)
7387 if (x == error_mark_node)
7388 return;
7389 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
7391 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7392 TYPE_NAME (TREE_TYPE (x)) = x;
7394 else if (TREE_TYPE (x) != error_mark_node
7395 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
7397 tree tt = TREE_TYPE (x);
7398 DECL_ORIGINAL_TYPE (x) = tt;
7399 tt = build_variant_type_copy (tt);
7400 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7401 TYPE_NAME (tt) = x;
7402 TREE_USED (tt) = TREE_USED (x);
7403 TREE_TYPE (x) = tt;
7407 /* Record the types used by the current global variable declaration
7408 being parsed, so that we can decide later to emit their debug info.
7409 Those types are in types_used_by_cur_var_decl, and we are going to
7410 store them in the types_used_by_vars_hash hash table.
7411 DECL is the declaration of the global variable that has been parsed. */
7413 void
7414 record_types_used_by_current_var_decl (tree decl)
7416 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7418 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
7420 tree type = types_used_by_cur_var_decl->pop ();
7421 types_used_by_var_decl_insert (type, decl);
7425 /* The C and C++ parsers both use vectors to hold function arguments.
7426 For efficiency, we keep a cache of unused vectors. This is the
7427 cache. */
7429 typedef vec<tree, va_gc> *tree_gc_vec;
7430 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
7432 /* Return a new vector from the cache. If the cache is empty,
7433 allocate a new vector. These vectors are GC'ed, so it is OK if the
7434 pointer is not released.. */
7436 vec<tree, va_gc> *
7437 make_tree_vector (void)
7439 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7440 return tree_vector_cache->pop ();
7441 else
7443 /* Passing 0 to vec::alloc returns NULL, and our callers require
7444 that we always return a non-NULL value. The vector code uses
7445 4 when growing a NULL vector, so we do too. */
7446 vec<tree, va_gc> *v;
7447 vec_alloc (v, 4);
7448 return v;
7452 /* Release a vector of trees back to the cache. */
7454 void
7455 release_tree_vector (vec<tree, va_gc> *vec)
7457 if (vec != NULL)
7459 vec->truncate (0);
7460 vec_safe_push (tree_vector_cache, vec);
7464 /* Get a new tree vector holding a single tree. */
7466 vec<tree, va_gc> *
7467 make_tree_vector_single (tree t)
7469 vec<tree, va_gc> *ret = make_tree_vector ();
7470 ret->quick_push (t);
7471 return ret;
7474 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7476 vec<tree, va_gc> *
7477 make_tree_vector_from_list (tree list)
7479 vec<tree, va_gc> *ret = make_tree_vector ();
7480 for (; list; list = TREE_CHAIN (list))
7481 vec_safe_push (ret, TREE_VALUE (list));
7482 return ret;
7485 /* Get a new tree vector of the values of a CONSTRUCTOR. */
7487 vec<tree, va_gc> *
7488 make_tree_vector_from_ctor (tree ctor)
7490 vec<tree,va_gc> *ret = make_tree_vector ();
7491 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7492 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7493 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7494 return ret;
7497 /* Get a new tree vector which is a copy of an existing one. */
7499 vec<tree, va_gc> *
7500 make_tree_vector_copy (const vec<tree, va_gc> *orig)
7502 vec<tree, va_gc> *ret;
7503 unsigned int ix;
7504 tree t;
7506 ret = make_tree_vector ();
7507 vec_safe_reserve (ret, vec_safe_length (orig));
7508 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7509 ret->quick_push (t);
7510 return ret;
7513 /* Return true if KEYWORD starts a type specifier. */
7515 bool
7516 keyword_begins_type_specifier (enum rid keyword)
7518 switch (keyword)
7520 case RID_AUTO_TYPE:
7521 case RID_INT:
7522 case RID_CHAR:
7523 case RID_FLOAT:
7524 case RID_DOUBLE:
7525 case RID_VOID:
7526 case RID_UNSIGNED:
7527 case RID_LONG:
7528 case RID_SHORT:
7529 case RID_SIGNED:
7530 CASE_RID_FLOATN_NX:
7531 case RID_DFLOAT32:
7532 case RID_DFLOAT64:
7533 case RID_DFLOAT128:
7534 case RID_FRACT:
7535 case RID_ACCUM:
7536 case RID_BOOL:
7537 case RID_WCHAR:
7538 case RID_CHAR16:
7539 case RID_CHAR32:
7540 case RID_SAT:
7541 case RID_COMPLEX:
7542 case RID_TYPEOF:
7543 case RID_STRUCT:
7544 case RID_CLASS:
7545 case RID_UNION:
7546 case RID_ENUM:
7547 return true;
7548 default:
7549 if (keyword >= RID_FIRST_INT_N
7550 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7551 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7552 return true;
7553 return false;
7557 /* Return true if KEYWORD names a type qualifier. */
7559 bool
7560 keyword_is_type_qualifier (enum rid keyword)
7562 switch (keyword)
7564 case RID_CONST:
7565 case RID_VOLATILE:
7566 case RID_RESTRICT:
7567 case RID_ATOMIC:
7568 return true;
7569 default:
7570 return false;
7574 /* Return true if KEYWORD names a storage class specifier.
7576 RID_TYPEDEF is not included in this list despite `typedef' being
7577 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7578 such for syntactic convenience only. */
7580 bool
7581 keyword_is_storage_class_specifier (enum rid keyword)
7583 switch (keyword)
7585 case RID_STATIC:
7586 case RID_EXTERN:
7587 case RID_REGISTER:
7588 case RID_AUTO:
7589 case RID_MUTABLE:
7590 case RID_THREAD:
7591 return true;
7592 default:
7593 return false;
7597 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7599 static bool
7600 keyword_is_function_specifier (enum rid keyword)
7602 switch (keyword)
7604 case RID_INLINE:
7605 case RID_NORETURN:
7606 case RID_VIRTUAL:
7607 case RID_EXPLICIT:
7608 return true;
7609 default:
7610 return false;
7614 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7615 declaration-specifier (C99 6.7). */
7617 bool
7618 keyword_is_decl_specifier (enum rid keyword)
7620 if (keyword_is_storage_class_specifier (keyword)
7621 || keyword_is_type_qualifier (keyword)
7622 || keyword_is_function_specifier (keyword))
7623 return true;
7625 switch (keyword)
7627 case RID_TYPEDEF:
7628 case RID_FRIEND:
7629 case RID_CONSTEXPR:
7630 return true;
7631 default:
7632 return false;
7636 /* Initialize language-specific-bits of tree_contains_struct. */
7638 void
7639 c_common_init_ts (void)
7641 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7642 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7643 MARK_TS_TYPED (ARRAY_NOTATION_REF);
7646 /* Build a user-defined numeric literal out of an integer constant type VALUE
7647 with identifier SUFFIX. */
7649 tree
7650 build_userdef_literal (tree suffix_id, tree value,
7651 enum overflow_type overflow, tree num_string)
7653 tree literal = make_node (USERDEF_LITERAL);
7654 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7655 USERDEF_LITERAL_VALUE (literal) = value;
7656 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
7657 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7658 return literal;
7661 /* For vector[index], convert the vector to an array of the underlying type.
7662 Return true if the resulting ARRAY_REF should not be an lvalue. */
7664 bool
7665 convert_vector_to_array_for_subscript (location_t loc,
7666 tree *vecp, tree index)
7668 bool ret = false;
7669 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7671 tree type = TREE_TYPE (*vecp);
7673 ret = !lvalue_p (*vecp);
7675 if (TREE_CODE (index) == INTEGER_CST)
7676 if (!tree_fits_uhwi_p (index)
7677 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7678 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7680 /* We are building an ARRAY_REF so mark the vector as addressable
7681 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7682 for function parameters. */
7683 c_common_mark_addressable_vec (*vecp);
7685 *vecp = build1 (VIEW_CONVERT_EXPR,
7686 build_array_type_nelts (TREE_TYPE (type),
7687 TYPE_VECTOR_SUBPARTS (type)),
7688 *vecp);
7690 return ret;
7693 /* Determine which of the operands, if any, is a scalar that needs to be
7694 converted to a vector, for the range of operations. */
7695 enum stv_conv
7696 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7697 bool complain)
7699 tree type0 = TREE_TYPE (op0);
7700 tree type1 = TREE_TYPE (op1);
7701 bool integer_only_op = false;
7702 enum stv_conv ret = stv_firstarg;
7704 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
7705 switch (code)
7707 /* Most GENERIC binary expressions require homogeneous arguments.
7708 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7709 argument that is a vector and a second one that is a scalar, so
7710 we never return stv_secondarg for them. */
7711 case RSHIFT_EXPR:
7712 case LSHIFT_EXPR:
7713 if (TREE_CODE (type0) == INTEGER_TYPE
7714 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7716 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7718 if (complain)
7719 error_at (loc, "conversion of scalar %qT to vector %qT "
7720 "involves truncation", type0, type1);
7721 return stv_error;
7723 else
7724 return stv_firstarg;
7726 break;
7728 case BIT_IOR_EXPR:
7729 case BIT_XOR_EXPR:
7730 case BIT_AND_EXPR:
7731 integer_only_op = true;
7732 /* fall through */
7734 case VEC_COND_EXPR:
7736 case PLUS_EXPR:
7737 case MINUS_EXPR:
7738 case MULT_EXPR:
7739 case TRUNC_DIV_EXPR:
7740 case CEIL_DIV_EXPR:
7741 case FLOOR_DIV_EXPR:
7742 case ROUND_DIV_EXPR:
7743 case EXACT_DIV_EXPR:
7744 case TRUNC_MOD_EXPR:
7745 case FLOOR_MOD_EXPR:
7746 case RDIV_EXPR:
7747 case EQ_EXPR:
7748 case NE_EXPR:
7749 case LE_EXPR:
7750 case GE_EXPR:
7751 case LT_EXPR:
7752 case GT_EXPR:
7753 /* What about UNLT_EXPR? */
7754 if (VECTOR_TYPE_P (type0))
7756 ret = stv_secondarg;
7757 std::swap (type0, type1);
7758 std::swap (op0, op1);
7761 if (TREE_CODE (type0) == INTEGER_TYPE
7762 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7764 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7766 if (complain)
7767 error_at (loc, "conversion of scalar %qT to vector %qT "
7768 "involves truncation", type0, type1);
7769 return stv_error;
7771 return ret;
7773 else if (!integer_only_op
7774 /* Allow integer --> real conversion if safe. */
7775 && (TREE_CODE (type0) == REAL_TYPE
7776 || TREE_CODE (type0) == INTEGER_TYPE)
7777 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7779 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
7781 if (complain)
7782 error_at (loc, "conversion of scalar %qT to vector %qT "
7783 "involves truncation", type0, type1);
7784 return stv_error;
7786 return ret;
7788 default:
7789 break;
7792 return stv_nothing;
7795 /* Return the alignment of std::max_align_t.
7797 [support.types.layout] The type max_align_t is a POD type whose alignment
7798 requirement is at least as great as that of every scalar type, and whose
7799 alignment requirement is supported in every context. */
7801 unsigned
7802 max_align_t_align ()
7804 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
7805 TYPE_ALIGN (long_double_type_node));
7806 if (float128_type_node != NULL_TREE)
7807 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
7808 return max_align;
7811 /* Return true iff ALIGN is an integral constant that is a fundamental
7812 alignment, as defined by [basic.align] in the c++-11
7813 specifications.
7815 That is:
7817 [A fundamental alignment is represented by an alignment less than or
7818 equal to the greatest alignment supported by the implementation
7819 in all contexts, which is equal to alignof(max_align_t)]. */
7821 bool
7822 cxx_fundamental_alignment_p (unsigned align)
7824 return (align <= max_align_t_align ());
7827 /* Return true if T is a pointer to a zero-sized aggregate. */
7829 bool
7830 pointer_to_zero_sized_aggr_p (tree t)
7832 if (!POINTER_TYPE_P (t))
7833 return false;
7834 t = TREE_TYPE (t);
7835 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
7838 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
7839 with no library fallback or for an ADDR_EXPR whose operand is such type
7840 issues an error pointing to the location LOC.
7841 Returns true when the expression has been diagnosed and false
7842 otherwise. */
7844 bool
7845 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7847 if (TREE_CODE (expr) == ADDR_EXPR)
7848 expr = TREE_OPERAND (expr, 0);
7850 if (TREE_TYPE (expr)
7851 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7852 && TREE_CODE (expr) == FUNCTION_DECL
7853 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7854 false positives for user-declared built-ins such as abs or
7855 strlen, and for C++ operators new and delete.
7856 The c_decl_implicit() test avoids false positives for implicitly
7857 declared built-ins with library fallbacks (such as abs). */
7858 && DECL_BUILT_IN (expr)
7859 && DECL_IS_BUILTIN (expr)
7860 && !c_decl_implicit (expr)
7861 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7863 if (loc == UNKNOWN_LOCATION)
7864 loc = EXPR_LOC_OR_LOC (expr, input_location);
7866 /* Reject arguments that are built-in functions with
7867 no library fallback. */
7868 error_at (loc, "built-in function %qE must be directly called", expr);
7870 return true;
7873 return false;
7876 /* Check if array size calculations overflow or if the array covers more
7877 than half of the address space. Return true if the size of the array
7878 is valid, false otherwise. TYPE is the type of the array and NAME is
7879 the name of the array, or NULL_TREE for unnamed arrays. */
7881 bool
7882 valid_array_size_p (location_t loc, tree type, tree name)
7884 if (type != error_mark_node
7885 && COMPLETE_TYPE_P (type)
7886 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7887 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7889 if (name)
7890 error_at (loc, "size of array %qE is too large", name);
7891 else
7892 error_at (loc, "size of unnamed array is too large");
7893 return false;
7895 return true;
7898 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
7899 timestamp to replace embedded current dates to get reproducible
7900 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
7902 time_t
7903 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
7905 char *source_date_epoch;
7906 int64_t epoch;
7907 char *endptr;
7909 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
7910 if (!source_date_epoch)
7911 return (time_t) -1;
7913 errno = 0;
7914 #if defined(INT64_T_IS_LONG)
7915 epoch = strtol (source_date_epoch, &endptr, 10);
7916 #else
7917 epoch = strtoll (source_date_epoch, &endptr, 10);
7918 #endif
7919 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
7920 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
7922 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
7923 "expand to a non-negative integer less than or equal to %wd",
7924 MAX_SOURCE_DATE_EPOCH);
7925 return (time_t) -1;
7928 return (time_t) epoch;
7931 /* Callback for libcpp for offering spelling suggestions for misspelled
7932 directives. GOAL is an unrecognized string; CANDIDATES is a
7933 NULL-terminated array of candidate strings. Return the closest
7934 match to GOAL within CANDIDATES, or NULL if none are good
7935 suggestions. */
7937 const char *
7938 cb_get_suggestion (cpp_reader *, const char *goal,
7939 const char *const *candidates)
7941 best_match<const char *, const char *> bm (goal);
7942 while (*candidates)
7943 bm.consider (*candidates++);
7944 return bm.get_best_meaningful_candidate ();
7947 #include "gt-c-family-c-common.h"