1/* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#define GCC_C_COMMON_C
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "target.h"
26#include "function.h"
27#include "tree.h"
28#include "memmodel.h"
29#include "c-common.h"
30#include "gimple-expr.h"
31#include "tm_p.h"
32#include "stringpool.h"
33#include "cgraph.h"
34#include "diagnostic.h"
35#include "intl.h"
36#include "stor-layout.h"
37#include "calls.h"
38#include "attribs.h"
39#include "varasm.h"
40#include "trans-mem.h"
41#include "c-objc.h"
42#include "common/common-target.h"
43#include "langhooks.h"
44#include "tree-inline.h"
45#include "toplev.h"
46#include "tree-iterator.h"
47#include "opts.h"
48#include "gimplify.h"
49#include "substring-locations.h"
50#include "spellcheck.h"
51
52cpp_reader *parse_in; /* Declared in c-pragma.h. */
53
54/* Mode used to build pointers (VOIDmode means ptr_mode). */
55
56machine_mode c_default_pointer_mode = VOIDmode;
57
58/* The following symbols are subsumed in the c_global_trees array, and
59 listed here individually for documentation purposes.
60
61 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
62
63 tree short_integer_type_node;
64 tree long_integer_type_node;
65 tree long_long_integer_type_node;
66
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
70
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
74
75 tree ptrdiff_type_node;
76
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
80
81 tree char16_type_node;
82 tree char32_type_node;
83
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
87
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;
92
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
96
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;
102
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;
108
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
111
112 Nodes for types `void *' and `const void *'.
113
114 tree ptr_type_node, const_ptr_type_node;
115
116 Nodes for types `char *' and `const char *'.
117
118 tree string_type_node, const_string_type_node;
119
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
122
123 tree char_array_type_node;
124
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
127
128 tree wchar_array_type_node;
129
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
132
133 tree char16_array_type_node;
134
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
137
138 tree char32_array_type_node;
139
140 Type `int ()' -- used for implicit declaration of functions.
141
142 tree default_function_type;
143
144 A VOID_TYPE node, packaged in a TREE_LIST.
145
146 tree void_list_node;
147
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.)
151
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
155
156 Stack of nested function name VAR_DECLs.
157
158 tree saved_function_name_decls;
159
160*/
161
162tree c_global_trees[CTI_MAX];
163
164/* Switches common to the C front ends. */
165
166/* Nonzero means don't output line number information. */
167
168char flag_no_line_commands;
169
170/* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
172
173char flag_no_output;
174
175/* Nonzero means dump macros in some fashion. */
176
177char flag_dump_macros;
178
179/* Nonzero means pass #include lines through to the output. */
180
181char flag_dump_includes;
182
183/* Nonzero means process PCH files while preprocessing. */
184
185bool flag_pch_preprocess;
186
187/* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
189
190const char *pch_file;
191
192/* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194int flag_iso;
195
196/* C/ObjC language option variables. */
197
198
199/* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
201
202int flag_cond_mismatch;
203
204/* Nonzero means enable C89 Amendment 1 features. */
205
206int flag_isoc94;
207
208/* Nonzero means use the ISO C99 (or C11) dialect of C. */
209
210int flag_isoc99;
211
212/* Nonzero means use the ISO C11 dialect of C. */
213
214int flag_isoc11;
215
216/* Nonzero means that we have builtin functions, and main is an int. */
217
218int flag_hosted = 1;
219
220
221/* ObjC language option variables. */
222
223
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. */
227
228int print_struct_values;
229
230/* Tells the compiler what is the constant string class for ObjC. */
231
232const char *constant_string_class_name;
233
234
235/* C++ language option variables. */
236
237/* The reference version of the ABI for -Wabi. */
238
239int warn_abi_version = -1;
240
241/* Nonzero means generate separate instantiation control files and
242 juggle them at link time. */
243
244int flag_use_repository;
245
246/* The C++ dialect being used. Default set in c_common_post_options. */
247
248enum cxx_dialect cxx_dialect = cxx_unset;
249
250/* Maximum template instantiation depth. This limit exists to limit the
251 time it takes to notice excessively recursive template instantiations.
252
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). */
256
257int max_tinst_depth = 900;
258
259/* The elements of `ridpointers' are identifier nodes for the reserved
260 type names and storage classes. It is indexed by a RID_... value. */
261tree *ridpointers;
262
263tree (*make_fname_decl) (location_t, tree, int);
264
265/* Nonzero means don't warn about problems that occur when the code is
266 executed. */
267int c_inhibit_evaluation_warnings;
268
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. */
275bool in_late_binary_op;
276
277/* Whether lexing has been completed, so subsequent preprocessor
278 errors should use the compiler's input_location. */
279bool done_lexing = false;
280
281/* Information about how a function name is generated. */
282struct fname_var_t
283{
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? */
287};
288
289/* The three ways of getting then name of the current function. */
290
291const struct fname_var_t fname_vars[] =
292{
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},
300};
301
302/* Global visibility options. */
303struct visibility_flags visibility_options;
304
305static tree check_case_value (location_t, tree);
306static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
307 bool *);
308
309
310static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
311static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
312
313/* Reserved words. The third field is a mask: keywords are disabled
314 if they match the mask.
315
316 Masks for languages:
317 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
318 C --std=c99: D_CXXONLY | D_OBJC
319 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
320 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
321 C++ --std=c++11: D_CONLY | D_OBJC
322 ObjC++ is like C++ except that D_OBJC is not set
323
324 If -fno-asm is used, D_ASM is added to the mask. If
325 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
326 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
327 In C with -Wc++-compat, we warn if D_CXXWARN is set.
328
329 Note the complication of the D_CXX_OBJC keywords. These are
330 reserved words such as 'class'. In C++, 'class' is a reserved
331 word. In Objective-C++ it is too. In Objective-C, it is a
332 reserved word too, but only if it follows an '@' sign.
333*/
334const struct c_common_resword c_common_reswords[] =
335{
336 { "_Alignas", RID_ALIGNAS, D_CONLY },
337 { "_Alignof", RID_ALIGNOF, D_CONLY },
338 { "_Atomic", RID_ATOMIC, D_CONLY },
339 { "_Bool", RID_BOOL, D_CONLY },
340 { "_Complex", RID_COMPLEX, 0 },
341 { "_Imaginary", RID_IMAGINARY, D_CONLY },
342 { "_Float16", RID_FLOAT16, D_CONLY },
343 { "_Float32", RID_FLOAT32, D_CONLY },
344 { "_Float64", RID_FLOAT64, D_CONLY },
345 { "_Float128", RID_FLOAT128, D_CONLY },
346 { "_Float32x", RID_FLOAT32X, D_CONLY },
347 { "_Float64x", RID_FLOAT64X, D_CONLY },
348 { "_Float128x", RID_FLOAT128X, D_CONLY },
349 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
350 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
351 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
352 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
353 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
354 { "_Sat", RID_SAT, D_CONLY | D_EXT },
355 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
356 { "_Noreturn", RID_NORETURN, D_CONLY },
357 { "_Generic", RID_GENERIC, D_CONLY },
358 { "_Thread_local", RID_THREAD, D_CONLY },
359 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
360 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
361 { "__alignof", RID_ALIGNOF, 0 },
362 { "__alignof__", RID_ALIGNOF, 0 },
363 { "__asm", RID_ASM, 0 },
364 { "__asm__", RID_ASM, 0 },
365 { "__attribute", RID_ATTRIBUTE, 0 },
366 { "__attribute__", RID_ATTRIBUTE, 0 },
367 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
368 { "__bases", RID_BASES, D_CXXONLY },
369 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
370 { "__builtin_call_with_static_chain",
371 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
372 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
373 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
374 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
375 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
376 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
377 { "__builtin_offsetof", RID_OFFSETOF, 0 },
378 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
379 { "__builtin_va_arg", RID_VA_ARG, 0 },
380 { "__complex", RID_COMPLEX, 0 },
381 { "__complex__", RID_COMPLEX, 0 },
382 { "__const", RID_CONST, 0 },
383 { "__const__", RID_CONST, 0 },
384 { "__decltype", RID_DECLTYPE, D_CXXONLY },
385 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
386 { "__extension__", RID_EXTENSION, 0 },
387 { "__func__", RID_C99_FUNCTION_NAME, 0 },
388 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
389 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
390 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
391 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
392 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
393 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
394 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
395 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
396 D_CXXONLY },
397 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
398 { "__imag", RID_IMAGPART, 0 },
399 { "__imag__", RID_IMAGPART, 0 },
400 { "__inline", RID_INLINE, 0 },
401 { "__inline__", RID_INLINE, 0 },
402 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
403 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
404 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
405 { "__is_class", RID_IS_CLASS, D_CXXONLY },
406 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
407 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
408 { "__is_final", RID_IS_FINAL, D_CXXONLY },
409 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
410 { "__is_pod", RID_IS_POD, D_CXXONLY },
411 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
412 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
413 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
414 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
415 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
416 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
417 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
418 { "__is_union", RID_IS_UNION, D_CXXONLY },
419 { "__label__", RID_LABEL, 0 },
420 { "__null", RID_NULL, 0 },
421 { "__real", RID_REALPART, 0 },
422 { "__real__", RID_REALPART, 0 },
423 { "__restrict", RID_RESTRICT, 0 },
424 { "__restrict__", RID_RESTRICT, 0 },
425 { "__signed", RID_SIGNED, 0 },
426 { "__signed__", RID_SIGNED, 0 },
427 { "__thread", RID_THREAD, 0 },
428 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
429 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
430 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
431 { "__typeof", RID_TYPEOF, 0 },
432 { "__typeof__", RID_TYPEOF, 0 },
433 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
434 { "__volatile", RID_VOLATILE, 0 },
435 { "__volatile__", RID_VOLATILE, 0 },
436 { "__GIMPLE", RID_GIMPLE, D_CONLY },
437 { "__PHI", RID_PHI, D_CONLY },
438 { "__RTL", RID_RTL, D_CONLY },
439 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
440 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
441 { "asm", RID_ASM, D_ASM },
442 { "auto", RID_AUTO, 0 },
443 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
444 { "break", RID_BREAK, 0 },
445 { "case", RID_CASE, 0 },
446 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
447 { "char", RID_CHAR, 0 },
448 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
449 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
450 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
451 { "const", RID_CONST, 0 },
452 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
453 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
454 { "continue", RID_CONTINUE, 0 },
455 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
456 { "default", RID_DEFAULT, 0 },
457 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
458 { "do", RID_DO, 0 },
459 { "double", RID_DOUBLE, 0 },
460 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
461 { "else", RID_ELSE, 0 },
462 { "enum", RID_ENUM, 0 },
463 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
464 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
465 { "extern", RID_EXTERN, 0 },
466 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
467 { "float", RID_FLOAT, 0 },
468 { "for", RID_FOR, 0 },
469 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
470 { "goto", RID_GOTO, 0 },
471 { "if", RID_IF, 0 },
472 { "inline", RID_INLINE, D_EXT89 },
473 { "int", RID_INT, 0 },
474 { "long", RID_LONG, 0 },
475 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
476 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
477 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
478 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
479 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
480 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
481 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
482 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
483 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
484 { "register", RID_REGISTER, 0 },
485 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
486 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
487 { "return", RID_RETURN, 0 },
488 { "short", RID_SHORT, 0 },
489 { "signed", RID_SIGNED, 0 },
490 { "sizeof", RID_SIZEOF, 0 },
491 { "static", RID_STATIC, 0 },
492 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
493 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
494 { "struct", RID_STRUCT, 0 },
495 { "switch", RID_SWITCH, 0 },
496 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
497 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
498 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
499 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
500 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
501 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
502 { "typedef", RID_TYPEDEF, 0 },
503 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
504 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
505 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
506 { "union", RID_UNION, 0 },
507 { "unsigned", RID_UNSIGNED, 0 },
508 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
509 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
510 { "void", RID_VOID, 0 },
511 { "volatile", RID_VOLATILE, 0 },
512 { "wchar_t", RID_WCHAR, D_CXXONLY },
513 { "while", RID_WHILE, 0 },
514 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
515 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
516
517 /* C++ transactional memory. */
518 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
519 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
520 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
521 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
522
523 /* Concepts-related keywords */
524 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
525 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
526
527 /* These Objective-C keywords are recognized only immediately after
528 an '@'. */
529 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
530 { "defs", RID_AT_DEFS, D_OBJC },
531 { "encode", RID_AT_ENCODE, D_OBJC },
532 { "end", RID_AT_END, D_OBJC },
533 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
534 { "interface", RID_AT_INTERFACE, D_OBJC },
535 { "protocol", RID_AT_PROTOCOL, D_OBJC },
536 { "selector", RID_AT_SELECTOR, D_OBJC },
537 { "finally", RID_AT_FINALLY, D_OBJC },
538 { "optional", RID_AT_OPTIONAL, D_OBJC },
539 { "required", RID_AT_REQUIRED, D_OBJC },
540 { "property", RID_AT_PROPERTY, D_OBJC },
541 { "package", RID_AT_PACKAGE, D_OBJC },
542 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
543 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
544 /* These are recognized only in protocol-qualifier context
545 (see above) */
546 { "bycopy", RID_BYCOPY, D_OBJC },
547 { "byref", RID_BYREF, D_OBJC },
548 { "in", RID_IN, D_OBJC },
549 { "inout", RID_INOUT, D_OBJC },
550 { "oneway", RID_ONEWAY, D_OBJC },
551 { "out", RID_OUT, D_OBJC },
552 /* These are recognized inside a property attribute list */
553 { "assign", RID_ASSIGN, D_OBJC },
554 { "copy", RID_COPY, D_OBJC },
555 { "getter", RID_GETTER, D_OBJC },
556 { "nonatomic", RID_NONATOMIC, D_OBJC },
557 { "readonly", RID_READONLY, D_OBJC },
558 { "readwrite", RID_READWRITE, D_OBJC },
559 { "retain", RID_RETAIN, D_OBJC },
560 { "setter", RID_SETTER, D_OBJC },
561};
562
563const unsigned int num_c_common_reswords =
564 sizeof c_common_reswords / sizeof (struct c_common_resword);
565
566/* Return identifier for address space AS. */
567
568const char *
569c_addr_space_name (addr_space_t as)
570{
571 int rid = RID_FIRST_ADDR_SPACE + as;
572 gcc_assert (ridpointers [rid]);
573 return IDENTIFIER_POINTER (ridpointers [rid]);
574}
575
576/* Push current bindings for the function name VAR_DECLS. */
577
578void
579start_fname_decls (void)
580{
581 unsigned ix;
582 tree saved = NULL_TREE;
583
584 for (ix = 0; fname_vars[ix].decl; ix++)
585 {
586 tree decl = *fname_vars[ix].decl;
587
588 if (decl)
589 {
590 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
591 saved);
592 *fname_vars[ix].decl = NULL_TREE;
593 }
594 }
595 if (saved || saved_function_name_decls)
596 /* Normally they'll have been NULL, so only push if we've got a
597 stack, or they are non-NULL. */
598 saved_function_name_decls = tree_cons (saved, NULL_TREE,
599 saved_function_name_decls);
600}
601
602/* Finish up the current bindings, adding them into the current function's
603 statement tree. This must be done _before_ finish_stmt_tree is called.
604 If there is no current function, we must be at file scope and no statements
605 are involved. Pop the previous bindings. */
606
607void
608finish_fname_decls (void)
609{
610 unsigned ix;
611 tree stmts = NULL_TREE;
612 tree stack = saved_function_name_decls;
613
614 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
615 append_to_statement_list (TREE_VALUE (stack), &stmts);
616
617 if (stmts)
618 {
619 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
620
621 if (TREE_CODE (*bodyp) == BIND_EXPR)
622 bodyp = &BIND_EXPR_BODY (*bodyp);
623
624 append_to_statement_list_force (*bodyp, &stmts);
625 *bodyp = stmts;
626 }
627
628 for (ix = 0; fname_vars[ix].decl; ix++)
629 *fname_vars[ix].decl = NULL_TREE;
630
631 if (stack)
632 {
633 /* We had saved values, restore them. */
634 tree saved;
635
636 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
637 {
638 tree decl = TREE_PURPOSE (saved);
639 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
640
641 *fname_vars[ix].decl = decl;
642 }
643 stack = TREE_CHAIN (stack);
644 }
645 saved_function_name_decls = stack;
646}
647
648/* Return the text name of the current function, suitably prettified
649 by PRETTY_P. Return string must be freed by caller. */
650
651const char *
652fname_as_string (int pretty_p)
653{
654 const char *name = "top level";
655 char *namep;
656 int vrb = 2, len;
657 cpp_string cstr = { 0, 0 }, strname;
658
659 if (!pretty_p)
660 {
661 name = "";
662 vrb = 0;
663 }
664
665 if (current_function_decl)
666 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
667
668 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
669
670 namep = XNEWVEC (char, len);
671 snprintf (namep, len, "\"%s\"", name);
672 strname.text = (unsigned char *) namep;
673 strname.len = len - 1;
674
675 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
676 {
677 XDELETEVEC (namep);
678 return (const char *) cstr.text;
679 }
680
681 return namep;
682}
683
684/* Return the VAR_DECL for a const char array naming the current
685 function. If the VAR_DECL has not yet been created, create it
686 now. RID indicates how it should be formatted and IDENTIFIER_NODE
687 ID is its name (unfortunately C and C++ hold the RID values of
688 keywords in different places, so we can't derive RID from ID in
689 this language independent code. LOC is the location of the
690 function. */
691
692tree
693fname_decl (location_t loc, unsigned int rid, tree id)
694{
695 unsigned ix;
696 tree decl = NULL_TREE;
697
698 for (ix = 0; fname_vars[ix].decl; ix++)
699 if (fname_vars[ix].rid == rid)
700 break;
701
702 decl = *fname_vars[ix].decl;
703 if (!decl)
704 {
705 /* If a tree is built here, it would normally have the lineno of
706 the current statement. Later this tree will be moved to the
707 beginning of the function and this line number will be wrong.
708 To avoid this problem set the lineno to 0 here; that prevents
709 it from appearing in the RTL. */
710 tree stmts;
711 location_t saved_location = input_location;
712 input_location = UNKNOWN_LOCATION;
713
714 stmts = push_stmt_list ();
715 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
716 stmts = pop_stmt_list (stmts);
717 if (!IS_EMPTY_STMT (stmts))
718 saved_function_name_decls
719 = tree_cons (decl, stmts, saved_function_name_decls);
720 *fname_vars[ix].decl = decl;
721 input_location = saved_location;
722 }
723 if (!ix && !current_function_decl)
724 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
725
726 return decl;
727}
728
729/* Given a STRING_CST, give it a suitable array-of-chars data type. */
730
731tree
732fix_string_type (tree value)
733{
734 int length = TREE_STRING_LENGTH (value);
735 int nchars;
736 tree e_type, i_type, a_type;
737
738 /* Compute the number of elements, for the array type. */
739 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
740 {
741 nchars = length;
742 e_type = char_type_node;
743 }
744 else if (TREE_TYPE (value) == char16_array_type_node)
745 {
746 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
747 e_type = char16_type_node;
748 }
749 else if (TREE_TYPE (value) == char32_array_type_node)
750 {
751 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
752 e_type = char32_type_node;
753 }
754 else
755 {
756 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
757 e_type = wchar_type_node;
758 }
759
760 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
761 limit in C++98 Annex B is very large (65536) and is not normative,
762 so we do not diagnose it (warn_overlength_strings is forced off
763 in c_common_post_options). */
764 if (warn_overlength_strings)
765 {
766 const int nchars_max = flag_isoc99 ? 4095 : 509;
767 const int relevant_std = flag_isoc99 ? 99 : 90;
768 if (nchars - 1 > nchars_max)
769 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
770 separate the %d from the 'C'. 'ISO' should not be
771 translated, but it may be moved after 'C%d' in languages
772 where modifiers follow nouns. */
773 pedwarn (input_location, OPT_Woverlength_strings,
774 "string length %qd is greater than the length %qd "
775 "ISO C%d compilers are required to support",
776 nchars - 1, nchars_max, relevant_std);
777 }
778
779 /* Create the array type for the string constant. The ISO C++
780 standard says that a string literal has type `const char[N]' or
781 `const wchar_t[N]'. We use the same logic when invoked as a C
782 front-end with -Wwrite-strings.
783 ??? We should change the type of an expression depending on the
784 state of a warning flag. We should just be warning -- see how
785 this is handled in the C++ front-end for the deprecated implicit
786 conversion from string literals to `char*' or `wchar_t*'.
787
788 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
789 array type being the unqualified version of that type.
790 Therefore, if we are constructing an array of const char, we must
791 construct the matching unqualified array type first. The C front
792 end does not require this, but it does no harm, so we do it
793 unconditionally. */
794 i_type = build_index_type (size_int (nchars - 1));
795 a_type = build_array_type (e_type, i_type);
796 if (c_dialect_cxx() || warn_write_strings)
797 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
798
799 TREE_TYPE (value) = a_type;
800 TREE_CONSTANT (value) = 1;
801 TREE_READONLY (value) = 1;
802 TREE_STATIC (value) = 1;
803 return value;
804}
805
806/* Given a string of type STRING_TYPE, determine what kind of string
807 token would give an equivalent execution encoding: CPP_STRING,
808 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
809 This may not be exactly the string token type that initially created
810 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
811 string type at this point.
812
813 This effectively reverses part of the logic in lex_string and
814 fix_string_type. */
815
816static enum cpp_ttype
817get_cpp_ttype_from_string_type (tree string_type)
818{
819 gcc_assert (string_type);
820 if (TREE_CODE (string_type) == POINTER_TYPE)
821 string_type = TREE_TYPE (string_type);
822
823 if (TREE_CODE (string_type) != ARRAY_TYPE)
824 return CPP_OTHER;
825
826 tree element_type = TREE_TYPE (string_type);
827 if (TREE_CODE (element_type) != INTEGER_TYPE)
828 return CPP_OTHER;
829
830 int bits_per_character = TYPE_PRECISION (element_type);
831 switch (bits_per_character)
832 {
833 case 8:
834 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
835 case 16:
836 return CPP_STRING16;
837 case 32:
838 return CPP_STRING32;
839 }
840
841 return CPP_OTHER;
842}
843
844/* The global record of string concatentations, for use in
845 extracting locations within string literals. */
846
847GTY(()) string_concat_db *g_string_concat_db;
848
849/* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
850
851const char *
852c_get_substring_location (const substring_loc &substr_loc,
853 location_t *out_loc)
854{
855 enum cpp_ttype tok_type
856 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
857 if (tok_type == CPP_OTHER)
858 return "unrecognized string type";
859
860 return get_source_location_for_substring (parse_in, g_string_concat_db,
861 substr_loc.get_fmt_string_loc (),
862 tok_type,
863 substr_loc.get_caret_idx (),
864 substr_loc.get_start_idx (),
865 substr_loc.get_end_idx (),
866 out_loc);
867}
868
869
870/* Fold X for consideration by one of the warning functions when checking
871 whether an expression has a constant value. */
872
873tree
874fold_for_warn (tree x)
875{
876 if (c_dialect_cxx ())
877 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
878 else
879 /* The C front-end has already folded X appropriately. */
880 return x;
881}
882
883/* Return true iff T is a boolean promoted to int. */
884
885bool
886bool_promoted_to_int_p (tree t)
887{
888 return (CONVERT_EXPR_P (t)
889 && TREE_TYPE (t) == integer_type_node
890 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
891}
892
893/* vector_targets_convertible_p is used for vector pointer types. The
894 callers perform various checks that the qualifiers are satisfactory,
895 while OTOH vector_targets_convertible_p ignores the number of elements
896 in the vectors. That's fine with vector pointers as we can consider,
897 say, a vector of 8 elements as two consecutive vectors of 4 elements,
898 and that does not require and conversion of the pointer values.
899 In contrast, vector_types_convertible_p and
900 vector_types_compatible_elements_p are used for vector value types. */
901/* True if pointers to distinct types T1 and T2 can be converted to
902 each other without an explicit cast. Only returns true for opaque
903 vector types. */
904bool
905vector_targets_convertible_p (const_tree t1, const_tree t2)
906{
907 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
908 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
909 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
910 return true;
911
912 return false;
913}
914
915/* vector_types_convertible_p is used for vector value types.
916 It could in principle call vector_targets_convertible_p as a subroutine,
917 but then the check for vector type would be duplicated with its callers,
918 and also the purpose of vector_targets_convertible_p would become
919 muddled.
920 Where vector_types_convertible_p returns true, a conversion might still be
921 needed to make the types match.
922 In contrast, vector_targets_convertible_p is used for vector pointer
923 values, and vector_types_compatible_elements_p is used specifically
924 in the context for binary operators, as a check if use is possible without
925 conversion. */
926/* True if vector types T1 and T2 can be converted to each other
927 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
928 can only be converted with -flax-vector-conversions yet that is not
929 in effect, emit a note telling the user about that option if such
930 a note has not previously been emitted. */
931bool
932vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
933{
934 static bool emitted_lax_note = false;
935 bool convertible_lax;
936
937 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
938 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
939 return true;
940
941 convertible_lax =
942 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
943 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
944 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
945 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
946 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
947
948 if (!convertible_lax || flag_lax_vector_conversions)
949 return convertible_lax;
950
951 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
952 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
953 return true;
954
955 if (emit_lax_note && !emitted_lax_note)
956 {
957 emitted_lax_note = true;
958 inform (input_location, "use -flax-vector-conversions to permit "
959 "conversions between vectors with differing "
960 "element types or numbers of subparts");
961 }
962
963 return false;
964}
965
966/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
967 and have vector types, V0 has the same type as V1, and the number of
968 elements of V0, V1, MASK is the same.
969
970 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
971 called with two arguments. In this case implementation passes the
972 first argument twice in order to share the same tree code. This fact
973 could enable the mask-values being twice the vector length. This is
974 an implementation accident and this semantics is not guaranteed to
975 the user. */
976tree
977c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
978 bool complain)
979{
980 tree ret;
981 bool wrap = true;
982 bool maybe_const = false;
983 bool two_arguments = false;
984
985 if (v1 == NULL_TREE)
986 {
987 two_arguments = true;
988 v1 = v0;
989 }
990
991 if (v0 == error_mark_node || v1 == error_mark_node
992 || mask == error_mark_node)
993 return error_mark_node;
994
995 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
996 {
997 if (complain)
998 error_at (loc, "__builtin_shuffle last argument must "
999 "be an integer vector");
1000 return error_mark_node;
1001 }
1002
1003 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1004 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
1005 {
1006 if (complain)
1007 error_at (loc, "__builtin_shuffle arguments must be vectors");
1008 return error_mark_node;
1009 }
1010
1011 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1012 {
1013 if (complain)
1014 error_at (loc, "__builtin_shuffle argument vectors must be of "
1015 "the same type");
1016 return error_mark_node;
1017 }
1018
1019 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
1020 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
1021 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
1022 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1023 {
1024 if (complain)
1025 error_at (loc, "__builtin_shuffle number of elements of the "
1026 "argument vector(s) and the mask vector should "
1027 "be the same");
1028 return error_mark_node;
1029 }
1030
1031 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1032 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1033 {
1034 if (complain)
1035 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
1036 "must have the same size as inner type of the mask");
1037 return error_mark_node;
1038 }
1039
1040 if (!c_dialect_cxx ())
1041 {
1042 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1043 v0 = c_fully_fold (v0, false, &maybe_const);
1044 wrap &= maybe_const;
1045
1046 if (two_arguments)
1047 v1 = v0 = save_expr (v0);
1048 else
1049 {
1050 v1 = c_fully_fold (v1, false, &maybe_const);
1051 wrap &= maybe_const;
1052 }
1053
1054 mask = c_fully_fold (mask, false, &maybe_const);
1055 wrap &= maybe_const;
1056 }
1057 else if (two_arguments)
1058 v1 = v0 = save_expr (v0);
1059
1060 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1061
1062 if (!c_dialect_cxx () && !wrap)
1063 ret = c_wrap_maybe_const (ret, true);
1064
1065 return ret;
1066}
1067
1068/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1069 to integral type. */
1070
1071tree
1072c_common_get_narrower (tree op, int *unsignedp_ptr)
1073{
1074 op = get_narrower (op, unsignedp_ptr);
1075
1076 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1077 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1078 {
1079 /* C++0x scoped enumerations don't implicitly convert to integral
1080 type; if we stripped an explicit conversion to a larger type we
1081 need to replace it so common_type will still work. */
1082 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1083 TYPE_UNSIGNED (TREE_TYPE (op)));
1084 op = fold_convert (type, op);
1085 }
1086 return op;
1087}
1088
1089/* This is a helper function of build_binary_op.
1090
1091 For certain operations if both args were extended from the same
1092 smaller type, do the arithmetic in that type and then extend.
1093
1094 BITWISE indicates a bitwise operation.
1095 For them, this optimization is safe only if
1096 both args are zero-extended or both are sign-extended.
1097 Otherwise, we might change the result.
1098 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1099 but calculated in (unsigned short) it would be (unsigned short)-1.
1100*/
1101tree
1102shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1103{
1104 int unsigned0, unsigned1;
1105 tree arg0, arg1;
1106 int uns;
1107 tree type;
1108
1109 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1110 excessive narrowing when we call get_narrower below. For
1111 example, suppose that OP0 is of unsigned int extended
1112 from signed char and that RESULT_TYPE is long long int.
1113 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1114 like
1115
1116 (long long int) (unsigned int) signed_char
1117
1118 which get_narrower would narrow down to
1119
1120 (unsigned int) signed char
1121
1122 If we do not cast OP0 first, get_narrower would return
1123 signed_char, which is inconsistent with the case of the
1124 explicit cast. */
1125 op0 = convert (result_type, op0);
1126 op1 = convert (result_type, op1);
1127
1128 arg0 = c_common_get_narrower (op0, &unsigned0);
1129 arg1 = c_common_get_narrower (op1, &unsigned1);
1130
1131 /* UNS is 1 if the operation to be done is an unsigned one. */
1132 uns = TYPE_UNSIGNED (result_type);
1133
1134 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1135 but it *requires* conversion to FINAL_TYPE. */
1136
1137 if ((TYPE_PRECISION (TREE_TYPE (op0))
1138 == TYPE_PRECISION (TREE_TYPE (arg0)))
1139 && TREE_TYPE (op0) != result_type)
1140 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1141 if ((TYPE_PRECISION (TREE_TYPE (op1))
1142 == TYPE_PRECISION (TREE_TYPE (arg1)))
1143 && TREE_TYPE (op1) != result_type)
1144 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1145
1146 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1147
1148 /* For bitwise operations, signedness of nominal type
1149 does not matter. Consider only how operands were extended. */
1150 if (bitwise)
1151 uns = unsigned0;
1152
1153 /* Note that in all three cases below we refrain from optimizing
1154 an unsigned operation on sign-extended args.
1155 That would not be valid. */
1156
1157 /* Both args variable: if both extended in same way
1158 from same width, do it in that width.
1159 Do it unsigned if args were zero-extended. */
1160 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1161 < TYPE_PRECISION (result_type))
1162 && (TYPE_PRECISION (TREE_TYPE (arg1))
1163 == TYPE_PRECISION (TREE_TYPE (arg0)))
1164 && unsigned0 == unsigned1
1165 && (unsigned0 || !uns))
1166 return c_common_signed_or_unsigned_type
1167 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1168
1169 else if (TREE_CODE (arg0) == INTEGER_CST
1170 && (unsigned1 || !uns)
1171 && (TYPE_PRECISION (TREE_TYPE (arg1))
1172 < TYPE_PRECISION (result_type))
1173 && (type
1174 = c_common_signed_or_unsigned_type (unsigned1,
1175 TREE_TYPE (arg1)))
1176 && !POINTER_TYPE_P (type)
1177 && int_fits_type_p (arg0, type))
1178 return type;
1179
1180 else if (TREE_CODE (arg1) == INTEGER_CST
1181 && (unsigned0 || !uns)
1182 && (TYPE_PRECISION (TREE_TYPE (arg0))
1183 < TYPE_PRECISION (result_type))
1184 && (type
1185 = c_common_signed_or_unsigned_type (unsigned0,
1186 TREE_TYPE (arg0)))
1187 && !POINTER_TYPE_P (type)
1188 && int_fits_type_p (arg1, type))
1189 return type;
1190
1191 return result_type;
1192}
1193
1194/* Returns true iff any integer value of type FROM_TYPE can be represented as
1195 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1196
1197static bool
1198int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1199{
1200 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1201 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1202 REAL_VALUE_TYPE real_low_bound =
1203 real_value_from_int_cst (0, type_low_bound);
1204 REAL_VALUE_TYPE real_high_bound =
1205 real_value_from_int_cst (0, type_high_bound);
1206
1207 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1208 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1209}
1210
1211/* Checks if expression EXPR of complex/real/integer type cannot be converted
1212 to the complex/real/integer type TYPE. Function returns non-zero when:
1213 * EXPR is a constant which cannot be exactly converted to TYPE.
1214 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1215 for EXPR type and TYPE being both integers or both real, or both
1216 complex.
1217 * EXPR is not a constant of complex type and TYPE is a real or
1218 an integer.
1219 * EXPR is not a constant of real type and TYPE is an integer.
1220 * EXPR is not a constant of integer type which cannot be
1221 exactly converted to real type.
1222
1223 Function allows conversions between types of different signedness and
1224 can return SAFE_CONVERSION (zero) in that case. Function can produce
1225 signedness warnings if PRODUCE_WARNS is true.
1226
1227 RESULT, when non-null is the result of the conversion. When constant
1228 it is included in the text of diagnostics.
1229
1230 Function allows conversions from complex constants to non-complex types,
1231 provided that imaginary part is zero and real part can be safely converted
1232 to TYPE. */
1233
1234enum conversion_safety
1235unsafe_conversion_p (location_t loc, tree type, tree expr, tree result,
1236 bool produce_warns)
1237{
1238 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1239 tree expr_type = TREE_TYPE (expr);
1240
1241 bool cstresult = (result
1242 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1243
1244 loc = expansion_point_location_if_in_system_header (loc);
1245
1246 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1247 {
1248 /* If type is complex, we are interested in compatibility with
1249 underlying type. */
1250 if (TREE_CODE (type) == COMPLEX_TYPE)
1251 type = TREE_TYPE (type);
1252
1253 /* Warn for real constant that is not an exact integer converted
1254 to integer type. */
1255 if (TREE_CODE (expr_type) == REAL_TYPE
1256 && TREE_CODE (type) == INTEGER_TYPE)
1257 {
1258 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1259 give_warning = UNSAFE_REAL;
1260 }
1261 /* Warn for an integer constant that does not fit into integer type. */
1262 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1263 && TREE_CODE (type) == INTEGER_TYPE
1264 && !int_fits_type_p (expr, type))
1265 {
1266 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1267 && tree_int_cst_sgn (expr) < 0)
1268 {
1269 if (produce_warns)
1270 {
1271 if (cstresult)
1272 warning_at (loc, OPT_Wsign_conversion,
1273 "unsigned conversion from %qT to %qT "
1274 "changes value from %qE to %qE",
1275 expr_type, type, expr, result);
1276 else
1277 warning_at (loc, OPT_Wsign_conversion,
1278 "unsigned conversion from %qT to %qT "
1279 "changes the value of %qE",
1280 expr_type, type, expr);
1281 }
1282 }
1283 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1284 {
1285 if (cstresult)
1286 warning_at (loc, OPT_Wsign_conversion,
1287 "signed conversion from %qT to %qT changes "
1288 "value from %qE to %qE",
1289 expr_type, type, expr, result);
1290 else
1291 warning_at (loc, OPT_Wsign_conversion,
1292 "signed conversion from %qT to %qT changes "
1293 "the value of %qE",
1294 expr_type, type, expr);
1295 }
1296 else
1297 give_warning = UNSAFE_OTHER;
1298 }
1299 else if (TREE_CODE (type) == REAL_TYPE)
1300 {
1301 /* Warn for an integer constant that does not fit into real type. */
1302 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1303 {
1304 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1305 if (!exact_real_truncate (TYPE_MODE (type), &a))
1306 give_warning = UNSAFE_REAL;
1307 }
1308 /* Warn for a real constant that does not fit into a smaller
1309 real type. */
1310 else if (TREE_CODE (expr_type) == REAL_TYPE
1311 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1312 {
1313 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1314 if (!exact_real_truncate (TYPE_MODE (type), &a))
1315 give_warning = UNSAFE_REAL;
1316 }
1317 }
1318 }
1319
1320 else if (TREE_CODE (expr) == COMPLEX_CST)
1321 {
1322 tree imag_part = TREE_IMAGPART (expr);
1323 /* Conversion from complex constant with zero imaginary part,
1324 perform check for conversion of real part. */
1325 if ((TREE_CODE (imag_part) == REAL_CST
1326 && real_zerop (imag_part))
1327 || (TREE_CODE (imag_part) == INTEGER_CST
1328 && integer_zerop (imag_part)))
1329 /* Note: in this branch we use recursive call to unsafe_conversion_p
1330 with different type of EXPR, but it is still safe, because when EXPR
1331 is a constant, it's type is not used in text of generated warnings
1332 (otherwise they could sound misleading). */
1333 return unsafe_conversion_p (loc, type, TREE_REALPART (expr), result,
1334 produce_warns);
1335 /* Conversion from complex constant with non-zero imaginary part. */
1336 else
1337 {
1338 /* Conversion to complex type.
1339 Perform checks for both real and imaginary parts. */
1340 if (TREE_CODE (type) == COMPLEX_TYPE)
1341 {
1342 /* Unfortunately, produce_warns must be false in two subsequent
1343 calls of unsafe_conversion_p, because otherwise we could
1344 produce strange "double" warnings, if both real and imaginary
1345 parts have conversion problems related to signedness.
1346
1347 For example:
1348 int32_t _Complex a = 0x80000000 + 0x80000000i;
1349
1350 Possible solution: add a separate function for checking
1351 constants and combine result of two calls appropriately. */
1352 enum conversion_safety re_safety =
1353 unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1354 result, false);
1355 enum conversion_safety im_safety =
1356 unsafe_conversion_p (loc, type, imag_part, result, false);
1357
1358 /* Merge the results into appropriate single warning. */
1359
1360 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1361 if (re_safety == im_safety)
1362 give_warning = re_safety;
1363 else if (!re_safety && im_safety)
1364 give_warning = im_safety;
1365 else if (re_safety && !im_safety)
1366 give_warning = re_safety;
1367 else
1368 give_warning = UNSAFE_OTHER;
1369 }
1370 /* Warn about conversion from complex to real or integer type. */
1371 else
1372 give_warning = UNSAFE_IMAGINARY;
1373 }
1374 }
1375
1376 /* Checks for remaining case: EXPR is not constant. */
1377 else
1378 {
1379 /* Warn for real types converted to integer types. */
1380 if (TREE_CODE (expr_type) == REAL_TYPE
1381 && TREE_CODE (type) == INTEGER_TYPE)
1382 give_warning = UNSAFE_REAL;
1383
1384 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1385 && TREE_CODE (type) == INTEGER_TYPE)
1386 {
1387 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1388 expr = get_unwidened (expr, 0);
1389 expr_type = TREE_TYPE (expr);
1390
1391 /* Don't warn for short y; short x = ((int)y & 0xff); */
1392 if (TREE_CODE (expr) == BIT_AND_EXPR
1393 || TREE_CODE (expr) == BIT_IOR_EXPR
1394 || TREE_CODE (expr) == BIT_XOR_EXPR)
1395 {
1396 /* If both args were extended from a shortest type,
1397 use that type if that is safe. */
1398 expr_type = shorten_binary_op (expr_type,
1399 TREE_OPERAND (expr, 0),
1400 TREE_OPERAND (expr, 1),
1401 /* bitwise */1);
1402
1403 if (TREE_CODE (expr) == BIT_AND_EXPR)
1404 {
1405 tree op0 = TREE_OPERAND (expr, 0);
1406 tree op1 = TREE_OPERAND (expr, 1);
1407 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1408 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1409
1410 /* If one of the operands is a non-negative constant
1411 that fits in the target type, then the type of the
1412 other operand does not matter. */
1413 if ((TREE_CODE (op0) == INTEGER_CST
1414 && int_fits_type_p (op0, c_common_signed_type (type))
1415 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1416 || (TREE_CODE (op1) == INTEGER_CST
1417 && int_fits_type_p (op1, c_common_signed_type (type))
1418 && int_fits_type_p (op1,
1419 c_common_unsigned_type (type))))
1420 return SAFE_CONVERSION;
1421 /* If constant is unsigned and fits in the target
1422 type, then the result will also fit. */
1423 else if ((TREE_CODE (op0) == INTEGER_CST
1424 && unsigned0
1425 && int_fits_type_p (op0, type))
1426 || (TREE_CODE (op1) == INTEGER_CST
1427 && unsigned1
1428 && int_fits_type_p (op1, type)))
1429 return SAFE_CONVERSION;
1430 }
1431 }
1432 /* Warn for integer types converted to smaller integer types. */
1433 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1434 give_warning = UNSAFE_OTHER;
1435
1436 /* When they are the same width but different signedness,
1437 then the value may change. */
1438 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1439 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1440 /* Even when converted to a bigger type, if the type is
1441 unsigned but expr is signed, then negative values
1442 will be changed. */
1443 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1444 && produce_warns)
1445 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1446 "may change the sign of the result",
1447 type, expr_type);
1448 }
1449
1450 /* Warn for integer types converted to real types if and only if
1451 all the range of values of the integer type cannot be
1452 represented by the real type. */
1453 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1454 && TREE_CODE (type) == REAL_TYPE)
1455 {
1456 /* Don't warn about char y = 0xff; float x = (int) y; */
1457 expr = get_unwidened (expr, 0);
1458 expr_type = TREE_TYPE (expr);
1459
1460 if (!int_safely_convertible_to_real_p (expr_type, type))
1461 give_warning = UNSAFE_OTHER;
1462 }
1463
1464 /* Warn for real types converted to smaller real types. */
1465 else if (TREE_CODE (expr_type) == REAL_TYPE
1466 && TREE_CODE (type) == REAL_TYPE
1467 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1468 give_warning = UNSAFE_REAL;
1469
1470 /* Check conversion between two complex types. */
1471 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1472 && TREE_CODE (type) == COMPLEX_TYPE)
1473 {
1474 /* Extract underlying types (i.e., type of real and imaginary
1475 parts) of expr_type and type. */
1476 tree from_type = TREE_TYPE (expr_type);
1477 tree to_type = TREE_TYPE (type);
1478
1479 /* Warn for real types converted to integer types. */
1480 if (TREE_CODE (from_type) == REAL_TYPE
1481 && TREE_CODE (to_type) == INTEGER_TYPE)
1482 give_warning = UNSAFE_REAL;
1483
1484 /* Warn for real types converted to smaller real types. */
1485 else if (TREE_CODE (from_type) == REAL_TYPE
1486 && TREE_CODE (to_type) == REAL_TYPE
1487 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1488 give_warning = UNSAFE_REAL;
1489
1490 /* Check conversion for complex integer types. Here implementation
1491 is simpler than for real-domain integers because it does not
1492 involve sophisticated cases, such as bitmasks, casts, etc. */
1493 else if (TREE_CODE (from_type) == INTEGER_TYPE
1494 && TREE_CODE (to_type) == INTEGER_TYPE)
1495 {
1496 /* Warn for integer types converted to smaller integer types. */
1497 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1498 give_warning = UNSAFE_OTHER;
1499
1500 /* Check for different signedness, see case for real-domain
1501 integers (above) for a more detailed comment. */
1502 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1503 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1504 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1505 && produce_warns)
1506 warning_at (loc, OPT_Wsign_conversion,
1507 "conversion to %qT from %qT "
1508 "may change the sign of the result",
1509 type, expr_type);
1510 }
1511 else if (TREE_CODE (from_type) == INTEGER_TYPE
1512 && TREE_CODE (to_type) == REAL_TYPE
1513 && !int_safely_convertible_to_real_p (from_type, to_type))
1514 give_warning = UNSAFE_OTHER;
1515 }
1516
1517 /* Warn for complex types converted to real or integer types. */
1518 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1519 && TREE_CODE (type) != COMPLEX_TYPE)
1520 give_warning = UNSAFE_IMAGINARY;
1521 }
1522
1523 return give_warning;
1524}
1525
1526
1527/* Convert EXPR to TYPE, warning about conversion problems with constants.
1528 Invoke this function on every expression that is converted implicitly,
1529 i.e. because of language rules and not because of an explicit cast. */
1530
1531tree
1532convert_and_check (location_t loc, tree type, tree expr)
1533{
1534 tree result;
1535 tree expr_for_warning;
1536
1537 /* Convert from a value with possible excess precision rather than
1538 via the semantic type, but do not warn about values not fitting
1539 exactly in the semantic type. */
1540 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1541 {
1542 tree orig_type = TREE_TYPE (expr);
1543 expr = TREE_OPERAND (expr, 0);
1544 expr_for_warning = convert (orig_type, expr);
1545 if (orig_type == type)
1546 return expr_for_warning;
1547 }
1548 else
1549 expr_for_warning = expr;
1550
1551 if (TREE_TYPE (expr) == type)
1552 return expr;
1553
1554 result = convert (type, expr);
1555
1556 if (c_inhibit_evaluation_warnings == 0
1557 && !TREE_OVERFLOW_P (expr)
1558 && result != error_mark_node)
1559 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1560
1561 return result;
1562}
1563
1564/* A node in a list that describes references to variables (EXPR), which are
1565 either read accesses if WRITER is zero, or write accesses, in which case
1566 WRITER is the parent of EXPR. */
1567struct tlist
1568{
1569 struct tlist *next;
1570 tree expr, writer;
1571};
1572
1573/* Used to implement a cache the results of a call to verify_tree. We only
1574 use this for SAVE_EXPRs. */
1575struct tlist_cache
1576{
1577 struct tlist_cache *next;
1578 struct tlist *cache_before_sp;
1579 struct tlist *cache_after_sp;
1580 tree expr;
1581};
1582
1583/* Obstack to use when allocating tlist structures, and corresponding
1584 firstobj. */
1585static struct obstack tlist_obstack;
1586static char *tlist_firstobj = 0;
1587
1588/* Keep track of the identifiers we've warned about, so we can avoid duplicate
1589 warnings. */
1590static struct tlist *warned_ids;
1591/* SAVE_EXPRs need special treatment. We process them only once and then
1592 cache the results. */
1593static struct tlist_cache *save_expr_cache;
1594
1595static void add_tlist (struct tlist **, struct tlist *, tree, int);
1596static void merge_tlist (struct tlist **, struct tlist *, int);
1597static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1598static bool warning_candidate_p (tree);
1599static bool candidate_equal_p (const_tree, const_tree);
1600static void warn_for_collisions (struct tlist *);
1601static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1602static struct tlist *new_tlist (struct tlist *, tree, tree);
1603
1604/* Create a new struct tlist and fill in its fields. */
1605static struct tlist *
1606new_tlist (struct tlist *next, tree t, tree writer)
1607{
1608 struct tlist *l;
1609 l = XOBNEW (&tlist_obstack, struct tlist);
1610 l->next = next;
1611 l->expr = t;
1612 l->writer = writer;
1613 return l;
1614}
1615
1616/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1617 is nonnull, we ignore any node we find which has a writer equal to it. */
1618
1619static void
1620add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1621{
1622 while (add)
1623 {
1624 struct tlist *next = add->next;
1625 if (!copy)
1626 add->next = *to;
1627 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1628 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1629 add = next;
1630 }
1631}
1632
1633/* Merge the nodes of ADD into TO. This merging process is done so that for
1634 each variable that already exists in TO, no new node is added; however if
1635 there is a write access recorded in ADD, and an occurrence on TO is only
1636 a read access, then the occurrence in TO will be modified to record the
1637 write. */
1638
1639static void
1640merge_tlist (struct tlist **to, struct tlist *add, int copy)
1641{
1642 struct tlist **end = to;
1643
1644 while (*end)
1645 end = &(*end)->next;
1646
1647 while (add)
1648 {
1649 int found = 0;
1650 struct tlist *tmp2;
1651 struct tlist *next = add->next;
1652
1653 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1654 if (candidate_equal_p (tmp2->expr, add->expr))
1655 {
1656 found = 1;
1657 if (!tmp2->writer)
1658 tmp2->writer = add->writer;
1659 }
1660 if (!found)
1661 {
1662 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1663 end = &(*end)->next;
1664 *end = 0;
1665 }
1666 add = next;
1667 }
1668}
1669
1670/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1671 references in list LIST conflict with it, excluding reads if ONLY writers
1672 is nonzero. */
1673
1674static void
1675warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1676 int only_writes)
1677{
1678 struct tlist *tmp;
1679
1680 /* Avoid duplicate warnings. */
1681 for (tmp = warned_ids; tmp; tmp = tmp->next)
1682 if (candidate_equal_p (tmp->expr, written))
1683 return;
1684
1685 while (list)
1686 {
1687 if (candidate_equal_p (list->expr, written)
1688 && !candidate_equal_p (list->writer, writer)
1689 && (!only_writes || list->writer))
1690 {
1691 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1692 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1693 OPT_Wsequence_point, "operation on %qE may be undefined",
1694 list->expr);
1695 }
1696 list = list->next;
1697 }
1698}
1699
1700/* Given a list LIST of references to variables, find whether any of these
1701 can cause conflicts due to missing sequence points. */
1702
1703static void
1704warn_for_collisions (struct tlist *list)
1705{
1706 struct tlist *tmp;
1707
1708 for (tmp = list; tmp; tmp = tmp->next)
1709 {
1710 if (tmp->writer)
1711 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1712 }
1713}
1714
1715/* Return nonzero if X is a tree that can be verified by the sequence point
1716 warnings. */
1717
1718static bool
1719warning_candidate_p (tree x)
1720{
1721 if (DECL_P (x) && DECL_ARTIFICIAL (x))
1722 return false;
1723
1724 if (TREE_CODE (x) == BLOCK)
1725 return false;
1726
1727 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1728 (lvalue_p) crash on TRY/CATCH. */
1729 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
1730 return false;
1731
1732 if (!lvalue_p (x))
1733 return false;
1734
1735 /* No point to track non-const calls, they will never satisfy
1736 operand_equal_p. */
1737 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
1738 return false;
1739
1740 if (TREE_CODE (x) == STRING_CST)
1741 return false;
1742
1743 return true;
1744}
1745
1746/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1747static bool
1748candidate_equal_p (const_tree x, const_tree y)
1749{
1750 return (x == y) || (x && y && operand_equal_p (x, y, 0));
1751}
1752
1753/* Walk the tree X, and record accesses to variables. If X is written by the
1754 parent tree, WRITER is the parent.
1755 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1756 expression or its only operand forces a sequence point, then everything up
1757 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1758 in PNO_SP.
1759 Once we return, we will have emitted warnings if any subexpression before
1760 such a sequence point could be undefined. On a higher level, however, the
1761 sequence point may not be relevant, and we'll merge the two lists.
1762
1763 Example: (b++, a) + b;
1764 The call that processes the COMPOUND_EXPR will store the increment of B
1765 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1766 processes the PLUS_EXPR will need to merge the two lists so that
1767 eventually, all accesses end up on the same list (and we'll warn about the
1768 unordered subexpressions b++ and b.
1769
1770 A note on merging. If we modify the former example so that our expression
1771 becomes
1772 (b++, b) + a
1773 care must be taken not simply to add all three expressions into the final
1774 PNO_SP list. The function merge_tlist takes care of that by merging the
1775 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1776 way, so that no more than one access to B is recorded. */
1777
1778static void
1779verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1780 tree writer)
1781{
1782 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1783 enum tree_code code;
1784 enum tree_code_class cl;
1785
1786 /* X may be NULL if it is the operand of an empty statement expression
1787 ({ }). */
1788 if (x == NULL)
1789 return;
1790
1791 restart:
1792 code = TREE_CODE (x);
1793 cl = TREE_CODE_CLASS (code);
1794
1795 if (warning_candidate_p (x))
1796 *pno_sp = new_tlist (*pno_sp, x, writer);
1797
1798 switch (code)
1799 {
1800 case CONSTRUCTOR:
1801 case SIZEOF_EXPR:
1802 return;
1803
1804 case COMPOUND_EXPR:
1805 case TRUTH_ANDIF_EXPR:
1806 case TRUTH_ORIF_EXPR:
1807 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1808 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1809 warn_for_collisions (tmp_nosp);
1810 merge_tlist (pbefore_sp, tmp_before, 0);
1811 merge_tlist (pbefore_sp, tmp_nosp, 0);
1812 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1813 warn_for_collisions (tmp_list2);
1814 merge_tlist (pbefore_sp, tmp_list3, 0);
1815 merge_tlist (pno_sp, tmp_list2, 0);
1816 return;
1817
1818 case COND_EXPR:
1819 tmp_before = tmp_list2 = 0;
1820 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1821 warn_for_collisions (tmp_list2);
1822 merge_tlist (pbefore_sp, tmp_before, 0);
1823 merge_tlist (pbefore_sp, tmp_list2, 0);
1824
1825 tmp_list3 = tmp_nosp = 0;
1826 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1827 warn_for_collisions (tmp_nosp);
1828 merge_tlist (pbefore_sp, tmp_list3, 0);
1829
1830 tmp_list3 = tmp_list2 = 0;
1831 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1832 warn_for_collisions (tmp_list2);
1833 merge_tlist (pbefore_sp, tmp_list3, 0);
1834 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1835 two first, to avoid warning for (a ? b++ : b++). */
1836 merge_tlist (&tmp_nosp, tmp_list2, 0);
1837 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1838 return;
1839
1840 case PREDECREMENT_EXPR:
1841 case PREINCREMENT_EXPR:
1842 case POSTDECREMENT_EXPR:
1843 case POSTINCREMENT_EXPR:
1844 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1845 return;
1846
1847 case MODIFY_EXPR:
1848 tmp_before = tmp_nosp = tmp_list3 = 0;
1849 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1850 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1851 /* Expressions inside the LHS are not ordered wrt. the sequence points
1852 in the RHS. Example:
1853 *a = (a++, 2)
1854 Despite the fact that the modification of "a" is in the before_sp
1855 list (tmp_before), it conflicts with the use of "a" in the LHS.
1856 We can handle this by adding the contents of tmp_list3
1857 to those of tmp_before, and redoing the collision warnings for that
1858 list. */
1859 add_tlist (&tmp_before, tmp_list3, x, 1);
1860 warn_for_collisions (tmp_before);
1861 /* Exclude the LHS itself here; we first have to merge it into the
1862 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1863 didn't exclude the LHS, we'd get it twice, once as a read and once
1864 as a write. */
1865 add_tlist (pno_sp, tmp_list3, x, 0);
1866 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1867
1868 merge_tlist (pbefore_sp, tmp_before, 0);
1869 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1870 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1871 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1872 return;
1873
1874 case CALL_EXPR:
1875 /* We need to warn about conflicts among arguments and conflicts between
1876 args and the function address. Side effects of the function address,
1877 however, are not ordered by the sequence point of the call. */
1878 {
1879 call_expr_arg_iterator iter;
1880 tree arg;
1881 tmp_before = tmp_nosp = 0;
1882 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1883 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1884 {
1885 tmp_list2 = tmp_list3 = 0;
1886 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1887 merge_tlist (&tmp_list3, tmp_list2, 0);
1888 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1889 }
1890 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1891 warn_for_collisions (tmp_before);
1892 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1893 return;
1894 }
1895
1896 case TREE_LIST:
1897 /* Scan all the list, e.g. indices of multi dimensional array. */
1898 while (x)
1899 {
1900 tmp_before = tmp_nosp = 0;
1901 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1902 merge_tlist (&tmp_nosp, tmp_before, 0);
1903 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1904 x = TREE_CHAIN (x);
1905 }
1906 return;
1907
1908 case SAVE_EXPR:
1909 {
1910 struct tlist_cache *t;
1911 for (t = save_expr_cache; t; t = t->next)
1912 if (candidate_equal_p (t->expr, x))
1913 break;
1914
1915 if (!t)
1916 {
1917 t = XOBNEW (&tlist_obstack, struct tlist_cache);
1918 t->next = save_expr_cache;
1919 t->expr = x;
1920 save_expr_cache = t;
1921
1922 tmp_before = tmp_nosp = 0;
1923 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1924 warn_for_collisions (tmp_nosp);
1925
1926 tmp_list3 = 0;
1927 merge_tlist (&tmp_list3, tmp_nosp, 0);
1928 t->cache_before_sp = tmp_before;
1929 t->cache_after_sp = tmp_list3;
1930 }
1931 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1932 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1933 return;
1934 }
1935
1936 case ADDR_EXPR:
1937 x = TREE_OPERAND (x, 0);
1938 if (DECL_P (x))
1939 return;
1940 writer = 0;
1941 goto restart;
1942
1943 default:
1944 /* For other expressions, simply recurse on their operands.
1945 Manual tail recursion for unary expressions.
1946 Other non-expressions need not be processed. */
1947 if (cl == tcc_unary)
1948 {
1949 x = TREE_OPERAND (x, 0);
1950 writer = 0;
1951 goto restart;
1952 }
1953 else if (IS_EXPR_CODE_CLASS (cl))
1954 {
1955 int lp;
1956 int max = TREE_OPERAND_LENGTH (x);
1957 for (lp = 0; lp < max; lp++)
1958 {
1959 tmp_before = tmp_nosp = 0;
1960 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
1961 merge_tlist (&tmp_nosp, tmp_before, 0);
1962 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1963 }
1964 }
1965 return;
1966 }
1967}
1968
1969/* Try to warn for undefined behavior in EXPR due to missing sequence
1970 points. */
1971
1972DEBUG_FUNCTION void
1973verify_sequence_points (tree expr)
1974{
1975 struct tlist *before_sp = 0, *after_sp = 0;
1976
1977 warned_ids = 0;
1978 save_expr_cache = 0;
1979 if (tlist_firstobj == 0)
1980 {
1981 gcc_obstack_init (&tlist_obstack);
1982 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
1983 }
1984
1985 verify_tree (expr, &before_sp, &after_sp, 0);
1986 warn_for_collisions (after_sp);
1987 obstack_free (&tlist_obstack, tlist_firstobj);
1988}
1989
1990/* Validate the expression after `case' and apply default promotions. */
1991
1992static tree
1993check_case_value (location_t loc, tree value)
1994{
1995 if (value == NULL_TREE)
1996 return value;
1997
1998 if (TREE_CODE (value) == INTEGER_CST)
1999 /* Promote char or short to int. */
2000 value = perform_integral_promotions (value);
2001 else if (value != error_mark_node)
2002 {
2003 error_at (loc, "case label does not reduce to an integer constant");
2004 value = error_mark_node;
2005 }
2006
2007 constant_expression_warning (value);
2008
2009 return value;
2010}
2011
2012/* See if the case values LOW and HIGH are in the range of the original
2013 type (i.e. before the default conversion to int) of the switch testing
2014 expression.
2015 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2016 the type before promoting it. CASE_LOW_P is a pointer to the lower
2017 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2018 if the case is not a case range.
2019 The caller has to make sure that we are not called with NULL for
2020 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
2021 was a case value that doesn't fit into the range of the ORIG_TYPE.
2022 Returns true if the case label is in range of ORIG_TYPE (saturated or
2023 untouched) or false if the label is out of range. */
2024
2025static bool
2026check_case_bounds (location_t loc, tree type, tree orig_type,
2027 tree *case_low_p, tree *case_high_p,
2028 bool *outside_range_p)
2029{
2030 tree min_value, max_value;
2031 tree case_low = *case_low_p;
2032 tree case_high = case_high_p ? *case_high_p : case_low;
2033
2034 /* If there was a problem with the original type, do nothing. */
2035 if (orig_type == error_mark_node)
2036 return true;
2037
2038 min_value = TYPE_MIN_VALUE (orig_type);
2039 max_value = TYPE_MAX_VALUE (orig_type);
2040
2041 /* We'll really need integer constants here. */
2042 case_low = fold (case_low);
2043 case_high = fold (case_high);
2044
2045 /* Case label is less than minimum for type. */
2046 if (tree_int_cst_compare (case_low, min_value) < 0
2047 && tree_int_cst_compare (case_high, min_value) < 0)
2048 {
2049 warning_at (loc, 0, "case label value is less than minimum value "
2050 "for type");
2051 *outside_range_p = true;
2052 return false;
2053 }
2054
2055 /* Case value is greater than maximum for type. */
2056 if (tree_int_cst_compare (case_low, max_value) > 0
2057 && tree_int_cst_compare (case_high, max_value) > 0)
2058 {
2059 warning_at (loc, 0, "case label value exceeds maximum value for type");
2060 *outside_range_p = true;
2061 return false;
2062 }
2063
2064 /* Saturate lower case label value to minimum. */
2065 if (tree_int_cst_compare (case_high, min_value) >= 0
2066 && tree_int_cst_compare (case_low, min_value) < 0)
2067 {
2068 warning_at (loc, 0, "lower value in case label range"
2069 " less than minimum value for type");
2070 *outside_range_p = true;
2071 case_low = min_value;
2072 }
2073
2074 /* Saturate upper case label value to maximum. */
2075 if (tree_int_cst_compare (case_low, max_value) <= 0
2076 && tree_int_cst_compare (case_high, max_value) > 0)
2077 {
2078 warning_at (loc, 0, "upper value in case label range"
2079 " exceeds maximum value for type");
2080 *outside_range_p = true;
2081 case_high = max_value;
2082 }
2083
2084 if (*case_low_p != case_low)
2085 *case_low_p = convert (type, case_low);
2086 if (case_high_p && *case_high_p != case_high)
2087 *case_high_p = convert (type, case_high);
2088
2089 return true;
2090}
2091
2092/* Return an integer type with BITS bits of precision,
2093 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2094
2095tree
2096c_common_type_for_size (unsigned int bits, int unsignedp)
2097{
2098 int i;
2099
2100 if (bits == TYPE_PRECISION (integer_type_node))
2101 return unsignedp ? unsigned_type_node : integer_type_node;
2102
2103 if (bits == TYPE_PRECISION (signed_char_type_node))
2104 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2105
2106 if (bits == TYPE_PRECISION (short_integer_type_node))
2107 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2108
2109 if (bits == TYPE_PRECISION (long_integer_type_node))
2110 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2111
2112 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2113 return (unsignedp ? long_long_unsigned_type_node
2114 : long_long_integer_type_node);
2115
2116 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2117 if (int_n_enabled_p[i]
2118 && bits == int_n_data[i].bitsize)
2119 return (unsignedp ? int_n_trees[i].unsigned_type
2120 : int_n_trees[i].signed_type);
2121
2122 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2123 return (unsignedp ? widest_unsigned_literal_type_node
2124 : widest_integer_literal_type_node);
2125
2126 if (bits <= TYPE_PRECISION (intQI_type_node))
2127 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2128
2129 if (bits <= TYPE_PRECISION (intHI_type_node))
2130 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2131
2132 if (bits <= TYPE_PRECISION (intSI_type_node))
2133 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2134
2135 if (bits <= TYPE_PRECISION (intDI_type_node))
2136 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2137
2138 return NULL_TREE;
2139}
2140
2141/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2142 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2143 and saturating if SATP is nonzero, otherwise not saturating. */
2144
2145tree
2146c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2147 int unsignedp, int satp)
2148{
2149 enum mode_class mclass;
2150 if (ibit == 0)
2151 mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2152 else
2153 mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2154
2155 opt_scalar_mode opt_mode;
2156 scalar_mode mode;
2157 FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2158 {
2159 mode = opt_mode.require ();
2160 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2161 break;
2162 }
2163
2164 if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2165 {
2166 sorry ("GCC cannot support operators with integer types and "
2167 "fixed-point types that have too many integral and "
2168 "fractional bits together");
2169 return NULL_TREE;
2170 }
2171
2172 return c_common_type_for_mode (mode, satp);
2173}
2174
2175/* Used for communication between c_common_type_for_mode and
2176 c_register_builtin_type. */
2177tree registered_builtin_types;
2178
2179/* Return a data type that has machine mode MODE.
2180 If the mode is an integer,
2181 then UNSIGNEDP selects between signed and unsigned types.
2182 If the mode is a fixed-point mode,
2183 then UNSIGNEDP selects between saturating and nonsaturating types. */
2184
2185tree
2186c_common_type_for_mode (machine_mode mode, int unsignedp)
2187{
2188 tree t;
2189 int i;
2190
2191 if (mode == TYPE_MODE (integer_type_node))
2192 return unsignedp ? unsigned_type_node : integer_type_node;
2193
2194 if (mode == TYPE_MODE (signed_char_type_node))
2195 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2196
2197 if (mode == TYPE_MODE (short_integer_type_node))
2198 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2199
2200 if (mode == TYPE_MODE (long_integer_type_node))
2201 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2202
2203 if (mode == TYPE_MODE (long_long_integer_type_node))
2204 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2205
2206 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2207 if (int_n_enabled_p[i]
2208 && mode == int_n_data[i].m)
2209 return (unsignedp ? int_n_trees[i].unsigned_type
2210 : int_n_trees[i].signed_type);
2211
2212 if (mode == QImode)
2213 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2214
2215 if (mode == HImode)
2216 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2217
2218 if (mode == SImode)
2219 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2220
2221 if (mode == DImode)
2222 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2223
2224#if HOST_BITS_PER_WIDE_INT >= 64
2225 if (mode == TYPE_MODE (intTI_type_node))
2226 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2227#endif
2228
2229 if (mode == TYPE_MODE (float_type_node))
2230 return float_type_node;
2231
2232 if (mode == TYPE_MODE (double_type_node))
2233 return double_type_node;
2234
2235 if (mode == TYPE_MODE (long_double_type_node))
2236 return long_double_type_node;
2237
2238 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2239 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2240 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2241 return FLOATN_NX_TYPE_NODE (i);
2242
2243 if (mode == TYPE_MODE (void_type_node))
2244 return void_type_node;
2245
2246 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2247 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2248 {
2249 unsigned int precision
2250 = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2251 return (unsignedp
2252 ? make_unsigned_type (precision)
2253 : make_signed_type (precision));
2254 }
2255
2256 if (COMPLEX_MODE_P (mode))
2257 {
2258 machine_mode inner_mode;
2259 tree inner_type;
2260
2261 if (mode == TYPE_MODE (complex_float_type_node))
2262 return complex_float_type_node;
2263 if (mode == TYPE_MODE (complex_double_type_node))
2264 return complex_double_type_node;
2265 if (mode == TYPE_MODE (complex_long_double_type_node))
2266 return complex_long_double_type_node;
2267
2268 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2269 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2270 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2271 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2272
2273 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2274 return complex_integer_type_node;
2275
2276 inner_mode = GET_MODE_INNER (mode);
2277 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2278 if (inner_type != NULL_TREE)
2279 return build_complex_type (inner_type);
2280 }
2281 else if (VECTOR_MODE_P (mode))
2282 {
2283 machine_mode inner_mode = GET_MODE_INNER (mode);
2284 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2285 if (inner_type != NULL_TREE)
2286 return build_vector_type_for_mode (inner_type, mode);
2287 }
2288
2289 if (mode == TYPE_MODE (dfloat32_type_node))
2290 return dfloat32_type_node;
2291 if (mode == TYPE_MODE (dfloat64_type_node))
2292 return dfloat64_type_node;
2293 if (mode == TYPE_MODE (dfloat128_type_node))
2294 return dfloat128_type_node;
2295
2296 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2297 {
2298 if (mode == TYPE_MODE (short_fract_type_node))
2299 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2300 if (mode == TYPE_MODE (fract_type_node))
2301 return unsignedp ? sat_fract_type_node : fract_type_node;
2302 if (mode == TYPE_MODE (long_fract_type_node))
2303 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2304 if (mode == TYPE_MODE (long_long_fract_type_node))
2305 return unsignedp ? sat_long_long_fract_type_node
2306 : long_long_fract_type_node;
2307
2308 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2309 return unsignedp ? sat_unsigned_short_fract_type_node
2310 : unsigned_short_fract_type_node;
2311 if (mode == TYPE_MODE (unsigned_fract_type_node))
2312 return unsignedp ? sat_unsigned_fract_type_node
2313 : unsigned_fract_type_node;
2314 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2315 return unsignedp ? sat_unsigned_long_fract_type_node
2316 : unsigned_long_fract_type_node;
2317 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2318 return unsignedp ? sat_unsigned_long_long_fract_type_node
2319 : unsigned_long_long_fract_type_node;
2320
2321 if (mode == TYPE_MODE (short_accum_type_node))
2322 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2323 if (mode == TYPE_MODE (accum_type_node))
2324 return unsignedp ? sat_accum_type_node : accum_type_node;
2325 if (mode == TYPE_MODE (long_accum_type_node))
2326 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2327 if (mode == TYPE_MODE (long_long_accum_type_node))
2328 return unsignedp ? sat_long_long_accum_type_node
2329 : long_long_accum_type_node;
2330
2331 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2332 return unsignedp ? sat_unsigned_short_accum_type_node
2333 : unsigned_short_accum_type_node;
2334 if (mode == TYPE_MODE (unsigned_accum_type_node))
2335 return unsignedp ? sat_unsigned_accum_type_node
2336 : unsigned_accum_type_node;
2337 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2338 return unsignedp ? sat_unsigned_long_accum_type_node
2339 : unsigned_long_accum_type_node;
2340 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2341 return unsignedp ? sat_unsigned_long_long_accum_type_node
2342 : unsigned_long_long_accum_type_node;
2343
2344 if (mode == QQmode)
2345 return unsignedp ? sat_qq_type_node : qq_type_node;
2346 if (mode == HQmode)
2347 return unsignedp ? sat_hq_type_node : hq_type_node;
2348 if (mode == SQmode)
2349 return unsignedp ? sat_sq_type_node : sq_type_node;
2350 if (mode == DQmode)
2351 return unsignedp ? sat_dq_type_node : dq_type_node;
2352 if (mode == TQmode)
2353 return unsignedp ? sat_tq_type_node : tq_type_node;
2354
2355 if (mode == UQQmode)
2356 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2357 if (mode == UHQmode)
2358 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2359 if (mode == USQmode)
2360 return unsignedp ? sat_usq_type_node : usq_type_node;
2361 if (mode == UDQmode)
2362 return unsignedp ? sat_udq_type_node : udq_type_node;
2363 if (mode == UTQmode)
2364 return unsignedp ? sat_utq_type_node : utq_type_node;
2365
2366 if (mode == HAmode)
2367 return unsignedp ? sat_ha_type_node : ha_type_node;
2368 if (mode == SAmode)
2369 return unsignedp ? sat_sa_type_node : sa_type_node;
2370 if (mode == DAmode)
2371 return unsignedp ? sat_da_type_node : da_type_node;
2372 if (mode == TAmode)
2373 return unsignedp ? sat_ta_type_node : ta_type_node;
2374
2375 if (mode == UHAmode)
2376 return unsignedp ? sat_uha_type_node : uha_type_node;
2377 if (mode == USAmode)
2378 return unsignedp ? sat_usa_type_node : usa_type_node;
2379 if (mode == UDAmode)
2380 return unsignedp ? sat_uda_type_node : uda_type_node;
2381 if (mode == UTAmode)
2382 return unsignedp ? sat_uta_type_node : uta_type_node;
2383 }
2384
2385 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2386 if (TYPE_MODE (TREE_VALUE (t)) == mode
2387 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
2388 return TREE_VALUE (t);
2389
2390 return NULL_TREE;
2391}
2392
2393tree
2394c_common_unsigned_type (tree type)
2395{
2396 return c_common_signed_or_unsigned_type (1, type);
2397}
2398
2399/* Return a signed type the same as TYPE in other respects. */
2400
2401tree
2402c_common_signed_type (tree type)
2403{
2404 return c_common_signed_or_unsigned_type (0, type);
2405}
2406
2407/* Return a type the same as TYPE except unsigned or
2408 signed according to UNSIGNEDP. */
2409
2410tree
2411c_common_signed_or_unsigned_type (int unsignedp, tree type)
2412{
2413 tree type1;
2414 int i;
2415
2416 /* This block of code emulates the behavior of the old
2417 c_common_unsigned_type. In particular, it returns
2418 long_unsigned_type_node if passed a long, even when a int would
2419 have the same size. This is necessary for warnings to work
2420 correctly in archs where sizeof(int) == sizeof(long) */
2421
2422 type1 = TYPE_MAIN_VARIANT (type);
2423 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2424 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2425 if (type1 == integer_type_node || type1 == unsigned_type_node)
2426 return unsignedp ? unsigned_type_node : integer_type_node;
2427 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2428 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2429 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2430 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2431 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2432 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2433
2434 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2435 if (int_n_enabled_p[i]
2436 && (type1 == int_n_trees[i].unsigned_type
2437 || type1 == int_n_trees[i].signed_type))
2438 return (unsignedp ? int_n_trees[i].unsigned_type
2439 : int_n_trees[i].signed_type);
2440
2441#if HOST_BITS_PER_WIDE_INT >= 64
2442 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2443 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2444#endif
2445 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2446 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2447 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2448 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2449 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2450 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2451 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2452 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2453
2454#define C_COMMON_FIXED_TYPES(NAME) \
2455 if (type1 == short_ ## NAME ## _type_node \
2456 || type1 == unsigned_short_ ## NAME ## _type_node) \
2457 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2458 : short_ ## NAME ## _type_node; \
2459 if (type1 == NAME ## _type_node \
2460 || type1 == unsigned_ ## NAME ## _type_node) \
2461 return unsignedp ? unsigned_ ## NAME ## _type_node \
2462 : NAME ## _type_node; \
2463 if (type1 == long_ ## NAME ## _type_node \
2464 || type1 == unsigned_long_ ## NAME ## _type_node) \
2465 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2466 : long_ ## NAME ## _type_node; \
2467 if (type1 == long_long_ ## NAME ## _type_node \
2468 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2469 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2470 : long_long_ ## NAME ## _type_node;
2471
2472#define C_COMMON_FIXED_MODE_TYPES(NAME) \
2473 if (type1 == NAME ## _type_node \
2474 || type1 == u ## NAME ## _type_node) \
2475 return unsignedp ? u ## NAME ## _type_node \
2476 : NAME ## _type_node;
2477
2478#define C_COMMON_FIXED_TYPES_SAT(NAME) \
2479 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2480 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2481 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2482 : sat_ ## short_ ## NAME ## _type_node; \
2483 if (type1 == sat_ ## NAME ## _type_node \
2484 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2485 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2486 : sat_ ## NAME ## _type_node; \
2487 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2488 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2489 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2490 : sat_ ## long_ ## NAME ## _type_node; \
2491 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2492 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2493 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2494 : sat_ ## long_long_ ## NAME ## _type_node;
2495
2496#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2497 if (type1 == sat_ ## NAME ## _type_node \
2498 || type1 == sat_ ## u ## NAME ## _type_node) \
2499 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2500 : sat_ ## NAME ## _type_node;
2501
2502 C_COMMON_FIXED_TYPES (fract);
2503 C_COMMON_FIXED_TYPES_SAT (fract);
2504 C_COMMON_FIXED_TYPES (accum);
2505 C_COMMON_FIXED_TYPES_SAT (accum);
2506
2507 C_COMMON_FIXED_MODE_TYPES (qq);
2508 C_COMMON_FIXED_MODE_TYPES (hq);
2509 C_COMMON_FIXED_MODE_TYPES (sq);
2510 C_COMMON_FIXED_MODE_TYPES (dq);
2511 C_COMMON_FIXED_MODE_TYPES (tq);
2512 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2513 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2514 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2515 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2516 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2517 C_COMMON_FIXED_MODE_TYPES (ha);
2518 C_COMMON_FIXED_MODE_TYPES (sa);
2519 C_COMMON_FIXED_MODE_TYPES (da);
2520 C_COMMON_FIXED_MODE_TYPES (ta);
2521 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2522 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2523 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2524 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2525
2526 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2527 the precision; they have precision set to match their range, but
2528 may use a wider mode to match an ABI. If we change modes, we may
2529 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2530 the precision as well, so as to yield correct results for
2531 bit-field types. C++ does not have these separate bit-field
2532 types, and producing a signed or unsigned variant of an
2533 ENUMERAL_TYPE may cause other problems as well. */
2534
2535 if (!INTEGRAL_TYPE_P (type)
2536 || TYPE_UNSIGNED (type) == unsignedp)
2537 return type;
2538
2539#define TYPE_OK(node) \
2540 (TYPE_MODE (type) == TYPE_MODE (node) \
2541 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2542 if (TYPE_OK (signed_char_type_node))
2543 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2544 if (TYPE_OK (integer_type_node))
2545 return unsignedp ? unsigned_type_node : integer_type_node;
2546 if (TYPE_OK (short_integer_type_node))
2547 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2548 if (TYPE_OK (long_integer_type_node))
2549 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2550 if (TYPE_OK (long_long_integer_type_node))
2551 return (unsignedp ? long_long_unsigned_type_node
2552 : long_long_integer_type_node);
2553
2554 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2555 if (int_n_enabled_p[i]
2556 && TYPE_MODE (type) == int_n_data[i].m
2557 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2558 return (unsignedp ? int_n_trees[i].unsigned_type
2559 : int_n_trees[i].signed_type);
2560
2561#if HOST_BITS_PER_WIDE_INT >= 64
2562 if (TYPE_OK (intTI_type_node))
2563 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2564#endif
2565 if (TYPE_OK (intDI_type_node))
2566 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2567 if (TYPE_OK (intSI_type_node))
2568 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2569 if (TYPE_OK (intHI_type_node))
2570 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2571 if (TYPE_OK (intQI_type_node))
2572 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2573#undef TYPE_OK
2574
2575 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2576}
2577
2578/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2579
2580tree
2581c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2582{
2583 int i;
2584
2585 /* Extended integer types of the same width as a standard type have
2586 lesser rank, so those of the same width as int promote to int or
2587 unsigned int and are valid for printf formats expecting int or
2588 unsigned int. To avoid such special cases, avoid creating
2589 extended integer types for bit-fields if a standard integer type
2590 is available. */
2591 if (width == TYPE_PRECISION (integer_type_node))
2592 return unsignedp ? unsigned_type_node : integer_type_node;
2593 if (width == TYPE_PRECISION (signed_char_type_node))
2594 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2595 if (width == TYPE_PRECISION (short_integer_type_node))
2596 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2597 if (width == TYPE_PRECISION (long_integer_type_node))
2598 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2599 if (width == TYPE_PRECISION (long_long_integer_type_node))
2600 return (unsignedp ? long_long_unsigned_type_node
2601 : long_long_integer_type_node);
2602 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2603 if (int_n_enabled_p[i]
2604 && width == int_n_data[i].bitsize)
2605 return (unsignedp ? int_n_trees[i].unsigned_type
2606 : int_n_trees[i].signed_type);
2607 return build_nonstandard_integer_type (width, unsignedp);
2608}
2609
2610/* The C version of the register_builtin_type langhook. */
2611
2612void
2613c_register_builtin_type (tree type, const char* name)
2614{
2615 tree decl;
2616
2617 decl = build_decl (UNKNOWN_LOCATION,
2618 TYPE_DECL, get_identifier (name), type);
2619 DECL_ARTIFICIAL (decl) = 1;
2620 if (!TYPE_NAME (type))
2621 TYPE_NAME (type) = decl;
2622 lang_hooks.decls.pushdecl (decl);
2623
2624 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2625}
2626
2627/* Print an error message for invalid operands to arith operation
2628 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2629 RICHLOC is a rich location for the message, containing either
2630 three separate locations for each of the operator and operands
2631
2632 lhs op rhs
2633 ~~~ ^~ ~~~
2634
2635 (C FE), or one location ranging over all over them
2636
2637 lhs op rhs
2638 ~~~~^~~~~~
2639
2640 (C++ FE). */
2641
2642void
2643binary_op_error (rich_location *richloc, enum tree_code code,
2644 tree type0, tree type1)
2645{
2646 const char *opname;
2647
2648 switch (code)
2649 {
2650 case PLUS_EXPR:
2651 opname = "+"; break;
2652 case MINUS_EXPR:
2653 opname = "-"; break;
2654 case MULT_EXPR:
2655 opname = "*"; break;
2656 case MAX_EXPR:
2657 opname = "max"; break;
2658 case MIN_EXPR:
2659 opname = "min"; break;
2660 case EQ_EXPR:
2661 opname = "=="; break;
2662 case NE_EXPR:
2663 opname = "!="; break;
2664 case LE_EXPR:
2665 opname = "<="; break;
2666 case GE_EXPR:
2667 opname = ">="; break;
2668 case LT_EXPR:
2669 opname = "<"; break;
2670 case GT_EXPR:
2671 opname = ">"; break;
2672 case LSHIFT_EXPR:
2673 opname = "<<"; break;
2674 case RSHIFT_EXPR:
2675 opname = ">>"; break;
2676 case TRUNC_MOD_EXPR:
2677 case FLOOR_MOD_EXPR:
2678 opname = "%"; break;
2679 case TRUNC_DIV_EXPR:
2680 case FLOOR_DIV_EXPR:
2681 opname = "/"; break;
2682 case BIT_AND_EXPR:
2683 opname = "&"; break;
2684 case BIT_IOR_EXPR:
2685 opname = "|"; break;
2686 case TRUTH_ANDIF_EXPR:
2687 opname = "&&"; break;
2688 case TRUTH_ORIF_EXPR:
2689 opname = "||"; break;
2690 case BIT_XOR_EXPR:
2691 opname = "^"; break;
2692 default:
2693 gcc_unreachable ();
2694 }
2695 error_at (richloc,
2696 "invalid operands to binary %s (have %qT and %qT)",
2697 opname, type0, type1);
2698}
2699
2700/* Given an expression as a tree, return its original type. Do this
2701 by stripping any conversion that preserves the sign and precision. */
2702static tree
2703expr_original_type (tree expr)
2704{
2705 STRIP_SIGN_NOPS (expr);
2706 return TREE_TYPE (expr);
2707}
2708
2709/* Subroutine of build_binary_op, used for comparison operations.
2710 See if the operands have both been converted from subword integer types
2711 and, if so, perhaps change them both back to their original type.
2712 This function is also responsible for converting the two operands
2713 to the proper common type for comparison.
2714
2715 The arguments of this function are all pointers to local variables
2716 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2717 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2718
2719 LOC is the location of the comparison.
2720
2721 If this function returns non-NULL_TREE, it means that the comparison has
2722 a constant value. What this function returns is an expression for
2723 that value. */
2724
2725tree
2726shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2727 tree *restype_ptr, enum tree_code *rescode_ptr)
2728{
2729 tree type;
2730 tree op0 = *op0_ptr;
2731 tree op1 = *op1_ptr;
2732 int unsignedp0, unsignedp1;
2733 int real1, real2;
2734 tree primop0, primop1;
2735 enum tree_code code = *rescode_ptr;
2736
2737 /* Throw away any conversions to wider types
2738 already present in the operands. */
2739
2740 primop0 = c_common_get_narrower (op0, &unsignedp0);
2741 primop1 = c_common_get_narrower (op1, &unsignedp1);
2742
2743 /* If primopN is first sign-extended from primopN's precision to opN's
2744 precision, then zero-extended from opN's precision to
2745 *restype_ptr precision, shortenings might be invalid. */
2746 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2747 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2748 && !unsignedp0
2749 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2750 primop0 = op0;
2751 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2752 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2753 && !unsignedp1
2754 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2755 primop1 = op1;
2756
2757 /* Handle the case that OP0 does not *contain* a conversion
2758 but it *requires* conversion to FINAL_TYPE. */
2759
2760 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2761 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2762 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2763 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2764
2765 /* If one of the operands must be floated, we cannot optimize. */
2766 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2767 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2768
2769 /* If first arg is constant, swap the args (changing operation
2770 so value is preserved), for canonicalization. Don't do this if
2771 the second arg is 0. */
2772
2773 if (TREE_CONSTANT (primop0)
2774 && !integer_zerop (primop1) && !real_zerop (primop1)
2775 && !fixed_zerop (primop1))
2776 {
2777 std::swap (primop0, primop1);
2778 std::swap (op0, op1);
2779 *op0_ptr = op0;
2780 *op1_ptr = op1;
2781 std::swap (unsignedp0, unsignedp1);
2782 std::swap (real1, real2);
2783
2784 switch (code)
2785 {
2786 case LT_EXPR:
2787 code = GT_EXPR;
2788 break;
2789 case GT_EXPR:
2790 code = LT_EXPR;
2791 break;
2792 case LE_EXPR:
2793 code = GE_EXPR;
2794 break;
2795 case GE_EXPR:
2796 code = LE_EXPR;
2797 break;
2798 default:
2799 break;
2800 }
2801 *rescode_ptr = code;
2802 }
2803
2804 /* If comparing an integer against a constant more bits wide,
2805 maybe we can deduce a value of 1 or 0 independent of the data.
2806 Or else truncate the constant now
2807 rather than extend the variable at run time.
2808
2809 This is only interesting if the constant is the wider arg.
2810 Also, it is not safe if the constant is unsigned and the
2811 variable arg is signed, since in this case the variable
2812 would be sign-extended and then regarded as unsigned.
2813 Our technique fails in this case because the lowest/highest
2814 possible unsigned results don't follow naturally from the
2815 lowest/highest possible values of the variable operand.
2816 For just EQ_EXPR and NE_EXPR there is another technique that
2817 could be used: see if the constant can be faithfully represented
2818 in the other operand's type, by truncating it and reextending it
2819 and see if that preserves the constant's value. */
2820
2821 if (!real1 && !real2
2822 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
2823 && TREE_CODE (primop1) == INTEGER_CST
2824 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2825 {
2826 int min_gt, max_gt, min_lt, max_lt;
2827 tree maxval, minval;
2828 /* 1 if comparison is nominally unsigned. */
2829 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
2830 tree val;
2831
2832 type = c_common_signed_or_unsigned_type (unsignedp0,
2833 TREE_TYPE (primop0));
2834
2835 maxval = TYPE_MAX_VALUE (type);
2836 minval = TYPE_MIN_VALUE (type);
2837
2838 if (unsignedp && !unsignedp0)
2839 *restype_ptr = c_common_signed_type (*restype_ptr);
2840
2841 if (TREE_TYPE (primop1) != *restype_ptr)
2842 {
2843 /* Convert primop1 to target type, but do not introduce
2844 additional overflow. We know primop1 is an int_cst. */
2845 primop1 = force_fit_type (*restype_ptr,
2846 wi::to_wide
2847 (primop1,
2848 TYPE_PRECISION (*restype_ptr)),
2849 0, TREE_OVERFLOW (primop1));
2850 }
2851 if (type != *restype_ptr)
2852 {
2853 minval = convert (*restype_ptr, minval);
2854 maxval = convert (*restype_ptr, maxval);
2855 }
2856
2857 min_gt = tree_int_cst_lt (primop1, minval);
2858 max_gt = tree_int_cst_lt (primop1, maxval);
2859 min_lt = tree_int_cst_lt (minval, primop1);
2860 max_lt = tree_int_cst_lt (maxval, primop1);
2861
2862 val = 0;
2863 /* This used to be a switch, but Genix compiler can't handle that. */
2864 if (code == NE_EXPR)
2865 {
2866 if (max_lt || min_gt)
2867 val = truthvalue_true_node;
2868 }
2869 else if (code == EQ_EXPR)
2870 {
2871 if (max_lt || min_gt)
2872 val = truthvalue_false_node;
2873 }
2874 else if (code == LT_EXPR)
2875 {
2876 if (max_lt)
2877 val = truthvalue_true_node;
2878 if (!min_lt)
2879 val = truthvalue_false_node;
2880 }
2881 else if (code == GT_EXPR)
2882 {
2883 if (min_gt)
2884 val = truthvalue_true_node;
2885 if (!max_gt)
2886 val = truthvalue_false_node;
2887 }
2888 else if (code == LE_EXPR)
2889 {
2890 if (!max_gt)
2891 val = truthvalue_true_node;
2892 if (min_gt)
2893 val = truthvalue_false_node;
2894 }
2895 else if (code == GE_EXPR)
2896 {
2897 if (!min_lt)
2898 val = truthvalue_true_node;
2899 if (max_lt)
2900 val = truthvalue_false_node;
2901 }
2902
2903 /* If primop0 was sign-extended and unsigned comparison specd,
2904 we did a signed comparison above using the signed type bounds.
2905 But the comparison we output must be unsigned.
2906
2907 Also, for inequalities, VAL is no good; but if the signed
2908 comparison had *any* fixed result, it follows that the
2909 unsigned comparison just tests the sign in reverse
2910 (positive values are LE, negative ones GE).
2911 So we can generate an unsigned comparison
2912 against an extreme value of the signed type. */
2913
2914 if (unsignedp && !unsignedp0)
2915 {
2916 if (val != 0)
2917 switch (code)
2918 {
2919 case LT_EXPR:
2920 case GE_EXPR:
2921 primop1 = TYPE_MIN_VALUE (type);
2922 val = 0;
2923 break;
2924
2925 case LE_EXPR:
2926 case GT_EXPR:
2927 primop1 = TYPE_MAX_VALUE (type);
2928 val = 0;
2929 break;
2930
2931 default:
2932 break;
2933 }
2934 type = c_common_unsigned_type (type);
2935 }
2936
2937 if (TREE_CODE (primop0) != INTEGER_CST
2938 /* Don't warn if it's from a (non-system) macro. */
2939 && !(from_macro_expansion_at
2940 (expansion_point_location_if_in_system_header
2941 (EXPR_LOCATION (primop0)))))
2942 {
2943 if (val == truthvalue_false_node)
2944 warning_at (loc, OPT_Wtype_limits,
2945 "comparison is always false due to limited range of data type");
2946 if (val == truthvalue_true_node)
2947 warning_at (loc, OPT_Wtype_limits,
2948 "comparison is always true due to limited range of data type");
2949 }
2950
2951 if (val != 0)
2952 {
2953 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2954 if (TREE_SIDE_EFFECTS (primop0))
2955 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2956 return val;
2957 }
2958
2959 /* Value is not predetermined, but do the comparison
2960 in the type of the operand that is not constant.
2961 TYPE is already properly set. */
2962 }
2963
2964 /* If either arg is decimal float and the other is float, find the
2965 proper common type to use for comparison. */
2966 else if (real1 && real2
2967 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2968 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
2969 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2970
2971 /* If either arg is decimal float and the other is float, fail. */
2972 else if (real1 && real2
2973 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
2974 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
2975 return NULL_TREE;
2976
2977 else if (real1 && real2
2978 && (TYPE_PRECISION (TREE_TYPE (primop0))
2979 == TYPE_PRECISION (TREE_TYPE (primop1))))
2980 type = TREE_TYPE (primop0);
2981
2982 /* If args' natural types are both narrower than nominal type
2983 and both extend in the same manner, compare them
2984 in the type of the wider arg.
2985 Otherwise must actually extend both to the nominal
2986 common type lest different ways of extending
2987 alter the result.
2988 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2989
2990 else if (unsignedp0 == unsignedp1 && real1 == real2
2991 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2992 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2993 {
2994 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2995 type = c_common_signed_or_unsigned_type (unsignedp0
2996 || TYPE_UNSIGNED (*restype_ptr),
2997 type);
2998 /* Make sure shorter operand is extended the right way
2999 to match the longer operand. */
3000 primop0
3001 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3002 TREE_TYPE (primop0)),
3003 primop0);
3004 primop1
3005 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3006 TREE_TYPE (primop1)),
3007 primop1);
3008 }
3009 else
3010 {
3011 /* Here we must do the comparison on the nominal type
3012 using the args exactly as we received them. */
3013 type = *restype_ptr;
3014 primop0 = op0;
3015 primop1 = op1;
3016
3017 if (!real1 && !real2 && integer_zerop (primop1)
3018 && TYPE_UNSIGNED (*restype_ptr))
3019 {
3020 tree value = NULL_TREE;
3021 /* All unsigned values are >= 0, so we warn. However,
3022 if OP0 is a constant that is >= 0, the signedness of
3023 the comparison isn't an issue, so suppress the
3024 warning. */
3025 bool warn =
3026 warn_type_limits && !in_system_header_at (loc)
3027 && !(TREE_CODE (primop0) == INTEGER_CST
3028 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3029 primop0)))
3030 /* Do not warn for enumeration types. */
3031 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3032
3033 switch (code)
3034 {
3035 case GE_EXPR:
3036 if (warn)
3037 warning_at (loc, OPT_Wtype_limits,
3038 "comparison of unsigned expression >= 0 is always true");
3039 value = truthvalue_true_node;
3040 break;
3041
3042 case LT_EXPR:
3043 if (warn)
3044 warning_at (loc, OPT_Wtype_limits,
3045 "comparison of unsigned expression < 0 is always false");
3046 value = truthvalue_false_node;
3047 break;
3048
3049 default:
3050 break;
3051 }
3052
3053 if (value != NULL_TREE)
3054 {
3055 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3056 if (TREE_SIDE_EFFECTS (primop0))
3057 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3058 primop0, value);
3059 return value;
3060 }
3061 }
3062 }
3063
3064 *op0_ptr = convert (type, primop0);
3065 *op1_ptr = convert (type, primop1);
3066
3067 *restype_ptr = truthvalue_type_node;
3068
3069 return NULL_TREE;
3070}
3071
3072/* Return a tree for the sum or difference (RESULTCODE says which)
3073 of pointer PTROP and integer INTOP. */
3074
3075tree
3076pointer_int_sum (location_t loc, enum tree_code resultcode,
3077 tree ptrop, tree intop, bool complain)
3078{
3079 tree size_exp, ret;
3080
3081 /* The result is a pointer of the same type that is being added. */
3082 tree result_type = TREE_TYPE (ptrop);
3083
3084 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3085 {
3086 if (complain && warn_pointer_arith)
3087 pedwarn (loc, OPT_Wpointer_arith,
3088 "pointer of type %<void *%> used in arithmetic");
3089 else if (!complain)
3090 return error_mark_node;
3091 size_exp = integer_one_node;
3092 }
3093 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3094 {
3095 if (complain && warn_pointer_arith)
3096 pedwarn (loc, OPT_Wpointer_arith,
3097 "pointer to a function used in arithmetic");
3098 else if (!complain)
3099 return error_mark_node;
3100 size_exp = integer_one_node;
3101 }
3102 else
3103 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3104
3105 /* We are manipulating pointer values, so we don't need to warn
3106 about relying on undefined signed overflow. We disable the
3107 warning here because we use integer types so fold won't know that
3108 they are really pointers. */
3109 fold_defer_overflow_warnings ();
3110
3111 /* If what we are about to multiply by the size of the elements
3112 contains a constant term, apply distributive law
3113 and multiply that constant term separately.
3114 This helps produce common subexpressions. */
3115 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3116 && !TREE_CONSTANT (intop)
3117 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3118 && TREE_CONSTANT (size_exp)
3119 /* If the constant comes from pointer subtraction,
3120 skip this optimization--it would cause an error. */
3121 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3122 /* If the constant is unsigned, and smaller than the pointer size,
3123 then we must skip this optimization. This is because it could cause
3124 an overflow error if the constant is negative but INTOP is not. */
3125 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3126 || (TYPE_PRECISION (TREE_TYPE (intop))
3127 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3128 {
3129 enum tree_code subcode = resultcode;
3130 tree int_type = TREE_TYPE (intop);
3131 if (TREE_CODE (intop) == MINUS_EXPR)
3132 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3133 /* Convert both subexpression types to the type of intop,
3134 because weird cases involving pointer arithmetic
3135 can result in a sum or difference with different type args. */
3136 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3137 subcode, ptrop,
3138 convert (int_type, TREE_OPERAND (intop, 1)),
3139 true);
3140 intop = convert (int_type, TREE_OPERAND (intop, 0));
3141 }
3142
3143 /* Convert the integer argument to a type the same size as sizetype
3144 so the multiply won't overflow spuriously. */
3145 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3146 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3147 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3148 TYPE_UNSIGNED (sizetype)), intop);
3149
3150 /* Replace the integer argument with a suitable product by the object size.
3151 Do this multiplication as signed, then convert to the appropriate type
3152 for the pointer operation and disregard an overflow that occurred only
3153 because of the sign-extension change in the latter conversion. */
3154 {
3155 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3156 convert (TREE_TYPE (intop), size_exp));
3157 intop = convert (sizetype, t);
3158 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3159 intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3160 }
3161
3162 /* Create the sum or difference. */
3163 if (resultcode == MINUS_EXPR)
3164 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3165
3166 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3167
3168 fold_undefer_and_ignore_overflow_warnings ();
3169
3170 return ret;
3171}
3172
3173/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3174 and if NON_CONST is known not to be permitted in an evaluated part
3175 of a constant expression. */
3176
3177tree
3178c_wrap_maybe_const (tree expr, bool non_const)
3179{
3180 bool nowarning = TREE_NO_WARNING (expr);
3181 location_t loc = EXPR_LOCATION (expr);
3182
3183 /* This should never be called for C++. */
3184 if (c_dialect_cxx ())
3185 gcc_unreachable ();
3186
3187 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3188 STRIP_TYPE_NOPS (expr);
3189 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3190 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3191 if (nowarning)
3192 TREE_NO_WARNING (expr) = 1;
3193 protected_set_expr_location (expr, loc);
3194
3195 return expr;
3196}
3197
3198/* Return whether EXPR is a declaration whose address can never be
3199 NULL. */
3200
3201bool
3202decl_with_nonnull_addr_p (const_tree expr)
3203{
3204 return (DECL_P (expr)
3205 && (TREE_CODE (expr) == PARM_DECL
3206 || TREE_CODE (expr) == LABEL_DECL
3207 || !DECL_WEAK (expr)));
3208}
3209
3210/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3211 or for an `if' or `while' statement or ?..: exp. It should already
3212 have been validated to be of suitable type; otherwise, a bad
3213 diagnostic may result.
3214
3215 The EXPR is located at LOCATION.
3216
3217 This preparation consists of taking the ordinary
3218 representation of an expression expr and producing a valid tree
3219 boolean expression describing whether expr is nonzero. We could
3220 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3221 but we optimize comparisons, &&, ||, and !.
3222
3223 The resulting type should always be `truthvalue_type_node'. */
3224
3225tree
3226c_common_truthvalue_conversion (location_t location, tree expr)
3227{
3228 switch (TREE_CODE (expr))
3229 {
3230 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3231 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3232 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3233 case ORDERED_EXPR: case UNORDERED_EXPR:
3234 if (TREE_TYPE (expr) == truthvalue_type_node)
3235 return expr;
3236 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3237 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3238 goto ret;
3239
3240 case TRUTH_ANDIF_EXPR:
3241 case TRUTH_ORIF_EXPR:
3242 case TRUTH_AND_EXPR:
3243 case TRUTH_OR_EXPR:
3244 case TRUTH_XOR_EXPR:
3245 if (TREE_TYPE (expr) == truthvalue_type_node)
3246 return expr;
3247 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3248 c_common_truthvalue_conversion (location,
3249 TREE_OPERAND (expr, 0)),
3250 c_common_truthvalue_conversion (location,
3251 TREE_OPERAND (expr, 1)));
3252 goto ret;
3253
3254 case TRUTH_NOT_EXPR:
3255 if (TREE_TYPE (expr) == truthvalue_type_node)
3256 return expr;
3257 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3258 c_common_truthvalue_conversion (location,
3259 TREE_OPERAND (expr, 0)));
3260 goto ret;
3261
3262 case ERROR_MARK:
3263 return expr;
3264
3265 case INTEGER_CST:
3266 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3267 && !integer_zerop (expr)
3268 && !integer_onep (expr))
3269 warning_at (location, OPT_Wint_in_bool_context,
3270 "enum constant in boolean context");
3271 return integer_zerop (expr) ? truthvalue_false_node
3272 : truthvalue_true_node;
3273
3274 case REAL_CST:
3275 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3276 ? truthvalue_true_node
3277 : truthvalue_false_node;
3278
3279 case FIXED_CST:
3280 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3281 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3282 ? truthvalue_true_node
3283 : truthvalue_false_node;
3284
3285 case FUNCTION_DECL:
3286 expr = build_unary_op (location, ADDR_EXPR, expr, false);
3287 /* Fall through. */
3288
3289 case ADDR_EXPR:
3290 {
3291 tree inner = TREE_OPERAND (expr, 0);
3292 if (decl_with_nonnull_addr_p (inner))
3293 {
3294 /* Common Ada/Pascal programmer's mistake. */
3295 warning_at (location,
3296 OPT_Waddress,
3297 "the address of %qD will always evaluate as %<true%>",
3298 inner);
3299 return truthvalue_true_node;
3300 }
3301 break;
3302 }
3303
3304 case COMPLEX_EXPR:
3305 expr = build_binary_op (EXPR_LOCATION (expr),
3306 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3307 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3308 c_common_truthvalue_conversion (location,
3309 TREE_OPERAND (expr, 0)),
3310 c_common_truthvalue_conversion (location,
3311 TREE_OPERAND (expr, 1)),
3312 false);
3313 goto ret;
3314
3315 case NEGATE_EXPR:
3316 case ABS_EXPR:
3317 case FLOAT_EXPR:
3318 case EXCESS_PRECISION_EXPR:
3319 /* These don't change whether an object is nonzero or zero. */
3320 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3321
3322 case LROTATE_EXPR:
3323 case RROTATE_EXPR:
3324 /* These don't change whether an object is zero or nonzero, but
3325 we can't ignore them if their second arg has side-effects. */
3326 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3327 {
3328 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3329 TREE_OPERAND (expr, 1),
3330 c_common_truthvalue_conversion
3331 (location, TREE_OPERAND (expr, 0)));
3332 goto ret;
3333 }
3334 else
3335 return c_common_truthvalue_conversion (location,
3336 TREE_OPERAND (expr, 0));
3337
3338 case MULT_EXPR:
3339 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3340 "%<*%> in boolean context, suggest %<&&%> instead");
3341 break;
3342
3343 case LSHIFT_EXPR:
3344 /* We will only warn on signed shifts here, because the majority of
3345 false positive warnings happen in code where unsigned arithmetic
3346 was used in anticipation of a possible overflow.
3347 Furthermore, if we see an unsigned type here we know that the
3348 result of the shift is not subject to integer promotion rules. */
3349 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3350 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3351 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3352 "%<<<%> in boolean context, did you mean %<<%> ?");
3353 break;
3354
3355 case COND_EXPR:
3356 if (warn_int_in_bool_context
3357 && !from_macro_definition_at (EXPR_LOCATION (expr)))
3358 {
3359 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3360 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3361 if (TREE_CODE (val1) == INTEGER_CST
3362 && TREE_CODE (val2) == INTEGER_CST
3363 && !integer_zerop (val1)
3364 && !integer_zerop (val2)
3365 && (!integer_onep (val1)
3366 || !integer_onep (val2)))
3367 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3368 "?: using integer constants in boolean context, "
3369 "the expression will always evaluate to %<true%>");
3370 else if ((TREE_CODE (val1) == INTEGER_CST
3371 && !integer_zerop (val1)
3372 && !integer_onep (val1))
3373 || (TREE_CODE (val2) == INTEGER_CST
3374 && !integer_zerop (val2)
3375 && !integer_onep (val2)))
3376 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3377 "?: using integer constants in boolean context");
3378 }
3379 /* Distribute the conversion into the arms of a COND_EXPR. */
3380 if (c_dialect_cxx ())
3381 /* Avoid premature folding. */
3382 break;
3383 else
3384 {
3385 int w = warn_int_in_bool_context;
3386 warn_int_in_bool_context = 0;
3387 /* Folding will happen later for C. */
3388 expr = build3 (COND_EXPR, truthvalue_type_node,
3389 TREE_OPERAND (expr, 0),
3390 c_common_truthvalue_conversion (location,
3391 TREE_OPERAND (expr, 1)),
3392 c_common_truthvalue_conversion (location,
3393 TREE_OPERAND (expr, 2)));
3394 warn_int_in_bool_context = w;
3395 goto ret;
3396 }
3397
3398 CASE_CONVERT:
3399 {
3400 tree totype = TREE_TYPE (expr);
3401 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3402
3403 if (POINTER_TYPE_P (totype)
3404 && !c_inhibit_evaluation_warnings
3405 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3406 {
3407 tree inner = expr;
3408 STRIP_NOPS (inner);
3409
3410 if (DECL_P (inner))
3411 warning_at (location,
3412 OPT_Waddress,
3413 "the compiler can assume that the address of "
3414 "%qD will always evaluate to %<true%>",
3415 inner);
3416 }
3417
3418 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3419 since that affects how `default_conversion' will behave. */
3420 if (TREE_CODE (totype) == REFERENCE_TYPE
3421 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3422 break;
3423 /* Don't strip a conversion from C++0x scoped enum, since they
3424 don't implicitly convert to other types. */
3425 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3426 && ENUM_IS_SCOPED (fromtype))
3427 break;
3428 /* If this isn't narrowing the argument, we can ignore it. */
3429 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3430 return c_common_truthvalue_conversion (location,
3431 TREE_OPERAND (expr, 0));
3432 }
3433 break;
3434
3435 case MODIFY_EXPR:
3436 if (!TREE_NO_WARNING (expr)
3437 && warn_parentheses)
3438 {
3439 warning_at (location, OPT_Wparentheses,
3440 "suggest parentheses around assignment used as "
3441 "truth value");
3442 TREE_NO_WARNING (expr) = 1;
3443 }
3444 break;
3445
3446 default:
3447 break;
3448 }
3449
3450 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3451 {
3452 tree t = save_expr (expr);
3453 expr = (build_binary_op
3454 (EXPR_LOCATION (expr),
3455 (TREE_SIDE_EFFECTS (expr)
3456 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3457 c_common_truthvalue_conversion
3458 (location,
3459 build_unary_op (location, REALPART_EXPR, t, false)),
3460 c_common_truthvalue_conversion
3461 (location,
3462 build_unary_op (location, IMAGPART_EXPR, t, false)),
3463 false));
3464 goto ret;
3465 }
3466
3467 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3468 {
3469 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3470 FCONST0 (TYPE_MODE
3471 (TREE_TYPE (expr))));
3472 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3473 }
3474 else
3475 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3476
3477 ret:
3478 protected_set_expr_location (expr, location);
3479 return expr;
3480}
3481
3482static void def_builtin_1 (enum built_in_function fncode,
3483 const char *name,
3484 enum built_in_class fnclass,
3485 tree fntype, tree libtype,
3486 bool both_p, bool fallback_p, bool nonansi_p,
3487 tree fnattrs, bool implicit_p);
3488
3489
3490/* Apply the TYPE_QUALS to the new DECL. */
3491
3492void
3493c_apply_type_quals_to_decl (int type_quals, tree decl)
3494{
3495 tree type = TREE_TYPE (decl);
3496
3497 if (type == error_mark_node)
3498 return;
3499
3500 if ((type_quals & TYPE_QUAL_CONST)
3501 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3502 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3503 constructor can produce constant init, so rely on cp_finish_decl to
3504 clear TREE_READONLY if the variable has non-constant init. */
3505 TREE_READONLY (decl) = 1;
3506 if (type_quals & TYPE_QUAL_VOLATILE)
3507 {
3508 TREE_SIDE_EFFECTS (decl) = 1;
3509 TREE_THIS_VOLATILE (decl) = 1;
3510 }
3511 if (type_quals & TYPE_QUAL_RESTRICT)
3512 {
3513 while (type && TREE_CODE (type) == ARRAY_TYPE)
3514 /* Allow 'restrict' on arrays of pointers.
3515 FIXME currently we just ignore it. */
3516 type = TREE_TYPE (type);
3517 if (!type
3518 || !POINTER_TYPE_P (type)
3519 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3520 error ("invalid use of %<restrict%>");
3521 }
3522}
3523
3524/* Return the typed-based alias set for T, which may be an expression
3525 or a type. Return -1 if we don't do anything special. */
3526
3527alias_set_type
3528c_common_get_alias_set (tree t)
3529{
3530 /* For VLAs, use the alias set of the element type rather than the
3531 default of alias set 0 for types compared structurally. */
3532 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3533 {
3534 if (TREE_CODE (t) == ARRAY_TYPE)
3535 return get_alias_set (TREE_TYPE (t));
3536 return -1;
3537 }
3538
3539 /* That's all the expressions we handle specially. */
3540 if (!TYPE_P (t))
3541 return -1;
3542
3543 /* The C standard guarantees that any object may be accessed via an
3544 lvalue that has character type. */
3545 if (t == char_type_node
3546 || t == signed_char_type_node
3547 || t == unsigned_char_type_node)
3548 return 0;
3549
3550 /* The C standard specifically allows aliasing between signed and
3551 unsigned variants of the same type. We treat the signed
3552 variant as canonical. */
3553 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3554 {
3555 tree t1 = c_common_signed_type (t);
3556
3557 /* t1 == t can happen for boolean nodes which are always unsigned. */
3558 if (t1 != t)
3559 return get_alias_set (t1);
3560 }
3561
3562 return -1;
3563}
3564
3565/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3566 the IS_SIZEOF parameter indicates which operator is being applied.
3567 The COMPLAIN flag controls whether we should diagnose possibly
3568 ill-formed constructs or not. LOC is the location of the SIZEOF or
3569 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3570 a type in any context should be returned, rather than the normal
3571 alignment for that type. */
3572
3573tree
3574c_sizeof_or_alignof_type (location_t loc,
3575 tree type, bool is_sizeof, bool min_alignof,
3576 int complain)
3577{
3578 const char *op_name;
3579 tree value = NULL;
3580 enum tree_code type_code = TREE_CODE (type);
3581
3582 op_name = is_sizeof ? "sizeof" : "__alignof__";
3583
3584 if (type_code == FUNCTION_TYPE)
3585 {
3586 if (is_sizeof)
3587 {
3588 if (complain && warn_pointer_arith)
3589 pedwarn (loc, OPT_Wpointer_arith,
3590 "invalid application of %<sizeof%> to a function type");
3591 else if (!complain)
3592 return error_mark_node;
3593 value = size_one_node;
3594 }
3595 else
3596 {
3597 if (complain)
3598 {
3599 if (c_dialect_cxx ())
3600 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3601 "%<alignof%> applied to a function type");
3602 else
3603 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3604 "%<_Alignof%> applied to a function type");
3605 }
3606 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3607 }
3608 }
3609 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3610 {
3611 if (type_code == VOID_TYPE
3612 && complain && warn_pointer_arith)
3613 pedwarn (loc, OPT_Wpointer_arith,
3614 "invalid application of %qs to a void type", op_name);
3615 else if (!complain)
3616 return error_mark_node;
3617 value = size_one_node;
3618 }
3619 else if (!COMPLETE_TYPE_P (type)
3620 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
3621 {
3622 if (complain)
3623 error_at (loc, "invalid application of %qs to incomplete type %qT",
3624 op_name, type);
3625 return error_mark_node;
3626 }
3627 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3628 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3629 {
3630 if (complain)
3631 error_at (loc, "invalid application of %qs to array type %qT of "
3632 "incomplete element type", op_name, type);
3633 return error_mark_node;
3634 }
3635 else
3636 {
3637 if (is_sizeof)
3638 /* Convert in case a char is more than one unit. */
3639 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3640 size_int (TYPE_PRECISION (char_type_node)
3641 / BITS_PER_UNIT));
3642 else if (min_alignof)
3643 value = size_int (min_align_of_type (type));
3644 else
3645 value = size_int (TYPE_ALIGN_UNIT (type));
3646 }
3647
3648 /* VALUE will have the middle-end integer type sizetype.
3649 However, we should really return a value of type `size_t',
3650 which is just a typedef for an ordinary integer type. */
3651 value = fold_convert_loc (loc, size_type_node, value);
3652
3653 return value;
3654}
3655
3656/* Implement the __alignof keyword: Return the minimum required
3657 alignment of EXPR, measured in bytes. For VAR_DECLs,
3658 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
3659 from an "aligned" __attribute__ specification). LOC is the
3660 location of the ALIGNOF operator. */
3661
3662tree
3663c_alignof_expr (location_t loc, tree expr)
3664{
3665 tree t;
3666
3667 if (VAR_OR_FUNCTION_DECL_P (expr))
3668 t = size_int (DECL_ALIGN_UNIT (expr));
3669
3670 else if (TREE_CODE (expr) == COMPONENT_REF
3671 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3672 {
3673 error_at (loc, "%<__alignof%> applied to a bit-field");
3674 t = size_one_node;
3675 }
3676 else if (TREE_CODE (expr) == COMPONENT_REF
3677 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3678 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
3679
3680 else if (INDIRECT_REF_P (expr))
3681 {
3682 tree t = TREE_OPERAND (expr, 0);
3683 tree best = t;
3684 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3685
3686 while (CONVERT_EXPR_P (t)
3687 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3688 {
3689 int thisalign;
3690
3691 t = TREE_OPERAND (t, 0);
3692 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3693 if (thisalign > bestalign)
3694 best = t, bestalign = thisalign;
3695 }
3696 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
3697 }
3698 else
3699 return c_alignof (loc, TREE_TYPE (expr));
3700
3701 return fold_convert_loc (loc, size_type_node, t);
3702}
3703
3704/* Handle C and C++ default attributes. */
3705
3706enum built_in_attribute
3707{
3708#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3709#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3710#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
3711#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3712#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3713#include "builtin-attrs.def"
3714#undef DEF_ATTR_NULL_TREE
3715#undef DEF_ATTR_INT
3716#undef DEF_ATTR_STRING
3717#undef DEF_ATTR_IDENT
3718#undef DEF_ATTR_TREE_LIST
3719 ATTR_LAST
3720};
3721
3722static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3723
3724static void c_init_attributes (void);
3725
3726enum c_builtin_type
3727{
3728#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3729#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3730#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3731#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3732#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3733#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3734#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3735#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3736 ARG6) NAME,
3737#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3738 ARG6, ARG7) NAME,
3739#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3740 ARG6, ARG7, ARG8) NAME,
3741#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3742 ARG6, ARG7, ARG8, ARG9) NAME,
3743#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3744 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3745#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3746 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
3747#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3748#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3749#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3750#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3751#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3752#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3753 NAME,
3754#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3755 ARG6) NAME,
3756#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3757 ARG6, ARG7) NAME,
3758#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3759#include "builtin-types.def"
3760#undef DEF_PRIMITIVE_TYPE
3761#undef DEF_FUNCTION_TYPE_0
3762#undef DEF_FUNCTION_TYPE_1
3763#undef DEF_FUNCTION_TYPE_2
3764#undef DEF_FUNCTION_TYPE_3
3765#undef DEF_FUNCTION_TYPE_4
3766#undef DEF_FUNCTION_TYPE_5
3767#undef DEF_FUNCTION_TYPE_6
3768#undef DEF_FUNCTION_TYPE_7
3769#undef DEF_FUNCTION_TYPE_8
3770#undef DEF_FUNCTION_TYPE_9
3771#undef DEF_FUNCTION_TYPE_10
3772#undef DEF_FUNCTION_TYPE_11
3773#undef DEF_FUNCTION_TYPE_VAR_0
3774#undef DEF_FUNCTION_TYPE_VAR_1
3775#undef DEF_FUNCTION_TYPE_VAR_2
3776#undef DEF_FUNCTION_TYPE_VAR_3
3777#undef DEF_FUNCTION_TYPE_VAR_4
3778#undef DEF_FUNCTION_TYPE_VAR_5
3779#undef DEF_FUNCTION_TYPE_VAR_6
3780#undef DEF_FUNCTION_TYPE_VAR_7
3781#undef DEF_POINTER_TYPE
3782 BT_LAST
3783};
3784
3785typedef enum c_builtin_type builtin_type;
3786
3787/* A temporary array for c_common_nodes_and_builtins. Used in
3788 communication with def_fn_type. */
3789static tree builtin_types[(int) BT_LAST + 1];
3790
3791/* A helper function for c_common_nodes_and_builtins. Build function type
3792 for DEF with return type RET and N arguments. If VAR is true, then the
3793 function should be variadic after those N arguments.
3794
3795 Takes special care not to ICE if any of the types involved are
3796 error_mark_node, which indicates that said type is not in fact available
3797 (see builtin_type_for_size). In which case the function type as a whole
3798 should be error_mark_node. */
3799
3800static void
3801def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3802{
3803 tree t;
3804 tree *args = XALLOCAVEC (tree, n);
3805 va_list list;
3806 int i;
3807
3808 va_start (list, n);
3809 for (i = 0; i < n; ++i)
3810 {
3811 builtin_type a = (builtin_type) va_arg (list, int);
3812 t = builtin_types[a];
3813 if (t == error_mark_node)
3814 goto egress;
3815 args[i] = t;
3816 }
3817
3818 t = builtin_types[ret];
3819 if (t == error_mark_node)
3820 goto egress;
3821 if (var)
3822 t = build_varargs_function_type_array (t, n, args);
3823 else
3824 t = build_function_type_array (t, n, args);
3825
3826 egress:
3827 builtin_types[def] = t;
3828 va_end (list);
3829}
3830
3831/* Build builtin functions common to both C and C++ language
3832 frontends. */
3833
3834static void
3835c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3836{
3837#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3838 builtin_types[ENUM] = VALUE;
3839#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3840 def_fn_type (ENUM, RETURN, 0, 0);
3841#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3842 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3843#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3844 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3845#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3846 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3847#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3848 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3849#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3850 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3851#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3852 ARG6) \
3853 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3854#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3855 ARG6, ARG7) \
3856 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3857#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3858 ARG6, ARG7, ARG8) \
3859 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3860 ARG7, ARG8);
3861#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3862 ARG6, ARG7, ARG8, ARG9) \
3863 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3864 ARG7, ARG8, ARG9);
3865#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3866 ARG6, ARG7, ARG8, ARG9, ARG10) \
3867 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3868 ARG7, ARG8, ARG9, ARG10);
3869#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3870 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3871 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3872 ARG7, ARG8, ARG9, ARG10, ARG11);
3873#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3874 def_fn_type (ENUM, RETURN, 1, 0);
3875#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3876 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3877#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3878 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
3879#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3880 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
3881#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3882 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
3883#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3884 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3885#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3886 ARG6) \
3887 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3888#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3889 ARG6, ARG7) \
3890 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
3891#define DEF_POINTER_TYPE(ENUM, TYPE) \
3892 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
3893
3894#include "builtin-types.def"
3895
3896#undef DEF_PRIMITIVE_TYPE
3897#undef DEF_FUNCTION_TYPE_0
3898#undef DEF_FUNCTION_TYPE_1
3899#undef DEF_FUNCTION_TYPE_2
3900#undef DEF_FUNCTION_TYPE_3
3901#undef DEF_FUNCTION_TYPE_4
3902#undef DEF_FUNCTION_TYPE_5
3903#undef DEF_FUNCTION_TYPE_6
3904#undef DEF_FUNCTION_TYPE_7
3905#undef DEF_FUNCTION_TYPE_8
3906#undef DEF_FUNCTION_TYPE_9
3907#undef DEF_FUNCTION_TYPE_10
3908#undef DEF_FUNCTION_TYPE_11
3909#undef DEF_FUNCTION_TYPE_VAR_0
3910#undef DEF_FUNCTION_TYPE_VAR_1
3911#undef DEF_FUNCTION_TYPE_VAR_2
3912#undef DEF_FUNCTION_TYPE_VAR_3
3913#undef DEF_FUNCTION_TYPE_VAR_4
3914#undef DEF_FUNCTION_TYPE_VAR_5
3915#undef DEF_FUNCTION_TYPE_VAR_6
3916#undef DEF_FUNCTION_TYPE_VAR_7
3917#undef DEF_POINTER_TYPE
3918 builtin_types[(int) BT_LAST] = NULL_TREE;
3919
3920 c_init_attributes ();
3921
3922#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
3923 NONANSI_P, ATTRS, IMPLICIT, COND) \
3924 if (NAME && COND) \
3925 def_builtin_1 (ENUM, NAME, CLASS, \
3926 builtin_types[(int) TYPE], \
3927 builtin_types[(int) LIBTYPE], \
3928 BOTH_P, FALLBACK_P, NONANSI_P, \
3929 built_in_attributes[(int) ATTRS], IMPLICIT);
3930#include "builtins.def"
3931
3932 targetm.init_builtins ();
3933
3934 build_common_builtin_nodes ();
3935}
3936
3937/* Like get_identifier, but avoid warnings about null arguments when
3938 the argument may be NULL for targets where GCC lacks stdint.h type
3939 information. */
3940
3941static inline tree
3942c_get_ident (const char *id)
3943{
3944 return get_identifier (id);
3945}
3946
3947/* Build tree nodes and builtin functions common to both C and C++ language
3948 frontends. */
3949
3950void
3951c_common_nodes_and_builtins (void)
3952{
3953 int char16_type_size;
3954 int char32_type_size;
3955 int wchar_type_size;
3956 tree array_domain_type;
3957 tree va_list_ref_type_node;
3958 tree va_list_arg_type_node;
3959 int i;
3960
3961 build_common_tree_nodes (flag_signed_char);
3962
3963 /* Define `int' and `char' first so that dbx will output them first. */
3964 record_builtin_type (RID_INT, NULL, integer_type_node);
3965 record_builtin_type (RID_CHAR, "char", char_type_node);
3966
3967 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3968 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3969 but not C. Are the conditionals here needed? */
3970 if (c_dialect_cxx ())
3971 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3972 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3973 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3974 record_builtin_type (RID_MAX, "long unsigned int",
3975 long_unsigned_type_node);
3976
3977 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3978 {
3979 char name[25];
3980
3981 sprintf (name, "__int%d", int_n_data[i].bitsize);
3982 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
3983 int_n_trees[i].signed_type);
3984 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
3985 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
3986 }
3987
3988 if (c_dialect_cxx ())
3989 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3990 record_builtin_type (RID_MAX, "long long int",
3991 long_long_integer_type_node);
3992 record_builtin_type (RID_MAX, "long long unsigned int",
3993 long_long_unsigned_type_node);
3994 if (c_dialect_cxx ())
3995 record_builtin_type (RID_MAX, "long long unsigned",
3996 long_long_unsigned_type_node);
3997 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
3998 record_builtin_type (RID_MAX, "short unsigned int",
3999 short_unsigned_type_node);
4000 if (c_dialect_cxx ())
4001 record_builtin_type (RID_MAX, "unsigned short",
4002 short_unsigned_type_node);
4003
4004 /* Define both `signed char' and `unsigned char'. */
4005 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4006 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4007
4008 /* These are types that c_common_type_for_size and
4009 c_common_type_for_mode use. */
4010 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4011 TYPE_DECL, NULL_TREE,
4012 intQI_type_node));
4013 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4014 TYPE_DECL, NULL_TREE,
4015 intHI_type_node));
4016 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4017 TYPE_DECL, NULL_TREE,
4018 intSI_type_node));
4019 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4020 TYPE_DECL, NULL_TREE,
4021 intDI_type_node));
4022#if HOST_BITS_PER_WIDE_INT >= 64
4023 /* Note that this is different than the __int128 type that's part of
4024 the generic __intN support. */
4025 if (targetm.scalar_mode_supported_p (TImode))
4026 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4027 TYPE_DECL,
4028 get_identifier ("__int128_t"),
4029 intTI_type_node));
4030#endif
4031 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4032 TYPE_DECL, NULL_TREE,
4033 unsigned_intQI_type_node));
4034 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4035 TYPE_DECL, NULL_TREE,
4036 unsigned_intHI_type_node));
4037 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4038 TYPE_DECL, NULL_TREE,
4039 unsigned_intSI_type_node));
4040 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4041 TYPE_DECL, NULL_TREE,
4042 unsigned_intDI_type_node));
4043#if HOST_BITS_PER_WIDE_INT >= 64
4044 if (targetm.scalar_mode_supported_p (TImode))
4045 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4046 TYPE_DECL,
4047 get_identifier ("__uint128_t"),
4048 unsigned_intTI_type_node));
4049#endif
4050
4051 /* Create the widest literal types. */
4052 if (targetm.scalar_mode_supported_p (TImode))
4053 {
4054 widest_integer_literal_type_node = intTI_type_node;
4055 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4056 }
4057 else
4058 {
4059 widest_integer_literal_type_node = intDI_type_node;
4060 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4061 }
4062
4063 signed_size_type_node = c_common_signed_type (size_type_node);
4064
4065 pid_type_node =
4066 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4067
4068 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4069 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4070 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4071
4072 if (!c_dialect_cxx ())
4073 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4074 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4075 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4076 FLOATN_NX_TYPE_NODE (i));
4077
4078 /* Only supported decimal floating point extension if the target
4079 actually supports underlying modes. */
4080 if (targetm.scalar_mode_supported_p (SDmode)
4081 && targetm.scalar_mode_supported_p (DDmode)
4082 && targetm.scalar_mode_supported_p (TDmode))
4083 {
4084 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4085 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4086 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4087 }
4088
4089 if (targetm.fixed_point_supported_p ())
4090 {
4091 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4092 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4093 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4094 record_builtin_type (RID_MAX, "long long _Fract",
4095 long_long_fract_type_node);
4096 record_builtin_type (RID_MAX, "unsigned short _Fract",
4097 unsigned_short_fract_type_node);
4098 record_builtin_type (RID_MAX, "unsigned _Fract",
4099 unsigned_fract_type_node);
4100 record_builtin_type (RID_MAX, "unsigned long _Fract",
4101 unsigned_long_fract_type_node);
4102 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4103 unsigned_long_long_fract_type_node);
4104 record_builtin_type (RID_MAX, "_Sat short _Fract",
4105 sat_short_fract_type_node);
4106 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4107 record_builtin_type (RID_MAX, "_Sat long _Fract",
4108 sat_long_fract_type_node);
4109 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4110 sat_long_long_fract_type_node);
4111 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4112 sat_unsigned_short_fract_type_node);
4113 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4114 sat_unsigned_fract_type_node);
4115 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4116 sat_unsigned_long_fract_type_node);
4117 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4118 sat_unsigned_long_long_fract_type_node);
4119 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4120 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4121 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4122 record_builtin_type (RID_MAX, "long long _Accum",
4123 long_long_accum_type_node);
4124 record_builtin_type (RID_MAX, "unsigned short _Accum",
4125 unsigned_short_accum_type_node);
4126 record_builtin_type (RID_MAX, "unsigned _Accum",
4127 unsigned_accum_type_node);
4128 record_builtin_type (RID_MAX, "unsigned long _Accum",
4129 unsigned_long_accum_type_node);
4130 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4131 unsigned_long_long_accum_type_node);
4132 record_builtin_type (RID_MAX, "_Sat short _Accum",
4133 sat_short_accum_type_node);
4134 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4135 record_builtin_type (RID_MAX, "_Sat long _Accum",
4136 sat_long_accum_type_node);
4137 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4138 sat_long_long_accum_type_node);
4139 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4140 sat_unsigned_short_accum_type_node);
4141 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4142 sat_unsigned_accum_type_node);
4143 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4144 sat_unsigned_long_accum_type_node);
4145 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4146 sat_unsigned_long_long_accum_type_node);
4147
4148 }
4149
4150 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4151 TYPE_DECL,
4152 get_identifier ("complex int"),
4153 complex_integer_type_node));
4154 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4155 TYPE_DECL,
4156 get_identifier ("complex float"),
4157 complex_float_type_node));
4158 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4159 TYPE_DECL,
4160 get_identifier ("complex double"),
4161 complex_double_type_node));
4162 lang_hooks.decls.pushdecl
4163 (build_decl (UNKNOWN_LOCATION,
4164 TYPE_DECL, get_identifier ("complex long double"),
4165 complex_long_double_type_node));
4166
4167 if (!c_dialect_cxx ())
4168 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4169 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4170 {
4171 char buf[30];
4172 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4173 floatn_nx_types[i].extended ? "x" : "");
4174 lang_hooks.decls.pushdecl
4175 (build_decl (UNKNOWN_LOCATION,
4176 TYPE_DECL,
4177 get_identifier (buf),
4178 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4179 }
4180
4181 if (c_dialect_cxx ())
4182 {
4183 /* For C++, make fileptr_type_node a distinct void * type until
4184 FILE type is defined. Likewise for const struct tm*. */
4185 for (unsigned i = 0;
4186 i < sizeof (builtin_structptr_types)
4187 / sizeof (builtin_structptr_type);
4188 ++i)
4189 builtin_structptr_types[i].node =
4190 build_variant_type_copy (builtin_structptr_types[i].base);
4191
4192 }
4193
4194 record_builtin_type (RID_VOID, NULL, void_type_node);
4195
4196 /* Set the TYPE_NAME for any variants that were built before
4197 record_builtin_type gave names to the built-in types. */
4198 {
4199 tree void_name = TYPE_NAME (void_type_node);
4200 TYPE_NAME (void_type_node) = NULL_TREE;
4201 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4202 = void_name;
4203 TYPE_NAME (void_type_node) = void_name;
4204 }
4205
4206 void_list_node = build_void_list_node ();
4207
4208 /* Make a type to be the domain of a few array types
4209 whose domains don't really matter.
4210 200 is small enough that it always fits in size_t
4211 and large enough that it can hold most function names for the
4212 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4213 array_domain_type = build_index_type (size_int (200));
4214
4215 /* Make a type for arrays of characters.
4216 With luck nothing will ever really depend on the length of this
4217 array type. */
4218 char_array_type_node
4219 = build_array_type (char_type_node, array_domain_type);
4220
4221 string_type_node = build_pointer_type (char_type_node);
4222 const_string_type_node
4223 = build_pointer_type (build_qualified_type
4224 (char_type_node, TYPE_QUAL_CONST));
4225
4226 /* This is special for C++ so functions can be overloaded. */
4227 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4228 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4229 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4230 underlying_wchar_type_node = wchar_type_node;
4231 if (c_dialect_cxx ())
4232 {
4233 if (TYPE_UNSIGNED (wchar_type_node))
4234 wchar_type_node = make_unsigned_type (wchar_type_size);
4235 else
4236 wchar_type_node = make_signed_type (wchar_type_size);
4237 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4238 }
4239
4240 /* This is for wide string constants. */
4241 wchar_array_type_node
4242 = build_array_type (wchar_type_node, array_domain_type);
4243
4244 /* Define 'char16_t'. */
4245 char16_type_node = get_identifier (CHAR16_TYPE);
4246 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4247 char16_type_size = TYPE_PRECISION (char16_type_node);
4248 if (c_dialect_cxx ())
4249 {
4250 char16_type_node = make_unsigned_type (char16_type_size);
4251
4252 if (cxx_dialect >= cxx11)
4253 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4254 }
4255
4256 /* This is for UTF-16 string constants. */
4257 char16_array_type_node
4258 = build_array_type (char16_type_node, array_domain_type);
4259
4260 /* Define 'char32_t'. */
4261 char32_type_node = get_identifier (CHAR32_TYPE);
4262 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4263 char32_type_size =