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