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