* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / c-family / c-common.c
blob39be9569395588996a147ccfc088a807726cb85e
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 "c-common.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "trans-mem.h"
34 #include "flags.h"
35 #include "c-pragma.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++98 is the default. */
245 enum cxx_dialect cxx_dialect = cxx98;
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 (location_t, 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 *);
383 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
385 static void check_function_nonnull (tree, int, tree *);
386 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
387 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
388 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
389 static int resort_field_decl_cmp (const void *, const void *);
391 /* Reserved words. The third field is a mask: keywords are disabled
392 if they match the mask.
394 Masks for languages:
395 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
396 C --std=c99: D_CXXONLY | D_OBJC
397 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
398 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
399 C++ --std=c0x: D_CONLY | D_OBJC
400 ObjC++ is like C++ except that D_OBJC is not set
402 If -fno-asm is used, D_ASM is added to the mask. If
403 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
404 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
405 In C with -Wc++-compat, we warn if D_CXXWARN is set.
407 Note the complication of the D_CXX_OBJC keywords. These are
408 reserved words such as 'class'. In C++, 'class' is a reserved
409 word. In Objective-C++ it is too. In Objective-C, it is a
410 reserved word too, but only if it follows an '@' sign.
412 const struct c_common_resword c_common_reswords[] =
414 { "_Alignas", RID_ALIGNAS, D_CONLY },
415 { "_Alignof", RID_ALIGNOF, D_CONLY },
416 { "_Atomic", RID_ATOMIC, D_CONLY },
417 { "_Bool", RID_BOOL, D_CONLY },
418 { "_Complex", RID_COMPLEX, 0 },
419 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
420 { "_Cilk_sync", RID_CILK_SYNC, 0 },
421 { "_Cilk_for", RID_CILK_FOR, 0 },
422 { "_Imaginary", RID_IMAGINARY, D_CONLY },
423 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
424 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
425 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
426 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
427 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
428 { "_Sat", RID_SAT, D_CONLY | D_EXT },
429 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
430 { "_Noreturn", RID_NORETURN, D_CONLY },
431 { "_Generic", RID_GENERIC, D_CONLY },
432 { "_Thread_local", RID_THREAD, D_CONLY },
433 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
434 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
435 { "__alignof", RID_ALIGNOF, 0 },
436 { "__alignof__", RID_ALIGNOF, 0 },
437 { "__asm", RID_ASM, 0 },
438 { "__asm__", RID_ASM, 0 },
439 { "__attribute", RID_ATTRIBUTE, 0 },
440 { "__attribute__", RID_ATTRIBUTE, 0 },
441 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
442 { "__bases", RID_BASES, D_CXXONLY },
443 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
444 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
445 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
446 { "__builtin_offsetof", RID_OFFSETOF, 0 },
447 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
448 { "__builtin_va_arg", RID_VA_ARG, 0 },
449 { "__complex", RID_COMPLEX, 0 },
450 { "__complex__", RID_COMPLEX, 0 },
451 { "__const", RID_CONST, 0 },
452 { "__const__", RID_CONST, 0 },
453 { "__decltype", RID_DECLTYPE, D_CXXONLY },
454 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
455 { "__extension__", RID_EXTENSION, 0 },
456 { "__func__", RID_C99_FUNCTION_NAME, 0 },
457 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
458 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
459 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
460 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
461 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
462 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
463 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
464 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
465 { "__imag", RID_IMAGPART, 0 },
466 { "__imag__", RID_IMAGPART, 0 },
467 { "__inline", RID_INLINE, 0 },
468 { "__inline__", RID_INLINE, 0 },
469 { "__int128", RID_INT128, 0 },
470 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
471 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
472 { "__is_class", RID_IS_CLASS, D_CXXONLY },
473 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
474 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
475 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
476 { "__is_final", RID_IS_FINAL, D_CXXONLY },
477 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
478 { "__is_pod", RID_IS_POD, D_CXXONLY },
479 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
480 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
481 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
482 { "__is_union", RID_IS_UNION, D_CXXONLY },
483 { "__label__", RID_LABEL, 0 },
484 { "__null", RID_NULL, 0 },
485 { "__real", RID_REALPART, 0 },
486 { "__real__", RID_REALPART, 0 },
487 { "__restrict", RID_RESTRICT, 0 },
488 { "__restrict__", RID_RESTRICT, 0 },
489 { "__signed", RID_SIGNED, 0 },
490 { "__signed__", RID_SIGNED, 0 },
491 { "__thread", RID_THREAD, 0 },
492 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
493 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
494 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
495 { "__typeof", RID_TYPEOF, 0 },
496 { "__typeof__", RID_TYPEOF, 0 },
497 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
498 { "__volatile", RID_VOLATILE, 0 },
499 { "__volatile__", RID_VOLATILE, 0 },
500 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
501 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
502 { "asm", RID_ASM, D_ASM },
503 { "auto", RID_AUTO, 0 },
504 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
505 { "break", RID_BREAK, 0 },
506 { "case", RID_CASE, 0 },
507 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
508 { "char", RID_CHAR, 0 },
509 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
510 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
511 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
512 { "const", RID_CONST, 0 },
513 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
514 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
515 { "continue", RID_CONTINUE, 0 },
516 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
517 { "default", RID_DEFAULT, 0 },
518 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
519 { "do", RID_DO, 0 },
520 { "double", RID_DOUBLE, 0 },
521 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
522 { "else", RID_ELSE, 0 },
523 { "enum", RID_ENUM, 0 },
524 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
525 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
526 { "extern", RID_EXTERN, 0 },
527 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
528 { "float", RID_FLOAT, 0 },
529 { "for", RID_FOR, 0 },
530 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
531 { "goto", RID_GOTO, 0 },
532 { "if", RID_IF, 0 },
533 { "inline", RID_INLINE, D_EXT89 },
534 { "int", RID_INT, 0 },
535 { "long", RID_LONG, 0 },
536 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
537 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
538 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
539 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
541 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
542 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
543 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
544 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
545 { "register", RID_REGISTER, 0 },
546 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
547 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
548 { "return", RID_RETURN, 0 },
549 { "short", RID_SHORT, 0 },
550 { "signed", RID_SIGNED, 0 },
551 { "sizeof", RID_SIZEOF, 0 },
552 { "static", RID_STATIC, 0 },
553 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
554 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
555 { "struct", RID_STRUCT, 0 },
556 { "switch", RID_SWITCH, 0 },
557 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
558 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
559 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
560 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
561 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
562 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
563 { "typedef", RID_TYPEDEF, 0 },
564 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
565 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
566 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
567 { "union", RID_UNION, 0 },
568 { "unsigned", RID_UNSIGNED, 0 },
569 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
570 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
571 { "void", RID_VOID, 0 },
572 { "volatile", RID_VOLATILE, 0 },
573 { "wchar_t", RID_WCHAR, D_CXXONLY },
574 { "while", RID_WHILE, 0 },
575 /* These Objective-C keywords are recognized only immediately after
576 an '@'. */
577 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
578 { "defs", RID_AT_DEFS, D_OBJC },
579 { "encode", RID_AT_ENCODE, D_OBJC },
580 { "end", RID_AT_END, D_OBJC },
581 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
582 { "interface", RID_AT_INTERFACE, D_OBJC },
583 { "protocol", RID_AT_PROTOCOL, D_OBJC },
584 { "selector", RID_AT_SELECTOR, D_OBJC },
585 { "finally", RID_AT_FINALLY, D_OBJC },
586 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
587 { "optional", RID_AT_OPTIONAL, D_OBJC },
588 { "required", RID_AT_REQUIRED, D_OBJC },
589 { "property", RID_AT_PROPERTY, D_OBJC },
590 { "package", RID_AT_PACKAGE, D_OBJC },
591 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
592 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
593 /* These are recognized only in protocol-qualifier context
594 (see above) */
595 { "bycopy", RID_BYCOPY, D_OBJC },
596 { "byref", RID_BYREF, D_OBJC },
597 { "in", RID_IN, D_OBJC },
598 { "inout", RID_INOUT, D_OBJC },
599 { "oneway", RID_ONEWAY, D_OBJC },
600 { "out", RID_OUT, D_OBJC },
601 /* These are recognized inside a property attribute list */
602 { "assign", RID_ASSIGN, D_OBJC },
603 { "copy", RID_COPY, D_OBJC },
604 { "getter", RID_GETTER, D_OBJC },
605 { "nonatomic", RID_NONATOMIC, D_OBJC },
606 { "readonly", RID_READONLY, D_OBJC },
607 { "readwrite", RID_READWRITE, D_OBJC },
608 { "retain", RID_RETAIN, D_OBJC },
609 { "setter", RID_SETTER, D_OBJC },
612 const unsigned int num_c_common_reswords =
613 sizeof c_common_reswords / sizeof (struct c_common_resword);
615 /* Table of machine-independent attributes common to all C-like languages. */
616 const struct attribute_spec c_common_attribute_table[] =
618 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
619 affects_type_identity } */
620 { "packed", 0, 0, false, false, false,
621 handle_packed_attribute , false},
622 { "nocommon", 0, 0, true, false, false,
623 handle_nocommon_attribute, false},
624 { "common", 0, 0, true, false, false,
625 handle_common_attribute, false },
626 /* FIXME: logically, noreturn attributes should be listed as
627 "false, true, true" and apply to function types. But implementing this
628 would require all the places in the compiler that use TREE_THIS_VOLATILE
629 on a decl to identify non-returning functions to be located and fixed
630 to check the function type instead. */
631 { "noreturn", 0, 0, true, false, false,
632 handle_noreturn_attribute, false },
633 { "volatile", 0, 0, true, false, false,
634 handle_noreturn_attribute, false },
635 { "noinline", 0, 0, true, false, false,
636 handle_noinline_attribute, false },
637 { "noclone", 0, 0, true, false, false,
638 handle_noclone_attribute, false },
639 { "leaf", 0, 0, true, false, false,
640 handle_leaf_attribute, false },
641 { "always_inline", 0, 0, true, false, false,
642 handle_always_inline_attribute, false },
643 { "gnu_inline", 0, 0, true, false, false,
644 handle_gnu_inline_attribute, false },
645 { "artificial", 0, 0, true, false, false,
646 handle_artificial_attribute, false },
647 { "flatten", 0, 0, true, false, false,
648 handle_flatten_attribute, false },
649 { "used", 0, 0, true, false, false,
650 handle_used_attribute, false },
651 { "unused", 0, 0, false, false, false,
652 handle_unused_attribute, false },
653 { "externally_visible", 0, 0, true, false, false,
654 handle_externally_visible_attribute, false },
655 /* The same comments as for noreturn attributes apply to const ones. */
656 { "const", 0, 0, true, false, false,
657 handle_const_attribute, false },
658 { "transparent_union", 0, 0, false, false, false,
659 handle_transparent_union_attribute, false },
660 { "constructor", 0, 1, true, false, false,
661 handle_constructor_attribute, false },
662 { "destructor", 0, 1, true, false, false,
663 handle_destructor_attribute, false },
664 { "mode", 1, 1, false, true, false,
665 handle_mode_attribute, false },
666 { "section", 1, 1, true, false, false,
667 handle_section_attribute, false },
668 { "aligned", 0, 1, false, false, false,
669 handle_aligned_attribute, false },
670 { "weak", 0, 0, true, false, false,
671 handle_weak_attribute, false },
672 { "ifunc", 1, 1, true, false, false,
673 handle_ifunc_attribute, false },
674 { "alias", 1, 1, true, false, false,
675 handle_alias_attribute, false },
676 { "weakref", 0, 1, true, false, false,
677 handle_weakref_attribute, false },
678 { "no_instrument_function", 0, 0, true, false, false,
679 handle_no_instrument_function_attribute,
680 false },
681 { "malloc", 0, 0, true, false, false,
682 handle_malloc_attribute, false },
683 { "returns_twice", 0, 0, true, false, false,
684 handle_returns_twice_attribute, false },
685 { "no_stack_limit", 0, 0, true, false, false,
686 handle_no_limit_stack_attribute, false },
687 { "pure", 0, 0, true, false, false,
688 handle_pure_attribute, false },
689 { "transaction_callable", 0, 0, false, true, false,
690 handle_tm_attribute, false },
691 { "transaction_unsafe", 0, 0, false, true, false,
692 handle_tm_attribute, false },
693 { "transaction_safe", 0, 0, false, true, false,
694 handle_tm_attribute, false },
695 { "transaction_may_cancel_outer", 0, 0, false, true, false,
696 handle_tm_attribute, false },
697 /* ??? These two attributes didn't make the transition from the
698 Intel language document to the multi-vendor language document. */
699 { "transaction_pure", 0, 0, false, true, false,
700 handle_tm_attribute, false },
701 { "transaction_wrap", 1, 1, true, false, false,
702 handle_tm_wrap_attribute, false },
703 /* For internal use (marking of builtins) only. The name contains space
704 to prevent its usage in source code. */
705 { "no vops", 0, 0, true, false, false,
706 handle_novops_attribute, false },
707 { "deprecated", 0, 1, false, false, false,
708 handle_deprecated_attribute, false },
709 { "vector_size", 1, 1, false, true, false,
710 handle_vector_size_attribute, false },
711 { "visibility", 1, 1, false, false, false,
712 handle_visibility_attribute, false },
713 { "tls_model", 1, 1, true, false, false,
714 handle_tls_model_attribute, false },
715 { "nonnull", 0, -1, false, true, true,
716 handle_nonnull_attribute, false },
717 { "nothrow", 0, 0, true, false, false,
718 handle_nothrow_attribute, false },
719 { "may_alias", 0, 0, false, true, false, NULL, false },
720 { "cleanup", 1, 1, true, false, false,
721 handle_cleanup_attribute, false },
722 { "warn_unused_result", 0, 0, false, true, true,
723 handle_warn_unused_result_attribute, false },
724 { "sentinel", 0, 1, false, true, true,
725 handle_sentinel_attribute, false },
726 /* For internal use (marking of builtins) only. The name contains space
727 to prevent its usage in source code. */
728 { "type generic", 0, 0, false, true, true,
729 handle_type_generic_attribute, false },
730 { "alloc_size", 1, 2, false, true, true,
731 handle_alloc_size_attribute, false },
732 { "cold", 0, 0, true, false, false,
733 handle_cold_attribute, false },
734 { "hot", 0, 0, true, false, false,
735 handle_hot_attribute, false },
736 { "no_address_safety_analysis",
737 0, 0, true, false, false,
738 handle_no_address_safety_analysis_attribute,
739 false },
740 { "no_sanitize_address", 0, 0, true, false, false,
741 handle_no_sanitize_address_attribute,
742 false },
743 { "no_sanitize_undefined", 0, 0, true, false, false,
744 handle_no_sanitize_undefined_attribute,
745 false },
746 { "warning", 1, 1, true, false, false,
747 handle_error_attribute, false },
748 { "error", 1, 1, true, false, false,
749 handle_error_attribute, false },
750 { "target", 1, -1, true, false, false,
751 handle_target_attribute, false },
752 { "optimize", 1, -1, true, false, false,
753 handle_optimize_attribute, false },
754 /* For internal use only. The leading '*' both prevents its usage in
755 source code and signals that it may be overridden by machine tables. */
756 { "*tm regparm", 0, 0, false, true, true,
757 ignore_attribute, false },
758 { "no_split_stack", 0, 0, true, false, false,
759 handle_no_split_stack_attribute, false },
760 /* For internal use (marking of builtins and runtime functions) only.
761 The name contains space to prevent its usage in source code. */
762 { "fn spec", 1, 1, false, true, true,
763 handle_fnspec_attribute, false },
764 { "warn_unused", 0, 0, false, false, false,
765 handle_warn_unused_attribute, false },
766 { "returns_nonnull", 0, 0, false, true, true,
767 handle_returns_nonnull_attribute, false },
768 { "omp declare simd", 0, -1, true, false, false,
769 handle_omp_declare_simd_attribute, false },
770 { "cilk simd function", 0, -1, true, false, false,
771 handle_omp_declare_simd_attribute, false },
772 { "omp declare target", 0, 0, true, false, false,
773 handle_omp_declare_target_attribute, false },
774 { "alloc_align", 1, 1, false, true, true,
775 handle_alloc_align_attribute, false },
776 { "assume_aligned", 1, 2, false, true, true,
777 handle_assume_aligned_attribute, false },
778 { "designated_init", 0, 0, false, true, false,
779 handle_designated_init_attribute, false },
780 { NULL, 0, 0, false, false, false, NULL, false }
783 /* Give the specifications for the format attributes, used by C and all
784 descendants. */
786 const struct attribute_spec c_common_format_attribute_table[] =
788 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
789 affects_type_identity } */
790 { "format", 3, 3, false, true, true,
791 handle_format_attribute, false },
792 { "format_arg", 1, 1, false, true, true,
793 handle_format_arg_attribute, false },
794 { NULL, 0, 0, false, false, false, NULL, false }
797 /* Return identifier for address space AS. */
799 const char *
800 c_addr_space_name (addr_space_t as)
802 int rid = RID_FIRST_ADDR_SPACE + as;
803 gcc_assert (ridpointers [rid]);
804 return IDENTIFIER_POINTER (ridpointers [rid]);
807 /* Push current bindings for the function name VAR_DECLS. */
809 void
810 start_fname_decls (void)
812 unsigned ix;
813 tree saved = NULL_TREE;
815 for (ix = 0; fname_vars[ix].decl; ix++)
817 tree decl = *fname_vars[ix].decl;
819 if (decl)
821 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
822 saved);
823 *fname_vars[ix].decl = NULL_TREE;
826 if (saved || saved_function_name_decls)
827 /* Normally they'll have been NULL, so only push if we've got a
828 stack, or they are non-NULL. */
829 saved_function_name_decls = tree_cons (saved, NULL_TREE,
830 saved_function_name_decls);
833 /* Finish up the current bindings, adding them into the current function's
834 statement tree. This must be done _before_ finish_stmt_tree is called.
835 If there is no current function, we must be at file scope and no statements
836 are involved. Pop the previous bindings. */
838 void
839 finish_fname_decls (void)
841 unsigned ix;
842 tree stmts = NULL_TREE;
843 tree stack = saved_function_name_decls;
845 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
846 append_to_statement_list (TREE_VALUE (stack), &stmts);
848 if (stmts)
850 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
852 if (TREE_CODE (*bodyp) == BIND_EXPR)
853 bodyp = &BIND_EXPR_BODY (*bodyp);
855 append_to_statement_list_force (*bodyp, &stmts);
856 *bodyp = stmts;
859 for (ix = 0; fname_vars[ix].decl; ix++)
860 *fname_vars[ix].decl = NULL_TREE;
862 if (stack)
864 /* We had saved values, restore them. */
865 tree saved;
867 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
869 tree decl = TREE_PURPOSE (saved);
870 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
872 *fname_vars[ix].decl = decl;
874 stack = TREE_CHAIN (stack);
876 saved_function_name_decls = stack;
879 /* Return the text name of the current function, suitably prettified
880 by PRETTY_P. Return string must be freed by caller. */
882 const char *
883 fname_as_string (int pretty_p)
885 const char *name = "top level";
886 char *namep;
887 int vrb = 2, len;
888 cpp_string cstr = { 0, 0 }, strname;
890 if (!pretty_p)
892 name = "";
893 vrb = 0;
896 if (current_function_decl)
897 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
899 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
901 namep = XNEWVEC (char, len);
902 snprintf (namep, len, "\"%s\"", name);
903 strname.text = (unsigned char *) namep;
904 strname.len = len - 1;
906 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
908 XDELETEVEC (namep);
909 return (const char *) cstr.text;
912 return namep;
915 /* Return the VAR_DECL for a const char array naming the current
916 function. If the VAR_DECL has not yet been created, create it
917 now. RID indicates how it should be formatted and IDENTIFIER_NODE
918 ID is its name (unfortunately C and C++ hold the RID values of
919 keywords in different places, so we can't derive RID from ID in
920 this language independent code. LOC is the location of the
921 function. */
923 tree
924 fname_decl (location_t loc, unsigned int rid, tree id)
926 unsigned ix;
927 tree decl = NULL_TREE;
929 for (ix = 0; fname_vars[ix].decl; ix++)
930 if (fname_vars[ix].rid == rid)
931 break;
933 decl = *fname_vars[ix].decl;
934 if (!decl)
936 /* If a tree is built here, it would normally have the lineno of
937 the current statement. Later this tree will be moved to the
938 beginning of the function and this line number will be wrong.
939 To avoid this problem set the lineno to 0 here; that prevents
940 it from appearing in the RTL. */
941 tree stmts;
942 location_t saved_location = input_location;
943 input_location = UNKNOWN_LOCATION;
945 stmts = push_stmt_list ();
946 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
947 stmts = pop_stmt_list (stmts);
948 if (!IS_EMPTY_STMT (stmts))
949 saved_function_name_decls
950 = tree_cons (decl, stmts, saved_function_name_decls);
951 *fname_vars[ix].decl = decl;
952 input_location = saved_location;
954 if (!ix && !current_function_decl)
955 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
957 return decl;
960 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
962 tree
963 fix_string_type (tree value)
965 int length = TREE_STRING_LENGTH (value);
966 int nchars;
967 tree e_type, i_type, a_type;
969 /* Compute the number of elements, for the array type. */
970 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
972 nchars = length;
973 e_type = char_type_node;
975 else if (TREE_TYPE (value) == char16_array_type_node)
977 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
978 e_type = char16_type_node;
980 else if (TREE_TYPE (value) == char32_array_type_node)
982 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
983 e_type = char32_type_node;
985 else
987 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
988 e_type = wchar_type_node;
991 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
992 limit in C++98 Annex B is very large (65536) and is not normative,
993 so we do not diagnose it (warn_overlength_strings is forced off
994 in c_common_post_options). */
995 if (warn_overlength_strings)
997 const int nchars_max = flag_isoc99 ? 4095 : 509;
998 const int relevant_std = flag_isoc99 ? 99 : 90;
999 if (nchars - 1 > nchars_max)
1000 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1001 separate the %d from the 'C'. 'ISO' should not be
1002 translated, but it may be moved after 'C%d' in languages
1003 where modifiers follow nouns. */
1004 pedwarn (input_location, OPT_Woverlength_strings,
1005 "string length %qd is greater than the length %qd "
1006 "ISO C%d compilers are required to support",
1007 nchars - 1, nchars_max, relevant_std);
1010 /* Create the array type for the string constant. The ISO C++
1011 standard says that a string literal has type `const char[N]' or
1012 `const wchar_t[N]'. We use the same logic when invoked as a C
1013 front-end with -Wwrite-strings.
1014 ??? We should change the type of an expression depending on the
1015 state of a warning flag. We should just be warning -- see how
1016 this is handled in the C++ front-end for the deprecated implicit
1017 conversion from string literals to `char*' or `wchar_t*'.
1019 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1020 array type being the unqualified version of that type.
1021 Therefore, if we are constructing an array of const char, we must
1022 construct the matching unqualified array type first. The C front
1023 end does not require this, but it does no harm, so we do it
1024 unconditionally. */
1025 i_type = build_index_type (size_int (nchars - 1));
1026 a_type = build_array_type (e_type, i_type);
1027 if (c_dialect_cxx() || warn_write_strings)
1028 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1030 TREE_TYPE (value) = a_type;
1031 TREE_CONSTANT (value) = 1;
1032 TREE_READONLY (value) = 1;
1033 TREE_STATIC (value) = 1;
1034 return value;
1037 /* If DISABLE is true, stop issuing warnings. This is used when
1038 parsing code that we know will not be executed. This function may
1039 be called multiple times, and works as a stack. */
1041 static void
1042 c_disable_warnings (bool disable)
1044 if (disable)
1046 ++c_inhibit_evaluation_warnings;
1047 fold_defer_overflow_warnings ();
1051 /* If ENABLE is true, reenable issuing warnings. */
1053 static void
1054 c_enable_warnings (bool enable)
1056 if (enable)
1058 --c_inhibit_evaluation_warnings;
1059 fold_undefer_and_ignore_overflow_warnings ();
1063 /* Fully fold EXPR, an expression that was not folded (beyond integer
1064 constant expressions and null pointer constants) when being built
1065 up. If IN_INIT, this is in a static initializer and certain
1066 changes are made to the folding done. Clear *MAYBE_CONST if
1067 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1068 expression because it contains an evaluated operator (in C99) or an
1069 operator outside of sizeof returning an integer constant (in C90)
1070 not permitted in constant expressions, or because it contains an
1071 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1072 set to true by callers before calling this function.) Return the
1073 folded expression. Function arguments have already been folded
1074 before calling this function, as have the contents of SAVE_EXPR,
1075 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1076 C_MAYBE_CONST_EXPR. */
1078 tree
1079 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1081 tree ret;
1082 tree eptype = NULL_TREE;
1083 bool dummy = true;
1084 bool maybe_const_itself = true;
1085 location_t loc = EXPR_LOCATION (expr);
1087 /* This function is not relevant to C++ because C++ folds while
1088 parsing, and may need changes to be correct for C++ when C++
1089 stops folding while parsing. */
1090 if (c_dialect_cxx ())
1091 gcc_unreachable ();
1093 if (!maybe_const)
1094 maybe_const = &dummy;
1095 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1097 eptype = TREE_TYPE (expr);
1098 expr = TREE_OPERAND (expr, 0);
1100 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1101 &maybe_const_itself);
1102 if (eptype)
1103 ret = fold_convert_loc (loc, eptype, ret);
1104 *maybe_const &= maybe_const_itself;
1105 return ret;
1108 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1109 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1110 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1111 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1112 both evaluated and unevaluated subexpressions while
1113 *MAYBE_CONST_ITSELF is carried from only evaluated
1114 subexpressions). */
1116 static tree
1117 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1118 bool *maybe_const_itself)
1120 tree ret = expr;
1121 enum tree_code code = TREE_CODE (expr);
1122 enum tree_code_class kind = TREE_CODE_CLASS (code);
1123 location_t loc = EXPR_LOCATION (expr);
1124 tree op0, op1, op2, op3;
1125 tree orig_op0, orig_op1, orig_op2;
1126 bool op0_const = true, op1_const = true, op2_const = true;
1127 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1128 bool nowarning = TREE_NO_WARNING (expr);
1129 bool unused_p;
1131 /* This function is not relevant to C++ because C++ folds while
1132 parsing, and may need changes to be correct for C++ when C++
1133 stops folding while parsing. */
1134 if (c_dialect_cxx ())
1135 gcc_unreachable ();
1137 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1138 anything else not counted as an expression cannot usefully be
1139 folded further at this point. */
1140 if (!IS_EXPR_CODE_CLASS (kind)
1141 || kind == tcc_statement
1142 || code == SAVE_EXPR)
1143 return expr;
1145 /* Operands of variable-length expressions (function calls) have
1146 already been folded, as have __builtin_* function calls, and such
1147 expressions cannot occur in constant expressions. */
1148 if (kind == tcc_vl_exp)
1150 *maybe_const_operands = false;
1151 ret = fold (expr);
1152 goto out;
1155 if (code == C_MAYBE_CONST_EXPR)
1157 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1158 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1159 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1160 *maybe_const_operands = false;
1161 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1162 *maybe_const_itself = false;
1163 if (pre && !in_init)
1164 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1165 else
1166 ret = inner;
1167 goto out;
1170 /* Assignment, increment, decrement, function call and comma
1171 operators, and statement expressions, cannot occur in constant
1172 expressions if evaluated / outside of sizeof. (Function calls
1173 were handled above, though VA_ARG_EXPR is treated like a function
1174 call here, and statement expressions are handled through
1175 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1176 switch (code)
1178 case MODIFY_EXPR:
1179 case PREDECREMENT_EXPR:
1180 case PREINCREMENT_EXPR:
1181 case POSTDECREMENT_EXPR:
1182 case POSTINCREMENT_EXPR:
1183 case COMPOUND_EXPR:
1184 *maybe_const_operands = false;
1185 break;
1187 case VA_ARG_EXPR:
1188 case TARGET_EXPR:
1189 case BIND_EXPR:
1190 case OBJ_TYPE_REF:
1191 *maybe_const_operands = false;
1192 ret = fold (expr);
1193 goto out;
1195 default:
1196 break;
1199 /* Fold individual tree codes as appropriate. */
1200 switch (code)
1202 case COMPOUND_LITERAL_EXPR:
1203 /* Any non-constancy will have been marked in a containing
1204 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1205 goto out;
1207 case COMPONENT_REF:
1208 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1209 op1 = TREE_OPERAND (expr, 1);
1210 op2 = TREE_OPERAND (expr, 2);
1211 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1212 maybe_const_itself);
1213 STRIP_TYPE_NOPS (op0);
1214 if (op0 != orig_op0)
1215 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1216 if (ret != expr)
1218 TREE_READONLY (ret) = TREE_READONLY (expr);
1219 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1221 goto out;
1223 case ARRAY_REF:
1224 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1225 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1226 op2 = TREE_OPERAND (expr, 2);
1227 op3 = TREE_OPERAND (expr, 3);
1228 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1229 maybe_const_itself);
1230 STRIP_TYPE_NOPS (op0);
1231 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1232 maybe_const_itself);
1233 STRIP_TYPE_NOPS (op1);
1234 op1 = decl_constant_value_for_optimization (op1);
1235 if (op0 != orig_op0 || op1 != orig_op1)
1236 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1237 if (ret != expr)
1239 TREE_READONLY (ret) = TREE_READONLY (expr);
1240 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1241 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1243 ret = fold (ret);
1244 goto out;
1246 case COMPOUND_EXPR:
1247 case MODIFY_EXPR:
1248 case PREDECREMENT_EXPR:
1249 case PREINCREMENT_EXPR:
1250 case POSTDECREMENT_EXPR:
1251 case POSTINCREMENT_EXPR:
1252 case PLUS_EXPR:
1253 case MINUS_EXPR:
1254 case MULT_EXPR:
1255 case POINTER_PLUS_EXPR:
1256 case TRUNC_DIV_EXPR:
1257 case CEIL_DIV_EXPR:
1258 case FLOOR_DIV_EXPR:
1259 case TRUNC_MOD_EXPR:
1260 case RDIV_EXPR:
1261 case EXACT_DIV_EXPR:
1262 case LSHIFT_EXPR:
1263 case RSHIFT_EXPR:
1264 case BIT_IOR_EXPR:
1265 case BIT_XOR_EXPR:
1266 case BIT_AND_EXPR:
1267 case LT_EXPR:
1268 case LE_EXPR:
1269 case GT_EXPR:
1270 case GE_EXPR:
1271 case EQ_EXPR:
1272 case NE_EXPR:
1273 case COMPLEX_EXPR:
1274 case TRUTH_AND_EXPR:
1275 case TRUTH_OR_EXPR:
1276 case TRUTH_XOR_EXPR:
1277 case UNORDERED_EXPR:
1278 case ORDERED_EXPR:
1279 case UNLT_EXPR:
1280 case UNLE_EXPR:
1281 case UNGT_EXPR:
1282 case UNGE_EXPR:
1283 case UNEQ_EXPR:
1284 /* Binary operations evaluating both arguments (increment and
1285 decrement are binary internally in GCC). */
1286 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1287 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1288 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1289 maybe_const_itself);
1290 STRIP_TYPE_NOPS (op0);
1291 if (code != MODIFY_EXPR
1292 && code != PREDECREMENT_EXPR
1293 && code != PREINCREMENT_EXPR
1294 && code != POSTDECREMENT_EXPR
1295 && code != POSTINCREMENT_EXPR)
1296 op0 = decl_constant_value_for_optimization (op0);
1297 /* The RHS of a MODIFY_EXPR was fully folded when building that
1298 expression for the sake of conversion warnings. */
1299 if (code != MODIFY_EXPR)
1300 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1301 maybe_const_itself);
1302 STRIP_TYPE_NOPS (op1);
1303 op1 = decl_constant_value_for_optimization (op1);
1304 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1305 ret = in_init
1306 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1307 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1308 else
1309 ret = fold (expr);
1310 if (TREE_OVERFLOW_P (ret)
1311 && !TREE_OVERFLOW_P (op0)
1312 && !TREE_OVERFLOW_P (op1))
1313 overflow_warning (EXPR_LOCATION (expr), ret);
1314 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1315 && TREE_CODE (orig_op1) != INTEGER_CST
1316 && TREE_CODE (op1) == INTEGER_CST
1317 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1318 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1319 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1320 && c_inhibit_evaluation_warnings == 0)
1322 if (tree_int_cst_sgn (op1) < 0)
1323 warning_at (loc, 0, (code == LSHIFT_EXPR
1324 ? G_("left shift count is negative")
1325 : G_("right shift count is negative")));
1326 else if (compare_tree_int (op1,
1327 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1328 >= 0)
1329 warning_at (loc, 0, (code == LSHIFT_EXPR
1330 ? G_("left shift count >= width of type")
1331 : G_("right shift count >= width of type")));
1333 goto out;
1335 case INDIRECT_REF:
1336 case FIX_TRUNC_EXPR:
1337 case FLOAT_EXPR:
1338 CASE_CONVERT:
1339 case ADDR_SPACE_CONVERT_EXPR:
1340 case VIEW_CONVERT_EXPR:
1341 case NON_LVALUE_EXPR:
1342 case NEGATE_EXPR:
1343 case BIT_NOT_EXPR:
1344 case TRUTH_NOT_EXPR:
1345 case ADDR_EXPR:
1346 case CONJ_EXPR:
1347 case REALPART_EXPR:
1348 case IMAGPART_EXPR:
1349 /* Unary operations. */
1350 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1351 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1352 maybe_const_itself);
1353 STRIP_TYPE_NOPS (op0);
1354 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1355 op0 = decl_constant_value_for_optimization (op0);
1356 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1357 not prepared to deal with them if they occur in initializers. */
1358 if (op0 != orig_op0
1359 && code == ADDR_EXPR
1360 && (op1 = get_base_address (op0)) != NULL_TREE
1361 && TREE_CODE (op1) == INDIRECT_REF
1362 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1363 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1364 else if (op0 != orig_op0 || in_init)
1365 ret = in_init
1366 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1367 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1368 else
1369 ret = fold (expr);
1370 if (code == INDIRECT_REF
1371 && ret != expr
1372 && TREE_CODE (ret) == INDIRECT_REF)
1374 TREE_READONLY (ret) = TREE_READONLY (expr);
1375 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1376 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1378 switch (code)
1380 case FIX_TRUNC_EXPR:
1381 case FLOAT_EXPR:
1382 CASE_CONVERT:
1383 /* Don't warn about explicit conversions. We will already
1384 have warned about suspect implicit conversions. */
1385 break;
1387 default:
1388 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1389 overflow_warning (EXPR_LOCATION (expr), ret);
1390 break;
1392 goto out;
1394 case TRUTH_ANDIF_EXPR:
1395 case TRUTH_ORIF_EXPR:
1396 /* Binary operations not necessarily evaluating both
1397 arguments. */
1398 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1399 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1400 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1401 STRIP_TYPE_NOPS (op0);
1403 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1404 ? truthvalue_false_node
1405 : truthvalue_true_node));
1406 c_disable_warnings (unused_p);
1407 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1408 STRIP_TYPE_NOPS (op1);
1409 c_enable_warnings (unused_p);
1411 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1412 ret = in_init
1413 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1414 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1415 else
1416 ret = fold (expr);
1417 *maybe_const_operands &= op0_const;
1418 *maybe_const_itself &= op0_const_self;
1419 if (!(flag_isoc99
1420 && op0_const
1421 && op0_const_self
1422 && (code == TRUTH_ANDIF_EXPR
1423 ? op0 == truthvalue_false_node
1424 : op0 == truthvalue_true_node)))
1425 *maybe_const_operands &= op1_const;
1426 if (!(op0_const
1427 && op0_const_self
1428 && (code == TRUTH_ANDIF_EXPR
1429 ? op0 == truthvalue_false_node
1430 : op0 == truthvalue_true_node)))
1431 *maybe_const_itself &= op1_const_self;
1432 goto out;
1434 case COND_EXPR:
1435 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1436 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1437 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1438 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1440 STRIP_TYPE_NOPS (op0);
1441 c_disable_warnings (op0 == truthvalue_false_node);
1442 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1443 STRIP_TYPE_NOPS (op1);
1444 c_enable_warnings (op0 == truthvalue_false_node);
1446 c_disable_warnings (op0 == truthvalue_true_node);
1447 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1448 STRIP_TYPE_NOPS (op2);
1449 c_enable_warnings (op0 == truthvalue_true_node);
1451 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1452 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1453 else
1454 ret = fold (expr);
1455 *maybe_const_operands &= op0_const;
1456 *maybe_const_itself &= op0_const_self;
1457 if (!(flag_isoc99
1458 && op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_false_node))
1461 *maybe_const_operands &= op1_const;
1462 if (!(op0_const
1463 && op0_const_self
1464 && op0 == truthvalue_false_node))
1465 *maybe_const_itself &= op1_const_self;
1466 if (!(flag_isoc99
1467 && op0_const
1468 && op0_const_self
1469 && op0 == truthvalue_true_node))
1470 *maybe_const_operands &= op2_const;
1471 if (!(op0_const
1472 && op0_const_self
1473 && op0 == truthvalue_true_node))
1474 *maybe_const_itself &= op2_const_self;
1475 goto out;
1477 case EXCESS_PRECISION_EXPR:
1478 /* Each case where an operand with excess precision may be
1479 encountered must remove the EXCESS_PRECISION_EXPR around
1480 inner operands and possibly put one around the whole
1481 expression or possibly convert to the semantic type (which
1482 c_fully_fold does); we cannot tell at this stage which is
1483 appropriate in any particular case. */
1484 gcc_unreachable ();
1486 default:
1487 /* Various codes may appear through folding built-in functions
1488 and their arguments. */
1489 goto out;
1492 out:
1493 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1494 have been done by this point, so remove them again. */
1495 nowarning |= TREE_NO_WARNING (ret);
1496 STRIP_TYPE_NOPS (ret);
1497 if (nowarning && !TREE_NO_WARNING (ret))
1499 if (!CAN_HAVE_LOCATION_P (ret))
1500 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1501 TREE_NO_WARNING (ret) = 1;
1503 if (ret != expr)
1504 protected_set_expr_location (ret, loc);
1505 return ret;
1508 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1509 return EXP. Otherwise, return either EXP or its known constant
1510 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1511 Is the BLKmode test appropriate? */
1513 tree
1514 decl_constant_value_for_optimization (tree exp)
1516 tree ret;
1518 /* This function is only used by C, for c_fully_fold and other
1519 optimization, and may not be correct for C++. */
1520 if (c_dialect_cxx ())
1521 gcc_unreachable ();
1523 if (!optimize
1524 || TREE_CODE (exp) != VAR_DECL
1525 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1526 || DECL_MODE (exp) == BLKmode)
1527 return exp;
1529 ret = decl_constant_value (exp);
1530 /* Avoid unwanted tree sharing between the initializer and current
1531 function's body where the tree can be modified e.g. by the
1532 gimplifier. */
1533 if (ret != exp && TREE_STATIC (exp))
1534 ret = unshare_expr (ret);
1535 return ret;
1538 /* Print a warning if a constant expression had overflow in folding.
1539 Invoke this function on every expression that the language
1540 requires to be a constant expression.
1541 Note the ANSI C standard says it is erroneous for a
1542 constant expression to overflow. */
1544 void
1545 constant_expression_warning (tree value)
1547 if (warn_overflow && pedantic
1548 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1549 || TREE_CODE (value) == FIXED_CST
1550 || TREE_CODE (value) == VECTOR_CST
1551 || TREE_CODE (value) == COMPLEX_CST)
1552 && TREE_OVERFLOW (value))
1553 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1556 /* The same as above but print an unconditional error. */
1557 void
1558 constant_expression_error (tree value)
1560 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1561 || TREE_CODE (value) == FIXED_CST
1562 || TREE_CODE (value) == VECTOR_CST
1563 || TREE_CODE (value) == COMPLEX_CST)
1564 && TREE_OVERFLOW (value))
1565 error ("overflow in constant expression");
1568 /* Print a warning if an expression had overflow in folding and its
1569 operands hadn't.
1571 Invoke this function on every expression that
1572 (1) appears in the source code, and
1573 (2) is a constant expression that overflowed, and
1574 (3) is not already checked by convert_and_check;
1575 however, do not invoke this function on operands of explicit casts
1576 or when the expression is the result of an operator and any operand
1577 already overflowed. */
1579 void
1580 overflow_warning (location_t loc, tree value)
1582 if (c_inhibit_evaluation_warnings != 0)
1583 return;
1585 switch (TREE_CODE (value))
1587 case INTEGER_CST:
1588 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1589 break;
1591 case REAL_CST:
1592 warning_at (loc, OPT_Woverflow,
1593 "floating point overflow in expression");
1594 break;
1596 case FIXED_CST:
1597 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1598 break;
1600 case VECTOR_CST:
1601 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1602 break;
1604 case COMPLEX_CST:
1605 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1606 warning_at (loc, OPT_Woverflow,
1607 "complex integer overflow in expression");
1608 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1609 warning_at (loc, OPT_Woverflow,
1610 "complex floating point overflow in expression");
1611 break;
1613 default:
1614 break;
1618 /* Warn about uses of logical || / && operator in a context where it
1619 is likely that the bitwise equivalent was intended by the
1620 programmer. We have seen an expression in which CODE is a binary
1621 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1622 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1623 void
1624 warn_logical_operator (location_t location, enum tree_code code, tree type,
1625 enum tree_code code_left, tree op_left,
1626 enum tree_code ARG_UNUSED (code_right), tree op_right)
1628 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1629 int in0_p, in1_p, in_p;
1630 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1631 bool strict_overflow_p = false;
1633 if (code != TRUTH_ANDIF_EXPR
1634 && code != TRUTH_AND_EXPR
1635 && code != TRUTH_ORIF_EXPR
1636 && code != TRUTH_OR_EXPR)
1637 return;
1639 /* Warn if &&/|| are being used in a context where it is
1640 likely that the bitwise equivalent was intended by the
1641 programmer. That is, an expression such as op && MASK
1642 where op should not be any boolean expression, nor a
1643 constant, and mask seems to be a non-boolean integer constant. */
1644 if (!truth_value_p (code_left)
1645 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1646 && !CONSTANT_CLASS_P (op_left)
1647 && !TREE_NO_WARNING (op_left)
1648 && TREE_CODE (op_right) == INTEGER_CST
1649 && !integer_zerop (op_right)
1650 && !integer_onep (op_right))
1652 if (or_op)
1653 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1654 " applied to non-boolean constant");
1655 else
1656 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1657 " applied to non-boolean constant");
1658 TREE_NO_WARNING (op_left) = true;
1659 return;
1662 /* We do not warn for constants because they are typical of macro
1663 expansions that test for features. */
1664 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1665 return;
1667 /* This warning only makes sense with logical operands. */
1668 if (!(truth_value_p (TREE_CODE (op_left))
1669 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1670 || !(truth_value_p (TREE_CODE (op_right))
1671 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1672 return;
1675 /* We first test whether either side separately is trivially true
1676 (with OR) or trivially false (with AND). If so, do not warn.
1677 This is a common idiom for testing ranges of data types in
1678 portable code. */
1679 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1680 if (!lhs)
1681 return;
1682 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1683 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1685 /* If this is an OR operation, invert both sides; now, the result
1686 should be always false to get a warning. */
1687 if (or_op)
1688 in0_p = !in0_p;
1690 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1691 if (tem && integer_zerop (tem))
1692 return;
1694 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1695 if (!rhs)
1696 return;
1697 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1698 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1700 /* If this is an OR operation, invert both sides; now, the result
1701 should be always false to get a warning. */
1702 if (or_op)
1703 in1_p = !in1_p;
1705 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1706 if (tem && integer_zerop (tem))
1707 return;
1709 /* If both expressions have the same operand, if we can merge the
1710 ranges, and if the range test is always false, then warn. */
1711 if (operand_equal_p (lhs, rhs, 0)
1712 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1713 in1_p, low1, high1)
1714 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1715 type, lhs, in_p, low, high))
1716 && integer_zerop (tem))
1718 if (or_op)
1719 warning_at (location, OPT_Wlogical_op,
1720 "logical %<or%> "
1721 "of collectively exhaustive tests is always true");
1722 else
1723 warning_at (location, OPT_Wlogical_op,
1724 "logical %<and%> "
1725 "of mutually exclusive tests is always false");
1729 /* Warn about logical not used on the left hand side operand of a comparison.
1730 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1731 Do not warn if RHS is of a boolean type. */
1733 void
1734 warn_logical_not_parentheses (location_t location, enum tree_code code,
1735 tree rhs)
1737 if (TREE_CODE_CLASS (code) != tcc_comparison
1738 || TREE_TYPE (rhs) == NULL_TREE
1739 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1740 return;
1742 warning_at (location, OPT_Wlogical_not_parentheses,
1743 "logical not is only applied to the left hand side of "
1744 "comparison");
1747 /* Warn if EXP contains any computations whose results are not used.
1748 Return true if a warning is printed; false otherwise. LOCUS is the
1749 (potential) location of the expression. */
1751 bool
1752 warn_if_unused_value (const_tree exp, location_t locus)
1754 restart:
1755 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1756 return false;
1758 /* Don't warn about void constructs. This includes casting to void,
1759 void function calls, and statement expressions with a final cast
1760 to void. */
1761 if (VOID_TYPE_P (TREE_TYPE (exp)))
1762 return false;
1764 if (EXPR_HAS_LOCATION (exp))
1765 locus = EXPR_LOCATION (exp);
1767 switch (TREE_CODE (exp))
1769 case PREINCREMENT_EXPR:
1770 case POSTINCREMENT_EXPR:
1771 case PREDECREMENT_EXPR:
1772 case POSTDECREMENT_EXPR:
1773 case MODIFY_EXPR:
1774 case INIT_EXPR:
1775 case TARGET_EXPR:
1776 case CALL_EXPR:
1777 case TRY_CATCH_EXPR:
1778 case WITH_CLEANUP_EXPR:
1779 case EXIT_EXPR:
1780 case VA_ARG_EXPR:
1781 return false;
1783 case BIND_EXPR:
1784 /* For a binding, warn if no side effect within it. */
1785 exp = BIND_EXPR_BODY (exp);
1786 goto restart;
1788 case SAVE_EXPR:
1789 case NON_LVALUE_EXPR:
1790 case NOP_EXPR:
1791 exp = TREE_OPERAND (exp, 0);
1792 goto restart;
1794 case TRUTH_ORIF_EXPR:
1795 case TRUTH_ANDIF_EXPR:
1796 /* In && or ||, warn if 2nd operand has no side effect. */
1797 exp = TREE_OPERAND (exp, 1);
1798 goto restart;
1800 case COMPOUND_EXPR:
1801 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1802 return true;
1803 /* Let people do `(foo (), 0)' without a warning. */
1804 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1805 return false;
1806 exp = TREE_OPERAND (exp, 1);
1807 goto restart;
1809 case COND_EXPR:
1810 /* If this is an expression with side effects, don't warn; this
1811 case commonly appears in macro expansions. */
1812 if (TREE_SIDE_EFFECTS (exp))
1813 return false;
1814 goto warn;
1816 case INDIRECT_REF:
1817 /* Don't warn about automatic dereferencing of references, since
1818 the user cannot control it. */
1819 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1821 exp = TREE_OPERAND (exp, 0);
1822 goto restart;
1824 /* Fall through. */
1826 default:
1827 /* Referencing a volatile value is a side effect, so don't warn. */
1828 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1829 && TREE_THIS_VOLATILE (exp))
1830 return false;
1832 /* If this is an expression which has no operands, there is no value
1833 to be unused. There are no such language-independent codes,
1834 but front ends may define such. */
1835 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1836 return false;
1838 warn:
1839 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1844 /* Print a warning about casts that might indicate violation
1845 of strict aliasing rules if -Wstrict-aliasing is used and
1846 strict aliasing mode is in effect. OTYPE is the original
1847 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1849 bool
1850 strict_aliasing_warning (tree otype, tree type, tree expr)
1852 /* Strip pointer conversion chains and get to the correct original type. */
1853 STRIP_NOPS (expr);
1854 otype = TREE_TYPE (expr);
1856 if (!(flag_strict_aliasing
1857 && POINTER_TYPE_P (type)
1858 && POINTER_TYPE_P (otype)
1859 && !VOID_TYPE_P (TREE_TYPE (type)))
1860 /* If the type we are casting to is a ref-all pointer
1861 dereferencing it is always valid. */
1862 || TYPE_REF_CAN_ALIAS_ALL (type))
1863 return false;
1865 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1866 && (DECL_P (TREE_OPERAND (expr, 0))
1867 || handled_component_p (TREE_OPERAND (expr, 0))))
1869 /* Casting the address of an object to non void pointer. Warn
1870 if the cast breaks type based aliasing. */
1871 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1873 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1874 "might break strict-aliasing rules");
1875 return true;
1877 else
1879 /* warn_strict_aliasing >= 3. This includes the default (3).
1880 Only warn if the cast is dereferenced immediately. */
1881 alias_set_type set1 =
1882 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1883 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1885 if (set1 != set2 && set2 != 0
1886 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1888 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1889 "pointer will break strict-aliasing rules");
1890 return true;
1892 else if (warn_strict_aliasing == 2
1893 && !alias_sets_must_conflict_p (set1, set2))
1895 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1896 "pointer might break strict-aliasing rules");
1897 return true;
1901 else
1902 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1904 /* At this level, warn for any conversions, even if an address is
1905 not taken in the same statement. This will likely produce many
1906 false positives, but could be useful to pinpoint problems that
1907 are not revealed at higher levels. */
1908 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1909 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1910 if (!COMPLETE_TYPE_P (type)
1911 || !alias_sets_must_conflict_p (set1, set2))
1913 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1914 "pointer might break strict-aliasing rules");
1915 return true;
1919 return false;
1922 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1923 sizeof as last operand of certain builtins. */
1925 void
1926 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1927 vec<tree, va_gc> *params, tree *sizeof_arg,
1928 bool (*comp_types) (tree, tree))
1930 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1931 bool strop = false, cmp = false;
1932 unsigned int idx = ~0;
1933 location_t loc;
1935 if (TREE_CODE (callee) != FUNCTION_DECL
1936 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1937 || vec_safe_length (params) <= 1)
1938 return;
1940 switch (DECL_FUNCTION_CODE (callee))
1942 case BUILT_IN_STRNCMP:
1943 case BUILT_IN_STRNCASECMP:
1944 cmp = true;
1945 /* FALLTHRU */
1946 case BUILT_IN_STRNCPY:
1947 case BUILT_IN_STRNCPY_CHK:
1948 case BUILT_IN_STRNCAT:
1949 case BUILT_IN_STRNCAT_CHK:
1950 case BUILT_IN_STPNCPY:
1951 case BUILT_IN_STPNCPY_CHK:
1952 strop = true;
1953 /* FALLTHRU */
1954 case BUILT_IN_MEMCPY:
1955 case BUILT_IN_MEMCPY_CHK:
1956 case BUILT_IN_MEMMOVE:
1957 case BUILT_IN_MEMMOVE_CHK:
1958 if (params->length () < 3)
1959 return;
1960 src = (*params)[1];
1961 dest = (*params)[0];
1962 idx = 2;
1963 break;
1964 case BUILT_IN_BCOPY:
1965 if (params->length () < 3)
1966 return;
1967 src = (*params)[0];
1968 dest = (*params)[1];
1969 idx = 2;
1970 break;
1971 case BUILT_IN_MEMCMP:
1972 case BUILT_IN_BCMP:
1973 if (params->length () < 3)
1974 return;
1975 src = (*params)[1];
1976 dest = (*params)[0];
1977 idx = 2;
1978 cmp = true;
1979 break;
1980 case BUILT_IN_MEMSET:
1981 case BUILT_IN_MEMSET_CHK:
1982 if (params->length () < 3)
1983 return;
1984 dest = (*params)[0];
1985 idx = 2;
1986 break;
1987 case BUILT_IN_BZERO:
1988 dest = (*params)[0];
1989 idx = 1;
1990 break;
1991 case BUILT_IN_STRNDUP:
1992 src = (*params)[0];
1993 strop = true;
1994 idx = 1;
1995 break;
1996 case BUILT_IN_MEMCHR:
1997 if (params->length () < 3)
1998 return;
1999 src = (*params)[0];
2000 idx = 2;
2001 break;
2002 case BUILT_IN_SNPRINTF:
2003 case BUILT_IN_SNPRINTF_CHK:
2004 case BUILT_IN_VSNPRINTF:
2005 case BUILT_IN_VSNPRINTF_CHK:
2006 dest = (*params)[0];
2007 idx = 1;
2008 strop = true;
2009 break;
2010 default:
2011 break;
2014 if (idx >= 3)
2015 return;
2017 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2018 return;
2020 type = TYPE_P (sizeof_arg[idx])
2021 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2022 if (!POINTER_TYPE_P (type))
2023 return;
2025 if (dest
2026 && (tem = tree_strip_nop_conversions (dest))
2027 && POINTER_TYPE_P (TREE_TYPE (tem))
2028 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2029 return;
2031 if (src
2032 && (tem = tree_strip_nop_conversions (src))
2033 && POINTER_TYPE_P (TREE_TYPE (tem))
2034 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2035 return;
2037 loc = sizeof_arg_loc[idx];
2039 if (dest && !cmp)
2041 if (!TYPE_P (sizeof_arg[idx])
2042 && operand_equal_p (dest, sizeof_arg[idx], 0)
2043 && comp_types (TREE_TYPE (dest), type))
2045 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2046 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2047 "argument to %<sizeof%> in %qD call is the same "
2048 "expression as the destination; did you mean to "
2049 "remove the addressof?", callee);
2050 else if ((TYPE_PRECISION (TREE_TYPE (type))
2051 == TYPE_PRECISION (char_type_node))
2052 || strop)
2053 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2054 "argument to %<sizeof%> in %qD call is the same "
2055 "expression as the destination; did you mean to "
2056 "provide an explicit length?", callee);
2057 else
2058 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2059 "argument to %<sizeof%> in %qD call is the same "
2060 "expression as the destination; did you mean to "
2061 "dereference it?", callee);
2062 return;
2065 if (POINTER_TYPE_P (TREE_TYPE (dest))
2066 && !strop
2067 && comp_types (TREE_TYPE (dest), type)
2068 && !VOID_TYPE_P (TREE_TYPE (type)))
2070 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2071 "argument to %<sizeof%> in %qD call is the same "
2072 "pointer type %qT as the destination; expected %qT "
2073 "or an explicit length", callee, TREE_TYPE (dest),
2074 TREE_TYPE (TREE_TYPE (dest)));
2075 return;
2079 if (src && !cmp)
2081 if (!TYPE_P (sizeof_arg[idx])
2082 && operand_equal_p (src, sizeof_arg[idx], 0)
2083 && comp_types (TREE_TYPE (src), type))
2085 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2086 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2087 "argument to %<sizeof%> in %qD call is the same "
2088 "expression as the source; did you mean to "
2089 "remove the addressof?", callee);
2090 else if ((TYPE_PRECISION (TREE_TYPE (type))
2091 == TYPE_PRECISION (char_type_node))
2092 || strop)
2093 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2094 "argument to %<sizeof%> in %qD call is the same "
2095 "expression as the source; did you mean to "
2096 "provide an explicit length?", callee);
2097 else
2098 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2099 "argument to %<sizeof%> in %qD call is the same "
2100 "expression as the source; did you mean to "
2101 "dereference it?", callee);
2102 return;
2105 if (POINTER_TYPE_P (TREE_TYPE (src))
2106 && !strop
2107 && comp_types (TREE_TYPE (src), type)
2108 && !VOID_TYPE_P (TREE_TYPE (type)))
2110 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2111 "argument to %<sizeof%> in %qD call is the same "
2112 "pointer type %qT as the source; expected %qT "
2113 "or an explicit length", callee, TREE_TYPE (src),
2114 TREE_TYPE (TREE_TYPE (src)));
2115 return;
2119 if (dest)
2121 if (!TYPE_P (sizeof_arg[idx])
2122 && operand_equal_p (dest, sizeof_arg[idx], 0)
2123 && comp_types (TREE_TYPE (dest), type))
2125 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2126 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2127 "argument to %<sizeof%> in %qD call is the same "
2128 "expression as the first source; did you mean to "
2129 "remove the addressof?", callee);
2130 else if ((TYPE_PRECISION (TREE_TYPE (type))
2131 == TYPE_PRECISION (char_type_node))
2132 || strop)
2133 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2134 "argument to %<sizeof%> in %qD call is the same "
2135 "expression as the first source; did you mean to "
2136 "provide an explicit length?", callee);
2137 else
2138 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2139 "argument to %<sizeof%> in %qD call is the same "
2140 "expression as the first source; did you mean to "
2141 "dereference it?", callee);
2142 return;
2145 if (POINTER_TYPE_P (TREE_TYPE (dest))
2146 && !strop
2147 && comp_types (TREE_TYPE (dest), type)
2148 && !VOID_TYPE_P (TREE_TYPE (type)))
2150 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2151 "argument to %<sizeof%> in %qD call is the same "
2152 "pointer type %qT as the first source; expected %qT "
2153 "or an explicit length", callee, TREE_TYPE (dest),
2154 TREE_TYPE (TREE_TYPE (dest)));
2155 return;
2159 if (src)
2161 if (!TYPE_P (sizeof_arg[idx])
2162 && operand_equal_p (src, sizeof_arg[idx], 0)
2163 && comp_types (TREE_TYPE (src), type))
2165 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2166 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2167 "argument to %<sizeof%> in %qD call is the same "
2168 "expression as the second source; did you mean to "
2169 "remove the addressof?", callee);
2170 else if ((TYPE_PRECISION (TREE_TYPE (type))
2171 == TYPE_PRECISION (char_type_node))
2172 || strop)
2173 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2174 "argument to %<sizeof%> in %qD call is the same "
2175 "expression as the second source; did you mean to "
2176 "provide an explicit length?", callee);
2177 else
2178 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2179 "argument to %<sizeof%> in %qD call is the same "
2180 "expression as the second source; did you mean to "
2181 "dereference it?", callee);
2182 return;
2185 if (POINTER_TYPE_P (TREE_TYPE (src))
2186 && !strop
2187 && comp_types (TREE_TYPE (src), type)
2188 && !VOID_TYPE_P (TREE_TYPE (type)))
2190 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2191 "argument to %<sizeof%> in %qD call is the same "
2192 "pointer type %qT as the second source; expected %qT "
2193 "or an explicit length", callee, TREE_TYPE (src),
2194 TREE_TYPE (TREE_TYPE (src)));
2195 return;
2201 /* Warn for unlikely, improbable, or stupid DECL declarations
2202 of `main'. */
2204 void
2205 check_main_parameter_types (tree decl)
2207 function_args_iterator iter;
2208 tree type;
2209 int argct = 0;
2211 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2213 /* XXX void_type_node belies the abstraction. */
2214 if (type == void_type_node || type == error_mark_node )
2215 break;
2217 tree t = type;
2218 if (TYPE_ATOMIC (t))
2219 pedwarn (input_location, OPT_Wmain,
2220 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2221 type, decl);
2222 while (POINTER_TYPE_P (t))
2224 t = TREE_TYPE (t);
2225 if (TYPE_ATOMIC (t))
2226 pedwarn (input_location, OPT_Wmain,
2227 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2228 type, decl);
2231 ++argct;
2232 switch (argct)
2234 case 1:
2235 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2236 pedwarn (input_location, OPT_Wmain,
2237 "first argument of %q+D should be %<int%>", decl);
2238 break;
2240 case 2:
2241 if (TREE_CODE (type) != POINTER_TYPE
2242 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2243 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2244 != char_type_node))
2245 pedwarn (input_location, OPT_Wmain,
2246 "second argument of %q+D should be %<char **%>", decl);
2247 break;
2249 case 3:
2250 if (TREE_CODE (type) != POINTER_TYPE
2251 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2252 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2253 != char_type_node))
2254 pedwarn (input_location, OPT_Wmain,
2255 "third argument of %q+D should probably be "
2256 "%<char **%>", decl);
2257 break;
2261 /* It is intentional that this message does not mention the third
2262 argument because it's only mentioned in an appendix of the
2263 standard. */
2264 if (argct > 0 && (argct < 2 || argct > 3))
2265 pedwarn (input_location, OPT_Wmain,
2266 "%q+D takes only zero or two arguments", decl);
2268 if (stdarg_p (TREE_TYPE (decl)))
2269 pedwarn (input_location, OPT_Wmain,
2270 "%q+D declared as variadic function", decl);
2273 /* vector_targets_convertible_p is used for vector pointer types. The
2274 callers perform various checks that the qualifiers are satisfactory,
2275 while OTOH vector_targets_convertible_p ignores the number of elements
2276 in the vectors. That's fine with vector pointers as we can consider,
2277 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2278 and that does not require and conversion of the pointer values.
2279 In contrast, vector_types_convertible_p and
2280 vector_types_compatible_elements_p are used for vector value types. */
2281 /* True if pointers to distinct types T1 and T2 can be converted to
2282 each other without an explicit cast. Only returns true for opaque
2283 vector types. */
2284 bool
2285 vector_targets_convertible_p (const_tree t1, const_tree t2)
2287 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2288 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2289 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2290 return true;
2292 return false;
2295 /* vector_types_convertible_p is used for vector value types.
2296 It could in principle call vector_targets_convertible_p as a subroutine,
2297 but then the check for vector type would be duplicated with its callers,
2298 and also the purpose of vector_targets_convertible_p would become
2299 muddled.
2300 Where vector_types_convertible_p returns true, a conversion might still be
2301 needed to make the types match.
2302 In contrast, vector_targets_convertible_p is used for vector pointer
2303 values, and vector_types_compatible_elements_p is used specifically
2304 in the context for binary operators, as a check if use is possible without
2305 conversion. */
2306 /* True if vector types T1 and T2 can be converted to each other
2307 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2308 can only be converted with -flax-vector-conversions yet that is not
2309 in effect, emit a note telling the user about that option if such
2310 a note has not previously been emitted. */
2311 bool
2312 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2314 static bool emitted_lax_note = false;
2315 bool convertible_lax;
2317 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2318 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2319 return true;
2321 convertible_lax =
2322 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2323 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2324 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2325 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2326 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2328 if (!convertible_lax || flag_lax_vector_conversions)
2329 return convertible_lax;
2331 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2332 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2333 return true;
2335 if (emit_lax_note && !emitted_lax_note)
2337 emitted_lax_note = true;
2338 inform (input_location, "use -flax-vector-conversions to permit "
2339 "conversions between vectors with differing "
2340 "element types or numbers of subparts");
2343 return false;
2346 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2347 and have vector types, V0 has the same type as V1, and the number of
2348 elements of V0, V1, MASK is the same.
2350 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2351 called with two arguments. In this case implementation passes the
2352 first argument twice in order to share the same tree code. This fact
2353 could enable the mask-values being twice the vector length. This is
2354 an implementation accident and this semantics is not guaranteed to
2355 the user. */
2356 tree
2357 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2358 bool complain)
2360 tree ret;
2361 bool wrap = true;
2362 bool maybe_const = false;
2363 bool two_arguments = false;
2365 if (v1 == NULL_TREE)
2367 two_arguments = true;
2368 v1 = v0;
2371 if (v0 == error_mark_node || v1 == error_mark_node
2372 || mask == error_mark_node)
2373 return error_mark_node;
2375 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2376 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2378 if (complain)
2379 error_at (loc, "__builtin_shuffle last argument must "
2380 "be an integer vector");
2381 return error_mark_node;
2384 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2385 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2387 if (complain)
2388 error_at (loc, "__builtin_shuffle arguments must be vectors");
2389 return error_mark_node;
2392 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2394 if (complain)
2395 error_at (loc, "__builtin_shuffle argument vectors must be of "
2396 "the same type");
2397 return error_mark_node;
2400 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2401 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2402 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2403 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2405 if (complain)
2406 error_at (loc, "__builtin_shuffle number of elements of the "
2407 "argument vector(s) and the mask vector should "
2408 "be the same");
2409 return error_mark_node;
2412 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2413 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2415 if (complain)
2416 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2417 "must have the same size as inner type of the mask");
2418 return error_mark_node;
2421 if (!c_dialect_cxx ())
2423 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2424 v0 = c_fully_fold (v0, false, &maybe_const);
2425 wrap &= maybe_const;
2427 if (two_arguments)
2428 v1 = v0 = save_expr (v0);
2429 else
2431 v1 = c_fully_fold (v1, false, &maybe_const);
2432 wrap &= maybe_const;
2435 mask = c_fully_fold (mask, false, &maybe_const);
2436 wrap &= maybe_const;
2438 else if (two_arguments)
2439 v1 = v0 = save_expr (v0);
2441 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2443 if (!c_dialect_cxx () && !wrap)
2444 ret = c_wrap_maybe_const (ret, true);
2446 return ret;
2449 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2450 to integral type. */
2452 static tree
2453 c_common_get_narrower (tree op, int *unsignedp_ptr)
2455 op = get_narrower (op, unsignedp_ptr);
2457 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2458 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2460 /* C++0x scoped enumerations don't implicitly convert to integral
2461 type; if we stripped an explicit conversion to a larger type we
2462 need to replace it so common_type will still work. */
2463 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2464 TYPE_UNSIGNED (TREE_TYPE (op)));
2465 op = fold_convert (type, op);
2467 return op;
2470 /* This is a helper function of build_binary_op.
2472 For certain operations if both args were extended from the same
2473 smaller type, do the arithmetic in that type and then extend.
2475 BITWISE indicates a bitwise operation.
2476 For them, this optimization is safe only if
2477 both args are zero-extended or both are sign-extended.
2478 Otherwise, we might change the result.
2479 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2480 but calculated in (unsigned short) it would be (unsigned short)-1.
2482 tree
2483 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2485 int unsigned0, unsigned1;
2486 tree arg0, arg1;
2487 int uns;
2488 tree type;
2490 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2491 excessive narrowing when we call get_narrower below. For
2492 example, suppose that OP0 is of unsigned int extended
2493 from signed char and that RESULT_TYPE is long long int.
2494 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2495 like
2497 (long long int) (unsigned int) signed_char
2499 which get_narrower would narrow down to
2501 (unsigned int) signed char
2503 If we do not cast OP0 first, get_narrower would return
2504 signed_char, which is inconsistent with the case of the
2505 explicit cast. */
2506 op0 = convert (result_type, op0);
2507 op1 = convert (result_type, op1);
2509 arg0 = c_common_get_narrower (op0, &unsigned0);
2510 arg1 = c_common_get_narrower (op1, &unsigned1);
2512 /* UNS is 1 if the operation to be done is an unsigned one. */
2513 uns = TYPE_UNSIGNED (result_type);
2515 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2516 but it *requires* conversion to FINAL_TYPE. */
2518 if ((TYPE_PRECISION (TREE_TYPE (op0))
2519 == TYPE_PRECISION (TREE_TYPE (arg0)))
2520 && TREE_TYPE (op0) != result_type)
2521 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2522 if ((TYPE_PRECISION (TREE_TYPE (op1))
2523 == TYPE_PRECISION (TREE_TYPE (arg1)))
2524 && TREE_TYPE (op1) != result_type)
2525 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2527 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2529 /* For bitwise operations, signedness of nominal type
2530 does not matter. Consider only how operands were extended. */
2531 if (bitwise)
2532 uns = unsigned0;
2534 /* Note that in all three cases below we refrain from optimizing
2535 an unsigned operation on sign-extended args.
2536 That would not be valid. */
2538 /* Both args variable: if both extended in same way
2539 from same width, do it in that width.
2540 Do it unsigned if args were zero-extended. */
2541 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2542 < TYPE_PRECISION (result_type))
2543 && (TYPE_PRECISION (TREE_TYPE (arg1))
2544 == TYPE_PRECISION (TREE_TYPE (arg0)))
2545 && unsigned0 == unsigned1
2546 && (unsigned0 || !uns))
2547 return c_common_signed_or_unsigned_type
2548 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2550 else if (TREE_CODE (arg0) == INTEGER_CST
2551 && (unsigned1 || !uns)
2552 && (TYPE_PRECISION (TREE_TYPE (arg1))
2553 < TYPE_PRECISION (result_type))
2554 && (type
2555 = c_common_signed_or_unsigned_type (unsigned1,
2556 TREE_TYPE (arg1)))
2557 && !POINTER_TYPE_P (type)
2558 && int_fits_type_p (arg0, type))
2559 return type;
2561 else if (TREE_CODE (arg1) == INTEGER_CST
2562 && (unsigned0 || !uns)
2563 && (TYPE_PRECISION (TREE_TYPE (arg0))
2564 < TYPE_PRECISION (result_type))
2565 && (type
2566 = c_common_signed_or_unsigned_type (unsigned0,
2567 TREE_TYPE (arg0)))
2568 && !POINTER_TYPE_P (type)
2569 && int_fits_type_p (arg1, type))
2570 return type;
2572 return result_type;
2575 /* Checks if expression EXPR of real/integer type cannot be converted
2576 to the real/integer type TYPE. Function returns non-zero when:
2577 * EXPR is a constant which cannot be exactly converted to TYPE.
2578 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2579 for EXPR type and TYPE being both integers or both real.
2580 * EXPR is not a constant of real type and TYPE is an integer.
2581 * EXPR is not a constant of integer type which cannot be
2582 exactly converted to real type.
2583 Function allows conversions between types of different signedness and
2584 can return SAFE_CONVERSION (zero) in that case. Function can produce
2585 signedness warnings if PRODUCE_WARNS is true. */
2587 enum conversion_safety
2588 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2590 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2591 tree expr_type = TREE_TYPE (expr);
2592 loc = expansion_point_location_if_in_system_header (loc);
2594 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2596 /* Warn for real constant that is not an exact integer converted
2597 to integer type. */
2598 if (TREE_CODE (expr_type) == REAL_TYPE
2599 && TREE_CODE (type) == INTEGER_TYPE)
2601 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2602 give_warning = UNSAFE_REAL;
2604 /* Warn for an integer constant that does not fit into integer type. */
2605 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2606 && TREE_CODE (type) == INTEGER_TYPE
2607 && !int_fits_type_p (expr, type))
2609 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2610 && tree_int_cst_sgn (expr) < 0)
2612 if (produce_warns)
2613 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2614 " implicitly converted to unsigned type");
2616 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2618 if (produce_warns)
2619 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2620 " constant value to negative integer");
2622 else
2623 give_warning = UNSAFE_OTHER;
2625 else if (TREE_CODE (type) == REAL_TYPE)
2627 /* Warn for an integer constant that does not fit into real type. */
2628 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2630 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2631 if (!exact_real_truncate (TYPE_MODE (type), &a))
2632 give_warning = UNSAFE_REAL;
2634 /* Warn for a real constant that does not fit into a smaller
2635 real type. */
2636 else if (TREE_CODE (expr_type) == REAL_TYPE
2637 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2639 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2640 if (!exact_real_truncate (TYPE_MODE (type), &a))
2641 give_warning = UNSAFE_REAL;
2645 else
2647 /* Warn for real types converted to integer types. */
2648 if (TREE_CODE (expr_type) == REAL_TYPE
2649 && TREE_CODE (type) == INTEGER_TYPE)
2650 give_warning = UNSAFE_REAL;
2652 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2653 && TREE_CODE (type) == INTEGER_TYPE)
2655 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2656 expr = get_unwidened (expr, 0);
2657 expr_type = TREE_TYPE (expr);
2659 /* Don't warn for short y; short x = ((int)y & 0xff); */
2660 if (TREE_CODE (expr) == BIT_AND_EXPR
2661 || TREE_CODE (expr) == BIT_IOR_EXPR
2662 || TREE_CODE (expr) == BIT_XOR_EXPR)
2664 /* If both args were extended from a shortest type,
2665 use that type if that is safe. */
2666 expr_type = shorten_binary_op (expr_type,
2667 TREE_OPERAND (expr, 0),
2668 TREE_OPERAND (expr, 1),
2669 /* bitwise */1);
2671 if (TREE_CODE (expr) == BIT_AND_EXPR)
2673 tree op0 = TREE_OPERAND (expr, 0);
2674 tree op1 = TREE_OPERAND (expr, 1);
2675 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2676 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2678 /* If one of the operands is a non-negative constant
2679 that fits in the target type, then the type of the
2680 other operand does not matter. */
2681 if ((TREE_CODE (op0) == INTEGER_CST
2682 && int_fits_type_p (op0, c_common_signed_type (type))
2683 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2684 || (TREE_CODE (op1) == INTEGER_CST
2685 && int_fits_type_p (op1, c_common_signed_type (type))
2686 && int_fits_type_p (op1,
2687 c_common_unsigned_type (type))))
2688 return SAFE_CONVERSION;
2689 /* If constant is unsigned and fits in the target
2690 type, then the result will also fit. */
2691 else if ((TREE_CODE (op0) == INTEGER_CST
2692 && unsigned0
2693 && int_fits_type_p (op0, type))
2694 || (TREE_CODE (op1) == INTEGER_CST
2695 && unsigned1
2696 && int_fits_type_p (op1, type)))
2697 return SAFE_CONVERSION;
2700 /* Warn for integer types converted to smaller integer types. */
2701 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2702 give_warning = UNSAFE_OTHER;
2704 /* When they are the same width but different signedness,
2705 then the value may change. */
2706 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2707 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2708 /* Even when converted to a bigger type, if the type is
2709 unsigned but expr is signed, then negative values
2710 will be changed. */
2711 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2712 && produce_warns)
2713 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2714 "may change the sign of the result",
2715 type, expr_type);
2718 /* Warn for integer types converted to real types if and only if
2719 all the range of values of the integer type cannot be
2720 represented by the real type. */
2721 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2722 && TREE_CODE (type) == REAL_TYPE)
2724 tree type_low_bound, type_high_bound;
2725 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2727 /* Don't warn about char y = 0xff; float x = (int) y; */
2728 expr = get_unwidened (expr, 0);
2729 expr_type = TREE_TYPE (expr);
2731 type_low_bound = TYPE_MIN_VALUE (expr_type);
2732 type_high_bound = TYPE_MAX_VALUE (expr_type);
2733 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2734 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2736 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2737 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2738 give_warning = UNSAFE_OTHER;
2741 /* Warn for real types converted to smaller real types. */
2742 else if (TREE_CODE (expr_type) == REAL_TYPE
2743 && TREE_CODE (type) == REAL_TYPE
2744 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2745 give_warning = UNSAFE_REAL;
2748 return give_warning;
2751 /* Warns if the conversion of EXPR to TYPE may alter a value.
2752 This is a helper function for warnings_for_convert_and_check. */
2754 static void
2755 conversion_warning (location_t loc, tree type, tree expr)
2757 tree expr_type = TREE_TYPE (expr);
2758 enum conversion_safety conversion_kind;
2760 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2761 return;
2763 /* This may happen, because for LHS op= RHS we preevaluate
2764 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2765 means we could no longer see the code of the EXPR. */
2766 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2767 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2768 if (TREE_CODE (expr) == SAVE_EXPR)
2769 expr = TREE_OPERAND (expr, 0);
2771 switch (TREE_CODE (expr))
2773 case EQ_EXPR:
2774 case NE_EXPR:
2775 case LE_EXPR:
2776 case GE_EXPR:
2777 case LT_EXPR:
2778 case GT_EXPR:
2779 case TRUTH_ANDIF_EXPR:
2780 case TRUTH_ORIF_EXPR:
2781 case TRUTH_AND_EXPR:
2782 case TRUTH_OR_EXPR:
2783 case TRUTH_XOR_EXPR:
2784 case TRUTH_NOT_EXPR:
2785 /* Conversion from boolean to a signed:1 bit-field (which only
2786 can hold the values 0 and -1) doesn't lose information - but
2787 it does change the value. */
2788 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2789 warning_at (loc, OPT_Wconversion,
2790 "conversion to %qT from boolean expression", type);
2791 return;
2793 case REAL_CST:
2794 case INTEGER_CST:
2795 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2796 if (conversion_kind == UNSAFE_REAL)
2797 warning_at (loc, OPT_Wfloat_conversion,
2798 "conversion to %qT alters %qT constant value",
2799 type, expr_type);
2800 else if (conversion_kind)
2801 warning_at (loc, OPT_Wconversion,
2802 "conversion to %qT alters %qT constant value",
2803 type, expr_type);
2804 return;
2806 case COND_EXPR:
2808 /* In case of COND_EXPR, we do not care about the type of
2809 COND_EXPR, only about the conversion of each operand. */
2810 tree op1 = TREE_OPERAND (expr, 1);
2811 tree op2 = TREE_OPERAND (expr, 2);
2813 conversion_warning (loc, type, op1);
2814 conversion_warning (loc, type, op2);
2815 return;
2818 default: /* 'expr' is not a constant. */
2819 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2820 if (conversion_kind == UNSAFE_REAL)
2821 warning_at (loc, OPT_Wfloat_conversion,
2822 "conversion to %qT from %qT may alter its value",
2823 type, expr_type);
2824 else if (conversion_kind)
2825 warning_at (loc, OPT_Wconversion,
2826 "conversion to %qT from %qT may alter its value",
2827 type, expr_type);
2831 /* Produce warnings after a conversion. RESULT is the result of
2832 converting EXPR to TYPE. This is a helper function for
2833 convert_and_check and cp_convert_and_check. */
2835 void
2836 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2837 tree result)
2839 loc = expansion_point_location_if_in_system_header (loc);
2841 if (TREE_CODE (expr) == INTEGER_CST
2842 && (TREE_CODE (type) == INTEGER_TYPE
2843 || TREE_CODE (type) == ENUMERAL_TYPE)
2844 && !int_fits_type_p (expr, type))
2846 /* Do not diagnose overflow in a constant expression merely
2847 because a conversion overflowed. */
2848 if (TREE_OVERFLOW (result))
2849 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2851 if (TYPE_UNSIGNED (type))
2853 /* This detects cases like converting -129 or 256 to
2854 unsigned char. */
2855 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2856 warning_at (loc, OPT_Woverflow,
2857 "large integer implicitly truncated to unsigned type");
2858 else
2859 conversion_warning (loc, type, expr);
2861 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2862 warning_at (loc, OPT_Woverflow,
2863 "overflow in implicit constant conversion");
2864 /* No warning for converting 0x80000000 to int. */
2865 else if (pedantic
2866 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2867 || TYPE_PRECISION (TREE_TYPE (expr))
2868 != TYPE_PRECISION (type)))
2869 warning_at (loc, OPT_Woverflow,
2870 "overflow in implicit constant conversion");
2872 else
2873 conversion_warning (loc, type, expr);
2875 else if ((TREE_CODE (result) == INTEGER_CST
2876 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2877 warning_at (loc, OPT_Woverflow,
2878 "overflow in implicit constant conversion");
2879 else
2880 conversion_warning (loc, type, expr);
2884 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2885 Invoke this function on every expression that is converted implicitly,
2886 i.e. because of language rules and not because of an explicit cast. */
2888 tree
2889 convert_and_check (location_t loc, tree type, tree expr)
2891 tree result;
2892 tree expr_for_warning;
2894 /* Convert from a value with possible excess precision rather than
2895 via the semantic type, but do not warn about values not fitting
2896 exactly in the semantic type. */
2897 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2899 tree orig_type = TREE_TYPE (expr);
2900 expr = TREE_OPERAND (expr, 0);
2901 expr_for_warning = convert (orig_type, expr);
2902 if (orig_type == type)
2903 return expr_for_warning;
2905 else
2906 expr_for_warning = expr;
2908 if (TREE_TYPE (expr) == type)
2909 return expr;
2911 result = convert (type, expr);
2913 if (c_inhibit_evaluation_warnings == 0
2914 && !TREE_OVERFLOW_P (expr)
2915 && result != error_mark_node)
2916 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2918 return result;
2921 /* A node in a list that describes references to variables (EXPR), which are
2922 either read accesses if WRITER is zero, or write accesses, in which case
2923 WRITER is the parent of EXPR. */
2924 struct tlist
2926 struct tlist *next;
2927 tree expr, writer;
2930 /* Used to implement a cache the results of a call to verify_tree. We only
2931 use this for SAVE_EXPRs. */
2932 struct tlist_cache
2934 struct tlist_cache *next;
2935 struct tlist *cache_before_sp;
2936 struct tlist *cache_after_sp;
2937 tree expr;
2940 /* Obstack to use when allocating tlist structures, and corresponding
2941 firstobj. */
2942 static struct obstack tlist_obstack;
2943 static char *tlist_firstobj = 0;
2945 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2946 warnings. */
2947 static struct tlist *warned_ids;
2948 /* SAVE_EXPRs need special treatment. We process them only once and then
2949 cache the results. */
2950 static struct tlist_cache *save_expr_cache;
2952 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2953 static void merge_tlist (struct tlist **, struct tlist *, int);
2954 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2955 static int warning_candidate_p (tree);
2956 static bool candidate_equal_p (const_tree, const_tree);
2957 static void warn_for_collisions (struct tlist *);
2958 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2959 static struct tlist *new_tlist (struct tlist *, tree, tree);
2961 /* Create a new struct tlist and fill in its fields. */
2962 static struct tlist *
2963 new_tlist (struct tlist *next, tree t, tree writer)
2965 struct tlist *l;
2966 l = XOBNEW (&tlist_obstack, struct tlist);
2967 l->next = next;
2968 l->expr = t;
2969 l->writer = writer;
2970 return l;
2973 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2974 is nonnull, we ignore any node we find which has a writer equal to it. */
2976 static void
2977 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2979 while (add)
2981 struct tlist *next = add->next;
2982 if (!copy)
2983 add->next = *to;
2984 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2985 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2986 add = next;
2990 /* Merge the nodes of ADD into TO. This merging process is done so that for
2991 each variable that already exists in TO, no new node is added; however if
2992 there is a write access recorded in ADD, and an occurrence on TO is only
2993 a read access, then the occurrence in TO will be modified to record the
2994 write. */
2996 static void
2997 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2999 struct tlist **end = to;
3001 while (*end)
3002 end = &(*end)->next;
3004 while (add)
3006 int found = 0;
3007 struct tlist *tmp2;
3008 struct tlist *next = add->next;
3010 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3011 if (candidate_equal_p (tmp2->expr, add->expr))
3013 found = 1;
3014 if (!tmp2->writer)
3015 tmp2->writer = add->writer;
3017 if (!found)
3019 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3020 end = &(*end)->next;
3021 *end = 0;
3023 add = next;
3027 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3028 references in list LIST conflict with it, excluding reads if ONLY writers
3029 is nonzero. */
3031 static void
3032 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3033 int only_writes)
3035 struct tlist *tmp;
3037 /* Avoid duplicate warnings. */
3038 for (tmp = warned_ids; tmp; tmp = tmp->next)
3039 if (candidate_equal_p (tmp->expr, written))
3040 return;
3042 while (list)
3044 if (candidate_equal_p (list->expr, written)
3045 && !candidate_equal_p (list->writer, writer)
3046 && (!only_writes || list->writer))
3048 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3049 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3050 OPT_Wsequence_point, "operation on %qE may be undefined",
3051 list->expr);
3053 list = list->next;
3057 /* Given a list LIST of references to variables, find whether any of these
3058 can cause conflicts due to missing sequence points. */
3060 static void
3061 warn_for_collisions (struct tlist *list)
3063 struct tlist *tmp;
3065 for (tmp = list; tmp; tmp = tmp->next)
3067 if (tmp->writer)
3068 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3072 /* Return nonzero if X is a tree that can be verified by the sequence point
3073 warnings. */
3074 static int
3075 warning_candidate_p (tree x)
3077 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3078 return 0;
3080 if (TREE_CODE (x) == BLOCK)
3081 return 0;
3083 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3084 (lvalue_p) crash on TRY/CATCH. */
3085 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3086 return 0;
3088 if (!lvalue_p (x))
3089 return 0;
3091 /* No point to track non-const calls, they will never satisfy
3092 operand_equal_p. */
3093 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3094 return 0;
3096 if (TREE_CODE (x) == STRING_CST)
3097 return 0;
3099 return 1;
3102 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3103 static bool
3104 candidate_equal_p (const_tree x, const_tree y)
3106 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3109 /* Walk the tree X, and record accesses to variables. If X is written by the
3110 parent tree, WRITER is the parent.
3111 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3112 expression or its only operand forces a sequence point, then everything up
3113 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3114 in PNO_SP.
3115 Once we return, we will have emitted warnings if any subexpression before
3116 such a sequence point could be undefined. On a higher level, however, the
3117 sequence point may not be relevant, and we'll merge the two lists.
3119 Example: (b++, a) + b;
3120 The call that processes the COMPOUND_EXPR will store the increment of B
3121 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3122 processes the PLUS_EXPR will need to merge the two lists so that
3123 eventually, all accesses end up on the same list (and we'll warn about the
3124 unordered subexpressions b++ and b.
3126 A note on merging. If we modify the former example so that our expression
3127 becomes
3128 (b++, b) + a
3129 care must be taken not simply to add all three expressions into the final
3130 PNO_SP list. The function merge_tlist takes care of that by merging the
3131 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3132 way, so that no more than one access to B is recorded. */
3134 static void
3135 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3136 tree writer)
3138 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3139 enum tree_code code;
3140 enum tree_code_class cl;
3142 /* X may be NULL if it is the operand of an empty statement expression
3143 ({ }). */
3144 if (x == NULL)
3145 return;
3147 restart:
3148 code = TREE_CODE (x);
3149 cl = TREE_CODE_CLASS (code);
3151 if (warning_candidate_p (x))
3152 *pno_sp = new_tlist (*pno_sp, x, writer);
3154 switch (code)
3156 case CONSTRUCTOR:
3157 case SIZEOF_EXPR:
3158 return;
3160 case COMPOUND_EXPR:
3161 case TRUTH_ANDIF_EXPR:
3162 case TRUTH_ORIF_EXPR:
3163 tmp_before = tmp_nosp = tmp_list3 = 0;
3164 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3165 warn_for_collisions (tmp_nosp);
3166 merge_tlist (pbefore_sp, tmp_before, 0);
3167 merge_tlist (pbefore_sp, tmp_nosp, 0);
3168 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3169 merge_tlist (pbefore_sp, tmp_list3, 0);
3170 return;
3172 case COND_EXPR:
3173 tmp_before = tmp_list2 = 0;
3174 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3175 warn_for_collisions (tmp_list2);
3176 merge_tlist (pbefore_sp, tmp_before, 0);
3177 merge_tlist (pbefore_sp, tmp_list2, 0);
3179 tmp_list3 = tmp_nosp = 0;
3180 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3181 warn_for_collisions (tmp_nosp);
3182 merge_tlist (pbefore_sp, tmp_list3, 0);
3184 tmp_list3 = tmp_list2 = 0;
3185 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3186 warn_for_collisions (tmp_list2);
3187 merge_tlist (pbefore_sp, tmp_list3, 0);
3188 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3189 two first, to avoid warning for (a ? b++ : b++). */
3190 merge_tlist (&tmp_nosp, tmp_list2, 0);
3191 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3192 return;
3194 case PREDECREMENT_EXPR:
3195 case PREINCREMENT_EXPR:
3196 case POSTDECREMENT_EXPR:
3197 case POSTINCREMENT_EXPR:
3198 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3199 return;
3201 case MODIFY_EXPR:
3202 tmp_before = tmp_nosp = tmp_list3 = 0;
3203 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3204 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3205 /* Expressions inside the LHS are not ordered wrt. the sequence points
3206 in the RHS. Example:
3207 *a = (a++, 2)
3208 Despite the fact that the modification of "a" is in the before_sp
3209 list (tmp_before), it conflicts with the use of "a" in the LHS.
3210 We can handle this by adding the contents of tmp_list3
3211 to those of tmp_before, and redoing the collision warnings for that
3212 list. */
3213 add_tlist (&tmp_before, tmp_list3, x, 1);
3214 warn_for_collisions (tmp_before);
3215 /* Exclude the LHS itself here; we first have to merge it into the
3216 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3217 didn't exclude the LHS, we'd get it twice, once as a read and once
3218 as a write. */
3219 add_tlist (pno_sp, tmp_list3, x, 0);
3220 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3222 merge_tlist (pbefore_sp, tmp_before, 0);
3223 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3224 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3225 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3226 return;
3228 case CALL_EXPR:
3229 /* We need to warn about conflicts among arguments and conflicts between
3230 args and the function address. Side effects of the function address,
3231 however, are not ordered by the sequence point of the call. */
3233 call_expr_arg_iterator iter;
3234 tree arg;
3235 tmp_before = tmp_nosp = 0;
3236 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3237 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3239 tmp_list2 = tmp_list3 = 0;
3240 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3241 merge_tlist (&tmp_list3, tmp_list2, 0);
3242 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3244 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3245 warn_for_collisions (tmp_before);
3246 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3247 return;
3250 case TREE_LIST:
3251 /* Scan all the list, e.g. indices of multi dimensional array. */
3252 while (x)
3254 tmp_before = tmp_nosp = 0;
3255 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3256 merge_tlist (&tmp_nosp, tmp_before, 0);
3257 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3258 x = TREE_CHAIN (x);
3260 return;
3262 case SAVE_EXPR:
3264 struct tlist_cache *t;
3265 for (t = save_expr_cache; t; t = t->next)
3266 if (candidate_equal_p (t->expr, x))
3267 break;
3269 if (!t)
3271 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3272 t->next = save_expr_cache;
3273 t->expr = x;
3274 save_expr_cache = t;
3276 tmp_before = tmp_nosp = 0;
3277 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3278 warn_for_collisions (tmp_nosp);
3280 tmp_list3 = 0;
3281 merge_tlist (&tmp_list3, tmp_nosp, 0);
3282 t->cache_before_sp = tmp_before;
3283 t->cache_after_sp = tmp_list3;
3285 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3286 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3287 return;
3290 case ADDR_EXPR:
3291 x = TREE_OPERAND (x, 0);
3292 if (DECL_P (x))
3293 return;
3294 writer = 0;
3295 goto restart;
3297 default:
3298 /* For other expressions, simply recurse on their operands.
3299 Manual tail recursion for unary expressions.
3300 Other non-expressions need not be processed. */
3301 if (cl == tcc_unary)
3303 x = TREE_OPERAND (x, 0);
3304 writer = 0;
3305 goto restart;
3307 else if (IS_EXPR_CODE_CLASS (cl))
3309 int lp;
3310 int max = TREE_OPERAND_LENGTH (x);
3311 for (lp = 0; lp < max; lp++)
3313 tmp_before = tmp_nosp = 0;
3314 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3315 merge_tlist (&tmp_nosp, tmp_before, 0);
3316 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3319 return;
3323 /* Try to warn for undefined behavior in EXPR due to missing sequence
3324 points. */
3326 DEBUG_FUNCTION void
3327 verify_sequence_points (tree expr)
3329 struct tlist *before_sp = 0, *after_sp = 0;
3331 warned_ids = 0;
3332 save_expr_cache = 0;
3333 if (tlist_firstobj == 0)
3335 gcc_obstack_init (&tlist_obstack);
3336 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3339 verify_tree (expr, &before_sp, &after_sp, 0);
3340 warn_for_collisions (after_sp);
3341 obstack_free (&tlist_obstack, tlist_firstobj);
3344 /* Validate the expression after `case' and apply default promotions. */
3346 static tree
3347 check_case_value (location_t loc, tree value)
3349 if (value == NULL_TREE)
3350 return value;
3352 if (TREE_CODE (value) == INTEGER_CST)
3353 /* Promote char or short to int. */
3354 value = perform_integral_promotions (value);
3355 else if (value != error_mark_node)
3357 error_at (loc, "case label does not reduce to an integer constant");
3358 value = error_mark_node;
3361 constant_expression_warning (value);
3363 return value;
3366 /* See if the case values LOW and HIGH are in the range of the original
3367 type (i.e. before the default conversion to int) of the switch testing
3368 expression.
3369 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3370 the type before promoting it. CASE_LOW_P is a pointer to the lower
3371 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3372 if the case is not a case range.
3373 The caller has to make sure that we are not called with NULL for
3374 CASE_LOW_P (i.e. the default case).
3375 Returns true if the case label is in range of ORIG_TYPE (saturated or
3376 untouched) or false if the label is out of range. */
3378 static bool
3379 check_case_bounds (location_t loc, tree type, tree orig_type,
3380 tree *case_low_p, tree *case_high_p)
3382 tree min_value, max_value;
3383 tree case_low = *case_low_p;
3384 tree case_high = case_high_p ? *case_high_p : case_low;
3386 /* If there was a problem with the original type, do nothing. */
3387 if (orig_type == error_mark_node)
3388 return true;
3390 min_value = TYPE_MIN_VALUE (orig_type);
3391 max_value = TYPE_MAX_VALUE (orig_type);
3393 /* Case label is less than minimum for type. */
3394 if (tree_int_cst_compare (case_low, min_value) < 0
3395 && tree_int_cst_compare (case_high, min_value) < 0)
3397 warning_at (loc, 0, "case label value is less than minimum value "
3398 "for type");
3399 return false;
3402 /* Case value is greater than maximum for type. */
3403 if (tree_int_cst_compare (case_low, max_value) > 0
3404 && tree_int_cst_compare (case_high, max_value) > 0)
3406 warning_at (loc, 0, "case label value exceeds maximum value for type");
3407 return false;
3410 /* Saturate lower case label value to minimum. */
3411 if (tree_int_cst_compare (case_high, min_value) >= 0
3412 && tree_int_cst_compare (case_low, min_value) < 0)
3414 warning_at (loc, 0, "lower value in case label range"
3415 " less than minimum value for type");
3416 case_low = min_value;
3419 /* Saturate upper case label value to maximum. */
3420 if (tree_int_cst_compare (case_low, max_value) <= 0
3421 && tree_int_cst_compare (case_high, max_value) > 0)
3423 warning_at (loc, 0, "upper value in case label range"
3424 " exceeds maximum value for type");
3425 case_high = max_value;
3428 if (*case_low_p != case_low)
3429 *case_low_p = convert (type, case_low);
3430 if (case_high_p && *case_high_p != case_high)
3431 *case_high_p = convert (type, case_high);
3433 return true;
3436 /* Return an integer type with BITS bits of precision,
3437 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3439 tree
3440 c_common_type_for_size (unsigned int bits, int unsignedp)
3442 if (bits == TYPE_PRECISION (integer_type_node))
3443 return unsignedp ? unsigned_type_node : integer_type_node;
3445 if (bits == TYPE_PRECISION (signed_char_type_node))
3446 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3448 if (bits == TYPE_PRECISION (short_integer_type_node))
3449 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3451 if (bits == TYPE_PRECISION (long_integer_type_node))
3452 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3454 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3455 return (unsignedp ? long_long_unsigned_type_node
3456 : long_long_integer_type_node);
3458 if (int128_integer_type_node
3459 && bits == TYPE_PRECISION (int128_integer_type_node))
3460 return (unsignedp ? int128_unsigned_type_node
3461 : int128_integer_type_node);
3463 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3464 return (unsignedp ? widest_unsigned_literal_type_node
3465 : widest_integer_literal_type_node);
3467 if (bits <= TYPE_PRECISION (intQI_type_node))
3468 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3470 if (bits <= TYPE_PRECISION (intHI_type_node))
3471 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3473 if (bits <= TYPE_PRECISION (intSI_type_node))
3474 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3476 if (bits <= TYPE_PRECISION (intDI_type_node))
3477 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3479 return 0;
3482 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3483 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3484 and saturating if SATP is nonzero, otherwise not saturating. */
3486 tree
3487 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3488 int unsignedp, int satp)
3490 enum machine_mode mode;
3491 if (ibit == 0)
3492 mode = unsignedp ? UQQmode : QQmode;
3493 else
3494 mode = unsignedp ? UHAmode : HAmode;
3496 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3497 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3498 break;
3500 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3502 sorry ("GCC cannot support operators with integer types and "
3503 "fixed-point types that have too many integral and "
3504 "fractional bits together");
3505 return 0;
3508 return c_common_type_for_mode (mode, satp);
3511 /* Used for communication between c_common_type_for_mode and
3512 c_register_builtin_type. */
3513 tree registered_builtin_types;
3515 /* Return a data type that has machine mode MODE.
3516 If the mode is an integer,
3517 then UNSIGNEDP selects between signed and unsigned types.
3518 If the mode is a fixed-point mode,
3519 then UNSIGNEDP selects between saturating and nonsaturating types. */
3521 tree
3522 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3524 tree t;
3526 if (mode == TYPE_MODE (integer_type_node))
3527 return unsignedp ? unsigned_type_node : integer_type_node;
3529 if (mode == TYPE_MODE (signed_char_type_node))
3530 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3532 if (mode == TYPE_MODE (short_integer_type_node))
3533 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3535 if (mode == TYPE_MODE (long_integer_type_node))
3536 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3538 if (mode == TYPE_MODE (long_long_integer_type_node))
3539 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3541 if (int128_integer_type_node
3542 && mode == TYPE_MODE (int128_integer_type_node))
3543 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3545 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3546 return unsignedp ? widest_unsigned_literal_type_node
3547 : widest_integer_literal_type_node;
3549 if (mode == QImode)
3550 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3552 if (mode == HImode)
3553 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3555 if (mode == SImode)
3556 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3558 if (mode == DImode)
3559 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3561 #if HOST_BITS_PER_WIDE_INT >= 64
3562 if (mode == TYPE_MODE (intTI_type_node))
3563 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3564 #endif
3566 if (mode == TYPE_MODE (float_type_node))
3567 return float_type_node;
3569 if (mode == TYPE_MODE (double_type_node))
3570 return double_type_node;
3572 if (mode == TYPE_MODE (long_double_type_node))
3573 return long_double_type_node;
3575 if (mode == TYPE_MODE (void_type_node))
3576 return void_type_node;
3578 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3579 return (unsignedp
3580 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3581 : make_signed_type (GET_MODE_PRECISION (mode)));
3583 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3584 return (unsignedp
3585 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3586 : make_signed_type (GET_MODE_PRECISION (mode)));
3588 if (COMPLEX_MODE_P (mode))
3590 enum machine_mode inner_mode;
3591 tree inner_type;
3593 if (mode == TYPE_MODE (complex_float_type_node))
3594 return complex_float_type_node;
3595 if (mode == TYPE_MODE (complex_double_type_node))
3596 return complex_double_type_node;
3597 if (mode == TYPE_MODE (complex_long_double_type_node))
3598 return complex_long_double_type_node;
3600 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3601 return complex_integer_type_node;
3603 inner_mode = GET_MODE_INNER (mode);
3604 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3605 if (inner_type != NULL_TREE)
3606 return build_complex_type (inner_type);
3608 else if (VECTOR_MODE_P (mode))
3610 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3611 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3612 if (inner_type != NULL_TREE)
3613 return build_vector_type_for_mode (inner_type, mode);
3616 if (mode == TYPE_MODE (dfloat32_type_node))
3617 return dfloat32_type_node;
3618 if (mode == TYPE_MODE (dfloat64_type_node))
3619 return dfloat64_type_node;
3620 if (mode == TYPE_MODE (dfloat128_type_node))
3621 return dfloat128_type_node;
3623 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3625 if (mode == TYPE_MODE (short_fract_type_node))
3626 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3627 if (mode == TYPE_MODE (fract_type_node))
3628 return unsignedp ? sat_fract_type_node : fract_type_node;
3629 if (mode == TYPE_MODE (long_fract_type_node))
3630 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3631 if (mode == TYPE_MODE (long_long_fract_type_node))
3632 return unsignedp ? sat_long_long_fract_type_node
3633 : long_long_fract_type_node;
3635 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3636 return unsignedp ? sat_unsigned_short_fract_type_node
3637 : unsigned_short_fract_type_node;
3638 if (mode == TYPE_MODE (unsigned_fract_type_node))
3639 return unsignedp ? sat_unsigned_fract_type_node
3640 : unsigned_fract_type_node;
3641 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3642 return unsignedp ? sat_unsigned_long_fract_type_node
3643 : unsigned_long_fract_type_node;
3644 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3645 return unsignedp ? sat_unsigned_long_long_fract_type_node
3646 : unsigned_long_long_fract_type_node;
3648 if (mode == TYPE_MODE (short_accum_type_node))
3649 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3650 if (mode == TYPE_MODE (accum_type_node))
3651 return unsignedp ? sat_accum_type_node : accum_type_node;
3652 if (mode == TYPE_MODE (long_accum_type_node))
3653 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3654 if (mode == TYPE_MODE (long_long_accum_type_node))
3655 return unsignedp ? sat_long_long_accum_type_node
3656 : long_long_accum_type_node;
3658 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3659 return unsignedp ? sat_unsigned_short_accum_type_node
3660 : unsigned_short_accum_type_node;
3661 if (mode == TYPE_MODE (unsigned_accum_type_node))
3662 return unsignedp ? sat_unsigned_accum_type_node
3663 : unsigned_accum_type_node;
3664 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3665 return unsignedp ? sat_unsigned_long_accum_type_node
3666 : unsigned_long_accum_type_node;
3667 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3668 return unsignedp ? sat_unsigned_long_long_accum_type_node
3669 : unsigned_long_long_accum_type_node;
3671 if (mode == QQmode)
3672 return unsignedp ? sat_qq_type_node : qq_type_node;
3673 if (mode == HQmode)
3674 return unsignedp ? sat_hq_type_node : hq_type_node;
3675 if (mode == SQmode)
3676 return unsignedp ? sat_sq_type_node : sq_type_node;
3677 if (mode == DQmode)
3678 return unsignedp ? sat_dq_type_node : dq_type_node;
3679 if (mode == TQmode)
3680 return unsignedp ? sat_tq_type_node : tq_type_node;
3682 if (mode == UQQmode)
3683 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3684 if (mode == UHQmode)
3685 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3686 if (mode == USQmode)
3687 return unsignedp ? sat_usq_type_node : usq_type_node;
3688 if (mode == UDQmode)
3689 return unsignedp ? sat_udq_type_node : udq_type_node;
3690 if (mode == UTQmode)
3691 return unsignedp ? sat_utq_type_node : utq_type_node;
3693 if (mode == HAmode)
3694 return unsignedp ? sat_ha_type_node : ha_type_node;
3695 if (mode == SAmode)
3696 return unsignedp ? sat_sa_type_node : sa_type_node;
3697 if (mode == DAmode)
3698 return unsignedp ? sat_da_type_node : da_type_node;
3699 if (mode == TAmode)
3700 return unsignedp ? sat_ta_type_node : ta_type_node;
3702 if (mode == UHAmode)
3703 return unsignedp ? sat_uha_type_node : uha_type_node;
3704 if (mode == USAmode)
3705 return unsignedp ? sat_usa_type_node : usa_type_node;
3706 if (mode == UDAmode)
3707 return unsignedp ? sat_uda_type_node : uda_type_node;
3708 if (mode == UTAmode)
3709 return unsignedp ? sat_uta_type_node : uta_type_node;
3712 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3713 if (TYPE_MODE (TREE_VALUE (t)) == mode
3714 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3715 return TREE_VALUE (t);
3717 return 0;
3720 tree
3721 c_common_unsigned_type (tree type)
3723 return c_common_signed_or_unsigned_type (1, type);
3726 /* Return a signed type the same as TYPE in other respects. */
3728 tree
3729 c_common_signed_type (tree type)
3731 return c_common_signed_or_unsigned_type (0, type);
3734 /* Return a type the same as TYPE except unsigned or
3735 signed according to UNSIGNEDP. */
3737 tree
3738 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3740 tree type1;
3742 /* This block of code emulates the behavior of the old
3743 c_common_unsigned_type. In particular, it returns
3744 long_unsigned_type_node if passed a long, even when a int would
3745 have the same size. This is necessary for warnings to work
3746 correctly in archs where sizeof(int) == sizeof(long) */
3748 type1 = TYPE_MAIN_VARIANT (type);
3749 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3750 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3751 if (type1 == integer_type_node || type1 == unsigned_type_node)
3752 return unsignedp ? unsigned_type_node : integer_type_node;
3753 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3754 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3755 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3756 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3757 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3758 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3759 if (int128_integer_type_node
3760 && (type1 == int128_integer_type_node
3761 || type1 == int128_unsigned_type_node))
3762 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3763 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3764 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3765 #if HOST_BITS_PER_WIDE_INT >= 64
3766 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3767 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3768 #endif
3769 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3770 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3771 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3772 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3773 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3774 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3775 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3776 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3778 #define C_COMMON_FIXED_TYPES(NAME) \
3779 if (type1 == short_ ## NAME ## _type_node \
3780 || type1 == unsigned_short_ ## NAME ## _type_node) \
3781 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3782 : short_ ## NAME ## _type_node; \
3783 if (type1 == NAME ## _type_node \
3784 || type1 == unsigned_ ## NAME ## _type_node) \
3785 return unsignedp ? unsigned_ ## NAME ## _type_node \
3786 : NAME ## _type_node; \
3787 if (type1 == long_ ## NAME ## _type_node \
3788 || type1 == unsigned_long_ ## NAME ## _type_node) \
3789 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3790 : long_ ## NAME ## _type_node; \
3791 if (type1 == long_long_ ## NAME ## _type_node \
3792 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3793 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3794 : long_long_ ## NAME ## _type_node;
3796 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3797 if (type1 == NAME ## _type_node \
3798 || type1 == u ## NAME ## _type_node) \
3799 return unsignedp ? u ## NAME ## _type_node \
3800 : NAME ## _type_node;
3802 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3803 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3804 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3805 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3806 : sat_ ## short_ ## NAME ## _type_node; \
3807 if (type1 == sat_ ## NAME ## _type_node \
3808 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3809 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3810 : sat_ ## NAME ## _type_node; \
3811 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3812 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3813 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3814 : sat_ ## long_ ## NAME ## _type_node; \
3815 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3816 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3817 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3818 : sat_ ## long_long_ ## NAME ## _type_node;
3820 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3821 if (type1 == sat_ ## NAME ## _type_node \
3822 || type1 == sat_ ## u ## NAME ## _type_node) \
3823 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3824 : sat_ ## NAME ## _type_node;
3826 C_COMMON_FIXED_TYPES (fract);
3827 C_COMMON_FIXED_TYPES_SAT (fract);
3828 C_COMMON_FIXED_TYPES (accum);
3829 C_COMMON_FIXED_TYPES_SAT (accum);
3831 C_COMMON_FIXED_MODE_TYPES (qq);
3832 C_COMMON_FIXED_MODE_TYPES (hq);
3833 C_COMMON_FIXED_MODE_TYPES (sq);
3834 C_COMMON_FIXED_MODE_TYPES (dq);
3835 C_COMMON_FIXED_MODE_TYPES (tq);
3836 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3837 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3838 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3839 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3840 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3841 C_COMMON_FIXED_MODE_TYPES (ha);
3842 C_COMMON_FIXED_MODE_TYPES (sa);
3843 C_COMMON_FIXED_MODE_TYPES (da);
3844 C_COMMON_FIXED_MODE_TYPES (ta);
3845 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3846 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3847 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3848 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3850 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3851 the precision; they have precision set to match their range, but
3852 may use a wider mode to match an ABI. If we change modes, we may
3853 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3854 the precision as well, so as to yield correct results for
3855 bit-field types. C++ does not have these separate bit-field
3856 types, and producing a signed or unsigned variant of an
3857 ENUMERAL_TYPE may cause other problems as well. */
3859 if (!INTEGRAL_TYPE_P (type)
3860 || TYPE_UNSIGNED (type) == unsignedp)
3861 return type;
3863 #define TYPE_OK(node) \
3864 (TYPE_MODE (type) == TYPE_MODE (node) \
3865 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3866 if (TYPE_OK (signed_char_type_node))
3867 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3868 if (TYPE_OK (integer_type_node))
3869 return unsignedp ? unsigned_type_node : integer_type_node;
3870 if (TYPE_OK (short_integer_type_node))
3871 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3872 if (TYPE_OK (long_integer_type_node))
3873 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3874 if (TYPE_OK (long_long_integer_type_node))
3875 return (unsignedp ? long_long_unsigned_type_node
3876 : long_long_integer_type_node);
3877 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3878 return (unsignedp ? int128_unsigned_type_node
3879 : int128_integer_type_node);
3880 if (TYPE_OK (widest_integer_literal_type_node))
3881 return (unsignedp ? widest_unsigned_literal_type_node
3882 : widest_integer_literal_type_node);
3884 #if HOST_BITS_PER_WIDE_INT >= 64
3885 if (TYPE_OK (intTI_type_node))
3886 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3887 #endif
3888 if (TYPE_OK (intDI_type_node))
3889 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3890 if (TYPE_OK (intSI_type_node))
3891 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3892 if (TYPE_OK (intHI_type_node))
3893 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3894 if (TYPE_OK (intQI_type_node))
3895 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3896 #undef TYPE_OK
3898 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3901 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3903 tree
3904 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3906 /* Extended integer types of the same width as a standard type have
3907 lesser rank, so those of the same width as int promote to int or
3908 unsigned int and are valid for printf formats expecting int or
3909 unsigned int. To avoid such special cases, avoid creating
3910 extended integer types for bit-fields if a standard integer type
3911 is available. */
3912 if (width == TYPE_PRECISION (integer_type_node))
3913 return unsignedp ? unsigned_type_node : integer_type_node;
3914 if (width == TYPE_PRECISION (signed_char_type_node))
3915 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3916 if (width == TYPE_PRECISION (short_integer_type_node))
3917 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3918 if (width == TYPE_PRECISION (long_integer_type_node))
3919 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3920 if (width == TYPE_PRECISION (long_long_integer_type_node))
3921 return (unsignedp ? long_long_unsigned_type_node
3922 : long_long_integer_type_node);
3923 if (int128_integer_type_node
3924 && width == TYPE_PRECISION (int128_integer_type_node))
3925 return (unsignedp ? int128_unsigned_type_node
3926 : int128_integer_type_node);
3927 return build_nonstandard_integer_type (width, unsignedp);
3930 /* The C version of the register_builtin_type langhook. */
3932 void
3933 c_register_builtin_type (tree type, const char* name)
3935 tree decl;
3937 decl = build_decl (UNKNOWN_LOCATION,
3938 TYPE_DECL, get_identifier (name), type);
3939 DECL_ARTIFICIAL (decl) = 1;
3940 if (!TYPE_NAME (type))
3941 TYPE_NAME (type) = decl;
3942 pushdecl (decl);
3944 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3947 /* Print an error message for invalid operands to arith operation
3948 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3949 LOCATION is the location of the message. */
3951 void
3952 binary_op_error (location_t location, enum tree_code code,
3953 tree type0, tree type1)
3955 const char *opname;
3957 switch (code)
3959 case PLUS_EXPR:
3960 opname = "+"; break;
3961 case MINUS_EXPR:
3962 opname = "-"; break;
3963 case MULT_EXPR:
3964 opname = "*"; break;
3965 case MAX_EXPR:
3966 opname = "max"; break;
3967 case MIN_EXPR:
3968 opname = "min"; break;
3969 case EQ_EXPR:
3970 opname = "=="; break;
3971 case NE_EXPR:
3972 opname = "!="; break;
3973 case LE_EXPR:
3974 opname = "<="; break;
3975 case GE_EXPR:
3976 opname = ">="; break;
3977 case LT_EXPR:
3978 opname = "<"; break;
3979 case GT_EXPR:
3980 opname = ">"; break;
3981 case LSHIFT_EXPR:
3982 opname = "<<"; break;
3983 case RSHIFT_EXPR:
3984 opname = ">>"; break;
3985 case TRUNC_MOD_EXPR:
3986 case FLOOR_MOD_EXPR:
3987 opname = "%"; break;
3988 case TRUNC_DIV_EXPR:
3989 case FLOOR_DIV_EXPR:
3990 opname = "/"; break;
3991 case BIT_AND_EXPR:
3992 opname = "&"; break;
3993 case BIT_IOR_EXPR:
3994 opname = "|"; break;
3995 case TRUTH_ANDIF_EXPR:
3996 opname = "&&"; break;
3997 case TRUTH_ORIF_EXPR:
3998 opname = "||"; break;
3999 case BIT_XOR_EXPR:
4000 opname = "^"; break;
4001 default:
4002 gcc_unreachable ();
4004 error_at (location,
4005 "invalid operands to binary %s (have %qT and %qT)", opname,
4006 type0, type1);
4009 /* Given an expression as a tree, return its original type. Do this
4010 by stripping any conversion that preserves the sign and precision. */
4011 static tree
4012 expr_original_type (tree expr)
4014 STRIP_SIGN_NOPS (expr);
4015 return TREE_TYPE (expr);
4018 /* Subroutine of build_binary_op, used for comparison operations.
4019 See if the operands have both been converted from subword integer types
4020 and, if so, perhaps change them both back to their original type.
4021 This function is also responsible for converting the two operands
4022 to the proper common type for comparison.
4024 The arguments of this function are all pointers to local variables
4025 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4026 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4028 LOC is the location of the comparison.
4030 If this function returns nonzero, it means that the comparison has
4031 a constant value. What this function returns is an expression for
4032 that value. */
4034 tree
4035 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4036 tree *restype_ptr, enum tree_code *rescode_ptr)
4038 tree type;
4039 tree op0 = *op0_ptr;
4040 tree op1 = *op1_ptr;
4041 int unsignedp0, unsignedp1;
4042 int real1, real2;
4043 tree primop0, primop1;
4044 enum tree_code code = *rescode_ptr;
4046 /* Throw away any conversions to wider types
4047 already present in the operands. */
4049 primop0 = c_common_get_narrower (op0, &unsignedp0);
4050 primop1 = c_common_get_narrower (op1, &unsignedp1);
4052 /* If primopN is first sign-extended from primopN's precision to opN's
4053 precision, then zero-extended from opN's precision to
4054 *restype_ptr precision, shortenings might be invalid. */
4055 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4056 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4057 && !unsignedp0
4058 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4059 primop0 = op0;
4060 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4061 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4062 && !unsignedp1
4063 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4064 primop1 = op1;
4066 /* Handle the case that OP0 does not *contain* a conversion
4067 but it *requires* conversion to FINAL_TYPE. */
4069 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4070 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4071 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4072 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4074 /* If one of the operands must be floated, we cannot optimize. */
4075 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4076 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4078 /* If first arg is constant, swap the args (changing operation
4079 so value is preserved), for canonicalization. Don't do this if
4080 the second arg is 0. */
4082 if (TREE_CONSTANT (primop0)
4083 && !integer_zerop (primop1) && !real_zerop (primop1)
4084 && !fixed_zerop (primop1))
4086 tree tem = primop0;
4087 int temi = unsignedp0;
4088 primop0 = primop1;
4089 primop1 = tem;
4090 tem = op0;
4091 op0 = op1;
4092 op1 = tem;
4093 *op0_ptr = op0;
4094 *op1_ptr = op1;
4095 unsignedp0 = unsignedp1;
4096 unsignedp1 = temi;
4097 temi = real1;
4098 real1 = real2;
4099 real2 = temi;
4101 switch (code)
4103 case LT_EXPR:
4104 code = GT_EXPR;
4105 break;
4106 case GT_EXPR:
4107 code = LT_EXPR;
4108 break;
4109 case LE_EXPR:
4110 code = GE_EXPR;
4111 break;
4112 case GE_EXPR:
4113 code = LE_EXPR;
4114 break;
4115 default:
4116 break;
4118 *rescode_ptr = code;
4121 /* If comparing an integer against a constant more bits wide,
4122 maybe we can deduce a value of 1 or 0 independent of the data.
4123 Or else truncate the constant now
4124 rather than extend the variable at run time.
4126 This is only interesting if the constant is the wider arg.
4127 Also, it is not safe if the constant is unsigned and the
4128 variable arg is signed, since in this case the variable
4129 would be sign-extended and then regarded as unsigned.
4130 Our technique fails in this case because the lowest/highest
4131 possible unsigned results don't follow naturally from the
4132 lowest/highest possible values of the variable operand.
4133 For just EQ_EXPR and NE_EXPR there is another technique that
4134 could be used: see if the constant can be faithfully represented
4135 in the other operand's type, by truncating it and reextending it
4136 and see if that preserves the constant's value. */
4138 if (!real1 && !real2
4139 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4140 && TREE_CODE (primop1) == INTEGER_CST
4141 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4143 int min_gt, max_gt, min_lt, max_lt;
4144 tree maxval, minval;
4145 /* 1 if comparison is nominally unsigned. */
4146 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4147 tree val;
4149 type = c_common_signed_or_unsigned_type (unsignedp0,
4150 TREE_TYPE (primop0));
4152 maxval = TYPE_MAX_VALUE (type);
4153 minval = TYPE_MIN_VALUE (type);
4155 if (unsignedp && !unsignedp0)
4156 *restype_ptr = c_common_signed_type (*restype_ptr);
4158 if (TREE_TYPE (primop1) != *restype_ptr)
4160 /* Convert primop1 to target type, but do not introduce
4161 additional overflow. We know primop1 is an int_cst. */
4162 primop1 = force_fit_type (*restype_ptr,
4163 wide_int::from
4164 (primop1,
4165 TYPE_PRECISION (*restype_ptr),
4166 TYPE_SIGN (TREE_TYPE (primop1))),
4167 0, TREE_OVERFLOW (primop1));
4169 if (type != *restype_ptr)
4171 minval = convert (*restype_ptr, minval);
4172 maxval = convert (*restype_ptr, maxval);
4175 min_gt = tree_int_cst_lt (primop1, minval);
4176 max_gt = tree_int_cst_lt (primop1, maxval);
4177 min_lt = tree_int_cst_lt (minval, primop1);
4178 max_lt = tree_int_cst_lt (maxval, primop1);
4180 val = 0;
4181 /* This used to be a switch, but Genix compiler can't handle that. */
4182 if (code == NE_EXPR)
4184 if (max_lt || min_gt)
4185 val = truthvalue_true_node;
4187 else if (code == EQ_EXPR)
4189 if (max_lt || min_gt)
4190 val = truthvalue_false_node;
4192 else if (code == LT_EXPR)
4194 if (max_lt)
4195 val = truthvalue_true_node;
4196 if (!min_lt)
4197 val = truthvalue_false_node;
4199 else if (code == GT_EXPR)
4201 if (min_gt)
4202 val = truthvalue_true_node;
4203 if (!max_gt)
4204 val = truthvalue_false_node;
4206 else if (code == LE_EXPR)
4208 if (!max_gt)
4209 val = truthvalue_true_node;
4210 if (min_gt)
4211 val = truthvalue_false_node;
4213 else if (code == GE_EXPR)
4215 if (!min_lt)
4216 val = truthvalue_true_node;
4217 if (max_lt)
4218 val = truthvalue_false_node;
4221 /* If primop0 was sign-extended and unsigned comparison specd,
4222 we did a signed comparison above using the signed type bounds.
4223 But the comparison we output must be unsigned.
4225 Also, for inequalities, VAL is no good; but if the signed
4226 comparison had *any* fixed result, it follows that the
4227 unsigned comparison just tests the sign in reverse
4228 (positive values are LE, negative ones GE).
4229 So we can generate an unsigned comparison
4230 against an extreme value of the signed type. */
4232 if (unsignedp && !unsignedp0)
4234 if (val != 0)
4235 switch (code)
4237 case LT_EXPR:
4238 case GE_EXPR:
4239 primop1 = TYPE_MIN_VALUE (type);
4240 val = 0;
4241 break;
4243 case LE_EXPR:
4244 case GT_EXPR:
4245 primop1 = TYPE_MAX_VALUE (type);
4246 val = 0;
4247 break;
4249 default:
4250 break;
4252 type = c_common_unsigned_type (type);
4255 if (TREE_CODE (primop0) != INTEGER_CST)
4257 if (val == truthvalue_false_node)
4258 warning_at (loc, OPT_Wtype_limits,
4259 "comparison is always false due to limited range of data type");
4260 if (val == truthvalue_true_node)
4261 warning_at (loc, OPT_Wtype_limits,
4262 "comparison is always true due to limited range of data type");
4265 if (val != 0)
4267 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4268 if (TREE_SIDE_EFFECTS (primop0))
4269 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4270 return val;
4273 /* Value is not predetermined, but do the comparison
4274 in the type of the operand that is not constant.
4275 TYPE is already properly set. */
4278 /* If either arg is decimal float and the other is float, find the
4279 proper common type to use for comparison. */
4280 else if (real1 && real2
4281 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4282 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4283 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4285 else if (real1 && real2
4286 && (TYPE_PRECISION (TREE_TYPE (primop0))
4287 == TYPE_PRECISION (TREE_TYPE (primop1))))
4288 type = TREE_TYPE (primop0);
4290 /* If args' natural types are both narrower than nominal type
4291 and both extend in the same manner, compare them
4292 in the type of the wider arg.
4293 Otherwise must actually extend both to the nominal
4294 common type lest different ways of extending
4295 alter the result.
4296 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4298 else if (unsignedp0 == unsignedp1 && real1 == real2
4299 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4300 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4302 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4303 type = c_common_signed_or_unsigned_type (unsignedp0
4304 || TYPE_UNSIGNED (*restype_ptr),
4305 type);
4306 /* Make sure shorter operand is extended the right way
4307 to match the longer operand. */
4308 primop0
4309 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4310 TREE_TYPE (primop0)),
4311 primop0);
4312 primop1
4313 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4314 TREE_TYPE (primop1)),
4315 primop1);
4317 else
4319 /* Here we must do the comparison on the nominal type
4320 using the args exactly as we received them. */
4321 type = *restype_ptr;
4322 primop0 = op0;
4323 primop1 = op1;
4325 if (!real1 && !real2 && integer_zerop (primop1)
4326 && TYPE_UNSIGNED (*restype_ptr))
4328 tree value = 0;
4329 /* All unsigned values are >= 0, so we warn. However,
4330 if OP0 is a constant that is >= 0, the signedness of
4331 the comparison isn't an issue, so suppress the
4332 warning. */
4333 bool warn =
4334 warn_type_limits && !in_system_header_at (loc)
4335 && !(TREE_CODE (primop0) == INTEGER_CST
4336 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4337 primop0)))
4338 /* Do not warn for enumeration types. */
4339 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4341 switch (code)
4343 case GE_EXPR:
4344 if (warn)
4345 warning_at (loc, OPT_Wtype_limits,
4346 "comparison of unsigned expression >= 0 is always true");
4347 value = truthvalue_true_node;
4348 break;
4350 case LT_EXPR:
4351 if (warn)
4352 warning_at (loc, OPT_Wtype_limits,
4353 "comparison of unsigned expression < 0 is always false");
4354 value = truthvalue_false_node;
4355 break;
4357 default:
4358 break;
4361 if (value != 0)
4363 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4364 if (TREE_SIDE_EFFECTS (primop0))
4365 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4366 primop0, value);
4367 return value;
4372 *op0_ptr = convert (type, primop0);
4373 *op1_ptr = convert (type, primop1);
4375 *restype_ptr = truthvalue_type_node;
4377 return 0;
4380 /* Return a tree for the sum or difference (RESULTCODE says which)
4381 of pointer PTROP and integer INTOP. */
4383 tree
4384 pointer_int_sum (location_t loc, enum tree_code resultcode,
4385 tree ptrop, tree intop, bool complain)
4387 tree size_exp, ret;
4389 /* The result is a pointer of the same type that is being added. */
4390 tree result_type = TREE_TYPE (ptrop);
4392 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4394 if (complain && warn_pointer_arith)
4395 pedwarn (loc, OPT_Wpointer_arith,
4396 "pointer of type %<void *%> used in arithmetic");
4397 else if (!complain)
4398 return error_mark_node;
4399 size_exp = integer_one_node;
4401 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4403 if (complain && warn_pointer_arith)
4404 pedwarn (loc, OPT_Wpointer_arith,
4405 "pointer to a function used in arithmetic");
4406 else if (!complain)
4407 return error_mark_node;
4408 size_exp = integer_one_node;
4410 else
4411 size_exp = size_in_bytes (TREE_TYPE (result_type));
4413 /* We are manipulating pointer values, so we don't need to warn
4414 about relying on undefined signed overflow. We disable the
4415 warning here because we use integer types so fold won't know that
4416 they are really pointers. */
4417 fold_defer_overflow_warnings ();
4419 /* If what we are about to multiply by the size of the elements
4420 contains a constant term, apply distributive law
4421 and multiply that constant term separately.
4422 This helps produce common subexpressions. */
4423 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4424 && !TREE_CONSTANT (intop)
4425 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4426 && TREE_CONSTANT (size_exp)
4427 /* If the constant comes from pointer subtraction,
4428 skip this optimization--it would cause an error. */
4429 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4430 /* If the constant is unsigned, and smaller than the pointer size,
4431 then we must skip this optimization. This is because it could cause
4432 an overflow error if the constant is negative but INTOP is not. */
4433 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4434 || (TYPE_PRECISION (TREE_TYPE (intop))
4435 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4437 enum tree_code subcode = resultcode;
4438 tree int_type = TREE_TYPE (intop);
4439 if (TREE_CODE (intop) == MINUS_EXPR)
4440 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4441 /* Convert both subexpression types to the type of intop,
4442 because weird cases involving pointer arithmetic
4443 can result in a sum or difference with different type args. */
4444 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4445 subcode, ptrop,
4446 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4447 intop = convert (int_type, TREE_OPERAND (intop, 0));
4450 /* Convert the integer argument to a type the same size as sizetype
4451 so the multiply won't overflow spuriously. */
4452 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4453 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4454 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4455 TYPE_UNSIGNED (sizetype)), intop);
4457 /* Replace the integer argument with a suitable product by the object size.
4458 Do this multiplication as signed, then convert to the appropriate type
4459 for the pointer operation and disregard an overflow that occurred only
4460 because of the sign-extension change in the latter conversion. */
4462 tree t = build_binary_op (loc,
4463 MULT_EXPR, intop,
4464 convert (TREE_TYPE (intop), size_exp), 1);
4465 intop = convert (sizetype, t);
4466 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4467 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4470 /* Create the sum or difference. */
4471 if (resultcode == MINUS_EXPR)
4472 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4474 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4476 fold_undefer_and_ignore_overflow_warnings ();
4478 return ret;
4481 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4482 and if NON_CONST is known not to be permitted in an evaluated part
4483 of a constant expression. */
4485 tree
4486 c_wrap_maybe_const (tree expr, bool non_const)
4488 bool nowarning = TREE_NO_WARNING (expr);
4489 location_t loc = EXPR_LOCATION (expr);
4491 /* This should never be called for C++. */
4492 if (c_dialect_cxx ())
4493 gcc_unreachable ();
4495 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4496 STRIP_TYPE_NOPS (expr);
4497 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4498 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4499 if (nowarning)
4500 TREE_NO_WARNING (expr) = 1;
4501 protected_set_expr_location (expr, loc);
4503 return expr;
4506 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4507 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4508 around the SAVE_EXPR if needed so that c_fully_fold does not need
4509 to look inside SAVE_EXPRs. */
4511 tree
4512 c_save_expr (tree expr)
4514 bool maybe_const = true;
4515 if (c_dialect_cxx ())
4516 return save_expr (expr);
4517 expr = c_fully_fold (expr, false, &maybe_const);
4518 expr = save_expr (expr);
4519 if (!maybe_const)
4520 expr = c_wrap_maybe_const (expr, true);
4521 return expr;
4524 /* Return whether EXPR is a declaration whose address can never be
4525 NULL. */
4527 bool
4528 decl_with_nonnull_addr_p (const_tree expr)
4530 return (DECL_P (expr)
4531 && (TREE_CODE (expr) == PARM_DECL
4532 || TREE_CODE (expr) == LABEL_DECL
4533 || !DECL_WEAK (expr)));
4536 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4537 or for an `if' or `while' statement or ?..: exp. It should already
4538 have been validated to be of suitable type; otherwise, a bad
4539 diagnostic may result.
4541 The EXPR is located at LOCATION.
4543 This preparation consists of taking the ordinary
4544 representation of an expression expr and producing a valid tree
4545 boolean expression describing whether expr is nonzero. We could
4546 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4547 but we optimize comparisons, &&, ||, and !.
4549 The resulting type should always be `truthvalue_type_node'. */
4551 tree
4552 c_common_truthvalue_conversion (location_t location, tree expr)
4554 switch (TREE_CODE (expr))
4556 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4557 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4558 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4559 case ORDERED_EXPR: case UNORDERED_EXPR:
4560 if (TREE_TYPE (expr) == truthvalue_type_node)
4561 return expr;
4562 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4563 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4564 goto ret;
4566 case TRUTH_ANDIF_EXPR:
4567 case TRUTH_ORIF_EXPR:
4568 case TRUTH_AND_EXPR:
4569 case TRUTH_OR_EXPR:
4570 case TRUTH_XOR_EXPR:
4571 if (TREE_TYPE (expr) == truthvalue_type_node)
4572 return expr;
4573 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4574 c_common_truthvalue_conversion (location,
4575 TREE_OPERAND (expr, 0)),
4576 c_common_truthvalue_conversion (location,
4577 TREE_OPERAND (expr, 1)));
4578 goto ret;
4580 case TRUTH_NOT_EXPR:
4581 if (TREE_TYPE (expr) == truthvalue_type_node)
4582 return expr;
4583 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4584 c_common_truthvalue_conversion (location,
4585 TREE_OPERAND (expr, 0)));
4586 goto ret;
4588 case ERROR_MARK:
4589 return expr;
4591 case INTEGER_CST:
4592 return integer_zerop (expr) ? truthvalue_false_node
4593 : truthvalue_true_node;
4595 case REAL_CST:
4596 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4597 ? truthvalue_true_node
4598 : truthvalue_false_node;
4600 case FIXED_CST:
4601 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4602 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4603 ? truthvalue_true_node
4604 : truthvalue_false_node;
4606 case FUNCTION_DECL:
4607 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4608 /* Fall through. */
4610 case ADDR_EXPR:
4612 tree inner = TREE_OPERAND (expr, 0);
4613 if (decl_with_nonnull_addr_p (inner))
4615 /* Common Ada/Pascal programmer's mistake. */
4616 warning_at (location,
4617 OPT_Waddress,
4618 "the address of %qD will always evaluate as %<true%>",
4619 inner);
4620 return truthvalue_true_node;
4622 break;
4625 case COMPLEX_EXPR:
4626 expr = build_binary_op (EXPR_LOCATION (expr),
4627 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4628 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4629 c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 0)),
4631 c_common_truthvalue_conversion (location,
4632 TREE_OPERAND (expr, 1)),
4634 goto ret;
4636 case NEGATE_EXPR:
4637 case ABS_EXPR:
4638 case FLOAT_EXPR:
4639 case EXCESS_PRECISION_EXPR:
4640 /* These don't change whether an object is nonzero or zero. */
4641 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4643 case LROTATE_EXPR:
4644 case RROTATE_EXPR:
4645 /* These don't change whether an object is zero or nonzero, but
4646 we can't ignore them if their second arg has side-effects. */
4647 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4649 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4650 TREE_OPERAND (expr, 1),
4651 c_common_truthvalue_conversion
4652 (location, TREE_OPERAND (expr, 0)));
4653 goto ret;
4655 else
4656 return c_common_truthvalue_conversion (location,
4657 TREE_OPERAND (expr, 0));
4659 case COND_EXPR:
4660 /* Distribute the conversion into the arms of a COND_EXPR. */
4661 if (c_dialect_cxx ())
4663 tree op1 = TREE_OPERAND (expr, 1);
4664 tree op2 = TREE_OPERAND (expr, 2);
4665 /* In C++ one of the arms might have void type if it is throw. */
4666 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4667 op1 = c_common_truthvalue_conversion (location, op1);
4668 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4669 op2 = c_common_truthvalue_conversion (location, op2);
4670 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4671 TREE_OPERAND (expr, 0), op1, op2);
4672 goto ret;
4674 else
4676 /* Folding will happen later for C. */
4677 expr = build3 (COND_EXPR, truthvalue_type_node,
4678 TREE_OPERAND (expr, 0),
4679 c_common_truthvalue_conversion (location,
4680 TREE_OPERAND (expr, 1)),
4681 c_common_truthvalue_conversion (location,
4682 TREE_OPERAND (expr, 2)));
4683 goto ret;
4686 CASE_CONVERT:
4688 tree totype = TREE_TYPE (expr);
4689 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4691 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4692 since that affects how `default_conversion' will behave. */
4693 if (TREE_CODE (totype) == REFERENCE_TYPE
4694 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4695 break;
4696 /* Don't strip a conversion from C++0x scoped enum, since they
4697 don't implicitly convert to other types. */
4698 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4699 && ENUM_IS_SCOPED (fromtype))
4700 break;
4701 /* If this isn't narrowing the argument, we can ignore it. */
4702 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4703 return c_common_truthvalue_conversion (location,
4704 TREE_OPERAND (expr, 0));
4706 break;
4708 case MODIFY_EXPR:
4709 if (!TREE_NO_WARNING (expr)
4710 && warn_parentheses)
4712 warning (OPT_Wparentheses,
4713 "suggest parentheses around assignment used as truth value");
4714 TREE_NO_WARNING (expr) = 1;
4716 break;
4718 default:
4719 break;
4722 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4724 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4725 expr = (build_binary_op
4726 (EXPR_LOCATION (expr),
4727 (TREE_SIDE_EFFECTS (expr)
4728 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4729 c_common_truthvalue_conversion
4730 (location,
4731 build_unary_op (location, REALPART_EXPR, t, 0)),
4732 c_common_truthvalue_conversion
4733 (location,
4734 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4735 0));
4736 goto ret;
4739 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4741 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4742 FCONST0 (TYPE_MODE
4743 (TREE_TYPE (expr))));
4744 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4746 else
4747 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4749 ret:
4750 protected_set_expr_location (expr, location);
4751 return expr;
4754 static void def_builtin_1 (enum built_in_function fncode,
4755 const char *name,
4756 enum built_in_class fnclass,
4757 tree fntype, tree libtype,
4758 bool both_p, bool fallback_p, bool nonansi_p,
4759 tree fnattrs, bool implicit_p);
4762 /* Apply the TYPE_QUALS to the new DECL. */
4764 void
4765 c_apply_type_quals_to_decl (int type_quals, tree decl)
4767 tree type = TREE_TYPE (decl);
4769 if (type == error_mark_node)
4770 return;
4772 if ((type_quals & TYPE_QUAL_CONST)
4773 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4774 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4775 constructor can produce constant init, so rely on cp_finish_decl to
4776 clear TREE_READONLY if the variable has non-constant init. */
4777 TREE_READONLY (decl) = 1;
4778 if (type_quals & TYPE_QUAL_VOLATILE)
4780 TREE_SIDE_EFFECTS (decl) = 1;
4781 TREE_THIS_VOLATILE (decl) = 1;
4783 if (type_quals & TYPE_QUAL_RESTRICT)
4785 while (type && TREE_CODE (type) == ARRAY_TYPE)
4786 /* Allow 'restrict' on arrays of pointers.
4787 FIXME currently we just ignore it. */
4788 type = TREE_TYPE (type);
4789 if (!type
4790 || !POINTER_TYPE_P (type)
4791 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4792 error ("invalid use of %<restrict%>");
4796 /* Hash function for the problem of multiple type definitions in
4797 different files. This must hash all types that will compare
4798 equal via comptypes to the same value. In practice it hashes
4799 on some of the simple stuff and leaves the details to comptypes. */
4801 static hashval_t
4802 c_type_hash (const void *p)
4804 int n_elements;
4805 int shift, size;
4806 const_tree const t = (const_tree) p;
4807 tree t2;
4808 switch (TREE_CODE (t))
4810 /* For pointers, hash on pointee type plus some swizzling. */
4811 case POINTER_TYPE:
4812 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4813 /* Hash on number of elements and total size. */
4814 case ENUMERAL_TYPE:
4815 shift = 3;
4816 t2 = TYPE_VALUES (t);
4817 break;
4818 case RECORD_TYPE:
4819 shift = 0;
4820 t2 = TYPE_FIELDS (t);
4821 break;
4822 case QUAL_UNION_TYPE:
4823 shift = 1;
4824 t2 = TYPE_FIELDS (t);
4825 break;
4826 case UNION_TYPE:
4827 shift = 2;
4828 t2 = TYPE_FIELDS (t);
4829 break;
4830 default:
4831 gcc_unreachable ();
4833 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4834 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4835 n_elements = list_length (t2);
4836 /* We might have a VLA here. */
4837 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4838 size = 0;
4839 else
4840 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4841 return ((size << 24) | (n_elements << shift));
4844 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4846 /* Return the typed-based alias set for T, which may be an expression
4847 or a type. Return -1 if we don't do anything special. */
4849 alias_set_type
4850 c_common_get_alias_set (tree t)
4852 tree u;
4853 PTR *slot;
4855 /* For VLAs, use the alias set of the element type rather than the
4856 default of alias set 0 for types compared structurally. */
4857 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4859 if (TREE_CODE (t) == ARRAY_TYPE)
4860 return get_alias_set (TREE_TYPE (t));
4861 return -1;
4864 /* Permit type-punning when accessing a union, provided the access
4865 is directly through the union. For example, this code does not
4866 permit taking the address of a union member and then storing
4867 through it. Even the type-punning allowed here is a GCC
4868 extension, albeit a common and useful one; the C standard says
4869 that such accesses have implementation-defined behavior. */
4870 for (u = t;
4871 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4872 u = TREE_OPERAND (u, 0))
4873 if (TREE_CODE (u) == COMPONENT_REF
4874 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4875 return 0;
4877 /* That's all the expressions we handle specially. */
4878 if (!TYPE_P (t))
4879 return -1;
4881 /* The C standard guarantees that any object may be accessed via an
4882 lvalue that has character type. */
4883 if (t == char_type_node
4884 || t == signed_char_type_node
4885 || t == unsigned_char_type_node)
4886 return 0;
4888 /* The C standard specifically allows aliasing between signed and
4889 unsigned variants of the same type. We treat the signed
4890 variant as canonical. */
4891 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4893 tree t1 = c_common_signed_type (t);
4895 /* t1 == t can happen for boolean nodes which are always unsigned. */
4896 if (t1 != t)
4897 return get_alias_set (t1);
4900 /* Handle the case of multiple type nodes referring to "the same" type,
4901 which occurs with IMA. These share an alias set. FIXME: Currently only
4902 C90 is handled. (In C99 type compatibility is not transitive, which
4903 complicates things mightily. The alias set splay trees can theoretically
4904 represent this, but insertion is tricky when you consider all the
4905 different orders things might arrive in.) */
4907 if (c_language != clk_c || flag_isoc99)
4908 return -1;
4910 /* Save time if there's only one input file. */
4911 if (num_in_fnames == 1)
4912 return -1;
4914 /* Pointers need special handling if they point to any type that
4915 needs special handling (below). */
4916 if (TREE_CODE (t) == POINTER_TYPE)
4918 tree t2;
4919 /* Find bottom type under any nested POINTERs. */
4920 for (t2 = TREE_TYPE (t);
4921 TREE_CODE (t2) == POINTER_TYPE;
4922 t2 = TREE_TYPE (t2))
4924 if (TREE_CODE (t2) != RECORD_TYPE
4925 && TREE_CODE (t2) != ENUMERAL_TYPE
4926 && TREE_CODE (t2) != QUAL_UNION_TYPE
4927 && TREE_CODE (t2) != UNION_TYPE)
4928 return -1;
4929 if (TYPE_SIZE (t2) == 0)
4930 return -1;
4932 /* These are the only cases that need special handling. */
4933 if (TREE_CODE (t) != RECORD_TYPE
4934 && TREE_CODE (t) != ENUMERAL_TYPE
4935 && TREE_CODE (t) != QUAL_UNION_TYPE
4936 && TREE_CODE (t) != UNION_TYPE
4937 && TREE_CODE (t) != POINTER_TYPE)
4938 return -1;
4939 /* Undefined? */
4940 if (TYPE_SIZE (t) == 0)
4941 return -1;
4943 /* Look up t in hash table. Only one of the compatible types within each
4944 alias set is recorded in the table. */
4945 if (!type_hash_table)
4946 type_hash_table = htab_create_ggc (1021, c_type_hash,
4947 (htab_eq) lang_hooks.types_compatible_p,
4948 NULL);
4949 slot = htab_find_slot (type_hash_table, t, INSERT);
4950 if (*slot != NULL)
4952 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4953 return TYPE_ALIAS_SET ((tree)*slot);
4955 else
4956 /* Our caller will assign and record (in t) a new alias set; all we need
4957 to do is remember t in the hash table. */
4958 *slot = t;
4960 return -1;
4963 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4964 the IS_SIZEOF parameter indicates which operator is being applied.
4965 The COMPLAIN flag controls whether we should diagnose possibly
4966 ill-formed constructs or not. LOC is the location of the SIZEOF or
4967 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4968 a type in any context should be returned, rather than the normal
4969 alignment for that type. */
4971 tree
4972 c_sizeof_or_alignof_type (location_t loc,
4973 tree type, bool is_sizeof, bool min_alignof,
4974 int complain)
4976 const char *op_name;
4977 tree value = NULL;
4978 enum tree_code type_code = TREE_CODE (type);
4980 op_name = is_sizeof ? "sizeof" : "__alignof__";
4982 if (type_code == FUNCTION_TYPE)
4984 if (is_sizeof)
4986 if (complain && warn_pointer_arith)
4987 pedwarn (loc, OPT_Wpointer_arith,
4988 "invalid application of %<sizeof%> to a function type");
4989 else if (!complain)
4990 return error_mark_node;
4991 value = size_one_node;
4993 else
4995 if (complain)
4997 if (c_dialect_cxx ())
4998 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4999 "%<alignof%> applied to a function type");
5000 else
5001 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5002 "%<_Alignof%> applied to a function type");
5004 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5007 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5009 if (type_code == VOID_TYPE
5010 && complain && warn_pointer_arith)
5011 pedwarn (loc, OPT_Wpointer_arith,
5012 "invalid application of %qs to a void type", op_name);
5013 else if (!complain)
5014 return error_mark_node;
5015 value = size_one_node;
5017 else if (!COMPLETE_TYPE_P (type)
5018 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5020 if (complain)
5021 error_at (loc, "invalid application of %qs to incomplete type %qT",
5022 op_name, type);
5023 return error_mark_node;
5025 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5026 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5028 if (complain)
5029 error_at (loc, "invalid application of %qs to array type %qT of "
5030 "incomplete element type", op_name, type);
5031 return error_mark_node;
5033 else
5035 if (is_sizeof)
5036 /* Convert in case a char is more than one unit. */
5037 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5038 size_int (TYPE_PRECISION (char_type_node)
5039 / BITS_PER_UNIT));
5040 else if (min_alignof)
5041 value = size_int (min_align_of_type (type));
5042 else
5043 value = size_int (TYPE_ALIGN_UNIT (type));
5046 /* VALUE will have the middle-end integer type sizetype.
5047 However, we should really return a value of type `size_t',
5048 which is just a typedef for an ordinary integer type. */
5049 value = fold_convert_loc (loc, size_type_node, value);
5051 return value;
5054 /* Implement the __alignof keyword: Return the minimum required
5055 alignment of EXPR, measured in bytes. For VAR_DECLs,
5056 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5057 from an "aligned" __attribute__ specification). LOC is the
5058 location of the ALIGNOF operator. */
5060 tree
5061 c_alignof_expr (location_t loc, tree expr)
5063 tree t;
5065 if (VAR_OR_FUNCTION_DECL_P (expr))
5066 t = size_int (DECL_ALIGN_UNIT (expr));
5068 else if (TREE_CODE (expr) == COMPONENT_REF
5069 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5071 error_at (loc, "%<__alignof%> applied to a bit-field");
5072 t = size_one_node;
5074 else if (TREE_CODE (expr) == COMPONENT_REF
5075 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5076 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5078 else if (TREE_CODE (expr) == INDIRECT_REF)
5080 tree t = TREE_OPERAND (expr, 0);
5081 tree best = t;
5082 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5084 while (CONVERT_EXPR_P (t)
5085 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5087 int thisalign;
5089 t = TREE_OPERAND (t, 0);
5090 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5091 if (thisalign > bestalign)
5092 best = t, bestalign = thisalign;
5094 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5096 else
5097 return c_alignof (loc, TREE_TYPE (expr));
5099 return fold_convert_loc (loc, size_type_node, t);
5102 /* Handle C and C++ default attributes. */
5104 enum built_in_attribute
5106 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5107 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5108 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5109 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5110 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5111 #include "builtin-attrs.def"
5112 #undef DEF_ATTR_NULL_TREE
5113 #undef DEF_ATTR_INT
5114 #undef DEF_ATTR_STRING
5115 #undef DEF_ATTR_IDENT
5116 #undef DEF_ATTR_TREE_LIST
5117 ATTR_LAST
5120 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5122 static void c_init_attributes (void);
5124 enum c_builtin_type
5126 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5127 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5128 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5129 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5130 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5131 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5132 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5133 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5134 ARG6) NAME,
5135 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5136 ARG6, ARG7) NAME,
5137 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5138 ARG6, ARG7, ARG8) NAME,
5139 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5140 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5141 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5142 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5143 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5144 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5145 NAME,
5146 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5147 #include "builtin-types.def"
5148 #undef DEF_PRIMITIVE_TYPE
5149 #undef DEF_FUNCTION_TYPE_0
5150 #undef DEF_FUNCTION_TYPE_1
5151 #undef DEF_FUNCTION_TYPE_2
5152 #undef DEF_FUNCTION_TYPE_3
5153 #undef DEF_FUNCTION_TYPE_4
5154 #undef DEF_FUNCTION_TYPE_5
5155 #undef DEF_FUNCTION_TYPE_6
5156 #undef DEF_FUNCTION_TYPE_7
5157 #undef DEF_FUNCTION_TYPE_8
5158 #undef DEF_FUNCTION_TYPE_VAR_0
5159 #undef DEF_FUNCTION_TYPE_VAR_1
5160 #undef DEF_FUNCTION_TYPE_VAR_2
5161 #undef DEF_FUNCTION_TYPE_VAR_3
5162 #undef DEF_FUNCTION_TYPE_VAR_4
5163 #undef DEF_FUNCTION_TYPE_VAR_5
5164 #undef DEF_POINTER_TYPE
5165 BT_LAST
5168 typedef enum c_builtin_type builtin_type;
5170 /* A temporary array for c_common_nodes_and_builtins. Used in
5171 communication with def_fn_type. */
5172 static tree builtin_types[(int) BT_LAST + 1];
5174 /* A helper function for c_common_nodes_and_builtins. Build function type
5175 for DEF with return type RET and N arguments. If VAR is true, then the
5176 function should be variadic after those N arguments.
5178 Takes special care not to ICE if any of the types involved are
5179 error_mark_node, which indicates that said type is not in fact available
5180 (see builtin_type_for_size). In which case the function type as a whole
5181 should be error_mark_node. */
5183 static void
5184 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5186 tree t;
5187 tree *args = XALLOCAVEC (tree, n);
5188 va_list list;
5189 int i;
5191 va_start (list, n);
5192 for (i = 0; i < n; ++i)
5194 builtin_type a = (builtin_type) va_arg (list, int);
5195 t = builtin_types[a];
5196 if (t == error_mark_node)
5197 goto egress;
5198 args[i] = t;
5201 t = builtin_types[ret];
5202 if (t == error_mark_node)
5203 goto egress;
5204 if (var)
5205 t = build_varargs_function_type_array (t, n, args);
5206 else
5207 t = build_function_type_array (t, n, args);
5209 egress:
5210 builtin_types[def] = t;
5211 va_end (list);
5214 /* Build builtin functions common to both C and C++ language
5215 frontends. */
5217 static void
5218 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5220 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5221 builtin_types[ENUM] = VALUE;
5222 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5223 def_fn_type (ENUM, RETURN, 0, 0);
5224 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5225 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5226 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5227 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5228 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5229 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5230 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5231 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5232 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5233 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5234 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5235 ARG6) \
5236 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5237 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5238 ARG6, ARG7) \
5239 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5240 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5241 ARG6, ARG7, ARG8) \
5242 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5243 ARG7, ARG8);
5244 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5245 def_fn_type (ENUM, RETURN, 1, 0);
5246 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5247 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5248 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5249 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5250 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5251 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5252 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5253 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5254 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5255 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5256 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5257 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5259 #include "builtin-types.def"
5261 #undef DEF_PRIMITIVE_TYPE
5262 #undef DEF_FUNCTION_TYPE_0
5263 #undef DEF_FUNCTION_TYPE_1
5264 #undef DEF_FUNCTION_TYPE_2
5265 #undef DEF_FUNCTION_TYPE_3
5266 #undef DEF_FUNCTION_TYPE_4
5267 #undef DEF_FUNCTION_TYPE_5
5268 #undef DEF_FUNCTION_TYPE_6
5269 #undef DEF_FUNCTION_TYPE_7
5270 #undef DEF_FUNCTION_TYPE_8
5271 #undef DEF_FUNCTION_TYPE_VAR_0
5272 #undef DEF_FUNCTION_TYPE_VAR_1
5273 #undef DEF_FUNCTION_TYPE_VAR_2
5274 #undef DEF_FUNCTION_TYPE_VAR_3
5275 #undef DEF_FUNCTION_TYPE_VAR_4
5276 #undef DEF_FUNCTION_TYPE_VAR_5
5277 #undef DEF_POINTER_TYPE
5278 builtin_types[(int) BT_LAST] = NULL_TREE;
5280 c_init_attributes ();
5282 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5283 NONANSI_P, ATTRS, IMPLICIT, COND) \
5284 if (NAME && COND) \
5285 def_builtin_1 (ENUM, NAME, CLASS, \
5286 builtin_types[(int) TYPE], \
5287 builtin_types[(int) LIBTYPE], \
5288 BOTH_P, FALLBACK_P, NONANSI_P, \
5289 built_in_attributes[(int) ATTRS], IMPLICIT);
5290 #include "builtins.def"
5291 #undef DEF_BUILTIN
5293 targetm.init_builtins ();
5295 build_common_builtin_nodes ();
5297 if (flag_cilkplus)
5298 cilk_init_builtins ();
5301 /* Like get_identifier, but avoid warnings about null arguments when
5302 the argument may be NULL for targets where GCC lacks stdint.h type
5303 information. */
5305 static inline tree
5306 c_get_ident (const char *id)
5308 return get_identifier (id);
5311 /* Build tree nodes and builtin functions common to both C and C++ language
5312 frontends. */
5314 void
5315 c_common_nodes_and_builtins (void)
5317 int char16_type_size;
5318 int char32_type_size;
5319 int wchar_type_size;
5320 tree array_domain_type;
5321 tree va_list_ref_type_node;
5322 tree va_list_arg_type_node;
5324 build_common_tree_nodes (flag_signed_char, flag_short_double);
5326 /* Define `int' and `char' first so that dbx will output them first. */
5327 record_builtin_type (RID_INT, NULL, integer_type_node);
5328 record_builtin_type (RID_CHAR, "char", char_type_node);
5330 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5331 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5332 but not C. Are the conditionals here needed? */
5333 if (c_dialect_cxx ())
5334 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5335 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5336 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5337 record_builtin_type (RID_MAX, "long unsigned int",
5338 long_unsigned_type_node);
5339 if (int128_integer_type_node != NULL_TREE)
5341 record_builtin_type (RID_INT128, "__int128",
5342 int128_integer_type_node);
5343 record_builtin_type (RID_MAX, "__int128 unsigned",
5344 int128_unsigned_type_node);
5346 if (c_dialect_cxx ())
5347 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5348 record_builtin_type (RID_MAX, "long long int",
5349 long_long_integer_type_node);
5350 record_builtin_type (RID_MAX, "long long unsigned int",
5351 long_long_unsigned_type_node);
5352 if (c_dialect_cxx ())
5353 record_builtin_type (RID_MAX, "long long unsigned",
5354 long_long_unsigned_type_node);
5355 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5356 record_builtin_type (RID_MAX, "short unsigned int",
5357 short_unsigned_type_node);
5358 if (c_dialect_cxx ())
5359 record_builtin_type (RID_MAX, "unsigned short",
5360 short_unsigned_type_node);
5362 /* Define both `signed char' and `unsigned char'. */
5363 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5364 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5366 /* These are types that c_common_type_for_size and
5367 c_common_type_for_mode use. */
5368 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5369 TYPE_DECL, NULL_TREE,
5370 intQI_type_node));
5371 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5372 TYPE_DECL, NULL_TREE,
5373 intHI_type_node));
5374 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5375 TYPE_DECL, NULL_TREE,
5376 intSI_type_node));
5377 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5378 TYPE_DECL, NULL_TREE,
5379 intDI_type_node));
5380 #if HOST_BITS_PER_WIDE_INT >= 64
5381 if (targetm.scalar_mode_supported_p (TImode))
5382 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL,
5384 get_identifier ("__int128_t"),
5385 intTI_type_node));
5386 #endif
5387 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5388 TYPE_DECL, NULL_TREE,
5389 unsigned_intQI_type_node));
5390 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5391 TYPE_DECL, NULL_TREE,
5392 unsigned_intHI_type_node));
5393 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5394 TYPE_DECL, NULL_TREE,
5395 unsigned_intSI_type_node));
5396 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5397 TYPE_DECL, NULL_TREE,
5398 unsigned_intDI_type_node));
5399 #if HOST_BITS_PER_WIDE_INT >= 64
5400 if (targetm.scalar_mode_supported_p (TImode))
5401 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5402 TYPE_DECL,
5403 get_identifier ("__uint128_t"),
5404 unsigned_intTI_type_node));
5405 #endif
5407 /* Create the widest literal types. */
5408 widest_integer_literal_type_node
5409 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5410 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5411 TYPE_DECL, NULL_TREE,
5412 widest_integer_literal_type_node));
5414 widest_unsigned_literal_type_node
5415 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5416 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5417 TYPE_DECL, NULL_TREE,
5418 widest_unsigned_literal_type_node));
5420 signed_size_type_node = c_common_signed_type (size_type_node);
5422 pid_type_node =
5423 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5425 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5426 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5427 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5429 /* Only supported decimal floating point extension if the target
5430 actually supports underlying modes. */
5431 if (targetm.scalar_mode_supported_p (SDmode)
5432 && targetm.scalar_mode_supported_p (DDmode)
5433 && targetm.scalar_mode_supported_p (TDmode))
5435 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5436 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5437 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5440 if (targetm.fixed_point_supported_p ())
5442 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5443 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5444 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5445 record_builtin_type (RID_MAX, "long long _Fract",
5446 long_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "unsigned short _Fract",
5448 unsigned_short_fract_type_node);
5449 record_builtin_type (RID_MAX, "unsigned _Fract",
5450 unsigned_fract_type_node);
5451 record_builtin_type (RID_MAX, "unsigned long _Fract",
5452 unsigned_long_fract_type_node);
5453 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5454 unsigned_long_long_fract_type_node);
5455 record_builtin_type (RID_MAX, "_Sat short _Fract",
5456 sat_short_fract_type_node);
5457 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5458 record_builtin_type (RID_MAX, "_Sat long _Fract",
5459 sat_long_fract_type_node);
5460 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5461 sat_long_long_fract_type_node);
5462 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5463 sat_unsigned_short_fract_type_node);
5464 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5465 sat_unsigned_fract_type_node);
5466 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5467 sat_unsigned_long_fract_type_node);
5468 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5469 sat_unsigned_long_long_fract_type_node);
5470 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5471 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5472 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5473 record_builtin_type (RID_MAX, "long long _Accum",
5474 long_long_accum_type_node);
5475 record_builtin_type (RID_MAX, "unsigned short _Accum",
5476 unsigned_short_accum_type_node);
5477 record_builtin_type (RID_MAX, "unsigned _Accum",
5478 unsigned_accum_type_node);
5479 record_builtin_type (RID_MAX, "unsigned long _Accum",
5480 unsigned_long_accum_type_node);
5481 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5482 unsigned_long_long_accum_type_node);
5483 record_builtin_type (RID_MAX, "_Sat short _Accum",
5484 sat_short_accum_type_node);
5485 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5486 record_builtin_type (RID_MAX, "_Sat long _Accum",
5487 sat_long_accum_type_node);
5488 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5489 sat_long_long_accum_type_node);
5490 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5491 sat_unsigned_short_accum_type_node);
5492 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5493 sat_unsigned_accum_type_node);
5494 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5495 sat_unsigned_long_accum_type_node);
5496 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5497 sat_unsigned_long_long_accum_type_node);
5501 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5502 TYPE_DECL,
5503 get_identifier ("complex int"),
5504 complex_integer_type_node));
5505 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5506 TYPE_DECL,
5507 get_identifier ("complex float"),
5508 complex_float_type_node));
5509 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5510 TYPE_DECL,
5511 get_identifier ("complex double"),
5512 complex_double_type_node));
5513 lang_hooks.decls.pushdecl
5514 (build_decl (UNKNOWN_LOCATION,
5515 TYPE_DECL, get_identifier ("complex long double"),
5516 complex_long_double_type_node));
5518 if (c_dialect_cxx ())
5519 /* For C++, make fileptr_type_node a distinct void * type until
5520 FILE type is defined. */
5521 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5523 record_builtin_type (RID_VOID, NULL, void_type_node);
5525 /* Set the TYPE_NAME for any variants that were built before
5526 record_builtin_type gave names to the built-in types. */
5528 tree void_name = TYPE_NAME (void_type_node);
5529 TYPE_NAME (void_type_node) = NULL_TREE;
5530 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5531 = void_name;
5532 TYPE_NAME (void_type_node) = void_name;
5535 void_list_node = build_void_list_node ();
5537 /* Make a type to be the domain of a few array types
5538 whose domains don't really matter.
5539 200 is small enough that it always fits in size_t
5540 and large enough that it can hold most function names for the
5541 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5542 array_domain_type = build_index_type (size_int (200));
5544 /* Make a type for arrays of characters.
5545 With luck nothing will ever really depend on the length of this
5546 array type. */
5547 char_array_type_node
5548 = build_array_type (char_type_node, array_domain_type);
5550 string_type_node = build_pointer_type (char_type_node);
5551 const_string_type_node
5552 = build_pointer_type (build_qualified_type
5553 (char_type_node, TYPE_QUAL_CONST));
5555 /* This is special for C++ so functions can be overloaded. */
5556 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5557 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5558 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5559 underlying_wchar_type_node = wchar_type_node;
5560 if (c_dialect_cxx ())
5562 if (TYPE_UNSIGNED (wchar_type_node))
5563 wchar_type_node = make_unsigned_type (wchar_type_size);
5564 else
5565 wchar_type_node = make_signed_type (wchar_type_size);
5566 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5569 /* This is for wide string constants. */
5570 wchar_array_type_node
5571 = build_array_type (wchar_type_node, array_domain_type);
5573 /* Define 'char16_t'. */
5574 char16_type_node = get_identifier (CHAR16_TYPE);
5575 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5576 char16_type_size = TYPE_PRECISION (char16_type_node);
5577 if (c_dialect_cxx ())
5579 char16_type_node = make_unsigned_type (char16_type_size);
5581 if (cxx_dialect >= cxx11)
5582 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5585 /* This is for UTF-16 string constants. */
5586 char16_array_type_node
5587 = build_array_type (char16_type_node, array_domain_type);
5589 /* Define 'char32_t'. */
5590 char32_type_node = get_identifier (CHAR32_TYPE);
5591 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5592 char32_type_size = TYPE_PRECISION (char32_type_node);
5593 if (c_dialect_cxx ())
5595 char32_type_node = make_unsigned_type (char32_type_size);
5597 if (cxx_dialect >= cxx11)
5598 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5601 /* This is for UTF-32 string constants. */
5602 char32_array_type_node
5603 = build_array_type (char32_type_node, array_domain_type);
5605 wint_type_node =
5606 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5608 intmax_type_node =
5609 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5610 uintmax_type_node =
5611 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5613 if (SIG_ATOMIC_TYPE)
5614 sig_atomic_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5616 if (INT8_TYPE)
5617 int8_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5619 if (INT16_TYPE)
5620 int16_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5622 if (INT32_TYPE)
5623 int32_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5625 if (INT64_TYPE)
5626 int64_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5628 if (UINT8_TYPE)
5629 uint8_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5631 if (UINT16_TYPE)
5632 c_uint16_type_node = uint16_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5634 if (UINT32_TYPE)
5635 c_uint32_type_node = uint32_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5637 if (UINT64_TYPE)
5638 c_uint64_type_node = uint64_type_node =
5639 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5640 if (INT_LEAST8_TYPE)
5641 int_least8_type_node =
5642 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5643 if (INT_LEAST16_TYPE)
5644 int_least16_type_node =
5645 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5646 if (INT_LEAST32_TYPE)
5647 int_least32_type_node =
5648 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5649 if (INT_LEAST64_TYPE)
5650 int_least64_type_node =
5651 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5652 if (UINT_LEAST8_TYPE)
5653 uint_least8_type_node =
5654 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5655 if (UINT_LEAST16_TYPE)
5656 uint_least16_type_node =
5657 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5658 if (UINT_LEAST32_TYPE)
5659 uint_least32_type_node =
5660 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5661 if (UINT_LEAST64_TYPE)
5662 uint_least64_type_node =
5663 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5664 if (INT_FAST8_TYPE)
5665 int_fast8_type_node =
5666 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5667 if (INT_FAST16_TYPE)
5668 int_fast16_type_node =
5669 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5670 if (INT_FAST32_TYPE)
5671 int_fast32_type_node =
5672 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5673 if (INT_FAST64_TYPE)
5674 int_fast64_type_node =
5675 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5676 if (UINT_FAST8_TYPE)
5677 uint_fast8_type_node =
5678 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5679 if (UINT_FAST16_TYPE)
5680 uint_fast16_type_node =
5681 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5682 if (UINT_FAST32_TYPE)
5683 uint_fast32_type_node =
5684 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5685 if (UINT_FAST64_TYPE)
5686 uint_fast64_type_node =
5687 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5688 if (INTPTR_TYPE)
5689 intptr_type_node =
5690 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5691 if (UINTPTR_TYPE)
5692 uintptr_type_node =
5693 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5695 default_function_type
5696 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5697 ptrdiff_type_node
5698 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5699 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5701 lang_hooks.decls.pushdecl
5702 (build_decl (UNKNOWN_LOCATION,
5703 TYPE_DECL, get_identifier ("__builtin_va_list"),
5704 va_list_type_node));
5705 if (targetm.enum_va_list_p)
5707 int l;
5708 const char *pname;
5709 tree ptype;
5711 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5713 lang_hooks.decls.pushdecl
5714 (build_decl (UNKNOWN_LOCATION,
5715 TYPE_DECL, get_identifier (pname),
5716 ptype));
5721 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5723 va_list_arg_type_node = va_list_ref_type_node =
5724 build_pointer_type (TREE_TYPE (va_list_type_node));
5726 else
5728 va_list_arg_type_node = va_list_type_node;
5729 va_list_ref_type_node = build_reference_type (va_list_type_node);
5732 if (!flag_preprocess_only)
5733 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5735 main_identifier_node = get_identifier ("main");
5737 /* Create the built-in __null node. It is important that this is
5738 not shared. */
5739 null_node = make_int_cst (1, 1);
5740 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5742 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5743 memset (builtin_types, 0, sizeof (builtin_types));
5746 /* The number of named compound-literals generated thus far. */
5747 static GTY(()) int compound_literal_number;
5749 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5751 void
5752 set_compound_literal_name (tree decl)
5754 char *name;
5755 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5756 compound_literal_number);
5757 compound_literal_number++;
5758 DECL_NAME (decl) = get_identifier (name);
5761 tree
5762 build_va_arg (location_t loc, tree expr, tree type)
5764 expr = build1 (VA_ARG_EXPR, type, expr);
5765 SET_EXPR_LOCATION (expr, loc);
5766 return expr;
5770 /* Linked list of disabled built-in functions. */
5772 typedef struct disabled_builtin
5774 const char *name;
5775 struct disabled_builtin *next;
5776 } disabled_builtin;
5777 static disabled_builtin *disabled_builtins = NULL;
5779 static bool builtin_function_disabled_p (const char *);
5781 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5782 begins with "__builtin_", give an error. */
5784 void
5785 disable_builtin_function (const char *name)
5787 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5788 error ("cannot disable built-in function %qs", name);
5789 else
5791 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5792 new_disabled_builtin->name = name;
5793 new_disabled_builtin->next = disabled_builtins;
5794 disabled_builtins = new_disabled_builtin;
5799 /* Return true if the built-in function NAME has been disabled, false
5800 otherwise. */
5802 static bool
5803 builtin_function_disabled_p (const char *name)
5805 disabled_builtin *p;
5806 for (p = disabled_builtins; p != NULL; p = p->next)
5808 if (strcmp (name, p->name) == 0)
5809 return true;
5811 return false;
5815 /* Worker for DEF_BUILTIN.
5816 Possibly define a builtin function with one or two names.
5817 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5818 nonansi_p and flag_no_nonansi_builtin. */
5820 static void
5821 def_builtin_1 (enum built_in_function fncode,
5822 const char *name,
5823 enum built_in_class fnclass,
5824 tree fntype, tree libtype,
5825 bool both_p, bool fallback_p, bool nonansi_p,
5826 tree fnattrs, bool implicit_p)
5828 tree decl;
5829 const char *libname;
5831 if (fntype == error_mark_node)
5832 return;
5834 gcc_assert ((!both_p && !fallback_p)
5835 || !strncmp (name, "__builtin_",
5836 strlen ("__builtin_")));
5838 libname = name + strlen ("__builtin_");
5839 decl = add_builtin_function (name, fntype, fncode, fnclass,
5840 (fallback_p ? libname : NULL),
5841 fnattrs);
5843 set_builtin_decl (fncode, decl, implicit_p);
5845 if (both_p
5846 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5847 && !(nonansi_p && flag_no_nonansi_builtin))
5848 add_builtin_function (libname, libtype, fncode, fnclass,
5849 NULL, fnattrs);
5852 /* Nonzero if the type T promotes to int. This is (nearly) the
5853 integral promotions defined in ISO C99 6.3.1.1/2. */
5855 bool
5856 c_promoting_integer_type_p (const_tree t)
5858 switch (TREE_CODE (t))
5860 case INTEGER_TYPE:
5861 return (TYPE_MAIN_VARIANT (t) == char_type_node
5862 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5863 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5864 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5865 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5866 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5868 case ENUMERAL_TYPE:
5869 /* ??? Technically all enumerations not larger than an int
5870 promote to an int. But this is used along code paths
5871 that only want to notice a size change. */
5872 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5874 case BOOLEAN_TYPE:
5875 return 1;
5877 default:
5878 return 0;
5882 /* Return 1 if PARMS specifies a fixed number of parameters
5883 and none of their types is affected by default promotions. */
5886 self_promoting_args_p (const_tree parms)
5888 const_tree t;
5889 for (t = parms; t; t = TREE_CHAIN (t))
5891 tree type = TREE_VALUE (t);
5893 if (type == error_mark_node)
5894 continue;
5896 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5897 return 0;
5899 if (type == 0)
5900 return 0;
5902 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5903 return 0;
5905 if (c_promoting_integer_type_p (type))
5906 return 0;
5908 return 1;
5911 /* Recursively remove any '*' or '&' operator from TYPE. */
5912 tree
5913 strip_pointer_operator (tree t)
5915 while (POINTER_TYPE_P (t))
5916 t = TREE_TYPE (t);
5917 return t;
5920 /* Recursively remove pointer or array type from TYPE. */
5921 tree
5922 strip_pointer_or_array_types (tree t)
5924 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5925 t = TREE_TYPE (t);
5926 return t;
5929 /* Used to compare case labels. K1 and K2 are actually tree nodes
5930 representing case labels, or NULL_TREE for a `default' label.
5931 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5932 K2, and 0 if K1 and K2 are equal. */
5935 case_compare (splay_tree_key k1, splay_tree_key k2)
5937 /* Consider a NULL key (such as arises with a `default' label) to be
5938 smaller than anything else. */
5939 if (!k1)
5940 return k2 ? -1 : 0;
5941 else if (!k2)
5942 return k1 ? 1 : 0;
5944 return tree_int_cst_compare ((tree) k1, (tree) k2);
5947 /* Process a case label, located at LOC, for the range LOW_VALUE
5948 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5949 then this case label is actually a `default' label. If only
5950 HIGH_VALUE is NULL_TREE, then case label was declared using the
5951 usual C/C++ syntax, rather than the GNU case range extension.
5952 CASES is a tree containing all the case ranges processed so far;
5953 COND is the condition for the switch-statement itself. Returns the
5954 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5955 is created. */
5957 tree
5958 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5959 tree low_value, tree high_value)
5961 tree type;
5962 tree label;
5963 tree case_label;
5964 splay_tree_node node;
5966 /* Create the LABEL_DECL itself. */
5967 label = create_artificial_label (loc);
5969 /* If there was an error processing the switch condition, bail now
5970 before we get more confused. */
5971 if (!cond || cond == error_mark_node)
5972 goto error_out;
5974 if ((low_value && TREE_TYPE (low_value)
5975 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5976 || (high_value && TREE_TYPE (high_value)
5977 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5979 error_at (loc, "pointers are not permitted as case values");
5980 goto error_out;
5983 /* Case ranges are a GNU extension. */
5984 if (high_value)
5985 pedwarn (loc, OPT_Wpedantic,
5986 "range expressions in switch statements are non-standard");
5988 type = TREE_TYPE (cond);
5989 if (low_value)
5991 low_value = check_case_value (loc, low_value);
5992 low_value = convert_and_check (loc, type, low_value);
5993 if (low_value == error_mark_node)
5994 goto error_out;
5996 if (high_value)
5998 high_value = check_case_value (loc, high_value);
5999 high_value = convert_and_check (loc, type, high_value);
6000 if (high_value == error_mark_node)
6001 goto error_out;
6004 if (low_value && high_value)
6006 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6007 really a case range, even though it was written that way.
6008 Remove the HIGH_VALUE to simplify later processing. */
6009 if (tree_int_cst_equal (low_value, high_value))
6010 high_value = NULL_TREE;
6011 else if (!tree_int_cst_lt (low_value, high_value))
6012 warning_at (loc, 0, "empty range specified");
6015 /* See if the case is in range of the type of the original testing
6016 expression. If both low_value and high_value are out of range,
6017 don't insert the case label and return NULL_TREE. */
6018 if (low_value
6019 && !check_case_bounds (loc, type, orig_type,
6020 &low_value, high_value ? &high_value : NULL))
6021 return NULL_TREE;
6023 /* Look up the LOW_VALUE in the table of case labels we already
6024 have. */
6025 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6026 /* If there was not an exact match, check for overlapping ranges.
6027 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6028 that's a `default' label and the only overlap is an exact match. */
6029 if (!node && (low_value || high_value))
6031 splay_tree_node low_bound;
6032 splay_tree_node high_bound;
6034 /* Even though there wasn't an exact match, there might be an
6035 overlap between this case range and another case range.
6036 Since we've (inductively) not allowed any overlapping case
6037 ranges, we simply need to find the greatest low case label
6038 that is smaller that LOW_VALUE, and the smallest low case
6039 label that is greater than LOW_VALUE. If there is an overlap
6040 it will occur in one of these two ranges. */
6041 low_bound = splay_tree_predecessor (cases,
6042 (splay_tree_key) low_value);
6043 high_bound = splay_tree_successor (cases,
6044 (splay_tree_key) low_value);
6046 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6047 the LOW_VALUE, so there is no need to check unless the
6048 LOW_BOUND is in fact itself a case range. */
6049 if (low_bound
6050 && CASE_HIGH ((tree) low_bound->value)
6051 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6052 low_value) >= 0)
6053 node = low_bound;
6054 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6055 range is bigger than the low end of the current range, so we
6056 are only interested if the current range is a real range, and
6057 not an ordinary case label. */
6058 else if (high_bound
6059 && high_value
6060 && (tree_int_cst_compare ((tree) high_bound->key,
6061 high_value)
6062 <= 0))
6063 node = high_bound;
6065 /* If there was an overlap, issue an error. */
6066 if (node)
6068 tree duplicate = CASE_LABEL ((tree) node->value);
6070 if (high_value)
6072 error_at (loc, "duplicate (or overlapping) case value");
6073 error_at (DECL_SOURCE_LOCATION (duplicate),
6074 "this is the first entry overlapping that value");
6076 else if (low_value)
6078 error_at (loc, "duplicate case value") ;
6079 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6081 else
6083 error_at (loc, "multiple default labels in one switch");
6084 error_at (DECL_SOURCE_LOCATION (duplicate),
6085 "this is the first default label");
6087 goto error_out;
6090 /* Add a CASE_LABEL to the statement-tree. */
6091 case_label = add_stmt (build_case_label (low_value, high_value, label));
6092 /* Register this case label in the splay tree. */
6093 splay_tree_insert (cases,
6094 (splay_tree_key) low_value,
6095 (splay_tree_value) case_label);
6097 return case_label;
6099 error_out:
6100 /* Add a label so that the back-end doesn't think that the beginning of
6101 the switch is unreachable. Note that we do not add a case label, as
6102 that just leads to duplicates and thence to failure later on. */
6103 if (!cases->root)
6105 tree t = create_artificial_label (loc);
6106 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6108 return error_mark_node;
6111 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6112 Used to verify that case values match up with enumerator values. */
6114 static void
6115 match_case_to_enum_1 (tree key, tree type, tree label)
6117 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6119 if (tree_fits_uhwi_p (key))
6120 print_dec (key, buf, UNSIGNED);
6121 else if (tree_fits_shwi_p (key))
6122 print_dec (key, buf, SIGNED);
6123 else
6124 print_hex (key, buf);
6126 if (TYPE_NAME (type) == 0)
6127 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6128 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6129 "case value %qs not in enumerated type",
6130 buf);
6131 else
6132 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6133 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6134 "case value %qs not in enumerated type %qT",
6135 buf, type);
6138 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6139 Used to verify that case values match up with enumerator values. */
6141 static int
6142 match_case_to_enum (splay_tree_node node, void *data)
6144 tree label = (tree) node->value;
6145 tree type = (tree) data;
6147 /* Skip default case. */
6148 if (!CASE_LOW (label))
6149 return 0;
6151 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6152 when we did our enum->case scan. Reset our scratch bit after. */
6153 if (!CASE_LOW_SEEN (label))
6154 match_case_to_enum_1 (CASE_LOW (label), type, label);
6155 else
6156 CASE_LOW_SEEN (label) = 0;
6158 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6159 not set, that means that CASE_HIGH did not appear when we did our
6160 enum->case scan. Reset our scratch bit after. */
6161 if (CASE_HIGH (label))
6163 if (!CASE_HIGH_SEEN (label))
6164 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6165 else
6166 CASE_HIGH_SEEN (label) = 0;
6169 return 0;
6172 /* Handle -Wswitch*. Called from the front end after parsing the
6173 switch construct. */
6174 /* ??? Should probably be somewhere generic, since other languages
6175 besides C and C++ would want this. At the moment, however, C/C++
6176 are the only tree-ssa languages that support enumerations at all,
6177 so the point is moot. */
6179 void
6180 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6181 tree type, tree cond)
6183 splay_tree_node default_node;
6184 splay_tree_node node;
6185 tree chain;
6187 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6188 return;
6190 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6191 if (!default_node)
6192 warning_at (switch_location, OPT_Wswitch_default,
6193 "switch missing default case");
6195 /* From here on, we only care about about enumerated types. */
6196 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6197 return;
6199 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6200 if (!warn_switch_enum && !warn_switch)
6201 return;
6203 /* Check the cases. Warn about case values which are not members of
6204 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6205 there is no default case, check that exactly all enumeration
6206 literals are covered by the cases. */
6208 /* Clearing COND if it is not an integer constant simplifies
6209 the tests inside the loop below. */
6210 if (TREE_CODE (cond) != INTEGER_CST)
6211 cond = NULL_TREE;
6213 /* The time complexity here is O(N*lg(N)) worst case, but for the
6214 common case of monotonically increasing enumerators, it is
6215 O(N), since the nature of the splay tree will keep the next
6216 element adjacent to the root at all times. */
6218 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6220 tree value = TREE_VALUE (chain);
6221 if (TREE_CODE (value) == CONST_DECL)
6222 value = DECL_INITIAL (value);
6223 node = splay_tree_lookup (cases, (splay_tree_key) value);
6224 if (node)
6226 /* Mark the CASE_LOW part of the case entry as seen. */
6227 tree label = (tree) node->value;
6228 CASE_LOW_SEEN (label) = 1;
6229 continue;
6232 /* Even though there wasn't an exact match, there might be a
6233 case range which includes the enumerator's value. */
6234 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6235 if (node && CASE_HIGH ((tree) node->value))
6237 tree label = (tree) node->value;
6238 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6239 if (cmp >= 0)
6241 /* If we match the upper bound exactly, mark the CASE_HIGH
6242 part of the case entry as seen. */
6243 if (cmp == 0)
6244 CASE_HIGH_SEEN (label) = 1;
6245 continue;
6249 /* We've now determined that this enumerated literal isn't
6250 handled by the case labels of the switch statement. */
6252 /* If the switch expression is a constant, we only really care
6253 about whether that constant is handled by the switch. */
6254 if (cond && tree_int_cst_compare (cond, value))
6255 continue;
6257 /* If there is a default_node, the only relevant option is
6258 Wswitch-enum. Otherwise, if both are enabled then we prefer
6259 to warn using -Wswitch because -Wswitch is enabled by -Wall
6260 while -Wswitch-enum is explicit. */
6261 warning_at (switch_location,
6262 (default_node || !warn_switch
6263 ? OPT_Wswitch_enum
6264 : OPT_Wswitch),
6265 "enumeration value %qE not handled in switch",
6266 TREE_PURPOSE (chain));
6269 /* Warn if there are case expressions that don't correspond to
6270 enumerators. This can occur since C and C++ don't enforce
6271 type-checking of assignments to enumeration variables.
6273 The time complexity here is now always O(N) worst case, since
6274 we should have marked both the lower bound and upper bound of
6275 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6276 above. This scan also resets those fields. */
6278 splay_tree_foreach (cases, match_case_to_enum, type);
6281 /* Finish an expression taking the address of LABEL (an
6282 IDENTIFIER_NODE). Returns an expression for the address.
6284 LOC is the location for the expression returned. */
6286 tree
6287 finish_label_address_expr (tree label, location_t loc)
6289 tree result;
6291 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6293 if (label == error_mark_node)
6294 return error_mark_node;
6296 label = lookup_label (label);
6297 if (label == NULL_TREE)
6298 result = null_pointer_node;
6299 else
6301 TREE_USED (label) = 1;
6302 result = build1 (ADDR_EXPR, ptr_type_node, label);
6303 /* The current function is not necessarily uninlinable.
6304 Computed gotos are incompatible with inlining, but the value
6305 here could be used only in a diagnostic, for example. */
6306 protected_set_expr_location (result, loc);
6309 return result;
6313 /* Given a boolean expression ARG, return a tree representing an increment
6314 or decrement (as indicated by CODE) of ARG. The front end must check for
6315 invalid cases (e.g., decrement in C++). */
6316 tree
6317 boolean_increment (enum tree_code code, tree arg)
6319 tree val;
6320 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6322 arg = stabilize_reference (arg);
6323 switch (code)
6325 case PREINCREMENT_EXPR:
6326 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6327 break;
6328 case POSTINCREMENT_EXPR:
6329 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6330 arg = save_expr (arg);
6331 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6332 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6333 break;
6334 case PREDECREMENT_EXPR:
6335 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6336 invert_truthvalue_loc (input_location, arg));
6337 break;
6338 case POSTDECREMENT_EXPR:
6339 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6340 invert_truthvalue_loc (input_location, arg));
6341 arg = save_expr (arg);
6342 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6343 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6344 break;
6345 default:
6346 gcc_unreachable ();
6348 TREE_SIDE_EFFECTS (val) = 1;
6349 return val;
6352 /* Built-in macros for stddef.h and stdint.h, that require macros
6353 defined in this file. */
6354 void
6355 c_stddef_cpp_builtins(void)
6357 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6358 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6359 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6360 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6361 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6362 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6363 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6364 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6365 if (SIG_ATOMIC_TYPE)
6366 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6367 if (INT8_TYPE)
6368 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6369 if (INT16_TYPE)
6370 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6371 if (INT32_TYPE)
6372 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6373 if (INT64_TYPE)
6374 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6375 if (UINT8_TYPE)
6376 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6377 if (UINT16_TYPE)
6378 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6379 if (UINT32_TYPE)
6380 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6381 if (UINT64_TYPE)
6382 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6383 if (INT_LEAST8_TYPE)
6384 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6385 if (INT_LEAST16_TYPE)
6386 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6387 if (INT_LEAST32_TYPE)
6388 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6389 if (INT_LEAST64_TYPE)
6390 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6391 if (UINT_LEAST8_TYPE)
6392 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6393 if (UINT_LEAST16_TYPE)
6394 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6395 if (UINT_LEAST32_TYPE)
6396 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6397 if (UINT_LEAST64_TYPE)
6398 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6399 if (INT_FAST8_TYPE)
6400 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6401 if (INT_FAST16_TYPE)
6402 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6403 if (INT_FAST32_TYPE)
6404 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6405 if (INT_FAST64_TYPE)
6406 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6407 if (UINT_FAST8_TYPE)
6408 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6409 if (UINT_FAST16_TYPE)
6410 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6411 if (UINT_FAST32_TYPE)
6412 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6413 if (UINT_FAST64_TYPE)
6414 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6415 if (INTPTR_TYPE)
6416 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6417 if (UINTPTR_TYPE)
6418 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6421 static void
6422 c_init_attributes (void)
6424 /* Fill in the built_in_attributes array. */
6425 #define DEF_ATTR_NULL_TREE(ENUM) \
6426 built_in_attributes[(int) ENUM] = NULL_TREE;
6427 #define DEF_ATTR_INT(ENUM, VALUE) \
6428 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6429 #define DEF_ATTR_STRING(ENUM, VALUE) \
6430 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431 #define DEF_ATTR_IDENT(ENUM, STRING) \
6432 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6433 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6434 built_in_attributes[(int) ENUM] \
6435 = tree_cons (built_in_attributes[(int) PURPOSE], \
6436 built_in_attributes[(int) VALUE], \
6437 built_in_attributes[(int) CHAIN]);
6438 #include "builtin-attrs.def"
6439 #undef DEF_ATTR_NULL_TREE
6440 #undef DEF_ATTR_INT
6441 #undef DEF_ATTR_IDENT
6442 #undef DEF_ATTR_TREE_LIST
6445 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6446 identifier as an argument, so the front end shouldn't look it up. */
6448 bool
6449 attribute_takes_identifier_p (const_tree attr_id)
6451 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6452 if (spec == NULL)
6453 /* Unknown attribute that we'll end up ignoring, return true so we
6454 don't complain about an identifier argument. */
6455 return true;
6456 else if (!strcmp ("mode", spec->name)
6457 || !strcmp ("format", spec->name)
6458 || !strcmp ("cleanup", spec->name))
6459 return true;
6460 else
6461 return targetm.attribute_takes_identifier_p (attr_id);
6464 /* Attribute handlers common to C front ends. */
6466 /* Handle a "packed" attribute; arguments as in
6467 struct attribute_spec.handler. */
6469 static tree
6470 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6471 int flags, bool *no_add_attrs)
6473 if (TYPE_P (*node))
6475 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6476 *node = build_variant_type_copy (*node);
6477 TYPE_PACKED (*node) = 1;
6479 else if (TREE_CODE (*node) == FIELD_DECL)
6481 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6482 /* Still pack bitfields. */
6483 && ! DECL_INITIAL (*node))
6484 warning (OPT_Wattributes,
6485 "%qE attribute ignored for field of type %qT",
6486 name, TREE_TYPE (*node));
6487 else
6488 DECL_PACKED (*node) = 1;
6490 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6491 used for DECL_REGISTER. It wouldn't mean anything anyway.
6492 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6493 that changes what the typedef is typing. */
6494 else
6496 warning (OPT_Wattributes, "%qE attribute ignored", name);
6497 *no_add_attrs = true;
6500 return NULL_TREE;
6503 /* Handle a "nocommon" attribute; arguments as in
6504 struct attribute_spec.handler. */
6506 static tree
6507 handle_nocommon_attribute (tree *node, tree name,
6508 tree ARG_UNUSED (args),
6509 int ARG_UNUSED (flags), bool *no_add_attrs)
6511 if (TREE_CODE (*node) == VAR_DECL)
6512 DECL_COMMON (*node) = 0;
6513 else
6515 warning (OPT_Wattributes, "%qE attribute ignored", name);
6516 *no_add_attrs = true;
6519 return NULL_TREE;
6522 /* Handle a "common" attribute; arguments as in
6523 struct attribute_spec.handler. */
6525 static tree
6526 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6527 int ARG_UNUSED (flags), bool *no_add_attrs)
6529 if (TREE_CODE (*node) == VAR_DECL)
6530 DECL_COMMON (*node) = 1;
6531 else
6533 warning (OPT_Wattributes, "%qE attribute ignored", name);
6534 *no_add_attrs = true;
6537 return NULL_TREE;
6540 /* Handle a "noreturn" attribute; arguments as in
6541 struct attribute_spec.handler. */
6543 static tree
6544 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6545 int ARG_UNUSED (flags), bool *no_add_attrs)
6547 tree type = TREE_TYPE (*node);
6549 /* See FIXME comment in c_common_attribute_table. */
6550 if (TREE_CODE (*node) == FUNCTION_DECL
6551 || objc_method_decl (TREE_CODE (*node)))
6552 TREE_THIS_VOLATILE (*node) = 1;
6553 else if (TREE_CODE (type) == POINTER_TYPE
6554 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6555 TREE_TYPE (*node)
6556 = (build_qualified_type
6557 (build_pointer_type
6558 (build_type_variant (TREE_TYPE (type),
6559 TYPE_READONLY (TREE_TYPE (type)), 1)),
6560 TYPE_QUALS (type)));
6561 else
6563 warning (OPT_Wattributes, "%qE attribute ignored", name);
6564 *no_add_attrs = true;
6567 return NULL_TREE;
6570 /* Handle a "hot" and attribute; arguments as in
6571 struct attribute_spec.handler. */
6573 static tree
6574 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6575 int ARG_UNUSED (flags), bool *no_add_attrs)
6577 if (TREE_CODE (*node) == FUNCTION_DECL
6578 || TREE_CODE (*node) == LABEL_DECL)
6580 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6582 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6583 "with attribute %qs", name, "cold");
6584 *no_add_attrs = true;
6586 /* Most of the rest of the hot processing is done later with
6587 lookup_attribute. */
6589 else
6591 warning (OPT_Wattributes, "%qE attribute ignored", name);
6592 *no_add_attrs = true;
6595 return NULL_TREE;
6598 /* Handle a "cold" and attribute; arguments as in
6599 struct attribute_spec.handler. */
6601 static tree
6602 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6603 int ARG_UNUSED (flags), bool *no_add_attrs)
6605 if (TREE_CODE (*node) == FUNCTION_DECL
6606 || TREE_CODE (*node) == LABEL_DECL)
6608 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6610 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6611 "with attribute %qs", name, "hot");
6612 *no_add_attrs = true;
6614 /* Most of the rest of the cold processing is done later with
6615 lookup_attribute. */
6617 else
6619 warning (OPT_Wattributes, "%qE attribute ignored", name);
6620 *no_add_attrs = true;
6623 return NULL_TREE;
6626 /* Handle a "no_sanitize_address" attribute; arguments as in
6627 struct attribute_spec.handler. */
6629 static tree
6630 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6631 bool *no_add_attrs)
6633 if (TREE_CODE (*node) != FUNCTION_DECL)
6635 warning (OPT_Wattributes, "%qE attribute ignored", name);
6636 *no_add_attrs = true;
6639 return NULL_TREE;
6642 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6643 struct attribute_spec.handler. */
6645 static tree
6646 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6647 bool *no_add_attrs)
6649 if (TREE_CODE (*node) != FUNCTION_DECL)
6650 warning (OPT_Wattributes, "%qE attribute ignored", name);
6651 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6652 DECL_ATTRIBUTES (*node)
6653 = tree_cons (get_identifier ("no_sanitize_address"),
6654 NULL_TREE, DECL_ATTRIBUTES (*node));
6655 *no_add_attrs = true;
6656 return NULL_TREE;
6659 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6660 struct attribute_spec.handler. */
6662 static tree
6663 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6664 bool *no_add_attrs)
6666 if (TREE_CODE (*node) != FUNCTION_DECL)
6668 warning (OPT_Wattributes, "%qE attribute ignored", name);
6669 *no_add_attrs = true;
6672 return NULL_TREE;
6675 /* Handle a "noinline" attribute; arguments as in
6676 struct attribute_spec.handler. */
6678 static tree
6679 handle_noinline_attribute (tree *node, tree name,
6680 tree ARG_UNUSED (args),
6681 int ARG_UNUSED (flags), bool *no_add_attrs)
6683 if (TREE_CODE (*node) == FUNCTION_DECL)
6685 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6687 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6688 "with attribute %qs", name, "always_inline");
6689 *no_add_attrs = true;
6691 else
6692 DECL_UNINLINABLE (*node) = 1;
6694 else
6696 warning (OPT_Wattributes, "%qE attribute ignored", name);
6697 *no_add_attrs = true;
6700 return NULL_TREE;
6703 /* Handle a "noclone" attribute; arguments as in
6704 struct attribute_spec.handler. */
6706 static tree
6707 handle_noclone_attribute (tree *node, tree name,
6708 tree ARG_UNUSED (args),
6709 int ARG_UNUSED (flags), bool *no_add_attrs)
6711 if (TREE_CODE (*node) != FUNCTION_DECL)
6713 warning (OPT_Wattributes, "%qE attribute ignored", name);
6714 *no_add_attrs = true;
6717 return NULL_TREE;
6720 /* Handle a "always_inline" attribute; arguments as in
6721 struct attribute_spec.handler. */
6723 static tree
6724 handle_always_inline_attribute (tree *node, tree name,
6725 tree ARG_UNUSED (args),
6726 int ARG_UNUSED (flags),
6727 bool *no_add_attrs)
6729 if (TREE_CODE (*node) == FUNCTION_DECL)
6731 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6733 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6734 "with %qs attribute", name, "noinline");
6735 *no_add_attrs = true;
6737 else
6738 /* Set the attribute and mark it for disregarding inline
6739 limits. */
6740 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6742 else
6744 warning (OPT_Wattributes, "%qE attribute ignored", name);
6745 *no_add_attrs = true;
6748 return NULL_TREE;
6751 /* Handle a "gnu_inline" attribute; arguments as in
6752 struct attribute_spec.handler. */
6754 static tree
6755 handle_gnu_inline_attribute (tree *node, tree name,
6756 tree ARG_UNUSED (args),
6757 int ARG_UNUSED (flags),
6758 bool *no_add_attrs)
6760 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6762 /* Do nothing else, just set the attribute. We'll get at
6763 it later with lookup_attribute. */
6765 else
6767 warning (OPT_Wattributes, "%qE attribute ignored", name);
6768 *no_add_attrs = true;
6771 return NULL_TREE;
6774 /* Handle a "leaf" attribute; arguments as in
6775 struct attribute_spec.handler. */
6777 static tree
6778 handle_leaf_attribute (tree *node, tree name,
6779 tree ARG_UNUSED (args),
6780 int ARG_UNUSED (flags), bool *no_add_attrs)
6782 if (TREE_CODE (*node) != FUNCTION_DECL)
6784 warning (OPT_Wattributes, "%qE attribute ignored", name);
6785 *no_add_attrs = true;
6787 if (!TREE_PUBLIC (*node))
6789 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6790 *no_add_attrs = true;
6793 return NULL_TREE;
6796 /* Handle an "artificial" attribute; arguments as in
6797 struct attribute_spec.handler. */
6799 static tree
6800 handle_artificial_attribute (tree *node, tree name,
6801 tree ARG_UNUSED (args),
6802 int ARG_UNUSED (flags),
6803 bool *no_add_attrs)
6805 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6807 /* Do nothing else, just set the attribute. We'll get at
6808 it later with lookup_attribute. */
6810 else
6812 warning (OPT_Wattributes, "%qE attribute ignored", name);
6813 *no_add_attrs = true;
6816 return NULL_TREE;
6819 /* Handle a "flatten" attribute; arguments as in
6820 struct attribute_spec.handler. */
6822 static tree
6823 handle_flatten_attribute (tree *node, tree name,
6824 tree args ATTRIBUTE_UNUSED,
6825 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6827 if (TREE_CODE (*node) == FUNCTION_DECL)
6828 /* Do nothing else, just set the attribute. We'll get at
6829 it later with lookup_attribute. */
6831 else
6833 warning (OPT_Wattributes, "%qE attribute ignored", name);
6834 *no_add_attrs = true;
6837 return NULL_TREE;
6840 /* Handle a "warning" or "error" attribute; arguments as in
6841 struct attribute_spec.handler. */
6843 static tree
6844 handle_error_attribute (tree *node, tree name, tree args,
6845 int ARG_UNUSED (flags), bool *no_add_attrs)
6847 if (TREE_CODE (*node) == FUNCTION_DECL
6848 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6849 /* Do nothing else, just set the attribute. We'll get at
6850 it later with lookup_attribute. */
6852 else
6854 warning (OPT_Wattributes, "%qE attribute ignored", name);
6855 *no_add_attrs = true;
6858 return NULL_TREE;
6861 /* Handle a "used" attribute; arguments as in
6862 struct attribute_spec.handler. */
6864 static tree
6865 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6866 int ARG_UNUSED (flags), bool *no_add_attrs)
6868 tree node = *pnode;
6870 if (TREE_CODE (node) == FUNCTION_DECL
6871 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6872 || (TREE_CODE (node) == TYPE_DECL))
6874 TREE_USED (node) = 1;
6875 DECL_PRESERVE_P (node) = 1;
6876 if (TREE_CODE (node) == VAR_DECL)
6877 DECL_READ_P (node) = 1;
6879 else
6881 warning (OPT_Wattributes, "%qE attribute ignored", name);
6882 *no_add_attrs = true;
6885 return NULL_TREE;
6888 /* Handle a "unused" attribute; arguments as in
6889 struct attribute_spec.handler. */
6891 static tree
6892 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6893 int flags, bool *no_add_attrs)
6895 if (DECL_P (*node))
6897 tree decl = *node;
6899 if (TREE_CODE (decl) == PARM_DECL
6900 || TREE_CODE (decl) == VAR_DECL
6901 || TREE_CODE (decl) == FUNCTION_DECL
6902 || TREE_CODE (decl) == LABEL_DECL
6903 || TREE_CODE (decl) == TYPE_DECL)
6905 TREE_USED (decl) = 1;
6906 if (TREE_CODE (decl) == VAR_DECL
6907 || TREE_CODE (decl) == PARM_DECL)
6908 DECL_READ_P (decl) = 1;
6910 else
6912 warning (OPT_Wattributes, "%qE attribute ignored", name);
6913 *no_add_attrs = true;
6916 else
6918 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6919 *node = build_variant_type_copy (*node);
6920 TREE_USED (*node) = 1;
6923 return NULL_TREE;
6926 /* Handle a "externally_visible" attribute; arguments as in
6927 struct attribute_spec.handler. */
6929 static tree
6930 handle_externally_visible_attribute (tree *pnode, tree name,
6931 tree ARG_UNUSED (args),
6932 int ARG_UNUSED (flags),
6933 bool *no_add_attrs)
6935 tree node = *pnode;
6937 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6939 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6940 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6942 warning (OPT_Wattributes,
6943 "%qE attribute have effect only on public objects", name);
6944 *no_add_attrs = true;
6947 else
6949 warning (OPT_Wattributes, "%qE attribute ignored", name);
6950 *no_add_attrs = true;
6953 return NULL_TREE;
6956 /* Handle a "const" attribute; arguments as in
6957 struct attribute_spec.handler. */
6959 static tree
6960 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6961 int ARG_UNUSED (flags), bool *no_add_attrs)
6963 tree type = TREE_TYPE (*node);
6965 /* See FIXME comment on noreturn in c_common_attribute_table. */
6966 if (TREE_CODE (*node) == FUNCTION_DECL)
6967 TREE_READONLY (*node) = 1;
6968 else if (TREE_CODE (type) == POINTER_TYPE
6969 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6970 TREE_TYPE (*node)
6971 = (build_qualified_type
6972 (build_pointer_type
6973 (build_type_variant (TREE_TYPE (type), 1,
6974 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
6975 TYPE_QUALS (type)));
6976 else
6978 warning (OPT_Wattributes, "%qE attribute ignored", name);
6979 *no_add_attrs = true;
6982 return NULL_TREE;
6985 /* Handle a "transparent_union" attribute; arguments as in
6986 struct attribute_spec.handler. */
6988 static tree
6989 handle_transparent_union_attribute (tree *node, tree name,
6990 tree ARG_UNUSED (args), int flags,
6991 bool *no_add_attrs)
6993 tree type;
6995 *no_add_attrs = true;
6998 if (TREE_CODE (*node) == TYPE_DECL
6999 && ! (flags & ATTR_FLAG_CXX11))
7000 node = &TREE_TYPE (*node);
7001 type = *node;
7003 if (TREE_CODE (type) == UNION_TYPE)
7005 /* Make sure that the first field will work for a transparent union.
7006 If the type isn't complete yet, leave the check to the code in
7007 finish_struct. */
7008 if (TYPE_SIZE (type))
7010 tree first = first_field (type);
7011 if (first == NULL_TREE
7012 || DECL_ARTIFICIAL (first)
7013 || TYPE_MODE (type) != DECL_MODE (first))
7014 goto ignored;
7017 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7019 /* If the type isn't complete yet, setting the flag
7020 on a variant wouldn't ever be checked. */
7021 if (!TYPE_SIZE (type))
7022 goto ignored;
7024 /* build_duplicate_type doesn't work for C++. */
7025 if (c_dialect_cxx ())
7026 goto ignored;
7028 /* A type variant isn't good enough, since we don't a cast
7029 to such a type removed as a no-op. */
7030 *node = type = build_duplicate_type (type);
7033 TYPE_TRANSPARENT_AGGR (type) = 1;
7034 return NULL_TREE;
7037 ignored:
7038 warning (OPT_Wattributes, "%qE attribute ignored", name);
7039 return NULL_TREE;
7042 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7043 get the requested priority for a constructor or destructor,
7044 possibly issuing diagnostics for invalid or reserved
7045 priorities. */
7047 static priority_type
7048 get_priority (tree args, bool is_destructor)
7050 HOST_WIDE_INT pri;
7051 tree arg;
7053 if (!args)
7054 return DEFAULT_INIT_PRIORITY;
7056 if (!SUPPORTS_INIT_PRIORITY)
7058 if (is_destructor)
7059 error ("destructor priorities are not supported");
7060 else
7061 error ("constructor priorities are not supported");
7062 return DEFAULT_INIT_PRIORITY;
7065 arg = TREE_VALUE (args);
7066 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7067 goto invalid;
7068 if (arg == error_mark_node)
7069 return DEFAULT_INIT_PRIORITY;
7070 arg = default_conversion (arg);
7071 if (!tree_fits_shwi_p (arg)
7072 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7073 goto invalid;
7075 pri = tree_to_shwi (arg);
7076 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7077 goto invalid;
7079 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7081 if (is_destructor)
7082 warning (0,
7083 "destructor priorities from 0 to %d are reserved "
7084 "for the implementation",
7085 MAX_RESERVED_INIT_PRIORITY);
7086 else
7087 warning (0,
7088 "constructor priorities from 0 to %d are reserved "
7089 "for the implementation",
7090 MAX_RESERVED_INIT_PRIORITY);
7092 return pri;
7094 invalid:
7095 if (is_destructor)
7096 error ("destructor priorities must be integers from 0 to %d inclusive",
7097 MAX_INIT_PRIORITY);
7098 else
7099 error ("constructor priorities must be integers from 0 to %d inclusive",
7100 MAX_INIT_PRIORITY);
7101 return DEFAULT_INIT_PRIORITY;
7104 /* Handle a "constructor" attribute; arguments as in
7105 struct attribute_spec.handler. */
7107 static tree
7108 handle_constructor_attribute (tree *node, tree name, tree args,
7109 int ARG_UNUSED (flags),
7110 bool *no_add_attrs)
7112 tree decl = *node;
7113 tree type = TREE_TYPE (decl);
7115 if (TREE_CODE (decl) == FUNCTION_DECL
7116 && TREE_CODE (type) == FUNCTION_TYPE
7117 && decl_function_context (decl) == 0)
7119 priority_type priority;
7120 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7121 priority = get_priority (args, /*is_destructor=*/false);
7122 SET_DECL_INIT_PRIORITY (decl, priority);
7123 TREE_USED (decl) = 1;
7125 else
7127 warning (OPT_Wattributes, "%qE attribute ignored", name);
7128 *no_add_attrs = true;
7131 return NULL_TREE;
7134 /* Handle a "destructor" attribute; arguments as in
7135 struct attribute_spec.handler. */
7137 static tree
7138 handle_destructor_attribute (tree *node, tree name, tree args,
7139 int ARG_UNUSED (flags),
7140 bool *no_add_attrs)
7142 tree decl = *node;
7143 tree type = TREE_TYPE (decl);
7145 if (TREE_CODE (decl) == FUNCTION_DECL
7146 && TREE_CODE (type) == FUNCTION_TYPE
7147 && decl_function_context (decl) == 0)
7149 priority_type priority;
7150 DECL_STATIC_DESTRUCTOR (decl) = 1;
7151 priority = get_priority (args, /*is_destructor=*/true);
7152 SET_DECL_FINI_PRIORITY (decl, priority);
7153 TREE_USED (decl) = 1;
7155 else
7157 warning (OPT_Wattributes, "%qE attribute ignored", name);
7158 *no_add_attrs = true;
7161 return NULL_TREE;
7164 /* Nonzero if the mode is a valid vector mode for this architecture.
7165 This returns nonzero even if there is no hardware support for the
7166 vector mode, but we can emulate with narrower modes. */
7168 static int
7169 vector_mode_valid_p (enum machine_mode mode)
7171 enum mode_class mclass = GET_MODE_CLASS (mode);
7172 enum machine_mode innermode;
7174 /* Doh! What's going on? */
7175 if (mclass != MODE_VECTOR_INT
7176 && mclass != MODE_VECTOR_FLOAT
7177 && mclass != MODE_VECTOR_FRACT
7178 && mclass != MODE_VECTOR_UFRACT
7179 && mclass != MODE_VECTOR_ACCUM
7180 && mclass != MODE_VECTOR_UACCUM)
7181 return 0;
7183 /* Hardware support. Woo hoo! */
7184 if (targetm.vector_mode_supported_p (mode))
7185 return 1;
7187 innermode = GET_MODE_INNER (mode);
7189 /* We should probably return 1 if requesting V4DI and we have no DI,
7190 but we have V2DI, but this is probably very unlikely. */
7192 /* If we have support for the inner mode, we can safely emulate it.
7193 We may not have V2DI, but me can emulate with a pair of DIs. */
7194 return targetm.scalar_mode_supported_p (innermode);
7198 /* Handle a "mode" attribute; arguments as in
7199 struct attribute_spec.handler. */
7201 static tree
7202 handle_mode_attribute (tree *node, tree name, tree args,
7203 int ARG_UNUSED (flags), bool *no_add_attrs)
7205 tree type = *node;
7206 tree ident = TREE_VALUE (args);
7208 *no_add_attrs = true;
7210 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7211 warning (OPT_Wattributes, "%qE attribute ignored", name);
7212 else
7214 int j;
7215 const char *p = IDENTIFIER_POINTER (ident);
7216 int len = strlen (p);
7217 enum machine_mode mode = VOIDmode;
7218 tree typefm;
7219 bool valid_mode;
7221 if (len > 4 && p[0] == '_' && p[1] == '_'
7222 && p[len - 1] == '_' && p[len - 2] == '_')
7224 char *newp = (char *) alloca (len - 1);
7226 strcpy (newp, &p[2]);
7227 newp[len - 4] = '\0';
7228 p = newp;
7231 /* Change this type to have a type with the specified mode.
7232 First check for the special modes. */
7233 if (!strcmp (p, "byte"))
7234 mode = byte_mode;
7235 else if (!strcmp (p, "word"))
7236 mode = word_mode;
7237 else if (!strcmp (p, "pointer"))
7238 mode = ptr_mode;
7239 else if (!strcmp (p, "libgcc_cmp_return"))
7240 mode = targetm.libgcc_cmp_return_mode ();
7241 else if (!strcmp (p, "libgcc_shift_count"))
7242 mode = targetm.libgcc_shift_count_mode ();
7243 else if (!strcmp (p, "unwind_word"))
7244 mode = targetm.unwind_word_mode ();
7245 else
7246 for (j = 0; j < NUM_MACHINE_MODES; j++)
7247 if (!strcmp (p, GET_MODE_NAME (j)))
7249 mode = (enum machine_mode) j;
7250 break;
7253 if (mode == VOIDmode)
7255 error ("unknown machine mode %qE", ident);
7256 return NULL_TREE;
7259 valid_mode = false;
7260 switch (GET_MODE_CLASS (mode))
7262 case MODE_INT:
7263 case MODE_PARTIAL_INT:
7264 case MODE_FLOAT:
7265 case MODE_DECIMAL_FLOAT:
7266 case MODE_FRACT:
7267 case MODE_UFRACT:
7268 case MODE_ACCUM:
7269 case MODE_UACCUM:
7270 valid_mode = targetm.scalar_mode_supported_p (mode);
7271 break;
7273 case MODE_COMPLEX_INT:
7274 case MODE_COMPLEX_FLOAT:
7275 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7276 break;
7278 case MODE_VECTOR_INT:
7279 case MODE_VECTOR_FLOAT:
7280 case MODE_VECTOR_FRACT:
7281 case MODE_VECTOR_UFRACT:
7282 case MODE_VECTOR_ACCUM:
7283 case MODE_VECTOR_UACCUM:
7284 warning (OPT_Wattributes, "specifying vector types with "
7285 "__attribute__ ((mode)) is deprecated");
7286 warning (OPT_Wattributes,
7287 "use __attribute__ ((vector_size)) instead");
7288 valid_mode = vector_mode_valid_p (mode);
7289 break;
7291 default:
7292 break;
7294 if (!valid_mode)
7296 error ("unable to emulate %qs", p);
7297 return NULL_TREE;
7300 if (POINTER_TYPE_P (type))
7302 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7303 tree (*fn)(tree, enum machine_mode, bool);
7305 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7307 error ("invalid pointer mode %qs", p);
7308 return NULL_TREE;
7311 if (TREE_CODE (type) == POINTER_TYPE)
7312 fn = build_pointer_type_for_mode;
7313 else
7314 fn = build_reference_type_for_mode;
7315 typefm = fn (TREE_TYPE (type), mode, false);
7317 else
7319 /* For fixed-point modes, we need to test if the signness of type
7320 and the machine mode are consistent. */
7321 if (ALL_FIXED_POINT_MODE_P (mode)
7322 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7324 error ("signedness of type and machine mode %qs don%'t match", p);
7325 return NULL_TREE;
7327 /* For fixed-point modes, we need to pass saturating info. */
7328 typefm = lang_hooks.types.type_for_mode (mode,
7329 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7330 : TYPE_UNSIGNED (type));
7333 if (typefm == NULL_TREE)
7335 error ("no data type for mode %qs", p);
7336 return NULL_TREE;
7338 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7340 /* For enumeral types, copy the precision from the integer
7341 type returned above. If not an INTEGER_TYPE, we can't use
7342 this mode for this type. */
7343 if (TREE_CODE (typefm) != INTEGER_TYPE)
7345 error ("cannot use mode %qs for enumeral types", p);
7346 return NULL_TREE;
7349 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7351 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7352 typefm = type;
7354 else
7356 /* We cannot build a type variant, as there's code that assumes
7357 that TYPE_MAIN_VARIANT has the same mode. This includes the
7358 debug generators. Instead, create a subrange type. This
7359 results in all of the enumeral values being emitted only once
7360 in the original, and the subtype gets them by reference. */
7361 if (TYPE_UNSIGNED (type))
7362 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7363 else
7364 typefm = make_signed_type (TYPE_PRECISION (typefm));
7365 TREE_TYPE (typefm) = type;
7368 else if (VECTOR_MODE_P (mode)
7369 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7370 : TREE_CODE (type) != TREE_CODE (typefm))
7372 error ("mode %qs applied to inappropriate type", p);
7373 return NULL_TREE;
7376 *node = typefm;
7379 return NULL_TREE;
7382 /* Handle a "section" attribute; arguments as in
7383 struct attribute_spec.handler. */
7385 static tree
7386 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7387 int ARG_UNUSED (flags), bool *no_add_attrs)
7389 tree decl = *node;
7391 if (targetm_common.have_named_sections)
7393 user_defined_section_attribute = true;
7395 if ((TREE_CODE (decl) == FUNCTION_DECL
7396 || TREE_CODE (decl) == VAR_DECL)
7397 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7399 if (TREE_CODE (decl) == VAR_DECL
7400 && current_function_decl != NULL_TREE
7401 && !TREE_STATIC (decl))
7403 error_at (DECL_SOURCE_LOCATION (decl),
7404 "section attribute cannot be specified for "
7405 "local variables");
7406 *no_add_attrs = true;
7409 /* The decl may have already been given a section attribute
7410 from a previous declaration. Ensure they match. */
7411 else if (DECL_SECTION_NAME (decl) != NULL
7412 && strcmp (DECL_SECTION_NAME (decl),
7413 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7415 error ("section of %q+D conflicts with previous declaration",
7416 *node);
7417 *no_add_attrs = true;
7419 else if (TREE_CODE (decl) == VAR_DECL
7420 && !targetm.have_tls && targetm.emutls.tmpl_section
7421 && DECL_THREAD_LOCAL_P (decl))
7423 error ("section of %q+D cannot be overridden", *node);
7424 *no_add_attrs = true;
7426 else
7427 set_decl_section_name (decl,
7428 TREE_STRING_POINTER (TREE_VALUE (args)));
7430 else
7432 error ("section attribute not allowed for %q+D", *node);
7433 *no_add_attrs = true;
7436 else
7438 error_at (DECL_SOURCE_LOCATION (*node),
7439 "section attributes are not supported for this target");
7440 *no_add_attrs = true;
7443 return NULL_TREE;
7446 /* Check whether ALIGN is a valid user-specified alignment. If so,
7447 return its base-2 log; if not, output an error and return -1. If
7448 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7449 no error. */
7451 check_user_alignment (const_tree align, bool allow_zero)
7453 int i;
7455 if (error_operand_p (align))
7456 return -1;
7457 if (TREE_CODE (align) != INTEGER_CST
7458 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7460 error ("requested alignment is not an integer constant");
7461 return -1;
7463 else if (allow_zero && integer_zerop (align))
7464 return -1;
7465 else if (tree_int_cst_sgn (align) == -1
7466 || (i = tree_log2 (align)) == -1)
7468 error ("requested alignment is not a positive power of 2");
7469 return -1;
7471 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7473 error ("requested alignment is too large");
7474 return -1;
7476 return i;
7480 If in c++-11, check if the c++-11 alignment constraint with respect
7481 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7482 c++-11 mode, does nothing.
7484 [dcl.align]2/ says:
7486 [* if the constant expression evaluates to a fundamental alignment,
7487 the alignment requirement of the declared entity shall be the
7488 specified fundamental alignment.
7490 * if the constant expression evaluates to an extended alignment
7491 and the implementation supports that alignment in the context
7492 of the declaration, the alignment of the declared entity shall
7493 be that alignment
7495 * if the constant expression evaluates to an extended alignment
7496 and the implementation does not support that alignment in the
7497 context of the declaration, the program is ill-formed]. */
7499 static bool
7500 check_cxx_fundamental_alignment_constraints (tree node,
7501 unsigned align_log,
7502 int flags)
7504 bool alignment_too_large_p = false;
7505 unsigned requested_alignment = 1U << align_log;
7506 unsigned max_align = 0;
7508 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7509 || (node == NULL_TREE || node == error_mark_node))
7510 return true;
7512 if (cxx_fundamental_alignment_p (requested_alignment))
7513 return true;
7515 if (DECL_P (node))
7517 if (TREE_STATIC (node))
7519 /* For file scope variables and static members, the target
7520 supports alignments that are at most
7521 MAX_OFILE_ALIGNMENT. */
7522 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7523 alignment_too_large_p = true;
7525 else
7527 #ifdef BIGGEST_FIELD_ALIGNMENT
7528 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7529 #else
7530 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7531 #endif
7532 /* For non-static members, the target supports either
7533 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7534 if it is defined or BIGGEST_ALIGNMENT. */
7535 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7536 if (TREE_CODE (node) == FIELD_DECL
7537 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7538 alignment_too_large_p = true;
7539 #undef MAX_TARGET_FIELD_ALIGNMENT
7540 /* For stack variables, the target supports at most
7541 MAX_STACK_ALIGNMENT. */
7542 else if (decl_function_context (node) != NULL
7543 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7544 alignment_too_large_p = true;
7547 else if (TYPE_P (node))
7549 /* Let's be liberal for types. */
7550 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7551 alignment_too_large_p = true;
7554 if (alignment_too_large_p)
7555 pedwarn (input_location, OPT_Wattributes,
7556 "requested alignment %d is larger than %d",
7557 requested_alignment, max_align);
7559 return !alignment_too_large_p;
7562 /* Handle a "aligned" attribute; arguments as in
7563 struct attribute_spec.handler. */
7565 static tree
7566 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7567 int flags, bool *no_add_attrs)
7569 tree decl = NULL_TREE;
7570 tree *type = NULL;
7571 int is_type = 0;
7572 tree align_expr;
7573 int i;
7575 if (args)
7577 align_expr = TREE_VALUE (args);
7578 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7579 && TREE_CODE (align_expr) != FUNCTION_DECL)
7580 align_expr = default_conversion (align_expr);
7582 else
7583 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7585 if (DECL_P (*node))
7587 decl = *node;
7588 type = &TREE_TYPE (decl);
7589 is_type = TREE_CODE (*node) == TYPE_DECL;
7591 else if (TYPE_P (*node))
7592 type = node, is_type = 1;
7594 if ((i = check_user_alignment (align_expr, false)) == -1
7595 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7596 *no_add_attrs = true;
7597 else if (is_type)
7599 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7600 /* OK, modify the type in place. */;
7601 /* If we have a TYPE_DECL, then copy the type, so that we
7602 don't accidentally modify a builtin type. See pushdecl. */
7603 else if (decl && TREE_TYPE (decl) != error_mark_node
7604 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7606 tree tt = TREE_TYPE (decl);
7607 *type = build_variant_type_copy (*type);
7608 DECL_ORIGINAL_TYPE (decl) = tt;
7609 TYPE_NAME (*type) = decl;
7610 TREE_USED (*type) = TREE_USED (decl);
7611 TREE_TYPE (decl) = *type;
7613 else
7614 *type = build_variant_type_copy (*type);
7616 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7617 TYPE_USER_ALIGN (*type) = 1;
7619 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7620 && TREE_CODE (decl) != FIELD_DECL)
7622 error ("alignment may not be specified for %q+D", decl);
7623 *no_add_attrs = true;
7625 else if (DECL_USER_ALIGN (decl)
7626 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7627 /* C++-11 [dcl.align/4]:
7629 When multiple alignment-specifiers are specified for an
7630 entity, the alignment requirement shall be set to the
7631 strictest specified alignment.
7633 This formally comes from the c++11 specification but we are
7634 doing it for the GNU attribute syntax as well. */
7635 *no_add_attrs = true;
7636 else if (TREE_CODE (decl) == FUNCTION_DECL
7637 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7639 if (DECL_USER_ALIGN (decl))
7640 error ("alignment for %q+D was previously specified as %d "
7641 "and may not be decreased", decl,
7642 DECL_ALIGN (decl) / BITS_PER_UNIT);
7643 else
7644 error ("alignment for %q+D must be at least %d", decl,
7645 DECL_ALIGN (decl) / BITS_PER_UNIT);
7646 *no_add_attrs = true;
7648 else
7650 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7651 DECL_USER_ALIGN (decl) = 1;
7654 return NULL_TREE;
7657 /* Handle a "weak" attribute; arguments as in
7658 struct attribute_spec.handler. */
7660 static tree
7661 handle_weak_attribute (tree *node, tree name,
7662 tree ARG_UNUSED (args),
7663 int ARG_UNUSED (flags),
7664 bool * ARG_UNUSED (no_add_attrs))
7666 if (TREE_CODE (*node) == FUNCTION_DECL
7667 && DECL_DECLARED_INLINE_P (*node))
7669 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7670 *no_add_attrs = true;
7672 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7674 error ("indirect function %q+D cannot be declared weak", *node);
7675 *no_add_attrs = true;
7676 return NULL_TREE;
7678 else if (TREE_CODE (*node) == FUNCTION_DECL
7679 || TREE_CODE (*node) == VAR_DECL)
7680 declare_weak (*node);
7681 else
7682 warning (OPT_Wattributes, "%qE attribute ignored", name);
7684 return NULL_TREE;
7687 /* Handle an "alias" or "ifunc" attribute; arguments as in
7688 struct attribute_spec.handler, except that IS_ALIAS tells us
7689 whether this is an alias as opposed to ifunc attribute. */
7691 static tree
7692 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7693 bool *no_add_attrs)
7695 tree decl = *node;
7697 if (TREE_CODE (decl) != FUNCTION_DECL
7698 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7700 warning (OPT_Wattributes, "%qE attribute ignored", name);
7701 *no_add_attrs = true;
7703 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7704 || (TREE_CODE (decl) != FUNCTION_DECL
7705 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7706 /* A static variable declaration is always a tentative definition,
7707 but the alias is a non-tentative definition which overrides. */
7708 || (TREE_CODE (decl) != FUNCTION_DECL
7709 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7711 error ("%q+D defined both normally and as %qE attribute", decl, name);
7712 *no_add_attrs = true;
7713 return NULL_TREE;
7715 else if (!is_alias
7716 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7717 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7719 error ("weak %q+D cannot be defined %qE", decl, name);
7720 *no_add_attrs = true;
7721 return NULL_TREE;
7724 /* Note that the very first time we process a nested declaration,
7725 decl_function_context will not be set. Indeed, *would* never
7726 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7727 we do below. After such frobbery, pushdecl would set the context.
7728 In any case, this is never what we want. */
7729 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7731 tree id;
7733 id = TREE_VALUE (args);
7734 if (TREE_CODE (id) != STRING_CST)
7736 error ("attribute %qE argument not a string", name);
7737 *no_add_attrs = true;
7738 return NULL_TREE;
7740 id = get_identifier (TREE_STRING_POINTER (id));
7741 /* This counts as a use of the object pointed to. */
7742 TREE_USED (id) = 1;
7744 if (TREE_CODE (decl) == FUNCTION_DECL)
7745 DECL_INITIAL (decl) = error_mark_node;
7746 else
7747 TREE_STATIC (decl) = 1;
7749 if (!is_alias)
7750 /* ifuncs are also aliases, so set that attribute too. */
7751 DECL_ATTRIBUTES (decl)
7752 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7754 else
7756 warning (OPT_Wattributes, "%qE attribute ignored", name);
7757 *no_add_attrs = true;
7760 return NULL_TREE;
7763 /* Handle an "alias" or "ifunc" attribute; arguments as in
7764 struct attribute_spec.handler. */
7766 static tree
7767 handle_ifunc_attribute (tree *node, tree name, tree args,
7768 int ARG_UNUSED (flags), bool *no_add_attrs)
7770 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7773 /* Handle an "alias" or "ifunc" attribute; arguments as in
7774 struct attribute_spec.handler. */
7776 static tree
7777 handle_alias_attribute (tree *node, tree name, tree args,
7778 int ARG_UNUSED (flags), bool *no_add_attrs)
7780 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7783 /* Handle a "weakref" attribute; arguments as in struct
7784 attribute_spec.handler. */
7786 static tree
7787 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7788 int flags, bool *no_add_attrs)
7790 tree attr = NULL_TREE;
7792 /* We must ignore the attribute when it is associated with
7793 local-scoped decls, since attribute alias is ignored and many
7794 such symbols do not even have a DECL_WEAK field. */
7795 if (decl_function_context (*node)
7796 || current_function_decl
7797 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7799 warning (OPT_Wattributes, "%qE attribute ignored", name);
7800 *no_add_attrs = true;
7801 return NULL_TREE;
7804 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7806 error ("indirect function %q+D cannot be declared weakref", *node);
7807 *no_add_attrs = true;
7808 return NULL_TREE;
7811 /* The idea here is that `weakref("name")' mutates into `weakref,
7812 alias("name")', and weakref without arguments, in turn,
7813 implicitly adds weak. */
7815 if (args)
7817 attr = tree_cons (get_identifier ("alias"), args, attr);
7818 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7820 *no_add_attrs = true;
7822 decl_attributes (node, attr, flags);
7824 else
7826 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7827 error_at (DECL_SOURCE_LOCATION (*node),
7828 "weakref attribute must appear before alias attribute");
7830 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7831 and that isn't supported; and because it wants to add it to
7832 the list of weak decls, which isn't helpful. */
7833 DECL_WEAK (*node) = 1;
7836 return NULL_TREE;
7839 /* Handle an "visibility" attribute; arguments as in
7840 struct attribute_spec.handler. */
7842 static tree
7843 handle_visibility_attribute (tree *node, tree name, tree args,
7844 int ARG_UNUSED (flags),
7845 bool *ARG_UNUSED (no_add_attrs))
7847 tree decl = *node;
7848 tree id = TREE_VALUE (args);
7849 enum symbol_visibility vis;
7851 if (TYPE_P (*node))
7853 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7854 /* OK */;
7855 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7857 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7858 name);
7859 return NULL_TREE;
7861 else if (TYPE_FIELDS (*node))
7863 error ("%qE attribute ignored because %qT is already defined",
7864 name, *node);
7865 return NULL_TREE;
7868 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7870 warning (OPT_Wattributes, "%qE attribute ignored", name);
7871 return NULL_TREE;
7874 if (TREE_CODE (id) != STRING_CST)
7876 error ("visibility argument not a string");
7877 return NULL_TREE;
7880 /* If this is a type, set the visibility on the type decl. */
7881 if (TYPE_P (decl))
7883 decl = TYPE_NAME (decl);
7884 if (!decl)
7885 return NULL_TREE;
7886 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7888 warning (OPT_Wattributes, "%qE attribute ignored on types",
7889 name);
7890 return NULL_TREE;
7894 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7895 vis = VISIBILITY_DEFAULT;
7896 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7897 vis = VISIBILITY_INTERNAL;
7898 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7899 vis = VISIBILITY_HIDDEN;
7900 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7901 vis = VISIBILITY_PROTECTED;
7902 else
7904 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7905 vis = VISIBILITY_DEFAULT;
7908 if (DECL_VISIBILITY_SPECIFIED (decl)
7909 && vis != DECL_VISIBILITY (decl))
7911 tree attributes = (TYPE_P (*node)
7912 ? TYPE_ATTRIBUTES (*node)
7913 : DECL_ATTRIBUTES (decl));
7914 if (lookup_attribute ("visibility", attributes))
7915 error ("%qD redeclared with different visibility", decl);
7916 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7917 && lookup_attribute ("dllimport", attributes))
7918 error ("%qD was declared %qs which implies default visibility",
7919 decl, "dllimport");
7920 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7921 && lookup_attribute ("dllexport", attributes))
7922 error ("%qD was declared %qs which implies default visibility",
7923 decl, "dllexport");
7926 DECL_VISIBILITY (decl) = vis;
7927 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7929 /* Go ahead and attach the attribute to the node as well. This is needed
7930 so we can determine whether we have VISIBILITY_DEFAULT because the
7931 visibility was not specified, or because it was explicitly overridden
7932 from the containing scope. */
7934 return NULL_TREE;
7937 /* Determine the ELF symbol visibility for DECL, which is either a
7938 variable or a function. It is an error to use this function if a
7939 definition of DECL is not available in this translation unit.
7940 Returns true if the final visibility has been determined by this
7941 function; false if the caller is free to make additional
7942 modifications. */
7944 bool
7945 c_determine_visibility (tree decl)
7947 gcc_assert (TREE_CODE (decl) == VAR_DECL
7948 || TREE_CODE (decl) == FUNCTION_DECL);
7950 /* If the user explicitly specified the visibility with an
7951 attribute, honor that. DECL_VISIBILITY will have been set during
7952 the processing of the attribute. We check for an explicit
7953 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7954 to distinguish the use of an attribute from the use of a "#pragma
7955 GCC visibility push(...)"; in the latter case we still want other
7956 considerations to be able to overrule the #pragma. */
7957 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7958 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7959 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7960 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7961 return true;
7963 /* Set default visibility to whatever the user supplied with
7964 visibility_specified depending on #pragma GCC visibility. */
7965 if (!DECL_VISIBILITY_SPECIFIED (decl))
7967 if (visibility_options.inpragma
7968 || DECL_VISIBILITY (decl) != default_visibility)
7970 DECL_VISIBILITY (decl) = default_visibility;
7971 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7972 /* If visibility changed and DECL already has DECL_RTL, ensure
7973 symbol flags are updated. */
7974 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7975 || TREE_CODE (decl) == FUNCTION_DECL)
7976 && DECL_RTL_SET_P (decl))
7977 make_decl_rtl (decl);
7980 return false;
7983 /* Handle an "tls_model" attribute; arguments as in
7984 struct attribute_spec.handler. */
7986 static tree
7987 handle_tls_model_attribute (tree *node, tree name, tree args,
7988 int ARG_UNUSED (flags), bool *no_add_attrs)
7990 tree id;
7991 tree decl = *node;
7992 enum tls_model kind;
7994 *no_add_attrs = true;
7996 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7998 warning (OPT_Wattributes, "%qE attribute ignored", name);
7999 return NULL_TREE;
8002 kind = DECL_TLS_MODEL (decl);
8003 id = TREE_VALUE (args);
8004 if (TREE_CODE (id) != STRING_CST)
8006 error ("tls_model argument not a string");
8007 return NULL_TREE;
8010 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8011 kind = TLS_MODEL_LOCAL_EXEC;
8012 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8013 kind = TLS_MODEL_INITIAL_EXEC;
8014 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8015 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8016 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8017 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8018 else
8019 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8021 set_decl_tls_model (decl, kind);
8022 return NULL_TREE;
8025 /* Handle a "no_instrument_function" attribute; arguments as in
8026 struct attribute_spec.handler. */
8028 static tree
8029 handle_no_instrument_function_attribute (tree *node, tree name,
8030 tree ARG_UNUSED (args),
8031 int ARG_UNUSED (flags),
8032 bool *no_add_attrs)
8034 tree decl = *node;
8036 if (TREE_CODE (decl) != FUNCTION_DECL)
8038 error_at (DECL_SOURCE_LOCATION (decl),
8039 "%qE attribute applies only to functions", name);
8040 *no_add_attrs = true;
8042 else
8043 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8045 return NULL_TREE;
8048 /* Handle a "malloc" attribute; arguments as in
8049 struct attribute_spec.handler. */
8051 static tree
8052 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8053 int ARG_UNUSED (flags), bool *no_add_attrs)
8055 if (TREE_CODE (*node) == FUNCTION_DECL
8056 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8057 DECL_IS_MALLOC (*node) = 1;
8058 else
8060 warning (OPT_Wattributes, "%qE attribute ignored", name);
8061 *no_add_attrs = true;
8064 return NULL_TREE;
8067 /* Handle a "alloc_size" attribute; arguments as in
8068 struct attribute_spec.handler. */
8070 static tree
8071 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8072 int ARG_UNUSED (flags), bool *no_add_attrs)
8074 unsigned arg_count = type_num_arguments (*node);
8075 for (; args; args = TREE_CHAIN (args))
8077 tree position = TREE_VALUE (args);
8078 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8079 && TREE_CODE (position) != FUNCTION_DECL)
8080 position = default_conversion (position);
8082 if (!tree_fits_uhwi_p (position)
8083 || !arg_count
8084 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8086 warning (OPT_Wattributes,
8087 "alloc_size parameter outside range");
8088 *no_add_attrs = true;
8089 return NULL_TREE;
8092 return NULL_TREE;
8095 /* Handle a "alloc_align" attribute; arguments as in
8096 struct attribute_spec.handler. */
8098 static tree
8099 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8100 bool *no_add_attrs)
8102 unsigned arg_count = type_num_arguments (*node);
8103 tree position = TREE_VALUE (args);
8104 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8105 position = default_conversion (position);
8107 if (!tree_fits_uhwi_p (position)
8108 || !arg_count
8109 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8111 warning (OPT_Wattributes,
8112 "alloc_align parameter outside range");
8113 *no_add_attrs = true;
8114 return NULL_TREE;
8116 return NULL_TREE;
8119 /* Handle a "assume_aligned" attribute; arguments as in
8120 struct attribute_spec.handler. */
8122 static tree
8123 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8124 bool *no_add_attrs)
8126 for (; args; args = TREE_CHAIN (args))
8128 tree position = TREE_VALUE (args);
8129 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8130 && TREE_CODE (position) != FUNCTION_DECL)
8131 position = default_conversion (position);
8133 if (TREE_CODE (position) != INTEGER_CST)
8135 warning (OPT_Wattributes,
8136 "assume_aligned parameter not integer constant");
8137 *no_add_attrs = true;
8138 return NULL_TREE;
8141 return NULL_TREE;
8144 /* Handle a "fn spec" attribute; arguments as in
8145 struct attribute_spec.handler. */
8147 static tree
8148 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8149 tree args, int ARG_UNUSED (flags),
8150 bool *no_add_attrs ATTRIBUTE_UNUSED)
8152 gcc_assert (args
8153 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8154 && !TREE_CHAIN (args));
8155 return NULL_TREE;
8158 /* Handle a "warn_unused" attribute; arguments as in
8159 struct attribute_spec.handler. */
8161 static tree
8162 handle_warn_unused_attribute (tree *node, tree name,
8163 tree args ATTRIBUTE_UNUSED,
8164 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8166 if (TYPE_P (*node))
8167 /* Do nothing else, just set the attribute. We'll get at
8168 it later with lookup_attribute. */
8170 else
8172 warning (OPT_Wattributes, "%qE attribute ignored", name);
8173 *no_add_attrs = true;
8176 return NULL_TREE;
8179 /* Handle an "omp declare simd" attribute; arguments as in
8180 struct attribute_spec.handler. */
8182 static tree
8183 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8185 return NULL_TREE;
8188 /* Handle an "omp declare target" attribute; arguments as in
8189 struct attribute_spec.handler. */
8191 static tree
8192 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8194 return NULL_TREE;
8197 /* Handle a "returns_twice" attribute; arguments as in
8198 struct attribute_spec.handler. */
8200 static tree
8201 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8202 int ARG_UNUSED (flags), bool *no_add_attrs)
8204 if (TREE_CODE (*node) == FUNCTION_DECL)
8205 DECL_IS_RETURNS_TWICE (*node) = 1;
8206 else
8208 warning (OPT_Wattributes, "%qE attribute ignored", name);
8209 *no_add_attrs = true;
8212 return NULL_TREE;
8215 /* Handle a "no_limit_stack" attribute; arguments as in
8216 struct attribute_spec.handler. */
8218 static tree
8219 handle_no_limit_stack_attribute (tree *node, tree name,
8220 tree ARG_UNUSED (args),
8221 int ARG_UNUSED (flags),
8222 bool *no_add_attrs)
8224 tree decl = *node;
8226 if (TREE_CODE (decl) != FUNCTION_DECL)
8228 error_at (DECL_SOURCE_LOCATION (decl),
8229 "%qE attribute applies only to functions", name);
8230 *no_add_attrs = true;
8232 else if (DECL_INITIAL (decl))
8234 error_at (DECL_SOURCE_LOCATION (decl),
8235 "can%'t set %qE attribute after definition", name);
8236 *no_add_attrs = true;
8238 else
8239 DECL_NO_LIMIT_STACK (decl) = 1;
8241 return NULL_TREE;
8244 /* Handle a "pure" attribute; arguments as in
8245 struct attribute_spec.handler. */
8247 static tree
8248 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8249 int ARG_UNUSED (flags), bool *no_add_attrs)
8251 if (TREE_CODE (*node) == FUNCTION_DECL)
8252 DECL_PURE_P (*node) = 1;
8253 /* ??? TODO: Support types. */
8254 else
8256 warning (OPT_Wattributes, "%qE attribute ignored", name);
8257 *no_add_attrs = true;
8260 return NULL_TREE;
8263 /* Digest an attribute list destined for a transactional memory statement.
8264 ALLOWED is the set of attributes that are allowed for this statement;
8265 return the attribute we parsed. Multiple attributes are never allowed. */
8268 parse_tm_stmt_attr (tree attrs, int allowed)
8270 tree a_seen = NULL;
8271 int m_seen = 0;
8273 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8275 tree a = TREE_PURPOSE (attrs);
8276 int m = 0;
8278 if (is_attribute_p ("outer", a))
8279 m = TM_STMT_ATTR_OUTER;
8281 if ((m & allowed) == 0)
8283 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8284 continue;
8287 if (m_seen == 0)
8289 a_seen = a;
8290 m_seen = m;
8292 else if (m_seen == m)
8293 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8294 else
8295 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8298 return m_seen;
8301 /* Transform a TM attribute name into a maskable integer and back.
8302 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8303 to how the lack of an attribute is treated. */
8306 tm_attr_to_mask (tree attr)
8308 if (attr == NULL)
8309 return 0;
8310 if (is_attribute_p ("transaction_safe", attr))
8311 return TM_ATTR_SAFE;
8312 if (is_attribute_p ("transaction_callable", attr))
8313 return TM_ATTR_CALLABLE;
8314 if (is_attribute_p ("transaction_pure", attr))
8315 return TM_ATTR_PURE;
8316 if (is_attribute_p ("transaction_unsafe", attr))
8317 return TM_ATTR_IRREVOCABLE;
8318 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8319 return TM_ATTR_MAY_CANCEL_OUTER;
8320 return 0;
8323 tree
8324 tm_mask_to_attr (int mask)
8326 const char *str;
8327 switch (mask)
8329 case TM_ATTR_SAFE:
8330 str = "transaction_safe";
8331 break;
8332 case TM_ATTR_CALLABLE:
8333 str = "transaction_callable";
8334 break;
8335 case TM_ATTR_PURE:
8336 str = "transaction_pure";
8337 break;
8338 case TM_ATTR_IRREVOCABLE:
8339 str = "transaction_unsafe";
8340 break;
8341 case TM_ATTR_MAY_CANCEL_OUTER:
8342 str = "transaction_may_cancel_outer";
8343 break;
8344 default:
8345 gcc_unreachable ();
8347 return get_identifier (str);
8350 /* Return the first TM attribute seen in LIST. */
8352 tree
8353 find_tm_attribute (tree list)
8355 for (; list ; list = TREE_CHAIN (list))
8357 tree name = TREE_PURPOSE (list);
8358 if (tm_attr_to_mask (name) != 0)
8359 return name;
8361 return NULL_TREE;
8364 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8365 Here we accept only function types, and verify that none of the other
8366 function TM attributes are also applied. */
8367 /* ??? We need to accept class types for C++, but not C. This greatly
8368 complicates this function, since we can no longer rely on the extra
8369 processing given by function_type_required. */
8371 static tree
8372 handle_tm_attribute (tree *node, tree name, tree args,
8373 int flags, bool *no_add_attrs)
8375 /* Only one path adds the attribute; others don't. */
8376 *no_add_attrs = true;
8378 switch (TREE_CODE (*node))
8380 case RECORD_TYPE:
8381 case UNION_TYPE:
8382 /* Only tm_callable and tm_safe apply to classes. */
8383 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8384 goto ignored;
8385 /* FALLTHRU */
8387 case FUNCTION_TYPE:
8388 case METHOD_TYPE:
8390 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8391 if (old_name == name)
8393 else if (old_name != NULL_TREE)
8394 error ("type was previously declared %qE", old_name);
8395 else
8396 *no_add_attrs = false;
8398 break;
8400 case POINTER_TYPE:
8402 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8403 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8405 tree fn_tmp = TREE_TYPE (*node);
8406 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8407 *node = build_pointer_type (fn_tmp);
8408 break;
8411 /* FALLTHRU */
8413 default:
8414 /* If a function is next, pass it on to be tried next. */
8415 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8416 return tree_cons (name, args, NULL);
8418 ignored:
8419 warning (OPT_Wattributes, "%qE attribute ignored", name);
8420 break;
8423 return NULL_TREE;
8426 /* Handle the TM_WRAP attribute; arguments as in
8427 struct attribute_spec.handler. */
8429 static tree
8430 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8431 int ARG_UNUSED (flags), bool *no_add_attrs)
8433 tree decl = *node;
8435 /* We don't need the attribute even on success, since we
8436 record the entry in an external table. */
8437 *no_add_attrs = true;
8439 if (TREE_CODE (decl) != FUNCTION_DECL)
8440 warning (OPT_Wattributes, "%qE attribute ignored", name);
8441 else
8443 tree wrap_decl = TREE_VALUE (args);
8444 if (error_operand_p (wrap_decl))
8446 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8447 && TREE_CODE (wrap_decl) != VAR_DECL
8448 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8449 error ("%qE argument not an identifier", name);
8450 else
8452 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8453 wrap_decl = lookup_name (wrap_decl);
8454 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8456 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8457 TREE_TYPE (wrap_decl)))
8458 record_tm_replacement (wrap_decl, decl);
8459 else
8460 error ("%qD is not compatible with %qD", wrap_decl, decl);
8462 else
8463 error ("%qE argument is not a function", name);
8467 return NULL_TREE;
8470 /* Ignore the given attribute. Used when this attribute may be usefully
8471 overridden by the target, but is not used generically. */
8473 static tree
8474 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8475 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8476 bool *no_add_attrs)
8478 *no_add_attrs = true;
8479 return NULL_TREE;
8482 /* Handle a "no vops" attribute; arguments as in
8483 struct attribute_spec.handler. */
8485 static tree
8486 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8487 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8488 bool *ARG_UNUSED (no_add_attrs))
8490 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8491 DECL_IS_NOVOPS (*node) = 1;
8492 return NULL_TREE;
8495 /* Handle a "deprecated" attribute; arguments as in
8496 struct attribute_spec.handler. */
8498 static tree
8499 handle_deprecated_attribute (tree *node, tree name,
8500 tree args, int flags,
8501 bool *no_add_attrs)
8503 tree type = NULL_TREE;
8504 int warn = 0;
8505 tree what = NULL_TREE;
8507 if (!args)
8508 *no_add_attrs = true;
8509 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8511 error ("deprecated message is not a string");
8512 *no_add_attrs = true;
8515 if (DECL_P (*node))
8517 tree decl = *node;
8518 type = TREE_TYPE (decl);
8520 if (TREE_CODE (decl) == TYPE_DECL
8521 || TREE_CODE (decl) == PARM_DECL
8522 || TREE_CODE (decl) == VAR_DECL
8523 || TREE_CODE (decl) == FUNCTION_DECL
8524 || TREE_CODE (decl) == FIELD_DECL
8525 || objc_method_decl (TREE_CODE (decl)))
8526 TREE_DEPRECATED (decl) = 1;
8527 else
8528 warn = 1;
8530 else if (TYPE_P (*node))
8532 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8533 *node = build_variant_type_copy (*node);
8534 TREE_DEPRECATED (*node) = 1;
8535 type = *node;
8537 else
8538 warn = 1;
8540 if (warn)
8542 *no_add_attrs = true;
8543 if (type && TYPE_NAME (type))
8545 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8546 what = TYPE_NAME (*node);
8547 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8548 && DECL_NAME (TYPE_NAME (type)))
8549 what = DECL_NAME (TYPE_NAME (type));
8551 if (what)
8552 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8553 else
8554 warning (OPT_Wattributes, "%qE attribute ignored", name);
8557 return NULL_TREE;
8560 /* Handle a "vector_size" attribute; arguments as in
8561 struct attribute_spec.handler. */
8563 static tree
8564 handle_vector_size_attribute (tree *node, tree name, tree args,
8565 int ARG_UNUSED (flags),
8566 bool *no_add_attrs)
8568 unsigned HOST_WIDE_INT vecsize, nunits;
8569 enum machine_mode orig_mode;
8570 tree type = *node, new_type, size;
8572 *no_add_attrs = true;
8574 size = TREE_VALUE (args);
8575 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8576 && TREE_CODE (size) != FUNCTION_DECL)
8577 size = default_conversion (size);
8579 if (!tree_fits_uhwi_p (size))
8581 warning (OPT_Wattributes, "%qE attribute ignored", name);
8582 return NULL_TREE;
8585 /* Get the vector size (in bytes). */
8586 vecsize = tree_to_uhwi (size);
8588 /* We need to provide for vector pointers, vector arrays, and
8589 functions returning vectors. For example:
8591 __attribute__((vector_size(16))) short *foo;
8593 In this case, the mode is SI, but the type being modified is
8594 HI, so we need to look further. */
8596 while (POINTER_TYPE_P (type)
8597 || TREE_CODE (type) == FUNCTION_TYPE
8598 || TREE_CODE (type) == METHOD_TYPE
8599 || TREE_CODE (type) == ARRAY_TYPE
8600 || TREE_CODE (type) == OFFSET_TYPE)
8601 type = TREE_TYPE (type);
8603 /* Get the mode of the type being modified. */
8604 orig_mode = TYPE_MODE (type);
8606 if ((!INTEGRAL_TYPE_P (type)
8607 && !SCALAR_FLOAT_TYPE_P (type)
8608 && !FIXED_POINT_TYPE_P (type))
8609 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8610 && GET_MODE_CLASS (orig_mode) != MODE_INT
8611 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8612 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8613 || TREE_CODE (type) == BOOLEAN_TYPE)
8615 error ("invalid vector type for attribute %qE", name);
8616 return NULL_TREE;
8619 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8621 error ("vector size not an integral multiple of component size");
8622 return NULL;
8625 if (vecsize == 0)
8627 error ("zero vector size");
8628 return NULL;
8631 /* Calculate how many units fit in the vector. */
8632 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8633 if (nunits & (nunits - 1))
8635 error ("number of components of the vector not a power of two");
8636 return NULL_TREE;
8639 new_type = build_vector_type (type, nunits);
8641 /* Build back pointers if needed. */
8642 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8644 return NULL_TREE;
8647 /* Handle the "nonnull" attribute. */
8648 static tree
8649 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8650 tree args, int ARG_UNUSED (flags),
8651 bool *no_add_attrs)
8653 tree type = *node;
8654 unsigned HOST_WIDE_INT attr_arg_num;
8656 /* If no arguments are specified, all pointer arguments should be
8657 non-null. Verify a full prototype is given so that the arguments
8658 will have the correct types when we actually check them later. */
8659 if (!args)
8661 if (!prototype_p (type))
8663 error ("nonnull attribute without arguments on a non-prototype");
8664 *no_add_attrs = true;
8666 return NULL_TREE;
8669 /* Argument list specified. Verify that each argument number references
8670 a pointer argument. */
8671 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8673 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8675 tree arg = TREE_VALUE (args);
8676 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8677 && TREE_CODE (arg) != FUNCTION_DECL)
8678 arg = default_conversion (arg);
8680 if (!get_nonnull_operand (arg, &arg_num))
8682 error ("nonnull argument has invalid operand number (argument %lu)",
8683 (unsigned long) attr_arg_num);
8684 *no_add_attrs = true;
8685 return NULL_TREE;
8688 if (prototype_p (type))
8690 function_args_iterator iter;
8691 tree argument;
8693 function_args_iter_init (&iter, type);
8694 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8696 argument = function_args_iter_cond (&iter);
8697 if (argument == NULL_TREE || ck_num == arg_num)
8698 break;
8701 if (!argument
8702 || TREE_CODE (argument) == VOID_TYPE)
8704 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8705 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8706 *no_add_attrs = true;
8707 return NULL_TREE;
8710 if (TREE_CODE (argument) != POINTER_TYPE)
8712 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8713 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8714 *no_add_attrs = true;
8715 return NULL_TREE;
8720 return NULL_TREE;
8723 /* Check the argument list of a function call for null in argument slots
8724 that are marked as requiring a non-null pointer argument. The NARGS
8725 arguments are passed in the array ARGARRAY.
8728 static void
8729 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8731 tree a;
8732 int i;
8734 attrs = lookup_attribute ("nonnull", attrs);
8735 if (attrs == NULL_TREE)
8736 return;
8738 a = attrs;
8739 /* See if any of the nonnull attributes has no arguments. If so,
8740 then every pointer argument is checked (in which case the check
8741 for pointer type is done in check_nonnull_arg). */
8742 if (TREE_VALUE (a) != NULL_TREE)
8744 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8745 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8747 if (a != NULL_TREE)
8748 for (i = 0; i < nargs; i++)
8749 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8750 i + 1);
8751 else
8753 /* Walk the argument list. If we encounter an argument number we
8754 should check for non-null, do it. */
8755 for (i = 0; i < nargs; i++)
8757 for (a = attrs; ; a = TREE_CHAIN (a))
8759 a = lookup_attribute ("nonnull", a);
8760 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8761 break;
8764 if (a != NULL_TREE)
8765 check_function_arguments_recurse (check_nonnull_arg, NULL,
8766 argarray[i], i + 1);
8771 /* Check that the Nth argument of a function call (counting backwards
8772 from the end) is a (pointer)0. The NARGS arguments are passed in the
8773 array ARGARRAY. */
8775 static void
8776 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8778 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8780 if (attr)
8782 int len = 0;
8783 int pos = 0;
8784 tree sentinel;
8785 function_args_iterator iter;
8786 tree t;
8788 /* Skip over the named arguments. */
8789 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8791 if (len == nargs)
8792 break;
8793 len++;
8796 if (TREE_VALUE (attr))
8798 tree p = TREE_VALUE (TREE_VALUE (attr));
8799 pos = TREE_INT_CST_LOW (p);
8802 /* The sentinel must be one of the varargs, i.e.
8803 in position >= the number of fixed arguments. */
8804 if ((nargs - 1 - pos) < len)
8806 warning (OPT_Wformat_,
8807 "not enough variable arguments to fit a sentinel");
8808 return;
8811 /* Validate the sentinel. */
8812 sentinel = argarray[nargs - 1 - pos];
8813 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8814 || !integer_zerop (sentinel))
8815 /* Although __null (in C++) is only an integer we allow it
8816 nevertheless, as we are guaranteed that it's exactly
8817 as wide as a pointer, and we don't want to force
8818 users to cast the NULL they have written there.
8819 We warn with -Wstrict-null-sentinel, though. */
8820 && (warn_strict_null_sentinel || null_node != sentinel))
8821 warning (OPT_Wformat_, "missing sentinel in function call");
8825 /* Helper for check_function_nonnull; given a list of operands which
8826 must be non-null in ARGS, determine if operand PARAM_NUM should be
8827 checked. */
8829 static bool
8830 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8832 unsigned HOST_WIDE_INT arg_num = 0;
8834 for (; args; args = TREE_CHAIN (args))
8836 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8838 gcc_assert (found);
8840 if (arg_num == param_num)
8841 return true;
8843 return false;
8846 /* Check that the function argument PARAM (which is operand number
8847 PARAM_NUM) is non-null. This is called by check_function_nonnull
8848 via check_function_arguments_recurse. */
8850 static void
8851 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8852 unsigned HOST_WIDE_INT param_num)
8854 /* Just skip checking the argument if it's not a pointer. This can
8855 happen if the "nonnull" attribute was given without an operand
8856 list (which means to check every pointer argument). */
8858 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8859 return;
8861 if (integer_zerop (param))
8862 warning (OPT_Wnonnull, "null argument where non-null required "
8863 "(argument %lu)", (unsigned long) param_num);
8866 /* Helper for nonnull attribute handling; fetch the operand number
8867 from the attribute argument list. */
8869 static bool
8870 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8872 /* Verify the arg number is a small constant. */
8873 if (tree_fits_uhwi_p (arg_num_expr))
8875 *valp = TREE_INT_CST_LOW (arg_num_expr);
8876 return true;
8878 else
8879 return false;
8882 /* Handle a "nothrow" attribute; arguments as in
8883 struct attribute_spec.handler. */
8885 static tree
8886 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8887 int ARG_UNUSED (flags), bool *no_add_attrs)
8889 if (TREE_CODE (*node) == FUNCTION_DECL)
8890 TREE_NOTHROW (*node) = 1;
8891 /* ??? TODO: Support types. */
8892 else
8894 warning (OPT_Wattributes, "%qE attribute ignored", name);
8895 *no_add_attrs = true;
8898 return NULL_TREE;
8901 /* Handle a "cleanup" attribute; arguments as in
8902 struct attribute_spec.handler. */
8904 static tree
8905 handle_cleanup_attribute (tree *node, tree name, tree args,
8906 int ARG_UNUSED (flags), bool *no_add_attrs)
8908 tree decl = *node;
8909 tree cleanup_id, cleanup_decl;
8911 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8912 for global destructors in C++. This requires infrastructure that
8913 we don't have generically at the moment. It's also not a feature
8914 we'd be missing too much, since we do have attribute constructor. */
8915 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8917 warning (OPT_Wattributes, "%qE attribute ignored", name);
8918 *no_add_attrs = true;
8919 return NULL_TREE;
8922 /* Verify that the argument is a function in scope. */
8923 /* ??? We could support pointers to functions here as well, if
8924 that was considered desirable. */
8925 cleanup_id = TREE_VALUE (args);
8926 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8928 error ("cleanup argument not an identifier");
8929 *no_add_attrs = true;
8930 return NULL_TREE;
8932 cleanup_decl = lookup_name (cleanup_id);
8933 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8935 error ("cleanup argument not a function");
8936 *no_add_attrs = true;
8937 return NULL_TREE;
8940 /* That the function has proper type is checked with the
8941 eventual call to build_function_call. */
8943 return NULL_TREE;
8946 /* Handle a "warn_unused_result" attribute. No special handling. */
8948 static tree
8949 handle_warn_unused_result_attribute (tree *node, tree name,
8950 tree ARG_UNUSED (args),
8951 int ARG_UNUSED (flags), bool *no_add_attrs)
8953 /* Ignore the attribute for functions not returning any value. */
8954 if (VOID_TYPE_P (TREE_TYPE (*node)))
8956 warning (OPT_Wattributes, "%qE attribute ignored", name);
8957 *no_add_attrs = true;
8960 return NULL_TREE;
8963 /* Handle a "sentinel" attribute. */
8965 static tree
8966 handle_sentinel_attribute (tree *node, tree name, tree args,
8967 int ARG_UNUSED (flags), bool *no_add_attrs)
8969 if (!prototype_p (*node))
8971 warning (OPT_Wattributes,
8972 "%qE attribute requires prototypes with named arguments", name);
8973 *no_add_attrs = true;
8975 else
8977 if (!stdarg_p (*node))
8979 warning (OPT_Wattributes,
8980 "%qE attribute only applies to variadic functions", name);
8981 *no_add_attrs = true;
8985 if (args)
8987 tree position = TREE_VALUE (args);
8988 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8989 && TREE_CODE (position) != FUNCTION_DECL)
8990 position = default_conversion (position);
8992 if (TREE_CODE (position) != INTEGER_CST
8993 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
8995 warning (OPT_Wattributes,
8996 "requested position is not an integer constant");
8997 *no_add_attrs = true;
8999 else
9001 if (tree_int_cst_lt (position, integer_zero_node))
9003 warning (OPT_Wattributes,
9004 "requested position is less than zero");
9005 *no_add_attrs = true;
9010 return NULL_TREE;
9013 /* Handle a "type_generic" attribute. */
9015 static tree
9016 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9017 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9018 bool * ARG_UNUSED (no_add_attrs))
9020 /* Ensure we have a function type. */
9021 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9023 /* Ensure we have a variadic function. */
9024 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9026 return NULL_TREE;
9029 /* Handle a "target" attribute. */
9031 static tree
9032 handle_target_attribute (tree *node, tree name, tree args, int flags,
9033 bool *no_add_attrs)
9035 /* Ensure we have a function type. */
9036 if (TREE_CODE (*node) != FUNCTION_DECL)
9038 warning (OPT_Wattributes, "%qE attribute ignored", name);
9039 *no_add_attrs = true;
9041 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9042 flags))
9043 *no_add_attrs = true;
9045 return NULL_TREE;
9048 /* Arguments being collected for optimization. */
9049 typedef const char *const_char_p; /* For DEF_VEC_P. */
9050 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9053 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9054 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9055 false for #pragma GCC optimize. */
9057 bool
9058 parse_optimize_options (tree args, bool attr_p)
9060 bool ret = true;
9061 unsigned opt_argc;
9062 unsigned i;
9063 int saved_flag_strict_aliasing;
9064 const char **opt_argv;
9065 struct cl_decoded_option *decoded_options;
9066 unsigned int decoded_options_count;
9067 tree ap;
9069 /* Build up argv vector. Just in case the string is stored away, use garbage
9070 collected strings. */
9071 vec_safe_truncate (optimize_args, 0);
9072 vec_safe_push (optimize_args, (const char *) NULL);
9074 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9076 tree value = TREE_VALUE (ap);
9078 if (TREE_CODE (value) == INTEGER_CST)
9080 char buffer[20];
9081 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9082 vec_safe_push (optimize_args, ggc_strdup (buffer));
9085 else if (TREE_CODE (value) == STRING_CST)
9087 /* Split string into multiple substrings. */
9088 size_t len = TREE_STRING_LENGTH (value);
9089 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9090 char *end = p + len;
9091 char *comma;
9092 char *next_p = p;
9094 while (next_p != NULL)
9096 size_t len2;
9097 char *q, *r;
9099 p = next_p;
9100 comma = strchr (p, ',');
9101 if (comma)
9103 len2 = comma - p;
9104 *comma = '\0';
9105 next_p = comma+1;
9107 else
9109 len2 = end - p;
9110 next_p = NULL;
9113 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9115 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9116 options. */
9117 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9119 ret = false;
9120 if (attr_p)
9121 warning (OPT_Wattributes,
9122 "bad option %s to optimize attribute", p);
9123 else
9124 warning (OPT_Wpragmas,
9125 "bad option %s to pragma attribute", p);
9126 continue;
9129 if (*p != '-')
9131 *r++ = '-';
9133 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9134 itself is -Os, and any other switch begins with a -f. */
9135 if ((*p >= '0' && *p <= '9')
9136 || (p[0] == 's' && p[1] == '\0'))
9137 *r++ = 'O';
9138 else if (*p != 'O')
9139 *r++ = 'f';
9142 memcpy (r, p, len2);
9143 r[len2] = '\0';
9144 vec_safe_push (optimize_args, (const char *) q);
9150 opt_argc = optimize_args->length ();
9151 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9153 for (i = 1; i < opt_argc; i++)
9154 opt_argv[i] = (*optimize_args)[i];
9156 saved_flag_strict_aliasing = flag_strict_aliasing;
9158 /* Now parse the options. */
9159 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9160 &decoded_options,
9161 &decoded_options_count);
9162 decode_options (&global_options, &global_options_set,
9163 decoded_options, decoded_options_count,
9164 input_location, global_dc);
9166 targetm.override_options_after_change();
9168 /* Don't allow changing -fstrict-aliasing. */
9169 flag_strict_aliasing = saved_flag_strict_aliasing;
9171 optimize_args->truncate (0);
9172 return ret;
9175 /* For handling "optimize" attribute. arguments as in
9176 struct attribute_spec.handler. */
9178 static tree
9179 handle_optimize_attribute (tree *node, tree name, tree args,
9180 int ARG_UNUSED (flags), bool *no_add_attrs)
9182 /* Ensure we have a function type. */
9183 if (TREE_CODE (*node) != FUNCTION_DECL)
9185 warning (OPT_Wattributes, "%qE attribute ignored", name);
9186 *no_add_attrs = true;
9188 else
9190 struct cl_optimization cur_opts;
9191 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9193 /* Save current options. */
9194 cl_optimization_save (&cur_opts, &global_options);
9196 /* If we previously had some optimization options, use them as the
9197 default. */
9198 if (old_opts)
9199 cl_optimization_restore (&global_options,
9200 TREE_OPTIMIZATION (old_opts));
9202 /* Parse options, and update the vector. */
9203 parse_optimize_options (args, true);
9204 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9205 = build_optimization_node (&global_options);
9207 /* Restore current options. */
9208 cl_optimization_restore (&global_options, &cur_opts);
9211 return NULL_TREE;
9214 /* Handle a "no_split_stack" attribute. */
9216 static tree
9217 handle_no_split_stack_attribute (tree *node, tree name,
9218 tree ARG_UNUSED (args),
9219 int ARG_UNUSED (flags),
9220 bool *no_add_attrs)
9222 tree decl = *node;
9224 if (TREE_CODE (decl) != FUNCTION_DECL)
9226 error_at (DECL_SOURCE_LOCATION (decl),
9227 "%qE attribute applies only to functions", name);
9228 *no_add_attrs = true;
9230 else if (DECL_INITIAL (decl))
9232 error_at (DECL_SOURCE_LOCATION (decl),
9233 "can%'t set %qE attribute after definition", name);
9234 *no_add_attrs = true;
9237 return NULL_TREE;
9240 /* Handle a "returns_nonnull" attribute; arguments as in
9241 struct attribute_spec.handler. */
9243 static tree
9244 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9245 bool *no_add_attrs)
9247 // Even without a prototype we still have a return type we can check.
9248 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9250 error ("returns_nonnull attribute on a function not returning a pointer");
9251 *no_add_attrs = true;
9253 return NULL_TREE;
9256 /* Handle a "designated_init" attribute; arguments as in
9257 struct attribute_spec.handler. */
9259 static tree
9260 handle_designated_init_attribute (tree *node, tree name, tree, int,
9261 bool *no_add_attrs)
9263 if (TREE_CODE (*node) != RECORD_TYPE)
9265 error ("%qE attribute is only valid on %<struct%> type", name);
9266 *no_add_attrs = true;
9268 return NULL_TREE;
9272 /* Check for valid arguments being passed to a function with FNTYPE.
9273 There are NARGS arguments in the array ARGARRAY. */
9274 void
9275 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9277 /* Check for null being passed in a pointer argument that must be
9278 non-null. We also need to do this if format checking is enabled. */
9280 if (warn_nonnull)
9281 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9283 /* Check for errors in format strings. */
9285 if (warn_format || warn_suggest_attribute_format)
9286 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9288 if (warn_format)
9289 check_function_sentinel (fntype, nargs, argarray);
9292 /* Generic argument checking recursion routine. PARAM is the argument to
9293 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9294 once the argument is resolved. CTX is context for the callback. */
9295 void
9296 check_function_arguments_recurse (void (*callback)
9297 (void *, tree, unsigned HOST_WIDE_INT),
9298 void *ctx, tree param,
9299 unsigned HOST_WIDE_INT param_num)
9301 if (CONVERT_EXPR_P (param)
9302 && (TYPE_PRECISION (TREE_TYPE (param))
9303 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9305 /* Strip coercion. */
9306 check_function_arguments_recurse (callback, ctx,
9307 TREE_OPERAND (param, 0), param_num);
9308 return;
9311 if (TREE_CODE (param) == CALL_EXPR)
9313 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9314 tree attrs;
9315 bool found_format_arg = false;
9317 /* See if this is a call to a known internationalization function
9318 that modifies a format arg. Such a function may have multiple
9319 format_arg attributes (for example, ngettext). */
9321 for (attrs = TYPE_ATTRIBUTES (type);
9322 attrs;
9323 attrs = TREE_CHAIN (attrs))
9324 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9326 tree inner_arg;
9327 tree format_num_expr;
9328 int format_num;
9329 int i;
9330 call_expr_arg_iterator iter;
9332 /* Extract the argument number, which was previously checked
9333 to be valid. */
9334 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9336 format_num = tree_to_uhwi (format_num_expr);
9338 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9339 inner_arg != 0;
9340 inner_arg = next_call_expr_arg (&iter), i++)
9341 if (i == format_num)
9343 check_function_arguments_recurse (callback, ctx,
9344 inner_arg, param_num);
9345 found_format_arg = true;
9346 break;
9350 /* If we found a format_arg attribute and did a recursive check,
9351 we are done with checking this argument. Otherwise, we continue
9352 and this will be considered a non-literal. */
9353 if (found_format_arg)
9354 return;
9357 if (TREE_CODE (param) == COND_EXPR)
9359 /* Check both halves of the conditional expression. */
9360 check_function_arguments_recurse (callback, ctx,
9361 TREE_OPERAND (param, 1), param_num);
9362 check_function_arguments_recurse (callback, ctx,
9363 TREE_OPERAND (param, 2), param_num);
9364 return;
9367 (*callback) (ctx, param, param_num);
9370 /* Checks for a builtin function FNDECL that the number of arguments
9371 NARGS against the required number REQUIRED and issues an error if
9372 there is a mismatch. Returns true if the number of arguments is
9373 correct, otherwise false. */
9375 static bool
9376 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9378 if (nargs < required)
9380 error_at (input_location,
9381 "not enough arguments to function %qE", fndecl);
9382 return false;
9384 else if (nargs > required)
9386 error_at (input_location,
9387 "too many arguments to function %qE", fndecl);
9388 return false;
9390 return true;
9393 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9394 Returns false if there was an error, otherwise true. */
9396 bool
9397 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9399 if (!DECL_BUILT_IN (fndecl)
9400 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9401 return true;
9403 switch (DECL_FUNCTION_CODE (fndecl))
9405 case BUILT_IN_CONSTANT_P:
9406 return builtin_function_validate_nargs (fndecl, nargs, 1);
9408 case BUILT_IN_ISFINITE:
9409 case BUILT_IN_ISINF:
9410 case BUILT_IN_ISINF_SIGN:
9411 case BUILT_IN_ISNAN:
9412 case BUILT_IN_ISNORMAL:
9413 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9415 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9417 error ("non-floating-point argument in call to "
9418 "function %qE", fndecl);
9419 return false;
9421 return true;
9423 return false;
9425 case BUILT_IN_ISGREATER:
9426 case BUILT_IN_ISGREATEREQUAL:
9427 case BUILT_IN_ISLESS:
9428 case BUILT_IN_ISLESSEQUAL:
9429 case BUILT_IN_ISLESSGREATER:
9430 case BUILT_IN_ISUNORDERED:
9431 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9433 enum tree_code code0, code1;
9434 code0 = TREE_CODE (TREE_TYPE (args[0]));
9435 code1 = TREE_CODE (TREE_TYPE (args[1]));
9436 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9437 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9438 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9440 error ("non-floating-point arguments in call to "
9441 "function %qE", fndecl);
9442 return false;
9444 return true;
9446 return false;
9448 case BUILT_IN_FPCLASSIFY:
9449 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9451 unsigned i;
9453 for (i=0; i<5; i++)
9454 if (TREE_CODE (args[i]) != INTEGER_CST)
9456 error ("non-const integer argument %u in call to function %qE",
9457 i+1, fndecl);
9458 return false;
9461 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9463 error ("non-floating-point argument in call to function %qE",
9464 fndecl);
9465 return false;
9467 return true;
9469 return false;
9471 case BUILT_IN_ASSUME_ALIGNED:
9472 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9474 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9476 error ("non-integer argument 3 in call to function %qE", fndecl);
9477 return false;
9479 return true;
9481 return false;
9483 default:
9484 return true;
9488 /* Function to help qsort sort FIELD_DECLs by name order. */
9491 field_decl_cmp (const void *x_p, const void *y_p)
9493 const tree *const x = (const tree *const) x_p;
9494 const tree *const y = (const tree *const) y_p;
9496 if (DECL_NAME (*x) == DECL_NAME (*y))
9497 /* A nontype is "greater" than a type. */
9498 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9499 if (DECL_NAME (*x) == NULL_TREE)
9500 return -1;
9501 if (DECL_NAME (*y) == NULL_TREE)
9502 return 1;
9503 if (DECL_NAME (*x) < DECL_NAME (*y))
9504 return -1;
9505 return 1;
9508 static struct {
9509 gt_pointer_operator new_value;
9510 void *cookie;
9511 } resort_data;
9513 /* This routine compares two fields like field_decl_cmp but using the
9514 pointer operator in resort_data. */
9516 static int
9517 resort_field_decl_cmp (const void *x_p, const void *y_p)
9519 const tree *const x = (const tree *const) x_p;
9520 const tree *const y = (const tree *const) y_p;
9522 if (DECL_NAME (*x) == DECL_NAME (*y))
9523 /* A nontype is "greater" than a type. */
9524 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9525 if (DECL_NAME (*x) == NULL_TREE)
9526 return -1;
9527 if (DECL_NAME (*y) == NULL_TREE)
9528 return 1;
9530 tree d1 = DECL_NAME (*x);
9531 tree d2 = DECL_NAME (*y);
9532 resort_data.new_value (&d1, resort_data.cookie);
9533 resort_data.new_value (&d2, resort_data.cookie);
9534 if (d1 < d2)
9535 return -1;
9537 return 1;
9540 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9542 void
9543 resort_sorted_fields (void *obj,
9544 void * ARG_UNUSED (orig_obj),
9545 gt_pointer_operator new_value,
9546 void *cookie)
9548 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9549 resort_data.new_value = new_value;
9550 resort_data.cookie = cookie;
9551 qsort (&sf->elts[0], sf->len, sizeof (tree),
9552 resort_field_decl_cmp);
9555 /* Subroutine of c_parse_error.
9556 Return the result of concatenating LHS and RHS. RHS is really
9557 a string literal, its first character is indicated by RHS_START and
9558 RHS_SIZE is its length (including the terminating NUL character).
9560 The caller is responsible for deleting the returned pointer. */
9562 static char *
9563 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9565 const int lhs_size = strlen (lhs);
9566 char *result = XNEWVEC (char, lhs_size + rhs_size);
9567 strncpy (result, lhs, lhs_size);
9568 strncpy (result + lhs_size, rhs_start, rhs_size);
9569 return result;
9572 /* Issue the error given by GMSGID, indicating that it occurred before
9573 TOKEN, which had the associated VALUE. */
9575 void
9576 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9577 tree value, unsigned char token_flags)
9579 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9581 char *message = NULL;
9583 if (token_type == CPP_EOF)
9584 message = catenate_messages (gmsgid, " at end of input");
9585 else if (token_type == CPP_CHAR
9586 || token_type == CPP_WCHAR
9587 || token_type == CPP_CHAR16
9588 || token_type == CPP_CHAR32)
9590 unsigned int val = TREE_INT_CST_LOW (value);
9591 const char *prefix;
9593 switch (token_type)
9595 default:
9596 prefix = "";
9597 break;
9598 case CPP_WCHAR:
9599 prefix = "L";
9600 break;
9601 case CPP_CHAR16:
9602 prefix = "u";
9603 break;
9604 case CPP_CHAR32:
9605 prefix = "U";
9606 break;
9609 if (val <= UCHAR_MAX && ISGRAPH (val))
9610 message = catenate_messages (gmsgid, " before %s'%c'");
9611 else
9612 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9614 error (message, prefix, val);
9615 free (message);
9616 message = NULL;
9618 else if (token_type == CPP_CHAR_USERDEF
9619 || token_type == CPP_WCHAR_USERDEF
9620 || token_type == CPP_CHAR16_USERDEF
9621 || token_type == CPP_CHAR32_USERDEF)
9622 message = catenate_messages (gmsgid,
9623 " before user-defined character literal");
9624 else if (token_type == CPP_STRING_USERDEF
9625 || token_type == CPP_WSTRING_USERDEF
9626 || token_type == CPP_STRING16_USERDEF
9627 || token_type == CPP_STRING32_USERDEF
9628 || token_type == CPP_UTF8STRING_USERDEF)
9629 message = catenate_messages (gmsgid, " before user-defined string literal");
9630 else if (token_type == CPP_STRING
9631 || token_type == CPP_WSTRING
9632 || token_type == CPP_STRING16
9633 || token_type == CPP_STRING32
9634 || token_type == CPP_UTF8STRING)
9635 message = catenate_messages (gmsgid, " before string constant");
9636 else if (token_type == CPP_NUMBER)
9637 message = catenate_messages (gmsgid, " before numeric constant");
9638 else if (token_type == CPP_NAME)
9640 message = catenate_messages (gmsgid, " before %qE");
9641 error (message, value);
9642 free (message);
9643 message = NULL;
9645 else if (token_type == CPP_PRAGMA)
9646 message = catenate_messages (gmsgid, " before %<#pragma%>");
9647 else if (token_type == CPP_PRAGMA_EOL)
9648 message = catenate_messages (gmsgid, " before end of line");
9649 else if (token_type == CPP_DECLTYPE)
9650 message = catenate_messages (gmsgid, " before %<decltype%>");
9651 else if (token_type < N_TTYPES)
9653 message = catenate_messages (gmsgid, " before %qs token");
9654 error (message, cpp_type2name (token_type, token_flags));
9655 free (message);
9656 message = NULL;
9658 else
9659 error (gmsgid);
9661 if (message)
9663 error (message);
9664 free (message);
9666 #undef catenate_messages
9669 /* Return the gcc option code associated with the reason for a cpp
9670 message, or 0 if none. */
9672 static int
9673 c_option_controlling_cpp_error (int reason)
9675 const struct cpp_reason_option_codes_t *entry;
9677 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9679 if (entry->reason == reason)
9680 return entry->option_code;
9682 return 0;
9685 /* Callback from cpp_error for PFILE to print diagnostics from the
9686 preprocessor. The diagnostic is of type LEVEL, with REASON set
9687 to the reason code if LEVEL is represents a warning, at location
9688 LOCATION unless this is after lexing and the compiler's location
9689 should be used instead, with column number possibly overridden by
9690 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9691 the arguments. Returns true if a diagnostic was emitted, false
9692 otherwise. */
9694 bool
9695 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9696 location_t location, unsigned int column_override,
9697 const char *msg, va_list *ap)
9699 diagnostic_info diagnostic;
9700 diagnostic_t dlevel;
9701 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9702 bool ret;
9704 switch (level)
9706 case CPP_DL_WARNING_SYSHDR:
9707 if (flag_no_output)
9708 return false;
9709 global_dc->dc_warn_system_headers = 1;
9710 /* Fall through. */
9711 case CPP_DL_WARNING:
9712 if (flag_no_output)
9713 return false;
9714 dlevel = DK_WARNING;
9715 break;
9716 case CPP_DL_PEDWARN:
9717 if (flag_no_output && !flag_pedantic_errors)
9718 return false;
9719 dlevel = DK_PEDWARN;
9720 break;
9721 case CPP_DL_ERROR:
9722 dlevel = DK_ERROR;
9723 break;
9724 case CPP_DL_ICE:
9725 dlevel = DK_ICE;
9726 break;
9727 case CPP_DL_NOTE:
9728 dlevel = DK_NOTE;
9729 break;
9730 case CPP_DL_FATAL:
9731 dlevel = DK_FATAL;
9732 break;
9733 default:
9734 gcc_unreachable ();
9736 if (done_lexing)
9737 location = input_location;
9738 diagnostic_set_info_translated (&diagnostic, msg, ap,
9739 location, dlevel);
9740 if (column_override)
9741 diagnostic_override_column (&diagnostic, column_override);
9742 diagnostic_override_option_index (&diagnostic,
9743 c_option_controlling_cpp_error (reason));
9744 ret = report_diagnostic (&diagnostic);
9745 if (level == CPP_DL_WARNING_SYSHDR)
9746 global_dc->dc_warn_system_headers = save_warn_system_headers;
9747 return ret;
9750 /* Convert a character from the host to the target execution character
9751 set. cpplib handles this, mostly. */
9753 HOST_WIDE_INT
9754 c_common_to_target_charset (HOST_WIDE_INT c)
9756 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9757 zero-extended under -fno-signed-char. cpplib insists that characters
9758 and character constants are always unsigned. Hence we must convert
9759 back and forth. */
9760 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9762 uc = cpp_host_to_exec_charset (parse_in, uc);
9764 if (flag_signed_char)
9765 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9766 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9767 else
9768 return uc;
9771 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9772 references with an INDIRECT_REF of a constant at the bottom; much like the
9773 traditional rendering of offsetof as a macro. Return the folded result. */
9775 tree
9776 fold_offsetof_1 (tree expr)
9778 tree base, off, t;
9780 switch (TREE_CODE (expr))
9782 case ERROR_MARK:
9783 return expr;
9785 case VAR_DECL:
9786 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9787 return error_mark_node;
9789 case CALL_EXPR:
9790 case TARGET_EXPR:
9791 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9792 return error_mark_node;
9794 case NOP_EXPR:
9795 case INDIRECT_REF:
9796 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9798 error ("cannot apply %<offsetof%> to a non constant address");
9799 return error_mark_node;
9801 return TREE_OPERAND (expr, 0);
9803 case COMPONENT_REF:
9804 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9805 if (base == error_mark_node)
9806 return base;
9808 t = TREE_OPERAND (expr, 1);
9809 if (DECL_C_BIT_FIELD (t))
9811 error ("attempt to take address of bit-field structure "
9812 "member %qD", t);
9813 return error_mark_node;
9815 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9816 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9817 / BITS_PER_UNIT));
9818 break;
9820 case ARRAY_REF:
9821 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9822 if (base == error_mark_node)
9823 return base;
9825 t = TREE_OPERAND (expr, 1);
9827 /* Check if the offset goes beyond the upper bound of the array. */
9828 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9830 tree upbound = array_ref_up_bound (expr);
9831 if (upbound != NULL_TREE
9832 && TREE_CODE (upbound) == INTEGER_CST
9833 && !tree_int_cst_equal (upbound,
9834 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9836 upbound = size_binop (PLUS_EXPR, upbound,
9837 build_int_cst (TREE_TYPE (upbound), 1));
9838 if (tree_int_cst_lt (upbound, t))
9840 tree v;
9842 for (v = TREE_OPERAND (expr, 0);
9843 TREE_CODE (v) == COMPONENT_REF;
9844 v = TREE_OPERAND (v, 0))
9845 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9846 == RECORD_TYPE)
9848 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9849 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9850 if (TREE_CODE (fld_chain) == FIELD_DECL)
9851 break;
9853 if (fld_chain)
9854 break;
9856 /* Don't warn if the array might be considered a poor
9857 man's flexible array member with a very permissive
9858 definition thereof. */
9859 if (TREE_CODE (v) == ARRAY_REF
9860 || TREE_CODE (v) == COMPONENT_REF)
9861 warning (OPT_Warray_bounds,
9862 "index %E denotes an offset "
9863 "greater than size of %qT",
9864 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9869 t = convert (sizetype, t);
9870 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9871 break;
9873 case COMPOUND_EXPR:
9874 /* Handle static members of volatile structs. */
9875 t = TREE_OPERAND (expr, 1);
9876 gcc_assert (TREE_CODE (t) == VAR_DECL);
9877 return fold_offsetof_1 (t);
9879 default:
9880 gcc_unreachable ();
9883 return fold_build_pointer_plus (base, off);
9886 /* Likewise, but convert it to the return type of offsetof. */
9888 tree
9889 fold_offsetof (tree expr)
9891 return convert (size_type_node, fold_offsetof_1 (expr));
9894 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9895 expression, because B will always be true. */
9897 void
9898 warn_for_omitted_condop (location_t location, tree cond)
9900 if (truth_value_p (TREE_CODE (cond)))
9901 warning_at (location, OPT_Wparentheses,
9902 "the omitted middle operand in ?: will always be %<true%>, "
9903 "suggest explicit middle operand");
9906 /* Give an error for storing into ARG, which is 'const'. USE indicates
9907 how ARG was being used. */
9909 void
9910 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9912 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9913 || use == lv_asm);
9914 /* Using this macro rather than (for example) arrays of messages
9915 ensures that all the format strings are checked at compile
9916 time. */
9917 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9918 : (use == lv_increment ? (I) \
9919 : (use == lv_decrement ? (D) : (AS))))
9920 if (TREE_CODE (arg) == COMPONENT_REF)
9922 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9923 error_at (loc, READONLY_MSG (G_("assignment of member "
9924 "%qD in read-only object"),
9925 G_("increment of member "
9926 "%qD in read-only object"),
9927 G_("decrement of member "
9928 "%qD in read-only object"),
9929 G_("member %qD in read-only object "
9930 "used as %<asm%> output")),
9931 TREE_OPERAND (arg, 1));
9932 else
9933 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9934 G_("increment of read-only member %qD"),
9935 G_("decrement of read-only member %qD"),
9936 G_("read-only member %qD used as %<asm%> output")),
9937 TREE_OPERAND (arg, 1));
9939 else if (TREE_CODE (arg) == VAR_DECL)
9940 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9941 G_("increment of read-only variable %qD"),
9942 G_("decrement of read-only variable %qD"),
9943 G_("read-only variable %qD used as %<asm%> output")),
9944 arg);
9945 else if (TREE_CODE (arg) == PARM_DECL)
9946 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9947 G_("increment of read-only parameter %qD"),
9948 G_("decrement of read-only parameter %qD"),
9949 G_("read-only parameter %qD use as %<asm%> output")),
9950 arg);
9951 else if (TREE_CODE (arg) == RESULT_DECL)
9953 gcc_assert (c_dialect_cxx ());
9954 error_at (loc, READONLY_MSG (G_("assignment of "
9955 "read-only named return value %qD"),
9956 G_("increment of "
9957 "read-only named return value %qD"),
9958 G_("decrement of "
9959 "read-only named return value %qD"),
9960 G_("read-only named return value %qD "
9961 "used as %<asm%>output")),
9962 arg);
9964 else if (TREE_CODE (arg) == FUNCTION_DECL)
9965 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9966 G_("increment of function %qD"),
9967 G_("decrement of function %qD"),
9968 G_("function %qD used as %<asm%> output")),
9969 arg);
9970 else
9971 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9972 G_("increment of read-only location %qE"),
9973 G_("decrement of read-only location %qE"),
9974 G_("read-only location %qE used as %<asm%> output")),
9975 arg);
9978 /* Print an error message for an invalid lvalue. USE says
9979 how the lvalue is being used and so selects the error message. LOC
9980 is the location for the error. */
9982 void
9983 lvalue_error (location_t loc, enum lvalue_use use)
9985 switch (use)
9987 case lv_assign:
9988 error_at (loc, "lvalue required as left operand of assignment");
9989 break;
9990 case lv_increment:
9991 error_at (loc, "lvalue required as increment operand");
9992 break;
9993 case lv_decrement:
9994 error_at (loc, "lvalue required as decrement operand");
9995 break;
9996 case lv_addressof:
9997 error_at (loc, "lvalue required as unary %<&%> operand");
9998 break;
9999 case lv_asm:
10000 error_at (loc, "lvalue required in asm statement");
10001 break;
10002 default:
10003 gcc_unreachable ();
10007 /* Print an error message for an invalid indirection of type TYPE.
10008 ERRSTRING is the name of the operator for the indirection. */
10010 void
10011 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10013 switch (errstring)
10015 case RO_NULL:
10016 gcc_assert (c_dialect_cxx ());
10017 error_at (loc, "invalid type argument (have %qT)", type);
10018 break;
10019 case RO_ARRAY_INDEXING:
10020 error_at (loc,
10021 "invalid type argument of array indexing (have %qT)",
10022 type);
10023 break;
10024 case RO_UNARY_STAR:
10025 error_at (loc,
10026 "invalid type argument of unary %<*%> (have %qT)",
10027 type);
10028 break;
10029 case RO_ARROW:
10030 error_at (loc,
10031 "invalid type argument of %<->%> (have %qT)",
10032 type);
10033 break;
10034 case RO_ARROW_STAR:
10035 error_at (loc,
10036 "invalid type argument of %<->*%> (have %qT)",
10037 type);
10038 break;
10039 case RO_IMPLICIT_CONVERSION:
10040 error_at (loc,
10041 "invalid type argument of implicit conversion (have %qT)",
10042 type);
10043 break;
10044 default:
10045 gcc_unreachable ();
10049 /* *PTYPE is an incomplete array. Complete it with a domain based on
10050 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10051 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10052 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10055 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10057 tree maxindex, type, main_type, elt, unqual_elt;
10058 int failure = 0, quals;
10059 hashval_t hashcode = 0;
10060 bool overflow_p = false;
10062 maxindex = size_zero_node;
10063 if (initial_value)
10065 if (TREE_CODE (initial_value) == STRING_CST)
10067 int eltsize
10068 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10069 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10071 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10073 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10075 if (vec_safe_is_empty (v))
10077 if (pedantic)
10078 failure = 3;
10079 maxindex = ssize_int (-1);
10081 else
10083 tree curindex;
10084 unsigned HOST_WIDE_INT cnt;
10085 constructor_elt *ce;
10086 bool fold_p = false;
10088 if ((*v)[0].index)
10089 maxindex = (*v)[0].index, fold_p = true;
10091 curindex = maxindex;
10093 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10095 bool curfold_p = false;
10096 if (ce->index)
10097 curindex = ce->index, curfold_p = true;
10098 else
10100 if (fold_p)
10102 /* Since we treat size types now as ordinary
10103 unsigned types, we need an explicit overflow
10104 check. */
10105 tree orig = curindex;
10106 curindex = fold_convert (sizetype, curindex);
10107 overflow_p |= tree_int_cst_lt (curindex, orig);
10109 curindex = size_binop (PLUS_EXPR, curindex,
10110 size_one_node);
10112 if (tree_int_cst_lt (maxindex, curindex))
10113 maxindex = curindex, fold_p = curfold_p;
10115 if (fold_p)
10117 tree orig = maxindex;
10118 maxindex = fold_convert (sizetype, maxindex);
10119 overflow_p |= tree_int_cst_lt (maxindex, orig);
10123 else
10125 /* Make an error message unless that happened already. */
10126 if (initial_value != error_mark_node)
10127 failure = 1;
10130 else
10132 failure = 2;
10133 if (!do_default)
10134 return failure;
10137 type = *ptype;
10138 elt = TREE_TYPE (type);
10139 quals = TYPE_QUALS (strip_array_types (elt));
10140 if (quals == 0)
10141 unqual_elt = elt;
10142 else
10143 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10145 /* Using build_distinct_type_copy and modifying things afterward instead
10146 of using build_array_type to create a new type preserves all of the
10147 TYPE_LANG_FLAG_? bits that the front end may have set. */
10148 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10149 TREE_TYPE (main_type) = unqual_elt;
10150 TYPE_DOMAIN (main_type)
10151 = build_range_type (TREE_TYPE (maxindex),
10152 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10153 layout_type (main_type);
10155 /* Make sure we have the canonical MAIN_TYPE. */
10156 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10157 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10158 hashcode);
10159 main_type = type_hash_canon (hashcode, main_type);
10161 /* Fix the canonical type. */
10162 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10163 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10164 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10165 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10166 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10167 != TYPE_DOMAIN (main_type)))
10168 TYPE_CANONICAL (main_type)
10169 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10170 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10171 else
10172 TYPE_CANONICAL (main_type) = main_type;
10174 if (quals == 0)
10175 type = main_type;
10176 else
10177 type = c_build_qualified_type (main_type, quals);
10179 if (COMPLETE_TYPE_P (type)
10180 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10181 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10183 error ("size of array is too large");
10184 /* If we proceed with the array type as it is, we'll eventually
10185 crash in tree_to_[su]hwi(). */
10186 type = error_mark_node;
10189 *ptype = type;
10190 return failure;
10193 /* Like c_mark_addressable but don't check register qualifier. */
10194 void
10195 c_common_mark_addressable_vec (tree t)
10197 while (handled_component_p (t))
10198 t = TREE_OPERAND (t, 0);
10199 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10200 return;
10201 TREE_ADDRESSABLE (t) = 1;
10206 /* Used to help initialize the builtin-types.def table. When a type of
10207 the correct size doesn't exist, use error_mark_node instead of NULL.
10208 The later results in segfaults even when a decl using the type doesn't
10209 get invoked. */
10211 tree
10212 builtin_type_for_size (int size, bool unsignedp)
10214 tree type = c_common_type_for_size (size, unsignedp);
10215 return type ? type : error_mark_node;
10218 /* A helper function for resolve_overloaded_builtin in resolving the
10219 overloaded __sync_ builtins. Returns a positive power of 2 if the
10220 first operand of PARAMS is a pointer to a supported data type.
10221 Returns 0 if an error is encountered. */
10223 static int
10224 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10226 tree type;
10227 int size;
10229 if (!params)
10231 error ("too few arguments to function %qE", function);
10232 return 0;
10235 type = TREE_TYPE ((*params)[0]);
10236 if (TREE_CODE (type) == ARRAY_TYPE)
10238 /* Force array-to-pointer decay for C++. */
10239 gcc_assert (c_dialect_cxx());
10240 (*params)[0] = default_conversion ((*params)[0]);
10241 type = TREE_TYPE ((*params)[0]);
10243 if (TREE_CODE (type) != POINTER_TYPE)
10244 goto incompatible;
10246 type = TREE_TYPE (type);
10247 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10248 goto incompatible;
10250 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10251 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10252 return size;
10254 incompatible:
10255 error ("incompatible type for argument %d of %qE", 1, function);
10256 return 0;
10259 /* A helper function for resolve_overloaded_builtin. Adds casts to
10260 PARAMS to make arguments match up with those of FUNCTION. Drops
10261 the variadic arguments at the end. Returns false if some error
10262 was encountered; true on success. */
10264 static bool
10265 sync_resolve_params (location_t loc, tree orig_function, tree function,
10266 vec<tree, va_gc> *params, bool orig_format)
10268 function_args_iterator iter;
10269 tree ptype;
10270 unsigned int parmnum;
10272 function_args_iter_init (&iter, TREE_TYPE (function));
10273 /* We've declared the implementation functions to use "volatile void *"
10274 as the pointer parameter, so we shouldn't get any complaints from the
10275 call to check_function_arguments what ever type the user used. */
10276 function_args_iter_next (&iter);
10277 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10278 ptype = TYPE_MAIN_VARIANT (ptype);
10280 /* For the rest of the values, we need to cast these to FTYPE, so that we
10281 don't get warnings for passing pointer types, etc. */
10282 parmnum = 0;
10283 while (1)
10285 tree val, arg_type;
10287 arg_type = function_args_iter_cond (&iter);
10288 /* XXX void_type_node belies the abstraction. */
10289 if (arg_type == void_type_node)
10290 break;
10292 ++parmnum;
10293 if (params->length () <= parmnum)
10295 error_at (loc, "too few arguments to function %qE", orig_function);
10296 return false;
10299 /* Only convert parameters if arg_type is unsigned integer type with
10300 new format sync routines, i.e. don't attempt to convert pointer
10301 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10302 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10303 kinds). */
10304 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10306 /* Ideally for the first conversion we'd use convert_for_assignment
10307 so that we get warnings for anything that doesn't match the pointer
10308 type. This isn't portable across the C and C++ front ends atm. */
10309 val = (*params)[parmnum];
10310 val = convert (ptype, val);
10311 val = convert (arg_type, val);
10312 (*params)[parmnum] = val;
10315 function_args_iter_next (&iter);
10318 /* __atomic routines are not variadic. */
10319 if (!orig_format && params->length () != parmnum + 1)
10321 error_at (loc, "too many arguments to function %qE", orig_function);
10322 return false;
10325 /* The definition of these primitives is variadic, with the remaining
10326 being "an optional list of variables protected by the memory barrier".
10327 No clue what that's supposed to mean, precisely, but we consider all
10328 call-clobbered variables to be protected so we're safe. */
10329 params->truncate (parmnum + 1);
10331 return true;
10334 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10335 RESULT to make it match the type of the first pointer argument in
10336 PARAMS. */
10338 static tree
10339 sync_resolve_return (tree first_param, tree result, bool orig_format)
10341 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10342 tree rtype = TREE_TYPE (result);
10343 ptype = TYPE_MAIN_VARIANT (ptype);
10345 /* New format doesn't require casting unless the types are the same size. */
10346 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10347 return convert (ptype, result);
10348 else
10349 return result;
10352 /* This function verifies the PARAMS to generic atomic FUNCTION.
10353 It returns the size if all the parameters are the same size, otherwise
10354 0 is returned if the parameters are invalid. */
10356 static int
10357 get_atomic_generic_size (location_t loc, tree function,
10358 vec<tree, va_gc> *params)
10360 unsigned int n_param;
10361 unsigned int n_model;
10362 unsigned int x;
10363 int size_0;
10364 tree type_0;
10366 /* Determine the parameter makeup. */
10367 switch (DECL_FUNCTION_CODE (function))
10369 case BUILT_IN_ATOMIC_EXCHANGE:
10370 n_param = 4;
10371 n_model = 1;
10372 break;
10373 case BUILT_IN_ATOMIC_LOAD:
10374 case BUILT_IN_ATOMIC_STORE:
10375 n_param = 3;
10376 n_model = 1;
10377 break;
10378 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10379 n_param = 6;
10380 n_model = 2;
10381 break;
10382 default:
10383 gcc_unreachable ();
10386 if (vec_safe_length (params) != n_param)
10388 error_at (loc, "incorrect number of arguments to function %qE", function);
10389 return 0;
10392 /* Get type of first parameter, and determine its size. */
10393 type_0 = TREE_TYPE ((*params)[0]);
10394 if (TREE_CODE (type_0) == ARRAY_TYPE)
10396 /* Force array-to-pointer decay for C++. */
10397 gcc_assert (c_dialect_cxx());
10398 (*params)[0] = default_conversion ((*params)[0]);
10399 type_0 = TREE_TYPE ((*params)[0]);
10401 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10403 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10404 function);
10405 return 0;
10408 /* Types must be compile time constant sizes. */
10409 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10411 error_at (loc,
10412 "argument 1 of %qE must be a pointer to a constant size type",
10413 function);
10414 return 0;
10417 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10419 /* Zero size objects are not allowed. */
10420 if (size_0 == 0)
10422 error_at (loc,
10423 "argument 1 of %qE must be a pointer to a nonzero size object",
10424 function);
10425 return 0;
10428 /* Check each other parameter is a pointer and the same size. */
10429 for (x = 0; x < n_param - n_model; x++)
10431 int size;
10432 tree type = TREE_TYPE ((*params)[x]);
10433 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10434 if (n_param == 6 && x == 3)
10435 continue;
10436 if (!POINTER_TYPE_P (type))
10438 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10439 function);
10440 return 0;
10442 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10443 size = type_size ? tree_to_uhwi (type_size) : 0;
10444 if (size != size_0)
10446 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10447 function);
10448 return 0;
10452 /* Check memory model parameters for validity. */
10453 for (x = n_param - n_model ; x < n_param; x++)
10455 tree p = (*params)[x];
10456 if (TREE_CODE (p) == INTEGER_CST)
10458 int i = tree_to_uhwi (p);
10459 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10461 warning_at (loc, OPT_Winvalid_memory_model,
10462 "invalid memory model argument %d of %qE", x + 1,
10463 function);
10466 else
10467 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10469 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10470 function);
10471 return 0;
10475 return size_0;
10479 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10480 at the beginning of the parameter list PARAMS representing the size of the
10481 objects. This is to match the library ABI requirement. LOC is the location
10482 of the function call.
10483 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10484 returned to allow the external call to be constructed. */
10486 static tree
10487 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10488 vec<tree, va_gc> *params)
10490 tree size_node;
10492 /* Insert a SIZE_T parameter as the first param. If there isn't
10493 enough space, allocate a new vector and recursively re-build with that. */
10494 if (!params->space (1))
10496 unsigned int z, len;
10497 vec<tree, va_gc> *v;
10498 tree f;
10500 len = params->length ();
10501 vec_alloc (v, len + 1);
10502 v->quick_push (build_int_cst (size_type_node, n));
10503 for (z = 0; z < len; z++)
10504 v->quick_push ((*params)[z]);
10505 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10506 vec_free (v);
10507 return f;
10510 /* Add the size parameter and leave as a function call for processing. */
10511 size_node = build_int_cst (size_type_node, n);
10512 params->quick_insert (0, size_node);
10513 return NULL_TREE;
10517 /* Return whether atomic operations for naturally aligned N-byte
10518 arguments are supported, whether inline or through libatomic. */
10519 static bool
10520 atomic_size_supported_p (int n)
10522 switch (n)
10524 case 1:
10525 case 2:
10526 case 4:
10527 case 8:
10528 return true;
10530 case 16:
10531 return targetm.scalar_mode_supported_p (TImode);
10533 default:
10534 return false;
10538 /* This will process an __atomic_exchange function call, determine whether it
10539 needs to be mapped to the _N variation, or turned into a library call.
10540 LOC is the location of the builtin call.
10541 FUNCTION is the DECL that has been invoked;
10542 PARAMS is the argument list for the call. The return value is non-null
10543 TRUE is returned if it is translated into the proper format for a call to the
10544 external library, and NEW_RETURN is set the tree for that function.
10545 FALSE is returned if processing for the _N variation is required, and
10546 NEW_RETURN is set to the the return value the result is copied into. */
10547 static bool
10548 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10549 vec<tree, va_gc> *params, tree *new_return)
10551 tree p0, p1, p2, p3;
10552 tree I_type, I_type_ptr;
10553 int n = get_atomic_generic_size (loc, function, params);
10555 /* Size of 0 is an error condition. */
10556 if (n == 0)
10558 *new_return = error_mark_node;
10559 return true;
10562 /* If not a lock-free size, change to the library generic format. */
10563 if (!atomic_size_supported_p (n))
10565 *new_return = add_atomic_size_parameter (n, loc, function, params);
10566 return true;
10569 /* Otherwise there is a lockfree match, transform the call from:
10570 void fn(T* mem, T* desired, T* return, model)
10571 into
10572 *return = (T) (fn (In* mem, (In) *desired, model)) */
10574 p0 = (*params)[0];
10575 p1 = (*params)[1];
10576 p2 = (*params)[2];
10577 p3 = (*params)[3];
10579 /* Create pointer to appropriate size. */
10580 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10581 I_type_ptr = build_pointer_type (I_type);
10583 /* Convert object pointer to required type. */
10584 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10585 (*params)[0] = p0;
10586 /* Convert new value to required type, and dereference it. */
10587 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10588 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10589 (*params)[1] = p1;
10591 /* Move memory model to the 3rd position, and end param list. */
10592 (*params)[2] = p3;
10593 params->truncate (3);
10595 /* Convert return pointer and dereference it for later assignment. */
10596 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10598 return false;
10602 /* This will process an __atomic_compare_exchange function call, determine
10603 whether it needs to be mapped to the _N variation, or turned into a lib call.
10604 LOC is the location of the builtin call.
10605 FUNCTION is the DECL that has been invoked;
10606 PARAMS is the argument list for the call. The return value is non-null
10607 TRUE is returned if it is translated into the proper format for a call to the
10608 external library, and NEW_RETURN is set the tree for that function.
10609 FALSE is returned if processing for the _N variation is required. */
10611 static bool
10612 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10613 vec<tree, va_gc> *params,
10614 tree *new_return)
10616 tree p0, p1, p2;
10617 tree I_type, I_type_ptr;
10618 int n = get_atomic_generic_size (loc, function, params);
10620 /* Size of 0 is an error condition. */
10621 if (n == 0)
10623 *new_return = error_mark_node;
10624 return true;
10627 /* If not a lock-free size, change to the library generic format. */
10628 if (!atomic_size_supported_p (n))
10630 /* The library generic format does not have the weak parameter, so
10631 remove it from the param list. Since a parameter has been removed,
10632 we can be sure that there is room for the SIZE_T parameter, meaning
10633 there will not be a recursive rebuilding of the parameter list, so
10634 there is no danger this will be done twice. */
10635 if (n > 0)
10637 (*params)[3] = (*params)[4];
10638 (*params)[4] = (*params)[5];
10639 params->truncate (5);
10641 *new_return = add_atomic_size_parameter (n, loc, function, params);
10642 return true;
10645 /* Otherwise, there is a match, so the call needs to be transformed from:
10646 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10647 into
10648 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10650 p0 = (*params)[0];
10651 p1 = (*params)[1];
10652 p2 = (*params)[2];
10654 /* Create pointer to appropriate size. */
10655 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10656 I_type_ptr = build_pointer_type (I_type);
10658 /* Convert object pointer to required type. */
10659 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10660 (*params)[0] = p0;
10662 /* Convert expected pointer to required type. */
10663 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10664 (*params)[1] = p1;
10666 /* Convert desired value to required type, and dereference it. */
10667 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10668 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10669 (*params)[2] = p2;
10671 /* The rest of the parameters are fine. NULL means no special return value
10672 processing.*/
10673 *new_return = NULL;
10674 return false;
10678 /* This will process an __atomic_load function call, determine whether it
10679 needs to be mapped to the _N variation, or turned into a library call.
10680 LOC is the location of the builtin call.
10681 FUNCTION is the DECL that has been invoked;
10682 PARAMS is the argument list for the call. The return value is non-null
10683 TRUE is returned if it is translated into the proper format for a call to the
10684 external library, and NEW_RETURN is set the tree for that function.
10685 FALSE is returned if processing for the _N variation is required, and
10686 NEW_RETURN is set to the the return value the result is copied into. */
10688 static bool
10689 resolve_overloaded_atomic_load (location_t loc, tree function,
10690 vec<tree, va_gc> *params, tree *new_return)
10692 tree p0, p1, p2;
10693 tree I_type, I_type_ptr;
10694 int n = get_atomic_generic_size (loc, function, params);
10696 /* Size of 0 is an error condition. */
10697 if (n == 0)
10699 *new_return = error_mark_node;
10700 return true;
10703 /* If not a lock-free size, change to the library generic format. */
10704 if (!atomic_size_supported_p (n))
10706 *new_return = add_atomic_size_parameter (n, loc, function, params);
10707 return true;
10710 /* Otherwise, there is a match, so the call needs to be transformed from:
10711 void fn(T* mem, T* return, model)
10712 into
10713 *return = (T) (fn ((In *) mem, model)) */
10715 p0 = (*params)[0];
10716 p1 = (*params)[1];
10717 p2 = (*params)[2];
10719 /* Create pointer to appropriate size. */
10720 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10721 I_type_ptr = build_pointer_type (I_type);
10723 /* Convert object pointer to required type. */
10724 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10725 (*params)[0] = p0;
10727 /* Move memory model to the 2nd position, and end param list. */
10728 (*params)[1] = p2;
10729 params->truncate (2);
10731 /* Convert return pointer and dereference it for later assignment. */
10732 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10734 return false;
10738 /* This will process an __atomic_store function call, determine whether it
10739 needs to be mapped to the _N variation, or turned into a library call.
10740 LOC is the location of the builtin call.
10741 FUNCTION is the DECL that has been invoked;
10742 PARAMS is the argument list for the call. The return value is non-null
10743 TRUE is returned if it is translated into the proper format for a call to the
10744 external library, and NEW_RETURN is set the tree for that function.
10745 FALSE is returned if processing for the _N variation is required, and
10746 NEW_RETURN is set to the the return value the result is copied into. */
10748 static bool
10749 resolve_overloaded_atomic_store (location_t loc, tree function,
10750 vec<tree, va_gc> *params, tree *new_return)
10752 tree p0, p1;
10753 tree I_type, I_type_ptr;
10754 int n = get_atomic_generic_size (loc, function, params);
10756 /* Size of 0 is an error condition. */
10757 if (n == 0)
10759 *new_return = error_mark_node;
10760 return true;
10763 /* If not a lock-free size, change to the library generic format. */
10764 if (!atomic_size_supported_p (n))
10766 *new_return = add_atomic_size_parameter (n, loc, function, params);
10767 return true;
10770 /* Otherwise, there is a match, so the call needs to be transformed from:
10771 void fn(T* mem, T* value, model)
10772 into
10773 fn ((In *) mem, (In) *value, model) */
10775 p0 = (*params)[0];
10776 p1 = (*params)[1];
10778 /* Create pointer to appropriate size. */
10779 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10780 I_type_ptr = build_pointer_type (I_type);
10782 /* Convert object pointer to required type. */
10783 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10784 (*params)[0] = p0;
10786 /* Convert new value to required type, and dereference it. */
10787 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10788 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10789 (*params)[1] = p1;
10791 /* The memory model is in the right spot already. Return is void. */
10792 *new_return = NULL_TREE;
10794 return false;
10798 /* Some builtin functions are placeholders for other expressions. This
10799 function should be called immediately after parsing the call expression
10800 before surrounding code has committed to the type of the expression.
10802 LOC is the location of the builtin call.
10804 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10805 PARAMS is the argument list for the call. The return value is non-null
10806 when expansion is complete, and null if normal processing should
10807 continue. */
10809 tree
10810 resolve_overloaded_builtin (location_t loc, tree function,
10811 vec<tree, va_gc> *params)
10813 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10814 bool orig_format = true;
10815 tree new_return = NULL_TREE;
10817 switch (DECL_BUILT_IN_CLASS (function))
10819 case BUILT_IN_NORMAL:
10820 break;
10821 case BUILT_IN_MD:
10822 if (targetm.resolve_overloaded_builtin)
10823 return targetm.resolve_overloaded_builtin (loc, function, params);
10824 else
10825 return NULL_TREE;
10826 default:
10827 return NULL_TREE;
10830 /* Handle BUILT_IN_NORMAL here. */
10831 switch (orig_code)
10833 case BUILT_IN_ATOMIC_EXCHANGE:
10834 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10835 case BUILT_IN_ATOMIC_LOAD:
10836 case BUILT_IN_ATOMIC_STORE:
10838 /* Handle these 4 together so that they can fall through to the next
10839 case if the call is transformed to an _N variant. */
10840 switch (orig_code)
10842 case BUILT_IN_ATOMIC_EXCHANGE:
10844 if (resolve_overloaded_atomic_exchange (loc, function, params,
10845 &new_return))
10846 return new_return;
10847 /* Change to the _N variant. */
10848 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10849 break;
10852 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10854 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10855 params,
10856 &new_return))
10857 return new_return;
10858 /* Change to the _N variant. */
10859 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10860 break;
10862 case BUILT_IN_ATOMIC_LOAD:
10864 if (resolve_overloaded_atomic_load (loc, function, params,
10865 &new_return))
10866 return new_return;
10867 /* Change to the _N variant. */
10868 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10869 break;
10871 case BUILT_IN_ATOMIC_STORE:
10873 if (resolve_overloaded_atomic_store (loc, function, params,
10874 &new_return))
10875 return new_return;
10876 /* Change to the _N variant. */
10877 orig_code = BUILT_IN_ATOMIC_STORE_N;
10878 break;
10880 default:
10881 gcc_unreachable ();
10883 /* Fallthrough to the normal processing. */
10885 case BUILT_IN_ATOMIC_EXCHANGE_N:
10886 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10887 case BUILT_IN_ATOMIC_LOAD_N:
10888 case BUILT_IN_ATOMIC_STORE_N:
10889 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10890 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10891 case BUILT_IN_ATOMIC_AND_FETCH_N:
10892 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10893 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10894 case BUILT_IN_ATOMIC_OR_FETCH_N:
10895 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10896 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10897 case BUILT_IN_ATOMIC_FETCH_AND_N:
10898 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10899 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10900 case BUILT_IN_ATOMIC_FETCH_OR_N:
10902 orig_format = false;
10903 /* Fallthru for parameter processing. */
10905 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10906 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10907 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10908 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10909 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10910 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10911 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10912 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10913 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10914 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10915 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10916 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10917 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10918 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10919 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10920 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10922 int n = sync_resolve_size (function, params);
10923 tree new_function, first_param, result;
10924 enum built_in_function fncode;
10926 if (n == 0)
10927 return error_mark_node;
10929 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10930 new_function = builtin_decl_explicit (fncode);
10931 if (!sync_resolve_params (loc, function, new_function, params,
10932 orig_format))
10933 return error_mark_node;
10935 first_param = (*params)[0];
10936 result = build_function_call_vec (loc, vNULL, new_function, params,
10937 NULL);
10938 if (result == error_mark_node)
10939 return result;
10940 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10941 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10942 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10943 result = sync_resolve_return (first_param, result, orig_format);
10945 /* If new_return is set, assign function to that expr and cast the
10946 result to void since the generic interface returned void. */
10947 if (new_return)
10949 /* Cast function result from I{1,2,4,8,16} to the required type. */
10950 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10951 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10952 result);
10953 TREE_SIDE_EFFECTS (result) = 1;
10954 protected_set_expr_location (result, loc);
10955 result = convert (void_type_node, result);
10957 return result;
10960 default:
10961 return NULL_TREE;
10965 /* vector_types_compatible_elements_p is used in type checks of vectors
10966 values used as operands of binary operators. Where it returns true, and
10967 the other checks of the caller succeed (being vector types in he first
10968 place, and matching number of elements), we can just treat the types
10969 as essentially the same.
10970 Contrast with vector_targets_convertible_p, which is used for vector
10971 pointer types, and vector_types_convertible_p, which will allow
10972 language-specific matches under the control of flag_lax_vector_conversions,
10973 and might still require a conversion. */
10974 /* True if vector types T1 and T2 can be inputs to the same binary
10975 operator without conversion.
10976 We don't check the overall vector size here because some of our callers
10977 want to give different error messages when the vectors are compatible
10978 except for the element count. */
10980 bool
10981 vector_types_compatible_elements_p (tree t1, tree t2)
10983 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10984 t1 = TREE_TYPE (t1);
10985 t2 = TREE_TYPE (t2);
10987 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10989 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10990 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10991 || c2 == FIXED_POINT_TYPE));
10993 t1 = c_common_signed_type (t1);
10994 t2 = c_common_signed_type (t2);
10995 /* Equality works here because c_common_signed_type uses
10996 TYPE_MAIN_VARIANT. */
10997 if (t1 == t2)
10998 return true;
10999 if (opaque && c1 == c2
11000 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11001 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11002 return true;
11003 return false;
11006 /* Check for missing format attributes on function pointers. LTYPE is
11007 the new type or left-hand side type. RTYPE is the old type or
11008 right-hand side type. Returns TRUE if LTYPE is missing the desired
11009 attribute. */
11011 bool
11012 check_missing_format_attribute (tree ltype, tree rtype)
11014 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11015 tree ra;
11017 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11018 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11019 break;
11020 if (ra)
11022 tree la;
11023 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11024 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11025 break;
11026 return !la;
11028 else
11029 return false;
11032 /* Subscripting with type char is likely to lose on a machine where
11033 chars are signed. So warn on any machine, but optionally. Don't
11034 warn for unsigned char since that type is safe. Don't warn for
11035 signed char because anyone who uses that must have done so
11036 deliberately. Furthermore, we reduce the false positive load by
11037 warning only for non-constant value of type char. */
11039 void
11040 warn_array_subscript_with_type_char (tree index)
11042 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11043 && TREE_CODE (index) != INTEGER_CST)
11044 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11047 /* Implement -Wparentheses for the unexpected C precedence rules, to
11048 cover cases like x + y << z which readers are likely to
11049 misinterpret. We have seen an expression in which CODE is a binary
11050 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11051 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11052 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11053 expression was not formed using a binary or unary operator, or it
11054 was enclosed in parentheses. */
11056 void
11057 warn_about_parentheses (location_t loc, enum tree_code code,
11058 enum tree_code code_left, tree arg_left,
11059 enum tree_code code_right, tree arg_right)
11061 if (!warn_parentheses)
11062 return;
11064 /* This macro tests that the expression ARG with original tree code
11065 CODE appears to be a boolean expression. or the result of folding a
11066 boolean expression. */
11067 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11068 (truth_value_p (TREE_CODE (ARG)) \
11069 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11070 /* Folding may create 0 or 1 integers from other expressions. */ \
11071 || ((CODE) != INTEGER_CST \
11072 && (integer_onep (ARG) || integer_zerop (ARG))))
11074 switch (code)
11076 case LSHIFT_EXPR:
11077 if (code_left == PLUS_EXPR)
11078 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11079 "suggest parentheses around %<+%> inside %<<<%>");
11080 else if (code_right == PLUS_EXPR)
11081 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11082 "suggest parentheses around %<+%> inside %<<<%>");
11083 else if (code_left == MINUS_EXPR)
11084 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11085 "suggest parentheses around %<-%> inside %<<<%>");
11086 else if (code_right == MINUS_EXPR)
11087 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11088 "suggest parentheses around %<-%> inside %<<<%>");
11089 return;
11091 case RSHIFT_EXPR:
11092 if (code_left == PLUS_EXPR)
11093 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11094 "suggest parentheses around %<+%> inside %<>>%>");
11095 else if (code_right == PLUS_EXPR)
11096 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11097 "suggest parentheses around %<+%> inside %<>>%>");
11098 else if (code_left == MINUS_EXPR)
11099 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11100 "suggest parentheses around %<-%> inside %<>>%>");
11101 else if (code_right == MINUS_EXPR)
11102 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11103 "suggest parentheses around %<-%> inside %<>>%>");
11104 return;
11106 case TRUTH_ORIF_EXPR:
11107 if (code_left == TRUTH_ANDIF_EXPR)
11108 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11109 "suggest parentheses around %<&&%> within %<||%>");
11110 else if (code_right == TRUTH_ANDIF_EXPR)
11111 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11112 "suggest parentheses around %<&&%> within %<||%>");
11113 return;
11115 case BIT_IOR_EXPR:
11116 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11117 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11118 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11119 "suggest parentheses around arithmetic in operand of %<|%>");
11120 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11121 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11122 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11123 "suggest parentheses around arithmetic in operand of %<|%>");
11124 /* Check cases like x|y==z */
11125 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11126 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11127 "suggest parentheses around comparison in operand of %<|%>");
11128 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11129 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11130 "suggest parentheses around comparison in operand of %<|%>");
11131 /* Check cases like !x | y */
11132 else if (code_left == TRUTH_NOT_EXPR
11133 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11134 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11135 "suggest parentheses around operand of "
11136 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11137 return;
11139 case BIT_XOR_EXPR:
11140 if (code_left == BIT_AND_EXPR
11141 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11142 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11143 "suggest parentheses around arithmetic in operand of %<^%>");
11144 else if (code_right == BIT_AND_EXPR
11145 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11146 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11147 "suggest parentheses around arithmetic in operand of %<^%>");
11148 /* Check cases like x^y==z */
11149 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11150 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11151 "suggest parentheses around comparison in operand of %<^%>");
11152 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11153 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11154 "suggest parentheses around comparison in operand of %<^%>");
11155 return;
11157 case BIT_AND_EXPR:
11158 if (code_left == PLUS_EXPR)
11159 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11160 "suggest parentheses around %<+%> in operand of %<&%>");
11161 else if (code_right == PLUS_EXPR)
11162 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11163 "suggest parentheses around %<+%> in operand of %<&%>");
11164 else if (code_left == MINUS_EXPR)
11165 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11166 "suggest parentheses around %<-%> in operand of %<&%>");
11167 else if (code_right == MINUS_EXPR)
11168 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11169 "suggest parentheses around %<-%> in operand of %<&%>");
11170 /* Check cases like x&y==z */
11171 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11172 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11173 "suggest parentheses around comparison in operand of %<&%>");
11174 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11175 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11176 "suggest parentheses around comparison in operand of %<&%>");
11177 /* Check cases like !x & y */
11178 else if (code_left == TRUTH_NOT_EXPR
11179 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11180 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11181 "suggest parentheses around operand of "
11182 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11183 return;
11185 case EQ_EXPR:
11186 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11187 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11188 "suggest parentheses around comparison in operand of %<==%>");
11189 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11190 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11191 "suggest parentheses around comparison in operand of %<==%>");
11192 return;
11193 case NE_EXPR:
11194 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11195 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11196 "suggest parentheses around comparison in operand of %<!=%>");
11197 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11198 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11199 "suggest parentheses around comparison in operand of %<!=%>");
11200 return;
11202 default:
11203 if (TREE_CODE_CLASS (code) == tcc_comparison)
11205 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11206 && code_left != NE_EXPR && code_left != EQ_EXPR
11207 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11208 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11209 "comparisons like %<X<=Y<=Z%> do not "
11210 "have their mathematical meaning");
11211 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11212 && code_right != NE_EXPR && code_right != EQ_EXPR
11213 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11214 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11215 "comparisons like %<X<=Y<=Z%> do not "
11216 "have their mathematical meaning");
11218 return;
11220 #undef NOT_A_BOOLEAN_EXPR_P
11223 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11225 void
11226 warn_for_unused_label (tree label)
11228 if (!TREE_USED (label))
11230 if (DECL_INITIAL (label))
11231 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11232 else
11233 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11237 /* Warn for division by zero according to the value of DIVISOR. LOC
11238 is the location of the division operator. */
11240 void
11241 warn_for_div_by_zero (location_t loc, tree divisor)
11243 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11244 about division by zero. Do not issue a warning if DIVISOR has a
11245 floating-point type, since we consider 0.0/0.0 a valid way of
11246 generating a NaN. */
11247 if (c_inhibit_evaluation_warnings == 0
11248 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11249 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11252 /* Subroutine of build_binary_op. Give warnings for comparisons
11253 between signed and unsigned quantities that may fail. Do the
11254 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11255 so that casts will be considered, but default promotions won't
11258 LOCATION is the location of the comparison operator.
11260 The arguments of this function map directly to local variables
11261 of build_binary_op. */
11263 void
11264 warn_for_sign_compare (location_t location,
11265 tree orig_op0, tree orig_op1,
11266 tree op0, tree op1,
11267 tree result_type, enum tree_code resultcode)
11269 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11270 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11271 int unsignedp0, unsignedp1;
11273 /* In C++, check for comparison of different enum types. */
11274 if (c_dialect_cxx()
11275 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11276 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11277 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11278 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11280 warning_at (location,
11281 OPT_Wsign_compare, "comparison between types %qT and %qT",
11282 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11285 /* Do not warn if the comparison is being done in a signed type,
11286 since the signed type will only be chosen if it can represent
11287 all the values of the unsigned type. */
11288 if (!TYPE_UNSIGNED (result_type))
11289 /* OK */;
11290 /* Do not warn if both operands are unsigned. */
11291 else if (op0_signed == op1_signed)
11292 /* OK */;
11293 else
11295 tree sop, uop, base_type;
11296 bool ovf;
11298 if (op0_signed)
11299 sop = orig_op0, uop = orig_op1;
11300 else
11301 sop = orig_op1, uop = orig_op0;
11303 STRIP_TYPE_NOPS (sop);
11304 STRIP_TYPE_NOPS (uop);
11305 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11306 ? TREE_TYPE (result_type) : result_type);
11308 /* Do not warn if the signed quantity is an unsuffixed integer
11309 literal (or some static constant expression involving such
11310 literals or a conditional expression involving such literals)
11311 and it is non-negative. */
11312 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11313 /* OK */;
11314 /* Do not warn if the comparison is an equality operation, the
11315 unsigned quantity is an integral constant, and it would fit
11316 in the result if the result were signed. */
11317 else if (TREE_CODE (uop) == INTEGER_CST
11318 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11319 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11320 /* OK */;
11321 /* In C, do not warn if the unsigned quantity is an enumeration
11322 constant and its maximum value would fit in the result if the
11323 result were signed. */
11324 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11325 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11326 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11327 c_common_signed_type (base_type)))
11328 /* OK */;
11329 else
11330 warning_at (location,
11331 OPT_Wsign_compare,
11332 "comparison between signed and unsigned integer expressions");
11335 /* Warn if two unsigned values are being compared in a size larger
11336 than their original size, and one (and only one) is the result of
11337 a `~' operator. This comparison will always fail.
11339 Also warn if one operand is a constant, and the constant does not
11340 have all bits set that are set in the ~ operand when it is
11341 extended. */
11343 op0 = c_common_get_narrower (op0, &unsignedp0);
11344 op1 = c_common_get_narrower (op1, &unsignedp1);
11346 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11347 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11349 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11350 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11351 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11352 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11354 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11356 tree primop;
11357 HOST_WIDE_INT constant, mask;
11358 int unsignedp;
11359 unsigned int bits;
11361 if (tree_fits_shwi_p (op0))
11363 primop = op1;
11364 unsignedp = unsignedp1;
11365 constant = tree_to_shwi (op0);
11367 else
11369 primop = op0;
11370 unsignedp = unsignedp0;
11371 constant = tree_to_shwi (op1);
11374 bits = TYPE_PRECISION (TREE_TYPE (primop));
11375 if (bits < TYPE_PRECISION (result_type)
11376 && bits < HOST_BITS_PER_LONG && unsignedp)
11378 mask = (~ (HOST_WIDE_INT) 0) << bits;
11379 if ((mask & constant) != mask)
11381 if (constant == 0)
11382 warning_at (location, OPT_Wsign_compare,
11383 "promoted ~unsigned is always non-zero");
11384 else
11385 warning_at (location, OPT_Wsign_compare,
11386 "comparison of promoted ~unsigned with constant");
11390 else if (unsignedp0 && unsignedp1
11391 && (TYPE_PRECISION (TREE_TYPE (op0))
11392 < TYPE_PRECISION (result_type))
11393 && (TYPE_PRECISION (TREE_TYPE (op1))
11394 < TYPE_PRECISION (result_type)))
11395 warning_at (location, OPT_Wsign_compare,
11396 "comparison of promoted ~unsigned with unsigned");
11400 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11401 type via c_common_type. If -Wdouble-promotion is in use, and the
11402 conditions for warning have been met, issue a warning. GMSGID is
11403 the warning message. It must have two %T specifiers for the type
11404 that was converted (generally "float") and the type to which it was
11405 converted (generally "double), respectively. LOC is the location
11406 to which the awrning should refer. */
11408 void
11409 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11410 const char *gmsgid, location_t loc)
11412 tree source_type;
11414 if (!warn_double_promotion)
11415 return;
11416 /* If the conversion will not occur at run-time, there is no need to
11417 warn about it. */
11418 if (c_inhibit_evaluation_warnings)
11419 return;
11420 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11421 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11422 return;
11423 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11424 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11425 source_type = type1;
11426 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11427 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11428 source_type = type2;
11429 else
11430 return;
11431 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11434 /* Setup a TYPE_DECL node as a typedef representation.
11436 X is a TYPE_DECL for a typedef statement. Create a brand new
11437 ..._TYPE node (which will be just a variant of the existing
11438 ..._TYPE node with identical properties) and then install X
11439 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11441 The whole point here is to end up with a situation where each
11442 and every ..._TYPE node the compiler creates will be uniquely
11443 associated with AT MOST one node representing a typedef name.
11444 This way, even though the compiler substitutes corresponding
11445 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11446 early on, later parts of the compiler can always do the reverse
11447 translation and get back the corresponding typedef name. For
11448 example, given:
11450 typedef struct S MY_TYPE;
11451 MY_TYPE object;
11453 Later parts of the compiler might only know that `object' was of
11454 type `struct S' if it were not for code just below. With this
11455 code however, later parts of the compiler see something like:
11457 struct S' == struct S
11458 typedef struct S' MY_TYPE;
11459 struct S' object;
11461 And they can then deduce (from the node for type struct S') that
11462 the original object declaration was:
11464 MY_TYPE object;
11466 Being able to do this is important for proper support of protoize,
11467 and also for generating precise symbolic debugging information
11468 which takes full account of the programmer's (typedef) vocabulary.
11470 Obviously, we don't want to generate a duplicate ..._TYPE node if
11471 the TYPE_DECL node that we are now processing really represents a
11472 standard built-in type. */
11474 void
11475 set_underlying_type (tree x)
11477 if (x == error_mark_node)
11478 return;
11479 if (DECL_IS_BUILTIN (x))
11481 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11482 TYPE_NAME (TREE_TYPE (x)) = x;
11484 else if (TREE_TYPE (x) != error_mark_node
11485 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11487 tree tt = TREE_TYPE (x);
11488 DECL_ORIGINAL_TYPE (x) = tt;
11489 tt = build_variant_type_copy (tt);
11490 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11491 TYPE_NAME (tt) = x;
11492 TREE_USED (tt) = TREE_USED (x);
11493 TREE_TYPE (x) = tt;
11497 /* Record the types used by the current global variable declaration
11498 being parsed, so that we can decide later to emit their debug info.
11499 Those types are in types_used_by_cur_var_decl, and we are going to
11500 store them in the types_used_by_vars_hash hash table.
11501 DECL is the declaration of the global variable that has been parsed. */
11503 void
11504 record_types_used_by_current_var_decl (tree decl)
11506 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11508 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11510 tree type = types_used_by_cur_var_decl->pop ();
11511 types_used_by_var_decl_insert (type, decl);
11515 /* If DECL is a typedef that is declared in the current function,
11516 record it for the purpose of -Wunused-local-typedefs. */
11518 void
11519 record_locally_defined_typedef (tree decl)
11521 struct c_language_function *l;
11523 if (!warn_unused_local_typedefs
11524 || cfun == NULL
11525 /* if this is not a locally defined typedef then we are not
11526 interested. */
11527 || !is_typedef_decl (decl)
11528 || !decl_function_context (decl))
11529 return;
11531 l = (struct c_language_function *) cfun->language;
11532 vec_safe_push (l->local_typedefs, decl);
11535 /* If T is a TYPE_DECL declared locally, mark it as used. */
11537 void
11538 maybe_record_typedef_use (tree t)
11540 if (!is_typedef_decl (t))
11541 return;
11543 TREE_USED (t) = true;
11546 /* Warn if there are some unused locally defined typedefs in the
11547 current function. */
11549 void
11550 maybe_warn_unused_local_typedefs (void)
11552 int i;
11553 tree decl;
11554 /* The number of times we have emitted -Wunused-local-typedefs
11555 warnings. If this is different from errorcount, that means some
11556 unrelated errors have been issued. In which case, we'll avoid
11557 emitting "unused-local-typedefs" warnings. */
11558 static int unused_local_typedefs_warn_count;
11559 struct c_language_function *l;
11561 if (cfun == NULL)
11562 return;
11564 if ((l = (struct c_language_function *) cfun->language) == NULL)
11565 return;
11567 if (warn_unused_local_typedefs
11568 && errorcount == unused_local_typedefs_warn_count)
11570 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11571 if (!TREE_USED (decl))
11572 warning_at (DECL_SOURCE_LOCATION (decl),
11573 OPT_Wunused_local_typedefs,
11574 "typedef %qD locally defined but not used", decl);
11575 unused_local_typedefs_warn_count = errorcount;
11578 vec_free (l->local_typedefs);
11581 /* Warn about boolean expression compared with an integer value different
11582 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11583 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11584 are the operands of the comparison. The caller must ensure that
11585 either operand is a boolean expression. */
11587 void
11588 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11589 tree op1)
11591 if (TREE_CODE_CLASS (code) != tcc_comparison)
11592 return;
11594 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11595 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11596 if (!cst)
11597 return;
11599 if (!integer_zerop (cst) && !integer_onep (cst))
11601 int sign = (TREE_CODE (op0) == INTEGER_CST)
11602 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11603 if (code == EQ_EXPR
11604 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11605 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11606 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11607 "with boolean expression is always false", cst);
11608 else
11609 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11610 "with boolean expression is always true", cst);
11614 /* The C and C++ parsers both use vectors to hold function arguments.
11615 For efficiency, we keep a cache of unused vectors. This is the
11616 cache. */
11618 typedef vec<tree, va_gc> *tree_gc_vec;
11619 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11621 /* Return a new vector from the cache. If the cache is empty,
11622 allocate a new vector. These vectors are GC'ed, so it is OK if the
11623 pointer is not released.. */
11625 vec<tree, va_gc> *
11626 make_tree_vector (void)
11628 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11629 return tree_vector_cache->pop ();
11630 else
11632 /* Passing 0 to vec::alloc returns NULL, and our callers require
11633 that we always return a non-NULL value. The vector code uses
11634 4 when growing a NULL vector, so we do too. */
11635 vec<tree, va_gc> *v;
11636 vec_alloc (v, 4);
11637 return v;
11641 /* Release a vector of trees back to the cache. */
11643 void
11644 release_tree_vector (vec<tree, va_gc> *vec)
11646 if (vec != NULL)
11648 vec->truncate (0);
11649 vec_safe_push (tree_vector_cache, vec);
11653 /* Get a new tree vector holding a single tree. */
11655 vec<tree, va_gc> *
11656 make_tree_vector_single (tree t)
11658 vec<tree, va_gc> *ret = make_tree_vector ();
11659 ret->quick_push (t);
11660 return ret;
11663 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11665 vec<tree, va_gc> *
11666 make_tree_vector_from_list (tree list)
11668 vec<tree, va_gc> *ret = make_tree_vector ();
11669 for (; list; list = TREE_CHAIN (list))
11670 vec_safe_push (ret, TREE_VALUE (list));
11671 return ret;
11674 /* Get a new tree vector which is a copy of an existing one. */
11676 vec<tree, va_gc> *
11677 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11679 vec<tree, va_gc> *ret;
11680 unsigned int ix;
11681 tree t;
11683 ret = make_tree_vector ();
11684 vec_safe_reserve (ret, vec_safe_length (orig));
11685 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11686 ret->quick_push (t);
11687 return ret;
11690 /* Return true if KEYWORD starts a type specifier. */
11692 bool
11693 keyword_begins_type_specifier (enum rid keyword)
11695 switch (keyword)
11697 case RID_AUTO_TYPE:
11698 case RID_INT:
11699 case RID_CHAR:
11700 case RID_FLOAT:
11701 case RID_DOUBLE:
11702 case RID_VOID:
11703 case RID_INT128:
11704 case RID_UNSIGNED:
11705 case RID_LONG:
11706 case RID_SHORT:
11707 case RID_SIGNED:
11708 case RID_DFLOAT32:
11709 case RID_DFLOAT64:
11710 case RID_DFLOAT128:
11711 case RID_FRACT:
11712 case RID_ACCUM:
11713 case RID_BOOL:
11714 case RID_WCHAR:
11715 case RID_CHAR16:
11716 case RID_CHAR32:
11717 case RID_SAT:
11718 case RID_COMPLEX:
11719 case RID_TYPEOF:
11720 case RID_STRUCT:
11721 case RID_CLASS:
11722 case RID_UNION:
11723 case RID_ENUM:
11724 return true;
11725 default:
11726 return false;
11730 /* Return true if KEYWORD names a type qualifier. */
11732 bool
11733 keyword_is_type_qualifier (enum rid keyword)
11735 switch (keyword)
11737 case RID_CONST:
11738 case RID_VOLATILE:
11739 case RID_RESTRICT:
11740 case RID_ATOMIC:
11741 return true;
11742 default:
11743 return false;
11747 /* Return true if KEYWORD names a storage class specifier.
11749 RID_TYPEDEF is not included in this list despite `typedef' being
11750 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11751 such for syntactic convenience only. */
11753 bool
11754 keyword_is_storage_class_specifier (enum rid keyword)
11756 switch (keyword)
11758 case RID_STATIC:
11759 case RID_EXTERN:
11760 case RID_REGISTER:
11761 case RID_AUTO:
11762 case RID_MUTABLE:
11763 case RID_THREAD:
11764 return true;
11765 default:
11766 return false;
11770 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11772 static bool
11773 keyword_is_function_specifier (enum rid keyword)
11775 switch (keyword)
11777 case RID_INLINE:
11778 case RID_NORETURN:
11779 case RID_VIRTUAL:
11780 case RID_EXPLICIT:
11781 return true;
11782 default:
11783 return false;
11787 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11788 declaration-specifier (C99 6.7). */
11790 bool
11791 keyword_is_decl_specifier (enum rid keyword)
11793 if (keyword_is_storage_class_specifier (keyword)
11794 || keyword_is_type_qualifier (keyword)
11795 || keyword_is_function_specifier (keyword))
11796 return true;
11798 switch (keyword)
11800 case RID_TYPEDEF:
11801 case RID_FRIEND:
11802 case RID_CONSTEXPR:
11803 return true;
11804 default:
11805 return false;
11809 /* Initialize language-specific-bits of tree_contains_struct. */
11811 void
11812 c_common_init_ts (void)
11814 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11815 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11816 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11819 /* Build a user-defined numeric literal out of an integer constant type VALUE
11820 with identifier SUFFIX. */
11822 tree
11823 build_userdef_literal (tree suffix_id, tree value,
11824 enum overflow_type overflow, tree num_string)
11826 tree literal = make_node (USERDEF_LITERAL);
11827 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11828 USERDEF_LITERAL_VALUE (literal) = value;
11829 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11830 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11831 return literal;
11834 /* For vector[index], convert the vector to a
11835 pointer of the underlying type. */
11836 void
11837 convert_vector_to_pointer_for_subscript (location_t loc,
11838 tree* vecp, tree index)
11840 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11842 tree type = TREE_TYPE (*vecp);
11843 tree type1;
11845 if (TREE_CODE (index) == INTEGER_CST)
11846 if (!tree_fits_uhwi_p (index)
11847 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11848 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11850 c_common_mark_addressable_vec (*vecp);
11851 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11852 type1 = build_pointer_type (TREE_TYPE (*vecp));
11853 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
11854 if (!ref_all
11855 && !DECL_P (*vecp))
11857 /* If the original vector isn't declared may_alias and it
11858 isn't a bare vector look if the subscripting would
11859 alias the vector we subscript, and if not, force ref-all. */
11860 alias_set_type vecset = get_alias_set (*vecp);
11861 alias_set_type sset = get_alias_set (type);
11862 if (!alias_sets_must_conflict_p (sset, vecset)
11863 && !alias_set_subset_of (sset, vecset))
11864 ref_all = true;
11866 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
11867 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11868 *vecp = convert (type, *vecp);
11872 /* Determine which of the operands, if any, is a scalar that needs to be
11873 converted to a vector, for the range of operations. */
11874 enum stv_conv
11875 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11876 bool complain)
11878 tree type0 = TREE_TYPE (op0);
11879 tree type1 = TREE_TYPE (op1);
11880 bool integer_only_op = false;
11881 enum stv_conv ret = stv_firstarg;
11883 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11884 || TREE_CODE (type1) == VECTOR_TYPE);
11885 switch (code)
11887 /* Most GENERIC binary expressions require homogeneous arguments.
11888 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11889 argument that is a vector and a second one that is a scalar, so
11890 we never return stv_secondarg for them. */
11891 case RSHIFT_EXPR:
11892 case LSHIFT_EXPR:
11893 if (TREE_CODE (type0) == INTEGER_TYPE
11894 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11896 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11898 if (complain)
11899 error_at (loc, "conversion of scalar %qT to vector %qT "
11900 "involves truncation", type0, type1);
11901 return stv_error;
11903 else
11904 return stv_firstarg;
11906 break;
11908 case BIT_IOR_EXPR:
11909 case BIT_XOR_EXPR:
11910 case BIT_AND_EXPR:
11911 integer_only_op = true;
11912 /* ... fall through ... */
11914 case VEC_COND_EXPR:
11916 case PLUS_EXPR:
11917 case MINUS_EXPR:
11918 case MULT_EXPR:
11919 case TRUNC_DIV_EXPR:
11920 case CEIL_DIV_EXPR:
11921 case FLOOR_DIV_EXPR:
11922 case ROUND_DIV_EXPR:
11923 case EXACT_DIV_EXPR:
11924 case TRUNC_MOD_EXPR:
11925 case FLOOR_MOD_EXPR:
11926 case RDIV_EXPR:
11927 case EQ_EXPR:
11928 case NE_EXPR:
11929 case LE_EXPR:
11930 case GE_EXPR:
11931 case LT_EXPR:
11932 case GT_EXPR:
11933 /* What about UNLT_EXPR? */
11934 if (TREE_CODE (type0) == VECTOR_TYPE)
11936 tree tmp;
11937 ret = stv_secondarg;
11938 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11939 tmp = type0; type0 = type1; type1 = tmp;
11940 tmp = op0; op0 = op1; op1 = tmp;
11943 if (TREE_CODE (type0) == INTEGER_TYPE
11944 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11946 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11948 if (complain)
11949 error_at (loc, "conversion of scalar %qT to vector %qT "
11950 "involves truncation", type0, type1);
11951 return stv_error;
11953 return ret;
11955 else if (!integer_only_op
11956 /* Allow integer --> real conversion if safe. */
11957 && (TREE_CODE (type0) == REAL_TYPE
11958 || TREE_CODE (type0) == INTEGER_TYPE)
11959 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11961 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11963 if (complain)
11964 error_at (loc, "conversion of scalar %qT to vector %qT "
11965 "involves truncation", type0, type1);
11966 return stv_error;
11968 return ret;
11970 default:
11971 break;
11974 return stv_nothing;
11977 /* Return true iff ALIGN is an integral constant that is a fundamental
11978 alignment, as defined by [basic.align] in the c++-11
11979 specifications.
11981 That is:
11983 [A fundamental alignment is represented by an alignment less than or
11984 equal to the greatest alignment supported by the implementation
11985 in all contexts, which is equal to
11986 alignof(max_align_t)]. */
11988 bool
11989 cxx_fundamental_alignment_p (unsigned align)
11991 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11992 TYPE_ALIGN (long_double_type_node)));
11995 /* Return true if T is a pointer to a zero-sized aggregate. */
11997 bool
11998 pointer_to_zero_sized_aggr_p (tree t)
12000 if (!POINTER_TYPE_P (t))
12001 return false;
12002 t = TREE_TYPE (t);
12003 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12006 #include "gt-c-family-c-common.h"