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