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