PR libstdc++/65978
[official-gcc.git] / gcc / c-family / c-common.c
blobada8e8aa9016d0e93448f6befe97ef2560e3f016
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 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 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "c-common.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "varasm.h"
36 #include "trans-mem.h"
37 #include "flags.h"
38 #include "c-pragma.h"
39 #include "c-objc.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "cpplib.h"
43 #include "target.h"
44 #include "common/common-target.h"
45 #include "langhooks.h"
46 #include "tree-inline.h"
47 #include "toplev.h"
48 #include "diagnostic.h"
49 #include "tree-iterator.h"
50 #include "hashtab.h"
51 #include "opts.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "vec.h"
56 #include "hash-set.h"
57 #include "machmode.h"
58 #include "hard-reg-set.h"
59 #include "input.h"
60 #include "function.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "target-def.h"
64 #include "gimplify.h"
65 #include "wide-int-print.h"
66 #include "gimple-expr.h"
68 cpp_reader *parse_in; /* Declared in c-pragma.h. */
70 /* Mode used to build pointers (VOIDmode means ptr_mode). */
72 machine_mode c_default_pointer_mode = VOIDmode;
74 /* The following symbols are subsumed in the c_global_trees array, and
75 listed here individually for documentation purposes.
77 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
79 tree short_integer_type_node;
80 tree long_integer_type_node;
81 tree long_long_integer_type_node;
83 tree short_unsigned_type_node;
84 tree long_unsigned_type_node;
85 tree long_long_unsigned_type_node;
87 tree truthvalue_type_node;
88 tree truthvalue_false_node;
89 tree truthvalue_true_node;
91 tree ptrdiff_type_node;
93 tree unsigned_char_type_node;
94 tree signed_char_type_node;
95 tree wchar_type_node;
97 tree char16_type_node;
98 tree char32_type_node;
100 tree float_type_node;
101 tree double_type_node;
102 tree long_double_type_node;
104 tree complex_integer_type_node;
105 tree complex_float_type_node;
106 tree complex_double_type_node;
107 tree complex_long_double_type_node;
109 tree dfloat32_type_node;
110 tree dfloat64_type_node;
111 tree_dfloat128_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `wchar_t[SOMENUMBER]' or something like it.
142 Used when a wide string literal is created.
144 tree wchar_array_type_node;
146 Type `char16_t[SOMENUMBER]' or something like it.
147 Used when a UTF-16 string literal is created.
149 tree char16_array_type_node;
151 Type `char32_t[SOMENUMBER]' or something like it.
152 Used when a UTF-32 string literal is created.
154 tree char32_array_type_node;
156 Type `int ()' -- used for implicit declaration of functions.
158 tree default_function_type;
160 A VOID_TYPE node, packaged in a TREE_LIST.
162 tree void_list_node;
164 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
165 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 VAR_DECLS, but C++ does.)
168 tree function_name_decl_node;
169 tree pretty_function_name_decl_node;
170 tree c99_function_name_decl_node;
172 Stack of nested function name VAR_DECLs.
174 tree saved_function_name_decls;
178 tree c_global_trees[CTI_MAX];
180 /* Switches common to the C front ends. */
182 /* Nonzero means don't output line number information. */
184 char flag_no_line_commands;
186 /* Nonzero causes -E output not to be done, but directives such as
187 #define that have side effects are still obeyed. */
189 char flag_no_output;
191 /* Nonzero means dump macros in some fashion. */
193 char flag_dump_macros;
195 /* Nonzero means pass #include lines through to the output. */
197 char flag_dump_includes;
199 /* Nonzero means process PCH files while preprocessing. */
201 bool flag_pch_preprocess;
203 /* The file name to which we should write a precompiled header, or
204 NULL if no header will be written in this compile. */
206 const char *pch_file;
208 /* Nonzero if an ISO standard was selected. It rejects macros in the
209 user's namespace. */
210 int flag_iso;
212 /* C/ObjC language option variables. */
215 /* Nonzero means allow type mismatches in conditional expressions;
216 just make their values `void'. */
218 int flag_cond_mismatch;
220 /* Nonzero means enable C89 Amendment 1 features. */
222 int flag_isoc94;
224 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
226 int flag_isoc99;
228 /* Nonzero means use the ISO C11 dialect of C. */
230 int flag_isoc11;
232 /* Nonzero means that we have builtin functions, and main is an int. */
234 int flag_hosted = 1;
237 /* ObjC language option variables. */
240 /* Tells the compiler that this is a special run. Do not perform any
241 compiling, instead we are to test some platform dependent features
242 and output a C header file with appropriate definitions. */
244 int print_struct_values;
246 /* Tells the compiler what is the constant string class for ObjC. */
248 const char *constant_string_class_name;
251 /* C++ language option variables. */
254 /* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
257 int flag_use_repository;
259 /* The C++ dialect being used. C++98 is the default. */
261 enum cxx_dialect cxx_dialect = cxx98;
263 /* Maximum template instantiation depth. This limit exists to limit the
264 time it takes to notice excessively recursive template instantiations.
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
270 int max_tinst_depth = 900;
272 /* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
274 tree *ridpointers;
276 tree (*make_fname_decl) (location_t, tree, int);
278 /* Nonzero means don't warn about problems that occur when the code is
279 executed. */
280 int c_inhibit_evaluation_warnings;
282 /* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
287 be generated. */
288 bool in_late_binary_op;
290 /* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292 bool done_lexing = false;
294 /* Information about how a function name is generated. */
295 struct fname_var_t
297 tree *const decl; /* pointer to the VAR_DECL. */
298 const unsigned rid; /* RID number for the identifier. */
299 const int pretty; /* How pretty is it? */
302 /* The three ways of getting then name of the current function. */
304 const struct fname_var_t fname_vars[] =
306 /* C99 compliant __func__, must be first. */
307 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
308 /* GCC __FUNCTION__ compliant. */
309 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
310 /* GCC __PRETTY_FUNCTION__ compliant. */
311 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
312 {NULL, 0, 0},
315 /* Global visibility options. */
316 struct visibility_flags visibility_options;
318 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
319 static tree check_case_value (location_t, tree);
320 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
322 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
329 int, bool *);
330 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
331 int, bool *);
332 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_always_inline_attribute (tree *, tree, tree, int,
340 bool *);
341 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_transparent_union_attribute (tree *, tree, tree,
353 int, bool *);
354 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
360 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
361 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
364 static tree handle_visibility_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_tls_model_attribute (tree *, tree, tree, int,
367 bool *);
368 static tree handle_no_instrument_function_attribute (tree *, tree,
369 tree, int, bool *);
370 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_deprecated_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_vector_size_attribute (tree *, tree, tree, int,
381 bool *);
382 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
386 bool *);
387 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
394 static tree ignore_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
400 bool *);
401 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
402 bool *);
403 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
404 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
406 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
408 static void check_function_nonnull (tree, int, tree *);
409 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
410 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
411 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
412 static int resort_field_decl_cmp (const void *, const void *);
414 /* Reserved words. The third field is a mask: keywords are disabled
415 if they match the mask.
417 Masks for languages:
418 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
419 C --std=c99: D_CXXONLY | D_OBJC
420 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
421 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
422 C++ --std=c0x: D_CONLY | D_OBJC
423 ObjC++ is like C++ except that D_OBJC is not set
425 If -fno-asm is used, D_ASM is added to the mask. If
426 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
427 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
428 In C with -Wc++-compat, we warn if D_CXXWARN is set.
430 Note the complication of the D_CXX_OBJC keywords. These are
431 reserved words such as 'class'. In C++, 'class' is a reserved
432 word. In Objective-C++ it is too. In Objective-C, it is a
433 reserved word too, but only if it follows an '@' sign.
435 const struct c_common_resword c_common_reswords[] =
437 { "_Alignas", RID_ALIGNAS, D_CONLY },
438 { "_Alignof", RID_ALIGNOF, D_CONLY },
439 { "_Atomic", RID_ATOMIC, D_CONLY },
440 { "_Bool", RID_BOOL, D_CONLY },
441 { "_Complex", RID_COMPLEX, 0 },
442 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
443 { "_Cilk_sync", RID_CILK_SYNC, 0 },
444 { "_Cilk_for", RID_CILK_FOR, 0 },
445 { "_Imaginary", RID_IMAGINARY, D_CONLY },
446 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
447 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
448 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
449 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
450 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
451 { "_Sat", RID_SAT, D_CONLY | D_EXT },
452 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
453 { "_Noreturn", RID_NORETURN, D_CONLY },
454 { "_Generic", RID_GENERIC, D_CONLY },
455 { "_Thread_local", RID_THREAD, D_CONLY },
456 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
457 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
458 { "__alignof", RID_ALIGNOF, 0 },
459 { "__alignof__", RID_ALIGNOF, 0 },
460 { "__asm", RID_ASM, 0 },
461 { "__asm__", RID_ASM, 0 },
462 { "__attribute", RID_ATTRIBUTE, 0 },
463 { "__attribute__", RID_ATTRIBUTE, 0 },
464 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
465 { "__bases", RID_BASES, D_CXXONLY },
466 { "__builtin_call_with_static_chain",
467 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
468 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
469 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
470 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
471 { "__builtin_offsetof", RID_OFFSETOF, 0 },
472 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
473 { "__builtin_va_arg", RID_VA_ARG, 0 },
474 { "__complex", RID_COMPLEX, 0 },
475 { "__complex__", RID_COMPLEX, 0 },
476 { "__const", RID_CONST, 0 },
477 { "__const__", RID_CONST, 0 },
478 { "__decltype", RID_DECLTYPE, D_CXXONLY },
479 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
480 { "__extension__", RID_EXTENSION, 0 },
481 { "__func__", RID_C99_FUNCTION_NAME, 0 },
482 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
483 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
484 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
485 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
486 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
487 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
488 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
489 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
490 { "__imag", RID_IMAGPART, 0 },
491 { "__imag__", RID_IMAGPART, 0 },
492 { "__inline", RID_INLINE, 0 },
493 { "__inline__", RID_INLINE, 0 },
494 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
495 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
496 { "__is_class", RID_IS_CLASS, D_CXXONLY },
497 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
498 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
499 { "__is_final", RID_IS_FINAL, D_CXXONLY },
500 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
501 { "__is_pod", RID_IS_POD, D_CXXONLY },
502 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
503 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
504 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
505 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
506 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
507 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
508 { "__is_union", RID_IS_UNION, D_CXXONLY },
509 { "__label__", RID_LABEL, 0 },
510 { "__null", RID_NULL, 0 },
511 { "__real", RID_REALPART, 0 },
512 { "__real__", RID_REALPART, 0 },
513 { "__restrict", RID_RESTRICT, 0 },
514 { "__restrict__", RID_RESTRICT, 0 },
515 { "__signed", RID_SIGNED, 0 },
516 { "__signed__", RID_SIGNED, 0 },
517 { "__thread", RID_THREAD, 0 },
518 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
519 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
520 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
521 { "__typeof", RID_TYPEOF, 0 },
522 { "__typeof__", RID_TYPEOF, 0 },
523 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
524 { "__volatile", RID_VOLATILE, 0 },
525 { "__volatile__", RID_VOLATILE, 0 },
526 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "asm", RID_ASM, D_ASM },
529 { "auto", RID_AUTO, 0 },
530 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
531 { "break", RID_BREAK, 0 },
532 { "case", RID_CASE, 0 },
533 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
534 { "char", RID_CHAR, 0 },
535 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
538 { "const", RID_CONST, 0 },
539 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
541 { "continue", RID_CONTINUE, 0 },
542 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
543 { "default", RID_DEFAULT, 0 },
544 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
545 { "do", RID_DO, 0 },
546 { "double", RID_DOUBLE, 0 },
547 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
548 { "else", RID_ELSE, 0 },
549 { "enum", RID_ENUM, 0 },
550 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
551 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
552 { "extern", RID_EXTERN, 0 },
553 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
554 { "float", RID_FLOAT, 0 },
555 { "for", RID_FOR, 0 },
556 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
557 { "goto", RID_GOTO, 0 },
558 { "if", RID_IF, 0 },
559 { "inline", RID_INLINE, D_EXT89 },
560 { "int", RID_INT, 0 },
561 { "long", RID_LONG, 0 },
562 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
563 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
564 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
565 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
566 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
568 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
569 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
570 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
571 { "register", RID_REGISTER, 0 },
572 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
573 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
574 { "return", RID_RETURN, 0 },
575 { "short", RID_SHORT, 0 },
576 { "signed", RID_SIGNED, 0 },
577 { "sizeof", RID_SIZEOF, 0 },
578 { "static", RID_STATIC, 0 },
579 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
580 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
581 { "struct", RID_STRUCT, 0 },
582 { "switch", RID_SWITCH, 0 },
583 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
584 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
585 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
586 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
587 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
588 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
589 { "typedef", RID_TYPEDEF, 0 },
590 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
591 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
592 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
593 { "union", RID_UNION, 0 },
594 { "unsigned", RID_UNSIGNED, 0 },
595 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
596 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
597 { "void", RID_VOID, 0 },
598 { "volatile", RID_VOLATILE, 0 },
599 { "wchar_t", RID_WCHAR, D_CXXONLY },
600 { "while", RID_WHILE, 0 },
601 /* These Objective-C keywords are recognized only immediately after
602 an '@'. */
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
612 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
615 { "property", RID_AT_PROPERTY, D_OBJC },
616 { "package", RID_AT_PACKAGE, D_OBJC },
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 /* These are recognized only in protocol-qualifier context
620 (see above) */
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
641 /* Table of machine-independent attributes common to all C-like languages.
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table[] =
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute , false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, false },
695 { "transparent_union", 0, 0, false, false, false,
696 handle_transparent_union_attribute, false },
697 { "constructor", 0, 1, true, false, false,
698 handle_constructor_attribute, false },
699 { "destructor", 0, 1, true, false, false,
700 handle_destructor_attribute, false },
701 { "mode", 1, 1, false, true, false,
702 handle_mode_attribute, false },
703 { "section", 1, 1, true, false, false,
704 handle_section_attribute, false },
705 { "aligned", 0, 1, false, false, false,
706 handle_aligned_attribute, false },
707 { "weak", 0, 0, true, false, false,
708 handle_weak_attribute, false },
709 { "ifunc", 1, 1, true, false, false,
710 handle_ifunc_attribute, false },
711 { "alias", 1, 1, true, false, false,
712 handle_alias_attribute, false },
713 { "weakref", 0, 1, true, false, false,
714 handle_weakref_attribute, false },
715 { "no_instrument_function", 0, 0, true, false, false,
716 handle_no_instrument_function_attribute,
717 false },
718 { "malloc", 0, 0, true, false, false,
719 handle_malloc_attribute, false },
720 { "returns_twice", 0, 0, true, false, false,
721 handle_returns_twice_attribute, false },
722 { "no_stack_limit", 0, 0, true, false, false,
723 handle_no_limit_stack_attribute, false },
724 { "pure", 0, 0, true, false, false,
725 handle_pure_attribute, false },
726 { "transaction_callable", 0, 0, false, true, false,
727 handle_tm_attribute, false },
728 { "transaction_unsafe", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_safe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_may_cancel_outer", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 /* ??? These two attributes didn't make the transition from the
735 Intel language document to the multi-vendor language document. */
736 { "transaction_pure", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 { "transaction_wrap", 1, 1, true, false, false,
739 handle_tm_wrap_attribute, false },
740 /* For internal use (marking of builtins) only. The name contains space
741 to prevent its usage in source code. */
742 { "no vops", 0, 0, true, false, false,
743 handle_novops_attribute, false },
744 { "deprecated", 0, 1, false, false, false,
745 handle_deprecated_attribute, false },
746 { "vector_size", 1, 1, false, true, false,
747 handle_vector_size_attribute, false },
748 { "visibility", 1, 1, false, false, false,
749 handle_visibility_attribute, false },
750 { "tls_model", 1, 1, true, false, false,
751 handle_tls_model_attribute, false },
752 { "nonnull", 0, -1, false, true, true,
753 handle_nonnull_attribute, false },
754 { "nothrow", 0, 0, true, false, false,
755 handle_nothrow_attribute, false },
756 { "may_alias", 0, 0, false, true, false, NULL, false },
757 { "cleanup", 1, 1, true, false, false,
758 handle_cleanup_attribute, false },
759 { "warn_unused_result", 0, 0, false, true, true,
760 handle_warn_unused_result_attribute, false },
761 { "sentinel", 0, 1, false, true, true,
762 handle_sentinel_attribute, false },
763 /* For internal use (marking of builtins) only. The name contains space
764 to prevent its usage in source code. */
765 { "type generic", 0, 0, false, true, true,
766 handle_type_generic_attribute, false },
767 { "alloc_size", 1, 2, false, true, true,
768 handle_alloc_size_attribute, false },
769 { "cold", 0, 0, true, false, false,
770 handle_cold_attribute, false },
771 { "hot", 0, 0, true, false, false,
772 handle_hot_attribute, false },
773 { "no_address_safety_analysis",
774 0, 0, true, false, false,
775 handle_no_address_safety_analysis_attribute,
776 false },
777 { "no_sanitize_address", 0, 0, true, false, false,
778 handle_no_sanitize_address_attribute,
779 false },
780 { "no_sanitize_thread", 0, 0, true, false, false,
781 handle_no_sanitize_address_attribute,
782 false },
783 { "no_sanitize_undefined", 0, 0, true, false, false,
784 handle_no_sanitize_undefined_attribute,
785 false },
786 { "warning", 1, 1, true, false, false,
787 handle_error_attribute, false },
788 { "error", 1, 1, true, false, false,
789 handle_error_attribute, false },
790 { "target", 1, -1, true, false, false,
791 handle_target_attribute, false },
792 { "optimize", 1, -1, true, false, false,
793 handle_optimize_attribute, false },
794 /* For internal use only. The leading '*' both prevents its usage in
795 source code and signals that it may be overridden by machine tables. */
796 { "*tm regparm", 0, 0, false, true, true,
797 ignore_attribute, false },
798 { "no_split_stack", 0, 0, true, false, false,
799 handle_no_split_stack_attribute, false },
800 /* For internal use (marking of builtins and runtime functions) only.
801 The name contains space to prevent its usage in source code. */
802 { "fn spec", 1, 1, false, true, true,
803 handle_fnspec_attribute, false },
804 { "warn_unused", 0, 0, false, false, false,
805 handle_warn_unused_attribute, false },
806 { "returns_nonnull", 0, 0, false, true, true,
807 handle_returns_nonnull_attribute, false },
808 { "omp declare simd", 0, -1, true, false, false,
809 handle_omp_declare_simd_attribute, false },
810 { "cilk simd function", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
812 { "omp declare target", 0, 0, true, false, false,
813 handle_omp_declare_target_attribute, false },
814 { "alloc_align", 1, 1, false, true, true,
815 handle_alloc_align_attribute, false },
816 { "assume_aligned", 1, 2, false, true, true,
817 handle_assume_aligned_attribute, false },
818 { "designated_init", 0, 0, false, true, false,
819 handle_designated_init_attribute, false },
820 { "bnd_variable_size", 0, 0, true, false, false,
821 handle_bnd_variable_size_attribute, false },
822 { "bnd_legacy", 0, 0, true, false, false,
823 handle_bnd_legacy, false },
824 { "bnd_instrument", 0, 0, true, false, false,
825 handle_bnd_instrument, false },
826 { NULL, 0, 0, false, false, false, NULL, false }
829 /* Give the specifications for the format attributes, used by C and all
830 descendants.
832 All attributes referencing arguments should be additionally processed
833 in chkp_copy_function_type_adding_bounds for correct instrumentation
834 by Pointer Bounds Checker.
835 Current list of processed format attributes: format, format_arg. */
836 const struct attribute_spec c_common_format_attribute_table[] =
838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
839 affects_type_identity } */
840 { "format", 3, 3, false, true, true,
841 handle_format_attribute, false },
842 { "format_arg", 1, 1, false, true, true,
843 handle_format_arg_attribute, false },
844 { NULL, 0, 0, false, false, false, NULL, false }
847 /* Return identifier for address space AS. */
849 const char *
850 c_addr_space_name (addr_space_t as)
852 int rid = RID_FIRST_ADDR_SPACE + as;
853 gcc_assert (ridpointers [rid]);
854 return IDENTIFIER_POINTER (ridpointers [rid]);
857 /* Push current bindings for the function name VAR_DECLS. */
859 void
860 start_fname_decls (void)
862 unsigned ix;
863 tree saved = NULL_TREE;
865 for (ix = 0; fname_vars[ix].decl; ix++)
867 tree decl = *fname_vars[ix].decl;
869 if (decl)
871 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
872 saved);
873 *fname_vars[ix].decl = NULL_TREE;
876 if (saved || saved_function_name_decls)
877 /* Normally they'll have been NULL, so only push if we've got a
878 stack, or they are non-NULL. */
879 saved_function_name_decls = tree_cons (saved, NULL_TREE,
880 saved_function_name_decls);
883 /* Finish up the current bindings, adding them into the current function's
884 statement tree. This must be done _before_ finish_stmt_tree is called.
885 If there is no current function, we must be at file scope and no statements
886 are involved. Pop the previous bindings. */
888 void
889 finish_fname_decls (void)
891 unsigned ix;
892 tree stmts = NULL_TREE;
893 tree stack = saved_function_name_decls;
895 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
896 append_to_statement_list (TREE_VALUE (stack), &stmts);
898 if (stmts)
900 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
902 if (TREE_CODE (*bodyp) == BIND_EXPR)
903 bodyp = &BIND_EXPR_BODY (*bodyp);
905 append_to_statement_list_force (*bodyp, &stmts);
906 *bodyp = stmts;
909 for (ix = 0; fname_vars[ix].decl; ix++)
910 *fname_vars[ix].decl = NULL_TREE;
912 if (stack)
914 /* We had saved values, restore them. */
915 tree saved;
917 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
919 tree decl = TREE_PURPOSE (saved);
920 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
922 *fname_vars[ix].decl = decl;
924 stack = TREE_CHAIN (stack);
926 saved_function_name_decls = stack;
929 /* Return the text name of the current function, suitably prettified
930 by PRETTY_P. Return string must be freed by caller. */
932 const char *
933 fname_as_string (int pretty_p)
935 const char *name = "top level";
936 char *namep;
937 int vrb = 2, len;
938 cpp_string cstr = { 0, 0 }, strname;
940 if (!pretty_p)
942 name = "";
943 vrb = 0;
946 if (current_function_decl)
947 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
949 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
951 namep = XNEWVEC (char, len);
952 snprintf (namep, len, "\"%s\"", name);
953 strname.text = (unsigned char *) namep;
954 strname.len = len - 1;
956 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
958 XDELETEVEC (namep);
959 return (const char *) cstr.text;
962 return namep;
965 /* Return the VAR_DECL for a const char array naming the current
966 function. If the VAR_DECL has not yet been created, create it
967 now. RID indicates how it should be formatted and IDENTIFIER_NODE
968 ID is its name (unfortunately C and C++ hold the RID values of
969 keywords in different places, so we can't derive RID from ID in
970 this language independent code. LOC is the location of the
971 function. */
973 tree
974 fname_decl (location_t loc, unsigned int rid, tree id)
976 unsigned ix;
977 tree decl = NULL_TREE;
979 for (ix = 0; fname_vars[ix].decl; ix++)
980 if (fname_vars[ix].rid == rid)
981 break;
983 decl = *fname_vars[ix].decl;
984 if (!decl)
986 /* If a tree is built here, it would normally have the lineno of
987 the current statement. Later this tree will be moved to the
988 beginning of the function and this line number will be wrong.
989 To avoid this problem set the lineno to 0 here; that prevents
990 it from appearing in the RTL. */
991 tree stmts;
992 location_t saved_location = input_location;
993 input_location = UNKNOWN_LOCATION;
995 stmts = push_stmt_list ();
996 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
997 stmts = pop_stmt_list (stmts);
998 if (!IS_EMPTY_STMT (stmts))
999 saved_function_name_decls
1000 = tree_cons (decl, stmts, saved_function_name_decls);
1001 *fname_vars[ix].decl = decl;
1002 input_location = saved_location;
1004 if (!ix && !current_function_decl)
1005 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1007 return decl;
1010 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1012 tree
1013 fix_string_type (tree value)
1015 int length = TREE_STRING_LENGTH (value);
1016 int nchars;
1017 tree e_type, i_type, a_type;
1019 /* Compute the number of elements, for the array type. */
1020 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1022 nchars = length;
1023 e_type = char_type_node;
1025 else if (TREE_TYPE (value) == char16_array_type_node)
1027 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1028 e_type = char16_type_node;
1030 else if (TREE_TYPE (value) == char32_array_type_node)
1032 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1033 e_type = char32_type_node;
1035 else
1037 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1038 e_type = wchar_type_node;
1041 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1042 limit in C++98 Annex B is very large (65536) and is not normative,
1043 so we do not diagnose it (warn_overlength_strings is forced off
1044 in c_common_post_options). */
1045 if (warn_overlength_strings)
1047 const int nchars_max = flag_isoc99 ? 4095 : 509;
1048 const int relevant_std = flag_isoc99 ? 99 : 90;
1049 if (nchars - 1 > nchars_max)
1050 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1051 separate the %d from the 'C'. 'ISO' should not be
1052 translated, but it may be moved after 'C%d' in languages
1053 where modifiers follow nouns. */
1054 pedwarn (input_location, OPT_Woverlength_strings,
1055 "string length %qd is greater than the length %qd "
1056 "ISO C%d compilers are required to support",
1057 nchars - 1, nchars_max, relevant_std);
1060 /* Create the array type for the string constant. The ISO C++
1061 standard says that a string literal has type `const char[N]' or
1062 `const wchar_t[N]'. We use the same logic when invoked as a C
1063 front-end with -Wwrite-strings.
1064 ??? We should change the type of an expression depending on the
1065 state of a warning flag. We should just be warning -- see how
1066 this is handled in the C++ front-end for the deprecated implicit
1067 conversion from string literals to `char*' or `wchar_t*'.
1069 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1070 array type being the unqualified version of that type.
1071 Therefore, if we are constructing an array of const char, we must
1072 construct the matching unqualified array type first. The C front
1073 end does not require this, but it does no harm, so we do it
1074 unconditionally. */
1075 i_type = build_index_type (size_int (nchars - 1));
1076 a_type = build_array_type (e_type, i_type);
1077 if (c_dialect_cxx() || warn_write_strings)
1078 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1080 TREE_TYPE (value) = a_type;
1081 TREE_CONSTANT (value) = 1;
1082 TREE_READONLY (value) = 1;
1083 TREE_STATIC (value) = 1;
1084 return value;
1087 /* If DISABLE is true, stop issuing warnings. This is used when
1088 parsing code that we know will not be executed. This function may
1089 be called multiple times, and works as a stack. */
1091 static void
1092 c_disable_warnings (bool disable)
1094 if (disable)
1096 ++c_inhibit_evaluation_warnings;
1097 fold_defer_overflow_warnings ();
1101 /* If ENABLE is true, reenable issuing warnings. */
1103 static void
1104 c_enable_warnings (bool enable)
1106 if (enable)
1108 --c_inhibit_evaluation_warnings;
1109 fold_undefer_and_ignore_overflow_warnings ();
1113 /* Fully fold EXPR, an expression that was not folded (beyond integer
1114 constant expressions and null pointer constants) when being built
1115 up. If IN_INIT, this is in a static initializer and certain
1116 changes are made to the folding done. Clear *MAYBE_CONST if
1117 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1118 expression because it contains an evaluated operator (in C99) or an
1119 operator outside of sizeof returning an integer constant (in C90)
1120 not permitted in constant expressions, or because it contains an
1121 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1122 set to true by callers before calling this function.) Return the
1123 folded expression. Function arguments have already been folded
1124 before calling this function, as have the contents of SAVE_EXPR,
1125 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1126 C_MAYBE_CONST_EXPR. */
1128 tree
1129 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1131 tree ret;
1132 tree eptype = NULL_TREE;
1133 bool dummy = true;
1134 bool maybe_const_itself = true;
1135 location_t loc = EXPR_LOCATION (expr);
1137 /* This function is not relevant to C++ because C++ folds while
1138 parsing, and may need changes to be correct for C++ when C++
1139 stops folding while parsing. */
1140 if (c_dialect_cxx ())
1141 gcc_unreachable ();
1143 if (!maybe_const)
1144 maybe_const = &dummy;
1145 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1147 eptype = TREE_TYPE (expr);
1148 expr = TREE_OPERAND (expr, 0);
1150 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1151 &maybe_const_itself);
1152 if (eptype)
1153 ret = fold_convert_loc (loc, eptype, ret);
1154 *maybe_const &= maybe_const_itself;
1155 return ret;
1158 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1159 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1160 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1161 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1162 both evaluated and unevaluated subexpressions while
1163 *MAYBE_CONST_ITSELF is carried from only evaluated
1164 subexpressions). */
1166 static tree
1167 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1168 bool *maybe_const_itself)
1170 tree ret = expr;
1171 enum tree_code code = TREE_CODE (expr);
1172 enum tree_code_class kind = TREE_CODE_CLASS (code);
1173 location_t loc = EXPR_LOCATION (expr);
1174 tree op0, op1, op2, op3;
1175 tree orig_op0, orig_op1, orig_op2;
1176 bool op0_const = true, op1_const = true, op2_const = true;
1177 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1178 bool nowarning = TREE_NO_WARNING (expr);
1179 bool unused_p;
1181 /* This function is not relevant to C++ because C++ folds while
1182 parsing, and may need changes to be correct for C++ when C++
1183 stops folding while parsing. */
1184 if (c_dialect_cxx ())
1185 gcc_unreachable ();
1187 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1188 anything else not counted as an expression cannot usefully be
1189 folded further at this point. */
1190 if (!IS_EXPR_CODE_CLASS (kind)
1191 || kind == tcc_statement
1192 || code == SAVE_EXPR)
1193 return expr;
1195 /* Operands of variable-length expressions (function calls) have
1196 already been folded, as have __builtin_* function calls, and such
1197 expressions cannot occur in constant expressions. */
1198 if (kind == tcc_vl_exp)
1200 *maybe_const_operands = false;
1201 ret = fold (expr);
1202 goto out;
1205 if (code == C_MAYBE_CONST_EXPR)
1207 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1208 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1209 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1210 *maybe_const_operands = false;
1211 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1212 *maybe_const_itself = false;
1213 if (pre && !in_init)
1214 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1215 else
1216 ret = inner;
1217 goto out;
1220 /* Assignment, increment, decrement, function call and comma
1221 operators, and statement expressions, cannot occur in constant
1222 expressions if evaluated / outside of sizeof. (Function calls
1223 were handled above, though VA_ARG_EXPR is treated like a function
1224 call here, and statement expressions are handled through
1225 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1226 switch (code)
1228 case MODIFY_EXPR:
1229 case PREDECREMENT_EXPR:
1230 case PREINCREMENT_EXPR:
1231 case POSTDECREMENT_EXPR:
1232 case POSTINCREMENT_EXPR:
1233 case COMPOUND_EXPR:
1234 *maybe_const_operands = false;
1235 break;
1237 case VA_ARG_EXPR:
1238 case TARGET_EXPR:
1239 case BIND_EXPR:
1240 case OBJ_TYPE_REF:
1241 *maybe_const_operands = false;
1242 ret = fold (expr);
1243 goto out;
1245 default:
1246 break;
1249 /* Fold individual tree codes as appropriate. */
1250 switch (code)
1252 case COMPOUND_LITERAL_EXPR:
1253 /* Any non-constancy will have been marked in a containing
1254 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1255 goto out;
1257 case COMPONENT_REF:
1258 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1259 op1 = TREE_OPERAND (expr, 1);
1260 op2 = TREE_OPERAND (expr, 2);
1261 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1262 maybe_const_itself);
1263 STRIP_TYPE_NOPS (op0);
1264 if (op0 != orig_op0)
1265 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1266 if (ret != expr)
1268 TREE_READONLY (ret) = TREE_READONLY (expr);
1269 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1271 goto out;
1273 case ARRAY_REF:
1274 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1275 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1276 op2 = TREE_OPERAND (expr, 2);
1277 op3 = TREE_OPERAND (expr, 3);
1278 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1279 maybe_const_itself);
1280 STRIP_TYPE_NOPS (op0);
1281 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1282 maybe_const_itself);
1283 STRIP_TYPE_NOPS (op1);
1284 op1 = decl_constant_value_for_optimization (op1);
1285 if (op0 != orig_op0 || op1 != orig_op1)
1286 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1287 if (ret != expr)
1289 TREE_READONLY (ret) = TREE_READONLY (expr);
1290 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1291 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1293 ret = fold (ret);
1294 goto out;
1296 case COMPOUND_EXPR:
1297 case MODIFY_EXPR:
1298 case PREDECREMENT_EXPR:
1299 case PREINCREMENT_EXPR:
1300 case POSTDECREMENT_EXPR:
1301 case POSTINCREMENT_EXPR:
1302 case PLUS_EXPR:
1303 case MINUS_EXPR:
1304 case MULT_EXPR:
1305 case POINTER_PLUS_EXPR:
1306 case TRUNC_DIV_EXPR:
1307 case CEIL_DIV_EXPR:
1308 case FLOOR_DIV_EXPR:
1309 case TRUNC_MOD_EXPR:
1310 case RDIV_EXPR:
1311 case EXACT_DIV_EXPR:
1312 case LSHIFT_EXPR:
1313 case RSHIFT_EXPR:
1314 case BIT_IOR_EXPR:
1315 case BIT_XOR_EXPR:
1316 case BIT_AND_EXPR:
1317 case LT_EXPR:
1318 case LE_EXPR:
1319 case GT_EXPR:
1320 case GE_EXPR:
1321 case EQ_EXPR:
1322 case NE_EXPR:
1323 case COMPLEX_EXPR:
1324 case TRUTH_AND_EXPR:
1325 case TRUTH_OR_EXPR:
1326 case TRUTH_XOR_EXPR:
1327 case UNORDERED_EXPR:
1328 case ORDERED_EXPR:
1329 case UNLT_EXPR:
1330 case UNLE_EXPR:
1331 case UNGT_EXPR:
1332 case UNGE_EXPR:
1333 case UNEQ_EXPR:
1334 /* Binary operations evaluating both arguments (increment and
1335 decrement are binary internally in GCC). */
1336 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1337 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1338 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1339 maybe_const_itself);
1340 STRIP_TYPE_NOPS (op0);
1341 if (code != MODIFY_EXPR
1342 && code != PREDECREMENT_EXPR
1343 && code != PREINCREMENT_EXPR
1344 && code != POSTDECREMENT_EXPR
1345 && code != POSTINCREMENT_EXPR)
1346 op0 = decl_constant_value_for_optimization (op0);
1347 /* The RHS of a MODIFY_EXPR was fully folded when building that
1348 expression for the sake of conversion warnings. */
1349 if (code != MODIFY_EXPR)
1350 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1351 maybe_const_itself);
1352 STRIP_TYPE_NOPS (op1);
1353 op1 = decl_constant_value_for_optimization (op1);
1354 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1355 ret = in_init
1356 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1357 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1358 else
1359 ret = fold (expr);
1360 if (TREE_OVERFLOW_P (ret)
1361 && !TREE_OVERFLOW_P (op0)
1362 && !TREE_OVERFLOW_P (op1))
1363 overflow_warning (EXPR_LOCATION (expr), ret);
1364 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1365 && TREE_CODE (orig_op1) != INTEGER_CST
1366 && TREE_CODE (op1) == INTEGER_CST
1367 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1368 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1369 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1370 && c_inhibit_evaluation_warnings == 0)
1372 if (tree_int_cst_sgn (op1) < 0)
1373 warning_at (loc, OPT_Wshift_count_negative,
1374 (code == LSHIFT_EXPR
1375 ? G_("left shift count is negative")
1376 : G_("right shift count is negative")));
1377 else if (compare_tree_int (op1,
1378 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1379 >= 0)
1380 warning_at (loc, OPT_Wshift_count_overflow,
1381 (code == LSHIFT_EXPR
1382 ? G_("left shift count >= width of type")
1383 : G_("right shift count >= width of type")));
1385 if ((code == TRUNC_DIV_EXPR
1386 || code == CEIL_DIV_EXPR
1387 || code == FLOOR_DIV_EXPR
1388 || code == EXACT_DIV_EXPR
1389 || code == TRUNC_MOD_EXPR)
1390 && TREE_CODE (orig_op1) != INTEGER_CST
1391 && TREE_CODE (op1) == INTEGER_CST
1392 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1393 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1394 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1395 warn_for_div_by_zero (loc, op1);
1396 goto out;
1398 case INDIRECT_REF:
1399 case FIX_TRUNC_EXPR:
1400 case FLOAT_EXPR:
1401 CASE_CONVERT:
1402 case ADDR_SPACE_CONVERT_EXPR:
1403 case VIEW_CONVERT_EXPR:
1404 case NON_LVALUE_EXPR:
1405 case NEGATE_EXPR:
1406 case BIT_NOT_EXPR:
1407 case TRUTH_NOT_EXPR:
1408 case ADDR_EXPR:
1409 case CONJ_EXPR:
1410 case REALPART_EXPR:
1411 case IMAGPART_EXPR:
1412 /* Unary operations. */
1413 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1414 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1415 maybe_const_itself);
1416 STRIP_TYPE_NOPS (op0);
1417 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1418 op0 = decl_constant_value_for_optimization (op0);
1419 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1420 not prepared to deal with them if they occur in initializers. */
1421 if (op0 != orig_op0
1422 && code == ADDR_EXPR
1423 && (op1 = get_base_address (op0)) != NULL_TREE
1424 && TREE_CODE (op1) == INDIRECT_REF
1425 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1426 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1427 else if (op0 != orig_op0 || in_init)
1428 ret = in_init
1429 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1430 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1431 else
1432 ret = fold (expr);
1433 if (code == INDIRECT_REF
1434 && ret != expr
1435 && TREE_CODE (ret) == INDIRECT_REF)
1437 TREE_READONLY (ret) = TREE_READONLY (expr);
1438 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1439 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1441 switch (code)
1443 case FIX_TRUNC_EXPR:
1444 case FLOAT_EXPR:
1445 CASE_CONVERT:
1446 /* Don't warn about explicit conversions. We will already
1447 have warned about suspect implicit conversions. */
1448 break;
1450 default:
1451 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1452 overflow_warning (EXPR_LOCATION (expr), ret);
1453 break;
1455 goto out;
1457 case TRUTH_ANDIF_EXPR:
1458 case TRUTH_ORIF_EXPR:
1459 /* Binary operations not necessarily evaluating both
1460 arguments. */
1461 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1462 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1463 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1464 STRIP_TYPE_NOPS (op0);
1466 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1467 ? truthvalue_false_node
1468 : truthvalue_true_node));
1469 c_disable_warnings (unused_p);
1470 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1471 STRIP_TYPE_NOPS (op1);
1472 c_enable_warnings (unused_p);
1474 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1475 ret = in_init
1476 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1477 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1478 else
1479 ret = fold (expr);
1480 *maybe_const_operands &= op0_const;
1481 *maybe_const_itself &= op0_const_self;
1482 if (!(flag_isoc99
1483 && op0_const
1484 && op0_const_self
1485 && (code == TRUTH_ANDIF_EXPR
1486 ? op0 == truthvalue_false_node
1487 : op0 == truthvalue_true_node)))
1488 *maybe_const_operands &= op1_const;
1489 if (!(op0_const
1490 && op0_const_self
1491 && (code == TRUTH_ANDIF_EXPR
1492 ? op0 == truthvalue_false_node
1493 : op0 == truthvalue_true_node)))
1494 *maybe_const_itself &= op1_const_self;
1495 goto out;
1497 case COND_EXPR:
1498 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1499 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1500 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1501 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1503 STRIP_TYPE_NOPS (op0);
1504 c_disable_warnings (op0 == truthvalue_false_node);
1505 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1506 STRIP_TYPE_NOPS (op1);
1507 c_enable_warnings (op0 == truthvalue_false_node);
1509 c_disable_warnings (op0 == truthvalue_true_node);
1510 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1511 STRIP_TYPE_NOPS (op2);
1512 c_enable_warnings (op0 == truthvalue_true_node);
1514 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1515 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1516 else
1517 ret = fold (expr);
1518 *maybe_const_operands &= op0_const;
1519 *maybe_const_itself &= op0_const_self;
1520 if (!(flag_isoc99
1521 && op0_const
1522 && op0_const_self
1523 && op0 == truthvalue_false_node))
1524 *maybe_const_operands &= op1_const;
1525 if (!(op0_const
1526 && op0_const_self
1527 && op0 == truthvalue_false_node))
1528 *maybe_const_itself &= op1_const_self;
1529 if (!(flag_isoc99
1530 && op0_const
1531 && op0_const_self
1532 && op0 == truthvalue_true_node))
1533 *maybe_const_operands &= op2_const;
1534 if (!(op0_const
1535 && op0_const_self
1536 && op0 == truthvalue_true_node))
1537 *maybe_const_itself &= op2_const_self;
1538 goto out;
1540 case EXCESS_PRECISION_EXPR:
1541 /* Each case where an operand with excess precision may be
1542 encountered must remove the EXCESS_PRECISION_EXPR around
1543 inner operands and possibly put one around the whole
1544 expression or possibly convert to the semantic type (which
1545 c_fully_fold does); we cannot tell at this stage which is
1546 appropriate in any particular case. */
1547 gcc_unreachable ();
1549 default:
1550 /* Various codes may appear through folding built-in functions
1551 and their arguments. */
1552 goto out;
1555 out:
1556 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1557 have been done by this point, so remove them again. */
1558 nowarning |= TREE_NO_WARNING (ret);
1559 STRIP_TYPE_NOPS (ret);
1560 if (nowarning && !TREE_NO_WARNING (ret))
1562 if (!CAN_HAVE_LOCATION_P (ret))
1563 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1564 TREE_NO_WARNING (ret) = 1;
1566 if (ret != expr)
1567 protected_set_expr_location (ret, loc);
1568 return ret;
1571 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1572 return EXP. Otherwise, return either EXP or its known constant
1573 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1574 Is the BLKmode test appropriate? */
1576 tree
1577 decl_constant_value_for_optimization (tree exp)
1579 tree ret;
1581 /* This function is only used by C, for c_fully_fold and other
1582 optimization, and may not be correct for C++. */
1583 if (c_dialect_cxx ())
1584 gcc_unreachable ();
1586 if (!optimize
1587 || TREE_CODE (exp) != VAR_DECL
1588 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1589 || DECL_MODE (exp) == BLKmode)
1590 return exp;
1592 ret = decl_constant_value (exp);
1593 /* Avoid unwanted tree sharing between the initializer and current
1594 function's body where the tree can be modified e.g. by the
1595 gimplifier. */
1596 if (ret != exp && TREE_STATIC (exp))
1597 ret = unshare_expr (ret);
1598 return ret;
1601 /* Print a warning if a constant expression had overflow in folding.
1602 Invoke this function on every expression that the language
1603 requires to be a constant expression.
1604 Note the ANSI C standard says it is erroneous for a
1605 constant expression to overflow. */
1607 void
1608 constant_expression_warning (tree value)
1610 if (warn_overflow && pedantic
1611 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1612 || TREE_CODE (value) == FIXED_CST
1613 || TREE_CODE (value) == VECTOR_CST
1614 || TREE_CODE (value) == COMPLEX_CST)
1615 && TREE_OVERFLOW (value))
1616 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1619 /* The same as above but print an unconditional error. */
1620 void
1621 constant_expression_error (tree value)
1623 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1624 || TREE_CODE (value) == FIXED_CST
1625 || TREE_CODE (value) == VECTOR_CST
1626 || TREE_CODE (value) == COMPLEX_CST)
1627 && TREE_OVERFLOW (value))
1628 error ("overflow in constant expression");
1631 /* Print a warning if an expression had overflow in folding and its
1632 operands hadn't.
1634 Invoke this function on every expression that
1635 (1) appears in the source code, and
1636 (2) is a constant expression that overflowed, and
1637 (3) is not already checked by convert_and_check;
1638 however, do not invoke this function on operands of explicit casts
1639 or when the expression is the result of an operator and any operand
1640 already overflowed. */
1642 void
1643 overflow_warning (location_t loc, tree value)
1645 if (c_inhibit_evaluation_warnings != 0)
1646 return;
1648 switch (TREE_CODE (value))
1650 case INTEGER_CST:
1651 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1652 break;
1654 case REAL_CST:
1655 warning_at (loc, OPT_Woverflow,
1656 "floating point overflow in expression");
1657 break;
1659 case FIXED_CST:
1660 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1661 break;
1663 case VECTOR_CST:
1664 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1665 break;
1667 case COMPLEX_CST:
1668 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1669 warning_at (loc, OPT_Woverflow,
1670 "complex integer overflow in expression");
1671 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1672 warning_at (loc, OPT_Woverflow,
1673 "complex floating point overflow in expression");
1674 break;
1676 default:
1677 break;
1681 /* Warn about uses of logical || / && operator in a context where it
1682 is likely that the bitwise equivalent was intended by the
1683 programmer. We have seen an expression in which CODE is a binary
1684 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1685 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1686 void
1687 warn_logical_operator (location_t location, enum tree_code code, tree type,
1688 enum tree_code code_left, tree op_left,
1689 enum tree_code ARG_UNUSED (code_right), tree op_right)
1691 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1692 int in0_p, in1_p, in_p;
1693 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1694 bool strict_overflow_p = false;
1696 if (code != TRUTH_ANDIF_EXPR
1697 && code != TRUTH_AND_EXPR
1698 && code != TRUTH_ORIF_EXPR
1699 && code != TRUTH_OR_EXPR)
1700 return;
1702 /* We don't want to warn if either operand comes from a macro
1703 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1704 see PR61534. */
1705 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1706 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1707 return;
1709 /* Warn if &&/|| are being used in a context where it is
1710 likely that the bitwise equivalent was intended by the
1711 programmer. That is, an expression such as op && MASK
1712 where op should not be any boolean expression, nor a
1713 constant, and mask seems to be a non-boolean integer constant. */
1714 if (!truth_value_p (code_left)
1715 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1716 && !CONSTANT_CLASS_P (op_left)
1717 && !TREE_NO_WARNING (op_left)
1718 && TREE_CODE (op_right) == INTEGER_CST
1719 && !integer_zerop (op_right)
1720 && !integer_onep (op_right))
1722 if (or_op)
1723 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1724 " applied to non-boolean constant");
1725 else
1726 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1727 " applied to non-boolean constant");
1728 TREE_NO_WARNING (op_left) = true;
1729 return;
1732 /* We do not warn for constants because they are typical of macro
1733 expansions that test for features. */
1734 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1735 return;
1737 /* This warning only makes sense with logical operands. */
1738 if (!(truth_value_p (TREE_CODE (op_left))
1739 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1740 || !(truth_value_p (TREE_CODE (op_right))
1741 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1742 return;
1744 /* The range computations only work with scalars. */
1745 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1746 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1747 return;
1749 /* We first test whether either side separately is trivially true
1750 (with OR) or trivially false (with AND). If so, do not warn.
1751 This is a common idiom for testing ranges of data types in
1752 portable code. */
1753 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1754 if (!lhs)
1755 return;
1756 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1757 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1759 /* If this is an OR operation, invert both sides; now, the result
1760 should be always false to get a warning. */
1761 if (or_op)
1762 in0_p = !in0_p;
1764 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1765 if (tem && integer_zerop (tem))
1766 return;
1768 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1769 if (!rhs)
1770 return;
1771 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1772 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1774 /* If this is an OR operation, invert both sides; now, the result
1775 should be always false to get a warning. */
1776 if (or_op)
1777 in1_p = !in1_p;
1779 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1780 if (tem && integer_zerop (tem))
1781 return;
1783 /* If both expressions have the same operand, if we can merge the
1784 ranges, ... */
1785 if (operand_equal_p (lhs, rhs, 0)
1786 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1787 in1_p, low1, high1))
1789 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1790 /* ... and if the range test is always false, then warn. */
1791 if (tem && integer_zerop (tem))
1793 if (or_op)
1794 warning_at (location, OPT_Wlogical_op,
1795 "logical %<or%> of collectively exhaustive tests is "
1796 "always true");
1797 else
1798 warning_at (location, OPT_Wlogical_op,
1799 "logical %<and%> of mutually exclusive tests is "
1800 "always false");
1802 /* Or warn if the operands have exactly the same range, e.g.
1803 A > 0 && A > 0. */
1804 else if (low0 == low1 && high0 == high1)
1806 if (or_op)
1807 warning_at (location, OPT_Wlogical_op,
1808 "logical %<or%> of equal expressions");
1809 else
1810 warning_at (location, OPT_Wlogical_op,
1811 "logical %<and%> of equal expressions");
1816 /* Warn about logical not used on the left hand side operand of a comparison.
1817 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1818 Do not warn if RHS is of a boolean type. */
1820 void
1821 warn_logical_not_parentheses (location_t location, enum tree_code code,
1822 tree rhs)
1824 if (TREE_CODE_CLASS (code) != tcc_comparison
1825 || TREE_TYPE (rhs) == NULL_TREE
1826 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1827 return;
1829 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1830 !(x == 0) or !(y != 0). */
1831 if ((code == EQ_EXPR || code == NE_EXPR)
1832 && integer_zerop (rhs))
1833 return;
1835 warning_at (location, OPT_Wlogical_not_parentheses,
1836 "logical not is only applied to the left hand side of "
1837 "comparison");
1840 /* Warn if EXP contains any computations whose results are not used.
1841 Return true if a warning is printed; false otherwise. LOCUS is the
1842 (potential) location of the expression. */
1844 bool
1845 warn_if_unused_value (const_tree exp, location_t locus)
1847 restart:
1848 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1849 return false;
1851 /* Don't warn about void constructs. This includes casting to void,
1852 void function calls, and statement expressions with a final cast
1853 to void. */
1854 if (VOID_TYPE_P (TREE_TYPE (exp)))
1855 return false;
1857 if (EXPR_HAS_LOCATION (exp))
1858 locus = EXPR_LOCATION (exp);
1860 switch (TREE_CODE (exp))
1862 case PREINCREMENT_EXPR:
1863 case POSTINCREMENT_EXPR:
1864 case PREDECREMENT_EXPR:
1865 case POSTDECREMENT_EXPR:
1866 case MODIFY_EXPR:
1867 case INIT_EXPR:
1868 case TARGET_EXPR:
1869 case CALL_EXPR:
1870 case TRY_CATCH_EXPR:
1871 case WITH_CLEANUP_EXPR:
1872 case EXIT_EXPR:
1873 case VA_ARG_EXPR:
1874 return false;
1876 case BIND_EXPR:
1877 /* For a binding, warn if no side effect within it. */
1878 exp = BIND_EXPR_BODY (exp);
1879 goto restart;
1881 case SAVE_EXPR:
1882 case NON_LVALUE_EXPR:
1883 case NOP_EXPR:
1884 exp = TREE_OPERAND (exp, 0);
1885 goto restart;
1887 case TRUTH_ORIF_EXPR:
1888 case TRUTH_ANDIF_EXPR:
1889 /* In && or ||, warn if 2nd operand has no side effect. */
1890 exp = TREE_OPERAND (exp, 1);
1891 goto restart;
1893 case COMPOUND_EXPR:
1894 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1895 return true;
1896 /* Let people do `(foo (), 0)' without a warning. */
1897 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1898 return false;
1899 exp = TREE_OPERAND (exp, 1);
1900 goto restart;
1902 case COND_EXPR:
1903 /* If this is an expression with side effects, don't warn; this
1904 case commonly appears in macro expansions. */
1905 if (TREE_SIDE_EFFECTS (exp))
1906 return false;
1907 goto warn;
1909 case INDIRECT_REF:
1910 /* Don't warn about automatic dereferencing of references, since
1911 the user cannot control it. */
1912 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1914 exp = TREE_OPERAND (exp, 0);
1915 goto restart;
1917 /* Fall through. */
1919 default:
1920 /* Referencing a volatile value is a side effect, so don't warn. */
1921 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1922 && TREE_THIS_VOLATILE (exp))
1923 return false;
1925 /* If this is an expression which has no operands, there is no value
1926 to be unused. There are no such language-independent codes,
1927 but front ends may define such. */
1928 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1929 return false;
1931 warn:
1932 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1937 /* Print a warning about casts that might indicate violation
1938 of strict aliasing rules if -Wstrict-aliasing is used and
1939 strict aliasing mode is in effect. OTYPE is the original
1940 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1942 bool
1943 strict_aliasing_warning (tree otype, tree type, tree expr)
1945 /* Strip pointer conversion chains and get to the correct original type. */
1946 STRIP_NOPS (expr);
1947 otype = TREE_TYPE (expr);
1949 if (!(flag_strict_aliasing
1950 && POINTER_TYPE_P (type)
1951 && POINTER_TYPE_P (otype)
1952 && !VOID_TYPE_P (TREE_TYPE (type)))
1953 /* If the type we are casting to is a ref-all pointer
1954 dereferencing it is always valid. */
1955 || TYPE_REF_CAN_ALIAS_ALL (type))
1956 return false;
1958 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1959 && (DECL_P (TREE_OPERAND (expr, 0))
1960 || handled_component_p (TREE_OPERAND (expr, 0))))
1962 /* Casting the address of an object to non void pointer. Warn
1963 if the cast breaks type based aliasing. */
1964 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1966 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1967 "might break strict-aliasing rules");
1968 return true;
1970 else
1972 /* warn_strict_aliasing >= 3. This includes the default (3).
1973 Only warn if the cast is dereferenced immediately. */
1974 alias_set_type set1 =
1975 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1976 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1978 if (set1 != set2 && set2 != 0
1979 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1981 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1982 "pointer will break strict-aliasing rules");
1983 return true;
1985 else if (warn_strict_aliasing == 2
1986 && !alias_sets_must_conflict_p (set1, set2))
1988 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1989 "pointer might break strict-aliasing rules");
1990 return true;
1994 else
1995 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1997 /* At this level, warn for any conversions, even if an address is
1998 not taken in the same statement. This will likely produce many
1999 false positives, but could be useful to pinpoint problems that
2000 are not revealed at higher levels. */
2001 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2002 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2003 if (!COMPLETE_TYPE_P (type)
2004 || !alias_sets_must_conflict_p (set1, set2))
2006 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2007 "pointer might break strict-aliasing rules");
2008 return true;
2012 return false;
2015 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2016 sizeof as last operand of certain builtins. */
2018 void
2019 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2020 vec<tree, va_gc> *params, tree *sizeof_arg,
2021 bool (*comp_types) (tree, tree))
2023 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2024 bool strop = false, cmp = false;
2025 unsigned int idx = ~0;
2026 location_t loc;
2028 if (TREE_CODE (callee) != FUNCTION_DECL
2029 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2030 || vec_safe_length (params) <= 1)
2031 return;
2033 switch (DECL_FUNCTION_CODE (callee))
2035 case BUILT_IN_STRNCMP:
2036 case BUILT_IN_STRNCASECMP:
2037 cmp = true;
2038 /* FALLTHRU */
2039 case BUILT_IN_STRNCPY:
2040 case BUILT_IN_STRNCPY_CHK:
2041 case BUILT_IN_STRNCAT:
2042 case BUILT_IN_STRNCAT_CHK:
2043 case BUILT_IN_STPNCPY:
2044 case BUILT_IN_STPNCPY_CHK:
2045 strop = true;
2046 /* FALLTHRU */
2047 case BUILT_IN_MEMCPY:
2048 case BUILT_IN_MEMCPY_CHK:
2049 case BUILT_IN_MEMMOVE:
2050 case BUILT_IN_MEMMOVE_CHK:
2051 if (params->length () < 3)
2052 return;
2053 src = (*params)[1];
2054 dest = (*params)[0];
2055 idx = 2;
2056 break;
2057 case BUILT_IN_BCOPY:
2058 if (params->length () < 3)
2059 return;
2060 src = (*params)[0];
2061 dest = (*params)[1];
2062 idx = 2;
2063 break;
2064 case BUILT_IN_MEMCMP:
2065 case BUILT_IN_BCMP:
2066 if (params->length () < 3)
2067 return;
2068 src = (*params)[1];
2069 dest = (*params)[0];
2070 idx = 2;
2071 cmp = true;
2072 break;
2073 case BUILT_IN_MEMSET:
2074 case BUILT_IN_MEMSET_CHK:
2075 if (params->length () < 3)
2076 return;
2077 dest = (*params)[0];
2078 idx = 2;
2079 break;
2080 case BUILT_IN_BZERO:
2081 dest = (*params)[0];
2082 idx = 1;
2083 break;
2084 case BUILT_IN_STRNDUP:
2085 src = (*params)[0];
2086 strop = true;
2087 idx = 1;
2088 break;
2089 case BUILT_IN_MEMCHR:
2090 if (params->length () < 3)
2091 return;
2092 src = (*params)[0];
2093 idx = 2;
2094 break;
2095 case BUILT_IN_SNPRINTF:
2096 case BUILT_IN_SNPRINTF_CHK:
2097 case BUILT_IN_VSNPRINTF:
2098 case BUILT_IN_VSNPRINTF_CHK:
2099 dest = (*params)[0];
2100 idx = 1;
2101 strop = true;
2102 break;
2103 default:
2104 break;
2107 if (idx >= 3)
2108 return;
2110 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2111 return;
2113 type = TYPE_P (sizeof_arg[idx])
2114 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2115 if (!POINTER_TYPE_P (type))
2116 return;
2118 if (dest
2119 && (tem = tree_strip_nop_conversions (dest))
2120 && POINTER_TYPE_P (TREE_TYPE (tem))
2121 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2122 return;
2124 if (src
2125 && (tem = tree_strip_nop_conversions (src))
2126 && POINTER_TYPE_P (TREE_TYPE (tem))
2127 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2128 return;
2130 loc = sizeof_arg_loc[idx];
2132 if (dest && !cmp)
2134 if (!TYPE_P (sizeof_arg[idx])
2135 && operand_equal_p (dest, sizeof_arg[idx], 0)
2136 && comp_types (TREE_TYPE (dest), type))
2138 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2139 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2140 "argument to %<sizeof%> in %qD call is the same "
2141 "expression as the destination; did you mean to "
2142 "remove the addressof?", callee);
2143 else if ((TYPE_PRECISION (TREE_TYPE (type))
2144 == TYPE_PRECISION (char_type_node))
2145 || strop)
2146 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2147 "argument to %<sizeof%> in %qD call is the same "
2148 "expression as the destination; did you mean to "
2149 "provide an explicit length?", callee);
2150 else
2151 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2152 "argument to %<sizeof%> in %qD call is the same "
2153 "expression as the destination; did you mean to "
2154 "dereference it?", callee);
2155 return;
2158 if (POINTER_TYPE_P (TREE_TYPE (dest))
2159 && !strop
2160 && comp_types (TREE_TYPE (dest), type)
2161 && !VOID_TYPE_P (TREE_TYPE (type)))
2163 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2164 "argument to %<sizeof%> in %qD call is the same "
2165 "pointer type %qT as the destination; expected %qT "
2166 "or an explicit length", callee, TREE_TYPE (dest),
2167 TREE_TYPE (TREE_TYPE (dest)));
2168 return;
2172 if (src && !cmp)
2174 if (!TYPE_P (sizeof_arg[idx])
2175 && operand_equal_p (src, sizeof_arg[idx], 0)
2176 && comp_types (TREE_TYPE (src), type))
2178 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2179 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2180 "argument to %<sizeof%> in %qD call is the same "
2181 "expression as the source; did you mean to "
2182 "remove the addressof?", callee);
2183 else if ((TYPE_PRECISION (TREE_TYPE (type))
2184 == TYPE_PRECISION (char_type_node))
2185 || strop)
2186 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2187 "argument to %<sizeof%> in %qD call is the same "
2188 "expression as the source; did you mean to "
2189 "provide an explicit length?", callee);
2190 else
2191 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2192 "argument to %<sizeof%> in %qD call is the same "
2193 "expression as the source; did you mean to "
2194 "dereference it?", callee);
2195 return;
2198 if (POINTER_TYPE_P (TREE_TYPE (src))
2199 && !strop
2200 && comp_types (TREE_TYPE (src), type)
2201 && !VOID_TYPE_P (TREE_TYPE (type)))
2203 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2204 "argument to %<sizeof%> in %qD call is the same "
2205 "pointer type %qT as the source; expected %qT "
2206 "or an explicit length", callee, TREE_TYPE (src),
2207 TREE_TYPE (TREE_TYPE (src)));
2208 return;
2212 if (dest)
2214 if (!TYPE_P (sizeof_arg[idx])
2215 && operand_equal_p (dest, sizeof_arg[idx], 0)
2216 && comp_types (TREE_TYPE (dest), type))
2218 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2219 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2220 "argument to %<sizeof%> in %qD call is the same "
2221 "expression as the first source; did you mean to "
2222 "remove the addressof?", callee);
2223 else if ((TYPE_PRECISION (TREE_TYPE (type))
2224 == TYPE_PRECISION (char_type_node))
2225 || strop)
2226 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2227 "argument to %<sizeof%> in %qD call is the same "
2228 "expression as the first source; did you mean to "
2229 "provide an explicit length?", callee);
2230 else
2231 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2232 "argument to %<sizeof%> in %qD call is the same "
2233 "expression as the first source; did you mean to "
2234 "dereference it?", callee);
2235 return;
2238 if (POINTER_TYPE_P (TREE_TYPE (dest))
2239 && !strop
2240 && comp_types (TREE_TYPE (dest), type)
2241 && !VOID_TYPE_P (TREE_TYPE (type)))
2243 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2244 "argument to %<sizeof%> in %qD call is the same "
2245 "pointer type %qT as the first source; expected %qT "
2246 "or an explicit length", callee, TREE_TYPE (dest),
2247 TREE_TYPE (TREE_TYPE (dest)));
2248 return;
2252 if (src)
2254 if (!TYPE_P (sizeof_arg[idx])
2255 && operand_equal_p (src, sizeof_arg[idx], 0)
2256 && comp_types (TREE_TYPE (src), type))
2258 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2259 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2260 "argument to %<sizeof%> in %qD call is the same "
2261 "expression as the second source; did you mean to "
2262 "remove the addressof?", callee);
2263 else if ((TYPE_PRECISION (TREE_TYPE (type))
2264 == TYPE_PRECISION (char_type_node))
2265 || strop)
2266 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2267 "argument to %<sizeof%> in %qD call is the same "
2268 "expression as the second source; did you mean to "
2269 "provide an explicit length?", callee);
2270 else
2271 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2272 "argument to %<sizeof%> in %qD call is the same "
2273 "expression as the second source; did you mean to "
2274 "dereference it?", callee);
2275 return;
2278 if (POINTER_TYPE_P (TREE_TYPE (src))
2279 && !strop
2280 && comp_types (TREE_TYPE (src), type)
2281 && !VOID_TYPE_P (TREE_TYPE (type)))
2283 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2284 "argument to %<sizeof%> in %qD call is the same "
2285 "pointer type %qT as the second source; expected %qT "
2286 "or an explicit length", callee, TREE_TYPE (src),
2287 TREE_TYPE (TREE_TYPE (src)));
2288 return;
2294 /* Warn for unlikely, improbable, or stupid DECL declarations
2295 of `main'. */
2297 void
2298 check_main_parameter_types (tree decl)
2300 function_args_iterator iter;
2301 tree type;
2302 int argct = 0;
2304 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2306 /* XXX void_type_node belies the abstraction. */
2307 if (type == void_type_node || type == error_mark_node )
2308 break;
2310 tree t = type;
2311 if (TYPE_ATOMIC (t))
2312 pedwarn (input_location, OPT_Wmain,
2313 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2314 type, decl);
2315 while (POINTER_TYPE_P (t))
2317 t = TREE_TYPE (t);
2318 if (TYPE_ATOMIC (t))
2319 pedwarn (input_location, OPT_Wmain,
2320 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2321 type, decl);
2324 ++argct;
2325 switch (argct)
2327 case 1:
2328 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2329 pedwarn (input_location, OPT_Wmain,
2330 "first argument of %q+D should be %<int%>", decl);
2331 break;
2333 case 2:
2334 if (TREE_CODE (type) != POINTER_TYPE
2335 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2336 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2337 != char_type_node))
2338 pedwarn (input_location, OPT_Wmain,
2339 "second argument of %q+D should be %<char **%>", decl);
2340 break;
2342 case 3:
2343 if (TREE_CODE (type) != POINTER_TYPE
2344 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2345 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2346 != char_type_node))
2347 pedwarn (input_location, OPT_Wmain,
2348 "third argument of %q+D should probably be "
2349 "%<char **%>", decl);
2350 break;
2354 /* It is intentional that this message does not mention the third
2355 argument because it's only mentioned in an appendix of the
2356 standard. */
2357 if (argct > 0 && (argct < 2 || argct > 3))
2358 pedwarn (input_location, OPT_Wmain,
2359 "%q+D takes only zero or two arguments", decl);
2361 if (stdarg_p (TREE_TYPE (decl)))
2362 pedwarn (input_location, OPT_Wmain,
2363 "%q+D declared as variadic function", decl);
2366 /* vector_targets_convertible_p is used for vector pointer types. The
2367 callers perform various checks that the qualifiers are satisfactory,
2368 while OTOH vector_targets_convertible_p ignores the number of elements
2369 in the vectors. That's fine with vector pointers as we can consider,
2370 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2371 and that does not require and conversion of the pointer values.
2372 In contrast, vector_types_convertible_p and
2373 vector_types_compatible_elements_p are used for vector value types. */
2374 /* True if pointers to distinct types T1 and T2 can be converted to
2375 each other without an explicit cast. Only returns true for opaque
2376 vector types. */
2377 bool
2378 vector_targets_convertible_p (const_tree t1, const_tree t2)
2380 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2381 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2382 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2383 return true;
2385 return false;
2388 /* vector_types_convertible_p is used for vector value types.
2389 It could in principle call vector_targets_convertible_p as a subroutine,
2390 but then the check for vector type would be duplicated with its callers,
2391 and also the purpose of vector_targets_convertible_p would become
2392 muddled.
2393 Where vector_types_convertible_p returns true, a conversion might still be
2394 needed to make the types match.
2395 In contrast, vector_targets_convertible_p is used for vector pointer
2396 values, and vector_types_compatible_elements_p is used specifically
2397 in the context for binary operators, as a check if use is possible without
2398 conversion. */
2399 /* True if vector types T1 and T2 can be converted to each other
2400 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2401 can only be converted with -flax-vector-conversions yet that is not
2402 in effect, emit a note telling the user about that option if such
2403 a note has not previously been emitted. */
2404 bool
2405 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2407 static bool emitted_lax_note = false;
2408 bool convertible_lax;
2410 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2411 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2412 return true;
2414 convertible_lax =
2415 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2416 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2417 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2418 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2419 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2421 if (!convertible_lax || flag_lax_vector_conversions)
2422 return convertible_lax;
2424 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2425 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2426 return true;
2428 if (emit_lax_note && !emitted_lax_note)
2430 emitted_lax_note = true;
2431 inform (input_location, "use -flax-vector-conversions to permit "
2432 "conversions between vectors with differing "
2433 "element types or numbers of subparts");
2436 return false;
2439 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2440 and have vector types, V0 has the same type as V1, and the number of
2441 elements of V0, V1, MASK is the same.
2443 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2444 called with two arguments. In this case implementation passes the
2445 first argument twice in order to share the same tree code. This fact
2446 could enable the mask-values being twice the vector length. This is
2447 an implementation accident and this semantics is not guaranteed to
2448 the user. */
2449 tree
2450 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2451 bool complain)
2453 tree ret;
2454 bool wrap = true;
2455 bool maybe_const = false;
2456 bool two_arguments = false;
2458 if (v1 == NULL_TREE)
2460 two_arguments = true;
2461 v1 = v0;
2464 if (v0 == error_mark_node || v1 == error_mark_node
2465 || mask == error_mark_node)
2466 return error_mark_node;
2468 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2469 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2471 if (complain)
2472 error_at (loc, "__builtin_shuffle last argument must "
2473 "be an integer vector");
2474 return error_mark_node;
2477 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2478 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2480 if (complain)
2481 error_at (loc, "__builtin_shuffle arguments must be vectors");
2482 return error_mark_node;
2485 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2487 if (complain)
2488 error_at (loc, "__builtin_shuffle argument vectors must be of "
2489 "the same type");
2490 return error_mark_node;
2493 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2494 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2495 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2496 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2498 if (complain)
2499 error_at (loc, "__builtin_shuffle number of elements of the "
2500 "argument vector(s) and the mask vector should "
2501 "be the same");
2502 return error_mark_node;
2505 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2506 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2508 if (complain)
2509 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2510 "must have the same size as inner type of the mask");
2511 return error_mark_node;
2514 if (!c_dialect_cxx ())
2516 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2517 v0 = c_fully_fold (v0, false, &maybe_const);
2518 wrap &= maybe_const;
2520 if (two_arguments)
2521 v1 = v0 = save_expr (v0);
2522 else
2524 v1 = c_fully_fold (v1, false, &maybe_const);
2525 wrap &= maybe_const;
2528 mask = c_fully_fold (mask, false, &maybe_const);
2529 wrap &= maybe_const;
2531 else if (two_arguments)
2532 v1 = v0 = save_expr (v0);
2534 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2536 if (!c_dialect_cxx () && !wrap)
2537 ret = c_wrap_maybe_const (ret, true);
2539 return ret;
2542 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2543 to integral type. */
2545 static tree
2546 c_common_get_narrower (tree op, int *unsignedp_ptr)
2548 op = get_narrower (op, unsignedp_ptr);
2550 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2551 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2553 /* C++0x scoped enumerations don't implicitly convert to integral
2554 type; if we stripped an explicit conversion to a larger type we
2555 need to replace it so common_type will still work. */
2556 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2557 TYPE_UNSIGNED (TREE_TYPE (op)));
2558 op = fold_convert (type, op);
2560 return op;
2563 /* This is a helper function of build_binary_op.
2565 For certain operations if both args were extended from the same
2566 smaller type, do the arithmetic in that type and then extend.
2568 BITWISE indicates a bitwise operation.
2569 For them, this optimization is safe only if
2570 both args are zero-extended or both are sign-extended.
2571 Otherwise, we might change the result.
2572 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2573 but calculated in (unsigned short) it would be (unsigned short)-1.
2575 tree
2576 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2578 int unsigned0, unsigned1;
2579 tree arg0, arg1;
2580 int uns;
2581 tree type;
2583 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2584 excessive narrowing when we call get_narrower below. For
2585 example, suppose that OP0 is of unsigned int extended
2586 from signed char and that RESULT_TYPE is long long int.
2587 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2588 like
2590 (long long int) (unsigned int) signed_char
2592 which get_narrower would narrow down to
2594 (unsigned int) signed char
2596 If we do not cast OP0 first, get_narrower would return
2597 signed_char, which is inconsistent with the case of the
2598 explicit cast. */
2599 op0 = convert (result_type, op0);
2600 op1 = convert (result_type, op1);
2602 arg0 = c_common_get_narrower (op0, &unsigned0);
2603 arg1 = c_common_get_narrower (op1, &unsigned1);
2605 /* UNS is 1 if the operation to be done is an unsigned one. */
2606 uns = TYPE_UNSIGNED (result_type);
2608 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2609 but it *requires* conversion to FINAL_TYPE. */
2611 if ((TYPE_PRECISION (TREE_TYPE (op0))
2612 == TYPE_PRECISION (TREE_TYPE (arg0)))
2613 && TREE_TYPE (op0) != result_type)
2614 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2615 if ((TYPE_PRECISION (TREE_TYPE (op1))
2616 == TYPE_PRECISION (TREE_TYPE (arg1)))
2617 && TREE_TYPE (op1) != result_type)
2618 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2620 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2622 /* For bitwise operations, signedness of nominal type
2623 does not matter. Consider only how operands were extended. */
2624 if (bitwise)
2625 uns = unsigned0;
2627 /* Note that in all three cases below we refrain from optimizing
2628 an unsigned operation on sign-extended args.
2629 That would not be valid. */
2631 /* Both args variable: if both extended in same way
2632 from same width, do it in that width.
2633 Do it unsigned if args were zero-extended. */
2634 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2635 < TYPE_PRECISION (result_type))
2636 && (TYPE_PRECISION (TREE_TYPE (arg1))
2637 == TYPE_PRECISION (TREE_TYPE (arg0)))
2638 && unsigned0 == unsigned1
2639 && (unsigned0 || !uns))
2640 return c_common_signed_or_unsigned_type
2641 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2643 else if (TREE_CODE (arg0) == INTEGER_CST
2644 && (unsigned1 || !uns)
2645 && (TYPE_PRECISION (TREE_TYPE (arg1))
2646 < TYPE_PRECISION (result_type))
2647 && (type
2648 = c_common_signed_or_unsigned_type (unsigned1,
2649 TREE_TYPE (arg1)))
2650 && !POINTER_TYPE_P (type)
2651 && int_fits_type_p (arg0, type))
2652 return type;
2654 else if (TREE_CODE (arg1) == INTEGER_CST
2655 && (unsigned0 || !uns)
2656 && (TYPE_PRECISION (TREE_TYPE (arg0))
2657 < TYPE_PRECISION (result_type))
2658 && (type
2659 = c_common_signed_or_unsigned_type (unsigned0,
2660 TREE_TYPE (arg0)))
2661 && !POINTER_TYPE_P (type)
2662 && int_fits_type_p (arg1, type))
2663 return type;
2665 return result_type;
2668 /* Checks if expression EXPR of real/integer type cannot be converted
2669 to the real/integer type TYPE. Function returns non-zero when:
2670 * EXPR is a constant which cannot be exactly converted to TYPE.
2671 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2672 for EXPR type and TYPE being both integers or both real.
2673 * EXPR is not a constant of real type and TYPE is an integer.
2674 * EXPR is not a constant of integer type which cannot be
2675 exactly converted to real type.
2676 Function allows conversions between types of different signedness and
2677 can return SAFE_CONVERSION (zero) in that case. Function can produce
2678 signedness warnings if PRODUCE_WARNS is true. */
2680 enum conversion_safety
2681 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2683 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2684 tree expr_type = TREE_TYPE (expr);
2685 loc = expansion_point_location_if_in_system_header (loc);
2687 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2689 /* Warn for real constant that is not an exact integer converted
2690 to integer type. */
2691 if (TREE_CODE (expr_type) == REAL_TYPE
2692 && TREE_CODE (type) == INTEGER_TYPE)
2694 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2695 give_warning = UNSAFE_REAL;
2697 /* Warn for an integer constant that does not fit into integer type. */
2698 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2699 && TREE_CODE (type) == INTEGER_TYPE
2700 && !int_fits_type_p (expr, type))
2702 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2703 && tree_int_cst_sgn (expr) < 0)
2705 if (produce_warns)
2706 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2707 " implicitly converted to unsigned type");
2709 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2711 if (produce_warns)
2712 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2713 " constant value to negative integer");
2715 else
2716 give_warning = UNSAFE_OTHER;
2718 else if (TREE_CODE (type) == REAL_TYPE)
2720 /* Warn for an integer constant that does not fit into real type. */
2721 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2723 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2724 if (!exact_real_truncate (TYPE_MODE (type), &a))
2725 give_warning = UNSAFE_REAL;
2727 /* Warn for a real constant that does not fit into a smaller
2728 real type. */
2729 else if (TREE_CODE (expr_type) == REAL_TYPE
2730 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2732 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2733 if (!exact_real_truncate (TYPE_MODE (type), &a))
2734 give_warning = UNSAFE_REAL;
2738 else
2740 /* Warn for real types converted to integer types. */
2741 if (TREE_CODE (expr_type) == REAL_TYPE
2742 && TREE_CODE (type) == INTEGER_TYPE)
2743 give_warning = UNSAFE_REAL;
2745 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2746 && TREE_CODE (type) == INTEGER_TYPE)
2748 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2749 expr = get_unwidened (expr, 0);
2750 expr_type = TREE_TYPE (expr);
2752 /* Don't warn for short y; short x = ((int)y & 0xff); */
2753 if (TREE_CODE (expr) == BIT_AND_EXPR
2754 || TREE_CODE (expr) == BIT_IOR_EXPR
2755 || TREE_CODE (expr) == BIT_XOR_EXPR)
2757 /* If both args were extended from a shortest type,
2758 use that type if that is safe. */
2759 expr_type = shorten_binary_op (expr_type,
2760 TREE_OPERAND (expr, 0),
2761 TREE_OPERAND (expr, 1),
2762 /* bitwise */1);
2764 if (TREE_CODE (expr) == BIT_AND_EXPR)
2766 tree op0 = TREE_OPERAND (expr, 0);
2767 tree op1 = TREE_OPERAND (expr, 1);
2768 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2769 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2771 /* If one of the operands is a non-negative constant
2772 that fits in the target type, then the type of the
2773 other operand does not matter. */
2774 if ((TREE_CODE (op0) == INTEGER_CST
2775 && int_fits_type_p (op0, c_common_signed_type (type))
2776 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2777 || (TREE_CODE (op1) == INTEGER_CST
2778 && int_fits_type_p (op1, c_common_signed_type (type))
2779 && int_fits_type_p (op1,
2780 c_common_unsigned_type (type))))
2781 return SAFE_CONVERSION;
2782 /* If constant is unsigned and fits in the target
2783 type, then the result will also fit. */
2784 else if ((TREE_CODE (op0) == INTEGER_CST
2785 && unsigned0
2786 && int_fits_type_p (op0, type))
2787 || (TREE_CODE (op1) == INTEGER_CST
2788 && unsigned1
2789 && int_fits_type_p (op1, type)))
2790 return SAFE_CONVERSION;
2793 /* Warn for integer types converted to smaller integer types. */
2794 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2795 give_warning = UNSAFE_OTHER;
2797 /* When they are the same width but different signedness,
2798 then the value may change. */
2799 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2800 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2801 /* Even when converted to a bigger type, if the type is
2802 unsigned but expr is signed, then negative values
2803 will be changed. */
2804 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2805 && produce_warns)
2806 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2807 "may change the sign of the result",
2808 type, expr_type);
2811 /* Warn for integer types converted to real types if and only if
2812 all the range of values of the integer type cannot be
2813 represented by the real type. */
2814 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2815 && TREE_CODE (type) == REAL_TYPE)
2817 tree type_low_bound, type_high_bound;
2818 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2820 /* Don't warn about char y = 0xff; float x = (int) y; */
2821 expr = get_unwidened (expr, 0);
2822 expr_type = TREE_TYPE (expr);
2824 type_low_bound = TYPE_MIN_VALUE (expr_type);
2825 type_high_bound = TYPE_MAX_VALUE (expr_type);
2826 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2827 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2829 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2830 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2831 give_warning = UNSAFE_OTHER;
2834 /* Warn for real types converted to smaller real types. */
2835 else if (TREE_CODE (expr_type) == REAL_TYPE
2836 && TREE_CODE (type) == REAL_TYPE
2837 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2838 give_warning = UNSAFE_REAL;
2841 return give_warning;
2844 /* Warns if the conversion of EXPR to TYPE may alter a value.
2845 This is a helper function for warnings_for_convert_and_check. */
2847 static void
2848 conversion_warning (location_t loc, tree type, tree expr)
2850 tree expr_type = TREE_TYPE (expr);
2851 enum conversion_safety conversion_kind;
2853 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2854 return;
2856 /* This may happen, because for LHS op= RHS we preevaluate
2857 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2858 means we could no longer see the code of the EXPR. */
2859 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2860 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2861 if (TREE_CODE (expr) == SAVE_EXPR)
2862 expr = TREE_OPERAND (expr, 0);
2864 switch (TREE_CODE (expr))
2866 case EQ_EXPR:
2867 case NE_EXPR:
2868 case LE_EXPR:
2869 case GE_EXPR:
2870 case LT_EXPR:
2871 case GT_EXPR:
2872 case TRUTH_ANDIF_EXPR:
2873 case TRUTH_ORIF_EXPR:
2874 case TRUTH_AND_EXPR:
2875 case TRUTH_OR_EXPR:
2876 case TRUTH_XOR_EXPR:
2877 case TRUTH_NOT_EXPR:
2878 /* Conversion from boolean to a signed:1 bit-field (which only
2879 can hold the values 0 and -1) doesn't lose information - but
2880 it does change the value. */
2881 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2882 warning_at (loc, OPT_Wconversion,
2883 "conversion to %qT from boolean expression", type);
2884 return;
2886 case REAL_CST:
2887 case INTEGER_CST:
2888 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2889 if (conversion_kind == UNSAFE_REAL)
2890 warning_at (loc, OPT_Wfloat_conversion,
2891 "conversion to %qT alters %qT constant value",
2892 type, expr_type);
2893 else if (conversion_kind)
2894 warning_at (loc, OPT_Wconversion,
2895 "conversion to %qT alters %qT constant value",
2896 type, expr_type);
2897 return;
2899 case COND_EXPR:
2901 /* In case of COND_EXPR, we do not care about the type of
2902 COND_EXPR, only about the conversion of each operand. */
2903 tree op1 = TREE_OPERAND (expr, 1);
2904 tree op2 = TREE_OPERAND (expr, 2);
2906 conversion_warning (loc, type, op1);
2907 conversion_warning (loc, type, op2);
2908 return;
2911 default: /* 'expr' is not a constant. */
2912 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2913 if (conversion_kind == UNSAFE_REAL)
2914 warning_at (loc, OPT_Wfloat_conversion,
2915 "conversion to %qT from %qT may alter its value",
2916 type, expr_type);
2917 else if (conversion_kind)
2918 warning_at (loc, OPT_Wconversion,
2919 "conversion to %qT from %qT may alter its value",
2920 type, expr_type);
2924 /* Produce warnings after a conversion. RESULT is the result of
2925 converting EXPR to TYPE. This is a helper function for
2926 convert_and_check and cp_convert_and_check. */
2928 void
2929 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2930 tree result)
2932 loc = expansion_point_location_if_in_system_header (loc);
2934 if (TREE_CODE (expr) == INTEGER_CST
2935 && (TREE_CODE (type) == INTEGER_TYPE
2936 || TREE_CODE (type) == ENUMERAL_TYPE)
2937 && !int_fits_type_p (expr, type))
2939 /* Do not diagnose overflow in a constant expression merely
2940 because a conversion overflowed. */
2941 if (TREE_OVERFLOW (result))
2942 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2944 if (TYPE_UNSIGNED (type))
2946 /* This detects cases like converting -129 or 256 to
2947 unsigned char. */
2948 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2949 warning_at (loc, OPT_Woverflow,
2950 "large integer implicitly truncated to unsigned type");
2951 else
2952 conversion_warning (loc, type, expr);
2954 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2955 warning_at (loc, OPT_Woverflow,
2956 "overflow in implicit constant conversion");
2957 /* No warning for converting 0x80000000 to int. */
2958 else if (pedantic
2959 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2960 || TYPE_PRECISION (TREE_TYPE (expr))
2961 != TYPE_PRECISION (type)))
2962 warning_at (loc, OPT_Woverflow,
2963 "overflow in implicit constant conversion");
2965 else
2966 conversion_warning (loc, type, expr);
2968 else if ((TREE_CODE (result) == INTEGER_CST
2969 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2970 warning_at (loc, OPT_Woverflow,
2971 "overflow in implicit constant conversion");
2972 else
2973 conversion_warning (loc, type, expr);
2977 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2978 Invoke this function on every expression that is converted implicitly,
2979 i.e. because of language rules and not because of an explicit cast. */
2981 tree
2982 convert_and_check (location_t loc, tree type, tree expr)
2984 tree result;
2985 tree expr_for_warning;
2987 /* Convert from a value with possible excess precision rather than
2988 via the semantic type, but do not warn about values not fitting
2989 exactly in the semantic type. */
2990 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2992 tree orig_type = TREE_TYPE (expr);
2993 expr = TREE_OPERAND (expr, 0);
2994 expr_for_warning = convert (orig_type, expr);
2995 if (orig_type == type)
2996 return expr_for_warning;
2998 else
2999 expr_for_warning = expr;
3001 if (TREE_TYPE (expr) == type)
3002 return expr;
3004 result = convert (type, expr);
3006 if (c_inhibit_evaluation_warnings == 0
3007 && !TREE_OVERFLOW_P (expr)
3008 && result != error_mark_node)
3009 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3011 return result;
3014 /* A node in a list that describes references to variables (EXPR), which are
3015 either read accesses if WRITER is zero, or write accesses, in which case
3016 WRITER is the parent of EXPR. */
3017 struct tlist
3019 struct tlist *next;
3020 tree expr, writer;
3023 /* Used to implement a cache the results of a call to verify_tree. We only
3024 use this for SAVE_EXPRs. */
3025 struct tlist_cache
3027 struct tlist_cache *next;
3028 struct tlist *cache_before_sp;
3029 struct tlist *cache_after_sp;
3030 tree expr;
3033 /* Obstack to use when allocating tlist structures, and corresponding
3034 firstobj. */
3035 static struct obstack tlist_obstack;
3036 static char *tlist_firstobj = 0;
3038 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3039 warnings. */
3040 static struct tlist *warned_ids;
3041 /* SAVE_EXPRs need special treatment. We process them only once and then
3042 cache the results. */
3043 static struct tlist_cache *save_expr_cache;
3045 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3046 static void merge_tlist (struct tlist **, struct tlist *, int);
3047 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3048 static int warning_candidate_p (tree);
3049 static bool candidate_equal_p (const_tree, const_tree);
3050 static void warn_for_collisions (struct tlist *);
3051 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3052 static struct tlist *new_tlist (struct tlist *, tree, tree);
3054 /* Create a new struct tlist and fill in its fields. */
3055 static struct tlist *
3056 new_tlist (struct tlist *next, tree t, tree writer)
3058 struct tlist *l;
3059 l = XOBNEW (&tlist_obstack, struct tlist);
3060 l->next = next;
3061 l->expr = t;
3062 l->writer = writer;
3063 return l;
3066 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3067 is nonnull, we ignore any node we find which has a writer equal to it. */
3069 static void
3070 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3072 while (add)
3074 struct tlist *next = add->next;
3075 if (!copy)
3076 add->next = *to;
3077 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3078 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3079 add = next;
3083 /* Merge the nodes of ADD into TO. This merging process is done so that for
3084 each variable that already exists in TO, no new node is added; however if
3085 there is a write access recorded in ADD, and an occurrence on TO is only
3086 a read access, then the occurrence in TO will be modified to record the
3087 write. */
3089 static void
3090 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3092 struct tlist **end = to;
3094 while (*end)
3095 end = &(*end)->next;
3097 while (add)
3099 int found = 0;
3100 struct tlist *tmp2;
3101 struct tlist *next = add->next;
3103 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3104 if (candidate_equal_p (tmp2->expr, add->expr))
3106 found = 1;
3107 if (!tmp2->writer)
3108 tmp2->writer = add->writer;
3110 if (!found)
3112 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3113 end = &(*end)->next;
3114 *end = 0;
3116 add = next;
3120 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3121 references in list LIST conflict with it, excluding reads if ONLY writers
3122 is nonzero. */
3124 static void
3125 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3126 int only_writes)
3128 struct tlist *tmp;
3130 /* Avoid duplicate warnings. */
3131 for (tmp = warned_ids; tmp; tmp = tmp->next)
3132 if (candidate_equal_p (tmp->expr, written))
3133 return;
3135 while (list)
3137 if (candidate_equal_p (list->expr, written)
3138 && !candidate_equal_p (list->writer, writer)
3139 && (!only_writes || list->writer))
3141 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3142 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3143 OPT_Wsequence_point, "operation on %qE may be undefined",
3144 list->expr);
3146 list = list->next;
3150 /* Given a list LIST of references to variables, find whether any of these
3151 can cause conflicts due to missing sequence points. */
3153 static void
3154 warn_for_collisions (struct tlist *list)
3156 struct tlist *tmp;
3158 for (tmp = list; tmp; tmp = tmp->next)
3160 if (tmp->writer)
3161 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3165 /* Return nonzero if X is a tree that can be verified by the sequence point
3166 warnings. */
3167 static int
3168 warning_candidate_p (tree x)
3170 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3171 return 0;
3173 if (TREE_CODE (x) == BLOCK)
3174 return 0;
3176 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3177 (lvalue_p) crash on TRY/CATCH. */
3178 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3179 return 0;
3181 if (!lvalue_p (x))
3182 return 0;
3184 /* No point to track non-const calls, they will never satisfy
3185 operand_equal_p. */
3186 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3187 return 0;
3189 if (TREE_CODE (x) == STRING_CST)
3190 return 0;
3192 return 1;
3195 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3196 static bool
3197 candidate_equal_p (const_tree x, const_tree y)
3199 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3202 /* Walk the tree X, and record accesses to variables. If X is written by the
3203 parent tree, WRITER is the parent.
3204 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3205 expression or its only operand forces a sequence point, then everything up
3206 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3207 in PNO_SP.
3208 Once we return, we will have emitted warnings if any subexpression before
3209 such a sequence point could be undefined. On a higher level, however, the
3210 sequence point may not be relevant, and we'll merge the two lists.
3212 Example: (b++, a) + b;
3213 The call that processes the COMPOUND_EXPR will store the increment of B
3214 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3215 processes the PLUS_EXPR will need to merge the two lists so that
3216 eventually, all accesses end up on the same list (and we'll warn about the
3217 unordered subexpressions b++ and b.
3219 A note on merging. If we modify the former example so that our expression
3220 becomes
3221 (b++, b) + a
3222 care must be taken not simply to add all three expressions into the final
3223 PNO_SP list. The function merge_tlist takes care of that by merging the
3224 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3225 way, so that no more than one access to B is recorded. */
3227 static void
3228 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3229 tree writer)
3231 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3232 enum tree_code code;
3233 enum tree_code_class cl;
3235 /* X may be NULL if it is the operand of an empty statement expression
3236 ({ }). */
3237 if (x == NULL)
3238 return;
3240 restart:
3241 code = TREE_CODE (x);
3242 cl = TREE_CODE_CLASS (code);
3244 if (warning_candidate_p (x))
3245 *pno_sp = new_tlist (*pno_sp, x, writer);
3247 switch (code)
3249 case CONSTRUCTOR:
3250 case SIZEOF_EXPR:
3251 return;
3253 case COMPOUND_EXPR:
3254 case TRUTH_ANDIF_EXPR:
3255 case TRUTH_ORIF_EXPR:
3256 tmp_before = tmp_nosp = tmp_list3 = 0;
3257 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3258 warn_for_collisions (tmp_nosp);
3259 merge_tlist (pbefore_sp, tmp_before, 0);
3260 merge_tlist (pbefore_sp, tmp_nosp, 0);
3261 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3262 merge_tlist (pbefore_sp, tmp_list3, 0);
3263 return;
3265 case COND_EXPR:
3266 tmp_before = tmp_list2 = 0;
3267 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3268 warn_for_collisions (tmp_list2);
3269 merge_tlist (pbefore_sp, tmp_before, 0);
3270 merge_tlist (pbefore_sp, tmp_list2, 0);
3272 tmp_list3 = tmp_nosp = 0;
3273 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3274 warn_for_collisions (tmp_nosp);
3275 merge_tlist (pbefore_sp, tmp_list3, 0);
3277 tmp_list3 = tmp_list2 = 0;
3278 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3279 warn_for_collisions (tmp_list2);
3280 merge_tlist (pbefore_sp, tmp_list3, 0);
3281 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3282 two first, to avoid warning for (a ? b++ : b++). */
3283 merge_tlist (&tmp_nosp, tmp_list2, 0);
3284 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3285 return;
3287 case PREDECREMENT_EXPR:
3288 case PREINCREMENT_EXPR:
3289 case POSTDECREMENT_EXPR:
3290 case POSTINCREMENT_EXPR:
3291 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3292 return;
3294 case MODIFY_EXPR:
3295 tmp_before = tmp_nosp = tmp_list3 = 0;
3296 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3297 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3298 /* Expressions inside the LHS are not ordered wrt. the sequence points
3299 in the RHS. Example:
3300 *a = (a++, 2)
3301 Despite the fact that the modification of "a" is in the before_sp
3302 list (tmp_before), it conflicts with the use of "a" in the LHS.
3303 We can handle this by adding the contents of tmp_list3
3304 to those of tmp_before, and redoing the collision warnings for that
3305 list. */
3306 add_tlist (&tmp_before, tmp_list3, x, 1);
3307 warn_for_collisions (tmp_before);
3308 /* Exclude the LHS itself here; we first have to merge it into the
3309 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3310 didn't exclude the LHS, we'd get it twice, once as a read and once
3311 as a write. */
3312 add_tlist (pno_sp, tmp_list3, x, 0);
3313 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3315 merge_tlist (pbefore_sp, tmp_before, 0);
3316 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3317 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3318 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3319 return;
3321 case CALL_EXPR:
3322 /* We need to warn about conflicts among arguments and conflicts between
3323 args and the function address. Side effects of the function address,
3324 however, are not ordered by the sequence point of the call. */
3326 call_expr_arg_iterator iter;
3327 tree arg;
3328 tmp_before = tmp_nosp = 0;
3329 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3330 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3332 tmp_list2 = tmp_list3 = 0;
3333 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3334 merge_tlist (&tmp_list3, tmp_list2, 0);
3335 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3337 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3338 warn_for_collisions (tmp_before);
3339 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3340 return;
3343 case TREE_LIST:
3344 /* Scan all the list, e.g. indices of multi dimensional array. */
3345 while (x)
3347 tmp_before = tmp_nosp = 0;
3348 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3349 merge_tlist (&tmp_nosp, tmp_before, 0);
3350 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3351 x = TREE_CHAIN (x);
3353 return;
3355 case SAVE_EXPR:
3357 struct tlist_cache *t;
3358 for (t = save_expr_cache; t; t = t->next)
3359 if (candidate_equal_p (t->expr, x))
3360 break;
3362 if (!t)
3364 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3365 t->next = save_expr_cache;
3366 t->expr = x;
3367 save_expr_cache = t;
3369 tmp_before = tmp_nosp = 0;
3370 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3371 warn_for_collisions (tmp_nosp);
3373 tmp_list3 = 0;
3374 merge_tlist (&tmp_list3, tmp_nosp, 0);
3375 t->cache_before_sp = tmp_before;
3376 t->cache_after_sp = tmp_list3;
3378 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3379 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3380 return;
3383 case ADDR_EXPR:
3384 x = TREE_OPERAND (x, 0);
3385 if (DECL_P (x))
3386 return;
3387 writer = 0;
3388 goto restart;
3390 default:
3391 /* For other expressions, simply recurse on their operands.
3392 Manual tail recursion for unary expressions.
3393 Other non-expressions need not be processed. */
3394 if (cl == tcc_unary)
3396 x = TREE_OPERAND (x, 0);
3397 writer = 0;
3398 goto restart;
3400 else if (IS_EXPR_CODE_CLASS (cl))
3402 int lp;
3403 int max = TREE_OPERAND_LENGTH (x);
3404 for (lp = 0; lp < max; lp++)
3406 tmp_before = tmp_nosp = 0;
3407 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3408 merge_tlist (&tmp_nosp, tmp_before, 0);
3409 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3412 return;
3416 /* Try to warn for undefined behavior in EXPR due to missing sequence
3417 points. */
3419 DEBUG_FUNCTION void
3420 verify_sequence_points (tree expr)
3422 struct tlist *before_sp = 0, *after_sp = 0;
3424 warned_ids = 0;
3425 save_expr_cache = 0;
3426 if (tlist_firstobj == 0)
3428 gcc_obstack_init (&tlist_obstack);
3429 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3432 verify_tree (expr, &before_sp, &after_sp, 0);
3433 warn_for_collisions (after_sp);
3434 obstack_free (&tlist_obstack, tlist_firstobj);
3437 /* Validate the expression after `case' and apply default promotions. */
3439 static tree
3440 check_case_value (location_t loc, tree value)
3442 if (value == NULL_TREE)
3443 return value;
3445 if (TREE_CODE (value) == INTEGER_CST)
3446 /* Promote char or short to int. */
3447 value = perform_integral_promotions (value);
3448 else if (value != error_mark_node)
3450 error_at (loc, "case label does not reduce to an integer constant");
3451 value = error_mark_node;
3454 constant_expression_warning (value);
3456 return value;
3459 /* See if the case values LOW and HIGH are in the range of the original
3460 type (i.e. before the default conversion to int) of the switch testing
3461 expression.
3462 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3463 the type before promoting it. CASE_LOW_P is a pointer to the lower
3464 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3465 if the case is not a case range.
3466 The caller has to make sure that we are not called with NULL for
3467 CASE_LOW_P (i.e. the default case).
3468 Returns true if the case label is in range of ORIG_TYPE (saturated or
3469 untouched) or false if the label is out of range. */
3471 static bool
3472 check_case_bounds (location_t loc, tree type, tree orig_type,
3473 tree *case_low_p, tree *case_high_p)
3475 tree min_value, max_value;
3476 tree case_low = *case_low_p;
3477 tree case_high = case_high_p ? *case_high_p : case_low;
3479 /* If there was a problem with the original type, do nothing. */
3480 if (orig_type == error_mark_node)
3481 return true;
3483 min_value = TYPE_MIN_VALUE (orig_type);
3484 max_value = TYPE_MAX_VALUE (orig_type);
3486 /* Case label is less than minimum for type. */
3487 if (tree_int_cst_compare (case_low, min_value) < 0
3488 && tree_int_cst_compare (case_high, min_value) < 0)
3490 warning_at (loc, 0, "case label value is less than minimum value "
3491 "for type");
3492 return false;
3495 /* Case value is greater than maximum for type. */
3496 if (tree_int_cst_compare (case_low, max_value) > 0
3497 && tree_int_cst_compare (case_high, max_value) > 0)
3499 warning_at (loc, 0, "case label value exceeds maximum value for type");
3500 return false;
3503 /* Saturate lower case label value to minimum. */
3504 if (tree_int_cst_compare (case_high, min_value) >= 0
3505 && tree_int_cst_compare (case_low, min_value) < 0)
3507 warning_at (loc, 0, "lower value in case label range"
3508 " less than minimum value for type");
3509 case_low = min_value;
3512 /* Saturate upper case label value to maximum. */
3513 if (tree_int_cst_compare (case_low, max_value) <= 0
3514 && tree_int_cst_compare (case_high, max_value) > 0)
3516 warning_at (loc, 0, "upper value in case label range"
3517 " exceeds maximum value for type");
3518 case_high = max_value;
3521 if (*case_low_p != case_low)
3522 *case_low_p = convert (type, case_low);
3523 if (case_high_p && *case_high_p != case_high)
3524 *case_high_p = convert (type, case_high);
3526 return true;
3529 /* Return an integer type with BITS bits of precision,
3530 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3532 tree
3533 c_common_type_for_size (unsigned int bits, int unsignedp)
3535 int i;
3537 if (bits == TYPE_PRECISION (integer_type_node))
3538 return unsignedp ? unsigned_type_node : integer_type_node;
3540 if (bits == TYPE_PRECISION (signed_char_type_node))
3541 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3543 if (bits == TYPE_PRECISION (short_integer_type_node))
3544 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3546 if (bits == TYPE_PRECISION (long_integer_type_node))
3547 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3549 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3550 return (unsignedp ? long_long_unsigned_type_node
3551 : long_long_integer_type_node);
3553 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3554 if (int_n_enabled_p[i]
3555 && bits == int_n_data[i].bitsize)
3556 return (unsignedp ? int_n_trees[i].unsigned_type
3557 : int_n_trees[i].signed_type);
3559 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3560 return (unsignedp ? widest_unsigned_literal_type_node
3561 : widest_integer_literal_type_node);
3563 if (bits <= TYPE_PRECISION (intQI_type_node))
3564 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3566 if (bits <= TYPE_PRECISION (intHI_type_node))
3567 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3569 if (bits <= TYPE_PRECISION (intSI_type_node))
3570 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3572 if (bits <= TYPE_PRECISION (intDI_type_node))
3573 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3575 return 0;
3578 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3579 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3580 and saturating if SATP is nonzero, otherwise not saturating. */
3582 tree
3583 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3584 int unsignedp, int satp)
3586 machine_mode mode;
3587 if (ibit == 0)
3588 mode = unsignedp ? UQQmode : QQmode;
3589 else
3590 mode = unsignedp ? UHAmode : HAmode;
3592 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3593 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3594 break;
3596 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3598 sorry ("GCC cannot support operators with integer types and "
3599 "fixed-point types that have too many integral and "
3600 "fractional bits together");
3601 return 0;
3604 return c_common_type_for_mode (mode, satp);
3607 /* Used for communication between c_common_type_for_mode and
3608 c_register_builtin_type. */
3609 tree registered_builtin_types;
3611 /* Return a data type that has machine mode MODE.
3612 If the mode is an integer,
3613 then UNSIGNEDP selects between signed and unsigned types.
3614 If the mode is a fixed-point mode,
3615 then UNSIGNEDP selects between saturating and nonsaturating types. */
3617 tree
3618 c_common_type_for_mode (machine_mode mode, int unsignedp)
3620 tree t;
3621 int i;
3623 if (mode == TYPE_MODE (integer_type_node))
3624 return unsignedp ? unsigned_type_node : integer_type_node;
3626 if (mode == TYPE_MODE (signed_char_type_node))
3627 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3629 if (mode == TYPE_MODE (short_integer_type_node))
3630 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3632 if (mode == TYPE_MODE (long_integer_type_node))
3633 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3635 if (mode == TYPE_MODE (long_long_integer_type_node))
3636 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3638 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3639 if (int_n_enabled_p[i]
3640 && mode == int_n_data[i].m)
3641 return (unsignedp ? int_n_trees[i].unsigned_type
3642 : int_n_trees[i].signed_type);
3644 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3645 return unsignedp ? widest_unsigned_literal_type_node
3646 : widest_integer_literal_type_node;
3648 if (mode == QImode)
3649 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3651 if (mode == HImode)
3652 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3654 if (mode == SImode)
3655 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3657 if (mode == DImode)
3658 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3660 #if HOST_BITS_PER_WIDE_INT >= 64
3661 if (mode == TYPE_MODE (intTI_type_node))
3662 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3663 #endif
3665 if (mode == TYPE_MODE (float_type_node))
3666 return float_type_node;
3668 if (mode == TYPE_MODE (double_type_node))
3669 return double_type_node;
3671 if (mode == TYPE_MODE (long_double_type_node))
3672 return long_double_type_node;
3674 if (mode == TYPE_MODE (void_type_node))
3675 return void_type_node;
3677 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3678 return (unsignedp
3679 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3680 : make_signed_type (GET_MODE_PRECISION (mode)));
3682 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3683 return (unsignedp
3684 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3685 : make_signed_type (GET_MODE_PRECISION (mode)));
3687 if (COMPLEX_MODE_P (mode))
3689 machine_mode inner_mode;
3690 tree inner_type;
3692 if (mode == TYPE_MODE (complex_float_type_node))
3693 return complex_float_type_node;
3694 if (mode == TYPE_MODE (complex_double_type_node))
3695 return complex_double_type_node;
3696 if (mode == TYPE_MODE (complex_long_double_type_node))
3697 return complex_long_double_type_node;
3699 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3700 return complex_integer_type_node;
3702 inner_mode = GET_MODE_INNER (mode);
3703 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3704 if (inner_type != NULL_TREE)
3705 return build_complex_type (inner_type);
3707 else if (VECTOR_MODE_P (mode))
3709 machine_mode inner_mode = GET_MODE_INNER (mode);
3710 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3711 if (inner_type != NULL_TREE)
3712 return build_vector_type_for_mode (inner_type, mode);
3715 if (mode == TYPE_MODE (dfloat32_type_node))
3716 return dfloat32_type_node;
3717 if (mode == TYPE_MODE (dfloat64_type_node))
3718 return dfloat64_type_node;
3719 if (mode == TYPE_MODE (dfloat128_type_node))
3720 return dfloat128_type_node;
3722 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3724 if (mode == TYPE_MODE (short_fract_type_node))
3725 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3726 if (mode == TYPE_MODE (fract_type_node))
3727 return unsignedp ? sat_fract_type_node : fract_type_node;
3728 if (mode == TYPE_MODE (long_fract_type_node))
3729 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3730 if (mode == TYPE_MODE (long_long_fract_type_node))
3731 return unsignedp ? sat_long_long_fract_type_node
3732 : long_long_fract_type_node;
3734 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3735 return unsignedp ? sat_unsigned_short_fract_type_node
3736 : unsigned_short_fract_type_node;
3737 if (mode == TYPE_MODE (unsigned_fract_type_node))
3738 return unsignedp ? sat_unsigned_fract_type_node
3739 : unsigned_fract_type_node;
3740 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3741 return unsignedp ? sat_unsigned_long_fract_type_node
3742 : unsigned_long_fract_type_node;
3743 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3744 return unsignedp ? sat_unsigned_long_long_fract_type_node
3745 : unsigned_long_long_fract_type_node;
3747 if (mode == TYPE_MODE (short_accum_type_node))
3748 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3749 if (mode == TYPE_MODE (accum_type_node))
3750 return unsignedp ? sat_accum_type_node : accum_type_node;
3751 if (mode == TYPE_MODE (long_accum_type_node))
3752 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3753 if (mode == TYPE_MODE (long_long_accum_type_node))
3754 return unsignedp ? sat_long_long_accum_type_node
3755 : long_long_accum_type_node;
3757 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3758 return unsignedp ? sat_unsigned_short_accum_type_node
3759 : unsigned_short_accum_type_node;
3760 if (mode == TYPE_MODE (unsigned_accum_type_node))
3761 return unsignedp ? sat_unsigned_accum_type_node
3762 : unsigned_accum_type_node;
3763 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3764 return unsignedp ? sat_unsigned_long_accum_type_node
3765 : unsigned_long_accum_type_node;
3766 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3767 return unsignedp ? sat_unsigned_long_long_accum_type_node
3768 : unsigned_long_long_accum_type_node;
3770 if (mode == QQmode)
3771 return unsignedp ? sat_qq_type_node : qq_type_node;
3772 if (mode == HQmode)
3773 return unsignedp ? sat_hq_type_node : hq_type_node;
3774 if (mode == SQmode)
3775 return unsignedp ? sat_sq_type_node : sq_type_node;
3776 if (mode == DQmode)
3777 return unsignedp ? sat_dq_type_node : dq_type_node;
3778 if (mode == TQmode)
3779 return unsignedp ? sat_tq_type_node : tq_type_node;
3781 if (mode == UQQmode)
3782 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3783 if (mode == UHQmode)
3784 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3785 if (mode == USQmode)
3786 return unsignedp ? sat_usq_type_node : usq_type_node;
3787 if (mode == UDQmode)
3788 return unsignedp ? sat_udq_type_node : udq_type_node;
3789 if (mode == UTQmode)
3790 return unsignedp ? sat_utq_type_node : utq_type_node;
3792 if (mode == HAmode)
3793 return unsignedp ? sat_ha_type_node : ha_type_node;
3794 if (mode == SAmode)
3795 return unsignedp ? sat_sa_type_node : sa_type_node;
3796 if (mode == DAmode)
3797 return unsignedp ? sat_da_type_node : da_type_node;
3798 if (mode == TAmode)
3799 return unsignedp ? sat_ta_type_node : ta_type_node;
3801 if (mode == UHAmode)
3802 return unsignedp ? sat_uha_type_node : uha_type_node;
3803 if (mode == USAmode)
3804 return unsignedp ? sat_usa_type_node : usa_type_node;
3805 if (mode == UDAmode)
3806 return unsignedp ? sat_uda_type_node : uda_type_node;
3807 if (mode == UTAmode)
3808 return unsignedp ? sat_uta_type_node : uta_type_node;
3811 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3812 if (TYPE_MODE (TREE_VALUE (t)) == mode
3813 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3814 return TREE_VALUE (t);
3816 return 0;
3819 tree
3820 c_common_unsigned_type (tree type)
3822 return c_common_signed_or_unsigned_type (1, type);
3825 /* Return a signed type the same as TYPE in other respects. */
3827 tree
3828 c_common_signed_type (tree type)
3830 return c_common_signed_or_unsigned_type (0, type);
3833 /* Return a type the same as TYPE except unsigned or
3834 signed according to UNSIGNEDP. */
3836 tree
3837 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3839 tree type1;
3840 int i;
3842 /* This block of code emulates the behavior of the old
3843 c_common_unsigned_type. In particular, it returns
3844 long_unsigned_type_node if passed a long, even when a int would
3845 have the same size. This is necessary for warnings to work
3846 correctly in archs where sizeof(int) == sizeof(long) */
3848 type1 = TYPE_MAIN_VARIANT (type);
3849 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3850 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3851 if (type1 == integer_type_node || type1 == unsigned_type_node)
3852 return unsignedp ? unsigned_type_node : integer_type_node;
3853 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3854 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3855 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3856 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3857 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3858 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3860 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3861 if (int_n_enabled_p[i]
3862 && (type1 == int_n_trees[i].unsigned_type
3863 || type1 == int_n_trees[i].signed_type))
3864 return (unsignedp ? int_n_trees[i].unsigned_type
3865 : int_n_trees[i].signed_type);
3867 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3868 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3869 #if HOST_BITS_PER_WIDE_INT >= 64
3870 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3871 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3872 #endif
3873 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3874 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3875 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3876 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3877 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3878 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3879 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3880 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3882 #define C_COMMON_FIXED_TYPES(NAME) \
3883 if (type1 == short_ ## NAME ## _type_node \
3884 || type1 == unsigned_short_ ## NAME ## _type_node) \
3885 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3886 : short_ ## NAME ## _type_node; \
3887 if (type1 == NAME ## _type_node \
3888 || type1 == unsigned_ ## NAME ## _type_node) \
3889 return unsignedp ? unsigned_ ## NAME ## _type_node \
3890 : NAME ## _type_node; \
3891 if (type1 == long_ ## NAME ## _type_node \
3892 || type1 == unsigned_long_ ## NAME ## _type_node) \
3893 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3894 : long_ ## NAME ## _type_node; \
3895 if (type1 == long_long_ ## NAME ## _type_node \
3896 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3897 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3898 : long_long_ ## NAME ## _type_node;
3900 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3901 if (type1 == NAME ## _type_node \
3902 || type1 == u ## NAME ## _type_node) \
3903 return unsignedp ? u ## NAME ## _type_node \
3904 : NAME ## _type_node;
3906 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3907 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3908 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3909 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3910 : sat_ ## short_ ## NAME ## _type_node; \
3911 if (type1 == sat_ ## NAME ## _type_node \
3912 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3913 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3914 : sat_ ## NAME ## _type_node; \
3915 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3916 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3917 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3918 : sat_ ## long_ ## NAME ## _type_node; \
3919 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3920 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3921 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3922 : sat_ ## long_long_ ## NAME ## _type_node;
3924 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3925 if (type1 == sat_ ## NAME ## _type_node \
3926 || type1 == sat_ ## u ## NAME ## _type_node) \
3927 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3928 : sat_ ## NAME ## _type_node;
3930 C_COMMON_FIXED_TYPES (fract);
3931 C_COMMON_FIXED_TYPES_SAT (fract);
3932 C_COMMON_FIXED_TYPES (accum);
3933 C_COMMON_FIXED_TYPES_SAT (accum);
3935 C_COMMON_FIXED_MODE_TYPES (qq);
3936 C_COMMON_FIXED_MODE_TYPES (hq);
3937 C_COMMON_FIXED_MODE_TYPES (sq);
3938 C_COMMON_FIXED_MODE_TYPES (dq);
3939 C_COMMON_FIXED_MODE_TYPES (tq);
3940 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3941 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3942 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3943 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3944 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3945 C_COMMON_FIXED_MODE_TYPES (ha);
3946 C_COMMON_FIXED_MODE_TYPES (sa);
3947 C_COMMON_FIXED_MODE_TYPES (da);
3948 C_COMMON_FIXED_MODE_TYPES (ta);
3949 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3950 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3951 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3952 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3954 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3955 the precision; they have precision set to match their range, but
3956 may use a wider mode to match an ABI. If we change modes, we may
3957 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3958 the precision as well, so as to yield correct results for
3959 bit-field types. C++ does not have these separate bit-field
3960 types, and producing a signed or unsigned variant of an
3961 ENUMERAL_TYPE may cause other problems as well. */
3963 if (!INTEGRAL_TYPE_P (type)
3964 || TYPE_UNSIGNED (type) == unsignedp)
3965 return type;
3967 #define TYPE_OK(node) \
3968 (TYPE_MODE (type) == TYPE_MODE (node) \
3969 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3970 if (TYPE_OK (signed_char_type_node))
3971 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3972 if (TYPE_OK (integer_type_node))
3973 return unsignedp ? unsigned_type_node : integer_type_node;
3974 if (TYPE_OK (short_integer_type_node))
3975 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3976 if (TYPE_OK (long_integer_type_node))
3977 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3978 if (TYPE_OK (long_long_integer_type_node))
3979 return (unsignedp ? long_long_unsigned_type_node
3980 : long_long_integer_type_node);
3982 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3983 if (int_n_enabled_p[i]
3984 && TYPE_MODE (type) == int_n_data[i].m
3985 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3986 return (unsignedp ? int_n_trees[i].unsigned_type
3987 : int_n_trees[i].signed_type);
3989 if (TYPE_OK (widest_integer_literal_type_node))
3990 return (unsignedp ? widest_unsigned_literal_type_node
3991 : widest_integer_literal_type_node);
3993 #if HOST_BITS_PER_WIDE_INT >= 64
3994 if (TYPE_OK (intTI_type_node))
3995 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3996 #endif
3997 if (TYPE_OK (intDI_type_node))
3998 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3999 if (TYPE_OK (intSI_type_node))
4000 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4001 if (TYPE_OK (intHI_type_node))
4002 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4003 if (TYPE_OK (intQI_type_node))
4004 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4005 #undef TYPE_OK
4007 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4010 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4012 tree
4013 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4015 int i;
4017 /* Extended integer types of the same width as a standard type have
4018 lesser rank, so those of the same width as int promote to int or
4019 unsigned int and are valid for printf formats expecting int or
4020 unsigned int. To avoid such special cases, avoid creating
4021 extended integer types for bit-fields if a standard integer type
4022 is available. */
4023 if (width == TYPE_PRECISION (integer_type_node))
4024 return unsignedp ? unsigned_type_node : integer_type_node;
4025 if (width == TYPE_PRECISION (signed_char_type_node))
4026 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4027 if (width == TYPE_PRECISION (short_integer_type_node))
4028 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4029 if (width == TYPE_PRECISION (long_integer_type_node))
4030 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4031 if (width == TYPE_PRECISION (long_long_integer_type_node))
4032 return (unsignedp ? long_long_unsigned_type_node
4033 : long_long_integer_type_node);
4034 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4035 if (int_n_enabled_p[i]
4036 && width == int_n_data[i].bitsize)
4037 return (unsignedp ? int_n_trees[i].unsigned_type
4038 : int_n_trees[i].signed_type);
4039 return build_nonstandard_integer_type (width, unsignedp);
4042 /* The C version of the register_builtin_type langhook. */
4044 void
4045 c_register_builtin_type (tree type, const char* name)
4047 tree decl;
4049 decl = build_decl (UNKNOWN_LOCATION,
4050 TYPE_DECL, get_identifier (name), type);
4051 DECL_ARTIFICIAL (decl) = 1;
4052 if (!TYPE_NAME (type))
4053 TYPE_NAME (type) = decl;
4054 pushdecl (decl);
4056 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4059 /* Print an error message for invalid operands to arith operation
4060 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4061 LOCATION is the location of the message. */
4063 void
4064 binary_op_error (location_t location, enum tree_code code,
4065 tree type0, tree type1)
4067 const char *opname;
4069 switch (code)
4071 case PLUS_EXPR:
4072 opname = "+"; break;
4073 case MINUS_EXPR:
4074 opname = "-"; break;
4075 case MULT_EXPR:
4076 opname = "*"; break;
4077 case MAX_EXPR:
4078 opname = "max"; break;
4079 case MIN_EXPR:
4080 opname = "min"; break;
4081 case EQ_EXPR:
4082 opname = "=="; break;
4083 case NE_EXPR:
4084 opname = "!="; break;
4085 case LE_EXPR:
4086 opname = "<="; break;
4087 case GE_EXPR:
4088 opname = ">="; break;
4089 case LT_EXPR:
4090 opname = "<"; break;
4091 case GT_EXPR:
4092 opname = ">"; break;
4093 case LSHIFT_EXPR:
4094 opname = "<<"; break;
4095 case RSHIFT_EXPR:
4096 opname = ">>"; break;
4097 case TRUNC_MOD_EXPR:
4098 case FLOOR_MOD_EXPR:
4099 opname = "%"; break;
4100 case TRUNC_DIV_EXPR:
4101 case FLOOR_DIV_EXPR:
4102 opname = "/"; break;
4103 case BIT_AND_EXPR:
4104 opname = "&"; break;
4105 case BIT_IOR_EXPR:
4106 opname = "|"; break;
4107 case TRUTH_ANDIF_EXPR:
4108 opname = "&&"; break;
4109 case TRUTH_ORIF_EXPR:
4110 opname = "||"; break;
4111 case BIT_XOR_EXPR:
4112 opname = "^"; break;
4113 default:
4114 gcc_unreachable ();
4116 error_at (location,
4117 "invalid operands to binary %s (have %qT and %qT)", opname,
4118 type0, type1);
4121 /* Given an expression as a tree, return its original type. Do this
4122 by stripping any conversion that preserves the sign and precision. */
4123 static tree
4124 expr_original_type (tree expr)
4126 STRIP_SIGN_NOPS (expr);
4127 return TREE_TYPE (expr);
4130 /* Subroutine of build_binary_op, used for comparison operations.
4131 See if the operands have both been converted from subword integer types
4132 and, if so, perhaps change them both back to their original type.
4133 This function is also responsible for converting the two operands
4134 to the proper common type for comparison.
4136 The arguments of this function are all pointers to local variables
4137 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4138 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4140 LOC is the location of the comparison.
4142 If this function returns nonzero, it means that the comparison has
4143 a constant value. What this function returns is an expression for
4144 that value. */
4146 tree
4147 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4148 tree *restype_ptr, enum tree_code *rescode_ptr)
4150 tree type;
4151 tree op0 = *op0_ptr;
4152 tree op1 = *op1_ptr;
4153 int unsignedp0, unsignedp1;
4154 int real1, real2;
4155 tree primop0, primop1;
4156 enum tree_code code = *rescode_ptr;
4158 /* Throw away any conversions to wider types
4159 already present in the operands. */
4161 primop0 = c_common_get_narrower (op0, &unsignedp0);
4162 primop1 = c_common_get_narrower (op1, &unsignedp1);
4164 /* If primopN is first sign-extended from primopN's precision to opN's
4165 precision, then zero-extended from opN's precision to
4166 *restype_ptr precision, shortenings might be invalid. */
4167 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4168 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4169 && !unsignedp0
4170 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4171 primop0 = op0;
4172 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4173 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4174 && !unsignedp1
4175 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4176 primop1 = op1;
4178 /* Handle the case that OP0 does not *contain* a conversion
4179 but it *requires* conversion to FINAL_TYPE. */
4181 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4182 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4183 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4184 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4186 /* If one of the operands must be floated, we cannot optimize. */
4187 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4188 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4190 /* If first arg is constant, swap the args (changing operation
4191 so value is preserved), for canonicalization. Don't do this if
4192 the second arg is 0. */
4194 if (TREE_CONSTANT (primop0)
4195 && !integer_zerop (primop1) && !real_zerop (primop1)
4196 && !fixed_zerop (primop1))
4198 tree tem = primop0;
4199 int temi = unsignedp0;
4200 primop0 = primop1;
4201 primop1 = tem;
4202 tem = op0;
4203 op0 = op1;
4204 op1 = tem;
4205 *op0_ptr = op0;
4206 *op1_ptr = op1;
4207 unsignedp0 = unsignedp1;
4208 unsignedp1 = temi;
4209 temi = real1;
4210 real1 = real2;
4211 real2 = temi;
4213 switch (code)
4215 case LT_EXPR:
4216 code = GT_EXPR;
4217 break;
4218 case GT_EXPR:
4219 code = LT_EXPR;
4220 break;
4221 case LE_EXPR:
4222 code = GE_EXPR;
4223 break;
4224 case GE_EXPR:
4225 code = LE_EXPR;
4226 break;
4227 default:
4228 break;
4230 *rescode_ptr = code;
4233 /* If comparing an integer against a constant more bits wide,
4234 maybe we can deduce a value of 1 or 0 independent of the data.
4235 Or else truncate the constant now
4236 rather than extend the variable at run time.
4238 This is only interesting if the constant is the wider arg.
4239 Also, it is not safe if the constant is unsigned and the
4240 variable arg is signed, since in this case the variable
4241 would be sign-extended and then regarded as unsigned.
4242 Our technique fails in this case because the lowest/highest
4243 possible unsigned results don't follow naturally from the
4244 lowest/highest possible values of the variable operand.
4245 For just EQ_EXPR and NE_EXPR there is another technique that
4246 could be used: see if the constant can be faithfully represented
4247 in the other operand's type, by truncating it and reextending it
4248 and see if that preserves the constant's value. */
4250 if (!real1 && !real2
4251 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4252 && TREE_CODE (primop1) == INTEGER_CST
4253 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4255 int min_gt, max_gt, min_lt, max_lt;
4256 tree maxval, minval;
4257 /* 1 if comparison is nominally unsigned. */
4258 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4259 tree val;
4261 type = c_common_signed_or_unsigned_type (unsignedp0,
4262 TREE_TYPE (primop0));
4264 maxval = TYPE_MAX_VALUE (type);
4265 minval = TYPE_MIN_VALUE (type);
4267 if (unsignedp && !unsignedp0)
4268 *restype_ptr = c_common_signed_type (*restype_ptr);
4270 if (TREE_TYPE (primop1) != *restype_ptr)
4272 /* Convert primop1 to target type, but do not introduce
4273 additional overflow. We know primop1 is an int_cst. */
4274 primop1 = force_fit_type (*restype_ptr,
4275 wide_int::from
4276 (primop1,
4277 TYPE_PRECISION (*restype_ptr),
4278 TYPE_SIGN (TREE_TYPE (primop1))),
4279 0, TREE_OVERFLOW (primop1));
4281 if (type != *restype_ptr)
4283 minval = convert (*restype_ptr, minval);
4284 maxval = convert (*restype_ptr, maxval);
4287 min_gt = tree_int_cst_lt (primop1, minval);
4288 max_gt = tree_int_cst_lt (primop1, maxval);
4289 min_lt = tree_int_cst_lt (minval, primop1);
4290 max_lt = tree_int_cst_lt (maxval, primop1);
4292 val = 0;
4293 /* This used to be a switch, but Genix compiler can't handle that. */
4294 if (code == NE_EXPR)
4296 if (max_lt || min_gt)
4297 val = truthvalue_true_node;
4299 else if (code == EQ_EXPR)
4301 if (max_lt || min_gt)
4302 val = truthvalue_false_node;
4304 else if (code == LT_EXPR)
4306 if (max_lt)
4307 val = truthvalue_true_node;
4308 if (!min_lt)
4309 val = truthvalue_false_node;
4311 else if (code == GT_EXPR)
4313 if (min_gt)
4314 val = truthvalue_true_node;
4315 if (!max_gt)
4316 val = truthvalue_false_node;
4318 else if (code == LE_EXPR)
4320 if (!max_gt)
4321 val = truthvalue_true_node;
4322 if (min_gt)
4323 val = truthvalue_false_node;
4325 else if (code == GE_EXPR)
4327 if (!min_lt)
4328 val = truthvalue_true_node;
4329 if (max_lt)
4330 val = truthvalue_false_node;
4333 /* If primop0 was sign-extended and unsigned comparison specd,
4334 we did a signed comparison above using the signed type bounds.
4335 But the comparison we output must be unsigned.
4337 Also, for inequalities, VAL is no good; but if the signed
4338 comparison had *any* fixed result, it follows that the
4339 unsigned comparison just tests the sign in reverse
4340 (positive values are LE, negative ones GE).
4341 So we can generate an unsigned comparison
4342 against an extreme value of the signed type. */
4344 if (unsignedp && !unsignedp0)
4346 if (val != 0)
4347 switch (code)
4349 case LT_EXPR:
4350 case GE_EXPR:
4351 primop1 = TYPE_MIN_VALUE (type);
4352 val = 0;
4353 break;
4355 case LE_EXPR:
4356 case GT_EXPR:
4357 primop1 = TYPE_MAX_VALUE (type);
4358 val = 0;
4359 break;
4361 default:
4362 break;
4364 type = c_common_unsigned_type (type);
4367 if (TREE_CODE (primop0) != INTEGER_CST)
4369 if (val == truthvalue_false_node)
4370 warning_at (loc, OPT_Wtype_limits,
4371 "comparison is always false due to limited range of data type");
4372 if (val == truthvalue_true_node)
4373 warning_at (loc, OPT_Wtype_limits,
4374 "comparison is always true due to limited range of data type");
4377 if (val != 0)
4379 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4380 if (TREE_SIDE_EFFECTS (primop0))
4381 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4382 return val;
4385 /* Value is not predetermined, but do the comparison
4386 in the type of the operand that is not constant.
4387 TYPE is already properly set. */
4390 /* If either arg is decimal float and the other is float, find the
4391 proper common type to use for comparison. */
4392 else if (real1 && real2
4393 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4394 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4395 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4397 /* If either arg is decimal float and the other is float, fail. */
4398 else if (real1 && real2
4399 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4400 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4401 return 0;
4403 else if (real1 && real2
4404 && (TYPE_PRECISION (TREE_TYPE (primop0))
4405 == TYPE_PRECISION (TREE_TYPE (primop1))))
4406 type = TREE_TYPE (primop0);
4408 /* If args' natural types are both narrower than nominal type
4409 and both extend in the same manner, compare them
4410 in the type of the wider arg.
4411 Otherwise must actually extend both to the nominal
4412 common type lest different ways of extending
4413 alter the result.
4414 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4416 else if (unsignedp0 == unsignedp1 && real1 == real2
4417 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4418 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4420 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4421 type = c_common_signed_or_unsigned_type (unsignedp0
4422 || TYPE_UNSIGNED (*restype_ptr),
4423 type);
4424 /* Make sure shorter operand is extended the right way
4425 to match the longer operand. */
4426 primop0
4427 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4428 TREE_TYPE (primop0)),
4429 primop0);
4430 primop1
4431 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4432 TREE_TYPE (primop1)),
4433 primop1);
4435 else
4437 /* Here we must do the comparison on the nominal type
4438 using the args exactly as we received them. */
4439 type = *restype_ptr;
4440 primop0 = op0;
4441 primop1 = op1;
4443 if (!real1 && !real2 && integer_zerop (primop1)
4444 && TYPE_UNSIGNED (*restype_ptr))
4446 tree value = 0;
4447 /* All unsigned values are >= 0, so we warn. However,
4448 if OP0 is a constant that is >= 0, the signedness of
4449 the comparison isn't an issue, so suppress the
4450 warning. */
4451 bool warn =
4452 warn_type_limits && !in_system_header_at (loc)
4453 && !(TREE_CODE (primop0) == INTEGER_CST
4454 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4455 primop0)))
4456 /* Do not warn for enumeration types. */
4457 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4459 switch (code)
4461 case GE_EXPR:
4462 if (warn)
4463 warning_at (loc, OPT_Wtype_limits,
4464 "comparison of unsigned expression >= 0 is always true");
4465 value = truthvalue_true_node;
4466 break;
4468 case LT_EXPR:
4469 if (warn)
4470 warning_at (loc, OPT_Wtype_limits,
4471 "comparison of unsigned expression < 0 is always false");
4472 value = truthvalue_false_node;
4473 break;
4475 default:
4476 break;
4479 if (value != 0)
4481 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4482 if (TREE_SIDE_EFFECTS (primop0))
4483 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4484 primop0, value);
4485 return value;
4490 *op0_ptr = convert (type, primop0);
4491 *op1_ptr = convert (type, primop1);
4493 *restype_ptr = truthvalue_type_node;
4495 return 0;
4498 /* Return a tree for the sum or difference (RESULTCODE says which)
4499 of pointer PTROP and integer INTOP. */
4501 tree
4502 pointer_int_sum (location_t loc, enum tree_code resultcode,
4503 tree ptrop, tree intop, bool complain)
4505 tree size_exp, ret;
4507 /* The result is a pointer of the same type that is being added. */
4508 tree result_type = TREE_TYPE (ptrop);
4510 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4512 if (complain && warn_pointer_arith)
4513 pedwarn (loc, OPT_Wpointer_arith,
4514 "pointer of type %<void *%> used in arithmetic");
4515 else if (!complain)
4516 return error_mark_node;
4517 size_exp = integer_one_node;
4519 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4521 if (complain && warn_pointer_arith)
4522 pedwarn (loc, OPT_Wpointer_arith,
4523 "pointer to a function used in arithmetic");
4524 else if (!complain)
4525 return error_mark_node;
4526 size_exp = integer_one_node;
4528 else
4529 size_exp = size_in_bytes (TREE_TYPE (result_type));
4531 /* We are manipulating pointer values, so we don't need to warn
4532 about relying on undefined signed overflow. We disable the
4533 warning here because we use integer types so fold won't know that
4534 they are really pointers. */
4535 fold_defer_overflow_warnings ();
4537 /* If what we are about to multiply by the size of the elements
4538 contains a constant term, apply distributive law
4539 and multiply that constant term separately.
4540 This helps produce common subexpressions. */
4541 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4542 && !TREE_CONSTANT (intop)
4543 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4544 && TREE_CONSTANT (size_exp)
4545 /* If the constant comes from pointer subtraction,
4546 skip this optimization--it would cause an error. */
4547 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4548 /* If the constant is unsigned, and smaller than the pointer size,
4549 then we must skip this optimization. This is because it could cause
4550 an overflow error if the constant is negative but INTOP is not. */
4551 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4552 || (TYPE_PRECISION (TREE_TYPE (intop))
4553 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4555 enum tree_code subcode = resultcode;
4556 tree int_type = TREE_TYPE (intop);
4557 if (TREE_CODE (intop) == MINUS_EXPR)
4558 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4559 /* Convert both subexpression types to the type of intop,
4560 because weird cases involving pointer arithmetic
4561 can result in a sum or difference with different type args. */
4562 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4563 subcode, ptrop,
4564 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4565 intop = convert (int_type, TREE_OPERAND (intop, 0));
4568 /* Convert the integer argument to a type the same size as sizetype
4569 so the multiply won't overflow spuriously. */
4570 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4571 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4572 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4573 TYPE_UNSIGNED (sizetype)), intop);
4575 /* Replace the integer argument with a suitable product by the object size.
4576 Do this multiplication as signed, then convert to the appropriate type
4577 for the pointer operation and disregard an overflow that occurred only
4578 because of the sign-extension change in the latter conversion. */
4580 tree t = build_binary_op (loc,
4581 MULT_EXPR, intop,
4582 convert (TREE_TYPE (intop), size_exp), 1);
4583 intop = convert (sizetype, t);
4584 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4585 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4588 /* Create the sum or difference. */
4589 if (resultcode == MINUS_EXPR)
4590 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4592 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4594 fold_undefer_and_ignore_overflow_warnings ();
4596 return ret;
4599 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4600 and if NON_CONST is known not to be permitted in an evaluated part
4601 of a constant expression. */
4603 tree
4604 c_wrap_maybe_const (tree expr, bool non_const)
4606 bool nowarning = TREE_NO_WARNING (expr);
4607 location_t loc = EXPR_LOCATION (expr);
4609 /* This should never be called for C++. */
4610 if (c_dialect_cxx ())
4611 gcc_unreachable ();
4613 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4614 STRIP_TYPE_NOPS (expr);
4615 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4616 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4617 if (nowarning)
4618 TREE_NO_WARNING (expr) = 1;
4619 protected_set_expr_location (expr, loc);
4621 return expr;
4624 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4625 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4626 around the SAVE_EXPR if needed so that c_fully_fold does not need
4627 to look inside SAVE_EXPRs. */
4629 tree
4630 c_save_expr (tree expr)
4632 bool maybe_const = true;
4633 if (c_dialect_cxx ())
4634 return save_expr (expr);
4635 expr = c_fully_fold (expr, false, &maybe_const);
4636 expr = save_expr (expr);
4637 if (!maybe_const)
4638 expr = c_wrap_maybe_const (expr, true);
4639 return expr;
4642 /* Return whether EXPR is a declaration whose address can never be
4643 NULL. */
4645 bool
4646 decl_with_nonnull_addr_p (const_tree expr)
4648 return (DECL_P (expr)
4649 && (TREE_CODE (expr) == PARM_DECL
4650 || TREE_CODE (expr) == LABEL_DECL
4651 || !DECL_WEAK (expr)));
4654 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4655 or for an `if' or `while' statement or ?..: exp. It should already
4656 have been validated to be of suitable type; otherwise, a bad
4657 diagnostic may result.
4659 The EXPR is located at LOCATION.
4661 This preparation consists of taking the ordinary
4662 representation of an expression expr and producing a valid tree
4663 boolean expression describing whether expr is nonzero. We could
4664 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4665 but we optimize comparisons, &&, ||, and !.
4667 The resulting type should always be `truthvalue_type_node'. */
4669 tree
4670 c_common_truthvalue_conversion (location_t location, tree expr)
4672 switch (TREE_CODE (expr))
4674 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4675 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4676 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4677 case ORDERED_EXPR: case UNORDERED_EXPR:
4678 if (TREE_TYPE (expr) == truthvalue_type_node)
4679 return expr;
4680 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4681 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4682 goto ret;
4684 case TRUTH_ANDIF_EXPR:
4685 case TRUTH_ORIF_EXPR:
4686 case TRUTH_AND_EXPR:
4687 case TRUTH_OR_EXPR:
4688 case TRUTH_XOR_EXPR:
4689 if (TREE_TYPE (expr) == truthvalue_type_node)
4690 return expr;
4691 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4692 c_common_truthvalue_conversion (location,
4693 TREE_OPERAND (expr, 0)),
4694 c_common_truthvalue_conversion (location,
4695 TREE_OPERAND (expr, 1)));
4696 goto ret;
4698 case TRUTH_NOT_EXPR:
4699 if (TREE_TYPE (expr) == truthvalue_type_node)
4700 return expr;
4701 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4702 c_common_truthvalue_conversion (location,
4703 TREE_OPERAND (expr, 0)));
4704 goto ret;
4706 case ERROR_MARK:
4707 return expr;
4709 case INTEGER_CST:
4710 return integer_zerop (expr) ? truthvalue_false_node
4711 : truthvalue_true_node;
4713 case REAL_CST:
4714 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4715 ? truthvalue_true_node
4716 : truthvalue_false_node;
4718 case FIXED_CST:
4719 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4720 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4721 ? truthvalue_true_node
4722 : truthvalue_false_node;
4724 case FUNCTION_DECL:
4725 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4726 /* Fall through. */
4728 case ADDR_EXPR:
4730 tree inner = TREE_OPERAND (expr, 0);
4731 if (decl_with_nonnull_addr_p (inner))
4733 /* Common Ada/Pascal programmer's mistake. */
4734 warning_at (location,
4735 OPT_Waddress,
4736 "the address of %qD will always evaluate as %<true%>",
4737 inner);
4738 return truthvalue_true_node;
4740 break;
4743 case COMPLEX_EXPR:
4744 expr = build_binary_op (EXPR_LOCATION (expr),
4745 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4746 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4747 c_common_truthvalue_conversion (location,
4748 TREE_OPERAND (expr, 0)),
4749 c_common_truthvalue_conversion (location,
4750 TREE_OPERAND (expr, 1)),
4752 goto ret;
4754 case NEGATE_EXPR:
4755 case ABS_EXPR:
4756 case FLOAT_EXPR:
4757 case EXCESS_PRECISION_EXPR:
4758 /* These don't change whether an object is nonzero or zero. */
4759 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4761 case LROTATE_EXPR:
4762 case RROTATE_EXPR:
4763 /* These don't change whether an object is zero or nonzero, but
4764 we can't ignore them if their second arg has side-effects. */
4765 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4767 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4768 TREE_OPERAND (expr, 1),
4769 c_common_truthvalue_conversion
4770 (location, TREE_OPERAND (expr, 0)));
4771 goto ret;
4773 else
4774 return c_common_truthvalue_conversion (location,
4775 TREE_OPERAND (expr, 0));
4777 case COND_EXPR:
4778 /* Distribute the conversion into the arms of a COND_EXPR. */
4779 if (c_dialect_cxx ())
4781 tree op1 = TREE_OPERAND (expr, 1);
4782 tree op2 = TREE_OPERAND (expr, 2);
4783 /* In C++ one of the arms might have void type if it is throw. */
4784 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4785 op1 = c_common_truthvalue_conversion (location, op1);
4786 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4787 op2 = c_common_truthvalue_conversion (location, op2);
4788 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4789 TREE_OPERAND (expr, 0), op1, op2);
4790 goto ret;
4792 else
4794 /* Folding will happen later for C. */
4795 expr = build3 (COND_EXPR, truthvalue_type_node,
4796 TREE_OPERAND (expr, 0),
4797 c_common_truthvalue_conversion (location,
4798 TREE_OPERAND (expr, 1)),
4799 c_common_truthvalue_conversion (location,
4800 TREE_OPERAND (expr, 2)));
4801 goto ret;
4804 CASE_CONVERT:
4806 tree totype = TREE_TYPE (expr);
4807 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4809 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4810 since that affects how `default_conversion' will behave. */
4811 if (TREE_CODE (totype) == REFERENCE_TYPE
4812 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4813 break;
4814 /* Don't strip a conversion from C++0x scoped enum, since they
4815 don't implicitly convert to other types. */
4816 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4817 && ENUM_IS_SCOPED (fromtype))
4818 break;
4819 /* If this isn't narrowing the argument, we can ignore it. */
4820 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4821 return c_common_truthvalue_conversion (location,
4822 TREE_OPERAND (expr, 0));
4824 break;
4826 case MODIFY_EXPR:
4827 if (!TREE_NO_WARNING (expr)
4828 && warn_parentheses)
4830 warning (OPT_Wparentheses,
4831 "suggest parentheses around assignment used as truth value");
4832 TREE_NO_WARNING (expr) = 1;
4834 break;
4836 default:
4837 break;
4840 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4842 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4843 expr = (build_binary_op
4844 (EXPR_LOCATION (expr),
4845 (TREE_SIDE_EFFECTS (expr)
4846 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4847 c_common_truthvalue_conversion
4848 (location,
4849 build_unary_op (location, REALPART_EXPR, t, 0)),
4850 c_common_truthvalue_conversion
4851 (location,
4852 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4853 0));
4854 goto ret;
4857 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4859 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4860 FCONST0 (TYPE_MODE
4861 (TREE_TYPE (expr))));
4862 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4864 else
4865 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4867 ret:
4868 protected_set_expr_location (expr, location);
4869 return expr;
4872 static void def_builtin_1 (enum built_in_function fncode,
4873 const char *name,
4874 enum built_in_class fnclass,
4875 tree fntype, tree libtype,
4876 bool both_p, bool fallback_p, bool nonansi_p,
4877 tree fnattrs, bool implicit_p);
4880 /* Apply the TYPE_QUALS to the new DECL. */
4882 void
4883 c_apply_type_quals_to_decl (int type_quals, tree decl)
4885 tree type = TREE_TYPE (decl);
4887 if (type == error_mark_node)
4888 return;
4890 if ((type_quals & TYPE_QUAL_CONST)
4891 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4892 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4893 constructor can produce constant init, so rely on cp_finish_decl to
4894 clear TREE_READONLY if the variable has non-constant init. */
4895 TREE_READONLY (decl) = 1;
4896 if (type_quals & TYPE_QUAL_VOLATILE)
4898 TREE_SIDE_EFFECTS (decl) = 1;
4899 TREE_THIS_VOLATILE (decl) = 1;
4901 if (type_quals & TYPE_QUAL_RESTRICT)
4903 while (type && TREE_CODE (type) == ARRAY_TYPE)
4904 /* Allow 'restrict' on arrays of pointers.
4905 FIXME currently we just ignore it. */
4906 type = TREE_TYPE (type);
4907 if (!type
4908 || !POINTER_TYPE_P (type)
4909 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4910 error ("invalid use of %<restrict%>");
4914 struct c_type_hasher : ggc_hasher<tree>
4916 static hashval_t hash (tree);
4917 static bool equal (tree, tree);
4920 /* Hash function for the problem of multiple type definitions in
4921 different files. This must hash all types that will compare
4922 equal via comptypes to the same value. In practice it hashes
4923 on some of the simple stuff and leaves the details to comptypes. */
4925 hashval_t
4926 c_type_hasher::hash (tree t)
4928 int n_elements;
4929 int shift, size;
4930 tree t2;
4931 switch (TREE_CODE (t))
4933 /* For pointers, hash on pointee type plus some swizzling. */
4934 case POINTER_TYPE:
4935 return hash (TREE_TYPE (t)) ^ 0x3003003;
4936 /* Hash on number of elements and total size. */
4937 case ENUMERAL_TYPE:
4938 shift = 3;
4939 t2 = TYPE_VALUES (t);
4940 break;
4941 case RECORD_TYPE:
4942 shift = 0;
4943 t2 = TYPE_FIELDS (t);
4944 break;
4945 case QUAL_UNION_TYPE:
4946 shift = 1;
4947 t2 = TYPE_FIELDS (t);
4948 break;
4949 case UNION_TYPE:
4950 shift = 2;
4951 t2 = TYPE_FIELDS (t);
4952 break;
4953 default:
4954 gcc_unreachable ();
4956 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4957 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4958 n_elements = list_length (t2);
4959 /* We might have a VLA here. */
4960 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4961 size = 0;
4962 else
4963 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4964 return ((size << 24) | (n_elements << shift));
4967 bool
4968 c_type_hasher::equal (tree t1, tree t2)
4970 return lang_hooks.types_compatible_p (t1, t2);
4973 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4975 /* Return the typed-based alias set for T, which may be an expression
4976 or a type. Return -1 if we don't do anything special. */
4978 alias_set_type
4979 c_common_get_alias_set (tree t)
4981 tree u;
4983 /* For VLAs, use the alias set of the element type rather than the
4984 default of alias set 0 for types compared structurally. */
4985 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4987 if (TREE_CODE (t) == ARRAY_TYPE)
4988 return get_alias_set (TREE_TYPE (t));
4989 return -1;
4992 /* Permit type-punning when accessing a union, provided the access
4993 is directly through the union. For example, this code does not
4994 permit taking the address of a union member and then storing
4995 through it. Even the type-punning allowed here is a GCC
4996 extension, albeit a common and useful one; the C standard says
4997 that such accesses have implementation-defined behavior. */
4998 for (u = t;
4999 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5000 u = TREE_OPERAND (u, 0))
5001 if (TREE_CODE (u) == COMPONENT_REF
5002 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5003 return 0;
5005 /* That's all the expressions we handle specially. */
5006 if (!TYPE_P (t))
5007 return -1;
5009 /* The C standard guarantees that any object may be accessed via an
5010 lvalue that has character type. */
5011 if (t == char_type_node
5012 || t == signed_char_type_node
5013 || t == unsigned_char_type_node)
5014 return 0;
5016 /* The C standard specifically allows aliasing between signed and
5017 unsigned variants of the same type. We treat the signed
5018 variant as canonical. */
5019 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5021 tree t1 = c_common_signed_type (t);
5023 /* t1 == t can happen for boolean nodes which are always unsigned. */
5024 if (t1 != t)
5025 return get_alias_set (t1);
5028 /* Handle the case of multiple type nodes referring to "the same" type,
5029 which occurs with IMA. These share an alias set. FIXME: Currently only
5030 C90 is handled. (In C99 type compatibility is not transitive, which
5031 complicates things mightily. The alias set splay trees can theoretically
5032 represent this, but insertion is tricky when you consider all the
5033 different orders things might arrive in.) */
5035 if (c_language != clk_c || flag_isoc99)
5036 return -1;
5038 /* Save time if there's only one input file. */
5039 if (num_in_fnames == 1)
5040 return -1;
5042 /* Pointers need special handling if they point to any type that
5043 needs special handling (below). */
5044 if (TREE_CODE (t) == POINTER_TYPE)
5046 tree t2;
5047 /* Find bottom type under any nested POINTERs. */
5048 for (t2 = TREE_TYPE (t);
5049 TREE_CODE (t2) == POINTER_TYPE;
5050 t2 = TREE_TYPE (t2))
5052 if (TREE_CODE (t2) != RECORD_TYPE
5053 && TREE_CODE (t2) != ENUMERAL_TYPE
5054 && TREE_CODE (t2) != QUAL_UNION_TYPE
5055 && TREE_CODE (t2) != UNION_TYPE)
5056 return -1;
5057 if (TYPE_SIZE (t2) == 0)
5058 return -1;
5060 /* These are the only cases that need special handling. */
5061 if (TREE_CODE (t) != RECORD_TYPE
5062 && TREE_CODE (t) != ENUMERAL_TYPE
5063 && TREE_CODE (t) != QUAL_UNION_TYPE
5064 && TREE_CODE (t) != UNION_TYPE
5065 && TREE_CODE (t) != POINTER_TYPE)
5066 return -1;
5067 /* Undefined? */
5068 if (TYPE_SIZE (t) == 0)
5069 return -1;
5071 /* Look up t in hash table. Only one of the compatible types within each
5072 alias set is recorded in the table. */
5073 if (!type_hash_table)
5074 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5075 tree *slot = type_hash_table->find_slot (t, INSERT);
5076 if (*slot != NULL)
5078 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5079 return TYPE_ALIAS_SET ((tree)*slot);
5081 else
5082 /* Our caller will assign and record (in t) a new alias set; all we need
5083 to do is remember t in the hash table. */
5084 *slot = t;
5086 return -1;
5089 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5090 the IS_SIZEOF parameter indicates which operator is being applied.
5091 The COMPLAIN flag controls whether we should diagnose possibly
5092 ill-formed constructs or not. LOC is the location of the SIZEOF or
5093 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5094 a type in any context should be returned, rather than the normal
5095 alignment for that type. */
5097 tree
5098 c_sizeof_or_alignof_type (location_t loc,
5099 tree type, bool is_sizeof, bool min_alignof,
5100 int complain)
5102 const char *op_name;
5103 tree value = NULL;
5104 enum tree_code type_code = TREE_CODE (type);
5106 op_name = is_sizeof ? "sizeof" : "__alignof__";
5108 if (type_code == FUNCTION_TYPE)
5110 if (is_sizeof)
5112 if (complain && warn_pointer_arith)
5113 pedwarn (loc, OPT_Wpointer_arith,
5114 "invalid application of %<sizeof%> to a function type");
5115 else if (!complain)
5116 return error_mark_node;
5117 value = size_one_node;
5119 else
5121 if (complain)
5123 if (c_dialect_cxx ())
5124 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5125 "%<alignof%> applied to a function type");
5126 else
5127 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5128 "%<_Alignof%> applied to a function type");
5130 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5133 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5135 if (type_code == VOID_TYPE
5136 && complain && warn_pointer_arith)
5137 pedwarn (loc, OPT_Wpointer_arith,
5138 "invalid application of %qs to a void type", op_name);
5139 else if (!complain)
5140 return error_mark_node;
5141 value = size_one_node;
5143 else if (!COMPLETE_TYPE_P (type)
5144 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5146 if (complain)
5147 error_at (loc, "invalid application of %qs to incomplete type %qT",
5148 op_name, type);
5149 return error_mark_node;
5151 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5152 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5154 if (complain)
5155 error_at (loc, "invalid application of %qs to array type %qT of "
5156 "incomplete element type", op_name, type);
5157 return error_mark_node;
5159 else
5161 if (is_sizeof)
5162 /* Convert in case a char is more than one unit. */
5163 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5164 size_int (TYPE_PRECISION (char_type_node)
5165 / BITS_PER_UNIT));
5166 else if (min_alignof)
5167 value = size_int (min_align_of_type (type));
5168 else
5169 value = size_int (TYPE_ALIGN_UNIT (type));
5172 /* VALUE will have the middle-end integer type sizetype.
5173 However, we should really return a value of type `size_t',
5174 which is just a typedef for an ordinary integer type. */
5175 value = fold_convert_loc (loc, size_type_node, value);
5177 return value;
5180 /* Implement the __alignof keyword: Return the minimum required
5181 alignment of EXPR, measured in bytes. For VAR_DECLs,
5182 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5183 from an "aligned" __attribute__ specification). LOC is the
5184 location of the ALIGNOF operator. */
5186 tree
5187 c_alignof_expr (location_t loc, tree expr)
5189 tree t;
5191 if (VAR_OR_FUNCTION_DECL_P (expr))
5192 t = size_int (DECL_ALIGN_UNIT (expr));
5194 else if (TREE_CODE (expr) == COMPONENT_REF
5195 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5197 error_at (loc, "%<__alignof%> applied to a bit-field");
5198 t = size_one_node;
5200 else if (TREE_CODE (expr) == COMPONENT_REF
5201 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5202 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5204 else if (TREE_CODE (expr) == INDIRECT_REF)
5206 tree t = TREE_OPERAND (expr, 0);
5207 tree best = t;
5208 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5210 while (CONVERT_EXPR_P (t)
5211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5213 int thisalign;
5215 t = TREE_OPERAND (t, 0);
5216 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5217 if (thisalign > bestalign)
5218 best = t, bestalign = thisalign;
5220 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5222 else
5223 return c_alignof (loc, TREE_TYPE (expr));
5225 return fold_convert_loc (loc, size_type_node, t);
5228 /* Handle C and C++ default attributes. */
5230 enum built_in_attribute
5232 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5233 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5234 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5235 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5236 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5237 #include "builtin-attrs.def"
5238 #undef DEF_ATTR_NULL_TREE
5239 #undef DEF_ATTR_INT
5240 #undef DEF_ATTR_STRING
5241 #undef DEF_ATTR_IDENT
5242 #undef DEF_ATTR_TREE_LIST
5243 ATTR_LAST
5246 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5248 static void c_init_attributes (void);
5250 enum c_builtin_type
5252 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5253 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5254 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5255 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5256 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5257 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5258 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5259 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5260 ARG6) NAME,
5261 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5262 ARG6, ARG7) NAME,
5263 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5264 ARG6, ARG7, ARG8) NAME,
5265 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5266 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5267 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5268 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5269 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5270 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5271 NAME,
5272 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5273 ARG6, ARG7) NAME,
5274 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5275 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5276 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5277 #include "builtin-types.def"
5278 #undef DEF_PRIMITIVE_TYPE
5279 #undef DEF_FUNCTION_TYPE_0
5280 #undef DEF_FUNCTION_TYPE_1
5281 #undef DEF_FUNCTION_TYPE_2
5282 #undef DEF_FUNCTION_TYPE_3
5283 #undef DEF_FUNCTION_TYPE_4
5284 #undef DEF_FUNCTION_TYPE_5
5285 #undef DEF_FUNCTION_TYPE_6
5286 #undef DEF_FUNCTION_TYPE_7
5287 #undef DEF_FUNCTION_TYPE_8
5288 #undef DEF_FUNCTION_TYPE_VAR_0
5289 #undef DEF_FUNCTION_TYPE_VAR_1
5290 #undef DEF_FUNCTION_TYPE_VAR_2
5291 #undef DEF_FUNCTION_TYPE_VAR_3
5292 #undef DEF_FUNCTION_TYPE_VAR_4
5293 #undef DEF_FUNCTION_TYPE_VAR_5
5294 #undef DEF_FUNCTION_TYPE_VAR_7
5295 #undef DEF_FUNCTION_TYPE_VAR_11
5296 #undef DEF_POINTER_TYPE
5297 BT_LAST
5300 typedef enum c_builtin_type builtin_type;
5302 /* A temporary array for c_common_nodes_and_builtins. Used in
5303 communication with def_fn_type. */
5304 static tree builtin_types[(int) BT_LAST + 1];
5306 /* A helper function for c_common_nodes_and_builtins. Build function type
5307 for DEF with return type RET and N arguments. If VAR is true, then the
5308 function should be variadic after those N arguments.
5310 Takes special care not to ICE if any of the types involved are
5311 error_mark_node, which indicates that said type is not in fact available
5312 (see builtin_type_for_size). In which case the function type as a whole
5313 should be error_mark_node. */
5315 static void
5316 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5318 tree t;
5319 tree *args = XALLOCAVEC (tree, n);
5320 va_list list;
5321 int i;
5323 va_start (list, n);
5324 for (i = 0; i < n; ++i)
5326 builtin_type a = (builtin_type) va_arg (list, int);
5327 t = builtin_types[a];
5328 if (t == error_mark_node)
5329 goto egress;
5330 args[i] = t;
5333 t = builtin_types[ret];
5334 if (t == error_mark_node)
5335 goto egress;
5336 if (var)
5337 t = build_varargs_function_type_array (t, n, args);
5338 else
5339 t = build_function_type_array (t, n, args);
5341 egress:
5342 builtin_types[def] = t;
5343 va_end (list);
5346 /* Build builtin functions common to both C and C++ language
5347 frontends. */
5349 static void
5350 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5352 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5353 builtin_types[ENUM] = VALUE;
5354 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5355 def_fn_type (ENUM, RETURN, 0, 0);
5356 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5357 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5358 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5359 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5360 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5361 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5362 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5363 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5364 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5365 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5366 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5367 ARG6) \
5368 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5369 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5370 ARG6, ARG7) \
5371 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5372 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5373 ARG6, ARG7, ARG8) \
5374 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5375 ARG7, ARG8);
5376 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5377 def_fn_type (ENUM, RETURN, 1, 0);
5378 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5379 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5380 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5381 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5382 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5383 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5384 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5385 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5386 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5387 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5388 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5389 ARG6, ARG7) \
5390 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5391 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5392 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5393 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5394 ARG7, ARG8, ARG9, ARG10, ARG11);
5395 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5396 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5398 #include "builtin-types.def"
5400 #undef DEF_PRIMITIVE_TYPE
5401 #undef DEF_FUNCTION_TYPE_0
5402 #undef DEF_FUNCTION_TYPE_1
5403 #undef DEF_FUNCTION_TYPE_2
5404 #undef DEF_FUNCTION_TYPE_3
5405 #undef DEF_FUNCTION_TYPE_4
5406 #undef DEF_FUNCTION_TYPE_5
5407 #undef DEF_FUNCTION_TYPE_6
5408 #undef DEF_FUNCTION_TYPE_7
5409 #undef DEF_FUNCTION_TYPE_8
5410 #undef DEF_FUNCTION_TYPE_VAR_0
5411 #undef DEF_FUNCTION_TYPE_VAR_1
5412 #undef DEF_FUNCTION_TYPE_VAR_2
5413 #undef DEF_FUNCTION_TYPE_VAR_3
5414 #undef DEF_FUNCTION_TYPE_VAR_4
5415 #undef DEF_FUNCTION_TYPE_VAR_5
5416 #undef DEF_FUNCTION_TYPE_VAR_7
5417 #undef DEF_FUNCTION_TYPE_VAR_11
5418 #undef DEF_POINTER_TYPE
5419 builtin_types[(int) BT_LAST] = NULL_TREE;
5421 c_init_attributes ();
5423 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5424 NONANSI_P, ATTRS, IMPLICIT, COND) \
5425 if (NAME && COND) \
5426 def_builtin_1 (ENUM, NAME, CLASS, \
5427 builtin_types[(int) TYPE], \
5428 builtin_types[(int) LIBTYPE], \
5429 BOTH_P, FALLBACK_P, NONANSI_P, \
5430 built_in_attributes[(int) ATTRS], IMPLICIT);
5431 #include "builtins.def"
5432 #undef DEF_BUILTIN
5434 targetm.init_builtins ();
5436 build_common_builtin_nodes ();
5438 if (flag_cilkplus)
5439 cilk_init_builtins ();
5442 /* Like get_identifier, but avoid warnings about null arguments when
5443 the argument may be NULL for targets where GCC lacks stdint.h type
5444 information. */
5446 static inline tree
5447 c_get_ident (const char *id)
5449 return get_identifier (id);
5452 /* Build tree nodes and builtin functions common to both C and C++ language
5453 frontends. */
5455 void
5456 c_common_nodes_and_builtins (void)
5458 int char16_type_size;
5459 int char32_type_size;
5460 int wchar_type_size;
5461 tree array_domain_type;
5462 tree va_list_ref_type_node;
5463 tree va_list_arg_type_node;
5464 int i;
5466 build_common_tree_nodes (flag_signed_char, flag_short_double);
5468 /* Define `int' and `char' first so that dbx will output them first. */
5469 record_builtin_type (RID_INT, NULL, integer_type_node);
5470 record_builtin_type (RID_CHAR, "char", char_type_node);
5472 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5473 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5474 but not C. Are the conditionals here needed? */
5475 if (c_dialect_cxx ())
5476 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5477 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5478 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5479 record_builtin_type (RID_MAX, "long unsigned int",
5480 long_unsigned_type_node);
5482 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5484 char name[25];
5486 sprintf (name, "__int%d", int_n_data[i].bitsize);
5487 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5488 int_n_trees[i].signed_type);
5489 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5490 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5493 if (c_dialect_cxx ())
5494 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5495 record_builtin_type (RID_MAX, "long long int",
5496 long_long_integer_type_node);
5497 record_builtin_type (RID_MAX, "long long unsigned int",
5498 long_long_unsigned_type_node);
5499 if (c_dialect_cxx ())
5500 record_builtin_type (RID_MAX, "long long unsigned",
5501 long_long_unsigned_type_node);
5502 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5503 record_builtin_type (RID_MAX, "short unsigned int",
5504 short_unsigned_type_node);
5505 if (c_dialect_cxx ())
5506 record_builtin_type (RID_MAX, "unsigned short",
5507 short_unsigned_type_node);
5509 /* Define both `signed char' and `unsigned char'. */
5510 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5511 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5513 /* These are types that c_common_type_for_size and
5514 c_common_type_for_mode use. */
5515 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5516 TYPE_DECL, NULL_TREE,
5517 intQI_type_node));
5518 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5519 TYPE_DECL, NULL_TREE,
5520 intHI_type_node));
5521 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5522 TYPE_DECL, NULL_TREE,
5523 intSI_type_node));
5524 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5525 TYPE_DECL, NULL_TREE,
5526 intDI_type_node));
5527 #if HOST_BITS_PER_WIDE_INT >= 64
5528 /* Note that this is different than the __int128 type that's part of
5529 the generic __intN support. */
5530 if (targetm.scalar_mode_supported_p (TImode))
5531 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5532 TYPE_DECL,
5533 get_identifier ("__int128_t"),
5534 intTI_type_node));
5535 #endif
5536 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5537 TYPE_DECL, NULL_TREE,
5538 unsigned_intQI_type_node));
5539 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5540 TYPE_DECL, NULL_TREE,
5541 unsigned_intHI_type_node));
5542 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5543 TYPE_DECL, NULL_TREE,
5544 unsigned_intSI_type_node));
5545 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5546 TYPE_DECL, NULL_TREE,
5547 unsigned_intDI_type_node));
5548 #if HOST_BITS_PER_WIDE_INT >= 64
5549 if (targetm.scalar_mode_supported_p (TImode))
5550 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5551 TYPE_DECL,
5552 get_identifier ("__uint128_t"),
5553 unsigned_intTI_type_node));
5554 #endif
5556 /* Create the widest literal types. */
5557 widest_integer_literal_type_node
5558 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5559 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5560 TYPE_DECL, NULL_TREE,
5561 widest_integer_literal_type_node));
5563 widest_unsigned_literal_type_node
5564 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5565 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5566 TYPE_DECL, NULL_TREE,
5567 widest_unsigned_literal_type_node));
5569 signed_size_type_node = c_common_signed_type (size_type_node);
5571 pid_type_node =
5572 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5574 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5575 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5576 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5578 /* Only supported decimal floating point extension if the target
5579 actually supports underlying modes. */
5580 if (targetm.scalar_mode_supported_p (SDmode)
5581 && targetm.scalar_mode_supported_p (DDmode)
5582 && targetm.scalar_mode_supported_p (TDmode))
5584 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5585 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5586 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5589 if (targetm.fixed_point_supported_p ())
5591 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5592 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5593 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5594 record_builtin_type (RID_MAX, "long long _Fract",
5595 long_long_fract_type_node);
5596 record_builtin_type (RID_MAX, "unsigned short _Fract",
5597 unsigned_short_fract_type_node);
5598 record_builtin_type (RID_MAX, "unsigned _Fract",
5599 unsigned_fract_type_node);
5600 record_builtin_type (RID_MAX, "unsigned long _Fract",
5601 unsigned_long_fract_type_node);
5602 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5603 unsigned_long_long_fract_type_node);
5604 record_builtin_type (RID_MAX, "_Sat short _Fract",
5605 sat_short_fract_type_node);
5606 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5607 record_builtin_type (RID_MAX, "_Sat long _Fract",
5608 sat_long_fract_type_node);
5609 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5610 sat_long_long_fract_type_node);
5611 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5612 sat_unsigned_short_fract_type_node);
5613 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5614 sat_unsigned_fract_type_node);
5615 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5616 sat_unsigned_long_fract_type_node);
5617 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5618 sat_unsigned_long_long_fract_type_node);
5619 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5620 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5621 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5622 record_builtin_type (RID_MAX, "long long _Accum",
5623 long_long_accum_type_node);
5624 record_builtin_type (RID_MAX, "unsigned short _Accum",
5625 unsigned_short_accum_type_node);
5626 record_builtin_type (RID_MAX, "unsigned _Accum",
5627 unsigned_accum_type_node);
5628 record_builtin_type (RID_MAX, "unsigned long _Accum",
5629 unsigned_long_accum_type_node);
5630 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5631 unsigned_long_long_accum_type_node);
5632 record_builtin_type (RID_MAX, "_Sat short _Accum",
5633 sat_short_accum_type_node);
5634 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5635 record_builtin_type (RID_MAX, "_Sat long _Accum",
5636 sat_long_accum_type_node);
5637 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5638 sat_long_long_accum_type_node);
5639 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5640 sat_unsigned_short_accum_type_node);
5641 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5642 sat_unsigned_accum_type_node);
5643 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5644 sat_unsigned_long_accum_type_node);
5645 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5646 sat_unsigned_long_long_accum_type_node);
5650 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5651 TYPE_DECL,
5652 get_identifier ("complex int"),
5653 complex_integer_type_node));
5654 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5655 TYPE_DECL,
5656 get_identifier ("complex float"),
5657 complex_float_type_node));
5658 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5659 TYPE_DECL,
5660 get_identifier ("complex double"),
5661 complex_double_type_node));
5662 lang_hooks.decls.pushdecl
5663 (build_decl (UNKNOWN_LOCATION,
5664 TYPE_DECL, get_identifier ("complex long double"),
5665 complex_long_double_type_node));
5667 if (c_dialect_cxx ())
5668 /* For C++, make fileptr_type_node a distinct void * type until
5669 FILE type is defined. */
5670 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5672 record_builtin_type (RID_VOID, NULL, void_type_node);
5674 /* Set the TYPE_NAME for any variants that were built before
5675 record_builtin_type gave names to the built-in types. */
5677 tree void_name = TYPE_NAME (void_type_node);
5678 TYPE_NAME (void_type_node) = NULL_TREE;
5679 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5680 = void_name;
5681 TYPE_NAME (void_type_node) = void_name;
5684 void_list_node = build_void_list_node ();
5686 /* Make a type to be the domain of a few array types
5687 whose domains don't really matter.
5688 200 is small enough that it always fits in size_t
5689 and large enough that it can hold most function names for the
5690 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5691 array_domain_type = build_index_type (size_int (200));
5693 /* Make a type for arrays of characters.
5694 With luck nothing will ever really depend on the length of this
5695 array type. */
5696 char_array_type_node
5697 = build_array_type (char_type_node, array_domain_type);
5699 string_type_node = build_pointer_type (char_type_node);
5700 const_string_type_node
5701 = build_pointer_type (build_qualified_type
5702 (char_type_node, TYPE_QUAL_CONST));
5704 /* This is special for C++ so functions can be overloaded. */
5705 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5706 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5707 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5708 underlying_wchar_type_node = wchar_type_node;
5709 if (c_dialect_cxx ())
5711 if (TYPE_UNSIGNED (wchar_type_node))
5712 wchar_type_node = make_unsigned_type (wchar_type_size);
5713 else
5714 wchar_type_node = make_signed_type (wchar_type_size);
5715 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5718 /* This is for wide string constants. */
5719 wchar_array_type_node
5720 = build_array_type (wchar_type_node, array_domain_type);
5722 /* Define 'char16_t'. */
5723 char16_type_node = get_identifier (CHAR16_TYPE);
5724 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5725 char16_type_size = TYPE_PRECISION (char16_type_node);
5726 if (c_dialect_cxx ())
5728 char16_type_node = make_unsigned_type (char16_type_size);
5730 if (cxx_dialect >= cxx11)
5731 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5734 /* This is for UTF-16 string constants. */
5735 char16_array_type_node
5736 = build_array_type (char16_type_node, array_domain_type);
5738 /* Define 'char32_t'. */
5739 char32_type_node = get_identifier (CHAR32_TYPE);
5740 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5741 char32_type_size = TYPE_PRECISION (char32_type_node);
5742 if (c_dialect_cxx ())
5744 char32_type_node = make_unsigned_type (char32_type_size);
5746 if (cxx_dialect >= cxx11)
5747 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5750 /* This is for UTF-32 string constants. */
5751 char32_array_type_node
5752 = build_array_type (char32_type_node, array_domain_type);
5754 wint_type_node =
5755 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5757 intmax_type_node =
5758 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5759 uintmax_type_node =
5760 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5762 if (SIG_ATOMIC_TYPE)
5763 sig_atomic_type_node =
5764 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5765 if (INT8_TYPE)
5766 int8_type_node =
5767 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5768 if (INT16_TYPE)
5769 int16_type_node =
5770 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5771 if (INT32_TYPE)
5772 int32_type_node =
5773 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5774 if (INT64_TYPE)
5775 int64_type_node =
5776 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5777 if (UINT8_TYPE)
5778 uint8_type_node =
5779 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5780 if (UINT16_TYPE)
5781 c_uint16_type_node = uint16_type_node =
5782 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5783 if (UINT32_TYPE)
5784 c_uint32_type_node = uint32_type_node =
5785 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5786 if (UINT64_TYPE)
5787 c_uint64_type_node = uint64_type_node =
5788 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5789 if (INT_LEAST8_TYPE)
5790 int_least8_type_node =
5791 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5792 if (INT_LEAST16_TYPE)
5793 int_least16_type_node =
5794 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5795 if (INT_LEAST32_TYPE)
5796 int_least32_type_node =
5797 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5798 if (INT_LEAST64_TYPE)
5799 int_least64_type_node =
5800 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5801 if (UINT_LEAST8_TYPE)
5802 uint_least8_type_node =
5803 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5804 if (UINT_LEAST16_TYPE)
5805 uint_least16_type_node =
5806 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5807 if (UINT_LEAST32_TYPE)
5808 uint_least32_type_node =
5809 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5810 if (UINT_LEAST64_TYPE)
5811 uint_least64_type_node =
5812 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5813 if (INT_FAST8_TYPE)
5814 int_fast8_type_node =
5815 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5816 if (INT_FAST16_TYPE)
5817 int_fast16_type_node =
5818 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5819 if (INT_FAST32_TYPE)
5820 int_fast32_type_node =
5821 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5822 if (INT_FAST64_TYPE)
5823 int_fast64_type_node =
5824 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5825 if (UINT_FAST8_TYPE)
5826 uint_fast8_type_node =
5827 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5828 if (UINT_FAST16_TYPE)
5829 uint_fast16_type_node =
5830 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5831 if (UINT_FAST32_TYPE)
5832 uint_fast32_type_node =
5833 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5834 if (UINT_FAST64_TYPE)
5835 uint_fast64_type_node =
5836 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5837 if (INTPTR_TYPE)
5838 intptr_type_node =
5839 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5840 if (UINTPTR_TYPE)
5841 uintptr_type_node =
5842 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5844 default_function_type
5845 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5846 ptrdiff_type_node
5847 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5848 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5850 lang_hooks.decls.pushdecl
5851 (build_decl (UNKNOWN_LOCATION,
5852 TYPE_DECL, get_identifier ("__builtin_va_list"),
5853 va_list_type_node));
5854 if (targetm.enum_va_list_p)
5856 int l;
5857 const char *pname;
5858 tree ptype;
5860 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5862 lang_hooks.decls.pushdecl
5863 (build_decl (UNKNOWN_LOCATION,
5864 TYPE_DECL, get_identifier (pname),
5865 ptype));
5870 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5872 va_list_arg_type_node = va_list_ref_type_node =
5873 build_pointer_type (TREE_TYPE (va_list_type_node));
5875 else
5877 va_list_arg_type_node = va_list_type_node;
5878 va_list_ref_type_node = build_reference_type (va_list_type_node);
5881 if (!flag_preprocess_only)
5882 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5884 main_identifier_node = get_identifier ("main");
5886 /* Create the built-in __null node. It is important that this is
5887 not shared. */
5888 null_node = make_int_cst (1, 1);
5889 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5891 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5892 memset (builtin_types, 0, sizeof (builtin_types));
5895 /* The number of named compound-literals generated thus far. */
5896 static GTY(()) int compound_literal_number;
5898 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5900 void
5901 set_compound_literal_name (tree decl)
5903 char *name;
5904 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5905 compound_literal_number);
5906 compound_literal_number++;
5907 DECL_NAME (decl) = get_identifier (name);
5910 tree
5911 build_va_arg (location_t loc, tree expr, tree type)
5913 /* In gimplify_va_arg_expr we take the address of the ap argument, mark it
5914 addressable now. */
5915 mark_addressable (expr);
5917 expr = build1 (VA_ARG_EXPR, type, expr);
5918 SET_EXPR_LOCATION (expr, loc);
5919 return expr;
5923 /* Linked list of disabled built-in functions. */
5925 typedef struct disabled_builtin
5927 const char *name;
5928 struct disabled_builtin *next;
5929 } disabled_builtin;
5930 static disabled_builtin *disabled_builtins = NULL;
5932 static bool builtin_function_disabled_p (const char *);
5934 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5935 begins with "__builtin_", give an error. */
5937 void
5938 disable_builtin_function (const char *name)
5940 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5941 error ("cannot disable built-in function %qs", name);
5942 else
5944 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5945 new_disabled_builtin->name = name;
5946 new_disabled_builtin->next = disabled_builtins;
5947 disabled_builtins = new_disabled_builtin;
5952 /* Return true if the built-in function NAME has been disabled, false
5953 otherwise. */
5955 static bool
5956 builtin_function_disabled_p (const char *name)
5958 disabled_builtin *p;
5959 for (p = disabled_builtins; p != NULL; p = p->next)
5961 if (strcmp (name, p->name) == 0)
5962 return true;
5964 return false;
5968 /* Worker for DEF_BUILTIN.
5969 Possibly define a builtin function with one or two names.
5970 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5971 nonansi_p and flag_no_nonansi_builtin. */
5973 static void
5974 def_builtin_1 (enum built_in_function fncode,
5975 const char *name,
5976 enum built_in_class fnclass,
5977 tree fntype, tree libtype,
5978 bool both_p, bool fallback_p, bool nonansi_p,
5979 tree fnattrs, bool implicit_p)
5981 tree decl;
5982 const char *libname;
5984 if (fntype == error_mark_node)
5985 return;
5987 gcc_assert ((!both_p && !fallback_p)
5988 || !strncmp (name, "__builtin_",
5989 strlen ("__builtin_")));
5991 libname = name + strlen ("__builtin_");
5992 decl = add_builtin_function (name, fntype, fncode, fnclass,
5993 (fallback_p ? libname : NULL),
5994 fnattrs);
5996 set_builtin_decl (fncode, decl, implicit_p);
5998 if (both_p
5999 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6000 && !(nonansi_p && flag_no_nonansi_builtin))
6001 add_builtin_function (libname, libtype, fncode, fnclass,
6002 NULL, fnattrs);
6005 /* Nonzero if the type T promotes to int. This is (nearly) the
6006 integral promotions defined in ISO C99 6.3.1.1/2. */
6008 bool
6009 c_promoting_integer_type_p (const_tree t)
6011 switch (TREE_CODE (t))
6013 case INTEGER_TYPE:
6014 return (TYPE_MAIN_VARIANT (t) == char_type_node
6015 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6016 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6017 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6018 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6019 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6021 case ENUMERAL_TYPE:
6022 /* ??? Technically all enumerations not larger than an int
6023 promote to an int. But this is used along code paths
6024 that only want to notice a size change. */
6025 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6027 case BOOLEAN_TYPE:
6028 return 1;
6030 default:
6031 return 0;
6035 /* Return 1 if PARMS specifies a fixed number of parameters
6036 and none of their types is affected by default promotions. */
6039 self_promoting_args_p (const_tree parms)
6041 const_tree t;
6042 for (t = parms; t; t = TREE_CHAIN (t))
6044 tree type = TREE_VALUE (t);
6046 if (type == error_mark_node)
6047 continue;
6049 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6050 return 0;
6052 if (type == 0)
6053 return 0;
6055 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6056 return 0;
6058 if (c_promoting_integer_type_p (type))
6059 return 0;
6061 return 1;
6064 /* Recursively remove any '*' or '&' operator from TYPE. */
6065 tree
6066 strip_pointer_operator (tree t)
6068 while (POINTER_TYPE_P (t))
6069 t = TREE_TYPE (t);
6070 return t;
6073 /* Recursively remove pointer or array type from TYPE. */
6074 tree
6075 strip_pointer_or_array_types (tree t)
6077 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6078 t = TREE_TYPE (t);
6079 return t;
6082 /* Used to compare case labels. K1 and K2 are actually tree nodes
6083 representing case labels, or NULL_TREE for a `default' label.
6084 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6085 K2, and 0 if K1 and K2 are equal. */
6088 case_compare (splay_tree_key k1, splay_tree_key k2)
6090 /* Consider a NULL key (such as arises with a `default' label) to be
6091 smaller than anything else. */
6092 if (!k1)
6093 return k2 ? -1 : 0;
6094 else if (!k2)
6095 return k1 ? 1 : 0;
6097 return tree_int_cst_compare ((tree) k1, (tree) k2);
6100 /* Process a case label, located at LOC, for the range LOW_VALUE
6101 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6102 then this case label is actually a `default' label. If only
6103 HIGH_VALUE is NULL_TREE, then case label was declared using the
6104 usual C/C++ syntax, rather than the GNU case range extension.
6105 CASES is a tree containing all the case ranges processed so far;
6106 COND is the condition for the switch-statement itself. Returns the
6107 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6108 is created. */
6110 tree
6111 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6112 tree low_value, tree high_value)
6114 tree type;
6115 tree label;
6116 tree case_label;
6117 splay_tree_node node;
6119 /* Create the LABEL_DECL itself. */
6120 label = create_artificial_label (loc);
6122 /* If there was an error processing the switch condition, bail now
6123 before we get more confused. */
6124 if (!cond || cond == error_mark_node)
6125 goto error_out;
6127 if ((low_value && TREE_TYPE (low_value)
6128 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6129 || (high_value && TREE_TYPE (high_value)
6130 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6132 error_at (loc, "pointers are not permitted as case values");
6133 goto error_out;
6136 /* Case ranges are a GNU extension. */
6137 if (high_value)
6138 pedwarn (loc, OPT_Wpedantic,
6139 "range expressions in switch statements are non-standard");
6141 type = TREE_TYPE (cond);
6142 if (low_value)
6144 low_value = check_case_value (loc, low_value);
6145 low_value = convert_and_check (loc, type, low_value);
6146 if (low_value == error_mark_node)
6147 goto error_out;
6149 if (high_value)
6151 high_value = check_case_value (loc, high_value);
6152 high_value = convert_and_check (loc, type, high_value);
6153 if (high_value == error_mark_node)
6154 goto error_out;
6157 if (low_value && high_value)
6159 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6160 really a case range, even though it was written that way.
6161 Remove the HIGH_VALUE to simplify later processing. */
6162 if (tree_int_cst_equal (low_value, high_value))
6163 high_value = NULL_TREE;
6164 else if (!tree_int_cst_lt (low_value, high_value))
6165 warning_at (loc, 0, "empty range specified");
6168 /* See if the case is in range of the type of the original testing
6169 expression. If both low_value and high_value are out of range,
6170 don't insert the case label and return NULL_TREE. */
6171 if (low_value
6172 && !check_case_bounds (loc, type, orig_type,
6173 &low_value, high_value ? &high_value : NULL))
6174 return NULL_TREE;
6176 /* Look up the LOW_VALUE in the table of case labels we already
6177 have. */
6178 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6179 /* If there was not an exact match, check for overlapping ranges.
6180 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6181 that's a `default' label and the only overlap is an exact match. */
6182 if (!node && (low_value || high_value))
6184 splay_tree_node low_bound;
6185 splay_tree_node high_bound;
6187 /* Even though there wasn't an exact match, there might be an
6188 overlap between this case range and another case range.
6189 Since we've (inductively) not allowed any overlapping case
6190 ranges, we simply need to find the greatest low case label
6191 that is smaller that LOW_VALUE, and the smallest low case
6192 label that is greater than LOW_VALUE. If there is an overlap
6193 it will occur in one of these two ranges. */
6194 low_bound = splay_tree_predecessor (cases,
6195 (splay_tree_key) low_value);
6196 high_bound = splay_tree_successor (cases,
6197 (splay_tree_key) low_value);
6199 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6200 the LOW_VALUE, so there is no need to check unless the
6201 LOW_BOUND is in fact itself a case range. */
6202 if (low_bound
6203 && CASE_HIGH ((tree) low_bound->value)
6204 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6205 low_value) >= 0)
6206 node = low_bound;
6207 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6208 range is bigger than the low end of the current range, so we
6209 are only interested if the current range is a real range, and
6210 not an ordinary case label. */
6211 else if (high_bound
6212 && high_value
6213 && (tree_int_cst_compare ((tree) high_bound->key,
6214 high_value)
6215 <= 0))
6216 node = high_bound;
6218 /* If there was an overlap, issue an error. */
6219 if (node)
6221 tree duplicate = CASE_LABEL ((tree) node->value);
6223 if (high_value)
6225 error_at (loc, "duplicate (or overlapping) case value");
6226 error_at (DECL_SOURCE_LOCATION (duplicate),
6227 "this is the first entry overlapping that value");
6229 else if (low_value)
6231 error_at (loc, "duplicate case value") ;
6232 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6234 else
6236 error_at (loc, "multiple default labels in one switch");
6237 error_at (DECL_SOURCE_LOCATION (duplicate),
6238 "this is the first default label");
6240 goto error_out;
6243 /* Add a CASE_LABEL to the statement-tree. */
6244 case_label = add_stmt (build_case_label (low_value, high_value, label));
6245 /* Register this case label in the splay tree. */
6246 splay_tree_insert (cases,
6247 (splay_tree_key) low_value,
6248 (splay_tree_value) case_label);
6250 return case_label;
6252 error_out:
6253 /* Add a label so that the back-end doesn't think that the beginning of
6254 the switch is unreachable. Note that we do not add a case label, as
6255 that just leads to duplicates and thence to failure later on. */
6256 if (!cases->root)
6258 tree t = create_artificial_label (loc);
6259 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6261 return error_mark_node;
6264 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6265 Used to verify that case values match up with enumerator values. */
6267 static void
6268 match_case_to_enum_1 (tree key, tree type, tree label)
6270 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6272 if (tree_fits_uhwi_p (key))
6273 print_dec (key, buf, UNSIGNED);
6274 else if (tree_fits_shwi_p (key))
6275 print_dec (key, buf, SIGNED);
6276 else
6277 print_hex (key, buf);
6279 if (TYPE_NAME (type) == 0)
6280 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6281 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6282 "case value %qs not in enumerated type",
6283 buf);
6284 else
6285 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6286 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6287 "case value %qs not in enumerated type %qT",
6288 buf, type);
6291 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6292 Used to verify that case values match up with enumerator values. */
6294 static int
6295 match_case_to_enum (splay_tree_node node, void *data)
6297 tree label = (tree) node->value;
6298 tree type = (tree) data;
6300 /* Skip default case. */
6301 if (!CASE_LOW (label))
6302 return 0;
6304 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6305 when we did our enum->case scan. Reset our scratch bit after. */
6306 if (!CASE_LOW_SEEN (label))
6307 match_case_to_enum_1 (CASE_LOW (label), type, label);
6308 else
6309 CASE_LOW_SEEN (label) = 0;
6311 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6312 not set, that means that CASE_HIGH did not appear when we did our
6313 enum->case scan. Reset our scratch bit after. */
6314 if (CASE_HIGH (label))
6316 if (!CASE_HIGH_SEEN (label))
6317 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6318 else
6319 CASE_HIGH_SEEN (label) = 0;
6322 return 0;
6325 /* Handle -Wswitch*. Called from the front end after parsing the
6326 switch construct. */
6327 /* ??? Should probably be somewhere generic, since other languages
6328 besides C and C++ would want this. At the moment, however, C/C++
6329 are the only tree-ssa languages that support enumerations at all,
6330 so the point is moot. */
6332 void
6333 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6334 tree type, tree cond)
6336 splay_tree_node default_node;
6337 splay_tree_node node;
6338 tree chain;
6340 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6341 return;
6343 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6344 if (!default_node)
6345 warning_at (switch_location, OPT_Wswitch_default,
6346 "switch missing default case");
6348 /* From here on, we only care about about enumerated types. */
6349 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6350 return;
6352 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6353 if (!warn_switch_enum && !warn_switch)
6354 return;
6356 /* Check the cases. Warn about case values which are not members of
6357 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6358 there is no default case, check that exactly all enumeration
6359 literals are covered by the cases. */
6361 /* Clearing COND if it is not an integer constant simplifies
6362 the tests inside the loop below. */
6363 if (TREE_CODE (cond) != INTEGER_CST)
6364 cond = NULL_TREE;
6366 /* The time complexity here is O(N*lg(N)) worst case, but for the
6367 common case of monotonically increasing enumerators, it is
6368 O(N), since the nature of the splay tree will keep the next
6369 element adjacent to the root at all times. */
6371 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6373 tree value = TREE_VALUE (chain);
6374 if (TREE_CODE (value) == CONST_DECL)
6375 value = DECL_INITIAL (value);
6376 node = splay_tree_lookup (cases, (splay_tree_key) value);
6377 if (node)
6379 /* Mark the CASE_LOW part of the case entry as seen. */
6380 tree label = (tree) node->value;
6381 CASE_LOW_SEEN (label) = 1;
6382 continue;
6385 /* Even though there wasn't an exact match, there might be a
6386 case range which includes the enumerator's value. */
6387 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6388 if (node && CASE_HIGH ((tree) node->value))
6390 tree label = (tree) node->value;
6391 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6392 if (cmp >= 0)
6394 /* If we match the upper bound exactly, mark the CASE_HIGH
6395 part of the case entry as seen. */
6396 if (cmp == 0)
6397 CASE_HIGH_SEEN (label) = 1;
6398 continue;
6402 /* We've now determined that this enumerated literal isn't
6403 handled by the case labels of the switch statement. */
6405 /* If the switch expression is a constant, we only really care
6406 about whether that constant is handled by the switch. */
6407 if (cond && tree_int_cst_compare (cond, value))
6408 continue;
6410 /* If there is a default_node, the only relevant option is
6411 Wswitch-enum. Otherwise, if both are enabled then we prefer
6412 to warn using -Wswitch because -Wswitch is enabled by -Wall
6413 while -Wswitch-enum is explicit. */
6414 warning_at (switch_location,
6415 (default_node || !warn_switch
6416 ? OPT_Wswitch_enum
6417 : OPT_Wswitch),
6418 "enumeration value %qE not handled in switch",
6419 TREE_PURPOSE (chain));
6422 /* Warn if there are case expressions that don't correspond to
6423 enumerators. This can occur since C and C++ don't enforce
6424 type-checking of assignments to enumeration variables.
6426 The time complexity here is now always O(N) worst case, since
6427 we should have marked both the lower bound and upper bound of
6428 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6429 above. This scan also resets those fields. */
6431 splay_tree_foreach (cases, match_case_to_enum, type);
6434 /* Finish an expression taking the address of LABEL (an
6435 IDENTIFIER_NODE). Returns an expression for the address.
6437 LOC is the location for the expression returned. */
6439 tree
6440 finish_label_address_expr (tree label, location_t loc)
6442 tree result;
6444 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6446 if (label == error_mark_node)
6447 return error_mark_node;
6449 label = lookup_label (label);
6450 if (label == NULL_TREE)
6451 result = null_pointer_node;
6452 else
6454 TREE_USED (label) = 1;
6455 result = build1 (ADDR_EXPR, ptr_type_node, label);
6456 /* The current function is not necessarily uninlinable.
6457 Computed gotos are incompatible with inlining, but the value
6458 here could be used only in a diagnostic, for example. */
6459 protected_set_expr_location (result, loc);
6462 return result;
6466 /* Given a boolean expression ARG, return a tree representing an increment
6467 or decrement (as indicated by CODE) of ARG. The front end must check for
6468 invalid cases (e.g., decrement in C++). */
6469 tree
6470 boolean_increment (enum tree_code code, tree arg)
6472 tree val;
6473 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6475 arg = stabilize_reference (arg);
6476 switch (code)
6478 case PREINCREMENT_EXPR:
6479 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6480 break;
6481 case POSTINCREMENT_EXPR:
6482 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6483 arg = save_expr (arg);
6484 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6485 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6486 break;
6487 case PREDECREMENT_EXPR:
6488 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6489 invert_truthvalue_loc (input_location, arg));
6490 break;
6491 case POSTDECREMENT_EXPR:
6492 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6493 invert_truthvalue_loc (input_location, arg));
6494 arg = save_expr (arg);
6495 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6496 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6497 break;
6498 default:
6499 gcc_unreachable ();
6501 TREE_SIDE_EFFECTS (val) = 1;
6502 return val;
6505 /* Built-in macros for stddef.h and stdint.h, that require macros
6506 defined in this file. */
6507 void
6508 c_stddef_cpp_builtins(void)
6510 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6511 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6512 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6513 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6514 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6515 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6516 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6517 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6518 if (SIG_ATOMIC_TYPE)
6519 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6520 if (INT8_TYPE)
6521 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6522 if (INT16_TYPE)
6523 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6524 if (INT32_TYPE)
6525 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6526 if (INT64_TYPE)
6527 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6528 if (UINT8_TYPE)
6529 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6530 if (UINT16_TYPE)
6531 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6532 if (UINT32_TYPE)
6533 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6534 if (UINT64_TYPE)
6535 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6536 if (INT_LEAST8_TYPE)
6537 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6538 if (INT_LEAST16_TYPE)
6539 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6540 if (INT_LEAST32_TYPE)
6541 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6542 if (INT_LEAST64_TYPE)
6543 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6544 if (UINT_LEAST8_TYPE)
6545 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6546 if (UINT_LEAST16_TYPE)
6547 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6548 if (UINT_LEAST32_TYPE)
6549 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6550 if (UINT_LEAST64_TYPE)
6551 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6552 if (INT_FAST8_TYPE)
6553 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6554 if (INT_FAST16_TYPE)
6555 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6556 if (INT_FAST32_TYPE)
6557 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6558 if (INT_FAST64_TYPE)
6559 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6560 if (UINT_FAST8_TYPE)
6561 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6562 if (UINT_FAST16_TYPE)
6563 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6564 if (UINT_FAST32_TYPE)
6565 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6566 if (UINT_FAST64_TYPE)
6567 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6568 if (INTPTR_TYPE)
6569 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6570 if (UINTPTR_TYPE)
6571 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6574 static void
6575 c_init_attributes (void)
6577 /* Fill in the built_in_attributes array. */
6578 #define DEF_ATTR_NULL_TREE(ENUM) \
6579 built_in_attributes[(int) ENUM] = NULL_TREE;
6580 #define DEF_ATTR_INT(ENUM, VALUE) \
6581 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6582 #define DEF_ATTR_STRING(ENUM, VALUE) \
6583 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6584 #define DEF_ATTR_IDENT(ENUM, STRING) \
6585 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6586 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6587 built_in_attributes[(int) ENUM] \
6588 = tree_cons (built_in_attributes[(int) PURPOSE], \
6589 built_in_attributes[(int) VALUE], \
6590 built_in_attributes[(int) CHAIN]);
6591 #include "builtin-attrs.def"
6592 #undef DEF_ATTR_NULL_TREE
6593 #undef DEF_ATTR_INT
6594 #undef DEF_ATTR_IDENT
6595 #undef DEF_ATTR_TREE_LIST
6598 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6599 identifier as an argument, so the front end shouldn't look it up. */
6601 bool
6602 attribute_takes_identifier_p (const_tree attr_id)
6604 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6605 if (spec == NULL)
6606 /* Unknown attribute that we'll end up ignoring, return true so we
6607 don't complain about an identifier argument. */
6608 return true;
6609 else if (!strcmp ("mode", spec->name)
6610 || !strcmp ("format", spec->name)
6611 || !strcmp ("cleanup", spec->name))
6612 return true;
6613 else
6614 return targetm.attribute_takes_identifier_p (attr_id);
6617 /* Attribute handlers common to C front ends. */
6619 /* Handle a "packed" attribute; arguments as in
6620 struct attribute_spec.handler. */
6622 static tree
6623 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6624 int flags, bool *no_add_attrs)
6626 if (TYPE_P (*node))
6628 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6629 *node = build_variant_type_copy (*node);
6630 TYPE_PACKED (*node) = 1;
6632 else if (TREE_CODE (*node) == FIELD_DECL)
6634 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6635 /* Still pack bitfields. */
6636 && ! DECL_INITIAL (*node))
6637 warning (OPT_Wattributes,
6638 "%qE attribute ignored for field of type %qT",
6639 name, TREE_TYPE (*node));
6640 else
6641 DECL_PACKED (*node) = 1;
6643 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6644 used for DECL_REGISTER. It wouldn't mean anything anyway.
6645 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6646 that changes what the typedef is typing. */
6647 else
6649 warning (OPT_Wattributes, "%qE attribute ignored", name);
6650 *no_add_attrs = true;
6653 return NULL_TREE;
6656 /* Handle a "nocommon" attribute; arguments as in
6657 struct attribute_spec.handler. */
6659 static tree
6660 handle_nocommon_attribute (tree *node, tree name,
6661 tree ARG_UNUSED (args),
6662 int ARG_UNUSED (flags), bool *no_add_attrs)
6664 if (TREE_CODE (*node) == VAR_DECL)
6665 DECL_COMMON (*node) = 0;
6666 else
6668 warning (OPT_Wattributes, "%qE attribute ignored", name);
6669 *no_add_attrs = true;
6672 return NULL_TREE;
6675 /* Handle a "common" attribute; arguments as in
6676 struct attribute_spec.handler. */
6678 static tree
6679 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6680 int ARG_UNUSED (flags), bool *no_add_attrs)
6682 if (TREE_CODE (*node) == VAR_DECL)
6683 DECL_COMMON (*node) = 1;
6684 else
6686 warning (OPT_Wattributes, "%qE attribute ignored", name);
6687 *no_add_attrs = true;
6690 return NULL_TREE;
6693 /* Handle a "noreturn" attribute; arguments as in
6694 struct attribute_spec.handler. */
6696 static tree
6697 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6698 int ARG_UNUSED (flags), bool *no_add_attrs)
6700 tree type = TREE_TYPE (*node);
6702 /* See FIXME comment in c_common_attribute_table. */
6703 if (TREE_CODE (*node) == FUNCTION_DECL
6704 || objc_method_decl (TREE_CODE (*node)))
6705 TREE_THIS_VOLATILE (*node) = 1;
6706 else if (TREE_CODE (type) == POINTER_TYPE
6707 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6708 TREE_TYPE (*node)
6709 = (build_qualified_type
6710 (build_pointer_type
6711 (build_type_variant (TREE_TYPE (type),
6712 TYPE_READONLY (TREE_TYPE (type)), 1)),
6713 TYPE_QUALS (type)));
6714 else
6716 warning (OPT_Wattributes, "%qE attribute ignored", name);
6717 *no_add_attrs = true;
6720 return NULL_TREE;
6723 /* Handle a "hot" and attribute; arguments as in
6724 struct attribute_spec.handler. */
6726 static tree
6727 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6728 int ARG_UNUSED (flags), bool *no_add_attrs)
6730 if (TREE_CODE (*node) == FUNCTION_DECL
6731 || TREE_CODE (*node) == LABEL_DECL)
6733 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6735 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6736 "with attribute %qs", name, "cold");
6737 *no_add_attrs = true;
6739 /* Most of the rest of the hot processing is done later with
6740 lookup_attribute. */
6742 else
6744 warning (OPT_Wattributes, "%qE attribute ignored", name);
6745 *no_add_attrs = true;
6748 return NULL_TREE;
6751 /* Handle a "cold" and attribute; arguments as in
6752 struct attribute_spec.handler. */
6754 static tree
6755 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6756 int ARG_UNUSED (flags), bool *no_add_attrs)
6758 if (TREE_CODE (*node) == FUNCTION_DECL
6759 || TREE_CODE (*node) == LABEL_DECL)
6761 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6763 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6764 "with attribute %qs", name, "hot");
6765 *no_add_attrs = true;
6767 /* Most of the rest of the cold processing is done later with
6768 lookup_attribute. */
6770 else
6772 warning (OPT_Wattributes, "%qE attribute ignored", name);
6773 *no_add_attrs = true;
6776 return NULL_TREE;
6779 /* Handle a "no_sanitize_address" attribute; arguments as in
6780 struct attribute_spec.handler. */
6782 static tree
6783 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6784 bool *no_add_attrs)
6786 if (TREE_CODE (*node) != FUNCTION_DECL)
6788 warning (OPT_Wattributes, "%qE attribute ignored", name);
6789 *no_add_attrs = true;
6792 return NULL_TREE;
6795 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6796 struct attribute_spec.handler. */
6798 static tree
6799 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6800 bool *no_add_attrs)
6802 if (TREE_CODE (*node) != FUNCTION_DECL)
6803 warning (OPT_Wattributes, "%qE attribute ignored", name);
6804 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6805 DECL_ATTRIBUTES (*node)
6806 = tree_cons (get_identifier ("no_sanitize_address"),
6807 NULL_TREE, DECL_ATTRIBUTES (*node));
6808 *no_add_attrs = true;
6809 return NULL_TREE;
6812 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6813 struct attribute_spec.handler. */
6815 static tree
6816 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6817 bool *no_add_attrs)
6819 if (TREE_CODE (*node) != FUNCTION_DECL)
6821 warning (OPT_Wattributes, "%qE attribute ignored", name);
6822 *no_add_attrs = true;
6825 return NULL_TREE;
6828 /* Handle a "stack_protect" attribute; arguments as in
6829 struct attribute_spec.handler. */
6830 static tree
6831 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6832 bool *no_add_attrs)
6834 if (TREE_CODE (*node) != FUNCTION_DECL)
6836 warning (OPT_Wattributes, "%qE attribute ignored", name);
6837 *no_add_attrs = true;
6839 else
6840 DECL_ATTRIBUTES (*node)
6841 = tree_cons (get_identifier ("stack_protect"),
6842 NULL_TREE, DECL_ATTRIBUTES (*node));
6844 return NULL_TREE;
6847 /* Handle a "noinline" attribute; arguments as in
6848 struct attribute_spec.handler. */
6850 static tree
6851 handle_noinline_attribute (tree *node, tree name,
6852 tree ARG_UNUSED (args),
6853 int ARG_UNUSED (flags), bool *no_add_attrs)
6855 if (TREE_CODE (*node) == FUNCTION_DECL)
6857 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6859 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6860 "with attribute %qs", name, "always_inline");
6861 *no_add_attrs = true;
6863 else
6864 DECL_UNINLINABLE (*node) = 1;
6866 else
6868 warning (OPT_Wattributes, "%qE attribute ignored", name);
6869 *no_add_attrs = true;
6872 return NULL_TREE;
6875 /* Handle a "noclone" attribute; arguments as in
6876 struct attribute_spec.handler. */
6878 static tree
6879 handle_noclone_attribute (tree *node, tree name,
6880 tree ARG_UNUSED (args),
6881 int ARG_UNUSED (flags), bool *no_add_attrs)
6883 if (TREE_CODE (*node) != FUNCTION_DECL)
6885 warning (OPT_Wattributes, "%qE attribute ignored", name);
6886 *no_add_attrs = true;
6889 return NULL_TREE;
6892 /* Handle a "no_icf" attribute; arguments as in
6893 struct attribute_spec.handler. */
6895 static tree
6896 handle_noicf_attribute (tree *node, tree name,
6897 tree ARG_UNUSED (args),
6898 int ARG_UNUSED (flags), bool *no_add_attrs)
6900 if (TREE_CODE (*node) != FUNCTION_DECL)
6902 warning (OPT_Wattributes, "%qE attribute ignored", name);
6903 *no_add_attrs = true;
6906 return NULL_TREE;
6910 /* Handle a "always_inline" attribute; arguments as in
6911 struct attribute_spec.handler. */
6913 static tree
6914 handle_always_inline_attribute (tree *node, tree name,
6915 tree ARG_UNUSED (args),
6916 int ARG_UNUSED (flags),
6917 bool *no_add_attrs)
6919 if (TREE_CODE (*node) == FUNCTION_DECL)
6921 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6923 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6924 "with %qs attribute", name, "noinline");
6925 *no_add_attrs = true;
6927 else
6928 /* Set the attribute and mark it for disregarding inline
6929 limits. */
6930 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6932 else
6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
6935 *no_add_attrs = true;
6938 return NULL_TREE;
6941 /* Handle a "gnu_inline" attribute; arguments as in
6942 struct attribute_spec.handler. */
6944 static tree
6945 handle_gnu_inline_attribute (tree *node, tree name,
6946 tree ARG_UNUSED (args),
6947 int ARG_UNUSED (flags),
6948 bool *no_add_attrs)
6950 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6952 /* Do nothing else, just set the attribute. We'll get at
6953 it later with lookup_attribute. */
6955 else
6957 warning (OPT_Wattributes, "%qE attribute ignored", name);
6958 *no_add_attrs = true;
6961 return NULL_TREE;
6964 /* Handle a "leaf" attribute; arguments as in
6965 struct attribute_spec.handler. */
6967 static tree
6968 handle_leaf_attribute (tree *node, tree name,
6969 tree ARG_UNUSED (args),
6970 int ARG_UNUSED (flags), bool *no_add_attrs)
6972 if (TREE_CODE (*node) != FUNCTION_DECL)
6974 warning (OPT_Wattributes, "%qE attribute ignored", name);
6975 *no_add_attrs = true;
6977 if (!TREE_PUBLIC (*node))
6979 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6980 *no_add_attrs = true;
6983 return NULL_TREE;
6986 /* Handle an "artificial" attribute; arguments as in
6987 struct attribute_spec.handler. */
6989 static tree
6990 handle_artificial_attribute (tree *node, tree name,
6991 tree ARG_UNUSED (args),
6992 int ARG_UNUSED (flags),
6993 bool *no_add_attrs)
6995 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6997 /* Do nothing else, just set the attribute. We'll get at
6998 it later with lookup_attribute. */
7000 else
7002 warning (OPT_Wattributes, "%qE attribute ignored", name);
7003 *no_add_attrs = true;
7006 return NULL_TREE;
7009 /* Handle a "flatten" attribute; arguments as in
7010 struct attribute_spec.handler. */
7012 static tree
7013 handle_flatten_attribute (tree *node, tree name,
7014 tree args ATTRIBUTE_UNUSED,
7015 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7017 if (TREE_CODE (*node) == FUNCTION_DECL)
7018 /* Do nothing else, just set the attribute. We'll get at
7019 it later with lookup_attribute. */
7021 else
7023 warning (OPT_Wattributes, "%qE attribute ignored", name);
7024 *no_add_attrs = true;
7027 return NULL_TREE;
7030 /* Handle a "warning" or "error" attribute; arguments as in
7031 struct attribute_spec.handler. */
7033 static tree
7034 handle_error_attribute (tree *node, tree name, tree args,
7035 int ARG_UNUSED (flags), bool *no_add_attrs)
7037 if (TREE_CODE (*node) == FUNCTION_DECL
7038 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7039 /* Do nothing else, just set the attribute. We'll get at
7040 it later with lookup_attribute. */
7042 else
7044 warning (OPT_Wattributes, "%qE attribute ignored", name);
7045 *no_add_attrs = true;
7048 return NULL_TREE;
7051 /* Handle a "used" attribute; arguments as in
7052 struct attribute_spec.handler. */
7054 static tree
7055 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7056 int ARG_UNUSED (flags), bool *no_add_attrs)
7058 tree node = *pnode;
7060 if (TREE_CODE (node) == FUNCTION_DECL
7061 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7062 || (TREE_CODE (node) == TYPE_DECL))
7064 TREE_USED (node) = 1;
7065 DECL_PRESERVE_P (node) = 1;
7066 if (TREE_CODE (node) == VAR_DECL)
7067 DECL_READ_P (node) = 1;
7069 else
7071 warning (OPT_Wattributes, "%qE attribute ignored", name);
7072 *no_add_attrs = true;
7075 return NULL_TREE;
7078 /* Handle a "unused" attribute; arguments as in
7079 struct attribute_spec.handler. */
7081 static tree
7082 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7083 int flags, bool *no_add_attrs)
7085 if (DECL_P (*node))
7087 tree decl = *node;
7089 if (TREE_CODE (decl) == PARM_DECL
7090 || TREE_CODE (decl) == VAR_DECL
7091 || TREE_CODE (decl) == FUNCTION_DECL
7092 || TREE_CODE (decl) == LABEL_DECL
7093 || TREE_CODE (decl) == TYPE_DECL)
7095 TREE_USED (decl) = 1;
7096 if (TREE_CODE (decl) == VAR_DECL
7097 || TREE_CODE (decl) == PARM_DECL)
7098 DECL_READ_P (decl) = 1;
7100 else
7102 warning (OPT_Wattributes, "%qE attribute ignored", name);
7103 *no_add_attrs = true;
7106 else
7108 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7109 *node = build_variant_type_copy (*node);
7110 TREE_USED (*node) = 1;
7113 return NULL_TREE;
7116 /* Handle a "externally_visible" attribute; arguments as in
7117 struct attribute_spec.handler. */
7119 static tree
7120 handle_externally_visible_attribute (tree *pnode, tree name,
7121 tree ARG_UNUSED (args),
7122 int ARG_UNUSED (flags),
7123 bool *no_add_attrs)
7125 tree node = *pnode;
7127 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7129 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7130 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7132 warning (OPT_Wattributes,
7133 "%qE attribute have effect only on public objects", name);
7134 *no_add_attrs = true;
7137 else
7139 warning (OPT_Wattributes, "%qE attribute ignored", name);
7140 *no_add_attrs = true;
7143 return NULL_TREE;
7146 /* Handle the "no_reorder" attribute. Arguments as in
7147 struct attribute_spec.handler. */
7149 static tree
7150 handle_no_reorder_attribute (tree *pnode,
7151 tree name,
7152 tree,
7153 int,
7154 bool *no_add_attrs)
7156 tree node = *pnode;
7158 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7159 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7161 warning (OPT_Wattributes,
7162 "%qE attribute only affects top level objects",
7163 name);
7164 *no_add_attrs = true;
7167 return NULL_TREE;
7170 /* Handle a "const" attribute; arguments as in
7171 struct attribute_spec.handler. */
7173 static tree
7174 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7175 int ARG_UNUSED (flags), bool *no_add_attrs)
7177 tree type = TREE_TYPE (*node);
7179 /* See FIXME comment on noreturn in c_common_attribute_table. */
7180 if (TREE_CODE (*node) == FUNCTION_DECL)
7181 TREE_READONLY (*node) = 1;
7182 else if (TREE_CODE (type) == POINTER_TYPE
7183 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7184 TREE_TYPE (*node)
7185 = (build_qualified_type
7186 (build_pointer_type
7187 (build_type_variant (TREE_TYPE (type), 1,
7188 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7189 TYPE_QUALS (type)));
7190 else
7192 warning (OPT_Wattributes, "%qE attribute ignored", name);
7193 *no_add_attrs = true;
7196 return NULL_TREE;
7199 /* Handle a "transparent_union" attribute; arguments as in
7200 struct attribute_spec.handler. */
7202 static tree
7203 handle_transparent_union_attribute (tree *node, tree name,
7204 tree ARG_UNUSED (args), int flags,
7205 bool *no_add_attrs)
7207 tree type;
7209 *no_add_attrs = true;
7212 if (TREE_CODE (*node) == TYPE_DECL
7213 && ! (flags & ATTR_FLAG_CXX11))
7214 node = &TREE_TYPE (*node);
7215 type = *node;
7217 if (TREE_CODE (type) == UNION_TYPE)
7219 /* Make sure that the first field will work for a transparent union.
7220 If the type isn't complete yet, leave the check to the code in
7221 finish_struct. */
7222 if (TYPE_SIZE (type))
7224 tree first = first_field (type);
7225 if (first == NULL_TREE
7226 || DECL_ARTIFICIAL (first)
7227 || TYPE_MODE (type) != DECL_MODE (first))
7228 goto ignored;
7231 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7233 /* If the type isn't complete yet, setting the flag
7234 on a variant wouldn't ever be checked. */
7235 if (!TYPE_SIZE (type))
7236 goto ignored;
7238 /* build_duplicate_type doesn't work for C++. */
7239 if (c_dialect_cxx ())
7240 goto ignored;
7242 /* A type variant isn't good enough, since we don't a cast
7243 to such a type removed as a no-op. */
7244 *node = type = build_duplicate_type (type);
7247 TYPE_TRANSPARENT_AGGR (type) = 1;
7248 return NULL_TREE;
7251 ignored:
7252 warning (OPT_Wattributes, "%qE attribute ignored", name);
7253 return NULL_TREE;
7256 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7257 get the requested priority for a constructor or destructor,
7258 possibly issuing diagnostics for invalid or reserved
7259 priorities. */
7261 static priority_type
7262 get_priority (tree args, bool is_destructor)
7264 HOST_WIDE_INT pri;
7265 tree arg;
7267 if (!args)
7268 return DEFAULT_INIT_PRIORITY;
7270 if (!SUPPORTS_INIT_PRIORITY)
7272 if (is_destructor)
7273 error ("destructor priorities are not supported");
7274 else
7275 error ("constructor priorities are not supported");
7276 return DEFAULT_INIT_PRIORITY;
7279 arg = TREE_VALUE (args);
7280 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7281 goto invalid;
7282 if (arg == error_mark_node)
7283 return DEFAULT_INIT_PRIORITY;
7284 arg = default_conversion (arg);
7285 if (!tree_fits_shwi_p (arg)
7286 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7287 goto invalid;
7289 pri = tree_to_shwi (arg);
7290 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7291 goto invalid;
7293 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7295 if (is_destructor)
7296 warning (0,
7297 "destructor priorities from 0 to %d are reserved "
7298 "for the implementation",
7299 MAX_RESERVED_INIT_PRIORITY);
7300 else
7301 warning (0,
7302 "constructor priorities from 0 to %d are reserved "
7303 "for the implementation",
7304 MAX_RESERVED_INIT_PRIORITY);
7306 return pri;
7308 invalid:
7309 if (is_destructor)
7310 error ("destructor priorities must be integers from 0 to %d inclusive",
7311 MAX_INIT_PRIORITY);
7312 else
7313 error ("constructor priorities must be integers from 0 to %d inclusive",
7314 MAX_INIT_PRIORITY);
7315 return DEFAULT_INIT_PRIORITY;
7318 /* Handle a "constructor" attribute; arguments as in
7319 struct attribute_spec.handler. */
7321 static tree
7322 handle_constructor_attribute (tree *node, tree name, tree args,
7323 int ARG_UNUSED (flags),
7324 bool *no_add_attrs)
7326 tree decl = *node;
7327 tree type = TREE_TYPE (decl);
7329 if (TREE_CODE (decl) == FUNCTION_DECL
7330 && TREE_CODE (type) == FUNCTION_TYPE
7331 && decl_function_context (decl) == 0)
7333 priority_type priority;
7334 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7335 priority = get_priority (args, /*is_destructor=*/false);
7336 SET_DECL_INIT_PRIORITY (decl, priority);
7337 TREE_USED (decl) = 1;
7339 else
7341 warning (OPT_Wattributes, "%qE attribute ignored", name);
7342 *no_add_attrs = true;
7345 return NULL_TREE;
7348 /* Handle a "destructor" attribute; arguments as in
7349 struct attribute_spec.handler. */
7351 static tree
7352 handle_destructor_attribute (tree *node, tree name, tree args,
7353 int ARG_UNUSED (flags),
7354 bool *no_add_attrs)
7356 tree decl = *node;
7357 tree type = TREE_TYPE (decl);
7359 if (TREE_CODE (decl) == FUNCTION_DECL
7360 && TREE_CODE (type) == FUNCTION_TYPE
7361 && decl_function_context (decl) == 0)
7363 priority_type priority;
7364 DECL_STATIC_DESTRUCTOR (decl) = 1;
7365 priority = get_priority (args, /*is_destructor=*/true);
7366 SET_DECL_FINI_PRIORITY (decl, priority);
7367 TREE_USED (decl) = 1;
7369 else
7371 warning (OPT_Wattributes, "%qE attribute ignored", name);
7372 *no_add_attrs = true;
7375 return NULL_TREE;
7378 /* Nonzero if the mode is a valid vector mode for this architecture.
7379 This returns nonzero even if there is no hardware support for the
7380 vector mode, but we can emulate with narrower modes. */
7382 static int
7383 vector_mode_valid_p (machine_mode mode)
7385 enum mode_class mclass = GET_MODE_CLASS (mode);
7386 machine_mode innermode;
7388 /* Doh! What's going on? */
7389 if (mclass != MODE_VECTOR_INT
7390 && mclass != MODE_VECTOR_FLOAT
7391 && mclass != MODE_VECTOR_FRACT
7392 && mclass != MODE_VECTOR_UFRACT
7393 && mclass != MODE_VECTOR_ACCUM
7394 && mclass != MODE_VECTOR_UACCUM)
7395 return 0;
7397 /* Hardware support. Woo hoo! */
7398 if (targetm.vector_mode_supported_p (mode))
7399 return 1;
7401 innermode = GET_MODE_INNER (mode);
7403 /* We should probably return 1 if requesting V4DI and we have no DI,
7404 but we have V2DI, but this is probably very unlikely. */
7406 /* If we have support for the inner mode, we can safely emulate it.
7407 We may not have V2DI, but me can emulate with a pair of DIs. */
7408 return targetm.scalar_mode_supported_p (innermode);
7412 /* Handle a "mode" attribute; arguments as in
7413 struct attribute_spec.handler. */
7415 static tree
7416 handle_mode_attribute (tree *node, tree name, tree args,
7417 int ARG_UNUSED (flags), bool *no_add_attrs)
7419 tree type = *node;
7420 tree ident = TREE_VALUE (args);
7422 *no_add_attrs = true;
7424 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7425 warning (OPT_Wattributes, "%qE attribute ignored", name);
7426 else
7428 int j;
7429 const char *p = IDENTIFIER_POINTER (ident);
7430 int len = strlen (p);
7431 machine_mode mode = VOIDmode;
7432 tree typefm;
7433 bool valid_mode;
7435 if (len > 4 && p[0] == '_' && p[1] == '_'
7436 && p[len - 1] == '_' && p[len - 2] == '_')
7438 char *newp = (char *) alloca (len - 1);
7440 strcpy (newp, &p[2]);
7441 newp[len - 4] = '\0';
7442 p = newp;
7445 /* Change this type to have a type with the specified mode.
7446 First check for the special modes. */
7447 if (!strcmp (p, "byte"))
7448 mode = byte_mode;
7449 else if (!strcmp (p, "word"))
7450 mode = word_mode;
7451 else if (!strcmp (p, "pointer"))
7452 mode = ptr_mode;
7453 else if (!strcmp (p, "libgcc_cmp_return"))
7454 mode = targetm.libgcc_cmp_return_mode ();
7455 else if (!strcmp (p, "libgcc_shift_count"))
7456 mode = targetm.libgcc_shift_count_mode ();
7457 else if (!strcmp (p, "unwind_word"))
7458 mode = targetm.unwind_word_mode ();
7459 else
7460 for (j = 0; j < NUM_MACHINE_MODES; j++)
7461 if (!strcmp (p, GET_MODE_NAME (j)))
7463 mode = (machine_mode) j;
7464 break;
7467 if (mode == VOIDmode)
7469 error ("unknown machine mode %qE", ident);
7470 return NULL_TREE;
7473 valid_mode = false;
7474 switch (GET_MODE_CLASS (mode))
7476 case MODE_INT:
7477 case MODE_PARTIAL_INT:
7478 case MODE_FLOAT:
7479 case MODE_DECIMAL_FLOAT:
7480 case MODE_FRACT:
7481 case MODE_UFRACT:
7482 case MODE_ACCUM:
7483 case MODE_UACCUM:
7484 valid_mode = targetm.scalar_mode_supported_p (mode);
7485 break;
7487 case MODE_COMPLEX_INT:
7488 case MODE_COMPLEX_FLOAT:
7489 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7490 break;
7492 case MODE_VECTOR_INT:
7493 case MODE_VECTOR_FLOAT:
7494 case MODE_VECTOR_FRACT:
7495 case MODE_VECTOR_UFRACT:
7496 case MODE_VECTOR_ACCUM:
7497 case MODE_VECTOR_UACCUM:
7498 warning (OPT_Wattributes, "specifying vector types with "
7499 "__attribute__ ((mode)) is deprecated");
7500 warning (OPT_Wattributes,
7501 "use __attribute__ ((vector_size)) instead");
7502 valid_mode = vector_mode_valid_p (mode);
7503 break;
7505 default:
7506 break;
7508 if (!valid_mode)
7510 error ("unable to emulate %qs", p);
7511 return NULL_TREE;
7514 if (POINTER_TYPE_P (type))
7516 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7517 tree (*fn)(tree, machine_mode, bool);
7519 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7521 error ("invalid pointer mode %qs", p);
7522 return NULL_TREE;
7525 if (TREE_CODE (type) == POINTER_TYPE)
7526 fn = build_pointer_type_for_mode;
7527 else
7528 fn = build_reference_type_for_mode;
7529 typefm = fn (TREE_TYPE (type), mode, false);
7531 else
7533 /* For fixed-point modes, we need to test if the signness of type
7534 and the machine mode are consistent. */
7535 if (ALL_FIXED_POINT_MODE_P (mode)
7536 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7538 error ("signedness of type and machine mode %qs don%'t match", p);
7539 return NULL_TREE;
7541 /* For fixed-point modes, we need to pass saturating info. */
7542 typefm = lang_hooks.types.type_for_mode (mode,
7543 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7544 : TYPE_UNSIGNED (type));
7547 if (typefm == NULL_TREE)
7549 error ("no data type for mode %qs", p);
7550 return NULL_TREE;
7552 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7554 /* For enumeral types, copy the precision from the integer
7555 type returned above. If not an INTEGER_TYPE, we can't use
7556 this mode for this type. */
7557 if (TREE_CODE (typefm) != INTEGER_TYPE)
7559 error ("cannot use mode %qs for enumeral types", p);
7560 return NULL_TREE;
7563 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7565 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7566 typefm = type;
7568 else
7570 /* We cannot build a type variant, as there's code that assumes
7571 that TYPE_MAIN_VARIANT has the same mode. This includes the
7572 debug generators. Instead, create a subrange type. This
7573 results in all of the enumeral values being emitted only once
7574 in the original, and the subtype gets them by reference. */
7575 if (TYPE_UNSIGNED (type))
7576 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7577 else
7578 typefm = make_signed_type (TYPE_PRECISION (typefm));
7579 TREE_TYPE (typefm) = type;
7582 else if (VECTOR_MODE_P (mode)
7583 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7584 : TREE_CODE (type) != TREE_CODE (typefm))
7586 error ("mode %qs applied to inappropriate type", p);
7587 return NULL_TREE;
7590 *node = typefm;
7593 return NULL_TREE;
7596 /* Handle a "section" attribute; arguments as in
7597 struct attribute_spec.handler. */
7599 static tree
7600 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7601 int ARG_UNUSED (flags), bool *no_add_attrs)
7603 tree decl = *node;
7605 if (!targetm_common.have_named_sections)
7607 error_at (DECL_SOURCE_LOCATION (*node),
7608 "section attributes are not supported for this target");
7609 goto fail;
7612 user_defined_section_attribute = true;
7614 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
7616 error ("section attribute not allowed for %q+D", *node);
7617 goto fail;
7620 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7622 error ("section attribute argument not a string constant");
7623 goto fail;
7626 if (TREE_CODE (decl) == VAR_DECL
7627 && current_function_decl != NULL_TREE
7628 && !TREE_STATIC (decl))
7630 error_at (DECL_SOURCE_LOCATION (decl),
7631 "section attribute cannot be specified for local variables");
7632 goto fail;
7635 /* The decl may have already been given a section attribute
7636 from a previous declaration. Ensure they match. */
7637 if (DECL_SECTION_NAME (decl) != NULL
7638 && strcmp (DECL_SECTION_NAME (decl),
7639 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7641 error ("section of %q+D conflicts with previous declaration", *node);
7642 goto fail;
7645 if (TREE_CODE (decl) == VAR_DECL
7646 && !targetm.have_tls && targetm.emutls.tmpl_section
7647 && DECL_THREAD_LOCAL_P (decl))
7649 error ("section of %q+D cannot be overridden", *node);
7650 goto fail;
7653 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7654 return NULL_TREE;
7656 fail:
7657 *no_add_attrs = true;
7658 return NULL_TREE;
7661 /* Check whether ALIGN is a valid user-specified alignment. If so,
7662 return its base-2 log; if not, output an error and return -1. If
7663 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7664 no error. */
7666 check_user_alignment (const_tree align, bool allow_zero)
7668 int i;
7670 if (error_operand_p (align))
7671 return -1;
7672 if (TREE_CODE (align) != INTEGER_CST
7673 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7675 error ("requested alignment is not an integer constant");
7676 return -1;
7678 else if (allow_zero && integer_zerop (align))
7679 return -1;
7680 else if (tree_int_cst_sgn (align) == -1
7681 || (i = tree_log2 (align)) == -1)
7683 error ("requested alignment is not a positive power of 2");
7684 return -1;
7686 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7688 error ("requested alignment is too large");
7689 return -1;
7691 return i;
7695 If in c++-11, check if the c++-11 alignment constraint with respect
7696 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7697 c++-11 mode, does nothing.
7699 [dcl.align]2/ says:
7701 [* if the constant expression evaluates to a fundamental alignment,
7702 the alignment requirement of the declared entity shall be the
7703 specified fundamental alignment.
7705 * if the constant expression evaluates to an extended alignment
7706 and the implementation supports that alignment in the context
7707 of the declaration, the alignment of the declared entity shall
7708 be that alignment
7710 * if the constant expression evaluates to an extended alignment
7711 and the implementation does not support that alignment in the
7712 context of the declaration, the program is ill-formed]. */
7714 static bool
7715 check_cxx_fundamental_alignment_constraints (tree node,
7716 unsigned align_log,
7717 int flags)
7719 bool alignment_too_large_p = false;
7720 unsigned requested_alignment = 1U << align_log;
7721 unsigned max_align = 0;
7723 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7724 || (node == NULL_TREE || node == error_mark_node))
7725 return true;
7727 if (cxx_fundamental_alignment_p (requested_alignment))
7728 return true;
7730 if (DECL_P (node))
7732 if (TREE_STATIC (node))
7734 /* For file scope variables and static members, the target
7735 supports alignments that are at most
7736 MAX_OFILE_ALIGNMENT. */
7737 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7738 alignment_too_large_p = true;
7740 else
7742 #ifdef BIGGEST_FIELD_ALIGNMENT
7743 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7744 #else
7745 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7746 #endif
7747 /* For non-static members, the target supports either
7748 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7749 if it is defined or BIGGEST_ALIGNMENT. */
7750 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7751 if (TREE_CODE (node) == FIELD_DECL
7752 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7753 alignment_too_large_p = true;
7754 #undef MAX_TARGET_FIELD_ALIGNMENT
7755 /* For stack variables, the target supports at most
7756 MAX_STACK_ALIGNMENT. */
7757 else if (decl_function_context (node) != NULL
7758 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7759 alignment_too_large_p = true;
7762 else if (TYPE_P (node))
7764 /* Let's be liberal for types. */
7765 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7766 alignment_too_large_p = true;
7769 if (alignment_too_large_p)
7770 pedwarn (input_location, OPT_Wattributes,
7771 "requested alignment %d is larger than %d",
7772 requested_alignment, max_align);
7774 return !alignment_too_large_p;
7777 /* Handle a "aligned" attribute; arguments as in
7778 struct attribute_spec.handler. */
7780 static tree
7781 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7782 int flags, bool *no_add_attrs)
7784 tree decl = NULL_TREE;
7785 tree *type = NULL;
7786 int is_type = 0;
7787 tree align_expr;
7788 int i;
7790 if (args)
7792 align_expr = TREE_VALUE (args);
7793 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7794 && TREE_CODE (align_expr) != FUNCTION_DECL)
7795 align_expr = default_conversion (align_expr);
7797 else
7798 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7800 if (DECL_P (*node))
7802 decl = *node;
7803 type = &TREE_TYPE (decl);
7804 is_type = TREE_CODE (*node) == TYPE_DECL;
7806 else if (TYPE_P (*node))
7807 type = node, is_type = 1;
7809 if ((i = check_user_alignment (align_expr, false)) == -1
7810 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7811 *no_add_attrs = true;
7812 else if (is_type)
7814 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7815 /* OK, modify the type in place. */;
7816 /* If we have a TYPE_DECL, then copy the type, so that we
7817 don't accidentally modify a builtin type. See pushdecl. */
7818 else if (decl && TREE_TYPE (decl) != error_mark_node
7819 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7821 tree tt = TREE_TYPE (decl);
7822 *type = build_variant_type_copy (*type);
7823 DECL_ORIGINAL_TYPE (decl) = tt;
7824 TYPE_NAME (*type) = decl;
7825 TREE_USED (*type) = TREE_USED (decl);
7826 TREE_TYPE (decl) = *type;
7828 else
7829 *type = build_variant_type_copy (*type);
7831 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7832 TYPE_USER_ALIGN (*type) = 1;
7834 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7835 && TREE_CODE (decl) != FIELD_DECL)
7837 error ("alignment may not be specified for %q+D", decl);
7838 *no_add_attrs = true;
7840 else if (DECL_USER_ALIGN (decl)
7841 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7842 /* C++-11 [dcl.align/4]:
7844 When multiple alignment-specifiers are specified for an
7845 entity, the alignment requirement shall be set to the
7846 strictest specified alignment.
7848 This formally comes from the c++11 specification but we are
7849 doing it for the GNU attribute syntax as well. */
7850 *no_add_attrs = true;
7851 else if (TREE_CODE (decl) == FUNCTION_DECL
7852 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7854 if (DECL_USER_ALIGN (decl))
7855 error ("alignment for %q+D was previously specified as %d "
7856 "and may not be decreased", decl,
7857 DECL_ALIGN (decl) / BITS_PER_UNIT);
7858 else
7859 error ("alignment for %q+D must be at least %d", decl,
7860 DECL_ALIGN (decl) / BITS_PER_UNIT);
7861 *no_add_attrs = true;
7863 else
7865 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7866 DECL_USER_ALIGN (decl) = 1;
7869 return NULL_TREE;
7872 /* Handle a "weak" attribute; arguments as in
7873 struct attribute_spec.handler. */
7875 static tree
7876 handle_weak_attribute (tree *node, tree name,
7877 tree ARG_UNUSED (args),
7878 int ARG_UNUSED (flags),
7879 bool * ARG_UNUSED (no_add_attrs))
7881 if (TREE_CODE (*node) == FUNCTION_DECL
7882 && DECL_DECLARED_INLINE_P (*node))
7884 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7885 *no_add_attrs = true;
7887 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7889 error ("indirect function %q+D cannot be declared weak", *node);
7890 *no_add_attrs = true;
7891 return NULL_TREE;
7893 else if (TREE_CODE (*node) == FUNCTION_DECL
7894 || TREE_CODE (*node) == VAR_DECL)
7896 struct symtab_node *n = symtab_node::get (*node);
7897 if (n && n->refuse_visibility_changes)
7898 error ("%+D declared weak after being used", *node);
7899 declare_weak (*node);
7901 else
7902 warning (OPT_Wattributes, "%qE attribute ignored", name);
7904 return NULL_TREE;
7907 /* Handle an "alias" or "ifunc" attribute; arguments as in
7908 struct attribute_spec.handler, except that IS_ALIAS tells us
7909 whether this is an alias as opposed to ifunc attribute. */
7911 static tree
7912 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7913 bool *no_add_attrs)
7915 tree decl = *node;
7917 if (TREE_CODE (decl) != FUNCTION_DECL
7918 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7920 warning (OPT_Wattributes, "%qE attribute ignored", name);
7921 *no_add_attrs = true;
7923 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7924 || (TREE_CODE (decl) != FUNCTION_DECL
7925 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7926 /* A static variable declaration is always a tentative definition,
7927 but the alias is a non-tentative definition which overrides. */
7928 || (TREE_CODE (decl) != FUNCTION_DECL
7929 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7931 error ("%q+D defined both normally and as %qE attribute", decl, name);
7932 *no_add_attrs = true;
7933 return NULL_TREE;
7935 else if (!is_alias
7936 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7937 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7939 error ("weak %q+D cannot be defined %qE", decl, name);
7940 *no_add_attrs = true;
7941 return NULL_TREE;
7944 /* Note that the very first time we process a nested declaration,
7945 decl_function_context will not be set. Indeed, *would* never
7946 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7947 we do below. After such frobbery, pushdecl would set the context.
7948 In any case, this is never what we want. */
7949 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7951 tree id;
7953 id = TREE_VALUE (args);
7954 if (TREE_CODE (id) != STRING_CST)
7956 error ("attribute %qE argument not a string", name);
7957 *no_add_attrs = true;
7958 return NULL_TREE;
7960 id = get_identifier (TREE_STRING_POINTER (id));
7961 /* This counts as a use of the object pointed to. */
7962 TREE_USED (id) = 1;
7964 if (TREE_CODE (decl) == FUNCTION_DECL)
7965 DECL_INITIAL (decl) = error_mark_node;
7966 else
7967 TREE_STATIC (decl) = 1;
7969 if (!is_alias)
7970 /* ifuncs are also aliases, so set that attribute too. */
7971 DECL_ATTRIBUTES (decl)
7972 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7974 else
7976 warning (OPT_Wattributes, "%qE attribute ignored", name);
7977 *no_add_attrs = true;
7980 if (decl_in_symtab_p (*node))
7982 struct symtab_node *n = symtab_node::get (decl);
7983 if (n && n->refuse_visibility_changes)
7985 if (is_alias)
7986 error ("%+D declared alias after being used", decl);
7987 else
7988 error ("%+D declared ifunc after being used", decl);
7993 return NULL_TREE;
7996 /* Handle an "alias" or "ifunc" attribute; arguments as in
7997 struct attribute_spec.handler. */
7999 static tree
8000 handle_ifunc_attribute (tree *node, tree name, tree args,
8001 int ARG_UNUSED (flags), bool *no_add_attrs)
8003 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8006 /* Handle an "alias" or "ifunc" attribute; arguments as in
8007 struct attribute_spec.handler. */
8009 static tree
8010 handle_alias_attribute (tree *node, tree name, tree args,
8011 int ARG_UNUSED (flags), bool *no_add_attrs)
8013 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8016 /* Handle a "weakref" attribute; arguments as in struct
8017 attribute_spec.handler. */
8019 static tree
8020 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8021 int flags, bool *no_add_attrs)
8023 tree attr = NULL_TREE;
8025 /* We must ignore the attribute when it is associated with
8026 local-scoped decls, since attribute alias is ignored and many
8027 such symbols do not even have a DECL_WEAK field. */
8028 if (decl_function_context (*node)
8029 || current_function_decl
8030 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8032 warning (OPT_Wattributes, "%qE attribute ignored", name);
8033 *no_add_attrs = true;
8034 return NULL_TREE;
8037 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8039 error ("indirect function %q+D cannot be declared weakref", *node);
8040 *no_add_attrs = true;
8041 return NULL_TREE;
8044 /* The idea here is that `weakref("name")' mutates into `weakref,
8045 alias("name")', and weakref without arguments, in turn,
8046 implicitly adds weak. */
8048 if (args)
8050 attr = tree_cons (get_identifier ("alias"), args, attr);
8051 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8053 *no_add_attrs = true;
8055 decl_attributes (node, attr, flags);
8057 else
8059 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8060 error_at (DECL_SOURCE_LOCATION (*node),
8061 "weakref attribute must appear before alias attribute");
8063 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8064 and that isn't supported; and because it wants to add it to
8065 the list of weak decls, which isn't helpful. */
8066 DECL_WEAK (*node) = 1;
8069 if (decl_in_symtab_p (*node))
8071 struct symtab_node *n = symtab_node::get (*node);
8072 if (n && n->refuse_visibility_changes)
8073 error ("%+D declared weakref after being used", *node);
8076 return NULL_TREE;
8079 /* Handle an "visibility" attribute; arguments as in
8080 struct attribute_spec.handler. */
8082 static tree
8083 handle_visibility_attribute (tree *node, tree name, tree args,
8084 int ARG_UNUSED (flags),
8085 bool *ARG_UNUSED (no_add_attrs))
8087 tree decl = *node;
8088 tree id = TREE_VALUE (args);
8089 enum symbol_visibility vis;
8091 if (TYPE_P (*node))
8093 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8094 /* OK */;
8095 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8097 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8098 name);
8099 return NULL_TREE;
8101 else if (TYPE_FIELDS (*node))
8103 error ("%qE attribute ignored because %qT is already defined",
8104 name, *node);
8105 return NULL_TREE;
8108 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8110 warning (OPT_Wattributes, "%qE attribute ignored", name);
8111 return NULL_TREE;
8114 if (TREE_CODE (id) != STRING_CST)
8116 error ("visibility argument not a string");
8117 return NULL_TREE;
8120 /* If this is a type, set the visibility on the type decl. */
8121 if (TYPE_P (decl))
8123 decl = TYPE_NAME (decl);
8124 if (!decl)
8125 return NULL_TREE;
8126 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8128 warning (OPT_Wattributes, "%qE attribute ignored on types",
8129 name);
8130 return NULL_TREE;
8134 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8135 vis = VISIBILITY_DEFAULT;
8136 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8137 vis = VISIBILITY_INTERNAL;
8138 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8139 vis = VISIBILITY_HIDDEN;
8140 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8141 vis = VISIBILITY_PROTECTED;
8142 else
8144 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8145 vis = VISIBILITY_DEFAULT;
8148 if (DECL_VISIBILITY_SPECIFIED (decl)
8149 && vis != DECL_VISIBILITY (decl))
8151 tree attributes = (TYPE_P (*node)
8152 ? TYPE_ATTRIBUTES (*node)
8153 : DECL_ATTRIBUTES (decl));
8154 if (lookup_attribute ("visibility", attributes))
8155 error ("%qD redeclared with different visibility", decl);
8156 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8157 && lookup_attribute ("dllimport", attributes))
8158 error ("%qD was declared %qs which implies default visibility",
8159 decl, "dllimport");
8160 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8161 && lookup_attribute ("dllexport", attributes))
8162 error ("%qD was declared %qs which implies default visibility",
8163 decl, "dllexport");
8166 DECL_VISIBILITY (decl) = vis;
8167 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8169 /* Go ahead and attach the attribute to the node as well. This is needed
8170 so we can determine whether we have VISIBILITY_DEFAULT because the
8171 visibility was not specified, or because it was explicitly overridden
8172 from the containing scope. */
8174 return NULL_TREE;
8177 /* Determine the ELF symbol visibility for DECL, which is either a
8178 variable or a function. It is an error to use this function if a
8179 definition of DECL is not available in this translation unit.
8180 Returns true if the final visibility has been determined by this
8181 function; false if the caller is free to make additional
8182 modifications. */
8184 bool
8185 c_determine_visibility (tree decl)
8187 gcc_assert (TREE_CODE (decl) == VAR_DECL
8188 || TREE_CODE (decl) == FUNCTION_DECL);
8190 /* If the user explicitly specified the visibility with an
8191 attribute, honor that. DECL_VISIBILITY will have been set during
8192 the processing of the attribute. We check for an explicit
8193 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8194 to distinguish the use of an attribute from the use of a "#pragma
8195 GCC visibility push(...)"; in the latter case we still want other
8196 considerations to be able to overrule the #pragma. */
8197 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8198 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8199 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8200 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8201 return true;
8203 /* Set default visibility to whatever the user supplied with
8204 visibility_specified depending on #pragma GCC visibility. */
8205 if (!DECL_VISIBILITY_SPECIFIED (decl))
8207 if (visibility_options.inpragma
8208 || DECL_VISIBILITY (decl) != default_visibility)
8210 DECL_VISIBILITY (decl) = default_visibility;
8211 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8212 /* If visibility changed and DECL already has DECL_RTL, ensure
8213 symbol flags are updated. */
8214 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8215 || TREE_CODE (decl) == FUNCTION_DECL)
8216 && DECL_RTL_SET_P (decl))
8217 make_decl_rtl (decl);
8220 return false;
8223 /* Handle an "tls_model" attribute; arguments as in
8224 struct attribute_spec.handler. */
8226 static tree
8227 handle_tls_model_attribute (tree *node, tree name, tree args,
8228 int ARG_UNUSED (flags), bool *no_add_attrs)
8230 tree id;
8231 tree decl = *node;
8232 enum tls_model kind;
8234 *no_add_attrs = true;
8236 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8238 warning (OPT_Wattributes, "%qE attribute ignored", name);
8239 return NULL_TREE;
8242 kind = DECL_TLS_MODEL (decl);
8243 id = TREE_VALUE (args);
8244 if (TREE_CODE (id) != STRING_CST)
8246 error ("tls_model argument not a string");
8247 return NULL_TREE;
8250 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8251 kind = TLS_MODEL_LOCAL_EXEC;
8252 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8253 kind = TLS_MODEL_INITIAL_EXEC;
8254 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8255 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8256 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8257 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8258 else
8259 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8261 set_decl_tls_model (decl, kind);
8262 return NULL_TREE;
8265 /* Handle a "no_instrument_function" attribute; arguments as in
8266 struct attribute_spec.handler. */
8268 static tree
8269 handle_no_instrument_function_attribute (tree *node, tree name,
8270 tree ARG_UNUSED (args),
8271 int ARG_UNUSED (flags),
8272 bool *no_add_attrs)
8274 tree decl = *node;
8276 if (TREE_CODE (decl) != FUNCTION_DECL)
8278 error_at (DECL_SOURCE_LOCATION (decl),
8279 "%qE attribute applies only to functions", name);
8280 *no_add_attrs = true;
8282 else
8283 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8285 return NULL_TREE;
8288 /* Handle a "malloc" attribute; arguments as in
8289 struct attribute_spec.handler. */
8291 static tree
8292 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8293 int ARG_UNUSED (flags), bool *no_add_attrs)
8295 if (TREE_CODE (*node) == FUNCTION_DECL
8296 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8297 DECL_IS_MALLOC (*node) = 1;
8298 else
8300 warning (OPT_Wattributes, "%qE attribute ignored", name);
8301 *no_add_attrs = true;
8304 return NULL_TREE;
8307 /* Handle a "alloc_size" attribute; arguments as in
8308 struct attribute_spec.handler. */
8310 static tree
8311 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8312 int ARG_UNUSED (flags), bool *no_add_attrs)
8314 unsigned arg_count = type_num_arguments (*node);
8315 for (; args; args = TREE_CHAIN (args))
8317 tree position = TREE_VALUE (args);
8318 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8319 && TREE_CODE (position) != FUNCTION_DECL)
8320 position = default_conversion (position);
8322 if (!tree_fits_uhwi_p (position)
8323 || !arg_count
8324 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8326 warning (OPT_Wattributes,
8327 "alloc_size parameter outside range");
8328 *no_add_attrs = true;
8329 return NULL_TREE;
8332 return NULL_TREE;
8335 /* Handle a "alloc_align" attribute; arguments as in
8336 struct attribute_spec.handler. */
8338 static tree
8339 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8340 bool *no_add_attrs)
8342 unsigned arg_count = type_num_arguments (*node);
8343 tree position = TREE_VALUE (args);
8344 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8345 position = default_conversion (position);
8347 if (!tree_fits_uhwi_p (position)
8348 || !arg_count
8349 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8351 warning (OPT_Wattributes,
8352 "alloc_align parameter outside range");
8353 *no_add_attrs = true;
8354 return NULL_TREE;
8356 return NULL_TREE;
8359 /* Handle a "assume_aligned" attribute; arguments as in
8360 struct attribute_spec.handler. */
8362 static tree
8363 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8364 bool *no_add_attrs)
8366 for (; args; args = TREE_CHAIN (args))
8368 tree position = TREE_VALUE (args);
8369 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8370 && TREE_CODE (position) != FUNCTION_DECL)
8371 position = default_conversion (position);
8373 if (TREE_CODE (position) != INTEGER_CST)
8375 warning (OPT_Wattributes,
8376 "assume_aligned parameter not integer constant");
8377 *no_add_attrs = true;
8378 return NULL_TREE;
8381 return NULL_TREE;
8384 /* Handle a "fn spec" attribute; arguments as in
8385 struct attribute_spec.handler. */
8387 static tree
8388 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8389 tree args, int ARG_UNUSED (flags),
8390 bool *no_add_attrs ATTRIBUTE_UNUSED)
8392 gcc_assert (args
8393 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8394 && !TREE_CHAIN (args));
8395 return NULL_TREE;
8398 /* Handle a "bnd_variable_size" attribute; arguments as in
8399 struct attribute_spec.handler. */
8401 static tree
8402 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8403 int ARG_UNUSED (flags), bool *no_add_attrs)
8405 if (TREE_CODE (*node) != FIELD_DECL)
8407 warning (OPT_Wattributes, "%qE attribute ignored", name);
8408 *no_add_attrs = true;
8411 return NULL_TREE;
8414 /* Handle a "bnd_legacy" attribute; arguments as in
8415 struct attribute_spec.handler. */
8417 static tree
8418 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8419 int ARG_UNUSED (flags), bool *no_add_attrs)
8421 if (TREE_CODE (*node) != FUNCTION_DECL)
8423 warning (OPT_Wattributes, "%qE attribute ignored", name);
8424 *no_add_attrs = true;
8427 return NULL_TREE;
8430 /* Handle a "bnd_instrument" attribute; arguments as in
8431 struct attribute_spec.handler. */
8433 static tree
8434 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8435 int ARG_UNUSED (flags), bool *no_add_attrs)
8437 if (TREE_CODE (*node) != FUNCTION_DECL)
8439 warning (OPT_Wattributes, "%qE attribute ignored", name);
8440 *no_add_attrs = true;
8443 return NULL_TREE;
8446 /* Handle a "warn_unused" attribute; arguments as in
8447 struct attribute_spec.handler. */
8449 static tree
8450 handle_warn_unused_attribute (tree *node, tree name,
8451 tree args ATTRIBUTE_UNUSED,
8452 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8454 if (TYPE_P (*node))
8455 /* Do nothing else, just set the attribute. We'll get at
8456 it later with lookup_attribute. */
8458 else
8460 warning (OPT_Wattributes, "%qE attribute ignored", name);
8461 *no_add_attrs = true;
8464 return NULL_TREE;
8467 /* Handle an "omp declare simd" attribute; arguments as in
8468 struct attribute_spec.handler. */
8470 static tree
8471 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8473 return NULL_TREE;
8476 /* Handle an "omp declare target" attribute; arguments as in
8477 struct attribute_spec.handler. */
8479 static tree
8480 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8482 return NULL_TREE;
8485 /* Handle a "returns_twice" attribute; arguments as in
8486 struct attribute_spec.handler. */
8488 static tree
8489 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8490 int ARG_UNUSED (flags), bool *no_add_attrs)
8492 if (TREE_CODE (*node) == FUNCTION_DECL)
8493 DECL_IS_RETURNS_TWICE (*node) = 1;
8494 else
8496 warning (OPT_Wattributes, "%qE attribute ignored", name);
8497 *no_add_attrs = true;
8500 return NULL_TREE;
8503 /* Handle a "no_limit_stack" attribute; arguments as in
8504 struct attribute_spec.handler. */
8506 static tree
8507 handle_no_limit_stack_attribute (tree *node, tree name,
8508 tree ARG_UNUSED (args),
8509 int ARG_UNUSED (flags),
8510 bool *no_add_attrs)
8512 tree decl = *node;
8514 if (TREE_CODE (decl) != FUNCTION_DECL)
8516 error_at (DECL_SOURCE_LOCATION (decl),
8517 "%qE attribute applies only to functions", name);
8518 *no_add_attrs = true;
8520 else if (DECL_INITIAL (decl))
8522 error_at (DECL_SOURCE_LOCATION (decl),
8523 "can%'t set %qE attribute after definition", name);
8524 *no_add_attrs = true;
8526 else
8527 DECL_NO_LIMIT_STACK (decl) = 1;
8529 return NULL_TREE;
8532 /* Handle a "pure" attribute; arguments as in
8533 struct attribute_spec.handler. */
8535 static tree
8536 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8537 int ARG_UNUSED (flags), bool *no_add_attrs)
8539 if (TREE_CODE (*node) == FUNCTION_DECL)
8540 DECL_PURE_P (*node) = 1;
8541 /* ??? TODO: Support types. */
8542 else
8544 warning (OPT_Wattributes, "%qE attribute ignored", name);
8545 *no_add_attrs = true;
8548 return NULL_TREE;
8551 /* Digest an attribute list destined for a transactional memory statement.
8552 ALLOWED is the set of attributes that are allowed for this statement;
8553 return the attribute we parsed. Multiple attributes are never allowed. */
8556 parse_tm_stmt_attr (tree attrs, int allowed)
8558 tree a_seen = NULL;
8559 int m_seen = 0;
8561 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8563 tree a = TREE_PURPOSE (attrs);
8564 int m = 0;
8566 if (is_attribute_p ("outer", a))
8567 m = TM_STMT_ATTR_OUTER;
8569 if ((m & allowed) == 0)
8571 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8572 continue;
8575 if (m_seen == 0)
8577 a_seen = a;
8578 m_seen = m;
8580 else if (m_seen == m)
8581 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8582 else
8583 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8586 return m_seen;
8589 /* Transform a TM attribute name into a maskable integer and back.
8590 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8591 to how the lack of an attribute is treated. */
8594 tm_attr_to_mask (tree attr)
8596 if (attr == NULL)
8597 return 0;
8598 if (is_attribute_p ("transaction_safe", attr))
8599 return TM_ATTR_SAFE;
8600 if (is_attribute_p ("transaction_callable", attr))
8601 return TM_ATTR_CALLABLE;
8602 if (is_attribute_p ("transaction_pure", attr))
8603 return TM_ATTR_PURE;
8604 if (is_attribute_p ("transaction_unsafe", attr))
8605 return TM_ATTR_IRREVOCABLE;
8606 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8607 return TM_ATTR_MAY_CANCEL_OUTER;
8608 return 0;
8611 tree
8612 tm_mask_to_attr (int mask)
8614 const char *str;
8615 switch (mask)
8617 case TM_ATTR_SAFE:
8618 str = "transaction_safe";
8619 break;
8620 case TM_ATTR_CALLABLE:
8621 str = "transaction_callable";
8622 break;
8623 case TM_ATTR_PURE:
8624 str = "transaction_pure";
8625 break;
8626 case TM_ATTR_IRREVOCABLE:
8627 str = "transaction_unsafe";
8628 break;
8629 case TM_ATTR_MAY_CANCEL_OUTER:
8630 str = "transaction_may_cancel_outer";
8631 break;
8632 default:
8633 gcc_unreachable ();
8635 return get_identifier (str);
8638 /* Return the first TM attribute seen in LIST. */
8640 tree
8641 find_tm_attribute (tree list)
8643 for (; list ; list = TREE_CHAIN (list))
8645 tree name = TREE_PURPOSE (list);
8646 if (tm_attr_to_mask (name) != 0)
8647 return name;
8649 return NULL_TREE;
8652 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8653 Here we accept only function types, and verify that none of the other
8654 function TM attributes are also applied. */
8655 /* ??? We need to accept class types for C++, but not C. This greatly
8656 complicates this function, since we can no longer rely on the extra
8657 processing given by function_type_required. */
8659 static tree
8660 handle_tm_attribute (tree *node, tree name, tree args,
8661 int flags, bool *no_add_attrs)
8663 /* Only one path adds the attribute; others don't. */
8664 *no_add_attrs = true;
8666 switch (TREE_CODE (*node))
8668 case RECORD_TYPE:
8669 case UNION_TYPE:
8670 /* Only tm_callable and tm_safe apply to classes. */
8671 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8672 goto ignored;
8673 /* FALLTHRU */
8675 case FUNCTION_TYPE:
8676 case METHOD_TYPE:
8678 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8679 if (old_name == name)
8681 else if (old_name != NULL_TREE)
8682 error ("type was previously declared %qE", old_name);
8683 else
8684 *no_add_attrs = false;
8686 break;
8688 case POINTER_TYPE:
8690 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8691 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8693 tree fn_tmp = TREE_TYPE (*node);
8694 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8695 *node = build_pointer_type (fn_tmp);
8696 break;
8699 /* FALLTHRU */
8701 default:
8702 /* If a function is next, pass it on to be tried next. */
8703 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8704 return tree_cons (name, args, NULL);
8706 ignored:
8707 warning (OPT_Wattributes, "%qE attribute ignored", name);
8708 break;
8711 return NULL_TREE;
8714 /* Handle the TM_WRAP attribute; arguments as in
8715 struct attribute_spec.handler. */
8717 static tree
8718 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8719 int ARG_UNUSED (flags), bool *no_add_attrs)
8721 tree decl = *node;
8723 /* We don't need the attribute even on success, since we
8724 record the entry in an external table. */
8725 *no_add_attrs = true;
8727 if (TREE_CODE (decl) != FUNCTION_DECL)
8728 warning (OPT_Wattributes, "%qE attribute ignored", name);
8729 else
8731 tree wrap_decl = TREE_VALUE (args);
8732 if (error_operand_p (wrap_decl))
8734 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8735 && TREE_CODE (wrap_decl) != VAR_DECL
8736 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8737 error ("%qE argument not an identifier", name);
8738 else
8740 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8741 wrap_decl = lookup_name (wrap_decl);
8742 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8744 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8745 TREE_TYPE (wrap_decl)))
8746 record_tm_replacement (wrap_decl, decl);
8747 else
8748 error ("%qD is not compatible with %qD", wrap_decl, decl);
8750 else
8751 error ("%qE argument is not a function", name);
8755 return NULL_TREE;
8758 /* Ignore the given attribute. Used when this attribute may be usefully
8759 overridden by the target, but is not used generically. */
8761 static tree
8762 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8763 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8764 bool *no_add_attrs)
8766 *no_add_attrs = true;
8767 return NULL_TREE;
8770 /* Handle a "no vops" attribute; arguments as in
8771 struct attribute_spec.handler. */
8773 static tree
8774 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8775 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8776 bool *ARG_UNUSED (no_add_attrs))
8778 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8779 DECL_IS_NOVOPS (*node) = 1;
8780 return NULL_TREE;
8783 /* Handle a "deprecated" attribute; arguments as in
8784 struct attribute_spec.handler. */
8786 static tree
8787 handle_deprecated_attribute (tree *node, tree name,
8788 tree args, int flags,
8789 bool *no_add_attrs)
8791 tree type = NULL_TREE;
8792 int warn = 0;
8793 tree what = NULL_TREE;
8795 if (!args)
8796 *no_add_attrs = true;
8797 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8799 error ("deprecated message is not a string");
8800 *no_add_attrs = true;
8803 if (DECL_P (*node))
8805 tree decl = *node;
8806 type = TREE_TYPE (decl);
8808 if (TREE_CODE (decl) == TYPE_DECL
8809 || TREE_CODE (decl) == PARM_DECL
8810 || TREE_CODE (decl) == VAR_DECL
8811 || TREE_CODE (decl) == FUNCTION_DECL
8812 || TREE_CODE (decl) == FIELD_DECL
8813 || objc_method_decl (TREE_CODE (decl)))
8814 TREE_DEPRECATED (decl) = 1;
8815 else
8816 warn = 1;
8818 else if (TYPE_P (*node))
8820 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8821 *node = build_variant_type_copy (*node);
8822 TREE_DEPRECATED (*node) = 1;
8823 type = *node;
8825 else
8826 warn = 1;
8828 if (warn)
8830 *no_add_attrs = true;
8831 if (type && TYPE_NAME (type))
8833 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8834 what = TYPE_NAME (*node);
8835 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8836 && DECL_NAME (TYPE_NAME (type)))
8837 what = DECL_NAME (TYPE_NAME (type));
8839 if (what)
8840 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8841 else
8842 warning (OPT_Wattributes, "%qE attribute ignored", name);
8845 return NULL_TREE;
8848 /* Handle a "vector_size" attribute; arguments as in
8849 struct attribute_spec.handler. */
8851 static tree
8852 handle_vector_size_attribute (tree *node, tree name, tree args,
8853 int ARG_UNUSED (flags),
8854 bool *no_add_attrs)
8856 unsigned HOST_WIDE_INT vecsize, nunits;
8857 machine_mode orig_mode;
8858 tree type = *node, new_type, size;
8860 *no_add_attrs = true;
8862 size = TREE_VALUE (args);
8863 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8864 && TREE_CODE (size) != FUNCTION_DECL)
8865 size = default_conversion (size);
8867 if (!tree_fits_uhwi_p (size))
8869 warning (OPT_Wattributes, "%qE attribute ignored", name);
8870 return NULL_TREE;
8873 /* Get the vector size (in bytes). */
8874 vecsize = tree_to_uhwi (size);
8876 /* We need to provide for vector pointers, vector arrays, and
8877 functions returning vectors. For example:
8879 __attribute__((vector_size(16))) short *foo;
8881 In this case, the mode is SI, but the type being modified is
8882 HI, so we need to look further. */
8884 while (POINTER_TYPE_P (type)
8885 || TREE_CODE (type) == FUNCTION_TYPE
8886 || TREE_CODE (type) == METHOD_TYPE
8887 || TREE_CODE (type) == ARRAY_TYPE
8888 || TREE_CODE (type) == OFFSET_TYPE)
8889 type = TREE_TYPE (type);
8891 /* Get the mode of the type being modified. */
8892 orig_mode = TYPE_MODE (type);
8894 if ((!INTEGRAL_TYPE_P (type)
8895 && !SCALAR_FLOAT_TYPE_P (type)
8896 && !FIXED_POINT_TYPE_P (type))
8897 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8898 && GET_MODE_CLASS (orig_mode) != MODE_INT
8899 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8900 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8901 || TREE_CODE (type) == BOOLEAN_TYPE)
8903 error ("invalid vector type for attribute %qE", name);
8904 return NULL_TREE;
8907 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8909 error ("vector size not an integral multiple of component size");
8910 return NULL;
8913 if (vecsize == 0)
8915 error ("zero vector size");
8916 return NULL;
8919 /* Calculate how many units fit in the vector. */
8920 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8921 if (nunits & (nunits - 1))
8923 error ("number of components of the vector not a power of two");
8924 return NULL_TREE;
8927 new_type = build_vector_type (type, nunits);
8929 /* Build back pointers if needed. */
8930 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8932 return NULL_TREE;
8935 /* Handle the "nonnull" attribute. */
8936 static tree
8937 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8938 tree args, int ARG_UNUSED (flags),
8939 bool *no_add_attrs)
8941 tree type = *node;
8942 unsigned HOST_WIDE_INT attr_arg_num;
8944 /* If no arguments are specified, all pointer arguments should be
8945 non-null. Verify a full prototype is given so that the arguments
8946 will have the correct types when we actually check them later. */
8947 if (!args)
8949 if (!prototype_p (type))
8951 error ("nonnull attribute without arguments on a non-prototype");
8952 *no_add_attrs = true;
8954 return NULL_TREE;
8957 /* Argument list specified. Verify that each argument number references
8958 a pointer argument. */
8959 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8961 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8963 tree arg = TREE_VALUE (args);
8964 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8965 && TREE_CODE (arg) != FUNCTION_DECL)
8966 arg = default_conversion (arg);
8968 if (!get_nonnull_operand (arg, &arg_num))
8970 error ("nonnull argument has invalid operand number (argument %lu)",
8971 (unsigned long) attr_arg_num);
8972 *no_add_attrs = true;
8973 return NULL_TREE;
8976 if (prototype_p (type))
8978 function_args_iterator iter;
8979 tree argument;
8981 function_args_iter_init (&iter, type);
8982 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8984 argument = function_args_iter_cond (&iter);
8985 if (argument == NULL_TREE || ck_num == arg_num)
8986 break;
8989 if (!argument
8990 || TREE_CODE (argument) == VOID_TYPE)
8992 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8993 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8994 *no_add_attrs = true;
8995 return NULL_TREE;
8998 if (TREE_CODE (argument) != POINTER_TYPE)
9000 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9001 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9002 *no_add_attrs = true;
9003 return NULL_TREE;
9008 return NULL_TREE;
9011 /* Check the argument list of a function call for null in argument slots
9012 that are marked as requiring a non-null pointer argument. The NARGS
9013 arguments are passed in the array ARGARRAY.
9016 static void
9017 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9019 tree a;
9020 int i;
9022 attrs = lookup_attribute ("nonnull", attrs);
9023 if (attrs == NULL_TREE)
9024 return;
9026 a = attrs;
9027 /* See if any of the nonnull attributes has no arguments. If so,
9028 then every pointer argument is checked (in which case the check
9029 for pointer type is done in check_nonnull_arg). */
9030 if (TREE_VALUE (a) != NULL_TREE)
9032 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9033 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9035 if (a != NULL_TREE)
9036 for (i = 0; i < nargs; i++)
9037 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9038 i + 1);
9039 else
9041 /* Walk the argument list. If we encounter an argument number we
9042 should check for non-null, do it. */
9043 for (i = 0; i < nargs; i++)
9045 for (a = attrs; ; a = TREE_CHAIN (a))
9047 a = lookup_attribute ("nonnull", a);
9048 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9049 break;
9052 if (a != NULL_TREE)
9053 check_function_arguments_recurse (check_nonnull_arg, NULL,
9054 argarray[i], i + 1);
9059 /* Check that the Nth argument of a function call (counting backwards
9060 from the end) is a (pointer)0. The NARGS arguments are passed in the
9061 array ARGARRAY. */
9063 static void
9064 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9066 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9068 if (attr)
9070 int len = 0;
9071 int pos = 0;
9072 tree sentinel;
9073 function_args_iterator iter;
9074 tree t;
9076 /* Skip over the named arguments. */
9077 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9079 if (len == nargs)
9080 break;
9081 len++;
9084 if (TREE_VALUE (attr))
9086 tree p = TREE_VALUE (TREE_VALUE (attr));
9087 pos = TREE_INT_CST_LOW (p);
9090 /* The sentinel must be one of the varargs, i.e.
9091 in position >= the number of fixed arguments. */
9092 if ((nargs - 1 - pos) < len)
9094 warning (OPT_Wformat_,
9095 "not enough variable arguments to fit a sentinel");
9096 return;
9099 /* Validate the sentinel. */
9100 sentinel = argarray[nargs - 1 - pos];
9101 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9102 || !integer_zerop (sentinel))
9103 /* Although __null (in C++) is only an integer we allow it
9104 nevertheless, as we are guaranteed that it's exactly
9105 as wide as a pointer, and we don't want to force
9106 users to cast the NULL they have written there.
9107 We warn with -Wstrict-null-sentinel, though. */
9108 && (warn_strict_null_sentinel || null_node != sentinel))
9109 warning (OPT_Wformat_, "missing sentinel in function call");
9113 /* Helper for check_function_nonnull; given a list of operands which
9114 must be non-null in ARGS, determine if operand PARAM_NUM should be
9115 checked. */
9117 static bool
9118 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9120 unsigned HOST_WIDE_INT arg_num = 0;
9122 for (; args; args = TREE_CHAIN (args))
9124 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9126 gcc_assert (found);
9128 if (arg_num == param_num)
9129 return true;
9131 return false;
9134 /* Check that the function argument PARAM (which is operand number
9135 PARAM_NUM) is non-null. This is called by check_function_nonnull
9136 via check_function_arguments_recurse. */
9138 static void
9139 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9140 unsigned HOST_WIDE_INT param_num)
9142 /* Just skip checking the argument if it's not a pointer. This can
9143 happen if the "nonnull" attribute was given without an operand
9144 list (which means to check every pointer argument). */
9146 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9147 return;
9149 if (integer_zerop (param))
9150 warning (OPT_Wnonnull, "null argument where non-null required "
9151 "(argument %lu)", (unsigned long) param_num);
9154 /* Helper for nonnull attribute handling; fetch the operand number
9155 from the attribute argument list. */
9157 static bool
9158 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9160 /* Verify the arg number is a small constant. */
9161 if (tree_fits_uhwi_p (arg_num_expr))
9163 *valp = TREE_INT_CST_LOW (arg_num_expr);
9164 return true;
9166 else
9167 return false;
9170 /* Handle a "nothrow" attribute; arguments as in
9171 struct attribute_spec.handler. */
9173 static tree
9174 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9175 int ARG_UNUSED (flags), bool *no_add_attrs)
9177 if (TREE_CODE (*node) == FUNCTION_DECL)
9178 TREE_NOTHROW (*node) = 1;
9179 /* ??? TODO: Support types. */
9180 else
9182 warning (OPT_Wattributes, "%qE attribute ignored", name);
9183 *no_add_attrs = true;
9186 return NULL_TREE;
9189 /* Handle a "cleanup" attribute; arguments as in
9190 struct attribute_spec.handler. */
9192 static tree
9193 handle_cleanup_attribute (tree *node, tree name, tree args,
9194 int ARG_UNUSED (flags), bool *no_add_attrs)
9196 tree decl = *node;
9197 tree cleanup_id, cleanup_decl;
9199 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9200 for global destructors in C++. This requires infrastructure that
9201 we don't have generically at the moment. It's also not a feature
9202 we'd be missing too much, since we do have attribute constructor. */
9203 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9205 warning (OPT_Wattributes, "%qE attribute ignored", name);
9206 *no_add_attrs = true;
9207 return NULL_TREE;
9210 /* Verify that the argument is a function in scope. */
9211 /* ??? We could support pointers to functions here as well, if
9212 that was considered desirable. */
9213 cleanup_id = TREE_VALUE (args);
9214 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9216 error ("cleanup argument not an identifier");
9217 *no_add_attrs = true;
9218 return NULL_TREE;
9220 cleanup_decl = lookup_name (cleanup_id);
9221 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9223 error ("cleanup argument not a function");
9224 *no_add_attrs = true;
9225 return NULL_TREE;
9228 /* That the function has proper type is checked with the
9229 eventual call to build_function_call. */
9231 return NULL_TREE;
9234 /* Handle a "warn_unused_result" attribute. No special handling. */
9236 static tree
9237 handle_warn_unused_result_attribute (tree *node, tree name,
9238 tree ARG_UNUSED (args),
9239 int ARG_UNUSED (flags), bool *no_add_attrs)
9241 /* Ignore the attribute for functions not returning any value. */
9242 if (VOID_TYPE_P (TREE_TYPE (*node)))
9244 warning (OPT_Wattributes, "%qE attribute ignored", name);
9245 *no_add_attrs = true;
9248 return NULL_TREE;
9251 /* Handle a "sentinel" attribute. */
9253 static tree
9254 handle_sentinel_attribute (tree *node, tree name, tree args,
9255 int ARG_UNUSED (flags), bool *no_add_attrs)
9257 if (!prototype_p (*node))
9259 warning (OPT_Wattributes,
9260 "%qE attribute requires prototypes with named arguments", name);
9261 *no_add_attrs = true;
9263 else
9265 if (!stdarg_p (*node))
9267 warning (OPT_Wattributes,
9268 "%qE attribute only applies to variadic functions", name);
9269 *no_add_attrs = true;
9273 if (args)
9275 tree position = TREE_VALUE (args);
9276 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9277 && TREE_CODE (position) != FUNCTION_DECL)
9278 position = default_conversion (position);
9280 if (TREE_CODE (position) != INTEGER_CST
9281 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9283 warning (OPT_Wattributes,
9284 "requested position is not an integer constant");
9285 *no_add_attrs = true;
9287 else
9289 if (tree_int_cst_lt (position, integer_zero_node))
9291 warning (OPT_Wattributes,
9292 "requested position is less than zero");
9293 *no_add_attrs = true;
9298 return NULL_TREE;
9301 /* Handle a "type_generic" attribute. */
9303 static tree
9304 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9305 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9306 bool * ARG_UNUSED (no_add_attrs))
9308 /* Ensure we have a function type. */
9309 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9311 /* Ensure we have a variadic function. */
9312 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9314 return NULL_TREE;
9317 /* Handle a "target" attribute. */
9319 static tree
9320 handle_target_attribute (tree *node, tree name, tree args, int flags,
9321 bool *no_add_attrs)
9323 /* Ensure we have a function type. */
9324 if (TREE_CODE (*node) != FUNCTION_DECL)
9326 warning (OPT_Wattributes, "%qE attribute ignored", name);
9327 *no_add_attrs = true;
9329 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9330 flags))
9331 *no_add_attrs = true;
9333 return NULL_TREE;
9336 /* Arguments being collected for optimization. */
9337 typedef const char *const_char_p; /* For DEF_VEC_P. */
9338 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9341 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9342 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9343 false for #pragma GCC optimize. */
9345 bool
9346 parse_optimize_options (tree args, bool attr_p)
9348 bool ret = true;
9349 unsigned opt_argc;
9350 unsigned i;
9351 int saved_flag_strict_aliasing;
9352 const char **opt_argv;
9353 struct cl_decoded_option *decoded_options;
9354 unsigned int decoded_options_count;
9355 tree ap;
9357 /* Build up argv vector. Just in case the string is stored away, use garbage
9358 collected strings. */
9359 vec_safe_truncate (optimize_args, 0);
9360 vec_safe_push (optimize_args, (const char *) NULL);
9362 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9364 tree value = TREE_VALUE (ap);
9366 if (TREE_CODE (value) == INTEGER_CST)
9368 char buffer[20];
9369 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9370 vec_safe_push (optimize_args, ggc_strdup (buffer));
9373 else if (TREE_CODE (value) == STRING_CST)
9375 /* Split string into multiple substrings. */
9376 size_t len = TREE_STRING_LENGTH (value);
9377 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9378 char *end = p + len;
9379 char *comma;
9380 char *next_p = p;
9382 while (next_p != NULL)
9384 size_t len2;
9385 char *q, *r;
9387 p = next_p;
9388 comma = strchr (p, ',');
9389 if (comma)
9391 len2 = comma - p;
9392 *comma = '\0';
9393 next_p = comma+1;
9395 else
9397 len2 = end - p;
9398 next_p = NULL;
9401 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9403 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9404 options. */
9405 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9407 ret = false;
9408 if (attr_p)
9409 warning (OPT_Wattributes,
9410 "bad option %s to optimize attribute", p);
9411 else
9412 warning (OPT_Wpragmas,
9413 "bad option %s to pragma attribute", p);
9414 continue;
9417 if (*p != '-')
9419 *r++ = '-';
9421 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9422 itself is -Os, and any other switch begins with a -f. */
9423 if ((*p >= '0' && *p <= '9')
9424 || (p[0] == 's' && p[1] == '\0'))
9425 *r++ = 'O';
9426 else if (*p != 'O')
9427 *r++ = 'f';
9430 memcpy (r, p, len2);
9431 r[len2] = '\0';
9432 vec_safe_push (optimize_args, (const char *) q);
9438 opt_argc = optimize_args->length ();
9439 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9441 for (i = 1; i < opt_argc; i++)
9442 opt_argv[i] = (*optimize_args)[i];
9444 saved_flag_strict_aliasing = flag_strict_aliasing;
9446 /* Now parse the options. */
9447 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9448 &decoded_options,
9449 &decoded_options_count);
9450 decode_options (&global_options, &global_options_set,
9451 decoded_options, decoded_options_count,
9452 input_location, global_dc);
9454 targetm.override_options_after_change();
9456 /* Don't allow changing -fstrict-aliasing. */
9457 flag_strict_aliasing = saved_flag_strict_aliasing;
9459 optimize_args->truncate (0);
9460 return ret;
9463 /* For handling "optimize" attribute. arguments as in
9464 struct attribute_spec.handler. */
9466 static tree
9467 handle_optimize_attribute (tree *node, tree name, tree args,
9468 int ARG_UNUSED (flags), bool *no_add_attrs)
9470 /* Ensure we have a function type. */
9471 if (TREE_CODE (*node) != FUNCTION_DECL)
9473 warning (OPT_Wattributes, "%qE attribute ignored", name);
9474 *no_add_attrs = true;
9476 else
9478 struct cl_optimization cur_opts;
9479 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9481 /* Save current options. */
9482 cl_optimization_save (&cur_opts, &global_options);
9484 /* If we previously had some optimization options, use them as the
9485 default. */
9486 if (old_opts)
9487 cl_optimization_restore (&global_options,
9488 TREE_OPTIMIZATION (old_opts));
9490 /* Parse options, and update the vector. */
9491 parse_optimize_options (args, true);
9492 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9493 = build_optimization_node (&global_options);
9495 /* Restore current options. */
9496 cl_optimization_restore (&global_options, &cur_opts);
9499 return NULL_TREE;
9502 /* Handle a "no_split_stack" attribute. */
9504 static tree
9505 handle_no_split_stack_attribute (tree *node, tree name,
9506 tree ARG_UNUSED (args),
9507 int ARG_UNUSED (flags),
9508 bool *no_add_attrs)
9510 tree decl = *node;
9512 if (TREE_CODE (decl) != FUNCTION_DECL)
9514 error_at (DECL_SOURCE_LOCATION (decl),
9515 "%qE attribute applies only to functions", name);
9516 *no_add_attrs = true;
9518 else if (DECL_INITIAL (decl))
9520 error_at (DECL_SOURCE_LOCATION (decl),
9521 "can%'t set %qE attribute after definition", name);
9522 *no_add_attrs = true;
9525 return NULL_TREE;
9528 /* Handle a "returns_nonnull" attribute; arguments as in
9529 struct attribute_spec.handler. */
9531 static tree
9532 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9533 bool *no_add_attrs)
9535 // Even without a prototype we still have a return type we can check.
9536 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9538 error ("returns_nonnull attribute on a function not returning a pointer");
9539 *no_add_attrs = true;
9541 return NULL_TREE;
9544 /* Handle a "designated_init" attribute; arguments as in
9545 struct attribute_spec.handler. */
9547 static tree
9548 handle_designated_init_attribute (tree *node, tree name, tree, int,
9549 bool *no_add_attrs)
9551 if (TREE_CODE (*node) != RECORD_TYPE)
9553 error ("%qE attribute is only valid on %<struct%> type", name);
9554 *no_add_attrs = true;
9556 return NULL_TREE;
9560 /* Check for valid arguments being passed to a function with FNTYPE.
9561 There are NARGS arguments in the array ARGARRAY. */
9562 void
9563 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9565 /* Check for null being passed in a pointer argument that must be
9566 non-null. We also need to do this if format checking is enabled. */
9568 if (warn_nonnull)
9569 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9571 /* Check for errors in format strings. */
9573 if (warn_format || warn_suggest_attribute_format)
9574 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9576 if (warn_format)
9577 check_function_sentinel (fntype, nargs, argarray);
9580 /* Generic argument checking recursion routine. PARAM is the argument to
9581 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9582 once the argument is resolved. CTX is context for the callback. */
9583 void
9584 check_function_arguments_recurse (void (*callback)
9585 (void *, tree, unsigned HOST_WIDE_INT),
9586 void *ctx, tree param,
9587 unsigned HOST_WIDE_INT param_num)
9589 if (CONVERT_EXPR_P (param)
9590 && (TYPE_PRECISION (TREE_TYPE (param))
9591 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9593 /* Strip coercion. */
9594 check_function_arguments_recurse (callback, ctx,
9595 TREE_OPERAND (param, 0), param_num);
9596 return;
9599 if (TREE_CODE (param) == CALL_EXPR)
9601 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9602 tree attrs;
9603 bool found_format_arg = false;
9605 /* See if this is a call to a known internationalization function
9606 that modifies a format arg. Such a function may have multiple
9607 format_arg attributes (for example, ngettext). */
9609 for (attrs = TYPE_ATTRIBUTES (type);
9610 attrs;
9611 attrs = TREE_CHAIN (attrs))
9612 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9614 tree inner_arg;
9615 tree format_num_expr;
9616 int format_num;
9617 int i;
9618 call_expr_arg_iterator iter;
9620 /* Extract the argument number, which was previously checked
9621 to be valid. */
9622 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9624 format_num = tree_to_uhwi (format_num_expr);
9626 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9627 inner_arg != 0;
9628 inner_arg = next_call_expr_arg (&iter), i++)
9629 if (i == format_num)
9631 check_function_arguments_recurse (callback, ctx,
9632 inner_arg, param_num);
9633 found_format_arg = true;
9634 break;
9638 /* If we found a format_arg attribute and did a recursive check,
9639 we are done with checking this argument. Otherwise, we continue
9640 and this will be considered a non-literal. */
9641 if (found_format_arg)
9642 return;
9645 if (TREE_CODE (param) == COND_EXPR)
9647 /* Check both halves of the conditional expression. */
9648 check_function_arguments_recurse (callback, ctx,
9649 TREE_OPERAND (param, 1), param_num);
9650 check_function_arguments_recurse (callback, ctx,
9651 TREE_OPERAND (param, 2), param_num);
9652 return;
9655 (*callback) (ctx, param, param_num);
9658 /* Checks for a builtin function FNDECL that the number of arguments
9659 NARGS against the required number REQUIRED and issues an error if
9660 there is a mismatch. Returns true if the number of arguments is
9661 correct, otherwise false. */
9663 static bool
9664 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9666 if (nargs < required)
9668 error_at (input_location,
9669 "not enough arguments to function %qE", fndecl);
9670 return false;
9672 else if (nargs > required)
9674 error_at (input_location,
9675 "too many arguments to function %qE", fndecl);
9676 return false;
9678 return true;
9681 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9682 Returns false if there was an error, otherwise true. */
9684 bool
9685 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9687 if (!DECL_BUILT_IN (fndecl)
9688 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9689 return true;
9691 switch (DECL_FUNCTION_CODE (fndecl))
9693 case BUILT_IN_CONSTANT_P:
9694 return builtin_function_validate_nargs (fndecl, nargs, 1);
9696 case BUILT_IN_ISFINITE:
9697 case BUILT_IN_ISINF:
9698 case BUILT_IN_ISINF_SIGN:
9699 case BUILT_IN_ISNAN:
9700 case BUILT_IN_ISNORMAL:
9701 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9703 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9705 error ("non-floating-point argument in call to "
9706 "function %qE", fndecl);
9707 return false;
9709 return true;
9711 return false;
9713 case BUILT_IN_ISGREATER:
9714 case BUILT_IN_ISGREATEREQUAL:
9715 case BUILT_IN_ISLESS:
9716 case BUILT_IN_ISLESSEQUAL:
9717 case BUILT_IN_ISLESSGREATER:
9718 case BUILT_IN_ISUNORDERED:
9719 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9721 enum tree_code code0, code1;
9722 code0 = TREE_CODE (TREE_TYPE (args[0]));
9723 code1 = TREE_CODE (TREE_TYPE (args[1]));
9724 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9725 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9726 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9728 error ("non-floating-point arguments in call to "
9729 "function %qE", fndecl);
9730 return false;
9732 return true;
9734 return false;
9736 case BUILT_IN_FPCLASSIFY:
9737 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9739 unsigned i;
9741 for (i=0; i<5; i++)
9742 if (TREE_CODE (args[i]) != INTEGER_CST)
9744 error ("non-const integer argument %u in call to function %qE",
9745 i+1, fndecl);
9746 return false;
9749 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9751 error ("non-floating-point argument in call to function %qE",
9752 fndecl);
9753 return false;
9755 return true;
9757 return false;
9759 case BUILT_IN_ASSUME_ALIGNED:
9760 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9762 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9764 error ("non-integer argument 3 in call to function %qE", fndecl);
9765 return false;
9767 return true;
9769 return false;
9771 case BUILT_IN_ADD_OVERFLOW:
9772 case BUILT_IN_SUB_OVERFLOW:
9773 case BUILT_IN_MUL_OVERFLOW:
9774 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9776 unsigned i;
9777 for (i = 0; i < 2; i++)
9778 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9780 error ("argument %u in call to function %qE does not have "
9781 "integral type", i + 1, fndecl);
9782 return false;
9784 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9785 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9787 error ("argument 3 in call to function %qE does not have "
9788 "pointer to integer type", fndecl);
9789 return false;
9791 return true;
9793 return false;
9795 default:
9796 return true;
9800 /* Function to help qsort sort FIELD_DECLs by name order. */
9803 field_decl_cmp (const void *x_p, const void *y_p)
9805 const tree *const x = (const tree *const) x_p;
9806 const tree *const y = (const tree *const) y_p;
9808 if (DECL_NAME (*x) == DECL_NAME (*y))
9809 /* A nontype is "greater" than a type. */
9810 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9811 if (DECL_NAME (*x) == NULL_TREE)
9812 return -1;
9813 if (DECL_NAME (*y) == NULL_TREE)
9814 return 1;
9815 if (DECL_NAME (*x) < DECL_NAME (*y))
9816 return -1;
9817 return 1;
9820 static struct {
9821 gt_pointer_operator new_value;
9822 void *cookie;
9823 } resort_data;
9825 /* This routine compares two fields like field_decl_cmp but using the
9826 pointer operator in resort_data. */
9828 static int
9829 resort_field_decl_cmp (const void *x_p, const void *y_p)
9831 const tree *const x = (const tree *const) x_p;
9832 const tree *const y = (const tree *const) y_p;
9834 if (DECL_NAME (*x) == DECL_NAME (*y))
9835 /* A nontype is "greater" than a type. */
9836 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9837 if (DECL_NAME (*x) == NULL_TREE)
9838 return -1;
9839 if (DECL_NAME (*y) == NULL_TREE)
9840 return 1;
9842 tree d1 = DECL_NAME (*x);
9843 tree d2 = DECL_NAME (*y);
9844 resort_data.new_value (&d1, resort_data.cookie);
9845 resort_data.new_value (&d2, resort_data.cookie);
9846 if (d1 < d2)
9847 return -1;
9849 return 1;
9852 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9854 void
9855 resort_sorted_fields (void *obj,
9856 void * ARG_UNUSED (orig_obj),
9857 gt_pointer_operator new_value,
9858 void *cookie)
9860 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9861 resort_data.new_value = new_value;
9862 resort_data.cookie = cookie;
9863 qsort (&sf->elts[0], sf->len, sizeof (tree),
9864 resort_field_decl_cmp);
9867 /* Subroutine of c_parse_error.
9868 Return the result of concatenating LHS and RHS. RHS is really
9869 a string literal, its first character is indicated by RHS_START and
9870 RHS_SIZE is its length (including the terminating NUL character).
9872 The caller is responsible for deleting the returned pointer. */
9874 static char *
9875 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9877 const int lhs_size = strlen (lhs);
9878 char *result = XNEWVEC (char, lhs_size + rhs_size);
9879 strncpy (result, lhs, lhs_size);
9880 strncpy (result + lhs_size, rhs_start, rhs_size);
9881 return result;
9884 /* Issue the error given by GMSGID, indicating that it occurred before
9885 TOKEN, which had the associated VALUE. */
9887 void
9888 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9889 tree value, unsigned char token_flags)
9891 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9893 char *message = NULL;
9895 if (token_type == CPP_EOF)
9896 message = catenate_messages (gmsgid, " at end of input");
9897 else if (token_type == CPP_CHAR
9898 || token_type == CPP_WCHAR
9899 || token_type == CPP_CHAR16
9900 || token_type == CPP_CHAR32)
9902 unsigned int val = TREE_INT_CST_LOW (value);
9903 const char *prefix;
9905 switch (token_type)
9907 default:
9908 prefix = "";
9909 break;
9910 case CPP_WCHAR:
9911 prefix = "L";
9912 break;
9913 case CPP_CHAR16:
9914 prefix = "u";
9915 break;
9916 case CPP_CHAR32:
9917 prefix = "U";
9918 break;
9921 if (val <= UCHAR_MAX && ISGRAPH (val))
9922 message = catenate_messages (gmsgid, " before %s'%c'");
9923 else
9924 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9926 error (message, prefix, val);
9927 free (message);
9928 message = NULL;
9930 else if (token_type == CPP_CHAR_USERDEF
9931 || token_type == CPP_WCHAR_USERDEF
9932 || token_type == CPP_CHAR16_USERDEF
9933 || token_type == CPP_CHAR32_USERDEF)
9934 message = catenate_messages (gmsgid,
9935 " before user-defined character literal");
9936 else if (token_type == CPP_STRING_USERDEF
9937 || token_type == CPP_WSTRING_USERDEF
9938 || token_type == CPP_STRING16_USERDEF
9939 || token_type == CPP_STRING32_USERDEF
9940 || token_type == CPP_UTF8STRING_USERDEF)
9941 message = catenate_messages (gmsgid, " before user-defined string literal");
9942 else if (token_type == CPP_STRING
9943 || token_type == CPP_WSTRING
9944 || token_type == CPP_STRING16
9945 || token_type == CPP_STRING32
9946 || token_type == CPP_UTF8STRING)
9947 message = catenate_messages (gmsgid, " before string constant");
9948 else if (token_type == CPP_NUMBER)
9949 message = catenate_messages (gmsgid, " before numeric constant");
9950 else if (token_type == CPP_NAME)
9952 message = catenate_messages (gmsgid, " before %qE");
9953 error (message, value);
9954 free (message);
9955 message = NULL;
9957 else if (token_type == CPP_PRAGMA)
9958 message = catenate_messages (gmsgid, " before %<#pragma%>");
9959 else if (token_type == CPP_PRAGMA_EOL)
9960 message = catenate_messages (gmsgid, " before end of line");
9961 else if (token_type == CPP_DECLTYPE)
9962 message = catenate_messages (gmsgid, " before %<decltype%>");
9963 else if (token_type < N_TTYPES)
9965 message = catenate_messages (gmsgid, " before %qs token");
9966 error (message, cpp_type2name (token_type, token_flags));
9967 free (message);
9968 message = NULL;
9970 else
9971 error (gmsgid);
9973 if (message)
9975 error (message);
9976 free (message);
9978 #undef catenate_messages
9981 /* Return the gcc option code associated with the reason for a cpp
9982 message, or 0 if none. */
9984 static int
9985 c_option_controlling_cpp_error (int reason)
9987 const struct cpp_reason_option_codes_t *entry;
9989 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9991 if (entry->reason == reason)
9992 return entry->option_code;
9994 return 0;
9997 /* Callback from cpp_error for PFILE to print diagnostics from the
9998 preprocessor. The diagnostic is of type LEVEL, with REASON set
9999 to the reason code if LEVEL is represents a warning, at location
10000 LOCATION unless this is after lexing and the compiler's location
10001 should be used instead, with column number possibly overridden by
10002 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10003 the arguments. Returns true if a diagnostic was emitted, false
10004 otherwise. */
10006 bool
10007 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10008 location_t location, unsigned int column_override,
10009 const char *msg, va_list *ap)
10011 diagnostic_info diagnostic;
10012 diagnostic_t dlevel;
10013 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10014 bool ret;
10016 switch (level)
10018 case CPP_DL_WARNING_SYSHDR:
10019 if (flag_no_output)
10020 return false;
10021 global_dc->dc_warn_system_headers = 1;
10022 /* Fall through. */
10023 case CPP_DL_WARNING:
10024 if (flag_no_output)
10025 return false;
10026 dlevel = DK_WARNING;
10027 break;
10028 case CPP_DL_PEDWARN:
10029 if (flag_no_output && !flag_pedantic_errors)
10030 return false;
10031 dlevel = DK_PEDWARN;
10032 break;
10033 case CPP_DL_ERROR:
10034 dlevel = DK_ERROR;
10035 break;
10036 case CPP_DL_ICE:
10037 dlevel = DK_ICE;
10038 break;
10039 case CPP_DL_NOTE:
10040 dlevel = DK_NOTE;
10041 break;
10042 case CPP_DL_FATAL:
10043 dlevel = DK_FATAL;
10044 break;
10045 default:
10046 gcc_unreachable ();
10048 if (done_lexing)
10049 location = input_location;
10050 diagnostic_set_info_translated (&diagnostic, msg, ap,
10051 location, dlevel);
10052 if (column_override)
10053 diagnostic_override_column (&diagnostic, column_override);
10054 diagnostic_override_option_index (&diagnostic,
10055 c_option_controlling_cpp_error (reason));
10056 ret = report_diagnostic (&diagnostic);
10057 if (level == CPP_DL_WARNING_SYSHDR)
10058 global_dc->dc_warn_system_headers = save_warn_system_headers;
10059 return ret;
10062 /* Convert a character from the host to the target execution character
10063 set. cpplib handles this, mostly. */
10065 HOST_WIDE_INT
10066 c_common_to_target_charset (HOST_WIDE_INT c)
10068 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10069 zero-extended under -fno-signed-char. cpplib insists that characters
10070 and character constants are always unsigned. Hence we must convert
10071 back and forth. */
10072 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10074 uc = cpp_host_to_exec_charset (parse_in, uc);
10076 if (flag_signed_char)
10077 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10078 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10079 else
10080 return uc;
10083 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10084 references with an INDIRECT_REF of a constant at the bottom; much like the
10085 traditional rendering of offsetof as a macro. Return the folded result. */
10087 tree
10088 fold_offsetof_1 (tree expr)
10090 tree base, off, t;
10092 switch (TREE_CODE (expr))
10094 case ERROR_MARK:
10095 return expr;
10097 case VAR_DECL:
10098 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10099 return error_mark_node;
10101 case CALL_EXPR:
10102 case TARGET_EXPR:
10103 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10104 return error_mark_node;
10106 case NOP_EXPR:
10107 case INDIRECT_REF:
10108 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10110 error ("cannot apply %<offsetof%> to a non constant address");
10111 return error_mark_node;
10113 return TREE_OPERAND (expr, 0);
10115 case COMPONENT_REF:
10116 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10117 if (base == error_mark_node)
10118 return base;
10120 t = TREE_OPERAND (expr, 1);
10121 if (DECL_C_BIT_FIELD (t))
10123 error ("attempt to take address of bit-field structure "
10124 "member %qD", t);
10125 return error_mark_node;
10127 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10128 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10129 / BITS_PER_UNIT));
10130 break;
10132 case ARRAY_REF:
10133 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10134 if (base == error_mark_node)
10135 return base;
10137 t = TREE_OPERAND (expr, 1);
10139 /* Check if the offset goes beyond the upper bound of the array. */
10140 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10142 tree upbound = array_ref_up_bound (expr);
10143 if (upbound != NULL_TREE
10144 && TREE_CODE (upbound) == INTEGER_CST
10145 && !tree_int_cst_equal (upbound,
10146 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10148 upbound = size_binop (PLUS_EXPR, upbound,
10149 build_int_cst (TREE_TYPE (upbound), 1));
10150 if (tree_int_cst_lt (upbound, t))
10152 tree v;
10154 for (v = TREE_OPERAND (expr, 0);
10155 TREE_CODE (v) == COMPONENT_REF;
10156 v = TREE_OPERAND (v, 0))
10157 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10158 == RECORD_TYPE)
10160 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10161 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10162 if (TREE_CODE (fld_chain) == FIELD_DECL)
10163 break;
10165 if (fld_chain)
10166 break;
10168 /* Don't warn if the array might be considered a poor
10169 man's flexible array member with a very permissive
10170 definition thereof. */
10171 if (TREE_CODE (v) == ARRAY_REF
10172 || TREE_CODE (v) == COMPONENT_REF)
10173 warning (OPT_Warray_bounds,
10174 "index %E denotes an offset "
10175 "greater than size of %qT",
10176 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10181 t = convert (sizetype, t);
10182 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10183 break;
10185 case COMPOUND_EXPR:
10186 /* Handle static members of volatile structs. */
10187 t = TREE_OPERAND (expr, 1);
10188 gcc_assert (TREE_CODE (t) == VAR_DECL);
10189 return fold_offsetof_1 (t);
10191 default:
10192 gcc_unreachable ();
10195 return fold_build_pointer_plus (base, off);
10198 /* Likewise, but convert it to the return type of offsetof. */
10200 tree
10201 fold_offsetof (tree expr)
10203 return convert (size_type_node, fold_offsetof_1 (expr));
10206 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10207 expression, because B will always be true. */
10209 void
10210 warn_for_omitted_condop (location_t location, tree cond)
10212 if (truth_value_p (TREE_CODE (cond)))
10213 warning_at (location, OPT_Wparentheses,
10214 "the omitted middle operand in ?: will always be %<true%>, "
10215 "suggest explicit middle operand");
10218 /* Give an error for storing into ARG, which is 'const'. USE indicates
10219 how ARG was being used. */
10221 void
10222 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10224 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10225 || use == lv_asm);
10226 /* Using this macro rather than (for example) arrays of messages
10227 ensures that all the format strings are checked at compile
10228 time. */
10229 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10230 : (use == lv_increment ? (I) \
10231 : (use == lv_decrement ? (D) : (AS))))
10232 if (TREE_CODE (arg) == COMPONENT_REF)
10234 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10235 error_at (loc, READONLY_MSG (G_("assignment of member "
10236 "%qD in read-only object"),
10237 G_("increment of member "
10238 "%qD in read-only object"),
10239 G_("decrement of member "
10240 "%qD in read-only object"),
10241 G_("member %qD in read-only object "
10242 "used as %<asm%> output")),
10243 TREE_OPERAND (arg, 1));
10244 else
10245 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10246 G_("increment of read-only member %qD"),
10247 G_("decrement of read-only member %qD"),
10248 G_("read-only member %qD used as %<asm%> output")),
10249 TREE_OPERAND (arg, 1));
10251 else if (TREE_CODE (arg) == VAR_DECL)
10252 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10253 G_("increment of read-only variable %qD"),
10254 G_("decrement of read-only variable %qD"),
10255 G_("read-only variable %qD used as %<asm%> output")),
10256 arg);
10257 else if (TREE_CODE (arg) == PARM_DECL)
10258 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10259 G_("increment of read-only parameter %qD"),
10260 G_("decrement of read-only parameter %qD"),
10261 G_("read-only parameter %qD use as %<asm%> output")),
10262 arg);
10263 else if (TREE_CODE (arg) == RESULT_DECL)
10265 gcc_assert (c_dialect_cxx ());
10266 error_at (loc, READONLY_MSG (G_("assignment of "
10267 "read-only named return value %qD"),
10268 G_("increment of "
10269 "read-only named return value %qD"),
10270 G_("decrement of "
10271 "read-only named return value %qD"),
10272 G_("read-only named return value %qD "
10273 "used as %<asm%>output")),
10274 arg);
10276 else if (TREE_CODE (arg) == FUNCTION_DECL)
10277 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10278 G_("increment of function %qD"),
10279 G_("decrement of function %qD"),
10280 G_("function %qD used as %<asm%> output")),
10281 arg);
10282 else
10283 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10284 G_("increment of read-only location %qE"),
10285 G_("decrement of read-only location %qE"),
10286 G_("read-only location %qE used as %<asm%> output")),
10287 arg);
10290 /* Print an error message for an invalid lvalue. USE says
10291 how the lvalue is being used and so selects the error message. LOC
10292 is the location for the error. */
10294 void
10295 lvalue_error (location_t loc, enum lvalue_use use)
10297 switch (use)
10299 case lv_assign:
10300 error_at (loc, "lvalue required as left operand of assignment");
10301 break;
10302 case lv_increment:
10303 error_at (loc, "lvalue required as increment operand");
10304 break;
10305 case lv_decrement:
10306 error_at (loc, "lvalue required as decrement operand");
10307 break;
10308 case lv_addressof:
10309 error_at (loc, "lvalue required as unary %<&%> operand");
10310 break;
10311 case lv_asm:
10312 error_at (loc, "lvalue required in asm statement");
10313 break;
10314 default:
10315 gcc_unreachable ();
10319 /* Print an error message for an invalid indirection of type TYPE.
10320 ERRSTRING is the name of the operator for the indirection. */
10322 void
10323 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10325 switch (errstring)
10327 case RO_NULL:
10328 gcc_assert (c_dialect_cxx ());
10329 error_at (loc, "invalid type argument (have %qT)", type);
10330 break;
10331 case RO_ARRAY_INDEXING:
10332 error_at (loc,
10333 "invalid type argument of array indexing (have %qT)",
10334 type);
10335 break;
10336 case RO_UNARY_STAR:
10337 error_at (loc,
10338 "invalid type argument of unary %<*%> (have %qT)",
10339 type);
10340 break;
10341 case RO_ARROW:
10342 error_at (loc,
10343 "invalid type argument of %<->%> (have %qT)",
10344 type);
10345 break;
10346 case RO_ARROW_STAR:
10347 error_at (loc,
10348 "invalid type argument of %<->*%> (have %qT)",
10349 type);
10350 break;
10351 case RO_IMPLICIT_CONVERSION:
10352 error_at (loc,
10353 "invalid type argument of implicit conversion (have %qT)",
10354 type);
10355 break;
10356 default:
10357 gcc_unreachable ();
10361 /* *PTYPE is an incomplete array. Complete it with a domain based on
10362 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10363 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10364 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10367 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10369 tree maxindex, type, main_type, elt, unqual_elt;
10370 int failure = 0, quals;
10371 hashval_t hashcode = 0;
10372 bool overflow_p = false;
10374 maxindex = size_zero_node;
10375 if (initial_value)
10377 if (TREE_CODE (initial_value) == STRING_CST)
10379 int eltsize
10380 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10381 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10383 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10385 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10387 if (vec_safe_is_empty (v))
10389 if (pedantic)
10390 failure = 3;
10391 maxindex = ssize_int (-1);
10393 else
10395 tree curindex;
10396 unsigned HOST_WIDE_INT cnt;
10397 constructor_elt *ce;
10398 bool fold_p = false;
10400 if ((*v)[0].index)
10401 maxindex = (*v)[0].index, fold_p = true;
10403 curindex = maxindex;
10405 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10407 bool curfold_p = false;
10408 if (ce->index)
10409 curindex = ce->index, curfold_p = true;
10410 else
10412 if (fold_p)
10414 /* Since we treat size types now as ordinary
10415 unsigned types, we need an explicit overflow
10416 check. */
10417 tree orig = curindex;
10418 curindex = fold_convert (sizetype, curindex);
10419 overflow_p |= tree_int_cst_lt (curindex, orig);
10421 curindex = size_binop (PLUS_EXPR, curindex,
10422 size_one_node);
10424 if (tree_int_cst_lt (maxindex, curindex))
10425 maxindex = curindex, fold_p = curfold_p;
10427 if (fold_p)
10429 tree orig = maxindex;
10430 maxindex = fold_convert (sizetype, maxindex);
10431 overflow_p |= tree_int_cst_lt (maxindex, orig);
10435 else
10437 /* Make an error message unless that happened already. */
10438 if (initial_value != error_mark_node)
10439 failure = 1;
10442 else
10444 failure = 2;
10445 if (!do_default)
10446 return failure;
10449 type = *ptype;
10450 elt = TREE_TYPE (type);
10451 quals = TYPE_QUALS (strip_array_types (elt));
10452 if (quals == 0)
10453 unqual_elt = elt;
10454 else
10455 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10457 /* Using build_distinct_type_copy and modifying things afterward instead
10458 of using build_array_type to create a new type preserves all of the
10459 TYPE_LANG_FLAG_? bits that the front end may have set. */
10460 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10461 TREE_TYPE (main_type) = unqual_elt;
10462 TYPE_DOMAIN (main_type)
10463 = build_range_type (TREE_TYPE (maxindex),
10464 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10465 layout_type (main_type);
10467 /* Make sure we have the canonical MAIN_TYPE. */
10468 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10469 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10470 hashcode);
10471 main_type = type_hash_canon (hashcode, main_type);
10473 /* Fix the canonical type. */
10474 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10475 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10476 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10477 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10478 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10479 != TYPE_DOMAIN (main_type)))
10480 TYPE_CANONICAL (main_type)
10481 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10482 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10483 else
10484 TYPE_CANONICAL (main_type) = main_type;
10486 if (quals == 0)
10487 type = main_type;
10488 else
10489 type = c_build_qualified_type (main_type, quals);
10491 if (COMPLETE_TYPE_P (type)
10492 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10493 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10495 error ("size of array is too large");
10496 /* If we proceed with the array type as it is, we'll eventually
10497 crash in tree_to_[su]hwi(). */
10498 type = error_mark_node;
10501 *ptype = type;
10502 return failure;
10505 /* Like c_mark_addressable but don't check register qualifier. */
10506 void
10507 c_common_mark_addressable_vec (tree t)
10509 while (handled_component_p (t))
10510 t = TREE_OPERAND (t, 0);
10511 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10512 return;
10513 TREE_ADDRESSABLE (t) = 1;
10518 /* Used to help initialize the builtin-types.def table. When a type of
10519 the correct size doesn't exist, use error_mark_node instead of NULL.
10520 The later results in segfaults even when a decl using the type doesn't
10521 get invoked. */
10523 tree
10524 builtin_type_for_size (int size, bool unsignedp)
10526 tree type = c_common_type_for_size (size, unsignedp);
10527 return type ? type : error_mark_node;
10530 /* A helper function for resolve_overloaded_builtin in resolving the
10531 overloaded __sync_ builtins. Returns a positive power of 2 if the
10532 first operand of PARAMS is a pointer to a supported data type.
10533 Returns 0 if an error is encountered. */
10535 static int
10536 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10538 tree type;
10539 int size;
10541 if (!params)
10543 error ("too few arguments to function %qE", function);
10544 return 0;
10547 type = TREE_TYPE ((*params)[0]);
10548 if (TREE_CODE (type) == ARRAY_TYPE)
10550 /* Force array-to-pointer decay for C++. */
10551 gcc_assert (c_dialect_cxx());
10552 (*params)[0] = default_conversion ((*params)[0]);
10553 type = TREE_TYPE ((*params)[0]);
10555 if (TREE_CODE (type) != POINTER_TYPE)
10556 goto incompatible;
10558 type = TREE_TYPE (type);
10559 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10560 goto incompatible;
10562 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10563 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10564 return size;
10566 incompatible:
10567 error ("incompatible type for argument %d of %qE", 1, function);
10568 return 0;
10571 /* A helper function for resolve_overloaded_builtin. Adds casts to
10572 PARAMS to make arguments match up with those of FUNCTION. Drops
10573 the variadic arguments at the end. Returns false if some error
10574 was encountered; true on success. */
10576 static bool
10577 sync_resolve_params (location_t loc, tree orig_function, tree function,
10578 vec<tree, va_gc> *params, bool orig_format)
10580 function_args_iterator iter;
10581 tree ptype;
10582 unsigned int parmnum;
10584 function_args_iter_init (&iter, TREE_TYPE (function));
10585 /* We've declared the implementation functions to use "volatile void *"
10586 as the pointer parameter, so we shouldn't get any complaints from the
10587 call to check_function_arguments what ever type the user used. */
10588 function_args_iter_next (&iter);
10589 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10590 ptype = TYPE_MAIN_VARIANT (ptype);
10592 /* For the rest of the values, we need to cast these to FTYPE, so that we
10593 don't get warnings for passing pointer types, etc. */
10594 parmnum = 0;
10595 while (1)
10597 tree val, arg_type;
10599 arg_type = function_args_iter_cond (&iter);
10600 /* XXX void_type_node belies the abstraction. */
10601 if (arg_type == void_type_node)
10602 break;
10604 ++parmnum;
10605 if (params->length () <= parmnum)
10607 error_at (loc, "too few arguments to function %qE", orig_function);
10608 return false;
10611 /* Only convert parameters if arg_type is unsigned integer type with
10612 new format sync routines, i.e. don't attempt to convert pointer
10613 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10614 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10615 kinds). */
10616 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10618 /* Ideally for the first conversion we'd use convert_for_assignment
10619 so that we get warnings for anything that doesn't match the pointer
10620 type. This isn't portable across the C and C++ front ends atm. */
10621 val = (*params)[parmnum];
10622 val = convert (ptype, val);
10623 val = convert (arg_type, val);
10624 (*params)[parmnum] = val;
10627 function_args_iter_next (&iter);
10630 /* __atomic routines are not variadic. */
10631 if (!orig_format && params->length () != parmnum + 1)
10633 error_at (loc, "too many arguments to function %qE", orig_function);
10634 return false;
10637 /* The definition of these primitives is variadic, with the remaining
10638 being "an optional list of variables protected by the memory barrier".
10639 No clue what that's supposed to mean, precisely, but we consider all
10640 call-clobbered variables to be protected so we're safe. */
10641 params->truncate (parmnum + 1);
10643 return true;
10646 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10647 RESULT to make it match the type of the first pointer argument in
10648 PARAMS. */
10650 static tree
10651 sync_resolve_return (tree first_param, tree result, bool orig_format)
10653 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10654 tree rtype = TREE_TYPE (result);
10655 ptype = TYPE_MAIN_VARIANT (ptype);
10657 /* New format doesn't require casting unless the types are the same size. */
10658 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10659 return convert (ptype, result);
10660 else
10661 return result;
10664 /* This function verifies the PARAMS to generic atomic FUNCTION.
10665 It returns the size if all the parameters are the same size, otherwise
10666 0 is returned if the parameters are invalid. */
10668 static int
10669 get_atomic_generic_size (location_t loc, tree function,
10670 vec<tree, va_gc> *params)
10672 unsigned int n_param;
10673 unsigned int n_model;
10674 unsigned int x;
10675 int size_0;
10676 tree type_0;
10678 /* Determine the parameter makeup. */
10679 switch (DECL_FUNCTION_CODE (function))
10681 case BUILT_IN_ATOMIC_EXCHANGE:
10682 n_param = 4;
10683 n_model = 1;
10684 break;
10685 case BUILT_IN_ATOMIC_LOAD:
10686 case BUILT_IN_ATOMIC_STORE:
10687 n_param = 3;
10688 n_model = 1;
10689 break;
10690 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10691 n_param = 6;
10692 n_model = 2;
10693 break;
10694 default:
10695 gcc_unreachable ();
10698 if (vec_safe_length (params) != n_param)
10700 error_at (loc, "incorrect number of arguments to function %qE", function);
10701 return 0;
10704 /* Get type of first parameter, and determine its size. */
10705 type_0 = TREE_TYPE ((*params)[0]);
10706 if (TREE_CODE (type_0) == ARRAY_TYPE)
10708 /* Force array-to-pointer decay for C++. */
10709 gcc_assert (c_dialect_cxx());
10710 (*params)[0] = default_conversion ((*params)[0]);
10711 type_0 = TREE_TYPE ((*params)[0]);
10713 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10715 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10716 function);
10717 return 0;
10720 /* Types must be compile time constant sizes. */
10721 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10723 error_at (loc,
10724 "argument 1 of %qE must be a pointer to a constant size type",
10725 function);
10726 return 0;
10729 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10731 /* Zero size objects are not allowed. */
10732 if (size_0 == 0)
10734 error_at (loc,
10735 "argument 1 of %qE must be a pointer to a nonzero size object",
10736 function);
10737 return 0;
10740 /* Check each other parameter is a pointer and the same size. */
10741 for (x = 0; x < n_param - n_model; x++)
10743 int size;
10744 tree type = TREE_TYPE ((*params)[x]);
10745 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10746 if (n_param == 6 && x == 3)
10747 continue;
10748 if (!POINTER_TYPE_P (type))
10750 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10751 function);
10752 return 0;
10754 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10755 size = type_size ? tree_to_uhwi (type_size) : 0;
10756 if (size != size_0)
10758 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10759 function);
10760 return 0;
10764 /* Check memory model parameters for validity. */
10765 for (x = n_param - n_model ; x < n_param; x++)
10767 tree p = (*params)[x];
10768 if (TREE_CODE (p) == INTEGER_CST)
10770 int i = tree_to_uhwi (p);
10771 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10773 warning_at (loc, OPT_Winvalid_memory_model,
10774 "invalid memory model argument %d of %qE", x + 1,
10775 function);
10778 else
10779 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10781 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10782 function);
10783 return 0;
10787 return size_0;
10791 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10792 at the beginning of the parameter list PARAMS representing the size of the
10793 objects. This is to match the library ABI requirement. LOC is the location
10794 of the function call.
10795 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10796 returned to allow the external call to be constructed. */
10798 static tree
10799 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10800 vec<tree, va_gc> *params)
10802 tree size_node;
10804 /* Insert a SIZE_T parameter as the first param. If there isn't
10805 enough space, allocate a new vector and recursively re-build with that. */
10806 if (!params->space (1))
10808 unsigned int z, len;
10809 vec<tree, va_gc> *v;
10810 tree f;
10812 len = params->length ();
10813 vec_alloc (v, len + 1);
10814 v->quick_push (build_int_cst (size_type_node, n));
10815 for (z = 0; z < len; z++)
10816 v->quick_push ((*params)[z]);
10817 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10818 vec_free (v);
10819 return f;
10822 /* Add the size parameter and leave as a function call for processing. */
10823 size_node = build_int_cst (size_type_node, n);
10824 params->quick_insert (0, size_node);
10825 return NULL_TREE;
10829 /* Return whether atomic operations for naturally aligned N-byte
10830 arguments are supported, whether inline or through libatomic. */
10831 static bool
10832 atomic_size_supported_p (int n)
10834 switch (n)
10836 case 1:
10837 case 2:
10838 case 4:
10839 case 8:
10840 return true;
10842 case 16:
10843 return targetm.scalar_mode_supported_p (TImode);
10845 default:
10846 return false;
10850 /* This will process an __atomic_exchange function call, determine whether it
10851 needs to be mapped to the _N variation, or turned into a library call.
10852 LOC is the location of the builtin call.
10853 FUNCTION is the DECL that has been invoked;
10854 PARAMS is the argument list for the call. The return value is non-null
10855 TRUE is returned if it is translated into the proper format for a call to the
10856 external library, and NEW_RETURN is set the tree for that function.
10857 FALSE is returned if processing for the _N variation is required, and
10858 NEW_RETURN is set to the the return value the result is copied into. */
10859 static bool
10860 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10861 vec<tree, va_gc> *params, tree *new_return)
10863 tree p0, p1, p2, p3;
10864 tree I_type, I_type_ptr;
10865 int n = get_atomic_generic_size (loc, function, params);
10867 /* Size of 0 is an error condition. */
10868 if (n == 0)
10870 *new_return = error_mark_node;
10871 return true;
10874 /* If not a lock-free size, change to the library generic format. */
10875 if (!atomic_size_supported_p (n))
10877 *new_return = add_atomic_size_parameter (n, loc, function, params);
10878 return true;
10881 /* Otherwise there is a lockfree match, transform the call from:
10882 void fn(T* mem, T* desired, T* return, model)
10883 into
10884 *return = (T) (fn (In* mem, (In) *desired, model)) */
10886 p0 = (*params)[0];
10887 p1 = (*params)[1];
10888 p2 = (*params)[2];
10889 p3 = (*params)[3];
10891 /* Create pointer to appropriate size. */
10892 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10893 I_type_ptr = build_pointer_type (I_type);
10895 /* Convert object pointer to required type. */
10896 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10897 (*params)[0] = p0;
10898 /* Convert new value to required type, and dereference it. */
10899 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10900 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10901 (*params)[1] = p1;
10903 /* Move memory model to the 3rd position, and end param list. */
10904 (*params)[2] = p3;
10905 params->truncate (3);
10907 /* Convert return pointer and dereference it for later assignment. */
10908 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10910 return false;
10914 /* This will process an __atomic_compare_exchange function call, determine
10915 whether it needs to be mapped to the _N variation, or turned into a lib call.
10916 LOC is the location of the builtin call.
10917 FUNCTION is the DECL that has been invoked;
10918 PARAMS is the argument list for the call. The return value is non-null
10919 TRUE is returned if it is translated into the proper format for a call to the
10920 external library, and NEW_RETURN is set the tree for that function.
10921 FALSE is returned if processing for the _N variation is required. */
10923 static bool
10924 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10925 vec<tree, va_gc> *params,
10926 tree *new_return)
10928 tree p0, p1, p2;
10929 tree I_type, I_type_ptr;
10930 int n = get_atomic_generic_size (loc, function, params);
10932 /* Size of 0 is an error condition. */
10933 if (n == 0)
10935 *new_return = error_mark_node;
10936 return true;
10939 /* If not a lock-free size, change to the library generic format. */
10940 if (!atomic_size_supported_p (n))
10942 /* The library generic format does not have the weak parameter, so
10943 remove it from the param list. Since a parameter has been removed,
10944 we can be sure that there is room for the SIZE_T parameter, meaning
10945 there will not be a recursive rebuilding of the parameter list, so
10946 there is no danger this will be done twice. */
10947 if (n > 0)
10949 (*params)[3] = (*params)[4];
10950 (*params)[4] = (*params)[5];
10951 params->truncate (5);
10953 *new_return = add_atomic_size_parameter (n, loc, function, params);
10954 return true;
10957 /* Otherwise, there is a match, so the call needs to be transformed from:
10958 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10959 into
10960 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10962 p0 = (*params)[0];
10963 p1 = (*params)[1];
10964 p2 = (*params)[2];
10966 /* Create pointer to appropriate size. */
10967 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10968 I_type_ptr = build_pointer_type (I_type);
10970 /* Convert object pointer to required type. */
10971 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10972 (*params)[0] = p0;
10974 /* Convert expected pointer to required type. */
10975 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10976 (*params)[1] = p1;
10978 /* Convert desired value to required type, and dereference it. */
10979 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10980 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10981 (*params)[2] = p2;
10983 /* The rest of the parameters are fine. NULL means no special return value
10984 processing.*/
10985 *new_return = NULL;
10986 return false;
10990 /* This will process an __atomic_load function call, determine whether it
10991 needs to be mapped to the _N variation, or turned into a library call.
10992 LOC is the location of the builtin call.
10993 FUNCTION is the DECL that has been invoked;
10994 PARAMS is the argument list for the call. The return value is non-null
10995 TRUE is returned if it is translated into the proper format for a call to the
10996 external library, and NEW_RETURN is set the tree for that function.
10997 FALSE is returned if processing for the _N variation is required, and
10998 NEW_RETURN is set to the the return value the result is copied into. */
11000 static bool
11001 resolve_overloaded_atomic_load (location_t loc, tree function,
11002 vec<tree, va_gc> *params, tree *new_return)
11004 tree p0, p1, p2;
11005 tree I_type, I_type_ptr;
11006 int n = get_atomic_generic_size (loc, function, params);
11008 /* Size of 0 is an error condition. */
11009 if (n == 0)
11011 *new_return = error_mark_node;
11012 return true;
11015 /* If not a lock-free size, change to the library generic format. */
11016 if (!atomic_size_supported_p (n))
11018 *new_return = add_atomic_size_parameter (n, loc, function, params);
11019 return true;
11022 /* Otherwise, there is a match, so the call needs to be transformed from:
11023 void fn(T* mem, T* return, model)
11024 into
11025 *return = (T) (fn ((In *) mem, model)) */
11027 p0 = (*params)[0];
11028 p1 = (*params)[1];
11029 p2 = (*params)[2];
11031 /* Create pointer to appropriate size. */
11032 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11033 I_type_ptr = build_pointer_type (I_type);
11035 /* Convert object pointer to required type. */
11036 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11037 (*params)[0] = p0;
11039 /* Move memory model to the 2nd position, and end param list. */
11040 (*params)[1] = p2;
11041 params->truncate (2);
11043 /* Convert return pointer and dereference it for later assignment. */
11044 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11046 return false;
11050 /* This will process an __atomic_store function call, determine whether it
11051 needs to be mapped to the _N variation, or turned into a library call.
11052 LOC is the location of the builtin call.
11053 FUNCTION is the DECL that has been invoked;
11054 PARAMS is the argument list for the call. The return value is non-null
11055 TRUE is returned if it is translated into the proper format for a call to the
11056 external library, and NEW_RETURN is set the tree for that function.
11057 FALSE is returned if processing for the _N variation is required, and
11058 NEW_RETURN is set to the the return value the result is copied into. */
11060 static bool
11061 resolve_overloaded_atomic_store (location_t loc, tree function,
11062 vec<tree, va_gc> *params, tree *new_return)
11064 tree p0, p1;
11065 tree I_type, I_type_ptr;
11066 int n = get_atomic_generic_size (loc, function, params);
11068 /* Size of 0 is an error condition. */
11069 if (n == 0)
11071 *new_return = error_mark_node;
11072 return true;
11075 /* If not a lock-free size, change to the library generic format. */
11076 if (!atomic_size_supported_p (n))
11078 *new_return = add_atomic_size_parameter (n, loc, function, params);
11079 return true;
11082 /* Otherwise, there is a match, so the call needs to be transformed from:
11083 void fn(T* mem, T* value, model)
11084 into
11085 fn ((In *) mem, (In) *value, model) */
11087 p0 = (*params)[0];
11088 p1 = (*params)[1];
11090 /* Create pointer to appropriate size. */
11091 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11092 I_type_ptr = build_pointer_type (I_type);
11094 /* Convert object pointer to required type. */
11095 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11096 (*params)[0] = p0;
11098 /* Convert new value to required type, and dereference it. */
11099 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11100 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11101 (*params)[1] = p1;
11103 /* The memory model is in the right spot already. Return is void. */
11104 *new_return = NULL_TREE;
11106 return false;
11110 /* Some builtin functions are placeholders for other expressions. This
11111 function should be called immediately after parsing the call expression
11112 before surrounding code has committed to the type of the expression.
11114 LOC is the location of the builtin call.
11116 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11117 PARAMS is the argument list for the call. The return value is non-null
11118 when expansion is complete, and null if normal processing should
11119 continue. */
11121 tree
11122 resolve_overloaded_builtin (location_t loc, tree function,
11123 vec<tree, va_gc> *params)
11125 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11126 bool orig_format = true;
11127 tree new_return = NULL_TREE;
11129 switch (DECL_BUILT_IN_CLASS (function))
11131 case BUILT_IN_NORMAL:
11132 break;
11133 case BUILT_IN_MD:
11134 if (targetm.resolve_overloaded_builtin)
11135 return targetm.resolve_overloaded_builtin (loc, function, params);
11136 else
11137 return NULL_TREE;
11138 default:
11139 return NULL_TREE;
11142 /* Handle BUILT_IN_NORMAL here. */
11143 switch (orig_code)
11145 case BUILT_IN_ATOMIC_EXCHANGE:
11146 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11147 case BUILT_IN_ATOMIC_LOAD:
11148 case BUILT_IN_ATOMIC_STORE:
11150 /* Handle these 4 together so that they can fall through to the next
11151 case if the call is transformed to an _N variant. */
11152 switch (orig_code)
11154 case BUILT_IN_ATOMIC_EXCHANGE:
11156 if (resolve_overloaded_atomic_exchange (loc, function, params,
11157 &new_return))
11158 return new_return;
11159 /* Change to the _N variant. */
11160 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11161 break;
11164 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11166 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11167 params,
11168 &new_return))
11169 return new_return;
11170 /* Change to the _N variant. */
11171 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11172 break;
11174 case BUILT_IN_ATOMIC_LOAD:
11176 if (resolve_overloaded_atomic_load (loc, function, params,
11177 &new_return))
11178 return new_return;
11179 /* Change to the _N variant. */
11180 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11181 break;
11183 case BUILT_IN_ATOMIC_STORE:
11185 if (resolve_overloaded_atomic_store (loc, function, params,
11186 &new_return))
11187 return new_return;
11188 /* Change to the _N variant. */
11189 orig_code = BUILT_IN_ATOMIC_STORE_N;
11190 break;
11192 default:
11193 gcc_unreachable ();
11195 /* Fallthrough to the normal processing. */
11197 case BUILT_IN_ATOMIC_EXCHANGE_N:
11198 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11199 case BUILT_IN_ATOMIC_LOAD_N:
11200 case BUILT_IN_ATOMIC_STORE_N:
11201 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11202 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11203 case BUILT_IN_ATOMIC_AND_FETCH_N:
11204 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11205 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11206 case BUILT_IN_ATOMIC_OR_FETCH_N:
11207 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11208 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11209 case BUILT_IN_ATOMIC_FETCH_AND_N:
11210 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11211 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11212 case BUILT_IN_ATOMIC_FETCH_OR_N:
11214 orig_format = false;
11215 /* Fallthru for parameter processing. */
11217 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11218 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11219 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11220 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11221 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11222 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11223 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11224 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11225 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11226 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11227 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11228 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11229 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11230 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11231 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11232 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11234 int n = sync_resolve_size (function, params);
11235 tree new_function, first_param, result;
11236 enum built_in_function fncode;
11238 if (n == 0)
11239 return error_mark_node;
11241 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11242 new_function = builtin_decl_explicit (fncode);
11243 if (!sync_resolve_params (loc, function, new_function, params,
11244 orig_format))
11245 return error_mark_node;
11247 first_param = (*params)[0];
11248 result = build_function_call_vec (loc, vNULL, new_function, params,
11249 NULL);
11250 if (result == error_mark_node)
11251 return result;
11252 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11253 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11254 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11255 result = sync_resolve_return (first_param, result, orig_format);
11257 /* If new_return is set, assign function to that expr and cast the
11258 result to void since the generic interface returned void. */
11259 if (new_return)
11261 /* Cast function result from I{1,2,4,8,16} to the required type. */
11262 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11263 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11264 result);
11265 TREE_SIDE_EFFECTS (result) = 1;
11266 protected_set_expr_location (result, loc);
11267 result = convert (void_type_node, result);
11269 return result;
11272 default:
11273 return NULL_TREE;
11277 /* vector_types_compatible_elements_p is used in type checks of vectors
11278 values used as operands of binary operators. Where it returns true, and
11279 the other checks of the caller succeed (being vector types in he first
11280 place, and matching number of elements), we can just treat the types
11281 as essentially the same.
11282 Contrast with vector_targets_convertible_p, which is used for vector
11283 pointer types, and vector_types_convertible_p, which will allow
11284 language-specific matches under the control of flag_lax_vector_conversions,
11285 and might still require a conversion. */
11286 /* True if vector types T1 and T2 can be inputs to the same binary
11287 operator without conversion.
11288 We don't check the overall vector size here because some of our callers
11289 want to give different error messages when the vectors are compatible
11290 except for the element count. */
11292 bool
11293 vector_types_compatible_elements_p (tree t1, tree t2)
11295 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11296 t1 = TREE_TYPE (t1);
11297 t2 = TREE_TYPE (t2);
11299 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11301 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11302 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11303 || c2 == FIXED_POINT_TYPE));
11305 t1 = c_common_signed_type (t1);
11306 t2 = c_common_signed_type (t2);
11307 /* Equality works here because c_common_signed_type uses
11308 TYPE_MAIN_VARIANT. */
11309 if (t1 == t2)
11310 return true;
11311 if (opaque && c1 == c2
11312 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11313 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11314 return true;
11315 return false;
11318 /* Check for missing format attributes on function pointers. LTYPE is
11319 the new type or left-hand side type. RTYPE is the old type or
11320 right-hand side type. Returns TRUE if LTYPE is missing the desired
11321 attribute. */
11323 bool
11324 check_missing_format_attribute (tree ltype, tree rtype)
11326 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11327 tree ra;
11329 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11330 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11331 break;
11332 if (ra)
11334 tree la;
11335 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11336 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11337 break;
11338 return !la;
11340 else
11341 return false;
11344 /* Subscripting with type char is likely to lose on a machine where
11345 chars are signed. So warn on any machine, but optionally. Don't
11346 warn for unsigned char since that type is safe. Don't warn for
11347 signed char because anyone who uses that must have done so
11348 deliberately. Furthermore, we reduce the false positive load by
11349 warning only for non-constant value of type char. */
11351 void
11352 warn_array_subscript_with_type_char (location_t loc, tree index)
11354 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11355 && TREE_CODE (index) != INTEGER_CST)
11356 warning_at (loc, OPT_Wchar_subscripts,
11357 "array subscript has type %<char%>");
11360 /* Implement -Wparentheses for the unexpected C precedence rules, to
11361 cover cases like x + y << z which readers are likely to
11362 misinterpret. We have seen an expression in which CODE is a binary
11363 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11364 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11365 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11366 expression was not formed using a binary or unary operator, or it
11367 was enclosed in parentheses. */
11369 void
11370 warn_about_parentheses (location_t loc, enum tree_code code,
11371 enum tree_code code_left, tree arg_left,
11372 enum tree_code code_right, tree arg_right)
11374 if (!warn_parentheses)
11375 return;
11377 /* This macro tests that the expression ARG with original tree code
11378 CODE appears to be a boolean expression. or the result of folding a
11379 boolean expression. */
11380 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11381 (truth_value_p (TREE_CODE (ARG)) \
11382 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11383 /* Folding may create 0 or 1 integers from other expressions. */ \
11384 || ((CODE) != INTEGER_CST \
11385 && (integer_onep (ARG) || integer_zerop (ARG))))
11387 switch (code)
11389 case LSHIFT_EXPR:
11390 if (code_left == PLUS_EXPR)
11391 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11392 "suggest parentheses around %<+%> inside %<<<%>");
11393 else if (code_right == PLUS_EXPR)
11394 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11395 "suggest parentheses around %<+%> inside %<<<%>");
11396 else if (code_left == MINUS_EXPR)
11397 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11398 "suggest parentheses around %<-%> inside %<<<%>");
11399 else if (code_right == MINUS_EXPR)
11400 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11401 "suggest parentheses around %<-%> inside %<<<%>");
11402 return;
11404 case RSHIFT_EXPR:
11405 if (code_left == PLUS_EXPR)
11406 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11407 "suggest parentheses around %<+%> inside %<>>%>");
11408 else if (code_right == PLUS_EXPR)
11409 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11410 "suggest parentheses around %<+%> inside %<>>%>");
11411 else if (code_left == MINUS_EXPR)
11412 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11413 "suggest parentheses around %<-%> inside %<>>%>");
11414 else if (code_right == MINUS_EXPR)
11415 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11416 "suggest parentheses around %<-%> inside %<>>%>");
11417 return;
11419 case TRUTH_ORIF_EXPR:
11420 if (code_left == TRUTH_ANDIF_EXPR)
11421 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11422 "suggest parentheses around %<&&%> within %<||%>");
11423 else if (code_right == TRUTH_ANDIF_EXPR)
11424 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11425 "suggest parentheses around %<&&%> within %<||%>");
11426 return;
11428 case BIT_IOR_EXPR:
11429 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11430 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11431 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11432 "suggest parentheses around arithmetic in operand of %<|%>");
11433 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11434 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11435 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11436 "suggest parentheses around arithmetic in operand of %<|%>");
11437 /* Check cases like x|y==z */
11438 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11439 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11440 "suggest parentheses around comparison in operand of %<|%>");
11441 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11442 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11443 "suggest parentheses around comparison in operand of %<|%>");
11444 /* Check cases like !x | y */
11445 else if (code_left == TRUTH_NOT_EXPR
11446 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11447 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11448 "suggest parentheses around operand of "
11449 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11450 return;
11452 case BIT_XOR_EXPR:
11453 if (code_left == BIT_AND_EXPR
11454 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11455 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11456 "suggest parentheses around arithmetic in operand of %<^%>");
11457 else if (code_right == BIT_AND_EXPR
11458 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11459 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11460 "suggest parentheses around arithmetic in operand of %<^%>");
11461 /* Check cases like x^y==z */
11462 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11463 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11464 "suggest parentheses around comparison in operand of %<^%>");
11465 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11466 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11467 "suggest parentheses around comparison in operand of %<^%>");
11468 return;
11470 case BIT_AND_EXPR:
11471 if (code_left == PLUS_EXPR)
11472 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11473 "suggest parentheses around %<+%> in operand of %<&%>");
11474 else if (code_right == PLUS_EXPR)
11475 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11476 "suggest parentheses around %<+%> in operand of %<&%>");
11477 else if (code_left == MINUS_EXPR)
11478 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11479 "suggest parentheses around %<-%> in operand of %<&%>");
11480 else if (code_right == MINUS_EXPR)
11481 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11482 "suggest parentheses around %<-%> in operand of %<&%>");
11483 /* Check cases like x&y==z */
11484 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11485 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11486 "suggest parentheses around comparison in operand of %<&%>");
11487 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11488 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11489 "suggest parentheses around comparison in operand of %<&%>");
11490 /* Check cases like !x & y */
11491 else if (code_left == TRUTH_NOT_EXPR
11492 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11493 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11494 "suggest parentheses around operand of "
11495 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11496 return;
11498 case EQ_EXPR:
11499 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11500 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11501 "suggest parentheses around comparison in operand of %<==%>");
11502 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11503 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11504 "suggest parentheses around comparison in operand of %<==%>");
11505 return;
11506 case NE_EXPR:
11507 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11508 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11509 "suggest parentheses around comparison in operand of %<!=%>");
11510 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11511 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11512 "suggest parentheses around comparison in operand of %<!=%>");
11513 return;
11515 default:
11516 if (TREE_CODE_CLASS (code) == tcc_comparison)
11518 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11519 && code_left != NE_EXPR && code_left != EQ_EXPR
11520 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11521 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11522 "comparisons like %<X<=Y<=Z%> do not "
11523 "have their mathematical meaning");
11524 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11525 && code_right != NE_EXPR && code_right != EQ_EXPR
11526 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11527 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11528 "comparisons like %<X<=Y<=Z%> do not "
11529 "have their mathematical meaning");
11531 return;
11533 #undef NOT_A_BOOLEAN_EXPR_P
11536 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11538 void
11539 warn_for_unused_label (tree label)
11541 if (!TREE_USED (label))
11543 if (DECL_INITIAL (label))
11544 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11545 else
11546 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11550 /* Warn for division by zero according to the value of DIVISOR. LOC
11551 is the location of the division operator. */
11553 void
11554 warn_for_div_by_zero (location_t loc, tree divisor)
11556 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11557 about division by zero. Do not issue a warning if DIVISOR has a
11558 floating-point type, since we consider 0.0/0.0 a valid way of
11559 generating a NaN. */
11560 if (c_inhibit_evaluation_warnings == 0
11561 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11562 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11565 /* Subroutine of build_binary_op. Give warnings for comparisons
11566 between signed and unsigned quantities that may fail. Do the
11567 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11568 so that casts will be considered, but default promotions won't
11571 LOCATION is the location of the comparison operator.
11573 The arguments of this function map directly to local variables
11574 of build_binary_op. */
11576 void
11577 warn_for_sign_compare (location_t location,
11578 tree orig_op0, tree orig_op1,
11579 tree op0, tree op1,
11580 tree result_type, enum tree_code resultcode)
11582 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11583 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11584 int unsignedp0, unsignedp1;
11586 /* In C++, check for comparison of different enum types. */
11587 if (c_dialect_cxx()
11588 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11589 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11590 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11591 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11593 warning_at (location,
11594 OPT_Wsign_compare, "comparison between types %qT and %qT",
11595 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11598 /* Do not warn if the comparison is being done in a signed type,
11599 since the signed type will only be chosen if it can represent
11600 all the values of the unsigned type. */
11601 if (!TYPE_UNSIGNED (result_type))
11602 /* OK */;
11603 /* Do not warn if both operands are unsigned. */
11604 else if (op0_signed == op1_signed)
11605 /* OK */;
11606 else
11608 tree sop, uop, base_type;
11609 bool ovf;
11611 if (op0_signed)
11612 sop = orig_op0, uop = orig_op1;
11613 else
11614 sop = orig_op1, uop = orig_op0;
11616 STRIP_TYPE_NOPS (sop);
11617 STRIP_TYPE_NOPS (uop);
11618 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11619 ? TREE_TYPE (result_type) : result_type);
11621 /* Do not warn if the signed quantity is an unsuffixed integer
11622 literal (or some static constant expression involving such
11623 literals or a conditional expression involving such literals)
11624 and it is non-negative. */
11625 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11626 /* OK */;
11627 /* Do not warn if the comparison is an equality operation, the
11628 unsigned quantity is an integral constant, and it would fit
11629 in the result if the result were signed. */
11630 else if (TREE_CODE (uop) == INTEGER_CST
11631 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11632 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11633 /* OK */;
11634 /* In C, do not warn if the unsigned quantity is an enumeration
11635 constant and its maximum value would fit in the result if the
11636 result were signed. */
11637 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11638 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11639 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11640 c_common_signed_type (base_type)))
11641 /* OK */;
11642 else
11643 warning_at (location,
11644 OPT_Wsign_compare,
11645 "comparison between signed and unsigned integer expressions");
11648 /* Warn if two unsigned values are being compared in a size larger
11649 than their original size, and one (and only one) is the result of
11650 a `~' operator. This comparison will always fail.
11652 Also warn if one operand is a constant, and the constant does not
11653 have all bits set that are set in the ~ operand when it is
11654 extended. */
11656 op0 = c_common_get_narrower (op0, &unsignedp0);
11657 op1 = c_common_get_narrower (op1, &unsignedp1);
11659 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11660 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11662 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11663 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11664 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11665 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11667 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11669 tree primop;
11670 HOST_WIDE_INT constant, mask;
11671 int unsignedp;
11672 unsigned int bits;
11674 if (tree_fits_shwi_p (op0))
11676 primop = op1;
11677 unsignedp = unsignedp1;
11678 constant = tree_to_shwi (op0);
11680 else
11682 primop = op0;
11683 unsignedp = unsignedp0;
11684 constant = tree_to_shwi (op1);
11687 bits = TYPE_PRECISION (TREE_TYPE (primop));
11688 if (bits < TYPE_PRECISION (result_type)
11689 && bits < HOST_BITS_PER_LONG && unsignedp)
11691 mask = (~ (HOST_WIDE_INT) 0) << bits;
11692 if ((mask & constant) != mask)
11694 if (constant == 0)
11695 warning_at (location, OPT_Wsign_compare,
11696 "promoted ~unsigned is always non-zero");
11697 else
11698 warning_at (location, OPT_Wsign_compare,
11699 "comparison of promoted ~unsigned with constant");
11703 else if (unsignedp0 && unsignedp1
11704 && (TYPE_PRECISION (TREE_TYPE (op0))
11705 < TYPE_PRECISION (result_type))
11706 && (TYPE_PRECISION (TREE_TYPE (op1))
11707 < TYPE_PRECISION (result_type)))
11708 warning_at (location, OPT_Wsign_compare,
11709 "comparison of promoted ~unsigned with unsigned");
11713 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11714 type via c_common_type. If -Wdouble-promotion is in use, and the
11715 conditions for warning have been met, issue a warning. GMSGID is
11716 the warning message. It must have two %T specifiers for the type
11717 that was converted (generally "float") and the type to which it was
11718 converted (generally "double), respectively. LOC is the location
11719 to which the awrning should refer. */
11721 void
11722 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11723 const char *gmsgid, location_t loc)
11725 tree source_type;
11727 if (!warn_double_promotion)
11728 return;
11729 /* If the conversion will not occur at run-time, there is no need to
11730 warn about it. */
11731 if (c_inhibit_evaluation_warnings)
11732 return;
11733 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11734 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11735 return;
11736 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11737 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11738 source_type = type1;
11739 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11740 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11741 source_type = type2;
11742 else
11743 return;
11744 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11747 /* Setup a TYPE_DECL node as a typedef representation.
11749 X is a TYPE_DECL for a typedef statement. Create a brand new
11750 ..._TYPE node (which will be just a variant of the existing
11751 ..._TYPE node with identical properties) and then install X
11752 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11754 The whole point here is to end up with a situation where each
11755 and every ..._TYPE node the compiler creates will be uniquely
11756 associated with AT MOST one node representing a typedef name.
11757 This way, even though the compiler substitutes corresponding
11758 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11759 early on, later parts of the compiler can always do the reverse
11760 translation and get back the corresponding typedef name. For
11761 example, given:
11763 typedef struct S MY_TYPE;
11764 MY_TYPE object;
11766 Later parts of the compiler might only know that `object' was of
11767 type `struct S' if it were not for code just below. With this
11768 code however, later parts of the compiler see something like:
11770 struct S' == struct S
11771 typedef struct S' MY_TYPE;
11772 struct S' object;
11774 And they can then deduce (from the node for type struct S') that
11775 the original object declaration was:
11777 MY_TYPE object;
11779 Being able to do this is important for proper support of protoize,
11780 and also for generating precise symbolic debugging information
11781 which takes full account of the programmer's (typedef) vocabulary.
11783 Obviously, we don't want to generate a duplicate ..._TYPE node if
11784 the TYPE_DECL node that we are now processing really represents a
11785 standard built-in type. */
11787 void
11788 set_underlying_type (tree x)
11790 if (x == error_mark_node)
11791 return;
11792 if (DECL_IS_BUILTIN (x))
11794 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11795 TYPE_NAME (TREE_TYPE (x)) = x;
11797 else if (TREE_TYPE (x) != error_mark_node
11798 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11800 tree tt = TREE_TYPE (x);
11801 DECL_ORIGINAL_TYPE (x) = tt;
11802 tt = build_variant_type_copy (tt);
11803 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11804 TYPE_NAME (tt) = x;
11805 TREE_USED (tt) = TREE_USED (x);
11806 TREE_TYPE (x) = tt;
11810 /* Record the types used by the current global variable declaration
11811 being parsed, so that we can decide later to emit their debug info.
11812 Those types are in types_used_by_cur_var_decl, and we are going to
11813 store them in the types_used_by_vars_hash hash table.
11814 DECL is the declaration of the global variable that has been parsed. */
11816 void
11817 record_types_used_by_current_var_decl (tree decl)
11819 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11821 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11823 tree type = types_used_by_cur_var_decl->pop ();
11824 types_used_by_var_decl_insert (type, decl);
11828 /* If DECL is a typedef that is declared in the current function,
11829 record it for the purpose of -Wunused-local-typedefs. */
11831 void
11832 record_locally_defined_typedef (tree decl)
11834 struct c_language_function *l;
11836 if (!warn_unused_local_typedefs
11837 || cfun == NULL
11838 /* if this is not a locally defined typedef then we are not
11839 interested. */
11840 || !is_typedef_decl (decl)
11841 || !decl_function_context (decl))
11842 return;
11844 l = (struct c_language_function *) cfun->language;
11845 vec_safe_push (l->local_typedefs, decl);
11848 /* If T is a TYPE_DECL declared locally, mark it as used. */
11850 void
11851 maybe_record_typedef_use (tree t)
11853 if (!is_typedef_decl (t))
11854 return;
11856 TREE_USED (t) = true;
11859 /* Warn if there are some unused locally defined typedefs in the
11860 current function. */
11862 void
11863 maybe_warn_unused_local_typedefs (void)
11865 int i;
11866 tree decl;
11867 /* The number of times we have emitted -Wunused-local-typedefs
11868 warnings. If this is different from errorcount, that means some
11869 unrelated errors have been issued. In which case, we'll avoid
11870 emitting "unused-local-typedefs" warnings. */
11871 static int unused_local_typedefs_warn_count;
11872 struct c_language_function *l;
11874 if (cfun == NULL)
11875 return;
11877 if ((l = (struct c_language_function *) cfun->language) == NULL)
11878 return;
11880 if (warn_unused_local_typedefs
11881 && errorcount == unused_local_typedefs_warn_count)
11883 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11884 if (!TREE_USED (decl))
11885 warning_at (DECL_SOURCE_LOCATION (decl),
11886 OPT_Wunused_local_typedefs,
11887 "typedef %qD locally defined but not used", decl);
11888 unused_local_typedefs_warn_count = errorcount;
11891 vec_free (l->local_typedefs);
11894 /* Warn about boolean expression compared with an integer value different
11895 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11896 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11897 are the operands of the comparison. The caller must ensure that
11898 either operand is a boolean expression. */
11900 void
11901 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11902 tree op1)
11904 if (TREE_CODE_CLASS (code) != tcc_comparison)
11905 return;
11907 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11908 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11909 if (!cst)
11910 return;
11912 if (!integer_zerop (cst) && !integer_onep (cst))
11914 int sign = (TREE_CODE (op0) == INTEGER_CST
11915 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
11916 if (code == EQ_EXPR
11917 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11918 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11919 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11920 "with boolean expression is always false", cst);
11921 else
11922 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11923 "with boolean expression is always true", cst);
11925 else if (integer_zerop (cst) || integer_onep (cst))
11927 /* If the non-constant operand isn't of a boolean type, we
11928 don't want to warn here. */
11929 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
11930 /* Handle booleans promoted to integers. */
11931 if (CONVERT_EXPR_P (noncst)
11932 && TREE_TYPE (noncst) == integer_type_node
11933 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
11934 /* Warn. */;
11935 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
11936 && !truth_value_p (TREE_CODE (noncst)))
11937 return;
11938 /* Do some magic to get the right diagnostics. */
11939 bool flag = TREE_CODE (op0) == INTEGER_CST;
11940 flag = integer_zerop (cst) ? flag : !flag;
11941 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
11942 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11943 "with boolean expression is always true", cst);
11944 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
11945 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11946 "with boolean expression is always false", cst);
11950 /* The C and C++ parsers both use vectors to hold function arguments.
11951 For efficiency, we keep a cache of unused vectors. This is the
11952 cache. */
11954 typedef vec<tree, va_gc> *tree_gc_vec;
11955 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11957 /* Return a new vector from the cache. If the cache is empty,
11958 allocate a new vector. These vectors are GC'ed, so it is OK if the
11959 pointer is not released.. */
11961 vec<tree, va_gc> *
11962 make_tree_vector (void)
11964 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11965 return tree_vector_cache->pop ();
11966 else
11968 /* Passing 0 to vec::alloc returns NULL, and our callers require
11969 that we always return a non-NULL value. The vector code uses
11970 4 when growing a NULL vector, so we do too. */
11971 vec<tree, va_gc> *v;
11972 vec_alloc (v, 4);
11973 return v;
11977 /* Release a vector of trees back to the cache. */
11979 void
11980 release_tree_vector (vec<tree, va_gc> *vec)
11982 if (vec != NULL)
11984 vec->truncate (0);
11985 vec_safe_push (tree_vector_cache, vec);
11989 /* Get a new tree vector holding a single tree. */
11991 vec<tree, va_gc> *
11992 make_tree_vector_single (tree t)
11994 vec<tree, va_gc> *ret = make_tree_vector ();
11995 ret->quick_push (t);
11996 return ret;
11999 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12001 vec<tree, va_gc> *
12002 make_tree_vector_from_list (tree list)
12004 vec<tree, va_gc> *ret = make_tree_vector ();
12005 for (; list; list = TREE_CHAIN (list))
12006 vec_safe_push (ret, TREE_VALUE (list));
12007 return ret;
12010 /* Get a new tree vector which is a copy of an existing one. */
12012 vec<tree, va_gc> *
12013 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12015 vec<tree, va_gc> *ret;
12016 unsigned int ix;
12017 tree t;
12019 ret = make_tree_vector ();
12020 vec_safe_reserve (ret, vec_safe_length (orig));
12021 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12022 ret->quick_push (t);
12023 return ret;
12026 /* Return true if KEYWORD starts a type specifier. */
12028 bool
12029 keyword_begins_type_specifier (enum rid keyword)
12031 switch (keyword)
12033 case RID_AUTO_TYPE:
12034 case RID_INT:
12035 case RID_CHAR:
12036 case RID_FLOAT:
12037 case RID_DOUBLE:
12038 case RID_VOID:
12039 case RID_UNSIGNED:
12040 case RID_LONG:
12041 case RID_SHORT:
12042 case RID_SIGNED:
12043 case RID_DFLOAT32:
12044 case RID_DFLOAT64:
12045 case RID_DFLOAT128:
12046 case RID_FRACT:
12047 case RID_ACCUM:
12048 case RID_BOOL:
12049 case RID_WCHAR:
12050 case RID_CHAR16:
12051 case RID_CHAR32:
12052 case RID_SAT:
12053 case RID_COMPLEX:
12054 case RID_TYPEOF:
12055 case RID_STRUCT:
12056 case RID_CLASS:
12057 case RID_UNION:
12058 case RID_ENUM:
12059 return true;
12060 default:
12061 if (keyword >= RID_FIRST_INT_N
12062 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12063 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12064 return true;
12065 return false;
12069 /* Return true if KEYWORD names a type qualifier. */
12071 bool
12072 keyword_is_type_qualifier (enum rid keyword)
12074 switch (keyword)
12076 case RID_CONST:
12077 case RID_VOLATILE:
12078 case RID_RESTRICT:
12079 case RID_ATOMIC:
12080 return true;
12081 default:
12082 return false;
12086 /* Return true if KEYWORD names a storage class specifier.
12088 RID_TYPEDEF is not included in this list despite `typedef' being
12089 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12090 such for syntactic convenience only. */
12092 bool
12093 keyword_is_storage_class_specifier (enum rid keyword)
12095 switch (keyword)
12097 case RID_STATIC:
12098 case RID_EXTERN:
12099 case RID_REGISTER:
12100 case RID_AUTO:
12101 case RID_MUTABLE:
12102 case RID_THREAD:
12103 return true;
12104 default:
12105 return false;
12109 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12111 static bool
12112 keyword_is_function_specifier (enum rid keyword)
12114 switch (keyword)
12116 case RID_INLINE:
12117 case RID_NORETURN:
12118 case RID_VIRTUAL:
12119 case RID_EXPLICIT:
12120 return true;
12121 default:
12122 return false;
12126 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12127 declaration-specifier (C99 6.7). */
12129 bool
12130 keyword_is_decl_specifier (enum rid keyword)
12132 if (keyword_is_storage_class_specifier (keyword)
12133 || keyword_is_type_qualifier (keyword)
12134 || keyword_is_function_specifier (keyword))
12135 return true;
12137 switch (keyword)
12139 case RID_TYPEDEF:
12140 case RID_FRIEND:
12141 case RID_CONSTEXPR:
12142 return true;
12143 default:
12144 return false;
12148 /* Initialize language-specific-bits of tree_contains_struct. */
12150 void
12151 c_common_init_ts (void)
12153 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12154 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12155 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12158 /* Build a user-defined numeric literal out of an integer constant type VALUE
12159 with identifier SUFFIX. */
12161 tree
12162 build_userdef_literal (tree suffix_id, tree value,
12163 enum overflow_type overflow, tree num_string)
12165 tree literal = make_node (USERDEF_LITERAL);
12166 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12167 USERDEF_LITERAL_VALUE (literal) = value;
12168 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12169 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12170 return literal;
12173 /* For vector[index], convert the vector to a
12174 pointer of the underlying type. Return true if the resulting
12175 ARRAY_REF should not be an lvalue. */
12177 bool
12178 convert_vector_to_pointer_for_subscript (location_t loc,
12179 tree *vecp, tree index)
12181 bool ret = false;
12182 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12184 tree type = TREE_TYPE (*vecp);
12185 tree type1;
12187 ret = !lvalue_p (*vecp);
12188 if (TREE_CODE (index) == INTEGER_CST)
12189 if (!tree_fits_uhwi_p (index)
12190 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12191 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12193 if (ret)
12195 tree tmp = create_tmp_var_raw (type);
12196 DECL_SOURCE_LOCATION (tmp) = loc;
12197 *vecp = c_save_expr (*vecp);
12198 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12200 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12201 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12202 *vecp
12203 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12204 *vecp, NULL_TREE, NULL_TREE),
12205 non_const);
12207 else
12208 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12209 NULL_TREE, NULL_TREE);
12210 SET_EXPR_LOCATION (*vecp, loc);
12211 c_common_mark_addressable_vec (tmp);
12213 else
12214 c_common_mark_addressable_vec (*vecp);
12215 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12216 type1 = build_pointer_type (TREE_TYPE (*vecp));
12217 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12218 if (!ref_all
12219 && !DECL_P (*vecp))
12221 /* If the original vector isn't declared may_alias and it
12222 isn't a bare vector look if the subscripting would
12223 alias the vector we subscript, and if not, force ref-all. */
12224 alias_set_type vecset = get_alias_set (*vecp);
12225 alias_set_type sset = get_alias_set (type);
12226 if (!alias_sets_must_conflict_p (sset, vecset)
12227 && !alias_set_subset_of (sset, vecset))
12228 ref_all = true;
12230 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12231 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12232 *vecp = convert (type, *vecp);
12234 return ret;
12237 /* Determine which of the operands, if any, is a scalar that needs to be
12238 converted to a vector, for the range of operations. */
12239 enum stv_conv
12240 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12241 bool complain)
12243 tree type0 = TREE_TYPE (op0);
12244 tree type1 = TREE_TYPE (op1);
12245 bool integer_only_op = false;
12246 enum stv_conv ret = stv_firstarg;
12248 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12249 || TREE_CODE (type1) == VECTOR_TYPE);
12250 switch (code)
12252 /* Most GENERIC binary expressions require homogeneous arguments.
12253 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12254 argument that is a vector and a second one that is a scalar, so
12255 we never return stv_secondarg for them. */
12256 case RSHIFT_EXPR:
12257 case LSHIFT_EXPR:
12258 if (TREE_CODE (type0) == INTEGER_TYPE
12259 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12261 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12263 if (complain)
12264 error_at (loc, "conversion of scalar %qT to vector %qT "
12265 "involves truncation", type0, type1);
12266 return stv_error;
12268 else
12269 return stv_firstarg;
12271 break;
12273 case BIT_IOR_EXPR:
12274 case BIT_XOR_EXPR:
12275 case BIT_AND_EXPR:
12276 integer_only_op = true;
12277 /* ... fall through ... */
12279 case VEC_COND_EXPR:
12281 case PLUS_EXPR:
12282 case MINUS_EXPR:
12283 case MULT_EXPR:
12284 case TRUNC_DIV_EXPR:
12285 case CEIL_DIV_EXPR:
12286 case FLOOR_DIV_EXPR:
12287 case ROUND_DIV_EXPR:
12288 case EXACT_DIV_EXPR:
12289 case TRUNC_MOD_EXPR:
12290 case FLOOR_MOD_EXPR:
12291 case RDIV_EXPR:
12292 case EQ_EXPR:
12293 case NE_EXPR:
12294 case LE_EXPR:
12295 case GE_EXPR:
12296 case LT_EXPR:
12297 case GT_EXPR:
12298 /* What about UNLT_EXPR? */
12299 if (TREE_CODE (type0) == VECTOR_TYPE)
12301 tree tmp;
12302 ret = stv_secondarg;
12303 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12304 tmp = type0; type0 = type1; type1 = tmp;
12305 tmp = op0; op0 = op1; op1 = tmp;
12308 if (TREE_CODE (type0) == INTEGER_TYPE
12309 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12311 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12313 if (complain)
12314 error_at (loc, "conversion of scalar %qT to vector %qT "
12315 "involves truncation", type0, type1);
12316 return stv_error;
12318 return ret;
12320 else if (!integer_only_op
12321 /* Allow integer --> real conversion if safe. */
12322 && (TREE_CODE (type0) == REAL_TYPE
12323 || TREE_CODE (type0) == INTEGER_TYPE)
12324 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12326 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12328 if (complain)
12329 error_at (loc, "conversion of scalar %qT to vector %qT "
12330 "involves truncation", type0, type1);
12331 return stv_error;
12333 return ret;
12335 default:
12336 break;
12339 return stv_nothing;
12342 /* Return true iff ALIGN is an integral constant that is a fundamental
12343 alignment, as defined by [basic.align] in the c++-11
12344 specifications.
12346 That is:
12348 [A fundamental alignment is represented by an alignment less than or
12349 equal to the greatest alignment supported by the implementation
12350 in all contexts, which is equal to
12351 alignof(max_align_t)]. */
12353 bool
12354 cxx_fundamental_alignment_p (unsigned align)
12356 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12357 TYPE_ALIGN (long_double_type_node)));
12360 /* Return true if T is a pointer to a zero-sized aggregate. */
12362 bool
12363 pointer_to_zero_sized_aggr_p (tree t)
12365 if (!POINTER_TYPE_P (t))
12366 return false;
12367 t = TREE_TYPE (t);
12368 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12371 #include "gt-c-family-c-common.h"