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