Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob2147a14f659dba75cde9f448cc3f0b25b9f3bc22
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2013 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 "flags.h"
27 #include "c-pragma.h"
28 #include "ggc.h"
29 #include "c-common.h"
30 #include "c-objc.h"
31 #include "c-upc.h"
32 #include "tm_p.h"
33 #include "obstack.h"
34 #include "cpplib.h"
35 #include "target.h"
36 #include "common/common-target.h"
37 #include "langhooks.h"
38 #include "tree-inline.h"
39 #include "toplev.h"
40 #include "diagnostic.h"
41 #include "tree-iterator.h"
42 #include "hashtab.h"
43 #include "opts.h"
44 #include "cgraph.h"
45 #include "target-def.h"
47 cpp_reader *parse_in; /* Declared in c-pragma.h. */
49 /* The following symbols are subsumed in the c_global_trees array, and
50 listed here individually for documentation purposes.
52 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
54 tree short_integer_type_node;
55 tree long_integer_type_node;
56 tree long_long_integer_type_node;
57 tree int128_integer_type_node;
59 tree short_unsigned_type_node;
60 tree long_unsigned_type_node;
61 tree long_long_unsigned_type_node;
62 tree int128_unsigned_type_node;
64 tree truthvalue_type_node;
65 tree truthvalue_false_node;
66 tree truthvalue_true_node;
68 tree ptrdiff_type_node;
70 tree unsigned_char_type_node;
71 tree signed_char_type_node;
72 tree wchar_type_node;
74 tree char16_type_node;
75 tree char32_type_node;
77 tree float_type_node;
78 tree double_type_node;
79 tree long_double_type_node;
81 tree complex_integer_type_node;
82 tree complex_float_type_node;
83 tree complex_double_type_node;
84 tree complex_long_double_type_node;
86 tree dfloat32_type_node;
87 tree dfloat64_type_node;
88 tree_dfloat128_type_node;
90 tree intQI_type_node;
91 tree intHI_type_node;
92 tree intSI_type_node;
93 tree intDI_type_node;
94 tree intTI_type_node;
96 tree unsigned_intQI_type_node;
97 tree unsigned_intHI_type_node;
98 tree unsigned_intSI_type_node;
99 tree unsigned_intDI_type_node;
100 tree unsigned_intTI_type_node;
102 tree widest_integer_literal_type_node;
103 tree widest_unsigned_literal_type_node;
105 Nodes for types `void *' and `const void *'.
107 tree ptr_type_node, const_ptr_type_node;
109 Nodes for types `char *' and `const char *'.
111 tree string_type_node, const_string_type_node;
113 Type `char[SOMENUMBER]'.
114 Used when an array of char is needed and the size is irrelevant.
116 tree char_array_type_node;
118 Type `int[SOMENUMBER]' or something like it.
119 Used when an array of int needed and the size is irrelevant.
121 tree int_array_type_node;
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
126 tree wchar_array_type_node;
128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
131 tree char16_array_type_node;
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
136 tree char32_array_type_node;
138 Type `int ()' -- used for implicit declaration of functions.
140 tree default_function_type;
142 A VOID_TYPE node, packaged in a TREE_LIST.
144 tree void_list_node;
146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
150 tree function_name_decl_node;
151 tree pretty_function_name_decl_node;
152 tree c99_function_name_decl_node;
154 Stack of nested function name VAR_DECLs.
156 tree saved_function_name_decls;
160 tree c_global_trees[CTI_MAX];
162 /* Switches common to the C front ends. */
164 /* Nonzero means don't output line number information. */
166 char flag_no_line_commands;
168 /* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
171 char flag_no_output;
173 /* Nonzero means dump macros in some fashion. */
175 char flag_dump_macros;
177 /* Nonzero means pass #include lines through to the output. */
179 char flag_dump_includes;
181 /* Nonzero means process PCH files while preprocessing. */
183 bool flag_pch_preprocess;
185 /* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
188 const char *pch_file;
190 /* Nonzero if an ISO standard was selected. It rejects macros in the
191 user's namespace. */
192 int flag_iso;
194 /* Nonzero whenever UPC -fupc-threads-N is asserted.
195 The value N gives the number of UPC threads to be
196 defined at compile-time. */
197 int flag_upc_threads;
199 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
200 int flag_upc_pthreads;
202 /* The implementation model for UPC threads that
203 are mapped to POSIX threads, specified at compilation
204 time by the -fupc-pthreads-model-* switch. */
205 upc_pthreads_model_kind upc_pthreads_model;
207 /* C/ObjC language option variables. */
210 /* Nonzero means allow type mismatches in conditional expressions;
211 just make their values `void'. */
213 int flag_cond_mismatch;
215 /* Nonzero means enable C89 Amendment 1 features. */
217 int flag_isoc94;
219 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
221 int flag_isoc99;
223 /* Nonzero means use the ISO C11 dialect of C. */
225 int flag_isoc11;
227 /* Nonzero means that we have builtin functions, and main is an int. */
229 int flag_hosted = 1;
232 /* ObjC language option variables. */
235 /* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
239 int print_struct_values;
241 /* Tells the compiler what is the constant string class for ObjC. */
243 const char *constant_string_class_name;
246 /* C++ language option variables. */
249 /* Nonzero means generate separate instantiation control files and
250 juggle them at link time. */
252 int flag_use_repository;
254 /* The C++ dialect being used. C++98 is the default. */
256 enum cxx_dialect cxx_dialect = cxx98;
258 /* Maximum template instantiation depth. This limit exists to limit the
259 time it takes to notice excessively recursive template instantiations.
261 The default is lower than the 1024 recommended by the C++0x standard
262 because G++ runs out of stack before 1024 with highly recursive template
263 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
265 int max_tinst_depth = 900;
267 /* The elements of `ridpointers' are identifier nodes for the reserved
268 type names and storage classes. It is indexed by a RID_... value. */
269 tree *ridpointers;
271 tree (*make_fname_decl) (location_t, tree, int);
273 /* Nonzero means don't warn about problems that occur when the code is
274 executed. */
275 int c_inhibit_evaluation_warnings;
277 /* Whether we are building a boolean conversion inside
278 convert_for_assignment, or some other late binary operation. If
279 build_binary_op is called for C (from code shared by C and C++) in
280 this case, then the operands have already been folded and the
281 result will not be folded again, so C_MAYBE_CONST_EXPR should not
282 be generated. */
283 bool in_late_binary_op;
285 /* Whether lexing has been completed, so subsequent preprocessor
286 errors should use the compiler's input_location. */
287 bool done_lexing = false;
289 /* Information about how a function name is generated. */
290 struct fname_var_t
292 tree *const decl; /* pointer to the VAR_DECL. */
293 const unsigned rid; /* RID number for the identifier. */
294 const int pretty; /* How pretty is it? */
297 /* The three ways of getting then name of the current function. */
299 const struct fname_var_t fname_vars[] =
301 /* C99 compliant __func__, must be first. */
302 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
303 /* GCC __FUNCTION__ compliant. */
304 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
305 /* GCC __PRETTY_FUNCTION__ compliant. */
306 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
307 {NULL, 0, 0},
310 /* Global visibility options. */
311 struct visibility_flags visibility_options;
313 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
314 static tree check_case_value (tree);
315 static bool check_case_bounds (tree, tree, tree *, tree *);
317 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
324 int, bool *);
325 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
326 int, bool *);
327 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
328 bool *);
329 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_always_inline_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
341 bool *);
342 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_transparent_union_attribute (tree *, tree, tree,
344 int, bool *);
345 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
352 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
355 static tree handle_visibility_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_no_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371 static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
383 static tree ignore_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
389 bool *);
390 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
395 static void check_function_nonnull (tree, int, tree *);
396 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
397 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
398 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
399 static int resort_field_decl_cmp (const void *, const void *);
401 /* Reserved words. The third field is a mask: keywords are disabled
402 if they match the mask.
404 Masks for languages:
405 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
406 C --std=c99: D_CXXONLY | D_OBJC
407 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
408 UPC is like C except that D_UPC is not set
409 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
410 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
411 ObjC++ is like C++ except that D_OBJC is not set
413 If -fno-asm is used, D_ASM is added to the mask. If
414 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
415 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
416 In C with -Wc++-compat, we warn if D_CXXWARN is set.
418 Note the complication of the D_CXX_OBJC keywords. These are
419 reserved words such as 'class'. In C++, 'class' is a reserved
420 word. In Objective-C++ it is too. In Objective-C, it is a
421 reserved word too, but only if it follows an '@' sign.
423 const struct c_common_resword c_common_reswords[] =
425 { "_Alignas", RID_ALIGNAS, D_CONLY },
426 { "_Alignof", RID_ALIGNOF, D_CONLY },
427 { "_Atomic", RID_ATOMIC, D_CONLY },
428 { "_Bool", RID_BOOL, D_CONLY },
429 { "_Complex", RID_COMPLEX, 0 },
430 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
431 { "_Cilk_sync", RID_CILK_SYNC, 0 },
432 { "_Imaginary", RID_IMAGINARY, D_CONLY },
433 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
434 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
435 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
436 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
437 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
438 { "_Sat", RID_SAT, D_CONLY | D_EXT },
439 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
440 { "_Noreturn", RID_NORETURN, D_CONLY },
441 { "_Generic", RID_GENERIC, D_CONLY },
442 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
443 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
444 { "__alignof", RID_ALIGNOF, 0 },
445 { "__alignof__", RID_ALIGNOF, 0 },
446 { "__asm", RID_ASM, 0 },
447 { "__asm__", RID_ASM, 0 },
448 { "__attribute", RID_ATTRIBUTE, 0 },
449 { "__attribute__", RID_ATTRIBUTE, 0 },
450 { "__bases", RID_BASES, D_CXXONLY },
451 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
452 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
453 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
454 { "__builtin_offsetof", RID_OFFSETOF, 0 },
455 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
456 { "__builtin_va_arg", RID_VA_ARG, 0 },
457 { "__complex", RID_COMPLEX, 0 },
458 { "__complex__", RID_COMPLEX, 0 },
459 { "__const", RID_CONST, 0 },
460 { "__const__", RID_CONST, 0 },
461 { "__decltype", RID_DECLTYPE, D_CXXONLY },
462 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
463 { "__extension__", RID_EXTENSION, 0 },
464 { "__func__", RID_C99_FUNCTION_NAME, 0 },
465 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
466 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
467 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
468 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
469 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
470 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
471 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
472 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
473 { "__imag", RID_IMAGPART, 0 },
474 { "__imag__", RID_IMAGPART, 0 },
475 { "__inline", RID_INLINE, 0 },
476 { "__inline__", RID_INLINE, 0 },
477 { "__int128", RID_INT128, 0 },
478 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
479 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
480 { "__is_class", RID_IS_CLASS, D_CXXONLY },
481 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
482 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
483 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
484 { "__is_final", RID_IS_FINAL, D_CXXONLY },
485 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
486 { "__is_pod", RID_IS_POD, D_CXXONLY },
487 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
488 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
489 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
490 { "__is_union", RID_IS_UNION, D_CXXONLY },
491 { "__label__", RID_LABEL, 0 },
492 { "__null", RID_NULL, 0 },
493 { "__real", RID_REALPART, 0 },
494 { "__real__", RID_REALPART, 0 },
495 { "__restrict", RID_RESTRICT, 0 },
496 { "__restrict__", RID_RESTRICT, 0 },
497 { "__signed", RID_SIGNED, 0 },
498 { "__signed__", RID_SIGNED, 0 },
499 { "__thread", RID_THREAD, 0 },
500 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
501 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
502 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
503 { "__typeof", RID_TYPEOF, 0 },
504 { "__typeof__", RID_TYPEOF, 0 },
505 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
506 { "__volatile", RID_VOLATILE, 0 },
507 { "__volatile__", RID_VOLATILE, 0 },
508 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
510 { "asm", RID_ASM, D_ASM },
511 { "auto", RID_AUTO, 0 },
512 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
513 { "break", RID_BREAK, 0 },
514 { "case", RID_CASE, 0 },
515 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
516 { "char", RID_CHAR, 0 },
517 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
518 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
519 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
520 { "const", RID_CONST, 0 },
521 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
522 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
523 { "continue", RID_CONTINUE, 0 },
524 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
525 { "default", RID_DEFAULT, 0 },
526 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
527 { "do", RID_DO, 0 },
528 { "double", RID_DOUBLE, 0 },
529 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
530 { "else", RID_ELSE, 0 },
531 { "enum", RID_ENUM, 0 },
532 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
533 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
534 { "extern", RID_EXTERN, 0 },
535 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
536 { "float", RID_FLOAT, 0 },
537 { "for", RID_FOR, 0 },
538 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
539 { "goto", RID_GOTO, 0 },
540 { "if", RID_IF, 0 },
541 { "inline", RID_INLINE, D_EXT89 },
542 { "int", RID_INT, 0 },
543 { "long", RID_LONG, 0 },
544 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
545 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
546 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
547 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
548 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
549 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
550 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
551 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
552 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
553 { "register", RID_REGISTER, 0 },
554 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
555 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
556 { "return", RID_RETURN, 0 },
557 { "short", RID_SHORT, 0 },
558 { "signed", RID_SIGNED, 0 },
559 { "sizeof", RID_SIZEOF, 0 },
560 { "static", RID_STATIC, 0 },
561 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
562 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
563 { "struct", RID_STRUCT, 0 },
564 { "switch", RID_SWITCH, 0 },
565 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
566 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
567 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
568 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
569 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
570 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
571 { "typedef", RID_TYPEDEF, 0 },
572 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
573 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
574 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
575 { "union", RID_UNION, 0 },
576 { "unsigned", RID_UNSIGNED, 0 },
577 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
578 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
579 { "void", RID_VOID, 0 },
580 { "volatile", RID_VOLATILE, 0 },
581 { "wchar_t", RID_WCHAR, D_CXXONLY },
582 { "while", RID_WHILE, 0 },
583 /* These Objective-C keywords are recognized only immediately after
584 an '@'. */
585 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
586 { "defs", RID_AT_DEFS, D_OBJC },
587 { "encode", RID_AT_ENCODE, D_OBJC },
588 { "end", RID_AT_END, D_OBJC },
589 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
590 { "interface", RID_AT_INTERFACE, D_OBJC },
591 { "protocol", RID_AT_PROTOCOL, D_OBJC },
592 { "selector", RID_AT_SELECTOR, D_OBJC },
593 { "finally", RID_AT_FINALLY, D_OBJC },
594 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
595 { "optional", RID_AT_OPTIONAL, D_OBJC },
596 { "required", RID_AT_REQUIRED, D_OBJC },
597 { "property", RID_AT_PROPERTY, D_OBJC },
598 { "package", RID_AT_PACKAGE, D_OBJC },
599 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
600 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
601 /* These are recognized only in protocol-qualifier context
602 (see above) */
603 { "bycopy", RID_BYCOPY, D_OBJC },
604 { "byref", RID_BYREF, D_OBJC },
605 { "in", RID_IN, D_OBJC },
606 { "inout", RID_INOUT, D_OBJC },
607 { "oneway", RID_ONEWAY, D_OBJC },
608 { "out", RID_OUT, D_OBJC },
610 /* UPC keywords */
611 { "shared", RID_SHARED, D_UPC },
612 { "relaxed", RID_RELAXED, D_UPC },
613 { "strict", RID_STRICT, D_UPC },
614 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
615 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
616 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
617 { "upc_forall", RID_UPC_FORALL, D_UPC },
618 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
619 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
620 { "upc_wait", RID_UPC_WAIT, D_UPC },
622 /* These are recognized inside a property attribute list */
623 { "assign", RID_ASSIGN, D_OBJC },
624 { "copy", RID_COPY, D_OBJC },
625 { "getter", RID_GETTER, D_OBJC },
626 { "nonatomic", RID_NONATOMIC, D_OBJC },
627 { "readonly", RID_READONLY, D_OBJC },
628 { "readwrite", RID_READWRITE, D_OBJC },
629 { "retain", RID_RETAIN, D_OBJC },
630 { "setter", RID_SETTER, D_OBJC },
633 const unsigned int num_c_common_reswords =
634 sizeof c_common_reswords / sizeof (struct c_common_resword);
636 /* Table of machine-independent attributes common to all C-like languages. */
637 const struct attribute_spec c_common_attribute_table[] =
639 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
640 affects_type_identity } */
641 { "packed", 0, 0, false, false, false,
642 handle_packed_attribute , false},
643 { "nocommon", 0, 0, true, false, false,
644 handle_nocommon_attribute, false},
645 { "common", 0, 0, true, false, false,
646 handle_common_attribute, false },
647 /* FIXME: logically, noreturn attributes should be listed as
648 "false, true, true" and apply to function types. But implementing this
649 would require all the places in the compiler that use TREE_THIS_VOLATILE
650 on a decl to identify non-returning functions to be located and fixed
651 to check the function type instead. */
652 { "noreturn", 0, 0, true, false, false,
653 handle_noreturn_attribute, false },
654 { "volatile", 0, 0, true, false, false,
655 handle_noreturn_attribute, false },
656 { "noinline", 0, 0, true, false, false,
657 handle_noinline_attribute, false },
658 { "noclone", 0, 0, true, false, false,
659 handle_noclone_attribute, false },
660 { "leaf", 0, 0, true, false, false,
661 handle_leaf_attribute, false },
662 { "always_inline", 0, 0, true, false, false,
663 handle_always_inline_attribute, false },
664 { "gnu_inline", 0, 0, true, false, false,
665 handle_gnu_inline_attribute, false },
666 { "artificial", 0, 0, true, false, false,
667 handle_artificial_attribute, false },
668 { "flatten", 0, 0, true, false, false,
669 handle_flatten_attribute, false },
670 { "used", 0, 0, true, false, false,
671 handle_used_attribute, false },
672 { "unused", 0, 0, false, false, false,
673 handle_unused_attribute, false },
674 { "externally_visible", 0, 0, true, false, false,
675 handle_externally_visible_attribute, false },
676 /* The same comments as for noreturn attributes apply to const ones. */
677 { "const", 0, 0, true, false, false,
678 handle_const_attribute, false },
679 { "transparent_union", 0, 0, false, false, false,
680 handle_transparent_union_attribute, false },
681 { "constructor", 0, 1, true, false, false,
682 handle_constructor_attribute, false },
683 { "destructor", 0, 1, true, false, false,
684 handle_destructor_attribute, false },
685 { "mode", 1, 1, false, true, false,
686 handle_mode_attribute, false },
687 { "section", 1, 1, true, false, false,
688 handle_section_attribute, false },
689 { "aligned", 0, 1, false, false, false,
690 handle_aligned_attribute, false },
691 { "weak", 0, 0, true, false, false,
692 handle_weak_attribute, false },
693 { "ifunc", 1, 1, true, false, false,
694 handle_ifunc_attribute, false },
695 { "alias", 1, 1, true, false, false,
696 handle_alias_attribute, false },
697 { "weakref", 0, 1, true, false, false,
698 handle_weakref_attribute, false },
699 { "no_instrument_function", 0, 0, true, false, false,
700 handle_no_instrument_function_attribute,
701 false },
702 { "malloc", 0, 0, true, false, false,
703 handle_malloc_attribute, false },
704 { "returns_twice", 0, 0, true, false, false,
705 handle_returns_twice_attribute, false },
706 { "no_stack_limit", 0, 0, true, false, false,
707 handle_no_limit_stack_attribute, false },
708 { "pure", 0, 0, true, false, false,
709 handle_pure_attribute, false },
710 { "transaction_callable", 0, 0, false, true, false,
711 handle_tm_attribute, false },
712 { "transaction_unsafe", 0, 0, false, true, false,
713 handle_tm_attribute, false },
714 { "transaction_safe", 0, 0, false, true, false,
715 handle_tm_attribute, false },
716 { "transaction_may_cancel_outer", 0, 0, false, true, false,
717 handle_tm_attribute, false },
718 /* ??? These two attributes didn't make the transition from the
719 Intel language document to the multi-vendor language document. */
720 { "transaction_pure", 0, 0, false, true, false,
721 handle_tm_attribute, false },
722 { "transaction_wrap", 1, 1, true, false, false,
723 handle_tm_wrap_attribute, false },
724 /* For internal use (marking of builtins) only. The name contains space
725 to prevent its usage in source code. */
726 { "no vops", 0, 0, true, false, false,
727 handle_novops_attribute, false },
728 { "deprecated", 0, 1, false, false, false,
729 handle_deprecated_attribute, false },
730 { "vector_size", 1, 1, false, true, false,
731 handle_vector_size_attribute, false },
732 { "visibility", 1, 1, false, false, false,
733 handle_visibility_attribute, false },
734 { "tls_model", 1, 1, true, false, false,
735 handle_tls_model_attribute, false },
736 { "nonnull", 0, -1, false, true, true,
737 handle_nonnull_attribute, false },
738 { "nothrow", 0, 0, true, false, false,
739 handle_nothrow_attribute, false },
740 { "may_alias", 0, 0, false, true, false, NULL, false },
741 { "cleanup", 1, 1, true, false, false,
742 handle_cleanup_attribute, false },
743 { "warn_unused_result", 0, 0, false, true, true,
744 handle_warn_unused_result_attribute, false },
745 { "sentinel", 0, 1, false, true, true,
746 handle_sentinel_attribute, false },
747 /* For internal use (marking of builtins) only. The name contains space
748 to prevent its usage in source code. */
749 { "type generic", 0, 0, false, true, true,
750 handle_type_generic_attribute, false },
751 { "alloc_size", 1, 2, false, true, true,
752 handle_alloc_size_attribute, false },
753 { "cold", 0, 0, true, false, false,
754 handle_cold_attribute, false },
755 { "hot", 0, 0, true, false, false,
756 handle_hot_attribute, false },
757 { "no_address_safety_analysis",
758 0, 0, true, false, false,
759 handle_no_address_safety_analysis_attribute,
760 false },
761 { "no_sanitize_address", 0, 0, true, false, false,
762 handle_no_sanitize_address_attribute,
763 false },
764 { "no_sanitize_undefined", 0, 0, true, false, false,
765 handle_no_sanitize_undefined_attribute,
766 false },
767 { "warning", 1, 1, true, false, false,
768 handle_error_attribute, false },
769 { "error", 1, 1, true, false, false,
770 handle_error_attribute, false },
771 { "target", 1, -1, true, false, false,
772 handle_target_attribute, false },
773 { "optimize", 1, -1, true, false, false,
774 handle_optimize_attribute, false },
775 /* For internal use only. The leading '*' both prevents its usage in
776 source code and signals that it may be overridden by machine tables. */
777 { "*tm regparm", 0, 0, false, true, true,
778 ignore_attribute, false },
779 { "no_split_stack", 0, 0, true, false, false,
780 handle_no_split_stack_attribute, false },
781 /* For internal use (marking of builtins and runtime functions) only.
782 The name contains space to prevent its usage in source code. */
783 { "fn spec", 1, 1, false, true, true,
784 handle_fnspec_attribute, false },
785 { "warn_unused", 0, 0, false, false, false,
786 handle_warn_unused_attribute, false },
787 { "returns_nonnull", 0, 0, false, true, true,
788 handle_returns_nonnull_attribute, false },
789 { "omp declare simd", 0, -1, true, false, false,
790 handle_omp_declare_simd_attribute, false },
791 { "omp declare target", 0, 0, true, false, false,
792 handle_omp_declare_target_attribute, false },
793 { "bnd_variable_size", 0, 0, true, false, false,
794 handle_bnd_variable_size_attribute, false },
795 { "bnd_legacy", 0, 0, true, false, false,
796 handle_bnd_legacy, false },
797 { NULL, 0, 0, false, false, false, NULL, false }
800 /* Give the specifications for the format attributes, used by C and all
801 descendants. */
803 const struct attribute_spec c_common_format_attribute_table[] =
805 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
806 affects_type_identity } */
807 { "format", 3, 3, false, true, true,
808 handle_format_attribute, false },
809 { "format_arg", 1, 1, false, true, true,
810 handle_format_arg_attribute, false },
811 { NULL, 0, 0, false, false, false, NULL, false }
814 /* Return identifier for address space AS. */
816 const char *
817 c_addr_space_name (addr_space_t as)
819 int rid = RID_FIRST_ADDR_SPACE + as;
820 gcc_assert (ridpointers [rid]);
821 return IDENTIFIER_POINTER (ridpointers [rid]);
824 /* Push current bindings for the function name VAR_DECLS. */
826 void
827 start_fname_decls (void)
829 unsigned ix;
830 tree saved = NULL_TREE;
832 for (ix = 0; fname_vars[ix].decl; ix++)
834 tree decl = *fname_vars[ix].decl;
836 if (decl)
838 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
839 saved);
840 *fname_vars[ix].decl = NULL_TREE;
843 if (saved || saved_function_name_decls)
844 /* Normally they'll have been NULL, so only push if we've got a
845 stack, or they are non-NULL. */
846 saved_function_name_decls = tree_cons (saved, NULL_TREE,
847 saved_function_name_decls);
850 /* Finish up the current bindings, adding them into the current function's
851 statement tree. This must be done _before_ finish_stmt_tree is called.
852 If there is no current function, we must be at file scope and no statements
853 are involved. Pop the previous bindings. */
855 void
856 finish_fname_decls (void)
858 unsigned ix;
859 tree stmts = NULL_TREE;
860 tree stack = saved_function_name_decls;
862 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
863 append_to_statement_list (TREE_VALUE (stack), &stmts);
865 if (stmts)
867 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
869 if (TREE_CODE (*bodyp) == BIND_EXPR)
870 bodyp = &BIND_EXPR_BODY (*bodyp);
872 append_to_statement_list_force (*bodyp, &stmts);
873 *bodyp = stmts;
876 for (ix = 0; fname_vars[ix].decl; ix++)
877 *fname_vars[ix].decl = NULL_TREE;
879 if (stack)
881 /* We had saved values, restore them. */
882 tree saved;
884 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
886 tree decl = TREE_PURPOSE (saved);
887 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
889 *fname_vars[ix].decl = decl;
891 stack = TREE_CHAIN (stack);
893 saved_function_name_decls = stack;
896 /* Return the text name of the current function, suitably prettified
897 by PRETTY_P. Return string must be freed by caller. */
899 const char *
900 fname_as_string (int pretty_p)
902 const char *name = "top level";
903 char *namep;
904 int vrb = 2, len;
905 cpp_string cstr = { 0, 0 }, strname;
907 if (!pretty_p)
909 name = "";
910 vrb = 0;
913 if (current_function_decl)
914 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
916 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
918 namep = XNEWVEC (char, len);
919 snprintf (namep, len, "\"%s\"", name);
920 strname.text = (unsigned char *) namep;
921 strname.len = len - 1;
923 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
925 XDELETEVEC (namep);
926 return (const char *) cstr.text;
929 return namep;
932 /* Return the VAR_DECL for a const char array naming the current
933 function. If the VAR_DECL has not yet been created, create it
934 now. RID indicates how it should be formatted and IDENTIFIER_NODE
935 ID is its name (unfortunately C and C++ hold the RID values of
936 keywords in different places, so we can't derive RID from ID in
937 this language independent code. LOC is the location of the
938 function. */
940 tree
941 fname_decl (location_t loc, unsigned int rid, tree id)
943 unsigned ix;
944 tree decl = NULL_TREE;
946 for (ix = 0; fname_vars[ix].decl; ix++)
947 if (fname_vars[ix].rid == rid)
948 break;
950 decl = *fname_vars[ix].decl;
951 if (!decl)
953 /* If a tree is built here, it would normally have the lineno of
954 the current statement. Later this tree will be moved to the
955 beginning of the function and this line number will be wrong.
956 To avoid this problem set the lineno to 0 here; that prevents
957 it from appearing in the RTL. */
958 tree stmts;
959 location_t saved_location = input_location;
960 input_location = UNKNOWN_LOCATION;
962 stmts = push_stmt_list ();
963 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
964 stmts = pop_stmt_list (stmts);
965 if (!IS_EMPTY_STMT (stmts))
966 saved_function_name_decls
967 = tree_cons (decl, stmts, saved_function_name_decls);
968 *fname_vars[ix].decl = decl;
969 input_location = saved_location;
971 if (!ix && !current_function_decl)
972 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
974 return decl;
977 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
979 tree
980 fix_string_type (tree value)
982 int length = TREE_STRING_LENGTH (value);
983 int nchars;
984 tree e_type, i_type, a_type;
986 /* Compute the number of elements, for the array type. */
987 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
989 nchars = length;
990 e_type = char_type_node;
992 else if (TREE_TYPE (value) == char16_array_type_node)
994 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
995 e_type = char16_type_node;
997 else if (TREE_TYPE (value) == char32_array_type_node)
999 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1000 e_type = char32_type_node;
1002 else
1004 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1005 e_type = wchar_type_node;
1008 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1009 limit in C++98 Annex B is very large (65536) and is not normative,
1010 so we do not diagnose it (warn_overlength_strings is forced off
1011 in c_common_post_options). */
1012 if (warn_overlength_strings)
1014 const int nchars_max = flag_isoc99 ? 4095 : 509;
1015 const int relevant_std = flag_isoc99 ? 99 : 90;
1016 if (nchars - 1 > nchars_max)
1017 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1018 separate the %d from the 'C'. 'ISO' should not be
1019 translated, but it may be moved after 'C%d' in languages
1020 where modifiers follow nouns. */
1021 pedwarn (input_location, OPT_Woverlength_strings,
1022 "string length %qd is greater than the length %qd "
1023 "ISO C%d compilers are required to support",
1024 nchars - 1, nchars_max, relevant_std);
1027 /* Create the array type for the string constant. The ISO C++
1028 standard says that a string literal has type `const char[N]' or
1029 `const wchar_t[N]'. We use the same logic when invoked as a C
1030 front-end with -Wwrite-strings.
1031 ??? We should change the type of an expression depending on the
1032 state of a warning flag. We should just be warning -- see how
1033 this is handled in the C++ front-end for the deprecated implicit
1034 conversion from string literals to `char*' or `wchar_t*'.
1036 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1037 array type being the unqualified version of that type.
1038 Therefore, if we are constructing an array of const char, we must
1039 construct the matching unqualified array type first. The C front
1040 end does not require this, but it does no harm, so we do it
1041 unconditionally. */
1042 i_type = build_index_type (size_int (nchars - 1));
1043 a_type = build_array_type (e_type, i_type);
1044 if (c_dialect_cxx() || warn_write_strings)
1045 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1047 TREE_TYPE (value) = a_type;
1048 TREE_CONSTANT (value) = 1;
1049 TREE_READONLY (value) = 1;
1050 TREE_STATIC (value) = 1;
1051 return value;
1054 /* If DISABLE is true, stop issuing warnings. This is used when
1055 parsing code that we know will not be executed. This function may
1056 be called multiple times, and works as a stack. */
1058 static void
1059 c_disable_warnings (bool disable)
1061 if (disable)
1063 ++c_inhibit_evaluation_warnings;
1064 fold_defer_overflow_warnings ();
1068 /* If ENABLE is true, reenable issuing warnings. */
1070 static void
1071 c_enable_warnings (bool enable)
1073 if (enable)
1075 --c_inhibit_evaluation_warnings;
1076 fold_undefer_and_ignore_overflow_warnings ();
1080 /* Fully fold EXPR, an expression that was not folded (beyond integer
1081 constant expressions and null pointer constants) when being built
1082 up. If IN_INIT, this is in a static initializer and certain
1083 changes are made to the folding done. Clear *MAYBE_CONST if
1084 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1085 expression because it contains an evaluated operator (in C99) or an
1086 operator outside of sizeof returning an integer constant (in C90)
1087 not permitted in constant expressions, or because it contains an
1088 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1089 set to true by callers before calling this function.) Return the
1090 folded expression. Function arguments have already been folded
1091 before calling this function, as have the contents of SAVE_EXPR,
1092 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1093 C_MAYBE_CONST_EXPR. */
1095 tree
1096 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1098 tree ret;
1099 tree eptype = NULL_TREE;
1100 bool dummy = true;
1101 bool maybe_const_itself = true;
1102 location_t loc = EXPR_LOCATION (expr);
1104 /* This function is not relevant to C++ because C++ folds while
1105 parsing, and may need changes to be correct for C++ when C++
1106 stops folding while parsing. */
1107 if (c_dialect_cxx ())
1108 gcc_unreachable ();
1110 if (!maybe_const)
1111 maybe_const = &dummy;
1112 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1114 eptype = TREE_TYPE (expr);
1115 expr = TREE_OPERAND (expr, 0);
1117 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1118 &maybe_const_itself);
1119 if (eptype)
1120 ret = fold_convert_loc (loc, eptype, ret);
1121 *maybe_const &= maybe_const_itself;
1122 return ret;
1125 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1126 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1127 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1128 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1129 both evaluated and unevaluated subexpressions while
1130 *MAYBE_CONST_ITSELF is carried from only evaluated
1131 subexpressions). */
1133 static tree
1134 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1135 bool *maybe_const_itself)
1137 tree ret = expr;
1138 enum tree_code code = TREE_CODE (expr);
1139 enum tree_code_class kind = TREE_CODE_CLASS (code);
1140 location_t loc = EXPR_LOCATION (expr);
1141 tree op0, op1, op2, op3;
1142 tree orig_op0, orig_op1, orig_op2;
1143 bool op0_const = true, op1_const = true, op2_const = true;
1144 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1145 bool nowarning = TREE_NO_WARNING (expr);
1146 bool unused_p;
1148 /* This function is not relevant to C++ because C++ folds while
1149 parsing, and may need changes to be correct for C++ when C++
1150 stops folding while parsing. */
1151 if (c_dialect_cxx ())
1152 gcc_unreachable ();
1154 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1155 anything else not counted as an expression cannot usefully be
1156 folded further at this point. */
1157 if (!IS_EXPR_CODE_CLASS (kind)
1158 || kind == tcc_statement
1159 || code == SAVE_EXPR)
1160 return expr;
1162 /* Operands of variable-length expressions (function calls) have
1163 already been folded, as have __builtin_* function calls, and such
1164 expressions cannot occur in constant expressions. */
1165 if (kind == tcc_vl_exp)
1167 *maybe_const_operands = false;
1168 ret = fold (expr);
1169 goto out;
1172 if (code == C_MAYBE_CONST_EXPR)
1174 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1175 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1176 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1177 *maybe_const_operands = false;
1178 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1179 *maybe_const_itself = false;
1180 if (pre && !in_init)
1181 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1182 else
1183 ret = inner;
1184 goto out;
1187 /* Assignment, increment, decrement, function call and comma
1188 operators, and statement expressions, cannot occur in constant
1189 expressions if evaluated / outside of sizeof. (Function calls
1190 were handled above, though VA_ARG_EXPR is treated like a function
1191 call here, and statement expressions are handled through
1192 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1193 switch (code)
1195 case MODIFY_EXPR:
1196 case PREDECREMENT_EXPR:
1197 case PREINCREMENT_EXPR:
1198 case POSTDECREMENT_EXPR:
1199 case POSTINCREMENT_EXPR:
1200 case COMPOUND_EXPR:
1201 *maybe_const_operands = false;
1202 break;
1204 case VA_ARG_EXPR:
1205 case TARGET_EXPR:
1206 case BIND_EXPR:
1207 case OBJ_TYPE_REF:
1208 *maybe_const_operands = false;
1209 ret = fold (expr);
1210 goto out;
1212 default:
1213 break;
1216 /* Fold individual tree codes as appropriate. */
1217 switch (code)
1219 case COMPOUND_LITERAL_EXPR:
1220 /* Any non-constancy will have been marked in a containing
1221 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1222 goto out;
1224 case COMPONENT_REF:
1225 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1226 op1 = TREE_OPERAND (expr, 1);
1227 op2 = TREE_OPERAND (expr, 2);
1228 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1229 maybe_const_itself);
1230 STRIP_TYPE_NOPS (op0);
1231 if (op0 != orig_op0)
1232 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1233 if (ret != expr)
1235 TREE_READONLY (ret) = TREE_READONLY (expr);
1236 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1238 goto out;
1240 case ARRAY_REF:
1241 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1242 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1243 op2 = TREE_OPERAND (expr, 2);
1244 op3 = TREE_OPERAND (expr, 3);
1245 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1246 maybe_const_itself);
1247 STRIP_TYPE_NOPS (op0);
1248 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1249 maybe_const_itself);
1250 STRIP_TYPE_NOPS (op1);
1251 op1 = decl_constant_value_for_optimization (op1);
1252 if (op0 != orig_op0 || op1 != orig_op1)
1253 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1254 if (ret != expr)
1256 TREE_READONLY (ret) = TREE_READONLY (expr);
1257 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1258 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1260 ret = fold (ret);
1261 goto out;
1263 case COMPOUND_EXPR:
1264 case MODIFY_EXPR:
1265 case PREDECREMENT_EXPR:
1266 case PREINCREMENT_EXPR:
1267 case POSTDECREMENT_EXPR:
1268 case POSTINCREMENT_EXPR:
1269 case PLUS_EXPR:
1270 case MINUS_EXPR:
1271 case MULT_EXPR:
1272 case POINTER_PLUS_EXPR:
1273 case TRUNC_DIV_EXPR:
1274 case CEIL_DIV_EXPR:
1275 case FLOOR_DIV_EXPR:
1276 case TRUNC_MOD_EXPR:
1277 case RDIV_EXPR:
1278 case EXACT_DIV_EXPR:
1279 case LSHIFT_EXPR:
1280 case RSHIFT_EXPR:
1281 case BIT_IOR_EXPR:
1282 case BIT_XOR_EXPR:
1283 case BIT_AND_EXPR:
1284 case LT_EXPR:
1285 case LE_EXPR:
1286 case GT_EXPR:
1287 case GE_EXPR:
1288 case EQ_EXPR:
1289 case NE_EXPR:
1290 case COMPLEX_EXPR:
1291 case TRUTH_AND_EXPR:
1292 case TRUTH_OR_EXPR:
1293 case TRUTH_XOR_EXPR:
1294 case UNORDERED_EXPR:
1295 case ORDERED_EXPR:
1296 case UNLT_EXPR:
1297 case UNLE_EXPR:
1298 case UNGT_EXPR:
1299 case UNGE_EXPR:
1300 case UNEQ_EXPR:
1301 /* Binary operations evaluating both arguments (increment and
1302 decrement are binary internally in GCC). */
1303 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1304 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1305 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1306 maybe_const_itself);
1307 STRIP_TYPE_NOPS (op0);
1308 if (code != MODIFY_EXPR
1309 && code != PREDECREMENT_EXPR
1310 && code != PREINCREMENT_EXPR
1311 && code != POSTDECREMENT_EXPR
1312 && code != POSTINCREMENT_EXPR)
1313 op0 = decl_constant_value_for_optimization (op0);
1314 /* The RHS of a MODIFY_EXPR was fully folded when building that
1315 expression for the sake of conversion warnings. */
1316 if (code != MODIFY_EXPR)
1317 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1318 maybe_const_itself);
1319 STRIP_TYPE_NOPS (op1);
1320 op1 = decl_constant_value_for_optimization (op1);
1321 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1322 ret = in_init
1323 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1324 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1325 else
1326 ret = fold (expr);
1327 if (TREE_OVERFLOW_P (ret)
1328 && !TREE_OVERFLOW_P (op0)
1329 && !TREE_OVERFLOW_P (op1))
1330 overflow_warning (EXPR_LOCATION (expr), ret);
1331 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1332 && TREE_CODE (orig_op1) != INTEGER_CST
1333 && TREE_CODE (op1) == INTEGER_CST
1334 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1335 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1336 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1337 && c_inhibit_evaluation_warnings == 0)
1339 if (tree_int_cst_sgn (op1) < 0)
1340 warning_at (loc, 0, (code == LSHIFT_EXPR
1341 ? G_("left shift count is negative")
1342 : G_("right shift count is negative")));
1343 else if (compare_tree_int (op1,
1344 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1345 >= 0)
1346 warning_at (loc, 0, (code == LSHIFT_EXPR
1347 ? G_("left shift count >= width of type")
1348 : G_("right shift count >= width of type")));
1350 goto out;
1352 case INDIRECT_REF:
1353 case FIX_TRUNC_EXPR:
1354 case FLOAT_EXPR:
1355 CASE_CONVERT:
1356 case VIEW_CONVERT_EXPR:
1357 case NON_LVALUE_EXPR:
1358 case NEGATE_EXPR:
1359 case BIT_NOT_EXPR:
1360 case TRUTH_NOT_EXPR:
1361 case ADDR_EXPR:
1362 case CONJ_EXPR:
1363 case REALPART_EXPR:
1364 case IMAGPART_EXPR:
1365 /* Unary operations. */
1366 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1367 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1368 maybe_const_itself);
1369 STRIP_TYPE_NOPS (op0);
1370 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1371 op0 = decl_constant_value_for_optimization (op0);
1372 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1373 not prepared to deal with them if they occur in initializers.
1374 Avoid attempts to fold references to UPC shared components
1375 due to the complexities of UPC pointer-to-shared arithmetic. */
1376 if (op0 != orig_op0
1377 && code == ADDR_EXPR
1378 && (op1 = get_base_address (op0)) != NULL_TREE
1379 && TREE_CODE (op1) == INDIRECT_REF
1380 && !upc_shared_type_p (TREE_TYPE (op1))
1381 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1382 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1383 else if (op0 != orig_op0 || in_init)
1384 ret = in_init
1385 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1386 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1387 else
1388 ret = fold (expr);
1389 if (code == INDIRECT_REF
1390 && ret != expr
1391 && TREE_CODE (ret) == INDIRECT_REF)
1393 TREE_READONLY (ret) = TREE_READONLY (expr);
1394 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1395 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1397 switch (code)
1399 case FIX_TRUNC_EXPR:
1400 case FLOAT_EXPR:
1401 CASE_CONVERT:
1402 /* Don't warn about explicit conversions. We will already
1403 have warned about suspect implicit conversions. */
1404 break;
1406 default:
1407 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1408 overflow_warning (EXPR_LOCATION (expr), ret);
1409 break;
1411 goto out;
1413 case TRUTH_ANDIF_EXPR:
1414 case TRUTH_ORIF_EXPR:
1415 /* Binary operations not necessarily evaluating both
1416 arguments. */
1417 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1418 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1419 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1420 STRIP_TYPE_NOPS (op0);
1422 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1423 ? truthvalue_false_node
1424 : truthvalue_true_node));
1425 c_disable_warnings (unused_p);
1426 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1427 STRIP_TYPE_NOPS (op1);
1428 c_enable_warnings (unused_p);
1430 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1431 ret = in_init
1432 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1433 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1434 else
1435 ret = fold (expr);
1436 *maybe_const_operands &= op0_const;
1437 *maybe_const_itself &= op0_const_self;
1438 if (!(flag_isoc99
1439 && op0_const
1440 && op0_const_self
1441 && (code == TRUTH_ANDIF_EXPR
1442 ? op0 == truthvalue_false_node
1443 : op0 == truthvalue_true_node)))
1444 *maybe_const_operands &= op1_const;
1445 if (!(op0_const
1446 && op0_const_self
1447 && (code == TRUTH_ANDIF_EXPR
1448 ? op0 == truthvalue_false_node
1449 : op0 == truthvalue_true_node)))
1450 *maybe_const_itself &= op1_const_self;
1451 goto out;
1453 case COND_EXPR:
1454 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1455 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1456 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1457 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1459 STRIP_TYPE_NOPS (op0);
1460 c_disable_warnings (op0 == truthvalue_false_node);
1461 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1462 STRIP_TYPE_NOPS (op1);
1463 c_enable_warnings (op0 == truthvalue_false_node);
1465 c_disable_warnings (op0 == truthvalue_true_node);
1466 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1467 STRIP_TYPE_NOPS (op2);
1468 c_enable_warnings (op0 == truthvalue_true_node);
1470 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1471 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1472 else
1473 ret = fold (expr);
1474 *maybe_const_operands &= op0_const;
1475 *maybe_const_itself &= op0_const_self;
1476 if (!(flag_isoc99
1477 && op0_const
1478 && op0_const_self
1479 && op0 == truthvalue_false_node))
1480 *maybe_const_operands &= op1_const;
1481 if (!(op0_const
1482 && op0_const_self
1483 && op0 == truthvalue_false_node))
1484 *maybe_const_itself &= op1_const_self;
1485 if (!(flag_isoc99
1486 && op0_const
1487 && op0_const_self
1488 && op0 == truthvalue_true_node))
1489 *maybe_const_operands &= op2_const;
1490 if (!(op0_const
1491 && op0_const_self
1492 && op0 == truthvalue_true_node))
1493 *maybe_const_itself &= op2_const_self;
1494 goto out;
1496 case EXCESS_PRECISION_EXPR:
1497 /* Each case where an operand with excess precision may be
1498 encountered must remove the EXCESS_PRECISION_EXPR around
1499 inner operands and possibly put one around the whole
1500 expression or possibly convert to the semantic type (which
1501 c_fully_fold does); we cannot tell at this stage which is
1502 appropriate in any particular case. */
1503 gcc_unreachable ();
1505 default:
1506 /* Various codes may appear through folding built-in functions
1507 and their arguments. */
1508 goto out;
1511 out:
1512 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1513 have been done by this point, so remove them again. */
1514 nowarning |= TREE_NO_WARNING (ret);
1515 STRIP_TYPE_NOPS (ret);
1516 if (nowarning && !TREE_NO_WARNING (ret))
1518 if (!CAN_HAVE_LOCATION_P (ret))
1519 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1520 TREE_NO_WARNING (ret) = 1;
1522 if (ret != expr)
1523 protected_set_expr_location (ret, loc);
1524 return ret;
1527 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1528 return EXP. Otherwise, return either EXP or its known constant
1529 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1530 Is the BLKmode test appropriate? */
1532 tree
1533 decl_constant_value_for_optimization (tree exp)
1535 tree ret;
1537 /* This function is only used by C, for c_fully_fold and other
1538 optimization, and may not be correct for C++. */
1539 if (c_dialect_cxx ())
1540 gcc_unreachable ();
1542 if (!optimize
1543 || TREE_CODE (exp) != VAR_DECL
1544 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1545 || DECL_MODE (exp) == BLKmode)
1546 return exp;
1548 ret = decl_constant_value (exp);
1549 /* Avoid unwanted tree sharing between the initializer and current
1550 function's body where the tree can be modified e.g. by the
1551 gimplifier. */
1552 if (ret != exp && TREE_STATIC (exp))
1553 ret = unshare_expr (ret);
1554 return ret;
1557 /* Print a warning if a constant expression had overflow in folding.
1558 Invoke this function on every expression that the language
1559 requires to be a constant expression.
1560 Note the ANSI C standard says it is erroneous for a
1561 constant expression to overflow. */
1563 void
1564 constant_expression_warning (tree value)
1566 if (warn_overflow && pedantic
1567 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1568 || TREE_CODE (value) == FIXED_CST
1569 || TREE_CODE (value) == VECTOR_CST
1570 || TREE_CODE (value) == COMPLEX_CST)
1571 && TREE_OVERFLOW (value))
1572 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1575 /* The same as above but print an unconditional error. */
1576 void
1577 constant_expression_error (tree value)
1579 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1580 || TREE_CODE (value) == FIXED_CST
1581 || TREE_CODE (value) == VECTOR_CST
1582 || TREE_CODE (value) == COMPLEX_CST)
1583 && TREE_OVERFLOW (value))
1584 error ("overflow in constant expression");
1587 /* Print a warning if an expression had overflow in folding and its
1588 operands hadn't.
1590 Invoke this function on every expression that
1591 (1) appears in the source code, and
1592 (2) is a constant expression that overflowed, and
1593 (3) is not already checked by convert_and_check;
1594 however, do not invoke this function on operands of explicit casts
1595 or when the expression is the result of an operator and any operand
1596 already overflowed. */
1598 void
1599 overflow_warning (location_t loc, tree value)
1601 if (c_inhibit_evaluation_warnings != 0)
1602 return;
1604 switch (TREE_CODE (value))
1606 case INTEGER_CST:
1607 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1608 break;
1610 case REAL_CST:
1611 warning_at (loc, OPT_Woverflow,
1612 "floating point overflow in expression");
1613 break;
1615 case FIXED_CST:
1616 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1617 break;
1619 case VECTOR_CST:
1620 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1621 break;
1623 case COMPLEX_CST:
1624 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1625 warning_at (loc, OPT_Woverflow,
1626 "complex integer overflow in expression");
1627 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1628 warning_at (loc, OPT_Woverflow,
1629 "complex floating point overflow in expression");
1630 break;
1632 default:
1633 break;
1637 /* Warn about uses of logical || / && operator in a context where it
1638 is likely that the bitwise equivalent was intended by the
1639 programmer. We have seen an expression in which CODE is a binary
1640 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1641 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1642 void
1643 warn_logical_operator (location_t location, enum tree_code code, tree type,
1644 enum tree_code code_left, tree op_left,
1645 enum tree_code ARG_UNUSED (code_right), tree op_right)
1647 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1648 int in0_p, in1_p, in_p;
1649 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1650 bool strict_overflow_p = false;
1652 if (code != TRUTH_ANDIF_EXPR
1653 && code != TRUTH_AND_EXPR
1654 && code != TRUTH_ORIF_EXPR
1655 && code != TRUTH_OR_EXPR)
1656 return;
1658 /* Warn if &&/|| are being used in a context where it is
1659 likely that the bitwise equivalent was intended by the
1660 programmer. That is, an expression such as op && MASK
1661 where op should not be any boolean expression, nor a
1662 constant, and mask seems to be a non-boolean integer constant. */
1663 if (!truth_value_p (code_left)
1664 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1665 && !CONSTANT_CLASS_P (op_left)
1666 && !TREE_NO_WARNING (op_left)
1667 && TREE_CODE (op_right) == INTEGER_CST
1668 && !integer_zerop (op_right)
1669 && !integer_onep (op_right))
1671 if (or_op)
1672 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1673 " applied to non-boolean constant");
1674 else
1675 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1676 " applied to non-boolean constant");
1677 TREE_NO_WARNING (op_left) = true;
1678 return;
1681 /* We do not warn for constants because they are typical of macro
1682 expansions that test for features. */
1683 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1684 return;
1686 /* This warning only makes sense with logical operands. */
1687 if (!(truth_value_p (TREE_CODE (op_left))
1688 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1689 || !(truth_value_p (TREE_CODE (op_right))
1690 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1691 return;
1694 /* We first test whether either side separately is trivially true
1695 (with OR) or trivially false (with AND). If so, do not warn.
1696 This is a common idiom for testing ranges of data types in
1697 portable code. */
1698 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1699 if (!lhs)
1700 return;
1701 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1702 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1704 /* If this is an OR operation, invert both sides; now, the result
1705 should be always false to get a warning. */
1706 if (or_op)
1707 in0_p = !in0_p;
1709 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1710 if (tem && integer_zerop (tem))
1711 return;
1713 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1714 if (!rhs)
1715 return;
1716 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1717 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1719 /* If this is an OR operation, invert both sides; now, the result
1720 should be always false to get a warning. */
1721 if (or_op)
1722 in1_p = !in1_p;
1724 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1725 if (tem && integer_zerop (tem))
1726 return;
1728 /* If both expressions have the same operand, if we can merge the
1729 ranges, and if the range test is always false, then warn. */
1730 if (operand_equal_p (lhs, rhs, 0)
1731 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1732 in1_p, low1, high1)
1733 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1734 type, lhs, in_p, low, high))
1735 && integer_zerop (tem))
1737 if (or_op)
1738 warning_at (location, OPT_Wlogical_op,
1739 "logical %<or%> "
1740 "of collectively exhaustive tests is always true");
1741 else
1742 warning_at (location, OPT_Wlogical_op,
1743 "logical %<and%> "
1744 "of mutually exclusive tests is always false");
1749 /* Warn if EXP contains any computations whose results are not used.
1750 Return true if a warning is printed; false otherwise. LOCUS is the
1751 (potential) location of the expression. */
1753 bool
1754 warn_if_unused_value (const_tree exp, location_t locus)
1756 restart:
1757 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1758 return false;
1760 /* Don't warn about void constructs. This includes casting to void,
1761 void function calls, and statement expressions with a final cast
1762 to void. */
1763 if (VOID_TYPE_P (TREE_TYPE (exp)))
1764 return false;
1766 if (EXPR_HAS_LOCATION (exp))
1767 locus = EXPR_LOCATION (exp);
1769 switch (TREE_CODE (exp))
1771 case PREINCREMENT_EXPR:
1772 case POSTINCREMENT_EXPR:
1773 case PREDECREMENT_EXPR:
1774 case POSTDECREMENT_EXPR:
1775 case MODIFY_EXPR:
1776 case INIT_EXPR:
1777 case TARGET_EXPR:
1778 case CALL_EXPR:
1779 case TRY_CATCH_EXPR:
1780 case WITH_CLEANUP_EXPR:
1781 case EXIT_EXPR:
1782 case VA_ARG_EXPR:
1783 return false;
1785 case BIND_EXPR:
1786 /* For a binding, warn if no side effect within it. */
1787 exp = BIND_EXPR_BODY (exp);
1788 goto restart;
1790 case SAVE_EXPR:
1791 case NON_LVALUE_EXPR:
1792 case NOP_EXPR:
1793 exp = TREE_OPERAND (exp, 0);
1794 goto restart;
1796 case TRUTH_ORIF_EXPR:
1797 case TRUTH_ANDIF_EXPR:
1798 /* In && or ||, warn if 2nd operand has no side effect. */
1799 exp = TREE_OPERAND (exp, 1);
1800 goto restart;
1802 case COMPOUND_EXPR:
1803 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1804 return true;
1805 /* Let people do `(foo (), 0)' without a warning. */
1806 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1807 return false;
1808 exp = TREE_OPERAND (exp, 1);
1809 goto restart;
1811 case COND_EXPR:
1812 /* If this is an expression with side effects, don't warn; this
1813 case commonly appears in macro expansions. */
1814 if (TREE_SIDE_EFFECTS (exp))
1815 return false;
1816 goto warn;
1818 case INDIRECT_REF:
1819 /* Don't warn about automatic dereferencing of references, since
1820 the user cannot control it. */
1821 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1823 exp = TREE_OPERAND (exp, 0);
1824 goto restart;
1826 /* Fall through. */
1828 default:
1829 /* Referencing a volatile value is a side effect, so don't warn. */
1830 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1831 && TREE_THIS_VOLATILE (exp))
1832 return false;
1834 /* If this is an expression which has no operands, there is no value
1835 to be unused. There are no such language-independent codes,
1836 but front ends may define such. */
1837 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1838 return false;
1840 warn:
1841 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1846 /* Print a warning about casts that might indicate violation
1847 of strict aliasing rules if -Wstrict-aliasing is used and
1848 strict aliasing mode is in effect. OTYPE is the original
1849 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1851 bool
1852 strict_aliasing_warning (tree otype, tree type, tree expr)
1854 /* Strip pointer conversion chains and get to the correct original type. */
1855 STRIP_NOPS (expr);
1856 otype = TREE_TYPE (expr);
1858 if (!(flag_strict_aliasing
1859 && POINTER_TYPE_P (type)
1860 && POINTER_TYPE_P (otype)
1861 && !VOID_TYPE_P (TREE_TYPE (type)))
1862 /* If the type we are casting to is a ref-all pointer
1863 dereferencing it is always valid. */
1864 || TYPE_REF_CAN_ALIAS_ALL (type))
1865 return false;
1867 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1868 && (DECL_P (TREE_OPERAND (expr, 0))
1869 || handled_component_p (TREE_OPERAND (expr, 0))))
1871 /* Casting the address of an object to non void pointer. Warn
1872 if the cast breaks type based aliasing. */
1873 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1875 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1876 "might break strict-aliasing rules");
1877 return true;
1879 else
1881 /* warn_strict_aliasing >= 3. This includes the default (3).
1882 Only warn if the cast is dereferenced immediately. */
1883 alias_set_type set1 =
1884 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1885 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1887 if (set1 != set2 && set2 != 0
1888 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1890 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1891 "pointer will break strict-aliasing rules");
1892 return true;
1894 else if (warn_strict_aliasing == 2
1895 && !alias_sets_must_conflict_p (set1, set2))
1897 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1898 "pointer might break strict-aliasing rules");
1899 return true;
1903 else
1904 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1906 /* At this level, warn for any conversions, even if an address is
1907 not taken in the same statement. This will likely produce many
1908 false positives, but could be useful to pinpoint problems that
1909 are not revealed at higher levels. */
1910 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1911 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1912 if (!COMPLETE_TYPE_P (type)
1913 || !alias_sets_must_conflict_p (set1, set2))
1915 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1916 "pointer might break strict-aliasing rules");
1917 return true;
1921 return false;
1924 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1925 sizeof as last operand of certain builtins. */
1927 void
1928 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1929 vec<tree, va_gc> *params, tree *sizeof_arg,
1930 bool (*comp_types) (tree, tree))
1932 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1933 bool strop = false, cmp = false;
1934 unsigned int idx = ~0;
1935 location_t loc;
1937 if (TREE_CODE (callee) != FUNCTION_DECL
1938 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1939 || vec_safe_length (params) <= 1)
1940 return;
1942 switch (DECL_FUNCTION_CODE (callee))
1944 case BUILT_IN_STRNCMP:
1945 case BUILT_IN_STRNCASECMP:
1946 cmp = true;
1947 /* FALLTHRU */
1948 case BUILT_IN_STRNCPY:
1949 case BUILT_IN_STRNCPY_CHK:
1950 case BUILT_IN_STRNCAT:
1951 case BUILT_IN_STRNCAT_CHK:
1952 case BUILT_IN_STPNCPY:
1953 case BUILT_IN_STPNCPY_CHK:
1954 strop = true;
1955 /* FALLTHRU */
1956 case BUILT_IN_MEMCPY:
1957 case BUILT_IN_MEMCPY_CHK:
1958 case BUILT_IN_MEMMOVE:
1959 case BUILT_IN_MEMMOVE_CHK:
1960 if (params->length () < 3)
1961 return;
1962 src = (*params)[1];
1963 dest = (*params)[0];
1964 idx = 2;
1965 break;
1966 case BUILT_IN_BCOPY:
1967 if (params->length () < 3)
1968 return;
1969 src = (*params)[0];
1970 dest = (*params)[1];
1971 idx = 2;
1972 break;
1973 case BUILT_IN_MEMCMP:
1974 case BUILT_IN_BCMP:
1975 if (params->length () < 3)
1976 return;
1977 src = (*params)[1];
1978 dest = (*params)[0];
1979 idx = 2;
1980 cmp = true;
1981 break;
1982 case BUILT_IN_MEMSET:
1983 case BUILT_IN_MEMSET_CHK:
1984 if (params->length () < 3)
1985 return;
1986 dest = (*params)[0];
1987 idx = 2;
1988 break;
1989 case BUILT_IN_BZERO:
1990 dest = (*params)[0];
1991 idx = 1;
1992 break;
1993 case BUILT_IN_STRNDUP:
1994 src = (*params)[0];
1995 strop = true;
1996 idx = 1;
1997 break;
1998 case BUILT_IN_MEMCHR:
1999 if (params->length () < 3)
2000 return;
2001 src = (*params)[0];
2002 idx = 2;
2003 break;
2004 case BUILT_IN_SNPRINTF:
2005 case BUILT_IN_SNPRINTF_CHK:
2006 case BUILT_IN_VSNPRINTF:
2007 case BUILT_IN_VSNPRINTF_CHK:
2008 dest = (*params)[0];
2009 idx = 1;
2010 strop = true;
2011 break;
2012 default:
2013 break;
2016 if (idx >= 3)
2017 return;
2019 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2020 return;
2022 type = TYPE_P (sizeof_arg[idx])
2023 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2024 if (!POINTER_TYPE_P (type))
2025 return;
2027 if (dest
2028 && (tem = tree_strip_nop_conversions (dest))
2029 && POINTER_TYPE_P (TREE_TYPE (tem))
2030 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2031 return;
2033 if (src
2034 && (tem = tree_strip_nop_conversions (src))
2035 && POINTER_TYPE_P (TREE_TYPE (tem))
2036 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2037 return;
2039 loc = sizeof_arg_loc[idx];
2041 if (dest && !cmp)
2043 if (!TYPE_P (sizeof_arg[idx])
2044 && operand_equal_p (dest, sizeof_arg[idx], 0)
2045 && comp_types (TREE_TYPE (dest), type))
2047 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2048 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2049 "argument to %<sizeof%> in %qD call is the same "
2050 "expression as the destination; did you mean to "
2051 "remove the addressof?", callee);
2052 else if ((TYPE_PRECISION (TREE_TYPE (type))
2053 == TYPE_PRECISION (char_type_node))
2054 || strop)
2055 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2056 "argument to %<sizeof%> in %qD call is the same "
2057 "expression as the destination; did you mean to "
2058 "provide an explicit length?", callee);
2059 else
2060 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2061 "argument to %<sizeof%> in %qD call is the same "
2062 "expression as the destination; did you mean to "
2063 "dereference it?", callee);
2064 return;
2067 if (POINTER_TYPE_P (TREE_TYPE (dest))
2068 && !strop
2069 && comp_types (TREE_TYPE (dest), type)
2070 && !VOID_TYPE_P (TREE_TYPE (type)))
2072 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2073 "argument to %<sizeof%> in %qD call is the same "
2074 "pointer type %qT as the destination; expected %qT "
2075 "or an explicit length", callee, TREE_TYPE (dest),
2076 TREE_TYPE (TREE_TYPE (dest)));
2077 return;
2081 if (src && !cmp)
2083 if (!TYPE_P (sizeof_arg[idx])
2084 && operand_equal_p (src, sizeof_arg[idx], 0)
2085 && comp_types (TREE_TYPE (src), type))
2087 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2088 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2089 "argument to %<sizeof%> in %qD call is the same "
2090 "expression as the source; did you mean to "
2091 "remove the addressof?", callee);
2092 else if ((TYPE_PRECISION (TREE_TYPE (type))
2093 == TYPE_PRECISION (char_type_node))
2094 || strop)
2095 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2096 "argument to %<sizeof%> in %qD call is the same "
2097 "expression as the source; did you mean to "
2098 "provide an explicit length?", callee);
2099 else
2100 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2101 "argument to %<sizeof%> in %qD call is the same "
2102 "expression as the source; did you mean to "
2103 "dereference it?", callee);
2104 return;
2107 if (POINTER_TYPE_P (TREE_TYPE (src))
2108 && !strop
2109 && comp_types (TREE_TYPE (src), type)
2110 && !VOID_TYPE_P (TREE_TYPE (type)))
2112 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2113 "argument to %<sizeof%> in %qD call is the same "
2114 "pointer type %qT as the source; expected %qT "
2115 "or an explicit length", callee, TREE_TYPE (src),
2116 TREE_TYPE (TREE_TYPE (src)));
2117 return;
2121 if (dest)
2123 if (!TYPE_P (sizeof_arg[idx])
2124 && operand_equal_p (dest, sizeof_arg[idx], 0)
2125 && comp_types (TREE_TYPE (dest), type))
2127 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2128 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2129 "argument to %<sizeof%> in %qD call is the same "
2130 "expression as the first source; did you mean to "
2131 "remove the addressof?", callee);
2132 else if ((TYPE_PRECISION (TREE_TYPE (type))
2133 == TYPE_PRECISION (char_type_node))
2134 || strop)
2135 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2136 "argument to %<sizeof%> in %qD call is the same "
2137 "expression as the first source; did you mean to "
2138 "provide an explicit length?", callee);
2139 else
2140 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2141 "argument to %<sizeof%> in %qD call is the same "
2142 "expression as the first source; did you mean to "
2143 "dereference it?", callee);
2144 return;
2147 if (POINTER_TYPE_P (TREE_TYPE (dest))
2148 && !strop
2149 && comp_types (TREE_TYPE (dest), type)
2150 && !VOID_TYPE_P (TREE_TYPE (type)))
2152 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2153 "argument to %<sizeof%> in %qD call is the same "
2154 "pointer type %qT as the first source; expected %qT "
2155 "or an explicit length", callee, TREE_TYPE (dest),
2156 TREE_TYPE (TREE_TYPE (dest)));
2157 return;
2161 if (src)
2163 if (!TYPE_P (sizeof_arg[idx])
2164 && operand_equal_p (src, sizeof_arg[idx], 0)
2165 && comp_types (TREE_TYPE (src), type))
2167 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2168 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2169 "argument to %<sizeof%> in %qD call is the same "
2170 "expression as the second source; did you mean to "
2171 "remove the addressof?", callee);
2172 else if ((TYPE_PRECISION (TREE_TYPE (type))
2173 == TYPE_PRECISION (char_type_node))
2174 || strop)
2175 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2176 "argument to %<sizeof%> in %qD call is the same "
2177 "expression as the second source; did you mean to "
2178 "provide an explicit length?", callee);
2179 else
2180 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2181 "argument to %<sizeof%> in %qD call is the same "
2182 "expression as the second source; did you mean to "
2183 "dereference it?", callee);
2184 return;
2187 if (POINTER_TYPE_P (TREE_TYPE (src))
2188 && !strop
2189 && comp_types (TREE_TYPE (src), type)
2190 && !VOID_TYPE_P (TREE_TYPE (type)))
2192 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2193 "argument to %<sizeof%> in %qD call is the same "
2194 "pointer type %qT as the second source; expected %qT "
2195 "or an explicit length", callee, TREE_TYPE (src),
2196 TREE_TYPE (TREE_TYPE (src)));
2197 return;
2203 /* Warn for unlikely, improbable, or stupid DECL declarations
2204 of `main'. */
2206 void
2207 check_main_parameter_types (tree decl)
2209 function_args_iterator iter;
2210 tree type;
2211 int argct = 0;
2213 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2215 /* XXX void_type_node belies the abstraction. */
2216 if (type == void_type_node || type == error_mark_node )
2217 break;
2219 ++argct;
2220 switch (argct)
2222 case 1:
2223 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2224 pedwarn (input_location, OPT_Wmain,
2225 "first argument of %q+D should be %<int%>", decl);
2226 break;
2228 case 2:
2229 if (TREE_CODE (type) != POINTER_TYPE
2230 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2231 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2232 != char_type_node))
2233 pedwarn (input_location, OPT_Wmain,
2234 "second argument of %q+D should be %<char **%>", decl);
2235 break;
2237 case 3:
2238 if (TREE_CODE (type) != POINTER_TYPE
2239 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2240 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2241 != char_type_node))
2242 pedwarn (input_location, OPT_Wmain,
2243 "third argument of %q+D should probably be "
2244 "%<char **%>", decl);
2245 break;
2249 /* It is intentional that this message does not mention the third
2250 argument because it's only mentioned in an appendix of the
2251 standard. */
2252 if (argct > 0 && (argct < 2 || argct > 3))
2253 pedwarn (input_location, OPT_Wmain,
2254 "%q+D takes only zero or two arguments", decl);
2257 /* vector_targets_convertible_p is used for vector pointer types. The
2258 callers perform various checks that the qualifiers are satisfactory,
2259 while OTOH vector_targets_convertible_p ignores the number of elements
2260 in the vectors. That's fine with vector pointers as we can consider,
2261 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2262 and that does not require and conversion of the pointer values.
2263 In contrast, vector_types_convertible_p and
2264 vector_types_compatible_elements_p are used for vector value types. */
2265 /* True if pointers to distinct types T1 and T2 can be converted to
2266 each other without an explicit cast. Only returns true for opaque
2267 vector types. */
2268 bool
2269 vector_targets_convertible_p (const_tree t1, const_tree t2)
2271 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2272 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2273 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2274 return true;
2276 return false;
2279 /* vector_types_convertible_p is used for vector value types.
2280 It could in principle call vector_targets_convertible_p as a subroutine,
2281 but then the check for vector type would be duplicated with its callers,
2282 and also the purpose of vector_targets_convertible_p would become
2283 muddled.
2284 Where vector_types_convertible_p returns true, a conversion might still be
2285 needed to make the types match.
2286 In contrast, vector_targets_convertible_p is used for vector pointer
2287 values, and vector_types_compatible_elements_p is used specifically
2288 in the context for binary operators, as a check if use is possible without
2289 conversion. */
2290 /* True if vector types T1 and T2 can be converted to each other
2291 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2292 can only be converted with -flax-vector-conversions yet that is not
2293 in effect, emit a note telling the user about that option if such
2294 a note has not previously been emitted. */
2295 bool
2296 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2298 static bool emitted_lax_note = false;
2299 bool convertible_lax;
2301 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2302 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2303 return true;
2305 convertible_lax =
2306 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2307 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2308 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2309 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2310 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2312 if (!convertible_lax || flag_lax_vector_conversions)
2313 return convertible_lax;
2315 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2316 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2317 return true;
2319 if (emit_lax_note && !emitted_lax_note)
2321 emitted_lax_note = true;
2322 inform (input_location, "use -flax-vector-conversions to permit "
2323 "conversions between vectors with differing "
2324 "element types or numbers of subparts");
2327 return false;
2330 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2331 and have vector types, V0 has the same type as V1, and the number of
2332 elements of V0, V1, MASK is the same.
2334 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2335 called with two arguments. In this case implementation passes the
2336 first argument twice in order to share the same tree code. This fact
2337 could enable the mask-values being twice the vector length. This is
2338 an implementation accident and this semantics is not guaranteed to
2339 the user. */
2340 tree
2341 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2342 bool complain)
2344 tree ret;
2345 bool wrap = true;
2346 bool maybe_const = false;
2347 bool two_arguments = false;
2349 if (v1 == NULL_TREE)
2351 two_arguments = true;
2352 v1 = v0;
2355 if (v0 == error_mark_node || v1 == error_mark_node
2356 || mask == error_mark_node)
2357 return error_mark_node;
2359 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2360 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2362 if (complain)
2363 error_at (loc, "__builtin_shuffle last argument must "
2364 "be an integer vector");
2365 return error_mark_node;
2368 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2369 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2371 if (complain)
2372 error_at (loc, "__builtin_shuffle arguments must be vectors");
2373 return error_mark_node;
2376 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2378 if (complain)
2379 error_at (loc, "__builtin_shuffle argument vectors must be of "
2380 "the same type");
2381 return error_mark_node;
2384 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2385 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2386 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2387 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2389 if (complain)
2390 error_at (loc, "__builtin_shuffle number of elements of the "
2391 "argument vector(s) and the mask vector should "
2392 "be the same");
2393 return error_mark_node;
2396 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2397 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2399 if (complain)
2400 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2401 "must have the same size as inner type of the mask");
2402 return error_mark_node;
2405 if (!c_dialect_cxx ())
2407 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2408 v0 = c_fully_fold (v0, false, &maybe_const);
2409 wrap &= maybe_const;
2411 if (two_arguments)
2412 v1 = v0 = save_expr (v0);
2413 else
2415 v1 = c_fully_fold (v1, false, &maybe_const);
2416 wrap &= maybe_const;
2419 mask = c_fully_fold (mask, false, &maybe_const);
2420 wrap &= maybe_const;
2422 else if (two_arguments)
2423 v1 = v0 = save_expr (v0);
2425 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2427 if (!c_dialect_cxx () && !wrap)
2428 ret = c_wrap_maybe_const (ret, true);
2430 return ret;
2433 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2434 to integral type. */
2436 static tree
2437 c_common_get_narrower (tree op, int *unsignedp_ptr)
2439 op = get_narrower (op, unsignedp_ptr);
2441 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2442 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2444 /* C++0x scoped enumerations don't implicitly convert to integral
2445 type; if we stripped an explicit conversion to a larger type we
2446 need to replace it so common_type will still work. */
2447 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2448 TYPE_UNSIGNED (TREE_TYPE (op)));
2449 op = fold_convert (type, op);
2451 return op;
2454 /* This is a helper function of build_binary_op.
2456 For certain operations if both args were extended from the same
2457 smaller type, do the arithmetic in that type and then extend.
2459 BITWISE indicates a bitwise operation.
2460 For them, this optimization is safe only if
2461 both args are zero-extended or both are sign-extended.
2462 Otherwise, we might change the result.
2463 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2464 but calculated in (unsigned short) it would be (unsigned short)-1.
2466 tree
2467 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2469 int unsigned0, unsigned1;
2470 tree arg0, arg1;
2471 int uns;
2472 tree type;
2474 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2475 excessive narrowing when we call get_narrower below. For
2476 example, suppose that OP0 is of unsigned int extended
2477 from signed char and that RESULT_TYPE is long long int.
2478 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2479 like
2481 (long long int) (unsigned int) signed_char
2483 which get_narrower would narrow down to
2485 (unsigned int) signed char
2487 If we do not cast OP0 first, get_narrower would return
2488 signed_char, which is inconsistent with the case of the
2489 explicit cast. */
2490 op0 = convert (result_type, op0);
2491 op1 = convert (result_type, op1);
2493 arg0 = c_common_get_narrower (op0, &unsigned0);
2494 arg1 = c_common_get_narrower (op1, &unsigned1);
2496 /* UNS is 1 if the operation to be done is an unsigned one. */
2497 uns = TYPE_UNSIGNED (result_type);
2499 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2500 but it *requires* conversion to FINAL_TYPE. */
2502 if ((TYPE_PRECISION (TREE_TYPE (op0))
2503 == TYPE_PRECISION (TREE_TYPE (arg0)))
2504 && TREE_TYPE (op0) != result_type)
2505 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2506 if ((TYPE_PRECISION (TREE_TYPE (op1))
2507 == TYPE_PRECISION (TREE_TYPE (arg1)))
2508 && TREE_TYPE (op1) != result_type)
2509 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2511 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2513 /* For bitwise operations, signedness of nominal type
2514 does not matter. Consider only how operands were extended. */
2515 if (bitwise)
2516 uns = unsigned0;
2518 /* Note that in all three cases below we refrain from optimizing
2519 an unsigned operation on sign-extended args.
2520 That would not be valid. */
2522 /* Both args variable: if both extended in same way
2523 from same width, do it in that width.
2524 Do it unsigned if args were zero-extended. */
2525 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2526 < TYPE_PRECISION (result_type))
2527 && (TYPE_PRECISION (TREE_TYPE (arg1))
2528 == TYPE_PRECISION (TREE_TYPE (arg0)))
2529 && unsigned0 == unsigned1
2530 && (unsigned0 || !uns))
2531 return c_common_signed_or_unsigned_type
2532 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2534 else if (TREE_CODE (arg0) == INTEGER_CST
2535 && (unsigned1 || !uns)
2536 && (TYPE_PRECISION (TREE_TYPE (arg1))
2537 < TYPE_PRECISION (result_type))
2538 && (type
2539 = c_common_signed_or_unsigned_type (unsigned1,
2540 TREE_TYPE (arg1)))
2541 && !POINTER_TYPE_P (type)
2542 && int_fits_type_p (arg0, type))
2543 return type;
2545 else if (TREE_CODE (arg1) == INTEGER_CST
2546 && (unsigned0 || !uns)
2547 && (TYPE_PRECISION (TREE_TYPE (arg0))
2548 < TYPE_PRECISION (result_type))
2549 && (type
2550 = c_common_signed_or_unsigned_type (unsigned0,
2551 TREE_TYPE (arg0)))
2552 && !POINTER_TYPE_P (type)
2553 && int_fits_type_p (arg1, type))
2554 return type;
2556 return result_type;
2559 /* Checks if expression EXPR of real/integer type cannot be converted
2560 to the real/integer type TYPE. Function returns true when:
2561 * EXPR is a constant which cannot be exactly converted to TYPE
2562 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2563 for EXPR type and TYPE being both integers or both real.
2564 * EXPR is not a constant of real type and TYPE is an integer.
2565 * EXPR is not a constant of integer type which cannot be
2566 exactly converted to real type.
2567 Function allows conversions between types of different signedness and
2568 does not return true in that case. Function can produce signedness
2569 warnings if PRODUCE_WARNS is true. */
2570 bool
2571 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2573 bool give_warning = false;
2574 tree expr_type = TREE_TYPE (expr);
2575 location_t loc = EXPR_LOC_OR_HERE (expr);
2577 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2579 /* Warn for real constant that is not an exact integer converted
2580 to integer type. */
2581 if (TREE_CODE (expr_type) == REAL_TYPE
2582 && TREE_CODE (type) == INTEGER_TYPE)
2584 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2585 give_warning = true;
2587 /* Warn for an integer constant that does not fit into integer type. */
2588 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2589 && TREE_CODE (type) == INTEGER_TYPE
2590 && !int_fits_type_p (expr, type))
2592 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2593 && tree_int_cst_sgn (expr) < 0)
2595 if (produce_warns)
2596 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2597 " implicitly converted to unsigned type");
2599 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2601 if (produce_warns)
2602 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2603 " constant value to negative integer");
2605 else
2606 give_warning = true;
2608 else if (TREE_CODE (type) == REAL_TYPE)
2610 /* Warn for an integer constant that does not fit into real type. */
2611 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2613 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2614 if (!exact_real_truncate (TYPE_MODE (type), &a))
2615 give_warning = true;
2617 /* Warn for a real constant that does not fit into a smaller
2618 real type. */
2619 else if (TREE_CODE (expr_type) == REAL_TYPE
2620 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2622 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2623 if (!exact_real_truncate (TYPE_MODE (type), &a))
2624 give_warning = true;
2628 else
2630 /* Warn for real types converted to integer types. */
2631 if (TREE_CODE (expr_type) == REAL_TYPE
2632 && TREE_CODE (type) == INTEGER_TYPE)
2633 give_warning = true;
2635 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2636 && TREE_CODE (type) == INTEGER_TYPE)
2638 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2639 expr = get_unwidened (expr, 0);
2640 expr_type = TREE_TYPE (expr);
2642 /* Don't warn for short y; short x = ((int)y & 0xff); */
2643 if (TREE_CODE (expr) == BIT_AND_EXPR
2644 || TREE_CODE (expr) == BIT_IOR_EXPR
2645 || TREE_CODE (expr) == BIT_XOR_EXPR)
2647 /* If both args were extended from a shortest type,
2648 use that type if that is safe. */
2649 expr_type = shorten_binary_op (expr_type,
2650 TREE_OPERAND (expr, 0),
2651 TREE_OPERAND (expr, 1),
2652 /* bitwise */1);
2654 if (TREE_CODE (expr) == BIT_AND_EXPR)
2656 tree op0 = TREE_OPERAND (expr, 0);
2657 tree op1 = TREE_OPERAND (expr, 1);
2658 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2659 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2661 /* If one of the operands is a non-negative constant
2662 that fits in the target type, then the type of the
2663 other operand does not matter. */
2664 if ((TREE_CODE (op0) == INTEGER_CST
2665 && int_fits_type_p (op0, c_common_signed_type (type))
2666 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2667 || (TREE_CODE (op1) == INTEGER_CST
2668 && int_fits_type_p (op1, c_common_signed_type (type))
2669 && int_fits_type_p (op1,
2670 c_common_unsigned_type (type))))
2671 return false;
2672 /* If constant is unsigned and fits in the target
2673 type, then the result will also fit. */
2674 else if ((TREE_CODE (op0) == INTEGER_CST
2675 && unsigned0
2676 && int_fits_type_p (op0, type))
2677 || (TREE_CODE (op1) == INTEGER_CST
2678 && unsigned1
2679 && int_fits_type_p (op1, type)))
2680 return false;
2683 /* Warn for integer types converted to smaller integer types. */
2684 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2685 give_warning = true;
2687 /* When they are the same width but different signedness,
2688 then the value may change. */
2689 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2690 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2691 /* Even when converted to a bigger type, if the type is
2692 unsigned but expr is signed, then negative values
2693 will be changed. */
2694 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2695 && produce_warns)
2696 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2697 "may change the sign of the result",
2698 type, expr_type);
2701 /* Warn for integer types converted to real types if and only if
2702 all the range of values of the integer type cannot be
2703 represented by the real type. */
2704 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2705 && TREE_CODE (type) == REAL_TYPE)
2707 tree type_low_bound, type_high_bound;
2708 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2710 /* Don't warn about char y = 0xff; float x = (int) y; */
2711 expr = get_unwidened (expr, 0);
2712 expr_type = TREE_TYPE (expr);
2714 type_low_bound = TYPE_MIN_VALUE (expr_type);
2715 type_high_bound = TYPE_MAX_VALUE (expr_type);
2716 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2717 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2719 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2720 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2721 give_warning = true;
2724 /* Warn for real types converted to smaller real types. */
2725 else if (TREE_CODE (expr_type) == REAL_TYPE
2726 && TREE_CODE (type) == REAL_TYPE
2727 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2728 give_warning = true;
2731 return give_warning;
2734 /* Warns if the conversion of EXPR to TYPE may alter a value.
2735 This is a helper function for warnings_for_convert_and_check. */
2737 static void
2738 conversion_warning (tree type, tree expr)
2740 tree expr_type = TREE_TYPE (expr);
2741 location_t loc = EXPR_LOC_OR_HERE (expr);
2743 if (!warn_conversion && !warn_sign_conversion)
2744 return;
2746 switch (TREE_CODE (expr))
2748 case EQ_EXPR:
2749 case NE_EXPR:
2750 case LE_EXPR:
2751 case GE_EXPR:
2752 case LT_EXPR:
2753 case GT_EXPR:
2754 case TRUTH_ANDIF_EXPR:
2755 case TRUTH_ORIF_EXPR:
2756 case TRUTH_AND_EXPR:
2757 case TRUTH_OR_EXPR:
2758 case TRUTH_XOR_EXPR:
2759 case TRUTH_NOT_EXPR:
2760 /* Conversion from boolean to a signed:1 bit-field (which only
2761 can hold the values 0 and -1) doesn't lose information - but
2762 it does change the value. */
2763 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2764 warning_at (loc, OPT_Wconversion,
2765 "conversion to %qT from boolean expression", type);
2766 return;
2768 case REAL_CST:
2769 case INTEGER_CST:
2770 if (unsafe_conversion_p (type, expr, true))
2771 warning_at (loc, OPT_Wconversion,
2772 "conversion to %qT alters %qT constant value",
2773 type, expr_type);
2774 return;
2776 case COND_EXPR:
2778 /* In case of COND_EXPR, we do not care about the type of
2779 COND_EXPR, only about the conversion of each operand. */
2780 tree op1 = TREE_OPERAND (expr, 1);
2781 tree op2 = TREE_OPERAND (expr, 2);
2783 conversion_warning (type, op1);
2784 conversion_warning (type, op2);
2785 return;
2788 default: /* 'expr' is not a constant. */
2789 if (unsafe_conversion_p (type, expr, true))
2790 warning_at (loc, OPT_Wconversion,
2791 "conversion to %qT from %qT may alter its value",
2792 type, expr_type);
2796 /* Produce warnings after a conversion. RESULT is the result of
2797 converting EXPR to TYPE. This is a helper function for
2798 convert_and_check and cp_convert_and_check. */
2800 void
2801 warnings_for_convert_and_check (tree type, tree expr, tree result)
2803 location_t loc = EXPR_LOC_OR_HERE (expr);
2805 if (TREE_CODE (expr) == INTEGER_CST
2806 && (TREE_CODE (type) == INTEGER_TYPE
2807 || TREE_CODE (type) == ENUMERAL_TYPE)
2808 && !int_fits_type_p (expr, type))
2810 /* Do not diagnose overflow in a constant expression merely
2811 because a conversion overflowed. */
2812 if (TREE_OVERFLOW (result))
2813 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2815 if (TYPE_UNSIGNED (type))
2817 /* This detects cases like converting -129 or 256 to
2818 unsigned char. */
2819 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2820 warning_at (loc, OPT_Woverflow,
2821 "large integer implicitly truncated to unsigned type");
2822 else
2823 conversion_warning (type, expr);
2825 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2826 warning (OPT_Woverflow,
2827 "overflow in implicit constant conversion");
2828 /* No warning for converting 0x80000000 to int. */
2829 else if (pedantic
2830 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2831 || TYPE_PRECISION (TREE_TYPE (expr))
2832 != TYPE_PRECISION (type)))
2833 warning_at (loc, OPT_Woverflow,
2834 "overflow in implicit constant conversion");
2836 else
2837 conversion_warning (type, expr);
2839 else if ((TREE_CODE (result) == INTEGER_CST
2840 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2841 warning_at (loc, OPT_Woverflow,
2842 "overflow in implicit constant conversion");
2843 else
2844 conversion_warning (type, expr);
2848 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2849 Invoke this function on every expression that is converted implicitly,
2850 i.e. because of language rules and not because of an explicit cast. */
2852 tree
2853 convert_and_check (tree type, tree expr)
2855 tree result;
2856 tree expr_for_warning;
2858 /* Convert from a value with possible excess precision rather than
2859 via the semantic type, but do not warn about values not fitting
2860 exactly in the semantic type. */
2861 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2863 tree orig_type = TREE_TYPE (expr);
2864 expr = TREE_OPERAND (expr, 0);
2865 expr_for_warning = convert (orig_type, expr);
2866 if (orig_type == type)
2867 return expr_for_warning;
2869 else
2870 expr_for_warning = expr;
2872 if (TREE_TYPE (expr) == type)
2873 return expr;
2875 result = convert (type, expr);
2877 if (c_inhibit_evaluation_warnings == 0
2878 && !TREE_OVERFLOW_P (expr)
2879 && result != error_mark_node)
2880 warnings_for_convert_and_check (type, expr_for_warning, result);
2882 return result;
2885 /* A node in a list that describes references to variables (EXPR), which are
2886 either read accesses if WRITER is zero, or write accesses, in which case
2887 WRITER is the parent of EXPR. */
2888 struct tlist
2890 struct tlist *next;
2891 tree expr, writer;
2894 /* Used to implement a cache the results of a call to verify_tree. We only
2895 use this for SAVE_EXPRs. */
2896 struct tlist_cache
2898 struct tlist_cache *next;
2899 struct tlist *cache_before_sp;
2900 struct tlist *cache_after_sp;
2901 tree expr;
2904 /* Obstack to use when allocating tlist structures, and corresponding
2905 firstobj. */
2906 static struct obstack tlist_obstack;
2907 static char *tlist_firstobj = 0;
2909 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2910 warnings. */
2911 static struct tlist *warned_ids;
2912 /* SAVE_EXPRs need special treatment. We process them only once and then
2913 cache the results. */
2914 static struct tlist_cache *save_expr_cache;
2916 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2917 static void merge_tlist (struct tlist **, struct tlist *, int);
2918 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2919 static int warning_candidate_p (tree);
2920 static bool candidate_equal_p (const_tree, const_tree);
2921 static void warn_for_collisions (struct tlist *);
2922 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2923 static struct tlist *new_tlist (struct tlist *, tree, tree);
2925 /* Create a new struct tlist and fill in its fields. */
2926 static struct tlist *
2927 new_tlist (struct tlist *next, tree t, tree writer)
2929 struct tlist *l;
2930 l = XOBNEW (&tlist_obstack, struct tlist);
2931 l->next = next;
2932 l->expr = t;
2933 l->writer = writer;
2934 return l;
2937 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2938 is nonnull, we ignore any node we find which has a writer equal to it. */
2940 static void
2941 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2943 while (add)
2945 struct tlist *next = add->next;
2946 if (!copy)
2947 add->next = *to;
2948 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2949 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2950 add = next;
2954 /* Merge the nodes of ADD into TO. This merging process is done so that for
2955 each variable that already exists in TO, no new node is added; however if
2956 there is a write access recorded in ADD, and an occurrence on TO is only
2957 a read access, then the occurrence in TO will be modified to record the
2958 write. */
2960 static void
2961 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2963 struct tlist **end = to;
2965 while (*end)
2966 end = &(*end)->next;
2968 while (add)
2970 int found = 0;
2971 struct tlist *tmp2;
2972 struct tlist *next = add->next;
2974 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2975 if (candidate_equal_p (tmp2->expr, add->expr))
2977 found = 1;
2978 if (!tmp2->writer)
2979 tmp2->writer = add->writer;
2981 if (!found)
2983 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2984 end = &(*end)->next;
2985 *end = 0;
2987 add = next;
2991 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2992 references in list LIST conflict with it, excluding reads if ONLY writers
2993 is nonzero. */
2995 static void
2996 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2997 int only_writes)
2999 struct tlist *tmp;
3001 /* Avoid duplicate warnings. */
3002 for (tmp = warned_ids; tmp; tmp = tmp->next)
3003 if (candidate_equal_p (tmp->expr, written))
3004 return;
3006 while (list)
3008 if (candidate_equal_p (list->expr, written)
3009 && !candidate_equal_p (list->writer, writer)
3010 && (!only_writes || list->writer))
3012 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3013 warning_at (EXPR_LOC_OR_HERE (writer),
3014 OPT_Wsequence_point, "operation on %qE may be undefined",
3015 list->expr);
3017 list = list->next;
3021 /* Given a list LIST of references to variables, find whether any of these
3022 can cause conflicts due to missing sequence points. */
3024 static void
3025 warn_for_collisions (struct tlist *list)
3027 struct tlist *tmp;
3029 for (tmp = list; tmp; tmp = tmp->next)
3031 if (tmp->writer)
3032 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3036 /* Return nonzero if X is a tree that can be verified by the sequence point
3037 warnings. */
3038 static int
3039 warning_candidate_p (tree x)
3041 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3042 return 0;
3044 if (TREE_CODE (x) == BLOCK)
3045 return 0;
3047 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3048 (lvalue_p) crash on TRY/CATCH. */
3049 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3050 return 0;
3052 if (!lvalue_p (x))
3053 return 0;
3055 /* No point to track non-const calls, they will never satisfy
3056 operand_equal_p. */
3057 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3058 return 0;
3060 if (TREE_CODE (x) == STRING_CST)
3061 return 0;
3063 return 1;
3066 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3067 static bool
3068 candidate_equal_p (const_tree x, const_tree y)
3070 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3073 /* Walk the tree X, and record accesses to variables. If X is written by the
3074 parent tree, WRITER is the parent.
3075 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3076 expression or its only operand forces a sequence point, then everything up
3077 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3078 in PNO_SP.
3079 Once we return, we will have emitted warnings if any subexpression before
3080 such a sequence point could be undefined. On a higher level, however, the
3081 sequence point may not be relevant, and we'll merge the two lists.
3083 Example: (b++, a) + b;
3084 The call that processes the COMPOUND_EXPR will store the increment of B
3085 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3086 processes the PLUS_EXPR will need to merge the two lists so that
3087 eventually, all accesses end up on the same list (and we'll warn about the
3088 unordered subexpressions b++ and b.
3090 A note on merging. If we modify the former example so that our expression
3091 becomes
3092 (b++, b) + a
3093 care must be taken not simply to add all three expressions into the final
3094 PNO_SP list. The function merge_tlist takes care of that by merging the
3095 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3096 way, so that no more than one access to B is recorded. */
3098 static void
3099 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3100 tree writer)
3102 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3103 enum tree_code code;
3104 enum tree_code_class cl;
3106 /* X may be NULL if it is the operand of an empty statement expression
3107 ({ }). */
3108 if (x == NULL)
3109 return;
3111 restart:
3112 code = TREE_CODE (x);
3113 cl = TREE_CODE_CLASS (code);
3115 if (warning_candidate_p (x))
3116 *pno_sp = new_tlist (*pno_sp, x, writer);
3118 switch (code)
3120 case CONSTRUCTOR:
3121 case SIZEOF_EXPR:
3122 return;
3124 case COMPOUND_EXPR:
3125 case TRUTH_ANDIF_EXPR:
3126 case TRUTH_ORIF_EXPR:
3127 tmp_before = tmp_nosp = tmp_list3 = 0;
3128 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3129 warn_for_collisions (tmp_nosp);
3130 merge_tlist (pbefore_sp, tmp_before, 0);
3131 merge_tlist (pbefore_sp, tmp_nosp, 0);
3132 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3133 merge_tlist (pbefore_sp, tmp_list3, 0);
3134 return;
3136 case COND_EXPR:
3137 tmp_before = tmp_list2 = 0;
3138 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3139 warn_for_collisions (tmp_list2);
3140 merge_tlist (pbefore_sp, tmp_before, 0);
3141 merge_tlist (pbefore_sp, tmp_list2, 1);
3143 tmp_list3 = tmp_nosp = 0;
3144 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3145 warn_for_collisions (tmp_nosp);
3146 merge_tlist (pbefore_sp, tmp_list3, 0);
3148 tmp_list3 = tmp_list2 = 0;
3149 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3150 warn_for_collisions (tmp_list2);
3151 merge_tlist (pbefore_sp, tmp_list3, 0);
3152 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3153 two first, to avoid warning for (a ? b++ : b++). */
3154 merge_tlist (&tmp_nosp, tmp_list2, 0);
3155 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3156 return;
3158 case PREDECREMENT_EXPR:
3159 case PREINCREMENT_EXPR:
3160 case POSTDECREMENT_EXPR:
3161 case POSTINCREMENT_EXPR:
3162 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3163 return;
3165 case MODIFY_EXPR:
3166 tmp_before = tmp_nosp = tmp_list3 = 0;
3167 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3168 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3169 /* Expressions inside the LHS are not ordered wrt. the sequence points
3170 in the RHS. Example:
3171 *a = (a++, 2)
3172 Despite the fact that the modification of "a" is in the before_sp
3173 list (tmp_before), it conflicts with the use of "a" in the LHS.
3174 We can handle this by adding the contents of tmp_list3
3175 to those of tmp_before, and redoing the collision warnings for that
3176 list. */
3177 add_tlist (&tmp_before, tmp_list3, x, 1);
3178 warn_for_collisions (tmp_before);
3179 /* Exclude the LHS itself here; we first have to merge it into the
3180 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3181 didn't exclude the LHS, we'd get it twice, once as a read and once
3182 as a write. */
3183 add_tlist (pno_sp, tmp_list3, x, 0);
3184 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3186 merge_tlist (pbefore_sp, tmp_before, 0);
3187 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3188 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3189 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3190 return;
3192 case CALL_EXPR:
3193 /* We need to warn about conflicts among arguments and conflicts between
3194 args and the function address. Side effects of the function address,
3195 however, are not ordered by the sequence point of the call. */
3197 call_expr_arg_iterator iter;
3198 tree arg;
3199 tmp_before = tmp_nosp = 0;
3200 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3201 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3203 tmp_list2 = tmp_list3 = 0;
3204 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3205 merge_tlist (&tmp_list3, tmp_list2, 0);
3206 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3208 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3209 warn_for_collisions (tmp_before);
3210 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3211 return;
3214 case TREE_LIST:
3215 /* Scan all the list, e.g. indices of multi dimensional array. */
3216 while (x)
3218 tmp_before = tmp_nosp = 0;
3219 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3220 merge_tlist (&tmp_nosp, tmp_before, 0);
3221 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3222 x = TREE_CHAIN (x);
3224 return;
3226 case SAVE_EXPR:
3228 struct tlist_cache *t;
3229 for (t = save_expr_cache; t; t = t->next)
3230 if (candidate_equal_p (t->expr, x))
3231 break;
3233 if (!t)
3235 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3236 t->next = save_expr_cache;
3237 t->expr = x;
3238 save_expr_cache = t;
3240 tmp_before = tmp_nosp = 0;
3241 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3242 warn_for_collisions (tmp_nosp);
3244 tmp_list3 = 0;
3245 while (tmp_nosp)
3247 struct tlist *t = tmp_nosp;
3248 tmp_nosp = t->next;
3249 merge_tlist (&tmp_list3, t, 0);
3251 t->cache_before_sp = tmp_before;
3252 t->cache_after_sp = tmp_list3;
3254 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3255 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3256 return;
3259 case ADDR_EXPR:
3260 x = TREE_OPERAND (x, 0);
3261 if (DECL_P (x))
3262 return;
3263 writer = 0;
3264 goto restart;
3266 default:
3267 /* For other expressions, simply recurse on their operands.
3268 Manual tail recursion for unary expressions.
3269 Other non-expressions need not be processed. */
3270 if (cl == tcc_unary)
3272 x = TREE_OPERAND (x, 0);
3273 writer = 0;
3274 goto restart;
3276 else if (IS_EXPR_CODE_CLASS (cl))
3278 int lp;
3279 int max = TREE_OPERAND_LENGTH (x);
3280 for (lp = 0; lp < max; lp++)
3282 tmp_before = tmp_nosp = 0;
3283 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3284 merge_tlist (&tmp_nosp, tmp_before, 0);
3285 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3288 return;
3292 /* Try to warn for undefined behavior in EXPR due to missing sequence
3293 points. */
3295 DEBUG_FUNCTION void
3296 verify_sequence_points (tree expr)
3298 struct tlist *before_sp = 0, *after_sp = 0;
3300 warned_ids = 0;
3301 save_expr_cache = 0;
3302 if (tlist_firstobj == 0)
3304 gcc_obstack_init (&tlist_obstack);
3305 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3308 verify_tree (expr, &before_sp, &after_sp, 0);
3309 warn_for_collisions (after_sp);
3310 obstack_free (&tlist_obstack, tlist_firstobj);
3313 /* Validate the expression after `case' and apply default promotions. */
3315 static tree
3316 check_case_value (tree value)
3318 if (value == NULL_TREE)
3319 return value;
3321 if (TREE_CODE (value) == INTEGER_CST)
3322 /* Promote char or short to int. */
3323 value = perform_integral_promotions (value);
3324 else if (value != error_mark_node)
3326 error ("case label does not reduce to an integer constant");
3327 value = error_mark_node;
3330 constant_expression_warning (value);
3332 return value;
3335 /* See if the case values LOW and HIGH are in the range of the original
3336 type (i.e. before the default conversion to int) of the switch testing
3337 expression.
3338 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3339 the type before promoting it. CASE_LOW_P is a pointer to the lower
3340 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3341 if the case is not a case range.
3342 The caller has to make sure that we are not called with NULL for
3343 CASE_LOW_P (i.e. the default case).
3344 Returns true if the case label is in range of ORIG_TYPE (saturated or
3345 untouched) or false if the label is out of range. */
3347 static bool
3348 check_case_bounds (tree type, tree orig_type,
3349 tree *case_low_p, tree *case_high_p)
3351 tree min_value, max_value;
3352 tree case_low = *case_low_p;
3353 tree case_high = case_high_p ? *case_high_p : case_low;
3355 /* If there was a problem with the original type, do nothing. */
3356 if (orig_type == error_mark_node)
3357 return true;
3359 min_value = TYPE_MIN_VALUE (orig_type);
3360 max_value = TYPE_MAX_VALUE (orig_type);
3362 /* Case label is less than minimum for type. */
3363 if (tree_int_cst_compare (case_low, min_value) < 0
3364 && tree_int_cst_compare (case_high, min_value) < 0)
3366 warning (0, "case label value is less than minimum value for type");
3367 return false;
3370 /* Case value is greater than maximum for type. */
3371 if (tree_int_cst_compare (case_low, max_value) > 0
3372 && tree_int_cst_compare (case_high, max_value) > 0)
3374 warning (0, "case label value exceeds maximum value for type");
3375 return false;
3378 /* Saturate lower case label value to minimum. */
3379 if (tree_int_cst_compare (case_high, min_value) >= 0
3380 && tree_int_cst_compare (case_low, min_value) < 0)
3382 warning (0, "lower value in case label range"
3383 " less than minimum value for type");
3384 case_low = min_value;
3387 /* Saturate upper case label value to maximum. */
3388 if (tree_int_cst_compare (case_low, max_value) <= 0
3389 && tree_int_cst_compare (case_high, max_value) > 0)
3391 warning (0, "upper value in case label range"
3392 " exceeds maximum value for type");
3393 case_high = max_value;
3396 if (*case_low_p != case_low)
3397 *case_low_p = convert (type, case_low);
3398 if (case_high_p && *case_high_p != case_high)
3399 *case_high_p = convert (type, case_high);
3401 return true;
3404 /* Return an integer type with BITS bits of precision,
3405 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3407 tree
3408 c_common_type_for_size (unsigned int bits, int unsignedp)
3410 if (bits == TYPE_PRECISION (integer_type_node))
3411 return unsignedp ? unsigned_type_node : integer_type_node;
3413 if (bits == TYPE_PRECISION (signed_char_type_node))
3414 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3416 if (bits == TYPE_PRECISION (short_integer_type_node))
3417 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3419 if (bits == TYPE_PRECISION (long_integer_type_node))
3420 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3422 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3423 return (unsignedp ? long_long_unsigned_type_node
3424 : long_long_integer_type_node);
3426 if (int128_integer_type_node
3427 && bits == TYPE_PRECISION (int128_integer_type_node))
3428 return (unsignedp ? int128_unsigned_type_node
3429 : int128_integer_type_node);
3431 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3432 return (unsignedp ? widest_unsigned_literal_type_node
3433 : widest_integer_literal_type_node);
3435 if (bits <= TYPE_PRECISION (intQI_type_node))
3436 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3438 if (bits <= TYPE_PRECISION (intHI_type_node))
3439 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3441 if (bits <= TYPE_PRECISION (intSI_type_node))
3442 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3444 if (bits <= TYPE_PRECISION (intDI_type_node))
3445 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3447 return 0;
3450 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3451 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3452 and saturating if SATP is nonzero, otherwise not saturating. */
3454 tree
3455 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3456 int unsignedp, int satp)
3458 enum machine_mode mode;
3459 if (ibit == 0)
3460 mode = unsignedp ? UQQmode : QQmode;
3461 else
3462 mode = unsignedp ? UHAmode : HAmode;
3464 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3465 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3466 break;
3468 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3470 sorry ("GCC cannot support operators with integer types and "
3471 "fixed-point types that have too many integral and "
3472 "fractional bits together");
3473 return 0;
3476 return c_common_type_for_mode (mode, satp);
3479 /* Used for communication between c_common_type_for_mode and
3480 c_register_builtin_type. */
3481 static GTY(()) tree registered_builtin_types;
3483 /* Return a data type that has machine mode MODE.
3484 If the mode is an integer,
3485 then UNSIGNEDP selects between signed and unsigned types.
3486 If the mode is a fixed-point mode,
3487 then UNSIGNEDP selects between saturating and nonsaturating types. */
3489 tree
3490 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3492 tree t;
3494 if (mode == TYPE_MODE (integer_type_node))
3495 return unsignedp ? unsigned_type_node : integer_type_node;
3497 if (mode == TYPE_MODE (signed_char_type_node))
3498 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3500 if (mode == TYPE_MODE (short_integer_type_node))
3501 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3503 if (mode == TYPE_MODE (long_integer_type_node))
3504 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3506 if (mode == TYPE_MODE (long_long_integer_type_node))
3507 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3509 if (int128_integer_type_node
3510 && mode == TYPE_MODE (int128_integer_type_node))
3511 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3513 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3514 return unsignedp ? widest_unsigned_literal_type_node
3515 : widest_integer_literal_type_node;
3517 if (mode == QImode)
3518 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3520 if (mode == HImode)
3521 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3523 if (mode == SImode)
3524 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3526 if (mode == DImode)
3527 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3529 #if HOST_BITS_PER_WIDE_INT >= 64
3530 if (mode == TYPE_MODE (intTI_type_node))
3531 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3532 #endif
3534 if (mode == TYPE_MODE (float_type_node))
3535 return float_type_node;
3537 if (mode == TYPE_MODE (double_type_node))
3538 return double_type_node;
3540 if (mode == TYPE_MODE (long_double_type_node))
3541 return long_double_type_node;
3543 if (mode == TYPE_MODE (void_type_node))
3544 return void_type_node;
3546 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3547 return (unsignedp
3548 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3549 : make_signed_type (GET_MODE_PRECISION (mode)));
3551 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3552 return (unsignedp
3553 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3554 : make_signed_type (GET_MODE_PRECISION (mode)));
3556 if (COMPLEX_MODE_P (mode))
3558 enum machine_mode inner_mode;
3559 tree inner_type;
3561 if (mode == TYPE_MODE (complex_float_type_node))
3562 return complex_float_type_node;
3563 if (mode == TYPE_MODE (complex_double_type_node))
3564 return complex_double_type_node;
3565 if (mode == TYPE_MODE (complex_long_double_type_node))
3566 return complex_long_double_type_node;
3568 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3569 return complex_integer_type_node;
3571 inner_mode = GET_MODE_INNER (mode);
3572 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3573 if (inner_type != NULL_TREE)
3574 return build_complex_type (inner_type);
3576 else if (VECTOR_MODE_P (mode))
3578 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3579 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3580 if (inner_type != NULL_TREE)
3581 return build_vector_type_for_mode (inner_type, mode);
3584 if (mode == TYPE_MODE (dfloat32_type_node))
3585 return dfloat32_type_node;
3586 if (mode == TYPE_MODE (dfloat64_type_node))
3587 return dfloat64_type_node;
3588 if (mode == TYPE_MODE (dfloat128_type_node))
3589 return dfloat128_type_node;
3591 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3593 if (mode == TYPE_MODE (short_fract_type_node))
3594 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3595 if (mode == TYPE_MODE (fract_type_node))
3596 return unsignedp ? sat_fract_type_node : fract_type_node;
3597 if (mode == TYPE_MODE (long_fract_type_node))
3598 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3599 if (mode == TYPE_MODE (long_long_fract_type_node))
3600 return unsignedp ? sat_long_long_fract_type_node
3601 : long_long_fract_type_node;
3603 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3604 return unsignedp ? sat_unsigned_short_fract_type_node
3605 : unsigned_short_fract_type_node;
3606 if (mode == TYPE_MODE (unsigned_fract_type_node))
3607 return unsignedp ? sat_unsigned_fract_type_node
3608 : unsigned_fract_type_node;
3609 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3610 return unsignedp ? sat_unsigned_long_fract_type_node
3611 : unsigned_long_fract_type_node;
3612 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3613 return unsignedp ? sat_unsigned_long_long_fract_type_node
3614 : unsigned_long_long_fract_type_node;
3616 if (mode == TYPE_MODE (short_accum_type_node))
3617 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3618 if (mode == TYPE_MODE (accum_type_node))
3619 return unsignedp ? sat_accum_type_node : accum_type_node;
3620 if (mode == TYPE_MODE (long_accum_type_node))
3621 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3622 if (mode == TYPE_MODE (long_long_accum_type_node))
3623 return unsignedp ? sat_long_long_accum_type_node
3624 : long_long_accum_type_node;
3626 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3627 return unsignedp ? sat_unsigned_short_accum_type_node
3628 : unsigned_short_accum_type_node;
3629 if (mode == TYPE_MODE (unsigned_accum_type_node))
3630 return unsignedp ? sat_unsigned_accum_type_node
3631 : unsigned_accum_type_node;
3632 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3633 return unsignedp ? sat_unsigned_long_accum_type_node
3634 : unsigned_long_accum_type_node;
3635 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3636 return unsignedp ? sat_unsigned_long_long_accum_type_node
3637 : unsigned_long_long_accum_type_node;
3639 if (mode == QQmode)
3640 return unsignedp ? sat_qq_type_node : qq_type_node;
3641 if (mode == HQmode)
3642 return unsignedp ? sat_hq_type_node : hq_type_node;
3643 if (mode == SQmode)
3644 return unsignedp ? sat_sq_type_node : sq_type_node;
3645 if (mode == DQmode)
3646 return unsignedp ? sat_dq_type_node : dq_type_node;
3647 if (mode == TQmode)
3648 return unsignedp ? sat_tq_type_node : tq_type_node;
3650 if (mode == UQQmode)
3651 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3652 if (mode == UHQmode)
3653 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3654 if (mode == USQmode)
3655 return unsignedp ? sat_usq_type_node : usq_type_node;
3656 if (mode == UDQmode)
3657 return unsignedp ? sat_udq_type_node : udq_type_node;
3658 if (mode == UTQmode)
3659 return unsignedp ? sat_utq_type_node : utq_type_node;
3661 if (mode == HAmode)
3662 return unsignedp ? sat_ha_type_node : ha_type_node;
3663 if (mode == SAmode)
3664 return unsignedp ? sat_sa_type_node : sa_type_node;
3665 if (mode == DAmode)
3666 return unsignedp ? sat_da_type_node : da_type_node;
3667 if (mode == TAmode)
3668 return unsignedp ? sat_ta_type_node : ta_type_node;
3670 if (mode == UHAmode)
3671 return unsignedp ? sat_uha_type_node : uha_type_node;
3672 if (mode == USAmode)
3673 return unsignedp ? sat_usa_type_node : usa_type_node;
3674 if (mode == UDAmode)
3675 return unsignedp ? sat_uda_type_node : uda_type_node;
3676 if (mode == UTAmode)
3677 return unsignedp ? sat_uta_type_node : uta_type_node;
3680 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3681 if (TYPE_MODE (TREE_VALUE (t)) == mode
3682 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3683 return TREE_VALUE (t);
3685 return 0;
3688 tree
3689 c_common_unsigned_type (tree type)
3691 return c_common_signed_or_unsigned_type (1, type);
3694 /* Return a signed type the same as TYPE in other respects. */
3696 tree
3697 c_common_signed_type (tree type)
3699 return c_common_signed_or_unsigned_type (0, type);
3702 /* Return a type the same as TYPE except unsigned or
3703 signed according to UNSIGNEDP. */
3705 tree
3706 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3708 tree type1;
3710 /* This block of code emulates the behavior of the old
3711 c_common_unsigned_type. In particular, it returns
3712 long_unsigned_type_node if passed a long, even when a int would
3713 have the same size. This is necessary for warnings to work
3714 correctly in archs where sizeof(int) == sizeof(long) */
3716 type1 = TYPE_MAIN_VARIANT (type);
3717 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3718 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3719 if (type1 == integer_type_node || type1 == unsigned_type_node)
3720 return unsignedp ? unsigned_type_node : integer_type_node;
3721 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3722 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3723 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3724 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3725 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3726 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3727 if (int128_integer_type_node
3728 && (type1 == int128_integer_type_node
3729 || type1 == int128_unsigned_type_node))
3730 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3731 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3732 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3733 #if HOST_BITS_PER_WIDE_INT >= 64
3734 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3735 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3736 #endif
3737 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3738 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3739 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3740 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3741 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3742 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3743 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3744 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3746 #define C_COMMON_FIXED_TYPES(NAME) \
3747 if (type1 == short_ ## NAME ## _type_node \
3748 || type1 == unsigned_short_ ## NAME ## _type_node) \
3749 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3750 : short_ ## NAME ## _type_node; \
3751 if (type1 == NAME ## _type_node \
3752 || type1 == unsigned_ ## NAME ## _type_node) \
3753 return unsignedp ? unsigned_ ## NAME ## _type_node \
3754 : NAME ## _type_node; \
3755 if (type1 == long_ ## NAME ## _type_node \
3756 || type1 == unsigned_long_ ## NAME ## _type_node) \
3757 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3758 : long_ ## NAME ## _type_node; \
3759 if (type1 == long_long_ ## NAME ## _type_node \
3760 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3761 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3762 : long_long_ ## NAME ## _type_node;
3764 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3765 if (type1 == NAME ## _type_node \
3766 || type1 == u ## NAME ## _type_node) \
3767 return unsignedp ? u ## NAME ## _type_node \
3768 : NAME ## _type_node;
3770 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3771 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3772 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3773 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3774 : sat_ ## short_ ## NAME ## _type_node; \
3775 if (type1 == sat_ ## NAME ## _type_node \
3776 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3777 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3778 : sat_ ## NAME ## _type_node; \
3779 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3780 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3781 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3782 : sat_ ## long_ ## NAME ## _type_node; \
3783 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3784 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3785 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3786 : sat_ ## long_long_ ## NAME ## _type_node;
3788 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3789 if (type1 == sat_ ## NAME ## _type_node \
3790 || type1 == sat_ ## u ## NAME ## _type_node) \
3791 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3792 : sat_ ## NAME ## _type_node;
3794 C_COMMON_FIXED_TYPES (fract);
3795 C_COMMON_FIXED_TYPES_SAT (fract);
3796 C_COMMON_FIXED_TYPES (accum);
3797 C_COMMON_FIXED_TYPES_SAT (accum);
3799 C_COMMON_FIXED_MODE_TYPES (qq);
3800 C_COMMON_FIXED_MODE_TYPES (hq);
3801 C_COMMON_FIXED_MODE_TYPES (sq);
3802 C_COMMON_FIXED_MODE_TYPES (dq);
3803 C_COMMON_FIXED_MODE_TYPES (tq);
3804 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3805 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3806 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3807 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3808 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3809 C_COMMON_FIXED_MODE_TYPES (ha);
3810 C_COMMON_FIXED_MODE_TYPES (sa);
3811 C_COMMON_FIXED_MODE_TYPES (da);
3812 C_COMMON_FIXED_MODE_TYPES (ta);
3813 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3814 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3815 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3816 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3818 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3819 the precision; they have precision set to match their range, but
3820 may use a wider mode to match an ABI. If we change modes, we may
3821 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3822 the precision as well, so as to yield correct results for
3823 bit-field types. C++ does not have these separate bit-field
3824 types, and producing a signed or unsigned variant of an
3825 ENUMERAL_TYPE may cause other problems as well. */
3827 if (!INTEGRAL_TYPE_P (type)
3828 || TYPE_UNSIGNED (type) == unsignedp)
3829 return type;
3831 #define TYPE_OK(node) \
3832 (TYPE_MODE (type) == TYPE_MODE (node) \
3833 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3834 if (TYPE_OK (signed_char_type_node))
3835 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3836 if (TYPE_OK (integer_type_node))
3837 return unsignedp ? unsigned_type_node : integer_type_node;
3838 if (TYPE_OK (short_integer_type_node))
3839 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3840 if (TYPE_OK (long_integer_type_node))
3841 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3842 if (TYPE_OK (long_long_integer_type_node))
3843 return (unsignedp ? long_long_unsigned_type_node
3844 : long_long_integer_type_node);
3845 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3846 return (unsignedp ? int128_unsigned_type_node
3847 : int128_integer_type_node);
3848 if (TYPE_OK (widest_integer_literal_type_node))
3849 return (unsignedp ? widest_unsigned_literal_type_node
3850 : widest_integer_literal_type_node);
3852 #if HOST_BITS_PER_WIDE_INT >= 64
3853 if (TYPE_OK (intTI_type_node))
3854 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3855 #endif
3856 if (TYPE_OK (intDI_type_node))
3857 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3858 if (TYPE_OK (intSI_type_node))
3859 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3860 if (TYPE_OK (intHI_type_node))
3861 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3862 if (TYPE_OK (intQI_type_node))
3863 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3864 #undef TYPE_OK
3866 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3869 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3871 tree
3872 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3874 /* Extended integer types of the same width as a standard type have
3875 lesser rank, so those of the same width as int promote to int or
3876 unsigned int and are valid for printf formats expecting int or
3877 unsigned int. To avoid such special cases, avoid creating
3878 extended integer types for bit-fields if a standard integer type
3879 is available. */
3880 if (width == TYPE_PRECISION (integer_type_node))
3881 return unsignedp ? unsigned_type_node : integer_type_node;
3882 if (width == TYPE_PRECISION (signed_char_type_node))
3883 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3884 if (width == TYPE_PRECISION (short_integer_type_node))
3885 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3886 if (width == TYPE_PRECISION (long_integer_type_node))
3887 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3888 if (width == TYPE_PRECISION (long_long_integer_type_node))
3889 return (unsignedp ? long_long_unsigned_type_node
3890 : long_long_integer_type_node);
3891 if (int128_integer_type_node
3892 && width == TYPE_PRECISION (int128_integer_type_node))
3893 return (unsignedp ? int128_unsigned_type_node
3894 : int128_integer_type_node);
3895 return build_nonstandard_integer_type (width, unsignedp);
3898 /* The C version of the register_builtin_type langhook. */
3900 void
3901 c_register_builtin_type (tree type, const char* name)
3903 tree decl;
3905 decl = build_decl (UNKNOWN_LOCATION,
3906 TYPE_DECL, get_identifier (name), type);
3907 DECL_ARTIFICIAL (decl) = 1;
3908 if (!TYPE_NAME (type))
3909 TYPE_NAME (type) = decl;
3910 pushdecl (decl);
3912 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3915 /* Print an error message for invalid operands to arith operation
3916 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3917 LOCATION is the location of the message. */
3919 void
3920 binary_op_error (location_t location, enum tree_code code,
3921 tree type0, tree type1)
3923 const char *opname;
3925 switch (code)
3927 case PLUS_EXPR:
3928 opname = "+"; break;
3929 case MINUS_EXPR:
3930 opname = "-"; break;
3931 case MULT_EXPR:
3932 opname = "*"; break;
3933 case MAX_EXPR:
3934 opname = "max"; break;
3935 case MIN_EXPR:
3936 opname = "min"; break;
3937 case EQ_EXPR:
3938 opname = "=="; break;
3939 case NE_EXPR:
3940 opname = "!="; break;
3941 case LE_EXPR:
3942 opname = "<="; break;
3943 case GE_EXPR:
3944 opname = ">="; break;
3945 case LT_EXPR:
3946 opname = "<"; break;
3947 case GT_EXPR:
3948 opname = ">"; break;
3949 case LSHIFT_EXPR:
3950 opname = "<<"; break;
3951 case RSHIFT_EXPR:
3952 opname = ">>"; break;
3953 case TRUNC_MOD_EXPR:
3954 case FLOOR_MOD_EXPR:
3955 opname = "%"; break;
3956 case TRUNC_DIV_EXPR:
3957 case FLOOR_DIV_EXPR:
3958 opname = "/"; break;
3959 case BIT_AND_EXPR:
3960 opname = "&"; break;
3961 case BIT_IOR_EXPR:
3962 opname = "|"; break;
3963 case TRUTH_ANDIF_EXPR:
3964 opname = "&&"; break;
3965 case TRUTH_ORIF_EXPR:
3966 opname = "||"; break;
3967 case BIT_XOR_EXPR:
3968 opname = "^"; break;
3969 default:
3970 gcc_unreachable ();
3972 error_at (location,
3973 "invalid operands to binary %s (have %qT and %qT)", opname,
3974 type0, type1);
3977 /* Given an expression as a tree, return its original type. Do this
3978 by stripping any conversion that preserves the sign and precision. */
3979 static tree
3980 expr_original_type (tree expr)
3982 STRIP_SIGN_NOPS (expr);
3983 return TREE_TYPE (expr);
3986 /* Subroutine of build_binary_op, used for comparison operations.
3987 See if the operands have both been converted from subword integer types
3988 and, if so, perhaps change them both back to their original type.
3989 This function is also responsible for converting the two operands
3990 to the proper common type for comparison.
3992 The arguments of this function are all pointers to local variables
3993 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3994 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3996 If this function returns nonzero, it means that the comparison has
3997 a constant value. What this function returns is an expression for
3998 that value. */
4000 tree
4001 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
4002 enum tree_code *rescode_ptr)
4004 tree type;
4005 tree op0 = *op0_ptr;
4006 tree op1 = *op1_ptr;
4007 int unsignedp0, unsignedp1;
4008 int real1, real2;
4009 tree primop0, primop1;
4010 enum tree_code code = *rescode_ptr;
4011 location_t loc = EXPR_LOC_OR_HERE (op0);
4013 /* Throw away any conversions to wider types
4014 already present in the operands. */
4016 primop0 = c_common_get_narrower (op0, &unsignedp0);
4017 primop1 = c_common_get_narrower (op1, &unsignedp1);
4019 /* If primopN is first sign-extended from primopN's precision to opN's
4020 precision, then zero-extended from opN's precision to
4021 *restype_ptr precision, shortenings might be invalid. */
4022 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4023 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4024 && !unsignedp0
4025 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4026 primop0 = op0;
4027 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4028 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4029 && !unsignedp1
4030 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4031 primop1 = op1;
4033 /* Handle the case that OP0 does not *contain* a conversion
4034 but it *requires* conversion to FINAL_TYPE. */
4036 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4037 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4038 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4039 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4041 /* If one of the operands must be floated, we cannot optimize. */
4042 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4043 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4045 /* If first arg is constant, swap the args (changing operation
4046 so value is preserved), for canonicalization. Don't do this if
4047 the second arg is 0. */
4049 if (TREE_CONSTANT (primop0)
4050 && !integer_zerop (primop1) && !real_zerop (primop1)
4051 && !fixed_zerop (primop1))
4053 tree tem = primop0;
4054 int temi = unsignedp0;
4055 primop0 = primop1;
4056 primop1 = tem;
4057 tem = op0;
4058 op0 = op1;
4059 op1 = tem;
4060 *op0_ptr = op0;
4061 *op1_ptr = op1;
4062 unsignedp0 = unsignedp1;
4063 unsignedp1 = temi;
4064 temi = real1;
4065 real1 = real2;
4066 real2 = temi;
4068 switch (code)
4070 case LT_EXPR:
4071 code = GT_EXPR;
4072 break;
4073 case GT_EXPR:
4074 code = LT_EXPR;
4075 break;
4076 case LE_EXPR:
4077 code = GE_EXPR;
4078 break;
4079 case GE_EXPR:
4080 code = LE_EXPR;
4081 break;
4082 default:
4083 break;
4085 *rescode_ptr = code;
4088 /* If comparing an integer against a constant more bits wide,
4089 maybe we can deduce a value of 1 or 0 independent of the data.
4090 Or else truncate the constant now
4091 rather than extend the variable at run time.
4093 This is only interesting if the constant is the wider arg.
4094 Also, it is not safe if the constant is unsigned and the
4095 variable arg is signed, since in this case the variable
4096 would be sign-extended and then regarded as unsigned.
4097 Our technique fails in this case because the lowest/highest
4098 possible unsigned results don't follow naturally from the
4099 lowest/highest possible values of the variable operand.
4100 For just EQ_EXPR and NE_EXPR there is another technique that
4101 could be used: see if the constant can be faithfully represented
4102 in the other operand's type, by truncating it and reextending it
4103 and see if that preserves the constant's value. */
4105 if (!real1 && !real2
4106 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4107 && TREE_CODE (primop1) == INTEGER_CST
4108 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4110 int min_gt, max_gt, min_lt, max_lt;
4111 tree maxval, minval;
4112 /* 1 if comparison is nominally unsigned. */
4113 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4114 tree val;
4116 type = c_common_signed_or_unsigned_type (unsignedp0,
4117 TREE_TYPE (primop0));
4119 maxval = TYPE_MAX_VALUE (type);
4120 minval = TYPE_MIN_VALUE (type);
4122 if (unsignedp && !unsignedp0)
4123 *restype_ptr = c_common_signed_type (*restype_ptr);
4125 if (TREE_TYPE (primop1) != *restype_ptr)
4127 /* Convert primop1 to target type, but do not introduce
4128 additional overflow. We know primop1 is an int_cst. */
4129 primop1 = force_fit_type_double (*restype_ptr,
4130 tree_to_double_int (primop1),
4131 0, TREE_OVERFLOW (primop1));
4133 if (type != *restype_ptr)
4135 minval = convert (*restype_ptr, minval);
4136 maxval = convert (*restype_ptr, maxval);
4139 if (unsignedp && unsignedp0)
4141 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4142 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4143 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4144 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4146 else
4148 min_gt = INT_CST_LT (primop1, minval);
4149 max_gt = INT_CST_LT (primop1, maxval);
4150 min_lt = INT_CST_LT (minval, primop1);
4151 max_lt = INT_CST_LT (maxval, primop1);
4154 val = 0;
4155 /* This used to be a switch, but Genix compiler can't handle that. */
4156 if (code == NE_EXPR)
4158 if (max_lt || min_gt)
4159 val = truthvalue_true_node;
4161 else if (code == EQ_EXPR)
4163 if (max_lt || min_gt)
4164 val = truthvalue_false_node;
4166 else if (code == LT_EXPR)
4168 if (max_lt)
4169 val = truthvalue_true_node;
4170 if (!min_lt)
4171 val = truthvalue_false_node;
4173 else if (code == GT_EXPR)
4175 if (min_gt)
4176 val = truthvalue_true_node;
4177 if (!max_gt)
4178 val = truthvalue_false_node;
4180 else if (code == LE_EXPR)
4182 if (!max_gt)
4183 val = truthvalue_true_node;
4184 if (min_gt)
4185 val = truthvalue_false_node;
4187 else if (code == GE_EXPR)
4189 if (!min_lt)
4190 val = truthvalue_true_node;
4191 if (max_lt)
4192 val = truthvalue_false_node;
4195 /* If primop0 was sign-extended and unsigned comparison specd,
4196 we did a signed comparison above using the signed type bounds.
4197 But the comparison we output must be unsigned.
4199 Also, for inequalities, VAL is no good; but if the signed
4200 comparison had *any* fixed result, it follows that the
4201 unsigned comparison just tests the sign in reverse
4202 (positive values are LE, negative ones GE).
4203 So we can generate an unsigned comparison
4204 against an extreme value of the signed type. */
4206 if (unsignedp && !unsignedp0)
4208 if (val != 0)
4209 switch (code)
4211 case LT_EXPR:
4212 case GE_EXPR:
4213 primop1 = TYPE_MIN_VALUE (type);
4214 val = 0;
4215 break;
4217 case LE_EXPR:
4218 case GT_EXPR:
4219 primop1 = TYPE_MAX_VALUE (type);
4220 val = 0;
4221 break;
4223 default:
4224 break;
4226 type = c_common_unsigned_type (type);
4229 if (TREE_CODE (primop0) != INTEGER_CST
4230 && c_inhibit_evaluation_warnings == 0)
4232 if (val == truthvalue_false_node)
4233 warning_at (loc, OPT_Wtype_limits,
4234 "comparison is always false due to limited range of data type");
4235 if (val == truthvalue_true_node)
4236 warning_at (loc, OPT_Wtype_limits,
4237 "comparison is always true due to limited range of data type");
4240 if (val != 0)
4242 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4243 if (TREE_SIDE_EFFECTS (primop0))
4244 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4245 return val;
4248 /* Value is not predetermined, but do the comparison
4249 in the type of the operand that is not constant.
4250 TYPE is already properly set. */
4253 /* If either arg is decimal float and the other is float, find the
4254 proper common type to use for comparison. */
4255 else if (real1 && real2
4256 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4257 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4258 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4260 else if (real1 && real2
4261 && (TYPE_PRECISION (TREE_TYPE (primop0))
4262 == TYPE_PRECISION (TREE_TYPE (primop1))))
4263 type = TREE_TYPE (primop0);
4265 /* If args' natural types are both narrower than nominal type
4266 and both extend in the same manner, compare them
4267 in the type of the wider arg.
4268 Otherwise must actually extend both to the nominal
4269 common type lest different ways of extending
4270 alter the result.
4271 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4273 else if (unsignedp0 == unsignedp1 && real1 == real2
4274 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4275 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4277 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4278 type = c_common_signed_or_unsigned_type (unsignedp0
4279 || TYPE_UNSIGNED (*restype_ptr),
4280 type);
4281 /* Make sure shorter operand is extended the right way
4282 to match the longer operand. */
4283 primop0
4284 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4285 TREE_TYPE (primop0)),
4286 primop0);
4287 primop1
4288 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4289 TREE_TYPE (primop1)),
4290 primop1);
4292 else
4294 /* Here we must do the comparison on the nominal type
4295 using the args exactly as we received them. */
4296 type = *restype_ptr;
4297 primop0 = op0;
4298 primop1 = op1;
4300 if (!real1 && !real2 && integer_zerop (primop1)
4301 && TYPE_UNSIGNED (*restype_ptr))
4303 tree value = 0;
4304 /* All unsigned values are >= 0, so we warn. However,
4305 if OP0 is a constant that is >= 0, the signedness of
4306 the comparison isn't an issue, so suppress the
4307 warning. */
4308 bool warn =
4309 warn_type_limits && !in_system_header
4310 && c_inhibit_evaluation_warnings == 0
4311 && !(TREE_CODE (primop0) == INTEGER_CST
4312 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4313 primop0)))
4314 /* Do not warn for enumeration types. */
4315 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4317 switch (code)
4319 case GE_EXPR:
4320 if (warn)
4321 warning_at (loc, OPT_Wtype_limits,
4322 "comparison of unsigned expression >= 0 is always true");
4323 value = truthvalue_true_node;
4324 break;
4326 case LT_EXPR:
4327 if (warn)
4328 warning_at (loc, OPT_Wtype_limits,
4329 "comparison of unsigned expression < 0 is always false");
4330 value = truthvalue_false_node;
4331 break;
4333 default:
4334 break;
4337 if (value != 0)
4339 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4340 if (TREE_SIDE_EFFECTS (primop0))
4341 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4342 primop0, value);
4343 return value;
4348 *op0_ptr = convert (type, primop0);
4349 *op1_ptr = convert (type, primop1);
4351 *restype_ptr = truthvalue_type_node;
4353 return 0;
4356 /* Return a tree for the sum or difference (RESULTCODE says which)
4357 of pointer PTROP and integer INTOP. */
4359 tree
4360 pointer_int_sum (location_t loc, enum tree_code resultcode,
4361 tree ptrop, tree intop, bool complain)
4363 tree size_exp, ret;
4365 /* The result is a pointer of the same type that is being added. */
4366 tree result_type = TREE_TYPE (ptrop);
4368 /* If the pointer lives in UPC shared memory, then
4369 drop the 'shared' qualifier. */
4370 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4371 result_type = build_upc_unshared_type (result_type);
4373 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4375 if (complain && warn_pointer_arith)
4376 pedwarn (loc, OPT_Wpointer_arith,
4377 "pointer of type %<void *%> used in arithmetic");
4378 else if (!complain)
4379 return error_mark_node;
4380 size_exp = integer_one_node;
4382 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4384 if (complain && warn_pointer_arith)
4385 pedwarn (loc, OPT_Wpointer_arith,
4386 "pointer to a function used in arithmetic");
4387 else if (!complain)
4388 return error_mark_node;
4389 size_exp = integer_one_node;
4391 else
4392 size_exp = size_in_bytes (TREE_TYPE (result_type));
4394 /* We are manipulating pointer values, so we don't need to warn
4395 about relying on undefined signed overflow. We disable the
4396 warning here because we use integer types so fold won't know that
4397 they are really pointers. */
4398 fold_defer_overflow_warnings ();
4400 /* If what we are about to multiply by the size of the elements
4401 contains a constant term, apply distributive law
4402 and multiply that constant term separately.
4403 This helps produce common subexpressions. */
4404 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4405 && !TREE_CONSTANT (intop)
4406 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4407 && TREE_CONSTANT (size_exp)
4408 /* If the constant comes from pointer subtraction,
4409 skip this optimization--it would cause an error. */
4410 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4411 /* If the constant is unsigned, and smaller than the pointer size,
4412 then we must skip this optimization. This is because it could cause
4413 an overflow error if the constant is negative but INTOP is not. */
4414 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4415 || (TYPE_PRECISION (TREE_TYPE (intop))
4416 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4418 enum tree_code subcode = resultcode;
4419 tree int_type = TREE_TYPE (intop);
4420 if (TREE_CODE (intop) == MINUS_EXPR)
4421 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4422 /* Convert both subexpression types to the type of intop,
4423 because weird cases involving pointer arithmetic
4424 can result in a sum or difference with different type args. */
4425 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4426 subcode, ptrop,
4427 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4428 intop = convert (int_type, TREE_OPERAND (intop, 0));
4431 /* Convert the integer argument to a type the same size as sizetype
4432 so the multiply won't overflow spuriously. */
4433 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4434 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4435 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4436 TYPE_UNSIGNED (sizetype)), intop);
4438 /* Replace the integer argument with a suitable product by the object size.
4439 Do this multiplication as signed, then convert to the appropriate type
4440 for the pointer operation and disregard an overflow that occurred only
4441 because of the sign-extension change in the latter conversion. */
4443 tree t = build_binary_op (loc,
4444 MULT_EXPR, intop,
4445 convert (TREE_TYPE (intop), size_exp), 1);
4446 intop = convert (sizetype, t);
4447 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4448 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4449 TREE_INT_CST_HIGH (intop));
4452 /* Create the sum or difference. */
4453 if (resultcode == MINUS_EXPR)
4454 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4456 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4458 fold_undefer_and_ignore_overflow_warnings ();
4460 return ret;
4463 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4464 and if NON_CONST is known not to be permitted in an evaluated part
4465 of a constant expression. */
4467 tree
4468 c_wrap_maybe_const (tree expr, bool non_const)
4470 bool nowarning = TREE_NO_WARNING (expr);
4471 location_t loc = EXPR_LOCATION (expr);
4473 /* This should never be called for C++. */
4474 if (c_dialect_cxx ())
4475 gcc_unreachable ();
4477 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4478 STRIP_TYPE_NOPS (expr);
4479 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4480 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4481 if (nowarning)
4482 TREE_NO_WARNING (expr) = 1;
4483 protected_set_expr_location (expr, loc);
4485 return expr;
4488 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4489 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4490 around the SAVE_EXPR if needed so that c_fully_fold does not need
4491 to look inside SAVE_EXPRs. */
4493 tree
4494 c_save_expr (tree expr)
4496 bool maybe_const = true;
4497 if (c_dialect_cxx ())
4498 return save_expr (expr);
4499 expr = c_fully_fold (expr, false, &maybe_const);
4500 expr = save_expr (expr);
4501 if (!maybe_const)
4502 expr = c_wrap_maybe_const (expr, true);
4503 return expr;
4506 /* Return whether EXPR is a declaration whose address can never be
4507 NULL. */
4509 bool
4510 decl_with_nonnull_addr_p (const_tree expr)
4512 return (DECL_P (expr)
4513 && (TREE_CODE (expr) == PARM_DECL
4514 || TREE_CODE (expr) == LABEL_DECL
4515 || !DECL_WEAK (expr)));
4518 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4519 or for an `if' or `while' statement or ?..: exp. It should already
4520 have been validated to be of suitable type; otherwise, a bad
4521 diagnostic may result.
4523 The EXPR is located at LOCATION.
4525 This preparation consists of taking the ordinary
4526 representation of an expression expr and producing a valid tree
4527 boolean expression describing whether expr is nonzero. We could
4528 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4529 but we optimize comparisons, &&, ||, and !.
4531 The resulting type should always be `truthvalue_type_node'. */
4533 tree
4534 c_common_truthvalue_conversion (location_t location, tree expr)
4536 switch (TREE_CODE (expr))
4538 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4539 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4540 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4541 case ORDERED_EXPR: case UNORDERED_EXPR:
4542 if (TREE_TYPE (expr) == truthvalue_type_node)
4543 return expr;
4544 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4545 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4546 goto ret;
4548 case TRUTH_ANDIF_EXPR:
4549 case TRUTH_ORIF_EXPR:
4550 case TRUTH_AND_EXPR:
4551 case TRUTH_OR_EXPR:
4552 case TRUTH_XOR_EXPR:
4553 if (TREE_TYPE (expr) == truthvalue_type_node)
4554 return expr;
4555 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4556 c_common_truthvalue_conversion (location,
4557 TREE_OPERAND (expr, 0)),
4558 c_common_truthvalue_conversion (location,
4559 TREE_OPERAND (expr, 1)));
4560 goto ret;
4562 case TRUTH_NOT_EXPR:
4563 if (TREE_TYPE (expr) == truthvalue_type_node)
4564 return expr;
4565 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4566 c_common_truthvalue_conversion (location,
4567 TREE_OPERAND (expr, 0)));
4568 goto ret;
4570 case ERROR_MARK:
4571 return expr;
4573 case INTEGER_CST:
4574 return integer_zerop (expr) ? truthvalue_false_node
4575 : truthvalue_true_node;
4577 case REAL_CST:
4578 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4579 ? truthvalue_true_node
4580 : truthvalue_false_node;
4582 case FIXED_CST:
4583 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4584 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4585 ? truthvalue_true_node
4586 : truthvalue_false_node;
4588 case FUNCTION_DECL:
4589 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4590 /* Fall through. */
4592 case ADDR_EXPR:
4594 tree inner = TREE_OPERAND (expr, 0);
4595 if (decl_with_nonnull_addr_p (inner))
4597 /* Common Ada/Pascal programmer's mistake. */
4598 warning_at (location,
4599 OPT_Waddress,
4600 "the address of %qD will always evaluate as %<true%>",
4601 inner);
4602 return truthvalue_true_node;
4604 break;
4607 case COMPLEX_EXPR:
4608 expr = build_binary_op (EXPR_LOCATION (expr),
4609 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4610 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4611 c_common_truthvalue_conversion (location,
4612 TREE_OPERAND (expr, 0)),
4613 c_common_truthvalue_conversion (location,
4614 TREE_OPERAND (expr, 1)),
4616 goto ret;
4618 case NEGATE_EXPR:
4619 case ABS_EXPR:
4620 case FLOAT_EXPR:
4621 case EXCESS_PRECISION_EXPR:
4622 /* These don't change whether an object is nonzero or zero. */
4623 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4625 case LROTATE_EXPR:
4626 case RROTATE_EXPR:
4627 /* These don't change whether an object is zero or nonzero, but
4628 we can't ignore them if their second arg has side-effects. */
4629 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4631 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4632 TREE_OPERAND (expr, 1),
4633 c_common_truthvalue_conversion
4634 (location, TREE_OPERAND (expr, 0)));
4635 goto ret;
4637 else
4638 return c_common_truthvalue_conversion (location,
4639 TREE_OPERAND (expr, 0));
4641 case COND_EXPR:
4642 /* Distribute the conversion into the arms of a COND_EXPR. */
4643 if (c_dialect_cxx ())
4645 tree op1 = TREE_OPERAND (expr, 1);
4646 tree op2 = TREE_OPERAND (expr, 2);
4647 /* In C++ one of the arms might have void type if it is throw. */
4648 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4649 op1 = c_common_truthvalue_conversion (location, op1);
4650 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4651 op2 = c_common_truthvalue_conversion (location, op2);
4652 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4653 TREE_OPERAND (expr, 0), op1, op2);
4654 goto ret;
4656 else
4658 /* Folding will happen later for C. */
4659 expr = build3 (COND_EXPR, truthvalue_type_node,
4660 TREE_OPERAND (expr, 0),
4661 c_common_truthvalue_conversion (location,
4662 TREE_OPERAND (expr, 1)),
4663 c_common_truthvalue_conversion (location,
4664 TREE_OPERAND (expr, 2)));
4665 goto ret;
4668 CASE_CONVERT:
4670 tree totype = TREE_TYPE (expr);
4671 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4673 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4674 since that affects how `default_conversion' will behave. */
4675 if (TREE_CODE (totype) == REFERENCE_TYPE
4676 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4677 break;
4678 /* Don't strip a conversion from C++0x scoped enum, since they
4679 don't implicitly convert to other types. */
4680 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4681 && ENUM_IS_SCOPED (fromtype))
4682 break;
4683 /* If this isn't narrowing the argument, we can ignore it. */
4684 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4685 return c_common_truthvalue_conversion (location,
4686 TREE_OPERAND (expr, 0));
4688 break;
4690 case MODIFY_EXPR:
4691 if (!TREE_NO_WARNING (expr)
4692 && warn_parentheses)
4694 warning (OPT_Wparentheses,
4695 "suggest parentheses around assignment used as truth value");
4696 TREE_NO_WARNING (expr) = 1;
4698 break;
4700 default:
4701 break;
4704 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4706 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4707 expr = (build_binary_op
4708 (EXPR_LOCATION (expr),
4709 (TREE_SIDE_EFFECTS (expr)
4710 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4711 c_common_truthvalue_conversion
4712 (location,
4713 build_unary_op (location, REALPART_EXPR, t, 0)),
4714 c_common_truthvalue_conversion
4715 (location,
4716 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4717 0));
4718 goto ret;
4721 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4723 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4724 FCONST0 (TYPE_MODE
4725 (TREE_TYPE (expr))));
4726 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4728 else
4729 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4731 ret:
4732 protected_set_expr_location (expr, location);
4733 return expr;
4736 static void def_builtin_1 (enum built_in_function fncode,
4737 const char *name,
4738 enum built_in_class fnclass,
4739 tree fntype, tree libtype,
4740 bool both_p, bool fallback_p, bool nonansi_p,
4741 tree fnattrs, bool implicit_p);
4744 /* Apply the TYPE_QUALS to the new DECL. */
4746 void
4747 c_apply_type_quals_to_decl (int type_quals, tree decl)
4749 tree type = TREE_TYPE (decl);
4751 if (type == error_mark_node)
4752 return;
4754 if ((type_quals & TYPE_QUAL_CONST)
4755 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4756 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4757 constructor can produce constant init, so rely on cp_finish_decl to
4758 clear TREE_READONLY if the variable has non-constant init. */
4759 TREE_READONLY (decl) = 1;
4760 if (type_quals & TYPE_QUAL_VOLATILE)
4762 TREE_SIDE_EFFECTS (decl) = 1;
4763 TREE_THIS_VOLATILE (decl) = 1;
4765 if (type_quals & TYPE_QUAL_RESTRICT)
4767 while (type && TREE_CODE (type) == ARRAY_TYPE)
4768 /* Allow 'restrict' on arrays of pointers.
4769 FIXME currently we just ignore it. */
4770 type = TREE_TYPE (type);
4771 if (!type
4772 || !POINTER_TYPE_P (type)
4773 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4774 error ("invalid use of %<restrict%>");
4776 if (type_quals & TYPE_QUAL_SHARED)
4778 TREE_SHARED (decl) = 1;
4779 if (type_quals & TYPE_QUAL_STRICT)
4780 TREE_STRICT(decl) = 1;
4781 else if (type_quals & TYPE_QUAL_RELAXED)
4782 TREE_RELAXED(decl) = 1;
4783 /* The declaration's type should have been previously defined
4784 as a UPC shared type. */
4785 gcc_assert (upc_shared_type_p (type));
4789 /* Hash function for the problem of multiple type definitions in
4790 different files. This must hash all types that will compare
4791 equal via comptypes to the same value. In practice it hashes
4792 on some of the simple stuff and leaves the details to comptypes. */
4794 static hashval_t
4795 c_type_hash (const void *p)
4797 int n_elements;
4798 int shift, size;
4799 const_tree const t = (const_tree) p;
4800 tree t2;
4801 switch (TREE_CODE (t))
4803 /* For pointers, hash on pointee type plus some swizzling. */
4804 case POINTER_TYPE:
4805 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4806 /* Hash on number of elements and total size. */
4807 case ENUMERAL_TYPE:
4808 shift = 3;
4809 t2 = TYPE_VALUES (t);
4810 break;
4811 case RECORD_TYPE:
4812 shift = 0;
4813 t2 = TYPE_FIELDS (t);
4814 break;
4815 case QUAL_UNION_TYPE:
4816 shift = 1;
4817 t2 = TYPE_FIELDS (t);
4818 break;
4819 case UNION_TYPE:
4820 shift = 2;
4821 t2 = TYPE_FIELDS (t);
4822 break;
4823 default:
4824 gcc_unreachable ();
4826 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4827 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4828 n_elements = list_length (t2);
4829 /* We might have a VLA here. */
4830 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4831 size = 0;
4832 else
4833 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4834 return ((size << 24) | (n_elements << shift));
4837 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4839 /* Return the typed-based alias set for T, which may be an expression
4840 or a type. Return -1 if we don't do anything special. */
4842 alias_set_type
4843 c_common_get_alias_set (tree t)
4845 tree u;
4846 PTR *slot;
4848 /* For VLAs, use the alias set of the element type rather than the
4849 default of alias set 0 for types compared structurally. */
4850 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4852 if (TREE_CODE (t) == ARRAY_TYPE)
4853 return get_alias_set (TREE_TYPE (t));
4854 return -1;
4857 /* Permit type-punning when accessing a union, provided the access
4858 is directly through the union. For example, this code does not
4859 permit taking the address of a union member and then storing
4860 through it. Even the type-punning allowed here is a GCC
4861 extension, albeit a common and useful one; the C standard says
4862 that such accesses have implementation-defined behavior. */
4863 for (u = t;
4864 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4865 u = TREE_OPERAND (u, 0))
4866 if (TREE_CODE (u) == COMPONENT_REF
4867 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4868 return 0;
4870 /* That's all the expressions we handle specially. */
4871 if (!TYPE_P (t))
4872 return -1;
4874 /* The C standard guarantees that any object may be accessed via an
4875 lvalue that has character type. */
4876 if (t == char_type_node
4877 || t == signed_char_type_node
4878 || t == unsigned_char_type_node)
4879 return 0;
4881 /* The C standard specifically allows aliasing between signed and
4882 unsigned variants of the same type. We treat the signed
4883 variant as canonical. */
4884 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4886 tree t1 = c_common_signed_type (t);
4888 /* t1 == t can happen for boolean nodes which are always unsigned. */
4889 if (t1 != t)
4890 return get_alias_set (t1);
4893 /* Handle the case of multiple type nodes referring to "the same" type,
4894 which occurs with IMA. These share an alias set. FIXME: Currently only
4895 C90 is handled. (In C99 type compatibility is not transitive, which
4896 complicates things mightily. The alias set splay trees can theoretically
4897 represent this, but insertion is tricky when you consider all the
4898 different orders things might arrive in.) */
4900 if (c_language != clk_c || flag_isoc99)
4901 return -1;
4903 /* Save time if there's only one input file. */
4904 if (num_in_fnames == 1)
4905 return -1;
4907 /* Pointers need special handling if they point to any type that
4908 needs special handling (below). */
4909 if (TREE_CODE (t) == POINTER_TYPE)
4911 tree t2;
4912 /* Find bottom type under any nested POINTERs. */
4913 for (t2 = TREE_TYPE (t);
4914 TREE_CODE (t2) == POINTER_TYPE;
4915 t2 = TREE_TYPE (t2))
4917 if (TREE_CODE (t2) != RECORD_TYPE
4918 && TREE_CODE (t2) != ENUMERAL_TYPE
4919 && TREE_CODE (t2) != QUAL_UNION_TYPE
4920 && TREE_CODE (t2) != UNION_TYPE)
4921 return -1;
4922 if (TYPE_SIZE (t2) == 0)
4923 return -1;
4925 /* These are the only cases that need special handling. */
4926 if (TREE_CODE (t) != RECORD_TYPE
4927 && TREE_CODE (t) != ENUMERAL_TYPE
4928 && TREE_CODE (t) != QUAL_UNION_TYPE
4929 && TREE_CODE (t) != UNION_TYPE
4930 && TREE_CODE (t) != POINTER_TYPE)
4931 return -1;
4932 /* Undefined? */
4933 if (TYPE_SIZE (t) == 0)
4934 return -1;
4936 /* Look up t in hash table. Only one of the compatible types within each
4937 alias set is recorded in the table. */
4938 if (!type_hash_table)
4939 type_hash_table = htab_create_ggc (1021, c_type_hash,
4940 (htab_eq) lang_hooks.types_compatible_p,
4941 NULL);
4942 slot = htab_find_slot (type_hash_table, t, INSERT);
4943 if (*slot != NULL)
4945 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4946 return TYPE_ALIAS_SET ((tree)*slot);
4948 else
4949 /* Our caller will assign and record (in t) a new alias set; all we need
4950 to do is remember t in the hash table. */
4951 *slot = t;
4953 return -1;
4956 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4957 the second parameter indicates which OPERATOR is being applied.
4958 The COMPLAIN flag controls whether we should diagnose possibly
4959 ill-formed constructs or not. LOC is the location of the SIZEOF or
4960 TYPEOF operator. */
4962 tree
4963 c_sizeof_or_alignof_type (location_t loc,
4964 tree type, bool is_sizeof, int complain)
4966 const char *op_name;
4967 tree value = NULL;
4968 enum tree_code type_code = TREE_CODE (type);
4970 op_name = is_sizeof ? "sizeof" : "__alignof__";
4972 if (type_code == FUNCTION_TYPE)
4974 if (is_sizeof)
4976 if (complain && warn_pointer_arith)
4977 pedwarn (loc, OPT_Wpointer_arith,
4978 "invalid application of %<sizeof%> to a function type");
4979 else if (!complain)
4980 return error_mark_node;
4981 value = size_one_node;
4983 else
4985 if (complain)
4987 if (c_dialect_cxx ())
4988 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4989 "%<alignof%> applied to a function type");
4990 else
4991 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4992 "%<_Alignof%> applied to a function type");
4994 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4997 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4999 if (complain)
5001 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5002 error_at (loc, "invalid application of %qs"
5003 " to %<shared void%> type", op_name);
5004 else if (type_code == VOID_TYPE && warn_pointer_arith)
5005 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5006 "invalid application of %qs to a void type", op_name);
5008 else
5009 return error_mark_node;
5010 value = size_one_node;
5012 else if (!COMPLETE_TYPE_P (type)
5013 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5015 if (complain)
5016 error_at (loc, "invalid application of %qs to incomplete type %qT",
5017 op_name, type);
5018 return error_mark_node;
5020 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5021 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5023 if (complain)
5024 error_at (loc, "invalid application of %qs to array type %qT of "
5025 "incomplete element type", op_name, type);
5026 return error_mark_node;
5028 else
5030 if (is_sizeof)
5031 /* Convert in case a char is more than one unit. */
5032 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5033 size_int (TYPE_PRECISION (char_type_node)
5034 / BITS_PER_UNIT));
5035 else
5036 value = size_int (TYPE_ALIGN_UNIT (type));
5039 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5040 && upc_shared_type_p (type)
5041 && TYPE_HAS_THREADS_FACTOR (type))
5043 const tree n_threads = convert (sizetype, upc_num_threads ());
5044 value = size_binop (MULT_EXPR, value, n_threads);
5047 /* VALUE will have the middle-end integer type sizetype.
5048 However, we should really return a value of type `size_t',
5049 which is just a typedef for an ordinary integer type. */
5050 value = fold_convert_loc (loc, size_type_node, value);
5052 return value;
5055 /* Implement the __alignof keyword: Return the minimum required
5056 alignment of EXPR, measured in bytes. For VAR_DECLs,
5057 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5058 from an "aligned" __attribute__ specification). LOC is the
5059 location of the ALIGNOF operator. */
5061 tree
5062 c_alignof_expr (location_t loc, tree expr)
5064 tree t;
5066 if (VAR_OR_FUNCTION_DECL_P (expr))
5067 t = size_int (DECL_ALIGN_UNIT (expr));
5069 else if (TREE_CODE (expr) == COMPONENT_REF
5070 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5072 error_at (loc, "%<__alignof%> applied to a bit-field");
5073 t = size_one_node;
5075 else if (TREE_CODE (expr) == COMPONENT_REF
5076 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5077 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5079 else if (TREE_CODE (expr) == INDIRECT_REF)
5081 tree t = TREE_OPERAND (expr, 0);
5082 tree best = t;
5083 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5085 while (CONVERT_EXPR_P (t)
5086 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5088 int thisalign;
5090 t = TREE_OPERAND (t, 0);
5091 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5092 if (thisalign > bestalign)
5093 best = t, bestalign = thisalign;
5095 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5097 else
5098 return c_alignof (loc, TREE_TYPE (expr));
5100 return fold_convert_loc (loc, size_type_node, t);
5103 /* Handle C and C++ default attributes. */
5105 enum built_in_attribute
5107 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5108 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5109 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5110 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5111 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5112 #include "builtin-attrs.def"
5113 #undef DEF_ATTR_NULL_TREE
5114 #undef DEF_ATTR_INT
5115 #undef DEF_ATTR_STRING
5116 #undef DEF_ATTR_IDENT
5117 #undef DEF_ATTR_TREE_LIST
5118 ATTR_LAST
5121 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5123 static void c_init_attributes (void);
5125 enum c_builtin_type
5127 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5128 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5129 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5130 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5131 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5132 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5133 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5134 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5135 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5136 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5137 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5138 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5139 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5140 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5141 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5142 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5143 NAME,
5144 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5145 #include "builtin-types.def"
5146 #undef DEF_PRIMITIVE_TYPE
5147 #undef DEF_FUNCTION_TYPE_0
5148 #undef DEF_FUNCTION_TYPE_1
5149 #undef DEF_FUNCTION_TYPE_2
5150 #undef DEF_FUNCTION_TYPE_3
5151 #undef DEF_FUNCTION_TYPE_4
5152 #undef DEF_FUNCTION_TYPE_5
5153 #undef DEF_FUNCTION_TYPE_6
5154 #undef DEF_FUNCTION_TYPE_7
5155 #undef DEF_FUNCTION_TYPE_8
5156 #undef DEF_FUNCTION_TYPE_VAR_0
5157 #undef DEF_FUNCTION_TYPE_VAR_1
5158 #undef DEF_FUNCTION_TYPE_VAR_2
5159 #undef DEF_FUNCTION_TYPE_VAR_3
5160 #undef DEF_FUNCTION_TYPE_VAR_4
5161 #undef DEF_FUNCTION_TYPE_VAR_5
5162 #undef DEF_POINTER_TYPE
5163 BT_LAST
5166 typedef enum c_builtin_type builtin_type;
5168 /* A temporary array for c_common_nodes_and_builtins. Used in
5169 communication with def_fn_type. */
5170 static tree builtin_types[(int) BT_LAST + 1];
5172 /* A helper function for c_common_nodes_and_builtins. Build function type
5173 for DEF with return type RET and N arguments. If VAR is true, then the
5174 function should be variadic after those N arguments.
5176 Takes special care not to ICE if any of the types involved are
5177 error_mark_node, which indicates that said type is not in fact available
5178 (see builtin_type_for_size). In which case the function type as a whole
5179 should be error_mark_node. */
5181 static void
5182 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5184 tree t;
5185 tree *args = XALLOCAVEC (tree, n);
5186 va_list list;
5187 int i;
5189 va_start (list, n);
5190 for (i = 0; i < n; ++i)
5192 builtin_type a = (builtin_type) va_arg (list, int);
5193 t = builtin_types[a];
5194 if (t == error_mark_node)
5195 goto egress;
5196 args[i] = t;
5199 t = builtin_types[ret];
5200 if (t == error_mark_node)
5201 goto egress;
5202 if (var)
5203 t = build_varargs_function_type_array (t, n, args);
5204 else
5205 t = build_function_type_array (t, n, args);
5207 egress:
5208 builtin_types[def] = t;
5209 va_end (list);
5212 /* Build builtin functions common to both C and C++ language
5213 frontends. */
5215 static void
5216 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5218 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5219 builtin_types[ENUM] = VALUE;
5220 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5221 def_fn_type (ENUM, RETURN, 0, 0);
5222 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5223 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5224 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5225 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5226 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5227 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5228 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5229 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5230 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5231 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5232 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5233 ARG6) \
5234 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5235 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5236 ARG6, ARG7) \
5237 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5238 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5239 ARG6, ARG7, ARG8) \
5240 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5241 ARG7, ARG8);
5242 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5243 def_fn_type (ENUM, RETURN, 1, 0);
5244 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5245 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5246 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5247 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5248 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5249 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5250 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5251 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5252 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5253 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5254 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5255 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5257 #include "builtin-types.def"
5259 #undef DEF_PRIMITIVE_TYPE
5260 #undef DEF_FUNCTION_TYPE_1
5261 #undef DEF_FUNCTION_TYPE_2
5262 #undef DEF_FUNCTION_TYPE_3
5263 #undef DEF_FUNCTION_TYPE_4
5264 #undef DEF_FUNCTION_TYPE_5
5265 #undef DEF_FUNCTION_TYPE_6
5266 #undef DEF_FUNCTION_TYPE_VAR_0
5267 #undef DEF_FUNCTION_TYPE_VAR_1
5268 #undef DEF_FUNCTION_TYPE_VAR_2
5269 #undef DEF_FUNCTION_TYPE_VAR_3
5270 #undef DEF_FUNCTION_TYPE_VAR_4
5271 #undef DEF_FUNCTION_TYPE_VAR_5
5272 #undef DEF_POINTER_TYPE
5273 builtin_types[(int) BT_LAST] = NULL_TREE;
5275 c_init_attributes ();
5277 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5278 NONANSI_P, ATTRS, IMPLICIT, COND) \
5279 if (NAME && COND) \
5280 def_builtin_1 (ENUM, NAME, CLASS, \
5281 builtin_types[(int) TYPE], \
5282 builtin_types[(int) LIBTYPE], \
5283 BOTH_P, FALLBACK_P, NONANSI_P, \
5284 built_in_attributes[(int) ATTRS], IMPLICIT);
5285 #include "builtins.def"
5286 #undef DEF_BUILTIN
5288 targetm.init_builtins ();
5290 build_common_builtin_nodes ();
5292 if (flag_enable_cilkplus)
5293 cilk_init_builtins ();
5296 /* Like get_identifier, but avoid warnings about null arguments when
5297 the argument may be NULL for targets where GCC lacks stdint.h type
5298 information. */
5300 static inline tree
5301 c_get_ident (const char *id)
5303 return get_identifier (id);
5306 /* Build tree nodes and builtin functions common to both C and C++ language
5307 frontends. */
5309 void
5310 c_common_nodes_and_builtins (void)
5312 int char16_type_size;
5313 int char32_type_size;
5314 int wchar_type_size;
5315 tree array_domain_type;
5316 tree va_list_ref_type_node;
5317 tree va_list_arg_type_node;
5319 build_common_tree_nodes (flag_signed_char, flag_short_double);
5321 /* Define `int' and `char' first so that dbx will output them first. */
5322 record_builtin_type (RID_INT, NULL, integer_type_node);
5323 record_builtin_type (RID_CHAR, "char", char_type_node);
5325 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5326 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5327 but not C. Are the conditionals here needed? */
5328 if (c_dialect_cxx ())
5329 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5330 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5331 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5332 record_builtin_type (RID_MAX, "long unsigned int",
5333 long_unsigned_type_node);
5334 if (int128_integer_type_node != NULL_TREE)
5336 record_builtin_type (RID_INT128, "__int128",
5337 int128_integer_type_node);
5338 record_builtin_type (RID_MAX, "__int128 unsigned",
5339 int128_unsigned_type_node);
5341 if (c_dialect_cxx ())
5342 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5343 record_builtin_type (RID_MAX, "long long int",
5344 long_long_integer_type_node);
5345 record_builtin_type (RID_MAX, "long long unsigned int",
5346 long_long_unsigned_type_node);
5347 if (c_dialect_cxx ())
5348 record_builtin_type (RID_MAX, "long long unsigned",
5349 long_long_unsigned_type_node);
5350 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5351 record_builtin_type (RID_MAX, "short unsigned int",
5352 short_unsigned_type_node);
5353 if (c_dialect_cxx ())
5354 record_builtin_type (RID_MAX, "unsigned short",
5355 short_unsigned_type_node);
5357 /* Define both `signed char' and `unsigned char'. */
5358 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5359 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5361 /* These are types that c_common_type_for_size and
5362 c_common_type_for_mode use. */
5363 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5364 TYPE_DECL, NULL_TREE,
5365 intQI_type_node));
5366 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5367 TYPE_DECL, NULL_TREE,
5368 intHI_type_node));
5369 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 TYPE_DECL, NULL_TREE,
5371 intSI_type_node));
5372 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5373 TYPE_DECL, NULL_TREE,
5374 intDI_type_node));
5375 #if HOST_BITS_PER_WIDE_INT >= 64
5376 if (targetm.scalar_mode_supported_p (TImode))
5377 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5378 TYPE_DECL,
5379 get_identifier ("__int128_t"),
5380 intTI_type_node));
5381 #endif
5382 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5383 TYPE_DECL, NULL_TREE,
5384 unsigned_intQI_type_node));
5385 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5386 TYPE_DECL, NULL_TREE,
5387 unsigned_intHI_type_node));
5388 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5389 TYPE_DECL, NULL_TREE,
5390 unsigned_intSI_type_node));
5391 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5392 TYPE_DECL, NULL_TREE,
5393 unsigned_intDI_type_node));
5394 #if HOST_BITS_PER_WIDE_INT >= 64
5395 if (targetm.scalar_mode_supported_p (TImode))
5396 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5397 TYPE_DECL,
5398 get_identifier ("__uint128_t"),
5399 unsigned_intTI_type_node));
5400 #endif
5402 /* Create the widest literal types. */
5403 widest_integer_literal_type_node
5404 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5405 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5406 TYPE_DECL, NULL_TREE,
5407 widest_integer_literal_type_node));
5409 widest_unsigned_literal_type_node
5410 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5411 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5412 TYPE_DECL, NULL_TREE,
5413 widest_unsigned_literal_type_node));
5415 signed_size_type_node = c_common_signed_type (size_type_node);
5417 pid_type_node =
5418 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5420 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5421 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5422 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5424 /* Only supported decimal floating point extension if the target
5425 actually supports underlying modes. */
5426 if (targetm.scalar_mode_supported_p (SDmode)
5427 && targetm.scalar_mode_supported_p (DDmode)
5428 && targetm.scalar_mode_supported_p (TDmode))
5430 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5431 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5432 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5435 if (targetm.fixed_point_supported_p ())
5437 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5438 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5439 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5440 record_builtin_type (RID_MAX, "long long _Fract",
5441 long_long_fract_type_node);
5442 record_builtin_type (RID_MAX, "unsigned short _Fract",
5443 unsigned_short_fract_type_node);
5444 record_builtin_type (RID_MAX, "unsigned _Fract",
5445 unsigned_fract_type_node);
5446 record_builtin_type (RID_MAX, "unsigned long _Fract",
5447 unsigned_long_fract_type_node);
5448 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5449 unsigned_long_long_fract_type_node);
5450 record_builtin_type (RID_MAX, "_Sat short _Fract",
5451 sat_short_fract_type_node);
5452 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5453 record_builtin_type (RID_MAX, "_Sat long _Fract",
5454 sat_long_fract_type_node);
5455 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5456 sat_long_long_fract_type_node);
5457 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5458 sat_unsigned_short_fract_type_node);
5459 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5460 sat_unsigned_fract_type_node);
5461 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5462 sat_unsigned_long_fract_type_node);
5463 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5464 sat_unsigned_long_long_fract_type_node);
5465 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5466 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5467 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5468 record_builtin_type (RID_MAX, "long long _Accum",
5469 long_long_accum_type_node);
5470 record_builtin_type (RID_MAX, "unsigned short _Accum",
5471 unsigned_short_accum_type_node);
5472 record_builtin_type (RID_MAX, "unsigned _Accum",
5473 unsigned_accum_type_node);
5474 record_builtin_type (RID_MAX, "unsigned long _Accum",
5475 unsigned_long_accum_type_node);
5476 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5477 unsigned_long_long_accum_type_node);
5478 record_builtin_type (RID_MAX, "_Sat short _Accum",
5479 sat_short_accum_type_node);
5480 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5481 record_builtin_type (RID_MAX, "_Sat long _Accum",
5482 sat_long_accum_type_node);
5483 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5484 sat_long_long_accum_type_node);
5485 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5486 sat_unsigned_short_accum_type_node);
5487 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5488 sat_unsigned_accum_type_node);
5489 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5490 sat_unsigned_long_accum_type_node);
5491 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5492 sat_unsigned_long_long_accum_type_node);
5496 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5497 TYPE_DECL,
5498 get_identifier ("complex int"),
5499 complex_integer_type_node));
5500 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5501 TYPE_DECL,
5502 get_identifier ("complex float"),
5503 complex_float_type_node));
5504 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5505 TYPE_DECL,
5506 get_identifier ("complex double"),
5507 complex_double_type_node));
5508 lang_hooks.decls.pushdecl
5509 (build_decl (UNKNOWN_LOCATION,
5510 TYPE_DECL, get_identifier ("complex long double"),
5511 complex_long_double_type_node));
5513 if (c_dialect_cxx ())
5514 /* For C++, make fileptr_type_node a distinct void * type until
5515 FILE type is defined. */
5516 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5518 record_builtin_type (RID_VOID, NULL, void_type_node);
5520 /* Set the TYPE_NAME for any variants that were built before
5521 record_builtin_type gave names to the built-in types. */
5523 tree void_name = TYPE_NAME (void_type_node);
5524 TYPE_NAME (void_type_node) = NULL_TREE;
5525 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5526 = void_name;
5527 TYPE_NAME (void_type_node) = void_name;
5530 /* This node must not be shared. */
5531 void_zero_node = make_node (INTEGER_CST);
5532 TREE_TYPE (void_zero_node) = void_type_node;
5534 void_list_node = build_void_list_node ();
5536 /* Make a type to be the domain of a few array types
5537 whose domains don't really matter.
5538 200 is small enough that it always fits in size_t
5539 and large enough that it can hold most function names for the
5540 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5541 array_domain_type = build_index_type (size_int (200));
5543 /* Make a type for arrays of characters.
5544 With luck nothing will ever really depend on the length of this
5545 array type. */
5546 char_array_type_node
5547 = build_array_type (char_type_node, array_domain_type);
5549 /* Likewise for arrays of ints. */
5550 int_array_type_node
5551 = build_array_type (integer_type_node, array_domain_type);
5553 string_type_node = build_pointer_type (char_type_node);
5554 const_string_type_node
5555 = build_pointer_type (build_qualified_type
5556 (char_type_node, TYPE_QUAL_CONST));
5558 /* This is special for C++ so functions can be overloaded. */
5559 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5560 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5561 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5562 underlying_wchar_type_node = wchar_type_node;
5563 if (c_dialect_cxx ())
5565 if (TYPE_UNSIGNED (wchar_type_node))
5566 wchar_type_node = make_unsigned_type (wchar_type_size);
5567 else
5568 wchar_type_node = make_signed_type (wchar_type_size);
5569 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5572 /* This is for wide string constants. */
5573 wchar_array_type_node
5574 = build_array_type (wchar_type_node, array_domain_type);
5576 /* Define 'char16_t'. */
5577 char16_type_node = get_identifier (CHAR16_TYPE);
5578 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5579 char16_type_size = TYPE_PRECISION (char16_type_node);
5580 if (c_dialect_cxx ())
5582 char16_type_node = make_unsigned_type (char16_type_size);
5584 if (cxx_dialect >= cxx11)
5585 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5588 /* This is for UTF-16 string constants. */
5589 char16_array_type_node
5590 = build_array_type (char16_type_node, array_domain_type);
5592 /* Define 'char32_t'. */
5593 char32_type_node = get_identifier (CHAR32_TYPE);
5594 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5595 char32_type_size = TYPE_PRECISION (char32_type_node);
5596 if (c_dialect_cxx ())
5598 char32_type_node = make_unsigned_type (char32_type_size);
5600 if (cxx_dialect >= cxx11)
5601 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5604 /* This is for UTF-32 string constants. */
5605 char32_array_type_node
5606 = build_array_type (char32_type_node, array_domain_type);
5608 wint_type_node =
5609 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5611 intmax_type_node =
5612 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5613 uintmax_type_node =
5614 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5616 if (SIG_ATOMIC_TYPE)
5617 sig_atomic_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5619 if (INT8_TYPE)
5620 int8_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5622 if (INT16_TYPE)
5623 int16_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5625 if (INT32_TYPE)
5626 int32_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5628 if (INT64_TYPE)
5629 int64_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5631 if (UINT8_TYPE)
5632 uint8_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5634 if (UINT16_TYPE)
5635 c_uint16_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5637 if (UINT32_TYPE)
5638 c_uint32_type_node =
5639 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5640 if (UINT64_TYPE)
5641 c_uint64_type_node =
5642 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5643 if (INT_LEAST8_TYPE)
5644 int_least8_type_node =
5645 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5646 if (INT_LEAST16_TYPE)
5647 int_least16_type_node =
5648 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5649 if (INT_LEAST32_TYPE)
5650 int_least32_type_node =
5651 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5652 if (INT_LEAST64_TYPE)
5653 int_least64_type_node =
5654 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5655 if (UINT_LEAST8_TYPE)
5656 uint_least8_type_node =
5657 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5658 if (UINT_LEAST16_TYPE)
5659 uint_least16_type_node =
5660 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5661 if (UINT_LEAST32_TYPE)
5662 uint_least32_type_node =
5663 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5664 if (UINT_LEAST64_TYPE)
5665 uint_least64_type_node =
5666 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5667 if (INT_FAST8_TYPE)
5668 int_fast8_type_node =
5669 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5670 if (INT_FAST16_TYPE)
5671 int_fast16_type_node =
5672 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5673 if (INT_FAST32_TYPE)
5674 int_fast32_type_node =
5675 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5676 if (INT_FAST64_TYPE)
5677 int_fast64_type_node =
5678 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5679 if (UINT_FAST8_TYPE)
5680 uint_fast8_type_node =
5681 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5682 if (UINT_FAST16_TYPE)
5683 uint_fast16_type_node =
5684 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5685 if (UINT_FAST32_TYPE)
5686 uint_fast32_type_node =
5687 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5688 if (UINT_FAST64_TYPE)
5689 uint_fast64_type_node =
5690 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5691 if (INTPTR_TYPE)
5692 intptr_type_node =
5693 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5694 if (UINTPTR_TYPE)
5695 uintptr_type_node =
5696 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5698 default_function_type
5699 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5700 ptrdiff_type_node
5701 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5702 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5704 lang_hooks.decls.pushdecl
5705 (build_decl (UNKNOWN_LOCATION,
5706 TYPE_DECL, get_identifier ("__builtin_va_list"),
5707 va_list_type_node));
5708 if (targetm.enum_va_list_p)
5710 int l;
5711 const char *pname;
5712 tree ptype;
5714 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5716 lang_hooks.decls.pushdecl
5717 (build_decl (UNKNOWN_LOCATION,
5718 TYPE_DECL, get_identifier (pname),
5719 ptype));
5724 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5726 va_list_arg_type_node = va_list_ref_type_node =
5727 build_pointer_type (TREE_TYPE (va_list_type_node));
5729 else
5731 va_list_arg_type_node = va_list_type_node;
5732 va_list_ref_type_node = build_reference_type (va_list_type_node);
5735 if (!flag_preprocess_only)
5736 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5738 main_identifier_node = get_identifier ("main");
5740 /* Create the built-in __null node. It is important that this is
5741 not shared. */
5742 null_node = make_node (INTEGER_CST);
5743 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5745 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5746 memset (builtin_types, 0, sizeof (builtin_types));
5749 /* The number of named compound-literals generated thus far. */
5750 static GTY(()) int compound_literal_number;
5752 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5754 void
5755 set_compound_literal_name (tree decl)
5757 char *name;
5758 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5759 compound_literal_number);
5760 compound_literal_number++;
5761 DECL_NAME (decl) = get_identifier (name);
5764 tree
5765 build_va_arg (location_t loc, tree expr, tree type)
5767 expr = build1 (VA_ARG_EXPR, type, expr);
5768 SET_EXPR_LOCATION (expr, loc);
5769 return expr;
5773 /* Linked list of disabled built-in functions. */
5775 typedef struct disabled_builtin
5777 const char *name;
5778 struct disabled_builtin *next;
5779 } disabled_builtin;
5780 static disabled_builtin *disabled_builtins = NULL;
5782 static bool builtin_function_disabled_p (const char *);
5784 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5785 begins with "__builtin_", give an error. */
5787 void
5788 disable_builtin_function (const char *name)
5790 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5791 error ("cannot disable built-in function %qs", name);
5792 else
5794 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5795 new_disabled_builtin->name = name;
5796 new_disabled_builtin->next = disabled_builtins;
5797 disabled_builtins = new_disabled_builtin;
5802 /* Return true if the built-in function NAME has been disabled, false
5803 otherwise. */
5805 static bool
5806 builtin_function_disabled_p (const char *name)
5808 disabled_builtin *p;
5809 for (p = disabled_builtins; p != NULL; p = p->next)
5811 if (strcmp (name, p->name) == 0)
5812 return true;
5814 return false;
5818 /* Worker for DEF_BUILTIN.
5819 Possibly define a builtin function with one or two names.
5820 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5821 nonansi_p and flag_no_nonansi_builtin. */
5823 static void
5824 def_builtin_1 (enum built_in_function fncode,
5825 const char *name,
5826 enum built_in_class fnclass,
5827 tree fntype, tree libtype,
5828 bool both_p, bool fallback_p, bool nonansi_p,
5829 tree fnattrs, bool implicit_p)
5831 tree decl;
5832 const char *libname;
5834 if (fntype == error_mark_node)
5835 return;
5837 gcc_assert ((!both_p && !fallback_p)
5838 || !strncmp (name, "__builtin_",
5839 strlen ("__builtin_")));
5841 libname = name + strlen ("__builtin_");
5842 decl = add_builtin_function (name, fntype, fncode, fnclass,
5843 (fallback_p ? libname : NULL),
5844 fnattrs);
5846 set_builtin_decl (fncode, decl, implicit_p);
5848 if (both_p
5849 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5850 && !(nonansi_p && flag_no_nonansi_builtin))
5851 add_builtin_function (libname, libtype, fncode, fnclass,
5852 NULL, fnattrs);
5855 /* Nonzero if the type T promotes to int. This is (nearly) the
5856 integral promotions defined in ISO C99 6.3.1.1/2. */
5858 bool
5859 c_promoting_integer_type_p (const_tree t)
5861 switch (TREE_CODE (t))
5863 case INTEGER_TYPE:
5864 return (TYPE_MAIN_VARIANT (t) == char_type_node
5865 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5866 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5867 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5868 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5869 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5871 case ENUMERAL_TYPE:
5872 /* ??? Technically all enumerations not larger than an int
5873 promote to an int. But this is used along code paths
5874 that only want to notice a size change. */
5875 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5877 case BOOLEAN_TYPE:
5878 return 1;
5880 default:
5881 return 0;
5885 /* Return 1 if PARMS specifies a fixed number of parameters
5886 and none of their types is affected by default promotions. */
5889 self_promoting_args_p (const_tree parms)
5891 const_tree t;
5892 for (t = parms; t; t = TREE_CHAIN (t))
5894 tree type = TREE_VALUE (t);
5896 if (type == error_mark_node)
5897 continue;
5899 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5900 return 0;
5902 if (type == 0)
5903 return 0;
5905 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5906 return 0;
5908 if (c_promoting_integer_type_p (type))
5909 return 0;
5911 return 1;
5914 /* Recursively remove any '*' or '&' operator from TYPE. */
5915 tree
5916 strip_pointer_operator (tree t)
5918 while (POINTER_TYPE_P (t))
5919 t = TREE_TYPE (t);
5920 return t;
5923 /* Recursively remove pointer or array type from TYPE. */
5924 tree
5925 strip_pointer_or_array_types (tree t)
5927 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5928 t = TREE_TYPE (t);
5929 return t;
5932 /* Used to compare case labels. K1 and K2 are actually tree nodes
5933 representing case labels, or NULL_TREE for a `default' label.
5934 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5935 K2, and 0 if K1 and K2 are equal. */
5938 case_compare (splay_tree_key k1, splay_tree_key k2)
5940 /* Consider a NULL key (such as arises with a `default' label) to be
5941 smaller than anything else. */
5942 if (!k1)
5943 return k2 ? -1 : 0;
5944 else if (!k2)
5945 return k1 ? 1 : 0;
5947 return tree_int_cst_compare ((tree) k1, (tree) k2);
5950 /* Process a case label, located at LOC, for the range LOW_VALUE
5951 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5952 then this case label is actually a `default' label. If only
5953 HIGH_VALUE is NULL_TREE, then case label was declared using the
5954 usual C/C++ syntax, rather than the GNU case range extension.
5955 CASES is a tree containing all the case ranges processed so far;
5956 COND is the condition for the switch-statement itself. Returns the
5957 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5958 is created. */
5960 tree
5961 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5962 tree low_value, tree high_value)
5964 tree type;
5965 tree label;
5966 tree case_label;
5967 splay_tree_node node;
5969 /* Create the LABEL_DECL itself. */
5970 label = create_artificial_label (loc);
5972 /* If there was an error processing the switch condition, bail now
5973 before we get more confused. */
5974 if (!cond || cond == error_mark_node)
5975 goto error_out;
5977 if ((low_value && TREE_TYPE (low_value)
5978 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5979 || (high_value && TREE_TYPE (high_value)
5980 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5982 error_at (loc, "pointers are not permitted as case values");
5983 goto error_out;
5986 /* Case ranges are a GNU extension. */
5987 if (high_value)
5988 pedwarn (loc, OPT_Wpedantic,
5989 "range expressions in switch statements are non-standard");
5991 type = TREE_TYPE (cond);
5992 if (low_value)
5994 low_value = check_case_value (low_value);
5995 low_value = convert_and_check (type, low_value);
5996 if (low_value == error_mark_node)
5997 goto error_out;
5999 if (high_value)
6001 high_value = check_case_value (high_value);
6002 high_value = convert_and_check (type, high_value);
6003 if (high_value == error_mark_node)
6004 goto error_out;
6007 if (low_value && high_value)
6009 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6010 really a case range, even though it was written that way.
6011 Remove the HIGH_VALUE to simplify later processing. */
6012 if (tree_int_cst_equal (low_value, high_value))
6013 high_value = NULL_TREE;
6014 else if (!tree_int_cst_lt (low_value, high_value))
6015 warning_at (loc, 0, "empty range specified");
6018 /* See if the case is in range of the type of the original testing
6019 expression. If both low_value and high_value are out of range,
6020 don't insert the case label and return NULL_TREE. */
6021 if (low_value
6022 && !check_case_bounds (type, orig_type,
6023 &low_value, high_value ? &high_value : NULL))
6024 return NULL_TREE;
6026 /* Look up the LOW_VALUE in the table of case labels we already
6027 have. */
6028 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6029 /* If there was not an exact match, check for overlapping ranges.
6030 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6031 that's a `default' label and the only overlap is an exact match. */
6032 if (!node && (low_value || high_value))
6034 splay_tree_node low_bound;
6035 splay_tree_node high_bound;
6037 /* Even though there wasn't an exact match, there might be an
6038 overlap between this case range and another case range.
6039 Since we've (inductively) not allowed any overlapping case
6040 ranges, we simply need to find the greatest low case label
6041 that is smaller that LOW_VALUE, and the smallest low case
6042 label that is greater than LOW_VALUE. If there is an overlap
6043 it will occur in one of these two ranges. */
6044 low_bound = splay_tree_predecessor (cases,
6045 (splay_tree_key) low_value);
6046 high_bound = splay_tree_successor (cases,
6047 (splay_tree_key) low_value);
6049 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6050 the LOW_VALUE, so there is no need to check unless the
6051 LOW_BOUND is in fact itself a case range. */
6052 if (low_bound
6053 && CASE_HIGH ((tree) low_bound->value)
6054 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6055 low_value) >= 0)
6056 node = low_bound;
6057 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6058 range is bigger than the low end of the current range, so we
6059 are only interested if the current range is a real range, and
6060 not an ordinary case label. */
6061 else if (high_bound
6062 && high_value
6063 && (tree_int_cst_compare ((tree) high_bound->key,
6064 high_value)
6065 <= 0))
6066 node = high_bound;
6068 /* If there was an overlap, issue an error. */
6069 if (node)
6071 tree duplicate = CASE_LABEL ((tree) node->value);
6073 if (high_value)
6075 error_at (loc, "duplicate (or overlapping) case value");
6076 error_at (DECL_SOURCE_LOCATION (duplicate),
6077 "this is the first entry overlapping that value");
6079 else if (low_value)
6081 error_at (loc, "duplicate case value") ;
6082 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6084 else
6086 error_at (loc, "multiple default labels in one switch");
6087 error_at (DECL_SOURCE_LOCATION (duplicate),
6088 "this is the first default label");
6090 goto error_out;
6093 /* Add a CASE_LABEL to the statement-tree. */
6094 case_label = add_stmt (build_case_label (low_value, high_value, label));
6095 /* Register this case label in the splay tree. */
6096 splay_tree_insert (cases,
6097 (splay_tree_key) low_value,
6098 (splay_tree_value) case_label);
6100 return case_label;
6102 error_out:
6103 /* Add a label so that the back-end doesn't think that the beginning of
6104 the switch is unreachable. Note that we do not add a case label, as
6105 that just leads to duplicates and thence to failure later on. */
6106 if (!cases->root)
6108 tree t = create_artificial_label (loc);
6109 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6111 return error_mark_node;
6114 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6115 Used to verify that case values match up with enumerator values. */
6117 static void
6118 match_case_to_enum_1 (tree key, tree type, tree label)
6120 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6122 /* ??? Not working too hard to print the double-word value.
6123 Should perhaps be done with %lwd in the diagnostic routines? */
6124 if (TREE_INT_CST_HIGH (key) == 0)
6125 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6126 TREE_INT_CST_LOW (key));
6127 else if (!TYPE_UNSIGNED (type)
6128 && TREE_INT_CST_HIGH (key) == -1
6129 && TREE_INT_CST_LOW (key) != 0)
6130 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6131 -TREE_INT_CST_LOW (key));
6132 else
6133 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6134 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6135 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6137 if (TYPE_NAME (type) == 0)
6138 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6139 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6140 "case value %qs not in enumerated type",
6141 buf);
6142 else
6143 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6144 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6145 "case value %qs not in enumerated type %qT",
6146 buf, type);
6149 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6150 Used to verify that case values match up with enumerator values. */
6152 static int
6153 match_case_to_enum (splay_tree_node node, void *data)
6155 tree label = (tree) node->value;
6156 tree type = (tree) data;
6158 /* Skip default case. */
6159 if (!CASE_LOW (label))
6160 return 0;
6162 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6163 when we did our enum->case scan. Reset our scratch bit after. */
6164 if (!CASE_LOW_SEEN (label))
6165 match_case_to_enum_1 (CASE_LOW (label), type, label);
6166 else
6167 CASE_LOW_SEEN (label) = 0;
6169 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6170 not set, that means that CASE_HIGH did not appear when we did our
6171 enum->case scan. Reset our scratch bit after. */
6172 if (CASE_HIGH (label))
6174 if (!CASE_HIGH_SEEN (label))
6175 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6176 else
6177 CASE_HIGH_SEEN (label) = 0;
6180 return 0;
6183 /* Handle -Wswitch*. Called from the front end after parsing the
6184 switch construct. */
6185 /* ??? Should probably be somewhere generic, since other languages
6186 besides C and C++ would want this. At the moment, however, C/C++
6187 are the only tree-ssa languages that support enumerations at all,
6188 so the point is moot. */
6190 void
6191 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6192 tree type, tree cond)
6194 splay_tree_node default_node;
6195 splay_tree_node node;
6196 tree chain;
6198 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6199 return;
6201 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6202 if (!default_node)
6203 warning_at (switch_location, OPT_Wswitch_default,
6204 "switch missing default case");
6206 /* From here on, we only care about about enumerated types. */
6207 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6208 return;
6210 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6211 if (!warn_switch_enum && !warn_switch)
6212 return;
6214 /* Check the cases. Warn about case values which are not members of
6215 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6216 there is no default case, check that exactly all enumeration
6217 literals are covered by the cases. */
6219 /* Clearing COND if it is not an integer constant simplifies
6220 the tests inside the loop below. */
6221 if (TREE_CODE (cond) != INTEGER_CST)
6222 cond = NULL_TREE;
6224 /* The time complexity here is O(N*lg(N)) worst case, but for the
6225 common case of monotonically increasing enumerators, it is
6226 O(N), since the nature of the splay tree will keep the next
6227 element adjacent to the root at all times. */
6229 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6231 tree value = TREE_VALUE (chain);
6232 if (TREE_CODE (value) == CONST_DECL)
6233 value = DECL_INITIAL (value);
6234 node = splay_tree_lookup (cases, (splay_tree_key) value);
6235 if (node)
6237 /* Mark the CASE_LOW part of the case entry as seen. */
6238 tree label = (tree) node->value;
6239 CASE_LOW_SEEN (label) = 1;
6240 continue;
6243 /* Even though there wasn't an exact match, there might be a
6244 case range which includes the enumerator's value. */
6245 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6246 if (node && CASE_HIGH ((tree) node->value))
6248 tree label = (tree) node->value;
6249 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6250 if (cmp >= 0)
6252 /* If we match the upper bound exactly, mark the CASE_HIGH
6253 part of the case entry as seen. */
6254 if (cmp == 0)
6255 CASE_HIGH_SEEN (label) = 1;
6256 continue;
6260 /* We've now determined that this enumerated literal isn't
6261 handled by the case labels of the switch statement. */
6263 /* If the switch expression is a constant, we only really care
6264 about whether that constant is handled by the switch. */
6265 if (cond && tree_int_cst_compare (cond, value))
6266 continue;
6268 /* If there is a default_node, the only relevant option is
6269 Wswitch-enum. Otherwise, if both are enabled then we prefer
6270 to warn using -Wswitch because -Wswitch is enabled by -Wall
6271 while -Wswitch-enum is explicit. */
6272 warning_at (switch_location,
6273 (default_node || !warn_switch
6274 ? OPT_Wswitch_enum
6275 : OPT_Wswitch),
6276 "enumeration value %qE not handled in switch",
6277 TREE_PURPOSE (chain));
6280 /* Warn if there are case expressions that don't correspond to
6281 enumerators. This can occur since C and C++ don't enforce
6282 type-checking of assignments to enumeration variables.
6284 The time complexity here is now always O(N) worst case, since
6285 we should have marked both the lower bound and upper bound of
6286 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6287 above. This scan also resets those fields. */
6289 splay_tree_foreach (cases, match_case_to_enum, type);
6292 /* Finish an expression taking the address of LABEL (an
6293 IDENTIFIER_NODE). Returns an expression for the address.
6295 LOC is the location for the expression returned. */
6297 tree
6298 finish_label_address_expr (tree label, location_t loc)
6300 tree result;
6302 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6304 if (label == error_mark_node)
6305 return error_mark_node;
6307 label = lookup_label (label);
6308 if (label == NULL_TREE)
6309 result = null_pointer_node;
6310 else
6312 TREE_USED (label) = 1;
6313 result = build1 (ADDR_EXPR, ptr_type_node, label);
6314 /* The current function is not necessarily uninlinable.
6315 Computed gotos are incompatible with inlining, but the value
6316 here could be used only in a diagnostic, for example. */
6317 protected_set_expr_location (result, loc);
6320 return result;
6324 /* Given a boolean expression ARG, return a tree representing an increment
6325 or decrement (as indicated by CODE) of ARG. The front end must check for
6326 invalid cases (e.g., decrement in C++). */
6327 tree
6328 boolean_increment (enum tree_code code, tree arg)
6330 tree val;
6331 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6333 arg = stabilize_reference (arg);
6334 switch (code)
6336 case PREINCREMENT_EXPR:
6337 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6338 break;
6339 case POSTINCREMENT_EXPR:
6340 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6341 arg = save_expr (arg);
6342 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6343 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6344 break;
6345 case PREDECREMENT_EXPR:
6346 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6347 invert_truthvalue_loc (input_location, arg));
6348 break;
6349 case POSTDECREMENT_EXPR:
6350 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6351 invert_truthvalue_loc (input_location, arg));
6352 arg = save_expr (arg);
6353 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6354 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6355 break;
6356 default:
6357 gcc_unreachable ();
6359 TREE_SIDE_EFFECTS (val) = 1;
6360 return val;
6363 /* Built-in macros for stddef.h and stdint.h, that require macros
6364 defined in this file. */
6365 void
6366 c_stddef_cpp_builtins(void)
6368 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6369 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6370 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6371 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6372 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6373 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6374 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6375 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6376 if (SIG_ATOMIC_TYPE)
6377 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6378 if (INT8_TYPE)
6379 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6380 if (INT16_TYPE)
6381 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6382 if (INT32_TYPE)
6383 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6384 if (INT64_TYPE)
6385 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6386 if (UINT8_TYPE)
6387 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6388 if (UINT16_TYPE)
6389 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6390 if (UINT32_TYPE)
6391 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6392 if (UINT64_TYPE)
6393 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6394 if (INT_LEAST8_TYPE)
6395 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6396 if (INT_LEAST16_TYPE)
6397 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6398 if (INT_LEAST32_TYPE)
6399 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6400 if (INT_LEAST64_TYPE)
6401 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6402 if (UINT_LEAST8_TYPE)
6403 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6404 if (UINT_LEAST16_TYPE)
6405 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6406 if (UINT_LEAST32_TYPE)
6407 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6408 if (UINT_LEAST64_TYPE)
6409 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6410 if (INT_FAST8_TYPE)
6411 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6412 if (INT_FAST16_TYPE)
6413 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6414 if (INT_FAST32_TYPE)
6415 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6416 if (INT_FAST64_TYPE)
6417 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6418 if (UINT_FAST8_TYPE)
6419 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6420 if (UINT_FAST16_TYPE)
6421 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6422 if (UINT_FAST32_TYPE)
6423 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6424 if (UINT_FAST64_TYPE)
6425 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6426 if (INTPTR_TYPE)
6427 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6428 if (UINTPTR_TYPE)
6429 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6432 static void
6433 c_init_attributes (void)
6435 /* Fill in the built_in_attributes array. */
6436 #define DEF_ATTR_NULL_TREE(ENUM) \
6437 built_in_attributes[(int) ENUM] = NULL_TREE;
6438 #define DEF_ATTR_INT(ENUM, VALUE) \
6439 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6440 #define DEF_ATTR_STRING(ENUM, VALUE) \
6441 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6442 #define DEF_ATTR_IDENT(ENUM, STRING) \
6443 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6444 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6445 built_in_attributes[(int) ENUM] \
6446 = tree_cons (built_in_attributes[(int) PURPOSE], \
6447 built_in_attributes[(int) VALUE], \
6448 built_in_attributes[(int) CHAIN]);
6449 #include "builtin-attrs.def"
6450 #undef DEF_ATTR_NULL_TREE
6451 #undef DEF_ATTR_INT
6452 #undef DEF_ATTR_IDENT
6453 #undef DEF_ATTR_TREE_LIST
6456 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6457 identifier as an argument, so the front end shouldn't look it up. */
6459 bool
6460 attribute_takes_identifier_p (const_tree attr_id)
6462 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6463 if (spec == NULL)
6464 /* Unknown attribute that we'll end up ignoring, return true so we
6465 don't complain about an identifier argument. */
6466 return true;
6467 else if (!strcmp ("mode", spec->name)
6468 || !strcmp ("format", spec->name)
6469 || !strcmp ("cleanup", spec->name))
6470 return true;
6471 else
6472 return targetm.attribute_takes_identifier_p (attr_id);
6475 /* Attribute handlers common to C front ends. */
6477 /* Handle a "packed" attribute; arguments as in
6478 struct attribute_spec.handler. */
6480 static tree
6481 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6482 int flags, bool *no_add_attrs)
6484 if (TYPE_P (*node))
6486 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6487 *node = build_variant_type_copy (*node);
6488 TYPE_PACKED (*node) = 1;
6490 else if (TREE_CODE (*node) == FIELD_DECL)
6492 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6493 /* Still pack bitfields. */
6494 && ! DECL_INITIAL (*node))
6495 warning (OPT_Wattributes,
6496 "%qE attribute ignored for field of type %qT",
6497 name, TREE_TYPE (*node));
6498 else
6499 DECL_PACKED (*node) = 1;
6501 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6502 used for DECL_REGISTER. It wouldn't mean anything anyway.
6503 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6504 that changes what the typedef is typing. */
6505 else
6507 warning (OPT_Wattributes, "%qE attribute ignored", name);
6508 *no_add_attrs = true;
6511 return NULL_TREE;
6514 /* Handle a "nocommon" attribute; arguments as in
6515 struct attribute_spec.handler. */
6517 static tree
6518 handle_nocommon_attribute (tree *node, tree name,
6519 tree ARG_UNUSED (args),
6520 int ARG_UNUSED (flags), bool *no_add_attrs)
6522 if (TREE_CODE (*node) == VAR_DECL)
6523 DECL_COMMON (*node) = 0;
6524 else
6526 warning (OPT_Wattributes, "%qE attribute ignored", name);
6527 *no_add_attrs = true;
6530 return NULL_TREE;
6533 /* Handle a "common" attribute; arguments as in
6534 struct attribute_spec.handler. */
6536 static tree
6537 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6538 int ARG_UNUSED (flags), bool *no_add_attrs)
6540 if (TREE_CODE (*node) == VAR_DECL)
6541 DECL_COMMON (*node) = 1;
6542 else
6544 warning (OPT_Wattributes, "%qE attribute ignored", name);
6545 *no_add_attrs = true;
6548 return NULL_TREE;
6551 /* Handle a "noreturn" attribute; arguments as in
6552 struct attribute_spec.handler. */
6554 static tree
6555 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6556 int ARG_UNUSED (flags), bool *no_add_attrs)
6558 tree type = TREE_TYPE (*node);
6560 /* See FIXME comment in c_common_attribute_table. */
6561 if (TREE_CODE (*node) == FUNCTION_DECL
6562 || objc_method_decl (TREE_CODE (*node)))
6563 TREE_THIS_VOLATILE (*node) = 1;
6564 else if (TREE_CODE (type) == POINTER_TYPE
6565 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6566 TREE_TYPE (*node)
6567 = build_pointer_type
6568 (build_type_variant (TREE_TYPE (type),
6569 TYPE_READONLY (TREE_TYPE (type)), 1));
6570 else
6572 warning (OPT_Wattributes, "%qE attribute ignored", name);
6573 *no_add_attrs = true;
6576 return NULL_TREE;
6579 /* Handle a "hot" and attribute; arguments as in
6580 struct attribute_spec.handler. */
6582 static tree
6583 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6584 int ARG_UNUSED (flags), bool *no_add_attrs)
6586 if (TREE_CODE (*node) == FUNCTION_DECL
6587 || TREE_CODE (*node) == LABEL_DECL)
6589 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6591 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6592 name, "cold");
6593 *no_add_attrs = true;
6595 /* Most of the rest of the hot processing is done later with
6596 lookup_attribute. */
6598 else
6600 warning (OPT_Wattributes, "%qE attribute ignored", name);
6601 *no_add_attrs = true;
6604 return NULL_TREE;
6607 /* Handle a "cold" and attribute; arguments as in
6608 struct attribute_spec.handler. */
6610 static tree
6611 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6612 int ARG_UNUSED (flags), bool *no_add_attrs)
6614 if (TREE_CODE (*node) == FUNCTION_DECL
6615 || TREE_CODE (*node) == LABEL_DECL)
6617 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6619 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6620 name, "hot");
6621 *no_add_attrs = true;
6623 /* Most of the rest of the cold processing is done later with
6624 lookup_attribute. */
6626 else
6628 warning (OPT_Wattributes, "%qE attribute ignored", name);
6629 *no_add_attrs = true;
6632 return NULL_TREE;
6635 /* Handle a "no_sanitize_address" attribute; arguments as in
6636 struct attribute_spec.handler. */
6638 static tree
6639 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6640 bool *no_add_attrs)
6642 if (TREE_CODE (*node) != FUNCTION_DECL)
6644 warning (OPT_Wattributes, "%qE attribute ignored", name);
6645 *no_add_attrs = true;
6648 return NULL_TREE;
6651 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6652 struct attribute_spec.handler. */
6654 static tree
6655 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6656 bool *no_add_attrs)
6658 if (TREE_CODE (*node) != FUNCTION_DECL)
6659 warning (OPT_Wattributes, "%qE attribute ignored", name);
6660 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6661 DECL_ATTRIBUTES (*node)
6662 = tree_cons (get_identifier ("no_sanitize_address"),
6663 NULL_TREE, DECL_ATTRIBUTES (*node));
6664 *no_add_attrs = true;
6665 return NULL_TREE;
6668 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6669 struct attribute_spec.handler. */
6671 static tree
6672 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6673 bool *no_add_attrs)
6675 if (TREE_CODE (*node) != FUNCTION_DECL)
6677 warning (OPT_Wattributes, "%qE attribute ignored", name);
6678 *no_add_attrs = true;
6681 return NULL_TREE;
6684 /* Handle a "noinline" attribute; arguments as in
6685 struct attribute_spec.handler. */
6687 static tree
6688 handle_noinline_attribute (tree *node, tree name,
6689 tree ARG_UNUSED (args),
6690 int ARG_UNUSED (flags), bool *no_add_attrs)
6692 if (TREE_CODE (*node) == FUNCTION_DECL)
6693 DECL_UNINLINABLE (*node) = 1;
6694 else
6696 warning (OPT_Wattributes, "%qE attribute ignored", name);
6697 *no_add_attrs = true;
6700 return NULL_TREE;
6703 /* Handle a "noclone" attribute; arguments as in
6704 struct attribute_spec.handler. */
6706 static tree
6707 handle_noclone_attribute (tree *node, tree name,
6708 tree ARG_UNUSED (args),
6709 int ARG_UNUSED (flags), bool *no_add_attrs)
6711 if (TREE_CODE (*node) != FUNCTION_DECL)
6713 warning (OPT_Wattributes, "%qE attribute ignored", name);
6714 *no_add_attrs = true;
6717 return NULL_TREE;
6720 /* Handle a "always_inline" attribute; arguments as in
6721 struct attribute_spec.handler. */
6723 static tree
6724 handle_always_inline_attribute (tree *node, tree name,
6725 tree ARG_UNUSED (args),
6726 int ARG_UNUSED (flags),
6727 bool *no_add_attrs)
6729 if (TREE_CODE (*node) == FUNCTION_DECL)
6731 /* Set the attribute and mark it for disregarding inline
6732 limits. */
6733 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6735 else
6737 warning (OPT_Wattributes, "%qE attribute ignored", name);
6738 *no_add_attrs = true;
6741 return NULL_TREE;
6744 /* Handle a "gnu_inline" attribute; arguments as in
6745 struct attribute_spec.handler. */
6747 static tree
6748 handle_gnu_inline_attribute (tree *node, tree name,
6749 tree ARG_UNUSED (args),
6750 int ARG_UNUSED (flags),
6751 bool *no_add_attrs)
6753 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6755 /* Do nothing else, just set the attribute. We'll get at
6756 it later with lookup_attribute. */
6758 else
6760 warning (OPT_Wattributes, "%qE attribute ignored", name);
6761 *no_add_attrs = true;
6764 return NULL_TREE;
6767 /* Handle a "leaf" attribute; arguments as in
6768 struct attribute_spec.handler. */
6770 static tree
6771 handle_leaf_attribute (tree *node, tree name,
6772 tree ARG_UNUSED (args),
6773 int ARG_UNUSED (flags), bool *no_add_attrs)
6775 if (TREE_CODE (*node) != FUNCTION_DECL)
6777 warning (OPT_Wattributes, "%qE attribute ignored", name);
6778 *no_add_attrs = true;
6780 if (!TREE_PUBLIC (*node))
6782 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6783 *no_add_attrs = true;
6786 return NULL_TREE;
6789 /* Handle an "artificial" attribute; arguments as in
6790 struct attribute_spec.handler. */
6792 static tree
6793 handle_artificial_attribute (tree *node, tree name,
6794 tree ARG_UNUSED (args),
6795 int ARG_UNUSED (flags),
6796 bool *no_add_attrs)
6798 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6800 /* Do nothing else, just set the attribute. We'll get at
6801 it later with lookup_attribute. */
6803 else
6805 warning (OPT_Wattributes, "%qE attribute ignored", name);
6806 *no_add_attrs = true;
6809 return NULL_TREE;
6812 /* Handle a "flatten" attribute; arguments as in
6813 struct attribute_spec.handler. */
6815 static tree
6816 handle_flatten_attribute (tree *node, tree name,
6817 tree args ATTRIBUTE_UNUSED,
6818 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6820 if (TREE_CODE (*node) == FUNCTION_DECL)
6821 /* Do nothing else, just set the attribute. We'll get at
6822 it later with lookup_attribute. */
6824 else
6826 warning (OPT_Wattributes, "%qE attribute ignored", name);
6827 *no_add_attrs = true;
6830 return NULL_TREE;
6833 /* Handle a "warning" or "error" attribute; arguments as in
6834 struct attribute_spec.handler. */
6836 static tree
6837 handle_error_attribute (tree *node, tree name, tree args,
6838 int ARG_UNUSED (flags), bool *no_add_attrs)
6840 if (TREE_CODE (*node) == FUNCTION_DECL
6841 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
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 "used" attribute; arguments as in
6855 struct attribute_spec.handler. */
6857 static tree
6858 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6859 int ARG_UNUSED (flags), bool *no_add_attrs)
6861 tree node = *pnode;
6863 if (TREE_CODE (node) == FUNCTION_DECL
6864 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6865 || (TREE_CODE (node) == TYPE_DECL))
6867 TREE_USED (node) = 1;
6868 DECL_PRESERVE_P (node) = 1;
6869 if (TREE_CODE (node) == VAR_DECL)
6870 DECL_READ_P (node) = 1;
6872 else
6874 warning (OPT_Wattributes, "%qE attribute ignored", name);
6875 *no_add_attrs = true;
6878 return NULL_TREE;
6881 /* Handle a "unused" attribute; arguments as in
6882 struct attribute_spec.handler. */
6884 static tree
6885 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6886 int flags, bool *no_add_attrs)
6888 if (DECL_P (*node))
6890 tree decl = *node;
6892 if (TREE_CODE (decl) == PARM_DECL
6893 || TREE_CODE (decl) == VAR_DECL
6894 || TREE_CODE (decl) == FUNCTION_DECL
6895 || TREE_CODE (decl) == LABEL_DECL
6896 || TREE_CODE (decl) == TYPE_DECL)
6898 TREE_USED (decl) = 1;
6899 if (TREE_CODE (decl) == VAR_DECL
6900 || TREE_CODE (decl) == PARM_DECL)
6901 DECL_READ_P (decl) = 1;
6903 else
6905 warning (OPT_Wattributes, "%qE attribute ignored", name);
6906 *no_add_attrs = true;
6909 else
6911 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6912 *node = build_variant_type_copy (*node);
6913 TREE_USED (*node) = 1;
6916 return NULL_TREE;
6919 /* Handle a "externally_visible" attribute; arguments as in
6920 struct attribute_spec.handler. */
6922 static tree
6923 handle_externally_visible_attribute (tree *pnode, tree name,
6924 tree ARG_UNUSED (args),
6925 int ARG_UNUSED (flags),
6926 bool *no_add_attrs)
6928 tree node = *pnode;
6930 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6932 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6933 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6935 warning (OPT_Wattributes,
6936 "%qE attribute have effect only on public objects", name);
6937 *no_add_attrs = true;
6940 else
6942 warning (OPT_Wattributes, "%qE attribute ignored", name);
6943 *no_add_attrs = true;
6946 return NULL_TREE;
6949 /* Handle a "const" attribute; arguments as in
6950 struct attribute_spec.handler. */
6952 static tree
6953 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6954 int ARG_UNUSED (flags), bool *no_add_attrs)
6956 tree type = TREE_TYPE (*node);
6958 /* See FIXME comment on noreturn in c_common_attribute_table. */
6959 if (TREE_CODE (*node) == FUNCTION_DECL)
6960 TREE_READONLY (*node) = 1;
6961 else if (TREE_CODE (type) == POINTER_TYPE
6962 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6963 TREE_TYPE (*node)
6964 = build_pointer_type
6965 (build_type_variant (TREE_TYPE (type), 1,
6966 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6967 else
6969 warning (OPT_Wattributes, "%qE attribute ignored", name);
6970 *no_add_attrs = true;
6973 return NULL_TREE;
6976 /* Handle a "transparent_union" attribute; arguments as in
6977 struct attribute_spec.handler. */
6979 static tree
6980 handle_transparent_union_attribute (tree *node, tree name,
6981 tree ARG_UNUSED (args), int flags,
6982 bool *no_add_attrs)
6984 tree type;
6986 *no_add_attrs = true;
6989 if (TREE_CODE (*node) == TYPE_DECL
6990 && ! (flags & ATTR_FLAG_CXX11))
6991 node = &TREE_TYPE (*node);
6992 type = *node;
6994 if (TREE_CODE (type) == UNION_TYPE)
6996 /* Make sure that the first field will work for a transparent union.
6997 If the type isn't complete yet, leave the check to the code in
6998 finish_struct. */
6999 if (TYPE_SIZE (type))
7001 tree first = first_field (type);
7002 if (first == NULL_TREE
7003 || DECL_ARTIFICIAL (first)
7004 || TYPE_MODE (type) != DECL_MODE (first))
7005 goto ignored;
7008 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7010 /* If the type isn't complete yet, setting the flag
7011 on a variant wouldn't ever be checked. */
7012 if (!TYPE_SIZE (type))
7013 goto ignored;
7015 /* build_duplicate_type doesn't work for C++. */
7016 if (c_dialect_cxx ())
7017 goto ignored;
7019 /* A type variant isn't good enough, since we don't a cast
7020 to such a type removed as a no-op. */
7021 *node = type = build_duplicate_type (type);
7024 TYPE_TRANSPARENT_AGGR (type) = 1;
7025 return NULL_TREE;
7028 ignored:
7029 warning (OPT_Wattributes, "%qE attribute ignored", name);
7030 return NULL_TREE;
7033 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7034 get the requested priority for a constructor or destructor,
7035 possibly issuing diagnostics for invalid or reserved
7036 priorities. */
7038 static priority_type
7039 get_priority (tree args, bool is_destructor)
7041 HOST_WIDE_INT pri;
7042 tree arg;
7044 if (!args)
7045 return DEFAULT_INIT_PRIORITY;
7047 if (!SUPPORTS_INIT_PRIORITY)
7049 if (is_destructor)
7050 error ("destructor priorities are not supported");
7051 else
7052 error ("constructor priorities are not supported");
7053 return DEFAULT_INIT_PRIORITY;
7056 arg = TREE_VALUE (args);
7057 arg = default_conversion (arg);
7058 if (!host_integerp (arg, /*pos=*/0)
7059 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7060 goto invalid;
7062 pri = tree_low_cst (arg, /*pos=*/0);
7063 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7064 goto invalid;
7066 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7068 if (is_destructor)
7069 warning (0,
7070 "destructor priorities from 0 to %d are reserved "
7071 "for the implementation",
7072 MAX_RESERVED_INIT_PRIORITY);
7073 else
7074 warning (0,
7075 "constructor priorities from 0 to %d are reserved "
7076 "for the implementation",
7077 MAX_RESERVED_INIT_PRIORITY);
7079 return pri;
7081 invalid:
7082 if (is_destructor)
7083 error ("destructor priorities must be integers from 0 to %d inclusive",
7084 MAX_INIT_PRIORITY);
7085 else
7086 error ("constructor priorities must be integers from 0 to %d inclusive",
7087 MAX_INIT_PRIORITY);
7088 return DEFAULT_INIT_PRIORITY;
7091 /* Handle a "constructor" attribute; arguments as in
7092 struct attribute_spec.handler. */
7094 static tree
7095 handle_constructor_attribute (tree *node, tree name, tree args,
7096 int ARG_UNUSED (flags),
7097 bool *no_add_attrs)
7099 tree decl = *node;
7100 tree type = TREE_TYPE (decl);
7102 if (TREE_CODE (decl) == FUNCTION_DECL
7103 && TREE_CODE (type) == FUNCTION_TYPE
7104 && decl_function_context (decl) == 0)
7106 priority_type priority;
7107 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7108 priority = get_priority (args, /*is_destructor=*/false);
7109 SET_DECL_INIT_PRIORITY (decl, priority);
7110 TREE_USED (decl) = 1;
7112 else
7114 warning (OPT_Wattributes, "%qE attribute ignored", name);
7115 *no_add_attrs = true;
7118 return NULL_TREE;
7121 /* Handle a "destructor" attribute; arguments as in
7122 struct attribute_spec.handler. */
7124 static tree
7125 handle_destructor_attribute (tree *node, tree name, tree args,
7126 int ARG_UNUSED (flags),
7127 bool *no_add_attrs)
7129 tree decl = *node;
7130 tree type = TREE_TYPE (decl);
7132 if (TREE_CODE (decl) == FUNCTION_DECL
7133 && TREE_CODE (type) == FUNCTION_TYPE
7134 && decl_function_context (decl) == 0)
7136 priority_type priority;
7137 DECL_STATIC_DESTRUCTOR (decl) = 1;
7138 priority = get_priority (args, /*is_destructor=*/true);
7139 SET_DECL_FINI_PRIORITY (decl, priority);
7140 TREE_USED (decl) = 1;
7142 else
7144 warning (OPT_Wattributes, "%qE attribute ignored", name);
7145 *no_add_attrs = true;
7148 return NULL_TREE;
7151 /* Nonzero if the mode is a valid vector mode for this architecture.
7152 This returns nonzero even if there is no hardware support for the
7153 vector mode, but we can emulate with narrower modes. */
7155 static int
7156 vector_mode_valid_p (enum machine_mode mode)
7158 enum mode_class mclass = GET_MODE_CLASS (mode);
7159 enum machine_mode innermode;
7161 /* Doh! What's going on? */
7162 if (mclass != MODE_VECTOR_INT
7163 && mclass != MODE_VECTOR_FLOAT
7164 && mclass != MODE_VECTOR_FRACT
7165 && mclass != MODE_VECTOR_UFRACT
7166 && mclass != MODE_VECTOR_ACCUM
7167 && mclass != MODE_VECTOR_UACCUM)
7168 return 0;
7170 /* Hardware support. Woo hoo! */
7171 if (targetm.vector_mode_supported_p (mode))
7172 return 1;
7174 innermode = GET_MODE_INNER (mode);
7176 /* We should probably return 1 if requesting V4DI and we have no DI,
7177 but we have V2DI, but this is probably very unlikely. */
7179 /* If we have support for the inner mode, we can safely emulate it.
7180 We may not have V2DI, but me can emulate with a pair of DIs. */
7181 return targetm.scalar_mode_supported_p (innermode);
7185 /* Handle a "mode" attribute; arguments as in
7186 struct attribute_spec.handler. */
7188 static tree
7189 handle_mode_attribute (tree *node, tree name, tree args,
7190 int ARG_UNUSED (flags), bool *no_add_attrs)
7192 tree type = *node;
7193 tree ident = TREE_VALUE (args);
7195 *no_add_attrs = true;
7197 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7198 warning (OPT_Wattributes, "%qE attribute ignored", name);
7199 else
7201 int j;
7202 const char *p = IDENTIFIER_POINTER (ident);
7203 int len = strlen (p);
7204 enum machine_mode mode = VOIDmode;
7205 tree typefm;
7206 bool valid_mode;
7208 if (len > 4 && p[0] == '_' && p[1] == '_'
7209 && p[len - 1] == '_' && p[len - 2] == '_')
7211 char *newp = (char *) alloca (len - 1);
7213 strcpy (newp, &p[2]);
7214 newp[len - 4] = '\0';
7215 p = newp;
7218 /* Change this type to have a type with the specified mode.
7219 First check for the special modes. */
7220 if (!strcmp (p, "byte"))
7221 mode = byte_mode;
7222 else if (!strcmp (p, "word"))
7223 mode = word_mode;
7224 else if (!strcmp (p, "pointer"))
7225 mode = ptr_mode;
7226 else if (!strcmp (p, "libgcc_cmp_return"))
7227 mode = targetm.libgcc_cmp_return_mode ();
7228 else if (!strcmp (p, "libgcc_shift_count"))
7229 mode = targetm.libgcc_shift_count_mode ();
7230 else if (!strcmp (p, "unwind_word"))
7231 mode = targetm.unwind_word_mode ();
7232 else
7233 for (j = 0; j < NUM_MACHINE_MODES; j++)
7234 if (!strcmp (p, GET_MODE_NAME (j)))
7236 mode = (enum machine_mode) j;
7237 break;
7240 if (mode == VOIDmode)
7242 error ("unknown machine mode %qE", ident);
7243 return NULL_TREE;
7246 valid_mode = false;
7247 switch (GET_MODE_CLASS (mode))
7249 case MODE_INT:
7250 case MODE_PARTIAL_INT:
7251 case MODE_FLOAT:
7252 case MODE_DECIMAL_FLOAT:
7253 case MODE_FRACT:
7254 case MODE_UFRACT:
7255 case MODE_ACCUM:
7256 case MODE_UACCUM:
7257 valid_mode = targetm.scalar_mode_supported_p (mode);
7258 break;
7260 case MODE_COMPLEX_INT:
7261 case MODE_COMPLEX_FLOAT:
7262 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7263 break;
7265 case MODE_VECTOR_INT:
7266 case MODE_VECTOR_FLOAT:
7267 case MODE_VECTOR_FRACT:
7268 case MODE_VECTOR_UFRACT:
7269 case MODE_VECTOR_ACCUM:
7270 case MODE_VECTOR_UACCUM:
7271 warning (OPT_Wattributes, "specifying vector types with "
7272 "__attribute__ ((mode)) is deprecated");
7273 warning (OPT_Wattributes,
7274 "use __attribute__ ((vector_size)) instead");
7275 valid_mode = vector_mode_valid_p (mode);
7276 break;
7278 default:
7279 break;
7281 if (!valid_mode)
7283 error ("unable to emulate %qs", p);
7284 return NULL_TREE;
7287 if (POINTER_TYPE_P (type))
7289 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7290 tree (*fn)(tree, enum machine_mode, bool);
7292 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7294 error ("invalid pointer mode %qs", p);
7295 return NULL_TREE;
7298 if (TREE_CODE (type) == POINTER_TYPE)
7299 fn = build_pointer_type_for_mode;
7300 else
7301 fn = build_reference_type_for_mode;
7302 typefm = fn (TREE_TYPE (type), mode, false);
7304 else
7306 /* For fixed-point modes, we need to test if the signness of type
7307 and the machine mode are consistent. */
7308 if (ALL_FIXED_POINT_MODE_P (mode)
7309 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7311 error ("signedness of type and machine mode %qs don%'t match", p);
7312 return NULL_TREE;
7314 /* For fixed-point modes, we need to pass saturating info. */
7315 typefm = lang_hooks.types.type_for_mode (mode,
7316 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7317 : TYPE_UNSIGNED (type));
7320 if (typefm == NULL_TREE)
7322 error ("no data type for mode %qs", p);
7323 return NULL_TREE;
7325 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7327 /* For enumeral types, copy the precision from the integer
7328 type returned above. If not an INTEGER_TYPE, we can't use
7329 this mode for this type. */
7330 if (TREE_CODE (typefm) != INTEGER_TYPE)
7332 error ("cannot use mode %qs for enumeral types", p);
7333 return NULL_TREE;
7336 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7338 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7339 typefm = type;
7341 else
7343 /* We cannot build a type variant, as there's code that assumes
7344 that TYPE_MAIN_VARIANT has the same mode. This includes the
7345 debug generators. Instead, create a subrange type. This
7346 results in all of the enumeral values being emitted only once
7347 in the original, and the subtype gets them by reference. */
7348 if (TYPE_UNSIGNED (type))
7349 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7350 else
7351 typefm = make_signed_type (TYPE_PRECISION (typefm));
7352 TREE_TYPE (typefm) = type;
7355 else if (VECTOR_MODE_P (mode)
7356 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7357 : TREE_CODE (type) != TREE_CODE (typefm))
7359 error ("mode %qs applied to inappropriate type", p);
7360 return NULL_TREE;
7363 *node = typefm;
7366 return NULL_TREE;
7369 /* Handle a "section" attribute; arguments as in
7370 struct attribute_spec.handler. */
7372 static tree
7373 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7374 int ARG_UNUSED (flags), bool *no_add_attrs)
7376 tree decl = *node;
7378 if (targetm_common.have_named_sections)
7380 user_defined_section_attribute = true;
7382 if ((TREE_CODE (decl) == FUNCTION_DECL
7383 || TREE_CODE (decl) == VAR_DECL)
7384 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7386 if (TREE_CODE (decl) == VAR_DECL
7387 && current_function_decl != NULL_TREE
7388 && !TREE_STATIC (decl))
7390 error_at (DECL_SOURCE_LOCATION (decl),
7391 "section attribute cannot be specified for "
7392 "local variables");
7393 *no_add_attrs = true;
7396 /* The decl may have already been given a section attribute
7397 from a previous declaration. Ensure they match. */
7398 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7399 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7400 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7402 error ("section of %q+D conflicts with previous declaration",
7403 *node);
7404 *no_add_attrs = true;
7406 else if (TREE_CODE (decl) == VAR_DECL
7407 && !targetm.have_tls && targetm.emutls.tmpl_section
7408 && DECL_THREAD_LOCAL_P (decl))
7410 error ("section of %q+D cannot be overridden", *node);
7411 *no_add_attrs = true;
7413 else
7414 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7416 else
7418 error ("section attribute not allowed for %q+D", *node);
7419 *no_add_attrs = true;
7422 else
7424 error_at (DECL_SOURCE_LOCATION (*node),
7425 "section attributes are not supported for this target");
7426 *no_add_attrs = true;
7429 return NULL_TREE;
7432 /* Check whether ALIGN is a valid user-specified alignment. If so,
7433 return its base-2 log; if not, output an error and return -1. If
7434 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7435 no error. */
7437 check_user_alignment (const_tree align, bool allow_zero)
7439 int i;
7441 if (TREE_CODE (align) != INTEGER_CST
7442 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7444 error ("requested alignment is not an integer constant");
7445 return -1;
7447 else if (allow_zero && integer_zerop (align))
7448 return -1;
7449 else if (tree_int_cst_sgn (align) == -1
7450 || (i = tree_log2 (align)) == -1)
7452 error ("requested alignment is not a positive power of 2");
7453 return -1;
7455 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7457 error ("requested alignment is too large");
7458 return -1;
7460 return i;
7464 If in c++-11, check if the c++-11 alignment constraint with respect
7465 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7466 c++-11 mode, does nothing.
7468 [dcl.align]2/ says:
7470 [* if the constant expression evaluates to a fundamental alignment,
7471 the alignment requirement of the declared entity shall be the
7472 specified fundamental alignment.
7474 * if the constant expression evaluates to an extended alignment
7475 and the implementation supports that alignment in the context
7476 of the declaration, the alignment of the declared entity shall
7477 be that alignment
7479 * if the constant expression evaluates to an extended alignment
7480 and the implementation does not support that alignment in the
7481 context of the declaration, the program is ill-formed]. */
7483 static bool
7484 check_cxx_fundamental_alignment_constraints (tree node,
7485 unsigned align_log,
7486 int flags)
7488 bool alignment_too_large_p = false;
7489 unsigned requested_alignment = 1U << align_log;
7490 unsigned max_align = 0;
7492 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7493 || (node == NULL_TREE || node == error_mark_node))
7494 return true;
7496 if (cxx_fundamental_alignment_p (requested_alignment))
7497 return true;
7499 if (DECL_P (node))
7501 if (TREE_STATIC (node))
7503 /* For file scope variables and static members, the target
7504 supports alignments that are at most
7505 MAX_OFILE_ALIGNMENT. */
7506 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7507 alignment_too_large_p = true;
7509 else
7511 #ifdef BIGGEST_FIELD_ALIGNMENT
7512 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7513 #else
7514 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7515 #endif
7516 /* For non-static members, the target supports either
7517 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7518 if it is defined or BIGGEST_ALIGNMENT. */
7519 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7520 if (TREE_CODE (node) == FIELD_DECL
7521 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7522 alignment_too_large_p = true;
7523 #undef MAX_TARGET_FIELD_ALIGNMENT
7524 /* For stack variables, the target supports at most
7525 MAX_STACK_ALIGNMENT. */
7526 else if (decl_function_context (node) != NULL
7527 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7528 alignment_too_large_p = true;
7531 else if (TYPE_P (node))
7533 /* Let's be liberal for types. */
7534 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7535 alignment_too_large_p = true;
7538 if (alignment_too_large_p)
7539 pedwarn (input_location, OPT_Wattributes,
7540 "requested alignment %d is larger than %d",
7541 requested_alignment, max_align);
7543 return !alignment_too_large_p;
7546 /* Handle a "aligned" attribute; arguments as in
7547 struct attribute_spec.handler. */
7549 static tree
7550 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7551 int flags, bool *no_add_attrs)
7553 tree decl = NULL_TREE;
7554 tree *type = NULL;
7555 int is_type = 0;
7556 tree align_expr = (args ? TREE_VALUE (args)
7557 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7558 int i;
7560 if (DECL_P (*node))
7562 decl = *node;
7563 type = &TREE_TYPE (decl);
7564 is_type = TREE_CODE (*node) == TYPE_DECL;
7566 else if (TYPE_P (*node))
7567 type = node, is_type = 1;
7569 if ((i = check_user_alignment (align_expr, false)) == -1
7570 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7571 *no_add_attrs = true;
7572 else if (is_type)
7574 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7575 /* OK, modify the type in place. */;
7576 /* If we have a TYPE_DECL, then copy the type, so that we
7577 don't accidentally modify a builtin type. See pushdecl. */
7578 else if (decl && TREE_TYPE (decl) != error_mark_node
7579 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7581 tree tt = TREE_TYPE (decl);
7582 *type = build_variant_type_copy (*type);
7583 DECL_ORIGINAL_TYPE (decl) = tt;
7584 TYPE_NAME (*type) = decl;
7585 TREE_USED (*type) = TREE_USED (decl);
7586 TREE_TYPE (decl) = *type;
7588 else
7589 *type = build_variant_type_copy (*type);
7591 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7592 TYPE_USER_ALIGN (*type) = 1;
7594 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7595 && TREE_CODE (decl) != FIELD_DECL)
7597 error ("alignment may not be specified for %q+D", decl);
7598 *no_add_attrs = true;
7600 else if (DECL_USER_ALIGN (decl)
7601 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7602 /* C++-11 [dcl.align/4]:
7604 When multiple alignment-specifiers are specified for an
7605 entity, the alignment requirement shall be set to the
7606 strictest specified alignment.
7608 This formally comes from the c++11 specification but we are
7609 doing it for the GNU attribute syntax as well. */
7610 *no_add_attrs = true;
7611 else if (TREE_CODE (decl) == FUNCTION_DECL
7612 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7614 if (DECL_USER_ALIGN (decl))
7615 error ("alignment for %q+D was previously specified as %d "
7616 "and may not be decreased", decl,
7617 DECL_ALIGN (decl) / BITS_PER_UNIT);
7618 else
7619 error ("alignment for %q+D must be at least %d", decl,
7620 DECL_ALIGN (decl) / BITS_PER_UNIT);
7621 *no_add_attrs = true;
7623 else
7625 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7626 DECL_USER_ALIGN (decl) = 1;
7629 return NULL_TREE;
7632 /* Handle a "weak" attribute; arguments as in
7633 struct attribute_spec.handler. */
7635 static tree
7636 handle_weak_attribute (tree *node, tree name,
7637 tree ARG_UNUSED (args),
7638 int ARG_UNUSED (flags),
7639 bool * ARG_UNUSED (no_add_attrs))
7641 if (TREE_CODE (*node) == FUNCTION_DECL
7642 && DECL_DECLARED_INLINE_P (*node))
7644 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7645 *no_add_attrs = true;
7647 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7649 error ("indirect function %q+D cannot be declared weak", *node);
7650 *no_add_attrs = true;
7651 return NULL_TREE;
7653 else if (TREE_CODE (*node) == FUNCTION_DECL
7654 || TREE_CODE (*node) == VAR_DECL)
7655 declare_weak (*node);
7656 else
7657 warning (OPT_Wattributes, "%qE attribute ignored", name);
7659 return NULL_TREE;
7662 /* Handle an "alias" or "ifunc" attribute; arguments as in
7663 struct attribute_spec.handler, except that IS_ALIAS tells us
7664 whether this is an alias as opposed to ifunc attribute. */
7666 static tree
7667 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7668 bool *no_add_attrs)
7670 tree decl = *node;
7672 if (TREE_CODE (decl) != FUNCTION_DECL
7673 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7675 warning (OPT_Wattributes, "%qE attribute ignored", name);
7676 *no_add_attrs = true;
7678 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7679 || (TREE_CODE (decl) != FUNCTION_DECL
7680 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7681 /* A static variable declaration is always a tentative definition,
7682 but the alias is a non-tentative definition which overrides. */
7683 || (TREE_CODE (decl) != FUNCTION_DECL
7684 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7686 error ("%q+D defined both normally and as %qE attribute", decl, name);
7687 *no_add_attrs = true;
7688 return NULL_TREE;
7690 else if (!is_alias
7691 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7692 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7694 error ("weak %q+D cannot be defined %qE", decl, name);
7695 *no_add_attrs = true;
7696 return NULL_TREE;
7699 /* Note that the very first time we process a nested declaration,
7700 decl_function_context will not be set. Indeed, *would* never
7701 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7702 we do below. After such frobbery, pushdecl would set the context.
7703 In any case, this is never what we want. */
7704 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7706 tree id;
7708 id = TREE_VALUE (args);
7709 if (TREE_CODE (id) != STRING_CST)
7711 error ("attribute %qE argument not a string", name);
7712 *no_add_attrs = true;
7713 return NULL_TREE;
7715 id = get_identifier (TREE_STRING_POINTER (id));
7716 /* This counts as a use of the object pointed to. */
7717 TREE_USED (id) = 1;
7719 if (TREE_CODE (decl) == FUNCTION_DECL)
7720 DECL_INITIAL (decl) = error_mark_node;
7721 else
7722 TREE_STATIC (decl) = 1;
7724 if (!is_alias)
7725 /* ifuncs are also aliases, so set that attribute too. */
7726 DECL_ATTRIBUTES (decl)
7727 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7729 else
7731 warning (OPT_Wattributes, "%qE attribute ignored", name);
7732 *no_add_attrs = true;
7735 return NULL_TREE;
7738 /* Handle an "alias" or "ifunc" attribute; arguments as in
7739 struct attribute_spec.handler. */
7741 static tree
7742 handle_ifunc_attribute (tree *node, tree name, tree args,
7743 int ARG_UNUSED (flags), bool *no_add_attrs)
7745 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7748 /* Handle an "alias" or "ifunc" attribute; arguments as in
7749 struct attribute_spec.handler. */
7751 static tree
7752 handle_alias_attribute (tree *node, tree name, tree args,
7753 int ARG_UNUSED (flags), bool *no_add_attrs)
7755 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7758 /* Handle a "weakref" attribute; arguments as in struct
7759 attribute_spec.handler. */
7761 static tree
7762 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7763 int flags, bool *no_add_attrs)
7765 tree attr = NULL_TREE;
7767 /* We must ignore the attribute when it is associated with
7768 local-scoped decls, since attribute alias is ignored and many
7769 such symbols do not even have a DECL_WEAK field. */
7770 if (decl_function_context (*node)
7771 || current_function_decl
7772 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7774 warning (OPT_Wattributes, "%qE attribute ignored", name);
7775 *no_add_attrs = true;
7776 return NULL_TREE;
7779 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7781 error ("indirect function %q+D cannot be declared weakref", *node);
7782 *no_add_attrs = true;
7783 return NULL_TREE;
7786 /* The idea here is that `weakref("name")' mutates into `weakref,
7787 alias("name")', and weakref without arguments, in turn,
7788 implicitly adds weak. */
7790 if (args)
7792 attr = tree_cons (get_identifier ("alias"), args, attr);
7793 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7795 *no_add_attrs = true;
7797 decl_attributes (node, attr, flags);
7799 else
7801 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7802 error_at (DECL_SOURCE_LOCATION (*node),
7803 "weakref attribute must appear before alias attribute");
7805 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7806 and that isn't supported; and because it wants to add it to
7807 the list of weak decls, which isn't helpful. */
7808 DECL_WEAK (*node) = 1;
7811 return NULL_TREE;
7814 /* Handle an "visibility" attribute; arguments as in
7815 struct attribute_spec.handler. */
7817 static tree
7818 handle_visibility_attribute (tree *node, tree name, tree args,
7819 int ARG_UNUSED (flags),
7820 bool *ARG_UNUSED (no_add_attrs))
7822 tree decl = *node;
7823 tree id = TREE_VALUE (args);
7824 enum symbol_visibility vis;
7826 if (TYPE_P (*node))
7828 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7829 /* OK */;
7830 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7832 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7833 name);
7834 return NULL_TREE;
7836 else if (TYPE_FIELDS (*node))
7838 error ("%qE attribute ignored because %qT is already defined",
7839 name, *node);
7840 return NULL_TREE;
7843 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7845 warning (OPT_Wattributes, "%qE attribute ignored", name);
7846 return NULL_TREE;
7849 if (TREE_CODE (id) != STRING_CST)
7851 error ("visibility argument not a string");
7852 return NULL_TREE;
7855 /* If this is a type, set the visibility on the type decl. */
7856 if (TYPE_P (decl))
7858 decl = TYPE_NAME (decl);
7859 if (!decl)
7860 return NULL_TREE;
7861 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7863 warning (OPT_Wattributes, "%qE attribute ignored on types",
7864 name);
7865 return NULL_TREE;
7869 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7870 vis = VISIBILITY_DEFAULT;
7871 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7872 vis = VISIBILITY_INTERNAL;
7873 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7874 vis = VISIBILITY_HIDDEN;
7875 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7876 vis = VISIBILITY_PROTECTED;
7877 else
7879 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7880 vis = VISIBILITY_DEFAULT;
7883 if (DECL_VISIBILITY_SPECIFIED (decl)
7884 && vis != DECL_VISIBILITY (decl))
7886 tree attributes = (TYPE_P (*node)
7887 ? TYPE_ATTRIBUTES (*node)
7888 : DECL_ATTRIBUTES (decl));
7889 if (lookup_attribute ("visibility", attributes))
7890 error ("%qD redeclared with different visibility", decl);
7891 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7892 && lookup_attribute ("dllimport", attributes))
7893 error ("%qD was declared %qs which implies default visibility",
7894 decl, "dllimport");
7895 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7896 && lookup_attribute ("dllexport", attributes))
7897 error ("%qD was declared %qs which implies default visibility",
7898 decl, "dllexport");
7901 DECL_VISIBILITY (decl) = vis;
7902 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7904 /* Go ahead and attach the attribute to the node as well. This is needed
7905 so we can determine whether we have VISIBILITY_DEFAULT because the
7906 visibility was not specified, or because it was explicitly overridden
7907 from the containing scope. */
7909 return NULL_TREE;
7912 /* Determine the ELF symbol visibility for DECL, which is either a
7913 variable or a function. It is an error to use this function if a
7914 definition of DECL is not available in this translation unit.
7915 Returns true if the final visibility has been determined by this
7916 function; false if the caller is free to make additional
7917 modifications. */
7919 bool
7920 c_determine_visibility (tree decl)
7922 gcc_assert (TREE_CODE (decl) == VAR_DECL
7923 || TREE_CODE (decl) == FUNCTION_DECL);
7925 /* If the user explicitly specified the visibility with an
7926 attribute, honor that. DECL_VISIBILITY will have been set during
7927 the processing of the attribute. We check for an explicit
7928 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7929 to distinguish the use of an attribute from the use of a "#pragma
7930 GCC visibility push(...)"; in the latter case we still want other
7931 considerations to be able to overrule the #pragma. */
7932 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7933 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7934 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7935 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7936 return true;
7938 /* Set default visibility to whatever the user supplied with
7939 visibility_specified depending on #pragma GCC visibility. */
7940 if (!DECL_VISIBILITY_SPECIFIED (decl))
7942 if (visibility_options.inpragma
7943 || DECL_VISIBILITY (decl) != default_visibility)
7945 DECL_VISIBILITY (decl) = default_visibility;
7946 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7947 /* If visibility changed and DECL already has DECL_RTL, ensure
7948 symbol flags are updated. */
7949 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7950 || TREE_CODE (decl) == FUNCTION_DECL)
7951 && DECL_RTL_SET_P (decl))
7952 make_decl_rtl (decl);
7955 return false;
7958 /* Handle an "tls_model" attribute; arguments as in
7959 struct attribute_spec.handler. */
7961 static tree
7962 handle_tls_model_attribute (tree *node, tree name, tree args,
7963 int ARG_UNUSED (flags), bool *no_add_attrs)
7965 tree id;
7966 tree decl = *node;
7967 enum tls_model kind;
7969 *no_add_attrs = true;
7971 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7973 warning (OPT_Wattributes, "%qE attribute ignored", name);
7974 return NULL_TREE;
7977 kind = DECL_TLS_MODEL (decl);
7978 id = TREE_VALUE (args);
7979 if (TREE_CODE (id) != STRING_CST)
7981 error ("tls_model argument not a string");
7982 return NULL_TREE;
7985 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7986 kind = TLS_MODEL_LOCAL_EXEC;
7987 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7988 kind = TLS_MODEL_INITIAL_EXEC;
7989 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7990 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7991 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7992 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7993 else
7994 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7996 DECL_TLS_MODEL (decl) = kind;
7997 return NULL_TREE;
8000 /* Handle a "no_instrument_function" attribute; arguments as in
8001 struct attribute_spec.handler. */
8003 static tree
8004 handle_no_instrument_function_attribute (tree *node, tree name,
8005 tree ARG_UNUSED (args),
8006 int ARG_UNUSED (flags),
8007 bool *no_add_attrs)
8009 tree decl = *node;
8011 if (TREE_CODE (decl) != FUNCTION_DECL)
8013 error_at (DECL_SOURCE_LOCATION (decl),
8014 "%qE attribute applies only to functions", name);
8015 *no_add_attrs = true;
8017 else if (DECL_INITIAL (decl))
8019 error_at (DECL_SOURCE_LOCATION (decl),
8020 "can%'t set %qE attribute after definition", name);
8021 *no_add_attrs = true;
8023 else
8024 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8026 return NULL_TREE;
8029 /* Handle a "malloc" attribute; arguments as in
8030 struct attribute_spec.handler. */
8032 static tree
8033 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8034 int ARG_UNUSED (flags), bool *no_add_attrs)
8036 if (TREE_CODE (*node) == FUNCTION_DECL
8037 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8038 DECL_IS_MALLOC (*node) = 1;
8039 else
8041 warning (OPT_Wattributes, "%qE attribute ignored", name);
8042 *no_add_attrs = true;
8045 return NULL_TREE;
8048 /* Handle a "alloc_size" attribute; arguments as in
8049 struct attribute_spec.handler. */
8051 static tree
8052 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8053 int ARG_UNUSED (flags), bool *no_add_attrs)
8055 unsigned arg_count = type_num_arguments (*node);
8056 for (; args; args = TREE_CHAIN (args))
8058 tree position = TREE_VALUE (args);
8060 if (TREE_CODE (position) != INTEGER_CST
8061 || TREE_INT_CST_HIGH (position)
8062 || TREE_INT_CST_LOW (position) < 1
8063 || TREE_INT_CST_LOW (position) > arg_count )
8065 warning (OPT_Wattributes,
8066 "alloc_size parameter outside range");
8067 *no_add_attrs = true;
8068 return NULL_TREE;
8071 return NULL_TREE;
8074 /* Handle a "fn spec" attribute; arguments as in
8075 struct attribute_spec.handler. */
8077 static tree
8078 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8079 tree args, int ARG_UNUSED (flags),
8080 bool *no_add_attrs ATTRIBUTE_UNUSED)
8082 gcc_assert (args
8083 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8084 && !TREE_CHAIN (args));
8085 return NULL_TREE;
8088 /* Handle a "bnd_variable_size" attribute; arguments as in
8089 struct attribute_spec.handler. */
8091 static tree
8092 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8093 int ARG_UNUSED (flags), bool *no_add_attrs)
8095 if (TREE_CODE (*node) != FIELD_DECL)
8097 warning (OPT_Wattributes, "%qE attribute ignored", name);
8098 *no_add_attrs = true;
8101 return NULL_TREE;
8104 /* Handle a "bnd_legacy" attribute; arguments as in
8105 struct attribute_spec.handler. */
8107 static tree
8108 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8109 int ARG_UNUSED (flags), bool *no_add_attrs)
8111 if (TREE_CODE (*node) != FUNCTION_DECL)
8113 warning (OPT_Wattributes, "%qE attribute ignored", name);
8114 *no_add_attrs = true;
8117 return NULL_TREE;
8120 /* Handle a "warn_unused" attribute; arguments as in
8121 struct attribute_spec.handler. */
8123 static tree
8124 handle_warn_unused_attribute (tree *node, tree name,
8125 tree args ATTRIBUTE_UNUSED,
8126 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8128 if (TYPE_P (*node))
8129 /* Do nothing else, just set the attribute. We'll get at
8130 it later with lookup_attribute. */
8132 else
8134 warning (OPT_Wattributes, "%qE attribute ignored", name);
8135 *no_add_attrs = true;
8138 return NULL_TREE;
8141 /* Handle an "omp declare simd" attribute; arguments as in
8142 struct attribute_spec.handler. */
8144 static tree
8145 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8147 return NULL_TREE;
8150 /* Handle an "omp declare target" attribute; arguments as in
8151 struct attribute_spec.handler. */
8153 static tree
8154 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8156 return NULL_TREE;
8159 /* Handle a "returns_twice" attribute; arguments as in
8160 struct attribute_spec.handler. */
8162 static tree
8163 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8164 int ARG_UNUSED (flags), bool *no_add_attrs)
8166 if (TREE_CODE (*node) == FUNCTION_DECL)
8167 DECL_IS_RETURNS_TWICE (*node) = 1;
8168 else
8170 warning (OPT_Wattributes, "%qE attribute ignored", name);
8171 *no_add_attrs = true;
8174 return NULL_TREE;
8177 /* Handle a "no_limit_stack" attribute; arguments as in
8178 struct attribute_spec.handler. */
8180 static tree
8181 handle_no_limit_stack_attribute (tree *node, tree name,
8182 tree ARG_UNUSED (args),
8183 int ARG_UNUSED (flags),
8184 bool *no_add_attrs)
8186 tree decl = *node;
8188 if (TREE_CODE (decl) != FUNCTION_DECL)
8190 error_at (DECL_SOURCE_LOCATION (decl),
8191 "%qE attribute applies only to functions", name);
8192 *no_add_attrs = true;
8194 else if (DECL_INITIAL (decl))
8196 error_at (DECL_SOURCE_LOCATION (decl),
8197 "can%'t set %qE attribute after definition", name);
8198 *no_add_attrs = true;
8200 else
8201 DECL_NO_LIMIT_STACK (decl) = 1;
8203 return NULL_TREE;
8206 /* Handle a "pure" attribute; arguments as in
8207 struct attribute_spec.handler. */
8209 static tree
8210 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8211 int ARG_UNUSED (flags), bool *no_add_attrs)
8213 if (TREE_CODE (*node) == FUNCTION_DECL)
8214 DECL_PURE_P (*node) = 1;
8215 /* ??? TODO: Support types. */
8216 else
8218 warning (OPT_Wattributes, "%qE attribute ignored", name);
8219 *no_add_attrs = true;
8222 return NULL_TREE;
8225 /* Digest an attribute list destined for a transactional memory statement.
8226 ALLOWED is the set of attributes that are allowed for this statement;
8227 return the attribute we parsed. Multiple attributes are never allowed. */
8230 parse_tm_stmt_attr (tree attrs, int allowed)
8232 tree a_seen = NULL;
8233 int m_seen = 0;
8235 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8237 tree a = TREE_PURPOSE (attrs);
8238 int m = 0;
8240 if (is_attribute_p ("outer", a))
8241 m = TM_STMT_ATTR_OUTER;
8243 if ((m & allowed) == 0)
8245 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8246 continue;
8249 if (m_seen == 0)
8251 a_seen = a;
8252 m_seen = m;
8254 else if (m_seen == m)
8255 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8256 else
8257 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8260 return m_seen;
8263 /* Transform a TM attribute name into a maskable integer and back.
8264 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8265 to how the lack of an attribute is treated. */
8268 tm_attr_to_mask (tree attr)
8270 if (attr == NULL)
8271 return 0;
8272 if (is_attribute_p ("transaction_safe", attr))
8273 return TM_ATTR_SAFE;
8274 if (is_attribute_p ("transaction_callable", attr))
8275 return TM_ATTR_CALLABLE;
8276 if (is_attribute_p ("transaction_pure", attr))
8277 return TM_ATTR_PURE;
8278 if (is_attribute_p ("transaction_unsafe", attr))
8279 return TM_ATTR_IRREVOCABLE;
8280 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8281 return TM_ATTR_MAY_CANCEL_OUTER;
8282 return 0;
8285 tree
8286 tm_mask_to_attr (int mask)
8288 const char *str;
8289 switch (mask)
8291 case TM_ATTR_SAFE:
8292 str = "transaction_safe";
8293 break;
8294 case TM_ATTR_CALLABLE:
8295 str = "transaction_callable";
8296 break;
8297 case TM_ATTR_PURE:
8298 str = "transaction_pure";
8299 break;
8300 case TM_ATTR_IRREVOCABLE:
8301 str = "transaction_unsafe";
8302 break;
8303 case TM_ATTR_MAY_CANCEL_OUTER:
8304 str = "transaction_may_cancel_outer";
8305 break;
8306 default:
8307 gcc_unreachable ();
8309 return get_identifier (str);
8312 /* Return the first TM attribute seen in LIST. */
8314 tree
8315 find_tm_attribute (tree list)
8317 for (; list ; list = TREE_CHAIN (list))
8319 tree name = TREE_PURPOSE (list);
8320 if (tm_attr_to_mask (name) != 0)
8321 return name;
8323 return NULL_TREE;
8326 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8327 Here we accept only function types, and verify that none of the other
8328 function TM attributes are also applied. */
8329 /* ??? We need to accept class types for C++, but not C. This greatly
8330 complicates this function, since we can no longer rely on the extra
8331 processing given by function_type_required. */
8333 static tree
8334 handle_tm_attribute (tree *node, tree name, tree args,
8335 int flags, bool *no_add_attrs)
8337 /* Only one path adds the attribute; others don't. */
8338 *no_add_attrs = true;
8340 switch (TREE_CODE (*node))
8342 case RECORD_TYPE:
8343 case UNION_TYPE:
8344 /* Only tm_callable and tm_safe apply to classes. */
8345 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8346 goto ignored;
8347 /* FALLTHRU */
8349 case FUNCTION_TYPE:
8350 case METHOD_TYPE:
8352 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8353 if (old_name == name)
8355 else if (old_name != NULL_TREE)
8356 error ("type was previously declared %qE", old_name);
8357 else
8358 *no_add_attrs = false;
8360 break;
8362 case POINTER_TYPE:
8364 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8365 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8367 tree fn_tmp = TREE_TYPE (*node);
8368 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8369 *node = build_pointer_type (fn_tmp);
8370 break;
8373 /* FALLTHRU */
8375 default:
8376 /* If a function is next, pass it on to be tried next. */
8377 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8378 return tree_cons (name, args, NULL);
8380 ignored:
8381 warning (OPT_Wattributes, "%qE attribute ignored", name);
8382 break;
8385 return NULL_TREE;
8388 /* Handle the TM_WRAP attribute; arguments as in
8389 struct attribute_spec.handler. */
8391 static tree
8392 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8393 int ARG_UNUSED (flags), bool *no_add_attrs)
8395 tree decl = *node;
8397 /* We don't need the attribute even on success, since we
8398 record the entry in an external table. */
8399 *no_add_attrs = true;
8401 if (TREE_CODE (decl) != FUNCTION_DECL)
8402 warning (OPT_Wattributes, "%qE attribute ignored", name);
8403 else
8405 tree wrap_decl = TREE_VALUE (args);
8406 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8407 && TREE_CODE (wrap_decl) != VAR_DECL
8408 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8409 error ("%qE argument not an identifier", name);
8410 else
8412 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8413 wrap_decl = lookup_name (wrap_decl);
8414 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8416 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8417 TREE_TYPE (wrap_decl)))
8418 record_tm_replacement (wrap_decl, decl);
8419 else
8420 error ("%qD is not compatible with %qD", wrap_decl, decl);
8422 else
8423 error ("transaction_wrap argument is not a function");
8427 return NULL_TREE;
8430 /* Ignore the given attribute. Used when this attribute may be usefully
8431 overridden by the target, but is not used generically. */
8433 static tree
8434 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8435 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8436 bool *no_add_attrs)
8438 *no_add_attrs = true;
8439 return NULL_TREE;
8442 /* Handle a "no vops" attribute; arguments as in
8443 struct attribute_spec.handler. */
8445 static tree
8446 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8447 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8448 bool *ARG_UNUSED (no_add_attrs))
8450 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8451 DECL_IS_NOVOPS (*node) = 1;
8452 return NULL_TREE;
8455 /* Handle a "deprecated" attribute; arguments as in
8456 struct attribute_spec.handler. */
8458 static tree
8459 handle_deprecated_attribute (tree *node, tree name,
8460 tree args, int flags,
8461 bool *no_add_attrs)
8463 tree type = NULL_TREE;
8464 int warn = 0;
8465 tree what = NULL_TREE;
8467 if (!args)
8468 *no_add_attrs = true;
8469 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8471 error ("deprecated message is not a string");
8472 *no_add_attrs = true;
8475 if (DECL_P (*node))
8477 tree decl = *node;
8478 type = TREE_TYPE (decl);
8480 if (TREE_CODE (decl) == TYPE_DECL
8481 || TREE_CODE (decl) == PARM_DECL
8482 || TREE_CODE (decl) == VAR_DECL
8483 || TREE_CODE (decl) == FUNCTION_DECL
8484 || TREE_CODE (decl) == FIELD_DECL
8485 || objc_method_decl (TREE_CODE (decl)))
8486 TREE_DEPRECATED (decl) = 1;
8487 else
8488 warn = 1;
8490 else if (TYPE_P (*node))
8492 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8493 *node = build_variant_type_copy (*node);
8494 TREE_DEPRECATED (*node) = 1;
8495 type = *node;
8497 else
8498 warn = 1;
8500 if (warn)
8502 *no_add_attrs = true;
8503 if (type && TYPE_NAME (type))
8505 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8506 what = TYPE_NAME (*node);
8507 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8508 && DECL_NAME (TYPE_NAME (type)))
8509 what = DECL_NAME (TYPE_NAME (type));
8511 if (what)
8512 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8513 else
8514 warning (OPT_Wattributes, "%qE attribute ignored", name);
8517 return NULL_TREE;
8520 /* Handle a "vector_size" attribute; arguments as in
8521 struct attribute_spec.handler. */
8523 static tree
8524 handle_vector_size_attribute (tree *node, tree name, tree args,
8525 int ARG_UNUSED (flags),
8526 bool *no_add_attrs)
8528 unsigned HOST_WIDE_INT vecsize, nunits;
8529 enum machine_mode orig_mode;
8530 tree type = *node, new_type, size;
8532 *no_add_attrs = true;
8534 size = TREE_VALUE (args);
8536 if (!host_integerp (size, 1))
8538 warning (OPT_Wattributes, "%qE attribute ignored", name);
8539 return NULL_TREE;
8542 /* Get the vector size (in bytes). */
8543 vecsize = tree_low_cst (size, 1);
8545 /* We need to provide for vector pointers, vector arrays, and
8546 functions returning vectors. For example:
8548 __attribute__((vector_size(16))) short *foo;
8550 In this case, the mode is SI, but the type being modified is
8551 HI, so we need to look further. */
8553 while (POINTER_TYPE_P (type)
8554 || TREE_CODE (type) == FUNCTION_TYPE
8555 || TREE_CODE (type) == METHOD_TYPE
8556 || TREE_CODE (type) == ARRAY_TYPE
8557 || TREE_CODE (type) == OFFSET_TYPE)
8558 type = TREE_TYPE (type);
8560 /* Get the mode of the type being modified. */
8561 orig_mode = TYPE_MODE (type);
8563 if ((!INTEGRAL_TYPE_P (type)
8564 && !SCALAR_FLOAT_TYPE_P (type)
8565 && !FIXED_POINT_TYPE_P (type))
8566 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8567 && GET_MODE_CLASS (orig_mode) != MODE_INT
8568 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8569 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8570 || TREE_CODE (type) == BOOLEAN_TYPE)
8572 error ("invalid vector type for attribute %qE", name);
8573 return NULL_TREE;
8576 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8578 error ("vector size not an integral multiple of component size");
8579 return NULL;
8582 if (vecsize == 0)
8584 error ("zero vector size");
8585 return NULL;
8588 /* Calculate how many units fit in the vector. */
8589 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8590 if (nunits & (nunits - 1))
8592 error ("number of components of the vector not a power of two");
8593 return NULL_TREE;
8596 new_type = build_vector_type (type, nunits);
8598 /* Build back pointers if needed. */
8599 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8601 return NULL_TREE;
8604 /* Handle the "nonnull" attribute. */
8605 static tree
8606 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8607 tree args, int ARG_UNUSED (flags),
8608 bool *no_add_attrs)
8610 tree type = *node;
8611 unsigned HOST_WIDE_INT attr_arg_num;
8613 /* If no arguments are specified, all pointer arguments should be
8614 non-null. Verify a full prototype is given so that the arguments
8615 will have the correct types when we actually check them later. */
8616 if (!args)
8618 if (!prototype_p (type))
8620 error ("nonnull attribute without arguments on a non-prototype");
8621 *no_add_attrs = true;
8623 return NULL_TREE;
8626 /* Argument list specified. Verify that each argument number references
8627 a pointer argument. */
8628 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8630 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8632 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8634 error ("nonnull argument has invalid operand number (argument %lu)",
8635 (unsigned long) attr_arg_num);
8636 *no_add_attrs = true;
8637 return NULL_TREE;
8640 if (prototype_p (type))
8642 function_args_iterator iter;
8643 tree argument;
8645 function_args_iter_init (&iter, type);
8646 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8648 argument = function_args_iter_cond (&iter);
8649 if (argument == NULL_TREE || ck_num == arg_num)
8650 break;
8653 if (!argument
8654 || TREE_CODE (argument) == VOID_TYPE)
8656 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8657 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8658 *no_add_attrs = true;
8659 return NULL_TREE;
8662 if (TREE_CODE (argument) != POINTER_TYPE)
8664 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8665 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8666 *no_add_attrs = true;
8667 return NULL_TREE;
8672 return NULL_TREE;
8675 /* Check the argument list of a function call for null in argument slots
8676 that are marked as requiring a non-null pointer argument. The NARGS
8677 arguments are passed in the array ARGARRAY.
8680 static void
8681 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8683 tree a;
8684 int i;
8686 attrs = lookup_attribute ("nonnull", attrs);
8687 if (attrs == NULL_TREE)
8688 return;
8690 a = attrs;
8691 /* See if any of the nonnull attributes has no arguments. If so,
8692 then every pointer argument is checked (in which case the check
8693 for pointer type is done in check_nonnull_arg). */
8694 if (TREE_VALUE (a) != NULL_TREE)
8696 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8697 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8699 if (a != NULL_TREE)
8700 for (i = 0; i < nargs; i++)
8701 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8702 i + 1);
8703 else
8705 /* Walk the argument list. If we encounter an argument number we
8706 should check for non-null, do it. */
8707 for (i = 0; i < nargs; i++)
8709 for (a = attrs; ; a = TREE_CHAIN (a))
8711 a = lookup_attribute ("nonnull", a);
8712 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8713 break;
8716 if (a != NULL_TREE)
8717 check_function_arguments_recurse (check_nonnull_arg, NULL,
8718 argarray[i], i + 1);
8723 /* Check that the Nth argument of a function call (counting backwards
8724 from the end) is a (pointer)0. The NARGS arguments are passed in the
8725 array ARGARRAY. */
8727 static void
8728 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8730 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8732 if (attr)
8734 int len = 0;
8735 int pos = 0;
8736 tree sentinel;
8737 function_args_iterator iter;
8738 tree t;
8740 /* Skip over the named arguments. */
8741 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8743 if (len == nargs)
8744 break;
8745 len++;
8748 if (TREE_VALUE (attr))
8750 tree p = TREE_VALUE (TREE_VALUE (attr));
8751 pos = TREE_INT_CST_LOW (p);
8754 /* The sentinel must be one of the varargs, i.e.
8755 in position >= the number of fixed arguments. */
8756 if ((nargs - 1 - pos) < len)
8758 warning (OPT_Wformat_,
8759 "not enough variable arguments to fit a sentinel");
8760 return;
8763 /* Validate the sentinel. */
8764 sentinel = argarray[nargs - 1 - pos];
8765 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8766 || !integer_zerop (sentinel))
8767 /* Although __null (in C++) is only an integer we allow it
8768 nevertheless, as we are guaranteed that it's exactly
8769 as wide as a pointer, and we don't want to force
8770 users to cast the NULL they have written there.
8771 We warn with -Wstrict-null-sentinel, though. */
8772 && (warn_strict_null_sentinel || null_node != sentinel))
8773 warning (OPT_Wformat_, "missing sentinel in function call");
8777 /* Helper for check_function_nonnull; given a list of operands which
8778 must be non-null in ARGS, determine if operand PARAM_NUM should be
8779 checked. */
8781 static bool
8782 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8784 unsigned HOST_WIDE_INT arg_num = 0;
8786 for (; args; args = TREE_CHAIN (args))
8788 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8790 gcc_assert (found);
8792 if (arg_num == param_num)
8793 return true;
8795 return false;
8798 /* Check that the function argument PARAM (which is operand number
8799 PARAM_NUM) is non-null. This is called by check_function_nonnull
8800 via check_function_arguments_recurse. */
8802 static void
8803 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8804 unsigned HOST_WIDE_INT param_num)
8806 /* Just skip checking the argument if it's not a pointer. This can
8807 happen if the "nonnull" attribute was given without an operand
8808 list (which means to check every pointer argument). */
8810 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8811 return;
8813 if (integer_zerop (param))
8814 warning (OPT_Wnonnull, "null argument where non-null required "
8815 "(argument %lu)", (unsigned long) param_num);
8818 /* Helper for nonnull attribute handling; fetch the operand number
8819 from the attribute argument list. */
8821 static bool
8822 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8824 /* Verify the arg number is a constant. */
8825 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8826 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8827 return false;
8829 *valp = TREE_INT_CST_LOW (arg_num_expr);
8830 return true;
8833 /* Handle a "nothrow" attribute; arguments as in
8834 struct attribute_spec.handler. */
8836 static tree
8837 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8838 int ARG_UNUSED (flags), bool *no_add_attrs)
8840 if (TREE_CODE (*node) == FUNCTION_DECL)
8841 TREE_NOTHROW (*node) = 1;
8842 /* ??? TODO: Support types. */
8843 else
8845 warning (OPT_Wattributes, "%qE attribute ignored", name);
8846 *no_add_attrs = true;
8849 return NULL_TREE;
8852 /* Handle a "cleanup" attribute; arguments as in
8853 struct attribute_spec.handler. */
8855 static tree
8856 handle_cleanup_attribute (tree *node, tree name, tree args,
8857 int ARG_UNUSED (flags), bool *no_add_attrs)
8859 tree decl = *node;
8860 tree cleanup_id, cleanup_decl;
8862 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8863 for global destructors in C++. This requires infrastructure that
8864 we don't have generically at the moment. It's also not a feature
8865 we'd be missing too much, since we do have attribute constructor. */
8866 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8868 warning (OPT_Wattributes, "%qE attribute ignored", name);
8869 *no_add_attrs = true;
8870 return NULL_TREE;
8873 /* Verify that the argument is a function in scope. */
8874 /* ??? We could support pointers to functions here as well, if
8875 that was considered desirable. */
8876 cleanup_id = TREE_VALUE (args);
8877 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8879 error ("cleanup argument not an identifier");
8880 *no_add_attrs = true;
8881 return NULL_TREE;
8883 cleanup_decl = lookup_name (cleanup_id);
8884 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8886 error ("cleanup argument not a function");
8887 *no_add_attrs = true;
8888 return NULL_TREE;
8891 /* That the function has proper type is checked with the
8892 eventual call to build_function_call. */
8894 return NULL_TREE;
8897 /* Handle a "warn_unused_result" attribute. No special handling. */
8899 static tree
8900 handle_warn_unused_result_attribute (tree *node, tree name,
8901 tree ARG_UNUSED (args),
8902 int ARG_UNUSED (flags), bool *no_add_attrs)
8904 /* Ignore the attribute for functions not returning any value. */
8905 if (VOID_TYPE_P (TREE_TYPE (*node)))
8907 warning (OPT_Wattributes, "%qE attribute ignored", name);
8908 *no_add_attrs = true;
8911 return NULL_TREE;
8914 /* Handle a "sentinel" attribute. */
8916 static tree
8917 handle_sentinel_attribute (tree *node, tree name, tree args,
8918 int ARG_UNUSED (flags), bool *no_add_attrs)
8920 if (!prototype_p (*node))
8922 warning (OPT_Wattributes,
8923 "%qE attribute requires prototypes with named arguments", name);
8924 *no_add_attrs = true;
8926 else
8928 if (!stdarg_p (*node))
8930 warning (OPT_Wattributes,
8931 "%qE attribute only applies to variadic functions", name);
8932 *no_add_attrs = true;
8936 if (args)
8938 tree position = TREE_VALUE (args);
8940 if (TREE_CODE (position) != INTEGER_CST)
8942 warning (OPT_Wattributes,
8943 "requested position is not an integer constant");
8944 *no_add_attrs = true;
8946 else
8948 if (tree_int_cst_lt (position, integer_zero_node))
8950 warning (OPT_Wattributes,
8951 "requested position is less than zero");
8952 *no_add_attrs = true;
8957 return NULL_TREE;
8960 /* Handle a "type_generic" attribute. */
8962 static tree
8963 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8964 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8965 bool * ARG_UNUSED (no_add_attrs))
8967 /* Ensure we have a function type. */
8968 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8970 /* Ensure we have a variadic function. */
8971 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8973 return NULL_TREE;
8976 /* Handle a "target" attribute. */
8978 static tree
8979 handle_target_attribute (tree *node, tree name, tree args, int flags,
8980 bool *no_add_attrs)
8982 /* Ensure we have a function type. */
8983 if (TREE_CODE (*node) != FUNCTION_DECL)
8985 warning (OPT_Wattributes, "%qE attribute ignored", name);
8986 *no_add_attrs = true;
8988 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8989 flags))
8990 *no_add_attrs = true;
8992 return NULL_TREE;
8995 /* Arguments being collected for optimization. */
8996 typedef const char *const_char_p; /* For DEF_VEC_P. */
8997 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9000 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9001 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9002 false for #pragma GCC optimize. */
9004 bool
9005 parse_optimize_options (tree args, bool attr_p)
9007 bool ret = true;
9008 unsigned opt_argc;
9009 unsigned i;
9010 int saved_flag_strict_aliasing;
9011 const char **opt_argv;
9012 struct cl_decoded_option *decoded_options;
9013 unsigned int decoded_options_count;
9014 tree ap;
9016 /* Build up argv vector. Just in case the string is stored away, use garbage
9017 collected strings. */
9018 vec_safe_truncate (optimize_args, 0);
9019 vec_safe_push (optimize_args, (const char *) NULL);
9021 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9023 tree value = TREE_VALUE (ap);
9025 if (TREE_CODE (value) == INTEGER_CST)
9027 char buffer[20];
9028 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9029 vec_safe_push (optimize_args, ggc_strdup (buffer));
9032 else if (TREE_CODE (value) == STRING_CST)
9034 /* Split string into multiple substrings. */
9035 size_t len = TREE_STRING_LENGTH (value);
9036 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9037 char *end = p + len;
9038 char *comma;
9039 char *next_p = p;
9041 while (next_p != NULL)
9043 size_t len2;
9044 char *q, *r;
9046 p = next_p;
9047 comma = strchr (p, ',');
9048 if (comma)
9050 len2 = comma - p;
9051 *comma = '\0';
9052 next_p = comma+1;
9054 else
9056 len2 = end - p;
9057 next_p = NULL;
9060 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9062 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9063 options. */
9064 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9066 ret = false;
9067 if (attr_p)
9068 warning (OPT_Wattributes,
9069 "bad option %s to optimize attribute", p);
9070 else
9071 warning (OPT_Wpragmas,
9072 "bad option %s to pragma attribute", p);
9073 continue;
9076 if (*p != '-')
9078 *r++ = '-';
9080 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9081 itself is -Os, and any other switch begins with a -f. */
9082 if ((*p >= '0' && *p <= '9')
9083 || (p[0] == 's' && p[1] == '\0'))
9084 *r++ = 'O';
9085 else if (*p != 'O')
9086 *r++ = 'f';
9089 memcpy (r, p, len2);
9090 r[len2] = '\0';
9091 vec_safe_push (optimize_args, (const char *) q);
9097 opt_argc = optimize_args->length ();
9098 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9100 for (i = 1; i < opt_argc; i++)
9101 opt_argv[i] = (*optimize_args)[i];
9103 saved_flag_strict_aliasing = flag_strict_aliasing;
9105 /* Now parse the options. */
9106 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9107 &decoded_options,
9108 &decoded_options_count);
9109 decode_options (&global_options, &global_options_set,
9110 decoded_options, decoded_options_count,
9111 input_location, global_dc);
9113 targetm.override_options_after_change();
9115 /* Don't allow changing -fstrict-aliasing. */
9116 flag_strict_aliasing = saved_flag_strict_aliasing;
9118 optimize_args->truncate (0);
9119 return ret;
9122 /* For handling "optimize" attribute. arguments as in
9123 struct attribute_spec.handler. */
9125 static tree
9126 handle_optimize_attribute (tree *node, tree name, tree args,
9127 int ARG_UNUSED (flags), bool *no_add_attrs)
9129 /* Ensure we have a function type. */
9130 if (TREE_CODE (*node) != FUNCTION_DECL)
9132 warning (OPT_Wattributes, "%qE attribute ignored", name);
9133 *no_add_attrs = true;
9135 else
9137 struct cl_optimization cur_opts;
9138 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9140 /* Save current options. */
9141 cl_optimization_save (&cur_opts, &global_options);
9143 /* If we previously had some optimization options, use them as the
9144 default. */
9145 if (old_opts)
9146 cl_optimization_restore (&global_options,
9147 TREE_OPTIMIZATION (old_opts));
9149 /* Parse options, and update the vector. */
9150 parse_optimize_options (args, true);
9151 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9152 = build_optimization_node (&global_options);
9154 /* Restore current options. */
9155 cl_optimization_restore (&global_options, &cur_opts);
9158 return NULL_TREE;
9161 /* Handle a "no_split_stack" attribute. */
9163 static tree
9164 handle_no_split_stack_attribute (tree *node, tree name,
9165 tree ARG_UNUSED (args),
9166 int ARG_UNUSED (flags),
9167 bool *no_add_attrs)
9169 tree decl = *node;
9171 if (TREE_CODE (decl) != FUNCTION_DECL)
9173 error_at (DECL_SOURCE_LOCATION (decl),
9174 "%qE attribute applies only to functions", name);
9175 *no_add_attrs = true;
9177 else if (DECL_INITIAL (decl))
9179 error_at (DECL_SOURCE_LOCATION (decl),
9180 "can%'t set %qE attribute after definition", name);
9181 *no_add_attrs = true;
9184 return NULL_TREE;
9187 /* Handle a "returns_nonnull" attribute; arguments as in
9188 struct attribute_spec.handler. */
9190 static tree
9191 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9192 bool *no_add_attrs)
9194 // Even without a prototype we still have a return type we can check.
9195 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9197 error ("returns_nonnull attribute on a function not returning a pointer");
9198 *no_add_attrs = true;
9200 return NULL_TREE;
9204 /* Check for valid arguments being passed to a function with FNTYPE.
9205 There are NARGS arguments in the array ARGARRAY. */
9206 void
9207 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9209 /* Check for null being passed in a pointer argument that must be
9210 non-null. We also need to do this if format checking is enabled. */
9212 if (warn_nonnull)
9213 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9215 /* Check for errors in format strings. */
9217 if (warn_format || warn_suggest_attribute_format)
9218 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9220 if (warn_format)
9221 check_function_sentinel (fntype, nargs, argarray);
9224 /* Generic argument checking recursion routine. PARAM is the argument to
9225 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9226 once the argument is resolved. CTX is context for the callback. */
9227 void
9228 check_function_arguments_recurse (void (*callback)
9229 (void *, tree, unsigned HOST_WIDE_INT),
9230 void *ctx, tree param,
9231 unsigned HOST_WIDE_INT param_num)
9233 if (CONVERT_EXPR_P (param)
9234 && (TYPE_PRECISION (TREE_TYPE (param))
9235 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9237 /* Strip coercion. */
9238 check_function_arguments_recurse (callback, ctx,
9239 TREE_OPERAND (param, 0), param_num);
9240 return;
9243 if (TREE_CODE (param) == CALL_EXPR)
9245 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9246 tree attrs;
9247 bool found_format_arg = false;
9249 /* See if this is a call to a known internationalization function
9250 that modifies a format arg. Such a function may have multiple
9251 format_arg attributes (for example, ngettext). */
9253 for (attrs = TYPE_ATTRIBUTES (type);
9254 attrs;
9255 attrs = TREE_CHAIN (attrs))
9256 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9258 tree inner_arg;
9259 tree format_num_expr;
9260 int format_num;
9261 int i;
9262 call_expr_arg_iterator iter;
9264 /* Extract the argument number, which was previously checked
9265 to be valid. */
9266 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9268 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9269 && !TREE_INT_CST_HIGH (format_num_expr));
9271 format_num = TREE_INT_CST_LOW (format_num_expr);
9273 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9274 inner_arg != 0;
9275 inner_arg = next_call_expr_arg (&iter), i++)
9276 if (i == format_num)
9278 check_function_arguments_recurse (callback, ctx,
9279 inner_arg, param_num);
9280 found_format_arg = true;
9281 break;
9285 /* If we found a format_arg attribute and did a recursive check,
9286 we are done with checking this argument. Otherwise, we continue
9287 and this will be considered a non-literal. */
9288 if (found_format_arg)
9289 return;
9292 if (TREE_CODE (param) == COND_EXPR)
9294 /* Check both halves of the conditional expression. */
9295 check_function_arguments_recurse (callback, ctx,
9296 TREE_OPERAND (param, 1), param_num);
9297 check_function_arguments_recurse (callback, ctx,
9298 TREE_OPERAND (param, 2), param_num);
9299 return;
9302 (*callback) (ctx, param, param_num);
9305 /* Checks for a builtin function FNDECL that the number of arguments
9306 NARGS against the required number REQUIRED and issues an error if
9307 there is a mismatch. Returns true if the number of arguments is
9308 correct, otherwise false. */
9310 static bool
9311 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9313 if (nargs < required)
9315 error_at (input_location,
9316 "not enough arguments to function %qE", fndecl);
9317 return false;
9319 else if (nargs > required)
9321 error_at (input_location,
9322 "too many arguments to function %qE", fndecl);
9323 return false;
9325 return true;
9328 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9329 Returns false if there was an error, otherwise true. */
9331 bool
9332 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9334 if (!DECL_BUILT_IN (fndecl)
9335 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9336 return true;
9338 switch (DECL_FUNCTION_CODE (fndecl))
9340 case BUILT_IN_CONSTANT_P:
9341 return builtin_function_validate_nargs (fndecl, nargs, 1);
9343 case BUILT_IN_ISFINITE:
9344 case BUILT_IN_ISINF:
9345 case BUILT_IN_ISINF_SIGN:
9346 case BUILT_IN_ISNAN:
9347 case BUILT_IN_ISNORMAL:
9348 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9350 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9352 error ("non-floating-point argument in call to "
9353 "function %qE", fndecl);
9354 return false;
9356 return true;
9358 return false;
9360 case BUILT_IN_ISGREATER:
9361 case BUILT_IN_ISGREATEREQUAL:
9362 case BUILT_IN_ISLESS:
9363 case BUILT_IN_ISLESSEQUAL:
9364 case BUILT_IN_ISLESSGREATER:
9365 case BUILT_IN_ISUNORDERED:
9366 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9368 enum tree_code code0, code1;
9369 code0 = TREE_CODE (TREE_TYPE (args[0]));
9370 code1 = TREE_CODE (TREE_TYPE (args[1]));
9371 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9372 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9373 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9375 error ("non-floating-point arguments in call to "
9376 "function %qE", fndecl);
9377 return false;
9379 return true;
9381 return false;
9383 case BUILT_IN_FPCLASSIFY:
9384 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9386 unsigned i;
9388 for (i=0; i<5; i++)
9389 if (TREE_CODE (args[i]) != INTEGER_CST)
9391 error ("non-const integer argument %u in call to function %qE",
9392 i+1, fndecl);
9393 return false;
9396 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9398 error ("non-floating-point argument in call to function %qE",
9399 fndecl);
9400 return false;
9402 return true;
9404 return false;
9406 case BUILT_IN_ASSUME_ALIGNED:
9407 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9409 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9411 error ("non-integer argument 3 in call to function %qE", fndecl);
9412 return false;
9414 return true;
9416 return false;
9418 default:
9419 return true;
9423 /* Function to help qsort sort FIELD_DECLs by name order. */
9426 field_decl_cmp (const void *x_p, const void *y_p)
9428 const tree *const x = (const tree *const) x_p;
9429 const tree *const y = (const tree *const) y_p;
9431 if (DECL_NAME (*x) == DECL_NAME (*y))
9432 /* A nontype is "greater" than a type. */
9433 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9434 if (DECL_NAME (*x) == NULL_TREE)
9435 return -1;
9436 if (DECL_NAME (*y) == NULL_TREE)
9437 return 1;
9438 if (DECL_NAME (*x) < DECL_NAME (*y))
9439 return -1;
9440 return 1;
9443 static struct {
9444 gt_pointer_operator new_value;
9445 void *cookie;
9446 } resort_data;
9448 /* This routine compares two fields like field_decl_cmp but using the
9449 pointer operator in resort_data. */
9451 static int
9452 resort_field_decl_cmp (const void *x_p, const void *y_p)
9454 const tree *const x = (const tree *const) x_p;
9455 const tree *const y = (const tree *const) y_p;
9457 if (DECL_NAME (*x) == DECL_NAME (*y))
9458 /* A nontype is "greater" than a type. */
9459 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9460 if (DECL_NAME (*x) == NULL_TREE)
9461 return -1;
9462 if (DECL_NAME (*y) == NULL_TREE)
9463 return 1;
9465 tree d1 = DECL_NAME (*x);
9466 tree d2 = DECL_NAME (*y);
9467 resort_data.new_value (&d1, resort_data.cookie);
9468 resort_data.new_value (&d2, resort_data.cookie);
9469 if (d1 < d2)
9470 return -1;
9472 return 1;
9475 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9477 void
9478 resort_sorted_fields (void *obj,
9479 void * ARG_UNUSED (orig_obj),
9480 gt_pointer_operator new_value,
9481 void *cookie)
9483 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9484 resort_data.new_value = new_value;
9485 resort_data.cookie = cookie;
9486 qsort (&sf->elts[0], sf->len, sizeof (tree),
9487 resort_field_decl_cmp);
9490 /* Subroutine of c_parse_error.
9491 Return the result of concatenating LHS and RHS. RHS is really
9492 a string literal, its first character is indicated by RHS_START and
9493 RHS_SIZE is its length (including the terminating NUL character).
9495 The caller is responsible for deleting the returned pointer. */
9497 static char *
9498 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9500 const int lhs_size = strlen (lhs);
9501 char *result = XNEWVEC (char, lhs_size + rhs_size);
9502 strncpy (result, lhs, lhs_size);
9503 strncpy (result + lhs_size, rhs_start, rhs_size);
9504 return result;
9507 /* Issue the error given by GMSGID, indicating that it occurred before
9508 TOKEN, which had the associated VALUE. */
9510 void
9511 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9512 tree value, unsigned char token_flags)
9514 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9516 char *message = NULL;
9518 if (token_type == CPP_EOF)
9519 message = catenate_messages (gmsgid, " at end of input");
9520 else if (token_type == CPP_CHAR
9521 || token_type == CPP_WCHAR
9522 || token_type == CPP_CHAR16
9523 || token_type == CPP_CHAR32)
9525 unsigned int val = TREE_INT_CST_LOW (value);
9526 const char *prefix;
9528 switch (token_type)
9530 default:
9531 prefix = "";
9532 break;
9533 case CPP_WCHAR:
9534 prefix = "L";
9535 break;
9536 case CPP_CHAR16:
9537 prefix = "u";
9538 break;
9539 case CPP_CHAR32:
9540 prefix = "U";
9541 break;
9544 if (val <= UCHAR_MAX && ISGRAPH (val))
9545 message = catenate_messages (gmsgid, " before %s'%c'");
9546 else
9547 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9549 error (message, prefix, val);
9550 free (message);
9551 message = NULL;
9553 else if (token_type == CPP_CHAR_USERDEF
9554 || token_type == CPP_WCHAR_USERDEF
9555 || token_type == CPP_CHAR16_USERDEF
9556 || token_type == CPP_CHAR32_USERDEF)
9557 message = catenate_messages (gmsgid,
9558 " before user-defined character literal");
9559 else if (token_type == CPP_STRING_USERDEF
9560 || token_type == CPP_WSTRING_USERDEF
9561 || token_type == CPP_STRING16_USERDEF
9562 || token_type == CPP_STRING32_USERDEF
9563 || token_type == CPP_UTF8STRING_USERDEF)
9564 message = catenate_messages (gmsgid, " before user-defined string literal");
9565 else if (token_type == CPP_STRING
9566 || token_type == CPP_WSTRING
9567 || token_type == CPP_STRING16
9568 || token_type == CPP_STRING32
9569 || token_type == CPP_UTF8STRING)
9570 message = catenate_messages (gmsgid, " before string constant");
9571 else if (token_type == CPP_NUMBER)
9572 message = catenate_messages (gmsgid, " before numeric constant");
9573 else if (token_type == CPP_NAME)
9575 message = catenate_messages (gmsgid, " before %qE");
9576 error (message, value);
9577 free (message);
9578 message = NULL;
9580 else if (token_type == CPP_PRAGMA)
9581 message = catenate_messages (gmsgid, " before %<#pragma%>");
9582 else if (token_type == CPP_PRAGMA_EOL)
9583 message = catenate_messages (gmsgid, " before end of line");
9584 else if (token_type == CPP_DECLTYPE)
9585 message = catenate_messages (gmsgid, " before %<decltype%>");
9586 else if (token_type < N_TTYPES)
9588 message = catenate_messages (gmsgid, " before %qs token");
9589 error (message, cpp_type2name (token_type, token_flags));
9590 free (message);
9591 message = NULL;
9593 else
9594 error (gmsgid);
9596 if (message)
9598 error (message);
9599 free (message);
9601 #undef catenate_messages
9604 /* Mapping for cpp message reasons to the options that enable them. */
9606 struct reason_option_codes_t
9608 const int reason; /* cpplib message reason. */
9609 const int option_code; /* gcc option that controls this message. */
9612 static const struct reason_option_codes_t option_codes[] = {
9613 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9614 {CPP_W_COMMENTS, OPT_Wcomment},
9615 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9616 {CPP_W_MULTICHAR, OPT_Wmultichar},
9617 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9618 {CPP_W_LONG_LONG, OPT_Wlong_long},
9619 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9620 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9621 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9622 {CPP_W_UNDEF, OPT_Wundef},
9623 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9624 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9625 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9626 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9627 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9628 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9629 {CPP_W_DATE_TIME, OPT_Wdate_time},
9630 {CPP_W_NONE, 0}
9633 /* Return the gcc option code associated with the reason for a cpp
9634 message, or 0 if none. */
9636 static int
9637 c_option_controlling_cpp_error (int reason)
9639 const struct reason_option_codes_t *entry;
9641 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9643 if (entry->reason == reason)
9644 return entry->option_code;
9646 return 0;
9649 /* Callback from cpp_error for PFILE to print diagnostics from the
9650 preprocessor. The diagnostic is of type LEVEL, with REASON set
9651 to the reason code if LEVEL is represents a warning, at location
9652 LOCATION unless this is after lexing and the compiler's location
9653 should be used instead, with column number possibly overridden by
9654 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9655 the arguments. Returns true if a diagnostic was emitted, false
9656 otherwise. */
9658 bool
9659 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9660 location_t location, unsigned int column_override,
9661 const char *msg, va_list *ap)
9663 diagnostic_info diagnostic;
9664 diagnostic_t dlevel;
9665 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9666 bool ret;
9668 switch (level)
9670 case CPP_DL_WARNING_SYSHDR:
9671 if (flag_no_output)
9672 return false;
9673 global_dc->dc_warn_system_headers = 1;
9674 /* Fall through. */
9675 case CPP_DL_WARNING:
9676 if (flag_no_output)
9677 return false;
9678 dlevel = DK_WARNING;
9679 break;
9680 case CPP_DL_PEDWARN:
9681 if (flag_no_output && !flag_pedantic_errors)
9682 return false;
9683 dlevel = DK_PEDWARN;
9684 break;
9685 case CPP_DL_ERROR:
9686 dlevel = DK_ERROR;
9687 break;
9688 case CPP_DL_ICE:
9689 dlevel = DK_ICE;
9690 break;
9691 case CPP_DL_NOTE:
9692 dlevel = DK_NOTE;
9693 break;
9694 case CPP_DL_FATAL:
9695 dlevel = DK_FATAL;
9696 break;
9697 default:
9698 gcc_unreachable ();
9700 if (done_lexing)
9701 location = input_location;
9702 diagnostic_set_info_translated (&diagnostic, msg, ap,
9703 location, dlevel);
9704 if (column_override)
9705 diagnostic_override_column (&diagnostic, column_override);
9706 diagnostic_override_option_index (&diagnostic,
9707 c_option_controlling_cpp_error (reason));
9708 ret = report_diagnostic (&diagnostic);
9709 if (level == CPP_DL_WARNING_SYSHDR)
9710 global_dc->dc_warn_system_headers = save_warn_system_headers;
9711 return ret;
9714 /* Convert a character from the host to the target execution character
9715 set. cpplib handles this, mostly. */
9717 HOST_WIDE_INT
9718 c_common_to_target_charset (HOST_WIDE_INT c)
9720 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9721 zero-extended under -fno-signed-char. cpplib insists that characters
9722 and character constants are always unsigned. Hence we must convert
9723 back and forth. */
9724 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9726 uc = cpp_host_to_exec_charset (parse_in, uc);
9728 if (flag_signed_char)
9729 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9730 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9731 else
9732 return uc;
9735 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9736 references with an INDIRECT_REF of a constant at the bottom; much like the
9737 traditional rendering of offsetof as a macro. Return the folded result. */
9739 tree
9740 fold_offsetof_1 (tree expr)
9742 tree base, off, t;
9744 switch (TREE_CODE (expr))
9746 case ERROR_MARK:
9747 return expr;
9749 case VAR_DECL:
9750 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9751 return error_mark_node;
9753 case CALL_EXPR:
9754 case TARGET_EXPR:
9755 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9756 return error_mark_node;
9758 case NOP_EXPR:
9759 case INDIRECT_REF:
9760 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9762 error ("cannot apply %<offsetof%> to a non constant address");
9763 return error_mark_node;
9765 return TREE_OPERAND (expr, 0);
9767 case COMPONENT_REF:
9768 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9769 if (base == error_mark_node)
9770 return base;
9772 t = TREE_OPERAND (expr, 1);
9773 if (DECL_C_BIT_FIELD (t))
9775 error ("attempt to take address of bit-field structure "
9776 "member %qD", t);
9777 return error_mark_node;
9779 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9780 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9782 / BITS_PER_UNIT));
9783 break;
9785 case ARRAY_REF:
9786 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9787 if (base == error_mark_node)
9788 return base;
9790 t = TREE_OPERAND (expr, 1);
9792 /* Check if the offset goes beyond the upper bound of the array. */
9793 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9795 tree upbound = array_ref_up_bound (expr);
9796 if (upbound != NULL_TREE
9797 && TREE_CODE (upbound) == INTEGER_CST
9798 && !tree_int_cst_equal (upbound,
9799 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9801 upbound = size_binop (PLUS_EXPR, upbound,
9802 build_int_cst (TREE_TYPE (upbound), 1));
9803 if (tree_int_cst_lt (upbound, t))
9805 tree v;
9807 for (v = TREE_OPERAND (expr, 0);
9808 TREE_CODE (v) == COMPONENT_REF;
9809 v = TREE_OPERAND (v, 0))
9810 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9811 == RECORD_TYPE)
9813 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9814 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9815 if (TREE_CODE (fld_chain) == FIELD_DECL)
9816 break;
9818 if (fld_chain)
9819 break;
9821 /* Don't warn if the array might be considered a poor
9822 man's flexible array member with a very permissive
9823 definition thereof. */
9824 if (TREE_CODE (v) == ARRAY_REF
9825 || TREE_CODE (v) == COMPONENT_REF)
9826 warning (OPT_Warray_bounds,
9827 "index %E denotes an offset "
9828 "greater than size of %qT",
9829 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9834 t = convert (sizetype, t);
9835 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9836 break;
9838 case COMPOUND_EXPR:
9839 /* Handle static members of volatile structs. */
9840 t = TREE_OPERAND (expr, 1);
9841 gcc_assert (TREE_CODE (t) == VAR_DECL);
9842 return fold_offsetof_1 (t);
9844 default:
9845 gcc_unreachable ();
9848 return fold_build_pointer_plus (base, off);
9851 /* Likewise, but convert it to the return type of offsetof. */
9853 tree
9854 fold_offsetof (tree expr)
9856 return convert (size_type_node, fold_offsetof_1 (expr));
9859 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9860 expression, because B will always be true. */
9862 void
9863 warn_for_omitted_condop (location_t location, tree cond)
9865 if (truth_value_p (TREE_CODE (cond)))
9866 warning_at (location, OPT_Wparentheses,
9867 "the omitted middle operand in ?: will always be %<true%>, "
9868 "suggest explicit middle operand");
9871 /* Give an error for storing into ARG, which is 'const'. USE indicates
9872 how ARG was being used. */
9874 void
9875 readonly_error (tree arg, enum lvalue_use use)
9877 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9878 || use == lv_asm);
9879 /* Using this macro rather than (for example) arrays of messages
9880 ensures that all the format strings are checked at compile
9881 time. */
9882 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9883 : (use == lv_increment ? (I) \
9884 : (use == lv_decrement ? (D) : (AS))))
9885 if (TREE_CODE (arg) == COMPONENT_REF)
9887 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9888 error (READONLY_MSG (G_("assignment of member "
9889 "%qD in read-only object"),
9890 G_("increment of member "
9891 "%qD in read-only object"),
9892 G_("decrement of member "
9893 "%qD in read-only object"),
9894 G_("member %qD in read-only object "
9895 "used as %<asm%> output")),
9896 TREE_OPERAND (arg, 1));
9897 else
9898 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9899 G_("increment of read-only member %qD"),
9900 G_("decrement of read-only member %qD"),
9901 G_("read-only member %qD used as %<asm%> output")),
9902 TREE_OPERAND (arg, 1));
9904 else if (TREE_CODE (arg) == VAR_DECL)
9905 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9906 G_("increment of read-only variable %qD"),
9907 G_("decrement of read-only variable %qD"),
9908 G_("read-only variable %qD used as %<asm%> output")),
9909 arg);
9910 else if (TREE_CODE (arg) == PARM_DECL)
9911 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9912 G_("increment of read-only parameter %qD"),
9913 G_("decrement of read-only parameter %qD"),
9914 G_("read-only parameter %qD use as %<asm%> output")),
9915 arg);
9916 else if (TREE_CODE (arg) == RESULT_DECL)
9918 gcc_assert (c_dialect_cxx ());
9919 error (READONLY_MSG (G_("assignment of "
9920 "read-only named return value %qD"),
9921 G_("increment of "
9922 "read-only named return value %qD"),
9923 G_("decrement of "
9924 "read-only named return value %qD"),
9925 G_("read-only named return value %qD "
9926 "used as %<asm%>output")),
9927 arg);
9929 else if (TREE_CODE (arg) == FUNCTION_DECL)
9930 error (READONLY_MSG (G_("assignment of function %qD"),
9931 G_("increment of function %qD"),
9932 G_("decrement of function %qD"),
9933 G_("function %qD used as %<asm%> output")),
9934 arg);
9935 else
9936 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9937 G_("increment of read-only location %qE"),
9938 G_("decrement of read-only location %qE"),
9939 G_("read-only location %qE used as %<asm%> output")),
9940 arg);
9943 /* Print an error message for an invalid lvalue. USE says
9944 how the lvalue is being used and so selects the error message. LOC
9945 is the location for the error. */
9947 void
9948 lvalue_error (location_t loc, enum lvalue_use use)
9950 switch (use)
9952 case lv_assign:
9953 error_at (loc, "lvalue required as left operand of assignment");
9954 break;
9955 case lv_increment:
9956 error_at (loc, "lvalue required as increment operand");
9957 break;
9958 case lv_decrement:
9959 error_at (loc, "lvalue required as decrement operand");
9960 break;
9961 case lv_addressof:
9962 error_at (loc, "lvalue required as unary %<&%> operand");
9963 break;
9964 case lv_asm:
9965 error_at (loc, "lvalue required in asm statement");
9966 break;
9967 default:
9968 gcc_unreachable ();
9972 /* Print an error message for an invalid indirection of type TYPE.
9973 ERRSTRING is the name of the operator for the indirection. */
9975 void
9976 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9978 switch (errstring)
9980 case RO_NULL:
9981 gcc_assert (c_dialect_cxx ());
9982 error_at (loc, "invalid type argument (have %qT)", type);
9983 break;
9984 case RO_ARRAY_INDEXING:
9985 error_at (loc,
9986 "invalid type argument of array indexing (have %qT)",
9987 type);
9988 break;
9989 case RO_UNARY_STAR:
9990 error_at (loc,
9991 "invalid type argument of unary %<*%> (have %qT)",
9992 type);
9993 break;
9994 case RO_ARROW:
9995 error_at (loc,
9996 "invalid type argument of %<->%> (have %qT)",
9997 type);
9998 break;
9999 case RO_ARROW_STAR:
10000 error_at (loc,
10001 "invalid type argument of %<->*%> (have %qT)",
10002 type);
10003 break;
10004 case RO_IMPLICIT_CONVERSION:
10005 error_at (loc,
10006 "invalid type argument of implicit conversion (have %qT)",
10007 type);
10008 break;
10009 default:
10010 gcc_unreachable ();
10014 /* *PTYPE is an incomplete array. Complete it with a domain based on
10015 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10016 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10017 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10020 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10022 tree maxindex, type, main_type, elt, unqual_elt;
10023 int failure = 0, quals;
10024 hashval_t hashcode = 0;
10025 bool overflow_p = false;
10027 maxindex = size_zero_node;
10028 if (initial_value)
10030 if (TREE_CODE (initial_value) == STRING_CST)
10032 int eltsize
10033 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10034 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10036 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10038 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10040 if (vec_safe_is_empty (v))
10042 if (pedantic)
10043 failure = 3;
10044 maxindex = ssize_int (-1);
10046 else
10048 tree curindex;
10049 unsigned HOST_WIDE_INT cnt;
10050 constructor_elt *ce;
10051 bool fold_p = false;
10053 if ((*v)[0].index)
10054 maxindex = (*v)[0].index, fold_p = true;
10056 curindex = maxindex;
10058 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10060 bool curfold_p = false;
10061 if (ce->index)
10062 curindex = ce->index, curfold_p = true;
10063 else
10065 if (fold_p)
10067 /* Since we treat size types now as ordinary
10068 unsigned types, we need an explicit overflow
10069 check. */
10070 tree orig = curindex;
10071 curindex = fold_convert (sizetype, curindex);
10072 overflow_p |= tree_int_cst_lt (curindex, orig);
10074 curindex = size_binop (PLUS_EXPR, curindex,
10075 size_one_node);
10077 if (tree_int_cst_lt (maxindex, curindex))
10078 maxindex = curindex, fold_p = curfold_p;
10080 if (fold_p)
10082 tree orig = maxindex;
10083 maxindex = fold_convert (sizetype, maxindex);
10084 overflow_p |= tree_int_cst_lt (maxindex, orig);
10088 else
10090 /* Make an error message unless that happened already. */
10091 if (initial_value != error_mark_node)
10092 failure = 1;
10095 else
10097 failure = 2;
10098 if (!do_default)
10099 return failure;
10102 type = *ptype;
10103 /* Force an indefinite layout factor. */
10104 if (upc_shared_type_p (type))
10105 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
10106 elt = TREE_TYPE (type);
10107 quals = TYPE_QUALS (strip_array_types (elt));
10108 if (quals == 0)
10109 unqual_elt = elt;
10110 else
10111 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10113 /* Using build_distinct_type_copy and modifying things afterward instead
10114 of using build_array_type to create a new type preserves all of the
10115 TYPE_LANG_FLAG_? bits that the front end may have set. */
10116 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10117 TREE_TYPE (main_type) = unqual_elt;
10118 TYPE_DOMAIN (main_type)
10119 = build_range_type (TREE_TYPE (maxindex),
10120 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10121 layout_type (main_type);
10123 /* Make sure we have the canonical MAIN_TYPE. */
10124 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10125 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10126 hashcode);
10127 main_type = type_hash_canon (hashcode, main_type);
10129 /* Fix the canonical type. */
10130 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10131 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10132 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10133 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10134 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10135 != TYPE_DOMAIN (main_type)))
10136 TYPE_CANONICAL (main_type)
10137 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10138 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10139 else
10140 TYPE_CANONICAL (main_type) = main_type;
10142 if (quals == 0)
10143 type = main_type;
10144 else
10145 type = c_build_qualified_type (main_type, quals);
10147 if (COMPLETE_TYPE_P (type)
10148 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10149 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10151 error ("size of array is too large");
10152 /* If we proceed with the array type as it is, we'll eventually
10153 crash in tree_low_cst(). */
10154 type = error_mark_node;
10157 *ptype = type;
10158 return failure;
10161 /* Like c_mark_addressable but don't check register qualifier. */
10162 void
10163 c_common_mark_addressable_vec (tree t)
10165 while (handled_component_p (t))
10166 t = TREE_OPERAND (t, 0);
10167 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10168 return;
10169 TREE_ADDRESSABLE (t) = 1;
10174 /* Used to help initialize the builtin-types.def table. When a type of
10175 the correct size doesn't exist, use error_mark_node instead of NULL.
10176 The later results in segfaults even when a decl using the type doesn't
10177 get invoked. */
10179 tree
10180 builtin_type_for_size (int size, bool unsignedp)
10182 tree type = c_common_type_for_size (size, unsignedp);
10183 return type ? type : error_mark_node;
10186 /* A helper function for resolve_overloaded_builtin in resolving the
10187 overloaded __sync_ builtins. Returns a positive power of 2 if the
10188 first operand of PARAMS is a pointer to a supported data type.
10189 Returns 0 if an error is encountered. */
10191 static int
10192 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10194 tree type;
10195 int size;
10197 if (!params)
10199 error ("too few arguments to function %qE", function);
10200 return 0;
10203 type = TREE_TYPE ((*params)[0]);
10204 if (TREE_CODE (type) != POINTER_TYPE)
10205 goto incompatible;
10207 type = TREE_TYPE (type);
10208 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10209 goto incompatible;
10211 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10212 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10213 return size;
10215 incompatible:
10216 error ("incompatible type for argument %d of %qE", 1, function);
10217 return 0;
10220 /* A helper function for resolve_overloaded_builtin. Adds casts to
10221 PARAMS to make arguments match up with those of FUNCTION. Drops
10222 the variadic arguments at the end. Returns false if some error
10223 was encountered; true on success. */
10225 static bool
10226 sync_resolve_params (location_t loc, tree orig_function, tree function,
10227 vec<tree, va_gc> *params, bool orig_format)
10229 function_args_iterator iter;
10230 tree ptype;
10231 unsigned int parmnum;
10233 function_args_iter_init (&iter, TREE_TYPE (function));
10234 /* We've declared the implementation functions to use "volatile void *"
10235 as the pointer parameter, so we shouldn't get any complaints from the
10236 call to check_function_arguments what ever type the user used. */
10237 function_args_iter_next (&iter);
10238 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10239 ptype = TYPE_MAIN_VARIANT (ptype);
10241 /* For the rest of the values, we need to cast these to FTYPE, so that we
10242 don't get warnings for passing pointer types, etc. */
10243 parmnum = 0;
10244 while (1)
10246 tree val, arg_type;
10248 arg_type = function_args_iter_cond (&iter);
10249 /* XXX void_type_node belies the abstraction. */
10250 if (arg_type == void_type_node)
10251 break;
10253 ++parmnum;
10254 if (params->length () <= parmnum)
10256 error_at (loc, "too few arguments to function %qE", orig_function);
10257 return false;
10260 /* Only convert parameters if arg_type is unsigned integer type with
10261 new format sync routines, i.e. don't attempt to convert pointer
10262 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10263 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10264 kinds). */
10265 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10267 /* Ideally for the first conversion we'd use convert_for_assignment
10268 so that we get warnings for anything that doesn't match the pointer
10269 type. This isn't portable across the C and C++ front ends atm. */
10270 val = (*params)[parmnum];
10271 val = convert (ptype, val);
10272 val = convert (arg_type, val);
10273 (*params)[parmnum] = val;
10276 function_args_iter_next (&iter);
10279 /* __atomic routines are not variadic. */
10280 if (!orig_format && params->length () != parmnum + 1)
10282 error_at (loc, "too many arguments to function %qE", orig_function);
10283 return false;
10286 /* The definition of these primitives is variadic, with the remaining
10287 being "an optional list of variables protected by the memory barrier".
10288 No clue what that's supposed to mean, precisely, but we consider all
10289 call-clobbered variables to be protected so we're safe. */
10290 params->truncate (parmnum + 1);
10292 return true;
10295 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10296 RESULT to make it match the type of the first pointer argument in
10297 PARAMS. */
10299 static tree
10300 sync_resolve_return (tree first_param, tree result, bool orig_format)
10302 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10303 tree rtype = TREE_TYPE (result);
10304 ptype = TYPE_MAIN_VARIANT (ptype);
10306 /* New format doesn't require casting unless the types are the same size. */
10307 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10308 return convert (ptype, result);
10309 else
10310 return result;
10313 /* This function verifies the PARAMS to generic atomic FUNCTION.
10314 It returns the size if all the parameters are the same size, otherwise
10315 0 is returned if the parameters are invalid. */
10317 static int
10318 get_atomic_generic_size (location_t loc, tree function,
10319 vec<tree, va_gc> *params)
10321 unsigned int n_param;
10322 unsigned int n_model;
10323 unsigned int x;
10324 int size_0;
10325 tree type_0;
10327 /* Determine the parameter makeup. */
10328 switch (DECL_FUNCTION_CODE (function))
10330 case BUILT_IN_ATOMIC_EXCHANGE:
10331 n_param = 4;
10332 n_model = 1;
10333 break;
10334 case BUILT_IN_ATOMIC_LOAD:
10335 case BUILT_IN_ATOMIC_STORE:
10336 n_param = 3;
10337 n_model = 1;
10338 break;
10339 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10340 n_param = 6;
10341 n_model = 2;
10342 break;
10343 default:
10344 gcc_unreachable ();
10347 if (vec_safe_length (params) != n_param)
10349 error_at (loc, "incorrect number of arguments to function %qE", function);
10350 return 0;
10353 /* Get type of first parameter, and determine its size. */
10354 type_0 = TREE_TYPE ((*params)[0]);
10355 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10357 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10358 function);
10359 return 0;
10362 /* Types must be compile time constant sizes. */
10363 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10365 error_at (loc,
10366 "argument 1 of %qE must be a pointer to a constant size type",
10367 function);
10368 return 0;
10371 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10373 /* Zero size objects are not allowed. */
10374 if (size_0 == 0)
10376 error_at (loc,
10377 "argument 1 of %qE must be a pointer to a nonzero size object",
10378 function);
10379 return 0;
10382 /* Check each other parameter is a pointer and the same size. */
10383 for (x = 0; x < n_param - n_model; x++)
10385 int size;
10386 tree type = TREE_TYPE ((*params)[x]);
10387 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10388 if (n_param == 6 && x == 3)
10389 continue;
10390 if (!POINTER_TYPE_P (type))
10392 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10393 function);
10394 return 0;
10396 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10397 if (size != size_0)
10399 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10400 function);
10401 return 0;
10405 /* Check memory model parameters for validity. */
10406 for (x = n_param - n_model ; x < n_param; x++)
10408 tree p = (*params)[x];
10409 if (TREE_CODE (p) == INTEGER_CST)
10411 int i = tree_low_cst (p, 1);
10412 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10414 warning_at (loc, OPT_Winvalid_memory_model,
10415 "invalid memory model argument %d of %qE", x + 1,
10416 function);
10419 else
10420 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10422 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10423 function);
10424 return 0;
10428 return size_0;
10432 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10433 at the beginning of the parameter list PARAMS representing the size of the
10434 objects. This is to match the library ABI requirement. LOC is the location
10435 of the function call.
10436 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10437 returned to allow the external call to be constructed. */
10439 static tree
10440 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10441 vec<tree, va_gc> *params)
10443 tree size_node;
10445 /* Insert a SIZE_T parameter as the first param. If there isn't
10446 enough space, allocate a new vector and recursively re-build with that. */
10447 if (!params->space (1))
10449 unsigned int z, len;
10450 vec<tree, va_gc> *v;
10451 tree f;
10453 len = params->length ();
10454 vec_alloc (v, len + 1);
10455 for (z = 0; z < len; z++)
10456 v->quick_push ((*params)[z]);
10457 f = build_function_call_vec (loc, function, v, NULL);
10458 vec_free (v);
10459 return f;
10462 /* Add the size parameter and leave as a function call for processing. */
10463 size_node = build_int_cst (size_type_node, n);
10464 params->quick_insert (0, size_node);
10465 return NULL_TREE;
10469 /* Return whether atomic operations for naturally aligned N-byte
10470 arguments are supported, whether inline or through libatomic. */
10471 static bool
10472 atomic_size_supported_p (int n)
10474 switch (n)
10476 case 1:
10477 case 2:
10478 case 4:
10479 case 8:
10480 return true;
10482 case 16:
10483 return targetm.scalar_mode_supported_p (TImode);
10485 default:
10486 return false;
10490 /* This will process an __atomic_exchange function call, determine whether it
10491 needs to be mapped to the _N variation, or turned into a library call.
10492 LOC is the location of the builtin call.
10493 FUNCTION is the DECL that has been invoked;
10494 PARAMS is the argument list for the call. The return value is non-null
10495 TRUE is returned if it is translated into the proper format for a call to the
10496 external library, and NEW_RETURN is set the tree for that function.
10497 FALSE is returned if processing for the _N variation is required, and
10498 NEW_RETURN is set to the the return value the result is copied into. */
10499 static bool
10500 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10501 vec<tree, va_gc> *params, tree *new_return)
10503 tree p0, p1, p2, p3;
10504 tree I_type, I_type_ptr;
10505 int n = get_atomic_generic_size (loc, function, params);
10507 /* Size of 0 is an error condition. */
10508 if (n == 0)
10510 *new_return = error_mark_node;
10511 return true;
10514 /* If not a lock-free size, change to the library generic format. */
10515 if (!atomic_size_supported_p (n))
10517 *new_return = add_atomic_size_parameter (n, loc, function, params);
10518 return true;
10521 /* Otherwise there is a lockfree match, transform the call from:
10522 void fn(T* mem, T* desired, T* return, model)
10523 into
10524 *return = (T) (fn (In* mem, (In) *desired, model)) */
10526 p0 = (*params)[0];
10527 p1 = (*params)[1];
10528 p2 = (*params)[2];
10529 p3 = (*params)[3];
10531 /* Create pointer to appropriate size. */
10532 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10533 I_type_ptr = build_pointer_type (I_type);
10535 /* Convert object pointer to required type. */
10536 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10537 (*params)[0] = p0;
10538 /* Convert new value to required type, and dereference it. */
10539 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10540 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10541 (*params)[1] = p1;
10543 /* Move memory model to the 3rd position, and end param list. */
10544 (*params)[2] = p3;
10545 params->truncate (3);
10547 /* Convert return pointer and dereference it for later assignment. */
10548 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10550 return false;
10554 /* This will process an __atomic_compare_exchange function call, determine
10555 whether it needs to be mapped to the _N variation, or turned into a lib call.
10556 LOC is the location of the builtin call.
10557 FUNCTION is the DECL that has been invoked;
10558 PARAMS is the argument list for the call. The return value is non-null
10559 TRUE is returned if it is translated into the proper format for a call to the
10560 external library, and NEW_RETURN is set the tree for that function.
10561 FALSE is returned if processing for the _N variation is required. */
10563 static bool
10564 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10565 vec<tree, va_gc> *params,
10566 tree *new_return)
10568 tree p0, p1, p2;
10569 tree I_type, I_type_ptr;
10570 int n = get_atomic_generic_size (loc, function, params);
10572 /* Size of 0 is an error condition. */
10573 if (n == 0)
10575 *new_return = error_mark_node;
10576 return true;
10579 /* If not a lock-free size, change to the library generic format. */
10580 if (!atomic_size_supported_p (n))
10582 /* The library generic format does not have the weak parameter, so
10583 remove it from the param list. Since a parameter has been removed,
10584 we can be sure that there is room for the SIZE_T parameter, meaning
10585 there will not be a recursive rebuilding of the parameter list, so
10586 there is no danger this will be done twice. */
10587 if (n > 0)
10589 (*params)[3] = (*params)[4];
10590 (*params)[4] = (*params)[5];
10591 params->truncate (5);
10593 *new_return = add_atomic_size_parameter (n, loc, function, params);
10594 return true;
10597 /* Otherwise, there is a match, so the call needs to be transformed from:
10598 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10599 into
10600 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10602 p0 = (*params)[0];
10603 p1 = (*params)[1];
10604 p2 = (*params)[2];
10606 /* Create pointer to appropriate size. */
10607 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10608 I_type_ptr = build_pointer_type (I_type);
10610 /* Convert object pointer to required type. */
10611 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10612 (*params)[0] = p0;
10614 /* Convert expected pointer to required type. */
10615 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10616 (*params)[1] = p1;
10618 /* Convert desired value to required type, and dereference it. */
10619 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10620 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10621 (*params)[2] = p2;
10623 /* The rest of the parameters are fine. NULL means no special return value
10624 processing.*/
10625 *new_return = NULL;
10626 return false;
10630 /* This will process an __atomic_load function call, determine whether it
10631 needs to be mapped to the _N variation, or turned into a library call.
10632 LOC is the location of the builtin call.
10633 FUNCTION is the DECL that has been invoked;
10634 PARAMS is the argument list for the call. The return value is non-null
10635 TRUE is returned if it is translated into the proper format for a call to the
10636 external library, and NEW_RETURN is set the tree for that function.
10637 FALSE is returned if processing for the _N variation is required, and
10638 NEW_RETURN is set to the the return value the result is copied into. */
10640 static bool
10641 resolve_overloaded_atomic_load (location_t loc, tree function,
10642 vec<tree, va_gc> *params, tree *new_return)
10644 tree p0, p1, p2;
10645 tree I_type, I_type_ptr;
10646 int n = get_atomic_generic_size (loc, function, params);
10648 /* Size of 0 is an error condition. */
10649 if (n == 0)
10651 *new_return = error_mark_node;
10652 return true;
10655 /* If not a lock-free size, change to the library generic format. */
10656 if (!atomic_size_supported_p (n))
10658 *new_return = add_atomic_size_parameter (n, loc, function, params);
10659 return true;
10662 /* Otherwise, there is a match, so the call needs to be transformed from:
10663 void fn(T* mem, T* return, model)
10664 into
10665 *return = (T) (fn ((In *) mem, model)) */
10667 p0 = (*params)[0];
10668 p1 = (*params)[1];
10669 p2 = (*params)[2];
10671 /* Create pointer to appropriate size. */
10672 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10673 I_type_ptr = build_pointer_type (I_type);
10675 /* Convert object pointer to required type. */
10676 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10677 (*params)[0] = p0;
10679 /* Move memory model to the 2nd position, and end param list. */
10680 (*params)[1] = p2;
10681 params->truncate (2);
10683 /* Convert return pointer and dereference it for later assignment. */
10684 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10686 return false;
10690 /* This will process an __atomic_store function call, determine whether it
10691 needs to be mapped to the _N variation, or turned into a library call.
10692 LOC is the location of the builtin call.
10693 FUNCTION is the DECL that has been invoked;
10694 PARAMS is the argument list for the call. The return value is non-null
10695 TRUE is returned if it is translated into the proper format for a call to the
10696 external library, and NEW_RETURN is set the tree for that function.
10697 FALSE is returned if processing for the _N variation is required, and
10698 NEW_RETURN is set to the the return value the result is copied into. */
10700 static bool
10701 resolve_overloaded_atomic_store (location_t loc, tree function,
10702 vec<tree, va_gc> *params, tree *new_return)
10704 tree p0, p1;
10705 tree I_type, I_type_ptr;
10706 int n = get_atomic_generic_size (loc, function, params);
10708 /* Size of 0 is an error condition. */
10709 if (n == 0)
10711 *new_return = error_mark_node;
10712 return true;
10715 /* If not a lock-free size, change to the library generic format. */
10716 if (!atomic_size_supported_p (n))
10718 *new_return = add_atomic_size_parameter (n, loc, function, params);
10719 return true;
10722 /* Otherwise, there is a match, so the call needs to be transformed from:
10723 void fn(T* mem, T* value, model)
10724 into
10725 fn ((In *) mem, (In) *value, model) */
10727 p0 = (*params)[0];
10728 p1 = (*params)[1];
10730 /* Create pointer to appropriate size. */
10731 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10732 I_type_ptr = build_pointer_type (I_type);
10734 /* Convert object pointer to required type. */
10735 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10736 (*params)[0] = p0;
10738 /* Convert new value to required type, and dereference it. */
10739 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10740 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10741 (*params)[1] = p1;
10743 /* The memory model is in the right spot already. Return is void. */
10744 *new_return = NULL_TREE;
10746 return false;
10750 /* Some builtin functions are placeholders for other expressions. This
10751 function should be called immediately after parsing the call expression
10752 before surrounding code has committed to the type of the expression.
10754 LOC is the location of the builtin call.
10756 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10757 PARAMS is the argument list for the call. The return value is non-null
10758 when expansion is complete, and null if normal processing should
10759 continue. */
10761 tree
10762 resolve_overloaded_builtin (location_t loc, tree function,
10763 vec<tree, va_gc> *params)
10765 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10766 bool orig_format = true;
10767 tree new_return = NULL_TREE;
10769 switch (DECL_BUILT_IN_CLASS (function))
10771 case BUILT_IN_NORMAL:
10772 break;
10773 case BUILT_IN_MD:
10774 if (targetm.resolve_overloaded_builtin)
10775 return targetm.resolve_overloaded_builtin (loc, function, params);
10776 else
10777 return NULL_TREE;
10778 default:
10779 return NULL_TREE;
10782 /* Handle BUILT_IN_NORMAL here. */
10783 switch (orig_code)
10785 case BUILT_IN_ATOMIC_EXCHANGE:
10786 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10787 case BUILT_IN_ATOMIC_LOAD:
10788 case BUILT_IN_ATOMIC_STORE:
10790 /* Handle these 4 together so that they can fall through to the next
10791 case if the call is transformed to an _N variant. */
10792 switch (orig_code)
10794 case BUILT_IN_ATOMIC_EXCHANGE:
10796 if (resolve_overloaded_atomic_exchange (loc, function, params,
10797 &new_return))
10798 return new_return;
10799 /* Change to the _N variant. */
10800 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10801 break;
10804 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10806 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10807 params,
10808 &new_return))
10809 return new_return;
10810 /* Change to the _N variant. */
10811 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10812 break;
10814 case BUILT_IN_ATOMIC_LOAD:
10816 if (resolve_overloaded_atomic_load (loc, function, params,
10817 &new_return))
10818 return new_return;
10819 /* Change to the _N variant. */
10820 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10821 break;
10823 case BUILT_IN_ATOMIC_STORE:
10825 if (resolve_overloaded_atomic_store (loc, function, params,
10826 &new_return))
10827 return new_return;
10828 /* Change to the _N variant. */
10829 orig_code = BUILT_IN_ATOMIC_STORE_N;
10830 break;
10832 default:
10833 gcc_unreachable ();
10835 /* Fallthrough to the normal processing. */
10837 case BUILT_IN_ATOMIC_EXCHANGE_N:
10838 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10839 case BUILT_IN_ATOMIC_LOAD_N:
10840 case BUILT_IN_ATOMIC_STORE_N:
10841 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10842 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10843 case BUILT_IN_ATOMIC_AND_FETCH_N:
10844 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10845 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10846 case BUILT_IN_ATOMIC_OR_FETCH_N:
10847 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10848 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10849 case BUILT_IN_ATOMIC_FETCH_AND_N:
10850 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10851 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10852 case BUILT_IN_ATOMIC_FETCH_OR_N:
10854 orig_format = false;
10855 /* Fallthru for parameter processing. */
10857 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10858 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10859 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10860 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10861 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10862 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10863 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10864 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10865 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10866 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10867 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10868 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10869 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10870 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10871 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10872 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10874 int n = sync_resolve_size (function, params);
10875 tree new_function, first_param, result;
10876 enum built_in_function fncode;
10878 if (n == 0)
10879 return error_mark_node;
10881 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10882 new_function = builtin_decl_explicit (fncode);
10883 if (!sync_resolve_params (loc, function, new_function, params,
10884 orig_format))
10885 return error_mark_node;
10887 first_param = (*params)[0];
10888 result = build_function_call_vec (loc, new_function, params, NULL);
10889 if (result == error_mark_node)
10890 return result;
10891 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10892 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10893 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10894 result = sync_resolve_return (first_param, result, orig_format);
10896 /* If new_return is set, assign function to that expr and cast the
10897 result to void since the generic interface returned void. */
10898 if (new_return)
10900 /* Cast function result from I{1,2,4,8,16} to the required type. */
10901 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10902 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10903 result);
10904 TREE_SIDE_EFFECTS (result) = 1;
10905 protected_set_expr_location (result, loc);
10906 result = convert (void_type_node, result);
10908 return result;
10911 default:
10912 return NULL_TREE;
10916 /* vector_types_compatible_elements_p is used in type checks of vectors
10917 values used as operands of binary operators. Where it returns true, and
10918 the other checks of the caller succeed (being vector types in he first
10919 place, and matching number of elements), we can just treat the types
10920 as essentially the same.
10921 Contrast with vector_targets_convertible_p, which is used for vector
10922 pointer types, and vector_types_convertible_p, which will allow
10923 language-specific matches under the control of flag_lax_vector_conversions,
10924 and might still require a conversion. */
10925 /* True if vector types T1 and T2 can be inputs to the same binary
10926 operator without conversion.
10927 We don't check the overall vector size here because some of our callers
10928 want to give different error messages when the vectors are compatible
10929 except for the element count. */
10931 bool
10932 vector_types_compatible_elements_p (tree t1, tree t2)
10934 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10935 t1 = TREE_TYPE (t1);
10936 t2 = TREE_TYPE (t2);
10938 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10940 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10941 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10942 || c2 == FIXED_POINT_TYPE));
10944 t1 = c_common_signed_type (t1);
10945 t2 = c_common_signed_type (t2);
10946 /* Equality works here because c_common_signed_type uses
10947 TYPE_MAIN_VARIANT. */
10948 if (t1 == t2)
10949 return true;
10950 if (opaque && c1 == c2
10951 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10952 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10953 return true;
10954 return false;
10957 /* Check for missing format attributes on function pointers. LTYPE is
10958 the new type or left-hand side type. RTYPE is the old type or
10959 right-hand side type. Returns TRUE if LTYPE is missing the desired
10960 attribute. */
10962 bool
10963 check_missing_format_attribute (tree ltype, tree rtype)
10965 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10966 tree ra;
10968 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10969 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10970 break;
10971 if (ra)
10973 tree la;
10974 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10975 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10976 break;
10977 return !la;
10979 else
10980 return false;
10983 /* Subscripting with type char is likely to lose on a machine where
10984 chars are signed. So warn on any machine, but optionally. Don't
10985 warn for unsigned char since that type is safe. Don't warn for
10986 signed char because anyone who uses that must have done so
10987 deliberately. Furthermore, we reduce the false positive load by
10988 warning only for non-constant value of type char. */
10990 void
10991 warn_array_subscript_with_type_char (tree index)
10993 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10994 && TREE_CODE (index) != INTEGER_CST)
10995 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10998 /* Implement -Wparentheses for the unexpected C precedence rules, to
10999 cover cases like x + y << z which readers are likely to
11000 misinterpret. We have seen an expression in which CODE is a binary
11001 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11002 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11003 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11004 expression was not formed using a binary or unary operator, or it
11005 was enclosed in parentheses. */
11007 void
11008 warn_about_parentheses (location_t loc, enum tree_code code,
11009 enum tree_code code_left, tree arg_left,
11010 enum tree_code code_right, tree arg_right)
11012 if (!warn_parentheses)
11013 return;
11015 /* This macro tests that the expression ARG with original tree code
11016 CODE appears to be a boolean expression. or the result of folding a
11017 boolean expression. */
11018 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11019 (truth_value_p (TREE_CODE (ARG)) \
11020 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11021 /* Folding may create 0 or 1 integers from other expressions. */ \
11022 || ((CODE) != INTEGER_CST \
11023 && (integer_onep (ARG) || integer_zerop (ARG))))
11025 switch (code)
11027 case LSHIFT_EXPR:
11028 if (code_left == PLUS_EXPR)
11029 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11030 "suggest parentheses around %<+%> inside %<<<%>");
11031 else if (code_right == PLUS_EXPR)
11032 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11033 "suggest parentheses around %<+%> inside %<<<%>");
11034 else if (code_left == MINUS_EXPR)
11035 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11036 "suggest parentheses around %<-%> inside %<<<%>");
11037 else if (code_right == MINUS_EXPR)
11038 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11039 "suggest parentheses around %<-%> inside %<<<%>");
11040 return;
11042 case RSHIFT_EXPR:
11043 if (code_left == PLUS_EXPR)
11044 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11045 "suggest parentheses around %<+%> inside %<>>%>");
11046 else if (code_right == PLUS_EXPR)
11047 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11048 "suggest parentheses around %<+%> inside %<>>%>");
11049 else if (code_left == MINUS_EXPR)
11050 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11051 "suggest parentheses around %<-%> inside %<>>%>");
11052 else if (code_right == MINUS_EXPR)
11053 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11054 "suggest parentheses around %<-%> inside %<>>%>");
11055 return;
11057 case TRUTH_ORIF_EXPR:
11058 if (code_left == TRUTH_ANDIF_EXPR)
11059 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11060 "suggest parentheses around %<&&%> within %<||%>");
11061 else if (code_right == TRUTH_ANDIF_EXPR)
11062 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11063 "suggest parentheses around %<&&%> within %<||%>");
11064 return;
11066 case BIT_IOR_EXPR:
11067 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11068 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11069 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11070 "suggest parentheses around arithmetic in operand of %<|%>");
11071 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11072 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11073 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11074 "suggest parentheses around arithmetic in operand of %<|%>");
11075 /* Check cases like x|y==z */
11076 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11077 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11078 "suggest parentheses around comparison in operand of %<|%>");
11079 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11080 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11081 "suggest parentheses around comparison in operand of %<|%>");
11082 /* Check cases like !x | y */
11083 else if (code_left == TRUTH_NOT_EXPR
11084 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11085 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11086 "suggest parentheses around operand of "
11087 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11088 return;
11090 case BIT_XOR_EXPR:
11091 if (code_left == BIT_AND_EXPR
11092 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11093 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11094 "suggest parentheses around arithmetic in operand of %<^%>");
11095 else if (code_right == BIT_AND_EXPR
11096 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11097 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11098 "suggest parentheses around arithmetic in operand of %<^%>");
11099 /* Check cases like x^y==z */
11100 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11101 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11102 "suggest parentheses around comparison in operand of %<^%>");
11103 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11104 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11105 "suggest parentheses around comparison in operand of %<^%>");
11106 return;
11108 case BIT_AND_EXPR:
11109 if (code_left == PLUS_EXPR)
11110 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11111 "suggest parentheses around %<+%> in operand of %<&%>");
11112 else if (code_right == PLUS_EXPR)
11113 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11114 "suggest parentheses around %<+%> in operand of %<&%>");
11115 else if (code_left == MINUS_EXPR)
11116 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11117 "suggest parentheses around %<-%> in operand of %<&%>");
11118 else if (code_right == MINUS_EXPR)
11119 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11120 "suggest parentheses around %<-%> in operand of %<&%>");
11121 /* Check cases like x&y==z */
11122 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11123 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11124 "suggest parentheses around comparison in operand of %<&%>");
11125 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11126 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11127 "suggest parentheses around comparison in operand of %<&%>");
11128 /* Check cases like !x & y */
11129 else if (code_left == TRUTH_NOT_EXPR
11130 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11131 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11132 "suggest parentheses around operand of "
11133 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11134 return;
11136 case EQ_EXPR:
11137 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11138 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11139 "suggest parentheses around comparison in operand of %<==%>");
11140 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11141 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11142 "suggest parentheses around comparison in operand of %<==%>");
11143 return;
11144 case NE_EXPR:
11145 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11146 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11147 "suggest parentheses around comparison in operand of %<!=%>");
11148 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11149 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11150 "suggest parentheses around comparison in operand of %<!=%>");
11151 return;
11153 default:
11154 if (TREE_CODE_CLASS (code) == tcc_comparison)
11156 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11157 && code_left != NE_EXPR && code_left != EQ_EXPR
11158 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11159 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11160 "comparisons like %<X<=Y<=Z%> do not "
11161 "have their mathematical meaning");
11162 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11163 && code_right != NE_EXPR && code_right != EQ_EXPR
11164 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11165 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11166 "comparisons like %<X<=Y<=Z%> do not "
11167 "have their mathematical meaning");
11169 return;
11171 #undef NOT_A_BOOLEAN_EXPR_P
11174 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11176 void
11177 warn_for_unused_label (tree label)
11179 if (!TREE_USED (label))
11181 if (DECL_INITIAL (label))
11182 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11183 else
11184 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11188 /* Warn for division by zero according to the value of DIVISOR. LOC
11189 is the location of the division operator. */
11191 void
11192 warn_for_div_by_zero (location_t loc, tree divisor)
11194 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11195 about division by zero. Do not issue a warning if DIVISOR has a
11196 floating-point type, since we consider 0.0/0.0 a valid way of
11197 generating a NaN. */
11198 if (c_inhibit_evaluation_warnings == 0
11199 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11200 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11203 /* Subroutine of build_binary_op. Give warnings for comparisons
11204 between signed and unsigned quantities that may fail. Do the
11205 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11206 so that casts will be considered, but default promotions won't
11209 LOCATION is the location of the comparison operator.
11211 The arguments of this function map directly to local variables
11212 of build_binary_op. */
11214 void
11215 warn_for_sign_compare (location_t location,
11216 tree orig_op0, tree orig_op1,
11217 tree op0, tree op1,
11218 tree result_type, enum tree_code resultcode)
11220 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11221 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11222 int unsignedp0, unsignedp1;
11224 /* In C++, check for comparison of different enum types. */
11225 if (c_dialect_cxx()
11226 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11227 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11228 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11229 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11231 warning_at (location,
11232 OPT_Wsign_compare, "comparison between types %qT and %qT",
11233 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11236 /* Do not warn if the comparison is being done in a signed type,
11237 since the signed type will only be chosen if it can represent
11238 all the values of the unsigned type. */
11239 if (!TYPE_UNSIGNED (result_type))
11240 /* OK */;
11241 /* Do not warn if both operands are unsigned. */
11242 else if (op0_signed == op1_signed)
11243 /* OK */;
11244 else
11246 tree sop, uop, base_type;
11247 bool ovf;
11249 if (op0_signed)
11250 sop = orig_op0, uop = orig_op1;
11251 else
11252 sop = orig_op1, uop = orig_op0;
11254 STRIP_TYPE_NOPS (sop);
11255 STRIP_TYPE_NOPS (uop);
11256 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11257 ? TREE_TYPE (result_type) : result_type);
11259 /* Do not warn if the signed quantity is an unsuffixed integer
11260 literal (or some static constant expression involving such
11261 literals or a conditional expression involving such literals)
11262 and it is non-negative. */
11263 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11264 /* OK */;
11265 /* Do not warn if the comparison is an equality operation, the
11266 unsigned quantity is an integral constant, and it would fit
11267 in the result if the result were signed. */
11268 else if (TREE_CODE (uop) == INTEGER_CST
11269 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11270 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11271 /* OK */;
11272 /* In C, do not warn if the unsigned quantity is an enumeration
11273 constant and its maximum value would fit in the result if the
11274 result were signed. */
11275 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11276 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11277 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11278 c_common_signed_type (base_type)))
11279 /* OK */;
11280 else
11281 warning_at (location,
11282 OPT_Wsign_compare,
11283 "comparison between signed and unsigned integer expressions");
11286 /* Warn if two unsigned values are being compared in a size larger
11287 than their original size, and one (and only one) is the result of
11288 a `~' operator. This comparison will always fail.
11290 Also warn if one operand is a constant, and the constant does not
11291 have all bits set that are set in the ~ operand when it is
11292 extended. */
11294 op0 = c_common_get_narrower (op0, &unsignedp0);
11295 op1 = c_common_get_narrower (op1, &unsignedp1);
11297 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11298 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11300 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11301 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11302 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11303 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11305 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11307 tree primop;
11308 HOST_WIDE_INT constant, mask;
11309 int unsignedp;
11310 unsigned int bits;
11312 if (host_integerp (op0, 0))
11314 primop = op1;
11315 unsignedp = unsignedp1;
11316 constant = tree_low_cst (op0, 0);
11318 else
11320 primop = op0;
11321 unsignedp = unsignedp0;
11322 constant = tree_low_cst (op1, 0);
11325 bits = TYPE_PRECISION (TREE_TYPE (primop));
11326 if (bits < TYPE_PRECISION (result_type)
11327 && bits < HOST_BITS_PER_LONG && unsignedp)
11329 mask = (~ (HOST_WIDE_INT) 0) << bits;
11330 if ((mask & constant) != mask)
11332 if (constant == 0)
11333 warning (OPT_Wsign_compare,
11334 "promoted ~unsigned is always non-zero");
11335 else
11336 warning_at (location, OPT_Wsign_compare,
11337 "comparison of promoted ~unsigned with constant");
11341 else if (unsignedp0 && unsignedp1
11342 && (TYPE_PRECISION (TREE_TYPE (op0))
11343 < TYPE_PRECISION (result_type))
11344 && (TYPE_PRECISION (TREE_TYPE (op1))
11345 < TYPE_PRECISION (result_type)))
11346 warning_at (location, OPT_Wsign_compare,
11347 "comparison of promoted ~unsigned with unsigned");
11351 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11352 type via c_common_type. If -Wdouble-promotion is in use, and the
11353 conditions for warning have been met, issue a warning. GMSGID is
11354 the warning message. It must have two %T specifiers for the type
11355 that was converted (generally "float") and the type to which it was
11356 converted (generally "double), respectively. LOC is the location
11357 to which the awrning should refer. */
11359 void
11360 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11361 const char *gmsgid, location_t loc)
11363 tree source_type;
11365 if (!warn_double_promotion)
11366 return;
11367 /* If the conversion will not occur at run-time, there is no need to
11368 warn about it. */
11369 if (c_inhibit_evaluation_warnings)
11370 return;
11371 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11372 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11373 return;
11374 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11375 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11376 source_type = type1;
11377 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11378 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11379 source_type = type2;
11380 else
11381 return;
11382 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11385 /* Setup a TYPE_DECL node as a typedef representation.
11387 X is a TYPE_DECL for a typedef statement. Create a brand new
11388 ..._TYPE node (which will be just a variant of the existing
11389 ..._TYPE node with identical properties) and then install X
11390 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11392 The whole point here is to end up with a situation where each
11393 and every ..._TYPE node the compiler creates will be uniquely
11394 associated with AT MOST one node representing a typedef name.
11395 This way, even though the compiler substitutes corresponding
11396 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11397 early on, later parts of the compiler can always do the reverse
11398 translation and get back the corresponding typedef name. For
11399 example, given:
11401 typedef struct S MY_TYPE;
11402 MY_TYPE object;
11404 Later parts of the compiler might only know that `object' was of
11405 type `struct S' if it were not for code just below. With this
11406 code however, later parts of the compiler see something like:
11408 struct S' == struct S
11409 typedef struct S' MY_TYPE;
11410 struct S' object;
11412 And they can then deduce (from the node for type struct S') that
11413 the original object declaration was:
11415 MY_TYPE object;
11417 Being able to do this is important for proper support of protoize,
11418 and also for generating precise symbolic debugging information
11419 which takes full account of the programmer's (typedef) vocabulary.
11421 Obviously, we don't want to generate a duplicate ..._TYPE node if
11422 the TYPE_DECL node that we are now processing really represents a
11423 standard built-in type. */
11425 void
11426 set_underlying_type (tree x)
11428 if (x == error_mark_node)
11429 return;
11430 if (DECL_IS_BUILTIN (x))
11432 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11433 TYPE_NAME (TREE_TYPE (x)) = x;
11435 else if (TREE_TYPE (x) != error_mark_node
11436 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11438 tree tt = TREE_TYPE (x);
11439 DECL_ORIGINAL_TYPE (x) = tt;
11440 tt = build_variant_type_copy (tt);
11441 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11442 TYPE_NAME (tt) = x;
11443 TREE_USED (tt) = TREE_USED (x);
11444 TREE_TYPE (x) = tt;
11448 /* Record the types used by the current global variable declaration
11449 being parsed, so that we can decide later to emit their debug info.
11450 Those types are in types_used_by_cur_var_decl, and we are going to
11451 store them in the types_used_by_vars_hash hash table.
11452 DECL is the declaration of the global variable that has been parsed. */
11454 void
11455 record_types_used_by_current_var_decl (tree decl)
11457 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11459 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11461 tree type = types_used_by_cur_var_decl->pop ();
11462 types_used_by_var_decl_insert (type, decl);
11466 /* If DECL is a typedef that is declared in the current function,
11467 record it for the purpose of -Wunused-local-typedefs. */
11469 void
11470 record_locally_defined_typedef (tree decl)
11472 struct c_language_function *l;
11474 if (!warn_unused_local_typedefs
11475 || cfun == NULL
11476 /* if this is not a locally defined typedef then we are not
11477 interested. */
11478 || !is_typedef_decl (decl)
11479 || !decl_function_context (decl))
11480 return;
11482 l = (struct c_language_function *) cfun->language;
11483 vec_safe_push (l->local_typedefs, decl);
11486 /* If T is a TYPE_DECL declared locally, mark it as used. */
11488 void
11489 maybe_record_typedef_use (tree t)
11491 if (!is_typedef_decl (t))
11492 return;
11494 TREE_USED (t) = true;
11497 /* Warn if there are some unused locally defined typedefs in the
11498 current function. */
11500 void
11501 maybe_warn_unused_local_typedefs (void)
11503 int i;
11504 tree decl;
11505 /* The number of times we have emitted -Wunused-local-typedefs
11506 warnings. If this is different from errorcount, that means some
11507 unrelated errors have been issued. In which case, we'll avoid
11508 emitting "unused-local-typedefs" warnings. */
11509 static int unused_local_typedefs_warn_count;
11510 struct c_language_function *l;
11512 if (cfun == NULL)
11513 return;
11515 if ((l = (struct c_language_function *) cfun->language) == NULL)
11516 return;
11518 if (warn_unused_local_typedefs
11519 && errorcount == unused_local_typedefs_warn_count)
11521 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11522 if (!TREE_USED (decl))
11523 warning_at (DECL_SOURCE_LOCATION (decl),
11524 OPT_Wunused_local_typedefs,
11525 "typedef %qD locally defined but not used", decl);
11526 unused_local_typedefs_warn_count = errorcount;
11529 vec_free (l->local_typedefs);
11532 /* The C and C++ parsers both use vectors to hold function arguments.
11533 For efficiency, we keep a cache of unused vectors. This is the
11534 cache. */
11536 typedef vec<tree, va_gc> *tree_gc_vec;
11537 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11539 /* Return a new vector from the cache. If the cache is empty,
11540 allocate a new vector. These vectors are GC'ed, so it is OK if the
11541 pointer is not released.. */
11543 vec<tree, va_gc> *
11544 make_tree_vector (void)
11546 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11547 return tree_vector_cache->pop ();
11548 else
11550 /* Passing 0 to vec::alloc returns NULL, and our callers require
11551 that we always return a non-NULL value. The vector code uses
11552 4 when growing a NULL vector, so we do too. */
11553 vec<tree, va_gc> *v;
11554 vec_alloc (v, 4);
11555 return v;
11559 /* Release a vector of trees back to the cache. */
11561 void
11562 release_tree_vector (vec<tree, va_gc> *vec)
11564 if (vec != NULL)
11566 vec->truncate (0);
11567 vec_safe_push (tree_vector_cache, vec);
11571 /* Get a new tree vector holding a single tree. */
11573 vec<tree, va_gc> *
11574 make_tree_vector_single (tree t)
11576 vec<tree, va_gc> *ret = make_tree_vector ();
11577 ret->quick_push (t);
11578 return ret;
11581 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11583 vec<tree, va_gc> *
11584 make_tree_vector_from_list (tree list)
11586 vec<tree, va_gc> *ret = make_tree_vector ();
11587 for (; list; list = TREE_CHAIN (list))
11588 vec_safe_push (ret, TREE_VALUE (list));
11589 return ret;
11592 /* Get a new tree vector which is a copy of an existing one. */
11594 vec<tree, va_gc> *
11595 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11597 vec<tree, va_gc> *ret;
11598 unsigned int ix;
11599 tree t;
11601 ret = make_tree_vector ();
11602 vec_safe_reserve (ret, vec_safe_length (orig));
11603 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11604 ret->quick_push (t);
11605 return ret;
11608 /* Return true if KEYWORD starts a type specifier. */
11610 bool
11611 keyword_begins_type_specifier (enum rid keyword)
11613 switch (keyword)
11615 case RID_INT:
11616 case RID_CHAR:
11617 case RID_FLOAT:
11618 case RID_DOUBLE:
11619 case RID_VOID:
11620 case RID_INT128:
11621 case RID_UNSIGNED:
11622 case RID_LONG:
11623 case RID_SHORT:
11624 case RID_SIGNED:
11625 case RID_DFLOAT32:
11626 case RID_DFLOAT64:
11627 case RID_DFLOAT128:
11628 case RID_FRACT:
11629 case RID_ACCUM:
11630 case RID_BOOL:
11631 case RID_WCHAR:
11632 case RID_CHAR16:
11633 case RID_CHAR32:
11634 case RID_SAT:
11635 case RID_COMPLEX:
11636 case RID_TYPEOF:
11637 case RID_STRUCT:
11638 case RID_CLASS:
11639 case RID_UNION:
11640 case RID_ENUM:
11641 return true;
11642 default:
11643 return false;
11647 /* Return true if KEYWORD names a type qualifier. */
11649 bool
11650 keyword_is_type_qualifier (enum rid keyword)
11652 switch (keyword)
11654 case RID_CONST:
11655 case RID_VOLATILE:
11656 case RID_RESTRICT:
11657 case RID_ATOMIC:
11658 return true;
11659 default:
11660 return false;
11664 /* Return true if KEYWORD names a storage class specifier.
11666 RID_TYPEDEF is not included in this list despite `typedef' being
11667 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11668 such for syntactic convenience only. */
11670 bool
11671 keyword_is_storage_class_specifier (enum rid keyword)
11673 switch (keyword)
11675 case RID_STATIC:
11676 case RID_EXTERN:
11677 case RID_REGISTER:
11678 case RID_AUTO:
11679 case RID_MUTABLE:
11680 case RID_THREAD:
11681 return true;
11682 default:
11683 return false;
11687 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11689 static bool
11690 keyword_is_function_specifier (enum rid keyword)
11692 switch (keyword)
11694 case RID_INLINE:
11695 case RID_NORETURN:
11696 case RID_VIRTUAL:
11697 case RID_EXPLICIT:
11698 return true;
11699 default:
11700 return false;
11704 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11705 declaration-specifier (C99 6.7). */
11707 bool
11708 keyword_is_decl_specifier (enum rid keyword)
11710 if (keyword_is_storage_class_specifier (keyword)
11711 || keyword_is_type_qualifier (keyword)
11712 || keyword_is_function_specifier (keyword))
11713 return true;
11715 switch (keyword)
11717 case RID_TYPEDEF:
11718 case RID_FRIEND:
11719 case RID_CONSTEXPR:
11720 return true;
11721 default:
11722 return false;
11726 /* Initialize language-specific-bits of tree_contains_struct. */
11728 void
11729 c_common_init_ts (void)
11731 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11732 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11733 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11736 /* Build a user-defined numeric literal out of an integer constant type VALUE
11737 with identifier SUFFIX. */
11739 tree
11740 build_userdef_literal (tree suffix_id, tree value,
11741 enum overflow_type overflow, tree num_string)
11743 tree literal = make_node (USERDEF_LITERAL);
11744 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11745 USERDEF_LITERAL_VALUE (literal) = value;
11746 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11747 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11748 return literal;
11751 /* For vector[index], convert the vector to a
11752 pointer of the underlying type. */
11753 void
11754 convert_vector_to_pointer_for_subscript (location_t loc,
11755 tree* vecp, tree index)
11757 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11759 tree type = TREE_TYPE (*vecp);
11760 tree type1;
11762 if (TREE_CODE (index) == INTEGER_CST)
11763 if (!host_integerp (index, 1)
11764 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11765 >= TYPE_VECTOR_SUBPARTS (type)))
11766 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11768 c_common_mark_addressable_vec (*vecp);
11769 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11770 type = build_pointer_type (type);
11771 type1 = build_pointer_type (TREE_TYPE (*vecp));
11772 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11773 *vecp = convert (type, *vecp);
11777 /* Determine which of the operands, if any, is a scalar that needs to be
11778 converted to a vector, for the range of operations. */
11779 enum stv_conv
11780 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11781 bool complain)
11783 tree type0 = TREE_TYPE (op0);
11784 tree type1 = TREE_TYPE (op1);
11785 bool integer_only_op = false;
11786 enum stv_conv ret = stv_firstarg;
11788 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11789 || TREE_CODE (type1) == VECTOR_TYPE);
11790 switch (code)
11792 /* Most GENERIC binary expressions require homogeneous arguments.
11793 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11794 argument that is a vector and a second one that is a scalar, so
11795 we never return stv_secondarg for them. */
11796 case RSHIFT_EXPR:
11797 case LSHIFT_EXPR:
11798 if (TREE_CODE (type0) == INTEGER_TYPE
11799 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11801 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11803 if (complain)
11804 error_at (loc, "conversion of scalar %qT to vector %qT "
11805 "involves truncation", type0, type1);
11806 return stv_error;
11808 else
11809 return stv_firstarg;
11811 break;
11813 case BIT_IOR_EXPR:
11814 case BIT_XOR_EXPR:
11815 case BIT_AND_EXPR:
11816 integer_only_op = true;
11817 /* ... fall through ... */
11819 case VEC_COND_EXPR:
11821 case PLUS_EXPR:
11822 case MINUS_EXPR:
11823 case MULT_EXPR:
11824 case TRUNC_DIV_EXPR:
11825 case CEIL_DIV_EXPR:
11826 case FLOOR_DIV_EXPR:
11827 case ROUND_DIV_EXPR:
11828 case EXACT_DIV_EXPR:
11829 case TRUNC_MOD_EXPR:
11830 case FLOOR_MOD_EXPR:
11831 case RDIV_EXPR:
11832 case EQ_EXPR:
11833 case NE_EXPR:
11834 case LE_EXPR:
11835 case GE_EXPR:
11836 case LT_EXPR:
11837 case GT_EXPR:
11838 /* What about UNLT_EXPR? */
11839 if (TREE_CODE (type0) == VECTOR_TYPE)
11841 tree tmp;
11842 ret = stv_secondarg;
11843 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11844 tmp = type0; type0 = type1; type1 = tmp;
11845 tmp = op0; op0 = op1; op1 = tmp;
11848 if (TREE_CODE (type0) == INTEGER_TYPE
11849 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11851 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11853 if (complain)
11854 error_at (loc, "conversion of scalar %qT to vector %qT "
11855 "involves truncation", type0, type1);
11856 return stv_error;
11858 return ret;
11860 else if (!integer_only_op
11861 /* Allow integer --> real conversion if safe. */
11862 && (TREE_CODE (type0) == REAL_TYPE
11863 || TREE_CODE (type0) == INTEGER_TYPE)
11864 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11866 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11868 if (complain)
11869 error_at (loc, "conversion of scalar %qT to vector %qT "
11870 "involves truncation", type0, type1);
11871 return stv_error;
11873 return ret;
11875 default:
11876 break;
11879 return stv_nothing;
11882 /* Return true iff ALIGN is an integral constant that is a fundamental
11883 alignment, as defined by [basic.align] in the c++-11
11884 specifications.
11886 That is:
11888 [A fundamental alignment is represented by an alignment less than or
11889 equal to the greatest alignment supported by the implementation
11890 in all contexts, which is equal to
11891 alignof(max_align_t)]. */
11893 bool
11894 cxx_fundamental_alignment_p (unsigned align)
11896 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11897 TYPE_ALIGN (long_double_type_node)));
11900 #include "gt-c-family-c-common.h"