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