]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-family/c-common.c
macro.c (_cpp_builtin_macro_text): Correct wording of two warnings.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2013 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "c-pragma.h"
28 #include "ggc.h"
29 #include "c-common.h"
30 #include "c-objc.h"
31 #include "tm_p.h"
32 #include "obstack.h"
33 #include "cpplib.h"
34 #include "target.h"
35 #include "common/common-target.h"
36 #include "langhooks.h"
37 #include "tree-inline.h"
38 #include "toplev.h"
39 #include "diagnostic.h"
40 #include "tree-iterator.h"
41 #include "hashtab.h"
42 #include "opts.h"
43 #include "cgraph.h"
44 #include "target-def.h"
45
46 cpp_reader *parse_in; /* Declared in c-pragma.h. */
47
48 /* The following symbols are subsumed in the c_global_trees array, and
49 listed here individually for documentation purposes.
50
51 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
52
53 tree short_integer_type_node;
54 tree long_integer_type_node;
55 tree long_long_integer_type_node;
56 tree int128_integer_type_node;
57
58 tree short_unsigned_type_node;
59 tree long_unsigned_type_node;
60 tree long_long_unsigned_type_node;
61 tree int128_unsigned_type_node;
62
63 tree truthvalue_type_node;
64 tree truthvalue_false_node;
65 tree truthvalue_true_node;
66
67 tree ptrdiff_type_node;
68
69 tree unsigned_char_type_node;
70 tree signed_char_type_node;
71 tree wchar_type_node;
72
73 tree char16_type_node;
74 tree char32_type_node;
75
76 tree float_type_node;
77 tree double_type_node;
78 tree long_double_type_node;
79
80 tree complex_integer_type_node;
81 tree complex_float_type_node;
82 tree complex_double_type_node;
83 tree complex_long_double_type_node;
84
85 tree dfloat32_type_node;
86 tree dfloat64_type_node;
87 tree_dfloat128_type_node;
88
89 tree intQI_type_node;
90 tree intHI_type_node;
91 tree intSI_type_node;
92 tree intDI_type_node;
93 tree intTI_type_node;
94
95 tree unsigned_intQI_type_node;
96 tree unsigned_intHI_type_node;
97 tree unsigned_intSI_type_node;
98 tree unsigned_intDI_type_node;
99 tree unsigned_intTI_type_node;
100
101 tree widest_integer_literal_type_node;
102 tree widest_unsigned_literal_type_node;
103
104 Nodes for types `void *' and `const void *'.
105
106 tree ptr_type_node, const_ptr_type_node;
107
108 Nodes for types `char *' and `const char *'.
109
110 tree string_type_node, const_string_type_node;
111
112 Type `char[SOMENUMBER]'.
113 Used when an array of char is needed and the size is irrelevant.
114
115 tree char_array_type_node;
116
117 Type `int[SOMENUMBER]' or something like it.
118 Used when an array of int needed and the size is irrelevant.
119
120 tree int_array_type_node;
121
122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
124
125 tree wchar_array_type_node;
126
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
129
130 tree char16_array_type_node;
131
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
134
135 tree char32_array_type_node;
136
137 Type `int ()' -- used for implicit declaration of functions.
138
139 tree default_function_type;
140
141 A VOID_TYPE node, packaged in a TREE_LIST.
142
143 tree void_list_node;
144
145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
148
149 tree function_name_decl_node;
150 tree pretty_function_name_decl_node;
151 tree c99_function_name_decl_node;
152
153 Stack of nested function name VAR_DECLs.
154
155 tree saved_function_name_decls;
156
157 */
158
159 tree c_global_trees[CTI_MAX];
160 \f
161 /* Switches common to the C front ends. */
162
163 /* Nonzero means don't output line number information. */
164
165 char flag_no_line_commands;
166
167 /* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
169
170 char flag_no_output;
171
172 /* Nonzero means dump macros in some fashion. */
173
174 char flag_dump_macros;
175
176 /* Nonzero means pass #include lines through to the output. */
177
178 char flag_dump_includes;
179
180 /* Nonzero means process PCH files while preprocessing. */
181
182 bool flag_pch_preprocess;
183
184 /* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
186
187 const char *pch_file;
188
189 /* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191 int flag_iso;
192
193 /* C/ObjC language option variables. */
194
195
196 /* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
198
199 int flag_cond_mismatch;
200
201 /* Nonzero means enable C89 Amendment 1 features. */
202
203 int flag_isoc94;
204
205 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
206
207 int flag_isoc99;
208
209 /* Nonzero means use the ISO C11 dialect of C. */
210
211 int flag_isoc11;
212
213 /* Nonzero means that we have builtin functions, and main is an int. */
214
215 int flag_hosted = 1;
216
217
218 /* ObjC language option variables. */
219
220
221 /* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
224
225 int print_struct_values;
226
227 /* Tells the compiler what is the constant string class for ObjC. */
228
229 const char *constant_string_class_name;
230
231
232 /* C++ language option variables. */
233
234
235 /* Nonzero means generate separate instantiation control files and
236 juggle them at link time. */
237
238 int flag_use_repository;
239
240 /* The C++ dialect being used. C++98 is the default. */
241
242 enum cxx_dialect cxx_dialect = cxx98;
243
244 /* Maximum template instantiation depth. This limit exists to limit the
245 time it takes to notice excessively recursive template instantiations.
246
247 The default is lower than the 1024 recommended by the C++0x standard
248 because G++ runs out of stack before 1024 with highly recursive template
249 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
250
251 int max_tinst_depth = 900;
252
253 /* The elements of `ridpointers' are identifier nodes for the reserved
254 type names and storage classes. It is indexed by a RID_... value. */
255 tree *ridpointers;
256
257 tree (*make_fname_decl) (location_t, tree, int);
258
259 /* Nonzero means don't warn about problems that occur when the code is
260 executed. */
261 int c_inhibit_evaluation_warnings;
262
263 /* Whether we are building a boolean conversion inside
264 convert_for_assignment, or some other late binary operation. If
265 build_binary_op is called for C (from code shared by C and C++) in
266 this case, then the operands have already been folded and the
267 result will not be folded again, so C_MAYBE_CONST_EXPR should not
268 be generated. */
269 bool in_late_binary_op;
270
271 /* Whether lexing has been completed, so subsequent preprocessor
272 errors should use the compiler's input_location. */
273 bool done_lexing = false;
274
275 /* Information about how a function name is generated. */
276 struct fname_var_t
277 {
278 tree *const decl; /* pointer to the VAR_DECL. */
279 const unsigned rid; /* RID number for the identifier. */
280 const int pretty; /* How pretty is it? */
281 };
282
283 /* The three ways of getting then name of the current function. */
284
285 const struct fname_var_t fname_vars[] =
286 {
287 /* C99 compliant __func__, must be first. */
288 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
289 /* GCC __FUNCTION__ compliant. */
290 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
291 /* GCC __PRETTY_FUNCTION__ compliant. */
292 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
293 {NULL, 0, 0},
294 };
295
296 /* Global visibility options. */
297 struct visibility_flags visibility_options;
298
299 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
300 static tree check_case_value (tree);
301 static bool check_case_bounds (tree, tree, tree *, tree *);
302
303 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
304 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
305 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
310 int, bool *);
311 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
312 int, bool *);
313 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
314 bool *);
315 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_always_inline_attribute (tree *, tree, tree, int,
319 bool *);
320 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_transparent_union_attribute (tree *, tree, tree,
330 int, bool *);
331 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
337 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
338 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
341 static tree handle_visibility_attribute (tree *, tree, tree, int,
342 bool *);
343 static tree handle_tls_model_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_no_instrument_function_attribute (tree *, tree,
346 tree, int, bool *);
347 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_deprecated_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_vector_size_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
369 static tree ignore_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
380
381 static void check_function_nonnull (tree, int, tree *);
382 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
383 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
384 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
385 static int resort_field_decl_cmp (const void *, const void *);
386
387 /* Reserved words. The third field is a mask: keywords are disabled
388 if they match the mask.
389
390 Masks for languages:
391 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
392 C --std=c99: D_CXXONLY | D_OBJC
393 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
394 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
395 C++ --std=c0x: D_CONLY | D_OBJC
396 ObjC++ is like C++ except that D_OBJC is not set
397
398 If -fno-asm is used, D_ASM is added to the mask. If
399 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
400 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
401 In C with -Wc++-compat, we warn if D_CXXWARN is set.
402
403 Note the complication of the D_CXX_OBJC keywords. These are
404 reserved words such as 'class'. In C++, 'class' is a reserved
405 word. In Objective-C++ it is too. In Objective-C, it is a
406 reserved word too, but only if it follows an '@' sign.
407 */
408 const struct c_common_resword c_common_reswords[] =
409 {
410 { "_Alignas", RID_ALIGNAS, D_CONLY },
411 { "_Alignof", RID_ALIGNOF, D_CONLY },
412 { "_Bool", RID_BOOL, D_CONLY },
413 { "_Complex", RID_COMPLEX, 0 },
414 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
415 { "_Cilk_sync", RID_CILK_SYNC, 0 },
416 { "_Imaginary", RID_IMAGINARY, D_CONLY },
417 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
418 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
419 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
420 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
421 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
422 { "_Sat", RID_SAT, D_CONLY | D_EXT },
423 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
424 { "_Noreturn", RID_NORETURN, D_CONLY },
425 { "_Generic", RID_GENERIC, D_CONLY },
426 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
427 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
428 { "__alignof", RID_ALIGNOF, 0 },
429 { "__alignof__", RID_ALIGNOF, 0 },
430 { "__asm", RID_ASM, 0 },
431 { "__asm__", RID_ASM, 0 },
432 { "__attribute", RID_ATTRIBUTE, 0 },
433 { "__attribute__", RID_ATTRIBUTE, 0 },
434 { "__bases", RID_BASES, D_CXXONLY },
435 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
436 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
437 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
438 { "__builtin_offsetof", RID_OFFSETOF, 0 },
439 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
440 { "__builtin_va_arg", RID_VA_ARG, 0 },
441 { "__complex", RID_COMPLEX, 0 },
442 { "__complex__", RID_COMPLEX, 0 },
443 { "__const", RID_CONST, 0 },
444 { "__const__", RID_CONST, 0 },
445 { "__decltype", RID_DECLTYPE, D_CXXONLY },
446 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
447 { "__extension__", RID_EXTENSION, 0 },
448 { "__func__", RID_C99_FUNCTION_NAME, 0 },
449 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
450 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
451 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
452 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
453 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
454 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
455 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
456 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
457 { "__imag", RID_IMAGPART, 0 },
458 { "__imag__", RID_IMAGPART, 0 },
459 { "__inline", RID_INLINE, 0 },
460 { "__inline__", RID_INLINE, 0 },
461 { "__int128", RID_INT128, 0 },
462 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
463 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
464 { "__is_class", RID_IS_CLASS, D_CXXONLY },
465 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
466 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
467 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
468 { "__is_final", RID_IS_FINAL, D_CXXONLY },
469 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
470 { "__is_pod", RID_IS_POD, D_CXXONLY },
471 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
472 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
473 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
474 { "__is_union", RID_IS_UNION, D_CXXONLY },
475 { "__label__", RID_LABEL, 0 },
476 { "__null", RID_NULL, 0 },
477 { "__real", RID_REALPART, 0 },
478 { "__real__", RID_REALPART, 0 },
479 { "__restrict", RID_RESTRICT, 0 },
480 { "__restrict__", RID_RESTRICT, 0 },
481 { "__signed", RID_SIGNED, 0 },
482 { "__signed__", RID_SIGNED, 0 },
483 { "__thread", RID_THREAD, 0 },
484 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
485 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
486 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
487 { "__typeof", RID_TYPEOF, 0 },
488 { "__typeof__", RID_TYPEOF, 0 },
489 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
490 { "__volatile", RID_VOLATILE, 0 },
491 { "__volatile__", RID_VOLATILE, 0 },
492 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
493 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
494 { "asm", RID_ASM, D_ASM },
495 { "auto", RID_AUTO, 0 },
496 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
497 { "break", RID_BREAK, 0 },
498 { "case", RID_CASE, 0 },
499 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
500 { "char", RID_CHAR, 0 },
501 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
502 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
503 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
504 { "const", RID_CONST, 0 },
505 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
507 { "continue", RID_CONTINUE, 0 },
508 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "default", RID_DEFAULT, 0 },
510 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
511 { "do", RID_DO, 0 },
512 { "double", RID_DOUBLE, 0 },
513 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
514 { "else", RID_ELSE, 0 },
515 { "enum", RID_ENUM, 0 },
516 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
517 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
518 { "extern", RID_EXTERN, 0 },
519 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
520 { "float", RID_FLOAT, 0 },
521 { "for", RID_FOR, 0 },
522 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
523 { "goto", RID_GOTO, 0 },
524 { "if", RID_IF, 0 },
525 { "inline", RID_INLINE, D_EXT89 },
526 { "int", RID_INT, 0 },
527 { "long", RID_LONG, 0 },
528 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
529 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
530 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
531 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
532 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
533 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
534 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
535 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
536 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
537 { "register", RID_REGISTER, 0 },
538 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
539 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
540 { "return", RID_RETURN, 0 },
541 { "short", RID_SHORT, 0 },
542 { "signed", RID_SIGNED, 0 },
543 { "sizeof", RID_SIZEOF, 0 },
544 { "static", RID_STATIC, 0 },
545 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
546 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
547 { "struct", RID_STRUCT, 0 },
548 { "switch", RID_SWITCH, 0 },
549 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
550 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
551 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
552 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
553 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
554 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
555 { "typedef", RID_TYPEDEF, 0 },
556 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
557 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
558 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
559 { "union", RID_UNION, 0 },
560 { "unsigned", RID_UNSIGNED, 0 },
561 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
562 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
563 { "void", RID_VOID, 0 },
564 { "volatile", RID_VOLATILE, 0 },
565 { "wchar_t", RID_WCHAR, D_CXXONLY },
566 { "while", RID_WHILE, 0 },
567 /* These Objective-C keywords are recognized only immediately after
568 an '@'. */
569 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
570 { "defs", RID_AT_DEFS, D_OBJC },
571 { "encode", RID_AT_ENCODE, D_OBJC },
572 { "end", RID_AT_END, D_OBJC },
573 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
574 { "interface", RID_AT_INTERFACE, D_OBJC },
575 { "protocol", RID_AT_PROTOCOL, D_OBJC },
576 { "selector", RID_AT_SELECTOR, D_OBJC },
577 { "finally", RID_AT_FINALLY, D_OBJC },
578 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
579 { "optional", RID_AT_OPTIONAL, D_OBJC },
580 { "required", RID_AT_REQUIRED, D_OBJC },
581 { "property", RID_AT_PROPERTY, D_OBJC },
582 { "package", RID_AT_PACKAGE, D_OBJC },
583 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
584 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
585 /* These are recognized only in protocol-qualifier context
586 (see above) */
587 { "bycopy", RID_BYCOPY, D_OBJC },
588 { "byref", RID_BYREF, D_OBJC },
589 { "in", RID_IN, D_OBJC },
590 { "inout", RID_INOUT, D_OBJC },
591 { "oneway", RID_ONEWAY, D_OBJC },
592 { "out", RID_OUT, D_OBJC },
593 /* These are recognized inside a property attribute list */
594 { "assign", RID_ASSIGN, D_OBJC },
595 { "copy", RID_COPY, D_OBJC },
596 { "getter", RID_GETTER, D_OBJC },
597 { "nonatomic", RID_NONATOMIC, D_OBJC },
598 { "readonly", RID_READONLY, D_OBJC },
599 { "readwrite", RID_READWRITE, D_OBJC },
600 { "retain", RID_RETAIN, D_OBJC },
601 { "setter", RID_SETTER, D_OBJC },
602 };
603
604 const unsigned int num_c_common_reswords =
605 sizeof c_common_reswords / sizeof (struct c_common_resword);
606
607 /* Table of machine-independent attributes common to all C-like languages. */
608 const struct attribute_spec c_common_attribute_table[] =
609 {
610 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
611 affects_type_identity } */
612 { "packed", 0, 0, false, false, false,
613 handle_packed_attribute , false},
614 { "nocommon", 0, 0, true, false, false,
615 handle_nocommon_attribute, false},
616 { "common", 0, 0, true, false, false,
617 handle_common_attribute, false },
618 /* FIXME: logically, noreturn attributes should be listed as
619 "false, true, true" and apply to function types. But implementing this
620 would require all the places in the compiler that use TREE_THIS_VOLATILE
621 on a decl to identify non-returning functions to be located and fixed
622 to check the function type instead. */
623 { "noreturn", 0, 0, true, false, false,
624 handle_noreturn_attribute, false },
625 { "volatile", 0, 0, true, false, false,
626 handle_noreturn_attribute, false },
627 { "noinline", 0, 0, true, false, false,
628 handle_noinline_attribute, false },
629 { "noclone", 0, 0, true, false, false,
630 handle_noclone_attribute, false },
631 { "leaf", 0, 0, true, false, false,
632 handle_leaf_attribute, false },
633 { "always_inline", 0, 0, true, false, false,
634 handle_always_inline_attribute, false },
635 { "gnu_inline", 0, 0, true, false, false,
636 handle_gnu_inline_attribute, false },
637 { "artificial", 0, 0, true, false, false,
638 handle_artificial_attribute, false },
639 { "flatten", 0, 0, true, false, false,
640 handle_flatten_attribute, false },
641 { "used", 0, 0, true, false, false,
642 handle_used_attribute, false },
643 { "unused", 0, 0, false, false, false,
644 handle_unused_attribute, false },
645 { "externally_visible", 0, 0, true, false, false,
646 handle_externally_visible_attribute, false },
647 /* The same comments as for noreturn attributes apply to const ones. */
648 { "const", 0, 0, true, false, false,
649 handle_const_attribute, false },
650 { "transparent_union", 0, 0, false, false, false,
651 handle_transparent_union_attribute, false },
652 { "constructor", 0, 1, true, false, false,
653 handle_constructor_attribute, false },
654 { "destructor", 0, 1, true, false, false,
655 handle_destructor_attribute, false },
656 { "mode", 1, 1, false, true, false,
657 handle_mode_attribute, false },
658 { "section", 1, 1, true, false, false,
659 handle_section_attribute, false },
660 { "aligned", 0, 1, false, false, false,
661 handle_aligned_attribute, false },
662 { "weak", 0, 0, true, false, false,
663 handle_weak_attribute, false },
664 { "ifunc", 1, 1, true, false, false,
665 handle_ifunc_attribute, false },
666 { "alias", 1, 1, true, false, false,
667 handle_alias_attribute, false },
668 { "weakref", 0, 1, true, false, false,
669 handle_weakref_attribute, false },
670 { "no_instrument_function", 0, 0, true, false, false,
671 handle_no_instrument_function_attribute,
672 false },
673 { "malloc", 0, 0, true, false, false,
674 handle_malloc_attribute, false },
675 { "returns_twice", 0, 0, true, false, false,
676 handle_returns_twice_attribute, false },
677 { "no_stack_limit", 0, 0, true, false, false,
678 handle_no_limit_stack_attribute, false },
679 { "pure", 0, 0, true, false, false,
680 handle_pure_attribute, false },
681 { "transaction_callable", 0, 0, false, true, false,
682 handle_tm_attribute, false },
683 { "transaction_unsafe", 0, 0, false, true, false,
684 handle_tm_attribute, false },
685 { "transaction_safe", 0, 0, false, true, false,
686 handle_tm_attribute, false },
687 { "transaction_may_cancel_outer", 0, 0, false, true, false,
688 handle_tm_attribute, false },
689 /* ??? These two attributes didn't make the transition from the
690 Intel language document to the multi-vendor language document. */
691 { "transaction_pure", 0, 0, false, true, false,
692 handle_tm_attribute, false },
693 { "transaction_wrap", 1, 1, true, false, false,
694 handle_tm_wrap_attribute, false },
695 /* For internal use (marking of builtins) only. The name contains space
696 to prevent its usage in source code. */
697 { "no vops", 0, 0, true, false, false,
698 handle_novops_attribute, false },
699 { "deprecated", 0, 1, false, false, false,
700 handle_deprecated_attribute, false },
701 { "vector_size", 1, 1, false, true, false,
702 handle_vector_size_attribute, false },
703 { "visibility", 1, 1, false, false, false,
704 handle_visibility_attribute, false },
705 { "tls_model", 1, 1, true, false, false,
706 handle_tls_model_attribute, false },
707 { "nonnull", 0, -1, false, true, true,
708 handle_nonnull_attribute, false },
709 { "nothrow", 0, 0, true, false, false,
710 handle_nothrow_attribute, false },
711 { "may_alias", 0, 0, false, true, false, NULL, false },
712 { "cleanup", 1, 1, true, false, false,
713 handle_cleanup_attribute, false },
714 { "warn_unused_result", 0, 0, false, true, true,
715 handle_warn_unused_result_attribute, false },
716 { "sentinel", 0, 1, false, true, true,
717 handle_sentinel_attribute, false },
718 /* For internal use (marking of builtins) only. The name contains space
719 to prevent its usage in source code. */
720 { "type generic", 0, 0, false, true, true,
721 handle_type_generic_attribute, false },
722 { "alloc_size", 1, 2, false, true, true,
723 handle_alloc_size_attribute, false },
724 { "cold", 0, 0, true, false, false,
725 handle_cold_attribute, false },
726 { "hot", 0, 0, true, false, false,
727 handle_hot_attribute, false },
728 { "no_address_safety_analysis",
729 0, 0, true, false, false,
730 handle_no_address_safety_analysis_attribute,
731 false },
732 { "no_sanitize_address", 0, 0, true, false, false,
733 handle_no_sanitize_address_attribute,
734 false },
735 { "no_sanitize_undefined", 0, 0, true, false, false,
736 handle_no_sanitize_undefined_attribute,
737 false },
738 { "warning", 1, 1, true, false, false,
739 handle_error_attribute, false },
740 { "error", 1, 1, true, false, false,
741 handle_error_attribute, false },
742 { "target", 1, -1, true, false, false,
743 handle_target_attribute, false },
744 { "optimize", 1, -1, true, false, false,
745 handle_optimize_attribute, false },
746 /* For internal use only. The leading '*' both prevents its usage in
747 source code and signals that it may be overridden by machine tables. */
748 { "*tm regparm", 0, 0, false, true, true,
749 ignore_attribute, false },
750 { "no_split_stack", 0, 0, true, false, false,
751 handle_no_split_stack_attribute, false },
752 /* For internal use (marking of builtins and runtime functions) only.
753 The name contains space to prevent its usage in source code. */
754 { "fn spec", 1, 1, false, true, true,
755 handle_fnspec_attribute, false },
756 { "warn_unused", 0, 0, false, false, false,
757 handle_warn_unused_attribute, false },
758 { "returns_nonnull", 0, 0, false, true, true,
759 handle_returns_nonnull_attribute, false },
760 { "omp declare simd", 0, -1, true, false, false,
761 handle_omp_declare_simd_attribute, false },
762 { "omp declare target", 0, 0, true, false, false,
763 handle_omp_declare_target_attribute, false },
764 { "bnd_variable_size", 0, 0, true, false, false,
765 handle_bnd_variable_size_attribute, false },
766 { "bnd_legacy", 0, 0, true, false, false,
767 handle_bnd_legacy, false },
768 { NULL, 0, 0, false, false, false, NULL, false }
769 };
770
771 /* Give the specifications for the format attributes, used by C and all
772 descendants. */
773
774 const struct attribute_spec c_common_format_attribute_table[] =
775 {
776 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
777 affects_type_identity } */
778 { "format", 3, 3, false, true, true,
779 handle_format_attribute, false },
780 { "format_arg", 1, 1, false, true, true,
781 handle_format_arg_attribute, false },
782 { NULL, 0, 0, false, false, false, NULL, false }
783 };
784
785 /* Return identifier for address space AS. */
786
787 const char *
788 c_addr_space_name (addr_space_t as)
789 {
790 int rid = RID_FIRST_ADDR_SPACE + as;
791 gcc_assert (ridpointers [rid]);
792 return IDENTIFIER_POINTER (ridpointers [rid]);
793 }
794
795 /* Push current bindings for the function name VAR_DECLS. */
796
797 void
798 start_fname_decls (void)
799 {
800 unsigned ix;
801 tree saved = NULL_TREE;
802
803 for (ix = 0; fname_vars[ix].decl; ix++)
804 {
805 tree decl = *fname_vars[ix].decl;
806
807 if (decl)
808 {
809 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
810 saved);
811 *fname_vars[ix].decl = NULL_TREE;
812 }
813 }
814 if (saved || saved_function_name_decls)
815 /* Normally they'll have been NULL, so only push if we've got a
816 stack, or they are non-NULL. */
817 saved_function_name_decls = tree_cons (saved, NULL_TREE,
818 saved_function_name_decls);
819 }
820
821 /* Finish up the current bindings, adding them into the current function's
822 statement tree. This must be done _before_ finish_stmt_tree is called.
823 If there is no current function, we must be at file scope and no statements
824 are involved. Pop the previous bindings. */
825
826 void
827 finish_fname_decls (void)
828 {
829 unsigned ix;
830 tree stmts = NULL_TREE;
831 tree stack = saved_function_name_decls;
832
833 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
834 append_to_statement_list (TREE_VALUE (stack), &stmts);
835
836 if (stmts)
837 {
838 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
839
840 if (TREE_CODE (*bodyp) == BIND_EXPR)
841 bodyp = &BIND_EXPR_BODY (*bodyp);
842
843 append_to_statement_list_force (*bodyp, &stmts);
844 *bodyp = stmts;
845 }
846
847 for (ix = 0; fname_vars[ix].decl; ix++)
848 *fname_vars[ix].decl = NULL_TREE;
849
850 if (stack)
851 {
852 /* We had saved values, restore them. */
853 tree saved;
854
855 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
856 {
857 tree decl = TREE_PURPOSE (saved);
858 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
859
860 *fname_vars[ix].decl = decl;
861 }
862 stack = TREE_CHAIN (stack);
863 }
864 saved_function_name_decls = stack;
865 }
866
867 /* Return the text name of the current function, suitably prettified
868 by PRETTY_P. Return string must be freed by caller. */
869
870 const char *
871 fname_as_string (int pretty_p)
872 {
873 const char *name = "top level";
874 char *namep;
875 int vrb = 2, len;
876 cpp_string cstr = { 0, 0 }, strname;
877
878 if (!pretty_p)
879 {
880 name = "";
881 vrb = 0;
882 }
883
884 if (current_function_decl)
885 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
886
887 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
888
889 namep = XNEWVEC (char, len);
890 snprintf (namep, len, "\"%s\"", name);
891 strname.text = (unsigned char *) namep;
892 strname.len = len - 1;
893
894 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
895 {
896 XDELETEVEC (namep);
897 return (const char *) cstr.text;
898 }
899
900 return namep;
901 }
902
903 /* Return the VAR_DECL for a const char array naming the current
904 function. If the VAR_DECL has not yet been created, create it
905 now. RID indicates how it should be formatted and IDENTIFIER_NODE
906 ID is its name (unfortunately C and C++ hold the RID values of
907 keywords in different places, so we can't derive RID from ID in
908 this language independent code. LOC is the location of the
909 function. */
910
911 tree
912 fname_decl (location_t loc, unsigned int rid, tree id)
913 {
914 unsigned ix;
915 tree decl = NULL_TREE;
916
917 for (ix = 0; fname_vars[ix].decl; ix++)
918 if (fname_vars[ix].rid == rid)
919 break;
920
921 decl = *fname_vars[ix].decl;
922 if (!decl)
923 {
924 /* If a tree is built here, it would normally have the lineno of
925 the current statement. Later this tree will be moved to the
926 beginning of the function and this line number will be wrong.
927 To avoid this problem set the lineno to 0 here; that prevents
928 it from appearing in the RTL. */
929 tree stmts;
930 location_t saved_location = input_location;
931 input_location = UNKNOWN_LOCATION;
932
933 stmts = push_stmt_list ();
934 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
935 stmts = pop_stmt_list (stmts);
936 if (!IS_EMPTY_STMT (stmts))
937 saved_function_name_decls
938 = tree_cons (decl, stmts, saved_function_name_decls);
939 *fname_vars[ix].decl = decl;
940 input_location = saved_location;
941 }
942 if (!ix && !current_function_decl)
943 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
944
945 return decl;
946 }
947
948 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
949
950 tree
951 fix_string_type (tree value)
952 {
953 int length = TREE_STRING_LENGTH (value);
954 int nchars;
955 tree e_type, i_type, a_type;
956
957 /* Compute the number of elements, for the array type. */
958 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
959 {
960 nchars = length;
961 e_type = char_type_node;
962 }
963 else if (TREE_TYPE (value) == char16_array_type_node)
964 {
965 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
966 e_type = char16_type_node;
967 }
968 else if (TREE_TYPE (value) == char32_array_type_node)
969 {
970 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
971 e_type = char32_type_node;
972 }
973 else
974 {
975 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
976 e_type = wchar_type_node;
977 }
978
979 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
980 limit in C++98 Annex B is very large (65536) and is not normative,
981 so we do not diagnose it (warn_overlength_strings is forced off
982 in c_common_post_options). */
983 if (warn_overlength_strings)
984 {
985 const int nchars_max = flag_isoc99 ? 4095 : 509;
986 const int relevant_std = flag_isoc99 ? 99 : 90;
987 if (nchars - 1 > nchars_max)
988 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
989 separate the %d from the 'C'. 'ISO' should not be
990 translated, but it may be moved after 'C%d' in languages
991 where modifiers follow nouns. */
992 pedwarn (input_location, OPT_Woverlength_strings,
993 "string length %qd is greater than the length %qd "
994 "ISO C%d compilers are required to support",
995 nchars - 1, nchars_max, relevant_std);
996 }
997
998 /* Create the array type for the string constant. The ISO C++
999 standard says that a string literal has type `const char[N]' or
1000 `const wchar_t[N]'. We use the same logic when invoked as a C
1001 front-end with -Wwrite-strings.
1002 ??? We should change the type of an expression depending on the
1003 state of a warning flag. We should just be warning -- see how
1004 this is handled in the C++ front-end for the deprecated implicit
1005 conversion from string literals to `char*' or `wchar_t*'.
1006
1007 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1008 array type being the unqualified version of that type.
1009 Therefore, if we are constructing an array of const char, we must
1010 construct the matching unqualified array type first. The C front
1011 end does not require this, but it does no harm, so we do it
1012 unconditionally. */
1013 i_type = build_index_type (size_int (nchars - 1));
1014 a_type = build_array_type (e_type, i_type);
1015 if (c_dialect_cxx() || warn_write_strings)
1016 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1017
1018 TREE_TYPE (value) = a_type;
1019 TREE_CONSTANT (value) = 1;
1020 TREE_READONLY (value) = 1;
1021 TREE_STATIC (value) = 1;
1022 return value;
1023 }
1024 \f
1025 /* If DISABLE is true, stop issuing warnings. This is used when
1026 parsing code that we know will not be executed. This function may
1027 be called multiple times, and works as a stack. */
1028
1029 static void
1030 c_disable_warnings (bool disable)
1031 {
1032 if (disable)
1033 {
1034 ++c_inhibit_evaluation_warnings;
1035 fold_defer_overflow_warnings ();
1036 }
1037 }
1038
1039 /* If ENABLE is true, reenable issuing warnings. */
1040
1041 static void
1042 c_enable_warnings (bool enable)
1043 {
1044 if (enable)
1045 {
1046 --c_inhibit_evaluation_warnings;
1047 fold_undefer_and_ignore_overflow_warnings ();
1048 }
1049 }
1050
1051 /* Fully fold EXPR, an expression that was not folded (beyond integer
1052 constant expressions and null pointer constants) when being built
1053 up. If IN_INIT, this is in a static initializer and certain
1054 changes are made to the folding done. Clear *MAYBE_CONST if
1055 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1056 expression because it contains an evaluated operator (in C99) or an
1057 operator outside of sizeof returning an integer constant (in C90)
1058 not permitted in constant expressions, or because it contains an
1059 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1060 set to true by callers before calling this function.) Return the
1061 folded expression. Function arguments have already been folded
1062 before calling this function, as have the contents of SAVE_EXPR,
1063 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1064 C_MAYBE_CONST_EXPR. */
1065
1066 tree
1067 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1068 {
1069 tree ret;
1070 tree eptype = NULL_TREE;
1071 bool dummy = true;
1072 bool maybe_const_itself = true;
1073 location_t loc = EXPR_LOCATION (expr);
1074
1075 /* This function is not relevant to C++ because C++ folds while
1076 parsing, and may need changes to be correct for C++ when C++
1077 stops folding while parsing. */
1078 if (c_dialect_cxx ())
1079 gcc_unreachable ();
1080
1081 if (!maybe_const)
1082 maybe_const = &dummy;
1083 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1084 {
1085 eptype = TREE_TYPE (expr);
1086 expr = TREE_OPERAND (expr, 0);
1087 }
1088 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1089 &maybe_const_itself);
1090 if (eptype)
1091 ret = fold_convert_loc (loc, eptype, ret);
1092 *maybe_const &= maybe_const_itself;
1093 return ret;
1094 }
1095
1096 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1097 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1098 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1099 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1100 both evaluated and unevaluated subexpressions while
1101 *MAYBE_CONST_ITSELF is carried from only evaluated
1102 subexpressions). */
1103
1104 static tree
1105 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1106 bool *maybe_const_itself)
1107 {
1108 tree ret = expr;
1109 enum tree_code code = TREE_CODE (expr);
1110 enum tree_code_class kind = TREE_CODE_CLASS (code);
1111 location_t loc = EXPR_LOCATION (expr);
1112 tree op0, op1, op2, op3;
1113 tree orig_op0, orig_op1, orig_op2;
1114 bool op0_const = true, op1_const = true, op2_const = true;
1115 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1116 bool nowarning = TREE_NO_WARNING (expr);
1117 bool unused_p;
1118
1119 /* This function is not relevant to C++ because C++ folds while
1120 parsing, and may need changes to be correct for C++ when C++
1121 stops folding while parsing. */
1122 if (c_dialect_cxx ())
1123 gcc_unreachable ();
1124
1125 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1126 anything else not counted as an expression cannot usefully be
1127 folded further at this point. */
1128 if (!IS_EXPR_CODE_CLASS (kind)
1129 || kind == tcc_statement
1130 || code == SAVE_EXPR)
1131 return expr;
1132
1133 /* Operands of variable-length expressions (function calls) have
1134 already been folded, as have __builtin_* function calls, and such
1135 expressions cannot occur in constant expressions. */
1136 if (kind == tcc_vl_exp)
1137 {
1138 *maybe_const_operands = false;
1139 ret = fold (expr);
1140 goto out;
1141 }
1142
1143 if (code == C_MAYBE_CONST_EXPR)
1144 {
1145 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1146 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1147 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1148 *maybe_const_operands = false;
1149 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1150 *maybe_const_itself = false;
1151 if (pre && !in_init)
1152 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1153 else
1154 ret = inner;
1155 goto out;
1156 }
1157
1158 /* Assignment, increment, decrement, function call and comma
1159 operators, and statement expressions, cannot occur in constant
1160 expressions if evaluated / outside of sizeof. (Function calls
1161 were handled above, though VA_ARG_EXPR is treated like a function
1162 call here, and statement expressions are handled through
1163 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1164 switch (code)
1165 {
1166 case MODIFY_EXPR:
1167 case PREDECREMENT_EXPR:
1168 case PREINCREMENT_EXPR:
1169 case POSTDECREMENT_EXPR:
1170 case POSTINCREMENT_EXPR:
1171 case COMPOUND_EXPR:
1172 *maybe_const_operands = false;
1173 break;
1174
1175 case VA_ARG_EXPR:
1176 case TARGET_EXPR:
1177 case BIND_EXPR:
1178 case OBJ_TYPE_REF:
1179 *maybe_const_operands = false;
1180 ret = fold (expr);
1181 goto out;
1182
1183 default:
1184 break;
1185 }
1186
1187 /* Fold individual tree codes as appropriate. */
1188 switch (code)
1189 {
1190 case COMPOUND_LITERAL_EXPR:
1191 /* Any non-constancy will have been marked in a containing
1192 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1193 goto out;
1194
1195 case COMPONENT_REF:
1196 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1197 op1 = TREE_OPERAND (expr, 1);
1198 op2 = TREE_OPERAND (expr, 2);
1199 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1200 maybe_const_itself);
1201 STRIP_TYPE_NOPS (op0);
1202 if (op0 != orig_op0)
1203 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1204 if (ret != expr)
1205 {
1206 TREE_READONLY (ret) = TREE_READONLY (expr);
1207 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1208 }
1209 goto out;
1210
1211 case ARRAY_REF:
1212 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1213 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1214 op2 = TREE_OPERAND (expr, 2);
1215 op3 = TREE_OPERAND (expr, 3);
1216 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1217 maybe_const_itself);
1218 STRIP_TYPE_NOPS (op0);
1219 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1220 maybe_const_itself);
1221 STRIP_TYPE_NOPS (op1);
1222 op1 = decl_constant_value_for_optimization (op1);
1223 if (op0 != orig_op0 || op1 != orig_op1)
1224 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1225 if (ret != expr)
1226 {
1227 TREE_READONLY (ret) = TREE_READONLY (expr);
1228 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1229 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1230 }
1231 ret = fold (ret);
1232 goto out;
1233
1234 case COMPOUND_EXPR:
1235 case MODIFY_EXPR:
1236 case PREDECREMENT_EXPR:
1237 case PREINCREMENT_EXPR:
1238 case POSTDECREMENT_EXPR:
1239 case POSTINCREMENT_EXPR:
1240 case PLUS_EXPR:
1241 case MINUS_EXPR:
1242 case MULT_EXPR:
1243 case POINTER_PLUS_EXPR:
1244 case TRUNC_DIV_EXPR:
1245 case CEIL_DIV_EXPR:
1246 case FLOOR_DIV_EXPR:
1247 case TRUNC_MOD_EXPR:
1248 case RDIV_EXPR:
1249 case EXACT_DIV_EXPR:
1250 case LSHIFT_EXPR:
1251 case RSHIFT_EXPR:
1252 case BIT_IOR_EXPR:
1253 case BIT_XOR_EXPR:
1254 case BIT_AND_EXPR:
1255 case LT_EXPR:
1256 case LE_EXPR:
1257 case GT_EXPR:
1258 case GE_EXPR:
1259 case EQ_EXPR:
1260 case NE_EXPR:
1261 case COMPLEX_EXPR:
1262 case TRUTH_AND_EXPR:
1263 case TRUTH_OR_EXPR:
1264 case TRUTH_XOR_EXPR:
1265 case UNORDERED_EXPR:
1266 case ORDERED_EXPR:
1267 case UNLT_EXPR:
1268 case UNLE_EXPR:
1269 case UNGT_EXPR:
1270 case UNGE_EXPR:
1271 case UNEQ_EXPR:
1272 /* Binary operations evaluating both arguments (increment and
1273 decrement are binary internally in GCC). */
1274 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1275 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1276 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1277 maybe_const_itself);
1278 STRIP_TYPE_NOPS (op0);
1279 if (code != MODIFY_EXPR
1280 && code != PREDECREMENT_EXPR
1281 && code != PREINCREMENT_EXPR
1282 && code != POSTDECREMENT_EXPR
1283 && code != POSTINCREMENT_EXPR)
1284 op0 = decl_constant_value_for_optimization (op0);
1285 /* The RHS of a MODIFY_EXPR was fully folded when building that
1286 expression for the sake of conversion warnings. */
1287 if (code != MODIFY_EXPR)
1288 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1289 maybe_const_itself);
1290 STRIP_TYPE_NOPS (op1);
1291 op1 = decl_constant_value_for_optimization (op1);
1292 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1293 ret = in_init
1294 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1295 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1296 else
1297 ret = fold (expr);
1298 if (TREE_OVERFLOW_P (ret)
1299 && !TREE_OVERFLOW_P (op0)
1300 && !TREE_OVERFLOW_P (op1))
1301 overflow_warning (EXPR_LOCATION (expr), ret);
1302 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1303 && TREE_CODE (orig_op1) != INTEGER_CST
1304 && TREE_CODE (op1) == INTEGER_CST
1305 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1306 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1307 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1308 && c_inhibit_evaluation_warnings == 0)
1309 {
1310 if (tree_int_cst_sgn (op1) < 0)
1311 warning_at (loc, 0, (code == LSHIFT_EXPR
1312 ? G_("left shift count is negative")
1313 : G_("right shift count is negative")));
1314 else if (compare_tree_int (op1,
1315 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1316 >= 0)
1317 warning_at (loc, 0, (code == LSHIFT_EXPR
1318 ? G_("left shift count >= width of type")
1319 : G_("right shift count >= width of type")));
1320 }
1321 goto out;
1322
1323 case INDIRECT_REF:
1324 case FIX_TRUNC_EXPR:
1325 case FLOAT_EXPR:
1326 CASE_CONVERT:
1327 case VIEW_CONVERT_EXPR:
1328 case NON_LVALUE_EXPR:
1329 case NEGATE_EXPR:
1330 case BIT_NOT_EXPR:
1331 case TRUTH_NOT_EXPR:
1332 case ADDR_EXPR:
1333 case CONJ_EXPR:
1334 case REALPART_EXPR:
1335 case IMAGPART_EXPR:
1336 /* Unary operations. */
1337 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1338 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1339 maybe_const_itself);
1340 STRIP_TYPE_NOPS (op0);
1341 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1342 op0 = decl_constant_value_for_optimization (op0);
1343 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1344 not prepared to deal with them if they occur in initializers. */
1345 if (op0 != orig_op0
1346 && code == ADDR_EXPR
1347 && (op1 = get_base_address (op0)) != NULL_TREE
1348 && TREE_CODE (op1) == INDIRECT_REF
1349 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1350 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1351 else if (op0 != orig_op0 || in_init)
1352 ret = in_init
1353 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1354 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1355 else
1356 ret = fold (expr);
1357 if (code == INDIRECT_REF
1358 && ret != expr
1359 && TREE_CODE (ret) == INDIRECT_REF)
1360 {
1361 TREE_READONLY (ret) = TREE_READONLY (expr);
1362 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1363 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1364 }
1365 switch (code)
1366 {
1367 case FIX_TRUNC_EXPR:
1368 case FLOAT_EXPR:
1369 CASE_CONVERT:
1370 /* Don't warn about explicit conversions. We will already
1371 have warned about suspect implicit conversions. */
1372 break;
1373
1374 default:
1375 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1376 overflow_warning (EXPR_LOCATION (expr), ret);
1377 break;
1378 }
1379 goto out;
1380
1381 case TRUTH_ANDIF_EXPR:
1382 case TRUTH_ORIF_EXPR:
1383 /* Binary operations not necessarily evaluating both
1384 arguments. */
1385 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1386 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1387 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1388 STRIP_TYPE_NOPS (op0);
1389
1390 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1391 ? truthvalue_false_node
1392 : truthvalue_true_node));
1393 c_disable_warnings (unused_p);
1394 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1395 STRIP_TYPE_NOPS (op1);
1396 c_enable_warnings (unused_p);
1397
1398 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1399 ret = in_init
1400 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1401 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1402 else
1403 ret = fold (expr);
1404 *maybe_const_operands &= op0_const;
1405 *maybe_const_itself &= op0_const_self;
1406 if (!(flag_isoc99
1407 && op0_const
1408 && op0_const_self
1409 && (code == TRUTH_ANDIF_EXPR
1410 ? op0 == truthvalue_false_node
1411 : op0 == truthvalue_true_node)))
1412 *maybe_const_operands &= op1_const;
1413 if (!(op0_const
1414 && op0_const_self
1415 && (code == TRUTH_ANDIF_EXPR
1416 ? op0 == truthvalue_false_node
1417 : op0 == truthvalue_true_node)))
1418 *maybe_const_itself &= op1_const_self;
1419 goto out;
1420
1421 case COND_EXPR:
1422 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1423 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1424 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1425 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1426
1427 STRIP_TYPE_NOPS (op0);
1428 c_disable_warnings (op0 == truthvalue_false_node);
1429 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1430 STRIP_TYPE_NOPS (op1);
1431 c_enable_warnings (op0 == truthvalue_false_node);
1432
1433 c_disable_warnings (op0 == truthvalue_true_node);
1434 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1435 STRIP_TYPE_NOPS (op2);
1436 c_enable_warnings (op0 == truthvalue_true_node);
1437
1438 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1439 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1440 else
1441 ret = fold (expr);
1442 *maybe_const_operands &= op0_const;
1443 *maybe_const_itself &= op0_const_self;
1444 if (!(flag_isoc99
1445 && op0_const
1446 && op0_const_self
1447 && op0 == truthvalue_false_node))
1448 *maybe_const_operands &= op1_const;
1449 if (!(op0_const
1450 && op0_const_self
1451 && op0 == truthvalue_false_node))
1452 *maybe_const_itself &= op1_const_self;
1453 if (!(flag_isoc99
1454 && op0_const
1455 && op0_const_self
1456 && op0 == truthvalue_true_node))
1457 *maybe_const_operands &= op2_const;
1458 if (!(op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_true_node))
1461 *maybe_const_itself &= op2_const_self;
1462 goto out;
1463
1464 case EXCESS_PRECISION_EXPR:
1465 /* Each case where an operand with excess precision may be
1466 encountered must remove the EXCESS_PRECISION_EXPR around
1467 inner operands and possibly put one around the whole
1468 expression or possibly convert to the semantic type (which
1469 c_fully_fold does); we cannot tell at this stage which is
1470 appropriate in any particular case. */
1471 gcc_unreachable ();
1472
1473 default:
1474 /* Various codes may appear through folding built-in functions
1475 and their arguments. */
1476 goto out;
1477 }
1478
1479 out:
1480 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1481 have been done by this point, so remove them again. */
1482 nowarning |= TREE_NO_WARNING (ret);
1483 STRIP_TYPE_NOPS (ret);
1484 if (nowarning && !TREE_NO_WARNING (ret))
1485 {
1486 if (!CAN_HAVE_LOCATION_P (ret))
1487 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1488 TREE_NO_WARNING (ret) = 1;
1489 }
1490 if (ret != expr)
1491 protected_set_expr_location (ret, loc);
1492 return ret;
1493 }
1494
1495 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1496 return EXP. Otherwise, return either EXP or its known constant
1497 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1498 Is the BLKmode test appropriate? */
1499
1500 tree
1501 decl_constant_value_for_optimization (tree exp)
1502 {
1503 tree ret;
1504
1505 /* This function is only used by C, for c_fully_fold and other
1506 optimization, and may not be correct for C++. */
1507 if (c_dialect_cxx ())
1508 gcc_unreachable ();
1509
1510 if (!optimize
1511 || TREE_CODE (exp) != VAR_DECL
1512 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1513 || DECL_MODE (exp) == BLKmode)
1514 return exp;
1515
1516 ret = decl_constant_value (exp);
1517 /* Avoid unwanted tree sharing between the initializer and current
1518 function's body where the tree can be modified e.g. by the
1519 gimplifier. */
1520 if (ret != exp && TREE_STATIC (exp))
1521 ret = unshare_expr (ret);
1522 return ret;
1523 }
1524
1525 /* Print a warning if a constant expression had overflow in folding.
1526 Invoke this function on every expression that the language
1527 requires to be a constant expression.
1528 Note the ANSI C standard says it is erroneous for a
1529 constant expression to overflow. */
1530
1531 void
1532 constant_expression_warning (tree value)
1533 {
1534 if (warn_overflow && pedantic
1535 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1536 || TREE_CODE (value) == FIXED_CST
1537 || TREE_CODE (value) == VECTOR_CST
1538 || TREE_CODE (value) == COMPLEX_CST)
1539 && TREE_OVERFLOW (value))
1540 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1541 }
1542
1543 /* The same as above but print an unconditional error. */
1544 void
1545 constant_expression_error (tree value)
1546 {
1547 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1548 || TREE_CODE (value) == FIXED_CST
1549 || TREE_CODE (value) == VECTOR_CST
1550 || TREE_CODE (value) == COMPLEX_CST)
1551 && TREE_OVERFLOW (value))
1552 error ("overflow in constant expression");
1553 }
1554
1555 /* Print a warning if an expression had overflow in folding and its
1556 operands hadn't.
1557
1558 Invoke this function on every expression that
1559 (1) appears in the source code, and
1560 (2) is a constant expression that overflowed, and
1561 (3) is not already checked by convert_and_check;
1562 however, do not invoke this function on operands of explicit casts
1563 or when the expression is the result of an operator and any operand
1564 already overflowed. */
1565
1566 void
1567 overflow_warning (location_t loc, tree value)
1568 {
1569 if (c_inhibit_evaluation_warnings != 0)
1570 return;
1571
1572 switch (TREE_CODE (value))
1573 {
1574 case INTEGER_CST:
1575 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1576 break;
1577
1578 case REAL_CST:
1579 warning_at (loc, OPT_Woverflow,
1580 "floating point overflow in expression");
1581 break;
1582
1583 case FIXED_CST:
1584 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1585 break;
1586
1587 case VECTOR_CST:
1588 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1589 break;
1590
1591 case COMPLEX_CST:
1592 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1593 warning_at (loc, OPT_Woverflow,
1594 "complex integer overflow in expression");
1595 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1596 warning_at (loc, OPT_Woverflow,
1597 "complex floating point overflow in expression");
1598 break;
1599
1600 default:
1601 break;
1602 }
1603 }
1604
1605 /* Warn about uses of logical || / && operator in a context where it
1606 is likely that the bitwise equivalent was intended by the
1607 programmer. We have seen an expression in which CODE is a binary
1608 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1609 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1610 void
1611 warn_logical_operator (location_t location, enum tree_code code, tree type,
1612 enum tree_code code_left, tree op_left,
1613 enum tree_code ARG_UNUSED (code_right), tree op_right)
1614 {
1615 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1616 int in0_p, in1_p, in_p;
1617 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1618 bool strict_overflow_p = false;
1619
1620 if (code != TRUTH_ANDIF_EXPR
1621 && code != TRUTH_AND_EXPR
1622 && code != TRUTH_ORIF_EXPR
1623 && code != TRUTH_OR_EXPR)
1624 return;
1625
1626 /* Warn if &&/|| are being used in a context where it is
1627 likely that the bitwise equivalent was intended by the
1628 programmer. That is, an expression such as op && MASK
1629 where op should not be any boolean expression, nor a
1630 constant, and mask seems to be a non-boolean integer constant. */
1631 if (!truth_value_p (code_left)
1632 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1633 && !CONSTANT_CLASS_P (op_left)
1634 && !TREE_NO_WARNING (op_left)
1635 && TREE_CODE (op_right) == INTEGER_CST
1636 && !integer_zerop (op_right)
1637 && !integer_onep (op_right))
1638 {
1639 if (or_op)
1640 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1641 " applied to non-boolean constant");
1642 else
1643 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1644 " applied to non-boolean constant");
1645 TREE_NO_WARNING (op_left) = true;
1646 return;
1647 }
1648
1649 /* We do not warn for constants because they are typical of macro
1650 expansions that test for features. */
1651 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1652 return;
1653
1654 /* This warning only makes sense with logical operands. */
1655 if (!(truth_value_p (TREE_CODE (op_left))
1656 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1657 || !(truth_value_p (TREE_CODE (op_right))
1658 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1659 return;
1660
1661
1662 /* We first test whether either side separately is trivially true
1663 (with OR) or trivially false (with AND). If so, do not warn.
1664 This is a common idiom for testing ranges of data types in
1665 portable code. */
1666 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1667 if (!lhs)
1668 return;
1669 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1670 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1671
1672 /* If this is an OR operation, invert both sides; now, the result
1673 should be always false to get a warning. */
1674 if (or_op)
1675 in0_p = !in0_p;
1676
1677 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1678 if (tem && integer_zerop (tem))
1679 return;
1680
1681 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1682 if (!rhs)
1683 return;
1684 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1685 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1686
1687 /* If this is an OR operation, invert both sides; now, the result
1688 should be always false to get a warning. */
1689 if (or_op)
1690 in1_p = !in1_p;
1691
1692 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1693 if (tem && integer_zerop (tem))
1694 return;
1695
1696 /* If both expressions have the same operand, if we can merge the
1697 ranges, and if the range test is always false, then warn. */
1698 if (operand_equal_p (lhs, rhs, 0)
1699 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1700 in1_p, low1, high1)
1701 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1702 type, lhs, in_p, low, high))
1703 && integer_zerop (tem))
1704 {
1705 if (or_op)
1706 warning_at (location, OPT_Wlogical_op,
1707 "logical %<or%> "
1708 "of collectively exhaustive tests is always true");
1709 else
1710 warning_at (location, OPT_Wlogical_op,
1711 "logical %<and%> "
1712 "of mutually exclusive tests is always false");
1713 }
1714 }
1715
1716
1717 /* Warn if EXP contains any computations whose results are not used.
1718 Return true if a warning is printed; false otherwise. LOCUS is the
1719 (potential) location of the expression. */
1720
1721 bool
1722 warn_if_unused_value (const_tree exp, location_t locus)
1723 {
1724 restart:
1725 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1726 return false;
1727
1728 /* Don't warn about void constructs. This includes casting to void,
1729 void function calls, and statement expressions with a final cast
1730 to void. */
1731 if (VOID_TYPE_P (TREE_TYPE (exp)))
1732 return false;
1733
1734 if (EXPR_HAS_LOCATION (exp))
1735 locus = EXPR_LOCATION (exp);
1736
1737 switch (TREE_CODE (exp))
1738 {
1739 case PREINCREMENT_EXPR:
1740 case POSTINCREMENT_EXPR:
1741 case PREDECREMENT_EXPR:
1742 case POSTDECREMENT_EXPR:
1743 case MODIFY_EXPR:
1744 case INIT_EXPR:
1745 case TARGET_EXPR:
1746 case CALL_EXPR:
1747 case TRY_CATCH_EXPR:
1748 case WITH_CLEANUP_EXPR:
1749 case EXIT_EXPR:
1750 case VA_ARG_EXPR:
1751 return false;
1752
1753 case BIND_EXPR:
1754 /* For a binding, warn if no side effect within it. */
1755 exp = BIND_EXPR_BODY (exp);
1756 goto restart;
1757
1758 case SAVE_EXPR:
1759 case NON_LVALUE_EXPR:
1760 case NOP_EXPR:
1761 exp = TREE_OPERAND (exp, 0);
1762 goto restart;
1763
1764 case TRUTH_ORIF_EXPR:
1765 case TRUTH_ANDIF_EXPR:
1766 /* In && or ||, warn if 2nd operand has no side effect. */
1767 exp = TREE_OPERAND (exp, 1);
1768 goto restart;
1769
1770 case COMPOUND_EXPR:
1771 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1772 return true;
1773 /* Let people do `(foo (), 0)' without a warning. */
1774 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1775 return false;
1776 exp = TREE_OPERAND (exp, 1);
1777 goto restart;
1778
1779 case COND_EXPR:
1780 /* If this is an expression with side effects, don't warn; this
1781 case commonly appears in macro expansions. */
1782 if (TREE_SIDE_EFFECTS (exp))
1783 return false;
1784 goto warn;
1785
1786 case INDIRECT_REF:
1787 /* Don't warn about automatic dereferencing of references, since
1788 the user cannot control it. */
1789 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1790 {
1791 exp = TREE_OPERAND (exp, 0);
1792 goto restart;
1793 }
1794 /* Fall through. */
1795
1796 default:
1797 /* Referencing a volatile value is a side effect, so don't warn. */
1798 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1799 && TREE_THIS_VOLATILE (exp))
1800 return false;
1801
1802 /* If this is an expression which has no operands, there is no value
1803 to be unused. There are no such language-independent codes,
1804 but front ends may define such. */
1805 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1806 return false;
1807
1808 warn:
1809 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1810 }
1811 }
1812
1813
1814 /* Print a warning about casts that might indicate violation
1815 of strict aliasing rules if -Wstrict-aliasing is used and
1816 strict aliasing mode is in effect. OTYPE is the original
1817 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1818
1819 bool
1820 strict_aliasing_warning (tree otype, tree type, tree expr)
1821 {
1822 /* Strip pointer conversion chains and get to the correct original type. */
1823 STRIP_NOPS (expr);
1824 otype = TREE_TYPE (expr);
1825
1826 if (!(flag_strict_aliasing
1827 && POINTER_TYPE_P (type)
1828 && POINTER_TYPE_P (otype)
1829 && !VOID_TYPE_P (TREE_TYPE (type)))
1830 /* If the type we are casting to is a ref-all pointer
1831 dereferencing it is always valid. */
1832 || TYPE_REF_CAN_ALIAS_ALL (type))
1833 return false;
1834
1835 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1836 && (DECL_P (TREE_OPERAND (expr, 0))
1837 || handled_component_p (TREE_OPERAND (expr, 0))))
1838 {
1839 /* Casting the address of an object to non void pointer. Warn
1840 if the cast breaks type based aliasing. */
1841 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1842 {
1843 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1844 "might break strict-aliasing rules");
1845 return true;
1846 }
1847 else
1848 {
1849 /* warn_strict_aliasing >= 3. This includes the default (3).
1850 Only warn if the cast is dereferenced immediately. */
1851 alias_set_type set1 =
1852 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1853 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1854
1855 if (set1 != set2 && set2 != 0
1856 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1857 {
1858 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1859 "pointer will break strict-aliasing rules");
1860 return true;
1861 }
1862 else if (warn_strict_aliasing == 2
1863 && !alias_sets_must_conflict_p (set1, set2))
1864 {
1865 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1866 "pointer might break strict-aliasing rules");
1867 return true;
1868 }
1869 }
1870 }
1871 else
1872 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1873 {
1874 /* At this level, warn for any conversions, even if an address is
1875 not taken in the same statement. This will likely produce many
1876 false positives, but could be useful to pinpoint problems that
1877 are not revealed at higher levels. */
1878 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1879 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1880 if (!COMPLETE_TYPE_P (type)
1881 || !alias_sets_must_conflict_p (set1, set2))
1882 {
1883 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1884 "pointer might break strict-aliasing rules");
1885 return true;
1886 }
1887 }
1888
1889 return false;
1890 }
1891
1892 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1893 sizeof as last operand of certain builtins. */
1894
1895 void
1896 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1897 vec<tree, va_gc> *params, tree *sizeof_arg,
1898 bool (*comp_types) (tree, tree))
1899 {
1900 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1901 bool strop = false, cmp = false;
1902 unsigned int idx = ~0;
1903 location_t loc;
1904
1905 if (TREE_CODE (callee) != FUNCTION_DECL
1906 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1907 || vec_safe_length (params) <= 1)
1908 return;
1909
1910 switch (DECL_FUNCTION_CODE (callee))
1911 {
1912 case BUILT_IN_STRNCMP:
1913 case BUILT_IN_STRNCASECMP:
1914 cmp = true;
1915 /* FALLTHRU */
1916 case BUILT_IN_STRNCPY:
1917 case BUILT_IN_STRNCPY_CHK:
1918 case BUILT_IN_STRNCAT:
1919 case BUILT_IN_STRNCAT_CHK:
1920 case BUILT_IN_STPNCPY:
1921 case BUILT_IN_STPNCPY_CHK:
1922 strop = true;
1923 /* FALLTHRU */
1924 case BUILT_IN_MEMCPY:
1925 case BUILT_IN_MEMCPY_CHK:
1926 case BUILT_IN_MEMMOVE:
1927 case BUILT_IN_MEMMOVE_CHK:
1928 if (params->length () < 3)
1929 return;
1930 src = (*params)[1];
1931 dest = (*params)[0];
1932 idx = 2;
1933 break;
1934 case BUILT_IN_BCOPY:
1935 if (params->length () < 3)
1936 return;
1937 src = (*params)[0];
1938 dest = (*params)[1];
1939 idx = 2;
1940 break;
1941 case BUILT_IN_MEMCMP:
1942 case BUILT_IN_BCMP:
1943 if (params->length () < 3)
1944 return;
1945 src = (*params)[1];
1946 dest = (*params)[0];
1947 idx = 2;
1948 cmp = true;
1949 break;
1950 case BUILT_IN_MEMSET:
1951 case BUILT_IN_MEMSET_CHK:
1952 if (params->length () < 3)
1953 return;
1954 dest = (*params)[0];
1955 idx = 2;
1956 break;
1957 case BUILT_IN_BZERO:
1958 dest = (*params)[0];
1959 idx = 1;
1960 break;
1961 case BUILT_IN_STRNDUP:
1962 src = (*params)[0];
1963 strop = true;
1964 idx = 1;
1965 break;
1966 case BUILT_IN_MEMCHR:
1967 if (params->length () < 3)
1968 return;
1969 src = (*params)[0];
1970 idx = 2;
1971 break;
1972 case BUILT_IN_SNPRINTF:
1973 case BUILT_IN_SNPRINTF_CHK:
1974 case BUILT_IN_VSNPRINTF:
1975 case BUILT_IN_VSNPRINTF_CHK:
1976 dest = (*params)[0];
1977 idx = 1;
1978 strop = true;
1979 break;
1980 default:
1981 break;
1982 }
1983
1984 if (idx >= 3)
1985 return;
1986
1987 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1988 return;
1989
1990 type = TYPE_P (sizeof_arg[idx])
1991 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1992 if (!POINTER_TYPE_P (type))
1993 return;
1994
1995 if (dest
1996 && (tem = tree_strip_nop_conversions (dest))
1997 && POINTER_TYPE_P (TREE_TYPE (tem))
1998 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1999 return;
2000
2001 if (src
2002 && (tem = tree_strip_nop_conversions (src))
2003 && POINTER_TYPE_P (TREE_TYPE (tem))
2004 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2005 return;
2006
2007 loc = sizeof_arg_loc[idx];
2008
2009 if (dest && !cmp)
2010 {
2011 if (!TYPE_P (sizeof_arg[idx])
2012 && operand_equal_p (dest, sizeof_arg[idx], 0)
2013 && comp_types (TREE_TYPE (dest), type))
2014 {
2015 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2016 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2017 "argument to %<sizeof%> in %qD call is the same "
2018 "expression as the destination; did you mean to "
2019 "remove the addressof?", callee);
2020 else if ((TYPE_PRECISION (TREE_TYPE (type))
2021 == TYPE_PRECISION (char_type_node))
2022 || strop)
2023 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2024 "argument to %<sizeof%> in %qD call is the same "
2025 "expression as the destination; did you mean to "
2026 "provide an explicit length?", callee);
2027 else
2028 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2029 "argument to %<sizeof%> in %qD call is the same "
2030 "expression as the destination; did you mean to "
2031 "dereference it?", callee);
2032 return;
2033 }
2034
2035 if (POINTER_TYPE_P (TREE_TYPE (dest))
2036 && !strop
2037 && comp_types (TREE_TYPE (dest), type)
2038 && !VOID_TYPE_P (TREE_TYPE (type)))
2039 {
2040 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2041 "argument to %<sizeof%> in %qD call is the same "
2042 "pointer type %qT as the destination; expected %qT "
2043 "or an explicit length", callee, TREE_TYPE (dest),
2044 TREE_TYPE (TREE_TYPE (dest)));
2045 return;
2046 }
2047 }
2048
2049 if (src && !cmp)
2050 {
2051 if (!TYPE_P (sizeof_arg[idx])
2052 && operand_equal_p (src, sizeof_arg[idx], 0)
2053 && comp_types (TREE_TYPE (src), type))
2054 {
2055 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2056 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2057 "argument to %<sizeof%> in %qD call is the same "
2058 "expression as the source; did you mean to "
2059 "remove the addressof?", callee);
2060 else if ((TYPE_PRECISION (TREE_TYPE (type))
2061 == TYPE_PRECISION (char_type_node))
2062 || strop)
2063 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2064 "argument to %<sizeof%> in %qD call is the same "
2065 "expression as the source; did you mean to "
2066 "provide an explicit length?", callee);
2067 else
2068 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2069 "argument to %<sizeof%> in %qD call is the same "
2070 "expression as the source; did you mean to "
2071 "dereference it?", callee);
2072 return;
2073 }
2074
2075 if (POINTER_TYPE_P (TREE_TYPE (src))
2076 && !strop
2077 && comp_types (TREE_TYPE (src), type)
2078 && !VOID_TYPE_P (TREE_TYPE (type)))
2079 {
2080 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2081 "argument to %<sizeof%> in %qD call is the same "
2082 "pointer type %qT as the source; expected %qT "
2083 "or an explicit length", callee, TREE_TYPE (src),
2084 TREE_TYPE (TREE_TYPE (src)));
2085 return;
2086 }
2087 }
2088
2089 if (dest)
2090 {
2091 if (!TYPE_P (sizeof_arg[idx])
2092 && operand_equal_p (dest, sizeof_arg[idx], 0)
2093 && comp_types (TREE_TYPE (dest), type))
2094 {
2095 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2096 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2097 "argument to %<sizeof%> in %qD call is the same "
2098 "expression as the first source; did you mean to "
2099 "remove the addressof?", callee);
2100 else if ((TYPE_PRECISION (TREE_TYPE (type))
2101 == TYPE_PRECISION (char_type_node))
2102 || strop)
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "expression as the first source; did you mean to "
2106 "provide an explicit length?", callee);
2107 else
2108 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2109 "argument to %<sizeof%> in %qD call is the same "
2110 "expression as the first source; did you mean to "
2111 "dereference it?", callee);
2112 return;
2113 }
2114
2115 if (POINTER_TYPE_P (TREE_TYPE (dest))
2116 && !strop
2117 && comp_types (TREE_TYPE (dest), type)
2118 && !VOID_TYPE_P (TREE_TYPE (type)))
2119 {
2120 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2121 "argument to %<sizeof%> in %qD call is the same "
2122 "pointer type %qT as the first source; expected %qT "
2123 "or an explicit length", callee, TREE_TYPE (dest),
2124 TREE_TYPE (TREE_TYPE (dest)));
2125 return;
2126 }
2127 }
2128
2129 if (src)
2130 {
2131 if (!TYPE_P (sizeof_arg[idx])
2132 && operand_equal_p (src, sizeof_arg[idx], 0)
2133 && comp_types (TREE_TYPE (src), type))
2134 {
2135 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2136 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2137 "argument to %<sizeof%> in %qD call is the same "
2138 "expression as the second source; did you mean to "
2139 "remove the addressof?", callee);
2140 else if ((TYPE_PRECISION (TREE_TYPE (type))
2141 == TYPE_PRECISION (char_type_node))
2142 || strop)
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "expression as the second source; did you mean to "
2146 "provide an explicit length?", callee);
2147 else
2148 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2149 "argument to %<sizeof%> in %qD call is the same "
2150 "expression as the second source; did you mean to "
2151 "dereference it?", callee);
2152 return;
2153 }
2154
2155 if (POINTER_TYPE_P (TREE_TYPE (src))
2156 && !strop
2157 && comp_types (TREE_TYPE (src), type)
2158 && !VOID_TYPE_P (TREE_TYPE (type)))
2159 {
2160 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2161 "argument to %<sizeof%> in %qD call is the same "
2162 "pointer type %qT as the second source; expected %qT "
2163 "or an explicit length", callee, TREE_TYPE (src),
2164 TREE_TYPE (TREE_TYPE (src)));
2165 return;
2166 }
2167 }
2168
2169 }
2170
2171 /* Warn for unlikely, improbable, or stupid DECL declarations
2172 of `main'. */
2173
2174 void
2175 check_main_parameter_types (tree decl)
2176 {
2177 function_args_iterator iter;
2178 tree type;
2179 int argct = 0;
2180
2181 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2182 {
2183 /* XXX void_type_node belies the abstraction. */
2184 if (type == void_type_node || type == error_mark_node )
2185 break;
2186
2187 ++argct;
2188 switch (argct)
2189 {
2190 case 1:
2191 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2192 pedwarn (input_location, OPT_Wmain,
2193 "first argument of %q+D should be %<int%>", decl);
2194 break;
2195
2196 case 2:
2197 if (TREE_CODE (type) != POINTER_TYPE
2198 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2199 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2200 != char_type_node))
2201 pedwarn (input_location, OPT_Wmain,
2202 "second argument of %q+D should be %<char **%>", decl);
2203 break;
2204
2205 case 3:
2206 if (TREE_CODE (type) != POINTER_TYPE
2207 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2208 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2209 != char_type_node))
2210 pedwarn (input_location, OPT_Wmain,
2211 "third argument of %q+D should probably be "
2212 "%<char **%>", decl);
2213 break;
2214 }
2215 }
2216
2217 /* It is intentional that this message does not mention the third
2218 argument because it's only mentioned in an appendix of the
2219 standard. */
2220 if (argct > 0 && (argct < 2 || argct > 3))
2221 pedwarn (input_location, OPT_Wmain,
2222 "%q+D takes only zero or two arguments", decl);
2223 }
2224
2225 /* vector_targets_convertible_p is used for vector pointer types. The
2226 callers perform various checks that the qualifiers are satisfactory,
2227 while OTOH vector_targets_convertible_p ignores the number of elements
2228 in the vectors. That's fine with vector pointers as we can consider,
2229 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2230 and that does not require and conversion of the pointer values.
2231 In contrast, vector_types_convertible_p and
2232 vector_types_compatible_elements_p are used for vector value types. */
2233 /* True if pointers to distinct types T1 and T2 can be converted to
2234 each other without an explicit cast. Only returns true for opaque
2235 vector types. */
2236 bool
2237 vector_targets_convertible_p (const_tree t1, const_tree t2)
2238 {
2239 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2240 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2241 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2242 return true;
2243
2244 return false;
2245 }
2246
2247 /* vector_types_convertible_p is used for vector value types.
2248 It could in principle call vector_targets_convertible_p as a subroutine,
2249 but then the check for vector type would be duplicated with its callers,
2250 and also the purpose of vector_targets_convertible_p would become
2251 muddled.
2252 Where vector_types_convertible_p returns true, a conversion might still be
2253 needed to make the types match.
2254 In contrast, vector_targets_convertible_p is used for vector pointer
2255 values, and vector_types_compatible_elements_p is used specifically
2256 in the context for binary operators, as a check if use is possible without
2257 conversion. */
2258 /* True if vector types T1 and T2 can be converted to each other
2259 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2260 can only be converted with -flax-vector-conversions yet that is not
2261 in effect, emit a note telling the user about that option if such
2262 a note has not previously been emitted. */
2263 bool
2264 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2265 {
2266 static bool emitted_lax_note = false;
2267 bool convertible_lax;
2268
2269 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2270 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2271 return true;
2272
2273 convertible_lax =
2274 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2275 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2276 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2277 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2278 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2279
2280 if (!convertible_lax || flag_lax_vector_conversions)
2281 return convertible_lax;
2282
2283 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2284 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2285 return true;
2286
2287 if (emit_lax_note && !emitted_lax_note)
2288 {
2289 emitted_lax_note = true;
2290 inform (input_location, "use -flax-vector-conversions to permit "
2291 "conversions between vectors with differing "
2292 "element types or numbers of subparts");
2293 }
2294
2295 return false;
2296 }
2297
2298 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2299 and have vector types, V0 has the same type as V1, and the number of
2300 elements of V0, V1, MASK is the same.
2301
2302 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2303 called with two arguments. In this case implementation passes the
2304 first argument twice in order to share the same tree code. This fact
2305 could enable the mask-values being twice the vector length. This is
2306 an implementation accident and this semantics is not guaranteed to
2307 the user. */
2308 tree
2309 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2310 bool complain)
2311 {
2312 tree ret;
2313 bool wrap = true;
2314 bool maybe_const = false;
2315 bool two_arguments = false;
2316
2317 if (v1 == NULL_TREE)
2318 {
2319 two_arguments = true;
2320 v1 = v0;
2321 }
2322
2323 if (v0 == error_mark_node || v1 == error_mark_node
2324 || mask == error_mark_node)
2325 return error_mark_node;
2326
2327 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2328 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2329 {
2330 if (complain)
2331 error_at (loc, "__builtin_shuffle last argument must "
2332 "be an integer vector");
2333 return error_mark_node;
2334 }
2335
2336 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2337 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2338 {
2339 if (complain)
2340 error_at (loc, "__builtin_shuffle arguments must be vectors");
2341 return error_mark_node;
2342 }
2343
2344 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2345 {
2346 if (complain)
2347 error_at (loc, "__builtin_shuffle argument vectors must be of "
2348 "the same type");
2349 return error_mark_node;
2350 }
2351
2352 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2353 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2354 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2355 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2356 {
2357 if (complain)
2358 error_at (loc, "__builtin_shuffle number of elements of the "
2359 "argument vector(s) and the mask vector should "
2360 "be the same");
2361 return error_mark_node;
2362 }
2363
2364 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2365 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2366 {
2367 if (complain)
2368 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2369 "must have the same size as inner type of the mask");
2370 return error_mark_node;
2371 }
2372
2373 if (!c_dialect_cxx ())
2374 {
2375 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2376 v0 = c_fully_fold (v0, false, &maybe_const);
2377 wrap &= maybe_const;
2378
2379 if (two_arguments)
2380 v1 = v0 = save_expr (v0);
2381 else
2382 {
2383 v1 = c_fully_fold (v1, false, &maybe_const);
2384 wrap &= maybe_const;
2385 }
2386
2387 mask = c_fully_fold (mask, false, &maybe_const);
2388 wrap &= maybe_const;
2389 }
2390 else if (two_arguments)
2391 v1 = v0 = save_expr (v0);
2392
2393 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2394
2395 if (!c_dialect_cxx () && !wrap)
2396 ret = c_wrap_maybe_const (ret, true);
2397
2398 return ret;
2399 }
2400
2401 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2402 to integral type. */
2403
2404 static tree
2405 c_common_get_narrower (tree op, int *unsignedp_ptr)
2406 {
2407 op = get_narrower (op, unsignedp_ptr);
2408
2409 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2410 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2411 {
2412 /* C++0x scoped enumerations don't implicitly convert to integral
2413 type; if we stripped an explicit conversion to a larger type we
2414 need to replace it so common_type will still work. */
2415 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2416 TYPE_UNSIGNED (TREE_TYPE (op)));
2417 op = fold_convert (type, op);
2418 }
2419 return op;
2420 }
2421
2422 /* This is a helper function of build_binary_op.
2423
2424 For certain operations if both args were extended from the same
2425 smaller type, do the arithmetic in that type and then extend.
2426
2427 BITWISE indicates a bitwise operation.
2428 For them, this optimization is safe only if
2429 both args are zero-extended or both are sign-extended.
2430 Otherwise, we might change the result.
2431 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2432 but calculated in (unsigned short) it would be (unsigned short)-1.
2433 */
2434 tree
2435 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2436 {
2437 int unsigned0, unsigned1;
2438 tree arg0, arg1;
2439 int uns;
2440 tree type;
2441
2442 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2443 excessive narrowing when we call get_narrower below. For
2444 example, suppose that OP0 is of unsigned int extended
2445 from signed char and that RESULT_TYPE is long long int.
2446 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2447 like
2448
2449 (long long int) (unsigned int) signed_char
2450
2451 which get_narrower would narrow down to
2452
2453 (unsigned int) signed char
2454
2455 If we do not cast OP0 first, get_narrower would return
2456 signed_char, which is inconsistent with the case of the
2457 explicit cast. */
2458 op0 = convert (result_type, op0);
2459 op1 = convert (result_type, op1);
2460
2461 arg0 = c_common_get_narrower (op0, &unsigned0);
2462 arg1 = c_common_get_narrower (op1, &unsigned1);
2463
2464 /* UNS is 1 if the operation to be done is an unsigned one. */
2465 uns = TYPE_UNSIGNED (result_type);
2466
2467 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2468 but it *requires* conversion to FINAL_TYPE. */
2469
2470 if ((TYPE_PRECISION (TREE_TYPE (op0))
2471 == TYPE_PRECISION (TREE_TYPE (arg0)))
2472 && TREE_TYPE (op0) != result_type)
2473 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2474 if ((TYPE_PRECISION (TREE_TYPE (op1))
2475 == TYPE_PRECISION (TREE_TYPE (arg1)))
2476 && TREE_TYPE (op1) != result_type)
2477 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2478
2479 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2480
2481 /* For bitwise operations, signedness of nominal type
2482 does not matter. Consider only how operands were extended. */
2483 if (bitwise)
2484 uns = unsigned0;
2485
2486 /* Note that in all three cases below we refrain from optimizing
2487 an unsigned operation on sign-extended args.
2488 That would not be valid. */
2489
2490 /* Both args variable: if both extended in same way
2491 from same width, do it in that width.
2492 Do it unsigned if args were zero-extended. */
2493 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2494 < TYPE_PRECISION (result_type))
2495 && (TYPE_PRECISION (TREE_TYPE (arg1))
2496 == TYPE_PRECISION (TREE_TYPE (arg0)))
2497 && unsigned0 == unsigned1
2498 && (unsigned0 || !uns))
2499 return c_common_signed_or_unsigned_type
2500 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2501
2502 else if (TREE_CODE (arg0) == INTEGER_CST
2503 && (unsigned1 || !uns)
2504 && (TYPE_PRECISION (TREE_TYPE (arg1))
2505 < TYPE_PRECISION (result_type))
2506 && (type
2507 = c_common_signed_or_unsigned_type (unsigned1,
2508 TREE_TYPE (arg1)))
2509 && !POINTER_TYPE_P (type)
2510 && int_fits_type_p (arg0, type))
2511 return type;
2512
2513 else if (TREE_CODE (arg1) == INTEGER_CST
2514 && (unsigned0 || !uns)
2515 && (TYPE_PRECISION (TREE_TYPE (arg0))
2516 < TYPE_PRECISION (result_type))
2517 && (type
2518 = c_common_signed_or_unsigned_type (unsigned0,
2519 TREE_TYPE (arg0)))
2520 && !POINTER_TYPE_P (type)
2521 && int_fits_type_p (arg1, type))
2522 return type;
2523
2524 return result_type;
2525 }
2526
2527 /* Checks if expression EXPR of real/integer type cannot be converted
2528 to the real/integer type TYPE. Function returns true when:
2529 * EXPR is a constant which cannot be exactly converted to TYPE
2530 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2531 for EXPR type and TYPE being both integers or both real.
2532 * EXPR is not a constant of real type and TYPE is an integer.
2533 * EXPR is not a constant of integer type which cannot be
2534 exactly converted to real type.
2535 Function allows conversions between types of different signedness and
2536 does not return true in that case. Function can produce signedness
2537 warnings if PRODUCE_WARNS is true. */
2538 bool
2539 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2540 {
2541 bool give_warning = false;
2542 tree expr_type = TREE_TYPE (expr);
2543 location_t loc = EXPR_LOC_OR_HERE (expr);
2544
2545 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2546 {
2547 /* Warn for real constant that is not an exact integer converted
2548 to integer type. */
2549 if (TREE_CODE (expr_type) == REAL_TYPE
2550 && TREE_CODE (type) == INTEGER_TYPE)
2551 {
2552 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2553 give_warning = true;
2554 }
2555 /* Warn for an integer constant that does not fit into integer type. */
2556 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2557 && TREE_CODE (type) == INTEGER_TYPE
2558 && !int_fits_type_p (expr, type))
2559 {
2560 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2561 && tree_int_cst_sgn (expr) < 0)
2562 {
2563 if (produce_warns)
2564 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2565 " implicitly converted to unsigned type");
2566 }
2567 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2568 {
2569 if (produce_warns)
2570 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2571 " constant value to negative integer");
2572 }
2573 else
2574 give_warning = true;
2575 }
2576 else if (TREE_CODE (type) == REAL_TYPE)
2577 {
2578 /* Warn for an integer constant that does not fit into real type. */
2579 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2580 {
2581 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2582 if (!exact_real_truncate (TYPE_MODE (type), &a))
2583 give_warning = true;
2584 }
2585 /* Warn for a real constant that does not fit into a smaller
2586 real type. */
2587 else if (TREE_CODE (expr_type) == REAL_TYPE
2588 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2589 {
2590 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2591 if (!exact_real_truncate (TYPE_MODE (type), &a))
2592 give_warning = true;
2593 }
2594 }
2595 }
2596 else
2597 {
2598 /* Warn for real types converted to integer types. */
2599 if (TREE_CODE (expr_type) == REAL_TYPE
2600 && TREE_CODE (type) == INTEGER_TYPE)
2601 give_warning = true;
2602
2603 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2604 && TREE_CODE (type) == INTEGER_TYPE)
2605 {
2606 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2607 expr = get_unwidened (expr, 0);
2608 expr_type = TREE_TYPE (expr);
2609
2610 /* Don't warn for short y; short x = ((int)y & 0xff); */
2611 if (TREE_CODE (expr) == BIT_AND_EXPR
2612 || TREE_CODE (expr) == BIT_IOR_EXPR
2613 || TREE_CODE (expr) == BIT_XOR_EXPR)
2614 {
2615 /* If both args were extended from a shortest type,
2616 use that type if that is safe. */
2617 expr_type = shorten_binary_op (expr_type,
2618 TREE_OPERAND (expr, 0),
2619 TREE_OPERAND (expr, 1),
2620 /* bitwise */1);
2621
2622 if (TREE_CODE (expr) == BIT_AND_EXPR)
2623 {
2624 tree op0 = TREE_OPERAND (expr, 0);
2625 tree op1 = TREE_OPERAND (expr, 1);
2626 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2627 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2628
2629 /* If one of the operands is a non-negative constant
2630 that fits in the target type, then the type of the
2631 other operand does not matter. */
2632 if ((TREE_CODE (op0) == INTEGER_CST
2633 && int_fits_type_p (op0, c_common_signed_type (type))
2634 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2635 || (TREE_CODE (op1) == INTEGER_CST
2636 && int_fits_type_p (op1, c_common_signed_type (type))
2637 && int_fits_type_p (op1,
2638 c_common_unsigned_type (type))))
2639 return false;
2640 /* If constant is unsigned and fits in the target
2641 type, then the result will also fit. */
2642 else if ((TREE_CODE (op0) == INTEGER_CST
2643 && unsigned0
2644 && int_fits_type_p (op0, type))
2645 || (TREE_CODE (op1) == INTEGER_CST
2646 && unsigned1
2647 && int_fits_type_p (op1, type)))
2648 return false;
2649 }
2650 }
2651 /* Warn for integer types converted to smaller integer types. */
2652 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2653 give_warning = true;
2654
2655 /* When they are the same width but different signedness,
2656 then the value may change. */
2657 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2658 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2659 /* Even when converted to a bigger type, if the type is
2660 unsigned but expr is signed, then negative values
2661 will be changed. */
2662 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2663 && produce_warns)
2664 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2665 "may change the sign of the result",
2666 type, expr_type);
2667 }
2668
2669 /* Warn for integer types converted to real types if and only if
2670 all the range of values of the integer type cannot be
2671 represented by the real type. */
2672 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2673 && TREE_CODE (type) == REAL_TYPE)
2674 {
2675 tree type_low_bound, type_high_bound;
2676 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2677
2678 /* Don't warn about char y = 0xff; float x = (int) y; */
2679 expr = get_unwidened (expr, 0);
2680 expr_type = TREE_TYPE (expr);
2681
2682 type_low_bound = TYPE_MIN_VALUE (expr_type);
2683 type_high_bound = TYPE_MAX_VALUE (expr_type);
2684 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2685 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2686
2687 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2688 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2689 give_warning = true;
2690 }
2691
2692 /* Warn for real types converted to smaller real types. */
2693 else if (TREE_CODE (expr_type) == REAL_TYPE
2694 && TREE_CODE (type) == REAL_TYPE
2695 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2696 give_warning = true;
2697 }
2698
2699 return give_warning;
2700 }
2701
2702 /* Warns if the conversion of EXPR to TYPE may alter a value.
2703 This is a helper function for warnings_for_convert_and_check. */
2704
2705 static void
2706 conversion_warning (tree type, tree expr)
2707 {
2708 tree expr_type = TREE_TYPE (expr);
2709 location_t loc = EXPR_LOC_OR_HERE (expr);
2710
2711 if (!warn_conversion && !warn_sign_conversion)
2712 return;
2713
2714 switch (TREE_CODE (expr))
2715 {
2716 case EQ_EXPR:
2717 case NE_EXPR:
2718 case LE_EXPR:
2719 case GE_EXPR:
2720 case LT_EXPR:
2721 case GT_EXPR:
2722 case TRUTH_ANDIF_EXPR:
2723 case TRUTH_ORIF_EXPR:
2724 case TRUTH_AND_EXPR:
2725 case TRUTH_OR_EXPR:
2726 case TRUTH_XOR_EXPR:
2727 case TRUTH_NOT_EXPR:
2728 /* Conversion from boolean to a signed:1 bit-field (which only
2729 can hold the values 0 and -1) doesn't lose information - but
2730 it does change the value. */
2731 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2732 warning_at (loc, OPT_Wconversion,
2733 "conversion to %qT from boolean expression", type);
2734 return;
2735
2736 case REAL_CST:
2737 case INTEGER_CST:
2738 if (unsafe_conversion_p (type, expr, true))
2739 warning_at (loc, OPT_Wconversion,
2740 "conversion to %qT alters %qT constant value",
2741 type, expr_type);
2742 return;
2743
2744 case COND_EXPR:
2745 {
2746 /* In case of COND_EXPR, we do not care about the type of
2747 COND_EXPR, only about the conversion of each operand. */
2748 tree op1 = TREE_OPERAND (expr, 1);
2749 tree op2 = TREE_OPERAND (expr, 2);
2750
2751 conversion_warning (type, op1);
2752 conversion_warning (type, op2);
2753 return;
2754 }
2755
2756 default: /* 'expr' is not a constant. */
2757 if (unsafe_conversion_p (type, expr, true))
2758 warning_at (loc, OPT_Wconversion,
2759 "conversion to %qT from %qT may alter its value",
2760 type, expr_type);
2761 }
2762 }
2763
2764 /* Produce warnings after a conversion. RESULT is the result of
2765 converting EXPR to TYPE. This is a helper function for
2766 convert_and_check and cp_convert_and_check. */
2767
2768 void
2769 warnings_for_convert_and_check (tree type, tree expr, tree result)
2770 {
2771 location_t loc = EXPR_LOC_OR_HERE (expr);
2772
2773 if (TREE_CODE (expr) == INTEGER_CST
2774 && (TREE_CODE (type) == INTEGER_TYPE
2775 || TREE_CODE (type) == ENUMERAL_TYPE)
2776 && !int_fits_type_p (expr, type))
2777 {
2778 /* Do not diagnose overflow in a constant expression merely
2779 because a conversion overflowed. */
2780 if (TREE_OVERFLOW (result))
2781 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2782
2783 if (TYPE_UNSIGNED (type))
2784 {
2785 /* This detects cases like converting -129 or 256 to
2786 unsigned char. */
2787 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2788 warning_at (loc, OPT_Woverflow,
2789 "large integer implicitly truncated to unsigned type");
2790 else
2791 conversion_warning (type, expr);
2792 }
2793 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2794 warning (OPT_Woverflow,
2795 "overflow in implicit constant conversion");
2796 /* No warning for converting 0x80000000 to int. */
2797 else if (pedantic
2798 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2799 || TYPE_PRECISION (TREE_TYPE (expr))
2800 != TYPE_PRECISION (type)))
2801 warning_at (loc, OPT_Woverflow,
2802 "overflow in implicit constant conversion");
2803
2804 else
2805 conversion_warning (type, expr);
2806 }
2807 else if ((TREE_CODE (result) == INTEGER_CST
2808 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2809 warning_at (loc, OPT_Woverflow,
2810 "overflow in implicit constant conversion");
2811 else
2812 conversion_warning (type, expr);
2813 }
2814
2815
2816 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2817 Invoke this function on every expression that is converted implicitly,
2818 i.e. because of language rules and not because of an explicit cast. */
2819
2820 tree
2821 convert_and_check (tree type, tree expr)
2822 {
2823 tree result;
2824 tree expr_for_warning;
2825
2826 /* Convert from a value with possible excess precision rather than
2827 via the semantic type, but do not warn about values not fitting
2828 exactly in the semantic type. */
2829 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2830 {
2831 tree orig_type = TREE_TYPE (expr);
2832 expr = TREE_OPERAND (expr, 0);
2833 expr_for_warning = convert (orig_type, expr);
2834 if (orig_type == type)
2835 return expr_for_warning;
2836 }
2837 else
2838 expr_for_warning = expr;
2839
2840 if (TREE_TYPE (expr) == type)
2841 return expr;
2842
2843 result = convert (type, expr);
2844
2845 if (c_inhibit_evaluation_warnings == 0
2846 && !TREE_OVERFLOW_P (expr)
2847 && result != error_mark_node)
2848 warnings_for_convert_and_check (type, expr_for_warning, result);
2849
2850 return result;
2851 }
2852 \f
2853 /* A node in a list that describes references to variables (EXPR), which are
2854 either read accesses if WRITER is zero, or write accesses, in which case
2855 WRITER is the parent of EXPR. */
2856 struct tlist
2857 {
2858 struct tlist *next;
2859 tree expr, writer;
2860 };
2861
2862 /* Used to implement a cache the results of a call to verify_tree. We only
2863 use this for SAVE_EXPRs. */
2864 struct tlist_cache
2865 {
2866 struct tlist_cache *next;
2867 struct tlist *cache_before_sp;
2868 struct tlist *cache_after_sp;
2869 tree expr;
2870 };
2871
2872 /* Obstack to use when allocating tlist structures, and corresponding
2873 firstobj. */
2874 static struct obstack tlist_obstack;
2875 static char *tlist_firstobj = 0;
2876
2877 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2878 warnings. */
2879 static struct tlist *warned_ids;
2880 /* SAVE_EXPRs need special treatment. We process them only once and then
2881 cache the results. */
2882 static struct tlist_cache *save_expr_cache;
2883
2884 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2885 static void merge_tlist (struct tlist **, struct tlist *, int);
2886 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2887 static int warning_candidate_p (tree);
2888 static bool candidate_equal_p (const_tree, const_tree);
2889 static void warn_for_collisions (struct tlist *);
2890 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2891 static struct tlist *new_tlist (struct tlist *, tree, tree);
2892
2893 /* Create a new struct tlist and fill in its fields. */
2894 static struct tlist *
2895 new_tlist (struct tlist *next, tree t, tree writer)
2896 {
2897 struct tlist *l;
2898 l = XOBNEW (&tlist_obstack, struct tlist);
2899 l->next = next;
2900 l->expr = t;
2901 l->writer = writer;
2902 return l;
2903 }
2904
2905 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2906 is nonnull, we ignore any node we find which has a writer equal to it. */
2907
2908 static void
2909 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2910 {
2911 while (add)
2912 {
2913 struct tlist *next = add->next;
2914 if (!copy)
2915 add->next = *to;
2916 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2917 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2918 add = next;
2919 }
2920 }
2921
2922 /* Merge the nodes of ADD into TO. This merging process is done so that for
2923 each variable that already exists in TO, no new node is added; however if
2924 there is a write access recorded in ADD, and an occurrence on TO is only
2925 a read access, then the occurrence in TO will be modified to record the
2926 write. */
2927
2928 static void
2929 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2930 {
2931 struct tlist **end = to;
2932
2933 while (*end)
2934 end = &(*end)->next;
2935
2936 while (add)
2937 {
2938 int found = 0;
2939 struct tlist *tmp2;
2940 struct tlist *next = add->next;
2941
2942 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2943 if (candidate_equal_p (tmp2->expr, add->expr))
2944 {
2945 found = 1;
2946 if (!tmp2->writer)
2947 tmp2->writer = add->writer;
2948 }
2949 if (!found)
2950 {
2951 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2952 end = &(*end)->next;
2953 *end = 0;
2954 }
2955 add = next;
2956 }
2957 }
2958
2959 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2960 references in list LIST conflict with it, excluding reads if ONLY writers
2961 is nonzero. */
2962
2963 static void
2964 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2965 int only_writes)
2966 {
2967 struct tlist *tmp;
2968
2969 /* Avoid duplicate warnings. */
2970 for (tmp = warned_ids; tmp; tmp = tmp->next)
2971 if (candidate_equal_p (tmp->expr, written))
2972 return;
2973
2974 while (list)
2975 {
2976 if (candidate_equal_p (list->expr, written)
2977 && !candidate_equal_p (list->writer, writer)
2978 && (!only_writes || list->writer))
2979 {
2980 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2981 warning_at (EXPR_LOC_OR_HERE (writer),
2982 OPT_Wsequence_point, "operation on %qE may be undefined",
2983 list->expr);
2984 }
2985 list = list->next;
2986 }
2987 }
2988
2989 /* Given a list LIST of references to variables, find whether any of these
2990 can cause conflicts due to missing sequence points. */
2991
2992 static void
2993 warn_for_collisions (struct tlist *list)
2994 {
2995 struct tlist *tmp;
2996
2997 for (tmp = list; tmp; tmp = tmp->next)
2998 {
2999 if (tmp->writer)
3000 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3001 }
3002 }
3003
3004 /* Return nonzero if X is a tree that can be verified by the sequence point
3005 warnings. */
3006 static int
3007 warning_candidate_p (tree x)
3008 {
3009 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3010 return 0;
3011
3012 if (TREE_CODE (x) == BLOCK)
3013 return 0;
3014
3015 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3016 (lvalue_p) crash on TRY/CATCH. */
3017 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3018 return 0;
3019
3020 if (!lvalue_p (x))
3021 return 0;
3022
3023 /* No point to track non-const calls, they will never satisfy
3024 operand_equal_p. */
3025 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3026 return 0;
3027
3028 if (TREE_CODE (x) == STRING_CST)
3029 return 0;
3030
3031 return 1;
3032 }
3033
3034 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3035 static bool
3036 candidate_equal_p (const_tree x, const_tree y)
3037 {
3038 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3039 }
3040
3041 /* Walk the tree X, and record accesses to variables. If X is written by the
3042 parent tree, WRITER is the parent.
3043 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3044 expression or its only operand forces a sequence point, then everything up
3045 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3046 in PNO_SP.
3047 Once we return, we will have emitted warnings if any subexpression before
3048 such a sequence point could be undefined. On a higher level, however, the
3049 sequence point may not be relevant, and we'll merge the two lists.
3050
3051 Example: (b++, a) + b;
3052 The call that processes the COMPOUND_EXPR will store the increment of B
3053 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3054 processes the PLUS_EXPR will need to merge the two lists so that
3055 eventually, all accesses end up on the same list (and we'll warn about the
3056 unordered subexpressions b++ and b.
3057
3058 A note on merging. If we modify the former example so that our expression
3059 becomes
3060 (b++, b) + a
3061 care must be taken not simply to add all three expressions into the final
3062 PNO_SP list. The function merge_tlist takes care of that by merging the
3063 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3064 way, so that no more than one access to B is recorded. */
3065
3066 static void
3067 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3068 tree writer)
3069 {
3070 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3071 enum tree_code code;
3072 enum tree_code_class cl;
3073
3074 /* X may be NULL if it is the operand of an empty statement expression
3075 ({ }). */
3076 if (x == NULL)
3077 return;
3078
3079 restart:
3080 code = TREE_CODE (x);
3081 cl = TREE_CODE_CLASS (code);
3082
3083 if (warning_candidate_p (x))
3084 *pno_sp = new_tlist (*pno_sp, x, writer);
3085
3086 switch (code)
3087 {
3088 case CONSTRUCTOR:
3089 case SIZEOF_EXPR:
3090 return;
3091
3092 case COMPOUND_EXPR:
3093 case TRUTH_ANDIF_EXPR:
3094 case TRUTH_ORIF_EXPR:
3095 tmp_before = tmp_nosp = tmp_list3 = 0;
3096 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3097 warn_for_collisions (tmp_nosp);
3098 merge_tlist (pbefore_sp, tmp_before, 0);
3099 merge_tlist (pbefore_sp, tmp_nosp, 0);
3100 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3101 merge_tlist (pbefore_sp, tmp_list3, 0);
3102 return;
3103
3104 case COND_EXPR:
3105 tmp_before = tmp_list2 = 0;
3106 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3107 warn_for_collisions (tmp_list2);
3108 merge_tlist (pbefore_sp, tmp_before, 0);
3109 merge_tlist (pbefore_sp, tmp_list2, 1);
3110
3111 tmp_list3 = tmp_nosp = 0;
3112 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3113 warn_for_collisions (tmp_nosp);
3114 merge_tlist (pbefore_sp, tmp_list3, 0);
3115
3116 tmp_list3 = tmp_list2 = 0;
3117 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3118 warn_for_collisions (tmp_list2);
3119 merge_tlist (pbefore_sp, tmp_list3, 0);
3120 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3121 two first, to avoid warning for (a ? b++ : b++). */
3122 merge_tlist (&tmp_nosp, tmp_list2, 0);
3123 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3124 return;
3125
3126 case PREDECREMENT_EXPR:
3127 case PREINCREMENT_EXPR:
3128 case POSTDECREMENT_EXPR:
3129 case POSTINCREMENT_EXPR:
3130 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3131 return;
3132
3133 case MODIFY_EXPR:
3134 tmp_before = tmp_nosp = tmp_list3 = 0;
3135 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3136 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3137 /* Expressions inside the LHS are not ordered wrt. the sequence points
3138 in the RHS. Example:
3139 *a = (a++, 2)
3140 Despite the fact that the modification of "a" is in the before_sp
3141 list (tmp_before), it conflicts with the use of "a" in the LHS.
3142 We can handle this by adding the contents of tmp_list3
3143 to those of tmp_before, and redoing the collision warnings for that
3144 list. */
3145 add_tlist (&tmp_before, tmp_list3, x, 1);
3146 warn_for_collisions (tmp_before);
3147 /* Exclude the LHS itself here; we first have to merge it into the
3148 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3149 didn't exclude the LHS, we'd get it twice, once as a read and once
3150 as a write. */
3151 add_tlist (pno_sp, tmp_list3, x, 0);
3152 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3153
3154 merge_tlist (pbefore_sp, tmp_before, 0);
3155 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3156 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3157 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3158 return;
3159
3160 case CALL_EXPR:
3161 /* We need to warn about conflicts among arguments and conflicts between
3162 args and the function address. Side effects of the function address,
3163 however, are not ordered by the sequence point of the call. */
3164 {
3165 call_expr_arg_iterator iter;
3166 tree arg;
3167 tmp_before = tmp_nosp = 0;
3168 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3169 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3170 {
3171 tmp_list2 = tmp_list3 = 0;
3172 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3173 merge_tlist (&tmp_list3, tmp_list2, 0);
3174 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3175 }
3176 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3177 warn_for_collisions (tmp_before);
3178 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3179 return;
3180 }
3181
3182 case TREE_LIST:
3183 /* Scan all the list, e.g. indices of multi dimensional array. */
3184 while (x)
3185 {
3186 tmp_before = tmp_nosp = 0;
3187 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3188 merge_tlist (&tmp_nosp, tmp_before, 0);
3189 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3190 x = TREE_CHAIN (x);
3191 }
3192 return;
3193
3194 case SAVE_EXPR:
3195 {
3196 struct tlist_cache *t;
3197 for (t = save_expr_cache; t; t = t->next)
3198 if (candidate_equal_p (t->expr, x))
3199 break;
3200
3201 if (!t)
3202 {
3203 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3204 t->next = save_expr_cache;
3205 t->expr = x;
3206 save_expr_cache = t;
3207
3208 tmp_before = tmp_nosp = 0;
3209 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3210 warn_for_collisions (tmp_nosp);
3211
3212 tmp_list3 = 0;
3213 while (tmp_nosp)
3214 {
3215 struct tlist *t = tmp_nosp;
3216 tmp_nosp = t->next;
3217 merge_tlist (&tmp_list3, t, 0);
3218 }
3219 t->cache_before_sp = tmp_before;
3220 t->cache_after_sp = tmp_list3;
3221 }
3222 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3223 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3224 return;
3225 }
3226
3227 case ADDR_EXPR:
3228 x = TREE_OPERAND (x, 0);
3229 if (DECL_P (x))
3230 return;
3231 writer = 0;
3232 goto restart;
3233
3234 default:
3235 /* For other expressions, simply recurse on their operands.
3236 Manual tail recursion for unary expressions.
3237 Other non-expressions need not be processed. */
3238 if (cl == tcc_unary)
3239 {
3240 x = TREE_OPERAND (x, 0);
3241 writer = 0;
3242 goto restart;
3243 }
3244 else if (IS_EXPR_CODE_CLASS (cl))
3245 {
3246 int lp;
3247 int max = TREE_OPERAND_LENGTH (x);
3248 for (lp = 0; lp < max; lp++)
3249 {
3250 tmp_before = tmp_nosp = 0;
3251 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3252 merge_tlist (&tmp_nosp, tmp_before, 0);
3253 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3254 }
3255 }
3256 return;
3257 }
3258 }
3259
3260 /* Try to warn for undefined behavior in EXPR due to missing sequence
3261 points. */
3262
3263 DEBUG_FUNCTION void
3264 verify_sequence_points (tree expr)
3265 {
3266 struct tlist *before_sp = 0, *after_sp = 0;
3267
3268 warned_ids = 0;
3269 save_expr_cache = 0;
3270 if (tlist_firstobj == 0)
3271 {
3272 gcc_obstack_init (&tlist_obstack);
3273 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3274 }
3275
3276 verify_tree (expr, &before_sp, &after_sp, 0);
3277 warn_for_collisions (after_sp);
3278 obstack_free (&tlist_obstack, tlist_firstobj);
3279 }
3280 \f
3281 /* Validate the expression after `case' and apply default promotions. */
3282
3283 static tree
3284 check_case_value (tree value)
3285 {
3286 if (value == NULL_TREE)
3287 return value;
3288
3289 if (TREE_CODE (value) == INTEGER_CST)
3290 /* Promote char or short to int. */
3291 value = perform_integral_promotions (value);
3292 else if (value != error_mark_node)
3293 {
3294 error ("case label does not reduce to an integer constant");
3295 value = error_mark_node;
3296 }
3297
3298 constant_expression_warning (value);
3299
3300 return value;
3301 }
3302 \f
3303 /* See if the case values LOW and HIGH are in the range of the original
3304 type (i.e. before the default conversion to int) of the switch testing
3305 expression.
3306 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3307 the type before promoting it. CASE_LOW_P is a pointer to the lower
3308 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3309 if the case is not a case range.
3310 The caller has to make sure that we are not called with NULL for
3311 CASE_LOW_P (i.e. the default case).
3312 Returns true if the case label is in range of ORIG_TYPE (saturated or
3313 untouched) or false if the label is out of range. */
3314
3315 static bool
3316 check_case_bounds (tree type, tree orig_type,
3317 tree *case_low_p, tree *case_high_p)
3318 {
3319 tree min_value, max_value;
3320 tree case_low = *case_low_p;
3321 tree case_high = case_high_p ? *case_high_p : case_low;
3322
3323 /* If there was a problem with the original type, do nothing. */
3324 if (orig_type == error_mark_node)
3325 return true;
3326
3327 min_value = TYPE_MIN_VALUE (orig_type);
3328 max_value = TYPE_MAX_VALUE (orig_type);
3329
3330 /* Case label is less than minimum for type. */
3331 if (tree_int_cst_compare (case_low, min_value) < 0
3332 && tree_int_cst_compare (case_high, min_value) < 0)
3333 {
3334 warning (0, "case label value is less than minimum value for type");
3335 return false;
3336 }
3337
3338 /* Case value is greater than maximum for type. */
3339 if (tree_int_cst_compare (case_low, max_value) > 0
3340 && tree_int_cst_compare (case_high, max_value) > 0)
3341 {
3342 warning (0, "case label value exceeds maximum value for type");
3343 return false;
3344 }
3345
3346 /* Saturate lower case label value to minimum. */
3347 if (tree_int_cst_compare (case_high, min_value) >= 0
3348 && tree_int_cst_compare (case_low, min_value) < 0)
3349 {
3350 warning (0, "lower value in case label range"
3351 " less than minimum value for type");
3352 case_low = min_value;
3353 }
3354
3355 /* Saturate upper case label value to maximum. */
3356 if (tree_int_cst_compare (case_low, max_value) <= 0
3357 && tree_int_cst_compare (case_high, max_value) > 0)
3358 {
3359 warning (0, "upper value in case label range"
3360 " exceeds maximum value for type");
3361 case_high = max_value;
3362 }
3363
3364 if (*case_low_p != case_low)
3365 *case_low_p = convert (type, case_low);
3366 if (case_high_p && *case_high_p != case_high)
3367 *case_high_p = convert (type, case_high);
3368
3369 return true;
3370 }
3371 \f
3372 /* Return an integer type with BITS bits of precision,
3373 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3374
3375 tree
3376 c_common_type_for_size (unsigned int bits, int unsignedp)
3377 {
3378 if (bits == TYPE_PRECISION (integer_type_node))
3379 return unsignedp ? unsigned_type_node : integer_type_node;
3380
3381 if (bits == TYPE_PRECISION (signed_char_type_node))
3382 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3383
3384 if (bits == TYPE_PRECISION (short_integer_type_node))
3385 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3386
3387 if (bits == TYPE_PRECISION (long_integer_type_node))
3388 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3389
3390 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3391 return (unsignedp ? long_long_unsigned_type_node
3392 : long_long_integer_type_node);
3393
3394 if (int128_integer_type_node
3395 && bits == TYPE_PRECISION (int128_integer_type_node))
3396 return (unsignedp ? int128_unsigned_type_node
3397 : int128_integer_type_node);
3398
3399 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3400 return (unsignedp ? widest_unsigned_literal_type_node
3401 : widest_integer_literal_type_node);
3402
3403 if (bits <= TYPE_PRECISION (intQI_type_node))
3404 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3405
3406 if (bits <= TYPE_PRECISION (intHI_type_node))
3407 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3408
3409 if (bits <= TYPE_PRECISION (intSI_type_node))
3410 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3411
3412 if (bits <= TYPE_PRECISION (intDI_type_node))
3413 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3414
3415 return 0;
3416 }
3417
3418 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3419 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3420 and saturating if SATP is nonzero, otherwise not saturating. */
3421
3422 tree
3423 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3424 int unsignedp, int satp)
3425 {
3426 enum machine_mode mode;
3427 if (ibit == 0)
3428 mode = unsignedp ? UQQmode : QQmode;
3429 else
3430 mode = unsignedp ? UHAmode : HAmode;
3431
3432 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3433 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3434 break;
3435
3436 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3437 {
3438 sorry ("GCC cannot support operators with integer types and "
3439 "fixed-point types that have too many integral and "
3440 "fractional bits together");
3441 return 0;
3442 }
3443
3444 return c_common_type_for_mode (mode, satp);
3445 }
3446
3447 /* Used for communication between c_common_type_for_mode and
3448 c_register_builtin_type. */
3449 static GTY(()) tree registered_builtin_types;
3450
3451 /* Return a data type that has machine mode MODE.
3452 If the mode is an integer,
3453 then UNSIGNEDP selects between signed and unsigned types.
3454 If the mode is a fixed-point mode,
3455 then UNSIGNEDP selects between saturating and nonsaturating types. */
3456
3457 tree
3458 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3459 {
3460 tree t;
3461
3462 if (mode == TYPE_MODE (integer_type_node))
3463 return unsignedp ? unsigned_type_node : integer_type_node;
3464
3465 if (mode == TYPE_MODE (signed_char_type_node))
3466 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3467
3468 if (mode == TYPE_MODE (short_integer_type_node))
3469 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3470
3471 if (mode == TYPE_MODE (long_integer_type_node))
3472 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3473
3474 if (mode == TYPE_MODE (long_long_integer_type_node))
3475 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3476
3477 if (int128_integer_type_node
3478 && mode == TYPE_MODE (int128_integer_type_node))
3479 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3480
3481 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3482 return unsignedp ? widest_unsigned_literal_type_node
3483 : widest_integer_literal_type_node;
3484
3485 if (mode == QImode)
3486 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3487
3488 if (mode == HImode)
3489 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3490
3491 if (mode == SImode)
3492 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3493
3494 if (mode == DImode)
3495 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3496
3497 #if HOST_BITS_PER_WIDE_INT >= 64
3498 if (mode == TYPE_MODE (intTI_type_node))
3499 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3500 #endif
3501
3502 if (mode == TYPE_MODE (float_type_node))
3503 return float_type_node;
3504
3505 if (mode == TYPE_MODE (double_type_node))
3506 return double_type_node;
3507
3508 if (mode == TYPE_MODE (long_double_type_node))
3509 return long_double_type_node;
3510
3511 if (mode == TYPE_MODE (void_type_node))
3512 return void_type_node;
3513
3514 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3515 return (unsignedp
3516 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3517 : make_signed_type (GET_MODE_PRECISION (mode)));
3518
3519 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3520 return (unsignedp
3521 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3522 : make_signed_type (GET_MODE_PRECISION (mode)));
3523
3524 if (COMPLEX_MODE_P (mode))
3525 {
3526 enum machine_mode inner_mode;
3527 tree inner_type;
3528
3529 if (mode == TYPE_MODE (complex_float_type_node))
3530 return complex_float_type_node;
3531 if (mode == TYPE_MODE (complex_double_type_node))
3532 return complex_double_type_node;
3533 if (mode == TYPE_MODE (complex_long_double_type_node))
3534 return complex_long_double_type_node;
3535
3536 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3537 return complex_integer_type_node;
3538
3539 inner_mode = GET_MODE_INNER (mode);
3540 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3541 if (inner_type != NULL_TREE)
3542 return build_complex_type (inner_type);
3543 }
3544 else if (VECTOR_MODE_P (mode))
3545 {
3546 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3547 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3548 if (inner_type != NULL_TREE)
3549 return build_vector_type_for_mode (inner_type, mode);
3550 }
3551
3552 if (mode == TYPE_MODE (dfloat32_type_node))
3553 return dfloat32_type_node;
3554 if (mode == TYPE_MODE (dfloat64_type_node))
3555 return dfloat64_type_node;
3556 if (mode == TYPE_MODE (dfloat128_type_node))
3557 return dfloat128_type_node;
3558
3559 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3560 {
3561 if (mode == TYPE_MODE (short_fract_type_node))
3562 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3563 if (mode == TYPE_MODE (fract_type_node))
3564 return unsignedp ? sat_fract_type_node : fract_type_node;
3565 if (mode == TYPE_MODE (long_fract_type_node))
3566 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3567 if (mode == TYPE_MODE (long_long_fract_type_node))
3568 return unsignedp ? sat_long_long_fract_type_node
3569 : long_long_fract_type_node;
3570
3571 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3572 return unsignedp ? sat_unsigned_short_fract_type_node
3573 : unsigned_short_fract_type_node;
3574 if (mode == TYPE_MODE (unsigned_fract_type_node))
3575 return unsignedp ? sat_unsigned_fract_type_node
3576 : unsigned_fract_type_node;
3577 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3578 return unsignedp ? sat_unsigned_long_fract_type_node
3579 : unsigned_long_fract_type_node;
3580 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3581 return unsignedp ? sat_unsigned_long_long_fract_type_node
3582 : unsigned_long_long_fract_type_node;
3583
3584 if (mode == TYPE_MODE (short_accum_type_node))
3585 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3586 if (mode == TYPE_MODE (accum_type_node))
3587 return unsignedp ? sat_accum_type_node : accum_type_node;
3588 if (mode == TYPE_MODE (long_accum_type_node))
3589 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3590 if (mode == TYPE_MODE (long_long_accum_type_node))
3591 return unsignedp ? sat_long_long_accum_type_node
3592 : long_long_accum_type_node;
3593
3594 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3595 return unsignedp ? sat_unsigned_short_accum_type_node
3596 : unsigned_short_accum_type_node;
3597 if (mode == TYPE_MODE (unsigned_accum_type_node))
3598 return unsignedp ? sat_unsigned_accum_type_node
3599 : unsigned_accum_type_node;
3600 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3601 return unsignedp ? sat_unsigned_long_accum_type_node
3602 : unsigned_long_accum_type_node;
3603 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3604 return unsignedp ? sat_unsigned_long_long_accum_type_node
3605 : unsigned_long_long_accum_type_node;
3606
3607 if (mode == QQmode)
3608 return unsignedp ? sat_qq_type_node : qq_type_node;
3609 if (mode == HQmode)
3610 return unsignedp ? sat_hq_type_node : hq_type_node;
3611 if (mode == SQmode)
3612 return unsignedp ? sat_sq_type_node : sq_type_node;
3613 if (mode == DQmode)
3614 return unsignedp ? sat_dq_type_node : dq_type_node;
3615 if (mode == TQmode)
3616 return unsignedp ? sat_tq_type_node : tq_type_node;
3617
3618 if (mode == UQQmode)
3619 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3620 if (mode == UHQmode)
3621 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3622 if (mode == USQmode)
3623 return unsignedp ? sat_usq_type_node : usq_type_node;
3624 if (mode == UDQmode)
3625 return unsignedp ? sat_udq_type_node : udq_type_node;
3626 if (mode == UTQmode)
3627 return unsignedp ? sat_utq_type_node : utq_type_node;
3628
3629 if (mode == HAmode)
3630 return unsignedp ? sat_ha_type_node : ha_type_node;
3631 if (mode == SAmode)
3632 return unsignedp ? sat_sa_type_node : sa_type_node;
3633 if (mode == DAmode)
3634 return unsignedp ? sat_da_type_node : da_type_node;
3635 if (mode == TAmode)
3636 return unsignedp ? sat_ta_type_node : ta_type_node;
3637
3638 if (mode == UHAmode)
3639 return unsignedp ? sat_uha_type_node : uha_type_node;
3640 if (mode == USAmode)
3641 return unsignedp ? sat_usa_type_node : usa_type_node;
3642 if (mode == UDAmode)
3643 return unsignedp ? sat_uda_type_node : uda_type_node;
3644 if (mode == UTAmode)
3645 return unsignedp ? sat_uta_type_node : uta_type_node;
3646 }
3647
3648 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3649 if (TYPE_MODE (TREE_VALUE (t)) == mode
3650 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3651 return TREE_VALUE (t);
3652
3653 return 0;
3654 }
3655
3656 tree
3657 c_common_unsigned_type (tree type)
3658 {
3659 return c_common_signed_or_unsigned_type (1, type);
3660 }
3661
3662 /* Return a signed type the same as TYPE in other respects. */
3663
3664 tree
3665 c_common_signed_type (tree type)
3666 {
3667 return c_common_signed_or_unsigned_type (0, type);
3668 }
3669
3670 /* Return a type the same as TYPE except unsigned or
3671 signed according to UNSIGNEDP. */
3672
3673 tree
3674 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3675 {
3676 tree type1;
3677
3678 /* This block of code emulates the behavior of the old
3679 c_common_unsigned_type. In particular, it returns
3680 long_unsigned_type_node if passed a long, even when a int would
3681 have the same size. This is necessary for warnings to work
3682 correctly in archs where sizeof(int) == sizeof(long) */
3683
3684 type1 = TYPE_MAIN_VARIANT (type);
3685 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3686 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3687 if (type1 == integer_type_node || type1 == unsigned_type_node)
3688 return unsignedp ? unsigned_type_node : integer_type_node;
3689 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3690 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3691 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3692 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3693 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3694 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3695 if (int128_integer_type_node
3696 && (type1 == int128_integer_type_node
3697 || type1 == int128_unsigned_type_node))
3698 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3699 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3700 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3701 #if HOST_BITS_PER_WIDE_INT >= 64
3702 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3703 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3704 #endif
3705 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3706 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3707 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3708 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3709 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3710 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3711 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3712 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3713
3714 #define C_COMMON_FIXED_TYPES(NAME) \
3715 if (type1 == short_ ## NAME ## _type_node \
3716 || type1 == unsigned_short_ ## NAME ## _type_node) \
3717 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3718 : short_ ## NAME ## _type_node; \
3719 if (type1 == NAME ## _type_node \
3720 || type1 == unsigned_ ## NAME ## _type_node) \
3721 return unsignedp ? unsigned_ ## NAME ## _type_node \
3722 : NAME ## _type_node; \
3723 if (type1 == long_ ## NAME ## _type_node \
3724 || type1 == unsigned_long_ ## NAME ## _type_node) \
3725 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3726 : long_ ## NAME ## _type_node; \
3727 if (type1 == long_long_ ## NAME ## _type_node \
3728 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3729 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3730 : long_long_ ## NAME ## _type_node;
3731
3732 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3733 if (type1 == NAME ## _type_node \
3734 || type1 == u ## NAME ## _type_node) \
3735 return unsignedp ? u ## NAME ## _type_node \
3736 : NAME ## _type_node;
3737
3738 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3739 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3740 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3741 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3742 : sat_ ## short_ ## NAME ## _type_node; \
3743 if (type1 == sat_ ## NAME ## _type_node \
3744 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3745 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3746 : sat_ ## NAME ## _type_node; \
3747 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3748 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3749 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3750 : sat_ ## long_ ## NAME ## _type_node; \
3751 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3752 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3753 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3754 : sat_ ## long_long_ ## NAME ## _type_node;
3755
3756 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3757 if (type1 == sat_ ## NAME ## _type_node \
3758 || type1 == sat_ ## u ## NAME ## _type_node) \
3759 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3760 : sat_ ## NAME ## _type_node;
3761
3762 C_COMMON_FIXED_TYPES (fract);
3763 C_COMMON_FIXED_TYPES_SAT (fract);
3764 C_COMMON_FIXED_TYPES (accum);
3765 C_COMMON_FIXED_TYPES_SAT (accum);
3766
3767 C_COMMON_FIXED_MODE_TYPES (qq);
3768 C_COMMON_FIXED_MODE_TYPES (hq);
3769 C_COMMON_FIXED_MODE_TYPES (sq);
3770 C_COMMON_FIXED_MODE_TYPES (dq);
3771 C_COMMON_FIXED_MODE_TYPES (tq);
3772 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3773 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3774 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3775 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3776 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3777 C_COMMON_FIXED_MODE_TYPES (ha);
3778 C_COMMON_FIXED_MODE_TYPES (sa);
3779 C_COMMON_FIXED_MODE_TYPES (da);
3780 C_COMMON_FIXED_MODE_TYPES (ta);
3781 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3782 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3783 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3784 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3785
3786 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3787 the precision; they have precision set to match their range, but
3788 may use a wider mode to match an ABI. If we change modes, we may
3789 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3790 the precision as well, so as to yield correct results for
3791 bit-field types. C++ does not have these separate bit-field
3792 types, and producing a signed or unsigned variant of an
3793 ENUMERAL_TYPE may cause other problems as well. */
3794
3795 if (!INTEGRAL_TYPE_P (type)
3796 || TYPE_UNSIGNED (type) == unsignedp)
3797 return type;
3798
3799 #define TYPE_OK(node) \
3800 (TYPE_MODE (type) == TYPE_MODE (node) \
3801 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3802 if (TYPE_OK (signed_char_type_node))
3803 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3804 if (TYPE_OK (integer_type_node))
3805 return unsignedp ? unsigned_type_node : integer_type_node;
3806 if (TYPE_OK (short_integer_type_node))
3807 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3808 if (TYPE_OK (long_integer_type_node))
3809 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3810 if (TYPE_OK (long_long_integer_type_node))
3811 return (unsignedp ? long_long_unsigned_type_node
3812 : long_long_integer_type_node);
3813 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3814 return (unsignedp ? int128_unsigned_type_node
3815 : int128_integer_type_node);
3816 if (TYPE_OK (widest_integer_literal_type_node))
3817 return (unsignedp ? widest_unsigned_literal_type_node
3818 : widest_integer_literal_type_node);
3819
3820 #if HOST_BITS_PER_WIDE_INT >= 64
3821 if (TYPE_OK (intTI_type_node))
3822 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3823 #endif
3824 if (TYPE_OK (intDI_type_node))
3825 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3826 if (TYPE_OK (intSI_type_node))
3827 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3828 if (TYPE_OK (intHI_type_node))
3829 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3830 if (TYPE_OK (intQI_type_node))
3831 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3832 #undef TYPE_OK
3833
3834 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3835 }
3836
3837 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3838
3839 tree
3840 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3841 {
3842 /* Extended integer types of the same width as a standard type have
3843 lesser rank, so those of the same width as int promote to int or
3844 unsigned int and are valid for printf formats expecting int or
3845 unsigned int. To avoid such special cases, avoid creating
3846 extended integer types for bit-fields if a standard integer type
3847 is available. */
3848 if (width == TYPE_PRECISION (integer_type_node))
3849 return unsignedp ? unsigned_type_node : integer_type_node;
3850 if (width == TYPE_PRECISION (signed_char_type_node))
3851 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3852 if (width == TYPE_PRECISION (short_integer_type_node))
3853 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3854 if (width == TYPE_PRECISION (long_integer_type_node))
3855 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3856 if (width == TYPE_PRECISION (long_long_integer_type_node))
3857 return (unsignedp ? long_long_unsigned_type_node
3858 : long_long_integer_type_node);
3859 if (int128_integer_type_node
3860 && width == TYPE_PRECISION (int128_integer_type_node))
3861 return (unsignedp ? int128_unsigned_type_node
3862 : int128_integer_type_node);
3863 return build_nonstandard_integer_type (width, unsignedp);
3864 }
3865
3866 /* The C version of the register_builtin_type langhook. */
3867
3868 void
3869 c_register_builtin_type (tree type, const char* name)
3870 {
3871 tree decl;
3872
3873 decl = build_decl (UNKNOWN_LOCATION,
3874 TYPE_DECL, get_identifier (name), type);
3875 DECL_ARTIFICIAL (decl) = 1;
3876 if (!TYPE_NAME (type))
3877 TYPE_NAME (type) = decl;
3878 pushdecl (decl);
3879
3880 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3881 }
3882 \f
3883 /* Print an error message for invalid operands to arith operation
3884 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3885 LOCATION is the location of the message. */
3886
3887 void
3888 binary_op_error (location_t location, enum tree_code code,
3889 tree type0, tree type1)
3890 {
3891 const char *opname;
3892
3893 switch (code)
3894 {
3895 case PLUS_EXPR:
3896 opname = "+"; break;
3897 case MINUS_EXPR:
3898 opname = "-"; break;
3899 case MULT_EXPR:
3900 opname = "*"; break;
3901 case MAX_EXPR:
3902 opname = "max"; break;
3903 case MIN_EXPR:
3904 opname = "min"; break;
3905 case EQ_EXPR:
3906 opname = "=="; break;
3907 case NE_EXPR:
3908 opname = "!="; break;
3909 case LE_EXPR:
3910 opname = "<="; break;
3911 case GE_EXPR:
3912 opname = ">="; break;
3913 case LT_EXPR:
3914 opname = "<"; break;
3915 case GT_EXPR:
3916 opname = ">"; break;
3917 case LSHIFT_EXPR:
3918 opname = "<<"; break;
3919 case RSHIFT_EXPR:
3920 opname = ">>"; break;
3921 case TRUNC_MOD_EXPR:
3922 case FLOOR_MOD_EXPR:
3923 opname = "%"; break;
3924 case TRUNC_DIV_EXPR:
3925 case FLOOR_DIV_EXPR:
3926 opname = "/"; break;
3927 case BIT_AND_EXPR:
3928 opname = "&"; break;
3929 case BIT_IOR_EXPR:
3930 opname = "|"; break;
3931 case TRUTH_ANDIF_EXPR:
3932 opname = "&&"; break;
3933 case TRUTH_ORIF_EXPR:
3934 opname = "||"; break;
3935 case BIT_XOR_EXPR:
3936 opname = "^"; break;
3937 default:
3938 gcc_unreachable ();
3939 }
3940 error_at (location,
3941 "invalid operands to binary %s (have %qT and %qT)", opname,
3942 type0, type1);
3943 }
3944 \f
3945 /* Given an expression as a tree, return its original type. Do this
3946 by stripping any conversion that preserves the sign and precision. */
3947 static tree
3948 expr_original_type (tree expr)
3949 {
3950 STRIP_SIGN_NOPS (expr);
3951 return TREE_TYPE (expr);
3952 }
3953
3954 /* Subroutine of build_binary_op, used for comparison operations.
3955 See if the operands have both been converted from subword integer types
3956 and, if so, perhaps change them both back to their original type.
3957 This function is also responsible for converting the two operands
3958 to the proper common type for comparison.
3959
3960 The arguments of this function are all pointers to local variables
3961 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3962 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3963
3964 If this function returns nonzero, it means that the comparison has
3965 a constant value. What this function returns is an expression for
3966 that value. */
3967
3968 tree
3969 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3970 enum tree_code *rescode_ptr)
3971 {
3972 tree type;
3973 tree op0 = *op0_ptr;
3974 tree op1 = *op1_ptr;
3975 int unsignedp0, unsignedp1;
3976 int real1, real2;
3977 tree primop0, primop1;
3978 enum tree_code code = *rescode_ptr;
3979 location_t loc = EXPR_LOC_OR_HERE (op0);
3980
3981 /* Throw away any conversions to wider types
3982 already present in the operands. */
3983
3984 primop0 = c_common_get_narrower (op0, &unsignedp0);
3985 primop1 = c_common_get_narrower (op1, &unsignedp1);
3986
3987 /* If primopN is first sign-extended from primopN's precision to opN's
3988 precision, then zero-extended from opN's precision to
3989 *restype_ptr precision, shortenings might be invalid. */
3990 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3991 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3992 && !unsignedp0
3993 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3994 primop0 = op0;
3995 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3996 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3997 && !unsignedp1
3998 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3999 primop1 = op1;
4000
4001 /* Handle the case that OP0 does not *contain* a conversion
4002 but it *requires* conversion to FINAL_TYPE. */
4003
4004 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4005 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4006 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4007 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4008
4009 /* If one of the operands must be floated, we cannot optimize. */
4010 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4011 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4012
4013 /* If first arg is constant, swap the args (changing operation
4014 so value is preserved), for canonicalization. Don't do this if
4015 the second arg is 0. */
4016
4017 if (TREE_CONSTANT (primop0)
4018 && !integer_zerop (primop1) && !real_zerop (primop1)
4019 && !fixed_zerop (primop1))
4020 {
4021 tree tem = primop0;
4022 int temi = unsignedp0;
4023 primop0 = primop1;
4024 primop1 = tem;
4025 tem = op0;
4026 op0 = op1;
4027 op1 = tem;
4028 *op0_ptr = op0;
4029 *op1_ptr = op1;
4030 unsignedp0 = unsignedp1;
4031 unsignedp1 = temi;
4032 temi = real1;
4033 real1 = real2;
4034 real2 = temi;
4035
4036 switch (code)
4037 {
4038 case LT_EXPR:
4039 code = GT_EXPR;
4040 break;
4041 case GT_EXPR:
4042 code = LT_EXPR;
4043 break;
4044 case LE_EXPR:
4045 code = GE_EXPR;
4046 break;
4047 case GE_EXPR:
4048 code = LE_EXPR;
4049 break;
4050 default:
4051 break;
4052 }
4053 *rescode_ptr = code;
4054 }
4055
4056 /* If comparing an integer against a constant more bits wide,
4057 maybe we can deduce a value of 1 or 0 independent of the data.
4058 Or else truncate the constant now
4059 rather than extend the variable at run time.
4060
4061 This is only interesting if the constant is the wider arg.
4062 Also, it is not safe if the constant is unsigned and the
4063 variable arg is signed, since in this case the variable
4064 would be sign-extended and then regarded as unsigned.
4065 Our technique fails in this case because the lowest/highest
4066 possible unsigned results don't follow naturally from the
4067 lowest/highest possible values of the variable operand.
4068 For just EQ_EXPR and NE_EXPR there is another technique that
4069 could be used: see if the constant can be faithfully represented
4070 in the other operand's type, by truncating it and reextending it
4071 and see if that preserves the constant's value. */
4072
4073 if (!real1 && !real2
4074 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4075 && TREE_CODE (primop1) == INTEGER_CST
4076 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4077 {
4078 int min_gt, max_gt, min_lt, max_lt;
4079 tree maxval, minval;
4080 /* 1 if comparison is nominally unsigned. */
4081 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4082 tree val;
4083
4084 type = c_common_signed_or_unsigned_type (unsignedp0,
4085 TREE_TYPE (primop0));
4086
4087 maxval = TYPE_MAX_VALUE (type);
4088 minval = TYPE_MIN_VALUE (type);
4089
4090 if (unsignedp && !unsignedp0)
4091 *restype_ptr = c_common_signed_type (*restype_ptr);
4092
4093 if (TREE_TYPE (primop1) != *restype_ptr)
4094 {
4095 /* Convert primop1 to target type, but do not introduce
4096 additional overflow. We know primop1 is an int_cst. */
4097 primop1 = force_fit_type_double (*restype_ptr,
4098 tree_to_double_int (primop1),
4099 0, TREE_OVERFLOW (primop1));
4100 }
4101 if (type != *restype_ptr)
4102 {
4103 minval = convert (*restype_ptr, minval);
4104 maxval = convert (*restype_ptr, maxval);
4105 }
4106
4107 if (unsignedp && unsignedp0)
4108 {
4109 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4110 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4111 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4112 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4113 }
4114 else
4115 {
4116 min_gt = INT_CST_LT (primop1, minval);
4117 max_gt = INT_CST_LT (primop1, maxval);
4118 min_lt = INT_CST_LT (minval, primop1);
4119 max_lt = INT_CST_LT (maxval, primop1);
4120 }
4121
4122 val = 0;
4123 /* This used to be a switch, but Genix compiler can't handle that. */
4124 if (code == NE_EXPR)
4125 {
4126 if (max_lt || min_gt)
4127 val = truthvalue_true_node;
4128 }
4129 else if (code == EQ_EXPR)
4130 {
4131 if (max_lt || min_gt)
4132 val = truthvalue_false_node;
4133 }
4134 else if (code == LT_EXPR)
4135 {
4136 if (max_lt)
4137 val = truthvalue_true_node;
4138 if (!min_lt)
4139 val = truthvalue_false_node;
4140 }
4141 else if (code == GT_EXPR)
4142 {
4143 if (min_gt)
4144 val = truthvalue_true_node;
4145 if (!max_gt)
4146 val = truthvalue_false_node;
4147 }
4148 else if (code == LE_EXPR)
4149 {
4150 if (!max_gt)
4151 val = truthvalue_true_node;
4152 if (min_gt)
4153 val = truthvalue_false_node;
4154 }
4155 else if (code == GE_EXPR)
4156 {
4157 if (!min_lt)
4158 val = truthvalue_true_node;
4159 if (max_lt)
4160 val = truthvalue_false_node;
4161 }
4162
4163 /* If primop0 was sign-extended and unsigned comparison specd,
4164 we did a signed comparison above using the signed type bounds.
4165 But the comparison we output must be unsigned.
4166
4167 Also, for inequalities, VAL is no good; but if the signed
4168 comparison had *any* fixed result, it follows that the
4169 unsigned comparison just tests the sign in reverse
4170 (positive values are LE, negative ones GE).
4171 So we can generate an unsigned comparison
4172 against an extreme value of the signed type. */
4173
4174 if (unsignedp && !unsignedp0)
4175 {
4176 if (val != 0)
4177 switch (code)
4178 {
4179 case LT_EXPR:
4180 case GE_EXPR:
4181 primop1 = TYPE_MIN_VALUE (type);
4182 val = 0;
4183 break;
4184
4185 case LE_EXPR:
4186 case GT_EXPR:
4187 primop1 = TYPE_MAX_VALUE (type);
4188 val = 0;
4189 break;
4190
4191 default:
4192 break;
4193 }
4194 type = c_common_unsigned_type (type);
4195 }
4196
4197 if (TREE_CODE (primop0) != INTEGER_CST
4198 && c_inhibit_evaluation_warnings == 0)
4199 {
4200 if (val == truthvalue_false_node)
4201 warning_at (loc, OPT_Wtype_limits,
4202 "comparison is always false due to limited range of data type");
4203 if (val == truthvalue_true_node)
4204 warning_at (loc, OPT_Wtype_limits,
4205 "comparison is always true due to limited range of data type");
4206 }
4207
4208 if (val != 0)
4209 {
4210 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4211 if (TREE_SIDE_EFFECTS (primop0))
4212 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4213 return val;
4214 }
4215
4216 /* Value is not predetermined, but do the comparison
4217 in the type of the operand that is not constant.
4218 TYPE is already properly set. */
4219 }
4220
4221 /* If either arg is decimal float and the other is float, find the
4222 proper common type to use for comparison. */
4223 else if (real1 && real2
4224 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4225 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4226 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4227
4228 else if (real1 && real2
4229 && (TYPE_PRECISION (TREE_TYPE (primop0))
4230 == TYPE_PRECISION (TREE_TYPE (primop1))))
4231 type = TREE_TYPE (primop0);
4232
4233 /* If args' natural types are both narrower than nominal type
4234 and both extend in the same manner, compare them
4235 in the type of the wider arg.
4236 Otherwise must actually extend both to the nominal
4237 common type lest different ways of extending
4238 alter the result.
4239 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4240
4241 else if (unsignedp0 == unsignedp1 && real1 == real2
4242 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4243 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4244 {
4245 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4246 type = c_common_signed_or_unsigned_type (unsignedp0
4247 || TYPE_UNSIGNED (*restype_ptr),
4248 type);
4249 /* Make sure shorter operand is extended the right way
4250 to match the longer operand. */
4251 primop0
4252 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4253 TREE_TYPE (primop0)),
4254 primop0);
4255 primop1
4256 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4257 TREE_TYPE (primop1)),
4258 primop1);
4259 }
4260 else
4261 {
4262 /* Here we must do the comparison on the nominal type
4263 using the args exactly as we received them. */
4264 type = *restype_ptr;
4265 primop0 = op0;
4266 primop1 = op1;
4267
4268 if (!real1 && !real2 && integer_zerop (primop1)
4269 && TYPE_UNSIGNED (*restype_ptr))
4270 {
4271 tree value = 0;
4272 /* All unsigned values are >= 0, so we warn. However,
4273 if OP0 is a constant that is >= 0, the signedness of
4274 the comparison isn't an issue, so suppress the
4275 warning. */
4276 bool warn =
4277 warn_type_limits && !in_system_header
4278 && c_inhibit_evaluation_warnings == 0
4279 && !(TREE_CODE (primop0) == INTEGER_CST
4280 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4281 primop0)))
4282 /* Do not warn for enumeration types. */
4283 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4284
4285 switch (code)
4286 {
4287 case GE_EXPR:
4288 if (warn)
4289 warning_at (loc, OPT_Wtype_limits,
4290 "comparison of unsigned expression >= 0 is always true");
4291 value = truthvalue_true_node;
4292 break;
4293
4294 case LT_EXPR:
4295 if (warn)
4296 warning_at (loc, OPT_Wtype_limits,
4297 "comparison of unsigned expression < 0 is always false");
4298 value = truthvalue_false_node;
4299 break;
4300
4301 default:
4302 break;
4303 }
4304
4305 if (value != 0)
4306 {
4307 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4308 if (TREE_SIDE_EFFECTS (primop0))
4309 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4310 primop0, value);
4311 return value;
4312 }
4313 }
4314 }
4315
4316 *op0_ptr = convert (type, primop0);
4317 *op1_ptr = convert (type, primop1);
4318
4319 *restype_ptr = truthvalue_type_node;
4320
4321 return 0;
4322 }
4323 \f
4324 /* Return a tree for the sum or difference (RESULTCODE says which)
4325 of pointer PTROP and integer INTOP. */
4326
4327 tree
4328 pointer_int_sum (location_t loc, enum tree_code resultcode,
4329 tree ptrop, tree intop, bool complain)
4330 {
4331 tree size_exp, ret;
4332
4333 /* The result is a pointer of the same type that is being added. */
4334 tree result_type = TREE_TYPE (ptrop);
4335
4336 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4337 {
4338 if (complain && warn_pointer_arith)
4339 pedwarn (loc, OPT_Wpointer_arith,
4340 "pointer of type %<void *%> used in arithmetic");
4341 else if (!complain)
4342 return error_mark_node;
4343 size_exp = integer_one_node;
4344 }
4345 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4346 {
4347 if (complain && warn_pointer_arith)
4348 pedwarn (loc, OPT_Wpointer_arith,
4349 "pointer to a function used in arithmetic");
4350 else if (!complain)
4351 return error_mark_node;
4352 size_exp = integer_one_node;
4353 }
4354 else
4355 size_exp = size_in_bytes (TREE_TYPE (result_type));
4356
4357 /* We are manipulating pointer values, so we don't need to warn
4358 about relying on undefined signed overflow. We disable the
4359 warning here because we use integer types so fold won't know that
4360 they are really pointers. */
4361 fold_defer_overflow_warnings ();
4362
4363 /* If what we are about to multiply by the size of the elements
4364 contains a constant term, apply distributive law
4365 and multiply that constant term separately.
4366 This helps produce common subexpressions. */
4367 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4368 && !TREE_CONSTANT (intop)
4369 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4370 && TREE_CONSTANT (size_exp)
4371 /* If the constant comes from pointer subtraction,
4372 skip this optimization--it would cause an error. */
4373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4374 /* If the constant is unsigned, and smaller than the pointer size,
4375 then we must skip this optimization. This is because it could cause
4376 an overflow error if the constant is negative but INTOP is not. */
4377 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4378 || (TYPE_PRECISION (TREE_TYPE (intop))
4379 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4380 {
4381 enum tree_code subcode = resultcode;
4382 tree int_type = TREE_TYPE (intop);
4383 if (TREE_CODE (intop) == MINUS_EXPR)
4384 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4385 /* Convert both subexpression types to the type of intop,
4386 because weird cases involving pointer arithmetic
4387 can result in a sum or difference with different type args. */
4388 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4389 subcode, ptrop,
4390 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4391 intop = convert (int_type, TREE_OPERAND (intop, 0));
4392 }
4393
4394 /* Convert the integer argument to a type the same size as sizetype
4395 so the multiply won't overflow spuriously. */
4396 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4397 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4398 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4399 TYPE_UNSIGNED (sizetype)), intop);
4400
4401 /* Replace the integer argument with a suitable product by the object size.
4402 Do this multiplication as signed, then convert to the appropriate type
4403 for the pointer operation and disregard an overflow that occurred only
4404 because of the sign-extension change in the latter conversion. */
4405 {
4406 tree t = build_binary_op (loc,
4407 MULT_EXPR, intop,
4408 convert (TREE_TYPE (intop), size_exp), 1);
4409 intop = convert (sizetype, t);
4410 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4411 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4412 TREE_INT_CST_HIGH (intop));
4413 }
4414
4415 /* Create the sum or difference. */
4416 if (resultcode == MINUS_EXPR)
4417 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4418
4419 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4420
4421 fold_undefer_and_ignore_overflow_warnings ();
4422
4423 return ret;
4424 }
4425 \f
4426 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4427 and if NON_CONST is known not to be permitted in an evaluated part
4428 of a constant expression. */
4429
4430 tree
4431 c_wrap_maybe_const (tree expr, bool non_const)
4432 {
4433 bool nowarning = TREE_NO_WARNING (expr);
4434 location_t loc = EXPR_LOCATION (expr);
4435
4436 /* This should never be called for C++. */
4437 if (c_dialect_cxx ())
4438 gcc_unreachable ();
4439
4440 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4441 STRIP_TYPE_NOPS (expr);
4442 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4443 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4444 if (nowarning)
4445 TREE_NO_WARNING (expr) = 1;
4446 protected_set_expr_location (expr, loc);
4447
4448 return expr;
4449 }
4450
4451 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4452 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4453 around the SAVE_EXPR if needed so that c_fully_fold does not need
4454 to look inside SAVE_EXPRs. */
4455
4456 tree
4457 c_save_expr (tree expr)
4458 {
4459 bool maybe_const = true;
4460 if (c_dialect_cxx ())
4461 return save_expr (expr);
4462 expr = c_fully_fold (expr, false, &maybe_const);
4463 expr = save_expr (expr);
4464 if (!maybe_const)
4465 expr = c_wrap_maybe_const (expr, true);
4466 return expr;
4467 }
4468
4469 /* Return whether EXPR is a declaration whose address can never be
4470 NULL. */
4471
4472 bool
4473 decl_with_nonnull_addr_p (const_tree expr)
4474 {
4475 return (DECL_P (expr)
4476 && (TREE_CODE (expr) == PARM_DECL
4477 || TREE_CODE (expr) == LABEL_DECL
4478 || !DECL_WEAK (expr)));
4479 }
4480
4481 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4482 or for an `if' or `while' statement or ?..: exp. It should already
4483 have been validated to be of suitable type; otherwise, a bad
4484 diagnostic may result.
4485
4486 The EXPR is located at LOCATION.
4487
4488 This preparation consists of taking the ordinary
4489 representation of an expression expr and producing a valid tree
4490 boolean expression describing whether expr is nonzero. We could
4491 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4492 but we optimize comparisons, &&, ||, and !.
4493
4494 The resulting type should always be `truthvalue_type_node'. */
4495
4496 tree
4497 c_common_truthvalue_conversion (location_t location, tree expr)
4498 {
4499 switch (TREE_CODE (expr))
4500 {
4501 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4502 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4503 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4504 case ORDERED_EXPR: case UNORDERED_EXPR:
4505 if (TREE_TYPE (expr) == truthvalue_type_node)
4506 return expr;
4507 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4508 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4509 goto ret;
4510
4511 case TRUTH_ANDIF_EXPR:
4512 case TRUTH_ORIF_EXPR:
4513 case TRUTH_AND_EXPR:
4514 case TRUTH_OR_EXPR:
4515 case TRUTH_XOR_EXPR:
4516 if (TREE_TYPE (expr) == truthvalue_type_node)
4517 return expr;
4518 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4519 c_common_truthvalue_conversion (location,
4520 TREE_OPERAND (expr, 0)),
4521 c_common_truthvalue_conversion (location,
4522 TREE_OPERAND (expr, 1)));
4523 goto ret;
4524
4525 case TRUTH_NOT_EXPR:
4526 if (TREE_TYPE (expr) == truthvalue_type_node)
4527 return expr;
4528 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4529 c_common_truthvalue_conversion (location,
4530 TREE_OPERAND (expr, 0)));
4531 goto ret;
4532
4533 case ERROR_MARK:
4534 return expr;
4535
4536 case INTEGER_CST:
4537 return integer_zerop (expr) ? truthvalue_false_node
4538 : truthvalue_true_node;
4539
4540 case REAL_CST:
4541 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4542 ? truthvalue_true_node
4543 : truthvalue_false_node;
4544
4545 case FIXED_CST:
4546 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4547 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4548 ? truthvalue_true_node
4549 : truthvalue_false_node;
4550
4551 case FUNCTION_DECL:
4552 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4553 /* Fall through. */
4554
4555 case ADDR_EXPR:
4556 {
4557 tree inner = TREE_OPERAND (expr, 0);
4558 if (decl_with_nonnull_addr_p (inner))
4559 {
4560 /* Common Ada/Pascal programmer's mistake. */
4561 warning_at (location,
4562 OPT_Waddress,
4563 "the address of %qD will always evaluate as %<true%>",
4564 inner);
4565 return truthvalue_true_node;
4566 }
4567 break;
4568 }
4569
4570 case COMPLEX_EXPR:
4571 expr = build_binary_op (EXPR_LOCATION (expr),
4572 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4573 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4574 c_common_truthvalue_conversion (location,
4575 TREE_OPERAND (expr, 0)),
4576 c_common_truthvalue_conversion (location,
4577 TREE_OPERAND (expr, 1)),
4578 0);
4579 goto ret;
4580
4581 case NEGATE_EXPR:
4582 case ABS_EXPR:
4583 case FLOAT_EXPR:
4584 case EXCESS_PRECISION_EXPR:
4585 /* These don't change whether an object is nonzero or zero. */
4586 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4587
4588 case LROTATE_EXPR:
4589 case RROTATE_EXPR:
4590 /* These don't change whether an object is zero or nonzero, but
4591 we can't ignore them if their second arg has side-effects. */
4592 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4593 {
4594 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4595 TREE_OPERAND (expr, 1),
4596 c_common_truthvalue_conversion
4597 (location, TREE_OPERAND (expr, 0)));
4598 goto ret;
4599 }
4600 else
4601 return c_common_truthvalue_conversion (location,
4602 TREE_OPERAND (expr, 0));
4603
4604 case COND_EXPR:
4605 /* Distribute the conversion into the arms of a COND_EXPR. */
4606 if (c_dialect_cxx ())
4607 {
4608 tree op1 = TREE_OPERAND (expr, 1);
4609 tree op2 = TREE_OPERAND (expr, 2);
4610 /* In C++ one of the arms might have void type if it is throw. */
4611 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4612 op1 = c_common_truthvalue_conversion (location, op1);
4613 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4614 op2 = c_common_truthvalue_conversion (location, op2);
4615 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4616 TREE_OPERAND (expr, 0), op1, op2);
4617 goto ret;
4618 }
4619 else
4620 {
4621 /* Folding will happen later for C. */
4622 expr = build3 (COND_EXPR, truthvalue_type_node,
4623 TREE_OPERAND (expr, 0),
4624 c_common_truthvalue_conversion (location,
4625 TREE_OPERAND (expr, 1)),
4626 c_common_truthvalue_conversion (location,
4627 TREE_OPERAND (expr, 2)));
4628 goto ret;
4629 }
4630
4631 CASE_CONVERT:
4632 {
4633 tree totype = TREE_TYPE (expr);
4634 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4635
4636 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4637 since that affects how `default_conversion' will behave. */
4638 if (TREE_CODE (totype) == REFERENCE_TYPE
4639 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4640 break;
4641 /* Don't strip a conversion from C++0x scoped enum, since they
4642 don't implicitly convert to other types. */
4643 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4644 && ENUM_IS_SCOPED (fromtype))
4645 break;
4646 /* If this isn't narrowing the argument, we can ignore it. */
4647 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4648 return c_common_truthvalue_conversion (location,
4649 TREE_OPERAND (expr, 0));
4650 }
4651 break;
4652
4653 case MODIFY_EXPR:
4654 if (!TREE_NO_WARNING (expr)
4655 && warn_parentheses)
4656 {
4657 warning (OPT_Wparentheses,
4658 "suggest parentheses around assignment used as truth value");
4659 TREE_NO_WARNING (expr) = 1;
4660 }
4661 break;
4662
4663 default:
4664 break;
4665 }
4666
4667 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4668 {
4669 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4670 expr = (build_binary_op
4671 (EXPR_LOCATION (expr),
4672 (TREE_SIDE_EFFECTS (expr)
4673 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4674 c_common_truthvalue_conversion
4675 (location,
4676 build_unary_op (location, REALPART_EXPR, t, 0)),
4677 c_common_truthvalue_conversion
4678 (location,
4679 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4680 0));
4681 goto ret;
4682 }
4683
4684 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4685 {
4686 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4687 FCONST0 (TYPE_MODE
4688 (TREE_TYPE (expr))));
4689 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4690 }
4691 else
4692 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4693
4694 ret:
4695 protected_set_expr_location (expr, location);
4696 return expr;
4697 }
4698 \f
4699 static void def_builtin_1 (enum built_in_function fncode,
4700 const char *name,
4701 enum built_in_class fnclass,
4702 tree fntype, tree libtype,
4703 bool both_p, bool fallback_p, bool nonansi_p,
4704 tree fnattrs, bool implicit_p);
4705
4706
4707 /* Apply the TYPE_QUALS to the new DECL. */
4708
4709 void
4710 c_apply_type_quals_to_decl (int type_quals, tree decl)
4711 {
4712 tree type = TREE_TYPE (decl);
4713
4714 if (type == error_mark_node)
4715 return;
4716
4717 if ((type_quals & TYPE_QUAL_CONST)
4718 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4719 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4720 constructor can produce constant init, so rely on cp_finish_decl to
4721 clear TREE_READONLY if the variable has non-constant init. */
4722 TREE_READONLY (decl) = 1;
4723 if (type_quals & TYPE_QUAL_VOLATILE)
4724 {
4725 TREE_SIDE_EFFECTS (decl) = 1;
4726 TREE_THIS_VOLATILE (decl) = 1;
4727 }
4728 if (type_quals & TYPE_QUAL_RESTRICT)
4729 {
4730 while (type && TREE_CODE (type) == ARRAY_TYPE)
4731 /* Allow 'restrict' on arrays of pointers.
4732 FIXME currently we just ignore it. */
4733 type = TREE_TYPE (type);
4734 if (!type
4735 || !POINTER_TYPE_P (type)
4736 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4737 error ("invalid use of %<restrict%>");
4738 }
4739 }
4740
4741 /* Hash function for the problem of multiple type definitions in
4742 different files. This must hash all types that will compare
4743 equal via comptypes to the same value. In practice it hashes
4744 on some of the simple stuff and leaves the details to comptypes. */
4745
4746 static hashval_t
4747 c_type_hash (const void *p)
4748 {
4749 int n_elements;
4750 int shift, size;
4751 const_tree const t = (const_tree) p;
4752 tree t2;
4753 switch (TREE_CODE (t))
4754 {
4755 /* For pointers, hash on pointee type plus some swizzling. */
4756 case POINTER_TYPE:
4757 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4758 /* Hash on number of elements and total size. */
4759 case ENUMERAL_TYPE:
4760 shift = 3;
4761 t2 = TYPE_VALUES (t);
4762 break;
4763 case RECORD_TYPE:
4764 shift = 0;
4765 t2 = TYPE_FIELDS (t);
4766 break;
4767 case QUAL_UNION_TYPE:
4768 shift = 1;
4769 t2 = TYPE_FIELDS (t);
4770 break;
4771 case UNION_TYPE:
4772 shift = 2;
4773 t2 = TYPE_FIELDS (t);
4774 break;
4775 default:
4776 gcc_unreachable ();
4777 }
4778 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4779 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4780 n_elements = list_length (t2);
4781 /* We might have a VLA here. */
4782 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4783 size = 0;
4784 else
4785 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4786 return ((size << 24) | (n_elements << shift));
4787 }
4788
4789 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4790
4791 /* Return the typed-based alias set for T, which may be an expression
4792 or a type. Return -1 if we don't do anything special. */
4793
4794 alias_set_type
4795 c_common_get_alias_set (tree t)
4796 {
4797 tree u;
4798 PTR *slot;
4799
4800 /* For VLAs, use the alias set of the element type rather than the
4801 default of alias set 0 for types compared structurally. */
4802 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4803 {
4804 if (TREE_CODE (t) == ARRAY_TYPE)
4805 return get_alias_set (TREE_TYPE (t));
4806 return -1;
4807 }
4808
4809 /* Permit type-punning when accessing a union, provided the access
4810 is directly through the union. For example, this code does not
4811 permit taking the address of a union member and then storing
4812 through it. Even the type-punning allowed here is a GCC
4813 extension, albeit a common and useful one; the C standard says
4814 that such accesses have implementation-defined behavior. */
4815 for (u = t;
4816 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4817 u = TREE_OPERAND (u, 0))
4818 if (TREE_CODE (u) == COMPONENT_REF
4819 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4820 return 0;
4821
4822 /* That's all the expressions we handle specially. */
4823 if (!TYPE_P (t))
4824 return -1;
4825
4826 /* The C standard guarantees that any object may be accessed via an
4827 lvalue that has character type. */
4828 if (t == char_type_node
4829 || t == signed_char_type_node
4830 || t == unsigned_char_type_node)
4831 return 0;
4832
4833 /* The C standard specifically allows aliasing between signed and
4834 unsigned variants of the same type. We treat the signed
4835 variant as canonical. */
4836 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4837 {
4838 tree t1 = c_common_signed_type (t);
4839
4840 /* t1 == t can happen for boolean nodes which are always unsigned. */
4841 if (t1 != t)
4842 return get_alias_set (t1);
4843 }
4844
4845 /* Handle the case of multiple type nodes referring to "the same" type,
4846 which occurs with IMA. These share an alias set. FIXME: Currently only
4847 C90 is handled. (In C99 type compatibility is not transitive, which
4848 complicates things mightily. The alias set splay trees can theoretically
4849 represent this, but insertion is tricky when you consider all the
4850 different orders things might arrive in.) */
4851
4852 if (c_language != clk_c || flag_isoc99)
4853 return -1;
4854
4855 /* Save time if there's only one input file. */
4856 if (num_in_fnames == 1)
4857 return -1;
4858
4859 /* Pointers need special handling if they point to any type that
4860 needs special handling (below). */
4861 if (TREE_CODE (t) == POINTER_TYPE)
4862 {
4863 tree t2;
4864 /* Find bottom type under any nested POINTERs. */
4865 for (t2 = TREE_TYPE (t);
4866 TREE_CODE (t2) == POINTER_TYPE;
4867 t2 = TREE_TYPE (t2))
4868 ;
4869 if (TREE_CODE (t2) != RECORD_TYPE
4870 && TREE_CODE (t2) != ENUMERAL_TYPE
4871 && TREE_CODE (t2) != QUAL_UNION_TYPE
4872 && TREE_CODE (t2) != UNION_TYPE)
4873 return -1;
4874 if (TYPE_SIZE (t2) == 0)
4875 return -1;
4876 }
4877 /* These are the only cases that need special handling. */
4878 if (TREE_CODE (t) != RECORD_TYPE
4879 && TREE_CODE (t) != ENUMERAL_TYPE
4880 && TREE_CODE (t) != QUAL_UNION_TYPE
4881 && TREE_CODE (t) != UNION_TYPE
4882 && TREE_CODE (t) != POINTER_TYPE)
4883 return -1;
4884 /* Undefined? */
4885 if (TYPE_SIZE (t) == 0)
4886 return -1;
4887
4888 /* Look up t in hash table. Only one of the compatible types within each
4889 alias set is recorded in the table. */
4890 if (!type_hash_table)
4891 type_hash_table = htab_create_ggc (1021, c_type_hash,
4892 (htab_eq) lang_hooks.types_compatible_p,
4893 NULL);
4894 slot = htab_find_slot (type_hash_table, t, INSERT);
4895 if (*slot != NULL)
4896 {
4897 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4898 return TYPE_ALIAS_SET ((tree)*slot);
4899 }
4900 else
4901 /* Our caller will assign and record (in t) a new alias set; all we need
4902 to do is remember t in the hash table. */
4903 *slot = t;
4904
4905 return -1;
4906 }
4907 \f
4908 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4909 the second parameter indicates which OPERATOR is being applied.
4910 The COMPLAIN flag controls whether we should diagnose possibly
4911 ill-formed constructs or not. LOC is the location of the SIZEOF or
4912 TYPEOF operator. */
4913
4914 tree
4915 c_sizeof_or_alignof_type (location_t loc,
4916 tree type, bool is_sizeof, int complain)
4917 {
4918 const char *op_name;
4919 tree value = NULL;
4920 enum tree_code type_code = TREE_CODE (type);
4921
4922 op_name = is_sizeof ? "sizeof" : "__alignof__";
4923
4924 if (type_code == FUNCTION_TYPE)
4925 {
4926 if (is_sizeof)
4927 {
4928 if (complain && warn_pointer_arith)
4929 pedwarn (loc, OPT_Wpointer_arith,
4930 "invalid application of %<sizeof%> to a function type");
4931 else if (!complain)
4932 return error_mark_node;
4933 value = size_one_node;
4934 }
4935 else
4936 {
4937 if (complain)
4938 {
4939 if (c_dialect_cxx ())
4940 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4941 "%<alignof%> applied to a function type");
4942 else
4943 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4944 "%<_Alignof%> applied to a function type");
4945 }
4946 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4947 }
4948 }
4949 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4950 {
4951 if (type_code == VOID_TYPE
4952 && complain && warn_pointer_arith)
4953 pedwarn (loc, OPT_Wpointer_arith,
4954 "invalid application of %qs to a void type", op_name);
4955 else if (!complain)
4956 return error_mark_node;
4957 value = size_one_node;
4958 }
4959 else if (!COMPLETE_TYPE_P (type)
4960 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4961 {
4962 if (complain)
4963 error_at (loc, "invalid application of %qs to incomplete type %qT",
4964 op_name, type);
4965 return error_mark_node;
4966 }
4967 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4968 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4969 {
4970 if (complain)
4971 error_at (loc, "invalid application of %qs to array type %qT of "
4972 "incomplete element type", op_name, type);
4973 return error_mark_node;
4974 }
4975 else
4976 {
4977 if (is_sizeof)
4978 /* Convert in case a char is more than one unit. */
4979 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4980 size_int (TYPE_PRECISION (char_type_node)
4981 / BITS_PER_UNIT));
4982 else
4983 value = size_int (TYPE_ALIGN_UNIT (type));
4984 }
4985
4986 /* VALUE will have the middle-end integer type sizetype.
4987 However, we should really return a value of type `size_t',
4988 which is just a typedef for an ordinary integer type. */
4989 value = fold_convert_loc (loc, size_type_node, value);
4990
4991 return value;
4992 }
4993
4994 /* Implement the __alignof keyword: Return the minimum required
4995 alignment of EXPR, measured in bytes. For VAR_DECLs,
4996 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4997 from an "aligned" __attribute__ specification). LOC is the
4998 location of the ALIGNOF operator. */
4999
5000 tree
5001 c_alignof_expr (location_t loc, tree expr)
5002 {
5003 tree t;
5004
5005 if (VAR_OR_FUNCTION_DECL_P (expr))
5006 t = size_int (DECL_ALIGN_UNIT (expr));
5007
5008 else if (TREE_CODE (expr) == COMPONENT_REF
5009 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5010 {
5011 error_at (loc, "%<__alignof%> applied to a bit-field");
5012 t = size_one_node;
5013 }
5014 else if (TREE_CODE (expr) == COMPONENT_REF
5015 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5016 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5017
5018 else if (TREE_CODE (expr) == INDIRECT_REF)
5019 {
5020 tree t = TREE_OPERAND (expr, 0);
5021 tree best = t;
5022 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5023
5024 while (CONVERT_EXPR_P (t)
5025 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5026 {
5027 int thisalign;
5028
5029 t = TREE_OPERAND (t, 0);
5030 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5031 if (thisalign > bestalign)
5032 best = t, bestalign = thisalign;
5033 }
5034 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5035 }
5036 else
5037 return c_alignof (loc, TREE_TYPE (expr));
5038
5039 return fold_convert_loc (loc, size_type_node, t);
5040 }
5041 \f
5042 /* Handle C and C++ default attributes. */
5043
5044 enum built_in_attribute
5045 {
5046 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5047 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5048 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5049 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5050 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5051 #include "builtin-attrs.def"
5052 #undef DEF_ATTR_NULL_TREE
5053 #undef DEF_ATTR_INT
5054 #undef DEF_ATTR_STRING
5055 #undef DEF_ATTR_IDENT
5056 #undef DEF_ATTR_TREE_LIST
5057 ATTR_LAST
5058 };
5059
5060 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5061
5062 static void c_init_attributes (void);
5063
5064 enum c_builtin_type
5065 {
5066 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5067 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5068 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5069 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5070 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5071 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5072 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5073 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5074 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5075 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5076 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5077 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5078 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5082 NAME,
5083 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5084 #include "builtin-types.def"
5085 #undef DEF_PRIMITIVE_TYPE
5086 #undef DEF_FUNCTION_TYPE_0
5087 #undef DEF_FUNCTION_TYPE_1
5088 #undef DEF_FUNCTION_TYPE_2
5089 #undef DEF_FUNCTION_TYPE_3
5090 #undef DEF_FUNCTION_TYPE_4
5091 #undef DEF_FUNCTION_TYPE_5
5092 #undef DEF_FUNCTION_TYPE_6
5093 #undef DEF_FUNCTION_TYPE_7
5094 #undef DEF_FUNCTION_TYPE_8
5095 #undef DEF_FUNCTION_TYPE_VAR_0
5096 #undef DEF_FUNCTION_TYPE_VAR_1
5097 #undef DEF_FUNCTION_TYPE_VAR_2
5098 #undef DEF_FUNCTION_TYPE_VAR_3
5099 #undef DEF_FUNCTION_TYPE_VAR_4
5100 #undef DEF_FUNCTION_TYPE_VAR_5
5101 #undef DEF_POINTER_TYPE
5102 BT_LAST
5103 };
5104
5105 typedef enum c_builtin_type builtin_type;
5106
5107 /* A temporary array for c_common_nodes_and_builtins. Used in
5108 communication with def_fn_type. */
5109 static tree builtin_types[(int) BT_LAST + 1];
5110
5111 /* A helper function for c_common_nodes_and_builtins. Build function type
5112 for DEF with return type RET and N arguments. If VAR is true, then the
5113 function should be variadic after those N arguments.
5114
5115 Takes special care not to ICE if any of the types involved are
5116 error_mark_node, which indicates that said type is not in fact available
5117 (see builtin_type_for_size). In which case the function type as a whole
5118 should be error_mark_node. */
5119
5120 static void
5121 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5122 {
5123 tree t;
5124 tree *args = XALLOCAVEC (tree, n);
5125 va_list list;
5126 int i;
5127
5128 va_start (list, n);
5129 for (i = 0; i < n; ++i)
5130 {
5131 builtin_type a = (builtin_type) va_arg (list, int);
5132 t = builtin_types[a];
5133 if (t == error_mark_node)
5134 goto egress;
5135 args[i] = t;
5136 }
5137
5138 t = builtin_types[ret];
5139 if (t == error_mark_node)
5140 goto egress;
5141 if (var)
5142 t = build_varargs_function_type_array (t, n, args);
5143 else
5144 t = build_function_type_array (t, n, args);
5145
5146 egress:
5147 builtin_types[def] = t;
5148 va_end (list);
5149 }
5150
5151 /* Build builtin functions common to both C and C++ language
5152 frontends. */
5153
5154 static void
5155 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5156 {
5157 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5158 builtin_types[ENUM] = VALUE;
5159 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5160 def_fn_type (ENUM, RETURN, 0, 0);
5161 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5162 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5163 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5164 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5165 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5166 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5167 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5168 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5169 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5170 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5171 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5172 ARG6) \
5173 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5174 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5175 ARG6, ARG7) \
5176 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5177 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5178 ARG6, ARG7, ARG8) \
5179 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5180 ARG7, ARG8);
5181 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5182 def_fn_type (ENUM, RETURN, 1, 0);
5183 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5184 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5185 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5186 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5187 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5188 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5189 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5190 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5191 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5192 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5193 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5194 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5195
5196 #include "builtin-types.def"
5197
5198 #undef DEF_PRIMITIVE_TYPE
5199 #undef DEF_FUNCTION_TYPE_1
5200 #undef DEF_FUNCTION_TYPE_2
5201 #undef DEF_FUNCTION_TYPE_3
5202 #undef DEF_FUNCTION_TYPE_4
5203 #undef DEF_FUNCTION_TYPE_5
5204 #undef DEF_FUNCTION_TYPE_6
5205 #undef DEF_FUNCTION_TYPE_VAR_0
5206 #undef DEF_FUNCTION_TYPE_VAR_1
5207 #undef DEF_FUNCTION_TYPE_VAR_2
5208 #undef DEF_FUNCTION_TYPE_VAR_3
5209 #undef DEF_FUNCTION_TYPE_VAR_4
5210 #undef DEF_FUNCTION_TYPE_VAR_5
5211 #undef DEF_POINTER_TYPE
5212 builtin_types[(int) BT_LAST] = NULL_TREE;
5213
5214 c_init_attributes ();
5215
5216 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5217 NONANSI_P, ATTRS, IMPLICIT, COND) \
5218 if (NAME && COND) \
5219 def_builtin_1 (ENUM, NAME, CLASS, \
5220 builtin_types[(int) TYPE], \
5221 builtin_types[(int) LIBTYPE], \
5222 BOTH_P, FALLBACK_P, NONANSI_P, \
5223 built_in_attributes[(int) ATTRS], IMPLICIT);
5224 #include "builtins.def"
5225 #undef DEF_BUILTIN
5226
5227 targetm.init_builtins ();
5228
5229 build_common_builtin_nodes ();
5230
5231 if (flag_enable_cilkplus)
5232 cilk_init_builtins ();
5233 }
5234
5235 /* Like get_identifier, but avoid warnings about null arguments when
5236 the argument may be NULL for targets where GCC lacks stdint.h type
5237 information. */
5238
5239 static inline tree
5240 c_get_ident (const char *id)
5241 {
5242 return get_identifier (id);
5243 }
5244
5245 /* Build tree nodes and builtin functions common to both C and C++ language
5246 frontends. */
5247
5248 void
5249 c_common_nodes_and_builtins (void)
5250 {
5251 int char16_type_size;
5252 int char32_type_size;
5253 int wchar_type_size;
5254 tree array_domain_type;
5255 tree va_list_ref_type_node;
5256 tree va_list_arg_type_node;
5257
5258 build_common_tree_nodes (flag_signed_char, flag_short_double);
5259
5260 /* Define `int' and `char' first so that dbx will output them first. */
5261 record_builtin_type (RID_INT, NULL, integer_type_node);
5262 record_builtin_type (RID_CHAR, "char", char_type_node);
5263
5264 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5265 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5266 but not C. Are the conditionals here needed? */
5267 if (c_dialect_cxx ())
5268 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5269 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5270 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5271 record_builtin_type (RID_MAX, "long unsigned int",
5272 long_unsigned_type_node);
5273 if (int128_integer_type_node != NULL_TREE)
5274 {
5275 record_builtin_type (RID_INT128, "__int128",
5276 int128_integer_type_node);
5277 record_builtin_type (RID_MAX, "__int128 unsigned",
5278 int128_unsigned_type_node);
5279 }
5280 if (c_dialect_cxx ())
5281 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5282 record_builtin_type (RID_MAX, "long long int",
5283 long_long_integer_type_node);
5284 record_builtin_type (RID_MAX, "long long unsigned int",
5285 long_long_unsigned_type_node);
5286 if (c_dialect_cxx ())
5287 record_builtin_type (RID_MAX, "long long unsigned",
5288 long_long_unsigned_type_node);
5289 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5290 record_builtin_type (RID_MAX, "short unsigned int",
5291 short_unsigned_type_node);
5292 if (c_dialect_cxx ())
5293 record_builtin_type (RID_MAX, "unsigned short",
5294 short_unsigned_type_node);
5295
5296 /* Define both `signed char' and `unsigned char'. */
5297 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5298 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5299
5300 /* These are types that c_common_type_for_size and
5301 c_common_type_for_mode use. */
5302 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5303 TYPE_DECL, NULL_TREE,
5304 intQI_type_node));
5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL, NULL_TREE,
5307 intHI_type_node));
5308 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5309 TYPE_DECL, NULL_TREE,
5310 intSI_type_node));
5311 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5312 TYPE_DECL, NULL_TREE,
5313 intDI_type_node));
5314 #if HOST_BITS_PER_WIDE_INT >= 64
5315 if (targetm.scalar_mode_supported_p (TImode))
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL,
5318 get_identifier ("__int128_t"),
5319 intTI_type_node));
5320 #endif
5321 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5322 TYPE_DECL, NULL_TREE,
5323 unsigned_intQI_type_node));
5324 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5325 TYPE_DECL, NULL_TREE,
5326 unsigned_intHI_type_node));
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL, NULL_TREE,
5329 unsigned_intSI_type_node));
5330 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5331 TYPE_DECL, NULL_TREE,
5332 unsigned_intDI_type_node));
5333 #if HOST_BITS_PER_WIDE_INT >= 64
5334 if (targetm.scalar_mode_supported_p (TImode))
5335 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5336 TYPE_DECL,
5337 get_identifier ("__uint128_t"),
5338 unsigned_intTI_type_node));
5339 #endif
5340
5341 /* Create the widest literal types. */
5342 widest_integer_literal_type_node
5343 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5344 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5345 TYPE_DECL, NULL_TREE,
5346 widest_integer_literal_type_node));
5347
5348 widest_unsigned_literal_type_node
5349 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5350 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5351 TYPE_DECL, NULL_TREE,
5352 widest_unsigned_literal_type_node));
5353
5354 signed_size_type_node = c_common_signed_type (size_type_node);
5355
5356 pid_type_node =
5357 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5358
5359 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5360 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5361 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5362
5363 /* Only supported decimal floating point extension if the target
5364 actually supports underlying modes. */
5365 if (targetm.scalar_mode_supported_p (SDmode)
5366 && targetm.scalar_mode_supported_p (DDmode)
5367 && targetm.scalar_mode_supported_p (TDmode))
5368 {
5369 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5370 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5371 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5372 }
5373
5374 if (targetm.fixed_point_supported_p ())
5375 {
5376 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5377 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5378 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5379 record_builtin_type (RID_MAX, "long long _Fract",
5380 long_long_fract_type_node);
5381 record_builtin_type (RID_MAX, "unsigned short _Fract",
5382 unsigned_short_fract_type_node);
5383 record_builtin_type (RID_MAX, "unsigned _Fract",
5384 unsigned_fract_type_node);
5385 record_builtin_type (RID_MAX, "unsigned long _Fract",
5386 unsigned_long_fract_type_node);
5387 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5388 unsigned_long_long_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat short _Fract",
5390 sat_short_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5392 record_builtin_type (RID_MAX, "_Sat long _Fract",
5393 sat_long_fract_type_node);
5394 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5395 sat_long_long_fract_type_node);
5396 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5397 sat_unsigned_short_fract_type_node);
5398 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5399 sat_unsigned_fract_type_node);
5400 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5401 sat_unsigned_long_fract_type_node);
5402 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5403 sat_unsigned_long_long_fract_type_node);
5404 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5405 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5406 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5407 record_builtin_type (RID_MAX, "long long _Accum",
5408 long_long_accum_type_node);
5409 record_builtin_type (RID_MAX, "unsigned short _Accum",
5410 unsigned_short_accum_type_node);
5411 record_builtin_type (RID_MAX, "unsigned _Accum",
5412 unsigned_accum_type_node);
5413 record_builtin_type (RID_MAX, "unsigned long _Accum",
5414 unsigned_long_accum_type_node);
5415 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5416 unsigned_long_long_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat short _Accum",
5418 sat_short_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5420 record_builtin_type (RID_MAX, "_Sat long _Accum",
5421 sat_long_accum_type_node);
5422 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5423 sat_long_long_accum_type_node);
5424 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5425 sat_unsigned_short_accum_type_node);
5426 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5427 sat_unsigned_accum_type_node);
5428 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5429 sat_unsigned_long_accum_type_node);
5430 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5431 sat_unsigned_long_long_accum_type_node);
5432
5433 }
5434
5435 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5436 TYPE_DECL,
5437 get_identifier ("complex int"),
5438 complex_integer_type_node));
5439 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5440 TYPE_DECL,
5441 get_identifier ("complex float"),
5442 complex_float_type_node));
5443 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5444 TYPE_DECL,
5445 get_identifier ("complex double"),
5446 complex_double_type_node));
5447 lang_hooks.decls.pushdecl
5448 (build_decl (UNKNOWN_LOCATION,
5449 TYPE_DECL, get_identifier ("complex long double"),
5450 complex_long_double_type_node));
5451
5452 if (c_dialect_cxx ())
5453 /* For C++, make fileptr_type_node a distinct void * type until
5454 FILE type is defined. */
5455 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5456
5457 record_builtin_type (RID_VOID, NULL, void_type_node);
5458
5459 /* Set the TYPE_NAME for any variants that were built before
5460 record_builtin_type gave names to the built-in types. */
5461 {
5462 tree void_name = TYPE_NAME (void_type_node);
5463 TYPE_NAME (void_type_node) = NULL_TREE;
5464 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5465 = void_name;
5466 TYPE_NAME (void_type_node) = void_name;
5467 }
5468
5469 /* This node must not be shared. */
5470 void_zero_node = make_node (INTEGER_CST);
5471 TREE_TYPE (void_zero_node) = void_type_node;
5472
5473 void_list_node = build_void_list_node ();
5474
5475 /* Make a type to be the domain of a few array types
5476 whose domains don't really matter.
5477 200 is small enough that it always fits in size_t
5478 and large enough that it can hold most function names for the
5479 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5480 array_domain_type = build_index_type (size_int (200));
5481
5482 /* Make a type for arrays of characters.
5483 With luck nothing will ever really depend on the length of this
5484 array type. */
5485 char_array_type_node
5486 = build_array_type (char_type_node, array_domain_type);
5487
5488 /* Likewise for arrays of ints. */
5489 int_array_type_node
5490 = build_array_type (integer_type_node, array_domain_type);
5491
5492 string_type_node = build_pointer_type (char_type_node);
5493 const_string_type_node
5494 = build_pointer_type (build_qualified_type
5495 (char_type_node, TYPE_QUAL_CONST));
5496
5497 /* This is special for C++ so functions can be overloaded. */
5498 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5499 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5500 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5501 underlying_wchar_type_node = wchar_type_node;
5502 if (c_dialect_cxx ())
5503 {
5504 if (TYPE_UNSIGNED (wchar_type_node))
5505 wchar_type_node = make_unsigned_type (wchar_type_size);
5506 else
5507 wchar_type_node = make_signed_type (wchar_type_size);
5508 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5509 }
5510
5511 /* This is for wide string constants. */
5512 wchar_array_type_node
5513 = build_array_type (wchar_type_node, array_domain_type);
5514
5515 /* Define 'char16_t'. */
5516 char16_type_node = get_identifier (CHAR16_TYPE);
5517 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5518 char16_type_size = TYPE_PRECISION (char16_type_node);
5519 if (c_dialect_cxx ())
5520 {
5521 char16_type_node = make_unsigned_type (char16_type_size);
5522
5523 if (cxx_dialect >= cxx11)
5524 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5525 }
5526
5527 /* This is for UTF-16 string constants. */
5528 char16_array_type_node
5529 = build_array_type (char16_type_node, array_domain_type);
5530
5531 /* Define 'char32_t'. */
5532 char32_type_node = get_identifier (CHAR32_TYPE);
5533 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5534 char32_type_size = TYPE_PRECISION (char32_type_node);
5535 if (c_dialect_cxx ())
5536 {
5537 char32_type_node = make_unsigned_type (char32_type_size);
5538
5539 if (cxx_dialect >= cxx11)
5540 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5541 }
5542
5543 /* This is for UTF-32 string constants. */
5544 char32_array_type_node
5545 = build_array_type (char32_type_node, array_domain_type);
5546
5547 wint_type_node =
5548 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5549
5550 intmax_type_node =
5551 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5552 uintmax_type_node =
5553 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5554
5555 if (SIG_ATOMIC_TYPE)
5556 sig_atomic_type_node =
5557 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5558 if (INT8_TYPE)
5559 int8_type_node =
5560 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5561 if (INT16_TYPE)
5562 int16_type_node =
5563 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5564 if (INT32_TYPE)
5565 int32_type_node =
5566 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5567 if (INT64_TYPE)
5568 int64_type_node =
5569 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5570 if (UINT8_TYPE)
5571 uint8_type_node =
5572 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5573 if (UINT16_TYPE)
5574 c_uint16_type_node =
5575 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5576 if (UINT32_TYPE)
5577 c_uint32_type_node =
5578 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5579 if (UINT64_TYPE)
5580 c_uint64_type_node =
5581 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5582 if (INT_LEAST8_TYPE)
5583 int_least8_type_node =
5584 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5585 if (INT_LEAST16_TYPE)
5586 int_least16_type_node =
5587 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5588 if (INT_LEAST32_TYPE)
5589 int_least32_type_node =
5590 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5591 if (INT_LEAST64_TYPE)
5592 int_least64_type_node =
5593 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5594 if (UINT_LEAST8_TYPE)
5595 uint_least8_type_node =
5596 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5597 if (UINT_LEAST16_TYPE)
5598 uint_least16_type_node =
5599 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5600 if (UINT_LEAST32_TYPE)
5601 uint_least32_type_node =
5602 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5603 if (UINT_LEAST64_TYPE)
5604 uint_least64_type_node =
5605 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5606 if (INT_FAST8_TYPE)
5607 int_fast8_type_node =
5608 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5609 if (INT_FAST16_TYPE)
5610 int_fast16_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5612 if (INT_FAST32_TYPE)
5613 int_fast32_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5615 if (INT_FAST64_TYPE)
5616 int_fast64_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5618 if (UINT_FAST8_TYPE)
5619 uint_fast8_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5621 if (UINT_FAST16_TYPE)
5622 uint_fast16_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5624 if (UINT_FAST32_TYPE)
5625 uint_fast32_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5627 if (UINT_FAST64_TYPE)
5628 uint_fast64_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5630 if (INTPTR_TYPE)
5631 intptr_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5633 if (UINTPTR_TYPE)
5634 uintptr_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5636
5637 default_function_type
5638 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5639 ptrdiff_type_node
5640 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5641 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5642
5643 lang_hooks.decls.pushdecl
5644 (build_decl (UNKNOWN_LOCATION,
5645 TYPE_DECL, get_identifier ("__builtin_va_list"),
5646 va_list_type_node));
5647 if (targetm.enum_va_list_p)
5648 {
5649 int l;
5650 const char *pname;
5651 tree ptype;
5652
5653 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5654 {
5655 lang_hooks.decls.pushdecl
5656 (build_decl (UNKNOWN_LOCATION,
5657 TYPE_DECL, get_identifier (pname),
5658 ptype));
5659
5660 }
5661 }
5662
5663 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5664 {
5665 va_list_arg_type_node = va_list_ref_type_node =
5666 build_pointer_type (TREE_TYPE (va_list_type_node));
5667 }
5668 else
5669 {
5670 va_list_arg_type_node = va_list_type_node;
5671 va_list_ref_type_node = build_reference_type (va_list_type_node);
5672 }
5673
5674 if (!flag_preprocess_only)
5675 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5676
5677 main_identifier_node = get_identifier ("main");
5678
5679 /* Create the built-in __null node. It is important that this is
5680 not shared. */
5681 null_node = make_node (INTEGER_CST);
5682 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5683
5684 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5685 memset (builtin_types, 0, sizeof (builtin_types));
5686 }
5687
5688 /* The number of named compound-literals generated thus far. */
5689 static GTY(()) int compound_literal_number;
5690
5691 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5692
5693 void
5694 set_compound_literal_name (tree decl)
5695 {
5696 char *name;
5697 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5698 compound_literal_number);
5699 compound_literal_number++;
5700 DECL_NAME (decl) = get_identifier (name);
5701 }
5702
5703 tree
5704 build_va_arg (location_t loc, tree expr, tree type)
5705 {
5706 expr = build1 (VA_ARG_EXPR, type, expr);
5707 SET_EXPR_LOCATION (expr, loc);
5708 return expr;
5709 }
5710
5711
5712 /* Linked list of disabled built-in functions. */
5713
5714 typedef struct disabled_builtin
5715 {
5716 const char *name;
5717 struct disabled_builtin *next;
5718 } disabled_builtin;
5719 static disabled_builtin *disabled_builtins = NULL;
5720
5721 static bool builtin_function_disabled_p (const char *);
5722
5723 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5724 begins with "__builtin_", give an error. */
5725
5726 void
5727 disable_builtin_function (const char *name)
5728 {
5729 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5730 error ("cannot disable built-in function %qs", name);
5731 else
5732 {
5733 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5734 new_disabled_builtin->name = name;
5735 new_disabled_builtin->next = disabled_builtins;
5736 disabled_builtins = new_disabled_builtin;
5737 }
5738 }
5739
5740
5741 /* Return true if the built-in function NAME has been disabled, false
5742 otherwise. */
5743
5744 static bool
5745 builtin_function_disabled_p (const char *name)
5746 {
5747 disabled_builtin *p;
5748 for (p = disabled_builtins; p != NULL; p = p->next)
5749 {
5750 if (strcmp (name, p->name) == 0)
5751 return true;
5752 }
5753 return false;
5754 }
5755
5756
5757 /* Worker for DEF_BUILTIN.
5758 Possibly define a builtin function with one or two names.
5759 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5760 nonansi_p and flag_no_nonansi_builtin. */
5761
5762 static void
5763 def_builtin_1 (enum built_in_function fncode,
5764 const char *name,
5765 enum built_in_class fnclass,
5766 tree fntype, tree libtype,
5767 bool both_p, bool fallback_p, bool nonansi_p,
5768 tree fnattrs, bool implicit_p)
5769 {
5770 tree decl;
5771 const char *libname;
5772
5773 if (fntype == error_mark_node)
5774 return;
5775
5776 gcc_assert ((!both_p && !fallback_p)
5777 || !strncmp (name, "__builtin_",
5778 strlen ("__builtin_")));
5779
5780 libname = name + strlen ("__builtin_");
5781 decl = add_builtin_function (name, fntype, fncode, fnclass,
5782 (fallback_p ? libname : NULL),
5783 fnattrs);
5784
5785 set_builtin_decl (fncode, decl, implicit_p);
5786
5787 if (both_p
5788 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5789 && !(nonansi_p && flag_no_nonansi_builtin))
5790 add_builtin_function (libname, libtype, fncode, fnclass,
5791 NULL, fnattrs);
5792 }
5793 \f
5794 /* Nonzero if the type T promotes to int. This is (nearly) the
5795 integral promotions defined in ISO C99 6.3.1.1/2. */
5796
5797 bool
5798 c_promoting_integer_type_p (const_tree t)
5799 {
5800 switch (TREE_CODE (t))
5801 {
5802 case INTEGER_TYPE:
5803 return (TYPE_MAIN_VARIANT (t) == char_type_node
5804 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5805 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5806 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5807 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5808 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5809
5810 case ENUMERAL_TYPE:
5811 /* ??? Technically all enumerations not larger than an int
5812 promote to an int. But this is used along code paths
5813 that only want to notice a size change. */
5814 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5815
5816 case BOOLEAN_TYPE:
5817 return 1;
5818
5819 default:
5820 return 0;
5821 }
5822 }
5823
5824 /* Return 1 if PARMS specifies a fixed number of parameters
5825 and none of their types is affected by default promotions. */
5826
5827 int
5828 self_promoting_args_p (const_tree parms)
5829 {
5830 const_tree t;
5831 for (t = parms; t; t = TREE_CHAIN (t))
5832 {
5833 tree type = TREE_VALUE (t);
5834
5835 if (type == error_mark_node)
5836 continue;
5837
5838 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5839 return 0;
5840
5841 if (type == 0)
5842 return 0;
5843
5844 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5845 return 0;
5846
5847 if (c_promoting_integer_type_p (type))
5848 return 0;
5849 }
5850 return 1;
5851 }
5852
5853 /* Recursively remove any '*' or '&' operator from TYPE. */
5854 tree
5855 strip_pointer_operator (tree t)
5856 {
5857 while (POINTER_TYPE_P (t))
5858 t = TREE_TYPE (t);
5859 return t;
5860 }
5861
5862 /* Recursively remove pointer or array type from TYPE. */
5863 tree
5864 strip_pointer_or_array_types (tree t)
5865 {
5866 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5867 t = TREE_TYPE (t);
5868 return t;
5869 }
5870
5871 /* Used to compare case labels. K1 and K2 are actually tree nodes
5872 representing case labels, or NULL_TREE for a `default' label.
5873 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5874 K2, and 0 if K1 and K2 are equal. */
5875
5876 int
5877 case_compare (splay_tree_key k1, splay_tree_key k2)
5878 {
5879 /* Consider a NULL key (such as arises with a `default' label) to be
5880 smaller than anything else. */
5881 if (!k1)
5882 return k2 ? -1 : 0;
5883 else if (!k2)
5884 return k1 ? 1 : 0;
5885
5886 return tree_int_cst_compare ((tree) k1, (tree) k2);
5887 }
5888
5889 /* Process a case label, located at LOC, for the range LOW_VALUE
5890 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5891 then this case label is actually a `default' label. If only
5892 HIGH_VALUE is NULL_TREE, then case label was declared using the
5893 usual C/C++ syntax, rather than the GNU case range extension.
5894 CASES is a tree containing all the case ranges processed so far;
5895 COND is the condition for the switch-statement itself. Returns the
5896 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5897 is created. */
5898
5899 tree
5900 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5901 tree low_value, tree high_value)
5902 {
5903 tree type;
5904 tree label;
5905 tree case_label;
5906 splay_tree_node node;
5907
5908 /* Create the LABEL_DECL itself. */
5909 label = create_artificial_label (loc);
5910
5911 /* If there was an error processing the switch condition, bail now
5912 before we get more confused. */
5913 if (!cond || cond == error_mark_node)
5914 goto error_out;
5915
5916 if ((low_value && TREE_TYPE (low_value)
5917 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5918 || (high_value && TREE_TYPE (high_value)
5919 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5920 {
5921 error_at (loc, "pointers are not permitted as case values");
5922 goto error_out;
5923 }
5924
5925 /* Case ranges are a GNU extension. */
5926 if (high_value)
5927 pedwarn (loc, OPT_Wpedantic,
5928 "range expressions in switch statements are non-standard");
5929
5930 type = TREE_TYPE (cond);
5931 if (low_value)
5932 {
5933 low_value = check_case_value (low_value);
5934 low_value = convert_and_check (type, low_value);
5935 if (low_value == error_mark_node)
5936 goto error_out;
5937 }
5938 if (high_value)
5939 {
5940 high_value = check_case_value (high_value);
5941 high_value = convert_and_check (type, high_value);
5942 if (high_value == error_mark_node)
5943 goto error_out;
5944 }
5945
5946 if (low_value && high_value)
5947 {
5948 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5949 really a case range, even though it was written that way.
5950 Remove the HIGH_VALUE to simplify later processing. */
5951 if (tree_int_cst_equal (low_value, high_value))
5952 high_value = NULL_TREE;
5953 else if (!tree_int_cst_lt (low_value, high_value))
5954 warning_at (loc, 0, "empty range specified");
5955 }
5956
5957 /* See if the case is in range of the type of the original testing
5958 expression. If both low_value and high_value are out of range,
5959 don't insert the case label and return NULL_TREE. */
5960 if (low_value
5961 && !check_case_bounds (type, orig_type,
5962 &low_value, high_value ? &high_value : NULL))
5963 return NULL_TREE;
5964
5965 /* Look up the LOW_VALUE in the table of case labels we already
5966 have. */
5967 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5968 /* If there was not an exact match, check for overlapping ranges.
5969 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5970 that's a `default' label and the only overlap is an exact match. */
5971 if (!node && (low_value || high_value))
5972 {
5973 splay_tree_node low_bound;
5974 splay_tree_node high_bound;
5975
5976 /* Even though there wasn't an exact match, there might be an
5977 overlap between this case range and another case range.
5978 Since we've (inductively) not allowed any overlapping case
5979 ranges, we simply need to find the greatest low case label
5980 that is smaller that LOW_VALUE, and the smallest low case
5981 label that is greater than LOW_VALUE. If there is an overlap
5982 it will occur in one of these two ranges. */
5983 low_bound = splay_tree_predecessor (cases,
5984 (splay_tree_key) low_value);
5985 high_bound = splay_tree_successor (cases,
5986 (splay_tree_key) low_value);
5987
5988 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5989 the LOW_VALUE, so there is no need to check unless the
5990 LOW_BOUND is in fact itself a case range. */
5991 if (low_bound
5992 && CASE_HIGH ((tree) low_bound->value)
5993 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5994 low_value) >= 0)
5995 node = low_bound;
5996 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5997 range is bigger than the low end of the current range, so we
5998 are only interested if the current range is a real range, and
5999 not an ordinary case label. */
6000 else if (high_bound
6001 && high_value
6002 && (tree_int_cst_compare ((tree) high_bound->key,
6003 high_value)
6004 <= 0))
6005 node = high_bound;
6006 }
6007 /* If there was an overlap, issue an error. */
6008 if (node)
6009 {
6010 tree duplicate = CASE_LABEL ((tree) node->value);
6011
6012 if (high_value)
6013 {
6014 error_at (loc, "duplicate (or overlapping) case value");
6015 error_at (DECL_SOURCE_LOCATION (duplicate),
6016 "this is the first entry overlapping that value");
6017 }
6018 else if (low_value)
6019 {
6020 error_at (loc, "duplicate case value") ;
6021 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6022 }
6023 else
6024 {
6025 error_at (loc, "multiple default labels in one switch");
6026 error_at (DECL_SOURCE_LOCATION (duplicate),
6027 "this is the first default label");
6028 }
6029 goto error_out;
6030 }
6031
6032 /* Add a CASE_LABEL to the statement-tree. */
6033 case_label = add_stmt (build_case_label (low_value, high_value, label));
6034 /* Register this case label in the splay tree. */
6035 splay_tree_insert (cases,
6036 (splay_tree_key) low_value,
6037 (splay_tree_value) case_label);
6038
6039 return case_label;
6040
6041 error_out:
6042 /* Add a label so that the back-end doesn't think that the beginning of
6043 the switch is unreachable. Note that we do not add a case label, as
6044 that just leads to duplicates and thence to failure later on. */
6045 if (!cases->root)
6046 {
6047 tree t = create_artificial_label (loc);
6048 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6049 }
6050 return error_mark_node;
6051 }
6052
6053 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6054 Used to verify that case values match up with enumerator values. */
6055
6056 static void
6057 match_case_to_enum_1 (tree key, tree type, tree label)
6058 {
6059 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6060
6061 /* ??? Not working too hard to print the double-word value.
6062 Should perhaps be done with %lwd in the diagnostic routines? */
6063 if (TREE_INT_CST_HIGH (key) == 0)
6064 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6065 TREE_INT_CST_LOW (key));
6066 else if (!TYPE_UNSIGNED (type)
6067 && TREE_INT_CST_HIGH (key) == -1
6068 && TREE_INT_CST_LOW (key) != 0)
6069 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6070 -TREE_INT_CST_LOW (key));
6071 else
6072 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6073 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6074 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6075
6076 if (TYPE_NAME (type) == 0)
6077 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6078 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6079 "case value %qs not in enumerated type",
6080 buf);
6081 else
6082 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6083 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6084 "case value %qs not in enumerated type %qT",
6085 buf, type);
6086 }
6087
6088 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6089 Used to verify that case values match up with enumerator values. */
6090
6091 static int
6092 match_case_to_enum (splay_tree_node node, void *data)
6093 {
6094 tree label = (tree) node->value;
6095 tree type = (tree) data;
6096
6097 /* Skip default case. */
6098 if (!CASE_LOW (label))
6099 return 0;
6100
6101 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6102 when we did our enum->case scan. Reset our scratch bit after. */
6103 if (!CASE_LOW_SEEN (label))
6104 match_case_to_enum_1 (CASE_LOW (label), type, label);
6105 else
6106 CASE_LOW_SEEN (label) = 0;
6107
6108 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6109 not set, that means that CASE_HIGH did not appear when we did our
6110 enum->case scan. Reset our scratch bit after. */
6111 if (CASE_HIGH (label))
6112 {
6113 if (!CASE_HIGH_SEEN (label))
6114 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6115 else
6116 CASE_HIGH_SEEN (label) = 0;
6117 }
6118
6119 return 0;
6120 }
6121
6122 /* Handle -Wswitch*. Called from the front end after parsing the
6123 switch construct. */
6124 /* ??? Should probably be somewhere generic, since other languages
6125 besides C and C++ would want this. At the moment, however, C/C++
6126 are the only tree-ssa languages that support enumerations at all,
6127 so the point is moot. */
6128
6129 void
6130 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6131 tree type, tree cond)
6132 {
6133 splay_tree_node default_node;
6134 splay_tree_node node;
6135 tree chain;
6136
6137 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6138 return;
6139
6140 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6141 if (!default_node)
6142 warning_at (switch_location, OPT_Wswitch_default,
6143 "switch missing default case");
6144
6145 /* From here on, we only care about about enumerated types. */
6146 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6147 return;
6148
6149 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6150 if (!warn_switch_enum && !warn_switch)
6151 return;
6152
6153 /* Check the cases. Warn about case values which are not members of
6154 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6155 there is no default case, check that exactly all enumeration
6156 literals are covered by the cases. */
6157
6158 /* Clearing COND if it is not an integer constant simplifies
6159 the tests inside the loop below. */
6160 if (TREE_CODE (cond) != INTEGER_CST)
6161 cond = NULL_TREE;
6162
6163 /* The time complexity here is O(N*lg(N)) worst case, but for the
6164 common case of monotonically increasing enumerators, it is
6165 O(N), since the nature of the splay tree will keep the next
6166 element adjacent to the root at all times. */
6167
6168 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6169 {
6170 tree value = TREE_VALUE (chain);
6171 if (TREE_CODE (value) == CONST_DECL)
6172 value = DECL_INITIAL (value);
6173 node = splay_tree_lookup (cases, (splay_tree_key) value);
6174 if (node)
6175 {
6176 /* Mark the CASE_LOW part of the case entry as seen. */
6177 tree label = (tree) node->value;
6178 CASE_LOW_SEEN (label) = 1;
6179 continue;
6180 }
6181
6182 /* Even though there wasn't an exact match, there might be a
6183 case range which includes the enumerator's value. */
6184 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6185 if (node && CASE_HIGH ((tree) node->value))
6186 {
6187 tree label = (tree) node->value;
6188 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6189 if (cmp >= 0)
6190 {
6191 /* If we match the upper bound exactly, mark the CASE_HIGH
6192 part of the case entry as seen. */
6193 if (cmp == 0)
6194 CASE_HIGH_SEEN (label) = 1;
6195 continue;
6196 }
6197 }
6198
6199 /* We've now determined that this enumerated literal isn't
6200 handled by the case labels of the switch statement. */
6201
6202 /* If the switch expression is a constant, we only really care
6203 about whether that constant is handled by the switch. */
6204 if (cond && tree_int_cst_compare (cond, value))
6205 continue;
6206
6207 /* If there is a default_node, the only relevant option is
6208 Wswitch-enum. Otherwise, if both are enabled then we prefer
6209 to warn using -Wswitch because -Wswitch is enabled by -Wall
6210 while -Wswitch-enum is explicit. */
6211 warning_at (switch_location,
6212 (default_node || !warn_switch
6213 ? OPT_Wswitch_enum
6214 : OPT_Wswitch),
6215 "enumeration value %qE not handled in switch",
6216 TREE_PURPOSE (chain));
6217 }
6218
6219 /* Warn if there are case expressions that don't correspond to
6220 enumerators. This can occur since C and C++ don't enforce
6221 type-checking of assignments to enumeration variables.
6222
6223 The time complexity here is now always O(N) worst case, since
6224 we should have marked both the lower bound and upper bound of
6225 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6226 above. This scan also resets those fields. */
6227
6228 splay_tree_foreach (cases, match_case_to_enum, type);
6229 }
6230
6231 /* Finish an expression taking the address of LABEL (an
6232 IDENTIFIER_NODE). Returns an expression for the address.
6233
6234 LOC is the location for the expression returned. */
6235
6236 tree
6237 finish_label_address_expr (tree label, location_t loc)
6238 {
6239 tree result;
6240
6241 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6242
6243 if (label == error_mark_node)
6244 return error_mark_node;
6245
6246 label = lookup_label (label);
6247 if (label == NULL_TREE)
6248 result = null_pointer_node;
6249 else
6250 {
6251 TREE_USED (label) = 1;
6252 result = build1 (ADDR_EXPR, ptr_type_node, label);
6253 /* The current function is not necessarily uninlinable.
6254 Computed gotos are incompatible with inlining, but the value
6255 here could be used only in a diagnostic, for example. */
6256 protected_set_expr_location (result, loc);
6257 }
6258
6259 return result;
6260 }
6261 \f
6262
6263 /* Given a boolean expression ARG, return a tree representing an increment
6264 or decrement (as indicated by CODE) of ARG. The front end must check for
6265 invalid cases (e.g., decrement in C++). */
6266 tree
6267 boolean_increment (enum tree_code code, tree arg)
6268 {
6269 tree val;
6270 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6271
6272 arg = stabilize_reference (arg);
6273 switch (code)
6274 {
6275 case PREINCREMENT_EXPR:
6276 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6277 break;
6278 case POSTINCREMENT_EXPR:
6279 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6280 arg = save_expr (arg);
6281 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6282 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6283 break;
6284 case PREDECREMENT_EXPR:
6285 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6286 invert_truthvalue_loc (input_location, arg));
6287 break;
6288 case POSTDECREMENT_EXPR:
6289 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6290 invert_truthvalue_loc (input_location, arg));
6291 arg = save_expr (arg);
6292 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6293 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6294 break;
6295 default:
6296 gcc_unreachable ();
6297 }
6298 TREE_SIDE_EFFECTS (val) = 1;
6299 return val;
6300 }
6301 \f
6302 /* Built-in macros for stddef.h and stdint.h, that require macros
6303 defined in this file. */
6304 void
6305 c_stddef_cpp_builtins(void)
6306 {
6307 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6308 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6309 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6310 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6311 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6312 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6313 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6314 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6315 if (SIG_ATOMIC_TYPE)
6316 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6317 if (INT8_TYPE)
6318 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6319 if (INT16_TYPE)
6320 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6321 if (INT32_TYPE)
6322 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6323 if (INT64_TYPE)
6324 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6325 if (UINT8_TYPE)
6326 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6327 if (UINT16_TYPE)
6328 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6329 if (UINT32_TYPE)
6330 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6331 if (UINT64_TYPE)
6332 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6333 if (INT_LEAST8_TYPE)
6334 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6335 if (INT_LEAST16_TYPE)
6336 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6337 if (INT_LEAST32_TYPE)
6338 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6339 if (INT_LEAST64_TYPE)
6340 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6341 if (UINT_LEAST8_TYPE)
6342 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6343 if (UINT_LEAST16_TYPE)
6344 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6345 if (UINT_LEAST32_TYPE)
6346 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6347 if (UINT_LEAST64_TYPE)
6348 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6349 if (INT_FAST8_TYPE)
6350 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6351 if (INT_FAST16_TYPE)
6352 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6353 if (INT_FAST32_TYPE)
6354 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6355 if (INT_FAST64_TYPE)
6356 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6357 if (UINT_FAST8_TYPE)
6358 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6359 if (UINT_FAST16_TYPE)
6360 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6361 if (UINT_FAST32_TYPE)
6362 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6363 if (UINT_FAST64_TYPE)
6364 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6365 if (INTPTR_TYPE)
6366 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6367 if (UINTPTR_TYPE)
6368 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6369 }
6370
6371 static void
6372 c_init_attributes (void)
6373 {
6374 /* Fill in the built_in_attributes array. */
6375 #define DEF_ATTR_NULL_TREE(ENUM) \
6376 built_in_attributes[(int) ENUM] = NULL_TREE;
6377 #define DEF_ATTR_INT(ENUM, VALUE) \
6378 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6379 #define DEF_ATTR_STRING(ENUM, VALUE) \
6380 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6381 #define DEF_ATTR_IDENT(ENUM, STRING) \
6382 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6383 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6384 built_in_attributes[(int) ENUM] \
6385 = tree_cons (built_in_attributes[(int) PURPOSE], \
6386 built_in_attributes[(int) VALUE], \
6387 built_in_attributes[(int) CHAIN]);
6388 #include "builtin-attrs.def"
6389 #undef DEF_ATTR_NULL_TREE
6390 #undef DEF_ATTR_INT
6391 #undef DEF_ATTR_IDENT
6392 #undef DEF_ATTR_TREE_LIST
6393 }
6394
6395 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6396 identifier as an argument, so the front end shouldn't look it up. */
6397
6398 bool
6399 attribute_takes_identifier_p (const_tree attr_id)
6400 {
6401 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6402 if (spec == NULL)
6403 /* Unknown attribute that we'll end up ignoring, return true so we
6404 don't complain about an identifier argument. */
6405 return true;
6406 else if (!strcmp ("mode", spec->name)
6407 || !strcmp ("format", spec->name)
6408 || !strcmp ("cleanup", spec->name))
6409 return true;
6410 else
6411 return targetm.attribute_takes_identifier_p (attr_id);
6412 }
6413
6414 /* Attribute handlers common to C front ends. */
6415
6416 /* Handle a "packed" attribute; arguments as in
6417 struct attribute_spec.handler. */
6418
6419 static tree
6420 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6421 int flags, bool *no_add_attrs)
6422 {
6423 if (TYPE_P (*node))
6424 {
6425 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6426 *node = build_variant_type_copy (*node);
6427 TYPE_PACKED (*node) = 1;
6428 }
6429 else if (TREE_CODE (*node) == FIELD_DECL)
6430 {
6431 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6432 /* Still pack bitfields. */
6433 && ! DECL_INITIAL (*node))
6434 warning (OPT_Wattributes,
6435 "%qE attribute ignored for field of type %qT",
6436 name, TREE_TYPE (*node));
6437 else
6438 DECL_PACKED (*node) = 1;
6439 }
6440 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6441 used for DECL_REGISTER. It wouldn't mean anything anyway.
6442 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6443 that changes what the typedef is typing. */
6444 else
6445 {
6446 warning (OPT_Wattributes, "%qE attribute ignored", name);
6447 *no_add_attrs = true;
6448 }
6449
6450 return NULL_TREE;
6451 }
6452
6453 /* Handle a "nocommon" attribute; arguments as in
6454 struct attribute_spec.handler. */
6455
6456 static tree
6457 handle_nocommon_attribute (tree *node, tree name,
6458 tree ARG_UNUSED (args),
6459 int ARG_UNUSED (flags), bool *no_add_attrs)
6460 {
6461 if (TREE_CODE (*node) == VAR_DECL)
6462 DECL_COMMON (*node) = 0;
6463 else
6464 {
6465 warning (OPT_Wattributes, "%qE attribute ignored", name);
6466 *no_add_attrs = true;
6467 }
6468
6469 return NULL_TREE;
6470 }
6471
6472 /* Handle a "common" attribute; arguments as in
6473 struct attribute_spec.handler. */
6474
6475 static tree
6476 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6477 int ARG_UNUSED (flags), bool *no_add_attrs)
6478 {
6479 if (TREE_CODE (*node) == VAR_DECL)
6480 DECL_COMMON (*node) = 1;
6481 else
6482 {
6483 warning (OPT_Wattributes, "%qE attribute ignored", name);
6484 *no_add_attrs = true;
6485 }
6486
6487 return NULL_TREE;
6488 }
6489
6490 /* Handle a "noreturn" attribute; arguments as in
6491 struct attribute_spec.handler. */
6492
6493 static tree
6494 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6495 int ARG_UNUSED (flags), bool *no_add_attrs)
6496 {
6497 tree type = TREE_TYPE (*node);
6498
6499 /* See FIXME comment in c_common_attribute_table. */
6500 if (TREE_CODE (*node) == FUNCTION_DECL
6501 || objc_method_decl (TREE_CODE (*node)))
6502 TREE_THIS_VOLATILE (*node) = 1;
6503 else if (TREE_CODE (type) == POINTER_TYPE
6504 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6505 TREE_TYPE (*node)
6506 = build_pointer_type
6507 (build_type_variant (TREE_TYPE (type),
6508 TYPE_READONLY (TREE_TYPE (type)), 1));
6509 else
6510 {
6511 warning (OPT_Wattributes, "%qE attribute ignored", name);
6512 *no_add_attrs = true;
6513 }
6514
6515 return NULL_TREE;
6516 }
6517
6518 /* Handle a "hot" and attribute; arguments as in
6519 struct attribute_spec.handler. */
6520
6521 static tree
6522 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6523 int ARG_UNUSED (flags), bool *no_add_attrs)
6524 {
6525 if (TREE_CODE (*node) == FUNCTION_DECL
6526 || TREE_CODE (*node) == LABEL_DECL)
6527 {
6528 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6529 {
6530 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6531 name, "cold");
6532 *no_add_attrs = true;
6533 }
6534 /* Most of the rest of the hot processing is done later with
6535 lookup_attribute. */
6536 }
6537 else
6538 {
6539 warning (OPT_Wattributes, "%qE attribute ignored", name);
6540 *no_add_attrs = true;
6541 }
6542
6543 return NULL_TREE;
6544 }
6545
6546 /* Handle a "cold" and attribute; arguments as in
6547 struct attribute_spec.handler. */
6548
6549 static tree
6550 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6551 int ARG_UNUSED (flags), bool *no_add_attrs)
6552 {
6553 if (TREE_CODE (*node) == FUNCTION_DECL
6554 || TREE_CODE (*node) == LABEL_DECL)
6555 {
6556 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6557 {
6558 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6559 name, "hot");
6560 *no_add_attrs = true;
6561 }
6562 /* Most of the rest of the cold processing is done later with
6563 lookup_attribute. */
6564 }
6565 else
6566 {
6567 warning (OPT_Wattributes, "%qE attribute ignored", name);
6568 *no_add_attrs = true;
6569 }
6570
6571 return NULL_TREE;
6572 }
6573
6574 /* Handle a "no_sanitize_address" attribute; arguments as in
6575 struct attribute_spec.handler. */
6576
6577 static tree
6578 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6579 bool *no_add_attrs)
6580 {
6581 if (TREE_CODE (*node) != FUNCTION_DECL)
6582 {
6583 warning (OPT_Wattributes, "%qE attribute ignored", name);
6584 *no_add_attrs = true;
6585 }
6586
6587 return NULL_TREE;
6588 }
6589
6590 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6591 struct attribute_spec.handler. */
6592
6593 static tree
6594 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6595 bool *no_add_attrs)
6596 {
6597 if (TREE_CODE (*node) != FUNCTION_DECL)
6598 warning (OPT_Wattributes, "%qE attribute ignored", name);
6599 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6600 DECL_ATTRIBUTES (*node)
6601 = tree_cons (get_identifier ("no_sanitize_address"),
6602 NULL_TREE, DECL_ATTRIBUTES (*node));
6603 *no_add_attrs = true;
6604 return NULL_TREE;
6605 }
6606
6607 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6608 struct attribute_spec.handler. */
6609
6610 static tree
6611 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6612 bool *no_add_attrs)
6613 {
6614 if (TREE_CODE (*node) != FUNCTION_DECL)
6615 {
6616 warning (OPT_Wattributes, "%qE attribute ignored", name);
6617 *no_add_attrs = true;
6618 }
6619
6620 return NULL_TREE;
6621 }
6622
6623 /* Handle a "noinline" attribute; arguments as in
6624 struct attribute_spec.handler. */
6625
6626 static tree
6627 handle_noinline_attribute (tree *node, tree name,
6628 tree ARG_UNUSED (args),
6629 int ARG_UNUSED (flags), bool *no_add_attrs)
6630 {
6631 if (TREE_CODE (*node) == FUNCTION_DECL)
6632 DECL_UNINLINABLE (*node) = 1;
6633 else
6634 {
6635 warning (OPT_Wattributes, "%qE attribute ignored", name);
6636 *no_add_attrs = true;
6637 }
6638
6639 return NULL_TREE;
6640 }
6641
6642 /* Handle a "noclone" attribute; arguments as in
6643 struct attribute_spec.handler. */
6644
6645 static tree
6646 handle_noclone_attribute (tree *node, tree name,
6647 tree ARG_UNUSED (args),
6648 int ARG_UNUSED (flags), bool *no_add_attrs)
6649 {
6650 if (TREE_CODE (*node) != FUNCTION_DECL)
6651 {
6652 warning (OPT_Wattributes, "%qE attribute ignored", name);
6653 *no_add_attrs = true;
6654 }
6655
6656 return NULL_TREE;
6657 }
6658
6659 /* Handle a "always_inline" attribute; arguments as in
6660 struct attribute_spec.handler. */
6661
6662 static tree
6663 handle_always_inline_attribute (tree *node, tree name,
6664 tree ARG_UNUSED (args),
6665 int ARG_UNUSED (flags),
6666 bool *no_add_attrs)
6667 {
6668 if (TREE_CODE (*node) == FUNCTION_DECL)
6669 {
6670 /* Set the attribute and mark it for disregarding inline
6671 limits. */
6672 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6673 }
6674 else
6675 {
6676 warning (OPT_Wattributes, "%qE attribute ignored", name);
6677 *no_add_attrs = true;
6678 }
6679
6680 return NULL_TREE;
6681 }
6682
6683 /* Handle a "gnu_inline" attribute; arguments as in
6684 struct attribute_spec.handler. */
6685
6686 static tree
6687 handle_gnu_inline_attribute (tree *node, tree name,
6688 tree ARG_UNUSED (args),
6689 int ARG_UNUSED (flags),
6690 bool *no_add_attrs)
6691 {
6692 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6693 {
6694 /* Do nothing else, just set the attribute. We'll get at
6695 it later with lookup_attribute. */
6696 }
6697 else
6698 {
6699 warning (OPT_Wattributes, "%qE attribute ignored", name);
6700 *no_add_attrs = true;
6701 }
6702
6703 return NULL_TREE;
6704 }
6705
6706 /* Handle a "leaf" attribute; arguments as in
6707 struct attribute_spec.handler. */
6708
6709 static tree
6710 handle_leaf_attribute (tree *node, tree name,
6711 tree ARG_UNUSED (args),
6712 int ARG_UNUSED (flags), bool *no_add_attrs)
6713 {
6714 if (TREE_CODE (*node) != FUNCTION_DECL)
6715 {
6716 warning (OPT_Wattributes, "%qE attribute ignored", name);
6717 *no_add_attrs = true;
6718 }
6719 if (!TREE_PUBLIC (*node))
6720 {
6721 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6722 *no_add_attrs = true;
6723 }
6724
6725 return NULL_TREE;
6726 }
6727
6728 /* Handle an "artificial" attribute; arguments as in
6729 struct attribute_spec.handler. */
6730
6731 static tree
6732 handle_artificial_attribute (tree *node, tree name,
6733 tree ARG_UNUSED (args),
6734 int ARG_UNUSED (flags),
6735 bool *no_add_attrs)
6736 {
6737 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6738 {
6739 /* Do nothing else, just set the attribute. We'll get at
6740 it later with lookup_attribute. */
6741 }
6742 else
6743 {
6744 warning (OPT_Wattributes, "%qE attribute ignored", name);
6745 *no_add_attrs = true;
6746 }
6747
6748 return NULL_TREE;
6749 }
6750
6751 /* Handle a "flatten" attribute; arguments as in
6752 struct attribute_spec.handler. */
6753
6754 static tree
6755 handle_flatten_attribute (tree *node, tree name,
6756 tree args ATTRIBUTE_UNUSED,
6757 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6758 {
6759 if (TREE_CODE (*node) == FUNCTION_DECL)
6760 /* Do nothing else, just set the attribute. We'll get at
6761 it later with lookup_attribute. */
6762 ;
6763 else
6764 {
6765 warning (OPT_Wattributes, "%qE attribute ignored", name);
6766 *no_add_attrs = true;
6767 }
6768
6769 return NULL_TREE;
6770 }
6771
6772 /* Handle a "warning" or "error" attribute; arguments as in
6773 struct attribute_spec.handler. */
6774
6775 static tree
6776 handle_error_attribute (tree *node, tree name, tree args,
6777 int ARG_UNUSED (flags), bool *no_add_attrs)
6778 {
6779 if (TREE_CODE (*node) == FUNCTION_DECL
6780 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6781 /* Do nothing else, just set the attribute. We'll get at
6782 it later with lookup_attribute. */
6783 ;
6784 else
6785 {
6786 warning (OPT_Wattributes, "%qE attribute ignored", name);
6787 *no_add_attrs = true;
6788 }
6789
6790 return NULL_TREE;
6791 }
6792
6793 /* Handle a "used" attribute; arguments as in
6794 struct attribute_spec.handler. */
6795
6796 static tree
6797 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6798 int ARG_UNUSED (flags), bool *no_add_attrs)
6799 {
6800 tree node = *pnode;
6801
6802 if (TREE_CODE (node) == FUNCTION_DECL
6803 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6804 || (TREE_CODE (node) == TYPE_DECL))
6805 {
6806 TREE_USED (node) = 1;
6807 DECL_PRESERVE_P (node) = 1;
6808 if (TREE_CODE (node) == VAR_DECL)
6809 DECL_READ_P (node) = 1;
6810 }
6811 else
6812 {
6813 warning (OPT_Wattributes, "%qE attribute ignored", name);
6814 *no_add_attrs = true;
6815 }
6816
6817 return NULL_TREE;
6818 }
6819
6820 /* Handle a "unused" attribute; arguments as in
6821 struct attribute_spec.handler. */
6822
6823 static tree
6824 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6825 int flags, bool *no_add_attrs)
6826 {
6827 if (DECL_P (*node))
6828 {
6829 tree decl = *node;
6830
6831 if (TREE_CODE (decl) == PARM_DECL
6832 || TREE_CODE (decl) == VAR_DECL
6833 || TREE_CODE (decl) == FUNCTION_DECL
6834 || TREE_CODE (decl) == LABEL_DECL
6835 || TREE_CODE (decl) == TYPE_DECL)
6836 {
6837 TREE_USED (decl) = 1;
6838 if (TREE_CODE (decl) == VAR_DECL
6839 || TREE_CODE (decl) == PARM_DECL)
6840 DECL_READ_P (decl) = 1;
6841 }
6842 else
6843 {
6844 warning (OPT_Wattributes, "%qE attribute ignored", name);
6845 *no_add_attrs = true;
6846 }
6847 }
6848 else
6849 {
6850 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6851 *node = build_variant_type_copy (*node);
6852 TREE_USED (*node) = 1;
6853 }
6854
6855 return NULL_TREE;
6856 }
6857
6858 /* Handle a "externally_visible" attribute; arguments as in
6859 struct attribute_spec.handler. */
6860
6861 static tree
6862 handle_externally_visible_attribute (tree *pnode, tree name,
6863 tree ARG_UNUSED (args),
6864 int ARG_UNUSED (flags),
6865 bool *no_add_attrs)
6866 {
6867 tree node = *pnode;
6868
6869 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6870 {
6871 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6872 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6873 {
6874 warning (OPT_Wattributes,
6875 "%qE attribute have effect only on public objects", name);
6876 *no_add_attrs = true;
6877 }
6878 }
6879 else
6880 {
6881 warning (OPT_Wattributes, "%qE attribute ignored", name);
6882 *no_add_attrs = true;
6883 }
6884
6885 return NULL_TREE;
6886 }
6887
6888 /* Handle a "const" attribute; arguments as in
6889 struct attribute_spec.handler. */
6890
6891 static tree
6892 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6893 int ARG_UNUSED (flags), bool *no_add_attrs)
6894 {
6895 tree type = TREE_TYPE (*node);
6896
6897 /* See FIXME comment on noreturn in c_common_attribute_table. */
6898 if (TREE_CODE (*node) == FUNCTION_DECL)
6899 TREE_READONLY (*node) = 1;
6900 else if (TREE_CODE (type) == POINTER_TYPE
6901 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6902 TREE_TYPE (*node)
6903 = build_pointer_type
6904 (build_type_variant (TREE_TYPE (type), 1,
6905 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6906 else
6907 {
6908 warning (OPT_Wattributes, "%qE attribute ignored", name);
6909 *no_add_attrs = true;
6910 }
6911
6912 return NULL_TREE;
6913 }
6914
6915 /* Handle a "transparent_union" attribute; arguments as in
6916 struct attribute_spec.handler. */
6917
6918 static tree
6919 handle_transparent_union_attribute (tree *node, tree name,
6920 tree ARG_UNUSED (args), int flags,
6921 bool *no_add_attrs)
6922 {
6923 tree type;
6924
6925 *no_add_attrs = true;
6926
6927
6928 if (TREE_CODE (*node) == TYPE_DECL
6929 && ! (flags & ATTR_FLAG_CXX11))
6930 node = &TREE_TYPE (*node);
6931 type = *node;
6932
6933 if (TREE_CODE (type) == UNION_TYPE)
6934 {
6935 /* Make sure that the first field will work for a transparent union.
6936 If the type isn't complete yet, leave the check to the code in
6937 finish_struct. */
6938 if (TYPE_SIZE (type))
6939 {
6940 tree first = first_field (type);
6941 if (first == NULL_TREE
6942 || DECL_ARTIFICIAL (first)
6943 || TYPE_MODE (type) != DECL_MODE (first))
6944 goto ignored;
6945 }
6946
6947 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6948 {
6949 /* If the type isn't complete yet, setting the flag
6950 on a variant wouldn't ever be checked. */
6951 if (!TYPE_SIZE (type))
6952 goto ignored;
6953
6954 /* build_duplicate_type doesn't work for C++. */
6955 if (c_dialect_cxx ())
6956 goto ignored;
6957
6958 /* A type variant isn't good enough, since we don't a cast
6959 to such a type removed as a no-op. */
6960 *node = type = build_duplicate_type (type);
6961 }
6962
6963 TYPE_TRANSPARENT_AGGR (type) = 1;
6964 return NULL_TREE;
6965 }
6966
6967 ignored:
6968 warning (OPT_Wattributes, "%qE attribute ignored", name);
6969 return NULL_TREE;
6970 }
6971
6972 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6973 get the requested priority for a constructor or destructor,
6974 possibly issuing diagnostics for invalid or reserved
6975 priorities. */
6976
6977 static priority_type
6978 get_priority (tree args, bool is_destructor)
6979 {
6980 HOST_WIDE_INT pri;
6981 tree arg;
6982
6983 if (!args)
6984 return DEFAULT_INIT_PRIORITY;
6985
6986 if (!SUPPORTS_INIT_PRIORITY)
6987 {
6988 if (is_destructor)
6989 error ("destructor priorities are not supported");
6990 else
6991 error ("constructor priorities are not supported");
6992 return DEFAULT_INIT_PRIORITY;
6993 }
6994
6995 arg = TREE_VALUE (args);
6996 arg = default_conversion (arg);
6997 if (!host_integerp (arg, /*pos=*/0)
6998 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6999 goto invalid;
7000
7001 pri = tree_low_cst (arg, /*pos=*/0);
7002 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7003 goto invalid;
7004
7005 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7006 {
7007 if (is_destructor)
7008 warning (0,
7009 "destructor priorities from 0 to %d are reserved "
7010 "for the implementation",
7011 MAX_RESERVED_INIT_PRIORITY);
7012 else
7013 warning (0,
7014 "constructor priorities from 0 to %d are reserved "
7015 "for the implementation",
7016 MAX_RESERVED_INIT_PRIORITY);
7017 }
7018 return pri;
7019
7020 invalid:
7021 if (is_destructor)
7022 error ("destructor priorities must be integers from 0 to %d inclusive",
7023 MAX_INIT_PRIORITY);
7024 else
7025 error ("constructor priorities must be integers from 0 to %d inclusive",
7026 MAX_INIT_PRIORITY);
7027 return DEFAULT_INIT_PRIORITY;
7028 }
7029
7030 /* Handle a "constructor" attribute; arguments as in
7031 struct attribute_spec.handler. */
7032
7033 static tree
7034 handle_constructor_attribute (tree *node, tree name, tree args,
7035 int ARG_UNUSED (flags),
7036 bool *no_add_attrs)
7037 {
7038 tree decl = *node;
7039 tree type = TREE_TYPE (decl);
7040
7041 if (TREE_CODE (decl) == FUNCTION_DECL
7042 && TREE_CODE (type) == FUNCTION_TYPE
7043 && decl_function_context (decl) == 0)
7044 {
7045 priority_type priority;
7046 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7047 priority = get_priority (args, /*is_destructor=*/false);
7048 SET_DECL_INIT_PRIORITY (decl, priority);
7049 TREE_USED (decl) = 1;
7050 }
7051 else
7052 {
7053 warning (OPT_Wattributes, "%qE attribute ignored", name);
7054 *no_add_attrs = true;
7055 }
7056
7057 return NULL_TREE;
7058 }
7059
7060 /* Handle a "destructor" attribute; arguments as in
7061 struct attribute_spec.handler. */
7062
7063 static tree
7064 handle_destructor_attribute (tree *node, tree name, tree args,
7065 int ARG_UNUSED (flags),
7066 bool *no_add_attrs)
7067 {
7068 tree decl = *node;
7069 tree type = TREE_TYPE (decl);
7070
7071 if (TREE_CODE (decl) == FUNCTION_DECL
7072 && TREE_CODE (type) == FUNCTION_TYPE
7073 && decl_function_context (decl) == 0)
7074 {
7075 priority_type priority;
7076 DECL_STATIC_DESTRUCTOR (decl) = 1;
7077 priority = get_priority (args, /*is_destructor=*/true);
7078 SET_DECL_FINI_PRIORITY (decl, priority);
7079 TREE_USED (decl) = 1;
7080 }
7081 else
7082 {
7083 warning (OPT_Wattributes, "%qE attribute ignored", name);
7084 *no_add_attrs = true;
7085 }
7086
7087 return NULL_TREE;
7088 }
7089
7090 /* Nonzero if the mode is a valid vector mode for this architecture.
7091 This returns nonzero even if there is no hardware support for the
7092 vector mode, but we can emulate with narrower modes. */
7093
7094 static int
7095 vector_mode_valid_p (enum machine_mode mode)
7096 {
7097 enum mode_class mclass = GET_MODE_CLASS (mode);
7098 enum machine_mode innermode;
7099
7100 /* Doh! What's going on? */
7101 if (mclass != MODE_VECTOR_INT
7102 && mclass != MODE_VECTOR_FLOAT
7103 && mclass != MODE_VECTOR_FRACT
7104 && mclass != MODE_VECTOR_UFRACT
7105 && mclass != MODE_VECTOR_ACCUM
7106 && mclass != MODE_VECTOR_UACCUM)
7107 return 0;
7108
7109 /* Hardware support. Woo hoo! */
7110 if (targetm.vector_mode_supported_p (mode))
7111 return 1;
7112
7113 innermode = GET_MODE_INNER (mode);
7114
7115 /* We should probably return 1 if requesting V4DI and we have no DI,
7116 but we have V2DI, but this is probably very unlikely. */
7117
7118 /* If we have support for the inner mode, we can safely emulate it.
7119 We may not have V2DI, but me can emulate with a pair of DIs. */
7120 return targetm.scalar_mode_supported_p (innermode);
7121 }
7122
7123
7124 /* Handle a "mode" attribute; arguments as in
7125 struct attribute_spec.handler. */
7126
7127 static tree
7128 handle_mode_attribute (tree *node, tree name, tree args,
7129 int ARG_UNUSED (flags), bool *no_add_attrs)
7130 {
7131 tree type = *node;
7132 tree ident = TREE_VALUE (args);
7133
7134 *no_add_attrs = true;
7135
7136 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7137 warning (OPT_Wattributes, "%qE attribute ignored", name);
7138 else
7139 {
7140 int j;
7141 const char *p = IDENTIFIER_POINTER (ident);
7142 int len = strlen (p);
7143 enum machine_mode mode = VOIDmode;
7144 tree typefm;
7145 bool valid_mode;
7146
7147 if (len > 4 && p[0] == '_' && p[1] == '_'
7148 && p[len - 1] == '_' && p[len - 2] == '_')
7149 {
7150 char *newp = (char *) alloca (len - 1);
7151
7152 strcpy (newp, &p[2]);
7153 newp[len - 4] = '\0';
7154 p = newp;
7155 }
7156
7157 /* Change this type to have a type with the specified mode.
7158 First check for the special modes. */
7159 if (!strcmp (p, "byte"))
7160 mode = byte_mode;
7161 else if (!strcmp (p, "word"))
7162 mode = word_mode;
7163 else if (!strcmp (p, "pointer"))
7164 mode = ptr_mode;
7165 else if (!strcmp (p, "libgcc_cmp_return"))
7166 mode = targetm.libgcc_cmp_return_mode ();
7167 else if (!strcmp (p, "libgcc_shift_count"))
7168 mode = targetm.libgcc_shift_count_mode ();
7169 else if (!strcmp (p, "unwind_word"))
7170 mode = targetm.unwind_word_mode ();
7171 else
7172 for (j = 0; j < NUM_MACHINE_MODES; j++)
7173 if (!strcmp (p, GET_MODE_NAME (j)))
7174 {
7175 mode = (enum machine_mode) j;
7176 break;
7177 }
7178
7179 if (mode == VOIDmode)
7180 {
7181 error ("unknown machine mode %qE", ident);
7182 return NULL_TREE;
7183 }
7184
7185 valid_mode = false;
7186 switch (GET_MODE_CLASS (mode))
7187 {
7188 case MODE_INT:
7189 case MODE_PARTIAL_INT:
7190 case MODE_FLOAT:
7191 case MODE_DECIMAL_FLOAT:
7192 case MODE_FRACT:
7193 case MODE_UFRACT:
7194 case MODE_ACCUM:
7195 case MODE_UACCUM:
7196 valid_mode = targetm.scalar_mode_supported_p (mode);
7197 break;
7198
7199 case MODE_COMPLEX_INT:
7200 case MODE_COMPLEX_FLOAT:
7201 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7202 break;
7203
7204 case MODE_VECTOR_INT:
7205 case MODE_VECTOR_FLOAT:
7206 case MODE_VECTOR_FRACT:
7207 case MODE_VECTOR_UFRACT:
7208 case MODE_VECTOR_ACCUM:
7209 case MODE_VECTOR_UACCUM:
7210 warning (OPT_Wattributes, "specifying vector types with "
7211 "__attribute__ ((mode)) is deprecated");
7212 warning (OPT_Wattributes,
7213 "use __attribute__ ((vector_size)) instead");
7214 valid_mode = vector_mode_valid_p (mode);
7215 break;
7216
7217 default:
7218 break;
7219 }
7220 if (!valid_mode)
7221 {
7222 error ("unable to emulate %qs", p);
7223 return NULL_TREE;
7224 }
7225
7226 if (POINTER_TYPE_P (type))
7227 {
7228 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7229 tree (*fn)(tree, enum machine_mode, bool);
7230
7231 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7232 {
7233 error ("invalid pointer mode %qs", p);
7234 return NULL_TREE;
7235 }
7236
7237 if (TREE_CODE (type) == POINTER_TYPE)
7238 fn = build_pointer_type_for_mode;
7239 else
7240 fn = build_reference_type_for_mode;
7241 typefm = fn (TREE_TYPE (type), mode, false);
7242 }
7243 else
7244 {
7245 /* For fixed-point modes, we need to test if the signness of type
7246 and the machine mode are consistent. */
7247 if (ALL_FIXED_POINT_MODE_P (mode)
7248 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7249 {
7250 error ("signedness of type and machine mode %qs don%'t match", p);
7251 return NULL_TREE;
7252 }
7253 /* For fixed-point modes, we need to pass saturating info. */
7254 typefm = lang_hooks.types.type_for_mode (mode,
7255 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7256 : TYPE_UNSIGNED (type));
7257 }
7258
7259 if (typefm == NULL_TREE)
7260 {
7261 error ("no data type for mode %qs", p);
7262 return NULL_TREE;
7263 }
7264 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7265 {
7266 /* For enumeral types, copy the precision from the integer
7267 type returned above. If not an INTEGER_TYPE, we can't use
7268 this mode for this type. */
7269 if (TREE_CODE (typefm) != INTEGER_TYPE)
7270 {
7271 error ("cannot use mode %qs for enumeral types", p);
7272 return NULL_TREE;
7273 }
7274
7275 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7276 {
7277 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7278 typefm = type;
7279 }
7280 else
7281 {
7282 /* We cannot build a type variant, as there's code that assumes
7283 that TYPE_MAIN_VARIANT has the same mode. This includes the
7284 debug generators. Instead, create a subrange type. This
7285 results in all of the enumeral values being emitted only once
7286 in the original, and the subtype gets them by reference. */
7287 if (TYPE_UNSIGNED (type))
7288 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7289 else
7290 typefm = make_signed_type (TYPE_PRECISION (typefm));
7291 TREE_TYPE (typefm) = type;
7292 }
7293 }
7294 else if (VECTOR_MODE_P (mode)
7295 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7296 : TREE_CODE (type) != TREE_CODE (typefm))
7297 {
7298 error ("mode %qs applied to inappropriate type", p);
7299 return NULL_TREE;
7300 }
7301
7302 *node = typefm;
7303 }
7304
7305 return NULL_TREE;
7306 }
7307
7308 /* Handle a "section" attribute; arguments as in
7309 struct attribute_spec.handler. */
7310
7311 static tree
7312 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7313 int ARG_UNUSED (flags), bool *no_add_attrs)
7314 {
7315 tree decl = *node;
7316
7317 if (targetm_common.have_named_sections)
7318 {
7319 user_defined_section_attribute = true;
7320
7321 if ((TREE_CODE (decl) == FUNCTION_DECL
7322 || TREE_CODE (decl) == VAR_DECL)
7323 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7324 {
7325 if (TREE_CODE (decl) == VAR_DECL
7326 && current_function_decl != NULL_TREE
7327 && !TREE_STATIC (decl))
7328 {
7329 error_at (DECL_SOURCE_LOCATION (decl),
7330 "section attribute cannot be specified for "
7331 "local variables");
7332 *no_add_attrs = true;
7333 }
7334
7335 /* The decl may have already been given a section attribute
7336 from a previous declaration. Ensure they match. */
7337 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7338 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7339 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7340 {
7341 error ("section of %q+D conflicts with previous declaration",
7342 *node);
7343 *no_add_attrs = true;
7344 }
7345 else if (TREE_CODE (decl) == VAR_DECL
7346 && !targetm.have_tls && targetm.emutls.tmpl_section
7347 && DECL_THREAD_LOCAL_P (decl))
7348 {
7349 error ("section of %q+D cannot be overridden", *node);
7350 *no_add_attrs = true;
7351 }
7352 else
7353 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7354 }
7355 else
7356 {
7357 error ("section attribute not allowed for %q+D", *node);
7358 *no_add_attrs = true;
7359 }
7360 }
7361 else
7362 {
7363 error_at (DECL_SOURCE_LOCATION (*node),
7364 "section attributes are not supported for this target");
7365 *no_add_attrs = true;
7366 }
7367
7368 return NULL_TREE;
7369 }
7370
7371 /* Check whether ALIGN is a valid user-specified alignment. If so,
7372 return its base-2 log; if not, output an error and return -1. If
7373 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7374 no error. */
7375 int
7376 check_user_alignment (const_tree align, bool allow_zero)
7377 {
7378 int i;
7379
7380 if (TREE_CODE (align) != INTEGER_CST
7381 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7382 {
7383 error ("requested alignment is not an integer constant");
7384 return -1;
7385 }
7386 else if (allow_zero && integer_zerop (align))
7387 return -1;
7388 else if (tree_int_cst_sgn (align) == -1
7389 || (i = tree_log2 (align)) == -1)
7390 {
7391 error ("requested alignment is not a positive power of 2");
7392 return -1;
7393 }
7394 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7395 {
7396 error ("requested alignment is too large");
7397 return -1;
7398 }
7399 return i;
7400 }
7401
7402 /*
7403 If in c++-11, check if the c++-11 alignment constraint with respect
7404 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7405 c++-11 mode, does nothing.
7406
7407 [dcl.align]2/ says:
7408
7409 [* if the constant expression evaluates to a fundamental alignment,
7410 the alignment requirement of the declared entity shall be the
7411 specified fundamental alignment.
7412
7413 * if the constant expression evaluates to an extended alignment
7414 and the implementation supports that alignment in the context
7415 of the declaration, the alignment of the declared entity shall
7416 be that alignment
7417
7418 * if the constant expression evaluates to an extended alignment
7419 and the implementation does not support that alignment in the
7420 context of the declaration, the program is ill-formed]. */
7421
7422 static bool
7423 check_cxx_fundamental_alignment_constraints (tree node,
7424 unsigned align_log,
7425 int flags)
7426 {
7427 bool alignment_too_large_p = false;
7428 unsigned requested_alignment = 1U << align_log;
7429 unsigned max_align = 0;
7430
7431 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7432 || (node == NULL_TREE || node == error_mark_node))
7433 return true;
7434
7435 if (cxx_fundamental_alignment_p (requested_alignment))
7436 return true;
7437
7438 if (DECL_P (node))
7439 {
7440 if (TREE_STATIC (node))
7441 {
7442 /* For file scope variables and static members, the target
7443 supports alignments that are at most
7444 MAX_OFILE_ALIGNMENT. */
7445 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7446 alignment_too_large_p = true;
7447 }
7448 else
7449 {
7450 #ifdef BIGGEST_FIELD_ALIGNMENT
7451 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7452 #else
7453 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7454 #endif
7455 /* For non-static members, the target supports either
7456 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7457 if it is defined or BIGGEST_ALIGNMENT. */
7458 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7459 if (TREE_CODE (node) == FIELD_DECL
7460 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7461 alignment_too_large_p = true;
7462 #undef MAX_TARGET_FIELD_ALIGNMENT
7463 /* For stack variables, the target supports at most
7464 MAX_STACK_ALIGNMENT. */
7465 else if (decl_function_context (node) != NULL
7466 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7467 alignment_too_large_p = true;
7468 }
7469 }
7470 else if (TYPE_P (node))
7471 {
7472 /* Let's be liberal for types. */
7473 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7474 alignment_too_large_p = true;
7475 }
7476
7477 if (alignment_too_large_p)
7478 pedwarn (input_location, OPT_Wattributes,
7479 "requested alignment %d is larger than %d",
7480 requested_alignment, max_align);
7481
7482 return !alignment_too_large_p;
7483 }
7484
7485 /* Handle a "aligned" attribute; arguments as in
7486 struct attribute_spec.handler. */
7487
7488 static tree
7489 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7490 int flags, bool *no_add_attrs)
7491 {
7492 tree decl = NULL_TREE;
7493 tree *type = NULL;
7494 int is_type = 0;
7495 tree align_expr = (args ? TREE_VALUE (args)
7496 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7497 int i;
7498
7499 if (DECL_P (*node))
7500 {
7501 decl = *node;
7502 type = &TREE_TYPE (decl);
7503 is_type = TREE_CODE (*node) == TYPE_DECL;
7504 }
7505 else if (TYPE_P (*node))
7506 type = node, is_type = 1;
7507
7508 if ((i = check_user_alignment (align_expr, false)) == -1
7509 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7510 *no_add_attrs = true;
7511 else if (is_type)
7512 {
7513 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7514 /* OK, modify the type in place. */;
7515 /* If we have a TYPE_DECL, then copy the type, so that we
7516 don't accidentally modify a builtin type. See pushdecl. */
7517 else if (decl && TREE_TYPE (decl) != error_mark_node
7518 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7519 {
7520 tree tt = TREE_TYPE (decl);
7521 *type = build_variant_type_copy (*type);
7522 DECL_ORIGINAL_TYPE (decl) = tt;
7523 TYPE_NAME (*type) = decl;
7524 TREE_USED (*type) = TREE_USED (decl);
7525 TREE_TYPE (decl) = *type;
7526 }
7527 else
7528 *type = build_variant_type_copy (*type);
7529
7530 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7531 TYPE_USER_ALIGN (*type) = 1;
7532 }
7533 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7534 && TREE_CODE (decl) != FIELD_DECL)
7535 {
7536 error ("alignment may not be specified for %q+D", decl);
7537 *no_add_attrs = true;
7538 }
7539 else if (DECL_USER_ALIGN (decl)
7540 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7541 /* C++-11 [dcl.align/4]:
7542
7543 When multiple alignment-specifiers are specified for an
7544 entity, the alignment requirement shall be set to the
7545 strictest specified alignment.
7546
7547 This formally comes from the c++11 specification but we are
7548 doing it for the GNU attribute syntax as well. */
7549 *no_add_attrs = true;
7550 else if (TREE_CODE (decl) == FUNCTION_DECL
7551 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7552 {
7553 if (DECL_USER_ALIGN (decl))
7554 error ("alignment for %q+D was previously specified as %d "
7555 "and may not be decreased", decl,
7556 DECL_ALIGN (decl) / BITS_PER_UNIT);
7557 else
7558 error ("alignment for %q+D must be at least %d", decl,
7559 DECL_ALIGN (decl) / BITS_PER_UNIT);
7560 *no_add_attrs = true;
7561 }
7562 else
7563 {
7564 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7565 DECL_USER_ALIGN (decl) = 1;
7566 }
7567
7568 return NULL_TREE;
7569 }
7570
7571 /* Handle a "weak" attribute; arguments as in
7572 struct attribute_spec.handler. */
7573
7574 static tree
7575 handle_weak_attribute (tree *node, tree name,
7576 tree ARG_UNUSED (args),
7577 int ARG_UNUSED (flags),
7578 bool * ARG_UNUSED (no_add_attrs))
7579 {
7580 if (TREE_CODE (*node) == FUNCTION_DECL
7581 && DECL_DECLARED_INLINE_P (*node))
7582 {
7583 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7584 *no_add_attrs = true;
7585 }
7586 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7587 {
7588 error ("indirect function %q+D cannot be declared weak", *node);
7589 *no_add_attrs = true;
7590 return NULL_TREE;
7591 }
7592 else if (TREE_CODE (*node) == FUNCTION_DECL
7593 || TREE_CODE (*node) == VAR_DECL)
7594 declare_weak (*node);
7595 else
7596 warning (OPT_Wattributes, "%qE attribute ignored", name);
7597
7598 return NULL_TREE;
7599 }
7600
7601 /* Handle an "alias" or "ifunc" attribute; arguments as in
7602 struct attribute_spec.handler, except that IS_ALIAS tells us
7603 whether this is an alias as opposed to ifunc attribute. */
7604
7605 static tree
7606 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7607 bool *no_add_attrs)
7608 {
7609 tree decl = *node;
7610
7611 if (TREE_CODE (decl) != FUNCTION_DECL
7612 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7613 {
7614 warning (OPT_Wattributes, "%qE attribute ignored", name);
7615 *no_add_attrs = true;
7616 }
7617 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7618 || (TREE_CODE (decl) != FUNCTION_DECL
7619 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7620 /* A static variable declaration is always a tentative definition,
7621 but the alias is a non-tentative definition which overrides. */
7622 || (TREE_CODE (decl) != FUNCTION_DECL
7623 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7624 {
7625 error ("%q+D defined both normally and as %qE attribute", decl, name);
7626 *no_add_attrs = true;
7627 return NULL_TREE;
7628 }
7629 else if (!is_alias
7630 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7631 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7632 {
7633 error ("weak %q+D cannot be defined %qE", decl, name);
7634 *no_add_attrs = true;
7635 return NULL_TREE;
7636 }
7637
7638 /* Note that the very first time we process a nested declaration,
7639 decl_function_context will not be set. Indeed, *would* never
7640 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7641 we do below. After such frobbery, pushdecl would set the context.
7642 In any case, this is never what we want. */
7643 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7644 {
7645 tree id;
7646
7647 id = TREE_VALUE (args);
7648 if (TREE_CODE (id) != STRING_CST)
7649 {
7650 error ("attribute %qE argument not a string", name);
7651 *no_add_attrs = true;
7652 return NULL_TREE;
7653 }
7654 id = get_identifier (TREE_STRING_POINTER (id));
7655 /* This counts as a use of the object pointed to. */
7656 TREE_USED (id) = 1;
7657
7658 if (TREE_CODE (decl) == FUNCTION_DECL)
7659 DECL_INITIAL (decl) = error_mark_node;
7660 else
7661 TREE_STATIC (decl) = 1;
7662
7663 if (!is_alias)
7664 /* ifuncs are also aliases, so set that attribute too. */
7665 DECL_ATTRIBUTES (decl)
7666 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7667 }
7668 else
7669 {
7670 warning (OPT_Wattributes, "%qE attribute ignored", name);
7671 *no_add_attrs = true;
7672 }
7673
7674 return NULL_TREE;
7675 }
7676
7677 /* Handle an "alias" or "ifunc" attribute; arguments as in
7678 struct attribute_spec.handler. */
7679
7680 static tree
7681 handle_ifunc_attribute (tree *node, tree name, tree args,
7682 int ARG_UNUSED (flags), bool *no_add_attrs)
7683 {
7684 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7685 }
7686
7687 /* Handle an "alias" or "ifunc" attribute; arguments as in
7688 struct attribute_spec.handler. */
7689
7690 static tree
7691 handle_alias_attribute (tree *node, tree name, tree args,
7692 int ARG_UNUSED (flags), bool *no_add_attrs)
7693 {
7694 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7695 }
7696
7697 /* Handle a "weakref" attribute; arguments as in struct
7698 attribute_spec.handler. */
7699
7700 static tree
7701 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7702 int flags, bool *no_add_attrs)
7703 {
7704 tree attr = NULL_TREE;
7705
7706 /* We must ignore the attribute when it is associated with
7707 local-scoped decls, since attribute alias is ignored and many
7708 such symbols do not even have a DECL_WEAK field. */
7709 if (decl_function_context (*node)
7710 || current_function_decl
7711 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7712 {
7713 warning (OPT_Wattributes, "%qE attribute ignored", name);
7714 *no_add_attrs = true;
7715 return NULL_TREE;
7716 }
7717
7718 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7719 {
7720 error ("indirect function %q+D cannot be declared weakref", *node);
7721 *no_add_attrs = true;
7722 return NULL_TREE;
7723 }
7724
7725 /* The idea here is that `weakref("name")' mutates into `weakref,
7726 alias("name")', and weakref without arguments, in turn,
7727 implicitly adds weak. */
7728
7729 if (args)
7730 {
7731 attr = tree_cons (get_identifier ("alias"), args, attr);
7732 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7733
7734 *no_add_attrs = true;
7735
7736 decl_attributes (node, attr, flags);
7737 }
7738 else
7739 {
7740 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7741 error_at (DECL_SOURCE_LOCATION (*node),
7742 "weakref attribute must appear before alias attribute");
7743
7744 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7745 and that isn't supported; and because it wants to add it to
7746 the list of weak decls, which isn't helpful. */
7747 DECL_WEAK (*node) = 1;
7748 }
7749
7750 return NULL_TREE;
7751 }
7752
7753 /* Handle an "visibility" attribute; arguments as in
7754 struct attribute_spec.handler. */
7755
7756 static tree
7757 handle_visibility_attribute (tree *node, tree name, tree args,
7758 int ARG_UNUSED (flags),
7759 bool *ARG_UNUSED (no_add_attrs))
7760 {
7761 tree decl = *node;
7762 tree id = TREE_VALUE (args);
7763 enum symbol_visibility vis;
7764
7765 if (TYPE_P (*node))
7766 {
7767 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7768 /* OK */;
7769 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7770 {
7771 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7772 name);
7773 return NULL_TREE;
7774 }
7775 else if (TYPE_FIELDS (*node))
7776 {
7777 error ("%qE attribute ignored because %qT is already defined",
7778 name, *node);
7779 return NULL_TREE;
7780 }
7781 }
7782 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7783 {
7784 warning (OPT_Wattributes, "%qE attribute ignored", name);
7785 return NULL_TREE;
7786 }
7787
7788 if (TREE_CODE (id) != STRING_CST)
7789 {
7790 error ("visibility argument not a string");
7791 return NULL_TREE;
7792 }
7793
7794 /* If this is a type, set the visibility on the type decl. */
7795 if (TYPE_P (decl))
7796 {
7797 decl = TYPE_NAME (decl);
7798 if (!decl)
7799 return NULL_TREE;
7800 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7801 {
7802 warning (OPT_Wattributes, "%qE attribute ignored on types",
7803 name);
7804 return NULL_TREE;
7805 }
7806 }
7807
7808 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7809 vis = VISIBILITY_DEFAULT;
7810 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7811 vis = VISIBILITY_INTERNAL;
7812 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7813 vis = VISIBILITY_HIDDEN;
7814 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7815 vis = VISIBILITY_PROTECTED;
7816 else
7817 {
7818 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7819 vis = VISIBILITY_DEFAULT;
7820 }
7821
7822 if (DECL_VISIBILITY_SPECIFIED (decl)
7823 && vis != DECL_VISIBILITY (decl))
7824 {
7825 tree attributes = (TYPE_P (*node)
7826 ? TYPE_ATTRIBUTES (*node)
7827 : DECL_ATTRIBUTES (decl));
7828 if (lookup_attribute ("visibility", attributes))
7829 error ("%qD redeclared with different visibility", decl);
7830 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7831 && lookup_attribute ("dllimport", attributes))
7832 error ("%qD was declared %qs which implies default visibility",
7833 decl, "dllimport");
7834 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7835 && lookup_attribute ("dllexport", attributes))
7836 error ("%qD was declared %qs which implies default visibility",
7837 decl, "dllexport");
7838 }
7839
7840 DECL_VISIBILITY (decl) = vis;
7841 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7842
7843 /* Go ahead and attach the attribute to the node as well. This is needed
7844 so we can determine whether we have VISIBILITY_DEFAULT because the
7845 visibility was not specified, or because it was explicitly overridden
7846 from the containing scope. */
7847
7848 return NULL_TREE;
7849 }
7850
7851 /* Determine the ELF symbol visibility for DECL, which is either a
7852 variable or a function. It is an error to use this function if a
7853 definition of DECL is not available in this translation unit.
7854 Returns true if the final visibility has been determined by this
7855 function; false if the caller is free to make additional
7856 modifications. */
7857
7858 bool
7859 c_determine_visibility (tree decl)
7860 {
7861 gcc_assert (TREE_CODE (decl) == VAR_DECL
7862 || TREE_CODE (decl) == FUNCTION_DECL);
7863
7864 /* If the user explicitly specified the visibility with an
7865 attribute, honor that. DECL_VISIBILITY will have been set during
7866 the processing of the attribute. We check for an explicit
7867 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7868 to distinguish the use of an attribute from the use of a "#pragma
7869 GCC visibility push(...)"; in the latter case we still want other
7870 considerations to be able to overrule the #pragma. */
7871 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7872 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7873 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7874 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7875 return true;
7876
7877 /* Set default visibility to whatever the user supplied with
7878 visibility_specified depending on #pragma GCC visibility. */
7879 if (!DECL_VISIBILITY_SPECIFIED (decl))
7880 {
7881 if (visibility_options.inpragma
7882 || DECL_VISIBILITY (decl) != default_visibility)
7883 {
7884 DECL_VISIBILITY (decl) = default_visibility;
7885 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7886 /* If visibility changed and DECL already has DECL_RTL, ensure
7887 symbol flags are updated. */
7888 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7889 || TREE_CODE (decl) == FUNCTION_DECL)
7890 && DECL_RTL_SET_P (decl))
7891 make_decl_rtl (decl);
7892 }
7893 }
7894 return false;
7895 }
7896
7897 /* Handle an "tls_model" attribute; arguments as in
7898 struct attribute_spec.handler. */
7899
7900 static tree
7901 handle_tls_model_attribute (tree *node, tree name, tree args,
7902 int ARG_UNUSED (flags), bool *no_add_attrs)
7903 {
7904 tree id;
7905 tree decl = *node;
7906 enum tls_model kind;
7907
7908 *no_add_attrs = true;
7909
7910 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7911 {
7912 warning (OPT_Wattributes, "%qE attribute ignored", name);
7913 return NULL_TREE;
7914 }
7915
7916 kind = DECL_TLS_MODEL (decl);
7917 id = TREE_VALUE (args);
7918 if (TREE_CODE (id) != STRING_CST)
7919 {
7920 error ("tls_model argument not a string");
7921 return NULL_TREE;
7922 }
7923
7924 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7925 kind = TLS_MODEL_LOCAL_EXEC;
7926 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7927 kind = TLS_MODEL_INITIAL_EXEC;
7928 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7929 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7930 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7931 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7932 else
7933 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7934
7935 DECL_TLS_MODEL (decl) = kind;
7936 return NULL_TREE;
7937 }
7938
7939 /* Handle a "no_instrument_function" attribute; arguments as in
7940 struct attribute_spec.handler. */
7941
7942 static tree
7943 handle_no_instrument_function_attribute (tree *node, tree name,
7944 tree ARG_UNUSED (args),
7945 int ARG_UNUSED (flags),
7946 bool *no_add_attrs)
7947 {
7948 tree decl = *node;
7949
7950 if (TREE_CODE (decl) != FUNCTION_DECL)
7951 {
7952 error_at (DECL_SOURCE_LOCATION (decl),
7953 "%qE attribute applies only to functions", name);
7954 *no_add_attrs = true;
7955 }
7956 else if (DECL_INITIAL (decl))
7957 {
7958 error_at (DECL_SOURCE_LOCATION (decl),
7959 "can%'t set %qE attribute after definition", name);
7960 *no_add_attrs = true;
7961 }
7962 else
7963 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7964
7965 return NULL_TREE;
7966 }
7967
7968 /* Handle a "malloc" attribute; arguments as in
7969 struct attribute_spec.handler. */
7970
7971 static tree
7972 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7973 int ARG_UNUSED (flags), bool *no_add_attrs)
7974 {
7975 if (TREE_CODE (*node) == FUNCTION_DECL
7976 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7977 DECL_IS_MALLOC (*node) = 1;
7978 else
7979 {
7980 warning (OPT_Wattributes, "%qE attribute ignored", name);
7981 *no_add_attrs = true;
7982 }
7983
7984 return NULL_TREE;
7985 }
7986
7987 /* Handle a "alloc_size" attribute; arguments as in
7988 struct attribute_spec.handler. */
7989
7990 static tree
7991 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7992 int ARG_UNUSED (flags), bool *no_add_attrs)
7993 {
7994 unsigned arg_count = type_num_arguments (*node);
7995 for (; args; args = TREE_CHAIN (args))
7996 {
7997 tree position = TREE_VALUE (args);
7998
7999 if (TREE_CODE (position) != INTEGER_CST
8000 || TREE_INT_CST_HIGH (position)
8001 || TREE_INT_CST_LOW (position) < 1
8002 || TREE_INT_CST_LOW (position) > arg_count )
8003 {
8004 warning (OPT_Wattributes,
8005 "alloc_size parameter outside range");
8006 *no_add_attrs = true;
8007 return NULL_TREE;
8008 }
8009 }
8010 return NULL_TREE;
8011 }
8012
8013 /* Handle a "fn spec" attribute; arguments as in
8014 struct attribute_spec.handler. */
8015
8016 static tree
8017 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8018 tree args, int ARG_UNUSED (flags),
8019 bool *no_add_attrs ATTRIBUTE_UNUSED)
8020 {
8021 gcc_assert (args
8022 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8023 && !TREE_CHAIN (args));
8024 return NULL_TREE;
8025 }
8026
8027 /* Handle a "bnd_variable_size" attribute; arguments as in
8028 struct attribute_spec.handler. */
8029
8030 static tree
8031 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8032 int ARG_UNUSED (flags), bool *no_add_attrs)
8033 {
8034 if (TREE_CODE (*node) != FIELD_DECL)
8035 {
8036 warning (OPT_Wattributes, "%qE attribute ignored", name);
8037 *no_add_attrs = true;
8038 }
8039
8040 return NULL_TREE;
8041 }
8042
8043 /* Handle a "bnd_legacy" attribute; arguments as in
8044 struct attribute_spec.handler. */
8045
8046 static tree
8047 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8048 int ARG_UNUSED (flags), bool *no_add_attrs)
8049 {
8050 if (TREE_CODE (*node) != FUNCTION_DECL)
8051 {
8052 warning (OPT_Wattributes, "%qE attribute ignored", name);
8053 *no_add_attrs = true;
8054 }
8055
8056 return NULL_TREE;
8057 }
8058
8059 /* Handle a "warn_unused" attribute; arguments as in
8060 struct attribute_spec.handler. */
8061
8062 static tree
8063 handle_warn_unused_attribute (tree *node, tree name,
8064 tree args ATTRIBUTE_UNUSED,
8065 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8066 {
8067 if (TYPE_P (*node))
8068 /* Do nothing else, just set the attribute. We'll get at
8069 it later with lookup_attribute. */
8070 ;
8071 else
8072 {
8073 warning (OPT_Wattributes, "%qE attribute ignored", name);
8074 *no_add_attrs = true;
8075 }
8076
8077 return NULL_TREE;
8078 }
8079
8080 /* Handle an "omp declare simd" attribute; arguments as in
8081 struct attribute_spec.handler. */
8082
8083 static tree
8084 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8085 {
8086 return NULL_TREE;
8087 }
8088
8089 /* Handle an "omp declare target" attribute; arguments as in
8090 struct attribute_spec.handler. */
8091
8092 static tree
8093 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8094 {
8095 return NULL_TREE;
8096 }
8097
8098 /* Handle a "returns_twice" attribute; arguments as in
8099 struct attribute_spec.handler. */
8100
8101 static tree
8102 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8103 int ARG_UNUSED (flags), bool *no_add_attrs)
8104 {
8105 if (TREE_CODE (*node) == FUNCTION_DECL)
8106 DECL_IS_RETURNS_TWICE (*node) = 1;
8107 else
8108 {
8109 warning (OPT_Wattributes, "%qE attribute ignored", name);
8110 *no_add_attrs = true;
8111 }
8112
8113 return NULL_TREE;
8114 }
8115
8116 /* Handle a "no_limit_stack" attribute; arguments as in
8117 struct attribute_spec.handler. */
8118
8119 static tree
8120 handle_no_limit_stack_attribute (tree *node, tree name,
8121 tree ARG_UNUSED (args),
8122 int ARG_UNUSED (flags),
8123 bool *no_add_attrs)
8124 {
8125 tree decl = *node;
8126
8127 if (TREE_CODE (decl) != FUNCTION_DECL)
8128 {
8129 error_at (DECL_SOURCE_LOCATION (decl),
8130 "%qE attribute applies only to functions", name);
8131 *no_add_attrs = true;
8132 }
8133 else if (DECL_INITIAL (decl))
8134 {
8135 error_at (DECL_SOURCE_LOCATION (decl),
8136 "can%'t set %qE attribute after definition", name);
8137 *no_add_attrs = true;
8138 }
8139 else
8140 DECL_NO_LIMIT_STACK (decl) = 1;
8141
8142 return NULL_TREE;
8143 }
8144
8145 /* Handle a "pure" attribute; arguments as in
8146 struct attribute_spec.handler. */
8147
8148 static tree
8149 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8150 int ARG_UNUSED (flags), bool *no_add_attrs)
8151 {
8152 if (TREE_CODE (*node) == FUNCTION_DECL)
8153 DECL_PURE_P (*node) = 1;
8154 /* ??? TODO: Support types. */
8155 else
8156 {
8157 warning (OPT_Wattributes, "%qE attribute ignored", name);
8158 *no_add_attrs = true;
8159 }
8160
8161 return NULL_TREE;
8162 }
8163
8164 /* Digest an attribute list destined for a transactional memory statement.
8165 ALLOWED is the set of attributes that are allowed for this statement;
8166 return the attribute we parsed. Multiple attributes are never allowed. */
8167
8168 int
8169 parse_tm_stmt_attr (tree attrs, int allowed)
8170 {
8171 tree a_seen = NULL;
8172 int m_seen = 0;
8173
8174 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8175 {
8176 tree a = TREE_PURPOSE (attrs);
8177 int m = 0;
8178
8179 if (is_attribute_p ("outer", a))
8180 m = TM_STMT_ATTR_OUTER;
8181
8182 if ((m & allowed) == 0)
8183 {
8184 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8185 continue;
8186 }
8187
8188 if (m_seen == 0)
8189 {
8190 a_seen = a;
8191 m_seen = m;
8192 }
8193 else if (m_seen == m)
8194 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8195 else
8196 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8197 }
8198
8199 return m_seen;
8200 }
8201
8202 /* Transform a TM attribute name into a maskable integer and back.
8203 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8204 to how the lack of an attribute is treated. */
8205
8206 int
8207 tm_attr_to_mask (tree attr)
8208 {
8209 if (attr == NULL)
8210 return 0;
8211 if (is_attribute_p ("transaction_safe", attr))
8212 return TM_ATTR_SAFE;
8213 if (is_attribute_p ("transaction_callable", attr))
8214 return TM_ATTR_CALLABLE;
8215 if (is_attribute_p ("transaction_pure", attr))
8216 return TM_ATTR_PURE;
8217 if (is_attribute_p ("transaction_unsafe", attr))
8218 return TM_ATTR_IRREVOCABLE;
8219 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8220 return TM_ATTR_MAY_CANCEL_OUTER;
8221 return 0;
8222 }
8223
8224 tree
8225 tm_mask_to_attr (int mask)
8226 {
8227 const char *str;
8228 switch (mask)
8229 {
8230 case TM_ATTR_SAFE:
8231 str = "transaction_safe";
8232 break;
8233 case TM_ATTR_CALLABLE:
8234 str = "transaction_callable";
8235 break;
8236 case TM_ATTR_PURE:
8237 str = "transaction_pure";
8238 break;
8239 case TM_ATTR_IRREVOCABLE:
8240 str = "transaction_unsafe";
8241 break;
8242 case TM_ATTR_MAY_CANCEL_OUTER:
8243 str = "transaction_may_cancel_outer";
8244 break;
8245 default:
8246 gcc_unreachable ();
8247 }
8248 return get_identifier (str);
8249 }
8250
8251 /* Return the first TM attribute seen in LIST. */
8252
8253 tree
8254 find_tm_attribute (tree list)
8255 {
8256 for (; list ; list = TREE_CHAIN (list))
8257 {
8258 tree name = TREE_PURPOSE (list);
8259 if (tm_attr_to_mask (name) != 0)
8260 return name;
8261 }
8262 return NULL_TREE;
8263 }
8264
8265 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8266 Here we accept only function types, and verify that none of the other
8267 function TM attributes are also applied. */
8268 /* ??? We need to accept class types for C++, but not C. This greatly
8269 complicates this function, since we can no longer rely on the extra
8270 processing given by function_type_required. */
8271
8272 static tree
8273 handle_tm_attribute (tree *node, tree name, tree args,
8274 int flags, bool *no_add_attrs)
8275 {
8276 /* Only one path adds the attribute; others don't. */
8277 *no_add_attrs = true;
8278
8279 switch (TREE_CODE (*node))
8280 {
8281 case RECORD_TYPE:
8282 case UNION_TYPE:
8283 /* Only tm_callable and tm_safe apply to classes. */
8284 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8285 goto ignored;
8286 /* FALLTHRU */
8287
8288 case FUNCTION_TYPE:
8289 case METHOD_TYPE:
8290 {
8291 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8292 if (old_name == name)
8293 ;
8294 else if (old_name != NULL_TREE)
8295 error ("type was previously declared %qE", old_name);
8296 else
8297 *no_add_attrs = false;
8298 }
8299 break;
8300
8301 case POINTER_TYPE:
8302 {
8303 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8304 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8305 {
8306 tree fn_tmp = TREE_TYPE (*node);
8307 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8308 *node = build_pointer_type (fn_tmp);
8309 break;
8310 }
8311 }
8312 /* FALLTHRU */
8313
8314 default:
8315 /* If a function is next, pass it on to be tried next. */
8316 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8317 return tree_cons (name, args, NULL);
8318
8319 ignored:
8320 warning (OPT_Wattributes, "%qE attribute ignored", name);
8321 break;
8322 }
8323
8324 return NULL_TREE;
8325 }
8326
8327 /* Handle the TM_WRAP attribute; arguments as in
8328 struct attribute_spec.handler. */
8329
8330 static tree
8331 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8332 int ARG_UNUSED (flags), bool *no_add_attrs)
8333 {
8334 tree decl = *node;
8335
8336 /* We don't need the attribute even on success, since we
8337 record the entry in an external table. */
8338 *no_add_attrs = true;
8339
8340 if (TREE_CODE (decl) != FUNCTION_DECL)
8341 warning (OPT_Wattributes, "%qE attribute ignored", name);
8342 else
8343 {
8344 tree wrap_decl = TREE_VALUE (args);
8345 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8346 && TREE_CODE (wrap_decl) != VAR_DECL
8347 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8348 error ("%qE argument not an identifier", name);
8349 else
8350 {
8351 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8352 wrap_decl = lookup_name (wrap_decl);
8353 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8354 {
8355 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8356 TREE_TYPE (wrap_decl)))
8357 record_tm_replacement (wrap_decl, decl);
8358 else
8359 error ("%qD is not compatible with %qD", wrap_decl, decl);
8360 }
8361 else
8362 error ("transaction_wrap argument is not a function");
8363 }
8364 }
8365
8366 return NULL_TREE;
8367 }
8368
8369 /* Ignore the given attribute. Used when this attribute may be usefully
8370 overridden by the target, but is not used generically. */
8371
8372 static tree
8373 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8374 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8375 bool *no_add_attrs)
8376 {
8377 *no_add_attrs = true;
8378 return NULL_TREE;
8379 }
8380
8381 /* Handle a "no vops" attribute; arguments as in
8382 struct attribute_spec.handler. */
8383
8384 static tree
8385 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8386 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8387 bool *ARG_UNUSED (no_add_attrs))
8388 {
8389 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8390 DECL_IS_NOVOPS (*node) = 1;
8391 return NULL_TREE;
8392 }
8393
8394 /* Handle a "deprecated" attribute; arguments as in
8395 struct attribute_spec.handler. */
8396
8397 static tree
8398 handle_deprecated_attribute (tree *node, tree name,
8399 tree args, int flags,
8400 bool *no_add_attrs)
8401 {
8402 tree type = NULL_TREE;
8403 int warn = 0;
8404 tree what = NULL_TREE;
8405
8406 if (!args)
8407 *no_add_attrs = true;
8408 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8409 {
8410 error ("deprecated message is not a string");
8411 *no_add_attrs = true;
8412 }
8413
8414 if (DECL_P (*node))
8415 {
8416 tree decl = *node;
8417 type = TREE_TYPE (decl);
8418
8419 if (TREE_CODE (decl) == TYPE_DECL
8420 || TREE_CODE (decl) == PARM_DECL
8421 || TREE_CODE (decl) == VAR_DECL
8422 || TREE_CODE (decl) == FUNCTION_DECL
8423 || TREE_CODE (decl) == FIELD_DECL
8424 || objc_method_decl (TREE_CODE (decl)))
8425 TREE_DEPRECATED (decl) = 1;
8426 else
8427 warn = 1;
8428 }
8429 else if (TYPE_P (*node))
8430 {
8431 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8432 *node = build_variant_type_copy (*node);
8433 TREE_DEPRECATED (*node) = 1;
8434 type = *node;
8435 }
8436 else
8437 warn = 1;
8438
8439 if (warn)
8440 {
8441 *no_add_attrs = true;
8442 if (type && TYPE_NAME (type))
8443 {
8444 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8445 what = TYPE_NAME (*node);
8446 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8447 && DECL_NAME (TYPE_NAME (type)))
8448 what = DECL_NAME (TYPE_NAME (type));
8449 }
8450 if (what)
8451 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8452 else
8453 warning (OPT_Wattributes, "%qE attribute ignored", name);
8454 }
8455
8456 return NULL_TREE;
8457 }
8458
8459 /* Handle a "vector_size" attribute; arguments as in
8460 struct attribute_spec.handler. */
8461
8462 static tree
8463 handle_vector_size_attribute (tree *node, tree name, tree args,
8464 int ARG_UNUSED (flags),
8465 bool *no_add_attrs)
8466 {
8467 unsigned HOST_WIDE_INT vecsize, nunits;
8468 enum machine_mode orig_mode;
8469 tree type = *node, new_type, size;
8470
8471 *no_add_attrs = true;
8472
8473 size = TREE_VALUE (args);
8474
8475 if (!host_integerp (size, 1))
8476 {
8477 warning (OPT_Wattributes, "%qE attribute ignored", name);
8478 return NULL_TREE;
8479 }
8480
8481 /* Get the vector size (in bytes). */
8482 vecsize = tree_low_cst (size, 1);
8483
8484 /* We need to provide for vector pointers, vector arrays, and
8485 functions returning vectors. For example:
8486
8487 __attribute__((vector_size(16))) short *foo;
8488
8489 In this case, the mode is SI, but the type being modified is
8490 HI, so we need to look further. */
8491
8492 while (POINTER_TYPE_P (type)
8493 || TREE_CODE (type) == FUNCTION_TYPE
8494 || TREE_CODE (type) == METHOD_TYPE
8495 || TREE_CODE (type) == ARRAY_TYPE
8496 || TREE_CODE (type) == OFFSET_TYPE)
8497 type = TREE_TYPE (type);
8498
8499 /* Get the mode of the type being modified. */
8500 orig_mode = TYPE_MODE (type);
8501
8502 if ((!INTEGRAL_TYPE_P (type)
8503 && !SCALAR_FLOAT_TYPE_P (type)
8504 && !FIXED_POINT_TYPE_P (type))
8505 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8506 && GET_MODE_CLASS (orig_mode) != MODE_INT
8507 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8508 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8509 || TREE_CODE (type) == BOOLEAN_TYPE)
8510 {
8511 error ("invalid vector type for attribute %qE", name);
8512 return NULL_TREE;
8513 }
8514
8515 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8516 {
8517 error ("vector size not an integral multiple of component size");
8518 return NULL;
8519 }
8520
8521 if (vecsize == 0)
8522 {
8523 error ("zero vector size");
8524 return NULL;
8525 }
8526
8527 /* Calculate how many units fit in the vector. */
8528 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8529 if (nunits & (nunits - 1))
8530 {
8531 error ("number of components of the vector not a power of two");
8532 return NULL_TREE;
8533 }
8534
8535 new_type = build_vector_type (type, nunits);
8536
8537 /* Build back pointers if needed. */
8538 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8539
8540 return NULL_TREE;
8541 }
8542
8543 /* Handle the "nonnull" attribute. */
8544 static tree
8545 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8546 tree args, int ARG_UNUSED (flags),
8547 bool *no_add_attrs)
8548 {
8549 tree type = *node;
8550 unsigned HOST_WIDE_INT attr_arg_num;
8551
8552 /* If no arguments are specified, all pointer arguments should be
8553 non-null. Verify a full prototype is given so that the arguments
8554 will have the correct types when we actually check them later. */
8555 if (!args)
8556 {
8557 if (!prototype_p (type))
8558 {
8559 error ("nonnull attribute without arguments on a non-prototype");
8560 *no_add_attrs = true;
8561 }
8562 return NULL_TREE;
8563 }
8564
8565 /* Argument list specified. Verify that each argument number references
8566 a pointer argument. */
8567 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8568 {
8569 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8570
8571 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8572 {
8573 error ("nonnull argument has invalid operand number (argument %lu)",
8574 (unsigned long) attr_arg_num);
8575 *no_add_attrs = true;
8576 return NULL_TREE;
8577 }
8578
8579 if (prototype_p (type))
8580 {
8581 function_args_iterator iter;
8582 tree argument;
8583
8584 function_args_iter_init (&iter, type);
8585 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8586 {
8587 argument = function_args_iter_cond (&iter);
8588 if (argument == NULL_TREE || ck_num == arg_num)
8589 break;
8590 }
8591
8592 if (!argument
8593 || TREE_CODE (argument) == VOID_TYPE)
8594 {
8595 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8596 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8597 *no_add_attrs = true;
8598 return NULL_TREE;
8599 }
8600
8601 if (TREE_CODE (argument) != POINTER_TYPE)
8602 {
8603 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8604 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8605 *no_add_attrs = true;
8606 return NULL_TREE;
8607 }
8608 }
8609 }
8610
8611 return NULL_TREE;
8612 }
8613
8614 /* Check the argument list of a function call for null in argument slots
8615 that are marked as requiring a non-null pointer argument. The NARGS
8616 arguments are passed in the array ARGARRAY.
8617 */
8618
8619 static void
8620 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8621 {
8622 tree a;
8623 int i;
8624
8625 attrs = lookup_attribute ("nonnull", attrs);
8626 if (attrs == NULL_TREE)
8627 return;
8628
8629 a = attrs;
8630 /* See if any of the nonnull attributes has no arguments. If so,
8631 then every pointer argument is checked (in which case the check
8632 for pointer type is done in check_nonnull_arg). */
8633 if (TREE_VALUE (a) != NULL_TREE)
8634 do
8635 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8636 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8637
8638 if (a != NULL_TREE)
8639 for (i = 0; i < nargs; i++)
8640 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8641 i + 1);
8642 else
8643 {
8644 /* Walk the argument list. If we encounter an argument number we
8645 should check for non-null, do it. */
8646 for (i = 0; i < nargs; i++)
8647 {
8648 for (a = attrs; ; a = TREE_CHAIN (a))
8649 {
8650 a = lookup_attribute ("nonnull", a);
8651 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8652 break;
8653 }
8654
8655 if (a != NULL_TREE)
8656 check_function_arguments_recurse (check_nonnull_arg, NULL,
8657 argarray[i], i + 1);
8658 }
8659 }
8660 }
8661
8662 /* Check that the Nth argument of a function call (counting backwards
8663 from the end) is a (pointer)0. The NARGS arguments are passed in the
8664 array ARGARRAY. */
8665
8666 static void
8667 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8668 {
8669 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8670
8671 if (attr)
8672 {
8673 int len = 0;
8674 int pos = 0;
8675 tree sentinel;
8676 function_args_iterator iter;
8677 tree t;
8678
8679 /* Skip over the named arguments. */
8680 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8681 {
8682 if (len == nargs)
8683 break;
8684 len++;
8685 }
8686
8687 if (TREE_VALUE (attr))
8688 {
8689 tree p = TREE_VALUE (TREE_VALUE (attr));
8690 pos = TREE_INT_CST_LOW (p);
8691 }
8692
8693 /* The sentinel must be one of the varargs, i.e.
8694 in position >= the number of fixed arguments. */
8695 if ((nargs - 1 - pos) < len)
8696 {
8697 warning (OPT_Wformat_,
8698 "not enough variable arguments to fit a sentinel");
8699 return;
8700 }
8701
8702 /* Validate the sentinel. */
8703 sentinel = argarray[nargs - 1 - pos];
8704 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8705 || !integer_zerop (sentinel))
8706 /* Although __null (in C++) is only an integer we allow it
8707 nevertheless, as we are guaranteed that it's exactly
8708 as wide as a pointer, and we don't want to force
8709 users to cast the NULL they have written there.
8710 We warn with -Wstrict-null-sentinel, though. */
8711 && (warn_strict_null_sentinel || null_node != sentinel))
8712 warning (OPT_Wformat_, "missing sentinel in function call");
8713 }
8714 }
8715
8716 /* Helper for check_function_nonnull; given a list of operands which
8717 must be non-null in ARGS, determine if operand PARAM_NUM should be
8718 checked. */
8719
8720 static bool
8721 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8722 {
8723 unsigned HOST_WIDE_INT arg_num = 0;
8724
8725 for (; args; args = TREE_CHAIN (args))
8726 {
8727 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8728
8729 gcc_assert (found);
8730
8731 if (arg_num == param_num)
8732 return true;
8733 }
8734 return false;
8735 }
8736
8737 /* Check that the function argument PARAM (which is operand number
8738 PARAM_NUM) is non-null. This is called by check_function_nonnull
8739 via check_function_arguments_recurse. */
8740
8741 static void
8742 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8743 unsigned HOST_WIDE_INT param_num)
8744 {
8745 /* Just skip checking the argument if it's not a pointer. This can
8746 happen if the "nonnull" attribute was given without an operand
8747 list (which means to check every pointer argument). */
8748
8749 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8750 return;
8751
8752 if (integer_zerop (param))
8753 warning (OPT_Wnonnull, "null argument where non-null required "
8754 "(argument %lu)", (unsigned long) param_num);
8755 }
8756
8757 /* Helper for nonnull attribute handling; fetch the operand number
8758 from the attribute argument list. */
8759
8760 static bool
8761 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8762 {
8763 /* Verify the arg number is a constant. */
8764 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8765 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8766 return false;
8767
8768 *valp = TREE_INT_CST_LOW (arg_num_expr);
8769 return true;
8770 }
8771
8772 /* Handle a "nothrow" attribute; arguments as in
8773 struct attribute_spec.handler. */
8774
8775 static tree
8776 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8777 int ARG_UNUSED (flags), bool *no_add_attrs)
8778 {
8779 if (TREE_CODE (*node) == FUNCTION_DECL)
8780 TREE_NOTHROW (*node) = 1;
8781 /* ??? TODO: Support types. */
8782 else
8783 {
8784 warning (OPT_Wattributes, "%qE attribute ignored", name);
8785 *no_add_attrs = true;
8786 }
8787
8788 return NULL_TREE;
8789 }
8790
8791 /* Handle a "cleanup" attribute; arguments as in
8792 struct attribute_spec.handler. */
8793
8794 static tree
8795 handle_cleanup_attribute (tree *node, tree name, tree args,
8796 int ARG_UNUSED (flags), bool *no_add_attrs)
8797 {
8798 tree decl = *node;
8799 tree cleanup_id, cleanup_decl;
8800
8801 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8802 for global destructors in C++. This requires infrastructure that
8803 we don't have generically at the moment. It's also not a feature
8804 we'd be missing too much, since we do have attribute constructor. */
8805 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8806 {
8807 warning (OPT_Wattributes, "%qE attribute ignored", name);
8808 *no_add_attrs = true;
8809 return NULL_TREE;
8810 }
8811
8812 /* Verify that the argument is a function in scope. */
8813 /* ??? We could support pointers to functions here as well, if
8814 that was considered desirable. */
8815 cleanup_id = TREE_VALUE (args);
8816 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8817 {
8818 error ("cleanup argument not an identifier");
8819 *no_add_attrs = true;
8820 return NULL_TREE;
8821 }
8822 cleanup_decl = lookup_name (cleanup_id);
8823 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8824 {
8825 error ("cleanup argument not a function");
8826 *no_add_attrs = true;
8827 return NULL_TREE;
8828 }
8829
8830 /* That the function has proper type is checked with the
8831 eventual call to build_function_call. */
8832
8833 return NULL_TREE;
8834 }
8835
8836 /* Handle a "warn_unused_result" attribute. No special handling. */
8837
8838 static tree
8839 handle_warn_unused_result_attribute (tree *node, tree name,
8840 tree ARG_UNUSED (args),
8841 int ARG_UNUSED (flags), bool *no_add_attrs)
8842 {
8843 /* Ignore the attribute for functions not returning any value. */
8844 if (VOID_TYPE_P (TREE_TYPE (*node)))
8845 {
8846 warning (OPT_Wattributes, "%qE attribute ignored", name);
8847 *no_add_attrs = true;
8848 }
8849
8850 return NULL_TREE;
8851 }
8852
8853 /* Handle a "sentinel" attribute. */
8854
8855 static tree
8856 handle_sentinel_attribute (tree *node, tree name, tree args,
8857 int ARG_UNUSED (flags), bool *no_add_attrs)
8858 {
8859 if (!prototype_p (*node))
8860 {
8861 warning (OPT_Wattributes,
8862 "%qE attribute requires prototypes with named arguments", name);
8863 *no_add_attrs = true;
8864 }
8865 else
8866 {
8867 if (!stdarg_p (*node))
8868 {
8869 warning (OPT_Wattributes,
8870 "%qE attribute only applies to variadic functions", name);
8871 *no_add_attrs = true;
8872 }
8873 }
8874
8875 if (args)
8876 {
8877 tree position = TREE_VALUE (args);
8878
8879 if (TREE_CODE (position) != INTEGER_CST)
8880 {
8881 warning (OPT_Wattributes,
8882 "requested position is not an integer constant");
8883 *no_add_attrs = true;
8884 }
8885 else
8886 {
8887 if (tree_int_cst_lt (position, integer_zero_node))
8888 {
8889 warning (OPT_Wattributes,
8890 "requested position is less than zero");
8891 *no_add_attrs = true;
8892 }
8893 }
8894 }
8895
8896 return NULL_TREE;
8897 }
8898
8899 /* Handle a "type_generic" attribute. */
8900
8901 static tree
8902 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8903 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8904 bool * ARG_UNUSED (no_add_attrs))
8905 {
8906 /* Ensure we have a function type. */
8907 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8908
8909 /* Ensure we have a variadic function. */
8910 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8911
8912 return NULL_TREE;
8913 }
8914
8915 /* Handle a "target" attribute. */
8916
8917 static tree
8918 handle_target_attribute (tree *node, tree name, tree args, int flags,
8919 bool *no_add_attrs)
8920 {
8921 /* Ensure we have a function type. */
8922 if (TREE_CODE (*node) != FUNCTION_DECL)
8923 {
8924 warning (OPT_Wattributes, "%qE attribute ignored", name);
8925 *no_add_attrs = true;
8926 }
8927 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8928 flags))
8929 *no_add_attrs = true;
8930
8931 return NULL_TREE;
8932 }
8933
8934 /* Arguments being collected for optimization. */
8935 typedef const char *const_char_p; /* For DEF_VEC_P. */
8936 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8937
8938
8939 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8940 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8941 false for #pragma GCC optimize. */
8942
8943 bool
8944 parse_optimize_options (tree args, bool attr_p)
8945 {
8946 bool ret = true;
8947 unsigned opt_argc;
8948 unsigned i;
8949 int saved_flag_strict_aliasing;
8950 const char **opt_argv;
8951 struct cl_decoded_option *decoded_options;
8952 unsigned int decoded_options_count;
8953 tree ap;
8954
8955 /* Build up argv vector. Just in case the string is stored away, use garbage
8956 collected strings. */
8957 vec_safe_truncate (optimize_args, 0);
8958 vec_safe_push (optimize_args, (const char *) NULL);
8959
8960 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8961 {
8962 tree value = TREE_VALUE (ap);
8963
8964 if (TREE_CODE (value) == INTEGER_CST)
8965 {
8966 char buffer[20];
8967 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8968 vec_safe_push (optimize_args, ggc_strdup (buffer));
8969 }
8970
8971 else if (TREE_CODE (value) == STRING_CST)
8972 {
8973 /* Split string into multiple substrings. */
8974 size_t len = TREE_STRING_LENGTH (value);
8975 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8976 char *end = p + len;
8977 char *comma;
8978 char *next_p = p;
8979
8980 while (next_p != NULL)
8981 {
8982 size_t len2;
8983 char *q, *r;
8984
8985 p = next_p;
8986 comma = strchr (p, ',');
8987 if (comma)
8988 {
8989 len2 = comma - p;
8990 *comma = '\0';
8991 next_p = comma+1;
8992 }
8993 else
8994 {
8995 len2 = end - p;
8996 next_p = NULL;
8997 }
8998
8999 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9000
9001 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9002 options. */
9003 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9004 {
9005 ret = false;
9006 if (attr_p)
9007 warning (OPT_Wattributes,
9008 "bad option %s to optimize attribute", p);
9009 else
9010 warning (OPT_Wpragmas,
9011 "bad option %s to pragma attribute", p);
9012 continue;
9013 }
9014
9015 if (*p != '-')
9016 {
9017 *r++ = '-';
9018
9019 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9020 itself is -Os, and any other switch begins with a -f. */
9021 if ((*p >= '0' && *p <= '9')
9022 || (p[0] == 's' && p[1] == '\0'))
9023 *r++ = 'O';
9024 else if (*p != 'O')
9025 *r++ = 'f';
9026 }
9027
9028 memcpy (r, p, len2);
9029 r[len2] = '\0';
9030 vec_safe_push (optimize_args, (const char *) q);
9031 }
9032
9033 }
9034 }
9035
9036 opt_argc = optimize_args->length ();
9037 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9038
9039 for (i = 1; i < opt_argc; i++)
9040 opt_argv[i] = (*optimize_args)[i];
9041
9042 saved_flag_strict_aliasing = flag_strict_aliasing;
9043
9044 /* Now parse the options. */
9045 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9046 &decoded_options,
9047 &decoded_options_count);
9048 decode_options (&global_options, &global_options_set,
9049 decoded_options, decoded_options_count,
9050 input_location, global_dc);
9051
9052 targetm.override_options_after_change();
9053
9054 /* Don't allow changing -fstrict-aliasing. */
9055 flag_strict_aliasing = saved_flag_strict_aliasing;
9056
9057 optimize_args->truncate (0);
9058 return ret;
9059 }
9060
9061 /* For handling "optimize" attribute. arguments as in
9062 struct attribute_spec.handler. */
9063
9064 static tree
9065 handle_optimize_attribute (tree *node, tree name, tree args,
9066 int ARG_UNUSED (flags), bool *no_add_attrs)
9067 {
9068 /* Ensure we have a function type. */
9069 if (TREE_CODE (*node) != FUNCTION_DECL)
9070 {
9071 warning (OPT_Wattributes, "%qE attribute ignored", name);
9072 *no_add_attrs = true;
9073 }
9074 else
9075 {
9076 struct cl_optimization cur_opts;
9077 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9078
9079 /* Save current options. */
9080 cl_optimization_save (&cur_opts, &global_options);
9081
9082 /* If we previously had some optimization options, use them as the
9083 default. */
9084 if (old_opts)
9085 cl_optimization_restore (&global_options,
9086 TREE_OPTIMIZATION (old_opts));
9087
9088 /* Parse options, and update the vector. */
9089 parse_optimize_options (args, true);
9090 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9091 = build_optimization_node (&global_options);
9092
9093 /* Restore current options. */
9094 cl_optimization_restore (&global_options, &cur_opts);
9095 }
9096
9097 return NULL_TREE;
9098 }
9099
9100 /* Handle a "no_split_stack" attribute. */
9101
9102 static tree
9103 handle_no_split_stack_attribute (tree *node, tree name,
9104 tree ARG_UNUSED (args),
9105 int ARG_UNUSED (flags),
9106 bool *no_add_attrs)
9107 {
9108 tree decl = *node;
9109
9110 if (TREE_CODE (decl) != FUNCTION_DECL)
9111 {
9112 error_at (DECL_SOURCE_LOCATION (decl),
9113 "%qE attribute applies only to functions", name);
9114 *no_add_attrs = true;
9115 }
9116 else if (DECL_INITIAL (decl))
9117 {
9118 error_at (DECL_SOURCE_LOCATION (decl),
9119 "can%'t set %qE attribute after definition", name);
9120 *no_add_attrs = true;
9121 }
9122
9123 return NULL_TREE;
9124 }
9125
9126 /* Handle a "returns_nonnull" attribute; arguments as in
9127 struct attribute_spec.handler. */
9128
9129 static tree
9130 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9131 bool *no_add_attrs)
9132 {
9133 // Even without a prototype we still have a return type we can check.
9134 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9135 {
9136 error ("returns_nonnull attribute on a function not returning a pointer");
9137 *no_add_attrs = true;
9138 }
9139 return NULL_TREE;
9140 }
9141
9142 \f
9143 /* Check for valid arguments being passed to a function with FNTYPE.
9144 There are NARGS arguments in the array ARGARRAY. */
9145 void
9146 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9147 {
9148 /* Check for null being passed in a pointer argument that must be
9149 non-null. We also need to do this if format checking is enabled. */
9150
9151 if (warn_nonnull)
9152 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9153
9154 /* Check for errors in format strings. */
9155
9156 if (warn_format || warn_suggest_attribute_format)
9157 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9158
9159 if (warn_format)
9160 check_function_sentinel (fntype, nargs, argarray);
9161 }
9162
9163 /* Generic argument checking recursion routine. PARAM is the argument to
9164 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9165 once the argument is resolved. CTX is context for the callback. */
9166 void
9167 check_function_arguments_recurse (void (*callback)
9168 (void *, tree, unsigned HOST_WIDE_INT),
9169 void *ctx, tree param,
9170 unsigned HOST_WIDE_INT param_num)
9171 {
9172 if (CONVERT_EXPR_P (param)
9173 && (TYPE_PRECISION (TREE_TYPE (param))
9174 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9175 {
9176 /* Strip coercion. */
9177 check_function_arguments_recurse (callback, ctx,
9178 TREE_OPERAND (param, 0), param_num);
9179 return;
9180 }
9181
9182 if (TREE_CODE (param) == CALL_EXPR)
9183 {
9184 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9185 tree attrs;
9186 bool found_format_arg = false;
9187
9188 /* See if this is a call to a known internationalization function
9189 that modifies a format arg. Such a function may have multiple
9190 format_arg attributes (for example, ngettext). */
9191
9192 for (attrs = TYPE_ATTRIBUTES (type);
9193 attrs;
9194 attrs = TREE_CHAIN (attrs))
9195 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9196 {
9197 tree inner_arg;
9198 tree format_num_expr;
9199 int format_num;
9200 int i;
9201 call_expr_arg_iterator iter;
9202
9203 /* Extract the argument number, which was previously checked
9204 to be valid. */
9205 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9206
9207 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9208 && !TREE_INT_CST_HIGH (format_num_expr));
9209
9210 format_num = TREE_INT_CST_LOW (format_num_expr);
9211
9212 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9213 inner_arg != 0;
9214 inner_arg = next_call_expr_arg (&iter), i++)
9215 if (i == format_num)
9216 {
9217 check_function_arguments_recurse (callback, ctx,
9218 inner_arg, param_num);
9219 found_format_arg = true;
9220 break;
9221 }
9222 }
9223
9224 /* If we found a format_arg attribute and did a recursive check,
9225 we are done with checking this argument. Otherwise, we continue
9226 and this will be considered a non-literal. */
9227 if (found_format_arg)
9228 return;
9229 }
9230
9231 if (TREE_CODE (param) == COND_EXPR)
9232 {
9233 /* Check both halves of the conditional expression. */
9234 check_function_arguments_recurse (callback, ctx,
9235 TREE_OPERAND (param, 1), param_num);
9236 check_function_arguments_recurse (callback, ctx,
9237 TREE_OPERAND (param, 2), param_num);
9238 return;
9239 }
9240
9241 (*callback) (ctx, param, param_num);
9242 }
9243
9244 /* Checks for a builtin function FNDECL that the number of arguments
9245 NARGS against the required number REQUIRED and issues an error if
9246 there is a mismatch. Returns true if the number of arguments is
9247 correct, otherwise false. */
9248
9249 static bool
9250 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9251 {
9252 if (nargs < required)
9253 {
9254 error_at (input_location,
9255 "not enough arguments to function %qE", fndecl);
9256 return false;
9257 }
9258 else if (nargs > required)
9259 {
9260 error_at (input_location,
9261 "too many arguments to function %qE", fndecl);
9262 return false;
9263 }
9264 return true;
9265 }
9266
9267 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9268 Returns false if there was an error, otherwise true. */
9269
9270 bool
9271 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9272 {
9273 if (!DECL_BUILT_IN (fndecl)
9274 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9275 return true;
9276
9277 switch (DECL_FUNCTION_CODE (fndecl))
9278 {
9279 case BUILT_IN_CONSTANT_P:
9280 return builtin_function_validate_nargs (fndecl, nargs, 1);
9281
9282 case BUILT_IN_ISFINITE:
9283 case BUILT_IN_ISINF:
9284 case BUILT_IN_ISINF_SIGN:
9285 case BUILT_IN_ISNAN:
9286 case BUILT_IN_ISNORMAL:
9287 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9288 {
9289 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9290 {
9291 error ("non-floating-point argument in call to "
9292 "function %qE", fndecl);
9293 return false;
9294 }
9295 return true;
9296 }
9297 return false;
9298
9299 case BUILT_IN_ISGREATER:
9300 case BUILT_IN_ISGREATEREQUAL:
9301 case BUILT_IN_ISLESS:
9302 case BUILT_IN_ISLESSEQUAL:
9303 case BUILT_IN_ISLESSGREATER:
9304 case BUILT_IN_ISUNORDERED:
9305 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9306 {
9307 enum tree_code code0, code1;
9308 code0 = TREE_CODE (TREE_TYPE (args[0]));
9309 code1 = TREE_CODE (TREE_TYPE (args[1]));
9310 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9311 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9312 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9313 {
9314 error ("non-floating-point arguments in call to "
9315 "function %qE", fndecl);
9316 return false;
9317 }
9318 return true;
9319 }
9320 return false;
9321
9322 case BUILT_IN_FPCLASSIFY:
9323 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9324 {
9325 unsigned i;
9326
9327 for (i=0; i<5; i++)
9328 if (TREE_CODE (args[i]) != INTEGER_CST)
9329 {
9330 error ("non-const integer argument %u in call to function %qE",
9331 i+1, fndecl);
9332 return false;
9333 }
9334
9335 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9336 {
9337 error ("non-floating-point argument in call to function %qE",
9338 fndecl);
9339 return false;
9340 }
9341 return true;
9342 }
9343 return false;
9344
9345 case BUILT_IN_ASSUME_ALIGNED:
9346 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9347 {
9348 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9349 {
9350 error ("non-integer argument 3 in call to function %qE", fndecl);
9351 return false;
9352 }
9353 return true;
9354 }
9355 return false;
9356
9357 default:
9358 return true;
9359 }
9360 }
9361
9362 /* Function to help qsort sort FIELD_DECLs by name order. */
9363
9364 int
9365 field_decl_cmp (const void *x_p, const void *y_p)
9366 {
9367 const tree *const x = (const tree *const) x_p;
9368 const tree *const y = (const tree *const) y_p;
9369
9370 if (DECL_NAME (*x) == DECL_NAME (*y))
9371 /* A nontype is "greater" than a type. */
9372 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9373 if (DECL_NAME (*x) == NULL_TREE)
9374 return -1;
9375 if (DECL_NAME (*y) == NULL_TREE)
9376 return 1;
9377 if (DECL_NAME (*x) < DECL_NAME (*y))
9378 return -1;
9379 return 1;
9380 }
9381
9382 static struct {
9383 gt_pointer_operator new_value;
9384 void *cookie;
9385 } resort_data;
9386
9387 /* This routine compares two fields like field_decl_cmp but using the
9388 pointer operator in resort_data. */
9389
9390 static int
9391 resort_field_decl_cmp (const void *x_p, const void *y_p)
9392 {
9393 const tree *const x = (const tree *const) x_p;
9394 const tree *const y = (const tree *const) y_p;
9395
9396 if (DECL_NAME (*x) == DECL_NAME (*y))
9397 /* A nontype is "greater" than a type. */
9398 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9399 if (DECL_NAME (*x) == NULL_TREE)
9400 return -1;
9401 if (DECL_NAME (*y) == NULL_TREE)
9402 return 1;
9403 {
9404 tree d1 = DECL_NAME (*x);
9405 tree d2 = DECL_NAME (*y);
9406 resort_data.new_value (&d1, resort_data.cookie);
9407 resort_data.new_value (&d2, resort_data.cookie);
9408 if (d1 < d2)
9409 return -1;
9410 }
9411 return 1;
9412 }
9413
9414 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9415
9416 void
9417 resort_sorted_fields (void *obj,
9418 void * ARG_UNUSED (orig_obj),
9419 gt_pointer_operator new_value,
9420 void *cookie)
9421 {
9422 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9423 resort_data.new_value = new_value;
9424 resort_data.cookie = cookie;
9425 qsort (&sf->elts[0], sf->len, sizeof (tree),
9426 resort_field_decl_cmp);
9427 }
9428
9429 /* Subroutine of c_parse_error.
9430 Return the result of concatenating LHS and RHS. RHS is really
9431 a string literal, its first character is indicated by RHS_START and
9432 RHS_SIZE is its length (including the terminating NUL character).
9433
9434 The caller is responsible for deleting the returned pointer. */
9435
9436 static char *
9437 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9438 {
9439 const int lhs_size = strlen (lhs);
9440 char *result = XNEWVEC (char, lhs_size + rhs_size);
9441 strncpy (result, lhs, lhs_size);
9442 strncpy (result + lhs_size, rhs_start, rhs_size);
9443 return result;
9444 }
9445
9446 /* Issue the error given by GMSGID, indicating that it occurred before
9447 TOKEN, which had the associated VALUE. */
9448
9449 void
9450 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9451 tree value, unsigned char token_flags)
9452 {
9453 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9454
9455 char *message = NULL;
9456
9457 if (token_type == CPP_EOF)
9458 message = catenate_messages (gmsgid, " at end of input");
9459 else if (token_type == CPP_CHAR
9460 || token_type == CPP_WCHAR
9461 || token_type == CPP_CHAR16
9462 || token_type == CPP_CHAR32)
9463 {
9464 unsigned int val = TREE_INT_CST_LOW (value);
9465 const char *prefix;
9466
9467 switch (token_type)
9468 {
9469 default:
9470 prefix = "";
9471 break;
9472 case CPP_WCHAR:
9473 prefix = "L";
9474 break;
9475 case CPP_CHAR16:
9476 prefix = "u";
9477 break;
9478 case CPP_CHAR32:
9479 prefix = "U";
9480 break;
9481 }
9482
9483 if (val <= UCHAR_MAX && ISGRAPH (val))
9484 message = catenate_messages (gmsgid, " before %s'%c'");
9485 else
9486 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9487
9488 error (message, prefix, val);
9489 free (message);
9490 message = NULL;
9491 }
9492 else if (token_type == CPP_CHAR_USERDEF
9493 || token_type == CPP_WCHAR_USERDEF
9494 || token_type == CPP_CHAR16_USERDEF
9495 || token_type == CPP_CHAR32_USERDEF)
9496 message = catenate_messages (gmsgid,
9497 " before user-defined character literal");
9498 else if (token_type == CPP_STRING_USERDEF
9499 || token_type == CPP_WSTRING_USERDEF
9500 || token_type == CPP_STRING16_USERDEF
9501 || token_type == CPP_STRING32_USERDEF
9502 || token_type == CPP_UTF8STRING_USERDEF)
9503 message = catenate_messages (gmsgid, " before user-defined string literal");
9504 else if (token_type == CPP_STRING
9505 || token_type == CPP_WSTRING
9506 || token_type == CPP_STRING16
9507 || token_type == CPP_STRING32
9508 || token_type == CPP_UTF8STRING)
9509 message = catenate_messages (gmsgid, " before string constant");
9510 else if (token_type == CPP_NUMBER)
9511 message = catenate_messages (gmsgid, " before numeric constant");
9512 else if (token_type == CPP_NAME)
9513 {
9514 message = catenate_messages (gmsgid, " before %qE");
9515 error (message, value);
9516 free (message);
9517 message = NULL;
9518 }
9519 else if (token_type == CPP_PRAGMA)
9520 message = catenate_messages (gmsgid, " before %<#pragma%>");
9521 else if (token_type == CPP_PRAGMA_EOL)
9522 message = catenate_messages (gmsgid, " before end of line");
9523 else if (token_type == CPP_DECLTYPE)
9524 message = catenate_messages (gmsgid, " before %<decltype%>");
9525 else if (token_type < N_TTYPES)
9526 {
9527 message = catenate_messages (gmsgid, " before %qs token");
9528 error (message, cpp_type2name (token_type, token_flags));
9529 free (message);
9530 message = NULL;
9531 }
9532 else
9533 error (gmsgid);
9534
9535 if (message)
9536 {
9537 error (message);
9538 free (message);
9539 }
9540 #undef catenate_messages
9541 }
9542
9543 /* Mapping for cpp message reasons to the options that enable them. */
9544
9545 struct reason_option_codes_t
9546 {
9547 const int reason; /* cpplib message reason. */
9548 const int option_code; /* gcc option that controls this message. */
9549 };
9550
9551 static const struct reason_option_codes_t option_codes[] = {
9552 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9553 {CPP_W_COMMENTS, OPT_Wcomment},
9554 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9555 {CPP_W_MULTICHAR, OPT_Wmultichar},
9556 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9557 {CPP_W_LONG_LONG, OPT_Wlong_long},
9558 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9559 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9560 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9561 {CPP_W_UNDEF, OPT_Wundef},
9562 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9563 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9564 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9565 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9566 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9567 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9568 {CPP_W_DATE_TIME, OPT_Wdate_time},
9569 {CPP_W_NONE, 0}
9570 };
9571
9572 /* Return the gcc option code associated with the reason for a cpp
9573 message, or 0 if none. */
9574
9575 static int
9576 c_option_controlling_cpp_error (int reason)
9577 {
9578 const struct reason_option_codes_t *entry;
9579
9580 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9581 {
9582 if (entry->reason == reason)
9583 return entry->option_code;
9584 }
9585 return 0;
9586 }
9587
9588 /* Callback from cpp_error for PFILE to print diagnostics from the
9589 preprocessor. The diagnostic is of type LEVEL, with REASON set
9590 to the reason code if LEVEL is represents a warning, at location
9591 LOCATION unless this is after lexing and the compiler's location
9592 should be used instead, with column number possibly overridden by
9593 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9594 the arguments. Returns true if a diagnostic was emitted, false
9595 otherwise. */
9596
9597 bool
9598 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9599 location_t location, unsigned int column_override,
9600 const char *msg, va_list *ap)
9601 {
9602 diagnostic_info diagnostic;
9603 diagnostic_t dlevel;
9604 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9605 bool ret;
9606
9607 switch (level)
9608 {
9609 case CPP_DL_WARNING_SYSHDR:
9610 if (flag_no_output)
9611 return false;
9612 global_dc->dc_warn_system_headers = 1;
9613 /* Fall through. */
9614 case CPP_DL_WARNING:
9615 if (flag_no_output)
9616 return false;
9617 dlevel = DK_WARNING;
9618 break;
9619 case CPP_DL_PEDWARN:
9620 if (flag_no_output && !flag_pedantic_errors)
9621 return false;
9622 dlevel = DK_PEDWARN;
9623 break;
9624 case CPP_DL_ERROR:
9625 dlevel = DK_ERROR;
9626 break;
9627 case CPP_DL_ICE:
9628 dlevel = DK_ICE;
9629 break;
9630 case CPP_DL_NOTE:
9631 dlevel = DK_NOTE;
9632 break;
9633 case CPP_DL_FATAL:
9634 dlevel = DK_FATAL;
9635 break;
9636 default:
9637 gcc_unreachable ();
9638 }
9639 if (done_lexing)
9640 location = input_location;
9641 diagnostic_set_info_translated (&diagnostic, msg, ap,
9642 location, dlevel);
9643 if (column_override)
9644 diagnostic_override_column (&diagnostic, column_override);
9645 diagnostic_override_option_index (&diagnostic,
9646 c_option_controlling_cpp_error (reason));
9647 ret = report_diagnostic (&diagnostic);
9648 if (level == CPP_DL_WARNING_SYSHDR)
9649 global_dc->dc_warn_system_headers = save_warn_system_headers;
9650 return ret;
9651 }
9652
9653 /* Convert a character from the host to the target execution character
9654 set. cpplib handles this, mostly. */
9655
9656 HOST_WIDE_INT
9657 c_common_to_target_charset (HOST_WIDE_INT c)
9658 {
9659 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9660 zero-extended under -fno-signed-char. cpplib insists that characters
9661 and character constants are always unsigned. Hence we must convert
9662 back and forth. */
9663 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9664
9665 uc = cpp_host_to_exec_charset (parse_in, uc);
9666
9667 if (flag_signed_char)
9668 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9669 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9670 else
9671 return uc;
9672 }
9673
9674 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9675 references with an INDIRECT_REF of a constant at the bottom; much like the
9676 traditional rendering of offsetof as a macro. Return the folded result. */
9677
9678 tree
9679 fold_offsetof_1 (tree expr)
9680 {
9681 tree base, off, t;
9682
9683 switch (TREE_CODE (expr))
9684 {
9685 case ERROR_MARK:
9686 return expr;
9687
9688 case VAR_DECL:
9689 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9690 return error_mark_node;
9691
9692 case CALL_EXPR:
9693 case TARGET_EXPR:
9694 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9695 return error_mark_node;
9696
9697 case NOP_EXPR:
9698 case INDIRECT_REF:
9699 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9700 {
9701 error ("cannot apply %<offsetof%> to a non constant address");
9702 return error_mark_node;
9703 }
9704 return TREE_OPERAND (expr, 0);
9705
9706 case COMPONENT_REF:
9707 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9708 if (base == error_mark_node)
9709 return base;
9710
9711 t = TREE_OPERAND (expr, 1);
9712 if (DECL_C_BIT_FIELD (t))
9713 {
9714 error ("attempt to take address of bit-field structure "
9715 "member %qD", t);
9716 return error_mark_node;
9717 }
9718 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9719 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9720 1)
9721 / BITS_PER_UNIT));
9722 break;
9723
9724 case ARRAY_REF:
9725 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9726 if (base == error_mark_node)
9727 return base;
9728
9729 t = TREE_OPERAND (expr, 1);
9730
9731 /* Check if the offset goes beyond the upper bound of the array. */
9732 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9733 {
9734 tree upbound = array_ref_up_bound (expr);
9735 if (upbound != NULL_TREE
9736 && TREE_CODE (upbound) == INTEGER_CST
9737 && !tree_int_cst_equal (upbound,
9738 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9739 {
9740 upbound = size_binop (PLUS_EXPR, upbound,
9741 build_int_cst (TREE_TYPE (upbound), 1));
9742 if (tree_int_cst_lt (upbound, t))
9743 {
9744 tree v;
9745
9746 for (v = TREE_OPERAND (expr, 0);
9747 TREE_CODE (v) == COMPONENT_REF;
9748 v = TREE_OPERAND (v, 0))
9749 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9750 == RECORD_TYPE)
9751 {
9752 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9753 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9754 if (TREE_CODE (fld_chain) == FIELD_DECL)
9755 break;
9756
9757 if (fld_chain)
9758 break;
9759 }
9760 /* Don't warn if the array might be considered a poor
9761 man's flexible array member with a very permissive
9762 definition thereof. */
9763 if (TREE_CODE (v) == ARRAY_REF
9764 || TREE_CODE (v) == COMPONENT_REF)
9765 warning (OPT_Warray_bounds,
9766 "index %E denotes an offset "
9767 "greater than size of %qT",
9768 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9769 }
9770 }
9771 }
9772
9773 t = convert (sizetype, t);
9774 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9775 break;
9776
9777 case COMPOUND_EXPR:
9778 /* Handle static members of volatile structs. */
9779 t = TREE_OPERAND (expr, 1);
9780 gcc_assert (TREE_CODE (t) == VAR_DECL);
9781 return fold_offsetof_1 (t);
9782
9783 default:
9784 gcc_unreachable ();
9785 }
9786
9787 return fold_build_pointer_plus (base, off);
9788 }
9789
9790 /* Likewise, but convert it to the return type of offsetof. */
9791
9792 tree
9793 fold_offsetof (tree expr)
9794 {
9795 return convert (size_type_node, fold_offsetof_1 (expr));
9796 }
9797
9798 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9799 expression, because B will always be true. */
9800
9801 void
9802 warn_for_omitted_condop (location_t location, tree cond)
9803 {
9804 if (truth_value_p (TREE_CODE (cond)))
9805 warning_at (location, OPT_Wparentheses,
9806 "the omitted middle operand in ?: will always be %<true%>, "
9807 "suggest explicit middle operand");
9808 }
9809
9810 /* Give an error for storing into ARG, which is 'const'. USE indicates
9811 how ARG was being used. */
9812
9813 void
9814 readonly_error (tree arg, enum lvalue_use use)
9815 {
9816 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9817 || use == lv_asm);
9818 /* Using this macro rather than (for example) arrays of messages
9819 ensures that all the format strings are checked at compile
9820 time. */
9821 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9822 : (use == lv_increment ? (I) \
9823 : (use == lv_decrement ? (D) : (AS))))
9824 if (TREE_CODE (arg) == COMPONENT_REF)
9825 {
9826 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9827 error (READONLY_MSG (G_("assignment of member "
9828 "%qD in read-only object"),
9829 G_("increment of member "
9830 "%qD in read-only object"),
9831 G_("decrement of member "
9832 "%qD in read-only object"),
9833 G_("member %qD in read-only object "
9834 "used as %<asm%> output")),
9835 TREE_OPERAND (arg, 1));
9836 else
9837 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9838 G_("increment of read-only member %qD"),
9839 G_("decrement of read-only member %qD"),
9840 G_("read-only member %qD used as %<asm%> output")),
9841 TREE_OPERAND (arg, 1));
9842 }
9843 else if (TREE_CODE (arg) == VAR_DECL)
9844 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9845 G_("increment of read-only variable %qD"),
9846 G_("decrement of read-only variable %qD"),
9847 G_("read-only variable %qD used as %<asm%> output")),
9848 arg);
9849 else if (TREE_CODE (arg) == PARM_DECL)
9850 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9851 G_("increment of read-only parameter %qD"),
9852 G_("decrement of read-only parameter %qD"),
9853 G_("read-only parameter %qD use as %<asm%> output")),
9854 arg);
9855 else if (TREE_CODE (arg) == RESULT_DECL)
9856 {
9857 gcc_assert (c_dialect_cxx ());
9858 error (READONLY_MSG (G_("assignment of "
9859 "read-only named return value %qD"),
9860 G_("increment of "
9861 "read-only named return value %qD"),
9862 G_("decrement of "
9863 "read-only named return value %qD"),
9864 G_("read-only named return value %qD "
9865 "used as %<asm%>output")),
9866 arg);
9867 }
9868 else if (TREE_CODE (arg) == FUNCTION_DECL)
9869 error (READONLY_MSG (G_("assignment of function %qD"),
9870 G_("increment of function %qD"),
9871 G_("decrement of function %qD"),
9872 G_("function %qD used as %<asm%> output")),
9873 arg);
9874 else
9875 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9876 G_("increment of read-only location %qE"),
9877 G_("decrement of read-only location %qE"),
9878 G_("read-only location %qE used as %<asm%> output")),
9879 arg);
9880 }
9881
9882 /* Print an error message for an invalid lvalue. USE says
9883 how the lvalue is being used and so selects the error message. LOC
9884 is the location for the error. */
9885
9886 void
9887 lvalue_error (location_t loc, enum lvalue_use use)
9888 {
9889 switch (use)
9890 {
9891 case lv_assign:
9892 error_at (loc, "lvalue required as left operand of assignment");
9893 break;
9894 case lv_increment:
9895 error_at (loc, "lvalue required as increment operand");
9896 break;
9897 case lv_decrement:
9898 error_at (loc, "lvalue required as decrement operand");
9899 break;
9900 case lv_addressof:
9901 error_at (loc, "lvalue required as unary %<&%> operand");
9902 break;
9903 case lv_asm:
9904 error_at (loc, "lvalue required in asm statement");
9905 break;
9906 default:
9907 gcc_unreachable ();
9908 }
9909 }
9910
9911 /* Print an error message for an invalid indirection of type TYPE.
9912 ERRSTRING is the name of the operator for the indirection. */
9913
9914 void
9915 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9916 {
9917 switch (errstring)
9918 {
9919 case RO_NULL:
9920 gcc_assert (c_dialect_cxx ());
9921 error_at (loc, "invalid type argument (have %qT)", type);
9922 break;
9923 case RO_ARRAY_INDEXING:
9924 error_at (loc,
9925 "invalid type argument of array indexing (have %qT)",
9926 type);
9927 break;
9928 case RO_UNARY_STAR:
9929 error_at (loc,
9930 "invalid type argument of unary %<*%> (have %qT)",
9931 type);
9932 break;
9933 case RO_ARROW:
9934 error_at (loc,
9935 "invalid type argument of %<->%> (have %qT)",
9936 type);
9937 break;
9938 case RO_ARROW_STAR:
9939 error_at (loc,
9940 "invalid type argument of %<->*%> (have %qT)",
9941 type);
9942 break;
9943 case RO_IMPLICIT_CONVERSION:
9944 error_at (loc,
9945 "invalid type argument of implicit conversion (have %qT)",
9946 type);
9947 break;
9948 default:
9949 gcc_unreachable ();
9950 }
9951 }
9952 \f
9953 /* *PTYPE is an incomplete array. Complete it with a domain based on
9954 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9955 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9956 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9957
9958 int
9959 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9960 {
9961 tree maxindex, type, main_type, elt, unqual_elt;
9962 int failure = 0, quals;
9963 hashval_t hashcode = 0;
9964 bool overflow_p = false;
9965
9966 maxindex = size_zero_node;
9967 if (initial_value)
9968 {
9969 if (TREE_CODE (initial_value) == STRING_CST)
9970 {
9971 int eltsize
9972 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9973 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9974 }
9975 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9976 {
9977 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9978
9979 if (vec_safe_is_empty (v))
9980 {
9981 if (pedantic)
9982 failure = 3;
9983 maxindex = ssize_int (-1);
9984 }
9985 else
9986 {
9987 tree curindex;
9988 unsigned HOST_WIDE_INT cnt;
9989 constructor_elt *ce;
9990 bool fold_p = false;
9991
9992 if ((*v)[0].index)
9993 maxindex = (*v)[0].index, fold_p = true;
9994
9995 curindex = maxindex;
9996
9997 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
9998 {
9999 bool curfold_p = false;
10000 if (ce->index)
10001 curindex = ce->index, curfold_p = true;
10002 else
10003 {
10004 if (fold_p)
10005 {
10006 /* Since we treat size types now as ordinary
10007 unsigned types, we need an explicit overflow
10008 check. */
10009 tree orig = curindex;
10010 curindex = fold_convert (sizetype, curindex);
10011 overflow_p |= tree_int_cst_lt (curindex, orig);
10012 }
10013 curindex = size_binop (PLUS_EXPR, curindex,
10014 size_one_node);
10015 }
10016 if (tree_int_cst_lt (maxindex, curindex))
10017 maxindex = curindex, fold_p = curfold_p;
10018 }
10019 if (fold_p)
10020 {
10021 tree orig = maxindex;
10022 maxindex = fold_convert (sizetype, maxindex);
10023 overflow_p |= tree_int_cst_lt (maxindex, orig);
10024 }
10025 }
10026 }
10027 else
10028 {
10029 /* Make an error message unless that happened already. */
10030 if (initial_value != error_mark_node)
10031 failure = 1;
10032 }
10033 }
10034 else
10035 {
10036 failure = 2;
10037 if (!do_default)
10038 return failure;
10039 }
10040
10041 type = *ptype;
10042 elt = TREE_TYPE (type);
10043 quals = TYPE_QUALS (strip_array_types (elt));
10044 if (quals == 0)
10045 unqual_elt = elt;
10046 else
10047 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10048
10049 /* Using build_distinct_type_copy and modifying things afterward instead
10050 of using build_array_type to create a new type preserves all of the
10051 TYPE_LANG_FLAG_? bits that the front end may have set. */
10052 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10053 TREE_TYPE (main_type) = unqual_elt;
10054 TYPE_DOMAIN (main_type)
10055 = build_range_type (TREE_TYPE (maxindex),
10056 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10057 layout_type (main_type);
10058
10059 /* Make sure we have the canonical MAIN_TYPE. */
10060 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10061 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10062 hashcode);
10063 main_type = type_hash_canon (hashcode, main_type);
10064
10065 /* Fix the canonical type. */
10066 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10067 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10068 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10069 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10070 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10071 != TYPE_DOMAIN (main_type)))
10072 TYPE_CANONICAL (main_type)
10073 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10074 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10075 else
10076 TYPE_CANONICAL (main_type) = main_type;
10077
10078 if (quals == 0)
10079 type = main_type;
10080 else
10081 type = c_build_qualified_type (main_type, quals);
10082
10083 if (COMPLETE_TYPE_P (type)
10084 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10085 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10086 {
10087 error ("size of array is too large");
10088 /* If we proceed with the array type as it is, we'll eventually
10089 crash in tree_low_cst(). */
10090 type = error_mark_node;
10091 }
10092
10093 *ptype = type;
10094 return failure;
10095 }
10096
10097 /* Like c_mark_addressable but don't check register qualifier. */
10098 void
10099 c_common_mark_addressable_vec (tree t)
10100 {
10101 while (handled_component_p (t))
10102 t = TREE_OPERAND (t, 0);
10103 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10104 return;
10105 TREE_ADDRESSABLE (t) = 1;
10106 }
10107
10108
10109 \f
10110 /* Used to help initialize the builtin-types.def table. When a type of
10111 the correct size doesn't exist, use error_mark_node instead of NULL.
10112 The later results in segfaults even when a decl using the type doesn't
10113 get invoked. */
10114
10115 tree
10116 builtin_type_for_size (int size, bool unsignedp)
10117 {
10118 tree type = c_common_type_for_size (size, unsignedp);
10119 return type ? type : error_mark_node;
10120 }
10121
10122 /* A helper function for resolve_overloaded_builtin in resolving the
10123 overloaded __sync_ builtins. Returns a positive power of 2 if the
10124 first operand of PARAMS is a pointer to a supported data type.
10125 Returns 0 if an error is encountered. */
10126
10127 static int
10128 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10129 {
10130 tree type;
10131 int size;
10132
10133 if (!params)
10134 {
10135 error ("too few arguments to function %qE", function);
10136 return 0;
10137 }
10138
10139 type = TREE_TYPE ((*params)[0]);
10140 if (TREE_CODE (type) != POINTER_TYPE)
10141 goto incompatible;
10142
10143 type = TREE_TYPE (type);
10144 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10145 goto incompatible;
10146
10147 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10148 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10149 return size;
10150
10151 incompatible:
10152 error ("incompatible type for argument %d of %qE", 1, function);
10153 return 0;
10154 }
10155
10156 /* A helper function for resolve_overloaded_builtin. Adds casts to
10157 PARAMS to make arguments match up with those of FUNCTION. Drops
10158 the variadic arguments at the end. Returns false if some error
10159 was encountered; true on success. */
10160
10161 static bool
10162 sync_resolve_params (location_t loc, tree orig_function, tree function,
10163 vec<tree, va_gc> *params, bool orig_format)
10164 {
10165 function_args_iterator iter;
10166 tree ptype;
10167 unsigned int parmnum;
10168
10169 function_args_iter_init (&iter, TREE_TYPE (function));
10170 /* We've declared the implementation functions to use "volatile void *"
10171 as the pointer parameter, so we shouldn't get any complaints from the
10172 call to check_function_arguments what ever type the user used. */
10173 function_args_iter_next (&iter);
10174 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10175
10176 /* For the rest of the values, we need to cast these to FTYPE, so that we
10177 don't get warnings for passing pointer types, etc. */
10178 parmnum = 0;
10179 while (1)
10180 {
10181 tree val, arg_type;
10182
10183 arg_type = function_args_iter_cond (&iter);
10184 /* XXX void_type_node belies the abstraction. */
10185 if (arg_type == void_type_node)
10186 break;
10187
10188 ++parmnum;
10189 if (params->length () <= parmnum)
10190 {
10191 error_at (loc, "too few arguments to function %qE", orig_function);
10192 return false;
10193 }
10194
10195 /* Only convert parameters if arg_type is unsigned integer type with
10196 new format sync routines, i.e. don't attempt to convert pointer
10197 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10198 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10199 kinds). */
10200 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10201 {
10202 /* Ideally for the first conversion we'd use convert_for_assignment
10203 so that we get warnings for anything that doesn't match the pointer
10204 type. This isn't portable across the C and C++ front ends atm. */
10205 val = (*params)[parmnum];
10206 val = convert (ptype, val);
10207 val = convert (arg_type, val);
10208 (*params)[parmnum] = val;
10209 }
10210
10211 function_args_iter_next (&iter);
10212 }
10213
10214 /* __atomic routines are not variadic. */
10215 if (!orig_format && params->length () != parmnum + 1)
10216 {
10217 error_at (loc, "too many arguments to function %qE", orig_function);
10218 return false;
10219 }
10220
10221 /* The definition of these primitives is variadic, with the remaining
10222 being "an optional list of variables protected by the memory barrier".
10223 No clue what that's supposed to mean, precisely, but we consider all
10224 call-clobbered variables to be protected so we're safe. */
10225 params->truncate (parmnum + 1);
10226
10227 return true;
10228 }
10229
10230 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10231 RESULT to make it match the type of the first pointer argument in
10232 PARAMS. */
10233
10234 static tree
10235 sync_resolve_return (tree first_param, tree result, bool orig_format)
10236 {
10237 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10238 tree rtype = TREE_TYPE (result);
10239 ptype = TYPE_MAIN_VARIANT (ptype);
10240
10241 /* New format doesn't require casting unless the types are the same size. */
10242 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10243 return convert (ptype, result);
10244 else
10245 return result;
10246 }
10247
10248 /* This function verifies the PARAMS to generic atomic FUNCTION.
10249 It returns the size if all the parameters are the same size, otherwise
10250 0 is returned if the parameters are invalid. */
10251
10252 static int
10253 get_atomic_generic_size (location_t loc, tree function,
10254 vec<tree, va_gc> *params)
10255 {
10256 unsigned int n_param;
10257 unsigned int n_model;
10258 unsigned int x;
10259 int size_0;
10260 tree type_0;
10261
10262 /* Determine the parameter makeup. */
10263 switch (DECL_FUNCTION_CODE (function))
10264 {
10265 case BUILT_IN_ATOMIC_EXCHANGE:
10266 n_param = 4;
10267 n_model = 1;
10268 break;
10269 case BUILT_IN_ATOMIC_LOAD:
10270 case BUILT_IN_ATOMIC_STORE:
10271 n_param = 3;
10272 n_model = 1;
10273 break;
10274 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10275 n_param = 6;
10276 n_model = 2;
10277 break;
10278 default:
10279 gcc_unreachable ();
10280 }
10281
10282 if (vec_safe_length (params) != n_param)
10283 {
10284 error_at (loc, "incorrect number of arguments to function %qE", function);
10285 return 0;
10286 }
10287
10288 /* Get type of first parameter, and determine its size. */
10289 type_0 = TREE_TYPE ((*params)[0]);
10290 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10291 {
10292 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10293 function);
10294 return 0;
10295 }
10296
10297 /* Types must be compile time constant sizes. */
10298 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10299 {
10300 error_at (loc,
10301 "argument 1 of %qE must be a pointer to a constant size type",
10302 function);
10303 return 0;
10304 }
10305
10306 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10307
10308 /* Zero size objects are not allowed. */
10309 if (size_0 == 0)
10310 {
10311 error_at (loc,
10312 "argument 1 of %qE must be a pointer to a nonzero size object",
10313 function);
10314 return 0;
10315 }
10316
10317 /* Check each other parameter is a pointer and the same size. */
10318 for (x = 0; x < n_param - n_model; x++)
10319 {
10320 int size;
10321 tree type = TREE_TYPE ((*params)[x]);
10322 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10323 if (n_param == 6 && x == 3)
10324 continue;
10325 if (!POINTER_TYPE_P (type))
10326 {
10327 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10328 function);
10329 return 0;
10330 }
10331 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10332 if (size != size_0)
10333 {
10334 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10335 function);
10336 return 0;
10337 }
10338 }
10339
10340 /* Check memory model parameters for validity. */
10341 for (x = n_param - n_model ; x < n_param; x++)
10342 {
10343 tree p = (*params)[x];
10344 if (TREE_CODE (p) == INTEGER_CST)
10345 {
10346 int i = tree_low_cst (p, 1);
10347 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10348 {
10349 warning_at (loc, OPT_Winvalid_memory_model,
10350 "invalid memory model argument %d of %qE", x + 1,
10351 function);
10352 }
10353 }
10354 else
10355 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10356 {
10357 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10358 function);
10359 return 0;
10360 }
10361 }
10362
10363 return size_0;
10364 }
10365
10366
10367 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10368 at the beginning of the parameter list PARAMS representing the size of the
10369 objects. This is to match the library ABI requirement. LOC is the location
10370 of the function call.
10371 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10372 returned to allow the external call to be constructed. */
10373
10374 static tree
10375 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10376 vec<tree, va_gc> *params)
10377 {
10378 tree size_node;
10379
10380 /* Insert a SIZE_T parameter as the first param. If there isn't
10381 enough space, allocate a new vector and recursively re-build with that. */
10382 if (!params->space (1))
10383 {
10384 unsigned int z, len;
10385 vec<tree, va_gc> *v;
10386 tree f;
10387
10388 len = params->length ();
10389 vec_alloc (v, len + 1);
10390 for (z = 0; z < len; z++)
10391 v->quick_push ((*params)[z]);
10392 f = build_function_call_vec (loc, function, v, NULL);
10393 vec_free (v);
10394 return f;
10395 }
10396
10397 /* Add the size parameter and leave as a function call for processing. */
10398 size_node = build_int_cst (size_type_node, n);
10399 params->quick_insert (0, size_node);
10400 return NULL_TREE;
10401 }
10402
10403
10404 /* This will process an __atomic_exchange function call, determine whether it
10405 needs to be mapped to the _N variation, or turned into a library call.
10406 LOC is the location of the builtin call.
10407 FUNCTION is the DECL that has been invoked;
10408 PARAMS is the argument list for the call. The return value is non-null
10409 TRUE is returned if it is translated into the proper format for a call to the
10410 external library, and NEW_RETURN is set the tree for that function.
10411 FALSE is returned if processing for the _N variation is required, and
10412 NEW_RETURN is set to the the return value the result is copied into. */
10413 static bool
10414 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10415 vec<tree, va_gc> *params, tree *new_return)
10416 {
10417 tree p0, p1, p2, p3;
10418 tree I_type, I_type_ptr;
10419 int n = get_atomic_generic_size (loc, function, params);
10420
10421 /* Size of 0 is an error condition. */
10422 if (n == 0)
10423 {
10424 *new_return = error_mark_node;
10425 return true;
10426 }
10427
10428 /* If not a lock-free size, change to the library generic format. */
10429 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10430 {
10431 *new_return = add_atomic_size_parameter (n, loc, function, params);
10432 return true;
10433 }
10434
10435 /* Otherwise there is a lockfree match, transform the call from:
10436 void fn(T* mem, T* desired, T* return, model)
10437 into
10438 *return = (T) (fn (In* mem, (In) *desired, model)) */
10439
10440 p0 = (*params)[0];
10441 p1 = (*params)[1];
10442 p2 = (*params)[2];
10443 p3 = (*params)[3];
10444
10445 /* Create pointer to appropriate size. */
10446 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10447 I_type_ptr = build_pointer_type (I_type);
10448
10449 /* Convert object pointer to required type. */
10450 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10451 (*params)[0] = p0;
10452 /* Convert new value to required type, and dereference it. */
10453 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10454 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10455 (*params)[1] = p1;
10456
10457 /* Move memory model to the 3rd position, and end param list. */
10458 (*params)[2] = p3;
10459 params->truncate (3);
10460
10461 /* Convert return pointer and dereference it for later assignment. */
10462 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10463
10464 return false;
10465 }
10466
10467
10468 /* This will process an __atomic_compare_exchange function call, determine
10469 whether it needs to be mapped to the _N variation, or turned into a lib call.
10470 LOC is the location of the builtin call.
10471 FUNCTION is the DECL that has been invoked;
10472 PARAMS is the argument list for the call. The return value is non-null
10473 TRUE is returned if it is translated into the proper format for a call to the
10474 external library, and NEW_RETURN is set the tree for that function.
10475 FALSE is returned if processing for the _N variation is required. */
10476
10477 static bool
10478 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10479 vec<tree, va_gc> *params,
10480 tree *new_return)
10481 {
10482 tree p0, p1, p2;
10483 tree I_type, I_type_ptr;
10484 int n = get_atomic_generic_size (loc, function, params);
10485
10486 /* Size of 0 is an error condition. */
10487 if (n == 0)
10488 {
10489 *new_return = error_mark_node;
10490 return true;
10491 }
10492
10493 /* If not a lock-free size, change to the library generic format. */
10494 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10495 {
10496 /* The library generic format does not have the weak parameter, so
10497 remove it from the param list. Since a parameter has been removed,
10498 we can be sure that there is room for the SIZE_T parameter, meaning
10499 there will not be a recursive rebuilding of the parameter list, so
10500 there is no danger this will be done twice. */
10501 if (n > 0)
10502 {
10503 (*params)[3] = (*params)[4];
10504 (*params)[4] = (*params)[5];
10505 params->truncate (5);
10506 }
10507 *new_return = add_atomic_size_parameter (n, loc, function, params);
10508 return true;
10509 }
10510
10511 /* Otherwise, there is a match, so the call needs to be transformed from:
10512 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10513 into
10514 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10515
10516 p0 = (*params)[0];
10517 p1 = (*params)[1];
10518 p2 = (*params)[2];
10519
10520 /* Create pointer to appropriate size. */
10521 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10522 I_type_ptr = build_pointer_type (I_type);
10523
10524 /* Convert object pointer to required type. */
10525 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10526 (*params)[0] = p0;
10527
10528 /* Convert expected pointer to required type. */
10529 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10530 (*params)[1] = p1;
10531
10532 /* Convert desired value to required type, and dereference it. */
10533 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10534 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10535 (*params)[2] = p2;
10536
10537 /* The rest of the parameters are fine. NULL means no special return value
10538 processing.*/
10539 *new_return = NULL;
10540 return false;
10541 }
10542
10543
10544 /* This will process an __atomic_load function call, determine whether it
10545 needs to be mapped to the _N variation, or turned into a library call.
10546 LOC is the location of the builtin call.
10547 FUNCTION is the DECL that has been invoked;
10548 PARAMS is the argument list for the call. The return value is non-null
10549 TRUE is returned if it is translated into the proper format for a call to the
10550 external library, and NEW_RETURN is set the tree for that function.
10551 FALSE is returned if processing for the _N variation is required, and
10552 NEW_RETURN is set to the the return value the result is copied into. */
10553
10554 static bool
10555 resolve_overloaded_atomic_load (location_t loc, tree function,
10556 vec<tree, va_gc> *params, tree *new_return)
10557 {
10558 tree p0, p1, p2;
10559 tree I_type, I_type_ptr;
10560 int n = get_atomic_generic_size (loc, function, params);
10561
10562 /* Size of 0 is an error condition. */
10563 if (n == 0)
10564 {
10565 *new_return = error_mark_node;
10566 return true;
10567 }
10568
10569 /* If not a lock-free size, change to the library generic format. */
10570 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10571 {
10572 *new_return = add_atomic_size_parameter (n, loc, function, params);
10573 return true;
10574 }
10575
10576 /* Otherwise, there is a match, so the call needs to be transformed from:
10577 void fn(T* mem, T* return, model)
10578 into
10579 *return = (T) (fn ((In *) mem, model)) */
10580
10581 p0 = (*params)[0];
10582 p1 = (*params)[1];
10583 p2 = (*params)[2];
10584
10585 /* Create pointer to appropriate size. */
10586 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10587 I_type_ptr = build_pointer_type (I_type);
10588
10589 /* Convert object pointer to required type. */
10590 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10591 (*params)[0] = p0;
10592
10593 /* Move memory model to the 2nd position, and end param list. */
10594 (*params)[1] = p2;
10595 params->truncate (2);
10596
10597 /* Convert return pointer and dereference it for later assignment. */
10598 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10599
10600 return false;
10601 }
10602
10603
10604 /* This will process an __atomic_store function call, determine whether it
10605 needs to be mapped to the _N variation, or turned into a library call.
10606 LOC is the location of the builtin call.
10607 FUNCTION is the DECL that has been invoked;
10608 PARAMS is the argument list for the call. The return value is non-null
10609 TRUE is returned if it is translated into the proper format for a call to the
10610 external library, and NEW_RETURN is set the tree for that function.
10611 FALSE is returned if processing for the _N variation is required, and
10612 NEW_RETURN is set to the the return value the result is copied into. */
10613
10614 static bool
10615 resolve_overloaded_atomic_store (location_t loc, tree function,
10616 vec<tree, va_gc> *params, tree *new_return)
10617 {
10618 tree p0, p1;
10619 tree I_type, I_type_ptr;
10620 int n = get_atomic_generic_size (loc, function, params);
10621
10622 /* Size of 0 is an error condition. */
10623 if (n == 0)
10624 {
10625 *new_return = error_mark_node;
10626 return true;
10627 }
10628
10629 /* If not a lock-free size, change to the library generic format. */
10630 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10631 {
10632 *new_return = add_atomic_size_parameter (n, loc, function, params);
10633 return true;
10634 }
10635
10636 /* Otherwise, there is a match, so the call needs to be transformed from:
10637 void fn(T* mem, T* value, model)
10638 into
10639 fn ((In *) mem, (In) *value, model) */
10640
10641 p0 = (*params)[0];
10642 p1 = (*params)[1];
10643
10644 /* Create pointer to appropriate size. */
10645 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10646 I_type_ptr = build_pointer_type (I_type);
10647
10648 /* Convert object pointer to required type. */
10649 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10650 (*params)[0] = p0;
10651
10652 /* Convert new value to required type, and dereference it. */
10653 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10654 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10655 (*params)[1] = p1;
10656
10657 /* The memory model is in the right spot already. Return is void. */
10658 *new_return = NULL_TREE;
10659
10660 return false;
10661 }
10662
10663
10664 /* Some builtin functions are placeholders for other expressions. This
10665 function should be called immediately after parsing the call expression
10666 before surrounding code has committed to the type of the expression.
10667
10668 LOC is the location of the builtin call.
10669
10670 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10671 PARAMS is the argument list for the call. The return value is non-null
10672 when expansion is complete, and null if normal processing should
10673 continue. */
10674
10675 tree
10676 resolve_overloaded_builtin (location_t loc, tree function,
10677 vec<tree, va_gc> *params)
10678 {
10679 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10680 bool orig_format = true;
10681 tree new_return = NULL_TREE;
10682
10683 switch (DECL_BUILT_IN_CLASS (function))
10684 {
10685 case BUILT_IN_NORMAL:
10686 break;
10687 case BUILT_IN_MD:
10688 if (targetm.resolve_overloaded_builtin)
10689 return targetm.resolve_overloaded_builtin (loc, function, params);
10690 else
10691 return NULL_TREE;
10692 default:
10693 return NULL_TREE;
10694 }
10695
10696 /* Handle BUILT_IN_NORMAL here. */
10697 switch (orig_code)
10698 {
10699 case BUILT_IN_ATOMIC_EXCHANGE:
10700 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10701 case BUILT_IN_ATOMIC_LOAD:
10702 case BUILT_IN_ATOMIC_STORE:
10703 {
10704 /* Handle these 4 together so that they can fall through to the next
10705 case if the call is transformed to an _N variant. */
10706 switch (orig_code)
10707 {
10708 case BUILT_IN_ATOMIC_EXCHANGE:
10709 {
10710 if (resolve_overloaded_atomic_exchange (loc, function, params,
10711 &new_return))
10712 return new_return;
10713 /* Change to the _N variant. */
10714 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10715 break;
10716 }
10717
10718 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10719 {
10720 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10721 params,
10722 &new_return))
10723 return new_return;
10724 /* Change to the _N variant. */
10725 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10726 break;
10727 }
10728 case BUILT_IN_ATOMIC_LOAD:
10729 {
10730 if (resolve_overloaded_atomic_load (loc, function, params,
10731 &new_return))
10732 return new_return;
10733 /* Change to the _N variant. */
10734 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10735 break;
10736 }
10737 case BUILT_IN_ATOMIC_STORE:
10738 {
10739 if (resolve_overloaded_atomic_store (loc, function, params,
10740 &new_return))
10741 return new_return;
10742 /* Change to the _N variant. */
10743 orig_code = BUILT_IN_ATOMIC_STORE_N;
10744 break;
10745 }
10746 default:
10747 gcc_unreachable ();
10748 }
10749 /* Fallthrough to the normal processing. */
10750 }
10751 case BUILT_IN_ATOMIC_EXCHANGE_N:
10752 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10753 case BUILT_IN_ATOMIC_LOAD_N:
10754 case BUILT_IN_ATOMIC_STORE_N:
10755 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10756 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10757 case BUILT_IN_ATOMIC_AND_FETCH_N:
10758 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10759 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10760 case BUILT_IN_ATOMIC_OR_FETCH_N:
10761 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10762 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10763 case BUILT_IN_ATOMIC_FETCH_AND_N:
10764 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10765 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10766 case BUILT_IN_ATOMIC_FETCH_OR_N:
10767 {
10768 orig_format = false;
10769 /* Fallthru for parameter processing. */
10770 }
10771 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10772 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10773 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10774 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10775 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10776 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10777 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10778 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10779 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10780 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10781 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10782 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10783 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10784 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10785 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10786 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10787 {
10788 int n = sync_resolve_size (function, params);
10789 tree new_function, first_param, result;
10790 enum built_in_function fncode;
10791
10792 if (n == 0)
10793 return error_mark_node;
10794
10795 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10796 new_function = builtin_decl_explicit (fncode);
10797 if (!sync_resolve_params (loc, function, new_function, params,
10798 orig_format))
10799 return error_mark_node;
10800
10801 first_param = (*params)[0];
10802 result = build_function_call_vec (loc, new_function, params, NULL);
10803 if (result == error_mark_node)
10804 return result;
10805 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10806 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10807 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10808 result = sync_resolve_return (first_param, result, orig_format);
10809
10810 /* If new_return is set, assign function to that expr and cast the
10811 result to void since the generic interface returned void. */
10812 if (new_return)
10813 {
10814 /* Cast function result from I{1,2,4,8,16} to the required type. */
10815 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10816 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10817 result);
10818 TREE_SIDE_EFFECTS (result) = 1;
10819 protected_set_expr_location (result, loc);
10820 result = convert (void_type_node, result);
10821 }
10822 return result;
10823 }
10824
10825 default:
10826 return NULL_TREE;
10827 }
10828 }
10829
10830 /* vector_types_compatible_elements_p is used in type checks of vectors
10831 values used as operands of binary operators. Where it returns true, and
10832 the other checks of the caller succeed (being vector types in he first
10833 place, and matching number of elements), we can just treat the types
10834 as essentially the same.
10835 Contrast with vector_targets_convertible_p, which is used for vector
10836 pointer types, and vector_types_convertible_p, which will allow
10837 language-specific matches under the control of flag_lax_vector_conversions,
10838 and might still require a conversion. */
10839 /* True if vector types T1 and T2 can be inputs to the same binary
10840 operator without conversion.
10841 We don't check the overall vector size here because some of our callers
10842 want to give different error messages when the vectors are compatible
10843 except for the element count. */
10844
10845 bool
10846 vector_types_compatible_elements_p (tree t1, tree t2)
10847 {
10848 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10849 t1 = TREE_TYPE (t1);
10850 t2 = TREE_TYPE (t2);
10851
10852 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10853
10854 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10855 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10856 || c2 == FIXED_POINT_TYPE));
10857
10858 t1 = c_common_signed_type (t1);
10859 t2 = c_common_signed_type (t2);
10860 /* Equality works here because c_common_signed_type uses
10861 TYPE_MAIN_VARIANT. */
10862 if (t1 == t2)
10863 return true;
10864 if (opaque && c1 == c2
10865 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10866 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10867 return true;
10868 return false;
10869 }
10870
10871 /* Check for missing format attributes on function pointers. LTYPE is
10872 the new type or left-hand side type. RTYPE is the old type or
10873 right-hand side type. Returns TRUE if LTYPE is missing the desired
10874 attribute. */
10875
10876 bool
10877 check_missing_format_attribute (tree ltype, tree rtype)
10878 {
10879 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10880 tree ra;
10881
10882 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10883 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10884 break;
10885 if (ra)
10886 {
10887 tree la;
10888 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10889 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10890 break;
10891 return !la;
10892 }
10893 else
10894 return false;
10895 }
10896
10897 /* Subscripting with type char is likely to lose on a machine where
10898 chars are signed. So warn on any machine, but optionally. Don't
10899 warn for unsigned char since that type is safe. Don't warn for
10900 signed char because anyone who uses that must have done so
10901 deliberately. Furthermore, we reduce the false positive load by
10902 warning only for non-constant value of type char. */
10903
10904 void
10905 warn_array_subscript_with_type_char (tree index)
10906 {
10907 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10908 && TREE_CODE (index) != INTEGER_CST)
10909 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10910 }
10911
10912 /* Implement -Wparentheses for the unexpected C precedence rules, to
10913 cover cases like x + y << z which readers are likely to
10914 misinterpret. We have seen an expression in which CODE is a binary
10915 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10916 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10917 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10918 expression was not formed using a binary or unary operator, or it
10919 was enclosed in parentheses. */
10920
10921 void
10922 warn_about_parentheses (location_t loc, enum tree_code code,
10923 enum tree_code code_left, tree arg_left,
10924 enum tree_code code_right, tree arg_right)
10925 {
10926 if (!warn_parentheses)
10927 return;
10928
10929 /* This macro tests that the expression ARG with original tree code
10930 CODE appears to be a boolean expression. or the result of folding a
10931 boolean expression. */
10932 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10933 (truth_value_p (TREE_CODE (ARG)) \
10934 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10935 /* Folding may create 0 or 1 integers from other expressions. */ \
10936 || ((CODE) != INTEGER_CST \
10937 && (integer_onep (ARG) || integer_zerop (ARG))))
10938
10939 switch (code)
10940 {
10941 case LSHIFT_EXPR:
10942 if (code_left == PLUS_EXPR)
10943 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10944 "suggest parentheses around %<+%> inside %<<<%>");
10945 else if (code_right == PLUS_EXPR)
10946 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10947 "suggest parentheses around %<+%> inside %<<<%>");
10948 else if (code_left == MINUS_EXPR)
10949 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10950 "suggest parentheses around %<-%> inside %<<<%>");
10951 else if (code_right == MINUS_EXPR)
10952 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10953 "suggest parentheses around %<-%> inside %<<<%>");
10954 return;
10955
10956 case RSHIFT_EXPR:
10957 if (code_left == PLUS_EXPR)
10958 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10959 "suggest parentheses around %<+%> inside %<>>%>");
10960 else if (code_right == PLUS_EXPR)
10961 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10962 "suggest parentheses around %<+%> inside %<>>%>");
10963 else if (code_left == MINUS_EXPR)
10964 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10965 "suggest parentheses around %<-%> inside %<>>%>");
10966 else if (code_right == MINUS_EXPR)
10967 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10968 "suggest parentheses around %<-%> inside %<>>%>");
10969 return;
10970
10971 case TRUTH_ORIF_EXPR:
10972 if (code_left == TRUTH_ANDIF_EXPR)
10973 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10974 "suggest parentheses around %<&&%> within %<||%>");
10975 else if (code_right == TRUTH_ANDIF_EXPR)
10976 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10977 "suggest parentheses around %<&&%> within %<||%>");
10978 return;
10979
10980 case BIT_IOR_EXPR:
10981 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10982 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10983 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10984 "suggest parentheses around arithmetic in operand of %<|%>");
10985 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10986 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10987 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10988 "suggest parentheses around arithmetic in operand of %<|%>");
10989 /* Check cases like x|y==z */
10990 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10991 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10992 "suggest parentheses around comparison in operand of %<|%>");
10993 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10994 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10995 "suggest parentheses around comparison in operand of %<|%>");
10996 /* Check cases like !x | y */
10997 else if (code_left == TRUTH_NOT_EXPR
10998 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10999 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11000 "suggest parentheses around operand of "
11001 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11002 return;
11003
11004 case BIT_XOR_EXPR:
11005 if (code_left == BIT_AND_EXPR
11006 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11007 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11008 "suggest parentheses around arithmetic in operand of %<^%>");
11009 else if (code_right == BIT_AND_EXPR
11010 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11011 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11012 "suggest parentheses around arithmetic in operand of %<^%>");
11013 /* Check cases like x^y==z */
11014 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11015 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11016 "suggest parentheses around comparison in operand of %<^%>");
11017 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11018 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11019 "suggest parentheses around comparison in operand of %<^%>");
11020 return;
11021
11022 case BIT_AND_EXPR:
11023 if (code_left == PLUS_EXPR)
11024 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11025 "suggest parentheses around %<+%> in operand of %<&%>");
11026 else if (code_right == PLUS_EXPR)
11027 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11028 "suggest parentheses around %<+%> in operand of %<&%>");
11029 else if (code_left == MINUS_EXPR)
11030 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11031 "suggest parentheses around %<-%> in operand of %<&%>");
11032 else if (code_right == MINUS_EXPR)
11033 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11034 "suggest parentheses around %<-%> in operand of %<&%>");
11035 /* Check cases like x&y==z */
11036 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11037 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11038 "suggest parentheses around comparison in operand of %<&%>");
11039 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11040 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11041 "suggest parentheses around comparison in operand of %<&%>");
11042 /* Check cases like !x & y */
11043 else if (code_left == TRUTH_NOT_EXPR
11044 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11045 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11046 "suggest parentheses around operand of "
11047 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11048 return;
11049
11050 case EQ_EXPR:
11051 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11052 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11053 "suggest parentheses around comparison in operand of %<==%>");
11054 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11055 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11056 "suggest parentheses around comparison in operand of %<==%>");
11057 return;
11058 case NE_EXPR:
11059 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11060 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11061 "suggest parentheses around comparison in operand of %<!=%>");
11062 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11063 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11064 "suggest parentheses around comparison in operand of %<!=%>");
11065 return;
11066
11067 default:
11068 if (TREE_CODE_CLASS (code) == tcc_comparison)
11069 {
11070 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11071 && code_left != NE_EXPR && code_left != EQ_EXPR
11072 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11073 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11074 "comparisons like %<X<=Y<=Z%> do not "
11075 "have their mathematical meaning");
11076 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11077 && code_right != NE_EXPR && code_right != EQ_EXPR
11078 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11079 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11080 "comparisons like %<X<=Y<=Z%> do not "
11081 "have their mathematical meaning");
11082 }
11083 return;
11084 }
11085 #undef NOT_A_BOOLEAN_EXPR_P
11086 }
11087
11088 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11089
11090 void
11091 warn_for_unused_label (tree label)
11092 {
11093 if (!TREE_USED (label))
11094 {
11095 if (DECL_INITIAL (label))
11096 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11097 else
11098 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11099 }
11100 }
11101
11102 /* Warn for division by zero according to the value of DIVISOR. LOC
11103 is the location of the division operator. */
11104
11105 void
11106 warn_for_div_by_zero (location_t loc, tree divisor)
11107 {
11108 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11109 about division by zero. Do not issue a warning if DIVISOR has a
11110 floating-point type, since we consider 0.0/0.0 a valid way of
11111 generating a NaN. */
11112 if (c_inhibit_evaluation_warnings == 0
11113 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11114 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11115 }
11116
11117 /* Subroutine of build_binary_op. Give warnings for comparisons
11118 between signed and unsigned quantities that may fail. Do the
11119 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11120 so that casts will be considered, but default promotions won't
11121 be.
11122
11123 LOCATION is the location of the comparison operator.
11124
11125 The arguments of this function map directly to local variables
11126 of build_binary_op. */
11127
11128 void
11129 warn_for_sign_compare (location_t location,
11130 tree orig_op0, tree orig_op1,
11131 tree op0, tree op1,
11132 tree result_type, enum tree_code resultcode)
11133 {
11134 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11135 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11136 int unsignedp0, unsignedp1;
11137
11138 /* In C++, check for comparison of different enum types. */
11139 if (c_dialect_cxx()
11140 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11141 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11142 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11143 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11144 {
11145 warning_at (location,
11146 OPT_Wsign_compare, "comparison between types %qT and %qT",
11147 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11148 }
11149
11150 /* Do not warn if the comparison is being done in a signed type,
11151 since the signed type will only be chosen if it can represent
11152 all the values of the unsigned type. */
11153 if (!TYPE_UNSIGNED (result_type))
11154 /* OK */;
11155 /* Do not warn if both operands are unsigned. */
11156 else if (op0_signed == op1_signed)
11157 /* OK */;
11158 else
11159 {
11160 tree sop, uop, base_type;
11161 bool ovf;
11162
11163 if (op0_signed)
11164 sop = orig_op0, uop = orig_op1;
11165 else
11166 sop = orig_op1, uop = orig_op0;
11167
11168 STRIP_TYPE_NOPS (sop);
11169 STRIP_TYPE_NOPS (uop);
11170 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11171 ? TREE_TYPE (result_type) : result_type);
11172
11173 /* Do not warn if the signed quantity is an unsuffixed integer
11174 literal (or some static constant expression involving such
11175 literals or a conditional expression involving such literals)
11176 and it is non-negative. */
11177 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11178 /* OK */;
11179 /* Do not warn if the comparison is an equality operation, the
11180 unsigned quantity is an integral constant, and it would fit
11181 in the result if the result were signed. */
11182 else if (TREE_CODE (uop) == INTEGER_CST
11183 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11184 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11185 /* OK */;
11186 /* In C, do not warn if the unsigned quantity is an enumeration
11187 constant and its maximum value would fit in the result if the
11188 result were signed. */
11189 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11190 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11191 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11192 c_common_signed_type (base_type)))
11193 /* OK */;
11194 else
11195 warning_at (location,
11196 OPT_Wsign_compare,
11197 "comparison between signed and unsigned integer expressions");
11198 }
11199
11200 /* Warn if two unsigned values are being compared in a size larger
11201 than their original size, and one (and only one) is the result of
11202 a `~' operator. This comparison will always fail.
11203
11204 Also warn if one operand is a constant, and the constant does not
11205 have all bits set that are set in the ~ operand when it is
11206 extended. */
11207
11208 op0 = c_common_get_narrower (op0, &unsignedp0);
11209 op1 = c_common_get_narrower (op1, &unsignedp1);
11210
11211 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11212 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11213 {
11214 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11215 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11216 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11217 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11218
11219 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11220 {
11221 tree primop;
11222 HOST_WIDE_INT constant, mask;
11223 int unsignedp;
11224 unsigned int bits;
11225
11226 if (host_integerp (op0, 0))
11227 {
11228 primop = op1;
11229 unsignedp = unsignedp1;
11230 constant = tree_low_cst (op0, 0);
11231 }
11232 else
11233 {
11234 primop = op0;
11235 unsignedp = unsignedp0;
11236 constant = tree_low_cst (op1, 0);
11237 }
11238
11239 bits = TYPE_PRECISION (TREE_TYPE (primop));
11240 if (bits < TYPE_PRECISION (result_type)
11241 && bits < HOST_BITS_PER_LONG && unsignedp)
11242 {
11243 mask = (~ (HOST_WIDE_INT) 0) << bits;
11244 if ((mask & constant) != mask)
11245 {
11246 if (constant == 0)
11247 warning (OPT_Wsign_compare,
11248 "promoted ~unsigned is always non-zero");
11249 else
11250 warning_at (location, OPT_Wsign_compare,
11251 "comparison of promoted ~unsigned with constant");
11252 }
11253 }
11254 }
11255 else if (unsignedp0 && unsignedp1
11256 && (TYPE_PRECISION (TREE_TYPE (op0))
11257 < TYPE_PRECISION (result_type))
11258 && (TYPE_PRECISION (TREE_TYPE (op1))
11259 < TYPE_PRECISION (result_type)))
11260 warning_at (location, OPT_Wsign_compare,
11261 "comparison of promoted ~unsigned with unsigned");
11262 }
11263 }
11264
11265 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11266 type via c_common_type. If -Wdouble-promotion is in use, and the
11267 conditions for warning have been met, issue a warning. GMSGID is
11268 the warning message. It must have two %T specifiers for the type
11269 that was converted (generally "float") and the type to which it was
11270 converted (generally "double), respectively. LOC is the location
11271 to which the awrning should refer. */
11272
11273 void
11274 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11275 const char *gmsgid, location_t loc)
11276 {
11277 tree source_type;
11278
11279 if (!warn_double_promotion)
11280 return;
11281 /* If the conversion will not occur at run-time, there is no need to
11282 warn about it. */
11283 if (c_inhibit_evaluation_warnings)
11284 return;
11285 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11286 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11287 return;
11288 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11289 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11290 source_type = type1;
11291 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11292 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11293 source_type = type2;
11294 else
11295 return;
11296 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11297 }
11298
11299 /* Setup a TYPE_DECL node as a typedef representation.
11300
11301 X is a TYPE_DECL for a typedef statement. Create a brand new
11302 ..._TYPE node (which will be just a variant of the existing
11303 ..._TYPE node with identical properties) and then install X
11304 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11305
11306 The whole point here is to end up with a situation where each
11307 and every ..._TYPE node the compiler creates will be uniquely
11308 associated with AT MOST one node representing a typedef name.
11309 This way, even though the compiler substitutes corresponding
11310 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11311 early on, later parts of the compiler can always do the reverse
11312 translation and get back the corresponding typedef name. For
11313 example, given:
11314
11315 typedef struct S MY_TYPE;
11316 MY_TYPE object;
11317
11318 Later parts of the compiler might only know that `object' was of
11319 type `struct S' if it were not for code just below. With this
11320 code however, later parts of the compiler see something like:
11321
11322 struct S' == struct S
11323 typedef struct S' MY_TYPE;
11324 struct S' object;
11325
11326 And they can then deduce (from the node for type struct S') that
11327 the original object declaration was:
11328
11329 MY_TYPE object;
11330
11331 Being able to do this is important for proper support of protoize,
11332 and also for generating precise symbolic debugging information
11333 which takes full account of the programmer's (typedef) vocabulary.
11334
11335 Obviously, we don't want to generate a duplicate ..._TYPE node if
11336 the TYPE_DECL node that we are now processing really represents a
11337 standard built-in type. */
11338
11339 void
11340 set_underlying_type (tree x)
11341 {
11342 if (x == error_mark_node)
11343 return;
11344 if (DECL_IS_BUILTIN (x))
11345 {
11346 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11347 TYPE_NAME (TREE_TYPE (x)) = x;
11348 }
11349 else if (TREE_TYPE (x) != error_mark_node
11350 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11351 {
11352 tree tt = TREE_TYPE (x);
11353 DECL_ORIGINAL_TYPE (x) = tt;
11354 tt = build_variant_type_copy (tt);
11355 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11356 TYPE_NAME (tt) = x;
11357 TREE_USED (tt) = TREE_USED (x);
11358 TREE_TYPE (x) = tt;
11359 }
11360 }
11361
11362 /* Record the types used by the current global variable declaration
11363 being parsed, so that we can decide later to emit their debug info.
11364 Those types are in types_used_by_cur_var_decl, and we are going to
11365 store them in the types_used_by_vars_hash hash table.
11366 DECL is the declaration of the global variable that has been parsed. */
11367
11368 void
11369 record_types_used_by_current_var_decl (tree decl)
11370 {
11371 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11372
11373 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11374 {
11375 tree type = types_used_by_cur_var_decl->pop ();
11376 types_used_by_var_decl_insert (type, decl);
11377 }
11378 }
11379
11380 /* If DECL is a typedef that is declared in the current function,
11381 record it for the purpose of -Wunused-local-typedefs. */
11382
11383 void
11384 record_locally_defined_typedef (tree decl)
11385 {
11386 struct c_language_function *l;
11387
11388 if (!warn_unused_local_typedefs
11389 || cfun == NULL
11390 /* if this is not a locally defined typedef then we are not
11391 interested. */
11392 || !is_typedef_decl (decl)
11393 || !decl_function_context (decl))
11394 return;
11395
11396 l = (struct c_language_function *) cfun->language;
11397 vec_safe_push (l->local_typedefs, decl);
11398 }
11399
11400 /* If T is a TYPE_DECL declared locally, mark it as used. */
11401
11402 void
11403 maybe_record_typedef_use (tree t)
11404 {
11405 if (!is_typedef_decl (t))
11406 return;
11407
11408 TREE_USED (t) = true;
11409 }
11410
11411 /* Warn if there are some unused locally defined typedefs in the
11412 current function. */
11413
11414 void
11415 maybe_warn_unused_local_typedefs (void)
11416 {
11417 int i;
11418 tree decl;
11419 /* The number of times we have emitted -Wunused-local-typedefs
11420 warnings. If this is different from errorcount, that means some
11421 unrelated errors have been issued. In which case, we'll avoid
11422 emitting "unused-local-typedefs" warnings. */
11423 static int unused_local_typedefs_warn_count;
11424 struct c_language_function *l;
11425
11426 if (cfun == NULL)
11427 return;
11428
11429 if ((l = (struct c_language_function *) cfun->language) == NULL)
11430 return;
11431
11432 if (warn_unused_local_typedefs
11433 && errorcount == unused_local_typedefs_warn_count)
11434 {
11435 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11436 if (!TREE_USED (decl))
11437 warning_at (DECL_SOURCE_LOCATION (decl),
11438 OPT_Wunused_local_typedefs,
11439 "typedef %qD locally defined but not used", decl);
11440 unused_local_typedefs_warn_count = errorcount;
11441 }
11442
11443 vec_free (l->local_typedefs);
11444 }
11445
11446 /* The C and C++ parsers both use vectors to hold function arguments.
11447 For efficiency, we keep a cache of unused vectors. This is the
11448 cache. */
11449
11450 typedef vec<tree, va_gc> *tree_gc_vec;
11451 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11452
11453 /* Return a new vector from the cache. If the cache is empty,
11454 allocate a new vector. These vectors are GC'ed, so it is OK if the
11455 pointer is not released.. */
11456
11457 vec<tree, va_gc> *
11458 make_tree_vector (void)
11459 {
11460 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11461 return tree_vector_cache->pop ();
11462 else
11463 {
11464 /* Passing 0 to vec::alloc returns NULL, and our callers require
11465 that we always return a non-NULL value. The vector code uses
11466 4 when growing a NULL vector, so we do too. */
11467 vec<tree, va_gc> *v;
11468 vec_alloc (v, 4);
11469 return v;
11470 }
11471 }
11472
11473 /* Release a vector of trees back to the cache. */
11474
11475 void
11476 release_tree_vector (vec<tree, va_gc> *vec)
11477 {
11478 if (vec != NULL)
11479 {
11480 vec->truncate (0);
11481 vec_safe_push (tree_vector_cache, vec);
11482 }
11483 }
11484
11485 /* Get a new tree vector holding a single tree. */
11486
11487 vec<tree, va_gc> *
11488 make_tree_vector_single (tree t)
11489 {
11490 vec<tree, va_gc> *ret = make_tree_vector ();
11491 ret->quick_push (t);
11492 return ret;
11493 }
11494
11495 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11496
11497 vec<tree, va_gc> *
11498 make_tree_vector_from_list (tree list)
11499 {
11500 vec<tree, va_gc> *ret = make_tree_vector ();
11501 for (; list; list = TREE_CHAIN (list))
11502 vec_safe_push (ret, TREE_VALUE (list));
11503 return ret;
11504 }
11505
11506 /* Get a new tree vector which is a copy of an existing one. */
11507
11508 vec<tree, va_gc> *
11509 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11510 {
11511 vec<tree, va_gc> *ret;
11512 unsigned int ix;
11513 tree t;
11514
11515 ret = make_tree_vector ();
11516 vec_safe_reserve (ret, vec_safe_length (orig));
11517 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11518 ret->quick_push (t);
11519 return ret;
11520 }
11521
11522 /* Return true if KEYWORD starts a type specifier. */
11523
11524 bool
11525 keyword_begins_type_specifier (enum rid keyword)
11526 {
11527 switch (keyword)
11528 {
11529 case RID_INT:
11530 case RID_CHAR:
11531 case RID_FLOAT:
11532 case RID_DOUBLE:
11533 case RID_VOID:
11534 case RID_INT128:
11535 case RID_UNSIGNED:
11536 case RID_LONG:
11537 case RID_SHORT:
11538 case RID_SIGNED:
11539 case RID_DFLOAT32:
11540 case RID_DFLOAT64:
11541 case RID_DFLOAT128:
11542 case RID_FRACT:
11543 case RID_ACCUM:
11544 case RID_BOOL:
11545 case RID_WCHAR:
11546 case RID_CHAR16:
11547 case RID_CHAR32:
11548 case RID_SAT:
11549 case RID_COMPLEX:
11550 case RID_TYPEOF:
11551 case RID_STRUCT:
11552 case RID_CLASS:
11553 case RID_UNION:
11554 case RID_ENUM:
11555 return true;
11556 default:
11557 return false;
11558 }
11559 }
11560
11561 /* Return true if KEYWORD names a type qualifier. */
11562
11563 bool
11564 keyword_is_type_qualifier (enum rid keyword)
11565 {
11566 switch (keyword)
11567 {
11568 case RID_CONST:
11569 case RID_VOLATILE:
11570 case RID_RESTRICT:
11571 return true;
11572 default:
11573 return false;
11574 }
11575 }
11576
11577 /* Return true if KEYWORD names a storage class specifier.
11578
11579 RID_TYPEDEF is not included in this list despite `typedef' being
11580 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11581 such for syntactic convenience only. */
11582
11583 bool
11584 keyword_is_storage_class_specifier (enum rid keyword)
11585 {
11586 switch (keyword)
11587 {
11588 case RID_STATIC:
11589 case RID_EXTERN:
11590 case RID_REGISTER:
11591 case RID_AUTO:
11592 case RID_MUTABLE:
11593 case RID_THREAD:
11594 return true;
11595 default:
11596 return false;
11597 }
11598 }
11599
11600 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11601
11602 static bool
11603 keyword_is_function_specifier (enum rid keyword)
11604 {
11605 switch (keyword)
11606 {
11607 case RID_INLINE:
11608 case RID_NORETURN:
11609 case RID_VIRTUAL:
11610 case RID_EXPLICIT:
11611 return true;
11612 default:
11613 return false;
11614 }
11615 }
11616
11617 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11618 declaration-specifier (C99 6.7). */
11619
11620 bool
11621 keyword_is_decl_specifier (enum rid keyword)
11622 {
11623 if (keyword_is_storage_class_specifier (keyword)
11624 || keyword_is_type_qualifier (keyword)
11625 || keyword_is_function_specifier (keyword))
11626 return true;
11627
11628 switch (keyword)
11629 {
11630 case RID_TYPEDEF:
11631 case RID_FRIEND:
11632 case RID_CONSTEXPR:
11633 return true;
11634 default:
11635 return false;
11636 }
11637 }
11638
11639 /* Initialize language-specific-bits of tree_contains_struct. */
11640
11641 void
11642 c_common_init_ts (void)
11643 {
11644 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11645 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11646 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11647 }
11648
11649 /* Build a user-defined numeric literal out of an integer constant type VALUE
11650 with identifier SUFFIX. */
11651
11652 tree
11653 build_userdef_literal (tree suffix_id, tree value,
11654 enum overflow_type overflow, tree num_string)
11655 {
11656 tree literal = make_node (USERDEF_LITERAL);
11657 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11658 USERDEF_LITERAL_VALUE (literal) = value;
11659 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11660 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11661 return literal;
11662 }
11663
11664 /* For vector[index], convert the vector to a
11665 pointer of the underlying type. */
11666 void
11667 convert_vector_to_pointer_for_subscript (location_t loc,
11668 tree* vecp, tree index)
11669 {
11670 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11671 {
11672 tree type = TREE_TYPE (*vecp);
11673 tree type1;
11674
11675 if (TREE_CODE (index) == INTEGER_CST)
11676 if (!host_integerp (index, 1)
11677 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11678 >= TYPE_VECTOR_SUBPARTS (type)))
11679 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11680
11681 c_common_mark_addressable_vec (*vecp);
11682 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11683 type = build_pointer_type (type);
11684 type1 = build_pointer_type (TREE_TYPE (*vecp));
11685 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11686 *vecp = convert (type, *vecp);
11687 }
11688 }
11689
11690 /* Determine which of the operands, if any, is a scalar that needs to be
11691 converted to a vector, for the range of operations. */
11692 enum stv_conv
11693 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11694 bool complain)
11695 {
11696 tree type0 = TREE_TYPE (op0);
11697 tree type1 = TREE_TYPE (op1);
11698 bool integer_only_op = false;
11699 enum stv_conv ret = stv_firstarg;
11700
11701 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11702 || TREE_CODE (type1) == VECTOR_TYPE);
11703 switch (code)
11704 {
11705 /* Most GENERIC binary expressions require homogeneous arguments.
11706 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11707 argument that is a vector and a second one that is a scalar, so
11708 we never return stv_secondarg for them. */
11709 case RSHIFT_EXPR:
11710 case LSHIFT_EXPR:
11711 if (TREE_CODE (type0) == INTEGER_TYPE
11712 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11713 {
11714 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11715 {
11716 if (complain)
11717 error_at (loc, "conversion of scalar %qT to vector %qT "
11718 "involves truncation", type0, type1);
11719 return stv_error;
11720 }
11721 else
11722 return stv_firstarg;
11723 }
11724 break;
11725
11726 case BIT_IOR_EXPR:
11727 case BIT_XOR_EXPR:
11728 case BIT_AND_EXPR:
11729 integer_only_op = true;
11730 /* ... fall through ... */
11731
11732 case VEC_COND_EXPR:
11733
11734 case PLUS_EXPR:
11735 case MINUS_EXPR:
11736 case MULT_EXPR:
11737 case TRUNC_DIV_EXPR:
11738 case CEIL_DIV_EXPR:
11739 case FLOOR_DIV_EXPR:
11740 case ROUND_DIV_EXPR:
11741 case EXACT_DIV_EXPR:
11742 case TRUNC_MOD_EXPR:
11743 case FLOOR_MOD_EXPR:
11744 case RDIV_EXPR:
11745 case EQ_EXPR:
11746 case NE_EXPR:
11747 case LE_EXPR:
11748 case GE_EXPR:
11749 case LT_EXPR:
11750 case GT_EXPR:
11751 /* What about UNLT_EXPR? */
11752 if (TREE_CODE (type0) == VECTOR_TYPE)
11753 {
11754 tree tmp;
11755 ret = stv_secondarg;
11756 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11757 tmp = type0; type0 = type1; type1 = tmp;
11758 tmp = op0; op0 = op1; op1 = tmp;
11759 }
11760
11761 if (TREE_CODE (type0) == INTEGER_TYPE
11762 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11763 {
11764 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11765 {
11766 if (complain)
11767 error_at (loc, "conversion of scalar %qT to vector %qT "
11768 "involves truncation", type0, type1);
11769 return stv_error;
11770 }
11771 return ret;
11772 }
11773 else if (!integer_only_op
11774 /* Allow integer --> real conversion if safe. */
11775 && (TREE_CODE (type0) == REAL_TYPE
11776 || TREE_CODE (type0) == INTEGER_TYPE)
11777 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11778 {
11779 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11780 {
11781 if (complain)
11782 error_at (loc, "conversion of scalar %qT to vector %qT "
11783 "involves truncation", type0, type1);
11784 return stv_error;
11785 }
11786 return ret;
11787 }
11788 default:
11789 break;
11790 }
11791
11792 return stv_nothing;
11793 }
11794
11795 /* Return true iff ALIGN is an integral constant that is a fundamental
11796 alignment, as defined by [basic.align] in the c++-11
11797 specifications.
11798
11799 That is:
11800
11801 [A fundamental alignment is represented by an alignment less than or
11802 equal to the greatest alignment supported by the implementation
11803 in all contexts, which is equal to
11804 alignof(max_align_t)]. */
11805
11806 bool
11807 cxx_fundamental_alignment_p (unsigned align)
11808 {
11809 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11810 TYPE_ALIGN (long_double_type_node)));
11811 }
11812
11813 #include "gt-c-family-c-common.h"