Merged trunk revision 225993 into the hsa branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob98a14290753e5c117415cda54c678d83b9c925a5
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "c-common.h"
26 #include "tm.h"
27 #include "intl.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "calls.h"
32 #include "stringpool.h"
33 #include "attribs.h"
34 #include "varasm.h"
35 #include "trans-mem.h"
36 #include "flags.h"
37 #include "c-pragma.h"
38 #include "c-objc.h"
39 #include "tm_p.h"
40 #include "obstack.h"
41 #include "cpplib.h"
42 #include "target.h"
43 #include "common/common-target.h"
44 #include "langhooks.h"
45 #include "tree-inline.h"
46 #include "toplev.h"
47 #include "diagnostic.h"
48 #include "tree-iterator.h"
49 #include "opts.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "cgraph.h"
53 #include "gimplify.h"
54 #include "wide-int-print.h"
55 #include "gimple-expr.h"
57 cpp_reader *parse_in; /* Declared in c-pragma.h. */
59 /* Mode used to build pointers (VOIDmode means ptr_mode). */
61 machine_mode c_default_pointer_mode = VOIDmode;
63 /* The following symbols are subsumed in the c_global_trees array, and
64 listed here individually for documentation purposes.
66 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
68 tree short_integer_type_node;
69 tree long_integer_type_node;
70 tree long_long_integer_type_node;
72 tree short_unsigned_type_node;
73 tree long_unsigned_type_node;
74 tree long_long_unsigned_type_node;
76 tree truthvalue_type_node;
77 tree truthvalue_false_node;
78 tree truthvalue_true_node;
80 tree ptrdiff_type_node;
82 tree unsigned_char_type_node;
83 tree signed_char_type_node;
84 tree wchar_type_node;
86 tree char16_type_node;
87 tree char32_type_node;
89 tree float_type_node;
90 tree double_type_node;
91 tree long_double_type_node;
93 tree complex_integer_type_node;
94 tree complex_float_type_node;
95 tree complex_double_type_node;
96 tree complex_long_double_type_node;
98 tree dfloat32_type_node;
99 tree dfloat64_type_node;
100 tree_dfloat128_type_node;
102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
117 Nodes for types `void *' and `const void *'.
119 tree ptr_type_node, const_ptr_type_node;
121 Nodes for types `char *' and `const char *'.
123 tree string_type_node, const_string_type_node;
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
128 tree char_array_type_node;
130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
133 tree wchar_array_type_node;
135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
138 tree char16_array_type_node;
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
143 tree char32_array_type_node;
145 Type `int ()' -- used for implicit declaration of functions.
147 tree default_function_type;
149 A VOID_TYPE node, packaged in a TREE_LIST.
151 tree void_list_node;
153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
157 tree function_name_decl_node;
158 tree pretty_function_name_decl_node;
159 tree c99_function_name_decl_node;
161 Stack of nested function name VAR_DECLs.
163 tree saved_function_name_decls;
167 tree c_global_trees[CTI_MAX];
169 /* Switches common to the C front ends. */
171 /* Nonzero means don't output line number information. */
173 char flag_no_line_commands;
175 /* Nonzero causes -E output not to be done, but directives such as
176 #define that have side effects are still obeyed. */
178 char flag_no_output;
180 /* Nonzero means dump macros in some fashion. */
182 char flag_dump_macros;
184 /* Nonzero means pass #include lines through to the output. */
186 char flag_dump_includes;
188 /* Nonzero means process PCH files while preprocessing. */
190 bool flag_pch_preprocess;
192 /* The file name to which we should write a precompiled header, or
193 NULL if no header will be written in this compile. */
195 const char *pch_file;
197 /* Nonzero if an ISO standard was selected. It rejects macros in the
198 user's namespace. */
199 int flag_iso;
201 /* C/ObjC language option variables. */
204 /* Nonzero means allow type mismatches in conditional expressions;
205 just make their values `void'. */
207 int flag_cond_mismatch;
209 /* Nonzero means enable C89 Amendment 1 features. */
211 int flag_isoc94;
213 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
215 int flag_isoc99;
217 /* Nonzero means use the ISO C11 dialect of C. */
219 int flag_isoc11;
221 /* Nonzero means that we have builtin functions, and main is an int. */
223 int flag_hosted = 1;
226 /* ObjC language option variables. */
229 /* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
233 int print_struct_values;
235 /* Tells the compiler what is the constant string class for ObjC. */
237 const char *constant_string_class_name;
240 /* C++ language option variables. */
243 /* Nonzero means generate separate instantiation control files and
244 juggle them at link time. */
246 int flag_use_repository;
248 /* The C++ dialect being used. Default set in c_common_post_options. */
250 enum cxx_dialect cxx_dialect = cxx_unset;
252 /* Maximum template instantiation depth. This limit exists to limit the
253 time it takes to notice excessively recursive template instantiations.
255 The default is lower than the 1024 recommended by the C++0x standard
256 because G++ runs out of stack before 1024 with highly recursive template
257 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
259 int max_tinst_depth = 900;
261 /* The elements of `ridpointers' are identifier nodes for the reserved
262 type names and storage classes. It is indexed by a RID_... value. */
263 tree *ridpointers;
265 tree (*make_fname_decl) (location_t, tree, int);
267 /* Nonzero means don't warn about problems that occur when the code is
268 executed. */
269 int c_inhibit_evaluation_warnings;
271 /* Whether we are building a boolean conversion inside
272 convert_for_assignment, or some other late binary operation. If
273 build_binary_op is called for C (from code shared by C and C++) in
274 this case, then the operands have already been folded and the
275 result will not be folded again, so C_MAYBE_CONST_EXPR should not
276 be generated. */
277 bool in_late_binary_op;
279 /* Whether lexing has been completed, so subsequent preprocessor
280 errors should use the compiler's input_location. */
281 bool done_lexing = false;
283 /* Information about how a function name is generated. */
284 struct fname_var_t
286 tree *const decl; /* pointer to the VAR_DECL. */
287 const unsigned rid; /* RID number for the identifier. */
288 const int pretty; /* How pretty is it? */
291 /* The three ways of getting then name of the current function. */
293 const struct fname_var_t fname_vars[] =
295 /* C99 compliant __func__, must be first. */
296 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
297 /* GCC __FUNCTION__ compliant. */
298 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
299 /* GCC __PRETTY_FUNCTION__ compliant. */
300 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
301 {NULL, 0, 0},
304 /* Global visibility options. */
305 struct visibility_flags visibility_options;
307 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
308 static tree check_case_value (location_t, tree);
309 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
310 bool *);
312 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
313 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
314 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
319 int, bool *);
320 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
321 int, bool *);
322 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
323 bool *);
324 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_hsa_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_always_inline_attribute (tree *, tree, tree, int,
331 bool *);
332 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
339 bool *);
340 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_transparent_union_attribute (tree *, tree, tree,
344 int, bool *);
345 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
352 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
353 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
356 static tree handle_visibility_attribute (tree *, tree, tree, int,
357 bool *);
358 static tree handle_tls_model_attribute (tree *, tree, tree, int,
359 bool *);
360 static tree handle_no_instrument_function_attribute (tree *, tree,
361 tree, int, bool *);
362 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_deprecated_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_vector_size_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
378 bool *);
379 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
386 static tree ignore_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
392 bool *);
393 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
394 bool *);
395 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
398 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
400 static void check_function_nonnull (tree, int, tree *);
401 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
402 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
403 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
404 static int resort_field_decl_cmp (const void *, const void *);
406 /* Reserved words. The third field is a mask: keywords are disabled
407 if they match the mask.
409 Masks for languages:
410 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
411 C --std=c99: D_CXXONLY | D_OBJC
412 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
413 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
414 C++ --std=c0x: D_CONLY | D_OBJC
415 ObjC++ is like C++ except that D_OBJC is not set
417 If -fno-asm is used, D_ASM is added to the mask. If
418 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
419 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
420 In C with -Wc++-compat, we warn if D_CXXWARN is set.
422 Note the complication of the D_CXX_OBJC keywords. These are
423 reserved words such as 'class'. In C++, 'class' is a reserved
424 word. In Objective-C++ it is too. In Objective-C, it is a
425 reserved word too, but only if it follows an '@' sign.
427 const struct c_common_resword c_common_reswords[] =
429 { "_Alignas", RID_ALIGNAS, D_CONLY },
430 { "_Alignof", RID_ALIGNOF, D_CONLY },
431 { "_Atomic", RID_ATOMIC, D_CONLY },
432 { "_Bool", RID_BOOL, D_CONLY },
433 { "_Complex", RID_COMPLEX, 0 },
434 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
435 { "_Cilk_sync", RID_CILK_SYNC, 0 },
436 { "_Cilk_for", RID_CILK_FOR, 0 },
437 { "_Imaginary", RID_IMAGINARY, D_CONLY },
438 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
439 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
440 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
441 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
442 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
443 { "_Sat", RID_SAT, D_CONLY | D_EXT },
444 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
445 { "_Noreturn", RID_NORETURN, D_CONLY },
446 { "_Generic", RID_GENERIC, D_CONLY },
447 { "_Thread_local", RID_THREAD, D_CONLY },
448 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
449 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
450 { "__alignof", RID_ALIGNOF, 0 },
451 { "__alignof__", RID_ALIGNOF, 0 },
452 { "__asm", RID_ASM, 0 },
453 { "__asm__", RID_ASM, 0 },
454 { "__attribute", RID_ATTRIBUTE, 0 },
455 { "__attribute__", RID_ATTRIBUTE, 0 },
456 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
457 { "__bases", RID_BASES, D_CXXONLY },
458 { "__builtin_call_with_static_chain",
459 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
460 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
461 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
462 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
463 { "__builtin_offsetof", RID_OFFSETOF, 0 },
464 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
465 { "__builtin_va_arg", RID_VA_ARG, 0 },
466 { "__complex", RID_COMPLEX, 0 },
467 { "__complex__", RID_COMPLEX, 0 },
468 { "__const", RID_CONST, 0 },
469 { "__const__", RID_CONST, 0 },
470 { "__decltype", RID_DECLTYPE, D_CXXONLY },
471 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
472 { "__extension__", RID_EXTENSION, 0 },
473 { "__func__", RID_C99_FUNCTION_NAME, 0 },
474 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
475 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
476 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
477 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
478 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
479 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
480 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
481 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
482 { "__imag", RID_IMAGPART, 0 },
483 { "__imag__", RID_IMAGPART, 0 },
484 { "__inline", RID_INLINE, 0 },
485 { "__inline__", RID_INLINE, 0 },
486 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
487 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
488 { "__is_class", RID_IS_CLASS, D_CXXONLY },
489 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
490 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
491 { "__is_final", RID_IS_FINAL, D_CXXONLY },
492 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
493 { "__is_pod", RID_IS_POD, D_CXXONLY },
494 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
495 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
496 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
497 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
498 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
499 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
500 { "__is_union", RID_IS_UNION, D_CXXONLY },
501 { "__label__", RID_LABEL, 0 },
502 { "__null", RID_NULL, 0 },
503 { "__real", RID_REALPART, 0 },
504 { "__real__", RID_REALPART, 0 },
505 { "__restrict", RID_RESTRICT, 0 },
506 { "__restrict__", RID_RESTRICT, 0 },
507 { "__signed", RID_SIGNED, 0 },
508 { "__signed__", RID_SIGNED, 0 },
509 { "__thread", RID_THREAD, 0 },
510 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
511 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
512 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
513 { "__typeof", RID_TYPEOF, 0 },
514 { "__typeof__", RID_TYPEOF, 0 },
515 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
516 { "__volatile", RID_VOLATILE, 0 },
517 { "__volatile__", RID_VOLATILE, 0 },
518 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
519 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
520 { "asm", RID_ASM, D_ASM },
521 { "auto", RID_AUTO, 0 },
522 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
523 { "break", RID_BREAK, 0 },
524 { "case", RID_CASE, 0 },
525 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
526 { "char", RID_CHAR, 0 },
527 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
528 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
529 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
530 { "const", RID_CONST, 0 },
531 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
532 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
533 { "continue", RID_CONTINUE, 0 },
534 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
535 { "default", RID_DEFAULT, 0 },
536 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
537 { "do", RID_DO, 0 },
538 { "double", RID_DOUBLE, 0 },
539 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
540 { "else", RID_ELSE, 0 },
541 { "enum", RID_ENUM, 0 },
542 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
543 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
544 { "extern", RID_EXTERN, 0 },
545 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
546 { "float", RID_FLOAT, 0 },
547 { "for", RID_FOR, 0 },
548 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
549 { "goto", RID_GOTO, 0 },
550 { "if", RID_IF, 0 },
551 { "inline", RID_INLINE, D_EXT89 },
552 { "int", RID_INT, 0 },
553 { "long", RID_LONG, 0 },
554 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
555 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
556 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
557 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
558 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
559 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
560 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
561 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
562 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
563 { "register", RID_REGISTER, 0 },
564 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
565 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
566 { "return", RID_RETURN, 0 },
567 { "short", RID_SHORT, 0 },
568 { "signed", RID_SIGNED, 0 },
569 { "sizeof", RID_SIZEOF, 0 },
570 { "static", RID_STATIC, 0 },
571 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
572 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
573 { "struct", RID_STRUCT, 0 },
574 { "switch", RID_SWITCH, 0 },
575 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
576 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
577 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
578 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
579 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
580 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
581 { "typedef", RID_TYPEDEF, 0 },
582 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
583 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
584 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
585 { "union", RID_UNION, 0 },
586 { "unsigned", RID_UNSIGNED, 0 },
587 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
588 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
589 { "void", RID_VOID, 0 },
590 { "volatile", RID_VOLATILE, 0 },
591 { "wchar_t", RID_WCHAR, D_CXXONLY },
592 { "while", RID_WHILE, 0 },
593 /* These Objective-C keywords are recognized only immediately after
594 an '@'. */
595 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
596 { "defs", RID_AT_DEFS, D_OBJC },
597 { "encode", RID_AT_ENCODE, D_OBJC },
598 { "end", RID_AT_END, D_OBJC },
599 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
600 { "interface", RID_AT_INTERFACE, D_OBJC },
601 { "protocol", RID_AT_PROTOCOL, D_OBJC },
602 { "selector", RID_AT_SELECTOR, D_OBJC },
603 { "finally", RID_AT_FINALLY, D_OBJC },
604 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
605 { "optional", RID_AT_OPTIONAL, D_OBJC },
606 { "required", RID_AT_REQUIRED, D_OBJC },
607 { "property", RID_AT_PROPERTY, D_OBJC },
608 { "package", RID_AT_PACKAGE, D_OBJC },
609 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
610 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
611 /* These are recognized only in protocol-qualifier context
612 (see above) */
613 { "bycopy", RID_BYCOPY, D_OBJC },
614 { "byref", RID_BYREF, D_OBJC },
615 { "in", RID_IN, D_OBJC },
616 { "inout", RID_INOUT, D_OBJC },
617 { "oneway", RID_ONEWAY, D_OBJC },
618 { "out", RID_OUT, D_OBJC },
619 /* These are recognized inside a property attribute list */
620 { "assign", RID_ASSIGN, D_OBJC },
621 { "copy", RID_COPY, D_OBJC },
622 { "getter", RID_GETTER, D_OBJC },
623 { "nonatomic", RID_NONATOMIC, D_OBJC },
624 { "readonly", RID_READONLY, D_OBJC },
625 { "readwrite", RID_READWRITE, D_OBJC },
626 { "retain", RID_RETAIN, D_OBJC },
627 { "setter", RID_SETTER, D_OBJC },
630 const unsigned int num_c_common_reswords =
631 sizeof c_common_reswords / sizeof (struct c_common_resword);
633 /* Table of machine-independent attributes common to all C-like languages.
635 All attributes referencing arguments should be additionally processed
636 in chkp_copy_function_type_adding_bounds for correct instrumentation
637 by Pointer Bounds Checker.
638 Current list of processed common attributes: nonnull. */
639 const struct attribute_spec c_common_attribute_table[] =
641 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
642 affects_type_identity } */
643 { "packed", 0, 0, false, false, false,
644 handle_packed_attribute , false},
645 { "nocommon", 0, 0, true, false, false,
646 handle_nocommon_attribute, false},
647 { "common", 0, 0, true, false, false,
648 handle_common_attribute, false },
649 /* FIXME: logically, noreturn attributes should be listed as
650 "false, true, true" and apply to function types. But implementing this
651 would require all the places in the compiler that use TREE_THIS_VOLATILE
652 on a decl to identify non-returning functions to be located and fixed
653 to check the function type instead. */
654 { "noreturn", 0, 0, true, false, false,
655 handle_noreturn_attribute, false },
656 { "volatile", 0, 0, true, false, false,
657 handle_noreturn_attribute, false },
658 { "stack_protect", 0, 0, true, false, false,
659 handle_stack_protect_attribute, false },
660 { "noinline", 0, 0, true, false, false,
661 handle_noinline_attribute, false },
662 { "noclone", 0, 0, true, false, false,
663 handle_noclone_attribute, false },
664 { "hsa", 0, 0, true, false, false,
665 handle_hsa_attribute, false },
666 { "hsakernel", 0, 0, true, false, false,
667 handle_hsa_attribute, false },
668 { "hsafunc", 0, 0, true, false, false,
669 handle_hsa_attribute, false },
670 { "no_icf", 0, 0, true, false, false,
671 handle_noicf_attribute, false },
672 { "leaf", 0, 0, true, false, false,
673 handle_leaf_attribute, false },
674 { "always_inline", 0, 0, true, false, false,
675 handle_always_inline_attribute, false },
676 { "gnu_inline", 0, 0, true, false, false,
677 handle_gnu_inline_attribute, false },
678 { "artificial", 0, 0, true, false, false,
679 handle_artificial_attribute, false },
680 { "flatten", 0, 0, true, false, false,
681 handle_flatten_attribute, false },
682 { "used", 0, 0, true, false, false,
683 handle_used_attribute, false },
684 { "unused", 0, 0, false, false, false,
685 handle_unused_attribute, false },
686 { "externally_visible", 0, 0, true, false, false,
687 handle_externally_visible_attribute, false },
688 { "no_reorder", 0, 0, true, false, false,
689 handle_no_reorder_attribute, false },
690 /* The same comments as for noreturn attributes apply to const ones. */
691 { "const", 0, 0, true, false, false,
692 handle_const_attribute, false },
693 { "transparent_union", 0, 0, false, false, false,
694 handle_transparent_union_attribute, false },
695 { "constructor", 0, 1, true, false, false,
696 handle_constructor_attribute, false },
697 { "destructor", 0, 1, true, false, false,
698 handle_destructor_attribute, false },
699 { "mode", 1, 1, false, true, false,
700 handle_mode_attribute, false },
701 { "section", 1, 1, true, false, false,
702 handle_section_attribute, false },
703 { "aligned", 0, 1, false, false, false,
704 handle_aligned_attribute, false },
705 { "weak", 0, 0, true, false, false,
706 handle_weak_attribute, false },
707 { "noplt", 0, 0, true, false, false,
708 handle_noplt_attribute, false },
709 { "ifunc", 1, 1, true, false, false,
710 handle_ifunc_attribute, false },
711 { "alias", 1, 1, true, false, false,
712 handle_alias_attribute, false },
713 { "weakref", 0, 1, true, false, false,
714 handle_weakref_attribute, false },
715 { "no_instrument_function", 0, 0, true, false, false,
716 handle_no_instrument_function_attribute,
717 false },
718 { "malloc", 0, 0, true, false, false,
719 handle_malloc_attribute, false },
720 { "returns_twice", 0, 0, true, false, false,
721 handle_returns_twice_attribute, false },
722 { "no_stack_limit", 0, 0, true, false, false,
723 handle_no_limit_stack_attribute, false },
724 { "pure", 0, 0, true, false, false,
725 handle_pure_attribute, false },
726 { "transaction_callable", 0, 0, false, true, false,
727 handle_tm_attribute, false },
728 { "transaction_unsafe", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_safe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_may_cancel_outer", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 /* ??? These two attributes didn't make the transition from the
735 Intel language document to the multi-vendor language document. */
736 { "transaction_pure", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 { "transaction_wrap", 1, 1, true, false, false,
739 handle_tm_wrap_attribute, false },
740 /* For internal use (marking of builtins) only. The name contains space
741 to prevent its usage in source code. */
742 { "no vops", 0, 0, true, false, false,
743 handle_novops_attribute, false },
744 { "deprecated", 0, 1, false, false, false,
745 handle_deprecated_attribute, false },
746 { "vector_size", 1, 1, false, true, false,
747 handle_vector_size_attribute, false },
748 { "visibility", 1, 1, false, false, false,
749 handle_visibility_attribute, false },
750 { "tls_model", 1, 1, true, false, false,
751 handle_tls_model_attribute, false },
752 { "nonnull", 0, -1, false, true, true,
753 handle_nonnull_attribute, false },
754 { "nothrow", 0, 0, true, false, false,
755 handle_nothrow_attribute, false },
756 { "may_alias", 0, 0, false, true, false, NULL, false },
757 { "cleanup", 1, 1, true, false, false,
758 handle_cleanup_attribute, false },
759 { "warn_unused_result", 0, 0, false, true, true,
760 handle_warn_unused_result_attribute, false },
761 { "sentinel", 0, 1, false, true, true,
762 handle_sentinel_attribute, false },
763 /* For internal use (marking of builtins) only. The name contains space
764 to prevent its usage in source code. */
765 { "type generic", 0, 0, false, true, true,
766 handle_type_generic_attribute, false },
767 { "alloc_size", 1, 2, false, true, true,
768 handle_alloc_size_attribute, false },
769 { "cold", 0, 0, true, false, false,
770 handle_cold_attribute, false },
771 { "hot", 0, 0, true, false, false,
772 handle_hot_attribute, false },
773 { "no_address_safety_analysis",
774 0, 0, true, false, false,
775 handle_no_address_safety_analysis_attribute,
776 false },
777 { "no_sanitize_address", 0, 0, true, false, false,
778 handle_no_sanitize_address_attribute,
779 false },
780 { "no_sanitize_thread", 0, 0, true, false, false,
781 handle_no_sanitize_address_attribute,
782 false },
783 { "no_sanitize_undefined", 0, 0, true, false, false,
784 handle_no_sanitize_undefined_attribute,
785 false },
786 { "warning", 1, 1, true, false, false,
787 handle_error_attribute, false },
788 { "error", 1, 1, true, false, false,
789 handle_error_attribute, false },
790 { "target", 1, -1, true, false, false,
791 handle_target_attribute, false },
792 { "optimize", 1, -1, true, false, false,
793 handle_optimize_attribute, false },
794 /* For internal use only. The leading '*' both prevents its usage in
795 source code and signals that it may be overridden by machine tables. */
796 { "*tm regparm", 0, 0, false, true, true,
797 ignore_attribute, false },
798 { "no_split_stack", 0, 0, true, false, false,
799 handle_no_split_stack_attribute, false },
800 /* For internal use (marking of builtins and runtime functions) only.
801 The name contains space to prevent its usage in source code. */
802 { "fn spec", 1, 1, false, true, true,
803 handle_fnspec_attribute, false },
804 { "warn_unused", 0, 0, false, false, false,
805 handle_warn_unused_attribute, false },
806 { "returns_nonnull", 0, 0, false, true, true,
807 handle_returns_nonnull_attribute, false },
808 { "omp declare simd", 0, -1, true, false, false,
809 handle_omp_declare_simd_attribute, false },
810 { "cilk simd function", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
812 { "omp declare target", 0, 0, true, false, false,
813 handle_omp_declare_target_attribute, false },
814 { "alloc_align", 1, 1, false, true, true,
815 handle_alloc_align_attribute, false },
816 { "assume_aligned", 1, 2, false, true, true,
817 handle_assume_aligned_attribute, false },
818 { "designated_init", 0, 0, false, true, false,
819 handle_designated_init_attribute, false },
820 { "bnd_variable_size", 0, 0, true, false, false,
821 handle_bnd_variable_size_attribute, false },
822 { "bnd_legacy", 0, 0, true, false, false,
823 handle_bnd_legacy, false },
824 { "bnd_instrument", 0, 0, true, false, false,
825 handle_bnd_instrument, false },
826 { NULL, 0, 0, false, false, false, NULL, false }
829 /* Give the specifications for the format attributes, used by C and all
830 descendants.
832 All attributes referencing arguments should be additionally processed
833 in chkp_copy_function_type_adding_bounds for correct instrumentation
834 by Pointer Bounds Checker.
835 Current list of processed format attributes: format, format_arg. */
836 const struct attribute_spec c_common_format_attribute_table[] =
838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
839 affects_type_identity } */
840 { "format", 3, 3, false, true, true,
841 handle_format_attribute, false },
842 { "format_arg", 1, 1, false, true, true,
843 handle_format_arg_attribute, false },
844 { NULL, 0, 0, false, false, false, NULL, false }
847 /* Return identifier for address space AS. */
849 const char *
850 c_addr_space_name (addr_space_t as)
852 int rid = RID_FIRST_ADDR_SPACE + as;
853 gcc_assert (ridpointers [rid]);
854 return IDENTIFIER_POINTER (ridpointers [rid]);
857 /* Push current bindings for the function name VAR_DECLS. */
859 void
860 start_fname_decls (void)
862 unsigned ix;
863 tree saved = NULL_TREE;
865 for (ix = 0; fname_vars[ix].decl; ix++)
867 tree decl = *fname_vars[ix].decl;
869 if (decl)
871 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
872 saved);
873 *fname_vars[ix].decl = NULL_TREE;
876 if (saved || saved_function_name_decls)
877 /* Normally they'll have been NULL, so only push if we've got a
878 stack, or they are non-NULL. */
879 saved_function_name_decls = tree_cons (saved, NULL_TREE,
880 saved_function_name_decls);
883 /* Finish up the current bindings, adding them into the current function's
884 statement tree. This must be done _before_ finish_stmt_tree is called.
885 If there is no current function, we must be at file scope and no statements
886 are involved. Pop the previous bindings. */
888 void
889 finish_fname_decls (void)
891 unsigned ix;
892 tree stmts = NULL_TREE;
893 tree stack = saved_function_name_decls;
895 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
896 append_to_statement_list (TREE_VALUE (stack), &stmts);
898 if (stmts)
900 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
902 if (TREE_CODE (*bodyp) == BIND_EXPR)
903 bodyp = &BIND_EXPR_BODY (*bodyp);
905 append_to_statement_list_force (*bodyp, &stmts);
906 *bodyp = stmts;
909 for (ix = 0; fname_vars[ix].decl; ix++)
910 *fname_vars[ix].decl = NULL_TREE;
912 if (stack)
914 /* We had saved values, restore them. */
915 tree saved;
917 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
919 tree decl = TREE_PURPOSE (saved);
920 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
922 *fname_vars[ix].decl = decl;
924 stack = TREE_CHAIN (stack);
926 saved_function_name_decls = stack;
929 /* Return the text name of the current function, suitably prettified
930 by PRETTY_P. Return string must be freed by caller. */
932 const char *
933 fname_as_string (int pretty_p)
935 const char *name = "top level";
936 char *namep;
937 int vrb = 2, len;
938 cpp_string cstr = { 0, 0 }, strname;
940 if (!pretty_p)
942 name = "";
943 vrb = 0;
946 if (current_function_decl)
947 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
949 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
951 namep = XNEWVEC (char, len);
952 snprintf (namep, len, "\"%s\"", name);
953 strname.text = (unsigned char *) namep;
954 strname.len = len - 1;
956 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
958 XDELETEVEC (namep);
959 return (const char *) cstr.text;
962 return namep;
965 /* Return the VAR_DECL for a const char array naming the current
966 function. If the VAR_DECL has not yet been created, create it
967 now. RID indicates how it should be formatted and IDENTIFIER_NODE
968 ID is its name (unfortunately C and C++ hold the RID values of
969 keywords in different places, so we can't derive RID from ID in
970 this language independent code. LOC is the location of the
971 function. */
973 tree
974 fname_decl (location_t loc, unsigned int rid, tree id)
976 unsigned ix;
977 tree decl = NULL_TREE;
979 for (ix = 0; fname_vars[ix].decl; ix++)
980 if (fname_vars[ix].rid == rid)
981 break;
983 decl = *fname_vars[ix].decl;
984 if (!decl)
986 /* If a tree is built here, it would normally have the lineno of
987 the current statement. Later this tree will be moved to the
988 beginning of the function and this line number will be wrong.
989 To avoid this problem set the lineno to 0 here; that prevents
990 it from appearing in the RTL. */
991 tree stmts;
992 location_t saved_location = input_location;
993 input_location = UNKNOWN_LOCATION;
995 stmts = push_stmt_list ();
996 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
997 stmts = pop_stmt_list (stmts);
998 if (!IS_EMPTY_STMT (stmts))
999 saved_function_name_decls
1000 = tree_cons (decl, stmts, saved_function_name_decls);
1001 *fname_vars[ix].decl = decl;
1002 input_location = saved_location;
1004 if (!ix && !current_function_decl)
1005 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1007 return decl;
1010 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1012 tree
1013 fix_string_type (tree value)
1015 int length = TREE_STRING_LENGTH (value);
1016 int nchars;
1017 tree e_type, i_type, a_type;
1019 /* Compute the number of elements, for the array type. */
1020 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1022 nchars = length;
1023 e_type = char_type_node;
1025 else if (TREE_TYPE (value) == char16_array_type_node)
1027 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1028 e_type = char16_type_node;
1030 else if (TREE_TYPE (value) == char32_array_type_node)
1032 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1033 e_type = char32_type_node;
1035 else
1037 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1038 e_type = wchar_type_node;
1041 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1042 limit in C++98 Annex B is very large (65536) and is not normative,
1043 so we do not diagnose it (warn_overlength_strings is forced off
1044 in c_common_post_options). */
1045 if (warn_overlength_strings)
1047 const int nchars_max = flag_isoc99 ? 4095 : 509;
1048 const int relevant_std = flag_isoc99 ? 99 : 90;
1049 if (nchars - 1 > nchars_max)
1050 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1051 separate the %d from the 'C'. 'ISO' should not be
1052 translated, but it may be moved after 'C%d' in languages
1053 where modifiers follow nouns. */
1054 pedwarn (input_location, OPT_Woverlength_strings,
1055 "string length %qd is greater than the length %qd "
1056 "ISO C%d compilers are required to support",
1057 nchars - 1, nchars_max, relevant_std);
1060 /* Create the array type for the string constant. The ISO C++
1061 standard says that a string literal has type `const char[N]' or
1062 `const wchar_t[N]'. We use the same logic when invoked as a C
1063 front-end with -Wwrite-strings.
1064 ??? We should change the type of an expression depending on the
1065 state of a warning flag. We should just be warning -- see how
1066 this is handled in the C++ front-end for the deprecated implicit
1067 conversion from string literals to `char*' or `wchar_t*'.
1069 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1070 array type being the unqualified version of that type.
1071 Therefore, if we are constructing an array of const char, we must
1072 construct the matching unqualified array type first. The C front
1073 end does not require this, but it does no harm, so we do it
1074 unconditionally. */
1075 i_type = build_index_type (size_int (nchars - 1));
1076 a_type = build_array_type (e_type, i_type);
1077 if (c_dialect_cxx() || warn_write_strings)
1078 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1080 TREE_TYPE (value) = a_type;
1081 TREE_CONSTANT (value) = 1;
1082 TREE_READONLY (value) = 1;
1083 TREE_STATIC (value) = 1;
1084 return value;
1087 /* If DISABLE is true, stop issuing warnings. This is used when
1088 parsing code that we know will not be executed. This function may
1089 be called multiple times, and works as a stack. */
1091 static void
1092 c_disable_warnings (bool disable)
1094 if (disable)
1096 ++c_inhibit_evaluation_warnings;
1097 fold_defer_overflow_warnings ();
1101 /* If ENABLE is true, reenable issuing warnings. */
1103 static void
1104 c_enable_warnings (bool enable)
1106 if (enable)
1108 --c_inhibit_evaluation_warnings;
1109 fold_undefer_and_ignore_overflow_warnings ();
1113 /* Fully fold EXPR, an expression that was not folded (beyond integer
1114 constant expressions and null pointer constants) when being built
1115 up. If IN_INIT, this is in a static initializer and certain
1116 changes are made to the folding done. Clear *MAYBE_CONST if
1117 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1118 expression because it contains an evaluated operator (in C99) or an
1119 operator outside of sizeof returning an integer constant (in C90)
1120 not permitted in constant expressions, or because it contains an
1121 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1122 set to true by callers before calling this function.) Return the
1123 folded expression. Function arguments have already been folded
1124 before calling this function, as have the contents of SAVE_EXPR,
1125 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1126 C_MAYBE_CONST_EXPR. */
1128 tree
1129 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1131 tree ret;
1132 tree eptype = NULL_TREE;
1133 bool dummy = true;
1134 bool maybe_const_itself = true;
1135 location_t loc = EXPR_LOCATION (expr);
1137 /* This function is not relevant to C++ because C++ folds while
1138 parsing, and may need changes to be correct for C++ when C++
1139 stops folding while parsing. */
1140 if (c_dialect_cxx ())
1141 gcc_unreachable ();
1143 if (!maybe_const)
1144 maybe_const = &dummy;
1145 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1147 eptype = TREE_TYPE (expr);
1148 expr = TREE_OPERAND (expr, 0);
1150 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1151 &maybe_const_itself, false);
1152 if (eptype)
1153 ret = fold_convert_loc (loc, eptype, ret);
1154 *maybe_const &= maybe_const_itself;
1155 return ret;
1158 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1159 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1160 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1161 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1162 both evaluated and unevaluated subexpressions while
1163 *MAYBE_CONST_ITSELF is carried from only evaluated
1164 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1165 with integer constant operands, and if any of the operands doesn't
1166 get folded to an integer constant, don't fold the expression itself. */
1168 static tree
1169 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1170 bool *maybe_const_itself, bool for_int_const)
1172 tree ret = expr;
1173 enum tree_code code = TREE_CODE (expr);
1174 enum tree_code_class kind = TREE_CODE_CLASS (code);
1175 location_t loc = EXPR_LOCATION (expr);
1176 tree op0, op1, op2, op3;
1177 tree orig_op0, orig_op1, orig_op2;
1178 bool op0_const = true, op1_const = true, op2_const = true;
1179 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1180 bool nowarning = TREE_NO_WARNING (expr);
1181 bool unused_p;
1183 /* This function is not relevant to C++ because C++ folds while
1184 parsing, and may need changes to be correct for C++ when C++
1185 stops folding while parsing. */
1186 if (c_dialect_cxx ())
1187 gcc_unreachable ();
1189 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1190 anything else not counted as an expression cannot usefully be
1191 folded further at this point. */
1192 if (!IS_EXPR_CODE_CLASS (kind)
1193 || kind == tcc_statement
1194 || code == SAVE_EXPR)
1195 return expr;
1197 /* Operands of variable-length expressions (function calls) have
1198 already been folded, as have __builtin_* function calls, and such
1199 expressions cannot occur in constant expressions. */
1200 if (kind == tcc_vl_exp)
1202 *maybe_const_operands = false;
1203 ret = fold (expr);
1204 goto out;
1207 if (code == C_MAYBE_CONST_EXPR)
1209 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1210 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1211 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1212 *maybe_const_operands = false;
1213 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1215 *maybe_const_itself = false;
1216 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1217 maybe_const_itself, true);
1219 if (pre && !in_init)
1220 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1221 else
1222 ret = inner;
1223 goto out;
1226 /* Assignment, increment, decrement, function call and comma
1227 operators, and statement expressions, cannot occur in constant
1228 expressions if evaluated / outside of sizeof. (Function calls
1229 were handled above, though VA_ARG_EXPR is treated like a function
1230 call here, and statement expressions are handled through
1231 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1232 switch (code)
1234 case MODIFY_EXPR:
1235 case PREDECREMENT_EXPR:
1236 case PREINCREMENT_EXPR:
1237 case POSTDECREMENT_EXPR:
1238 case POSTINCREMENT_EXPR:
1239 case COMPOUND_EXPR:
1240 *maybe_const_operands = false;
1241 break;
1243 case VA_ARG_EXPR:
1244 case TARGET_EXPR:
1245 case BIND_EXPR:
1246 case OBJ_TYPE_REF:
1247 *maybe_const_operands = false;
1248 ret = fold (expr);
1249 goto out;
1251 default:
1252 break;
1255 /* Fold individual tree codes as appropriate. */
1256 switch (code)
1258 case COMPOUND_LITERAL_EXPR:
1259 /* Any non-constancy will have been marked in a containing
1260 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1261 goto out;
1263 case COMPONENT_REF:
1264 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1265 op1 = TREE_OPERAND (expr, 1);
1266 op2 = TREE_OPERAND (expr, 2);
1267 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1268 maybe_const_itself, for_int_const);
1269 STRIP_TYPE_NOPS (op0);
1270 if (op0 != orig_op0)
1271 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1272 if (ret != expr)
1274 TREE_READONLY (ret) = TREE_READONLY (expr);
1275 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1277 goto out;
1279 case ARRAY_REF:
1280 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1281 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1282 op2 = TREE_OPERAND (expr, 2);
1283 op3 = TREE_OPERAND (expr, 3);
1284 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1285 maybe_const_itself, for_int_const);
1286 STRIP_TYPE_NOPS (op0);
1287 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1288 maybe_const_itself, for_int_const);
1289 STRIP_TYPE_NOPS (op1);
1290 op1 = decl_constant_value_for_optimization (op1);
1291 if (op0 != orig_op0 || op1 != orig_op1)
1292 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1293 if (ret != expr)
1295 TREE_READONLY (ret) = TREE_READONLY (expr);
1296 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1297 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1299 ret = fold (ret);
1300 goto out;
1302 case COMPOUND_EXPR:
1303 case MODIFY_EXPR:
1304 case PREDECREMENT_EXPR:
1305 case PREINCREMENT_EXPR:
1306 case POSTDECREMENT_EXPR:
1307 case POSTINCREMENT_EXPR:
1308 case PLUS_EXPR:
1309 case MINUS_EXPR:
1310 case MULT_EXPR:
1311 case POINTER_PLUS_EXPR:
1312 case TRUNC_DIV_EXPR:
1313 case CEIL_DIV_EXPR:
1314 case FLOOR_DIV_EXPR:
1315 case TRUNC_MOD_EXPR:
1316 case RDIV_EXPR:
1317 case EXACT_DIV_EXPR:
1318 case LSHIFT_EXPR:
1319 case RSHIFT_EXPR:
1320 case BIT_IOR_EXPR:
1321 case BIT_XOR_EXPR:
1322 case BIT_AND_EXPR:
1323 case LT_EXPR:
1324 case LE_EXPR:
1325 case GT_EXPR:
1326 case GE_EXPR:
1327 case EQ_EXPR:
1328 case NE_EXPR:
1329 case COMPLEX_EXPR:
1330 case TRUTH_AND_EXPR:
1331 case TRUTH_OR_EXPR:
1332 case TRUTH_XOR_EXPR:
1333 case UNORDERED_EXPR:
1334 case ORDERED_EXPR:
1335 case UNLT_EXPR:
1336 case UNLE_EXPR:
1337 case UNGT_EXPR:
1338 case UNGE_EXPR:
1339 case UNEQ_EXPR:
1340 /* Binary operations evaluating both arguments (increment and
1341 decrement are binary internally in GCC). */
1342 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1343 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1344 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1345 maybe_const_itself, for_int_const);
1346 STRIP_TYPE_NOPS (op0);
1347 if (code != MODIFY_EXPR
1348 && code != PREDECREMENT_EXPR
1349 && code != PREINCREMENT_EXPR
1350 && code != POSTDECREMENT_EXPR
1351 && code != POSTINCREMENT_EXPR)
1352 op0 = decl_constant_value_for_optimization (op0);
1353 /* The RHS of a MODIFY_EXPR was fully folded when building that
1354 expression for the sake of conversion warnings. */
1355 if (code != MODIFY_EXPR)
1356 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1357 maybe_const_itself, for_int_const);
1358 STRIP_TYPE_NOPS (op1);
1359 op1 = decl_constant_value_for_optimization (op1);
1361 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1362 || TREE_CODE (op1) != INTEGER_CST))
1363 goto out;
1365 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1366 ret = in_init
1367 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1368 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1369 else
1370 ret = fold (expr);
1371 if (TREE_OVERFLOW_P (ret)
1372 && !TREE_OVERFLOW_P (op0)
1373 && !TREE_OVERFLOW_P (op1))
1374 overflow_warning (EXPR_LOCATION (expr), ret);
1375 if (code == LSHIFT_EXPR
1376 && TREE_CODE (orig_op0) != INTEGER_CST
1377 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1378 && TREE_CODE (op0) == INTEGER_CST
1379 && c_inhibit_evaluation_warnings == 0
1380 && tree_int_cst_sgn (op0) < 0)
1381 warning_at (loc, OPT_Wshift_negative_value,
1382 "left shift of negative value");
1383 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1384 && TREE_CODE (orig_op1) != INTEGER_CST
1385 && TREE_CODE (op1) == INTEGER_CST
1386 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1387 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1388 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1389 && c_inhibit_evaluation_warnings == 0)
1391 if (tree_int_cst_sgn (op1) < 0)
1392 warning_at (loc, OPT_Wshift_count_negative,
1393 (code == LSHIFT_EXPR
1394 ? G_("left shift count is negative")
1395 : G_("right shift count is negative")));
1396 else if (compare_tree_int (op1,
1397 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1398 >= 0)
1399 warning_at (loc, OPT_Wshift_count_overflow,
1400 (code == LSHIFT_EXPR
1401 ? G_("left shift count >= width of type")
1402 : G_("right shift count >= width of type")));
1404 if ((code == TRUNC_DIV_EXPR
1405 || code == CEIL_DIV_EXPR
1406 || code == FLOOR_DIV_EXPR
1407 || code == EXACT_DIV_EXPR
1408 || code == TRUNC_MOD_EXPR)
1409 && TREE_CODE (orig_op1) != INTEGER_CST
1410 && TREE_CODE (op1) == INTEGER_CST
1411 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1412 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1413 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1414 warn_for_div_by_zero (loc, op1);
1415 goto out;
1417 case INDIRECT_REF:
1418 case FIX_TRUNC_EXPR:
1419 case FLOAT_EXPR:
1420 CASE_CONVERT:
1421 case ADDR_SPACE_CONVERT_EXPR:
1422 case VIEW_CONVERT_EXPR:
1423 case NON_LVALUE_EXPR:
1424 case NEGATE_EXPR:
1425 case BIT_NOT_EXPR:
1426 case TRUTH_NOT_EXPR:
1427 case ADDR_EXPR:
1428 case CONJ_EXPR:
1429 case REALPART_EXPR:
1430 case IMAGPART_EXPR:
1431 /* Unary operations. */
1432 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1433 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1434 maybe_const_itself, for_int_const);
1435 STRIP_TYPE_NOPS (op0);
1436 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1437 op0 = decl_constant_value_for_optimization (op0);
1439 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1440 goto out;
1442 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1443 not prepared to deal with them if they occur in initializers. */
1444 if (op0 != orig_op0
1445 && code == ADDR_EXPR
1446 && (op1 = get_base_address (op0)) != NULL_TREE
1447 && INDIRECT_REF_P (op1)
1448 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1449 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1450 else if (op0 != orig_op0 || in_init)
1451 ret = in_init
1452 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1453 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1454 else
1455 ret = fold (expr);
1456 if (code == INDIRECT_REF
1457 && ret != expr
1458 && INDIRECT_REF_P (ret))
1460 TREE_READONLY (ret) = TREE_READONLY (expr);
1461 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1462 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1464 switch (code)
1466 case FIX_TRUNC_EXPR:
1467 case FLOAT_EXPR:
1468 CASE_CONVERT:
1469 /* Don't warn about explicit conversions. We will already
1470 have warned about suspect implicit conversions. */
1471 break;
1473 default:
1474 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1475 overflow_warning (EXPR_LOCATION (expr), ret);
1476 break;
1478 goto out;
1480 case TRUTH_ANDIF_EXPR:
1481 case TRUTH_ORIF_EXPR:
1482 /* Binary operations not necessarily evaluating both
1483 arguments. */
1484 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1485 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1486 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1487 for_int_const);
1488 STRIP_TYPE_NOPS (op0);
1490 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1491 ? truthvalue_false_node
1492 : truthvalue_true_node));
1493 c_disable_warnings (unused_p);
1494 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1495 for_int_const);
1496 STRIP_TYPE_NOPS (op1);
1497 c_enable_warnings (unused_p);
1499 if (for_int_const
1500 && (TREE_CODE (op0) != INTEGER_CST
1501 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1502 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1503 goto out;
1505 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1506 ret = in_init
1507 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1508 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1509 else
1510 ret = fold (expr);
1511 *maybe_const_operands &= op0_const;
1512 *maybe_const_itself &= op0_const_self;
1513 if (!(flag_isoc99
1514 && op0_const
1515 && op0_const_self
1516 && (code == TRUTH_ANDIF_EXPR
1517 ? op0 == truthvalue_false_node
1518 : op0 == truthvalue_true_node)))
1519 *maybe_const_operands &= op1_const;
1520 if (!(op0_const
1521 && op0_const_self
1522 && (code == TRUTH_ANDIF_EXPR
1523 ? op0 == truthvalue_false_node
1524 : op0 == truthvalue_true_node)))
1525 *maybe_const_itself &= op1_const_self;
1526 goto out;
1528 case COND_EXPR:
1529 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1530 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1531 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1532 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1533 for_int_const);
1535 STRIP_TYPE_NOPS (op0);
1536 c_disable_warnings (op0 == truthvalue_false_node);
1537 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1538 for_int_const);
1539 STRIP_TYPE_NOPS (op1);
1540 c_enable_warnings (op0 == truthvalue_false_node);
1542 c_disable_warnings (op0 == truthvalue_true_node);
1543 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1544 for_int_const);
1545 STRIP_TYPE_NOPS (op2);
1546 c_enable_warnings (op0 == truthvalue_true_node);
1548 if (for_int_const
1549 && (TREE_CODE (op0) != INTEGER_CST
1550 /* Only the evaluated operand must be an INTEGER_CST. */
1551 || (op0 == truthvalue_true_node
1552 ? TREE_CODE (op1) != INTEGER_CST
1553 : TREE_CODE (op2) != INTEGER_CST)))
1554 goto out;
1556 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1557 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1558 else
1559 ret = fold (expr);
1560 *maybe_const_operands &= op0_const;
1561 *maybe_const_itself &= op0_const_self;
1562 if (!(flag_isoc99
1563 && op0_const
1564 && op0_const_self
1565 && op0 == truthvalue_false_node))
1566 *maybe_const_operands &= op1_const;
1567 if (!(op0_const
1568 && op0_const_self
1569 && op0 == truthvalue_false_node))
1570 *maybe_const_itself &= op1_const_self;
1571 if (!(flag_isoc99
1572 && op0_const
1573 && op0_const_self
1574 && op0 == truthvalue_true_node))
1575 *maybe_const_operands &= op2_const;
1576 if (!(op0_const
1577 && op0_const_self
1578 && op0 == truthvalue_true_node))
1579 *maybe_const_itself &= op2_const_self;
1580 goto out;
1582 case EXCESS_PRECISION_EXPR:
1583 /* Each case where an operand with excess precision may be
1584 encountered must remove the EXCESS_PRECISION_EXPR around
1585 inner operands and possibly put one around the whole
1586 expression or possibly convert to the semantic type (which
1587 c_fully_fold does); we cannot tell at this stage which is
1588 appropriate in any particular case. */
1589 gcc_unreachable ();
1591 default:
1592 /* Various codes may appear through folding built-in functions
1593 and their arguments. */
1594 goto out;
1597 out:
1598 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1599 have been done by this point, so remove them again. */
1600 nowarning |= TREE_NO_WARNING (ret);
1601 STRIP_TYPE_NOPS (ret);
1602 if (nowarning && !TREE_NO_WARNING (ret))
1604 if (!CAN_HAVE_LOCATION_P (ret))
1605 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1606 TREE_NO_WARNING (ret) = 1;
1608 if (ret != expr)
1609 protected_set_expr_location (ret, loc);
1610 return ret;
1613 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1614 return EXP. Otherwise, return either EXP or its known constant
1615 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1616 Is the BLKmode test appropriate? */
1618 tree
1619 decl_constant_value_for_optimization (tree exp)
1621 tree ret;
1623 /* This function is only used by C, for c_fully_fold and other
1624 optimization, and may not be correct for C++. */
1625 if (c_dialect_cxx ())
1626 gcc_unreachable ();
1628 if (!optimize
1629 || !VAR_P (exp)
1630 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1631 || DECL_MODE (exp) == BLKmode)
1632 return exp;
1634 ret = decl_constant_value (exp);
1635 /* Avoid unwanted tree sharing between the initializer and current
1636 function's body where the tree can be modified e.g. by the
1637 gimplifier. */
1638 if (ret != exp && TREE_STATIC (exp))
1639 ret = unshare_expr (ret);
1640 return ret;
1643 /* Print a warning if a constant expression had overflow in folding.
1644 Invoke this function on every expression that the language
1645 requires to be a constant expression.
1646 Note the ANSI C standard says it is erroneous for a
1647 constant expression to overflow. */
1649 void
1650 constant_expression_warning (tree value)
1652 if (warn_overflow && pedantic
1653 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1654 || TREE_CODE (value) == FIXED_CST
1655 || TREE_CODE (value) == VECTOR_CST
1656 || TREE_CODE (value) == COMPLEX_CST)
1657 && TREE_OVERFLOW (value))
1658 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1661 /* The same as above but print an unconditional error. */
1662 void
1663 constant_expression_error (tree value)
1665 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1666 || TREE_CODE (value) == FIXED_CST
1667 || TREE_CODE (value) == VECTOR_CST
1668 || TREE_CODE (value) == COMPLEX_CST)
1669 && TREE_OVERFLOW (value))
1670 error ("overflow in constant expression");
1673 /* Print a warning if an expression had overflow in folding and its
1674 operands hadn't.
1676 Invoke this function on every expression that
1677 (1) appears in the source code, and
1678 (2) is a constant expression that overflowed, and
1679 (3) is not already checked by convert_and_check;
1680 however, do not invoke this function on operands of explicit casts
1681 or when the expression is the result of an operator and any operand
1682 already overflowed. */
1684 void
1685 overflow_warning (location_t loc, tree value)
1687 if (c_inhibit_evaluation_warnings != 0)
1688 return;
1690 switch (TREE_CODE (value))
1692 case INTEGER_CST:
1693 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1694 break;
1696 case REAL_CST:
1697 warning_at (loc, OPT_Woverflow,
1698 "floating point overflow in expression");
1699 break;
1701 case FIXED_CST:
1702 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1703 break;
1705 case VECTOR_CST:
1706 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1707 break;
1709 case COMPLEX_CST:
1710 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1711 warning_at (loc, OPT_Woverflow,
1712 "complex integer overflow in expression");
1713 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1714 warning_at (loc, OPT_Woverflow,
1715 "complex floating point overflow in expression");
1716 break;
1718 default:
1719 break;
1723 /* Warn about uses of logical || / && operator in a context where it
1724 is likely that the bitwise equivalent was intended by the
1725 programmer. We have seen an expression in which CODE is a binary
1726 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1727 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1728 void
1729 warn_logical_operator (location_t location, enum tree_code code, tree type,
1730 enum tree_code code_left, tree op_left,
1731 enum tree_code ARG_UNUSED (code_right), tree op_right)
1733 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1734 int in0_p, in1_p, in_p;
1735 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1736 bool strict_overflow_p = false;
1738 if (code != TRUTH_ANDIF_EXPR
1739 && code != TRUTH_AND_EXPR
1740 && code != TRUTH_ORIF_EXPR
1741 && code != TRUTH_OR_EXPR)
1742 return;
1744 /* We don't want to warn if either operand comes from a macro
1745 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1746 see PR61534. */
1747 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1748 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1749 return;
1751 /* Warn if &&/|| are being used in a context where it is
1752 likely that the bitwise equivalent was intended by the
1753 programmer. That is, an expression such as op && MASK
1754 where op should not be any boolean expression, nor a
1755 constant, and mask seems to be a non-boolean integer constant. */
1756 if (!truth_value_p (code_left)
1757 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1758 && !CONSTANT_CLASS_P (op_left)
1759 && !TREE_NO_WARNING (op_left)
1760 && TREE_CODE (op_right) == INTEGER_CST
1761 && !integer_zerop (op_right)
1762 && !integer_onep (op_right))
1764 if (or_op)
1765 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1766 " applied to non-boolean constant");
1767 else
1768 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1769 " applied to non-boolean constant");
1770 TREE_NO_WARNING (op_left) = true;
1771 return;
1774 /* We do not warn for constants because they are typical of macro
1775 expansions that test for features. */
1776 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1777 return;
1779 /* This warning only makes sense with logical operands. */
1780 if (!(truth_value_p (TREE_CODE (op_left))
1781 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1782 || !(truth_value_p (TREE_CODE (op_right))
1783 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1784 return;
1786 /* The range computations only work with scalars. */
1787 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1788 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1789 return;
1791 /* We first test whether either side separately is trivially true
1792 (with OR) or trivially false (with AND). If so, do not warn.
1793 This is a common idiom for testing ranges of data types in
1794 portable code. */
1795 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1796 if (!lhs)
1797 return;
1798 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1799 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1801 /* If this is an OR operation, invert both sides; now, the result
1802 should be always false to get a warning. */
1803 if (or_op)
1804 in0_p = !in0_p;
1806 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1807 if (tem && integer_zerop (tem))
1808 return;
1810 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1811 if (!rhs)
1812 return;
1813 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1814 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1816 /* If this is an OR operation, invert both sides; now, the result
1817 should be always false to get a warning. */
1818 if (or_op)
1819 in1_p = !in1_p;
1821 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1822 if (tem && integer_zerop (tem))
1823 return;
1825 /* If both expressions have the same operand, if we can merge the
1826 ranges, ... */
1827 if (operand_equal_p (lhs, rhs, 0)
1828 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1829 in1_p, low1, high1))
1831 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1832 /* ... and if the range test is always false, then warn. */
1833 if (tem && integer_zerop (tem))
1835 if (or_op)
1836 warning_at (location, OPT_Wlogical_op,
1837 "logical %<or%> of collectively exhaustive tests is "
1838 "always true");
1839 else
1840 warning_at (location, OPT_Wlogical_op,
1841 "logical %<and%> of mutually exclusive tests is "
1842 "always false");
1844 /* Or warn if the operands have exactly the same range, e.g.
1845 A > 0 && A > 0. */
1846 else if (tree_int_cst_equal (low0, low1)
1847 && tree_int_cst_equal (high0, high1))
1849 if (or_op)
1850 warning_at (location, OPT_Wlogical_op,
1851 "logical %<or%> of equal expressions");
1852 else
1853 warning_at (location, OPT_Wlogical_op,
1854 "logical %<and%> of equal expressions");
1859 /* Warn about logical not used on the left hand side operand of a comparison.
1860 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1861 Do not warn if RHS is of a boolean type. */
1863 void
1864 warn_logical_not_parentheses (location_t location, enum tree_code code,
1865 tree rhs)
1867 if (TREE_CODE_CLASS (code) != tcc_comparison
1868 || TREE_TYPE (rhs) == NULL_TREE
1869 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1870 return;
1872 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1873 !(x == 0) or !(y != 0). */
1874 if ((code == EQ_EXPR || code == NE_EXPR)
1875 && integer_zerop (rhs))
1876 return;
1878 warning_at (location, OPT_Wlogical_not_parentheses,
1879 "logical not is only applied to the left hand side of "
1880 "comparison");
1883 /* Warn if EXP contains any computations whose results are not used.
1884 Return true if a warning is printed; false otherwise. LOCUS is the
1885 (potential) location of the expression. */
1887 bool
1888 warn_if_unused_value (const_tree exp, location_t locus)
1890 restart:
1891 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1892 return false;
1894 /* Don't warn about void constructs. This includes casting to void,
1895 void function calls, and statement expressions with a final cast
1896 to void. */
1897 if (VOID_TYPE_P (TREE_TYPE (exp)))
1898 return false;
1900 if (EXPR_HAS_LOCATION (exp))
1901 locus = EXPR_LOCATION (exp);
1903 switch (TREE_CODE (exp))
1905 case PREINCREMENT_EXPR:
1906 case POSTINCREMENT_EXPR:
1907 case PREDECREMENT_EXPR:
1908 case POSTDECREMENT_EXPR:
1909 case MODIFY_EXPR:
1910 case INIT_EXPR:
1911 case TARGET_EXPR:
1912 case CALL_EXPR:
1913 case TRY_CATCH_EXPR:
1914 case WITH_CLEANUP_EXPR:
1915 case EXIT_EXPR:
1916 case VA_ARG_EXPR:
1917 return false;
1919 case BIND_EXPR:
1920 /* For a binding, warn if no side effect within it. */
1921 exp = BIND_EXPR_BODY (exp);
1922 goto restart;
1924 case SAVE_EXPR:
1925 case NON_LVALUE_EXPR:
1926 case NOP_EXPR:
1927 exp = TREE_OPERAND (exp, 0);
1928 goto restart;
1930 case TRUTH_ORIF_EXPR:
1931 case TRUTH_ANDIF_EXPR:
1932 /* In && or ||, warn if 2nd operand has no side effect. */
1933 exp = TREE_OPERAND (exp, 1);
1934 goto restart;
1936 case COMPOUND_EXPR:
1937 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1938 return true;
1939 /* Let people do `(foo (), 0)' without a warning. */
1940 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1941 return false;
1942 exp = TREE_OPERAND (exp, 1);
1943 goto restart;
1945 case COND_EXPR:
1946 /* If this is an expression with side effects, don't warn; this
1947 case commonly appears in macro expansions. */
1948 if (TREE_SIDE_EFFECTS (exp))
1949 return false;
1950 goto warn;
1952 case INDIRECT_REF:
1953 /* Don't warn about automatic dereferencing of references, since
1954 the user cannot control it. */
1955 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1957 exp = TREE_OPERAND (exp, 0);
1958 goto restart;
1960 /* Fall through. */
1962 default:
1963 /* Referencing a volatile value is a side effect, so don't warn. */
1964 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1965 && TREE_THIS_VOLATILE (exp))
1966 return false;
1968 /* If this is an expression which has no operands, there is no value
1969 to be unused. There are no such language-independent codes,
1970 but front ends may define such. */
1971 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1972 return false;
1974 warn:
1975 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1980 /* Print a warning about casts that might indicate violation
1981 of strict aliasing rules if -Wstrict-aliasing is used and
1982 strict aliasing mode is in effect. OTYPE is the original
1983 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1985 bool
1986 strict_aliasing_warning (tree otype, tree type, tree expr)
1988 /* Strip pointer conversion chains and get to the correct original type. */
1989 STRIP_NOPS (expr);
1990 otype = TREE_TYPE (expr);
1992 if (!(flag_strict_aliasing
1993 && POINTER_TYPE_P (type)
1994 && POINTER_TYPE_P (otype)
1995 && !VOID_TYPE_P (TREE_TYPE (type)))
1996 /* If the type we are casting to is a ref-all pointer
1997 dereferencing it is always valid. */
1998 || TYPE_REF_CAN_ALIAS_ALL (type))
1999 return false;
2001 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
2002 && (DECL_P (TREE_OPERAND (expr, 0))
2003 || handled_component_p (TREE_OPERAND (expr, 0))))
2005 /* Casting the address of an object to non void pointer. Warn
2006 if the cast breaks type based aliasing. */
2007 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2009 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2010 "might break strict-aliasing rules");
2011 return true;
2013 else
2015 /* warn_strict_aliasing >= 3. This includes the default (3).
2016 Only warn if the cast is dereferenced immediately. */
2017 alias_set_type set1 =
2018 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
2019 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2021 if (set1 != set2 && set2 != 0
2022 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
2024 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2025 "pointer will break strict-aliasing rules");
2026 return true;
2028 else if (warn_strict_aliasing == 2
2029 && !alias_sets_must_conflict_p (set1, set2))
2031 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2032 "pointer might break strict-aliasing rules");
2033 return true;
2037 else
2038 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2040 /* At this level, warn for any conversions, even if an address is
2041 not taken in the same statement. This will likely produce many
2042 false positives, but could be useful to pinpoint problems that
2043 are not revealed at higher levels. */
2044 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2045 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2046 if (!COMPLETE_TYPE_P (type)
2047 || !alias_sets_must_conflict_p (set1, set2))
2049 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2050 "pointer might break strict-aliasing rules");
2051 return true;
2055 return false;
2058 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2059 sizeof as last operand of certain builtins. */
2061 void
2062 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2063 vec<tree, va_gc> *params, tree *sizeof_arg,
2064 bool (*comp_types) (tree, tree))
2066 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2067 bool strop = false, cmp = false;
2068 unsigned int idx = ~0;
2069 location_t loc;
2071 if (TREE_CODE (callee) != FUNCTION_DECL
2072 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2073 || vec_safe_length (params) <= 1)
2074 return;
2076 switch (DECL_FUNCTION_CODE (callee))
2078 case BUILT_IN_STRNCMP:
2079 case BUILT_IN_STRNCASECMP:
2080 cmp = true;
2081 /* FALLTHRU */
2082 case BUILT_IN_STRNCPY:
2083 case BUILT_IN_STRNCPY_CHK:
2084 case BUILT_IN_STRNCAT:
2085 case BUILT_IN_STRNCAT_CHK:
2086 case BUILT_IN_STPNCPY:
2087 case BUILT_IN_STPNCPY_CHK:
2088 strop = true;
2089 /* FALLTHRU */
2090 case BUILT_IN_MEMCPY:
2091 case BUILT_IN_MEMCPY_CHK:
2092 case BUILT_IN_MEMMOVE:
2093 case BUILT_IN_MEMMOVE_CHK:
2094 if (params->length () < 3)
2095 return;
2096 src = (*params)[1];
2097 dest = (*params)[0];
2098 idx = 2;
2099 break;
2100 case BUILT_IN_BCOPY:
2101 if (params->length () < 3)
2102 return;
2103 src = (*params)[0];
2104 dest = (*params)[1];
2105 idx = 2;
2106 break;
2107 case BUILT_IN_MEMCMP:
2108 case BUILT_IN_BCMP:
2109 if (params->length () < 3)
2110 return;
2111 src = (*params)[1];
2112 dest = (*params)[0];
2113 idx = 2;
2114 cmp = true;
2115 break;
2116 case BUILT_IN_MEMSET:
2117 case BUILT_IN_MEMSET_CHK:
2118 if (params->length () < 3)
2119 return;
2120 dest = (*params)[0];
2121 idx = 2;
2122 break;
2123 case BUILT_IN_BZERO:
2124 dest = (*params)[0];
2125 idx = 1;
2126 break;
2127 case BUILT_IN_STRNDUP:
2128 src = (*params)[0];
2129 strop = true;
2130 idx = 1;
2131 break;
2132 case BUILT_IN_MEMCHR:
2133 if (params->length () < 3)
2134 return;
2135 src = (*params)[0];
2136 idx = 2;
2137 break;
2138 case BUILT_IN_SNPRINTF:
2139 case BUILT_IN_SNPRINTF_CHK:
2140 case BUILT_IN_VSNPRINTF:
2141 case BUILT_IN_VSNPRINTF_CHK:
2142 dest = (*params)[0];
2143 idx = 1;
2144 strop = true;
2145 break;
2146 default:
2147 break;
2150 if (idx >= 3)
2151 return;
2153 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2154 return;
2156 type = TYPE_P (sizeof_arg[idx])
2157 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2158 if (!POINTER_TYPE_P (type))
2159 return;
2161 if (dest
2162 && (tem = tree_strip_nop_conversions (dest))
2163 && POINTER_TYPE_P (TREE_TYPE (tem))
2164 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2165 return;
2167 if (src
2168 && (tem = tree_strip_nop_conversions (src))
2169 && POINTER_TYPE_P (TREE_TYPE (tem))
2170 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2171 return;
2173 loc = sizeof_arg_loc[idx];
2175 if (dest && !cmp)
2177 if (!TYPE_P (sizeof_arg[idx])
2178 && operand_equal_p (dest, sizeof_arg[idx], 0)
2179 && comp_types (TREE_TYPE (dest), type))
2181 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2182 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2183 "argument to %<sizeof%> in %qD call is the same "
2184 "expression as the destination; did you mean to "
2185 "remove the addressof?", callee);
2186 else if ((TYPE_PRECISION (TREE_TYPE (type))
2187 == TYPE_PRECISION (char_type_node))
2188 || strop)
2189 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2190 "argument to %<sizeof%> in %qD call is the same "
2191 "expression as the destination; did you mean to "
2192 "provide an explicit length?", callee);
2193 else
2194 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2195 "argument to %<sizeof%> in %qD call is the same "
2196 "expression as the destination; did you mean to "
2197 "dereference it?", callee);
2198 return;
2201 if (POINTER_TYPE_P (TREE_TYPE (dest))
2202 && !strop
2203 && comp_types (TREE_TYPE (dest), type)
2204 && !VOID_TYPE_P (TREE_TYPE (type)))
2206 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2207 "argument to %<sizeof%> in %qD call is the same "
2208 "pointer type %qT as the destination; expected %qT "
2209 "or an explicit length", callee, TREE_TYPE (dest),
2210 TREE_TYPE (TREE_TYPE (dest)));
2211 return;
2215 if (src && !cmp)
2217 if (!TYPE_P (sizeof_arg[idx])
2218 && operand_equal_p (src, sizeof_arg[idx], 0)
2219 && comp_types (TREE_TYPE (src), type))
2221 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2222 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2223 "argument to %<sizeof%> in %qD call is the same "
2224 "expression as the source; did you mean to "
2225 "remove the addressof?", callee);
2226 else if ((TYPE_PRECISION (TREE_TYPE (type))
2227 == TYPE_PRECISION (char_type_node))
2228 || strop)
2229 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2230 "argument to %<sizeof%> in %qD call is the same "
2231 "expression as the source; did you mean to "
2232 "provide an explicit length?", callee);
2233 else
2234 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2235 "argument to %<sizeof%> in %qD call is the same "
2236 "expression as the source; did you mean to "
2237 "dereference it?", callee);
2238 return;
2241 if (POINTER_TYPE_P (TREE_TYPE (src))
2242 && !strop
2243 && comp_types (TREE_TYPE (src), type)
2244 && !VOID_TYPE_P (TREE_TYPE (type)))
2246 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2247 "argument to %<sizeof%> in %qD call is the same "
2248 "pointer type %qT as the source; expected %qT "
2249 "or an explicit length", callee, TREE_TYPE (src),
2250 TREE_TYPE (TREE_TYPE (src)));
2251 return;
2255 if (dest)
2257 if (!TYPE_P (sizeof_arg[idx])
2258 && operand_equal_p (dest, sizeof_arg[idx], 0)
2259 && comp_types (TREE_TYPE (dest), type))
2261 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2262 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2263 "argument to %<sizeof%> in %qD call is the same "
2264 "expression as the first source; did you mean to "
2265 "remove the addressof?", callee);
2266 else if ((TYPE_PRECISION (TREE_TYPE (type))
2267 == TYPE_PRECISION (char_type_node))
2268 || strop)
2269 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2270 "argument to %<sizeof%> in %qD call is the same "
2271 "expression as the first source; did you mean to "
2272 "provide an explicit length?", callee);
2273 else
2274 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2275 "argument to %<sizeof%> in %qD call is the same "
2276 "expression as the first source; did you mean to "
2277 "dereference it?", callee);
2278 return;
2281 if (POINTER_TYPE_P (TREE_TYPE (dest))
2282 && !strop
2283 && comp_types (TREE_TYPE (dest), type)
2284 && !VOID_TYPE_P (TREE_TYPE (type)))
2286 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2287 "argument to %<sizeof%> in %qD call is the same "
2288 "pointer type %qT as the first source; expected %qT "
2289 "or an explicit length", callee, TREE_TYPE (dest),
2290 TREE_TYPE (TREE_TYPE (dest)));
2291 return;
2295 if (src)
2297 if (!TYPE_P (sizeof_arg[idx])
2298 && operand_equal_p (src, sizeof_arg[idx], 0)
2299 && comp_types (TREE_TYPE (src), type))
2301 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2302 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2303 "argument to %<sizeof%> in %qD call is the same "
2304 "expression as the second source; did you mean to "
2305 "remove the addressof?", callee);
2306 else if ((TYPE_PRECISION (TREE_TYPE (type))
2307 == TYPE_PRECISION (char_type_node))
2308 || strop)
2309 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2310 "argument to %<sizeof%> in %qD call is the same "
2311 "expression as the second source; did you mean to "
2312 "provide an explicit length?", callee);
2313 else
2314 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2315 "argument to %<sizeof%> in %qD call is the same "
2316 "expression as the second source; did you mean to "
2317 "dereference it?", callee);
2318 return;
2321 if (POINTER_TYPE_P (TREE_TYPE (src))
2322 && !strop
2323 && comp_types (TREE_TYPE (src), type)
2324 && !VOID_TYPE_P (TREE_TYPE (type)))
2326 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2327 "argument to %<sizeof%> in %qD call is the same "
2328 "pointer type %qT as the second source; expected %qT "
2329 "or an explicit length", callee, TREE_TYPE (src),
2330 TREE_TYPE (TREE_TYPE (src)));
2331 return;
2337 /* Warn for unlikely, improbable, or stupid DECL declarations
2338 of `main'. */
2340 void
2341 check_main_parameter_types (tree decl)
2343 function_args_iterator iter;
2344 tree type;
2345 int argct = 0;
2347 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2349 /* XXX void_type_node belies the abstraction. */
2350 if (type == void_type_node || type == error_mark_node )
2351 break;
2353 tree t = type;
2354 if (TYPE_ATOMIC (t))
2355 pedwarn (input_location, OPT_Wmain,
2356 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2357 type, decl);
2358 while (POINTER_TYPE_P (t))
2360 t = TREE_TYPE (t);
2361 if (TYPE_ATOMIC (t))
2362 pedwarn (input_location, OPT_Wmain,
2363 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2364 type, decl);
2367 ++argct;
2368 switch (argct)
2370 case 1:
2371 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2372 pedwarn (input_location, OPT_Wmain,
2373 "first argument of %q+D should be %<int%>", decl);
2374 break;
2376 case 2:
2377 if (TREE_CODE (type) != POINTER_TYPE
2378 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2379 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2380 != char_type_node))
2381 pedwarn (input_location, OPT_Wmain,
2382 "second argument of %q+D should be %<char **%>", decl);
2383 break;
2385 case 3:
2386 if (TREE_CODE (type) != POINTER_TYPE
2387 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2388 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2389 != char_type_node))
2390 pedwarn (input_location, OPT_Wmain,
2391 "third argument of %q+D should probably be "
2392 "%<char **%>", decl);
2393 break;
2397 /* It is intentional that this message does not mention the third
2398 argument because it's only mentioned in an appendix of the
2399 standard. */
2400 if (argct > 0 && (argct < 2 || argct > 3))
2401 pedwarn (input_location, OPT_Wmain,
2402 "%q+D takes only zero or two arguments", decl);
2404 if (stdarg_p (TREE_TYPE (decl)))
2405 pedwarn (input_location, OPT_Wmain,
2406 "%q+D declared as variadic function", decl);
2409 /* vector_targets_convertible_p is used for vector pointer types. The
2410 callers perform various checks that the qualifiers are satisfactory,
2411 while OTOH vector_targets_convertible_p ignores the number of elements
2412 in the vectors. That's fine with vector pointers as we can consider,
2413 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2414 and that does not require and conversion of the pointer values.
2415 In contrast, vector_types_convertible_p and
2416 vector_types_compatible_elements_p are used for vector value types. */
2417 /* True if pointers to distinct types T1 and T2 can be converted to
2418 each other without an explicit cast. Only returns true for opaque
2419 vector types. */
2420 bool
2421 vector_targets_convertible_p (const_tree t1, const_tree t2)
2423 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
2424 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2425 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2426 return true;
2428 return false;
2431 /* vector_types_convertible_p is used for vector value types.
2432 It could in principle call vector_targets_convertible_p as a subroutine,
2433 but then the check for vector type would be duplicated with its callers,
2434 and also the purpose of vector_targets_convertible_p would become
2435 muddled.
2436 Where vector_types_convertible_p returns true, a conversion might still be
2437 needed to make the types match.
2438 In contrast, vector_targets_convertible_p is used for vector pointer
2439 values, and vector_types_compatible_elements_p is used specifically
2440 in the context for binary operators, as a check if use is possible without
2441 conversion. */
2442 /* True if vector types T1 and T2 can be converted to each other
2443 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2444 can only be converted with -flax-vector-conversions yet that is not
2445 in effect, emit a note telling the user about that option if such
2446 a note has not previously been emitted. */
2447 bool
2448 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2450 static bool emitted_lax_note = false;
2451 bool convertible_lax;
2453 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2454 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2455 return true;
2457 convertible_lax =
2458 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2459 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2460 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2461 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2462 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2464 if (!convertible_lax || flag_lax_vector_conversions)
2465 return convertible_lax;
2467 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2468 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2469 return true;
2471 if (emit_lax_note && !emitted_lax_note)
2473 emitted_lax_note = true;
2474 inform (input_location, "use -flax-vector-conversions to permit "
2475 "conversions between vectors with differing "
2476 "element types or numbers of subparts");
2479 return false;
2482 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2483 and have vector types, V0 has the same type as V1, and the number of
2484 elements of V0, V1, MASK is the same.
2486 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2487 called with two arguments. In this case implementation passes the
2488 first argument twice in order to share the same tree code. This fact
2489 could enable the mask-values being twice the vector length. This is
2490 an implementation accident and this semantics is not guaranteed to
2491 the user. */
2492 tree
2493 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2494 bool complain)
2496 tree ret;
2497 bool wrap = true;
2498 bool maybe_const = false;
2499 bool two_arguments = false;
2501 if (v1 == NULL_TREE)
2503 two_arguments = true;
2504 v1 = v0;
2507 if (v0 == error_mark_node || v1 == error_mark_node
2508 || mask == error_mark_node)
2509 return error_mark_node;
2511 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2513 if (complain)
2514 error_at (loc, "__builtin_shuffle last argument must "
2515 "be an integer vector");
2516 return error_mark_node;
2519 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2520 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2522 if (complain)
2523 error_at (loc, "__builtin_shuffle arguments must be vectors");
2524 return error_mark_node;
2527 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2529 if (complain)
2530 error_at (loc, "__builtin_shuffle argument vectors must be of "
2531 "the same type");
2532 return error_mark_node;
2535 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2536 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2537 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2538 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2540 if (complain)
2541 error_at (loc, "__builtin_shuffle number of elements of the "
2542 "argument vector(s) and the mask vector should "
2543 "be the same");
2544 return error_mark_node;
2547 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2548 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2550 if (complain)
2551 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2552 "must have the same size as inner type of the mask");
2553 return error_mark_node;
2556 if (!c_dialect_cxx ())
2558 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2559 v0 = c_fully_fold (v0, false, &maybe_const);
2560 wrap &= maybe_const;
2562 if (two_arguments)
2563 v1 = v0 = save_expr (v0);
2564 else
2566 v1 = c_fully_fold (v1, false, &maybe_const);
2567 wrap &= maybe_const;
2570 mask = c_fully_fold (mask, false, &maybe_const);
2571 wrap &= maybe_const;
2573 else if (two_arguments)
2574 v1 = v0 = save_expr (v0);
2576 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2578 if (!c_dialect_cxx () && !wrap)
2579 ret = c_wrap_maybe_const (ret, true);
2581 return ret;
2584 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2585 to integral type. */
2587 static tree
2588 c_common_get_narrower (tree op, int *unsignedp_ptr)
2590 op = get_narrower (op, unsignedp_ptr);
2592 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2593 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2595 /* C++0x scoped enumerations don't implicitly convert to integral
2596 type; if we stripped an explicit conversion to a larger type we
2597 need to replace it so common_type will still work. */
2598 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2599 TYPE_UNSIGNED (TREE_TYPE (op)));
2600 op = fold_convert (type, op);
2602 return op;
2605 /* This is a helper function of build_binary_op.
2607 For certain operations if both args were extended from the same
2608 smaller type, do the arithmetic in that type and then extend.
2610 BITWISE indicates a bitwise operation.
2611 For them, this optimization is safe only if
2612 both args are zero-extended or both are sign-extended.
2613 Otherwise, we might change the result.
2614 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2615 but calculated in (unsigned short) it would be (unsigned short)-1.
2617 tree
2618 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2620 int unsigned0, unsigned1;
2621 tree arg0, arg1;
2622 int uns;
2623 tree type;
2625 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2626 excessive narrowing when we call get_narrower below. For
2627 example, suppose that OP0 is of unsigned int extended
2628 from signed char and that RESULT_TYPE is long long int.
2629 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2630 like
2632 (long long int) (unsigned int) signed_char
2634 which get_narrower would narrow down to
2636 (unsigned int) signed char
2638 If we do not cast OP0 first, get_narrower would return
2639 signed_char, which is inconsistent with the case of the
2640 explicit cast. */
2641 op0 = convert (result_type, op0);
2642 op1 = convert (result_type, op1);
2644 arg0 = c_common_get_narrower (op0, &unsigned0);
2645 arg1 = c_common_get_narrower (op1, &unsigned1);
2647 /* UNS is 1 if the operation to be done is an unsigned one. */
2648 uns = TYPE_UNSIGNED (result_type);
2650 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2651 but it *requires* conversion to FINAL_TYPE. */
2653 if ((TYPE_PRECISION (TREE_TYPE (op0))
2654 == TYPE_PRECISION (TREE_TYPE (arg0)))
2655 && TREE_TYPE (op0) != result_type)
2656 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2657 if ((TYPE_PRECISION (TREE_TYPE (op1))
2658 == TYPE_PRECISION (TREE_TYPE (arg1)))
2659 && TREE_TYPE (op1) != result_type)
2660 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2662 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2664 /* For bitwise operations, signedness of nominal type
2665 does not matter. Consider only how operands were extended. */
2666 if (bitwise)
2667 uns = unsigned0;
2669 /* Note that in all three cases below we refrain from optimizing
2670 an unsigned operation on sign-extended args.
2671 That would not be valid. */
2673 /* Both args variable: if both extended in same way
2674 from same width, do it in that width.
2675 Do it unsigned if args were zero-extended. */
2676 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2677 < TYPE_PRECISION (result_type))
2678 && (TYPE_PRECISION (TREE_TYPE (arg1))
2679 == TYPE_PRECISION (TREE_TYPE (arg0)))
2680 && unsigned0 == unsigned1
2681 && (unsigned0 || !uns))
2682 return c_common_signed_or_unsigned_type
2683 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2685 else if (TREE_CODE (arg0) == INTEGER_CST
2686 && (unsigned1 || !uns)
2687 && (TYPE_PRECISION (TREE_TYPE (arg1))
2688 < TYPE_PRECISION (result_type))
2689 && (type
2690 = c_common_signed_or_unsigned_type (unsigned1,
2691 TREE_TYPE (arg1)))
2692 && !POINTER_TYPE_P (type)
2693 && int_fits_type_p (arg0, type))
2694 return type;
2696 else if (TREE_CODE (arg1) == INTEGER_CST
2697 && (unsigned0 || !uns)
2698 && (TYPE_PRECISION (TREE_TYPE (arg0))
2699 < TYPE_PRECISION (result_type))
2700 && (type
2701 = c_common_signed_or_unsigned_type (unsigned0,
2702 TREE_TYPE (arg0)))
2703 && !POINTER_TYPE_P (type)
2704 && int_fits_type_p (arg1, type))
2705 return type;
2707 return result_type;
2710 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2711 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2713 static bool
2714 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2716 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2717 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2718 REAL_VALUE_TYPE real_low_bound =
2719 real_value_from_int_cst (0, type_low_bound);
2720 REAL_VALUE_TYPE real_high_bound =
2721 real_value_from_int_cst (0, type_high_bound);
2723 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2724 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2727 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2728 to the complex/real/integer type TYPE. Function returns non-zero when:
2729 * EXPR is a constant which cannot be exactly converted to TYPE.
2730 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2731 for EXPR type and TYPE being both integers or both real, or both
2732 complex.
2733 * EXPR is not a constant of complex type and TYPE is a real or
2734 an integer.
2735 * EXPR is not a constant of real type and TYPE is an integer.
2736 * EXPR is not a constant of integer type which cannot be
2737 exactly converted to real type.
2739 Function allows conversions between types of different signedness and
2740 can return SAFE_CONVERSION (zero) in that case. Function can produce
2741 signedness warnings if PRODUCE_WARNS is true.
2743 Function allows conversions from complex constants to non-complex types,
2744 provided that imaginary part is zero and real part can be safely converted
2745 to TYPE. */
2747 enum conversion_safety
2748 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2750 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2751 tree expr_type = TREE_TYPE (expr);
2752 loc = expansion_point_location_if_in_system_header (loc);
2754 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2756 /* If type is complex, we are interested in compatibility with
2757 underlying type. */
2758 if (TREE_CODE (type) == COMPLEX_TYPE)
2759 type = TREE_TYPE (type);
2761 /* Warn for real constant that is not an exact integer converted
2762 to integer type. */
2763 if (TREE_CODE (expr_type) == REAL_TYPE
2764 && TREE_CODE (type) == INTEGER_TYPE)
2766 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2767 give_warning = UNSAFE_REAL;
2769 /* Warn for an integer constant that does not fit into integer type. */
2770 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2771 && TREE_CODE (type) == INTEGER_TYPE
2772 && !int_fits_type_p (expr, type))
2774 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2775 && tree_int_cst_sgn (expr) < 0)
2777 if (produce_warns)
2778 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2779 " implicitly converted to unsigned type");
2781 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2783 if (produce_warns)
2784 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2785 " constant value to negative integer");
2787 else
2788 give_warning = UNSAFE_OTHER;
2790 else if (TREE_CODE (type) == REAL_TYPE)
2792 /* Warn for an integer constant that does not fit into real type. */
2793 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2795 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2796 if (!exact_real_truncate (TYPE_MODE (type), &a))
2797 give_warning = UNSAFE_REAL;
2799 /* Warn for a real constant that does not fit into a smaller
2800 real type. */
2801 else if (TREE_CODE (expr_type) == REAL_TYPE
2802 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2804 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2805 if (!exact_real_truncate (TYPE_MODE (type), &a))
2806 give_warning = UNSAFE_REAL;
2811 else if (TREE_CODE (expr) == COMPLEX_CST)
2813 tree imag_part = TREE_IMAGPART (expr);
2814 /* Conversion from complex constant with zero imaginary part,
2815 perform check for conversion of real part. */
2816 if ((TREE_CODE (imag_part) == REAL_CST
2817 && real_zerop (imag_part))
2818 || (TREE_CODE (imag_part) == INTEGER_CST
2819 && integer_zerop (imag_part)))
2820 /* Note: in this branch we use recursive call to unsafe_conversion_p
2821 with different type of EXPR, but it is still safe, because when EXPR
2822 is a constant, it's type is not used in text of generated warnings
2823 (otherwise they could sound misleading). */
2824 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2825 produce_warns);
2826 /* Conversion from complex constant with non-zero imaginary part. */
2827 else
2829 /* Conversion to complex type.
2830 Perform checks for both real and imaginary parts. */
2831 if (TREE_CODE (type) == COMPLEX_TYPE)
2833 /* Unfortunately, produce_warns must be false in two subsequent
2834 calls of unsafe_conversion_p, because otherwise we could
2835 produce strange "double" warnings, if both real and imaginary
2836 parts have conversion problems related to signedness.
2838 For example:
2839 int32_t _Complex a = 0x80000000 + 0x80000000i;
2841 Possible solution: add a separate function for checking
2842 constants and combine result of two calls appropriately. */
2843 enum conversion_safety re_safety =
2844 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2845 enum conversion_safety im_safety =
2846 unsafe_conversion_p (loc, type, imag_part, false);
2848 /* Merge the results into appropriate single warning. */
2850 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2851 if (re_safety == im_safety)
2852 give_warning = re_safety;
2853 else if (!re_safety && im_safety)
2854 give_warning = im_safety;
2855 else if (re_safety && !im_safety)
2856 give_warning = re_safety;
2857 else
2858 give_warning = UNSAFE_OTHER;
2860 /* Warn about conversion from complex to real or integer type. */
2861 else
2862 give_warning = UNSAFE_IMAGINARY;
2866 /* Checks for remaining case: EXPR is not constant. */
2867 else
2869 /* Warn for real types converted to integer types. */
2870 if (TREE_CODE (expr_type) == REAL_TYPE
2871 && TREE_CODE (type) == INTEGER_TYPE)
2872 give_warning = UNSAFE_REAL;
2874 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2875 && TREE_CODE (type) == INTEGER_TYPE)
2877 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2878 expr = get_unwidened (expr, 0);
2879 expr_type = TREE_TYPE (expr);
2881 /* Don't warn for short y; short x = ((int)y & 0xff); */
2882 if (TREE_CODE (expr) == BIT_AND_EXPR
2883 || TREE_CODE (expr) == BIT_IOR_EXPR
2884 || TREE_CODE (expr) == BIT_XOR_EXPR)
2886 /* If both args were extended from a shortest type,
2887 use that type if that is safe. */
2888 expr_type = shorten_binary_op (expr_type,
2889 TREE_OPERAND (expr, 0),
2890 TREE_OPERAND (expr, 1),
2891 /* bitwise */1);
2893 if (TREE_CODE (expr) == BIT_AND_EXPR)
2895 tree op0 = TREE_OPERAND (expr, 0);
2896 tree op1 = TREE_OPERAND (expr, 1);
2897 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2898 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2900 /* If one of the operands is a non-negative constant
2901 that fits in the target type, then the type of the
2902 other operand does not matter. */
2903 if ((TREE_CODE (op0) == INTEGER_CST
2904 && int_fits_type_p (op0, c_common_signed_type (type))
2905 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2906 || (TREE_CODE (op1) == INTEGER_CST
2907 && int_fits_type_p (op1, c_common_signed_type (type))
2908 && int_fits_type_p (op1,
2909 c_common_unsigned_type (type))))
2910 return SAFE_CONVERSION;
2911 /* If constant is unsigned and fits in the target
2912 type, then the result will also fit. */
2913 else if ((TREE_CODE (op0) == INTEGER_CST
2914 && unsigned0
2915 && int_fits_type_p (op0, type))
2916 || (TREE_CODE (op1) == INTEGER_CST
2917 && unsigned1
2918 && int_fits_type_p (op1, type)))
2919 return SAFE_CONVERSION;
2922 /* Warn for integer types converted to smaller integer types. */
2923 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2924 give_warning = UNSAFE_OTHER;
2926 /* When they are the same width but different signedness,
2927 then the value may change. */
2928 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2929 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2930 /* Even when converted to a bigger type, if the type is
2931 unsigned but expr is signed, then negative values
2932 will be changed. */
2933 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2934 && produce_warns)
2935 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2936 "may change the sign of the result",
2937 type, expr_type);
2940 /* Warn for integer types converted to real types if and only if
2941 all the range of values of the integer type cannot be
2942 represented by the real type. */
2943 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2944 && TREE_CODE (type) == REAL_TYPE)
2946 /* Don't warn about char y = 0xff; float x = (int) y; */
2947 expr = get_unwidened (expr, 0);
2948 expr_type = TREE_TYPE (expr);
2950 if (!int_safely_convertible_to_real_p (expr_type, type))
2951 give_warning = UNSAFE_OTHER;
2954 /* Warn for real types converted to smaller real types. */
2955 else if (TREE_CODE (expr_type) == REAL_TYPE
2956 && TREE_CODE (type) == REAL_TYPE
2957 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2958 give_warning = UNSAFE_REAL;
2960 /* Check conversion between two complex types. */
2961 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2962 && TREE_CODE (type) == COMPLEX_TYPE)
2964 /* Extract underlying types (i.e., type of real and imaginary
2965 parts) of expr_type and type. */
2966 tree from_type = TREE_TYPE (expr_type);
2967 tree to_type = TREE_TYPE (type);
2969 /* Warn for real types converted to integer types. */
2970 if (TREE_CODE (from_type) == REAL_TYPE
2971 && TREE_CODE (to_type) == INTEGER_TYPE)
2972 give_warning = UNSAFE_REAL;
2974 /* Warn for real types converted to smaller real types. */
2975 else if (TREE_CODE (from_type) == REAL_TYPE
2976 && TREE_CODE (to_type) == REAL_TYPE
2977 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2978 give_warning = UNSAFE_REAL;
2980 /* Check conversion for complex integer types. Here implementation
2981 is simpler than for real-domain integers because it does not
2982 involve sophisticated cases, such as bitmasks, casts, etc. */
2983 else if (TREE_CODE (from_type) == INTEGER_TYPE
2984 && TREE_CODE (to_type) == INTEGER_TYPE)
2986 /* Warn for integer types converted to smaller integer types. */
2987 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2988 give_warning = UNSAFE_OTHER;
2990 /* Check for different signedness, see case for real-domain
2991 integers (above) for a more detailed comment. */
2992 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2993 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2994 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2995 && produce_warns)
2996 warning_at (loc, OPT_Wsign_conversion,
2997 "conversion to %qT from %qT "
2998 "may change the sign of the result",
2999 type, expr_type);
3001 else if (TREE_CODE (from_type) == INTEGER_TYPE
3002 && TREE_CODE (to_type) == REAL_TYPE
3003 && !int_safely_convertible_to_real_p (from_type, to_type))
3004 give_warning = UNSAFE_OTHER;
3007 /* Warn for complex types converted to real or integer types. */
3008 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3009 && TREE_CODE (type) != COMPLEX_TYPE)
3010 give_warning = UNSAFE_IMAGINARY;
3013 return give_warning;
3016 /* Warns if the conversion of EXPR to TYPE may alter a value.
3017 This is a helper function for warnings_for_convert_and_check. */
3019 static void
3020 conversion_warning (location_t loc, tree type, tree expr)
3022 tree expr_type = TREE_TYPE (expr);
3023 enum conversion_safety conversion_kind;
3025 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
3026 return;
3028 /* This may happen, because for LHS op= RHS we preevaluate
3029 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3030 means we could no longer see the code of the EXPR. */
3031 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3032 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3033 if (TREE_CODE (expr) == SAVE_EXPR)
3034 expr = TREE_OPERAND (expr, 0);
3036 switch (TREE_CODE (expr))
3038 case EQ_EXPR:
3039 case NE_EXPR:
3040 case LE_EXPR:
3041 case GE_EXPR:
3042 case LT_EXPR:
3043 case GT_EXPR:
3044 case TRUTH_ANDIF_EXPR:
3045 case TRUTH_ORIF_EXPR:
3046 case TRUTH_AND_EXPR:
3047 case TRUTH_OR_EXPR:
3048 case TRUTH_XOR_EXPR:
3049 case TRUTH_NOT_EXPR:
3050 /* Conversion from boolean to a signed:1 bit-field (which only
3051 can hold the values 0 and -1) doesn't lose information - but
3052 it does change the value. */
3053 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3054 warning_at (loc, OPT_Wconversion,
3055 "conversion to %qT from boolean expression", type);
3056 return;
3058 case REAL_CST:
3059 case INTEGER_CST:
3060 case COMPLEX_CST:
3061 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3062 if (conversion_kind == UNSAFE_REAL)
3063 warning_at (loc, OPT_Wfloat_conversion,
3064 "conversion to %qT alters %qT constant value",
3065 type, expr_type);
3066 else if (conversion_kind)
3067 warning_at (loc, OPT_Wconversion,
3068 "conversion to %qT alters %qT constant value",
3069 type, expr_type);
3070 return;
3072 case COND_EXPR:
3074 /* In case of COND_EXPR, we do not care about the type of
3075 COND_EXPR, only about the conversion of each operand. */
3076 tree op1 = TREE_OPERAND (expr, 1);
3077 tree op2 = TREE_OPERAND (expr, 2);
3079 conversion_warning (loc, type, op1);
3080 conversion_warning (loc, type, op2);
3081 return;
3084 default: /* 'expr' is not a constant. */
3085 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3086 if (conversion_kind == UNSAFE_REAL)
3087 warning_at (loc, OPT_Wfloat_conversion,
3088 "conversion to %qT from %qT may alter its value",
3089 type, expr_type);
3090 else if (conversion_kind == UNSAFE_IMAGINARY)
3091 warning_at (loc, OPT_Wconversion,
3092 "conversion to %qT from %qT discards imaginary component",
3093 type, expr_type);
3094 else if (conversion_kind)
3095 warning_at (loc, OPT_Wconversion,
3096 "conversion to %qT from %qT may alter its value",
3097 type, expr_type);
3101 /* Produce warnings after a conversion. RESULT is the result of
3102 converting EXPR to TYPE. This is a helper function for
3103 convert_and_check and cp_convert_and_check. */
3105 void
3106 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3107 tree result)
3109 loc = expansion_point_location_if_in_system_header (loc);
3111 if (TREE_CODE (expr) == INTEGER_CST
3112 && (TREE_CODE (type) == INTEGER_TYPE
3113 || TREE_CODE (type) == ENUMERAL_TYPE)
3114 && !int_fits_type_p (expr, type))
3116 /* Do not diagnose overflow in a constant expression merely
3117 because a conversion overflowed. */
3118 if (TREE_OVERFLOW (result))
3119 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3121 if (TYPE_UNSIGNED (type))
3123 /* This detects cases like converting -129 or 256 to
3124 unsigned char. */
3125 if (!int_fits_type_p (expr, c_common_signed_type (type)))
3126 warning_at (loc, OPT_Woverflow,
3127 "large integer implicitly truncated to unsigned type");
3128 else
3129 conversion_warning (loc, type, expr);
3131 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
3132 warning_at (loc, OPT_Woverflow,
3133 "overflow in implicit constant conversion");
3134 /* No warning for converting 0x80000000 to int. */
3135 else if (pedantic
3136 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3137 || TYPE_PRECISION (TREE_TYPE (expr))
3138 != TYPE_PRECISION (type)))
3139 warning_at (loc, OPT_Woverflow,
3140 "overflow in implicit constant conversion");
3142 else
3143 conversion_warning (loc, type, expr);
3145 else if ((TREE_CODE (result) == INTEGER_CST
3146 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
3147 warning_at (loc, OPT_Woverflow,
3148 "overflow in implicit constant conversion");
3149 else
3150 conversion_warning (loc, type, expr);
3154 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3155 Invoke this function on every expression that is converted implicitly,
3156 i.e. because of language rules and not because of an explicit cast. */
3158 tree
3159 convert_and_check (location_t loc, tree type, tree expr)
3161 tree result;
3162 tree expr_for_warning;
3164 /* Convert from a value with possible excess precision rather than
3165 via the semantic type, but do not warn about values not fitting
3166 exactly in the semantic type. */
3167 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3169 tree orig_type = TREE_TYPE (expr);
3170 expr = TREE_OPERAND (expr, 0);
3171 expr_for_warning = convert (orig_type, expr);
3172 if (orig_type == type)
3173 return expr_for_warning;
3175 else
3176 expr_for_warning = expr;
3178 if (TREE_TYPE (expr) == type)
3179 return expr;
3181 result = convert (type, expr);
3183 if (c_inhibit_evaluation_warnings == 0
3184 && !TREE_OVERFLOW_P (expr)
3185 && result != error_mark_node)
3186 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3188 return result;
3191 /* A node in a list that describes references to variables (EXPR), which are
3192 either read accesses if WRITER is zero, or write accesses, in which case
3193 WRITER is the parent of EXPR. */
3194 struct tlist
3196 struct tlist *next;
3197 tree expr, writer;
3200 /* Used to implement a cache the results of a call to verify_tree. We only
3201 use this for SAVE_EXPRs. */
3202 struct tlist_cache
3204 struct tlist_cache *next;
3205 struct tlist *cache_before_sp;
3206 struct tlist *cache_after_sp;
3207 tree expr;
3210 /* Obstack to use when allocating tlist structures, and corresponding
3211 firstobj. */
3212 static struct obstack tlist_obstack;
3213 static char *tlist_firstobj = 0;
3215 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3216 warnings. */
3217 static struct tlist *warned_ids;
3218 /* SAVE_EXPRs need special treatment. We process them only once and then
3219 cache the results. */
3220 static struct tlist_cache *save_expr_cache;
3222 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3223 static void merge_tlist (struct tlist **, struct tlist *, int);
3224 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3225 static int warning_candidate_p (tree);
3226 static bool candidate_equal_p (const_tree, const_tree);
3227 static void warn_for_collisions (struct tlist *);
3228 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3229 static struct tlist *new_tlist (struct tlist *, tree, tree);
3231 /* Create a new struct tlist and fill in its fields. */
3232 static struct tlist *
3233 new_tlist (struct tlist *next, tree t, tree writer)
3235 struct tlist *l;
3236 l = XOBNEW (&tlist_obstack, struct tlist);
3237 l->next = next;
3238 l->expr = t;
3239 l->writer = writer;
3240 return l;
3243 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3244 is nonnull, we ignore any node we find which has a writer equal to it. */
3246 static void
3247 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3249 while (add)
3251 struct tlist *next = add->next;
3252 if (!copy)
3253 add->next = *to;
3254 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3255 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3256 add = next;
3260 /* Merge the nodes of ADD into TO. This merging process is done so that for
3261 each variable that already exists in TO, no new node is added; however if
3262 there is a write access recorded in ADD, and an occurrence on TO is only
3263 a read access, then the occurrence in TO will be modified to record the
3264 write. */
3266 static void
3267 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3269 struct tlist **end = to;
3271 while (*end)
3272 end = &(*end)->next;
3274 while (add)
3276 int found = 0;
3277 struct tlist *tmp2;
3278 struct tlist *next = add->next;
3280 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3281 if (candidate_equal_p (tmp2->expr, add->expr))
3283 found = 1;
3284 if (!tmp2->writer)
3285 tmp2->writer = add->writer;
3287 if (!found)
3289 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3290 end = &(*end)->next;
3291 *end = 0;
3293 add = next;
3297 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3298 references in list LIST conflict with it, excluding reads if ONLY writers
3299 is nonzero. */
3301 static void
3302 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3303 int only_writes)
3305 struct tlist *tmp;
3307 /* Avoid duplicate warnings. */
3308 for (tmp = warned_ids; tmp; tmp = tmp->next)
3309 if (candidate_equal_p (tmp->expr, written))
3310 return;
3312 while (list)
3314 if (candidate_equal_p (list->expr, written)
3315 && !candidate_equal_p (list->writer, writer)
3316 && (!only_writes || list->writer))
3318 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3319 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3320 OPT_Wsequence_point, "operation on %qE may be undefined",
3321 list->expr);
3323 list = list->next;
3327 /* Given a list LIST of references to variables, find whether any of these
3328 can cause conflicts due to missing sequence points. */
3330 static void
3331 warn_for_collisions (struct tlist *list)
3333 struct tlist *tmp;
3335 for (tmp = list; tmp; tmp = tmp->next)
3337 if (tmp->writer)
3338 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3342 /* Return nonzero if X is a tree that can be verified by the sequence point
3343 warnings. */
3344 static int
3345 warning_candidate_p (tree x)
3347 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3348 return 0;
3350 if (TREE_CODE (x) == BLOCK)
3351 return 0;
3353 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3354 (lvalue_p) crash on TRY/CATCH. */
3355 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3356 return 0;
3358 if (!lvalue_p (x))
3359 return 0;
3361 /* No point to track non-const calls, they will never satisfy
3362 operand_equal_p. */
3363 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3364 return 0;
3366 if (TREE_CODE (x) == STRING_CST)
3367 return 0;
3369 return 1;
3372 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3373 static bool
3374 candidate_equal_p (const_tree x, const_tree y)
3376 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3379 /* Walk the tree X, and record accesses to variables. If X is written by the
3380 parent tree, WRITER is the parent.
3381 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3382 expression or its only operand forces a sequence point, then everything up
3383 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3384 in PNO_SP.
3385 Once we return, we will have emitted warnings if any subexpression before
3386 such a sequence point could be undefined. On a higher level, however, the
3387 sequence point may not be relevant, and we'll merge the two lists.
3389 Example: (b++, a) + b;
3390 The call that processes the COMPOUND_EXPR will store the increment of B
3391 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3392 processes the PLUS_EXPR will need to merge the two lists so that
3393 eventually, all accesses end up on the same list (and we'll warn about the
3394 unordered subexpressions b++ and b.
3396 A note on merging. If we modify the former example so that our expression
3397 becomes
3398 (b++, b) + a
3399 care must be taken not simply to add all three expressions into the final
3400 PNO_SP list. The function merge_tlist takes care of that by merging the
3401 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3402 way, so that no more than one access to B is recorded. */
3404 static void
3405 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3406 tree writer)
3408 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3409 enum tree_code code;
3410 enum tree_code_class cl;
3412 /* X may be NULL if it is the operand of an empty statement expression
3413 ({ }). */
3414 if (x == NULL)
3415 return;
3417 restart:
3418 code = TREE_CODE (x);
3419 cl = TREE_CODE_CLASS (code);
3421 if (warning_candidate_p (x))
3422 *pno_sp = new_tlist (*pno_sp, x, writer);
3424 switch (code)
3426 case CONSTRUCTOR:
3427 case SIZEOF_EXPR:
3428 return;
3430 case COMPOUND_EXPR:
3431 case TRUTH_ANDIF_EXPR:
3432 case TRUTH_ORIF_EXPR:
3433 tmp_before = tmp_nosp = tmp_list3 = 0;
3434 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3435 warn_for_collisions (tmp_nosp);
3436 merge_tlist (pbefore_sp, tmp_before, 0);
3437 merge_tlist (pbefore_sp, tmp_nosp, 0);
3438 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3439 merge_tlist (pbefore_sp, tmp_list3, 0);
3440 return;
3442 case COND_EXPR:
3443 tmp_before = tmp_list2 = 0;
3444 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3445 warn_for_collisions (tmp_list2);
3446 merge_tlist (pbefore_sp, tmp_before, 0);
3447 merge_tlist (pbefore_sp, tmp_list2, 0);
3449 tmp_list3 = tmp_nosp = 0;
3450 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3451 warn_for_collisions (tmp_nosp);
3452 merge_tlist (pbefore_sp, tmp_list3, 0);
3454 tmp_list3 = tmp_list2 = 0;
3455 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3456 warn_for_collisions (tmp_list2);
3457 merge_tlist (pbefore_sp, tmp_list3, 0);
3458 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3459 two first, to avoid warning for (a ? b++ : b++). */
3460 merge_tlist (&tmp_nosp, tmp_list2, 0);
3461 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3462 return;
3464 case PREDECREMENT_EXPR:
3465 case PREINCREMENT_EXPR:
3466 case POSTDECREMENT_EXPR:
3467 case POSTINCREMENT_EXPR:
3468 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3469 return;
3471 case MODIFY_EXPR:
3472 tmp_before = tmp_nosp = tmp_list3 = 0;
3473 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3474 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3475 /* Expressions inside the LHS are not ordered wrt. the sequence points
3476 in the RHS. Example:
3477 *a = (a++, 2)
3478 Despite the fact that the modification of "a" is in the before_sp
3479 list (tmp_before), it conflicts with the use of "a" in the LHS.
3480 We can handle this by adding the contents of tmp_list3
3481 to those of tmp_before, and redoing the collision warnings for that
3482 list. */
3483 add_tlist (&tmp_before, tmp_list3, x, 1);
3484 warn_for_collisions (tmp_before);
3485 /* Exclude the LHS itself here; we first have to merge it into the
3486 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3487 didn't exclude the LHS, we'd get it twice, once as a read and once
3488 as a write. */
3489 add_tlist (pno_sp, tmp_list3, x, 0);
3490 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3492 merge_tlist (pbefore_sp, tmp_before, 0);
3493 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3494 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3495 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3496 return;
3498 case CALL_EXPR:
3499 /* We need to warn about conflicts among arguments and conflicts between
3500 args and the function address. Side effects of the function address,
3501 however, are not ordered by the sequence point of the call. */
3503 call_expr_arg_iterator iter;
3504 tree arg;
3505 tmp_before = tmp_nosp = 0;
3506 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3507 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3509 tmp_list2 = tmp_list3 = 0;
3510 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3511 merge_tlist (&tmp_list3, tmp_list2, 0);
3512 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3514 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3515 warn_for_collisions (tmp_before);
3516 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3517 return;
3520 case TREE_LIST:
3521 /* Scan all the list, e.g. indices of multi dimensional array. */
3522 while (x)
3524 tmp_before = tmp_nosp = 0;
3525 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3526 merge_tlist (&tmp_nosp, tmp_before, 0);
3527 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3528 x = TREE_CHAIN (x);
3530 return;
3532 case SAVE_EXPR:
3534 struct tlist_cache *t;
3535 for (t = save_expr_cache; t; t = t->next)
3536 if (candidate_equal_p (t->expr, x))
3537 break;
3539 if (!t)
3541 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3542 t->next = save_expr_cache;
3543 t->expr = x;
3544 save_expr_cache = t;
3546 tmp_before = tmp_nosp = 0;
3547 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3548 warn_for_collisions (tmp_nosp);
3550 tmp_list3 = 0;
3551 merge_tlist (&tmp_list3, tmp_nosp, 0);
3552 t->cache_before_sp = tmp_before;
3553 t->cache_after_sp = tmp_list3;
3555 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3556 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3557 return;
3560 case ADDR_EXPR:
3561 x = TREE_OPERAND (x, 0);
3562 if (DECL_P (x))
3563 return;
3564 writer = 0;
3565 goto restart;
3567 default:
3568 /* For other expressions, simply recurse on their operands.
3569 Manual tail recursion for unary expressions.
3570 Other non-expressions need not be processed. */
3571 if (cl == tcc_unary)
3573 x = TREE_OPERAND (x, 0);
3574 writer = 0;
3575 goto restart;
3577 else if (IS_EXPR_CODE_CLASS (cl))
3579 int lp;
3580 int max = TREE_OPERAND_LENGTH (x);
3581 for (lp = 0; lp < max; lp++)
3583 tmp_before = tmp_nosp = 0;
3584 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3585 merge_tlist (&tmp_nosp, tmp_before, 0);
3586 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3589 return;
3593 /* Try to warn for undefined behavior in EXPR due to missing sequence
3594 points. */
3596 DEBUG_FUNCTION void
3597 verify_sequence_points (tree expr)
3599 struct tlist *before_sp = 0, *after_sp = 0;
3601 warned_ids = 0;
3602 save_expr_cache = 0;
3603 if (tlist_firstobj == 0)
3605 gcc_obstack_init (&tlist_obstack);
3606 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3609 verify_tree (expr, &before_sp, &after_sp, 0);
3610 warn_for_collisions (after_sp);
3611 obstack_free (&tlist_obstack, tlist_firstobj);
3614 /* Validate the expression after `case' and apply default promotions. */
3616 static tree
3617 check_case_value (location_t loc, tree value)
3619 if (value == NULL_TREE)
3620 return value;
3622 if (TREE_CODE (value) == INTEGER_CST)
3623 /* Promote char or short to int. */
3624 value = perform_integral_promotions (value);
3625 else if (value != error_mark_node)
3627 error_at (loc, "case label does not reduce to an integer constant");
3628 value = error_mark_node;
3631 constant_expression_warning (value);
3633 return value;
3636 /* See if the case values LOW and HIGH are in the range of the original
3637 type (i.e. before the default conversion to int) of the switch testing
3638 expression.
3639 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3640 the type before promoting it. CASE_LOW_P is a pointer to the lower
3641 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3642 if the case is not a case range.
3643 The caller has to make sure that we are not called with NULL for
3644 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3645 was a case value that doesn't fit into the range of the ORIG_TYPE.
3646 Returns true if the case label is in range of ORIG_TYPE (saturated or
3647 untouched) or false if the label is out of range. */
3649 static bool
3650 check_case_bounds (location_t loc, tree type, tree orig_type,
3651 tree *case_low_p, tree *case_high_p,
3652 bool *outside_range_p)
3654 tree min_value, max_value;
3655 tree case_low = *case_low_p;
3656 tree case_high = case_high_p ? *case_high_p : case_low;
3658 /* If there was a problem with the original type, do nothing. */
3659 if (orig_type == error_mark_node)
3660 return true;
3662 min_value = TYPE_MIN_VALUE (orig_type);
3663 max_value = TYPE_MAX_VALUE (orig_type);
3665 /* Case label is less than minimum for type. */
3666 if (tree_int_cst_compare (case_low, min_value) < 0
3667 && tree_int_cst_compare (case_high, min_value) < 0)
3669 warning_at (loc, 0, "case label value is less than minimum value "
3670 "for type");
3671 *outside_range_p = true;
3672 return false;
3675 /* Case value is greater than maximum for type. */
3676 if (tree_int_cst_compare (case_low, max_value) > 0
3677 && tree_int_cst_compare (case_high, max_value) > 0)
3679 warning_at (loc, 0, "case label value exceeds maximum value for type");
3680 *outside_range_p = true;
3681 return false;
3684 /* Saturate lower case label value to minimum. */
3685 if (tree_int_cst_compare (case_high, min_value) >= 0
3686 && tree_int_cst_compare (case_low, min_value) < 0)
3688 warning_at (loc, 0, "lower value in case label range"
3689 " less than minimum value for type");
3690 *outside_range_p = true;
3691 case_low = min_value;
3694 /* Saturate upper case label value to maximum. */
3695 if (tree_int_cst_compare (case_low, max_value) <= 0
3696 && tree_int_cst_compare (case_high, max_value) > 0)
3698 warning_at (loc, 0, "upper value in case label range"
3699 " exceeds maximum value for type");
3700 *outside_range_p = true;
3701 case_high = max_value;
3704 if (*case_low_p != case_low)
3705 *case_low_p = convert (type, case_low);
3706 if (case_high_p && *case_high_p != case_high)
3707 *case_high_p = convert (type, case_high);
3709 return true;
3712 /* Return an integer type with BITS bits of precision,
3713 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3715 tree
3716 c_common_type_for_size (unsigned int bits, int unsignedp)
3718 int i;
3720 if (bits == TYPE_PRECISION (integer_type_node))
3721 return unsignedp ? unsigned_type_node : integer_type_node;
3723 if (bits == TYPE_PRECISION (signed_char_type_node))
3724 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3726 if (bits == TYPE_PRECISION (short_integer_type_node))
3727 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3729 if (bits == TYPE_PRECISION (long_integer_type_node))
3730 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3732 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3733 return (unsignedp ? long_long_unsigned_type_node
3734 : long_long_integer_type_node);
3736 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3737 if (int_n_enabled_p[i]
3738 && bits == int_n_data[i].bitsize)
3739 return (unsignedp ? int_n_trees[i].unsigned_type
3740 : int_n_trees[i].signed_type);
3742 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3743 return (unsignedp ? widest_unsigned_literal_type_node
3744 : widest_integer_literal_type_node);
3746 if (bits <= TYPE_PRECISION (intQI_type_node))
3747 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3749 if (bits <= TYPE_PRECISION (intHI_type_node))
3750 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3752 if (bits <= TYPE_PRECISION (intSI_type_node))
3753 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3755 if (bits <= TYPE_PRECISION (intDI_type_node))
3756 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3758 return 0;
3761 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3762 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3763 and saturating if SATP is nonzero, otherwise not saturating. */
3765 tree
3766 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3767 int unsignedp, int satp)
3769 machine_mode mode;
3770 if (ibit == 0)
3771 mode = unsignedp ? UQQmode : QQmode;
3772 else
3773 mode = unsignedp ? UHAmode : HAmode;
3775 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3776 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3777 break;
3779 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3781 sorry ("GCC cannot support operators with integer types and "
3782 "fixed-point types that have too many integral and "
3783 "fractional bits together");
3784 return 0;
3787 return c_common_type_for_mode (mode, satp);
3790 /* Used for communication between c_common_type_for_mode and
3791 c_register_builtin_type. */
3792 tree registered_builtin_types;
3794 /* Return a data type that has machine mode MODE.
3795 If the mode is an integer,
3796 then UNSIGNEDP selects between signed and unsigned types.
3797 If the mode is a fixed-point mode,
3798 then UNSIGNEDP selects between saturating and nonsaturating types. */
3800 tree
3801 c_common_type_for_mode (machine_mode mode, int unsignedp)
3803 tree t;
3804 int i;
3806 if (mode == TYPE_MODE (integer_type_node))
3807 return unsignedp ? unsigned_type_node : integer_type_node;
3809 if (mode == TYPE_MODE (signed_char_type_node))
3810 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3812 if (mode == TYPE_MODE (short_integer_type_node))
3813 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3815 if (mode == TYPE_MODE (long_integer_type_node))
3816 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3818 if (mode == TYPE_MODE (long_long_integer_type_node))
3819 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3821 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3822 if (int_n_enabled_p[i]
3823 && mode == int_n_data[i].m)
3824 return (unsignedp ? int_n_trees[i].unsigned_type
3825 : int_n_trees[i].signed_type);
3827 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3828 return unsignedp ? widest_unsigned_literal_type_node
3829 : widest_integer_literal_type_node;
3831 if (mode == QImode)
3832 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3834 if (mode == HImode)
3835 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3837 if (mode == SImode)
3838 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3840 if (mode == DImode)
3841 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3843 #if HOST_BITS_PER_WIDE_INT >= 64
3844 if (mode == TYPE_MODE (intTI_type_node))
3845 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3846 #endif
3848 if (mode == TYPE_MODE (float_type_node))
3849 return float_type_node;
3851 if (mode == TYPE_MODE (double_type_node))
3852 return double_type_node;
3854 if (mode == TYPE_MODE (long_double_type_node))
3855 return long_double_type_node;
3857 if (mode == TYPE_MODE (void_type_node))
3858 return void_type_node;
3860 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3861 return (unsignedp
3862 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3863 : make_signed_type (GET_MODE_PRECISION (mode)));
3865 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3866 return (unsignedp
3867 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3868 : make_signed_type (GET_MODE_PRECISION (mode)));
3870 if (COMPLEX_MODE_P (mode))
3872 machine_mode inner_mode;
3873 tree inner_type;
3875 if (mode == TYPE_MODE (complex_float_type_node))
3876 return complex_float_type_node;
3877 if (mode == TYPE_MODE (complex_double_type_node))
3878 return complex_double_type_node;
3879 if (mode == TYPE_MODE (complex_long_double_type_node))
3880 return complex_long_double_type_node;
3882 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3883 return complex_integer_type_node;
3885 inner_mode = GET_MODE_INNER (mode);
3886 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3887 if (inner_type != NULL_TREE)
3888 return build_complex_type (inner_type);
3890 else if (VECTOR_MODE_P (mode))
3892 machine_mode inner_mode = GET_MODE_INNER (mode);
3893 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3894 if (inner_type != NULL_TREE)
3895 return build_vector_type_for_mode (inner_type, mode);
3898 if (mode == TYPE_MODE (dfloat32_type_node))
3899 return dfloat32_type_node;
3900 if (mode == TYPE_MODE (dfloat64_type_node))
3901 return dfloat64_type_node;
3902 if (mode == TYPE_MODE (dfloat128_type_node))
3903 return dfloat128_type_node;
3905 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3907 if (mode == TYPE_MODE (short_fract_type_node))
3908 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3909 if (mode == TYPE_MODE (fract_type_node))
3910 return unsignedp ? sat_fract_type_node : fract_type_node;
3911 if (mode == TYPE_MODE (long_fract_type_node))
3912 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3913 if (mode == TYPE_MODE (long_long_fract_type_node))
3914 return unsignedp ? sat_long_long_fract_type_node
3915 : long_long_fract_type_node;
3917 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3918 return unsignedp ? sat_unsigned_short_fract_type_node
3919 : unsigned_short_fract_type_node;
3920 if (mode == TYPE_MODE (unsigned_fract_type_node))
3921 return unsignedp ? sat_unsigned_fract_type_node
3922 : unsigned_fract_type_node;
3923 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3924 return unsignedp ? sat_unsigned_long_fract_type_node
3925 : unsigned_long_fract_type_node;
3926 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3927 return unsignedp ? sat_unsigned_long_long_fract_type_node
3928 : unsigned_long_long_fract_type_node;
3930 if (mode == TYPE_MODE (short_accum_type_node))
3931 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3932 if (mode == TYPE_MODE (accum_type_node))
3933 return unsignedp ? sat_accum_type_node : accum_type_node;
3934 if (mode == TYPE_MODE (long_accum_type_node))
3935 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3936 if (mode == TYPE_MODE (long_long_accum_type_node))
3937 return unsignedp ? sat_long_long_accum_type_node
3938 : long_long_accum_type_node;
3940 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3941 return unsignedp ? sat_unsigned_short_accum_type_node
3942 : unsigned_short_accum_type_node;
3943 if (mode == TYPE_MODE (unsigned_accum_type_node))
3944 return unsignedp ? sat_unsigned_accum_type_node
3945 : unsigned_accum_type_node;
3946 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3947 return unsignedp ? sat_unsigned_long_accum_type_node
3948 : unsigned_long_accum_type_node;
3949 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3950 return unsignedp ? sat_unsigned_long_long_accum_type_node
3951 : unsigned_long_long_accum_type_node;
3953 if (mode == QQmode)
3954 return unsignedp ? sat_qq_type_node : qq_type_node;
3955 if (mode == HQmode)
3956 return unsignedp ? sat_hq_type_node : hq_type_node;
3957 if (mode == SQmode)
3958 return unsignedp ? sat_sq_type_node : sq_type_node;
3959 if (mode == DQmode)
3960 return unsignedp ? sat_dq_type_node : dq_type_node;
3961 if (mode == TQmode)
3962 return unsignedp ? sat_tq_type_node : tq_type_node;
3964 if (mode == UQQmode)
3965 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3966 if (mode == UHQmode)
3967 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3968 if (mode == USQmode)
3969 return unsignedp ? sat_usq_type_node : usq_type_node;
3970 if (mode == UDQmode)
3971 return unsignedp ? sat_udq_type_node : udq_type_node;
3972 if (mode == UTQmode)
3973 return unsignedp ? sat_utq_type_node : utq_type_node;
3975 if (mode == HAmode)
3976 return unsignedp ? sat_ha_type_node : ha_type_node;
3977 if (mode == SAmode)
3978 return unsignedp ? sat_sa_type_node : sa_type_node;
3979 if (mode == DAmode)
3980 return unsignedp ? sat_da_type_node : da_type_node;
3981 if (mode == TAmode)
3982 return unsignedp ? sat_ta_type_node : ta_type_node;
3984 if (mode == UHAmode)
3985 return unsignedp ? sat_uha_type_node : uha_type_node;
3986 if (mode == USAmode)
3987 return unsignedp ? sat_usa_type_node : usa_type_node;
3988 if (mode == UDAmode)
3989 return unsignedp ? sat_uda_type_node : uda_type_node;
3990 if (mode == UTAmode)
3991 return unsignedp ? sat_uta_type_node : uta_type_node;
3994 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3995 if (TYPE_MODE (TREE_VALUE (t)) == mode
3996 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3997 return TREE_VALUE (t);
3999 return 0;
4002 tree
4003 c_common_unsigned_type (tree type)
4005 return c_common_signed_or_unsigned_type (1, type);
4008 /* Return a signed type the same as TYPE in other respects. */
4010 tree
4011 c_common_signed_type (tree type)
4013 return c_common_signed_or_unsigned_type (0, type);
4016 /* Return a type the same as TYPE except unsigned or
4017 signed according to UNSIGNEDP. */
4019 tree
4020 c_common_signed_or_unsigned_type (int unsignedp, tree type)
4022 tree type1;
4023 int i;
4025 /* This block of code emulates the behavior of the old
4026 c_common_unsigned_type. In particular, it returns
4027 long_unsigned_type_node if passed a long, even when a int would
4028 have the same size. This is necessary for warnings to work
4029 correctly in archs where sizeof(int) == sizeof(long) */
4031 type1 = TYPE_MAIN_VARIANT (type);
4032 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4033 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4034 if (type1 == integer_type_node || type1 == unsigned_type_node)
4035 return unsignedp ? unsigned_type_node : integer_type_node;
4036 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4037 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4038 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4039 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4040 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4041 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
4043 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4044 if (int_n_enabled_p[i]
4045 && (type1 == int_n_trees[i].unsigned_type
4046 || type1 == int_n_trees[i].signed_type))
4047 return (unsignedp ? int_n_trees[i].unsigned_type
4048 : int_n_trees[i].signed_type);
4050 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4051 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4052 #if HOST_BITS_PER_WIDE_INT >= 64
4053 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4054 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4055 #endif
4056 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4057 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4058 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4059 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4060 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4061 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4062 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4063 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4065 #define C_COMMON_FIXED_TYPES(NAME) \
4066 if (type1 == short_ ## NAME ## _type_node \
4067 || type1 == unsigned_short_ ## NAME ## _type_node) \
4068 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4069 : short_ ## NAME ## _type_node; \
4070 if (type1 == NAME ## _type_node \
4071 || type1 == unsigned_ ## NAME ## _type_node) \
4072 return unsignedp ? unsigned_ ## NAME ## _type_node \
4073 : NAME ## _type_node; \
4074 if (type1 == long_ ## NAME ## _type_node \
4075 || type1 == unsigned_long_ ## NAME ## _type_node) \
4076 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4077 : long_ ## NAME ## _type_node; \
4078 if (type1 == long_long_ ## NAME ## _type_node \
4079 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4080 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4081 : long_long_ ## NAME ## _type_node;
4083 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
4084 if (type1 == NAME ## _type_node \
4085 || type1 == u ## NAME ## _type_node) \
4086 return unsignedp ? u ## NAME ## _type_node \
4087 : NAME ## _type_node;
4089 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
4090 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4091 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4092 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4093 : sat_ ## short_ ## NAME ## _type_node; \
4094 if (type1 == sat_ ## NAME ## _type_node \
4095 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4096 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4097 : sat_ ## NAME ## _type_node; \
4098 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4099 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4100 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4101 : sat_ ## long_ ## NAME ## _type_node; \
4102 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4103 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4104 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4105 : sat_ ## long_long_ ## NAME ## _type_node;
4107 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4108 if (type1 == sat_ ## NAME ## _type_node \
4109 || type1 == sat_ ## u ## NAME ## _type_node) \
4110 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4111 : sat_ ## NAME ## _type_node;
4113 C_COMMON_FIXED_TYPES (fract);
4114 C_COMMON_FIXED_TYPES_SAT (fract);
4115 C_COMMON_FIXED_TYPES (accum);
4116 C_COMMON_FIXED_TYPES_SAT (accum);
4118 C_COMMON_FIXED_MODE_TYPES (qq);
4119 C_COMMON_FIXED_MODE_TYPES (hq);
4120 C_COMMON_FIXED_MODE_TYPES (sq);
4121 C_COMMON_FIXED_MODE_TYPES (dq);
4122 C_COMMON_FIXED_MODE_TYPES (tq);
4123 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4124 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4125 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4126 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4127 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4128 C_COMMON_FIXED_MODE_TYPES (ha);
4129 C_COMMON_FIXED_MODE_TYPES (sa);
4130 C_COMMON_FIXED_MODE_TYPES (da);
4131 C_COMMON_FIXED_MODE_TYPES (ta);
4132 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4133 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4134 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4135 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
4137 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4138 the precision; they have precision set to match their range, but
4139 may use a wider mode to match an ABI. If we change modes, we may
4140 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4141 the precision as well, so as to yield correct results for
4142 bit-field types. C++ does not have these separate bit-field
4143 types, and producing a signed or unsigned variant of an
4144 ENUMERAL_TYPE may cause other problems as well. */
4146 if (!INTEGRAL_TYPE_P (type)
4147 || TYPE_UNSIGNED (type) == unsignedp)
4148 return type;
4150 #define TYPE_OK(node) \
4151 (TYPE_MODE (type) == TYPE_MODE (node) \
4152 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4153 if (TYPE_OK (signed_char_type_node))
4154 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4155 if (TYPE_OK (integer_type_node))
4156 return unsignedp ? unsigned_type_node : integer_type_node;
4157 if (TYPE_OK (short_integer_type_node))
4158 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4159 if (TYPE_OK (long_integer_type_node))
4160 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4161 if (TYPE_OK (long_long_integer_type_node))
4162 return (unsignedp ? long_long_unsigned_type_node
4163 : long_long_integer_type_node);
4165 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4166 if (int_n_enabled_p[i]
4167 && TYPE_MODE (type) == int_n_data[i].m
4168 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4169 return (unsignedp ? int_n_trees[i].unsigned_type
4170 : int_n_trees[i].signed_type);
4172 if (TYPE_OK (widest_integer_literal_type_node))
4173 return (unsignedp ? widest_unsigned_literal_type_node
4174 : widest_integer_literal_type_node);
4176 #if HOST_BITS_PER_WIDE_INT >= 64
4177 if (TYPE_OK (intTI_type_node))
4178 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4179 #endif
4180 if (TYPE_OK (intDI_type_node))
4181 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4182 if (TYPE_OK (intSI_type_node))
4183 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4184 if (TYPE_OK (intHI_type_node))
4185 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4186 if (TYPE_OK (intQI_type_node))
4187 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4188 #undef TYPE_OK
4190 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4193 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4195 tree
4196 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4198 int i;
4200 /* Extended integer types of the same width as a standard type have
4201 lesser rank, so those of the same width as int promote to int or
4202 unsigned int and are valid for printf formats expecting int or
4203 unsigned int. To avoid such special cases, avoid creating
4204 extended integer types for bit-fields if a standard integer type
4205 is available. */
4206 if (width == TYPE_PRECISION (integer_type_node))
4207 return unsignedp ? unsigned_type_node : integer_type_node;
4208 if (width == TYPE_PRECISION (signed_char_type_node))
4209 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4210 if (width == TYPE_PRECISION (short_integer_type_node))
4211 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4212 if (width == TYPE_PRECISION (long_integer_type_node))
4213 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4214 if (width == TYPE_PRECISION (long_long_integer_type_node))
4215 return (unsignedp ? long_long_unsigned_type_node
4216 : long_long_integer_type_node);
4217 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4218 if (int_n_enabled_p[i]
4219 && width == int_n_data[i].bitsize)
4220 return (unsignedp ? int_n_trees[i].unsigned_type
4221 : int_n_trees[i].signed_type);
4222 return build_nonstandard_integer_type (width, unsignedp);
4225 /* The C version of the register_builtin_type langhook. */
4227 void
4228 c_register_builtin_type (tree type, const char* name)
4230 tree decl;
4232 decl = build_decl (UNKNOWN_LOCATION,
4233 TYPE_DECL, get_identifier (name), type);
4234 DECL_ARTIFICIAL (decl) = 1;
4235 if (!TYPE_NAME (type))
4236 TYPE_NAME (type) = decl;
4237 pushdecl (decl);
4239 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4242 /* Print an error message for invalid operands to arith operation
4243 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4244 LOCATION is the location of the message. */
4246 void
4247 binary_op_error (location_t location, enum tree_code code,
4248 tree type0, tree type1)
4250 const char *opname;
4252 switch (code)
4254 case PLUS_EXPR:
4255 opname = "+"; break;
4256 case MINUS_EXPR:
4257 opname = "-"; break;
4258 case MULT_EXPR:
4259 opname = "*"; break;
4260 case MAX_EXPR:
4261 opname = "max"; break;
4262 case MIN_EXPR:
4263 opname = "min"; break;
4264 case EQ_EXPR:
4265 opname = "=="; break;
4266 case NE_EXPR:
4267 opname = "!="; break;
4268 case LE_EXPR:
4269 opname = "<="; break;
4270 case GE_EXPR:
4271 opname = ">="; break;
4272 case LT_EXPR:
4273 opname = "<"; break;
4274 case GT_EXPR:
4275 opname = ">"; break;
4276 case LSHIFT_EXPR:
4277 opname = "<<"; break;
4278 case RSHIFT_EXPR:
4279 opname = ">>"; break;
4280 case TRUNC_MOD_EXPR:
4281 case FLOOR_MOD_EXPR:
4282 opname = "%"; break;
4283 case TRUNC_DIV_EXPR:
4284 case FLOOR_DIV_EXPR:
4285 opname = "/"; break;
4286 case BIT_AND_EXPR:
4287 opname = "&"; break;
4288 case BIT_IOR_EXPR:
4289 opname = "|"; break;
4290 case TRUTH_ANDIF_EXPR:
4291 opname = "&&"; break;
4292 case TRUTH_ORIF_EXPR:
4293 opname = "||"; break;
4294 case BIT_XOR_EXPR:
4295 opname = "^"; break;
4296 default:
4297 gcc_unreachable ();
4299 error_at (location,
4300 "invalid operands to binary %s (have %qT and %qT)", opname,
4301 type0, type1);
4304 /* Given an expression as a tree, return its original type. Do this
4305 by stripping any conversion that preserves the sign and precision. */
4306 static tree
4307 expr_original_type (tree expr)
4309 STRIP_SIGN_NOPS (expr);
4310 return TREE_TYPE (expr);
4313 /* Subroutine of build_binary_op, used for comparison operations.
4314 See if the operands have both been converted from subword integer types
4315 and, if so, perhaps change them both back to their original type.
4316 This function is also responsible for converting the two operands
4317 to the proper common type for comparison.
4319 The arguments of this function are all pointers to local variables
4320 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4321 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4323 LOC is the location of the comparison.
4325 If this function returns nonzero, it means that the comparison has
4326 a constant value. What this function returns is an expression for
4327 that value. */
4329 tree
4330 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4331 tree *restype_ptr, enum tree_code *rescode_ptr)
4333 tree type;
4334 tree op0 = *op0_ptr;
4335 tree op1 = *op1_ptr;
4336 int unsignedp0, unsignedp1;
4337 int real1, real2;
4338 tree primop0, primop1;
4339 enum tree_code code = *rescode_ptr;
4341 /* Throw away any conversions to wider types
4342 already present in the operands. */
4344 primop0 = c_common_get_narrower (op0, &unsignedp0);
4345 primop1 = c_common_get_narrower (op1, &unsignedp1);
4347 /* If primopN is first sign-extended from primopN's precision to opN's
4348 precision, then zero-extended from opN's precision to
4349 *restype_ptr precision, shortenings might be invalid. */
4350 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4351 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4352 && !unsignedp0
4353 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4354 primop0 = op0;
4355 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4356 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4357 && !unsignedp1
4358 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4359 primop1 = op1;
4361 /* Handle the case that OP0 does not *contain* a conversion
4362 but it *requires* conversion to FINAL_TYPE. */
4364 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4365 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4366 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4367 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4369 /* If one of the operands must be floated, we cannot optimize. */
4370 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4371 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4373 /* If first arg is constant, swap the args (changing operation
4374 so value is preserved), for canonicalization. Don't do this if
4375 the second arg is 0. */
4377 if (TREE_CONSTANT (primop0)
4378 && !integer_zerop (primop1) && !real_zerop (primop1)
4379 && !fixed_zerop (primop1))
4381 std::swap (primop0, primop1);
4382 std::swap (op0, op1);
4383 *op0_ptr = op0;
4384 *op1_ptr = op1;
4385 std::swap (unsignedp0, unsignedp1);
4386 std::swap (real1, real2);
4388 switch (code)
4390 case LT_EXPR:
4391 code = GT_EXPR;
4392 break;
4393 case GT_EXPR:
4394 code = LT_EXPR;
4395 break;
4396 case LE_EXPR:
4397 code = GE_EXPR;
4398 break;
4399 case GE_EXPR:
4400 code = LE_EXPR;
4401 break;
4402 default:
4403 break;
4405 *rescode_ptr = code;
4408 /* If comparing an integer against a constant more bits wide,
4409 maybe we can deduce a value of 1 or 0 independent of the data.
4410 Or else truncate the constant now
4411 rather than extend the variable at run time.
4413 This is only interesting if the constant is the wider arg.
4414 Also, it is not safe if the constant is unsigned and the
4415 variable arg is signed, since in this case the variable
4416 would be sign-extended and then regarded as unsigned.
4417 Our technique fails in this case because the lowest/highest
4418 possible unsigned results don't follow naturally from the
4419 lowest/highest possible values of the variable operand.
4420 For just EQ_EXPR and NE_EXPR there is another technique that
4421 could be used: see if the constant can be faithfully represented
4422 in the other operand's type, by truncating it and reextending it
4423 and see if that preserves the constant's value. */
4425 if (!real1 && !real2
4426 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4427 && TREE_CODE (primop1) == INTEGER_CST
4428 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4430 int min_gt, max_gt, min_lt, max_lt;
4431 tree maxval, minval;
4432 /* 1 if comparison is nominally unsigned. */
4433 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4434 tree val;
4436 type = c_common_signed_or_unsigned_type (unsignedp0,
4437 TREE_TYPE (primop0));
4439 maxval = TYPE_MAX_VALUE (type);
4440 minval = TYPE_MIN_VALUE (type);
4442 if (unsignedp && !unsignedp0)
4443 *restype_ptr = c_common_signed_type (*restype_ptr);
4445 if (TREE_TYPE (primop1) != *restype_ptr)
4447 /* Convert primop1 to target type, but do not introduce
4448 additional overflow. We know primop1 is an int_cst. */
4449 primop1 = force_fit_type (*restype_ptr,
4450 wide_int::from
4451 (primop1,
4452 TYPE_PRECISION (*restype_ptr),
4453 TYPE_SIGN (TREE_TYPE (primop1))),
4454 0, TREE_OVERFLOW (primop1));
4456 if (type != *restype_ptr)
4458 minval = convert (*restype_ptr, minval);
4459 maxval = convert (*restype_ptr, maxval);
4462 min_gt = tree_int_cst_lt (primop1, minval);
4463 max_gt = tree_int_cst_lt (primop1, maxval);
4464 min_lt = tree_int_cst_lt (minval, primop1);
4465 max_lt = tree_int_cst_lt (maxval, primop1);
4467 val = 0;
4468 /* This used to be a switch, but Genix compiler can't handle that. */
4469 if (code == NE_EXPR)
4471 if (max_lt || min_gt)
4472 val = truthvalue_true_node;
4474 else if (code == EQ_EXPR)
4476 if (max_lt || min_gt)
4477 val = truthvalue_false_node;
4479 else if (code == LT_EXPR)
4481 if (max_lt)
4482 val = truthvalue_true_node;
4483 if (!min_lt)
4484 val = truthvalue_false_node;
4486 else if (code == GT_EXPR)
4488 if (min_gt)
4489 val = truthvalue_true_node;
4490 if (!max_gt)
4491 val = truthvalue_false_node;
4493 else if (code == LE_EXPR)
4495 if (!max_gt)
4496 val = truthvalue_true_node;
4497 if (min_gt)
4498 val = truthvalue_false_node;
4500 else if (code == GE_EXPR)
4502 if (!min_lt)
4503 val = truthvalue_true_node;
4504 if (max_lt)
4505 val = truthvalue_false_node;
4508 /* If primop0 was sign-extended and unsigned comparison specd,
4509 we did a signed comparison above using the signed type bounds.
4510 But the comparison we output must be unsigned.
4512 Also, for inequalities, VAL is no good; but if the signed
4513 comparison had *any* fixed result, it follows that the
4514 unsigned comparison just tests the sign in reverse
4515 (positive values are LE, negative ones GE).
4516 So we can generate an unsigned comparison
4517 against an extreme value of the signed type. */
4519 if (unsignedp && !unsignedp0)
4521 if (val != 0)
4522 switch (code)
4524 case LT_EXPR:
4525 case GE_EXPR:
4526 primop1 = TYPE_MIN_VALUE (type);
4527 val = 0;
4528 break;
4530 case LE_EXPR:
4531 case GT_EXPR:
4532 primop1 = TYPE_MAX_VALUE (type);
4533 val = 0;
4534 break;
4536 default:
4537 break;
4539 type = c_common_unsigned_type (type);
4542 if (TREE_CODE (primop0) != INTEGER_CST)
4544 if (val == truthvalue_false_node)
4545 warning_at (loc, OPT_Wtype_limits,
4546 "comparison is always false due to limited range of data type");
4547 if (val == truthvalue_true_node)
4548 warning_at (loc, OPT_Wtype_limits,
4549 "comparison is always true due to limited range of data type");
4552 if (val != 0)
4554 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4555 if (TREE_SIDE_EFFECTS (primop0))
4556 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4557 return val;
4560 /* Value is not predetermined, but do the comparison
4561 in the type of the operand that is not constant.
4562 TYPE is already properly set. */
4565 /* If either arg is decimal float and the other is float, find the
4566 proper common type to use for comparison. */
4567 else if (real1 && real2
4568 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4569 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4570 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4572 /* If either arg is decimal float and the other is float, fail. */
4573 else if (real1 && real2
4574 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4575 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4576 return 0;
4578 else if (real1 && real2
4579 && (TYPE_PRECISION (TREE_TYPE (primop0))
4580 == TYPE_PRECISION (TREE_TYPE (primop1))))
4581 type = TREE_TYPE (primop0);
4583 /* If args' natural types are both narrower than nominal type
4584 and both extend in the same manner, compare them
4585 in the type of the wider arg.
4586 Otherwise must actually extend both to the nominal
4587 common type lest different ways of extending
4588 alter the result.
4589 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4591 else if (unsignedp0 == unsignedp1 && real1 == real2
4592 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4593 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4595 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4596 type = c_common_signed_or_unsigned_type (unsignedp0
4597 || TYPE_UNSIGNED (*restype_ptr),
4598 type);
4599 /* Make sure shorter operand is extended the right way
4600 to match the longer operand. */
4601 primop0
4602 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4603 TREE_TYPE (primop0)),
4604 primop0);
4605 primop1
4606 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4607 TREE_TYPE (primop1)),
4608 primop1);
4610 else
4612 /* Here we must do the comparison on the nominal type
4613 using the args exactly as we received them. */
4614 type = *restype_ptr;
4615 primop0 = op0;
4616 primop1 = op1;
4618 if (!real1 && !real2 && integer_zerop (primop1)
4619 && TYPE_UNSIGNED (*restype_ptr))
4621 tree value = 0;
4622 /* All unsigned values are >= 0, so we warn. However,
4623 if OP0 is a constant that is >= 0, the signedness of
4624 the comparison isn't an issue, so suppress the
4625 warning. */
4626 bool warn =
4627 warn_type_limits && !in_system_header_at (loc)
4628 && !(TREE_CODE (primop0) == INTEGER_CST
4629 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4630 primop0)))
4631 /* Do not warn for enumeration types. */
4632 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4634 switch (code)
4636 case GE_EXPR:
4637 if (warn)
4638 warning_at (loc, OPT_Wtype_limits,
4639 "comparison of unsigned expression >= 0 is always true");
4640 value = truthvalue_true_node;
4641 break;
4643 case LT_EXPR:
4644 if (warn)
4645 warning_at (loc, OPT_Wtype_limits,
4646 "comparison of unsigned expression < 0 is always false");
4647 value = truthvalue_false_node;
4648 break;
4650 default:
4651 break;
4654 if (value != 0)
4656 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4657 if (TREE_SIDE_EFFECTS (primop0))
4658 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4659 primop0, value);
4660 return value;
4665 *op0_ptr = convert (type, primop0);
4666 *op1_ptr = convert (type, primop1);
4668 *restype_ptr = truthvalue_type_node;
4670 return 0;
4673 /* Return a tree for the sum or difference (RESULTCODE says which)
4674 of pointer PTROP and integer INTOP. */
4676 tree
4677 pointer_int_sum (location_t loc, enum tree_code resultcode,
4678 tree ptrop, tree intop, bool complain)
4680 tree size_exp, ret;
4682 /* The result is a pointer of the same type that is being added. */
4683 tree result_type = TREE_TYPE (ptrop);
4685 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4687 if (complain && warn_pointer_arith)
4688 pedwarn (loc, OPT_Wpointer_arith,
4689 "pointer of type %<void *%> used in arithmetic");
4690 else if (!complain)
4691 return error_mark_node;
4692 size_exp = integer_one_node;
4694 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4696 if (complain && warn_pointer_arith)
4697 pedwarn (loc, OPT_Wpointer_arith,
4698 "pointer to a function used in arithmetic");
4699 else if (!complain)
4700 return error_mark_node;
4701 size_exp = integer_one_node;
4703 else
4704 size_exp = size_in_bytes (TREE_TYPE (result_type));
4706 /* We are manipulating pointer values, so we don't need to warn
4707 about relying on undefined signed overflow. We disable the
4708 warning here because we use integer types so fold won't know that
4709 they are really pointers. */
4710 fold_defer_overflow_warnings ();
4712 /* If what we are about to multiply by the size of the elements
4713 contains a constant term, apply distributive law
4714 and multiply that constant term separately.
4715 This helps produce common subexpressions. */
4716 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4717 && !TREE_CONSTANT (intop)
4718 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4719 && TREE_CONSTANT (size_exp)
4720 /* If the constant comes from pointer subtraction,
4721 skip this optimization--it would cause an error. */
4722 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4723 /* If the constant is unsigned, and smaller than the pointer size,
4724 then we must skip this optimization. This is because it could cause
4725 an overflow error if the constant is negative but INTOP is not. */
4726 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4727 || (TYPE_PRECISION (TREE_TYPE (intop))
4728 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4730 enum tree_code subcode = resultcode;
4731 tree int_type = TREE_TYPE (intop);
4732 if (TREE_CODE (intop) == MINUS_EXPR)
4733 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4734 /* Convert both subexpression types to the type of intop,
4735 because weird cases involving pointer arithmetic
4736 can result in a sum or difference with different type args. */
4737 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4738 subcode, ptrop,
4739 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4740 intop = convert (int_type, TREE_OPERAND (intop, 0));
4743 /* Convert the integer argument to a type the same size as sizetype
4744 so the multiply won't overflow spuriously. */
4745 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4746 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4747 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4748 TYPE_UNSIGNED (sizetype)), intop);
4750 /* Replace the integer argument with a suitable product by the object size.
4751 Do this multiplication as signed, then convert to the appropriate type
4752 for the pointer operation and disregard an overflow that occurred only
4753 because of the sign-extension change in the latter conversion. */
4755 tree t = build_binary_op (loc,
4756 MULT_EXPR, intop,
4757 convert (TREE_TYPE (intop), size_exp), 1);
4758 intop = convert (sizetype, t);
4759 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4760 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4763 /* Create the sum or difference. */
4764 if (resultcode == MINUS_EXPR)
4765 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4767 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4769 fold_undefer_and_ignore_overflow_warnings ();
4771 return ret;
4774 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4775 and if NON_CONST is known not to be permitted in an evaluated part
4776 of a constant expression. */
4778 tree
4779 c_wrap_maybe_const (tree expr, bool non_const)
4781 bool nowarning = TREE_NO_WARNING (expr);
4782 location_t loc = EXPR_LOCATION (expr);
4784 /* This should never be called for C++. */
4785 if (c_dialect_cxx ())
4786 gcc_unreachable ();
4788 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4789 STRIP_TYPE_NOPS (expr);
4790 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4791 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4792 if (nowarning)
4793 TREE_NO_WARNING (expr) = 1;
4794 protected_set_expr_location (expr, loc);
4796 return expr;
4799 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4800 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4801 around the SAVE_EXPR if needed so that c_fully_fold does not need
4802 to look inside SAVE_EXPRs. */
4804 tree
4805 c_save_expr (tree expr)
4807 bool maybe_const = true;
4808 if (c_dialect_cxx ())
4809 return save_expr (expr);
4810 expr = c_fully_fold (expr, false, &maybe_const);
4811 expr = save_expr (expr);
4812 if (!maybe_const)
4813 expr = c_wrap_maybe_const (expr, true);
4814 return expr;
4817 /* Return whether EXPR is a declaration whose address can never be
4818 NULL. */
4820 bool
4821 decl_with_nonnull_addr_p (const_tree expr)
4823 return (DECL_P (expr)
4824 && (TREE_CODE (expr) == PARM_DECL
4825 || TREE_CODE (expr) == LABEL_DECL
4826 || !DECL_WEAK (expr)));
4829 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4830 or for an `if' or `while' statement or ?..: exp. It should already
4831 have been validated to be of suitable type; otherwise, a bad
4832 diagnostic may result.
4834 The EXPR is located at LOCATION.
4836 This preparation consists of taking the ordinary
4837 representation of an expression expr and producing a valid tree
4838 boolean expression describing whether expr is nonzero. We could
4839 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4840 but we optimize comparisons, &&, ||, and !.
4842 The resulting type should always be `truthvalue_type_node'. */
4844 tree
4845 c_common_truthvalue_conversion (location_t location, tree expr)
4847 switch (TREE_CODE (expr))
4849 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4850 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4851 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4852 case ORDERED_EXPR: case UNORDERED_EXPR:
4853 if (TREE_TYPE (expr) == truthvalue_type_node)
4854 return expr;
4855 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4856 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4857 goto ret;
4859 case TRUTH_ANDIF_EXPR:
4860 case TRUTH_ORIF_EXPR:
4861 case TRUTH_AND_EXPR:
4862 case TRUTH_OR_EXPR:
4863 case TRUTH_XOR_EXPR:
4864 if (TREE_TYPE (expr) == truthvalue_type_node)
4865 return expr;
4866 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4867 c_common_truthvalue_conversion (location,
4868 TREE_OPERAND (expr, 0)),
4869 c_common_truthvalue_conversion (location,
4870 TREE_OPERAND (expr, 1)));
4871 goto ret;
4873 case TRUTH_NOT_EXPR:
4874 if (TREE_TYPE (expr) == truthvalue_type_node)
4875 return expr;
4876 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4877 c_common_truthvalue_conversion (location,
4878 TREE_OPERAND (expr, 0)));
4879 goto ret;
4881 case ERROR_MARK:
4882 return expr;
4884 case INTEGER_CST:
4885 return integer_zerop (expr) ? truthvalue_false_node
4886 : truthvalue_true_node;
4888 case REAL_CST:
4889 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4890 ? truthvalue_true_node
4891 : truthvalue_false_node;
4893 case FIXED_CST:
4894 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4895 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4896 ? truthvalue_true_node
4897 : truthvalue_false_node;
4899 case FUNCTION_DECL:
4900 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4901 /* Fall through. */
4903 case ADDR_EXPR:
4905 tree inner = TREE_OPERAND (expr, 0);
4906 if (decl_with_nonnull_addr_p (inner))
4908 /* Common Ada/Pascal programmer's mistake. */
4909 warning_at (location,
4910 OPT_Waddress,
4911 "the address of %qD will always evaluate as %<true%>",
4912 inner);
4913 return truthvalue_true_node;
4915 break;
4918 case COMPLEX_EXPR:
4919 expr = build_binary_op (EXPR_LOCATION (expr),
4920 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4921 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4922 c_common_truthvalue_conversion (location,
4923 TREE_OPERAND (expr, 0)),
4924 c_common_truthvalue_conversion (location,
4925 TREE_OPERAND (expr, 1)),
4927 goto ret;
4929 case NEGATE_EXPR:
4930 case ABS_EXPR:
4931 case FLOAT_EXPR:
4932 case EXCESS_PRECISION_EXPR:
4933 /* These don't change whether an object is nonzero or zero. */
4934 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4936 case LROTATE_EXPR:
4937 case RROTATE_EXPR:
4938 /* These don't change whether an object is zero or nonzero, but
4939 we can't ignore them if their second arg has side-effects. */
4940 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4942 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4943 TREE_OPERAND (expr, 1),
4944 c_common_truthvalue_conversion
4945 (location, TREE_OPERAND (expr, 0)));
4946 goto ret;
4948 else
4949 return c_common_truthvalue_conversion (location,
4950 TREE_OPERAND (expr, 0));
4952 case COND_EXPR:
4953 /* Distribute the conversion into the arms of a COND_EXPR. */
4954 if (c_dialect_cxx ())
4956 tree op1 = TREE_OPERAND (expr, 1);
4957 tree op2 = TREE_OPERAND (expr, 2);
4958 /* In C++ one of the arms might have void type if it is throw. */
4959 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4960 op1 = c_common_truthvalue_conversion (location, op1);
4961 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4962 op2 = c_common_truthvalue_conversion (location, op2);
4963 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4964 TREE_OPERAND (expr, 0), op1, op2);
4965 goto ret;
4967 else
4969 /* Folding will happen later for C. */
4970 expr = build3 (COND_EXPR, truthvalue_type_node,
4971 TREE_OPERAND (expr, 0),
4972 c_common_truthvalue_conversion (location,
4973 TREE_OPERAND (expr, 1)),
4974 c_common_truthvalue_conversion (location,
4975 TREE_OPERAND (expr, 2)));
4976 goto ret;
4979 CASE_CONVERT:
4981 tree totype = TREE_TYPE (expr);
4982 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4984 if (POINTER_TYPE_P (totype)
4985 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4987 tree inner = expr;
4988 STRIP_NOPS (inner);
4990 if (DECL_P (inner))
4991 warning_at (location,
4992 OPT_Waddress,
4993 "the compiler can assume that the address of "
4994 "%qD will always evaluate to %<true%>",
4995 inner);
4998 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4999 since that affects how `default_conversion' will behave. */
5000 if (TREE_CODE (totype) == REFERENCE_TYPE
5001 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5002 break;
5003 /* Don't strip a conversion from C++0x scoped enum, since they
5004 don't implicitly convert to other types. */
5005 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5006 && ENUM_IS_SCOPED (fromtype))
5007 break;
5008 /* If this isn't narrowing the argument, we can ignore it. */
5009 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5010 return c_common_truthvalue_conversion (location,
5011 TREE_OPERAND (expr, 0));
5013 break;
5015 case MODIFY_EXPR:
5016 if (!TREE_NO_WARNING (expr)
5017 && warn_parentheses)
5019 warning (OPT_Wparentheses,
5020 "suggest parentheses around assignment used as truth value");
5021 TREE_NO_WARNING (expr) = 1;
5023 break;
5025 default:
5026 break;
5029 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
5031 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
5032 expr = (build_binary_op
5033 (EXPR_LOCATION (expr),
5034 (TREE_SIDE_EFFECTS (expr)
5035 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
5036 c_common_truthvalue_conversion
5037 (location,
5038 build_unary_op (location, REALPART_EXPR, t, 0)),
5039 c_common_truthvalue_conversion
5040 (location,
5041 build_unary_op (location, IMAGPART_EXPR, t, 0)),
5042 0));
5043 goto ret;
5046 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5048 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5049 FCONST0 (TYPE_MODE
5050 (TREE_TYPE (expr))));
5051 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
5053 else
5054 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
5056 ret:
5057 protected_set_expr_location (expr, location);
5058 return expr;
5061 static void def_builtin_1 (enum built_in_function fncode,
5062 const char *name,
5063 enum built_in_class fnclass,
5064 tree fntype, tree libtype,
5065 bool both_p, bool fallback_p, bool nonansi_p,
5066 tree fnattrs, bool implicit_p);
5069 /* Apply the TYPE_QUALS to the new DECL. */
5071 void
5072 c_apply_type_quals_to_decl (int type_quals, tree decl)
5074 tree type = TREE_TYPE (decl);
5076 if (type == error_mark_node)
5077 return;
5079 if ((type_quals & TYPE_QUAL_CONST)
5080 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5081 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5082 constructor can produce constant init, so rely on cp_finish_decl to
5083 clear TREE_READONLY if the variable has non-constant init. */
5084 TREE_READONLY (decl) = 1;
5085 if (type_quals & TYPE_QUAL_VOLATILE)
5087 TREE_SIDE_EFFECTS (decl) = 1;
5088 TREE_THIS_VOLATILE (decl) = 1;
5090 if (type_quals & TYPE_QUAL_RESTRICT)
5092 while (type && TREE_CODE (type) == ARRAY_TYPE)
5093 /* Allow 'restrict' on arrays of pointers.
5094 FIXME currently we just ignore it. */
5095 type = TREE_TYPE (type);
5096 if (!type
5097 || !POINTER_TYPE_P (type)
5098 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
5099 error ("invalid use of %<restrict%>");
5103 struct c_type_hasher : ggc_ptr_hash<tree_node>
5105 static hashval_t hash (tree);
5106 static bool equal (tree, tree);
5109 /* Hash function for the problem of multiple type definitions in
5110 different files. This must hash all types that will compare
5111 equal via comptypes to the same value. In practice it hashes
5112 on some of the simple stuff and leaves the details to comptypes. */
5114 hashval_t
5115 c_type_hasher::hash (tree t)
5117 int n_elements;
5118 int shift, size;
5119 tree t2;
5120 switch (TREE_CODE (t))
5122 /* For pointers, hash on pointee type plus some swizzling. */
5123 case POINTER_TYPE:
5124 return hash (TREE_TYPE (t)) ^ 0x3003003;
5125 /* Hash on number of elements and total size. */
5126 case ENUMERAL_TYPE:
5127 shift = 3;
5128 t2 = TYPE_VALUES (t);
5129 break;
5130 case RECORD_TYPE:
5131 shift = 0;
5132 t2 = TYPE_FIELDS (t);
5133 break;
5134 case QUAL_UNION_TYPE:
5135 shift = 1;
5136 t2 = TYPE_FIELDS (t);
5137 break;
5138 case UNION_TYPE:
5139 shift = 2;
5140 t2 = TYPE_FIELDS (t);
5141 break;
5142 default:
5143 gcc_unreachable ();
5145 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5146 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5147 n_elements = list_length (t2);
5148 /* We might have a VLA here. */
5149 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5150 size = 0;
5151 else
5152 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
5153 return ((size << 24) | (n_elements << shift));
5156 bool
5157 c_type_hasher::equal (tree t1, tree t2)
5159 return lang_hooks.types_compatible_p (t1, t2);
5162 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
5164 /* Return the typed-based alias set for T, which may be an expression
5165 or a type. Return -1 if we don't do anything special. */
5167 alias_set_type
5168 c_common_get_alias_set (tree t)
5170 tree u;
5172 /* For VLAs, use the alias set of the element type rather than the
5173 default of alias set 0 for types compared structurally. */
5174 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5176 if (TREE_CODE (t) == ARRAY_TYPE)
5177 return get_alias_set (TREE_TYPE (t));
5178 return -1;
5181 /* Permit type-punning when accessing a union, provided the access
5182 is directly through the union. For example, this code does not
5183 permit taking the address of a union member and then storing
5184 through it. Even the type-punning allowed here is a GCC
5185 extension, albeit a common and useful one; the C standard says
5186 that such accesses have implementation-defined behavior. */
5187 for (u = t;
5188 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5189 u = TREE_OPERAND (u, 0))
5190 if (TREE_CODE (u) == COMPONENT_REF
5191 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5192 return 0;
5194 /* That's all the expressions we handle specially. */
5195 if (!TYPE_P (t))
5196 return -1;
5198 /* The C standard guarantees that any object may be accessed via an
5199 lvalue that has character type. */
5200 if (t == char_type_node
5201 || t == signed_char_type_node
5202 || t == unsigned_char_type_node)
5203 return 0;
5205 /* The C standard specifically allows aliasing between signed and
5206 unsigned variants of the same type. We treat the signed
5207 variant as canonical. */
5208 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5210 tree t1 = c_common_signed_type (t);
5212 /* t1 == t can happen for boolean nodes which are always unsigned. */
5213 if (t1 != t)
5214 return get_alias_set (t1);
5217 /* Handle the case of multiple type nodes referring to "the same" type,
5218 which occurs with IMA. These share an alias set. FIXME: Currently only
5219 C90 is handled. (In C99 type compatibility is not transitive, which
5220 complicates things mightily. The alias set splay trees can theoretically
5221 represent this, but insertion is tricky when you consider all the
5222 different orders things might arrive in.) */
5224 if (c_language != clk_c || flag_isoc99)
5225 return -1;
5227 /* Save time if there's only one input file. */
5228 if (num_in_fnames == 1)
5229 return -1;
5231 /* Pointers need special handling if they point to any type that
5232 needs special handling (below). */
5233 if (TREE_CODE (t) == POINTER_TYPE)
5235 tree t2;
5236 /* Find bottom type under any nested POINTERs. */
5237 for (t2 = TREE_TYPE (t);
5238 TREE_CODE (t2) == POINTER_TYPE;
5239 t2 = TREE_TYPE (t2))
5241 if (TREE_CODE (t2) != RECORD_TYPE
5242 && TREE_CODE (t2) != ENUMERAL_TYPE
5243 && TREE_CODE (t2) != QUAL_UNION_TYPE
5244 && TREE_CODE (t2) != UNION_TYPE)
5245 return -1;
5246 if (TYPE_SIZE (t2) == 0)
5247 return -1;
5249 /* These are the only cases that need special handling. */
5250 if (TREE_CODE (t) != RECORD_TYPE
5251 && TREE_CODE (t) != ENUMERAL_TYPE
5252 && TREE_CODE (t) != QUAL_UNION_TYPE
5253 && TREE_CODE (t) != UNION_TYPE
5254 && TREE_CODE (t) != POINTER_TYPE)
5255 return -1;
5256 /* Undefined? */
5257 if (TYPE_SIZE (t) == 0)
5258 return -1;
5260 /* Look up t in hash table. Only one of the compatible types within each
5261 alias set is recorded in the table. */
5262 if (!type_hash_table)
5263 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5264 tree *slot = type_hash_table->find_slot (t, INSERT);
5265 if (*slot != NULL)
5267 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5268 return TYPE_ALIAS_SET ((tree)*slot);
5270 else
5271 /* Our caller will assign and record (in t) a new alias set; all we need
5272 to do is remember t in the hash table. */
5273 *slot = t;
5275 return -1;
5278 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5279 the IS_SIZEOF parameter indicates which operator is being applied.
5280 The COMPLAIN flag controls whether we should diagnose possibly
5281 ill-formed constructs or not. LOC is the location of the SIZEOF or
5282 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5283 a type in any context should be returned, rather than the normal
5284 alignment for that type. */
5286 tree
5287 c_sizeof_or_alignof_type (location_t loc,
5288 tree type, bool is_sizeof, bool min_alignof,
5289 int complain)
5291 const char *op_name;
5292 tree value = NULL;
5293 enum tree_code type_code = TREE_CODE (type);
5295 op_name = is_sizeof ? "sizeof" : "__alignof__";
5297 if (type_code == FUNCTION_TYPE)
5299 if (is_sizeof)
5301 if (complain && warn_pointer_arith)
5302 pedwarn (loc, OPT_Wpointer_arith,
5303 "invalid application of %<sizeof%> to a function type");
5304 else if (!complain)
5305 return error_mark_node;
5306 value = size_one_node;
5308 else
5310 if (complain)
5312 if (c_dialect_cxx ())
5313 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5314 "%<alignof%> applied to a function type");
5315 else
5316 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5317 "%<_Alignof%> applied to a function type");
5319 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5322 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5324 if (type_code == VOID_TYPE
5325 && complain && warn_pointer_arith)
5326 pedwarn (loc, OPT_Wpointer_arith,
5327 "invalid application of %qs to a void type", op_name);
5328 else if (!complain)
5329 return error_mark_node;
5330 value = size_one_node;
5332 else if (!COMPLETE_TYPE_P (type)
5333 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5335 if (complain)
5336 error_at (loc, "invalid application of %qs to incomplete type %qT",
5337 op_name, type);
5338 return error_mark_node;
5340 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5341 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5343 if (complain)
5344 error_at (loc, "invalid application of %qs to array type %qT of "
5345 "incomplete element type", op_name, type);
5346 return error_mark_node;
5348 else
5350 if (is_sizeof)
5351 /* Convert in case a char is more than one unit. */
5352 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5353 size_int (TYPE_PRECISION (char_type_node)
5354 / BITS_PER_UNIT));
5355 else if (min_alignof)
5356 value = size_int (min_align_of_type (type));
5357 else
5358 value = size_int (TYPE_ALIGN_UNIT (type));
5361 /* VALUE will have the middle-end integer type sizetype.
5362 However, we should really return a value of type `size_t',
5363 which is just a typedef for an ordinary integer type. */
5364 value = fold_convert_loc (loc, size_type_node, value);
5366 return value;
5369 /* Implement the __alignof keyword: Return the minimum required
5370 alignment of EXPR, measured in bytes. For VAR_DECLs,
5371 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5372 from an "aligned" __attribute__ specification). LOC is the
5373 location of the ALIGNOF operator. */
5375 tree
5376 c_alignof_expr (location_t loc, tree expr)
5378 tree t;
5380 if (VAR_OR_FUNCTION_DECL_P (expr))
5381 t = size_int (DECL_ALIGN_UNIT (expr));
5383 else if (TREE_CODE (expr) == COMPONENT_REF
5384 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5386 error_at (loc, "%<__alignof%> applied to a bit-field");
5387 t = size_one_node;
5389 else if (TREE_CODE (expr) == COMPONENT_REF
5390 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5391 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5393 else if (INDIRECT_REF_P (expr))
5395 tree t = TREE_OPERAND (expr, 0);
5396 tree best = t;
5397 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5399 while (CONVERT_EXPR_P (t)
5400 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5402 int thisalign;
5404 t = TREE_OPERAND (t, 0);
5405 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5406 if (thisalign > bestalign)
5407 best = t, bestalign = thisalign;
5409 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5411 else
5412 return c_alignof (loc, TREE_TYPE (expr));
5414 return fold_convert_loc (loc, size_type_node, t);
5417 /* Handle C and C++ default attributes. */
5419 enum built_in_attribute
5421 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5422 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5423 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5424 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5425 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5426 #include "builtin-attrs.def"
5427 #undef DEF_ATTR_NULL_TREE
5428 #undef DEF_ATTR_INT
5429 #undef DEF_ATTR_STRING
5430 #undef DEF_ATTR_IDENT
5431 #undef DEF_ATTR_TREE_LIST
5432 ATTR_LAST
5435 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5437 static void c_init_attributes (void);
5439 enum c_builtin_type
5441 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5442 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5443 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5444 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5445 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5446 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5447 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5448 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5449 ARG6) NAME,
5450 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5451 ARG6, ARG7) NAME,
5452 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5453 ARG6, ARG7, ARG8) NAME,
5454 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5455 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5456 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5457 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5458 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5459 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5460 NAME,
5461 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5462 ARG6, ARG7) NAME,
5463 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5464 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5465 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5466 #include "builtin-types.def"
5467 #undef DEF_PRIMITIVE_TYPE
5468 #undef DEF_FUNCTION_TYPE_0
5469 #undef DEF_FUNCTION_TYPE_1
5470 #undef DEF_FUNCTION_TYPE_2
5471 #undef DEF_FUNCTION_TYPE_3
5472 #undef DEF_FUNCTION_TYPE_4
5473 #undef DEF_FUNCTION_TYPE_5
5474 #undef DEF_FUNCTION_TYPE_6
5475 #undef DEF_FUNCTION_TYPE_7
5476 #undef DEF_FUNCTION_TYPE_8
5477 #undef DEF_FUNCTION_TYPE_VAR_0
5478 #undef DEF_FUNCTION_TYPE_VAR_1
5479 #undef DEF_FUNCTION_TYPE_VAR_2
5480 #undef DEF_FUNCTION_TYPE_VAR_3
5481 #undef DEF_FUNCTION_TYPE_VAR_4
5482 #undef DEF_FUNCTION_TYPE_VAR_5
5483 #undef DEF_FUNCTION_TYPE_VAR_7
5484 #undef DEF_FUNCTION_TYPE_VAR_11
5485 #undef DEF_POINTER_TYPE
5486 BT_LAST
5489 typedef enum c_builtin_type builtin_type;
5491 /* A temporary array for c_common_nodes_and_builtins. Used in
5492 communication with def_fn_type. */
5493 static tree builtin_types[(int) BT_LAST + 1];
5495 /* A helper function for c_common_nodes_and_builtins. Build function type
5496 for DEF with return type RET and N arguments. If VAR is true, then the
5497 function should be variadic after those N arguments.
5499 Takes special care not to ICE if any of the types involved are
5500 error_mark_node, which indicates that said type is not in fact available
5501 (see builtin_type_for_size). In which case the function type as a whole
5502 should be error_mark_node. */
5504 static void
5505 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5507 tree t;
5508 tree *args = XALLOCAVEC (tree, n);
5509 va_list list;
5510 int i;
5512 va_start (list, n);
5513 for (i = 0; i < n; ++i)
5515 builtin_type a = (builtin_type) va_arg (list, int);
5516 t = builtin_types[a];
5517 if (t == error_mark_node)
5518 goto egress;
5519 args[i] = t;
5522 t = builtin_types[ret];
5523 if (t == error_mark_node)
5524 goto egress;
5525 if (var)
5526 t = build_varargs_function_type_array (t, n, args);
5527 else
5528 t = build_function_type_array (t, n, args);
5530 egress:
5531 builtin_types[def] = t;
5532 va_end (list);
5535 /* Build builtin functions common to both C and C++ language
5536 frontends. */
5538 static void
5539 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5541 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5542 builtin_types[ENUM] = VALUE;
5543 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5544 def_fn_type (ENUM, RETURN, 0, 0);
5545 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5546 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5547 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5548 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5549 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5550 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5551 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5552 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5553 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5554 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5555 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5556 ARG6) \
5557 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5558 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5559 ARG6, ARG7) \
5560 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5561 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5562 ARG6, ARG7, ARG8) \
5563 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5564 ARG7, ARG8);
5565 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5566 def_fn_type (ENUM, RETURN, 1, 0);
5567 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5568 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5569 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5570 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5571 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5572 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5573 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5574 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5575 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5576 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5577 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5578 ARG6, ARG7) \
5579 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5580 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5581 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5582 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5583 ARG7, ARG8, ARG9, ARG10, ARG11);
5584 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5585 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5587 #include "builtin-types.def"
5589 #undef DEF_PRIMITIVE_TYPE
5590 #undef DEF_FUNCTION_TYPE_0
5591 #undef DEF_FUNCTION_TYPE_1
5592 #undef DEF_FUNCTION_TYPE_2
5593 #undef DEF_FUNCTION_TYPE_3
5594 #undef DEF_FUNCTION_TYPE_4
5595 #undef DEF_FUNCTION_TYPE_5
5596 #undef DEF_FUNCTION_TYPE_6
5597 #undef DEF_FUNCTION_TYPE_7
5598 #undef DEF_FUNCTION_TYPE_8
5599 #undef DEF_FUNCTION_TYPE_VAR_0
5600 #undef DEF_FUNCTION_TYPE_VAR_1
5601 #undef DEF_FUNCTION_TYPE_VAR_2
5602 #undef DEF_FUNCTION_TYPE_VAR_3
5603 #undef DEF_FUNCTION_TYPE_VAR_4
5604 #undef DEF_FUNCTION_TYPE_VAR_5
5605 #undef DEF_FUNCTION_TYPE_VAR_7
5606 #undef DEF_FUNCTION_TYPE_VAR_11
5607 #undef DEF_POINTER_TYPE
5608 builtin_types[(int) BT_LAST] = NULL_TREE;
5610 c_init_attributes ();
5612 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5613 NONANSI_P, ATTRS, IMPLICIT, COND) \
5614 if (NAME && COND) \
5615 def_builtin_1 (ENUM, NAME, CLASS, \
5616 builtin_types[(int) TYPE], \
5617 builtin_types[(int) LIBTYPE], \
5618 BOTH_P, FALLBACK_P, NONANSI_P, \
5619 built_in_attributes[(int) ATTRS], IMPLICIT);
5620 #include "builtins.def"
5621 #undef DEF_BUILTIN
5623 targetm.init_builtins ();
5625 build_common_builtin_nodes ();
5627 if (flag_cilkplus)
5628 cilk_init_builtins ();
5631 /* Like get_identifier, but avoid warnings about null arguments when
5632 the argument may be NULL for targets where GCC lacks stdint.h type
5633 information. */
5635 static inline tree
5636 c_get_ident (const char *id)
5638 return get_identifier (id);
5641 /* Build tree nodes and builtin functions common to both C and C++ language
5642 frontends. */
5644 void
5645 c_common_nodes_and_builtins (void)
5647 int char16_type_size;
5648 int char32_type_size;
5649 int wchar_type_size;
5650 tree array_domain_type;
5651 tree va_list_ref_type_node;
5652 tree va_list_arg_type_node;
5653 int i;
5655 build_common_tree_nodes (flag_signed_char, flag_short_double);
5657 /* Define `int' and `char' first so that dbx will output them first. */
5658 record_builtin_type (RID_INT, NULL, integer_type_node);
5659 record_builtin_type (RID_CHAR, "char", char_type_node);
5661 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5662 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5663 but not C. Are the conditionals here needed? */
5664 if (c_dialect_cxx ())
5665 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5666 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5667 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5668 record_builtin_type (RID_MAX, "long unsigned int",
5669 long_unsigned_type_node);
5671 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5673 char name[25];
5675 sprintf (name, "__int%d", int_n_data[i].bitsize);
5676 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5677 int_n_trees[i].signed_type);
5678 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5679 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5682 if (c_dialect_cxx ())
5683 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5684 record_builtin_type (RID_MAX, "long long int",
5685 long_long_integer_type_node);
5686 record_builtin_type (RID_MAX, "long long unsigned int",
5687 long_long_unsigned_type_node);
5688 if (c_dialect_cxx ())
5689 record_builtin_type (RID_MAX, "long long unsigned",
5690 long_long_unsigned_type_node);
5691 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5692 record_builtin_type (RID_MAX, "short unsigned int",
5693 short_unsigned_type_node);
5694 if (c_dialect_cxx ())
5695 record_builtin_type (RID_MAX, "unsigned short",
5696 short_unsigned_type_node);
5698 /* Define both `signed char' and `unsigned char'. */
5699 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5700 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5702 /* These are types that c_common_type_for_size and
5703 c_common_type_for_mode use. */
5704 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5705 TYPE_DECL, NULL_TREE,
5706 intQI_type_node));
5707 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5708 TYPE_DECL, NULL_TREE,
5709 intHI_type_node));
5710 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5711 TYPE_DECL, NULL_TREE,
5712 intSI_type_node));
5713 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5714 TYPE_DECL, NULL_TREE,
5715 intDI_type_node));
5716 #if HOST_BITS_PER_WIDE_INT >= 64
5717 /* Note that this is different than the __int128 type that's part of
5718 the generic __intN support. */
5719 if (targetm.scalar_mode_supported_p (TImode))
5720 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5721 TYPE_DECL,
5722 get_identifier ("__int128_t"),
5723 intTI_type_node));
5724 #endif
5725 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5726 TYPE_DECL, NULL_TREE,
5727 unsigned_intQI_type_node));
5728 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5729 TYPE_DECL, NULL_TREE,
5730 unsigned_intHI_type_node));
5731 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5732 TYPE_DECL, NULL_TREE,
5733 unsigned_intSI_type_node));
5734 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5735 TYPE_DECL, NULL_TREE,
5736 unsigned_intDI_type_node));
5737 #if HOST_BITS_PER_WIDE_INT >= 64
5738 if (targetm.scalar_mode_supported_p (TImode))
5739 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5740 TYPE_DECL,
5741 get_identifier ("__uint128_t"),
5742 unsigned_intTI_type_node));
5743 #endif
5745 /* Create the widest literal types. */
5746 widest_integer_literal_type_node
5747 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5748 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5749 TYPE_DECL, NULL_TREE,
5750 widest_integer_literal_type_node));
5752 widest_unsigned_literal_type_node
5753 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5754 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5755 TYPE_DECL, NULL_TREE,
5756 widest_unsigned_literal_type_node));
5758 signed_size_type_node = c_common_signed_type (size_type_node);
5760 pid_type_node =
5761 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5763 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5764 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5765 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5767 /* Only supported decimal floating point extension if the target
5768 actually supports underlying modes. */
5769 if (targetm.scalar_mode_supported_p (SDmode)
5770 && targetm.scalar_mode_supported_p (DDmode)
5771 && targetm.scalar_mode_supported_p (TDmode))
5773 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5774 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5775 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5778 if (targetm.fixed_point_supported_p ())
5780 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5781 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5782 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5783 record_builtin_type (RID_MAX, "long long _Fract",
5784 long_long_fract_type_node);
5785 record_builtin_type (RID_MAX, "unsigned short _Fract",
5786 unsigned_short_fract_type_node);
5787 record_builtin_type (RID_MAX, "unsigned _Fract",
5788 unsigned_fract_type_node);
5789 record_builtin_type (RID_MAX, "unsigned long _Fract",
5790 unsigned_long_fract_type_node);
5791 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5792 unsigned_long_long_fract_type_node);
5793 record_builtin_type (RID_MAX, "_Sat short _Fract",
5794 sat_short_fract_type_node);
5795 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5796 record_builtin_type (RID_MAX, "_Sat long _Fract",
5797 sat_long_fract_type_node);
5798 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5799 sat_long_long_fract_type_node);
5800 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5801 sat_unsigned_short_fract_type_node);
5802 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5803 sat_unsigned_fract_type_node);
5804 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5805 sat_unsigned_long_fract_type_node);
5806 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5807 sat_unsigned_long_long_fract_type_node);
5808 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5809 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5810 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5811 record_builtin_type (RID_MAX, "long long _Accum",
5812 long_long_accum_type_node);
5813 record_builtin_type (RID_MAX, "unsigned short _Accum",
5814 unsigned_short_accum_type_node);
5815 record_builtin_type (RID_MAX, "unsigned _Accum",
5816 unsigned_accum_type_node);
5817 record_builtin_type (RID_MAX, "unsigned long _Accum",
5818 unsigned_long_accum_type_node);
5819 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5820 unsigned_long_long_accum_type_node);
5821 record_builtin_type (RID_MAX, "_Sat short _Accum",
5822 sat_short_accum_type_node);
5823 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5824 record_builtin_type (RID_MAX, "_Sat long _Accum",
5825 sat_long_accum_type_node);
5826 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5827 sat_long_long_accum_type_node);
5828 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5829 sat_unsigned_short_accum_type_node);
5830 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5831 sat_unsigned_accum_type_node);
5832 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5833 sat_unsigned_long_accum_type_node);
5834 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5835 sat_unsigned_long_long_accum_type_node);
5839 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5840 TYPE_DECL,
5841 get_identifier ("complex int"),
5842 complex_integer_type_node));
5843 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5844 TYPE_DECL,
5845 get_identifier ("complex float"),
5846 complex_float_type_node));
5847 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5848 TYPE_DECL,
5849 get_identifier ("complex double"),
5850 complex_double_type_node));
5851 lang_hooks.decls.pushdecl
5852 (build_decl (UNKNOWN_LOCATION,
5853 TYPE_DECL, get_identifier ("complex long double"),
5854 complex_long_double_type_node));
5856 if (c_dialect_cxx ())
5857 /* For C++, make fileptr_type_node a distinct void * type until
5858 FILE type is defined. */
5859 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5861 record_builtin_type (RID_VOID, NULL, void_type_node);
5863 /* Set the TYPE_NAME for any variants that were built before
5864 record_builtin_type gave names to the built-in types. */
5866 tree void_name = TYPE_NAME (void_type_node);
5867 TYPE_NAME (void_type_node) = NULL_TREE;
5868 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5869 = void_name;
5870 TYPE_NAME (void_type_node) = void_name;
5873 void_list_node = build_void_list_node ();
5875 /* Make a type to be the domain of a few array types
5876 whose domains don't really matter.
5877 200 is small enough that it always fits in size_t
5878 and large enough that it can hold most function names for the
5879 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5880 array_domain_type = build_index_type (size_int (200));
5882 /* Make a type for arrays of characters.
5883 With luck nothing will ever really depend on the length of this
5884 array type. */
5885 char_array_type_node
5886 = build_array_type (char_type_node, array_domain_type);
5888 string_type_node = build_pointer_type (char_type_node);
5889 const_string_type_node
5890 = build_pointer_type (build_qualified_type
5891 (char_type_node, TYPE_QUAL_CONST));
5893 /* This is special for C++ so functions can be overloaded. */
5894 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5895 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5896 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5897 underlying_wchar_type_node = wchar_type_node;
5898 if (c_dialect_cxx ())
5900 if (TYPE_UNSIGNED (wchar_type_node))
5901 wchar_type_node = make_unsigned_type (wchar_type_size);
5902 else
5903 wchar_type_node = make_signed_type (wchar_type_size);
5904 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5907 /* This is for wide string constants. */
5908 wchar_array_type_node
5909 = build_array_type (wchar_type_node, array_domain_type);
5911 /* Define 'char16_t'. */
5912 char16_type_node = get_identifier (CHAR16_TYPE);
5913 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5914 char16_type_size = TYPE_PRECISION (char16_type_node);
5915 if (c_dialect_cxx ())
5917 char16_type_node = make_unsigned_type (char16_type_size);
5919 if (cxx_dialect >= cxx11)
5920 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5923 /* This is for UTF-16 string constants. */
5924 char16_array_type_node
5925 = build_array_type (char16_type_node, array_domain_type);
5927 /* Define 'char32_t'. */
5928 char32_type_node = get_identifier (CHAR32_TYPE);
5929 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5930 char32_type_size = TYPE_PRECISION (char32_type_node);
5931 if (c_dialect_cxx ())
5933 char32_type_node = make_unsigned_type (char32_type_size);
5935 if (cxx_dialect >= cxx11)
5936 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5939 /* This is for UTF-32 string constants. */
5940 char32_array_type_node
5941 = build_array_type (char32_type_node, array_domain_type);
5943 wint_type_node =
5944 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5946 intmax_type_node =
5947 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5948 uintmax_type_node =
5949 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5951 if (SIG_ATOMIC_TYPE)
5952 sig_atomic_type_node =
5953 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5954 if (INT8_TYPE)
5955 int8_type_node =
5956 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5957 if (INT16_TYPE)
5958 int16_type_node =
5959 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5960 if (INT32_TYPE)
5961 int32_type_node =
5962 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5963 if (INT64_TYPE)
5964 int64_type_node =
5965 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5966 if (UINT8_TYPE)
5967 uint8_type_node =
5968 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5969 if (UINT16_TYPE)
5970 c_uint16_type_node = uint16_type_node =
5971 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5972 if (UINT32_TYPE)
5973 c_uint32_type_node = uint32_type_node =
5974 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5975 if (UINT64_TYPE)
5976 c_uint64_type_node = uint64_type_node =
5977 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5978 if (INT_LEAST8_TYPE)
5979 int_least8_type_node =
5980 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5981 if (INT_LEAST16_TYPE)
5982 int_least16_type_node =
5983 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5984 if (INT_LEAST32_TYPE)
5985 int_least32_type_node =
5986 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5987 if (INT_LEAST64_TYPE)
5988 int_least64_type_node =
5989 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5990 if (UINT_LEAST8_TYPE)
5991 uint_least8_type_node =
5992 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5993 if (UINT_LEAST16_TYPE)
5994 uint_least16_type_node =
5995 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5996 if (UINT_LEAST32_TYPE)
5997 uint_least32_type_node =
5998 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5999 if (UINT_LEAST64_TYPE)
6000 uint_least64_type_node =
6001 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
6002 if (INT_FAST8_TYPE)
6003 int_fast8_type_node =
6004 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
6005 if (INT_FAST16_TYPE)
6006 int_fast16_type_node =
6007 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
6008 if (INT_FAST32_TYPE)
6009 int_fast32_type_node =
6010 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
6011 if (INT_FAST64_TYPE)
6012 int_fast64_type_node =
6013 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
6014 if (UINT_FAST8_TYPE)
6015 uint_fast8_type_node =
6016 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
6017 if (UINT_FAST16_TYPE)
6018 uint_fast16_type_node =
6019 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
6020 if (UINT_FAST32_TYPE)
6021 uint_fast32_type_node =
6022 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
6023 if (UINT_FAST64_TYPE)
6024 uint_fast64_type_node =
6025 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
6026 if (INTPTR_TYPE)
6027 intptr_type_node =
6028 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
6029 if (UINTPTR_TYPE)
6030 uintptr_type_node =
6031 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
6033 default_function_type
6034 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6035 ptrdiff_type_node
6036 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
6037 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6039 lang_hooks.decls.pushdecl
6040 (build_decl (UNKNOWN_LOCATION,
6041 TYPE_DECL, get_identifier ("__builtin_va_list"),
6042 va_list_type_node));
6043 if (targetm.enum_va_list_p)
6045 int l;
6046 const char *pname;
6047 tree ptype;
6049 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
6051 lang_hooks.decls.pushdecl
6052 (build_decl (UNKNOWN_LOCATION,
6053 TYPE_DECL, get_identifier (pname),
6054 ptype));
6059 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
6061 va_list_arg_type_node = va_list_ref_type_node =
6062 build_pointer_type (TREE_TYPE (va_list_type_node));
6064 else
6066 va_list_arg_type_node = va_list_type_node;
6067 va_list_ref_type_node = build_reference_type (va_list_type_node);
6070 if (!flag_preprocess_only)
6071 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
6073 main_identifier_node = get_identifier ("main");
6075 /* Create the built-in __null node. It is important that this is
6076 not shared. */
6077 null_node = make_int_cst (1, 1);
6078 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
6080 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6081 memset (builtin_types, 0, sizeof (builtin_types));
6084 /* The number of named compound-literals generated thus far. */
6085 static GTY(()) int compound_literal_number;
6087 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6089 void
6090 set_compound_literal_name (tree decl)
6092 char *name;
6093 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6094 compound_literal_number);
6095 compound_literal_number++;
6096 DECL_NAME (decl) = get_identifier (name);
6099 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6100 TYPE and operand OP. */
6102 static tree
6103 build_va_arg_1 (location_t loc, tree type, tree op)
6105 tree expr = build1 (VA_ARG_EXPR, type, op);
6106 SET_EXPR_LOCATION (expr, loc);
6107 return expr;
6110 /* Return a VA_ARG_EXPR corresponding to a source-level expression
6111 va_arg (EXPR, TYPE) at source location LOC. */
6113 tree
6114 build_va_arg (location_t loc, tree expr, tree type)
6116 tree va_type = TREE_TYPE (expr);
6117 tree canon_va_type = (va_type == error_mark_node
6118 ? NULL_TREE
6119 : targetm.canonical_va_list_type (va_type));
6121 if (va_type == error_mark_node
6122 || canon_va_type == NULL_TREE)
6124 /* Let's handle things neutrallly, if expr:
6125 - has undeclared type, or
6126 - is not an va_list type. */
6127 return build_va_arg_1 (loc, type, expr);
6130 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6132 /* Case 1: Not an array type. */
6134 /* Take the address, to get '&ap'. */
6135 mark_addressable (expr);
6136 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6138 /* Verify that &ap is still recognized as having va_list type. */
6139 tree canon_expr_type
6140 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6141 gcc_assert (canon_expr_type != NULL_TREE);
6143 return build_va_arg_1 (loc, type, expr);
6146 /* Case 2: Array type.
6148 Background:
6150 For contrast, let's start with the simple case (case 1). If
6151 canon_va_type is not an array type, but say a char *, then when
6152 passing-by-value a va_list, the type of the va_list param decl is
6153 the same as for another va_list decl (all ap's are char *):
6155 f2_1 (char * ap)
6156 D.1815 = VA_ARG (&ap, 0B, 1);
6157 return D.1815;
6159 f2 (int i)
6160 char * ap.0;
6161 char * ap;
6162 __builtin_va_start (&ap, 0);
6163 ap.0 = ap;
6164 res = f2_1 (ap.0);
6165 __builtin_va_end (&ap);
6166 D.1812 = res;
6167 return D.1812;
6169 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6170 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6171 the same as for another va_list decl (case 2a, struct ap[1]).
6173 f2_1 (struct * ap)
6174 D.1844 = VA_ARG (ap, 0B, 0);
6175 return D.1844;
6177 f2 (int i)
6178 struct ap[1];
6179 __builtin_va_start (&ap, 0);
6180 res = f2_1 (&ap);
6181 __builtin_va_end (&ap);
6182 D.1841 = res;
6183 return D.1841;
6185 Case 2b is different because:
6186 - on the callee side, the parm decl has declared type va_list, but
6187 grokdeclarator changes the type of the parm decl to a pointer to the
6188 array elem type.
6189 - on the caller side, the pass-by-value uses &ap.
6191 We unify these two cases (case 2a: va_list is array type,
6192 case 2b: va_list is pointer to array elem type), by adding '&' for the
6193 array type case, such that we have a pointer to array elem in both
6194 cases. */
6196 if (TREE_CODE (va_type) == ARRAY_TYPE)
6198 /* Case 2a: va_list is array type. */
6200 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6201 elem type. */
6202 mark_addressable (expr);
6203 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6204 expr);
6206 /* Verify that &ap is still recognized as having va_list type. */
6207 tree canon_expr_type
6208 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6209 gcc_assert (canon_expr_type != NULL_TREE);
6211 else
6213 /* Case 2b: va_list is pointer to array elem type. */
6214 gcc_assert (POINTER_TYPE_P (va_type));
6215 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6217 /* Don't take the address. We've already got '&ap'. */
6221 return build_va_arg_1 (loc, type, expr);
6225 /* Linked list of disabled built-in functions. */
6227 typedef struct disabled_builtin
6229 const char *name;
6230 struct disabled_builtin *next;
6231 } disabled_builtin;
6232 static disabled_builtin *disabled_builtins = NULL;
6234 static bool builtin_function_disabled_p (const char *);
6236 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6237 begins with "__builtin_", give an error. */
6239 void
6240 disable_builtin_function (const char *name)
6242 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6243 error ("cannot disable built-in function %qs", name);
6244 else
6246 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6247 new_disabled_builtin->name = name;
6248 new_disabled_builtin->next = disabled_builtins;
6249 disabled_builtins = new_disabled_builtin;
6254 /* Return true if the built-in function NAME has been disabled, false
6255 otherwise. */
6257 static bool
6258 builtin_function_disabled_p (const char *name)
6260 disabled_builtin *p;
6261 for (p = disabled_builtins; p != NULL; p = p->next)
6263 if (strcmp (name, p->name) == 0)
6264 return true;
6266 return false;
6270 /* Worker for DEF_BUILTIN.
6271 Possibly define a builtin function with one or two names.
6272 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6273 nonansi_p and flag_no_nonansi_builtin. */
6275 static void
6276 def_builtin_1 (enum built_in_function fncode,
6277 const char *name,
6278 enum built_in_class fnclass,
6279 tree fntype, tree libtype,
6280 bool both_p, bool fallback_p, bool nonansi_p,
6281 tree fnattrs, bool implicit_p)
6283 tree decl;
6284 const char *libname;
6286 if (fntype == error_mark_node)
6287 return;
6289 gcc_assert ((!both_p && !fallback_p)
6290 || !strncmp (name, "__builtin_",
6291 strlen ("__builtin_")));
6293 libname = name + strlen ("__builtin_");
6294 decl = add_builtin_function (name, fntype, fncode, fnclass,
6295 (fallback_p ? libname : NULL),
6296 fnattrs);
6298 set_builtin_decl (fncode, decl, implicit_p);
6300 if (both_p
6301 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6302 && !(nonansi_p && flag_no_nonansi_builtin))
6303 add_builtin_function (libname, libtype, fncode, fnclass,
6304 NULL, fnattrs);
6307 /* Nonzero if the type T promotes to int. This is (nearly) the
6308 integral promotions defined in ISO C99 6.3.1.1/2. */
6310 bool
6311 c_promoting_integer_type_p (const_tree t)
6313 switch (TREE_CODE (t))
6315 case INTEGER_TYPE:
6316 return (TYPE_MAIN_VARIANT (t) == char_type_node
6317 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6318 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6319 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6320 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6321 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6323 case ENUMERAL_TYPE:
6324 /* ??? Technically all enumerations not larger than an int
6325 promote to an int. But this is used along code paths
6326 that only want to notice a size change. */
6327 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6329 case BOOLEAN_TYPE:
6330 return 1;
6332 default:
6333 return 0;
6337 /* Return 1 if PARMS specifies a fixed number of parameters
6338 and none of their types is affected by default promotions. */
6341 self_promoting_args_p (const_tree parms)
6343 const_tree t;
6344 for (t = parms; t; t = TREE_CHAIN (t))
6346 tree type = TREE_VALUE (t);
6348 if (type == error_mark_node)
6349 continue;
6351 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6352 return 0;
6354 if (type == 0)
6355 return 0;
6357 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6358 return 0;
6360 if (c_promoting_integer_type_p (type))
6361 return 0;
6363 return 1;
6366 /* Recursively remove any '*' or '&' operator from TYPE. */
6367 tree
6368 strip_pointer_operator (tree t)
6370 while (POINTER_TYPE_P (t))
6371 t = TREE_TYPE (t);
6372 return t;
6375 /* Recursively remove pointer or array type from TYPE. */
6376 tree
6377 strip_pointer_or_array_types (tree t)
6379 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6380 t = TREE_TYPE (t);
6381 return t;
6384 /* Used to compare case labels. K1 and K2 are actually tree nodes
6385 representing case labels, or NULL_TREE for a `default' label.
6386 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6387 K2, and 0 if K1 and K2 are equal. */
6390 case_compare (splay_tree_key k1, splay_tree_key k2)
6392 /* Consider a NULL key (such as arises with a `default' label) to be
6393 smaller than anything else. */
6394 if (!k1)
6395 return k2 ? -1 : 0;
6396 else if (!k2)
6397 return k1 ? 1 : 0;
6399 return tree_int_cst_compare ((tree) k1, (tree) k2);
6402 /* Process a case label, located at LOC, for the range LOW_VALUE
6403 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6404 then this case label is actually a `default' label. If only
6405 HIGH_VALUE is NULL_TREE, then case label was declared using the
6406 usual C/C++ syntax, rather than the GNU case range extension.
6407 CASES is a tree containing all the case ranges processed so far;
6408 COND is the condition for the switch-statement itself.
6409 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6410 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6411 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6413 tree
6414 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6415 tree low_value, tree high_value, bool *outside_range_p)
6417 tree type;
6418 tree label;
6419 tree case_label;
6420 splay_tree_node node;
6422 /* Create the LABEL_DECL itself. */
6423 label = create_artificial_label (loc);
6425 /* If there was an error processing the switch condition, bail now
6426 before we get more confused. */
6427 if (!cond || cond == error_mark_node)
6428 goto error_out;
6430 if ((low_value && TREE_TYPE (low_value)
6431 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6432 || (high_value && TREE_TYPE (high_value)
6433 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6435 error_at (loc, "pointers are not permitted as case values");
6436 goto error_out;
6439 /* Case ranges are a GNU extension. */
6440 if (high_value)
6441 pedwarn (loc, OPT_Wpedantic,
6442 "range expressions in switch statements are non-standard");
6444 type = TREE_TYPE (cond);
6445 if (low_value)
6447 low_value = check_case_value (loc, low_value);
6448 low_value = convert_and_check (loc, type, low_value);
6449 if (low_value == error_mark_node)
6450 goto error_out;
6452 if (high_value)
6454 high_value = check_case_value (loc, high_value);
6455 high_value = convert_and_check (loc, type, high_value);
6456 if (high_value == error_mark_node)
6457 goto error_out;
6460 if (low_value && high_value)
6462 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6463 really a case range, even though it was written that way.
6464 Remove the HIGH_VALUE to simplify later processing. */
6465 if (tree_int_cst_equal (low_value, high_value))
6466 high_value = NULL_TREE;
6467 else if (!tree_int_cst_lt (low_value, high_value))
6468 warning_at (loc, 0, "empty range specified");
6471 /* See if the case is in range of the type of the original testing
6472 expression. If both low_value and high_value are out of range,
6473 don't insert the case label and return NULL_TREE. */
6474 if (low_value
6475 && !check_case_bounds (loc, type, orig_type,
6476 &low_value, high_value ? &high_value : NULL,
6477 outside_range_p))
6478 return NULL_TREE;
6480 /* Look up the LOW_VALUE in the table of case labels we already
6481 have. */
6482 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6483 /* If there was not an exact match, check for overlapping ranges.
6484 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6485 that's a `default' label and the only overlap is an exact match. */
6486 if (!node && (low_value || high_value))
6488 splay_tree_node low_bound;
6489 splay_tree_node high_bound;
6491 /* Even though there wasn't an exact match, there might be an
6492 overlap between this case range and another case range.
6493 Since we've (inductively) not allowed any overlapping case
6494 ranges, we simply need to find the greatest low case label
6495 that is smaller that LOW_VALUE, and the smallest low case
6496 label that is greater than LOW_VALUE. If there is an overlap
6497 it will occur in one of these two ranges. */
6498 low_bound = splay_tree_predecessor (cases,
6499 (splay_tree_key) low_value);
6500 high_bound = splay_tree_successor (cases,
6501 (splay_tree_key) low_value);
6503 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6504 the LOW_VALUE, so there is no need to check unless the
6505 LOW_BOUND is in fact itself a case range. */
6506 if (low_bound
6507 && CASE_HIGH ((tree) low_bound->value)
6508 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6509 low_value) >= 0)
6510 node = low_bound;
6511 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6512 range is bigger than the low end of the current range, so we
6513 are only interested if the current range is a real range, and
6514 not an ordinary case label. */
6515 else if (high_bound
6516 && high_value
6517 && (tree_int_cst_compare ((tree) high_bound->key,
6518 high_value)
6519 <= 0))
6520 node = high_bound;
6522 /* If there was an overlap, issue an error. */
6523 if (node)
6525 tree duplicate = CASE_LABEL ((tree) node->value);
6527 if (high_value)
6529 error_at (loc, "duplicate (or overlapping) case value");
6530 error_at (DECL_SOURCE_LOCATION (duplicate),
6531 "this is the first entry overlapping that value");
6533 else if (low_value)
6535 error_at (loc, "duplicate case value") ;
6536 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6538 else
6540 error_at (loc, "multiple default labels in one switch");
6541 error_at (DECL_SOURCE_LOCATION (duplicate),
6542 "this is the first default label");
6544 goto error_out;
6547 /* Add a CASE_LABEL to the statement-tree. */
6548 case_label = add_stmt (build_case_label (low_value, high_value, label));
6549 /* Register this case label in the splay tree. */
6550 splay_tree_insert (cases,
6551 (splay_tree_key) low_value,
6552 (splay_tree_value) case_label);
6554 return case_label;
6556 error_out:
6557 /* Add a label so that the back-end doesn't think that the beginning of
6558 the switch is unreachable. Note that we do not add a case label, as
6559 that just leads to duplicates and thence to failure later on. */
6560 if (!cases->root)
6562 tree t = create_artificial_label (loc);
6563 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6565 return error_mark_node;
6568 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6569 Used to verify that case values match up with enumerator values. */
6571 static void
6572 match_case_to_enum_1 (tree key, tree type, tree label)
6574 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6576 if (tree_fits_uhwi_p (key))
6577 print_dec (key, buf, UNSIGNED);
6578 else if (tree_fits_shwi_p (key))
6579 print_dec (key, buf, SIGNED);
6580 else
6581 print_hex (key, buf);
6583 if (TYPE_NAME (type) == 0)
6584 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6585 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6586 "case value %qs not in enumerated type",
6587 buf);
6588 else
6589 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6590 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6591 "case value %qs not in enumerated type %qT",
6592 buf, type);
6595 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6596 Used to verify that case values match up with enumerator values. */
6598 static int
6599 match_case_to_enum (splay_tree_node node, void *data)
6601 tree label = (tree) node->value;
6602 tree type = (tree) data;
6604 /* Skip default case. */
6605 if (!CASE_LOW (label))
6606 return 0;
6608 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6609 when we did our enum->case scan. Reset our scratch bit after. */
6610 if (!CASE_LOW_SEEN (label))
6611 match_case_to_enum_1 (CASE_LOW (label), type, label);
6612 else
6613 CASE_LOW_SEEN (label) = 0;
6615 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6616 not set, that means that CASE_HIGH did not appear when we did our
6617 enum->case scan. Reset our scratch bit after. */
6618 if (CASE_HIGH (label))
6620 if (!CASE_HIGH_SEEN (label))
6621 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6622 else
6623 CASE_HIGH_SEEN (label) = 0;
6626 return 0;
6629 /* Handle -Wswitch*. Called from the front end after parsing the
6630 switch construct. */
6631 /* ??? Should probably be somewhere generic, since other languages
6632 besides C and C++ would want this. At the moment, however, C/C++
6633 are the only tree-ssa languages that support enumerations at all,
6634 so the point is moot. */
6636 void
6637 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6638 tree type, tree cond, bool bool_cond_p,
6639 bool outside_range_p)
6641 splay_tree_node default_node;
6642 splay_tree_node node;
6643 tree chain;
6645 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6646 && !warn_switch_bool)
6647 return;
6649 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6650 if (!default_node)
6651 warning_at (switch_location, OPT_Wswitch_default,
6652 "switch missing default case");
6654 /* There are certain cases where -Wswitch-bool warnings aren't
6655 desirable, such as
6656 switch (boolean)
6658 case true: ...
6659 case false: ...
6661 so be careful here. */
6662 if (warn_switch_bool && bool_cond_p)
6664 splay_tree_node min_node;
6665 /* If there's a default node, it's also the value with the minimal
6666 key. So look at the penultimate key (if any). */
6667 if (default_node)
6668 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6669 else
6670 min_node = splay_tree_min (cases);
6671 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6673 splay_tree_node max_node = splay_tree_max (cases);
6674 /* This might be a case range, so look at the value with the
6675 maximal key and then check CASE_HIGH. */
6676 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6677 if (max)
6678 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6680 /* If there's a case value > 1 or < 0, that is outside bool
6681 range, warn. */
6682 if (outside_range_p
6683 || (max && wi::gts_p (max, 1))
6684 || (min && wi::lts_p (min, 0))
6685 /* And handle the
6686 switch (boolean)
6688 case true: ...
6689 case false: ...
6690 default: ...
6692 case, where we want to warn. */
6693 || (default_node
6694 && max && wi::eq_p (max, 1)
6695 && min && wi::eq_p (min, 0)))
6696 warning_at (switch_location, OPT_Wswitch_bool,
6697 "switch condition has boolean value");
6700 /* From here on, we only care about enumerated types. */
6701 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6702 return;
6704 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6705 if (!warn_switch_enum && !warn_switch)
6706 return;
6708 /* Check the cases. Warn about case values which are not members of
6709 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6710 there is no default case, check that exactly all enumeration
6711 literals are covered by the cases. */
6713 /* Clearing COND if it is not an integer constant simplifies
6714 the tests inside the loop below. */
6715 if (TREE_CODE (cond) != INTEGER_CST)
6716 cond = NULL_TREE;
6718 /* The time complexity here is O(N*lg(N)) worst case, but for the
6719 common case of monotonically increasing enumerators, it is
6720 O(N), since the nature of the splay tree will keep the next
6721 element adjacent to the root at all times. */
6723 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6725 tree value = TREE_VALUE (chain);
6726 if (TREE_CODE (value) == CONST_DECL)
6727 value = DECL_INITIAL (value);
6728 node = splay_tree_lookup (cases, (splay_tree_key) value);
6729 if (node)
6731 /* Mark the CASE_LOW part of the case entry as seen. */
6732 tree label = (tree) node->value;
6733 CASE_LOW_SEEN (label) = 1;
6734 continue;
6737 /* Even though there wasn't an exact match, there might be a
6738 case range which includes the enumerator's value. */
6739 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6740 if (node && CASE_HIGH ((tree) node->value))
6742 tree label = (tree) node->value;
6743 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6744 if (cmp >= 0)
6746 /* If we match the upper bound exactly, mark the CASE_HIGH
6747 part of the case entry as seen. */
6748 if (cmp == 0)
6749 CASE_HIGH_SEEN (label) = 1;
6750 continue;
6754 /* We've now determined that this enumerated literal isn't
6755 handled by the case labels of the switch statement. */
6757 /* If the switch expression is a constant, we only really care
6758 about whether that constant is handled by the switch. */
6759 if (cond && tree_int_cst_compare (cond, value))
6760 continue;
6762 /* If there is a default_node, the only relevant option is
6763 Wswitch-enum. Otherwise, if both are enabled then we prefer
6764 to warn using -Wswitch because -Wswitch is enabled by -Wall
6765 while -Wswitch-enum is explicit. */
6766 warning_at (switch_location,
6767 (default_node || !warn_switch
6768 ? OPT_Wswitch_enum
6769 : OPT_Wswitch),
6770 "enumeration value %qE not handled in switch",
6771 TREE_PURPOSE (chain));
6774 /* Warn if there are case expressions that don't correspond to
6775 enumerators. This can occur since C and C++ don't enforce
6776 type-checking of assignments to enumeration variables.
6778 The time complexity here is now always O(N) worst case, since
6779 we should have marked both the lower bound and upper bound of
6780 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6781 above. This scan also resets those fields. */
6783 splay_tree_foreach (cases, match_case_to_enum, type);
6786 /* Finish an expression taking the address of LABEL (an
6787 IDENTIFIER_NODE). Returns an expression for the address.
6789 LOC is the location for the expression returned. */
6791 tree
6792 finish_label_address_expr (tree label, location_t loc)
6794 tree result;
6796 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6798 if (label == error_mark_node)
6799 return error_mark_node;
6801 label = lookup_label (label);
6802 if (label == NULL_TREE)
6803 result = null_pointer_node;
6804 else
6806 TREE_USED (label) = 1;
6807 result = build1 (ADDR_EXPR, ptr_type_node, label);
6808 /* The current function is not necessarily uninlinable.
6809 Computed gotos are incompatible with inlining, but the value
6810 here could be used only in a diagnostic, for example. */
6811 protected_set_expr_location (result, loc);
6814 return result;
6818 /* Given a boolean expression ARG, return a tree representing an increment
6819 or decrement (as indicated by CODE) of ARG. The front end must check for
6820 invalid cases (e.g., decrement in C++). */
6821 tree
6822 boolean_increment (enum tree_code code, tree arg)
6824 tree val;
6825 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6827 arg = stabilize_reference (arg);
6828 switch (code)
6830 case PREINCREMENT_EXPR:
6831 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6832 break;
6833 case POSTINCREMENT_EXPR:
6834 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6835 arg = save_expr (arg);
6836 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6837 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6838 break;
6839 case PREDECREMENT_EXPR:
6840 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6841 invert_truthvalue_loc (input_location, arg));
6842 break;
6843 case POSTDECREMENT_EXPR:
6844 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6845 invert_truthvalue_loc (input_location, arg));
6846 arg = save_expr (arg);
6847 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6848 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6849 break;
6850 default:
6851 gcc_unreachable ();
6853 TREE_SIDE_EFFECTS (val) = 1;
6854 return val;
6857 /* Built-in macros for stddef.h and stdint.h, that require macros
6858 defined in this file. */
6859 void
6860 c_stddef_cpp_builtins(void)
6862 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6863 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6864 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6865 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6866 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6867 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6868 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6869 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6870 if (SIG_ATOMIC_TYPE)
6871 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6872 if (INT8_TYPE)
6873 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6874 if (INT16_TYPE)
6875 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6876 if (INT32_TYPE)
6877 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6878 if (INT64_TYPE)
6879 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6880 if (UINT8_TYPE)
6881 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6882 if (UINT16_TYPE)
6883 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6884 if (UINT32_TYPE)
6885 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6886 if (UINT64_TYPE)
6887 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6888 if (INT_LEAST8_TYPE)
6889 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6890 if (INT_LEAST16_TYPE)
6891 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6892 if (INT_LEAST32_TYPE)
6893 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6894 if (INT_LEAST64_TYPE)
6895 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6896 if (UINT_LEAST8_TYPE)
6897 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6898 if (UINT_LEAST16_TYPE)
6899 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6900 if (UINT_LEAST32_TYPE)
6901 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6902 if (UINT_LEAST64_TYPE)
6903 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6904 if (INT_FAST8_TYPE)
6905 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6906 if (INT_FAST16_TYPE)
6907 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6908 if (INT_FAST32_TYPE)
6909 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6910 if (INT_FAST64_TYPE)
6911 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6912 if (UINT_FAST8_TYPE)
6913 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6914 if (UINT_FAST16_TYPE)
6915 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6916 if (UINT_FAST32_TYPE)
6917 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6918 if (UINT_FAST64_TYPE)
6919 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6920 if (INTPTR_TYPE)
6921 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6922 if (UINTPTR_TYPE)
6923 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6926 static void
6927 c_init_attributes (void)
6929 /* Fill in the built_in_attributes array. */
6930 #define DEF_ATTR_NULL_TREE(ENUM) \
6931 built_in_attributes[(int) ENUM] = NULL_TREE;
6932 #define DEF_ATTR_INT(ENUM, VALUE) \
6933 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6934 #define DEF_ATTR_STRING(ENUM, VALUE) \
6935 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6936 #define DEF_ATTR_IDENT(ENUM, STRING) \
6937 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6938 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6939 built_in_attributes[(int) ENUM] \
6940 = tree_cons (built_in_attributes[(int) PURPOSE], \
6941 built_in_attributes[(int) VALUE], \
6942 built_in_attributes[(int) CHAIN]);
6943 #include "builtin-attrs.def"
6944 #undef DEF_ATTR_NULL_TREE
6945 #undef DEF_ATTR_INT
6946 #undef DEF_ATTR_IDENT
6947 #undef DEF_ATTR_TREE_LIST
6950 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6951 identifier as an argument, so the front end shouldn't look it up. */
6953 bool
6954 attribute_takes_identifier_p (const_tree attr_id)
6956 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6957 if (spec == NULL)
6958 /* Unknown attribute that we'll end up ignoring, return true so we
6959 don't complain about an identifier argument. */
6960 return true;
6961 else if (!strcmp ("mode", spec->name)
6962 || !strcmp ("format", spec->name)
6963 || !strcmp ("cleanup", spec->name))
6964 return true;
6965 else
6966 return targetm.attribute_takes_identifier_p (attr_id);
6969 /* Attribute handlers common to C front ends. */
6971 /* Handle a "packed" attribute; arguments as in
6972 struct attribute_spec.handler. */
6974 static tree
6975 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6976 int flags, bool *no_add_attrs)
6978 if (TYPE_P (*node))
6980 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6981 *node = build_variant_type_copy (*node);
6982 TYPE_PACKED (*node) = 1;
6984 else if (TREE_CODE (*node) == FIELD_DECL)
6986 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6987 /* Still pack bitfields. */
6988 && ! DECL_INITIAL (*node))
6989 warning (OPT_Wattributes,
6990 "%qE attribute ignored for field of type %qT",
6991 name, TREE_TYPE (*node));
6992 else
6993 DECL_PACKED (*node) = 1;
6995 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6996 used for DECL_REGISTER. It wouldn't mean anything anyway.
6997 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6998 that changes what the typedef is typing. */
6999 else
7001 warning (OPT_Wattributes, "%qE attribute ignored", name);
7002 *no_add_attrs = true;
7005 return NULL_TREE;
7008 /* Handle a "nocommon" attribute; arguments as in
7009 struct attribute_spec.handler. */
7011 static tree
7012 handle_nocommon_attribute (tree *node, tree name,
7013 tree ARG_UNUSED (args),
7014 int ARG_UNUSED (flags), bool *no_add_attrs)
7016 if (VAR_P (*node))
7017 DECL_COMMON (*node) = 0;
7018 else
7020 warning (OPT_Wattributes, "%qE attribute ignored", name);
7021 *no_add_attrs = true;
7024 return NULL_TREE;
7027 /* Handle a "common" attribute; arguments as in
7028 struct attribute_spec.handler. */
7030 static tree
7031 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7032 int ARG_UNUSED (flags), bool *no_add_attrs)
7034 if (VAR_P (*node))
7035 DECL_COMMON (*node) = 1;
7036 else
7038 warning (OPT_Wattributes, "%qE attribute ignored", name);
7039 *no_add_attrs = true;
7042 return NULL_TREE;
7045 /* Handle a "noreturn" attribute; arguments as in
7046 struct attribute_spec.handler. */
7048 static tree
7049 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7050 int ARG_UNUSED (flags), bool *no_add_attrs)
7052 tree type = TREE_TYPE (*node);
7054 /* See FIXME comment in c_common_attribute_table. */
7055 if (TREE_CODE (*node) == FUNCTION_DECL
7056 || objc_method_decl (TREE_CODE (*node)))
7057 TREE_THIS_VOLATILE (*node) = 1;
7058 else if (TREE_CODE (type) == POINTER_TYPE
7059 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7060 TREE_TYPE (*node)
7061 = (build_qualified_type
7062 (build_pointer_type
7063 (build_type_variant (TREE_TYPE (type),
7064 TYPE_READONLY (TREE_TYPE (type)), 1)),
7065 TYPE_QUALS (type)));
7066 else
7068 warning (OPT_Wattributes, "%qE attribute ignored", name);
7069 *no_add_attrs = true;
7072 return NULL_TREE;
7075 /* Handle a "hot" and attribute; arguments as in
7076 struct attribute_spec.handler. */
7078 static tree
7079 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7080 int ARG_UNUSED (flags), bool *no_add_attrs)
7082 if (TREE_CODE (*node) == FUNCTION_DECL
7083 || TREE_CODE (*node) == LABEL_DECL)
7085 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7087 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7088 "with attribute %qs", name, "cold");
7089 *no_add_attrs = true;
7091 /* Most of the rest of the hot processing is done later with
7092 lookup_attribute. */
7094 else
7096 warning (OPT_Wattributes, "%qE attribute ignored", name);
7097 *no_add_attrs = true;
7100 return NULL_TREE;
7103 /* Handle a "cold" and attribute; arguments as in
7104 struct attribute_spec.handler. */
7106 static tree
7107 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7108 int ARG_UNUSED (flags), bool *no_add_attrs)
7110 if (TREE_CODE (*node) == FUNCTION_DECL
7111 || TREE_CODE (*node) == LABEL_DECL)
7113 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7115 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7116 "with attribute %qs", name, "hot");
7117 *no_add_attrs = true;
7119 /* Most of the rest of the cold processing is done later with
7120 lookup_attribute. */
7122 else
7124 warning (OPT_Wattributes, "%qE attribute ignored", name);
7125 *no_add_attrs = true;
7128 return NULL_TREE;
7131 /* Handle a "no_sanitize_address" attribute; arguments as in
7132 struct attribute_spec.handler. */
7134 static tree
7135 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7136 bool *no_add_attrs)
7138 if (TREE_CODE (*node) != FUNCTION_DECL)
7140 warning (OPT_Wattributes, "%qE attribute ignored", name);
7141 *no_add_attrs = true;
7144 return NULL_TREE;
7147 /* Handle a "no_address_safety_analysis" attribute; arguments as in
7148 struct attribute_spec.handler. */
7150 static tree
7151 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7152 bool *no_add_attrs)
7154 if (TREE_CODE (*node) != FUNCTION_DECL)
7155 warning (OPT_Wattributes, "%qE attribute ignored", name);
7156 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7157 DECL_ATTRIBUTES (*node)
7158 = tree_cons (get_identifier ("no_sanitize_address"),
7159 NULL_TREE, DECL_ATTRIBUTES (*node));
7160 *no_add_attrs = true;
7161 return NULL_TREE;
7164 /* Handle a "no_sanitize_undefined" attribute; arguments as in
7165 struct attribute_spec.handler. */
7167 static tree
7168 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7169 bool *no_add_attrs)
7171 if (TREE_CODE (*node) != FUNCTION_DECL)
7173 warning (OPT_Wattributes, "%qE attribute ignored", name);
7174 *no_add_attrs = true;
7177 return NULL_TREE;
7180 /* Handle a "stack_protect" attribute; arguments as in
7181 struct attribute_spec.handler. */
7182 static tree
7183 handle_stack_protect_attribute (tree *node, tree name, tree, int,
7184 bool *no_add_attrs)
7186 if (TREE_CODE (*node) != FUNCTION_DECL)
7188 warning (OPT_Wattributes, "%qE attribute ignored", name);
7189 *no_add_attrs = true;
7191 else
7192 DECL_ATTRIBUTES (*node)
7193 = tree_cons (get_identifier ("stack_protect"),
7194 NULL_TREE, DECL_ATTRIBUTES (*node));
7196 return NULL_TREE;
7199 /* Handle a "noinline" attribute; arguments as in
7200 struct attribute_spec.handler. */
7202 static tree
7203 handle_noinline_attribute (tree *node, tree name,
7204 tree ARG_UNUSED (args),
7205 int ARG_UNUSED (flags), bool *no_add_attrs)
7207 if (TREE_CODE (*node) == FUNCTION_DECL)
7209 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7211 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7212 "with attribute %qs", name, "always_inline");
7213 *no_add_attrs = true;
7215 else
7216 DECL_UNINLINABLE (*node) = 1;
7218 else
7220 warning (OPT_Wattributes, "%qE attribute ignored", name);
7221 *no_add_attrs = true;
7224 return NULL_TREE;
7227 /* Handle a "noclone" attribute; arguments as in
7228 struct attribute_spec.handler. */
7230 static tree
7231 handle_noclone_attribute (tree *node, tree name,
7232 tree ARG_UNUSED (args),
7233 int ARG_UNUSED (flags), bool *no_add_attrs)
7235 if (TREE_CODE (*node) != FUNCTION_DECL)
7237 warning (OPT_Wattributes, "%qE attribute ignored", name);
7238 *no_add_attrs = true;
7241 return NULL_TREE;
7244 /* Handle a "no_icf" attribute; arguments as in
7245 struct attribute_spec.handler. */
7247 static tree
7248 handle_noicf_attribute (tree *node, tree name,
7249 tree ARG_UNUSED (args),
7250 int ARG_UNUSED (flags), bool *no_add_attrs)
7252 if (TREE_CODE (*node) != FUNCTION_DECL)
7254 warning (OPT_Wattributes, "%qE attribute ignored", name);
7255 *no_add_attrs = true;
7258 return NULL_TREE;
7261 /* Handle a "hsa" attribute; arguments as in
7262 struct attribute_spec.handler. */
7264 static tree
7265 handle_hsa_attribute (tree *node, tree name,
7266 tree ARG_UNUSED (args),
7267 int ARG_UNUSED (flags), bool *no_add_attrs)
7269 if (TREE_CODE (*node) != FUNCTION_DECL)
7271 warning (OPT_Wattributes, "%qE attribute ignored", name);
7272 *no_add_attrs = true;
7273 return NULL_TREE;
7276 TREE_USED (*node) = 1;
7277 DECL_UNINLINABLE (*node) = 1;
7278 if (strcmp ("hsakernel", IDENTIFIER_POINTER (name)) == 0
7279 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (*node))))
7280 == void_type_node))
7281 warning (OPT_Wattributes, "%qE attribute on a function with fixed number "
7282 "of argument makes no sense", name);
7284 return NULL_TREE;
7287 /* Handle a "always_inline" attribute; arguments as in
7288 struct attribute_spec.handler. */
7290 static tree
7291 handle_always_inline_attribute (tree *node, tree name,
7292 tree ARG_UNUSED (args),
7293 int ARG_UNUSED (flags),
7294 bool *no_add_attrs)
7296 if (TREE_CODE (*node) == FUNCTION_DECL)
7298 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7300 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7301 "with %qs attribute", name, "noinline");
7302 *no_add_attrs = true;
7304 else
7305 /* Set the attribute and mark it for disregarding inline
7306 limits. */
7307 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7309 else
7311 warning (OPT_Wattributes, "%qE attribute ignored", name);
7312 *no_add_attrs = true;
7315 return NULL_TREE;
7318 /* Handle a "gnu_inline" attribute; arguments as in
7319 struct attribute_spec.handler. */
7321 static tree
7322 handle_gnu_inline_attribute (tree *node, tree name,
7323 tree ARG_UNUSED (args),
7324 int ARG_UNUSED (flags),
7325 bool *no_add_attrs)
7327 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7329 /* Do nothing else, just set the attribute. We'll get at
7330 it later with lookup_attribute. */
7332 else
7334 warning (OPT_Wattributes, "%qE attribute ignored", name);
7335 *no_add_attrs = true;
7338 return NULL_TREE;
7341 /* Handle a "leaf" attribute; arguments as in
7342 struct attribute_spec.handler. */
7344 static tree
7345 handle_leaf_attribute (tree *node, tree name,
7346 tree ARG_UNUSED (args),
7347 int ARG_UNUSED (flags), bool *no_add_attrs)
7349 if (TREE_CODE (*node) != FUNCTION_DECL)
7351 warning (OPT_Wattributes, "%qE attribute ignored", name);
7352 *no_add_attrs = true;
7354 if (!TREE_PUBLIC (*node))
7356 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7357 *no_add_attrs = true;
7360 return NULL_TREE;
7363 /* Handle an "artificial" attribute; arguments as in
7364 struct attribute_spec.handler. */
7366 static tree
7367 handle_artificial_attribute (tree *node, tree name,
7368 tree ARG_UNUSED (args),
7369 int ARG_UNUSED (flags),
7370 bool *no_add_attrs)
7372 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7374 /* Do nothing else, just set the attribute. We'll get at
7375 it later with lookup_attribute. */
7377 else
7379 warning (OPT_Wattributes, "%qE attribute ignored", name);
7380 *no_add_attrs = true;
7383 return NULL_TREE;
7386 /* Handle a "flatten" attribute; arguments as in
7387 struct attribute_spec.handler. */
7389 static tree
7390 handle_flatten_attribute (tree *node, tree name,
7391 tree args ATTRIBUTE_UNUSED,
7392 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7394 if (TREE_CODE (*node) == FUNCTION_DECL)
7395 /* Do nothing else, just set the attribute. We'll get at
7396 it later with lookup_attribute. */
7398 else
7400 warning (OPT_Wattributes, "%qE attribute ignored", name);
7401 *no_add_attrs = true;
7404 return NULL_TREE;
7407 /* Handle a "warning" or "error" attribute; arguments as in
7408 struct attribute_spec.handler. */
7410 static tree
7411 handle_error_attribute (tree *node, tree name, tree args,
7412 int ARG_UNUSED (flags), bool *no_add_attrs)
7414 if (TREE_CODE (*node) == FUNCTION_DECL
7415 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7416 /* Do nothing else, just set the attribute. We'll get at
7417 it later with lookup_attribute. */
7419 else
7421 warning (OPT_Wattributes, "%qE attribute ignored", name);
7422 *no_add_attrs = true;
7425 return NULL_TREE;
7428 /* Handle a "used" attribute; arguments as in
7429 struct attribute_spec.handler. */
7431 static tree
7432 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7433 int ARG_UNUSED (flags), bool *no_add_attrs)
7435 tree node = *pnode;
7437 if (TREE_CODE (node) == FUNCTION_DECL
7438 || (VAR_P (node) && TREE_STATIC (node))
7439 || (TREE_CODE (node) == TYPE_DECL))
7441 TREE_USED (node) = 1;
7442 DECL_PRESERVE_P (node) = 1;
7443 if (VAR_P (node))
7444 DECL_READ_P (node) = 1;
7446 else
7448 warning (OPT_Wattributes, "%qE attribute ignored", name);
7449 *no_add_attrs = true;
7452 return NULL_TREE;
7455 /* Handle a "unused" attribute; arguments as in
7456 struct attribute_spec.handler. */
7458 static tree
7459 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7460 int flags, bool *no_add_attrs)
7462 if (DECL_P (*node))
7464 tree decl = *node;
7466 if (TREE_CODE (decl) == PARM_DECL
7467 || VAR_OR_FUNCTION_DECL_P (decl)
7468 || TREE_CODE (decl) == LABEL_DECL
7469 || TREE_CODE (decl) == TYPE_DECL)
7471 TREE_USED (decl) = 1;
7472 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7473 DECL_READ_P (decl) = 1;
7475 else
7477 warning (OPT_Wattributes, "%qE attribute ignored", name);
7478 *no_add_attrs = true;
7481 else
7483 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7484 *node = build_variant_type_copy (*node);
7485 TREE_USED (*node) = 1;
7488 return NULL_TREE;
7491 /* Handle a "externally_visible" attribute; arguments as in
7492 struct attribute_spec.handler. */
7494 static tree
7495 handle_externally_visible_attribute (tree *pnode, tree name,
7496 tree ARG_UNUSED (args),
7497 int ARG_UNUSED (flags),
7498 bool *no_add_attrs)
7500 tree node = *pnode;
7502 if (VAR_OR_FUNCTION_DECL_P (node))
7504 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7505 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7507 warning (OPT_Wattributes,
7508 "%qE attribute have effect only on public objects", name);
7509 *no_add_attrs = true;
7512 else
7514 warning (OPT_Wattributes, "%qE attribute ignored", name);
7515 *no_add_attrs = true;
7518 return NULL_TREE;
7521 /* Handle the "no_reorder" attribute. Arguments as in
7522 struct attribute_spec.handler. */
7524 static tree
7525 handle_no_reorder_attribute (tree *pnode,
7526 tree name,
7527 tree,
7528 int,
7529 bool *no_add_attrs)
7531 tree node = *pnode;
7533 if (!VAR_OR_FUNCTION_DECL_P (node)
7534 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7536 warning (OPT_Wattributes,
7537 "%qE attribute only affects top level objects",
7538 name);
7539 *no_add_attrs = true;
7542 return NULL_TREE;
7545 /* Handle a "const" attribute; arguments as in
7546 struct attribute_spec.handler. */
7548 static tree
7549 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7550 int ARG_UNUSED (flags), bool *no_add_attrs)
7552 tree type = TREE_TYPE (*node);
7554 /* See FIXME comment on noreturn in c_common_attribute_table. */
7555 if (TREE_CODE (*node) == FUNCTION_DECL)
7556 TREE_READONLY (*node) = 1;
7557 else if (TREE_CODE (type) == POINTER_TYPE
7558 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7559 TREE_TYPE (*node)
7560 = (build_qualified_type
7561 (build_pointer_type
7562 (build_type_variant (TREE_TYPE (type), 1,
7563 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7564 TYPE_QUALS (type)));
7565 else
7567 warning (OPT_Wattributes, "%qE attribute ignored", name);
7568 *no_add_attrs = true;
7571 return NULL_TREE;
7574 /* Handle a "transparent_union" attribute; arguments as in
7575 struct attribute_spec.handler. */
7577 static tree
7578 handle_transparent_union_attribute (tree *node, tree name,
7579 tree ARG_UNUSED (args), int flags,
7580 bool *no_add_attrs)
7582 tree type;
7584 *no_add_attrs = true;
7587 if (TREE_CODE (*node) == TYPE_DECL
7588 && ! (flags & ATTR_FLAG_CXX11))
7589 node = &TREE_TYPE (*node);
7590 type = *node;
7592 if (TREE_CODE (type) == UNION_TYPE)
7594 /* Make sure that the first field will work for a transparent union.
7595 If the type isn't complete yet, leave the check to the code in
7596 finish_struct. */
7597 if (TYPE_SIZE (type))
7599 tree first = first_field (type);
7600 if (first == NULL_TREE
7601 || DECL_ARTIFICIAL (first)
7602 || TYPE_MODE (type) != DECL_MODE (first))
7603 goto ignored;
7606 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7608 /* If the type isn't complete yet, setting the flag
7609 on a variant wouldn't ever be checked. */
7610 if (!TYPE_SIZE (type))
7611 goto ignored;
7613 /* build_duplicate_type doesn't work for C++. */
7614 if (c_dialect_cxx ())
7615 goto ignored;
7617 /* A type variant isn't good enough, since we don't a cast
7618 to such a type removed as a no-op. */
7619 *node = type = build_duplicate_type (type);
7622 TYPE_TRANSPARENT_AGGR (type) = 1;
7623 return NULL_TREE;
7626 ignored:
7627 warning (OPT_Wattributes, "%qE attribute ignored", name);
7628 return NULL_TREE;
7631 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7632 get the requested priority for a constructor or destructor,
7633 possibly issuing diagnostics for invalid or reserved
7634 priorities. */
7636 static priority_type
7637 get_priority (tree args, bool is_destructor)
7639 HOST_WIDE_INT pri;
7640 tree arg;
7642 if (!args)
7643 return DEFAULT_INIT_PRIORITY;
7645 if (!SUPPORTS_INIT_PRIORITY)
7647 if (is_destructor)
7648 error ("destructor priorities are not supported");
7649 else
7650 error ("constructor priorities are not supported");
7651 return DEFAULT_INIT_PRIORITY;
7654 arg = TREE_VALUE (args);
7655 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7656 goto invalid;
7657 if (arg == error_mark_node)
7658 return DEFAULT_INIT_PRIORITY;
7659 arg = default_conversion (arg);
7660 if (!tree_fits_shwi_p (arg)
7661 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7662 goto invalid;
7664 pri = tree_to_shwi (arg);
7665 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7666 goto invalid;
7668 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7670 if (is_destructor)
7671 warning (0,
7672 "destructor priorities from 0 to %d are reserved "
7673 "for the implementation",
7674 MAX_RESERVED_INIT_PRIORITY);
7675 else
7676 warning (0,
7677 "constructor priorities from 0 to %d are reserved "
7678 "for the implementation",
7679 MAX_RESERVED_INIT_PRIORITY);
7681 return pri;
7683 invalid:
7684 if (is_destructor)
7685 error ("destructor priorities must be integers from 0 to %d inclusive",
7686 MAX_INIT_PRIORITY);
7687 else
7688 error ("constructor priorities must be integers from 0 to %d inclusive",
7689 MAX_INIT_PRIORITY);
7690 return DEFAULT_INIT_PRIORITY;
7693 /* Handle a "constructor" attribute; arguments as in
7694 struct attribute_spec.handler. */
7696 static tree
7697 handle_constructor_attribute (tree *node, tree name, tree args,
7698 int ARG_UNUSED (flags),
7699 bool *no_add_attrs)
7701 tree decl = *node;
7702 tree type = TREE_TYPE (decl);
7704 if (TREE_CODE (decl) == FUNCTION_DECL
7705 && TREE_CODE (type) == FUNCTION_TYPE
7706 && decl_function_context (decl) == 0)
7708 priority_type priority;
7709 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7710 priority = get_priority (args, /*is_destructor=*/false);
7711 SET_DECL_INIT_PRIORITY (decl, priority);
7712 TREE_USED (decl) = 1;
7714 else
7716 warning (OPT_Wattributes, "%qE attribute ignored", name);
7717 *no_add_attrs = true;
7720 return NULL_TREE;
7723 /* Handle a "destructor" attribute; arguments as in
7724 struct attribute_spec.handler. */
7726 static tree
7727 handle_destructor_attribute (tree *node, tree name, tree args,
7728 int ARG_UNUSED (flags),
7729 bool *no_add_attrs)
7731 tree decl = *node;
7732 tree type = TREE_TYPE (decl);
7734 if (TREE_CODE (decl) == FUNCTION_DECL
7735 && TREE_CODE (type) == FUNCTION_TYPE
7736 && decl_function_context (decl) == 0)
7738 priority_type priority;
7739 DECL_STATIC_DESTRUCTOR (decl) = 1;
7740 priority = get_priority (args, /*is_destructor=*/true);
7741 SET_DECL_FINI_PRIORITY (decl, priority);
7742 TREE_USED (decl) = 1;
7744 else
7746 warning (OPT_Wattributes, "%qE attribute ignored", name);
7747 *no_add_attrs = true;
7750 return NULL_TREE;
7753 /* Nonzero if the mode is a valid vector mode for this architecture.
7754 This returns nonzero even if there is no hardware support for the
7755 vector mode, but we can emulate with narrower modes. */
7757 static int
7758 vector_mode_valid_p (machine_mode mode)
7760 enum mode_class mclass = GET_MODE_CLASS (mode);
7761 machine_mode innermode;
7763 /* Doh! What's going on? */
7764 if (mclass != MODE_VECTOR_INT
7765 && mclass != MODE_VECTOR_FLOAT
7766 && mclass != MODE_VECTOR_FRACT
7767 && mclass != MODE_VECTOR_UFRACT
7768 && mclass != MODE_VECTOR_ACCUM
7769 && mclass != MODE_VECTOR_UACCUM)
7770 return 0;
7772 /* Hardware support. Woo hoo! */
7773 if (targetm.vector_mode_supported_p (mode))
7774 return 1;
7776 innermode = GET_MODE_INNER (mode);
7778 /* We should probably return 1 if requesting V4DI and we have no DI,
7779 but we have V2DI, but this is probably very unlikely. */
7781 /* If we have support for the inner mode, we can safely emulate it.
7782 We may not have V2DI, but me can emulate with a pair of DIs. */
7783 return targetm.scalar_mode_supported_p (innermode);
7787 /* Handle a "mode" attribute; arguments as in
7788 struct attribute_spec.handler. */
7790 static tree
7791 handle_mode_attribute (tree *node, tree name, tree args,
7792 int ARG_UNUSED (flags), bool *no_add_attrs)
7794 tree type = *node;
7795 tree ident = TREE_VALUE (args);
7797 *no_add_attrs = true;
7799 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7800 warning (OPT_Wattributes, "%qE attribute ignored", name);
7801 else
7803 int j;
7804 const char *p = IDENTIFIER_POINTER (ident);
7805 int len = strlen (p);
7806 machine_mode mode = VOIDmode;
7807 tree typefm;
7808 bool valid_mode;
7810 if (len > 4 && p[0] == '_' && p[1] == '_'
7811 && p[len - 1] == '_' && p[len - 2] == '_')
7813 char *newp = (char *) alloca (len - 1);
7815 strcpy (newp, &p[2]);
7816 newp[len - 4] = '\0';
7817 p = newp;
7820 /* Change this type to have a type with the specified mode.
7821 First check for the special modes. */
7822 if (!strcmp (p, "byte"))
7823 mode = byte_mode;
7824 else if (!strcmp (p, "word"))
7825 mode = word_mode;
7826 else if (!strcmp (p, "pointer"))
7827 mode = ptr_mode;
7828 else if (!strcmp (p, "libgcc_cmp_return"))
7829 mode = targetm.libgcc_cmp_return_mode ();
7830 else if (!strcmp (p, "libgcc_shift_count"))
7831 mode = targetm.libgcc_shift_count_mode ();
7832 else if (!strcmp (p, "unwind_word"))
7833 mode = targetm.unwind_word_mode ();
7834 else
7835 for (j = 0; j < NUM_MACHINE_MODES; j++)
7836 if (!strcmp (p, GET_MODE_NAME (j)))
7838 mode = (machine_mode) j;
7839 break;
7842 if (mode == VOIDmode)
7844 error ("unknown machine mode %qE", ident);
7845 return NULL_TREE;
7848 valid_mode = false;
7849 switch (GET_MODE_CLASS (mode))
7851 case MODE_INT:
7852 case MODE_PARTIAL_INT:
7853 case MODE_FLOAT:
7854 case MODE_DECIMAL_FLOAT:
7855 case MODE_FRACT:
7856 case MODE_UFRACT:
7857 case MODE_ACCUM:
7858 case MODE_UACCUM:
7859 valid_mode = targetm.scalar_mode_supported_p (mode);
7860 break;
7862 case MODE_COMPLEX_INT:
7863 case MODE_COMPLEX_FLOAT:
7864 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7865 break;
7867 case MODE_VECTOR_INT:
7868 case MODE_VECTOR_FLOAT:
7869 case MODE_VECTOR_FRACT:
7870 case MODE_VECTOR_UFRACT:
7871 case MODE_VECTOR_ACCUM:
7872 case MODE_VECTOR_UACCUM:
7873 warning (OPT_Wattributes, "specifying vector types with "
7874 "__attribute__ ((mode)) is deprecated");
7875 warning (OPT_Wattributes,
7876 "use __attribute__ ((vector_size)) instead");
7877 valid_mode = vector_mode_valid_p (mode);
7878 break;
7880 default:
7881 break;
7883 if (!valid_mode)
7885 error ("unable to emulate %qs", p);
7886 return NULL_TREE;
7889 if (POINTER_TYPE_P (type))
7891 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7892 tree (*fn)(tree, machine_mode, bool);
7894 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7896 error ("invalid pointer mode %qs", p);
7897 return NULL_TREE;
7900 if (TREE_CODE (type) == POINTER_TYPE)
7901 fn = build_pointer_type_for_mode;
7902 else
7903 fn = build_reference_type_for_mode;
7904 typefm = fn (TREE_TYPE (type), mode, false);
7906 else
7908 /* For fixed-point modes, we need to test if the signness of type
7909 and the machine mode are consistent. */
7910 if (ALL_FIXED_POINT_MODE_P (mode)
7911 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7913 error ("signedness of type and machine mode %qs don%'t match", p);
7914 return NULL_TREE;
7916 /* For fixed-point modes, we need to pass saturating info. */
7917 typefm = lang_hooks.types.type_for_mode (mode,
7918 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7919 : TYPE_UNSIGNED (type));
7922 if (typefm == NULL_TREE)
7924 error ("no data type for mode %qs", p);
7925 return NULL_TREE;
7927 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7929 /* For enumeral types, copy the precision from the integer
7930 type returned above. If not an INTEGER_TYPE, we can't use
7931 this mode for this type. */
7932 if (TREE_CODE (typefm) != INTEGER_TYPE)
7934 error ("cannot use mode %qs for enumeral types", p);
7935 return NULL_TREE;
7938 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7940 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7941 typefm = type;
7943 else
7945 /* We cannot build a type variant, as there's code that assumes
7946 that TYPE_MAIN_VARIANT has the same mode. This includes the
7947 debug generators. Instead, create a subrange type. This
7948 results in all of the enumeral values being emitted only once
7949 in the original, and the subtype gets them by reference. */
7950 if (TYPE_UNSIGNED (type))
7951 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7952 else
7953 typefm = make_signed_type (TYPE_PRECISION (typefm));
7954 TREE_TYPE (typefm) = type;
7957 else if (VECTOR_MODE_P (mode)
7958 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7959 : TREE_CODE (type) != TREE_CODE (typefm))
7961 error ("mode %qs applied to inappropriate type", p);
7962 return NULL_TREE;
7965 *node = typefm;
7968 return NULL_TREE;
7971 /* Handle a "section" attribute; arguments as in
7972 struct attribute_spec.handler. */
7974 static tree
7975 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7976 int ARG_UNUSED (flags), bool *no_add_attrs)
7978 tree decl = *node;
7980 if (!targetm_common.have_named_sections)
7982 error_at (DECL_SOURCE_LOCATION (*node),
7983 "section attributes are not supported for this target");
7984 goto fail;
7987 user_defined_section_attribute = true;
7989 if (!VAR_OR_FUNCTION_DECL_P (decl))
7991 error ("section attribute not allowed for %q+D", *node);
7992 goto fail;
7995 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7997 error ("section attribute argument not a string constant");
7998 goto fail;
8001 if (VAR_P (decl)
8002 && current_function_decl != NULL_TREE
8003 && !TREE_STATIC (decl))
8005 error_at (DECL_SOURCE_LOCATION (decl),
8006 "section attribute cannot be specified for local variables");
8007 goto fail;
8010 /* The decl may have already been given a section attribute
8011 from a previous declaration. Ensure they match. */
8012 if (DECL_SECTION_NAME (decl) != NULL
8013 && strcmp (DECL_SECTION_NAME (decl),
8014 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8016 error ("section of %q+D conflicts with previous declaration", *node);
8017 goto fail;
8020 if (VAR_P (decl)
8021 && !targetm.have_tls && targetm.emutls.tmpl_section
8022 && DECL_THREAD_LOCAL_P (decl))
8024 error ("section of %q+D cannot be overridden", *node);
8025 goto fail;
8028 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8029 return NULL_TREE;
8031 fail:
8032 *no_add_attrs = true;
8033 return NULL_TREE;
8036 /* Check whether ALIGN is a valid user-specified alignment. If so,
8037 return its base-2 log; if not, output an error and return -1. If
8038 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8039 no error. */
8041 check_user_alignment (const_tree align, bool allow_zero)
8043 int i;
8045 if (error_operand_p (align))
8046 return -1;
8047 if (TREE_CODE (align) != INTEGER_CST
8048 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
8050 error ("requested alignment is not an integer constant");
8051 return -1;
8053 else if (allow_zero && integer_zerop (align))
8054 return -1;
8055 else if (tree_int_cst_sgn (align) == -1
8056 || (i = tree_log2 (align)) == -1)
8058 error ("requested alignment is not a positive power of 2");
8059 return -1;
8061 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8063 error ("requested alignment is too large");
8064 return -1;
8066 return i;
8070 If in c++-11, check if the c++-11 alignment constraint with respect
8071 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8072 c++-11 mode, does nothing.
8074 [dcl.align]2/ says:
8076 [* if the constant expression evaluates to a fundamental alignment,
8077 the alignment requirement of the declared entity shall be the
8078 specified fundamental alignment.
8080 * if the constant expression evaluates to an extended alignment
8081 and the implementation supports that alignment in the context
8082 of the declaration, the alignment of the declared entity shall
8083 be that alignment
8085 * if the constant expression evaluates to an extended alignment
8086 and the implementation does not support that alignment in the
8087 context of the declaration, the program is ill-formed]. */
8089 static bool
8090 check_cxx_fundamental_alignment_constraints (tree node,
8091 unsigned align_log,
8092 int flags)
8094 bool alignment_too_large_p = false;
8095 unsigned requested_alignment = 1U << align_log;
8096 unsigned max_align = 0;
8098 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8099 || (node == NULL_TREE || node == error_mark_node))
8100 return true;
8102 if (cxx_fundamental_alignment_p (requested_alignment))
8103 return true;
8105 if (DECL_P (node))
8107 if (TREE_STATIC (node))
8109 /* For file scope variables and static members, the target
8110 supports alignments that are at most
8111 MAX_OFILE_ALIGNMENT. */
8112 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8113 alignment_too_large_p = true;
8115 else
8117 #ifdef BIGGEST_FIELD_ALIGNMENT
8118 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8119 #else
8120 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8121 #endif
8122 /* For non-static members, the target supports either
8123 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8124 if it is defined or BIGGEST_ALIGNMENT. */
8125 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8126 if (TREE_CODE (node) == FIELD_DECL
8127 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8128 alignment_too_large_p = true;
8129 #undef MAX_TARGET_FIELD_ALIGNMENT
8130 /* For stack variables, the target supports at most
8131 MAX_STACK_ALIGNMENT. */
8132 else if (decl_function_context (node) != NULL
8133 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8134 alignment_too_large_p = true;
8137 else if (TYPE_P (node))
8139 /* Let's be liberal for types. */
8140 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8141 alignment_too_large_p = true;
8144 if (alignment_too_large_p)
8145 pedwarn (input_location, OPT_Wattributes,
8146 "requested alignment %d is larger than %d",
8147 requested_alignment, max_align);
8149 return !alignment_too_large_p;
8152 /* Handle a "aligned" attribute; arguments as in
8153 struct attribute_spec.handler. */
8155 static tree
8156 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8157 int flags, bool *no_add_attrs)
8159 tree decl = NULL_TREE;
8160 tree *type = NULL;
8161 int is_type = 0;
8162 tree align_expr;
8163 int i;
8165 if (args)
8167 align_expr = TREE_VALUE (args);
8168 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8169 && TREE_CODE (align_expr) != FUNCTION_DECL)
8170 align_expr = default_conversion (align_expr);
8172 else
8173 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8175 if (DECL_P (*node))
8177 decl = *node;
8178 type = &TREE_TYPE (decl);
8179 is_type = TREE_CODE (*node) == TYPE_DECL;
8181 else if (TYPE_P (*node))
8182 type = node, is_type = 1;
8184 if ((i = check_user_alignment (align_expr, false)) == -1
8185 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
8186 *no_add_attrs = true;
8187 else if (is_type)
8189 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8190 /* OK, modify the type in place. */;
8191 /* If we have a TYPE_DECL, then copy the type, so that we
8192 don't accidentally modify a builtin type. See pushdecl. */
8193 else if (decl && TREE_TYPE (decl) != error_mark_node
8194 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
8196 tree tt = TREE_TYPE (decl);
8197 *type = build_variant_type_copy (*type);
8198 DECL_ORIGINAL_TYPE (decl) = tt;
8199 TYPE_NAME (*type) = decl;
8200 TREE_USED (*type) = TREE_USED (decl);
8201 TREE_TYPE (decl) = *type;
8203 else
8204 *type = build_variant_type_copy (*type);
8206 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
8207 TYPE_USER_ALIGN (*type) = 1;
8209 else if (! VAR_OR_FUNCTION_DECL_P (decl)
8210 && TREE_CODE (decl) != FIELD_DECL)
8212 error ("alignment may not be specified for %q+D", decl);
8213 *no_add_attrs = true;
8215 else if (DECL_USER_ALIGN (decl)
8216 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8217 /* C++-11 [dcl.align/4]:
8219 When multiple alignment-specifiers are specified for an
8220 entity, the alignment requirement shall be set to the
8221 strictest specified alignment.
8223 This formally comes from the c++11 specification but we are
8224 doing it for the GNU attribute syntax as well. */
8225 *no_add_attrs = true;
8226 else if (TREE_CODE (decl) == FUNCTION_DECL
8227 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8229 if (DECL_USER_ALIGN (decl))
8230 error ("alignment for %q+D was previously specified as %d "
8231 "and may not be decreased", decl,
8232 DECL_ALIGN (decl) / BITS_PER_UNIT);
8233 else
8234 error ("alignment for %q+D must be at least %d", decl,
8235 DECL_ALIGN (decl) / BITS_PER_UNIT);
8236 *no_add_attrs = true;
8238 else
8240 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
8241 DECL_USER_ALIGN (decl) = 1;
8244 return NULL_TREE;
8247 /* Handle a "weak" attribute; arguments as in
8248 struct attribute_spec.handler. */
8250 static tree
8251 handle_weak_attribute (tree *node, tree name,
8252 tree ARG_UNUSED (args),
8253 int ARG_UNUSED (flags),
8254 bool * ARG_UNUSED (no_add_attrs))
8256 if (TREE_CODE (*node) == FUNCTION_DECL
8257 && DECL_DECLARED_INLINE_P (*node))
8259 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
8260 *no_add_attrs = true;
8262 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8264 error ("indirect function %q+D cannot be declared weak", *node);
8265 *no_add_attrs = true;
8266 return NULL_TREE;
8268 else if (VAR_OR_FUNCTION_DECL_P (*node))
8270 struct symtab_node *n = symtab_node::get (*node);
8271 if (n && n->refuse_visibility_changes)
8272 error ("%+D declared weak after being used", *node);
8273 declare_weak (*node);
8275 else
8276 warning (OPT_Wattributes, "%qE attribute ignored", name);
8278 return NULL_TREE;
8281 /* Handle a "noplt" attribute; arguments as in
8282 struct attribute_spec.handler. */
8284 static tree
8285 handle_noplt_attribute (tree *node, tree name,
8286 tree ARG_UNUSED (args),
8287 int ARG_UNUSED (flags),
8288 bool * ARG_UNUSED (no_add_attrs))
8290 if (TREE_CODE (*node) != FUNCTION_DECL)
8292 warning (OPT_Wattributes,
8293 "%qE attribute is only applicable on functions", name);
8294 *no_add_attrs = true;
8295 return NULL_TREE;
8297 return NULL_TREE;
8300 /* Handle an "alias" or "ifunc" attribute; arguments as in
8301 struct attribute_spec.handler, except that IS_ALIAS tells us
8302 whether this is an alias as opposed to ifunc attribute. */
8304 static tree
8305 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8306 bool *no_add_attrs)
8308 tree decl = *node;
8310 if (TREE_CODE (decl) != FUNCTION_DECL
8311 && (!is_alias || !VAR_P (decl)))
8313 warning (OPT_Wattributes, "%qE attribute ignored", name);
8314 *no_add_attrs = true;
8316 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8317 || (TREE_CODE (decl) != FUNCTION_DECL
8318 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8319 /* A static variable declaration is always a tentative definition,
8320 but the alias is a non-tentative definition which overrides. */
8321 || (TREE_CODE (decl) != FUNCTION_DECL
8322 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8324 error ("%q+D defined both normally and as %qE attribute", decl, name);
8325 *no_add_attrs = true;
8326 return NULL_TREE;
8328 else if (!is_alias
8329 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8330 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8332 error ("weak %q+D cannot be defined %qE", decl, name);
8333 *no_add_attrs = true;
8334 return NULL_TREE;
8337 /* Note that the very first time we process a nested declaration,
8338 decl_function_context will not be set. Indeed, *would* never
8339 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8340 we do below. After such frobbery, pushdecl would set the context.
8341 In any case, this is never what we want. */
8342 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8344 tree id;
8346 id = TREE_VALUE (args);
8347 if (TREE_CODE (id) != STRING_CST)
8349 error ("attribute %qE argument not a string", name);
8350 *no_add_attrs = true;
8351 return NULL_TREE;
8353 id = get_identifier (TREE_STRING_POINTER (id));
8354 /* This counts as a use of the object pointed to. */
8355 TREE_USED (id) = 1;
8357 if (TREE_CODE (decl) == FUNCTION_DECL)
8358 DECL_INITIAL (decl) = error_mark_node;
8359 else
8360 TREE_STATIC (decl) = 1;
8362 if (!is_alias)
8363 /* ifuncs are also aliases, so set that attribute too. */
8364 DECL_ATTRIBUTES (decl)
8365 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8367 else
8369 warning (OPT_Wattributes, "%qE attribute ignored", name);
8370 *no_add_attrs = true;
8373 if (decl_in_symtab_p (*node))
8375 struct symtab_node *n = symtab_node::get (decl);
8376 if (n && n->refuse_visibility_changes)
8378 if (is_alias)
8379 error ("%+D declared alias after being used", decl);
8380 else
8381 error ("%+D declared ifunc after being used", decl);
8386 return NULL_TREE;
8389 /* Handle an "alias" or "ifunc" attribute; arguments as in
8390 struct attribute_spec.handler. */
8392 static tree
8393 handle_ifunc_attribute (tree *node, tree name, tree args,
8394 int ARG_UNUSED (flags), bool *no_add_attrs)
8396 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8399 /* Handle an "alias" or "ifunc" attribute; arguments as in
8400 struct attribute_spec.handler. */
8402 static tree
8403 handle_alias_attribute (tree *node, tree name, tree args,
8404 int ARG_UNUSED (flags), bool *no_add_attrs)
8406 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8409 /* Handle a "weakref" attribute; arguments as in struct
8410 attribute_spec.handler. */
8412 static tree
8413 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8414 int flags, bool *no_add_attrs)
8416 tree attr = NULL_TREE;
8418 /* We must ignore the attribute when it is associated with
8419 local-scoped decls, since attribute alias is ignored and many
8420 such symbols do not even have a DECL_WEAK field. */
8421 if (decl_function_context (*node)
8422 || current_function_decl
8423 || !VAR_OR_FUNCTION_DECL_P (*node))
8425 warning (OPT_Wattributes, "%qE attribute ignored", name);
8426 *no_add_attrs = true;
8427 return NULL_TREE;
8430 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8432 error ("indirect function %q+D cannot be declared weakref", *node);
8433 *no_add_attrs = true;
8434 return NULL_TREE;
8437 /* The idea here is that `weakref("name")' mutates into `weakref,
8438 alias("name")', and weakref without arguments, in turn,
8439 implicitly adds weak. */
8441 if (args)
8443 attr = tree_cons (get_identifier ("alias"), args, attr);
8444 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8446 *no_add_attrs = true;
8448 decl_attributes (node, attr, flags);
8450 else
8452 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8453 error_at (DECL_SOURCE_LOCATION (*node),
8454 "weakref attribute must appear before alias attribute");
8456 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8457 and that isn't supported; and because it wants to add it to
8458 the list of weak decls, which isn't helpful. */
8459 DECL_WEAK (*node) = 1;
8462 if (decl_in_symtab_p (*node))
8464 struct symtab_node *n = symtab_node::get (*node);
8465 if (n && n->refuse_visibility_changes)
8466 error ("%+D declared weakref after being used", *node);
8469 return NULL_TREE;
8472 /* Handle an "visibility" attribute; arguments as in
8473 struct attribute_spec.handler. */
8475 static tree
8476 handle_visibility_attribute (tree *node, tree name, tree args,
8477 int ARG_UNUSED (flags),
8478 bool *ARG_UNUSED (no_add_attrs))
8480 tree decl = *node;
8481 tree id = TREE_VALUE (args);
8482 enum symbol_visibility vis;
8484 if (TYPE_P (*node))
8486 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8487 /* OK */;
8488 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8490 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8491 name);
8492 return NULL_TREE;
8494 else if (TYPE_FIELDS (*node))
8496 error ("%qE attribute ignored because %qT is already defined",
8497 name, *node);
8498 return NULL_TREE;
8501 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8503 warning (OPT_Wattributes, "%qE attribute ignored", name);
8504 return NULL_TREE;
8507 if (TREE_CODE (id) != STRING_CST)
8509 error ("visibility argument not a string");
8510 return NULL_TREE;
8513 /* If this is a type, set the visibility on the type decl. */
8514 if (TYPE_P (decl))
8516 decl = TYPE_NAME (decl);
8517 if (!decl)
8518 return NULL_TREE;
8519 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8521 warning (OPT_Wattributes, "%qE attribute ignored on types",
8522 name);
8523 return NULL_TREE;
8527 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8528 vis = VISIBILITY_DEFAULT;
8529 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8530 vis = VISIBILITY_INTERNAL;
8531 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8532 vis = VISIBILITY_HIDDEN;
8533 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8534 vis = VISIBILITY_PROTECTED;
8535 else
8537 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8538 vis = VISIBILITY_DEFAULT;
8541 if (DECL_VISIBILITY_SPECIFIED (decl)
8542 && vis != DECL_VISIBILITY (decl))
8544 tree attributes = (TYPE_P (*node)
8545 ? TYPE_ATTRIBUTES (*node)
8546 : DECL_ATTRIBUTES (decl));
8547 if (lookup_attribute ("visibility", attributes))
8548 error ("%qD redeclared with different visibility", decl);
8549 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8550 && lookup_attribute ("dllimport", attributes))
8551 error ("%qD was declared %qs which implies default visibility",
8552 decl, "dllimport");
8553 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8554 && lookup_attribute ("dllexport", attributes))
8555 error ("%qD was declared %qs which implies default visibility",
8556 decl, "dllexport");
8559 DECL_VISIBILITY (decl) = vis;
8560 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8562 /* Go ahead and attach the attribute to the node as well. This is needed
8563 so we can determine whether we have VISIBILITY_DEFAULT because the
8564 visibility was not specified, or because it was explicitly overridden
8565 from the containing scope. */
8567 return NULL_TREE;
8570 /* Determine the ELF symbol visibility for DECL, which is either a
8571 variable or a function. It is an error to use this function if a
8572 definition of DECL is not available in this translation unit.
8573 Returns true if the final visibility has been determined by this
8574 function; false if the caller is free to make additional
8575 modifications. */
8577 bool
8578 c_determine_visibility (tree decl)
8580 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8582 /* If the user explicitly specified the visibility with an
8583 attribute, honor that. DECL_VISIBILITY will have been set during
8584 the processing of the attribute. We check for an explicit
8585 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8586 to distinguish the use of an attribute from the use of a "#pragma
8587 GCC visibility push(...)"; in the latter case we still want other
8588 considerations to be able to overrule the #pragma. */
8589 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8590 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8591 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8592 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8593 return true;
8595 /* Set default visibility to whatever the user supplied with
8596 visibility_specified depending on #pragma GCC visibility. */
8597 if (!DECL_VISIBILITY_SPECIFIED (decl))
8599 if (visibility_options.inpragma
8600 || DECL_VISIBILITY (decl) != default_visibility)
8602 DECL_VISIBILITY (decl) = default_visibility;
8603 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8604 /* If visibility changed and DECL already has DECL_RTL, ensure
8605 symbol flags are updated. */
8606 if (((VAR_P (decl) && TREE_STATIC (decl))
8607 || TREE_CODE (decl) == FUNCTION_DECL)
8608 && DECL_RTL_SET_P (decl))
8609 make_decl_rtl (decl);
8612 return false;
8615 /* Handle an "tls_model" attribute; arguments as in
8616 struct attribute_spec.handler. */
8618 static tree
8619 handle_tls_model_attribute (tree *node, tree name, tree args,
8620 int ARG_UNUSED (flags), bool *no_add_attrs)
8622 tree id;
8623 tree decl = *node;
8624 enum tls_model kind;
8626 *no_add_attrs = true;
8628 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8630 warning (OPT_Wattributes, "%qE attribute ignored", name);
8631 return NULL_TREE;
8634 kind = DECL_TLS_MODEL (decl);
8635 id = TREE_VALUE (args);
8636 if (TREE_CODE (id) != STRING_CST)
8638 error ("tls_model argument not a string");
8639 return NULL_TREE;
8642 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8643 kind = TLS_MODEL_LOCAL_EXEC;
8644 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8645 kind = TLS_MODEL_INITIAL_EXEC;
8646 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8647 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8648 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8649 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8650 else
8651 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8653 set_decl_tls_model (decl, kind);
8654 return NULL_TREE;
8657 /* Handle a "no_instrument_function" attribute; arguments as in
8658 struct attribute_spec.handler. */
8660 static tree
8661 handle_no_instrument_function_attribute (tree *node, tree name,
8662 tree ARG_UNUSED (args),
8663 int ARG_UNUSED (flags),
8664 bool *no_add_attrs)
8666 tree decl = *node;
8668 if (TREE_CODE (decl) != FUNCTION_DECL)
8670 error_at (DECL_SOURCE_LOCATION (decl),
8671 "%qE attribute applies only to functions", name);
8672 *no_add_attrs = true;
8674 else
8675 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8677 return NULL_TREE;
8680 /* Handle a "malloc" attribute; arguments as in
8681 struct attribute_spec.handler. */
8683 static tree
8684 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8685 int ARG_UNUSED (flags), bool *no_add_attrs)
8687 if (TREE_CODE (*node) == FUNCTION_DECL
8688 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8689 DECL_IS_MALLOC (*node) = 1;
8690 else
8692 warning (OPT_Wattributes, "%qE attribute ignored", name);
8693 *no_add_attrs = true;
8696 return NULL_TREE;
8699 /* Handle a "alloc_size" attribute; arguments as in
8700 struct attribute_spec.handler. */
8702 static tree
8703 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8704 int ARG_UNUSED (flags), bool *no_add_attrs)
8706 unsigned arg_count = type_num_arguments (*node);
8707 for (; args; args = TREE_CHAIN (args))
8709 tree position = TREE_VALUE (args);
8710 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8711 && TREE_CODE (position) != FUNCTION_DECL)
8712 position = default_conversion (position);
8714 if (!tree_fits_uhwi_p (position)
8715 || !arg_count
8716 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8718 warning (OPT_Wattributes,
8719 "alloc_size parameter outside range");
8720 *no_add_attrs = true;
8721 return NULL_TREE;
8724 return NULL_TREE;
8727 /* Handle a "alloc_align" attribute; arguments as in
8728 struct attribute_spec.handler. */
8730 static tree
8731 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8732 bool *no_add_attrs)
8734 unsigned arg_count = type_num_arguments (*node);
8735 tree position = TREE_VALUE (args);
8736 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8737 position = default_conversion (position);
8739 if (!tree_fits_uhwi_p (position)
8740 || !arg_count
8741 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8743 warning (OPT_Wattributes,
8744 "alloc_align parameter outside range");
8745 *no_add_attrs = true;
8746 return NULL_TREE;
8748 return NULL_TREE;
8751 /* Handle a "assume_aligned" attribute; arguments as in
8752 struct attribute_spec.handler. */
8754 static tree
8755 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8756 bool *no_add_attrs)
8758 for (; args; args = TREE_CHAIN (args))
8760 tree position = TREE_VALUE (args);
8761 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8762 && TREE_CODE (position) != FUNCTION_DECL)
8763 position = default_conversion (position);
8765 if (TREE_CODE (position) != INTEGER_CST)
8767 warning (OPT_Wattributes,
8768 "assume_aligned parameter not integer constant");
8769 *no_add_attrs = true;
8770 return NULL_TREE;
8773 return NULL_TREE;
8776 /* Handle a "fn spec" attribute; arguments as in
8777 struct attribute_spec.handler. */
8779 static tree
8780 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8781 tree args, int ARG_UNUSED (flags),
8782 bool *no_add_attrs ATTRIBUTE_UNUSED)
8784 gcc_assert (args
8785 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8786 && !TREE_CHAIN (args));
8787 return NULL_TREE;
8790 /* Handle a "bnd_variable_size" attribute; arguments as in
8791 struct attribute_spec.handler. */
8793 static tree
8794 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8795 int ARG_UNUSED (flags), bool *no_add_attrs)
8797 if (TREE_CODE (*node) != FIELD_DECL)
8799 warning (OPT_Wattributes, "%qE attribute ignored", name);
8800 *no_add_attrs = true;
8803 return NULL_TREE;
8806 /* Handle a "bnd_legacy" attribute; arguments as in
8807 struct attribute_spec.handler. */
8809 static tree
8810 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8811 int ARG_UNUSED (flags), bool *no_add_attrs)
8813 if (TREE_CODE (*node) != FUNCTION_DECL)
8815 warning (OPT_Wattributes, "%qE attribute ignored", name);
8816 *no_add_attrs = true;
8819 return NULL_TREE;
8822 /* Handle a "bnd_instrument" attribute; arguments as in
8823 struct attribute_spec.handler. */
8825 static tree
8826 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8827 int ARG_UNUSED (flags), bool *no_add_attrs)
8829 if (TREE_CODE (*node) != FUNCTION_DECL)
8831 warning (OPT_Wattributes, "%qE attribute ignored", name);
8832 *no_add_attrs = true;
8835 return NULL_TREE;
8838 /* Handle a "warn_unused" attribute; arguments as in
8839 struct attribute_spec.handler. */
8841 static tree
8842 handle_warn_unused_attribute (tree *node, tree name,
8843 tree args ATTRIBUTE_UNUSED,
8844 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8846 if (TYPE_P (*node))
8847 /* Do nothing else, just set the attribute. We'll get at
8848 it later with lookup_attribute. */
8850 else
8852 warning (OPT_Wattributes, "%qE attribute ignored", name);
8853 *no_add_attrs = true;
8856 return NULL_TREE;
8859 /* Handle an "omp declare simd" attribute; arguments as in
8860 struct attribute_spec.handler. */
8862 static tree
8863 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8865 return NULL_TREE;
8868 /* Handle an "omp declare target" attribute; arguments as in
8869 struct attribute_spec.handler. */
8871 static tree
8872 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8874 return NULL_TREE;
8877 /* Handle a "returns_twice" attribute; arguments as in
8878 struct attribute_spec.handler. */
8880 static tree
8881 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8882 int ARG_UNUSED (flags), bool *no_add_attrs)
8884 if (TREE_CODE (*node) == FUNCTION_DECL)
8885 DECL_IS_RETURNS_TWICE (*node) = 1;
8886 else
8888 warning (OPT_Wattributes, "%qE attribute ignored", name);
8889 *no_add_attrs = true;
8892 return NULL_TREE;
8895 /* Handle a "no_limit_stack" attribute; arguments as in
8896 struct attribute_spec.handler. */
8898 static tree
8899 handle_no_limit_stack_attribute (tree *node, tree name,
8900 tree ARG_UNUSED (args),
8901 int ARG_UNUSED (flags),
8902 bool *no_add_attrs)
8904 tree decl = *node;
8906 if (TREE_CODE (decl) != FUNCTION_DECL)
8908 error_at (DECL_SOURCE_LOCATION (decl),
8909 "%qE attribute applies only to functions", name);
8910 *no_add_attrs = true;
8912 else if (DECL_INITIAL (decl))
8914 error_at (DECL_SOURCE_LOCATION (decl),
8915 "can%'t set %qE attribute after definition", name);
8916 *no_add_attrs = true;
8918 else
8919 DECL_NO_LIMIT_STACK (decl) = 1;
8921 return NULL_TREE;
8924 /* Handle a "pure" attribute; arguments as in
8925 struct attribute_spec.handler. */
8927 static tree
8928 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8929 int ARG_UNUSED (flags), bool *no_add_attrs)
8931 if (TREE_CODE (*node) == FUNCTION_DECL)
8932 DECL_PURE_P (*node) = 1;
8933 /* ??? TODO: Support types. */
8934 else
8936 warning (OPT_Wattributes, "%qE attribute ignored", name);
8937 *no_add_attrs = true;
8940 return NULL_TREE;
8943 /* Digest an attribute list destined for a transactional memory statement.
8944 ALLOWED is the set of attributes that are allowed for this statement;
8945 return the attribute we parsed. Multiple attributes are never allowed. */
8948 parse_tm_stmt_attr (tree attrs, int allowed)
8950 tree a_seen = NULL;
8951 int m_seen = 0;
8953 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8955 tree a = TREE_PURPOSE (attrs);
8956 int m = 0;
8958 if (is_attribute_p ("outer", a))
8959 m = TM_STMT_ATTR_OUTER;
8961 if ((m & allowed) == 0)
8963 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8964 continue;
8967 if (m_seen == 0)
8969 a_seen = a;
8970 m_seen = m;
8972 else if (m_seen == m)
8973 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8974 else
8975 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8978 return m_seen;
8981 /* Transform a TM attribute name into a maskable integer and back.
8982 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8983 to how the lack of an attribute is treated. */
8986 tm_attr_to_mask (tree attr)
8988 if (attr == NULL)
8989 return 0;
8990 if (is_attribute_p ("transaction_safe", attr))
8991 return TM_ATTR_SAFE;
8992 if (is_attribute_p ("transaction_callable", attr))
8993 return TM_ATTR_CALLABLE;
8994 if (is_attribute_p ("transaction_pure", attr))
8995 return TM_ATTR_PURE;
8996 if (is_attribute_p ("transaction_unsafe", attr))
8997 return TM_ATTR_IRREVOCABLE;
8998 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8999 return TM_ATTR_MAY_CANCEL_OUTER;
9000 return 0;
9003 tree
9004 tm_mask_to_attr (int mask)
9006 const char *str;
9007 switch (mask)
9009 case TM_ATTR_SAFE:
9010 str = "transaction_safe";
9011 break;
9012 case TM_ATTR_CALLABLE:
9013 str = "transaction_callable";
9014 break;
9015 case TM_ATTR_PURE:
9016 str = "transaction_pure";
9017 break;
9018 case TM_ATTR_IRREVOCABLE:
9019 str = "transaction_unsafe";
9020 break;
9021 case TM_ATTR_MAY_CANCEL_OUTER:
9022 str = "transaction_may_cancel_outer";
9023 break;
9024 default:
9025 gcc_unreachable ();
9027 return get_identifier (str);
9030 /* Return the first TM attribute seen in LIST. */
9032 tree
9033 find_tm_attribute (tree list)
9035 for (; list ; list = TREE_CHAIN (list))
9037 tree name = TREE_PURPOSE (list);
9038 if (tm_attr_to_mask (name) != 0)
9039 return name;
9041 return NULL_TREE;
9044 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9045 Here we accept only function types, and verify that none of the other
9046 function TM attributes are also applied. */
9047 /* ??? We need to accept class types for C++, but not C. This greatly
9048 complicates this function, since we can no longer rely on the extra
9049 processing given by function_type_required. */
9051 static tree
9052 handle_tm_attribute (tree *node, tree name, tree args,
9053 int flags, bool *no_add_attrs)
9055 /* Only one path adds the attribute; others don't. */
9056 *no_add_attrs = true;
9058 switch (TREE_CODE (*node))
9060 case RECORD_TYPE:
9061 case UNION_TYPE:
9062 /* Only tm_callable and tm_safe apply to classes. */
9063 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9064 goto ignored;
9065 /* FALLTHRU */
9067 case FUNCTION_TYPE:
9068 case METHOD_TYPE:
9070 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9071 if (old_name == name)
9073 else if (old_name != NULL_TREE)
9074 error ("type was previously declared %qE", old_name);
9075 else
9076 *no_add_attrs = false;
9078 break;
9080 case POINTER_TYPE:
9082 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9083 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9085 tree fn_tmp = TREE_TYPE (*node);
9086 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9087 *node = build_pointer_type (fn_tmp);
9088 break;
9091 /* FALLTHRU */
9093 default:
9094 /* If a function is next, pass it on to be tried next. */
9095 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9096 return tree_cons (name, args, NULL);
9098 ignored:
9099 warning (OPT_Wattributes, "%qE attribute ignored", name);
9100 break;
9103 return NULL_TREE;
9106 /* Handle the TM_WRAP attribute; arguments as in
9107 struct attribute_spec.handler. */
9109 static tree
9110 handle_tm_wrap_attribute (tree *node, tree name, tree args,
9111 int ARG_UNUSED (flags), bool *no_add_attrs)
9113 tree decl = *node;
9115 /* We don't need the attribute even on success, since we
9116 record the entry in an external table. */
9117 *no_add_attrs = true;
9119 if (TREE_CODE (decl) != FUNCTION_DECL)
9120 warning (OPT_Wattributes, "%qE attribute ignored", name);
9121 else
9123 tree wrap_decl = TREE_VALUE (args);
9124 if (error_operand_p (wrap_decl))
9126 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
9127 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
9128 error ("%qE argument not an identifier", name);
9129 else
9131 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9132 wrap_decl = lookup_name (wrap_decl);
9133 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9135 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9136 TREE_TYPE (wrap_decl)))
9137 record_tm_replacement (wrap_decl, decl);
9138 else
9139 error ("%qD is not compatible with %qD", wrap_decl, decl);
9141 else
9142 error ("%qE argument is not a function", name);
9146 return NULL_TREE;
9149 /* Ignore the given attribute. Used when this attribute may be usefully
9150 overridden by the target, but is not used generically. */
9152 static tree
9153 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9154 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9155 bool *no_add_attrs)
9157 *no_add_attrs = true;
9158 return NULL_TREE;
9161 /* Handle a "no vops" attribute; arguments as in
9162 struct attribute_spec.handler. */
9164 static tree
9165 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9166 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9167 bool *ARG_UNUSED (no_add_attrs))
9169 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9170 DECL_IS_NOVOPS (*node) = 1;
9171 return NULL_TREE;
9174 /* Handle a "deprecated" attribute; arguments as in
9175 struct attribute_spec.handler. */
9177 static tree
9178 handle_deprecated_attribute (tree *node, tree name,
9179 tree args, int flags,
9180 bool *no_add_attrs)
9182 tree type = NULL_TREE;
9183 int warn = 0;
9184 tree what = NULL_TREE;
9186 if (!args)
9187 *no_add_attrs = true;
9188 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9190 error ("deprecated message is not a string");
9191 *no_add_attrs = true;
9194 if (DECL_P (*node))
9196 tree decl = *node;
9197 type = TREE_TYPE (decl);
9199 if (TREE_CODE (decl) == TYPE_DECL
9200 || TREE_CODE (decl) == PARM_DECL
9201 || VAR_OR_FUNCTION_DECL_P (decl)
9202 || TREE_CODE (decl) == FIELD_DECL
9203 || TREE_CODE (decl) == CONST_DECL
9204 || objc_method_decl (TREE_CODE (decl)))
9205 TREE_DEPRECATED (decl) = 1;
9206 else
9207 warn = 1;
9209 else if (TYPE_P (*node))
9211 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
9212 *node = build_variant_type_copy (*node);
9213 TREE_DEPRECATED (*node) = 1;
9214 type = *node;
9216 else
9217 warn = 1;
9219 if (warn)
9221 *no_add_attrs = true;
9222 if (type && TYPE_NAME (type))
9224 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9225 what = TYPE_NAME (*node);
9226 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9227 && DECL_NAME (TYPE_NAME (type)))
9228 what = DECL_NAME (TYPE_NAME (type));
9230 if (what)
9231 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
9232 else
9233 warning (OPT_Wattributes, "%qE attribute ignored", name);
9236 return NULL_TREE;
9239 /* Handle a "vector_size" attribute; arguments as in
9240 struct attribute_spec.handler. */
9242 static tree
9243 handle_vector_size_attribute (tree *node, tree name, tree args,
9244 int ARG_UNUSED (flags),
9245 bool *no_add_attrs)
9247 unsigned HOST_WIDE_INT vecsize, nunits;
9248 machine_mode orig_mode;
9249 tree type = *node, new_type, size;
9251 *no_add_attrs = true;
9253 size = TREE_VALUE (args);
9254 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9255 && TREE_CODE (size) != FUNCTION_DECL)
9256 size = default_conversion (size);
9258 if (!tree_fits_uhwi_p (size))
9260 warning (OPT_Wattributes, "%qE attribute ignored", name);
9261 return NULL_TREE;
9264 /* Get the vector size (in bytes). */
9265 vecsize = tree_to_uhwi (size);
9267 /* We need to provide for vector pointers, vector arrays, and
9268 functions returning vectors. For example:
9270 __attribute__((vector_size(16))) short *foo;
9272 In this case, the mode is SI, but the type being modified is
9273 HI, so we need to look further. */
9275 while (POINTER_TYPE_P (type)
9276 || TREE_CODE (type) == FUNCTION_TYPE
9277 || TREE_CODE (type) == METHOD_TYPE
9278 || TREE_CODE (type) == ARRAY_TYPE
9279 || TREE_CODE (type) == OFFSET_TYPE)
9280 type = TREE_TYPE (type);
9282 /* Get the mode of the type being modified. */
9283 orig_mode = TYPE_MODE (type);
9285 if ((!INTEGRAL_TYPE_P (type)
9286 && !SCALAR_FLOAT_TYPE_P (type)
9287 && !FIXED_POINT_TYPE_P (type))
9288 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9289 && GET_MODE_CLASS (orig_mode) != MODE_INT
9290 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9291 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9292 || TREE_CODE (type) == BOOLEAN_TYPE)
9294 error ("invalid vector type for attribute %qE", name);
9295 return NULL_TREE;
9298 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9300 error ("vector size not an integral multiple of component size");
9301 return NULL;
9304 if (vecsize == 0)
9306 error ("zero vector size");
9307 return NULL;
9310 /* Calculate how many units fit in the vector. */
9311 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9312 if (nunits & (nunits - 1))
9314 error ("number of components of the vector not a power of two");
9315 return NULL_TREE;
9318 new_type = build_vector_type (type, nunits);
9320 /* Build back pointers if needed. */
9321 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9323 return NULL_TREE;
9326 /* Handle the "nonnull" attribute. */
9327 static tree
9328 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9329 tree args, int ARG_UNUSED (flags),
9330 bool *no_add_attrs)
9332 tree type = *node;
9333 unsigned HOST_WIDE_INT attr_arg_num;
9335 /* If no arguments are specified, all pointer arguments should be
9336 non-null. Verify a full prototype is given so that the arguments
9337 will have the correct types when we actually check them later. */
9338 if (!args)
9340 if (!prototype_p (type))
9342 error ("nonnull attribute without arguments on a non-prototype");
9343 *no_add_attrs = true;
9345 return NULL_TREE;
9348 /* Argument list specified. Verify that each argument number references
9349 a pointer argument. */
9350 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9352 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9354 tree arg = TREE_VALUE (args);
9355 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9356 && TREE_CODE (arg) != FUNCTION_DECL)
9357 arg = default_conversion (arg);
9359 if (!get_nonnull_operand (arg, &arg_num))
9361 error ("nonnull argument has invalid operand number (argument %lu)",
9362 (unsigned long) attr_arg_num);
9363 *no_add_attrs = true;
9364 return NULL_TREE;
9367 if (prototype_p (type))
9369 function_args_iterator iter;
9370 tree argument;
9372 function_args_iter_init (&iter, type);
9373 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9375 argument = function_args_iter_cond (&iter);
9376 if (argument == NULL_TREE || ck_num == arg_num)
9377 break;
9380 if (!argument
9381 || TREE_CODE (argument) == VOID_TYPE)
9383 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9384 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9385 *no_add_attrs = true;
9386 return NULL_TREE;
9389 if (TREE_CODE (argument) != POINTER_TYPE)
9391 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9392 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9393 *no_add_attrs = true;
9394 return NULL_TREE;
9399 return NULL_TREE;
9402 /* Check the argument list of a function call for null in argument slots
9403 that are marked as requiring a non-null pointer argument. The NARGS
9404 arguments are passed in the array ARGARRAY.
9407 static void
9408 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9410 tree a;
9411 int i;
9413 attrs = lookup_attribute ("nonnull", attrs);
9414 if (attrs == NULL_TREE)
9415 return;
9417 a = attrs;
9418 /* See if any of the nonnull attributes has no arguments. If so,
9419 then every pointer argument is checked (in which case the check
9420 for pointer type is done in check_nonnull_arg). */
9421 if (TREE_VALUE (a) != NULL_TREE)
9423 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9424 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9426 if (a != NULL_TREE)
9427 for (i = 0; i < nargs; i++)
9428 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9429 i + 1);
9430 else
9432 /* Walk the argument list. If we encounter an argument number we
9433 should check for non-null, do it. */
9434 for (i = 0; i < nargs; i++)
9436 for (a = attrs; ; a = TREE_CHAIN (a))
9438 a = lookup_attribute ("nonnull", a);
9439 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9440 break;
9443 if (a != NULL_TREE)
9444 check_function_arguments_recurse (check_nonnull_arg, NULL,
9445 argarray[i], i + 1);
9450 /* Check that the Nth argument of a function call (counting backwards
9451 from the end) is a (pointer)0. The NARGS arguments are passed in the
9452 array ARGARRAY. */
9454 static void
9455 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9457 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9459 if (attr)
9461 int len = 0;
9462 int pos = 0;
9463 tree sentinel;
9464 function_args_iterator iter;
9465 tree t;
9467 /* Skip over the named arguments. */
9468 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9470 if (len == nargs)
9471 break;
9472 len++;
9475 if (TREE_VALUE (attr))
9477 tree p = TREE_VALUE (TREE_VALUE (attr));
9478 pos = TREE_INT_CST_LOW (p);
9481 /* The sentinel must be one of the varargs, i.e.
9482 in position >= the number of fixed arguments. */
9483 if ((nargs - 1 - pos) < len)
9485 warning (OPT_Wformat_,
9486 "not enough variable arguments to fit a sentinel");
9487 return;
9490 /* Validate the sentinel. */
9491 sentinel = argarray[nargs - 1 - pos];
9492 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9493 || !integer_zerop (sentinel))
9494 /* Although __null (in C++) is only an integer we allow it
9495 nevertheless, as we are guaranteed that it's exactly
9496 as wide as a pointer, and we don't want to force
9497 users to cast the NULL they have written there.
9498 We warn with -Wstrict-null-sentinel, though. */
9499 && (warn_strict_null_sentinel || null_node != sentinel))
9500 warning (OPT_Wformat_, "missing sentinel in function call");
9504 /* Helper for check_function_nonnull; given a list of operands which
9505 must be non-null in ARGS, determine if operand PARAM_NUM should be
9506 checked. */
9508 static bool
9509 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9511 unsigned HOST_WIDE_INT arg_num = 0;
9513 for (; args; args = TREE_CHAIN (args))
9515 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9517 gcc_assert (found);
9519 if (arg_num == param_num)
9520 return true;
9522 return false;
9525 /* Check that the function argument PARAM (which is operand number
9526 PARAM_NUM) is non-null. This is called by check_function_nonnull
9527 via check_function_arguments_recurse. */
9529 static void
9530 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9531 unsigned HOST_WIDE_INT param_num)
9533 /* Just skip checking the argument if it's not a pointer. This can
9534 happen if the "nonnull" attribute was given without an operand
9535 list (which means to check every pointer argument). */
9537 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9538 return;
9540 if (integer_zerop (param))
9541 warning (OPT_Wnonnull, "null argument where non-null required "
9542 "(argument %lu)", (unsigned long) param_num);
9545 /* Helper for nonnull attribute handling; fetch the operand number
9546 from the attribute argument list. */
9548 static bool
9549 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9551 /* Verify the arg number is a small constant. */
9552 if (tree_fits_uhwi_p (arg_num_expr))
9554 *valp = TREE_INT_CST_LOW (arg_num_expr);
9555 return true;
9557 else
9558 return false;
9561 /* Handle a "nothrow" attribute; arguments as in
9562 struct attribute_spec.handler. */
9564 static tree
9565 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9566 int ARG_UNUSED (flags), bool *no_add_attrs)
9568 if (TREE_CODE (*node) == FUNCTION_DECL)
9569 TREE_NOTHROW (*node) = 1;
9570 /* ??? TODO: Support types. */
9571 else
9573 warning (OPT_Wattributes, "%qE attribute ignored", name);
9574 *no_add_attrs = true;
9577 return NULL_TREE;
9580 /* Handle a "cleanup" attribute; arguments as in
9581 struct attribute_spec.handler. */
9583 static tree
9584 handle_cleanup_attribute (tree *node, tree name, tree args,
9585 int ARG_UNUSED (flags), bool *no_add_attrs)
9587 tree decl = *node;
9588 tree cleanup_id, cleanup_decl;
9590 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9591 for global destructors in C++. This requires infrastructure that
9592 we don't have generically at the moment. It's also not a feature
9593 we'd be missing too much, since we do have attribute constructor. */
9594 if (!VAR_P (decl) || TREE_STATIC (decl))
9596 warning (OPT_Wattributes, "%qE attribute ignored", name);
9597 *no_add_attrs = true;
9598 return NULL_TREE;
9601 /* Verify that the argument is a function in scope. */
9602 /* ??? We could support pointers to functions here as well, if
9603 that was considered desirable. */
9604 cleanup_id = TREE_VALUE (args);
9605 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9607 error ("cleanup argument not an identifier");
9608 *no_add_attrs = true;
9609 return NULL_TREE;
9611 cleanup_decl = lookup_name (cleanup_id);
9612 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9614 error ("cleanup argument not a function");
9615 *no_add_attrs = true;
9616 return NULL_TREE;
9619 /* That the function has proper type is checked with the
9620 eventual call to build_function_call. */
9622 return NULL_TREE;
9625 /* Handle a "warn_unused_result" attribute. No special handling. */
9627 static tree
9628 handle_warn_unused_result_attribute (tree *node, tree name,
9629 tree ARG_UNUSED (args),
9630 int ARG_UNUSED (flags), bool *no_add_attrs)
9632 /* Ignore the attribute for functions not returning any value. */
9633 if (VOID_TYPE_P (TREE_TYPE (*node)))
9635 warning (OPT_Wattributes, "%qE attribute ignored", name);
9636 *no_add_attrs = true;
9639 return NULL_TREE;
9642 /* Handle a "sentinel" attribute. */
9644 static tree
9645 handle_sentinel_attribute (tree *node, tree name, tree args,
9646 int ARG_UNUSED (flags), bool *no_add_attrs)
9648 if (!prototype_p (*node))
9650 warning (OPT_Wattributes,
9651 "%qE attribute requires prototypes with named arguments", name);
9652 *no_add_attrs = true;
9654 else
9656 if (!stdarg_p (*node))
9658 warning (OPT_Wattributes,
9659 "%qE attribute only applies to variadic functions", name);
9660 *no_add_attrs = true;
9664 if (args)
9666 tree position = TREE_VALUE (args);
9667 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9668 && TREE_CODE (position) != FUNCTION_DECL)
9669 position = default_conversion (position);
9671 if (TREE_CODE (position) != INTEGER_CST
9672 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9674 warning (OPT_Wattributes,
9675 "requested position is not an integer constant");
9676 *no_add_attrs = true;
9678 else
9680 if (tree_int_cst_lt (position, integer_zero_node))
9682 warning (OPT_Wattributes,
9683 "requested position is less than zero");
9684 *no_add_attrs = true;
9689 return NULL_TREE;
9692 /* Handle a "type_generic" attribute. */
9694 static tree
9695 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9696 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9697 bool * ARG_UNUSED (no_add_attrs))
9699 /* Ensure we have a function type. */
9700 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9702 /* Ensure we have a variadic function. */
9703 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9705 return NULL_TREE;
9708 /* Handle a "target" attribute. */
9710 static tree
9711 handle_target_attribute (tree *node, tree name, tree args, int flags,
9712 bool *no_add_attrs)
9714 /* Ensure we have a function type. */
9715 if (TREE_CODE (*node) != FUNCTION_DECL)
9717 warning (OPT_Wattributes, "%qE attribute ignored", name);
9718 *no_add_attrs = true;
9720 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9721 flags))
9722 *no_add_attrs = true;
9724 return NULL_TREE;
9727 /* Arguments being collected for optimization. */
9728 typedef const char *const_char_p; /* For DEF_VEC_P. */
9729 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9732 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9733 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9734 false for #pragma GCC optimize. */
9736 bool
9737 parse_optimize_options (tree args, bool attr_p)
9739 bool ret = true;
9740 unsigned opt_argc;
9741 unsigned i;
9742 int saved_flag_strict_aliasing;
9743 const char **opt_argv;
9744 struct cl_decoded_option *decoded_options;
9745 unsigned int decoded_options_count;
9746 tree ap;
9748 /* Build up argv vector. Just in case the string is stored away, use garbage
9749 collected strings. */
9750 vec_safe_truncate (optimize_args, 0);
9751 vec_safe_push (optimize_args, (const char *) NULL);
9753 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9755 tree value = TREE_VALUE (ap);
9757 if (TREE_CODE (value) == INTEGER_CST)
9759 char buffer[20];
9760 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9761 vec_safe_push (optimize_args, ggc_strdup (buffer));
9764 else if (TREE_CODE (value) == STRING_CST)
9766 /* Split string into multiple substrings. */
9767 size_t len = TREE_STRING_LENGTH (value);
9768 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9769 char *end = p + len;
9770 char *comma;
9771 char *next_p = p;
9773 while (next_p != NULL)
9775 size_t len2;
9776 char *q, *r;
9778 p = next_p;
9779 comma = strchr (p, ',');
9780 if (comma)
9782 len2 = comma - p;
9783 *comma = '\0';
9784 next_p = comma+1;
9786 else
9788 len2 = end - p;
9789 next_p = NULL;
9792 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9794 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9795 options. */
9796 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9798 ret = false;
9799 if (attr_p)
9800 warning (OPT_Wattributes,
9801 "bad option %s to optimize attribute", p);
9802 else
9803 warning (OPT_Wpragmas,
9804 "bad option %s to pragma attribute", p);
9805 continue;
9808 if (*p != '-')
9810 *r++ = '-';
9812 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9813 itself is -Os, and any other switch begins with a -f. */
9814 if ((*p >= '0' && *p <= '9')
9815 || (p[0] == 's' && p[1] == '\0'))
9816 *r++ = 'O';
9817 else if (*p != 'O')
9818 *r++ = 'f';
9821 memcpy (r, p, len2);
9822 r[len2] = '\0';
9823 vec_safe_push (optimize_args, (const char *) q);
9829 opt_argc = optimize_args->length ();
9830 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9832 for (i = 1; i < opt_argc; i++)
9833 opt_argv[i] = (*optimize_args)[i];
9835 saved_flag_strict_aliasing = flag_strict_aliasing;
9837 /* Now parse the options. */
9838 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9839 &decoded_options,
9840 &decoded_options_count);
9841 decode_options (&global_options, &global_options_set,
9842 decoded_options, decoded_options_count,
9843 input_location, global_dc);
9845 targetm.override_options_after_change();
9847 /* Don't allow changing -fstrict-aliasing. */
9848 flag_strict_aliasing = saved_flag_strict_aliasing;
9850 optimize_args->truncate (0);
9851 return ret;
9854 /* For handling "optimize" attribute. arguments as in
9855 struct attribute_spec.handler. */
9857 static tree
9858 handle_optimize_attribute (tree *node, tree name, tree args,
9859 int ARG_UNUSED (flags), bool *no_add_attrs)
9861 /* Ensure we have a function type. */
9862 if (TREE_CODE (*node) != FUNCTION_DECL)
9864 warning (OPT_Wattributes, "%qE attribute ignored", name);
9865 *no_add_attrs = true;
9867 else
9869 struct cl_optimization cur_opts;
9870 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9872 /* Save current options. */
9873 cl_optimization_save (&cur_opts, &global_options);
9875 /* If we previously had some optimization options, use them as the
9876 default. */
9877 if (old_opts)
9878 cl_optimization_restore (&global_options,
9879 TREE_OPTIMIZATION (old_opts));
9881 /* Parse options, and update the vector. */
9882 parse_optimize_options (args, true);
9883 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9884 = build_optimization_node (&global_options);
9886 /* Restore current options. */
9887 cl_optimization_restore (&global_options, &cur_opts);
9890 return NULL_TREE;
9893 /* Handle a "no_split_stack" attribute. */
9895 static tree
9896 handle_no_split_stack_attribute (tree *node, tree name,
9897 tree ARG_UNUSED (args),
9898 int ARG_UNUSED (flags),
9899 bool *no_add_attrs)
9901 tree decl = *node;
9903 if (TREE_CODE (decl) != FUNCTION_DECL)
9905 error_at (DECL_SOURCE_LOCATION (decl),
9906 "%qE attribute applies only to functions", name);
9907 *no_add_attrs = true;
9909 else if (DECL_INITIAL (decl))
9911 error_at (DECL_SOURCE_LOCATION (decl),
9912 "can%'t set %qE attribute after definition", name);
9913 *no_add_attrs = true;
9916 return NULL_TREE;
9919 /* Handle a "returns_nonnull" attribute; arguments as in
9920 struct attribute_spec.handler. */
9922 static tree
9923 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9924 bool *no_add_attrs)
9926 // Even without a prototype we still have a return type we can check.
9927 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9929 error ("returns_nonnull attribute on a function not returning a pointer");
9930 *no_add_attrs = true;
9932 return NULL_TREE;
9935 /* Handle a "designated_init" attribute; arguments as in
9936 struct attribute_spec.handler. */
9938 static tree
9939 handle_designated_init_attribute (tree *node, tree name, tree, int,
9940 bool *no_add_attrs)
9942 if (TREE_CODE (*node) != RECORD_TYPE)
9944 error ("%qE attribute is only valid on %<struct%> type", name);
9945 *no_add_attrs = true;
9947 return NULL_TREE;
9951 /* Check for valid arguments being passed to a function with FNTYPE.
9952 There are NARGS arguments in the array ARGARRAY. */
9953 void
9954 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9956 /* Check for null being passed in a pointer argument that must be
9957 non-null. We also need to do this if format checking is enabled. */
9959 if (warn_nonnull)
9960 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9962 /* Check for errors in format strings. */
9964 if (warn_format || warn_suggest_attribute_format)
9965 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9967 if (warn_format)
9968 check_function_sentinel (fntype, nargs, argarray);
9971 /* Generic argument checking recursion routine. PARAM is the argument to
9972 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9973 once the argument is resolved. CTX is context for the callback. */
9974 void
9975 check_function_arguments_recurse (void (*callback)
9976 (void *, tree, unsigned HOST_WIDE_INT),
9977 void *ctx, tree param,
9978 unsigned HOST_WIDE_INT param_num)
9980 if (CONVERT_EXPR_P (param)
9981 && (TYPE_PRECISION (TREE_TYPE (param))
9982 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9984 /* Strip coercion. */
9985 check_function_arguments_recurse (callback, ctx,
9986 TREE_OPERAND (param, 0), param_num);
9987 return;
9990 if (TREE_CODE (param) == CALL_EXPR)
9992 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9993 tree attrs;
9994 bool found_format_arg = false;
9996 /* See if this is a call to a known internationalization function
9997 that modifies a format arg. Such a function may have multiple
9998 format_arg attributes (for example, ngettext). */
10000 for (attrs = TYPE_ATTRIBUTES (type);
10001 attrs;
10002 attrs = TREE_CHAIN (attrs))
10003 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10005 tree inner_arg;
10006 tree format_num_expr;
10007 int format_num;
10008 int i;
10009 call_expr_arg_iterator iter;
10011 /* Extract the argument number, which was previously checked
10012 to be valid. */
10013 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
10015 format_num = tree_to_uhwi (format_num_expr);
10017 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10018 inner_arg != 0;
10019 inner_arg = next_call_expr_arg (&iter), i++)
10020 if (i == format_num)
10022 check_function_arguments_recurse (callback, ctx,
10023 inner_arg, param_num);
10024 found_format_arg = true;
10025 break;
10029 /* If we found a format_arg attribute and did a recursive check,
10030 we are done with checking this argument. Otherwise, we continue
10031 and this will be considered a non-literal. */
10032 if (found_format_arg)
10033 return;
10036 if (TREE_CODE (param) == COND_EXPR)
10038 /* Check both halves of the conditional expression. */
10039 check_function_arguments_recurse (callback, ctx,
10040 TREE_OPERAND (param, 1), param_num);
10041 check_function_arguments_recurse (callback, ctx,
10042 TREE_OPERAND (param, 2), param_num);
10043 return;
10046 (*callback) (ctx, param, param_num);
10049 /* Checks for a builtin function FNDECL that the number of arguments
10050 NARGS against the required number REQUIRED and issues an error if
10051 there is a mismatch. Returns true if the number of arguments is
10052 correct, otherwise false. */
10054 static bool
10055 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
10057 if (nargs < required)
10059 error_at (input_location,
10060 "not enough arguments to function %qE", fndecl);
10061 return false;
10063 else if (nargs > required)
10065 error_at (input_location,
10066 "too many arguments to function %qE", fndecl);
10067 return false;
10069 return true;
10072 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10073 Returns false if there was an error, otherwise true. */
10075 bool
10076 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10078 if (!DECL_BUILT_IN (fndecl)
10079 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10080 return true;
10082 switch (DECL_FUNCTION_CODE (fndecl))
10084 case BUILT_IN_CONSTANT_P:
10085 return builtin_function_validate_nargs (fndecl, nargs, 1);
10087 case BUILT_IN_ISFINITE:
10088 case BUILT_IN_ISINF:
10089 case BUILT_IN_ISINF_SIGN:
10090 case BUILT_IN_ISNAN:
10091 case BUILT_IN_ISNORMAL:
10092 if (builtin_function_validate_nargs (fndecl, nargs, 1))
10094 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10096 error ("non-floating-point argument in call to "
10097 "function %qE", fndecl);
10098 return false;
10100 return true;
10102 return false;
10104 case BUILT_IN_ISGREATER:
10105 case BUILT_IN_ISGREATEREQUAL:
10106 case BUILT_IN_ISLESS:
10107 case BUILT_IN_ISLESSEQUAL:
10108 case BUILT_IN_ISLESSGREATER:
10109 case BUILT_IN_ISUNORDERED:
10110 if (builtin_function_validate_nargs (fndecl, nargs, 2))
10112 enum tree_code code0, code1;
10113 code0 = TREE_CODE (TREE_TYPE (args[0]));
10114 code1 = TREE_CODE (TREE_TYPE (args[1]));
10115 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10116 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10117 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10119 error ("non-floating-point arguments in call to "
10120 "function %qE", fndecl);
10121 return false;
10123 return true;
10125 return false;
10127 case BUILT_IN_FPCLASSIFY:
10128 if (builtin_function_validate_nargs (fndecl, nargs, 6))
10130 unsigned i;
10132 for (i=0; i<5; i++)
10133 if (TREE_CODE (args[i]) != INTEGER_CST)
10135 error ("non-const integer argument %u in call to function %qE",
10136 i+1, fndecl);
10137 return false;
10140 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10142 error ("non-floating-point argument in call to function %qE",
10143 fndecl);
10144 return false;
10146 return true;
10148 return false;
10150 case BUILT_IN_ASSUME_ALIGNED:
10151 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10153 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10155 error ("non-integer argument 3 in call to function %qE", fndecl);
10156 return false;
10158 return true;
10160 return false;
10162 case BUILT_IN_ADD_OVERFLOW:
10163 case BUILT_IN_SUB_OVERFLOW:
10164 case BUILT_IN_MUL_OVERFLOW:
10165 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10167 unsigned i;
10168 for (i = 0; i < 2; i++)
10169 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10171 error ("argument %u in call to function %qE does not have "
10172 "integral type", i + 1, fndecl);
10173 return false;
10175 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10176 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10178 error ("argument 3 in call to function %qE does not have "
10179 "pointer to integer type", fndecl);
10180 return false;
10182 return true;
10184 return false;
10186 default:
10187 return true;
10191 /* Function to help qsort sort FIELD_DECLs by name order. */
10194 field_decl_cmp (const void *x_p, const void *y_p)
10196 const tree *const x = (const tree *const) x_p;
10197 const tree *const y = (const tree *const) y_p;
10199 if (DECL_NAME (*x) == DECL_NAME (*y))
10200 /* A nontype is "greater" than a type. */
10201 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10202 if (DECL_NAME (*x) == NULL_TREE)
10203 return -1;
10204 if (DECL_NAME (*y) == NULL_TREE)
10205 return 1;
10206 if (DECL_NAME (*x) < DECL_NAME (*y))
10207 return -1;
10208 return 1;
10211 static struct {
10212 gt_pointer_operator new_value;
10213 void *cookie;
10214 } resort_data;
10216 /* This routine compares two fields like field_decl_cmp but using the
10217 pointer operator in resort_data. */
10219 static int
10220 resort_field_decl_cmp (const void *x_p, const void *y_p)
10222 const tree *const x = (const tree *const) x_p;
10223 const tree *const y = (const tree *const) y_p;
10225 if (DECL_NAME (*x) == DECL_NAME (*y))
10226 /* A nontype is "greater" than a type. */
10227 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10228 if (DECL_NAME (*x) == NULL_TREE)
10229 return -1;
10230 if (DECL_NAME (*y) == NULL_TREE)
10231 return 1;
10233 tree d1 = DECL_NAME (*x);
10234 tree d2 = DECL_NAME (*y);
10235 resort_data.new_value (&d1, resort_data.cookie);
10236 resort_data.new_value (&d2, resort_data.cookie);
10237 if (d1 < d2)
10238 return -1;
10240 return 1;
10243 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10245 void
10246 resort_sorted_fields (void *obj,
10247 void * ARG_UNUSED (orig_obj),
10248 gt_pointer_operator new_value,
10249 void *cookie)
10251 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10252 resort_data.new_value = new_value;
10253 resort_data.cookie = cookie;
10254 qsort (&sf->elts[0], sf->len, sizeof (tree),
10255 resort_field_decl_cmp);
10258 /* Subroutine of c_parse_error.
10259 Return the result of concatenating LHS and RHS. RHS is really
10260 a string literal, its first character is indicated by RHS_START and
10261 RHS_SIZE is its length (including the terminating NUL character).
10263 The caller is responsible for deleting the returned pointer. */
10265 static char *
10266 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10268 const int lhs_size = strlen (lhs);
10269 char *result = XNEWVEC (char, lhs_size + rhs_size);
10270 strncpy (result, lhs, lhs_size);
10271 strncpy (result + lhs_size, rhs_start, rhs_size);
10272 return result;
10275 /* Issue the error given by GMSGID, indicating that it occurred before
10276 TOKEN, which had the associated VALUE. */
10278 void
10279 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10280 tree value, unsigned char token_flags)
10282 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10284 char *message = NULL;
10286 if (token_type == CPP_EOF)
10287 message = catenate_messages (gmsgid, " at end of input");
10288 else if (token_type == CPP_CHAR
10289 || token_type == CPP_WCHAR
10290 || token_type == CPP_CHAR16
10291 || token_type == CPP_CHAR32
10292 || token_type == CPP_UTF8CHAR)
10294 unsigned int val = TREE_INT_CST_LOW (value);
10295 const char *prefix;
10297 switch (token_type)
10299 default:
10300 prefix = "";
10301 break;
10302 case CPP_WCHAR:
10303 prefix = "L";
10304 break;
10305 case CPP_CHAR16:
10306 prefix = "u";
10307 break;
10308 case CPP_CHAR32:
10309 prefix = "U";
10310 break;
10311 case CPP_UTF8CHAR:
10312 prefix = "u8";
10313 break;
10316 if (val <= UCHAR_MAX && ISGRAPH (val))
10317 message = catenate_messages (gmsgid, " before %s'%c'");
10318 else
10319 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10321 error (message, prefix, val);
10322 free (message);
10323 message = NULL;
10325 else if (token_type == CPP_CHAR_USERDEF
10326 || token_type == CPP_WCHAR_USERDEF
10327 || token_type == CPP_CHAR16_USERDEF
10328 || token_type == CPP_CHAR32_USERDEF
10329 || token_type == CPP_UTF8CHAR_USERDEF)
10330 message = catenate_messages (gmsgid,
10331 " before user-defined character literal");
10332 else if (token_type == CPP_STRING_USERDEF
10333 || token_type == CPP_WSTRING_USERDEF
10334 || token_type == CPP_STRING16_USERDEF
10335 || token_type == CPP_STRING32_USERDEF
10336 || token_type == CPP_UTF8STRING_USERDEF)
10337 message = catenate_messages (gmsgid, " before user-defined string literal");
10338 else if (token_type == CPP_STRING
10339 || token_type == CPP_WSTRING
10340 || token_type == CPP_STRING16
10341 || token_type == CPP_STRING32
10342 || token_type == CPP_UTF8STRING)
10343 message = catenate_messages (gmsgid, " before string constant");
10344 else if (token_type == CPP_NUMBER)
10345 message = catenate_messages (gmsgid, " before numeric constant");
10346 else if (token_type == CPP_NAME)
10348 message = catenate_messages (gmsgid, " before %qE");
10349 error (message, value);
10350 free (message);
10351 message = NULL;
10353 else if (token_type == CPP_PRAGMA)
10354 message = catenate_messages (gmsgid, " before %<#pragma%>");
10355 else if (token_type == CPP_PRAGMA_EOL)
10356 message = catenate_messages (gmsgid, " before end of line");
10357 else if (token_type == CPP_DECLTYPE)
10358 message = catenate_messages (gmsgid, " before %<decltype%>");
10359 else if (token_type < N_TTYPES)
10361 message = catenate_messages (gmsgid, " before %qs token");
10362 error (message, cpp_type2name (token_type, token_flags));
10363 free (message);
10364 message = NULL;
10366 else
10367 error (gmsgid);
10369 if (message)
10371 error (message);
10372 free (message);
10374 #undef catenate_messages
10377 /* Return the gcc option code associated with the reason for a cpp
10378 message, or 0 if none. */
10380 static int
10381 c_option_controlling_cpp_error (int reason)
10383 const struct cpp_reason_option_codes_t *entry;
10385 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10387 if (entry->reason == reason)
10388 return entry->option_code;
10390 return 0;
10393 /* Callback from cpp_error for PFILE to print diagnostics from the
10394 preprocessor. The diagnostic is of type LEVEL, with REASON set
10395 to the reason code if LEVEL is represents a warning, at location
10396 LOCATION unless this is after lexing and the compiler's location
10397 should be used instead, with column number possibly overridden by
10398 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10399 the arguments. Returns true if a diagnostic was emitted, false
10400 otherwise. */
10402 bool
10403 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10404 location_t location, unsigned int column_override,
10405 const char *msg, va_list *ap)
10407 diagnostic_info diagnostic;
10408 diagnostic_t dlevel;
10409 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10410 bool ret;
10412 switch (level)
10414 case CPP_DL_WARNING_SYSHDR:
10415 if (flag_no_output)
10416 return false;
10417 global_dc->dc_warn_system_headers = 1;
10418 /* Fall through. */
10419 case CPP_DL_WARNING:
10420 if (flag_no_output)
10421 return false;
10422 dlevel = DK_WARNING;
10423 break;
10424 case CPP_DL_PEDWARN:
10425 if (flag_no_output && !flag_pedantic_errors)
10426 return false;
10427 dlevel = DK_PEDWARN;
10428 break;
10429 case CPP_DL_ERROR:
10430 dlevel = DK_ERROR;
10431 break;
10432 case CPP_DL_ICE:
10433 dlevel = DK_ICE;
10434 break;
10435 case CPP_DL_NOTE:
10436 dlevel = DK_NOTE;
10437 break;
10438 case CPP_DL_FATAL:
10439 dlevel = DK_FATAL;
10440 break;
10441 default:
10442 gcc_unreachable ();
10444 if (done_lexing)
10445 location = input_location;
10446 diagnostic_set_info_translated (&diagnostic, msg, ap,
10447 location, dlevel);
10448 if (column_override)
10449 diagnostic_override_column (&diagnostic, column_override);
10450 diagnostic_override_option_index (&diagnostic,
10451 c_option_controlling_cpp_error (reason));
10452 ret = report_diagnostic (&diagnostic);
10453 if (level == CPP_DL_WARNING_SYSHDR)
10454 global_dc->dc_warn_system_headers = save_warn_system_headers;
10455 return ret;
10458 /* Convert a character from the host to the target execution character
10459 set. cpplib handles this, mostly. */
10461 HOST_WIDE_INT
10462 c_common_to_target_charset (HOST_WIDE_INT c)
10464 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10465 zero-extended under -fno-signed-char. cpplib insists that characters
10466 and character constants are always unsigned. Hence we must convert
10467 back and forth. */
10468 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10470 uc = cpp_host_to_exec_charset (parse_in, uc);
10472 if (flag_signed_char)
10473 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10474 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10475 else
10476 return uc;
10479 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10480 references with an INDIRECT_REF of a constant at the bottom; much like the
10481 traditional rendering of offsetof as a macro. Return the folded result. */
10483 tree
10484 fold_offsetof_1 (tree expr)
10486 tree base, off, t;
10488 switch (TREE_CODE (expr))
10490 case ERROR_MARK:
10491 return expr;
10493 case VAR_DECL:
10494 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10495 return error_mark_node;
10497 case CALL_EXPR:
10498 case TARGET_EXPR:
10499 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10500 return error_mark_node;
10502 case NOP_EXPR:
10503 case INDIRECT_REF:
10504 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10506 error ("cannot apply %<offsetof%> to a non constant address");
10507 return error_mark_node;
10509 return TREE_OPERAND (expr, 0);
10511 case COMPONENT_REF:
10512 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10513 if (base == error_mark_node)
10514 return base;
10516 t = TREE_OPERAND (expr, 1);
10517 if (DECL_C_BIT_FIELD (t))
10519 error ("attempt to take address of bit-field structure "
10520 "member %qD", t);
10521 return error_mark_node;
10523 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10524 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10525 / BITS_PER_UNIT));
10526 break;
10528 case ARRAY_REF:
10529 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10530 if (base == error_mark_node)
10531 return base;
10533 t = TREE_OPERAND (expr, 1);
10535 /* Check if the offset goes beyond the upper bound of the array. */
10536 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10538 tree upbound = array_ref_up_bound (expr);
10539 if (upbound != NULL_TREE
10540 && TREE_CODE (upbound) == INTEGER_CST
10541 && !tree_int_cst_equal (upbound,
10542 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10544 upbound = size_binop (PLUS_EXPR, upbound,
10545 build_int_cst (TREE_TYPE (upbound), 1));
10546 if (tree_int_cst_lt (upbound, t))
10548 tree v;
10550 for (v = TREE_OPERAND (expr, 0);
10551 TREE_CODE (v) == COMPONENT_REF;
10552 v = TREE_OPERAND (v, 0))
10553 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10554 == RECORD_TYPE)
10556 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10557 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10558 if (TREE_CODE (fld_chain) == FIELD_DECL)
10559 break;
10561 if (fld_chain)
10562 break;
10564 /* Don't warn if the array might be considered a poor
10565 man's flexible array member with a very permissive
10566 definition thereof. */
10567 if (TREE_CODE (v) == ARRAY_REF
10568 || TREE_CODE (v) == COMPONENT_REF)
10569 warning (OPT_Warray_bounds,
10570 "index %E denotes an offset "
10571 "greater than size of %qT",
10572 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10577 t = convert (sizetype, t);
10578 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10579 break;
10581 case COMPOUND_EXPR:
10582 /* Handle static members of volatile structs. */
10583 t = TREE_OPERAND (expr, 1);
10584 gcc_assert (VAR_P (t));
10585 return fold_offsetof_1 (t);
10587 default:
10588 gcc_unreachable ();
10591 return fold_build_pointer_plus (base, off);
10594 /* Likewise, but convert it to the return type of offsetof. */
10596 tree
10597 fold_offsetof (tree expr)
10599 return convert (size_type_node, fold_offsetof_1 (expr));
10602 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10603 expression, because B will always be true. */
10605 void
10606 warn_for_omitted_condop (location_t location, tree cond)
10608 if (truth_value_p (TREE_CODE (cond)))
10609 warning_at (location, OPT_Wparentheses,
10610 "the omitted middle operand in ?: will always be %<true%>, "
10611 "suggest explicit middle operand");
10614 /* Give an error for storing into ARG, which is 'const'. USE indicates
10615 how ARG was being used. */
10617 void
10618 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10620 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10621 || use == lv_asm);
10622 /* Using this macro rather than (for example) arrays of messages
10623 ensures that all the format strings are checked at compile
10624 time. */
10625 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10626 : (use == lv_increment ? (I) \
10627 : (use == lv_decrement ? (D) : (AS))))
10628 if (TREE_CODE (arg) == COMPONENT_REF)
10630 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10631 error_at (loc, READONLY_MSG (G_("assignment of member "
10632 "%qD in read-only object"),
10633 G_("increment of member "
10634 "%qD in read-only object"),
10635 G_("decrement of member "
10636 "%qD in read-only object"),
10637 G_("member %qD in read-only object "
10638 "used as %<asm%> output")),
10639 TREE_OPERAND (arg, 1));
10640 else
10641 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10642 G_("increment of read-only member %qD"),
10643 G_("decrement of read-only member %qD"),
10644 G_("read-only member %qD used as %<asm%> output")),
10645 TREE_OPERAND (arg, 1));
10647 else if (VAR_P (arg))
10648 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10649 G_("increment of read-only variable %qD"),
10650 G_("decrement of read-only variable %qD"),
10651 G_("read-only variable %qD used as %<asm%> output")),
10652 arg);
10653 else if (TREE_CODE (arg) == PARM_DECL)
10654 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10655 G_("increment of read-only parameter %qD"),
10656 G_("decrement of read-only parameter %qD"),
10657 G_("read-only parameter %qD use as %<asm%> output")),
10658 arg);
10659 else if (TREE_CODE (arg) == RESULT_DECL)
10661 gcc_assert (c_dialect_cxx ());
10662 error_at (loc, READONLY_MSG (G_("assignment of "
10663 "read-only named return value %qD"),
10664 G_("increment of "
10665 "read-only named return value %qD"),
10666 G_("decrement of "
10667 "read-only named return value %qD"),
10668 G_("read-only named return value %qD "
10669 "used as %<asm%>output")),
10670 arg);
10672 else if (TREE_CODE (arg) == FUNCTION_DECL)
10673 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10674 G_("increment of function %qD"),
10675 G_("decrement of function %qD"),
10676 G_("function %qD used as %<asm%> output")),
10677 arg);
10678 else
10679 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10680 G_("increment of read-only location %qE"),
10681 G_("decrement of read-only location %qE"),
10682 G_("read-only location %qE used as %<asm%> output")),
10683 arg);
10686 /* Print an error message for an invalid lvalue. USE says
10687 how the lvalue is being used and so selects the error message. LOC
10688 is the location for the error. */
10690 void
10691 lvalue_error (location_t loc, enum lvalue_use use)
10693 switch (use)
10695 case lv_assign:
10696 error_at (loc, "lvalue required as left operand of assignment");
10697 break;
10698 case lv_increment:
10699 error_at (loc, "lvalue required as increment operand");
10700 break;
10701 case lv_decrement:
10702 error_at (loc, "lvalue required as decrement operand");
10703 break;
10704 case lv_addressof:
10705 error_at (loc, "lvalue required as unary %<&%> operand");
10706 break;
10707 case lv_asm:
10708 error_at (loc, "lvalue required in asm statement");
10709 break;
10710 default:
10711 gcc_unreachable ();
10715 /* Print an error message for an invalid indirection of type TYPE.
10716 ERRSTRING is the name of the operator for the indirection. */
10718 void
10719 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10721 switch (errstring)
10723 case RO_NULL:
10724 gcc_assert (c_dialect_cxx ());
10725 error_at (loc, "invalid type argument (have %qT)", type);
10726 break;
10727 case RO_ARRAY_INDEXING:
10728 error_at (loc,
10729 "invalid type argument of array indexing (have %qT)",
10730 type);
10731 break;
10732 case RO_UNARY_STAR:
10733 error_at (loc,
10734 "invalid type argument of unary %<*%> (have %qT)",
10735 type);
10736 break;
10737 case RO_ARROW:
10738 error_at (loc,
10739 "invalid type argument of %<->%> (have %qT)",
10740 type);
10741 break;
10742 case RO_ARROW_STAR:
10743 error_at (loc,
10744 "invalid type argument of %<->*%> (have %qT)",
10745 type);
10746 break;
10747 case RO_IMPLICIT_CONVERSION:
10748 error_at (loc,
10749 "invalid type argument of implicit conversion (have %qT)",
10750 type);
10751 break;
10752 default:
10753 gcc_unreachable ();
10757 /* *PTYPE is an incomplete array. Complete it with a domain based on
10758 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10759 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10760 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10763 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10765 tree maxindex, type, main_type, elt, unqual_elt;
10766 int failure = 0, quals;
10767 hashval_t hashcode = 0;
10768 bool overflow_p = false;
10770 maxindex = size_zero_node;
10771 if (initial_value)
10773 if (TREE_CODE (initial_value) == STRING_CST)
10775 int eltsize
10776 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10777 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10779 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10781 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10783 if (vec_safe_is_empty (v))
10785 if (pedantic)
10786 failure = 3;
10787 maxindex = ssize_int (-1);
10789 else
10791 tree curindex;
10792 unsigned HOST_WIDE_INT cnt;
10793 constructor_elt *ce;
10794 bool fold_p = false;
10796 if ((*v)[0].index)
10797 maxindex = (*v)[0].index, fold_p = true;
10799 curindex = maxindex;
10801 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10803 bool curfold_p = false;
10804 if (ce->index)
10805 curindex = ce->index, curfold_p = true;
10806 else
10808 if (fold_p)
10810 /* Since we treat size types now as ordinary
10811 unsigned types, we need an explicit overflow
10812 check. */
10813 tree orig = curindex;
10814 curindex = fold_convert (sizetype, curindex);
10815 overflow_p |= tree_int_cst_lt (curindex, orig);
10817 curindex = size_binop (PLUS_EXPR, curindex,
10818 size_one_node);
10820 if (tree_int_cst_lt (maxindex, curindex))
10821 maxindex = curindex, fold_p = curfold_p;
10823 if (fold_p)
10825 tree orig = maxindex;
10826 maxindex = fold_convert (sizetype, maxindex);
10827 overflow_p |= tree_int_cst_lt (maxindex, orig);
10831 else
10833 /* Make an error message unless that happened already. */
10834 if (initial_value != error_mark_node)
10835 failure = 1;
10838 else
10840 failure = 2;
10841 if (!do_default)
10842 return failure;
10845 type = *ptype;
10846 elt = TREE_TYPE (type);
10847 quals = TYPE_QUALS (strip_array_types (elt));
10848 if (quals == 0)
10849 unqual_elt = elt;
10850 else
10851 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10853 /* Using build_distinct_type_copy and modifying things afterward instead
10854 of using build_array_type to create a new type preserves all of the
10855 TYPE_LANG_FLAG_? bits that the front end may have set. */
10856 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10857 TREE_TYPE (main_type) = unqual_elt;
10858 TYPE_DOMAIN (main_type)
10859 = build_range_type (TREE_TYPE (maxindex),
10860 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10861 layout_type (main_type);
10863 /* Make sure we have the canonical MAIN_TYPE. */
10864 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10865 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10866 hashcode);
10867 main_type = type_hash_canon (hashcode, main_type);
10869 /* Fix the canonical type. */
10870 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10871 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10872 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10873 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10874 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10875 != TYPE_DOMAIN (main_type)))
10876 TYPE_CANONICAL (main_type)
10877 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10878 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10879 else
10880 TYPE_CANONICAL (main_type) = main_type;
10882 if (quals == 0)
10883 type = main_type;
10884 else
10885 type = c_build_qualified_type (main_type, quals);
10887 if (COMPLETE_TYPE_P (type)
10888 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10889 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10891 error ("size of array is too large");
10892 /* If we proceed with the array type as it is, we'll eventually
10893 crash in tree_to_[su]hwi(). */
10894 type = error_mark_node;
10897 *ptype = type;
10898 return failure;
10901 /* Like c_mark_addressable but don't check register qualifier. */
10902 void
10903 c_common_mark_addressable_vec (tree t)
10905 while (handled_component_p (t))
10906 t = TREE_OPERAND (t, 0);
10907 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10908 return;
10909 TREE_ADDRESSABLE (t) = 1;
10914 /* Used to help initialize the builtin-types.def table. When a type of
10915 the correct size doesn't exist, use error_mark_node instead of NULL.
10916 The later results in segfaults even when a decl using the type doesn't
10917 get invoked. */
10919 tree
10920 builtin_type_for_size (int size, bool unsignedp)
10922 tree type = c_common_type_for_size (size, unsignedp);
10923 return type ? type : error_mark_node;
10926 /* A helper function for resolve_overloaded_builtin in resolving the
10927 overloaded __sync_ builtins. Returns a positive power of 2 if the
10928 first operand of PARAMS is a pointer to a supported data type.
10929 Returns 0 if an error is encountered. */
10931 static int
10932 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10934 tree type;
10935 int size;
10937 if (!params)
10939 error ("too few arguments to function %qE", function);
10940 return 0;
10943 type = TREE_TYPE ((*params)[0]);
10944 if (TREE_CODE (type) == ARRAY_TYPE)
10946 /* Force array-to-pointer decay for C++. */
10947 gcc_assert (c_dialect_cxx());
10948 (*params)[0] = default_conversion ((*params)[0]);
10949 type = TREE_TYPE ((*params)[0]);
10951 if (TREE_CODE (type) != POINTER_TYPE)
10952 goto incompatible;
10954 type = TREE_TYPE (type);
10955 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10956 goto incompatible;
10958 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10959 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10960 return size;
10962 incompatible:
10963 error ("incompatible type for argument %d of %qE", 1, function);
10964 return 0;
10967 /* A helper function for resolve_overloaded_builtin. Adds casts to
10968 PARAMS to make arguments match up with those of FUNCTION. Drops
10969 the variadic arguments at the end. Returns false if some error
10970 was encountered; true on success. */
10972 static bool
10973 sync_resolve_params (location_t loc, tree orig_function, tree function,
10974 vec<tree, va_gc> *params, bool orig_format)
10976 function_args_iterator iter;
10977 tree ptype;
10978 unsigned int parmnum;
10980 function_args_iter_init (&iter, TREE_TYPE (function));
10981 /* We've declared the implementation functions to use "volatile void *"
10982 as the pointer parameter, so we shouldn't get any complaints from the
10983 call to check_function_arguments what ever type the user used. */
10984 function_args_iter_next (&iter);
10985 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10986 ptype = TYPE_MAIN_VARIANT (ptype);
10988 /* For the rest of the values, we need to cast these to FTYPE, so that we
10989 don't get warnings for passing pointer types, etc. */
10990 parmnum = 0;
10991 while (1)
10993 tree val, arg_type;
10995 arg_type = function_args_iter_cond (&iter);
10996 /* XXX void_type_node belies the abstraction. */
10997 if (arg_type == void_type_node)
10998 break;
11000 ++parmnum;
11001 if (params->length () <= parmnum)
11003 error_at (loc, "too few arguments to function %qE", orig_function);
11004 return false;
11007 /* Only convert parameters if arg_type is unsigned integer type with
11008 new format sync routines, i.e. don't attempt to convert pointer
11009 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11010 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11011 kinds). */
11012 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
11014 /* Ideally for the first conversion we'd use convert_for_assignment
11015 so that we get warnings for anything that doesn't match the pointer
11016 type. This isn't portable across the C and C++ front ends atm. */
11017 val = (*params)[parmnum];
11018 val = convert (ptype, val);
11019 val = convert (arg_type, val);
11020 (*params)[parmnum] = val;
11023 function_args_iter_next (&iter);
11026 /* __atomic routines are not variadic. */
11027 if (!orig_format && params->length () != parmnum + 1)
11029 error_at (loc, "too many arguments to function %qE", orig_function);
11030 return false;
11033 /* The definition of these primitives is variadic, with the remaining
11034 being "an optional list of variables protected by the memory barrier".
11035 No clue what that's supposed to mean, precisely, but we consider all
11036 call-clobbered variables to be protected so we're safe. */
11037 params->truncate (parmnum + 1);
11039 return true;
11042 /* A helper function for resolve_overloaded_builtin. Adds a cast to
11043 RESULT to make it match the type of the first pointer argument in
11044 PARAMS. */
11046 static tree
11047 sync_resolve_return (tree first_param, tree result, bool orig_format)
11049 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
11050 tree rtype = TREE_TYPE (result);
11051 ptype = TYPE_MAIN_VARIANT (ptype);
11053 /* New format doesn't require casting unless the types are the same size. */
11054 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11055 return convert (ptype, result);
11056 else
11057 return result;
11060 /* This function verifies the PARAMS to generic atomic FUNCTION.
11061 It returns the size if all the parameters are the same size, otherwise
11062 0 is returned if the parameters are invalid. */
11064 static int
11065 get_atomic_generic_size (location_t loc, tree function,
11066 vec<tree, va_gc> *params)
11068 unsigned int n_param;
11069 unsigned int n_model;
11070 unsigned int x;
11071 int size_0;
11072 tree type_0;
11074 /* Determine the parameter makeup. */
11075 switch (DECL_FUNCTION_CODE (function))
11077 case BUILT_IN_ATOMIC_EXCHANGE:
11078 n_param = 4;
11079 n_model = 1;
11080 break;
11081 case BUILT_IN_ATOMIC_LOAD:
11082 case BUILT_IN_ATOMIC_STORE:
11083 n_param = 3;
11084 n_model = 1;
11085 break;
11086 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11087 n_param = 6;
11088 n_model = 2;
11089 break;
11090 default:
11091 gcc_unreachable ();
11094 if (vec_safe_length (params) != n_param)
11096 error_at (loc, "incorrect number of arguments to function %qE", function);
11097 return 0;
11100 /* Get type of first parameter, and determine its size. */
11101 type_0 = TREE_TYPE ((*params)[0]);
11102 if (TREE_CODE (type_0) == ARRAY_TYPE)
11104 /* Force array-to-pointer decay for C++. */
11105 gcc_assert (c_dialect_cxx());
11106 (*params)[0] = default_conversion ((*params)[0]);
11107 type_0 = TREE_TYPE ((*params)[0]);
11109 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11111 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11112 function);
11113 return 0;
11116 /* Types must be compile time constant sizes. */
11117 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
11119 error_at (loc,
11120 "argument 1 of %qE must be a pointer to a constant size type",
11121 function);
11122 return 0;
11125 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
11127 /* Zero size objects are not allowed. */
11128 if (size_0 == 0)
11130 error_at (loc,
11131 "argument 1 of %qE must be a pointer to a nonzero size object",
11132 function);
11133 return 0;
11136 /* Check each other parameter is a pointer and the same size. */
11137 for (x = 0; x < n_param - n_model; x++)
11139 int size;
11140 tree type = TREE_TYPE ((*params)[x]);
11141 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
11142 if (n_param == 6 && x == 3)
11143 continue;
11144 if (!POINTER_TYPE_P (type))
11146 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11147 function);
11148 return 0;
11150 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11151 size = type_size ? tree_to_uhwi (type_size) : 0;
11152 if (size != size_0)
11154 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11155 function);
11156 return 0;
11160 /* Check memory model parameters for validity. */
11161 for (x = n_param - n_model ; x < n_param; x++)
11163 tree p = (*params)[x];
11164 if (TREE_CODE (p) == INTEGER_CST)
11166 int i = tree_to_uhwi (p);
11167 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11169 warning_at (loc, OPT_Winvalid_memory_model,
11170 "invalid memory model argument %d of %qE", x + 1,
11171 function);
11174 else
11175 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11177 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11178 function);
11179 return 0;
11183 return size_0;
11187 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11188 at the beginning of the parameter list PARAMS representing the size of the
11189 objects. This is to match the library ABI requirement. LOC is the location
11190 of the function call.
11191 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11192 returned to allow the external call to be constructed. */
11194 static tree
11195 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11196 vec<tree, va_gc> *params)
11198 tree size_node;
11200 /* Insert a SIZE_T parameter as the first param. If there isn't
11201 enough space, allocate a new vector and recursively re-build with that. */
11202 if (!params->space (1))
11204 unsigned int z, len;
11205 vec<tree, va_gc> *v;
11206 tree f;
11208 len = params->length ();
11209 vec_alloc (v, len + 1);
11210 v->quick_push (build_int_cst (size_type_node, n));
11211 for (z = 0; z < len; z++)
11212 v->quick_push ((*params)[z]);
11213 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11214 vec_free (v);
11215 return f;
11218 /* Add the size parameter and leave as a function call for processing. */
11219 size_node = build_int_cst (size_type_node, n);
11220 params->quick_insert (0, size_node);
11221 return NULL_TREE;
11225 /* Return whether atomic operations for naturally aligned N-byte
11226 arguments are supported, whether inline or through libatomic. */
11227 static bool
11228 atomic_size_supported_p (int n)
11230 switch (n)
11232 case 1:
11233 case 2:
11234 case 4:
11235 case 8:
11236 return true;
11238 case 16:
11239 return targetm.scalar_mode_supported_p (TImode);
11241 default:
11242 return false;
11246 /* This will process an __atomic_exchange function call, determine whether it
11247 needs to be mapped to the _N variation, or turned into a library call.
11248 LOC is the location of the builtin call.
11249 FUNCTION is the DECL that has been invoked;
11250 PARAMS is the argument list for the call. The return value is non-null
11251 TRUE is returned if it is translated into the proper format for a call to the
11252 external library, and NEW_RETURN is set the tree for that function.
11253 FALSE is returned if processing for the _N variation is required, and
11254 NEW_RETURN is set to the return value the result is copied into. */
11255 static bool
11256 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11257 vec<tree, va_gc> *params, tree *new_return)
11259 tree p0, p1, p2, p3;
11260 tree I_type, I_type_ptr;
11261 int n = get_atomic_generic_size (loc, function, params);
11263 /* Size of 0 is an error condition. */
11264 if (n == 0)
11266 *new_return = error_mark_node;
11267 return true;
11270 /* If not a lock-free size, change to the library generic format. */
11271 if (!atomic_size_supported_p (n))
11273 *new_return = add_atomic_size_parameter (n, loc, function, params);
11274 return true;
11277 /* Otherwise there is a lockfree match, transform the call from:
11278 void fn(T* mem, T* desired, T* return, model)
11279 into
11280 *return = (T) (fn (In* mem, (In) *desired, model)) */
11282 p0 = (*params)[0];
11283 p1 = (*params)[1];
11284 p2 = (*params)[2];
11285 p3 = (*params)[3];
11287 /* Create pointer to appropriate size. */
11288 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11289 I_type_ptr = build_pointer_type (I_type);
11291 /* Convert object pointer to required type. */
11292 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11293 (*params)[0] = p0;
11294 /* Convert new value to required type, and dereference it. */
11295 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11296 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11297 (*params)[1] = p1;
11299 /* Move memory model to the 3rd position, and end param list. */
11300 (*params)[2] = p3;
11301 params->truncate (3);
11303 /* Convert return pointer and dereference it for later assignment. */
11304 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11306 return false;
11310 /* This will process an __atomic_compare_exchange function call, determine
11311 whether it needs to be mapped to the _N variation, or turned into a lib call.
11312 LOC is the location of the builtin call.
11313 FUNCTION is the DECL that has been invoked;
11314 PARAMS is the argument list for the call. The return value is non-null
11315 TRUE is returned if it is translated into the proper format for a call to the
11316 external library, and NEW_RETURN is set the tree for that function.
11317 FALSE is returned if processing for the _N variation is required. */
11319 static bool
11320 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11321 vec<tree, va_gc> *params,
11322 tree *new_return)
11324 tree p0, p1, p2;
11325 tree I_type, I_type_ptr;
11326 int n = get_atomic_generic_size (loc, function, params);
11328 /* Size of 0 is an error condition. */
11329 if (n == 0)
11331 *new_return = error_mark_node;
11332 return true;
11335 /* If not a lock-free size, change to the library generic format. */
11336 if (!atomic_size_supported_p (n))
11338 /* The library generic format does not have the weak parameter, so
11339 remove it from the param list. Since a parameter has been removed,
11340 we can be sure that there is room for the SIZE_T parameter, meaning
11341 there will not be a recursive rebuilding of the parameter list, so
11342 there is no danger this will be done twice. */
11343 if (n > 0)
11345 (*params)[3] = (*params)[4];
11346 (*params)[4] = (*params)[5];
11347 params->truncate (5);
11349 *new_return = add_atomic_size_parameter (n, loc, function, params);
11350 return true;
11353 /* Otherwise, there is a match, so the call needs to be transformed from:
11354 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11355 into
11356 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11358 p0 = (*params)[0];
11359 p1 = (*params)[1];
11360 p2 = (*params)[2];
11362 /* Create pointer to appropriate size. */
11363 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11364 I_type_ptr = build_pointer_type (I_type);
11366 /* Convert object pointer to required type. */
11367 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11368 (*params)[0] = p0;
11370 /* Convert expected pointer to required type. */
11371 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11372 (*params)[1] = p1;
11374 /* Convert desired value to required type, and dereference it. */
11375 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11376 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11377 (*params)[2] = p2;
11379 /* The rest of the parameters are fine. NULL means no special return value
11380 processing.*/
11381 *new_return = NULL;
11382 return false;
11386 /* This will process an __atomic_load function call, determine whether it
11387 needs to be mapped to the _N variation, or turned into a library call.
11388 LOC is the location of the builtin call.
11389 FUNCTION is the DECL that has been invoked;
11390 PARAMS is the argument list for the call. The return value is non-null
11391 TRUE is returned if it is translated into the proper format for a call to the
11392 external library, and NEW_RETURN is set the tree for that function.
11393 FALSE is returned if processing for the _N variation is required, and
11394 NEW_RETURN is set to the return value the result is copied into. */
11396 static bool
11397 resolve_overloaded_atomic_load (location_t loc, tree function,
11398 vec<tree, va_gc> *params, tree *new_return)
11400 tree p0, p1, p2;
11401 tree I_type, I_type_ptr;
11402 int n = get_atomic_generic_size (loc, function, params);
11404 /* Size of 0 is an error condition. */
11405 if (n == 0)
11407 *new_return = error_mark_node;
11408 return true;
11411 /* If not a lock-free size, change to the library generic format. */
11412 if (!atomic_size_supported_p (n))
11414 *new_return = add_atomic_size_parameter (n, loc, function, params);
11415 return true;
11418 /* Otherwise, there is a match, so the call needs to be transformed from:
11419 void fn(T* mem, T* return, model)
11420 into
11421 *return = (T) (fn ((In *) mem, model)) */
11423 p0 = (*params)[0];
11424 p1 = (*params)[1];
11425 p2 = (*params)[2];
11427 /* Create pointer to appropriate size. */
11428 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11429 I_type_ptr = build_pointer_type (I_type);
11431 /* Convert object pointer to required type. */
11432 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11433 (*params)[0] = p0;
11435 /* Move memory model to the 2nd position, and end param list. */
11436 (*params)[1] = p2;
11437 params->truncate (2);
11439 /* Convert return pointer and dereference it for later assignment. */
11440 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11442 return false;
11446 /* This will process an __atomic_store function call, determine whether it
11447 needs to be mapped to the _N variation, or turned into a library call.
11448 LOC is the location of the builtin call.
11449 FUNCTION is the DECL that has been invoked;
11450 PARAMS is the argument list for the call. The return value is non-null
11451 TRUE is returned if it is translated into the proper format for a call to the
11452 external library, and NEW_RETURN is set the tree for that function.
11453 FALSE is returned if processing for the _N variation is required, and
11454 NEW_RETURN is set to the return value the result is copied into. */
11456 static bool
11457 resolve_overloaded_atomic_store (location_t loc, tree function,
11458 vec<tree, va_gc> *params, tree *new_return)
11460 tree p0, p1;
11461 tree I_type, I_type_ptr;
11462 int n = get_atomic_generic_size (loc, function, params);
11464 /* Size of 0 is an error condition. */
11465 if (n == 0)
11467 *new_return = error_mark_node;
11468 return true;
11471 /* If not a lock-free size, change to the library generic format. */
11472 if (!atomic_size_supported_p (n))
11474 *new_return = add_atomic_size_parameter (n, loc, function, params);
11475 return true;
11478 /* Otherwise, there is a match, so the call needs to be transformed from:
11479 void fn(T* mem, T* value, model)
11480 into
11481 fn ((In *) mem, (In) *value, model) */
11483 p0 = (*params)[0];
11484 p1 = (*params)[1];
11486 /* Create pointer to appropriate size. */
11487 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11488 I_type_ptr = build_pointer_type (I_type);
11490 /* Convert object pointer to required type. */
11491 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11492 (*params)[0] = p0;
11494 /* Convert new value to required type, and dereference it. */
11495 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11496 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11497 (*params)[1] = p1;
11499 /* The memory model is in the right spot already. Return is void. */
11500 *new_return = NULL_TREE;
11502 return false;
11506 /* Some builtin functions are placeholders for other expressions. This
11507 function should be called immediately after parsing the call expression
11508 before surrounding code has committed to the type of the expression.
11510 LOC is the location of the builtin call.
11512 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11513 PARAMS is the argument list for the call. The return value is non-null
11514 when expansion is complete, and null if normal processing should
11515 continue. */
11517 tree
11518 resolve_overloaded_builtin (location_t loc, tree function,
11519 vec<tree, va_gc> *params)
11521 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11522 bool orig_format = true;
11523 tree new_return = NULL_TREE;
11525 switch (DECL_BUILT_IN_CLASS (function))
11527 case BUILT_IN_NORMAL:
11528 break;
11529 case BUILT_IN_MD:
11530 if (targetm.resolve_overloaded_builtin)
11531 return targetm.resolve_overloaded_builtin (loc, function, params);
11532 else
11533 return NULL_TREE;
11534 default:
11535 return NULL_TREE;
11538 /* Handle BUILT_IN_NORMAL here. */
11539 switch (orig_code)
11541 case BUILT_IN_ATOMIC_EXCHANGE:
11542 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11543 case BUILT_IN_ATOMIC_LOAD:
11544 case BUILT_IN_ATOMIC_STORE:
11546 /* Handle these 4 together so that they can fall through to the next
11547 case if the call is transformed to an _N variant. */
11548 switch (orig_code)
11550 case BUILT_IN_ATOMIC_EXCHANGE:
11552 if (resolve_overloaded_atomic_exchange (loc, function, params,
11553 &new_return))
11554 return new_return;
11555 /* Change to the _N variant. */
11556 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11557 break;
11560 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11562 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11563 params,
11564 &new_return))
11565 return new_return;
11566 /* Change to the _N variant. */
11567 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11568 break;
11570 case BUILT_IN_ATOMIC_LOAD:
11572 if (resolve_overloaded_atomic_load (loc, function, params,
11573 &new_return))
11574 return new_return;
11575 /* Change to the _N variant. */
11576 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11577 break;
11579 case BUILT_IN_ATOMIC_STORE:
11581 if (resolve_overloaded_atomic_store (loc, function, params,
11582 &new_return))
11583 return new_return;
11584 /* Change to the _N variant. */
11585 orig_code = BUILT_IN_ATOMIC_STORE_N;
11586 break;
11588 default:
11589 gcc_unreachable ();
11591 /* Fallthrough to the normal processing. */
11593 case BUILT_IN_ATOMIC_EXCHANGE_N:
11594 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11595 case BUILT_IN_ATOMIC_LOAD_N:
11596 case BUILT_IN_ATOMIC_STORE_N:
11597 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11598 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11599 case BUILT_IN_ATOMIC_AND_FETCH_N:
11600 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11601 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11602 case BUILT_IN_ATOMIC_OR_FETCH_N:
11603 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11604 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11605 case BUILT_IN_ATOMIC_FETCH_AND_N:
11606 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11607 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11608 case BUILT_IN_ATOMIC_FETCH_OR_N:
11610 orig_format = false;
11611 /* Fallthru for parameter processing. */
11613 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11614 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11615 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11616 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11617 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11618 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11619 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11620 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11621 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11622 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11623 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11624 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11625 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11626 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11627 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11628 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11630 int n = sync_resolve_size (function, params);
11631 tree new_function, first_param, result;
11632 enum built_in_function fncode;
11634 if (n == 0)
11635 return error_mark_node;
11637 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11638 new_function = builtin_decl_explicit (fncode);
11639 if (!sync_resolve_params (loc, function, new_function, params,
11640 orig_format))
11641 return error_mark_node;
11643 first_param = (*params)[0];
11644 result = build_function_call_vec (loc, vNULL, new_function, params,
11645 NULL);
11646 if (result == error_mark_node)
11647 return result;
11648 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11649 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11650 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11651 result = sync_resolve_return (first_param, result, orig_format);
11653 /* If new_return is set, assign function to that expr and cast the
11654 result to void since the generic interface returned void. */
11655 if (new_return)
11657 /* Cast function result from I{1,2,4,8,16} to the required type. */
11658 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11659 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11660 result);
11661 TREE_SIDE_EFFECTS (result) = 1;
11662 protected_set_expr_location (result, loc);
11663 result = convert (void_type_node, result);
11665 return result;
11668 default:
11669 return NULL_TREE;
11673 /* vector_types_compatible_elements_p is used in type checks of vectors
11674 values used as operands of binary operators. Where it returns true, and
11675 the other checks of the caller succeed (being vector types in he first
11676 place, and matching number of elements), we can just treat the types
11677 as essentially the same.
11678 Contrast with vector_targets_convertible_p, which is used for vector
11679 pointer types, and vector_types_convertible_p, which will allow
11680 language-specific matches under the control of flag_lax_vector_conversions,
11681 and might still require a conversion. */
11682 /* True if vector types T1 and T2 can be inputs to the same binary
11683 operator without conversion.
11684 We don't check the overall vector size here because some of our callers
11685 want to give different error messages when the vectors are compatible
11686 except for the element count. */
11688 bool
11689 vector_types_compatible_elements_p (tree t1, tree t2)
11691 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11692 t1 = TREE_TYPE (t1);
11693 t2 = TREE_TYPE (t2);
11695 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11697 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11698 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11699 || c2 == FIXED_POINT_TYPE));
11701 t1 = c_common_signed_type (t1);
11702 t2 = c_common_signed_type (t2);
11703 /* Equality works here because c_common_signed_type uses
11704 TYPE_MAIN_VARIANT. */
11705 if (t1 == t2)
11706 return true;
11707 if (opaque && c1 == c2
11708 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11709 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11710 return true;
11711 return false;
11714 /* Check for missing format attributes on function pointers. LTYPE is
11715 the new type or left-hand side type. RTYPE is the old type or
11716 right-hand side type. Returns TRUE if LTYPE is missing the desired
11717 attribute. */
11719 bool
11720 check_missing_format_attribute (tree ltype, tree rtype)
11722 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11723 tree ra;
11725 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11726 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11727 break;
11728 if (ra)
11730 tree la;
11731 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11732 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11733 break;
11734 return !la;
11736 else
11737 return false;
11740 /* Subscripting with type char is likely to lose on a machine where
11741 chars are signed. So warn on any machine, but optionally. Don't
11742 warn for unsigned char since that type is safe. Don't warn for
11743 signed char because anyone who uses that must have done so
11744 deliberately. Furthermore, we reduce the false positive load by
11745 warning only for non-constant value of type char. */
11747 void
11748 warn_array_subscript_with_type_char (location_t loc, tree index)
11750 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11751 && TREE_CODE (index) != INTEGER_CST)
11752 warning_at (loc, OPT_Wchar_subscripts,
11753 "array subscript has type %<char%>");
11756 /* Implement -Wparentheses for the unexpected C precedence rules, to
11757 cover cases like x + y << z which readers are likely to
11758 misinterpret. We have seen an expression in which CODE is a binary
11759 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11760 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11761 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11762 expression was not formed using a binary or unary operator, or it
11763 was enclosed in parentheses. */
11765 void
11766 warn_about_parentheses (location_t loc, enum tree_code code,
11767 enum tree_code code_left, tree arg_left,
11768 enum tree_code code_right, tree arg_right)
11770 if (!warn_parentheses)
11771 return;
11773 /* This macro tests that the expression ARG with original tree code
11774 CODE appears to be a boolean expression. or the result of folding a
11775 boolean expression. */
11776 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11777 (truth_value_p (TREE_CODE (ARG)) \
11778 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11779 /* Folding may create 0 or 1 integers from other expressions. */ \
11780 || ((CODE) != INTEGER_CST \
11781 && (integer_onep (ARG) || integer_zerop (ARG))))
11783 switch (code)
11785 case LSHIFT_EXPR:
11786 if (code_left == PLUS_EXPR)
11787 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11788 "suggest parentheses around %<+%> inside %<<<%>");
11789 else if (code_right == PLUS_EXPR)
11790 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11791 "suggest parentheses around %<+%> inside %<<<%>");
11792 else if (code_left == MINUS_EXPR)
11793 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11794 "suggest parentheses around %<-%> inside %<<<%>");
11795 else if (code_right == MINUS_EXPR)
11796 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11797 "suggest parentheses around %<-%> inside %<<<%>");
11798 return;
11800 case RSHIFT_EXPR:
11801 if (code_left == PLUS_EXPR)
11802 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11803 "suggest parentheses around %<+%> inside %<>>%>");
11804 else if (code_right == PLUS_EXPR)
11805 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11806 "suggest parentheses around %<+%> inside %<>>%>");
11807 else if (code_left == MINUS_EXPR)
11808 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11809 "suggest parentheses around %<-%> inside %<>>%>");
11810 else if (code_right == MINUS_EXPR)
11811 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11812 "suggest parentheses around %<-%> inside %<>>%>");
11813 return;
11815 case TRUTH_ORIF_EXPR:
11816 if (code_left == TRUTH_ANDIF_EXPR)
11817 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11818 "suggest parentheses around %<&&%> within %<||%>");
11819 else if (code_right == TRUTH_ANDIF_EXPR)
11820 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11821 "suggest parentheses around %<&&%> within %<||%>");
11822 return;
11824 case BIT_IOR_EXPR:
11825 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11826 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11827 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11828 "suggest parentheses around arithmetic in operand of %<|%>");
11829 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11830 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11831 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11832 "suggest parentheses around arithmetic in operand of %<|%>");
11833 /* Check cases like x|y==z */
11834 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11835 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11836 "suggest parentheses around comparison in operand of %<|%>");
11837 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11838 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11839 "suggest parentheses around comparison in operand of %<|%>");
11840 /* Check cases like !x | y */
11841 else if (code_left == TRUTH_NOT_EXPR
11842 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11843 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11844 "suggest parentheses around operand of "
11845 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11846 return;
11848 case BIT_XOR_EXPR:
11849 if (code_left == BIT_AND_EXPR
11850 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11851 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11852 "suggest parentheses around arithmetic in operand of %<^%>");
11853 else if (code_right == BIT_AND_EXPR
11854 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11855 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11856 "suggest parentheses around arithmetic in operand of %<^%>");
11857 /* Check cases like x^y==z */
11858 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11859 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11860 "suggest parentheses around comparison in operand of %<^%>");
11861 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11862 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11863 "suggest parentheses around comparison in operand of %<^%>");
11864 return;
11866 case BIT_AND_EXPR:
11867 if (code_left == PLUS_EXPR)
11868 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11869 "suggest parentheses around %<+%> in operand of %<&%>");
11870 else if (code_right == PLUS_EXPR)
11871 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11872 "suggest parentheses around %<+%> in operand of %<&%>");
11873 else if (code_left == MINUS_EXPR)
11874 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11875 "suggest parentheses around %<-%> in operand of %<&%>");
11876 else if (code_right == MINUS_EXPR)
11877 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11878 "suggest parentheses around %<-%> in operand of %<&%>");
11879 /* Check cases like x&y==z */
11880 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11881 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11882 "suggest parentheses around comparison in operand of %<&%>");
11883 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11884 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11885 "suggest parentheses around comparison in operand of %<&%>");
11886 /* Check cases like !x & y */
11887 else if (code_left == TRUTH_NOT_EXPR
11888 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11889 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11890 "suggest parentheses around operand of "
11891 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11892 return;
11894 case EQ_EXPR:
11895 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11896 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11897 "suggest parentheses around comparison in operand of %<==%>");
11898 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11899 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11900 "suggest parentheses around comparison in operand of %<==%>");
11901 return;
11902 case NE_EXPR:
11903 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11904 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11905 "suggest parentheses around comparison in operand of %<!=%>");
11906 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11907 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11908 "suggest parentheses around comparison in operand of %<!=%>");
11909 return;
11911 default:
11912 if (TREE_CODE_CLASS (code) == tcc_comparison)
11914 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11915 && code_left != NE_EXPR && code_left != EQ_EXPR
11916 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11917 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11918 "comparisons like %<X<=Y<=Z%> do not "
11919 "have their mathematical meaning");
11920 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11921 && code_right != NE_EXPR && code_right != EQ_EXPR
11922 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11923 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11924 "comparisons like %<X<=Y<=Z%> do not "
11925 "have their mathematical meaning");
11927 return;
11929 #undef NOT_A_BOOLEAN_EXPR_P
11932 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11934 void
11935 warn_for_unused_label (tree label)
11937 if (!TREE_USED (label))
11939 if (DECL_INITIAL (label))
11940 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11941 else
11942 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11946 /* Warn for division by zero according to the value of DIVISOR. LOC
11947 is the location of the division operator. */
11949 void
11950 warn_for_div_by_zero (location_t loc, tree divisor)
11952 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11953 about division by zero. Do not issue a warning if DIVISOR has a
11954 floating-point type, since we consider 0.0/0.0 a valid way of
11955 generating a NaN. */
11956 if (c_inhibit_evaluation_warnings == 0
11957 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11958 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11961 /* Subroutine of build_binary_op. Give warnings for comparisons
11962 between signed and unsigned quantities that may fail. Do the
11963 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11964 so that casts will be considered, but default promotions won't
11967 LOCATION is the location of the comparison operator.
11969 The arguments of this function map directly to local variables
11970 of build_binary_op. */
11972 void
11973 warn_for_sign_compare (location_t location,
11974 tree orig_op0, tree orig_op1,
11975 tree op0, tree op1,
11976 tree result_type, enum tree_code resultcode)
11978 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11979 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11980 int unsignedp0, unsignedp1;
11982 /* In C++, check for comparison of different enum types. */
11983 if (c_dialect_cxx()
11984 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11985 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11986 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11987 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11989 warning_at (location,
11990 OPT_Wsign_compare, "comparison between types %qT and %qT",
11991 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11994 /* Do not warn if the comparison is being done in a signed type,
11995 since the signed type will only be chosen if it can represent
11996 all the values of the unsigned type. */
11997 if (!TYPE_UNSIGNED (result_type))
11998 /* OK */;
11999 /* Do not warn if both operands are unsigned. */
12000 else if (op0_signed == op1_signed)
12001 /* OK */;
12002 else
12004 tree sop, uop, base_type;
12005 bool ovf;
12007 if (op0_signed)
12008 sop = orig_op0, uop = orig_op1;
12009 else
12010 sop = orig_op1, uop = orig_op0;
12012 STRIP_TYPE_NOPS (sop);
12013 STRIP_TYPE_NOPS (uop);
12014 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12015 ? TREE_TYPE (result_type) : result_type);
12017 /* Do not warn if the signed quantity is an unsuffixed integer
12018 literal (or some static constant expression involving such
12019 literals or a conditional expression involving such literals)
12020 and it is non-negative. */
12021 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12022 /* OK */;
12023 /* Do not warn if the comparison is an equality operation, the
12024 unsigned quantity is an integral constant, and it would fit
12025 in the result if the result were signed. */
12026 else if (TREE_CODE (uop) == INTEGER_CST
12027 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
12028 && int_fits_type_p (uop, c_common_signed_type (base_type)))
12029 /* OK */;
12030 /* In C, do not warn if the unsigned quantity is an enumeration
12031 constant and its maximum value would fit in the result if the
12032 result were signed. */
12033 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12034 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12035 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
12036 c_common_signed_type (base_type)))
12037 /* OK */;
12038 else
12039 warning_at (location,
12040 OPT_Wsign_compare,
12041 "comparison between signed and unsigned integer expressions");
12044 /* Warn if two unsigned values are being compared in a size larger
12045 than their original size, and one (and only one) is the result of
12046 a `~' operator. This comparison will always fail.
12048 Also warn if one operand is a constant, and the constant does not
12049 have all bits set that are set in the ~ operand when it is
12050 extended. */
12052 op0 = c_common_get_narrower (op0, &unsignedp0);
12053 op1 = c_common_get_narrower (op1, &unsignedp1);
12055 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12056 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12058 if (TREE_CODE (op0) == BIT_NOT_EXPR)
12059 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
12060 if (TREE_CODE (op1) == BIT_NOT_EXPR)
12061 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
12063 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
12065 tree primop;
12066 HOST_WIDE_INT constant, mask;
12067 int unsignedp;
12068 unsigned int bits;
12070 if (tree_fits_shwi_p (op0))
12072 primop = op1;
12073 unsignedp = unsignedp1;
12074 constant = tree_to_shwi (op0);
12076 else
12078 primop = op0;
12079 unsignedp = unsignedp0;
12080 constant = tree_to_shwi (op1);
12083 bits = TYPE_PRECISION (TREE_TYPE (primop));
12084 if (bits < TYPE_PRECISION (result_type)
12085 && bits < HOST_BITS_PER_LONG && unsignedp)
12087 mask = (~ (HOST_WIDE_INT) 0) << bits;
12088 if ((mask & constant) != mask)
12090 if (constant == 0)
12091 warning_at (location, OPT_Wsign_compare,
12092 "promoted ~unsigned is always non-zero");
12093 else
12094 warning_at (location, OPT_Wsign_compare,
12095 "comparison of promoted ~unsigned with constant");
12099 else if (unsignedp0 && unsignedp1
12100 && (TYPE_PRECISION (TREE_TYPE (op0))
12101 < TYPE_PRECISION (result_type))
12102 && (TYPE_PRECISION (TREE_TYPE (op1))
12103 < TYPE_PRECISION (result_type)))
12104 warning_at (location, OPT_Wsign_compare,
12105 "comparison of promoted ~unsigned with unsigned");
12109 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12110 type via c_common_type. If -Wdouble-promotion is in use, and the
12111 conditions for warning have been met, issue a warning. GMSGID is
12112 the warning message. It must have two %T specifiers for the type
12113 that was converted (generally "float") and the type to which it was
12114 converted (generally "double), respectively. LOC is the location
12115 to which the awrning should refer. */
12117 void
12118 do_warn_double_promotion (tree result_type, tree type1, tree type2,
12119 const char *gmsgid, location_t loc)
12121 tree source_type;
12123 if (!warn_double_promotion)
12124 return;
12125 /* If the conversion will not occur at run-time, there is no need to
12126 warn about it. */
12127 if (c_inhibit_evaluation_warnings)
12128 return;
12129 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12130 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12131 return;
12132 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12133 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12134 source_type = type1;
12135 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12136 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12137 source_type = type2;
12138 else
12139 return;
12140 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12143 /* Possibly warn about unused parameters. */
12145 void
12146 do_warn_unused_parameter (tree fn)
12148 tree decl;
12150 for (decl = DECL_ARGUMENTS (fn);
12151 decl; decl = DECL_CHAIN (decl))
12152 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12153 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12154 && !TREE_NO_WARNING (decl))
12155 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12156 "unused parameter %qD", decl);
12160 /* Setup a TYPE_DECL node as a typedef representation.
12162 X is a TYPE_DECL for a typedef statement. Create a brand new
12163 ..._TYPE node (which will be just a variant of the existing
12164 ..._TYPE node with identical properties) and then install X
12165 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12167 The whole point here is to end up with a situation where each
12168 and every ..._TYPE node the compiler creates will be uniquely
12169 associated with AT MOST one node representing a typedef name.
12170 This way, even though the compiler substitutes corresponding
12171 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12172 early on, later parts of the compiler can always do the reverse
12173 translation and get back the corresponding typedef name. For
12174 example, given:
12176 typedef struct S MY_TYPE;
12177 MY_TYPE object;
12179 Later parts of the compiler might only know that `object' was of
12180 type `struct S' if it were not for code just below. With this
12181 code however, later parts of the compiler see something like:
12183 struct S' == struct S
12184 typedef struct S' MY_TYPE;
12185 struct S' object;
12187 And they can then deduce (from the node for type struct S') that
12188 the original object declaration was:
12190 MY_TYPE object;
12192 Being able to do this is important for proper support of protoize,
12193 and also for generating precise symbolic debugging information
12194 which takes full account of the programmer's (typedef) vocabulary.
12196 Obviously, we don't want to generate a duplicate ..._TYPE node if
12197 the TYPE_DECL node that we are now processing really represents a
12198 standard built-in type. */
12200 void
12201 set_underlying_type (tree x)
12203 if (x == error_mark_node)
12204 return;
12205 if (DECL_IS_BUILTIN (x))
12207 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12208 TYPE_NAME (TREE_TYPE (x)) = x;
12210 else if (TREE_TYPE (x) != error_mark_node
12211 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12213 tree tt = TREE_TYPE (x);
12214 DECL_ORIGINAL_TYPE (x) = tt;
12215 tt = build_variant_type_copy (tt);
12216 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12217 TYPE_NAME (tt) = x;
12218 TREE_USED (tt) = TREE_USED (x);
12219 TREE_TYPE (x) = tt;
12223 /* Record the types used by the current global variable declaration
12224 being parsed, so that we can decide later to emit their debug info.
12225 Those types are in types_used_by_cur_var_decl, and we are going to
12226 store them in the types_used_by_vars_hash hash table.
12227 DECL is the declaration of the global variable that has been parsed. */
12229 void
12230 record_types_used_by_current_var_decl (tree decl)
12232 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12234 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12236 tree type = types_used_by_cur_var_decl->pop ();
12237 types_used_by_var_decl_insert (type, decl);
12241 /* If DECL is a typedef that is declared in the current function,
12242 record it for the purpose of -Wunused-local-typedefs. */
12244 void
12245 record_locally_defined_typedef (tree decl)
12247 struct c_language_function *l;
12249 if (!warn_unused_local_typedefs
12250 || cfun == NULL
12251 /* if this is not a locally defined typedef then we are not
12252 interested. */
12253 || !is_typedef_decl (decl)
12254 || !decl_function_context (decl))
12255 return;
12257 l = (struct c_language_function *) cfun->language;
12258 vec_safe_push (l->local_typedefs, decl);
12261 /* If T is a TYPE_DECL declared locally, mark it as used. */
12263 void
12264 maybe_record_typedef_use (tree t)
12266 if (!is_typedef_decl (t))
12267 return;
12269 TREE_USED (t) = true;
12272 /* Warn if there are some unused locally defined typedefs in the
12273 current function. */
12275 void
12276 maybe_warn_unused_local_typedefs (void)
12278 int i;
12279 tree decl;
12280 /* The number of times we have emitted -Wunused-local-typedefs
12281 warnings. If this is different from errorcount, that means some
12282 unrelated errors have been issued. In which case, we'll avoid
12283 emitting "unused-local-typedefs" warnings. */
12284 static int unused_local_typedefs_warn_count;
12285 struct c_language_function *l;
12287 if (cfun == NULL)
12288 return;
12290 if ((l = (struct c_language_function *) cfun->language) == NULL)
12291 return;
12293 if (warn_unused_local_typedefs
12294 && errorcount == unused_local_typedefs_warn_count)
12296 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12297 if (!TREE_USED (decl))
12298 warning_at (DECL_SOURCE_LOCATION (decl),
12299 OPT_Wunused_local_typedefs,
12300 "typedef %qD locally defined but not used", decl);
12301 unused_local_typedefs_warn_count = errorcount;
12304 vec_free (l->local_typedefs);
12307 /* Warn about boolean expression compared with an integer value different
12308 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12309 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12310 are the operands of the comparison. The caller must ensure that
12311 either operand is a boolean expression. */
12313 void
12314 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12315 tree op1)
12317 if (TREE_CODE_CLASS (code) != tcc_comparison)
12318 return;
12320 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12321 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12322 if (!cst)
12323 return;
12325 if (!integer_zerop (cst) && !integer_onep (cst))
12327 int sign = (TREE_CODE (op0) == INTEGER_CST
12328 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12329 if (code == EQ_EXPR
12330 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12331 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12332 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12333 "with boolean expression is always false", cst);
12334 else
12335 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12336 "with boolean expression is always true", cst);
12338 else if (integer_zerop (cst) || integer_onep (cst))
12340 /* If the non-constant operand isn't of a boolean type, we
12341 don't want to warn here. */
12342 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12343 /* Handle booleans promoted to integers. */
12344 if (CONVERT_EXPR_P (noncst)
12345 && TREE_TYPE (noncst) == integer_type_node
12346 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12347 /* Warn. */;
12348 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12349 && !truth_value_p (TREE_CODE (noncst)))
12350 return;
12351 /* Do some magic to get the right diagnostics. */
12352 bool flag = TREE_CODE (op0) == INTEGER_CST;
12353 flag = integer_zerop (cst) ? flag : !flag;
12354 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12355 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12356 "with boolean expression is always true", cst);
12357 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12358 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12359 "with boolean expression is always false", cst);
12363 /* The C and C++ parsers both use vectors to hold function arguments.
12364 For efficiency, we keep a cache of unused vectors. This is the
12365 cache. */
12367 typedef vec<tree, va_gc> *tree_gc_vec;
12368 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12370 /* Return a new vector from the cache. If the cache is empty,
12371 allocate a new vector. These vectors are GC'ed, so it is OK if the
12372 pointer is not released.. */
12374 vec<tree, va_gc> *
12375 make_tree_vector (void)
12377 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12378 return tree_vector_cache->pop ();
12379 else
12381 /* Passing 0 to vec::alloc returns NULL, and our callers require
12382 that we always return a non-NULL value. The vector code uses
12383 4 when growing a NULL vector, so we do too. */
12384 vec<tree, va_gc> *v;
12385 vec_alloc (v, 4);
12386 return v;
12390 /* Release a vector of trees back to the cache. */
12392 void
12393 release_tree_vector (vec<tree, va_gc> *vec)
12395 if (vec != NULL)
12397 vec->truncate (0);
12398 vec_safe_push (tree_vector_cache, vec);
12402 /* Get a new tree vector holding a single tree. */
12404 vec<tree, va_gc> *
12405 make_tree_vector_single (tree t)
12407 vec<tree, va_gc> *ret = make_tree_vector ();
12408 ret->quick_push (t);
12409 return ret;
12412 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12414 vec<tree, va_gc> *
12415 make_tree_vector_from_list (tree list)
12417 vec<tree, va_gc> *ret = make_tree_vector ();
12418 for (; list; list = TREE_CHAIN (list))
12419 vec_safe_push (ret, TREE_VALUE (list));
12420 return ret;
12423 /* Get a new tree vector which is a copy of an existing one. */
12425 vec<tree, va_gc> *
12426 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12428 vec<tree, va_gc> *ret;
12429 unsigned int ix;
12430 tree t;
12432 ret = make_tree_vector ();
12433 vec_safe_reserve (ret, vec_safe_length (orig));
12434 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12435 ret->quick_push (t);
12436 return ret;
12439 /* Return true if KEYWORD starts a type specifier. */
12441 bool
12442 keyword_begins_type_specifier (enum rid keyword)
12444 switch (keyword)
12446 case RID_AUTO_TYPE:
12447 case RID_INT:
12448 case RID_CHAR:
12449 case RID_FLOAT:
12450 case RID_DOUBLE:
12451 case RID_VOID:
12452 case RID_UNSIGNED:
12453 case RID_LONG:
12454 case RID_SHORT:
12455 case RID_SIGNED:
12456 case RID_DFLOAT32:
12457 case RID_DFLOAT64:
12458 case RID_DFLOAT128:
12459 case RID_FRACT:
12460 case RID_ACCUM:
12461 case RID_BOOL:
12462 case RID_WCHAR:
12463 case RID_CHAR16:
12464 case RID_CHAR32:
12465 case RID_SAT:
12466 case RID_COMPLEX:
12467 case RID_TYPEOF:
12468 case RID_STRUCT:
12469 case RID_CLASS:
12470 case RID_UNION:
12471 case RID_ENUM:
12472 return true;
12473 default:
12474 if (keyword >= RID_FIRST_INT_N
12475 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12476 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12477 return true;
12478 return false;
12482 /* Return true if KEYWORD names a type qualifier. */
12484 bool
12485 keyword_is_type_qualifier (enum rid keyword)
12487 switch (keyword)
12489 case RID_CONST:
12490 case RID_VOLATILE:
12491 case RID_RESTRICT:
12492 case RID_ATOMIC:
12493 return true;
12494 default:
12495 return false;
12499 /* Return true if KEYWORD names a storage class specifier.
12501 RID_TYPEDEF is not included in this list despite `typedef' being
12502 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12503 such for syntactic convenience only. */
12505 bool
12506 keyword_is_storage_class_specifier (enum rid keyword)
12508 switch (keyword)
12510 case RID_STATIC:
12511 case RID_EXTERN:
12512 case RID_REGISTER:
12513 case RID_AUTO:
12514 case RID_MUTABLE:
12515 case RID_THREAD:
12516 return true;
12517 default:
12518 return false;
12522 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12524 static bool
12525 keyword_is_function_specifier (enum rid keyword)
12527 switch (keyword)
12529 case RID_INLINE:
12530 case RID_NORETURN:
12531 case RID_VIRTUAL:
12532 case RID_EXPLICIT:
12533 return true;
12534 default:
12535 return false;
12539 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12540 declaration-specifier (C99 6.7). */
12542 bool
12543 keyword_is_decl_specifier (enum rid keyword)
12545 if (keyword_is_storage_class_specifier (keyword)
12546 || keyword_is_type_qualifier (keyword)
12547 || keyword_is_function_specifier (keyword))
12548 return true;
12550 switch (keyword)
12552 case RID_TYPEDEF:
12553 case RID_FRIEND:
12554 case RID_CONSTEXPR:
12555 return true;
12556 default:
12557 return false;
12561 /* Initialize language-specific-bits of tree_contains_struct. */
12563 void
12564 c_common_init_ts (void)
12566 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12567 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12568 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12571 /* Build a user-defined numeric literal out of an integer constant type VALUE
12572 with identifier SUFFIX. */
12574 tree
12575 build_userdef_literal (tree suffix_id, tree value,
12576 enum overflow_type overflow, tree num_string)
12578 tree literal = make_node (USERDEF_LITERAL);
12579 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12580 USERDEF_LITERAL_VALUE (literal) = value;
12581 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12582 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12583 return literal;
12586 /* For vector[index], convert the vector to a
12587 pointer of the underlying type. Return true if the resulting
12588 ARRAY_REF should not be an lvalue. */
12590 bool
12591 convert_vector_to_pointer_for_subscript (location_t loc,
12592 tree *vecp, tree index)
12594 bool ret = false;
12595 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12597 tree type = TREE_TYPE (*vecp);
12598 tree type1;
12600 ret = !lvalue_p (*vecp);
12601 if (TREE_CODE (index) == INTEGER_CST)
12602 if (!tree_fits_uhwi_p (index)
12603 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12604 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12606 if (ret)
12608 tree tmp = create_tmp_var_raw (type);
12609 DECL_SOURCE_LOCATION (tmp) = loc;
12610 *vecp = c_save_expr (*vecp);
12611 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12613 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12614 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12615 *vecp
12616 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12617 *vecp, NULL_TREE, NULL_TREE),
12618 non_const);
12620 else
12621 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12622 NULL_TREE, NULL_TREE);
12623 SET_EXPR_LOCATION (*vecp, loc);
12624 c_common_mark_addressable_vec (tmp);
12626 else
12627 c_common_mark_addressable_vec (*vecp);
12628 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12629 type1 = build_pointer_type (TREE_TYPE (*vecp));
12630 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12631 if (!ref_all
12632 && !DECL_P (*vecp))
12634 /* If the original vector isn't declared may_alias and it
12635 isn't a bare vector look if the subscripting would
12636 alias the vector we subscript, and if not, force ref-all. */
12637 alias_set_type vecset = get_alias_set (*vecp);
12638 alias_set_type sset = get_alias_set (type);
12639 if (!alias_sets_must_conflict_p (sset, vecset)
12640 && !alias_set_subset_of (sset, vecset))
12641 ref_all = true;
12643 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12644 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12645 *vecp = convert (type, *vecp);
12647 return ret;
12650 /* Determine which of the operands, if any, is a scalar that needs to be
12651 converted to a vector, for the range of operations. */
12652 enum stv_conv
12653 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12654 bool complain)
12656 tree type0 = TREE_TYPE (op0);
12657 tree type1 = TREE_TYPE (op1);
12658 bool integer_only_op = false;
12659 enum stv_conv ret = stv_firstarg;
12661 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12662 switch (code)
12664 /* Most GENERIC binary expressions require homogeneous arguments.
12665 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12666 argument that is a vector and a second one that is a scalar, so
12667 we never return stv_secondarg for them. */
12668 case RSHIFT_EXPR:
12669 case LSHIFT_EXPR:
12670 if (TREE_CODE (type0) == INTEGER_TYPE
12671 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12673 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12675 if (complain)
12676 error_at (loc, "conversion of scalar %qT to vector %qT "
12677 "involves truncation", type0, type1);
12678 return stv_error;
12680 else
12681 return stv_firstarg;
12683 break;
12685 case BIT_IOR_EXPR:
12686 case BIT_XOR_EXPR:
12687 case BIT_AND_EXPR:
12688 integer_only_op = true;
12689 /* ... fall through ... */
12691 case VEC_COND_EXPR:
12693 case PLUS_EXPR:
12694 case MINUS_EXPR:
12695 case MULT_EXPR:
12696 case TRUNC_DIV_EXPR:
12697 case CEIL_DIV_EXPR:
12698 case FLOOR_DIV_EXPR:
12699 case ROUND_DIV_EXPR:
12700 case EXACT_DIV_EXPR:
12701 case TRUNC_MOD_EXPR:
12702 case FLOOR_MOD_EXPR:
12703 case RDIV_EXPR:
12704 case EQ_EXPR:
12705 case NE_EXPR:
12706 case LE_EXPR:
12707 case GE_EXPR:
12708 case LT_EXPR:
12709 case GT_EXPR:
12710 /* What about UNLT_EXPR? */
12711 if (VECTOR_TYPE_P (type0))
12713 ret = stv_secondarg;
12714 std::swap (type0, type1);
12715 std::swap (op0, op1);
12718 if (TREE_CODE (type0) == INTEGER_TYPE
12719 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12721 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12723 if (complain)
12724 error_at (loc, "conversion of scalar %qT to vector %qT "
12725 "involves truncation", type0, type1);
12726 return stv_error;
12728 return ret;
12730 else if (!integer_only_op
12731 /* Allow integer --> real conversion if safe. */
12732 && (TREE_CODE (type0) == REAL_TYPE
12733 || TREE_CODE (type0) == INTEGER_TYPE)
12734 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12736 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12738 if (complain)
12739 error_at (loc, "conversion of scalar %qT to vector %qT "
12740 "involves truncation", type0, type1);
12741 return stv_error;
12743 return ret;
12745 default:
12746 break;
12749 return stv_nothing;
12752 /* Return true iff ALIGN is an integral constant that is a fundamental
12753 alignment, as defined by [basic.align] in the c++-11
12754 specifications.
12756 That is:
12758 [A fundamental alignment is represented by an alignment less than or
12759 equal to the greatest alignment supported by the implementation
12760 in all contexts, which is equal to
12761 alignof(max_align_t)]. */
12763 bool
12764 cxx_fundamental_alignment_p (unsigned align)
12766 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12767 TYPE_ALIGN (long_double_type_node)));
12770 /* Return true if T is a pointer to a zero-sized aggregate. */
12772 bool
12773 pointer_to_zero_sized_aggr_p (tree t)
12775 if (!POINTER_TYPE_P (t))
12776 return false;
12777 t = TREE_TYPE (t);
12778 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12781 #include "gt-c-family-c-common.h"