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