Merge trunk version 208955 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
bloba93aba1cd74c3cb88b0eea8299723edcb3c3d23d
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "intl.h"
25 #include "tree.h"
26 #include "fold-const.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "varasm.h"
32 #include "trans-mem.h"
33 #include "flags.h"
34 #include "c-pragma.h"
35 #include "c-common.h"
36 #include "c-objc.h"
37 #include "tm_p.h"
38 #include "obstack.h"
39 #include "cpplib.h"
40 #include "target.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
47 #include "hashtab.h"
48 #include "opts.h"
49 #include "cgraph.h"
50 #include "target-def.h"
51 #include "gimplify.h"
53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63 tree int128_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
68 tree int128_unsigned_type_node;
70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
74 tree ptrdiff_type_node;
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
78 tree wchar_type_node;
80 tree char16_type_node;
81 tree char32_type_node;
83 tree float_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
96 tree intQI_type_node;
97 tree intHI_type_node;
98 tree intSI_type_node;
99 tree intDI_type_node;
100 tree intTI_type_node;
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
111 Nodes for types `void *' and `const void *'.
113 tree ptr_type_node, const_ptr_type_node;
115 Nodes for types `char *' and `const char *'.
117 tree string_type_node, const_string_type_node;
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
122 tree char_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
145 tree void_list_node;
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
172 char flag_no_output;
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193 int flag_iso;
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
206 int flag_isoc94;
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 int flag_isoc99;
212 /* Nonzero means use the ISO C11 dialect of C. */
214 int flag_isoc11;
216 /* Nonzero means that we have builtin functions, and main is an int. */
218 int flag_hosted = 1;
221 /* ObjC language option variables. */
224 /* Tells the compiler that this is a special run. Do not perform any
225 compiling, instead we are to test some platform dependent features
226 and output a C header file with appropriate definitions. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
234 /* UPC language option variables. */
236 /* Nonzero whenever UPC -fupc-threads-N is asserted.
237 The value N gives the number of UPC threads to be
238 defined at compile-time. */
239 int flag_upc_threads;
241 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
242 int flag_upc_pthreads;
244 /* The implementation model for UPC threads that
245 are mapped to POSIX threads, specified at compilation
246 time by the -fupc-pthreads-model-* switch. */
247 upc_pthreads_model_kind upc_pthreads_model;
249 /* C++ language option variables. */
252 /* Nonzero means generate separate instantiation control files and
253 juggle them at link time. */
255 int flag_use_repository;
257 /* The C++ dialect being used. C++98 is the default. */
259 enum cxx_dialect cxx_dialect = cxx98;
261 /* Maximum template instantiation depth. This limit exists to limit the
262 time it takes to notice excessively recursive template instantiations.
264 The default is lower than the 1024 recommended by the C++0x standard
265 because G++ runs out of stack before 1024 with highly recursive template
266 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
268 int max_tinst_depth = 900;
270 /* The elements of `ridpointers' are identifier nodes for the reserved
271 type names and storage classes. It is indexed by a RID_... value. */
272 tree *ridpointers;
274 tree (*make_fname_decl) (location_t, tree, int);
276 /* Nonzero means don't warn about problems that occur when the code is
277 executed. */
278 int c_inhibit_evaluation_warnings;
280 /* Whether we are building a boolean conversion inside
281 convert_for_assignment, or some other late binary operation. If
282 build_binary_op is called for C (from code shared by C and C++) in
283 this case, then the operands have already been folded and the
284 result will not be folded again, so C_MAYBE_CONST_EXPR should not
285 be generated. */
286 bool in_late_binary_op;
288 /* Whether lexing has been completed, so subsequent preprocessor
289 errors should use the compiler's input_location. */
290 bool done_lexing = false;
292 /* Information about how a function name is generated. */
293 struct fname_var_t
295 tree *const decl; /* pointer to the VAR_DECL. */
296 const unsigned rid; /* RID number for the identifier. */
297 const int pretty; /* How pretty is it? */
300 /* The three ways of getting then name of the current function. */
302 const struct fname_var_t fname_vars[] =
304 /* C99 compliant __func__, must be first. */
305 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
306 /* GCC __FUNCTION__ compliant. */
307 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
308 /* GCC __PRETTY_FUNCTION__ compliant. */
309 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
310 {NULL, 0, 0},
313 /* Global visibility options. */
314 struct visibility_flags visibility_options;
316 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
317 static tree check_case_value (tree);
318 static bool check_case_bounds (tree, tree, tree *, tree *);
320 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
327 int, bool *);
328 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
329 int, bool *);
330 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
331 bool *);
332 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_always_inline_attribute (tree *, tree, tree, int,
336 bool *);
337 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_transparent_union_attribute (tree *, tree, tree,
347 int, bool *);
348 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
355 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
358 static tree handle_visibility_attribute (tree *, tree, tree, int,
359 bool *);
360 static tree handle_tls_model_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_no_instrument_function_attribute (tree *, tree,
363 tree, int, bool *);
364 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
367 bool *);
368 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_deprecated_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_vector_size_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
380 bool *);
381 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
388 static tree ignore_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
394 bool *);
395 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
396 bool *);
398 static void check_function_nonnull (tree, int, tree *);
399 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
402 static int resort_field_decl_cmp (const void *, const void *);
404 /* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 UPC is like C except that D_UPC is not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
413 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
414 ObjC++ is like C++ except that D_OBJC is not set
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
426 const struct c_common_resword c_common_reswords[] =
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
430 { "_Atomic", RID_ATOMIC, D_CONLY },
431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
435 { "_Imaginary", RID_IMAGINARY, D_CONLY },
436 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
437 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
438 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
439 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
440 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
441 { "_Sat", RID_SAT, D_CONLY | D_EXT },
442 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
443 { "_Noreturn", RID_NORETURN, D_CONLY },
444 { "_Generic", RID_GENERIC, D_CONLY },
445 { "_Thread_local", RID_THREAD, D_CONLY },
446 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
447 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
448 { "__alignof", RID_ALIGNOF, 0 },
449 { "__alignof__", RID_ALIGNOF, 0 },
450 { "__asm", RID_ASM, 0 },
451 { "__asm__", RID_ASM, 0 },
452 { "__attribute", RID_ATTRIBUTE, 0 },
453 { "__attribute__", RID_ATTRIBUTE, 0 },
454 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
455 { "__bases", RID_BASES, D_CXXONLY },
456 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
457 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
458 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
459 { "__builtin_offsetof", RID_OFFSETOF, 0 },
460 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
461 { "__builtin_va_arg", RID_VA_ARG, 0 },
462 { "__complex", RID_COMPLEX, 0 },
463 { "__complex__", RID_COMPLEX, 0 },
464 { "__const", RID_CONST, 0 },
465 { "__const__", RID_CONST, 0 },
466 { "__decltype", RID_DECLTYPE, D_CXXONLY },
467 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
468 { "__extension__", RID_EXTENSION, 0 },
469 { "__func__", RID_C99_FUNCTION_NAME, 0 },
470 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
471 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
472 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
473 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
474 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
475 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
476 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
477 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
478 { "__imag", RID_IMAGPART, 0 },
479 { "__imag__", RID_IMAGPART, 0 },
480 { "__inline", RID_INLINE, 0 },
481 { "__inline__", RID_INLINE, 0 },
482 { "__int128", RID_INT128, 0 },
483 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
484 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
485 { "__is_class", RID_IS_CLASS, D_CXXONLY },
486 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
487 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
488 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
489 { "__is_final", RID_IS_FINAL, D_CXXONLY },
490 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
491 { "__is_pod", RID_IS_POD, D_CXXONLY },
492 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
493 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
494 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
495 { "__is_union", RID_IS_UNION, D_CXXONLY },
496 { "__label__", RID_LABEL, 0 },
497 { "__null", RID_NULL, 0 },
498 { "__real", RID_REALPART, 0 },
499 { "__real__", RID_REALPART, 0 },
500 { "__restrict", RID_RESTRICT, 0 },
501 { "__restrict__", RID_RESTRICT, 0 },
502 { "__signed", RID_SIGNED, 0 },
503 { "__signed__", RID_SIGNED, 0 },
504 { "__thread", RID_THREAD, 0 },
505 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
506 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
507 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
508 { "__typeof", RID_TYPEOF, 0 },
509 { "__typeof__", RID_TYPEOF, 0 },
510 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
511 { "__volatile", RID_VOLATILE, 0 },
512 { "__volatile__", RID_VOLATILE, 0 },
513 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
514 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
515 { "asm", RID_ASM, D_ASM },
516 { "auto", RID_AUTO, 0 },
517 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
518 { "break", RID_BREAK, 0 },
519 { "case", RID_CASE, 0 },
520 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
521 { "char", RID_CHAR, 0 },
522 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
523 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
524 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
525 { "const", RID_CONST, 0 },
526 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
528 { "continue", RID_CONTINUE, 0 },
529 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
530 { "default", RID_DEFAULT, 0 },
531 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
532 { "do", RID_DO, 0 },
533 { "double", RID_DOUBLE, 0 },
534 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
535 { "else", RID_ELSE, 0 },
536 { "enum", RID_ENUM, 0 },
537 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
538 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
539 { "extern", RID_EXTERN, 0 },
540 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
541 { "float", RID_FLOAT, 0 },
542 { "for", RID_FOR, 0 },
543 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
544 { "goto", RID_GOTO, 0 },
545 { "if", RID_IF, 0 },
546 { "inline", RID_INLINE, D_EXT89 },
547 { "int", RID_INT, 0 },
548 { "long", RID_LONG, 0 },
549 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
550 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
551 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
552 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
553 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
554 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
555 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
556 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
557 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
558 { "register", RID_REGISTER, 0 },
559 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
560 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
561 { "return", RID_RETURN, 0 },
562 { "short", RID_SHORT, 0 },
563 { "signed", RID_SIGNED, 0 },
564 { "sizeof", RID_SIZEOF, 0 },
565 { "static", RID_STATIC, 0 },
566 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
568 { "struct", RID_STRUCT, 0 },
569 { "switch", RID_SWITCH, 0 },
570 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
571 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
572 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
573 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
574 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
575 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
576 { "typedef", RID_TYPEDEF, 0 },
577 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
578 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
579 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
580 { "union", RID_UNION, 0 },
581 { "unsigned", RID_UNSIGNED, 0 },
582 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
583 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
584 { "void", RID_VOID, 0 },
585 { "volatile", RID_VOLATILE, 0 },
586 { "wchar_t", RID_WCHAR, D_CXXONLY },
587 { "while", RID_WHILE, 0 },
588 /* These Objective-C keywords are recognized only immediately after
589 an '@'. */
590 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
591 { "defs", RID_AT_DEFS, D_OBJC },
592 { "encode", RID_AT_ENCODE, D_OBJC },
593 { "end", RID_AT_END, D_OBJC },
594 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
595 { "interface", RID_AT_INTERFACE, D_OBJC },
596 { "protocol", RID_AT_PROTOCOL, D_OBJC },
597 { "selector", RID_AT_SELECTOR, D_OBJC },
598 { "finally", RID_AT_FINALLY, D_OBJC },
599 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
600 { "optional", RID_AT_OPTIONAL, D_OBJC },
601 { "required", RID_AT_REQUIRED, D_OBJC },
602 { "property", RID_AT_PROPERTY, D_OBJC },
603 { "package", RID_AT_PACKAGE, D_OBJC },
604 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
605 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
606 /* These are recognized only in protocol-qualifier context
607 (see above) */
608 { "bycopy", RID_BYCOPY, D_OBJC },
609 { "byref", RID_BYREF, D_OBJC },
610 { "in", RID_IN, D_OBJC },
611 { "inout", RID_INOUT, D_OBJC },
612 { "oneway", RID_ONEWAY, D_OBJC },
613 { "out", RID_OUT, D_OBJC },
615 /* UPC keywords */
616 { "shared", RID_SHARED, D_UPC },
617 { "relaxed", RID_RELAXED, D_UPC },
618 { "strict", RID_STRICT, D_UPC },
619 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
620 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
621 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
622 { "upc_forall", RID_UPC_FORALL, D_UPC },
623 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
624 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
625 { "upc_wait", RID_UPC_WAIT, D_UPC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
641 /* Table of machine-independent attributes common to all C-like languages. */
642 const struct attribute_spec c_common_attribute_table[] =
644 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
645 affects_type_identity } */
646 { "packed", 0, 0, false, false, false,
647 handle_packed_attribute , false},
648 { "nocommon", 0, 0, true, false, false,
649 handle_nocommon_attribute, false},
650 { "common", 0, 0, true, false, false,
651 handle_common_attribute, false },
652 /* FIXME: logically, noreturn attributes should be listed as
653 "false, true, true" and apply to function types. But implementing this
654 would require all the places in the compiler that use TREE_THIS_VOLATILE
655 on a decl to identify non-returning functions to be located and fixed
656 to check the function type instead. */
657 { "noreturn", 0, 0, true, false, false,
658 handle_noreturn_attribute, false },
659 { "volatile", 0, 0, true, false, false,
660 handle_noreturn_attribute, false },
661 { "noinline", 0, 0, true, false, false,
662 handle_noinline_attribute, false },
663 { "noclone", 0, 0, true, false, false,
664 handle_noclone_attribute, false },
665 { "leaf", 0, 0, true, false, false,
666 handle_leaf_attribute, false },
667 { "always_inline", 0, 0, true, false, false,
668 handle_always_inline_attribute, false },
669 { "gnu_inline", 0, 0, true, false, false,
670 handle_gnu_inline_attribute, false },
671 { "artificial", 0, 0, true, false, false,
672 handle_artificial_attribute, false },
673 { "flatten", 0, 0, true, false, false,
674 handle_flatten_attribute, false },
675 { "used", 0, 0, true, false, false,
676 handle_used_attribute, false },
677 { "unused", 0, 0, false, false, false,
678 handle_unused_attribute, false },
679 { "externally_visible", 0, 0, true, false, false,
680 handle_externally_visible_attribute, false },
681 /* The same comments as for noreturn attributes apply to const ones. */
682 { "const", 0, 0, true, false, false,
683 handle_const_attribute, false },
684 { "transparent_union", 0, 0, false, false, false,
685 handle_transparent_union_attribute, false },
686 { "constructor", 0, 1, true, false, false,
687 handle_constructor_attribute, false },
688 { "destructor", 0, 1, true, false, false,
689 handle_destructor_attribute, false },
690 { "mode", 1, 1, false, true, false,
691 handle_mode_attribute, false },
692 { "section", 1, 1, true, false, false,
693 handle_section_attribute, false },
694 { "aligned", 0, 1, false, false, false,
695 handle_aligned_attribute, false },
696 { "weak", 0, 0, true, false, false,
697 handle_weak_attribute, false },
698 { "ifunc", 1, 1, true, false, false,
699 handle_ifunc_attribute, false },
700 { "alias", 1, 1, true, false, false,
701 handle_alias_attribute, false },
702 { "weakref", 0, 1, true, false, false,
703 handle_weakref_attribute, false },
704 { "no_instrument_function", 0, 0, true, false, false,
705 handle_no_instrument_function_attribute,
706 false },
707 { "malloc", 0, 0, true, false, false,
708 handle_malloc_attribute, false },
709 { "returns_twice", 0, 0, true, false, false,
710 handle_returns_twice_attribute, false },
711 { "no_stack_limit", 0, 0, true, false, false,
712 handle_no_limit_stack_attribute, false },
713 { "pure", 0, 0, true, false, false,
714 handle_pure_attribute, false },
715 { "transaction_callable", 0, 0, false, true, false,
716 handle_tm_attribute, false },
717 { "transaction_unsafe", 0, 0, false, true, false,
718 handle_tm_attribute, false },
719 { "transaction_safe", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 { "transaction_may_cancel_outer", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 /* ??? These two attributes didn't make the transition from the
724 Intel language document to the multi-vendor language document. */
725 { "transaction_pure", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_wrap", 1, 1, true, false, false,
728 handle_tm_wrap_attribute, false },
729 /* For internal use (marking of builtins) only. The name contains space
730 to prevent its usage in source code. */
731 { "no vops", 0, 0, true, false, false,
732 handle_novops_attribute, false },
733 { "deprecated", 0, 1, false, false, false,
734 handle_deprecated_attribute, false },
735 { "vector_size", 1, 1, false, true, false,
736 handle_vector_size_attribute, false },
737 { "visibility", 1, 1, false, false, false,
738 handle_visibility_attribute, false },
739 { "tls_model", 1, 1, true, false, false,
740 handle_tls_model_attribute, false },
741 { "nonnull", 0, -1, false, true, true,
742 handle_nonnull_attribute, false },
743 { "nothrow", 0, 0, true, false, false,
744 handle_nothrow_attribute, false },
745 { "may_alias", 0, 0, false, true, false, NULL, false },
746 { "cleanup", 1, 1, true, false, false,
747 handle_cleanup_attribute, false },
748 { "warn_unused_result", 0, 0, false, true, true,
749 handle_warn_unused_result_attribute, false },
750 { "sentinel", 0, 1, false, true, true,
751 handle_sentinel_attribute, false },
752 /* For internal use (marking of builtins) only. The name contains space
753 to prevent its usage in source code. */
754 { "type generic", 0, 0, false, true, true,
755 handle_type_generic_attribute, false },
756 { "alloc_size", 1, 2, false, true, true,
757 handle_alloc_size_attribute, false },
758 { "cold", 0, 0, true, false, false,
759 handle_cold_attribute, false },
760 { "hot", 0, 0, true, false, false,
761 handle_hot_attribute, false },
762 { "no_address_safety_analysis",
763 0, 0, true, false, false,
764 handle_no_address_safety_analysis_attribute,
765 false },
766 { "no_sanitize_address", 0, 0, true, false, false,
767 handle_no_sanitize_address_attribute,
768 false },
769 { "no_sanitize_undefined", 0, 0, true, false, false,
770 handle_no_sanitize_undefined_attribute,
771 false },
772 { "warning", 1, 1, true, false, false,
773 handle_error_attribute, false },
774 { "error", 1, 1, true, false, false,
775 handle_error_attribute, false },
776 { "target", 1, -1, true, false, false,
777 handle_target_attribute, false },
778 { "optimize", 1, -1, true, false, false,
779 handle_optimize_attribute, false },
780 /* For internal use only. The leading '*' both prevents its usage in
781 source code and signals that it may be overridden by machine tables. */
782 { "*tm regparm", 0, 0, false, true, true,
783 ignore_attribute, false },
784 { "no_split_stack", 0, 0, true, false, false,
785 handle_no_split_stack_attribute, false },
786 /* For internal use (marking of builtins and runtime functions) only.
787 The name contains space to prevent its usage in source code. */
788 { "fn spec", 1, 1, false, true, true,
789 handle_fnspec_attribute, false },
790 { "warn_unused", 0, 0, false, false, false,
791 handle_warn_unused_attribute, false },
792 { "returns_nonnull", 0, 0, false, true, true,
793 handle_returns_nonnull_attribute, false },
794 { "omp declare simd", 0, -1, true, false, false,
795 handle_omp_declare_simd_attribute, false },
796 { "cilk simd function", 0, -1, true, false, false,
797 handle_omp_declare_simd_attribute, false },
798 { "omp declare target", 0, 0, true, false, false,
799 handle_omp_declare_target_attribute, false },
800 { "alloc_align", 1, 1, false, true, true,
801 handle_alloc_align_attribute, false },
802 { "assume_aligned", 1, 2, false, true, true,
803 handle_assume_aligned_attribute, false },
804 { NULL, 0, 0, false, false, false, NULL, false }
807 /* Give the specifications for the format attributes, used by C and all
808 descendants. */
810 const struct attribute_spec c_common_format_attribute_table[] =
812 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
813 affects_type_identity } */
814 { "format", 3, 3, false, true, true,
815 handle_format_attribute, false },
816 { "format_arg", 1, 1, false, true, true,
817 handle_format_arg_attribute, false },
818 { NULL, 0, 0, false, false, false, NULL, false }
821 /* Return identifier for address space AS. */
823 const char *
824 c_addr_space_name (addr_space_t as)
826 int rid = RID_FIRST_ADDR_SPACE + as;
827 gcc_assert (ridpointers [rid]);
828 return IDENTIFIER_POINTER (ridpointers [rid]);
831 /* Push current bindings for the function name VAR_DECLS. */
833 void
834 start_fname_decls (void)
836 unsigned ix;
837 tree saved = NULL_TREE;
839 for (ix = 0; fname_vars[ix].decl; ix++)
841 tree decl = *fname_vars[ix].decl;
843 if (decl)
845 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
846 saved);
847 *fname_vars[ix].decl = NULL_TREE;
850 if (saved || saved_function_name_decls)
851 /* Normally they'll have been NULL, so only push if we've got a
852 stack, or they are non-NULL. */
853 saved_function_name_decls = tree_cons (saved, NULL_TREE,
854 saved_function_name_decls);
857 /* Finish up the current bindings, adding them into the current function's
858 statement tree. This must be done _before_ finish_stmt_tree is called.
859 If there is no current function, we must be at file scope and no statements
860 are involved. Pop the previous bindings. */
862 void
863 finish_fname_decls (void)
865 unsigned ix;
866 tree stmts = NULL_TREE;
867 tree stack = saved_function_name_decls;
869 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
870 append_to_statement_list (TREE_VALUE (stack), &stmts);
872 if (stmts)
874 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
876 if (TREE_CODE (*bodyp) == BIND_EXPR)
877 bodyp = &BIND_EXPR_BODY (*bodyp);
879 append_to_statement_list_force (*bodyp, &stmts);
880 *bodyp = stmts;
883 for (ix = 0; fname_vars[ix].decl; ix++)
884 *fname_vars[ix].decl = NULL_TREE;
886 if (stack)
888 /* We had saved values, restore them. */
889 tree saved;
891 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
893 tree decl = TREE_PURPOSE (saved);
894 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
896 *fname_vars[ix].decl = decl;
898 stack = TREE_CHAIN (stack);
900 saved_function_name_decls = stack;
903 /* Return the text name of the current function, suitably prettified
904 by PRETTY_P. Return string must be freed by caller. */
906 const char *
907 fname_as_string (int pretty_p)
909 const char *name = "top level";
910 char *namep;
911 int vrb = 2, len;
912 cpp_string cstr = { 0, 0 }, strname;
914 if (!pretty_p)
916 name = "";
917 vrb = 0;
920 if (current_function_decl)
921 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
923 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
925 namep = XNEWVEC (char, len);
926 snprintf (namep, len, "\"%s\"", name);
927 strname.text = (unsigned char *) namep;
928 strname.len = len - 1;
930 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
932 XDELETEVEC (namep);
933 return (const char *) cstr.text;
936 return namep;
939 /* Return the VAR_DECL for a const char array naming the current
940 function. If the VAR_DECL has not yet been created, create it
941 now. RID indicates how it should be formatted and IDENTIFIER_NODE
942 ID is its name (unfortunately C and C++ hold the RID values of
943 keywords in different places, so we can't derive RID from ID in
944 this language independent code. LOC is the location of the
945 function. */
947 tree
948 fname_decl (location_t loc, unsigned int rid, tree id)
950 unsigned ix;
951 tree decl = NULL_TREE;
953 for (ix = 0; fname_vars[ix].decl; ix++)
954 if (fname_vars[ix].rid == rid)
955 break;
957 decl = *fname_vars[ix].decl;
958 if (!decl)
960 /* If a tree is built here, it would normally have the lineno of
961 the current statement. Later this tree will be moved to the
962 beginning of the function and this line number will be wrong.
963 To avoid this problem set the lineno to 0 here; that prevents
964 it from appearing in the RTL. */
965 tree stmts;
966 location_t saved_location = input_location;
967 input_location = UNKNOWN_LOCATION;
969 stmts = push_stmt_list ();
970 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
971 stmts = pop_stmt_list (stmts);
972 if (!IS_EMPTY_STMT (stmts))
973 saved_function_name_decls
974 = tree_cons (decl, stmts, saved_function_name_decls);
975 *fname_vars[ix].decl = decl;
976 input_location = saved_location;
978 if (!ix && !current_function_decl)
979 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
981 return decl;
984 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
986 tree
987 fix_string_type (tree value)
989 int length = TREE_STRING_LENGTH (value);
990 int nchars;
991 tree e_type, i_type, a_type;
993 /* Compute the number of elements, for the array type. */
994 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
996 nchars = length;
997 e_type = char_type_node;
999 else if (TREE_TYPE (value) == char16_array_type_node)
1001 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1002 e_type = char16_type_node;
1004 else if (TREE_TYPE (value) == char32_array_type_node)
1006 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1007 e_type = char32_type_node;
1009 else
1011 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1012 e_type = wchar_type_node;
1015 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1016 limit in C++98 Annex B is very large (65536) and is not normative,
1017 so we do not diagnose it (warn_overlength_strings is forced off
1018 in c_common_post_options). */
1019 if (warn_overlength_strings)
1021 const int nchars_max = flag_isoc99 ? 4095 : 509;
1022 const int relevant_std = flag_isoc99 ? 99 : 90;
1023 if (nchars - 1 > nchars_max)
1024 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1025 separate the %d from the 'C'. 'ISO' should not be
1026 translated, but it may be moved after 'C%d' in languages
1027 where modifiers follow nouns. */
1028 pedwarn (input_location, OPT_Woverlength_strings,
1029 "string length %qd is greater than the length %qd "
1030 "ISO C%d compilers are required to support",
1031 nchars - 1, nchars_max, relevant_std);
1034 /* Create the array type for the string constant. The ISO C++
1035 standard says that a string literal has type `const char[N]' or
1036 `const wchar_t[N]'. We use the same logic when invoked as a C
1037 front-end with -Wwrite-strings.
1038 ??? We should change the type of an expression depending on the
1039 state of a warning flag. We should just be warning -- see how
1040 this is handled in the C++ front-end for the deprecated implicit
1041 conversion from string literals to `char*' or `wchar_t*'.
1043 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1044 array type being the unqualified version of that type.
1045 Therefore, if we are constructing an array of const char, we must
1046 construct the matching unqualified array type first. The C front
1047 end does not require this, but it does no harm, so we do it
1048 unconditionally. */
1049 i_type = build_index_type (size_int (nchars - 1));
1050 a_type = build_array_type (e_type, i_type);
1051 if (c_dialect_cxx() || warn_write_strings)
1052 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1054 TREE_TYPE (value) = a_type;
1055 TREE_CONSTANT (value) = 1;
1056 TREE_READONLY (value) = 1;
1057 TREE_STATIC (value) = 1;
1058 return value;
1061 /* If DISABLE is true, stop issuing warnings. This is used when
1062 parsing code that we know will not be executed. This function may
1063 be called multiple times, and works as a stack. */
1065 static void
1066 c_disable_warnings (bool disable)
1068 if (disable)
1070 ++c_inhibit_evaluation_warnings;
1071 fold_defer_overflow_warnings ();
1075 /* If ENABLE is true, reenable issuing warnings. */
1077 static void
1078 c_enable_warnings (bool enable)
1080 if (enable)
1082 --c_inhibit_evaluation_warnings;
1083 fold_undefer_and_ignore_overflow_warnings ();
1087 /* Fully fold EXPR, an expression that was not folded (beyond integer
1088 constant expressions and null pointer constants) when being built
1089 up. If IN_INIT, this is in a static initializer and certain
1090 changes are made to the folding done. Clear *MAYBE_CONST if
1091 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1092 expression because it contains an evaluated operator (in C99) or an
1093 operator outside of sizeof returning an integer constant (in C90)
1094 not permitted in constant expressions, or because it contains an
1095 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1096 set to true by callers before calling this function.) Return the
1097 folded expression. Function arguments have already been folded
1098 before calling this function, as have the contents of SAVE_EXPR,
1099 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1100 C_MAYBE_CONST_EXPR. */
1102 tree
1103 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1105 tree ret;
1106 tree eptype = NULL_TREE;
1107 bool dummy = true;
1108 bool maybe_const_itself = true;
1109 location_t loc = EXPR_LOCATION (expr);
1111 /* This function is not relevant to C++ because C++ folds while
1112 parsing, and may need changes to be correct for C++ when C++
1113 stops folding while parsing. */
1114 if (c_dialect_cxx ())
1115 gcc_unreachable ();
1117 if (!maybe_const)
1118 maybe_const = &dummy;
1119 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1121 eptype = TREE_TYPE (expr);
1122 expr = TREE_OPERAND (expr, 0);
1124 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1125 &maybe_const_itself);
1126 if (eptype)
1127 ret = fold_convert_loc (loc, eptype, ret);
1128 *maybe_const &= maybe_const_itself;
1129 return ret;
1132 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1133 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1134 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1135 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1136 both evaluated and unevaluated subexpressions while
1137 *MAYBE_CONST_ITSELF is carried from only evaluated
1138 subexpressions). */
1140 static tree
1141 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1142 bool *maybe_const_itself)
1144 tree ret = expr;
1145 enum tree_code code = TREE_CODE (expr);
1146 enum tree_code_class kind = TREE_CODE_CLASS (code);
1147 location_t loc = EXPR_LOCATION (expr);
1148 tree op0, op1, op2, op3;
1149 tree orig_op0, orig_op1, orig_op2;
1150 bool op0_const = true, op1_const = true, op2_const = true;
1151 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1152 bool nowarning = TREE_NO_WARNING (expr);
1153 bool unused_p;
1155 /* This function is not relevant to C++ because C++ folds while
1156 parsing, and may need changes to be correct for C++ when C++
1157 stops folding while parsing. */
1158 if (c_dialect_cxx ())
1159 gcc_unreachable ();
1161 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1162 anything else not counted as an expression cannot usefully be
1163 folded further at this point. */
1164 if (!IS_EXPR_CODE_CLASS (kind)
1165 || kind == tcc_statement
1166 || code == SAVE_EXPR)
1167 return expr;
1169 /* Operands of variable-length expressions (function calls) have
1170 already been folded, as have __builtin_* function calls, and such
1171 expressions cannot occur in constant expressions. */
1172 if (kind == tcc_vl_exp)
1174 *maybe_const_operands = false;
1175 ret = fold (expr);
1176 goto out;
1179 if (code == C_MAYBE_CONST_EXPR)
1181 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1182 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1183 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1184 *maybe_const_operands = false;
1185 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1186 *maybe_const_itself = false;
1187 if (pre && !in_init)
1188 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1189 else
1190 ret = inner;
1191 goto out;
1194 /* Assignment, increment, decrement, function call and comma
1195 operators, and statement expressions, cannot occur in constant
1196 expressions if evaluated / outside of sizeof. (Function calls
1197 were handled above, though VA_ARG_EXPR is treated like a function
1198 call here, and statement expressions are handled through
1199 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1200 switch (code)
1202 case MODIFY_EXPR:
1203 case PREDECREMENT_EXPR:
1204 case PREINCREMENT_EXPR:
1205 case POSTDECREMENT_EXPR:
1206 case POSTINCREMENT_EXPR:
1207 case COMPOUND_EXPR:
1208 *maybe_const_operands = false;
1209 break;
1211 case VA_ARG_EXPR:
1212 case TARGET_EXPR:
1213 case BIND_EXPR:
1214 case OBJ_TYPE_REF:
1215 *maybe_const_operands = false;
1216 ret = fold (expr);
1217 goto out;
1219 default:
1220 break;
1223 /* Fold individual tree codes as appropriate. */
1224 switch (code)
1226 case COMPOUND_LITERAL_EXPR:
1227 /* Any non-constancy will have been marked in a containing
1228 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1229 goto out;
1231 case COMPONENT_REF:
1232 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1233 op1 = TREE_OPERAND (expr, 1);
1234 op2 = TREE_OPERAND (expr, 2);
1235 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1236 maybe_const_itself);
1237 STRIP_TYPE_NOPS (op0);
1238 if (op0 != orig_op0)
1239 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1240 if (ret != expr)
1242 TREE_READONLY (ret) = TREE_READONLY (expr);
1243 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1245 goto out;
1247 case ARRAY_REF:
1248 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1249 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1250 op2 = TREE_OPERAND (expr, 2);
1251 op3 = TREE_OPERAND (expr, 3);
1252 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1253 maybe_const_itself);
1254 STRIP_TYPE_NOPS (op0);
1255 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1256 maybe_const_itself);
1257 STRIP_TYPE_NOPS (op1);
1258 op1 = decl_constant_value_for_optimization (op1);
1259 if (op0 != orig_op0 || op1 != orig_op1)
1260 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1261 if (ret != expr)
1263 TREE_READONLY (ret) = TREE_READONLY (expr);
1264 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1265 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1267 ret = fold (ret);
1268 goto out;
1270 case COMPOUND_EXPR:
1271 case MODIFY_EXPR:
1272 case PREDECREMENT_EXPR:
1273 case PREINCREMENT_EXPR:
1274 case POSTDECREMENT_EXPR:
1275 case POSTINCREMENT_EXPR:
1276 case PLUS_EXPR:
1277 case MINUS_EXPR:
1278 case MULT_EXPR:
1279 case POINTER_PLUS_EXPR:
1280 case TRUNC_DIV_EXPR:
1281 case CEIL_DIV_EXPR:
1282 case FLOOR_DIV_EXPR:
1283 case TRUNC_MOD_EXPR:
1284 case RDIV_EXPR:
1285 case EXACT_DIV_EXPR:
1286 case LSHIFT_EXPR:
1287 case RSHIFT_EXPR:
1288 case BIT_IOR_EXPR:
1289 case BIT_XOR_EXPR:
1290 case BIT_AND_EXPR:
1291 case LT_EXPR:
1292 case LE_EXPR:
1293 case GT_EXPR:
1294 case GE_EXPR:
1295 case EQ_EXPR:
1296 case NE_EXPR:
1297 case COMPLEX_EXPR:
1298 case TRUTH_AND_EXPR:
1299 case TRUTH_OR_EXPR:
1300 case TRUTH_XOR_EXPR:
1301 case UNORDERED_EXPR:
1302 case ORDERED_EXPR:
1303 case UNLT_EXPR:
1304 case UNLE_EXPR:
1305 case UNGT_EXPR:
1306 case UNGE_EXPR:
1307 case UNEQ_EXPR:
1308 /* Binary operations evaluating both arguments (increment and
1309 decrement are binary internally in GCC). */
1310 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1311 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1312 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1313 maybe_const_itself);
1314 STRIP_TYPE_NOPS (op0);
1315 if (code != MODIFY_EXPR
1316 && code != PREDECREMENT_EXPR
1317 && code != PREINCREMENT_EXPR
1318 && code != POSTDECREMENT_EXPR
1319 && code != POSTINCREMENT_EXPR)
1320 op0 = decl_constant_value_for_optimization (op0);
1321 /* The RHS of a MODIFY_EXPR was fully folded when building that
1322 expression for the sake of conversion warnings. */
1323 if (code != MODIFY_EXPR)
1324 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1325 maybe_const_itself);
1326 STRIP_TYPE_NOPS (op1);
1327 op1 = decl_constant_value_for_optimization (op1);
1328 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1329 ret = in_init
1330 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1331 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1332 else
1333 ret = fold (expr);
1334 if (TREE_OVERFLOW_P (ret)
1335 && !TREE_OVERFLOW_P (op0)
1336 && !TREE_OVERFLOW_P (op1))
1337 overflow_warning (EXPR_LOCATION (expr), ret);
1338 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1339 && TREE_CODE (orig_op1) != INTEGER_CST
1340 && TREE_CODE (op1) == INTEGER_CST
1341 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1342 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1343 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1344 && c_inhibit_evaluation_warnings == 0)
1346 if (tree_int_cst_sgn (op1) < 0)
1347 warning_at (loc, 0, (code == LSHIFT_EXPR
1348 ? G_("left shift count is negative")
1349 : G_("right shift count is negative")));
1350 else if (compare_tree_int (op1,
1351 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1352 >= 0)
1353 warning_at (loc, 0, (code == LSHIFT_EXPR
1354 ? G_("left shift count >= width of type")
1355 : G_("right shift count >= width of type")));
1357 goto out;
1359 case INDIRECT_REF:
1360 case FIX_TRUNC_EXPR:
1361 case FLOAT_EXPR:
1362 CASE_CONVERT:
1363 case ADDR_SPACE_CONVERT_EXPR:
1364 case VIEW_CONVERT_EXPR:
1365 case NON_LVALUE_EXPR:
1366 case NEGATE_EXPR:
1367 case BIT_NOT_EXPR:
1368 case TRUTH_NOT_EXPR:
1369 case ADDR_EXPR:
1370 case CONJ_EXPR:
1371 case REALPART_EXPR:
1372 case IMAGPART_EXPR:
1373 /* Unary operations. */
1374 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1375 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1376 maybe_const_itself);
1377 STRIP_TYPE_NOPS (op0);
1378 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1379 op0 = decl_constant_value_for_optimization (op0);
1380 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1381 not prepared to deal with them if they occur in initializers.
1382 Avoid attempts to fold references to UPC shared components
1383 due to the complexities of UPC pointer-to-shared arithmetic. */
1384 if (op0 != orig_op0
1385 && code == ADDR_EXPR
1386 && (op1 = get_base_address (op0)) != NULL_TREE
1387 && TREE_CODE (op1) == INDIRECT_REF
1388 && !upc_shared_type_p (TREE_TYPE (op1))
1389 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1390 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1391 else if (op0 != orig_op0 || in_init)
1392 ret = in_init
1393 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1394 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1395 else
1396 ret = fold (expr);
1397 if (code == INDIRECT_REF
1398 && ret != expr
1399 && TREE_CODE (ret) == INDIRECT_REF)
1401 TREE_READONLY (ret) = TREE_READONLY (expr);
1402 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1403 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1405 switch (code)
1407 case FIX_TRUNC_EXPR:
1408 case FLOAT_EXPR:
1409 CASE_CONVERT:
1410 /* Don't warn about explicit conversions. We will already
1411 have warned about suspect implicit conversions. */
1412 break;
1414 default:
1415 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1416 overflow_warning (EXPR_LOCATION (expr), ret);
1417 break;
1419 goto out;
1421 case TRUTH_ANDIF_EXPR:
1422 case TRUTH_ORIF_EXPR:
1423 /* Binary operations not necessarily evaluating both
1424 arguments. */
1425 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1426 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1427 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1428 STRIP_TYPE_NOPS (op0);
1430 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1431 ? truthvalue_false_node
1432 : truthvalue_true_node));
1433 c_disable_warnings (unused_p);
1434 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1435 STRIP_TYPE_NOPS (op1);
1436 c_enable_warnings (unused_p);
1438 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1439 ret = in_init
1440 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1441 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1442 else
1443 ret = fold (expr);
1444 *maybe_const_operands &= op0_const;
1445 *maybe_const_itself &= op0_const_self;
1446 if (!(flag_isoc99
1447 && op0_const
1448 && op0_const_self
1449 && (code == TRUTH_ANDIF_EXPR
1450 ? op0 == truthvalue_false_node
1451 : op0 == truthvalue_true_node)))
1452 *maybe_const_operands &= op1_const;
1453 if (!(op0_const
1454 && op0_const_self
1455 && (code == TRUTH_ANDIF_EXPR
1456 ? op0 == truthvalue_false_node
1457 : op0 == truthvalue_true_node)))
1458 *maybe_const_itself &= op1_const_self;
1459 goto out;
1461 case COND_EXPR:
1462 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1463 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1464 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1465 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1467 STRIP_TYPE_NOPS (op0);
1468 c_disable_warnings (op0 == truthvalue_false_node);
1469 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1470 STRIP_TYPE_NOPS (op1);
1471 c_enable_warnings (op0 == truthvalue_false_node);
1473 c_disable_warnings (op0 == truthvalue_true_node);
1474 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1475 STRIP_TYPE_NOPS (op2);
1476 c_enable_warnings (op0 == truthvalue_true_node);
1478 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1479 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1480 else
1481 ret = fold (expr);
1482 *maybe_const_operands &= op0_const;
1483 *maybe_const_itself &= op0_const_self;
1484 if (!(flag_isoc99
1485 && op0_const
1486 && op0_const_self
1487 && op0 == truthvalue_false_node))
1488 *maybe_const_operands &= op1_const;
1489 if (!(op0_const
1490 && op0_const_self
1491 && op0 == truthvalue_false_node))
1492 *maybe_const_itself &= op1_const_self;
1493 if (!(flag_isoc99
1494 && op0_const
1495 && op0_const_self
1496 && op0 == truthvalue_true_node))
1497 *maybe_const_operands &= op2_const;
1498 if (!(op0_const
1499 && op0_const_self
1500 && op0 == truthvalue_true_node))
1501 *maybe_const_itself &= op2_const_self;
1502 goto out;
1504 case EXCESS_PRECISION_EXPR:
1505 /* Each case where an operand with excess precision may be
1506 encountered must remove the EXCESS_PRECISION_EXPR around
1507 inner operands and possibly put one around the whole
1508 expression or possibly convert to the semantic type (which
1509 c_fully_fold does); we cannot tell at this stage which is
1510 appropriate in any particular case. */
1511 gcc_unreachable ();
1513 default:
1514 /* Various codes may appear through folding built-in functions
1515 and their arguments. */
1516 goto out;
1519 out:
1520 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1521 have been done by this point, so remove them again. */
1522 nowarning |= TREE_NO_WARNING (ret);
1523 STRIP_TYPE_NOPS (ret);
1524 if (nowarning && !TREE_NO_WARNING (ret))
1526 if (!CAN_HAVE_LOCATION_P (ret))
1527 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1528 TREE_NO_WARNING (ret) = 1;
1530 if (ret != expr)
1531 protected_set_expr_location (ret, loc);
1532 return ret;
1535 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1536 return EXP. Otherwise, return either EXP or its known constant
1537 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1538 Is the BLKmode test appropriate? */
1540 tree
1541 decl_constant_value_for_optimization (tree exp)
1543 tree ret;
1545 /* This function is only used by C, for c_fully_fold and other
1546 optimization, and may not be correct for C++. */
1547 if (c_dialect_cxx ())
1548 gcc_unreachable ();
1550 if (!optimize
1551 || TREE_CODE (exp) != VAR_DECL
1552 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1553 || DECL_MODE (exp) == BLKmode)
1554 return exp;
1556 ret = decl_constant_value (exp);
1557 /* Avoid unwanted tree sharing between the initializer and current
1558 function's body where the tree can be modified e.g. by the
1559 gimplifier. */
1560 if (ret != exp && TREE_STATIC (exp))
1561 ret = unshare_expr (ret);
1562 return ret;
1565 /* Print a warning if a constant expression had overflow in folding.
1566 Invoke this function on every expression that the language
1567 requires to be a constant expression.
1568 Note the ANSI C standard says it is erroneous for a
1569 constant expression to overflow. */
1571 void
1572 constant_expression_warning (tree value)
1574 if (warn_overflow && pedantic
1575 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1576 || TREE_CODE (value) == FIXED_CST
1577 || TREE_CODE (value) == VECTOR_CST
1578 || TREE_CODE (value) == COMPLEX_CST)
1579 && TREE_OVERFLOW (value))
1580 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1583 /* The same as above but print an unconditional error. */
1584 void
1585 constant_expression_error (tree value)
1587 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1588 || TREE_CODE (value) == FIXED_CST
1589 || TREE_CODE (value) == VECTOR_CST
1590 || TREE_CODE (value) == COMPLEX_CST)
1591 && TREE_OVERFLOW (value))
1592 error ("overflow in constant expression");
1595 /* Print a warning if an expression had overflow in folding and its
1596 operands hadn't.
1598 Invoke this function on every expression that
1599 (1) appears in the source code, and
1600 (2) is a constant expression that overflowed, and
1601 (3) is not already checked by convert_and_check;
1602 however, do not invoke this function on operands of explicit casts
1603 or when the expression is the result of an operator and any operand
1604 already overflowed. */
1606 void
1607 overflow_warning (location_t loc, tree value)
1609 if (c_inhibit_evaluation_warnings != 0)
1610 return;
1612 switch (TREE_CODE (value))
1614 case INTEGER_CST:
1615 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1616 break;
1618 case REAL_CST:
1619 warning_at (loc, OPT_Woverflow,
1620 "floating point overflow in expression");
1621 break;
1623 case FIXED_CST:
1624 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1625 break;
1627 case VECTOR_CST:
1628 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1629 break;
1631 case COMPLEX_CST:
1632 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1633 warning_at (loc, OPT_Woverflow,
1634 "complex integer overflow in expression");
1635 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1636 warning_at (loc, OPT_Woverflow,
1637 "complex floating point overflow in expression");
1638 break;
1640 default:
1641 break;
1645 /* Warn about uses of logical || / && operator in a context where it
1646 is likely that the bitwise equivalent was intended by the
1647 programmer. We have seen an expression in which CODE is a binary
1648 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1649 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1650 void
1651 warn_logical_operator (location_t location, enum tree_code code, tree type,
1652 enum tree_code code_left, tree op_left,
1653 enum tree_code ARG_UNUSED (code_right), tree op_right)
1655 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1656 int in0_p, in1_p, in_p;
1657 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1658 bool strict_overflow_p = false;
1660 if (code != TRUTH_ANDIF_EXPR
1661 && code != TRUTH_AND_EXPR
1662 && code != TRUTH_ORIF_EXPR
1663 && code != TRUTH_OR_EXPR)
1664 return;
1666 /* Warn if &&/|| are being used in a context where it is
1667 likely that the bitwise equivalent was intended by the
1668 programmer. That is, an expression such as op && MASK
1669 where op should not be any boolean expression, nor a
1670 constant, and mask seems to be a non-boolean integer constant. */
1671 if (!truth_value_p (code_left)
1672 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1673 && !CONSTANT_CLASS_P (op_left)
1674 && !TREE_NO_WARNING (op_left)
1675 && TREE_CODE (op_right) == INTEGER_CST
1676 && !integer_zerop (op_right)
1677 && !integer_onep (op_right))
1679 if (or_op)
1680 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1681 " applied to non-boolean constant");
1682 else
1683 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1684 " applied to non-boolean constant");
1685 TREE_NO_WARNING (op_left) = true;
1686 return;
1689 /* We do not warn for constants because they are typical of macro
1690 expansions that test for features. */
1691 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1692 return;
1694 /* This warning only makes sense with logical operands. */
1695 if (!(truth_value_p (TREE_CODE (op_left))
1696 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1697 || !(truth_value_p (TREE_CODE (op_right))
1698 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1699 return;
1702 /* We first test whether either side separately is trivially true
1703 (with OR) or trivially false (with AND). If so, do not warn.
1704 This is a common idiom for testing ranges of data types in
1705 portable code. */
1706 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1707 if (!lhs)
1708 return;
1709 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1710 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1712 /* If this is an OR operation, invert both sides; now, the result
1713 should be always false to get a warning. */
1714 if (or_op)
1715 in0_p = !in0_p;
1717 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1718 if (tem && integer_zerop (tem))
1719 return;
1721 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1722 if (!rhs)
1723 return;
1724 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1725 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1727 /* If this is an OR operation, invert both sides; now, the result
1728 should be always false to get a warning. */
1729 if (or_op)
1730 in1_p = !in1_p;
1732 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1733 if (tem && integer_zerop (tem))
1734 return;
1736 /* If both expressions have the same operand, if we can merge the
1737 ranges, and if the range test is always false, then warn. */
1738 if (operand_equal_p (lhs, rhs, 0)
1739 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1740 in1_p, low1, high1)
1741 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1742 type, lhs, in_p, low, high))
1743 && integer_zerop (tem))
1745 if (or_op)
1746 warning_at (location, OPT_Wlogical_op,
1747 "logical %<or%> "
1748 "of collectively exhaustive tests is always true");
1749 else
1750 warning_at (location, OPT_Wlogical_op,
1751 "logical %<and%> "
1752 "of mutually exclusive tests is always false");
1757 /* Warn if EXP contains any computations whose results are not used.
1758 Return true if a warning is printed; false otherwise. LOCUS is the
1759 (potential) location of the expression. */
1761 bool
1762 warn_if_unused_value (const_tree exp, location_t locus)
1764 restart:
1765 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1766 return false;
1768 /* Don't warn about void constructs. This includes casting to void,
1769 void function calls, and statement expressions with a final cast
1770 to void. */
1771 if (VOID_TYPE_P (TREE_TYPE (exp)))
1772 return false;
1774 if (EXPR_HAS_LOCATION (exp))
1775 locus = EXPR_LOCATION (exp);
1777 switch (TREE_CODE (exp))
1779 case PREINCREMENT_EXPR:
1780 case POSTINCREMENT_EXPR:
1781 case PREDECREMENT_EXPR:
1782 case POSTDECREMENT_EXPR:
1783 case MODIFY_EXPR:
1784 case INIT_EXPR:
1785 case TARGET_EXPR:
1786 case CALL_EXPR:
1787 case TRY_CATCH_EXPR:
1788 case WITH_CLEANUP_EXPR:
1789 case EXIT_EXPR:
1790 case VA_ARG_EXPR:
1791 return false;
1793 case BIND_EXPR:
1794 /* For a binding, warn if no side effect within it. */
1795 exp = BIND_EXPR_BODY (exp);
1796 goto restart;
1798 case SAVE_EXPR:
1799 case NON_LVALUE_EXPR:
1800 case NOP_EXPR:
1801 exp = TREE_OPERAND (exp, 0);
1802 goto restart;
1804 case TRUTH_ORIF_EXPR:
1805 case TRUTH_ANDIF_EXPR:
1806 /* In && or ||, warn if 2nd operand has no side effect. */
1807 exp = TREE_OPERAND (exp, 1);
1808 goto restart;
1810 case COMPOUND_EXPR:
1811 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1812 return true;
1813 /* Let people do `(foo (), 0)' without a warning. */
1814 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1815 return false;
1816 exp = TREE_OPERAND (exp, 1);
1817 goto restart;
1819 case COND_EXPR:
1820 /* If this is an expression with side effects, don't warn; this
1821 case commonly appears in macro expansions. */
1822 if (TREE_SIDE_EFFECTS (exp))
1823 return false;
1824 goto warn;
1826 case INDIRECT_REF:
1827 /* Don't warn about automatic dereferencing of references, since
1828 the user cannot control it. */
1829 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1831 exp = TREE_OPERAND (exp, 0);
1832 goto restart;
1834 /* Fall through. */
1836 default:
1837 /* Referencing a volatile value is a side effect, so don't warn. */
1838 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1839 && TREE_THIS_VOLATILE (exp))
1840 return false;
1842 /* If this is an expression which has no operands, there is no value
1843 to be unused. There are no such language-independent codes,
1844 but front ends may define such. */
1845 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1846 return false;
1848 warn:
1849 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1854 /* Print a warning about casts that might indicate violation
1855 of strict aliasing rules if -Wstrict-aliasing is used and
1856 strict aliasing mode is in effect. OTYPE is the original
1857 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1859 bool
1860 strict_aliasing_warning (tree otype, tree type, tree expr)
1862 /* Strip pointer conversion chains and get to the correct original type. */
1863 STRIP_NOPS (expr);
1864 otype = TREE_TYPE (expr);
1866 if (!(flag_strict_aliasing
1867 && POINTER_TYPE_P (type)
1868 && POINTER_TYPE_P (otype)
1869 && !VOID_TYPE_P (TREE_TYPE (type)))
1870 /* If the type we are casting to is a ref-all pointer
1871 dereferencing it is always valid. */
1872 || TYPE_REF_CAN_ALIAS_ALL (type))
1873 return false;
1875 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1876 && (DECL_P (TREE_OPERAND (expr, 0))
1877 || handled_component_p (TREE_OPERAND (expr, 0))))
1879 /* Casting the address of an object to non void pointer. Warn
1880 if the cast breaks type based aliasing. */
1881 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1883 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1884 "might break strict-aliasing rules");
1885 return true;
1887 else
1889 /* warn_strict_aliasing >= 3. This includes the default (3).
1890 Only warn if the cast is dereferenced immediately. */
1891 alias_set_type set1 =
1892 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1893 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1895 if (set1 != set2 && set2 != 0
1896 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1898 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1899 "pointer will break strict-aliasing rules");
1900 return true;
1902 else if (warn_strict_aliasing == 2
1903 && !alias_sets_must_conflict_p (set1, set2))
1905 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1906 "pointer might break strict-aliasing rules");
1907 return true;
1911 else
1912 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1914 /* At this level, warn for any conversions, even if an address is
1915 not taken in the same statement. This will likely produce many
1916 false positives, but could be useful to pinpoint problems that
1917 are not revealed at higher levels. */
1918 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1919 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1920 if (!COMPLETE_TYPE_P (type)
1921 || !alias_sets_must_conflict_p (set1, set2))
1923 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1924 "pointer might break strict-aliasing rules");
1925 return true;
1929 return false;
1932 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1933 sizeof as last operand of certain builtins. */
1935 void
1936 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1937 vec<tree, va_gc> *params, tree *sizeof_arg,
1938 bool (*comp_types) (tree, tree))
1940 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1941 bool strop = false, cmp = false;
1942 unsigned int idx = ~0;
1943 location_t loc;
1945 if (TREE_CODE (callee) != FUNCTION_DECL
1946 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1947 || vec_safe_length (params) <= 1)
1948 return;
1950 switch (DECL_FUNCTION_CODE (callee))
1952 case BUILT_IN_STRNCMP:
1953 case BUILT_IN_STRNCASECMP:
1954 cmp = true;
1955 /* FALLTHRU */
1956 case BUILT_IN_STRNCPY:
1957 case BUILT_IN_STRNCPY_CHK:
1958 case BUILT_IN_STRNCAT:
1959 case BUILT_IN_STRNCAT_CHK:
1960 case BUILT_IN_STPNCPY:
1961 case BUILT_IN_STPNCPY_CHK:
1962 strop = true;
1963 /* FALLTHRU */
1964 case BUILT_IN_MEMCPY:
1965 case BUILT_IN_MEMCPY_CHK:
1966 case BUILT_IN_MEMMOVE:
1967 case BUILT_IN_MEMMOVE_CHK:
1968 if (params->length () < 3)
1969 return;
1970 src = (*params)[1];
1971 dest = (*params)[0];
1972 idx = 2;
1973 break;
1974 case BUILT_IN_BCOPY:
1975 if (params->length () < 3)
1976 return;
1977 src = (*params)[0];
1978 dest = (*params)[1];
1979 idx = 2;
1980 break;
1981 case BUILT_IN_MEMCMP:
1982 case BUILT_IN_BCMP:
1983 if (params->length () < 3)
1984 return;
1985 src = (*params)[1];
1986 dest = (*params)[0];
1987 idx = 2;
1988 cmp = true;
1989 break;
1990 case BUILT_IN_MEMSET:
1991 case BUILT_IN_MEMSET_CHK:
1992 if (params->length () < 3)
1993 return;
1994 dest = (*params)[0];
1995 idx = 2;
1996 break;
1997 case BUILT_IN_BZERO:
1998 dest = (*params)[0];
1999 idx = 1;
2000 break;
2001 case BUILT_IN_STRNDUP:
2002 src = (*params)[0];
2003 strop = true;
2004 idx = 1;
2005 break;
2006 case BUILT_IN_MEMCHR:
2007 if (params->length () < 3)
2008 return;
2009 src = (*params)[0];
2010 idx = 2;
2011 break;
2012 case BUILT_IN_SNPRINTF:
2013 case BUILT_IN_SNPRINTF_CHK:
2014 case BUILT_IN_VSNPRINTF:
2015 case BUILT_IN_VSNPRINTF_CHK:
2016 dest = (*params)[0];
2017 idx = 1;
2018 strop = true;
2019 break;
2020 default:
2021 break;
2024 if (idx >= 3)
2025 return;
2027 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2028 return;
2030 type = TYPE_P (sizeof_arg[idx])
2031 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2032 if (!POINTER_TYPE_P (type))
2033 return;
2035 if (dest
2036 && (tem = tree_strip_nop_conversions (dest))
2037 && POINTER_TYPE_P (TREE_TYPE (tem))
2038 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2039 return;
2041 if (src
2042 && (tem = tree_strip_nop_conversions (src))
2043 && POINTER_TYPE_P (TREE_TYPE (tem))
2044 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2045 return;
2047 loc = sizeof_arg_loc[idx];
2049 if (dest && !cmp)
2051 if (!TYPE_P (sizeof_arg[idx])
2052 && operand_equal_p (dest, sizeof_arg[idx], 0)
2053 && comp_types (TREE_TYPE (dest), type))
2055 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2056 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2057 "argument to %<sizeof%> in %qD call is the same "
2058 "expression as the destination; did you mean to "
2059 "remove the addressof?", callee);
2060 else if ((TYPE_PRECISION (TREE_TYPE (type))
2061 == TYPE_PRECISION (char_type_node))
2062 || strop)
2063 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2064 "argument to %<sizeof%> in %qD call is the same "
2065 "expression as the destination; did you mean to "
2066 "provide an explicit length?", callee);
2067 else
2068 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2069 "argument to %<sizeof%> in %qD call is the same "
2070 "expression as the destination; did you mean to "
2071 "dereference it?", callee);
2072 return;
2075 if (POINTER_TYPE_P (TREE_TYPE (dest))
2076 && !strop
2077 && comp_types (TREE_TYPE (dest), type)
2078 && !VOID_TYPE_P (TREE_TYPE (type)))
2080 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2081 "argument to %<sizeof%> in %qD call is the same "
2082 "pointer type %qT as the destination; expected %qT "
2083 "or an explicit length", callee, TREE_TYPE (dest),
2084 TREE_TYPE (TREE_TYPE (dest)));
2085 return;
2089 if (src && !cmp)
2091 if (!TYPE_P (sizeof_arg[idx])
2092 && operand_equal_p (src, sizeof_arg[idx], 0)
2093 && comp_types (TREE_TYPE (src), type))
2095 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2096 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2097 "argument to %<sizeof%> in %qD call is the same "
2098 "expression as the source; did you mean to "
2099 "remove the addressof?", callee);
2100 else if ((TYPE_PRECISION (TREE_TYPE (type))
2101 == TYPE_PRECISION (char_type_node))
2102 || strop)
2103 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2104 "argument to %<sizeof%> in %qD call is the same "
2105 "expression as the source; did you mean to "
2106 "provide an explicit length?", callee);
2107 else
2108 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2109 "argument to %<sizeof%> in %qD call is the same "
2110 "expression as the source; did you mean to "
2111 "dereference it?", callee);
2112 return;
2115 if (POINTER_TYPE_P (TREE_TYPE (src))
2116 && !strop
2117 && comp_types (TREE_TYPE (src), type)
2118 && !VOID_TYPE_P (TREE_TYPE (type)))
2120 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2121 "argument to %<sizeof%> in %qD call is the same "
2122 "pointer type %qT as the source; expected %qT "
2123 "or an explicit length", callee, TREE_TYPE (src),
2124 TREE_TYPE (TREE_TYPE (src)));
2125 return;
2129 if (dest)
2131 if (!TYPE_P (sizeof_arg[idx])
2132 && operand_equal_p (dest, sizeof_arg[idx], 0)
2133 && comp_types (TREE_TYPE (dest), type))
2135 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2136 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2137 "argument to %<sizeof%> in %qD call is the same "
2138 "expression as the first source; did you mean to "
2139 "remove the addressof?", callee);
2140 else if ((TYPE_PRECISION (TREE_TYPE (type))
2141 == TYPE_PRECISION (char_type_node))
2142 || strop)
2143 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2144 "argument to %<sizeof%> in %qD call is the same "
2145 "expression as the first source; did you mean to "
2146 "provide an explicit length?", callee);
2147 else
2148 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2149 "argument to %<sizeof%> in %qD call is the same "
2150 "expression as the first source; did you mean to "
2151 "dereference it?", callee);
2152 return;
2155 if (POINTER_TYPE_P (TREE_TYPE (dest))
2156 && !strop
2157 && comp_types (TREE_TYPE (dest), type)
2158 && !VOID_TYPE_P (TREE_TYPE (type)))
2160 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2161 "argument to %<sizeof%> in %qD call is the same "
2162 "pointer type %qT as the first source; expected %qT "
2163 "or an explicit length", callee, TREE_TYPE (dest),
2164 TREE_TYPE (TREE_TYPE (dest)));
2165 return;
2169 if (src)
2171 if (!TYPE_P (sizeof_arg[idx])
2172 && operand_equal_p (src, sizeof_arg[idx], 0)
2173 && comp_types (TREE_TYPE (src), type))
2175 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2176 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2177 "argument to %<sizeof%> in %qD call is the same "
2178 "expression as the second source; did you mean to "
2179 "remove the addressof?", callee);
2180 else if ((TYPE_PRECISION (TREE_TYPE (type))
2181 == TYPE_PRECISION (char_type_node))
2182 || strop)
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "expression as the second source; did you mean to "
2186 "provide an explicit length?", callee);
2187 else
2188 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "expression as the second source; did you mean to "
2191 "dereference it?", callee);
2192 return;
2195 if (POINTER_TYPE_P (TREE_TYPE (src))
2196 && !strop
2197 && comp_types (TREE_TYPE (src), type)
2198 && !VOID_TYPE_P (TREE_TYPE (type)))
2200 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2201 "argument to %<sizeof%> in %qD call is the same "
2202 "pointer type %qT as the second source; expected %qT "
2203 "or an explicit length", callee, TREE_TYPE (src),
2204 TREE_TYPE (TREE_TYPE (src)));
2205 return;
2211 /* Warn for unlikely, improbable, or stupid DECL declarations
2212 of `main'. */
2214 void
2215 check_main_parameter_types (tree decl)
2217 function_args_iterator iter;
2218 tree type;
2219 int argct = 0;
2221 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2223 /* XXX void_type_node belies the abstraction. */
2224 if (type == void_type_node || type == error_mark_node )
2225 break;
2227 ++argct;
2228 switch (argct)
2230 case 1:
2231 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2232 pedwarn (input_location, OPT_Wmain,
2233 "first argument of %q+D should be %<int%>", decl);
2234 break;
2236 case 2:
2237 if (TREE_CODE (type) != POINTER_TYPE
2238 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2239 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2240 != char_type_node))
2241 pedwarn (input_location, OPT_Wmain,
2242 "second argument of %q+D should be %<char **%>", decl);
2243 break;
2245 case 3:
2246 if (TREE_CODE (type) != POINTER_TYPE
2247 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2248 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2249 != char_type_node))
2250 pedwarn (input_location, OPT_Wmain,
2251 "third argument of %q+D should probably be "
2252 "%<char **%>", decl);
2253 break;
2257 /* It is intentional that this message does not mention the third
2258 argument because it's only mentioned in an appendix of the
2259 standard. */
2260 if (argct > 0 && (argct < 2 || argct > 3))
2261 pedwarn (input_location, OPT_Wmain,
2262 "%q+D takes only zero or two arguments", decl);
2265 /* vector_targets_convertible_p is used for vector pointer types. The
2266 callers perform various checks that the qualifiers are satisfactory,
2267 while OTOH vector_targets_convertible_p ignores the number of elements
2268 in the vectors. That's fine with vector pointers as we can consider,
2269 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2270 and that does not require and conversion of the pointer values.
2271 In contrast, vector_types_convertible_p and
2272 vector_types_compatible_elements_p are used for vector value types. */
2273 /* True if pointers to distinct types T1 and T2 can be converted to
2274 each other without an explicit cast. Only returns true for opaque
2275 vector types. */
2276 bool
2277 vector_targets_convertible_p (const_tree t1, const_tree t2)
2279 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2280 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2281 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2282 return true;
2284 return false;
2287 /* vector_types_convertible_p is used for vector value types.
2288 It could in principle call vector_targets_convertible_p as a subroutine,
2289 but then the check for vector type would be duplicated with its callers,
2290 and also the purpose of vector_targets_convertible_p would become
2291 muddled.
2292 Where vector_types_convertible_p returns true, a conversion might still be
2293 needed to make the types match.
2294 In contrast, vector_targets_convertible_p is used for vector pointer
2295 values, and vector_types_compatible_elements_p is used specifically
2296 in the context for binary operators, as a check if use is possible without
2297 conversion. */
2298 /* True if vector types T1 and T2 can be converted to each other
2299 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2300 can only be converted with -flax-vector-conversions yet that is not
2301 in effect, emit a note telling the user about that option if such
2302 a note has not previously been emitted. */
2303 bool
2304 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2306 static bool emitted_lax_note = false;
2307 bool convertible_lax;
2309 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2310 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2311 return true;
2313 convertible_lax =
2314 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2315 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2316 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2317 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2318 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2320 if (!convertible_lax || flag_lax_vector_conversions)
2321 return convertible_lax;
2323 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2324 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2325 return true;
2327 if (emit_lax_note && !emitted_lax_note)
2329 emitted_lax_note = true;
2330 inform (input_location, "use -flax-vector-conversions to permit "
2331 "conversions between vectors with differing "
2332 "element types or numbers of subparts");
2335 return false;
2338 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2339 and have vector types, V0 has the same type as V1, and the number of
2340 elements of V0, V1, MASK is the same.
2342 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2343 called with two arguments. In this case implementation passes the
2344 first argument twice in order to share the same tree code. This fact
2345 could enable the mask-values being twice the vector length. This is
2346 an implementation accident and this semantics is not guaranteed to
2347 the user. */
2348 tree
2349 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2350 bool complain)
2352 tree ret;
2353 bool wrap = true;
2354 bool maybe_const = false;
2355 bool two_arguments = false;
2357 if (v1 == NULL_TREE)
2359 two_arguments = true;
2360 v1 = v0;
2363 if (v0 == error_mark_node || v1 == error_mark_node
2364 || mask == error_mark_node)
2365 return error_mark_node;
2367 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2368 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2370 if (complain)
2371 error_at (loc, "__builtin_shuffle last argument must "
2372 "be an integer vector");
2373 return error_mark_node;
2376 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2377 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2379 if (complain)
2380 error_at (loc, "__builtin_shuffle arguments must be vectors");
2381 return error_mark_node;
2384 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2386 if (complain)
2387 error_at (loc, "__builtin_shuffle argument vectors must be of "
2388 "the same type");
2389 return error_mark_node;
2392 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2393 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2394 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2395 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2397 if (complain)
2398 error_at (loc, "__builtin_shuffle number of elements of the "
2399 "argument vector(s) and the mask vector should "
2400 "be the same");
2401 return error_mark_node;
2404 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2405 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2407 if (complain)
2408 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2409 "must have the same size as inner type of the mask");
2410 return error_mark_node;
2413 if (!c_dialect_cxx ())
2415 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2416 v0 = c_fully_fold (v0, false, &maybe_const);
2417 wrap &= maybe_const;
2419 if (two_arguments)
2420 v1 = v0 = save_expr (v0);
2421 else
2423 v1 = c_fully_fold (v1, false, &maybe_const);
2424 wrap &= maybe_const;
2427 mask = c_fully_fold (mask, false, &maybe_const);
2428 wrap &= maybe_const;
2430 else if (two_arguments)
2431 v1 = v0 = save_expr (v0);
2433 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2435 if (!c_dialect_cxx () && !wrap)
2436 ret = c_wrap_maybe_const (ret, true);
2438 return ret;
2441 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2442 to integral type. */
2444 static tree
2445 c_common_get_narrower (tree op, int *unsignedp_ptr)
2447 op = get_narrower (op, unsignedp_ptr);
2449 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2450 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2452 /* C++0x scoped enumerations don't implicitly convert to integral
2453 type; if we stripped an explicit conversion to a larger type we
2454 need to replace it so common_type will still work. */
2455 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2456 TYPE_UNSIGNED (TREE_TYPE (op)));
2457 op = fold_convert (type, op);
2459 return op;
2462 /* This is a helper function of build_binary_op.
2464 For certain operations if both args were extended from the same
2465 smaller type, do the arithmetic in that type and then extend.
2467 BITWISE indicates a bitwise operation.
2468 For them, this optimization is safe only if
2469 both args are zero-extended or both are sign-extended.
2470 Otherwise, we might change the result.
2471 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2472 but calculated in (unsigned short) it would be (unsigned short)-1.
2474 tree
2475 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2477 int unsigned0, unsigned1;
2478 tree arg0, arg1;
2479 int uns;
2480 tree type;
2482 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2483 excessive narrowing when we call get_narrower below. For
2484 example, suppose that OP0 is of unsigned int extended
2485 from signed char and that RESULT_TYPE is long long int.
2486 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2487 like
2489 (long long int) (unsigned int) signed_char
2491 which get_narrower would narrow down to
2493 (unsigned int) signed char
2495 If we do not cast OP0 first, get_narrower would return
2496 signed_char, which is inconsistent with the case of the
2497 explicit cast. */
2498 op0 = convert (result_type, op0);
2499 op1 = convert (result_type, op1);
2501 arg0 = c_common_get_narrower (op0, &unsigned0);
2502 arg1 = c_common_get_narrower (op1, &unsigned1);
2504 /* UNS is 1 if the operation to be done is an unsigned one. */
2505 uns = TYPE_UNSIGNED (result_type);
2507 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2508 but it *requires* conversion to FINAL_TYPE. */
2510 if ((TYPE_PRECISION (TREE_TYPE (op0))
2511 == TYPE_PRECISION (TREE_TYPE (arg0)))
2512 && TREE_TYPE (op0) != result_type)
2513 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2514 if ((TYPE_PRECISION (TREE_TYPE (op1))
2515 == TYPE_PRECISION (TREE_TYPE (arg1)))
2516 && TREE_TYPE (op1) != result_type)
2517 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2519 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2521 /* For bitwise operations, signedness of nominal type
2522 does not matter. Consider only how operands were extended. */
2523 if (bitwise)
2524 uns = unsigned0;
2526 /* Note that in all three cases below we refrain from optimizing
2527 an unsigned operation on sign-extended args.
2528 That would not be valid. */
2530 /* Both args variable: if both extended in same way
2531 from same width, do it in that width.
2532 Do it unsigned if args were zero-extended. */
2533 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2534 < TYPE_PRECISION (result_type))
2535 && (TYPE_PRECISION (TREE_TYPE (arg1))
2536 == TYPE_PRECISION (TREE_TYPE (arg0)))
2537 && unsigned0 == unsigned1
2538 && (unsigned0 || !uns))
2539 return c_common_signed_or_unsigned_type
2540 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2542 else if (TREE_CODE (arg0) == INTEGER_CST
2543 && (unsigned1 || !uns)
2544 && (TYPE_PRECISION (TREE_TYPE (arg1))
2545 < TYPE_PRECISION (result_type))
2546 && (type
2547 = c_common_signed_or_unsigned_type (unsigned1,
2548 TREE_TYPE (arg1)))
2549 && !POINTER_TYPE_P (type)
2550 && int_fits_type_p (arg0, type))
2551 return type;
2553 else if (TREE_CODE (arg1) == INTEGER_CST
2554 && (unsigned0 || !uns)
2555 && (TYPE_PRECISION (TREE_TYPE (arg0))
2556 < TYPE_PRECISION (result_type))
2557 && (type
2558 = c_common_signed_or_unsigned_type (unsigned0,
2559 TREE_TYPE (arg0)))
2560 && !POINTER_TYPE_P (type)
2561 && int_fits_type_p (arg1, type))
2562 return type;
2564 return result_type;
2567 /* Checks if expression EXPR of real/integer type cannot be converted
2568 to the real/integer type TYPE. Function returns non-zero when:
2569 * EXPR is a constant which cannot be exactly converted to TYPE.
2570 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2571 for EXPR type and TYPE being both integers or both real.
2572 * EXPR is not a constant of real type and TYPE is an integer.
2573 * EXPR is not a constant of integer type which cannot be
2574 exactly converted to real type.
2575 Function allows conversions between types of different signedness and
2576 can return SAFE_CONVERSION (zero) in that case. Function can produce
2577 signedness warnings if PRODUCE_WARNS is true. */
2579 enum conversion_safety
2580 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2582 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2583 tree expr_type = TREE_TYPE (expr);
2584 loc = expansion_point_location_if_in_system_header (loc);
2586 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2588 /* Warn for real constant that is not an exact integer converted
2589 to integer type. */
2590 if (TREE_CODE (expr_type) == REAL_TYPE
2591 && TREE_CODE (type) == INTEGER_TYPE)
2593 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2594 give_warning = UNSAFE_REAL;
2596 /* Warn for an integer constant that does not fit into integer type. */
2597 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2598 && TREE_CODE (type) == INTEGER_TYPE
2599 && !int_fits_type_p (expr, type))
2601 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2602 && tree_int_cst_sgn (expr) < 0)
2604 if (produce_warns)
2605 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2606 " implicitly converted to unsigned type");
2608 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2610 if (produce_warns)
2611 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2612 " constant value to negative integer");
2614 else
2615 give_warning = UNSAFE_OTHER;
2617 else if (TREE_CODE (type) == REAL_TYPE)
2619 /* Warn for an integer constant that does not fit into real type. */
2620 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2622 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2623 if (!exact_real_truncate (TYPE_MODE (type), &a))
2624 give_warning = UNSAFE_REAL;
2626 /* Warn for a real constant that does not fit into a smaller
2627 real type. */
2628 else if (TREE_CODE (expr_type) == REAL_TYPE
2629 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2631 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2632 if (!exact_real_truncate (TYPE_MODE (type), &a))
2633 give_warning = UNSAFE_REAL;
2637 else
2639 /* Warn for real types converted to integer types. */
2640 if (TREE_CODE (expr_type) == REAL_TYPE
2641 && TREE_CODE (type) == INTEGER_TYPE)
2642 give_warning = UNSAFE_REAL;
2644 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2645 && TREE_CODE (type) == INTEGER_TYPE)
2647 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2648 expr = get_unwidened (expr, 0);
2649 expr_type = TREE_TYPE (expr);
2651 /* Don't warn for short y; short x = ((int)y & 0xff); */
2652 if (TREE_CODE (expr) == BIT_AND_EXPR
2653 || TREE_CODE (expr) == BIT_IOR_EXPR
2654 || TREE_CODE (expr) == BIT_XOR_EXPR)
2656 /* If both args were extended from a shortest type,
2657 use that type if that is safe. */
2658 expr_type = shorten_binary_op (expr_type,
2659 TREE_OPERAND (expr, 0),
2660 TREE_OPERAND (expr, 1),
2661 /* bitwise */1);
2663 if (TREE_CODE (expr) == BIT_AND_EXPR)
2665 tree op0 = TREE_OPERAND (expr, 0);
2666 tree op1 = TREE_OPERAND (expr, 1);
2667 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2668 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2670 /* If one of the operands is a non-negative constant
2671 that fits in the target type, then the type of the
2672 other operand does not matter. */
2673 if ((TREE_CODE (op0) == INTEGER_CST
2674 && int_fits_type_p (op0, c_common_signed_type (type))
2675 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2676 || (TREE_CODE (op1) == INTEGER_CST
2677 && int_fits_type_p (op1, c_common_signed_type (type))
2678 && int_fits_type_p (op1,
2679 c_common_unsigned_type (type))))
2680 return SAFE_CONVERSION;
2681 /* If constant is unsigned and fits in the target
2682 type, then the result will also fit. */
2683 else if ((TREE_CODE (op0) == INTEGER_CST
2684 && unsigned0
2685 && int_fits_type_p (op0, type))
2686 || (TREE_CODE (op1) == INTEGER_CST
2687 && unsigned1
2688 && int_fits_type_p (op1, type)))
2689 return SAFE_CONVERSION;
2692 /* Warn for integer types converted to smaller integer types. */
2693 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2694 give_warning = UNSAFE_OTHER;
2696 /* When they are the same width but different signedness,
2697 then the value may change. */
2698 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2699 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2700 /* Even when converted to a bigger type, if the type is
2701 unsigned but expr is signed, then negative values
2702 will be changed. */
2703 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2704 && produce_warns)
2705 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2706 "may change the sign of the result",
2707 type, expr_type);
2710 /* Warn for integer types converted to real types if and only if
2711 all the range of values of the integer type cannot be
2712 represented by the real type. */
2713 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2714 && TREE_CODE (type) == REAL_TYPE)
2716 tree type_low_bound, type_high_bound;
2717 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2719 /* Don't warn about char y = 0xff; float x = (int) y; */
2720 expr = get_unwidened (expr, 0);
2721 expr_type = TREE_TYPE (expr);
2723 type_low_bound = TYPE_MIN_VALUE (expr_type);
2724 type_high_bound = TYPE_MAX_VALUE (expr_type);
2725 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2726 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2728 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2729 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2730 give_warning = UNSAFE_OTHER;
2733 /* Warn for real types converted to smaller real types. */
2734 else if (TREE_CODE (expr_type) == REAL_TYPE
2735 && TREE_CODE (type) == REAL_TYPE
2736 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2737 give_warning = UNSAFE_REAL;
2740 return give_warning;
2743 /* Warns if the conversion of EXPR to TYPE may alter a value.
2744 This is a helper function for warnings_for_convert_and_check. */
2746 static void
2747 conversion_warning (location_t loc, tree type, tree expr)
2749 tree expr_type = TREE_TYPE (expr);
2750 enum conversion_safety conversion_kind;
2752 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2753 return;
2755 /* This may happen, because for LHS op= RHS we preevaluate
2756 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2757 means we could no longer see the code of the EXPR. */
2758 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2759 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2760 if (TREE_CODE (expr) == SAVE_EXPR)
2761 expr = TREE_OPERAND (expr, 0);
2763 switch (TREE_CODE (expr))
2765 case EQ_EXPR:
2766 case NE_EXPR:
2767 case LE_EXPR:
2768 case GE_EXPR:
2769 case LT_EXPR:
2770 case GT_EXPR:
2771 case TRUTH_ANDIF_EXPR:
2772 case TRUTH_ORIF_EXPR:
2773 case TRUTH_AND_EXPR:
2774 case TRUTH_OR_EXPR:
2775 case TRUTH_XOR_EXPR:
2776 case TRUTH_NOT_EXPR:
2777 /* Conversion from boolean to a signed:1 bit-field (which only
2778 can hold the values 0 and -1) doesn't lose information - but
2779 it does change the value. */
2780 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2781 warning_at (loc, OPT_Wconversion,
2782 "conversion to %qT from boolean expression", type);
2783 return;
2785 case REAL_CST:
2786 case INTEGER_CST:
2787 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2788 if (conversion_kind == UNSAFE_REAL)
2789 warning_at (loc, OPT_Wfloat_conversion,
2790 "conversion to %qT alters %qT constant value",
2791 type, expr_type);
2792 else if (conversion_kind)
2793 warning_at (loc, OPT_Wconversion,
2794 "conversion to %qT alters %qT constant value",
2795 type, expr_type);
2796 return;
2798 case COND_EXPR:
2800 /* In case of COND_EXPR, we do not care about the type of
2801 COND_EXPR, only about the conversion of each operand. */
2802 tree op1 = TREE_OPERAND (expr, 1);
2803 tree op2 = TREE_OPERAND (expr, 2);
2805 conversion_warning (loc, type, op1);
2806 conversion_warning (loc, type, op2);
2807 return;
2810 default: /* 'expr' is not a constant. */
2811 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2812 if (conversion_kind == UNSAFE_REAL)
2813 warning_at (loc, OPT_Wfloat_conversion,
2814 "conversion to %qT from %qT may alter its value",
2815 type, expr_type);
2816 else if (conversion_kind)
2817 warning_at (loc, OPT_Wconversion,
2818 "conversion to %qT from %qT may alter its value",
2819 type, expr_type);
2823 /* Produce warnings after a conversion. RESULT is the result of
2824 converting EXPR to TYPE. This is a helper function for
2825 convert_and_check and cp_convert_and_check. */
2827 void
2828 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2829 tree result)
2831 loc = expansion_point_location_if_in_system_header (loc);
2833 if (TREE_CODE (expr) == INTEGER_CST
2834 && (TREE_CODE (type) == INTEGER_TYPE
2835 || TREE_CODE (type) == ENUMERAL_TYPE)
2836 && !int_fits_type_p (expr, type))
2838 /* Do not diagnose overflow in a constant expression merely
2839 because a conversion overflowed. */
2840 if (TREE_OVERFLOW (result))
2841 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2843 if (TYPE_UNSIGNED (type))
2845 /* This detects cases like converting -129 or 256 to
2846 unsigned char. */
2847 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2848 warning_at (loc, OPT_Woverflow,
2849 "large integer implicitly truncated to unsigned type");
2850 else
2851 conversion_warning (loc, type, expr);
2853 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2854 warning_at (loc, OPT_Woverflow,
2855 "overflow in implicit constant conversion");
2856 /* No warning for converting 0x80000000 to int. */
2857 else if (pedantic
2858 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2859 || TYPE_PRECISION (TREE_TYPE (expr))
2860 != TYPE_PRECISION (type)))
2861 warning_at (loc, OPT_Woverflow,
2862 "overflow in implicit constant conversion");
2864 else
2865 conversion_warning (loc, type, expr);
2867 else if ((TREE_CODE (result) == INTEGER_CST
2868 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2869 warning_at (loc, OPT_Woverflow,
2870 "overflow in implicit constant conversion");
2871 else
2872 conversion_warning (loc, type, expr);
2876 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2877 Invoke this function on every expression that is converted implicitly,
2878 i.e. because of language rules and not because of an explicit cast. */
2880 tree
2881 convert_and_check (location_t loc, tree type, tree expr)
2883 tree result;
2884 tree expr_for_warning;
2886 /* Convert from a value with possible excess precision rather than
2887 via the semantic type, but do not warn about values not fitting
2888 exactly in the semantic type. */
2889 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2891 tree orig_type = TREE_TYPE (expr);
2892 expr = TREE_OPERAND (expr, 0);
2893 expr_for_warning = convert (orig_type, expr);
2894 if (orig_type == type)
2895 return expr_for_warning;
2897 else
2898 expr_for_warning = expr;
2900 if (TREE_TYPE (expr) == type)
2901 return expr;
2903 result = convert (type, expr);
2905 if (c_inhibit_evaluation_warnings == 0
2906 && !TREE_OVERFLOW_P (expr)
2907 && result != error_mark_node)
2908 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2910 return result;
2913 /* A node in a list that describes references to variables (EXPR), which are
2914 either read accesses if WRITER is zero, or write accesses, in which case
2915 WRITER is the parent of EXPR. */
2916 struct tlist
2918 struct tlist *next;
2919 tree expr, writer;
2922 /* Used to implement a cache the results of a call to verify_tree. We only
2923 use this for SAVE_EXPRs. */
2924 struct tlist_cache
2926 struct tlist_cache *next;
2927 struct tlist *cache_before_sp;
2928 struct tlist *cache_after_sp;
2929 tree expr;
2932 /* Obstack to use when allocating tlist structures, and corresponding
2933 firstobj. */
2934 static struct obstack tlist_obstack;
2935 static char *tlist_firstobj = 0;
2937 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2938 warnings. */
2939 static struct tlist *warned_ids;
2940 /* SAVE_EXPRs need special treatment. We process them only once and then
2941 cache the results. */
2942 static struct tlist_cache *save_expr_cache;
2944 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2945 static void merge_tlist (struct tlist **, struct tlist *, int);
2946 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2947 static int warning_candidate_p (tree);
2948 static bool candidate_equal_p (const_tree, const_tree);
2949 static void warn_for_collisions (struct tlist *);
2950 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2951 static struct tlist *new_tlist (struct tlist *, tree, tree);
2953 /* Create a new struct tlist and fill in its fields. */
2954 static struct tlist *
2955 new_tlist (struct tlist *next, tree t, tree writer)
2957 struct tlist *l;
2958 l = XOBNEW (&tlist_obstack, struct tlist);
2959 l->next = next;
2960 l->expr = t;
2961 l->writer = writer;
2962 return l;
2965 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2966 is nonnull, we ignore any node we find which has a writer equal to it. */
2968 static void
2969 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2971 while (add)
2973 struct tlist *next = add->next;
2974 if (!copy)
2975 add->next = *to;
2976 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2977 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2978 add = next;
2982 /* Merge the nodes of ADD into TO. This merging process is done so that for
2983 each variable that already exists in TO, no new node is added; however if
2984 there is a write access recorded in ADD, and an occurrence on TO is only
2985 a read access, then the occurrence in TO will be modified to record the
2986 write. */
2988 static void
2989 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2991 struct tlist **end = to;
2993 while (*end)
2994 end = &(*end)->next;
2996 while (add)
2998 int found = 0;
2999 struct tlist *tmp2;
3000 struct tlist *next = add->next;
3002 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3003 if (candidate_equal_p (tmp2->expr, add->expr))
3005 found = 1;
3006 if (!tmp2->writer)
3007 tmp2->writer = add->writer;
3009 if (!found)
3011 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3012 end = &(*end)->next;
3013 *end = 0;
3015 add = next;
3019 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3020 references in list LIST conflict with it, excluding reads if ONLY writers
3021 is nonzero. */
3023 static void
3024 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3025 int only_writes)
3027 struct tlist *tmp;
3029 /* Avoid duplicate warnings. */
3030 for (tmp = warned_ids; tmp; tmp = tmp->next)
3031 if (candidate_equal_p (tmp->expr, written))
3032 return;
3034 while (list)
3036 if (candidate_equal_p (list->expr, written)
3037 && !candidate_equal_p (list->writer, writer)
3038 && (!only_writes || list->writer))
3040 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3041 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3042 OPT_Wsequence_point, "operation on %qE may be undefined",
3043 list->expr);
3045 list = list->next;
3049 /* Given a list LIST of references to variables, find whether any of these
3050 can cause conflicts due to missing sequence points. */
3052 static void
3053 warn_for_collisions (struct tlist *list)
3055 struct tlist *tmp;
3057 for (tmp = list; tmp; tmp = tmp->next)
3059 if (tmp->writer)
3060 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3064 /* Return nonzero if X is a tree that can be verified by the sequence point
3065 warnings. */
3066 static int
3067 warning_candidate_p (tree x)
3069 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3070 return 0;
3072 if (TREE_CODE (x) == BLOCK)
3073 return 0;
3075 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3076 (lvalue_p) crash on TRY/CATCH. */
3077 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3078 return 0;
3080 if (!lvalue_p (x))
3081 return 0;
3083 /* No point to track non-const calls, they will never satisfy
3084 operand_equal_p. */
3085 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3086 return 0;
3088 if (TREE_CODE (x) == STRING_CST)
3089 return 0;
3091 return 1;
3094 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3095 static bool
3096 candidate_equal_p (const_tree x, const_tree y)
3098 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3101 /* Walk the tree X, and record accesses to variables. If X is written by the
3102 parent tree, WRITER is the parent.
3103 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3104 expression or its only operand forces a sequence point, then everything up
3105 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3106 in PNO_SP.
3107 Once we return, we will have emitted warnings if any subexpression before
3108 such a sequence point could be undefined. On a higher level, however, the
3109 sequence point may not be relevant, and we'll merge the two lists.
3111 Example: (b++, a) + b;
3112 The call that processes the COMPOUND_EXPR will store the increment of B
3113 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3114 processes the PLUS_EXPR will need to merge the two lists so that
3115 eventually, all accesses end up on the same list (and we'll warn about the
3116 unordered subexpressions b++ and b.
3118 A note on merging. If we modify the former example so that our expression
3119 becomes
3120 (b++, b) + a
3121 care must be taken not simply to add all three expressions into the final
3122 PNO_SP list. The function merge_tlist takes care of that by merging the
3123 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3124 way, so that no more than one access to B is recorded. */
3126 static void
3127 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3128 tree writer)
3130 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3131 enum tree_code code;
3132 enum tree_code_class cl;
3134 /* X may be NULL if it is the operand of an empty statement expression
3135 ({ }). */
3136 if (x == NULL)
3137 return;
3139 restart:
3140 code = TREE_CODE (x);
3141 cl = TREE_CODE_CLASS (code);
3143 if (warning_candidate_p (x))
3144 *pno_sp = new_tlist (*pno_sp, x, writer);
3146 switch (code)
3148 case CONSTRUCTOR:
3149 case SIZEOF_EXPR:
3150 return;
3152 case COMPOUND_EXPR:
3153 case TRUTH_ANDIF_EXPR:
3154 case TRUTH_ORIF_EXPR:
3155 tmp_before = tmp_nosp = tmp_list3 = 0;
3156 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3157 warn_for_collisions (tmp_nosp);
3158 merge_tlist (pbefore_sp, tmp_before, 0);
3159 merge_tlist (pbefore_sp, tmp_nosp, 0);
3160 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3161 merge_tlist (pbefore_sp, tmp_list3, 0);
3162 return;
3164 case COND_EXPR:
3165 tmp_before = tmp_list2 = 0;
3166 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3167 warn_for_collisions (tmp_list2);
3168 merge_tlist (pbefore_sp, tmp_before, 0);
3169 merge_tlist (pbefore_sp, tmp_list2, 0);
3171 tmp_list3 = tmp_nosp = 0;
3172 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3173 warn_for_collisions (tmp_nosp);
3174 merge_tlist (pbefore_sp, tmp_list3, 0);
3176 tmp_list3 = tmp_list2 = 0;
3177 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3178 warn_for_collisions (tmp_list2);
3179 merge_tlist (pbefore_sp, tmp_list3, 0);
3180 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3181 two first, to avoid warning for (a ? b++ : b++). */
3182 merge_tlist (&tmp_nosp, tmp_list2, 0);
3183 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3184 return;
3186 case PREDECREMENT_EXPR:
3187 case PREINCREMENT_EXPR:
3188 case POSTDECREMENT_EXPR:
3189 case POSTINCREMENT_EXPR:
3190 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3191 return;
3193 case MODIFY_EXPR:
3194 tmp_before = tmp_nosp = tmp_list3 = 0;
3195 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3196 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3197 /* Expressions inside the LHS are not ordered wrt. the sequence points
3198 in the RHS. Example:
3199 *a = (a++, 2)
3200 Despite the fact that the modification of "a" is in the before_sp
3201 list (tmp_before), it conflicts with the use of "a" in the LHS.
3202 We can handle this by adding the contents of tmp_list3
3203 to those of tmp_before, and redoing the collision warnings for that
3204 list. */
3205 add_tlist (&tmp_before, tmp_list3, x, 1);
3206 warn_for_collisions (tmp_before);
3207 /* Exclude the LHS itself here; we first have to merge it into the
3208 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3209 didn't exclude the LHS, we'd get it twice, once as a read and once
3210 as a write. */
3211 add_tlist (pno_sp, tmp_list3, x, 0);
3212 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3214 merge_tlist (pbefore_sp, tmp_before, 0);
3215 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3216 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3217 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3218 return;
3220 case CALL_EXPR:
3221 /* We need to warn about conflicts among arguments and conflicts between
3222 args and the function address. Side effects of the function address,
3223 however, are not ordered by the sequence point of the call. */
3225 call_expr_arg_iterator iter;
3226 tree arg;
3227 tmp_before = tmp_nosp = 0;
3228 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3229 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3231 tmp_list2 = tmp_list3 = 0;
3232 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3233 merge_tlist (&tmp_list3, tmp_list2, 0);
3234 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3236 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3237 warn_for_collisions (tmp_before);
3238 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3239 return;
3242 case TREE_LIST:
3243 /* Scan all the list, e.g. indices of multi dimensional array. */
3244 while (x)
3246 tmp_before = tmp_nosp = 0;
3247 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3248 merge_tlist (&tmp_nosp, tmp_before, 0);
3249 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3250 x = TREE_CHAIN (x);
3252 return;
3254 case SAVE_EXPR:
3256 struct tlist_cache *t;
3257 for (t = save_expr_cache; t; t = t->next)
3258 if (candidate_equal_p (t->expr, x))
3259 break;
3261 if (!t)
3263 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3264 t->next = save_expr_cache;
3265 t->expr = x;
3266 save_expr_cache = t;
3268 tmp_before = tmp_nosp = 0;
3269 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3270 warn_for_collisions (tmp_nosp);
3272 tmp_list3 = 0;
3273 merge_tlist (&tmp_list3, tmp_nosp, 0);
3274 t->cache_before_sp = tmp_before;
3275 t->cache_after_sp = tmp_list3;
3277 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3278 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3279 return;
3282 case ADDR_EXPR:
3283 x = TREE_OPERAND (x, 0);
3284 if (DECL_P (x))
3285 return;
3286 writer = 0;
3287 goto restart;
3289 default:
3290 /* For other expressions, simply recurse on their operands.
3291 Manual tail recursion for unary expressions.
3292 Other non-expressions need not be processed. */
3293 if (cl == tcc_unary)
3295 x = TREE_OPERAND (x, 0);
3296 writer = 0;
3297 goto restart;
3299 else if (IS_EXPR_CODE_CLASS (cl))
3301 int lp;
3302 int max = TREE_OPERAND_LENGTH (x);
3303 for (lp = 0; lp < max; lp++)
3305 tmp_before = tmp_nosp = 0;
3306 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3307 merge_tlist (&tmp_nosp, tmp_before, 0);
3308 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3311 return;
3315 /* Try to warn for undefined behavior in EXPR due to missing sequence
3316 points. */
3318 DEBUG_FUNCTION void
3319 verify_sequence_points (tree expr)
3321 struct tlist *before_sp = 0, *after_sp = 0;
3323 warned_ids = 0;
3324 save_expr_cache = 0;
3325 if (tlist_firstobj == 0)
3327 gcc_obstack_init (&tlist_obstack);
3328 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3331 verify_tree (expr, &before_sp, &after_sp, 0);
3332 warn_for_collisions (after_sp);
3333 obstack_free (&tlist_obstack, tlist_firstobj);
3336 /* Validate the expression after `case' and apply default promotions. */
3338 static tree
3339 check_case_value (tree value)
3341 if (value == NULL_TREE)
3342 return value;
3344 if (TREE_CODE (value) == INTEGER_CST)
3345 /* Promote char or short to int. */
3346 value = perform_integral_promotions (value);
3347 else if (value != error_mark_node)
3349 error ("case label does not reduce to an integer constant");
3350 value = error_mark_node;
3353 constant_expression_warning (value);
3355 return value;
3358 /* See if the case values LOW and HIGH are in the range of the original
3359 type (i.e. before the default conversion to int) of the switch testing
3360 expression.
3361 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3362 the type before promoting it. CASE_LOW_P is a pointer to the lower
3363 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3364 if the case is not a case range.
3365 The caller has to make sure that we are not called with NULL for
3366 CASE_LOW_P (i.e. the default case).
3367 Returns true if the case label is in range of ORIG_TYPE (saturated or
3368 untouched) or false if the label is out of range. */
3370 static bool
3371 check_case_bounds (tree type, tree orig_type,
3372 tree *case_low_p, tree *case_high_p)
3374 tree min_value, max_value;
3375 tree case_low = *case_low_p;
3376 tree case_high = case_high_p ? *case_high_p : case_low;
3378 /* If there was a problem with the original type, do nothing. */
3379 if (orig_type == error_mark_node)
3380 return true;
3382 min_value = TYPE_MIN_VALUE (orig_type);
3383 max_value = TYPE_MAX_VALUE (orig_type);
3385 /* Case label is less than minimum for type. */
3386 if (tree_int_cst_compare (case_low, min_value) < 0
3387 && tree_int_cst_compare (case_high, min_value) < 0)
3389 warning (0, "case label value is less than minimum value for type");
3390 return false;
3393 /* Case value is greater than maximum for type. */
3394 if (tree_int_cst_compare (case_low, max_value) > 0
3395 && tree_int_cst_compare (case_high, max_value) > 0)
3397 warning (0, "case label value exceeds maximum value for type");
3398 return false;
3401 /* Saturate lower case label value to minimum. */
3402 if (tree_int_cst_compare (case_high, min_value) >= 0
3403 && tree_int_cst_compare (case_low, min_value) < 0)
3405 warning (0, "lower value in case label range"
3406 " less than minimum value for type");
3407 case_low = min_value;
3410 /* Saturate upper case label value to maximum. */
3411 if (tree_int_cst_compare (case_low, max_value) <= 0
3412 && tree_int_cst_compare (case_high, max_value) > 0)
3414 warning (0, "upper value in case label range"
3415 " exceeds maximum value for type");
3416 case_high = max_value;
3419 if (*case_low_p != case_low)
3420 *case_low_p = convert (type, case_low);
3421 if (case_high_p && *case_high_p != case_high)
3422 *case_high_p = convert (type, case_high);
3424 return true;
3427 /* Return an integer type with BITS bits of precision,
3428 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3430 tree
3431 c_common_type_for_size (unsigned int bits, int unsignedp)
3433 if (bits == TYPE_PRECISION (integer_type_node))
3434 return unsignedp ? unsigned_type_node : integer_type_node;
3436 if (bits == TYPE_PRECISION (signed_char_type_node))
3437 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3439 if (bits == TYPE_PRECISION (short_integer_type_node))
3440 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3442 if (bits == TYPE_PRECISION (long_integer_type_node))
3443 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3445 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3446 return (unsignedp ? long_long_unsigned_type_node
3447 : long_long_integer_type_node);
3449 if (int128_integer_type_node
3450 && bits == TYPE_PRECISION (int128_integer_type_node))
3451 return (unsignedp ? int128_unsigned_type_node
3452 : int128_integer_type_node);
3454 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3455 return (unsignedp ? widest_unsigned_literal_type_node
3456 : widest_integer_literal_type_node);
3458 if (bits <= TYPE_PRECISION (intQI_type_node))
3459 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3461 if (bits <= TYPE_PRECISION (intHI_type_node))
3462 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3464 if (bits <= TYPE_PRECISION (intSI_type_node))
3465 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3467 if (bits <= TYPE_PRECISION (intDI_type_node))
3468 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3470 return 0;
3473 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3474 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3475 and saturating if SATP is nonzero, otherwise not saturating. */
3477 tree
3478 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3479 int unsignedp, int satp)
3481 enum machine_mode mode;
3482 if (ibit == 0)
3483 mode = unsignedp ? UQQmode : QQmode;
3484 else
3485 mode = unsignedp ? UHAmode : HAmode;
3487 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3488 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3489 break;
3491 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3493 sorry ("GCC cannot support operators with integer types and "
3494 "fixed-point types that have too many integral and "
3495 "fractional bits together");
3496 return 0;
3499 return c_common_type_for_mode (mode, satp);
3502 /* Used for communication between c_common_type_for_mode and
3503 c_register_builtin_type. */
3504 static GTY(()) tree registered_builtin_types;
3506 /* Return a data type that has machine mode MODE.
3507 If the mode is an integer,
3508 then UNSIGNEDP selects between signed and unsigned types.
3509 If the mode is a fixed-point mode,
3510 then UNSIGNEDP selects between saturating and nonsaturating types. */
3512 tree
3513 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3515 tree t;
3517 if (mode == TYPE_MODE (integer_type_node))
3518 return unsignedp ? unsigned_type_node : integer_type_node;
3520 if (mode == TYPE_MODE (signed_char_type_node))
3521 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3523 if (mode == TYPE_MODE (short_integer_type_node))
3524 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3526 if (mode == TYPE_MODE (long_integer_type_node))
3527 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3529 if (mode == TYPE_MODE (long_long_integer_type_node))
3530 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3532 if (int128_integer_type_node
3533 && mode == TYPE_MODE (int128_integer_type_node))
3534 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3536 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3537 return unsignedp ? widest_unsigned_literal_type_node
3538 : widest_integer_literal_type_node;
3540 if (mode == QImode)
3541 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3543 if (mode == HImode)
3544 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3546 if (mode == SImode)
3547 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3549 if (mode == DImode)
3550 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3552 #if HOST_BITS_PER_WIDE_INT >= 64
3553 if (mode == TYPE_MODE (intTI_type_node))
3554 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3555 #endif
3557 if (mode == TYPE_MODE (float_type_node))
3558 return float_type_node;
3560 if (mode == TYPE_MODE (double_type_node))
3561 return double_type_node;
3563 if (mode == TYPE_MODE (long_double_type_node))
3564 return long_double_type_node;
3566 if (mode == TYPE_MODE (void_type_node))
3567 return void_type_node;
3569 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3570 return (unsignedp
3571 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3572 : make_signed_type (GET_MODE_PRECISION (mode)));
3574 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3575 return (unsignedp
3576 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3577 : make_signed_type (GET_MODE_PRECISION (mode)));
3579 if (COMPLEX_MODE_P (mode))
3581 enum machine_mode inner_mode;
3582 tree inner_type;
3584 if (mode == TYPE_MODE (complex_float_type_node))
3585 return complex_float_type_node;
3586 if (mode == TYPE_MODE (complex_double_type_node))
3587 return complex_double_type_node;
3588 if (mode == TYPE_MODE (complex_long_double_type_node))
3589 return complex_long_double_type_node;
3591 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3592 return complex_integer_type_node;
3594 inner_mode = GET_MODE_INNER (mode);
3595 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3596 if (inner_type != NULL_TREE)
3597 return build_complex_type (inner_type);
3599 else if (VECTOR_MODE_P (mode))
3601 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3602 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3603 if (inner_type != NULL_TREE)
3604 return build_vector_type_for_mode (inner_type, mode);
3607 if (mode == TYPE_MODE (dfloat32_type_node))
3608 return dfloat32_type_node;
3609 if (mode == TYPE_MODE (dfloat64_type_node))
3610 return dfloat64_type_node;
3611 if (mode == TYPE_MODE (dfloat128_type_node))
3612 return dfloat128_type_node;
3614 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3616 if (mode == TYPE_MODE (short_fract_type_node))
3617 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3618 if (mode == TYPE_MODE (fract_type_node))
3619 return unsignedp ? sat_fract_type_node : fract_type_node;
3620 if (mode == TYPE_MODE (long_fract_type_node))
3621 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3622 if (mode == TYPE_MODE (long_long_fract_type_node))
3623 return unsignedp ? sat_long_long_fract_type_node
3624 : long_long_fract_type_node;
3626 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3627 return unsignedp ? sat_unsigned_short_fract_type_node
3628 : unsigned_short_fract_type_node;
3629 if (mode == TYPE_MODE (unsigned_fract_type_node))
3630 return unsignedp ? sat_unsigned_fract_type_node
3631 : unsigned_fract_type_node;
3632 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3633 return unsignedp ? sat_unsigned_long_fract_type_node
3634 : unsigned_long_fract_type_node;
3635 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3636 return unsignedp ? sat_unsigned_long_long_fract_type_node
3637 : unsigned_long_long_fract_type_node;
3639 if (mode == TYPE_MODE (short_accum_type_node))
3640 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3641 if (mode == TYPE_MODE (accum_type_node))
3642 return unsignedp ? sat_accum_type_node : accum_type_node;
3643 if (mode == TYPE_MODE (long_accum_type_node))
3644 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3645 if (mode == TYPE_MODE (long_long_accum_type_node))
3646 return unsignedp ? sat_long_long_accum_type_node
3647 : long_long_accum_type_node;
3649 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3650 return unsignedp ? sat_unsigned_short_accum_type_node
3651 : unsigned_short_accum_type_node;
3652 if (mode == TYPE_MODE (unsigned_accum_type_node))
3653 return unsignedp ? sat_unsigned_accum_type_node
3654 : unsigned_accum_type_node;
3655 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3656 return unsignedp ? sat_unsigned_long_accum_type_node
3657 : unsigned_long_accum_type_node;
3658 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3659 return unsignedp ? sat_unsigned_long_long_accum_type_node
3660 : unsigned_long_long_accum_type_node;
3662 if (mode == QQmode)
3663 return unsignedp ? sat_qq_type_node : qq_type_node;
3664 if (mode == HQmode)
3665 return unsignedp ? sat_hq_type_node : hq_type_node;
3666 if (mode == SQmode)
3667 return unsignedp ? sat_sq_type_node : sq_type_node;
3668 if (mode == DQmode)
3669 return unsignedp ? sat_dq_type_node : dq_type_node;
3670 if (mode == TQmode)
3671 return unsignedp ? sat_tq_type_node : tq_type_node;
3673 if (mode == UQQmode)
3674 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3675 if (mode == UHQmode)
3676 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3677 if (mode == USQmode)
3678 return unsignedp ? sat_usq_type_node : usq_type_node;
3679 if (mode == UDQmode)
3680 return unsignedp ? sat_udq_type_node : udq_type_node;
3681 if (mode == UTQmode)
3682 return unsignedp ? sat_utq_type_node : utq_type_node;
3684 if (mode == HAmode)
3685 return unsignedp ? sat_ha_type_node : ha_type_node;
3686 if (mode == SAmode)
3687 return unsignedp ? sat_sa_type_node : sa_type_node;
3688 if (mode == DAmode)
3689 return unsignedp ? sat_da_type_node : da_type_node;
3690 if (mode == TAmode)
3691 return unsignedp ? sat_ta_type_node : ta_type_node;
3693 if (mode == UHAmode)
3694 return unsignedp ? sat_uha_type_node : uha_type_node;
3695 if (mode == USAmode)
3696 return unsignedp ? sat_usa_type_node : usa_type_node;
3697 if (mode == UDAmode)
3698 return unsignedp ? sat_uda_type_node : uda_type_node;
3699 if (mode == UTAmode)
3700 return unsignedp ? sat_uta_type_node : uta_type_node;
3703 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3704 if (TYPE_MODE (TREE_VALUE (t)) == mode
3705 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3706 return TREE_VALUE (t);
3708 return 0;
3711 tree
3712 c_common_unsigned_type (tree type)
3714 return c_common_signed_or_unsigned_type (1, type);
3717 /* Return a signed type the same as TYPE in other respects. */
3719 tree
3720 c_common_signed_type (tree type)
3722 return c_common_signed_or_unsigned_type (0, type);
3725 /* Return a type the same as TYPE except unsigned or
3726 signed according to UNSIGNEDP. */
3728 tree
3729 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3731 tree type1;
3733 /* This block of code emulates the behavior of the old
3734 c_common_unsigned_type. In particular, it returns
3735 long_unsigned_type_node if passed a long, even when a int would
3736 have the same size. This is necessary for warnings to work
3737 correctly in archs where sizeof(int) == sizeof(long) */
3739 type1 = TYPE_MAIN_VARIANT (type);
3740 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3741 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3742 if (type1 == integer_type_node || type1 == unsigned_type_node)
3743 return unsignedp ? unsigned_type_node : integer_type_node;
3744 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3745 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3746 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3747 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3748 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3749 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3750 if (int128_integer_type_node
3751 && (type1 == int128_integer_type_node
3752 || type1 == int128_unsigned_type_node))
3753 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3754 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3755 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3756 #if HOST_BITS_PER_WIDE_INT >= 64
3757 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3758 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3759 #endif
3760 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3761 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3762 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3763 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3764 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3765 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3766 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3767 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3769 #define C_COMMON_FIXED_TYPES(NAME) \
3770 if (type1 == short_ ## NAME ## _type_node \
3771 || type1 == unsigned_short_ ## NAME ## _type_node) \
3772 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3773 : short_ ## NAME ## _type_node; \
3774 if (type1 == NAME ## _type_node \
3775 || type1 == unsigned_ ## NAME ## _type_node) \
3776 return unsignedp ? unsigned_ ## NAME ## _type_node \
3777 : NAME ## _type_node; \
3778 if (type1 == long_ ## NAME ## _type_node \
3779 || type1 == unsigned_long_ ## NAME ## _type_node) \
3780 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3781 : long_ ## NAME ## _type_node; \
3782 if (type1 == long_long_ ## NAME ## _type_node \
3783 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3784 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3785 : long_long_ ## NAME ## _type_node;
3787 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3788 if (type1 == NAME ## _type_node \
3789 || type1 == u ## NAME ## _type_node) \
3790 return unsignedp ? u ## NAME ## _type_node \
3791 : NAME ## _type_node;
3793 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3794 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3795 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3796 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3797 : sat_ ## short_ ## NAME ## _type_node; \
3798 if (type1 == sat_ ## NAME ## _type_node \
3799 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3800 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3801 : sat_ ## NAME ## _type_node; \
3802 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3803 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3804 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3805 : sat_ ## long_ ## NAME ## _type_node; \
3806 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3807 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3808 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3809 : sat_ ## long_long_ ## NAME ## _type_node;
3811 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3812 if (type1 == sat_ ## NAME ## _type_node \
3813 || type1 == sat_ ## u ## NAME ## _type_node) \
3814 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3815 : sat_ ## NAME ## _type_node;
3817 C_COMMON_FIXED_TYPES (fract);
3818 C_COMMON_FIXED_TYPES_SAT (fract);
3819 C_COMMON_FIXED_TYPES (accum);
3820 C_COMMON_FIXED_TYPES_SAT (accum);
3822 C_COMMON_FIXED_MODE_TYPES (qq);
3823 C_COMMON_FIXED_MODE_TYPES (hq);
3824 C_COMMON_FIXED_MODE_TYPES (sq);
3825 C_COMMON_FIXED_MODE_TYPES (dq);
3826 C_COMMON_FIXED_MODE_TYPES (tq);
3827 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3828 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3829 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3830 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3831 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3832 C_COMMON_FIXED_MODE_TYPES (ha);
3833 C_COMMON_FIXED_MODE_TYPES (sa);
3834 C_COMMON_FIXED_MODE_TYPES (da);
3835 C_COMMON_FIXED_MODE_TYPES (ta);
3836 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3837 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3838 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3839 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3841 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3842 the precision; they have precision set to match their range, but
3843 may use a wider mode to match an ABI. If we change modes, we may
3844 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3845 the precision as well, so as to yield correct results for
3846 bit-field types. C++ does not have these separate bit-field
3847 types, and producing a signed or unsigned variant of an
3848 ENUMERAL_TYPE may cause other problems as well. */
3850 if (!INTEGRAL_TYPE_P (type)
3851 || TYPE_UNSIGNED (type) == unsignedp)
3852 return type;
3854 #define TYPE_OK(node) \
3855 (TYPE_MODE (type) == TYPE_MODE (node) \
3856 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3857 if (TYPE_OK (signed_char_type_node))
3858 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3859 if (TYPE_OK (integer_type_node))
3860 return unsignedp ? unsigned_type_node : integer_type_node;
3861 if (TYPE_OK (short_integer_type_node))
3862 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3863 if (TYPE_OK (long_integer_type_node))
3864 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3865 if (TYPE_OK (long_long_integer_type_node))
3866 return (unsignedp ? long_long_unsigned_type_node
3867 : long_long_integer_type_node);
3868 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3869 return (unsignedp ? int128_unsigned_type_node
3870 : int128_integer_type_node);
3871 if (TYPE_OK (widest_integer_literal_type_node))
3872 return (unsignedp ? widest_unsigned_literal_type_node
3873 : widest_integer_literal_type_node);
3875 #if HOST_BITS_PER_WIDE_INT >= 64
3876 if (TYPE_OK (intTI_type_node))
3877 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3878 #endif
3879 if (TYPE_OK (intDI_type_node))
3880 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3881 if (TYPE_OK (intSI_type_node))
3882 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3883 if (TYPE_OK (intHI_type_node))
3884 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3885 if (TYPE_OK (intQI_type_node))
3886 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3887 #undef TYPE_OK
3889 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3892 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3894 tree
3895 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3897 /* Extended integer types of the same width as a standard type have
3898 lesser rank, so those of the same width as int promote to int or
3899 unsigned int and are valid for printf formats expecting int or
3900 unsigned int. To avoid such special cases, avoid creating
3901 extended integer types for bit-fields if a standard integer type
3902 is available. */
3903 if (width == TYPE_PRECISION (integer_type_node))
3904 return unsignedp ? unsigned_type_node : integer_type_node;
3905 if (width == TYPE_PRECISION (signed_char_type_node))
3906 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3907 if (width == TYPE_PRECISION (short_integer_type_node))
3908 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3909 if (width == TYPE_PRECISION (long_integer_type_node))
3910 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3911 if (width == TYPE_PRECISION (long_long_integer_type_node))
3912 return (unsignedp ? long_long_unsigned_type_node
3913 : long_long_integer_type_node);
3914 if (int128_integer_type_node
3915 && width == TYPE_PRECISION (int128_integer_type_node))
3916 return (unsignedp ? int128_unsigned_type_node
3917 : int128_integer_type_node);
3918 return build_nonstandard_integer_type (width, unsignedp);
3921 /* The C version of the register_builtin_type langhook. */
3923 void
3924 c_register_builtin_type (tree type, const char* name)
3926 tree decl;
3928 decl = build_decl (UNKNOWN_LOCATION,
3929 TYPE_DECL, get_identifier (name), type);
3930 DECL_ARTIFICIAL (decl) = 1;
3931 if (!TYPE_NAME (type))
3932 TYPE_NAME (type) = decl;
3933 pushdecl (decl);
3935 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3938 /* Print an error message for invalid operands to arith operation
3939 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3940 LOCATION is the location of the message. */
3942 void
3943 binary_op_error (location_t location, enum tree_code code,
3944 tree type0, tree type1)
3946 const char *opname;
3948 switch (code)
3950 case PLUS_EXPR:
3951 opname = "+"; break;
3952 case MINUS_EXPR:
3953 opname = "-"; break;
3954 case MULT_EXPR:
3955 opname = "*"; break;
3956 case MAX_EXPR:
3957 opname = "max"; break;
3958 case MIN_EXPR:
3959 opname = "min"; break;
3960 case EQ_EXPR:
3961 opname = "=="; break;
3962 case NE_EXPR:
3963 opname = "!="; break;
3964 case LE_EXPR:
3965 opname = "<="; break;
3966 case GE_EXPR:
3967 opname = ">="; break;
3968 case LT_EXPR:
3969 opname = "<"; break;
3970 case GT_EXPR:
3971 opname = ">"; break;
3972 case LSHIFT_EXPR:
3973 opname = "<<"; break;
3974 case RSHIFT_EXPR:
3975 opname = ">>"; break;
3976 case TRUNC_MOD_EXPR:
3977 case FLOOR_MOD_EXPR:
3978 opname = "%"; break;
3979 case TRUNC_DIV_EXPR:
3980 case FLOOR_DIV_EXPR:
3981 opname = "/"; break;
3982 case BIT_AND_EXPR:
3983 opname = "&"; break;
3984 case BIT_IOR_EXPR:
3985 opname = "|"; break;
3986 case TRUTH_ANDIF_EXPR:
3987 opname = "&&"; break;
3988 case TRUTH_ORIF_EXPR:
3989 opname = "||"; break;
3990 case BIT_XOR_EXPR:
3991 opname = "^"; break;
3992 default:
3993 gcc_unreachable ();
3995 error_at (location,
3996 "invalid operands to binary %s (have %qT and %qT)", opname,
3997 type0, type1);
4000 /* Given an expression as a tree, return its original type. Do this
4001 by stripping any conversion that preserves the sign and precision. */
4002 static tree
4003 expr_original_type (tree expr)
4005 STRIP_SIGN_NOPS (expr);
4006 return TREE_TYPE (expr);
4009 /* Subroutine of build_binary_op, used for comparison operations.
4010 See if the operands have both been converted from subword integer types
4011 and, if so, perhaps change them both back to their original type.
4012 This function is also responsible for converting the two operands
4013 to the proper common type for comparison.
4015 The arguments of this function are all pointers to local variables
4016 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4017 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4019 LOC is the location of the comparison.
4021 If this function returns nonzero, it means that the comparison has
4022 a constant value. What this function returns is an expression for
4023 that value. */
4025 tree
4026 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4027 tree *restype_ptr, enum tree_code *rescode_ptr)
4029 tree type;
4030 tree op0 = *op0_ptr;
4031 tree op1 = *op1_ptr;
4032 int unsignedp0, unsignedp1;
4033 int real1, real2;
4034 tree primop0, primop1;
4035 enum tree_code code = *rescode_ptr;
4037 /* Throw away any conversions to wider types
4038 already present in the operands. */
4040 primop0 = c_common_get_narrower (op0, &unsignedp0);
4041 primop1 = c_common_get_narrower (op1, &unsignedp1);
4043 /* If primopN is first sign-extended from primopN's precision to opN's
4044 precision, then zero-extended from opN's precision to
4045 *restype_ptr precision, shortenings might be invalid. */
4046 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4047 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4048 && !unsignedp0
4049 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4050 primop0 = op0;
4051 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4052 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4053 && !unsignedp1
4054 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4055 primop1 = op1;
4057 /* Handle the case that OP0 does not *contain* a conversion
4058 but it *requires* conversion to FINAL_TYPE. */
4060 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4061 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4062 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4063 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4065 /* If one of the operands must be floated, we cannot optimize. */
4066 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4067 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4069 /* If first arg is constant, swap the args (changing operation
4070 so value is preserved), for canonicalization. Don't do this if
4071 the second arg is 0. */
4073 if (TREE_CONSTANT (primop0)
4074 && !integer_zerop (primop1) && !real_zerop (primop1)
4075 && !fixed_zerop (primop1))
4077 tree tem = primop0;
4078 int temi = unsignedp0;
4079 primop0 = primop1;
4080 primop1 = tem;
4081 tem = op0;
4082 op0 = op1;
4083 op1 = tem;
4084 *op0_ptr = op0;
4085 *op1_ptr = op1;
4086 unsignedp0 = unsignedp1;
4087 unsignedp1 = temi;
4088 temi = real1;
4089 real1 = real2;
4090 real2 = temi;
4092 switch (code)
4094 case LT_EXPR:
4095 code = GT_EXPR;
4096 break;
4097 case GT_EXPR:
4098 code = LT_EXPR;
4099 break;
4100 case LE_EXPR:
4101 code = GE_EXPR;
4102 break;
4103 case GE_EXPR:
4104 code = LE_EXPR;
4105 break;
4106 default:
4107 break;
4109 *rescode_ptr = code;
4112 /* If comparing an integer against a constant more bits wide,
4113 maybe we can deduce a value of 1 or 0 independent of the data.
4114 Or else truncate the constant now
4115 rather than extend the variable at run time.
4117 This is only interesting if the constant is the wider arg.
4118 Also, it is not safe if the constant is unsigned and the
4119 variable arg is signed, since in this case the variable
4120 would be sign-extended and then regarded as unsigned.
4121 Our technique fails in this case because the lowest/highest
4122 possible unsigned results don't follow naturally from the
4123 lowest/highest possible values of the variable operand.
4124 For just EQ_EXPR and NE_EXPR there is another technique that
4125 could be used: see if the constant can be faithfully represented
4126 in the other operand's type, by truncating it and reextending it
4127 and see if that preserves the constant's value. */
4129 if (!real1 && !real2
4130 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4131 && TREE_CODE (primop1) == INTEGER_CST
4132 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4134 int min_gt, max_gt, min_lt, max_lt;
4135 tree maxval, minval;
4136 /* 1 if comparison is nominally unsigned. */
4137 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4138 tree val;
4140 type = c_common_signed_or_unsigned_type (unsignedp0,
4141 TREE_TYPE (primop0));
4143 maxval = TYPE_MAX_VALUE (type);
4144 minval = TYPE_MIN_VALUE (type);
4146 if (unsignedp && !unsignedp0)
4147 *restype_ptr = c_common_signed_type (*restype_ptr);
4149 if (TREE_TYPE (primop1) != *restype_ptr)
4151 /* Convert primop1 to target type, but do not introduce
4152 additional overflow. We know primop1 is an int_cst. */
4153 primop1 = force_fit_type_double (*restype_ptr,
4154 tree_to_double_int (primop1),
4155 0, TREE_OVERFLOW (primop1));
4157 if (type != *restype_ptr)
4159 minval = convert (*restype_ptr, minval);
4160 maxval = convert (*restype_ptr, maxval);
4163 if (unsignedp && unsignedp0)
4165 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4166 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4167 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4168 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4170 else
4172 min_gt = INT_CST_LT (primop1, minval);
4173 max_gt = INT_CST_LT (primop1, maxval);
4174 min_lt = INT_CST_LT (minval, primop1);
4175 max_lt = INT_CST_LT (maxval, primop1);
4178 val = 0;
4179 /* This used to be a switch, but Genix compiler can't handle that. */
4180 if (code == NE_EXPR)
4182 if (max_lt || min_gt)
4183 val = truthvalue_true_node;
4185 else if (code == EQ_EXPR)
4187 if (max_lt || min_gt)
4188 val = truthvalue_false_node;
4190 else if (code == LT_EXPR)
4192 if (max_lt)
4193 val = truthvalue_true_node;
4194 if (!min_lt)
4195 val = truthvalue_false_node;
4197 else if (code == GT_EXPR)
4199 if (min_gt)
4200 val = truthvalue_true_node;
4201 if (!max_gt)
4202 val = truthvalue_false_node;
4204 else if (code == LE_EXPR)
4206 if (!max_gt)
4207 val = truthvalue_true_node;
4208 if (min_gt)
4209 val = truthvalue_false_node;
4211 else if (code == GE_EXPR)
4213 if (!min_lt)
4214 val = truthvalue_true_node;
4215 if (max_lt)
4216 val = truthvalue_false_node;
4219 /* If primop0 was sign-extended and unsigned comparison specd,
4220 we did a signed comparison above using the signed type bounds.
4221 But the comparison we output must be unsigned.
4223 Also, for inequalities, VAL is no good; but if the signed
4224 comparison had *any* fixed result, it follows that the
4225 unsigned comparison just tests the sign in reverse
4226 (positive values are LE, negative ones GE).
4227 So we can generate an unsigned comparison
4228 against an extreme value of the signed type. */
4230 if (unsignedp && !unsignedp0)
4232 if (val != 0)
4233 switch (code)
4235 case LT_EXPR:
4236 case GE_EXPR:
4237 primop1 = TYPE_MIN_VALUE (type);
4238 val = 0;
4239 break;
4241 case LE_EXPR:
4242 case GT_EXPR:
4243 primop1 = TYPE_MAX_VALUE (type);
4244 val = 0;
4245 break;
4247 default:
4248 break;
4250 type = c_common_unsigned_type (type);
4253 if (TREE_CODE (primop0) != INTEGER_CST)
4255 if (val == truthvalue_false_node)
4256 warning_at (loc, OPT_Wtype_limits,
4257 "comparison is always false due to limited range of data type");
4258 if (val == truthvalue_true_node)
4259 warning_at (loc, OPT_Wtype_limits,
4260 "comparison is always true due to limited range of data type");
4263 if (val != 0)
4265 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4266 if (TREE_SIDE_EFFECTS (primop0))
4267 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4268 return val;
4271 /* Value is not predetermined, but do the comparison
4272 in the type of the operand that is not constant.
4273 TYPE is already properly set. */
4276 /* If either arg is decimal float and the other is float, find the
4277 proper common type to use for comparison. */
4278 else if (real1 && real2
4279 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4280 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4281 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4283 else if (real1 && real2
4284 && (TYPE_PRECISION (TREE_TYPE (primop0))
4285 == TYPE_PRECISION (TREE_TYPE (primop1))))
4286 type = TREE_TYPE (primop0);
4288 /* If args' natural types are both narrower than nominal type
4289 and both extend in the same manner, compare them
4290 in the type of the wider arg.
4291 Otherwise must actually extend both to the nominal
4292 common type lest different ways of extending
4293 alter the result.
4294 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4296 else if (unsignedp0 == unsignedp1 && real1 == real2
4297 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4298 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4300 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4301 type = c_common_signed_or_unsigned_type (unsignedp0
4302 || TYPE_UNSIGNED (*restype_ptr),
4303 type);
4304 /* Make sure shorter operand is extended the right way
4305 to match the longer operand. */
4306 primop0
4307 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4308 TREE_TYPE (primop0)),
4309 primop0);
4310 primop1
4311 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4312 TREE_TYPE (primop1)),
4313 primop1);
4315 else
4317 /* Here we must do the comparison on the nominal type
4318 using the args exactly as we received them. */
4319 type = *restype_ptr;
4320 primop0 = op0;
4321 primop1 = op1;
4323 if (!real1 && !real2 && integer_zerop (primop1)
4324 && TYPE_UNSIGNED (*restype_ptr))
4326 tree value = 0;
4327 /* All unsigned values are >= 0, so we warn. However,
4328 if OP0 is a constant that is >= 0, the signedness of
4329 the comparison isn't an issue, so suppress the
4330 warning. */
4331 bool warn =
4332 warn_type_limits && !in_system_header_at (loc)
4333 && !(TREE_CODE (primop0) == INTEGER_CST
4334 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4335 primop0)))
4336 /* Do not warn for enumeration types. */
4337 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4339 switch (code)
4341 case GE_EXPR:
4342 if (warn)
4343 warning_at (loc, OPT_Wtype_limits,
4344 "comparison of unsigned expression >= 0 is always true");
4345 value = truthvalue_true_node;
4346 break;
4348 case LT_EXPR:
4349 if (warn)
4350 warning_at (loc, OPT_Wtype_limits,
4351 "comparison of unsigned expression < 0 is always false");
4352 value = truthvalue_false_node;
4353 break;
4355 default:
4356 break;
4359 if (value != 0)
4361 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4362 if (TREE_SIDE_EFFECTS (primop0))
4363 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4364 primop0, value);
4365 return value;
4370 *op0_ptr = convert (type, primop0);
4371 *op1_ptr = convert (type, primop1);
4373 *restype_ptr = truthvalue_type_node;
4375 return 0;
4378 /* Return a tree for the sum or difference (RESULTCODE says which)
4379 of pointer PTROP and integer INTOP. */
4381 tree
4382 pointer_int_sum (location_t loc, enum tree_code resultcode,
4383 tree ptrop, tree intop, bool complain)
4385 tree size_exp, ret;
4387 /* The result is a pointer of the same type that is being added. */
4388 tree result_type = TREE_TYPE (ptrop);
4390 /* If the pointer lives in UPC shared memory, then
4391 drop the 'shared' qualifier. */
4392 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4393 result_type = build_upc_unshared_type (result_type);
4395 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4397 if (complain && warn_pointer_arith)
4398 pedwarn (loc, OPT_Wpointer_arith,
4399 "pointer of type %<void *%> used in arithmetic");
4400 else if (!complain)
4401 return error_mark_node;
4402 size_exp = integer_one_node;
4404 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4406 if (complain && warn_pointer_arith)
4407 pedwarn (loc, OPT_Wpointer_arith,
4408 "pointer to a function used in arithmetic");
4409 else if (!complain)
4410 return error_mark_node;
4411 size_exp = integer_one_node;
4413 else
4414 size_exp = size_in_bytes (TREE_TYPE (result_type));
4416 /* We are manipulating pointer values, so we don't need to warn
4417 about relying on undefined signed overflow. We disable the
4418 warning here because we use integer types so fold won't know that
4419 they are really pointers. */
4420 fold_defer_overflow_warnings ();
4422 /* If what we are about to multiply by the size of the elements
4423 contains a constant term, apply distributive law
4424 and multiply that constant term separately.
4425 This helps produce common subexpressions. */
4426 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4427 && !TREE_CONSTANT (intop)
4428 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4429 && TREE_CONSTANT (size_exp)
4430 /* If the constant comes from pointer subtraction,
4431 skip this optimization--it would cause an error. */
4432 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4433 /* If the constant is unsigned, and smaller than the pointer size,
4434 then we must skip this optimization. This is because it could cause
4435 an overflow error if the constant is negative but INTOP is not. */
4436 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4437 || (TYPE_PRECISION (TREE_TYPE (intop))
4438 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4440 enum tree_code subcode = resultcode;
4441 tree int_type = TREE_TYPE (intop);
4442 if (TREE_CODE (intop) == MINUS_EXPR)
4443 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4444 /* Convert both subexpression types to the type of intop,
4445 because weird cases involving pointer arithmetic
4446 can result in a sum or difference with different type args. */
4447 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4448 subcode, ptrop,
4449 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4450 intop = convert (int_type, TREE_OPERAND (intop, 0));
4453 /* Convert the integer argument to a type the same size as sizetype
4454 so the multiply won't overflow spuriously. */
4455 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4456 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4457 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4458 TYPE_UNSIGNED (sizetype)), intop);
4460 /* Replace the integer argument with a suitable product by the object size.
4461 Do this multiplication as signed, then convert to the appropriate type
4462 for the pointer operation and disregard an overflow that occurred only
4463 because of the sign-extension change in the latter conversion. */
4465 tree t = build_binary_op (loc,
4466 MULT_EXPR, intop,
4467 convert (TREE_TYPE (intop), size_exp), 1);
4468 intop = convert (sizetype, t);
4469 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4470 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4471 TREE_INT_CST_HIGH (intop));
4474 /* Create the sum or difference. */
4475 if (resultcode == MINUS_EXPR)
4476 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4478 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4480 fold_undefer_and_ignore_overflow_warnings ();
4482 return ret;
4485 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4486 and if NON_CONST is known not to be permitted in an evaluated part
4487 of a constant expression. */
4489 tree
4490 c_wrap_maybe_const (tree expr, bool non_const)
4492 bool nowarning = TREE_NO_WARNING (expr);
4493 location_t loc = EXPR_LOCATION (expr);
4495 /* This should never be called for C++. */
4496 if (c_dialect_cxx ())
4497 gcc_unreachable ();
4499 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4500 STRIP_TYPE_NOPS (expr);
4501 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4502 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4503 if (nowarning)
4504 TREE_NO_WARNING (expr) = 1;
4505 protected_set_expr_location (expr, loc);
4507 return expr;
4510 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4511 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4512 around the SAVE_EXPR if needed so that c_fully_fold does not need
4513 to look inside SAVE_EXPRs. */
4515 tree
4516 c_save_expr (tree expr)
4518 bool maybe_const = true;
4519 if (c_dialect_cxx ())
4520 return save_expr (expr);
4521 expr = c_fully_fold (expr, false, &maybe_const);
4522 expr = save_expr (expr);
4523 if (!maybe_const)
4524 expr = c_wrap_maybe_const (expr, true);
4525 return expr;
4528 /* Return whether EXPR is a declaration whose address can never be
4529 NULL. */
4531 bool
4532 decl_with_nonnull_addr_p (const_tree expr)
4534 return (DECL_P (expr)
4535 && (TREE_CODE (expr) == PARM_DECL
4536 || TREE_CODE (expr) == LABEL_DECL
4537 || !DECL_WEAK (expr)));
4540 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4541 or for an `if' or `while' statement or ?..: exp. It should already
4542 have been validated to be of suitable type; otherwise, a bad
4543 diagnostic may result.
4545 The EXPR is located at LOCATION.
4547 This preparation consists of taking the ordinary
4548 representation of an expression expr and producing a valid tree
4549 boolean expression describing whether expr is nonzero. We could
4550 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4551 but we optimize comparisons, &&, ||, and !.
4553 The resulting type should always be `truthvalue_type_node'. */
4555 tree
4556 c_common_truthvalue_conversion (location_t location, tree expr)
4558 switch (TREE_CODE (expr))
4560 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4561 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4562 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4563 case ORDERED_EXPR: case UNORDERED_EXPR:
4564 if (TREE_TYPE (expr) == truthvalue_type_node)
4565 return expr;
4566 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4567 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4568 goto ret;
4570 case TRUTH_ANDIF_EXPR:
4571 case TRUTH_ORIF_EXPR:
4572 case TRUTH_AND_EXPR:
4573 case TRUTH_OR_EXPR:
4574 case TRUTH_XOR_EXPR:
4575 if (TREE_TYPE (expr) == truthvalue_type_node)
4576 return expr;
4577 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4578 c_common_truthvalue_conversion (location,
4579 TREE_OPERAND (expr, 0)),
4580 c_common_truthvalue_conversion (location,
4581 TREE_OPERAND (expr, 1)));
4582 goto ret;
4584 case TRUTH_NOT_EXPR:
4585 if (TREE_TYPE (expr) == truthvalue_type_node)
4586 return expr;
4587 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4588 c_common_truthvalue_conversion (location,
4589 TREE_OPERAND (expr, 0)));
4590 goto ret;
4592 case ERROR_MARK:
4593 return expr;
4595 case INTEGER_CST:
4596 return integer_zerop (expr) ? truthvalue_false_node
4597 : truthvalue_true_node;
4599 case REAL_CST:
4600 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4601 ? truthvalue_true_node
4602 : truthvalue_false_node;
4604 case FIXED_CST:
4605 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4606 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4607 ? truthvalue_true_node
4608 : truthvalue_false_node;
4610 case FUNCTION_DECL:
4611 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4612 /* Fall through. */
4614 case ADDR_EXPR:
4616 tree inner = TREE_OPERAND (expr, 0);
4617 if (decl_with_nonnull_addr_p (inner))
4619 /* Common Ada/Pascal programmer's mistake. */
4620 warning_at (location,
4621 OPT_Waddress,
4622 "the address of %qD will always evaluate as %<true%>",
4623 inner);
4624 return truthvalue_true_node;
4626 break;
4629 case COMPLEX_EXPR:
4630 expr = build_binary_op (EXPR_LOCATION (expr),
4631 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4632 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4633 c_common_truthvalue_conversion (location,
4634 TREE_OPERAND (expr, 0)),
4635 c_common_truthvalue_conversion (location,
4636 TREE_OPERAND (expr, 1)),
4638 goto ret;
4640 case NEGATE_EXPR:
4641 case ABS_EXPR:
4642 case FLOAT_EXPR:
4643 case EXCESS_PRECISION_EXPR:
4644 /* These don't change whether an object is nonzero or zero. */
4645 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4647 case LROTATE_EXPR:
4648 case RROTATE_EXPR:
4649 /* These don't change whether an object is zero or nonzero, but
4650 we can't ignore them if their second arg has side-effects. */
4651 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4653 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4654 TREE_OPERAND (expr, 1),
4655 c_common_truthvalue_conversion
4656 (location, TREE_OPERAND (expr, 0)));
4657 goto ret;
4659 else
4660 return c_common_truthvalue_conversion (location,
4661 TREE_OPERAND (expr, 0));
4663 case COND_EXPR:
4664 /* Distribute the conversion into the arms of a COND_EXPR. */
4665 if (c_dialect_cxx ())
4667 tree op1 = TREE_OPERAND (expr, 1);
4668 tree op2 = TREE_OPERAND (expr, 2);
4669 /* In C++ one of the arms might have void type if it is throw. */
4670 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4671 op1 = c_common_truthvalue_conversion (location, op1);
4672 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4673 op2 = c_common_truthvalue_conversion (location, op2);
4674 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4675 TREE_OPERAND (expr, 0), op1, op2);
4676 goto ret;
4678 else
4680 /* Folding will happen later for C. */
4681 expr = build3 (COND_EXPR, truthvalue_type_node,
4682 TREE_OPERAND (expr, 0),
4683 c_common_truthvalue_conversion (location,
4684 TREE_OPERAND (expr, 1)),
4685 c_common_truthvalue_conversion (location,
4686 TREE_OPERAND (expr, 2)));
4687 goto ret;
4690 CASE_CONVERT:
4692 tree totype = TREE_TYPE (expr);
4693 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4695 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4696 since that affects how `default_conversion' will behave. */
4697 if (TREE_CODE (totype) == REFERENCE_TYPE
4698 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4699 break;
4700 /* Don't strip a conversion from C++0x scoped enum, since they
4701 don't implicitly convert to other types. */
4702 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4703 && ENUM_IS_SCOPED (fromtype))
4704 break;
4705 /* If this isn't narrowing the argument, we can ignore it. */
4706 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4707 return c_common_truthvalue_conversion (location,
4708 TREE_OPERAND (expr, 0));
4710 break;
4712 case MODIFY_EXPR:
4713 if (!TREE_NO_WARNING (expr)
4714 && warn_parentheses)
4716 warning (OPT_Wparentheses,
4717 "suggest parentheses around assignment used as truth value");
4718 TREE_NO_WARNING (expr) = 1;
4720 break;
4722 default:
4723 break;
4726 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4728 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4729 expr = (build_binary_op
4730 (EXPR_LOCATION (expr),
4731 (TREE_SIDE_EFFECTS (expr)
4732 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4733 c_common_truthvalue_conversion
4734 (location,
4735 build_unary_op (location, REALPART_EXPR, t, 0)),
4736 c_common_truthvalue_conversion
4737 (location,
4738 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4739 0));
4740 goto ret;
4743 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4745 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4746 FCONST0 (TYPE_MODE
4747 (TREE_TYPE (expr))));
4748 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4750 else
4751 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4753 ret:
4754 protected_set_expr_location (expr, location);
4755 return expr;
4758 static void def_builtin_1 (enum built_in_function fncode,
4759 const char *name,
4760 enum built_in_class fnclass,
4761 tree fntype, tree libtype,
4762 bool both_p, bool fallback_p, bool nonansi_p,
4763 tree fnattrs, bool implicit_p);
4766 /* Apply the TYPE_QUALS to the new DECL. */
4768 void
4769 c_apply_type_quals_to_decl (int type_quals, tree decl)
4771 tree type = TREE_TYPE (decl);
4773 if (type == error_mark_node)
4774 return;
4776 if ((type_quals & TYPE_QUAL_CONST)
4777 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4778 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4779 constructor can produce constant init, so rely on cp_finish_decl to
4780 clear TREE_READONLY if the variable has non-constant init. */
4781 TREE_READONLY (decl) = 1;
4782 if (type_quals & TYPE_QUAL_VOLATILE)
4784 TREE_SIDE_EFFECTS (decl) = 1;
4785 TREE_THIS_VOLATILE (decl) = 1;
4787 if (type_quals & TYPE_QUAL_RESTRICT)
4789 while (type && TREE_CODE (type) == ARRAY_TYPE)
4790 /* Allow 'restrict' on arrays of pointers.
4791 FIXME currently we just ignore it. */
4792 type = TREE_TYPE (type);
4793 if (!type
4794 || !POINTER_TYPE_P (type)
4795 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4796 error ("invalid use of %<restrict%>");
4798 if (type_quals & TYPE_QUAL_SHARED)
4800 TREE_SHARED (decl) = 1;
4801 if (type_quals & TYPE_QUAL_STRICT)
4802 TREE_STRICT(decl) = 1;
4803 else if (type_quals & TYPE_QUAL_RELAXED)
4804 TREE_RELAXED(decl) = 1;
4805 /* The declaration's type should have been previously defined
4806 as a UPC shared type. */
4807 gcc_assert (upc_shared_type_p (type));
4811 /* Hash function for the problem of multiple type definitions in
4812 different files. This must hash all types that will compare
4813 equal via comptypes to the same value. In practice it hashes
4814 on some of the simple stuff and leaves the details to comptypes. */
4816 static hashval_t
4817 c_type_hash (const void *p)
4819 int n_elements;
4820 int shift, size;
4821 const_tree const t = (const_tree) p;
4822 tree t2;
4823 switch (TREE_CODE (t))
4825 /* For pointers, hash on pointee type plus some swizzling. */
4826 case POINTER_TYPE:
4827 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4828 /* Hash on number of elements and total size. */
4829 case ENUMERAL_TYPE:
4830 shift = 3;
4831 t2 = TYPE_VALUES (t);
4832 break;
4833 case RECORD_TYPE:
4834 shift = 0;
4835 t2 = TYPE_FIELDS (t);
4836 break;
4837 case QUAL_UNION_TYPE:
4838 shift = 1;
4839 t2 = TYPE_FIELDS (t);
4840 break;
4841 case UNION_TYPE:
4842 shift = 2;
4843 t2 = TYPE_FIELDS (t);
4844 break;
4845 default:
4846 gcc_unreachable ();
4848 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4849 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4850 n_elements = list_length (t2);
4851 /* We might have a VLA here. */
4852 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4853 size = 0;
4854 else
4855 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4856 return ((size << 24) | (n_elements << shift));
4859 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4861 /* Return the typed-based alias set for T, which may be an expression
4862 or a type. Return -1 if we don't do anything special. */
4864 alias_set_type
4865 c_common_get_alias_set (tree t)
4867 tree u;
4868 PTR *slot;
4870 /* For VLAs, use the alias set of the element type rather than the
4871 default of alias set 0 for types compared structurally. */
4872 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4874 if (TREE_CODE (t) == ARRAY_TYPE)
4875 return get_alias_set (TREE_TYPE (t));
4876 return -1;
4879 /* Permit type-punning when accessing a union, provided the access
4880 is directly through the union. For example, this code does not
4881 permit taking the address of a union member and then storing
4882 through it. Even the type-punning allowed here is a GCC
4883 extension, albeit a common and useful one; the C standard says
4884 that such accesses have implementation-defined behavior. */
4885 for (u = t;
4886 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4887 u = TREE_OPERAND (u, 0))
4888 if (TREE_CODE (u) == COMPONENT_REF
4889 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4890 return 0;
4892 /* That's all the expressions we handle specially. */
4893 if (!TYPE_P (t))
4894 return -1;
4896 /* The C standard guarantees that any object may be accessed via an
4897 lvalue that has character type. */
4898 if (t == char_type_node
4899 || t == signed_char_type_node
4900 || t == unsigned_char_type_node)
4901 return 0;
4903 /* The C standard specifically allows aliasing between signed and
4904 unsigned variants of the same type. We treat the signed
4905 variant as canonical. */
4906 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4908 tree t1 = c_common_signed_type (t);
4910 /* t1 == t can happen for boolean nodes which are always unsigned. */
4911 if (t1 != t)
4912 return get_alias_set (t1);
4915 /* For the time being, make UPC pointers-to-shared conflict
4916 with everything else. Ideally, UPC pointers-to-shared should
4917 only conflict with the internal type used to represent
4918 the UPC pointer-to-shared (i.e., upc_pts_rep_type_node). */
4920 if (TYPE_P (t) ? (TREE_CODE (t) == POINTER_TYPE
4921 && upc_shared_type_p (TREE_TYPE (t)))
4922 : (TREE_TYPE(t)
4923 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
4924 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (t)))))
4925 return 0;
4927 /* Handle the case of multiple type nodes referring to "the same" type,
4928 which occurs with IMA. These share an alias set. FIXME: Currently only
4929 C90 is handled. (In C99 type compatibility is not transitive, which
4930 complicates things mightily. The alias set splay trees can theoretically
4931 represent this, but insertion is tricky when you consider all the
4932 different orders things might arrive in.) */
4934 if (c_language != clk_c || flag_isoc99)
4935 return -1;
4937 /* Save time if there's only one input file. */
4938 if (num_in_fnames == 1)
4939 return -1;
4941 /* Pointers need special handling if they point to any type that
4942 needs special handling (below). */
4943 if (TREE_CODE (t) == POINTER_TYPE)
4945 tree t2;
4946 /* Find bottom type under any nested POINTERs. */
4947 for (t2 = TREE_TYPE (t);
4948 TREE_CODE (t2) == POINTER_TYPE;
4949 t2 = TREE_TYPE (t2))
4951 if (TREE_CODE (t2) != RECORD_TYPE
4952 && TREE_CODE (t2) != ENUMERAL_TYPE
4953 && TREE_CODE (t2) != QUAL_UNION_TYPE
4954 && TREE_CODE (t2) != UNION_TYPE)
4955 return -1;
4956 if (TYPE_SIZE (t2) == 0)
4957 return -1;
4959 /* These are the only cases that need special handling. */
4960 if (TREE_CODE (t) != RECORD_TYPE
4961 && TREE_CODE (t) != ENUMERAL_TYPE
4962 && TREE_CODE (t) != QUAL_UNION_TYPE
4963 && TREE_CODE (t) != UNION_TYPE
4964 && TREE_CODE (t) != POINTER_TYPE)
4965 return -1;
4966 /* Undefined? */
4967 if (TYPE_SIZE (t) == 0)
4968 return -1;
4970 /* Look up t in hash table. Only one of the compatible types within each
4971 alias set is recorded in the table. */
4972 if (!type_hash_table)
4973 type_hash_table = htab_create_ggc (1021, c_type_hash,
4974 (htab_eq) lang_hooks.types_compatible_p,
4975 NULL);
4976 slot = htab_find_slot (type_hash_table, t, INSERT);
4977 if (*slot != NULL)
4979 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4980 return TYPE_ALIAS_SET ((tree)*slot);
4982 else
4983 /* Our caller will assign and record (in t) a new alias set; all we need
4984 to do is remember t in the hash table. */
4985 *slot = t;
4987 return -1;
4990 /* Return the value of THREADS.
4992 UPC defines a reserved variable, THREADS, which returns the
4993 number of threads that will be created when the UPC program
4994 executes. The value of threads can be specified at runtime via
4995 the -fupc-threads=N switch, where N is an integer specifying
4996 the number of threads. When the value of THREADS is specified
4997 at compile-time, this is called the "static threads compilation
4998 environment".
5000 In the static threads compilation environment, THREADS is a
5001 pre-defined preprocessor macro with the value, N.
5003 If no value for threads is given at compile-time, then the value
5004 must be specified when the application program is executed.
5005 This method of establishing the value of THREADS is called
5006 the "dynamic threads compilation environment". */
5008 tree
5009 upc_num_threads (void)
5011 tree n;
5012 gcc_assert (flag_upc);
5013 n = flag_upc_threads ? ssize_int (flag_upc_threads)
5014 : lookup_name (get_identifier ("THREADS"));
5015 if (!n)
5017 error ("the UPC-required THREADS variable is undefined; "
5018 "when compiling pre-processed source, "
5019 "all -fupc-* switches must be passed on the command line, "
5020 "asserting the same values as supplied when the "
5021 "original source file was preprocessed");
5022 abort ();
5025 return n;
5028 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5029 the IS_SIZEOF parameter indicates which operator is being applied.
5030 The COMPLAIN flag controls whether we should diagnose possibly
5031 ill-formed constructs or not. LOC is the location of the SIZEOF or
5032 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5033 a type in any context should be returned, rather than the normal
5034 alignment for that type. */
5036 tree
5037 c_sizeof_or_alignof_type (location_t loc,
5038 tree type, bool is_sizeof, bool min_alignof,
5039 int complain)
5041 const char *op_name;
5042 tree value = NULL;
5043 enum tree_code type_code = TREE_CODE (type);
5045 op_name = is_sizeof ? "sizeof" : "__alignof__";
5047 if (type_code == FUNCTION_TYPE)
5049 if (is_sizeof)
5051 if (complain && warn_pointer_arith)
5052 pedwarn (loc, OPT_Wpointer_arith,
5053 "invalid application of %<sizeof%> to a function type");
5054 else if (!complain)
5055 return error_mark_node;
5056 value = size_one_node;
5058 else
5060 if (complain)
5062 if (c_dialect_cxx ())
5063 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5064 "%<alignof%> applied to a function type");
5065 else
5066 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5067 "%<_Alignof%> applied to a function type");
5069 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5072 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5074 if (complain)
5076 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5077 error_at (loc, "invalid application of %qs"
5078 " to %<shared void%> type", op_name);
5079 else if (type_code == VOID_TYPE && warn_pointer_arith)
5080 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5081 "invalid application of %qs to a void type", op_name);
5083 else
5084 return error_mark_node;
5085 value = size_one_node;
5087 else if (!COMPLETE_TYPE_P (type)
5088 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5090 if (complain)
5091 error_at (loc, "invalid application of %qs to incomplete type %qT",
5092 op_name, type);
5093 return error_mark_node;
5095 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5096 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5098 if (complain)
5099 error_at (loc, "invalid application of %qs to array type %qT of "
5100 "incomplete element type", op_name, type);
5101 return error_mark_node;
5103 else
5105 if (is_sizeof)
5106 /* Convert in case a char is more than one unit. */
5107 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5108 size_int (TYPE_PRECISION (char_type_node)
5109 / BITS_PER_UNIT));
5110 else if (min_alignof)
5112 unsigned int align = TYPE_ALIGN (type);
5113 align = MIN (align, BIGGEST_ALIGNMENT);
5114 #ifdef BIGGEST_FIELD_ALIGNMENT
5115 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
5116 #endif
5117 unsigned int field_align = align;
5118 #ifdef ADJUST_FIELD_ALIGN
5119 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
5120 type);
5121 field_align = ADJUST_FIELD_ALIGN (field, field_align);
5122 #endif
5123 align = MIN (align, field_align);
5124 value = size_int (align / BITS_PER_UNIT);
5126 else
5127 value = size_int (TYPE_ALIGN_UNIT (type));
5130 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5131 && upc_shared_type_p (type)
5132 && TYPE_HAS_THREADS_FACTOR (type))
5134 const tree n_threads = convert (sizetype, upc_num_threads ());
5135 value = size_binop (MULT_EXPR, value, n_threads);
5138 /* VALUE will have the middle-end integer type sizetype.
5139 However, we should really return a value of type `size_t',
5140 which is just a typedef for an ordinary integer type. */
5141 value = fold_convert_loc (loc, size_type_node, value);
5143 return value;
5146 /* Implement the __alignof keyword: Return the minimum required
5147 alignment of EXPR, measured in bytes. For VAR_DECLs,
5148 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5149 from an "aligned" __attribute__ specification). LOC is the
5150 location of the ALIGNOF operator. */
5152 tree
5153 c_alignof_expr (location_t loc, tree expr)
5155 tree t;
5157 if (VAR_OR_FUNCTION_DECL_P (expr))
5158 t = size_int (DECL_ALIGN_UNIT (expr));
5160 else if (TREE_CODE (expr) == COMPONENT_REF
5161 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5163 error_at (loc, "%<__alignof%> applied to a bit-field");
5164 t = size_one_node;
5166 else if (TREE_CODE (expr) == COMPONENT_REF
5167 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5168 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5170 else if (TREE_CODE (expr) == INDIRECT_REF)
5172 tree t = TREE_OPERAND (expr, 0);
5173 tree best = t;
5174 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5176 while (CONVERT_EXPR_P (t)
5177 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5179 int thisalign;
5181 t = TREE_OPERAND (t, 0);
5182 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5183 if (thisalign > bestalign)
5184 best = t, bestalign = thisalign;
5186 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5188 else
5189 return c_alignof (loc, TREE_TYPE (expr));
5191 return fold_convert_loc (loc, size_type_node, t);
5194 /* Handle C and C++ default attributes. */
5196 enum built_in_attribute
5198 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5199 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5200 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5201 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5202 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5203 #include "builtin-attrs.def"
5204 #undef DEF_ATTR_NULL_TREE
5205 #undef DEF_ATTR_INT
5206 #undef DEF_ATTR_STRING
5207 #undef DEF_ATTR_IDENT
5208 #undef DEF_ATTR_TREE_LIST
5209 ATTR_LAST
5212 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5214 static void c_init_attributes (void);
5216 enum c_builtin_type
5218 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5219 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5220 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5221 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5222 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5223 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5224 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5225 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5226 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5227 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5228 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5229 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5230 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5231 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5232 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5233 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5234 NAME,
5235 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5236 #include "builtin-types.def"
5237 #undef DEF_PRIMITIVE_TYPE
5238 #undef DEF_FUNCTION_TYPE_0
5239 #undef DEF_FUNCTION_TYPE_1
5240 #undef DEF_FUNCTION_TYPE_2
5241 #undef DEF_FUNCTION_TYPE_3
5242 #undef DEF_FUNCTION_TYPE_4
5243 #undef DEF_FUNCTION_TYPE_5
5244 #undef DEF_FUNCTION_TYPE_6
5245 #undef DEF_FUNCTION_TYPE_7
5246 #undef DEF_FUNCTION_TYPE_8
5247 #undef DEF_FUNCTION_TYPE_VAR_0
5248 #undef DEF_FUNCTION_TYPE_VAR_1
5249 #undef DEF_FUNCTION_TYPE_VAR_2
5250 #undef DEF_FUNCTION_TYPE_VAR_3
5251 #undef DEF_FUNCTION_TYPE_VAR_4
5252 #undef DEF_FUNCTION_TYPE_VAR_5
5253 #undef DEF_POINTER_TYPE
5254 BT_LAST
5257 typedef enum c_builtin_type builtin_type;
5259 /* A temporary array for c_common_nodes_and_builtins. Used in
5260 communication with def_fn_type. */
5261 static tree builtin_types[(int) BT_LAST + 1];
5263 /* A helper function for c_common_nodes_and_builtins. Build function type
5264 for DEF with return type RET and N arguments. If VAR is true, then the
5265 function should be variadic after those N arguments.
5267 Takes special care not to ICE if any of the types involved are
5268 error_mark_node, which indicates that said type is not in fact available
5269 (see builtin_type_for_size). In which case the function type as a whole
5270 should be error_mark_node. */
5272 static void
5273 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5275 tree t;
5276 tree *args = XALLOCAVEC (tree, n);
5277 va_list list;
5278 int i;
5280 va_start (list, n);
5281 for (i = 0; i < n; ++i)
5283 builtin_type a = (builtin_type) va_arg (list, int);
5284 t = builtin_types[a];
5285 if (t == error_mark_node)
5286 goto egress;
5287 args[i] = t;
5290 t = builtin_types[ret];
5291 if (t == error_mark_node)
5292 goto egress;
5293 if (var)
5294 t = build_varargs_function_type_array (t, n, args);
5295 else
5296 t = build_function_type_array (t, n, args);
5298 egress:
5299 builtin_types[def] = t;
5300 va_end (list);
5303 /* Build builtin functions common to both C and C++ language
5304 frontends. */
5306 static void
5307 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5309 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5310 builtin_types[ENUM] = VALUE;
5311 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5312 def_fn_type (ENUM, RETURN, 0, 0);
5313 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5314 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5315 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5316 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5317 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5318 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5319 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5320 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5321 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5322 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5323 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5324 ARG6) \
5325 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5326 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5327 ARG6, ARG7) \
5328 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5329 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5330 ARG6, ARG7, ARG8) \
5331 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5332 ARG7, ARG8);
5333 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5334 def_fn_type (ENUM, RETURN, 1, 0);
5335 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5336 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5337 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5338 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5339 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5340 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5341 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5342 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5343 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5344 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5345 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5346 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5348 #include "builtin-types.def"
5350 #undef DEF_PRIMITIVE_TYPE
5351 #undef DEF_FUNCTION_TYPE_1
5352 #undef DEF_FUNCTION_TYPE_2
5353 #undef DEF_FUNCTION_TYPE_3
5354 #undef DEF_FUNCTION_TYPE_4
5355 #undef DEF_FUNCTION_TYPE_5
5356 #undef DEF_FUNCTION_TYPE_6
5357 #undef DEF_FUNCTION_TYPE_VAR_0
5358 #undef DEF_FUNCTION_TYPE_VAR_1
5359 #undef DEF_FUNCTION_TYPE_VAR_2
5360 #undef DEF_FUNCTION_TYPE_VAR_3
5361 #undef DEF_FUNCTION_TYPE_VAR_4
5362 #undef DEF_FUNCTION_TYPE_VAR_5
5363 #undef DEF_POINTER_TYPE
5364 builtin_types[(int) BT_LAST] = NULL_TREE;
5366 c_init_attributes ();
5368 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5369 NONANSI_P, ATTRS, IMPLICIT, COND) \
5370 if (NAME && COND) \
5371 def_builtin_1 (ENUM, NAME, CLASS, \
5372 builtin_types[(int) TYPE], \
5373 builtin_types[(int) LIBTYPE], \
5374 BOTH_P, FALLBACK_P, NONANSI_P, \
5375 built_in_attributes[(int) ATTRS], IMPLICIT);
5376 #include "builtins.def"
5377 #undef DEF_BUILTIN
5379 targetm.init_builtins ();
5381 build_common_builtin_nodes ();
5383 if (flag_cilkplus)
5384 cilk_init_builtins ();
5387 /* Like get_identifier, but avoid warnings about null arguments when
5388 the argument may be NULL for targets where GCC lacks stdint.h type
5389 information. */
5391 static inline tree
5392 c_get_ident (const char *id)
5394 return get_identifier (id);
5397 /* Build tree nodes and builtin functions common to both C and C++ language
5398 frontends. */
5400 void
5401 c_common_nodes_and_builtins (void)
5403 int char16_type_size;
5404 int char32_type_size;
5405 int wchar_type_size;
5406 tree array_domain_type;
5407 tree va_list_ref_type_node;
5408 tree va_list_arg_type_node;
5410 build_common_tree_nodes (flag_signed_char, flag_short_double);
5412 /* Define `int' and `char' first so that dbx will output them first. */
5413 record_builtin_type (RID_INT, NULL, integer_type_node);
5414 record_builtin_type (RID_CHAR, "char", char_type_node);
5416 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5417 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5418 but not C. Are the conditionals here needed? */
5419 if (c_dialect_cxx ())
5420 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5421 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5422 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5423 record_builtin_type (RID_MAX, "long unsigned int",
5424 long_unsigned_type_node);
5425 if (int128_integer_type_node != NULL_TREE)
5427 record_builtin_type (RID_INT128, "__int128",
5428 int128_integer_type_node);
5429 record_builtin_type (RID_MAX, "__int128 unsigned",
5430 int128_unsigned_type_node);
5432 if (c_dialect_cxx ())
5433 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5434 record_builtin_type (RID_MAX, "long long int",
5435 long_long_integer_type_node);
5436 record_builtin_type (RID_MAX, "long long unsigned int",
5437 long_long_unsigned_type_node);
5438 if (c_dialect_cxx ())
5439 record_builtin_type (RID_MAX, "long long unsigned",
5440 long_long_unsigned_type_node);
5441 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5442 record_builtin_type (RID_MAX, "short unsigned int",
5443 short_unsigned_type_node);
5444 if (c_dialect_cxx ())
5445 record_builtin_type (RID_MAX, "unsigned short",
5446 short_unsigned_type_node);
5448 /* Define both `signed char' and `unsigned char'. */
5449 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5450 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5452 /* These are types that c_common_type_for_size and
5453 c_common_type_for_mode use. */
5454 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5455 TYPE_DECL, NULL_TREE,
5456 intQI_type_node));
5457 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5458 TYPE_DECL, NULL_TREE,
5459 intHI_type_node));
5460 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5461 TYPE_DECL, NULL_TREE,
5462 intSI_type_node));
5463 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5464 TYPE_DECL, NULL_TREE,
5465 intDI_type_node));
5466 #if HOST_BITS_PER_WIDE_INT >= 64
5467 if (targetm.scalar_mode_supported_p (TImode))
5468 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5469 TYPE_DECL,
5470 get_identifier ("__int128_t"),
5471 intTI_type_node));
5472 #endif
5473 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5474 TYPE_DECL, NULL_TREE,
5475 unsigned_intQI_type_node));
5476 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5477 TYPE_DECL, NULL_TREE,
5478 unsigned_intHI_type_node));
5479 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5480 TYPE_DECL, NULL_TREE,
5481 unsigned_intSI_type_node));
5482 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5483 TYPE_DECL, NULL_TREE,
5484 unsigned_intDI_type_node));
5485 #if HOST_BITS_PER_WIDE_INT >= 64
5486 if (targetm.scalar_mode_supported_p (TImode))
5487 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5488 TYPE_DECL,
5489 get_identifier ("__uint128_t"),
5490 unsigned_intTI_type_node));
5491 #endif
5493 /* Create the widest literal types. */
5494 widest_integer_literal_type_node
5495 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5496 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5497 TYPE_DECL, NULL_TREE,
5498 widest_integer_literal_type_node));
5500 widest_unsigned_literal_type_node
5501 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5502 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5503 TYPE_DECL, NULL_TREE,
5504 widest_unsigned_literal_type_node));
5506 signed_size_type_node = c_common_signed_type (size_type_node);
5508 pid_type_node =
5509 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5511 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5512 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5513 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5515 /* Only supported decimal floating point extension if the target
5516 actually supports underlying modes. */
5517 if (targetm.scalar_mode_supported_p (SDmode)
5518 && targetm.scalar_mode_supported_p (DDmode)
5519 && targetm.scalar_mode_supported_p (TDmode))
5521 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5522 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5523 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5526 if (targetm.fixed_point_supported_p ())
5528 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5529 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5530 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5531 record_builtin_type (RID_MAX, "long long _Fract",
5532 long_long_fract_type_node);
5533 record_builtin_type (RID_MAX, "unsigned short _Fract",
5534 unsigned_short_fract_type_node);
5535 record_builtin_type (RID_MAX, "unsigned _Fract",
5536 unsigned_fract_type_node);
5537 record_builtin_type (RID_MAX, "unsigned long _Fract",
5538 unsigned_long_fract_type_node);
5539 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5540 unsigned_long_long_fract_type_node);
5541 record_builtin_type (RID_MAX, "_Sat short _Fract",
5542 sat_short_fract_type_node);
5543 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5544 record_builtin_type (RID_MAX, "_Sat long _Fract",
5545 sat_long_fract_type_node);
5546 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5547 sat_long_long_fract_type_node);
5548 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5549 sat_unsigned_short_fract_type_node);
5550 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5551 sat_unsigned_fract_type_node);
5552 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5553 sat_unsigned_long_fract_type_node);
5554 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5555 sat_unsigned_long_long_fract_type_node);
5556 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5557 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5558 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5559 record_builtin_type (RID_MAX, "long long _Accum",
5560 long_long_accum_type_node);
5561 record_builtin_type (RID_MAX, "unsigned short _Accum",
5562 unsigned_short_accum_type_node);
5563 record_builtin_type (RID_MAX, "unsigned _Accum",
5564 unsigned_accum_type_node);
5565 record_builtin_type (RID_MAX, "unsigned long _Accum",
5566 unsigned_long_accum_type_node);
5567 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5568 unsigned_long_long_accum_type_node);
5569 record_builtin_type (RID_MAX, "_Sat short _Accum",
5570 sat_short_accum_type_node);
5571 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5572 record_builtin_type (RID_MAX, "_Sat long _Accum",
5573 sat_long_accum_type_node);
5574 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5575 sat_long_long_accum_type_node);
5576 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5577 sat_unsigned_short_accum_type_node);
5578 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5579 sat_unsigned_accum_type_node);
5580 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5581 sat_unsigned_long_accum_type_node);
5582 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5583 sat_unsigned_long_long_accum_type_node);
5587 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5588 TYPE_DECL,
5589 get_identifier ("complex int"),
5590 complex_integer_type_node));
5591 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5592 TYPE_DECL,
5593 get_identifier ("complex float"),
5594 complex_float_type_node));
5595 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5596 TYPE_DECL,
5597 get_identifier ("complex double"),
5598 complex_double_type_node));
5599 lang_hooks.decls.pushdecl
5600 (build_decl (UNKNOWN_LOCATION,
5601 TYPE_DECL, get_identifier ("complex long double"),
5602 complex_long_double_type_node));
5604 if (c_dialect_cxx ())
5605 /* For C++, make fileptr_type_node a distinct void * type until
5606 FILE type is defined. */
5607 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5609 record_builtin_type (RID_VOID, NULL, void_type_node);
5611 /* Set the TYPE_NAME for any variants that were built before
5612 record_builtin_type gave names to the built-in types. */
5614 tree void_name = TYPE_NAME (void_type_node);
5615 TYPE_NAME (void_type_node) = NULL_TREE;
5616 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5617 = void_name;
5618 TYPE_NAME (void_type_node) = void_name;
5621 /* This node must not be shared. */
5622 void_zero_node = make_node (INTEGER_CST);
5623 TREE_TYPE (void_zero_node) = void_type_node;
5625 void_list_node = build_void_list_node ();
5627 /* Make a type to be the domain of a few array types
5628 whose domains don't really matter.
5629 200 is small enough that it always fits in size_t
5630 and large enough that it can hold most function names for the
5631 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5632 array_domain_type = build_index_type (size_int (200));
5634 /* Make a type for arrays of characters.
5635 With luck nothing will ever really depend on the length of this
5636 array type. */
5637 char_array_type_node
5638 = build_array_type (char_type_node, array_domain_type);
5640 string_type_node = build_pointer_type (char_type_node);
5641 const_string_type_node
5642 = build_pointer_type (build_qualified_type
5643 (char_type_node, TYPE_QUAL_CONST));
5645 /* This is special for C++ so functions can be overloaded. */
5646 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5647 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5648 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5649 underlying_wchar_type_node = wchar_type_node;
5650 if (c_dialect_cxx ())
5652 if (TYPE_UNSIGNED (wchar_type_node))
5653 wchar_type_node = make_unsigned_type (wchar_type_size);
5654 else
5655 wchar_type_node = make_signed_type (wchar_type_size);
5656 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5659 /* This is for wide string constants. */
5660 wchar_array_type_node
5661 = build_array_type (wchar_type_node, array_domain_type);
5663 /* Define 'char16_t'. */
5664 char16_type_node = get_identifier (CHAR16_TYPE);
5665 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5666 char16_type_size = TYPE_PRECISION (char16_type_node);
5667 if (c_dialect_cxx ())
5669 char16_type_node = make_unsigned_type (char16_type_size);
5671 if (cxx_dialect >= cxx11)
5672 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5675 /* This is for UTF-16 string constants. */
5676 char16_array_type_node
5677 = build_array_type (char16_type_node, array_domain_type);
5679 /* Define 'char32_t'. */
5680 char32_type_node = get_identifier (CHAR32_TYPE);
5681 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5682 char32_type_size = TYPE_PRECISION (char32_type_node);
5683 if (c_dialect_cxx ())
5685 char32_type_node = make_unsigned_type (char32_type_size);
5687 if (cxx_dialect >= cxx11)
5688 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5691 /* This is for UTF-32 string constants. */
5692 char32_array_type_node
5693 = build_array_type (char32_type_node, array_domain_type);
5695 wint_type_node =
5696 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5698 intmax_type_node =
5699 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5700 uintmax_type_node =
5701 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5703 if (SIG_ATOMIC_TYPE)
5704 sig_atomic_type_node =
5705 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5706 if (INT8_TYPE)
5707 int8_type_node =
5708 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5709 if (INT16_TYPE)
5710 int16_type_node =
5711 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5712 if (INT32_TYPE)
5713 int32_type_node =
5714 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5715 if (INT64_TYPE)
5716 int64_type_node =
5717 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5718 if (UINT8_TYPE)
5719 uint8_type_node =
5720 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5721 if (UINT16_TYPE)
5722 c_uint16_type_node = uint16_type_node =
5723 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5724 if (UINT32_TYPE)
5725 c_uint32_type_node = uint32_type_node =
5726 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5727 if (UINT64_TYPE)
5728 c_uint64_type_node = uint64_type_node =
5729 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5730 if (INT_LEAST8_TYPE)
5731 int_least8_type_node =
5732 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5733 if (INT_LEAST16_TYPE)
5734 int_least16_type_node =
5735 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5736 if (INT_LEAST32_TYPE)
5737 int_least32_type_node =
5738 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5739 if (INT_LEAST64_TYPE)
5740 int_least64_type_node =
5741 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5742 if (UINT_LEAST8_TYPE)
5743 uint_least8_type_node =
5744 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5745 if (UINT_LEAST16_TYPE)
5746 uint_least16_type_node =
5747 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5748 if (UINT_LEAST32_TYPE)
5749 uint_least32_type_node =
5750 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5751 if (UINT_LEAST64_TYPE)
5752 uint_least64_type_node =
5753 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5754 if (INT_FAST8_TYPE)
5755 int_fast8_type_node =
5756 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5757 if (INT_FAST16_TYPE)
5758 int_fast16_type_node =
5759 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5760 if (INT_FAST32_TYPE)
5761 int_fast32_type_node =
5762 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5763 if (INT_FAST64_TYPE)
5764 int_fast64_type_node =
5765 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5766 if (UINT_FAST8_TYPE)
5767 uint_fast8_type_node =
5768 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5769 if (UINT_FAST16_TYPE)
5770 uint_fast16_type_node =
5771 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5772 if (UINT_FAST32_TYPE)
5773 uint_fast32_type_node =
5774 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5775 if (UINT_FAST64_TYPE)
5776 uint_fast64_type_node =
5777 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5778 if (INTPTR_TYPE)
5779 intptr_type_node =
5780 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5781 if (UINTPTR_TYPE)
5782 uintptr_type_node =
5783 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5785 default_function_type
5786 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5787 ptrdiff_type_node
5788 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5789 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5791 lang_hooks.decls.pushdecl
5792 (build_decl (UNKNOWN_LOCATION,
5793 TYPE_DECL, get_identifier ("__builtin_va_list"),
5794 va_list_type_node));
5795 if (targetm.enum_va_list_p)
5797 int l;
5798 const char *pname;
5799 tree ptype;
5801 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5803 lang_hooks.decls.pushdecl
5804 (build_decl (UNKNOWN_LOCATION,
5805 TYPE_DECL, get_identifier (pname),
5806 ptype));
5811 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5813 va_list_arg_type_node = va_list_ref_type_node =
5814 build_pointer_type (TREE_TYPE (va_list_type_node));
5816 else
5818 va_list_arg_type_node = va_list_type_node;
5819 va_list_ref_type_node = build_reference_type (va_list_type_node);
5822 if (!flag_preprocess_only)
5823 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5825 main_identifier_node = get_identifier ("main");
5827 /* Create the built-in __null node. It is important that this is
5828 not shared. */
5829 null_node = make_node (INTEGER_CST);
5830 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5832 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5833 memset (builtin_types, 0, sizeof (builtin_types));
5836 /* The number of named compound-literals generated thus far. */
5837 static GTY(()) int compound_literal_number;
5839 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5841 void
5842 set_compound_literal_name (tree decl)
5844 char *name;
5845 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5846 compound_literal_number);
5847 compound_literal_number++;
5848 DECL_NAME (decl) = get_identifier (name);
5851 tree
5852 build_va_arg (location_t loc, tree expr, tree type)
5854 expr = build1 (VA_ARG_EXPR, type, expr);
5855 SET_EXPR_LOCATION (expr, loc);
5856 return expr;
5860 /* Linked list of disabled built-in functions. */
5862 typedef struct disabled_builtin
5864 const char *name;
5865 struct disabled_builtin *next;
5866 } disabled_builtin;
5867 static disabled_builtin *disabled_builtins = NULL;
5869 static bool builtin_function_disabled_p (const char *);
5871 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5872 begins with "__builtin_", give an error. */
5874 void
5875 disable_builtin_function (const char *name)
5877 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5878 error ("cannot disable built-in function %qs", name);
5879 else
5881 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5882 new_disabled_builtin->name = name;
5883 new_disabled_builtin->next = disabled_builtins;
5884 disabled_builtins = new_disabled_builtin;
5889 /* Return true if the built-in function NAME has been disabled, false
5890 otherwise. */
5892 static bool
5893 builtin_function_disabled_p (const char *name)
5895 disabled_builtin *p;
5896 for (p = disabled_builtins; p != NULL; p = p->next)
5898 if (strcmp (name, p->name) == 0)
5899 return true;
5901 return false;
5905 /* Worker for DEF_BUILTIN.
5906 Possibly define a builtin function with one or two names.
5907 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5908 nonansi_p and flag_no_nonansi_builtin. */
5910 static void
5911 def_builtin_1 (enum built_in_function fncode,
5912 const char *name,
5913 enum built_in_class fnclass,
5914 tree fntype, tree libtype,
5915 bool both_p, bool fallback_p, bool nonansi_p,
5916 tree fnattrs, bool implicit_p)
5918 tree decl;
5919 const char *libname;
5921 if (fntype == error_mark_node)
5922 return;
5924 gcc_assert ((!both_p && !fallback_p)
5925 || !strncmp (name, "__builtin_",
5926 strlen ("__builtin_")));
5928 libname = name + strlen ("__builtin_");
5929 decl = add_builtin_function (name, fntype, fncode, fnclass,
5930 (fallback_p ? libname : NULL),
5931 fnattrs);
5933 set_builtin_decl (fncode, decl, implicit_p);
5935 if (both_p
5936 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5937 && !(nonansi_p && flag_no_nonansi_builtin))
5938 add_builtin_function (libname, libtype, fncode, fnclass,
5939 NULL, fnattrs);
5942 /* Nonzero if the type T promotes to int. This is (nearly) the
5943 integral promotions defined in ISO C99 6.3.1.1/2. */
5945 bool
5946 c_promoting_integer_type_p (const_tree t)
5948 switch (TREE_CODE (t))
5950 case INTEGER_TYPE:
5951 return (TYPE_MAIN_VARIANT (t) == char_type_node
5952 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5953 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5954 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5955 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5956 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5958 case ENUMERAL_TYPE:
5959 /* ??? Technically all enumerations not larger than an int
5960 promote to an int. But this is used along code paths
5961 that only want to notice a size change. */
5962 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5964 case BOOLEAN_TYPE:
5965 return 1;
5967 default:
5968 return 0;
5972 /* Return 1 if PARMS specifies a fixed number of parameters
5973 and none of their types is affected by default promotions. */
5976 self_promoting_args_p (const_tree parms)
5978 const_tree t;
5979 for (t = parms; t; t = TREE_CHAIN (t))
5981 tree type = TREE_VALUE (t);
5983 if (type == error_mark_node)
5984 continue;
5986 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5987 return 0;
5989 if (type == 0)
5990 return 0;
5992 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5993 return 0;
5995 if (c_promoting_integer_type_p (type))
5996 return 0;
5998 return 1;
6001 /* Recursively remove any '*' or '&' operator from TYPE. */
6002 tree
6003 strip_pointer_operator (tree t)
6005 while (POINTER_TYPE_P (t))
6006 t = TREE_TYPE (t);
6007 return t;
6010 /* Recursively remove pointer or array type from TYPE. */
6011 tree
6012 strip_pointer_or_array_types (tree t)
6014 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6015 t = TREE_TYPE (t);
6016 return t;
6019 /* Used to compare case labels. K1 and K2 are actually tree nodes
6020 representing case labels, or NULL_TREE for a `default' label.
6021 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6022 K2, and 0 if K1 and K2 are equal. */
6025 case_compare (splay_tree_key k1, splay_tree_key k2)
6027 /* Consider a NULL key (such as arises with a `default' label) to be
6028 smaller than anything else. */
6029 if (!k1)
6030 return k2 ? -1 : 0;
6031 else if (!k2)
6032 return k1 ? 1 : 0;
6034 return tree_int_cst_compare ((tree) k1, (tree) k2);
6037 /* Process a case label, located at LOC, for the range LOW_VALUE
6038 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6039 then this case label is actually a `default' label. If only
6040 HIGH_VALUE is NULL_TREE, then case label was declared using the
6041 usual C/C++ syntax, rather than the GNU case range extension.
6042 CASES is a tree containing all the case ranges processed so far;
6043 COND is the condition for the switch-statement itself. Returns the
6044 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6045 is created. */
6047 tree
6048 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6049 tree low_value, tree high_value)
6051 tree type;
6052 tree label;
6053 tree case_label;
6054 splay_tree_node node;
6056 /* Create the LABEL_DECL itself. */
6057 label = create_artificial_label (loc);
6059 /* If there was an error processing the switch condition, bail now
6060 before we get more confused. */
6061 if (!cond || cond == error_mark_node)
6062 goto error_out;
6064 if ((low_value && TREE_TYPE (low_value)
6065 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6066 || (high_value && TREE_TYPE (high_value)
6067 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6069 error_at (loc, "pointers are not permitted as case values");
6070 goto error_out;
6073 /* Case ranges are a GNU extension. */
6074 if (high_value)
6075 pedwarn (loc, OPT_Wpedantic,
6076 "range expressions in switch statements are non-standard");
6078 type = TREE_TYPE (cond);
6079 if (low_value)
6081 low_value = check_case_value (low_value);
6082 low_value = convert_and_check (loc, type, low_value);
6083 if (low_value == error_mark_node)
6084 goto error_out;
6086 if (high_value)
6088 high_value = check_case_value (high_value);
6089 high_value = convert_and_check (loc, type, high_value);
6090 if (high_value == error_mark_node)
6091 goto error_out;
6094 if (low_value && high_value)
6096 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6097 really a case range, even though it was written that way.
6098 Remove the HIGH_VALUE to simplify later processing. */
6099 if (tree_int_cst_equal (low_value, high_value))
6100 high_value = NULL_TREE;
6101 else if (!tree_int_cst_lt (low_value, high_value))
6102 warning_at (loc, 0, "empty range specified");
6105 /* See if the case is in range of the type of the original testing
6106 expression. If both low_value and high_value are out of range,
6107 don't insert the case label and return NULL_TREE. */
6108 if (low_value
6109 && !check_case_bounds (type, orig_type,
6110 &low_value, high_value ? &high_value : NULL))
6111 return NULL_TREE;
6113 /* Look up the LOW_VALUE in the table of case labels we already
6114 have. */
6115 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6116 /* If there was not an exact match, check for overlapping ranges.
6117 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6118 that's a `default' label and the only overlap is an exact match. */
6119 if (!node && (low_value || high_value))
6121 splay_tree_node low_bound;
6122 splay_tree_node high_bound;
6124 /* Even though there wasn't an exact match, there might be an
6125 overlap between this case range and another case range.
6126 Since we've (inductively) not allowed any overlapping case
6127 ranges, we simply need to find the greatest low case label
6128 that is smaller that LOW_VALUE, and the smallest low case
6129 label that is greater than LOW_VALUE. If there is an overlap
6130 it will occur in one of these two ranges. */
6131 low_bound = splay_tree_predecessor (cases,
6132 (splay_tree_key) low_value);
6133 high_bound = splay_tree_successor (cases,
6134 (splay_tree_key) low_value);
6136 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6137 the LOW_VALUE, so there is no need to check unless the
6138 LOW_BOUND is in fact itself a case range. */
6139 if (low_bound
6140 && CASE_HIGH ((tree) low_bound->value)
6141 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6142 low_value) >= 0)
6143 node = low_bound;
6144 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6145 range is bigger than the low end of the current range, so we
6146 are only interested if the current range is a real range, and
6147 not an ordinary case label. */
6148 else if (high_bound
6149 && high_value
6150 && (tree_int_cst_compare ((tree) high_bound->key,
6151 high_value)
6152 <= 0))
6153 node = high_bound;
6155 /* If there was an overlap, issue an error. */
6156 if (node)
6158 tree duplicate = CASE_LABEL ((tree) node->value);
6160 if (high_value)
6162 error_at (loc, "duplicate (or overlapping) case value");
6163 error_at (DECL_SOURCE_LOCATION (duplicate),
6164 "this is the first entry overlapping that value");
6166 else if (low_value)
6168 error_at (loc, "duplicate case value") ;
6169 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6171 else
6173 error_at (loc, "multiple default labels in one switch");
6174 error_at (DECL_SOURCE_LOCATION (duplicate),
6175 "this is the first default label");
6177 goto error_out;
6180 /* Add a CASE_LABEL to the statement-tree. */
6181 case_label = add_stmt (build_case_label (low_value, high_value, label));
6182 /* Register this case label in the splay tree. */
6183 splay_tree_insert (cases,
6184 (splay_tree_key) low_value,
6185 (splay_tree_value) case_label);
6187 return case_label;
6189 error_out:
6190 /* Add a label so that the back-end doesn't think that the beginning of
6191 the switch is unreachable. Note that we do not add a case label, as
6192 that just leads to duplicates and thence to failure later on. */
6193 if (!cases->root)
6195 tree t = create_artificial_label (loc);
6196 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6198 return error_mark_node;
6201 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6202 Used to verify that case values match up with enumerator values. */
6204 static void
6205 match_case_to_enum_1 (tree key, tree type, tree label)
6207 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6209 /* ??? Not working too hard to print the double-word value.
6210 Should perhaps be done with %lwd in the diagnostic routines? */
6211 if (TREE_INT_CST_HIGH (key) == 0)
6212 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6213 TREE_INT_CST_LOW (key));
6214 else if (!TYPE_UNSIGNED (type)
6215 && TREE_INT_CST_HIGH (key) == -1
6216 && TREE_INT_CST_LOW (key) != 0)
6217 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6218 -TREE_INT_CST_LOW (key));
6219 else
6220 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6221 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6222 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6224 if (TYPE_NAME (type) == 0)
6225 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6226 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6227 "case value %qs not in enumerated type",
6228 buf);
6229 else
6230 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6231 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6232 "case value %qs not in enumerated type %qT",
6233 buf, type);
6236 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6237 Used to verify that case values match up with enumerator values. */
6239 static int
6240 match_case_to_enum (splay_tree_node node, void *data)
6242 tree label = (tree) node->value;
6243 tree type = (tree) data;
6245 /* Skip default case. */
6246 if (!CASE_LOW (label))
6247 return 0;
6249 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6250 when we did our enum->case scan. Reset our scratch bit after. */
6251 if (!CASE_LOW_SEEN (label))
6252 match_case_to_enum_1 (CASE_LOW (label), type, label);
6253 else
6254 CASE_LOW_SEEN (label) = 0;
6256 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6257 not set, that means that CASE_HIGH did not appear when we did our
6258 enum->case scan. Reset our scratch bit after. */
6259 if (CASE_HIGH (label))
6261 if (!CASE_HIGH_SEEN (label))
6262 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6263 else
6264 CASE_HIGH_SEEN (label) = 0;
6267 return 0;
6270 /* Handle -Wswitch*. Called from the front end after parsing the
6271 switch construct. */
6272 /* ??? Should probably be somewhere generic, since other languages
6273 besides C and C++ would want this. At the moment, however, C/C++
6274 are the only tree-ssa languages that support enumerations at all,
6275 so the point is moot. */
6277 void
6278 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6279 tree type, tree cond)
6281 splay_tree_node default_node;
6282 splay_tree_node node;
6283 tree chain;
6285 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6286 return;
6288 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6289 if (!default_node)
6290 warning_at (switch_location, OPT_Wswitch_default,
6291 "switch missing default case");
6293 /* From here on, we only care about about enumerated types. */
6294 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6295 return;
6297 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6298 if (!warn_switch_enum && !warn_switch)
6299 return;
6301 /* Check the cases. Warn about case values which are not members of
6302 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6303 there is no default case, check that exactly all enumeration
6304 literals are covered by the cases. */
6306 /* Clearing COND if it is not an integer constant simplifies
6307 the tests inside the loop below. */
6308 if (TREE_CODE (cond) != INTEGER_CST)
6309 cond = NULL_TREE;
6311 /* The time complexity here is O(N*lg(N)) worst case, but for the
6312 common case of monotonically increasing enumerators, it is
6313 O(N), since the nature of the splay tree will keep the next
6314 element adjacent to the root at all times. */
6316 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6318 tree value = TREE_VALUE (chain);
6319 if (TREE_CODE (value) == CONST_DECL)
6320 value = DECL_INITIAL (value);
6321 node = splay_tree_lookup (cases, (splay_tree_key) value);
6322 if (node)
6324 /* Mark the CASE_LOW part of the case entry as seen. */
6325 tree label = (tree) node->value;
6326 CASE_LOW_SEEN (label) = 1;
6327 continue;
6330 /* Even though there wasn't an exact match, there might be a
6331 case range which includes the enumerator's value. */
6332 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6333 if (node && CASE_HIGH ((tree) node->value))
6335 tree label = (tree) node->value;
6336 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6337 if (cmp >= 0)
6339 /* If we match the upper bound exactly, mark the CASE_HIGH
6340 part of the case entry as seen. */
6341 if (cmp == 0)
6342 CASE_HIGH_SEEN (label) = 1;
6343 continue;
6347 /* We've now determined that this enumerated literal isn't
6348 handled by the case labels of the switch statement. */
6350 /* If the switch expression is a constant, we only really care
6351 about whether that constant is handled by the switch. */
6352 if (cond && tree_int_cst_compare (cond, value))
6353 continue;
6355 /* If there is a default_node, the only relevant option is
6356 Wswitch-enum. Otherwise, if both are enabled then we prefer
6357 to warn using -Wswitch because -Wswitch is enabled by -Wall
6358 while -Wswitch-enum is explicit. */
6359 warning_at (switch_location,
6360 (default_node || !warn_switch
6361 ? OPT_Wswitch_enum
6362 : OPT_Wswitch),
6363 "enumeration value %qE not handled in switch",
6364 TREE_PURPOSE (chain));
6367 /* Warn if there are case expressions that don't correspond to
6368 enumerators. This can occur since C and C++ don't enforce
6369 type-checking of assignments to enumeration variables.
6371 The time complexity here is now always O(N) worst case, since
6372 we should have marked both the lower bound and upper bound of
6373 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6374 above. This scan also resets those fields. */
6376 splay_tree_foreach (cases, match_case_to_enum, type);
6379 /* Finish an expression taking the address of LABEL (an
6380 IDENTIFIER_NODE). Returns an expression for the address.
6382 LOC is the location for the expression returned. */
6384 tree
6385 finish_label_address_expr (tree label, location_t loc)
6387 tree result;
6389 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6391 if (label == error_mark_node)
6392 return error_mark_node;
6394 label = lookup_label (label);
6395 if (label == NULL_TREE)
6396 result = null_pointer_node;
6397 else
6399 TREE_USED (label) = 1;
6400 result = build1 (ADDR_EXPR, ptr_type_node, label);
6401 /* The current function is not necessarily uninlinable.
6402 Computed gotos are incompatible with inlining, but the value
6403 here could be used only in a diagnostic, for example. */
6404 protected_set_expr_location (result, loc);
6407 return result;
6411 /* Given a boolean expression ARG, return a tree representing an increment
6412 or decrement (as indicated by CODE) of ARG. The front end must check for
6413 invalid cases (e.g., decrement in C++). */
6414 tree
6415 boolean_increment (enum tree_code code, tree arg)
6417 tree val;
6418 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6420 arg = stabilize_reference (arg);
6421 switch (code)
6423 case PREINCREMENT_EXPR:
6424 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6425 break;
6426 case POSTINCREMENT_EXPR:
6427 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6428 arg = save_expr (arg);
6429 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6430 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6431 break;
6432 case PREDECREMENT_EXPR:
6433 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6434 invert_truthvalue_loc (input_location, arg));
6435 break;
6436 case POSTDECREMENT_EXPR:
6437 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6438 invert_truthvalue_loc (input_location, arg));
6439 arg = save_expr (arg);
6440 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6441 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6442 break;
6443 default:
6444 gcc_unreachable ();
6446 TREE_SIDE_EFFECTS (val) = 1;
6447 return val;
6450 /* Built-in macros for stddef.h and stdint.h, that require macros
6451 defined in this file. */
6452 void
6453 c_stddef_cpp_builtins(void)
6455 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6456 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6457 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6458 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6459 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6460 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6461 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6462 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6463 if (SIG_ATOMIC_TYPE)
6464 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6465 if (INT8_TYPE)
6466 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6467 if (INT16_TYPE)
6468 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6469 if (INT32_TYPE)
6470 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6471 if (INT64_TYPE)
6472 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6473 if (UINT8_TYPE)
6474 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6475 if (UINT16_TYPE)
6476 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6477 if (UINT32_TYPE)
6478 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6479 if (UINT64_TYPE)
6480 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6481 if (INT_LEAST8_TYPE)
6482 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6483 if (INT_LEAST16_TYPE)
6484 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6485 if (INT_LEAST32_TYPE)
6486 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6487 if (INT_LEAST64_TYPE)
6488 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6489 if (UINT_LEAST8_TYPE)
6490 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6491 if (UINT_LEAST16_TYPE)
6492 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6493 if (UINT_LEAST32_TYPE)
6494 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6495 if (UINT_LEAST64_TYPE)
6496 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6497 if (INT_FAST8_TYPE)
6498 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6499 if (INT_FAST16_TYPE)
6500 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6501 if (INT_FAST32_TYPE)
6502 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6503 if (INT_FAST64_TYPE)
6504 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6505 if (UINT_FAST8_TYPE)
6506 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6507 if (UINT_FAST16_TYPE)
6508 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6509 if (UINT_FAST32_TYPE)
6510 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6511 if (UINT_FAST64_TYPE)
6512 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6513 if (INTPTR_TYPE)
6514 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6515 if (UINTPTR_TYPE)
6516 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6519 static void
6520 c_init_attributes (void)
6522 /* Fill in the built_in_attributes array. */
6523 #define DEF_ATTR_NULL_TREE(ENUM) \
6524 built_in_attributes[(int) ENUM] = NULL_TREE;
6525 #define DEF_ATTR_INT(ENUM, VALUE) \
6526 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6527 #define DEF_ATTR_STRING(ENUM, VALUE) \
6528 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6529 #define DEF_ATTR_IDENT(ENUM, STRING) \
6530 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6531 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6532 built_in_attributes[(int) ENUM] \
6533 = tree_cons (built_in_attributes[(int) PURPOSE], \
6534 built_in_attributes[(int) VALUE], \
6535 built_in_attributes[(int) CHAIN]);
6536 #include "builtin-attrs.def"
6537 #undef DEF_ATTR_NULL_TREE
6538 #undef DEF_ATTR_INT
6539 #undef DEF_ATTR_IDENT
6540 #undef DEF_ATTR_TREE_LIST
6543 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6544 identifier as an argument, so the front end shouldn't look it up. */
6546 bool
6547 attribute_takes_identifier_p (const_tree attr_id)
6549 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6550 if (spec == NULL)
6551 /* Unknown attribute that we'll end up ignoring, return true so we
6552 don't complain about an identifier argument. */
6553 return true;
6554 else if (!strcmp ("mode", spec->name)
6555 || !strcmp ("format", spec->name)
6556 || !strcmp ("cleanup", spec->name))
6557 return true;
6558 else
6559 return targetm.attribute_takes_identifier_p (attr_id);
6562 /* Attribute handlers common to C front ends. */
6564 /* Handle a "packed" attribute; arguments as in
6565 struct attribute_spec.handler. */
6567 static tree
6568 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6569 int flags, bool *no_add_attrs)
6571 if (TYPE_P (*node))
6573 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6574 *node = build_variant_type_copy (*node);
6575 TYPE_PACKED (*node) = 1;
6577 else if (TREE_CODE (*node) == FIELD_DECL)
6579 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6580 /* Still pack bitfields. */
6581 && ! DECL_INITIAL (*node))
6582 warning (OPT_Wattributes,
6583 "%qE attribute ignored for field of type %qT",
6584 name, TREE_TYPE (*node));
6585 else
6586 DECL_PACKED (*node) = 1;
6588 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6589 used for DECL_REGISTER. It wouldn't mean anything anyway.
6590 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6591 that changes what the typedef is typing. */
6592 else
6594 warning (OPT_Wattributes, "%qE attribute ignored", name);
6595 *no_add_attrs = true;
6598 return NULL_TREE;
6601 /* Handle a "nocommon" attribute; arguments as in
6602 struct attribute_spec.handler. */
6604 static tree
6605 handle_nocommon_attribute (tree *node, tree name,
6606 tree ARG_UNUSED (args),
6607 int ARG_UNUSED (flags), bool *no_add_attrs)
6609 if (TREE_CODE (*node) == VAR_DECL)
6610 DECL_COMMON (*node) = 0;
6611 else
6613 warning (OPT_Wattributes, "%qE attribute ignored", name);
6614 *no_add_attrs = true;
6617 return NULL_TREE;
6620 /* Handle a "common" attribute; arguments as in
6621 struct attribute_spec.handler. */
6623 static tree
6624 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6625 int ARG_UNUSED (flags), bool *no_add_attrs)
6627 if (TREE_CODE (*node) == VAR_DECL)
6628 DECL_COMMON (*node) = 1;
6629 else
6631 warning (OPT_Wattributes, "%qE attribute ignored", name);
6632 *no_add_attrs = true;
6635 return NULL_TREE;
6638 /* Handle a "noreturn" attribute; arguments as in
6639 struct attribute_spec.handler. */
6641 static tree
6642 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6643 int ARG_UNUSED (flags), bool *no_add_attrs)
6645 tree type = TREE_TYPE (*node);
6647 /* See FIXME comment in c_common_attribute_table. */
6648 if (TREE_CODE (*node) == FUNCTION_DECL
6649 || objc_method_decl (TREE_CODE (*node)))
6650 TREE_THIS_VOLATILE (*node) = 1;
6651 else if (TREE_CODE (type) == POINTER_TYPE
6652 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6653 TREE_TYPE (*node)
6654 = build_pointer_type
6655 (build_type_variant (TREE_TYPE (type),
6656 TYPE_READONLY (TREE_TYPE (type)), 1));
6657 else
6659 warning (OPT_Wattributes, "%qE attribute ignored", name);
6660 *no_add_attrs = true;
6663 return NULL_TREE;
6666 /* Handle a "hot" and attribute; arguments as in
6667 struct attribute_spec.handler. */
6669 static tree
6670 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6671 int ARG_UNUSED (flags), bool *no_add_attrs)
6673 if (TREE_CODE (*node) == FUNCTION_DECL
6674 || TREE_CODE (*node) == LABEL_DECL)
6676 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6678 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6679 name, "cold");
6680 *no_add_attrs = true;
6682 /* Most of the rest of the hot processing is done later with
6683 lookup_attribute. */
6685 else
6687 warning (OPT_Wattributes, "%qE attribute ignored", name);
6688 *no_add_attrs = true;
6691 return NULL_TREE;
6694 /* Handle a "cold" and attribute; arguments as in
6695 struct attribute_spec.handler. */
6697 static tree
6698 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6699 int ARG_UNUSED (flags), bool *no_add_attrs)
6701 if (TREE_CODE (*node) == FUNCTION_DECL
6702 || TREE_CODE (*node) == LABEL_DECL)
6704 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6706 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6707 name, "hot");
6708 *no_add_attrs = true;
6710 /* Most of the rest of the cold processing is done later with
6711 lookup_attribute. */
6713 else
6715 warning (OPT_Wattributes, "%qE attribute ignored", name);
6716 *no_add_attrs = true;
6719 return NULL_TREE;
6722 /* Handle a "no_sanitize_address" attribute; arguments as in
6723 struct attribute_spec.handler. */
6725 static tree
6726 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6727 bool *no_add_attrs)
6729 if (TREE_CODE (*node) != FUNCTION_DECL)
6731 warning (OPT_Wattributes, "%qE attribute ignored", name);
6732 *no_add_attrs = true;
6735 return NULL_TREE;
6738 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6739 struct attribute_spec.handler. */
6741 static tree
6742 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6743 bool *no_add_attrs)
6745 if (TREE_CODE (*node) != FUNCTION_DECL)
6746 warning (OPT_Wattributes, "%qE attribute ignored", name);
6747 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6748 DECL_ATTRIBUTES (*node)
6749 = tree_cons (get_identifier ("no_sanitize_address"),
6750 NULL_TREE, DECL_ATTRIBUTES (*node));
6751 *no_add_attrs = true;
6752 return NULL_TREE;
6755 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6756 struct attribute_spec.handler. */
6758 static tree
6759 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6760 bool *no_add_attrs)
6762 if (TREE_CODE (*node) != FUNCTION_DECL)
6764 warning (OPT_Wattributes, "%qE attribute ignored", name);
6765 *no_add_attrs = true;
6768 return NULL_TREE;
6771 /* Handle a "noinline" attribute; arguments as in
6772 struct attribute_spec.handler. */
6774 static tree
6775 handle_noinline_attribute (tree *node, tree name,
6776 tree ARG_UNUSED (args),
6777 int ARG_UNUSED (flags), bool *no_add_attrs)
6779 if (TREE_CODE (*node) == FUNCTION_DECL)
6780 DECL_UNINLINABLE (*node) = 1;
6781 else
6783 warning (OPT_Wattributes, "%qE attribute ignored", name);
6784 *no_add_attrs = true;
6787 return NULL_TREE;
6790 /* Handle a "noclone" attribute; arguments as in
6791 struct attribute_spec.handler. */
6793 static tree
6794 handle_noclone_attribute (tree *node, tree name,
6795 tree ARG_UNUSED (args),
6796 int ARG_UNUSED (flags), bool *no_add_attrs)
6798 if (TREE_CODE (*node) != FUNCTION_DECL)
6800 warning (OPT_Wattributes, "%qE attribute ignored", name);
6801 *no_add_attrs = true;
6804 return NULL_TREE;
6807 /* Handle a "always_inline" attribute; arguments as in
6808 struct attribute_spec.handler. */
6810 static tree
6811 handle_always_inline_attribute (tree *node, tree name,
6812 tree ARG_UNUSED (args),
6813 int ARG_UNUSED (flags),
6814 bool *no_add_attrs)
6816 if (TREE_CODE (*node) == FUNCTION_DECL)
6818 /* Set the attribute and mark it for disregarding inline
6819 limits. */
6820 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6822 else
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6828 return NULL_TREE;
6831 /* Handle a "gnu_inline" attribute; arguments as in
6832 struct attribute_spec.handler. */
6834 static tree
6835 handle_gnu_inline_attribute (tree *node, tree name,
6836 tree ARG_UNUSED (args),
6837 int ARG_UNUSED (flags),
6838 bool *no_add_attrs)
6840 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6842 /* Do nothing else, just set the attribute. We'll get at
6843 it later with lookup_attribute. */
6845 else
6847 warning (OPT_Wattributes, "%qE attribute ignored", name);
6848 *no_add_attrs = true;
6851 return NULL_TREE;
6854 /* Handle a "leaf" attribute; arguments as in
6855 struct attribute_spec.handler. */
6857 static tree
6858 handle_leaf_attribute (tree *node, tree name,
6859 tree ARG_UNUSED (args),
6860 int ARG_UNUSED (flags), bool *no_add_attrs)
6862 if (TREE_CODE (*node) != FUNCTION_DECL)
6864 warning (OPT_Wattributes, "%qE attribute ignored", name);
6865 *no_add_attrs = true;
6867 if (!TREE_PUBLIC (*node))
6869 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6870 *no_add_attrs = true;
6873 return NULL_TREE;
6876 /* Handle an "artificial" attribute; arguments as in
6877 struct attribute_spec.handler. */
6879 static tree
6880 handle_artificial_attribute (tree *node, tree name,
6881 tree ARG_UNUSED (args),
6882 int ARG_UNUSED (flags),
6883 bool *no_add_attrs)
6885 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6887 /* Do nothing else, just set the attribute. We'll get at
6888 it later with lookup_attribute. */
6890 else
6892 warning (OPT_Wattributes, "%qE attribute ignored", name);
6893 *no_add_attrs = true;
6896 return NULL_TREE;
6899 /* Handle a "flatten" attribute; arguments as in
6900 struct attribute_spec.handler. */
6902 static tree
6903 handle_flatten_attribute (tree *node, tree name,
6904 tree args ATTRIBUTE_UNUSED,
6905 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6907 if (TREE_CODE (*node) == FUNCTION_DECL)
6908 /* Do nothing else, just set the attribute. We'll get at
6909 it later with lookup_attribute. */
6911 else
6913 warning (OPT_Wattributes, "%qE attribute ignored", name);
6914 *no_add_attrs = true;
6917 return NULL_TREE;
6920 /* Handle a "warning" or "error" attribute; arguments as in
6921 struct attribute_spec.handler. */
6923 static tree
6924 handle_error_attribute (tree *node, tree name, tree args,
6925 int ARG_UNUSED (flags), bool *no_add_attrs)
6927 if (TREE_CODE (*node) == FUNCTION_DECL
6928 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6929 /* Do nothing else, just set the attribute. We'll get at
6930 it later with lookup_attribute. */
6932 else
6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
6935 *no_add_attrs = true;
6938 return NULL_TREE;
6941 /* Handle a "used" attribute; arguments as in
6942 struct attribute_spec.handler. */
6944 static tree
6945 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6946 int ARG_UNUSED (flags), bool *no_add_attrs)
6948 tree node = *pnode;
6950 if (TREE_CODE (node) == FUNCTION_DECL
6951 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6952 || (TREE_CODE (node) == TYPE_DECL))
6954 TREE_USED (node) = 1;
6955 DECL_PRESERVE_P (node) = 1;
6956 if (TREE_CODE (node) == VAR_DECL)
6957 DECL_READ_P (node) = 1;
6959 else
6961 warning (OPT_Wattributes, "%qE attribute ignored", name);
6962 *no_add_attrs = true;
6965 return NULL_TREE;
6968 /* Handle a "unused" attribute; arguments as in
6969 struct attribute_spec.handler. */
6971 static tree
6972 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6973 int flags, bool *no_add_attrs)
6975 if (DECL_P (*node))
6977 tree decl = *node;
6979 if (TREE_CODE (decl) == PARM_DECL
6980 || TREE_CODE (decl) == VAR_DECL
6981 || TREE_CODE (decl) == FUNCTION_DECL
6982 || TREE_CODE (decl) == LABEL_DECL
6983 || TREE_CODE (decl) == TYPE_DECL)
6985 TREE_USED (decl) = 1;
6986 if (TREE_CODE (decl) == VAR_DECL
6987 || TREE_CODE (decl) == PARM_DECL)
6988 DECL_READ_P (decl) = 1;
6990 else
6992 warning (OPT_Wattributes, "%qE attribute ignored", name);
6993 *no_add_attrs = true;
6996 else
6998 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6999 *node = build_variant_type_copy (*node);
7000 TREE_USED (*node) = 1;
7003 return NULL_TREE;
7006 /* Handle a "externally_visible" attribute; arguments as in
7007 struct attribute_spec.handler. */
7009 static tree
7010 handle_externally_visible_attribute (tree *pnode, tree name,
7011 tree ARG_UNUSED (args),
7012 int ARG_UNUSED (flags),
7013 bool *no_add_attrs)
7015 tree node = *pnode;
7017 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
7019 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7020 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7022 warning (OPT_Wattributes,
7023 "%qE attribute have effect only on public objects", name);
7024 *no_add_attrs = true;
7027 else
7029 warning (OPT_Wattributes, "%qE attribute ignored", name);
7030 *no_add_attrs = true;
7033 return NULL_TREE;
7036 /* Handle a "const" attribute; arguments as in
7037 struct attribute_spec.handler. */
7039 static tree
7040 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7041 int ARG_UNUSED (flags), bool *no_add_attrs)
7043 tree type = TREE_TYPE (*node);
7045 /* See FIXME comment on noreturn in c_common_attribute_table. */
7046 if (TREE_CODE (*node) == FUNCTION_DECL)
7047 TREE_READONLY (*node) = 1;
7048 else if (TREE_CODE (type) == POINTER_TYPE
7049 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7050 TREE_TYPE (*node)
7051 = build_pointer_type
7052 (build_type_variant (TREE_TYPE (type), 1,
7053 TREE_THIS_VOLATILE (TREE_TYPE (type))));
7054 else
7056 warning (OPT_Wattributes, "%qE attribute ignored", name);
7057 *no_add_attrs = true;
7060 return NULL_TREE;
7063 /* Handle a "transparent_union" attribute; arguments as in
7064 struct attribute_spec.handler. */
7066 static tree
7067 handle_transparent_union_attribute (tree *node, tree name,
7068 tree ARG_UNUSED (args), int flags,
7069 bool *no_add_attrs)
7071 tree type;
7073 *no_add_attrs = true;
7076 if (TREE_CODE (*node) == TYPE_DECL
7077 && ! (flags & ATTR_FLAG_CXX11))
7078 node = &TREE_TYPE (*node);
7079 type = *node;
7081 if (TREE_CODE (type) == UNION_TYPE)
7083 /* Make sure that the first field will work for a transparent union.
7084 If the type isn't complete yet, leave the check to the code in
7085 finish_struct. */
7086 if (TYPE_SIZE (type))
7088 tree first = first_field (type);
7089 if (first == NULL_TREE
7090 || DECL_ARTIFICIAL (first)
7091 || TYPE_MODE (type) != DECL_MODE (first))
7092 goto ignored;
7095 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7097 /* If the type isn't complete yet, setting the flag
7098 on a variant wouldn't ever be checked. */
7099 if (!TYPE_SIZE (type))
7100 goto ignored;
7102 /* build_duplicate_type doesn't work for C++. */
7103 if (c_dialect_cxx ())
7104 goto ignored;
7106 /* A type variant isn't good enough, since we don't a cast
7107 to such a type removed as a no-op. */
7108 *node = type = build_duplicate_type (type);
7111 TYPE_TRANSPARENT_AGGR (type) = 1;
7112 return NULL_TREE;
7115 ignored:
7116 warning (OPT_Wattributes, "%qE attribute ignored", name);
7117 return NULL_TREE;
7120 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7121 get the requested priority for a constructor or destructor,
7122 possibly issuing diagnostics for invalid or reserved
7123 priorities. */
7125 static priority_type
7126 get_priority (tree args, bool is_destructor)
7128 HOST_WIDE_INT pri;
7129 tree arg;
7131 if (!args)
7132 return DEFAULT_INIT_PRIORITY;
7134 if (!SUPPORTS_INIT_PRIORITY)
7136 if (is_destructor)
7137 error ("destructor priorities are not supported");
7138 else
7139 error ("constructor priorities are not supported");
7140 return DEFAULT_INIT_PRIORITY;
7143 arg = TREE_VALUE (args);
7144 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7145 goto invalid;
7146 if (arg == error_mark_node)
7147 return DEFAULT_INIT_PRIORITY;
7148 arg = default_conversion (arg);
7149 if (!tree_fits_shwi_p (arg)
7150 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7151 goto invalid;
7153 pri = tree_to_shwi (arg);
7154 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7155 goto invalid;
7157 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7159 if (is_destructor)
7160 warning (0,
7161 "destructor priorities from 0 to %d are reserved "
7162 "for the implementation",
7163 MAX_RESERVED_INIT_PRIORITY);
7164 else
7165 warning (0,
7166 "constructor priorities from 0 to %d are reserved "
7167 "for the implementation",
7168 MAX_RESERVED_INIT_PRIORITY);
7170 return pri;
7172 invalid:
7173 if (is_destructor)
7174 error ("destructor priorities must be integers from 0 to %d inclusive",
7175 MAX_INIT_PRIORITY);
7176 else
7177 error ("constructor priorities must be integers from 0 to %d inclusive",
7178 MAX_INIT_PRIORITY);
7179 return DEFAULT_INIT_PRIORITY;
7182 /* Handle a "constructor" attribute; arguments as in
7183 struct attribute_spec.handler. */
7185 static tree
7186 handle_constructor_attribute (tree *node, tree name, tree args,
7187 int ARG_UNUSED (flags),
7188 bool *no_add_attrs)
7190 tree decl = *node;
7191 tree type = TREE_TYPE (decl);
7193 if (TREE_CODE (decl) == FUNCTION_DECL
7194 && TREE_CODE (type) == FUNCTION_TYPE
7195 && decl_function_context (decl) == 0)
7197 priority_type priority;
7198 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7199 priority = get_priority (args, /*is_destructor=*/false);
7200 SET_DECL_INIT_PRIORITY (decl, priority);
7201 TREE_USED (decl) = 1;
7203 else
7205 warning (OPT_Wattributes, "%qE attribute ignored", name);
7206 *no_add_attrs = true;
7209 return NULL_TREE;
7212 /* Handle a "destructor" attribute; arguments as in
7213 struct attribute_spec.handler. */
7215 static tree
7216 handle_destructor_attribute (tree *node, tree name, tree args,
7217 int ARG_UNUSED (flags),
7218 bool *no_add_attrs)
7220 tree decl = *node;
7221 tree type = TREE_TYPE (decl);
7223 if (TREE_CODE (decl) == FUNCTION_DECL
7224 && TREE_CODE (type) == FUNCTION_TYPE
7225 && decl_function_context (decl) == 0)
7227 priority_type priority;
7228 DECL_STATIC_DESTRUCTOR (decl) = 1;
7229 priority = get_priority (args, /*is_destructor=*/true);
7230 SET_DECL_FINI_PRIORITY (decl, priority);
7231 TREE_USED (decl) = 1;
7233 else
7235 warning (OPT_Wattributes, "%qE attribute ignored", name);
7236 *no_add_attrs = true;
7239 return NULL_TREE;
7242 /* Nonzero if the mode is a valid vector mode for this architecture.
7243 This returns nonzero even if there is no hardware support for the
7244 vector mode, but we can emulate with narrower modes. */
7246 static int
7247 vector_mode_valid_p (enum machine_mode mode)
7249 enum mode_class mclass = GET_MODE_CLASS (mode);
7250 enum machine_mode innermode;
7252 /* Doh! What's going on? */
7253 if (mclass != MODE_VECTOR_INT
7254 && mclass != MODE_VECTOR_FLOAT
7255 && mclass != MODE_VECTOR_FRACT
7256 && mclass != MODE_VECTOR_UFRACT
7257 && mclass != MODE_VECTOR_ACCUM
7258 && mclass != MODE_VECTOR_UACCUM)
7259 return 0;
7261 /* Hardware support. Woo hoo! */
7262 if (targetm.vector_mode_supported_p (mode))
7263 return 1;
7265 innermode = GET_MODE_INNER (mode);
7267 /* We should probably return 1 if requesting V4DI and we have no DI,
7268 but we have V2DI, but this is probably very unlikely. */
7270 /* If we have support for the inner mode, we can safely emulate it.
7271 We may not have V2DI, but me can emulate with a pair of DIs. */
7272 return targetm.scalar_mode_supported_p (innermode);
7276 /* Handle a "mode" attribute; arguments as in
7277 struct attribute_spec.handler. */
7279 static tree
7280 handle_mode_attribute (tree *node, tree name, tree args,
7281 int ARG_UNUSED (flags), bool *no_add_attrs)
7283 tree type = *node;
7284 tree ident = TREE_VALUE (args);
7286 *no_add_attrs = true;
7288 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7289 warning (OPT_Wattributes, "%qE attribute ignored", name);
7290 else
7292 int j;
7293 const char *p = IDENTIFIER_POINTER (ident);
7294 int len = strlen (p);
7295 enum machine_mode mode = VOIDmode;
7296 tree typefm;
7297 bool valid_mode;
7299 if (len > 4 && p[0] == '_' && p[1] == '_'
7300 && p[len - 1] == '_' && p[len - 2] == '_')
7302 char *newp = (char *) alloca (len - 1);
7304 strcpy (newp, &p[2]);
7305 newp[len - 4] = '\0';
7306 p = newp;
7309 /* Change this type to have a type with the specified mode.
7310 First check for the special modes. */
7311 if (!strcmp (p, "byte"))
7312 mode = byte_mode;
7313 else if (!strcmp (p, "word"))
7314 mode = word_mode;
7315 else if (!strcmp (p, "pointer"))
7316 mode = ptr_mode;
7317 else if (!strcmp (p, "libgcc_cmp_return"))
7318 mode = targetm.libgcc_cmp_return_mode ();
7319 else if (!strcmp (p, "libgcc_shift_count"))
7320 mode = targetm.libgcc_shift_count_mode ();
7321 else if (!strcmp (p, "unwind_word"))
7322 mode = targetm.unwind_word_mode ();
7323 else
7324 for (j = 0; j < NUM_MACHINE_MODES; j++)
7325 if (!strcmp (p, GET_MODE_NAME (j)))
7327 mode = (enum machine_mode) j;
7328 break;
7331 if (mode == VOIDmode)
7333 error ("unknown machine mode %qE", ident);
7334 return NULL_TREE;
7337 valid_mode = false;
7338 switch (GET_MODE_CLASS (mode))
7340 case MODE_INT:
7341 case MODE_PARTIAL_INT:
7342 case MODE_FLOAT:
7343 case MODE_DECIMAL_FLOAT:
7344 case MODE_FRACT:
7345 case MODE_UFRACT:
7346 case MODE_ACCUM:
7347 case MODE_UACCUM:
7348 valid_mode = targetm.scalar_mode_supported_p (mode);
7349 break;
7351 case MODE_COMPLEX_INT:
7352 case MODE_COMPLEX_FLOAT:
7353 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7354 break;
7356 case MODE_VECTOR_INT:
7357 case MODE_VECTOR_FLOAT:
7358 case MODE_VECTOR_FRACT:
7359 case MODE_VECTOR_UFRACT:
7360 case MODE_VECTOR_ACCUM:
7361 case MODE_VECTOR_UACCUM:
7362 warning (OPT_Wattributes, "specifying vector types with "
7363 "__attribute__ ((mode)) is deprecated");
7364 warning (OPT_Wattributes,
7365 "use __attribute__ ((vector_size)) instead");
7366 valid_mode = vector_mode_valid_p (mode);
7367 break;
7369 default:
7370 break;
7372 if (!valid_mode)
7374 error ("unable to emulate %qs", p);
7375 return NULL_TREE;
7378 if (POINTER_TYPE_P (type))
7380 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7381 tree (*fn)(tree, enum machine_mode, bool);
7383 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7385 error ("invalid pointer mode %qs", p);
7386 return NULL_TREE;
7389 if (TREE_CODE (type) == POINTER_TYPE)
7390 fn = build_pointer_type_for_mode;
7391 else
7392 fn = build_reference_type_for_mode;
7393 typefm = fn (TREE_TYPE (type), mode, false);
7395 else
7397 /* For fixed-point modes, we need to test if the signness of type
7398 and the machine mode are consistent. */
7399 if (ALL_FIXED_POINT_MODE_P (mode)
7400 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7402 error ("signedness of type and machine mode %qs don%'t match", p);
7403 return NULL_TREE;
7405 /* For fixed-point modes, we need to pass saturating info. */
7406 typefm = lang_hooks.types.type_for_mode (mode,
7407 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7408 : TYPE_UNSIGNED (type));
7411 if (typefm == NULL_TREE)
7413 error ("no data type for mode %qs", p);
7414 return NULL_TREE;
7416 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7418 /* For enumeral types, copy the precision from the integer
7419 type returned above. If not an INTEGER_TYPE, we can't use
7420 this mode for this type. */
7421 if (TREE_CODE (typefm) != INTEGER_TYPE)
7423 error ("cannot use mode %qs for enumeral types", p);
7424 return NULL_TREE;
7427 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7429 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7430 typefm = type;
7432 else
7434 /* We cannot build a type variant, as there's code that assumes
7435 that TYPE_MAIN_VARIANT has the same mode. This includes the
7436 debug generators. Instead, create a subrange type. This
7437 results in all of the enumeral values being emitted only once
7438 in the original, and the subtype gets them by reference. */
7439 if (TYPE_UNSIGNED (type))
7440 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7441 else
7442 typefm = make_signed_type (TYPE_PRECISION (typefm));
7443 TREE_TYPE (typefm) = type;
7446 else if (VECTOR_MODE_P (mode)
7447 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7448 : TREE_CODE (type) != TREE_CODE (typefm))
7450 error ("mode %qs applied to inappropriate type", p);
7451 return NULL_TREE;
7454 *node = typefm;
7457 return NULL_TREE;
7460 /* Handle a "section" attribute; arguments as in
7461 struct attribute_spec.handler. */
7463 static tree
7464 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7465 int ARG_UNUSED (flags), bool *no_add_attrs)
7467 tree decl = *node;
7469 if (targetm_common.have_named_sections)
7471 user_defined_section_attribute = true;
7473 if ((TREE_CODE (decl) == FUNCTION_DECL
7474 || TREE_CODE (decl) == VAR_DECL)
7475 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7477 if (TREE_CODE (decl) == VAR_DECL
7478 && current_function_decl != NULL_TREE
7479 && !TREE_STATIC (decl))
7481 error_at (DECL_SOURCE_LOCATION (decl),
7482 "section attribute cannot be specified for "
7483 "local variables");
7484 *no_add_attrs = true;
7487 /* The decl may have already been given a section attribute
7488 from a previous declaration. Ensure they match. */
7489 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7490 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7491 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7493 error ("section of %q+D conflicts with previous declaration",
7494 *node);
7495 *no_add_attrs = true;
7497 else if (TREE_CODE (decl) == VAR_DECL
7498 && !targetm.have_tls && targetm.emutls.tmpl_section
7499 && DECL_THREAD_LOCAL_P (decl))
7501 error ("section of %q+D cannot be overridden", *node);
7502 *no_add_attrs = true;
7504 else
7505 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7507 else
7509 error ("section attribute not allowed for %q+D", *node);
7510 *no_add_attrs = true;
7513 else
7515 error_at (DECL_SOURCE_LOCATION (*node),
7516 "section attributes are not supported for this target");
7517 *no_add_attrs = true;
7520 return NULL_TREE;
7523 /* Check whether ALIGN is a valid user-specified alignment. If so,
7524 return its base-2 log; if not, output an error and return -1. If
7525 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7526 no error. */
7528 check_user_alignment (const_tree align, bool allow_zero)
7530 int i;
7532 if (TREE_CODE (align) != INTEGER_CST
7533 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7535 error ("requested alignment is not an integer constant");
7536 return -1;
7538 else if (allow_zero && integer_zerop (align))
7539 return -1;
7540 else if (tree_int_cst_sgn (align) == -1
7541 || (i = tree_log2 (align)) == -1)
7543 error ("requested alignment is not a positive power of 2");
7544 return -1;
7546 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7548 error ("requested alignment is too large");
7549 return -1;
7551 return i;
7555 If in c++-11, check if the c++-11 alignment constraint with respect
7556 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7557 c++-11 mode, does nothing.
7559 [dcl.align]2/ says:
7561 [* if the constant expression evaluates to a fundamental alignment,
7562 the alignment requirement of the declared entity shall be the
7563 specified fundamental alignment.
7565 * if the constant expression evaluates to an extended alignment
7566 and the implementation supports that alignment in the context
7567 of the declaration, the alignment of the declared entity shall
7568 be that alignment
7570 * if the constant expression evaluates to an extended alignment
7571 and the implementation does not support that alignment in the
7572 context of the declaration, the program is ill-formed]. */
7574 static bool
7575 check_cxx_fundamental_alignment_constraints (tree node,
7576 unsigned align_log,
7577 int flags)
7579 bool alignment_too_large_p = false;
7580 unsigned requested_alignment = 1U << align_log;
7581 unsigned max_align = 0;
7583 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7584 || (node == NULL_TREE || node == error_mark_node))
7585 return true;
7587 if (cxx_fundamental_alignment_p (requested_alignment))
7588 return true;
7590 if (DECL_P (node))
7592 if (TREE_STATIC (node))
7594 /* For file scope variables and static members, the target
7595 supports alignments that are at most
7596 MAX_OFILE_ALIGNMENT. */
7597 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7598 alignment_too_large_p = true;
7600 else
7602 #ifdef BIGGEST_FIELD_ALIGNMENT
7603 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7604 #else
7605 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7606 #endif
7607 /* For non-static members, the target supports either
7608 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7609 if it is defined or BIGGEST_ALIGNMENT. */
7610 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7611 if (TREE_CODE (node) == FIELD_DECL
7612 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7613 alignment_too_large_p = true;
7614 #undef MAX_TARGET_FIELD_ALIGNMENT
7615 /* For stack variables, the target supports at most
7616 MAX_STACK_ALIGNMENT. */
7617 else if (decl_function_context (node) != NULL
7618 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7619 alignment_too_large_p = true;
7622 else if (TYPE_P (node))
7624 /* Let's be liberal for types. */
7625 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7626 alignment_too_large_p = true;
7629 if (alignment_too_large_p)
7630 pedwarn (input_location, OPT_Wattributes,
7631 "requested alignment %d is larger than %d",
7632 requested_alignment, max_align);
7634 return !alignment_too_large_p;
7637 /* Handle a "aligned" attribute; arguments as in
7638 struct attribute_spec.handler. */
7640 static tree
7641 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7642 int flags, bool *no_add_attrs)
7644 tree decl = NULL_TREE;
7645 tree *type = NULL;
7646 int is_type = 0;
7647 tree align_expr;
7648 int i;
7650 if (args)
7652 align_expr = TREE_VALUE (args);
7653 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE)
7654 align_expr = default_conversion (align_expr);
7656 else
7657 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7659 if (DECL_P (*node))
7661 decl = *node;
7662 type = &TREE_TYPE (decl);
7663 is_type = TREE_CODE (*node) == TYPE_DECL;
7665 else if (TYPE_P (*node))
7666 type = node, is_type = 1;
7668 if ((i = check_user_alignment (align_expr, false)) == -1
7669 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7670 *no_add_attrs = true;
7671 else if (is_type)
7673 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7674 /* OK, modify the type in place. */;
7675 /* If we have a TYPE_DECL, then copy the type, so that we
7676 don't accidentally modify a builtin type. See pushdecl. */
7677 else if (decl && TREE_TYPE (decl) != error_mark_node
7678 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7680 tree tt = TREE_TYPE (decl);
7681 *type = build_variant_type_copy (*type);
7682 DECL_ORIGINAL_TYPE (decl) = tt;
7683 TYPE_NAME (*type) = decl;
7684 TREE_USED (*type) = TREE_USED (decl);
7685 TREE_TYPE (decl) = *type;
7687 else
7688 *type = build_variant_type_copy (*type);
7690 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7691 TYPE_USER_ALIGN (*type) = 1;
7693 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7694 && TREE_CODE (decl) != FIELD_DECL)
7696 error ("alignment may not be specified for %q+D", decl);
7697 *no_add_attrs = true;
7699 else if (DECL_USER_ALIGN (decl)
7700 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7701 /* C++-11 [dcl.align/4]:
7703 When multiple alignment-specifiers are specified for an
7704 entity, the alignment requirement shall be set to the
7705 strictest specified alignment.
7707 This formally comes from the c++11 specification but we are
7708 doing it for the GNU attribute syntax as well. */
7709 *no_add_attrs = true;
7710 else if (TREE_CODE (decl) == FUNCTION_DECL
7711 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7713 if (DECL_USER_ALIGN (decl))
7714 error ("alignment for %q+D was previously specified as %d "
7715 "and may not be decreased", decl,
7716 DECL_ALIGN (decl) / BITS_PER_UNIT);
7717 else
7718 error ("alignment for %q+D must be at least %d", decl,
7719 DECL_ALIGN (decl) / BITS_PER_UNIT);
7720 *no_add_attrs = true;
7722 else
7724 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7725 DECL_USER_ALIGN (decl) = 1;
7728 return NULL_TREE;
7731 /* Handle a "weak" attribute; arguments as in
7732 struct attribute_spec.handler. */
7734 static tree
7735 handle_weak_attribute (tree *node, tree name,
7736 tree ARG_UNUSED (args),
7737 int ARG_UNUSED (flags),
7738 bool * ARG_UNUSED (no_add_attrs))
7740 if (TREE_CODE (*node) == FUNCTION_DECL
7741 && DECL_DECLARED_INLINE_P (*node))
7743 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7744 *no_add_attrs = true;
7746 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7748 error ("indirect function %q+D cannot be declared weak", *node);
7749 *no_add_attrs = true;
7750 return NULL_TREE;
7752 else if (TREE_CODE (*node) == FUNCTION_DECL
7753 || TREE_CODE (*node) == VAR_DECL)
7754 declare_weak (*node);
7755 else
7756 warning (OPT_Wattributes, "%qE attribute ignored", name);
7758 return NULL_TREE;
7761 /* Handle an "alias" or "ifunc" attribute; arguments as in
7762 struct attribute_spec.handler, except that IS_ALIAS tells us
7763 whether this is an alias as opposed to ifunc attribute. */
7765 static tree
7766 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7767 bool *no_add_attrs)
7769 tree decl = *node;
7771 if (TREE_CODE (decl) != FUNCTION_DECL
7772 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7774 warning (OPT_Wattributes, "%qE attribute ignored", name);
7775 *no_add_attrs = true;
7777 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7778 || (TREE_CODE (decl) != FUNCTION_DECL
7779 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7780 /* A static variable declaration is always a tentative definition,
7781 but the alias is a non-tentative definition which overrides. */
7782 || (TREE_CODE (decl) != FUNCTION_DECL
7783 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7785 error ("%q+D defined both normally and as %qE attribute", decl, name);
7786 *no_add_attrs = true;
7787 return NULL_TREE;
7789 else if (!is_alias
7790 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7791 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7793 error ("weak %q+D cannot be defined %qE", decl, name);
7794 *no_add_attrs = true;
7795 return NULL_TREE;
7798 /* Note that the very first time we process a nested declaration,
7799 decl_function_context will not be set. Indeed, *would* never
7800 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7801 we do below. After such frobbery, pushdecl would set the context.
7802 In any case, this is never what we want. */
7803 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7805 tree id;
7807 id = TREE_VALUE (args);
7808 if (TREE_CODE (id) != STRING_CST)
7810 error ("attribute %qE argument not a string", name);
7811 *no_add_attrs = true;
7812 return NULL_TREE;
7814 id = get_identifier (TREE_STRING_POINTER (id));
7815 /* This counts as a use of the object pointed to. */
7816 TREE_USED (id) = 1;
7818 if (TREE_CODE (decl) == FUNCTION_DECL)
7819 DECL_INITIAL (decl) = error_mark_node;
7820 else
7821 TREE_STATIC (decl) = 1;
7823 if (!is_alias)
7824 /* ifuncs are also aliases, so set that attribute too. */
7825 DECL_ATTRIBUTES (decl)
7826 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7828 else
7830 warning (OPT_Wattributes, "%qE attribute ignored", name);
7831 *no_add_attrs = true;
7834 return NULL_TREE;
7837 /* Handle an "alias" or "ifunc" attribute; arguments as in
7838 struct attribute_spec.handler. */
7840 static tree
7841 handle_ifunc_attribute (tree *node, tree name, tree args,
7842 int ARG_UNUSED (flags), bool *no_add_attrs)
7844 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7847 /* Handle an "alias" or "ifunc" attribute; arguments as in
7848 struct attribute_spec.handler. */
7850 static tree
7851 handle_alias_attribute (tree *node, tree name, tree args,
7852 int ARG_UNUSED (flags), bool *no_add_attrs)
7854 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7857 /* Handle a "weakref" attribute; arguments as in struct
7858 attribute_spec.handler. */
7860 static tree
7861 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7862 int flags, bool *no_add_attrs)
7864 tree attr = NULL_TREE;
7866 /* We must ignore the attribute when it is associated with
7867 local-scoped decls, since attribute alias is ignored and many
7868 such symbols do not even have a DECL_WEAK field. */
7869 if (decl_function_context (*node)
7870 || current_function_decl
7871 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7873 warning (OPT_Wattributes, "%qE attribute ignored", name);
7874 *no_add_attrs = true;
7875 return NULL_TREE;
7878 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7880 error ("indirect function %q+D cannot be declared weakref", *node);
7881 *no_add_attrs = true;
7882 return NULL_TREE;
7885 /* The idea here is that `weakref("name")' mutates into `weakref,
7886 alias("name")', and weakref without arguments, in turn,
7887 implicitly adds weak. */
7889 if (args)
7891 attr = tree_cons (get_identifier ("alias"), args, attr);
7892 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7894 *no_add_attrs = true;
7896 decl_attributes (node, attr, flags);
7898 else
7900 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7901 error_at (DECL_SOURCE_LOCATION (*node),
7902 "weakref attribute must appear before alias attribute");
7904 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7905 and that isn't supported; and because it wants to add it to
7906 the list of weak decls, which isn't helpful. */
7907 DECL_WEAK (*node) = 1;
7910 return NULL_TREE;
7913 /* Handle an "visibility" attribute; arguments as in
7914 struct attribute_spec.handler. */
7916 static tree
7917 handle_visibility_attribute (tree *node, tree name, tree args,
7918 int ARG_UNUSED (flags),
7919 bool *ARG_UNUSED (no_add_attrs))
7921 tree decl = *node;
7922 tree id = TREE_VALUE (args);
7923 enum symbol_visibility vis;
7925 if (TYPE_P (*node))
7927 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7928 /* OK */;
7929 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7931 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7932 name);
7933 return NULL_TREE;
7935 else if (TYPE_FIELDS (*node))
7937 error ("%qE attribute ignored because %qT is already defined",
7938 name, *node);
7939 return NULL_TREE;
7942 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7944 warning (OPT_Wattributes, "%qE attribute ignored", name);
7945 return NULL_TREE;
7948 if (TREE_CODE (id) != STRING_CST)
7950 error ("visibility argument not a string");
7951 return NULL_TREE;
7954 /* If this is a type, set the visibility on the type decl. */
7955 if (TYPE_P (decl))
7957 decl = TYPE_NAME (decl);
7958 if (!decl)
7959 return NULL_TREE;
7960 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7962 warning (OPT_Wattributes, "%qE attribute ignored on types",
7963 name);
7964 return NULL_TREE;
7968 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7969 vis = VISIBILITY_DEFAULT;
7970 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7971 vis = VISIBILITY_INTERNAL;
7972 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7973 vis = VISIBILITY_HIDDEN;
7974 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7975 vis = VISIBILITY_PROTECTED;
7976 else
7978 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7979 vis = VISIBILITY_DEFAULT;
7982 if (DECL_VISIBILITY_SPECIFIED (decl)
7983 && vis != DECL_VISIBILITY (decl))
7985 tree attributes = (TYPE_P (*node)
7986 ? TYPE_ATTRIBUTES (*node)
7987 : DECL_ATTRIBUTES (decl));
7988 if (lookup_attribute ("visibility", attributes))
7989 error ("%qD redeclared with different visibility", decl);
7990 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7991 && lookup_attribute ("dllimport", attributes))
7992 error ("%qD was declared %qs which implies default visibility",
7993 decl, "dllimport");
7994 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7995 && lookup_attribute ("dllexport", attributes))
7996 error ("%qD was declared %qs which implies default visibility",
7997 decl, "dllexport");
8000 DECL_VISIBILITY (decl) = vis;
8001 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8003 /* Go ahead and attach the attribute to the node as well. This is needed
8004 so we can determine whether we have VISIBILITY_DEFAULT because the
8005 visibility was not specified, or because it was explicitly overridden
8006 from the containing scope. */
8008 return NULL_TREE;
8011 /* Determine the ELF symbol visibility for DECL, which is either a
8012 variable or a function. It is an error to use this function if a
8013 definition of DECL is not available in this translation unit.
8014 Returns true if the final visibility has been determined by this
8015 function; false if the caller is free to make additional
8016 modifications. */
8018 bool
8019 c_determine_visibility (tree decl)
8021 gcc_assert (TREE_CODE (decl) == VAR_DECL
8022 || TREE_CODE (decl) == FUNCTION_DECL);
8024 /* If the user explicitly specified the visibility with an
8025 attribute, honor that. DECL_VISIBILITY will have been set during
8026 the processing of the attribute. We check for an explicit
8027 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8028 to distinguish the use of an attribute from the use of a "#pragma
8029 GCC visibility push(...)"; in the latter case we still want other
8030 considerations to be able to overrule the #pragma. */
8031 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8032 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8033 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8034 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8035 return true;
8037 /* Set default visibility to whatever the user supplied with
8038 visibility_specified depending on #pragma GCC visibility. */
8039 if (!DECL_VISIBILITY_SPECIFIED (decl))
8041 if (visibility_options.inpragma
8042 || DECL_VISIBILITY (decl) != default_visibility)
8044 DECL_VISIBILITY (decl) = default_visibility;
8045 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8046 /* If visibility changed and DECL already has DECL_RTL, ensure
8047 symbol flags are updated. */
8048 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8049 || TREE_CODE (decl) == FUNCTION_DECL)
8050 && DECL_RTL_SET_P (decl))
8051 make_decl_rtl (decl);
8054 return false;
8057 /* Handle an "tls_model" attribute; arguments as in
8058 struct attribute_spec.handler. */
8060 static tree
8061 handle_tls_model_attribute (tree *node, tree name, tree args,
8062 int ARG_UNUSED (flags), bool *no_add_attrs)
8064 tree id;
8065 tree decl = *node;
8066 enum tls_model kind;
8068 *no_add_attrs = true;
8070 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8072 warning (OPT_Wattributes, "%qE attribute ignored", name);
8073 return NULL_TREE;
8076 kind = DECL_TLS_MODEL (decl);
8077 id = TREE_VALUE (args);
8078 if (TREE_CODE (id) != STRING_CST)
8080 error ("tls_model argument not a string");
8081 return NULL_TREE;
8084 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8085 kind = TLS_MODEL_LOCAL_EXEC;
8086 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8087 kind = TLS_MODEL_INITIAL_EXEC;
8088 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8089 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8090 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8091 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8092 else
8093 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8095 DECL_TLS_MODEL (decl) = kind;
8096 return NULL_TREE;
8099 /* Handle a "no_instrument_function" attribute; arguments as in
8100 struct attribute_spec.handler. */
8102 static tree
8103 handle_no_instrument_function_attribute (tree *node, tree name,
8104 tree ARG_UNUSED (args),
8105 int ARG_UNUSED (flags),
8106 bool *no_add_attrs)
8108 tree decl = *node;
8110 if (TREE_CODE (decl) != FUNCTION_DECL)
8112 error_at (DECL_SOURCE_LOCATION (decl),
8113 "%qE attribute applies only to functions", name);
8114 *no_add_attrs = true;
8116 else
8117 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8119 return NULL_TREE;
8122 /* Handle a "malloc" attribute; arguments as in
8123 struct attribute_spec.handler. */
8125 static tree
8126 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8127 int ARG_UNUSED (flags), bool *no_add_attrs)
8129 if (TREE_CODE (*node) == FUNCTION_DECL
8130 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8131 DECL_IS_MALLOC (*node) = 1;
8132 else
8134 warning (OPT_Wattributes, "%qE attribute ignored", name);
8135 *no_add_attrs = true;
8138 return NULL_TREE;
8141 /* Handle a "alloc_size" attribute; arguments as in
8142 struct attribute_spec.handler. */
8144 static tree
8145 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8146 int ARG_UNUSED (flags), bool *no_add_attrs)
8148 unsigned arg_count = type_num_arguments (*node);
8149 for (; args; args = TREE_CHAIN (args))
8151 tree position = TREE_VALUE (args);
8152 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8153 && TREE_CODE (position) != FUNCTION_DECL)
8154 position = default_conversion (position);
8156 if (!tree_fits_uhwi_p (position)
8157 || !arg_count
8158 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8160 warning (OPT_Wattributes,
8161 "alloc_size parameter outside range");
8162 *no_add_attrs = true;
8163 return NULL_TREE;
8166 return NULL_TREE;
8169 /* Handle a "alloc_align" attribute; arguments as in
8170 struct attribute_spec.handler. */
8172 static tree
8173 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8174 bool *no_add_attrs)
8176 unsigned arg_count = type_num_arguments (*node);
8177 tree position = TREE_VALUE (args);
8178 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8179 position = default_conversion (position);
8181 if (!tree_fits_uhwi_p (position)
8182 || !arg_count
8183 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8185 warning (OPT_Wattributes,
8186 "alloc_align parameter outside range");
8187 *no_add_attrs = true;
8188 return NULL_TREE;
8190 return NULL_TREE;
8193 /* Handle a "assume_aligned" attribute; arguments as in
8194 struct attribute_spec.handler. */
8196 static tree
8197 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8198 bool *no_add_attrs)
8200 for (; args; args = TREE_CHAIN (args))
8202 tree position = TREE_VALUE (args);
8203 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8204 && TREE_CODE (position) != FUNCTION_DECL)
8205 position = default_conversion (position);
8207 if (TREE_CODE (position) != INTEGER_CST)
8209 warning (OPT_Wattributes,
8210 "assume_aligned parameter not integer constant");
8211 *no_add_attrs = true;
8212 return NULL_TREE;
8215 return NULL_TREE;
8218 /* Handle a "fn spec" attribute; arguments as in
8219 struct attribute_spec.handler. */
8221 static tree
8222 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8223 tree args, int ARG_UNUSED (flags),
8224 bool *no_add_attrs ATTRIBUTE_UNUSED)
8226 gcc_assert (args
8227 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8228 && !TREE_CHAIN (args));
8229 return NULL_TREE;
8232 /* Handle a "warn_unused" attribute; arguments as in
8233 struct attribute_spec.handler. */
8235 static tree
8236 handle_warn_unused_attribute (tree *node, tree name,
8237 tree args ATTRIBUTE_UNUSED,
8238 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8240 if (TYPE_P (*node))
8241 /* Do nothing else, just set the attribute. We'll get at
8242 it later with lookup_attribute. */
8244 else
8246 warning (OPT_Wattributes, "%qE attribute ignored", name);
8247 *no_add_attrs = true;
8250 return NULL_TREE;
8253 /* Handle an "omp declare simd" attribute; arguments as in
8254 struct attribute_spec.handler. */
8256 static tree
8257 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8259 return NULL_TREE;
8262 /* Handle an "omp declare target" attribute; arguments as in
8263 struct attribute_spec.handler. */
8265 static tree
8266 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8268 return NULL_TREE;
8271 /* Handle a "returns_twice" attribute; arguments as in
8272 struct attribute_spec.handler. */
8274 static tree
8275 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8276 int ARG_UNUSED (flags), bool *no_add_attrs)
8278 if (TREE_CODE (*node) == FUNCTION_DECL)
8279 DECL_IS_RETURNS_TWICE (*node) = 1;
8280 else
8282 warning (OPT_Wattributes, "%qE attribute ignored", name);
8283 *no_add_attrs = true;
8286 return NULL_TREE;
8289 /* Handle a "no_limit_stack" attribute; arguments as in
8290 struct attribute_spec.handler. */
8292 static tree
8293 handle_no_limit_stack_attribute (tree *node, tree name,
8294 tree ARG_UNUSED (args),
8295 int ARG_UNUSED (flags),
8296 bool *no_add_attrs)
8298 tree decl = *node;
8300 if (TREE_CODE (decl) != FUNCTION_DECL)
8302 error_at (DECL_SOURCE_LOCATION (decl),
8303 "%qE attribute applies only to functions", name);
8304 *no_add_attrs = true;
8306 else if (DECL_INITIAL (decl))
8308 error_at (DECL_SOURCE_LOCATION (decl),
8309 "can%'t set %qE attribute after definition", name);
8310 *no_add_attrs = true;
8312 else
8313 DECL_NO_LIMIT_STACK (decl) = 1;
8315 return NULL_TREE;
8318 /* Handle a "pure" attribute; arguments as in
8319 struct attribute_spec.handler. */
8321 static tree
8322 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8323 int ARG_UNUSED (flags), bool *no_add_attrs)
8325 if (TREE_CODE (*node) == FUNCTION_DECL)
8326 DECL_PURE_P (*node) = 1;
8327 /* ??? TODO: Support types. */
8328 else
8330 warning (OPT_Wattributes, "%qE attribute ignored", name);
8331 *no_add_attrs = true;
8334 return NULL_TREE;
8337 /* Digest an attribute list destined for a transactional memory statement.
8338 ALLOWED is the set of attributes that are allowed for this statement;
8339 return the attribute we parsed. Multiple attributes are never allowed. */
8342 parse_tm_stmt_attr (tree attrs, int allowed)
8344 tree a_seen = NULL;
8345 int m_seen = 0;
8347 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8349 tree a = TREE_PURPOSE (attrs);
8350 int m = 0;
8352 if (is_attribute_p ("outer", a))
8353 m = TM_STMT_ATTR_OUTER;
8355 if ((m & allowed) == 0)
8357 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8358 continue;
8361 if (m_seen == 0)
8363 a_seen = a;
8364 m_seen = m;
8366 else if (m_seen == m)
8367 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8368 else
8369 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8372 return m_seen;
8375 /* Transform a TM attribute name into a maskable integer and back.
8376 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8377 to how the lack of an attribute is treated. */
8380 tm_attr_to_mask (tree attr)
8382 if (attr == NULL)
8383 return 0;
8384 if (is_attribute_p ("transaction_safe", attr))
8385 return TM_ATTR_SAFE;
8386 if (is_attribute_p ("transaction_callable", attr))
8387 return TM_ATTR_CALLABLE;
8388 if (is_attribute_p ("transaction_pure", attr))
8389 return TM_ATTR_PURE;
8390 if (is_attribute_p ("transaction_unsafe", attr))
8391 return TM_ATTR_IRREVOCABLE;
8392 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8393 return TM_ATTR_MAY_CANCEL_OUTER;
8394 return 0;
8397 tree
8398 tm_mask_to_attr (int mask)
8400 const char *str;
8401 switch (mask)
8403 case TM_ATTR_SAFE:
8404 str = "transaction_safe";
8405 break;
8406 case TM_ATTR_CALLABLE:
8407 str = "transaction_callable";
8408 break;
8409 case TM_ATTR_PURE:
8410 str = "transaction_pure";
8411 break;
8412 case TM_ATTR_IRREVOCABLE:
8413 str = "transaction_unsafe";
8414 break;
8415 case TM_ATTR_MAY_CANCEL_OUTER:
8416 str = "transaction_may_cancel_outer";
8417 break;
8418 default:
8419 gcc_unreachable ();
8421 return get_identifier (str);
8424 /* Return the first TM attribute seen in LIST. */
8426 tree
8427 find_tm_attribute (tree list)
8429 for (; list ; list = TREE_CHAIN (list))
8431 tree name = TREE_PURPOSE (list);
8432 if (tm_attr_to_mask (name) != 0)
8433 return name;
8435 return NULL_TREE;
8438 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8439 Here we accept only function types, and verify that none of the other
8440 function TM attributes are also applied. */
8441 /* ??? We need to accept class types for C++, but not C. This greatly
8442 complicates this function, since we can no longer rely on the extra
8443 processing given by function_type_required. */
8445 static tree
8446 handle_tm_attribute (tree *node, tree name, tree args,
8447 int flags, bool *no_add_attrs)
8449 /* Only one path adds the attribute; others don't. */
8450 *no_add_attrs = true;
8452 switch (TREE_CODE (*node))
8454 case RECORD_TYPE:
8455 case UNION_TYPE:
8456 /* Only tm_callable and tm_safe apply to classes. */
8457 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8458 goto ignored;
8459 /* FALLTHRU */
8461 case FUNCTION_TYPE:
8462 case METHOD_TYPE:
8464 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8465 if (old_name == name)
8467 else if (old_name != NULL_TREE)
8468 error ("type was previously declared %qE", old_name);
8469 else
8470 *no_add_attrs = false;
8472 break;
8474 case POINTER_TYPE:
8476 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8477 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8479 tree fn_tmp = TREE_TYPE (*node);
8480 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8481 *node = build_pointer_type (fn_tmp);
8482 break;
8485 /* FALLTHRU */
8487 default:
8488 /* If a function is next, pass it on to be tried next. */
8489 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8490 return tree_cons (name, args, NULL);
8492 ignored:
8493 warning (OPT_Wattributes, "%qE attribute ignored", name);
8494 break;
8497 return NULL_TREE;
8500 /* Handle the TM_WRAP attribute; arguments as in
8501 struct attribute_spec.handler. */
8503 static tree
8504 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8505 int ARG_UNUSED (flags), bool *no_add_attrs)
8507 tree decl = *node;
8509 /* We don't need the attribute even on success, since we
8510 record the entry in an external table. */
8511 *no_add_attrs = true;
8513 if (TREE_CODE (decl) != FUNCTION_DECL)
8514 warning (OPT_Wattributes, "%qE attribute ignored", name);
8515 else
8517 tree wrap_decl = TREE_VALUE (args);
8518 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8519 && TREE_CODE (wrap_decl) != VAR_DECL
8520 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8521 error ("%qE argument not an identifier", name);
8522 else
8524 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8525 wrap_decl = lookup_name (wrap_decl);
8526 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8528 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8529 TREE_TYPE (wrap_decl)))
8530 record_tm_replacement (wrap_decl, decl);
8531 else
8532 error ("%qD is not compatible with %qD", wrap_decl, decl);
8534 else
8535 error ("transaction_wrap argument is not a function");
8539 return NULL_TREE;
8542 /* Ignore the given attribute. Used when this attribute may be usefully
8543 overridden by the target, but is not used generically. */
8545 static tree
8546 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8547 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8548 bool *no_add_attrs)
8550 *no_add_attrs = true;
8551 return NULL_TREE;
8554 /* Handle a "no vops" attribute; arguments as in
8555 struct attribute_spec.handler. */
8557 static tree
8558 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8559 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8560 bool *ARG_UNUSED (no_add_attrs))
8562 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8563 DECL_IS_NOVOPS (*node) = 1;
8564 return NULL_TREE;
8567 /* Handle a "deprecated" attribute; arguments as in
8568 struct attribute_spec.handler. */
8570 static tree
8571 handle_deprecated_attribute (tree *node, tree name,
8572 tree args, int flags,
8573 bool *no_add_attrs)
8575 tree type = NULL_TREE;
8576 int warn = 0;
8577 tree what = NULL_TREE;
8579 if (!args)
8580 *no_add_attrs = true;
8581 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8583 error ("deprecated message is not a string");
8584 *no_add_attrs = true;
8587 if (DECL_P (*node))
8589 tree decl = *node;
8590 type = TREE_TYPE (decl);
8592 if (TREE_CODE (decl) == TYPE_DECL
8593 || TREE_CODE (decl) == PARM_DECL
8594 || TREE_CODE (decl) == VAR_DECL
8595 || TREE_CODE (decl) == FUNCTION_DECL
8596 || TREE_CODE (decl) == FIELD_DECL
8597 || objc_method_decl (TREE_CODE (decl)))
8598 TREE_DEPRECATED (decl) = 1;
8599 else
8600 warn = 1;
8602 else if (TYPE_P (*node))
8604 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8605 *node = build_variant_type_copy (*node);
8606 TREE_DEPRECATED (*node) = 1;
8607 type = *node;
8609 else
8610 warn = 1;
8612 if (warn)
8614 *no_add_attrs = true;
8615 if (type && TYPE_NAME (type))
8617 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8618 what = TYPE_NAME (*node);
8619 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8620 && DECL_NAME (TYPE_NAME (type)))
8621 what = DECL_NAME (TYPE_NAME (type));
8623 if (what)
8624 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8625 else
8626 warning (OPT_Wattributes, "%qE attribute ignored", name);
8629 return NULL_TREE;
8632 /* Handle a "vector_size" attribute; arguments as in
8633 struct attribute_spec.handler. */
8635 static tree
8636 handle_vector_size_attribute (tree *node, tree name, tree args,
8637 int ARG_UNUSED (flags),
8638 bool *no_add_attrs)
8640 unsigned HOST_WIDE_INT vecsize, nunits;
8641 enum machine_mode orig_mode;
8642 tree type = *node, new_type, size;
8644 *no_add_attrs = true;
8646 size = TREE_VALUE (args);
8647 if (size && TREE_CODE (size) != IDENTIFIER_NODE)
8648 size = default_conversion (size);
8650 if (!tree_fits_uhwi_p (size))
8652 warning (OPT_Wattributes, "%qE attribute ignored", name);
8653 return NULL_TREE;
8656 /* Get the vector size (in bytes). */
8657 vecsize = tree_to_uhwi (size);
8659 /* We need to provide for vector pointers, vector arrays, and
8660 functions returning vectors. For example:
8662 __attribute__((vector_size(16))) short *foo;
8664 In this case, the mode is SI, but the type being modified is
8665 HI, so we need to look further. */
8667 while (POINTER_TYPE_P (type)
8668 || TREE_CODE (type) == FUNCTION_TYPE
8669 || TREE_CODE (type) == METHOD_TYPE
8670 || TREE_CODE (type) == ARRAY_TYPE
8671 || TREE_CODE (type) == OFFSET_TYPE)
8672 type = TREE_TYPE (type);
8674 /* Get the mode of the type being modified. */
8675 orig_mode = TYPE_MODE (type);
8677 if ((!INTEGRAL_TYPE_P (type)
8678 && !SCALAR_FLOAT_TYPE_P (type)
8679 && !FIXED_POINT_TYPE_P (type))
8680 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8681 && GET_MODE_CLASS (orig_mode) != MODE_INT
8682 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8683 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8684 || TREE_CODE (type) == BOOLEAN_TYPE)
8686 error ("invalid vector type for attribute %qE", name);
8687 return NULL_TREE;
8690 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8692 error ("vector size not an integral multiple of component size");
8693 return NULL;
8696 if (vecsize == 0)
8698 error ("zero vector size");
8699 return NULL;
8702 /* Calculate how many units fit in the vector. */
8703 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8704 if (nunits & (nunits - 1))
8706 error ("number of components of the vector not a power of two");
8707 return NULL_TREE;
8710 new_type = build_vector_type (type, nunits);
8712 /* Build back pointers if needed. */
8713 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8715 return NULL_TREE;
8718 /* Handle the "nonnull" attribute. */
8719 static tree
8720 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8721 tree args, int ARG_UNUSED (flags),
8722 bool *no_add_attrs)
8724 tree type = *node;
8725 unsigned HOST_WIDE_INT attr_arg_num;
8727 /* If no arguments are specified, all pointer arguments should be
8728 non-null. Verify a full prototype is given so that the arguments
8729 will have the correct types when we actually check them later. */
8730 if (!args)
8732 if (!prototype_p (type))
8734 error ("nonnull attribute without arguments on a non-prototype");
8735 *no_add_attrs = true;
8737 return NULL_TREE;
8740 /* Argument list specified. Verify that each argument number references
8741 a pointer argument. */
8742 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8744 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8746 tree arg = TREE_VALUE (args);
8747 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8748 && TREE_CODE (arg) != FUNCTION_DECL)
8749 arg = default_conversion (arg);
8751 if (!get_nonnull_operand (arg, &arg_num))
8753 error ("nonnull argument has invalid operand number (argument %lu)",
8754 (unsigned long) attr_arg_num);
8755 *no_add_attrs = true;
8756 return NULL_TREE;
8759 if (prototype_p (type))
8761 function_args_iterator iter;
8762 tree argument;
8764 function_args_iter_init (&iter, type);
8765 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8767 argument = function_args_iter_cond (&iter);
8768 if (argument == NULL_TREE || ck_num == arg_num)
8769 break;
8772 if (!argument
8773 || TREE_CODE (argument) == VOID_TYPE)
8775 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8776 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8777 *no_add_attrs = true;
8778 return NULL_TREE;
8781 if (TREE_CODE (argument) != POINTER_TYPE)
8783 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8784 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8785 *no_add_attrs = true;
8786 return NULL_TREE;
8791 return NULL_TREE;
8794 /* Check the argument list of a function call for null in argument slots
8795 that are marked as requiring a non-null pointer argument. The NARGS
8796 arguments are passed in the array ARGARRAY.
8799 static void
8800 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8802 tree a;
8803 int i;
8805 attrs = lookup_attribute ("nonnull", attrs);
8806 if (attrs == NULL_TREE)
8807 return;
8809 a = attrs;
8810 /* See if any of the nonnull attributes has no arguments. If so,
8811 then every pointer argument is checked (in which case the check
8812 for pointer type is done in check_nonnull_arg). */
8813 if (TREE_VALUE (a) != NULL_TREE)
8815 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8816 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8818 if (a != NULL_TREE)
8819 for (i = 0; i < nargs; i++)
8820 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8821 i + 1);
8822 else
8824 /* Walk the argument list. If we encounter an argument number we
8825 should check for non-null, do it. */
8826 for (i = 0; i < nargs; i++)
8828 for (a = attrs; ; a = TREE_CHAIN (a))
8830 a = lookup_attribute ("nonnull", a);
8831 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8832 break;
8835 if (a != NULL_TREE)
8836 check_function_arguments_recurse (check_nonnull_arg, NULL,
8837 argarray[i], i + 1);
8842 /* Check that the Nth argument of a function call (counting backwards
8843 from the end) is a (pointer)0. The NARGS arguments are passed in the
8844 array ARGARRAY. */
8846 static void
8847 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8849 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8851 if (attr)
8853 int len = 0;
8854 int pos = 0;
8855 tree sentinel;
8856 function_args_iterator iter;
8857 tree t;
8859 /* Skip over the named arguments. */
8860 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8862 if (len == nargs)
8863 break;
8864 len++;
8867 if (TREE_VALUE (attr))
8869 tree p = TREE_VALUE (TREE_VALUE (attr));
8870 pos = TREE_INT_CST_LOW (p);
8873 /* The sentinel must be one of the varargs, i.e.
8874 in position >= the number of fixed arguments. */
8875 if ((nargs - 1 - pos) < len)
8877 warning (OPT_Wformat_,
8878 "not enough variable arguments to fit a sentinel");
8879 return;
8882 /* Validate the sentinel. */
8883 sentinel = argarray[nargs - 1 - pos];
8884 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8885 || !integer_zerop (sentinel))
8886 /* Although __null (in C++) is only an integer we allow it
8887 nevertheless, as we are guaranteed that it's exactly
8888 as wide as a pointer, and we don't want to force
8889 users to cast the NULL they have written there.
8890 We warn with -Wstrict-null-sentinel, though. */
8891 && (warn_strict_null_sentinel || null_node != sentinel))
8892 warning (OPT_Wformat_, "missing sentinel in function call");
8896 /* Helper for check_function_nonnull; given a list of operands which
8897 must be non-null in ARGS, determine if operand PARAM_NUM should be
8898 checked. */
8900 static bool
8901 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8903 unsigned HOST_WIDE_INT arg_num = 0;
8905 for (; args; args = TREE_CHAIN (args))
8907 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8909 gcc_assert (found);
8911 if (arg_num == param_num)
8912 return true;
8914 return false;
8917 /* Check that the function argument PARAM (which is operand number
8918 PARAM_NUM) is non-null. This is called by check_function_nonnull
8919 via check_function_arguments_recurse. */
8921 static void
8922 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8923 unsigned HOST_WIDE_INT param_num)
8925 /* Just skip checking the argument if it's not a pointer. This can
8926 happen if the "nonnull" attribute was given without an operand
8927 list (which means to check every pointer argument). */
8929 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8930 return;
8932 if (integer_zerop (param))
8933 warning (OPT_Wnonnull, "null argument where non-null required "
8934 "(argument %lu)", (unsigned long) param_num);
8937 /* Helper for nonnull attribute handling; fetch the operand number
8938 from the attribute argument list. */
8940 static bool
8941 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8943 /* Verify the arg number is a constant. */
8944 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8945 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8946 return false;
8948 *valp = TREE_INT_CST_LOW (arg_num_expr);
8949 return true;
8952 /* Handle a "nothrow" attribute; arguments as in
8953 struct attribute_spec.handler. */
8955 static tree
8956 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8957 int ARG_UNUSED (flags), bool *no_add_attrs)
8959 if (TREE_CODE (*node) == FUNCTION_DECL)
8960 TREE_NOTHROW (*node) = 1;
8961 /* ??? TODO: Support types. */
8962 else
8964 warning (OPT_Wattributes, "%qE attribute ignored", name);
8965 *no_add_attrs = true;
8968 return NULL_TREE;
8971 /* Handle a "cleanup" attribute; arguments as in
8972 struct attribute_spec.handler. */
8974 static tree
8975 handle_cleanup_attribute (tree *node, tree name, tree args,
8976 int ARG_UNUSED (flags), bool *no_add_attrs)
8978 tree decl = *node;
8979 tree cleanup_id, cleanup_decl;
8981 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8982 for global destructors in C++. This requires infrastructure that
8983 we don't have generically at the moment. It's also not a feature
8984 we'd be missing too much, since we do have attribute constructor. */
8985 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8987 warning (OPT_Wattributes, "%qE attribute ignored", name);
8988 *no_add_attrs = true;
8989 return NULL_TREE;
8992 /* Verify that the argument is a function in scope. */
8993 /* ??? We could support pointers to functions here as well, if
8994 that was considered desirable. */
8995 cleanup_id = TREE_VALUE (args);
8996 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8998 error ("cleanup argument not an identifier");
8999 *no_add_attrs = true;
9000 return NULL_TREE;
9002 cleanup_decl = lookup_name (cleanup_id);
9003 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9005 error ("cleanup argument not a function");
9006 *no_add_attrs = true;
9007 return NULL_TREE;
9010 /* That the function has proper type is checked with the
9011 eventual call to build_function_call. */
9013 return NULL_TREE;
9016 /* Handle a "warn_unused_result" attribute. No special handling. */
9018 static tree
9019 handle_warn_unused_result_attribute (tree *node, tree name,
9020 tree ARG_UNUSED (args),
9021 int ARG_UNUSED (flags), bool *no_add_attrs)
9023 /* Ignore the attribute for functions not returning any value. */
9024 if (VOID_TYPE_P (TREE_TYPE (*node)))
9026 warning (OPT_Wattributes, "%qE attribute ignored", name);
9027 *no_add_attrs = true;
9030 return NULL_TREE;
9033 /* Handle a "sentinel" attribute. */
9035 static tree
9036 handle_sentinel_attribute (tree *node, tree name, tree args,
9037 int ARG_UNUSED (flags), bool *no_add_attrs)
9039 if (!prototype_p (*node))
9041 warning (OPT_Wattributes,
9042 "%qE attribute requires prototypes with named arguments", name);
9043 *no_add_attrs = true;
9045 else
9047 if (!stdarg_p (*node))
9049 warning (OPT_Wattributes,
9050 "%qE attribute only applies to variadic functions", name);
9051 *no_add_attrs = true;
9055 if (args)
9057 tree position = TREE_VALUE (args);
9059 if (TREE_CODE (position) != INTEGER_CST)
9061 warning (OPT_Wattributes,
9062 "requested position is not an integer constant");
9063 *no_add_attrs = true;
9065 else
9067 if (tree_int_cst_lt (position, integer_zero_node))
9069 warning (OPT_Wattributes,
9070 "requested position is less than zero");
9071 *no_add_attrs = true;
9076 return NULL_TREE;
9079 /* Handle a "type_generic" attribute. */
9081 static tree
9082 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9083 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9084 bool * ARG_UNUSED (no_add_attrs))
9086 /* Ensure we have a function type. */
9087 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9089 /* Ensure we have a variadic function. */
9090 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9092 return NULL_TREE;
9095 /* Handle a "target" attribute. */
9097 static tree
9098 handle_target_attribute (tree *node, tree name, tree args, int flags,
9099 bool *no_add_attrs)
9101 /* Ensure we have a function type. */
9102 if (TREE_CODE (*node) != FUNCTION_DECL)
9104 warning (OPT_Wattributes, "%qE attribute ignored", name);
9105 *no_add_attrs = true;
9107 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9108 flags))
9109 *no_add_attrs = true;
9111 return NULL_TREE;
9114 /* Arguments being collected for optimization. */
9115 typedef const char *const_char_p; /* For DEF_VEC_P. */
9116 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9119 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9120 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9121 false for #pragma GCC optimize. */
9123 bool
9124 parse_optimize_options (tree args, bool attr_p)
9126 bool ret = true;
9127 unsigned opt_argc;
9128 unsigned i;
9129 int saved_flag_strict_aliasing;
9130 const char **opt_argv;
9131 struct cl_decoded_option *decoded_options;
9132 unsigned int decoded_options_count;
9133 tree ap;
9135 /* Build up argv vector. Just in case the string is stored away, use garbage
9136 collected strings. */
9137 vec_safe_truncate (optimize_args, 0);
9138 vec_safe_push (optimize_args, (const char *) NULL);
9140 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9142 tree value = TREE_VALUE (ap);
9144 if (TREE_CODE (value) == INTEGER_CST)
9146 char buffer[20];
9147 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9148 vec_safe_push (optimize_args, ggc_strdup (buffer));
9151 else if (TREE_CODE (value) == STRING_CST)
9153 /* Split string into multiple substrings. */
9154 size_t len = TREE_STRING_LENGTH (value);
9155 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9156 char *end = p + len;
9157 char *comma;
9158 char *next_p = p;
9160 while (next_p != NULL)
9162 size_t len2;
9163 char *q, *r;
9165 p = next_p;
9166 comma = strchr (p, ',');
9167 if (comma)
9169 len2 = comma - p;
9170 *comma = '\0';
9171 next_p = comma+1;
9173 else
9175 len2 = end - p;
9176 next_p = NULL;
9179 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9181 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9182 options. */
9183 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9185 ret = false;
9186 if (attr_p)
9187 warning (OPT_Wattributes,
9188 "bad option %s to optimize attribute", p);
9189 else
9190 warning (OPT_Wpragmas,
9191 "bad option %s to pragma attribute", p);
9192 continue;
9195 if (*p != '-')
9197 *r++ = '-';
9199 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9200 itself is -Os, and any other switch begins with a -f. */
9201 if ((*p >= '0' && *p <= '9')
9202 || (p[0] == 's' && p[1] == '\0'))
9203 *r++ = 'O';
9204 else if (*p != 'O')
9205 *r++ = 'f';
9208 memcpy (r, p, len2);
9209 r[len2] = '\0';
9210 vec_safe_push (optimize_args, (const char *) q);
9216 opt_argc = optimize_args->length ();
9217 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9219 for (i = 1; i < opt_argc; i++)
9220 opt_argv[i] = (*optimize_args)[i];
9222 saved_flag_strict_aliasing = flag_strict_aliasing;
9224 /* Now parse the options. */
9225 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9226 &decoded_options,
9227 &decoded_options_count);
9228 decode_options (&global_options, &global_options_set,
9229 decoded_options, decoded_options_count,
9230 input_location, global_dc);
9232 targetm.override_options_after_change();
9234 /* Don't allow changing -fstrict-aliasing. */
9235 flag_strict_aliasing = saved_flag_strict_aliasing;
9237 optimize_args->truncate (0);
9238 return ret;
9241 /* For handling "optimize" attribute. arguments as in
9242 struct attribute_spec.handler. */
9244 static tree
9245 handle_optimize_attribute (tree *node, tree name, tree args,
9246 int ARG_UNUSED (flags), bool *no_add_attrs)
9248 /* Ensure we have a function type. */
9249 if (TREE_CODE (*node) != FUNCTION_DECL)
9251 warning (OPT_Wattributes, "%qE attribute ignored", name);
9252 *no_add_attrs = true;
9254 else
9256 struct cl_optimization cur_opts;
9257 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9259 /* Save current options. */
9260 cl_optimization_save (&cur_opts, &global_options);
9262 /* If we previously had some optimization options, use them as the
9263 default. */
9264 if (old_opts)
9265 cl_optimization_restore (&global_options,
9266 TREE_OPTIMIZATION (old_opts));
9268 /* Parse options, and update the vector. */
9269 parse_optimize_options (args, true);
9270 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9271 = build_optimization_node (&global_options);
9273 /* Restore current options. */
9274 cl_optimization_restore (&global_options, &cur_opts);
9277 return NULL_TREE;
9280 /* Handle a "no_split_stack" attribute. */
9282 static tree
9283 handle_no_split_stack_attribute (tree *node, tree name,
9284 tree ARG_UNUSED (args),
9285 int ARG_UNUSED (flags),
9286 bool *no_add_attrs)
9288 tree decl = *node;
9290 if (TREE_CODE (decl) != FUNCTION_DECL)
9292 error_at (DECL_SOURCE_LOCATION (decl),
9293 "%qE attribute applies only to functions", name);
9294 *no_add_attrs = true;
9296 else if (DECL_INITIAL (decl))
9298 error_at (DECL_SOURCE_LOCATION (decl),
9299 "can%'t set %qE attribute after definition", name);
9300 *no_add_attrs = true;
9303 return NULL_TREE;
9306 /* Handle a "returns_nonnull" attribute; arguments as in
9307 struct attribute_spec.handler. */
9309 static tree
9310 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9311 bool *no_add_attrs)
9313 // Even without a prototype we still have a return type we can check.
9314 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9316 error ("returns_nonnull attribute on a function not returning a pointer");
9317 *no_add_attrs = true;
9319 return NULL_TREE;
9323 /* Check for valid arguments being passed to a function with FNTYPE.
9324 There are NARGS arguments in the array ARGARRAY. */
9325 void
9326 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9328 /* Check for null being passed in a pointer argument that must be
9329 non-null. We also need to do this if format checking is enabled. */
9331 if (warn_nonnull)
9332 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9334 /* Check for errors in format strings. */
9336 if (warn_format || warn_suggest_attribute_format)
9337 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9339 if (warn_format)
9340 check_function_sentinel (fntype, nargs, argarray);
9343 /* Generic argument checking recursion routine. PARAM is the argument to
9344 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9345 once the argument is resolved. CTX is context for the callback. */
9346 void
9347 check_function_arguments_recurse (void (*callback)
9348 (void *, tree, unsigned HOST_WIDE_INT),
9349 void *ctx, tree param,
9350 unsigned HOST_WIDE_INT param_num)
9352 if (CONVERT_EXPR_P (param)
9353 && (TYPE_PRECISION (TREE_TYPE (param))
9354 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9356 /* Strip coercion. */
9357 check_function_arguments_recurse (callback, ctx,
9358 TREE_OPERAND (param, 0), param_num);
9359 return;
9362 if (TREE_CODE (param) == CALL_EXPR)
9364 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9365 tree attrs;
9366 bool found_format_arg = false;
9368 /* See if this is a call to a known internationalization function
9369 that modifies a format arg. Such a function may have multiple
9370 format_arg attributes (for example, ngettext). */
9372 for (attrs = TYPE_ATTRIBUTES (type);
9373 attrs;
9374 attrs = TREE_CHAIN (attrs))
9375 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9377 tree inner_arg;
9378 tree format_num_expr;
9379 int format_num;
9380 int i;
9381 call_expr_arg_iterator iter;
9383 /* Extract the argument number, which was previously checked
9384 to be valid. */
9385 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9387 format_num = tree_to_uhwi (format_num_expr);
9389 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9390 inner_arg != 0;
9391 inner_arg = next_call_expr_arg (&iter), i++)
9392 if (i == format_num)
9394 check_function_arguments_recurse (callback, ctx,
9395 inner_arg, param_num);
9396 found_format_arg = true;
9397 break;
9401 /* If we found a format_arg attribute and did a recursive check,
9402 we are done with checking this argument. Otherwise, we continue
9403 and this will be considered a non-literal. */
9404 if (found_format_arg)
9405 return;
9408 if (TREE_CODE (param) == COND_EXPR)
9410 /* Check both halves of the conditional expression. */
9411 check_function_arguments_recurse (callback, ctx,
9412 TREE_OPERAND (param, 1), param_num);
9413 check_function_arguments_recurse (callback, ctx,
9414 TREE_OPERAND (param, 2), param_num);
9415 return;
9418 (*callback) (ctx, param, param_num);
9421 /* Checks for a builtin function FNDECL that the number of arguments
9422 NARGS against the required number REQUIRED and issues an error if
9423 there is a mismatch. Returns true if the number of arguments is
9424 correct, otherwise false. */
9426 static bool
9427 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9429 if (nargs < required)
9431 error_at (input_location,
9432 "not enough arguments to function %qE", fndecl);
9433 return false;
9435 else if (nargs > required)
9437 error_at (input_location,
9438 "too many arguments to function %qE", fndecl);
9439 return false;
9441 return true;
9444 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9445 Returns false if there was an error, otherwise true. */
9447 bool
9448 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9450 if (!DECL_BUILT_IN (fndecl)
9451 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9452 return true;
9454 switch (DECL_FUNCTION_CODE (fndecl))
9456 case BUILT_IN_CONSTANT_P:
9457 return builtin_function_validate_nargs (fndecl, nargs, 1);
9459 case BUILT_IN_ISFINITE:
9460 case BUILT_IN_ISINF:
9461 case BUILT_IN_ISINF_SIGN:
9462 case BUILT_IN_ISNAN:
9463 case BUILT_IN_ISNORMAL:
9464 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9466 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9468 error ("non-floating-point argument in call to "
9469 "function %qE", fndecl);
9470 return false;
9472 return true;
9474 return false;
9476 case BUILT_IN_ISGREATER:
9477 case BUILT_IN_ISGREATEREQUAL:
9478 case BUILT_IN_ISLESS:
9479 case BUILT_IN_ISLESSEQUAL:
9480 case BUILT_IN_ISLESSGREATER:
9481 case BUILT_IN_ISUNORDERED:
9482 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9484 enum tree_code code0, code1;
9485 code0 = TREE_CODE (TREE_TYPE (args[0]));
9486 code1 = TREE_CODE (TREE_TYPE (args[1]));
9487 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9488 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9489 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9491 error ("non-floating-point arguments in call to "
9492 "function %qE", fndecl);
9493 return false;
9495 return true;
9497 return false;
9499 case BUILT_IN_FPCLASSIFY:
9500 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9502 unsigned i;
9504 for (i=0; i<5; i++)
9505 if (TREE_CODE (args[i]) != INTEGER_CST)
9507 error ("non-const integer argument %u in call to function %qE",
9508 i+1, fndecl);
9509 return false;
9512 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9514 error ("non-floating-point argument in call to function %qE",
9515 fndecl);
9516 return false;
9518 return true;
9520 return false;
9522 case BUILT_IN_ASSUME_ALIGNED:
9523 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9525 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9527 error ("non-integer argument 3 in call to function %qE", fndecl);
9528 return false;
9530 return true;
9532 return false;
9534 default:
9535 return true;
9539 /* Function to help qsort sort FIELD_DECLs by name order. */
9542 field_decl_cmp (const void *x_p, const void *y_p)
9544 const tree *const x = (const tree *const) x_p;
9545 const tree *const y = (const tree *const) y_p;
9547 if (DECL_NAME (*x) == DECL_NAME (*y))
9548 /* A nontype is "greater" than a type. */
9549 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9550 if (DECL_NAME (*x) == NULL_TREE)
9551 return -1;
9552 if (DECL_NAME (*y) == NULL_TREE)
9553 return 1;
9554 if (DECL_NAME (*x) < DECL_NAME (*y))
9555 return -1;
9556 return 1;
9559 static struct {
9560 gt_pointer_operator new_value;
9561 void *cookie;
9562 } resort_data;
9564 /* This routine compares two fields like field_decl_cmp but using the
9565 pointer operator in resort_data. */
9567 static int
9568 resort_field_decl_cmp (const void *x_p, const void *y_p)
9570 const tree *const x = (const tree *const) x_p;
9571 const tree *const y = (const tree *const) y_p;
9573 if (DECL_NAME (*x) == DECL_NAME (*y))
9574 /* A nontype is "greater" than a type. */
9575 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9576 if (DECL_NAME (*x) == NULL_TREE)
9577 return -1;
9578 if (DECL_NAME (*y) == NULL_TREE)
9579 return 1;
9581 tree d1 = DECL_NAME (*x);
9582 tree d2 = DECL_NAME (*y);
9583 resort_data.new_value (&d1, resort_data.cookie);
9584 resort_data.new_value (&d2, resort_data.cookie);
9585 if (d1 < d2)
9586 return -1;
9588 return 1;
9591 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9593 void
9594 resort_sorted_fields (void *obj,
9595 void * ARG_UNUSED (orig_obj),
9596 gt_pointer_operator new_value,
9597 void *cookie)
9599 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9600 resort_data.new_value = new_value;
9601 resort_data.cookie = cookie;
9602 qsort (&sf->elts[0], sf->len, sizeof (tree),
9603 resort_field_decl_cmp);
9606 /* Subroutine of c_parse_error.
9607 Return the result of concatenating LHS and RHS. RHS is really
9608 a string literal, its first character is indicated by RHS_START and
9609 RHS_SIZE is its length (including the terminating NUL character).
9611 The caller is responsible for deleting the returned pointer. */
9613 static char *
9614 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9616 const int lhs_size = strlen (lhs);
9617 char *result = XNEWVEC (char, lhs_size + rhs_size);
9618 strncpy (result, lhs, lhs_size);
9619 strncpy (result + lhs_size, rhs_start, rhs_size);
9620 return result;
9623 /* Issue the error given by GMSGID, indicating that it occurred before
9624 TOKEN, which had the associated VALUE. */
9626 void
9627 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9628 tree value, unsigned char token_flags)
9630 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9632 char *message = NULL;
9634 if (token_type == CPP_EOF)
9635 message = catenate_messages (gmsgid, " at end of input");
9636 else if (token_type == CPP_CHAR
9637 || token_type == CPP_WCHAR
9638 || token_type == CPP_CHAR16
9639 || token_type == CPP_CHAR32)
9641 unsigned int val = TREE_INT_CST_LOW (value);
9642 const char *prefix;
9644 switch (token_type)
9646 default:
9647 prefix = "";
9648 break;
9649 case CPP_WCHAR:
9650 prefix = "L";
9651 break;
9652 case CPP_CHAR16:
9653 prefix = "u";
9654 break;
9655 case CPP_CHAR32:
9656 prefix = "U";
9657 break;
9660 if (val <= UCHAR_MAX && ISGRAPH (val))
9661 message = catenate_messages (gmsgid, " before %s'%c'");
9662 else
9663 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9665 error (message, prefix, val);
9666 free (message);
9667 message = NULL;
9669 else if (token_type == CPP_CHAR_USERDEF
9670 || token_type == CPP_WCHAR_USERDEF
9671 || token_type == CPP_CHAR16_USERDEF
9672 || token_type == CPP_CHAR32_USERDEF)
9673 message = catenate_messages (gmsgid,
9674 " before user-defined character literal");
9675 else if (token_type == CPP_STRING_USERDEF
9676 || token_type == CPP_WSTRING_USERDEF
9677 || token_type == CPP_STRING16_USERDEF
9678 || token_type == CPP_STRING32_USERDEF
9679 || token_type == CPP_UTF8STRING_USERDEF)
9680 message = catenate_messages (gmsgid, " before user-defined string literal");
9681 else if (token_type == CPP_STRING
9682 || token_type == CPP_WSTRING
9683 || token_type == CPP_STRING16
9684 || token_type == CPP_STRING32
9685 || token_type == CPP_UTF8STRING)
9686 message = catenate_messages (gmsgid, " before string constant");
9687 else if (token_type == CPP_NUMBER)
9688 message = catenate_messages (gmsgid, " before numeric constant");
9689 else if (token_type == CPP_NAME)
9691 message = catenate_messages (gmsgid, " before %qE");
9692 error (message, value);
9693 free (message);
9694 message = NULL;
9696 else if (token_type == CPP_PRAGMA)
9697 message = catenate_messages (gmsgid, " before %<#pragma%>");
9698 else if (token_type == CPP_PRAGMA_EOL)
9699 message = catenate_messages (gmsgid, " before end of line");
9700 else if (token_type == CPP_DECLTYPE)
9701 message = catenate_messages (gmsgid, " before %<decltype%>");
9702 else if (token_type < N_TTYPES)
9704 message = catenate_messages (gmsgid, " before %qs token");
9705 error (message, cpp_type2name (token_type, token_flags));
9706 free (message);
9707 message = NULL;
9709 else
9710 error (gmsgid);
9712 if (message)
9714 error (message);
9715 free (message);
9717 #undef catenate_messages
9720 /* Mapping for cpp message reasons to the options that enable them. */
9722 struct reason_option_codes_t
9724 const int reason; /* cpplib message reason. */
9725 const int option_code; /* gcc option that controls this message. */
9728 static const struct reason_option_codes_t option_codes[] = {
9729 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9730 {CPP_W_COMMENTS, OPT_Wcomment},
9731 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9732 {CPP_W_MULTICHAR, OPT_Wmultichar},
9733 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9734 {CPP_W_LONG_LONG, OPT_Wlong_long},
9735 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9736 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9737 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9738 {CPP_W_UNDEF, OPT_Wundef},
9739 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9740 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9741 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9742 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9743 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9744 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9745 {CPP_W_DATE_TIME, OPT_Wdate_time},
9746 {CPP_W_NONE, 0}
9749 /* Return the gcc option code associated with the reason for a cpp
9750 message, or 0 if none. */
9752 static int
9753 c_option_controlling_cpp_error (int reason)
9755 const struct reason_option_codes_t *entry;
9757 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9759 if (entry->reason == reason)
9760 return entry->option_code;
9762 return 0;
9765 /* Callback from cpp_error for PFILE to print diagnostics from the
9766 preprocessor. The diagnostic is of type LEVEL, with REASON set
9767 to the reason code if LEVEL is represents a warning, at location
9768 LOCATION unless this is after lexing and the compiler's location
9769 should be used instead, with column number possibly overridden by
9770 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9771 the arguments. Returns true if a diagnostic was emitted, false
9772 otherwise. */
9774 bool
9775 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9776 location_t location, unsigned int column_override,
9777 const char *msg, va_list *ap)
9779 diagnostic_info diagnostic;
9780 diagnostic_t dlevel;
9781 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9782 bool ret;
9784 switch (level)
9786 case CPP_DL_WARNING_SYSHDR:
9787 if (flag_no_output)
9788 return false;
9789 global_dc->dc_warn_system_headers = 1;
9790 /* Fall through. */
9791 case CPP_DL_WARNING:
9792 if (flag_no_output)
9793 return false;
9794 dlevel = DK_WARNING;
9795 break;
9796 case CPP_DL_PEDWARN:
9797 if (flag_no_output && !flag_pedantic_errors)
9798 return false;
9799 dlevel = DK_PEDWARN;
9800 break;
9801 case CPP_DL_ERROR:
9802 dlevel = DK_ERROR;
9803 break;
9804 case CPP_DL_ICE:
9805 dlevel = DK_ICE;
9806 break;
9807 case CPP_DL_NOTE:
9808 dlevel = DK_NOTE;
9809 break;
9810 case CPP_DL_FATAL:
9811 dlevel = DK_FATAL;
9812 break;
9813 default:
9814 gcc_unreachable ();
9816 if (done_lexing)
9817 location = input_location;
9818 diagnostic_set_info_translated (&diagnostic, msg, ap,
9819 location, dlevel);
9820 if (column_override)
9821 diagnostic_override_column (&diagnostic, column_override);
9822 diagnostic_override_option_index (&diagnostic,
9823 c_option_controlling_cpp_error (reason));
9824 ret = report_diagnostic (&diagnostic);
9825 if (level == CPP_DL_WARNING_SYSHDR)
9826 global_dc->dc_warn_system_headers = save_warn_system_headers;
9827 return ret;
9830 /* Convert a character from the host to the target execution character
9831 set. cpplib handles this, mostly. */
9833 HOST_WIDE_INT
9834 c_common_to_target_charset (HOST_WIDE_INT c)
9836 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9837 zero-extended under -fno-signed-char. cpplib insists that characters
9838 and character constants are always unsigned. Hence we must convert
9839 back and forth. */
9840 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9842 uc = cpp_host_to_exec_charset (parse_in, uc);
9844 if (flag_signed_char)
9845 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9846 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9847 else
9848 return uc;
9851 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9852 references with an INDIRECT_REF of a constant at the bottom; much like the
9853 traditional rendering of offsetof as a macro. Return the folded result. */
9855 tree
9856 fold_offsetof_1 (tree expr)
9858 tree base, off, t;
9860 switch (TREE_CODE (expr))
9862 case ERROR_MARK:
9863 return expr;
9865 case VAR_DECL:
9866 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9867 return error_mark_node;
9869 case CALL_EXPR:
9870 case TARGET_EXPR:
9871 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9872 return error_mark_node;
9874 case NOP_EXPR:
9875 case INDIRECT_REF:
9876 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9878 error ("cannot apply %<offsetof%> to a non constant address");
9879 return error_mark_node;
9881 return TREE_OPERAND (expr, 0);
9883 case COMPONENT_REF:
9884 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9885 if (base == error_mark_node)
9886 return base;
9888 t = TREE_OPERAND (expr, 1);
9889 if (DECL_C_BIT_FIELD (t))
9891 error ("attempt to take address of bit-field structure "
9892 "member %qD", t);
9893 return error_mark_node;
9895 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9896 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9897 / BITS_PER_UNIT));
9898 break;
9900 case ARRAY_REF:
9901 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9902 if (base == error_mark_node)
9903 return base;
9905 t = TREE_OPERAND (expr, 1);
9907 /* Check if the offset goes beyond the upper bound of the array. */
9908 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9910 tree upbound = array_ref_up_bound (expr);
9911 if (upbound != NULL_TREE
9912 && TREE_CODE (upbound) == INTEGER_CST
9913 && !tree_int_cst_equal (upbound,
9914 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9916 upbound = size_binop (PLUS_EXPR, upbound,
9917 build_int_cst (TREE_TYPE (upbound), 1));
9918 if (tree_int_cst_lt (upbound, t))
9920 tree v;
9922 for (v = TREE_OPERAND (expr, 0);
9923 TREE_CODE (v) == COMPONENT_REF;
9924 v = TREE_OPERAND (v, 0))
9925 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9926 == RECORD_TYPE)
9928 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9929 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9930 if (TREE_CODE (fld_chain) == FIELD_DECL)
9931 break;
9933 if (fld_chain)
9934 break;
9936 /* Don't warn if the array might be considered a poor
9937 man's flexible array member with a very permissive
9938 definition thereof. */
9939 if (TREE_CODE (v) == ARRAY_REF
9940 || TREE_CODE (v) == COMPONENT_REF)
9941 warning (OPT_Warray_bounds,
9942 "index %E denotes an offset "
9943 "greater than size of %qT",
9944 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9949 t = convert (sizetype, t);
9950 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9951 break;
9953 case COMPOUND_EXPR:
9954 /* Handle static members of volatile structs. */
9955 t = TREE_OPERAND (expr, 1);
9956 gcc_assert (TREE_CODE (t) == VAR_DECL);
9957 return fold_offsetof_1 (t);
9959 default:
9960 gcc_unreachable ();
9963 return fold_build_pointer_plus (base, off);
9966 /* Likewise, but convert it to the return type of offsetof. */
9968 tree
9969 fold_offsetof (tree expr)
9971 return convert (size_type_node, fold_offsetof_1 (expr));
9974 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9975 expression, because B will always be true. */
9977 void
9978 warn_for_omitted_condop (location_t location, tree cond)
9980 if (truth_value_p (TREE_CODE (cond)))
9981 warning_at (location, OPT_Wparentheses,
9982 "the omitted middle operand in ?: will always be %<true%>, "
9983 "suggest explicit middle operand");
9986 /* Give an error for storing into ARG, which is 'const'. USE indicates
9987 how ARG was being used. */
9989 void
9990 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9992 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9993 || use == lv_asm);
9994 /* Using this macro rather than (for example) arrays of messages
9995 ensures that all the format strings are checked at compile
9996 time. */
9997 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9998 : (use == lv_increment ? (I) \
9999 : (use == lv_decrement ? (D) : (AS))))
10000 if (TREE_CODE (arg) == COMPONENT_REF)
10002 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10003 error_at (loc, READONLY_MSG (G_("assignment of member "
10004 "%qD in read-only object"),
10005 G_("increment of member "
10006 "%qD in read-only object"),
10007 G_("decrement of member "
10008 "%qD in read-only object"),
10009 G_("member %qD in read-only object "
10010 "used as %<asm%> output")),
10011 TREE_OPERAND (arg, 1));
10012 else
10013 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10014 G_("increment of read-only member %qD"),
10015 G_("decrement of read-only member %qD"),
10016 G_("read-only member %qD used as %<asm%> output")),
10017 TREE_OPERAND (arg, 1));
10019 else if (TREE_CODE (arg) == VAR_DECL)
10020 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10021 G_("increment of read-only variable %qD"),
10022 G_("decrement of read-only variable %qD"),
10023 G_("read-only variable %qD used as %<asm%> output")),
10024 arg);
10025 else if (TREE_CODE (arg) == PARM_DECL)
10026 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10027 G_("increment of read-only parameter %qD"),
10028 G_("decrement of read-only parameter %qD"),
10029 G_("read-only parameter %qD use as %<asm%> output")),
10030 arg);
10031 else if (TREE_CODE (arg) == RESULT_DECL)
10033 gcc_assert (c_dialect_cxx ());
10034 error_at (loc, READONLY_MSG (G_("assignment of "
10035 "read-only named return value %qD"),
10036 G_("increment of "
10037 "read-only named return value %qD"),
10038 G_("decrement of "
10039 "read-only named return value %qD"),
10040 G_("read-only named return value %qD "
10041 "used as %<asm%>output")),
10042 arg);
10044 else if (TREE_CODE (arg) == FUNCTION_DECL)
10045 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10046 G_("increment of function %qD"),
10047 G_("decrement of function %qD"),
10048 G_("function %qD used as %<asm%> output")),
10049 arg);
10050 else
10051 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10052 G_("increment of read-only location %qE"),
10053 G_("decrement of read-only location %qE"),
10054 G_("read-only location %qE used as %<asm%> output")),
10055 arg);
10058 /* Print an error message for an invalid lvalue. USE says
10059 how the lvalue is being used and so selects the error message. LOC
10060 is the location for the error. */
10062 void
10063 lvalue_error (location_t loc, enum lvalue_use use)
10065 switch (use)
10067 case lv_assign:
10068 error_at (loc, "lvalue required as left operand of assignment");
10069 break;
10070 case lv_increment:
10071 error_at (loc, "lvalue required as increment operand");
10072 break;
10073 case lv_decrement:
10074 error_at (loc, "lvalue required as decrement operand");
10075 break;
10076 case lv_addressof:
10077 error_at (loc, "lvalue required as unary %<&%> operand");
10078 break;
10079 case lv_asm:
10080 error_at (loc, "lvalue required in asm statement");
10081 break;
10082 default:
10083 gcc_unreachable ();
10087 /* Print an error message for an invalid indirection of type TYPE.
10088 ERRSTRING is the name of the operator for the indirection. */
10090 void
10091 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10093 switch (errstring)
10095 case RO_NULL:
10096 gcc_assert (c_dialect_cxx ());
10097 error_at (loc, "invalid type argument (have %qT)", type);
10098 break;
10099 case RO_ARRAY_INDEXING:
10100 error_at (loc,
10101 "invalid type argument of array indexing (have %qT)",
10102 type);
10103 break;
10104 case RO_UNARY_STAR:
10105 error_at (loc,
10106 "invalid type argument of unary %<*%> (have %qT)",
10107 type);
10108 break;
10109 case RO_ARROW:
10110 error_at (loc,
10111 "invalid type argument of %<->%> (have %qT)",
10112 type);
10113 break;
10114 case RO_ARROW_STAR:
10115 error_at (loc,
10116 "invalid type argument of %<->*%> (have %qT)",
10117 type);
10118 break;
10119 case RO_IMPLICIT_CONVERSION:
10120 error_at (loc,
10121 "invalid type argument of implicit conversion (have %qT)",
10122 type);
10123 break;
10124 default:
10125 gcc_unreachable ();
10129 /* *PTYPE is an incomplete array. Complete it with a domain based on
10130 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10131 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10132 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10135 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10137 tree maxindex, type, main_type, elt, unqual_elt;
10138 int failure = 0, quals;
10139 hashval_t hashcode = 0;
10140 bool overflow_p = false;
10142 maxindex = size_zero_node;
10143 if (initial_value)
10145 if (TREE_CODE (initial_value) == STRING_CST)
10147 int eltsize
10148 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10149 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10151 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10153 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10155 if (vec_safe_is_empty (v))
10157 if (pedantic)
10158 failure = 3;
10159 maxindex = ssize_int (-1);
10161 else
10163 tree curindex;
10164 unsigned HOST_WIDE_INT cnt;
10165 constructor_elt *ce;
10166 bool fold_p = false;
10168 if ((*v)[0].index)
10169 maxindex = (*v)[0].index, fold_p = true;
10171 curindex = maxindex;
10173 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10175 bool curfold_p = false;
10176 if (ce->index)
10177 curindex = ce->index, curfold_p = true;
10178 else
10180 if (fold_p)
10182 /* Since we treat size types now as ordinary
10183 unsigned types, we need an explicit overflow
10184 check. */
10185 tree orig = curindex;
10186 curindex = fold_convert (sizetype, curindex);
10187 overflow_p |= tree_int_cst_lt (curindex, orig);
10189 curindex = size_binop (PLUS_EXPR, curindex,
10190 size_one_node);
10192 if (tree_int_cst_lt (maxindex, curindex))
10193 maxindex = curindex, fold_p = curfold_p;
10195 if (fold_p)
10197 tree orig = maxindex;
10198 maxindex = fold_convert (sizetype, maxindex);
10199 overflow_p |= tree_int_cst_lt (maxindex, orig);
10203 else
10205 /* Make an error message unless that happened already. */
10206 if (initial_value != error_mark_node)
10207 failure = 1;
10210 else
10212 failure = 2;
10213 if (!do_default)
10214 return failure;
10217 type = *ptype;
10218 /* Force an indefinite layout factor. */
10219 if (upc_shared_type_p (type))
10220 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
10221 elt = TREE_TYPE (type);
10222 quals = TYPE_QUALS (strip_array_types (elt));
10223 if (quals == 0)
10224 unqual_elt = elt;
10225 else
10226 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10228 /* Using build_distinct_type_copy and modifying things afterward instead
10229 of using build_array_type to create a new type preserves all of the
10230 TYPE_LANG_FLAG_? bits that the front end may have set. */
10231 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10232 TREE_TYPE (main_type) = unqual_elt;
10233 TYPE_DOMAIN (main_type)
10234 = build_range_type (TREE_TYPE (maxindex),
10235 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10236 layout_type (main_type);
10238 /* Make sure we have the canonical MAIN_TYPE. */
10239 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10240 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10241 hashcode);
10242 main_type = type_hash_canon (hashcode, main_type);
10244 /* Fix the canonical type. */
10245 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10246 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10247 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10248 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10249 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10250 != TYPE_DOMAIN (main_type)))
10251 TYPE_CANONICAL (main_type)
10252 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10253 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10254 else
10255 TYPE_CANONICAL (main_type) = main_type;
10257 if (quals == 0)
10258 type = main_type;
10259 else
10260 type = c_build_qualified_type (main_type, quals);
10262 if (COMPLETE_TYPE_P (type)
10263 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10264 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10266 error ("size of array is too large");
10267 /* If we proceed with the array type as it is, we'll eventually
10268 crash in tree_to_[su]hwi(). */
10269 type = error_mark_node;
10272 *ptype = type;
10273 return failure;
10276 /* Like c_mark_addressable but don't check register qualifier. */
10277 void
10278 c_common_mark_addressable_vec (tree t)
10280 while (handled_component_p (t))
10281 t = TREE_OPERAND (t, 0);
10282 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10283 return;
10284 TREE_ADDRESSABLE (t) = 1;
10289 /* Used to help initialize the builtin-types.def table. When a type of
10290 the correct size doesn't exist, use error_mark_node instead of NULL.
10291 The later results in segfaults even when a decl using the type doesn't
10292 get invoked. */
10294 tree
10295 builtin_type_for_size (int size, bool unsignedp)
10297 tree type = c_common_type_for_size (size, unsignedp);
10298 return type ? type : error_mark_node;
10301 /* A helper function for resolve_overloaded_builtin in resolving the
10302 overloaded __sync_ builtins. Returns a positive power of 2 if the
10303 first operand of PARAMS is a pointer to a supported data type.
10304 Returns 0 if an error is encountered. */
10306 static int
10307 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10309 tree type;
10310 int size;
10312 if (!params)
10314 error ("too few arguments to function %qE", function);
10315 return 0;
10318 type = TREE_TYPE ((*params)[0]);
10319 if (TREE_CODE (type) != POINTER_TYPE)
10320 goto incompatible;
10322 type = TREE_TYPE (type);
10323 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10324 goto incompatible;
10326 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10327 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10328 return size;
10330 incompatible:
10331 error ("incompatible type for argument %d of %qE", 1, function);
10332 return 0;
10335 /* A helper function for resolve_overloaded_builtin. Adds casts to
10336 PARAMS to make arguments match up with those of FUNCTION. Drops
10337 the variadic arguments at the end. Returns false if some error
10338 was encountered; true on success. */
10340 static bool
10341 sync_resolve_params (location_t loc, tree orig_function, tree function,
10342 vec<tree, va_gc> *params, bool orig_format)
10344 function_args_iterator iter;
10345 tree ptype;
10346 unsigned int parmnum;
10348 function_args_iter_init (&iter, TREE_TYPE (function));
10349 /* We've declared the implementation functions to use "volatile void *"
10350 as the pointer parameter, so we shouldn't get any complaints from the
10351 call to check_function_arguments what ever type the user used. */
10352 function_args_iter_next (&iter);
10353 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10354 ptype = TYPE_MAIN_VARIANT (ptype);
10356 /* For the rest of the values, we need to cast these to FTYPE, so that we
10357 don't get warnings for passing pointer types, etc. */
10358 parmnum = 0;
10359 while (1)
10361 tree val, arg_type;
10363 arg_type = function_args_iter_cond (&iter);
10364 /* XXX void_type_node belies the abstraction. */
10365 if (arg_type == void_type_node)
10366 break;
10368 ++parmnum;
10369 if (params->length () <= parmnum)
10371 error_at (loc, "too few arguments to function %qE", orig_function);
10372 return false;
10375 /* Only convert parameters if arg_type is unsigned integer type with
10376 new format sync routines, i.e. don't attempt to convert pointer
10377 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10378 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10379 kinds). */
10380 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10382 /* Ideally for the first conversion we'd use convert_for_assignment
10383 so that we get warnings for anything that doesn't match the pointer
10384 type. This isn't portable across the C and C++ front ends atm. */
10385 val = (*params)[parmnum];
10386 val = convert (ptype, val);
10387 val = convert (arg_type, val);
10388 (*params)[parmnum] = val;
10391 function_args_iter_next (&iter);
10394 /* __atomic routines are not variadic. */
10395 if (!orig_format && params->length () != parmnum + 1)
10397 error_at (loc, "too many arguments to function %qE", orig_function);
10398 return false;
10401 /* The definition of these primitives is variadic, with the remaining
10402 being "an optional list of variables protected by the memory barrier".
10403 No clue what that's supposed to mean, precisely, but we consider all
10404 call-clobbered variables to be protected so we're safe. */
10405 params->truncate (parmnum + 1);
10407 return true;
10410 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10411 RESULT to make it match the type of the first pointer argument in
10412 PARAMS. */
10414 static tree
10415 sync_resolve_return (tree first_param, tree result, bool orig_format)
10417 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10418 tree rtype = TREE_TYPE (result);
10419 ptype = TYPE_MAIN_VARIANT (ptype);
10421 /* New format doesn't require casting unless the types are the same size. */
10422 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10423 return convert (ptype, result);
10424 else
10425 return result;
10428 /* This function verifies the PARAMS to generic atomic FUNCTION.
10429 It returns the size if all the parameters are the same size, otherwise
10430 0 is returned if the parameters are invalid. */
10432 static int
10433 get_atomic_generic_size (location_t loc, tree function,
10434 vec<tree, va_gc> *params)
10436 unsigned int n_param;
10437 unsigned int n_model;
10438 unsigned int x;
10439 int size_0;
10440 tree type_0;
10442 /* Determine the parameter makeup. */
10443 switch (DECL_FUNCTION_CODE (function))
10445 case BUILT_IN_ATOMIC_EXCHANGE:
10446 n_param = 4;
10447 n_model = 1;
10448 break;
10449 case BUILT_IN_ATOMIC_LOAD:
10450 case BUILT_IN_ATOMIC_STORE:
10451 n_param = 3;
10452 n_model = 1;
10453 break;
10454 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10455 n_param = 6;
10456 n_model = 2;
10457 break;
10458 default:
10459 gcc_unreachable ();
10462 if (vec_safe_length (params) != n_param)
10464 error_at (loc, "incorrect number of arguments to function %qE", function);
10465 return 0;
10468 /* Get type of first parameter, and determine its size. */
10469 type_0 = TREE_TYPE ((*params)[0]);
10470 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10472 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10473 function);
10474 return 0;
10477 /* Types must be compile time constant sizes. */
10478 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10480 error_at (loc,
10481 "argument 1 of %qE must be a pointer to a constant size type",
10482 function);
10483 return 0;
10486 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10488 /* Zero size objects are not allowed. */
10489 if (size_0 == 0)
10491 error_at (loc,
10492 "argument 1 of %qE must be a pointer to a nonzero size object",
10493 function);
10494 return 0;
10497 /* Check each other parameter is a pointer and the same size. */
10498 for (x = 0; x < n_param - n_model; x++)
10500 int size;
10501 tree type = TREE_TYPE ((*params)[x]);
10502 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10503 if (n_param == 6 && x == 3)
10504 continue;
10505 if (!POINTER_TYPE_P (type))
10507 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10508 function);
10509 return 0;
10511 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10512 if (size != size_0)
10514 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10515 function);
10516 return 0;
10520 /* Check memory model parameters for validity. */
10521 for (x = n_param - n_model ; x < n_param; x++)
10523 tree p = (*params)[x];
10524 if (TREE_CODE (p) == INTEGER_CST)
10526 int i = tree_to_uhwi (p);
10527 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10529 warning_at (loc, OPT_Winvalid_memory_model,
10530 "invalid memory model argument %d of %qE", x + 1,
10531 function);
10534 else
10535 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10537 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10538 function);
10539 return 0;
10543 return size_0;
10547 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10548 at the beginning of the parameter list PARAMS representing the size of the
10549 objects. This is to match the library ABI requirement. LOC is the location
10550 of the function call.
10551 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10552 returned to allow the external call to be constructed. */
10554 static tree
10555 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10556 vec<tree, va_gc> *params)
10558 tree size_node;
10560 /* Insert a SIZE_T parameter as the first param. If there isn't
10561 enough space, allocate a new vector and recursively re-build with that. */
10562 if (!params->space (1))
10564 unsigned int z, len;
10565 vec<tree, va_gc> *v;
10566 tree f;
10568 len = params->length ();
10569 vec_alloc (v, len + 1);
10570 v->quick_push (build_int_cst (size_type_node, n));
10571 for (z = 0; z < len; z++)
10572 v->quick_push ((*params)[z]);
10573 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10574 vec_free (v);
10575 return f;
10578 /* Add the size parameter and leave as a function call for processing. */
10579 size_node = build_int_cst (size_type_node, n);
10580 params->quick_insert (0, size_node);
10581 return NULL_TREE;
10585 /* Return whether atomic operations for naturally aligned N-byte
10586 arguments are supported, whether inline or through libatomic. */
10587 static bool
10588 atomic_size_supported_p (int n)
10590 switch (n)
10592 case 1:
10593 case 2:
10594 case 4:
10595 case 8:
10596 return true;
10598 case 16:
10599 return targetm.scalar_mode_supported_p (TImode);
10601 default:
10602 return false;
10606 /* This will process an __atomic_exchange function call, determine whether it
10607 needs to be mapped to the _N variation, or turned into a library call.
10608 LOC is the location of the builtin call.
10609 FUNCTION is the DECL that has been invoked;
10610 PARAMS is the argument list for the call. The return value is non-null
10611 TRUE is returned if it is translated into the proper format for a call to the
10612 external library, and NEW_RETURN is set the tree for that function.
10613 FALSE is returned if processing for the _N variation is required, and
10614 NEW_RETURN is set to the the return value the result is copied into. */
10615 static bool
10616 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10617 vec<tree, va_gc> *params, tree *new_return)
10619 tree p0, p1, p2, p3;
10620 tree I_type, I_type_ptr;
10621 int n = get_atomic_generic_size (loc, function, params);
10623 /* Size of 0 is an error condition. */
10624 if (n == 0)
10626 *new_return = error_mark_node;
10627 return true;
10630 /* If not a lock-free size, change to the library generic format. */
10631 if (!atomic_size_supported_p (n))
10633 *new_return = add_atomic_size_parameter (n, loc, function, params);
10634 return true;
10637 /* Otherwise there is a lockfree match, transform the call from:
10638 void fn(T* mem, T* desired, T* return, model)
10639 into
10640 *return = (T) (fn (In* mem, (In) *desired, model)) */
10642 p0 = (*params)[0];
10643 p1 = (*params)[1];
10644 p2 = (*params)[2];
10645 p3 = (*params)[3];
10647 /* Create pointer to appropriate size. */
10648 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10649 I_type_ptr = build_pointer_type (I_type);
10651 /* Convert object pointer to required type. */
10652 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10653 (*params)[0] = p0;
10654 /* Convert new value to required type, and dereference it. */
10655 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10656 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10657 (*params)[1] = p1;
10659 /* Move memory model to the 3rd position, and end param list. */
10660 (*params)[2] = p3;
10661 params->truncate (3);
10663 /* Convert return pointer and dereference it for later assignment. */
10664 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10666 return false;
10670 /* This will process an __atomic_compare_exchange function call, determine
10671 whether it needs to be mapped to the _N variation, or turned into a lib call.
10672 LOC is the location of the builtin call.
10673 FUNCTION is the DECL that has been invoked;
10674 PARAMS is the argument list for the call. The return value is non-null
10675 TRUE is returned if it is translated into the proper format for a call to the
10676 external library, and NEW_RETURN is set the tree for that function.
10677 FALSE is returned if processing for the _N variation is required. */
10679 static bool
10680 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10681 vec<tree, va_gc> *params,
10682 tree *new_return)
10684 tree p0, p1, p2;
10685 tree I_type, I_type_ptr;
10686 int n = get_atomic_generic_size (loc, function, params);
10688 /* Size of 0 is an error condition. */
10689 if (n == 0)
10691 *new_return = error_mark_node;
10692 return true;
10695 /* If not a lock-free size, change to the library generic format. */
10696 if (!atomic_size_supported_p (n))
10698 /* The library generic format does not have the weak parameter, so
10699 remove it from the param list. Since a parameter has been removed,
10700 we can be sure that there is room for the SIZE_T parameter, meaning
10701 there will not be a recursive rebuilding of the parameter list, so
10702 there is no danger this will be done twice. */
10703 if (n > 0)
10705 (*params)[3] = (*params)[4];
10706 (*params)[4] = (*params)[5];
10707 params->truncate (5);
10709 *new_return = add_atomic_size_parameter (n, loc, function, params);
10710 return true;
10713 /* Otherwise, there is a match, so the call needs to be transformed from:
10714 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10715 into
10716 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10718 p0 = (*params)[0];
10719 p1 = (*params)[1];
10720 p2 = (*params)[2];
10722 /* Create pointer to appropriate size. */
10723 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10724 I_type_ptr = build_pointer_type (I_type);
10726 /* Convert object pointer to required type. */
10727 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10728 (*params)[0] = p0;
10730 /* Convert expected pointer to required type. */
10731 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10732 (*params)[1] = p1;
10734 /* Convert desired value to required type, and dereference it. */
10735 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10736 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10737 (*params)[2] = p2;
10739 /* The rest of the parameters are fine. NULL means no special return value
10740 processing.*/
10741 *new_return = NULL;
10742 return false;
10746 /* This will process an __atomic_load function call, determine whether it
10747 needs to be mapped to the _N variation, or turned into a library call.
10748 LOC is the location of the builtin call.
10749 FUNCTION is the DECL that has been invoked;
10750 PARAMS is the argument list for the call. The return value is non-null
10751 TRUE is returned if it is translated into the proper format for a call to the
10752 external library, and NEW_RETURN is set the tree for that function.
10753 FALSE is returned if processing for the _N variation is required, and
10754 NEW_RETURN is set to the the return value the result is copied into. */
10756 static bool
10757 resolve_overloaded_atomic_load (location_t loc, tree function,
10758 vec<tree, va_gc> *params, tree *new_return)
10760 tree p0, p1, p2;
10761 tree I_type, I_type_ptr;
10762 int n = get_atomic_generic_size (loc, function, params);
10764 /* Size of 0 is an error condition. */
10765 if (n == 0)
10767 *new_return = error_mark_node;
10768 return true;
10771 /* If not a lock-free size, change to the library generic format. */
10772 if (!atomic_size_supported_p (n))
10774 *new_return = add_atomic_size_parameter (n, loc, function, params);
10775 return true;
10778 /* Otherwise, there is a match, so the call needs to be transformed from:
10779 void fn(T* mem, T* return, model)
10780 into
10781 *return = (T) (fn ((In *) mem, model)) */
10783 p0 = (*params)[0];
10784 p1 = (*params)[1];
10785 p2 = (*params)[2];
10787 /* Create pointer to appropriate size. */
10788 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10789 I_type_ptr = build_pointer_type (I_type);
10791 /* Convert object pointer to required type. */
10792 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10793 (*params)[0] = p0;
10795 /* Move memory model to the 2nd position, and end param list. */
10796 (*params)[1] = p2;
10797 params->truncate (2);
10799 /* Convert return pointer and dereference it for later assignment. */
10800 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10802 return false;
10806 /* This will process an __atomic_store function call, determine whether it
10807 needs to be mapped to the _N variation, or turned into a library call.
10808 LOC is the location of the builtin call.
10809 FUNCTION is the DECL that has been invoked;
10810 PARAMS is the argument list for the call. The return value is non-null
10811 TRUE is returned if it is translated into the proper format for a call to the
10812 external library, and NEW_RETURN is set the tree for that function.
10813 FALSE is returned if processing for the _N variation is required, and
10814 NEW_RETURN is set to the the return value the result is copied into. */
10816 static bool
10817 resolve_overloaded_atomic_store (location_t loc, tree function,
10818 vec<tree, va_gc> *params, tree *new_return)
10820 tree p0, p1;
10821 tree I_type, I_type_ptr;
10822 int n = get_atomic_generic_size (loc, function, params);
10824 /* Size of 0 is an error condition. */
10825 if (n == 0)
10827 *new_return = error_mark_node;
10828 return true;
10831 /* If not a lock-free size, change to the library generic format. */
10832 if (!atomic_size_supported_p (n))
10834 *new_return = add_atomic_size_parameter (n, loc, function, params);
10835 return true;
10838 /* Otherwise, there is a match, so the call needs to be transformed from:
10839 void fn(T* mem, T* value, model)
10840 into
10841 fn ((In *) mem, (In) *value, model) */
10843 p0 = (*params)[0];
10844 p1 = (*params)[1];
10846 /* Create pointer to appropriate size. */
10847 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10848 I_type_ptr = build_pointer_type (I_type);
10850 /* Convert object pointer to required type. */
10851 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10852 (*params)[0] = p0;
10854 /* Convert new value to required type, and dereference it. */
10855 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10856 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10857 (*params)[1] = p1;
10859 /* The memory model is in the right spot already. Return is void. */
10860 *new_return = NULL_TREE;
10862 return false;
10866 /* Some builtin functions are placeholders for other expressions. This
10867 function should be called immediately after parsing the call expression
10868 before surrounding code has committed to the type of the expression.
10870 LOC is the location of the builtin call.
10872 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10873 PARAMS is the argument list for the call. The return value is non-null
10874 when expansion is complete, and null if normal processing should
10875 continue. */
10877 tree
10878 resolve_overloaded_builtin (location_t loc, tree function,
10879 vec<tree, va_gc> *params)
10881 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10882 bool orig_format = true;
10883 tree new_return = NULL_TREE;
10885 switch (DECL_BUILT_IN_CLASS (function))
10887 case BUILT_IN_NORMAL:
10888 break;
10889 case BUILT_IN_MD:
10890 if (targetm.resolve_overloaded_builtin)
10891 return targetm.resolve_overloaded_builtin (loc, function, params);
10892 else
10893 return NULL_TREE;
10894 default:
10895 return NULL_TREE;
10898 /* Handle BUILT_IN_NORMAL here. */
10899 switch (orig_code)
10901 case BUILT_IN_ATOMIC_EXCHANGE:
10902 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10903 case BUILT_IN_ATOMIC_LOAD:
10904 case BUILT_IN_ATOMIC_STORE:
10906 /* Handle these 4 together so that they can fall through to the next
10907 case if the call is transformed to an _N variant. */
10908 switch (orig_code)
10910 case BUILT_IN_ATOMIC_EXCHANGE:
10912 if (resolve_overloaded_atomic_exchange (loc, function, params,
10913 &new_return))
10914 return new_return;
10915 /* Change to the _N variant. */
10916 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10917 break;
10920 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10922 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10923 params,
10924 &new_return))
10925 return new_return;
10926 /* Change to the _N variant. */
10927 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10928 break;
10930 case BUILT_IN_ATOMIC_LOAD:
10932 if (resolve_overloaded_atomic_load (loc, function, params,
10933 &new_return))
10934 return new_return;
10935 /* Change to the _N variant. */
10936 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10937 break;
10939 case BUILT_IN_ATOMIC_STORE:
10941 if (resolve_overloaded_atomic_store (loc, function, params,
10942 &new_return))
10943 return new_return;
10944 /* Change to the _N variant. */
10945 orig_code = BUILT_IN_ATOMIC_STORE_N;
10946 break;
10948 default:
10949 gcc_unreachable ();
10951 /* Fallthrough to the normal processing. */
10953 case BUILT_IN_ATOMIC_EXCHANGE_N:
10954 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10955 case BUILT_IN_ATOMIC_LOAD_N:
10956 case BUILT_IN_ATOMIC_STORE_N:
10957 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10958 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10959 case BUILT_IN_ATOMIC_AND_FETCH_N:
10960 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10961 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10962 case BUILT_IN_ATOMIC_OR_FETCH_N:
10963 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10964 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10965 case BUILT_IN_ATOMIC_FETCH_AND_N:
10966 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10967 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10968 case BUILT_IN_ATOMIC_FETCH_OR_N:
10970 orig_format = false;
10971 /* Fallthru for parameter processing. */
10973 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10974 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10975 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10976 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10977 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10978 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10979 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10980 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10981 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10982 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10983 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10984 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10985 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10986 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10987 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10988 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10990 int n = sync_resolve_size (function, params);
10991 tree new_function, first_param, result;
10992 enum built_in_function fncode;
10994 if (n == 0)
10995 return error_mark_node;
10997 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10998 new_function = builtin_decl_explicit (fncode);
10999 if (!sync_resolve_params (loc, function, new_function, params,
11000 orig_format))
11001 return error_mark_node;
11003 first_param = (*params)[0];
11004 result = build_function_call_vec (loc, vNULL, new_function, params,
11005 NULL);
11006 if (result == error_mark_node)
11007 return result;
11008 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11009 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11010 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11011 result = sync_resolve_return (first_param, result, orig_format);
11013 /* If new_return is set, assign function to that expr and cast the
11014 result to void since the generic interface returned void. */
11015 if (new_return)
11017 /* Cast function result from I{1,2,4,8,16} to the required type. */
11018 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11019 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11020 result);
11021 TREE_SIDE_EFFECTS (result) = 1;
11022 protected_set_expr_location (result, loc);
11023 result = convert (void_type_node, result);
11025 return result;
11028 default:
11029 return NULL_TREE;
11033 /* vector_types_compatible_elements_p is used in type checks of vectors
11034 values used as operands of binary operators. Where it returns true, and
11035 the other checks of the caller succeed (being vector types in he first
11036 place, and matching number of elements), we can just treat the types
11037 as essentially the same.
11038 Contrast with vector_targets_convertible_p, which is used for vector
11039 pointer types, and vector_types_convertible_p, which will allow
11040 language-specific matches under the control of flag_lax_vector_conversions,
11041 and might still require a conversion. */
11042 /* True if vector types T1 and T2 can be inputs to the same binary
11043 operator without conversion.
11044 We don't check the overall vector size here because some of our callers
11045 want to give different error messages when the vectors are compatible
11046 except for the element count. */
11048 bool
11049 vector_types_compatible_elements_p (tree t1, tree t2)
11051 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11052 t1 = TREE_TYPE (t1);
11053 t2 = TREE_TYPE (t2);
11055 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11057 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11058 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11059 || c2 == FIXED_POINT_TYPE));
11061 t1 = c_common_signed_type (t1);
11062 t2 = c_common_signed_type (t2);
11063 /* Equality works here because c_common_signed_type uses
11064 TYPE_MAIN_VARIANT. */
11065 if (t1 == t2)
11066 return true;
11067 if (opaque && c1 == c2
11068 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11069 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11070 return true;
11071 return false;
11074 /* Check for missing format attributes on function pointers. LTYPE is
11075 the new type or left-hand side type. RTYPE is the old type or
11076 right-hand side type. Returns TRUE if LTYPE is missing the desired
11077 attribute. */
11079 bool
11080 check_missing_format_attribute (tree ltype, tree rtype)
11082 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11083 tree ra;
11085 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11086 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11087 break;
11088 if (ra)
11090 tree la;
11091 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11092 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11093 break;
11094 return !la;
11096 else
11097 return false;
11100 /* Subscripting with type char is likely to lose on a machine where
11101 chars are signed. So warn on any machine, but optionally. Don't
11102 warn for unsigned char since that type is safe. Don't warn for
11103 signed char because anyone who uses that must have done so
11104 deliberately. Furthermore, we reduce the false positive load by
11105 warning only for non-constant value of type char. */
11107 void
11108 warn_array_subscript_with_type_char (tree index)
11110 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11111 && TREE_CODE (index) != INTEGER_CST)
11112 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11115 /* Implement -Wparentheses for the unexpected C precedence rules, to
11116 cover cases like x + y << z which readers are likely to
11117 misinterpret. We have seen an expression in which CODE is a binary
11118 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11119 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11120 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11121 expression was not formed using a binary or unary operator, or it
11122 was enclosed in parentheses. */
11124 void
11125 warn_about_parentheses (location_t loc, enum tree_code code,
11126 enum tree_code code_left, tree arg_left,
11127 enum tree_code code_right, tree arg_right)
11129 if (!warn_parentheses)
11130 return;
11132 /* This macro tests that the expression ARG with original tree code
11133 CODE appears to be a boolean expression. or the result of folding a
11134 boolean expression. */
11135 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11136 (truth_value_p (TREE_CODE (ARG)) \
11137 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11138 /* Folding may create 0 or 1 integers from other expressions. */ \
11139 || ((CODE) != INTEGER_CST \
11140 && (integer_onep (ARG) || integer_zerop (ARG))))
11142 switch (code)
11144 case LSHIFT_EXPR:
11145 if (code_left == PLUS_EXPR)
11146 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11147 "suggest parentheses around %<+%> inside %<<<%>");
11148 else if (code_right == PLUS_EXPR)
11149 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11150 "suggest parentheses around %<+%> inside %<<<%>");
11151 else if (code_left == MINUS_EXPR)
11152 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11153 "suggest parentheses around %<-%> inside %<<<%>");
11154 else if (code_right == MINUS_EXPR)
11155 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11156 "suggest parentheses around %<-%> inside %<<<%>");
11157 return;
11159 case RSHIFT_EXPR:
11160 if (code_left == PLUS_EXPR)
11161 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11162 "suggest parentheses around %<+%> inside %<>>%>");
11163 else if (code_right == PLUS_EXPR)
11164 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11165 "suggest parentheses around %<+%> inside %<>>%>");
11166 else if (code_left == MINUS_EXPR)
11167 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11168 "suggest parentheses around %<-%> inside %<>>%>");
11169 else if (code_right == MINUS_EXPR)
11170 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11171 "suggest parentheses around %<-%> inside %<>>%>");
11172 return;
11174 case TRUTH_ORIF_EXPR:
11175 if (code_left == TRUTH_ANDIF_EXPR)
11176 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11177 "suggest parentheses around %<&&%> within %<||%>");
11178 else if (code_right == TRUTH_ANDIF_EXPR)
11179 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11180 "suggest parentheses around %<&&%> within %<||%>");
11181 return;
11183 case BIT_IOR_EXPR:
11184 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11185 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11186 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11187 "suggest parentheses around arithmetic in operand of %<|%>");
11188 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11189 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11190 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11191 "suggest parentheses around arithmetic in operand of %<|%>");
11192 /* Check cases like x|y==z */
11193 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11194 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11195 "suggest parentheses around comparison in operand of %<|%>");
11196 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11197 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11198 "suggest parentheses around comparison in operand of %<|%>");
11199 /* Check cases like !x | y */
11200 else if (code_left == TRUTH_NOT_EXPR
11201 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11202 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11203 "suggest parentheses around operand of "
11204 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11205 return;
11207 case BIT_XOR_EXPR:
11208 if (code_left == BIT_AND_EXPR
11209 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11210 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11211 "suggest parentheses around arithmetic in operand of %<^%>");
11212 else if (code_right == BIT_AND_EXPR
11213 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11214 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11215 "suggest parentheses around arithmetic in operand of %<^%>");
11216 /* Check cases like x^y==z */
11217 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11218 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11219 "suggest parentheses around comparison in operand of %<^%>");
11220 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11221 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11222 "suggest parentheses around comparison in operand of %<^%>");
11223 return;
11225 case BIT_AND_EXPR:
11226 if (code_left == PLUS_EXPR)
11227 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11228 "suggest parentheses around %<+%> in operand of %<&%>");
11229 else if (code_right == PLUS_EXPR)
11230 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11231 "suggest parentheses around %<+%> in operand of %<&%>");
11232 else if (code_left == MINUS_EXPR)
11233 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11234 "suggest parentheses around %<-%> in operand of %<&%>");
11235 else if (code_right == MINUS_EXPR)
11236 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11237 "suggest parentheses around %<-%> in operand of %<&%>");
11238 /* Check cases like x&y==z */
11239 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11240 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11241 "suggest parentheses around comparison in operand of %<&%>");
11242 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11243 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11244 "suggest parentheses around comparison in operand of %<&%>");
11245 /* Check cases like !x & y */
11246 else if (code_left == TRUTH_NOT_EXPR
11247 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11248 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11249 "suggest parentheses around operand of "
11250 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11251 return;
11253 case EQ_EXPR:
11254 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11255 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11256 "suggest parentheses around comparison in operand of %<==%>");
11257 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11258 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11259 "suggest parentheses around comparison in operand of %<==%>");
11260 return;
11261 case NE_EXPR:
11262 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11263 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11264 "suggest parentheses around comparison in operand of %<!=%>");
11265 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11266 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11267 "suggest parentheses around comparison in operand of %<!=%>");
11268 return;
11270 default:
11271 if (TREE_CODE_CLASS (code) == tcc_comparison)
11273 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11274 && code_left != NE_EXPR && code_left != EQ_EXPR
11275 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11276 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11277 "comparisons like %<X<=Y<=Z%> do not "
11278 "have their mathematical meaning");
11279 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11280 && code_right != NE_EXPR && code_right != EQ_EXPR
11281 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11282 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11283 "comparisons like %<X<=Y<=Z%> do not "
11284 "have their mathematical meaning");
11286 return;
11288 #undef NOT_A_BOOLEAN_EXPR_P
11291 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11293 void
11294 warn_for_unused_label (tree label)
11296 if (!TREE_USED (label))
11298 if (DECL_INITIAL (label))
11299 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11300 else
11301 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11305 /* Warn for division by zero according to the value of DIVISOR. LOC
11306 is the location of the division operator. */
11308 void
11309 warn_for_div_by_zero (location_t loc, tree divisor)
11311 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11312 about division by zero. Do not issue a warning if DIVISOR has a
11313 floating-point type, since we consider 0.0/0.0 a valid way of
11314 generating a NaN. */
11315 if (c_inhibit_evaluation_warnings == 0
11316 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11317 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11320 /* Subroutine of build_binary_op. Give warnings for comparisons
11321 between signed and unsigned quantities that may fail. Do the
11322 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11323 so that casts will be considered, but default promotions won't
11326 LOCATION is the location of the comparison operator.
11328 The arguments of this function map directly to local variables
11329 of build_binary_op. */
11331 void
11332 warn_for_sign_compare (location_t location,
11333 tree orig_op0, tree orig_op1,
11334 tree op0, tree op1,
11335 tree result_type, enum tree_code resultcode)
11337 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11338 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11339 int unsignedp0, unsignedp1;
11341 /* In C++, check for comparison of different enum types. */
11342 if (c_dialect_cxx()
11343 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11344 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11345 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11346 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11348 warning_at (location,
11349 OPT_Wsign_compare, "comparison between types %qT and %qT",
11350 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11353 /* Do not warn if the comparison is being done in a signed type,
11354 since the signed type will only be chosen if it can represent
11355 all the values of the unsigned type. */
11356 if (!TYPE_UNSIGNED (result_type))
11357 /* OK */;
11358 /* Do not warn if both operands are unsigned. */
11359 else if (op0_signed == op1_signed)
11360 /* OK */;
11361 else
11363 tree sop, uop, base_type;
11364 bool ovf;
11366 if (op0_signed)
11367 sop = orig_op0, uop = orig_op1;
11368 else
11369 sop = orig_op1, uop = orig_op0;
11371 STRIP_TYPE_NOPS (sop);
11372 STRIP_TYPE_NOPS (uop);
11373 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11374 ? TREE_TYPE (result_type) : result_type);
11376 /* Do not warn if the signed quantity is an unsuffixed integer
11377 literal (or some static constant expression involving such
11378 literals or a conditional expression involving such literals)
11379 and it is non-negative. */
11380 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11381 /* OK */;
11382 /* Do not warn if the comparison is an equality operation, the
11383 unsigned quantity is an integral constant, and it would fit
11384 in the result if the result were signed. */
11385 else if (TREE_CODE (uop) == INTEGER_CST
11386 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11387 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11388 /* OK */;
11389 /* In C, do not warn if the unsigned quantity is an enumeration
11390 constant and its maximum value would fit in the result if the
11391 result were signed. */
11392 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11393 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11394 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11395 c_common_signed_type (base_type)))
11396 /* OK */;
11397 else
11398 warning_at (location,
11399 OPT_Wsign_compare,
11400 "comparison between signed and unsigned integer expressions");
11403 /* Warn if two unsigned values are being compared in a size larger
11404 than their original size, and one (and only one) is the result of
11405 a `~' operator. This comparison will always fail.
11407 Also warn if one operand is a constant, and the constant does not
11408 have all bits set that are set in the ~ operand when it is
11409 extended. */
11411 op0 = c_common_get_narrower (op0, &unsignedp0);
11412 op1 = c_common_get_narrower (op1, &unsignedp1);
11414 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11415 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11417 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11418 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11419 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11420 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11422 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11424 tree primop;
11425 HOST_WIDE_INT constant, mask;
11426 int unsignedp;
11427 unsigned int bits;
11429 if (tree_fits_shwi_p (op0))
11431 primop = op1;
11432 unsignedp = unsignedp1;
11433 constant = tree_to_shwi (op0);
11435 else
11437 primop = op0;
11438 unsignedp = unsignedp0;
11439 constant = tree_to_shwi (op1);
11442 bits = TYPE_PRECISION (TREE_TYPE (primop));
11443 if (bits < TYPE_PRECISION (result_type)
11444 && bits < HOST_BITS_PER_LONG && unsignedp)
11446 mask = (~ (HOST_WIDE_INT) 0) << bits;
11447 if ((mask & constant) != mask)
11449 if (constant == 0)
11450 warning_at (location, OPT_Wsign_compare,
11451 "promoted ~unsigned is always non-zero");
11452 else
11453 warning_at (location, OPT_Wsign_compare,
11454 "comparison of promoted ~unsigned with constant");
11458 else if (unsignedp0 && unsignedp1
11459 && (TYPE_PRECISION (TREE_TYPE (op0))
11460 < TYPE_PRECISION (result_type))
11461 && (TYPE_PRECISION (TREE_TYPE (op1))
11462 < TYPE_PRECISION (result_type)))
11463 warning_at (location, OPT_Wsign_compare,
11464 "comparison of promoted ~unsigned with unsigned");
11468 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11469 type via c_common_type. If -Wdouble-promotion is in use, and the
11470 conditions for warning have been met, issue a warning. GMSGID is
11471 the warning message. It must have two %T specifiers for the type
11472 that was converted (generally "float") and the type to which it was
11473 converted (generally "double), respectively. LOC is the location
11474 to which the awrning should refer. */
11476 void
11477 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11478 const char *gmsgid, location_t loc)
11480 tree source_type;
11482 if (!warn_double_promotion)
11483 return;
11484 /* If the conversion will not occur at run-time, there is no need to
11485 warn about it. */
11486 if (c_inhibit_evaluation_warnings)
11487 return;
11488 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11489 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11490 return;
11491 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11492 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11493 source_type = type1;
11494 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11495 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11496 source_type = type2;
11497 else
11498 return;
11499 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11502 /* Setup a TYPE_DECL node as a typedef representation.
11504 X is a TYPE_DECL for a typedef statement. Create a brand new
11505 ..._TYPE node (which will be just a variant of the existing
11506 ..._TYPE node with identical properties) and then install X
11507 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11509 The whole point here is to end up with a situation where each
11510 and every ..._TYPE node the compiler creates will be uniquely
11511 associated with AT MOST one node representing a typedef name.
11512 This way, even though the compiler substitutes corresponding
11513 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11514 early on, later parts of the compiler can always do the reverse
11515 translation and get back the corresponding typedef name. For
11516 example, given:
11518 typedef struct S MY_TYPE;
11519 MY_TYPE object;
11521 Later parts of the compiler might only know that `object' was of
11522 type `struct S' if it were not for code just below. With this
11523 code however, later parts of the compiler see something like:
11525 struct S' == struct S
11526 typedef struct S' MY_TYPE;
11527 struct S' object;
11529 And they can then deduce (from the node for type struct S') that
11530 the original object declaration was:
11532 MY_TYPE object;
11534 Being able to do this is important for proper support of protoize,
11535 and also for generating precise symbolic debugging information
11536 which takes full account of the programmer's (typedef) vocabulary.
11538 Obviously, we don't want to generate a duplicate ..._TYPE node if
11539 the TYPE_DECL node that we are now processing really represents a
11540 standard built-in type. */
11542 void
11543 set_underlying_type (tree x)
11545 if (x == error_mark_node)
11546 return;
11547 if (DECL_IS_BUILTIN (x))
11549 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11550 TYPE_NAME (TREE_TYPE (x)) = x;
11552 else if (TREE_TYPE (x) != error_mark_node
11553 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11555 tree tt = TREE_TYPE (x);
11556 DECL_ORIGINAL_TYPE (x) = tt;
11557 tt = build_variant_type_copy (tt);
11558 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11559 TYPE_NAME (tt) = x;
11560 TREE_USED (tt) = TREE_USED (x);
11561 TREE_TYPE (x) = tt;
11565 /* Record the types used by the current global variable declaration
11566 being parsed, so that we can decide later to emit their debug info.
11567 Those types are in types_used_by_cur_var_decl, and we are going to
11568 store them in the types_used_by_vars_hash hash table.
11569 DECL is the declaration of the global variable that has been parsed. */
11571 void
11572 record_types_used_by_current_var_decl (tree decl)
11574 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11576 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11578 tree type = types_used_by_cur_var_decl->pop ();
11579 types_used_by_var_decl_insert (type, decl);
11583 /* If DECL is a typedef that is declared in the current function,
11584 record it for the purpose of -Wunused-local-typedefs. */
11586 void
11587 record_locally_defined_typedef (tree decl)
11589 struct c_language_function *l;
11591 if (!warn_unused_local_typedefs
11592 || cfun == NULL
11593 /* if this is not a locally defined typedef then we are not
11594 interested. */
11595 || !is_typedef_decl (decl)
11596 || !decl_function_context (decl))
11597 return;
11599 l = (struct c_language_function *) cfun->language;
11600 vec_safe_push (l->local_typedefs, decl);
11603 /* If T is a TYPE_DECL declared locally, mark it as used. */
11605 void
11606 maybe_record_typedef_use (tree t)
11608 if (!is_typedef_decl (t))
11609 return;
11611 TREE_USED (t) = true;
11614 /* Warn if there are some unused locally defined typedefs in the
11615 current function. */
11617 void
11618 maybe_warn_unused_local_typedefs (void)
11620 int i;
11621 tree decl;
11622 /* The number of times we have emitted -Wunused-local-typedefs
11623 warnings. If this is different from errorcount, that means some
11624 unrelated errors have been issued. In which case, we'll avoid
11625 emitting "unused-local-typedefs" warnings. */
11626 static int unused_local_typedefs_warn_count;
11627 struct c_language_function *l;
11629 if (cfun == NULL)
11630 return;
11632 if ((l = (struct c_language_function *) cfun->language) == NULL)
11633 return;
11635 if (warn_unused_local_typedefs
11636 && errorcount == unused_local_typedefs_warn_count)
11638 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11639 if (!TREE_USED (decl))
11640 warning_at (DECL_SOURCE_LOCATION (decl),
11641 OPT_Wunused_local_typedefs,
11642 "typedef %qD locally defined but not used", decl);
11643 unused_local_typedefs_warn_count = errorcount;
11646 vec_free (l->local_typedefs);
11649 /* The C and C++ parsers both use vectors to hold function arguments.
11650 For efficiency, we keep a cache of unused vectors. This is the
11651 cache. */
11653 typedef vec<tree, va_gc> *tree_gc_vec;
11654 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11656 /* Return a new vector from the cache. If the cache is empty,
11657 allocate a new vector. These vectors are GC'ed, so it is OK if the
11658 pointer is not released.. */
11660 vec<tree, va_gc> *
11661 make_tree_vector (void)
11663 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11664 return tree_vector_cache->pop ();
11665 else
11667 /* Passing 0 to vec::alloc returns NULL, and our callers require
11668 that we always return a non-NULL value. The vector code uses
11669 4 when growing a NULL vector, so we do too. */
11670 vec<tree, va_gc> *v;
11671 vec_alloc (v, 4);
11672 return v;
11676 /* Release a vector of trees back to the cache. */
11678 void
11679 release_tree_vector (vec<tree, va_gc> *vec)
11681 if (vec != NULL)
11683 vec->truncate (0);
11684 vec_safe_push (tree_vector_cache, vec);
11688 /* Get a new tree vector holding a single tree. */
11690 vec<tree, va_gc> *
11691 make_tree_vector_single (tree t)
11693 vec<tree, va_gc> *ret = make_tree_vector ();
11694 ret->quick_push (t);
11695 return ret;
11698 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11700 vec<tree, va_gc> *
11701 make_tree_vector_from_list (tree list)
11703 vec<tree, va_gc> *ret = make_tree_vector ();
11704 for (; list; list = TREE_CHAIN (list))
11705 vec_safe_push (ret, TREE_VALUE (list));
11706 return ret;
11709 /* Get a new tree vector which is a copy of an existing one. */
11711 vec<tree, va_gc> *
11712 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11714 vec<tree, va_gc> *ret;
11715 unsigned int ix;
11716 tree t;
11718 ret = make_tree_vector ();
11719 vec_safe_reserve (ret, vec_safe_length (orig));
11720 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11721 ret->quick_push (t);
11722 return ret;
11725 /* Return true if KEYWORD starts a type specifier. */
11727 bool
11728 keyword_begins_type_specifier (enum rid keyword)
11730 switch (keyword)
11732 case RID_AUTO_TYPE:
11733 case RID_INT:
11734 case RID_CHAR:
11735 case RID_FLOAT:
11736 case RID_DOUBLE:
11737 case RID_VOID:
11738 case RID_INT128:
11739 case RID_UNSIGNED:
11740 case RID_LONG:
11741 case RID_SHORT:
11742 case RID_SIGNED:
11743 case RID_DFLOAT32:
11744 case RID_DFLOAT64:
11745 case RID_DFLOAT128:
11746 case RID_FRACT:
11747 case RID_ACCUM:
11748 case RID_BOOL:
11749 case RID_WCHAR:
11750 case RID_CHAR16:
11751 case RID_CHAR32:
11752 case RID_SAT:
11753 case RID_COMPLEX:
11754 case RID_TYPEOF:
11755 case RID_STRUCT:
11756 case RID_CLASS:
11757 case RID_UNION:
11758 case RID_ENUM:
11759 return true;
11760 default:
11761 return false;
11765 /* Return true if KEYWORD names a type qualifier. */
11767 bool
11768 keyword_is_type_qualifier (enum rid keyword)
11770 switch (keyword)
11772 case RID_CONST:
11773 case RID_VOLATILE:
11774 case RID_RESTRICT:
11775 case RID_ATOMIC:
11776 return true;
11777 default:
11778 return false;
11782 /* Return true if KEYWORD names a storage class specifier.
11784 RID_TYPEDEF is not included in this list despite `typedef' being
11785 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11786 such for syntactic convenience only. */
11788 bool
11789 keyword_is_storage_class_specifier (enum rid keyword)
11791 switch (keyword)
11793 case RID_STATIC:
11794 case RID_EXTERN:
11795 case RID_REGISTER:
11796 case RID_AUTO:
11797 case RID_MUTABLE:
11798 case RID_THREAD:
11799 return true;
11800 default:
11801 return false;
11805 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11807 static bool
11808 keyword_is_function_specifier (enum rid keyword)
11810 switch (keyword)
11812 case RID_INLINE:
11813 case RID_NORETURN:
11814 case RID_VIRTUAL:
11815 case RID_EXPLICIT:
11816 return true;
11817 default:
11818 return false;
11822 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11823 declaration-specifier (C99 6.7). */
11825 bool
11826 keyword_is_decl_specifier (enum rid keyword)
11828 if (keyword_is_storage_class_specifier (keyword)
11829 || keyword_is_type_qualifier (keyword)
11830 || keyword_is_function_specifier (keyword))
11831 return true;
11833 switch (keyword)
11835 case RID_TYPEDEF:
11836 case RID_FRIEND:
11837 case RID_CONSTEXPR:
11838 return true;
11839 default:
11840 return false;
11844 /* Initialize language-specific-bits of tree_contains_struct. */
11846 void
11847 c_common_init_ts (void)
11849 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11850 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11851 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11852 MARK_TS_COMMON (UPC_FORALL_STMT);
11853 MARK_TS_COMMON (UPC_SYNC_STMT);
11856 /* Build a user-defined numeric literal out of an integer constant type VALUE
11857 with identifier SUFFIX. */
11859 tree
11860 build_userdef_literal (tree suffix_id, tree value,
11861 enum overflow_type overflow, tree num_string)
11863 tree literal = make_node (USERDEF_LITERAL);
11864 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11865 USERDEF_LITERAL_VALUE (literal) = value;
11866 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11867 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11868 return literal;
11871 /* For vector[index], convert the vector to a
11872 pointer of the underlying type. */
11873 void
11874 convert_vector_to_pointer_for_subscript (location_t loc,
11875 tree* vecp, tree index)
11877 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11879 tree type = TREE_TYPE (*vecp);
11880 tree type1;
11882 if (TREE_CODE (index) == INTEGER_CST)
11883 if (!tree_fits_uhwi_p (index)
11884 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11885 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11887 c_common_mark_addressable_vec (*vecp);
11888 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11889 type = build_pointer_type (type);
11890 type1 = build_pointer_type (TREE_TYPE (*vecp));
11891 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11892 *vecp = convert (type, *vecp);
11896 /* Determine which of the operands, if any, is a scalar that needs to be
11897 converted to a vector, for the range of operations. */
11898 enum stv_conv
11899 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11900 bool complain)
11902 tree type0 = TREE_TYPE (op0);
11903 tree type1 = TREE_TYPE (op1);
11904 bool integer_only_op = false;
11905 enum stv_conv ret = stv_firstarg;
11907 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11908 || TREE_CODE (type1) == VECTOR_TYPE);
11909 switch (code)
11911 /* Most GENERIC binary expressions require homogeneous arguments.
11912 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11913 argument that is a vector and a second one that is a scalar, so
11914 we never return stv_secondarg for them. */
11915 case RSHIFT_EXPR:
11916 case LSHIFT_EXPR:
11917 if (TREE_CODE (type0) == INTEGER_TYPE
11918 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11920 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11922 if (complain)
11923 error_at (loc, "conversion of scalar %qT to vector %qT "
11924 "involves truncation", type0, type1);
11925 return stv_error;
11927 else
11928 return stv_firstarg;
11930 break;
11932 case BIT_IOR_EXPR:
11933 case BIT_XOR_EXPR:
11934 case BIT_AND_EXPR:
11935 integer_only_op = true;
11936 /* ... fall through ... */
11938 case VEC_COND_EXPR:
11940 case PLUS_EXPR:
11941 case MINUS_EXPR:
11942 case MULT_EXPR:
11943 case TRUNC_DIV_EXPR:
11944 case CEIL_DIV_EXPR:
11945 case FLOOR_DIV_EXPR:
11946 case ROUND_DIV_EXPR:
11947 case EXACT_DIV_EXPR:
11948 case TRUNC_MOD_EXPR:
11949 case FLOOR_MOD_EXPR:
11950 case RDIV_EXPR:
11951 case EQ_EXPR:
11952 case NE_EXPR:
11953 case LE_EXPR:
11954 case GE_EXPR:
11955 case LT_EXPR:
11956 case GT_EXPR:
11957 /* What about UNLT_EXPR? */
11958 if (TREE_CODE (type0) == VECTOR_TYPE)
11960 tree tmp;
11961 ret = stv_secondarg;
11962 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11963 tmp = type0; type0 = type1; type1 = tmp;
11964 tmp = op0; op0 = op1; op1 = tmp;
11967 if (TREE_CODE (type0) == INTEGER_TYPE
11968 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11970 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11972 if (complain)
11973 error_at (loc, "conversion of scalar %qT to vector %qT "
11974 "involves truncation", type0, type1);
11975 return stv_error;
11977 return ret;
11979 else if (!integer_only_op
11980 /* Allow integer --> real conversion if safe. */
11981 && (TREE_CODE (type0) == REAL_TYPE
11982 || TREE_CODE (type0) == INTEGER_TYPE)
11983 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11985 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11987 if (complain)
11988 error_at (loc, "conversion of scalar %qT to vector %qT "
11989 "involves truncation", type0, type1);
11990 return stv_error;
11992 return ret;
11994 default:
11995 break;
11998 return stv_nothing;
12001 /* Return true iff ALIGN is an integral constant that is a fundamental
12002 alignment, as defined by [basic.align] in the c++-11
12003 specifications.
12005 That is:
12007 [A fundamental alignment is represented by an alignment less than or
12008 equal to the greatest alignment supported by the implementation
12009 in all contexts, which is equal to
12010 alignof(max_align_t)]. */
12012 bool
12013 cxx_fundamental_alignment_p (unsigned align)
12015 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12016 TYPE_ALIGN (long_double_type_node)));
12019 /* Return true if T is a pointer to a zero-sized aggregate. */
12021 bool
12022 pointer_to_zero_sized_aggr_p (tree t)
12024 if (!POINTER_TYPE_P (t))
12025 return false;
12026 t = TREE_TYPE (t);
12027 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12030 #include "gt-c-family-c-common.h"