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