2014-12-19 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / c-family / c-common.c
blob3f12cadf2cdedaba7bfde941287f253e8b9d5988
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "c-common.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "trans-mem.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "hash-map.h"
50 #include "is-a.h"
51 #include "plugin-api.h"
52 #include "vec.h"
53 #include "hash-set.h"
54 #include "machmode.h"
55 #include "hard-reg-set.h"
56 #include "input.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "target-def.h"
61 #include "gimplify.h"
62 #include "wide-int-print.h"
63 #include "gimple-expr.h"
65 cpp_reader *parse_in; /* Declared in c-pragma.h. */
67 /* The following symbols are subsumed in the c_global_trees array, and
68 listed here individually for documentation purposes.
70 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
72 tree short_integer_type_node;
73 tree long_integer_type_node;
74 tree long_long_integer_type_node;
76 tree short_unsigned_type_node;
77 tree long_unsigned_type_node;
78 tree long_long_unsigned_type_node;
80 tree truthvalue_type_node;
81 tree truthvalue_false_node;
82 tree truthvalue_true_node;
84 tree ptrdiff_type_node;
86 tree unsigned_char_type_node;
87 tree signed_char_type_node;
88 tree wchar_type_node;
90 tree char16_type_node;
91 tree char32_type_node;
93 tree float_type_node;
94 tree double_type_node;
95 tree long_double_type_node;
97 tree complex_integer_type_node;
98 tree complex_float_type_node;
99 tree complex_double_type_node;
100 tree complex_long_double_type_node;
102 tree dfloat32_type_node;
103 tree dfloat64_type_node;
104 tree_dfloat128_type_node;
106 tree intQI_type_node;
107 tree intHI_type_node;
108 tree intSI_type_node;
109 tree intDI_type_node;
110 tree intTI_type_node;
112 tree unsigned_intQI_type_node;
113 tree unsigned_intHI_type_node;
114 tree unsigned_intSI_type_node;
115 tree unsigned_intDI_type_node;
116 tree unsigned_intTI_type_node;
118 tree widest_integer_literal_type_node;
119 tree widest_unsigned_literal_type_node;
121 Nodes for types `void *' and `const void *'.
123 tree ptr_type_node, const_ptr_type_node;
125 Nodes for types `char *' and `const char *'.
127 tree string_type_node, const_string_type_node;
129 Type `char[SOMENUMBER]'.
130 Used when an array of char is needed and the size is irrelevant.
132 tree char_array_type_node;
134 Type `wchar_t[SOMENUMBER]' or something like it.
135 Used when a wide string literal is created.
137 tree wchar_array_type_node;
139 Type `char16_t[SOMENUMBER]' or something like it.
140 Used when a UTF-16 string literal is created.
142 tree char16_array_type_node;
144 Type `char32_t[SOMENUMBER]' or something like it.
145 Used when a UTF-32 string literal is created.
147 tree char32_array_type_node;
149 Type `int ()' -- used for implicit declaration of functions.
151 tree default_function_type;
153 A VOID_TYPE node, packaged in a TREE_LIST.
155 tree void_list_node;
157 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
158 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
159 VAR_DECLS, but C++ does.)
161 tree function_name_decl_node;
162 tree pretty_function_name_decl_node;
163 tree c99_function_name_decl_node;
165 Stack of nested function name VAR_DECLs.
167 tree saved_function_name_decls;
171 tree c_global_trees[CTI_MAX];
173 /* Switches common to the C front ends. */
175 /* Nonzero means don't output line number information. */
177 char flag_no_line_commands;
179 /* Nonzero causes -E output not to be done, but directives such as
180 #define that have side effects are still obeyed. */
182 char flag_no_output;
184 /* Nonzero means dump macros in some fashion. */
186 char flag_dump_macros;
188 /* Nonzero means pass #include lines through to the output. */
190 char flag_dump_includes;
192 /* Nonzero means process PCH files while preprocessing. */
194 bool flag_pch_preprocess;
196 /* The file name to which we should write a precompiled header, or
197 NULL if no header will be written in this compile. */
199 const char *pch_file;
201 /* Nonzero if an ISO standard was selected. It rejects macros in the
202 user's namespace. */
203 int flag_iso;
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
215 int flag_isoc94;
217 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
219 int flag_isoc99;
221 /* Nonzero means use the ISO C11 dialect of C. */
223 int flag_isoc11;
225 /* Nonzero means that we have builtin functions, and main is an int. */
227 int flag_hosted = 1;
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice excessively recursive template instantiations.
259 The default is lower than the 1024 recommended by the C++0x standard
260 because G++ runs out of stack before 1024 with highly recursive template
261 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
263 int max_tinst_depth = 900;
265 /* The elements of `ridpointers' are identifier nodes for the reserved
266 type names and storage classes. It is indexed by a RID_... value. */
267 tree *ridpointers;
269 tree (*make_fname_decl) (location_t, tree, int);
271 /* Nonzero means don't warn about problems that occur when the code is
272 executed. */
273 int c_inhibit_evaluation_warnings;
275 /* Whether we are building a boolean conversion inside
276 convert_for_assignment, or some other late binary operation. If
277 build_binary_op is called for C (from code shared by C and C++) in
278 this case, then the operands have already been folded and the
279 result will not be folded again, so C_MAYBE_CONST_EXPR should not
280 be generated. */
281 bool in_late_binary_op;
283 /* Whether lexing has been completed, so subsequent preprocessor
284 errors should use the compiler's input_location. */
285 bool done_lexing = false;
287 /* Information about how a function name is generated. */
288 struct fname_var_t
290 tree *const decl; /* pointer to the VAR_DECL. */
291 const unsigned rid; /* RID number for the identifier. */
292 const int pretty; /* How pretty is it? */
295 /* The three ways of getting then name of the current function. */
297 const struct fname_var_t fname_vars[] =
299 /* C99 compliant __func__, must be first. */
300 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
301 /* GCC __FUNCTION__ compliant. */
302 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
303 /* GCC __PRETTY_FUNCTION__ compliant. */
304 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
305 {NULL, 0, 0},
308 /* Global visibility options. */
309 struct visibility_flags visibility_options;
311 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
312 static tree check_case_value (location_t, tree);
313 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
315 static tree handle_packed_decl_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_packed_type_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
323 int, bool *);
324 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
325 int, bool *);
326 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_always_inline_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_unused_decl_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_unused_type_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
343 bool *);
344 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_transparent_union_decl_attribute (tree *, tree, tree,
346 int, bool *);
347 static tree handle_transparent_union_type_attribute (tree *, tree, tree,
348 int, bool *);
349 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_aligned_type_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_aligned_decl_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
356 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
357 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
360 static tree handle_visibility_decl_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_visibility_type_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_tls_model_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_no_instrument_function_attribute (tree *, tree,
367 tree, int, bool *);
368 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_deprecated_type_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_deprecated_decl_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_decl_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_warn_unused_type_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
400 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
401 bool *);
402 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
403 bool *);
404 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
406 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
407 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
409 static void check_function_nonnull (tree, int, tree *);
410 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
411 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
412 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
413 static int resort_field_decl_cmp (const void *, const void *);
415 /* Reserved words. The third field is a mask: keywords are disabled
416 if they match the mask.
418 Masks for languages:
419 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
420 C --std=c99: D_CXXONLY | D_OBJC
421 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
422 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
423 C++ --std=c0x: D_CONLY | D_OBJC
424 ObjC++ is like C++ except that D_OBJC is not set
426 If -fno-asm is used, D_ASM is added to the mask. If
427 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
428 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
429 In C with -Wc++-compat, we warn if D_CXXWARN is set.
431 Note the complication of the D_CXX_OBJC keywords. These are
432 reserved words such as 'class'. In C++, 'class' is a reserved
433 word. In Objective-C++ it is too. In Objective-C, it is a
434 reserved word too, but only if it follows an '@' sign.
436 const struct c_common_resword c_common_reswords[] =
438 { "_Alignas", RID_ALIGNAS, D_CONLY },
439 { "_Alignof", RID_ALIGNOF, D_CONLY },
440 { "_Atomic", RID_ATOMIC, D_CONLY },
441 { "_Bool", RID_BOOL, D_CONLY },
442 { "_Complex", RID_COMPLEX, 0 },
443 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
444 { "_Cilk_sync", RID_CILK_SYNC, 0 },
445 { "_Cilk_for", RID_CILK_FOR, 0 },
446 { "_Imaginary", RID_IMAGINARY, D_CONLY },
447 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
448 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
449 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
450 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
451 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
452 { "_Sat", RID_SAT, D_CONLY | D_EXT },
453 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
454 { "_Noreturn", RID_NORETURN, D_CONLY },
455 { "_Generic", RID_GENERIC, D_CONLY },
456 { "_Thread_local", RID_THREAD, D_CONLY },
457 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
458 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
459 { "__alignof", RID_ALIGNOF, 0 },
460 { "__alignof__", RID_ALIGNOF, 0 },
461 { "__asm", RID_ASM, 0 },
462 { "__asm__", RID_ASM, 0 },
463 { "__attribute", RID_ATTRIBUTE, 0 },
464 { "__attribute__", RID_ATTRIBUTE, 0 },
465 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
466 { "__bases", RID_BASES, D_CXXONLY },
467 { "__builtin_call_with_static_chain",
468 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
469 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
470 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
471 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
472 { "__builtin_offsetof", RID_OFFSETOF, 0 },
473 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
474 { "__builtin_va_arg", RID_VA_ARG, 0 },
475 { "__complex", RID_COMPLEX, 0 },
476 { "__complex__", RID_COMPLEX, 0 },
477 { "__const", RID_CONST, 0 },
478 { "__const__", RID_CONST, 0 },
479 { "__decltype", RID_DECLTYPE, D_CXXONLY },
480 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
481 { "__extension__", RID_EXTENSION, 0 },
482 { "__func__", RID_C99_FUNCTION_NAME, 0 },
483 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
484 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
485 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
486 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
487 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
488 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
489 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
490 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
491 { "__imag", RID_IMAGPART, 0 },
492 { "__imag__", RID_IMAGPART, 0 },
493 { "__inline", RID_INLINE, 0 },
494 { "__inline__", RID_INLINE, 0 },
495 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
496 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
497 { "__is_class", RID_IS_CLASS, D_CXXONLY },
498 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
499 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
500 { "__is_final", RID_IS_FINAL, D_CXXONLY },
501 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
502 { "__is_pod", RID_IS_POD, D_CXXONLY },
503 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
504 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
505 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
506 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
507 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
508 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
509 { "__is_union", RID_IS_UNION, D_CXXONLY },
510 { "__label__", RID_LABEL, 0 },
511 { "__null", RID_NULL, 0 },
512 { "__real", RID_REALPART, 0 },
513 { "__real__", RID_REALPART, 0 },
514 { "__restrict", RID_RESTRICT, 0 },
515 { "__restrict__", RID_RESTRICT, 0 },
516 { "__signed", RID_SIGNED, 0 },
517 { "__signed__", RID_SIGNED, 0 },
518 { "__thread", RID_THREAD, 0 },
519 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
520 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
521 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
522 { "__typeof", RID_TYPEOF, 0 },
523 { "__typeof__", RID_TYPEOF, 0 },
524 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
525 { "__volatile", RID_VOLATILE, 0 },
526 { "__volatile__", RID_VOLATILE, 0 },
527 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "asm", RID_ASM, D_ASM },
530 { "auto", RID_AUTO, 0 },
531 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
532 { "break", RID_BREAK, 0 },
533 { "case", RID_CASE, 0 },
534 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
535 { "char", RID_CHAR, 0 },
536 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
539 { "const", RID_CONST, 0 },
540 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
541 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
542 { "continue", RID_CONTINUE, 0 },
543 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
544 { "default", RID_DEFAULT, 0 },
545 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
546 { "do", RID_DO, 0 },
547 { "double", RID_DOUBLE, 0 },
548 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
549 { "else", RID_ELSE, 0 },
550 { "enum", RID_ENUM, 0 },
551 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
552 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
553 { "extern", RID_EXTERN, 0 },
554 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
555 { "float", RID_FLOAT, 0 },
556 { "for", RID_FOR, 0 },
557 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
558 { "goto", RID_GOTO, 0 },
559 { "if", RID_IF, 0 },
560 { "inline", RID_INLINE, D_EXT89 },
561 { "int", RID_INT, 0 },
562 { "long", RID_LONG, 0 },
563 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
564 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
565 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
566 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
568 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
569 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
570 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
571 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
572 { "register", RID_REGISTER, 0 },
573 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
574 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
575 { "return", RID_RETURN, 0 },
576 { "short", RID_SHORT, 0 },
577 { "signed", RID_SIGNED, 0 },
578 { "sizeof", RID_SIZEOF, 0 },
579 { "static", RID_STATIC, 0 },
580 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
581 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
582 { "struct", RID_STRUCT, 0 },
583 { "switch", RID_SWITCH, 0 },
584 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
585 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
586 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
587 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
588 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
589 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
590 { "typedef", RID_TYPEDEF, 0 },
591 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
592 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
593 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
594 { "union", RID_UNION, 0 },
595 { "unsigned", RID_UNSIGNED, 0 },
596 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
597 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
598 { "void", RID_VOID, 0 },
599 { "volatile", RID_VOLATILE, 0 },
600 { "wchar_t", RID_WCHAR, D_CXXONLY },
601 { "while", RID_WHILE, 0 },
602 /* These Objective-C keywords are recognized only immediately after
603 an '@'. */
604 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 { "defs", RID_AT_DEFS, D_OBJC },
606 { "encode", RID_AT_ENCODE, D_OBJC },
607 { "end", RID_AT_END, D_OBJC },
608 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 { "interface", RID_AT_INTERFACE, D_OBJC },
610 { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 { "selector", RID_AT_SELECTOR, D_OBJC },
612 { "finally", RID_AT_FINALLY, D_OBJC },
613 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
614 { "optional", RID_AT_OPTIONAL, D_OBJC },
615 { "required", RID_AT_REQUIRED, D_OBJC },
616 { "property", RID_AT_PROPERTY, D_OBJC },
617 { "package", RID_AT_PACKAGE, D_OBJC },
618 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
619 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
620 /* These are recognized only in protocol-qualifier context
621 (see above) */
622 { "bycopy", RID_BYCOPY, D_OBJC },
623 { "byref", RID_BYREF, D_OBJC },
624 { "in", RID_IN, D_OBJC },
625 { "inout", RID_INOUT, D_OBJC },
626 { "oneway", RID_ONEWAY, D_OBJC },
627 { "out", RID_OUT, D_OBJC },
628 /* These are recognized inside a property attribute list */
629 { "assign", RID_ASSIGN, D_OBJC },
630 { "copy", RID_COPY, D_OBJC },
631 { "getter", RID_GETTER, D_OBJC },
632 { "nonatomic", RID_NONATOMIC, D_OBJC },
633 { "readonly", RID_READONLY, D_OBJC },
634 { "readwrite", RID_READWRITE, D_OBJC },
635 { "retain", RID_RETAIN, D_OBJC },
636 { "setter", RID_SETTER, D_OBJC },
639 const unsigned int num_c_common_reswords =
640 sizeof c_common_reswords / sizeof (struct c_common_resword);
642 /* Table of machine-independent attributes common to all C-like languages.
644 All attributes referencing arguments should be additionally processed
645 in chkp_copy_function_type_adding_bounds for correct instrumentation
646 by Pointer Bounds Checker.
647 Current list of processed common attributes: nonnull. */
648 const struct attribute_spec c_common_attribute_table[] =
650 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, decl_handler,
651 type_handler, affects_type_identity } */
652 { "packed", 0, 0, false, false, false,
653 handle_packed_decl_attribute,
654 handle_packed_type_attribute, false},
655 { "nocommon", 0, 0, true, false, false,
656 handle_nocommon_attribute, NULL, false},
657 { "common", 0, 0, true, false, false,
658 handle_common_attribute, NULL, false },
659 /* FIXME: logically, noreturn attributes should be listed as
660 "false, true, true" and apply to function types. But implementing this
661 would require all the places in the compiler that use TREE_THIS_VOLATILE
662 on a decl to identify non-returning functions to be located and fixed
663 to check the function type instead. */
664 { "noreturn", 0, 0, true, false, false,
665 handle_noreturn_attribute, NULL, false },
666 { "volatile", 0, 0, true, false, false,
667 handle_noreturn_attribute, NULL, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, NULL, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, NULL, false },
672 { "leaf", 0, 0, true, false, false,
673 handle_leaf_attribute, NULL, false },
674 { "always_inline", 0, 0, true, false, false,
675 handle_always_inline_attribute, NULL, false },
676 { "gnu_inline", 0, 0, true, false, false,
677 handle_gnu_inline_attribute, NULL, false },
678 { "artificial", 0, 0, true, false, false,
679 handle_artificial_attribute, NULL, false },
680 { "flatten", 0, 0, true, false, false,
681 handle_flatten_attribute, NULL, false },
682 { "used", 0, 0, true, false, false,
683 handle_used_attribute, NULL, false },
684 { "unused", 0, 0, false, false, false,
685 handle_unused_decl_attribute,
686 handle_unused_type_attribute, false },
687 { "externally_visible", 0, 0, true, false, false,
688 handle_externally_visible_attribute, NULL,
689 false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, NULL, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, NULL, false },
695 { "transparent_union", 0, 0, false, false, false,
696 handle_transparent_union_decl_attribute,
697 handle_transparent_union_type_attribute, false },
698 { "constructor", 0, 1, true, false, false,
699 handle_constructor_attribute, NULL, false },
700 { "destructor", 0, 1, true, false, false,
701 handle_destructor_attribute, NULL, false },
702 { "mode", 1, 1, false, true, false, NULL,
703 handle_mode_attribute, false },
704 { "section", 1, 1, true, false, false,
705 handle_section_attribute, NULL, false },
706 { "aligned", 0, 1, false, false, false,
707 handle_aligned_decl_attribute,
708 handle_aligned_type_attribute, false },
709 { "weak", 0, 0, true, false, false,
710 handle_weak_attribute, NULL, false },
711 { "ifunc", 1, 1, true, false, false,
712 handle_ifunc_attribute, NULL, false },
713 { "alias", 1, 1, true, false, false,
714 handle_alias_attribute, NULL, false },
715 { "weakref", 0, 1, true, false, false,
716 handle_weakref_attribute, NULL, false },
717 { "no_instrument_function", 0, 0, true, false, false,
718 handle_no_instrument_function_attribute,
719 NULL, false },
720 { "malloc", 0, 0, true, false, false,
721 handle_malloc_attribute, NULL, false },
722 { "returns_twice", 0, 0, true, false, false,
723 handle_returns_twice_attribute, NULL, false },
724 { "no_stack_limit", 0, 0, true, false, false,
725 handle_no_limit_stack_attribute, NULL, false },
726 { "pure", 0, 0, true, false, false,
727 handle_pure_attribute, NULL, false },
728 { "transaction_callable", 0, 0, false, true, false, NULL,
729 handle_tm_attribute, false },
730 { "transaction_unsafe", 0, 0, false, true, false,
731 NULL, handle_tm_attribute, false },
732 { "transaction_safe", 0, 0, false, true, false,
733 NULL, handle_tm_attribute, false },
734 { "transaction_may_cancel_outer", 0, 0, false, true, false,
735 NULL, handle_tm_attribute, false },
736 /* ??? These two attributes didn't make the transition from the
737 Intel language document to the multi-vendor language document. */
738 { "transaction_pure", 0, 0, false, true, false,
739 NULL, handle_tm_attribute, false },
740 { "transaction_wrap", 1, 1, true, false, false,
741 handle_tm_wrap_attribute, NULL, false },
742 /* For internal use (marking of builtins) only. The name contains space
743 to prevent its usage in source code. */
744 { "no vops", 0, 0, true, false, false,
745 handle_novops_attribute, NULL, false },
746 { "deprecated", 0, 1, false, false, false,
747 handle_deprecated_decl_attribute,
748 handle_deprecated_type_attribute, false },
749 { "vector_size", 1, 1, false, true, false,
750 NULL, handle_vector_size_attribute, false },
751 { "visibility", 1, 1, false, false, false,
752 handle_visibility_decl_attribute,
753 handle_visibility_type_attribute, false },
754 { "tls_model", 1, 1, true, false, false,
755 handle_tls_model_attribute, NULL, false },
756 { "nonnull", 0, -1, false, true, true,
757 NULL, handle_nonnull_attribute, false },
758 { "nothrow", 0, 0, true, false, false,
759 handle_nothrow_attribute, NULL, false },
760 { "may_alias", 0, 0, false, true, false, NULL, NULL, false },
761 { "cleanup", 1, 1, true, false, false,
762 handle_cleanup_attribute, NULL, false },
763 { "warn_unused_result", 0, 0, false, true, true, NULL,
764 handle_warn_unused_result_attribute, false },
765 { "sentinel", 0, 1, false, true, true,
766 NULL, handle_sentinel_attribute, false },
767 /* For internal use (marking of builtins) only. The name contains space
768 to prevent its usage in source code. */
769 { "type generic", 0, 0, false, true, true,
770 NULL, handle_type_generic_attribute, false },
771 { "alloc_size", 1, 2, false, true, true,
772 NULL, handle_alloc_size_attribute, false },
773 { "cold", 0, 0, true, false, false,
774 handle_cold_attribute, NULL, false },
775 { "hot", 0, 0, true, false, false,
776 handle_hot_attribute, NULL, false },
777 { "no_address_safety_analysis",
778 0, 0, true, false, false,
779 handle_no_address_safety_analysis_attribute,
780 NULL, false },
781 { "no_sanitize_address", 0, 0, true, false, false,
782 handle_no_sanitize_address_attribute,
783 NULL, false },
784 { "no_sanitize_undefined", 0, 0, true, false, false,
785 handle_no_sanitize_undefined_attribute,
786 NULL, false },
787 { "warning", 1, 1, true, false, false,
788 handle_error_attribute, NULL, false },
789 { "error", 1, 1, true, false, false,
790 handle_error_attribute, NULL, false },
791 { "target", 1, -1, true, false, false,
792 handle_target_attribute, NULL, false },
793 { "optimize", 1, -1, true, false, false,
794 handle_optimize_attribute, NULL, false },
795 /* For internal use only. The leading '*' both prevents its usage in
796 source code and signals that it may be overridden by machine tables. */
797 { "*tm regparm", 0, 0, false, true, true,
798 NULL, ignore_attribute, false },
799 { "no_split_stack", 0, 0, true, false, false,
800 handle_no_split_stack_attribute, NULL, false },
801 /* For internal use (marking of builtins and runtime functions) only.
802 The name contains space to prevent its usage in source code. */
803 { "fn spec", 1, 1, false, true, true,
804 NULL, handle_fnspec_attribute, false },
805 { "warn_unused", 0, 0, false, false, false,
806 handle_warn_unused_decl_attribute,
807 handle_warn_unused_type_attribute, false },
808 { "returns_nonnull", 0, 0, false, true, true,
809 NULL, handle_returns_nonnull_attribute, false },
810 { "omp declare simd", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, NULL, false },
812 { "cilk simd function", 0, -1, true, false, false,
813 handle_omp_declare_simd_attribute, NULL, false },
814 { "omp declare target", 0, 0, true, false, false,
815 handle_omp_declare_target_attribute, NULL,
816 false },
817 { "alloc_align", 1, 1, false, true, true,
818 NULL, handle_alloc_align_attribute, false },
819 { "assume_aligned", 1, 2, false, true, true,
820 NULL, handle_assume_aligned_attribute, false },
821 { "designated_init", 0, 0, false, true, false,
822 NULL, handle_designated_init_attribute, false },
823 { "bnd_variable_size", 0, 0, true, false, false,
824 handle_bnd_variable_size_attribute, NULL, false },
825 { "bnd_legacy", 0, 0, true, false, false,
826 handle_bnd_legacy, NULL, false },
827 { "bnd_instrument", 0, 0, true, false, false,
828 handle_bnd_instrument, NULL, false },
829 { NULL, 0, 0, false, false, false, NULL, NULL, false }
832 /* Give the specifications for the format attributes, used by C and all
833 descendants.
835 All attributes referencing arguments should be additionally processed
836 in chkp_copy_function_type_adding_bounds for correct instrumentation
837 by Pointer Bounds Checker.
838 Current list of processed format attributes: format, format_arg. */
839 const struct attribute_spec c_common_format_attribute_table[] =
841 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
842 affects_type_identity } */
843 { "format", 3, 3, false, true, true,
844 NULL, handle_format_attribute, false },
845 { "format_arg", 1, 1, false, true, true,
846 NULL, handle_format_arg_attribute, false },
847 { NULL, 0, 0, false, false, false, NULL, NULL, false }
850 /* Return identifier for address space AS. */
852 const char *
853 c_addr_space_name (addr_space_t as)
855 int rid = RID_FIRST_ADDR_SPACE + as;
856 gcc_assert (ridpointers [rid]);
857 return IDENTIFIER_POINTER (ridpointers [rid]);
860 /* Push current bindings for the function name VAR_DECLS. */
862 void
863 start_fname_decls (void)
865 unsigned ix;
866 tree saved = NULL_TREE;
868 for (ix = 0; fname_vars[ix].decl; ix++)
870 tree decl = *fname_vars[ix].decl;
872 if (decl)
874 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
875 saved);
876 *fname_vars[ix].decl = NULL_TREE;
879 if (saved || saved_function_name_decls)
880 /* Normally they'll have been NULL, so only push if we've got a
881 stack, or they are non-NULL. */
882 saved_function_name_decls = tree_cons (saved, NULL_TREE,
883 saved_function_name_decls);
886 /* Finish up the current bindings, adding them into the current function's
887 statement tree. This must be done _before_ finish_stmt_tree is called.
888 If there is no current function, we must be at file scope and no statements
889 are involved. Pop the previous bindings. */
891 void
892 finish_fname_decls (void)
894 unsigned ix;
895 tree stmts = NULL_TREE;
896 tree stack = saved_function_name_decls;
898 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
899 append_to_statement_list (TREE_VALUE (stack), &stmts);
901 if (stmts)
903 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
905 if (TREE_CODE (*bodyp) == BIND_EXPR)
906 bodyp = &BIND_EXPR_BODY (*bodyp);
908 append_to_statement_list_force (*bodyp, &stmts);
909 *bodyp = stmts;
912 for (ix = 0; fname_vars[ix].decl; ix++)
913 *fname_vars[ix].decl = NULL_TREE;
915 if (stack)
917 /* We had saved values, restore them. */
918 tree saved;
920 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
922 tree decl = TREE_PURPOSE (saved);
923 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
925 *fname_vars[ix].decl = decl;
927 stack = TREE_CHAIN (stack);
929 saved_function_name_decls = stack;
932 /* Return the text name of the current function, suitably prettified
933 by PRETTY_P. Return string must be freed by caller. */
935 const char *
936 fname_as_string (int pretty_p)
938 const char *name = "top level";
939 char *namep;
940 int vrb = 2, len;
941 cpp_string cstr = { 0, 0 }, strname;
943 if (!pretty_p)
945 name = "";
946 vrb = 0;
949 if (current_function_decl)
950 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
952 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
954 namep = XNEWVEC (char, len);
955 snprintf (namep, len, "\"%s\"", name);
956 strname.text = (unsigned char *) namep;
957 strname.len = len - 1;
959 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
961 XDELETEVEC (namep);
962 return (const char *) cstr.text;
965 return namep;
968 /* Return the VAR_DECL for a const char array naming the current
969 function. If the VAR_DECL has not yet been created, create it
970 now. RID indicates how it should be formatted and IDENTIFIER_NODE
971 ID is its name (unfortunately C and C++ hold the RID values of
972 keywords in different places, so we can't derive RID from ID in
973 this language independent code. LOC is the location of the
974 function. */
976 tree
977 fname_decl (location_t loc, unsigned int rid, tree id)
979 unsigned ix;
980 tree decl = NULL_TREE;
982 for (ix = 0; fname_vars[ix].decl; ix++)
983 if (fname_vars[ix].rid == rid)
984 break;
986 decl = *fname_vars[ix].decl;
987 if (!decl)
989 /* If a tree is built here, it would normally have the lineno of
990 the current statement. Later this tree will be moved to the
991 beginning of the function and this line number will be wrong.
992 To avoid this problem set the lineno to 0 here; that prevents
993 it from appearing in the RTL. */
994 tree stmts;
995 location_t saved_location = input_location;
996 input_location = UNKNOWN_LOCATION;
998 stmts = push_stmt_list ();
999 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1000 stmts = pop_stmt_list (stmts);
1001 if (!IS_EMPTY_STMT (stmts))
1002 saved_function_name_decls
1003 = tree_cons (decl, stmts, saved_function_name_decls);
1004 *fname_vars[ix].decl = decl;
1005 input_location = saved_location;
1007 if (!ix && !current_function_decl)
1008 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1010 return decl;
1013 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1015 tree
1016 fix_string_type (tree value)
1018 int length = TREE_STRING_LENGTH (value);
1019 int nchars;
1020 tree e_type, i_type, a_type;
1022 /* Compute the number of elements, for the array type. */
1023 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1025 nchars = length;
1026 e_type = char_type_node;
1028 else if (TREE_TYPE (value) == char16_array_type_node)
1030 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1031 e_type = char16_type_node;
1033 else if (TREE_TYPE (value) == char32_array_type_node)
1035 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1036 e_type = char32_type_node;
1038 else
1040 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1041 e_type = wchar_type_node;
1044 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1045 limit in C++98 Annex B is very large (65536) and is not normative,
1046 so we do not diagnose it (warn_overlength_strings is forced off
1047 in c_common_post_options). */
1048 if (warn_overlength_strings)
1050 const int nchars_max = flag_isoc99 ? 4095 : 509;
1051 const int relevant_std = flag_isoc99 ? 99 : 90;
1052 if (nchars - 1 > nchars_max)
1053 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1054 separate the %d from the 'C'. 'ISO' should not be
1055 translated, but it may be moved after 'C%d' in languages
1056 where modifiers follow nouns. */
1057 pedwarn (input_location, OPT_Woverlength_strings,
1058 "string length %qd is greater than the length %qd "
1059 "ISO C%d compilers are required to support",
1060 nchars - 1, nchars_max, relevant_std);
1063 /* Create the array type for the string constant. The ISO C++
1064 standard says that a string literal has type `const char[N]' or
1065 `const wchar_t[N]'. We use the same logic when invoked as a C
1066 front-end with -Wwrite-strings.
1067 ??? We should change the type of an expression depending on the
1068 state of a warning flag. We should just be warning -- see how
1069 this is handled in the C++ front-end for the deprecated implicit
1070 conversion from string literals to `char*' or `wchar_t*'.
1072 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1073 array type being the unqualified version of that type.
1074 Therefore, if we are constructing an array of const char, we must
1075 construct the matching unqualified array type first. The C front
1076 end does not require this, but it does no harm, so we do it
1077 unconditionally. */
1078 i_type = build_index_type (size_int (nchars - 1));
1079 a_type = build_array_type (e_type, i_type);
1080 if (c_dialect_cxx() || warn_write_strings)
1081 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1083 TREE_TYPE (value) = a_type;
1084 TREE_CONSTANT (value) = 1;
1085 TREE_READONLY (value) = 1;
1086 TREE_STATIC (value) = 1;
1087 return value;
1090 /* If DISABLE is true, stop issuing warnings. This is used when
1091 parsing code that we know will not be executed. This function may
1092 be called multiple times, and works as a stack. */
1094 static void
1095 c_disable_warnings (bool disable)
1097 if (disable)
1099 ++c_inhibit_evaluation_warnings;
1100 fold_defer_overflow_warnings ();
1104 /* If ENABLE is true, reenable issuing warnings. */
1106 static void
1107 c_enable_warnings (bool enable)
1109 if (enable)
1111 --c_inhibit_evaluation_warnings;
1112 fold_undefer_and_ignore_overflow_warnings ();
1116 /* Fully fold EXPR, an expression that was not folded (beyond integer
1117 constant expressions and null pointer constants) when being built
1118 up. If IN_INIT, this is in a static initializer and certain
1119 changes are made to the folding done. Clear *MAYBE_CONST if
1120 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1121 expression because it contains an evaluated operator (in C99) or an
1122 operator outside of sizeof returning an integer constant (in C90)
1123 not permitted in constant expressions, or because it contains an
1124 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1125 set to true by callers before calling this function.) Return the
1126 folded expression. Function arguments have already been folded
1127 before calling this function, as have the contents of SAVE_EXPR,
1128 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1129 C_MAYBE_CONST_EXPR. */
1131 tree
1132 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1134 tree ret;
1135 tree eptype = NULL_TREE;
1136 bool dummy = true;
1137 bool maybe_const_itself = true;
1138 location_t loc = EXPR_LOCATION (expr);
1140 /* This function is not relevant to C++ because C++ folds while
1141 parsing, and may need changes to be correct for C++ when C++
1142 stops folding while parsing. */
1143 if (c_dialect_cxx ())
1144 gcc_unreachable ();
1146 if (!maybe_const)
1147 maybe_const = &dummy;
1148 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1150 eptype = TREE_TYPE (expr);
1151 expr = TREE_OPERAND (expr, 0);
1153 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1154 &maybe_const_itself);
1155 if (eptype)
1156 ret = fold_convert_loc (loc, eptype, ret);
1157 *maybe_const &= maybe_const_itself;
1158 return ret;
1161 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1162 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1163 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1164 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1165 both evaluated and unevaluated subexpressions while
1166 *MAYBE_CONST_ITSELF is carried from only evaluated
1167 subexpressions). */
1169 static tree
1170 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1171 bool *maybe_const_itself)
1173 tree ret = expr;
1174 enum tree_code code = TREE_CODE (expr);
1175 enum tree_code_class kind = TREE_CODE_CLASS (code);
1176 location_t loc = EXPR_LOCATION (expr);
1177 tree op0, op1, op2, op3;
1178 tree orig_op0, orig_op1, orig_op2;
1179 bool op0_const = true, op1_const = true, op2_const = true;
1180 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1181 bool nowarning = TREE_NO_WARNING (expr);
1182 bool unused_p;
1184 /* This function is not relevant to C++ because C++ folds while
1185 parsing, and may need changes to be correct for C++ when C++
1186 stops folding while parsing. */
1187 if (c_dialect_cxx ())
1188 gcc_unreachable ();
1190 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1191 anything else not counted as an expression cannot usefully be
1192 folded further at this point. */
1193 if (!IS_EXPR_CODE_CLASS (kind)
1194 || kind == tcc_statement
1195 || code == SAVE_EXPR)
1196 return expr;
1198 /* Operands of variable-length expressions (function calls) have
1199 already been folded, as have __builtin_* function calls, and such
1200 expressions cannot occur in constant expressions. */
1201 if (kind == tcc_vl_exp)
1203 *maybe_const_operands = false;
1204 ret = fold (expr);
1205 goto out;
1208 if (code == C_MAYBE_CONST_EXPR)
1210 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1211 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1212 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1213 *maybe_const_operands = false;
1214 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1215 *maybe_const_itself = false;
1216 if (pre && !in_init)
1217 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1218 else
1219 ret = inner;
1220 goto out;
1223 /* Assignment, increment, decrement, function call and comma
1224 operators, and statement expressions, cannot occur in constant
1225 expressions if evaluated / outside of sizeof. (Function calls
1226 were handled above, though VA_ARG_EXPR is treated like a function
1227 call here, and statement expressions are handled through
1228 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1229 switch (code)
1231 case MODIFY_EXPR:
1232 case PREDECREMENT_EXPR:
1233 case PREINCREMENT_EXPR:
1234 case POSTDECREMENT_EXPR:
1235 case POSTINCREMENT_EXPR:
1236 case COMPOUND_EXPR:
1237 *maybe_const_operands = false;
1238 break;
1240 case VA_ARG_EXPR:
1241 case TARGET_EXPR:
1242 case BIND_EXPR:
1243 case OBJ_TYPE_REF:
1244 *maybe_const_operands = false;
1245 ret = fold (expr);
1246 goto out;
1248 default:
1249 break;
1252 /* Fold individual tree codes as appropriate. */
1253 switch (code)
1255 case COMPOUND_LITERAL_EXPR:
1256 /* Any non-constancy will have been marked in a containing
1257 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1258 goto out;
1260 case COMPONENT_REF:
1261 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1262 op1 = TREE_OPERAND (expr, 1);
1263 op2 = TREE_OPERAND (expr, 2);
1264 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1265 maybe_const_itself);
1266 STRIP_TYPE_NOPS (op0);
1267 if (op0 != orig_op0)
1268 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1269 if (ret != expr)
1271 TREE_READONLY (ret) = TREE_READONLY (expr);
1272 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1274 goto out;
1276 case ARRAY_REF:
1277 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1278 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1279 op2 = TREE_OPERAND (expr, 2);
1280 op3 = TREE_OPERAND (expr, 3);
1281 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1282 maybe_const_itself);
1283 STRIP_TYPE_NOPS (op0);
1284 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1285 maybe_const_itself);
1286 STRIP_TYPE_NOPS (op1);
1287 op1 = decl_constant_value_for_optimization (op1);
1288 if (op0 != orig_op0 || op1 != orig_op1)
1289 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1290 if (ret != expr)
1292 TREE_READONLY (ret) = TREE_READONLY (expr);
1293 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1294 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1296 ret = fold (ret);
1297 goto out;
1299 case COMPOUND_EXPR:
1300 case MODIFY_EXPR:
1301 case PREDECREMENT_EXPR:
1302 case PREINCREMENT_EXPR:
1303 case POSTDECREMENT_EXPR:
1304 case POSTINCREMENT_EXPR:
1305 case PLUS_EXPR:
1306 case MINUS_EXPR:
1307 case MULT_EXPR:
1308 case POINTER_PLUS_EXPR:
1309 case TRUNC_DIV_EXPR:
1310 case CEIL_DIV_EXPR:
1311 case FLOOR_DIV_EXPR:
1312 case TRUNC_MOD_EXPR:
1313 case RDIV_EXPR:
1314 case EXACT_DIV_EXPR:
1315 case LSHIFT_EXPR:
1316 case RSHIFT_EXPR:
1317 case BIT_IOR_EXPR:
1318 case BIT_XOR_EXPR:
1319 case BIT_AND_EXPR:
1320 case LT_EXPR:
1321 case LE_EXPR:
1322 case GT_EXPR:
1323 case GE_EXPR:
1324 case EQ_EXPR:
1325 case NE_EXPR:
1326 case COMPLEX_EXPR:
1327 case TRUTH_AND_EXPR:
1328 case TRUTH_OR_EXPR:
1329 case TRUTH_XOR_EXPR:
1330 case UNORDERED_EXPR:
1331 case ORDERED_EXPR:
1332 case UNLT_EXPR:
1333 case UNLE_EXPR:
1334 case UNGT_EXPR:
1335 case UNGE_EXPR:
1336 case UNEQ_EXPR:
1337 /* Binary operations evaluating both arguments (increment and
1338 decrement are binary internally in GCC). */
1339 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1340 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1341 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1342 maybe_const_itself);
1343 STRIP_TYPE_NOPS (op0);
1344 if (code != MODIFY_EXPR
1345 && code != PREDECREMENT_EXPR
1346 && code != PREINCREMENT_EXPR
1347 && code != POSTDECREMENT_EXPR
1348 && code != POSTINCREMENT_EXPR)
1349 op0 = decl_constant_value_for_optimization (op0);
1350 /* The RHS of a MODIFY_EXPR was fully folded when building that
1351 expression for the sake of conversion warnings. */
1352 if (code != MODIFY_EXPR)
1353 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1354 maybe_const_itself);
1355 STRIP_TYPE_NOPS (op1);
1356 op1 = decl_constant_value_for_optimization (op1);
1357 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1358 ret = in_init
1359 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1360 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1361 else
1362 ret = fold (expr);
1363 if (TREE_OVERFLOW_P (ret)
1364 && !TREE_OVERFLOW_P (op0)
1365 && !TREE_OVERFLOW_P (op1))
1366 overflow_warning (EXPR_LOCATION (expr), ret);
1367 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1368 && TREE_CODE (orig_op1) != INTEGER_CST
1369 && TREE_CODE (op1) == INTEGER_CST
1370 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1371 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1372 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1373 && c_inhibit_evaluation_warnings == 0)
1375 if (tree_int_cst_sgn (op1) < 0)
1376 warning_at (loc, 0, (code == LSHIFT_EXPR
1377 ? G_("left shift count is negative")
1378 : G_("right shift count is negative")));
1379 else if (compare_tree_int (op1,
1380 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1381 >= 0)
1382 warning_at (loc, 0, (code == LSHIFT_EXPR
1383 ? G_("left shift count >= width of type")
1384 : G_("right shift count >= width of type")));
1386 goto out;
1388 case INDIRECT_REF:
1389 case FIX_TRUNC_EXPR:
1390 case FLOAT_EXPR:
1391 CASE_CONVERT:
1392 case ADDR_SPACE_CONVERT_EXPR:
1393 case VIEW_CONVERT_EXPR:
1394 case NON_LVALUE_EXPR:
1395 case NEGATE_EXPR:
1396 case BIT_NOT_EXPR:
1397 case TRUTH_NOT_EXPR:
1398 case ADDR_EXPR:
1399 case CONJ_EXPR:
1400 case REALPART_EXPR:
1401 case IMAGPART_EXPR:
1402 /* Unary operations. */
1403 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1404 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1405 maybe_const_itself);
1406 STRIP_TYPE_NOPS (op0);
1407 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1408 op0 = decl_constant_value_for_optimization (op0);
1409 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1410 not prepared to deal with them if they occur in initializers. */
1411 if (op0 != orig_op0
1412 && code == ADDR_EXPR
1413 && (op1 = get_base_address (op0)) != NULL_TREE
1414 && TREE_CODE (op1) == INDIRECT_REF
1415 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1416 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1417 else if (op0 != orig_op0 || in_init)
1418 ret = in_init
1419 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1420 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1421 else
1422 ret = fold (expr);
1423 if (code == INDIRECT_REF
1424 && ret != expr
1425 && TREE_CODE (ret) == INDIRECT_REF)
1427 TREE_READONLY (ret) = TREE_READONLY (expr);
1428 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1429 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1431 switch (code)
1433 case FIX_TRUNC_EXPR:
1434 case FLOAT_EXPR:
1435 CASE_CONVERT:
1436 /* Don't warn about explicit conversions. We will already
1437 have warned about suspect implicit conversions. */
1438 break;
1440 default:
1441 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1442 overflow_warning (EXPR_LOCATION (expr), ret);
1443 break;
1445 goto out;
1447 case TRUTH_ANDIF_EXPR:
1448 case TRUTH_ORIF_EXPR:
1449 /* Binary operations not necessarily evaluating both
1450 arguments. */
1451 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1452 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1453 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1454 STRIP_TYPE_NOPS (op0);
1456 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1457 ? truthvalue_false_node
1458 : truthvalue_true_node));
1459 c_disable_warnings (unused_p);
1460 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1461 STRIP_TYPE_NOPS (op1);
1462 c_enable_warnings (unused_p);
1464 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1465 ret = in_init
1466 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1467 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1468 else
1469 ret = fold (expr);
1470 *maybe_const_operands &= op0_const;
1471 *maybe_const_itself &= op0_const_self;
1472 if (!(flag_isoc99
1473 && op0_const
1474 && op0_const_self
1475 && (code == TRUTH_ANDIF_EXPR
1476 ? op0 == truthvalue_false_node
1477 : op0 == truthvalue_true_node)))
1478 *maybe_const_operands &= op1_const;
1479 if (!(op0_const
1480 && op0_const_self
1481 && (code == TRUTH_ANDIF_EXPR
1482 ? op0 == truthvalue_false_node
1483 : op0 == truthvalue_true_node)))
1484 *maybe_const_itself &= op1_const_self;
1485 goto out;
1487 case COND_EXPR:
1488 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1489 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1490 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1491 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1493 STRIP_TYPE_NOPS (op0);
1494 c_disable_warnings (op0 == truthvalue_false_node);
1495 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1496 STRIP_TYPE_NOPS (op1);
1497 c_enable_warnings (op0 == truthvalue_false_node);
1499 c_disable_warnings (op0 == truthvalue_true_node);
1500 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1501 STRIP_TYPE_NOPS (op2);
1502 c_enable_warnings (op0 == truthvalue_true_node);
1504 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1505 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1506 else
1507 ret = fold (expr);
1508 *maybe_const_operands &= op0_const;
1509 *maybe_const_itself &= op0_const_self;
1510 if (!(flag_isoc99
1511 && op0_const
1512 && op0_const_self
1513 && op0 == truthvalue_false_node))
1514 *maybe_const_operands &= op1_const;
1515 if (!(op0_const
1516 && op0_const_self
1517 && op0 == truthvalue_false_node))
1518 *maybe_const_itself &= op1_const_self;
1519 if (!(flag_isoc99
1520 && op0_const
1521 && op0_const_self
1522 && op0 == truthvalue_true_node))
1523 *maybe_const_operands &= op2_const;
1524 if (!(op0_const
1525 && op0_const_self
1526 && op0 == truthvalue_true_node))
1527 *maybe_const_itself &= op2_const_self;
1528 goto out;
1530 case EXCESS_PRECISION_EXPR:
1531 /* Each case where an operand with excess precision may be
1532 encountered must remove the EXCESS_PRECISION_EXPR around
1533 inner operands and possibly put one around the whole
1534 expression or possibly convert to the semantic type (which
1535 c_fully_fold does); we cannot tell at this stage which is
1536 appropriate in any particular case. */
1537 gcc_unreachable ();
1539 default:
1540 /* Various codes may appear through folding built-in functions
1541 and their arguments. */
1542 goto out;
1545 out:
1546 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1547 have been done by this point, so remove them again. */
1548 nowarning |= TREE_NO_WARNING (ret);
1549 STRIP_TYPE_NOPS (ret);
1550 if (nowarning && !TREE_NO_WARNING (ret))
1552 if (!CAN_HAVE_LOCATION_P (ret))
1553 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1554 TREE_NO_WARNING (ret) = 1;
1556 if (ret != expr)
1557 protected_set_expr_location (ret, loc);
1558 return ret;
1561 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1562 return EXP. Otherwise, return either EXP or its known constant
1563 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1564 Is the BLKmode test appropriate? */
1566 tree
1567 decl_constant_value_for_optimization (tree exp)
1569 tree ret;
1571 /* This function is only used by C, for c_fully_fold and other
1572 optimization, and may not be correct for C++. */
1573 if (c_dialect_cxx ())
1574 gcc_unreachable ();
1576 if (!optimize
1577 || TREE_CODE (exp) != VAR_DECL
1578 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1579 || DECL_MODE (exp) == BLKmode)
1580 return exp;
1582 ret = decl_constant_value (exp);
1583 /* Avoid unwanted tree sharing between the initializer and current
1584 function's body where the tree can be modified e.g. by the
1585 gimplifier. */
1586 if (ret != exp && TREE_STATIC (exp))
1587 ret = unshare_expr (ret);
1588 return ret;
1591 /* Print a warning if a constant expression had overflow in folding.
1592 Invoke this function on every expression that the language
1593 requires to be a constant expression.
1594 Note the ANSI C standard says it is erroneous for a
1595 constant expression to overflow. */
1597 void
1598 constant_expression_warning (tree value)
1600 if (warn_overflow && pedantic
1601 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1602 || TREE_CODE (value) == FIXED_CST
1603 || TREE_CODE (value) == VECTOR_CST
1604 || TREE_CODE (value) == COMPLEX_CST)
1605 && TREE_OVERFLOW (value))
1606 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1609 /* The same as above but print an unconditional error. */
1610 void
1611 constant_expression_error (tree value)
1613 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1614 || TREE_CODE (value) == FIXED_CST
1615 || TREE_CODE (value) == VECTOR_CST
1616 || TREE_CODE (value) == COMPLEX_CST)
1617 && TREE_OVERFLOW (value))
1618 error ("overflow in constant expression");
1621 /* Print a warning if an expression had overflow in folding and its
1622 operands hadn't.
1624 Invoke this function on every expression that
1625 (1) appears in the source code, and
1626 (2) is a constant expression that overflowed, and
1627 (3) is not already checked by convert_and_check;
1628 however, do not invoke this function on operands of explicit casts
1629 or when the expression is the result of an operator and any operand
1630 already overflowed. */
1632 void
1633 overflow_warning (location_t loc, tree value)
1635 if (c_inhibit_evaluation_warnings != 0)
1636 return;
1638 switch (TREE_CODE (value))
1640 case INTEGER_CST:
1641 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1642 break;
1644 case REAL_CST:
1645 warning_at (loc, OPT_Woverflow,
1646 "floating point overflow in expression");
1647 break;
1649 case FIXED_CST:
1650 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1651 break;
1653 case VECTOR_CST:
1654 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1655 break;
1657 case COMPLEX_CST:
1658 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1659 warning_at (loc, OPT_Woverflow,
1660 "complex integer overflow in expression");
1661 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1662 warning_at (loc, OPT_Woverflow,
1663 "complex floating point overflow in expression");
1664 break;
1666 default:
1667 break;
1671 /* Warn about uses of logical || / && operator in a context where it
1672 is likely that the bitwise equivalent was intended by the
1673 programmer. We have seen an expression in which CODE is a binary
1674 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1675 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1676 void
1677 warn_logical_operator (location_t location, enum tree_code code, tree type,
1678 enum tree_code code_left, tree op_left,
1679 enum tree_code ARG_UNUSED (code_right), tree op_right)
1681 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1682 int in0_p, in1_p, in_p;
1683 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1684 bool strict_overflow_p = false;
1686 if (code != TRUTH_ANDIF_EXPR
1687 && code != TRUTH_AND_EXPR
1688 && code != TRUTH_ORIF_EXPR
1689 && code != TRUTH_OR_EXPR)
1690 return;
1692 /* Warn if &&/|| are being used in a context where it is
1693 likely that the bitwise equivalent was intended by the
1694 programmer. That is, an expression such as op && MASK
1695 where op should not be any boolean expression, nor a
1696 constant, and mask seems to be a non-boolean integer constant. */
1697 if (!truth_value_p (code_left)
1698 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1699 && !CONSTANT_CLASS_P (op_left)
1700 && !TREE_NO_WARNING (op_left)
1701 && TREE_CODE (op_right) == INTEGER_CST
1702 && !integer_zerop (op_right)
1703 && !integer_onep (op_right))
1705 if (or_op)
1706 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1707 " applied to non-boolean constant");
1708 else
1709 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1710 " applied to non-boolean constant");
1711 TREE_NO_WARNING (op_left) = true;
1712 return;
1715 /* We do not warn for constants because they are typical of macro
1716 expansions that test for features. */
1717 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1718 return;
1720 /* This warning only makes sense with logical operands. */
1721 if (!(truth_value_p (TREE_CODE (op_left))
1722 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1723 || !(truth_value_p (TREE_CODE (op_right))
1724 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1725 return;
1727 /* The range computations only work with scalars. */
1728 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1729 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1730 return;
1732 /* We first test whether either side separately is trivially true
1733 (with OR) or trivially false (with AND). If so, do not warn.
1734 This is a common idiom for testing ranges of data types in
1735 portable code. */
1736 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1737 if (!lhs)
1738 return;
1739 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1740 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1742 /* If this is an OR operation, invert both sides; now, the result
1743 should be always false to get a warning. */
1744 if (or_op)
1745 in0_p = !in0_p;
1747 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1748 if (tem && integer_zerop (tem))
1749 return;
1751 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1752 if (!rhs)
1753 return;
1754 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1755 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1757 /* If this is an OR operation, invert both sides; now, the result
1758 should be always false to get a warning. */
1759 if (or_op)
1760 in1_p = !in1_p;
1762 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1763 if (tem && integer_zerop (tem))
1764 return;
1766 /* If both expressions have the same operand, if we can merge the
1767 ranges, and if the range test is always false, then warn. */
1768 if (operand_equal_p (lhs, rhs, 0)
1769 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1770 in1_p, low1, high1)
1771 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1772 type, lhs, in_p, low, high))
1773 && integer_zerop (tem))
1775 if (or_op)
1776 warning_at (location, OPT_Wlogical_op,
1777 "logical %<or%> "
1778 "of collectively exhaustive tests is always true");
1779 else
1780 warning_at (location, OPT_Wlogical_op,
1781 "logical %<and%> "
1782 "of mutually exclusive tests is always false");
1786 /* Warn about logical not used on the left hand side operand of a comparison.
1787 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1788 Do not warn if RHS is of a boolean type. */
1790 void
1791 warn_logical_not_parentheses (location_t location, enum tree_code code,
1792 tree rhs)
1794 if (TREE_CODE_CLASS (code) != tcc_comparison
1795 || TREE_TYPE (rhs) == NULL_TREE
1796 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1797 return;
1799 warning_at (location, OPT_Wlogical_not_parentheses,
1800 "logical not is only applied to the left hand side of "
1801 "comparison");
1804 /* Warn if EXP contains any computations whose results are not used.
1805 Return true if a warning is printed; false otherwise. LOCUS is the
1806 (potential) location of the expression. */
1808 bool
1809 warn_if_unused_value (const_tree exp, location_t locus)
1811 restart:
1812 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1813 return false;
1815 /* Don't warn about void constructs. This includes casting to void,
1816 void function calls, and statement expressions with a final cast
1817 to void. */
1818 if (VOID_TYPE_P (TREE_TYPE (exp)))
1819 return false;
1821 if (EXPR_HAS_LOCATION (exp))
1822 locus = EXPR_LOCATION (exp);
1824 switch (TREE_CODE (exp))
1826 case PREINCREMENT_EXPR:
1827 case POSTINCREMENT_EXPR:
1828 case PREDECREMENT_EXPR:
1829 case POSTDECREMENT_EXPR:
1830 case MODIFY_EXPR:
1831 case INIT_EXPR:
1832 case TARGET_EXPR:
1833 case CALL_EXPR:
1834 case TRY_CATCH_EXPR:
1835 case WITH_CLEANUP_EXPR:
1836 case EXIT_EXPR:
1837 case VA_ARG_EXPR:
1838 return false;
1840 case BIND_EXPR:
1841 /* For a binding, warn if no side effect within it. */
1842 exp = BIND_EXPR_BODY (exp);
1843 goto restart;
1845 case SAVE_EXPR:
1846 case NON_LVALUE_EXPR:
1847 case NOP_EXPR:
1848 exp = TREE_OPERAND (exp, 0);
1849 goto restart;
1851 case TRUTH_ORIF_EXPR:
1852 case TRUTH_ANDIF_EXPR:
1853 /* In && or ||, warn if 2nd operand has no side effect. */
1854 exp = TREE_OPERAND (exp, 1);
1855 goto restart;
1857 case COMPOUND_EXPR:
1858 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1859 return true;
1860 /* Let people do `(foo (), 0)' without a warning. */
1861 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1862 return false;
1863 exp = TREE_OPERAND (exp, 1);
1864 goto restart;
1866 case COND_EXPR:
1867 /* If this is an expression with side effects, don't warn; this
1868 case commonly appears in macro expansions. */
1869 if (TREE_SIDE_EFFECTS (exp))
1870 return false;
1871 goto warn;
1873 case INDIRECT_REF:
1874 /* Don't warn about automatic dereferencing of references, since
1875 the user cannot control it. */
1876 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1878 exp = TREE_OPERAND (exp, 0);
1879 goto restart;
1881 /* Fall through. */
1883 default:
1884 /* Referencing a volatile value is a side effect, so don't warn. */
1885 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1886 && TREE_THIS_VOLATILE (exp))
1887 return false;
1889 /* If this is an expression which has no operands, there is no value
1890 to be unused. There are no such language-independent codes,
1891 but front ends may define such. */
1892 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1893 return false;
1895 warn:
1896 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1901 /* Print a warning about casts that might indicate violation
1902 of strict aliasing rules if -Wstrict-aliasing is used and
1903 strict aliasing mode is in effect. OTYPE is the original
1904 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1906 bool
1907 strict_aliasing_warning (tree otype, tree type, tree expr)
1909 /* Strip pointer conversion chains and get to the correct original type. */
1910 STRIP_NOPS (expr);
1911 otype = TREE_TYPE (expr);
1913 if (!(flag_strict_aliasing
1914 && POINTER_TYPE_P (type)
1915 && POINTER_TYPE_P (otype)
1916 && !VOID_TYPE_P (TREE_TYPE (type)))
1917 /* If the type we are casting to is a ref-all pointer
1918 dereferencing it is always valid. */
1919 || TYPE_REF_CAN_ALIAS_ALL (type))
1920 return false;
1922 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1923 && (DECL_P (TREE_OPERAND (expr, 0))
1924 || handled_component_p (TREE_OPERAND (expr, 0))))
1926 /* Casting the address of an object to non void pointer. Warn
1927 if the cast breaks type based aliasing. */
1928 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1930 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1931 "might break strict-aliasing rules");
1932 return true;
1934 else
1936 /* warn_strict_aliasing >= 3. This includes the default (3).
1937 Only warn if the cast is dereferenced immediately. */
1938 alias_set_type set1 =
1939 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1940 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1942 if (set1 != set2 && set2 != 0
1943 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1945 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1946 "pointer will break strict-aliasing rules");
1947 return true;
1949 else if (warn_strict_aliasing == 2
1950 && !alias_sets_must_conflict_p (set1, set2))
1952 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1953 "pointer might break strict-aliasing rules");
1954 return true;
1958 else
1959 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1961 /* At this level, warn for any conversions, even if an address is
1962 not taken in the same statement. This will likely produce many
1963 false positives, but could be useful to pinpoint problems that
1964 are not revealed at higher levels. */
1965 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1966 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1967 if (!COMPLETE_TYPE_P (type)
1968 || !alias_sets_must_conflict_p (set1, set2))
1970 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1971 "pointer might break strict-aliasing rules");
1972 return true;
1976 return false;
1979 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1980 sizeof as last operand of certain builtins. */
1982 void
1983 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1984 vec<tree, va_gc> *params, tree *sizeof_arg,
1985 bool (*comp_types) (tree, tree))
1987 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1988 bool strop = false, cmp = false;
1989 unsigned int idx = ~0;
1990 location_t loc;
1992 if (TREE_CODE (callee) != FUNCTION_DECL
1993 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1994 || vec_safe_length (params) <= 1)
1995 return;
1997 switch (DECL_FUNCTION_CODE (callee))
1999 case BUILT_IN_STRNCMP:
2000 case BUILT_IN_STRNCASECMP:
2001 cmp = true;
2002 /* FALLTHRU */
2003 case BUILT_IN_STRNCPY:
2004 case BUILT_IN_STRNCPY_CHK:
2005 case BUILT_IN_STRNCAT:
2006 case BUILT_IN_STRNCAT_CHK:
2007 case BUILT_IN_STPNCPY:
2008 case BUILT_IN_STPNCPY_CHK:
2009 strop = true;
2010 /* FALLTHRU */
2011 case BUILT_IN_MEMCPY:
2012 case BUILT_IN_MEMCPY_CHK:
2013 case BUILT_IN_MEMMOVE:
2014 case BUILT_IN_MEMMOVE_CHK:
2015 if (params->length () < 3)
2016 return;
2017 src = (*params)[1];
2018 dest = (*params)[0];
2019 idx = 2;
2020 break;
2021 case BUILT_IN_BCOPY:
2022 if (params->length () < 3)
2023 return;
2024 src = (*params)[0];
2025 dest = (*params)[1];
2026 idx = 2;
2027 break;
2028 case BUILT_IN_MEMCMP:
2029 case BUILT_IN_BCMP:
2030 if (params->length () < 3)
2031 return;
2032 src = (*params)[1];
2033 dest = (*params)[0];
2034 idx = 2;
2035 cmp = true;
2036 break;
2037 case BUILT_IN_MEMSET:
2038 case BUILT_IN_MEMSET_CHK:
2039 if (params->length () < 3)
2040 return;
2041 dest = (*params)[0];
2042 idx = 2;
2043 break;
2044 case BUILT_IN_BZERO:
2045 dest = (*params)[0];
2046 idx = 1;
2047 break;
2048 case BUILT_IN_STRNDUP:
2049 src = (*params)[0];
2050 strop = true;
2051 idx = 1;
2052 break;
2053 case BUILT_IN_MEMCHR:
2054 if (params->length () < 3)
2055 return;
2056 src = (*params)[0];
2057 idx = 2;
2058 break;
2059 case BUILT_IN_SNPRINTF:
2060 case BUILT_IN_SNPRINTF_CHK:
2061 case BUILT_IN_VSNPRINTF:
2062 case BUILT_IN_VSNPRINTF_CHK:
2063 dest = (*params)[0];
2064 idx = 1;
2065 strop = true;
2066 break;
2067 default:
2068 break;
2071 if (idx >= 3)
2072 return;
2074 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2075 return;
2077 type = TYPE_P (sizeof_arg[idx])
2078 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2079 if (!POINTER_TYPE_P (type))
2080 return;
2082 if (dest
2083 && (tem = tree_strip_nop_conversions (dest))
2084 && POINTER_TYPE_P (TREE_TYPE (tem))
2085 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2086 return;
2088 if (src
2089 && (tem = tree_strip_nop_conversions (src))
2090 && POINTER_TYPE_P (TREE_TYPE (tem))
2091 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2092 return;
2094 loc = sizeof_arg_loc[idx];
2096 if (dest && !cmp)
2098 if (!TYPE_P (sizeof_arg[idx])
2099 && operand_equal_p (dest, sizeof_arg[idx], 0)
2100 && comp_types (TREE_TYPE (dest), type))
2102 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "expression as the destination; did you mean to "
2106 "remove the addressof?", callee);
2107 else if ((TYPE_PRECISION (TREE_TYPE (type))
2108 == TYPE_PRECISION (char_type_node))
2109 || strop)
2110 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2111 "argument to %<sizeof%> in %qD call is the same "
2112 "expression as the destination; did you mean to "
2113 "provide an explicit length?", callee);
2114 else
2115 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2116 "argument to %<sizeof%> in %qD call is the same "
2117 "expression as the destination; did you mean to "
2118 "dereference it?", callee);
2119 return;
2122 if (POINTER_TYPE_P (TREE_TYPE (dest))
2123 && !strop
2124 && comp_types (TREE_TYPE (dest), type)
2125 && !VOID_TYPE_P (TREE_TYPE (type)))
2127 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2128 "argument to %<sizeof%> in %qD call is the same "
2129 "pointer type %qT as the destination; expected %qT "
2130 "or an explicit length", callee, TREE_TYPE (dest),
2131 TREE_TYPE (TREE_TYPE (dest)));
2132 return;
2136 if (src && !cmp)
2138 if (!TYPE_P (sizeof_arg[idx])
2139 && operand_equal_p (src, sizeof_arg[idx], 0)
2140 && comp_types (TREE_TYPE (src), type))
2142 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "expression as the source; did you mean to "
2146 "remove the addressof?", callee);
2147 else if ((TYPE_PRECISION (TREE_TYPE (type))
2148 == TYPE_PRECISION (char_type_node))
2149 || strop)
2150 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2151 "argument to %<sizeof%> in %qD call is the same "
2152 "expression as the source; did you mean to "
2153 "provide an explicit length?", callee);
2154 else
2155 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2156 "argument to %<sizeof%> in %qD call is the same "
2157 "expression as the source; did you mean to "
2158 "dereference it?", callee);
2159 return;
2162 if (POINTER_TYPE_P (TREE_TYPE (src))
2163 && !strop
2164 && comp_types (TREE_TYPE (src), type)
2165 && !VOID_TYPE_P (TREE_TYPE (type)))
2167 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2168 "argument to %<sizeof%> in %qD call is the same "
2169 "pointer type %qT as the source; expected %qT "
2170 "or an explicit length", callee, TREE_TYPE (src),
2171 TREE_TYPE (TREE_TYPE (src)));
2172 return;
2176 if (dest)
2178 if (!TYPE_P (sizeof_arg[idx])
2179 && operand_equal_p (dest, sizeof_arg[idx], 0)
2180 && comp_types (TREE_TYPE (dest), type))
2182 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "expression as the first source; did you mean to "
2186 "remove the addressof?", callee);
2187 else if ((TYPE_PRECISION (TREE_TYPE (type))
2188 == TYPE_PRECISION (char_type_node))
2189 || strop)
2190 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2191 "argument to %<sizeof%> in %qD call is the same "
2192 "expression as the first source; did you mean to "
2193 "provide an explicit length?", callee);
2194 else
2195 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2196 "argument to %<sizeof%> in %qD call is the same "
2197 "expression as the first source; did you mean to "
2198 "dereference it?", callee);
2199 return;
2202 if (POINTER_TYPE_P (TREE_TYPE (dest))
2203 && !strop
2204 && comp_types (TREE_TYPE (dest), type)
2205 && !VOID_TYPE_P (TREE_TYPE (type)))
2207 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2208 "argument to %<sizeof%> in %qD call is the same "
2209 "pointer type %qT as the first source; expected %qT "
2210 "or an explicit length", callee, TREE_TYPE (dest),
2211 TREE_TYPE (TREE_TYPE (dest)));
2212 return;
2216 if (src)
2218 if (!TYPE_P (sizeof_arg[idx])
2219 && operand_equal_p (src, sizeof_arg[idx], 0)
2220 && comp_types (TREE_TYPE (src), type))
2222 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2223 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2224 "argument to %<sizeof%> in %qD call is the same "
2225 "expression as the second source; did you mean to "
2226 "remove the addressof?", callee);
2227 else if ((TYPE_PRECISION (TREE_TYPE (type))
2228 == TYPE_PRECISION (char_type_node))
2229 || strop)
2230 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2231 "argument to %<sizeof%> in %qD call is the same "
2232 "expression as the second source; did you mean to "
2233 "provide an explicit length?", callee);
2234 else
2235 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2236 "argument to %<sizeof%> in %qD call is the same "
2237 "expression as the second source; did you mean to "
2238 "dereference it?", callee);
2239 return;
2242 if (POINTER_TYPE_P (TREE_TYPE (src))
2243 && !strop
2244 && comp_types (TREE_TYPE (src), type)
2245 && !VOID_TYPE_P (TREE_TYPE (type)))
2247 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2248 "argument to %<sizeof%> in %qD call is the same "
2249 "pointer type %qT as the second source; expected %qT "
2250 "or an explicit length", callee, TREE_TYPE (src),
2251 TREE_TYPE (TREE_TYPE (src)));
2252 return;
2258 /* Warn for unlikely, improbable, or stupid DECL declarations
2259 of `main'. */
2261 void
2262 check_main_parameter_types (tree decl)
2264 function_args_iterator iter;
2265 tree type;
2266 int argct = 0;
2268 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2270 /* XXX void_type_node belies the abstraction. */
2271 if (type == void_type_node || type == error_mark_node )
2272 break;
2274 tree t = type;
2275 if (TYPE_ATOMIC (t))
2276 pedwarn (input_location, OPT_Wmain,
2277 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2278 type, decl);
2279 while (POINTER_TYPE_P (t))
2281 t = TREE_TYPE (t);
2282 if (TYPE_ATOMIC (t))
2283 pedwarn (input_location, OPT_Wmain,
2284 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2285 type, decl);
2288 ++argct;
2289 switch (argct)
2291 case 1:
2292 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2293 pedwarn (input_location, OPT_Wmain,
2294 "first argument of %q+D should be %<int%>", decl);
2295 break;
2297 case 2:
2298 if (TREE_CODE (type) != POINTER_TYPE
2299 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2300 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2301 != char_type_node))
2302 pedwarn (input_location, OPT_Wmain,
2303 "second argument of %q+D should be %<char **%>", decl);
2304 break;
2306 case 3:
2307 if (TREE_CODE (type) != POINTER_TYPE
2308 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2309 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2310 != char_type_node))
2311 pedwarn (input_location, OPT_Wmain,
2312 "third argument of %q+D should probably be "
2313 "%<char **%>", decl);
2314 break;
2318 /* It is intentional that this message does not mention the third
2319 argument because it's only mentioned in an appendix of the
2320 standard. */
2321 if (argct > 0 && (argct < 2 || argct > 3))
2322 pedwarn (input_location, OPT_Wmain,
2323 "%q+D takes only zero or two arguments", decl);
2325 if (stdarg_p (TREE_TYPE (decl)))
2326 pedwarn (input_location, OPT_Wmain,
2327 "%q+D declared as variadic function", decl);
2330 /* vector_targets_convertible_p is used for vector pointer types. The
2331 callers perform various checks that the qualifiers are satisfactory,
2332 while OTOH vector_targets_convertible_p ignores the number of elements
2333 in the vectors. That's fine with vector pointers as we can consider,
2334 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2335 and that does not require and conversion of the pointer values.
2336 In contrast, vector_types_convertible_p and
2337 vector_types_compatible_elements_p are used for vector value types. */
2338 /* True if pointers to distinct types T1 and T2 can be converted to
2339 each other without an explicit cast. Only returns true for opaque
2340 vector types. */
2341 bool
2342 vector_targets_convertible_p (const_tree t1, const_tree t2)
2344 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2345 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2346 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2347 return true;
2349 return false;
2352 /* vector_types_convertible_p is used for vector value types.
2353 It could in principle call vector_targets_convertible_p as a subroutine,
2354 but then the check for vector type would be duplicated with its callers,
2355 and also the purpose of vector_targets_convertible_p would become
2356 muddled.
2357 Where vector_types_convertible_p returns true, a conversion might still be
2358 needed to make the types match.
2359 In contrast, vector_targets_convertible_p is used for vector pointer
2360 values, and vector_types_compatible_elements_p is used specifically
2361 in the context for binary operators, as a check if use is possible without
2362 conversion. */
2363 /* True if vector types T1 and T2 can be converted to each other
2364 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2365 can only be converted with -flax-vector-conversions yet that is not
2366 in effect, emit a note telling the user about that option if such
2367 a note has not previously been emitted. */
2368 bool
2369 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2371 static bool emitted_lax_note = false;
2372 bool convertible_lax;
2374 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2375 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2376 return true;
2378 convertible_lax =
2379 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2380 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2381 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2382 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2383 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2385 if (!convertible_lax || flag_lax_vector_conversions)
2386 return convertible_lax;
2388 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2389 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2390 return true;
2392 if (emit_lax_note && !emitted_lax_note)
2394 emitted_lax_note = true;
2395 inform (input_location, "use -flax-vector-conversions to permit "
2396 "conversions between vectors with differing "
2397 "element types or numbers of subparts");
2400 return false;
2403 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2404 and have vector types, V0 has the same type as V1, and the number of
2405 elements of V0, V1, MASK is the same.
2407 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2408 called with two arguments. In this case implementation passes the
2409 first argument twice in order to share the same tree code. This fact
2410 could enable the mask-values being twice the vector length. This is
2411 an implementation accident and this semantics is not guaranteed to
2412 the user. */
2413 tree
2414 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2415 bool complain)
2417 tree ret;
2418 bool wrap = true;
2419 bool maybe_const = false;
2420 bool two_arguments = false;
2422 if (v1 == NULL_TREE)
2424 two_arguments = true;
2425 v1 = v0;
2428 if (v0 == error_mark_node || v1 == error_mark_node
2429 || mask == error_mark_node)
2430 return error_mark_node;
2432 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2433 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2435 if (complain)
2436 error_at (loc, "__builtin_shuffle last argument must "
2437 "be an integer vector");
2438 return error_mark_node;
2441 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2442 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2444 if (complain)
2445 error_at (loc, "__builtin_shuffle arguments must be vectors");
2446 return error_mark_node;
2449 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2451 if (complain)
2452 error_at (loc, "__builtin_shuffle argument vectors must be of "
2453 "the same type");
2454 return error_mark_node;
2457 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2458 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2459 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2460 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2462 if (complain)
2463 error_at (loc, "__builtin_shuffle number of elements of the "
2464 "argument vector(s) and the mask vector should "
2465 "be the same");
2466 return error_mark_node;
2469 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2470 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2472 if (complain)
2473 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2474 "must have the same size as inner type of the mask");
2475 return error_mark_node;
2478 if (!c_dialect_cxx ())
2480 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2481 v0 = c_fully_fold (v0, false, &maybe_const);
2482 wrap &= maybe_const;
2484 if (two_arguments)
2485 v1 = v0 = save_expr (v0);
2486 else
2488 v1 = c_fully_fold (v1, false, &maybe_const);
2489 wrap &= maybe_const;
2492 mask = c_fully_fold (mask, false, &maybe_const);
2493 wrap &= maybe_const;
2495 else if (two_arguments)
2496 v1 = v0 = save_expr (v0);
2498 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2500 if (!c_dialect_cxx () && !wrap)
2501 ret = c_wrap_maybe_const (ret, true);
2503 return ret;
2506 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2507 to integral type. */
2509 static tree
2510 c_common_get_narrower (tree op, int *unsignedp_ptr)
2512 op = get_narrower (op, unsignedp_ptr);
2514 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2515 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2517 /* C++0x scoped enumerations don't implicitly convert to integral
2518 type; if we stripped an explicit conversion to a larger type we
2519 need to replace it so common_type will still work. */
2520 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2521 TYPE_UNSIGNED (TREE_TYPE (op)));
2522 op = fold_convert (type, op);
2524 return op;
2527 /* This is a helper function of build_binary_op.
2529 For certain operations if both args were extended from the same
2530 smaller type, do the arithmetic in that type and then extend.
2532 BITWISE indicates a bitwise operation.
2533 For them, this optimization is safe only if
2534 both args are zero-extended or both are sign-extended.
2535 Otherwise, we might change the result.
2536 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2537 but calculated in (unsigned short) it would be (unsigned short)-1.
2539 tree
2540 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2542 int unsigned0, unsigned1;
2543 tree arg0, arg1;
2544 int uns;
2545 tree type;
2547 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2548 excessive narrowing when we call get_narrower below. For
2549 example, suppose that OP0 is of unsigned int extended
2550 from signed char and that RESULT_TYPE is long long int.
2551 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2552 like
2554 (long long int) (unsigned int) signed_char
2556 which get_narrower would narrow down to
2558 (unsigned int) signed char
2560 If we do not cast OP0 first, get_narrower would return
2561 signed_char, which is inconsistent with the case of the
2562 explicit cast. */
2563 op0 = convert (result_type, op0);
2564 op1 = convert (result_type, op1);
2566 arg0 = c_common_get_narrower (op0, &unsigned0);
2567 arg1 = c_common_get_narrower (op1, &unsigned1);
2569 /* UNS is 1 if the operation to be done is an unsigned one. */
2570 uns = TYPE_UNSIGNED (result_type);
2572 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2573 but it *requires* conversion to FINAL_TYPE. */
2575 if ((TYPE_PRECISION (TREE_TYPE (op0))
2576 == TYPE_PRECISION (TREE_TYPE (arg0)))
2577 && TREE_TYPE (op0) != result_type)
2578 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2579 if ((TYPE_PRECISION (TREE_TYPE (op1))
2580 == TYPE_PRECISION (TREE_TYPE (arg1)))
2581 && TREE_TYPE (op1) != result_type)
2582 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2584 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2586 /* For bitwise operations, signedness of nominal type
2587 does not matter. Consider only how operands were extended. */
2588 if (bitwise)
2589 uns = unsigned0;
2591 /* Note that in all three cases below we refrain from optimizing
2592 an unsigned operation on sign-extended args.
2593 That would not be valid. */
2595 /* Both args variable: if both extended in same way
2596 from same width, do it in that width.
2597 Do it unsigned if args were zero-extended. */
2598 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2599 < TYPE_PRECISION (result_type))
2600 && (TYPE_PRECISION (TREE_TYPE (arg1))
2601 == TYPE_PRECISION (TREE_TYPE (arg0)))
2602 && unsigned0 == unsigned1
2603 && (unsigned0 || !uns))
2604 return c_common_signed_or_unsigned_type
2605 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2607 else if (TREE_CODE (arg0) == INTEGER_CST
2608 && (unsigned1 || !uns)
2609 && (TYPE_PRECISION (TREE_TYPE (arg1))
2610 < TYPE_PRECISION (result_type))
2611 && (type
2612 = c_common_signed_or_unsigned_type (unsigned1,
2613 TREE_TYPE (arg1)))
2614 && !POINTER_TYPE_P (type)
2615 && int_fits_type_p (arg0, type))
2616 return type;
2618 else if (TREE_CODE (arg1) == INTEGER_CST
2619 && (unsigned0 || !uns)
2620 && (TYPE_PRECISION (TREE_TYPE (arg0))
2621 < TYPE_PRECISION (result_type))
2622 && (type
2623 = c_common_signed_or_unsigned_type (unsigned0,
2624 TREE_TYPE (arg0)))
2625 && !POINTER_TYPE_P (type)
2626 && int_fits_type_p (arg1, type))
2627 return type;
2629 return result_type;
2632 /* Checks if expression EXPR of real/integer type cannot be converted
2633 to the real/integer type TYPE. Function returns non-zero when:
2634 * EXPR is a constant which cannot be exactly converted to TYPE.
2635 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2636 for EXPR type and TYPE being both integers or both real.
2637 * EXPR is not a constant of real type and TYPE is an integer.
2638 * EXPR is not a constant of integer type which cannot be
2639 exactly converted to real type.
2640 Function allows conversions between types of different signedness and
2641 can return SAFE_CONVERSION (zero) in that case. Function can produce
2642 signedness warnings if PRODUCE_WARNS is true. */
2644 enum conversion_safety
2645 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2647 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2648 tree expr_type = TREE_TYPE (expr);
2649 loc = expansion_point_location_if_in_system_header (loc);
2651 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2653 /* Warn for real constant that is not an exact integer converted
2654 to integer type. */
2655 if (TREE_CODE (expr_type) == REAL_TYPE
2656 && TREE_CODE (type) == INTEGER_TYPE)
2658 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2659 give_warning = UNSAFE_REAL;
2661 /* Warn for an integer constant that does not fit into integer type. */
2662 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2663 && TREE_CODE (type) == INTEGER_TYPE
2664 && !int_fits_type_p (expr, type))
2666 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2667 && tree_int_cst_sgn (expr) < 0)
2669 if (produce_warns)
2670 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2671 " implicitly converted to unsigned type");
2673 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2675 if (produce_warns)
2676 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2677 " constant value to negative integer");
2679 else
2680 give_warning = UNSAFE_OTHER;
2682 else if (TREE_CODE (type) == REAL_TYPE)
2684 /* Warn for an integer constant that does not fit into real type. */
2685 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2687 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2688 if (!exact_real_truncate (TYPE_MODE (type), &a))
2689 give_warning = UNSAFE_REAL;
2691 /* Warn for a real constant that does not fit into a smaller
2692 real type. */
2693 else if (TREE_CODE (expr_type) == REAL_TYPE
2694 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2696 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2697 if (!exact_real_truncate (TYPE_MODE (type), &a))
2698 give_warning = UNSAFE_REAL;
2702 else
2704 /* Warn for real types converted to integer types. */
2705 if (TREE_CODE (expr_type) == REAL_TYPE
2706 && TREE_CODE (type) == INTEGER_TYPE)
2707 give_warning = UNSAFE_REAL;
2709 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2710 && TREE_CODE (type) == INTEGER_TYPE)
2712 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2713 expr = get_unwidened (expr, 0);
2714 expr_type = TREE_TYPE (expr);
2716 /* Don't warn for short y; short x = ((int)y & 0xff); */
2717 if (TREE_CODE (expr) == BIT_AND_EXPR
2718 || TREE_CODE (expr) == BIT_IOR_EXPR
2719 || TREE_CODE (expr) == BIT_XOR_EXPR)
2721 /* If both args were extended from a shortest type,
2722 use that type if that is safe. */
2723 expr_type = shorten_binary_op (expr_type,
2724 TREE_OPERAND (expr, 0),
2725 TREE_OPERAND (expr, 1),
2726 /* bitwise */1);
2728 if (TREE_CODE (expr) == BIT_AND_EXPR)
2730 tree op0 = TREE_OPERAND (expr, 0);
2731 tree op1 = TREE_OPERAND (expr, 1);
2732 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2733 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2735 /* If one of the operands is a non-negative constant
2736 that fits in the target type, then the type of the
2737 other operand does not matter. */
2738 if ((TREE_CODE (op0) == INTEGER_CST
2739 && int_fits_type_p (op0, c_common_signed_type (type))
2740 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2741 || (TREE_CODE (op1) == INTEGER_CST
2742 && int_fits_type_p (op1, c_common_signed_type (type))
2743 && int_fits_type_p (op1,
2744 c_common_unsigned_type (type))))
2745 return SAFE_CONVERSION;
2746 /* If constant is unsigned and fits in the target
2747 type, then the result will also fit. */
2748 else if ((TREE_CODE (op0) == INTEGER_CST
2749 && unsigned0
2750 && int_fits_type_p (op0, type))
2751 || (TREE_CODE (op1) == INTEGER_CST
2752 && unsigned1
2753 && int_fits_type_p (op1, type)))
2754 return SAFE_CONVERSION;
2757 /* Warn for integer types converted to smaller integer types. */
2758 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2759 give_warning = UNSAFE_OTHER;
2761 /* When they are the same width but different signedness,
2762 then the value may change. */
2763 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2764 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2765 /* Even when converted to a bigger type, if the type is
2766 unsigned but expr is signed, then negative values
2767 will be changed. */
2768 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2769 && produce_warns)
2770 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2771 "may change the sign of the result",
2772 type, expr_type);
2775 /* Warn for integer types converted to real types if and only if
2776 all the range of values of the integer type cannot be
2777 represented by the real type. */
2778 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2779 && TREE_CODE (type) == REAL_TYPE)
2781 tree type_low_bound, type_high_bound;
2782 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2784 /* Don't warn about char y = 0xff; float x = (int) y; */
2785 expr = get_unwidened (expr, 0);
2786 expr_type = TREE_TYPE (expr);
2788 type_low_bound = TYPE_MIN_VALUE (expr_type);
2789 type_high_bound = TYPE_MAX_VALUE (expr_type);
2790 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2791 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2793 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2794 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2795 give_warning = UNSAFE_OTHER;
2798 /* Warn for real types converted to smaller real types. */
2799 else if (TREE_CODE (expr_type) == REAL_TYPE
2800 && TREE_CODE (type) == REAL_TYPE
2801 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2802 give_warning = UNSAFE_REAL;
2805 return give_warning;
2808 /* Warns if the conversion of EXPR to TYPE may alter a value.
2809 This is a helper function for warnings_for_convert_and_check. */
2811 static void
2812 conversion_warning (location_t loc, tree type, tree expr)
2814 tree expr_type = TREE_TYPE (expr);
2815 enum conversion_safety conversion_kind;
2817 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2818 return;
2820 /* This may happen, because for LHS op= RHS we preevaluate
2821 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2822 means we could no longer see the code of the EXPR. */
2823 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2824 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2825 if (TREE_CODE (expr) == SAVE_EXPR)
2826 expr = TREE_OPERAND (expr, 0);
2828 switch (TREE_CODE (expr))
2830 case EQ_EXPR:
2831 case NE_EXPR:
2832 case LE_EXPR:
2833 case GE_EXPR:
2834 case LT_EXPR:
2835 case GT_EXPR:
2836 case TRUTH_ANDIF_EXPR:
2837 case TRUTH_ORIF_EXPR:
2838 case TRUTH_AND_EXPR:
2839 case TRUTH_OR_EXPR:
2840 case TRUTH_XOR_EXPR:
2841 case TRUTH_NOT_EXPR:
2842 /* Conversion from boolean to a signed:1 bit-field (which only
2843 can hold the values 0 and -1) doesn't lose information - but
2844 it does change the value. */
2845 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2846 warning_at (loc, OPT_Wconversion,
2847 "conversion to %qT from boolean expression", type);
2848 return;
2850 case REAL_CST:
2851 case INTEGER_CST:
2852 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2853 if (conversion_kind == UNSAFE_REAL)
2854 warning_at (loc, OPT_Wfloat_conversion,
2855 "conversion to %qT alters %qT constant value",
2856 type, expr_type);
2857 else if (conversion_kind)
2858 warning_at (loc, OPT_Wconversion,
2859 "conversion to %qT alters %qT constant value",
2860 type, expr_type);
2861 return;
2863 case COND_EXPR:
2865 /* In case of COND_EXPR, we do not care about the type of
2866 COND_EXPR, only about the conversion of each operand. */
2867 tree op1 = TREE_OPERAND (expr, 1);
2868 tree op2 = TREE_OPERAND (expr, 2);
2870 conversion_warning (loc, type, op1);
2871 conversion_warning (loc, type, op2);
2872 return;
2875 default: /* 'expr' is not a constant. */
2876 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2877 if (conversion_kind == UNSAFE_REAL)
2878 warning_at (loc, OPT_Wfloat_conversion,
2879 "conversion to %qT from %qT may alter its value",
2880 type, expr_type);
2881 else if (conversion_kind)
2882 warning_at (loc, OPT_Wconversion,
2883 "conversion to %qT from %qT may alter its value",
2884 type, expr_type);
2888 /* Produce warnings after a conversion. RESULT is the result of
2889 converting EXPR to TYPE. This is a helper function for
2890 convert_and_check and cp_convert_and_check. */
2892 void
2893 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2894 tree result)
2896 loc = expansion_point_location_if_in_system_header (loc);
2898 if (TREE_CODE (expr) == INTEGER_CST
2899 && (TREE_CODE (type) == INTEGER_TYPE
2900 || TREE_CODE (type) == ENUMERAL_TYPE)
2901 && !int_fits_type_p (expr, type))
2903 /* Do not diagnose overflow in a constant expression merely
2904 because a conversion overflowed. */
2905 if (TREE_OVERFLOW (result))
2906 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2908 if (TYPE_UNSIGNED (type))
2910 /* This detects cases like converting -129 or 256 to
2911 unsigned char. */
2912 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2913 warning_at (loc, OPT_Woverflow,
2914 "large integer implicitly truncated to unsigned type");
2915 else
2916 conversion_warning (loc, type, expr);
2918 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2919 warning_at (loc, OPT_Woverflow,
2920 "overflow in implicit constant conversion");
2921 /* No warning for converting 0x80000000 to int. */
2922 else if (pedantic
2923 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2924 || TYPE_PRECISION (TREE_TYPE (expr))
2925 != TYPE_PRECISION (type)))
2926 warning_at (loc, OPT_Woverflow,
2927 "overflow in implicit constant conversion");
2929 else
2930 conversion_warning (loc, type, expr);
2932 else if ((TREE_CODE (result) == INTEGER_CST
2933 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2934 warning_at (loc, OPT_Woverflow,
2935 "overflow in implicit constant conversion");
2936 else
2937 conversion_warning (loc, type, expr);
2941 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2942 Invoke this function on every expression that is converted implicitly,
2943 i.e. because of language rules and not because of an explicit cast. */
2945 tree
2946 convert_and_check (location_t loc, tree type, tree expr)
2948 tree result;
2949 tree expr_for_warning;
2951 /* Convert from a value with possible excess precision rather than
2952 via the semantic type, but do not warn about values not fitting
2953 exactly in the semantic type. */
2954 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2956 tree orig_type = TREE_TYPE (expr);
2957 expr = TREE_OPERAND (expr, 0);
2958 expr_for_warning = convert (orig_type, expr);
2959 if (orig_type == type)
2960 return expr_for_warning;
2962 else
2963 expr_for_warning = expr;
2965 if (TREE_TYPE (expr) == type)
2966 return expr;
2968 result = convert (type, expr);
2970 if (c_inhibit_evaluation_warnings == 0
2971 && !TREE_OVERFLOW_P (expr)
2972 && result != error_mark_node)
2973 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2975 return result;
2978 /* A node in a list that describes references to variables (EXPR), which are
2979 either read accesses if WRITER is zero, or write accesses, in which case
2980 WRITER is the parent of EXPR. */
2981 struct tlist
2983 struct tlist *next;
2984 tree expr, writer;
2987 /* Used to implement a cache the results of a call to verify_tree. We only
2988 use this for SAVE_EXPRs. */
2989 struct tlist_cache
2991 struct tlist_cache *next;
2992 struct tlist *cache_before_sp;
2993 struct tlist *cache_after_sp;
2994 tree expr;
2997 /* Obstack to use when allocating tlist structures, and corresponding
2998 firstobj. */
2999 static struct obstack tlist_obstack;
3000 static char *tlist_firstobj = 0;
3002 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3003 warnings. */
3004 static struct tlist *warned_ids;
3005 /* SAVE_EXPRs need special treatment. We process them only once and then
3006 cache the results. */
3007 static struct tlist_cache *save_expr_cache;
3009 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3010 static void merge_tlist (struct tlist **, struct tlist *, int);
3011 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3012 static int warning_candidate_p (tree);
3013 static bool candidate_equal_p (const_tree, const_tree);
3014 static void warn_for_collisions (struct tlist *);
3015 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3016 static struct tlist *new_tlist (struct tlist *, tree, tree);
3018 /* Create a new struct tlist and fill in its fields. */
3019 static struct tlist *
3020 new_tlist (struct tlist *next, tree t, tree writer)
3022 struct tlist *l;
3023 l = XOBNEW (&tlist_obstack, struct tlist);
3024 l->next = next;
3025 l->expr = t;
3026 l->writer = writer;
3027 return l;
3030 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3031 is nonnull, we ignore any node we find which has a writer equal to it. */
3033 static void
3034 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3036 while (add)
3038 struct tlist *next = add->next;
3039 if (!copy)
3040 add->next = *to;
3041 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3042 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3043 add = next;
3047 /* Merge the nodes of ADD into TO. This merging process is done so that for
3048 each variable that already exists in TO, no new node is added; however if
3049 there is a write access recorded in ADD, and an occurrence on TO is only
3050 a read access, then the occurrence in TO will be modified to record the
3051 write. */
3053 static void
3054 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3056 struct tlist **end = to;
3058 while (*end)
3059 end = &(*end)->next;
3061 while (add)
3063 int found = 0;
3064 struct tlist *tmp2;
3065 struct tlist *next = add->next;
3067 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3068 if (candidate_equal_p (tmp2->expr, add->expr))
3070 found = 1;
3071 if (!tmp2->writer)
3072 tmp2->writer = add->writer;
3074 if (!found)
3076 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3077 end = &(*end)->next;
3078 *end = 0;
3080 add = next;
3084 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3085 references in list LIST conflict with it, excluding reads if ONLY writers
3086 is nonzero. */
3088 static void
3089 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3090 int only_writes)
3092 struct tlist *tmp;
3094 /* Avoid duplicate warnings. */
3095 for (tmp = warned_ids; tmp; tmp = tmp->next)
3096 if (candidate_equal_p (tmp->expr, written))
3097 return;
3099 while (list)
3101 if (candidate_equal_p (list->expr, written)
3102 && !candidate_equal_p (list->writer, writer)
3103 && (!only_writes || list->writer))
3105 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3106 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3107 OPT_Wsequence_point, "operation on %qE may be undefined",
3108 list->expr);
3110 list = list->next;
3114 /* Given a list LIST of references to variables, find whether any of these
3115 can cause conflicts due to missing sequence points. */
3117 static void
3118 warn_for_collisions (struct tlist *list)
3120 struct tlist *tmp;
3122 for (tmp = list; tmp; tmp = tmp->next)
3124 if (tmp->writer)
3125 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3129 /* Return nonzero if X is a tree that can be verified by the sequence point
3130 warnings. */
3131 static int
3132 warning_candidate_p (tree x)
3134 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3135 return 0;
3137 if (TREE_CODE (x) == BLOCK)
3138 return 0;
3140 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3141 (lvalue_p) crash on TRY/CATCH. */
3142 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3143 return 0;
3145 if (!lvalue_p (x))
3146 return 0;
3148 /* No point to track non-const calls, they will never satisfy
3149 operand_equal_p. */
3150 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3151 return 0;
3153 if (TREE_CODE (x) == STRING_CST)
3154 return 0;
3156 return 1;
3159 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3160 static bool
3161 candidate_equal_p (const_tree x, const_tree y)
3163 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3166 /* Walk the tree X, and record accesses to variables. If X is written by the
3167 parent tree, WRITER is the parent.
3168 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3169 expression or its only operand forces a sequence point, then everything up
3170 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3171 in PNO_SP.
3172 Once we return, we will have emitted warnings if any subexpression before
3173 such a sequence point could be undefined. On a higher level, however, the
3174 sequence point may not be relevant, and we'll merge the two lists.
3176 Example: (b++, a) + b;
3177 The call that processes the COMPOUND_EXPR will store the increment of B
3178 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3179 processes the PLUS_EXPR will need to merge the two lists so that
3180 eventually, all accesses end up on the same list (and we'll warn about the
3181 unordered subexpressions b++ and b.
3183 A note on merging. If we modify the former example so that our expression
3184 becomes
3185 (b++, b) + a
3186 care must be taken not simply to add all three expressions into the final
3187 PNO_SP list. The function merge_tlist takes care of that by merging the
3188 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3189 way, so that no more than one access to B is recorded. */
3191 static void
3192 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3193 tree writer)
3195 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3196 enum tree_code code;
3197 enum tree_code_class cl;
3199 /* X may be NULL if it is the operand of an empty statement expression
3200 ({ }). */
3201 if (x == NULL)
3202 return;
3204 restart:
3205 code = TREE_CODE (x);
3206 cl = TREE_CODE_CLASS (code);
3208 if (warning_candidate_p (x))
3209 *pno_sp = new_tlist (*pno_sp, x, writer);
3211 switch (code)
3213 case CONSTRUCTOR:
3214 case SIZEOF_EXPR:
3215 return;
3217 case COMPOUND_EXPR:
3218 case TRUTH_ANDIF_EXPR:
3219 case TRUTH_ORIF_EXPR:
3220 tmp_before = tmp_nosp = tmp_list3 = 0;
3221 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3222 warn_for_collisions (tmp_nosp);
3223 merge_tlist (pbefore_sp, tmp_before, 0);
3224 merge_tlist (pbefore_sp, tmp_nosp, 0);
3225 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3226 merge_tlist (pbefore_sp, tmp_list3, 0);
3227 return;
3229 case COND_EXPR:
3230 tmp_before = tmp_list2 = 0;
3231 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3232 warn_for_collisions (tmp_list2);
3233 merge_tlist (pbefore_sp, tmp_before, 0);
3234 merge_tlist (pbefore_sp, tmp_list2, 0);
3236 tmp_list3 = tmp_nosp = 0;
3237 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3238 warn_for_collisions (tmp_nosp);
3239 merge_tlist (pbefore_sp, tmp_list3, 0);
3241 tmp_list3 = tmp_list2 = 0;
3242 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3243 warn_for_collisions (tmp_list2);
3244 merge_tlist (pbefore_sp, tmp_list3, 0);
3245 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3246 two first, to avoid warning for (a ? b++ : b++). */
3247 merge_tlist (&tmp_nosp, tmp_list2, 0);
3248 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3249 return;
3251 case PREDECREMENT_EXPR:
3252 case PREINCREMENT_EXPR:
3253 case POSTDECREMENT_EXPR:
3254 case POSTINCREMENT_EXPR:
3255 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3256 return;
3258 case MODIFY_EXPR:
3259 tmp_before = tmp_nosp = tmp_list3 = 0;
3260 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3261 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3262 /* Expressions inside the LHS are not ordered wrt. the sequence points
3263 in the RHS. Example:
3264 *a = (a++, 2)
3265 Despite the fact that the modification of "a" is in the before_sp
3266 list (tmp_before), it conflicts with the use of "a" in the LHS.
3267 We can handle this by adding the contents of tmp_list3
3268 to those of tmp_before, and redoing the collision warnings for that
3269 list. */
3270 add_tlist (&tmp_before, tmp_list3, x, 1);
3271 warn_for_collisions (tmp_before);
3272 /* Exclude the LHS itself here; we first have to merge it into the
3273 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3274 didn't exclude the LHS, we'd get it twice, once as a read and once
3275 as a write. */
3276 add_tlist (pno_sp, tmp_list3, x, 0);
3277 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3279 merge_tlist (pbefore_sp, tmp_before, 0);
3280 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3281 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3282 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3283 return;
3285 case CALL_EXPR:
3286 /* We need to warn about conflicts among arguments and conflicts between
3287 args and the function address. Side effects of the function address,
3288 however, are not ordered by the sequence point of the call. */
3290 call_expr_arg_iterator iter;
3291 tree arg;
3292 tmp_before = tmp_nosp = 0;
3293 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3294 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3296 tmp_list2 = tmp_list3 = 0;
3297 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3298 merge_tlist (&tmp_list3, tmp_list2, 0);
3299 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3301 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3302 warn_for_collisions (tmp_before);
3303 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3304 return;
3307 case TREE_LIST:
3308 /* Scan all the list, e.g. indices of multi dimensional array. */
3309 while (x)
3311 tmp_before = tmp_nosp = 0;
3312 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3313 merge_tlist (&tmp_nosp, tmp_before, 0);
3314 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3315 x = TREE_CHAIN (x);
3317 return;
3319 case SAVE_EXPR:
3321 struct tlist_cache *t;
3322 for (t = save_expr_cache; t; t = t->next)
3323 if (candidate_equal_p (t->expr, x))
3324 break;
3326 if (!t)
3328 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3329 t->next = save_expr_cache;
3330 t->expr = x;
3331 save_expr_cache = t;
3333 tmp_before = tmp_nosp = 0;
3334 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3335 warn_for_collisions (tmp_nosp);
3337 tmp_list3 = 0;
3338 merge_tlist (&tmp_list3, tmp_nosp, 0);
3339 t->cache_before_sp = tmp_before;
3340 t->cache_after_sp = tmp_list3;
3342 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3343 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3344 return;
3347 case ADDR_EXPR:
3348 x = TREE_OPERAND (x, 0);
3349 if (DECL_P (x))
3350 return;
3351 writer = 0;
3352 goto restart;
3354 default:
3355 /* For other expressions, simply recurse on their operands.
3356 Manual tail recursion for unary expressions.
3357 Other non-expressions need not be processed. */
3358 if (cl == tcc_unary)
3360 x = TREE_OPERAND (x, 0);
3361 writer = 0;
3362 goto restart;
3364 else if (IS_EXPR_CODE_CLASS (cl))
3366 int lp;
3367 int max = TREE_OPERAND_LENGTH (x);
3368 for (lp = 0; lp < max; lp++)
3370 tmp_before = tmp_nosp = 0;
3371 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3372 merge_tlist (&tmp_nosp, tmp_before, 0);
3373 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3376 return;
3380 /* Try to warn for undefined behavior in EXPR due to missing sequence
3381 points. */
3383 DEBUG_FUNCTION void
3384 verify_sequence_points (tree expr)
3386 struct tlist *before_sp = 0, *after_sp = 0;
3388 warned_ids = 0;
3389 save_expr_cache = 0;
3390 if (tlist_firstobj == 0)
3392 gcc_obstack_init (&tlist_obstack);
3393 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3396 verify_tree (expr, &before_sp, &after_sp, 0);
3397 warn_for_collisions (after_sp);
3398 obstack_free (&tlist_obstack, tlist_firstobj);
3401 /* Validate the expression after `case' and apply default promotions. */
3403 static tree
3404 check_case_value (location_t loc, tree value)
3406 if (value == NULL_TREE)
3407 return value;
3409 if (TREE_CODE (value) == INTEGER_CST)
3410 /* Promote char or short to int. */
3411 value = perform_integral_promotions (value);
3412 else if (value != error_mark_node)
3414 error_at (loc, "case label does not reduce to an integer constant");
3415 value = error_mark_node;
3418 constant_expression_warning (value);
3420 return value;
3423 /* See if the case values LOW and HIGH are in the range of the original
3424 type (i.e. before the default conversion to int) of the switch testing
3425 expression.
3426 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3427 the type before promoting it. CASE_LOW_P is a pointer to the lower
3428 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3429 if the case is not a case range.
3430 The caller has to make sure that we are not called with NULL for
3431 CASE_LOW_P (i.e. the default case).
3432 Returns true if the case label is in range of ORIG_TYPE (saturated or
3433 untouched) or false if the label is out of range. */
3435 static bool
3436 check_case_bounds (location_t loc, tree type, tree orig_type,
3437 tree *case_low_p, tree *case_high_p)
3439 tree min_value, max_value;
3440 tree case_low = *case_low_p;
3441 tree case_high = case_high_p ? *case_high_p : case_low;
3443 /* If there was a problem with the original type, do nothing. */
3444 if (orig_type == error_mark_node)
3445 return true;
3447 min_value = TYPE_MIN_VALUE (orig_type);
3448 max_value = TYPE_MAX_VALUE (orig_type);
3450 /* Case label is less than minimum for type. */
3451 if (tree_int_cst_compare (case_low, min_value) < 0
3452 && tree_int_cst_compare (case_high, min_value) < 0)
3454 warning_at (loc, 0, "case label value is less than minimum value "
3455 "for type");
3456 return false;
3459 /* Case value is greater than maximum for type. */
3460 if (tree_int_cst_compare (case_low, max_value) > 0
3461 && tree_int_cst_compare (case_high, max_value) > 0)
3463 warning_at (loc, 0, "case label value exceeds maximum value for type");
3464 return false;
3467 /* Saturate lower case label value to minimum. */
3468 if (tree_int_cst_compare (case_high, min_value) >= 0
3469 && tree_int_cst_compare (case_low, min_value) < 0)
3471 warning_at (loc, 0, "lower value in case label range"
3472 " less than minimum value for type");
3473 case_low = min_value;
3476 /* Saturate upper case label value to maximum. */
3477 if (tree_int_cst_compare (case_low, max_value) <= 0
3478 && tree_int_cst_compare (case_high, max_value) > 0)
3480 warning_at (loc, 0, "upper value in case label range"
3481 " exceeds maximum value for type");
3482 case_high = max_value;
3485 if (*case_low_p != case_low)
3486 *case_low_p = convert (type, case_low);
3487 if (case_high_p && *case_high_p != case_high)
3488 *case_high_p = convert (type, case_high);
3490 return true;
3493 /* Return an integer type with BITS bits of precision,
3494 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3496 tree
3497 c_common_type_for_size (unsigned int bits, int unsignedp)
3499 int i;
3501 if (bits == TYPE_PRECISION (integer_type_node))
3502 return unsignedp ? unsigned_type_node : integer_type_node;
3504 if (bits == TYPE_PRECISION (signed_char_type_node))
3505 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3507 if (bits == TYPE_PRECISION (short_integer_type_node))
3508 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3510 if (bits == TYPE_PRECISION (long_integer_type_node))
3511 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3513 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3514 return (unsignedp ? long_long_unsigned_type_node
3515 : long_long_integer_type_node);
3517 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3518 if (int_n_enabled_p[i]
3519 && bits == int_n_data[i].bitsize)
3520 return (unsignedp ? int_n_trees[i].unsigned_type
3521 : int_n_trees[i].signed_type);
3523 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3524 return (unsignedp ? widest_unsigned_literal_type_node
3525 : widest_integer_literal_type_node);
3527 if (bits <= TYPE_PRECISION (intQI_type_node))
3528 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3530 if (bits <= TYPE_PRECISION (intHI_type_node))
3531 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3533 if (bits <= TYPE_PRECISION (intSI_type_node))
3534 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3536 if (bits <= TYPE_PRECISION (intDI_type_node))
3537 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3539 return 0;
3542 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3543 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3544 and saturating if SATP is nonzero, otherwise not saturating. */
3546 tree
3547 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3548 int unsignedp, int satp)
3550 machine_mode mode;
3551 if (ibit == 0)
3552 mode = unsignedp ? UQQmode : QQmode;
3553 else
3554 mode = unsignedp ? UHAmode : HAmode;
3556 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3557 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3558 break;
3560 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3562 sorry ("GCC cannot support operators with integer types and "
3563 "fixed-point types that have too many integral and "
3564 "fractional bits together");
3565 return 0;
3568 return c_common_type_for_mode (mode, satp);
3571 /* Used for communication between c_common_type_for_mode and
3572 c_register_builtin_type. */
3573 tree registered_builtin_types;
3575 /* Return a data type that has machine mode MODE.
3576 If the mode is an integer,
3577 then UNSIGNEDP selects between signed and unsigned types.
3578 If the mode is a fixed-point mode,
3579 then UNSIGNEDP selects between saturating and nonsaturating types. */
3581 tree
3582 c_common_type_for_mode (machine_mode mode, int unsignedp)
3584 tree t;
3585 int i;
3587 if (mode == TYPE_MODE (integer_type_node))
3588 return unsignedp ? unsigned_type_node : integer_type_node;
3590 if (mode == TYPE_MODE (signed_char_type_node))
3591 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3593 if (mode == TYPE_MODE (short_integer_type_node))
3594 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3596 if (mode == TYPE_MODE (long_integer_type_node))
3597 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3599 if (mode == TYPE_MODE (long_long_integer_type_node))
3600 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3602 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3603 if (int_n_enabled_p[i]
3604 && mode == int_n_data[i].m)
3605 return (unsignedp ? int_n_trees[i].unsigned_type
3606 : int_n_trees[i].signed_type);
3608 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3609 return unsignedp ? widest_unsigned_literal_type_node
3610 : widest_integer_literal_type_node;
3612 if (mode == QImode)
3613 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3615 if (mode == HImode)
3616 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3618 if (mode == SImode)
3619 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3621 if (mode == DImode)
3622 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3624 #if HOST_BITS_PER_WIDE_INT >= 64
3625 if (mode == TYPE_MODE (intTI_type_node))
3626 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3627 #endif
3629 if (mode == TYPE_MODE (float_type_node))
3630 return float_type_node;
3632 if (mode == TYPE_MODE (double_type_node))
3633 return double_type_node;
3635 if (mode == TYPE_MODE (long_double_type_node))
3636 return long_double_type_node;
3638 if (mode == TYPE_MODE (void_type_node))
3639 return void_type_node;
3641 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3642 return (unsignedp
3643 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3644 : make_signed_type (GET_MODE_PRECISION (mode)));
3646 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3647 return (unsignedp
3648 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3649 : make_signed_type (GET_MODE_PRECISION (mode)));
3651 if (COMPLEX_MODE_P (mode))
3653 machine_mode inner_mode;
3654 tree inner_type;
3656 if (mode == TYPE_MODE (complex_float_type_node))
3657 return complex_float_type_node;
3658 if (mode == TYPE_MODE (complex_double_type_node))
3659 return complex_double_type_node;
3660 if (mode == TYPE_MODE (complex_long_double_type_node))
3661 return complex_long_double_type_node;
3663 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3664 return complex_integer_type_node;
3666 inner_mode = GET_MODE_INNER (mode);
3667 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3668 if (inner_type != NULL_TREE)
3669 return build_complex_type (inner_type);
3671 else if (VECTOR_MODE_P (mode))
3673 machine_mode inner_mode = GET_MODE_INNER (mode);
3674 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3675 if (inner_type != NULL_TREE)
3676 return build_vector_type_for_mode (inner_type, mode);
3679 if (mode == TYPE_MODE (dfloat32_type_node))
3680 return dfloat32_type_node;
3681 if (mode == TYPE_MODE (dfloat64_type_node))
3682 return dfloat64_type_node;
3683 if (mode == TYPE_MODE (dfloat128_type_node))
3684 return dfloat128_type_node;
3686 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3688 if (mode == TYPE_MODE (short_fract_type_node))
3689 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3690 if (mode == TYPE_MODE (fract_type_node))
3691 return unsignedp ? sat_fract_type_node : fract_type_node;
3692 if (mode == TYPE_MODE (long_fract_type_node))
3693 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3694 if (mode == TYPE_MODE (long_long_fract_type_node))
3695 return unsignedp ? sat_long_long_fract_type_node
3696 : long_long_fract_type_node;
3698 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3699 return unsignedp ? sat_unsigned_short_fract_type_node
3700 : unsigned_short_fract_type_node;
3701 if (mode == TYPE_MODE (unsigned_fract_type_node))
3702 return unsignedp ? sat_unsigned_fract_type_node
3703 : unsigned_fract_type_node;
3704 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3705 return unsignedp ? sat_unsigned_long_fract_type_node
3706 : unsigned_long_fract_type_node;
3707 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3708 return unsignedp ? sat_unsigned_long_long_fract_type_node
3709 : unsigned_long_long_fract_type_node;
3711 if (mode == TYPE_MODE (short_accum_type_node))
3712 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3713 if (mode == TYPE_MODE (accum_type_node))
3714 return unsignedp ? sat_accum_type_node : accum_type_node;
3715 if (mode == TYPE_MODE (long_accum_type_node))
3716 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3717 if (mode == TYPE_MODE (long_long_accum_type_node))
3718 return unsignedp ? sat_long_long_accum_type_node
3719 : long_long_accum_type_node;
3721 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3722 return unsignedp ? sat_unsigned_short_accum_type_node
3723 : unsigned_short_accum_type_node;
3724 if (mode == TYPE_MODE (unsigned_accum_type_node))
3725 return unsignedp ? sat_unsigned_accum_type_node
3726 : unsigned_accum_type_node;
3727 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3728 return unsignedp ? sat_unsigned_long_accum_type_node
3729 : unsigned_long_accum_type_node;
3730 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3731 return unsignedp ? sat_unsigned_long_long_accum_type_node
3732 : unsigned_long_long_accum_type_node;
3734 if (mode == QQmode)
3735 return unsignedp ? sat_qq_type_node : qq_type_node;
3736 if (mode == HQmode)
3737 return unsignedp ? sat_hq_type_node : hq_type_node;
3738 if (mode == SQmode)
3739 return unsignedp ? sat_sq_type_node : sq_type_node;
3740 if (mode == DQmode)
3741 return unsignedp ? sat_dq_type_node : dq_type_node;
3742 if (mode == TQmode)
3743 return unsignedp ? sat_tq_type_node : tq_type_node;
3745 if (mode == UQQmode)
3746 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3747 if (mode == UHQmode)
3748 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3749 if (mode == USQmode)
3750 return unsignedp ? sat_usq_type_node : usq_type_node;
3751 if (mode == UDQmode)
3752 return unsignedp ? sat_udq_type_node : udq_type_node;
3753 if (mode == UTQmode)
3754 return unsignedp ? sat_utq_type_node : utq_type_node;
3756 if (mode == HAmode)
3757 return unsignedp ? sat_ha_type_node : ha_type_node;
3758 if (mode == SAmode)
3759 return unsignedp ? sat_sa_type_node : sa_type_node;
3760 if (mode == DAmode)
3761 return unsignedp ? sat_da_type_node : da_type_node;
3762 if (mode == TAmode)
3763 return unsignedp ? sat_ta_type_node : ta_type_node;
3765 if (mode == UHAmode)
3766 return unsignedp ? sat_uha_type_node : uha_type_node;
3767 if (mode == USAmode)
3768 return unsignedp ? sat_usa_type_node : usa_type_node;
3769 if (mode == UDAmode)
3770 return unsignedp ? sat_uda_type_node : uda_type_node;
3771 if (mode == UTAmode)
3772 return unsignedp ? sat_uta_type_node : uta_type_node;
3775 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3776 if (TYPE_MODE (TREE_VALUE (t)) == mode
3777 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3778 return TREE_VALUE (t);
3780 return 0;
3783 tree
3784 c_common_unsigned_type (tree type)
3786 return c_common_signed_or_unsigned_type (1, type);
3789 /* Return a signed type the same as TYPE in other respects. */
3791 tree
3792 c_common_signed_type (tree type)
3794 return c_common_signed_or_unsigned_type (0, type);
3797 /* Return a type the same as TYPE except unsigned or
3798 signed according to UNSIGNEDP. */
3800 tree
3801 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3803 tree type1;
3804 int i;
3806 /* This block of code emulates the behavior of the old
3807 c_common_unsigned_type. In particular, it returns
3808 long_unsigned_type_node if passed a long, even when a int would
3809 have the same size. This is necessary for warnings to work
3810 correctly in archs where sizeof(int) == sizeof(long) */
3812 type1 = TYPE_MAIN_VARIANT (type);
3813 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3814 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3815 if (type1 == integer_type_node || type1 == unsigned_type_node)
3816 return unsignedp ? unsigned_type_node : integer_type_node;
3817 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3818 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3819 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3820 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3821 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3822 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3824 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3825 if (int_n_enabled_p[i]
3826 && (type1 == int_n_trees[i].unsigned_type
3827 || type1 == int_n_trees[i].signed_type))
3828 return (unsignedp ? int_n_trees[i].unsigned_type
3829 : int_n_trees[i].signed_type);
3831 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3832 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3833 #if HOST_BITS_PER_WIDE_INT >= 64
3834 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3835 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3836 #endif
3837 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3838 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3839 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3840 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3841 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3842 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3843 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3844 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3846 #define C_COMMON_FIXED_TYPES(NAME) \
3847 if (type1 == short_ ## NAME ## _type_node \
3848 || type1 == unsigned_short_ ## NAME ## _type_node) \
3849 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3850 : short_ ## NAME ## _type_node; \
3851 if (type1 == NAME ## _type_node \
3852 || type1 == unsigned_ ## NAME ## _type_node) \
3853 return unsignedp ? unsigned_ ## NAME ## _type_node \
3854 : NAME ## _type_node; \
3855 if (type1 == long_ ## NAME ## _type_node \
3856 || type1 == unsigned_long_ ## NAME ## _type_node) \
3857 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3858 : long_ ## NAME ## _type_node; \
3859 if (type1 == long_long_ ## NAME ## _type_node \
3860 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3861 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3862 : long_long_ ## NAME ## _type_node;
3864 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3865 if (type1 == NAME ## _type_node \
3866 || type1 == u ## NAME ## _type_node) \
3867 return unsignedp ? u ## NAME ## _type_node \
3868 : NAME ## _type_node;
3870 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3871 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3872 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3873 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3874 : sat_ ## short_ ## NAME ## _type_node; \
3875 if (type1 == sat_ ## NAME ## _type_node \
3876 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3877 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3878 : sat_ ## NAME ## _type_node; \
3879 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3880 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3881 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3882 : sat_ ## long_ ## NAME ## _type_node; \
3883 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3884 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3885 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3886 : sat_ ## long_long_ ## NAME ## _type_node;
3888 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3889 if (type1 == sat_ ## NAME ## _type_node \
3890 || type1 == sat_ ## u ## NAME ## _type_node) \
3891 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3892 : sat_ ## NAME ## _type_node;
3894 C_COMMON_FIXED_TYPES (fract);
3895 C_COMMON_FIXED_TYPES_SAT (fract);
3896 C_COMMON_FIXED_TYPES (accum);
3897 C_COMMON_FIXED_TYPES_SAT (accum);
3899 C_COMMON_FIXED_MODE_TYPES (qq);
3900 C_COMMON_FIXED_MODE_TYPES (hq);
3901 C_COMMON_FIXED_MODE_TYPES (sq);
3902 C_COMMON_FIXED_MODE_TYPES (dq);
3903 C_COMMON_FIXED_MODE_TYPES (tq);
3904 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3905 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3906 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3907 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3908 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3909 C_COMMON_FIXED_MODE_TYPES (ha);
3910 C_COMMON_FIXED_MODE_TYPES (sa);
3911 C_COMMON_FIXED_MODE_TYPES (da);
3912 C_COMMON_FIXED_MODE_TYPES (ta);
3913 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3914 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3915 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3916 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3918 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3919 the precision; they have precision set to match their range, but
3920 may use a wider mode to match an ABI. If we change modes, we may
3921 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3922 the precision as well, so as to yield correct results for
3923 bit-field types. C++ does not have these separate bit-field
3924 types, and producing a signed or unsigned variant of an
3925 ENUMERAL_TYPE may cause other problems as well. */
3927 if (!INTEGRAL_TYPE_P (type)
3928 || TYPE_UNSIGNED (type) == unsignedp)
3929 return type;
3931 #define TYPE_OK(node) \
3932 (TYPE_MODE (type) == TYPE_MODE (node) \
3933 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3934 if (TYPE_OK (signed_char_type_node))
3935 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3936 if (TYPE_OK (integer_type_node))
3937 return unsignedp ? unsigned_type_node : integer_type_node;
3938 if (TYPE_OK (short_integer_type_node))
3939 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3940 if (TYPE_OK (long_integer_type_node))
3941 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3942 if (TYPE_OK (long_long_integer_type_node))
3943 return (unsignedp ? long_long_unsigned_type_node
3944 : long_long_integer_type_node);
3946 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3947 if (int_n_enabled_p[i]
3948 && TYPE_MODE (type) == int_n_data[i].m
3949 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3950 return (unsignedp ? int_n_trees[i].unsigned_type
3951 : int_n_trees[i].signed_type);
3953 if (TYPE_OK (widest_integer_literal_type_node))
3954 return (unsignedp ? widest_unsigned_literal_type_node
3955 : widest_integer_literal_type_node);
3957 #if HOST_BITS_PER_WIDE_INT >= 64
3958 if (TYPE_OK (intTI_type_node))
3959 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3960 #endif
3961 if (TYPE_OK (intDI_type_node))
3962 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3963 if (TYPE_OK (intSI_type_node))
3964 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3965 if (TYPE_OK (intHI_type_node))
3966 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3967 if (TYPE_OK (intQI_type_node))
3968 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3969 #undef TYPE_OK
3971 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3974 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3976 tree
3977 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3979 int i;
3981 /* Extended integer types of the same width as a standard type have
3982 lesser rank, so those of the same width as int promote to int or
3983 unsigned int and are valid for printf formats expecting int or
3984 unsigned int. To avoid such special cases, avoid creating
3985 extended integer types for bit-fields if a standard integer type
3986 is available. */
3987 if (width == TYPE_PRECISION (integer_type_node))
3988 return unsignedp ? unsigned_type_node : integer_type_node;
3989 if (width == TYPE_PRECISION (signed_char_type_node))
3990 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3991 if (width == TYPE_PRECISION (short_integer_type_node))
3992 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3993 if (width == TYPE_PRECISION (long_integer_type_node))
3994 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3995 if (width == TYPE_PRECISION (long_long_integer_type_node))
3996 return (unsignedp ? long_long_unsigned_type_node
3997 : long_long_integer_type_node);
3998 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3999 if (int_n_enabled_p[i]
4000 && width == int_n_data[i].bitsize)
4001 return (unsignedp ? int_n_trees[i].unsigned_type
4002 : int_n_trees[i].signed_type);
4003 return build_nonstandard_integer_type (width, unsignedp);
4006 /* The C version of the register_builtin_type langhook. */
4008 void
4009 c_register_builtin_type (tree type, const char* name)
4011 tree decl;
4013 decl = build_decl (UNKNOWN_LOCATION,
4014 TYPE_DECL, get_identifier (name), type);
4015 DECL_ARTIFICIAL (decl) = 1;
4016 if (!TYPE_NAME (type))
4017 TYPE_NAME (type) = decl;
4018 pushdecl (decl);
4020 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4023 /* Print an error message for invalid operands to arith operation
4024 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4025 LOCATION is the location of the message. */
4027 void
4028 binary_op_error (location_t location, enum tree_code code,
4029 tree type0, tree type1)
4031 const char *opname;
4033 switch (code)
4035 case PLUS_EXPR:
4036 opname = "+"; break;
4037 case MINUS_EXPR:
4038 opname = "-"; break;
4039 case MULT_EXPR:
4040 opname = "*"; break;
4041 case MAX_EXPR:
4042 opname = "max"; break;
4043 case MIN_EXPR:
4044 opname = "min"; break;
4045 case EQ_EXPR:
4046 opname = "=="; break;
4047 case NE_EXPR:
4048 opname = "!="; break;
4049 case LE_EXPR:
4050 opname = "<="; break;
4051 case GE_EXPR:
4052 opname = ">="; break;
4053 case LT_EXPR:
4054 opname = "<"; break;
4055 case GT_EXPR:
4056 opname = ">"; break;
4057 case LSHIFT_EXPR:
4058 opname = "<<"; break;
4059 case RSHIFT_EXPR:
4060 opname = ">>"; break;
4061 case TRUNC_MOD_EXPR:
4062 case FLOOR_MOD_EXPR:
4063 opname = "%"; break;
4064 case TRUNC_DIV_EXPR:
4065 case FLOOR_DIV_EXPR:
4066 opname = "/"; break;
4067 case BIT_AND_EXPR:
4068 opname = "&"; break;
4069 case BIT_IOR_EXPR:
4070 opname = "|"; break;
4071 case TRUTH_ANDIF_EXPR:
4072 opname = "&&"; break;
4073 case TRUTH_ORIF_EXPR:
4074 opname = "||"; break;
4075 case BIT_XOR_EXPR:
4076 opname = "^"; break;
4077 default:
4078 gcc_unreachable ();
4080 error_at (location,
4081 "invalid operands to binary %s (have %qT and %qT)", opname,
4082 type0, type1);
4085 /* Given an expression as a tree, return its original type. Do this
4086 by stripping any conversion that preserves the sign and precision. */
4087 static tree
4088 expr_original_type (tree expr)
4090 STRIP_SIGN_NOPS (expr);
4091 return TREE_TYPE (expr);
4094 /* Subroutine of build_binary_op, used for comparison operations.
4095 See if the operands have both been converted from subword integer types
4096 and, if so, perhaps change them both back to their original type.
4097 This function is also responsible for converting the two operands
4098 to the proper common type for comparison.
4100 The arguments of this function are all pointers to local variables
4101 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4102 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4104 LOC is the location of the comparison.
4106 If this function returns nonzero, it means that the comparison has
4107 a constant value. What this function returns is an expression for
4108 that value. */
4110 tree
4111 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4112 tree *restype_ptr, enum tree_code *rescode_ptr)
4114 tree type;
4115 tree op0 = *op0_ptr;
4116 tree op1 = *op1_ptr;
4117 int unsignedp0, unsignedp1;
4118 int real1, real2;
4119 tree primop0, primop1;
4120 enum tree_code code = *rescode_ptr;
4122 /* Throw away any conversions to wider types
4123 already present in the operands. */
4125 primop0 = c_common_get_narrower (op0, &unsignedp0);
4126 primop1 = c_common_get_narrower (op1, &unsignedp1);
4128 /* If primopN is first sign-extended from primopN's precision to opN's
4129 precision, then zero-extended from opN's precision to
4130 *restype_ptr precision, shortenings might be invalid. */
4131 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4132 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4133 && !unsignedp0
4134 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4135 primop0 = op0;
4136 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4137 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4138 && !unsignedp1
4139 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4140 primop1 = op1;
4142 /* Handle the case that OP0 does not *contain* a conversion
4143 but it *requires* conversion to FINAL_TYPE. */
4145 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4146 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4147 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4148 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4150 /* If one of the operands must be floated, we cannot optimize. */
4151 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4152 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4154 /* If first arg is constant, swap the args (changing operation
4155 so value is preserved), for canonicalization. Don't do this if
4156 the second arg is 0. */
4158 if (TREE_CONSTANT (primop0)
4159 && !integer_zerop (primop1) && !real_zerop (primop1)
4160 && !fixed_zerop (primop1))
4162 tree tem = primop0;
4163 int temi = unsignedp0;
4164 primop0 = primop1;
4165 primop1 = tem;
4166 tem = op0;
4167 op0 = op1;
4168 op1 = tem;
4169 *op0_ptr = op0;
4170 *op1_ptr = op1;
4171 unsignedp0 = unsignedp1;
4172 unsignedp1 = temi;
4173 temi = real1;
4174 real1 = real2;
4175 real2 = temi;
4177 switch (code)
4179 case LT_EXPR:
4180 code = GT_EXPR;
4181 break;
4182 case GT_EXPR:
4183 code = LT_EXPR;
4184 break;
4185 case LE_EXPR:
4186 code = GE_EXPR;
4187 break;
4188 case GE_EXPR:
4189 code = LE_EXPR;
4190 break;
4191 default:
4192 break;
4194 *rescode_ptr = code;
4197 /* If comparing an integer against a constant more bits wide,
4198 maybe we can deduce a value of 1 or 0 independent of the data.
4199 Or else truncate the constant now
4200 rather than extend the variable at run time.
4202 This is only interesting if the constant is the wider arg.
4203 Also, it is not safe if the constant is unsigned and the
4204 variable arg is signed, since in this case the variable
4205 would be sign-extended and then regarded as unsigned.
4206 Our technique fails in this case because the lowest/highest
4207 possible unsigned results don't follow naturally from the
4208 lowest/highest possible values of the variable operand.
4209 For just EQ_EXPR and NE_EXPR there is another technique that
4210 could be used: see if the constant can be faithfully represented
4211 in the other operand's type, by truncating it and reextending it
4212 and see if that preserves the constant's value. */
4214 if (!real1 && !real2
4215 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4216 && TREE_CODE (primop1) == INTEGER_CST
4217 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4219 int min_gt, max_gt, min_lt, max_lt;
4220 tree maxval, minval;
4221 /* 1 if comparison is nominally unsigned. */
4222 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4223 tree val;
4225 type = c_common_signed_or_unsigned_type (unsignedp0,
4226 TREE_TYPE (primop0));
4228 maxval = TYPE_MAX_VALUE (type);
4229 minval = TYPE_MIN_VALUE (type);
4231 if (unsignedp && !unsignedp0)
4232 *restype_ptr = c_common_signed_type (*restype_ptr);
4234 if (TREE_TYPE (primop1) != *restype_ptr)
4236 /* Convert primop1 to target type, but do not introduce
4237 additional overflow. We know primop1 is an int_cst. */
4238 primop1 = force_fit_type (*restype_ptr,
4239 wide_int::from
4240 (primop1,
4241 TYPE_PRECISION (*restype_ptr),
4242 TYPE_SIGN (TREE_TYPE (primop1))),
4243 0, TREE_OVERFLOW (primop1));
4245 if (type != *restype_ptr)
4247 minval = convert (*restype_ptr, minval);
4248 maxval = convert (*restype_ptr, maxval);
4251 min_gt = tree_int_cst_lt (primop1, minval);
4252 max_gt = tree_int_cst_lt (primop1, maxval);
4253 min_lt = tree_int_cst_lt (minval, primop1);
4254 max_lt = tree_int_cst_lt (maxval, primop1);
4256 val = 0;
4257 /* This used to be a switch, but Genix compiler can't handle that. */
4258 if (code == NE_EXPR)
4260 if (max_lt || min_gt)
4261 val = truthvalue_true_node;
4263 else if (code == EQ_EXPR)
4265 if (max_lt || min_gt)
4266 val = truthvalue_false_node;
4268 else if (code == LT_EXPR)
4270 if (max_lt)
4271 val = truthvalue_true_node;
4272 if (!min_lt)
4273 val = truthvalue_false_node;
4275 else if (code == GT_EXPR)
4277 if (min_gt)
4278 val = truthvalue_true_node;
4279 if (!max_gt)
4280 val = truthvalue_false_node;
4282 else if (code == LE_EXPR)
4284 if (!max_gt)
4285 val = truthvalue_true_node;
4286 if (min_gt)
4287 val = truthvalue_false_node;
4289 else if (code == GE_EXPR)
4291 if (!min_lt)
4292 val = truthvalue_true_node;
4293 if (max_lt)
4294 val = truthvalue_false_node;
4297 /* If primop0 was sign-extended and unsigned comparison specd,
4298 we did a signed comparison above using the signed type bounds.
4299 But the comparison we output must be unsigned.
4301 Also, for inequalities, VAL is no good; but if the signed
4302 comparison had *any* fixed result, it follows that the
4303 unsigned comparison just tests the sign in reverse
4304 (positive values are LE, negative ones GE).
4305 So we can generate an unsigned comparison
4306 against an extreme value of the signed type. */
4308 if (unsignedp && !unsignedp0)
4310 if (val != 0)
4311 switch (code)
4313 case LT_EXPR:
4314 case GE_EXPR:
4315 primop1 = TYPE_MIN_VALUE (type);
4316 val = 0;
4317 break;
4319 case LE_EXPR:
4320 case GT_EXPR:
4321 primop1 = TYPE_MAX_VALUE (type);
4322 val = 0;
4323 break;
4325 default:
4326 break;
4328 type = c_common_unsigned_type (type);
4331 if (TREE_CODE (primop0) != INTEGER_CST)
4333 if (val == truthvalue_false_node)
4334 warning_at (loc, OPT_Wtype_limits,
4335 "comparison is always false due to limited range of data type");
4336 if (val == truthvalue_true_node)
4337 warning_at (loc, OPT_Wtype_limits,
4338 "comparison is always true due to limited range of data type");
4341 if (val != 0)
4343 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4344 if (TREE_SIDE_EFFECTS (primop0))
4345 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4346 return val;
4349 /* Value is not predetermined, but do the comparison
4350 in the type of the operand that is not constant.
4351 TYPE is already properly set. */
4354 /* If either arg is decimal float and the other is float, find the
4355 proper common type to use for comparison. */
4356 else if (real1 && real2
4357 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4358 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4359 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4361 /* If either arg is decimal float and the other is float, fail. */
4362 else if (real1 && real2
4363 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4364 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4365 return 0;
4367 else if (real1 && real2
4368 && (TYPE_PRECISION (TREE_TYPE (primop0))
4369 == TYPE_PRECISION (TREE_TYPE (primop1))))
4370 type = TREE_TYPE (primop0);
4372 /* If args' natural types are both narrower than nominal type
4373 and both extend in the same manner, compare them
4374 in the type of the wider arg.
4375 Otherwise must actually extend both to the nominal
4376 common type lest different ways of extending
4377 alter the result.
4378 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4380 else if (unsignedp0 == unsignedp1 && real1 == real2
4381 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4382 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4384 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4385 type = c_common_signed_or_unsigned_type (unsignedp0
4386 || TYPE_UNSIGNED (*restype_ptr),
4387 type);
4388 /* Make sure shorter operand is extended the right way
4389 to match the longer operand. */
4390 primop0
4391 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4392 TREE_TYPE (primop0)),
4393 primop0);
4394 primop1
4395 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4396 TREE_TYPE (primop1)),
4397 primop1);
4399 else
4401 /* Here we must do the comparison on the nominal type
4402 using the args exactly as we received them. */
4403 type = *restype_ptr;
4404 primop0 = op0;
4405 primop1 = op1;
4407 if (!real1 && !real2 && integer_zerop (primop1)
4408 && TYPE_UNSIGNED (*restype_ptr))
4410 tree value = 0;
4411 /* All unsigned values are >= 0, so we warn. However,
4412 if OP0 is a constant that is >= 0, the signedness of
4413 the comparison isn't an issue, so suppress the
4414 warning. */
4415 bool warn =
4416 warn_type_limits && !in_system_header_at (loc)
4417 && !(TREE_CODE (primop0) == INTEGER_CST
4418 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4419 primop0)))
4420 /* Do not warn for enumeration types. */
4421 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4423 switch (code)
4425 case GE_EXPR:
4426 if (warn)
4427 warning_at (loc, OPT_Wtype_limits,
4428 "comparison of unsigned expression >= 0 is always true");
4429 value = truthvalue_true_node;
4430 break;
4432 case LT_EXPR:
4433 if (warn)
4434 warning_at (loc, OPT_Wtype_limits,
4435 "comparison of unsigned expression < 0 is always false");
4436 value = truthvalue_false_node;
4437 break;
4439 default:
4440 break;
4443 if (value != 0)
4445 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4446 if (TREE_SIDE_EFFECTS (primop0))
4447 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4448 primop0, value);
4449 return value;
4454 *op0_ptr = convert (type, primop0);
4455 *op1_ptr = convert (type, primop1);
4457 *restype_ptr = truthvalue_type_node;
4459 return 0;
4462 /* Return a tree for the sum or difference (RESULTCODE says which)
4463 of pointer PTROP and integer INTOP. */
4465 tree
4466 pointer_int_sum (location_t loc, enum tree_code resultcode,
4467 tree ptrop, tree intop, bool complain)
4469 tree size_exp, ret;
4471 /* The result is a pointer of the same type that is being added. */
4472 tree result_type = TREE_TYPE (ptrop);
4474 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4476 if (complain && warn_pointer_arith)
4477 pedwarn (loc, OPT_Wpointer_arith,
4478 "pointer of type %<void *%> used in arithmetic");
4479 else if (!complain)
4480 return error_mark_node;
4481 size_exp = integer_one_node;
4483 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4485 if (complain && warn_pointer_arith)
4486 pedwarn (loc, OPT_Wpointer_arith,
4487 "pointer to a function used in arithmetic");
4488 else if (!complain)
4489 return error_mark_node;
4490 size_exp = integer_one_node;
4492 else
4493 size_exp = size_in_bytes (TREE_TYPE (result_type));
4495 /* We are manipulating pointer values, so we don't need to warn
4496 about relying on undefined signed overflow. We disable the
4497 warning here because we use integer types so fold won't know that
4498 they are really pointers. */
4499 fold_defer_overflow_warnings ();
4501 /* If what we are about to multiply by the size of the elements
4502 contains a constant term, apply distributive law
4503 and multiply that constant term separately.
4504 This helps produce common subexpressions. */
4505 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4506 && !TREE_CONSTANT (intop)
4507 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4508 && TREE_CONSTANT (size_exp)
4509 /* If the constant comes from pointer subtraction,
4510 skip this optimization--it would cause an error. */
4511 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4512 /* If the constant is unsigned, and smaller than the pointer size,
4513 then we must skip this optimization. This is because it could cause
4514 an overflow error if the constant is negative but INTOP is not. */
4515 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4516 || (TYPE_PRECISION (TREE_TYPE (intop))
4517 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4519 enum tree_code subcode = resultcode;
4520 tree int_type = TREE_TYPE (intop);
4521 if (TREE_CODE (intop) == MINUS_EXPR)
4522 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4523 /* Convert both subexpression types to the type of intop,
4524 because weird cases involving pointer arithmetic
4525 can result in a sum or difference with different type args. */
4526 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4527 subcode, ptrop,
4528 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4529 intop = convert (int_type, TREE_OPERAND (intop, 0));
4532 /* Convert the integer argument to a type the same size as sizetype
4533 so the multiply won't overflow spuriously. */
4534 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4535 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4536 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4537 TYPE_UNSIGNED (sizetype)), intop);
4539 /* Replace the integer argument with a suitable product by the object size.
4540 Do this multiplication as signed, then convert to the appropriate type
4541 for the pointer operation and disregard an overflow that occurred only
4542 because of the sign-extension change in the latter conversion. */
4544 tree t = build_binary_op (loc,
4545 MULT_EXPR, intop,
4546 convert (TREE_TYPE (intop), size_exp), 1);
4547 intop = convert (sizetype, t);
4548 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4549 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4552 /* Create the sum or difference. */
4553 if (resultcode == MINUS_EXPR)
4554 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4556 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4558 fold_undefer_and_ignore_overflow_warnings ();
4560 return ret;
4563 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4564 and if NON_CONST is known not to be permitted in an evaluated part
4565 of a constant expression. */
4567 tree
4568 c_wrap_maybe_const (tree expr, bool non_const)
4570 bool nowarning = TREE_NO_WARNING (expr);
4571 location_t loc = EXPR_LOCATION (expr);
4573 /* This should never be called for C++. */
4574 if (c_dialect_cxx ())
4575 gcc_unreachable ();
4577 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4578 STRIP_TYPE_NOPS (expr);
4579 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4580 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4581 if (nowarning)
4582 TREE_NO_WARNING (expr) = 1;
4583 protected_set_expr_location (expr, loc);
4585 return expr;
4588 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4589 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4590 around the SAVE_EXPR if needed so that c_fully_fold does not need
4591 to look inside SAVE_EXPRs. */
4593 tree
4594 c_save_expr (tree expr)
4596 bool maybe_const = true;
4597 if (c_dialect_cxx ())
4598 return save_expr (expr);
4599 expr = c_fully_fold (expr, false, &maybe_const);
4600 expr = save_expr (expr);
4601 if (!maybe_const)
4602 expr = c_wrap_maybe_const (expr, true);
4603 return expr;
4606 /* Return whether EXPR is a declaration whose address can never be
4607 NULL. */
4609 bool
4610 decl_with_nonnull_addr_p (const_tree expr)
4612 return (DECL_P (expr)
4613 && (TREE_CODE (expr) == PARM_DECL
4614 || TREE_CODE (expr) == LABEL_DECL
4615 || !DECL_WEAK (expr)));
4618 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4619 or for an `if' or `while' statement or ?..: exp. It should already
4620 have been validated to be of suitable type; otherwise, a bad
4621 diagnostic may result.
4623 The EXPR is located at LOCATION.
4625 This preparation consists of taking the ordinary
4626 representation of an expression expr and producing a valid tree
4627 boolean expression describing whether expr is nonzero. We could
4628 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4629 but we optimize comparisons, &&, ||, and !.
4631 The resulting type should always be `truthvalue_type_node'. */
4633 tree
4634 c_common_truthvalue_conversion (location_t location, tree expr)
4636 switch (TREE_CODE (expr))
4638 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4639 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4640 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4641 case ORDERED_EXPR: case UNORDERED_EXPR:
4642 if (TREE_TYPE (expr) == truthvalue_type_node)
4643 return expr;
4644 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4645 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4646 goto ret;
4648 case TRUTH_ANDIF_EXPR:
4649 case TRUTH_ORIF_EXPR:
4650 case TRUTH_AND_EXPR:
4651 case TRUTH_OR_EXPR:
4652 case TRUTH_XOR_EXPR:
4653 if (TREE_TYPE (expr) == truthvalue_type_node)
4654 return expr;
4655 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4656 c_common_truthvalue_conversion (location,
4657 TREE_OPERAND (expr, 0)),
4658 c_common_truthvalue_conversion (location,
4659 TREE_OPERAND (expr, 1)));
4660 goto ret;
4662 case TRUTH_NOT_EXPR:
4663 if (TREE_TYPE (expr) == truthvalue_type_node)
4664 return expr;
4665 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4666 c_common_truthvalue_conversion (location,
4667 TREE_OPERAND (expr, 0)));
4668 goto ret;
4670 case ERROR_MARK:
4671 return expr;
4673 case INTEGER_CST:
4674 return integer_zerop (expr) ? truthvalue_false_node
4675 : truthvalue_true_node;
4677 case REAL_CST:
4678 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4679 ? truthvalue_true_node
4680 : truthvalue_false_node;
4682 case FIXED_CST:
4683 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4684 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4685 ? truthvalue_true_node
4686 : truthvalue_false_node;
4688 case FUNCTION_DECL:
4689 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4690 /* Fall through. */
4692 case ADDR_EXPR:
4694 tree inner = TREE_OPERAND (expr, 0);
4695 if (decl_with_nonnull_addr_p (inner))
4697 /* Common Ada/Pascal programmer's mistake. */
4698 warning_at (location,
4699 OPT_Waddress,
4700 "the address of %qD will always evaluate as %<true%>",
4701 inner);
4702 return truthvalue_true_node;
4704 break;
4707 case COMPLEX_EXPR:
4708 expr = build_binary_op (EXPR_LOCATION (expr),
4709 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4710 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4711 c_common_truthvalue_conversion (location,
4712 TREE_OPERAND (expr, 0)),
4713 c_common_truthvalue_conversion (location,
4714 TREE_OPERAND (expr, 1)),
4716 goto ret;
4718 case NEGATE_EXPR:
4719 case ABS_EXPR:
4720 case FLOAT_EXPR:
4721 case EXCESS_PRECISION_EXPR:
4722 /* These don't change whether an object is nonzero or zero. */
4723 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4725 case LROTATE_EXPR:
4726 case RROTATE_EXPR:
4727 /* These don't change whether an object is zero or nonzero, but
4728 we can't ignore them if their second arg has side-effects. */
4729 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4731 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4732 TREE_OPERAND (expr, 1),
4733 c_common_truthvalue_conversion
4734 (location, TREE_OPERAND (expr, 0)));
4735 goto ret;
4737 else
4738 return c_common_truthvalue_conversion (location,
4739 TREE_OPERAND (expr, 0));
4741 case COND_EXPR:
4742 /* Distribute the conversion into the arms of a COND_EXPR. */
4743 if (c_dialect_cxx ())
4745 tree op1 = TREE_OPERAND (expr, 1);
4746 tree op2 = TREE_OPERAND (expr, 2);
4747 /* In C++ one of the arms might have void type if it is throw. */
4748 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4749 op1 = c_common_truthvalue_conversion (location, op1);
4750 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4751 op2 = c_common_truthvalue_conversion (location, op2);
4752 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4753 TREE_OPERAND (expr, 0), op1, op2);
4754 goto ret;
4756 else
4758 /* Folding will happen later for C. */
4759 expr = build3 (COND_EXPR, truthvalue_type_node,
4760 TREE_OPERAND (expr, 0),
4761 c_common_truthvalue_conversion (location,
4762 TREE_OPERAND (expr, 1)),
4763 c_common_truthvalue_conversion (location,
4764 TREE_OPERAND (expr, 2)));
4765 goto ret;
4768 CASE_CONVERT:
4770 tree totype = TREE_TYPE (expr);
4771 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4773 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4774 since that affects how `default_conversion' will behave. */
4775 if (TREE_CODE (totype) == REFERENCE_TYPE
4776 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4777 break;
4778 /* Don't strip a conversion from C++0x scoped enum, since they
4779 don't implicitly convert to other types. */
4780 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4781 && ENUM_IS_SCOPED (fromtype))
4782 break;
4783 /* If this isn't narrowing the argument, we can ignore it. */
4784 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4785 return c_common_truthvalue_conversion (location,
4786 TREE_OPERAND (expr, 0));
4788 break;
4790 case MODIFY_EXPR:
4791 if (!TREE_NO_WARNING (expr)
4792 && warn_parentheses)
4794 warning (OPT_Wparentheses,
4795 "suggest parentheses around assignment used as truth value");
4796 TREE_NO_WARNING (expr) = 1;
4798 break;
4800 default:
4801 break;
4804 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4806 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4807 expr = (build_binary_op
4808 (EXPR_LOCATION (expr),
4809 (TREE_SIDE_EFFECTS (expr)
4810 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4811 c_common_truthvalue_conversion
4812 (location,
4813 build_unary_op (location, REALPART_EXPR, t, 0)),
4814 c_common_truthvalue_conversion
4815 (location,
4816 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4817 0));
4818 goto ret;
4821 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4823 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4824 FCONST0 (TYPE_MODE
4825 (TREE_TYPE (expr))));
4826 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4828 else
4829 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4831 ret:
4832 protected_set_expr_location (expr, location);
4833 return expr;
4836 static void def_builtin_1 (enum built_in_function fncode,
4837 const char *name,
4838 enum built_in_class fnclass,
4839 tree fntype, tree libtype,
4840 bool both_p, bool fallback_p, bool nonansi_p,
4841 tree fnattrs, bool implicit_p);
4844 /* Apply the TYPE_QUALS to the new DECL. */
4846 void
4847 c_apply_type_quals_to_decl (int type_quals, tree decl)
4849 tree type = TREE_TYPE (decl);
4851 if (type == error_mark_node)
4852 return;
4854 if ((type_quals & TYPE_QUAL_CONST)
4855 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4856 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4857 constructor can produce constant init, so rely on cp_finish_decl to
4858 clear TREE_READONLY if the variable has non-constant init. */
4859 TREE_READONLY (decl) = 1;
4860 if (type_quals & TYPE_QUAL_VOLATILE)
4862 TREE_SIDE_EFFECTS (decl) = 1;
4863 TREE_THIS_VOLATILE (decl) = 1;
4865 if (type_quals & TYPE_QUAL_RESTRICT)
4867 while (type && TREE_CODE (type) == ARRAY_TYPE)
4868 /* Allow 'restrict' on arrays of pointers.
4869 FIXME currently we just ignore it. */
4870 type = TREE_TYPE (type);
4871 if (!type
4872 || !POINTER_TYPE_P (type)
4873 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4874 error ("invalid use of %<restrict%>");
4878 struct c_type_hasher : ggc_hasher<tree>
4880 static hashval_t hash (tree);
4881 static bool equal (tree, tree);
4884 /* Hash function for the problem of multiple type definitions in
4885 different files. This must hash all types that will compare
4886 equal via comptypes to the same value. In practice it hashes
4887 on some of the simple stuff and leaves the details to comptypes. */
4889 hashval_t
4890 c_type_hasher::hash (tree t)
4892 int n_elements;
4893 int shift, size;
4894 tree t2;
4895 switch (TREE_CODE (t))
4897 /* For pointers, hash on pointee type plus some swizzling. */
4898 case POINTER_TYPE:
4899 return hash (TREE_TYPE (t)) ^ 0x3003003;
4900 /* Hash on number of elements and total size. */
4901 case ENUMERAL_TYPE:
4902 shift = 3;
4903 t2 = TYPE_VALUES (t);
4904 break;
4905 case RECORD_TYPE:
4906 shift = 0;
4907 t2 = TYPE_FIELDS (t);
4908 break;
4909 case QUAL_UNION_TYPE:
4910 shift = 1;
4911 t2 = TYPE_FIELDS (t);
4912 break;
4913 case UNION_TYPE:
4914 shift = 2;
4915 t2 = TYPE_FIELDS (t);
4916 break;
4917 default:
4918 gcc_unreachable ();
4920 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4921 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4922 n_elements = list_length (t2);
4923 /* We might have a VLA here. */
4924 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4925 size = 0;
4926 else
4927 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4928 return ((size << 24) | (n_elements << shift));
4931 bool
4932 c_type_hasher::equal (tree t1, tree t2)
4934 return lang_hooks.types_compatible_p (t1, t2);
4937 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4939 /* Return the typed-based alias set for T, which may be an expression
4940 or a type. Return -1 if we don't do anything special. */
4942 alias_set_type
4943 c_common_get_alias_set (tree t)
4945 tree u;
4947 /* For VLAs, use the alias set of the element type rather than the
4948 default of alias set 0 for types compared structurally. */
4949 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4951 if (TREE_CODE (t) == ARRAY_TYPE)
4952 return get_alias_set (TREE_TYPE (t));
4953 return -1;
4956 /* Permit type-punning when accessing a union, provided the access
4957 is directly through the union. For example, this code does not
4958 permit taking the address of a union member and then storing
4959 through it. Even the type-punning allowed here is a GCC
4960 extension, albeit a common and useful one; the C standard says
4961 that such accesses have implementation-defined behavior. */
4962 for (u = t;
4963 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4964 u = TREE_OPERAND (u, 0))
4965 if (TREE_CODE (u) == COMPONENT_REF
4966 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4967 return 0;
4969 /* That's all the expressions we handle specially. */
4970 if (!TYPE_P (t))
4971 return -1;
4973 /* The C standard guarantees that any object may be accessed via an
4974 lvalue that has character type. */
4975 if (t == char_type_node
4976 || t == signed_char_type_node
4977 || t == unsigned_char_type_node)
4978 return 0;
4980 /* The C standard specifically allows aliasing between signed and
4981 unsigned variants of the same type. We treat the signed
4982 variant as canonical. */
4983 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4985 tree t1 = c_common_signed_type (t);
4987 /* t1 == t can happen for boolean nodes which are always unsigned. */
4988 if (t1 != t)
4989 return get_alias_set (t1);
4992 /* Handle the case of multiple type nodes referring to "the same" type,
4993 which occurs with IMA. These share an alias set. FIXME: Currently only
4994 C90 is handled. (In C99 type compatibility is not transitive, which
4995 complicates things mightily. The alias set splay trees can theoretically
4996 represent this, but insertion is tricky when you consider all the
4997 different orders things might arrive in.) */
4999 if (c_language != clk_c || flag_isoc99)
5000 return -1;
5002 /* Save time if there's only one input file. */
5003 if (num_in_fnames == 1)
5004 return -1;
5006 /* Pointers need special handling if they point to any type that
5007 needs special handling (below). */
5008 if (TREE_CODE (t) == POINTER_TYPE)
5010 tree t2;
5011 /* Find bottom type under any nested POINTERs. */
5012 for (t2 = TREE_TYPE (t);
5013 TREE_CODE (t2) == POINTER_TYPE;
5014 t2 = TREE_TYPE (t2))
5016 if (TREE_CODE (t2) != RECORD_TYPE
5017 && TREE_CODE (t2) != ENUMERAL_TYPE
5018 && TREE_CODE (t2) != QUAL_UNION_TYPE
5019 && TREE_CODE (t2) != UNION_TYPE)
5020 return -1;
5021 if (TYPE_SIZE (t2) == 0)
5022 return -1;
5024 /* These are the only cases that need special handling. */
5025 if (TREE_CODE (t) != RECORD_TYPE
5026 && TREE_CODE (t) != ENUMERAL_TYPE
5027 && TREE_CODE (t) != QUAL_UNION_TYPE
5028 && TREE_CODE (t) != UNION_TYPE
5029 && TREE_CODE (t) != POINTER_TYPE)
5030 return -1;
5031 /* Undefined? */
5032 if (TYPE_SIZE (t) == 0)
5033 return -1;
5035 /* Look up t in hash table. Only one of the compatible types within each
5036 alias set is recorded in the table. */
5037 if (!type_hash_table)
5038 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5039 tree *slot = type_hash_table->find_slot (t, INSERT);
5040 if (*slot != NULL)
5042 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5043 return TYPE_ALIAS_SET ((tree)*slot);
5045 else
5046 /* Our caller will assign and record (in t) a new alias set; all we need
5047 to do is remember t in the hash table. */
5048 *slot = t;
5050 return -1;
5053 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5054 the IS_SIZEOF parameter indicates which operator is being applied.
5055 The COMPLAIN flag controls whether we should diagnose possibly
5056 ill-formed constructs or not. LOC is the location of the SIZEOF or
5057 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5058 a type in any context should be returned, rather than the normal
5059 alignment for that type. */
5061 tree
5062 c_sizeof_or_alignof_type (location_t loc,
5063 tree type, bool is_sizeof, bool min_alignof,
5064 int complain)
5066 const char *op_name;
5067 tree value = NULL;
5068 enum tree_code type_code = TREE_CODE (type);
5070 op_name = is_sizeof ? "sizeof" : "__alignof__";
5072 if (type_code == FUNCTION_TYPE)
5074 if (is_sizeof)
5076 if (complain && warn_pointer_arith)
5077 pedwarn (loc, OPT_Wpointer_arith,
5078 "invalid application of %<sizeof%> to a function type");
5079 else if (!complain)
5080 return error_mark_node;
5081 value = size_one_node;
5083 else
5085 if (complain)
5087 if (c_dialect_cxx ())
5088 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5089 "%<alignof%> applied to a function type");
5090 else
5091 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5092 "%<_Alignof%> applied to a function type");
5094 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5097 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5099 if (type_code == VOID_TYPE
5100 && complain && warn_pointer_arith)
5101 pedwarn (loc, OPT_Wpointer_arith,
5102 "invalid application of %qs to a void type", op_name);
5103 else if (!complain)
5104 return error_mark_node;
5105 value = size_one_node;
5107 else if (!COMPLETE_TYPE_P (type)
5108 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5110 if (complain)
5111 error_at (loc, "invalid application of %qs to incomplete type %qT",
5112 op_name, type);
5113 return error_mark_node;
5115 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5116 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5118 if (complain)
5119 error_at (loc, "invalid application of %qs to array type %qT of "
5120 "incomplete element type", op_name, type);
5121 return error_mark_node;
5123 else
5125 if (is_sizeof)
5126 /* Convert in case a char is more than one unit. */
5127 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5128 size_int (TYPE_PRECISION (char_type_node)
5129 / BITS_PER_UNIT));
5130 else if (min_alignof)
5131 value = size_int (min_align_of_type (type));
5132 else
5133 value = size_int (TYPE_ALIGN_UNIT (type));
5136 /* VALUE will have the middle-end integer type sizetype.
5137 However, we should really return a value of type `size_t',
5138 which is just a typedef for an ordinary integer type. */
5139 value = fold_convert_loc (loc, size_type_node, value);
5141 return value;
5144 /* Implement the __alignof keyword: Return the minimum required
5145 alignment of EXPR, measured in bytes. For VAR_DECLs,
5146 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5147 from an "aligned" __attribute__ specification). LOC is the
5148 location of the ALIGNOF operator. */
5150 tree
5151 c_alignof_expr (location_t loc, tree expr)
5153 tree t;
5155 if (VAR_OR_FUNCTION_DECL_P (expr))
5156 t = size_int (DECL_ALIGN_UNIT (expr));
5158 else if (TREE_CODE (expr) == COMPONENT_REF
5159 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5161 error_at (loc, "%<__alignof%> applied to a bit-field");
5162 t = size_one_node;
5164 else if (TREE_CODE (expr) == COMPONENT_REF
5165 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5166 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5168 else if (TREE_CODE (expr) == INDIRECT_REF)
5170 tree t = TREE_OPERAND (expr, 0);
5171 tree best = t;
5172 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5174 while (CONVERT_EXPR_P (t)
5175 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5177 int thisalign;
5179 t = TREE_OPERAND (t, 0);
5180 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5181 if (thisalign > bestalign)
5182 best = t, bestalign = thisalign;
5184 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5186 else
5187 return c_alignof (loc, TREE_TYPE (expr));
5189 return fold_convert_loc (loc, size_type_node, t);
5192 /* Handle C and C++ default attributes. */
5194 enum built_in_attribute
5196 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5197 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5198 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5199 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5200 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5201 #include "builtin-attrs.def"
5202 #undef DEF_ATTR_NULL_TREE
5203 #undef DEF_ATTR_INT
5204 #undef DEF_ATTR_STRING
5205 #undef DEF_ATTR_IDENT
5206 #undef DEF_ATTR_TREE_LIST
5207 ATTR_LAST
5210 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5212 static void c_init_attributes (void);
5214 enum c_builtin_type
5216 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5217 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5218 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5219 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5220 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5221 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5222 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5223 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5224 ARG6) NAME,
5225 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5226 ARG6, ARG7) NAME,
5227 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5228 ARG6, ARG7, ARG8) NAME,
5229 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5230 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5231 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5232 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5233 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5234 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5235 NAME,
5236 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5237 #include "builtin-types.def"
5238 #undef DEF_PRIMITIVE_TYPE
5239 #undef DEF_FUNCTION_TYPE_0
5240 #undef DEF_FUNCTION_TYPE_1
5241 #undef DEF_FUNCTION_TYPE_2
5242 #undef DEF_FUNCTION_TYPE_3
5243 #undef DEF_FUNCTION_TYPE_4
5244 #undef DEF_FUNCTION_TYPE_5
5245 #undef DEF_FUNCTION_TYPE_6
5246 #undef DEF_FUNCTION_TYPE_7
5247 #undef DEF_FUNCTION_TYPE_8
5248 #undef DEF_FUNCTION_TYPE_VAR_0
5249 #undef DEF_FUNCTION_TYPE_VAR_1
5250 #undef DEF_FUNCTION_TYPE_VAR_2
5251 #undef DEF_FUNCTION_TYPE_VAR_3
5252 #undef DEF_FUNCTION_TYPE_VAR_4
5253 #undef DEF_FUNCTION_TYPE_VAR_5
5254 #undef DEF_POINTER_TYPE
5255 BT_LAST
5258 typedef enum c_builtin_type builtin_type;
5260 /* A temporary array for c_common_nodes_and_builtins. Used in
5261 communication with def_fn_type. */
5262 static tree builtin_types[(int) BT_LAST + 1];
5264 /* A helper function for c_common_nodes_and_builtins. Build function type
5265 for DEF with return type RET and N arguments. If VAR is true, then the
5266 function should be variadic after those N arguments.
5268 Takes special care not to ICE if any of the types involved are
5269 error_mark_node, which indicates that said type is not in fact available
5270 (see builtin_type_for_size). In which case the function type as a whole
5271 should be error_mark_node. */
5273 static void
5274 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5276 tree t;
5277 tree *args = XALLOCAVEC (tree, n);
5278 va_list list;
5279 int i;
5281 va_start (list, n);
5282 for (i = 0; i < n; ++i)
5284 builtin_type a = (builtin_type) va_arg (list, int);
5285 t = builtin_types[a];
5286 if (t == error_mark_node)
5287 goto egress;
5288 args[i] = t;
5291 t = builtin_types[ret];
5292 if (t == error_mark_node)
5293 goto egress;
5294 if (var)
5295 t = build_varargs_function_type_array (t, n, args);
5296 else
5297 t = build_function_type_array (t, n, args);
5299 egress:
5300 builtin_types[def] = t;
5301 va_end (list);
5304 /* Build builtin functions common to both C and C++ language
5305 frontends. */
5307 static void
5308 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5310 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5311 builtin_types[ENUM] = VALUE;
5312 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5313 def_fn_type (ENUM, RETURN, 0, 0);
5314 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5315 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5316 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5317 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5318 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5319 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5320 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5321 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5322 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5323 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5324 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5325 ARG6) \
5326 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5327 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5328 ARG6, ARG7) \
5329 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5330 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5331 ARG6, ARG7, ARG8) \
5332 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5333 ARG7, ARG8);
5334 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5335 def_fn_type (ENUM, RETURN, 1, 0);
5336 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5337 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5338 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5339 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5340 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5341 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5342 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5343 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5344 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5345 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5346 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5347 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5349 #include "builtin-types.def"
5351 #undef DEF_PRIMITIVE_TYPE
5352 #undef DEF_FUNCTION_TYPE_0
5353 #undef DEF_FUNCTION_TYPE_1
5354 #undef DEF_FUNCTION_TYPE_2
5355 #undef DEF_FUNCTION_TYPE_3
5356 #undef DEF_FUNCTION_TYPE_4
5357 #undef DEF_FUNCTION_TYPE_5
5358 #undef DEF_FUNCTION_TYPE_6
5359 #undef DEF_FUNCTION_TYPE_7
5360 #undef DEF_FUNCTION_TYPE_8
5361 #undef DEF_FUNCTION_TYPE_VAR_0
5362 #undef DEF_FUNCTION_TYPE_VAR_1
5363 #undef DEF_FUNCTION_TYPE_VAR_2
5364 #undef DEF_FUNCTION_TYPE_VAR_3
5365 #undef DEF_FUNCTION_TYPE_VAR_4
5366 #undef DEF_FUNCTION_TYPE_VAR_5
5367 #undef DEF_POINTER_TYPE
5368 builtin_types[(int) BT_LAST] = NULL_TREE;
5370 c_init_attributes ();
5372 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5373 NONANSI_P, ATTRS, IMPLICIT, COND) \
5374 if (NAME && COND) \
5375 def_builtin_1 (ENUM, NAME, CLASS, \
5376 builtin_types[(int) TYPE], \
5377 builtin_types[(int) LIBTYPE], \
5378 BOTH_P, FALLBACK_P, NONANSI_P, \
5379 built_in_attributes[(int) ATTRS], IMPLICIT);
5380 #include "builtins.def"
5381 #undef DEF_BUILTIN
5383 targetm.init_builtins ();
5385 build_common_builtin_nodes ();
5387 if (flag_cilkplus)
5388 cilk_init_builtins ();
5391 /* Like get_identifier, but avoid warnings about null arguments when
5392 the argument may be NULL for targets where GCC lacks stdint.h type
5393 information. */
5395 static inline tree
5396 c_get_ident (const char *id)
5398 return get_identifier (id);
5401 /* Build tree nodes and builtin functions common to both C and C++ language
5402 frontends. */
5404 void
5405 c_common_nodes_and_builtins (void)
5407 int char16_type_size;
5408 int char32_type_size;
5409 int wchar_type_size;
5410 tree array_domain_type;
5411 tree va_list_ref_type_node;
5412 tree va_list_arg_type_node;
5413 int i;
5415 build_common_tree_nodes (flag_signed_char, flag_short_double);
5417 /* Define `int' and `char' first so that dbx will output them first. */
5418 record_builtin_type (RID_INT, NULL, integer_type_node);
5419 record_builtin_type (RID_CHAR, "char", char_type_node);
5421 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5422 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5423 but not C. Are the conditionals here needed? */
5424 if (c_dialect_cxx ())
5425 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5426 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5427 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5428 record_builtin_type (RID_MAX, "long unsigned int",
5429 long_unsigned_type_node);
5431 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5433 char name[25];
5435 sprintf (name, "__int%d", int_n_data[i].bitsize);
5436 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name),
5437 int_n_trees[i].signed_type);
5438 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5439 record_builtin_type (RID_MAX, xstrdup (name),
5440 int_n_trees[i].unsigned_type);
5443 if (c_dialect_cxx ())
5444 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5445 record_builtin_type (RID_MAX, "long long int",
5446 long_long_integer_type_node);
5447 record_builtin_type (RID_MAX, "long long unsigned int",
5448 long_long_unsigned_type_node);
5449 if (c_dialect_cxx ())
5450 record_builtin_type (RID_MAX, "long long unsigned",
5451 long_long_unsigned_type_node);
5452 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5453 record_builtin_type (RID_MAX, "short unsigned int",
5454 short_unsigned_type_node);
5455 if (c_dialect_cxx ())
5456 record_builtin_type (RID_MAX, "unsigned short",
5457 short_unsigned_type_node);
5459 /* Define both `signed char' and `unsigned char'. */
5460 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5461 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5463 /* These are types that c_common_type_for_size and
5464 c_common_type_for_mode use. */
5465 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5466 TYPE_DECL, NULL_TREE,
5467 intQI_type_node));
5468 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5469 TYPE_DECL, NULL_TREE,
5470 intHI_type_node));
5471 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5472 TYPE_DECL, NULL_TREE,
5473 intSI_type_node));
5474 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5475 TYPE_DECL, NULL_TREE,
5476 intDI_type_node));
5477 #if HOST_BITS_PER_WIDE_INT >= 64
5478 /* Note that this is different than the __int128 type that's part of
5479 the generic __intN support. */
5480 if (targetm.scalar_mode_supported_p (TImode))
5481 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5482 TYPE_DECL,
5483 get_identifier ("__int128_t"),
5484 intTI_type_node));
5485 #endif
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5487 TYPE_DECL, NULL_TREE,
5488 unsigned_intQI_type_node));
5489 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5490 TYPE_DECL, NULL_TREE,
5491 unsigned_intHI_type_node));
5492 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5493 TYPE_DECL, NULL_TREE,
5494 unsigned_intSI_type_node));
5495 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5496 TYPE_DECL, NULL_TREE,
5497 unsigned_intDI_type_node));
5498 #if HOST_BITS_PER_WIDE_INT >= 64
5499 if (targetm.scalar_mode_supported_p (TImode))
5500 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5501 TYPE_DECL,
5502 get_identifier ("__uint128_t"),
5503 unsigned_intTI_type_node));
5504 #endif
5506 /* Create the widest literal types. */
5507 widest_integer_literal_type_node
5508 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5509 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5510 TYPE_DECL, NULL_TREE,
5511 widest_integer_literal_type_node));
5513 widest_unsigned_literal_type_node
5514 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5515 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5516 TYPE_DECL, NULL_TREE,
5517 widest_unsigned_literal_type_node));
5519 signed_size_type_node = c_common_signed_type (size_type_node);
5521 pid_type_node =
5522 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5524 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5525 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5526 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5528 /* Only supported decimal floating point extension if the target
5529 actually supports underlying modes. */
5530 if (targetm.scalar_mode_supported_p (SDmode)
5531 && targetm.scalar_mode_supported_p (DDmode)
5532 && targetm.scalar_mode_supported_p (TDmode))
5534 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5535 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5536 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5539 if (targetm.fixed_point_supported_p ())
5541 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5542 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5543 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5544 record_builtin_type (RID_MAX, "long long _Fract",
5545 long_long_fract_type_node);
5546 record_builtin_type (RID_MAX, "unsigned short _Fract",
5547 unsigned_short_fract_type_node);
5548 record_builtin_type (RID_MAX, "unsigned _Fract",
5549 unsigned_fract_type_node);
5550 record_builtin_type (RID_MAX, "unsigned long _Fract",
5551 unsigned_long_fract_type_node);
5552 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5553 unsigned_long_long_fract_type_node);
5554 record_builtin_type (RID_MAX, "_Sat short _Fract",
5555 sat_short_fract_type_node);
5556 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5557 record_builtin_type (RID_MAX, "_Sat long _Fract",
5558 sat_long_fract_type_node);
5559 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5560 sat_long_long_fract_type_node);
5561 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5562 sat_unsigned_short_fract_type_node);
5563 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5564 sat_unsigned_fract_type_node);
5565 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5566 sat_unsigned_long_fract_type_node);
5567 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5568 sat_unsigned_long_long_fract_type_node);
5569 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5570 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5571 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5572 record_builtin_type (RID_MAX, "long long _Accum",
5573 long_long_accum_type_node);
5574 record_builtin_type (RID_MAX, "unsigned short _Accum",
5575 unsigned_short_accum_type_node);
5576 record_builtin_type (RID_MAX, "unsigned _Accum",
5577 unsigned_accum_type_node);
5578 record_builtin_type (RID_MAX, "unsigned long _Accum",
5579 unsigned_long_accum_type_node);
5580 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5581 unsigned_long_long_accum_type_node);
5582 record_builtin_type (RID_MAX, "_Sat short _Accum",
5583 sat_short_accum_type_node);
5584 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5585 record_builtin_type (RID_MAX, "_Sat long _Accum",
5586 sat_long_accum_type_node);
5587 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5588 sat_long_long_accum_type_node);
5589 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5590 sat_unsigned_short_accum_type_node);
5591 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5592 sat_unsigned_accum_type_node);
5593 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5594 sat_unsigned_long_accum_type_node);
5595 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5596 sat_unsigned_long_long_accum_type_node);
5600 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5601 TYPE_DECL,
5602 get_identifier ("complex int"),
5603 complex_integer_type_node));
5604 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5605 TYPE_DECL,
5606 get_identifier ("complex float"),
5607 complex_float_type_node));
5608 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5609 TYPE_DECL,
5610 get_identifier ("complex double"),
5611 complex_double_type_node));
5612 lang_hooks.decls.pushdecl
5613 (build_decl (UNKNOWN_LOCATION,
5614 TYPE_DECL, get_identifier ("complex long double"),
5615 complex_long_double_type_node));
5617 if (c_dialect_cxx ())
5618 /* For C++, make fileptr_type_node a distinct void * type until
5619 FILE type is defined. */
5620 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5622 record_builtin_type (RID_VOID, NULL, void_type_node);
5624 /* Set the TYPE_NAME for any variants that were built before
5625 record_builtin_type gave names to the built-in types. */
5627 tree void_name = TYPE_NAME (void_type_node);
5628 TYPE_NAME (void_type_node) = NULL_TREE;
5629 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5630 = void_name;
5631 TYPE_NAME (void_type_node) = void_name;
5634 void_list_node = build_void_list_node ();
5636 /* Make a type to be the domain of a few array types
5637 whose domains don't really matter.
5638 200 is small enough that it always fits in size_t
5639 and large enough that it can hold most function names for the
5640 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5641 array_domain_type = build_index_type (size_int (200));
5643 /* Make a type for arrays of characters.
5644 With luck nothing will ever really depend on the length of this
5645 array type. */
5646 char_array_type_node
5647 = build_array_type (char_type_node, array_domain_type);
5649 string_type_node = build_pointer_type (char_type_node);
5650 const_string_type_node
5651 = build_pointer_type (build_qualified_type
5652 (char_type_node, TYPE_QUAL_CONST));
5654 /* This is special for C++ so functions can be overloaded. */
5655 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5656 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5657 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5658 underlying_wchar_type_node = wchar_type_node;
5659 if (c_dialect_cxx ())
5661 if (TYPE_UNSIGNED (wchar_type_node))
5662 wchar_type_node = make_unsigned_type (wchar_type_size);
5663 else
5664 wchar_type_node = make_signed_type (wchar_type_size);
5665 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5668 /* This is for wide string constants. */
5669 wchar_array_type_node
5670 = build_array_type (wchar_type_node, array_domain_type);
5672 /* Define 'char16_t'. */
5673 char16_type_node = get_identifier (CHAR16_TYPE);
5674 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5675 char16_type_size = TYPE_PRECISION (char16_type_node);
5676 if (c_dialect_cxx ())
5678 char16_type_node = make_unsigned_type (char16_type_size);
5680 if (cxx_dialect >= cxx11)
5681 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5684 /* This is for UTF-16 string constants. */
5685 char16_array_type_node
5686 = build_array_type (char16_type_node, array_domain_type);
5688 /* Define 'char32_t'. */
5689 char32_type_node = get_identifier (CHAR32_TYPE);
5690 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5691 char32_type_size = TYPE_PRECISION (char32_type_node);
5692 if (c_dialect_cxx ())
5694 char32_type_node = make_unsigned_type (char32_type_size);
5696 if (cxx_dialect >= cxx11)
5697 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5700 /* This is for UTF-32 string constants. */
5701 char32_array_type_node
5702 = build_array_type (char32_type_node, array_domain_type);
5704 wint_type_node =
5705 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5707 intmax_type_node =
5708 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5709 uintmax_type_node =
5710 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5712 if (SIG_ATOMIC_TYPE)
5713 sig_atomic_type_node =
5714 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5715 if (INT8_TYPE)
5716 int8_type_node =
5717 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5718 if (INT16_TYPE)
5719 int16_type_node =
5720 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5721 if (INT32_TYPE)
5722 int32_type_node =
5723 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5724 if (INT64_TYPE)
5725 int64_type_node =
5726 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5727 if (UINT8_TYPE)
5728 uint8_type_node =
5729 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5730 if (UINT16_TYPE)
5731 c_uint16_type_node = uint16_type_node =
5732 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5733 if (UINT32_TYPE)
5734 c_uint32_type_node = uint32_type_node =
5735 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5736 if (UINT64_TYPE)
5737 c_uint64_type_node = uint64_type_node =
5738 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5739 if (INT_LEAST8_TYPE)
5740 int_least8_type_node =
5741 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5742 if (INT_LEAST16_TYPE)
5743 int_least16_type_node =
5744 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5745 if (INT_LEAST32_TYPE)
5746 int_least32_type_node =
5747 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5748 if (INT_LEAST64_TYPE)
5749 int_least64_type_node =
5750 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5751 if (UINT_LEAST8_TYPE)
5752 uint_least8_type_node =
5753 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5754 if (UINT_LEAST16_TYPE)
5755 uint_least16_type_node =
5756 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5757 if (UINT_LEAST32_TYPE)
5758 uint_least32_type_node =
5759 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5760 if (UINT_LEAST64_TYPE)
5761 uint_least64_type_node =
5762 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5763 if (INT_FAST8_TYPE)
5764 int_fast8_type_node =
5765 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5766 if (INT_FAST16_TYPE)
5767 int_fast16_type_node =
5768 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5769 if (INT_FAST32_TYPE)
5770 int_fast32_type_node =
5771 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5772 if (INT_FAST64_TYPE)
5773 int_fast64_type_node =
5774 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5775 if (UINT_FAST8_TYPE)
5776 uint_fast8_type_node =
5777 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5778 if (UINT_FAST16_TYPE)
5779 uint_fast16_type_node =
5780 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5781 if (UINT_FAST32_TYPE)
5782 uint_fast32_type_node =
5783 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5784 if (UINT_FAST64_TYPE)
5785 uint_fast64_type_node =
5786 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5787 if (INTPTR_TYPE)
5788 intptr_type_node =
5789 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5790 if (UINTPTR_TYPE)
5791 uintptr_type_node =
5792 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5794 default_function_type
5795 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5796 ptrdiff_type_node
5797 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5798 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5800 lang_hooks.decls.pushdecl
5801 (build_decl (UNKNOWN_LOCATION,
5802 TYPE_DECL, get_identifier ("__builtin_va_list"),
5803 va_list_type_node));
5804 if (targetm.enum_va_list_p)
5806 int l;
5807 const char *pname;
5808 tree ptype;
5810 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5812 lang_hooks.decls.pushdecl
5813 (build_decl (UNKNOWN_LOCATION,
5814 TYPE_DECL, get_identifier (pname),
5815 ptype));
5820 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5822 va_list_arg_type_node = va_list_ref_type_node =
5823 build_pointer_type (TREE_TYPE (va_list_type_node));
5825 else
5827 va_list_arg_type_node = va_list_type_node;
5828 va_list_ref_type_node = build_reference_type (va_list_type_node);
5831 if (!flag_preprocess_only)
5832 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5834 main_identifier_node = get_identifier ("main");
5836 /* Create the built-in __null node. It is important that this is
5837 not shared. */
5838 null_node = make_int_cst (1, 1);
5839 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5841 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5842 memset (builtin_types, 0, sizeof (builtin_types));
5845 /* The number of named compound-literals generated thus far. */
5846 static GTY(()) int compound_literal_number;
5848 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5850 void
5851 set_compound_literal_name (tree decl)
5853 char *name;
5854 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5855 compound_literal_number);
5856 compound_literal_number++;
5857 DECL_NAME (decl) = get_identifier (name);
5860 tree
5861 build_va_arg (location_t loc, tree expr, tree type)
5863 expr = build1 (VA_ARG_EXPR, type, expr);
5864 SET_EXPR_LOCATION (expr, loc);
5865 return expr;
5869 /* Linked list of disabled built-in functions. */
5871 typedef struct disabled_builtin
5873 const char *name;
5874 struct disabled_builtin *next;
5875 } disabled_builtin;
5876 static disabled_builtin *disabled_builtins = NULL;
5878 static bool builtin_function_disabled_p (const char *);
5880 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5881 begins with "__builtin_", give an error. */
5883 void
5884 disable_builtin_function (const char *name)
5886 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5887 error ("cannot disable built-in function %qs", name);
5888 else
5890 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5891 new_disabled_builtin->name = name;
5892 new_disabled_builtin->next = disabled_builtins;
5893 disabled_builtins = new_disabled_builtin;
5898 /* Return true if the built-in function NAME has been disabled, false
5899 otherwise. */
5901 static bool
5902 builtin_function_disabled_p (const char *name)
5904 disabled_builtin *p;
5905 for (p = disabled_builtins; p != NULL; p = p->next)
5907 if (strcmp (name, p->name) == 0)
5908 return true;
5910 return false;
5914 /* Worker for DEF_BUILTIN.
5915 Possibly define a builtin function with one or two names.
5916 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5917 nonansi_p and flag_no_nonansi_builtin. */
5919 static void
5920 def_builtin_1 (enum built_in_function fncode,
5921 const char *name,
5922 enum built_in_class fnclass,
5923 tree fntype, tree libtype,
5924 bool both_p, bool fallback_p, bool nonansi_p,
5925 tree fnattrs, bool implicit_p)
5927 tree decl;
5928 const char *libname;
5930 if (fntype == error_mark_node)
5931 return;
5933 gcc_assert ((!both_p && !fallback_p)
5934 || !strncmp (name, "__builtin_",
5935 strlen ("__builtin_")));
5937 libname = name + strlen ("__builtin_");
5938 decl = add_builtin_function (name, fntype, fncode, fnclass,
5939 (fallback_p ? libname : NULL),
5940 fnattrs);
5942 set_builtin_decl (fncode, decl, implicit_p);
5944 if (both_p
5945 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5946 && !(nonansi_p && flag_no_nonansi_builtin))
5947 add_builtin_function (libname, libtype, fncode, fnclass,
5948 NULL, fnattrs);
5951 /* Nonzero if the type T promotes to int. This is (nearly) the
5952 integral promotions defined in ISO C99 6.3.1.1/2. */
5954 bool
5955 c_promoting_integer_type_p (const_tree t)
5957 switch (TREE_CODE (t))
5959 case INTEGER_TYPE:
5960 return (TYPE_MAIN_VARIANT (t) == char_type_node
5961 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5962 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5963 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5964 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5965 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5967 case ENUMERAL_TYPE:
5968 /* ??? Technically all enumerations not larger than an int
5969 promote to an int. But this is used along code paths
5970 that only want to notice a size change. */
5971 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5973 case BOOLEAN_TYPE:
5974 return 1;
5976 default:
5977 return 0;
5981 /* Return 1 if PARMS specifies a fixed number of parameters
5982 and none of their types is affected by default promotions. */
5985 self_promoting_args_p (const_tree parms)
5987 const_tree t;
5988 for (t = parms; t; t = TREE_CHAIN (t))
5990 tree type = TREE_VALUE (t);
5992 if (type == error_mark_node)
5993 continue;
5995 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5996 return 0;
5998 if (type == 0)
5999 return 0;
6001 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6002 return 0;
6004 if (c_promoting_integer_type_p (type))
6005 return 0;
6007 return 1;
6010 /* Recursively remove any '*' or '&' operator from TYPE. */
6011 tree
6012 strip_pointer_operator (tree t)
6014 while (POINTER_TYPE_P (t))
6015 t = TREE_TYPE (t);
6016 return t;
6019 /* Recursively remove pointer or array type from TYPE. */
6020 tree
6021 strip_pointer_or_array_types (tree t)
6023 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6024 t = TREE_TYPE (t);
6025 return t;
6028 /* Used to compare case labels. K1 and K2 are actually tree nodes
6029 representing case labels, or NULL_TREE for a `default' label.
6030 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6031 K2, and 0 if K1 and K2 are equal. */
6034 case_compare (splay_tree_key k1, splay_tree_key k2)
6036 /* Consider a NULL key (such as arises with a `default' label) to be
6037 smaller than anything else. */
6038 if (!k1)
6039 return k2 ? -1 : 0;
6040 else if (!k2)
6041 return k1 ? 1 : 0;
6043 return tree_int_cst_compare ((tree) k1, (tree) k2);
6046 /* Process a case label, located at LOC, for the range LOW_VALUE
6047 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6048 then this case label is actually a `default' label. If only
6049 HIGH_VALUE is NULL_TREE, then case label was declared using the
6050 usual C/C++ syntax, rather than the GNU case range extension.
6051 CASES is a tree containing all the case ranges processed so far;
6052 COND is the condition for the switch-statement itself. Returns the
6053 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6054 is created. */
6056 tree
6057 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6058 tree low_value, tree high_value)
6060 tree type;
6061 tree label;
6062 tree case_label;
6063 splay_tree_node node;
6065 /* Create the LABEL_DECL itself. */
6066 label = create_artificial_label (loc);
6068 /* If there was an error processing the switch condition, bail now
6069 before we get more confused. */
6070 if (!cond || cond == error_mark_node)
6071 goto error_out;
6073 if ((low_value && TREE_TYPE (low_value)
6074 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6075 || (high_value && TREE_TYPE (high_value)
6076 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6078 error_at (loc, "pointers are not permitted as case values");
6079 goto error_out;
6082 /* Case ranges are a GNU extension. */
6083 if (high_value)
6084 pedwarn (loc, OPT_Wpedantic,
6085 "range expressions in switch statements are non-standard");
6087 type = TREE_TYPE (cond);
6088 if (low_value)
6090 low_value = check_case_value (loc, low_value);
6091 low_value = convert_and_check (loc, type, low_value);
6092 if (low_value == error_mark_node)
6093 goto error_out;
6095 if (high_value)
6097 high_value = check_case_value (loc, high_value);
6098 high_value = convert_and_check (loc, type, high_value);
6099 if (high_value == error_mark_node)
6100 goto error_out;
6103 if (low_value && high_value)
6105 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6106 really a case range, even though it was written that way.
6107 Remove the HIGH_VALUE to simplify later processing. */
6108 if (tree_int_cst_equal (low_value, high_value))
6109 high_value = NULL_TREE;
6110 else if (!tree_int_cst_lt (low_value, high_value))
6111 warning_at (loc, 0, "empty range specified");
6114 /* See if the case is in range of the type of the original testing
6115 expression. If both low_value and high_value are out of range,
6116 don't insert the case label and return NULL_TREE. */
6117 if (low_value
6118 && !check_case_bounds (loc, type, orig_type,
6119 &low_value, high_value ? &high_value : NULL))
6120 return NULL_TREE;
6122 /* Look up the LOW_VALUE in the table of case labels we already
6123 have. */
6124 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6125 /* If there was not an exact match, check for overlapping ranges.
6126 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6127 that's a `default' label and the only overlap is an exact match. */
6128 if (!node && (low_value || high_value))
6130 splay_tree_node low_bound;
6131 splay_tree_node high_bound;
6133 /* Even though there wasn't an exact match, there might be an
6134 overlap between this case range and another case range.
6135 Since we've (inductively) not allowed any overlapping case
6136 ranges, we simply need to find the greatest low case label
6137 that is smaller that LOW_VALUE, and the smallest low case
6138 label that is greater than LOW_VALUE. If there is an overlap
6139 it will occur in one of these two ranges. */
6140 low_bound = splay_tree_predecessor (cases,
6141 (splay_tree_key) low_value);
6142 high_bound = splay_tree_successor (cases,
6143 (splay_tree_key) low_value);
6145 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6146 the LOW_VALUE, so there is no need to check unless the
6147 LOW_BOUND is in fact itself a case range. */
6148 if (low_bound
6149 && CASE_HIGH ((tree) low_bound->value)
6150 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6151 low_value) >= 0)
6152 node = low_bound;
6153 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6154 range is bigger than the low end of the current range, so we
6155 are only interested if the current range is a real range, and
6156 not an ordinary case label. */
6157 else if (high_bound
6158 && high_value
6159 && (tree_int_cst_compare ((tree) high_bound->key,
6160 high_value)
6161 <= 0))
6162 node = high_bound;
6164 /* If there was an overlap, issue an error. */
6165 if (node)
6167 tree duplicate = CASE_LABEL ((tree) node->value);
6169 if (high_value)
6171 error_at (loc, "duplicate (or overlapping) case value");
6172 error_at (DECL_SOURCE_LOCATION (duplicate),
6173 "this is the first entry overlapping that value");
6175 else if (low_value)
6177 error_at (loc, "duplicate case value") ;
6178 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6180 else
6182 error_at (loc, "multiple default labels in one switch");
6183 error_at (DECL_SOURCE_LOCATION (duplicate),
6184 "this is the first default label");
6186 goto error_out;
6189 /* Add a CASE_LABEL to the statement-tree. */
6190 case_label = add_stmt (build_case_label (low_value, high_value, label));
6191 /* Register this case label in the splay tree. */
6192 splay_tree_insert (cases,
6193 (splay_tree_key) low_value,
6194 (splay_tree_value) case_label);
6196 return case_label;
6198 error_out:
6199 /* Add a label so that the back-end doesn't think that the beginning of
6200 the switch is unreachable. Note that we do not add a case label, as
6201 that just leads to duplicates and thence to failure later on. */
6202 if (!cases->root)
6204 tree t = create_artificial_label (loc);
6205 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6207 return error_mark_node;
6210 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6211 Used to verify that case values match up with enumerator values. */
6213 static void
6214 match_case_to_enum_1 (tree key, tree type, tree label)
6216 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6218 if (tree_fits_uhwi_p (key))
6219 print_dec (key, buf, UNSIGNED);
6220 else if (tree_fits_shwi_p (key))
6221 print_dec (key, buf, SIGNED);
6222 else
6223 print_hex (key, buf);
6225 if (TYPE_NAME (type) == 0)
6226 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6227 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6228 "case value %qs not in enumerated type",
6229 buf);
6230 else
6231 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6232 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6233 "case value %qs not in enumerated type %qT",
6234 buf, type);
6237 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6238 Used to verify that case values match up with enumerator values. */
6240 static int
6241 match_case_to_enum (splay_tree_node node, void *data)
6243 tree label = (tree) node->value;
6244 tree type = (tree) data;
6246 /* Skip default case. */
6247 if (!CASE_LOW (label))
6248 return 0;
6250 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6251 when we did our enum->case scan. Reset our scratch bit after. */
6252 if (!CASE_LOW_SEEN (label))
6253 match_case_to_enum_1 (CASE_LOW (label), type, label);
6254 else
6255 CASE_LOW_SEEN (label) = 0;
6257 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6258 not set, that means that CASE_HIGH did not appear when we did our
6259 enum->case scan. Reset our scratch bit after. */
6260 if (CASE_HIGH (label))
6262 if (!CASE_HIGH_SEEN (label))
6263 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6264 else
6265 CASE_HIGH_SEEN (label) = 0;
6268 return 0;
6271 /* Handle -Wswitch*. Called from the front end after parsing the
6272 switch construct. */
6273 /* ??? Should probably be somewhere generic, since other languages
6274 besides C and C++ would want this. At the moment, however, C/C++
6275 are the only tree-ssa languages that support enumerations at all,
6276 so the point is moot. */
6278 void
6279 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6280 tree type, tree cond)
6282 splay_tree_node default_node;
6283 splay_tree_node node;
6284 tree chain;
6286 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6287 return;
6289 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6290 if (!default_node)
6291 warning_at (switch_location, OPT_Wswitch_default,
6292 "switch missing default case");
6294 /* From here on, we only care about about enumerated types. */
6295 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6296 return;
6298 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6299 if (!warn_switch_enum && !warn_switch)
6300 return;
6302 /* Check the cases. Warn about case values which are not members of
6303 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6304 there is no default case, check that exactly all enumeration
6305 literals are covered by the cases. */
6307 /* Clearing COND if it is not an integer constant simplifies
6308 the tests inside the loop below. */
6309 if (TREE_CODE (cond) != INTEGER_CST)
6310 cond = NULL_TREE;
6312 /* The time complexity here is O(N*lg(N)) worst case, but for the
6313 common case of monotonically increasing enumerators, it is
6314 O(N), since the nature of the splay tree will keep the next
6315 element adjacent to the root at all times. */
6317 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6319 tree value = TREE_VALUE (chain);
6320 if (TREE_CODE (value) == CONST_DECL)
6321 value = DECL_INITIAL (value);
6322 node = splay_tree_lookup (cases, (splay_tree_key) value);
6323 if (node)
6325 /* Mark the CASE_LOW part of the case entry as seen. */
6326 tree label = (tree) node->value;
6327 CASE_LOW_SEEN (label) = 1;
6328 continue;
6331 /* Even though there wasn't an exact match, there might be a
6332 case range which includes the enumerator's value. */
6333 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6334 if (node && CASE_HIGH ((tree) node->value))
6336 tree label = (tree) node->value;
6337 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6338 if (cmp >= 0)
6340 /* If we match the upper bound exactly, mark the CASE_HIGH
6341 part of the case entry as seen. */
6342 if (cmp == 0)
6343 CASE_HIGH_SEEN (label) = 1;
6344 continue;
6348 /* We've now determined that this enumerated literal isn't
6349 handled by the case labels of the switch statement. */
6351 /* If the switch expression is a constant, we only really care
6352 about whether that constant is handled by the switch. */
6353 if (cond && tree_int_cst_compare (cond, value))
6354 continue;
6356 /* If there is a default_node, the only relevant option is
6357 Wswitch-enum. Otherwise, if both are enabled then we prefer
6358 to warn using -Wswitch because -Wswitch is enabled by -Wall
6359 while -Wswitch-enum is explicit. */
6360 warning_at (switch_location,
6361 (default_node || !warn_switch
6362 ? OPT_Wswitch_enum
6363 : OPT_Wswitch),
6364 "enumeration value %qE not handled in switch",
6365 TREE_PURPOSE (chain));
6368 /* Warn if there are case expressions that don't correspond to
6369 enumerators. This can occur since C and C++ don't enforce
6370 type-checking of assignments to enumeration variables.
6372 The time complexity here is now always O(N) worst case, since
6373 we should have marked both the lower bound and upper bound of
6374 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6375 above. This scan also resets those fields. */
6377 splay_tree_foreach (cases, match_case_to_enum, type);
6380 /* Finish an expression taking the address of LABEL (an
6381 IDENTIFIER_NODE). Returns an expression for the address.
6383 LOC is the location for the expression returned. */
6385 tree
6386 finish_label_address_expr (tree label, location_t loc)
6388 tree result;
6390 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6392 if (label == error_mark_node)
6393 return error_mark_node;
6395 label = lookup_label (label);
6396 if (label == NULL_TREE)
6397 result = null_pointer_node;
6398 else
6400 TREE_USED (label) = 1;
6401 result = build1 (ADDR_EXPR, ptr_type_node, label);
6402 /* The current function is not necessarily uninlinable.
6403 Computed gotos are incompatible with inlining, but the value
6404 here could be used only in a diagnostic, for example. */
6405 protected_set_expr_location (result, loc);
6408 return result;
6412 /* Given a boolean expression ARG, return a tree representing an increment
6413 or decrement (as indicated by CODE) of ARG. The front end must check for
6414 invalid cases (e.g., decrement in C++). */
6415 tree
6416 boolean_increment (enum tree_code code, tree arg)
6418 tree val;
6419 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6421 arg = stabilize_reference (arg);
6422 switch (code)
6424 case PREINCREMENT_EXPR:
6425 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6426 break;
6427 case POSTINCREMENT_EXPR:
6428 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6429 arg = save_expr (arg);
6430 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6431 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6432 break;
6433 case PREDECREMENT_EXPR:
6434 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6435 invert_truthvalue_loc (input_location, arg));
6436 break;
6437 case POSTDECREMENT_EXPR:
6438 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6439 invert_truthvalue_loc (input_location, arg));
6440 arg = save_expr (arg);
6441 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6442 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6443 break;
6444 default:
6445 gcc_unreachable ();
6447 TREE_SIDE_EFFECTS (val) = 1;
6448 return val;
6451 /* Built-in macros for stddef.h and stdint.h, that require macros
6452 defined in this file. */
6453 void
6454 c_stddef_cpp_builtins(void)
6456 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6457 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6458 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6459 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6460 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6461 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6462 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6463 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6464 if (SIG_ATOMIC_TYPE)
6465 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6466 if (INT8_TYPE)
6467 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6468 if (INT16_TYPE)
6469 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6470 if (INT32_TYPE)
6471 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6472 if (INT64_TYPE)
6473 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6474 if (UINT8_TYPE)
6475 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6476 if (UINT16_TYPE)
6477 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6478 if (UINT32_TYPE)
6479 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6480 if (UINT64_TYPE)
6481 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6482 if (INT_LEAST8_TYPE)
6483 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6484 if (INT_LEAST16_TYPE)
6485 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6486 if (INT_LEAST32_TYPE)
6487 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6488 if (INT_LEAST64_TYPE)
6489 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6490 if (UINT_LEAST8_TYPE)
6491 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6492 if (UINT_LEAST16_TYPE)
6493 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6494 if (UINT_LEAST32_TYPE)
6495 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6496 if (UINT_LEAST64_TYPE)
6497 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6498 if (INT_FAST8_TYPE)
6499 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6500 if (INT_FAST16_TYPE)
6501 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6502 if (INT_FAST32_TYPE)
6503 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6504 if (INT_FAST64_TYPE)
6505 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6506 if (UINT_FAST8_TYPE)
6507 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6508 if (UINT_FAST16_TYPE)
6509 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6510 if (UINT_FAST32_TYPE)
6511 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6512 if (UINT_FAST64_TYPE)
6513 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6514 if (INTPTR_TYPE)
6515 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6516 if (UINTPTR_TYPE)
6517 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6520 static void
6521 c_init_attributes (void)
6523 /* Fill in the built_in_attributes array. */
6524 #define DEF_ATTR_NULL_TREE(ENUM) \
6525 built_in_attributes[(int) ENUM] = NULL_TREE;
6526 #define DEF_ATTR_INT(ENUM, VALUE) \
6527 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6528 #define DEF_ATTR_STRING(ENUM, VALUE) \
6529 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6530 #define DEF_ATTR_IDENT(ENUM, STRING) \
6531 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6532 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6533 built_in_attributes[(int) ENUM] \
6534 = tree_cons (built_in_attributes[(int) PURPOSE], \
6535 built_in_attributes[(int) VALUE], \
6536 built_in_attributes[(int) CHAIN]);
6537 #include "builtin-attrs.def"
6538 #undef DEF_ATTR_NULL_TREE
6539 #undef DEF_ATTR_INT
6540 #undef DEF_ATTR_IDENT
6541 #undef DEF_ATTR_TREE_LIST
6544 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6545 identifier as an argument, so the front end shouldn't look it up. */
6547 bool
6548 attribute_takes_identifier_p (const_tree attr_id)
6550 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6551 if (spec == NULL)
6552 /* Unknown attribute that we'll end up ignoring, return true so we
6553 don't complain about an identifier argument. */
6554 return true;
6555 else if (!strcmp ("mode", spec->name)
6556 || !strcmp ("format", spec->name)
6557 || !strcmp ("cleanup", spec->name))
6558 return true;
6559 else
6560 return targetm.attribute_takes_identifier_p (attr_id);
6563 /* Attribute handlers common to C front ends. */
6565 /* Handle a "packed" attribute; arguments as in
6566 struct attribute_spec.handler. */
6568 static tree
6569 handle_packed_decl_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6570 int ARG_UNUSED (flags), bool *no_add_attrs)
6572 if (TREE_CODE (*node) == FIELD_DECL)
6574 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6575 /* Still pack bitfields. */
6576 && ! DECL_INITIAL (*node))
6577 warning (OPT_Wattributes,
6578 "%qE attribute ignored for field of type %qT",
6579 name, TREE_TYPE (*node));
6580 else
6581 DECL_PACKED (*node) = 1;
6583 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6584 used for DECL_REGISTER. It wouldn't mean anything anyway.
6585 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6586 that changes what the typedef is typing. */
6587 else
6589 warning (OPT_Wattributes, "%qE attribute ignored", name);
6590 *no_add_attrs = true;
6593 return NULL_TREE;
6596 static tree
6597 handle_packed_type_attribute (tree *node, tree ARG_UNUSED (name),
6598 tree ARG_UNUSED (args),
6599 int flags, bool *ARG_UNUSED (no_add_attrs))
6601 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6602 *node = build_variant_type_copy (*node);
6603 TYPE_PACKED (*node) = 1;
6605 return NULL_TREE;
6608 /* Handle a "nocommon" attribute; arguments as in
6609 struct attribute_spec.handler. */
6611 static tree
6612 handle_nocommon_attribute (tree *node, tree name,
6613 tree ARG_UNUSED (args),
6614 int ARG_UNUSED (flags), bool *no_add_attrs)
6616 if (TREE_CODE (*node) == VAR_DECL)
6617 DECL_COMMON (*node) = 0;
6618 else
6620 warning (OPT_Wattributes, "%qE attribute ignored", name);
6621 *no_add_attrs = true;
6624 return NULL_TREE;
6627 /* Handle a "common" attribute; arguments as in
6628 struct attribute_spec.handler. */
6630 static tree
6631 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6632 int ARG_UNUSED (flags), bool *no_add_attrs)
6634 if (TREE_CODE (*node) == VAR_DECL)
6635 DECL_COMMON (*node) = 1;
6636 else
6638 warning (OPT_Wattributes, "%qE attribute ignored", name);
6639 *no_add_attrs = true;
6642 return NULL_TREE;
6645 /* Handle a "noreturn" attribute; arguments as in
6646 struct attribute_spec.handler. */
6648 static tree
6649 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6650 int ARG_UNUSED (flags), bool *no_add_attrs)
6652 tree type = TREE_TYPE (*node);
6654 /* See FIXME comment in c_common_attribute_table. */
6655 if (TREE_CODE (*node) == FUNCTION_DECL
6656 || objc_method_decl (TREE_CODE (*node)))
6657 TREE_THIS_VOLATILE (*node) = 1;
6658 else if (TREE_CODE (type) == POINTER_TYPE
6659 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6660 TREE_TYPE (*node)
6661 = (build_qualified_type
6662 (build_pointer_type
6663 (build_type_variant (TREE_TYPE (type),
6664 TYPE_READONLY (TREE_TYPE (type)), 1)),
6665 TYPE_QUALS (type)));
6666 else
6668 warning (OPT_Wattributes, "%qE attribute ignored", name);
6669 *no_add_attrs = true;
6672 return NULL_TREE;
6675 /* Handle a "hot" and attribute; arguments as in
6676 struct attribute_spec.handler. */
6678 static tree
6679 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6680 int ARG_UNUSED (flags), bool *no_add_attrs)
6682 if (TREE_CODE (*node) == FUNCTION_DECL
6683 || TREE_CODE (*node) == LABEL_DECL)
6685 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6687 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6688 "with attribute %qs", name, "cold");
6689 *no_add_attrs = true;
6691 /* Most of the rest of the hot processing is done later with
6692 lookup_attribute. */
6694 else
6696 warning (OPT_Wattributes, "%qE attribute ignored", name);
6697 *no_add_attrs = true;
6700 return NULL_TREE;
6703 /* Handle a "cold" and attribute; arguments as in
6704 struct attribute_spec.handler. */
6706 static tree
6707 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6708 int ARG_UNUSED (flags), bool *no_add_attrs)
6710 if (TREE_CODE (*node) == FUNCTION_DECL
6711 || TREE_CODE (*node) == LABEL_DECL)
6713 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6715 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6716 "with attribute %qs", name, "hot");
6717 *no_add_attrs = true;
6719 /* Most of the rest of the cold processing is done later with
6720 lookup_attribute. */
6722 else
6724 warning (OPT_Wattributes, "%qE attribute ignored", name);
6725 *no_add_attrs = true;
6728 return NULL_TREE;
6731 /* Handle a "no_sanitize_address" attribute; arguments as in
6732 struct attribute_spec.handler. */
6734 static tree
6735 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6736 bool *no_add_attrs)
6738 if (TREE_CODE (*node) != FUNCTION_DECL)
6740 warning (OPT_Wattributes, "%qE attribute ignored", name);
6741 *no_add_attrs = true;
6744 return NULL_TREE;
6747 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6748 struct attribute_spec.handler. */
6750 static tree
6751 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6752 bool *no_add_attrs)
6754 if (TREE_CODE (*node) != FUNCTION_DECL)
6755 warning (OPT_Wattributes, "%qE attribute ignored", name);
6756 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6757 DECL_ATTRIBUTES (*node)
6758 = tree_cons (get_identifier ("no_sanitize_address"),
6759 NULL_TREE, DECL_ATTRIBUTES (*node));
6760 *no_add_attrs = true;
6761 return NULL_TREE;
6764 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6765 struct attribute_spec.handler. */
6767 static tree
6768 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6769 bool *no_add_attrs)
6771 if (TREE_CODE (*node) != FUNCTION_DECL)
6773 warning (OPT_Wattributes, "%qE attribute ignored", name);
6774 *no_add_attrs = true;
6777 return NULL_TREE;
6780 /* Handle a "noinline" attribute; arguments as in
6781 struct attribute_spec.handler. */
6783 static tree
6784 handle_noinline_attribute (tree *node, tree name,
6785 tree ARG_UNUSED (args),
6786 int ARG_UNUSED (flags), bool *no_add_attrs)
6788 if (TREE_CODE (*node) == FUNCTION_DECL)
6790 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6792 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6793 "with attribute %qs", name, "always_inline");
6794 *no_add_attrs = true;
6796 else
6797 DECL_UNINLINABLE (*node) = 1;
6799 else
6801 warning (OPT_Wattributes, "%qE attribute ignored", name);
6802 *no_add_attrs = true;
6805 return NULL_TREE;
6808 /* Handle a "noclone" attribute; arguments as in
6809 struct attribute_spec.handler. */
6811 static tree
6812 handle_noclone_attribute (tree *node, tree name,
6813 tree ARG_UNUSED (args),
6814 int ARG_UNUSED (flags), bool *no_add_attrs)
6816 if (TREE_CODE (*node) != FUNCTION_DECL)
6818 warning (OPT_Wattributes, "%qE attribute ignored", name);
6819 *no_add_attrs = true;
6822 return NULL_TREE;
6825 /* Handle a "always_inline" attribute; arguments as in
6826 struct attribute_spec.handler. */
6828 static tree
6829 handle_always_inline_attribute (tree *node, tree name,
6830 tree ARG_UNUSED (args),
6831 int ARG_UNUSED (flags),
6832 bool *no_add_attrs)
6834 if (TREE_CODE (*node) == FUNCTION_DECL)
6836 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6838 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6839 "with %qs attribute", name, "noinline");
6840 *no_add_attrs = true;
6842 else
6843 /* Set the attribute and mark it for disregarding inline
6844 limits. */
6845 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6847 else
6849 warning (OPT_Wattributes, "%qE attribute ignored", name);
6850 *no_add_attrs = true;
6853 return NULL_TREE;
6856 /* Handle a "gnu_inline" attribute; arguments as in
6857 struct attribute_spec.handler. */
6859 static tree
6860 handle_gnu_inline_attribute (tree *node, tree name,
6861 tree ARG_UNUSED (args),
6862 int ARG_UNUSED (flags),
6863 bool *no_add_attrs)
6865 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6867 /* Do nothing else, just set the attribute. We'll get at
6868 it later with lookup_attribute. */
6870 else
6872 warning (OPT_Wattributes, "%qE attribute ignored", name);
6873 *no_add_attrs = true;
6876 return NULL_TREE;
6879 /* Handle a "leaf" attribute; arguments as in
6880 struct attribute_spec.handler. */
6882 static tree
6883 handle_leaf_attribute (tree *node, tree name,
6884 tree ARG_UNUSED (args),
6885 int ARG_UNUSED (flags), bool *no_add_attrs)
6887 if (TREE_CODE (*node) != FUNCTION_DECL)
6889 warning (OPT_Wattributes, "%qE attribute ignored", name);
6890 *no_add_attrs = true;
6892 if (!TREE_PUBLIC (*node))
6894 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6895 *no_add_attrs = true;
6898 return NULL_TREE;
6901 /* Handle an "artificial" attribute; arguments as in
6902 struct attribute_spec.handler. */
6904 static tree
6905 handle_artificial_attribute (tree *node, tree name,
6906 tree ARG_UNUSED (args),
6907 int ARG_UNUSED (flags),
6908 bool *no_add_attrs)
6910 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6912 /* Do nothing else, just set the attribute. We'll get at
6913 it later with lookup_attribute. */
6915 else
6917 warning (OPT_Wattributes, "%qE attribute ignored", name);
6918 *no_add_attrs = true;
6921 return NULL_TREE;
6924 /* Handle a "flatten" attribute; arguments as in
6925 struct attribute_spec.handler. */
6927 static tree
6928 handle_flatten_attribute (tree *node, tree name,
6929 tree args ATTRIBUTE_UNUSED,
6930 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6932 if (TREE_CODE (*node) == FUNCTION_DECL)
6933 /* Do nothing else, just set the attribute. We'll get at
6934 it later with lookup_attribute. */
6936 else
6938 warning (OPT_Wattributes, "%qE attribute ignored", name);
6939 *no_add_attrs = true;
6942 return NULL_TREE;
6945 /* Handle a "warning" or "error" attribute; arguments as in
6946 struct attribute_spec.handler. */
6948 static tree
6949 handle_error_attribute (tree *node, tree name, tree args,
6950 int ARG_UNUSED (flags), bool *no_add_attrs)
6952 if (TREE_CODE (*node) == FUNCTION_DECL
6953 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6954 /* Do nothing else, just set the attribute. We'll get at
6955 it later with lookup_attribute. */
6957 else
6959 warning (OPT_Wattributes, "%qE attribute ignored", name);
6960 *no_add_attrs = true;
6963 return NULL_TREE;
6966 /* Handle a "used" attribute; arguments as in
6967 struct attribute_spec.handler. */
6969 static tree
6970 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6971 int ARG_UNUSED (flags), bool *no_add_attrs)
6973 tree node = *pnode;
6975 if (TREE_CODE (node) == FUNCTION_DECL
6976 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6977 || (TREE_CODE (node) == TYPE_DECL))
6979 TREE_USED (node) = 1;
6980 DECL_PRESERVE_P (node) = 1;
6981 if (TREE_CODE (node) == VAR_DECL)
6982 DECL_READ_P (node) = 1;
6984 else
6986 warning (OPT_Wattributes, "%qE attribute ignored", name);
6987 *no_add_attrs = true;
6990 return NULL_TREE;
6993 /* Handle a "unused" attribute; arguments as in
6994 struct attribute_spec.handler. */
6995 static tree
6996 handle_unused_decl_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6997 int ARG_UNUSED (flags), bool *no_add_attrs)
6999 tree decl = *node;
7001 if (TREE_CODE (decl) == PARM_DECL
7002 || TREE_CODE (decl) == VAR_DECL
7003 || TREE_CODE (decl) == FUNCTION_DECL
7004 || TREE_CODE (decl) == LABEL_DECL
7005 || TREE_CODE (decl) == TYPE_DECL)
7007 TREE_USED (decl) = 1;
7008 if (TREE_CODE (decl) == VAR_DECL
7009 || TREE_CODE (decl) == PARM_DECL)
7010 DECL_READ_P (decl) = 1;
7012 else
7014 warning (OPT_Wattributes, "%qE attribute ignored", name);
7015 *no_add_attrs = true;
7017 return NULL_TREE;
7021 static tree
7022 handle_unused_type_attribute (tree *node, tree ARG_UNUSED (name),
7023 tree ARG_UNUSED (args), int flags,
7024 bool *ARG_UNUSED (no_add_attrs))
7026 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7027 *node = build_variant_type_copy (*node);
7028 TREE_USED (*node) = 1;
7030 return NULL_TREE;
7033 /* Handle a "externally_visible" attribute; arguments as in
7034 struct attribute_spec.handler. */
7036 static tree
7037 handle_externally_visible_attribute (tree *pnode, tree name,
7038 tree ARG_UNUSED (args),
7039 int ARG_UNUSED (flags),
7040 bool *no_add_attrs)
7042 tree node = *pnode;
7044 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7046 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7047 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7049 warning (OPT_Wattributes,
7050 "%qE attribute have effect only on public objects", name);
7051 *no_add_attrs = true;
7054 else
7056 warning (OPT_Wattributes, "%qE attribute ignored", name);
7057 *no_add_attrs = true;
7060 return NULL_TREE;
7063 /* Handle the "no_reorder" attribute. Arguments as in
7064 struct attribute_spec.handler. */
7066 static tree
7067 handle_no_reorder_attribute (tree *pnode,
7068 tree name,
7069 tree,
7070 int,
7071 bool *no_add_attrs)
7073 tree node = *pnode;
7075 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7076 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7078 warning (OPT_Wattributes,
7079 "%qE attribute only affects top level objects",
7080 name);
7081 *no_add_attrs = true;
7084 return NULL_TREE;
7087 /* Handle a "const" attribute; arguments as in
7088 struct attribute_spec.handler. */
7090 static tree
7091 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7092 int ARG_UNUSED (flags), bool *no_add_attrs)
7094 tree type = TREE_TYPE (*node);
7096 /* See FIXME comment on noreturn in c_common_attribute_table. */
7097 if (TREE_CODE (*node) == FUNCTION_DECL)
7098 TREE_READONLY (*node) = 1;
7099 else if (TREE_CODE (type) == POINTER_TYPE
7100 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7101 TREE_TYPE (*node)
7102 = (build_qualified_type
7103 (build_pointer_type
7104 (build_type_variant (TREE_TYPE (type), 1,
7105 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7106 TYPE_QUALS (type)));
7107 else
7109 warning (OPT_Wattributes, "%qE attribute ignored", name);
7110 *no_add_attrs = true;
7113 return NULL_TREE;
7116 /* Handle a "transparent_union" attribute; arguments as in
7117 struct attribute_spec.handler. */
7119 static tree
7120 handle_transparent_union_type_attribute (tree *node, tree name,
7121 tree ARG_UNUSED (args), int flags,
7122 bool *no_add_attrs)
7124 tree type = *node;
7125 *no_add_attrs = true;
7127 if (TREE_CODE (type) == UNION_TYPE)
7129 /* Make sure that the first field will work for a transparent union.
7130 If the type isn't complete yet, leave the check to the code in
7131 finish_struct. */
7132 if (TYPE_SIZE (type))
7134 tree first = first_field (type);
7135 if (first == NULL_TREE
7136 || DECL_ARTIFICIAL (first)
7137 || TYPE_MODE (type) != DECL_MODE (first))
7138 goto ignored;
7141 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7143 /* If the type isn't complete yet, setting the flag
7144 on a variant wouldn't ever be checked. */
7145 if (!TYPE_SIZE (type))
7146 goto ignored;
7148 /* build_duplicate_type doesn't work for C++. */
7149 if (c_dialect_cxx ())
7150 goto ignored;
7152 /* A type variant isn't good enough, since we don't a cast
7153 to such a type removed as a no-op. */
7154 *node = type = build_duplicate_type (type);
7157 TYPE_TRANSPARENT_AGGR (type) = 1;
7158 return NULL_TREE;
7161 ignored:
7162 warning (OPT_Wattributes, "%qE attribute ignored", name);
7163 return NULL_TREE;
7167 static tree
7168 handle_transparent_union_decl_attribute (tree *node, tree name, tree args,
7169 int flags, bool *no_add_attrs)
7171 *no_add_attrs = true;
7173 if (TREE_CODE (*node) == TYPE_DECL && !(flags & ATTR_FLAG_CXX11))
7174 return handle_transparent_union_type_attribute (&TREE_TYPE (*node), name,
7175 args, flags, no_add_attrs);
7177 warning (OPT_Wattributes, "%qE attribute ignored", name);
7178 return NULL_TREE;
7181 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7182 get the requested priority for a constructor or destructor,
7183 possibly issuing diagnostics for invalid or reserved
7184 priorities. */
7186 static priority_type
7187 get_priority (tree args, bool is_destructor)
7189 HOST_WIDE_INT pri;
7190 tree arg;
7192 if (!args)
7193 return DEFAULT_INIT_PRIORITY;
7195 if (!SUPPORTS_INIT_PRIORITY)
7197 if (is_destructor)
7198 error ("destructor priorities are not supported");
7199 else
7200 error ("constructor priorities are not supported");
7201 return DEFAULT_INIT_PRIORITY;
7204 arg = TREE_VALUE (args);
7205 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7206 goto invalid;
7207 if (arg == error_mark_node)
7208 return DEFAULT_INIT_PRIORITY;
7209 arg = default_conversion (arg);
7210 if (!tree_fits_shwi_p (arg)
7211 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7212 goto invalid;
7214 pri = tree_to_shwi (arg);
7215 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7216 goto invalid;
7218 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7220 if (is_destructor)
7221 warning (0,
7222 "destructor priorities from 0 to %d are reserved "
7223 "for the implementation",
7224 MAX_RESERVED_INIT_PRIORITY);
7225 else
7226 warning (0,
7227 "constructor priorities from 0 to %d are reserved "
7228 "for the implementation",
7229 MAX_RESERVED_INIT_PRIORITY);
7231 return pri;
7233 invalid:
7234 if (is_destructor)
7235 error ("destructor priorities must be integers from 0 to %d inclusive",
7236 MAX_INIT_PRIORITY);
7237 else
7238 error ("constructor priorities must be integers from 0 to %d inclusive",
7239 MAX_INIT_PRIORITY);
7240 return DEFAULT_INIT_PRIORITY;
7243 /* Handle a "constructor" attribute; arguments as in
7244 struct attribute_spec.handler. */
7246 static tree
7247 handle_constructor_attribute (tree *node, tree name, tree args,
7248 int ARG_UNUSED (flags),
7249 bool *no_add_attrs)
7251 tree decl = *node;
7252 tree type = TREE_TYPE (decl);
7254 if (TREE_CODE (decl) == FUNCTION_DECL
7255 && TREE_CODE (type) == FUNCTION_TYPE
7256 && decl_function_context (decl) == 0)
7258 priority_type priority;
7259 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7260 priority = get_priority (args, /*is_destructor=*/false);
7261 SET_DECL_INIT_PRIORITY (decl, priority);
7262 TREE_USED (decl) = 1;
7264 else
7266 warning (OPT_Wattributes, "%qE attribute ignored", name);
7267 *no_add_attrs = true;
7270 return NULL_TREE;
7273 /* Handle a "destructor" attribute; arguments as in
7274 struct attribute_spec.handler. */
7276 static tree
7277 handle_destructor_attribute (tree *node, tree name, tree args,
7278 int ARG_UNUSED (flags),
7279 bool *no_add_attrs)
7281 tree decl = *node;
7282 tree type = TREE_TYPE (decl);
7284 if (TREE_CODE (decl) == FUNCTION_DECL
7285 && TREE_CODE (type) == FUNCTION_TYPE
7286 && decl_function_context (decl) == 0)
7288 priority_type priority;
7289 DECL_STATIC_DESTRUCTOR (decl) = 1;
7290 priority = get_priority (args, /*is_destructor=*/true);
7291 SET_DECL_FINI_PRIORITY (decl, priority);
7292 TREE_USED (decl) = 1;
7294 else
7296 warning (OPT_Wattributes, "%qE attribute ignored", name);
7297 *no_add_attrs = true;
7300 return NULL_TREE;
7303 /* Nonzero if the mode is a valid vector mode for this architecture.
7304 This returns nonzero even if there is no hardware support for the
7305 vector mode, but we can emulate with narrower modes. */
7307 static int
7308 vector_mode_valid_p (machine_mode mode)
7310 enum mode_class mclass = GET_MODE_CLASS (mode);
7311 machine_mode innermode;
7313 /* Doh! What's going on? */
7314 if (mclass != MODE_VECTOR_INT
7315 && mclass != MODE_VECTOR_FLOAT
7316 && mclass != MODE_VECTOR_FRACT
7317 && mclass != MODE_VECTOR_UFRACT
7318 && mclass != MODE_VECTOR_ACCUM
7319 && mclass != MODE_VECTOR_UACCUM)
7320 return 0;
7322 /* Hardware support. Woo hoo! */
7323 if (targetm.vector_mode_supported_p (mode))
7324 return 1;
7326 innermode = GET_MODE_INNER (mode);
7328 /* We should probably return 1 if requesting V4DI and we have no DI,
7329 but we have V2DI, but this is probably very unlikely. */
7331 /* If we have support for the inner mode, we can safely emulate it.
7332 We may not have V2DI, but me can emulate with a pair of DIs. */
7333 return targetm.scalar_mode_supported_p (innermode);
7337 /* Handle a "mode" attribute; arguments as in
7338 struct attribute_spec.handler. */
7340 static tree
7341 handle_mode_attribute (tree *node, tree name, tree args,
7342 int ARG_UNUSED (flags), bool *no_add_attrs)
7344 tree type = *node;
7345 tree ident = TREE_VALUE (args);
7347 *no_add_attrs = true;
7349 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7350 warning (OPT_Wattributes, "%qE attribute ignored", name);
7351 else
7353 int j;
7354 const char *p = IDENTIFIER_POINTER (ident);
7355 int len = strlen (p);
7356 machine_mode mode = VOIDmode;
7357 tree typefm;
7358 bool valid_mode;
7360 if (len > 4 && p[0] == '_' && p[1] == '_'
7361 && p[len - 1] == '_' && p[len - 2] == '_')
7363 char *newp = (char *) alloca (len - 1);
7365 strcpy (newp, &p[2]);
7366 newp[len - 4] = '\0';
7367 p = newp;
7370 /* Change this type to have a type with the specified mode.
7371 First check for the special modes. */
7372 if (!strcmp (p, "byte"))
7373 mode = byte_mode;
7374 else if (!strcmp (p, "word"))
7375 mode = word_mode;
7376 else if (!strcmp (p, "pointer"))
7377 mode = ptr_mode;
7378 else if (!strcmp (p, "libgcc_cmp_return"))
7379 mode = targetm.libgcc_cmp_return_mode ();
7380 else if (!strcmp (p, "libgcc_shift_count"))
7381 mode = targetm.libgcc_shift_count_mode ();
7382 else if (!strcmp (p, "unwind_word"))
7383 mode = targetm.unwind_word_mode ();
7384 else
7385 for (j = 0; j < NUM_MACHINE_MODES; j++)
7386 if (!strcmp (p, GET_MODE_NAME (j)))
7388 mode = (machine_mode) j;
7389 break;
7392 if (mode == VOIDmode)
7394 error ("unknown machine mode %qE", ident);
7395 return NULL_TREE;
7398 valid_mode = false;
7399 switch (GET_MODE_CLASS (mode))
7401 case MODE_INT:
7402 case MODE_PARTIAL_INT:
7403 case MODE_FLOAT:
7404 case MODE_DECIMAL_FLOAT:
7405 case MODE_FRACT:
7406 case MODE_UFRACT:
7407 case MODE_ACCUM:
7408 case MODE_UACCUM:
7409 valid_mode = targetm.scalar_mode_supported_p (mode);
7410 break;
7412 case MODE_COMPLEX_INT:
7413 case MODE_COMPLEX_FLOAT:
7414 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7415 break;
7417 case MODE_VECTOR_INT:
7418 case MODE_VECTOR_FLOAT:
7419 case MODE_VECTOR_FRACT:
7420 case MODE_VECTOR_UFRACT:
7421 case MODE_VECTOR_ACCUM:
7422 case MODE_VECTOR_UACCUM:
7423 warning (OPT_Wattributes, "specifying vector types with "
7424 "__attribute__ ((mode)) is deprecated");
7425 warning (OPT_Wattributes,
7426 "use __attribute__ ((vector_size)) instead");
7427 valid_mode = vector_mode_valid_p (mode);
7428 break;
7430 default:
7431 break;
7433 if (!valid_mode)
7435 error ("unable to emulate %qs", p);
7436 return NULL_TREE;
7439 if (POINTER_TYPE_P (type))
7441 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7442 tree (*fn)(tree, machine_mode, bool);
7444 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7446 error ("invalid pointer mode %qs", p);
7447 return NULL_TREE;
7450 if (TREE_CODE (type) == POINTER_TYPE)
7451 fn = build_pointer_type_for_mode;
7452 else
7453 fn = build_reference_type_for_mode;
7454 typefm = fn (TREE_TYPE (type), mode, false);
7456 else
7458 /* For fixed-point modes, we need to test if the signness of type
7459 and the machine mode are consistent. */
7460 if (ALL_FIXED_POINT_MODE_P (mode)
7461 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7463 error ("signedness of type and machine mode %qs don%'t match", p);
7464 return NULL_TREE;
7466 /* For fixed-point modes, we need to pass saturating info. */
7467 typefm = lang_hooks.types.type_for_mode (mode,
7468 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7469 : TYPE_UNSIGNED (type));
7472 if (typefm == NULL_TREE)
7474 error ("no data type for mode %qs", p);
7475 return NULL_TREE;
7477 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7479 /* For enumeral types, copy the precision from the integer
7480 type returned above. If not an INTEGER_TYPE, we can't use
7481 this mode for this type. */
7482 if (TREE_CODE (typefm) != INTEGER_TYPE)
7484 error ("cannot use mode %qs for enumeral types", p);
7485 return NULL_TREE;
7488 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7490 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7491 typefm = type;
7493 else
7495 /* We cannot build a type variant, as there's code that assumes
7496 that TYPE_MAIN_VARIANT has the same mode. This includes the
7497 debug generators. Instead, create a subrange type. This
7498 results in all of the enumeral values being emitted only once
7499 in the original, and the subtype gets them by reference. */
7500 if (TYPE_UNSIGNED (type))
7501 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7502 else
7503 typefm = make_signed_type (TYPE_PRECISION (typefm));
7504 TREE_TYPE (typefm) = type;
7507 else if (VECTOR_MODE_P (mode)
7508 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7509 : TREE_CODE (type) != TREE_CODE (typefm))
7511 error ("mode %qs applied to inappropriate type", p);
7512 return NULL_TREE;
7515 *node = typefm;
7518 return NULL_TREE;
7521 /* Handle a "section" attribute; arguments as in
7522 struct attribute_spec.handler. */
7524 static tree
7525 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7526 int ARG_UNUSED (flags), bool *no_add_attrs)
7528 tree decl = *node;
7530 if (targetm_common.have_named_sections)
7532 user_defined_section_attribute = true;
7534 if ((TREE_CODE (decl) == FUNCTION_DECL
7535 || TREE_CODE (decl) == VAR_DECL)
7536 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7538 if (TREE_CODE (decl) == VAR_DECL
7539 && current_function_decl != NULL_TREE
7540 && !TREE_STATIC (decl))
7542 error_at (DECL_SOURCE_LOCATION (decl),
7543 "section attribute cannot be specified for "
7544 "local variables");
7545 *no_add_attrs = true;
7548 /* The decl may have already been given a section attribute
7549 from a previous declaration. Ensure they match. */
7550 else if (DECL_SECTION_NAME (decl) != NULL
7551 && strcmp (DECL_SECTION_NAME (decl),
7552 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7554 error ("section of %q+D conflicts with previous declaration",
7555 *node);
7556 *no_add_attrs = true;
7558 else if (TREE_CODE (decl) == VAR_DECL
7559 && !targetm.have_tls && targetm.emutls.tmpl_section
7560 && DECL_THREAD_LOCAL_P (decl))
7562 error ("section of %q+D cannot be overridden", *node);
7563 *no_add_attrs = true;
7565 else
7566 set_decl_section_name (decl,
7567 TREE_STRING_POINTER (TREE_VALUE (args)));
7569 else
7571 error ("section attribute not allowed for %q+D", *node);
7572 *no_add_attrs = true;
7575 else
7577 error_at (DECL_SOURCE_LOCATION (*node),
7578 "section attributes are not supported for this target");
7579 *no_add_attrs = true;
7582 return NULL_TREE;
7585 /* Check whether ALIGN is a valid user-specified alignment. If so,
7586 return its base-2 log; if not, output an error and return -1. If
7587 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7588 no error. */
7590 check_user_alignment (const_tree align, bool allow_zero)
7592 int i;
7594 if (error_operand_p (align))
7595 return -1;
7596 if (TREE_CODE (align) != INTEGER_CST
7597 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7599 error ("requested alignment is not an integer constant");
7600 return -1;
7602 else if (allow_zero && integer_zerop (align))
7603 return -1;
7604 else if (tree_int_cst_sgn (align) == -1
7605 || (i = tree_log2 (align)) == -1)
7607 error ("requested alignment is not a positive power of 2");
7608 return -1;
7610 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7612 error ("requested alignment is too large");
7613 return -1;
7615 return i;
7619 If in c++-11, check if the c++-11 alignment constraint with respect
7620 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7621 c++-11 mode, does nothing.
7623 [dcl.align]2/ says:
7625 [* if the constant expression evaluates to a fundamental alignment,
7626 the alignment requirement of the declared entity shall be the
7627 specified fundamental alignment.
7629 * if the constant expression evaluates to an extended alignment
7630 and the implementation supports that alignment in the context
7631 of the declaration, the alignment of the declared entity shall
7632 be that alignment
7634 * if the constant expression evaluates to an extended alignment
7635 and the implementation does not support that alignment in the
7636 context of the declaration, the program is ill-formed]. */
7638 static bool
7639 check_cxx_fundamental_alignment_constraints (tree node,
7640 unsigned align_log,
7641 int flags)
7643 bool alignment_too_large_p = false;
7644 unsigned requested_alignment = 1U << align_log;
7645 unsigned max_align = 0;
7647 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7648 || (node == NULL_TREE || node == error_mark_node))
7649 return true;
7651 if (cxx_fundamental_alignment_p (requested_alignment))
7652 return true;
7654 if (DECL_P (node))
7656 if (TREE_STATIC (node))
7658 /* For file scope variables and static members, the target
7659 supports alignments that are at most
7660 MAX_OFILE_ALIGNMENT. */
7661 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7662 alignment_too_large_p = true;
7664 else
7666 #ifdef BIGGEST_FIELD_ALIGNMENT
7667 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7668 #else
7669 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7670 #endif
7671 /* For non-static members, the target supports either
7672 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7673 if it is defined or BIGGEST_ALIGNMENT. */
7674 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7675 if (TREE_CODE (node) == FIELD_DECL
7676 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7677 alignment_too_large_p = true;
7678 #undef MAX_TARGET_FIELD_ALIGNMENT
7679 /* For stack variables, the target supports at most
7680 MAX_STACK_ALIGNMENT. */
7681 else if (decl_function_context (node) != NULL
7682 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7683 alignment_too_large_p = true;
7686 else if (TYPE_P (node))
7688 /* Let's be liberal for types. */
7689 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7690 alignment_too_large_p = true;
7693 if (alignment_too_large_p)
7694 pedwarn (input_location, OPT_Wattributes,
7695 "requested alignment %d is larger than %d",
7696 requested_alignment, max_align);
7698 return !alignment_too_large_p;
7701 /* Return the alignment expression from tree_list element ARGS. */
7703 static tree
7704 get_align_expr (tree args)
7706 tree align_expr;
7708 if (args)
7710 align_expr = TREE_VALUE (args);
7711 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7712 && TREE_CODE (align_expr) != FUNCTION_DECL)
7713 align_expr = default_conversion (align_expr);
7715 else
7716 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7718 return align_expr;
7721 /* Set the type alignment fields of NODE, based on FLAGS to I. */
7723 static void
7724 handle_aligned_type (tree *node, int flags, int i)
7726 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7727 *node = build_variant_type_copy (*node);
7729 TYPE_ALIGN (*node) = (1U << i) * BITS_PER_UNIT;
7730 TYPE_USER_ALIGN (*node) = 1;
7733 /* Handle a "aligned" attribute; arguments as in
7734 struct attribute_spec.handler. */
7736 static tree
7737 handle_aligned_type_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7738 int flags, bool *no_add_attrs)
7740 tree align_expr;
7741 int i;
7743 align_expr = get_align_expr (args);
7745 if ((i = check_user_alignment (align_expr, false)) == -1
7746 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7747 *no_add_attrs = true;
7748 else
7749 handle_aligned_type (node, flags, i);
7751 return NULL_TREE;
7755 static tree
7756 handle_aligned_decl_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7757 int flags, bool *no_add_attrs)
7759 tree align_expr;
7760 tree decl = *node;
7761 int i;
7763 align_expr = get_align_expr (args);
7765 if ((i = check_user_alignment (align_expr, false)) == -1
7766 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7768 *no_add_attrs = true;
7770 else if (TREE_CODE (*node) == TYPE_DECL)
7771 handle_aligned_type (&TREE_TYPE (*node), flags, i);
7772 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7773 && TREE_CODE (decl) != FIELD_DECL)
7775 error ("alignment may not be specified for %q+D", decl);
7776 *no_add_attrs = true;
7778 else if (DECL_USER_ALIGN (decl)
7779 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7780 /* C++-11 [dcl.align/4]:
7782 When multiple alignment-specifiers are specified for an
7783 entity, the alignment requirement shall be set to the
7784 strictest specified alignment.
7786 This formally comes from the c++11 specification but we are
7787 doing it for the GNU attribute syntax as well. */
7788 *no_add_attrs = true;
7789 else if (TREE_CODE (decl) == FUNCTION_DECL
7790 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7792 if (DECL_USER_ALIGN (decl))
7793 error ("alignment for %q+D was previously specified as %d "
7794 "and may not be decreased", decl,
7795 DECL_ALIGN (decl) / BITS_PER_UNIT);
7796 else
7797 error ("alignment for %q+D must be at least %d", decl,
7798 DECL_ALIGN (decl) / BITS_PER_UNIT);
7799 *no_add_attrs = true;
7801 else
7803 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7804 DECL_USER_ALIGN (decl) = 1;
7807 return NULL_TREE;
7810 /* Handle a "weak" attribute; arguments as in
7811 struct attribute_spec.handler. */
7813 static tree
7814 handle_weak_attribute (tree *node, tree name,
7815 tree ARG_UNUSED (args),
7816 int ARG_UNUSED (flags),
7817 bool * ARG_UNUSED (no_add_attrs))
7819 if (TREE_CODE (*node) == FUNCTION_DECL
7820 && DECL_DECLARED_INLINE_P (*node))
7822 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7823 *no_add_attrs = true;
7825 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7827 error ("indirect function %q+D cannot be declared weak", *node);
7828 *no_add_attrs = true;
7829 return NULL_TREE;
7831 else if (TREE_CODE (*node) == FUNCTION_DECL
7832 || TREE_CODE (*node) == VAR_DECL)
7834 struct symtab_node *n = symtab_node::get (*node);
7835 if (n && n->refuse_visibility_changes)
7836 error ("%+D declared weak after being used", *node);
7837 declare_weak (*node);
7839 else
7840 warning (OPT_Wattributes, "%qE attribute ignored", name);
7842 return NULL_TREE;
7845 /* Handle an "alias" or "ifunc" attribute; arguments as in
7846 struct attribute_spec.handler, except that IS_ALIAS tells us
7847 whether this is an alias as opposed to ifunc attribute. */
7849 static tree
7850 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7851 bool *no_add_attrs)
7853 tree decl = *node;
7855 if (TREE_CODE (decl) != FUNCTION_DECL
7856 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7858 warning (OPT_Wattributes, "%qE attribute ignored", name);
7859 *no_add_attrs = true;
7861 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7862 || (TREE_CODE (decl) != FUNCTION_DECL
7863 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7864 /* A static variable declaration is always a tentative definition,
7865 but the alias is a non-tentative definition which overrides. */
7866 || (TREE_CODE (decl) != FUNCTION_DECL
7867 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7869 error ("%q+D defined both normally and as %qE attribute", decl, name);
7870 *no_add_attrs = true;
7871 return NULL_TREE;
7873 else if (!is_alias
7874 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7875 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7877 error ("weak %q+D cannot be defined %qE", decl, name);
7878 *no_add_attrs = true;
7879 return NULL_TREE;
7882 /* Note that the very first time we process a nested declaration,
7883 decl_function_context will not be set. Indeed, *would* never
7884 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7885 we do below. After such frobbery, pushdecl would set the context.
7886 In any case, this is never what we want. */
7887 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7889 tree id;
7891 id = TREE_VALUE (args);
7892 if (TREE_CODE (id) != STRING_CST)
7894 error ("attribute %qE argument not a string", name);
7895 *no_add_attrs = true;
7896 return NULL_TREE;
7898 id = get_identifier (TREE_STRING_POINTER (id));
7899 /* This counts as a use of the object pointed to. */
7900 TREE_USED (id) = 1;
7902 if (TREE_CODE (decl) == FUNCTION_DECL)
7903 DECL_INITIAL (decl) = error_mark_node;
7904 else
7905 TREE_STATIC (decl) = 1;
7907 if (!is_alias)
7908 /* ifuncs are also aliases, so set that attribute too. */
7909 DECL_ATTRIBUTES (decl)
7910 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7912 else
7914 warning (OPT_Wattributes, "%qE attribute ignored", name);
7915 *no_add_attrs = true;
7918 if (decl_in_symtab_p (*node))
7920 struct symtab_node *n = symtab_node::get (decl);
7921 if (n && n->refuse_visibility_changes)
7923 if (is_alias)
7924 error ("%+D declared alias after being used", decl);
7925 else
7926 error ("%+D declared ifunc after being used", decl);
7931 return NULL_TREE;
7934 /* Handle an "alias" or "ifunc" attribute; arguments as in
7935 struct attribute_spec.handler. */
7937 static tree
7938 handle_ifunc_attribute (tree *node, tree name, tree args,
7939 int ARG_UNUSED (flags), bool *no_add_attrs)
7941 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7944 /* Handle an "alias" or "ifunc" attribute; arguments as in
7945 struct attribute_spec.handler. */
7947 static tree
7948 handle_alias_attribute (tree *node, tree name, tree args,
7949 int ARG_UNUSED (flags), bool *no_add_attrs)
7951 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7954 /* Handle a "weakref" attribute; arguments as in struct
7955 attribute_spec.handler. */
7957 static tree
7958 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7959 int flags, bool *no_add_attrs)
7961 tree attr = NULL_TREE;
7963 /* We must ignore the attribute when it is associated with
7964 local-scoped decls, since attribute alias is ignored and many
7965 such symbols do not even have a DECL_WEAK field. */
7966 if (decl_function_context (*node)
7967 || current_function_decl
7968 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7970 warning (OPT_Wattributes, "%qE attribute ignored", name);
7971 *no_add_attrs = true;
7972 return NULL_TREE;
7975 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7977 error ("indirect function %q+D cannot be declared weakref", *node);
7978 *no_add_attrs = true;
7979 return NULL_TREE;
7982 /* The idea here is that `weakref("name")' mutates into `weakref,
7983 alias("name")', and weakref without arguments, in turn,
7984 implicitly adds weak. */
7986 if (args)
7988 attr = tree_cons (get_identifier ("alias"), args, attr);
7989 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7991 *no_add_attrs = true;
7993 decl_attributes (node, attr, flags);
7995 else
7997 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7998 error_at (DECL_SOURCE_LOCATION (*node),
7999 "weakref attribute must appear before alias attribute");
8001 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8002 and that isn't supported; and because it wants to add it to
8003 the list of weak decls, which isn't helpful. */
8004 DECL_WEAK (*node) = 1;
8007 if (decl_in_symtab_p (*node))
8009 struct symtab_node *n = symtab_node::get (*node);
8010 if (n && n->refuse_visibility_changes)
8011 error ("%+D declared weakref after being used", *node);
8014 return NULL_TREE;
8017 /* Process the DECL common parts of handle_visibility_attribute. */
8019 static void
8020 handle_decl_visibility (tree decl, tree id, tree attributes)
8022 enum symbol_visibility vis;
8024 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8025 vis = VISIBILITY_DEFAULT;
8026 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8027 vis = VISIBILITY_INTERNAL;
8028 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8029 vis = VISIBILITY_HIDDEN;
8030 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8031 vis = VISIBILITY_PROTECTED;
8032 else
8034 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8035 vis = VISIBILITY_DEFAULT;
8038 if (DECL_VISIBILITY_SPECIFIED (decl)
8039 && vis != DECL_VISIBILITY (decl))
8041 if (lookup_attribute ("visibility", attributes))
8042 error ("%qD redeclared with different visibility", decl);
8043 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8044 && lookup_attribute ("dllimport", attributes))
8045 error ("%qD was declared %qs which implies default visibility",
8046 decl, "dllimport");
8047 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8048 && lookup_attribute ("dllexport", attributes))
8049 error ("%qD was declared %qs which implies default visibility",
8050 decl, "dllexport");
8053 DECL_VISIBILITY (decl) = vis;
8054 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8057 /* Handle an "visibility" attribute; arguments as in
8058 struct attribute_spec.handler. */
8059 static tree
8060 handle_visibility_decl_attribute (tree *node, tree name, tree args,
8061 int ARG_UNUSED (flags),
8062 bool *ARG_UNUSED (no_add_attrs))
8064 tree decl = *node;
8065 tree id = TREE_VALUE (args);
8067 if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8069 warning (OPT_Wattributes, "%qE attribute ignored", name);
8070 return NULL_TREE;
8073 if (TREE_CODE (id) != STRING_CST)
8075 error ("visibility argument not a string");
8076 return NULL_TREE;
8079 handle_decl_visibility (decl, id, DECL_ATTRIBUTES (decl));
8080 /* Go ahead and attach the attribute to the node as well. This is needed
8081 so we can determine whether we have VISIBILITY_DEFAULT because the
8082 visibility was not specified, or because it was explicitly overridden
8083 from the containing scope. */
8085 return NULL_TREE;
8089 static tree
8090 handle_visibility_type_attribute (tree *node, tree name, tree args,
8091 int ARG_UNUSED (flags),
8092 bool *ARG_UNUSED (no_add_attrs))
8094 tree decl;
8095 tree id = TREE_VALUE (args);
8097 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8098 /* OK */;
8099 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8101 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8102 name);
8103 return NULL_TREE;
8105 else if (TYPE_FIELDS (*node))
8107 error ("%qE attribute ignored because %qT is already defined",
8108 name, *node);
8109 return NULL_TREE;
8112 if (TREE_CODE (id) != STRING_CST)
8114 error ("visibility argument not a string");
8115 return NULL_TREE;
8118 decl = TYPE_NAME (*node);
8119 if (!decl)
8120 return NULL_TREE;
8122 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8124 warning (OPT_Wattributes, "%qE attribute ignored on types",
8125 name);
8126 return NULL_TREE;
8129 handle_decl_visibility (decl, id, TYPE_ATTRIBUTES (*node));
8131 /* Go ahead and attach the attribute to the node as well. This is needed
8132 so we can determine whether we have VISIBILITY_DEFAULT because the
8133 visibility was not specified, or because it was explicitly overridden
8134 from the containing scope. */
8136 return NULL_TREE;
8139 /* Determine the ELF symbol visibility for DECL, which is either a
8140 variable or a function. It is an error to use this function if a
8141 definition of DECL is not available in this translation unit.
8142 Returns true if the final visibility has been determined by this
8143 function; false if the caller is free to make additional
8144 modifications. */
8146 bool
8147 c_determine_visibility (tree decl)
8149 gcc_assert (TREE_CODE (decl) == VAR_DECL
8150 || TREE_CODE (decl) == FUNCTION_DECL);
8152 /* If the user explicitly specified the visibility with an
8153 attribute, honor that. DECL_VISIBILITY will have been set during
8154 the processing of the attribute. We check for an explicit
8155 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8156 to distinguish the use of an attribute from the use of a "#pragma
8157 GCC visibility push(...)"; in the latter case we still want other
8158 considerations to be able to overrule the #pragma. */
8159 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8160 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8161 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8162 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8163 return true;
8165 /* Set default visibility to whatever the user supplied with
8166 visibility_specified depending on #pragma GCC visibility. */
8167 if (!DECL_VISIBILITY_SPECIFIED (decl))
8169 if (visibility_options.inpragma
8170 || DECL_VISIBILITY (decl) != default_visibility)
8172 DECL_VISIBILITY (decl) = default_visibility;
8173 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8174 /* If visibility changed and DECL already has DECL_RTL, ensure
8175 symbol flags are updated. */
8176 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8177 || TREE_CODE (decl) == FUNCTION_DECL)
8178 && DECL_RTL_SET_P (decl))
8179 make_decl_rtl (decl);
8182 return false;
8185 /* Handle an "tls_model" attribute; arguments as in
8186 struct attribute_spec.handler. */
8188 static tree
8189 handle_tls_model_attribute (tree *node, tree name, tree args,
8190 int ARG_UNUSED (flags), bool *no_add_attrs)
8192 tree id;
8193 tree decl = *node;
8194 enum tls_model kind;
8196 *no_add_attrs = true;
8198 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8200 warning (OPT_Wattributes, "%qE attribute ignored", name);
8201 return NULL_TREE;
8204 kind = DECL_TLS_MODEL (decl);
8205 id = TREE_VALUE (args);
8206 if (TREE_CODE (id) != STRING_CST)
8208 error ("tls_model argument not a string");
8209 return NULL_TREE;
8212 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8213 kind = TLS_MODEL_LOCAL_EXEC;
8214 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8215 kind = TLS_MODEL_INITIAL_EXEC;
8216 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8217 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8218 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8219 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8220 else
8221 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8223 set_decl_tls_model (decl, kind);
8224 return NULL_TREE;
8227 /* Handle a "no_instrument_function" attribute; arguments as in
8228 struct attribute_spec.handler. */
8230 static tree
8231 handle_no_instrument_function_attribute (tree *node, tree name,
8232 tree ARG_UNUSED (args),
8233 int ARG_UNUSED (flags),
8234 bool *no_add_attrs)
8236 tree decl = *node;
8238 if (TREE_CODE (decl) != FUNCTION_DECL)
8240 error_at (DECL_SOURCE_LOCATION (decl),
8241 "%qE attribute applies only to functions", name);
8242 *no_add_attrs = true;
8244 else
8245 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8247 return NULL_TREE;
8250 /* Handle a "malloc" attribute; arguments as in
8251 struct attribute_spec.handler. */
8253 static tree
8254 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8255 int ARG_UNUSED (flags), bool *no_add_attrs)
8257 if (TREE_CODE (*node) == FUNCTION_DECL
8258 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8259 DECL_IS_MALLOC (*node) = 1;
8260 else
8262 warning (OPT_Wattributes, "%qE attribute ignored", name);
8263 *no_add_attrs = true;
8266 return NULL_TREE;
8269 /* Handle a "alloc_size" attribute; arguments as in
8270 struct attribute_spec.handler. */
8272 static tree
8273 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8274 int ARG_UNUSED (flags), bool *no_add_attrs)
8276 unsigned arg_count = type_num_arguments (*node);
8277 for (; args; args = TREE_CHAIN (args))
8279 tree position = TREE_VALUE (args);
8280 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8281 && TREE_CODE (position) != FUNCTION_DECL)
8282 position = default_conversion (position);
8284 if (!tree_fits_uhwi_p (position)
8285 || !arg_count
8286 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8288 warning (OPT_Wattributes,
8289 "alloc_size parameter outside range");
8290 *no_add_attrs = true;
8291 return NULL_TREE;
8294 return NULL_TREE;
8297 /* Handle a "alloc_align" attribute; arguments as in
8298 struct attribute_spec.handler. */
8300 static tree
8301 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8302 bool *no_add_attrs)
8304 unsigned arg_count = type_num_arguments (*node);
8305 tree position = TREE_VALUE (args);
8306 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8307 position = default_conversion (position);
8309 if (!tree_fits_uhwi_p (position)
8310 || !arg_count
8311 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8313 warning (OPT_Wattributes,
8314 "alloc_align parameter outside range");
8315 *no_add_attrs = true;
8316 return NULL_TREE;
8318 return NULL_TREE;
8321 /* Handle a "assume_aligned" attribute; arguments as in
8322 struct attribute_spec.handler. */
8324 static tree
8325 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8326 bool *no_add_attrs)
8328 for (; args; args = TREE_CHAIN (args))
8330 tree position = TREE_VALUE (args);
8331 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8332 && TREE_CODE (position) != FUNCTION_DECL)
8333 position = default_conversion (position);
8335 if (TREE_CODE (position) != INTEGER_CST)
8337 warning (OPT_Wattributes,
8338 "assume_aligned parameter not integer constant");
8339 *no_add_attrs = true;
8340 return NULL_TREE;
8343 return NULL_TREE;
8346 /* Handle a "fn spec" attribute; arguments as in
8347 struct attribute_spec.handler. */
8349 static tree
8350 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8351 tree args, int ARG_UNUSED (flags),
8352 bool *no_add_attrs ATTRIBUTE_UNUSED)
8354 gcc_assert (TYPE_P (*node) && args
8355 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8356 && !TREE_CHAIN (args));
8357 return NULL_TREE;
8360 /* Handle a "bnd_variable_size" attribute; arguments as in
8361 struct attribute_spec.handler. */
8363 static tree
8364 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8365 int ARG_UNUSED (flags), bool *no_add_attrs)
8367 if (TREE_CODE (*node) != FIELD_DECL)
8369 warning (OPT_Wattributes, "%qE attribute ignored", name);
8370 *no_add_attrs = true;
8373 return NULL_TREE;
8376 /* Handle a "bnd_legacy" attribute; arguments as in
8377 struct attribute_spec.handler. */
8379 static tree
8380 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8381 int ARG_UNUSED (flags), bool *no_add_attrs)
8383 if (TREE_CODE (*node) != FUNCTION_DECL)
8385 warning (OPT_Wattributes, "%qE attribute ignored", name);
8386 *no_add_attrs = true;
8389 return NULL_TREE;
8392 /* Handle a "bnd_instrument" attribute; arguments as in
8393 struct attribute_spec.handler. */
8395 static tree
8396 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8397 int ARG_UNUSED (flags), bool *no_add_attrs)
8399 if (TREE_CODE (*node) != FUNCTION_DECL)
8401 warning (OPT_Wattributes, "%qE attribute ignored", name);
8402 *no_add_attrs = true;
8405 return NULL_TREE;
8408 /* Handle a "warn_unused" attribute; arguments as in
8409 struct attribute_spec.handler. */
8411 static tree
8412 handle_warn_unused_type_attribute (tree *node ATTRIBUTE_UNUSED,
8413 tree name ATTRIBUTE_UNUSED,
8414 tree args ATTRIBUTE_UNUSED,
8415 int flags ATTRIBUTE_UNUSED,
8416 bool *no_add_attrs ATTRIBUTE_UNUSED)
8418 /* Do nothing else, just set the attribute. We'll get at
8419 it later with lookup_attribute. */
8420 return NULL_TREE;
8423 static tree
8424 handle_warn_unused_decl_attribute (tree *node ATTRIBUTE_UNUSED,
8425 tree name ATTRIBUTE_UNUSED,
8426 tree args ATTRIBUTE_UNUSED,
8427 int flags ATTRIBUTE_UNUSED,
8428 bool *no_add_attrs ATTRIBUTE_UNUSED)
8430 /* Do nothing else, just set the attribute. We'll get at
8431 it later with lookup_attribute. */
8432 return NULL_TREE;
8435 /* Handle an "omp declare simd" attribute; arguments as in
8436 struct attribute_spec.handler. */
8438 static tree
8439 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8441 return NULL_TREE;
8444 /* Handle an "omp declare target" attribute; arguments as in
8445 struct attribute_spec.handler. */
8447 static tree
8448 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8450 return NULL_TREE;
8453 /* Handle a "returns_twice" attribute; arguments as in
8454 struct attribute_spec.handler. */
8456 static tree
8457 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8458 int ARG_UNUSED (flags), bool *no_add_attrs)
8460 if (TREE_CODE (*node) == FUNCTION_DECL)
8461 DECL_IS_RETURNS_TWICE (*node) = 1;
8462 else
8464 warning (OPT_Wattributes, "%qE attribute ignored", name);
8465 *no_add_attrs = true;
8468 return NULL_TREE;
8471 /* Handle a "no_limit_stack" attribute; arguments as in
8472 struct attribute_spec.handler. */
8474 static tree
8475 handle_no_limit_stack_attribute (tree *node, tree name,
8476 tree ARG_UNUSED (args),
8477 int ARG_UNUSED (flags),
8478 bool *no_add_attrs)
8480 tree decl = *node;
8482 if (TREE_CODE (decl) != FUNCTION_DECL)
8484 error_at (DECL_SOURCE_LOCATION (decl),
8485 "%qE attribute applies only to functions", name);
8486 *no_add_attrs = true;
8488 else if (DECL_INITIAL (decl))
8490 error_at (DECL_SOURCE_LOCATION (decl),
8491 "can%'t set %qE attribute after definition", name);
8492 *no_add_attrs = true;
8494 else
8495 DECL_NO_LIMIT_STACK (decl) = 1;
8497 return NULL_TREE;
8500 /* Handle a "pure" attribute; arguments as in
8501 struct attribute_spec.handler. */
8503 static tree
8504 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8505 int ARG_UNUSED (flags), bool *no_add_attrs)
8507 if (TREE_CODE (*node) == FUNCTION_DECL)
8508 DECL_PURE_P (*node) = 1;
8509 /* ??? TODO: Support types. */
8510 else
8512 warning (OPT_Wattributes, "%qE attribute ignored", name);
8513 *no_add_attrs = true;
8516 return NULL_TREE;
8519 /* Digest an attribute list destined for a transactional memory statement.
8520 ALLOWED is the set of attributes that are allowed for this statement;
8521 return the attribute we parsed. Multiple attributes are never allowed. */
8524 parse_tm_stmt_attr (tree attrs, int allowed)
8526 tree a_seen = NULL;
8527 int m_seen = 0;
8529 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8531 tree a = TREE_PURPOSE (attrs);
8532 int m = 0;
8534 if (is_attribute_p ("outer", a))
8535 m = TM_STMT_ATTR_OUTER;
8537 if ((m & allowed) == 0)
8539 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8540 continue;
8543 if (m_seen == 0)
8545 a_seen = a;
8546 m_seen = m;
8548 else if (m_seen == m)
8549 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8550 else
8551 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8554 return m_seen;
8557 /* Transform a TM attribute name into a maskable integer and back.
8558 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8559 to how the lack of an attribute is treated. */
8562 tm_attr_to_mask (tree attr)
8564 if (attr == NULL)
8565 return 0;
8566 if (is_attribute_p ("transaction_safe", attr))
8567 return TM_ATTR_SAFE;
8568 if (is_attribute_p ("transaction_callable", attr))
8569 return TM_ATTR_CALLABLE;
8570 if (is_attribute_p ("transaction_pure", attr))
8571 return TM_ATTR_PURE;
8572 if (is_attribute_p ("transaction_unsafe", attr))
8573 return TM_ATTR_IRREVOCABLE;
8574 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8575 return TM_ATTR_MAY_CANCEL_OUTER;
8576 return 0;
8579 tree
8580 tm_mask_to_attr (int mask)
8582 const char *str;
8583 switch (mask)
8585 case TM_ATTR_SAFE:
8586 str = "transaction_safe";
8587 break;
8588 case TM_ATTR_CALLABLE:
8589 str = "transaction_callable";
8590 break;
8591 case TM_ATTR_PURE:
8592 str = "transaction_pure";
8593 break;
8594 case TM_ATTR_IRREVOCABLE:
8595 str = "transaction_unsafe";
8596 break;
8597 case TM_ATTR_MAY_CANCEL_OUTER:
8598 str = "transaction_may_cancel_outer";
8599 break;
8600 default:
8601 gcc_unreachable ();
8603 return get_identifier (str);
8606 /* Return the first TM attribute seen in LIST. */
8608 tree
8609 find_tm_attribute (tree list)
8611 for (; list ; list = TREE_CHAIN (list))
8613 tree name = TREE_PURPOSE (list);
8614 if (tm_attr_to_mask (name) != 0)
8615 return name;
8617 return NULL_TREE;
8620 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8621 Here we accept only function types, and verify that none of the other
8622 function TM attributes are also applied. */
8623 /* ??? We need to accept class types for C++, but not C. This greatly
8624 complicates this function, since we can no longer rely on the extra
8625 processing given by function_type_required. */
8627 static tree
8628 handle_tm_attribute (tree *node, tree name, tree args,
8629 int flags, bool *no_add_attrs)
8631 /* Only one path adds the attribute; others don't. */
8632 *no_add_attrs = true;
8634 switch (TREE_CODE (*node))
8636 case RECORD_TYPE:
8637 case UNION_TYPE:
8638 /* Only tm_callable and tm_safe apply to classes. */
8639 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8640 goto ignored;
8641 /* FALLTHRU */
8643 case FUNCTION_TYPE:
8644 case METHOD_TYPE:
8646 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8647 if (old_name == name)
8649 else if (old_name != NULL_TREE)
8650 error ("type was previously declared %qE", old_name);
8651 else
8652 *no_add_attrs = false;
8654 break;
8656 case POINTER_TYPE:
8658 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8659 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8661 tree fn_tmp = TREE_TYPE (*node);
8662 type_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8663 *node = build_pointer_type (fn_tmp);
8664 break;
8667 /* FALLTHRU */
8669 default:
8670 /* If a function is next, pass it on to be tried next. */
8671 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8672 return tree_cons (name, args, NULL);
8674 ignored:
8675 warning (OPT_Wattributes, "%qE attribute ignored", name);
8676 break;
8679 return NULL_TREE;
8682 /* Handle the TM_WRAP attribute; arguments as in
8683 struct attribute_spec.handler. */
8685 static tree
8686 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8687 int ARG_UNUSED (flags), bool *no_add_attrs)
8689 tree decl = *node;
8691 /* We don't need the attribute even on success, since we
8692 record the entry in an external table. */
8693 *no_add_attrs = true;
8695 if (TREE_CODE (decl) != FUNCTION_DECL)
8696 warning (OPT_Wattributes, "%qE attribute ignored", name);
8697 else
8699 tree wrap_decl = TREE_VALUE (args);
8700 if (error_operand_p (wrap_decl))
8702 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8703 && TREE_CODE (wrap_decl) != VAR_DECL
8704 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8705 error ("%qE argument not an identifier", name);
8706 else
8708 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8709 wrap_decl = lookup_name (wrap_decl);
8710 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8712 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8713 TREE_TYPE (wrap_decl)))
8714 record_tm_replacement (wrap_decl, decl);
8715 else
8716 error ("%qD is not compatible with %qD", wrap_decl, decl);
8718 else
8719 error ("%qE argument is not a function", name);
8723 return NULL_TREE;
8726 /* Ignore the given attribute. Used when this attribute may be usefully
8727 overridden by the target, but is not used generically. */
8729 static tree
8730 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8731 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8732 bool *no_add_attrs)
8734 *no_add_attrs = true;
8735 return NULL_TREE;
8738 /* Handle a "no vops" attribute; arguments as in
8739 struct attribute_spec.handler. */
8741 static tree
8742 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8743 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8744 bool *ARG_UNUSED (no_add_attrs))
8746 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8747 DECL_IS_NOVOPS (*node) = 1;
8748 return NULL_TREE;
8751 /* Handle a "deprecated" attribute; arguments as in
8752 struct attribute_spec.handler. */
8754 static tree
8755 handle_deprecated_type_attribute (tree *node, tree ARG_UNUSED (name),
8756 tree args, int flags,
8757 bool *no_add_attrs)
8759 if (!args)
8760 *no_add_attrs = true;
8761 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8763 error ("deprecated message is not a string");
8764 *no_add_attrs = true;
8767 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8768 *node = build_variant_type_copy (*node);
8769 TREE_DEPRECATED (*node) = 1;
8771 return NULL_TREE;
8775 static tree
8776 handle_deprecated_decl_attribute (tree *node, tree name,
8777 tree args, int ARG_UNUSED (flags),
8778 bool *no_add_attrs)
8780 tree decl = *node;
8781 tree what = NULL_TREE;
8783 if (!args)
8784 *no_add_attrs = true;
8785 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8787 error ("deprecated message is not a string");
8788 *no_add_attrs = true;
8791 if (TREE_CODE (decl) == TYPE_DECL
8792 || TREE_CODE (decl) == PARM_DECL
8793 || TREE_CODE (decl) == VAR_DECL
8794 || TREE_CODE (decl) == FUNCTION_DECL
8795 || TREE_CODE (decl) == FIELD_DECL
8796 || objc_method_decl (TREE_CODE (decl)))
8797 TREE_DEPRECATED (decl) = 1;
8798 else
8800 tree type = TREE_TYPE (decl);
8801 *no_add_attrs = true;
8802 if (type && TYPE_NAME (type))
8804 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8805 what = TYPE_NAME (*node);
8806 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8807 && DECL_NAME (TYPE_NAME (type)))
8808 what = DECL_NAME (TYPE_NAME (type));
8810 if (what)
8811 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8812 else
8813 warning (OPT_Wattributes, "%qE attribute ignored", name);
8816 return NULL_TREE;
8819 /* Handle a "vector_size" attribute; arguments as in
8820 struct attribute_spec.handler. */
8822 static tree
8823 handle_vector_size_attribute (tree *node, tree name, tree args,
8824 int ARG_UNUSED (flags),
8825 bool *no_add_attrs)
8827 unsigned HOST_WIDE_INT vecsize, nunits;
8828 machine_mode orig_mode;
8829 tree type = *node, new_type, size;
8831 *no_add_attrs = true;
8833 size = TREE_VALUE (args);
8834 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8835 && TREE_CODE (size) != FUNCTION_DECL)
8836 size = default_conversion (size);
8838 if (!tree_fits_uhwi_p (size))
8840 warning (OPT_Wattributes, "%qE attribute ignored", name);
8841 return NULL_TREE;
8844 /* Get the vector size (in bytes). */
8845 vecsize = tree_to_uhwi (size);
8847 /* We need to provide for vector pointers, vector arrays, and
8848 functions returning vectors. For example:
8850 __attribute__((vector_size(16))) short *foo;
8852 In this case, the mode is SI, but the type being modified is
8853 HI, so we need to look further. */
8855 while (POINTER_TYPE_P (type)
8856 || TREE_CODE (type) == FUNCTION_TYPE
8857 || TREE_CODE (type) == METHOD_TYPE
8858 || TREE_CODE (type) == ARRAY_TYPE
8859 || TREE_CODE (type) == OFFSET_TYPE)
8860 type = TREE_TYPE (type);
8862 /* Get the mode of the type being modified. */
8863 orig_mode = TYPE_MODE (type);
8865 if ((!INTEGRAL_TYPE_P (type)
8866 && !SCALAR_FLOAT_TYPE_P (type)
8867 && !FIXED_POINT_TYPE_P (type))
8868 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8869 && GET_MODE_CLASS (orig_mode) != MODE_INT
8870 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8871 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8872 || TREE_CODE (type) == BOOLEAN_TYPE)
8874 error ("invalid vector type for attribute %qE", name);
8875 return NULL_TREE;
8878 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8880 error ("vector size not an integral multiple of component size");
8881 return NULL;
8884 if (vecsize == 0)
8886 error ("zero vector size");
8887 return NULL;
8890 /* Calculate how many units fit in the vector. */
8891 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8892 if (nunits & (nunits - 1))
8894 error ("number of components of the vector not a power of two");
8895 return NULL_TREE;
8898 new_type = build_vector_type (type, nunits);
8900 /* Build back pointers if needed. */
8901 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8903 return NULL_TREE;
8906 /* Handle the "nonnull" attribute. */
8907 static tree
8908 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8909 tree args, int ARG_UNUSED (flags),
8910 bool *no_add_attrs)
8912 tree type = *node;
8913 unsigned HOST_WIDE_INT attr_arg_num;
8915 /* If no arguments are specified, all pointer arguments should be
8916 non-null. Verify a full prototype is given so that the arguments
8917 will have the correct types when we actually check them later. */
8918 if (!args)
8920 if (!prototype_p (type))
8922 error ("nonnull attribute without arguments on a non-prototype");
8923 *no_add_attrs = true;
8925 return NULL_TREE;
8928 /* Argument list specified. Verify that each argument number references
8929 a pointer argument. */
8930 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8932 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8934 tree arg = TREE_VALUE (args);
8935 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8936 && TREE_CODE (arg) != FUNCTION_DECL)
8937 arg = default_conversion (arg);
8939 if (!get_nonnull_operand (arg, &arg_num))
8941 error ("nonnull argument has invalid operand number (argument %lu)",
8942 (unsigned long) attr_arg_num);
8943 *no_add_attrs = true;
8944 return NULL_TREE;
8947 if (prototype_p (type))
8949 function_args_iterator iter;
8950 tree argument;
8952 function_args_iter_init (&iter, type);
8953 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8955 argument = function_args_iter_cond (&iter);
8956 if (argument == NULL_TREE || ck_num == arg_num)
8957 break;
8960 if (!argument
8961 || TREE_CODE (argument) == VOID_TYPE)
8963 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8964 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8965 *no_add_attrs = true;
8966 return NULL_TREE;
8969 if (TREE_CODE (argument) != POINTER_TYPE)
8971 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8972 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8973 *no_add_attrs = true;
8974 return NULL_TREE;
8979 return NULL_TREE;
8982 /* Check the argument list of a function call for null in argument slots
8983 that are marked as requiring a non-null pointer argument. The NARGS
8984 arguments are passed in the array ARGARRAY.
8987 static void
8988 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8990 tree a;
8991 int i;
8993 attrs = lookup_attribute ("nonnull", attrs);
8994 if (attrs == NULL_TREE)
8995 return;
8997 a = attrs;
8998 /* See if any of the nonnull attributes has no arguments. If so,
8999 then every pointer argument is checked (in which case the check
9000 for pointer type is done in check_nonnull_arg). */
9001 if (TREE_VALUE (a) != NULL_TREE)
9003 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9004 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9006 if (a != NULL_TREE)
9007 for (i = 0; i < nargs; i++)
9008 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9009 i + 1);
9010 else
9012 /* Walk the argument list. If we encounter an argument number we
9013 should check for non-null, do it. */
9014 for (i = 0; i < nargs; i++)
9016 for (a = attrs; ; a = TREE_CHAIN (a))
9018 a = lookup_attribute ("nonnull", a);
9019 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9020 break;
9023 if (a != NULL_TREE)
9024 check_function_arguments_recurse (check_nonnull_arg, NULL,
9025 argarray[i], i + 1);
9030 /* Check that the Nth argument of a function call (counting backwards
9031 from the end) is a (pointer)0. The NARGS arguments are passed in the
9032 array ARGARRAY. */
9034 static void
9035 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9037 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9039 if (attr)
9041 int len = 0;
9042 int pos = 0;
9043 tree sentinel;
9044 function_args_iterator iter;
9045 tree t;
9047 /* Skip over the named arguments. */
9048 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9050 if (len == nargs)
9051 break;
9052 len++;
9055 if (TREE_VALUE (attr))
9057 tree p = TREE_VALUE (TREE_VALUE (attr));
9058 pos = TREE_INT_CST_LOW (p);
9061 /* The sentinel must be one of the varargs, i.e.
9062 in position >= the number of fixed arguments. */
9063 if ((nargs - 1 - pos) < len)
9065 warning (OPT_Wformat_,
9066 "not enough variable arguments to fit a sentinel");
9067 return;
9070 /* Validate the sentinel. */
9071 sentinel = argarray[nargs - 1 - pos];
9072 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9073 || !integer_zerop (sentinel))
9074 /* Although __null (in C++) is only an integer we allow it
9075 nevertheless, as we are guaranteed that it's exactly
9076 as wide as a pointer, and we don't want to force
9077 users to cast the NULL they have written there.
9078 We warn with -Wstrict-null-sentinel, though. */
9079 && (warn_strict_null_sentinel || null_node != sentinel))
9080 warning (OPT_Wformat_, "missing sentinel in function call");
9084 /* Helper for check_function_nonnull; given a list of operands which
9085 must be non-null in ARGS, determine if operand PARAM_NUM should be
9086 checked. */
9088 static bool
9089 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9091 unsigned HOST_WIDE_INT arg_num = 0;
9093 for (; args; args = TREE_CHAIN (args))
9095 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9097 gcc_assert (found);
9099 if (arg_num == param_num)
9100 return true;
9102 return false;
9105 /* Check that the function argument PARAM (which is operand number
9106 PARAM_NUM) is non-null. This is called by check_function_nonnull
9107 via check_function_arguments_recurse. */
9109 static void
9110 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9111 unsigned HOST_WIDE_INT param_num)
9113 /* Just skip checking the argument if it's not a pointer. This can
9114 happen if the "nonnull" attribute was given without an operand
9115 list (which means to check every pointer argument). */
9117 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9118 return;
9120 if (integer_zerop (param))
9121 warning (OPT_Wnonnull, "null argument where non-null required "
9122 "(argument %lu)", (unsigned long) param_num);
9125 /* Helper for nonnull attribute handling; fetch the operand number
9126 from the attribute argument list. */
9128 static bool
9129 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9131 /* Verify the arg number is a small constant. */
9132 if (tree_fits_uhwi_p (arg_num_expr))
9134 *valp = TREE_INT_CST_LOW (arg_num_expr);
9135 return true;
9137 else
9138 return false;
9141 /* Handle a "nothrow" attribute; arguments as in
9142 struct attribute_spec.handler. */
9144 static tree
9145 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9146 int ARG_UNUSED (flags), bool *no_add_attrs)
9148 if (TREE_CODE (*node) == FUNCTION_DECL)
9149 TREE_NOTHROW (*node) = 1;
9150 /* ??? TODO: Support types. */
9151 else
9153 warning (OPT_Wattributes, "%qE attribute ignored", name);
9154 *no_add_attrs = true;
9157 return NULL_TREE;
9160 /* Handle a "cleanup" attribute; arguments as in
9161 struct attribute_spec.handler. */
9163 static tree
9164 handle_cleanup_attribute (tree *node, tree name, tree args,
9165 int ARG_UNUSED (flags), bool *no_add_attrs)
9167 tree decl = *node;
9168 tree cleanup_id, cleanup_decl;
9170 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9171 for global destructors in C++. This requires infrastructure that
9172 we don't have generically at the moment. It's also not a feature
9173 we'd be missing too much, since we do have attribute constructor. */
9174 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9176 warning (OPT_Wattributes, "%qE attribute ignored", name);
9177 *no_add_attrs = true;
9178 return NULL_TREE;
9181 /* Verify that the argument is a function in scope. */
9182 /* ??? We could support pointers to functions here as well, if
9183 that was considered desirable. */
9184 cleanup_id = TREE_VALUE (args);
9185 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9187 error ("cleanup argument not an identifier");
9188 *no_add_attrs = true;
9189 return NULL_TREE;
9191 cleanup_decl = lookup_name (cleanup_id);
9192 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9194 error ("cleanup argument not a function");
9195 *no_add_attrs = true;
9196 return NULL_TREE;
9199 /* That the function has proper type is checked with the
9200 eventual call to build_function_call. */
9202 return NULL_TREE;
9205 /* Handle a "warn_unused_result" attribute. No special handling. */
9207 static tree
9208 handle_warn_unused_result_attribute (tree *node, tree name,
9209 tree ARG_UNUSED (args),
9210 int ARG_UNUSED (flags), bool *no_add_attrs)
9212 /* Ignore the attribute for functions not returning any value. */
9213 if (VOID_TYPE_P (TREE_TYPE (*node)))
9215 warning (OPT_Wattributes, "%qE attribute ignored", name);
9216 *no_add_attrs = true;
9219 return NULL_TREE;
9222 /* Handle a "sentinel" attribute. */
9224 static tree
9225 handle_sentinel_attribute (tree *node, tree name, tree args,
9226 int ARG_UNUSED (flags), bool *no_add_attrs)
9228 if (!prototype_p (*node))
9230 warning (OPT_Wattributes,
9231 "%qE attribute requires prototypes with named arguments", name);
9232 *no_add_attrs = true;
9234 else
9236 if (!stdarg_p (*node))
9238 warning (OPT_Wattributes,
9239 "%qE attribute only applies to variadic functions", name);
9240 *no_add_attrs = true;
9244 if (args)
9246 tree position = TREE_VALUE (args);
9247 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9248 && TREE_CODE (position) != FUNCTION_DECL)
9249 position = default_conversion (position);
9251 if (TREE_CODE (position) != INTEGER_CST
9252 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9254 warning (OPT_Wattributes,
9255 "requested position is not an integer constant");
9256 *no_add_attrs = true;
9258 else
9260 if (tree_int_cst_lt (position, integer_zero_node))
9262 warning (OPT_Wattributes,
9263 "requested position is less than zero");
9264 *no_add_attrs = true;
9269 return NULL_TREE;
9272 /* Handle a "type_generic" attribute. */
9274 static tree
9275 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9276 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9277 bool * ARG_UNUSED (no_add_attrs))
9279 /* Ensure we have a function type. */
9280 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9282 /* Ensure we have a variadic function. */
9283 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9285 return NULL_TREE;
9288 /* Handle a "target" attribute. */
9290 static tree
9291 handle_target_attribute (tree *node, tree name, tree args, int flags,
9292 bool *no_add_attrs)
9294 /* Ensure we have a function type. */
9295 if (TREE_CODE (*node) != FUNCTION_DECL)
9297 warning (OPT_Wattributes, "%qE attribute ignored", name);
9298 *no_add_attrs = true;
9300 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9301 flags))
9302 *no_add_attrs = true;
9304 return NULL_TREE;
9307 /* Arguments being collected for optimization. */
9308 typedef const char *const_char_p; /* For DEF_VEC_P. */
9309 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9312 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9313 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9314 false for #pragma GCC optimize. */
9316 bool
9317 parse_optimize_options (tree args, bool attr_p)
9319 bool ret = true;
9320 unsigned opt_argc;
9321 unsigned i;
9322 int saved_flag_strict_aliasing;
9323 const char **opt_argv;
9324 struct cl_decoded_option *decoded_options;
9325 unsigned int decoded_options_count;
9326 tree ap;
9328 /* Build up argv vector. Just in case the string is stored away, use garbage
9329 collected strings. */
9330 vec_safe_truncate (optimize_args, 0);
9331 vec_safe_push (optimize_args, (const char *) NULL);
9333 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9335 tree value = TREE_VALUE (ap);
9337 if (TREE_CODE (value) == INTEGER_CST)
9339 char buffer[20];
9340 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9341 vec_safe_push (optimize_args, ggc_strdup (buffer));
9344 else if (TREE_CODE (value) == STRING_CST)
9346 /* Split string into multiple substrings. */
9347 size_t len = TREE_STRING_LENGTH (value);
9348 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9349 char *end = p + len;
9350 char *comma;
9351 char *next_p = p;
9353 while (next_p != NULL)
9355 size_t len2;
9356 char *q, *r;
9358 p = next_p;
9359 comma = strchr (p, ',');
9360 if (comma)
9362 len2 = comma - p;
9363 *comma = '\0';
9364 next_p = comma+1;
9366 else
9368 len2 = end - p;
9369 next_p = NULL;
9372 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9374 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9375 options. */
9376 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9378 ret = false;
9379 if (attr_p)
9380 warning (OPT_Wattributes,
9381 "bad option %s to optimize attribute", p);
9382 else
9383 warning (OPT_Wpragmas,
9384 "bad option %s to pragma attribute", p);
9385 continue;
9388 if (*p != '-')
9390 *r++ = '-';
9392 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9393 itself is -Os, and any other switch begins with a -f. */
9394 if ((*p >= '0' && *p <= '9')
9395 || (p[0] == 's' && p[1] == '\0'))
9396 *r++ = 'O';
9397 else if (*p != 'O')
9398 *r++ = 'f';
9401 memcpy (r, p, len2);
9402 r[len2] = '\0';
9403 vec_safe_push (optimize_args, (const char *) q);
9409 opt_argc = optimize_args->length ();
9410 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9412 for (i = 1; i < opt_argc; i++)
9413 opt_argv[i] = (*optimize_args)[i];
9415 saved_flag_strict_aliasing = flag_strict_aliasing;
9417 /* Now parse the options. */
9418 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9419 &decoded_options,
9420 &decoded_options_count);
9421 decode_options (&global_options, &global_options_set,
9422 decoded_options, decoded_options_count,
9423 input_location, global_dc);
9425 targetm.override_options_after_change();
9427 /* Don't allow changing -fstrict-aliasing. */
9428 flag_strict_aliasing = saved_flag_strict_aliasing;
9430 optimize_args->truncate (0);
9431 return ret;
9434 /* For handling "optimize" attribute. arguments as in
9435 struct attribute_spec.handler. */
9437 static tree
9438 handle_optimize_attribute (tree *node, tree name, tree args,
9439 int ARG_UNUSED (flags), bool *no_add_attrs)
9441 /* Ensure we have a function type. */
9442 if (TREE_CODE (*node) != FUNCTION_DECL)
9444 warning (OPT_Wattributes, "%qE attribute ignored", name);
9445 *no_add_attrs = true;
9447 else
9449 struct cl_optimization cur_opts;
9450 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9452 /* Save current options. */
9453 cl_optimization_save (&cur_opts, &global_options);
9455 /* If we previously had some optimization options, use them as the
9456 default. */
9457 if (old_opts)
9458 cl_optimization_restore (&global_options,
9459 TREE_OPTIMIZATION (old_opts));
9461 /* Parse options, and update the vector. */
9462 parse_optimize_options (args, true);
9463 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9464 = build_optimization_node (&global_options);
9466 /* Restore current options. */
9467 cl_optimization_restore (&global_options, &cur_opts);
9470 return NULL_TREE;
9473 /* Handle a "no_split_stack" attribute. */
9475 static tree
9476 handle_no_split_stack_attribute (tree *node, tree name,
9477 tree ARG_UNUSED (args),
9478 int ARG_UNUSED (flags),
9479 bool *no_add_attrs)
9481 tree decl = *node;
9483 if (TREE_CODE (decl) != FUNCTION_DECL)
9485 error_at (DECL_SOURCE_LOCATION (decl),
9486 "%qE attribute applies only to functions", name);
9487 *no_add_attrs = true;
9489 else if (DECL_INITIAL (decl))
9491 error_at (DECL_SOURCE_LOCATION (decl),
9492 "can%'t set %qE attribute after definition", name);
9493 *no_add_attrs = true;
9496 return NULL_TREE;
9499 /* Handle a "returns_nonnull" attribute; arguments as in
9500 struct attribute_spec.handler. */
9502 static tree
9503 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9504 bool *no_add_attrs)
9506 // Even without a prototype we still have a return type we can check.
9507 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9509 error ("returns_nonnull attribute on a function not returning a pointer");
9510 *no_add_attrs = true;
9512 return NULL_TREE;
9515 /* Handle a "designated_init" attribute; arguments as in
9516 struct attribute_spec.handler. */
9518 static tree
9519 handle_designated_init_attribute (tree *node, tree name, tree, int,
9520 bool *no_add_attrs)
9522 if (TREE_CODE (*node) != RECORD_TYPE)
9524 error ("%qE attribute is only valid on %<struct%> type", name);
9525 *no_add_attrs = true;
9527 return NULL_TREE;
9531 /* Check for valid arguments being passed to a function with FNTYPE.
9532 There are NARGS arguments in the array ARGARRAY. */
9533 void
9534 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9536 /* Check for null being passed in a pointer argument that must be
9537 non-null. We also need to do this if format checking is enabled. */
9539 if (warn_nonnull)
9540 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9542 /* Check for errors in format strings. */
9544 if (warn_format || warn_suggest_attribute_format)
9545 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9547 if (warn_format)
9548 check_function_sentinel (fntype, nargs, argarray);
9551 /* Generic argument checking recursion routine. PARAM is the argument to
9552 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9553 once the argument is resolved. CTX is context for the callback. */
9554 void
9555 check_function_arguments_recurse (void (*callback)
9556 (void *, tree, unsigned HOST_WIDE_INT),
9557 void *ctx, tree param,
9558 unsigned HOST_WIDE_INT param_num)
9560 if (CONVERT_EXPR_P (param)
9561 && (TYPE_PRECISION (TREE_TYPE (param))
9562 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9564 /* Strip coercion. */
9565 check_function_arguments_recurse (callback, ctx,
9566 TREE_OPERAND (param, 0), param_num);
9567 return;
9570 if (TREE_CODE (param) == CALL_EXPR)
9572 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9573 tree attrs;
9574 bool found_format_arg = false;
9576 /* See if this is a call to a known internationalization function
9577 that modifies a format arg. Such a function may have multiple
9578 format_arg attributes (for example, ngettext). */
9580 for (attrs = TYPE_ATTRIBUTES (type);
9581 attrs;
9582 attrs = TREE_CHAIN (attrs))
9583 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9585 tree inner_arg;
9586 tree format_num_expr;
9587 int format_num;
9588 int i;
9589 call_expr_arg_iterator iter;
9591 /* Extract the argument number, which was previously checked
9592 to be valid. */
9593 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9595 format_num = tree_to_uhwi (format_num_expr);
9597 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9598 inner_arg != 0;
9599 inner_arg = next_call_expr_arg (&iter), i++)
9600 if (i == format_num)
9602 check_function_arguments_recurse (callback, ctx,
9603 inner_arg, param_num);
9604 found_format_arg = true;
9605 break;
9609 /* If we found a format_arg attribute and did a recursive check,
9610 we are done with checking this argument. Otherwise, we continue
9611 and this will be considered a non-literal. */
9612 if (found_format_arg)
9613 return;
9616 if (TREE_CODE (param) == COND_EXPR)
9618 /* Check both halves of the conditional expression. */
9619 check_function_arguments_recurse (callback, ctx,
9620 TREE_OPERAND (param, 1), param_num);
9621 check_function_arguments_recurse (callback, ctx,
9622 TREE_OPERAND (param, 2), param_num);
9623 return;
9626 (*callback) (ctx, param, param_num);
9629 /* Checks for a builtin function FNDECL that the number of arguments
9630 NARGS against the required number REQUIRED and issues an error if
9631 there is a mismatch. Returns true if the number of arguments is
9632 correct, otherwise false. */
9634 static bool
9635 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9637 if (nargs < required)
9639 error_at (input_location,
9640 "not enough arguments to function %qE", fndecl);
9641 return false;
9643 else if (nargs > required)
9645 error_at (input_location,
9646 "too many arguments to function %qE", fndecl);
9647 return false;
9649 return true;
9652 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9653 Returns false if there was an error, otherwise true. */
9655 bool
9656 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9658 if (!DECL_BUILT_IN (fndecl)
9659 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9660 return true;
9662 switch (DECL_FUNCTION_CODE (fndecl))
9664 case BUILT_IN_CONSTANT_P:
9665 return builtin_function_validate_nargs (fndecl, nargs, 1);
9667 case BUILT_IN_ISFINITE:
9668 case BUILT_IN_ISINF:
9669 case BUILT_IN_ISINF_SIGN:
9670 case BUILT_IN_ISNAN:
9671 case BUILT_IN_ISNORMAL:
9672 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9674 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9676 error ("non-floating-point argument in call to "
9677 "function %qE", fndecl);
9678 return false;
9680 return true;
9682 return false;
9684 case BUILT_IN_ISGREATER:
9685 case BUILT_IN_ISGREATEREQUAL:
9686 case BUILT_IN_ISLESS:
9687 case BUILT_IN_ISLESSEQUAL:
9688 case BUILT_IN_ISLESSGREATER:
9689 case BUILT_IN_ISUNORDERED:
9690 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9692 enum tree_code code0, code1;
9693 code0 = TREE_CODE (TREE_TYPE (args[0]));
9694 code1 = TREE_CODE (TREE_TYPE (args[1]));
9695 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9696 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9697 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9699 error ("non-floating-point arguments in call to "
9700 "function %qE", fndecl);
9701 return false;
9703 return true;
9705 return false;
9707 case BUILT_IN_FPCLASSIFY:
9708 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9710 unsigned i;
9712 for (i=0; i<5; i++)
9713 if (TREE_CODE (args[i]) != INTEGER_CST)
9715 error ("non-const integer argument %u in call to function %qE",
9716 i+1, fndecl);
9717 return false;
9720 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9722 error ("non-floating-point argument in call to function %qE",
9723 fndecl);
9724 return false;
9726 return true;
9728 return false;
9730 case BUILT_IN_ASSUME_ALIGNED:
9731 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9733 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9735 error ("non-integer argument 3 in call to function %qE", fndecl);
9736 return false;
9738 return true;
9740 return false;
9742 case BUILT_IN_ADD_OVERFLOW:
9743 case BUILT_IN_SUB_OVERFLOW:
9744 case BUILT_IN_MUL_OVERFLOW:
9745 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9747 unsigned i;
9748 for (i = 0; i < 2; i++)
9749 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9751 error ("argument %u in call to function %qE does not have "
9752 "integral type", i + 1, fndecl);
9753 return false;
9755 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9756 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9758 error ("argument 3 in call to function %qE does not have "
9759 "pointer to integer type", fndecl);
9760 return false;
9762 return true;
9764 return false;
9766 default:
9767 return true;
9771 /* Function to help qsort sort FIELD_DECLs by name order. */
9774 field_decl_cmp (const void *x_p, const void *y_p)
9776 const tree *const x = (const tree *const) x_p;
9777 const tree *const y = (const tree *const) y_p;
9779 if (DECL_NAME (*x) == DECL_NAME (*y))
9780 /* A nontype is "greater" than a type. */
9781 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9782 if (DECL_NAME (*x) == NULL_TREE)
9783 return -1;
9784 if (DECL_NAME (*y) == NULL_TREE)
9785 return 1;
9786 if (DECL_NAME (*x) < DECL_NAME (*y))
9787 return -1;
9788 return 1;
9791 static struct {
9792 gt_pointer_operator new_value;
9793 void *cookie;
9794 } resort_data;
9796 /* This routine compares two fields like field_decl_cmp but using the
9797 pointer operator in resort_data. */
9799 static int
9800 resort_field_decl_cmp (const void *x_p, const void *y_p)
9802 const tree *const x = (const tree *const) x_p;
9803 const tree *const y = (const tree *const) y_p;
9805 if (DECL_NAME (*x) == DECL_NAME (*y))
9806 /* A nontype is "greater" than a type. */
9807 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9808 if (DECL_NAME (*x) == NULL_TREE)
9809 return -1;
9810 if (DECL_NAME (*y) == NULL_TREE)
9811 return 1;
9813 tree d1 = DECL_NAME (*x);
9814 tree d2 = DECL_NAME (*y);
9815 resort_data.new_value (&d1, resort_data.cookie);
9816 resort_data.new_value (&d2, resort_data.cookie);
9817 if (d1 < d2)
9818 return -1;
9820 return 1;
9823 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9825 void
9826 resort_sorted_fields (void *obj,
9827 void * ARG_UNUSED (orig_obj),
9828 gt_pointer_operator new_value,
9829 void *cookie)
9831 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9832 resort_data.new_value = new_value;
9833 resort_data.cookie = cookie;
9834 qsort (&sf->elts[0], sf->len, sizeof (tree),
9835 resort_field_decl_cmp);
9838 /* Subroutine of c_parse_error.
9839 Return the result of concatenating LHS and RHS. RHS is really
9840 a string literal, its first character is indicated by RHS_START and
9841 RHS_SIZE is its length (including the terminating NUL character).
9843 The caller is responsible for deleting the returned pointer. */
9845 static char *
9846 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9848 const int lhs_size = strlen (lhs);
9849 char *result = XNEWVEC (char, lhs_size + rhs_size);
9850 strncpy (result, lhs, lhs_size);
9851 strncpy (result + lhs_size, rhs_start, rhs_size);
9852 return result;
9855 /* Issue the error given by GMSGID, indicating that it occurred before
9856 TOKEN, which had the associated VALUE. */
9858 void
9859 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9860 tree value, unsigned char token_flags)
9862 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9864 char *message = NULL;
9866 if (token_type == CPP_EOF)
9867 message = catenate_messages (gmsgid, " at end of input");
9868 else if (token_type == CPP_CHAR
9869 || token_type == CPP_WCHAR
9870 || token_type == CPP_CHAR16
9871 || token_type == CPP_CHAR32)
9873 unsigned int val = TREE_INT_CST_LOW (value);
9874 const char *prefix;
9876 switch (token_type)
9878 default:
9879 prefix = "";
9880 break;
9881 case CPP_WCHAR:
9882 prefix = "L";
9883 break;
9884 case CPP_CHAR16:
9885 prefix = "u";
9886 break;
9887 case CPP_CHAR32:
9888 prefix = "U";
9889 break;
9892 if (val <= UCHAR_MAX && ISGRAPH (val))
9893 message = catenate_messages (gmsgid, " before %s'%c'");
9894 else
9895 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9897 error (message, prefix, val);
9898 free (message);
9899 message = NULL;
9901 else if (token_type == CPP_CHAR_USERDEF
9902 || token_type == CPP_WCHAR_USERDEF
9903 || token_type == CPP_CHAR16_USERDEF
9904 || token_type == CPP_CHAR32_USERDEF)
9905 message = catenate_messages (gmsgid,
9906 " before user-defined character literal");
9907 else if (token_type == CPP_STRING_USERDEF
9908 || token_type == CPP_WSTRING_USERDEF
9909 || token_type == CPP_STRING16_USERDEF
9910 || token_type == CPP_STRING32_USERDEF
9911 || token_type == CPP_UTF8STRING_USERDEF)
9912 message = catenate_messages (gmsgid, " before user-defined string literal");
9913 else if (token_type == CPP_STRING
9914 || token_type == CPP_WSTRING
9915 || token_type == CPP_STRING16
9916 || token_type == CPP_STRING32
9917 || token_type == CPP_UTF8STRING)
9918 message = catenate_messages (gmsgid, " before string constant");
9919 else if (token_type == CPP_NUMBER)
9920 message = catenate_messages (gmsgid, " before numeric constant");
9921 else if (token_type == CPP_NAME)
9923 message = catenate_messages (gmsgid, " before %qE");
9924 error (message, value);
9925 free (message);
9926 message = NULL;
9928 else if (token_type == CPP_PRAGMA)
9929 message = catenate_messages (gmsgid, " before %<#pragma%>");
9930 else if (token_type == CPP_PRAGMA_EOL)
9931 message = catenate_messages (gmsgid, " before end of line");
9932 else if (token_type == CPP_DECLTYPE)
9933 message = catenate_messages (gmsgid, " before %<decltype%>");
9934 else if (token_type < N_TTYPES)
9936 message = catenate_messages (gmsgid, " before %qs token");
9937 error (message, cpp_type2name (token_type, token_flags));
9938 free (message);
9939 message = NULL;
9941 else
9942 error (gmsgid);
9944 if (message)
9946 error (message);
9947 free (message);
9949 #undef catenate_messages
9952 /* Return the gcc option code associated with the reason for a cpp
9953 message, or 0 if none. */
9955 static int
9956 c_option_controlling_cpp_error (int reason)
9958 const struct cpp_reason_option_codes_t *entry;
9960 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
9962 if (entry->reason == reason)
9963 return entry->option_code;
9965 return 0;
9968 /* Callback from cpp_error for PFILE to print diagnostics from the
9969 preprocessor. The diagnostic is of type LEVEL, with REASON set
9970 to the reason code if LEVEL is represents a warning, at location
9971 LOCATION unless this is after lexing and the compiler's location
9972 should be used instead, with column number possibly overridden by
9973 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9974 the arguments. Returns true if a diagnostic was emitted, false
9975 otherwise. */
9977 bool
9978 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9979 location_t location, unsigned int column_override,
9980 const char *msg, va_list *ap)
9982 diagnostic_info diagnostic;
9983 diagnostic_t dlevel;
9984 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9985 bool ret;
9987 switch (level)
9989 case CPP_DL_WARNING_SYSHDR:
9990 if (flag_no_output)
9991 return false;
9992 global_dc->dc_warn_system_headers = 1;
9993 /* Fall through. */
9994 case CPP_DL_WARNING:
9995 if (flag_no_output)
9996 return false;
9997 dlevel = DK_WARNING;
9998 break;
9999 case CPP_DL_PEDWARN:
10000 if (flag_no_output && !flag_pedantic_errors)
10001 return false;
10002 dlevel = DK_PEDWARN;
10003 break;
10004 case CPP_DL_ERROR:
10005 dlevel = DK_ERROR;
10006 break;
10007 case CPP_DL_ICE:
10008 dlevel = DK_ICE;
10009 break;
10010 case CPP_DL_NOTE:
10011 dlevel = DK_NOTE;
10012 break;
10013 case CPP_DL_FATAL:
10014 dlevel = DK_FATAL;
10015 break;
10016 default:
10017 gcc_unreachable ();
10019 if (done_lexing)
10020 location = input_location;
10021 diagnostic_set_info_translated (&diagnostic, msg, ap,
10022 location, dlevel);
10023 if (column_override)
10024 diagnostic_override_column (&diagnostic, column_override);
10025 diagnostic_override_option_index (&diagnostic,
10026 c_option_controlling_cpp_error (reason));
10027 ret = report_diagnostic (&diagnostic);
10028 if (level == CPP_DL_WARNING_SYSHDR)
10029 global_dc->dc_warn_system_headers = save_warn_system_headers;
10030 return ret;
10033 /* Convert a character from the host to the target execution character
10034 set. cpplib handles this, mostly. */
10036 HOST_WIDE_INT
10037 c_common_to_target_charset (HOST_WIDE_INT c)
10039 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10040 zero-extended under -fno-signed-char. cpplib insists that characters
10041 and character constants are always unsigned. Hence we must convert
10042 back and forth. */
10043 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10045 uc = cpp_host_to_exec_charset (parse_in, uc);
10047 if (flag_signed_char)
10048 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10049 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10050 else
10051 return uc;
10054 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10055 references with an INDIRECT_REF of a constant at the bottom; much like the
10056 traditional rendering of offsetof as a macro. Return the folded result. */
10058 tree
10059 fold_offsetof_1 (tree expr)
10061 tree base, off, t;
10063 switch (TREE_CODE (expr))
10065 case ERROR_MARK:
10066 return expr;
10068 case VAR_DECL:
10069 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10070 return error_mark_node;
10072 case CALL_EXPR:
10073 case TARGET_EXPR:
10074 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10075 return error_mark_node;
10077 case NOP_EXPR:
10078 case INDIRECT_REF:
10079 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10081 error ("cannot apply %<offsetof%> to a non constant address");
10082 return error_mark_node;
10084 return TREE_OPERAND (expr, 0);
10086 case COMPONENT_REF:
10087 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10088 if (base == error_mark_node)
10089 return base;
10091 t = TREE_OPERAND (expr, 1);
10092 if (DECL_C_BIT_FIELD (t))
10094 error ("attempt to take address of bit-field structure "
10095 "member %qD", t);
10096 return error_mark_node;
10098 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10099 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10100 / BITS_PER_UNIT));
10101 break;
10103 case ARRAY_REF:
10104 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10105 if (base == error_mark_node)
10106 return base;
10108 t = TREE_OPERAND (expr, 1);
10110 /* Check if the offset goes beyond the upper bound of the array. */
10111 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10113 tree upbound = array_ref_up_bound (expr);
10114 if (upbound != NULL_TREE
10115 && TREE_CODE (upbound) == INTEGER_CST
10116 && !tree_int_cst_equal (upbound,
10117 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10119 upbound = size_binop (PLUS_EXPR, upbound,
10120 build_int_cst (TREE_TYPE (upbound), 1));
10121 if (tree_int_cst_lt (upbound, t))
10123 tree v;
10125 for (v = TREE_OPERAND (expr, 0);
10126 TREE_CODE (v) == COMPONENT_REF;
10127 v = TREE_OPERAND (v, 0))
10128 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10129 == RECORD_TYPE)
10131 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10132 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10133 if (TREE_CODE (fld_chain) == FIELD_DECL)
10134 break;
10136 if (fld_chain)
10137 break;
10139 /* Don't warn if the array might be considered a poor
10140 man's flexible array member with a very permissive
10141 definition thereof. */
10142 if (TREE_CODE (v) == ARRAY_REF
10143 || TREE_CODE (v) == COMPONENT_REF)
10144 warning (OPT_Warray_bounds,
10145 "index %E denotes an offset "
10146 "greater than size of %qT",
10147 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10152 t = convert (sizetype, t);
10153 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10154 break;
10156 case COMPOUND_EXPR:
10157 /* Handle static members of volatile structs. */
10158 t = TREE_OPERAND (expr, 1);
10159 gcc_assert (TREE_CODE (t) == VAR_DECL);
10160 return fold_offsetof_1 (t);
10162 default:
10163 gcc_unreachable ();
10166 return fold_build_pointer_plus (base, off);
10169 /* Likewise, but convert it to the return type of offsetof. */
10171 tree
10172 fold_offsetof (tree expr)
10174 return convert (size_type_node, fold_offsetof_1 (expr));
10177 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10178 expression, because B will always be true. */
10180 void
10181 warn_for_omitted_condop (location_t location, tree cond)
10183 if (truth_value_p (TREE_CODE (cond)))
10184 warning_at (location, OPT_Wparentheses,
10185 "the omitted middle operand in ?: will always be %<true%>, "
10186 "suggest explicit middle operand");
10189 /* Give an error for storing into ARG, which is 'const'. USE indicates
10190 how ARG was being used. */
10192 void
10193 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10195 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10196 || use == lv_asm);
10197 /* Using this macro rather than (for example) arrays of messages
10198 ensures that all the format strings are checked at compile
10199 time. */
10200 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10201 : (use == lv_increment ? (I) \
10202 : (use == lv_decrement ? (D) : (AS))))
10203 if (TREE_CODE (arg) == COMPONENT_REF)
10205 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10206 error_at (loc, READONLY_MSG (G_("assignment of member "
10207 "%qD in read-only object"),
10208 G_("increment of member "
10209 "%qD in read-only object"),
10210 G_("decrement of member "
10211 "%qD in read-only object"),
10212 G_("member %qD in read-only object "
10213 "used as %<asm%> output")),
10214 TREE_OPERAND (arg, 1));
10215 else
10216 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10217 G_("increment of read-only member %qD"),
10218 G_("decrement of read-only member %qD"),
10219 G_("read-only member %qD used as %<asm%> output")),
10220 TREE_OPERAND (arg, 1));
10222 else if (TREE_CODE (arg) == VAR_DECL)
10223 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10224 G_("increment of read-only variable %qD"),
10225 G_("decrement of read-only variable %qD"),
10226 G_("read-only variable %qD used as %<asm%> output")),
10227 arg);
10228 else if (TREE_CODE (arg) == PARM_DECL)
10229 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10230 G_("increment of read-only parameter %qD"),
10231 G_("decrement of read-only parameter %qD"),
10232 G_("read-only parameter %qD use as %<asm%> output")),
10233 arg);
10234 else if (TREE_CODE (arg) == RESULT_DECL)
10236 gcc_assert (c_dialect_cxx ());
10237 error_at (loc, READONLY_MSG (G_("assignment of "
10238 "read-only named return value %qD"),
10239 G_("increment of "
10240 "read-only named return value %qD"),
10241 G_("decrement of "
10242 "read-only named return value %qD"),
10243 G_("read-only named return value %qD "
10244 "used as %<asm%>output")),
10245 arg);
10247 else if (TREE_CODE (arg) == FUNCTION_DECL)
10248 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10249 G_("increment of function %qD"),
10250 G_("decrement of function %qD"),
10251 G_("function %qD used as %<asm%> output")),
10252 arg);
10253 else
10254 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10255 G_("increment of read-only location %qE"),
10256 G_("decrement of read-only location %qE"),
10257 G_("read-only location %qE used as %<asm%> output")),
10258 arg);
10261 /* Print an error message for an invalid lvalue. USE says
10262 how the lvalue is being used and so selects the error message. LOC
10263 is the location for the error. */
10265 void
10266 lvalue_error (location_t loc, enum lvalue_use use)
10268 switch (use)
10270 case lv_assign:
10271 error_at (loc, "lvalue required as left operand of assignment");
10272 break;
10273 case lv_increment:
10274 error_at (loc, "lvalue required as increment operand");
10275 break;
10276 case lv_decrement:
10277 error_at (loc, "lvalue required as decrement operand");
10278 break;
10279 case lv_addressof:
10280 error_at (loc, "lvalue required as unary %<&%> operand");
10281 break;
10282 case lv_asm:
10283 error_at (loc, "lvalue required in asm statement");
10284 break;
10285 default:
10286 gcc_unreachable ();
10290 /* Print an error message for an invalid indirection of type TYPE.
10291 ERRSTRING is the name of the operator for the indirection. */
10293 void
10294 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10296 switch (errstring)
10298 case RO_NULL:
10299 gcc_assert (c_dialect_cxx ());
10300 error_at (loc, "invalid type argument (have %qT)", type);
10301 break;
10302 case RO_ARRAY_INDEXING:
10303 error_at (loc,
10304 "invalid type argument of array indexing (have %qT)",
10305 type);
10306 break;
10307 case RO_UNARY_STAR:
10308 error_at (loc,
10309 "invalid type argument of unary %<*%> (have %qT)",
10310 type);
10311 break;
10312 case RO_ARROW:
10313 error_at (loc,
10314 "invalid type argument of %<->%> (have %qT)",
10315 type);
10316 break;
10317 case RO_ARROW_STAR:
10318 error_at (loc,
10319 "invalid type argument of %<->*%> (have %qT)",
10320 type);
10321 break;
10322 case RO_IMPLICIT_CONVERSION:
10323 error_at (loc,
10324 "invalid type argument of implicit conversion (have %qT)",
10325 type);
10326 break;
10327 default:
10328 gcc_unreachable ();
10332 /* *PTYPE is an incomplete array. Complete it with a domain based on
10333 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10334 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10335 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10338 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10340 tree maxindex, type, main_type, elt, unqual_elt;
10341 int failure = 0, quals;
10342 hashval_t hashcode = 0;
10343 bool overflow_p = false;
10345 maxindex = size_zero_node;
10346 if (initial_value)
10348 if (TREE_CODE (initial_value) == STRING_CST)
10350 int eltsize
10351 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10352 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10354 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10356 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10358 if (vec_safe_is_empty (v))
10360 if (pedantic)
10361 failure = 3;
10362 maxindex = ssize_int (-1);
10364 else
10366 tree curindex;
10367 unsigned HOST_WIDE_INT cnt;
10368 constructor_elt *ce;
10369 bool fold_p = false;
10371 if ((*v)[0].index)
10372 maxindex = (*v)[0].index, fold_p = true;
10374 curindex = maxindex;
10376 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10378 bool curfold_p = false;
10379 if (ce->index)
10380 curindex = ce->index, curfold_p = true;
10381 else
10383 if (fold_p)
10385 /* Since we treat size types now as ordinary
10386 unsigned types, we need an explicit overflow
10387 check. */
10388 tree orig = curindex;
10389 curindex = fold_convert (sizetype, curindex);
10390 overflow_p |= tree_int_cst_lt (curindex, orig);
10392 curindex = size_binop (PLUS_EXPR, curindex,
10393 size_one_node);
10395 if (tree_int_cst_lt (maxindex, curindex))
10396 maxindex = curindex, fold_p = curfold_p;
10398 if (fold_p)
10400 tree orig = maxindex;
10401 maxindex = fold_convert (sizetype, maxindex);
10402 overflow_p |= tree_int_cst_lt (maxindex, orig);
10406 else
10408 /* Make an error message unless that happened already. */
10409 if (initial_value != error_mark_node)
10410 failure = 1;
10413 else
10415 failure = 2;
10416 if (!do_default)
10417 return failure;
10420 type = *ptype;
10421 elt = TREE_TYPE (type);
10422 quals = TYPE_QUALS (strip_array_types (elt));
10423 if (quals == 0)
10424 unqual_elt = elt;
10425 else
10426 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10428 /* Using build_distinct_type_copy and modifying things afterward instead
10429 of using build_array_type to create a new type preserves all of the
10430 TYPE_LANG_FLAG_? bits that the front end may have set. */
10431 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10432 TREE_TYPE (main_type) = unqual_elt;
10433 TYPE_DOMAIN (main_type)
10434 = build_range_type (TREE_TYPE (maxindex),
10435 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10436 layout_type (main_type);
10438 /* Make sure we have the canonical MAIN_TYPE. */
10439 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10440 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10441 hashcode);
10442 main_type = type_hash_canon (hashcode, main_type);
10444 /* Fix the canonical type. */
10445 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10446 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10447 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10448 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10449 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10450 != TYPE_DOMAIN (main_type)))
10451 TYPE_CANONICAL (main_type)
10452 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10453 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10454 else
10455 TYPE_CANONICAL (main_type) = main_type;
10457 if (quals == 0)
10458 type = main_type;
10459 else
10460 type = c_build_qualified_type (main_type, quals);
10462 if (COMPLETE_TYPE_P (type)
10463 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10464 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10466 error ("size of array is too large");
10467 /* If we proceed with the array type as it is, we'll eventually
10468 crash in tree_to_[su]hwi(). */
10469 type = error_mark_node;
10472 *ptype = type;
10473 return failure;
10476 /* Like c_mark_addressable but don't check register qualifier. */
10477 void
10478 c_common_mark_addressable_vec (tree t)
10480 while (handled_component_p (t))
10481 t = TREE_OPERAND (t, 0);
10482 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10483 return;
10484 TREE_ADDRESSABLE (t) = 1;
10489 /* Used to help initialize the builtin-types.def table. When a type of
10490 the correct size doesn't exist, use error_mark_node instead of NULL.
10491 The later results in segfaults even when a decl using the type doesn't
10492 get invoked. */
10494 tree
10495 builtin_type_for_size (int size, bool unsignedp)
10497 tree type = c_common_type_for_size (size, unsignedp);
10498 return type ? type : error_mark_node;
10501 /* A helper function for resolve_overloaded_builtin in resolving the
10502 overloaded __sync_ builtins. Returns a positive power of 2 if the
10503 first operand of PARAMS is a pointer to a supported data type.
10504 Returns 0 if an error is encountered. */
10506 static int
10507 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10509 tree type;
10510 int size;
10512 if (!params)
10514 error ("too few arguments to function %qE", function);
10515 return 0;
10518 type = TREE_TYPE ((*params)[0]);
10519 if (TREE_CODE (type) == ARRAY_TYPE)
10521 /* Force array-to-pointer decay for C++. */
10522 gcc_assert (c_dialect_cxx());
10523 (*params)[0] = default_conversion ((*params)[0]);
10524 type = TREE_TYPE ((*params)[0]);
10526 if (TREE_CODE (type) != POINTER_TYPE)
10527 goto incompatible;
10529 type = TREE_TYPE (type);
10530 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10531 goto incompatible;
10533 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10534 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10535 return size;
10537 incompatible:
10538 error ("incompatible type for argument %d of %qE", 1, function);
10539 return 0;
10542 /* A helper function for resolve_overloaded_builtin. Adds casts to
10543 PARAMS to make arguments match up with those of FUNCTION. Drops
10544 the variadic arguments at the end. Returns false if some error
10545 was encountered; true on success. */
10547 static bool
10548 sync_resolve_params (location_t loc, tree orig_function, tree function,
10549 vec<tree, va_gc> *params, bool orig_format)
10551 function_args_iterator iter;
10552 tree ptype;
10553 unsigned int parmnum;
10555 function_args_iter_init (&iter, TREE_TYPE (function));
10556 /* We've declared the implementation functions to use "volatile void *"
10557 as the pointer parameter, so we shouldn't get any complaints from the
10558 call to check_function_arguments what ever type the user used. */
10559 function_args_iter_next (&iter);
10560 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10561 ptype = TYPE_MAIN_VARIANT (ptype);
10563 /* For the rest of the values, we need to cast these to FTYPE, so that we
10564 don't get warnings for passing pointer types, etc. */
10565 parmnum = 0;
10566 while (1)
10568 tree val, arg_type;
10570 arg_type = function_args_iter_cond (&iter);
10571 /* XXX void_type_node belies the abstraction. */
10572 if (arg_type == void_type_node)
10573 break;
10575 ++parmnum;
10576 if (params->length () <= parmnum)
10578 error_at (loc, "too few arguments to function %qE", orig_function);
10579 return false;
10582 /* Only convert parameters if arg_type is unsigned integer type with
10583 new format sync routines, i.e. don't attempt to convert pointer
10584 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10585 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10586 kinds). */
10587 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10589 /* Ideally for the first conversion we'd use convert_for_assignment
10590 so that we get warnings for anything that doesn't match the pointer
10591 type. This isn't portable across the C and C++ front ends atm. */
10592 val = (*params)[parmnum];
10593 val = convert (ptype, val);
10594 val = convert (arg_type, val);
10595 (*params)[parmnum] = val;
10598 function_args_iter_next (&iter);
10601 /* __atomic routines are not variadic. */
10602 if (!orig_format && params->length () != parmnum + 1)
10604 error_at (loc, "too many arguments to function %qE", orig_function);
10605 return false;
10608 /* The definition of these primitives is variadic, with the remaining
10609 being "an optional list of variables protected by the memory barrier".
10610 No clue what that's supposed to mean, precisely, but we consider all
10611 call-clobbered variables to be protected so we're safe. */
10612 params->truncate (parmnum + 1);
10614 return true;
10617 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10618 RESULT to make it match the type of the first pointer argument in
10619 PARAMS. */
10621 static tree
10622 sync_resolve_return (tree first_param, tree result, bool orig_format)
10624 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10625 tree rtype = TREE_TYPE (result);
10626 ptype = TYPE_MAIN_VARIANT (ptype);
10628 /* New format doesn't require casting unless the types are the same size. */
10629 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10630 return convert (ptype, result);
10631 else
10632 return result;
10635 /* This function verifies the PARAMS to generic atomic FUNCTION.
10636 It returns the size if all the parameters are the same size, otherwise
10637 0 is returned if the parameters are invalid. */
10639 static int
10640 get_atomic_generic_size (location_t loc, tree function,
10641 vec<tree, va_gc> *params)
10643 unsigned int n_param;
10644 unsigned int n_model;
10645 unsigned int x;
10646 int size_0;
10647 tree type_0;
10649 /* Determine the parameter makeup. */
10650 switch (DECL_FUNCTION_CODE (function))
10652 case BUILT_IN_ATOMIC_EXCHANGE:
10653 n_param = 4;
10654 n_model = 1;
10655 break;
10656 case BUILT_IN_ATOMIC_LOAD:
10657 case BUILT_IN_ATOMIC_STORE:
10658 n_param = 3;
10659 n_model = 1;
10660 break;
10661 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10662 n_param = 6;
10663 n_model = 2;
10664 break;
10665 default:
10666 gcc_unreachable ();
10669 if (vec_safe_length (params) != n_param)
10671 error_at (loc, "incorrect number of arguments to function %qE", function);
10672 return 0;
10675 /* Get type of first parameter, and determine its size. */
10676 type_0 = TREE_TYPE ((*params)[0]);
10677 if (TREE_CODE (type_0) == ARRAY_TYPE)
10679 /* Force array-to-pointer decay for C++. */
10680 gcc_assert (c_dialect_cxx());
10681 (*params)[0] = default_conversion ((*params)[0]);
10682 type_0 = TREE_TYPE ((*params)[0]);
10684 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10686 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10687 function);
10688 return 0;
10691 /* Types must be compile time constant sizes. */
10692 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10694 error_at (loc,
10695 "argument 1 of %qE must be a pointer to a constant size type",
10696 function);
10697 return 0;
10700 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10702 /* Zero size objects are not allowed. */
10703 if (size_0 == 0)
10705 error_at (loc,
10706 "argument 1 of %qE must be a pointer to a nonzero size object",
10707 function);
10708 return 0;
10711 /* Check each other parameter is a pointer and the same size. */
10712 for (x = 0; x < n_param - n_model; x++)
10714 int size;
10715 tree type = TREE_TYPE ((*params)[x]);
10716 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10717 if (n_param == 6 && x == 3)
10718 continue;
10719 if (!POINTER_TYPE_P (type))
10721 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10722 function);
10723 return 0;
10725 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10726 size = type_size ? tree_to_uhwi (type_size) : 0;
10727 if (size != size_0)
10729 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10730 function);
10731 return 0;
10735 /* Check memory model parameters for validity. */
10736 for (x = n_param - n_model ; x < n_param; x++)
10738 tree p = (*params)[x];
10739 if (TREE_CODE (p) == INTEGER_CST)
10741 int i = tree_to_uhwi (p);
10742 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10744 warning_at (loc, OPT_Winvalid_memory_model,
10745 "invalid memory model argument %d of %qE", x + 1,
10746 function);
10749 else
10750 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10752 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10753 function);
10754 return 0;
10758 return size_0;
10762 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10763 at the beginning of the parameter list PARAMS representing the size of the
10764 objects. This is to match the library ABI requirement. LOC is the location
10765 of the function call.
10766 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10767 returned to allow the external call to be constructed. */
10769 static tree
10770 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10771 vec<tree, va_gc> *params)
10773 tree size_node;
10775 /* Insert a SIZE_T parameter as the first param. If there isn't
10776 enough space, allocate a new vector and recursively re-build with that. */
10777 if (!params->space (1))
10779 unsigned int z, len;
10780 vec<tree, va_gc> *v;
10781 tree f;
10783 len = params->length ();
10784 vec_alloc (v, len + 1);
10785 v->quick_push (build_int_cst (size_type_node, n));
10786 for (z = 0; z < len; z++)
10787 v->quick_push ((*params)[z]);
10788 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10789 vec_free (v);
10790 return f;
10793 /* Add the size parameter and leave as a function call for processing. */
10794 size_node = build_int_cst (size_type_node, n);
10795 params->quick_insert (0, size_node);
10796 return NULL_TREE;
10800 /* Return whether atomic operations for naturally aligned N-byte
10801 arguments are supported, whether inline or through libatomic. */
10802 static bool
10803 atomic_size_supported_p (int n)
10805 switch (n)
10807 case 1:
10808 case 2:
10809 case 4:
10810 case 8:
10811 return true;
10813 case 16:
10814 return targetm.scalar_mode_supported_p (TImode);
10816 default:
10817 return false;
10821 /* This will process an __atomic_exchange function call, determine whether it
10822 needs to be mapped to the _N variation, or turned into a library call.
10823 LOC is the location of the builtin call.
10824 FUNCTION is the DECL that has been invoked;
10825 PARAMS is the argument list for the call. The return value is non-null
10826 TRUE is returned if it is translated into the proper format for a call to the
10827 external library, and NEW_RETURN is set the tree for that function.
10828 FALSE is returned if processing for the _N variation is required, and
10829 NEW_RETURN is set to the the return value the result is copied into. */
10830 static bool
10831 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10832 vec<tree, va_gc> *params, tree *new_return)
10834 tree p0, p1, p2, p3;
10835 tree I_type, I_type_ptr;
10836 int n = get_atomic_generic_size (loc, function, params);
10838 /* Size of 0 is an error condition. */
10839 if (n == 0)
10841 *new_return = error_mark_node;
10842 return true;
10845 /* If not a lock-free size, change to the library generic format. */
10846 if (!atomic_size_supported_p (n))
10848 *new_return = add_atomic_size_parameter (n, loc, function, params);
10849 return true;
10852 /* Otherwise there is a lockfree match, transform the call from:
10853 void fn(T* mem, T* desired, T* return, model)
10854 into
10855 *return = (T) (fn (In* mem, (In) *desired, model)) */
10857 p0 = (*params)[0];
10858 p1 = (*params)[1];
10859 p2 = (*params)[2];
10860 p3 = (*params)[3];
10862 /* Create pointer to appropriate size. */
10863 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10864 I_type_ptr = build_pointer_type (I_type);
10866 /* Convert object pointer to required type. */
10867 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10868 (*params)[0] = p0;
10869 /* Convert new value to required type, and dereference it. */
10870 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10871 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10872 (*params)[1] = p1;
10874 /* Move memory model to the 3rd position, and end param list. */
10875 (*params)[2] = p3;
10876 params->truncate (3);
10878 /* Convert return pointer and dereference it for later assignment. */
10879 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10881 return false;
10885 /* This will process an __atomic_compare_exchange function call, determine
10886 whether it needs to be mapped to the _N variation, or turned into a lib call.
10887 LOC is the location of the builtin call.
10888 FUNCTION is the DECL that has been invoked;
10889 PARAMS is the argument list for the call. The return value is non-null
10890 TRUE is returned if it is translated into the proper format for a call to the
10891 external library, and NEW_RETURN is set the tree for that function.
10892 FALSE is returned if processing for the _N variation is required. */
10894 static bool
10895 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10896 vec<tree, va_gc> *params,
10897 tree *new_return)
10899 tree p0, p1, p2;
10900 tree I_type, I_type_ptr;
10901 int n = get_atomic_generic_size (loc, function, params);
10903 /* Size of 0 is an error condition. */
10904 if (n == 0)
10906 *new_return = error_mark_node;
10907 return true;
10910 /* If not a lock-free size, change to the library generic format. */
10911 if (!atomic_size_supported_p (n))
10913 /* The library generic format does not have the weak parameter, so
10914 remove it from the param list. Since a parameter has been removed,
10915 we can be sure that there is room for the SIZE_T parameter, meaning
10916 there will not be a recursive rebuilding of the parameter list, so
10917 there is no danger this will be done twice. */
10918 if (n > 0)
10920 (*params)[3] = (*params)[4];
10921 (*params)[4] = (*params)[5];
10922 params->truncate (5);
10924 *new_return = add_atomic_size_parameter (n, loc, function, params);
10925 return true;
10928 /* Otherwise, there is a match, so the call needs to be transformed from:
10929 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10930 into
10931 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10933 p0 = (*params)[0];
10934 p1 = (*params)[1];
10935 p2 = (*params)[2];
10937 /* Create pointer to appropriate size. */
10938 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10939 I_type_ptr = build_pointer_type (I_type);
10941 /* Convert object pointer to required type. */
10942 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10943 (*params)[0] = p0;
10945 /* Convert expected pointer to required type. */
10946 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10947 (*params)[1] = p1;
10949 /* Convert desired value to required type, and dereference it. */
10950 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10951 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10952 (*params)[2] = p2;
10954 /* The rest of the parameters are fine. NULL means no special return value
10955 processing.*/
10956 *new_return = NULL;
10957 return false;
10961 /* This will process an __atomic_load function call, determine whether it
10962 needs to be mapped to the _N variation, or turned into a library call.
10963 LOC is the location of the builtin call.
10964 FUNCTION is the DECL that has been invoked;
10965 PARAMS is the argument list for the call. The return value is non-null
10966 TRUE is returned if it is translated into the proper format for a call to the
10967 external library, and NEW_RETURN is set the tree for that function.
10968 FALSE is returned if processing for the _N variation is required, and
10969 NEW_RETURN is set to the the return value the result is copied into. */
10971 static bool
10972 resolve_overloaded_atomic_load (location_t loc, tree function,
10973 vec<tree, va_gc> *params, tree *new_return)
10975 tree p0, p1, p2;
10976 tree I_type, I_type_ptr;
10977 int n = get_atomic_generic_size (loc, function, params);
10979 /* Size of 0 is an error condition. */
10980 if (n == 0)
10982 *new_return = error_mark_node;
10983 return true;
10986 /* If not a lock-free size, change to the library generic format. */
10987 if (!atomic_size_supported_p (n))
10989 *new_return = add_atomic_size_parameter (n, loc, function, params);
10990 return true;
10993 /* Otherwise, there is a match, so the call needs to be transformed from:
10994 void fn(T* mem, T* return, model)
10995 into
10996 *return = (T) (fn ((In *) mem, model)) */
10998 p0 = (*params)[0];
10999 p1 = (*params)[1];
11000 p2 = (*params)[2];
11002 /* Create pointer to appropriate size. */
11003 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11004 I_type_ptr = build_pointer_type (I_type);
11006 /* Convert object pointer to required type. */
11007 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11008 (*params)[0] = p0;
11010 /* Move memory model to the 2nd position, and end param list. */
11011 (*params)[1] = p2;
11012 params->truncate (2);
11014 /* Convert return pointer and dereference it for later assignment. */
11015 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11017 return false;
11021 /* This will process an __atomic_store function call, determine whether it
11022 needs to be mapped to the _N variation, or turned into a library call.
11023 LOC is the location of the builtin call.
11024 FUNCTION is the DECL that has been invoked;
11025 PARAMS is the argument list for the call. The return value is non-null
11026 TRUE is returned if it is translated into the proper format for a call to the
11027 external library, and NEW_RETURN is set the tree for that function.
11028 FALSE is returned if processing for the _N variation is required, and
11029 NEW_RETURN is set to the the return value the result is copied into. */
11031 static bool
11032 resolve_overloaded_atomic_store (location_t loc, tree function,
11033 vec<tree, va_gc> *params, tree *new_return)
11035 tree p0, p1;
11036 tree I_type, I_type_ptr;
11037 int n = get_atomic_generic_size (loc, function, params);
11039 /* Size of 0 is an error condition. */
11040 if (n == 0)
11042 *new_return = error_mark_node;
11043 return true;
11046 /* If not a lock-free size, change to the library generic format. */
11047 if (!atomic_size_supported_p (n))
11049 *new_return = add_atomic_size_parameter (n, loc, function, params);
11050 return true;
11053 /* Otherwise, there is a match, so the call needs to be transformed from:
11054 void fn(T* mem, T* value, model)
11055 into
11056 fn ((In *) mem, (In) *value, model) */
11058 p0 = (*params)[0];
11059 p1 = (*params)[1];
11061 /* Create pointer to appropriate size. */
11062 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11063 I_type_ptr = build_pointer_type (I_type);
11065 /* Convert object pointer to required type. */
11066 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11067 (*params)[0] = p0;
11069 /* Convert new value to required type, and dereference it. */
11070 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11071 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11072 (*params)[1] = p1;
11074 /* The memory model is in the right spot already. Return is void. */
11075 *new_return = NULL_TREE;
11077 return false;
11081 /* Some builtin functions are placeholders for other expressions. This
11082 function should be called immediately after parsing the call expression
11083 before surrounding code has committed to the type of the expression.
11085 LOC is the location of the builtin call.
11087 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11088 PARAMS is the argument list for the call. The return value is non-null
11089 when expansion is complete, and null if normal processing should
11090 continue. */
11092 tree
11093 resolve_overloaded_builtin (location_t loc, tree function,
11094 vec<tree, va_gc> *params)
11096 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11097 bool orig_format = true;
11098 tree new_return = NULL_TREE;
11100 switch (DECL_BUILT_IN_CLASS (function))
11102 case BUILT_IN_NORMAL:
11103 break;
11104 case BUILT_IN_MD:
11105 if (targetm.resolve_overloaded_builtin)
11106 return targetm.resolve_overloaded_builtin (loc, function, params);
11107 else
11108 return NULL_TREE;
11109 default:
11110 return NULL_TREE;
11113 /* Handle BUILT_IN_NORMAL here. */
11114 switch (orig_code)
11116 case BUILT_IN_ATOMIC_EXCHANGE:
11117 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11118 case BUILT_IN_ATOMIC_LOAD:
11119 case BUILT_IN_ATOMIC_STORE:
11121 /* Handle these 4 together so that they can fall through to the next
11122 case if the call is transformed to an _N variant. */
11123 switch (orig_code)
11125 case BUILT_IN_ATOMIC_EXCHANGE:
11127 if (resolve_overloaded_atomic_exchange (loc, function, params,
11128 &new_return))
11129 return new_return;
11130 /* Change to the _N variant. */
11131 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11132 break;
11135 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11137 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11138 params,
11139 &new_return))
11140 return new_return;
11141 /* Change to the _N variant. */
11142 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11143 break;
11145 case BUILT_IN_ATOMIC_LOAD:
11147 if (resolve_overloaded_atomic_load (loc, function, params,
11148 &new_return))
11149 return new_return;
11150 /* Change to the _N variant. */
11151 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11152 break;
11154 case BUILT_IN_ATOMIC_STORE:
11156 if (resolve_overloaded_atomic_store (loc, function, params,
11157 &new_return))
11158 return new_return;
11159 /* Change to the _N variant. */
11160 orig_code = BUILT_IN_ATOMIC_STORE_N;
11161 break;
11163 default:
11164 gcc_unreachable ();
11166 /* Fallthrough to the normal processing. */
11168 case BUILT_IN_ATOMIC_EXCHANGE_N:
11169 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11170 case BUILT_IN_ATOMIC_LOAD_N:
11171 case BUILT_IN_ATOMIC_STORE_N:
11172 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11173 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11174 case BUILT_IN_ATOMIC_AND_FETCH_N:
11175 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11176 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11177 case BUILT_IN_ATOMIC_OR_FETCH_N:
11178 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11179 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11180 case BUILT_IN_ATOMIC_FETCH_AND_N:
11181 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11182 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11183 case BUILT_IN_ATOMIC_FETCH_OR_N:
11185 orig_format = false;
11186 /* Fallthru for parameter processing. */
11188 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11189 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11190 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11191 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11192 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11193 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11194 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11195 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11196 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11197 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11198 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11199 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11200 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11201 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11202 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11203 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11205 int n = sync_resolve_size (function, params);
11206 tree new_function, first_param, result;
11207 enum built_in_function fncode;
11209 if (n == 0)
11210 return error_mark_node;
11212 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11213 new_function = builtin_decl_explicit (fncode);
11214 if (!sync_resolve_params (loc, function, new_function, params,
11215 orig_format))
11216 return error_mark_node;
11218 first_param = (*params)[0];
11219 result = build_function_call_vec (loc, vNULL, new_function, params,
11220 NULL);
11221 if (result == error_mark_node)
11222 return result;
11223 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11224 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11225 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11226 result = sync_resolve_return (first_param, result, orig_format);
11228 /* If new_return is set, assign function to that expr and cast the
11229 result to void since the generic interface returned void. */
11230 if (new_return)
11232 /* Cast function result from I{1,2,4,8,16} to the required type. */
11233 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11234 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11235 result);
11236 TREE_SIDE_EFFECTS (result) = 1;
11237 protected_set_expr_location (result, loc);
11238 result = convert (void_type_node, result);
11240 return result;
11243 default:
11244 return NULL_TREE;
11248 /* vector_types_compatible_elements_p is used in type checks of vectors
11249 values used as operands of binary operators. Where it returns true, and
11250 the other checks of the caller succeed (being vector types in he first
11251 place, and matching number of elements), we can just treat the types
11252 as essentially the same.
11253 Contrast with vector_targets_convertible_p, which is used for vector
11254 pointer types, and vector_types_convertible_p, which will allow
11255 language-specific matches under the control of flag_lax_vector_conversions,
11256 and might still require a conversion. */
11257 /* True if vector types T1 and T2 can be inputs to the same binary
11258 operator without conversion.
11259 We don't check the overall vector size here because some of our callers
11260 want to give different error messages when the vectors are compatible
11261 except for the element count. */
11263 bool
11264 vector_types_compatible_elements_p (tree t1, tree t2)
11266 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11267 t1 = TREE_TYPE (t1);
11268 t2 = TREE_TYPE (t2);
11270 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11272 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11273 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11274 || c2 == FIXED_POINT_TYPE));
11276 t1 = c_common_signed_type (t1);
11277 t2 = c_common_signed_type (t2);
11278 /* Equality works here because c_common_signed_type uses
11279 TYPE_MAIN_VARIANT. */
11280 if (t1 == t2)
11281 return true;
11282 if (opaque && c1 == c2
11283 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11284 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11285 return true;
11286 return false;
11289 /* Check for missing format attributes on function pointers. LTYPE is
11290 the new type or left-hand side type. RTYPE is the old type or
11291 right-hand side type. Returns TRUE if LTYPE is missing the desired
11292 attribute. */
11294 bool
11295 check_missing_format_attribute (tree ltype, tree rtype)
11297 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11298 tree ra;
11300 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11301 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11302 break;
11303 if (ra)
11305 tree la;
11306 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11307 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11308 break;
11309 return !la;
11311 else
11312 return false;
11315 /* Subscripting with type char is likely to lose on a machine where
11316 chars are signed. So warn on any machine, but optionally. Don't
11317 warn for unsigned char since that type is safe. Don't warn for
11318 signed char because anyone who uses that must have done so
11319 deliberately. Furthermore, we reduce the false positive load by
11320 warning only for non-constant value of type char. */
11322 void
11323 warn_array_subscript_with_type_char (tree index)
11325 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11326 && TREE_CODE (index) != INTEGER_CST)
11327 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11330 /* Implement -Wparentheses for the unexpected C precedence rules, to
11331 cover cases like x + y << z which readers are likely to
11332 misinterpret. We have seen an expression in which CODE is a binary
11333 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11334 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11335 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11336 expression was not formed using a binary or unary operator, or it
11337 was enclosed in parentheses. */
11339 void
11340 warn_about_parentheses (location_t loc, enum tree_code code,
11341 enum tree_code code_left, tree arg_left,
11342 enum tree_code code_right, tree arg_right)
11344 if (!warn_parentheses)
11345 return;
11347 /* This macro tests that the expression ARG with original tree code
11348 CODE appears to be a boolean expression. or the result of folding a
11349 boolean expression. */
11350 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11351 (truth_value_p (TREE_CODE (ARG)) \
11352 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11353 /* Folding may create 0 or 1 integers from other expressions. */ \
11354 || ((CODE) != INTEGER_CST \
11355 && (integer_onep (ARG) || integer_zerop (ARG))))
11357 switch (code)
11359 case LSHIFT_EXPR:
11360 if (code_left == PLUS_EXPR)
11361 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11362 "suggest parentheses around %<+%> inside %<<<%>");
11363 else if (code_right == PLUS_EXPR)
11364 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11365 "suggest parentheses around %<+%> inside %<<<%>");
11366 else if (code_left == MINUS_EXPR)
11367 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11368 "suggest parentheses around %<-%> inside %<<<%>");
11369 else if (code_right == MINUS_EXPR)
11370 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11371 "suggest parentheses around %<-%> inside %<<<%>");
11372 return;
11374 case RSHIFT_EXPR:
11375 if (code_left == PLUS_EXPR)
11376 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11377 "suggest parentheses around %<+%> inside %<>>%>");
11378 else if (code_right == PLUS_EXPR)
11379 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11380 "suggest parentheses around %<+%> inside %<>>%>");
11381 else if (code_left == MINUS_EXPR)
11382 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11383 "suggest parentheses around %<-%> inside %<>>%>");
11384 else if (code_right == MINUS_EXPR)
11385 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11386 "suggest parentheses around %<-%> inside %<>>%>");
11387 return;
11389 case TRUTH_ORIF_EXPR:
11390 if (code_left == TRUTH_ANDIF_EXPR)
11391 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11392 "suggest parentheses around %<&&%> within %<||%>");
11393 else if (code_right == TRUTH_ANDIF_EXPR)
11394 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11395 "suggest parentheses around %<&&%> within %<||%>");
11396 return;
11398 case BIT_IOR_EXPR:
11399 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11400 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11401 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11402 "suggest parentheses around arithmetic in operand of %<|%>");
11403 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11404 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11405 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11406 "suggest parentheses around arithmetic in operand of %<|%>");
11407 /* Check cases like x|y==z */
11408 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11409 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11410 "suggest parentheses around comparison in operand of %<|%>");
11411 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11412 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11413 "suggest parentheses around comparison in operand of %<|%>");
11414 /* Check cases like !x | y */
11415 else if (code_left == TRUTH_NOT_EXPR
11416 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11417 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11418 "suggest parentheses around operand of "
11419 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11420 return;
11422 case BIT_XOR_EXPR:
11423 if (code_left == BIT_AND_EXPR
11424 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11425 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11426 "suggest parentheses around arithmetic in operand of %<^%>");
11427 else if (code_right == BIT_AND_EXPR
11428 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11429 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11430 "suggest parentheses around arithmetic in operand of %<^%>");
11431 /* Check cases like x^y==z */
11432 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11433 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11434 "suggest parentheses around comparison in operand of %<^%>");
11435 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11436 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11437 "suggest parentheses around comparison in operand of %<^%>");
11438 return;
11440 case BIT_AND_EXPR:
11441 if (code_left == PLUS_EXPR)
11442 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11443 "suggest parentheses around %<+%> in operand of %<&%>");
11444 else if (code_right == PLUS_EXPR)
11445 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11446 "suggest parentheses around %<+%> in operand of %<&%>");
11447 else if (code_left == MINUS_EXPR)
11448 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11449 "suggest parentheses around %<-%> in operand of %<&%>");
11450 else if (code_right == MINUS_EXPR)
11451 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11452 "suggest parentheses around %<-%> in operand of %<&%>");
11453 /* Check cases like x&y==z */
11454 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11455 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11456 "suggest parentheses around comparison in operand of %<&%>");
11457 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11458 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11459 "suggest parentheses around comparison in operand of %<&%>");
11460 /* Check cases like !x & y */
11461 else if (code_left == TRUTH_NOT_EXPR
11462 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11463 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11464 "suggest parentheses around operand of "
11465 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11466 return;
11468 case EQ_EXPR:
11469 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11470 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11471 "suggest parentheses around comparison in operand of %<==%>");
11472 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11473 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11474 "suggest parentheses around comparison in operand of %<==%>");
11475 return;
11476 case NE_EXPR:
11477 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11478 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11479 "suggest parentheses around comparison in operand of %<!=%>");
11480 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11481 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11482 "suggest parentheses around comparison in operand of %<!=%>");
11483 return;
11485 default:
11486 if (TREE_CODE_CLASS (code) == tcc_comparison)
11488 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11489 && code_left != NE_EXPR && code_left != EQ_EXPR
11490 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11491 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11492 "comparisons like %<X<=Y<=Z%> do not "
11493 "have their mathematical meaning");
11494 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11495 && code_right != NE_EXPR && code_right != EQ_EXPR
11496 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11497 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11498 "comparisons like %<X<=Y<=Z%> do not "
11499 "have their mathematical meaning");
11501 return;
11503 #undef NOT_A_BOOLEAN_EXPR_P
11506 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11508 void
11509 warn_for_unused_label (tree label)
11511 if (!TREE_USED (label))
11513 if (DECL_INITIAL (label))
11514 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11515 else
11516 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11520 /* Warn for division by zero according to the value of DIVISOR. LOC
11521 is the location of the division operator. */
11523 void
11524 warn_for_div_by_zero (location_t loc, tree divisor)
11526 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11527 about division by zero. Do not issue a warning if DIVISOR has a
11528 floating-point type, since we consider 0.0/0.0 a valid way of
11529 generating a NaN. */
11530 if (c_inhibit_evaluation_warnings == 0
11531 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11532 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11535 /* Subroutine of build_binary_op. Give warnings for comparisons
11536 between signed and unsigned quantities that may fail. Do the
11537 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11538 so that casts will be considered, but default promotions won't
11541 LOCATION is the location of the comparison operator.
11543 The arguments of this function map directly to local variables
11544 of build_binary_op. */
11546 void
11547 warn_for_sign_compare (location_t location,
11548 tree orig_op0, tree orig_op1,
11549 tree op0, tree op1,
11550 tree result_type, enum tree_code resultcode)
11552 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11553 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11554 int unsignedp0, unsignedp1;
11556 /* In C++, check for comparison of different enum types. */
11557 if (c_dialect_cxx()
11558 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11559 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11560 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11561 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11563 warning_at (location,
11564 OPT_Wsign_compare, "comparison between types %qT and %qT",
11565 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11568 /* Do not warn if the comparison is being done in a signed type,
11569 since the signed type will only be chosen if it can represent
11570 all the values of the unsigned type. */
11571 if (!TYPE_UNSIGNED (result_type))
11572 /* OK */;
11573 /* Do not warn if both operands are unsigned. */
11574 else if (op0_signed == op1_signed)
11575 /* OK */;
11576 else
11578 tree sop, uop, base_type;
11579 bool ovf;
11581 if (op0_signed)
11582 sop = orig_op0, uop = orig_op1;
11583 else
11584 sop = orig_op1, uop = orig_op0;
11586 STRIP_TYPE_NOPS (sop);
11587 STRIP_TYPE_NOPS (uop);
11588 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11589 ? TREE_TYPE (result_type) : result_type);
11591 /* Do not warn if the signed quantity is an unsuffixed integer
11592 literal (or some static constant expression involving such
11593 literals or a conditional expression involving such literals)
11594 and it is non-negative. */
11595 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11596 /* OK */;
11597 /* Do not warn if the comparison is an equality operation, the
11598 unsigned quantity is an integral constant, and it would fit
11599 in the result if the result were signed. */
11600 else if (TREE_CODE (uop) == INTEGER_CST
11601 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11602 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11603 /* OK */;
11604 /* In C, do not warn if the unsigned quantity is an enumeration
11605 constant and its maximum value would fit in the result if the
11606 result were signed. */
11607 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11608 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11609 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11610 c_common_signed_type (base_type)))
11611 /* OK */;
11612 else
11613 warning_at (location,
11614 OPT_Wsign_compare,
11615 "comparison between signed and unsigned integer expressions");
11618 /* Warn if two unsigned values are being compared in a size larger
11619 than their original size, and one (and only one) is the result of
11620 a `~' operator. This comparison will always fail.
11622 Also warn if one operand is a constant, and the constant does not
11623 have all bits set that are set in the ~ operand when it is
11624 extended. */
11626 op0 = c_common_get_narrower (op0, &unsignedp0);
11627 op1 = c_common_get_narrower (op1, &unsignedp1);
11629 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11630 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11632 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11633 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11634 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11635 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11637 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11639 tree primop;
11640 HOST_WIDE_INT constant, mask;
11641 int unsignedp;
11642 unsigned int bits;
11644 if (tree_fits_shwi_p (op0))
11646 primop = op1;
11647 unsignedp = unsignedp1;
11648 constant = tree_to_shwi (op0);
11650 else
11652 primop = op0;
11653 unsignedp = unsignedp0;
11654 constant = tree_to_shwi (op1);
11657 bits = TYPE_PRECISION (TREE_TYPE (primop));
11658 if (bits < TYPE_PRECISION (result_type)
11659 && bits < HOST_BITS_PER_LONG && unsignedp)
11661 mask = (~ (HOST_WIDE_INT) 0) << bits;
11662 if ((mask & constant) != mask)
11664 if (constant == 0)
11665 warning_at (location, OPT_Wsign_compare,
11666 "promoted ~unsigned is always non-zero");
11667 else
11668 warning_at (location, OPT_Wsign_compare,
11669 "comparison of promoted ~unsigned with constant");
11673 else if (unsignedp0 && unsignedp1
11674 && (TYPE_PRECISION (TREE_TYPE (op0))
11675 < TYPE_PRECISION (result_type))
11676 && (TYPE_PRECISION (TREE_TYPE (op1))
11677 < TYPE_PRECISION (result_type)))
11678 warning_at (location, OPT_Wsign_compare,
11679 "comparison of promoted ~unsigned with unsigned");
11683 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11684 type via c_common_type. If -Wdouble-promotion is in use, and the
11685 conditions for warning have been met, issue a warning. GMSGID is
11686 the warning message. It must have two %T specifiers for the type
11687 that was converted (generally "float") and the type to which it was
11688 converted (generally "double), respectively. LOC is the location
11689 to which the awrning should refer. */
11691 void
11692 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11693 const char *gmsgid, location_t loc)
11695 tree source_type;
11697 if (!warn_double_promotion)
11698 return;
11699 /* If the conversion will not occur at run-time, there is no need to
11700 warn about it. */
11701 if (c_inhibit_evaluation_warnings)
11702 return;
11703 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11704 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11705 return;
11706 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11707 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11708 source_type = type1;
11709 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11710 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11711 source_type = type2;
11712 else
11713 return;
11714 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11717 /* Setup a TYPE_DECL node as a typedef representation.
11719 X is a TYPE_DECL for a typedef statement. Create a brand new
11720 ..._TYPE node (which will be just a variant of the existing
11721 ..._TYPE node with identical properties) and then install X
11722 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11724 The whole point here is to end up with a situation where each
11725 and every ..._TYPE node the compiler creates will be uniquely
11726 associated with AT MOST one node representing a typedef name.
11727 This way, even though the compiler substitutes corresponding
11728 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11729 early on, later parts of the compiler can always do the reverse
11730 translation and get back the corresponding typedef name. For
11731 example, given:
11733 typedef struct S MY_TYPE;
11734 MY_TYPE object;
11736 Later parts of the compiler might only know that `object' was of
11737 type `struct S' if it were not for code just below. With this
11738 code however, later parts of the compiler see something like:
11740 struct S' == struct S
11741 typedef struct S' MY_TYPE;
11742 struct S' object;
11744 And they can then deduce (from the node for type struct S') that
11745 the original object declaration was:
11747 MY_TYPE object;
11749 Being able to do this is important for proper support of protoize,
11750 and also for generating precise symbolic debugging information
11751 which takes full account of the programmer's (typedef) vocabulary.
11753 Obviously, we don't want to generate a duplicate ..._TYPE node if
11754 the TYPE_DECL node that we are now processing really represents a
11755 standard built-in type. */
11757 void
11758 set_underlying_type (tree x)
11760 if (x == error_mark_node)
11761 return;
11762 if (DECL_IS_BUILTIN (x))
11764 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11765 TYPE_NAME (TREE_TYPE (x)) = x;
11767 else if (TREE_TYPE (x) != error_mark_node
11768 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11770 tree tt = TREE_TYPE (x);
11771 DECL_ORIGINAL_TYPE (x) = tt;
11772 tt = build_variant_type_copy (tt);
11773 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11774 TYPE_NAME (tt) = x;
11775 TREE_USED (tt) = TREE_USED (x);
11776 TREE_TYPE (x) = tt;
11780 /* Record the types used by the current global variable declaration
11781 being parsed, so that we can decide later to emit their debug info.
11782 Those types are in types_used_by_cur_var_decl, and we are going to
11783 store them in the types_used_by_vars_hash hash table.
11784 DECL is the declaration of the global variable that has been parsed. */
11786 void
11787 record_types_used_by_current_var_decl (tree decl)
11789 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11791 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11793 tree type = types_used_by_cur_var_decl->pop ();
11794 types_used_by_var_decl_insert (type, decl);
11798 /* If DECL is a typedef that is declared in the current function,
11799 record it for the purpose of -Wunused-local-typedefs. */
11801 void
11802 record_locally_defined_typedef (tree decl)
11804 struct c_language_function *l;
11806 if (!warn_unused_local_typedefs
11807 || cfun == NULL
11808 /* if this is not a locally defined typedef then we are not
11809 interested. */
11810 || !is_typedef_decl (decl)
11811 || !decl_function_context (decl))
11812 return;
11814 l = (struct c_language_function *) cfun->language;
11815 vec_safe_push (l->local_typedefs, decl);
11818 /* If T is a TYPE_DECL declared locally, mark it as used. */
11820 void
11821 maybe_record_typedef_use (tree t)
11823 if (!is_typedef_decl (t))
11824 return;
11826 TREE_USED (t) = true;
11829 /* Warn if there are some unused locally defined typedefs in the
11830 current function. */
11832 void
11833 maybe_warn_unused_local_typedefs (void)
11835 int i;
11836 tree decl;
11837 /* The number of times we have emitted -Wunused-local-typedefs
11838 warnings. If this is different from errorcount, that means some
11839 unrelated errors have been issued. In which case, we'll avoid
11840 emitting "unused-local-typedefs" warnings. */
11841 static int unused_local_typedefs_warn_count;
11842 struct c_language_function *l;
11844 if (cfun == NULL)
11845 return;
11847 if ((l = (struct c_language_function *) cfun->language) == NULL)
11848 return;
11850 if (warn_unused_local_typedefs
11851 && errorcount == unused_local_typedefs_warn_count)
11853 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11854 if (!TREE_USED (decl))
11855 warning_at (DECL_SOURCE_LOCATION (decl),
11856 OPT_Wunused_local_typedefs,
11857 "typedef %qD locally defined but not used", decl);
11858 unused_local_typedefs_warn_count = errorcount;
11861 vec_free (l->local_typedefs);
11864 /* Warn about boolean expression compared with an integer value different
11865 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11866 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11867 are the operands of the comparison. The caller must ensure that
11868 either operand is a boolean expression. */
11870 void
11871 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
11872 tree op1)
11874 if (TREE_CODE_CLASS (code) != tcc_comparison)
11875 return;
11877 tree cst = (TREE_CODE (op0) == INTEGER_CST)
11878 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
11879 if (!cst)
11880 return;
11882 if (!integer_zerop (cst) && !integer_onep (cst))
11884 int sign = (TREE_CODE (op0) == INTEGER_CST)
11885 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
11886 if (code == EQ_EXPR
11887 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
11888 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
11889 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11890 "with boolean expression is always false", cst);
11891 else
11892 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
11893 "with boolean expression is always true", cst);
11897 /* The C and C++ parsers both use vectors to hold function arguments.
11898 For efficiency, we keep a cache of unused vectors. This is the
11899 cache. */
11901 typedef vec<tree, va_gc> *tree_gc_vec;
11902 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11904 /* Return a new vector from the cache. If the cache is empty,
11905 allocate a new vector. These vectors are GC'ed, so it is OK if the
11906 pointer is not released.. */
11908 vec<tree, va_gc> *
11909 make_tree_vector (void)
11911 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11912 return tree_vector_cache->pop ();
11913 else
11915 /* Passing 0 to vec::alloc returns NULL, and our callers require
11916 that we always return a non-NULL value. The vector code uses
11917 4 when growing a NULL vector, so we do too. */
11918 vec<tree, va_gc> *v;
11919 vec_alloc (v, 4);
11920 return v;
11924 /* Release a vector of trees back to the cache. */
11926 void
11927 release_tree_vector (vec<tree, va_gc> *vec)
11929 if (vec != NULL)
11931 vec->truncate (0);
11932 vec_safe_push (tree_vector_cache, vec);
11936 /* Get a new tree vector holding a single tree. */
11938 vec<tree, va_gc> *
11939 make_tree_vector_single (tree t)
11941 vec<tree, va_gc> *ret = make_tree_vector ();
11942 ret->quick_push (t);
11943 return ret;
11946 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11948 vec<tree, va_gc> *
11949 make_tree_vector_from_list (tree list)
11951 vec<tree, va_gc> *ret = make_tree_vector ();
11952 for (; list; list = TREE_CHAIN (list))
11953 vec_safe_push (ret, TREE_VALUE (list));
11954 return ret;
11957 /* Get a new tree vector which is a copy of an existing one. */
11959 vec<tree, va_gc> *
11960 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11962 vec<tree, va_gc> *ret;
11963 unsigned int ix;
11964 tree t;
11966 ret = make_tree_vector ();
11967 vec_safe_reserve (ret, vec_safe_length (orig));
11968 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11969 ret->quick_push (t);
11970 return ret;
11973 /* Return true if KEYWORD starts a type specifier. */
11975 bool
11976 keyword_begins_type_specifier (enum rid keyword)
11978 switch (keyword)
11980 case RID_AUTO_TYPE:
11981 case RID_INT:
11982 case RID_CHAR:
11983 case RID_FLOAT:
11984 case RID_DOUBLE:
11985 case RID_VOID:
11986 case RID_UNSIGNED:
11987 case RID_LONG:
11988 case RID_SHORT:
11989 case RID_SIGNED:
11990 case RID_DFLOAT32:
11991 case RID_DFLOAT64:
11992 case RID_DFLOAT128:
11993 case RID_FRACT:
11994 case RID_ACCUM:
11995 case RID_BOOL:
11996 case RID_WCHAR:
11997 case RID_CHAR16:
11998 case RID_CHAR32:
11999 case RID_SAT:
12000 case RID_COMPLEX:
12001 case RID_TYPEOF:
12002 case RID_STRUCT:
12003 case RID_CLASS:
12004 case RID_UNION:
12005 case RID_ENUM:
12006 return true;
12007 default:
12008 if (keyword >= RID_FIRST_INT_N
12009 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12010 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12011 return true;
12012 return false;
12016 /* Return true if KEYWORD names a type qualifier. */
12018 bool
12019 keyword_is_type_qualifier (enum rid keyword)
12021 switch (keyword)
12023 case RID_CONST:
12024 case RID_VOLATILE:
12025 case RID_RESTRICT:
12026 case RID_ATOMIC:
12027 return true;
12028 default:
12029 return false;
12033 /* Return true if KEYWORD names a storage class specifier.
12035 RID_TYPEDEF is not included in this list despite `typedef' being
12036 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12037 such for syntactic convenience only. */
12039 bool
12040 keyword_is_storage_class_specifier (enum rid keyword)
12042 switch (keyword)
12044 case RID_STATIC:
12045 case RID_EXTERN:
12046 case RID_REGISTER:
12047 case RID_AUTO:
12048 case RID_MUTABLE:
12049 case RID_THREAD:
12050 return true;
12051 default:
12052 return false;
12056 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12058 static bool
12059 keyword_is_function_specifier (enum rid keyword)
12061 switch (keyword)
12063 case RID_INLINE:
12064 case RID_NORETURN:
12065 case RID_VIRTUAL:
12066 case RID_EXPLICIT:
12067 return true;
12068 default:
12069 return false;
12073 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12074 declaration-specifier (C99 6.7). */
12076 bool
12077 keyword_is_decl_specifier (enum rid keyword)
12079 if (keyword_is_storage_class_specifier (keyword)
12080 || keyword_is_type_qualifier (keyword)
12081 || keyword_is_function_specifier (keyword))
12082 return true;
12084 switch (keyword)
12086 case RID_TYPEDEF:
12087 case RID_FRIEND:
12088 case RID_CONSTEXPR:
12089 return true;
12090 default:
12091 return false;
12095 /* Initialize language-specific-bits of tree_contains_struct. */
12097 void
12098 c_common_init_ts (void)
12100 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12101 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12102 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12105 /* Build a user-defined numeric literal out of an integer constant type VALUE
12106 with identifier SUFFIX. */
12108 tree
12109 build_userdef_literal (tree suffix_id, tree value,
12110 enum overflow_type overflow, tree num_string)
12112 tree literal = make_node (USERDEF_LITERAL);
12113 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12114 USERDEF_LITERAL_VALUE (literal) = value;
12115 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12116 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12117 return literal;
12120 /* For vector[index], convert the vector to a
12121 pointer of the underlying type. Return true if the resulting
12122 ARRAY_REF should not be an lvalue. */
12124 bool
12125 convert_vector_to_pointer_for_subscript (location_t loc,
12126 tree *vecp, tree index)
12128 bool ret = false;
12129 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12131 tree type = TREE_TYPE (*vecp);
12132 tree type1;
12134 ret = !lvalue_p (*vecp);
12135 if (TREE_CODE (index) == INTEGER_CST)
12136 if (!tree_fits_uhwi_p (index)
12137 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12138 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12140 if (ret)
12142 tree tmp = create_tmp_var_raw (type);
12143 DECL_SOURCE_LOCATION (tmp) = loc;
12144 *vecp = c_save_expr (*vecp);
12145 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12147 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12148 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12149 *vecp
12150 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12151 *vecp, NULL_TREE, NULL_TREE),
12152 non_const);
12154 else
12155 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12156 NULL_TREE, NULL_TREE);
12157 SET_EXPR_LOCATION (*vecp, loc);
12158 c_common_mark_addressable_vec (tmp);
12160 else
12161 c_common_mark_addressable_vec (*vecp);
12162 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12163 type1 = build_pointer_type (TREE_TYPE (*vecp));
12164 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12165 if (!ref_all
12166 && !DECL_P (*vecp))
12168 /* If the original vector isn't declared may_alias and it
12169 isn't a bare vector look if the subscripting would
12170 alias the vector we subscript, and if not, force ref-all. */
12171 alias_set_type vecset = get_alias_set (*vecp);
12172 alias_set_type sset = get_alias_set (type);
12173 if (!alias_sets_must_conflict_p (sset, vecset)
12174 && !alias_set_subset_of (sset, vecset))
12175 ref_all = true;
12177 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12178 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12179 *vecp = convert (type, *vecp);
12181 return ret;
12184 /* Determine which of the operands, if any, is a scalar that needs to be
12185 converted to a vector, for the range of operations. */
12186 enum stv_conv
12187 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12188 bool complain)
12190 tree type0 = TREE_TYPE (op0);
12191 tree type1 = TREE_TYPE (op1);
12192 bool integer_only_op = false;
12193 enum stv_conv ret = stv_firstarg;
12195 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12196 || TREE_CODE (type1) == VECTOR_TYPE);
12197 switch (code)
12199 /* Most GENERIC binary expressions require homogeneous arguments.
12200 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12201 argument that is a vector and a second one that is a scalar, so
12202 we never return stv_secondarg for them. */
12203 case RSHIFT_EXPR:
12204 case LSHIFT_EXPR:
12205 if (TREE_CODE (type0) == INTEGER_TYPE
12206 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12208 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12210 if (complain)
12211 error_at (loc, "conversion of scalar %qT to vector %qT "
12212 "involves truncation", type0, type1);
12213 return stv_error;
12215 else
12216 return stv_firstarg;
12218 break;
12220 case BIT_IOR_EXPR:
12221 case BIT_XOR_EXPR:
12222 case BIT_AND_EXPR:
12223 integer_only_op = true;
12224 /* ... fall through ... */
12226 case VEC_COND_EXPR:
12228 case PLUS_EXPR:
12229 case MINUS_EXPR:
12230 case MULT_EXPR:
12231 case TRUNC_DIV_EXPR:
12232 case CEIL_DIV_EXPR:
12233 case FLOOR_DIV_EXPR:
12234 case ROUND_DIV_EXPR:
12235 case EXACT_DIV_EXPR:
12236 case TRUNC_MOD_EXPR:
12237 case FLOOR_MOD_EXPR:
12238 case RDIV_EXPR:
12239 case EQ_EXPR:
12240 case NE_EXPR:
12241 case LE_EXPR:
12242 case GE_EXPR:
12243 case LT_EXPR:
12244 case GT_EXPR:
12245 /* What about UNLT_EXPR? */
12246 if (TREE_CODE (type0) == VECTOR_TYPE)
12248 tree tmp;
12249 ret = stv_secondarg;
12250 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12251 tmp = type0; type0 = type1; type1 = tmp;
12252 tmp = op0; op0 = op1; op1 = tmp;
12255 if (TREE_CODE (type0) == INTEGER_TYPE
12256 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12258 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12260 if (complain)
12261 error_at (loc, "conversion of scalar %qT to vector %qT "
12262 "involves truncation", type0, type1);
12263 return stv_error;
12265 return ret;
12267 else if (!integer_only_op
12268 /* Allow integer --> real conversion if safe. */
12269 && (TREE_CODE (type0) == REAL_TYPE
12270 || TREE_CODE (type0) == INTEGER_TYPE)
12271 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12273 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12275 if (complain)
12276 error_at (loc, "conversion of scalar %qT to vector %qT "
12277 "involves truncation", type0, type1);
12278 return stv_error;
12280 return ret;
12282 default:
12283 break;
12286 return stv_nothing;
12289 /* Return true iff ALIGN is an integral constant that is a fundamental
12290 alignment, as defined by [basic.align] in the c++-11
12291 specifications.
12293 That is:
12295 [A fundamental alignment is represented by an alignment less than or
12296 equal to the greatest alignment supported by the implementation
12297 in all contexts, which is equal to
12298 alignof(max_align_t)]. */
12300 bool
12301 cxx_fundamental_alignment_p (unsigned align)
12303 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12304 TYPE_ALIGN (long_double_type_node)));
12307 /* Return true if T is a pointer to a zero-sized aggregate. */
12309 bool
12310 pointer_to_zero_sized_aggr_p (tree t)
12312 if (!POINTER_TYPE_P (t))
12313 return false;
12314 t = TREE_TYPE (t);
12315 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12318 #include "gt-c-family-c-common.h"