Merge trunk version 222452 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob016f4f45d7c22caaaaaddf86288c7e3864c886c3
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "c-common.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "tree.h"
30 #include "tree-upc.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "calls.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "varasm.h"
37 #include "trans-mem.h"
38 #include "flags.h"
39 #include "c-pragma.h"
40 #include "c-objc.h"
41 #include "tm_p.h"
42 #include "obstack.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.h"
48 #include "toplev.h"
49 #include "diagnostic.h"
50 #include "tree-iterator.h"
51 #include "hashtab.h"
52 #include "opts.h"
53 #include "hash-map.h"
54 #include "is-a.h"
55 #include "plugin-api.h"
56 #include "vec.h"
57 #include "hash-set.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "target-def.h"
65 #include "gimplify.h"
66 #include "wide-int-print.h"
67 #include "gimple-expr.h"
69 cpp_reader *parse_in; /* Declared in c-pragma.h. */
71 /* Mode used to build pointers (VOIDmode means ptr_mode). */
73 machine_mode c_default_pointer_mode = VOIDmode;
75 /* The following symbols are subsumed in the c_global_trees array, and
76 listed here individually for documentation purposes.
78 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
80 tree short_integer_type_node;
81 tree long_integer_type_node;
82 tree long_long_integer_type_node;
84 tree short_unsigned_type_node;
85 tree long_unsigned_type_node;
86 tree long_long_unsigned_type_node;
88 tree truthvalue_type_node;
89 tree truthvalue_false_node;
90 tree truthvalue_true_node;
92 tree ptrdiff_type_node;
94 tree unsigned_char_type_node;
95 tree signed_char_type_node;
96 tree wchar_type_node;
98 tree char16_type_node;
99 tree char32_type_node;
101 tree float_type_node;
102 tree double_type_node;
103 tree long_double_type_node;
105 tree complex_integer_type_node;
106 tree complex_float_type_node;
107 tree complex_double_type_node;
108 tree complex_long_double_type_node;
110 tree dfloat32_type_node;
111 tree dfloat64_type_node;
112 tree_dfloat128_type_node;
114 tree intQI_type_node;
115 tree intHI_type_node;
116 tree intSI_type_node;
117 tree intDI_type_node;
118 tree intTI_type_node;
120 tree unsigned_intQI_type_node;
121 tree unsigned_intHI_type_node;
122 tree unsigned_intSI_type_node;
123 tree unsigned_intDI_type_node;
124 tree unsigned_intTI_type_node;
126 tree widest_integer_literal_type_node;
127 tree widest_unsigned_literal_type_node;
129 Nodes for types `void *' and `const void *'.
131 tree ptr_type_node, const_ptr_type_node;
133 Nodes for types `char *' and `const char *'.
135 tree string_type_node, const_string_type_node;
137 Type `char[SOMENUMBER]'.
138 Used when an array of char is needed and the size is irrelevant.
140 tree char_array_type_node;
142 Type `wchar_t[SOMENUMBER]' or something like it.
143 Used when a wide string literal is created.
145 tree wchar_array_type_node;
147 Type `char16_t[SOMENUMBER]' or something like it.
148 Used when a UTF-16 string literal is created.
150 tree char16_array_type_node;
152 Type `char32_t[SOMENUMBER]' or something like it.
153 Used when a UTF-32 string literal is created.
155 tree char32_array_type_node;
157 Type `int ()' -- used for implicit declaration of functions.
159 tree default_function_type;
161 A VOID_TYPE node, packaged in a TREE_LIST.
163 tree void_list_node;
165 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
166 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
167 VAR_DECLS, but C++ does.)
169 tree function_name_decl_node;
170 tree pretty_function_name_decl_node;
171 tree c99_function_name_decl_node;
173 Stack of nested function name VAR_DECLs.
175 tree saved_function_name_decls;
179 tree c_global_trees[CTI_MAX];
181 /* Switches common to the C front ends. */
183 /* Nonzero means don't output line number information. */
185 char flag_no_line_commands;
187 /* Nonzero causes -E output not to be done, but directives such as
188 #define that have side effects are still obeyed. */
190 char flag_no_output;
192 /* Nonzero means dump macros in some fashion. */
194 char flag_dump_macros;
196 /* Nonzero means pass #include lines through to the output. */
198 char flag_dump_includes;
200 /* Nonzero means process PCH files while preprocessing. */
202 bool flag_pch_preprocess;
204 /* The file name to which we should write a precompiled header, or
205 NULL if no header will be written in this compile. */
207 const char *pch_file;
209 /* Nonzero if an ISO standard was selected. It rejects macros in the
210 user's namespace. */
211 int flag_iso;
214 /* C/ObjC language option variables. */
217 /* Nonzero means allow type mismatches in conditional expressions;
218 just make their values `void'. */
220 int flag_cond_mismatch;
222 /* Nonzero means enable C89 Amendment 1 features. */
224 int flag_isoc94;
226 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
228 int flag_isoc99;
230 /* Nonzero means use the ISO C11 dialect of C. */
232 int flag_isoc11;
234 /* Nonzero means that we have builtin functions, and main is an int. */
236 int flag_hosted = 1;
239 /* ObjC language option variables. */
242 /* Tells the compiler that this is a special run. Do not perform any
243 compiling, instead we are to test some platform dependent features
244 and output a C header file with appropriate definitions. */
246 int print_struct_values;
248 /* Tells the compiler what is the constant string class for ObjC. */
250 const char *constant_string_class_name;
252 /* UPC language option variables. */
254 /* Nonzero whenever UPC -fupc-threads-N is asserted.
255 The value N gives the number of UPC threads to be
256 defined at compile-time. */
257 int flag_upc_threads;
259 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
260 int flag_upc_pthreads;
262 /* The implementation model for UPC threads that
263 are mapped to POSIX threads, specified at compilation
264 time by the -fupc-pthreads-model-* switch. */
265 upc_pthreads_model_kind upc_pthreads_model;
267 /* C++ language option variables. */
270 /* Nonzero means generate separate instantiation control files and
271 juggle them at link time. */
273 int flag_use_repository;
275 /* The C++ dialect being used. C++98 is the default. */
277 enum cxx_dialect cxx_dialect = cxx98;
279 /* Maximum template instantiation depth. This limit exists to limit the
280 time it takes to notice excessively recursive template instantiations.
282 The default is lower than the 1024 recommended by the C++0x standard
283 because G++ runs out of stack before 1024 with highly recursive template
284 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
286 int max_tinst_depth = 900;
288 /* The elements of `ridpointers' are identifier nodes for the reserved
289 type names and storage classes. It is indexed by a RID_... value. */
290 tree *ridpointers;
292 tree (*make_fname_decl) (location_t, tree, int);
294 /* Nonzero means don't warn about problems that occur when the code is
295 executed. */
296 int c_inhibit_evaluation_warnings;
298 /* Whether we are building a boolean conversion inside
299 convert_for_assignment, or some other late binary operation. If
300 build_binary_op is called for C (from code shared by C and C++) in
301 this case, then the operands have already been folded and the
302 result will not be folded again, so C_MAYBE_CONST_EXPR should not
303 be generated. */
304 bool in_late_binary_op;
306 /* Whether lexing has been completed, so subsequent preprocessor
307 errors should use the compiler's input_location. */
308 bool done_lexing = false;
310 /* Information about how a function name is generated. */
311 struct fname_var_t
313 tree *const decl; /* pointer to the VAR_DECL. */
314 const unsigned rid; /* RID number for the identifier. */
315 const int pretty; /* How pretty is it? */
318 /* The three ways of getting then name of the current function. */
320 const struct fname_var_t fname_vars[] =
322 /* C99 compliant __func__, must be first. */
323 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
324 /* GCC __FUNCTION__ compliant. */
325 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
326 /* GCC __PRETTY_FUNCTION__ compliant. */
327 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
328 {NULL, 0, 0},
331 /* Global visibility options. */
332 struct visibility_flags visibility_options;
334 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
335 static tree check_case_value (location_t, tree);
336 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
338 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
345 int, bool *);
346 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
347 int, bool *);
348 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
349 bool *);
350 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_always_inline_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_transparent_union_attribute (tree *, tree, tree,
369 int, bool *);
370 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
376 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
377 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
380 static tree handle_visibility_attribute (tree *, tree, tree, int,
381 bool *);
382 static tree handle_tls_model_attribute (tree *, tree, tree, int,
383 bool *);
384 static tree handle_no_instrument_function_attribute (tree *, tree,
385 tree, int, bool *);
386 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
389 bool *);
390 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_deprecated_attribute (tree *, tree, tree, int,
395 bool *);
396 static tree handle_vector_size_attribute (tree *, tree, tree, int,
397 bool *);
398 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
400 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
401 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
402 bool *);
403 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
404 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
406 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
407 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
408 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
409 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
410 static tree ignore_attribute (tree *, tree, tree, int, bool *);
411 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
412 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
413 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
414 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
415 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
416 bool *);
417 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
418 bool *);
419 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
420 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
421 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
422 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
424 static void check_function_nonnull (tree, int, tree *);
425 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
426 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
427 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
428 static int resort_field_decl_cmp (const void *, const void *);
430 /* Reserved words. The third field is a mask: keywords are disabled
431 if they match the mask.
433 Masks for languages:
434 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
435 C --std=c99: D_CXXONLY | D_OBJC
436 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
437 UPC is like C except that D_UPC is not set
438 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
439 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
440 ObjC++ is like C++ except that D_OBJC is not set
442 If -fno-asm is used, D_ASM is added to the mask. If
443 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
444 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
445 In C with -Wc++-compat, we warn if D_CXXWARN is set.
447 Note the complication of the D_CXX_OBJC keywords. These are
448 reserved words such as 'class'. In C++, 'class' is a reserved
449 word. In Objective-C++ it is too. In Objective-C, it is a
450 reserved word too, but only if it follows an '@' sign.
452 const struct c_common_resword c_common_reswords[] =
454 { "_Alignas", RID_ALIGNAS, D_CONLY },
455 { "_Alignof", RID_ALIGNOF, D_CONLY },
456 { "_Atomic", RID_ATOMIC, D_CONLY },
457 { "_Bool", RID_BOOL, D_CONLY },
458 { "_Complex", RID_COMPLEX, 0 },
459 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
460 { "_Cilk_sync", RID_CILK_SYNC, 0 },
461 { "_Cilk_for", RID_CILK_FOR, 0 },
462 { "_Imaginary", RID_IMAGINARY, D_CONLY },
463 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
464 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
465 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
466 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
467 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
468 { "_Sat", RID_SAT, D_CONLY | D_EXT },
469 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
470 { "_Noreturn", RID_NORETURN, D_CONLY },
471 { "_Generic", RID_GENERIC, D_CONLY },
472 { "_Thread_local", RID_THREAD, D_CONLY },
473 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
474 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
475 { "__alignof", RID_ALIGNOF, 0 },
476 { "__alignof__", RID_ALIGNOF, 0 },
477 { "__asm", RID_ASM, 0 },
478 { "__asm__", RID_ASM, 0 },
479 { "__attribute", RID_ATTRIBUTE, 0 },
480 { "__attribute__", RID_ATTRIBUTE, 0 },
481 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
482 { "__bases", RID_BASES, D_CXXONLY },
483 { "__builtin_call_with_static_chain",
484 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
485 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
486 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
487 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
488 { "__builtin_offsetof", RID_OFFSETOF, 0 },
489 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
490 { "__builtin_va_arg", RID_VA_ARG, 0 },
491 { "__complex", RID_COMPLEX, 0 },
492 { "__complex__", RID_COMPLEX, 0 },
493 { "__const", RID_CONST, 0 },
494 { "__const__", RID_CONST, 0 },
495 { "__decltype", RID_DECLTYPE, D_CXXONLY },
496 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
497 { "__extension__", RID_EXTENSION, 0 },
498 { "__func__", RID_C99_FUNCTION_NAME, 0 },
499 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
500 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
501 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
502 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
503 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
504 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
505 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
506 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
507 { "__imag", RID_IMAGPART, 0 },
508 { "__imag__", RID_IMAGPART, 0 },
509 { "__inline", RID_INLINE, 0 },
510 { "__inline__", RID_INLINE, 0 },
511 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
512 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
513 { "__is_class", RID_IS_CLASS, D_CXXONLY },
514 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
515 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
516 { "__is_final", RID_IS_FINAL, D_CXXONLY },
517 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
518 { "__is_pod", RID_IS_POD, D_CXXONLY },
519 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
520 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
521 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
522 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
523 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
524 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
525 { "__is_union", RID_IS_UNION, D_CXXONLY },
526 { "__label__", RID_LABEL, 0 },
527 { "__null", RID_NULL, 0 },
528 { "__real", RID_REALPART, 0 },
529 { "__real__", RID_REALPART, 0 },
530 { "__restrict", RID_RESTRICT, 0 },
531 { "__restrict__", RID_RESTRICT, 0 },
532 { "__signed", RID_SIGNED, 0 },
533 { "__signed__", RID_SIGNED, 0 },
534 { "__thread", RID_THREAD, 0 },
535 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
536 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
537 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
538 { "__typeof", RID_TYPEOF, 0 },
539 { "__typeof__", RID_TYPEOF, 0 },
540 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
541 { "__volatile", RID_VOLATILE, 0 },
542 { "__volatile__", RID_VOLATILE, 0 },
543 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
544 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
545 { "asm", RID_ASM, D_ASM },
546 { "auto", RID_AUTO, 0 },
547 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
548 { "break", RID_BREAK, 0 },
549 { "case", RID_CASE, 0 },
550 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
551 { "char", RID_CHAR, 0 },
552 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
553 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
554 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
555 { "const", RID_CONST, 0 },
556 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
557 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
558 { "continue", RID_CONTINUE, 0 },
559 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
560 { "default", RID_DEFAULT, 0 },
561 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
562 { "do", RID_DO, 0 },
563 { "double", RID_DOUBLE, 0 },
564 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
565 { "else", RID_ELSE, 0 },
566 { "enum", RID_ENUM, 0 },
567 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
568 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
569 { "extern", RID_EXTERN, 0 },
570 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
571 { "float", RID_FLOAT, 0 },
572 { "for", RID_FOR, 0 },
573 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
574 { "goto", RID_GOTO, 0 },
575 { "if", RID_IF, 0 },
576 { "inline", RID_INLINE, D_EXT89 },
577 { "int", RID_INT, 0 },
578 { "long", RID_LONG, 0 },
579 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
580 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
581 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
582 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
583 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
584 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
585 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
586 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
587 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
588 { "register", RID_REGISTER, 0 },
589 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
590 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
591 { "return", RID_RETURN, 0 },
592 { "short", RID_SHORT, 0 },
593 { "signed", RID_SIGNED, 0 },
594 { "sizeof", RID_SIZEOF, 0 },
595 { "static", RID_STATIC, 0 },
596 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
597 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
598 { "struct", RID_STRUCT, 0 },
599 { "switch", RID_SWITCH, 0 },
600 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
601 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
602 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
603 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
604 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
605 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
606 { "typedef", RID_TYPEDEF, 0 },
607 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
608 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
609 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
610 { "union", RID_UNION, 0 },
611 { "unsigned", RID_UNSIGNED, 0 },
612 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
613 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
614 { "void", RID_VOID, 0 },
615 { "volatile", RID_VOLATILE, 0 },
616 { "wchar_t", RID_WCHAR, D_CXXONLY },
617 { "while", RID_WHILE, 0 },
618 /* These Objective-C keywords are recognized only immediately after
619 an '@'. */
620 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
621 { "defs", RID_AT_DEFS, D_OBJC },
622 { "encode", RID_AT_ENCODE, D_OBJC },
623 { "end", RID_AT_END, D_OBJC },
624 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
625 { "interface", RID_AT_INTERFACE, D_OBJC },
626 { "protocol", RID_AT_PROTOCOL, D_OBJC },
627 { "selector", RID_AT_SELECTOR, D_OBJC },
628 { "finally", RID_AT_FINALLY, D_OBJC },
629 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
630 { "optional", RID_AT_OPTIONAL, D_OBJC },
631 { "required", RID_AT_REQUIRED, D_OBJC },
632 { "property", RID_AT_PROPERTY, D_OBJC },
633 { "package", RID_AT_PACKAGE, D_OBJC },
634 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
635 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
636 /* These are recognized only in protocol-qualifier context
637 (see above) */
638 { "bycopy", RID_BYCOPY, D_OBJC },
639 { "byref", RID_BYREF, D_OBJC },
640 { "in", RID_IN, D_OBJC },
641 { "inout", RID_INOUT, D_OBJC },
642 { "oneway", RID_ONEWAY, D_OBJC },
643 { "out", RID_OUT, D_OBJC },
645 /* UPC keywords */
646 { "shared", RID_SHARED, D_UPC },
647 { "relaxed", RID_RELAXED, D_UPC },
648 { "strict", RID_STRICT, D_UPC },
649 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
650 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
651 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
652 { "upc_forall", RID_UPC_FORALL, D_UPC },
653 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
654 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
655 { "upc_wait", RID_UPC_WAIT, D_UPC },
657 /* These are recognized inside a property attribute list */
658 { "assign", RID_ASSIGN, D_OBJC },
659 { "copy", RID_COPY, D_OBJC },
660 { "getter", RID_GETTER, D_OBJC },
661 { "nonatomic", RID_NONATOMIC, D_OBJC },
662 { "readonly", RID_READONLY, D_OBJC },
663 { "readwrite", RID_READWRITE, D_OBJC },
664 { "retain", RID_RETAIN, D_OBJC },
665 { "setter", RID_SETTER, D_OBJC },
668 const unsigned int num_c_common_reswords =
669 sizeof c_common_reswords / sizeof (struct c_common_resword);
671 /* Table of machine-independent attributes common to all C-like languages.
673 All attributes referencing arguments should be additionally processed
674 in chkp_copy_function_type_adding_bounds for correct instrumentation
675 by Pointer Bounds Checker.
676 Current list of processed common attributes: nonnull. */
677 const struct attribute_spec c_common_attribute_table[] =
679 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
680 affects_type_identity } */
681 { "packed", 0, 0, false, false, false,
682 handle_packed_attribute , false},
683 { "nocommon", 0, 0, true, false, false,
684 handle_nocommon_attribute, false},
685 { "common", 0, 0, true, false, false,
686 handle_common_attribute, false },
687 /* FIXME: logically, noreturn attributes should be listed as
688 "false, true, true" and apply to function types. But implementing this
689 would require all the places in the compiler that use TREE_THIS_VOLATILE
690 on a decl to identify non-returning functions to be located and fixed
691 to check the function type instead. */
692 { "noreturn", 0, 0, true, false, false,
693 handle_noreturn_attribute, false },
694 { "volatile", 0, 0, true, false, false,
695 handle_noreturn_attribute, false },
696 { "stack_protect", 0, 0, true, false, false,
697 handle_stack_protect_attribute, false },
698 { "noinline", 0, 0, true, false, false,
699 handle_noinline_attribute, false },
700 { "noclone", 0, 0, true, false, false,
701 handle_noclone_attribute, false },
702 { "no_icf", 0, 0, true, false, false,
703 handle_noicf_attribute, false },
704 { "leaf", 0, 0, true, false, false,
705 handle_leaf_attribute, false },
706 { "always_inline", 0, 0, true, false, false,
707 handle_always_inline_attribute, false },
708 { "gnu_inline", 0, 0, true, false, false,
709 handle_gnu_inline_attribute, false },
710 { "artificial", 0, 0, true, false, false,
711 handle_artificial_attribute, false },
712 { "flatten", 0, 0, true, false, false,
713 handle_flatten_attribute, false },
714 { "used", 0, 0, true, false, false,
715 handle_used_attribute, false },
716 { "unused", 0, 0, false, false, false,
717 handle_unused_attribute, false },
718 { "externally_visible", 0, 0, true, false, false,
719 handle_externally_visible_attribute, false },
720 { "no_reorder", 0, 0, true, false, false,
721 handle_no_reorder_attribute, false },
722 /* The same comments as for noreturn attributes apply to const ones. */
723 { "const", 0, 0, true, false, false,
724 handle_const_attribute, false },
725 { "transparent_union", 0, 0, false, false, false,
726 handle_transparent_union_attribute, false },
727 { "constructor", 0, 1, true, false, false,
728 handle_constructor_attribute, false },
729 { "destructor", 0, 1, true, false, false,
730 handle_destructor_attribute, false },
731 { "mode", 1, 1, false, true, false,
732 handle_mode_attribute, false },
733 { "section", 1, 1, true, false, false,
734 handle_section_attribute, false },
735 { "aligned", 0, 1, false, false, false,
736 handle_aligned_attribute, false },
737 { "weak", 0, 0, true, false, false,
738 handle_weak_attribute, false },
739 { "ifunc", 1, 1, true, false, false,
740 handle_ifunc_attribute, false },
741 { "alias", 1, 1, true, false, false,
742 handle_alias_attribute, false },
743 { "weakref", 0, 1, true, false, false,
744 handle_weakref_attribute, false },
745 { "no_instrument_function", 0, 0, true, false, false,
746 handle_no_instrument_function_attribute,
747 false },
748 { "malloc", 0, 0, true, false, false,
749 handle_malloc_attribute, false },
750 { "returns_twice", 0, 0, true, false, false,
751 handle_returns_twice_attribute, false },
752 { "no_stack_limit", 0, 0, true, false, false,
753 handle_no_limit_stack_attribute, false },
754 { "pure", 0, 0, true, false, false,
755 handle_pure_attribute, false },
756 { "transaction_callable", 0, 0, false, true, false,
757 handle_tm_attribute, false },
758 { "transaction_unsafe", 0, 0, false, true, false,
759 handle_tm_attribute, false },
760 { "transaction_safe", 0, 0, false, true, false,
761 handle_tm_attribute, false },
762 { "transaction_may_cancel_outer", 0, 0, false, true, false,
763 handle_tm_attribute, false },
764 /* ??? These two attributes didn't make the transition from the
765 Intel language document to the multi-vendor language document. */
766 { "transaction_pure", 0, 0, false, true, false,
767 handle_tm_attribute, false },
768 { "transaction_wrap", 1, 1, true, false, false,
769 handle_tm_wrap_attribute, false },
770 /* For internal use (marking of builtins) only. The name contains space
771 to prevent its usage in source code. */
772 { "no vops", 0, 0, true, false, false,
773 handle_novops_attribute, false },
774 { "deprecated", 0, 1, false, false, false,
775 handle_deprecated_attribute, false },
776 { "vector_size", 1, 1, false, true, false,
777 handle_vector_size_attribute, false },
778 { "visibility", 1, 1, false, false, false,
779 handle_visibility_attribute, false },
780 { "tls_model", 1, 1, true, false, false,
781 handle_tls_model_attribute, false },
782 { "nonnull", 0, -1, false, true, true,
783 handle_nonnull_attribute, false },
784 { "nothrow", 0, 0, true, false, false,
785 handle_nothrow_attribute, false },
786 { "may_alias", 0, 0, false, true, false, NULL, false },
787 { "cleanup", 1, 1, true, false, false,
788 handle_cleanup_attribute, false },
789 { "warn_unused_result", 0, 0, false, true, true,
790 handle_warn_unused_result_attribute, false },
791 { "sentinel", 0, 1, false, true, true,
792 handle_sentinel_attribute, false },
793 /* For internal use (marking of builtins) only. The name contains space
794 to prevent its usage in source code. */
795 { "type generic", 0, 0, false, true, true,
796 handle_type_generic_attribute, false },
797 { "alloc_size", 1, 2, false, true, true,
798 handle_alloc_size_attribute, false },
799 { "cold", 0, 0, true, false, false,
800 handle_cold_attribute, false },
801 { "hot", 0, 0, true, false, false,
802 handle_hot_attribute, false },
803 { "no_address_safety_analysis",
804 0, 0, true, false, false,
805 handle_no_address_safety_analysis_attribute,
806 false },
807 { "no_sanitize_address", 0, 0, true, false, false,
808 handle_no_sanitize_address_attribute,
809 false },
810 { "no_sanitize_thread", 0, 0, true, false, false,
811 handle_no_sanitize_address_attribute,
812 false },
813 { "no_sanitize_undefined", 0, 0, true, false, false,
814 handle_no_sanitize_undefined_attribute,
815 false },
816 { "warning", 1, 1, true, false, false,
817 handle_error_attribute, false },
818 { "error", 1, 1, true, false, false,
819 handle_error_attribute, false },
820 { "target", 1, -1, true, false, false,
821 handle_target_attribute, false },
822 { "optimize", 1, -1, true, false, false,
823 handle_optimize_attribute, false },
824 /* For internal use only. The leading '*' both prevents its usage in
825 source code and signals that it may be overridden by machine tables. */
826 { "*tm regparm", 0, 0, false, true, true,
827 ignore_attribute, false },
828 { "no_split_stack", 0, 0, true, false, false,
829 handle_no_split_stack_attribute, false },
830 /* For internal use (marking of builtins and runtime functions) only.
831 The name contains space to prevent its usage in source code. */
832 { "fn spec", 1, 1, false, true, true,
833 handle_fnspec_attribute, false },
834 { "warn_unused", 0, 0, false, false, false,
835 handle_warn_unused_attribute, false },
836 { "returns_nonnull", 0, 0, false, true, true,
837 handle_returns_nonnull_attribute, false },
838 { "omp declare simd", 0, -1, true, false, false,
839 handle_omp_declare_simd_attribute, false },
840 { "cilk simd function", 0, -1, true, false, false,
841 handle_omp_declare_simd_attribute, false },
842 { "omp declare target", 0, 0, true, false, false,
843 handle_omp_declare_target_attribute, false },
844 { "alloc_align", 1, 1, false, true, true,
845 handle_alloc_align_attribute, false },
846 { "assume_aligned", 1, 2, false, true, true,
847 handle_assume_aligned_attribute, false },
848 { "designated_init", 0, 0, false, true, false,
849 handle_designated_init_attribute, false },
850 { "bnd_variable_size", 0, 0, true, false, false,
851 handle_bnd_variable_size_attribute, false },
852 { "bnd_legacy", 0, 0, true, false, false,
853 handle_bnd_legacy, false },
854 { "bnd_instrument", 0, 0, true, false, false,
855 handle_bnd_instrument, false },
856 { NULL, 0, 0, false, false, false, NULL, false }
859 /* Give the specifications for the format attributes, used by C and all
860 descendants.
862 All attributes referencing arguments should be additionally processed
863 in chkp_copy_function_type_adding_bounds for correct instrumentation
864 by Pointer Bounds Checker.
865 Current list of processed format attributes: format, format_arg. */
866 const struct attribute_spec c_common_format_attribute_table[] =
868 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
869 affects_type_identity } */
870 { "format", 3, 3, false, true, true,
871 handle_format_attribute, false },
872 { "format_arg", 1, 1, false, true, true,
873 handle_format_arg_attribute, false },
874 { NULL, 0, 0, false, false, false, NULL, false }
877 /* Return identifier for address space AS. */
879 const char *
880 c_addr_space_name (addr_space_t as)
882 int rid = RID_FIRST_ADDR_SPACE + as;
883 gcc_assert (ridpointers [rid]);
884 return IDENTIFIER_POINTER (ridpointers [rid]);
887 /* Push current bindings for the function name VAR_DECLS. */
889 void
890 start_fname_decls (void)
892 unsigned ix;
893 tree saved = NULL_TREE;
895 for (ix = 0; fname_vars[ix].decl; ix++)
897 tree decl = *fname_vars[ix].decl;
899 if (decl)
901 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
902 saved);
903 *fname_vars[ix].decl = NULL_TREE;
906 if (saved || saved_function_name_decls)
907 /* Normally they'll have been NULL, so only push if we've got a
908 stack, or they are non-NULL. */
909 saved_function_name_decls = tree_cons (saved, NULL_TREE,
910 saved_function_name_decls);
913 /* Finish up the current bindings, adding them into the current function's
914 statement tree. This must be done _before_ finish_stmt_tree is called.
915 If there is no current function, we must be at file scope and no statements
916 are involved. Pop the previous bindings. */
918 void
919 finish_fname_decls (void)
921 unsigned ix;
922 tree stmts = NULL_TREE;
923 tree stack = saved_function_name_decls;
925 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
926 append_to_statement_list (TREE_VALUE (stack), &stmts);
928 if (stmts)
930 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
932 if (TREE_CODE (*bodyp) == BIND_EXPR)
933 bodyp = &BIND_EXPR_BODY (*bodyp);
935 append_to_statement_list_force (*bodyp, &stmts);
936 *bodyp = stmts;
939 for (ix = 0; fname_vars[ix].decl; ix++)
940 *fname_vars[ix].decl = NULL_TREE;
942 if (stack)
944 /* We had saved values, restore them. */
945 tree saved;
947 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
949 tree decl = TREE_PURPOSE (saved);
950 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
952 *fname_vars[ix].decl = decl;
954 stack = TREE_CHAIN (stack);
956 saved_function_name_decls = stack;
959 /* Return the text name of the current function, suitably prettified
960 by PRETTY_P. Return string must be freed by caller. */
962 const char *
963 fname_as_string (int pretty_p)
965 const char *name = "top level";
966 char *namep;
967 int vrb = 2, len;
968 cpp_string cstr = { 0, 0 }, strname;
970 if (!pretty_p)
972 name = "";
973 vrb = 0;
976 if (current_function_decl)
977 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
979 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
981 namep = XNEWVEC (char, len);
982 snprintf (namep, len, "\"%s\"", name);
983 strname.text = (unsigned char *) namep;
984 strname.len = len - 1;
986 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
988 XDELETEVEC (namep);
989 return (const char *) cstr.text;
992 return namep;
995 /* Return the VAR_DECL for a const char array naming the current
996 function. If the VAR_DECL has not yet been created, create it
997 now. RID indicates how it should be formatted and IDENTIFIER_NODE
998 ID is its name (unfortunately C and C++ hold the RID values of
999 keywords in different places, so we can't derive RID from ID in
1000 this language independent code. LOC is the location of the
1001 function. */
1003 tree
1004 fname_decl (location_t loc, unsigned int rid, tree id)
1006 unsigned ix;
1007 tree decl = NULL_TREE;
1009 for (ix = 0; fname_vars[ix].decl; ix++)
1010 if (fname_vars[ix].rid == rid)
1011 break;
1013 decl = *fname_vars[ix].decl;
1014 if (!decl)
1016 /* If a tree is built here, it would normally have the lineno of
1017 the current statement. Later this tree will be moved to the
1018 beginning of the function and this line number will be wrong.
1019 To avoid this problem set the lineno to 0 here; that prevents
1020 it from appearing in the RTL. */
1021 tree stmts;
1022 location_t saved_location = input_location;
1023 input_location = UNKNOWN_LOCATION;
1025 stmts = push_stmt_list ();
1026 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1027 stmts = pop_stmt_list (stmts);
1028 if (!IS_EMPTY_STMT (stmts))
1029 saved_function_name_decls
1030 = tree_cons (decl, stmts, saved_function_name_decls);
1031 *fname_vars[ix].decl = decl;
1032 input_location = saved_location;
1034 if (!ix && !current_function_decl)
1035 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1037 return decl;
1040 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1042 tree
1043 fix_string_type (tree value)
1045 int length = TREE_STRING_LENGTH (value);
1046 int nchars;
1047 tree e_type, i_type, a_type;
1049 /* Compute the number of elements, for the array type. */
1050 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1052 nchars = length;
1053 e_type = char_type_node;
1055 else if (TREE_TYPE (value) == char16_array_type_node)
1057 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1058 e_type = char16_type_node;
1060 else if (TREE_TYPE (value) == char32_array_type_node)
1062 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1063 e_type = char32_type_node;
1065 else
1067 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1068 e_type = wchar_type_node;
1071 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1072 limit in C++98 Annex B is very large (65536) and is not normative,
1073 so we do not diagnose it (warn_overlength_strings is forced off
1074 in c_common_post_options). */
1075 if (warn_overlength_strings)
1077 const int nchars_max = flag_isoc99 ? 4095 : 509;
1078 const int relevant_std = flag_isoc99 ? 99 : 90;
1079 if (nchars - 1 > nchars_max)
1080 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1081 separate the %d from the 'C'. 'ISO' should not be
1082 translated, but it may be moved after 'C%d' in languages
1083 where modifiers follow nouns. */
1084 pedwarn (input_location, OPT_Woverlength_strings,
1085 "string length %qd is greater than the length %qd "
1086 "ISO C%d compilers are required to support",
1087 nchars - 1, nchars_max, relevant_std);
1090 /* Create the array type for the string constant. The ISO C++
1091 standard says that a string literal has type `const char[N]' or
1092 `const wchar_t[N]'. We use the same logic when invoked as a C
1093 front-end with -Wwrite-strings.
1094 ??? We should change the type of an expression depending on the
1095 state of a warning flag. We should just be warning -- see how
1096 this is handled in the C++ front-end for the deprecated implicit
1097 conversion from string literals to `char*' or `wchar_t*'.
1099 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1100 array type being the unqualified version of that type.
1101 Therefore, if we are constructing an array of const char, we must
1102 construct the matching unqualified array type first. The C front
1103 end does not require this, but it does no harm, so we do it
1104 unconditionally. */
1105 i_type = build_index_type (size_int (nchars - 1));
1106 a_type = build_array_type (e_type, i_type);
1107 if (c_dialect_cxx() || warn_write_strings)
1108 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1110 TREE_TYPE (value) = a_type;
1111 TREE_CONSTANT (value) = 1;
1112 TREE_READONLY (value) = 1;
1113 TREE_STATIC (value) = 1;
1114 return value;
1117 /* If DISABLE is true, stop issuing warnings. This is used when
1118 parsing code that we know will not be executed. This function may
1119 be called multiple times, and works as a stack. */
1121 static void
1122 c_disable_warnings (bool disable)
1124 if (disable)
1126 ++c_inhibit_evaluation_warnings;
1127 fold_defer_overflow_warnings ();
1131 /* If ENABLE is true, reenable issuing warnings. */
1133 static void
1134 c_enable_warnings (bool enable)
1136 if (enable)
1138 --c_inhibit_evaluation_warnings;
1139 fold_undefer_and_ignore_overflow_warnings ();
1143 /* Fully fold EXPR, an expression that was not folded (beyond integer
1144 constant expressions and null pointer constants) when being built
1145 up. If IN_INIT, this is in a static initializer and certain
1146 changes are made to the folding done. Clear *MAYBE_CONST if
1147 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1148 expression because it contains an evaluated operator (in C99) or an
1149 operator outside of sizeof returning an integer constant (in C90)
1150 not permitted in constant expressions, or because it contains an
1151 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1152 set to true by callers before calling this function.) Return the
1153 folded expression. Function arguments have already been folded
1154 before calling this function, as have the contents of SAVE_EXPR,
1155 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1156 C_MAYBE_CONST_EXPR. */
1158 tree
1159 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1161 tree ret;
1162 tree eptype = NULL_TREE;
1163 bool dummy = true;
1164 bool maybe_const_itself = true;
1165 location_t loc = EXPR_LOCATION (expr);
1167 /* This function is not relevant to C++ because C++ folds while
1168 parsing, and may need changes to be correct for C++ when C++
1169 stops folding while parsing. */
1170 if (c_dialect_cxx ())
1171 gcc_unreachable ();
1173 if (!maybe_const)
1174 maybe_const = &dummy;
1175 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1177 eptype = TREE_TYPE (expr);
1178 expr = TREE_OPERAND (expr, 0);
1180 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1181 &maybe_const_itself);
1182 if (eptype)
1183 ret = fold_convert_loc (loc, eptype, ret);
1184 *maybe_const &= maybe_const_itself;
1185 return ret;
1188 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1189 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1190 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1191 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1192 both evaluated and unevaluated subexpressions while
1193 *MAYBE_CONST_ITSELF is carried from only evaluated
1194 subexpressions). */
1196 static tree
1197 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1198 bool *maybe_const_itself)
1200 tree ret = expr;
1201 enum tree_code code = TREE_CODE (expr);
1202 enum tree_code_class kind = TREE_CODE_CLASS (code);
1203 location_t loc = EXPR_LOCATION (expr);
1204 tree op0, op1, op2, op3;
1205 tree orig_op0, orig_op1, orig_op2;
1206 bool op0_const = true, op1_const = true, op2_const = true;
1207 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1208 bool nowarning = TREE_NO_WARNING (expr);
1209 bool unused_p;
1211 /* This function is not relevant to C++ because C++ folds while
1212 parsing, and may need changes to be correct for C++ when C++
1213 stops folding while parsing. */
1214 if (c_dialect_cxx ())
1215 gcc_unreachable ();
1217 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1218 anything else not counted as an expression cannot usefully be
1219 folded further at this point. */
1220 if (!IS_EXPR_CODE_CLASS (kind)
1221 || kind == tcc_statement
1222 || code == SAVE_EXPR)
1223 return expr;
1225 /* Operands of variable-length expressions (function calls) have
1226 already been folded, as have __builtin_* function calls, and such
1227 expressions cannot occur in constant expressions. */
1228 if (kind == tcc_vl_exp)
1230 *maybe_const_operands = false;
1231 ret = fold (expr);
1232 goto out;
1235 if (code == C_MAYBE_CONST_EXPR)
1237 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1238 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1239 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1240 *maybe_const_operands = false;
1241 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1242 *maybe_const_itself = false;
1243 if (pre && !in_init)
1244 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1245 else
1246 ret = inner;
1247 goto out;
1250 /* Assignment, increment, decrement, function call and comma
1251 operators, and statement expressions, cannot occur in constant
1252 expressions if evaluated / outside of sizeof. (Function calls
1253 were handled above, though VA_ARG_EXPR is treated like a function
1254 call here, and statement expressions are handled through
1255 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1256 switch (code)
1258 case MODIFY_EXPR:
1259 case PREDECREMENT_EXPR:
1260 case PREINCREMENT_EXPR:
1261 case POSTDECREMENT_EXPR:
1262 case POSTINCREMENT_EXPR:
1263 case COMPOUND_EXPR:
1264 *maybe_const_operands = false;
1265 break;
1267 case VA_ARG_EXPR:
1268 case TARGET_EXPR:
1269 case BIND_EXPR:
1270 case OBJ_TYPE_REF:
1271 *maybe_const_operands = false;
1272 ret = fold (expr);
1273 goto out;
1275 default:
1276 break;
1279 /* Fold individual tree codes as appropriate. */
1280 switch (code)
1282 case COMPOUND_LITERAL_EXPR:
1283 /* Any non-constancy will have been marked in a containing
1284 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1285 goto out;
1287 case COMPONENT_REF:
1288 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1289 op1 = TREE_OPERAND (expr, 1);
1290 op2 = TREE_OPERAND (expr, 2);
1291 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1292 maybe_const_itself);
1293 STRIP_TYPE_NOPS (op0);
1294 if (op0 != orig_op0)
1295 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1296 if (ret != expr)
1298 TREE_READONLY (ret) = TREE_READONLY (expr);
1299 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1301 goto out;
1303 case ARRAY_REF:
1304 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1305 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1306 op2 = TREE_OPERAND (expr, 2);
1307 op3 = TREE_OPERAND (expr, 3);
1308 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1309 maybe_const_itself);
1310 STRIP_TYPE_NOPS (op0);
1311 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1312 maybe_const_itself);
1313 STRIP_TYPE_NOPS (op1);
1314 op1 = decl_constant_value_for_optimization (op1);
1315 if (op0 != orig_op0 || op1 != orig_op1)
1316 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1317 if (ret != expr)
1319 TREE_READONLY (ret) = TREE_READONLY (expr);
1320 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1321 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1323 ret = fold (ret);
1324 goto out;
1326 case COMPOUND_EXPR:
1327 case MODIFY_EXPR:
1328 case PREDECREMENT_EXPR:
1329 case PREINCREMENT_EXPR:
1330 case POSTDECREMENT_EXPR:
1331 case POSTINCREMENT_EXPR:
1332 case PLUS_EXPR:
1333 case MINUS_EXPR:
1334 case MULT_EXPR:
1335 case POINTER_PLUS_EXPR:
1336 case TRUNC_DIV_EXPR:
1337 case CEIL_DIV_EXPR:
1338 case FLOOR_DIV_EXPR:
1339 case TRUNC_MOD_EXPR:
1340 case RDIV_EXPR:
1341 case EXACT_DIV_EXPR:
1342 case LSHIFT_EXPR:
1343 case RSHIFT_EXPR:
1344 case BIT_IOR_EXPR:
1345 case BIT_XOR_EXPR:
1346 case BIT_AND_EXPR:
1347 case LT_EXPR:
1348 case LE_EXPR:
1349 case GT_EXPR:
1350 case GE_EXPR:
1351 case EQ_EXPR:
1352 case NE_EXPR:
1353 case COMPLEX_EXPR:
1354 case TRUTH_AND_EXPR:
1355 case TRUTH_OR_EXPR:
1356 case TRUTH_XOR_EXPR:
1357 case UNORDERED_EXPR:
1358 case ORDERED_EXPR:
1359 case UNLT_EXPR:
1360 case UNLE_EXPR:
1361 case UNGT_EXPR:
1362 case UNGE_EXPR:
1363 case UNEQ_EXPR:
1364 /* Binary operations evaluating both arguments (increment and
1365 decrement are binary internally in GCC). */
1366 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1367 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1368 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1369 maybe_const_itself);
1370 STRIP_TYPE_NOPS (op0);
1371 if (code != MODIFY_EXPR
1372 && code != PREDECREMENT_EXPR
1373 && code != PREINCREMENT_EXPR
1374 && code != POSTDECREMENT_EXPR
1375 && code != POSTINCREMENT_EXPR)
1376 op0 = decl_constant_value_for_optimization (op0);
1377 /* The RHS of a MODIFY_EXPR was fully folded when building that
1378 expression for the sake of conversion warnings. */
1379 if (code != MODIFY_EXPR)
1380 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1381 maybe_const_itself);
1382 STRIP_TYPE_NOPS (op1);
1383 op1 = decl_constant_value_for_optimization (op1);
1384 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1385 ret = in_init
1386 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1387 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1388 else
1389 ret = fold (expr);
1390 if (TREE_OVERFLOW_P (ret)
1391 && !TREE_OVERFLOW_P (op0)
1392 && !TREE_OVERFLOW_P (op1))
1393 overflow_warning (EXPR_LOCATION (expr), ret);
1394 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1395 && TREE_CODE (orig_op1) != INTEGER_CST
1396 && TREE_CODE (op1) == INTEGER_CST
1397 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1398 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1399 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1400 && c_inhibit_evaluation_warnings == 0)
1402 if (tree_int_cst_sgn (op1) < 0)
1403 warning_at (loc, OPT_Wshift_count_negative,
1404 (code == LSHIFT_EXPR
1405 ? G_("left shift count is negative")
1406 : G_("right shift count is negative")));
1407 else if (compare_tree_int (op1,
1408 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1409 >= 0)
1410 warning_at (loc, OPT_Wshift_count_overflow,
1411 (code == LSHIFT_EXPR
1412 ? G_("left shift count >= width of type")
1413 : G_("right shift count >= width of type")));
1415 if ((code == TRUNC_DIV_EXPR
1416 || code == CEIL_DIV_EXPR
1417 || code == FLOOR_DIV_EXPR
1418 || code == EXACT_DIV_EXPR
1419 || code == TRUNC_MOD_EXPR)
1420 && TREE_CODE (orig_op1) != INTEGER_CST
1421 && TREE_CODE (op1) == INTEGER_CST
1422 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1423 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1424 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1425 warn_for_div_by_zero (loc, op1);
1426 goto out;
1428 case INDIRECT_REF:
1429 case FIX_TRUNC_EXPR:
1430 case FLOAT_EXPR:
1431 CASE_CONVERT:
1432 case ADDR_SPACE_CONVERT_EXPR:
1433 case VIEW_CONVERT_EXPR:
1434 case NON_LVALUE_EXPR:
1435 case NEGATE_EXPR:
1436 case BIT_NOT_EXPR:
1437 case TRUTH_NOT_EXPR:
1438 case ADDR_EXPR:
1439 case CONJ_EXPR:
1440 case REALPART_EXPR:
1441 case IMAGPART_EXPR:
1442 /* Unary operations. */
1443 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1444 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1445 maybe_const_itself);
1446 STRIP_TYPE_NOPS (op0);
1447 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1448 op0 = decl_constant_value_for_optimization (op0);
1449 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1450 not prepared to deal with them if they occur in initializers.
1451 Avoid attempts to fold references to UPC shared components
1452 due to the complexities of UPC pointer-to-shared arithmetic. */
1453 if (op0 != orig_op0
1454 && code == ADDR_EXPR
1455 && (op1 = get_base_address (op0)) != NULL_TREE
1456 && TREE_CODE (op1) == INDIRECT_REF
1457 && !upc_shared_type_p (TREE_TYPE (op1))
1458 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1459 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1460 else if (op0 != orig_op0 || in_init)
1461 ret = in_init
1462 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1463 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1464 else
1465 ret = fold (expr);
1466 if (code == INDIRECT_REF
1467 && ret != expr
1468 && TREE_CODE (ret) == INDIRECT_REF)
1470 TREE_READONLY (ret) = TREE_READONLY (expr);
1471 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1472 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1474 switch (code)
1476 case FIX_TRUNC_EXPR:
1477 case FLOAT_EXPR:
1478 CASE_CONVERT:
1479 /* Don't warn about explicit conversions. We will already
1480 have warned about suspect implicit conversions. */
1481 break;
1483 default:
1484 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1485 overflow_warning (EXPR_LOCATION (expr), ret);
1486 break;
1488 goto out;
1490 case TRUTH_ANDIF_EXPR:
1491 case TRUTH_ORIF_EXPR:
1492 /* Binary operations not necessarily evaluating both
1493 arguments. */
1494 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1495 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1496 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1497 STRIP_TYPE_NOPS (op0);
1499 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1500 ? truthvalue_false_node
1501 : truthvalue_true_node));
1502 c_disable_warnings (unused_p);
1503 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1504 STRIP_TYPE_NOPS (op1);
1505 c_enable_warnings (unused_p);
1507 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1508 ret = in_init
1509 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1510 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1511 else
1512 ret = fold (expr);
1513 *maybe_const_operands &= op0_const;
1514 *maybe_const_itself &= op0_const_self;
1515 if (!(flag_isoc99
1516 && op0_const
1517 && op0_const_self
1518 && (code == TRUTH_ANDIF_EXPR
1519 ? op0 == truthvalue_false_node
1520 : op0 == truthvalue_true_node)))
1521 *maybe_const_operands &= op1_const;
1522 if (!(op0_const
1523 && op0_const_self
1524 && (code == TRUTH_ANDIF_EXPR
1525 ? op0 == truthvalue_false_node
1526 : op0 == truthvalue_true_node)))
1527 *maybe_const_itself &= op1_const_self;
1528 goto out;
1530 case COND_EXPR:
1531 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1532 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1533 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1534 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1536 STRIP_TYPE_NOPS (op0);
1537 c_disable_warnings (op0 == truthvalue_false_node);
1538 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
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 STRIP_TYPE_NOPS (op2);
1545 c_enable_warnings (op0 == truthvalue_true_node);
1547 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1548 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1549 else
1550 ret = fold (expr);
1551 *maybe_const_operands &= op0_const;
1552 *maybe_const_itself &= op0_const_self;
1553 if (!(flag_isoc99
1554 && op0_const
1555 && op0_const_self
1556 && op0 == truthvalue_false_node))
1557 *maybe_const_operands &= op1_const;
1558 if (!(op0_const
1559 && op0_const_self
1560 && op0 == truthvalue_false_node))
1561 *maybe_const_itself &= op1_const_self;
1562 if (!(flag_isoc99
1563 && op0_const
1564 && op0_const_self
1565 && op0 == truthvalue_true_node))
1566 *maybe_const_operands &= op2_const;
1567 if (!(op0_const
1568 && op0_const_self
1569 && op0 == truthvalue_true_node))
1570 *maybe_const_itself &= op2_const_self;
1571 goto out;
1573 case EXCESS_PRECISION_EXPR:
1574 /* Each case where an operand with excess precision may be
1575 encountered must remove the EXCESS_PRECISION_EXPR around
1576 inner operands and possibly put one around the whole
1577 expression or possibly convert to the semantic type (which
1578 c_fully_fold does); we cannot tell at this stage which is
1579 appropriate in any particular case. */
1580 gcc_unreachable ();
1582 default:
1583 /* Various codes may appear through folding built-in functions
1584 and their arguments. */
1585 goto out;
1588 out:
1589 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1590 have been done by this point, so remove them again. */
1591 nowarning |= TREE_NO_WARNING (ret);
1592 STRIP_TYPE_NOPS (ret);
1593 if (nowarning && !TREE_NO_WARNING (ret))
1595 if (!CAN_HAVE_LOCATION_P (ret))
1596 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1597 TREE_NO_WARNING (ret) = 1;
1599 if (ret != expr)
1600 protected_set_expr_location (ret, loc);
1601 return ret;
1604 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1605 return EXP. Otherwise, return either EXP or its known constant
1606 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1607 Is the BLKmode test appropriate? */
1609 tree
1610 decl_constant_value_for_optimization (tree exp)
1612 tree ret;
1614 /* This function is only used by C, for c_fully_fold and other
1615 optimization, and may not be correct for C++. */
1616 if (c_dialect_cxx ())
1617 gcc_unreachable ();
1619 if (!optimize
1620 || TREE_CODE (exp) != VAR_DECL
1621 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1622 || DECL_MODE (exp) == BLKmode)
1623 return exp;
1625 ret = decl_constant_value (exp);
1626 /* Avoid unwanted tree sharing between the initializer and current
1627 function's body where the tree can be modified e.g. by the
1628 gimplifier. */
1629 if (ret != exp && TREE_STATIC (exp))
1630 ret = unshare_expr (ret);
1631 return ret;
1634 /* Print a warning if a constant expression had overflow in folding.
1635 Invoke this function on every expression that the language
1636 requires to be a constant expression.
1637 Note the ANSI C standard says it is erroneous for a
1638 constant expression to overflow. */
1640 void
1641 constant_expression_warning (tree value)
1643 if (warn_overflow && pedantic
1644 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1645 || TREE_CODE (value) == FIXED_CST
1646 || TREE_CODE (value) == VECTOR_CST
1647 || TREE_CODE (value) == COMPLEX_CST)
1648 && TREE_OVERFLOW (value))
1649 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1652 /* The same as above but print an unconditional error. */
1653 void
1654 constant_expression_error (tree value)
1656 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1657 || TREE_CODE (value) == FIXED_CST
1658 || TREE_CODE (value) == VECTOR_CST
1659 || TREE_CODE (value) == COMPLEX_CST)
1660 && TREE_OVERFLOW (value))
1661 error ("overflow in constant expression");
1664 /* Print a warning if an expression had overflow in folding and its
1665 operands hadn't.
1667 Invoke this function on every expression that
1668 (1) appears in the source code, and
1669 (2) is a constant expression that overflowed, and
1670 (3) is not already checked by convert_and_check;
1671 however, do not invoke this function on operands of explicit casts
1672 or when the expression is the result of an operator and any operand
1673 already overflowed. */
1675 void
1676 overflow_warning (location_t loc, tree value)
1678 if (c_inhibit_evaluation_warnings != 0)
1679 return;
1681 switch (TREE_CODE (value))
1683 case INTEGER_CST:
1684 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1685 break;
1687 case REAL_CST:
1688 warning_at (loc, OPT_Woverflow,
1689 "floating point overflow in expression");
1690 break;
1692 case FIXED_CST:
1693 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1694 break;
1696 case VECTOR_CST:
1697 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1698 break;
1700 case COMPLEX_CST:
1701 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1702 warning_at (loc, OPT_Woverflow,
1703 "complex integer overflow in expression");
1704 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1705 warning_at (loc, OPT_Woverflow,
1706 "complex floating point overflow in expression");
1707 break;
1709 default:
1710 break;
1714 /* Warn about uses of logical || / && operator in a context where it
1715 is likely that the bitwise equivalent was intended by the
1716 programmer. We have seen an expression in which CODE is a binary
1717 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1718 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1719 void
1720 warn_logical_operator (location_t location, enum tree_code code, tree type,
1721 enum tree_code code_left, tree op_left,
1722 enum tree_code ARG_UNUSED (code_right), tree op_right)
1724 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1725 int in0_p, in1_p, in_p;
1726 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1727 bool strict_overflow_p = false;
1729 if (code != TRUTH_ANDIF_EXPR
1730 && code != TRUTH_AND_EXPR
1731 && code != TRUTH_ORIF_EXPR
1732 && code != TRUTH_OR_EXPR)
1733 return;
1735 /* We don't want to warn if either operand comes from a macro
1736 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1737 see PR61534. */
1738 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1739 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1740 return;
1742 /* Warn if &&/|| are being used in a context where it is
1743 likely that the bitwise equivalent was intended by the
1744 programmer. That is, an expression such as op && MASK
1745 where op should not be any boolean expression, nor a
1746 constant, and mask seems to be a non-boolean integer constant. */
1747 if (!truth_value_p (code_left)
1748 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1749 && !CONSTANT_CLASS_P (op_left)
1750 && !TREE_NO_WARNING (op_left)
1751 && TREE_CODE (op_right) == INTEGER_CST
1752 && !integer_zerop (op_right)
1753 && !integer_onep (op_right))
1755 if (or_op)
1756 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1757 " applied to non-boolean constant");
1758 else
1759 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1760 " applied to non-boolean constant");
1761 TREE_NO_WARNING (op_left) = true;
1762 return;
1765 /* We do not warn for constants because they are typical of macro
1766 expansions that test for features. */
1767 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1768 return;
1770 /* This warning only makes sense with logical operands. */
1771 if (!(truth_value_p (TREE_CODE (op_left))
1772 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1773 || !(truth_value_p (TREE_CODE (op_right))
1774 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1775 return;
1777 /* The range computations only work with scalars. */
1778 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1779 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1780 return;
1782 /* We first test whether either side separately is trivially true
1783 (with OR) or trivially false (with AND). If so, do not warn.
1784 This is a common idiom for testing ranges of data types in
1785 portable code. */
1786 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1787 if (!lhs)
1788 return;
1789 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1790 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1792 /* If this is an OR operation, invert both sides; now, the result
1793 should be always false to get a warning. */
1794 if (or_op)
1795 in0_p = !in0_p;
1797 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1798 if (tem && integer_zerop (tem))
1799 return;
1801 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1802 if (!rhs)
1803 return;
1804 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1805 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1807 /* If this is an OR operation, invert both sides; now, the result
1808 should be always false to get a warning. */
1809 if (or_op)
1810 in1_p = !in1_p;
1812 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1813 if (tem && integer_zerop (tem))
1814 return;
1816 /* If both expressions have the same operand, if we can merge the
1817 ranges, ... */
1818 if (operand_equal_p (lhs, rhs, 0)
1819 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1820 in1_p, low1, high1))
1822 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1823 /* ... and if the range test is always false, then warn. */
1824 if (tem && integer_zerop (tem))
1826 if (or_op)
1827 warning_at (location, OPT_Wlogical_op,
1828 "logical %<or%> of collectively exhaustive tests is "
1829 "always true");
1830 else
1831 warning_at (location, OPT_Wlogical_op,
1832 "logical %<and%> of mutually exclusive tests is "
1833 "always false");
1835 /* Or warn if the operands have exactly the same range, e.g.
1836 A > 0 && A > 0. */
1837 else if (low0 == low1 && high0 == high1)
1839 if (or_op)
1840 warning_at (location, OPT_Wlogical_op,
1841 "logical %<or%> of equal expressions");
1842 else
1843 warning_at (location, OPT_Wlogical_op,
1844 "logical %<and%> of equal expressions");
1849 /* Warn about logical not used on the left hand side operand of a comparison.
1850 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1851 Do not warn if RHS is of a boolean type. */
1853 void
1854 warn_logical_not_parentheses (location_t location, enum tree_code code,
1855 tree rhs)
1857 if (TREE_CODE_CLASS (code) != tcc_comparison
1858 || TREE_TYPE (rhs) == NULL_TREE
1859 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1860 return;
1862 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1863 !(x == 0) or !(y != 0). */
1864 if ((code == EQ_EXPR || code == NE_EXPR)
1865 && integer_zerop (rhs))
1866 return;
1868 warning_at (location, OPT_Wlogical_not_parentheses,
1869 "logical not is only applied to the left hand side of "
1870 "comparison");
1873 /* Warn if EXP contains any computations whose results are not used.
1874 Return true if a warning is printed; false otherwise. LOCUS is the
1875 (potential) location of the expression. */
1877 bool
1878 warn_if_unused_value (const_tree exp, location_t locus)
1880 restart:
1881 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1882 return false;
1884 /* Don't warn about void constructs. This includes casting to void,
1885 void function calls, and statement expressions with a final cast
1886 to void. */
1887 if (VOID_TYPE_P (TREE_TYPE (exp)))
1888 return false;
1890 if (EXPR_HAS_LOCATION (exp))
1891 locus = EXPR_LOCATION (exp);
1893 switch (TREE_CODE (exp))
1895 case PREINCREMENT_EXPR:
1896 case POSTINCREMENT_EXPR:
1897 case PREDECREMENT_EXPR:
1898 case POSTDECREMENT_EXPR:
1899 case MODIFY_EXPR:
1900 case INIT_EXPR:
1901 case TARGET_EXPR:
1902 case CALL_EXPR:
1903 case TRY_CATCH_EXPR:
1904 case WITH_CLEANUP_EXPR:
1905 case EXIT_EXPR:
1906 case VA_ARG_EXPR:
1907 return false;
1909 case BIND_EXPR:
1910 /* For a binding, warn if no side effect within it. */
1911 exp = BIND_EXPR_BODY (exp);
1912 goto restart;
1914 case SAVE_EXPR:
1915 case NON_LVALUE_EXPR:
1916 case NOP_EXPR:
1917 exp = TREE_OPERAND (exp, 0);
1918 goto restart;
1920 case TRUTH_ORIF_EXPR:
1921 case TRUTH_ANDIF_EXPR:
1922 /* In && or ||, warn if 2nd operand has no side effect. */
1923 exp = TREE_OPERAND (exp, 1);
1924 goto restart;
1926 case COMPOUND_EXPR:
1927 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1928 return true;
1929 /* Let people do `(foo (), 0)' without a warning. */
1930 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1931 return false;
1932 exp = TREE_OPERAND (exp, 1);
1933 goto restart;
1935 case COND_EXPR:
1936 /* If this is an expression with side effects, don't warn; this
1937 case commonly appears in macro expansions. */
1938 if (TREE_SIDE_EFFECTS (exp))
1939 return false;
1940 goto warn;
1942 case INDIRECT_REF:
1943 /* Don't warn about automatic dereferencing of references, since
1944 the user cannot control it. */
1945 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1947 exp = TREE_OPERAND (exp, 0);
1948 goto restart;
1950 /* Fall through. */
1952 default:
1953 /* Referencing a volatile value is a side effect, so don't warn. */
1954 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1955 && TREE_THIS_VOLATILE (exp))
1956 return false;
1958 /* If this is an expression which has no operands, there is no value
1959 to be unused. There are no such language-independent codes,
1960 but front ends may define such. */
1961 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1962 return false;
1964 warn:
1965 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1970 /* Print a warning about casts that might indicate violation
1971 of strict aliasing rules if -Wstrict-aliasing is used and
1972 strict aliasing mode is in effect. OTYPE is the original
1973 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1975 bool
1976 strict_aliasing_warning (tree otype, tree type, tree expr)
1978 /* Strip pointer conversion chains and get to the correct original type. */
1979 STRIP_NOPS (expr);
1980 otype = TREE_TYPE (expr);
1982 if (!(flag_strict_aliasing
1983 && POINTER_TYPE_P (type)
1984 && POINTER_TYPE_P (otype)
1985 && !VOID_TYPE_P (TREE_TYPE (type)))
1986 /* If the type we are casting to is a ref-all pointer
1987 dereferencing it is always valid. */
1988 || TYPE_REF_CAN_ALIAS_ALL (type))
1989 return false;
1991 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1992 && (DECL_P (TREE_OPERAND (expr, 0))
1993 || handled_component_p (TREE_OPERAND (expr, 0))))
1995 /* Casting the address of an object to non void pointer. Warn
1996 if the cast breaks type based aliasing. */
1997 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1999 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2000 "might break strict-aliasing rules");
2001 return true;
2003 else
2005 /* warn_strict_aliasing >= 3. This includes the default (3).
2006 Only warn if the cast is dereferenced immediately. */
2007 alias_set_type set1 =
2008 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
2009 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2011 if (set1 != set2 && set2 != 0
2012 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
2014 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2015 "pointer will break strict-aliasing rules");
2016 return true;
2018 else if (warn_strict_aliasing == 2
2019 && !alias_sets_must_conflict_p (set1, set2))
2021 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2022 "pointer might break strict-aliasing rules");
2023 return true;
2027 else
2028 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2030 /* At this level, warn for any conversions, even if an address is
2031 not taken in the same statement. This will likely produce many
2032 false positives, but could be useful to pinpoint problems that
2033 are not revealed at higher levels. */
2034 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2035 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2036 if (!COMPLETE_TYPE_P (type)
2037 || !alias_sets_must_conflict_p (set1, set2))
2039 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2040 "pointer might break strict-aliasing rules");
2041 return true;
2045 return false;
2048 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2049 sizeof as last operand of certain builtins. */
2051 void
2052 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2053 vec<tree, va_gc> *params, tree *sizeof_arg,
2054 bool (*comp_types) (tree, tree))
2056 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2057 bool strop = false, cmp = false;
2058 unsigned int idx = ~0;
2059 location_t loc;
2061 if (TREE_CODE (callee) != FUNCTION_DECL
2062 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2063 || vec_safe_length (params) <= 1)
2064 return;
2066 switch (DECL_FUNCTION_CODE (callee))
2068 case BUILT_IN_STRNCMP:
2069 case BUILT_IN_STRNCASECMP:
2070 cmp = true;
2071 /* FALLTHRU */
2072 case BUILT_IN_STRNCPY:
2073 case BUILT_IN_STRNCPY_CHK:
2074 case BUILT_IN_STRNCAT:
2075 case BUILT_IN_STRNCAT_CHK:
2076 case BUILT_IN_STPNCPY:
2077 case BUILT_IN_STPNCPY_CHK:
2078 strop = true;
2079 /* FALLTHRU */
2080 case BUILT_IN_MEMCPY:
2081 case BUILT_IN_MEMCPY_CHK:
2082 case BUILT_IN_MEMMOVE:
2083 case BUILT_IN_MEMMOVE_CHK:
2084 if (params->length () < 3)
2085 return;
2086 src = (*params)[1];
2087 dest = (*params)[0];
2088 idx = 2;
2089 break;
2090 case BUILT_IN_BCOPY:
2091 if (params->length () < 3)
2092 return;
2093 src = (*params)[0];
2094 dest = (*params)[1];
2095 idx = 2;
2096 break;
2097 case BUILT_IN_MEMCMP:
2098 case BUILT_IN_BCMP:
2099 if (params->length () < 3)
2100 return;
2101 src = (*params)[1];
2102 dest = (*params)[0];
2103 idx = 2;
2104 cmp = true;
2105 break;
2106 case BUILT_IN_MEMSET:
2107 case BUILT_IN_MEMSET_CHK:
2108 if (params->length () < 3)
2109 return;
2110 dest = (*params)[0];
2111 idx = 2;
2112 break;
2113 case BUILT_IN_BZERO:
2114 dest = (*params)[0];
2115 idx = 1;
2116 break;
2117 case BUILT_IN_STRNDUP:
2118 src = (*params)[0];
2119 strop = true;
2120 idx = 1;
2121 break;
2122 case BUILT_IN_MEMCHR:
2123 if (params->length () < 3)
2124 return;
2125 src = (*params)[0];
2126 idx = 2;
2127 break;
2128 case BUILT_IN_SNPRINTF:
2129 case BUILT_IN_SNPRINTF_CHK:
2130 case BUILT_IN_VSNPRINTF:
2131 case BUILT_IN_VSNPRINTF_CHK:
2132 dest = (*params)[0];
2133 idx = 1;
2134 strop = true;
2135 break;
2136 default:
2137 break;
2140 if (idx >= 3)
2141 return;
2143 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2144 return;
2146 type = TYPE_P (sizeof_arg[idx])
2147 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2148 if (!POINTER_TYPE_P (type))
2149 return;
2151 if (dest
2152 && (tem = tree_strip_nop_conversions (dest))
2153 && POINTER_TYPE_P (TREE_TYPE (tem))
2154 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2155 return;
2157 if (src
2158 && (tem = tree_strip_nop_conversions (src))
2159 && POINTER_TYPE_P (TREE_TYPE (tem))
2160 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2161 return;
2163 loc = sizeof_arg_loc[idx];
2165 if (dest && !cmp)
2167 if (!TYPE_P (sizeof_arg[idx])
2168 && operand_equal_p (dest, sizeof_arg[idx], 0)
2169 && comp_types (TREE_TYPE (dest), type))
2171 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2172 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2173 "argument to %<sizeof%> in %qD call is the same "
2174 "expression as the destination; did you mean to "
2175 "remove the addressof?", callee);
2176 else if ((TYPE_PRECISION (TREE_TYPE (type))
2177 == TYPE_PRECISION (char_type_node))
2178 || strop)
2179 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2180 "argument to %<sizeof%> in %qD call is the same "
2181 "expression as the destination; did you mean to "
2182 "provide an explicit length?", callee);
2183 else
2184 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2185 "argument to %<sizeof%> in %qD call is the same "
2186 "expression as the destination; did you mean to "
2187 "dereference it?", callee);
2188 return;
2191 if (POINTER_TYPE_P (TREE_TYPE (dest))
2192 && !strop
2193 && comp_types (TREE_TYPE (dest), type)
2194 && !VOID_TYPE_P (TREE_TYPE (type)))
2196 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2197 "argument to %<sizeof%> in %qD call is the same "
2198 "pointer type %qT as the destination; expected %qT "
2199 "or an explicit length", callee, TREE_TYPE (dest),
2200 TREE_TYPE (TREE_TYPE (dest)));
2201 return;
2205 if (src && !cmp)
2207 if (!TYPE_P (sizeof_arg[idx])
2208 && operand_equal_p (src, sizeof_arg[idx], 0)
2209 && comp_types (TREE_TYPE (src), type))
2211 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2212 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2213 "argument to %<sizeof%> in %qD call is the same "
2214 "expression as the source; did you mean to "
2215 "remove the addressof?", callee);
2216 else if ((TYPE_PRECISION (TREE_TYPE (type))
2217 == TYPE_PRECISION (char_type_node))
2218 || strop)
2219 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2220 "argument to %<sizeof%> in %qD call is the same "
2221 "expression as the source; did you mean to "
2222 "provide an explicit length?", callee);
2223 else
2224 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2225 "argument to %<sizeof%> in %qD call is the same "
2226 "expression as the source; did you mean to "
2227 "dereference it?", callee);
2228 return;
2231 if (POINTER_TYPE_P (TREE_TYPE (src))
2232 && !strop
2233 && comp_types (TREE_TYPE (src), type)
2234 && !VOID_TYPE_P (TREE_TYPE (type)))
2236 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2237 "argument to %<sizeof%> in %qD call is the same "
2238 "pointer type %qT as the source; expected %qT "
2239 "or an explicit length", callee, TREE_TYPE (src),
2240 TREE_TYPE (TREE_TYPE (src)));
2241 return;
2245 if (dest)
2247 if (!TYPE_P (sizeof_arg[idx])
2248 && operand_equal_p (dest, sizeof_arg[idx], 0)
2249 && comp_types (TREE_TYPE (dest), type))
2251 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2252 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2253 "argument to %<sizeof%> in %qD call is the same "
2254 "expression as the first source; did you mean to "
2255 "remove the addressof?", callee);
2256 else if ((TYPE_PRECISION (TREE_TYPE (type))
2257 == TYPE_PRECISION (char_type_node))
2258 || strop)
2259 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2260 "argument to %<sizeof%> in %qD call is the same "
2261 "expression as the first source; did you mean to "
2262 "provide an explicit length?", callee);
2263 else
2264 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2265 "argument to %<sizeof%> in %qD call is the same "
2266 "expression as the first source; did you mean to "
2267 "dereference it?", callee);
2268 return;
2271 if (POINTER_TYPE_P (TREE_TYPE (dest))
2272 && !strop
2273 && comp_types (TREE_TYPE (dest), type)
2274 && !VOID_TYPE_P (TREE_TYPE (type)))
2276 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2277 "argument to %<sizeof%> in %qD call is the same "
2278 "pointer type %qT as the first source; expected %qT "
2279 "or an explicit length", callee, TREE_TYPE (dest),
2280 TREE_TYPE (TREE_TYPE (dest)));
2281 return;
2285 if (src)
2287 if (!TYPE_P (sizeof_arg[idx])
2288 && operand_equal_p (src, sizeof_arg[idx], 0)
2289 && comp_types (TREE_TYPE (src), type))
2291 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2292 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2293 "argument to %<sizeof%> in %qD call is the same "
2294 "expression as the second source; did you mean to "
2295 "remove the addressof?", callee);
2296 else if ((TYPE_PRECISION (TREE_TYPE (type))
2297 == TYPE_PRECISION (char_type_node))
2298 || strop)
2299 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2300 "argument to %<sizeof%> in %qD call is the same "
2301 "expression as the second source; did you mean to "
2302 "provide an explicit length?", callee);
2303 else
2304 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2305 "argument to %<sizeof%> in %qD call is the same "
2306 "expression as the second source; did you mean to "
2307 "dereference it?", callee);
2308 return;
2311 if (POINTER_TYPE_P (TREE_TYPE (src))
2312 && !strop
2313 && comp_types (TREE_TYPE (src), type)
2314 && !VOID_TYPE_P (TREE_TYPE (type)))
2316 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2317 "argument to %<sizeof%> in %qD call is the same "
2318 "pointer type %qT as the second source; expected %qT "
2319 "or an explicit length", callee, TREE_TYPE (src),
2320 TREE_TYPE (TREE_TYPE (src)));
2321 return;
2327 /* Warn for unlikely, improbable, or stupid DECL declarations
2328 of `main'. */
2330 void
2331 check_main_parameter_types (tree decl)
2333 function_args_iterator iter;
2334 tree type;
2335 int argct = 0;
2337 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2339 /* XXX void_type_node belies the abstraction. */
2340 if (type == void_type_node || type == error_mark_node )
2341 break;
2343 tree t = type;
2344 if (TYPE_ATOMIC (t))
2345 pedwarn (input_location, OPT_Wmain,
2346 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2347 type, decl);
2348 while (POINTER_TYPE_P (t))
2350 t = TREE_TYPE (t);
2351 if (TYPE_ATOMIC (t))
2352 pedwarn (input_location, OPT_Wmain,
2353 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2354 type, decl);
2357 ++argct;
2358 switch (argct)
2360 case 1:
2361 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2362 pedwarn (input_location, OPT_Wmain,
2363 "first argument of %q+D should be %<int%>", decl);
2364 break;
2366 case 2:
2367 if (TREE_CODE (type) != POINTER_TYPE
2368 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2369 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2370 != char_type_node))
2371 pedwarn (input_location, OPT_Wmain,
2372 "second argument of %q+D should be %<char **%>", decl);
2373 break;
2375 case 3:
2376 if (TREE_CODE (type) != POINTER_TYPE
2377 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2378 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2379 != char_type_node))
2380 pedwarn (input_location, OPT_Wmain,
2381 "third argument of %q+D should probably be "
2382 "%<char **%>", decl);
2383 break;
2387 /* It is intentional that this message does not mention the third
2388 argument because it's only mentioned in an appendix of the
2389 standard. */
2390 if (argct > 0 && (argct < 2 || argct > 3))
2391 pedwarn (input_location, OPT_Wmain,
2392 "%q+D takes only zero or two arguments", decl);
2394 if (stdarg_p (TREE_TYPE (decl)))
2395 pedwarn (input_location, OPT_Wmain,
2396 "%q+D declared as variadic function", decl);
2399 /* vector_targets_convertible_p is used for vector pointer types. The
2400 callers perform various checks that the qualifiers are satisfactory,
2401 while OTOH vector_targets_convertible_p ignores the number of elements
2402 in the vectors. That's fine with vector pointers as we can consider,
2403 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2404 and that does not require and conversion of the pointer values.
2405 In contrast, vector_types_convertible_p and
2406 vector_types_compatible_elements_p are used for vector value types. */
2407 /* True if pointers to distinct types T1 and T2 can be converted to
2408 each other without an explicit cast. Only returns true for opaque
2409 vector types. */
2410 bool
2411 vector_targets_convertible_p (const_tree t1, const_tree t2)
2413 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2414 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2415 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2416 return true;
2418 return false;
2421 /* vector_types_convertible_p is used for vector value types.
2422 It could in principle call vector_targets_convertible_p as a subroutine,
2423 but then the check for vector type would be duplicated with its callers,
2424 and also the purpose of vector_targets_convertible_p would become
2425 muddled.
2426 Where vector_types_convertible_p returns true, a conversion might still be
2427 needed to make the types match.
2428 In contrast, vector_targets_convertible_p is used for vector pointer
2429 values, and vector_types_compatible_elements_p is used specifically
2430 in the context for binary operators, as a check if use is possible without
2431 conversion. */
2432 /* True if vector types T1 and T2 can be converted to each other
2433 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2434 can only be converted with -flax-vector-conversions yet that is not
2435 in effect, emit a note telling the user about that option if such
2436 a note has not previously been emitted. */
2437 bool
2438 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2440 static bool emitted_lax_note = false;
2441 bool convertible_lax;
2443 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2444 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2445 return true;
2447 convertible_lax =
2448 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2449 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2450 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2451 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2452 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2454 if (!convertible_lax || flag_lax_vector_conversions)
2455 return convertible_lax;
2457 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2458 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2459 return true;
2461 if (emit_lax_note && !emitted_lax_note)
2463 emitted_lax_note = true;
2464 inform (input_location, "use -flax-vector-conversions to permit "
2465 "conversions between vectors with differing "
2466 "element types or numbers of subparts");
2469 return false;
2472 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2473 and have vector types, V0 has the same type as V1, and the number of
2474 elements of V0, V1, MASK is the same.
2476 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2477 called with two arguments. In this case implementation passes the
2478 first argument twice in order to share the same tree code. This fact
2479 could enable the mask-values being twice the vector length. This is
2480 an implementation accident and this semantics is not guaranteed to
2481 the user. */
2482 tree
2483 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2484 bool complain)
2486 tree ret;
2487 bool wrap = true;
2488 bool maybe_const = false;
2489 bool two_arguments = false;
2491 if (v1 == NULL_TREE)
2493 two_arguments = true;
2494 v1 = v0;
2497 if (v0 == error_mark_node || v1 == error_mark_node
2498 || mask == error_mark_node)
2499 return error_mark_node;
2501 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2502 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2504 if (complain)
2505 error_at (loc, "__builtin_shuffle last argument must "
2506 "be an integer vector");
2507 return error_mark_node;
2510 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2511 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2513 if (complain)
2514 error_at (loc, "__builtin_shuffle arguments must be vectors");
2515 return error_mark_node;
2518 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2520 if (complain)
2521 error_at (loc, "__builtin_shuffle argument vectors must be of "
2522 "the same type");
2523 return error_mark_node;
2526 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2527 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2528 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2529 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2531 if (complain)
2532 error_at (loc, "__builtin_shuffle number of elements of the "
2533 "argument vector(s) and the mask vector should "
2534 "be the same");
2535 return error_mark_node;
2538 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2539 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2541 if (complain)
2542 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2543 "must have the same size as inner type of the mask");
2544 return error_mark_node;
2547 if (!c_dialect_cxx ())
2549 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2550 v0 = c_fully_fold (v0, false, &maybe_const);
2551 wrap &= maybe_const;
2553 if (two_arguments)
2554 v1 = v0 = save_expr (v0);
2555 else
2557 v1 = c_fully_fold (v1, false, &maybe_const);
2558 wrap &= maybe_const;
2561 mask = c_fully_fold (mask, false, &maybe_const);
2562 wrap &= maybe_const;
2564 else if (two_arguments)
2565 v1 = v0 = save_expr (v0);
2567 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2569 if (!c_dialect_cxx () && !wrap)
2570 ret = c_wrap_maybe_const (ret, true);
2572 return ret;
2575 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2576 to integral type. */
2578 static tree
2579 c_common_get_narrower (tree op, int *unsignedp_ptr)
2581 op = get_narrower (op, unsignedp_ptr);
2583 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2584 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2586 /* C++0x scoped enumerations don't implicitly convert to integral
2587 type; if we stripped an explicit conversion to a larger type we
2588 need to replace it so common_type will still work. */
2589 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2590 TYPE_UNSIGNED (TREE_TYPE (op)));
2591 op = fold_convert (type, op);
2593 return op;
2596 /* This is a helper function of build_binary_op.
2598 For certain operations if both args were extended from the same
2599 smaller type, do the arithmetic in that type and then extend.
2601 BITWISE indicates a bitwise operation.
2602 For them, this optimization is safe only if
2603 both args are zero-extended or both are sign-extended.
2604 Otherwise, we might change the result.
2605 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2606 but calculated in (unsigned short) it would be (unsigned short)-1.
2608 tree
2609 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2611 int unsigned0, unsigned1;
2612 tree arg0, arg1;
2613 int uns;
2614 tree type;
2616 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2617 excessive narrowing when we call get_narrower below. For
2618 example, suppose that OP0 is of unsigned int extended
2619 from signed char and that RESULT_TYPE is long long int.
2620 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2621 like
2623 (long long int) (unsigned int) signed_char
2625 which get_narrower would narrow down to
2627 (unsigned int) signed char
2629 If we do not cast OP0 first, get_narrower would return
2630 signed_char, which is inconsistent with the case of the
2631 explicit cast. */
2632 op0 = convert (result_type, op0);
2633 op1 = convert (result_type, op1);
2635 arg0 = c_common_get_narrower (op0, &unsigned0);
2636 arg1 = c_common_get_narrower (op1, &unsigned1);
2638 /* UNS is 1 if the operation to be done is an unsigned one. */
2639 uns = TYPE_UNSIGNED (result_type);
2641 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2642 but it *requires* conversion to FINAL_TYPE. */
2644 if ((TYPE_PRECISION (TREE_TYPE (op0))
2645 == TYPE_PRECISION (TREE_TYPE (arg0)))
2646 && TREE_TYPE (op0) != result_type)
2647 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2648 if ((TYPE_PRECISION (TREE_TYPE (op1))
2649 == TYPE_PRECISION (TREE_TYPE (arg1)))
2650 && TREE_TYPE (op1) != result_type)
2651 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2653 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2655 /* For bitwise operations, signedness of nominal type
2656 does not matter. Consider only how operands were extended. */
2657 if (bitwise)
2658 uns = unsigned0;
2660 /* Note that in all three cases below we refrain from optimizing
2661 an unsigned operation on sign-extended args.
2662 That would not be valid. */
2664 /* Both args variable: if both extended in same way
2665 from same width, do it in that width.
2666 Do it unsigned if args were zero-extended. */
2667 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2668 < TYPE_PRECISION (result_type))
2669 && (TYPE_PRECISION (TREE_TYPE (arg1))
2670 == TYPE_PRECISION (TREE_TYPE (arg0)))
2671 && unsigned0 == unsigned1
2672 && (unsigned0 || !uns))
2673 return c_common_signed_or_unsigned_type
2674 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2676 else if (TREE_CODE (arg0) == INTEGER_CST
2677 && (unsigned1 || !uns)
2678 && (TYPE_PRECISION (TREE_TYPE (arg1))
2679 < TYPE_PRECISION (result_type))
2680 && (type
2681 = c_common_signed_or_unsigned_type (unsigned1,
2682 TREE_TYPE (arg1)))
2683 && !POINTER_TYPE_P (type)
2684 && int_fits_type_p (arg0, type))
2685 return type;
2687 else if (TREE_CODE (arg1) == INTEGER_CST
2688 && (unsigned0 || !uns)
2689 && (TYPE_PRECISION (TREE_TYPE (arg0))
2690 < TYPE_PRECISION (result_type))
2691 && (type
2692 = c_common_signed_or_unsigned_type (unsigned0,
2693 TREE_TYPE (arg0)))
2694 && !POINTER_TYPE_P (type)
2695 && int_fits_type_p (arg1, type))
2696 return type;
2698 return result_type;
2701 /* Checks if expression EXPR of real/integer type cannot be converted
2702 to the real/integer type TYPE. Function returns non-zero when:
2703 * EXPR is a constant which cannot be exactly converted to TYPE.
2704 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2705 for EXPR type and TYPE being both integers or both real.
2706 * EXPR is not a constant of real type and TYPE is an integer.
2707 * EXPR is not a constant of integer type which cannot be
2708 exactly converted to real type.
2709 Function allows conversions between types of different signedness and
2710 can return SAFE_CONVERSION (zero) in that case. Function can produce
2711 signedness warnings if PRODUCE_WARNS is true. */
2713 enum conversion_safety
2714 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2716 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2717 tree expr_type = TREE_TYPE (expr);
2718 loc = expansion_point_location_if_in_system_header (loc);
2720 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2722 /* Warn for real constant that is not an exact integer converted
2723 to integer type. */
2724 if (TREE_CODE (expr_type) == REAL_TYPE
2725 && TREE_CODE (type) == INTEGER_TYPE)
2727 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2728 give_warning = UNSAFE_REAL;
2730 /* Warn for an integer constant that does not fit into integer type. */
2731 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2732 && TREE_CODE (type) == INTEGER_TYPE
2733 && !int_fits_type_p (expr, type))
2735 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2736 && tree_int_cst_sgn (expr) < 0)
2738 if (produce_warns)
2739 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2740 " implicitly converted to unsigned type");
2742 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2744 if (produce_warns)
2745 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2746 " constant value to negative integer");
2748 else
2749 give_warning = UNSAFE_OTHER;
2751 else if (TREE_CODE (type) == REAL_TYPE)
2753 /* Warn for an integer constant that does not fit into real type. */
2754 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2756 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2757 if (!exact_real_truncate (TYPE_MODE (type), &a))
2758 give_warning = UNSAFE_REAL;
2760 /* Warn for a real constant that does not fit into a smaller
2761 real type. */
2762 else if (TREE_CODE (expr_type) == REAL_TYPE
2763 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2765 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2766 if (!exact_real_truncate (TYPE_MODE (type), &a))
2767 give_warning = UNSAFE_REAL;
2771 else
2773 /* Warn for real types converted to integer types. */
2774 if (TREE_CODE (expr_type) == REAL_TYPE
2775 && TREE_CODE (type) == INTEGER_TYPE)
2776 give_warning = UNSAFE_REAL;
2778 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2779 && TREE_CODE (type) == INTEGER_TYPE)
2781 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2782 expr = get_unwidened (expr, 0);
2783 expr_type = TREE_TYPE (expr);
2785 /* Don't warn for short y; short x = ((int)y & 0xff); */
2786 if (TREE_CODE (expr) == BIT_AND_EXPR
2787 || TREE_CODE (expr) == BIT_IOR_EXPR
2788 || TREE_CODE (expr) == BIT_XOR_EXPR)
2790 /* If both args were extended from a shortest type,
2791 use that type if that is safe. */
2792 expr_type = shorten_binary_op (expr_type,
2793 TREE_OPERAND (expr, 0),
2794 TREE_OPERAND (expr, 1),
2795 /* bitwise */1);
2797 if (TREE_CODE (expr) == BIT_AND_EXPR)
2799 tree op0 = TREE_OPERAND (expr, 0);
2800 tree op1 = TREE_OPERAND (expr, 1);
2801 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2802 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2804 /* If one of the operands is a non-negative constant
2805 that fits in the target type, then the type of the
2806 other operand does not matter. */
2807 if ((TREE_CODE (op0) == INTEGER_CST
2808 && int_fits_type_p (op0, c_common_signed_type (type))
2809 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2810 || (TREE_CODE (op1) == INTEGER_CST
2811 && int_fits_type_p (op1, c_common_signed_type (type))
2812 && int_fits_type_p (op1,
2813 c_common_unsigned_type (type))))
2814 return SAFE_CONVERSION;
2815 /* If constant is unsigned and fits in the target
2816 type, then the result will also fit. */
2817 else if ((TREE_CODE (op0) == INTEGER_CST
2818 && unsigned0
2819 && int_fits_type_p (op0, type))
2820 || (TREE_CODE (op1) == INTEGER_CST
2821 && unsigned1
2822 && int_fits_type_p (op1, type)))
2823 return SAFE_CONVERSION;
2826 /* Warn for integer types converted to smaller integer types. */
2827 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2828 give_warning = UNSAFE_OTHER;
2830 /* When they are the same width but different signedness,
2831 then the value may change. */
2832 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2833 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2834 /* Even when converted to a bigger type, if the type is
2835 unsigned but expr is signed, then negative values
2836 will be changed. */
2837 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2838 && produce_warns)
2839 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2840 "may change the sign of the result",
2841 type, expr_type);
2844 /* Warn for integer types converted to real types if and only if
2845 all the range of values of the integer type cannot be
2846 represented by the real type. */
2847 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2848 && TREE_CODE (type) == REAL_TYPE)
2850 tree type_low_bound, type_high_bound;
2851 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2853 /* Don't warn about char y = 0xff; float x = (int) y; */
2854 expr = get_unwidened (expr, 0);
2855 expr_type = TREE_TYPE (expr);
2857 type_low_bound = TYPE_MIN_VALUE (expr_type);
2858 type_high_bound = TYPE_MAX_VALUE (expr_type);
2859 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2860 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2862 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2863 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2864 give_warning = UNSAFE_OTHER;
2867 /* Warn for real types converted to smaller real types. */
2868 else if (TREE_CODE (expr_type) == REAL_TYPE
2869 && TREE_CODE (type) == REAL_TYPE
2870 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2871 give_warning = UNSAFE_REAL;
2874 return give_warning;
2877 /* Warns if the conversion of EXPR to TYPE may alter a value.
2878 This is a helper function for warnings_for_convert_and_check. */
2880 static void
2881 conversion_warning (location_t loc, tree type, tree expr)
2883 tree expr_type = TREE_TYPE (expr);
2884 enum conversion_safety conversion_kind;
2886 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2887 return;
2889 /* This may happen, because for LHS op= RHS we preevaluate
2890 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2891 means we could no longer see the code of the EXPR. */
2892 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2893 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2894 if (TREE_CODE (expr) == SAVE_EXPR)
2895 expr = TREE_OPERAND (expr, 0);
2897 switch (TREE_CODE (expr))
2899 case EQ_EXPR:
2900 case NE_EXPR:
2901 case LE_EXPR:
2902 case GE_EXPR:
2903 case LT_EXPR:
2904 case GT_EXPR:
2905 case TRUTH_ANDIF_EXPR:
2906 case TRUTH_ORIF_EXPR:
2907 case TRUTH_AND_EXPR:
2908 case TRUTH_OR_EXPR:
2909 case TRUTH_XOR_EXPR:
2910 case TRUTH_NOT_EXPR:
2911 /* Conversion from boolean to a signed:1 bit-field (which only
2912 can hold the values 0 and -1) doesn't lose information - but
2913 it does change the value. */
2914 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2915 warning_at (loc, OPT_Wconversion,
2916 "conversion to %qT from boolean expression", type);
2917 return;
2919 case REAL_CST:
2920 case INTEGER_CST:
2921 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2922 if (conversion_kind == UNSAFE_REAL)
2923 warning_at (loc, OPT_Wfloat_conversion,
2924 "conversion to %qT alters %qT constant value",
2925 type, expr_type);
2926 else if (conversion_kind)
2927 warning_at (loc, OPT_Wconversion,
2928 "conversion to %qT alters %qT constant value",
2929 type, expr_type);
2930 return;
2932 case COND_EXPR:
2934 /* In case of COND_EXPR, we do not care about the type of
2935 COND_EXPR, only about the conversion of each operand. */
2936 tree op1 = TREE_OPERAND (expr, 1);
2937 tree op2 = TREE_OPERAND (expr, 2);
2939 conversion_warning (loc, type, op1);
2940 conversion_warning (loc, type, op2);
2941 return;
2944 default: /* 'expr' is not a constant. */
2945 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2946 if (conversion_kind == UNSAFE_REAL)
2947 warning_at (loc, OPT_Wfloat_conversion,
2948 "conversion to %qT from %qT may alter its value",
2949 type, expr_type);
2950 else if (conversion_kind)
2951 warning_at (loc, OPT_Wconversion,
2952 "conversion to %qT from %qT may alter its value",
2953 type, expr_type);
2957 /* Produce warnings after a conversion. RESULT is the result of
2958 converting EXPR to TYPE. This is a helper function for
2959 convert_and_check and cp_convert_and_check. */
2961 void
2962 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2963 tree result)
2965 loc = expansion_point_location_if_in_system_header (loc);
2967 if (TREE_CODE (expr) == INTEGER_CST
2968 && (TREE_CODE (type) == INTEGER_TYPE
2969 || TREE_CODE (type) == ENUMERAL_TYPE)
2970 && !int_fits_type_p (expr, type))
2972 /* Do not diagnose overflow in a constant expression merely
2973 because a conversion overflowed. */
2974 if (TREE_OVERFLOW (result))
2975 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2977 if (TYPE_UNSIGNED (type))
2979 /* This detects cases like converting -129 or 256 to
2980 unsigned char. */
2981 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2982 warning_at (loc, OPT_Woverflow,
2983 "large integer implicitly truncated to unsigned type");
2984 else
2985 conversion_warning (loc, type, expr);
2987 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2988 warning_at (loc, OPT_Woverflow,
2989 "overflow in implicit constant conversion");
2990 /* No warning for converting 0x80000000 to int. */
2991 else if (pedantic
2992 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2993 || TYPE_PRECISION (TREE_TYPE (expr))
2994 != TYPE_PRECISION (type)))
2995 warning_at (loc, OPT_Woverflow,
2996 "overflow in implicit constant conversion");
2998 else
2999 conversion_warning (loc, type, expr);
3001 else if ((TREE_CODE (result) == INTEGER_CST
3002 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
3003 warning_at (loc, OPT_Woverflow,
3004 "overflow in implicit constant conversion");
3005 else
3006 conversion_warning (loc, type, expr);
3010 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3011 Invoke this function on every expression that is converted implicitly,
3012 i.e. because of language rules and not because of an explicit cast. */
3014 tree
3015 convert_and_check (location_t loc, tree type, tree expr)
3017 tree result;
3018 tree expr_for_warning;
3020 /* Convert from a value with possible excess precision rather than
3021 via the semantic type, but do not warn about values not fitting
3022 exactly in the semantic type. */
3023 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3025 tree orig_type = TREE_TYPE (expr);
3026 expr = TREE_OPERAND (expr, 0);
3027 expr_for_warning = convert (orig_type, expr);
3028 if (orig_type == type)
3029 return expr_for_warning;
3031 else
3032 expr_for_warning = expr;
3034 if (TREE_TYPE (expr) == type)
3035 return expr;
3037 result = convert (type, expr);
3039 if (c_inhibit_evaluation_warnings == 0
3040 && !TREE_OVERFLOW_P (expr)
3041 && result != error_mark_node)
3042 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3044 return result;
3047 /* A node in a list that describes references to variables (EXPR), which are
3048 either read accesses if WRITER is zero, or write accesses, in which case
3049 WRITER is the parent of EXPR. */
3050 struct tlist
3052 struct tlist *next;
3053 tree expr, writer;
3056 /* Used to implement a cache the results of a call to verify_tree. We only
3057 use this for SAVE_EXPRs. */
3058 struct tlist_cache
3060 struct tlist_cache *next;
3061 struct tlist *cache_before_sp;
3062 struct tlist *cache_after_sp;
3063 tree expr;
3066 /* Obstack to use when allocating tlist structures, and corresponding
3067 firstobj. */
3068 static struct obstack tlist_obstack;
3069 static char *tlist_firstobj = 0;
3071 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3072 warnings. */
3073 static struct tlist *warned_ids;
3074 /* SAVE_EXPRs need special treatment. We process them only once and then
3075 cache the results. */
3076 static struct tlist_cache *save_expr_cache;
3078 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3079 static void merge_tlist (struct tlist **, struct tlist *, int);
3080 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3081 static int warning_candidate_p (tree);
3082 static bool candidate_equal_p (const_tree, const_tree);
3083 static void warn_for_collisions (struct tlist *);
3084 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3085 static struct tlist *new_tlist (struct tlist *, tree, tree);
3087 /* Create a new struct tlist and fill in its fields. */
3088 static struct tlist *
3089 new_tlist (struct tlist *next, tree t, tree writer)
3091 struct tlist *l;
3092 l = XOBNEW (&tlist_obstack, struct tlist);
3093 l->next = next;
3094 l->expr = t;
3095 l->writer = writer;
3096 return l;
3099 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3100 is nonnull, we ignore any node we find which has a writer equal to it. */
3102 static void
3103 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3105 while (add)
3107 struct tlist *next = add->next;
3108 if (!copy)
3109 add->next = *to;
3110 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3111 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3112 add = next;
3116 /* Merge the nodes of ADD into TO. This merging process is done so that for
3117 each variable that already exists in TO, no new node is added; however if
3118 there is a write access recorded in ADD, and an occurrence on TO is only
3119 a read access, then the occurrence in TO will be modified to record the
3120 write. */
3122 static void
3123 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3125 struct tlist **end = to;
3127 while (*end)
3128 end = &(*end)->next;
3130 while (add)
3132 int found = 0;
3133 struct tlist *tmp2;
3134 struct tlist *next = add->next;
3136 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3137 if (candidate_equal_p (tmp2->expr, add->expr))
3139 found = 1;
3140 if (!tmp2->writer)
3141 tmp2->writer = add->writer;
3143 if (!found)
3145 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3146 end = &(*end)->next;
3147 *end = 0;
3149 add = next;
3153 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3154 references in list LIST conflict with it, excluding reads if ONLY writers
3155 is nonzero. */
3157 static void
3158 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3159 int only_writes)
3161 struct tlist *tmp;
3163 /* Avoid duplicate warnings. */
3164 for (tmp = warned_ids; tmp; tmp = tmp->next)
3165 if (candidate_equal_p (tmp->expr, written))
3166 return;
3168 while (list)
3170 if (candidate_equal_p (list->expr, written)
3171 && !candidate_equal_p (list->writer, writer)
3172 && (!only_writes || list->writer))
3174 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3175 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3176 OPT_Wsequence_point, "operation on %qE may be undefined",
3177 list->expr);
3179 list = list->next;
3183 /* Given a list LIST of references to variables, find whether any of these
3184 can cause conflicts due to missing sequence points. */
3186 static void
3187 warn_for_collisions (struct tlist *list)
3189 struct tlist *tmp;
3191 for (tmp = list; tmp; tmp = tmp->next)
3193 if (tmp->writer)
3194 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3198 /* Return nonzero if X is a tree that can be verified by the sequence point
3199 warnings. */
3200 static int
3201 warning_candidate_p (tree x)
3203 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3204 return 0;
3206 if (TREE_CODE (x) == BLOCK)
3207 return 0;
3209 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3210 (lvalue_p) crash on TRY/CATCH. */
3211 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3212 return 0;
3214 if (!lvalue_p (x))
3215 return 0;
3217 /* No point to track non-const calls, they will never satisfy
3218 operand_equal_p. */
3219 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3220 return 0;
3222 if (TREE_CODE (x) == STRING_CST)
3223 return 0;
3225 return 1;
3228 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3229 static bool
3230 candidate_equal_p (const_tree x, const_tree y)
3232 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3235 /* Walk the tree X, and record accesses to variables. If X is written by the
3236 parent tree, WRITER is the parent.
3237 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3238 expression or its only operand forces a sequence point, then everything up
3239 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3240 in PNO_SP.
3241 Once we return, we will have emitted warnings if any subexpression before
3242 such a sequence point could be undefined. On a higher level, however, the
3243 sequence point may not be relevant, and we'll merge the two lists.
3245 Example: (b++, a) + b;
3246 The call that processes the COMPOUND_EXPR will store the increment of B
3247 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3248 processes the PLUS_EXPR will need to merge the two lists so that
3249 eventually, all accesses end up on the same list (and we'll warn about the
3250 unordered subexpressions b++ and b.
3252 A note on merging. If we modify the former example so that our expression
3253 becomes
3254 (b++, b) + a
3255 care must be taken not simply to add all three expressions into the final
3256 PNO_SP list. The function merge_tlist takes care of that by merging the
3257 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3258 way, so that no more than one access to B is recorded. */
3260 static void
3261 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3262 tree writer)
3264 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3265 enum tree_code code;
3266 enum tree_code_class cl;
3268 /* X may be NULL if it is the operand of an empty statement expression
3269 ({ }). */
3270 if (x == NULL)
3271 return;
3273 restart:
3274 code = TREE_CODE (x);
3275 cl = TREE_CODE_CLASS (code);
3277 if (warning_candidate_p (x))
3278 *pno_sp = new_tlist (*pno_sp, x, writer);
3280 switch (code)
3282 case CONSTRUCTOR:
3283 case SIZEOF_EXPR:
3284 return;
3286 case COMPOUND_EXPR:
3287 case TRUTH_ANDIF_EXPR:
3288 case TRUTH_ORIF_EXPR:
3289 tmp_before = tmp_nosp = tmp_list3 = 0;
3290 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3291 warn_for_collisions (tmp_nosp);
3292 merge_tlist (pbefore_sp, tmp_before, 0);
3293 merge_tlist (pbefore_sp, tmp_nosp, 0);
3294 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3295 merge_tlist (pbefore_sp, tmp_list3, 0);
3296 return;
3298 case COND_EXPR:
3299 tmp_before = tmp_list2 = 0;
3300 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3301 warn_for_collisions (tmp_list2);
3302 merge_tlist (pbefore_sp, tmp_before, 0);
3303 merge_tlist (pbefore_sp, tmp_list2, 0);
3305 tmp_list3 = tmp_nosp = 0;
3306 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3307 warn_for_collisions (tmp_nosp);
3308 merge_tlist (pbefore_sp, tmp_list3, 0);
3310 tmp_list3 = tmp_list2 = 0;
3311 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3312 warn_for_collisions (tmp_list2);
3313 merge_tlist (pbefore_sp, tmp_list3, 0);
3314 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3315 two first, to avoid warning for (a ? b++ : b++). */
3316 merge_tlist (&tmp_nosp, tmp_list2, 0);
3317 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3318 return;
3320 case PREDECREMENT_EXPR:
3321 case PREINCREMENT_EXPR:
3322 case POSTDECREMENT_EXPR:
3323 case POSTINCREMENT_EXPR:
3324 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3325 return;
3327 case MODIFY_EXPR:
3328 tmp_before = tmp_nosp = tmp_list3 = 0;
3329 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3330 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3331 /* Expressions inside the LHS are not ordered wrt. the sequence points
3332 in the RHS. Example:
3333 *a = (a++, 2)
3334 Despite the fact that the modification of "a" is in the before_sp
3335 list (tmp_before), it conflicts with the use of "a" in the LHS.
3336 We can handle this by adding the contents of tmp_list3
3337 to those of tmp_before, and redoing the collision warnings for that
3338 list. */
3339 add_tlist (&tmp_before, tmp_list3, x, 1);
3340 warn_for_collisions (tmp_before);
3341 /* Exclude the LHS itself here; we first have to merge it into the
3342 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3343 didn't exclude the LHS, we'd get it twice, once as a read and once
3344 as a write. */
3345 add_tlist (pno_sp, tmp_list3, x, 0);
3346 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3348 merge_tlist (pbefore_sp, tmp_before, 0);
3349 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3350 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3351 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3352 return;
3354 case CALL_EXPR:
3355 /* We need to warn about conflicts among arguments and conflicts between
3356 args and the function address. Side effects of the function address,
3357 however, are not ordered by the sequence point of the call. */
3359 call_expr_arg_iterator iter;
3360 tree arg;
3361 tmp_before = tmp_nosp = 0;
3362 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3363 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3365 tmp_list2 = tmp_list3 = 0;
3366 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3367 merge_tlist (&tmp_list3, tmp_list2, 0);
3368 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3370 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3371 warn_for_collisions (tmp_before);
3372 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3373 return;
3376 case TREE_LIST:
3377 /* Scan all the list, e.g. indices of multi dimensional array. */
3378 while (x)
3380 tmp_before = tmp_nosp = 0;
3381 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3382 merge_tlist (&tmp_nosp, tmp_before, 0);
3383 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3384 x = TREE_CHAIN (x);
3386 return;
3388 case SAVE_EXPR:
3390 struct tlist_cache *t;
3391 for (t = save_expr_cache; t; t = t->next)
3392 if (candidate_equal_p (t->expr, x))
3393 break;
3395 if (!t)
3397 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3398 t->next = save_expr_cache;
3399 t->expr = x;
3400 save_expr_cache = t;
3402 tmp_before = tmp_nosp = 0;
3403 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3404 warn_for_collisions (tmp_nosp);
3406 tmp_list3 = 0;
3407 merge_tlist (&tmp_list3, tmp_nosp, 0);
3408 t->cache_before_sp = tmp_before;
3409 t->cache_after_sp = tmp_list3;
3411 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3412 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3413 return;
3416 case ADDR_EXPR:
3417 x = TREE_OPERAND (x, 0);
3418 if (DECL_P (x))
3419 return;
3420 writer = 0;
3421 goto restart;
3423 default:
3424 /* For other expressions, simply recurse on their operands.
3425 Manual tail recursion for unary expressions.
3426 Other non-expressions need not be processed. */
3427 if (cl == tcc_unary)
3429 x = TREE_OPERAND (x, 0);
3430 writer = 0;
3431 goto restart;
3433 else if (IS_EXPR_CODE_CLASS (cl))
3435 int lp;
3436 int max = TREE_OPERAND_LENGTH (x);
3437 for (lp = 0; lp < max; lp++)
3439 tmp_before = tmp_nosp = 0;
3440 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3441 merge_tlist (&tmp_nosp, tmp_before, 0);
3442 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3445 return;
3449 /* Try to warn for undefined behavior in EXPR due to missing sequence
3450 points. */
3452 DEBUG_FUNCTION void
3453 verify_sequence_points (tree expr)
3455 struct tlist *before_sp = 0, *after_sp = 0;
3457 warned_ids = 0;
3458 save_expr_cache = 0;
3459 if (tlist_firstobj == 0)
3461 gcc_obstack_init (&tlist_obstack);
3462 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3465 verify_tree (expr, &before_sp, &after_sp, 0);
3466 warn_for_collisions (after_sp);
3467 obstack_free (&tlist_obstack, tlist_firstobj);
3470 /* Validate the expression after `case' and apply default promotions. */
3472 static tree
3473 check_case_value (location_t loc, tree value)
3475 if (value == NULL_TREE)
3476 return value;
3478 if (TREE_CODE (value) == INTEGER_CST)
3479 /* Promote char or short to int. */
3480 value = perform_integral_promotions (value);
3481 else if (value != error_mark_node)
3483 error_at (loc, "case label does not reduce to an integer constant");
3484 value = error_mark_node;
3487 constant_expression_warning (value);
3489 return value;
3492 /* See if the case values LOW and HIGH are in the range of the original
3493 type (i.e. before the default conversion to int) of the switch testing
3494 expression.
3495 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3496 the type before promoting it. CASE_LOW_P is a pointer to the lower
3497 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3498 if the case is not a case range.
3499 The caller has to make sure that we are not called with NULL for
3500 CASE_LOW_P (i.e. the default case).
3501 Returns true if the case label is in range of ORIG_TYPE (saturated or
3502 untouched) or false if the label is out of range. */
3504 static bool
3505 check_case_bounds (location_t loc, tree type, tree orig_type,
3506 tree *case_low_p, tree *case_high_p)
3508 tree min_value, max_value;
3509 tree case_low = *case_low_p;
3510 tree case_high = case_high_p ? *case_high_p : case_low;
3512 /* If there was a problem with the original type, do nothing. */
3513 if (orig_type == error_mark_node)
3514 return true;
3516 min_value = TYPE_MIN_VALUE (orig_type);
3517 max_value = TYPE_MAX_VALUE (orig_type);
3519 /* Case label is less than minimum for type. */
3520 if (tree_int_cst_compare (case_low, min_value) < 0
3521 && tree_int_cst_compare (case_high, min_value) < 0)
3523 warning_at (loc, 0, "case label value is less than minimum value "
3524 "for type");
3525 return false;
3528 /* Case value is greater than maximum for type. */
3529 if (tree_int_cst_compare (case_low, max_value) > 0
3530 && tree_int_cst_compare (case_high, max_value) > 0)
3532 warning_at (loc, 0, "case label value exceeds maximum value for type");
3533 return false;
3536 /* Saturate lower case label value to minimum. */
3537 if (tree_int_cst_compare (case_high, min_value) >= 0
3538 && tree_int_cst_compare (case_low, min_value) < 0)
3540 warning_at (loc, 0, "lower value in case label range"
3541 " less than minimum value for type");
3542 case_low = min_value;
3545 /* Saturate upper case label value to maximum. */
3546 if (tree_int_cst_compare (case_low, max_value) <= 0
3547 && tree_int_cst_compare (case_high, max_value) > 0)
3549 warning_at (loc, 0, "upper value in case label range"
3550 " exceeds maximum value for type");
3551 case_high = max_value;
3554 if (*case_low_p != case_low)
3555 *case_low_p = convert (type, case_low);
3556 if (case_high_p && *case_high_p != case_high)
3557 *case_high_p = convert (type, case_high);
3559 return true;
3562 /* Return an integer type with BITS bits of precision,
3563 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3565 tree
3566 c_common_type_for_size (unsigned int bits, int unsignedp)
3568 int i;
3570 if (bits == TYPE_PRECISION (integer_type_node))
3571 return unsignedp ? unsigned_type_node : integer_type_node;
3573 if (bits == TYPE_PRECISION (signed_char_type_node))
3574 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3576 if (bits == TYPE_PRECISION (short_integer_type_node))
3577 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3579 if (bits == TYPE_PRECISION (long_integer_type_node))
3580 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3582 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3583 return (unsignedp ? long_long_unsigned_type_node
3584 : long_long_integer_type_node);
3586 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3587 if (int_n_enabled_p[i]
3588 && bits == int_n_data[i].bitsize)
3589 return (unsignedp ? int_n_trees[i].unsigned_type
3590 : int_n_trees[i].signed_type);
3592 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3593 return (unsignedp ? widest_unsigned_literal_type_node
3594 : widest_integer_literal_type_node);
3596 if (bits <= TYPE_PRECISION (intQI_type_node))
3597 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3599 if (bits <= TYPE_PRECISION (intHI_type_node))
3600 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3602 if (bits <= TYPE_PRECISION (intSI_type_node))
3603 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3605 if (bits <= TYPE_PRECISION (intDI_type_node))
3606 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3608 return 0;
3611 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3612 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3613 and saturating if SATP is nonzero, otherwise not saturating. */
3615 tree
3616 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3617 int unsignedp, int satp)
3619 machine_mode mode;
3620 if (ibit == 0)
3621 mode = unsignedp ? UQQmode : QQmode;
3622 else
3623 mode = unsignedp ? UHAmode : HAmode;
3625 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3626 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3627 break;
3629 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3631 sorry ("GCC cannot support operators with integer types and "
3632 "fixed-point types that have too many integral and "
3633 "fractional bits together");
3634 return 0;
3637 return c_common_type_for_mode (mode, satp);
3640 /* Used for communication between c_common_type_for_mode and
3641 c_register_builtin_type. */
3642 tree registered_builtin_types;
3644 /* Return a data type that has machine mode MODE.
3645 If the mode is an integer,
3646 then UNSIGNEDP selects between signed and unsigned types.
3647 If the mode is a fixed-point mode,
3648 then UNSIGNEDP selects between saturating and nonsaturating types. */
3650 tree
3651 c_common_type_for_mode (machine_mode mode, int unsignedp)
3653 tree t;
3654 int i;
3656 if (mode == TYPE_MODE (integer_type_node))
3657 return unsignedp ? unsigned_type_node : integer_type_node;
3659 if (mode == TYPE_MODE (signed_char_type_node))
3660 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3662 if (mode == TYPE_MODE (short_integer_type_node))
3663 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3665 if (mode == TYPE_MODE (long_integer_type_node))
3666 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3668 if (mode == TYPE_MODE (long_long_integer_type_node))
3669 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3671 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3672 if (int_n_enabled_p[i]
3673 && mode == int_n_data[i].m)
3674 return (unsignedp ? int_n_trees[i].unsigned_type
3675 : int_n_trees[i].signed_type);
3677 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3678 return unsignedp ? widest_unsigned_literal_type_node
3679 : widest_integer_literal_type_node;
3681 if (mode == QImode)
3682 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3684 if (mode == HImode)
3685 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3687 if (mode == SImode)
3688 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3690 if (mode == DImode)
3691 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3693 #if HOST_BITS_PER_WIDE_INT >= 64
3694 if (mode == TYPE_MODE (intTI_type_node))
3695 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3696 #endif
3698 if (mode == TYPE_MODE (float_type_node))
3699 return float_type_node;
3701 if (mode == TYPE_MODE (double_type_node))
3702 return double_type_node;
3704 if (mode == TYPE_MODE (long_double_type_node))
3705 return long_double_type_node;
3707 if (mode == TYPE_MODE (void_type_node))
3708 return void_type_node;
3710 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3711 return (unsignedp
3712 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3713 : make_signed_type (GET_MODE_PRECISION (mode)));
3715 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3716 return (unsignedp
3717 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3718 : make_signed_type (GET_MODE_PRECISION (mode)));
3720 if (COMPLEX_MODE_P (mode))
3722 machine_mode inner_mode;
3723 tree inner_type;
3725 if (mode == TYPE_MODE (complex_float_type_node))
3726 return complex_float_type_node;
3727 if (mode == TYPE_MODE (complex_double_type_node))
3728 return complex_double_type_node;
3729 if (mode == TYPE_MODE (complex_long_double_type_node))
3730 return complex_long_double_type_node;
3732 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3733 return complex_integer_type_node;
3735 inner_mode = GET_MODE_INNER (mode);
3736 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3737 if (inner_type != NULL_TREE)
3738 return build_complex_type (inner_type);
3740 else if (VECTOR_MODE_P (mode))
3742 machine_mode inner_mode = GET_MODE_INNER (mode);
3743 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3744 if (inner_type != NULL_TREE)
3745 return build_vector_type_for_mode (inner_type, mode);
3748 if (mode == TYPE_MODE (dfloat32_type_node))
3749 return dfloat32_type_node;
3750 if (mode == TYPE_MODE (dfloat64_type_node))
3751 return dfloat64_type_node;
3752 if (mode == TYPE_MODE (dfloat128_type_node))
3753 return dfloat128_type_node;
3755 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3757 if (mode == TYPE_MODE (short_fract_type_node))
3758 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3759 if (mode == TYPE_MODE (fract_type_node))
3760 return unsignedp ? sat_fract_type_node : fract_type_node;
3761 if (mode == TYPE_MODE (long_fract_type_node))
3762 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3763 if (mode == TYPE_MODE (long_long_fract_type_node))
3764 return unsignedp ? sat_long_long_fract_type_node
3765 : long_long_fract_type_node;
3767 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3768 return unsignedp ? sat_unsigned_short_fract_type_node
3769 : unsigned_short_fract_type_node;
3770 if (mode == TYPE_MODE (unsigned_fract_type_node))
3771 return unsignedp ? sat_unsigned_fract_type_node
3772 : unsigned_fract_type_node;
3773 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3774 return unsignedp ? sat_unsigned_long_fract_type_node
3775 : unsigned_long_fract_type_node;
3776 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3777 return unsignedp ? sat_unsigned_long_long_fract_type_node
3778 : unsigned_long_long_fract_type_node;
3780 if (mode == TYPE_MODE (short_accum_type_node))
3781 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3782 if (mode == TYPE_MODE (accum_type_node))
3783 return unsignedp ? sat_accum_type_node : accum_type_node;
3784 if (mode == TYPE_MODE (long_accum_type_node))
3785 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3786 if (mode == TYPE_MODE (long_long_accum_type_node))
3787 return unsignedp ? sat_long_long_accum_type_node
3788 : long_long_accum_type_node;
3790 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3791 return unsignedp ? sat_unsigned_short_accum_type_node
3792 : unsigned_short_accum_type_node;
3793 if (mode == TYPE_MODE (unsigned_accum_type_node))
3794 return unsignedp ? sat_unsigned_accum_type_node
3795 : unsigned_accum_type_node;
3796 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3797 return unsignedp ? sat_unsigned_long_accum_type_node
3798 : unsigned_long_accum_type_node;
3799 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3800 return unsignedp ? sat_unsigned_long_long_accum_type_node
3801 : unsigned_long_long_accum_type_node;
3803 if (mode == QQmode)
3804 return unsignedp ? sat_qq_type_node : qq_type_node;
3805 if (mode == HQmode)
3806 return unsignedp ? sat_hq_type_node : hq_type_node;
3807 if (mode == SQmode)
3808 return unsignedp ? sat_sq_type_node : sq_type_node;
3809 if (mode == DQmode)
3810 return unsignedp ? sat_dq_type_node : dq_type_node;
3811 if (mode == TQmode)
3812 return unsignedp ? sat_tq_type_node : tq_type_node;
3814 if (mode == UQQmode)
3815 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3816 if (mode == UHQmode)
3817 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3818 if (mode == USQmode)
3819 return unsignedp ? sat_usq_type_node : usq_type_node;
3820 if (mode == UDQmode)
3821 return unsignedp ? sat_udq_type_node : udq_type_node;
3822 if (mode == UTQmode)
3823 return unsignedp ? sat_utq_type_node : utq_type_node;
3825 if (mode == HAmode)
3826 return unsignedp ? sat_ha_type_node : ha_type_node;
3827 if (mode == SAmode)
3828 return unsignedp ? sat_sa_type_node : sa_type_node;
3829 if (mode == DAmode)
3830 return unsignedp ? sat_da_type_node : da_type_node;
3831 if (mode == TAmode)
3832 return unsignedp ? sat_ta_type_node : ta_type_node;
3834 if (mode == UHAmode)
3835 return unsignedp ? sat_uha_type_node : uha_type_node;
3836 if (mode == USAmode)
3837 return unsignedp ? sat_usa_type_node : usa_type_node;
3838 if (mode == UDAmode)
3839 return unsignedp ? sat_uda_type_node : uda_type_node;
3840 if (mode == UTAmode)
3841 return unsignedp ? sat_uta_type_node : uta_type_node;
3844 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3845 if (TYPE_MODE (TREE_VALUE (t)) == mode
3846 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3847 return TREE_VALUE (t);
3849 return 0;
3852 tree
3853 c_common_unsigned_type (tree type)
3855 return c_common_signed_or_unsigned_type (1, type);
3858 /* Return a signed type the same as TYPE in other respects. */
3860 tree
3861 c_common_signed_type (tree type)
3863 return c_common_signed_or_unsigned_type (0, type);
3866 /* Return a type the same as TYPE except unsigned or
3867 signed according to UNSIGNEDP. */
3869 tree
3870 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3872 tree type1;
3873 int i;
3875 /* This block of code emulates the behavior of the old
3876 c_common_unsigned_type. In particular, it returns
3877 long_unsigned_type_node if passed a long, even when a int would
3878 have the same size. This is necessary for warnings to work
3879 correctly in archs where sizeof(int) == sizeof(long) */
3881 type1 = TYPE_MAIN_VARIANT (type);
3882 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3883 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3884 if (type1 == integer_type_node || type1 == unsigned_type_node)
3885 return unsignedp ? unsigned_type_node : integer_type_node;
3886 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3887 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3888 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3889 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3890 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3891 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3893 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3894 if (int_n_enabled_p[i]
3895 && (type1 == int_n_trees[i].unsigned_type
3896 || type1 == int_n_trees[i].signed_type))
3897 return (unsignedp ? int_n_trees[i].unsigned_type
3898 : int_n_trees[i].signed_type);
3900 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3901 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3902 #if HOST_BITS_PER_WIDE_INT >= 64
3903 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3904 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3905 #endif
3906 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3907 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3908 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3909 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3910 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3911 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3912 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3913 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3915 #define C_COMMON_FIXED_TYPES(NAME) \
3916 if (type1 == short_ ## NAME ## _type_node \
3917 || type1 == unsigned_short_ ## NAME ## _type_node) \
3918 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3919 : short_ ## NAME ## _type_node; \
3920 if (type1 == NAME ## _type_node \
3921 || type1 == unsigned_ ## NAME ## _type_node) \
3922 return unsignedp ? unsigned_ ## NAME ## _type_node \
3923 : NAME ## _type_node; \
3924 if (type1 == long_ ## NAME ## _type_node \
3925 || type1 == unsigned_long_ ## NAME ## _type_node) \
3926 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3927 : long_ ## NAME ## _type_node; \
3928 if (type1 == long_long_ ## NAME ## _type_node \
3929 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3930 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3931 : long_long_ ## NAME ## _type_node;
3933 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3934 if (type1 == NAME ## _type_node \
3935 || type1 == u ## NAME ## _type_node) \
3936 return unsignedp ? u ## NAME ## _type_node \
3937 : NAME ## _type_node;
3939 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3940 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3941 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3942 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3943 : sat_ ## short_ ## NAME ## _type_node; \
3944 if (type1 == sat_ ## NAME ## _type_node \
3945 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3946 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3947 : sat_ ## NAME ## _type_node; \
3948 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3949 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3950 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3951 : sat_ ## long_ ## NAME ## _type_node; \
3952 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3953 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3954 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3955 : sat_ ## long_long_ ## NAME ## _type_node;
3957 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3958 if (type1 == sat_ ## NAME ## _type_node \
3959 || type1 == sat_ ## u ## NAME ## _type_node) \
3960 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3961 : sat_ ## NAME ## _type_node;
3963 C_COMMON_FIXED_TYPES (fract);
3964 C_COMMON_FIXED_TYPES_SAT (fract);
3965 C_COMMON_FIXED_TYPES (accum);
3966 C_COMMON_FIXED_TYPES_SAT (accum);
3968 C_COMMON_FIXED_MODE_TYPES (qq);
3969 C_COMMON_FIXED_MODE_TYPES (hq);
3970 C_COMMON_FIXED_MODE_TYPES (sq);
3971 C_COMMON_FIXED_MODE_TYPES (dq);
3972 C_COMMON_FIXED_MODE_TYPES (tq);
3973 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3974 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3975 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3976 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3977 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3978 C_COMMON_FIXED_MODE_TYPES (ha);
3979 C_COMMON_FIXED_MODE_TYPES (sa);
3980 C_COMMON_FIXED_MODE_TYPES (da);
3981 C_COMMON_FIXED_MODE_TYPES (ta);
3982 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3983 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3984 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3985 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3987 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3988 the precision; they have precision set to match their range, but
3989 may use a wider mode to match an ABI. If we change modes, we may
3990 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3991 the precision as well, so as to yield correct results for
3992 bit-field types. C++ does not have these separate bit-field
3993 types, and producing a signed or unsigned variant of an
3994 ENUMERAL_TYPE may cause other problems as well. */
3996 if (!INTEGRAL_TYPE_P (type)
3997 || TYPE_UNSIGNED (type) == unsignedp)
3998 return type;
4000 #define TYPE_OK(node) \
4001 (TYPE_MODE (type) == TYPE_MODE (node) \
4002 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4003 if (TYPE_OK (signed_char_type_node))
4004 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4005 if (TYPE_OK (integer_type_node))
4006 return unsignedp ? unsigned_type_node : integer_type_node;
4007 if (TYPE_OK (short_integer_type_node))
4008 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4009 if (TYPE_OK (long_integer_type_node))
4010 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4011 if (TYPE_OK (long_long_integer_type_node))
4012 return (unsignedp ? long_long_unsigned_type_node
4013 : long_long_integer_type_node);
4015 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4016 if (int_n_enabled_p[i]
4017 && TYPE_MODE (type) == int_n_data[i].m
4018 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4019 return (unsignedp ? int_n_trees[i].unsigned_type
4020 : int_n_trees[i].signed_type);
4022 if (TYPE_OK (widest_integer_literal_type_node))
4023 return (unsignedp ? widest_unsigned_literal_type_node
4024 : widest_integer_literal_type_node);
4026 #if HOST_BITS_PER_WIDE_INT >= 64
4027 if (TYPE_OK (intTI_type_node))
4028 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4029 #endif
4030 if (TYPE_OK (intDI_type_node))
4031 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4032 if (TYPE_OK (intSI_type_node))
4033 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4034 if (TYPE_OK (intHI_type_node))
4035 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4036 if (TYPE_OK (intQI_type_node))
4037 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4038 #undef TYPE_OK
4040 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4043 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4045 tree
4046 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4048 int i;
4050 /* Extended integer types of the same width as a standard type have
4051 lesser rank, so those of the same width as int promote to int or
4052 unsigned int and are valid for printf formats expecting int or
4053 unsigned int. To avoid such special cases, avoid creating
4054 extended integer types for bit-fields if a standard integer type
4055 is available. */
4056 if (width == TYPE_PRECISION (integer_type_node))
4057 return unsignedp ? unsigned_type_node : integer_type_node;
4058 if (width == TYPE_PRECISION (signed_char_type_node))
4059 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4060 if (width == TYPE_PRECISION (short_integer_type_node))
4061 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4062 if (width == TYPE_PRECISION (long_integer_type_node))
4063 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4064 if (width == TYPE_PRECISION (long_long_integer_type_node))
4065 return (unsignedp ? long_long_unsigned_type_node
4066 : long_long_integer_type_node);
4067 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4068 if (int_n_enabled_p[i]
4069 && width == int_n_data[i].bitsize)
4070 return (unsignedp ? int_n_trees[i].unsigned_type
4071 : int_n_trees[i].signed_type);
4072 return build_nonstandard_integer_type (width, unsignedp);
4075 /* The C version of the register_builtin_type langhook. */
4077 void
4078 c_register_builtin_type (tree type, const char* name)
4080 tree decl;
4082 decl = build_decl (UNKNOWN_LOCATION,
4083 TYPE_DECL, get_identifier (name), type);
4084 DECL_ARTIFICIAL (decl) = 1;
4085 if (!TYPE_NAME (type))
4086 TYPE_NAME (type) = decl;
4087 pushdecl (decl);
4089 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4092 /* Print an error message for invalid operands to arith operation
4093 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4094 LOCATION is the location of the message. */
4096 void
4097 binary_op_error (location_t location, enum tree_code code,
4098 tree type0, tree type1)
4100 const char *opname;
4102 switch (code)
4104 case PLUS_EXPR:
4105 opname = "+"; break;
4106 case MINUS_EXPR:
4107 opname = "-"; break;
4108 case MULT_EXPR:
4109 opname = "*"; break;
4110 case MAX_EXPR:
4111 opname = "max"; break;
4112 case MIN_EXPR:
4113 opname = "min"; break;
4114 case EQ_EXPR:
4115 opname = "=="; break;
4116 case NE_EXPR:
4117 opname = "!="; break;
4118 case LE_EXPR:
4119 opname = "<="; break;
4120 case GE_EXPR:
4121 opname = ">="; break;
4122 case LT_EXPR:
4123 opname = "<"; break;
4124 case GT_EXPR:
4125 opname = ">"; break;
4126 case LSHIFT_EXPR:
4127 opname = "<<"; break;
4128 case RSHIFT_EXPR:
4129 opname = ">>"; break;
4130 case TRUNC_MOD_EXPR:
4131 case FLOOR_MOD_EXPR:
4132 opname = "%"; break;
4133 case TRUNC_DIV_EXPR:
4134 case FLOOR_DIV_EXPR:
4135 opname = "/"; break;
4136 case BIT_AND_EXPR:
4137 opname = "&"; break;
4138 case BIT_IOR_EXPR:
4139 opname = "|"; break;
4140 case TRUTH_ANDIF_EXPR:
4141 opname = "&&"; break;
4142 case TRUTH_ORIF_EXPR:
4143 opname = "||"; break;
4144 case BIT_XOR_EXPR:
4145 opname = "^"; break;
4146 default:
4147 gcc_unreachable ();
4149 error_at (location,
4150 "invalid operands to binary %s (have %qT and %qT)", opname,
4151 type0, type1);
4154 /* Given an expression as a tree, return its original type. Do this
4155 by stripping any conversion that preserves the sign and precision. */
4156 static tree
4157 expr_original_type (tree expr)
4159 STRIP_SIGN_NOPS (expr);
4160 return TREE_TYPE (expr);
4163 /* Subroutine of build_binary_op, used for comparison operations.
4164 See if the operands have both been converted from subword integer types
4165 and, if so, perhaps change them both back to their original type.
4166 This function is also responsible for converting the two operands
4167 to the proper common type for comparison.
4169 The arguments of this function are all pointers to local variables
4170 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4171 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4173 LOC is the location of the comparison.
4175 If this function returns nonzero, it means that the comparison has
4176 a constant value. What this function returns is an expression for
4177 that value. */
4179 tree
4180 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4181 tree *restype_ptr, enum tree_code *rescode_ptr)
4183 tree type;
4184 tree op0 = *op0_ptr;
4185 tree op1 = *op1_ptr;
4186 int unsignedp0, unsignedp1;
4187 int real1, real2;
4188 tree primop0, primop1;
4189 enum tree_code code = *rescode_ptr;
4191 /* Throw away any conversions to wider types
4192 already present in the operands. */
4194 primop0 = c_common_get_narrower (op0, &unsignedp0);
4195 primop1 = c_common_get_narrower (op1, &unsignedp1);
4197 /* If primopN is first sign-extended from primopN's precision to opN's
4198 precision, then zero-extended from opN's precision to
4199 *restype_ptr precision, shortenings might be invalid. */
4200 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4201 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4202 && !unsignedp0
4203 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4204 primop0 = op0;
4205 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4206 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4207 && !unsignedp1
4208 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4209 primop1 = op1;
4211 /* Handle the case that OP0 does not *contain* a conversion
4212 but it *requires* conversion to FINAL_TYPE. */
4214 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4215 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4216 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4217 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4219 /* If one of the operands must be floated, we cannot optimize. */
4220 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4221 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4223 /* If first arg is constant, swap the args (changing operation
4224 so value is preserved), for canonicalization. Don't do this if
4225 the second arg is 0. */
4227 if (TREE_CONSTANT (primop0)
4228 && !integer_zerop (primop1) && !real_zerop (primop1)
4229 && !fixed_zerop (primop1))
4231 tree tem = primop0;
4232 int temi = unsignedp0;
4233 primop0 = primop1;
4234 primop1 = tem;
4235 tem = op0;
4236 op0 = op1;
4237 op1 = tem;
4238 *op0_ptr = op0;
4239 *op1_ptr = op1;
4240 unsignedp0 = unsignedp1;
4241 unsignedp1 = temi;
4242 temi = real1;
4243 real1 = real2;
4244 real2 = temi;
4246 switch (code)
4248 case LT_EXPR:
4249 code = GT_EXPR;
4250 break;
4251 case GT_EXPR:
4252 code = LT_EXPR;
4253 break;
4254 case LE_EXPR:
4255 code = GE_EXPR;
4256 break;
4257 case GE_EXPR:
4258 code = LE_EXPR;
4259 break;
4260 default:
4261 break;
4263 *rescode_ptr = code;
4266 /* If comparing an integer against a constant more bits wide,
4267 maybe we can deduce a value of 1 or 0 independent of the data.
4268 Or else truncate the constant now
4269 rather than extend the variable at run time.
4271 This is only interesting if the constant is the wider arg.
4272 Also, it is not safe if the constant is unsigned and the
4273 variable arg is signed, since in this case the variable
4274 would be sign-extended and then regarded as unsigned.
4275 Our technique fails in this case because the lowest/highest
4276 possible unsigned results don't follow naturally from the
4277 lowest/highest possible values of the variable operand.
4278 For just EQ_EXPR and NE_EXPR there is another technique that
4279 could be used: see if the constant can be faithfully represented
4280 in the other operand's type, by truncating it and reextending it
4281 and see if that preserves the constant's value. */
4283 if (!real1 && !real2
4284 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4285 && TREE_CODE (primop1) == INTEGER_CST
4286 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4288 int min_gt, max_gt, min_lt, max_lt;
4289 tree maxval, minval;
4290 /* 1 if comparison is nominally unsigned. */
4291 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4292 tree val;
4294 type = c_common_signed_or_unsigned_type (unsignedp0,
4295 TREE_TYPE (primop0));
4297 maxval = TYPE_MAX_VALUE (type);
4298 minval = TYPE_MIN_VALUE (type);
4300 if (unsignedp && !unsignedp0)
4301 *restype_ptr = c_common_signed_type (*restype_ptr);
4303 if (TREE_TYPE (primop1) != *restype_ptr)
4305 /* Convert primop1 to target type, but do not introduce
4306 additional overflow. We know primop1 is an int_cst. */
4307 primop1 = force_fit_type (*restype_ptr,
4308 wide_int::from
4309 (primop1,
4310 TYPE_PRECISION (*restype_ptr),
4311 TYPE_SIGN (TREE_TYPE (primop1))),
4312 0, TREE_OVERFLOW (primop1));
4314 if (type != *restype_ptr)
4316 minval = convert (*restype_ptr, minval);
4317 maxval = convert (*restype_ptr, maxval);
4320 min_gt = tree_int_cst_lt (primop1, minval);
4321 max_gt = tree_int_cst_lt (primop1, maxval);
4322 min_lt = tree_int_cst_lt (minval, primop1);
4323 max_lt = tree_int_cst_lt (maxval, primop1);
4325 val = 0;
4326 /* This used to be a switch, but Genix compiler can't handle that. */
4327 if (code == NE_EXPR)
4329 if (max_lt || min_gt)
4330 val = truthvalue_true_node;
4332 else if (code == EQ_EXPR)
4334 if (max_lt || min_gt)
4335 val = truthvalue_false_node;
4337 else if (code == LT_EXPR)
4339 if (max_lt)
4340 val = truthvalue_true_node;
4341 if (!min_lt)
4342 val = truthvalue_false_node;
4344 else if (code == GT_EXPR)
4346 if (min_gt)
4347 val = truthvalue_true_node;
4348 if (!max_gt)
4349 val = truthvalue_false_node;
4351 else if (code == LE_EXPR)
4353 if (!max_gt)
4354 val = truthvalue_true_node;
4355 if (min_gt)
4356 val = truthvalue_false_node;
4358 else if (code == GE_EXPR)
4360 if (!min_lt)
4361 val = truthvalue_true_node;
4362 if (max_lt)
4363 val = truthvalue_false_node;
4366 /* If primop0 was sign-extended and unsigned comparison specd,
4367 we did a signed comparison above using the signed type bounds.
4368 But the comparison we output must be unsigned.
4370 Also, for inequalities, VAL is no good; but if the signed
4371 comparison had *any* fixed result, it follows that the
4372 unsigned comparison just tests the sign in reverse
4373 (positive values are LE, negative ones GE).
4374 So we can generate an unsigned comparison
4375 against an extreme value of the signed type. */
4377 if (unsignedp && !unsignedp0)
4379 if (val != 0)
4380 switch (code)
4382 case LT_EXPR:
4383 case GE_EXPR:
4384 primop1 = TYPE_MIN_VALUE (type);
4385 val = 0;
4386 break;
4388 case LE_EXPR:
4389 case GT_EXPR:
4390 primop1 = TYPE_MAX_VALUE (type);
4391 val = 0;
4392 break;
4394 default:
4395 break;
4397 type = c_common_unsigned_type (type);
4400 if (TREE_CODE (primop0) != INTEGER_CST)
4402 if (val == truthvalue_false_node)
4403 warning_at (loc, OPT_Wtype_limits,
4404 "comparison is always false due to limited range of data type");
4405 if (val == truthvalue_true_node)
4406 warning_at (loc, OPT_Wtype_limits,
4407 "comparison is always true due to limited range of data type");
4410 if (val != 0)
4412 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4413 if (TREE_SIDE_EFFECTS (primop0))
4414 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4415 return val;
4418 /* Value is not predetermined, but do the comparison
4419 in the type of the operand that is not constant.
4420 TYPE is already properly set. */
4423 /* If either arg is decimal float and the other is float, find the
4424 proper common type to use for comparison. */
4425 else if (real1 && real2
4426 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4427 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4428 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4430 /* If either arg is decimal float and the other is float, fail. */
4431 else if (real1 && real2
4432 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4433 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4434 return 0;
4436 else if (real1 && real2
4437 && (TYPE_PRECISION (TREE_TYPE (primop0))
4438 == TYPE_PRECISION (TREE_TYPE (primop1))))
4439 type = TREE_TYPE (primop0);
4441 /* If args' natural types are both narrower than nominal type
4442 and both extend in the same manner, compare them
4443 in the type of the wider arg.
4444 Otherwise must actually extend both to the nominal
4445 common type lest different ways of extending
4446 alter the result.
4447 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4449 else if (unsignedp0 == unsignedp1 && real1 == real2
4450 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4451 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4453 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4454 type = c_common_signed_or_unsigned_type (unsignedp0
4455 || TYPE_UNSIGNED (*restype_ptr),
4456 type);
4457 /* Make sure shorter operand is extended the right way
4458 to match the longer operand. */
4459 primop0
4460 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4461 TREE_TYPE (primop0)),
4462 primop0);
4463 primop1
4464 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4465 TREE_TYPE (primop1)),
4466 primop1);
4468 else
4470 /* Here we must do the comparison on the nominal type
4471 using the args exactly as we received them. */
4472 type = *restype_ptr;
4473 primop0 = op0;
4474 primop1 = op1;
4476 if (!real1 && !real2 && integer_zerop (primop1)
4477 && TYPE_UNSIGNED (*restype_ptr))
4479 tree value = 0;
4480 /* All unsigned values are >= 0, so we warn. However,
4481 if OP0 is a constant that is >= 0, the signedness of
4482 the comparison isn't an issue, so suppress the
4483 warning. */
4484 bool warn =
4485 warn_type_limits && !in_system_header_at (loc)
4486 && !(TREE_CODE (primop0) == INTEGER_CST
4487 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4488 primop0)))
4489 /* Do not warn for enumeration types. */
4490 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4492 switch (code)
4494 case GE_EXPR:
4495 if (warn)
4496 warning_at (loc, OPT_Wtype_limits,
4497 "comparison of unsigned expression >= 0 is always true");
4498 value = truthvalue_true_node;
4499 break;
4501 case LT_EXPR:
4502 if (warn)
4503 warning_at (loc, OPT_Wtype_limits,
4504 "comparison of unsigned expression < 0 is always false");
4505 value = truthvalue_false_node;
4506 break;
4508 default:
4509 break;
4512 if (value != 0)
4514 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4515 if (TREE_SIDE_EFFECTS (primop0))
4516 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4517 primop0, value);
4518 return value;
4523 *op0_ptr = convert (type, primop0);
4524 *op1_ptr = convert (type, primop1);
4526 *restype_ptr = truthvalue_type_node;
4528 return 0;
4531 /* Return a tree for the sum or difference (RESULTCODE says which)
4532 of pointer PTROP and integer INTOP. */
4534 tree
4535 pointer_int_sum (location_t loc, enum tree_code resultcode,
4536 tree ptrop, tree intop, bool complain)
4538 tree size_exp, ret;
4540 /* The result is a pointer of the same type that is being added. */
4541 tree result_type = TREE_TYPE (ptrop);
4543 /* If the pointer lives in UPC shared memory, then
4544 drop the 'shared' qualifier. */
4545 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4546 result_type = build_upc_unshared_type (result_type);
4548 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4550 if (complain && warn_pointer_arith)
4551 pedwarn (loc, OPT_Wpointer_arith,
4552 "pointer of type %<void *%> used in arithmetic");
4553 else if (!complain)
4554 return error_mark_node;
4555 size_exp = integer_one_node;
4557 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4559 if (complain && warn_pointer_arith)
4560 pedwarn (loc, OPT_Wpointer_arith,
4561 "pointer to a function used in arithmetic");
4562 else if (!complain)
4563 return error_mark_node;
4564 size_exp = integer_one_node;
4566 else
4567 size_exp = size_in_bytes (TREE_TYPE (result_type));
4569 /* We are manipulating pointer values, so we don't need to warn
4570 about relying on undefined signed overflow. We disable the
4571 warning here because we use integer types so fold won't know that
4572 they are really pointers. */
4573 fold_defer_overflow_warnings ();
4575 /* If what we are about to multiply by the size of the elements
4576 contains a constant term, apply distributive law
4577 and multiply that constant term separately.
4578 This helps produce common subexpressions. */
4579 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4580 && !TREE_CONSTANT (intop)
4581 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4582 && TREE_CONSTANT (size_exp)
4583 /* If the constant comes from pointer subtraction,
4584 skip this optimization--it would cause an error. */
4585 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4586 /* If the constant is unsigned, and smaller than the pointer size,
4587 then we must skip this optimization. This is because it could cause
4588 an overflow error if the constant is negative but INTOP is not. */
4589 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4590 || (TYPE_PRECISION (TREE_TYPE (intop))
4591 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4593 enum tree_code subcode = resultcode;
4594 tree int_type = TREE_TYPE (intop);
4595 if (TREE_CODE (intop) == MINUS_EXPR)
4596 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4597 /* Convert both subexpression types to the type of intop,
4598 because weird cases involving pointer arithmetic
4599 can result in a sum or difference with different type args. */
4600 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4601 subcode, ptrop,
4602 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4603 intop = convert (int_type, TREE_OPERAND (intop, 0));
4606 /* Convert the integer argument to a type the same size as sizetype
4607 so the multiply won't overflow spuriously. */
4608 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4609 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4610 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4611 TYPE_UNSIGNED (sizetype)), intop);
4613 /* Replace the integer argument with a suitable product by the object size.
4614 Do this multiplication as signed, then convert to the appropriate type
4615 for the pointer operation and disregard an overflow that occurred only
4616 because of the sign-extension change in the latter conversion. */
4618 tree t = build_binary_op (loc,
4619 MULT_EXPR, intop,
4620 convert (TREE_TYPE (intop), size_exp), 1);
4621 intop = convert (sizetype, t);
4622 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4623 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4626 /* Create the sum or difference. */
4627 if (resultcode == MINUS_EXPR)
4628 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4630 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4632 fold_undefer_and_ignore_overflow_warnings ();
4634 return ret;
4637 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4638 and if NON_CONST is known not to be permitted in an evaluated part
4639 of a constant expression. */
4641 tree
4642 c_wrap_maybe_const (tree expr, bool non_const)
4644 bool nowarning = TREE_NO_WARNING (expr);
4645 location_t loc = EXPR_LOCATION (expr);
4647 /* This should never be called for C++. */
4648 if (c_dialect_cxx ())
4649 gcc_unreachable ();
4651 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4652 STRIP_TYPE_NOPS (expr);
4653 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4654 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4655 if (nowarning)
4656 TREE_NO_WARNING (expr) = 1;
4657 protected_set_expr_location (expr, loc);
4659 return expr;
4662 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4663 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4664 around the SAVE_EXPR if needed so that c_fully_fold does not need
4665 to look inside SAVE_EXPRs. */
4667 tree
4668 c_save_expr (tree expr)
4670 bool maybe_const = true;
4671 if (c_dialect_cxx ())
4672 return save_expr (expr);
4673 expr = c_fully_fold (expr, false, &maybe_const);
4674 expr = save_expr (expr);
4675 if (!maybe_const)
4676 expr = c_wrap_maybe_const (expr, true);
4677 return expr;
4680 /* Return whether EXPR is a declaration whose address can never be
4681 NULL. */
4683 bool
4684 decl_with_nonnull_addr_p (const_tree expr)
4686 return (DECL_P (expr)
4687 && (TREE_CODE (expr) == PARM_DECL
4688 || TREE_CODE (expr) == LABEL_DECL
4689 || !DECL_WEAK (expr)));
4692 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4693 or for an `if' or `while' statement or ?..: exp. It should already
4694 have been validated to be of suitable type; otherwise, a bad
4695 diagnostic may result.
4697 The EXPR is located at LOCATION.
4699 This preparation consists of taking the ordinary
4700 representation of an expression expr and producing a valid tree
4701 boolean expression describing whether expr is nonzero. We could
4702 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4703 but we optimize comparisons, &&, ||, and !.
4705 The resulting type should always be `truthvalue_type_node'. */
4707 tree
4708 c_common_truthvalue_conversion (location_t location, tree expr)
4710 switch (TREE_CODE (expr))
4712 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4713 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4714 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4715 case ORDERED_EXPR: case UNORDERED_EXPR:
4716 if (TREE_TYPE (expr) == truthvalue_type_node)
4717 return expr;
4718 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4719 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4720 goto ret;
4722 case TRUTH_ANDIF_EXPR:
4723 case TRUTH_ORIF_EXPR:
4724 case TRUTH_AND_EXPR:
4725 case TRUTH_OR_EXPR:
4726 case TRUTH_XOR_EXPR:
4727 if (TREE_TYPE (expr) == truthvalue_type_node)
4728 return expr;
4729 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4730 c_common_truthvalue_conversion (location,
4731 TREE_OPERAND (expr, 0)),
4732 c_common_truthvalue_conversion (location,
4733 TREE_OPERAND (expr, 1)));
4734 goto ret;
4736 case TRUTH_NOT_EXPR:
4737 if (TREE_TYPE (expr) == truthvalue_type_node)
4738 return expr;
4739 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4740 c_common_truthvalue_conversion (location,
4741 TREE_OPERAND (expr, 0)));
4742 goto ret;
4744 case ERROR_MARK:
4745 return expr;
4747 case INTEGER_CST:
4748 return integer_zerop (expr) ? truthvalue_false_node
4749 : truthvalue_true_node;
4751 case REAL_CST:
4752 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4753 ? truthvalue_true_node
4754 : truthvalue_false_node;
4756 case FIXED_CST:
4757 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4758 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4759 ? truthvalue_true_node
4760 : truthvalue_false_node;
4762 case FUNCTION_DECL:
4763 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4764 /* Fall through. */
4766 case ADDR_EXPR:
4768 tree inner = TREE_OPERAND (expr, 0);
4769 if (decl_with_nonnull_addr_p (inner))
4771 /* Common Ada/Pascal programmer's mistake. */
4772 warning_at (location,
4773 OPT_Waddress,
4774 "the address of %qD will always evaluate as %<true%>",
4775 inner);
4776 return truthvalue_true_node;
4778 break;
4781 case COMPLEX_EXPR:
4782 expr = build_binary_op (EXPR_LOCATION (expr),
4783 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4784 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4785 c_common_truthvalue_conversion (location,
4786 TREE_OPERAND (expr, 0)),
4787 c_common_truthvalue_conversion (location,
4788 TREE_OPERAND (expr, 1)),
4790 goto ret;
4792 case NEGATE_EXPR:
4793 case ABS_EXPR:
4794 case FLOAT_EXPR:
4795 case EXCESS_PRECISION_EXPR:
4796 /* These don't change whether an object is nonzero or zero. */
4797 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4799 case LROTATE_EXPR:
4800 case RROTATE_EXPR:
4801 /* These don't change whether an object is zero or nonzero, but
4802 we can't ignore them if their second arg has side-effects. */
4803 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4805 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4806 TREE_OPERAND (expr, 1),
4807 c_common_truthvalue_conversion
4808 (location, TREE_OPERAND (expr, 0)));
4809 goto ret;
4811 else
4812 return c_common_truthvalue_conversion (location,
4813 TREE_OPERAND (expr, 0));
4815 case COND_EXPR:
4816 /* Distribute the conversion into the arms of a COND_EXPR. */
4817 if (c_dialect_cxx ())
4819 tree op1 = TREE_OPERAND (expr, 1);
4820 tree op2 = TREE_OPERAND (expr, 2);
4821 /* In C++ one of the arms might have void type if it is throw. */
4822 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4823 op1 = c_common_truthvalue_conversion (location, op1);
4824 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4825 op2 = c_common_truthvalue_conversion (location, op2);
4826 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4827 TREE_OPERAND (expr, 0), op1, op2);
4828 goto ret;
4830 else
4832 /* Folding will happen later for C. */
4833 expr = build3 (COND_EXPR, truthvalue_type_node,
4834 TREE_OPERAND (expr, 0),
4835 c_common_truthvalue_conversion (location,
4836 TREE_OPERAND (expr, 1)),
4837 c_common_truthvalue_conversion (location,
4838 TREE_OPERAND (expr, 2)));
4839 goto ret;
4842 CASE_CONVERT:
4844 tree totype = TREE_TYPE (expr);
4845 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4847 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4848 since that affects how `default_conversion' will behave. */
4849 if (TREE_CODE (totype) == REFERENCE_TYPE
4850 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4851 break;
4852 /* Don't strip a conversion from C++0x scoped enum, since they
4853 don't implicitly convert to other types. */
4854 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4855 && ENUM_IS_SCOPED (fromtype))
4856 break;
4857 /* If this isn't narrowing the argument, we can ignore it. */
4858 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4859 return c_common_truthvalue_conversion (location,
4860 TREE_OPERAND (expr, 0));
4862 break;
4864 case MODIFY_EXPR:
4865 if (!TREE_NO_WARNING (expr)
4866 && warn_parentheses)
4868 warning (OPT_Wparentheses,
4869 "suggest parentheses around assignment used as truth value");
4870 TREE_NO_WARNING (expr) = 1;
4872 break;
4874 default:
4875 break;
4878 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4880 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4881 expr = (build_binary_op
4882 (EXPR_LOCATION (expr),
4883 (TREE_SIDE_EFFECTS (expr)
4884 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4885 c_common_truthvalue_conversion
4886 (location,
4887 build_unary_op (location, REALPART_EXPR, t, 0)),
4888 c_common_truthvalue_conversion
4889 (location,
4890 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4891 0));
4892 goto ret;
4895 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4897 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4898 FCONST0 (TYPE_MODE
4899 (TREE_TYPE (expr))));
4900 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4902 else
4903 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4905 ret:
4906 protected_set_expr_location (expr, location);
4907 return expr;
4910 static void def_builtin_1 (enum built_in_function fncode,
4911 const char *name,
4912 enum built_in_class fnclass,
4913 tree fntype, tree libtype,
4914 bool both_p, bool fallback_p, bool nonansi_p,
4915 tree fnattrs, bool implicit_p);
4918 /* Apply the TYPE_QUALS to the new DECL. */
4920 void
4921 c_apply_type_quals_to_decl (int type_quals, tree decl)
4923 tree type = TREE_TYPE (decl);
4925 if (type == error_mark_node)
4926 return;
4928 if ((type_quals & TYPE_QUAL_CONST)
4929 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4930 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4931 constructor can produce constant init, so rely on cp_finish_decl to
4932 clear TREE_READONLY if the variable has non-constant init. */
4933 TREE_READONLY (decl) = 1;
4934 if (type_quals & TYPE_QUAL_VOLATILE)
4936 TREE_SIDE_EFFECTS (decl) = 1;
4937 TREE_THIS_VOLATILE (decl) = 1;
4939 if (type_quals & TYPE_QUAL_RESTRICT)
4941 while (type && TREE_CODE (type) == ARRAY_TYPE)
4942 /* Allow 'restrict' on arrays of pointers.
4943 FIXME currently we just ignore it. */
4944 type = TREE_TYPE (type);
4945 if (!type
4946 || !POINTER_TYPE_P (type)
4947 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4948 error ("invalid use of %<restrict%>");
4950 if (type_quals & TYPE_QUAL_UPC_SHARED)
4952 TREE_SHARED (decl) = 1;
4953 if (type_quals & TYPE_QUAL_UPC_STRICT)
4954 TREE_STRICT(decl) = 1;
4955 else if (type_quals & TYPE_QUAL_UPC_RELAXED)
4956 TREE_RELAXED(decl) = 1;
4957 /* The declaration's type should have been previously defined
4958 as a UPC shared type. */
4959 gcc_assert (upc_shared_type_p (type));
4963 struct c_type_hasher : ggc_hasher<tree>
4965 static hashval_t hash (tree);
4966 static bool equal (tree, tree);
4969 /* Hash function for the problem of multiple type definitions in
4970 different files. This must hash all types that will compare
4971 equal via comptypes to the same value. In practice it hashes
4972 on some of the simple stuff and leaves the details to comptypes. */
4974 hashval_t
4975 c_type_hasher::hash (tree t)
4977 int n_elements;
4978 int shift, size;
4979 tree t2;
4980 switch (TREE_CODE (t))
4982 /* For pointers, hash on pointee type plus some swizzling. */
4983 case POINTER_TYPE:
4984 return hash (TREE_TYPE (t)) ^ 0x3003003;
4985 /* Hash on number of elements and total size. */
4986 case ENUMERAL_TYPE:
4987 shift = 3;
4988 t2 = TYPE_VALUES (t);
4989 break;
4990 case RECORD_TYPE:
4991 shift = 0;
4992 t2 = TYPE_FIELDS (t);
4993 break;
4994 case QUAL_UNION_TYPE:
4995 shift = 1;
4996 t2 = TYPE_FIELDS (t);
4997 break;
4998 case UNION_TYPE:
4999 shift = 2;
5000 t2 = TYPE_FIELDS (t);
5001 break;
5002 default:
5003 gcc_unreachable ();
5005 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5006 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5007 n_elements = list_length (t2);
5008 /* We might have a VLA here. */
5009 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5010 size = 0;
5011 else
5012 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
5013 return ((size << 24) | (n_elements << shift));
5016 bool
5017 c_type_hasher::equal (tree t1, tree t2)
5019 return lang_hooks.types_compatible_p (t1, t2);
5022 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
5024 /* Return the typed-based alias set for T, which may be an expression
5025 or a type. Return -1 if we don't do anything special. */
5027 alias_set_type
5028 c_common_get_alias_set (tree t)
5030 tree u;
5032 /* For VLAs, use the alias set of the element type rather than the
5033 default of alias set 0 for types compared structurally. */
5034 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5036 if (TREE_CODE (t) == ARRAY_TYPE)
5037 return get_alias_set (TREE_TYPE (t));
5038 return -1;
5041 /* Permit type-punning when accessing a union, provided the access
5042 is directly through the union. For example, this code does not
5043 permit taking the address of a union member and then storing
5044 through it. Even the type-punning allowed here is a GCC
5045 extension, albeit a common and useful one; the C standard says
5046 that such accesses have implementation-defined behavior. */
5047 for (u = t;
5048 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5049 u = TREE_OPERAND (u, 0))
5050 if (TREE_CODE (u) == COMPONENT_REF
5051 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5052 return 0;
5054 /* That's all the expressions we handle specially. */
5055 if (!TYPE_P (t))
5056 return -1;
5058 /* The C standard guarantees that any object may be accessed via an
5059 lvalue that has character type. */
5060 if (t == char_type_node
5061 || t == signed_char_type_node
5062 || t == unsigned_char_type_node)
5063 return 0;
5065 /* The C standard specifically allows aliasing between signed and
5066 unsigned variants of the same type. We treat the signed
5067 variant as canonical. */
5068 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5070 tree t1 = c_common_signed_type (t);
5072 /* t1 == t can happen for boolean nodes which are always unsigned. */
5073 if (t1 != t)
5074 return get_alias_set (t1);
5077 /* For the time being, make UPC pointers-to-shared conflict
5078 with everything else. Ideally, UPC pointers-to-shared should
5079 only conflict with the internal type used to represent
5080 the UPC pointer-to-shared (i.e., upc_pts_rep_type_node). */
5082 if (TYPE_P (t) ? (TREE_CODE (t) == POINTER_TYPE
5083 && upc_shared_type_p (TREE_TYPE (t)))
5084 : (TREE_TYPE(t)
5085 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
5086 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (t)))))
5087 return 0;
5089 /* Handle the case of multiple type nodes referring to "the same" type,
5090 which occurs with IMA. These share an alias set. FIXME: Currently only
5091 C90 is handled. (In C99 type compatibility is not transitive, which
5092 complicates things mightily. The alias set splay trees can theoretically
5093 represent this, but insertion is tricky when you consider all the
5094 different orders things might arrive in.) */
5096 if (c_language != clk_c || flag_isoc99)
5097 return -1;
5099 /* Save time if there's only one input file. */
5100 if (num_in_fnames == 1)
5101 return -1;
5103 /* Pointers need special handling if they point to any type that
5104 needs special handling (below). */
5105 if (TREE_CODE (t) == POINTER_TYPE)
5107 tree t2;
5108 /* Find bottom type under any nested POINTERs. */
5109 for (t2 = TREE_TYPE (t);
5110 TREE_CODE (t2) == POINTER_TYPE;
5111 t2 = TREE_TYPE (t2))
5113 if (TREE_CODE (t2) != RECORD_TYPE
5114 && TREE_CODE (t2) != ENUMERAL_TYPE
5115 && TREE_CODE (t2) != QUAL_UNION_TYPE
5116 && TREE_CODE (t2) != UNION_TYPE)
5117 return -1;
5118 if (TYPE_SIZE (t2) == 0)
5119 return -1;
5121 /* These are the only cases that need special handling. */
5122 if (TREE_CODE (t) != RECORD_TYPE
5123 && TREE_CODE (t) != ENUMERAL_TYPE
5124 && TREE_CODE (t) != QUAL_UNION_TYPE
5125 && TREE_CODE (t) != UNION_TYPE
5126 && TREE_CODE (t) != POINTER_TYPE)
5127 return -1;
5128 /* Undefined? */
5129 if (TYPE_SIZE (t) == 0)
5130 return -1;
5132 /* Look up t in hash table. Only one of the compatible types within each
5133 alias set is recorded in the table. */
5134 if (!type_hash_table)
5135 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5136 tree *slot = type_hash_table->find_slot (t, INSERT);
5137 if (*slot != NULL)
5139 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5140 return TYPE_ALIAS_SET ((tree)*slot);
5142 else
5143 /* Our caller will assign and record (in t) a new alias set; all we need
5144 to do is remember t in the hash table. */
5145 *slot = t;
5147 return -1;
5150 /* Return the value of THREADS.
5152 UPC defines a reserved variable, THREADS, which returns the
5153 number of threads that will be created when the UPC program
5154 executes. The value of threads can be specified at runtime via
5155 the -fupc-threads=N switch, where N is an integer specifying
5156 the number of threads. When the value of THREADS is specified
5157 at compile-time, this is called the "static threads compilation
5158 environment".
5160 In the static threads compilation environment, THREADS is a
5161 pre-defined preprocessor macro with the value, N.
5163 If no value for threads is given at compile-time, then the value
5164 must be specified when the application program is executed.
5165 This method of establishing the value of THREADS is called
5166 the "dynamic threads compilation environment". */
5168 tree
5169 upc_num_threads (void)
5171 tree n;
5172 gcc_assert (flag_upc);
5173 n = flag_upc_threads ? ssize_int (flag_upc_threads)
5174 : lookup_name (get_identifier ("THREADS"));
5175 if (!n)
5177 error ("the UPC-required THREADS variable is undefined; "
5178 "when compiling pre-processed source, "
5179 "all -fupc-* switches must be passed on the command line, "
5180 "asserting the same values as supplied when the "
5181 "original source file was preprocessed");
5182 abort ();
5185 return n;
5188 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5189 the IS_SIZEOF parameter indicates which operator is being applied.
5190 The COMPLAIN flag controls whether we should diagnose possibly
5191 ill-formed constructs or not. LOC is the location of the SIZEOF or
5192 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5193 a type in any context should be returned, rather than the normal
5194 alignment for that type. */
5196 tree
5197 c_sizeof_or_alignof_type (location_t loc,
5198 tree type, bool is_sizeof, bool min_alignof,
5199 int complain)
5201 const char *op_name;
5202 tree value = NULL;
5203 enum tree_code type_code = TREE_CODE (type);
5205 op_name = is_sizeof ? "sizeof" : "__alignof__";
5207 if (type_code == FUNCTION_TYPE)
5209 if (is_sizeof)
5211 if (complain && warn_pointer_arith)
5212 pedwarn (loc, OPT_Wpointer_arith,
5213 "invalid application of %<sizeof%> to a function type");
5214 else if (!complain)
5215 return error_mark_node;
5216 value = size_one_node;
5218 else
5220 if (complain)
5222 if (c_dialect_cxx ())
5223 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5224 "%<alignof%> applied to a function type");
5225 else
5226 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5227 "%<_Alignof%> applied to a function type");
5229 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5232 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5234 if (complain)
5236 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5237 error_at (loc, "invalid application of %qs"
5238 " to %<shared void%> type", op_name);
5239 else if (type_code == VOID_TYPE && warn_pointer_arith)
5240 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5241 "invalid application of %qs to a void type", op_name);
5243 else
5244 return error_mark_node;
5245 value = size_one_node;
5247 else if (!COMPLETE_TYPE_P (type)
5248 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5250 if (complain)
5251 error_at (loc, "invalid application of %qs to incomplete type %qT",
5252 op_name, type);
5253 return error_mark_node;
5255 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5256 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5258 if (complain)
5259 error_at (loc, "invalid application of %qs to array type %qT of "
5260 "incomplete element type", op_name, type);
5261 return error_mark_node;
5263 else
5265 if (is_sizeof)
5266 /* Convert in case a char is more than one unit. */
5267 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5268 size_int (TYPE_PRECISION (char_type_node)
5269 / BITS_PER_UNIT));
5270 else if (min_alignof)
5271 value = size_int (min_align_of_type (type));
5272 else
5273 value = size_int (TYPE_ALIGN_UNIT (type));
5276 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5277 && upc_shared_type_p (type)
5278 && TYPE_HAS_THREADS_FACTOR (type))
5280 const tree n_threads = convert (sizetype, upc_num_threads ());
5281 value = size_binop (MULT_EXPR, value, n_threads);
5284 /* VALUE will have the middle-end integer type sizetype.
5285 However, we should really return a value of type `size_t',
5286 which is just a typedef for an ordinary integer type. */
5287 value = fold_convert_loc (loc, size_type_node, value);
5289 return value;
5292 /* Implement the __alignof keyword: Return the minimum required
5293 alignment of EXPR, measured in bytes. For VAR_DECLs,
5294 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5295 from an "aligned" __attribute__ specification). LOC is the
5296 location of the ALIGNOF operator. */
5298 tree
5299 c_alignof_expr (location_t loc, tree expr)
5301 tree t;
5303 if (VAR_OR_FUNCTION_DECL_P (expr))
5304 t = size_int (DECL_ALIGN_UNIT (expr));
5306 else if (TREE_CODE (expr) == COMPONENT_REF
5307 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5309 error_at (loc, "%<__alignof%> applied to a bit-field");
5310 t = size_one_node;
5312 else if (TREE_CODE (expr) == COMPONENT_REF
5313 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5314 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5316 else if (TREE_CODE (expr) == INDIRECT_REF)
5318 tree t = TREE_OPERAND (expr, 0);
5319 tree best = t;
5320 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5322 while (CONVERT_EXPR_P (t)
5323 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5325 int thisalign;
5327 t = TREE_OPERAND (t, 0);
5328 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5329 if (thisalign > bestalign)
5330 best = t, bestalign = thisalign;
5332 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5334 else
5335 return c_alignof (loc, TREE_TYPE (expr));
5337 return fold_convert_loc (loc, size_type_node, t);
5340 /* Handle C and C++ default attributes. */
5342 enum built_in_attribute
5344 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5345 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5346 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5347 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5348 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5349 #include "builtin-attrs.def"
5350 #undef DEF_ATTR_NULL_TREE
5351 #undef DEF_ATTR_INT
5352 #undef DEF_ATTR_STRING
5353 #undef DEF_ATTR_IDENT
5354 #undef DEF_ATTR_TREE_LIST
5355 ATTR_LAST
5358 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5360 static void c_init_attributes (void);
5362 enum c_builtin_type
5364 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5365 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5366 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5367 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5368 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5369 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5370 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5371 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5372 ARG6) NAME,
5373 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5374 ARG6, ARG7) NAME,
5375 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5376 ARG6, ARG7, ARG8) NAME,
5377 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5378 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5379 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5380 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5381 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5382 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5383 NAME,
5384 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5385 ARG6, ARG7) NAME,
5386 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5387 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5388 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5389 #include "builtin-types.def"
5390 #undef DEF_PRIMITIVE_TYPE
5391 #undef DEF_FUNCTION_TYPE_0
5392 #undef DEF_FUNCTION_TYPE_1
5393 #undef DEF_FUNCTION_TYPE_2
5394 #undef DEF_FUNCTION_TYPE_3
5395 #undef DEF_FUNCTION_TYPE_4
5396 #undef DEF_FUNCTION_TYPE_5
5397 #undef DEF_FUNCTION_TYPE_6
5398 #undef DEF_FUNCTION_TYPE_7
5399 #undef DEF_FUNCTION_TYPE_8
5400 #undef DEF_FUNCTION_TYPE_VAR_0
5401 #undef DEF_FUNCTION_TYPE_VAR_1
5402 #undef DEF_FUNCTION_TYPE_VAR_2
5403 #undef DEF_FUNCTION_TYPE_VAR_3
5404 #undef DEF_FUNCTION_TYPE_VAR_4
5405 #undef DEF_FUNCTION_TYPE_VAR_5
5406 #undef DEF_FUNCTION_TYPE_VAR_7
5407 #undef DEF_FUNCTION_TYPE_VAR_11
5408 #undef DEF_POINTER_TYPE
5409 BT_LAST
5412 typedef enum c_builtin_type builtin_type;
5414 /* A temporary array for c_common_nodes_and_builtins. Used in
5415 communication with def_fn_type. */
5416 static tree builtin_types[(int) BT_LAST + 1];
5418 /* A helper function for c_common_nodes_and_builtins. Build function type
5419 for DEF with return type RET and N arguments. If VAR is true, then the
5420 function should be variadic after those N arguments.
5422 Takes special care not to ICE if any of the types involved are
5423 error_mark_node, which indicates that said type is not in fact available
5424 (see builtin_type_for_size). In which case the function type as a whole
5425 should be error_mark_node. */
5427 static void
5428 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5430 tree t;
5431 tree *args = XALLOCAVEC (tree, n);
5432 va_list list;
5433 int i;
5435 va_start (list, n);
5436 for (i = 0; i < n; ++i)
5438 builtin_type a = (builtin_type) va_arg (list, int);
5439 t = builtin_types[a];
5440 if (t == error_mark_node)
5441 goto egress;
5442 args[i] = t;
5445 t = builtin_types[ret];
5446 if (t == error_mark_node)
5447 goto egress;
5448 if (var)
5449 t = build_varargs_function_type_array (t, n, args);
5450 else
5451 t = build_function_type_array (t, n, args);
5453 egress:
5454 builtin_types[def] = t;
5455 va_end (list);
5458 /* Build builtin functions common to both C and C++ language
5459 frontends. */
5461 static void
5462 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5464 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5465 builtin_types[ENUM] = VALUE;
5466 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5467 def_fn_type (ENUM, RETURN, 0, 0);
5468 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5469 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5470 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5471 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5472 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5473 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5474 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5475 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5476 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5477 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5478 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5479 ARG6) \
5480 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5481 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5482 ARG6, ARG7) \
5483 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5484 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5485 ARG6, ARG7, ARG8) \
5486 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5487 ARG7, ARG8);
5488 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5489 def_fn_type (ENUM, RETURN, 1, 0);
5490 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5491 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5492 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5493 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5494 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5495 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5496 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5497 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5498 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5499 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5500 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5501 ARG6, ARG7) \
5502 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5503 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5504 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5505 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5506 ARG7, ARG8, ARG9, ARG10, ARG11);
5507 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5508 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5510 #include "builtin-types.def"
5512 #undef DEF_PRIMITIVE_TYPE
5513 #undef DEF_FUNCTION_TYPE_0
5514 #undef DEF_FUNCTION_TYPE_1
5515 #undef DEF_FUNCTION_TYPE_2
5516 #undef DEF_FUNCTION_TYPE_3
5517 #undef DEF_FUNCTION_TYPE_4
5518 #undef DEF_FUNCTION_TYPE_5
5519 #undef DEF_FUNCTION_TYPE_6
5520 #undef DEF_FUNCTION_TYPE_7
5521 #undef DEF_FUNCTION_TYPE_8
5522 #undef DEF_FUNCTION_TYPE_VAR_0
5523 #undef DEF_FUNCTION_TYPE_VAR_1
5524 #undef DEF_FUNCTION_TYPE_VAR_2
5525 #undef DEF_FUNCTION_TYPE_VAR_3
5526 #undef DEF_FUNCTION_TYPE_VAR_4
5527 #undef DEF_FUNCTION_TYPE_VAR_5
5528 #undef DEF_FUNCTION_TYPE_VAR_7
5529 #undef DEF_FUNCTION_TYPE_VAR_11
5530 #undef DEF_POINTER_TYPE
5531 builtin_types[(int) BT_LAST] = NULL_TREE;
5533 c_init_attributes ();
5535 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5536 NONANSI_P, ATTRS, IMPLICIT, COND) \
5537 if (NAME && COND) \
5538 def_builtin_1 (ENUM, NAME, CLASS, \
5539 builtin_types[(int) TYPE], \
5540 builtin_types[(int) LIBTYPE], \
5541 BOTH_P, FALLBACK_P, NONANSI_P, \
5542 built_in_attributes[(int) ATTRS], IMPLICIT);
5543 #include "builtins.def"
5544 #undef DEF_BUILTIN
5546 targetm.init_builtins ();
5548 build_common_builtin_nodes ();
5550 if (flag_cilkplus)
5551 cilk_init_builtins ();
5554 /* Like get_identifier, but avoid warnings about null arguments when
5555 the argument may be NULL for targets where GCC lacks stdint.h type
5556 information. */
5558 static inline tree
5559 c_get_ident (const char *id)
5561 return get_identifier (id);
5564 /* Build tree nodes and builtin functions common to both C and C++ language
5565 frontends. */
5567 void
5568 c_common_nodes_and_builtins (void)
5570 int char16_type_size;
5571 int char32_type_size;
5572 int wchar_type_size;
5573 tree array_domain_type;
5574 tree va_list_ref_type_node;
5575 tree va_list_arg_type_node;
5576 int i;
5578 build_common_tree_nodes (flag_signed_char, flag_short_double);
5580 /* Define `int' and `char' first so that dbx will output them first. */
5581 record_builtin_type (RID_INT, NULL, integer_type_node);
5582 record_builtin_type (RID_CHAR, "char", char_type_node);
5584 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5585 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5586 but not C. Are the conditionals here needed? */
5587 if (c_dialect_cxx ())
5588 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5589 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5590 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5591 record_builtin_type (RID_MAX, "long unsigned int",
5592 long_unsigned_type_node);
5594 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5596 char name[25];
5598 sprintf (name, "__int%d", int_n_data[i].bitsize);
5599 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5600 int_n_trees[i].signed_type);
5601 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5602 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5605 if (c_dialect_cxx ())
5606 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5607 record_builtin_type (RID_MAX, "long long int",
5608 long_long_integer_type_node);
5609 record_builtin_type (RID_MAX, "long long unsigned int",
5610 long_long_unsigned_type_node);
5611 if (c_dialect_cxx ())
5612 record_builtin_type (RID_MAX, "long long unsigned",
5613 long_long_unsigned_type_node);
5614 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5615 record_builtin_type (RID_MAX, "short unsigned int",
5616 short_unsigned_type_node);
5617 if (c_dialect_cxx ())
5618 record_builtin_type (RID_MAX, "unsigned short",
5619 short_unsigned_type_node);
5621 /* Define both `signed char' and `unsigned char'. */
5622 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5623 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5625 /* These are types that c_common_type_for_size and
5626 c_common_type_for_mode use. */
5627 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5628 TYPE_DECL, NULL_TREE,
5629 intQI_type_node));
5630 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5631 TYPE_DECL, NULL_TREE,
5632 intHI_type_node));
5633 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5634 TYPE_DECL, NULL_TREE,
5635 intSI_type_node));
5636 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5637 TYPE_DECL, NULL_TREE,
5638 intDI_type_node));
5639 #if HOST_BITS_PER_WIDE_INT >= 64
5640 /* Note that this is different than the __int128 type that's part of
5641 the generic __intN support. */
5642 if (targetm.scalar_mode_supported_p (TImode))
5643 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5644 TYPE_DECL,
5645 get_identifier ("__int128_t"),
5646 intTI_type_node));
5647 #endif
5648 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5649 TYPE_DECL, NULL_TREE,
5650 unsigned_intQI_type_node));
5651 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5652 TYPE_DECL, NULL_TREE,
5653 unsigned_intHI_type_node));
5654 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5655 TYPE_DECL, NULL_TREE,
5656 unsigned_intSI_type_node));
5657 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5658 TYPE_DECL, NULL_TREE,
5659 unsigned_intDI_type_node));
5660 #if HOST_BITS_PER_WIDE_INT >= 64
5661 if (targetm.scalar_mode_supported_p (TImode))
5662 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5663 TYPE_DECL,
5664 get_identifier ("__uint128_t"),
5665 unsigned_intTI_type_node));
5666 #endif
5668 /* Create the widest literal types. */
5669 widest_integer_literal_type_node
5670 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5671 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5672 TYPE_DECL, NULL_TREE,
5673 widest_integer_literal_type_node));
5675 widest_unsigned_literal_type_node
5676 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5677 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5678 TYPE_DECL, NULL_TREE,
5679 widest_unsigned_literal_type_node));
5681 signed_size_type_node = c_common_signed_type (size_type_node);
5683 pid_type_node =
5684 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5686 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5687 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5688 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5690 /* Only supported decimal floating point extension if the target
5691 actually supports underlying modes. */
5692 if (targetm.scalar_mode_supported_p (SDmode)
5693 && targetm.scalar_mode_supported_p (DDmode)
5694 && targetm.scalar_mode_supported_p (TDmode))
5696 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5697 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5698 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5701 if (targetm.fixed_point_supported_p ())
5703 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5704 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5705 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5706 record_builtin_type (RID_MAX, "long long _Fract",
5707 long_long_fract_type_node);
5708 record_builtin_type (RID_MAX, "unsigned short _Fract",
5709 unsigned_short_fract_type_node);
5710 record_builtin_type (RID_MAX, "unsigned _Fract",
5711 unsigned_fract_type_node);
5712 record_builtin_type (RID_MAX, "unsigned long _Fract",
5713 unsigned_long_fract_type_node);
5714 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5715 unsigned_long_long_fract_type_node);
5716 record_builtin_type (RID_MAX, "_Sat short _Fract",
5717 sat_short_fract_type_node);
5718 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5719 record_builtin_type (RID_MAX, "_Sat long _Fract",
5720 sat_long_fract_type_node);
5721 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5722 sat_long_long_fract_type_node);
5723 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5724 sat_unsigned_short_fract_type_node);
5725 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5726 sat_unsigned_fract_type_node);
5727 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5728 sat_unsigned_long_fract_type_node);
5729 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5730 sat_unsigned_long_long_fract_type_node);
5731 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5732 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5733 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5734 record_builtin_type (RID_MAX, "long long _Accum",
5735 long_long_accum_type_node);
5736 record_builtin_type (RID_MAX, "unsigned short _Accum",
5737 unsigned_short_accum_type_node);
5738 record_builtin_type (RID_MAX, "unsigned _Accum",
5739 unsigned_accum_type_node);
5740 record_builtin_type (RID_MAX, "unsigned long _Accum",
5741 unsigned_long_accum_type_node);
5742 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5743 unsigned_long_long_accum_type_node);
5744 record_builtin_type (RID_MAX, "_Sat short _Accum",
5745 sat_short_accum_type_node);
5746 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5747 record_builtin_type (RID_MAX, "_Sat long _Accum",
5748 sat_long_accum_type_node);
5749 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5750 sat_long_long_accum_type_node);
5751 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5752 sat_unsigned_short_accum_type_node);
5753 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5754 sat_unsigned_accum_type_node);
5755 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5756 sat_unsigned_long_accum_type_node);
5757 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5758 sat_unsigned_long_long_accum_type_node);
5762 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5763 TYPE_DECL,
5764 get_identifier ("complex int"),
5765 complex_integer_type_node));
5766 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5767 TYPE_DECL,
5768 get_identifier ("complex float"),
5769 complex_float_type_node));
5770 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5771 TYPE_DECL,
5772 get_identifier ("complex double"),
5773 complex_double_type_node));
5774 lang_hooks.decls.pushdecl
5775 (build_decl (UNKNOWN_LOCATION,
5776 TYPE_DECL, get_identifier ("complex long double"),
5777 complex_long_double_type_node));
5779 if (c_dialect_cxx ())
5780 /* For C++, make fileptr_type_node a distinct void * type until
5781 FILE type is defined. */
5782 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5784 record_builtin_type (RID_VOID, NULL, void_type_node);
5786 /* Set the TYPE_NAME for any variants that were built before
5787 record_builtin_type gave names to the built-in types. */
5789 tree void_name = TYPE_NAME (void_type_node);
5790 TYPE_NAME (void_type_node) = NULL_TREE;
5791 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5792 = void_name;
5793 TYPE_NAME (void_type_node) = void_name;
5796 void_list_node = build_void_list_node ();
5798 /* Make a type to be the domain of a few array types
5799 whose domains don't really matter.
5800 200 is small enough that it always fits in size_t
5801 and large enough that it can hold most function names for the
5802 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5803 array_domain_type = build_index_type (size_int (200));
5805 /* Make a type for arrays of characters.
5806 With luck nothing will ever really depend on the length of this
5807 array type. */
5808 char_array_type_node
5809 = build_array_type (char_type_node, array_domain_type);
5811 string_type_node = build_pointer_type (char_type_node);
5812 const_string_type_node
5813 = build_pointer_type (build_qualified_type
5814 (char_type_node, TYPE_QUAL_CONST));
5816 /* This is special for C++ so functions can be overloaded. */
5817 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5818 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5819 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5820 underlying_wchar_type_node = wchar_type_node;
5821 if (c_dialect_cxx ())
5823 if (TYPE_UNSIGNED (wchar_type_node))
5824 wchar_type_node = make_unsigned_type (wchar_type_size);
5825 else
5826 wchar_type_node = make_signed_type (wchar_type_size);
5827 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5830 /* This is for wide string constants. */
5831 wchar_array_type_node
5832 = build_array_type (wchar_type_node, array_domain_type);
5834 /* Define 'char16_t'. */
5835 char16_type_node = get_identifier (CHAR16_TYPE);
5836 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5837 char16_type_size = TYPE_PRECISION (char16_type_node);
5838 if (c_dialect_cxx ())
5840 char16_type_node = make_unsigned_type (char16_type_size);
5842 if (cxx_dialect >= cxx11)
5843 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5846 /* This is for UTF-16 string constants. */
5847 char16_array_type_node
5848 = build_array_type (char16_type_node, array_domain_type);
5850 /* Define 'char32_t'. */
5851 char32_type_node = get_identifier (CHAR32_TYPE);
5852 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5853 char32_type_size = TYPE_PRECISION (char32_type_node);
5854 if (c_dialect_cxx ())
5856 char32_type_node = make_unsigned_type (char32_type_size);
5858 if (cxx_dialect >= cxx11)
5859 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5862 /* This is for UTF-32 string constants. */
5863 char32_array_type_node
5864 = build_array_type (char32_type_node, array_domain_type);
5866 wint_type_node =
5867 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5869 intmax_type_node =
5870 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5871 uintmax_type_node =
5872 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5874 if (SIG_ATOMIC_TYPE)
5875 sig_atomic_type_node =
5876 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5877 if (INT8_TYPE)
5878 int8_type_node =
5879 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5880 if (INT16_TYPE)
5881 int16_type_node =
5882 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5883 if (INT32_TYPE)
5884 int32_type_node =
5885 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5886 if (INT64_TYPE)
5887 int64_type_node =
5888 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5889 if (UINT8_TYPE)
5890 uint8_type_node =
5891 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5892 if (UINT16_TYPE)
5893 c_uint16_type_node = uint16_type_node =
5894 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5895 if (UINT32_TYPE)
5896 c_uint32_type_node = uint32_type_node =
5897 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5898 if (UINT64_TYPE)
5899 c_uint64_type_node = uint64_type_node =
5900 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5901 if (INT_LEAST8_TYPE)
5902 int_least8_type_node =
5903 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5904 if (INT_LEAST16_TYPE)
5905 int_least16_type_node =
5906 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5907 if (INT_LEAST32_TYPE)
5908 int_least32_type_node =
5909 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5910 if (INT_LEAST64_TYPE)
5911 int_least64_type_node =
5912 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5913 if (UINT_LEAST8_TYPE)
5914 uint_least8_type_node =
5915 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5916 if (UINT_LEAST16_TYPE)
5917 uint_least16_type_node =
5918 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5919 if (UINT_LEAST32_TYPE)
5920 uint_least32_type_node =
5921 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5922 if (UINT_LEAST64_TYPE)
5923 uint_least64_type_node =
5924 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5925 if (INT_FAST8_TYPE)
5926 int_fast8_type_node =
5927 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5928 if (INT_FAST16_TYPE)
5929 int_fast16_type_node =
5930 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5931 if (INT_FAST32_TYPE)
5932 int_fast32_type_node =
5933 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5934 if (INT_FAST64_TYPE)
5935 int_fast64_type_node =
5936 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5937 if (UINT_FAST8_TYPE)
5938 uint_fast8_type_node =
5939 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5940 if (UINT_FAST16_TYPE)
5941 uint_fast16_type_node =
5942 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5943 if (UINT_FAST32_TYPE)
5944 uint_fast32_type_node =
5945 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5946 if (UINT_FAST64_TYPE)
5947 uint_fast64_type_node =
5948 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5949 if (INTPTR_TYPE)
5950 intptr_type_node =
5951 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5952 if (UINTPTR_TYPE)
5953 uintptr_type_node =
5954 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5956 default_function_type
5957 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5958 ptrdiff_type_node
5959 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5960 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5962 lang_hooks.decls.pushdecl
5963 (build_decl (UNKNOWN_LOCATION,
5964 TYPE_DECL, get_identifier ("__builtin_va_list"),
5965 va_list_type_node));
5966 if (targetm.enum_va_list_p)
5968 int l;
5969 const char *pname;
5970 tree ptype;
5972 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5974 lang_hooks.decls.pushdecl
5975 (build_decl (UNKNOWN_LOCATION,
5976 TYPE_DECL, get_identifier (pname),
5977 ptype));
5982 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5984 va_list_arg_type_node = va_list_ref_type_node =
5985 build_pointer_type (TREE_TYPE (va_list_type_node));
5987 else
5989 va_list_arg_type_node = va_list_type_node;
5990 va_list_ref_type_node = build_reference_type (va_list_type_node);
5993 if (!flag_preprocess_only)
5994 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5996 main_identifier_node = get_identifier ("main");
5998 /* Create the built-in __null node. It is important that this is
5999 not shared. */
6000 null_node = make_int_cst (1, 1);
6001 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
6003 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6004 memset (builtin_types, 0, sizeof (builtin_types));
6007 /* The number of named compound-literals generated thus far. */
6008 static GTY(()) int compound_literal_number;
6010 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6012 void
6013 set_compound_literal_name (tree decl)
6015 char *name;
6016 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6017 compound_literal_number);
6018 compound_literal_number++;
6019 DECL_NAME (decl) = get_identifier (name);
6022 tree
6023 build_va_arg (location_t loc, tree expr, tree type)
6025 expr = build1 (VA_ARG_EXPR, type, expr);
6026 SET_EXPR_LOCATION (expr, loc);
6027 return expr;
6031 /* Linked list of disabled built-in functions. */
6033 typedef struct disabled_builtin
6035 const char *name;
6036 struct disabled_builtin *next;
6037 } disabled_builtin;
6038 static disabled_builtin *disabled_builtins = NULL;
6040 static bool builtin_function_disabled_p (const char *);
6042 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6043 begins with "__builtin_", give an error. */
6045 void
6046 disable_builtin_function (const char *name)
6048 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6049 error ("cannot disable built-in function %qs", name);
6050 else
6052 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6053 new_disabled_builtin->name = name;
6054 new_disabled_builtin->next = disabled_builtins;
6055 disabled_builtins = new_disabled_builtin;
6060 /* Return true if the built-in function NAME has been disabled, false
6061 otherwise. */
6063 static bool
6064 builtin_function_disabled_p (const char *name)
6066 disabled_builtin *p;
6067 for (p = disabled_builtins; p != NULL; p = p->next)
6069 if (strcmp (name, p->name) == 0)
6070 return true;
6072 return false;
6076 /* Worker for DEF_BUILTIN.
6077 Possibly define a builtin function with one or two names.
6078 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6079 nonansi_p and flag_no_nonansi_builtin. */
6081 static void
6082 def_builtin_1 (enum built_in_function fncode,
6083 const char *name,
6084 enum built_in_class fnclass,
6085 tree fntype, tree libtype,
6086 bool both_p, bool fallback_p, bool nonansi_p,
6087 tree fnattrs, bool implicit_p)
6089 tree decl;
6090 const char *libname;
6092 if (fntype == error_mark_node)
6093 return;
6095 gcc_assert ((!both_p && !fallback_p)
6096 || !strncmp (name, "__builtin_",
6097 strlen ("__builtin_")));
6099 libname = name + strlen ("__builtin_");
6100 decl = add_builtin_function (name, fntype, fncode, fnclass,
6101 (fallback_p ? libname : NULL),
6102 fnattrs);
6104 set_builtin_decl (fncode, decl, implicit_p);
6106 if (both_p
6107 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6108 && !(nonansi_p && flag_no_nonansi_builtin))
6109 add_builtin_function (libname, libtype, fncode, fnclass,
6110 NULL, fnattrs);
6113 /* Nonzero if the type T promotes to int. This is (nearly) the
6114 integral promotions defined in ISO C99 6.3.1.1/2. */
6116 bool
6117 c_promoting_integer_type_p (const_tree t)
6119 switch (TREE_CODE (t))
6121 case INTEGER_TYPE:
6122 return (TYPE_MAIN_VARIANT (t) == char_type_node
6123 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6124 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6125 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6126 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6127 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6129 case ENUMERAL_TYPE:
6130 /* ??? Technically all enumerations not larger than an int
6131 promote to an int. But this is used along code paths
6132 that only want to notice a size change. */
6133 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6135 case BOOLEAN_TYPE:
6136 return 1;
6138 default:
6139 return 0;
6143 /* Return 1 if PARMS specifies a fixed number of parameters
6144 and none of their types is affected by default promotions. */
6147 self_promoting_args_p (const_tree parms)
6149 const_tree t;
6150 for (t = parms; t; t = TREE_CHAIN (t))
6152 tree type = TREE_VALUE (t);
6154 if (type == error_mark_node)
6155 continue;
6157 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6158 return 0;
6160 if (type == 0)
6161 return 0;
6163 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6164 return 0;
6166 if (c_promoting_integer_type_p (type))
6167 return 0;
6169 return 1;
6172 /* Recursively remove any '*' or '&' operator from TYPE. */
6173 tree
6174 strip_pointer_operator (tree t)
6176 while (POINTER_TYPE_P (t))
6177 t = TREE_TYPE (t);
6178 return t;
6181 /* Recursively remove pointer or array type from TYPE. */
6182 tree
6183 strip_pointer_or_array_types (tree t)
6185 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6186 t = TREE_TYPE (t);
6187 return t;
6190 /* Used to compare case labels. K1 and K2 are actually tree nodes
6191 representing case labels, or NULL_TREE for a `default' label.
6192 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6193 K2, and 0 if K1 and K2 are equal. */
6196 case_compare (splay_tree_key k1, splay_tree_key k2)
6198 /* Consider a NULL key (such as arises with a `default' label) to be
6199 smaller than anything else. */
6200 if (!k1)
6201 return k2 ? -1 : 0;
6202 else if (!k2)
6203 return k1 ? 1 : 0;
6205 return tree_int_cst_compare ((tree) k1, (tree) k2);
6208 /* Process a case label, located at LOC, for the range LOW_VALUE
6209 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6210 then this case label is actually a `default' label. If only
6211 HIGH_VALUE is NULL_TREE, then case label was declared using the
6212 usual C/C++ syntax, rather than the GNU case range extension.
6213 CASES is a tree containing all the case ranges processed so far;
6214 COND is the condition for the switch-statement itself. Returns the
6215 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6216 is created. */
6218 tree
6219 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6220 tree low_value, tree high_value)
6222 tree type;
6223 tree label;
6224 tree case_label;
6225 splay_tree_node node;
6227 /* Create the LABEL_DECL itself. */
6228 label = create_artificial_label (loc);
6230 /* If there was an error processing the switch condition, bail now
6231 before we get more confused. */
6232 if (!cond || cond == error_mark_node)
6233 goto error_out;
6235 if ((low_value && TREE_TYPE (low_value)
6236 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6237 || (high_value && TREE_TYPE (high_value)
6238 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6240 error_at (loc, "pointers are not permitted as case values");
6241 goto error_out;
6244 /* Case ranges are a GNU extension. */
6245 if (high_value)
6246 pedwarn (loc, OPT_Wpedantic,
6247 "range expressions in switch statements are non-standard");
6249 type = TREE_TYPE (cond);
6250 if (low_value)
6252 low_value = check_case_value (loc, low_value);
6253 low_value = convert_and_check (loc, type, low_value);
6254 if (low_value == error_mark_node)
6255 goto error_out;
6257 if (high_value)
6259 high_value = check_case_value (loc, high_value);
6260 high_value = convert_and_check (loc, type, high_value);
6261 if (high_value == error_mark_node)
6262 goto error_out;
6265 if (low_value && high_value)
6267 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6268 really a case range, even though it was written that way.
6269 Remove the HIGH_VALUE to simplify later processing. */
6270 if (tree_int_cst_equal (low_value, high_value))
6271 high_value = NULL_TREE;
6272 else if (!tree_int_cst_lt (low_value, high_value))
6273 warning_at (loc, 0, "empty range specified");
6276 /* See if the case is in range of the type of the original testing
6277 expression. If both low_value and high_value are out of range,
6278 don't insert the case label and return NULL_TREE. */
6279 if (low_value
6280 && !check_case_bounds (loc, type, orig_type,
6281 &low_value, high_value ? &high_value : NULL))
6282 return NULL_TREE;
6284 /* Look up the LOW_VALUE in the table of case labels we already
6285 have. */
6286 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6287 /* If there was not an exact match, check for overlapping ranges.
6288 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6289 that's a `default' label and the only overlap is an exact match. */
6290 if (!node && (low_value || high_value))
6292 splay_tree_node low_bound;
6293 splay_tree_node high_bound;
6295 /* Even though there wasn't an exact match, there might be an
6296 overlap between this case range and another case range.
6297 Since we've (inductively) not allowed any overlapping case
6298 ranges, we simply need to find the greatest low case label
6299 that is smaller that LOW_VALUE, and the smallest low case
6300 label that is greater than LOW_VALUE. If there is an overlap
6301 it will occur in one of these two ranges. */
6302 low_bound = splay_tree_predecessor (cases,
6303 (splay_tree_key) low_value);
6304 high_bound = splay_tree_successor (cases,
6305 (splay_tree_key) low_value);
6307 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6308 the LOW_VALUE, so there is no need to check unless the
6309 LOW_BOUND is in fact itself a case range. */
6310 if (low_bound
6311 && CASE_HIGH ((tree) low_bound->value)
6312 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6313 low_value) >= 0)
6314 node = low_bound;
6315 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6316 range is bigger than the low end of the current range, so we
6317 are only interested if the current range is a real range, and
6318 not an ordinary case label. */
6319 else if (high_bound
6320 && high_value
6321 && (tree_int_cst_compare ((tree) high_bound->key,
6322 high_value)
6323 <= 0))
6324 node = high_bound;
6326 /* If there was an overlap, issue an error. */
6327 if (node)
6329 tree duplicate = CASE_LABEL ((tree) node->value);
6331 if (high_value)
6333 error_at (loc, "duplicate (or overlapping) case value");
6334 error_at (DECL_SOURCE_LOCATION (duplicate),
6335 "this is the first entry overlapping that value");
6337 else if (low_value)
6339 error_at (loc, "duplicate case value") ;
6340 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6342 else
6344 error_at (loc, "multiple default labels in one switch");
6345 error_at (DECL_SOURCE_LOCATION (duplicate),
6346 "this is the first default label");
6348 goto error_out;
6351 /* Add a CASE_LABEL to the statement-tree. */
6352 case_label = add_stmt (build_case_label (low_value, high_value, label));
6353 /* Register this case label in the splay tree. */
6354 splay_tree_insert (cases,
6355 (splay_tree_key) low_value,
6356 (splay_tree_value) case_label);
6358 return case_label;
6360 error_out:
6361 /* Add a label so that the back-end doesn't think that the beginning of
6362 the switch is unreachable. Note that we do not add a case label, as
6363 that just leads to duplicates and thence to failure later on. */
6364 if (!cases->root)
6366 tree t = create_artificial_label (loc);
6367 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6369 return error_mark_node;
6372 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6373 Used to verify that case values match up with enumerator values. */
6375 static void
6376 match_case_to_enum_1 (tree key, tree type, tree label)
6378 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6380 if (tree_fits_uhwi_p (key))
6381 print_dec (key, buf, UNSIGNED);
6382 else if (tree_fits_shwi_p (key))
6383 print_dec (key, buf, SIGNED);
6384 else
6385 print_hex (key, buf);
6387 if (TYPE_NAME (type) == 0)
6388 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6389 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6390 "case value %qs not in enumerated type",
6391 buf);
6392 else
6393 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6394 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6395 "case value %qs not in enumerated type %qT",
6396 buf, type);
6399 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6400 Used to verify that case values match up with enumerator values. */
6402 static int
6403 match_case_to_enum (splay_tree_node node, void *data)
6405 tree label = (tree) node->value;
6406 tree type = (tree) data;
6408 /* Skip default case. */
6409 if (!CASE_LOW (label))
6410 return 0;
6412 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6413 when we did our enum->case scan. Reset our scratch bit after. */
6414 if (!CASE_LOW_SEEN (label))
6415 match_case_to_enum_1 (CASE_LOW (label), type, label);
6416 else
6417 CASE_LOW_SEEN (label) = 0;
6419 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6420 not set, that means that CASE_HIGH did not appear when we did our
6421 enum->case scan. Reset our scratch bit after. */
6422 if (CASE_HIGH (label))
6424 if (!CASE_HIGH_SEEN (label))
6425 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6426 else
6427 CASE_HIGH_SEEN (label) = 0;
6430 return 0;
6433 /* Handle -Wswitch*. Called from the front end after parsing the
6434 switch construct. */
6435 /* ??? Should probably be somewhere generic, since other languages
6436 besides C and C++ would want this. At the moment, however, C/C++
6437 are the only tree-ssa languages that support enumerations at all,
6438 so the point is moot. */
6440 void
6441 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6442 tree type, tree cond)
6444 splay_tree_node default_node;
6445 splay_tree_node node;
6446 tree chain;
6448 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6449 return;
6451 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6452 if (!default_node)
6453 warning_at (switch_location, OPT_Wswitch_default,
6454 "switch missing default case");
6456 /* From here on, we only care about about enumerated types. */
6457 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6458 return;
6460 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6461 if (!warn_switch_enum && !warn_switch)
6462 return;
6464 /* Check the cases. Warn about case values which are not members of
6465 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6466 there is no default case, check that exactly all enumeration
6467 literals are covered by the cases. */
6469 /* Clearing COND if it is not an integer constant simplifies
6470 the tests inside the loop below. */
6471 if (TREE_CODE (cond) != INTEGER_CST)
6472 cond = NULL_TREE;
6474 /* The time complexity here is O(N*lg(N)) worst case, but for the
6475 common case of monotonically increasing enumerators, it is
6476 O(N), since the nature of the splay tree will keep the next
6477 element adjacent to the root at all times. */
6479 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6481 tree value = TREE_VALUE (chain);
6482 if (TREE_CODE (value) == CONST_DECL)
6483 value = DECL_INITIAL (value);
6484 node = splay_tree_lookup (cases, (splay_tree_key) value);
6485 if (node)
6487 /* Mark the CASE_LOW part of the case entry as seen. */
6488 tree label = (tree) node->value;
6489 CASE_LOW_SEEN (label) = 1;
6490 continue;
6493 /* Even though there wasn't an exact match, there might be a
6494 case range which includes the enumerator's value. */
6495 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6496 if (node && CASE_HIGH ((tree) node->value))
6498 tree label = (tree) node->value;
6499 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6500 if (cmp >= 0)
6502 /* If we match the upper bound exactly, mark the CASE_HIGH
6503 part of the case entry as seen. */
6504 if (cmp == 0)
6505 CASE_HIGH_SEEN (label) = 1;
6506 continue;
6510 /* We've now determined that this enumerated literal isn't
6511 handled by the case labels of the switch statement. */
6513 /* If the switch expression is a constant, we only really care
6514 about whether that constant is handled by the switch. */
6515 if (cond && tree_int_cst_compare (cond, value))
6516 continue;
6518 /* If there is a default_node, the only relevant option is
6519 Wswitch-enum. Otherwise, if both are enabled then we prefer
6520 to warn using -Wswitch because -Wswitch is enabled by -Wall
6521 while -Wswitch-enum is explicit. */
6522 warning_at (switch_location,
6523 (default_node || !warn_switch
6524 ? OPT_Wswitch_enum
6525 : OPT_Wswitch),
6526 "enumeration value %qE not handled in switch",
6527 TREE_PURPOSE (chain));
6530 /* Warn if there are case expressions that don't correspond to
6531 enumerators. This can occur since C and C++ don't enforce
6532 type-checking of assignments to enumeration variables.
6534 The time complexity here is now always O(N) worst case, since
6535 we should have marked both the lower bound and upper bound of
6536 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6537 above. This scan also resets those fields. */
6539 splay_tree_foreach (cases, match_case_to_enum, type);
6542 /* Finish an expression taking the address of LABEL (an
6543 IDENTIFIER_NODE). Returns an expression for the address.
6545 LOC is the location for the expression returned. */
6547 tree
6548 finish_label_address_expr (tree label, location_t loc)
6550 tree result;
6552 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6554 if (label == error_mark_node)
6555 return error_mark_node;
6557 label = lookup_label (label);
6558 if (label == NULL_TREE)
6559 result = null_pointer_node;
6560 else
6562 TREE_USED (label) = 1;
6563 result = build1 (ADDR_EXPR, ptr_type_node, label);
6564 /* The current function is not necessarily uninlinable.
6565 Computed gotos are incompatible with inlining, but the value
6566 here could be used only in a diagnostic, for example. */
6567 protected_set_expr_location (result, loc);
6570 return result;
6574 /* Given a boolean expression ARG, return a tree representing an increment
6575 or decrement (as indicated by CODE) of ARG. The front end must check for
6576 invalid cases (e.g., decrement in C++). */
6577 tree
6578 boolean_increment (enum tree_code code, tree arg)
6580 tree val;
6581 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6583 arg = stabilize_reference (arg);
6584 switch (code)
6586 case PREINCREMENT_EXPR:
6587 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6588 break;
6589 case POSTINCREMENT_EXPR:
6590 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6591 arg = save_expr (arg);
6592 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6593 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6594 break;
6595 case PREDECREMENT_EXPR:
6596 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6597 invert_truthvalue_loc (input_location, arg));
6598 break;
6599 case POSTDECREMENT_EXPR:
6600 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6601 invert_truthvalue_loc (input_location, arg));
6602 arg = save_expr (arg);
6603 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6604 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6605 break;
6606 default:
6607 gcc_unreachable ();
6609 TREE_SIDE_EFFECTS (val) = 1;
6610 return val;
6613 /* Built-in macros for stddef.h and stdint.h, that require macros
6614 defined in this file. */
6615 void
6616 c_stddef_cpp_builtins(void)
6618 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6619 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6620 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6621 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6622 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6623 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6624 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6625 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6626 if (SIG_ATOMIC_TYPE)
6627 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6628 if (INT8_TYPE)
6629 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6630 if (INT16_TYPE)
6631 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6632 if (INT32_TYPE)
6633 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6634 if (INT64_TYPE)
6635 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6636 if (UINT8_TYPE)
6637 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6638 if (UINT16_TYPE)
6639 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6640 if (UINT32_TYPE)
6641 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6642 if (UINT64_TYPE)
6643 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6644 if (INT_LEAST8_TYPE)
6645 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6646 if (INT_LEAST16_TYPE)
6647 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6648 if (INT_LEAST32_TYPE)
6649 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6650 if (INT_LEAST64_TYPE)
6651 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6652 if (UINT_LEAST8_TYPE)
6653 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6654 if (UINT_LEAST16_TYPE)
6655 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6656 if (UINT_LEAST32_TYPE)
6657 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6658 if (UINT_LEAST64_TYPE)
6659 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6660 if (INT_FAST8_TYPE)
6661 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6662 if (INT_FAST16_TYPE)
6663 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6664 if (INT_FAST32_TYPE)
6665 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6666 if (INT_FAST64_TYPE)
6667 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6668 if (UINT_FAST8_TYPE)
6669 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6670 if (UINT_FAST16_TYPE)
6671 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6672 if (UINT_FAST32_TYPE)
6673 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6674 if (UINT_FAST64_TYPE)
6675 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6676 if (INTPTR_TYPE)
6677 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6678 if (UINTPTR_TYPE)
6679 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6682 static void
6683 c_init_attributes (void)
6685 /* Fill in the built_in_attributes array. */
6686 #define DEF_ATTR_NULL_TREE(ENUM) \
6687 built_in_attributes[(int) ENUM] = NULL_TREE;
6688 #define DEF_ATTR_INT(ENUM, VALUE) \
6689 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6690 #define DEF_ATTR_STRING(ENUM, VALUE) \
6691 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6692 #define DEF_ATTR_IDENT(ENUM, STRING) \
6693 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6694 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6695 built_in_attributes[(int) ENUM] \
6696 = tree_cons (built_in_attributes[(int) PURPOSE], \
6697 built_in_attributes[(int) VALUE], \
6698 built_in_attributes[(int) CHAIN]);
6699 #include "builtin-attrs.def"
6700 #undef DEF_ATTR_NULL_TREE
6701 #undef DEF_ATTR_INT
6702 #undef DEF_ATTR_IDENT
6703 #undef DEF_ATTR_TREE_LIST
6706 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6707 identifier as an argument, so the front end shouldn't look it up. */
6709 bool
6710 attribute_takes_identifier_p (const_tree attr_id)
6712 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6713 if (spec == NULL)
6714 /* Unknown attribute that we'll end up ignoring, return true so we
6715 don't complain about an identifier argument. */
6716 return true;
6717 else if (!strcmp ("mode", spec->name)
6718 || !strcmp ("format", spec->name)
6719 || !strcmp ("cleanup", spec->name))
6720 return true;
6721 else
6722 return targetm.attribute_takes_identifier_p (attr_id);
6725 /* Attribute handlers common to C front ends. */
6727 /* Handle a "packed" attribute; arguments as in
6728 struct attribute_spec.handler. */
6730 static tree
6731 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6732 int flags, bool *no_add_attrs)
6734 if (TYPE_P (*node))
6736 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6737 *node = build_variant_type_copy (*node);
6738 TYPE_PACKED (*node) = 1;
6740 else if (TREE_CODE (*node) == FIELD_DECL)
6742 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6743 /* Still pack bitfields. */
6744 && ! DECL_INITIAL (*node))
6745 warning (OPT_Wattributes,
6746 "%qE attribute ignored for field of type %qT",
6747 name, TREE_TYPE (*node));
6748 else
6749 DECL_PACKED (*node) = 1;
6751 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6752 used for DECL_REGISTER. It wouldn't mean anything anyway.
6753 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6754 that changes what the typedef is typing. */
6755 else
6757 warning (OPT_Wattributes, "%qE attribute ignored", name);
6758 *no_add_attrs = true;
6761 return NULL_TREE;
6764 /* Handle a "nocommon" attribute; arguments as in
6765 struct attribute_spec.handler. */
6767 static tree
6768 handle_nocommon_attribute (tree *node, tree name,
6769 tree ARG_UNUSED (args),
6770 int ARG_UNUSED (flags), bool *no_add_attrs)
6772 if (TREE_CODE (*node) == VAR_DECL)
6773 DECL_COMMON (*node) = 0;
6774 else
6776 warning (OPT_Wattributes, "%qE attribute ignored", name);
6777 *no_add_attrs = true;
6780 return NULL_TREE;
6783 /* Handle a "common" attribute; arguments as in
6784 struct attribute_spec.handler. */
6786 static tree
6787 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6788 int ARG_UNUSED (flags), bool *no_add_attrs)
6790 if (TREE_CODE (*node) == VAR_DECL)
6791 DECL_COMMON (*node) = 1;
6792 else
6794 warning (OPT_Wattributes, "%qE attribute ignored", name);
6795 *no_add_attrs = true;
6798 return NULL_TREE;
6801 /* Handle a "noreturn" attribute; arguments as in
6802 struct attribute_spec.handler. */
6804 static tree
6805 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6806 int ARG_UNUSED (flags), bool *no_add_attrs)
6808 tree type = TREE_TYPE (*node);
6810 /* See FIXME comment in c_common_attribute_table. */
6811 if (TREE_CODE (*node) == FUNCTION_DECL
6812 || objc_method_decl (TREE_CODE (*node)))
6813 TREE_THIS_VOLATILE (*node) = 1;
6814 else if (TREE_CODE (type) == POINTER_TYPE
6815 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6816 TREE_TYPE (*node)
6817 = (build_qualified_type
6818 (build_pointer_type
6819 (build_type_variant (TREE_TYPE (type),
6820 TYPE_READONLY (TREE_TYPE (type)), 1)),
6821 TYPE_QUALS (type)));
6822 else
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6828 return NULL_TREE;
6831 /* Handle a "hot" and attribute; arguments as in
6832 struct attribute_spec.handler. */
6834 static tree
6835 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6836 int ARG_UNUSED (flags), bool *no_add_attrs)
6838 if (TREE_CODE (*node) == FUNCTION_DECL
6839 || TREE_CODE (*node) == LABEL_DECL)
6841 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6843 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6844 "with attribute %qs", name, "cold");
6845 *no_add_attrs = true;
6847 /* Most of the rest of the hot processing is done later with
6848 lookup_attribute. */
6850 else
6852 warning (OPT_Wattributes, "%qE attribute ignored", name);
6853 *no_add_attrs = true;
6856 return NULL_TREE;
6859 /* Handle a "cold" and attribute; arguments as in
6860 struct attribute_spec.handler. */
6862 static tree
6863 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6864 int ARG_UNUSED (flags), bool *no_add_attrs)
6866 if (TREE_CODE (*node) == FUNCTION_DECL
6867 || TREE_CODE (*node) == LABEL_DECL)
6869 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6871 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6872 "with attribute %qs", name, "hot");
6873 *no_add_attrs = true;
6875 /* Most of the rest of the cold processing is done later with
6876 lookup_attribute. */
6878 else
6880 warning (OPT_Wattributes, "%qE attribute ignored", name);
6881 *no_add_attrs = true;
6884 return NULL_TREE;
6887 /* Handle a "no_sanitize_address" attribute; arguments as in
6888 struct attribute_spec.handler. */
6890 static tree
6891 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6892 bool *no_add_attrs)
6894 if (TREE_CODE (*node) != FUNCTION_DECL)
6896 warning (OPT_Wattributes, "%qE attribute ignored", name);
6897 *no_add_attrs = true;
6900 return NULL_TREE;
6903 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6904 struct attribute_spec.handler. */
6906 static tree
6907 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6908 bool *no_add_attrs)
6910 if (TREE_CODE (*node) != FUNCTION_DECL)
6911 warning (OPT_Wattributes, "%qE attribute ignored", name);
6912 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6913 DECL_ATTRIBUTES (*node)
6914 = tree_cons (get_identifier ("no_sanitize_address"),
6915 NULL_TREE, DECL_ATTRIBUTES (*node));
6916 *no_add_attrs = true;
6917 return NULL_TREE;
6920 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6921 struct attribute_spec.handler. */
6923 static tree
6924 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6925 bool *no_add_attrs)
6927 if (TREE_CODE (*node) != FUNCTION_DECL)
6929 warning (OPT_Wattributes, "%qE attribute ignored", name);
6930 *no_add_attrs = true;
6933 return NULL_TREE;
6936 /* Handle a "stack_protect" attribute; arguments as in
6937 struct attribute_spec.handler. */
6938 static tree
6939 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6940 bool *no_add_attrs)
6942 if (TREE_CODE (*node) != FUNCTION_DECL)
6944 warning (OPT_Wattributes, "%qE attribute ignored", name);
6945 *no_add_attrs = true;
6947 else
6948 DECL_ATTRIBUTES (*node)
6949 = tree_cons (get_identifier ("stack_protect"),
6950 NULL_TREE, DECL_ATTRIBUTES (*node));
6952 return NULL_TREE;
6955 /* Handle a "noinline" attribute; arguments as in
6956 struct attribute_spec.handler. */
6958 static tree
6959 handle_noinline_attribute (tree *node, tree name,
6960 tree ARG_UNUSED (args),
6961 int ARG_UNUSED (flags), bool *no_add_attrs)
6963 if (TREE_CODE (*node) == FUNCTION_DECL)
6965 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6967 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6968 "with attribute %qs", name, "always_inline");
6969 *no_add_attrs = true;
6971 else
6972 DECL_UNINLINABLE (*node) = 1;
6974 else
6976 warning (OPT_Wattributes, "%qE attribute ignored", name);
6977 *no_add_attrs = true;
6980 return NULL_TREE;
6983 /* Handle a "noclone" attribute; arguments as in
6984 struct attribute_spec.handler. */
6986 static tree
6987 handle_noclone_attribute (tree *node, tree name,
6988 tree ARG_UNUSED (args),
6989 int ARG_UNUSED (flags), bool *no_add_attrs)
6991 if (TREE_CODE (*node) != FUNCTION_DECL)
6993 warning (OPT_Wattributes, "%qE attribute ignored", name);
6994 *no_add_attrs = true;
6997 return NULL_TREE;
7000 /* Handle a "no_icf" attribute; arguments as in
7001 struct attribute_spec.handler. */
7003 static tree
7004 handle_noicf_attribute (tree *node, tree name,
7005 tree ARG_UNUSED (args),
7006 int ARG_UNUSED (flags), bool *no_add_attrs)
7008 if (TREE_CODE (*node) != FUNCTION_DECL)
7010 warning (OPT_Wattributes, "%qE attribute ignored", name);
7011 *no_add_attrs = true;
7014 return NULL_TREE;
7018 /* Handle a "always_inline" attribute; arguments as in
7019 struct attribute_spec.handler. */
7021 static tree
7022 handle_always_inline_attribute (tree *node, tree name,
7023 tree ARG_UNUSED (args),
7024 int ARG_UNUSED (flags),
7025 bool *no_add_attrs)
7027 if (TREE_CODE (*node) == FUNCTION_DECL)
7029 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7031 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7032 "with %qs attribute", name, "noinline");
7033 *no_add_attrs = true;
7035 else
7036 /* Set the attribute and mark it for disregarding inline
7037 limits. */
7038 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7040 else
7042 warning (OPT_Wattributes, "%qE attribute ignored", name);
7043 *no_add_attrs = true;
7046 return NULL_TREE;
7049 /* Handle a "gnu_inline" attribute; arguments as in
7050 struct attribute_spec.handler. */
7052 static tree
7053 handle_gnu_inline_attribute (tree *node, tree name,
7054 tree ARG_UNUSED (args),
7055 int ARG_UNUSED (flags),
7056 bool *no_add_attrs)
7058 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7060 /* Do nothing else, just set the attribute. We'll get at
7061 it later with lookup_attribute. */
7063 else
7065 warning (OPT_Wattributes, "%qE attribute ignored", name);
7066 *no_add_attrs = true;
7069 return NULL_TREE;
7072 /* Handle a "leaf" attribute; arguments as in
7073 struct attribute_spec.handler. */
7075 static tree
7076 handle_leaf_attribute (tree *node, tree name,
7077 tree ARG_UNUSED (args),
7078 int ARG_UNUSED (flags), bool *no_add_attrs)
7080 if (TREE_CODE (*node) != FUNCTION_DECL)
7082 warning (OPT_Wattributes, "%qE attribute ignored", name);
7083 *no_add_attrs = true;
7085 if (!TREE_PUBLIC (*node))
7087 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7088 *no_add_attrs = true;
7091 return NULL_TREE;
7094 /* Handle an "artificial" attribute; arguments as in
7095 struct attribute_spec.handler. */
7097 static tree
7098 handle_artificial_attribute (tree *node, tree name,
7099 tree ARG_UNUSED (args),
7100 int ARG_UNUSED (flags),
7101 bool *no_add_attrs)
7103 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7105 /* Do nothing else, just set the attribute. We'll get at
7106 it later with lookup_attribute. */
7108 else
7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
7111 *no_add_attrs = true;
7114 return NULL_TREE;
7117 /* Handle a "flatten" attribute; arguments as in
7118 struct attribute_spec.handler. */
7120 static tree
7121 handle_flatten_attribute (tree *node, tree name,
7122 tree args ATTRIBUTE_UNUSED,
7123 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7125 if (TREE_CODE (*node) == FUNCTION_DECL)
7126 /* Do nothing else, just set the attribute. We'll get at
7127 it later with lookup_attribute. */
7129 else
7131 warning (OPT_Wattributes, "%qE attribute ignored", name);
7132 *no_add_attrs = true;
7135 return NULL_TREE;
7138 /* Handle a "warning" or "error" attribute; arguments as in
7139 struct attribute_spec.handler. */
7141 static tree
7142 handle_error_attribute (tree *node, tree name, tree args,
7143 int ARG_UNUSED (flags), bool *no_add_attrs)
7145 if (TREE_CODE (*node) == FUNCTION_DECL
7146 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7147 /* Do nothing else, just set the attribute. We'll get at
7148 it later with lookup_attribute. */
7150 else
7152 warning (OPT_Wattributes, "%qE attribute ignored", name);
7153 *no_add_attrs = true;
7156 return NULL_TREE;
7159 /* Handle a "used" attribute; arguments as in
7160 struct attribute_spec.handler. */
7162 static tree
7163 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7164 int ARG_UNUSED (flags), bool *no_add_attrs)
7166 tree node = *pnode;
7168 if (TREE_CODE (node) == FUNCTION_DECL
7169 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7170 || (TREE_CODE (node) == TYPE_DECL))
7172 TREE_USED (node) = 1;
7173 DECL_PRESERVE_P (node) = 1;
7174 if (TREE_CODE (node) == VAR_DECL)
7175 DECL_READ_P (node) = 1;
7177 else
7179 warning (OPT_Wattributes, "%qE attribute ignored", name);
7180 *no_add_attrs = true;
7183 return NULL_TREE;
7186 /* Handle a "unused" attribute; arguments as in
7187 struct attribute_spec.handler. */
7189 static tree
7190 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7191 int flags, bool *no_add_attrs)
7193 if (DECL_P (*node))
7195 tree decl = *node;
7197 if (TREE_CODE (decl) == PARM_DECL
7198 || TREE_CODE (decl) == VAR_DECL
7199 || TREE_CODE (decl) == FUNCTION_DECL
7200 || TREE_CODE (decl) == LABEL_DECL
7201 || TREE_CODE (decl) == TYPE_DECL)
7203 TREE_USED (decl) = 1;
7204 if (TREE_CODE (decl) == VAR_DECL
7205 || TREE_CODE (decl) == PARM_DECL)
7206 DECL_READ_P (decl) = 1;
7208 else
7210 warning (OPT_Wattributes, "%qE attribute ignored", name);
7211 *no_add_attrs = true;
7214 else
7216 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7217 *node = build_variant_type_copy (*node);
7218 TREE_USED (*node) = 1;
7221 return NULL_TREE;
7224 /* Handle a "externally_visible" attribute; arguments as in
7225 struct attribute_spec.handler. */
7227 static tree
7228 handle_externally_visible_attribute (tree *pnode, tree name,
7229 tree ARG_UNUSED (args),
7230 int ARG_UNUSED (flags),
7231 bool *no_add_attrs)
7233 tree node = *pnode;
7235 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7237 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7238 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7240 warning (OPT_Wattributes,
7241 "%qE attribute have effect only on public objects", name);
7242 *no_add_attrs = true;
7245 else
7247 warning (OPT_Wattributes, "%qE attribute ignored", name);
7248 *no_add_attrs = true;
7251 return NULL_TREE;
7254 /* Handle the "no_reorder" attribute. Arguments as in
7255 struct attribute_spec.handler. */
7257 static tree
7258 handle_no_reorder_attribute (tree *pnode,
7259 tree name,
7260 tree,
7261 int,
7262 bool *no_add_attrs)
7264 tree node = *pnode;
7266 if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
7267 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7269 warning (OPT_Wattributes,
7270 "%qE attribute only affects top level objects",
7271 name);
7272 *no_add_attrs = true;
7275 return NULL_TREE;
7278 /* Handle a "const" attribute; arguments as in
7279 struct attribute_spec.handler. */
7281 static tree
7282 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7283 int ARG_UNUSED (flags), bool *no_add_attrs)
7285 tree type = TREE_TYPE (*node);
7287 /* See FIXME comment on noreturn in c_common_attribute_table. */
7288 if (TREE_CODE (*node) == FUNCTION_DECL)
7289 TREE_READONLY (*node) = 1;
7290 else if (TREE_CODE (type) == POINTER_TYPE
7291 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7292 TREE_TYPE (*node)
7293 = (build_qualified_type
7294 (build_pointer_type
7295 (build_type_variant (TREE_TYPE (type), 1,
7296 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7297 TYPE_QUALS (type)));
7298 else
7300 warning (OPT_Wattributes, "%qE attribute ignored", name);
7301 *no_add_attrs = true;
7304 return NULL_TREE;
7307 /* Handle a "transparent_union" attribute; arguments as in
7308 struct attribute_spec.handler. */
7310 static tree
7311 handle_transparent_union_attribute (tree *node, tree name,
7312 tree ARG_UNUSED (args), int flags,
7313 bool *no_add_attrs)
7315 tree type;
7317 *no_add_attrs = true;
7320 if (TREE_CODE (*node) == TYPE_DECL
7321 && ! (flags & ATTR_FLAG_CXX11))
7322 node = &TREE_TYPE (*node);
7323 type = *node;
7325 if (TREE_CODE (type) == UNION_TYPE)
7327 /* Make sure that the first field will work for a transparent union.
7328 If the type isn't complete yet, leave the check to the code in
7329 finish_struct. */
7330 if (TYPE_SIZE (type))
7332 tree first = first_field (type);
7333 if (first == NULL_TREE
7334 || DECL_ARTIFICIAL (first)
7335 || TYPE_MODE (type) != DECL_MODE (first))
7336 goto ignored;
7339 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7341 /* If the type isn't complete yet, setting the flag
7342 on a variant wouldn't ever be checked. */
7343 if (!TYPE_SIZE (type))
7344 goto ignored;
7346 /* build_duplicate_type doesn't work for C++. */
7347 if (c_dialect_cxx ())
7348 goto ignored;
7350 /* A type variant isn't good enough, since we don't a cast
7351 to such a type removed as a no-op. */
7352 *node = type = build_duplicate_type (type);
7355 TYPE_TRANSPARENT_AGGR (type) = 1;
7356 return NULL_TREE;
7359 ignored:
7360 warning (OPT_Wattributes, "%qE attribute ignored", name);
7361 return NULL_TREE;
7364 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7365 get the requested priority for a constructor or destructor,
7366 possibly issuing diagnostics for invalid or reserved
7367 priorities. */
7369 static priority_type
7370 get_priority (tree args, bool is_destructor)
7372 HOST_WIDE_INT pri;
7373 tree arg;
7375 if (!args)
7376 return DEFAULT_INIT_PRIORITY;
7378 if (!SUPPORTS_INIT_PRIORITY)
7380 if (is_destructor)
7381 error ("destructor priorities are not supported");
7382 else
7383 error ("constructor priorities are not supported");
7384 return DEFAULT_INIT_PRIORITY;
7387 arg = TREE_VALUE (args);
7388 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7389 goto invalid;
7390 if (arg == error_mark_node)
7391 return DEFAULT_INIT_PRIORITY;
7392 arg = default_conversion (arg);
7393 if (!tree_fits_shwi_p (arg)
7394 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7395 goto invalid;
7397 pri = tree_to_shwi (arg);
7398 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7399 goto invalid;
7401 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7403 if (is_destructor)
7404 warning (0,
7405 "destructor priorities from 0 to %d are reserved "
7406 "for the implementation",
7407 MAX_RESERVED_INIT_PRIORITY);
7408 else
7409 warning (0,
7410 "constructor priorities from 0 to %d are reserved "
7411 "for the implementation",
7412 MAX_RESERVED_INIT_PRIORITY);
7414 return pri;
7416 invalid:
7417 if (is_destructor)
7418 error ("destructor priorities must be integers from 0 to %d inclusive",
7419 MAX_INIT_PRIORITY);
7420 else
7421 error ("constructor priorities must be integers from 0 to %d inclusive",
7422 MAX_INIT_PRIORITY);
7423 return DEFAULT_INIT_PRIORITY;
7426 /* Handle a "constructor" attribute; arguments as in
7427 struct attribute_spec.handler. */
7429 static tree
7430 handle_constructor_attribute (tree *node, tree name, tree args,
7431 int ARG_UNUSED (flags),
7432 bool *no_add_attrs)
7434 tree decl = *node;
7435 tree type = TREE_TYPE (decl);
7437 if (TREE_CODE (decl) == FUNCTION_DECL
7438 && TREE_CODE (type) == FUNCTION_TYPE
7439 && decl_function_context (decl) == 0)
7441 priority_type priority;
7442 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7443 priority = get_priority (args, /*is_destructor=*/false);
7444 SET_DECL_INIT_PRIORITY (decl, priority);
7445 TREE_USED (decl) = 1;
7447 else
7449 warning (OPT_Wattributes, "%qE attribute ignored", name);
7450 *no_add_attrs = true;
7453 return NULL_TREE;
7456 /* Handle a "destructor" attribute; arguments as in
7457 struct attribute_spec.handler. */
7459 static tree
7460 handle_destructor_attribute (tree *node, tree name, tree args,
7461 int ARG_UNUSED (flags),
7462 bool *no_add_attrs)
7464 tree decl = *node;
7465 tree type = TREE_TYPE (decl);
7467 if (TREE_CODE (decl) == FUNCTION_DECL
7468 && TREE_CODE (type) == FUNCTION_TYPE
7469 && decl_function_context (decl) == 0)
7471 priority_type priority;
7472 DECL_STATIC_DESTRUCTOR (decl) = 1;
7473 priority = get_priority (args, /*is_destructor=*/true);
7474 SET_DECL_FINI_PRIORITY (decl, priority);
7475 TREE_USED (decl) = 1;
7477 else
7479 warning (OPT_Wattributes, "%qE attribute ignored", name);
7480 *no_add_attrs = true;
7483 return NULL_TREE;
7486 /* Nonzero if the mode is a valid vector mode for this architecture.
7487 This returns nonzero even if there is no hardware support for the
7488 vector mode, but we can emulate with narrower modes. */
7490 static int
7491 vector_mode_valid_p (machine_mode mode)
7493 enum mode_class mclass = GET_MODE_CLASS (mode);
7494 machine_mode innermode;
7496 /* Doh! What's going on? */
7497 if (mclass != MODE_VECTOR_INT
7498 && mclass != MODE_VECTOR_FLOAT
7499 && mclass != MODE_VECTOR_FRACT
7500 && mclass != MODE_VECTOR_UFRACT
7501 && mclass != MODE_VECTOR_ACCUM
7502 && mclass != MODE_VECTOR_UACCUM)
7503 return 0;
7505 /* Hardware support. Woo hoo! */
7506 if (targetm.vector_mode_supported_p (mode))
7507 return 1;
7509 innermode = GET_MODE_INNER (mode);
7511 /* We should probably return 1 if requesting V4DI and we have no DI,
7512 but we have V2DI, but this is probably very unlikely. */
7514 /* If we have support for the inner mode, we can safely emulate it.
7515 We may not have V2DI, but me can emulate with a pair of DIs. */
7516 return targetm.scalar_mode_supported_p (innermode);
7520 /* Handle a "mode" attribute; arguments as in
7521 struct attribute_spec.handler. */
7523 static tree
7524 handle_mode_attribute (tree *node, tree name, tree args,
7525 int ARG_UNUSED (flags), bool *no_add_attrs)
7527 tree type = *node;
7528 tree ident = TREE_VALUE (args);
7530 *no_add_attrs = true;
7532 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7533 warning (OPT_Wattributes, "%qE attribute ignored", name);
7534 else
7536 int j;
7537 const char *p = IDENTIFIER_POINTER (ident);
7538 int len = strlen (p);
7539 machine_mode mode = VOIDmode;
7540 tree typefm;
7541 bool valid_mode;
7543 if (len > 4 && p[0] == '_' && p[1] == '_'
7544 && p[len - 1] == '_' && p[len - 2] == '_')
7546 char *newp = (char *) alloca (len - 1);
7548 strcpy (newp, &p[2]);
7549 newp[len - 4] = '\0';
7550 p = newp;
7553 /* Change this type to have a type with the specified mode.
7554 First check for the special modes. */
7555 if (!strcmp (p, "byte"))
7556 mode = byte_mode;
7557 else if (!strcmp (p, "word"))
7558 mode = word_mode;
7559 else if (!strcmp (p, "pointer"))
7560 mode = ptr_mode;
7561 else if (!strcmp (p, "libgcc_cmp_return"))
7562 mode = targetm.libgcc_cmp_return_mode ();
7563 else if (!strcmp (p, "libgcc_shift_count"))
7564 mode = targetm.libgcc_shift_count_mode ();
7565 else if (!strcmp (p, "unwind_word"))
7566 mode = targetm.unwind_word_mode ();
7567 else
7568 for (j = 0; j < NUM_MACHINE_MODES; j++)
7569 if (!strcmp (p, GET_MODE_NAME (j)))
7571 mode = (machine_mode) j;
7572 break;
7575 if (mode == VOIDmode)
7577 error ("unknown machine mode %qE", ident);
7578 return NULL_TREE;
7581 valid_mode = false;
7582 switch (GET_MODE_CLASS (mode))
7584 case MODE_INT:
7585 case MODE_PARTIAL_INT:
7586 case MODE_FLOAT:
7587 case MODE_DECIMAL_FLOAT:
7588 case MODE_FRACT:
7589 case MODE_UFRACT:
7590 case MODE_ACCUM:
7591 case MODE_UACCUM:
7592 valid_mode = targetm.scalar_mode_supported_p (mode);
7593 break;
7595 case MODE_COMPLEX_INT:
7596 case MODE_COMPLEX_FLOAT:
7597 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7598 break;
7600 case MODE_VECTOR_INT:
7601 case MODE_VECTOR_FLOAT:
7602 case MODE_VECTOR_FRACT:
7603 case MODE_VECTOR_UFRACT:
7604 case MODE_VECTOR_ACCUM:
7605 case MODE_VECTOR_UACCUM:
7606 warning (OPT_Wattributes, "specifying vector types with "
7607 "__attribute__ ((mode)) is deprecated");
7608 warning (OPT_Wattributes,
7609 "use __attribute__ ((vector_size)) instead");
7610 valid_mode = vector_mode_valid_p (mode);
7611 break;
7613 default:
7614 break;
7616 if (!valid_mode)
7618 error ("unable to emulate %qs", p);
7619 return NULL_TREE;
7622 if (POINTER_TYPE_P (type))
7624 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7625 tree (*fn)(tree, machine_mode, bool);
7627 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7629 error ("invalid pointer mode %qs", p);
7630 return NULL_TREE;
7633 if (TREE_CODE (type) == POINTER_TYPE)
7634 fn = build_pointer_type_for_mode;
7635 else
7636 fn = build_reference_type_for_mode;
7637 typefm = fn (TREE_TYPE (type), mode, false);
7639 else
7641 /* For fixed-point modes, we need to test if the signness of type
7642 and the machine mode are consistent. */
7643 if (ALL_FIXED_POINT_MODE_P (mode)
7644 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7646 error ("signedness of type and machine mode %qs don%'t match", p);
7647 return NULL_TREE;
7649 /* For fixed-point modes, we need to pass saturating info. */
7650 typefm = lang_hooks.types.type_for_mode (mode,
7651 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7652 : TYPE_UNSIGNED (type));
7655 if (typefm == NULL_TREE)
7657 error ("no data type for mode %qs", p);
7658 return NULL_TREE;
7660 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7662 /* For enumeral types, copy the precision from the integer
7663 type returned above. If not an INTEGER_TYPE, we can't use
7664 this mode for this type. */
7665 if (TREE_CODE (typefm) != INTEGER_TYPE)
7667 error ("cannot use mode %qs for enumeral types", p);
7668 return NULL_TREE;
7671 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7673 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7674 typefm = type;
7676 else
7678 /* We cannot build a type variant, as there's code that assumes
7679 that TYPE_MAIN_VARIANT has the same mode. This includes the
7680 debug generators. Instead, create a subrange type. This
7681 results in all of the enumeral values being emitted only once
7682 in the original, and the subtype gets them by reference. */
7683 if (TYPE_UNSIGNED (type))
7684 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7685 else
7686 typefm = make_signed_type (TYPE_PRECISION (typefm));
7687 TREE_TYPE (typefm) = type;
7690 else if (VECTOR_MODE_P (mode)
7691 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7692 : TREE_CODE (type) != TREE_CODE (typefm))
7694 error ("mode %qs applied to inappropriate type", p);
7695 return NULL_TREE;
7698 *node = typefm;
7701 return NULL_TREE;
7704 /* Handle a "section" attribute; arguments as in
7705 struct attribute_spec.handler. */
7707 static tree
7708 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7709 int ARG_UNUSED (flags), bool *no_add_attrs)
7711 tree decl = *node;
7713 if (targetm_common.have_named_sections)
7715 user_defined_section_attribute = true;
7717 if ((TREE_CODE (decl) == FUNCTION_DECL
7718 || TREE_CODE (decl) == VAR_DECL)
7719 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7721 if (TREE_CODE (decl) == VAR_DECL
7722 && current_function_decl != NULL_TREE
7723 && !TREE_STATIC (decl))
7725 error_at (DECL_SOURCE_LOCATION (decl),
7726 "section attribute cannot be specified for "
7727 "local variables");
7728 *no_add_attrs = true;
7731 /* The decl may have already been given a section attribute
7732 from a previous declaration. Ensure they match. */
7733 else if (DECL_SECTION_NAME (decl) != NULL
7734 && strcmp (DECL_SECTION_NAME (decl),
7735 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7737 error ("section of %q+D conflicts with previous declaration",
7738 *node);
7739 *no_add_attrs = true;
7741 else if (TREE_CODE (decl) == VAR_DECL
7742 && !targetm.have_tls && targetm.emutls.tmpl_section
7743 && DECL_THREAD_LOCAL_P (decl))
7745 error ("section of %q+D cannot be overridden", *node);
7746 *no_add_attrs = true;
7748 else
7749 set_decl_section_name (decl,
7750 TREE_STRING_POINTER (TREE_VALUE (args)));
7752 else
7754 error ("section attribute not allowed for %q+D", *node);
7755 *no_add_attrs = true;
7758 else
7760 error_at (DECL_SOURCE_LOCATION (*node),
7761 "section attributes are not supported for this target");
7762 *no_add_attrs = true;
7765 return NULL_TREE;
7768 /* Check whether ALIGN is a valid user-specified alignment. If so,
7769 return its base-2 log; if not, output an error and return -1. If
7770 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7771 no error. */
7773 check_user_alignment (const_tree align, bool allow_zero)
7775 int i;
7777 if (error_operand_p (align))
7778 return -1;
7779 if (TREE_CODE (align) != INTEGER_CST
7780 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7782 error ("requested alignment is not an integer constant");
7783 return -1;
7785 else if (allow_zero && integer_zerop (align))
7786 return -1;
7787 else if (tree_int_cst_sgn (align) == -1
7788 || (i = tree_log2 (align)) == -1)
7790 error ("requested alignment is not a positive power of 2");
7791 return -1;
7793 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7795 error ("requested alignment is too large");
7796 return -1;
7798 return i;
7802 If in c++-11, check if the c++-11 alignment constraint with respect
7803 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7804 c++-11 mode, does nothing.
7806 [dcl.align]2/ says:
7808 [* if the constant expression evaluates to a fundamental alignment,
7809 the alignment requirement of the declared entity shall be the
7810 specified fundamental alignment.
7812 * if the constant expression evaluates to an extended alignment
7813 and the implementation supports that alignment in the context
7814 of the declaration, the alignment of the declared entity shall
7815 be that alignment
7817 * if the constant expression evaluates to an extended alignment
7818 and the implementation does not support that alignment in the
7819 context of the declaration, the program is ill-formed]. */
7821 static bool
7822 check_cxx_fundamental_alignment_constraints (tree node,
7823 unsigned align_log,
7824 int flags)
7826 bool alignment_too_large_p = false;
7827 unsigned requested_alignment = 1U << align_log;
7828 unsigned max_align = 0;
7830 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7831 || (node == NULL_TREE || node == error_mark_node))
7832 return true;
7834 if (cxx_fundamental_alignment_p (requested_alignment))
7835 return true;
7837 if (DECL_P (node))
7839 if (TREE_STATIC (node))
7841 /* For file scope variables and static members, the target
7842 supports alignments that are at most
7843 MAX_OFILE_ALIGNMENT. */
7844 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7845 alignment_too_large_p = true;
7847 else
7849 #ifdef BIGGEST_FIELD_ALIGNMENT
7850 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7851 #else
7852 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7853 #endif
7854 /* For non-static members, the target supports either
7855 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7856 if it is defined or BIGGEST_ALIGNMENT. */
7857 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7858 if (TREE_CODE (node) == FIELD_DECL
7859 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7860 alignment_too_large_p = true;
7861 #undef MAX_TARGET_FIELD_ALIGNMENT
7862 /* For stack variables, the target supports at most
7863 MAX_STACK_ALIGNMENT. */
7864 else if (decl_function_context (node) != NULL
7865 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7866 alignment_too_large_p = true;
7869 else if (TYPE_P (node))
7871 /* Let's be liberal for types. */
7872 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7873 alignment_too_large_p = true;
7876 if (alignment_too_large_p)
7877 pedwarn (input_location, OPT_Wattributes,
7878 "requested alignment %d is larger than %d",
7879 requested_alignment, max_align);
7881 return !alignment_too_large_p;
7884 /* Handle a "aligned" attribute; arguments as in
7885 struct attribute_spec.handler. */
7887 static tree
7888 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7889 int flags, bool *no_add_attrs)
7891 tree decl = NULL_TREE;
7892 tree *type = NULL;
7893 int is_type = 0;
7894 tree align_expr;
7895 int i;
7897 if (args)
7899 align_expr = TREE_VALUE (args);
7900 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7901 && TREE_CODE (align_expr) != FUNCTION_DECL)
7902 align_expr = default_conversion (align_expr);
7904 else
7905 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7907 if (DECL_P (*node))
7909 decl = *node;
7910 type = &TREE_TYPE (decl);
7911 is_type = TREE_CODE (*node) == TYPE_DECL;
7913 else if (TYPE_P (*node))
7914 type = node, is_type = 1;
7916 if ((i = check_user_alignment (align_expr, false)) == -1
7917 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7918 *no_add_attrs = true;
7919 else if (is_type)
7921 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7922 /* OK, modify the type in place. */;
7923 /* If we have a TYPE_DECL, then copy the type, so that we
7924 don't accidentally modify a builtin type. See pushdecl. */
7925 else if (decl && TREE_TYPE (decl) != error_mark_node
7926 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7928 tree tt = TREE_TYPE (decl);
7929 *type = build_variant_type_copy (*type);
7930 DECL_ORIGINAL_TYPE (decl) = tt;
7931 TYPE_NAME (*type) = decl;
7932 TREE_USED (*type) = TREE_USED (decl);
7933 TREE_TYPE (decl) = *type;
7935 else
7936 *type = build_variant_type_copy (*type);
7938 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7939 TYPE_USER_ALIGN (*type) = 1;
7941 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7942 && TREE_CODE (decl) != FIELD_DECL)
7944 error ("alignment may not be specified for %q+D", decl);
7945 *no_add_attrs = true;
7947 else if (DECL_USER_ALIGN (decl)
7948 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7949 /* C++-11 [dcl.align/4]:
7951 When multiple alignment-specifiers are specified for an
7952 entity, the alignment requirement shall be set to the
7953 strictest specified alignment.
7955 This formally comes from the c++11 specification but we are
7956 doing it for the GNU attribute syntax as well. */
7957 *no_add_attrs = true;
7958 else if (TREE_CODE (decl) == FUNCTION_DECL
7959 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7961 if (DECL_USER_ALIGN (decl))
7962 error ("alignment for %q+D was previously specified as %d "
7963 "and may not be decreased", decl,
7964 DECL_ALIGN (decl) / BITS_PER_UNIT);
7965 else
7966 error ("alignment for %q+D must be at least %d", decl,
7967 DECL_ALIGN (decl) / BITS_PER_UNIT);
7968 *no_add_attrs = true;
7970 else
7972 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7973 DECL_USER_ALIGN (decl) = 1;
7976 return NULL_TREE;
7979 /* Handle a "weak" attribute; arguments as in
7980 struct attribute_spec.handler. */
7982 static tree
7983 handle_weak_attribute (tree *node, tree name,
7984 tree ARG_UNUSED (args),
7985 int ARG_UNUSED (flags),
7986 bool * ARG_UNUSED (no_add_attrs))
7988 if (TREE_CODE (*node) == FUNCTION_DECL
7989 && DECL_DECLARED_INLINE_P (*node))
7991 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7992 *no_add_attrs = true;
7994 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7996 error ("indirect function %q+D cannot be declared weak", *node);
7997 *no_add_attrs = true;
7998 return NULL_TREE;
8000 else if (TREE_CODE (*node) == FUNCTION_DECL
8001 || TREE_CODE (*node) == VAR_DECL)
8003 struct symtab_node *n = symtab_node::get (*node);
8004 if (n && n->refuse_visibility_changes)
8005 error ("%+D declared weak after being used", *node);
8006 declare_weak (*node);
8008 else
8009 warning (OPT_Wattributes, "%qE attribute ignored", name);
8011 return NULL_TREE;
8014 /* Handle an "alias" or "ifunc" attribute; arguments as in
8015 struct attribute_spec.handler, except that IS_ALIAS tells us
8016 whether this is an alias as opposed to ifunc attribute. */
8018 static tree
8019 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8020 bool *no_add_attrs)
8022 tree decl = *node;
8024 if (TREE_CODE (decl) != FUNCTION_DECL
8025 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
8027 warning (OPT_Wattributes, "%qE attribute ignored", name);
8028 *no_add_attrs = true;
8030 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8031 || (TREE_CODE (decl) != FUNCTION_DECL
8032 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8033 /* A static variable declaration is always a tentative definition,
8034 but the alias is a non-tentative definition which overrides. */
8035 || (TREE_CODE (decl) != FUNCTION_DECL
8036 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8038 error ("%q+D defined both normally and as %qE attribute", decl, name);
8039 *no_add_attrs = true;
8040 return NULL_TREE;
8042 else if (!is_alias
8043 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8044 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8046 error ("weak %q+D cannot be defined %qE", decl, name);
8047 *no_add_attrs = true;
8048 return NULL_TREE;
8051 /* Note that the very first time we process a nested declaration,
8052 decl_function_context will not be set. Indeed, *would* never
8053 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8054 we do below. After such frobbery, pushdecl would set the context.
8055 In any case, this is never what we want. */
8056 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8058 tree id;
8060 id = TREE_VALUE (args);
8061 if (TREE_CODE (id) != STRING_CST)
8063 error ("attribute %qE argument not a string", name);
8064 *no_add_attrs = true;
8065 return NULL_TREE;
8067 id = get_identifier (TREE_STRING_POINTER (id));
8068 /* This counts as a use of the object pointed to. */
8069 TREE_USED (id) = 1;
8071 if (TREE_CODE (decl) == FUNCTION_DECL)
8072 DECL_INITIAL (decl) = error_mark_node;
8073 else
8074 TREE_STATIC (decl) = 1;
8076 if (!is_alias)
8077 /* ifuncs are also aliases, so set that attribute too. */
8078 DECL_ATTRIBUTES (decl)
8079 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8081 else
8083 warning (OPT_Wattributes, "%qE attribute ignored", name);
8084 *no_add_attrs = true;
8087 if (decl_in_symtab_p (*node))
8089 struct symtab_node *n = symtab_node::get (decl);
8090 if (n && n->refuse_visibility_changes)
8092 if (is_alias)
8093 error ("%+D declared alias after being used", decl);
8094 else
8095 error ("%+D declared ifunc after being used", decl);
8100 return NULL_TREE;
8103 /* Handle an "alias" or "ifunc" attribute; arguments as in
8104 struct attribute_spec.handler. */
8106 static tree
8107 handle_ifunc_attribute (tree *node, tree name, tree args,
8108 int ARG_UNUSED (flags), bool *no_add_attrs)
8110 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8113 /* Handle an "alias" or "ifunc" attribute; arguments as in
8114 struct attribute_spec.handler. */
8116 static tree
8117 handle_alias_attribute (tree *node, tree name, tree args,
8118 int ARG_UNUSED (flags), bool *no_add_attrs)
8120 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8123 /* Handle a "weakref" attribute; arguments as in struct
8124 attribute_spec.handler. */
8126 static tree
8127 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8128 int flags, bool *no_add_attrs)
8130 tree attr = NULL_TREE;
8132 /* We must ignore the attribute when it is associated with
8133 local-scoped decls, since attribute alias is ignored and many
8134 such symbols do not even have a DECL_WEAK field. */
8135 if (decl_function_context (*node)
8136 || current_function_decl
8137 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
8139 warning (OPT_Wattributes, "%qE attribute ignored", name);
8140 *no_add_attrs = true;
8141 return NULL_TREE;
8144 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8146 error ("indirect function %q+D cannot be declared weakref", *node);
8147 *no_add_attrs = true;
8148 return NULL_TREE;
8151 /* The idea here is that `weakref("name")' mutates into `weakref,
8152 alias("name")', and weakref without arguments, in turn,
8153 implicitly adds weak. */
8155 if (args)
8157 attr = tree_cons (get_identifier ("alias"), args, attr);
8158 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8160 *no_add_attrs = true;
8162 decl_attributes (node, attr, flags);
8164 else
8166 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8167 error_at (DECL_SOURCE_LOCATION (*node),
8168 "weakref attribute must appear before alias attribute");
8170 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8171 and that isn't supported; and because it wants to add it to
8172 the list of weak decls, which isn't helpful. */
8173 DECL_WEAK (*node) = 1;
8176 if (decl_in_symtab_p (*node))
8178 struct symtab_node *n = symtab_node::get (*node);
8179 if (n && n->refuse_visibility_changes)
8180 error ("%+D declared weakref after being used", *node);
8183 return NULL_TREE;
8186 /* Handle an "visibility" attribute; arguments as in
8187 struct attribute_spec.handler. */
8189 static tree
8190 handle_visibility_attribute (tree *node, tree name, tree args,
8191 int ARG_UNUSED (flags),
8192 bool *ARG_UNUSED (no_add_attrs))
8194 tree decl = *node;
8195 tree id = TREE_VALUE (args);
8196 enum symbol_visibility vis;
8198 if (TYPE_P (*node))
8200 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8201 /* OK */;
8202 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8204 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8205 name);
8206 return NULL_TREE;
8208 else if (TYPE_FIELDS (*node))
8210 error ("%qE attribute ignored because %qT is already defined",
8211 name, *node);
8212 return NULL_TREE;
8215 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8217 warning (OPT_Wattributes, "%qE attribute ignored", name);
8218 return NULL_TREE;
8221 if (TREE_CODE (id) != STRING_CST)
8223 error ("visibility argument not a string");
8224 return NULL_TREE;
8227 /* If this is a type, set the visibility on the type decl. */
8228 if (TYPE_P (decl))
8230 decl = TYPE_NAME (decl);
8231 if (!decl)
8232 return NULL_TREE;
8233 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8235 warning (OPT_Wattributes, "%qE attribute ignored on types",
8236 name);
8237 return NULL_TREE;
8241 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8242 vis = VISIBILITY_DEFAULT;
8243 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8244 vis = VISIBILITY_INTERNAL;
8245 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8246 vis = VISIBILITY_HIDDEN;
8247 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8248 vis = VISIBILITY_PROTECTED;
8249 else
8251 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8252 vis = VISIBILITY_DEFAULT;
8255 if (DECL_VISIBILITY_SPECIFIED (decl)
8256 && vis != DECL_VISIBILITY (decl))
8258 tree attributes = (TYPE_P (*node)
8259 ? TYPE_ATTRIBUTES (*node)
8260 : DECL_ATTRIBUTES (decl));
8261 if (lookup_attribute ("visibility", attributes))
8262 error ("%qD redeclared with different visibility", decl);
8263 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8264 && lookup_attribute ("dllimport", attributes))
8265 error ("%qD was declared %qs which implies default visibility",
8266 decl, "dllimport");
8267 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8268 && lookup_attribute ("dllexport", attributes))
8269 error ("%qD was declared %qs which implies default visibility",
8270 decl, "dllexport");
8273 DECL_VISIBILITY (decl) = vis;
8274 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8276 /* Go ahead and attach the attribute to the node as well. This is needed
8277 so we can determine whether we have VISIBILITY_DEFAULT because the
8278 visibility was not specified, or because it was explicitly overridden
8279 from the containing scope. */
8281 return NULL_TREE;
8284 /* Determine the ELF symbol visibility for DECL, which is either a
8285 variable or a function. It is an error to use this function if a
8286 definition of DECL is not available in this translation unit.
8287 Returns true if the final visibility has been determined by this
8288 function; false if the caller is free to make additional
8289 modifications. */
8291 bool
8292 c_determine_visibility (tree decl)
8294 gcc_assert (TREE_CODE (decl) == VAR_DECL
8295 || TREE_CODE (decl) == FUNCTION_DECL);
8297 /* If the user explicitly specified the visibility with an
8298 attribute, honor that. DECL_VISIBILITY will have been set during
8299 the processing of the attribute. We check for an explicit
8300 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8301 to distinguish the use of an attribute from the use of a "#pragma
8302 GCC visibility push(...)"; in the latter case we still want other
8303 considerations to be able to overrule the #pragma. */
8304 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8305 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8306 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8307 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8308 return true;
8310 /* Set default visibility to whatever the user supplied with
8311 visibility_specified depending on #pragma GCC visibility. */
8312 if (!DECL_VISIBILITY_SPECIFIED (decl))
8314 if (visibility_options.inpragma
8315 || DECL_VISIBILITY (decl) != default_visibility)
8317 DECL_VISIBILITY (decl) = default_visibility;
8318 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8319 /* If visibility changed and DECL already has DECL_RTL, ensure
8320 symbol flags are updated. */
8321 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8322 || TREE_CODE (decl) == FUNCTION_DECL)
8323 && DECL_RTL_SET_P (decl))
8324 make_decl_rtl (decl);
8327 return false;
8330 /* Handle an "tls_model" attribute; arguments as in
8331 struct attribute_spec.handler. */
8333 static tree
8334 handle_tls_model_attribute (tree *node, tree name, tree args,
8335 int ARG_UNUSED (flags), bool *no_add_attrs)
8337 tree id;
8338 tree decl = *node;
8339 enum tls_model kind;
8341 *no_add_attrs = true;
8343 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8345 warning (OPT_Wattributes, "%qE attribute ignored", name);
8346 return NULL_TREE;
8349 kind = DECL_TLS_MODEL (decl);
8350 id = TREE_VALUE (args);
8351 if (TREE_CODE (id) != STRING_CST)
8353 error ("tls_model argument not a string");
8354 return NULL_TREE;
8357 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8358 kind = TLS_MODEL_LOCAL_EXEC;
8359 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8360 kind = TLS_MODEL_INITIAL_EXEC;
8361 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8362 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8363 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8364 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8365 else
8366 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8368 set_decl_tls_model (decl, kind);
8369 return NULL_TREE;
8372 /* Handle a "no_instrument_function" attribute; arguments as in
8373 struct attribute_spec.handler. */
8375 static tree
8376 handle_no_instrument_function_attribute (tree *node, tree name,
8377 tree ARG_UNUSED (args),
8378 int ARG_UNUSED (flags),
8379 bool *no_add_attrs)
8381 tree decl = *node;
8383 if (TREE_CODE (decl) != FUNCTION_DECL)
8385 error_at (DECL_SOURCE_LOCATION (decl),
8386 "%qE attribute applies only to functions", name);
8387 *no_add_attrs = true;
8389 else
8390 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8392 return NULL_TREE;
8395 /* Handle a "malloc" attribute; arguments as in
8396 struct attribute_spec.handler. */
8398 static tree
8399 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8400 int ARG_UNUSED (flags), bool *no_add_attrs)
8402 if (TREE_CODE (*node) == FUNCTION_DECL
8403 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8404 DECL_IS_MALLOC (*node) = 1;
8405 else
8407 warning (OPT_Wattributes, "%qE attribute ignored", name);
8408 *no_add_attrs = true;
8411 return NULL_TREE;
8414 /* Handle a "alloc_size" attribute; arguments as in
8415 struct attribute_spec.handler. */
8417 static tree
8418 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8419 int ARG_UNUSED (flags), bool *no_add_attrs)
8421 unsigned arg_count = type_num_arguments (*node);
8422 for (; args; args = TREE_CHAIN (args))
8424 tree position = TREE_VALUE (args);
8425 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8426 && TREE_CODE (position) != FUNCTION_DECL)
8427 position = default_conversion (position);
8429 if (!tree_fits_uhwi_p (position)
8430 || !arg_count
8431 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8433 warning (OPT_Wattributes,
8434 "alloc_size parameter outside range");
8435 *no_add_attrs = true;
8436 return NULL_TREE;
8439 return NULL_TREE;
8442 /* Handle a "alloc_align" attribute; arguments as in
8443 struct attribute_spec.handler. */
8445 static tree
8446 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8447 bool *no_add_attrs)
8449 unsigned arg_count = type_num_arguments (*node);
8450 tree position = TREE_VALUE (args);
8451 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8452 position = default_conversion (position);
8454 if (!tree_fits_uhwi_p (position)
8455 || !arg_count
8456 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8458 warning (OPT_Wattributes,
8459 "alloc_align parameter outside range");
8460 *no_add_attrs = true;
8461 return NULL_TREE;
8463 return NULL_TREE;
8466 /* Handle a "assume_aligned" attribute; arguments as in
8467 struct attribute_spec.handler. */
8469 static tree
8470 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8471 bool *no_add_attrs)
8473 for (; args; args = TREE_CHAIN (args))
8475 tree position = TREE_VALUE (args);
8476 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8477 && TREE_CODE (position) != FUNCTION_DECL)
8478 position = default_conversion (position);
8480 if (TREE_CODE (position) != INTEGER_CST)
8482 warning (OPT_Wattributes,
8483 "assume_aligned parameter not integer constant");
8484 *no_add_attrs = true;
8485 return NULL_TREE;
8488 return NULL_TREE;
8491 /* Handle a "fn spec" attribute; arguments as in
8492 struct attribute_spec.handler. */
8494 static tree
8495 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8496 tree args, int ARG_UNUSED (flags),
8497 bool *no_add_attrs ATTRIBUTE_UNUSED)
8499 gcc_assert (args
8500 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8501 && !TREE_CHAIN (args));
8502 return NULL_TREE;
8505 /* Handle a "bnd_variable_size" attribute; arguments as in
8506 struct attribute_spec.handler. */
8508 static tree
8509 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8510 int ARG_UNUSED (flags), bool *no_add_attrs)
8512 if (TREE_CODE (*node) != FIELD_DECL)
8514 warning (OPT_Wattributes, "%qE attribute ignored", name);
8515 *no_add_attrs = true;
8518 return NULL_TREE;
8521 /* Handle a "bnd_legacy" attribute; arguments as in
8522 struct attribute_spec.handler. */
8524 static tree
8525 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8526 int ARG_UNUSED (flags), bool *no_add_attrs)
8528 if (TREE_CODE (*node) != FUNCTION_DECL)
8530 warning (OPT_Wattributes, "%qE attribute ignored", name);
8531 *no_add_attrs = true;
8534 return NULL_TREE;
8537 /* Handle a "bnd_instrument" attribute; arguments as in
8538 struct attribute_spec.handler. */
8540 static tree
8541 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8542 int ARG_UNUSED (flags), bool *no_add_attrs)
8544 if (TREE_CODE (*node) != FUNCTION_DECL)
8546 warning (OPT_Wattributes, "%qE attribute ignored", name);
8547 *no_add_attrs = true;
8550 return NULL_TREE;
8553 /* Handle a "warn_unused" attribute; arguments as in
8554 struct attribute_spec.handler. */
8556 static tree
8557 handle_warn_unused_attribute (tree *node, tree name,
8558 tree args ATTRIBUTE_UNUSED,
8559 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8561 if (TYPE_P (*node))
8562 /* Do nothing else, just set the attribute. We'll get at
8563 it later with lookup_attribute. */
8565 else
8567 warning (OPT_Wattributes, "%qE attribute ignored", name);
8568 *no_add_attrs = true;
8571 return NULL_TREE;
8574 /* Handle an "omp declare simd" attribute; arguments as in
8575 struct attribute_spec.handler. */
8577 static tree
8578 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8580 return NULL_TREE;
8583 /* Handle an "omp declare target" attribute; arguments as in
8584 struct attribute_spec.handler. */
8586 static tree
8587 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8589 return NULL_TREE;
8592 /* Handle a "returns_twice" attribute; arguments as in
8593 struct attribute_spec.handler. */
8595 static tree
8596 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8597 int ARG_UNUSED (flags), bool *no_add_attrs)
8599 if (TREE_CODE (*node) == FUNCTION_DECL)
8600 DECL_IS_RETURNS_TWICE (*node) = 1;
8601 else
8603 warning (OPT_Wattributes, "%qE attribute ignored", name);
8604 *no_add_attrs = true;
8607 return NULL_TREE;
8610 /* Handle a "no_limit_stack" attribute; arguments as in
8611 struct attribute_spec.handler. */
8613 static tree
8614 handle_no_limit_stack_attribute (tree *node, tree name,
8615 tree ARG_UNUSED (args),
8616 int ARG_UNUSED (flags),
8617 bool *no_add_attrs)
8619 tree decl = *node;
8621 if (TREE_CODE (decl) != FUNCTION_DECL)
8623 error_at (DECL_SOURCE_LOCATION (decl),
8624 "%qE attribute applies only to functions", name);
8625 *no_add_attrs = true;
8627 else if (DECL_INITIAL (decl))
8629 error_at (DECL_SOURCE_LOCATION (decl),
8630 "can%'t set %qE attribute after definition", name);
8631 *no_add_attrs = true;
8633 else
8634 DECL_NO_LIMIT_STACK (decl) = 1;
8636 return NULL_TREE;
8639 /* Handle a "pure" attribute; arguments as in
8640 struct attribute_spec.handler. */
8642 static tree
8643 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8644 int ARG_UNUSED (flags), bool *no_add_attrs)
8646 if (TREE_CODE (*node) == FUNCTION_DECL)
8647 DECL_PURE_P (*node) = 1;
8648 /* ??? TODO: Support types. */
8649 else
8651 warning (OPT_Wattributes, "%qE attribute ignored", name);
8652 *no_add_attrs = true;
8655 return NULL_TREE;
8658 /* Digest an attribute list destined for a transactional memory statement.
8659 ALLOWED is the set of attributes that are allowed for this statement;
8660 return the attribute we parsed. Multiple attributes are never allowed. */
8663 parse_tm_stmt_attr (tree attrs, int allowed)
8665 tree a_seen = NULL;
8666 int m_seen = 0;
8668 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8670 tree a = TREE_PURPOSE (attrs);
8671 int m = 0;
8673 if (is_attribute_p ("outer", a))
8674 m = TM_STMT_ATTR_OUTER;
8676 if ((m & allowed) == 0)
8678 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8679 continue;
8682 if (m_seen == 0)
8684 a_seen = a;
8685 m_seen = m;
8687 else if (m_seen == m)
8688 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8689 else
8690 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8693 return m_seen;
8696 /* Transform a TM attribute name into a maskable integer and back.
8697 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8698 to how the lack of an attribute is treated. */
8701 tm_attr_to_mask (tree attr)
8703 if (attr == NULL)
8704 return 0;
8705 if (is_attribute_p ("transaction_safe", attr))
8706 return TM_ATTR_SAFE;
8707 if (is_attribute_p ("transaction_callable", attr))
8708 return TM_ATTR_CALLABLE;
8709 if (is_attribute_p ("transaction_pure", attr))
8710 return TM_ATTR_PURE;
8711 if (is_attribute_p ("transaction_unsafe", attr))
8712 return TM_ATTR_IRREVOCABLE;
8713 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8714 return TM_ATTR_MAY_CANCEL_OUTER;
8715 return 0;
8718 tree
8719 tm_mask_to_attr (int mask)
8721 const char *str;
8722 switch (mask)
8724 case TM_ATTR_SAFE:
8725 str = "transaction_safe";
8726 break;
8727 case TM_ATTR_CALLABLE:
8728 str = "transaction_callable";
8729 break;
8730 case TM_ATTR_PURE:
8731 str = "transaction_pure";
8732 break;
8733 case TM_ATTR_IRREVOCABLE:
8734 str = "transaction_unsafe";
8735 break;
8736 case TM_ATTR_MAY_CANCEL_OUTER:
8737 str = "transaction_may_cancel_outer";
8738 break;
8739 default:
8740 gcc_unreachable ();
8742 return get_identifier (str);
8745 /* Return the first TM attribute seen in LIST. */
8747 tree
8748 find_tm_attribute (tree list)
8750 for (; list ; list = TREE_CHAIN (list))
8752 tree name = TREE_PURPOSE (list);
8753 if (tm_attr_to_mask (name) != 0)
8754 return name;
8756 return NULL_TREE;
8759 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8760 Here we accept only function types, and verify that none of the other
8761 function TM attributes are also applied. */
8762 /* ??? We need to accept class types for C++, but not C. This greatly
8763 complicates this function, since we can no longer rely on the extra
8764 processing given by function_type_required. */
8766 static tree
8767 handle_tm_attribute (tree *node, tree name, tree args,
8768 int flags, bool *no_add_attrs)
8770 /* Only one path adds the attribute; others don't. */
8771 *no_add_attrs = true;
8773 switch (TREE_CODE (*node))
8775 case RECORD_TYPE:
8776 case UNION_TYPE:
8777 /* Only tm_callable and tm_safe apply to classes. */
8778 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8779 goto ignored;
8780 /* FALLTHRU */
8782 case FUNCTION_TYPE:
8783 case METHOD_TYPE:
8785 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8786 if (old_name == name)
8788 else if (old_name != NULL_TREE)
8789 error ("type was previously declared %qE", old_name);
8790 else
8791 *no_add_attrs = false;
8793 break;
8795 case POINTER_TYPE:
8797 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8798 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8800 tree fn_tmp = TREE_TYPE (*node);
8801 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8802 *node = build_pointer_type (fn_tmp);
8803 break;
8806 /* FALLTHRU */
8808 default:
8809 /* If a function is next, pass it on to be tried next. */
8810 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8811 return tree_cons (name, args, NULL);
8813 ignored:
8814 warning (OPT_Wattributes, "%qE attribute ignored", name);
8815 break;
8818 return NULL_TREE;
8821 /* Handle the TM_WRAP attribute; arguments as in
8822 struct attribute_spec.handler. */
8824 static tree
8825 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8826 int ARG_UNUSED (flags), bool *no_add_attrs)
8828 tree decl = *node;
8830 /* We don't need the attribute even on success, since we
8831 record the entry in an external table. */
8832 *no_add_attrs = true;
8834 if (TREE_CODE (decl) != FUNCTION_DECL)
8835 warning (OPT_Wattributes, "%qE attribute ignored", name);
8836 else
8838 tree wrap_decl = TREE_VALUE (args);
8839 if (error_operand_p (wrap_decl))
8841 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8842 && TREE_CODE (wrap_decl) != VAR_DECL
8843 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8844 error ("%qE argument not an identifier", name);
8845 else
8847 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8848 wrap_decl = lookup_name (wrap_decl);
8849 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8851 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8852 TREE_TYPE (wrap_decl)))
8853 record_tm_replacement (wrap_decl, decl);
8854 else
8855 error ("%qD is not compatible with %qD", wrap_decl, decl);
8857 else
8858 error ("%qE argument is not a function", name);
8862 return NULL_TREE;
8865 /* Ignore the given attribute. Used when this attribute may be usefully
8866 overridden by the target, but is not used generically. */
8868 static tree
8869 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8870 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8871 bool *no_add_attrs)
8873 *no_add_attrs = true;
8874 return NULL_TREE;
8877 /* Handle a "no vops" attribute; arguments as in
8878 struct attribute_spec.handler. */
8880 static tree
8881 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8882 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8883 bool *ARG_UNUSED (no_add_attrs))
8885 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8886 DECL_IS_NOVOPS (*node) = 1;
8887 return NULL_TREE;
8890 /* Handle a "deprecated" attribute; arguments as in
8891 struct attribute_spec.handler. */
8893 static tree
8894 handle_deprecated_attribute (tree *node, tree name,
8895 tree args, int flags,
8896 bool *no_add_attrs)
8898 tree type = NULL_TREE;
8899 int warn = 0;
8900 tree what = NULL_TREE;
8902 if (!args)
8903 *no_add_attrs = true;
8904 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8906 error ("deprecated message is not a string");
8907 *no_add_attrs = true;
8910 if (DECL_P (*node))
8912 tree decl = *node;
8913 type = TREE_TYPE (decl);
8915 if (TREE_CODE (decl) == TYPE_DECL
8916 || TREE_CODE (decl) == PARM_DECL
8917 || TREE_CODE (decl) == VAR_DECL
8918 || TREE_CODE (decl) == FUNCTION_DECL
8919 || TREE_CODE (decl) == FIELD_DECL
8920 || objc_method_decl (TREE_CODE (decl)))
8921 TREE_DEPRECATED (decl) = 1;
8922 else
8923 warn = 1;
8925 else if (TYPE_P (*node))
8927 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8928 *node = build_variant_type_copy (*node);
8929 TREE_DEPRECATED (*node) = 1;
8930 type = *node;
8932 else
8933 warn = 1;
8935 if (warn)
8937 *no_add_attrs = true;
8938 if (type && TYPE_NAME (type))
8940 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8941 what = TYPE_NAME (*node);
8942 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8943 && DECL_NAME (TYPE_NAME (type)))
8944 what = DECL_NAME (TYPE_NAME (type));
8946 if (what)
8947 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8948 else
8949 warning (OPT_Wattributes, "%qE attribute ignored", name);
8952 return NULL_TREE;
8955 /* Handle a "vector_size" attribute; arguments as in
8956 struct attribute_spec.handler. */
8958 static tree
8959 handle_vector_size_attribute (tree *node, tree name, tree args,
8960 int ARG_UNUSED (flags),
8961 bool *no_add_attrs)
8963 unsigned HOST_WIDE_INT vecsize, nunits;
8964 machine_mode orig_mode;
8965 tree type = *node, new_type, size;
8967 *no_add_attrs = true;
8969 size = TREE_VALUE (args);
8970 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8971 && TREE_CODE (size) != FUNCTION_DECL)
8972 size = default_conversion (size);
8974 if (!tree_fits_uhwi_p (size))
8976 warning (OPT_Wattributes, "%qE attribute ignored", name);
8977 return NULL_TREE;
8980 /* Get the vector size (in bytes). */
8981 vecsize = tree_to_uhwi (size);
8983 /* We need to provide for vector pointers, vector arrays, and
8984 functions returning vectors. For example:
8986 __attribute__((vector_size(16))) short *foo;
8988 In this case, the mode is SI, but the type being modified is
8989 HI, so we need to look further. */
8991 while (POINTER_TYPE_P (type)
8992 || TREE_CODE (type) == FUNCTION_TYPE
8993 || TREE_CODE (type) == METHOD_TYPE
8994 || TREE_CODE (type) == ARRAY_TYPE
8995 || TREE_CODE (type) == OFFSET_TYPE)
8996 type = TREE_TYPE (type);
8998 /* Get the mode of the type being modified. */
8999 orig_mode = TYPE_MODE (type);
9001 if ((!INTEGRAL_TYPE_P (type)
9002 && !SCALAR_FLOAT_TYPE_P (type)
9003 && !FIXED_POINT_TYPE_P (type))
9004 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9005 && GET_MODE_CLASS (orig_mode) != MODE_INT
9006 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9007 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9008 || TREE_CODE (type) == BOOLEAN_TYPE)
9010 error ("invalid vector type for attribute %qE", name);
9011 return NULL_TREE;
9014 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9016 error ("vector size not an integral multiple of component size");
9017 return NULL;
9020 if (vecsize == 0)
9022 error ("zero vector size");
9023 return NULL;
9026 /* Calculate how many units fit in the vector. */
9027 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9028 if (nunits & (nunits - 1))
9030 error ("number of components of the vector not a power of two");
9031 return NULL_TREE;
9034 new_type = build_vector_type (type, nunits);
9036 /* Build back pointers if needed. */
9037 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9039 return NULL_TREE;
9042 /* Handle the "nonnull" attribute. */
9043 static tree
9044 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9045 tree args, int ARG_UNUSED (flags),
9046 bool *no_add_attrs)
9048 tree type = *node;
9049 unsigned HOST_WIDE_INT attr_arg_num;
9051 /* If no arguments are specified, all pointer arguments should be
9052 non-null. Verify a full prototype is given so that the arguments
9053 will have the correct types when we actually check them later. */
9054 if (!args)
9056 if (!prototype_p (type))
9058 error ("nonnull attribute without arguments on a non-prototype");
9059 *no_add_attrs = true;
9061 return NULL_TREE;
9064 /* Argument list specified. Verify that each argument number references
9065 a pointer argument. */
9066 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9068 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9070 tree arg = TREE_VALUE (args);
9071 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9072 && TREE_CODE (arg) != FUNCTION_DECL)
9073 arg = default_conversion (arg);
9075 if (!get_nonnull_operand (arg, &arg_num))
9077 error ("nonnull argument has invalid operand number (argument %lu)",
9078 (unsigned long) attr_arg_num);
9079 *no_add_attrs = true;
9080 return NULL_TREE;
9083 if (prototype_p (type))
9085 function_args_iterator iter;
9086 tree argument;
9088 function_args_iter_init (&iter, type);
9089 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9091 argument = function_args_iter_cond (&iter);
9092 if (argument == NULL_TREE || ck_num == arg_num)
9093 break;
9096 if (!argument
9097 || TREE_CODE (argument) == VOID_TYPE)
9099 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9100 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9101 *no_add_attrs = true;
9102 return NULL_TREE;
9105 if (TREE_CODE (argument) != POINTER_TYPE)
9107 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9108 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9109 *no_add_attrs = true;
9110 return NULL_TREE;
9115 return NULL_TREE;
9118 /* Check the argument list of a function call for null in argument slots
9119 that are marked as requiring a non-null pointer argument. The NARGS
9120 arguments are passed in the array ARGARRAY.
9123 static void
9124 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9126 tree a;
9127 int i;
9129 attrs = lookup_attribute ("nonnull", attrs);
9130 if (attrs == NULL_TREE)
9131 return;
9133 a = attrs;
9134 /* See if any of the nonnull attributes has no arguments. If so,
9135 then every pointer argument is checked (in which case the check
9136 for pointer type is done in check_nonnull_arg). */
9137 if (TREE_VALUE (a) != NULL_TREE)
9139 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9140 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9142 if (a != NULL_TREE)
9143 for (i = 0; i < nargs; i++)
9144 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9145 i + 1);
9146 else
9148 /* Walk the argument list. If we encounter an argument number we
9149 should check for non-null, do it. */
9150 for (i = 0; i < nargs; i++)
9152 for (a = attrs; ; a = TREE_CHAIN (a))
9154 a = lookup_attribute ("nonnull", a);
9155 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9156 break;
9159 if (a != NULL_TREE)
9160 check_function_arguments_recurse (check_nonnull_arg, NULL,
9161 argarray[i], i + 1);
9166 /* Check that the Nth argument of a function call (counting backwards
9167 from the end) is a (pointer)0. The NARGS arguments are passed in the
9168 array ARGARRAY. */
9170 static void
9171 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9173 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9175 if (attr)
9177 int len = 0;
9178 int pos = 0;
9179 tree sentinel;
9180 function_args_iterator iter;
9181 tree t;
9183 /* Skip over the named arguments. */
9184 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9186 if (len == nargs)
9187 break;
9188 len++;
9191 if (TREE_VALUE (attr))
9193 tree p = TREE_VALUE (TREE_VALUE (attr));
9194 pos = TREE_INT_CST_LOW (p);
9197 /* The sentinel must be one of the varargs, i.e.
9198 in position >= the number of fixed arguments. */
9199 if ((nargs - 1 - pos) < len)
9201 warning (OPT_Wformat_,
9202 "not enough variable arguments to fit a sentinel");
9203 return;
9206 /* Validate the sentinel. */
9207 sentinel = argarray[nargs - 1 - pos];
9208 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9209 || !integer_zerop (sentinel))
9210 /* Although __null (in C++) is only an integer we allow it
9211 nevertheless, as we are guaranteed that it's exactly
9212 as wide as a pointer, and we don't want to force
9213 users to cast the NULL they have written there.
9214 We warn with -Wstrict-null-sentinel, though. */
9215 && (warn_strict_null_sentinel || null_node != sentinel))
9216 warning (OPT_Wformat_, "missing sentinel in function call");
9220 /* Helper for check_function_nonnull; given a list of operands which
9221 must be non-null in ARGS, determine if operand PARAM_NUM should be
9222 checked. */
9224 static bool
9225 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9227 unsigned HOST_WIDE_INT arg_num = 0;
9229 for (; args; args = TREE_CHAIN (args))
9231 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9233 gcc_assert (found);
9235 if (arg_num == param_num)
9236 return true;
9238 return false;
9241 /* Check that the function argument PARAM (which is operand number
9242 PARAM_NUM) is non-null. This is called by check_function_nonnull
9243 via check_function_arguments_recurse. */
9245 static void
9246 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9247 unsigned HOST_WIDE_INT param_num)
9249 /* Just skip checking the argument if it's not a pointer. This can
9250 happen if the "nonnull" attribute was given without an operand
9251 list (which means to check every pointer argument). */
9253 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9254 return;
9256 if (integer_zerop (param))
9257 warning (OPT_Wnonnull, "null argument where non-null required "
9258 "(argument %lu)", (unsigned long) param_num);
9261 /* Helper for nonnull attribute handling; fetch the operand number
9262 from the attribute argument list. */
9264 static bool
9265 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9267 /* Verify the arg number is a small constant. */
9268 if (tree_fits_uhwi_p (arg_num_expr))
9270 *valp = TREE_INT_CST_LOW (arg_num_expr);
9271 return true;
9273 else
9274 return false;
9277 /* Handle a "nothrow" attribute; arguments as in
9278 struct attribute_spec.handler. */
9280 static tree
9281 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9282 int ARG_UNUSED (flags), bool *no_add_attrs)
9284 if (TREE_CODE (*node) == FUNCTION_DECL)
9285 TREE_NOTHROW (*node) = 1;
9286 /* ??? TODO: Support types. */
9287 else
9289 warning (OPT_Wattributes, "%qE attribute ignored", name);
9290 *no_add_attrs = true;
9293 return NULL_TREE;
9296 /* Handle a "cleanup" attribute; arguments as in
9297 struct attribute_spec.handler. */
9299 static tree
9300 handle_cleanup_attribute (tree *node, tree name, tree args,
9301 int ARG_UNUSED (flags), bool *no_add_attrs)
9303 tree decl = *node;
9304 tree cleanup_id, cleanup_decl;
9306 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9307 for global destructors in C++. This requires infrastructure that
9308 we don't have generically at the moment. It's also not a feature
9309 we'd be missing too much, since we do have attribute constructor. */
9310 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9312 warning (OPT_Wattributes, "%qE attribute ignored", name);
9313 *no_add_attrs = true;
9314 return NULL_TREE;
9317 /* Verify that the argument is a function in scope. */
9318 /* ??? We could support pointers to functions here as well, if
9319 that was considered desirable. */
9320 cleanup_id = TREE_VALUE (args);
9321 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9323 error ("cleanup argument not an identifier");
9324 *no_add_attrs = true;
9325 return NULL_TREE;
9327 cleanup_decl = lookup_name (cleanup_id);
9328 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9330 error ("cleanup argument not a function");
9331 *no_add_attrs = true;
9332 return NULL_TREE;
9335 /* That the function has proper type is checked with the
9336 eventual call to build_function_call. */
9338 return NULL_TREE;
9341 /* Handle a "warn_unused_result" attribute. No special handling. */
9343 static tree
9344 handle_warn_unused_result_attribute (tree *node, tree name,
9345 tree ARG_UNUSED (args),
9346 int ARG_UNUSED (flags), bool *no_add_attrs)
9348 /* Ignore the attribute for functions not returning any value. */
9349 if (VOID_TYPE_P (TREE_TYPE (*node)))
9351 warning (OPT_Wattributes, "%qE attribute ignored", name);
9352 *no_add_attrs = true;
9355 return NULL_TREE;
9358 /* Handle a "sentinel" attribute. */
9360 static tree
9361 handle_sentinel_attribute (tree *node, tree name, tree args,
9362 int ARG_UNUSED (flags), bool *no_add_attrs)
9364 if (!prototype_p (*node))
9366 warning (OPT_Wattributes,
9367 "%qE attribute requires prototypes with named arguments", name);
9368 *no_add_attrs = true;
9370 else
9372 if (!stdarg_p (*node))
9374 warning (OPT_Wattributes,
9375 "%qE attribute only applies to variadic functions", name);
9376 *no_add_attrs = true;
9380 if (args)
9382 tree position = TREE_VALUE (args);
9383 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9384 && TREE_CODE (position) != FUNCTION_DECL)
9385 position = default_conversion (position);
9387 if (TREE_CODE (position) != INTEGER_CST
9388 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9390 warning (OPT_Wattributes,
9391 "requested position is not an integer constant");
9392 *no_add_attrs = true;
9394 else
9396 if (tree_int_cst_lt (position, integer_zero_node))
9398 warning (OPT_Wattributes,
9399 "requested position is less than zero");
9400 *no_add_attrs = true;
9405 return NULL_TREE;
9408 /* Handle a "type_generic" attribute. */
9410 static tree
9411 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9412 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9413 bool * ARG_UNUSED (no_add_attrs))
9415 /* Ensure we have a function type. */
9416 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9418 /* Ensure we have a variadic function. */
9419 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9421 return NULL_TREE;
9424 /* Handle a "target" attribute. */
9426 static tree
9427 handle_target_attribute (tree *node, tree name, tree args, int flags,
9428 bool *no_add_attrs)
9430 /* Ensure we have a function type. */
9431 if (TREE_CODE (*node) != FUNCTION_DECL)
9433 warning (OPT_Wattributes, "%qE attribute ignored", name);
9434 *no_add_attrs = true;
9436 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9437 flags))
9438 *no_add_attrs = true;
9440 return NULL_TREE;
9443 /* Arguments being collected for optimization. */
9444 typedef const char *const_char_p; /* For DEF_VEC_P. */
9445 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9448 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9449 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9450 false for #pragma GCC optimize. */
9452 bool
9453 parse_optimize_options (tree args, bool attr_p)
9455 bool ret = true;
9456 unsigned opt_argc;
9457 unsigned i;
9458 int saved_flag_strict_aliasing;
9459 const char **opt_argv;
9460 struct cl_decoded_option *decoded_options;
9461 unsigned int decoded_options_count;
9462 tree ap;
9464 /* Build up argv vector. Just in case the string is stored away, use garbage
9465 collected strings. */
9466 vec_safe_truncate (optimize_args, 0);
9467 vec_safe_push (optimize_args, (const char *) NULL);
9469 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9471 tree value = TREE_VALUE (ap);
9473 if (TREE_CODE (value) == INTEGER_CST)
9475 char buffer[20];
9476 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9477 vec_safe_push (optimize_args, ggc_strdup (buffer));
9480 else if (TREE_CODE (value) == STRING_CST)
9482 /* Split string into multiple substrings. */
9483 size_t len = TREE_STRING_LENGTH (value);
9484 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9485 char *end = p + len;
9486 char *comma;
9487 char *next_p = p;
9489 while (next_p != NULL)
9491 size_t len2;
9492 char *q, *r;
9494 p = next_p;
9495 comma = strchr (p, ',');
9496 if (comma)
9498 len2 = comma - p;
9499 *comma = '\0';
9500 next_p = comma+1;
9502 else
9504 len2 = end - p;
9505 next_p = NULL;
9508 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9510 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9511 options. */
9512 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9514 ret = false;
9515 if (attr_p)
9516 warning (OPT_Wattributes,
9517 "bad option %s to optimize attribute", p);
9518 else
9519 warning (OPT_Wpragmas,
9520 "bad option %s to pragma attribute", p);
9521 continue;
9524 if (*p != '-')
9526 *r++ = '-';
9528 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9529 itself is -Os, and any other switch begins with a -f. */
9530 if ((*p >= '0' && *p <= '9')
9531 || (p[0] == 's' && p[1] == '\0'))
9532 *r++ = 'O';
9533 else if (*p != 'O')
9534 *r++ = 'f';
9537 memcpy (r, p, len2);
9538 r[len2] = '\0';
9539 vec_safe_push (optimize_args, (const char *) q);
9545 opt_argc = optimize_args->length ();
9546 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9548 for (i = 1; i < opt_argc; i++)
9549 opt_argv[i] = (*optimize_args)[i];
9551 saved_flag_strict_aliasing = flag_strict_aliasing;
9553 /* Now parse the options. */
9554 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9555 &decoded_options,
9556 &decoded_options_count);
9557 decode_options (&global_options, &global_options_set,
9558 decoded_options, decoded_options_count,
9559 input_location, global_dc);
9561 targetm.override_options_after_change();
9563 /* Don't allow changing -fstrict-aliasing. */
9564 flag_strict_aliasing = saved_flag_strict_aliasing;
9566 optimize_args->truncate (0);
9567 return ret;
9570 /* For handling "optimize" attribute. arguments as in
9571 struct attribute_spec.handler. */
9573 static tree
9574 handle_optimize_attribute (tree *node, tree name, tree args,
9575 int ARG_UNUSED (flags), bool *no_add_attrs)
9577 /* Ensure we have a function type. */
9578 if (TREE_CODE (*node) != FUNCTION_DECL)
9580 warning (OPT_Wattributes, "%qE attribute ignored", name);
9581 *no_add_attrs = true;
9583 else
9585 struct cl_optimization cur_opts;
9586 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9588 /* Save current options. */
9589 cl_optimization_save (&cur_opts, &global_options);
9591 /* If we previously had some optimization options, use them as the
9592 default. */
9593 if (old_opts)
9594 cl_optimization_restore (&global_options,
9595 TREE_OPTIMIZATION (old_opts));
9597 /* Parse options, and update the vector. */
9598 parse_optimize_options (args, true);
9599 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9600 = build_optimization_node (&global_options);
9602 /* Restore current options. */
9603 cl_optimization_restore (&global_options, &cur_opts);
9606 return NULL_TREE;
9609 /* Handle a "no_split_stack" attribute. */
9611 static tree
9612 handle_no_split_stack_attribute (tree *node, tree name,
9613 tree ARG_UNUSED (args),
9614 int ARG_UNUSED (flags),
9615 bool *no_add_attrs)
9617 tree decl = *node;
9619 if (TREE_CODE (decl) != FUNCTION_DECL)
9621 error_at (DECL_SOURCE_LOCATION (decl),
9622 "%qE attribute applies only to functions", name);
9623 *no_add_attrs = true;
9625 else if (DECL_INITIAL (decl))
9627 error_at (DECL_SOURCE_LOCATION (decl),
9628 "can%'t set %qE attribute after definition", name);
9629 *no_add_attrs = true;
9632 return NULL_TREE;
9635 /* Handle a "returns_nonnull" attribute; arguments as in
9636 struct attribute_spec.handler. */
9638 static tree
9639 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9640 bool *no_add_attrs)
9642 // Even without a prototype we still have a return type we can check.
9643 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9645 error ("returns_nonnull attribute on a function not returning a pointer");
9646 *no_add_attrs = true;
9648 return NULL_TREE;
9651 /* Handle a "designated_init" attribute; arguments as in
9652 struct attribute_spec.handler. */
9654 static tree
9655 handle_designated_init_attribute (tree *node, tree name, tree, int,
9656 bool *no_add_attrs)
9658 if (TREE_CODE (*node) != RECORD_TYPE)
9660 error ("%qE attribute is only valid on %<struct%> type", name);
9661 *no_add_attrs = true;
9663 return NULL_TREE;
9667 /* Check for valid arguments being passed to a function with FNTYPE.
9668 There are NARGS arguments in the array ARGARRAY. */
9669 void
9670 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9672 /* Check for null being passed in a pointer argument that must be
9673 non-null. We also need to do this if format checking is enabled. */
9675 if (warn_nonnull)
9676 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9678 /* Check for errors in format strings. */
9680 if (warn_format || warn_suggest_attribute_format)
9681 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9683 if (warn_format)
9684 check_function_sentinel (fntype, nargs, argarray);
9687 /* Generic argument checking recursion routine. PARAM is the argument to
9688 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9689 once the argument is resolved. CTX is context for the callback. */
9690 void
9691 check_function_arguments_recurse (void (*callback)
9692 (void *, tree, unsigned HOST_WIDE_INT),
9693 void *ctx, tree param,
9694 unsigned HOST_WIDE_INT param_num)
9696 if (CONVERT_EXPR_P (param)
9697 && (TYPE_PRECISION (TREE_TYPE (param))
9698 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9700 /* Strip coercion. */
9701 check_function_arguments_recurse (callback, ctx,
9702 TREE_OPERAND (param, 0), param_num);
9703 return;
9706 if (TREE_CODE (param) == CALL_EXPR)
9708 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9709 tree attrs;
9710 bool found_format_arg = false;
9712 /* See if this is a call to a known internationalization function
9713 that modifies a format arg. Such a function may have multiple
9714 format_arg attributes (for example, ngettext). */
9716 for (attrs = TYPE_ATTRIBUTES (type);
9717 attrs;
9718 attrs = TREE_CHAIN (attrs))
9719 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9721 tree inner_arg;
9722 tree format_num_expr;
9723 int format_num;
9724 int i;
9725 call_expr_arg_iterator iter;
9727 /* Extract the argument number, which was previously checked
9728 to be valid. */
9729 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9731 format_num = tree_to_uhwi (format_num_expr);
9733 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9734 inner_arg != 0;
9735 inner_arg = next_call_expr_arg (&iter), i++)
9736 if (i == format_num)
9738 check_function_arguments_recurse (callback, ctx,
9739 inner_arg, param_num);
9740 found_format_arg = true;
9741 break;
9745 /* If we found a format_arg attribute and did a recursive check,
9746 we are done with checking this argument. Otherwise, we continue
9747 and this will be considered a non-literal. */
9748 if (found_format_arg)
9749 return;
9752 if (TREE_CODE (param) == COND_EXPR)
9754 /* Check both halves of the conditional expression. */
9755 check_function_arguments_recurse (callback, ctx,
9756 TREE_OPERAND (param, 1), param_num);
9757 check_function_arguments_recurse (callback, ctx,
9758 TREE_OPERAND (param, 2), param_num);
9759 return;
9762 (*callback) (ctx, param, param_num);
9765 /* Checks for a builtin function FNDECL that the number of arguments
9766 NARGS against the required number REQUIRED and issues an error if
9767 there is a mismatch. Returns true if the number of arguments is
9768 correct, otherwise false. */
9770 static bool
9771 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9773 if (nargs < required)
9775 error_at (input_location,
9776 "not enough arguments to function %qE", fndecl);
9777 return false;
9779 else if (nargs > required)
9781 error_at (input_location,
9782 "too many arguments to function %qE", fndecl);
9783 return false;
9785 return true;
9788 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9789 Returns false if there was an error, otherwise true. */
9791 bool
9792 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9794 if (!DECL_BUILT_IN (fndecl)
9795 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9796 return true;
9798 switch (DECL_FUNCTION_CODE (fndecl))
9800 case BUILT_IN_CONSTANT_P:
9801 return builtin_function_validate_nargs (fndecl, nargs, 1);
9803 case BUILT_IN_ISFINITE:
9804 case BUILT_IN_ISINF:
9805 case BUILT_IN_ISINF_SIGN:
9806 case BUILT_IN_ISNAN:
9807 case BUILT_IN_ISNORMAL:
9808 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9810 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9812 error ("non-floating-point argument in call to "
9813 "function %qE", fndecl);
9814 return false;
9816 return true;
9818 return false;
9820 case BUILT_IN_ISGREATER:
9821 case BUILT_IN_ISGREATEREQUAL:
9822 case BUILT_IN_ISLESS:
9823 case BUILT_IN_ISLESSEQUAL:
9824 case BUILT_IN_ISLESSGREATER:
9825 case BUILT_IN_ISUNORDERED:
9826 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9828 enum tree_code code0, code1;
9829 code0 = TREE_CODE (TREE_TYPE (args[0]));
9830 code1 = TREE_CODE (TREE_TYPE (args[1]));
9831 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9832 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9833 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9835 error ("non-floating-point arguments in call to "
9836 "function %qE", fndecl);
9837 return false;
9839 return true;
9841 return false;
9843 case BUILT_IN_FPCLASSIFY:
9844 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9846 unsigned i;
9848 for (i=0; i<5; i++)
9849 if (TREE_CODE (args[i]) != INTEGER_CST)
9851 error ("non-const integer argument %u in call to function %qE",
9852 i+1, fndecl);
9853 return false;
9856 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9858 error ("non-floating-point argument in call to function %qE",
9859 fndecl);
9860 return false;
9862 return true;
9864 return false;
9866 case BUILT_IN_ASSUME_ALIGNED:
9867 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9869 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9871 error ("non-integer argument 3 in call to function %qE", fndecl);
9872 return false;
9874 return true;
9876 return false;
9878 case BUILT_IN_ADD_OVERFLOW:
9879 case BUILT_IN_SUB_OVERFLOW:
9880 case BUILT_IN_MUL_OVERFLOW:
9881 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9883 unsigned i;
9884 for (i = 0; i < 2; i++)
9885 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9887 error ("argument %u in call to function %qE does not have "
9888 "integral type", i + 1, fndecl);
9889 return false;
9891 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9892 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9894 error ("argument 3 in call to function %qE does not have "
9895 "pointer to integer type", fndecl);
9896 return false;
9898 return true;
9900 return false;
9902 default:
9903 return true;
9907 /* Function to help qsort sort FIELD_DECLs by name order. */
9910 field_decl_cmp (const void *x_p, const void *y_p)
9912 const tree *const x = (const tree *const) x_p;
9913 const tree *const y = (const tree *const) y_p;
9915 if (DECL_NAME (*x) == DECL_NAME (*y))
9916 /* A nontype is "greater" than a type. */
9917 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9918 if (DECL_NAME (*x) == NULL_TREE)
9919 return -1;
9920 if (DECL_NAME (*y) == NULL_TREE)
9921 return 1;
9922 if (DECL_NAME (*x) < DECL_NAME (*y))
9923 return -1;
9924 return 1;
9927 static struct {
9928 gt_pointer_operator new_value;
9929 void *cookie;
9930 } resort_data;
9932 /* This routine compares two fields like field_decl_cmp but using the
9933 pointer operator in resort_data. */
9935 static int
9936 resort_field_decl_cmp (const void *x_p, const void *y_p)
9938 const tree *const x = (const tree *const) x_p;
9939 const tree *const y = (const tree *const) y_p;
9941 if (DECL_NAME (*x) == DECL_NAME (*y))
9942 /* A nontype is "greater" than a type. */
9943 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9944 if (DECL_NAME (*x) == NULL_TREE)
9945 return -1;
9946 if (DECL_NAME (*y) == NULL_TREE)
9947 return 1;
9949 tree d1 = DECL_NAME (*x);
9950 tree d2 = DECL_NAME (*y);
9951 resort_data.new_value (&d1, resort_data.cookie);
9952 resort_data.new_value (&d2, resort_data.cookie);
9953 if (d1 < d2)
9954 return -1;
9956 return 1;
9959 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9961 void
9962 resort_sorted_fields (void *obj,
9963 void * ARG_UNUSED (orig_obj),
9964 gt_pointer_operator new_value,
9965 void *cookie)
9967 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9968 resort_data.new_value = new_value;
9969 resort_data.cookie = cookie;
9970 qsort (&sf->elts[0], sf->len, sizeof (tree),
9971 resort_field_decl_cmp);
9974 /* Subroutine of c_parse_error.
9975 Return the result of concatenating LHS and RHS. RHS is really
9976 a string literal, its first character is indicated by RHS_START and
9977 RHS_SIZE is its length (including the terminating NUL character).
9979 The caller is responsible for deleting the returned pointer. */
9981 static char *
9982 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9984 const int lhs_size = strlen (lhs);
9985 char *result = XNEWVEC (char, lhs_size + rhs_size);
9986 strncpy (result, lhs, lhs_size);
9987 strncpy (result + lhs_size, rhs_start, rhs_size);
9988 return result;
9991 /* Issue the error given by GMSGID, indicating that it occurred before
9992 TOKEN, which had the associated VALUE. */
9994 void
9995 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9996 tree value, unsigned char token_flags)
9998 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10000 char *message = NULL;
10002 if (token_type == CPP_EOF)
10003 message = catenate_messages (gmsgid, " at end of input");
10004 else if (token_type == CPP_CHAR
10005 || token_type == CPP_WCHAR
10006 || token_type == CPP_CHAR16
10007 || token_type == CPP_CHAR32)
10009 unsigned int val = TREE_INT_CST_LOW (value);
10010 const char *prefix;
10012 switch (token_type)
10014 default:
10015 prefix = "";
10016 break;
10017 case CPP_WCHAR:
10018 prefix = "L";
10019 break;
10020 case CPP_CHAR16:
10021 prefix = "u";
10022 break;
10023 case CPP_CHAR32:
10024 prefix = "U";
10025 break;
10028 if (val <= UCHAR_MAX && ISGRAPH (val))
10029 message = catenate_messages (gmsgid, " before %s'%c'");
10030 else
10031 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10033 error (message, prefix, val);
10034 free (message);
10035 message = NULL;
10037 else if (token_type == CPP_CHAR_USERDEF
10038 || token_type == CPP_WCHAR_USERDEF
10039 || token_type == CPP_CHAR16_USERDEF
10040 || token_type == CPP_CHAR32_USERDEF)
10041 message = catenate_messages (gmsgid,
10042 " before user-defined character literal");
10043 else if (token_type == CPP_STRING_USERDEF
10044 || token_type == CPP_WSTRING_USERDEF
10045 || token_type == CPP_STRING16_USERDEF
10046 || token_type == CPP_STRING32_USERDEF
10047 || token_type == CPP_UTF8STRING_USERDEF)
10048 message = catenate_messages (gmsgid, " before user-defined string literal");
10049 else if (token_type == CPP_STRING
10050 || token_type == CPP_WSTRING
10051 || token_type == CPP_STRING16
10052 || token_type == CPP_STRING32
10053 || token_type == CPP_UTF8STRING)
10054 message = catenate_messages (gmsgid, " before string constant");
10055 else if (token_type == CPP_NUMBER)
10056 message = catenate_messages (gmsgid, " before numeric constant");
10057 else if (token_type == CPP_NAME)
10059 message = catenate_messages (gmsgid, " before %qE");
10060 error (message, value);
10061 free (message);
10062 message = NULL;
10064 else if (token_type == CPP_PRAGMA)
10065 message = catenate_messages (gmsgid, " before %<#pragma%>");
10066 else if (token_type == CPP_PRAGMA_EOL)
10067 message = catenate_messages (gmsgid, " before end of line");
10068 else if (token_type == CPP_DECLTYPE)
10069 message = catenate_messages (gmsgid, " before %<decltype%>");
10070 else if (token_type < N_TTYPES)
10072 message = catenate_messages (gmsgid, " before %qs token");
10073 error (message, cpp_type2name (token_type, token_flags));
10074 free (message);
10075 message = NULL;
10077 else
10078 error (gmsgid);
10080 if (message)
10082 error (message);
10083 free (message);
10085 #undef catenate_messages
10088 /* Return the gcc option code associated with the reason for a cpp
10089 message, or 0 if none. */
10091 static int
10092 c_option_controlling_cpp_error (int reason)
10094 const struct cpp_reason_option_codes_t *entry;
10096 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10098 if (entry->reason == reason)
10099 return entry->option_code;
10101 return 0;
10104 /* Callback from cpp_error for PFILE to print diagnostics from the
10105 preprocessor. The diagnostic is of type LEVEL, with REASON set
10106 to the reason code if LEVEL is represents a warning, at location
10107 LOCATION unless this is after lexing and the compiler's location
10108 should be used instead, with column number possibly overridden by
10109 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10110 the arguments. Returns true if a diagnostic was emitted, false
10111 otherwise. */
10113 bool
10114 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10115 location_t location, unsigned int column_override,
10116 const char *msg, va_list *ap)
10118 diagnostic_info diagnostic;
10119 diagnostic_t dlevel;
10120 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10121 bool ret;
10123 switch (level)
10125 case CPP_DL_WARNING_SYSHDR:
10126 if (flag_no_output)
10127 return false;
10128 global_dc->dc_warn_system_headers = 1;
10129 /* Fall through. */
10130 case CPP_DL_WARNING:
10131 if (flag_no_output)
10132 return false;
10133 dlevel = DK_WARNING;
10134 break;
10135 case CPP_DL_PEDWARN:
10136 if (flag_no_output && !flag_pedantic_errors)
10137 return false;
10138 dlevel = DK_PEDWARN;
10139 break;
10140 case CPP_DL_ERROR:
10141 dlevel = DK_ERROR;
10142 break;
10143 case CPP_DL_ICE:
10144 dlevel = DK_ICE;
10145 break;
10146 case CPP_DL_NOTE:
10147 dlevel = DK_NOTE;
10148 break;
10149 case CPP_DL_FATAL:
10150 dlevel = DK_FATAL;
10151 break;
10152 default:
10153 gcc_unreachable ();
10155 if (done_lexing)
10156 location = input_location;
10157 diagnostic_set_info_translated (&diagnostic, msg, ap,
10158 location, dlevel);
10159 if (column_override)
10160 diagnostic_override_column (&diagnostic, column_override);
10161 diagnostic_override_option_index (&diagnostic,
10162 c_option_controlling_cpp_error (reason));
10163 ret = report_diagnostic (&diagnostic);
10164 if (level == CPP_DL_WARNING_SYSHDR)
10165 global_dc->dc_warn_system_headers = save_warn_system_headers;
10166 return ret;
10169 /* Convert a character from the host to the target execution character
10170 set. cpplib handles this, mostly. */
10172 HOST_WIDE_INT
10173 c_common_to_target_charset (HOST_WIDE_INT c)
10175 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10176 zero-extended under -fno-signed-char. cpplib insists that characters
10177 and character constants are always unsigned. Hence we must convert
10178 back and forth. */
10179 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10181 uc = cpp_host_to_exec_charset (parse_in, uc);
10183 if (flag_signed_char)
10184 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10185 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10186 else
10187 return uc;
10190 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10191 references with an INDIRECT_REF of a constant at the bottom; much like the
10192 traditional rendering of offsetof as a macro. Return the folded result. */
10194 tree
10195 fold_offsetof_1 (tree expr)
10197 tree base, off, t;
10199 switch (TREE_CODE (expr))
10201 case ERROR_MARK:
10202 return expr;
10204 case VAR_DECL:
10205 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10206 return error_mark_node;
10208 case CALL_EXPR:
10209 case TARGET_EXPR:
10210 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10211 return error_mark_node;
10213 case NOP_EXPR:
10214 case INDIRECT_REF:
10215 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10217 error ("cannot apply %<offsetof%> to a non constant address");
10218 return error_mark_node;
10220 return TREE_OPERAND (expr, 0);
10222 case COMPONENT_REF:
10223 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10224 if (base == error_mark_node)
10225 return base;
10227 t = TREE_OPERAND (expr, 1);
10228 if (DECL_C_BIT_FIELD (t))
10230 error ("attempt to take address of bit-field structure "
10231 "member %qD", t);
10232 return error_mark_node;
10234 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10235 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10236 / BITS_PER_UNIT));
10237 break;
10239 case ARRAY_REF:
10240 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10241 if (base == error_mark_node)
10242 return base;
10244 t = TREE_OPERAND (expr, 1);
10246 /* Check if the offset goes beyond the upper bound of the array. */
10247 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10249 tree upbound = array_ref_up_bound (expr);
10250 if (upbound != NULL_TREE
10251 && TREE_CODE (upbound) == INTEGER_CST
10252 && !tree_int_cst_equal (upbound,
10253 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10255 upbound = size_binop (PLUS_EXPR, upbound,
10256 build_int_cst (TREE_TYPE (upbound), 1));
10257 if (tree_int_cst_lt (upbound, t))
10259 tree v;
10261 for (v = TREE_OPERAND (expr, 0);
10262 TREE_CODE (v) == COMPONENT_REF;
10263 v = TREE_OPERAND (v, 0))
10264 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10265 == RECORD_TYPE)
10267 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10268 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10269 if (TREE_CODE (fld_chain) == FIELD_DECL)
10270 break;
10272 if (fld_chain)
10273 break;
10275 /* Don't warn if the array might be considered a poor
10276 man's flexible array member with a very permissive
10277 definition thereof. */
10278 if (TREE_CODE (v) == ARRAY_REF
10279 || TREE_CODE (v) == COMPONENT_REF)
10280 warning (OPT_Warray_bounds,
10281 "index %E denotes an offset "
10282 "greater than size of %qT",
10283 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10288 t = convert (sizetype, t);
10289 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10290 break;
10292 case COMPOUND_EXPR:
10293 /* Handle static members of volatile structs. */
10294 t = TREE_OPERAND (expr, 1);
10295 gcc_assert (TREE_CODE (t) == VAR_DECL);
10296 return fold_offsetof_1 (t);
10298 default:
10299 gcc_unreachable ();
10302 return fold_build_pointer_plus (base, off);
10305 /* Likewise, but convert it to the return type of offsetof. */
10307 tree
10308 fold_offsetof (tree expr)
10310 return convert (size_type_node, fold_offsetof_1 (expr));
10313 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10314 expression, because B will always be true. */
10316 void
10317 warn_for_omitted_condop (location_t location, tree cond)
10319 if (truth_value_p (TREE_CODE (cond)))
10320 warning_at (location, OPT_Wparentheses,
10321 "the omitted middle operand in ?: will always be %<true%>, "
10322 "suggest explicit middle operand");
10325 /* Give an error for storing into ARG, which is 'const'. USE indicates
10326 how ARG was being used. */
10328 void
10329 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10331 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10332 || use == lv_asm);
10333 /* Using this macro rather than (for example) arrays of messages
10334 ensures that all the format strings are checked at compile
10335 time. */
10336 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10337 : (use == lv_increment ? (I) \
10338 : (use == lv_decrement ? (D) : (AS))))
10339 if (TREE_CODE (arg) == COMPONENT_REF)
10341 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10342 error_at (loc, READONLY_MSG (G_("assignment of member "
10343 "%qD in read-only object"),
10344 G_("increment of member "
10345 "%qD in read-only object"),
10346 G_("decrement of member "
10347 "%qD in read-only object"),
10348 G_("member %qD in read-only object "
10349 "used as %<asm%> output")),
10350 TREE_OPERAND (arg, 1));
10351 else
10352 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10353 G_("increment of read-only member %qD"),
10354 G_("decrement of read-only member %qD"),
10355 G_("read-only member %qD used as %<asm%> output")),
10356 TREE_OPERAND (arg, 1));
10358 else if (TREE_CODE (arg) == VAR_DECL)
10359 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10360 G_("increment of read-only variable %qD"),
10361 G_("decrement of read-only variable %qD"),
10362 G_("read-only variable %qD used as %<asm%> output")),
10363 arg);
10364 else if (TREE_CODE (arg) == PARM_DECL)
10365 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10366 G_("increment of read-only parameter %qD"),
10367 G_("decrement of read-only parameter %qD"),
10368 G_("read-only parameter %qD use as %<asm%> output")),
10369 arg);
10370 else if (TREE_CODE (arg) == RESULT_DECL)
10372 gcc_assert (c_dialect_cxx ());
10373 error_at (loc, READONLY_MSG (G_("assignment of "
10374 "read-only named return value %qD"),
10375 G_("increment of "
10376 "read-only named return value %qD"),
10377 G_("decrement of "
10378 "read-only named return value %qD"),
10379 G_("read-only named return value %qD "
10380 "used as %<asm%>output")),
10381 arg);
10383 else if (TREE_CODE (arg) == FUNCTION_DECL)
10384 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10385 G_("increment of function %qD"),
10386 G_("decrement of function %qD"),
10387 G_("function %qD used as %<asm%> output")),
10388 arg);
10389 else
10390 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10391 G_("increment of read-only location %qE"),
10392 G_("decrement of read-only location %qE"),
10393 G_("read-only location %qE used as %<asm%> output")),
10394 arg);
10397 /* Print an error message for an invalid lvalue. USE says
10398 how the lvalue is being used and so selects the error message. LOC
10399 is the location for the error. */
10401 void
10402 lvalue_error (location_t loc, enum lvalue_use use)
10404 switch (use)
10406 case lv_assign:
10407 error_at (loc, "lvalue required as left operand of assignment");
10408 break;
10409 case lv_increment:
10410 error_at (loc, "lvalue required as increment operand");
10411 break;
10412 case lv_decrement:
10413 error_at (loc, "lvalue required as decrement operand");
10414 break;
10415 case lv_addressof:
10416 error_at (loc, "lvalue required as unary %<&%> operand");
10417 break;
10418 case lv_asm:
10419 error_at (loc, "lvalue required in asm statement");
10420 break;
10421 default:
10422 gcc_unreachable ();
10426 /* Print an error message for an invalid indirection of type TYPE.
10427 ERRSTRING is the name of the operator for the indirection. */
10429 void
10430 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10432 switch (errstring)
10434 case RO_NULL:
10435 gcc_assert (c_dialect_cxx ());
10436 error_at (loc, "invalid type argument (have %qT)", type);
10437 break;
10438 case RO_ARRAY_INDEXING:
10439 error_at (loc,
10440 "invalid type argument of array indexing (have %qT)",
10441 type);
10442 break;
10443 case RO_UNARY_STAR:
10444 error_at (loc,
10445 "invalid type argument of unary %<*%> (have %qT)",
10446 type);
10447 break;
10448 case RO_ARROW:
10449 error_at (loc,
10450 "invalid type argument of %<->%> (have %qT)",
10451 type);
10452 break;
10453 case RO_ARROW_STAR:
10454 error_at (loc,
10455 "invalid type argument of %<->*%> (have %qT)",
10456 type);
10457 break;
10458 case RO_IMPLICIT_CONVERSION:
10459 error_at (loc,
10460 "invalid type argument of implicit conversion (have %qT)",
10461 type);
10462 break;
10463 default:
10464 gcc_unreachable ();
10468 /* *PTYPE is an incomplete array. Complete it with a domain based on
10469 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10470 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10471 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10474 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10476 tree maxindex, type, main_type, elt, unqual_elt;
10477 int failure = 0, quals;
10478 hashval_t hashcode = 0;
10479 bool overflow_p = false;
10481 maxindex = size_zero_node;
10482 if (initial_value)
10484 if (TREE_CODE (initial_value) == STRING_CST)
10486 int eltsize
10487 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10488 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10490 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10492 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10494 if (vec_safe_is_empty (v))
10496 if (pedantic)
10497 failure = 3;
10498 maxindex = ssize_int (-1);
10500 else
10502 tree curindex;
10503 unsigned HOST_WIDE_INT cnt;
10504 constructor_elt *ce;
10505 bool fold_p = false;
10507 if ((*v)[0].index)
10508 maxindex = (*v)[0].index, fold_p = true;
10510 curindex = maxindex;
10512 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10514 bool curfold_p = false;
10515 if (ce->index)
10516 curindex = ce->index, curfold_p = true;
10517 else
10519 if (fold_p)
10521 /* Since we treat size types now as ordinary
10522 unsigned types, we need an explicit overflow
10523 check. */
10524 tree orig = curindex;
10525 curindex = fold_convert (sizetype, curindex);
10526 overflow_p |= tree_int_cst_lt (curindex, orig);
10528 curindex = size_binop (PLUS_EXPR, curindex,
10529 size_one_node);
10531 if (tree_int_cst_lt (maxindex, curindex))
10532 maxindex = curindex, fold_p = curfold_p;
10534 if (fold_p)
10536 tree orig = maxindex;
10537 maxindex = fold_convert (sizetype, maxindex);
10538 overflow_p |= tree_int_cst_lt (maxindex, orig);
10542 else
10544 /* Make an error message unless that happened already. */
10545 if (initial_value != error_mark_node)
10546 failure = 1;
10549 else
10551 failure = 2;
10552 if (!do_default)
10553 return failure;
10556 type = *ptype;
10557 /* Force an indefinite layout factor. */
10558 if (upc_shared_type_p (type))
10559 type = c_build_qualified_type_1 (type, TYPE_QUAL_UPC_SHARED,
10560 size_zero_node);
10561 elt = TREE_TYPE (type);
10562 quals = TYPE_QUALS (strip_array_types (elt));
10563 if (quals == 0)
10564 unqual_elt = elt;
10565 else
10566 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10568 /* Using build_distinct_type_copy and modifying things afterward instead
10569 of using build_array_type to create a new type preserves all of the
10570 TYPE_LANG_FLAG_? bits that the front end may have set. */
10571 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10572 TREE_TYPE (main_type) = unqual_elt;
10573 TYPE_DOMAIN (main_type)
10574 = build_range_type (TREE_TYPE (maxindex),
10575 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10576 layout_type (main_type);
10578 /* Make sure we have the canonical MAIN_TYPE. */
10579 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10580 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10581 hashcode);
10582 main_type = type_hash_canon (hashcode, main_type);
10584 /* Fix the canonical type. */
10585 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10586 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10587 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10588 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10589 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10590 != TYPE_DOMAIN (main_type)))
10591 TYPE_CANONICAL (main_type)
10592 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10593 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10594 else
10595 TYPE_CANONICAL (main_type) = main_type;
10597 if (quals == 0)
10598 type = main_type;
10599 else
10600 type = c_build_qualified_type (main_type, quals);
10602 if (COMPLETE_TYPE_P (type)
10603 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10604 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10606 error ("size of array is too large");
10607 /* If we proceed with the array type as it is, we'll eventually
10608 crash in tree_to_[su]hwi(). */
10609 type = error_mark_node;
10612 *ptype = type;
10613 return failure;
10616 /* Like c_mark_addressable but don't check register qualifier. */
10617 void
10618 c_common_mark_addressable_vec (tree t)
10620 while (handled_component_p (t))
10621 t = TREE_OPERAND (t, 0);
10622 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10623 return;
10624 TREE_ADDRESSABLE (t) = 1;
10629 /* Used to help initialize the builtin-types.def table. When a type of
10630 the correct size doesn't exist, use error_mark_node instead of NULL.
10631 The later results in segfaults even when a decl using the type doesn't
10632 get invoked. */
10634 tree
10635 builtin_type_for_size (int size, bool unsignedp)
10637 tree type = c_common_type_for_size (size, unsignedp);
10638 return type ? type : error_mark_node;
10641 /* A helper function for resolve_overloaded_builtin in resolving the
10642 overloaded __sync_ builtins. Returns a positive power of 2 if the
10643 first operand of PARAMS is a pointer to a supported data type.
10644 Returns 0 if an error is encountered. */
10646 static int
10647 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10649 tree type;
10650 int size;
10652 if (!params)
10654 error ("too few arguments to function %qE", function);
10655 return 0;
10658 type = TREE_TYPE ((*params)[0]);
10659 if (TREE_CODE (type) == ARRAY_TYPE)
10661 /* Force array-to-pointer decay for C++. */
10662 gcc_assert (c_dialect_cxx());
10663 (*params)[0] = default_conversion ((*params)[0]);
10664 type = TREE_TYPE ((*params)[0]);
10666 if (TREE_CODE (type) != POINTER_TYPE)
10667 goto incompatible;
10669 type = TREE_TYPE (type);
10670 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10671 goto incompatible;
10673 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10674 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10675 return size;
10677 incompatible:
10678 error ("incompatible type for argument %d of %qE", 1, function);
10679 return 0;
10682 /* A helper function for resolve_overloaded_builtin. Adds casts to
10683 PARAMS to make arguments match up with those of FUNCTION. Drops
10684 the variadic arguments at the end. Returns false if some error
10685 was encountered; true on success. */
10687 static bool
10688 sync_resolve_params (location_t loc, tree orig_function, tree function,
10689 vec<tree, va_gc> *params, bool orig_format)
10691 function_args_iterator iter;
10692 tree ptype;
10693 unsigned int parmnum;
10695 function_args_iter_init (&iter, TREE_TYPE (function));
10696 /* We've declared the implementation functions to use "volatile void *"
10697 as the pointer parameter, so we shouldn't get any complaints from the
10698 call to check_function_arguments what ever type the user used. */
10699 function_args_iter_next (&iter);
10700 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10701 ptype = TYPE_MAIN_VARIANT (ptype);
10703 /* For the rest of the values, we need to cast these to FTYPE, so that we
10704 don't get warnings for passing pointer types, etc. */
10705 parmnum = 0;
10706 while (1)
10708 tree val, arg_type;
10710 arg_type = function_args_iter_cond (&iter);
10711 /* XXX void_type_node belies the abstraction. */
10712 if (arg_type == void_type_node)
10713 break;
10715 ++parmnum;
10716 if (params->length () <= parmnum)
10718 error_at (loc, "too few arguments to function %qE", orig_function);
10719 return false;
10722 /* Only convert parameters if arg_type is unsigned integer type with
10723 new format sync routines, i.e. don't attempt to convert pointer
10724 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10725 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10726 kinds). */
10727 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10729 /* Ideally for the first conversion we'd use convert_for_assignment
10730 so that we get warnings for anything that doesn't match the pointer
10731 type. This isn't portable across the C and C++ front ends atm. */
10732 val = (*params)[parmnum];
10733 val = convert (ptype, val);
10734 val = convert (arg_type, val);
10735 (*params)[parmnum] = val;
10738 function_args_iter_next (&iter);
10741 /* __atomic routines are not variadic. */
10742 if (!orig_format && params->length () != parmnum + 1)
10744 error_at (loc, "too many arguments to function %qE", orig_function);
10745 return false;
10748 /* The definition of these primitives is variadic, with the remaining
10749 being "an optional list of variables protected by the memory barrier".
10750 No clue what that's supposed to mean, precisely, but we consider all
10751 call-clobbered variables to be protected so we're safe. */
10752 params->truncate (parmnum + 1);
10754 return true;
10757 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10758 RESULT to make it match the type of the first pointer argument in
10759 PARAMS. */
10761 static tree
10762 sync_resolve_return (tree first_param, tree result, bool orig_format)
10764 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10765 tree rtype = TREE_TYPE (result);
10766 ptype = TYPE_MAIN_VARIANT (ptype);
10768 /* New format doesn't require casting unless the types are the same size. */
10769 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10770 return convert (ptype, result);
10771 else
10772 return result;
10775 /* This function verifies the PARAMS to generic atomic FUNCTION.
10776 It returns the size if all the parameters are the same size, otherwise
10777 0 is returned if the parameters are invalid. */
10779 static int
10780 get_atomic_generic_size (location_t loc, tree function,
10781 vec<tree, va_gc> *params)
10783 unsigned int n_param;
10784 unsigned int n_model;
10785 unsigned int x;
10786 int size_0;
10787 tree type_0;
10789 /* Determine the parameter makeup. */
10790 switch (DECL_FUNCTION_CODE (function))
10792 case BUILT_IN_ATOMIC_EXCHANGE:
10793 n_param = 4;
10794 n_model = 1;
10795 break;
10796 case BUILT_IN_ATOMIC_LOAD:
10797 case BUILT_IN_ATOMIC_STORE:
10798 n_param = 3;
10799 n_model = 1;
10800 break;
10801 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10802 n_param = 6;
10803 n_model = 2;
10804 break;
10805 default:
10806 gcc_unreachable ();
10809 if (vec_safe_length (params) != n_param)
10811 error_at (loc, "incorrect number of arguments to function %qE", function);
10812 return 0;
10815 /* Get type of first parameter, and determine its size. */
10816 type_0 = TREE_TYPE ((*params)[0]);
10817 if (TREE_CODE (type_0) == ARRAY_TYPE)
10819 /* Force array-to-pointer decay for C++. */
10820 gcc_assert (c_dialect_cxx());
10821 (*params)[0] = default_conversion ((*params)[0]);
10822 type_0 = TREE_TYPE ((*params)[0]);
10824 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10826 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10827 function);
10828 return 0;
10831 /* Types must be compile time constant sizes. */
10832 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10834 error_at (loc,
10835 "argument 1 of %qE must be a pointer to a constant size type",
10836 function);
10837 return 0;
10840 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10842 /* Zero size objects are not allowed. */
10843 if (size_0 == 0)
10845 error_at (loc,
10846 "argument 1 of %qE must be a pointer to a nonzero size object",
10847 function);
10848 return 0;
10851 /* Check each other parameter is a pointer and the same size. */
10852 for (x = 0; x < n_param - n_model; x++)
10854 int size;
10855 tree type = TREE_TYPE ((*params)[x]);
10856 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10857 if (n_param == 6 && x == 3)
10858 continue;
10859 if (!POINTER_TYPE_P (type))
10861 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10862 function);
10863 return 0;
10865 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10866 size = type_size ? tree_to_uhwi (type_size) : 0;
10867 if (size != size_0)
10869 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10870 function);
10871 return 0;
10875 /* Check memory model parameters for validity. */
10876 for (x = n_param - n_model ; x < n_param; x++)
10878 tree p = (*params)[x];
10879 if (TREE_CODE (p) == INTEGER_CST)
10881 int i = tree_to_uhwi (p);
10882 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10884 warning_at (loc, OPT_Winvalid_memory_model,
10885 "invalid memory model argument %d of %qE", x + 1,
10886 function);
10889 else
10890 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10892 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10893 function);
10894 return 0;
10898 return size_0;
10902 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10903 at the beginning of the parameter list PARAMS representing the size of the
10904 objects. This is to match the library ABI requirement. LOC is the location
10905 of the function call.
10906 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10907 returned to allow the external call to be constructed. */
10909 static tree
10910 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10911 vec<tree, va_gc> *params)
10913 tree size_node;
10915 /* Insert a SIZE_T parameter as the first param. If there isn't
10916 enough space, allocate a new vector and recursively re-build with that. */
10917 if (!params->space (1))
10919 unsigned int z, len;
10920 vec<tree, va_gc> *v;
10921 tree f;
10923 len = params->length ();
10924 vec_alloc (v, len + 1);
10925 v->quick_push (build_int_cst (size_type_node, n));
10926 for (z = 0; z < len; z++)
10927 v->quick_push ((*params)[z]);
10928 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10929 vec_free (v);
10930 return f;
10933 /* Add the size parameter and leave as a function call for processing. */
10934 size_node = build_int_cst (size_type_node, n);
10935 params->quick_insert (0, size_node);
10936 return NULL_TREE;
10940 /* Return whether atomic operations for naturally aligned N-byte
10941 arguments are supported, whether inline or through libatomic. */
10942 static bool
10943 atomic_size_supported_p (int n)
10945 switch (n)
10947 case 1:
10948 case 2:
10949 case 4:
10950 case 8:
10951 return true;
10953 case 16:
10954 return targetm.scalar_mode_supported_p (TImode);
10956 default:
10957 return false;
10961 /* This will process an __atomic_exchange function call, determine whether it
10962 needs to be mapped to the _N variation, or turned into a library call.
10963 LOC is the location of the builtin call.
10964 FUNCTION is the DECL that has been invoked;
10965 PARAMS is the argument list for the call. The return value is non-null
10966 TRUE is returned if it is translated into the proper format for a call to the
10967 external library, and NEW_RETURN is set the tree for that function.
10968 FALSE is returned if processing for the _N variation is required, and
10969 NEW_RETURN is set to the the return value the result is copied into. */
10970 static bool
10971 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10972 vec<tree, va_gc> *params, tree *new_return)
10974 tree p0, p1, p2, p3;
10975 tree I_type, I_type_ptr;
10976 int n = get_atomic_generic_size (loc, function, params);
10978 /* Size of 0 is an error condition. */
10979 if (n == 0)
10981 *new_return = error_mark_node;
10982 return true;
10985 /* If not a lock-free size, change to the library generic format. */
10986 if (!atomic_size_supported_p (n))
10988 *new_return = add_atomic_size_parameter (n, loc, function, params);
10989 return true;
10992 /* Otherwise there is a lockfree match, transform the call from:
10993 void fn(T* mem, T* desired, T* return, model)
10994 into
10995 *return = (T) (fn (In* mem, (In) *desired, model)) */
10997 p0 = (*params)[0];
10998 p1 = (*params)[1];
10999 p2 = (*params)[2];
11000 p3 = (*params)[3];
11002 /* Create pointer to appropriate size. */
11003 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11004 I_type_ptr = build_pointer_type (I_type);
11006 /* Convert object pointer to required type. */
11007 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11008 (*params)[0] = p0;
11009 /* Convert new value to required type, and dereference it. */
11010 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11011 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11012 (*params)[1] = p1;
11014 /* Move memory model to the 3rd position, and end param list. */
11015 (*params)[2] = p3;
11016 params->truncate (3);
11018 /* Convert return pointer and dereference it for later assignment. */
11019 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11021 return false;
11025 /* This will process an __atomic_compare_exchange function call, determine
11026 whether it needs to be mapped to the _N variation, or turned into a lib call.
11027 LOC is the location of the builtin call.
11028 FUNCTION is the DECL that has been invoked;
11029 PARAMS is the argument list for the call. The return value is non-null
11030 TRUE is returned if it is translated into the proper format for a call to the
11031 external library, and NEW_RETURN is set the tree for that function.
11032 FALSE is returned if processing for the _N variation is required. */
11034 static bool
11035 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11036 vec<tree, va_gc> *params,
11037 tree *new_return)
11039 tree p0, p1, p2;
11040 tree I_type, I_type_ptr;
11041 int n = get_atomic_generic_size (loc, function, params);
11043 /* Size of 0 is an error condition. */
11044 if (n == 0)
11046 *new_return = error_mark_node;
11047 return true;
11050 /* If not a lock-free size, change to the library generic format. */
11051 if (!atomic_size_supported_p (n))
11053 /* The library generic format does not have the weak parameter, so
11054 remove it from the param list. Since a parameter has been removed,
11055 we can be sure that there is room for the SIZE_T parameter, meaning
11056 there will not be a recursive rebuilding of the parameter list, so
11057 there is no danger this will be done twice. */
11058 if (n > 0)
11060 (*params)[3] = (*params)[4];
11061 (*params)[4] = (*params)[5];
11062 params->truncate (5);
11064 *new_return = add_atomic_size_parameter (n, loc, function, params);
11065 return true;
11068 /* Otherwise, there is a match, so the call needs to be transformed from:
11069 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11070 into
11071 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11073 p0 = (*params)[0];
11074 p1 = (*params)[1];
11075 p2 = (*params)[2];
11077 /* Create pointer to appropriate size. */
11078 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11079 I_type_ptr = build_pointer_type (I_type);
11081 /* Convert object pointer to required type. */
11082 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11083 (*params)[0] = p0;
11085 /* Convert expected pointer to required type. */
11086 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11087 (*params)[1] = p1;
11089 /* Convert desired value to required type, and dereference it. */
11090 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11091 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11092 (*params)[2] = p2;
11094 /* The rest of the parameters are fine. NULL means no special return value
11095 processing.*/
11096 *new_return = NULL;
11097 return false;
11101 /* This will process an __atomic_load function call, determine whether it
11102 needs to be mapped to the _N variation, or turned into a library call.
11103 LOC is the location of the builtin call.
11104 FUNCTION is the DECL that has been invoked;
11105 PARAMS is the argument list for the call. The return value is non-null
11106 TRUE is returned if it is translated into the proper format for a call to the
11107 external library, and NEW_RETURN is set the tree for that function.
11108 FALSE is returned if processing for the _N variation is required, and
11109 NEW_RETURN is set to the the return value the result is copied into. */
11111 static bool
11112 resolve_overloaded_atomic_load (location_t loc, tree function,
11113 vec<tree, va_gc> *params, tree *new_return)
11115 tree p0, p1, p2;
11116 tree I_type, I_type_ptr;
11117 int n = get_atomic_generic_size (loc, function, params);
11119 /* Size of 0 is an error condition. */
11120 if (n == 0)
11122 *new_return = error_mark_node;
11123 return true;
11126 /* If not a lock-free size, change to the library generic format. */
11127 if (!atomic_size_supported_p (n))
11129 *new_return = add_atomic_size_parameter (n, loc, function, params);
11130 return true;
11133 /* Otherwise, there is a match, so the call needs to be transformed from:
11134 void fn(T* mem, T* return, model)
11135 into
11136 *return = (T) (fn ((In *) mem, model)) */
11138 p0 = (*params)[0];
11139 p1 = (*params)[1];
11140 p2 = (*params)[2];
11142 /* Create pointer to appropriate size. */
11143 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11144 I_type_ptr = build_pointer_type (I_type);
11146 /* Convert object pointer to required type. */
11147 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11148 (*params)[0] = p0;
11150 /* Move memory model to the 2nd position, and end param list. */
11151 (*params)[1] = p2;
11152 params->truncate (2);
11154 /* Convert return pointer and dereference it for later assignment. */
11155 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11157 return false;
11161 /* This will process an __atomic_store function call, determine whether it
11162 needs to be mapped to the _N variation, or turned into a library call.
11163 LOC is the location of the builtin call.
11164 FUNCTION is the DECL that has been invoked;
11165 PARAMS is the argument list for the call. The return value is non-null
11166 TRUE is returned if it is translated into the proper format for a call to the
11167 external library, and NEW_RETURN is set the tree for that function.
11168 FALSE is returned if processing for the _N variation is required, and
11169 NEW_RETURN is set to the the return value the result is copied into. */
11171 static bool
11172 resolve_overloaded_atomic_store (location_t loc, tree function,
11173 vec<tree, va_gc> *params, tree *new_return)
11175 tree p0, p1;
11176 tree I_type, I_type_ptr;
11177 int n = get_atomic_generic_size (loc, function, params);
11179 /* Size of 0 is an error condition. */
11180 if (n == 0)
11182 *new_return = error_mark_node;
11183 return true;
11186 /* If not a lock-free size, change to the library generic format. */
11187 if (!atomic_size_supported_p (n))
11189 *new_return = add_atomic_size_parameter (n, loc, function, params);
11190 return true;
11193 /* Otherwise, there is a match, so the call needs to be transformed from:
11194 void fn(T* mem, T* value, model)
11195 into
11196 fn ((In *) mem, (In) *value, model) */
11198 p0 = (*params)[0];
11199 p1 = (*params)[1];
11201 /* Create pointer to appropriate size. */
11202 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11203 I_type_ptr = build_pointer_type (I_type);
11205 /* Convert object pointer to required type. */
11206 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11207 (*params)[0] = p0;
11209 /* Convert new value to required type, and dereference it. */
11210 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11211 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11212 (*params)[1] = p1;
11214 /* The memory model is in the right spot already. Return is void. */
11215 *new_return = NULL_TREE;
11217 return false;
11221 /* Some builtin functions are placeholders for other expressions. This
11222 function should be called immediately after parsing the call expression
11223 before surrounding code has committed to the type of the expression.
11225 LOC is the location of the builtin call.
11227 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11228 PARAMS is the argument list for the call. The return value is non-null
11229 when expansion is complete, and null if normal processing should
11230 continue. */
11232 tree
11233 resolve_overloaded_builtin (location_t loc, tree function,
11234 vec<tree, va_gc> *params)
11236 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11237 bool orig_format = true;
11238 tree new_return = NULL_TREE;
11240 switch (DECL_BUILT_IN_CLASS (function))
11242 case BUILT_IN_NORMAL:
11243 break;
11244 case BUILT_IN_MD:
11245 if (targetm.resolve_overloaded_builtin)
11246 return targetm.resolve_overloaded_builtin (loc, function, params);
11247 else
11248 return NULL_TREE;
11249 default:
11250 return NULL_TREE;
11253 /* Handle BUILT_IN_NORMAL here. */
11254 switch (orig_code)
11256 case BUILT_IN_ATOMIC_EXCHANGE:
11257 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11258 case BUILT_IN_ATOMIC_LOAD:
11259 case BUILT_IN_ATOMIC_STORE:
11261 /* Handle these 4 together so that they can fall through to the next
11262 case if the call is transformed to an _N variant. */
11263 switch (orig_code)
11265 case BUILT_IN_ATOMIC_EXCHANGE:
11267 if (resolve_overloaded_atomic_exchange (loc, function, params,
11268 &new_return))
11269 return new_return;
11270 /* Change to the _N variant. */
11271 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11272 break;
11275 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11277 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11278 params,
11279 &new_return))
11280 return new_return;
11281 /* Change to the _N variant. */
11282 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11283 break;
11285 case BUILT_IN_ATOMIC_LOAD:
11287 if (resolve_overloaded_atomic_load (loc, function, params,
11288 &new_return))
11289 return new_return;
11290 /* Change to the _N variant. */
11291 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11292 break;
11294 case BUILT_IN_ATOMIC_STORE:
11296 if (resolve_overloaded_atomic_store (loc, function, params,
11297 &new_return))
11298 return new_return;
11299 /* Change to the _N variant. */
11300 orig_code = BUILT_IN_ATOMIC_STORE_N;
11301 break;
11303 default:
11304 gcc_unreachable ();
11306 /* Fallthrough to the normal processing. */
11308 case BUILT_IN_ATOMIC_EXCHANGE_N:
11309 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11310 case BUILT_IN_ATOMIC_LOAD_N:
11311 case BUILT_IN_ATOMIC_STORE_N:
11312 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11313 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11314 case BUILT_IN_ATOMIC_AND_FETCH_N:
11315 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11316 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11317 case BUILT_IN_ATOMIC_OR_FETCH_N:
11318 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11319 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11320 case BUILT_IN_ATOMIC_FETCH_AND_N:
11321 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11322 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11323 case BUILT_IN_ATOMIC_FETCH_OR_N:
11325 orig_format = false;
11326 /* Fallthru for parameter processing. */
11328 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11329 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11330 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11331 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11332 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11333 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11334 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11335 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11336 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11337 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11338 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11339 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11340 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11341 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11342 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11343 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11345 int n = sync_resolve_size (function, params);
11346 tree new_function, first_param, result;
11347 enum built_in_function fncode;
11349 if (n == 0)
11350 return error_mark_node;
11352 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11353 new_function = builtin_decl_explicit (fncode);
11354 if (!sync_resolve_params (loc, function, new_function, params,
11355 orig_format))
11356 return error_mark_node;
11358 first_param = (*params)[0];
11359 result = build_function_call_vec (loc, vNULL, new_function, params,
11360 NULL);
11361 if (result == error_mark_node)
11362 return result;
11363 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11364 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11365 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11366 result = sync_resolve_return (first_param, result, orig_format);
11368 /* If new_return is set, assign function to that expr and cast the
11369 result to void since the generic interface returned void. */
11370 if (new_return)
11372 /* Cast function result from I{1,2,4,8,16} to the required type. */
11373 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11374 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11375 result);
11376 TREE_SIDE_EFFECTS (result) = 1;
11377 protected_set_expr_location (result, loc);
11378 result = convert (void_type_node, result);
11380 return result;
11383 default:
11384 return NULL_TREE;
11388 /* vector_types_compatible_elements_p is used in type checks of vectors
11389 values used as operands of binary operators. Where it returns true, and
11390 the other checks of the caller succeed (being vector types in he first
11391 place, and matching number of elements), we can just treat the types
11392 as essentially the same.
11393 Contrast with vector_targets_convertible_p, which is used for vector
11394 pointer types, and vector_types_convertible_p, which will allow
11395 language-specific matches under the control of flag_lax_vector_conversions,
11396 and might still require a conversion. */
11397 /* True if vector types T1 and T2 can be inputs to the same binary
11398 operator without conversion.
11399 We don't check the overall vector size here because some of our callers
11400 want to give different error messages when the vectors are compatible
11401 except for the element count. */
11403 bool
11404 vector_types_compatible_elements_p (tree t1, tree t2)
11406 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11407 t1 = TREE_TYPE (t1);
11408 t2 = TREE_TYPE (t2);
11410 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11412 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11413 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11414 || c2 == FIXED_POINT_TYPE));
11416 t1 = c_common_signed_type (t1);
11417 t2 = c_common_signed_type (t2);
11418 /* Equality works here because c_common_signed_type uses
11419 TYPE_MAIN_VARIANT. */
11420 if (t1 == t2)
11421 return true;
11422 if (opaque && c1 == c2
11423 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11424 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11425 return true;
11426 return false;
11429 /* Check for missing format attributes on function pointers. LTYPE is
11430 the new type or left-hand side type. RTYPE is the old type or
11431 right-hand side type. Returns TRUE if LTYPE is missing the desired
11432 attribute. */
11434 bool
11435 check_missing_format_attribute (tree ltype, tree rtype)
11437 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11438 tree ra;
11440 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11441 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11442 break;
11443 if (ra)
11445 tree la;
11446 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11447 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11448 break;
11449 return !la;
11451 else
11452 return false;
11455 /* Subscripting with type char is likely to lose on a machine where
11456 chars are signed. So warn on any machine, but optionally. Don't
11457 warn for unsigned char since that type is safe. Don't warn for
11458 signed char because anyone who uses that must have done so
11459 deliberately. Furthermore, we reduce the false positive load by
11460 warning only for non-constant value of type char. */
11462 void
11463 warn_array_subscript_with_type_char (location_t loc, tree index)
11465 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11466 && TREE_CODE (index) != INTEGER_CST)
11467 warning_at (loc, OPT_Wchar_subscripts,
11468 "array subscript has type %<char%>");
11471 /* Implement -Wparentheses for the unexpected C precedence rules, to
11472 cover cases like x + y << z which readers are likely to
11473 misinterpret. We have seen an expression in which CODE is a binary
11474 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11475 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11476 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11477 expression was not formed using a binary or unary operator, or it
11478 was enclosed in parentheses. */
11480 void
11481 warn_about_parentheses (location_t loc, enum tree_code code,
11482 enum tree_code code_left, tree arg_left,
11483 enum tree_code code_right, tree arg_right)
11485 if (!warn_parentheses)
11486 return;
11488 /* This macro tests that the expression ARG with original tree code
11489 CODE appears to be a boolean expression. or the result of folding a
11490 boolean expression. */
11491 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11492 (truth_value_p (TREE_CODE (ARG)) \
11493 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11494 /* Folding may create 0 or 1 integers from other expressions. */ \
11495 || ((CODE) != INTEGER_CST \
11496 && (integer_onep (ARG) || integer_zerop (ARG))))
11498 switch (code)
11500 case LSHIFT_EXPR:
11501 if (code_left == PLUS_EXPR)
11502 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11503 "suggest parentheses around %<+%> inside %<<<%>");
11504 else if (code_right == PLUS_EXPR)
11505 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11506 "suggest parentheses around %<+%> inside %<<<%>");
11507 else if (code_left == MINUS_EXPR)
11508 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11509 "suggest parentheses around %<-%> inside %<<<%>");
11510 else if (code_right == MINUS_EXPR)
11511 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11512 "suggest parentheses around %<-%> inside %<<<%>");
11513 return;
11515 case RSHIFT_EXPR:
11516 if (code_left == PLUS_EXPR)
11517 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11518 "suggest parentheses around %<+%> inside %<>>%>");
11519 else if (code_right == PLUS_EXPR)
11520 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11521 "suggest parentheses around %<+%> inside %<>>%>");
11522 else if (code_left == MINUS_EXPR)
11523 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11524 "suggest parentheses around %<-%> inside %<>>%>");
11525 else if (code_right == MINUS_EXPR)
11526 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11527 "suggest parentheses around %<-%> inside %<>>%>");
11528 return;
11530 case TRUTH_ORIF_EXPR:
11531 if (code_left == TRUTH_ANDIF_EXPR)
11532 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11533 "suggest parentheses around %<&&%> within %<||%>");
11534 else if (code_right == TRUTH_ANDIF_EXPR)
11535 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11536 "suggest parentheses around %<&&%> within %<||%>");
11537 return;
11539 case BIT_IOR_EXPR:
11540 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11541 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11542 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11543 "suggest parentheses around arithmetic in operand of %<|%>");
11544 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11545 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11546 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11547 "suggest parentheses around arithmetic in operand of %<|%>");
11548 /* Check cases like x|y==z */
11549 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11550 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11551 "suggest parentheses around comparison in operand of %<|%>");
11552 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11553 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11554 "suggest parentheses around comparison in operand of %<|%>");
11555 /* Check cases like !x | y */
11556 else if (code_left == TRUTH_NOT_EXPR
11557 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11558 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11559 "suggest parentheses around operand of "
11560 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11561 return;
11563 case BIT_XOR_EXPR:
11564 if (code_left == BIT_AND_EXPR
11565 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11566 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11567 "suggest parentheses around arithmetic in operand of %<^%>");
11568 else if (code_right == BIT_AND_EXPR
11569 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11570 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11571 "suggest parentheses around arithmetic in operand of %<^%>");
11572 /* Check cases like x^y==z */
11573 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11574 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11575 "suggest parentheses around comparison in operand of %<^%>");
11576 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11577 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11578 "suggest parentheses around comparison in operand of %<^%>");
11579 return;
11581 case BIT_AND_EXPR:
11582 if (code_left == PLUS_EXPR)
11583 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11584 "suggest parentheses around %<+%> in operand of %<&%>");
11585 else if (code_right == PLUS_EXPR)
11586 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11587 "suggest parentheses around %<+%> in operand of %<&%>");
11588 else if (code_left == MINUS_EXPR)
11589 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11590 "suggest parentheses around %<-%> in operand of %<&%>");
11591 else if (code_right == MINUS_EXPR)
11592 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11593 "suggest parentheses around %<-%> in operand of %<&%>");
11594 /* Check cases like x&y==z */
11595 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11596 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11597 "suggest parentheses around comparison in operand of %<&%>");
11598 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11599 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11600 "suggest parentheses around comparison in operand of %<&%>");
11601 /* Check cases like !x & y */
11602 else if (code_left == TRUTH_NOT_EXPR
11603 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11604 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11605 "suggest parentheses around operand of "
11606 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11607 return;
11609 case EQ_EXPR:
11610 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11611 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11612 "suggest parentheses around comparison in operand of %<==%>");
11613 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11614 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11615 "suggest parentheses around comparison in operand of %<==%>");
11616 return;
11617 case NE_EXPR:
11618 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11619 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11620 "suggest parentheses around comparison in operand of %<!=%>");
11621 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11622 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11623 "suggest parentheses around comparison in operand of %<!=%>");
11624 return;
11626 default:
11627 if (TREE_CODE_CLASS (code) == tcc_comparison)
11629 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11630 && code_left != NE_EXPR && code_left != EQ_EXPR
11631 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11632 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11633 "comparisons like %<X<=Y<=Z%> do not "
11634 "have their mathematical meaning");
11635 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11636 && code_right != NE_EXPR && code_right != EQ_EXPR
11637 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11638 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11639 "comparisons like %<X<=Y<=Z%> do not "
11640 "have their mathematical meaning");
11642 return;
11644 #undef NOT_A_BOOLEAN_EXPR_P
11647 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11649 void
11650 warn_for_unused_label (tree label)
11652 if (!TREE_USED (label))
11654 if (DECL_INITIAL (label))
11655 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11656 else
11657 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11661 /* Warn for division by zero according to the value of DIVISOR. LOC
11662 is the location of the division operator. */
11664 void
11665 warn_for_div_by_zero (location_t loc, tree divisor)
11667 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11668 about division by zero. Do not issue a warning if DIVISOR has a
11669 floating-point type, since we consider 0.0/0.0 a valid way of
11670 generating a NaN. */
11671 if (c_inhibit_evaluation_warnings == 0
11672 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11673 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11676 /* Subroutine of build_binary_op. Give warnings for comparisons
11677 between signed and unsigned quantities that may fail. Do the
11678 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11679 so that casts will be considered, but default promotions won't
11682 LOCATION is the location of the comparison operator.
11684 The arguments of this function map directly to local variables
11685 of build_binary_op. */
11687 void
11688 warn_for_sign_compare (location_t location,
11689 tree orig_op0, tree orig_op1,
11690 tree op0, tree op1,
11691 tree result_type, enum tree_code resultcode)
11693 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11694 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11695 int unsignedp0, unsignedp1;
11697 /* In C++, check for comparison of different enum types. */
11698 if (c_dialect_cxx()
11699 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11700 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11701 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11702 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11704 warning_at (location,
11705 OPT_Wsign_compare, "comparison between types %qT and %qT",
11706 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11709 /* Do not warn if the comparison is being done in a signed type,
11710 since the signed type will only be chosen if it can represent
11711 all the values of the unsigned type. */
11712 if (!TYPE_UNSIGNED (result_type))
11713 /* OK */;
11714 /* Do not warn if both operands are unsigned. */
11715 else if (op0_signed == op1_signed)
11716 /* OK */;
11717 else
11719 tree sop, uop, base_type;
11720 bool ovf;
11722 if (op0_signed)
11723 sop = orig_op0, uop = orig_op1;
11724 else
11725 sop = orig_op1, uop = orig_op0;
11727 STRIP_TYPE_NOPS (sop);
11728 STRIP_TYPE_NOPS (uop);
11729 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11730 ? TREE_TYPE (result_type) : result_type);
11732 /* Do not warn if the signed quantity is an unsuffixed integer
11733 literal (or some static constant expression involving such
11734 literals or a conditional expression involving such literals)
11735 and it is non-negative. */
11736 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11737 /* OK */;
11738 /* Do not warn if the comparison is an equality operation, the
11739 unsigned quantity is an integral constant, and it would fit
11740 in the result if the result were signed. */
11741 else if (TREE_CODE (uop) == INTEGER_CST
11742 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11743 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11744 /* OK */;
11745 /* In C, do not warn if the unsigned quantity is an enumeration
11746 constant and its maximum value would fit in the result if the
11747 result were signed. */
11748 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11749 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11750 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11751 c_common_signed_type (base_type)))
11752 /* OK */;
11753 else
11754 warning_at (location,
11755 OPT_Wsign_compare,
11756 "comparison between signed and unsigned integer expressions");
11759 /* Warn if two unsigned values are being compared in a size larger
11760 than their original size, and one (and only one) is the result of
11761 a `~' operator. This comparison will always fail.
11763 Also warn if one operand is a constant, and the constant does not
11764 have all bits set that are set in the ~ operand when it is
11765 extended. */
11767 op0 = c_common_get_narrower (op0, &unsignedp0);
11768 op1 = c_common_get_narrower (op1, &unsignedp1);
11770 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11771 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11773 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11774 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11775 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11776 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11778 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11780 tree primop;
11781 HOST_WIDE_INT constant, mask;
11782 int unsignedp;
11783 unsigned int bits;
11785 if (tree_fits_shwi_p (op0))
11787 primop = op1;
11788 unsignedp = unsignedp1;
11789 constant = tree_to_shwi (op0);
11791 else
11793 primop = op0;
11794 unsignedp = unsignedp0;
11795 constant = tree_to_shwi (op1);
11798 bits = TYPE_PRECISION (TREE_TYPE (primop));
11799 if (bits < TYPE_PRECISION (result_type)
11800 && bits < HOST_BITS_PER_LONG && unsignedp)
11802 mask = (~ (HOST_WIDE_INT) 0) << bits;
11803 if ((mask & constant) != mask)
11805 if (constant == 0)
11806 warning_at (location, OPT_Wsign_compare,
11807 "promoted ~unsigned is always non-zero");
11808 else
11809 warning_at (location, OPT_Wsign_compare,
11810 "comparison of promoted ~unsigned with constant");
11814 else if (unsignedp0 && unsignedp1
11815 && (TYPE_PRECISION (TREE_TYPE (op0))
11816 < TYPE_PRECISION (result_type))
11817 && (TYPE_PRECISION (TREE_TYPE (op1))
11818 < TYPE_PRECISION (result_type)))
11819 warning_at (location, OPT_Wsign_compare,
11820 "comparison of promoted ~unsigned with unsigned");
11824 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11825 type via c_common_type. If -Wdouble-promotion is in use, and the
11826 conditions for warning have been met, issue a warning. GMSGID is
11827 the warning message. It must have two %T specifiers for the type
11828 that was converted (generally "float") and the type to which it was
11829 converted (generally "double), respectively. LOC is the location
11830 to which the awrning should refer. */
11832 void
11833 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11834 const char *gmsgid, location_t loc)
11836 tree source_type;
11838 if (!warn_double_promotion)
11839 return;
11840 /* If the conversion will not occur at run-time, there is no need to
11841 warn about it. */
11842 if (c_inhibit_evaluation_warnings)
11843 return;
11844 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11845 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11846 return;
11847 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11848 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11849 source_type = type1;
11850 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11851 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11852 source_type = type2;
11853 else
11854 return;
11855 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11858 /* Setup a TYPE_DECL node as a typedef representation.
11860 X is a TYPE_DECL for a typedef statement. Create a brand new
11861 ..._TYPE node (which will be just a variant of the existing
11862 ..._TYPE node with identical properties) and then install X
11863 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11865 The whole point here is to end up with a situation where each
11866 and every ..._TYPE node the compiler creates will be uniquely
11867 associated with AT MOST one node representing a typedef name.
11868 This way, even though the compiler substitutes corresponding
11869 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11870 early on, later parts of the compiler can always do the reverse
11871 translation and get back the corresponding typedef name. For
11872 example, given:
11874 typedef struct S MY_TYPE;
11875 MY_TYPE object;
11877 Later parts of the compiler might only know that `object' was of
11878 type `struct S' if it were not for code just below. With this
11879 code however, later parts of the compiler see something like:
11881 struct S' == struct S
11882 typedef struct S' MY_TYPE;
11883 struct S' object;
11885 And they can then deduce (from the node for type struct S') that
11886 the original object declaration was:
11888 MY_TYPE object;
11890 Being able to do this is important for proper support of protoize,
11891 and also for generating precise symbolic debugging information
11892 which takes full account of the programmer's (typedef) vocabulary.
11894 Obviously, we don't want to generate a duplicate ..._TYPE node if
11895 the TYPE_DECL node that we are now processing really represents a
11896 standard built-in type. */
11898 void
11899 set_underlying_type (tree x)
11901 if (x == error_mark_node)
11902 return;
11903 if (DECL_IS_BUILTIN (x))
11905 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11906 TYPE_NAME (TREE_TYPE (x)) = x;
11908 else if (TREE_TYPE (x) != error_mark_node
11909 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11911 tree tt = TREE_TYPE (x);
11912 DECL_ORIGINAL_TYPE (x) = tt;
11913 tt = build_variant_type_copy (tt);
11914 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11915 TYPE_NAME (tt) = x;
11916 TREE_USED (tt) = TREE_USED (x);
11917 TREE_TYPE (x) = tt;
11921 /* Record the types used by the current global variable declaration
11922 being parsed, so that we can decide later to emit their debug info.
11923 Those types are in types_used_by_cur_var_decl, and we are going to
11924 store them in the types_used_by_vars_hash hash table.
11925 DECL is the declaration of the global variable that has been parsed. */
11927 void
11928 record_types_used_by_current_var_decl (tree decl)
11930 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11932 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11934 tree type = types_used_by_cur_var_decl->pop ();
11935 types_used_by_var_decl_insert (type, decl);
11939 /* If DECL is a typedef that is declared in the current function,
11940 record it for the purpose of -Wunused-local-typedefs. */
11942 void
11943 record_locally_defined_typedef (tree decl)
11945 struct c_language_function *l;
11947 if (!warn_unused_local_typedefs
11948 || cfun == NULL
11949 /* if this is not a locally defined typedef then we are not
11950 interested. */
11951 || !is_typedef_decl (decl)
11952 || !decl_function_context (decl))
11953 return;
11955 l = (struct c_language_function *) cfun->language;
11956 vec_safe_push (l->local_typedefs, decl);
11959 /* If T is a TYPE_DECL declared locally, mark it as used. */
11961 void
11962 maybe_record_typedef_use (tree t)
11964 if (!is_typedef_decl (t))
11965 return;
11967 TREE_USED (t) = true;
11970 /* Warn if there are some unused locally defined typedefs in the
11971 current function. */
11973 void
11974 maybe_warn_unused_local_typedefs (void)
11976 int i;
11977 tree decl;
11978 /* The number of times we have emitted -Wunused-local-typedefs
11979 warnings. If this is different from errorcount, that means some
11980 unrelated errors have been issued. In which case, we'll avoid
11981 emitting "unused-local-typedefs" warnings. */
11982 static int unused_local_typedefs_warn_count;
11983 struct c_language_function *l;
11985 if (cfun == NULL)
11986 return;
11988 if ((l = (struct c_language_function *) cfun->language) == NULL)
11989 return;
11991 if (warn_unused_local_typedefs
11992 && errorcount == unused_local_typedefs_warn_count)
11994 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11995 if (!TREE_USED (decl))
11996 warning_at (DECL_SOURCE_LOCATION (decl),
11997 OPT_Wunused_local_typedefs,
11998 "typedef %qD locally defined but not used", decl);
11999 unused_local_typedefs_warn_count = errorcount;
12002 vec_free (l->local_typedefs);
12005 /* Warn about boolean expression compared with an integer value different
12006 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12007 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12008 are the operands of the comparison. The caller must ensure that
12009 either operand is a boolean expression. */
12011 void
12012 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12013 tree op1)
12015 if (TREE_CODE_CLASS (code) != tcc_comparison)
12016 return;
12018 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12019 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12020 if (!cst)
12021 return;
12023 if (!integer_zerop (cst) && !integer_onep (cst))
12025 int sign = (TREE_CODE (op0) == INTEGER_CST)
12026 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
12027 if (code == EQ_EXPR
12028 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12029 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12030 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12031 "with boolean expression is always false", cst);
12032 else
12033 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12034 "with boolean expression is always true", cst);
12038 /* The C and C++ parsers both use vectors to hold function arguments.
12039 For efficiency, we keep a cache of unused vectors. This is the
12040 cache. */
12042 typedef vec<tree, va_gc> *tree_gc_vec;
12043 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12045 /* Return a new vector from the cache. If the cache is empty,
12046 allocate a new vector. These vectors are GC'ed, so it is OK if the
12047 pointer is not released.. */
12049 vec<tree, va_gc> *
12050 make_tree_vector (void)
12052 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12053 return tree_vector_cache->pop ();
12054 else
12056 /* Passing 0 to vec::alloc returns NULL, and our callers require
12057 that we always return a non-NULL value. The vector code uses
12058 4 when growing a NULL vector, so we do too. */
12059 vec<tree, va_gc> *v;
12060 vec_alloc (v, 4);
12061 return v;
12065 /* Release a vector of trees back to the cache. */
12067 void
12068 release_tree_vector (vec<tree, va_gc> *vec)
12070 if (vec != NULL)
12072 vec->truncate (0);
12073 vec_safe_push (tree_vector_cache, vec);
12077 /* Get a new tree vector holding a single tree. */
12079 vec<tree, va_gc> *
12080 make_tree_vector_single (tree t)
12082 vec<tree, va_gc> *ret = make_tree_vector ();
12083 ret->quick_push (t);
12084 return ret;
12087 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12089 vec<tree, va_gc> *
12090 make_tree_vector_from_list (tree list)
12092 vec<tree, va_gc> *ret = make_tree_vector ();
12093 for (; list; list = TREE_CHAIN (list))
12094 vec_safe_push (ret, TREE_VALUE (list));
12095 return ret;
12098 /* Get a new tree vector which is a copy of an existing one. */
12100 vec<tree, va_gc> *
12101 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12103 vec<tree, va_gc> *ret;
12104 unsigned int ix;
12105 tree t;
12107 ret = make_tree_vector ();
12108 vec_safe_reserve (ret, vec_safe_length (orig));
12109 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12110 ret->quick_push (t);
12111 return ret;
12114 /* Return true if KEYWORD starts a type specifier. */
12116 bool
12117 keyword_begins_type_specifier (enum rid keyword)
12119 switch (keyword)
12121 case RID_AUTO_TYPE:
12122 case RID_INT:
12123 case RID_CHAR:
12124 case RID_FLOAT:
12125 case RID_DOUBLE:
12126 case RID_VOID:
12127 case RID_UNSIGNED:
12128 case RID_LONG:
12129 case RID_SHORT:
12130 case RID_SIGNED:
12131 case RID_DFLOAT32:
12132 case RID_DFLOAT64:
12133 case RID_DFLOAT128:
12134 case RID_FRACT:
12135 case RID_ACCUM:
12136 case RID_BOOL:
12137 case RID_WCHAR:
12138 case RID_CHAR16:
12139 case RID_CHAR32:
12140 case RID_SAT:
12141 case RID_COMPLEX:
12142 case RID_TYPEOF:
12143 case RID_STRUCT:
12144 case RID_CLASS:
12145 case RID_UNION:
12146 case RID_ENUM:
12147 return true;
12148 default:
12149 if (keyword >= RID_FIRST_INT_N
12150 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12151 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12152 return true;
12153 return false;
12157 /* Return true if KEYWORD names a type qualifier. */
12159 bool
12160 keyword_is_type_qualifier (enum rid keyword)
12162 switch (keyword)
12164 case RID_CONST:
12165 case RID_VOLATILE:
12166 case RID_RESTRICT:
12167 case RID_ATOMIC:
12168 return true;
12169 default:
12170 return false;
12174 /* Return true if KEYWORD names a storage class specifier.
12176 RID_TYPEDEF is not included in this list despite `typedef' being
12177 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12178 such for syntactic convenience only. */
12180 bool
12181 keyword_is_storage_class_specifier (enum rid keyword)
12183 switch (keyword)
12185 case RID_STATIC:
12186 case RID_EXTERN:
12187 case RID_REGISTER:
12188 case RID_AUTO:
12189 case RID_MUTABLE:
12190 case RID_THREAD:
12191 return true;
12192 default:
12193 return false;
12197 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12199 static bool
12200 keyword_is_function_specifier (enum rid keyword)
12202 switch (keyword)
12204 case RID_INLINE:
12205 case RID_NORETURN:
12206 case RID_VIRTUAL:
12207 case RID_EXPLICIT:
12208 return true;
12209 default:
12210 return false;
12214 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12215 declaration-specifier (C99 6.7). */
12217 bool
12218 keyword_is_decl_specifier (enum rid keyword)
12220 if (keyword_is_storage_class_specifier (keyword)
12221 || keyword_is_type_qualifier (keyword)
12222 || keyword_is_function_specifier (keyword))
12223 return true;
12225 switch (keyword)
12227 case RID_TYPEDEF:
12228 case RID_FRIEND:
12229 case RID_CONSTEXPR:
12230 return true;
12231 default:
12232 return false;
12236 /* Initialize language-specific-bits of tree_contains_struct. */
12238 void
12239 c_common_init_ts (void)
12241 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12242 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12243 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12244 MARK_TS_COMMON (UPC_FORALL_STMT);
12245 MARK_TS_COMMON (UPC_SYNC_STMT);
12248 /* Build a user-defined numeric literal out of an integer constant type VALUE
12249 with identifier SUFFIX. */
12251 tree
12252 build_userdef_literal (tree suffix_id, tree value,
12253 enum overflow_type overflow, tree num_string)
12255 tree literal = make_node (USERDEF_LITERAL);
12256 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12257 USERDEF_LITERAL_VALUE (literal) = value;
12258 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12259 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12260 return literal;
12263 /* For vector[index], convert the vector to a
12264 pointer of the underlying type. Return true if the resulting
12265 ARRAY_REF should not be an lvalue. */
12267 bool
12268 convert_vector_to_pointer_for_subscript (location_t loc,
12269 tree *vecp, tree index)
12271 bool ret = false;
12272 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12274 tree type = TREE_TYPE (*vecp);
12275 tree type1;
12277 ret = !lvalue_p (*vecp);
12278 if (TREE_CODE (index) == INTEGER_CST)
12279 if (!tree_fits_uhwi_p (index)
12280 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12281 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12283 if (ret)
12285 tree tmp = create_tmp_var_raw (type);
12286 DECL_SOURCE_LOCATION (tmp) = loc;
12287 *vecp = c_save_expr (*vecp);
12288 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12290 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12291 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12292 *vecp
12293 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12294 *vecp, NULL_TREE, NULL_TREE),
12295 non_const);
12297 else
12298 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12299 NULL_TREE, NULL_TREE);
12300 SET_EXPR_LOCATION (*vecp, loc);
12301 c_common_mark_addressable_vec (tmp);
12303 else
12304 c_common_mark_addressable_vec (*vecp);
12305 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12306 type1 = build_pointer_type (TREE_TYPE (*vecp));
12307 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12308 if (!ref_all
12309 && !DECL_P (*vecp))
12311 /* If the original vector isn't declared may_alias and it
12312 isn't a bare vector look if the subscripting would
12313 alias the vector we subscript, and if not, force ref-all. */
12314 alias_set_type vecset = get_alias_set (*vecp);
12315 alias_set_type sset = get_alias_set (type);
12316 if (!alias_sets_must_conflict_p (sset, vecset)
12317 && !alias_set_subset_of (sset, vecset))
12318 ref_all = true;
12320 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12321 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12322 *vecp = convert (type, *vecp);
12324 return ret;
12327 /* Determine which of the operands, if any, is a scalar that needs to be
12328 converted to a vector, for the range of operations. */
12329 enum stv_conv
12330 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12331 bool complain)
12333 tree type0 = TREE_TYPE (op0);
12334 tree type1 = TREE_TYPE (op1);
12335 bool integer_only_op = false;
12336 enum stv_conv ret = stv_firstarg;
12338 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12339 || TREE_CODE (type1) == VECTOR_TYPE);
12340 switch (code)
12342 /* Most GENERIC binary expressions require homogeneous arguments.
12343 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12344 argument that is a vector and a second one that is a scalar, so
12345 we never return stv_secondarg for them. */
12346 case RSHIFT_EXPR:
12347 case LSHIFT_EXPR:
12348 if (TREE_CODE (type0) == INTEGER_TYPE
12349 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12351 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12353 if (complain)
12354 error_at (loc, "conversion of scalar %qT to vector %qT "
12355 "involves truncation", type0, type1);
12356 return stv_error;
12358 else
12359 return stv_firstarg;
12361 break;
12363 case BIT_IOR_EXPR:
12364 case BIT_XOR_EXPR:
12365 case BIT_AND_EXPR:
12366 integer_only_op = true;
12367 /* ... fall through ... */
12369 case VEC_COND_EXPR:
12371 case PLUS_EXPR:
12372 case MINUS_EXPR:
12373 case MULT_EXPR:
12374 case TRUNC_DIV_EXPR:
12375 case CEIL_DIV_EXPR:
12376 case FLOOR_DIV_EXPR:
12377 case ROUND_DIV_EXPR:
12378 case EXACT_DIV_EXPR:
12379 case TRUNC_MOD_EXPR:
12380 case FLOOR_MOD_EXPR:
12381 case RDIV_EXPR:
12382 case EQ_EXPR:
12383 case NE_EXPR:
12384 case LE_EXPR:
12385 case GE_EXPR:
12386 case LT_EXPR:
12387 case GT_EXPR:
12388 /* What about UNLT_EXPR? */
12389 if (TREE_CODE (type0) == VECTOR_TYPE)
12391 tree tmp;
12392 ret = stv_secondarg;
12393 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12394 tmp = type0; type0 = type1; type1 = tmp;
12395 tmp = op0; op0 = op1; op1 = tmp;
12398 if (TREE_CODE (type0) == INTEGER_TYPE
12399 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12401 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12403 if (complain)
12404 error_at (loc, "conversion of scalar %qT to vector %qT "
12405 "involves truncation", type0, type1);
12406 return stv_error;
12408 return ret;
12410 else if (!integer_only_op
12411 /* Allow integer --> real conversion if safe. */
12412 && (TREE_CODE (type0) == REAL_TYPE
12413 || TREE_CODE (type0) == INTEGER_TYPE)
12414 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12416 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12418 if (complain)
12419 error_at (loc, "conversion of scalar %qT to vector %qT "
12420 "involves truncation", type0, type1);
12421 return stv_error;
12423 return ret;
12425 default:
12426 break;
12429 return stv_nothing;
12432 /* Return true iff ALIGN is an integral constant that is a fundamental
12433 alignment, as defined by [basic.align] in the c++-11
12434 specifications.
12436 That is:
12438 [A fundamental alignment is represented by an alignment less than or
12439 equal to the greatest alignment supported by the implementation
12440 in all contexts, which is equal to
12441 alignof(max_align_t)]. */
12443 bool
12444 cxx_fundamental_alignment_p (unsigned align)
12446 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12447 TYPE_ALIGN (long_double_type_node)));
12450 /* Return true if T is a pointer to a zero-sized aggregate. */
12452 bool
12453 pointer_to_zero_sized_aggr_p (tree t)
12455 if (!POINTER_TYPE_P (t))
12456 return false;
12457 t = TREE_TYPE (t);
12458 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12461 #include "gt-c-family-c-common.h"