Merge trunk version 204345 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob4e2654ab6d04813338d44f3b47d6b6801e020206
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 { "_Bool", RID_BOOL, D_CONLY },
428 { "_Complex", RID_COMPLEX, 0 },
429 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
430 { "_Cilk_sync", RID_CILK_SYNC, 0 },
431 { "_Imaginary", RID_IMAGINARY, D_CONLY },
432 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
433 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
434 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
435 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
436 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
437 { "_Sat", RID_SAT, D_CONLY | D_EXT },
438 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
439 { "_Noreturn", RID_NORETURN, D_CONLY },
440 { "_Generic", RID_GENERIC, D_CONLY },
441 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
442 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
443 { "__alignof", RID_ALIGNOF, 0 },
444 { "__alignof__", RID_ALIGNOF, 0 },
445 { "__asm", RID_ASM, 0 },
446 { "__asm__", RID_ASM, 0 },
447 { "__attribute", RID_ATTRIBUTE, 0 },
448 { "__attribute__", RID_ATTRIBUTE, 0 },
449 { "__bases", RID_BASES, D_CXXONLY },
450 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
451 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
452 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
453 { "__builtin_offsetof", RID_OFFSETOF, 0 },
454 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
455 { "__builtin_va_arg", RID_VA_ARG, 0 },
456 { "__complex", RID_COMPLEX, 0 },
457 { "__complex__", RID_COMPLEX, 0 },
458 { "__const", RID_CONST, 0 },
459 { "__const__", RID_CONST, 0 },
460 { "__decltype", RID_DECLTYPE, D_CXXONLY },
461 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
462 { "__extension__", RID_EXTENSION, 0 },
463 { "__func__", RID_C99_FUNCTION_NAME, 0 },
464 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
465 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
466 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
467 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
468 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
469 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
470 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
471 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
472 { "__imag", RID_IMAGPART, 0 },
473 { "__imag__", RID_IMAGPART, 0 },
474 { "__inline", RID_INLINE, 0 },
475 { "__inline__", RID_INLINE, 0 },
476 { "__int128", RID_INT128, 0 },
477 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
478 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
479 { "__is_class", RID_IS_CLASS, D_CXXONLY },
480 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
481 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
482 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
483 { "__is_final", RID_IS_FINAL, D_CXXONLY },
484 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
485 { "__is_pod", RID_IS_POD, D_CXXONLY },
486 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
487 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
488 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
489 { "__is_union", RID_IS_UNION, D_CXXONLY },
490 { "__label__", RID_LABEL, 0 },
491 { "__null", RID_NULL, 0 },
492 { "__real", RID_REALPART, 0 },
493 { "__real__", RID_REALPART, 0 },
494 { "__restrict", RID_RESTRICT, 0 },
495 { "__restrict__", RID_RESTRICT, 0 },
496 { "__signed", RID_SIGNED, 0 },
497 { "__signed__", RID_SIGNED, 0 },
498 { "__thread", RID_THREAD, 0 },
499 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
500 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
501 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
502 { "__typeof", RID_TYPEOF, 0 },
503 { "__typeof__", RID_TYPEOF, 0 },
504 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
505 { "__volatile", RID_VOLATILE, 0 },
506 { "__volatile__", RID_VOLATILE, 0 },
507 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "asm", RID_ASM, D_ASM },
510 { "auto", RID_AUTO, 0 },
511 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
512 { "break", RID_BREAK, 0 },
513 { "case", RID_CASE, 0 },
514 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
515 { "char", RID_CHAR, 0 },
516 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
517 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
518 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
519 { "const", RID_CONST, 0 },
520 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
521 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
522 { "continue", RID_CONTINUE, 0 },
523 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
524 { "default", RID_DEFAULT, 0 },
525 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
526 { "do", RID_DO, 0 },
527 { "double", RID_DOUBLE, 0 },
528 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
529 { "else", RID_ELSE, 0 },
530 { "enum", RID_ENUM, 0 },
531 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
532 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
533 { "extern", RID_EXTERN, 0 },
534 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
535 { "float", RID_FLOAT, 0 },
536 { "for", RID_FOR, 0 },
537 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
538 { "goto", RID_GOTO, 0 },
539 { "if", RID_IF, 0 },
540 { "inline", RID_INLINE, D_EXT89 },
541 { "int", RID_INT, 0 },
542 { "long", RID_LONG, 0 },
543 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
544 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
545 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
546 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
547 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
548 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
549 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
550 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
551 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
552 { "register", RID_REGISTER, 0 },
553 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
554 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
555 { "return", RID_RETURN, 0 },
556 { "short", RID_SHORT, 0 },
557 { "signed", RID_SIGNED, 0 },
558 { "sizeof", RID_SIZEOF, 0 },
559 { "static", RID_STATIC, 0 },
560 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
561 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
562 { "struct", RID_STRUCT, 0 },
563 { "switch", RID_SWITCH, 0 },
564 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
565 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
566 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
568 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
569 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
570 { "typedef", RID_TYPEDEF, 0 },
571 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
572 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
573 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
574 { "union", RID_UNION, 0 },
575 { "unsigned", RID_UNSIGNED, 0 },
576 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
577 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
578 { "void", RID_VOID, 0 },
579 { "volatile", RID_VOLATILE, 0 },
580 { "wchar_t", RID_WCHAR, D_CXXONLY },
581 { "while", RID_WHILE, 0 },
582 /* These Objective-C keywords are recognized only immediately after
583 an '@'. */
584 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
585 { "defs", RID_AT_DEFS, D_OBJC },
586 { "encode", RID_AT_ENCODE, D_OBJC },
587 { "end", RID_AT_END, D_OBJC },
588 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
589 { "interface", RID_AT_INTERFACE, D_OBJC },
590 { "protocol", RID_AT_PROTOCOL, D_OBJC },
591 { "selector", RID_AT_SELECTOR, D_OBJC },
592 { "finally", RID_AT_FINALLY, D_OBJC },
593 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
594 { "optional", RID_AT_OPTIONAL, D_OBJC },
595 { "required", RID_AT_REQUIRED, D_OBJC },
596 { "property", RID_AT_PROPERTY, D_OBJC },
597 { "package", RID_AT_PACKAGE, D_OBJC },
598 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
599 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
600 /* These are recognized only in protocol-qualifier context
601 (see above) */
602 { "bycopy", RID_BYCOPY, D_OBJC },
603 { "byref", RID_BYREF, D_OBJC },
604 { "in", RID_IN, D_OBJC },
605 { "inout", RID_INOUT, D_OBJC },
606 { "oneway", RID_ONEWAY, D_OBJC },
607 { "out", RID_OUT, D_OBJC },
609 /* UPC keywords */
610 { "shared", RID_SHARED, D_UPC },
611 { "relaxed", RID_RELAXED, D_UPC },
612 { "strict", RID_STRICT, D_UPC },
613 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
614 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
615 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
616 { "upc_forall", RID_UPC_FORALL, D_UPC },
617 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
618 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
619 { "upc_wait", RID_UPC_WAIT, D_UPC },
621 /* These are recognized inside a property attribute list */
622 { "assign", RID_ASSIGN, D_OBJC },
623 { "copy", RID_COPY, D_OBJC },
624 { "getter", RID_GETTER, D_OBJC },
625 { "nonatomic", RID_NONATOMIC, D_OBJC },
626 { "readonly", RID_READONLY, D_OBJC },
627 { "readwrite", RID_READWRITE, D_OBJC },
628 { "retain", RID_RETAIN, D_OBJC },
629 { "setter", RID_SETTER, D_OBJC },
632 const unsigned int num_c_common_reswords =
633 sizeof c_common_reswords / sizeof (struct c_common_resword);
635 /* Table of machine-independent attributes common to all C-like languages. */
636 const struct attribute_spec c_common_attribute_table[] =
638 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
639 affects_type_identity } */
640 { "packed", 0, 0, false, false, false,
641 handle_packed_attribute , false},
642 { "nocommon", 0, 0, true, false, false,
643 handle_nocommon_attribute, false},
644 { "common", 0, 0, true, false, false,
645 handle_common_attribute, false },
646 /* FIXME: logically, noreturn attributes should be listed as
647 "false, true, true" and apply to function types. But implementing this
648 would require all the places in the compiler that use TREE_THIS_VOLATILE
649 on a decl to identify non-returning functions to be located and fixed
650 to check the function type instead. */
651 { "noreturn", 0, 0, true, false, false,
652 handle_noreturn_attribute, false },
653 { "volatile", 0, 0, true, false, false,
654 handle_noreturn_attribute, false },
655 { "noinline", 0, 0, true, false, false,
656 handle_noinline_attribute, false },
657 { "noclone", 0, 0, true, false, false,
658 handle_noclone_attribute, false },
659 { "leaf", 0, 0, true, false, false,
660 handle_leaf_attribute, false },
661 { "always_inline", 0, 0, true, false, false,
662 handle_always_inline_attribute, false },
663 { "gnu_inline", 0, 0, true, false, false,
664 handle_gnu_inline_attribute, false },
665 { "artificial", 0, 0, true, false, false,
666 handle_artificial_attribute, false },
667 { "flatten", 0, 0, true, false, false,
668 handle_flatten_attribute, false },
669 { "used", 0, 0, true, false, false,
670 handle_used_attribute, false },
671 { "unused", 0, 0, false, false, false,
672 handle_unused_attribute, false },
673 { "externally_visible", 0, 0, true, false, false,
674 handle_externally_visible_attribute, false },
675 /* The same comments as for noreturn attributes apply to const ones. */
676 { "const", 0, 0, true, false, false,
677 handle_const_attribute, false },
678 { "transparent_union", 0, 0, false, false, false,
679 handle_transparent_union_attribute, false },
680 { "constructor", 0, 1, true, false, false,
681 handle_constructor_attribute, false },
682 { "destructor", 0, 1, true, false, false,
683 handle_destructor_attribute, false },
684 { "mode", 1, 1, false, true, false,
685 handle_mode_attribute, false },
686 { "section", 1, 1, true, false, false,
687 handle_section_attribute, false },
688 { "aligned", 0, 1, false, false, false,
689 handle_aligned_attribute, false },
690 { "weak", 0, 0, true, false, false,
691 handle_weak_attribute, false },
692 { "ifunc", 1, 1, true, false, false,
693 handle_ifunc_attribute, false },
694 { "alias", 1, 1, true, false, false,
695 handle_alias_attribute, false },
696 { "weakref", 0, 1, true, false, false,
697 handle_weakref_attribute, false },
698 { "no_instrument_function", 0, 0, true, false, false,
699 handle_no_instrument_function_attribute,
700 false },
701 { "malloc", 0, 0, true, false, false,
702 handle_malloc_attribute, false },
703 { "returns_twice", 0, 0, true, false, false,
704 handle_returns_twice_attribute, false },
705 { "no_stack_limit", 0, 0, true, false, false,
706 handle_no_limit_stack_attribute, false },
707 { "pure", 0, 0, true, false, false,
708 handle_pure_attribute, false },
709 { "transaction_callable", 0, 0, false, true, false,
710 handle_tm_attribute, false },
711 { "transaction_unsafe", 0, 0, false, true, false,
712 handle_tm_attribute, false },
713 { "transaction_safe", 0, 0, false, true, false,
714 handle_tm_attribute, false },
715 { "transaction_may_cancel_outer", 0, 0, false, true, false,
716 handle_tm_attribute, false },
717 /* ??? These two attributes didn't make the transition from the
718 Intel language document to the multi-vendor language document. */
719 { "transaction_pure", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 { "transaction_wrap", 1, 1, true, false, false,
722 handle_tm_wrap_attribute, false },
723 /* For internal use (marking of builtins) only. The name contains space
724 to prevent its usage in source code. */
725 { "no vops", 0, 0, true, false, false,
726 handle_novops_attribute, false },
727 { "deprecated", 0, 1, false, false, false,
728 handle_deprecated_attribute, false },
729 { "vector_size", 1, 1, false, true, false,
730 handle_vector_size_attribute, false },
731 { "visibility", 1, 1, false, false, false,
732 handle_visibility_attribute, false },
733 { "tls_model", 1, 1, true, false, false,
734 handle_tls_model_attribute, false },
735 { "nonnull", 0, -1, false, true, true,
736 handle_nonnull_attribute, false },
737 { "nothrow", 0, 0, true, false, false,
738 handle_nothrow_attribute, false },
739 { "may_alias", 0, 0, false, true, false, NULL, false },
740 { "cleanup", 1, 1, true, false, false,
741 handle_cleanup_attribute, false },
742 { "warn_unused_result", 0, 0, false, true, true,
743 handle_warn_unused_result_attribute, false },
744 { "sentinel", 0, 1, false, true, true,
745 handle_sentinel_attribute, false },
746 /* For internal use (marking of builtins) only. The name contains space
747 to prevent its usage in source code. */
748 { "type generic", 0, 0, false, true, true,
749 handle_type_generic_attribute, false },
750 { "alloc_size", 1, 2, false, true, true,
751 handle_alloc_size_attribute, false },
752 { "cold", 0, 0, true, false, false,
753 handle_cold_attribute, false },
754 { "hot", 0, 0, true, false, false,
755 handle_hot_attribute, false },
756 { "no_address_safety_analysis",
757 0, 0, true, false, false,
758 handle_no_address_safety_analysis_attribute,
759 false },
760 { "no_sanitize_address", 0, 0, true, false, false,
761 handle_no_sanitize_address_attribute,
762 false },
763 { "no_sanitize_undefined", 0, 0, true, false, false,
764 handle_no_sanitize_undefined_attribute,
765 false },
766 { "warning", 1, 1, true, false, false,
767 handle_error_attribute, false },
768 { "error", 1, 1, true, false, false,
769 handle_error_attribute, false },
770 { "target", 1, -1, true, false, false,
771 handle_target_attribute, false },
772 { "optimize", 1, -1, true, false, false,
773 handle_optimize_attribute, false },
774 /* For internal use only. The leading '*' both prevents its usage in
775 source code and signals that it may be overridden by machine tables. */
776 { "*tm regparm", 0, 0, false, true, true,
777 ignore_attribute, false },
778 { "no_split_stack", 0, 0, true, false, false,
779 handle_no_split_stack_attribute, false },
780 /* For internal use (marking of builtins and runtime functions) only.
781 The name contains space to prevent its usage in source code. */
782 { "fn spec", 1, 1, false, true, true,
783 handle_fnspec_attribute, false },
784 { "warn_unused", 0, 0, false, false, false,
785 handle_warn_unused_attribute, false },
786 { "returns_nonnull", 0, 0, false, true, true,
787 handle_returns_nonnull_attribute, false },
788 { "omp declare simd", 0, -1, true, false, false,
789 handle_omp_declare_simd_attribute, false },
790 { "omp declare target", 0, 0, true, false, false,
791 handle_omp_declare_target_attribute, false },
792 { "bnd_variable_size", 0, 0, true, false, false,
793 handle_bnd_variable_size_attribute, false },
794 { "bnd_legacy", 0, 0, true, false, false,
795 handle_bnd_legacy, false },
796 { NULL, 0, 0, false, false, false, NULL, false }
799 /* Give the specifications for the format attributes, used by C and all
800 descendants. */
802 const struct attribute_spec c_common_format_attribute_table[] =
804 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
805 affects_type_identity } */
806 { "format", 3, 3, false, true, true,
807 handle_format_attribute, false },
808 { "format_arg", 1, 1, false, true, true,
809 handle_format_arg_attribute, false },
810 { NULL, 0, 0, false, false, false, NULL, false }
813 /* Return identifier for address space AS. */
815 const char *
816 c_addr_space_name (addr_space_t as)
818 int rid = RID_FIRST_ADDR_SPACE + as;
819 gcc_assert (ridpointers [rid]);
820 return IDENTIFIER_POINTER (ridpointers [rid]);
823 /* Push current bindings for the function name VAR_DECLS. */
825 void
826 start_fname_decls (void)
828 unsigned ix;
829 tree saved = NULL_TREE;
831 for (ix = 0; fname_vars[ix].decl; ix++)
833 tree decl = *fname_vars[ix].decl;
835 if (decl)
837 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
838 saved);
839 *fname_vars[ix].decl = NULL_TREE;
842 if (saved || saved_function_name_decls)
843 /* Normally they'll have been NULL, so only push if we've got a
844 stack, or they are non-NULL. */
845 saved_function_name_decls = tree_cons (saved, NULL_TREE,
846 saved_function_name_decls);
849 /* Finish up the current bindings, adding them into the current function's
850 statement tree. This must be done _before_ finish_stmt_tree is called.
851 If there is no current function, we must be at file scope and no statements
852 are involved. Pop the previous bindings. */
854 void
855 finish_fname_decls (void)
857 unsigned ix;
858 tree stmts = NULL_TREE;
859 tree stack = saved_function_name_decls;
861 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
862 append_to_statement_list (TREE_VALUE (stack), &stmts);
864 if (stmts)
866 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
868 if (TREE_CODE (*bodyp) == BIND_EXPR)
869 bodyp = &BIND_EXPR_BODY (*bodyp);
871 append_to_statement_list_force (*bodyp, &stmts);
872 *bodyp = stmts;
875 for (ix = 0; fname_vars[ix].decl; ix++)
876 *fname_vars[ix].decl = NULL_TREE;
878 if (stack)
880 /* We had saved values, restore them. */
881 tree saved;
883 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
885 tree decl = TREE_PURPOSE (saved);
886 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
888 *fname_vars[ix].decl = decl;
890 stack = TREE_CHAIN (stack);
892 saved_function_name_decls = stack;
895 /* Return the text name of the current function, suitably prettified
896 by PRETTY_P. Return string must be freed by caller. */
898 const char *
899 fname_as_string (int pretty_p)
901 const char *name = "top level";
902 char *namep;
903 int vrb = 2, len;
904 cpp_string cstr = { 0, 0 }, strname;
906 if (!pretty_p)
908 name = "";
909 vrb = 0;
912 if (current_function_decl)
913 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
915 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
917 namep = XNEWVEC (char, len);
918 snprintf (namep, len, "\"%s\"", name);
919 strname.text = (unsigned char *) namep;
920 strname.len = len - 1;
922 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
924 XDELETEVEC (namep);
925 return (const char *) cstr.text;
928 return namep;
931 /* Return the VAR_DECL for a const char array naming the current
932 function. If the VAR_DECL has not yet been created, create it
933 now. RID indicates how it should be formatted and IDENTIFIER_NODE
934 ID is its name (unfortunately C and C++ hold the RID values of
935 keywords in different places, so we can't derive RID from ID in
936 this language independent code. LOC is the location of the
937 function. */
939 tree
940 fname_decl (location_t loc, unsigned int rid, tree id)
942 unsigned ix;
943 tree decl = NULL_TREE;
945 for (ix = 0; fname_vars[ix].decl; ix++)
946 if (fname_vars[ix].rid == rid)
947 break;
949 decl = *fname_vars[ix].decl;
950 if (!decl)
952 /* If a tree is built here, it would normally have the lineno of
953 the current statement. Later this tree will be moved to the
954 beginning of the function and this line number will be wrong.
955 To avoid this problem set the lineno to 0 here; that prevents
956 it from appearing in the RTL. */
957 tree stmts;
958 location_t saved_location = input_location;
959 input_location = UNKNOWN_LOCATION;
961 stmts = push_stmt_list ();
962 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
963 stmts = pop_stmt_list (stmts);
964 if (!IS_EMPTY_STMT (stmts))
965 saved_function_name_decls
966 = tree_cons (decl, stmts, saved_function_name_decls);
967 *fname_vars[ix].decl = decl;
968 input_location = saved_location;
970 if (!ix && !current_function_decl)
971 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
973 return decl;
976 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
978 tree
979 fix_string_type (tree value)
981 int length = TREE_STRING_LENGTH (value);
982 int nchars;
983 tree e_type, i_type, a_type;
985 /* Compute the number of elements, for the array type. */
986 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
988 nchars = length;
989 e_type = char_type_node;
991 else if (TREE_TYPE (value) == char16_array_type_node)
993 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
994 e_type = char16_type_node;
996 else if (TREE_TYPE (value) == char32_array_type_node)
998 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
999 e_type = char32_type_node;
1001 else
1003 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1004 e_type = wchar_type_node;
1007 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1008 limit in C++98 Annex B is very large (65536) and is not normative,
1009 so we do not diagnose it (warn_overlength_strings is forced off
1010 in c_common_post_options). */
1011 if (warn_overlength_strings)
1013 const int nchars_max = flag_isoc99 ? 4095 : 509;
1014 const int relevant_std = flag_isoc99 ? 99 : 90;
1015 if (nchars - 1 > nchars_max)
1016 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1017 separate the %d from the 'C'. 'ISO' should not be
1018 translated, but it may be moved after 'C%d' in languages
1019 where modifiers follow nouns. */
1020 pedwarn (input_location, OPT_Woverlength_strings,
1021 "string length %qd is greater than the length %qd "
1022 "ISO C%d compilers are required to support",
1023 nchars - 1, nchars_max, relevant_std);
1026 /* Create the array type for the string constant. The ISO C++
1027 standard says that a string literal has type `const char[N]' or
1028 `const wchar_t[N]'. We use the same logic when invoked as a C
1029 front-end with -Wwrite-strings.
1030 ??? We should change the type of an expression depending on the
1031 state of a warning flag. We should just be warning -- see how
1032 this is handled in the C++ front-end for the deprecated implicit
1033 conversion from string literals to `char*' or `wchar_t*'.
1035 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1036 array type being the unqualified version of that type.
1037 Therefore, if we are constructing an array of const char, we must
1038 construct the matching unqualified array type first. The C front
1039 end does not require this, but it does no harm, so we do it
1040 unconditionally. */
1041 i_type = build_index_type (size_int (nchars - 1));
1042 a_type = build_array_type (e_type, i_type);
1043 if (c_dialect_cxx() || warn_write_strings)
1044 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1046 TREE_TYPE (value) = a_type;
1047 TREE_CONSTANT (value) = 1;
1048 TREE_READONLY (value) = 1;
1049 TREE_STATIC (value) = 1;
1050 return value;
1053 /* If DISABLE is true, stop issuing warnings. This is used when
1054 parsing code that we know will not be executed. This function may
1055 be called multiple times, and works as a stack. */
1057 static void
1058 c_disable_warnings (bool disable)
1060 if (disable)
1062 ++c_inhibit_evaluation_warnings;
1063 fold_defer_overflow_warnings ();
1067 /* If ENABLE is true, reenable issuing warnings. */
1069 static void
1070 c_enable_warnings (bool enable)
1072 if (enable)
1074 --c_inhibit_evaluation_warnings;
1075 fold_undefer_and_ignore_overflow_warnings ();
1079 /* Fully fold EXPR, an expression that was not folded (beyond integer
1080 constant expressions and null pointer constants) when being built
1081 up. If IN_INIT, this is in a static initializer and certain
1082 changes are made to the folding done. Clear *MAYBE_CONST if
1083 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1084 expression because it contains an evaluated operator (in C99) or an
1085 operator outside of sizeof returning an integer constant (in C90)
1086 not permitted in constant expressions, or because it contains an
1087 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1088 set to true by callers before calling this function.) Return the
1089 folded expression. Function arguments have already been folded
1090 before calling this function, as have the contents of SAVE_EXPR,
1091 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1092 C_MAYBE_CONST_EXPR. */
1094 tree
1095 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1097 tree ret;
1098 tree eptype = NULL_TREE;
1099 bool dummy = true;
1100 bool maybe_const_itself = true;
1101 location_t loc = EXPR_LOCATION (expr);
1103 /* This function is not relevant to C++ because C++ folds while
1104 parsing, and may need changes to be correct for C++ when C++
1105 stops folding while parsing. */
1106 if (c_dialect_cxx ())
1107 gcc_unreachable ();
1109 if (!maybe_const)
1110 maybe_const = &dummy;
1111 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1113 eptype = TREE_TYPE (expr);
1114 expr = TREE_OPERAND (expr, 0);
1116 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1117 &maybe_const_itself);
1118 if (eptype)
1119 ret = fold_convert_loc (loc, eptype, ret);
1120 *maybe_const &= maybe_const_itself;
1121 return ret;
1124 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1125 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1126 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1127 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1128 both evaluated and unevaluated subexpressions while
1129 *MAYBE_CONST_ITSELF is carried from only evaluated
1130 subexpressions). */
1132 static tree
1133 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1134 bool *maybe_const_itself)
1136 tree ret = expr;
1137 enum tree_code code = TREE_CODE (expr);
1138 enum tree_code_class kind = TREE_CODE_CLASS (code);
1139 location_t loc = EXPR_LOCATION (expr);
1140 tree op0, op1, op2, op3;
1141 tree orig_op0, orig_op1, orig_op2;
1142 bool op0_const = true, op1_const = true, op2_const = true;
1143 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1144 bool nowarning = TREE_NO_WARNING (expr);
1145 bool unused_p;
1147 /* This function is not relevant to C++ because C++ folds while
1148 parsing, and may need changes to be correct for C++ when C++
1149 stops folding while parsing. */
1150 if (c_dialect_cxx ())
1151 gcc_unreachable ();
1153 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1154 anything else not counted as an expression cannot usefully be
1155 folded further at this point. */
1156 if (!IS_EXPR_CODE_CLASS (kind)
1157 || kind == tcc_statement
1158 || code == SAVE_EXPR)
1159 return expr;
1161 /* Operands of variable-length expressions (function calls) have
1162 already been folded, as have __builtin_* function calls, and such
1163 expressions cannot occur in constant expressions. */
1164 if (kind == tcc_vl_exp)
1166 *maybe_const_operands = false;
1167 ret = fold (expr);
1168 goto out;
1171 if (code == C_MAYBE_CONST_EXPR)
1173 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1174 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1175 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1176 *maybe_const_operands = false;
1177 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1178 *maybe_const_itself = false;
1179 if (pre && !in_init)
1180 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1181 else
1182 ret = inner;
1183 goto out;
1186 /* Assignment, increment, decrement, function call and comma
1187 operators, and statement expressions, cannot occur in constant
1188 expressions if evaluated / outside of sizeof. (Function calls
1189 were handled above, though VA_ARG_EXPR is treated like a function
1190 call here, and statement expressions are handled through
1191 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1192 switch (code)
1194 case MODIFY_EXPR:
1195 case PREDECREMENT_EXPR:
1196 case PREINCREMENT_EXPR:
1197 case POSTDECREMENT_EXPR:
1198 case POSTINCREMENT_EXPR:
1199 case COMPOUND_EXPR:
1200 *maybe_const_operands = false;
1201 break;
1203 case VA_ARG_EXPR:
1204 case TARGET_EXPR:
1205 case BIND_EXPR:
1206 case OBJ_TYPE_REF:
1207 *maybe_const_operands = false;
1208 ret = fold (expr);
1209 goto out;
1211 default:
1212 break;
1215 /* Fold individual tree codes as appropriate. */
1216 switch (code)
1218 case COMPOUND_LITERAL_EXPR:
1219 /* Any non-constancy will have been marked in a containing
1220 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1221 goto out;
1223 case COMPONENT_REF:
1224 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1225 op1 = TREE_OPERAND (expr, 1);
1226 op2 = TREE_OPERAND (expr, 2);
1227 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1228 maybe_const_itself);
1229 STRIP_TYPE_NOPS (op0);
1230 if (op0 != orig_op0)
1231 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1232 if (ret != expr)
1234 TREE_READONLY (ret) = TREE_READONLY (expr);
1235 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1237 goto out;
1239 case ARRAY_REF:
1240 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1241 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1242 op2 = TREE_OPERAND (expr, 2);
1243 op3 = TREE_OPERAND (expr, 3);
1244 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1245 maybe_const_itself);
1246 STRIP_TYPE_NOPS (op0);
1247 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1248 maybe_const_itself);
1249 STRIP_TYPE_NOPS (op1);
1250 op1 = decl_constant_value_for_optimization (op1);
1251 if (op0 != orig_op0 || op1 != orig_op1)
1252 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1253 if (ret != expr)
1255 TREE_READONLY (ret) = TREE_READONLY (expr);
1256 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1257 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1259 ret = fold (ret);
1260 goto out;
1262 case COMPOUND_EXPR:
1263 case MODIFY_EXPR:
1264 case PREDECREMENT_EXPR:
1265 case PREINCREMENT_EXPR:
1266 case POSTDECREMENT_EXPR:
1267 case POSTINCREMENT_EXPR:
1268 case PLUS_EXPR:
1269 case MINUS_EXPR:
1270 case MULT_EXPR:
1271 case POINTER_PLUS_EXPR:
1272 case TRUNC_DIV_EXPR:
1273 case CEIL_DIV_EXPR:
1274 case FLOOR_DIV_EXPR:
1275 case TRUNC_MOD_EXPR:
1276 case RDIV_EXPR:
1277 case EXACT_DIV_EXPR:
1278 case LSHIFT_EXPR:
1279 case RSHIFT_EXPR:
1280 case BIT_IOR_EXPR:
1281 case BIT_XOR_EXPR:
1282 case BIT_AND_EXPR:
1283 case LT_EXPR:
1284 case LE_EXPR:
1285 case GT_EXPR:
1286 case GE_EXPR:
1287 case EQ_EXPR:
1288 case NE_EXPR:
1289 case COMPLEX_EXPR:
1290 case TRUTH_AND_EXPR:
1291 case TRUTH_OR_EXPR:
1292 case TRUTH_XOR_EXPR:
1293 case UNORDERED_EXPR:
1294 case ORDERED_EXPR:
1295 case UNLT_EXPR:
1296 case UNLE_EXPR:
1297 case UNGT_EXPR:
1298 case UNGE_EXPR:
1299 case UNEQ_EXPR:
1300 /* Binary operations evaluating both arguments (increment and
1301 decrement are binary internally in GCC). */
1302 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1303 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1304 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1305 maybe_const_itself);
1306 STRIP_TYPE_NOPS (op0);
1307 if (code != MODIFY_EXPR
1308 && code != PREDECREMENT_EXPR
1309 && code != PREINCREMENT_EXPR
1310 && code != POSTDECREMENT_EXPR
1311 && code != POSTINCREMENT_EXPR)
1312 op0 = decl_constant_value_for_optimization (op0);
1313 /* The RHS of a MODIFY_EXPR was fully folded when building that
1314 expression for the sake of conversion warnings. */
1315 if (code != MODIFY_EXPR)
1316 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1317 maybe_const_itself);
1318 STRIP_TYPE_NOPS (op1);
1319 op1 = decl_constant_value_for_optimization (op1);
1320 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1321 ret = in_init
1322 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1323 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1324 else
1325 ret = fold (expr);
1326 if (TREE_OVERFLOW_P (ret)
1327 && !TREE_OVERFLOW_P (op0)
1328 && !TREE_OVERFLOW_P (op1))
1329 overflow_warning (EXPR_LOCATION (expr), ret);
1330 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1331 && TREE_CODE (orig_op1) != INTEGER_CST
1332 && TREE_CODE (op1) == INTEGER_CST
1333 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1334 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1335 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1336 && c_inhibit_evaluation_warnings == 0)
1338 if (tree_int_cst_sgn (op1) < 0)
1339 warning_at (loc, 0, (code == LSHIFT_EXPR
1340 ? G_("left shift count is negative")
1341 : G_("right shift count is negative")));
1342 else if (compare_tree_int (op1,
1343 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1344 >= 0)
1345 warning_at (loc, 0, (code == LSHIFT_EXPR
1346 ? G_("left shift count >= width of type")
1347 : G_("right shift count >= width of type")));
1349 goto out;
1351 case INDIRECT_REF:
1352 case FIX_TRUNC_EXPR:
1353 case FLOAT_EXPR:
1354 CASE_CONVERT:
1355 case VIEW_CONVERT_EXPR:
1356 case NON_LVALUE_EXPR:
1357 case NEGATE_EXPR:
1358 case BIT_NOT_EXPR:
1359 case TRUTH_NOT_EXPR:
1360 case ADDR_EXPR:
1361 case CONJ_EXPR:
1362 case REALPART_EXPR:
1363 case IMAGPART_EXPR:
1364 /* Unary operations. */
1365 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1366 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1367 maybe_const_itself);
1368 STRIP_TYPE_NOPS (op0);
1369 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1370 op0 = decl_constant_value_for_optimization (op0);
1371 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1372 not prepared to deal with them if they occur in initializers.
1373 Avoid attempts to fold references to UPC shared components
1374 due to the complexities of UPC pointer-to-shared arithmetic. */
1375 if (op0 != orig_op0
1376 && code == ADDR_EXPR
1377 && (op1 = get_base_address (op0)) != NULL_TREE
1378 && TREE_CODE (op1) == INDIRECT_REF
1379 && !upc_shared_type_p (TREE_TYPE (op1))
1380 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1381 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1382 else if (op0 != orig_op0 || in_init)
1383 ret = in_init
1384 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1385 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1386 else
1387 ret = fold (expr);
1388 if (code == INDIRECT_REF
1389 && ret != expr
1390 && TREE_CODE (ret) == INDIRECT_REF)
1392 TREE_READONLY (ret) = TREE_READONLY (expr);
1393 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1394 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1396 switch (code)
1398 case FIX_TRUNC_EXPR:
1399 case FLOAT_EXPR:
1400 CASE_CONVERT:
1401 /* Don't warn about explicit conversions. We will already
1402 have warned about suspect implicit conversions. */
1403 break;
1405 default:
1406 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1407 overflow_warning (EXPR_LOCATION (expr), ret);
1408 break;
1410 goto out;
1412 case TRUTH_ANDIF_EXPR:
1413 case TRUTH_ORIF_EXPR:
1414 /* Binary operations not necessarily evaluating both
1415 arguments. */
1416 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1417 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1418 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1419 STRIP_TYPE_NOPS (op0);
1421 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1422 ? truthvalue_false_node
1423 : truthvalue_true_node));
1424 c_disable_warnings (unused_p);
1425 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1426 STRIP_TYPE_NOPS (op1);
1427 c_enable_warnings (unused_p);
1429 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1430 ret = in_init
1431 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1432 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1433 else
1434 ret = fold (expr);
1435 *maybe_const_operands &= op0_const;
1436 *maybe_const_itself &= op0_const_self;
1437 if (!(flag_isoc99
1438 && op0_const
1439 && op0_const_self
1440 && (code == TRUTH_ANDIF_EXPR
1441 ? op0 == truthvalue_false_node
1442 : op0 == truthvalue_true_node)))
1443 *maybe_const_operands &= op1_const;
1444 if (!(op0_const
1445 && op0_const_self
1446 && (code == TRUTH_ANDIF_EXPR
1447 ? op0 == truthvalue_false_node
1448 : op0 == truthvalue_true_node)))
1449 *maybe_const_itself &= op1_const_self;
1450 goto out;
1452 case COND_EXPR:
1453 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1454 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1455 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1456 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1458 STRIP_TYPE_NOPS (op0);
1459 c_disable_warnings (op0 == truthvalue_false_node);
1460 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1461 STRIP_TYPE_NOPS (op1);
1462 c_enable_warnings (op0 == truthvalue_false_node);
1464 c_disable_warnings (op0 == truthvalue_true_node);
1465 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1466 STRIP_TYPE_NOPS (op2);
1467 c_enable_warnings (op0 == truthvalue_true_node);
1469 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1470 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1471 else
1472 ret = fold (expr);
1473 *maybe_const_operands &= op0_const;
1474 *maybe_const_itself &= op0_const_self;
1475 if (!(flag_isoc99
1476 && op0_const
1477 && op0_const_self
1478 && op0 == truthvalue_false_node))
1479 *maybe_const_operands &= op1_const;
1480 if (!(op0_const
1481 && op0_const_self
1482 && op0 == truthvalue_false_node))
1483 *maybe_const_itself &= op1_const_self;
1484 if (!(flag_isoc99
1485 && op0_const
1486 && op0_const_self
1487 && op0 == truthvalue_true_node))
1488 *maybe_const_operands &= op2_const;
1489 if (!(op0_const
1490 && op0_const_self
1491 && op0 == truthvalue_true_node))
1492 *maybe_const_itself &= op2_const_self;
1493 goto out;
1495 case EXCESS_PRECISION_EXPR:
1496 /* Each case where an operand with excess precision may be
1497 encountered must remove the EXCESS_PRECISION_EXPR around
1498 inner operands and possibly put one around the whole
1499 expression or possibly convert to the semantic type (which
1500 c_fully_fold does); we cannot tell at this stage which is
1501 appropriate in any particular case. */
1502 gcc_unreachable ();
1504 default:
1505 /* Various codes may appear through folding built-in functions
1506 and their arguments. */
1507 goto out;
1510 out:
1511 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1512 have been done by this point, so remove them again. */
1513 nowarning |= TREE_NO_WARNING (ret);
1514 STRIP_TYPE_NOPS (ret);
1515 if (nowarning && !TREE_NO_WARNING (ret))
1517 if (!CAN_HAVE_LOCATION_P (ret))
1518 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1519 TREE_NO_WARNING (ret) = 1;
1521 if (ret != expr)
1522 protected_set_expr_location (ret, loc);
1523 return ret;
1526 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1527 return EXP. Otherwise, return either EXP or its known constant
1528 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1529 Is the BLKmode test appropriate? */
1531 tree
1532 decl_constant_value_for_optimization (tree exp)
1534 tree ret;
1536 /* This function is only used by C, for c_fully_fold and other
1537 optimization, and may not be correct for C++. */
1538 if (c_dialect_cxx ())
1539 gcc_unreachable ();
1541 if (!optimize
1542 || TREE_CODE (exp) != VAR_DECL
1543 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1544 || DECL_MODE (exp) == BLKmode)
1545 return exp;
1547 ret = decl_constant_value (exp);
1548 /* Avoid unwanted tree sharing between the initializer and current
1549 function's body where the tree can be modified e.g. by the
1550 gimplifier. */
1551 if (ret != exp && TREE_STATIC (exp))
1552 ret = unshare_expr (ret);
1553 return ret;
1556 /* Print a warning if a constant expression had overflow in folding.
1557 Invoke this function on every expression that the language
1558 requires to be a constant expression.
1559 Note the ANSI C standard says it is erroneous for a
1560 constant expression to overflow. */
1562 void
1563 constant_expression_warning (tree value)
1565 if (warn_overflow && pedantic
1566 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1567 || TREE_CODE (value) == FIXED_CST
1568 || TREE_CODE (value) == VECTOR_CST
1569 || TREE_CODE (value) == COMPLEX_CST)
1570 && TREE_OVERFLOW (value))
1571 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1574 /* The same as above but print an unconditional error. */
1575 void
1576 constant_expression_error (tree value)
1578 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1579 || TREE_CODE (value) == FIXED_CST
1580 || TREE_CODE (value) == VECTOR_CST
1581 || TREE_CODE (value) == COMPLEX_CST)
1582 && TREE_OVERFLOW (value))
1583 error ("overflow in constant expression");
1586 /* Print a warning if an expression had overflow in folding and its
1587 operands hadn't.
1589 Invoke this function on every expression that
1590 (1) appears in the source code, and
1591 (2) is a constant expression that overflowed, and
1592 (3) is not already checked by convert_and_check;
1593 however, do not invoke this function on operands of explicit casts
1594 or when the expression is the result of an operator and any operand
1595 already overflowed. */
1597 void
1598 overflow_warning (location_t loc, tree value)
1600 if (c_inhibit_evaluation_warnings != 0)
1601 return;
1603 switch (TREE_CODE (value))
1605 case INTEGER_CST:
1606 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1607 break;
1609 case REAL_CST:
1610 warning_at (loc, OPT_Woverflow,
1611 "floating point overflow in expression");
1612 break;
1614 case FIXED_CST:
1615 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1616 break;
1618 case VECTOR_CST:
1619 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1620 break;
1622 case COMPLEX_CST:
1623 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1624 warning_at (loc, OPT_Woverflow,
1625 "complex integer overflow in expression");
1626 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1627 warning_at (loc, OPT_Woverflow,
1628 "complex floating point overflow in expression");
1629 break;
1631 default:
1632 break;
1636 /* Warn about uses of logical || / && operator in a context where it
1637 is likely that the bitwise equivalent was intended by the
1638 programmer. We have seen an expression in which CODE is a binary
1639 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1640 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1641 void
1642 warn_logical_operator (location_t location, enum tree_code code, tree type,
1643 enum tree_code code_left, tree op_left,
1644 enum tree_code ARG_UNUSED (code_right), tree op_right)
1646 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1647 int in0_p, in1_p, in_p;
1648 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1649 bool strict_overflow_p = false;
1651 if (code != TRUTH_ANDIF_EXPR
1652 && code != TRUTH_AND_EXPR
1653 && code != TRUTH_ORIF_EXPR
1654 && code != TRUTH_OR_EXPR)
1655 return;
1657 /* Warn if &&/|| are being used in a context where it is
1658 likely that the bitwise equivalent was intended by the
1659 programmer. That is, an expression such as op && MASK
1660 where op should not be any boolean expression, nor a
1661 constant, and mask seems to be a non-boolean integer constant. */
1662 if (!truth_value_p (code_left)
1663 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1664 && !CONSTANT_CLASS_P (op_left)
1665 && !TREE_NO_WARNING (op_left)
1666 && TREE_CODE (op_right) == INTEGER_CST
1667 && !integer_zerop (op_right)
1668 && !integer_onep (op_right))
1670 if (or_op)
1671 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1672 " applied to non-boolean constant");
1673 else
1674 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1675 " applied to non-boolean constant");
1676 TREE_NO_WARNING (op_left) = true;
1677 return;
1680 /* We do not warn for constants because they are typical of macro
1681 expansions that test for features. */
1682 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1683 return;
1685 /* This warning only makes sense with logical operands. */
1686 if (!(truth_value_p (TREE_CODE (op_left))
1687 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1688 || !(truth_value_p (TREE_CODE (op_right))
1689 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1690 return;
1693 /* We first test whether either side separately is trivially true
1694 (with OR) or trivially false (with AND). If so, do not warn.
1695 This is a common idiom for testing ranges of data types in
1696 portable code. */
1697 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1698 if (!lhs)
1699 return;
1700 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1701 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1703 /* If this is an OR operation, invert both sides; now, the result
1704 should be always false to get a warning. */
1705 if (or_op)
1706 in0_p = !in0_p;
1708 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1709 if (tem && integer_zerop (tem))
1710 return;
1712 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1713 if (!rhs)
1714 return;
1715 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1716 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1718 /* If this is an OR operation, invert both sides; now, the result
1719 should be always false to get a warning. */
1720 if (or_op)
1721 in1_p = !in1_p;
1723 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1724 if (tem && integer_zerop (tem))
1725 return;
1727 /* If both expressions have the same operand, if we can merge the
1728 ranges, and if the range test is always false, then warn. */
1729 if (operand_equal_p (lhs, rhs, 0)
1730 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1731 in1_p, low1, high1)
1732 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1733 type, lhs, in_p, low, high))
1734 && integer_zerop (tem))
1736 if (or_op)
1737 warning_at (location, OPT_Wlogical_op,
1738 "logical %<or%> "
1739 "of collectively exhaustive tests is always true");
1740 else
1741 warning_at (location, OPT_Wlogical_op,
1742 "logical %<and%> "
1743 "of mutually exclusive tests is always false");
1748 /* Warn if EXP contains any computations whose results are not used.
1749 Return true if a warning is printed; false otherwise. LOCUS is the
1750 (potential) location of the expression. */
1752 bool
1753 warn_if_unused_value (const_tree exp, location_t locus)
1755 restart:
1756 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1757 return false;
1759 /* Don't warn about void constructs. This includes casting to void,
1760 void function calls, and statement expressions with a final cast
1761 to void. */
1762 if (VOID_TYPE_P (TREE_TYPE (exp)))
1763 return false;
1765 if (EXPR_HAS_LOCATION (exp))
1766 locus = EXPR_LOCATION (exp);
1768 switch (TREE_CODE (exp))
1770 case PREINCREMENT_EXPR:
1771 case POSTINCREMENT_EXPR:
1772 case PREDECREMENT_EXPR:
1773 case POSTDECREMENT_EXPR:
1774 case MODIFY_EXPR:
1775 case INIT_EXPR:
1776 case TARGET_EXPR:
1777 case CALL_EXPR:
1778 case TRY_CATCH_EXPR:
1779 case WITH_CLEANUP_EXPR:
1780 case EXIT_EXPR:
1781 case VA_ARG_EXPR:
1782 return false;
1784 case BIND_EXPR:
1785 /* For a binding, warn if no side effect within it. */
1786 exp = BIND_EXPR_BODY (exp);
1787 goto restart;
1789 case SAVE_EXPR:
1790 case NON_LVALUE_EXPR:
1791 case NOP_EXPR:
1792 exp = TREE_OPERAND (exp, 0);
1793 goto restart;
1795 case TRUTH_ORIF_EXPR:
1796 case TRUTH_ANDIF_EXPR:
1797 /* In && or ||, warn if 2nd operand has no side effect. */
1798 exp = TREE_OPERAND (exp, 1);
1799 goto restart;
1801 case COMPOUND_EXPR:
1802 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1803 return true;
1804 /* Let people do `(foo (), 0)' without a warning. */
1805 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1806 return false;
1807 exp = TREE_OPERAND (exp, 1);
1808 goto restart;
1810 case COND_EXPR:
1811 /* If this is an expression with side effects, don't warn; this
1812 case commonly appears in macro expansions. */
1813 if (TREE_SIDE_EFFECTS (exp))
1814 return false;
1815 goto warn;
1817 case INDIRECT_REF:
1818 /* Don't warn about automatic dereferencing of references, since
1819 the user cannot control it. */
1820 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1822 exp = TREE_OPERAND (exp, 0);
1823 goto restart;
1825 /* Fall through. */
1827 default:
1828 /* Referencing a volatile value is a side effect, so don't warn. */
1829 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1830 && TREE_THIS_VOLATILE (exp))
1831 return false;
1833 /* If this is an expression which has no operands, there is no value
1834 to be unused. There are no such language-independent codes,
1835 but front ends may define such. */
1836 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1837 return false;
1839 warn:
1840 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1845 /* Print a warning about casts that might indicate violation
1846 of strict aliasing rules if -Wstrict-aliasing is used and
1847 strict aliasing mode is in effect. OTYPE is the original
1848 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1850 bool
1851 strict_aliasing_warning (tree otype, tree type, tree expr)
1853 /* Strip pointer conversion chains and get to the correct original type. */
1854 STRIP_NOPS (expr);
1855 otype = TREE_TYPE (expr);
1857 if (!(flag_strict_aliasing
1858 && POINTER_TYPE_P (type)
1859 && POINTER_TYPE_P (otype)
1860 && !VOID_TYPE_P (TREE_TYPE (type)))
1861 /* If the type we are casting to is a ref-all pointer
1862 dereferencing it is always valid. */
1863 || TYPE_REF_CAN_ALIAS_ALL (type))
1864 return false;
1866 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1867 && (DECL_P (TREE_OPERAND (expr, 0))
1868 || handled_component_p (TREE_OPERAND (expr, 0))))
1870 /* Casting the address of an object to non void pointer. Warn
1871 if the cast breaks type based aliasing. */
1872 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1874 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1875 "might break strict-aliasing rules");
1876 return true;
1878 else
1880 /* warn_strict_aliasing >= 3. This includes the default (3).
1881 Only warn if the cast is dereferenced immediately. */
1882 alias_set_type set1 =
1883 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1884 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1886 if (set1 != set2 && set2 != 0
1887 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1889 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1890 "pointer will break strict-aliasing rules");
1891 return true;
1893 else if (warn_strict_aliasing == 2
1894 && !alias_sets_must_conflict_p (set1, set2))
1896 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1897 "pointer might break strict-aliasing rules");
1898 return true;
1902 else
1903 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1905 /* At this level, warn for any conversions, even if an address is
1906 not taken in the same statement. This will likely produce many
1907 false positives, but could be useful to pinpoint problems that
1908 are not revealed at higher levels. */
1909 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1910 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1911 if (!COMPLETE_TYPE_P (type)
1912 || !alias_sets_must_conflict_p (set1, set2))
1914 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1915 "pointer might break strict-aliasing rules");
1916 return true;
1920 return false;
1923 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1924 sizeof as last operand of certain builtins. */
1926 void
1927 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1928 vec<tree, va_gc> *params, tree *sizeof_arg,
1929 bool (*comp_types) (tree, tree))
1931 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1932 bool strop = false, cmp = false;
1933 unsigned int idx = ~0;
1934 location_t loc;
1936 if (TREE_CODE (callee) != FUNCTION_DECL
1937 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1938 || vec_safe_length (params) <= 1)
1939 return;
1941 switch (DECL_FUNCTION_CODE (callee))
1943 case BUILT_IN_STRNCMP:
1944 case BUILT_IN_STRNCASECMP:
1945 cmp = true;
1946 /* FALLTHRU */
1947 case BUILT_IN_STRNCPY:
1948 case BUILT_IN_STRNCPY_CHK:
1949 case BUILT_IN_STRNCAT:
1950 case BUILT_IN_STRNCAT_CHK:
1951 case BUILT_IN_STPNCPY:
1952 case BUILT_IN_STPNCPY_CHK:
1953 strop = true;
1954 /* FALLTHRU */
1955 case BUILT_IN_MEMCPY:
1956 case BUILT_IN_MEMCPY_CHK:
1957 case BUILT_IN_MEMMOVE:
1958 case BUILT_IN_MEMMOVE_CHK:
1959 if (params->length () < 3)
1960 return;
1961 src = (*params)[1];
1962 dest = (*params)[0];
1963 idx = 2;
1964 break;
1965 case BUILT_IN_BCOPY:
1966 if (params->length () < 3)
1967 return;
1968 src = (*params)[0];
1969 dest = (*params)[1];
1970 idx = 2;
1971 break;
1972 case BUILT_IN_MEMCMP:
1973 case BUILT_IN_BCMP:
1974 if (params->length () < 3)
1975 return;
1976 src = (*params)[1];
1977 dest = (*params)[0];
1978 idx = 2;
1979 cmp = true;
1980 break;
1981 case BUILT_IN_MEMSET:
1982 case BUILT_IN_MEMSET_CHK:
1983 if (params->length () < 3)
1984 return;
1985 dest = (*params)[0];
1986 idx = 2;
1987 break;
1988 case BUILT_IN_BZERO:
1989 dest = (*params)[0];
1990 idx = 1;
1991 break;
1992 case BUILT_IN_STRNDUP:
1993 src = (*params)[0];
1994 strop = true;
1995 idx = 1;
1996 break;
1997 case BUILT_IN_MEMCHR:
1998 if (params->length () < 3)
1999 return;
2000 src = (*params)[0];
2001 idx = 2;
2002 break;
2003 case BUILT_IN_SNPRINTF:
2004 case BUILT_IN_SNPRINTF_CHK:
2005 case BUILT_IN_VSNPRINTF:
2006 case BUILT_IN_VSNPRINTF_CHK:
2007 dest = (*params)[0];
2008 idx = 1;
2009 strop = true;
2010 break;
2011 default:
2012 break;
2015 if (idx >= 3)
2016 return;
2018 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2019 return;
2021 type = TYPE_P (sizeof_arg[idx])
2022 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2023 if (!POINTER_TYPE_P (type))
2024 return;
2026 if (dest
2027 && (tem = tree_strip_nop_conversions (dest))
2028 && POINTER_TYPE_P (TREE_TYPE (tem))
2029 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2030 return;
2032 if (src
2033 && (tem = tree_strip_nop_conversions (src))
2034 && POINTER_TYPE_P (TREE_TYPE (tem))
2035 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2036 return;
2038 loc = sizeof_arg_loc[idx];
2040 if (dest && !cmp)
2042 if (!TYPE_P (sizeof_arg[idx])
2043 && operand_equal_p (dest, sizeof_arg[idx], 0)
2044 && comp_types (TREE_TYPE (dest), type))
2046 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2047 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2048 "argument to %<sizeof%> in %qD call is the same "
2049 "expression as the destination; did you mean to "
2050 "remove the addressof?", callee);
2051 else if ((TYPE_PRECISION (TREE_TYPE (type))
2052 == TYPE_PRECISION (char_type_node))
2053 || strop)
2054 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2055 "argument to %<sizeof%> in %qD call is the same "
2056 "expression as the destination; did you mean to "
2057 "provide an explicit length?", callee);
2058 else
2059 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2060 "argument to %<sizeof%> in %qD call is the same "
2061 "expression as the destination; did you mean to "
2062 "dereference it?", callee);
2063 return;
2066 if (POINTER_TYPE_P (TREE_TYPE (dest))
2067 && !strop
2068 && comp_types (TREE_TYPE (dest), type)
2069 && !VOID_TYPE_P (TREE_TYPE (type)))
2071 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2072 "argument to %<sizeof%> in %qD call is the same "
2073 "pointer type %qT as the destination; expected %qT "
2074 "or an explicit length", callee, TREE_TYPE (dest),
2075 TREE_TYPE (TREE_TYPE (dest)));
2076 return;
2080 if (src && !cmp)
2082 if (!TYPE_P (sizeof_arg[idx])
2083 && operand_equal_p (src, sizeof_arg[idx], 0)
2084 && comp_types (TREE_TYPE (src), type))
2086 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2087 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2088 "argument to %<sizeof%> in %qD call is the same "
2089 "expression as the source; did you mean to "
2090 "remove the addressof?", callee);
2091 else if ((TYPE_PRECISION (TREE_TYPE (type))
2092 == TYPE_PRECISION (char_type_node))
2093 || strop)
2094 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2095 "argument to %<sizeof%> in %qD call is the same "
2096 "expression as the source; did you mean to "
2097 "provide an explicit length?", callee);
2098 else
2099 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2100 "argument to %<sizeof%> in %qD call is the same "
2101 "expression as the source; did you mean to "
2102 "dereference it?", callee);
2103 return;
2106 if (POINTER_TYPE_P (TREE_TYPE (src))
2107 && !strop
2108 && comp_types (TREE_TYPE (src), type)
2109 && !VOID_TYPE_P (TREE_TYPE (type)))
2111 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2112 "argument to %<sizeof%> in %qD call is the same "
2113 "pointer type %qT as the source; expected %qT "
2114 "or an explicit length", callee, TREE_TYPE (src),
2115 TREE_TYPE (TREE_TYPE (src)));
2116 return;
2120 if (dest)
2122 if (!TYPE_P (sizeof_arg[idx])
2123 && operand_equal_p (dest, sizeof_arg[idx], 0)
2124 && comp_types (TREE_TYPE (dest), type))
2126 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2127 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2128 "argument to %<sizeof%> in %qD call is the same "
2129 "expression as the first source; did you mean to "
2130 "remove the addressof?", callee);
2131 else if ((TYPE_PRECISION (TREE_TYPE (type))
2132 == TYPE_PRECISION (char_type_node))
2133 || strop)
2134 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2135 "argument to %<sizeof%> in %qD call is the same "
2136 "expression as the first source; did you mean to "
2137 "provide an explicit length?", callee);
2138 else
2139 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2140 "argument to %<sizeof%> in %qD call is the same "
2141 "expression as the first source; did you mean to "
2142 "dereference it?", callee);
2143 return;
2146 if (POINTER_TYPE_P (TREE_TYPE (dest))
2147 && !strop
2148 && comp_types (TREE_TYPE (dest), type)
2149 && !VOID_TYPE_P (TREE_TYPE (type)))
2151 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2152 "argument to %<sizeof%> in %qD call is the same "
2153 "pointer type %qT as the first source; expected %qT "
2154 "or an explicit length", callee, TREE_TYPE (dest),
2155 TREE_TYPE (TREE_TYPE (dest)));
2156 return;
2160 if (src)
2162 if (!TYPE_P (sizeof_arg[idx])
2163 && operand_equal_p (src, sizeof_arg[idx], 0)
2164 && comp_types (TREE_TYPE (src), type))
2166 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2167 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2168 "argument to %<sizeof%> in %qD call is the same "
2169 "expression as the second source; did you mean to "
2170 "remove the addressof?", callee);
2171 else if ((TYPE_PRECISION (TREE_TYPE (type))
2172 == TYPE_PRECISION (char_type_node))
2173 || strop)
2174 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2175 "argument to %<sizeof%> in %qD call is the same "
2176 "expression as the second source; did you mean to "
2177 "provide an explicit length?", callee);
2178 else
2179 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2180 "argument to %<sizeof%> in %qD call is the same "
2181 "expression as the second source; did you mean to "
2182 "dereference it?", callee);
2183 return;
2186 if (POINTER_TYPE_P (TREE_TYPE (src))
2187 && !strop
2188 && comp_types (TREE_TYPE (src), type)
2189 && !VOID_TYPE_P (TREE_TYPE (type)))
2191 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2192 "argument to %<sizeof%> in %qD call is the same "
2193 "pointer type %qT as the second source; expected %qT "
2194 "or an explicit length", callee, TREE_TYPE (src),
2195 TREE_TYPE (TREE_TYPE (src)));
2196 return;
2202 /* Warn for unlikely, improbable, or stupid DECL declarations
2203 of `main'. */
2205 void
2206 check_main_parameter_types (tree decl)
2208 function_args_iterator iter;
2209 tree type;
2210 int argct = 0;
2212 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2214 /* XXX void_type_node belies the abstraction. */
2215 if (type == void_type_node || type == error_mark_node )
2216 break;
2218 ++argct;
2219 switch (argct)
2221 case 1:
2222 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2223 pedwarn (input_location, OPT_Wmain,
2224 "first argument of %q+D should be %<int%>", decl);
2225 break;
2227 case 2:
2228 if (TREE_CODE (type) != POINTER_TYPE
2229 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2230 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2231 != char_type_node))
2232 pedwarn (input_location, OPT_Wmain,
2233 "second argument of %q+D should be %<char **%>", decl);
2234 break;
2236 case 3:
2237 if (TREE_CODE (type) != POINTER_TYPE
2238 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2239 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2240 != char_type_node))
2241 pedwarn (input_location, OPT_Wmain,
2242 "third argument of %q+D should probably be "
2243 "%<char **%>", decl);
2244 break;
2248 /* It is intentional that this message does not mention the third
2249 argument because it's only mentioned in an appendix of the
2250 standard. */
2251 if (argct > 0 && (argct < 2 || argct > 3))
2252 pedwarn (input_location, OPT_Wmain,
2253 "%q+D takes only zero or two arguments", decl);
2256 /* vector_targets_convertible_p is used for vector pointer types. The
2257 callers perform various checks that the qualifiers are satisfactory,
2258 while OTOH vector_targets_convertible_p ignores the number of elements
2259 in the vectors. That's fine with vector pointers as we can consider,
2260 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2261 and that does not require and conversion of the pointer values.
2262 In contrast, vector_types_convertible_p and
2263 vector_types_compatible_elements_p are used for vector value types. */
2264 /* True if pointers to distinct types T1 and T2 can be converted to
2265 each other without an explicit cast. Only returns true for opaque
2266 vector types. */
2267 bool
2268 vector_targets_convertible_p (const_tree t1, const_tree t2)
2270 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2271 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2272 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2273 return true;
2275 return false;
2278 /* vector_types_convertible_p is used for vector value types.
2279 It could in principle call vector_targets_convertible_p as a subroutine,
2280 but then the check for vector type would be duplicated with its callers,
2281 and also the purpose of vector_targets_convertible_p would become
2282 muddled.
2283 Where vector_types_convertible_p returns true, a conversion might still be
2284 needed to make the types match.
2285 In contrast, vector_targets_convertible_p is used for vector pointer
2286 values, and vector_types_compatible_elements_p is used specifically
2287 in the context for binary operators, as a check if use is possible without
2288 conversion. */
2289 /* True if vector types T1 and T2 can be converted to each other
2290 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2291 can only be converted with -flax-vector-conversions yet that is not
2292 in effect, emit a note telling the user about that option if such
2293 a note has not previously been emitted. */
2294 bool
2295 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2297 static bool emitted_lax_note = false;
2298 bool convertible_lax;
2300 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2301 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2302 return true;
2304 convertible_lax =
2305 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2306 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2307 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2308 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2309 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2311 if (!convertible_lax || flag_lax_vector_conversions)
2312 return convertible_lax;
2314 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2315 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2316 return true;
2318 if (emit_lax_note && !emitted_lax_note)
2320 emitted_lax_note = true;
2321 inform (input_location, "use -flax-vector-conversions to permit "
2322 "conversions between vectors with differing "
2323 "element types or numbers of subparts");
2326 return false;
2329 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2330 and have vector types, V0 has the same type as V1, and the number of
2331 elements of V0, V1, MASK is the same.
2333 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2334 called with two arguments. In this case implementation passes the
2335 first argument twice in order to share the same tree code. This fact
2336 could enable the mask-values being twice the vector length. This is
2337 an implementation accident and this semantics is not guaranteed to
2338 the user. */
2339 tree
2340 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2341 bool complain)
2343 tree ret;
2344 bool wrap = true;
2345 bool maybe_const = false;
2346 bool two_arguments = false;
2348 if (v1 == NULL_TREE)
2350 two_arguments = true;
2351 v1 = v0;
2354 if (v0 == error_mark_node || v1 == error_mark_node
2355 || mask == error_mark_node)
2356 return error_mark_node;
2358 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2359 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2361 if (complain)
2362 error_at (loc, "__builtin_shuffle last argument must "
2363 "be an integer vector");
2364 return error_mark_node;
2367 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2368 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2370 if (complain)
2371 error_at (loc, "__builtin_shuffle arguments must be vectors");
2372 return error_mark_node;
2375 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2377 if (complain)
2378 error_at (loc, "__builtin_shuffle argument vectors must be of "
2379 "the same type");
2380 return error_mark_node;
2383 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2384 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2385 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2386 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2388 if (complain)
2389 error_at (loc, "__builtin_shuffle number of elements of the "
2390 "argument vector(s) and the mask vector should "
2391 "be the same");
2392 return error_mark_node;
2395 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2396 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2398 if (complain)
2399 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2400 "must have the same size as inner type of the mask");
2401 return error_mark_node;
2404 if (!c_dialect_cxx ())
2406 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2407 v0 = c_fully_fold (v0, false, &maybe_const);
2408 wrap &= maybe_const;
2410 if (two_arguments)
2411 v1 = v0 = save_expr (v0);
2412 else
2414 v1 = c_fully_fold (v1, false, &maybe_const);
2415 wrap &= maybe_const;
2418 mask = c_fully_fold (mask, false, &maybe_const);
2419 wrap &= maybe_const;
2421 else if (two_arguments)
2422 v1 = v0 = save_expr (v0);
2424 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2426 if (!c_dialect_cxx () && !wrap)
2427 ret = c_wrap_maybe_const (ret, true);
2429 return ret;
2432 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2433 to integral type. */
2435 static tree
2436 c_common_get_narrower (tree op, int *unsignedp_ptr)
2438 op = get_narrower (op, unsignedp_ptr);
2440 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2441 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2443 /* C++0x scoped enumerations don't implicitly convert to integral
2444 type; if we stripped an explicit conversion to a larger type we
2445 need to replace it so common_type will still work. */
2446 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2447 TYPE_UNSIGNED (TREE_TYPE (op)));
2448 op = fold_convert (type, op);
2450 return op;
2453 /* This is a helper function of build_binary_op.
2455 For certain operations if both args were extended from the same
2456 smaller type, do the arithmetic in that type and then extend.
2458 BITWISE indicates a bitwise operation.
2459 For them, this optimization is safe only if
2460 both args are zero-extended or both are sign-extended.
2461 Otherwise, we might change the result.
2462 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2463 but calculated in (unsigned short) it would be (unsigned short)-1.
2465 tree
2466 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2468 int unsigned0, unsigned1;
2469 tree arg0, arg1;
2470 int uns;
2471 tree type;
2473 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2474 excessive narrowing when we call get_narrower below. For
2475 example, suppose that OP0 is of unsigned int extended
2476 from signed char and that RESULT_TYPE is long long int.
2477 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2478 like
2480 (long long int) (unsigned int) signed_char
2482 which get_narrower would narrow down to
2484 (unsigned int) signed char
2486 If we do not cast OP0 first, get_narrower would return
2487 signed_char, which is inconsistent with the case of the
2488 explicit cast. */
2489 op0 = convert (result_type, op0);
2490 op1 = convert (result_type, op1);
2492 arg0 = c_common_get_narrower (op0, &unsigned0);
2493 arg1 = c_common_get_narrower (op1, &unsigned1);
2495 /* UNS is 1 if the operation to be done is an unsigned one. */
2496 uns = TYPE_UNSIGNED (result_type);
2498 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2499 but it *requires* conversion to FINAL_TYPE. */
2501 if ((TYPE_PRECISION (TREE_TYPE (op0))
2502 == TYPE_PRECISION (TREE_TYPE (arg0)))
2503 && TREE_TYPE (op0) != result_type)
2504 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2505 if ((TYPE_PRECISION (TREE_TYPE (op1))
2506 == TYPE_PRECISION (TREE_TYPE (arg1)))
2507 && TREE_TYPE (op1) != result_type)
2508 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2510 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2512 /* For bitwise operations, signedness of nominal type
2513 does not matter. Consider only how operands were extended. */
2514 if (bitwise)
2515 uns = unsigned0;
2517 /* Note that in all three cases below we refrain from optimizing
2518 an unsigned operation on sign-extended args.
2519 That would not be valid. */
2521 /* Both args variable: if both extended in same way
2522 from same width, do it in that width.
2523 Do it unsigned if args were zero-extended. */
2524 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2525 < TYPE_PRECISION (result_type))
2526 && (TYPE_PRECISION (TREE_TYPE (arg1))
2527 == TYPE_PRECISION (TREE_TYPE (arg0)))
2528 && unsigned0 == unsigned1
2529 && (unsigned0 || !uns))
2530 return c_common_signed_or_unsigned_type
2531 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2533 else if (TREE_CODE (arg0) == INTEGER_CST
2534 && (unsigned1 || !uns)
2535 && (TYPE_PRECISION (TREE_TYPE (arg1))
2536 < TYPE_PRECISION (result_type))
2537 && (type
2538 = c_common_signed_or_unsigned_type (unsigned1,
2539 TREE_TYPE (arg1)))
2540 && !POINTER_TYPE_P (type)
2541 && int_fits_type_p (arg0, type))
2542 return type;
2544 else if (TREE_CODE (arg1) == INTEGER_CST
2545 && (unsigned0 || !uns)
2546 && (TYPE_PRECISION (TREE_TYPE (arg0))
2547 < TYPE_PRECISION (result_type))
2548 && (type
2549 = c_common_signed_or_unsigned_type (unsigned0,
2550 TREE_TYPE (arg0)))
2551 && !POINTER_TYPE_P (type)
2552 && int_fits_type_p (arg1, type))
2553 return type;
2555 return result_type;
2558 /* Checks if expression EXPR of real/integer type cannot be converted
2559 to the real/integer type TYPE. Function returns true when:
2560 * EXPR is a constant which cannot be exactly converted to TYPE
2561 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2562 for EXPR type and TYPE being both integers or both real.
2563 * EXPR is not a constant of real type and TYPE is an integer.
2564 * EXPR is not a constant of integer type which cannot be
2565 exactly converted to real type.
2566 Function allows conversions between types of different signedness and
2567 does not return true in that case. Function can produce signedness
2568 warnings if PRODUCE_WARNS is true. */
2569 bool
2570 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2572 bool give_warning = false;
2573 tree expr_type = TREE_TYPE (expr);
2574 location_t loc = EXPR_LOC_OR_HERE (expr);
2576 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2578 /* Warn for real constant that is not an exact integer converted
2579 to integer type. */
2580 if (TREE_CODE (expr_type) == REAL_TYPE
2581 && TREE_CODE (type) == INTEGER_TYPE)
2583 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2584 give_warning = true;
2586 /* Warn for an integer constant that does not fit into integer type. */
2587 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2588 && TREE_CODE (type) == INTEGER_TYPE
2589 && !int_fits_type_p (expr, type))
2591 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2592 && tree_int_cst_sgn (expr) < 0)
2594 if (produce_warns)
2595 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2596 " implicitly converted to unsigned type");
2598 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2600 if (produce_warns)
2601 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2602 " constant value to negative integer");
2604 else
2605 give_warning = true;
2607 else if (TREE_CODE (type) == REAL_TYPE)
2609 /* Warn for an integer constant that does not fit into real type. */
2610 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2612 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2613 if (!exact_real_truncate (TYPE_MODE (type), &a))
2614 give_warning = true;
2616 /* Warn for a real constant that does not fit into a smaller
2617 real type. */
2618 else if (TREE_CODE (expr_type) == REAL_TYPE
2619 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2621 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2622 if (!exact_real_truncate (TYPE_MODE (type), &a))
2623 give_warning = true;
2627 else
2629 /* Warn for real types converted to integer types. */
2630 if (TREE_CODE (expr_type) == REAL_TYPE
2631 && TREE_CODE (type) == INTEGER_TYPE)
2632 give_warning = true;
2634 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2635 && TREE_CODE (type) == INTEGER_TYPE)
2637 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2638 expr = get_unwidened (expr, 0);
2639 expr_type = TREE_TYPE (expr);
2641 /* Don't warn for short y; short x = ((int)y & 0xff); */
2642 if (TREE_CODE (expr) == BIT_AND_EXPR
2643 || TREE_CODE (expr) == BIT_IOR_EXPR
2644 || TREE_CODE (expr) == BIT_XOR_EXPR)
2646 /* If both args were extended from a shortest type,
2647 use that type if that is safe. */
2648 expr_type = shorten_binary_op (expr_type,
2649 TREE_OPERAND (expr, 0),
2650 TREE_OPERAND (expr, 1),
2651 /* bitwise */1);
2653 if (TREE_CODE (expr) == BIT_AND_EXPR)
2655 tree op0 = TREE_OPERAND (expr, 0);
2656 tree op1 = TREE_OPERAND (expr, 1);
2657 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2658 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2660 /* If one of the operands is a non-negative constant
2661 that fits in the target type, then the type of the
2662 other operand does not matter. */
2663 if ((TREE_CODE (op0) == INTEGER_CST
2664 && int_fits_type_p (op0, c_common_signed_type (type))
2665 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2666 || (TREE_CODE (op1) == INTEGER_CST
2667 && int_fits_type_p (op1, c_common_signed_type (type))
2668 && int_fits_type_p (op1,
2669 c_common_unsigned_type (type))))
2670 return false;
2671 /* If constant is unsigned and fits in the target
2672 type, then the result will also fit. */
2673 else if ((TREE_CODE (op0) == INTEGER_CST
2674 && unsigned0
2675 && int_fits_type_p (op0, type))
2676 || (TREE_CODE (op1) == INTEGER_CST
2677 && unsigned1
2678 && int_fits_type_p (op1, type)))
2679 return false;
2682 /* Warn for integer types converted to smaller integer types. */
2683 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2684 give_warning = true;
2686 /* When they are the same width but different signedness,
2687 then the value may change. */
2688 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2689 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2690 /* Even when converted to a bigger type, if the type is
2691 unsigned but expr is signed, then negative values
2692 will be changed. */
2693 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2694 && produce_warns)
2695 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2696 "may change the sign of the result",
2697 type, expr_type);
2700 /* Warn for integer types converted to real types if and only if
2701 all the range of values of the integer type cannot be
2702 represented by the real type. */
2703 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2704 && TREE_CODE (type) == REAL_TYPE)
2706 tree type_low_bound, type_high_bound;
2707 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2709 /* Don't warn about char y = 0xff; float x = (int) y; */
2710 expr = get_unwidened (expr, 0);
2711 expr_type = TREE_TYPE (expr);
2713 type_low_bound = TYPE_MIN_VALUE (expr_type);
2714 type_high_bound = TYPE_MAX_VALUE (expr_type);
2715 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2716 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2718 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2719 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2720 give_warning = true;
2723 /* Warn for real types converted to smaller real types. */
2724 else if (TREE_CODE (expr_type) == REAL_TYPE
2725 && TREE_CODE (type) == REAL_TYPE
2726 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2727 give_warning = true;
2730 return give_warning;
2733 /* Warns if the conversion of EXPR to TYPE may alter a value.
2734 This is a helper function for warnings_for_convert_and_check. */
2736 static void
2737 conversion_warning (tree type, tree expr)
2739 tree expr_type = TREE_TYPE (expr);
2740 location_t loc = EXPR_LOC_OR_HERE (expr);
2742 if (!warn_conversion && !warn_sign_conversion)
2743 return;
2745 switch (TREE_CODE (expr))
2747 case EQ_EXPR:
2748 case NE_EXPR:
2749 case LE_EXPR:
2750 case GE_EXPR:
2751 case LT_EXPR:
2752 case GT_EXPR:
2753 case TRUTH_ANDIF_EXPR:
2754 case TRUTH_ORIF_EXPR:
2755 case TRUTH_AND_EXPR:
2756 case TRUTH_OR_EXPR:
2757 case TRUTH_XOR_EXPR:
2758 case TRUTH_NOT_EXPR:
2759 /* Conversion from boolean to a signed:1 bit-field (which only
2760 can hold the values 0 and -1) doesn't lose information - but
2761 it does change the value. */
2762 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2763 warning_at (loc, OPT_Wconversion,
2764 "conversion to %qT from boolean expression", type);
2765 return;
2767 case REAL_CST:
2768 case INTEGER_CST:
2769 if (unsafe_conversion_p (type, expr, true))
2770 warning_at (loc, OPT_Wconversion,
2771 "conversion to %qT alters %qT constant value",
2772 type, expr_type);
2773 return;
2775 case COND_EXPR:
2777 /* In case of COND_EXPR, we do not care about the type of
2778 COND_EXPR, only about the conversion of each operand. */
2779 tree op1 = TREE_OPERAND (expr, 1);
2780 tree op2 = TREE_OPERAND (expr, 2);
2782 conversion_warning (type, op1);
2783 conversion_warning (type, op2);
2784 return;
2787 default: /* 'expr' is not a constant. */
2788 if (unsafe_conversion_p (type, expr, true))
2789 warning_at (loc, OPT_Wconversion,
2790 "conversion to %qT from %qT may alter its value",
2791 type, expr_type);
2795 /* Produce warnings after a conversion. RESULT is the result of
2796 converting EXPR to TYPE. This is a helper function for
2797 convert_and_check and cp_convert_and_check. */
2799 void
2800 warnings_for_convert_and_check (tree type, tree expr, tree result)
2802 location_t loc = EXPR_LOC_OR_HERE (expr);
2804 if (TREE_CODE (expr) == INTEGER_CST
2805 && (TREE_CODE (type) == INTEGER_TYPE
2806 || TREE_CODE (type) == ENUMERAL_TYPE)
2807 && !int_fits_type_p (expr, type))
2809 /* Do not diagnose overflow in a constant expression merely
2810 because a conversion overflowed. */
2811 if (TREE_OVERFLOW (result))
2812 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2814 if (TYPE_UNSIGNED (type))
2816 /* This detects cases like converting -129 or 256 to
2817 unsigned char. */
2818 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2819 warning_at (loc, OPT_Woverflow,
2820 "large integer implicitly truncated to unsigned type");
2821 else
2822 conversion_warning (type, expr);
2824 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2825 warning (OPT_Woverflow,
2826 "overflow in implicit constant conversion");
2827 /* No warning for converting 0x80000000 to int. */
2828 else if (pedantic
2829 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2830 || TYPE_PRECISION (TREE_TYPE (expr))
2831 != TYPE_PRECISION (type)))
2832 warning_at (loc, OPT_Woverflow,
2833 "overflow in implicit constant conversion");
2835 else
2836 conversion_warning (type, expr);
2838 else if ((TREE_CODE (result) == INTEGER_CST
2839 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2840 warning_at (loc, OPT_Woverflow,
2841 "overflow in implicit constant conversion");
2842 else
2843 conversion_warning (type, expr);
2847 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2848 Invoke this function on every expression that is converted implicitly,
2849 i.e. because of language rules and not because of an explicit cast. */
2851 tree
2852 convert_and_check (tree type, tree expr)
2854 tree result;
2855 tree expr_for_warning;
2857 /* Convert from a value with possible excess precision rather than
2858 via the semantic type, but do not warn about values not fitting
2859 exactly in the semantic type. */
2860 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2862 tree orig_type = TREE_TYPE (expr);
2863 expr = TREE_OPERAND (expr, 0);
2864 expr_for_warning = convert (orig_type, expr);
2865 if (orig_type == type)
2866 return expr_for_warning;
2868 else
2869 expr_for_warning = expr;
2871 if (TREE_TYPE (expr) == type)
2872 return expr;
2874 result = convert (type, expr);
2876 if (c_inhibit_evaluation_warnings == 0
2877 && !TREE_OVERFLOW_P (expr)
2878 && result != error_mark_node)
2879 warnings_for_convert_and_check (type, expr_for_warning, result);
2881 return result;
2884 /* A node in a list that describes references to variables (EXPR), which are
2885 either read accesses if WRITER is zero, or write accesses, in which case
2886 WRITER is the parent of EXPR. */
2887 struct tlist
2889 struct tlist *next;
2890 tree expr, writer;
2893 /* Used to implement a cache the results of a call to verify_tree. We only
2894 use this for SAVE_EXPRs. */
2895 struct tlist_cache
2897 struct tlist_cache *next;
2898 struct tlist *cache_before_sp;
2899 struct tlist *cache_after_sp;
2900 tree expr;
2903 /* Obstack to use when allocating tlist structures, and corresponding
2904 firstobj. */
2905 static struct obstack tlist_obstack;
2906 static char *tlist_firstobj = 0;
2908 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2909 warnings. */
2910 static struct tlist *warned_ids;
2911 /* SAVE_EXPRs need special treatment. We process them only once and then
2912 cache the results. */
2913 static struct tlist_cache *save_expr_cache;
2915 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2916 static void merge_tlist (struct tlist **, struct tlist *, int);
2917 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2918 static int warning_candidate_p (tree);
2919 static bool candidate_equal_p (const_tree, const_tree);
2920 static void warn_for_collisions (struct tlist *);
2921 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2922 static struct tlist *new_tlist (struct tlist *, tree, tree);
2924 /* Create a new struct tlist and fill in its fields. */
2925 static struct tlist *
2926 new_tlist (struct tlist *next, tree t, tree writer)
2928 struct tlist *l;
2929 l = XOBNEW (&tlist_obstack, struct tlist);
2930 l->next = next;
2931 l->expr = t;
2932 l->writer = writer;
2933 return l;
2936 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2937 is nonnull, we ignore any node we find which has a writer equal to it. */
2939 static void
2940 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2942 while (add)
2944 struct tlist *next = add->next;
2945 if (!copy)
2946 add->next = *to;
2947 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2948 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2949 add = next;
2953 /* Merge the nodes of ADD into TO. This merging process is done so that for
2954 each variable that already exists in TO, no new node is added; however if
2955 there is a write access recorded in ADD, and an occurrence on TO is only
2956 a read access, then the occurrence in TO will be modified to record the
2957 write. */
2959 static void
2960 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2962 struct tlist **end = to;
2964 while (*end)
2965 end = &(*end)->next;
2967 while (add)
2969 int found = 0;
2970 struct tlist *tmp2;
2971 struct tlist *next = add->next;
2973 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2974 if (candidate_equal_p (tmp2->expr, add->expr))
2976 found = 1;
2977 if (!tmp2->writer)
2978 tmp2->writer = add->writer;
2980 if (!found)
2982 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2983 end = &(*end)->next;
2984 *end = 0;
2986 add = next;
2990 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2991 references in list LIST conflict with it, excluding reads if ONLY writers
2992 is nonzero. */
2994 static void
2995 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2996 int only_writes)
2998 struct tlist *tmp;
3000 /* Avoid duplicate warnings. */
3001 for (tmp = warned_ids; tmp; tmp = tmp->next)
3002 if (candidate_equal_p (tmp->expr, written))
3003 return;
3005 while (list)
3007 if (candidate_equal_p (list->expr, written)
3008 && !candidate_equal_p (list->writer, writer)
3009 && (!only_writes || list->writer))
3011 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3012 warning_at (EXPR_LOC_OR_HERE (writer),
3013 OPT_Wsequence_point, "operation on %qE may be undefined",
3014 list->expr);
3016 list = list->next;
3020 /* Given a list LIST of references to variables, find whether any of these
3021 can cause conflicts due to missing sequence points. */
3023 static void
3024 warn_for_collisions (struct tlist *list)
3026 struct tlist *tmp;
3028 for (tmp = list; tmp; tmp = tmp->next)
3030 if (tmp->writer)
3031 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3035 /* Return nonzero if X is a tree that can be verified by the sequence point
3036 warnings. */
3037 static int
3038 warning_candidate_p (tree x)
3040 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3041 return 0;
3043 if (TREE_CODE (x) == BLOCK)
3044 return 0;
3046 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3047 (lvalue_p) crash on TRY/CATCH. */
3048 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3049 return 0;
3051 if (!lvalue_p (x))
3052 return 0;
3054 /* No point to track non-const calls, they will never satisfy
3055 operand_equal_p. */
3056 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3057 return 0;
3059 if (TREE_CODE (x) == STRING_CST)
3060 return 0;
3062 return 1;
3065 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3066 static bool
3067 candidate_equal_p (const_tree x, const_tree y)
3069 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3072 /* Walk the tree X, and record accesses to variables. If X is written by the
3073 parent tree, WRITER is the parent.
3074 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3075 expression or its only operand forces a sequence point, then everything up
3076 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3077 in PNO_SP.
3078 Once we return, we will have emitted warnings if any subexpression before
3079 such a sequence point could be undefined. On a higher level, however, the
3080 sequence point may not be relevant, and we'll merge the two lists.
3082 Example: (b++, a) + b;
3083 The call that processes the COMPOUND_EXPR will store the increment of B
3084 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3085 processes the PLUS_EXPR will need to merge the two lists so that
3086 eventually, all accesses end up on the same list (and we'll warn about the
3087 unordered subexpressions b++ and b.
3089 A note on merging. If we modify the former example so that our expression
3090 becomes
3091 (b++, b) + a
3092 care must be taken not simply to add all three expressions into the final
3093 PNO_SP list. The function merge_tlist takes care of that by merging the
3094 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3095 way, so that no more than one access to B is recorded. */
3097 static void
3098 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3099 tree writer)
3101 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3102 enum tree_code code;
3103 enum tree_code_class cl;
3105 /* X may be NULL if it is the operand of an empty statement expression
3106 ({ }). */
3107 if (x == NULL)
3108 return;
3110 restart:
3111 code = TREE_CODE (x);
3112 cl = TREE_CODE_CLASS (code);
3114 if (warning_candidate_p (x))
3115 *pno_sp = new_tlist (*pno_sp, x, writer);
3117 switch (code)
3119 case CONSTRUCTOR:
3120 case SIZEOF_EXPR:
3121 return;
3123 case COMPOUND_EXPR:
3124 case TRUTH_ANDIF_EXPR:
3125 case TRUTH_ORIF_EXPR:
3126 tmp_before = tmp_nosp = tmp_list3 = 0;
3127 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3128 warn_for_collisions (tmp_nosp);
3129 merge_tlist (pbefore_sp, tmp_before, 0);
3130 merge_tlist (pbefore_sp, tmp_nosp, 0);
3131 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3132 merge_tlist (pbefore_sp, tmp_list3, 0);
3133 return;
3135 case COND_EXPR:
3136 tmp_before = tmp_list2 = 0;
3137 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3138 warn_for_collisions (tmp_list2);
3139 merge_tlist (pbefore_sp, tmp_before, 0);
3140 merge_tlist (pbefore_sp, tmp_list2, 1);
3142 tmp_list3 = tmp_nosp = 0;
3143 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3144 warn_for_collisions (tmp_nosp);
3145 merge_tlist (pbefore_sp, tmp_list3, 0);
3147 tmp_list3 = tmp_list2 = 0;
3148 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3149 warn_for_collisions (tmp_list2);
3150 merge_tlist (pbefore_sp, tmp_list3, 0);
3151 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3152 two first, to avoid warning for (a ? b++ : b++). */
3153 merge_tlist (&tmp_nosp, tmp_list2, 0);
3154 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3155 return;
3157 case PREDECREMENT_EXPR:
3158 case PREINCREMENT_EXPR:
3159 case POSTDECREMENT_EXPR:
3160 case POSTINCREMENT_EXPR:
3161 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3162 return;
3164 case MODIFY_EXPR:
3165 tmp_before = tmp_nosp = tmp_list3 = 0;
3166 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3167 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3168 /* Expressions inside the LHS are not ordered wrt. the sequence points
3169 in the RHS. Example:
3170 *a = (a++, 2)
3171 Despite the fact that the modification of "a" is in the before_sp
3172 list (tmp_before), it conflicts with the use of "a" in the LHS.
3173 We can handle this by adding the contents of tmp_list3
3174 to those of tmp_before, and redoing the collision warnings for that
3175 list. */
3176 add_tlist (&tmp_before, tmp_list3, x, 1);
3177 warn_for_collisions (tmp_before);
3178 /* Exclude the LHS itself here; we first have to merge it into the
3179 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3180 didn't exclude the LHS, we'd get it twice, once as a read and once
3181 as a write. */
3182 add_tlist (pno_sp, tmp_list3, x, 0);
3183 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3185 merge_tlist (pbefore_sp, tmp_before, 0);
3186 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3187 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3188 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3189 return;
3191 case CALL_EXPR:
3192 /* We need to warn about conflicts among arguments and conflicts between
3193 args and the function address. Side effects of the function address,
3194 however, are not ordered by the sequence point of the call. */
3196 call_expr_arg_iterator iter;
3197 tree arg;
3198 tmp_before = tmp_nosp = 0;
3199 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3200 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3202 tmp_list2 = tmp_list3 = 0;
3203 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3204 merge_tlist (&tmp_list3, tmp_list2, 0);
3205 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3207 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3208 warn_for_collisions (tmp_before);
3209 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3210 return;
3213 case TREE_LIST:
3214 /* Scan all the list, e.g. indices of multi dimensional array. */
3215 while (x)
3217 tmp_before = tmp_nosp = 0;
3218 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3219 merge_tlist (&tmp_nosp, tmp_before, 0);
3220 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3221 x = TREE_CHAIN (x);
3223 return;
3225 case SAVE_EXPR:
3227 struct tlist_cache *t;
3228 for (t = save_expr_cache; t; t = t->next)
3229 if (candidate_equal_p (t->expr, x))
3230 break;
3232 if (!t)
3234 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3235 t->next = save_expr_cache;
3236 t->expr = x;
3237 save_expr_cache = t;
3239 tmp_before = tmp_nosp = 0;
3240 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3241 warn_for_collisions (tmp_nosp);
3243 tmp_list3 = 0;
3244 while (tmp_nosp)
3246 struct tlist *t = tmp_nosp;
3247 tmp_nosp = t->next;
3248 merge_tlist (&tmp_list3, t, 0);
3250 t->cache_before_sp = tmp_before;
3251 t->cache_after_sp = tmp_list3;
3253 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3254 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3255 return;
3258 case ADDR_EXPR:
3259 x = TREE_OPERAND (x, 0);
3260 if (DECL_P (x))
3261 return;
3262 writer = 0;
3263 goto restart;
3265 default:
3266 /* For other expressions, simply recurse on their operands.
3267 Manual tail recursion for unary expressions.
3268 Other non-expressions need not be processed. */
3269 if (cl == tcc_unary)
3271 x = TREE_OPERAND (x, 0);
3272 writer = 0;
3273 goto restart;
3275 else if (IS_EXPR_CODE_CLASS (cl))
3277 int lp;
3278 int max = TREE_OPERAND_LENGTH (x);
3279 for (lp = 0; lp < max; lp++)
3281 tmp_before = tmp_nosp = 0;
3282 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3283 merge_tlist (&tmp_nosp, tmp_before, 0);
3284 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3287 return;
3291 /* Try to warn for undefined behavior in EXPR due to missing sequence
3292 points. */
3294 DEBUG_FUNCTION void
3295 verify_sequence_points (tree expr)
3297 struct tlist *before_sp = 0, *after_sp = 0;
3299 warned_ids = 0;
3300 save_expr_cache = 0;
3301 if (tlist_firstobj == 0)
3303 gcc_obstack_init (&tlist_obstack);
3304 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3307 verify_tree (expr, &before_sp, &after_sp, 0);
3308 warn_for_collisions (after_sp);
3309 obstack_free (&tlist_obstack, tlist_firstobj);
3312 /* Validate the expression after `case' and apply default promotions. */
3314 static tree
3315 check_case_value (tree value)
3317 if (value == NULL_TREE)
3318 return value;
3320 if (TREE_CODE (value) == INTEGER_CST)
3321 /* Promote char or short to int. */
3322 value = perform_integral_promotions (value);
3323 else if (value != error_mark_node)
3325 error ("case label does not reduce to an integer constant");
3326 value = error_mark_node;
3329 constant_expression_warning (value);
3331 return value;
3334 /* See if the case values LOW and HIGH are in the range of the original
3335 type (i.e. before the default conversion to int) of the switch testing
3336 expression.
3337 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3338 the type before promoting it. CASE_LOW_P is a pointer to the lower
3339 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3340 if the case is not a case range.
3341 The caller has to make sure that we are not called with NULL for
3342 CASE_LOW_P (i.e. the default case).
3343 Returns true if the case label is in range of ORIG_TYPE (saturated or
3344 untouched) or false if the label is out of range. */
3346 static bool
3347 check_case_bounds (tree type, tree orig_type,
3348 tree *case_low_p, tree *case_high_p)
3350 tree min_value, max_value;
3351 tree case_low = *case_low_p;
3352 tree case_high = case_high_p ? *case_high_p : case_low;
3354 /* If there was a problem with the original type, do nothing. */
3355 if (orig_type == error_mark_node)
3356 return true;
3358 min_value = TYPE_MIN_VALUE (orig_type);
3359 max_value = TYPE_MAX_VALUE (orig_type);
3361 /* Case label is less than minimum for type. */
3362 if (tree_int_cst_compare (case_low, min_value) < 0
3363 && tree_int_cst_compare (case_high, min_value) < 0)
3365 warning (0, "case label value is less than minimum value for type");
3366 return false;
3369 /* Case value is greater than maximum for type. */
3370 if (tree_int_cst_compare (case_low, max_value) > 0
3371 && tree_int_cst_compare (case_high, max_value) > 0)
3373 warning (0, "case label value exceeds maximum value for type");
3374 return false;
3377 /* Saturate lower case label value to minimum. */
3378 if (tree_int_cst_compare (case_high, min_value) >= 0
3379 && tree_int_cst_compare (case_low, min_value) < 0)
3381 warning (0, "lower value in case label range"
3382 " less than minimum value for type");
3383 case_low = min_value;
3386 /* Saturate upper case label value to maximum. */
3387 if (tree_int_cst_compare (case_low, max_value) <= 0
3388 && tree_int_cst_compare (case_high, max_value) > 0)
3390 warning (0, "upper value in case label range"
3391 " exceeds maximum value for type");
3392 case_high = max_value;
3395 if (*case_low_p != case_low)
3396 *case_low_p = convert (type, case_low);
3397 if (case_high_p && *case_high_p != case_high)
3398 *case_high_p = convert (type, case_high);
3400 return true;
3403 /* Return an integer type with BITS bits of precision,
3404 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3406 tree
3407 c_common_type_for_size (unsigned int bits, int unsignedp)
3409 if (bits == TYPE_PRECISION (integer_type_node))
3410 return unsignedp ? unsigned_type_node : integer_type_node;
3412 if (bits == TYPE_PRECISION (signed_char_type_node))
3413 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3415 if (bits == TYPE_PRECISION (short_integer_type_node))
3416 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3418 if (bits == TYPE_PRECISION (long_integer_type_node))
3419 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3421 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3422 return (unsignedp ? long_long_unsigned_type_node
3423 : long_long_integer_type_node);
3425 if (int128_integer_type_node
3426 && bits == TYPE_PRECISION (int128_integer_type_node))
3427 return (unsignedp ? int128_unsigned_type_node
3428 : int128_integer_type_node);
3430 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3431 return (unsignedp ? widest_unsigned_literal_type_node
3432 : widest_integer_literal_type_node);
3434 if (bits <= TYPE_PRECISION (intQI_type_node))
3435 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3437 if (bits <= TYPE_PRECISION (intHI_type_node))
3438 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3440 if (bits <= TYPE_PRECISION (intSI_type_node))
3441 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3443 if (bits <= TYPE_PRECISION (intDI_type_node))
3444 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3446 return 0;
3449 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3450 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3451 and saturating if SATP is nonzero, otherwise not saturating. */
3453 tree
3454 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3455 int unsignedp, int satp)
3457 enum machine_mode mode;
3458 if (ibit == 0)
3459 mode = unsignedp ? UQQmode : QQmode;
3460 else
3461 mode = unsignedp ? UHAmode : HAmode;
3463 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3464 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3465 break;
3467 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3469 sorry ("GCC cannot support operators with integer types and "
3470 "fixed-point types that have too many integral and "
3471 "fractional bits together");
3472 return 0;
3475 return c_common_type_for_mode (mode, satp);
3478 /* Used for communication between c_common_type_for_mode and
3479 c_register_builtin_type. */
3480 static GTY(()) tree registered_builtin_types;
3482 /* Return a data type that has machine mode MODE.
3483 If the mode is an integer,
3484 then UNSIGNEDP selects between signed and unsigned types.
3485 If the mode is a fixed-point mode,
3486 then UNSIGNEDP selects between saturating and nonsaturating types. */
3488 tree
3489 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3491 tree t;
3493 if (mode == TYPE_MODE (integer_type_node))
3494 return unsignedp ? unsigned_type_node : integer_type_node;
3496 if (mode == TYPE_MODE (signed_char_type_node))
3497 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3499 if (mode == TYPE_MODE (short_integer_type_node))
3500 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3502 if (mode == TYPE_MODE (long_integer_type_node))
3503 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3505 if (mode == TYPE_MODE (long_long_integer_type_node))
3506 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3508 if (int128_integer_type_node
3509 && mode == TYPE_MODE (int128_integer_type_node))
3510 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3512 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3513 return unsignedp ? widest_unsigned_literal_type_node
3514 : widest_integer_literal_type_node;
3516 if (mode == QImode)
3517 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3519 if (mode == HImode)
3520 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3522 if (mode == SImode)
3523 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3525 if (mode == DImode)
3526 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3528 #if HOST_BITS_PER_WIDE_INT >= 64
3529 if (mode == TYPE_MODE (intTI_type_node))
3530 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3531 #endif
3533 if (mode == TYPE_MODE (float_type_node))
3534 return float_type_node;
3536 if (mode == TYPE_MODE (double_type_node))
3537 return double_type_node;
3539 if (mode == TYPE_MODE (long_double_type_node))
3540 return long_double_type_node;
3542 if (mode == TYPE_MODE (void_type_node))
3543 return void_type_node;
3545 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3546 return (unsignedp
3547 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3548 : make_signed_type (GET_MODE_PRECISION (mode)));
3550 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3551 return (unsignedp
3552 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3553 : make_signed_type (GET_MODE_PRECISION (mode)));
3555 if (COMPLEX_MODE_P (mode))
3557 enum machine_mode inner_mode;
3558 tree inner_type;
3560 if (mode == TYPE_MODE (complex_float_type_node))
3561 return complex_float_type_node;
3562 if (mode == TYPE_MODE (complex_double_type_node))
3563 return complex_double_type_node;
3564 if (mode == TYPE_MODE (complex_long_double_type_node))
3565 return complex_long_double_type_node;
3567 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3568 return complex_integer_type_node;
3570 inner_mode = GET_MODE_INNER (mode);
3571 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3572 if (inner_type != NULL_TREE)
3573 return build_complex_type (inner_type);
3575 else if (VECTOR_MODE_P (mode))
3577 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3578 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3579 if (inner_type != NULL_TREE)
3580 return build_vector_type_for_mode (inner_type, mode);
3583 if (mode == TYPE_MODE (dfloat32_type_node))
3584 return dfloat32_type_node;
3585 if (mode == TYPE_MODE (dfloat64_type_node))
3586 return dfloat64_type_node;
3587 if (mode == TYPE_MODE (dfloat128_type_node))
3588 return dfloat128_type_node;
3590 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3592 if (mode == TYPE_MODE (short_fract_type_node))
3593 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3594 if (mode == TYPE_MODE (fract_type_node))
3595 return unsignedp ? sat_fract_type_node : fract_type_node;
3596 if (mode == TYPE_MODE (long_fract_type_node))
3597 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3598 if (mode == TYPE_MODE (long_long_fract_type_node))
3599 return unsignedp ? sat_long_long_fract_type_node
3600 : long_long_fract_type_node;
3602 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3603 return unsignedp ? sat_unsigned_short_fract_type_node
3604 : unsigned_short_fract_type_node;
3605 if (mode == TYPE_MODE (unsigned_fract_type_node))
3606 return unsignedp ? sat_unsigned_fract_type_node
3607 : unsigned_fract_type_node;
3608 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3609 return unsignedp ? sat_unsigned_long_fract_type_node
3610 : unsigned_long_fract_type_node;
3611 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3612 return unsignedp ? sat_unsigned_long_long_fract_type_node
3613 : unsigned_long_long_fract_type_node;
3615 if (mode == TYPE_MODE (short_accum_type_node))
3616 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3617 if (mode == TYPE_MODE (accum_type_node))
3618 return unsignedp ? sat_accum_type_node : accum_type_node;
3619 if (mode == TYPE_MODE (long_accum_type_node))
3620 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3621 if (mode == TYPE_MODE (long_long_accum_type_node))
3622 return unsignedp ? sat_long_long_accum_type_node
3623 : long_long_accum_type_node;
3625 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3626 return unsignedp ? sat_unsigned_short_accum_type_node
3627 : unsigned_short_accum_type_node;
3628 if (mode == TYPE_MODE (unsigned_accum_type_node))
3629 return unsignedp ? sat_unsigned_accum_type_node
3630 : unsigned_accum_type_node;
3631 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3632 return unsignedp ? sat_unsigned_long_accum_type_node
3633 : unsigned_long_accum_type_node;
3634 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3635 return unsignedp ? sat_unsigned_long_long_accum_type_node
3636 : unsigned_long_long_accum_type_node;
3638 if (mode == QQmode)
3639 return unsignedp ? sat_qq_type_node : qq_type_node;
3640 if (mode == HQmode)
3641 return unsignedp ? sat_hq_type_node : hq_type_node;
3642 if (mode == SQmode)
3643 return unsignedp ? sat_sq_type_node : sq_type_node;
3644 if (mode == DQmode)
3645 return unsignedp ? sat_dq_type_node : dq_type_node;
3646 if (mode == TQmode)
3647 return unsignedp ? sat_tq_type_node : tq_type_node;
3649 if (mode == UQQmode)
3650 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3651 if (mode == UHQmode)
3652 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3653 if (mode == USQmode)
3654 return unsignedp ? sat_usq_type_node : usq_type_node;
3655 if (mode == UDQmode)
3656 return unsignedp ? sat_udq_type_node : udq_type_node;
3657 if (mode == UTQmode)
3658 return unsignedp ? sat_utq_type_node : utq_type_node;
3660 if (mode == HAmode)
3661 return unsignedp ? sat_ha_type_node : ha_type_node;
3662 if (mode == SAmode)
3663 return unsignedp ? sat_sa_type_node : sa_type_node;
3664 if (mode == DAmode)
3665 return unsignedp ? sat_da_type_node : da_type_node;
3666 if (mode == TAmode)
3667 return unsignedp ? sat_ta_type_node : ta_type_node;
3669 if (mode == UHAmode)
3670 return unsignedp ? sat_uha_type_node : uha_type_node;
3671 if (mode == USAmode)
3672 return unsignedp ? sat_usa_type_node : usa_type_node;
3673 if (mode == UDAmode)
3674 return unsignedp ? sat_uda_type_node : uda_type_node;
3675 if (mode == UTAmode)
3676 return unsignedp ? sat_uta_type_node : uta_type_node;
3679 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3680 if (TYPE_MODE (TREE_VALUE (t)) == mode
3681 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3682 return TREE_VALUE (t);
3684 return 0;
3687 tree
3688 c_common_unsigned_type (tree type)
3690 return c_common_signed_or_unsigned_type (1, type);
3693 /* Return a signed type the same as TYPE in other respects. */
3695 tree
3696 c_common_signed_type (tree type)
3698 return c_common_signed_or_unsigned_type (0, type);
3701 /* Return a type the same as TYPE except unsigned or
3702 signed according to UNSIGNEDP. */
3704 tree
3705 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3707 tree type1;
3709 /* This block of code emulates the behavior of the old
3710 c_common_unsigned_type. In particular, it returns
3711 long_unsigned_type_node if passed a long, even when a int would
3712 have the same size. This is necessary for warnings to work
3713 correctly in archs where sizeof(int) == sizeof(long) */
3715 type1 = TYPE_MAIN_VARIANT (type);
3716 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3717 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3718 if (type1 == integer_type_node || type1 == unsigned_type_node)
3719 return unsignedp ? unsigned_type_node : integer_type_node;
3720 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3721 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3722 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3723 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3724 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3725 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3726 if (int128_integer_type_node
3727 && (type1 == int128_integer_type_node
3728 || type1 == int128_unsigned_type_node))
3729 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3730 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3731 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3732 #if HOST_BITS_PER_WIDE_INT >= 64
3733 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3734 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3735 #endif
3736 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3737 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3738 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3739 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3740 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3741 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3742 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3743 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3745 #define C_COMMON_FIXED_TYPES(NAME) \
3746 if (type1 == short_ ## NAME ## _type_node \
3747 || type1 == unsigned_short_ ## NAME ## _type_node) \
3748 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3749 : short_ ## NAME ## _type_node; \
3750 if (type1 == NAME ## _type_node \
3751 || type1 == unsigned_ ## NAME ## _type_node) \
3752 return unsignedp ? unsigned_ ## NAME ## _type_node \
3753 : NAME ## _type_node; \
3754 if (type1 == long_ ## NAME ## _type_node \
3755 || type1 == unsigned_long_ ## NAME ## _type_node) \
3756 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3757 : long_ ## NAME ## _type_node; \
3758 if (type1 == long_long_ ## NAME ## _type_node \
3759 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3760 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3761 : long_long_ ## NAME ## _type_node;
3763 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3764 if (type1 == NAME ## _type_node \
3765 || type1 == u ## NAME ## _type_node) \
3766 return unsignedp ? u ## NAME ## _type_node \
3767 : NAME ## _type_node;
3769 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3770 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3771 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3772 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3773 : sat_ ## short_ ## NAME ## _type_node; \
3774 if (type1 == sat_ ## NAME ## _type_node \
3775 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3776 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3777 : sat_ ## NAME ## _type_node; \
3778 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3779 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3780 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3781 : sat_ ## long_ ## NAME ## _type_node; \
3782 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3783 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3784 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3785 : sat_ ## long_long_ ## NAME ## _type_node;
3787 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3788 if (type1 == sat_ ## NAME ## _type_node \
3789 || type1 == sat_ ## u ## NAME ## _type_node) \
3790 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3791 : sat_ ## NAME ## _type_node;
3793 C_COMMON_FIXED_TYPES (fract);
3794 C_COMMON_FIXED_TYPES_SAT (fract);
3795 C_COMMON_FIXED_TYPES (accum);
3796 C_COMMON_FIXED_TYPES_SAT (accum);
3798 C_COMMON_FIXED_MODE_TYPES (qq);
3799 C_COMMON_FIXED_MODE_TYPES (hq);
3800 C_COMMON_FIXED_MODE_TYPES (sq);
3801 C_COMMON_FIXED_MODE_TYPES (dq);
3802 C_COMMON_FIXED_MODE_TYPES (tq);
3803 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3804 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3805 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3806 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3807 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3808 C_COMMON_FIXED_MODE_TYPES (ha);
3809 C_COMMON_FIXED_MODE_TYPES (sa);
3810 C_COMMON_FIXED_MODE_TYPES (da);
3811 C_COMMON_FIXED_MODE_TYPES (ta);
3812 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3813 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3814 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3815 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3817 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3818 the precision; they have precision set to match their range, but
3819 may use a wider mode to match an ABI. If we change modes, we may
3820 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3821 the precision as well, so as to yield correct results for
3822 bit-field types. C++ does not have these separate bit-field
3823 types, and producing a signed or unsigned variant of an
3824 ENUMERAL_TYPE may cause other problems as well. */
3826 if (!INTEGRAL_TYPE_P (type)
3827 || TYPE_UNSIGNED (type) == unsignedp)
3828 return type;
3830 #define TYPE_OK(node) \
3831 (TYPE_MODE (type) == TYPE_MODE (node) \
3832 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3833 if (TYPE_OK (signed_char_type_node))
3834 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3835 if (TYPE_OK (integer_type_node))
3836 return unsignedp ? unsigned_type_node : integer_type_node;
3837 if (TYPE_OK (short_integer_type_node))
3838 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3839 if (TYPE_OK (long_integer_type_node))
3840 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3841 if (TYPE_OK (long_long_integer_type_node))
3842 return (unsignedp ? long_long_unsigned_type_node
3843 : long_long_integer_type_node);
3844 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3845 return (unsignedp ? int128_unsigned_type_node
3846 : int128_integer_type_node);
3847 if (TYPE_OK (widest_integer_literal_type_node))
3848 return (unsignedp ? widest_unsigned_literal_type_node
3849 : widest_integer_literal_type_node);
3851 #if HOST_BITS_PER_WIDE_INT >= 64
3852 if (TYPE_OK (intTI_type_node))
3853 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3854 #endif
3855 if (TYPE_OK (intDI_type_node))
3856 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3857 if (TYPE_OK (intSI_type_node))
3858 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3859 if (TYPE_OK (intHI_type_node))
3860 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3861 if (TYPE_OK (intQI_type_node))
3862 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3863 #undef TYPE_OK
3865 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3868 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3870 tree
3871 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3873 /* Extended integer types of the same width as a standard type have
3874 lesser rank, so those of the same width as int promote to int or
3875 unsigned int and are valid for printf formats expecting int or
3876 unsigned int. To avoid such special cases, avoid creating
3877 extended integer types for bit-fields if a standard integer type
3878 is available. */
3879 if (width == TYPE_PRECISION (integer_type_node))
3880 return unsignedp ? unsigned_type_node : integer_type_node;
3881 if (width == TYPE_PRECISION (signed_char_type_node))
3882 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3883 if (width == TYPE_PRECISION (short_integer_type_node))
3884 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3885 if (width == TYPE_PRECISION (long_integer_type_node))
3886 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3887 if (width == TYPE_PRECISION (long_long_integer_type_node))
3888 return (unsignedp ? long_long_unsigned_type_node
3889 : long_long_integer_type_node);
3890 if (int128_integer_type_node
3891 && width == TYPE_PRECISION (int128_integer_type_node))
3892 return (unsignedp ? int128_unsigned_type_node
3893 : int128_integer_type_node);
3894 return build_nonstandard_integer_type (width, unsignedp);
3897 /* The C version of the register_builtin_type langhook. */
3899 void
3900 c_register_builtin_type (tree type, const char* name)
3902 tree decl;
3904 decl = build_decl (UNKNOWN_LOCATION,
3905 TYPE_DECL, get_identifier (name), type);
3906 DECL_ARTIFICIAL (decl) = 1;
3907 if (!TYPE_NAME (type))
3908 TYPE_NAME (type) = decl;
3909 pushdecl (decl);
3911 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3914 /* Print an error message for invalid operands to arith operation
3915 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3916 LOCATION is the location of the message. */
3918 void
3919 binary_op_error (location_t location, enum tree_code code,
3920 tree type0, tree type1)
3922 const char *opname;
3924 switch (code)
3926 case PLUS_EXPR:
3927 opname = "+"; break;
3928 case MINUS_EXPR:
3929 opname = "-"; break;
3930 case MULT_EXPR:
3931 opname = "*"; break;
3932 case MAX_EXPR:
3933 opname = "max"; break;
3934 case MIN_EXPR:
3935 opname = "min"; break;
3936 case EQ_EXPR:
3937 opname = "=="; break;
3938 case NE_EXPR:
3939 opname = "!="; break;
3940 case LE_EXPR:
3941 opname = "<="; break;
3942 case GE_EXPR:
3943 opname = ">="; break;
3944 case LT_EXPR:
3945 opname = "<"; break;
3946 case GT_EXPR:
3947 opname = ">"; break;
3948 case LSHIFT_EXPR:
3949 opname = "<<"; break;
3950 case RSHIFT_EXPR:
3951 opname = ">>"; break;
3952 case TRUNC_MOD_EXPR:
3953 case FLOOR_MOD_EXPR:
3954 opname = "%"; break;
3955 case TRUNC_DIV_EXPR:
3956 case FLOOR_DIV_EXPR:
3957 opname = "/"; break;
3958 case BIT_AND_EXPR:
3959 opname = "&"; break;
3960 case BIT_IOR_EXPR:
3961 opname = "|"; break;
3962 case TRUTH_ANDIF_EXPR:
3963 opname = "&&"; break;
3964 case TRUTH_ORIF_EXPR:
3965 opname = "||"; break;
3966 case BIT_XOR_EXPR:
3967 opname = "^"; break;
3968 default:
3969 gcc_unreachable ();
3971 error_at (location,
3972 "invalid operands to binary %s (have %qT and %qT)", opname,
3973 type0, type1);
3976 /* Given an expression as a tree, return its original type. Do this
3977 by stripping any conversion that preserves the sign and precision. */
3978 static tree
3979 expr_original_type (tree expr)
3981 STRIP_SIGN_NOPS (expr);
3982 return TREE_TYPE (expr);
3985 /* Subroutine of build_binary_op, used for comparison operations.
3986 See if the operands have both been converted from subword integer types
3987 and, if so, perhaps change them both back to their original type.
3988 This function is also responsible for converting the two operands
3989 to the proper common type for comparison.
3991 The arguments of this function are all pointers to local variables
3992 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3993 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3995 If this function returns nonzero, it means that the comparison has
3996 a constant value. What this function returns is an expression for
3997 that value. */
3999 tree
4000 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
4001 enum tree_code *rescode_ptr)
4003 tree type;
4004 tree op0 = *op0_ptr;
4005 tree op1 = *op1_ptr;
4006 int unsignedp0, unsignedp1;
4007 int real1, real2;
4008 tree primop0, primop1;
4009 enum tree_code code = *rescode_ptr;
4010 location_t loc = EXPR_LOC_OR_HERE (op0);
4012 /* Throw away any conversions to wider types
4013 already present in the operands. */
4015 primop0 = c_common_get_narrower (op0, &unsignedp0);
4016 primop1 = c_common_get_narrower (op1, &unsignedp1);
4018 /* If primopN is first sign-extended from primopN's precision to opN's
4019 precision, then zero-extended from opN's precision to
4020 *restype_ptr precision, shortenings might be invalid. */
4021 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4022 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4023 && !unsignedp0
4024 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4025 primop0 = op0;
4026 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4027 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4028 && !unsignedp1
4029 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4030 primop1 = op1;
4032 /* Handle the case that OP0 does not *contain* a conversion
4033 but it *requires* conversion to FINAL_TYPE. */
4035 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4036 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4037 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4038 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4040 /* If one of the operands must be floated, we cannot optimize. */
4041 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4042 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4044 /* If first arg is constant, swap the args (changing operation
4045 so value is preserved), for canonicalization. Don't do this if
4046 the second arg is 0. */
4048 if (TREE_CONSTANT (primop0)
4049 && !integer_zerop (primop1) && !real_zerop (primop1)
4050 && !fixed_zerop (primop1))
4052 tree tem = primop0;
4053 int temi = unsignedp0;
4054 primop0 = primop1;
4055 primop1 = tem;
4056 tem = op0;
4057 op0 = op1;
4058 op1 = tem;
4059 *op0_ptr = op0;
4060 *op1_ptr = op1;
4061 unsignedp0 = unsignedp1;
4062 unsignedp1 = temi;
4063 temi = real1;
4064 real1 = real2;
4065 real2 = temi;
4067 switch (code)
4069 case LT_EXPR:
4070 code = GT_EXPR;
4071 break;
4072 case GT_EXPR:
4073 code = LT_EXPR;
4074 break;
4075 case LE_EXPR:
4076 code = GE_EXPR;
4077 break;
4078 case GE_EXPR:
4079 code = LE_EXPR;
4080 break;
4081 default:
4082 break;
4084 *rescode_ptr = code;
4087 /* If comparing an integer against a constant more bits wide,
4088 maybe we can deduce a value of 1 or 0 independent of the data.
4089 Or else truncate the constant now
4090 rather than extend the variable at run time.
4092 This is only interesting if the constant is the wider arg.
4093 Also, it is not safe if the constant is unsigned and the
4094 variable arg is signed, since in this case the variable
4095 would be sign-extended and then regarded as unsigned.
4096 Our technique fails in this case because the lowest/highest
4097 possible unsigned results don't follow naturally from the
4098 lowest/highest possible values of the variable operand.
4099 For just EQ_EXPR and NE_EXPR there is another technique that
4100 could be used: see if the constant can be faithfully represented
4101 in the other operand's type, by truncating it and reextending it
4102 and see if that preserves the constant's value. */
4104 if (!real1 && !real2
4105 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4106 && TREE_CODE (primop1) == INTEGER_CST
4107 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4109 int min_gt, max_gt, min_lt, max_lt;
4110 tree maxval, minval;
4111 /* 1 if comparison is nominally unsigned. */
4112 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4113 tree val;
4115 type = c_common_signed_or_unsigned_type (unsignedp0,
4116 TREE_TYPE (primop0));
4118 maxval = TYPE_MAX_VALUE (type);
4119 minval = TYPE_MIN_VALUE (type);
4121 if (unsignedp && !unsignedp0)
4122 *restype_ptr = c_common_signed_type (*restype_ptr);
4124 if (TREE_TYPE (primop1) != *restype_ptr)
4126 /* Convert primop1 to target type, but do not introduce
4127 additional overflow. We know primop1 is an int_cst. */
4128 primop1 = force_fit_type_double (*restype_ptr,
4129 tree_to_double_int (primop1),
4130 0, TREE_OVERFLOW (primop1));
4132 if (type != *restype_ptr)
4134 minval = convert (*restype_ptr, minval);
4135 maxval = convert (*restype_ptr, maxval);
4138 if (unsignedp && unsignedp0)
4140 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4141 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4142 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4143 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4145 else
4147 min_gt = INT_CST_LT (primop1, minval);
4148 max_gt = INT_CST_LT (primop1, maxval);
4149 min_lt = INT_CST_LT (minval, primop1);
4150 max_lt = INT_CST_LT (maxval, primop1);
4153 val = 0;
4154 /* This used to be a switch, but Genix compiler can't handle that. */
4155 if (code == NE_EXPR)
4157 if (max_lt || min_gt)
4158 val = truthvalue_true_node;
4160 else if (code == EQ_EXPR)
4162 if (max_lt || min_gt)
4163 val = truthvalue_false_node;
4165 else if (code == LT_EXPR)
4167 if (max_lt)
4168 val = truthvalue_true_node;
4169 if (!min_lt)
4170 val = truthvalue_false_node;
4172 else if (code == GT_EXPR)
4174 if (min_gt)
4175 val = truthvalue_true_node;
4176 if (!max_gt)
4177 val = truthvalue_false_node;
4179 else if (code == LE_EXPR)
4181 if (!max_gt)
4182 val = truthvalue_true_node;
4183 if (min_gt)
4184 val = truthvalue_false_node;
4186 else if (code == GE_EXPR)
4188 if (!min_lt)
4189 val = truthvalue_true_node;
4190 if (max_lt)
4191 val = truthvalue_false_node;
4194 /* If primop0 was sign-extended and unsigned comparison specd,
4195 we did a signed comparison above using the signed type bounds.
4196 But the comparison we output must be unsigned.
4198 Also, for inequalities, VAL is no good; but if the signed
4199 comparison had *any* fixed result, it follows that the
4200 unsigned comparison just tests the sign in reverse
4201 (positive values are LE, negative ones GE).
4202 So we can generate an unsigned comparison
4203 against an extreme value of the signed type. */
4205 if (unsignedp && !unsignedp0)
4207 if (val != 0)
4208 switch (code)
4210 case LT_EXPR:
4211 case GE_EXPR:
4212 primop1 = TYPE_MIN_VALUE (type);
4213 val = 0;
4214 break;
4216 case LE_EXPR:
4217 case GT_EXPR:
4218 primop1 = TYPE_MAX_VALUE (type);
4219 val = 0;
4220 break;
4222 default:
4223 break;
4225 type = c_common_unsigned_type (type);
4228 if (TREE_CODE (primop0) != INTEGER_CST
4229 && c_inhibit_evaluation_warnings == 0)
4231 if (val == truthvalue_false_node)
4232 warning_at (loc, OPT_Wtype_limits,
4233 "comparison is always false due to limited range of data type");
4234 if (val == truthvalue_true_node)
4235 warning_at (loc, OPT_Wtype_limits,
4236 "comparison is always true due to limited range of data type");
4239 if (val != 0)
4241 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4242 if (TREE_SIDE_EFFECTS (primop0))
4243 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4244 return val;
4247 /* Value is not predetermined, but do the comparison
4248 in the type of the operand that is not constant.
4249 TYPE is already properly set. */
4252 /* If either arg is decimal float and the other is float, find the
4253 proper common type to use for comparison. */
4254 else if (real1 && real2
4255 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4256 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4257 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4259 else if (real1 && real2
4260 && (TYPE_PRECISION (TREE_TYPE (primop0))
4261 == TYPE_PRECISION (TREE_TYPE (primop1))))
4262 type = TREE_TYPE (primop0);
4264 /* If args' natural types are both narrower than nominal type
4265 and both extend in the same manner, compare them
4266 in the type of the wider arg.
4267 Otherwise must actually extend both to the nominal
4268 common type lest different ways of extending
4269 alter the result.
4270 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4272 else if (unsignedp0 == unsignedp1 && real1 == real2
4273 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4274 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4276 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4277 type = c_common_signed_or_unsigned_type (unsignedp0
4278 || TYPE_UNSIGNED (*restype_ptr),
4279 type);
4280 /* Make sure shorter operand is extended the right way
4281 to match the longer operand. */
4282 primop0
4283 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4284 TREE_TYPE (primop0)),
4285 primop0);
4286 primop1
4287 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4288 TREE_TYPE (primop1)),
4289 primop1);
4291 else
4293 /* Here we must do the comparison on the nominal type
4294 using the args exactly as we received them. */
4295 type = *restype_ptr;
4296 primop0 = op0;
4297 primop1 = op1;
4299 if (!real1 && !real2 && integer_zerop (primop1)
4300 && TYPE_UNSIGNED (*restype_ptr))
4302 tree value = 0;
4303 /* All unsigned values are >= 0, so we warn. However,
4304 if OP0 is a constant that is >= 0, the signedness of
4305 the comparison isn't an issue, so suppress the
4306 warning. */
4307 bool warn =
4308 warn_type_limits && !in_system_header
4309 && c_inhibit_evaluation_warnings == 0
4310 && !(TREE_CODE (primop0) == INTEGER_CST
4311 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4312 primop0)))
4313 /* Do not warn for enumeration types. */
4314 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4316 switch (code)
4318 case GE_EXPR:
4319 if (warn)
4320 warning_at (loc, OPT_Wtype_limits,
4321 "comparison of unsigned expression >= 0 is always true");
4322 value = truthvalue_true_node;
4323 break;
4325 case LT_EXPR:
4326 if (warn)
4327 warning_at (loc, OPT_Wtype_limits,
4328 "comparison of unsigned expression < 0 is always false");
4329 value = truthvalue_false_node;
4330 break;
4332 default:
4333 break;
4336 if (value != 0)
4338 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4339 if (TREE_SIDE_EFFECTS (primop0))
4340 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4341 primop0, value);
4342 return value;
4347 *op0_ptr = convert (type, primop0);
4348 *op1_ptr = convert (type, primop1);
4350 *restype_ptr = truthvalue_type_node;
4352 return 0;
4355 /* Return a tree for the sum or difference (RESULTCODE says which)
4356 of pointer PTROP and integer INTOP. */
4358 tree
4359 pointer_int_sum (location_t loc, enum tree_code resultcode,
4360 tree ptrop, tree intop, bool complain)
4362 tree size_exp, ret;
4364 /* The result is a pointer of the same type that is being added. */
4365 tree result_type = TREE_TYPE (ptrop);
4367 /* If the pointer lives in UPC shared memory, then
4368 drop the 'shared' qualifier. */
4369 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4370 result_type = build_upc_unshared_type (result_type);
4372 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4374 if (complain && warn_pointer_arith)
4375 pedwarn (loc, OPT_Wpointer_arith,
4376 "pointer of type %<void *%> used in arithmetic");
4377 else if (!complain)
4378 return error_mark_node;
4379 size_exp = integer_one_node;
4381 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4383 if (complain && warn_pointer_arith)
4384 pedwarn (loc, OPT_Wpointer_arith,
4385 "pointer to a function used in arithmetic");
4386 else if (!complain)
4387 return error_mark_node;
4388 size_exp = integer_one_node;
4390 else
4391 size_exp = size_in_bytes (TREE_TYPE (result_type));
4393 /* We are manipulating pointer values, so we don't need to warn
4394 about relying on undefined signed overflow. We disable the
4395 warning here because we use integer types so fold won't know that
4396 they are really pointers. */
4397 fold_defer_overflow_warnings ();
4399 /* If what we are about to multiply by the size of the elements
4400 contains a constant term, apply distributive law
4401 and multiply that constant term separately.
4402 This helps produce common subexpressions. */
4403 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4404 && !TREE_CONSTANT (intop)
4405 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4406 && TREE_CONSTANT (size_exp)
4407 /* If the constant comes from pointer subtraction,
4408 skip this optimization--it would cause an error. */
4409 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4410 /* If the constant is unsigned, and smaller than the pointer size,
4411 then we must skip this optimization. This is because it could cause
4412 an overflow error if the constant is negative but INTOP is not. */
4413 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4414 || (TYPE_PRECISION (TREE_TYPE (intop))
4415 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4417 enum tree_code subcode = resultcode;
4418 tree int_type = TREE_TYPE (intop);
4419 if (TREE_CODE (intop) == MINUS_EXPR)
4420 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4421 /* Convert both subexpression types to the type of intop,
4422 because weird cases involving pointer arithmetic
4423 can result in a sum or difference with different type args. */
4424 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4425 subcode, ptrop,
4426 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4427 intop = convert (int_type, TREE_OPERAND (intop, 0));
4430 /* Convert the integer argument to a type the same size as sizetype
4431 so the multiply won't overflow spuriously. */
4432 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4433 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4434 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4435 TYPE_UNSIGNED (sizetype)), intop);
4437 /* Replace the integer argument with a suitable product by the object size.
4438 Do this multiplication as signed, then convert to the appropriate type
4439 for the pointer operation and disregard an overflow that occurred only
4440 because of the sign-extension change in the latter conversion. */
4442 tree t = build_binary_op (loc,
4443 MULT_EXPR, intop,
4444 convert (TREE_TYPE (intop), size_exp), 1);
4445 intop = convert (sizetype, t);
4446 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4447 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4448 TREE_INT_CST_HIGH (intop));
4451 /* Create the sum or difference. */
4452 if (resultcode == MINUS_EXPR)
4453 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4455 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4457 fold_undefer_and_ignore_overflow_warnings ();
4459 return ret;
4462 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4463 and if NON_CONST is known not to be permitted in an evaluated part
4464 of a constant expression. */
4466 tree
4467 c_wrap_maybe_const (tree expr, bool non_const)
4469 bool nowarning = TREE_NO_WARNING (expr);
4470 location_t loc = EXPR_LOCATION (expr);
4472 /* This should never be called for C++. */
4473 if (c_dialect_cxx ())
4474 gcc_unreachable ();
4476 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4477 STRIP_TYPE_NOPS (expr);
4478 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4479 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4480 if (nowarning)
4481 TREE_NO_WARNING (expr) = 1;
4482 protected_set_expr_location (expr, loc);
4484 return expr;
4487 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4488 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4489 around the SAVE_EXPR if needed so that c_fully_fold does not need
4490 to look inside SAVE_EXPRs. */
4492 tree
4493 c_save_expr (tree expr)
4495 bool maybe_const = true;
4496 if (c_dialect_cxx ())
4497 return save_expr (expr);
4498 expr = c_fully_fold (expr, false, &maybe_const);
4499 expr = save_expr (expr);
4500 if (!maybe_const)
4501 expr = c_wrap_maybe_const (expr, true);
4502 return expr;
4505 /* Return whether EXPR is a declaration whose address can never be
4506 NULL. */
4508 bool
4509 decl_with_nonnull_addr_p (const_tree expr)
4511 return (DECL_P (expr)
4512 && (TREE_CODE (expr) == PARM_DECL
4513 || TREE_CODE (expr) == LABEL_DECL
4514 || !DECL_WEAK (expr)));
4517 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4518 or for an `if' or `while' statement or ?..: exp. It should already
4519 have been validated to be of suitable type; otherwise, a bad
4520 diagnostic may result.
4522 The EXPR is located at LOCATION.
4524 This preparation consists of taking the ordinary
4525 representation of an expression expr and producing a valid tree
4526 boolean expression describing whether expr is nonzero. We could
4527 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4528 but we optimize comparisons, &&, ||, and !.
4530 The resulting type should always be `truthvalue_type_node'. */
4532 tree
4533 c_common_truthvalue_conversion (location_t location, tree expr)
4535 switch (TREE_CODE (expr))
4537 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4538 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4539 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4540 case ORDERED_EXPR: case UNORDERED_EXPR:
4541 if (TREE_TYPE (expr) == truthvalue_type_node)
4542 return expr;
4543 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4544 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4545 goto ret;
4547 case TRUTH_ANDIF_EXPR:
4548 case TRUTH_ORIF_EXPR:
4549 case TRUTH_AND_EXPR:
4550 case TRUTH_OR_EXPR:
4551 case TRUTH_XOR_EXPR:
4552 if (TREE_TYPE (expr) == truthvalue_type_node)
4553 return expr;
4554 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4555 c_common_truthvalue_conversion (location,
4556 TREE_OPERAND (expr, 0)),
4557 c_common_truthvalue_conversion (location,
4558 TREE_OPERAND (expr, 1)));
4559 goto ret;
4561 case TRUTH_NOT_EXPR:
4562 if (TREE_TYPE (expr) == truthvalue_type_node)
4563 return expr;
4564 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4565 c_common_truthvalue_conversion (location,
4566 TREE_OPERAND (expr, 0)));
4567 goto ret;
4569 case ERROR_MARK:
4570 return expr;
4572 case INTEGER_CST:
4573 return integer_zerop (expr) ? truthvalue_false_node
4574 : truthvalue_true_node;
4576 case REAL_CST:
4577 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4578 ? truthvalue_true_node
4579 : truthvalue_false_node;
4581 case FIXED_CST:
4582 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4583 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4584 ? truthvalue_true_node
4585 : truthvalue_false_node;
4587 case FUNCTION_DECL:
4588 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4589 /* Fall through. */
4591 case ADDR_EXPR:
4593 tree inner = TREE_OPERAND (expr, 0);
4594 if (decl_with_nonnull_addr_p (inner))
4596 /* Common Ada/Pascal programmer's mistake. */
4597 warning_at (location,
4598 OPT_Waddress,
4599 "the address of %qD will always evaluate as %<true%>",
4600 inner);
4601 return truthvalue_true_node;
4603 break;
4606 case COMPLEX_EXPR:
4607 expr = build_binary_op (EXPR_LOCATION (expr),
4608 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4609 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4610 c_common_truthvalue_conversion (location,
4611 TREE_OPERAND (expr, 0)),
4612 c_common_truthvalue_conversion (location,
4613 TREE_OPERAND (expr, 1)),
4615 goto ret;
4617 case NEGATE_EXPR:
4618 case ABS_EXPR:
4619 case FLOAT_EXPR:
4620 case EXCESS_PRECISION_EXPR:
4621 /* These don't change whether an object is nonzero or zero. */
4622 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4624 case LROTATE_EXPR:
4625 case RROTATE_EXPR:
4626 /* These don't change whether an object is zero or nonzero, but
4627 we can't ignore them if their second arg has side-effects. */
4628 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4630 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4631 TREE_OPERAND (expr, 1),
4632 c_common_truthvalue_conversion
4633 (location, TREE_OPERAND (expr, 0)));
4634 goto ret;
4636 else
4637 return c_common_truthvalue_conversion (location,
4638 TREE_OPERAND (expr, 0));
4640 case COND_EXPR:
4641 /* Distribute the conversion into the arms of a COND_EXPR. */
4642 if (c_dialect_cxx ())
4644 tree op1 = TREE_OPERAND (expr, 1);
4645 tree op2 = TREE_OPERAND (expr, 2);
4646 /* In C++ one of the arms might have void type if it is throw. */
4647 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4648 op1 = c_common_truthvalue_conversion (location, op1);
4649 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4650 op2 = c_common_truthvalue_conversion (location, op2);
4651 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4652 TREE_OPERAND (expr, 0), op1, op2);
4653 goto ret;
4655 else
4657 /* Folding will happen later for C. */
4658 expr = build3 (COND_EXPR, truthvalue_type_node,
4659 TREE_OPERAND (expr, 0),
4660 c_common_truthvalue_conversion (location,
4661 TREE_OPERAND (expr, 1)),
4662 c_common_truthvalue_conversion (location,
4663 TREE_OPERAND (expr, 2)));
4664 goto ret;
4667 CASE_CONVERT:
4669 tree totype = TREE_TYPE (expr);
4670 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4672 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4673 since that affects how `default_conversion' will behave. */
4674 if (TREE_CODE (totype) == REFERENCE_TYPE
4675 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4676 break;
4677 /* Don't strip a conversion from C++0x scoped enum, since they
4678 don't implicitly convert to other types. */
4679 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4680 && ENUM_IS_SCOPED (fromtype))
4681 break;
4682 /* If this isn't narrowing the argument, we can ignore it. */
4683 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4684 return c_common_truthvalue_conversion (location,
4685 TREE_OPERAND (expr, 0));
4687 break;
4689 case MODIFY_EXPR:
4690 if (!TREE_NO_WARNING (expr)
4691 && warn_parentheses)
4693 warning (OPT_Wparentheses,
4694 "suggest parentheses around assignment used as truth value");
4695 TREE_NO_WARNING (expr) = 1;
4697 break;
4699 default:
4700 break;
4703 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4705 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4706 expr = (build_binary_op
4707 (EXPR_LOCATION (expr),
4708 (TREE_SIDE_EFFECTS (expr)
4709 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4710 c_common_truthvalue_conversion
4711 (location,
4712 build_unary_op (location, REALPART_EXPR, t, 0)),
4713 c_common_truthvalue_conversion
4714 (location,
4715 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4716 0));
4717 goto ret;
4720 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4722 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4723 FCONST0 (TYPE_MODE
4724 (TREE_TYPE (expr))));
4725 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4727 else
4728 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4730 ret:
4731 protected_set_expr_location (expr, location);
4732 return expr;
4735 static void def_builtin_1 (enum built_in_function fncode,
4736 const char *name,
4737 enum built_in_class fnclass,
4738 tree fntype, tree libtype,
4739 bool both_p, bool fallback_p, bool nonansi_p,
4740 tree fnattrs, bool implicit_p);
4743 /* Apply the TYPE_QUALS to the new DECL. */
4745 void
4746 c_apply_type_quals_to_decl (int type_quals, tree decl)
4748 tree type = TREE_TYPE (decl);
4750 if (type == error_mark_node)
4751 return;
4753 if ((type_quals & TYPE_QUAL_CONST)
4754 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4755 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4756 constructor can produce constant init, so rely on cp_finish_decl to
4757 clear TREE_READONLY if the variable has non-constant init. */
4758 TREE_READONLY (decl) = 1;
4759 if (type_quals & TYPE_QUAL_VOLATILE)
4761 TREE_SIDE_EFFECTS (decl) = 1;
4762 TREE_THIS_VOLATILE (decl) = 1;
4764 if (type_quals & TYPE_QUAL_RESTRICT)
4766 while (type && TREE_CODE (type) == ARRAY_TYPE)
4767 /* Allow 'restrict' on arrays of pointers.
4768 FIXME currently we just ignore it. */
4769 type = TREE_TYPE (type);
4770 if (!type
4771 || !POINTER_TYPE_P (type)
4772 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4773 error ("invalid use of %<restrict%>");
4775 if (type_quals & TYPE_QUAL_SHARED)
4777 TREE_SHARED (decl) = 1;
4778 if (type_quals & TYPE_QUAL_STRICT)
4779 TREE_STRICT(decl) = 1;
4780 else if (type_quals & TYPE_QUAL_RELAXED)
4781 TREE_RELAXED(decl) = 1;
4782 /* The declaration's type should have been previously defined
4783 as a UPC shared type. */
4784 gcc_assert (upc_shared_type_p (type));
4788 /* Hash function for the problem of multiple type definitions in
4789 different files. This must hash all types that will compare
4790 equal via comptypes to the same value. In practice it hashes
4791 on some of the simple stuff and leaves the details to comptypes. */
4793 static hashval_t
4794 c_type_hash (const void *p)
4796 int n_elements;
4797 int shift, size;
4798 const_tree const t = (const_tree) p;
4799 tree t2;
4800 switch (TREE_CODE (t))
4802 /* For pointers, hash on pointee type plus some swizzling. */
4803 case POINTER_TYPE:
4804 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4805 /* Hash on number of elements and total size. */
4806 case ENUMERAL_TYPE:
4807 shift = 3;
4808 t2 = TYPE_VALUES (t);
4809 break;
4810 case RECORD_TYPE:
4811 shift = 0;
4812 t2 = TYPE_FIELDS (t);
4813 break;
4814 case QUAL_UNION_TYPE:
4815 shift = 1;
4816 t2 = TYPE_FIELDS (t);
4817 break;
4818 case UNION_TYPE:
4819 shift = 2;
4820 t2 = TYPE_FIELDS (t);
4821 break;
4822 default:
4823 gcc_unreachable ();
4825 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4826 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4827 n_elements = list_length (t2);
4828 /* We might have a VLA here. */
4829 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4830 size = 0;
4831 else
4832 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4833 return ((size << 24) | (n_elements << shift));
4836 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4838 /* Return the typed-based alias set for T, which may be an expression
4839 or a type. Return -1 if we don't do anything special. */
4841 alias_set_type
4842 c_common_get_alias_set (tree t)
4844 tree u;
4845 PTR *slot;
4847 /* For VLAs, use the alias set of the element type rather than the
4848 default of alias set 0 for types compared structurally. */
4849 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4851 if (TREE_CODE (t) == ARRAY_TYPE)
4852 return get_alias_set (TREE_TYPE (t));
4853 return -1;
4856 /* Permit type-punning when accessing a union, provided the access
4857 is directly through the union. For example, this code does not
4858 permit taking the address of a union member and then storing
4859 through it. Even the type-punning allowed here is a GCC
4860 extension, albeit a common and useful one; the C standard says
4861 that such accesses have implementation-defined behavior. */
4862 for (u = t;
4863 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4864 u = TREE_OPERAND (u, 0))
4865 if (TREE_CODE (u) == COMPONENT_REF
4866 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4867 return 0;
4869 /* That's all the expressions we handle specially. */
4870 if (!TYPE_P (t))
4871 return -1;
4873 /* The C standard guarantees that any object may be accessed via an
4874 lvalue that has character type. */
4875 if (t == char_type_node
4876 || t == signed_char_type_node
4877 || t == unsigned_char_type_node)
4878 return 0;
4880 /* The C standard specifically allows aliasing between signed and
4881 unsigned variants of the same type. We treat the signed
4882 variant as canonical. */
4883 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4885 tree t1 = c_common_signed_type (t);
4887 /* t1 == t can happen for boolean nodes which are always unsigned. */
4888 if (t1 != t)
4889 return get_alias_set (t1);
4892 /* Handle the case of multiple type nodes referring to "the same" type,
4893 which occurs with IMA. These share an alias set. FIXME: Currently only
4894 C90 is handled. (In C99 type compatibility is not transitive, which
4895 complicates things mightily. The alias set splay trees can theoretically
4896 represent this, but insertion is tricky when you consider all the
4897 different orders things might arrive in.) */
4899 if (c_language != clk_c || flag_isoc99)
4900 return -1;
4902 /* Save time if there's only one input file. */
4903 if (num_in_fnames == 1)
4904 return -1;
4906 /* Pointers need special handling if they point to any type that
4907 needs special handling (below). */
4908 if (TREE_CODE (t) == POINTER_TYPE)
4910 tree t2;
4911 /* Find bottom type under any nested POINTERs. */
4912 for (t2 = TREE_TYPE (t);
4913 TREE_CODE (t2) == POINTER_TYPE;
4914 t2 = TREE_TYPE (t2))
4916 if (TREE_CODE (t2) != RECORD_TYPE
4917 && TREE_CODE (t2) != ENUMERAL_TYPE
4918 && TREE_CODE (t2) != QUAL_UNION_TYPE
4919 && TREE_CODE (t2) != UNION_TYPE)
4920 return -1;
4921 if (TYPE_SIZE (t2) == 0)
4922 return -1;
4924 /* These are the only cases that need special handling. */
4925 if (TREE_CODE (t) != RECORD_TYPE
4926 && TREE_CODE (t) != ENUMERAL_TYPE
4927 && TREE_CODE (t) != QUAL_UNION_TYPE
4928 && TREE_CODE (t) != UNION_TYPE
4929 && TREE_CODE (t) != POINTER_TYPE)
4930 return -1;
4931 /* Undefined? */
4932 if (TYPE_SIZE (t) == 0)
4933 return -1;
4935 /* Look up t in hash table. Only one of the compatible types within each
4936 alias set is recorded in the table. */
4937 if (!type_hash_table)
4938 type_hash_table = htab_create_ggc (1021, c_type_hash,
4939 (htab_eq) lang_hooks.types_compatible_p,
4940 NULL);
4941 slot = htab_find_slot (type_hash_table, t, INSERT);
4942 if (*slot != NULL)
4944 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4945 return TYPE_ALIAS_SET ((tree)*slot);
4947 else
4948 /* Our caller will assign and record (in t) a new alias set; all we need
4949 to do is remember t in the hash table. */
4950 *slot = t;
4952 return -1;
4955 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4956 the second parameter indicates which OPERATOR is being applied.
4957 The COMPLAIN flag controls whether we should diagnose possibly
4958 ill-formed constructs or not. LOC is the location of the SIZEOF or
4959 TYPEOF operator. */
4961 tree
4962 c_sizeof_or_alignof_type (location_t loc,
4963 tree type, bool is_sizeof, int complain)
4965 const char *op_name;
4966 tree value = NULL;
4967 enum tree_code type_code = TREE_CODE (type);
4969 op_name = is_sizeof ? "sizeof" : "__alignof__";
4971 if (type_code == FUNCTION_TYPE)
4973 if (is_sizeof)
4975 if (complain && warn_pointer_arith)
4976 pedwarn (loc, OPT_Wpointer_arith,
4977 "invalid application of %<sizeof%> to a function type");
4978 else if (!complain)
4979 return error_mark_node;
4980 value = size_one_node;
4982 else
4984 if (complain)
4986 if (c_dialect_cxx ())
4987 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4988 "%<alignof%> applied to a function type");
4989 else
4990 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4991 "%<_Alignof%> applied to a function type");
4993 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4996 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4998 if (complain)
5000 if (type_code == VOID_TYPE && upc_shared_type_p (type))
5001 error_at (loc, "invalid application of %qs"
5002 " to %<shared void%> type", op_name);
5003 else if (type_code == VOID_TYPE && warn_pointer_arith)
5004 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
5005 "invalid application of %qs to a void type", op_name);
5007 else
5008 return error_mark_node;
5009 value = size_one_node;
5011 else if (!COMPLETE_TYPE_P (type)
5012 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5014 if (complain)
5015 error_at (loc, "invalid application of %qs to incomplete type %qT",
5016 op_name, type);
5017 return error_mark_node;
5019 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5020 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5022 if (complain)
5023 error_at (loc, "invalid application of %qs to array type %qT of "
5024 "incomplete element type", op_name, type);
5025 return error_mark_node;
5027 else
5029 if (is_sizeof)
5030 /* Convert in case a char is more than one unit. */
5031 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5032 size_int (TYPE_PRECISION (char_type_node)
5033 / BITS_PER_UNIT));
5034 else
5035 value = size_int (TYPE_ALIGN_UNIT (type));
5038 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
5039 && upc_shared_type_p (type)
5040 && TYPE_HAS_THREADS_FACTOR (type))
5042 const tree n_threads = convert (sizetype, upc_num_threads ());
5043 value = size_binop (MULT_EXPR, value, n_threads);
5046 /* VALUE will have the middle-end integer type sizetype.
5047 However, we should really return a value of type `size_t',
5048 which is just a typedef for an ordinary integer type. */
5049 value = fold_convert_loc (loc, size_type_node, value);
5051 return value;
5054 /* Implement the __alignof keyword: Return the minimum required
5055 alignment of EXPR, measured in bytes. For VAR_DECLs,
5056 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5057 from an "aligned" __attribute__ specification). LOC is the
5058 location of the ALIGNOF operator. */
5060 tree
5061 c_alignof_expr (location_t loc, tree expr)
5063 tree t;
5065 if (VAR_OR_FUNCTION_DECL_P (expr))
5066 t = size_int (DECL_ALIGN_UNIT (expr));
5068 else if (TREE_CODE (expr) == COMPONENT_REF
5069 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5071 error_at (loc, "%<__alignof%> applied to a bit-field");
5072 t = size_one_node;
5074 else if (TREE_CODE (expr) == COMPONENT_REF
5075 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5076 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5078 else if (TREE_CODE (expr) == INDIRECT_REF)
5080 tree t = TREE_OPERAND (expr, 0);
5081 tree best = t;
5082 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5084 while (CONVERT_EXPR_P (t)
5085 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5087 int thisalign;
5089 t = TREE_OPERAND (t, 0);
5090 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5091 if (thisalign > bestalign)
5092 best = t, bestalign = thisalign;
5094 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5096 else
5097 return c_alignof (loc, TREE_TYPE (expr));
5099 return fold_convert_loc (loc, size_type_node, t);
5102 /* Handle C and C++ default attributes. */
5104 enum built_in_attribute
5106 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5107 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5108 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5109 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5110 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5111 #include "builtin-attrs.def"
5112 #undef DEF_ATTR_NULL_TREE
5113 #undef DEF_ATTR_INT
5114 #undef DEF_ATTR_STRING
5115 #undef DEF_ATTR_IDENT
5116 #undef DEF_ATTR_TREE_LIST
5117 ATTR_LAST
5120 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5122 static void c_init_attributes (void);
5124 enum c_builtin_type
5126 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5127 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5128 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5129 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5130 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5131 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5132 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5133 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5134 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5135 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5136 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5137 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5138 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5139 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5140 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5141 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5142 NAME,
5143 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5144 #include "builtin-types.def"
5145 #undef DEF_PRIMITIVE_TYPE
5146 #undef DEF_FUNCTION_TYPE_0
5147 #undef DEF_FUNCTION_TYPE_1
5148 #undef DEF_FUNCTION_TYPE_2
5149 #undef DEF_FUNCTION_TYPE_3
5150 #undef DEF_FUNCTION_TYPE_4
5151 #undef DEF_FUNCTION_TYPE_5
5152 #undef DEF_FUNCTION_TYPE_6
5153 #undef DEF_FUNCTION_TYPE_7
5154 #undef DEF_FUNCTION_TYPE_8
5155 #undef DEF_FUNCTION_TYPE_VAR_0
5156 #undef DEF_FUNCTION_TYPE_VAR_1
5157 #undef DEF_FUNCTION_TYPE_VAR_2
5158 #undef DEF_FUNCTION_TYPE_VAR_3
5159 #undef DEF_FUNCTION_TYPE_VAR_4
5160 #undef DEF_FUNCTION_TYPE_VAR_5
5161 #undef DEF_POINTER_TYPE
5162 BT_LAST
5165 typedef enum c_builtin_type builtin_type;
5167 /* A temporary array for c_common_nodes_and_builtins. Used in
5168 communication with def_fn_type. */
5169 static tree builtin_types[(int) BT_LAST + 1];
5171 /* A helper function for c_common_nodes_and_builtins. Build function type
5172 for DEF with return type RET and N arguments. If VAR is true, then the
5173 function should be variadic after those N arguments.
5175 Takes special care not to ICE if any of the types involved are
5176 error_mark_node, which indicates that said type is not in fact available
5177 (see builtin_type_for_size). In which case the function type as a whole
5178 should be error_mark_node. */
5180 static void
5181 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5183 tree t;
5184 tree *args = XALLOCAVEC (tree, n);
5185 va_list list;
5186 int i;
5188 va_start (list, n);
5189 for (i = 0; i < n; ++i)
5191 builtin_type a = (builtin_type) va_arg (list, int);
5192 t = builtin_types[a];
5193 if (t == error_mark_node)
5194 goto egress;
5195 args[i] = t;
5198 t = builtin_types[ret];
5199 if (t == error_mark_node)
5200 goto egress;
5201 if (var)
5202 t = build_varargs_function_type_array (t, n, args);
5203 else
5204 t = build_function_type_array (t, n, args);
5206 egress:
5207 builtin_types[def] = t;
5208 va_end (list);
5211 /* Build builtin functions common to both C and C++ language
5212 frontends. */
5214 static void
5215 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5217 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5218 builtin_types[ENUM] = VALUE;
5219 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5220 def_fn_type (ENUM, RETURN, 0, 0);
5221 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5222 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5223 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5224 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5225 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5226 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5227 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5228 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5229 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5230 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5231 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5232 ARG6) \
5233 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5234 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5235 ARG6, ARG7) \
5236 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5237 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5238 ARG6, ARG7, ARG8) \
5239 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5240 ARG7, ARG8);
5241 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5242 def_fn_type (ENUM, RETURN, 1, 0);
5243 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5244 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5245 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5246 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5247 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5248 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5249 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5250 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5251 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5252 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5253 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5254 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5256 #include "builtin-types.def"
5258 #undef DEF_PRIMITIVE_TYPE
5259 #undef DEF_FUNCTION_TYPE_1
5260 #undef DEF_FUNCTION_TYPE_2
5261 #undef DEF_FUNCTION_TYPE_3
5262 #undef DEF_FUNCTION_TYPE_4
5263 #undef DEF_FUNCTION_TYPE_5
5264 #undef DEF_FUNCTION_TYPE_6
5265 #undef DEF_FUNCTION_TYPE_VAR_0
5266 #undef DEF_FUNCTION_TYPE_VAR_1
5267 #undef DEF_FUNCTION_TYPE_VAR_2
5268 #undef DEF_FUNCTION_TYPE_VAR_3
5269 #undef DEF_FUNCTION_TYPE_VAR_4
5270 #undef DEF_FUNCTION_TYPE_VAR_5
5271 #undef DEF_POINTER_TYPE
5272 builtin_types[(int) BT_LAST] = NULL_TREE;
5274 c_init_attributes ();
5276 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5277 NONANSI_P, ATTRS, IMPLICIT, COND) \
5278 if (NAME && COND) \
5279 def_builtin_1 (ENUM, NAME, CLASS, \
5280 builtin_types[(int) TYPE], \
5281 builtin_types[(int) LIBTYPE], \
5282 BOTH_P, FALLBACK_P, NONANSI_P, \
5283 built_in_attributes[(int) ATTRS], IMPLICIT);
5284 #include "builtins.def"
5285 #undef DEF_BUILTIN
5287 targetm.init_builtins ();
5289 build_common_builtin_nodes ();
5291 if (flag_enable_cilkplus)
5292 cilk_init_builtins ();
5295 /* Like get_identifier, but avoid warnings about null arguments when
5296 the argument may be NULL for targets where GCC lacks stdint.h type
5297 information. */
5299 static inline tree
5300 c_get_ident (const char *id)
5302 return get_identifier (id);
5305 /* Build tree nodes and builtin functions common to both C and C++ language
5306 frontends. */
5308 void
5309 c_common_nodes_and_builtins (void)
5311 int char16_type_size;
5312 int char32_type_size;
5313 int wchar_type_size;
5314 tree array_domain_type;
5315 tree va_list_ref_type_node;
5316 tree va_list_arg_type_node;
5318 build_common_tree_nodes (flag_signed_char, flag_short_double);
5320 /* Define `int' and `char' first so that dbx will output them first. */
5321 record_builtin_type (RID_INT, NULL, integer_type_node);
5322 record_builtin_type (RID_CHAR, "char", char_type_node);
5324 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5325 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5326 but not C. Are the conditionals here needed? */
5327 if (c_dialect_cxx ())
5328 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5329 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5330 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5331 record_builtin_type (RID_MAX, "long unsigned int",
5332 long_unsigned_type_node);
5333 if (int128_integer_type_node != NULL_TREE)
5335 record_builtin_type (RID_INT128, "__int128",
5336 int128_integer_type_node);
5337 record_builtin_type (RID_MAX, "__int128 unsigned",
5338 int128_unsigned_type_node);
5340 if (c_dialect_cxx ())
5341 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5342 record_builtin_type (RID_MAX, "long long int",
5343 long_long_integer_type_node);
5344 record_builtin_type (RID_MAX, "long long unsigned int",
5345 long_long_unsigned_type_node);
5346 if (c_dialect_cxx ())
5347 record_builtin_type (RID_MAX, "long long unsigned",
5348 long_long_unsigned_type_node);
5349 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5350 record_builtin_type (RID_MAX, "short unsigned int",
5351 short_unsigned_type_node);
5352 if (c_dialect_cxx ())
5353 record_builtin_type (RID_MAX, "unsigned short",
5354 short_unsigned_type_node);
5356 /* Define both `signed char' and `unsigned char'. */
5357 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5358 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5360 /* These are types that c_common_type_for_size and
5361 c_common_type_for_mode use. */
5362 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5363 TYPE_DECL, NULL_TREE,
5364 intQI_type_node));
5365 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5366 TYPE_DECL, NULL_TREE,
5367 intHI_type_node));
5368 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5369 TYPE_DECL, NULL_TREE,
5370 intSI_type_node));
5371 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5372 TYPE_DECL, NULL_TREE,
5373 intDI_type_node));
5374 #if HOST_BITS_PER_WIDE_INT >= 64
5375 if (targetm.scalar_mode_supported_p (TImode))
5376 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5377 TYPE_DECL,
5378 get_identifier ("__int128_t"),
5379 intTI_type_node));
5380 #endif
5381 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5382 TYPE_DECL, NULL_TREE,
5383 unsigned_intQI_type_node));
5384 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5385 TYPE_DECL, NULL_TREE,
5386 unsigned_intHI_type_node));
5387 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5388 TYPE_DECL, NULL_TREE,
5389 unsigned_intSI_type_node));
5390 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5391 TYPE_DECL, NULL_TREE,
5392 unsigned_intDI_type_node));
5393 #if HOST_BITS_PER_WIDE_INT >= 64
5394 if (targetm.scalar_mode_supported_p (TImode))
5395 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5396 TYPE_DECL,
5397 get_identifier ("__uint128_t"),
5398 unsigned_intTI_type_node));
5399 #endif
5401 /* Create the widest literal types. */
5402 widest_integer_literal_type_node
5403 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5404 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5405 TYPE_DECL, NULL_TREE,
5406 widest_integer_literal_type_node));
5408 widest_unsigned_literal_type_node
5409 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5410 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5411 TYPE_DECL, NULL_TREE,
5412 widest_unsigned_literal_type_node));
5414 signed_size_type_node = c_common_signed_type (size_type_node);
5416 pid_type_node =
5417 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5419 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5420 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5421 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5423 /* Only supported decimal floating point extension if the target
5424 actually supports underlying modes. */
5425 if (targetm.scalar_mode_supported_p (SDmode)
5426 && targetm.scalar_mode_supported_p (DDmode)
5427 && targetm.scalar_mode_supported_p (TDmode))
5429 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5430 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5431 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5434 if (targetm.fixed_point_supported_p ())
5436 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5437 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5438 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5439 record_builtin_type (RID_MAX, "long long _Fract",
5440 long_long_fract_type_node);
5441 record_builtin_type (RID_MAX, "unsigned short _Fract",
5442 unsigned_short_fract_type_node);
5443 record_builtin_type (RID_MAX, "unsigned _Fract",
5444 unsigned_fract_type_node);
5445 record_builtin_type (RID_MAX, "unsigned long _Fract",
5446 unsigned_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5448 unsigned_long_long_fract_type_node);
5449 record_builtin_type (RID_MAX, "_Sat short _Fract",
5450 sat_short_fract_type_node);
5451 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5452 record_builtin_type (RID_MAX, "_Sat long _Fract",
5453 sat_long_fract_type_node);
5454 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5455 sat_long_long_fract_type_node);
5456 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5457 sat_unsigned_short_fract_type_node);
5458 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5459 sat_unsigned_fract_type_node);
5460 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5461 sat_unsigned_long_fract_type_node);
5462 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5463 sat_unsigned_long_long_fract_type_node);
5464 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5465 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5466 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5467 record_builtin_type (RID_MAX, "long long _Accum",
5468 long_long_accum_type_node);
5469 record_builtin_type (RID_MAX, "unsigned short _Accum",
5470 unsigned_short_accum_type_node);
5471 record_builtin_type (RID_MAX, "unsigned _Accum",
5472 unsigned_accum_type_node);
5473 record_builtin_type (RID_MAX, "unsigned long _Accum",
5474 unsigned_long_accum_type_node);
5475 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5476 unsigned_long_long_accum_type_node);
5477 record_builtin_type (RID_MAX, "_Sat short _Accum",
5478 sat_short_accum_type_node);
5479 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5480 record_builtin_type (RID_MAX, "_Sat long _Accum",
5481 sat_long_accum_type_node);
5482 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5483 sat_long_long_accum_type_node);
5484 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5485 sat_unsigned_short_accum_type_node);
5486 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5487 sat_unsigned_accum_type_node);
5488 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5489 sat_unsigned_long_accum_type_node);
5490 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5491 sat_unsigned_long_long_accum_type_node);
5495 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5496 TYPE_DECL,
5497 get_identifier ("complex int"),
5498 complex_integer_type_node));
5499 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5500 TYPE_DECL,
5501 get_identifier ("complex float"),
5502 complex_float_type_node));
5503 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5504 TYPE_DECL,
5505 get_identifier ("complex double"),
5506 complex_double_type_node));
5507 lang_hooks.decls.pushdecl
5508 (build_decl (UNKNOWN_LOCATION,
5509 TYPE_DECL, get_identifier ("complex long double"),
5510 complex_long_double_type_node));
5512 if (c_dialect_cxx ())
5513 /* For C++, make fileptr_type_node a distinct void * type until
5514 FILE type is defined. */
5515 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5517 record_builtin_type (RID_VOID, NULL, void_type_node);
5519 /* Set the TYPE_NAME for any variants that were built before
5520 record_builtin_type gave names to the built-in types. */
5522 tree void_name = TYPE_NAME (void_type_node);
5523 TYPE_NAME (void_type_node) = NULL_TREE;
5524 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5525 = void_name;
5526 TYPE_NAME (void_type_node) = void_name;
5529 /* This node must not be shared. */
5530 void_zero_node = make_node (INTEGER_CST);
5531 TREE_TYPE (void_zero_node) = void_type_node;
5533 void_list_node = build_void_list_node ();
5535 /* Make a type to be the domain of a few array types
5536 whose domains don't really matter.
5537 200 is small enough that it always fits in size_t
5538 and large enough that it can hold most function names for the
5539 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5540 array_domain_type = build_index_type (size_int (200));
5542 /* Make a type for arrays of characters.
5543 With luck nothing will ever really depend on the length of this
5544 array type. */
5545 char_array_type_node
5546 = build_array_type (char_type_node, array_domain_type);
5548 /* Likewise for arrays of ints. */
5549 int_array_type_node
5550 = build_array_type (integer_type_node, array_domain_type);
5552 string_type_node = build_pointer_type (char_type_node);
5553 const_string_type_node
5554 = build_pointer_type (build_qualified_type
5555 (char_type_node, TYPE_QUAL_CONST));
5557 /* This is special for C++ so functions can be overloaded. */
5558 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5559 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5560 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5561 underlying_wchar_type_node = wchar_type_node;
5562 if (c_dialect_cxx ())
5564 if (TYPE_UNSIGNED (wchar_type_node))
5565 wchar_type_node = make_unsigned_type (wchar_type_size);
5566 else
5567 wchar_type_node = make_signed_type (wchar_type_size);
5568 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5571 /* This is for wide string constants. */
5572 wchar_array_type_node
5573 = build_array_type (wchar_type_node, array_domain_type);
5575 /* Define 'char16_t'. */
5576 char16_type_node = get_identifier (CHAR16_TYPE);
5577 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5578 char16_type_size = TYPE_PRECISION (char16_type_node);
5579 if (c_dialect_cxx ())
5581 char16_type_node = make_unsigned_type (char16_type_size);
5583 if (cxx_dialect >= cxx11)
5584 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5587 /* This is for UTF-16 string constants. */
5588 char16_array_type_node
5589 = build_array_type (char16_type_node, array_domain_type);
5591 /* Define 'char32_t'. */
5592 char32_type_node = get_identifier (CHAR32_TYPE);
5593 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5594 char32_type_size = TYPE_PRECISION (char32_type_node);
5595 if (c_dialect_cxx ())
5597 char32_type_node = make_unsigned_type (char32_type_size);
5599 if (cxx_dialect >= cxx11)
5600 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5603 /* This is for UTF-32 string constants. */
5604 char32_array_type_node
5605 = build_array_type (char32_type_node, array_domain_type);
5607 wint_type_node =
5608 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5610 intmax_type_node =
5611 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5612 uintmax_type_node =
5613 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5615 if (SIG_ATOMIC_TYPE)
5616 sig_atomic_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5618 if (INT8_TYPE)
5619 int8_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5621 if (INT16_TYPE)
5622 int16_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5624 if (INT32_TYPE)
5625 int32_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5627 if (INT64_TYPE)
5628 int64_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5630 if (UINT8_TYPE)
5631 uint8_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5633 if (UINT16_TYPE)
5634 c_uint16_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5636 if (UINT32_TYPE)
5637 c_uint32_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5639 if (UINT64_TYPE)
5640 c_uint64_type_node =
5641 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5642 if (INT_LEAST8_TYPE)
5643 int_least8_type_node =
5644 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5645 if (INT_LEAST16_TYPE)
5646 int_least16_type_node =
5647 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5648 if (INT_LEAST32_TYPE)
5649 int_least32_type_node =
5650 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5651 if (INT_LEAST64_TYPE)
5652 int_least64_type_node =
5653 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5654 if (UINT_LEAST8_TYPE)
5655 uint_least8_type_node =
5656 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5657 if (UINT_LEAST16_TYPE)
5658 uint_least16_type_node =
5659 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5660 if (UINT_LEAST32_TYPE)
5661 uint_least32_type_node =
5662 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5663 if (UINT_LEAST64_TYPE)
5664 uint_least64_type_node =
5665 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5666 if (INT_FAST8_TYPE)
5667 int_fast8_type_node =
5668 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5669 if (INT_FAST16_TYPE)
5670 int_fast16_type_node =
5671 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5672 if (INT_FAST32_TYPE)
5673 int_fast32_type_node =
5674 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5675 if (INT_FAST64_TYPE)
5676 int_fast64_type_node =
5677 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5678 if (UINT_FAST8_TYPE)
5679 uint_fast8_type_node =
5680 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5681 if (UINT_FAST16_TYPE)
5682 uint_fast16_type_node =
5683 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5684 if (UINT_FAST32_TYPE)
5685 uint_fast32_type_node =
5686 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5687 if (UINT_FAST64_TYPE)
5688 uint_fast64_type_node =
5689 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5690 if (INTPTR_TYPE)
5691 intptr_type_node =
5692 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5693 if (UINTPTR_TYPE)
5694 uintptr_type_node =
5695 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5697 default_function_type
5698 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5699 ptrdiff_type_node
5700 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5701 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5703 lang_hooks.decls.pushdecl
5704 (build_decl (UNKNOWN_LOCATION,
5705 TYPE_DECL, get_identifier ("__builtin_va_list"),
5706 va_list_type_node));
5707 if (targetm.enum_va_list_p)
5709 int l;
5710 const char *pname;
5711 tree ptype;
5713 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5715 lang_hooks.decls.pushdecl
5716 (build_decl (UNKNOWN_LOCATION,
5717 TYPE_DECL, get_identifier (pname),
5718 ptype));
5723 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5725 va_list_arg_type_node = va_list_ref_type_node =
5726 build_pointer_type (TREE_TYPE (va_list_type_node));
5728 else
5730 va_list_arg_type_node = va_list_type_node;
5731 va_list_ref_type_node = build_reference_type (va_list_type_node);
5734 if (!flag_preprocess_only)
5735 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5737 main_identifier_node = get_identifier ("main");
5739 /* Create the built-in __null node. It is important that this is
5740 not shared. */
5741 null_node = make_node (INTEGER_CST);
5742 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5744 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5745 memset (builtin_types, 0, sizeof (builtin_types));
5748 /* The number of named compound-literals generated thus far. */
5749 static GTY(()) int compound_literal_number;
5751 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5753 void
5754 set_compound_literal_name (tree decl)
5756 char *name;
5757 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5758 compound_literal_number);
5759 compound_literal_number++;
5760 DECL_NAME (decl) = get_identifier (name);
5763 tree
5764 build_va_arg (location_t loc, tree expr, tree type)
5766 expr = build1 (VA_ARG_EXPR, type, expr);
5767 SET_EXPR_LOCATION (expr, loc);
5768 return expr;
5772 /* Linked list of disabled built-in functions. */
5774 typedef struct disabled_builtin
5776 const char *name;
5777 struct disabled_builtin *next;
5778 } disabled_builtin;
5779 static disabled_builtin *disabled_builtins = NULL;
5781 static bool builtin_function_disabled_p (const char *);
5783 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5784 begins with "__builtin_", give an error. */
5786 void
5787 disable_builtin_function (const char *name)
5789 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5790 error ("cannot disable built-in function %qs", name);
5791 else
5793 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5794 new_disabled_builtin->name = name;
5795 new_disabled_builtin->next = disabled_builtins;
5796 disabled_builtins = new_disabled_builtin;
5801 /* Return true if the built-in function NAME has been disabled, false
5802 otherwise. */
5804 static bool
5805 builtin_function_disabled_p (const char *name)
5807 disabled_builtin *p;
5808 for (p = disabled_builtins; p != NULL; p = p->next)
5810 if (strcmp (name, p->name) == 0)
5811 return true;
5813 return false;
5817 /* Worker for DEF_BUILTIN.
5818 Possibly define a builtin function with one or two names.
5819 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5820 nonansi_p and flag_no_nonansi_builtin. */
5822 static void
5823 def_builtin_1 (enum built_in_function fncode,
5824 const char *name,
5825 enum built_in_class fnclass,
5826 tree fntype, tree libtype,
5827 bool both_p, bool fallback_p, bool nonansi_p,
5828 tree fnattrs, bool implicit_p)
5830 tree decl;
5831 const char *libname;
5833 if (fntype == error_mark_node)
5834 return;
5836 gcc_assert ((!both_p && !fallback_p)
5837 || !strncmp (name, "__builtin_",
5838 strlen ("__builtin_")));
5840 libname = name + strlen ("__builtin_");
5841 decl = add_builtin_function (name, fntype, fncode, fnclass,
5842 (fallback_p ? libname : NULL),
5843 fnattrs);
5845 set_builtin_decl (fncode, decl, implicit_p);
5847 if (both_p
5848 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5849 && !(nonansi_p && flag_no_nonansi_builtin))
5850 add_builtin_function (libname, libtype, fncode, fnclass,
5851 NULL, fnattrs);
5854 /* Nonzero if the type T promotes to int. This is (nearly) the
5855 integral promotions defined in ISO C99 6.3.1.1/2. */
5857 bool
5858 c_promoting_integer_type_p (const_tree t)
5860 switch (TREE_CODE (t))
5862 case INTEGER_TYPE:
5863 return (TYPE_MAIN_VARIANT (t) == char_type_node
5864 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5865 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5866 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5867 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5868 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5870 case ENUMERAL_TYPE:
5871 /* ??? Technically all enumerations not larger than an int
5872 promote to an int. But this is used along code paths
5873 that only want to notice a size change. */
5874 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5876 case BOOLEAN_TYPE:
5877 return 1;
5879 default:
5880 return 0;
5884 /* Return 1 if PARMS specifies a fixed number of parameters
5885 and none of their types is affected by default promotions. */
5888 self_promoting_args_p (const_tree parms)
5890 const_tree t;
5891 for (t = parms; t; t = TREE_CHAIN (t))
5893 tree type = TREE_VALUE (t);
5895 if (type == error_mark_node)
5896 continue;
5898 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5899 return 0;
5901 if (type == 0)
5902 return 0;
5904 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5905 return 0;
5907 if (c_promoting_integer_type_p (type))
5908 return 0;
5910 return 1;
5913 /* Recursively remove any '*' or '&' operator from TYPE. */
5914 tree
5915 strip_pointer_operator (tree t)
5917 while (POINTER_TYPE_P (t))
5918 t = TREE_TYPE (t);
5919 return t;
5922 /* Recursively remove pointer or array type from TYPE. */
5923 tree
5924 strip_pointer_or_array_types (tree t)
5926 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5927 t = TREE_TYPE (t);
5928 return t;
5931 /* Used to compare case labels. K1 and K2 are actually tree nodes
5932 representing case labels, or NULL_TREE for a `default' label.
5933 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5934 K2, and 0 if K1 and K2 are equal. */
5937 case_compare (splay_tree_key k1, splay_tree_key k2)
5939 /* Consider a NULL key (such as arises with a `default' label) to be
5940 smaller than anything else. */
5941 if (!k1)
5942 return k2 ? -1 : 0;
5943 else if (!k2)
5944 return k1 ? 1 : 0;
5946 return tree_int_cst_compare ((tree) k1, (tree) k2);
5949 /* Process a case label, located at LOC, for the range LOW_VALUE
5950 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5951 then this case label is actually a `default' label. If only
5952 HIGH_VALUE is NULL_TREE, then case label was declared using the
5953 usual C/C++ syntax, rather than the GNU case range extension.
5954 CASES is a tree containing all the case ranges processed so far;
5955 COND is the condition for the switch-statement itself. Returns the
5956 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5957 is created. */
5959 tree
5960 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5961 tree low_value, tree high_value)
5963 tree type;
5964 tree label;
5965 tree case_label;
5966 splay_tree_node node;
5968 /* Create the LABEL_DECL itself. */
5969 label = create_artificial_label (loc);
5971 /* If there was an error processing the switch condition, bail now
5972 before we get more confused. */
5973 if (!cond || cond == error_mark_node)
5974 goto error_out;
5976 if ((low_value && TREE_TYPE (low_value)
5977 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5978 || (high_value && TREE_TYPE (high_value)
5979 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5981 error_at (loc, "pointers are not permitted as case values");
5982 goto error_out;
5985 /* Case ranges are a GNU extension. */
5986 if (high_value)
5987 pedwarn (loc, OPT_Wpedantic,
5988 "range expressions in switch statements are non-standard");
5990 type = TREE_TYPE (cond);
5991 if (low_value)
5993 low_value = check_case_value (low_value);
5994 low_value = convert_and_check (type, low_value);
5995 if (low_value == error_mark_node)
5996 goto error_out;
5998 if (high_value)
6000 high_value = check_case_value (high_value);
6001 high_value = convert_and_check (type, high_value);
6002 if (high_value == error_mark_node)
6003 goto error_out;
6006 if (low_value && high_value)
6008 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6009 really a case range, even though it was written that way.
6010 Remove the HIGH_VALUE to simplify later processing. */
6011 if (tree_int_cst_equal (low_value, high_value))
6012 high_value = NULL_TREE;
6013 else if (!tree_int_cst_lt (low_value, high_value))
6014 warning_at (loc, 0, "empty range specified");
6017 /* See if the case is in range of the type of the original testing
6018 expression. If both low_value and high_value are out of range,
6019 don't insert the case label and return NULL_TREE. */
6020 if (low_value
6021 && !check_case_bounds (type, orig_type,
6022 &low_value, high_value ? &high_value : NULL))
6023 return NULL_TREE;
6025 /* Look up the LOW_VALUE in the table of case labels we already
6026 have. */
6027 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6028 /* If there was not an exact match, check for overlapping ranges.
6029 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6030 that's a `default' label and the only overlap is an exact match. */
6031 if (!node && (low_value || high_value))
6033 splay_tree_node low_bound;
6034 splay_tree_node high_bound;
6036 /* Even though there wasn't an exact match, there might be an
6037 overlap between this case range and another case range.
6038 Since we've (inductively) not allowed any overlapping case
6039 ranges, we simply need to find the greatest low case label
6040 that is smaller that LOW_VALUE, and the smallest low case
6041 label that is greater than LOW_VALUE. If there is an overlap
6042 it will occur in one of these two ranges. */
6043 low_bound = splay_tree_predecessor (cases,
6044 (splay_tree_key) low_value);
6045 high_bound = splay_tree_successor (cases,
6046 (splay_tree_key) low_value);
6048 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6049 the LOW_VALUE, so there is no need to check unless the
6050 LOW_BOUND is in fact itself a case range. */
6051 if (low_bound
6052 && CASE_HIGH ((tree) low_bound->value)
6053 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6054 low_value) >= 0)
6055 node = low_bound;
6056 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6057 range is bigger than the low end of the current range, so we
6058 are only interested if the current range is a real range, and
6059 not an ordinary case label. */
6060 else if (high_bound
6061 && high_value
6062 && (tree_int_cst_compare ((tree) high_bound->key,
6063 high_value)
6064 <= 0))
6065 node = high_bound;
6067 /* If there was an overlap, issue an error. */
6068 if (node)
6070 tree duplicate = CASE_LABEL ((tree) node->value);
6072 if (high_value)
6074 error_at (loc, "duplicate (or overlapping) case value");
6075 error_at (DECL_SOURCE_LOCATION (duplicate),
6076 "this is the first entry overlapping that value");
6078 else if (low_value)
6080 error_at (loc, "duplicate case value") ;
6081 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6083 else
6085 error_at (loc, "multiple default labels in one switch");
6086 error_at (DECL_SOURCE_LOCATION (duplicate),
6087 "this is the first default label");
6089 goto error_out;
6092 /* Add a CASE_LABEL to the statement-tree. */
6093 case_label = add_stmt (build_case_label (low_value, high_value, label));
6094 /* Register this case label in the splay tree. */
6095 splay_tree_insert (cases,
6096 (splay_tree_key) low_value,
6097 (splay_tree_value) case_label);
6099 return case_label;
6101 error_out:
6102 /* Add a label so that the back-end doesn't think that the beginning of
6103 the switch is unreachable. Note that we do not add a case label, as
6104 that just leads to duplicates and thence to failure later on. */
6105 if (!cases->root)
6107 tree t = create_artificial_label (loc);
6108 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6110 return error_mark_node;
6113 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6114 Used to verify that case values match up with enumerator values. */
6116 static void
6117 match_case_to_enum_1 (tree key, tree type, tree label)
6119 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6121 /* ??? Not working too hard to print the double-word value.
6122 Should perhaps be done with %lwd in the diagnostic routines? */
6123 if (TREE_INT_CST_HIGH (key) == 0)
6124 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6125 TREE_INT_CST_LOW (key));
6126 else if (!TYPE_UNSIGNED (type)
6127 && TREE_INT_CST_HIGH (key) == -1
6128 && TREE_INT_CST_LOW (key) != 0)
6129 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6130 -TREE_INT_CST_LOW (key));
6131 else
6132 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6133 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6134 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6136 if (TYPE_NAME (type) == 0)
6137 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6138 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6139 "case value %qs not in enumerated type",
6140 buf);
6141 else
6142 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6143 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6144 "case value %qs not in enumerated type %qT",
6145 buf, type);
6148 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6149 Used to verify that case values match up with enumerator values. */
6151 static int
6152 match_case_to_enum (splay_tree_node node, void *data)
6154 tree label = (tree) node->value;
6155 tree type = (tree) data;
6157 /* Skip default case. */
6158 if (!CASE_LOW (label))
6159 return 0;
6161 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6162 when we did our enum->case scan. Reset our scratch bit after. */
6163 if (!CASE_LOW_SEEN (label))
6164 match_case_to_enum_1 (CASE_LOW (label), type, label);
6165 else
6166 CASE_LOW_SEEN (label) = 0;
6168 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6169 not set, that means that CASE_HIGH did not appear when we did our
6170 enum->case scan. Reset our scratch bit after. */
6171 if (CASE_HIGH (label))
6173 if (!CASE_HIGH_SEEN (label))
6174 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6175 else
6176 CASE_HIGH_SEEN (label) = 0;
6179 return 0;
6182 /* Handle -Wswitch*. Called from the front end after parsing the
6183 switch construct. */
6184 /* ??? Should probably be somewhere generic, since other languages
6185 besides C and C++ would want this. At the moment, however, C/C++
6186 are the only tree-ssa languages that support enumerations at all,
6187 so the point is moot. */
6189 void
6190 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6191 tree type, tree cond)
6193 splay_tree_node default_node;
6194 splay_tree_node node;
6195 tree chain;
6197 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6198 return;
6200 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6201 if (!default_node)
6202 warning_at (switch_location, OPT_Wswitch_default,
6203 "switch missing default case");
6205 /* From here on, we only care about about enumerated types. */
6206 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6207 return;
6209 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6210 if (!warn_switch_enum && !warn_switch)
6211 return;
6213 /* Check the cases. Warn about case values which are not members of
6214 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6215 there is no default case, check that exactly all enumeration
6216 literals are covered by the cases. */
6218 /* Clearing COND if it is not an integer constant simplifies
6219 the tests inside the loop below. */
6220 if (TREE_CODE (cond) != INTEGER_CST)
6221 cond = NULL_TREE;
6223 /* The time complexity here is O(N*lg(N)) worst case, but for the
6224 common case of monotonically increasing enumerators, it is
6225 O(N), since the nature of the splay tree will keep the next
6226 element adjacent to the root at all times. */
6228 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6230 tree value = TREE_VALUE (chain);
6231 if (TREE_CODE (value) == CONST_DECL)
6232 value = DECL_INITIAL (value);
6233 node = splay_tree_lookup (cases, (splay_tree_key) value);
6234 if (node)
6236 /* Mark the CASE_LOW part of the case entry as seen. */
6237 tree label = (tree) node->value;
6238 CASE_LOW_SEEN (label) = 1;
6239 continue;
6242 /* Even though there wasn't an exact match, there might be a
6243 case range which includes the enumerator's value. */
6244 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6245 if (node && CASE_HIGH ((tree) node->value))
6247 tree label = (tree) node->value;
6248 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6249 if (cmp >= 0)
6251 /* If we match the upper bound exactly, mark the CASE_HIGH
6252 part of the case entry as seen. */
6253 if (cmp == 0)
6254 CASE_HIGH_SEEN (label) = 1;
6255 continue;
6259 /* We've now determined that this enumerated literal isn't
6260 handled by the case labels of the switch statement. */
6262 /* If the switch expression is a constant, we only really care
6263 about whether that constant is handled by the switch. */
6264 if (cond && tree_int_cst_compare (cond, value))
6265 continue;
6267 /* If there is a default_node, the only relevant option is
6268 Wswitch-enum. Otherwise, if both are enabled then we prefer
6269 to warn using -Wswitch because -Wswitch is enabled by -Wall
6270 while -Wswitch-enum is explicit. */
6271 warning_at (switch_location,
6272 (default_node || !warn_switch
6273 ? OPT_Wswitch_enum
6274 : OPT_Wswitch),
6275 "enumeration value %qE not handled in switch",
6276 TREE_PURPOSE (chain));
6279 /* Warn if there are case expressions that don't correspond to
6280 enumerators. This can occur since C and C++ don't enforce
6281 type-checking of assignments to enumeration variables.
6283 The time complexity here is now always O(N) worst case, since
6284 we should have marked both the lower bound and upper bound of
6285 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6286 above. This scan also resets those fields. */
6288 splay_tree_foreach (cases, match_case_to_enum, type);
6291 /* Finish an expression taking the address of LABEL (an
6292 IDENTIFIER_NODE). Returns an expression for the address.
6294 LOC is the location for the expression returned. */
6296 tree
6297 finish_label_address_expr (tree label, location_t loc)
6299 tree result;
6301 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6303 if (label == error_mark_node)
6304 return error_mark_node;
6306 label = lookup_label (label);
6307 if (label == NULL_TREE)
6308 result = null_pointer_node;
6309 else
6311 TREE_USED (label) = 1;
6312 result = build1 (ADDR_EXPR, ptr_type_node, label);
6313 /* The current function is not necessarily uninlinable.
6314 Computed gotos are incompatible with inlining, but the value
6315 here could be used only in a diagnostic, for example. */
6316 protected_set_expr_location (result, loc);
6319 return result;
6323 /* Given a boolean expression ARG, return a tree representing an increment
6324 or decrement (as indicated by CODE) of ARG. The front end must check for
6325 invalid cases (e.g., decrement in C++). */
6326 tree
6327 boolean_increment (enum tree_code code, tree arg)
6329 tree val;
6330 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6332 arg = stabilize_reference (arg);
6333 switch (code)
6335 case PREINCREMENT_EXPR:
6336 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6337 break;
6338 case POSTINCREMENT_EXPR:
6339 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6340 arg = save_expr (arg);
6341 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6342 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6343 break;
6344 case PREDECREMENT_EXPR:
6345 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6346 invert_truthvalue_loc (input_location, arg));
6347 break;
6348 case POSTDECREMENT_EXPR:
6349 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6350 invert_truthvalue_loc (input_location, arg));
6351 arg = save_expr (arg);
6352 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6353 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6354 break;
6355 default:
6356 gcc_unreachable ();
6358 TREE_SIDE_EFFECTS (val) = 1;
6359 return val;
6362 /* Built-in macros for stddef.h and stdint.h, that require macros
6363 defined in this file. */
6364 void
6365 c_stddef_cpp_builtins(void)
6367 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6368 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6369 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6370 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6371 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6372 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6373 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6374 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6375 if (SIG_ATOMIC_TYPE)
6376 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6377 if (INT8_TYPE)
6378 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6379 if (INT16_TYPE)
6380 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6381 if (INT32_TYPE)
6382 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6383 if (INT64_TYPE)
6384 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6385 if (UINT8_TYPE)
6386 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6387 if (UINT16_TYPE)
6388 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6389 if (UINT32_TYPE)
6390 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6391 if (UINT64_TYPE)
6392 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6393 if (INT_LEAST8_TYPE)
6394 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6395 if (INT_LEAST16_TYPE)
6396 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6397 if (INT_LEAST32_TYPE)
6398 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6399 if (INT_LEAST64_TYPE)
6400 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6401 if (UINT_LEAST8_TYPE)
6402 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6403 if (UINT_LEAST16_TYPE)
6404 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6405 if (UINT_LEAST32_TYPE)
6406 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6407 if (UINT_LEAST64_TYPE)
6408 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6409 if (INT_FAST8_TYPE)
6410 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6411 if (INT_FAST16_TYPE)
6412 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6413 if (INT_FAST32_TYPE)
6414 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6415 if (INT_FAST64_TYPE)
6416 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6417 if (UINT_FAST8_TYPE)
6418 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6419 if (UINT_FAST16_TYPE)
6420 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6421 if (UINT_FAST32_TYPE)
6422 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6423 if (UINT_FAST64_TYPE)
6424 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6425 if (INTPTR_TYPE)
6426 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6427 if (UINTPTR_TYPE)
6428 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6431 static void
6432 c_init_attributes (void)
6434 /* Fill in the built_in_attributes array. */
6435 #define DEF_ATTR_NULL_TREE(ENUM) \
6436 built_in_attributes[(int) ENUM] = NULL_TREE;
6437 #define DEF_ATTR_INT(ENUM, VALUE) \
6438 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6439 #define DEF_ATTR_STRING(ENUM, VALUE) \
6440 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6441 #define DEF_ATTR_IDENT(ENUM, STRING) \
6442 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6443 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6444 built_in_attributes[(int) ENUM] \
6445 = tree_cons (built_in_attributes[(int) PURPOSE], \
6446 built_in_attributes[(int) VALUE], \
6447 built_in_attributes[(int) CHAIN]);
6448 #include "builtin-attrs.def"
6449 #undef DEF_ATTR_NULL_TREE
6450 #undef DEF_ATTR_INT
6451 #undef DEF_ATTR_IDENT
6452 #undef DEF_ATTR_TREE_LIST
6455 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6456 identifier as an argument, so the front end shouldn't look it up. */
6458 bool
6459 attribute_takes_identifier_p (const_tree attr_id)
6461 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6462 if (spec == NULL)
6463 /* Unknown attribute that we'll end up ignoring, return true so we
6464 don't complain about an identifier argument. */
6465 return true;
6466 else if (!strcmp ("mode", spec->name)
6467 || !strcmp ("format", spec->name)
6468 || !strcmp ("cleanup", spec->name))
6469 return true;
6470 else
6471 return targetm.attribute_takes_identifier_p (attr_id);
6474 /* Attribute handlers common to C front ends. */
6476 /* Handle a "packed" attribute; arguments as in
6477 struct attribute_spec.handler. */
6479 static tree
6480 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6481 int flags, bool *no_add_attrs)
6483 if (TYPE_P (*node))
6485 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6486 *node = build_variant_type_copy (*node);
6487 TYPE_PACKED (*node) = 1;
6489 else if (TREE_CODE (*node) == FIELD_DECL)
6491 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6492 /* Still pack bitfields. */
6493 && ! DECL_INITIAL (*node))
6494 warning (OPT_Wattributes,
6495 "%qE attribute ignored for field of type %qT",
6496 name, TREE_TYPE (*node));
6497 else
6498 DECL_PACKED (*node) = 1;
6500 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6501 used for DECL_REGISTER. It wouldn't mean anything anyway.
6502 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6503 that changes what the typedef is typing. */
6504 else
6506 warning (OPT_Wattributes, "%qE attribute ignored", name);
6507 *no_add_attrs = true;
6510 return NULL_TREE;
6513 /* Handle a "nocommon" attribute; arguments as in
6514 struct attribute_spec.handler. */
6516 static tree
6517 handle_nocommon_attribute (tree *node, tree name,
6518 tree ARG_UNUSED (args),
6519 int ARG_UNUSED (flags), bool *no_add_attrs)
6521 if (TREE_CODE (*node) == VAR_DECL)
6522 DECL_COMMON (*node) = 0;
6523 else
6525 warning (OPT_Wattributes, "%qE attribute ignored", name);
6526 *no_add_attrs = true;
6529 return NULL_TREE;
6532 /* Handle a "common" attribute; arguments as in
6533 struct attribute_spec.handler. */
6535 static tree
6536 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6537 int ARG_UNUSED (flags), bool *no_add_attrs)
6539 if (TREE_CODE (*node) == VAR_DECL)
6540 DECL_COMMON (*node) = 1;
6541 else
6543 warning (OPT_Wattributes, "%qE attribute ignored", name);
6544 *no_add_attrs = true;
6547 return NULL_TREE;
6550 /* Handle a "noreturn" attribute; arguments as in
6551 struct attribute_spec.handler. */
6553 static tree
6554 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6555 int ARG_UNUSED (flags), bool *no_add_attrs)
6557 tree type = TREE_TYPE (*node);
6559 /* See FIXME comment in c_common_attribute_table. */
6560 if (TREE_CODE (*node) == FUNCTION_DECL
6561 || objc_method_decl (TREE_CODE (*node)))
6562 TREE_THIS_VOLATILE (*node) = 1;
6563 else if (TREE_CODE (type) == POINTER_TYPE
6564 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6565 TREE_TYPE (*node)
6566 = build_pointer_type
6567 (build_type_variant (TREE_TYPE (type),
6568 TYPE_READONLY (TREE_TYPE (type)), 1));
6569 else
6571 warning (OPT_Wattributes, "%qE attribute ignored", name);
6572 *no_add_attrs = true;
6575 return NULL_TREE;
6578 /* Handle a "hot" and attribute; arguments as in
6579 struct attribute_spec.handler. */
6581 static tree
6582 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6583 int ARG_UNUSED (flags), bool *no_add_attrs)
6585 if (TREE_CODE (*node) == FUNCTION_DECL
6586 || TREE_CODE (*node) == LABEL_DECL)
6588 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6590 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6591 name, "cold");
6592 *no_add_attrs = true;
6594 /* Most of the rest of the hot processing is done later with
6595 lookup_attribute. */
6597 else
6599 warning (OPT_Wattributes, "%qE attribute ignored", name);
6600 *no_add_attrs = true;
6603 return NULL_TREE;
6606 /* Handle a "cold" and attribute; arguments as in
6607 struct attribute_spec.handler. */
6609 static tree
6610 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6611 int ARG_UNUSED (flags), bool *no_add_attrs)
6613 if (TREE_CODE (*node) == FUNCTION_DECL
6614 || TREE_CODE (*node) == LABEL_DECL)
6616 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6618 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6619 name, "hot");
6620 *no_add_attrs = true;
6622 /* Most of the rest of the cold processing is done later with
6623 lookup_attribute. */
6625 else
6627 warning (OPT_Wattributes, "%qE attribute ignored", name);
6628 *no_add_attrs = true;
6631 return NULL_TREE;
6634 /* Handle a "no_sanitize_address" attribute; arguments as in
6635 struct attribute_spec.handler. */
6637 static tree
6638 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6639 bool *no_add_attrs)
6641 if (TREE_CODE (*node) != FUNCTION_DECL)
6643 warning (OPT_Wattributes, "%qE attribute ignored", name);
6644 *no_add_attrs = true;
6647 return NULL_TREE;
6650 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6651 struct attribute_spec.handler. */
6653 static tree
6654 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6655 bool *no_add_attrs)
6657 if (TREE_CODE (*node) != FUNCTION_DECL)
6658 warning (OPT_Wattributes, "%qE attribute ignored", name);
6659 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6660 DECL_ATTRIBUTES (*node)
6661 = tree_cons (get_identifier ("no_sanitize_address"),
6662 NULL_TREE, DECL_ATTRIBUTES (*node));
6663 *no_add_attrs = true;
6664 return NULL_TREE;
6667 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6668 struct attribute_spec.handler. */
6670 static tree
6671 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6672 bool *no_add_attrs)
6674 if (TREE_CODE (*node) != FUNCTION_DECL)
6676 warning (OPT_Wattributes, "%qE attribute ignored", name);
6677 *no_add_attrs = true;
6680 return NULL_TREE;
6683 /* Handle a "noinline" attribute; arguments as in
6684 struct attribute_spec.handler. */
6686 static tree
6687 handle_noinline_attribute (tree *node, tree name,
6688 tree ARG_UNUSED (args),
6689 int ARG_UNUSED (flags), bool *no_add_attrs)
6691 if (TREE_CODE (*node) == FUNCTION_DECL)
6692 DECL_UNINLINABLE (*node) = 1;
6693 else
6695 warning (OPT_Wattributes, "%qE attribute ignored", name);
6696 *no_add_attrs = true;
6699 return NULL_TREE;
6702 /* Handle a "noclone" attribute; arguments as in
6703 struct attribute_spec.handler. */
6705 static tree
6706 handle_noclone_attribute (tree *node, tree name,
6707 tree ARG_UNUSED (args),
6708 int ARG_UNUSED (flags), bool *no_add_attrs)
6710 if (TREE_CODE (*node) != FUNCTION_DECL)
6712 warning (OPT_Wattributes, "%qE attribute ignored", name);
6713 *no_add_attrs = true;
6716 return NULL_TREE;
6719 /* Handle a "always_inline" attribute; arguments as in
6720 struct attribute_spec.handler. */
6722 static tree
6723 handle_always_inline_attribute (tree *node, tree name,
6724 tree ARG_UNUSED (args),
6725 int ARG_UNUSED (flags),
6726 bool *no_add_attrs)
6728 if (TREE_CODE (*node) == FUNCTION_DECL)
6730 /* Set the attribute and mark it for disregarding inline
6731 limits. */
6732 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6734 else
6736 warning (OPT_Wattributes, "%qE attribute ignored", name);
6737 *no_add_attrs = true;
6740 return NULL_TREE;
6743 /* Handle a "gnu_inline" attribute; arguments as in
6744 struct attribute_spec.handler. */
6746 static tree
6747 handle_gnu_inline_attribute (tree *node, tree name,
6748 tree ARG_UNUSED (args),
6749 int ARG_UNUSED (flags),
6750 bool *no_add_attrs)
6752 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6754 /* Do nothing else, just set the attribute. We'll get at
6755 it later with lookup_attribute. */
6757 else
6759 warning (OPT_Wattributes, "%qE attribute ignored", name);
6760 *no_add_attrs = true;
6763 return NULL_TREE;
6766 /* Handle a "leaf" attribute; arguments as in
6767 struct attribute_spec.handler. */
6769 static tree
6770 handle_leaf_attribute (tree *node, tree name,
6771 tree ARG_UNUSED (args),
6772 int ARG_UNUSED (flags), bool *no_add_attrs)
6774 if (TREE_CODE (*node) != FUNCTION_DECL)
6776 warning (OPT_Wattributes, "%qE attribute ignored", name);
6777 *no_add_attrs = true;
6779 if (!TREE_PUBLIC (*node))
6781 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6782 *no_add_attrs = true;
6785 return NULL_TREE;
6788 /* Handle an "artificial" attribute; arguments as in
6789 struct attribute_spec.handler. */
6791 static tree
6792 handle_artificial_attribute (tree *node, tree name,
6793 tree ARG_UNUSED (args),
6794 int ARG_UNUSED (flags),
6795 bool *no_add_attrs)
6797 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6799 /* Do nothing else, just set the attribute. We'll get at
6800 it later with lookup_attribute. */
6802 else
6804 warning (OPT_Wattributes, "%qE attribute ignored", name);
6805 *no_add_attrs = true;
6808 return NULL_TREE;
6811 /* Handle a "flatten" attribute; arguments as in
6812 struct attribute_spec.handler. */
6814 static tree
6815 handle_flatten_attribute (tree *node, tree name,
6816 tree args ATTRIBUTE_UNUSED,
6817 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6819 if (TREE_CODE (*node) == FUNCTION_DECL)
6820 /* Do nothing else, just set the attribute. We'll get at
6821 it later with lookup_attribute. */
6823 else
6825 warning (OPT_Wattributes, "%qE attribute ignored", name);
6826 *no_add_attrs = true;
6829 return NULL_TREE;
6832 /* Handle a "warning" or "error" attribute; arguments as in
6833 struct attribute_spec.handler. */
6835 static tree
6836 handle_error_attribute (tree *node, tree name, tree args,
6837 int ARG_UNUSED (flags), bool *no_add_attrs)
6839 if (TREE_CODE (*node) == FUNCTION_DECL
6840 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6841 /* Do nothing else, just set the attribute. We'll get at
6842 it later with lookup_attribute. */
6844 else
6846 warning (OPT_Wattributes, "%qE attribute ignored", name);
6847 *no_add_attrs = true;
6850 return NULL_TREE;
6853 /* Handle a "used" attribute; arguments as in
6854 struct attribute_spec.handler. */
6856 static tree
6857 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6858 int ARG_UNUSED (flags), bool *no_add_attrs)
6860 tree node = *pnode;
6862 if (TREE_CODE (node) == FUNCTION_DECL
6863 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6864 || (TREE_CODE (node) == TYPE_DECL))
6866 TREE_USED (node) = 1;
6867 DECL_PRESERVE_P (node) = 1;
6868 if (TREE_CODE (node) == VAR_DECL)
6869 DECL_READ_P (node) = 1;
6871 else
6873 warning (OPT_Wattributes, "%qE attribute ignored", name);
6874 *no_add_attrs = true;
6877 return NULL_TREE;
6880 /* Handle a "unused" attribute; arguments as in
6881 struct attribute_spec.handler. */
6883 static tree
6884 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6885 int flags, bool *no_add_attrs)
6887 if (DECL_P (*node))
6889 tree decl = *node;
6891 if (TREE_CODE (decl) == PARM_DECL
6892 || TREE_CODE (decl) == VAR_DECL
6893 || TREE_CODE (decl) == FUNCTION_DECL
6894 || TREE_CODE (decl) == LABEL_DECL
6895 || TREE_CODE (decl) == TYPE_DECL)
6897 TREE_USED (decl) = 1;
6898 if (TREE_CODE (decl) == VAR_DECL
6899 || TREE_CODE (decl) == PARM_DECL)
6900 DECL_READ_P (decl) = 1;
6902 else
6904 warning (OPT_Wattributes, "%qE attribute ignored", name);
6905 *no_add_attrs = true;
6908 else
6910 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6911 *node = build_variant_type_copy (*node);
6912 TREE_USED (*node) = 1;
6915 return NULL_TREE;
6918 /* Handle a "externally_visible" attribute; arguments as in
6919 struct attribute_spec.handler. */
6921 static tree
6922 handle_externally_visible_attribute (tree *pnode, tree name,
6923 tree ARG_UNUSED (args),
6924 int ARG_UNUSED (flags),
6925 bool *no_add_attrs)
6927 tree node = *pnode;
6929 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6931 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6932 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6934 warning (OPT_Wattributes,
6935 "%qE attribute have effect only on public objects", name);
6936 *no_add_attrs = true;
6939 else
6941 warning (OPT_Wattributes, "%qE attribute ignored", name);
6942 *no_add_attrs = true;
6945 return NULL_TREE;
6948 /* Handle a "const" attribute; arguments as in
6949 struct attribute_spec.handler. */
6951 static tree
6952 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6953 int ARG_UNUSED (flags), bool *no_add_attrs)
6955 tree type = TREE_TYPE (*node);
6957 /* See FIXME comment on noreturn in c_common_attribute_table. */
6958 if (TREE_CODE (*node) == FUNCTION_DECL)
6959 TREE_READONLY (*node) = 1;
6960 else if (TREE_CODE (type) == POINTER_TYPE
6961 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6962 TREE_TYPE (*node)
6963 = build_pointer_type
6964 (build_type_variant (TREE_TYPE (type), 1,
6965 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6966 else
6968 warning (OPT_Wattributes, "%qE attribute ignored", name);
6969 *no_add_attrs = true;
6972 return NULL_TREE;
6975 /* Handle a "transparent_union" attribute; arguments as in
6976 struct attribute_spec.handler. */
6978 static tree
6979 handle_transparent_union_attribute (tree *node, tree name,
6980 tree ARG_UNUSED (args), int flags,
6981 bool *no_add_attrs)
6983 tree type;
6985 *no_add_attrs = true;
6988 if (TREE_CODE (*node) == TYPE_DECL
6989 && ! (flags & ATTR_FLAG_CXX11))
6990 node = &TREE_TYPE (*node);
6991 type = *node;
6993 if (TREE_CODE (type) == UNION_TYPE)
6995 /* Make sure that the first field will work for a transparent union.
6996 If the type isn't complete yet, leave the check to the code in
6997 finish_struct. */
6998 if (TYPE_SIZE (type))
7000 tree first = first_field (type);
7001 if (first == NULL_TREE
7002 || DECL_ARTIFICIAL (first)
7003 || TYPE_MODE (type) != DECL_MODE (first))
7004 goto ignored;
7007 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7009 /* If the type isn't complete yet, setting the flag
7010 on a variant wouldn't ever be checked. */
7011 if (!TYPE_SIZE (type))
7012 goto ignored;
7014 /* build_duplicate_type doesn't work for C++. */
7015 if (c_dialect_cxx ())
7016 goto ignored;
7018 /* A type variant isn't good enough, since we don't a cast
7019 to such a type removed as a no-op. */
7020 *node = type = build_duplicate_type (type);
7023 TYPE_TRANSPARENT_AGGR (type) = 1;
7024 return NULL_TREE;
7027 ignored:
7028 warning (OPT_Wattributes, "%qE attribute ignored", name);
7029 return NULL_TREE;
7032 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7033 get the requested priority for a constructor or destructor,
7034 possibly issuing diagnostics for invalid or reserved
7035 priorities. */
7037 static priority_type
7038 get_priority (tree args, bool is_destructor)
7040 HOST_WIDE_INT pri;
7041 tree arg;
7043 if (!args)
7044 return DEFAULT_INIT_PRIORITY;
7046 if (!SUPPORTS_INIT_PRIORITY)
7048 if (is_destructor)
7049 error ("destructor priorities are not supported");
7050 else
7051 error ("constructor priorities are not supported");
7052 return DEFAULT_INIT_PRIORITY;
7055 arg = TREE_VALUE (args);
7056 arg = default_conversion (arg);
7057 if (!host_integerp (arg, /*pos=*/0)
7058 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7059 goto invalid;
7061 pri = tree_low_cst (arg, /*pos=*/0);
7062 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7063 goto invalid;
7065 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7067 if (is_destructor)
7068 warning (0,
7069 "destructor priorities from 0 to %d are reserved "
7070 "for the implementation",
7071 MAX_RESERVED_INIT_PRIORITY);
7072 else
7073 warning (0,
7074 "constructor priorities from 0 to %d are reserved "
7075 "for the implementation",
7076 MAX_RESERVED_INIT_PRIORITY);
7078 return pri;
7080 invalid:
7081 if (is_destructor)
7082 error ("destructor priorities must be integers from 0 to %d inclusive",
7083 MAX_INIT_PRIORITY);
7084 else
7085 error ("constructor priorities must be integers from 0 to %d inclusive",
7086 MAX_INIT_PRIORITY);
7087 return DEFAULT_INIT_PRIORITY;
7090 /* Handle a "constructor" attribute; arguments as in
7091 struct attribute_spec.handler. */
7093 static tree
7094 handle_constructor_attribute (tree *node, tree name, tree args,
7095 int ARG_UNUSED (flags),
7096 bool *no_add_attrs)
7098 tree decl = *node;
7099 tree type = TREE_TYPE (decl);
7101 if (TREE_CODE (decl) == FUNCTION_DECL
7102 && TREE_CODE (type) == FUNCTION_TYPE
7103 && decl_function_context (decl) == 0)
7105 priority_type priority;
7106 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7107 priority = get_priority (args, /*is_destructor=*/false);
7108 SET_DECL_INIT_PRIORITY (decl, priority);
7109 TREE_USED (decl) = 1;
7111 else
7113 warning (OPT_Wattributes, "%qE attribute ignored", name);
7114 *no_add_attrs = true;
7117 return NULL_TREE;
7120 /* Handle a "destructor" attribute; arguments as in
7121 struct attribute_spec.handler. */
7123 static tree
7124 handle_destructor_attribute (tree *node, tree name, tree args,
7125 int ARG_UNUSED (flags),
7126 bool *no_add_attrs)
7128 tree decl = *node;
7129 tree type = TREE_TYPE (decl);
7131 if (TREE_CODE (decl) == FUNCTION_DECL
7132 && TREE_CODE (type) == FUNCTION_TYPE
7133 && decl_function_context (decl) == 0)
7135 priority_type priority;
7136 DECL_STATIC_DESTRUCTOR (decl) = 1;
7137 priority = get_priority (args, /*is_destructor=*/true);
7138 SET_DECL_FINI_PRIORITY (decl, priority);
7139 TREE_USED (decl) = 1;
7141 else
7143 warning (OPT_Wattributes, "%qE attribute ignored", name);
7144 *no_add_attrs = true;
7147 return NULL_TREE;
7150 /* Nonzero if the mode is a valid vector mode for this architecture.
7151 This returns nonzero even if there is no hardware support for the
7152 vector mode, but we can emulate with narrower modes. */
7154 static int
7155 vector_mode_valid_p (enum machine_mode mode)
7157 enum mode_class mclass = GET_MODE_CLASS (mode);
7158 enum machine_mode innermode;
7160 /* Doh! What's going on? */
7161 if (mclass != MODE_VECTOR_INT
7162 && mclass != MODE_VECTOR_FLOAT
7163 && mclass != MODE_VECTOR_FRACT
7164 && mclass != MODE_VECTOR_UFRACT
7165 && mclass != MODE_VECTOR_ACCUM
7166 && mclass != MODE_VECTOR_UACCUM)
7167 return 0;
7169 /* Hardware support. Woo hoo! */
7170 if (targetm.vector_mode_supported_p (mode))
7171 return 1;
7173 innermode = GET_MODE_INNER (mode);
7175 /* We should probably return 1 if requesting V4DI and we have no DI,
7176 but we have V2DI, but this is probably very unlikely. */
7178 /* If we have support for the inner mode, we can safely emulate it.
7179 We may not have V2DI, but me can emulate with a pair of DIs. */
7180 return targetm.scalar_mode_supported_p (innermode);
7184 /* Handle a "mode" attribute; arguments as in
7185 struct attribute_spec.handler. */
7187 static tree
7188 handle_mode_attribute (tree *node, tree name, tree args,
7189 int ARG_UNUSED (flags), bool *no_add_attrs)
7191 tree type = *node;
7192 tree ident = TREE_VALUE (args);
7194 *no_add_attrs = true;
7196 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7197 warning (OPT_Wattributes, "%qE attribute ignored", name);
7198 else
7200 int j;
7201 const char *p = IDENTIFIER_POINTER (ident);
7202 int len = strlen (p);
7203 enum machine_mode mode = VOIDmode;
7204 tree typefm;
7205 bool valid_mode;
7207 if (len > 4 && p[0] == '_' && p[1] == '_'
7208 && p[len - 1] == '_' && p[len - 2] == '_')
7210 char *newp = (char *) alloca (len - 1);
7212 strcpy (newp, &p[2]);
7213 newp[len - 4] = '\0';
7214 p = newp;
7217 /* Change this type to have a type with the specified mode.
7218 First check for the special modes. */
7219 if (!strcmp (p, "byte"))
7220 mode = byte_mode;
7221 else if (!strcmp (p, "word"))
7222 mode = word_mode;
7223 else if (!strcmp (p, "pointer"))
7224 mode = ptr_mode;
7225 else if (!strcmp (p, "libgcc_cmp_return"))
7226 mode = targetm.libgcc_cmp_return_mode ();
7227 else if (!strcmp (p, "libgcc_shift_count"))
7228 mode = targetm.libgcc_shift_count_mode ();
7229 else if (!strcmp (p, "unwind_word"))
7230 mode = targetm.unwind_word_mode ();
7231 else
7232 for (j = 0; j < NUM_MACHINE_MODES; j++)
7233 if (!strcmp (p, GET_MODE_NAME (j)))
7235 mode = (enum machine_mode) j;
7236 break;
7239 if (mode == VOIDmode)
7241 error ("unknown machine mode %qE", ident);
7242 return NULL_TREE;
7245 valid_mode = false;
7246 switch (GET_MODE_CLASS (mode))
7248 case MODE_INT:
7249 case MODE_PARTIAL_INT:
7250 case MODE_FLOAT:
7251 case MODE_DECIMAL_FLOAT:
7252 case MODE_FRACT:
7253 case MODE_UFRACT:
7254 case MODE_ACCUM:
7255 case MODE_UACCUM:
7256 valid_mode = targetm.scalar_mode_supported_p (mode);
7257 break;
7259 case MODE_COMPLEX_INT:
7260 case MODE_COMPLEX_FLOAT:
7261 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7262 break;
7264 case MODE_VECTOR_INT:
7265 case MODE_VECTOR_FLOAT:
7266 case MODE_VECTOR_FRACT:
7267 case MODE_VECTOR_UFRACT:
7268 case MODE_VECTOR_ACCUM:
7269 case MODE_VECTOR_UACCUM:
7270 warning (OPT_Wattributes, "specifying vector types with "
7271 "__attribute__ ((mode)) is deprecated");
7272 warning (OPT_Wattributes,
7273 "use __attribute__ ((vector_size)) instead");
7274 valid_mode = vector_mode_valid_p (mode);
7275 break;
7277 default:
7278 break;
7280 if (!valid_mode)
7282 error ("unable to emulate %qs", p);
7283 return NULL_TREE;
7286 if (POINTER_TYPE_P (type))
7288 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7289 tree (*fn)(tree, enum machine_mode, bool);
7291 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7293 error ("invalid pointer mode %qs", p);
7294 return NULL_TREE;
7297 if (TREE_CODE (type) == POINTER_TYPE)
7298 fn = build_pointer_type_for_mode;
7299 else
7300 fn = build_reference_type_for_mode;
7301 typefm = fn (TREE_TYPE (type), mode, false);
7303 else
7305 /* For fixed-point modes, we need to test if the signness of type
7306 and the machine mode are consistent. */
7307 if (ALL_FIXED_POINT_MODE_P (mode)
7308 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7310 error ("signedness of type and machine mode %qs don%'t match", p);
7311 return NULL_TREE;
7313 /* For fixed-point modes, we need to pass saturating info. */
7314 typefm = lang_hooks.types.type_for_mode (mode,
7315 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7316 : TYPE_UNSIGNED (type));
7319 if (typefm == NULL_TREE)
7321 error ("no data type for mode %qs", p);
7322 return NULL_TREE;
7324 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7326 /* For enumeral types, copy the precision from the integer
7327 type returned above. If not an INTEGER_TYPE, we can't use
7328 this mode for this type. */
7329 if (TREE_CODE (typefm) != INTEGER_TYPE)
7331 error ("cannot use mode %qs for enumeral types", p);
7332 return NULL_TREE;
7335 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7337 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7338 typefm = type;
7340 else
7342 /* We cannot build a type variant, as there's code that assumes
7343 that TYPE_MAIN_VARIANT has the same mode. This includes the
7344 debug generators. Instead, create a subrange type. This
7345 results in all of the enumeral values being emitted only once
7346 in the original, and the subtype gets them by reference. */
7347 if (TYPE_UNSIGNED (type))
7348 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7349 else
7350 typefm = make_signed_type (TYPE_PRECISION (typefm));
7351 TREE_TYPE (typefm) = type;
7354 else if (VECTOR_MODE_P (mode)
7355 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7356 : TREE_CODE (type) != TREE_CODE (typefm))
7358 error ("mode %qs applied to inappropriate type", p);
7359 return NULL_TREE;
7362 *node = typefm;
7365 return NULL_TREE;
7368 /* Handle a "section" attribute; arguments as in
7369 struct attribute_spec.handler. */
7371 static tree
7372 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7373 int ARG_UNUSED (flags), bool *no_add_attrs)
7375 tree decl = *node;
7377 if (targetm_common.have_named_sections)
7379 user_defined_section_attribute = true;
7381 if ((TREE_CODE (decl) == FUNCTION_DECL
7382 || TREE_CODE (decl) == VAR_DECL)
7383 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7385 if (TREE_CODE (decl) == VAR_DECL
7386 && current_function_decl != NULL_TREE
7387 && !TREE_STATIC (decl))
7389 error_at (DECL_SOURCE_LOCATION (decl),
7390 "section attribute cannot be specified for "
7391 "local variables");
7392 *no_add_attrs = true;
7395 /* The decl may have already been given a section attribute
7396 from a previous declaration. Ensure they match. */
7397 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7398 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7399 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7401 error ("section of %q+D conflicts with previous declaration",
7402 *node);
7403 *no_add_attrs = true;
7405 else if (TREE_CODE (decl) == VAR_DECL
7406 && !targetm.have_tls && targetm.emutls.tmpl_section
7407 && DECL_THREAD_LOCAL_P (decl))
7409 error ("section of %q+D cannot be overridden", *node);
7410 *no_add_attrs = true;
7412 else
7413 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7415 else
7417 error ("section attribute not allowed for %q+D", *node);
7418 *no_add_attrs = true;
7421 else
7423 error_at (DECL_SOURCE_LOCATION (*node),
7424 "section attributes are not supported for this target");
7425 *no_add_attrs = true;
7428 return NULL_TREE;
7431 /* Check whether ALIGN is a valid user-specified alignment. If so,
7432 return its base-2 log; if not, output an error and return -1. If
7433 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7434 no error. */
7436 check_user_alignment (const_tree align, bool allow_zero)
7438 int i;
7440 if (TREE_CODE (align) != INTEGER_CST
7441 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7443 error ("requested alignment is not an integer constant");
7444 return -1;
7446 else if (allow_zero && integer_zerop (align))
7447 return -1;
7448 else if (tree_int_cst_sgn (align) == -1
7449 || (i = tree_log2 (align)) == -1)
7451 error ("requested alignment is not a positive power of 2");
7452 return -1;
7454 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7456 error ("requested alignment is too large");
7457 return -1;
7459 return i;
7463 If in c++-11, check if the c++-11 alignment constraint with respect
7464 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7465 c++-11 mode, does nothing.
7467 [dcl.align]2/ says:
7469 [* if the constant expression evaluates to a fundamental alignment,
7470 the alignment requirement of the declared entity shall be the
7471 specified fundamental alignment.
7473 * if the constant expression evaluates to an extended alignment
7474 and the implementation supports that alignment in the context
7475 of the declaration, the alignment of the declared entity shall
7476 be that alignment
7478 * if the constant expression evaluates to an extended alignment
7479 and the implementation does not support that alignment in the
7480 context of the declaration, the program is ill-formed]. */
7482 static bool
7483 check_cxx_fundamental_alignment_constraints (tree node,
7484 unsigned align_log,
7485 int flags)
7487 bool alignment_too_large_p = false;
7488 unsigned requested_alignment = 1U << align_log;
7489 unsigned max_align = 0;
7491 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7492 || (node == NULL_TREE || node == error_mark_node))
7493 return true;
7495 if (cxx_fundamental_alignment_p (requested_alignment))
7496 return true;
7498 if (DECL_P (node))
7500 if (TREE_STATIC (node))
7502 /* For file scope variables and static members, the target
7503 supports alignments that are at most
7504 MAX_OFILE_ALIGNMENT. */
7505 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7506 alignment_too_large_p = true;
7508 else
7510 #ifdef BIGGEST_FIELD_ALIGNMENT
7511 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7512 #else
7513 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7514 #endif
7515 /* For non-static members, the target supports either
7516 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7517 if it is defined or BIGGEST_ALIGNMENT. */
7518 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7519 if (TREE_CODE (node) == FIELD_DECL
7520 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7521 alignment_too_large_p = true;
7522 #undef MAX_TARGET_FIELD_ALIGNMENT
7523 /* For stack variables, the target supports at most
7524 MAX_STACK_ALIGNMENT. */
7525 else if (decl_function_context (node) != NULL
7526 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7527 alignment_too_large_p = true;
7530 else if (TYPE_P (node))
7532 /* Let's be liberal for types. */
7533 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7534 alignment_too_large_p = true;
7537 if (alignment_too_large_p)
7538 pedwarn (input_location, OPT_Wattributes,
7539 "requested alignment %d is larger than %d",
7540 requested_alignment, max_align);
7542 return !alignment_too_large_p;
7545 /* Handle a "aligned" attribute; arguments as in
7546 struct attribute_spec.handler. */
7548 static tree
7549 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7550 int flags, bool *no_add_attrs)
7552 tree decl = NULL_TREE;
7553 tree *type = NULL;
7554 int is_type = 0;
7555 tree align_expr = (args ? TREE_VALUE (args)
7556 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7557 int i;
7559 if (DECL_P (*node))
7561 decl = *node;
7562 type = &TREE_TYPE (decl);
7563 is_type = TREE_CODE (*node) == TYPE_DECL;
7565 else if (TYPE_P (*node))
7566 type = node, is_type = 1;
7568 if ((i = check_user_alignment (align_expr, false)) == -1
7569 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7570 *no_add_attrs = true;
7571 else if (is_type)
7573 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7574 /* OK, modify the type in place. */;
7575 /* If we have a TYPE_DECL, then copy the type, so that we
7576 don't accidentally modify a builtin type. See pushdecl. */
7577 else if (decl && TREE_TYPE (decl) != error_mark_node
7578 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7580 tree tt = TREE_TYPE (decl);
7581 *type = build_variant_type_copy (*type);
7582 DECL_ORIGINAL_TYPE (decl) = tt;
7583 TYPE_NAME (*type) = decl;
7584 TREE_USED (*type) = TREE_USED (decl);
7585 TREE_TYPE (decl) = *type;
7587 else
7588 *type = build_variant_type_copy (*type);
7590 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7591 TYPE_USER_ALIGN (*type) = 1;
7593 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7594 && TREE_CODE (decl) != FIELD_DECL)
7596 error ("alignment may not be specified for %q+D", decl);
7597 *no_add_attrs = true;
7599 else if (DECL_USER_ALIGN (decl)
7600 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7601 /* C++-11 [dcl.align/4]:
7603 When multiple alignment-specifiers are specified for an
7604 entity, the alignment requirement shall be set to the
7605 strictest specified alignment.
7607 This formally comes from the c++11 specification but we are
7608 doing it for the GNU attribute syntax as well. */
7609 *no_add_attrs = true;
7610 else if (TREE_CODE (decl) == FUNCTION_DECL
7611 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7613 if (DECL_USER_ALIGN (decl))
7614 error ("alignment for %q+D was previously specified as %d "
7615 "and may not be decreased", decl,
7616 DECL_ALIGN (decl) / BITS_PER_UNIT);
7617 else
7618 error ("alignment for %q+D must be at least %d", decl,
7619 DECL_ALIGN (decl) / BITS_PER_UNIT);
7620 *no_add_attrs = true;
7622 else
7624 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7625 DECL_USER_ALIGN (decl) = 1;
7628 return NULL_TREE;
7631 /* Handle a "weak" attribute; arguments as in
7632 struct attribute_spec.handler. */
7634 static tree
7635 handle_weak_attribute (tree *node, tree name,
7636 tree ARG_UNUSED (args),
7637 int ARG_UNUSED (flags),
7638 bool * ARG_UNUSED (no_add_attrs))
7640 if (TREE_CODE (*node) == FUNCTION_DECL
7641 && DECL_DECLARED_INLINE_P (*node))
7643 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7644 *no_add_attrs = true;
7646 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7648 error ("indirect function %q+D cannot be declared weak", *node);
7649 *no_add_attrs = true;
7650 return NULL_TREE;
7652 else if (TREE_CODE (*node) == FUNCTION_DECL
7653 || TREE_CODE (*node) == VAR_DECL)
7654 declare_weak (*node);
7655 else
7656 warning (OPT_Wattributes, "%qE attribute ignored", name);
7658 return NULL_TREE;
7661 /* Handle an "alias" or "ifunc" attribute; arguments as in
7662 struct attribute_spec.handler, except that IS_ALIAS tells us
7663 whether this is an alias as opposed to ifunc attribute. */
7665 static tree
7666 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7667 bool *no_add_attrs)
7669 tree decl = *node;
7671 if (TREE_CODE (decl) != FUNCTION_DECL
7672 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7674 warning (OPT_Wattributes, "%qE attribute ignored", name);
7675 *no_add_attrs = true;
7677 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7678 || (TREE_CODE (decl) != FUNCTION_DECL
7679 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7680 /* A static variable declaration is always a tentative definition,
7681 but the alias is a non-tentative definition which overrides. */
7682 || (TREE_CODE (decl) != FUNCTION_DECL
7683 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7685 error ("%q+D defined both normally and as %qE attribute", decl, name);
7686 *no_add_attrs = true;
7687 return NULL_TREE;
7689 else if (!is_alias
7690 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7691 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7693 error ("weak %q+D cannot be defined %qE", decl, name);
7694 *no_add_attrs = true;
7695 return NULL_TREE;
7698 /* Note that the very first time we process a nested declaration,
7699 decl_function_context will not be set. Indeed, *would* never
7700 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7701 we do below. After such frobbery, pushdecl would set the context.
7702 In any case, this is never what we want. */
7703 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7705 tree id;
7707 id = TREE_VALUE (args);
7708 if (TREE_CODE (id) != STRING_CST)
7710 error ("attribute %qE argument not a string", name);
7711 *no_add_attrs = true;
7712 return NULL_TREE;
7714 id = get_identifier (TREE_STRING_POINTER (id));
7715 /* This counts as a use of the object pointed to. */
7716 TREE_USED (id) = 1;
7718 if (TREE_CODE (decl) == FUNCTION_DECL)
7719 DECL_INITIAL (decl) = error_mark_node;
7720 else
7721 TREE_STATIC (decl) = 1;
7723 if (!is_alias)
7724 /* ifuncs are also aliases, so set that attribute too. */
7725 DECL_ATTRIBUTES (decl)
7726 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7728 else
7730 warning (OPT_Wattributes, "%qE attribute ignored", name);
7731 *no_add_attrs = true;
7734 return NULL_TREE;
7737 /* Handle an "alias" or "ifunc" attribute; arguments as in
7738 struct attribute_spec.handler. */
7740 static tree
7741 handle_ifunc_attribute (tree *node, tree name, tree args,
7742 int ARG_UNUSED (flags), bool *no_add_attrs)
7744 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7747 /* Handle an "alias" or "ifunc" attribute; arguments as in
7748 struct attribute_spec.handler. */
7750 static tree
7751 handle_alias_attribute (tree *node, tree name, tree args,
7752 int ARG_UNUSED (flags), bool *no_add_attrs)
7754 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7757 /* Handle a "weakref" attribute; arguments as in struct
7758 attribute_spec.handler. */
7760 static tree
7761 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7762 int flags, bool *no_add_attrs)
7764 tree attr = NULL_TREE;
7766 /* We must ignore the attribute when it is associated with
7767 local-scoped decls, since attribute alias is ignored and many
7768 such symbols do not even have a DECL_WEAK field. */
7769 if (decl_function_context (*node)
7770 || current_function_decl
7771 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7773 warning (OPT_Wattributes, "%qE attribute ignored", name);
7774 *no_add_attrs = true;
7775 return NULL_TREE;
7778 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7780 error ("indirect function %q+D cannot be declared weakref", *node);
7781 *no_add_attrs = true;
7782 return NULL_TREE;
7785 /* The idea here is that `weakref("name")' mutates into `weakref,
7786 alias("name")', and weakref without arguments, in turn,
7787 implicitly adds weak. */
7789 if (args)
7791 attr = tree_cons (get_identifier ("alias"), args, attr);
7792 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7794 *no_add_attrs = true;
7796 decl_attributes (node, attr, flags);
7798 else
7800 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7801 error_at (DECL_SOURCE_LOCATION (*node),
7802 "weakref attribute must appear before alias attribute");
7804 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7805 and that isn't supported; and because it wants to add it to
7806 the list of weak decls, which isn't helpful. */
7807 DECL_WEAK (*node) = 1;
7810 return NULL_TREE;
7813 /* Handle an "visibility" attribute; arguments as in
7814 struct attribute_spec.handler. */
7816 static tree
7817 handle_visibility_attribute (tree *node, tree name, tree args,
7818 int ARG_UNUSED (flags),
7819 bool *ARG_UNUSED (no_add_attrs))
7821 tree decl = *node;
7822 tree id = TREE_VALUE (args);
7823 enum symbol_visibility vis;
7825 if (TYPE_P (*node))
7827 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7828 /* OK */;
7829 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7831 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7832 name);
7833 return NULL_TREE;
7835 else if (TYPE_FIELDS (*node))
7837 error ("%qE attribute ignored because %qT is already defined",
7838 name, *node);
7839 return NULL_TREE;
7842 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7844 warning (OPT_Wattributes, "%qE attribute ignored", name);
7845 return NULL_TREE;
7848 if (TREE_CODE (id) != STRING_CST)
7850 error ("visibility argument not a string");
7851 return NULL_TREE;
7854 /* If this is a type, set the visibility on the type decl. */
7855 if (TYPE_P (decl))
7857 decl = TYPE_NAME (decl);
7858 if (!decl)
7859 return NULL_TREE;
7860 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7862 warning (OPT_Wattributes, "%qE attribute ignored on types",
7863 name);
7864 return NULL_TREE;
7868 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7869 vis = VISIBILITY_DEFAULT;
7870 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7871 vis = VISIBILITY_INTERNAL;
7872 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7873 vis = VISIBILITY_HIDDEN;
7874 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7875 vis = VISIBILITY_PROTECTED;
7876 else
7878 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7879 vis = VISIBILITY_DEFAULT;
7882 if (DECL_VISIBILITY_SPECIFIED (decl)
7883 && vis != DECL_VISIBILITY (decl))
7885 tree attributes = (TYPE_P (*node)
7886 ? TYPE_ATTRIBUTES (*node)
7887 : DECL_ATTRIBUTES (decl));
7888 if (lookup_attribute ("visibility", attributes))
7889 error ("%qD redeclared with different visibility", decl);
7890 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7891 && lookup_attribute ("dllimport", attributes))
7892 error ("%qD was declared %qs which implies default visibility",
7893 decl, "dllimport");
7894 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7895 && lookup_attribute ("dllexport", attributes))
7896 error ("%qD was declared %qs which implies default visibility",
7897 decl, "dllexport");
7900 DECL_VISIBILITY (decl) = vis;
7901 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7903 /* Go ahead and attach the attribute to the node as well. This is needed
7904 so we can determine whether we have VISIBILITY_DEFAULT because the
7905 visibility was not specified, or because it was explicitly overridden
7906 from the containing scope. */
7908 return NULL_TREE;
7911 /* Determine the ELF symbol visibility for DECL, which is either a
7912 variable or a function. It is an error to use this function if a
7913 definition of DECL is not available in this translation unit.
7914 Returns true if the final visibility has been determined by this
7915 function; false if the caller is free to make additional
7916 modifications. */
7918 bool
7919 c_determine_visibility (tree decl)
7921 gcc_assert (TREE_CODE (decl) == VAR_DECL
7922 || TREE_CODE (decl) == FUNCTION_DECL);
7924 /* If the user explicitly specified the visibility with an
7925 attribute, honor that. DECL_VISIBILITY will have been set during
7926 the processing of the attribute. We check for an explicit
7927 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7928 to distinguish the use of an attribute from the use of a "#pragma
7929 GCC visibility push(...)"; in the latter case we still want other
7930 considerations to be able to overrule the #pragma. */
7931 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7932 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7933 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7934 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7935 return true;
7937 /* Set default visibility to whatever the user supplied with
7938 visibility_specified depending on #pragma GCC visibility. */
7939 if (!DECL_VISIBILITY_SPECIFIED (decl))
7941 if (visibility_options.inpragma
7942 || DECL_VISIBILITY (decl) != default_visibility)
7944 DECL_VISIBILITY (decl) = default_visibility;
7945 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7946 /* If visibility changed and DECL already has DECL_RTL, ensure
7947 symbol flags are updated. */
7948 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7949 || TREE_CODE (decl) == FUNCTION_DECL)
7950 && DECL_RTL_SET_P (decl))
7951 make_decl_rtl (decl);
7954 return false;
7957 /* Handle an "tls_model" attribute; arguments as in
7958 struct attribute_spec.handler. */
7960 static tree
7961 handle_tls_model_attribute (tree *node, tree name, tree args,
7962 int ARG_UNUSED (flags), bool *no_add_attrs)
7964 tree id;
7965 tree decl = *node;
7966 enum tls_model kind;
7968 *no_add_attrs = true;
7970 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7972 warning (OPT_Wattributes, "%qE attribute ignored", name);
7973 return NULL_TREE;
7976 kind = DECL_TLS_MODEL (decl);
7977 id = TREE_VALUE (args);
7978 if (TREE_CODE (id) != STRING_CST)
7980 error ("tls_model argument not a string");
7981 return NULL_TREE;
7984 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7985 kind = TLS_MODEL_LOCAL_EXEC;
7986 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7987 kind = TLS_MODEL_INITIAL_EXEC;
7988 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7989 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7990 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7991 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7992 else
7993 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7995 DECL_TLS_MODEL (decl) = kind;
7996 return NULL_TREE;
7999 /* Handle a "no_instrument_function" attribute; arguments as in
8000 struct attribute_spec.handler. */
8002 static tree
8003 handle_no_instrument_function_attribute (tree *node, tree name,
8004 tree ARG_UNUSED (args),
8005 int ARG_UNUSED (flags),
8006 bool *no_add_attrs)
8008 tree decl = *node;
8010 if (TREE_CODE (decl) != FUNCTION_DECL)
8012 error_at (DECL_SOURCE_LOCATION (decl),
8013 "%qE attribute applies only to functions", name);
8014 *no_add_attrs = true;
8016 else if (DECL_INITIAL (decl))
8018 error_at (DECL_SOURCE_LOCATION (decl),
8019 "can%'t set %qE attribute after definition", name);
8020 *no_add_attrs = true;
8022 else
8023 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8025 return NULL_TREE;
8028 /* Handle a "malloc" attribute; arguments as in
8029 struct attribute_spec.handler. */
8031 static tree
8032 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8033 int ARG_UNUSED (flags), bool *no_add_attrs)
8035 if (TREE_CODE (*node) == FUNCTION_DECL
8036 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8037 DECL_IS_MALLOC (*node) = 1;
8038 else
8040 warning (OPT_Wattributes, "%qE attribute ignored", name);
8041 *no_add_attrs = true;
8044 return NULL_TREE;
8047 /* Handle a "alloc_size" attribute; arguments as in
8048 struct attribute_spec.handler. */
8050 static tree
8051 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8052 int ARG_UNUSED (flags), bool *no_add_attrs)
8054 unsigned arg_count = type_num_arguments (*node);
8055 for (; args; args = TREE_CHAIN (args))
8057 tree position = TREE_VALUE (args);
8059 if (TREE_CODE (position) != INTEGER_CST
8060 || TREE_INT_CST_HIGH (position)
8061 || TREE_INT_CST_LOW (position) < 1
8062 || TREE_INT_CST_LOW (position) > arg_count )
8064 warning (OPT_Wattributes,
8065 "alloc_size parameter outside range");
8066 *no_add_attrs = true;
8067 return NULL_TREE;
8070 return NULL_TREE;
8073 /* Handle a "fn spec" attribute; arguments as in
8074 struct attribute_spec.handler. */
8076 static tree
8077 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8078 tree args, int ARG_UNUSED (flags),
8079 bool *no_add_attrs ATTRIBUTE_UNUSED)
8081 gcc_assert (args
8082 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8083 && !TREE_CHAIN (args));
8084 return NULL_TREE;
8087 /* Handle a "bnd_variable_size" attribute; arguments as in
8088 struct attribute_spec.handler. */
8090 static tree
8091 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8092 int ARG_UNUSED (flags), bool *no_add_attrs)
8094 if (TREE_CODE (*node) != FIELD_DECL)
8096 warning (OPT_Wattributes, "%qE attribute ignored", name);
8097 *no_add_attrs = true;
8100 return NULL_TREE;
8103 /* Handle a "bnd_legacy" attribute; arguments as in
8104 struct attribute_spec.handler. */
8106 static tree
8107 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8108 int ARG_UNUSED (flags), bool *no_add_attrs)
8110 if (TREE_CODE (*node) != FUNCTION_DECL)
8112 warning (OPT_Wattributes, "%qE attribute ignored", name);
8113 *no_add_attrs = true;
8116 return NULL_TREE;
8119 /* Handle a "warn_unused" attribute; arguments as in
8120 struct attribute_spec.handler. */
8122 static tree
8123 handle_warn_unused_attribute (tree *node, tree name,
8124 tree args ATTRIBUTE_UNUSED,
8125 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8127 if (TYPE_P (*node))
8128 /* Do nothing else, just set the attribute. We'll get at
8129 it later with lookup_attribute. */
8131 else
8133 warning (OPT_Wattributes, "%qE attribute ignored", name);
8134 *no_add_attrs = true;
8137 return NULL_TREE;
8140 /* Handle an "omp declare simd" attribute; arguments as in
8141 struct attribute_spec.handler. */
8143 static tree
8144 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8146 return NULL_TREE;
8149 /* Handle an "omp declare target" attribute; arguments as in
8150 struct attribute_spec.handler. */
8152 static tree
8153 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8155 return NULL_TREE;
8158 /* Handle a "returns_twice" attribute; arguments as in
8159 struct attribute_spec.handler. */
8161 static tree
8162 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8163 int ARG_UNUSED (flags), bool *no_add_attrs)
8165 if (TREE_CODE (*node) == FUNCTION_DECL)
8166 DECL_IS_RETURNS_TWICE (*node) = 1;
8167 else
8169 warning (OPT_Wattributes, "%qE attribute ignored", name);
8170 *no_add_attrs = true;
8173 return NULL_TREE;
8176 /* Handle a "no_limit_stack" attribute; arguments as in
8177 struct attribute_spec.handler. */
8179 static tree
8180 handle_no_limit_stack_attribute (tree *node, tree name,
8181 tree ARG_UNUSED (args),
8182 int ARG_UNUSED (flags),
8183 bool *no_add_attrs)
8185 tree decl = *node;
8187 if (TREE_CODE (decl) != FUNCTION_DECL)
8189 error_at (DECL_SOURCE_LOCATION (decl),
8190 "%qE attribute applies only to functions", name);
8191 *no_add_attrs = true;
8193 else if (DECL_INITIAL (decl))
8195 error_at (DECL_SOURCE_LOCATION (decl),
8196 "can%'t set %qE attribute after definition", name);
8197 *no_add_attrs = true;
8199 else
8200 DECL_NO_LIMIT_STACK (decl) = 1;
8202 return NULL_TREE;
8205 /* Handle a "pure" attribute; arguments as in
8206 struct attribute_spec.handler. */
8208 static tree
8209 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8210 int ARG_UNUSED (flags), bool *no_add_attrs)
8212 if (TREE_CODE (*node) == FUNCTION_DECL)
8213 DECL_PURE_P (*node) = 1;
8214 /* ??? TODO: Support types. */
8215 else
8217 warning (OPT_Wattributes, "%qE attribute ignored", name);
8218 *no_add_attrs = true;
8221 return NULL_TREE;
8224 /* Digest an attribute list destined for a transactional memory statement.
8225 ALLOWED is the set of attributes that are allowed for this statement;
8226 return the attribute we parsed. Multiple attributes are never allowed. */
8229 parse_tm_stmt_attr (tree attrs, int allowed)
8231 tree a_seen = NULL;
8232 int m_seen = 0;
8234 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8236 tree a = TREE_PURPOSE (attrs);
8237 int m = 0;
8239 if (is_attribute_p ("outer", a))
8240 m = TM_STMT_ATTR_OUTER;
8242 if ((m & allowed) == 0)
8244 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8245 continue;
8248 if (m_seen == 0)
8250 a_seen = a;
8251 m_seen = m;
8253 else if (m_seen == m)
8254 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8255 else
8256 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8259 return m_seen;
8262 /* Transform a TM attribute name into a maskable integer and back.
8263 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8264 to how the lack of an attribute is treated. */
8267 tm_attr_to_mask (tree attr)
8269 if (attr == NULL)
8270 return 0;
8271 if (is_attribute_p ("transaction_safe", attr))
8272 return TM_ATTR_SAFE;
8273 if (is_attribute_p ("transaction_callable", attr))
8274 return TM_ATTR_CALLABLE;
8275 if (is_attribute_p ("transaction_pure", attr))
8276 return TM_ATTR_PURE;
8277 if (is_attribute_p ("transaction_unsafe", attr))
8278 return TM_ATTR_IRREVOCABLE;
8279 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8280 return TM_ATTR_MAY_CANCEL_OUTER;
8281 return 0;
8284 tree
8285 tm_mask_to_attr (int mask)
8287 const char *str;
8288 switch (mask)
8290 case TM_ATTR_SAFE:
8291 str = "transaction_safe";
8292 break;
8293 case TM_ATTR_CALLABLE:
8294 str = "transaction_callable";
8295 break;
8296 case TM_ATTR_PURE:
8297 str = "transaction_pure";
8298 break;
8299 case TM_ATTR_IRREVOCABLE:
8300 str = "transaction_unsafe";
8301 break;
8302 case TM_ATTR_MAY_CANCEL_OUTER:
8303 str = "transaction_may_cancel_outer";
8304 break;
8305 default:
8306 gcc_unreachable ();
8308 return get_identifier (str);
8311 /* Return the first TM attribute seen in LIST. */
8313 tree
8314 find_tm_attribute (tree list)
8316 for (; list ; list = TREE_CHAIN (list))
8318 tree name = TREE_PURPOSE (list);
8319 if (tm_attr_to_mask (name) != 0)
8320 return name;
8322 return NULL_TREE;
8325 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8326 Here we accept only function types, and verify that none of the other
8327 function TM attributes are also applied. */
8328 /* ??? We need to accept class types for C++, but not C. This greatly
8329 complicates this function, since we can no longer rely on the extra
8330 processing given by function_type_required. */
8332 static tree
8333 handle_tm_attribute (tree *node, tree name, tree args,
8334 int flags, bool *no_add_attrs)
8336 /* Only one path adds the attribute; others don't. */
8337 *no_add_attrs = true;
8339 switch (TREE_CODE (*node))
8341 case RECORD_TYPE:
8342 case UNION_TYPE:
8343 /* Only tm_callable and tm_safe apply to classes. */
8344 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8345 goto ignored;
8346 /* FALLTHRU */
8348 case FUNCTION_TYPE:
8349 case METHOD_TYPE:
8351 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8352 if (old_name == name)
8354 else if (old_name != NULL_TREE)
8355 error ("type was previously declared %qE", old_name);
8356 else
8357 *no_add_attrs = false;
8359 break;
8361 case POINTER_TYPE:
8363 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8364 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8366 tree fn_tmp = TREE_TYPE (*node);
8367 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8368 *node = build_pointer_type (fn_tmp);
8369 break;
8372 /* FALLTHRU */
8374 default:
8375 /* If a function is next, pass it on to be tried next. */
8376 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8377 return tree_cons (name, args, NULL);
8379 ignored:
8380 warning (OPT_Wattributes, "%qE attribute ignored", name);
8381 break;
8384 return NULL_TREE;
8387 /* Handle the TM_WRAP attribute; arguments as in
8388 struct attribute_spec.handler. */
8390 static tree
8391 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8392 int ARG_UNUSED (flags), bool *no_add_attrs)
8394 tree decl = *node;
8396 /* We don't need the attribute even on success, since we
8397 record the entry in an external table. */
8398 *no_add_attrs = true;
8400 if (TREE_CODE (decl) != FUNCTION_DECL)
8401 warning (OPT_Wattributes, "%qE attribute ignored", name);
8402 else
8404 tree wrap_decl = TREE_VALUE (args);
8405 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8406 && TREE_CODE (wrap_decl) != VAR_DECL
8407 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8408 error ("%qE argument not an identifier", name);
8409 else
8411 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8412 wrap_decl = lookup_name (wrap_decl);
8413 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8415 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8416 TREE_TYPE (wrap_decl)))
8417 record_tm_replacement (wrap_decl, decl);
8418 else
8419 error ("%qD is not compatible with %qD", wrap_decl, decl);
8421 else
8422 error ("transaction_wrap argument is not a function");
8426 return NULL_TREE;
8429 /* Ignore the given attribute. Used when this attribute may be usefully
8430 overridden by the target, but is not used generically. */
8432 static tree
8433 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8434 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8435 bool *no_add_attrs)
8437 *no_add_attrs = true;
8438 return NULL_TREE;
8441 /* Handle a "no vops" attribute; arguments as in
8442 struct attribute_spec.handler. */
8444 static tree
8445 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8446 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8447 bool *ARG_UNUSED (no_add_attrs))
8449 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8450 DECL_IS_NOVOPS (*node) = 1;
8451 return NULL_TREE;
8454 /* Handle a "deprecated" attribute; arguments as in
8455 struct attribute_spec.handler. */
8457 static tree
8458 handle_deprecated_attribute (tree *node, tree name,
8459 tree args, int flags,
8460 bool *no_add_attrs)
8462 tree type = NULL_TREE;
8463 int warn = 0;
8464 tree what = NULL_TREE;
8466 if (!args)
8467 *no_add_attrs = true;
8468 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8470 error ("deprecated message is not a string");
8471 *no_add_attrs = true;
8474 if (DECL_P (*node))
8476 tree decl = *node;
8477 type = TREE_TYPE (decl);
8479 if (TREE_CODE (decl) == TYPE_DECL
8480 || TREE_CODE (decl) == PARM_DECL
8481 || TREE_CODE (decl) == VAR_DECL
8482 || TREE_CODE (decl) == FUNCTION_DECL
8483 || TREE_CODE (decl) == FIELD_DECL
8484 || objc_method_decl (TREE_CODE (decl)))
8485 TREE_DEPRECATED (decl) = 1;
8486 else
8487 warn = 1;
8489 else if (TYPE_P (*node))
8491 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8492 *node = build_variant_type_copy (*node);
8493 TREE_DEPRECATED (*node) = 1;
8494 type = *node;
8496 else
8497 warn = 1;
8499 if (warn)
8501 *no_add_attrs = true;
8502 if (type && TYPE_NAME (type))
8504 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8505 what = TYPE_NAME (*node);
8506 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8507 && DECL_NAME (TYPE_NAME (type)))
8508 what = DECL_NAME (TYPE_NAME (type));
8510 if (what)
8511 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8512 else
8513 warning (OPT_Wattributes, "%qE attribute ignored", name);
8516 return NULL_TREE;
8519 /* Handle a "vector_size" attribute; arguments as in
8520 struct attribute_spec.handler. */
8522 static tree
8523 handle_vector_size_attribute (tree *node, tree name, tree args,
8524 int ARG_UNUSED (flags),
8525 bool *no_add_attrs)
8527 unsigned HOST_WIDE_INT vecsize, nunits;
8528 enum machine_mode orig_mode;
8529 tree type = *node, new_type, size;
8531 *no_add_attrs = true;
8533 size = TREE_VALUE (args);
8535 if (!host_integerp (size, 1))
8537 warning (OPT_Wattributes, "%qE attribute ignored", name);
8538 return NULL_TREE;
8541 /* Get the vector size (in bytes). */
8542 vecsize = tree_low_cst (size, 1);
8544 /* We need to provide for vector pointers, vector arrays, and
8545 functions returning vectors. For example:
8547 __attribute__((vector_size(16))) short *foo;
8549 In this case, the mode is SI, but the type being modified is
8550 HI, so we need to look further. */
8552 while (POINTER_TYPE_P (type)
8553 || TREE_CODE (type) == FUNCTION_TYPE
8554 || TREE_CODE (type) == METHOD_TYPE
8555 || TREE_CODE (type) == ARRAY_TYPE
8556 || TREE_CODE (type) == OFFSET_TYPE)
8557 type = TREE_TYPE (type);
8559 /* Get the mode of the type being modified. */
8560 orig_mode = TYPE_MODE (type);
8562 if ((!INTEGRAL_TYPE_P (type)
8563 && !SCALAR_FLOAT_TYPE_P (type)
8564 && !FIXED_POINT_TYPE_P (type))
8565 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8566 && GET_MODE_CLASS (orig_mode) != MODE_INT
8567 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8568 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8569 || TREE_CODE (type) == BOOLEAN_TYPE)
8571 error ("invalid vector type for attribute %qE", name);
8572 return NULL_TREE;
8575 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8577 error ("vector size not an integral multiple of component size");
8578 return NULL;
8581 if (vecsize == 0)
8583 error ("zero vector size");
8584 return NULL;
8587 /* Calculate how many units fit in the vector. */
8588 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8589 if (nunits & (nunits - 1))
8591 error ("number of components of the vector not a power of two");
8592 return NULL_TREE;
8595 new_type = build_vector_type (type, nunits);
8597 /* Build back pointers if needed. */
8598 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8600 return NULL_TREE;
8603 /* Handle the "nonnull" attribute. */
8604 static tree
8605 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8606 tree args, int ARG_UNUSED (flags),
8607 bool *no_add_attrs)
8609 tree type = *node;
8610 unsigned HOST_WIDE_INT attr_arg_num;
8612 /* If no arguments are specified, all pointer arguments should be
8613 non-null. Verify a full prototype is given so that the arguments
8614 will have the correct types when we actually check them later. */
8615 if (!args)
8617 if (!prototype_p (type))
8619 error ("nonnull attribute without arguments on a non-prototype");
8620 *no_add_attrs = true;
8622 return NULL_TREE;
8625 /* Argument list specified. Verify that each argument number references
8626 a pointer argument. */
8627 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8629 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8631 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8633 error ("nonnull argument has invalid operand number (argument %lu)",
8634 (unsigned long) attr_arg_num);
8635 *no_add_attrs = true;
8636 return NULL_TREE;
8639 if (prototype_p (type))
8641 function_args_iterator iter;
8642 tree argument;
8644 function_args_iter_init (&iter, type);
8645 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8647 argument = function_args_iter_cond (&iter);
8648 if (argument == NULL_TREE || ck_num == arg_num)
8649 break;
8652 if (!argument
8653 || TREE_CODE (argument) == VOID_TYPE)
8655 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8656 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8657 *no_add_attrs = true;
8658 return NULL_TREE;
8661 if (TREE_CODE (argument) != POINTER_TYPE)
8663 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8664 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8665 *no_add_attrs = true;
8666 return NULL_TREE;
8671 return NULL_TREE;
8674 /* Check the argument list of a function call for null in argument slots
8675 that are marked as requiring a non-null pointer argument. The NARGS
8676 arguments are passed in the array ARGARRAY.
8679 static void
8680 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8682 tree a;
8683 int i;
8685 attrs = lookup_attribute ("nonnull", attrs);
8686 if (attrs == NULL_TREE)
8687 return;
8689 a = attrs;
8690 /* See if any of the nonnull attributes has no arguments. If so,
8691 then every pointer argument is checked (in which case the check
8692 for pointer type is done in check_nonnull_arg). */
8693 if (TREE_VALUE (a) != NULL_TREE)
8695 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8696 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8698 if (a != NULL_TREE)
8699 for (i = 0; i < nargs; i++)
8700 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8701 i + 1);
8702 else
8704 /* Walk the argument list. If we encounter an argument number we
8705 should check for non-null, do it. */
8706 for (i = 0; i < nargs; i++)
8708 for (a = attrs; ; a = TREE_CHAIN (a))
8710 a = lookup_attribute ("nonnull", a);
8711 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8712 break;
8715 if (a != NULL_TREE)
8716 check_function_arguments_recurse (check_nonnull_arg, NULL,
8717 argarray[i], i + 1);
8722 /* Check that the Nth argument of a function call (counting backwards
8723 from the end) is a (pointer)0. The NARGS arguments are passed in the
8724 array ARGARRAY. */
8726 static void
8727 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8729 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8731 if (attr)
8733 int len = 0;
8734 int pos = 0;
8735 tree sentinel;
8736 function_args_iterator iter;
8737 tree t;
8739 /* Skip over the named arguments. */
8740 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8742 if (len == nargs)
8743 break;
8744 len++;
8747 if (TREE_VALUE (attr))
8749 tree p = TREE_VALUE (TREE_VALUE (attr));
8750 pos = TREE_INT_CST_LOW (p);
8753 /* The sentinel must be one of the varargs, i.e.
8754 in position >= the number of fixed arguments. */
8755 if ((nargs - 1 - pos) < len)
8757 warning (OPT_Wformat_,
8758 "not enough variable arguments to fit a sentinel");
8759 return;
8762 /* Validate the sentinel. */
8763 sentinel = argarray[nargs - 1 - pos];
8764 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8765 || !integer_zerop (sentinel))
8766 /* Although __null (in C++) is only an integer we allow it
8767 nevertheless, as we are guaranteed that it's exactly
8768 as wide as a pointer, and we don't want to force
8769 users to cast the NULL they have written there.
8770 We warn with -Wstrict-null-sentinel, though. */
8771 && (warn_strict_null_sentinel || null_node != sentinel))
8772 warning (OPT_Wformat_, "missing sentinel in function call");
8776 /* Helper for check_function_nonnull; given a list of operands which
8777 must be non-null in ARGS, determine if operand PARAM_NUM should be
8778 checked. */
8780 static bool
8781 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8783 unsigned HOST_WIDE_INT arg_num = 0;
8785 for (; args; args = TREE_CHAIN (args))
8787 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8789 gcc_assert (found);
8791 if (arg_num == param_num)
8792 return true;
8794 return false;
8797 /* Check that the function argument PARAM (which is operand number
8798 PARAM_NUM) is non-null. This is called by check_function_nonnull
8799 via check_function_arguments_recurse. */
8801 static void
8802 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8803 unsigned HOST_WIDE_INT param_num)
8805 /* Just skip checking the argument if it's not a pointer. This can
8806 happen if the "nonnull" attribute was given without an operand
8807 list (which means to check every pointer argument). */
8809 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8810 return;
8812 if (integer_zerop (param))
8813 warning (OPT_Wnonnull, "null argument where non-null required "
8814 "(argument %lu)", (unsigned long) param_num);
8817 /* Helper for nonnull attribute handling; fetch the operand number
8818 from the attribute argument list. */
8820 static bool
8821 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8823 /* Verify the arg number is a constant. */
8824 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8825 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8826 return false;
8828 *valp = TREE_INT_CST_LOW (arg_num_expr);
8829 return true;
8832 /* Handle a "nothrow" attribute; arguments as in
8833 struct attribute_spec.handler. */
8835 static tree
8836 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8837 int ARG_UNUSED (flags), bool *no_add_attrs)
8839 if (TREE_CODE (*node) == FUNCTION_DECL)
8840 TREE_NOTHROW (*node) = 1;
8841 /* ??? TODO: Support types. */
8842 else
8844 warning (OPT_Wattributes, "%qE attribute ignored", name);
8845 *no_add_attrs = true;
8848 return NULL_TREE;
8851 /* Handle a "cleanup" attribute; arguments as in
8852 struct attribute_spec.handler. */
8854 static tree
8855 handle_cleanup_attribute (tree *node, tree name, tree args,
8856 int ARG_UNUSED (flags), bool *no_add_attrs)
8858 tree decl = *node;
8859 tree cleanup_id, cleanup_decl;
8861 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8862 for global destructors in C++. This requires infrastructure that
8863 we don't have generically at the moment. It's also not a feature
8864 we'd be missing too much, since we do have attribute constructor. */
8865 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8867 warning (OPT_Wattributes, "%qE attribute ignored", name);
8868 *no_add_attrs = true;
8869 return NULL_TREE;
8872 /* Verify that the argument is a function in scope. */
8873 /* ??? We could support pointers to functions here as well, if
8874 that was considered desirable. */
8875 cleanup_id = TREE_VALUE (args);
8876 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8878 error ("cleanup argument not an identifier");
8879 *no_add_attrs = true;
8880 return NULL_TREE;
8882 cleanup_decl = lookup_name (cleanup_id);
8883 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8885 error ("cleanup argument not a function");
8886 *no_add_attrs = true;
8887 return NULL_TREE;
8890 /* That the function has proper type is checked with the
8891 eventual call to build_function_call. */
8893 return NULL_TREE;
8896 /* Handle a "warn_unused_result" attribute. No special handling. */
8898 static tree
8899 handle_warn_unused_result_attribute (tree *node, tree name,
8900 tree ARG_UNUSED (args),
8901 int ARG_UNUSED (flags), bool *no_add_attrs)
8903 /* Ignore the attribute for functions not returning any value. */
8904 if (VOID_TYPE_P (TREE_TYPE (*node)))
8906 warning (OPT_Wattributes, "%qE attribute ignored", name);
8907 *no_add_attrs = true;
8910 return NULL_TREE;
8913 /* Handle a "sentinel" attribute. */
8915 static tree
8916 handle_sentinel_attribute (tree *node, tree name, tree args,
8917 int ARG_UNUSED (flags), bool *no_add_attrs)
8919 if (!prototype_p (*node))
8921 warning (OPT_Wattributes,
8922 "%qE attribute requires prototypes with named arguments", name);
8923 *no_add_attrs = true;
8925 else
8927 if (!stdarg_p (*node))
8929 warning (OPT_Wattributes,
8930 "%qE attribute only applies to variadic functions", name);
8931 *no_add_attrs = true;
8935 if (args)
8937 tree position = TREE_VALUE (args);
8939 if (TREE_CODE (position) != INTEGER_CST)
8941 warning (OPT_Wattributes,
8942 "requested position is not an integer constant");
8943 *no_add_attrs = true;
8945 else
8947 if (tree_int_cst_lt (position, integer_zero_node))
8949 warning (OPT_Wattributes,
8950 "requested position is less than zero");
8951 *no_add_attrs = true;
8956 return NULL_TREE;
8959 /* Handle a "type_generic" attribute. */
8961 static tree
8962 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8963 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8964 bool * ARG_UNUSED (no_add_attrs))
8966 /* Ensure we have a function type. */
8967 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8969 /* Ensure we have a variadic function. */
8970 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8972 return NULL_TREE;
8975 /* Handle a "target" attribute. */
8977 static tree
8978 handle_target_attribute (tree *node, tree name, tree args, int flags,
8979 bool *no_add_attrs)
8981 /* Ensure we have a function type. */
8982 if (TREE_CODE (*node) != FUNCTION_DECL)
8984 warning (OPT_Wattributes, "%qE attribute ignored", name);
8985 *no_add_attrs = true;
8987 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8988 flags))
8989 *no_add_attrs = true;
8991 return NULL_TREE;
8994 /* Arguments being collected for optimization. */
8995 typedef const char *const_char_p; /* For DEF_VEC_P. */
8996 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8999 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9000 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9001 false for #pragma GCC optimize. */
9003 bool
9004 parse_optimize_options (tree args, bool attr_p)
9006 bool ret = true;
9007 unsigned opt_argc;
9008 unsigned i;
9009 int saved_flag_strict_aliasing;
9010 const char **opt_argv;
9011 struct cl_decoded_option *decoded_options;
9012 unsigned int decoded_options_count;
9013 tree ap;
9015 /* Build up argv vector. Just in case the string is stored away, use garbage
9016 collected strings. */
9017 vec_safe_truncate (optimize_args, 0);
9018 vec_safe_push (optimize_args, (const char *) NULL);
9020 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9022 tree value = TREE_VALUE (ap);
9024 if (TREE_CODE (value) == INTEGER_CST)
9026 char buffer[20];
9027 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9028 vec_safe_push (optimize_args, ggc_strdup (buffer));
9031 else if (TREE_CODE (value) == STRING_CST)
9033 /* Split string into multiple substrings. */
9034 size_t len = TREE_STRING_LENGTH (value);
9035 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9036 char *end = p + len;
9037 char *comma;
9038 char *next_p = p;
9040 while (next_p != NULL)
9042 size_t len2;
9043 char *q, *r;
9045 p = next_p;
9046 comma = strchr (p, ',');
9047 if (comma)
9049 len2 = comma - p;
9050 *comma = '\0';
9051 next_p = comma+1;
9053 else
9055 len2 = end - p;
9056 next_p = NULL;
9059 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9061 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9062 options. */
9063 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9065 ret = false;
9066 if (attr_p)
9067 warning (OPT_Wattributes,
9068 "bad option %s to optimize attribute", p);
9069 else
9070 warning (OPT_Wpragmas,
9071 "bad option %s to pragma attribute", p);
9072 continue;
9075 if (*p != '-')
9077 *r++ = '-';
9079 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9080 itself is -Os, and any other switch begins with a -f. */
9081 if ((*p >= '0' && *p <= '9')
9082 || (p[0] == 's' && p[1] == '\0'))
9083 *r++ = 'O';
9084 else if (*p != 'O')
9085 *r++ = 'f';
9088 memcpy (r, p, len2);
9089 r[len2] = '\0';
9090 vec_safe_push (optimize_args, (const char *) q);
9096 opt_argc = optimize_args->length ();
9097 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9099 for (i = 1; i < opt_argc; i++)
9100 opt_argv[i] = (*optimize_args)[i];
9102 saved_flag_strict_aliasing = flag_strict_aliasing;
9104 /* Now parse the options. */
9105 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9106 &decoded_options,
9107 &decoded_options_count);
9108 decode_options (&global_options, &global_options_set,
9109 decoded_options, decoded_options_count,
9110 input_location, global_dc);
9112 targetm.override_options_after_change();
9114 /* Don't allow changing -fstrict-aliasing. */
9115 flag_strict_aliasing = saved_flag_strict_aliasing;
9117 optimize_args->truncate (0);
9118 return ret;
9121 /* For handling "optimize" attribute. arguments as in
9122 struct attribute_spec.handler. */
9124 static tree
9125 handle_optimize_attribute (tree *node, tree name, tree args,
9126 int ARG_UNUSED (flags), bool *no_add_attrs)
9128 /* Ensure we have a function type. */
9129 if (TREE_CODE (*node) != FUNCTION_DECL)
9131 warning (OPT_Wattributes, "%qE attribute ignored", name);
9132 *no_add_attrs = true;
9134 else
9136 struct cl_optimization cur_opts;
9137 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9139 /* Save current options. */
9140 cl_optimization_save (&cur_opts, &global_options);
9142 /* If we previously had some optimization options, use them as the
9143 default. */
9144 if (old_opts)
9145 cl_optimization_restore (&global_options,
9146 TREE_OPTIMIZATION (old_opts));
9148 /* Parse options, and update the vector. */
9149 parse_optimize_options (args, true);
9150 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9151 = build_optimization_node (&global_options);
9153 /* Restore current options. */
9154 cl_optimization_restore (&global_options, &cur_opts);
9157 return NULL_TREE;
9160 /* Handle a "no_split_stack" attribute. */
9162 static tree
9163 handle_no_split_stack_attribute (tree *node, tree name,
9164 tree ARG_UNUSED (args),
9165 int ARG_UNUSED (flags),
9166 bool *no_add_attrs)
9168 tree decl = *node;
9170 if (TREE_CODE (decl) != FUNCTION_DECL)
9172 error_at (DECL_SOURCE_LOCATION (decl),
9173 "%qE attribute applies only to functions", name);
9174 *no_add_attrs = true;
9176 else if (DECL_INITIAL (decl))
9178 error_at (DECL_SOURCE_LOCATION (decl),
9179 "can%'t set %qE attribute after definition", name);
9180 *no_add_attrs = true;
9183 return NULL_TREE;
9186 /* Handle a "returns_nonnull" attribute; arguments as in
9187 struct attribute_spec.handler. */
9189 static tree
9190 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9191 bool *no_add_attrs)
9193 // Even without a prototype we still have a return type we can check.
9194 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9196 error ("returns_nonnull attribute on a function not returning a pointer");
9197 *no_add_attrs = true;
9199 return NULL_TREE;
9203 /* Check for valid arguments being passed to a function with FNTYPE.
9204 There are NARGS arguments in the array ARGARRAY. */
9205 void
9206 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9208 /* Check for null being passed in a pointer argument that must be
9209 non-null. We also need to do this if format checking is enabled. */
9211 if (warn_nonnull)
9212 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9214 /* Check for errors in format strings. */
9216 if (warn_format || warn_suggest_attribute_format)
9217 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9219 if (warn_format)
9220 check_function_sentinel (fntype, nargs, argarray);
9223 /* Generic argument checking recursion routine. PARAM is the argument to
9224 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9225 once the argument is resolved. CTX is context for the callback. */
9226 void
9227 check_function_arguments_recurse (void (*callback)
9228 (void *, tree, unsigned HOST_WIDE_INT),
9229 void *ctx, tree param,
9230 unsigned HOST_WIDE_INT param_num)
9232 if (CONVERT_EXPR_P (param)
9233 && (TYPE_PRECISION (TREE_TYPE (param))
9234 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9236 /* Strip coercion. */
9237 check_function_arguments_recurse (callback, ctx,
9238 TREE_OPERAND (param, 0), param_num);
9239 return;
9242 if (TREE_CODE (param) == CALL_EXPR)
9244 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9245 tree attrs;
9246 bool found_format_arg = false;
9248 /* See if this is a call to a known internationalization function
9249 that modifies a format arg. Such a function may have multiple
9250 format_arg attributes (for example, ngettext). */
9252 for (attrs = TYPE_ATTRIBUTES (type);
9253 attrs;
9254 attrs = TREE_CHAIN (attrs))
9255 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9257 tree inner_arg;
9258 tree format_num_expr;
9259 int format_num;
9260 int i;
9261 call_expr_arg_iterator iter;
9263 /* Extract the argument number, which was previously checked
9264 to be valid. */
9265 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9267 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9268 && !TREE_INT_CST_HIGH (format_num_expr));
9270 format_num = TREE_INT_CST_LOW (format_num_expr);
9272 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9273 inner_arg != 0;
9274 inner_arg = next_call_expr_arg (&iter), i++)
9275 if (i == format_num)
9277 check_function_arguments_recurse (callback, ctx,
9278 inner_arg, param_num);
9279 found_format_arg = true;
9280 break;
9284 /* If we found a format_arg attribute and did a recursive check,
9285 we are done with checking this argument. Otherwise, we continue
9286 and this will be considered a non-literal. */
9287 if (found_format_arg)
9288 return;
9291 if (TREE_CODE (param) == COND_EXPR)
9293 /* Check both halves of the conditional expression. */
9294 check_function_arguments_recurse (callback, ctx,
9295 TREE_OPERAND (param, 1), param_num);
9296 check_function_arguments_recurse (callback, ctx,
9297 TREE_OPERAND (param, 2), param_num);
9298 return;
9301 (*callback) (ctx, param, param_num);
9304 /* Checks for a builtin function FNDECL that the number of arguments
9305 NARGS against the required number REQUIRED and issues an error if
9306 there is a mismatch. Returns true if the number of arguments is
9307 correct, otherwise false. */
9309 static bool
9310 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9312 if (nargs < required)
9314 error_at (input_location,
9315 "not enough arguments to function %qE", fndecl);
9316 return false;
9318 else if (nargs > required)
9320 error_at (input_location,
9321 "too many arguments to function %qE", fndecl);
9322 return false;
9324 return true;
9327 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9328 Returns false if there was an error, otherwise true. */
9330 bool
9331 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9333 if (!DECL_BUILT_IN (fndecl)
9334 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9335 return true;
9337 switch (DECL_FUNCTION_CODE (fndecl))
9339 case BUILT_IN_CONSTANT_P:
9340 return builtin_function_validate_nargs (fndecl, nargs, 1);
9342 case BUILT_IN_ISFINITE:
9343 case BUILT_IN_ISINF:
9344 case BUILT_IN_ISINF_SIGN:
9345 case BUILT_IN_ISNAN:
9346 case BUILT_IN_ISNORMAL:
9347 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9349 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9351 error ("non-floating-point argument in call to "
9352 "function %qE", fndecl);
9353 return false;
9355 return true;
9357 return false;
9359 case BUILT_IN_ISGREATER:
9360 case BUILT_IN_ISGREATEREQUAL:
9361 case BUILT_IN_ISLESS:
9362 case BUILT_IN_ISLESSEQUAL:
9363 case BUILT_IN_ISLESSGREATER:
9364 case BUILT_IN_ISUNORDERED:
9365 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9367 enum tree_code code0, code1;
9368 code0 = TREE_CODE (TREE_TYPE (args[0]));
9369 code1 = TREE_CODE (TREE_TYPE (args[1]));
9370 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9371 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9372 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9374 error ("non-floating-point arguments in call to "
9375 "function %qE", fndecl);
9376 return false;
9378 return true;
9380 return false;
9382 case BUILT_IN_FPCLASSIFY:
9383 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9385 unsigned i;
9387 for (i=0; i<5; i++)
9388 if (TREE_CODE (args[i]) != INTEGER_CST)
9390 error ("non-const integer argument %u in call to function %qE",
9391 i+1, fndecl);
9392 return false;
9395 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9397 error ("non-floating-point argument in call to function %qE",
9398 fndecl);
9399 return false;
9401 return true;
9403 return false;
9405 case BUILT_IN_ASSUME_ALIGNED:
9406 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9408 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9410 error ("non-integer argument 3 in call to function %qE", fndecl);
9411 return false;
9413 return true;
9415 return false;
9417 default:
9418 return true;
9422 /* Function to help qsort sort FIELD_DECLs by name order. */
9425 field_decl_cmp (const void *x_p, const void *y_p)
9427 const tree *const x = (const tree *const) x_p;
9428 const tree *const y = (const tree *const) y_p;
9430 if (DECL_NAME (*x) == DECL_NAME (*y))
9431 /* A nontype is "greater" than a type. */
9432 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9433 if (DECL_NAME (*x) == NULL_TREE)
9434 return -1;
9435 if (DECL_NAME (*y) == NULL_TREE)
9436 return 1;
9437 if (DECL_NAME (*x) < DECL_NAME (*y))
9438 return -1;
9439 return 1;
9442 static struct {
9443 gt_pointer_operator new_value;
9444 void *cookie;
9445 } resort_data;
9447 /* This routine compares two fields like field_decl_cmp but using the
9448 pointer operator in resort_data. */
9450 static int
9451 resort_field_decl_cmp (const void *x_p, const void *y_p)
9453 const tree *const x = (const tree *const) x_p;
9454 const tree *const y = (const tree *const) y_p;
9456 if (DECL_NAME (*x) == DECL_NAME (*y))
9457 /* A nontype is "greater" than a type. */
9458 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9459 if (DECL_NAME (*x) == NULL_TREE)
9460 return -1;
9461 if (DECL_NAME (*y) == NULL_TREE)
9462 return 1;
9464 tree d1 = DECL_NAME (*x);
9465 tree d2 = DECL_NAME (*y);
9466 resort_data.new_value (&d1, resort_data.cookie);
9467 resort_data.new_value (&d2, resort_data.cookie);
9468 if (d1 < d2)
9469 return -1;
9471 return 1;
9474 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9476 void
9477 resort_sorted_fields (void *obj,
9478 void * ARG_UNUSED (orig_obj),
9479 gt_pointer_operator new_value,
9480 void *cookie)
9482 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9483 resort_data.new_value = new_value;
9484 resort_data.cookie = cookie;
9485 qsort (&sf->elts[0], sf->len, sizeof (tree),
9486 resort_field_decl_cmp);
9489 /* Subroutine of c_parse_error.
9490 Return the result of concatenating LHS and RHS. RHS is really
9491 a string literal, its first character is indicated by RHS_START and
9492 RHS_SIZE is its length (including the terminating NUL character).
9494 The caller is responsible for deleting the returned pointer. */
9496 static char *
9497 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9499 const int lhs_size = strlen (lhs);
9500 char *result = XNEWVEC (char, lhs_size + rhs_size);
9501 strncpy (result, lhs, lhs_size);
9502 strncpy (result + lhs_size, rhs_start, rhs_size);
9503 return result;
9506 /* Issue the error given by GMSGID, indicating that it occurred before
9507 TOKEN, which had the associated VALUE. */
9509 void
9510 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9511 tree value, unsigned char token_flags)
9513 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9515 char *message = NULL;
9517 if (token_type == CPP_EOF)
9518 message = catenate_messages (gmsgid, " at end of input");
9519 else if (token_type == CPP_CHAR
9520 || token_type == CPP_WCHAR
9521 || token_type == CPP_CHAR16
9522 || token_type == CPP_CHAR32)
9524 unsigned int val = TREE_INT_CST_LOW (value);
9525 const char *prefix;
9527 switch (token_type)
9529 default:
9530 prefix = "";
9531 break;
9532 case CPP_WCHAR:
9533 prefix = "L";
9534 break;
9535 case CPP_CHAR16:
9536 prefix = "u";
9537 break;
9538 case CPP_CHAR32:
9539 prefix = "U";
9540 break;
9543 if (val <= UCHAR_MAX && ISGRAPH (val))
9544 message = catenate_messages (gmsgid, " before %s'%c'");
9545 else
9546 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9548 error (message, prefix, val);
9549 free (message);
9550 message = NULL;
9552 else if (token_type == CPP_CHAR_USERDEF
9553 || token_type == CPP_WCHAR_USERDEF
9554 || token_type == CPP_CHAR16_USERDEF
9555 || token_type == CPP_CHAR32_USERDEF)
9556 message = catenate_messages (gmsgid,
9557 " before user-defined character literal");
9558 else if (token_type == CPP_STRING_USERDEF
9559 || token_type == CPP_WSTRING_USERDEF
9560 || token_type == CPP_STRING16_USERDEF
9561 || token_type == CPP_STRING32_USERDEF
9562 || token_type == CPP_UTF8STRING_USERDEF)
9563 message = catenate_messages (gmsgid, " before user-defined string literal");
9564 else if (token_type == CPP_STRING
9565 || token_type == CPP_WSTRING
9566 || token_type == CPP_STRING16
9567 || token_type == CPP_STRING32
9568 || token_type == CPP_UTF8STRING)
9569 message = catenate_messages (gmsgid, " before string constant");
9570 else if (token_type == CPP_NUMBER)
9571 message = catenate_messages (gmsgid, " before numeric constant");
9572 else if (token_type == CPP_NAME)
9574 message = catenate_messages (gmsgid, " before %qE");
9575 error (message, value);
9576 free (message);
9577 message = NULL;
9579 else if (token_type == CPP_PRAGMA)
9580 message = catenate_messages (gmsgid, " before %<#pragma%>");
9581 else if (token_type == CPP_PRAGMA_EOL)
9582 message = catenate_messages (gmsgid, " before end of line");
9583 else if (token_type == CPP_DECLTYPE)
9584 message = catenate_messages (gmsgid, " before %<decltype%>");
9585 else if (token_type < N_TTYPES)
9587 message = catenate_messages (gmsgid, " before %qs token");
9588 error (message, cpp_type2name (token_type, token_flags));
9589 free (message);
9590 message = NULL;
9592 else
9593 error (gmsgid);
9595 if (message)
9597 error (message);
9598 free (message);
9600 #undef catenate_messages
9603 /* Mapping for cpp message reasons to the options that enable them. */
9605 struct reason_option_codes_t
9607 const int reason; /* cpplib message reason. */
9608 const int option_code; /* gcc option that controls this message. */
9611 static const struct reason_option_codes_t option_codes[] = {
9612 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9613 {CPP_W_COMMENTS, OPT_Wcomment},
9614 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9615 {CPP_W_MULTICHAR, OPT_Wmultichar},
9616 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9617 {CPP_W_LONG_LONG, OPT_Wlong_long},
9618 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9619 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9620 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9621 {CPP_W_UNDEF, OPT_Wundef},
9622 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9623 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9624 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9625 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9626 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9627 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9628 {CPP_W_NONE, 0}
9631 /* Return the gcc option code associated with the reason for a cpp
9632 message, or 0 if none. */
9634 static int
9635 c_option_controlling_cpp_error (int reason)
9637 const struct reason_option_codes_t *entry;
9639 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9641 if (entry->reason == reason)
9642 return entry->option_code;
9644 return 0;
9647 /* Callback from cpp_error for PFILE to print diagnostics from the
9648 preprocessor. The diagnostic is of type LEVEL, with REASON set
9649 to the reason code if LEVEL is represents a warning, at location
9650 LOCATION unless this is after lexing and the compiler's location
9651 should be used instead, with column number possibly overridden by
9652 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9653 the arguments. Returns true if a diagnostic was emitted, false
9654 otherwise. */
9656 bool
9657 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9658 location_t location, unsigned int column_override,
9659 const char *msg, va_list *ap)
9661 diagnostic_info diagnostic;
9662 diagnostic_t dlevel;
9663 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9664 bool ret;
9666 switch (level)
9668 case CPP_DL_WARNING_SYSHDR:
9669 if (flag_no_output)
9670 return false;
9671 global_dc->dc_warn_system_headers = 1;
9672 /* Fall through. */
9673 case CPP_DL_WARNING:
9674 if (flag_no_output)
9675 return false;
9676 dlevel = DK_WARNING;
9677 break;
9678 case CPP_DL_PEDWARN:
9679 if (flag_no_output && !flag_pedantic_errors)
9680 return false;
9681 dlevel = DK_PEDWARN;
9682 break;
9683 case CPP_DL_ERROR:
9684 dlevel = DK_ERROR;
9685 break;
9686 case CPP_DL_ICE:
9687 dlevel = DK_ICE;
9688 break;
9689 case CPP_DL_NOTE:
9690 dlevel = DK_NOTE;
9691 break;
9692 case CPP_DL_FATAL:
9693 dlevel = DK_FATAL;
9694 break;
9695 default:
9696 gcc_unreachable ();
9698 if (done_lexing)
9699 location = input_location;
9700 diagnostic_set_info_translated (&diagnostic, msg, ap,
9701 location, dlevel);
9702 if (column_override)
9703 diagnostic_override_column (&diagnostic, column_override);
9704 diagnostic_override_option_index (&diagnostic,
9705 c_option_controlling_cpp_error (reason));
9706 ret = report_diagnostic (&diagnostic);
9707 if (level == CPP_DL_WARNING_SYSHDR)
9708 global_dc->dc_warn_system_headers = save_warn_system_headers;
9709 return ret;
9712 /* Convert a character from the host to the target execution character
9713 set. cpplib handles this, mostly. */
9715 HOST_WIDE_INT
9716 c_common_to_target_charset (HOST_WIDE_INT c)
9718 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9719 zero-extended under -fno-signed-char. cpplib insists that characters
9720 and character constants are always unsigned. Hence we must convert
9721 back and forth. */
9722 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9724 uc = cpp_host_to_exec_charset (parse_in, uc);
9726 if (flag_signed_char)
9727 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9728 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9729 else
9730 return uc;
9733 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9734 references with an INDIRECT_REF of a constant at the bottom; much like the
9735 traditional rendering of offsetof as a macro. Return the folded result. */
9737 tree
9738 fold_offsetof_1 (tree expr)
9740 tree base, off, t;
9742 switch (TREE_CODE (expr))
9744 case ERROR_MARK:
9745 return expr;
9747 case VAR_DECL:
9748 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9749 return error_mark_node;
9751 case CALL_EXPR:
9752 case TARGET_EXPR:
9753 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9754 return error_mark_node;
9756 case NOP_EXPR:
9757 case INDIRECT_REF:
9758 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9760 error ("cannot apply %<offsetof%> to a non constant address");
9761 return error_mark_node;
9763 return TREE_OPERAND (expr, 0);
9765 case COMPONENT_REF:
9766 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9767 if (base == error_mark_node)
9768 return base;
9770 t = TREE_OPERAND (expr, 1);
9771 if (DECL_C_BIT_FIELD (t))
9773 error ("attempt to take address of bit-field structure "
9774 "member %qD", t);
9775 return error_mark_node;
9777 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9778 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9780 / BITS_PER_UNIT));
9781 break;
9783 case ARRAY_REF:
9784 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9785 if (base == error_mark_node)
9786 return base;
9788 t = TREE_OPERAND (expr, 1);
9790 /* Check if the offset goes beyond the upper bound of the array. */
9791 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9793 tree upbound = array_ref_up_bound (expr);
9794 if (upbound != NULL_TREE
9795 && TREE_CODE (upbound) == INTEGER_CST
9796 && !tree_int_cst_equal (upbound,
9797 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9799 upbound = size_binop (PLUS_EXPR, upbound,
9800 build_int_cst (TREE_TYPE (upbound), 1));
9801 if (tree_int_cst_lt (upbound, t))
9803 tree v;
9805 for (v = TREE_OPERAND (expr, 0);
9806 TREE_CODE (v) == COMPONENT_REF;
9807 v = TREE_OPERAND (v, 0))
9808 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9809 == RECORD_TYPE)
9811 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9812 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9813 if (TREE_CODE (fld_chain) == FIELD_DECL)
9814 break;
9816 if (fld_chain)
9817 break;
9819 /* Don't warn if the array might be considered a poor
9820 man's flexible array member with a very permissive
9821 definition thereof. */
9822 if (TREE_CODE (v) == ARRAY_REF
9823 || TREE_CODE (v) == COMPONENT_REF)
9824 warning (OPT_Warray_bounds,
9825 "index %E denotes an offset "
9826 "greater than size of %qT",
9827 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9832 t = convert (sizetype, t);
9833 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9834 break;
9836 case COMPOUND_EXPR:
9837 /* Handle static members of volatile structs. */
9838 t = TREE_OPERAND (expr, 1);
9839 gcc_assert (TREE_CODE (t) == VAR_DECL);
9840 return fold_offsetof_1 (t);
9842 default:
9843 gcc_unreachable ();
9846 return fold_build_pointer_plus (base, off);
9849 /* Likewise, but convert it to the return type of offsetof. */
9851 tree
9852 fold_offsetof (tree expr)
9854 return convert (size_type_node, fold_offsetof_1 (expr));
9857 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9858 expression, because B will always be true. */
9860 void
9861 warn_for_omitted_condop (location_t location, tree cond)
9863 if (truth_value_p (TREE_CODE (cond)))
9864 warning_at (location, OPT_Wparentheses,
9865 "the omitted middle operand in ?: will always be %<true%>, "
9866 "suggest explicit middle operand");
9869 /* Give an error for storing into ARG, which is 'const'. USE indicates
9870 how ARG was being used. */
9872 void
9873 readonly_error (tree arg, enum lvalue_use use)
9875 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9876 || use == lv_asm);
9877 /* Using this macro rather than (for example) arrays of messages
9878 ensures that all the format strings are checked at compile
9879 time. */
9880 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9881 : (use == lv_increment ? (I) \
9882 : (use == lv_decrement ? (D) : (AS))))
9883 if (TREE_CODE (arg) == COMPONENT_REF)
9885 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9886 error (READONLY_MSG (G_("assignment of member "
9887 "%qD in read-only object"),
9888 G_("increment of member "
9889 "%qD in read-only object"),
9890 G_("decrement of member "
9891 "%qD in read-only object"),
9892 G_("member %qD in read-only object "
9893 "used as %<asm%> output")),
9894 TREE_OPERAND (arg, 1));
9895 else
9896 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9897 G_("increment of read-only member %qD"),
9898 G_("decrement of read-only member %qD"),
9899 G_("read-only member %qD used as %<asm%> output")),
9900 TREE_OPERAND (arg, 1));
9902 else if (TREE_CODE (arg) == VAR_DECL)
9903 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9904 G_("increment of read-only variable %qD"),
9905 G_("decrement of read-only variable %qD"),
9906 G_("read-only variable %qD used as %<asm%> output")),
9907 arg);
9908 else if (TREE_CODE (arg) == PARM_DECL)
9909 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9910 G_("increment of read-only parameter %qD"),
9911 G_("decrement of read-only parameter %qD"),
9912 G_("read-only parameter %qD use as %<asm%> output")),
9913 arg);
9914 else if (TREE_CODE (arg) == RESULT_DECL)
9916 gcc_assert (c_dialect_cxx ());
9917 error (READONLY_MSG (G_("assignment of "
9918 "read-only named return value %qD"),
9919 G_("increment of "
9920 "read-only named return value %qD"),
9921 G_("decrement of "
9922 "read-only named return value %qD"),
9923 G_("read-only named return value %qD "
9924 "used as %<asm%>output")),
9925 arg);
9927 else if (TREE_CODE (arg) == FUNCTION_DECL)
9928 error (READONLY_MSG (G_("assignment of function %qD"),
9929 G_("increment of function %qD"),
9930 G_("decrement of function %qD"),
9931 G_("function %qD used as %<asm%> output")),
9932 arg);
9933 else
9934 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9935 G_("increment of read-only location %qE"),
9936 G_("decrement of read-only location %qE"),
9937 G_("read-only location %qE used as %<asm%> output")),
9938 arg);
9941 /* Print an error message for an invalid lvalue. USE says
9942 how the lvalue is being used and so selects the error message. LOC
9943 is the location for the error. */
9945 void
9946 lvalue_error (location_t loc, enum lvalue_use use)
9948 switch (use)
9950 case lv_assign:
9951 error_at (loc, "lvalue required as left operand of assignment");
9952 break;
9953 case lv_increment:
9954 error_at (loc, "lvalue required as increment operand");
9955 break;
9956 case lv_decrement:
9957 error_at (loc, "lvalue required as decrement operand");
9958 break;
9959 case lv_addressof:
9960 error_at (loc, "lvalue required as unary %<&%> operand");
9961 break;
9962 case lv_asm:
9963 error_at (loc, "lvalue required in asm statement");
9964 break;
9965 default:
9966 gcc_unreachable ();
9970 /* Print an error message for an invalid indirection of type TYPE.
9971 ERRSTRING is the name of the operator for the indirection. */
9973 void
9974 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9976 switch (errstring)
9978 case RO_NULL:
9979 gcc_assert (c_dialect_cxx ());
9980 error_at (loc, "invalid type argument (have %qT)", type);
9981 break;
9982 case RO_ARRAY_INDEXING:
9983 error_at (loc,
9984 "invalid type argument of array indexing (have %qT)",
9985 type);
9986 break;
9987 case RO_UNARY_STAR:
9988 error_at (loc,
9989 "invalid type argument of unary %<*%> (have %qT)",
9990 type);
9991 break;
9992 case RO_ARROW:
9993 error_at (loc,
9994 "invalid type argument of %<->%> (have %qT)",
9995 type);
9996 break;
9997 case RO_IMPLICIT_CONVERSION:
9998 error_at (loc,
9999 "invalid type argument of implicit conversion (have %qT)",
10000 type);
10001 break;
10002 default:
10003 gcc_unreachable ();
10007 /* *PTYPE is an incomplete array. Complete it with a domain based on
10008 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10009 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10010 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10013 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10015 tree maxindex, type, main_type, elt, unqual_elt;
10016 int failure = 0, quals;
10017 hashval_t hashcode = 0;
10018 bool overflow_p = false;
10020 maxindex = size_zero_node;
10021 if (initial_value)
10023 if (TREE_CODE (initial_value) == STRING_CST)
10025 int eltsize
10026 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10027 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10029 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10031 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10033 if (vec_safe_is_empty (v))
10035 if (pedantic)
10036 failure = 3;
10037 maxindex = ssize_int (-1);
10039 else
10041 tree curindex;
10042 unsigned HOST_WIDE_INT cnt;
10043 constructor_elt *ce;
10044 bool fold_p = false;
10046 if ((*v)[0].index)
10047 maxindex = (*v)[0].index, fold_p = true;
10049 curindex = maxindex;
10051 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10053 bool curfold_p = false;
10054 if (ce->index)
10055 curindex = ce->index, curfold_p = true;
10056 else
10058 if (fold_p)
10060 /* Since we treat size types now as ordinary
10061 unsigned types, we need an explicit overflow
10062 check. */
10063 tree orig = curindex;
10064 curindex = fold_convert (sizetype, curindex);
10065 overflow_p |= tree_int_cst_lt (curindex, orig);
10067 curindex = size_binop (PLUS_EXPR, curindex,
10068 size_one_node);
10070 if (tree_int_cst_lt (maxindex, curindex))
10071 maxindex = curindex, fold_p = curfold_p;
10073 if (fold_p)
10075 tree orig = maxindex;
10076 maxindex = fold_convert (sizetype, maxindex);
10077 overflow_p |= tree_int_cst_lt (maxindex, orig);
10081 else
10083 /* Make an error message unless that happened already. */
10084 if (initial_value != error_mark_node)
10085 failure = 1;
10088 else
10090 failure = 2;
10091 if (!do_default)
10092 return failure;
10095 type = *ptype;
10096 /* Force an indefinite layout factor. */
10097 if (upc_shared_type_p (type))
10098 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
10099 elt = TREE_TYPE (type);
10100 quals = TYPE_QUALS (strip_array_types (elt));
10101 if (quals == 0)
10102 unqual_elt = elt;
10103 else
10104 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10106 /* Using build_distinct_type_copy and modifying things afterward instead
10107 of using build_array_type to create a new type preserves all of the
10108 TYPE_LANG_FLAG_? bits that the front end may have set. */
10109 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10110 TREE_TYPE (main_type) = unqual_elt;
10111 TYPE_DOMAIN (main_type)
10112 = build_range_type (TREE_TYPE (maxindex),
10113 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10114 layout_type (main_type);
10116 /* Make sure we have the canonical MAIN_TYPE. */
10117 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10118 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10119 hashcode);
10120 main_type = type_hash_canon (hashcode, main_type);
10122 /* Fix the canonical type. */
10123 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10124 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10125 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10126 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10127 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10128 != TYPE_DOMAIN (main_type)))
10129 TYPE_CANONICAL (main_type)
10130 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10131 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10132 else
10133 TYPE_CANONICAL (main_type) = main_type;
10135 if (quals == 0)
10136 type = main_type;
10137 else
10138 type = c_build_qualified_type (main_type, quals);
10140 if (COMPLETE_TYPE_P (type)
10141 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10142 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10144 error ("size of array is too large");
10145 /* If we proceed with the array type as it is, we'll eventually
10146 crash in tree_low_cst(). */
10147 type = error_mark_node;
10150 *ptype = type;
10151 return failure;
10154 /* Like c_mark_addressable but don't check register qualifier. */
10155 void
10156 c_common_mark_addressable_vec (tree t)
10158 while (handled_component_p (t))
10159 t = TREE_OPERAND (t, 0);
10160 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10161 return;
10162 TREE_ADDRESSABLE (t) = 1;
10167 /* Used to help initialize the builtin-types.def table. When a type of
10168 the correct size doesn't exist, use error_mark_node instead of NULL.
10169 The later results in segfaults even when a decl using the type doesn't
10170 get invoked. */
10172 tree
10173 builtin_type_for_size (int size, bool unsignedp)
10175 tree type = c_common_type_for_size (size, unsignedp);
10176 return type ? type : error_mark_node;
10179 /* A helper function for resolve_overloaded_builtin in resolving the
10180 overloaded __sync_ builtins. Returns a positive power of 2 if the
10181 first operand of PARAMS is a pointer to a supported data type.
10182 Returns 0 if an error is encountered. */
10184 static int
10185 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10187 tree type;
10188 int size;
10190 if (!params)
10192 error ("too few arguments to function %qE", function);
10193 return 0;
10196 type = TREE_TYPE ((*params)[0]);
10197 if (TREE_CODE (type) != POINTER_TYPE)
10198 goto incompatible;
10200 type = TREE_TYPE (type);
10201 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10202 goto incompatible;
10204 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10205 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10206 return size;
10208 incompatible:
10209 error ("incompatible type for argument %d of %qE", 1, function);
10210 return 0;
10213 /* A helper function for resolve_overloaded_builtin. Adds casts to
10214 PARAMS to make arguments match up with those of FUNCTION. Drops
10215 the variadic arguments at the end. Returns false if some error
10216 was encountered; true on success. */
10218 static bool
10219 sync_resolve_params (location_t loc, tree orig_function, tree function,
10220 vec<tree, va_gc> *params, bool orig_format)
10222 function_args_iterator iter;
10223 tree ptype;
10224 unsigned int parmnum;
10226 function_args_iter_init (&iter, TREE_TYPE (function));
10227 /* We've declared the implementation functions to use "volatile void *"
10228 as the pointer parameter, so we shouldn't get any complaints from the
10229 call to check_function_arguments what ever type the user used. */
10230 function_args_iter_next (&iter);
10231 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10233 /* For the rest of the values, we need to cast these to FTYPE, so that we
10234 don't get warnings for passing pointer types, etc. */
10235 parmnum = 0;
10236 while (1)
10238 tree val, arg_type;
10240 arg_type = function_args_iter_cond (&iter);
10241 /* XXX void_type_node belies the abstraction. */
10242 if (arg_type == void_type_node)
10243 break;
10245 ++parmnum;
10246 if (params->length () <= parmnum)
10248 error_at (loc, "too few arguments to function %qE", orig_function);
10249 return false;
10252 /* Only convert parameters if arg_type is unsigned integer type with
10253 new format sync routines, i.e. don't attempt to convert pointer
10254 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10255 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10256 kinds). */
10257 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10259 /* Ideally for the first conversion we'd use convert_for_assignment
10260 so that we get warnings for anything that doesn't match the pointer
10261 type. This isn't portable across the C and C++ front ends atm. */
10262 val = (*params)[parmnum];
10263 val = convert (ptype, val);
10264 val = convert (arg_type, val);
10265 (*params)[parmnum] = val;
10268 function_args_iter_next (&iter);
10271 /* __atomic routines are not variadic. */
10272 if (!orig_format && params->length () != parmnum + 1)
10274 error_at (loc, "too many arguments to function %qE", orig_function);
10275 return false;
10278 /* The definition of these primitives is variadic, with the remaining
10279 being "an optional list of variables protected by the memory barrier".
10280 No clue what that's supposed to mean, precisely, but we consider all
10281 call-clobbered variables to be protected so we're safe. */
10282 params->truncate (parmnum + 1);
10284 return true;
10287 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10288 RESULT to make it match the type of the first pointer argument in
10289 PARAMS. */
10291 static tree
10292 sync_resolve_return (tree first_param, tree result, bool orig_format)
10294 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10295 tree rtype = TREE_TYPE (result);
10296 ptype = TYPE_MAIN_VARIANT (ptype);
10298 /* New format doesn't require casting unless the types are the same size. */
10299 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10300 return convert (ptype, result);
10301 else
10302 return result;
10305 /* This function verifies the PARAMS to generic atomic FUNCTION.
10306 It returns the size if all the parameters are the same size, otherwise
10307 0 is returned if the parameters are invalid. */
10309 static int
10310 get_atomic_generic_size (location_t loc, tree function,
10311 vec<tree, va_gc> *params)
10313 unsigned int n_param;
10314 unsigned int n_model;
10315 unsigned int x;
10316 int size_0;
10317 tree type_0;
10319 /* Determine the parameter makeup. */
10320 switch (DECL_FUNCTION_CODE (function))
10322 case BUILT_IN_ATOMIC_EXCHANGE:
10323 n_param = 4;
10324 n_model = 1;
10325 break;
10326 case BUILT_IN_ATOMIC_LOAD:
10327 case BUILT_IN_ATOMIC_STORE:
10328 n_param = 3;
10329 n_model = 1;
10330 break;
10331 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10332 n_param = 6;
10333 n_model = 2;
10334 break;
10335 default:
10336 gcc_unreachable ();
10339 if (vec_safe_length (params) != n_param)
10341 error_at (loc, "incorrect number of arguments to function %qE", function);
10342 return 0;
10345 /* Get type of first parameter, and determine its size. */
10346 type_0 = TREE_TYPE ((*params)[0]);
10347 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10349 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10350 function);
10351 return 0;
10354 /* Types must be compile time constant sizes. */
10355 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10357 error_at (loc,
10358 "argument 1 of %qE must be a pointer to a constant size type",
10359 function);
10360 return 0;
10363 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10365 /* Zero size objects are not allowed. */
10366 if (size_0 == 0)
10368 error_at (loc,
10369 "argument 1 of %qE must be a pointer to a nonzero size object",
10370 function);
10371 return 0;
10374 /* Check each other parameter is a pointer and the same size. */
10375 for (x = 0; x < n_param - n_model; x++)
10377 int size;
10378 tree type = TREE_TYPE ((*params)[x]);
10379 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10380 if (n_param == 6 && x == 3)
10381 continue;
10382 if (!POINTER_TYPE_P (type))
10384 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10385 function);
10386 return 0;
10388 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10389 if (size != size_0)
10391 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10392 function);
10393 return 0;
10397 /* Check memory model parameters for validity. */
10398 for (x = n_param - n_model ; x < n_param; x++)
10400 tree p = (*params)[x];
10401 if (TREE_CODE (p) == INTEGER_CST)
10403 int i = tree_low_cst (p, 1);
10404 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10406 warning_at (loc, OPT_Winvalid_memory_model,
10407 "invalid memory model argument %d of %qE", x + 1,
10408 function);
10411 else
10412 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10414 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10415 function);
10416 return 0;
10420 return size_0;
10424 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10425 at the beginning of the parameter list PARAMS representing the size of the
10426 objects. This is to match the library ABI requirement. LOC is the location
10427 of the function call.
10428 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10429 returned to allow the external call to be constructed. */
10431 static tree
10432 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10433 vec<tree, va_gc> *params)
10435 tree size_node;
10437 /* Insert a SIZE_T parameter as the first param. If there isn't
10438 enough space, allocate a new vector and recursively re-build with that. */
10439 if (!params->space (1))
10441 unsigned int z, len;
10442 vec<tree, va_gc> *v;
10443 tree f;
10445 len = params->length ();
10446 vec_alloc (v, len + 1);
10447 for (z = 0; z < len; z++)
10448 v->quick_push ((*params)[z]);
10449 f = build_function_call_vec (loc, function, v, NULL);
10450 vec_free (v);
10451 return f;
10454 /* Add the size parameter and leave as a function call for processing. */
10455 size_node = build_int_cst (size_type_node, n);
10456 params->quick_insert (0, size_node);
10457 return NULL_TREE;
10461 /* This will process an __atomic_exchange function call, determine whether it
10462 needs to be mapped to the _N variation, or turned into a library call.
10463 LOC is the location of the builtin call.
10464 FUNCTION is the DECL that has been invoked;
10465 PARAMS is the argument list for the call. The return value is non-null
10466 TRUE is returned if it is translated into the proper format for a call to the
10467 external library, and NEW_RETURN is set the tree for that function.
10468 FALSE is returned if processing for the _N variation is required, and
10469 NEW_RETURN is set to the the return value the result is copied into. */
10470 static bool
10471 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10472 vec<tree, va_gc> *params, tree *new_return)
10474 tree p0, p1, p2, p3;
10475 tree I_type, I_type_ptr;
10476 int n = get_atomic_generic_size (loc, function, params);
10478 /* Size of 0 is an error condition. */
10479 if (n == 0)
10481 *new_return = error_mark_node;
10482 return true;
10485 /* If not a lock-free size, change to the library generic format. */
10486 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10488 *new_return = add_atomic_size_parameter (n, loc, function, params);
10489 return true;
10492 /* Otherwise there is a lockfree match, transform the call from:
10493 void fn(T* mem, T* desired, T* return, model)
10494 into
10495 *return = (T) (fn (In* mem, (In) *desired, model)) */
10497 p0 = (*params)[0];
10498 p1 = (*params)[1];
10499 p2 = (*params)[2];
10500 p3 = (*params)[3];
10502 /* Create pointer to appropriate size. */
10503 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10504 I_type_ptr = build_pointer_type (I_type);
10506 /* Convert object pointer to required type. */
10507 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10508 (*params)[0] = p0;
10509 /* Convert new value to required type, and dereference it. */
10510 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10511 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10512 (*params)[1] = p1;
10514 /* Move memory model to the 3rd position, and end param list. */
10515 (*params)[2] = p3;
10516 params->truncate (3);
10518 /* Convert return pointer and dereference it for later assignment. */
10519 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10521 return false;
10525 /* This will process an __atomic_compare_exchange function call, determine
10526 whether it needs to be mapped to the _N variation, or turned into a lib call.
10527 LOC is the location of the builtin call.
10528 FUNCTION is the DECL that has been invoked;
10529 PARAMS is the argument list for the call. The return value is non-null
10530 TRUE is returned if it is translated into the proper format for a call to the
10531 external library, and NEW_RETURN is set the tree for that function.
10532 FALSE is returned if processing for the _N variation is required. */
10534 static bool
10535 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10536 vec<tree, va_gc> *params,
10537 tree *new_return)
10539 tree p0, p1, p2;
10540 tree I_type, I_type_ptr;
10541 int n = get_atomic_generic_size (loc, function, params);
10543 /* Size of 0 is an error condition. */
10544 if (n == 0)
10546 *new_return = error_mark_node;
10547 return true;
10550 /* If not a lock-free size, change to the library generic format. */
10551 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10553 /* The library generic format does not have the weak parameter, so
10554 remove it from the param list. Since a parameter has been removed,
10555 we can be sure that there is room for the SIZE_T parameter, meaning
10556 there will not be a recursive rebuilding of the parameter list, so
10557 there is no danger this will be done twice. */
10558 if (n > 0)
10560 (*params)[3] = (*params)[4];
10561 (*params)[4] = (*params)[5];
10562 params->truncate (5);
10564 *new_return = add_atomic_size_parameter (n, loc, function, params);
10565 return true;
10568 /* Otherwise, there is a match, so the call needs to be transformed from:
10569 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10570 into
10571 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10573 p0 = (*params)[0];
10574 p1 = (*params)[1];
10575 p2 = (*params)[2];
10577 /* Create pointer to appropriate size. */
10578 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10579 I_type_ptr = build_pointer_type (I_type);
10581 /* Convert object pointer to required type. */
10582 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10583 (*params)[0] = p0;
10585 /* Convert expected pointer to required type. */
10586 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10587 (*params)[1] = p1;
10589 /* Convert desired value to required type, and dereference it. */
10590 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10591 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10592 (*params)[2] = p2;
10594 /* The rest of the parameters are fine. NULL means no special return value
10595 processing.*/
10596 *new_return = NULL;
10597 return false;
10601 /* This will process an __atomic_load function call, determine whether it
10602 needs to be mapped to the _N variation, or turned into a library call.
10603 LOC is the location of the builtin call.
10604 FUNCTION is the DECL that has been invoked;
10605 PARAMS is the argument list for the call. The return value is non-null
10606 TRUE is returned if it is translated into the proper format for a call to the
10607 external library, and NEW_RETURN is set the tree for that function.
10608 FALSE is returned if processing for the _N variation is required, and
10609 NEW_RETURN is set to the the return value the result is copied into. */
10611 static bool
10612 resolve_overloaded_atomic_load (location_t loc, tree function,
10613 vec<tree, va_gc> *params, tree *new_return)
10615 tree p0, p1, p2;
10616 tree I_type, I_type_ptr;
10617 int n = get_atomic_generic_size (loc, function, params);
10619 /* Size of 0 is an error condition. */
10620 if (n == 0)
10622 *new_return = error_mark_node;
10623 return true;
10626 /* If not a lock-free size, change to the library generic format. */
10627 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10629 *new_return = add_atomic_size_parameter (n, loc, function, params);
10630 return true;
10633 /* Otherwise, there is a match, so the call needs to be transformed from:
10634 void fn(T* mem, T* return, model)
10635 into
10636 *return = (T) (fn ((In *) mem, model)) */
10638 p0 = (*params)[0];
10639 p1 = (*params)[1];
10640 p2 = (*params)[2];
10642 /* Create pointer to appropriate size. */
10643 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10644 I_type_ptr = build_pointer_type (I_type);
10646 /* Convert object pointer to required type. */
10647 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10648 (*params)[0] = p0;
10650 /* Move memory model to the 2nd position, and end param list. */
10651 (*params)[1] = p2;
10652 params->truncate (2);
10654 /* Convert return pointer and dereference it for later assignment. */
10655 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10657 return false;
10661 /* This will process an __atomic_store function call, determine whether it
10662 needs to be mapped to the _N variation, or turned into a library call.
10663 LOC is the location of the builtin call.
10664 FUNCTION is the DECL that has been invoked;
10665 PARAMS is the argument list for the call. The return value is non-null
10666 TRUE is returned if it is translated into the proper format for a call to the
10667 external library, and NEW_RETURN is set the tree for that function.
10668 FALSE is returned if processing for the _N variation is required, and
10669 NEW_RETURN is set to the the return value the result is copied into. */
10671 static bool
10672 resolve_overloaded_atomic_store (location_t loc, tree function,
10673 vec<tree, va_gc> *params, tree *new_return)
10675 tree p0, p1;
10676 tree I_type, I_type_ptr;
10677 int n = get_atomic_generic_size (loc, function, params);
10679 /* Size of 0 is an error condition. */
10680 if (n == 0)
10682 *new_return = error_mark_node;
10683 return true;
10686 /* If not a lock-free size, change to the library generic format. */
10687 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10689 *new_return = add_atomic_size_parameter (n, loc, function, params);
10690 return true;
10693 /* Otherwise, there is a match, so the call needs to be transformed from:
10694 void fn(T* mem, T* value, model)
10695 into
10696 fn ((In *) mem, (In) *value, model) */
10698 p0 = (*params)[0];
10699 p1 = (*params)[1];
10701 /* Create pointer to appropriate size. */
10702 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10703 I_type_ptr = build_pointer_type (I_type);
10705 /* Convert object pointer to required type. */
10706 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10707 (*params)[0] = p0;
10709 /* Convert new value to required type, and dereference it. */
10710 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10711 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10712 (*params)[1] = p1;
10714 /* The memory model is in the right spot already. Return is void. */
10715 *new_return = NULL_TREE;
10717 return false;
10721 /* Some builtin functions are placeholders for other expressions. This
10722 function should be called immediately after parsing the call expression
10723 before surrounding code has committed to the type of the expression.
10725 LOC is the location of the builtin call.
10727 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10728 PARAMS is the argument list for the call. The return value is non-null
10729 when expansion is complete, and null if normal processing should
10730 continue. */
10732 tree
10733 resolve_overloaded_builtin (location_t loc, tree function,
10734 vec<tree, va_gc> *params)
10736 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10737 bool orig_format = true;
10738 tree new_return = NULL_TREE;
10740 switch (DECL_BUILT_IN_CLASS (function))
10742 case BUILT_IN_NORMAL:
10743 break;
10744 case BUILT_IN_MD:
10745 if (targetm.resolve_overloaded_builtin)
10746 return targetm.resolve_overloaded_builtin (loc, function, params);
10747 else
10748 return NULL_TREE;
10749 default:
10750 return NULL_TREE;
10753 /* Handle BUILT_IN_NORMAL here. */
10754 switch (orig_code)
10756 case BUILT_IN_ATOMIC_EXCHANGE:
10757 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10758 case BUILT_IN_ATOMIC_LOAD:
10759 case BUILT_IN_ATOMIC_STORE:
10761 /* Handle these 4 together so that they can fall through to the next
10762 case if the call is transformed to an _N variant. */
10763 switch (orig_code)
10765 case BUILT_IN_ATOMIC_EXCHANGE:
10767 if (resolve_overloaded_atomic_exchange (loc, function, params,
10768 &new_return))
10769 return new_return;
10770 /* Change to the _N variant. */
10771 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10772 break;
10775 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10777 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10778 params,
10779 &new_return))
10780 return new_return;
10781 /* Change to the _N variant. */
10782 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10783 break;
10785 case BUILT_IN_ATOMIC_LOAD:
10787 if (resolve_overloaded_atomic_load (loc, function, params,
10788 &new_return))
10789 return new_return;
10790 /* Change to the _N variant. */
10791 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10792 break;
10794 case BUILT_IN_ATOMIC_STORE:
10796 if (resolve_overloaded_atomic_store (loc, function, params,
10797 &new_return))
10798 return new_return;
10799 /* Change to the _N variant. */
10800 orig_code = BUILT_IN_ATOMIC_STORE_N;
10801 break;
10803 default:
10804 gcc_unreachable ();
10806 /* Fallthrough to the normal processing. */
10808 case BUILT_IN_ATOMIC_EXCHANGE_N:
10809 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10810 case BUILT_IN_ATOMIC_LOAD_N:
10811 case BUILT_IN_ATOMIC_STORE_N:
10812 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10813 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10814 case BUILT_IN_ATOMIC_AND_FETCH_N:
10815 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10816 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10817 case BUILT_IN_ATOMIC_OR_FETCH_N:
10818 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10819 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10820 case BUILT_IN_ATOMIC_FETCH_AND_N:
10821 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10822 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10823 case BUILT_IN_ATOMIC_FETCH_OR_N:
10825 orig_format = false;
10826 /* Fallthru for parameter processing. */
10828 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10829 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10830 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10831 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10832 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10833 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10834 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10835 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10836 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10837 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10838 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10839 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10840 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10841 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10842 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10843 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10845 int n = sync_resolve_size (function, params);
10846 tree new_function, first_param, result;
10847 enum built_in_function fncode;
10849 if (n == 0)
10850 return error_mark_node;
10852 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10853 new_function = builtin_decl_explicit (fncode);
10854 if (!sync_resolve_params (loc, function, new_function, params,
10855 orig_format))
10856 return error_mark_node;
10858 first_param = (*params)[0];
10859 result = build_function_call_vec (loc, new_function, params, NULL);
10860 if (result == error_mark_node)
10861 return result;
10862 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10863 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10864 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10865 result = sync_resolve_return (first_param, result, orig_format);
10867 /* If new_return is set, assign function to that expr and cast the
10868 result to void since the generic interface returned void. */
10869 if (new_return)
10871 /* Cast function result from I{1,2,4,8,16} to the required type. */
10872 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10873 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10874 result);
10875 TREE_SIDE_EFFECTS (result) = 1;
10876 protected_set_expr_location (result, loc);
10877 result = convert (void_type_node, result);
10879 return result;
10882 default:
10883 return NULL_TREE;
10887 /* vector_types_compatible_elements_p is used in type checks of vectors
10888 values used as operands of binary operators. Where it returns true, and
10889 the other checks of the caller succeed (being vector types in he first
10890 place, and matching number of elements), we can just treat the types
10891 as essentially the same.
10892 Contrast with vector_targets_convertible_p, which is used for vector
10893 pointer types, and vector_types_convertible_p, which will allow
10894 language-specific matches under the control of flag_lax_vector_conversions,
10895 and might still require a conversion. */
10896 /* True if vector types T1 and T2 can be inputs to the same binary
10897 operator without conversion.
10898 We don't check the overall vector size here because some of our callers
10899 want to give different error messages when the vectors are compatible
10900 except for the element count. */
10902 bool
10903 vector_types_compatible_elements_p (tree t1, tree t2)
10905 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10906 t1 = TREE_TYPE (t1);
10907 t2 = TREE_TYPE (t2);
10909 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10911 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10912 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10913 || c2 == FIXED_POINT_TYPE));
10915 t1 = c_common_signed_type (t1);
10916 t2 = c_common_signed_type (t2);
10917 /* Equality works here because c_common_signed_type uses
10918 TYPE_MAIN_VARIANT. */
10919 if (t1 == t2)
10920 return true;
10921 if (opaque && c1 == c2
10922 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10923 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10924 return true;
10925 return false;
10928 /* Check for missing format attributes on function pointers. LTYPE is
10929 the new type or left-hand side type. RTYPE is the old type or
10930 right-hand side type. Returns TRUE if LTYPE is missing the desired
10931 attribute. */
10933 bool
10934 check_missing_format_attribute (tree ltype, tree rtype)
10936 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10937 tree ra;
10939 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10940 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10941 break;
10942 if (ra)
10944 tree la;
10945 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10946 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10947 break;
10948 return !la;
10950 else
10951 return false;
10954 /* Subscripting with type char is likely to lose on a machine where
10955 chars are signed. So warn on any machine, but optionally. Don't
10956 warn for unsigned char since that type is safe. Don't warn for
10957 signed char because anyone who uses that must have done so
10958 deliberately. Furthermore, we reduce the false positive load by
10959 warning only for non-constant value of type char. */
10961 void
10962 warn_array_subscript_with_type_char (tree index)
10964 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10965 && TREE_CODE (index) != INTEGER_CST)
10966 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10969 /* Implement -Wparentheses for the unexpected C precedence rules, to
10970 cover cases like x + y << z which readers are likely to
10971 misinterpret. We have seen an expression in which CODE is a binary
10972 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10973 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10974 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10975 expression was not formed using a binary or unary operator, or it
10976 was enclosed in parentheses. */
10978 void
10979 warn_about_parentheses (location_t loc, enum tree_code code,
10980 enum tree_code code_left, tree arg_left,
10981 enum tree_code code_right, tree arg_right)
10983 if (!warn_parentheses)
10984 return;
10986 /* This macro tests that the expression ARG with original tree code
10987 CODE appears to be a boolean expression. or the result of folding a
10988 boolean expression. */
10989 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10990 (truth_value_p (TREE_CODE (ARG)) \
10991 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10992 /* Folding may create 0 or 1 integers from other expressions. */ \
10993 || ((CODE) != INTEGER_CST \
10994 && (integer_onep (ARG) || integer_zerop (ARG))))
10996 switch (code)
10998 case LSHIFT_EXPR:
10999 if (code_left == PLUS_EXPR)
11000 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11001 "suggest parentheses around %<+%> inside %<<<%>");
11002 else if (code_right == PLUS_EXPR)
11003 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11004 "suggest parentheses around %<+%> inside %<<<%>");
11005 else if (code_left == MINUS_EXPR)
11006 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11007 "suggest parentheses around %<-%> inside %<<<%>");
11008 else if (code_right == MINUS_EXPR)
11009 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11010 "suggest parentheses around %<-%> inside %<<<%>");
11011 return;
11013 case RSHIFT_EXPR:
11014 if (code_left == PLUS_EXPR)
11015 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11016 "suggest parentheses around %<+%> inside %<>>%>");
11017 else if (code_right == PLUS_EXPR)
11018 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11019 "suggest parentheses around %<+%> inside %<>>%>");
11020 else if (code_left == MINUS_EXPR)
11021 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11022 "suggest parentheses around %<-%> inside %<>>%>");
11023 else if (code_right == MINUS_EXPR)
11024 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11025 "suggest parentheses around %<-%> inside %<>>%>");
11026 return;
11028 case TRUTH_ORIF_EXPR:
11029 if (code_left == TRUTH_ANDIF_EXPR)
11030 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11031 "suggest parentheses around %<&&%> within %<||%>");
11032 else if (code_right == TRUTH_ANDIF_EXPR)
11033 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11034 "suggest parentheses around %<&&%> within %<||%>");
11035 return;
11037 case BIT_IOR_EXPR:
11038 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11039 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11040 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11041 "suggest parentheses around arithmetic in operand of %<|%>");
11042 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11043 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11044 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11045 "suggest parentheses around arithmetic in operand of %<|%>");
11046 /* Check cases like x|y==z */
11047 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11048 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11049 "suggest parentheses around comparison in operand of %<|%>");
11050 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11051 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11052 "suggest parentheses around comparison in operand of %<|%>");
11053 /* Check cases like !x | y */
11054 else if (code_left == TRUTH_NOT_EXPR
11055 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11056 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11057 "suggest parentheses around operand of "
11058 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11059 return;
11061 case BIT_XOR_EXPR:
11062 if (code_left == BIT_AND_EXPR
11063 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11064 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11065 "suggest parentheses around arithmetic in operand of %<^%>");
11066 else if (code_right == BIT_AND_EXPR
11067 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11068 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11069 "suggest parentheses around arithmetic in operand of %<^%>");
11070 /* Check cases like x^y==z */
11071 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11072 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11073 "suggest parentheses around comparison in operand of %<^%>");
11074 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11075 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11076 "suggest parentheses around comparison in operand of %<^%>");
11077 return;
11079 case BIT_AND_EXPR:
11080 if (code_left == PLUS_EXPR)
11081 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11082 "suggest parentheses around %<+%> in operand of %<&%>");
11083 else if (code_right == PLUS_EXPR)
11084 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11085 "suggest parentheses around %<+%> in operand of %<&%>");
11086 else if (code_left == MINUS_EXPR)
11087 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11088 "suggest parentheses around %<-%> in operand of %<&%>");
11089 else if (code_right == MINUS_EXPR)
11090 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11091 "suggest parentheses around %<-%> in operand of %<&%>");
11092 /* Check cases like x&y==z */
11093 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11094 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11095 "suggest parentheses around comparison in operand of %<&%>");
11096 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11097 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11098 "suggest parentheses around comparison in operand of %<&%>");
11099 /* Check cases like !x & y */
11100 else if (code_left == TRUTH_NOT_EXPR
11101 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11102 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11103 "suggest parentheses around operand of "
11104 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11105 return;
11107 case EQ_EXPR:
11108 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11109 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11110 "suggest parentheses around comparison in operand of %<==%>");
11111 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11112 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11113 "suggest parentheses around comparison in operand of %<==%>");
11114 return;
11115 case NE_EXPR:
11116 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11117 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11118 "suggest parentheses around comparison in operand of %<!=%>");
11119 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11120 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11121 "suggest parentheses around comparison in operand of %<!=%>");
11122 return;
11124 default:
11125 if (TREE_CODE_CLASS (code) == tcc_comparison)
11127 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11128 && code_left != NE_EXPR && code_left != EQ_EXPR
11129 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11130 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11131 "comparisons like %<X<=Y<=Z%> do not "
11132 "have their mathematical meaning");
11133 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11134 && code_right != NE_EXPR && code_right != EQ_EXPR
11135 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11136 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11137 "comparisons like %<X<=Y<=Z%> do not "
11138 "have their mathematical meaning");
11140 return;
11142 #undef NOT_A_BOOLEAN_EXPR_P
11145 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11147 void
11148 warn_for_unused_label (tree label)
11150 if (!TREE_USED (label))
11152 if (DECL_INITIAL (label))
11153 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11154 else
11155 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11159 /* Warn for division by zero according to the value of DIVISOR. LOC
11160 is the location of the division operator. */
11162 void
11163 warn_for_div_by_zero (location_t loc, tree divisor)
11165 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11166 about division by zero. Do not issue a warning if DIVISOR has a
11167 floating-point type, since we consider 0.0/0.0 a valid way of
11168 generating a NaN. */
11169 if (c_inhibit_evaluation_warnings == 0
11170 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11171 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11174 /* Subroutine of build_binary_op. Give warnings for comparisons
11175 between signed and unsigned quantities that may fail. Do the
11176 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11177 so that casts will be considered, but default promotions won't
11180 LOCATION is the location of the comparison operator.
11182 The arguments of this function map directly to local variables
11183 of build_binary_op. */
11185 void
11186 warn_for_sign_compare (location_t location,
11187 tree orig_op0, tree orig_op1,
11188 tree op0, tree op1,
11189 tree result_type, enum tree_code resultcode)
11191 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11192 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11193 int unsignedp0, unsignedp1;
11195 /* In C++, check for comparison of different enum types. */
11196 if (c_dialect_cxx()
11197 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11198 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11199 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11200 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11202 warning_at (location,
11203 OPT_Wsign_compare, "comparison between types %qT and %qT",
11204 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11207 /* Do not warn if the comparison is being done in a signed type,
11208 since the signed type will only be chosen if it can represent
11209 all the values of the unsigned type. */
11210 if (!TYPE_UNSIGNED (result_type))
11211 /* OK */;
11212 /* Do not warn if both operands are unsigned. */
11213 else if (op0_signed == op1_signed)
11214 /* OK */;
11215 else
11217 tree sop, uop, base_type;
11218 bool ovf;
11220 if (op0_signed)
11221 sop = orig_op0, uop = orig_op1;
11222 else
11223 sop = orig_op1, uop = orig_op0;
11225 STRIP_TYPE_NOPS (sop);
11226 STRIP_TYPE_NOPS (uop);
11227 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11228 ? TREE_TYPE (result_type) : result_type);
11230 /* Do not warn if the signed quantity is an unsuffixed integer
11231 literal (or some static constant expression involving such
11232 literals or a conditional expression involving such literals)
11233 and it is non-negative. */
11234 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11235 /* OK */;
11236 /* Do not warn if the comparison is an equality operation, the
11237 unsigned quantity is an integral constant, and it would fit
11238 in the result if the result were signed. */
11239 else if (TREE_CODE (uop) == INTEGER_CST
11240 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11241 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11242 /* OK */;
11243 /* In C, do not warn if the unsigned quantity is an enumeration
11244 constant and its maximum value would fit in the result if the
11245 result were signed. */
11246 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11247 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11248 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11249 c_common_signed_type (base_type)))
11250 /* OK */;
11251 else
11252 warning_at (location,
11253 OPT_Wsign_compare,
11254 "comparison between signed and unsigned integer expressions");
11257 /* Warn if two unsigned values are being compared in a size larger
11258 than their original size, and one (and only one) is the result of
11259 a `~' operator. This comparison will always fail.
11261 Also warn if one operand is a constant, and the constant does not
11262 have all bits set that are set in the ~ operand when it is
11263 extended. */
11265 op0 = c_common_get_narrower (op0, &unsignedp0);
11266 op1 = c_common_get_narrower (op1, &unsignedp1);
11268 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11269 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11271 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11272 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11273 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11274 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11276 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11278 tree primop;
11279 HOST_WIDE_INT constant, mask;
11280 int unsignedp;
11281 unsigned int bits;
11283 if (host_integerp (op0, 0))
11285 primop = op1;
11286 unsignedp = unsignedp1;
11287 constant = tree_low_cst (op0, 0);
11289 else
11291 primop = op0;
11292 unsignedp = unsignedp0;
11293 constant = tree_low_cst (op1, 0);
11296 bits = TYPE_PRECISION (TREE_TYPE (primop));
11297 if (bits < TYPE_PRECISION (result_type)
11298 && bits < HOST_BITS_PER_LONG && unsignedp)
11300 mask = (~ (HOST_WIDE_INT) 0) << bits;
11301 if ((mask & constant) != mask)
11303 if (constant == 0)
11304 warning (OPT_Wsign_compare,
11305 "promoted ~unsigned is always non-zero");
11306 else
11307 warning_at (location, OPT_Wsign_compare,
11308 "comparison of promoted ~unsigned with constant");
11312 else if (unsignedp0 && unsignedp1
11313 && (TYPE_PRECISION (TREE_TYPE (op0))
11314 < TYPE_PRECISION (result_type))
11315 && (TYPE_PRECISION (TREE_TYPE (op1))
11316 < TYPE_PRECISION (result_type)))
11317 warning_at (location, OPT_Wsign_compare,
11318 "comparison of promoted ~unsigned with unsigned");
11322 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11323 type via c_common_type. If -Wdouble-promotion is in use, and the
11324 conditions for warning have been met, issue a warning. GMSGID is
11325 the warning message. It must have two %T specifiers for the type
11326 that was converted (generally "float") and the type to which it was
11327 converted (generally "double), respectively. LOC is the location
11328 to which the awrning should refer. */
11330 void
11331 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11332 const char *gmsgid, location_t loc)
11334 tree source_type;
11336 if (!warn_double_promotion)
11337 return;
11338 /* If the conversion will not occur at run-time, there is no need to
11339 warn about it. */
11340 if (c_inhibit_evaluation_warnings)
11341 return;
11342 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11343 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11344 return;
11345 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11346 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11347 source_type = type1;
11348 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11349 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11350 source_type = type2;
11351 else
11352 return;
11353 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11356 /* Setup a TYPE_DECL node as a typedef representation.
11358 X is a TYPE_DECL for a typedef statement. Create a brand new
11359 ..._TYPE node (which will be just a variant of the existing
11360 ..._TYPE node with identical properties) and then install X
11361 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11363 The whole point here is to end up with a situation where each
11364 and every ..._TYPE node the compiler creates will be uniquely
11365 associated with AT MOST one node representing a typedef name.
11366 This way, even though the compiler substitutes corresponding
11367 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11368 early on, later parts of the compiler can always do the reverse
11369 translation and get back the corresponding typedef name. For
11370 example, given:
11372 typedef struct S MY_TYPE;
11373 MY_TYPE object;
11375 Later parts of the compiler might only know that `object' was of
11376 type `struct S' if it were not for code just below. With this
11377 code however, later parts of the compiler see something like:
11379 struct S' == struct S
11380 typedef struct S' MY_TYPE;
11381 struct S' object;
11383 And they can then deduce (from the node for type struct S') that
11384 the original object declaration was:
11386 MY_TYPE object;
11388 Being able to do this is important for proper support of protoize,
11389 and also for generating precise symbolic debugging information
11390 which takes full account of the programmer's (typedef) vocabulary.
11392 Obviously, we don't want to generate a duplicate ..._TYPE node if
11393 the TYPE_DECL node that we are now processing really represents a
11394 standard built-in type. */
11396 void
11397 set_underlying_type (tree x)
11399 if (x == error_mark_node)
11400 return;
11401 if (DECL_IS_BUILTIN (x))
11403 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11404 TYPE_NAME (TREE_TYPE (x)) = x;
11406 else if (TREE_TYPE (x) != error_mark_node
11407 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11409 tree tt = TREE_TYPE (x);
11410 DECL_ORIGINAL_TYPE (x) = tt;
11411 tt = build_variant_type_copy (tt);
11412 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11413 TYPE_NAME (tt) = x;
11414 TREE_USED (tt) = TREE_USED (x);
11415 TREE_TYPE (x) = tt;
11419 /* Record the types used by the current global variable declaration
11420 being parsed, so that we can decide later to emit their debug info.
11421 Those types are in types_used_by_cur_var_decl, and we are going to
11422 store them in the types_used_by_vars_hash hash table.
11423 DECL is the declaration of the global variable that has been parsed. */
11425 void
11426 record_types_used_by_current_var_decl (tree decl)
11428 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11430 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11432 tree type = types_used_by_cur_var_decl->pop ();
11433 types_used_by_var_decl_insert (type, decl);
11437 /* If DECL is a typedef that is declared in the current function,
11438 record it for the purpose of -Wunused-local-typedefs. */
11440 void
11441 record_locally_defined_typedef (tree decl)
11443 struct c_language_function *l;
11445 if (!warn_unused_local_typedefs
11446 || cfun == NULL
11447 /* if this is not a locally defined typedef then we are not
11448 interested. */
11449 || !is_typedef_decl (decl)
11450 || !decl_function_context (decl))
11451 return;
11453 l = (struct c_language_function *) cfun->language;
11454 vec_safe_push (l->local_typedefs, decl);
11457 /* If T is a TYPE_DECL declared locally, mark it as used. */
11459 void
11460 maybe_record_typedef_use (tree t)
11462 if (!is_typedef_decl (t))
11463 return;
11465 TREE_USED (t) = true;
11468 /* Warn if there are some unused locally defined typedefs in the
11469 current function. */
11471 void
11472 maybe_warn_unused_local_typedefs (void)
11474 int i;
11475 tree decl;
11476 /* The number of times we have emitted -Wunused-local-typedefs
11477 warnings. If this is different from errorcount, that means some
11478 unrelated errors have been issued. In which case, we'll avoid
11479 emitting "unused-local-typedefs" warnings. */
11480 static int unused_local_typedefs_warn_count;
11481 struct c_language_function *l;
11483 if (cfun == NULL)
11484 return;
11486 if ((l = (struct c_language_function *) cfun->language) == NULL)
11487 return;
11489 if (warn_unused_local_typedefs
11490 && errorcount == unused_local_typedefs_warn_count)
11492 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11493 if (!TREE_USED (decl))
11494 warning_at (DECL_SOURCE_LOCATION (decl),
11495 OPT_Wunused_local_typedefs,
11496 "typedef %qD locally defined but not used", decl);
11497 unused_local_typedefs_warn_count = errorcount;
11500 vec_free (l->local_typedefs);
11503 /* The C and C++ parsers both use vectors to hold function arguments.
11504 For efficiency, we keep a cache of unused vectors. This is the
11505 cache. */
11507 typedef vec<tree, va_gc> *tree_gc_vec;
11508 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11510 /* Return a new vector from the cache. If the cache is empty,
11511 allocate a new vector. These vectors are GC'ed, so it is OK if the
11512 pointer is not released.. */
11514 vec<tree, va_gc> *
11515 make_tree_vector (void)
11517 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11518 return tree_vector_cache->pop ();
11519 else
11521 /* Passing 0 to vec::alloc returns NULL, and our callers require
11522 that we always return a non-NULL value. The vector code uses
11523 4 when growing a NULL vector, so we do too. */
11524 vec<tree, va_gc> *v;
11525 vec_alloc (v, 4);
11526 return v;
11530 /* Release a vector of trees back to the cache. */
11532 void
11533 release_tree_vector (vec<tree, va_gc> *vec)
11535 if (vec != NULL)
11537 vec->truncate (0);
11538 vec_safe_push (tree_vector_cache, vec);
11542 /* Get a new tree vector holding a single tree. */
11544 vec<tree, va_gc> *
11545 make_tree_vector_single (tree t)
11547 vec<tree, va_gc> *ret = make_tree_vector ();
11548 ret->quick_push (t);
11549 return ret;
11552 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11554 vec<tree, va_gc> *
11555 make_tree_vector_from_list (tree list)
11557 vec<tree, va_gc> *ret = make_tree_vector ();
11558 for (; list; list = TREE_CHAIN (list))
11559 vec_safe_push (ret, TREE_VALUE (list));
11560 return ret;
11563 /* Get a new tree vector which is a copy of an existing one. */
11565 vec<tree, va_gc> *
11566 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11568 vec<tree, va_gc> *ret;
11569 unsigned int ix;
11570 tree t;
11572 ret = make_tree_vector ();
11573 vec_safe_reserve (ret, vec_safe_length (orig));
11574 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11575 ret->quick_push (t);
11576 return ret;
11579 /* Return true if KEYWORD starts a type specifier. */
11581 bool
11582 keyword_begins_type_specifier (enum rid keyword)
11584 switch (keyword)
11586 case RID_INT:
11587 case RID_CHAR:
11588 case RID_FLOAT:
11589 case RID_DOUBLE:
11590 case RID_VOID:
11591 case RID_INT128:
11592 case RID_UNSIGNED:
11593 case RID_LONG:
11594 case RID_SHORT:
11595 case RID_SIGNED:
11596 case RID_DFLOAT32:
11597 case RID_DFLOAT64:
11598 case RID_DFLOAT128:
11599 case RID_FRACT:
11600 case RID_ACCUM:
11601 case RID_BOOL:
11602 case RID_WCHAR:
11603 case RID_CHAR16:
11604 case RID_CHAR32:
11605 case RID_SAT:
11606 case RID_COMPLEX:
11607 case RID_TYPEOF:
11608 case RID_STRUCT:
11609 case RID_CLASS:
11610 case RID_UNION:
11611 case RID_ENUM:
11612 return true;
11613 default:
11614 return false;
11618 /* Return true if KEYWORD names a type qualifier. */
11620 bool
11621 keyword_is_type_qualifier (enum rid keyword)
11623 switch (keyword)
11625 case RID_CONST:
11626 case RID_VOLATILE:
11627 case RID_RESTRICT:
11628 return true;
11629 default:
11630 return false;
11634 /* Return true if KEYWORD names a storage class specifier.
11636 RID_TYPEDEF is not included in this list despite `typedef' being
11637 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11638 such for syntactic convenience only. */
11640 bool
11641 keyword_is_storage_class_specifier (enum rid keyword)
11643 switch (keyword)
11645 case RID_STATIC:
11646 case RID_EXTERN:
11647 case RID_REGISTER:
11648 case RID_AUTO:
11649 case RID_MUTABLE:
11650 case RID_THREAD:
11651 return true;
11652 default:
11653 return false;
11657 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11659 static bool
11660 keyword_is_function_specifier (enum rid keyword)
11662 switch (keyword)
11664 case RID_INLINE:
11665 case RID_NORETURN:
11666 case RID_VIRTUAL:
11667 case RID_EXPLICIT:
11668 return true;
11669 default:
11670 return false;
11674 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11675 declaration-specifier (C99 6.7). */
11677 bool
11678 keyword_is_decl_specifier (enum rid keyword)
11680 if (keyword_is_storage_class_specifier (keyword)
11681 || keyword_is_type_qualifier (keyword)
11682 || keyword_is_function_specifier (keyword))
11683 return true;
11685 switch (keyword)
11687 case RID_TYPEDEF:
11688 case RID_FRIEND:
11689 case RID_CONSTEXPR:
11690 return true;
11691 default:
11692 return false;
11696 /* Initialize language-specific-bits of tree_contains_struct. */
11698 void
11699 c_common_init_ts (void)
11701 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11702 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11703 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11706 /* Build a user-defined numeric literal out of an integer constant type VALUE
11707 with identifier SUFFIX. */
11709 tree
11710 build_userdef_literal (tree suffix_id, tree value,
11711 enum overflow_type overflow, tree num_string)
11713 tree literal = make_node (USERDEF_LITERAL);
11714 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11715 USERDEF_LITERAL_VALUE (literal) = value;
11716 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11717 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11718 return literal;
11721 /* For vector[index], convert the vector to a
11722 pointer of the underlying type. */
11723 void
11724 convert_vector_to_pointer_for_subscript (location_t loc,
11725 tree* vecp, tree index)
11727 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11729 tree type = TREE_TYPE (*vecp);
11730 tree type1;
11732 if (TREE_CODE (index) == INTEGER_CST)
11733 if (!host_integerp (index, 1)
11734 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11735 >= TYPE_VECTOR_SUBPARTS (type)))
11736 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11738 c_common_mark_addressable_vec (*vecp);
11739 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11740 type = build_pointer_type (type);
11741 type1 = build_pointer_type (TREE_TYPE (*vecp));
11742 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11743 *vecp = convert (type, *vecp);
11747 /* Determine which of the operands, if any, is a scalar that needs to be
11748 converted to a vector, for the range of operations. */
11749 enum stv_conv
11750 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11751 bool complain)
11753 tree type0 = TREE_TYPE (op0);
11754 tree type1 = TREE_TYPE (op1);
11755 bool integer_only_op = false;
11756 enum stv_conv ret = stv_firstarg;
11758 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11759 || TREE_CODE (type1) == VECTOR_TYPE);
11760 switch (code)
11762 /* Most GENERIC binary expressions require homogeneous arguments.
11763 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11764 argument that is a vector and a second one that is a scalar, so
11765 we never return stv_secondarg for them. */
11766 case RSHIFT_EXPR:
11767 case LSHIFT_EXPR:
11768 if (TREE_CODE (type0) == INTEGER_TYPE
11769 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11771 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11773 if (complain)
11774 error_at (loc, "conversion of scalar %qT to vector %qT "
11775 "involves truncation", type0, type1);
11776 return stv_error;
11778 else
11779 return stv_firstarg;
11781 break;
11783 case BIT_IOR_EXPR:
11784 case BIT_XOR_EXPR:
11785 case BIT_AND_EXPR:
11786 integer_only_op = true;
11787 /* ... fall through ... */
11789 case VEC_COND_EXPR:
11791 case PLUS_EXPR:
11792 case MINUS_EXPR:
11793 case MULT_EXPR:
11794 case TRUNC_DIV_EXPR:
11795 case CEIL_DIV_EXPR:
11796 case FLOOR_DIV_EXPR:
11797 case ROUND_DIV_EXPR:
11798 case EXACT_DIV_EXPR:
11799 case TRUNC_MOD_EXPR:
11800 case FLOOR_MOD_EXPR:
11801 case RDIV_EXPR:
11802 case EQ_EXPR:
11803 case NE_EXPR:
11804 case LE_EXPR:
11805 case GE_EXPR:
11806 case LT_EXPR:
11807 case GT_EXPR:
11808 /* What about UNLT_EXPR? */
11809 if (TREE_CODE (type0) == VECTOR_TYPE)
11811 tree tmp;
11812 ret = stv_secondarg;
11813 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11814 tmp = type0; type0 = type1; type1 = tmp;
11815 tmp = op0; op0 = op1; op1 = tmp;
11818 if (TREE_CODE (type0) == INTEGER_TYPE
11819 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11821 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11823 if (complain)
11824 error_at (loc, "conversion of scalar %qT to vector %qT "
11825 "involves truncation", type0, type1);
11826 return stv_error;
11828 return ret;
11830 else if (!integer_only_op
11831 /* Allow integer --> real conversion if safe. */
11832 && (TREE_CODE (type0) == REAL_TYPE
11833 || TREE_CODE (type0) == INTEGER_TYPE)
11834 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11836 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11838 if (complain)
11839 error_at (loc, "conversion of scalar %qT to vector %qT "
11840 "involves truncation", type0, type1);
11841 return stv_error;
11843 return ret;
11845 default:
11846 break;
11849 return stv_nothing;
11852 /* Return true iff ALIGN is an integral constant that is a fundamental
11853 alignment, as defined by [basic.align] in the c++-11
11854 specifications.
11856 That is:
11858 [A fundamental alignment is represented by an alignment less than or
11859 equal to the greatest alignment supported by the implementation
11860 in all contexts, which is equal to
11861 alignof(max_align_t)]. */
11863 bool
11864 cxx_fundamental_alignment_p (unsigned align)
11866 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11867 TYPE_ALIGN (long_double_type_node)));
11870 #include "gt-c-family-c-common.h"