Merge trunk version 198815 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob9f8e8eed2ee22d0921ddc44ad3696376e5de7383
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 "tree-mudflap.h"
44 #include "opts.h"
45 #include "cgraph.h"
46 #include "target-def.h"
48 cpp_reader *parse_in; /* Declared in c-pragma.h. */
50 /* The following symbols are subsumed in the c_global_trees array, and
51 listed here individually for documentation purposes.
53 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
55 tree short_integer_type_node;
56 tree long_integer_type_node;
57 tree long_long_integer_type_node;
58 tree int128_integer_type_node;
60 tree short_unsigned_type_node;
61 tree long_unsigned_type_node;
62 tree long_long_unsigned_type_node;
63 tree int128_unsigned_type_node;
65 tree truthvalue_type_node;
66 tree truthvalue_false_node;
67 tree truthvalue_true_node;
69 tree ptrdiff_type_node;
71 tree unsigned_char_type_node;
72 tree signed_char_type_node;
73 tree wchar_type_node;
75 tree char16_type_node;
76 tree char32_type_node;
78 tree float_type_node;
79 tree double_type_node;
80 tree long_double_type_node;
82 tree complex_integer_type_node;
83 tree complex_float_type_node;
84 tree complex_double_type_node;
85 tree complex_long_double_type_node;
87 tree dfloat32_type_node;
88 tree dfloat64_type_node;
89 tree_dfloat128_type_node;
91 tree intQI_type_node;
92 tree intHI_type_node;
93 tree intSI_type_node;
94 tree intDI_type_node;
95 tree intTI_type_node;
97 tree unsigned_intQI_type_node;
98 tree unsigned_intHI_type_node;
99 tree unsigned_intSI_type_node;
100 tree unsigned_intDI_type_node;
101 tree unsigned_intTI_type_node;
103 tree widest_integer_literal_type_node;
104 tree widest_unsigned_literal_type_node;
106 Nodes for types `void *' and `const void *'.
108 tree ptr_type_node, const_ptr_type_node;
110 Nodes for types `char *' and `const char *'.
112 tree string_type_node, const_string_type_node;
114 Type `char[SOMENUMBER]'.
115 Used when an array of char is needed and the size is irrelevant.
117 tree char_array_type_node;
119 Type `int[SOMENUMBER]' or something like it.
120 Used when an array of int needed and the size is irrelevant.
122 tree int_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
145 tree void_list_node;
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
172 char flag_no_output;
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193 int flag_iso;
195 /* Nonzero whenever UPC -fupc-threads-N is asserted.
196 The value N gives the number of UPC threads to be
197 defined at compile-time. */
198 int flag_upc_threads;
200 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
201 int flag_upc_pthreads;
203 /* The implementation model for UPC threads that
204 are mapped to POSIX threads, specified at compilation
205 time by the -fupc-pthreads-model-* switch. */
206 upc_pthreads_model_kind upc_pthreads_model;
208 /* C/ObjC language option variables. */
211 /* Nonzero means allow type mismatches in conditional expressions;
212 just make their values `void'. */
214 int flag_cond_mismatch;
216 /* Nonzero means enable C89 Amendment 1 features. */
218 int flag_isoc94;
220 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
222 int flag_isoc99;
224 /* Nonzero means use the ISO C11 dialect of C. */
226 int flag_isoc11;
228 /* Nonzero means that we have builtin functions, and main is an int. */
230 int flag_hosted = 1;
233 /* ObjC language option variables. */
236 /* Tells the compiler that this is a special run. Do not perform any
237 compiling, instead we are to test some platform dependent features
238 and output a C header file with appropriate definitions. */
240 int print_struct_values;
242 /* Tells the compiler what is the constant string class for ObjC. */
244 const char *constant_string_class_name;
247 /* C++ language option variables. */
250 /* Nonzero means generate separate instantiation control files and
251 juggle them at link time. */
253 int flag_use_repository;
255 /* The C++ dialect being used. C++98 is the default. */
257 enum cxx_dialect cxx_dialect = cxx98;
259 /* Maximum template instantiation depth. This limit exists to limit the
260 time it takes to notice excessively recursive template instantiations.
262 The default is lower than the 1024 recommended by the C++0x standard
263 because G++ runs out of stack before 1024 with highly recursive template
264 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
266 int max_tinst_depth = 900;
268 /* The elements of `ridpointers' are identifier nodes for the reserved
269 type names and storage classes. It is indexed by a RID_... value. */
270 tree *ridpointers;
272 tree (*make_fname_decl) (location_t, tree, int);
274 /* Nonzero means don't warn about problems that occur when the code is
275 executed. */
276 int c_inhibit_evaluation_warnings;
278 /* Whether we are building a boolean conversion inside
279 convert_for_assignment, or some other late binary operation. If
280 build_binary_op is called for C (from code shared by C and C++) in
281 this case, then the operands have already been folded and the
282 result will not be folded again, so C_MAYBE_CONST_EXPR should not
283 be generated. */
284 bool in_late_binary_op;
286 /* Whether lexing has been completed, so subsequent preprocessor
287 errors should use the compiler's input_location. */
288 bool done_lexing = false;
290 /* Information about how a function name is generated. */
291 struct fname_var_t
293 tree *const decl; /* pointer to the VAR_DECL. */
294 const unsigned rid; /* RID number for the identifier. */
295 const int pretty; /* How pretty is it? */
298 /* The three ways of getting then name of the current function. */
300 const struct fname_var_t fname_vars[] =
302 /* C99 compliant __func__, must be first. */
303 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
304 /* GCC __FUNCTION__ compliant. */
305 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
306 /* GCC __PRETTY_FUNCTION__ compliant. */
307 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
308 {NULL, 0, 0},
311 /* Global visibility options. */
312 struct visibility_flags visibility_options;
314 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
315 static tree check_case_value (tree);
316 static bool check_case_bounds (tree, tree, tree *, tree *);
318 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
325 int, bool *);
326 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
327 int, bool *);
328 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_always_inline_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
340 bool *);
341 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_transparent_union_attribute (tree *, tree, tree,
343 int, bool *);
344 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
350 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
351 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_visibility_attribute (tree *, tree, tree, int,
355 bool *);
356 static tree handle_tls_model_attribute (tree *, tree, tree, int,
357 bool *);
358 static tree handle_no_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
360 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
377 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
382 static tree ignore_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
386 static void check_function_nonnull (tree, int, tree *);
387 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
388 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
389 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
390 static int resort_field_decl_cmp (const void *, const void *);
392 /* Reserved words. The third field is a mask: keywords are disabled
393 if they match the mask.
395 Masks for languages:
396 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
397 C --std=c99: D_CXXONLY | D_OBJC
398 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
399 UPC is like C except that D_UPC is not set
400 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
401 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
402 ObjC++ is like C++ except that D_OBJC is not set
404 If -fno-asm is used, D_ASM is added to the mask. If
405 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
406 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
407 In C with -Wc++-compat, we warn if D_CXXWARN is set.
409 Note the complication of the D_CXX_OBJC keywords. These are
410 reserved words such as 'class'. In C++, 'class' is a reserved
411 word. In Objective-C++ it is too. In Objective-C, it is a
412 reserved word too, but only if it follows an '@' sign.
414 const struct c_common_resword c_common_reswords[] =
416 { "_Alignas", RID_ALIGNAS, D_CONLY },
417 { "_Alignof", RID_ALIGNOF, D_CONLY },
418 { "_Bool", RID_BOOL, D_CONLY },
419 { "_Complex", RID_COMPLEX, 0 },
420 { "_Imaginary", RID_IMAGINARY, D_CONLY },
421 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
422 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
423 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
424 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
425 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
426 { "_Sat", RID_SAT, D_CONLY | D_EXT },
427 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
428 { "_Noreturn", RID_NORETURN, D_CONLY },
429 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
430 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
431 { "__alignof", RID_ALIGNOF, 0 },
432 { "__alignof__", RID_ALIGNOF, 0 },
433 { "__asm", RID_ASM, 0 },
434 { "__asm__", RID_ASM, 0 },
435 { "__attribute", RID_ATTRIBUTE, 0 },
436 { "__attribute__", RID_ATTRIBUTE, 0 },
437 { "__bases", RID_BASES, D_CXXONLY },
438 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
439 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
440 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
441 { "__builtin_offsetof", RID_OFFSETOF, 0 },
442 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
443 { "__builtin_va_arg", RID_VA_ARG, 0 },
444 { "__complex", RID_COMPLEX, 0 },
445 { "__complex__", RID_COMPLEX, 0 },
446 { "__const", RID_CONST, 0 },
447 { "__const__", RID_CONST, 0 },
448 { "__decltype", RID_DECLTYPE, D_CXXONLY },
449 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
450 { "__extension__", RID_EXTENSION, 0 },
451 { "__func__", RID_C99_FUNCTION_NAME, 0 },
452 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
453 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
454 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
455 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
456 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
457 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
458 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
459 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
460 { "__imag", RID_IMAGPART, 0 },
461 { "__imag__", RID_IMAGPART, 0 },
462 { "__inline", RID_INLINE, 0 },
463 { "__inline__", RID_INLINE, 0 },
464 { "__int128", RID_INT128, 0 },
465 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
466 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
467 { "__is_class", RID_IS_CLASS, D_CXXONLY },
468 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
469 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
470 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
471 { "__is_final", RID_IS_FINAL, D_CXXONLY },
472 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
473 { "__is_pod", RID_IS_POD, D_CXXONLY },
474 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
475 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
476 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
477 { "__is_union", RID_IS_UNION, D_CXXONLY },
478 { "__label__", RID_LABEL, 0 },
479 { "__null", RID_NULL, 0 },
480 { "__real", RID_REALPART, 0 },
481 { "__real__", RID_REALPART, 0 },
482 { "__restrict", RID_RESTRICT, 0 },
483 { "__restrict__", RID_RESTRICT, 0 },
484 { "__signed", RID_SIGNED, 0 },
485 { "__signed__", RID_SIGNED, 0 },
486 { "__thread", RID_THREAD, 0 },
487 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
488 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
489 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
490 { "__typeof", RID_TYPEOF, 0 },
491 { "__typeof__", RID_TYPEOF, 0 },
492 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
493 { "__volatile", RID_VOLATILE, 0 },
494 { "__volatile__", RID_VOLATILE, 0 },
495 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
496 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
497 { "asm", RID_ASM, D_ASM },
498 { "auto", RID_AUTO, 0 },
499 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
500 { "break", RID_BREAK, 0 },
501 { "case", RID_CASE, 0 },
502 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
503 { "char", RID_CHAR, 0 },
504 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
507 { "const", RID_CONST, 0 },
508 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
510 { "continue", RID_CONTINUE, 0 },
511 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "default", RID_DEFAULT, 0 },
513 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
514 { "do", RID_DO, 0 },
515 { "double", RID_DOUBLE, 0 },
516 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
517 { "else", RID_ELSE, 0 },
518 { "enum", RID_ENUM, 0 },
519 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
520 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
521 { "extern", RID_EXTERN, 0 },
522 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
523 { "float", RID_FLOAT, 0 },
524 { "for", RID_FOR, 0 },
525 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
526 { "goto", RID_GOTO, 0 },
527 { "if", RID_IF, 0 },
528 { "inline", RID_INLINE, D_EXT89 },
529 { "int", RID_INT, 0 },
530 { "long", RID_LONG, 0 },
531 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
532 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
533 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
534 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
535 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
537 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
538 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
539 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
540 { "register", RID_REGISTER, 0 },
541 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
542 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
543 { "return", RID_RETURN, 0 },
544 { "short", RID_SHORT, 0 },
545 { "signed", RID_SIGNED, 0 },
546 { "sizeof", RID_SIZEOF, 0 },
547 { "static", RID_STATIC, 0 },
548 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
549 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
550 { "struct", RID_STRUCT, 0 },
551 { "switch", RID_SWITCH, 0 },
552 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
553 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
554 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
555 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
556 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
557 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
558 { "typedef", RID_TYPEDEF, 0 },
559 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
560 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
561 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
562 { "union", RID_UNION, 0 },
563 { "unsigned", RID_UNSIGNED, 0 },
564 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
565 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
566 { "void", RID_VOID, 0 },
567 { "volatile", RID_VOLATILE, 0 },
568 { "wchar_t", RID_WCHAR, D_CXXONLY },
569 { "while", RID_WHILE, 0 },
570 /* These Objective-C keywords are recognized only immediately after
571 an '@'. */
572 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
573 { "defs", RID_AT_DEFS, D_OBJC },
574 { "encode", RID_AT_ENCODE, D_OBJC },
575 { "end", RID_AT_END, D_OBJC },
576 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
577 { "interface", RID_AT_INTERFACE, D_OBJC },
578 { "protocol", RID_AT_PROTOCOL, D_OBJC },
579 { "selector", RID_AT_SELECTOR, D_OBJC },
580 { "finally", RID_AT_FINALLY, D_OBJC },
581 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
582 { "optional", RID_AT_OPTIONAL, D_OBJC },
583 { "required", RID_AT_REQUIRED, D_OBJC },
584 { "property", RID_AT_PROPERTY, D_OBJC },
585 { "package", RID_AT_PACKAGE, D_OBJC },
586 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
587 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
588 /* These are recognized only in protocol-qualifier context
589 (see above) */
590 { "bycopy", RID_BYCOPY, D_OBJC },
591 { "byref", RID_BYREF, D_OBJC },
592 { "in", RID_IN, D_OBJC },
593 { "inout", RID_INOUT, D_OBJC },
594 { "oneway", RID_ONEWAY, D_OBJC },
595 { "out", RID_OUT, D_OBJC },
597 /* UPC keywords */
598 { "shared", RID_SHARED, D_UPC },
599 { "relaxed", RID_RELAXED, D_UPC },
600 { "strict", RID_STRICT, D_UPC },
601 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
602 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
603 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
604 { "upc_forall", RID_UPC_FORALL, D_UPC },
605 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
606 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
607 { "upc_wait", RID_UPC_WAIT, D_UPC },
609 /* These are recognized inside a property attribute list */
610 { "assign", RID_ASSIGN, D_OBJC },
611 { "copy", RID_COPY, D_OBJC },
612 { "getter", RID_GETTER, D_OBJC },
613 { "nonatomic", RID_NONATOMIC, D_OBJC },
614 { "readonly", RID_READONLY, D_OBJC },
615 { "readwrite", RID_READWRITE, D_OBJC },
616 { "retain", RID_RETAIN, D_OBJC },
617 { "setter", RID_SETTER, D_OBJC },
620 const unsigned int num_c_common_reswords =
621 sizeof c_common_reswords / sizeof (struct c_common_resword);
623 /* Table of machine-independent attributes common to all C-like languages. */
624 const struct attribute_spec c_common_attribute_table[] =
626 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
627 affects_type_identity } */
628 { "packed", 0, 0, false, false, false,
629 handle_packed_attribute , false},
630 { "nocommon", 0, 0, true, false, false,
631 handle_nocommon_attribute, false},
632 { "common", 0, 0, true, false, false,
633 handle_common_attribute, false },
634 /* FIXME: logically, noreturn attributes should be listed as
635 "false, true, true" and apply to function types. But implementing this
636 would require all the places in the compiler that use TREE_THIS_VOLATILE
637 on a decl to identify non-returning functions to be located and fixed
638 to check the function type instead. */
639 { "noreturn", 0, 0, true, false, false,
640 handle_noreturn_attribute, false },
641 { "volatile", 0, 0, true, false, false,
642 handle_noreturn_attribute, false },
643 { "noinline", 0, 0, true, false, false,
644 handle_noinline_attribute, false },
645 { "noclone", 0, 0, true, false, false,
646 handle_noclone_attribute, false },
647 { "leaf", 0, 0, true, false, false,
648 handle_leaf_attribute, false },
649 { "always_inline", 0, 0, true, false, false,
650 handle_always_inline_attribute, false },
651 { "gnu_inline", 0, 0, true, false, false,
652 handle_gnu_inline_attribute, false },
653 { "artificial", 0, 0, true, false, false,
654 handle_artificial_attribute, false },
655 { "flatten", 0, 0, true, false, false,
656 handle_flatten_attribute, false },
657 { "used", 0, 0, true, false, false,
658 handle_used_attribute, false },
659 { "unused", 0, 0, false, false, false,
660 handle_unused_attribute, false },
661 { "externally_visible", 0, 0, true, false, false,
662 handle_externally_visible_attribute, false },
663 /* The same comments as for noreturn attributes apply to const ones. */
664 { "const", 0, 0, true, false, false,
665 handle_const_attribute, false },
666 { "transparent_union", 0, 0, false, false, false,
667 handle_transparent_union_attribute, false },
668 { "constructor", 0, 1, true, false, false,
669 handle_constructor_attribute, false },
670 { "destructor", 0, 1, true, false, false,
671 handle_destructor_attribute, false },
672 { "mode", 1, 1, false, true, false,
673 handle_mode_attribute, false },
674 { "section", 1, 1, true, false, false,
675 handle_section_attribute, false },
676 { "aligned", 0, 1, false, false, false,
677 handle_aligned_attribute, false },
678 { "weak", 0, 0, true, false, false,
679 handle_weak_attribute, false },
680 { "ifunc", 1, 1, true, false, false,
681 handle_ifunc_attribute, false },
682 { "alias", 1, 1, true, false, false,
683 handle_alias_attribute, false },
684 { "weakref", 0, 1, true, false, false,
685 handle_weakref_attribute, false },
686 { "no_instrument_function", 0, 0, true, false, false,
687 handle_no_instrument_function_attribute,
688 false },
689 { "malloc", 0, 0, true, false, false,
690 handle_malloc_attribute, false },
691 { "returns_twice", 0, 0, true, false, false,
692 handle_returns_twice_attribute, false },
693 { "no_stack_limit", 0, 0, true, false, false,
694 handle_no_limit_stack_attribute, false },
695 { "pure", 0, 0, true, false, false,
696 handle_pure_attribute, false },
697 { "transaction_callable", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_unsafe", 0, 0, false, true, false,
700 handle_tm_attribute, false },
701 { "transaction_safe", 0, 0, false, true, false,
702 handle_tm_attribute, false },
703 { "transaction_may_cancel_outer", 0, 0, false, true, false,
704 handle_tm_attribute, false },
705 /* ??? These two attributes didn't make the transition from the
706 Intel language document to the multi-vendor language document. */
707 { "transaction_pure", 0, 0, false, true, false,
708 handle_tm_attribute, false },
709 { "transaction_wrap", 1, 1, true, false, false,
710 handle_tm_wrap_attribute, false },
711 /* For internal use (marking of builtins) only. The name contains space
712 to prevent its usage in source code. */
713 { "no vops", 0, 0, true, false, false,
714 handle_novops_attribute, false },
715 { "deprecated", 0, 1, false, false, false,
716 handle_deprecated_attribute, false },
717 { "vector_size", 1, 1, false, true, false,
718 handle_vector_size_attribute, false },
719 { "visibility", 1, 1, false, false, false,
720 handle_visibility_attribute, false },
721 { "tls_model", 1, 1, true, false, false,
722 handle_tls_model_attribute, false },
723 { "nonnull", 0, -1, false, true, true,
724 handle_nonnull_attribute, false },
725 { "nothrow", 0, 0, true, false, false,
726 handle_nothrow_attribute, false },
727 { "may_alias", 0, 0, false, true, false, NULL, false },
728 { "cleanup", 1, 1, true, false, false,
729 handle_cleanup_attribute, false },
730 { "warn_unused_result", 0, 0, false, true, true,
731 handle_warn_unused_result_attribute, false },
732 { "sentinel", 0, 1, false, true, true,
733 handle_sentinel_attribute, false },
734 /* For internal use (marking of builtins) only. The name contains space
735 to prevent its usage in source code. */
736 { "type generic", 0, 0, false, true, true,
737 handle_type_generic_attribute, false },
738 { "alloc_size", 1, 2, false, true, true,
739 handle_alloc_size_attribute, false },
740 { "cold", 0, 0, true, false, false,
741 handle_cold_attribute, false },
742 { "hot", 0, 0, true, false, false,
743 handle_hot_attribute, false },
744 { "no_address_safety_analysis",
745 0, 0, true, false, false,
746 handle_no_address_safety_analysis_attribute,
747 false },
748 { "no_sanitize_address", 0, 0, true, false, false,
749 handle_no_sanitize_address_attribute,
750 false },
751 { "warning", 1, 1, true, false, false,
752 handle_error_attribute, false },
753 { "error", 1, 1, true, false, false,
754 handle_error_attribute, false },
755 { "target", 1, -1, true, false, false,
756 handle_target_attribute, false },
757 { "optimize", 1, -1, true, false, false,
758 handle_optimize_attribute, false },
759 /* For internal use only. The leading '*' both prevents its usage in
760 source code and signals that it may be overridden by machine tables. */
761 { "*tm regparm", 0, 0, false, true, true,
762 ignore_attribute, false },
763 { "no_split_stack", 0, 0, true, false, false,
764 handle_no_split_stack_attribute, false },
765 /* For internal use (marking of builtins and runtime functions) only.
766 The name contains space to prevent its usage in source code. */
767 { "fn spec", 1, 1, false, true, true,
768 handle_fnspec_attribute, false },
769 { NULL, 0, 0, false, false, false, NULL, false }
772 /* Give the specifications for the format attributes, used by C and all
773 descendants. */
775 const struct attribute_spec c_common_format_attribute_table[] =
777 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
778 affects_type_identity } */
779 { "format", 3, 3, false, true, true,
780 handle_format_attribute, false },
781 { "format_arg", 1, 1, false, true, true,
782 handle_format_arg_attribute, false },
783 { NULL, 0, 0, false, false, false, NULL, false }
786 /* Return identifier for address space AS. */
788 const char *
789 c_addr_space_name (addr_space_t as)
791 int rid = RID_FIRST_ADDR_SPACE + as;
792 gcc_assert (ridpointers [rid]);
793 return IDENTIFIER_POINTER (ridpointers [rid]);
796 /* Push current bindings for the function name VAR_DECLS. */
798 void
799 start_fname_decls (void)
801 unsigned ix;
802 tree saved = NULL_TREE;
804 for (ix = 0; fname_vars[ix].decl; ix++)
806 tree decl = *fname_vars[ix].decl;
808 if (decl)
810 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
811 saved);
812 *fname_vars[ix].decl = NULL_TREE;
815 if (saved || saved_function_name_decls)
816 /* Normally they'll have been NULL, so only push if we've got a
817 stack, or they are non-NULL. */
818 saved_function_name_decls = tree_cons (saved, NULL_TREE,
819 saved_function_name_decls);
822 /* Finish up the current bindings, adding them into the current function's
823 statement tree. This must be done _before_ finish_stmt_tree is called.
824 If there is no current function, we must be at file scope and no statements
825 are involved. Pop the previous bindings. */
827 void
828 finish_fname_decls (void)
830 unsigned ix;
831 tree stmts = NULL_TREE;
832 tree stack = saved_function_name_decls;
834 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
835 append_to_statement_list (TREE_VALUE (stack), &stmts);
837 if (stmts)
839 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
841 if (TREE_CODE (*bodyp) == BIND_EXPR)
842 bodyp = &BIND_EXPR_BODY (*bodyp);
844 append_to_statement_list_force (*bodyp, &stmts);
845 *bodyp = stmts;
848 for (ix = 0; fname_vars[ix].decl; ix++)
849 *fname_vars[ix].decl = NULL_TREE;
851 if (stack)
853 /* We had saved values, restore them. */
854 tree saved;
856 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
858 tree decl = TREE_PURPOSE (saved);
859 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
861 *fname_vars[ix].decl = decl;
863 stack = TREE_CHAIN (stack);
865 saved_function_name_decls = stack;
868 /* Return the text name of the current function, suitably prettified
869 by PRETTY_P. Return string must be freed by caller. */
871 const char *
872 fname_as_string (int pretty_p)
874 const char *name = "top level";
875 char *namep;
876 int vrb = 2, len;
877 cpp_string cstr = { 0, 0 }, strname;
879 if (!pretty_p)
881 name = "";
882 vrb = 0;
885 if (current_function_decl)
886 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
888 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
890 namep = XNEWVEC (char, len);
891 snprintf (namep, len, "\"%s\"", name);
892 strname.text = (unsigned char *) namep;
893 strname.len = len - 1;
895 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
897 XDELETEVEC (namep);
898 return (const char *) cstr.text;
901 return namep;
904 /* Return the VAR_DECL for a const char array naming the current
905 function. If the VAR_DECL has not yet been created, create it
906 now. RID indicates how it should be formatted and IDENTIFIER_NODE
907 ID is its name (unfortunately C and C++ hold the RID values of
908 keywords in different places, so we can't derive RID from ID in
909 this language independent code. LOC is the location of the
910 function. */
912 tree
913 fname_decl (location_t loc, unsigned int rid, tree id)
915 unsigned ix;
916 tree decl = NULL_TREE;
918 for (ix = 0; fname_vars[ix].decl; ix++)
919 if (fname_vars[ix].rid == rid)
920 break;
922 decl = *fname_vars[ix].decl;
923 if (!decl)
925 /* If a tree is built here, it would normally have the lineno of
926 the current statement. Later this tree will be moved to the
927 beginning of the function and this line number will be wrong.
928 To avoid this problem set the lineno to 0 here; that prevents
929 it from appearing in the RTL. */
930 tree stmts;
931 location_t saved_location = input_location;
932 input_location = UNKNOWN_LOCATION;
934 stmts = push_stmt_list ();
935 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
936 stmts = pop_stmt_list (stmts);
937 if (!IS_EMPTY_STMT (stmts))
938 saved_function_name_decls
939 = tree_cons (decl, stmts, saved_function_name_decls);
940 *fname_vars[ix].decl = decl;
941 input_location = saved_location;
943 if (!ix && !current_function_decl)
944 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
946 return decl;
949 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
951 tree
952 fix_string_type (tree value)
954 int length = TREE_STRING_LENGTH (value);
955 int nchars;
956 tree e_type, i_type, a_type;
958 /* Compute the number of elements, for the array type. */
959 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
961 nchars = length;
962 e_type = char_type_node;
964 else if (TREE_TYPE (value) == char16_array_type_node)
966 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
967 e_type = char16_type_node;
969 else if (TREE_TYPE (value) == char32_array_type_node)
971 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
972 e_type = char32_type_node;
974 else
976 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
977 e_type = wchar_type_node;
980 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
981 limit in C++98 Annex B is very large (65536) and is not normative,
982 so we do not diagnose it (warn_overlength_strings is forced off
983 in c_common_post_options). */
984 if (warn_overlength_strings)
986 const int nchars_max = flag_isoc99 ? 4095 : 509;
987 const int relevant_std = flag_isoc99 ? 99 : 90;
988 if (nchars - 1 > nchars_max)
989 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
990 separate the %d from the 'C'. 'ISO' should not be
991 translated, but it may be moved after 'C%d' in languages
992 where modifiers follow nouns. */
993 pedwarn (input_location, OPT_Woverlength_strings,
994 "string length %qd is greater than the length %qd "
995 "ISO C%d compilers are required to support",
996 nchars - 1, nchars_max, relevant_std);
999 /* Create the array type for the string constant. The ISO C++
1000 standard says that a string literal has type `const char[N]' or
1001 `const wchar_t[N]'. We use the same logic when invoked as a C
1002 front-end with -Wwrite-strings.
1003 ??? We should change the type of an expression depending on the
1004 state of a warning flag. We should just be warning -- see how
1005 this is handled in the C++ front-end for the deprecated implicit
1006 conversion from string literals to `char*' or `wchar_t*'.
1008 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1009 array type being the unqualified version of that type.
1010 Therefore, if we are constructing an array of const char, we must
1011 construct the matching unqualified array type first. The C front
1012 end does not require this, but it does no harm, so we do it
1013 unconditionally. */
1014 i_type = build_index_type (size_int (nchars - 1));
1015 a_type = build_array_type (e_type, i_type);
1016 if (c_dialect_cxx() || warn_write_strings)
1017 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1019 TREE_TYPE (value) = a_type;
1020 TREE_CONSTANT (value) = 1;
1021 TREE_READONLY (value) = 1;
1022 TREE_STATIC (value) = 1;
1023 return value;
1026 /* If DISABLE is true, stop issuing warnings. This is used when
1027 parsing code that we know will not be executed. This function may
1028 be called multiple times, and works as a stack. */
1030 static void
1031 c_disable_warnings (bool disable)
1033 if (disable)
1035 ++c_inhibit_evaluation_warnings;
1036 fold_defer_overflow_warnings ();
1040 /* If ENABLE is true, reenable issuing warnings. */
1042 static void
1043 c_enable_warnings (bool enable)
1045 if (enable)
1047 --c_inhibit_evaluation_warnings;
1048 fold_undefer_and_ignore_overflow_warnings ();
1052 /* Fully fold EXPR, an expression that was not folded (beyond integer
1053 constant expressions and null pointer constants) when being built
1054 up. If IN_INIT, this is in a static initializer and certain
1055 changes are made to the folding done. Clear *MAYBE_CONST if
1056 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1057 expression because it contains an evaluated operator (in C99) or an
1058 operator outside of sizeof returning an integer constant (in C90)
1059 not permitted in constant expressions, or because it contains an
1060 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1061 set to true by callers before calling this function.) Return the
1062 folded expression. Function arguments have already been folded
1063 before calling this function, as have the contents of SAVE_EXPR,
1064 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1065 C_MAYBE_CONST_EXPR. */
1067 tree
1068 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1070 tree ret;
1071 tree eptype = NULL_TREE;
1072 bool dummy = true;
1073 bool maybe_const_itself = true;
1074 location_t loc = EXPR_LOCATION (expr);
1076 /* This function is not relevant to C++ because C++ folds while
1077 parsing, and may need changes to be correct for C++ when C++
1078 stops folding while parsing. */
1079 if (c_dialect_cxx ())
1080 gcc_unreachable ();
1082 if (!maybe_const)
1083 maybe_const = &dummy;
1084 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1086 eptype = TREE_TYPE (expr);
1087 expr = TREE_OPERAND (expr, 0);
1089 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1090 &maybe_const_itself);
1091 if (eptype)
1092 ret = fold_convert_loc (loc, eptype, ret);
1093 *maybe_const &= maybe_const_itself;
1094 return ret;
1097 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1098 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1099 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1100 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1101 both evaluated and unevaluated subexpressions while
1102 *MAYBE_CONST_ITSELF is carried from only evaluated
1103 subexpressions). */
1105 static tree
1106 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1107 bool *maybe_const_itself)
1109 tree ret = expr;
1110 enum tree_code code = TREE_CODE (expr);
1111 enum tree_code_class kind = TREE_CODE_CLASS (code);
1112 location_t loc = EXPR_LOCATION (expr);
1113 tree op0, op1, op2, op3;
1114 tree orig_op0, orig_op1, orig_op2;
1115 bool op0_const = true, op1_const = true, op2_const = true;
1116 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1117 bool nowarning = TREE_NO_WARNING (expr);
1118 bool unused_p;
1120 /* This function is not relevant to C++ because C++ folds while
1121 parsing, and may need changes to be correct for C++ when C++
1122 stops folding while parsing. */
1123 if (c_dialect_cxx ())
1124 gcc_unreachable ();
1126 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1127 anything else not counted as an expression cannot usefully be
1128 folded further at this point. */
1129 if (!IS_EXPR_CODE_CLASS (kind)
1130 || kind == tcc_statement
1131 || code == SAVE_EXPR)
1132 return expr;
1134 /* Operands of variable-length expressions (function calls) have
1135 already been folded, as have __builtin_* function calls, and such
1136 expressions cannot occur in constant expressions. */
1137 if (kind == tcc_vl_exp)
1139 *maybe_const_operands = false;
1140 ret = fold (expr);
1141 goto out;
1144 if (code == C_MAYBE_CONST_EXPR)
1146 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1147 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1148 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1149 *maybe_const_operands = false;
1150 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1151 *maybe_const_itself = false;
1152 if (pre && !in_init)
1153 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1154 else
1155 ret = inner;
1156 goto out;
1159 /* Assignment, increment, decrement, function call and comma
1160 operators, and statement expressions, cannot occur in constant
1161 expressions if evaluated / outside of sizeof. (Function calls
1162 were handled above, though VA_ARG_EXPR is treated like a function
1163 call here, and statement expressions are handled through
1164 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1165 switch (code)
1167 case MODIFY_EXPR:
1168 case PREDECREMENT_EXPR:
1169 case PREINCREMENT_EXPR:
1170 case POSTDECREMENT_EXPR:
1171 case POSTINCREMENT_EXPR:
1172 case COMPOUND_EXPR:
1173 *maybe_const_operands = false;
1174 break;
1176 case VA_ARG_EXPR:
1177 case TARGET_EXPR:
1178 case BIND_EXPR:
1179 case OBJ_TYPE_REF:
1180 *maybe_const_operands = false;
1181 ret = fold (expr);
1182 goto out;
1184 default:
1185 break;
1188 /* Fold individual tree codes as appropriate. */
1189 switch (code)
1191 case COMPOUND_LITERAL_EXPR:
1192 /* Any non-constancy will have been marked in a containing
1193 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1194 goto out;
1196 case COMPONENT_REF:
1197 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1198 op1 = TREE_OPERAND (expr, 1);
1199 op2 = TREE_OPERAND (expr, 2);
1200 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1201 maybe_const_itself);
1202 STRIP_TYPE_NOPS (op0);
1203 if (op0 != orig_op0)
1204 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1205 if (ret != expr)
1207 TREE_READONLY (ret) = TREE_READONLY (expr);
1208 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1210 goto out;
1212 case ARRAY_REF:
1213 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1214 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1215 op2 = TREE_OPERAND (expr, 2);
1216 op3 = TREE_OPERAND (expr, 3);
1217 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1218 maybe_const_itself);
1219 STRIP_TYPE_NOPS (op0);
1220 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1221 maybe_const_itself);
1222 STRIP_TYPE_NOPS (op1);
1223 op1 = decl_constant_value_for_optimization (op1);
1224 if (op0 != orig_op0 || op1 != orig_op1)
1225 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1226 if (ret != expr)
1228 TREE_READONLY (ret) = TREE_READONLY (expr);
1229 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1230 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1232 ret = fold (ret);
1233 goto out;
1235 case COMPOUND_EXPR:
1236 case MODIFY_EXPR:
1237 case PREDECREMENT_EXPR:
1238 case PREINCREMENT_EXPR:
1239 case POSTDECREMENT_EXPR:
1240 case POSTINCREMENT_EXPR:
1241 case PLUS_EXPR:
1242 case MINUS_EXPR:
1243 case MULT_EXPR:
1244 case POINTER_PLUS_EXPR:
1245 case TRUNC_DIV_EXPR:
1246 case CEIL_DIV_EXPR:
1247 case FLOOR_DIV_EXPR:
1248 case TRUNC_MOD_EXPR:
1249 case RDIV_EXPR:
1250 case EXACT_DIV_EXPR:
1251 case LSHIFT_EXPR:
1252 case RSHIFT_EXPR:
1253 case BIT_IOR_EXPR:
1254 case BIT_XOR_EXPR:
1255 case BIT_AND_EXPR:
1256 case LT_EXPR:
1257 case LE_EXPR:
1258 case GT_EXPR:
1259 case GE_EXPR:
1260 case EQ_EXPR:
1261 case NE_EXPR:
1262 case COMPLEX_EXPR:
1263 case TRUTH_AND_EXPR:
1264 case TRUTH_OR_EXPR:
1265 case TRUTH_XOR_EXPR:
1266 case UNORDERED_EXPR:
1267 case ORDERED_EXPR:
1268 case UNLT_EXPR:
1269 case UNLE_EXPR:
1270 case UNGT_EXPR:
1271 case UNGE_EXPR:
1272 case UNEQ_EXPR:
1273 /* Binary operations evaluating both arguments (increment and
1274 decrement are binary internally in GCC). */
1275 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1276 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1277 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1278 maybe_const_itself);
1279 STRIP_TYPE_NOPS (op0);
1280 if (code != MODIFY_EXPR
1281 && code != PREDECREMENT_EXPR
1282 && code != PREINCREMENT_EXPR
1283 && code != POSTDECREMENT_EXPR
1284 && code != POSTINCREMENT_EXPR)
1285 op0 = decl_constant_value_for_optimization (op0);
1286 /* The RHS of a MODIFY_EXPR was fully folded when building that
1287 expression for the sake of conversion warnings. */
1288 if (code != MODIFY_EXPR)
1289 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1290 maybe_const_itself);
1291 STRIP_TYPE_NOPS (op1);
1292 op1 = decl_constant_value_for_optimization (op1);
1293 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1294 ret = in_init
1295 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1296 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1297 else
1298 ret = fold (expr);
1299 if (TREE_OVERFLOW_P (ret)
1300 && !TREE_OVERFLOW_P (op0)
1301 && !TREE_OVERFLOW_P (op1))
1302 overflow_warning (EXPR_LOCATION (expr), ret);
1303 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1304 && TREE_CODE (orig_op1) != INTEGER_CST
1305 && TREE_CODE (op1) == INTEGER_CST
1306 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1307 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1308 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1309 && c_inhibit_evaluation_warnings == 0)
1311 if (tree_int_cst_sgn (op1) < 0)
1312 warning_at (loc, 0, (code == LSHIFT_EXPR
1313 ? G_("left shift count is negative")
1314 : G_("right shift count is negative")));
1315 else if (compare_tree_int (op1,
1316 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1317 >= 0)
1318 warning_at (loc, 0, (code == LSHIFT_EXPR
1319 ? G_("left shift count >= width of type")
1320 : G_("right shift count >= width of type")));
1322 goto out;
1324 case INDIRECT_REF:
1325 case FIX_TRUNC_EXPR:
1326 case FLOAT_EXPR:
1327 CASE_CONVERT:
1328 case VIEW_CONVERT_EXPR:
1329 case NON_LVALUE_EXPR:
1330 case NEGATE_EXPR:
1331 case BIT_NOT_EXPR:
1332 case TRUTH_NOT_EXPR:
1333 case ADDR_EXPR:
1334 case CONJ_EXPR:
1335 case REALPART_EXPR:
1336 case IMAGPART_EXPR:
1337 /* Unary operations. */
1338 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1339 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1340 maybe_const_itself);
1341 STRIP_TYPE_NOPS (op0);
1342 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1343 op0 = decl_constant_value_for_optimization (op0);
1344 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1345 not prepared to deal with them if they occur in initializers.
1346 Avoid attempts to fold references to UPC shared components
1347 due to the complexities of UPC pointer-to-shared arithmetic. */
1348 if (op0 != orig_op0
1349 && code == ADDR_EXPR
1350 && (op1 = get_base_address (op0)) != NULL_TREE
1351 && TREE_CODE (op1) == INDIRECT_REF
1352 && !upc_shared_type_p (TREE_TYPE (op1))
1353 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1354 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1355 else if (op0 != orig_op0 || in_init)
1356 ret = in_init
1357 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1358 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1359 else
1360 ret = fold (expr);
1361 if (code == INDIRECT_REF
1362 && ret != expr
1363 && TREE_CODE (ret) == INDIRECT_REF)
1365 TREE_READONLY (ret) = TREE_READONLY (expr);
1366 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1367 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1369 switch (code)
1371 case FIX_TRUNC_EXPR:
1372 case FLOAT_EXPR:
1373 CASE_CONVERT:
1374 /* Don't warn about explicit conversions. We will already
1375 have warned about suspect implicit conversions. */
1376 break;
1378 default:
1379 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1380 overflow_warning (EXPR_LOCATION (expr), ret);
1381 break;
1383 goto out;
1385 case TRUTH_ANDIF_EXPR:
1386 case TRUTH_ORIF_EXPR:
1387 /* Binary operations not necessarily evaluating both
1388 arguments. */
1389 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1390 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1391 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1392 STRIP_TYPE_NOPS (op0);
1394 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1395 ? truthvalue_false_node
1396 : truthvalue_true_node));
1397 c_disable_warnings (unused_p);
1398 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1399 STRIP_TYPE_NOPS (op1);
1400 c_enable_warnings (unused_p);
1402 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1403 ret = in_init
1404 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1405 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1406 else
1407 ret = fold (expr);
1408 *maybe_const_operands &= op0_const;
1409 *maybe_const_itself &= op0_const_self;
1410 if (!(flag_isoc99
1411 && op0_const
1412 && op0_const_self
1413 && (code == TRUTH_ANDIF_EXPR
1414 ? op0 == truthvalue_false_node
1415 : op0 == truthvalue_true_node)))
1416 *maybe_const_operands &= op1_const;
1417 if (!(op0_const
1418 && op0_const_self
1419 && (code == TRUTH_ANDIF_EXPR
1420 ? op0 == truthvalue_false_node
1421 : op0 == truthvalue_true_node)))
1422 *maybe_const_itself &= op1_const_self;
1423 goto out;
1425 case COND_EXPR:
1426 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1427 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1428 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1429 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1431 STRIP_TYPE_NOPS (op0);
1432 c_disable_warnings (op0 == truthvalue_false_node);
1433 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1434 STRIP_TYPE_NOPS (op1);
1435 c_enable_warnings (op0 == truthvalue_false_node);
1437 c_disable_warnings (op0 == truthvalue_true_node);
1438 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1439 STRIP_TYPE_NOPS (op2);
1440 c_enable_warnings (op0 == truthvalue_true_node);
1442 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1443 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1444 else
1445 ret = fold (expr);
1446 *maybe_const_operands &= op0_const;
1447 *maybe_const_itself &= op0_const_self;
1448 if (!(flag_isoc99
1449 && op0_const
1450 && op0_const_self
1451 && op0 == truthvalue_false_node))
1452 *maybe_const_operands &= op1_const;
1453 if (!(op0_const
1454 && op0_const_self
1455 && op0 == truthvalue_false_node))
1456 *maybe_const_itself &= op1_const_self;
1457 if (!(flag_isoc99
1458 && op0_const
1459 && op0_const_self
1460 && op0 == truthvalue_true_node))
1461 *maybe_const_operands &= op2_const;
1462 if (!(op0_const
1463 && op0_const_self
1464 && op0 == truthvalue_true_node))
1465 *maybe_const_itself &= op2_const_self;
1466 goto out;
1468 case EXCESS_PRECISION_EXPR:
1469 /* Each case where an operand with excess precision may be
1470 encountered must remove the EXCESS_PRECISION_EXPR around
1471 inner operands and possibly put one around the whole
1472 expression or possibly convert to the semantic type (which
1473 c_fully_fold does); we cannot tell at this stage which is
1474 appropriate in any particular case. */
1475 gcc_unreachable ();
1477 default:
1478 /* Various codes may appear through folding built-in functions
1479 and their arguments. */
1480 goto out;
1483 out:
1484 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1485 have been done by this point, so remove them again. */
1486 nowarning |= TREE_NO_WARNING (ret);
1487 STRIP_TYPE_NOPS (ret);
1488 if (nowarning && !TREE_NO_WARNING (ret))
1490 if (!CAN_HAVE_LOCATION_P (ret))
1491 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1492 TREE_NO_WARNING (ret) = 1;
1494 if (ret != expr)
1495 protected_set_expr_location (ret, loc);
1496 return ret;
1499 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1500 return EXP. Otherwise, return either EXP or its known constant
1501 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1502 Is the BLKmode test appropriate? */
1504 tree
1505 decl_constant_value_for_optimization (tree exp)
1507 tree ret;
1509 /* This function is only used by C, for c_fully_fold and other
1510 optimization, and may not be correct for C++. */
1511 if (c_dialect_cxx ())
1512 gcc_unreachable ();
1514 if (!optimize
1515 || TREE_CODE (exp) != VAR_DECL
1516 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1517 || DECL_MODE (exp) == BLKmode)
1518 return exp;
1520 ret = decl_constant_value (exp);
1521 /* Avoid unwanted tree sharing between the initializer and current
1522 function's body where the tree can be modified e.g. by the
1523 gimplifier. */
1524 if (ret != exp && TREE_STATIC (exp))
1525 ret = unshare_expr (ret);
1526 return ret;
1529 /* Print a warning if a constant expression had overflow in folding.
1530 Invoke this function on every expression that the language
1531 requires to be a constant expression.
1532 Note the ANSI C standard says it is erroneous for a
1533 constant expression to overflow. */
1535 void
1536 constant_expression_warning (tree value)
1538 if (warn_overflow && pedantic
1539 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1540 || TREE_CODE (value) == FIXED_CST
1541 || TREE_CODE (value) == VECTOR_CST
1542 || TREE_CODE (value) == COMPLEX_CST)
1543 && TREE_OVERFLOW (value))
1544 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1547 /* The same as above but print an unconditional error. */
1548 void
1549 constant_expression_error (tree value)
1551 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1552 || TREE_CODE (value) == FIXED_CST
1553 || TREE_CODE (value) == VECTOR_CST
1554 || TREE_CODE (value) == COMPLEX_CST)
1555 && TREE_OVERFLOW (value))
1556 error ("overflow in constant expression");
1559 /* Print a warning if an expression had overflow in folding and its
1560 operands hadn't.
1562 Invoke this function on every expression that
1563 (1) appears in the source code, and
1564 (2) is a constant expression that overflowed, and
1565 (3) is not already checked by convert_and_check;
1566 however, do not invoke this function on operands of explicit casts
1567 or when the expression is the result of an operator and any operand
1568 already overflowed. */
1570 void
1571 overflow_warning (location_t loc, tree value)
1573 if (c_inhibit_evaluation_warnings != 0)
1574 return;
1576 switch (TREE_CODE (value))
1578 case INTEGER_CST:
1579 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1580 break;
1582 case REAL_CST:
1583 warning_at (loc, OPT_Woverflow,
1584 "floating point overflow in expression");
1585 break;
1587 case FIXED_CST:
1588 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1589 break;
1591 case VECTOR_CST:
1592 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1593 break;
1595 case COMPLEX_CST:
1596 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1597 warning_at (loc, OPT_Woverflow,
1598 "complex integer overflow in expression");
1599 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1600 warning_at (loc, OPT_Woverflow,
1601 "complex floating point overflow in expression");
1602 break;
1604 default:
1605 break;
1609 /* Warn about uses of logical || / && operator in a context where it
1610 is likely that the bitwise equivalent was intended by the
1611 programmer. We have seen an expression in which CODE is a binary
1612 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1613 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1614 void
1615 warn_logical_operator (location_t location, enum tree_code code, tree type,
1616 enum tree_code code_left, tree op_left,
1617 enum tree_code ARG_UNUSED (code_right), tree op_right)
1619 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1620 int in0_p, in1_p, in_p;
1621 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1622 bool strict_overflow_p = false;
1624 if (code != TRUTH_ANDIF_EXPR
1625 && code != TRUTH_AND_EXPR
1626 && code != TRUTH_ORIF_EXPR
1627 && code != TRUTH_OR_EXPR)
1628 return;
1630 /* Warn if &&/|| are being used in a context where it is
1631 likely that the bitwise equivalent was intended by the
1632 programmer. That is, an expression such as op && MASK
1633 where op should not be any boolean expression, nor a
1634 constant, and mask seems to be a non-boolean integer constant. */
1635 if (!truth_value_p (code_left)
1636 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1637 && !CONSTANT_CLASS_P (op_left)
1638 && !TREE_NO_WARNING (op_left)
1639 && TREE_CODE (op_right) == INTEGER_CST
1640 && !integer_zerop (op_right)
1641 && !integer_onep (op_right))
1643 if (or_op)
1644 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1645 " applied to non-boolean constant");
1646 else
1647 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1648 " applied to non-boolean constant");
1649 TREE_NO_WARNING (op_left) = true;
1650 return;
1653 /* We do not warn for constants because they are typical of macro
1654 expansions that test for features. */
1655 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1656 return;
1658 /* This warning only makes sense with logical operands. */
1659 if (!(truth_value_p (TREE_CODE (op_left))
1660 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1661 || !(truth_value_p (TREE_CODE (op_right))
1662 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1663 return;
1666 /* We first test whether either side separately is trivially true
1667 (with OR) or trivially false (with AND). If so, do not warn.
1668 This is a common idiom for testing ranges of data types in
1669 portable code. */
1670 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1671 if (!lhs)
1672 return;
1673 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1674 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1676 /* If this is an OR operation, invert both sides; now, the result
1677 should be always false to get a warning. */
1678 if (or_op)
1679 in0_p = !in0_p;
1681 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1682 if (tem && integer_zerop (tem))
1683 return;
1685 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1686 if (!rhs)
1687 return;
1688 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1689 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1691 /* If this is an OR operation, invert both sides; now, the result
1692 should be always false to get a warning. */
1693 if (or_op)
1694 in1_p = !in1_p;
1696 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1697 if (tem && integer_zerop (tem))
1698 return;
1700 /* If both expressions have the same operand, if we can merge the
1701 ranges, and if the range test is always false, then warn. */
1702 if (operand_equal_p (lhs, rhs, 0)
1703 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1704 in1_p, low1, high1)
1705 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1706 type, lhs, in_p, low, high))
1707 && integer_zerop (tem))
1709 if (or_op)
1710 warning_at (location, OPT_Wlogical_op,
1711 "logical %<or%> "
1712 "of collectively exhaustive tests is always true");
1713 else
1714 warning_at (location, OPT_Wlogical_op,
1715 "logical %<and%> "
1716 "of mutually exclusive tests is always false");
1721 /* Warn if EXP contains any computations whose results are not used.
1722 Return true if a warning is printed; false otherwise. LOCUS is the
1723 (potential) location of the expression. */
1725 bool
1726 warn_if_unused_value (const_tree exp, location_t locus)
1728 restart:
1729 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1730 return false;
1732 /* Don't warn about void constructs. This includes casting to void,
1733 void function calls, and statement expressions with a final cast
1734 to void. */
1735 if (VOID_TYPE_P (TREE_TYPE (exp)))
1736 return false;
1738 if (EXPR_HAS_LOCATION (exp))
1739 locus = EXPR_LOCATION (exp);
1741 switch (TREE_CODE (exp))
1743 case PREINCREMENT_EXPR:
1744 case POSTINCREMENT_EXPR:
1745 case PREDECREMENT_EXPR:
1746 case POSTDECREMENT_EXPR:
1747 case MODIFY_EXPR:
1748 case INIT_EXPR:
1749 case TARGET_EXPR:
1750 case CALL_EXPR:
1751 case TRY_CATCH_EXPR:
1752 case WITH_CLEANUP_EXPR:
1753 case EXIT_EXPR:
1754 case VA_ARG_EXPR:
1755 return false;
1757 case BIND_EXPR:
1758 /* For a binding, warn if no side effect within it. */
1759 exp = BIND_EXPR_BODY (exp);
1760 goto restart;
1762 case SAVE_EXPR:
1763 case NON_LVALUE_EXPR:
1764 case NOP_EXPR:
1765 exp = TREE_OPERAND (exp, 0);
1766 goto restart;
1768 case TRUTH_ORIF_EXPR:
1769 case TRUTH_ANDIF_EXPR:
1770 /* In && or ||, warn if 2nd operand has no side effect. */
1771 exp = TREE_OPERAND (exp, 1);
1772 goto restart;
1774 case COMPOUND_EXPR:
1775 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1776 return true;
1777 /* Let people do `(foo (), 0)' without a warning. */
1778 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1779 return false;
1780 exp = TREE_OPERAND (exp, 1);
1781 goto restart;
1783 case COND_EXPR:
1784 /* If this is an expression with side effects, don't warn; this
1785 case commonly appears in macro expansions. */
1786 if (TREE_SIDE_EFFECTS (exp))
1787 return false;
1788 goto warn;
1790 case INDIRECT_REF:
1791 /* Don't warn about automatic dereferencing of references, since
1792 the user cannot control it. */
1793 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1795 exp = TREE_OPERAND (exp, 0);
1796 goto restart;
1798 /* Fall through. */
1800 default:
1801 /* Referencing a volatile value is a side effect, so don't warn. */
1802 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1803 && TREE_THIS_VOLATILE (exp))
1804 return false;
1806 /* If this is an expression which has no operands, there is no value
1807 to be unused. There are no such language-independent codes,
1808 but front ends may define such. */
1809 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1810 return false;
1812 warn:
1813 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1818 /* Print a warning about casts that might indicate violation
1819 of strict aliasing rules if -Wstrict-aliasing is used and
1820 strict aliasing mode is in effect. OTYPE is the original
1821 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1823 bool
1824 strict_aliasing_warning (tree otype, tree type, tree expr)
1826 /* Strip pointer conversion chains and get to the correct original type. */
1827 STRIP_NOPS (expr);
1828 otype = TREE_TYPE (expr);
1830 if (!(flag_strict_aliasing
1831 && POINTER_TYPE_P (type)
1832 && POINTER_TYPE_P (otype)
1833 && !VOID_TYPE_P (TREE_TYPE (type)))
1834 /* If the type we are casting to is a ref-all pointer
1835 dereferencing it is always valid. */
1836 || TYPE_REF_CAN_ALIAS_ALL (type))
1837 return false;
1839 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1840 && (DECL_P (TREE_OPERAND (expr, 0))
1841 || handled_component_p (TREE_OPERAND (expr, 0))))
1843 /* Casting the address of an object to non void pointer. Warn
1844 if the cast breaks type based aliasing. */
1845 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1847 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1848 "might break strict-aliasing rules");
1849 return true;
1851 else
1853 /* warn_strict_aliasing >= 3. This includes the default (3).
1854 Only warn if the cast is dereferenced immediately. */
1855 alias_set_type set1 =
1856 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1857 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1859 if (set1 != set2 && set2 != 0
1860 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1862 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1863 "pointer will break strict-aliasing rules");
1864 return true;
1866 else if (warn_strict_aliasing == 2
1867 && !alias_sets_must_conflict_p (set1, set2))
1869 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1870 "pointer might break strict-aliasing rules");
1871 return true;
1875 else
1876 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1878 /* At this level, warn for any conversions, even if an address is
1879 not taken in the same statement. This will likely produce many
1880 false positives, but could be useful to pinpoint problems that
1881 are not revealed at higher levels. */
1882 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1883 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1884 if (!COMPLETE_TYPE_P (type)
1885 || !alias_sets_must_conflict_p (set1, set2))
1887 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1888 "pointer might break strict-aliasing rules");
1889 return true;
1893 return false;
1896 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1897 sizeof as last operand of certain builtins. */
1899 void
1900 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1901 vec<tree, va_gc> *params, tree *sizeof_arg,
1902 bool (*comp_types) (tree, tree))
1904 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1905 bool strop = false, cmp = false;
1906 unsigned int idx = ~0;
1907 location_t loc;
1909 if (TREE_CODE (callee) != FUNCTION_DECL
1910 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1911 || vec_safe_length (params) <= 1)
1912 return;
1914 switch (DECL_FUNCTION_CODE (callee))
1916 case BUILT_IN_STRNCMP:
1917 case BUILT_IN_STRNCASECMP:
1918 cmp = true;
1919 /* FALLTHRU */
1920 case BUILT_IN_STRNCPY:
1921 case BUILT_IN_STRNCPY_CHK:
1922 case BUILT_IN_STRNCAT:
1923 case BUILT_IN_STRNCAT_CHK:
1924 case BUILT_IN_STPNCPY:
1925 case BUILT_IN_STPNCPY_CHK:
1926 strop = true;
1927 /* FALLTHRU */
1928 case BUILT_IN_MEMCPY:
1929 case BUILT_IN_MEMCPY_CHK:
1930 case BUILT_IN_MEMMOVE:
1931 case BUILT_IN_MEMMOVE_CHK:
1932 if (params->length () < 3)
1933 return;
1934 src = (*params)[1];
1935 dest = (*params)[0];
1936 idx = 2;
1937 break;
1938 case BUILT_IN_BCOPY:
1939 if (params->length () < 3)
1940 return;
1941 src = (*params)[0];
1942 dest = (*params)[1];
1943 idx = 2;
1944 break;
1945 case BUILT_IN_MEMCMP:
1946 case BUILT_IN_BCMP:
1947 if (params->length () < 3)
1948 return;
1949 src = (*params)[1];
1950 dest = (*params)[0];
1951 idx = 2;
1952 cmp = true;
1953 break;
1954 case BUILT_IN_MEMSET:
1955 case BUILT_IN_MEMSET_CHK:
1956 if (params->length () < 3)
1957 return;
1958 dest = (*params)[0];
1959 idx = 2;
1960 break;
1961 case BUILT_IN_BZERO:
1962 dest = (*params)[0];
1963 idx = 1;
1964 break;
1965 case BUILT_IN_STRNDUP:
1966 src = (*params)[0];
1967 strop = true;
1968 idx = 1;
1969 break;
1970 case BUILT_IN_MEMCHR:
1971 if (params->length () < 3)
1972 return;
1973 src = (*params)[0];
1974 idx = 2;
1975 break;
1976 case BUILT_IN_SNPRINTF:
1977 case BUILT_IN_SNPRINTF_CHK:
1978 case BUILT_IN_VSNPRINTF:
1979 case BUILT_IN_VSNPRINTF_CHK:
1980 dest = (*params)[0];
1981 idx = 1;
1982 strop = true;
1983 break;
1984 default:
1985 break;
1988 if (idx >= 3)
1989 return;
1991 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1992 return;
1994 type = TYPE_P (sizeof_arg[idx])
1995 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1996 if (!POINTER_TYPE_P (type))
1997 return;
1999 if (dest
2000 && (tem = tree_strip_nop_conversions (dest))
2001 && POINTER_TYPE_P (TREE_TYPE (tem))
2002 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2003 return;
2005 if (src
2006 && (tem = tree_strip_nop_conversions (src))
2007 && POINTER_TYPE_P (TREE_TYPE (tem))
2008 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2009 return;
2011 loc = sizeof_arg_loc[idx];
2013 if (dest && !cmp)
2015 if (!TYPE_P (sizeof_arg[idx])
2016 && operand_equal_p (dest, sizeof_arg[idx], 0)
2017 && comp_types (TREE_TYPE (dest), type))
2019 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2020 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2021 "argument to %<sizeof%> in %qD call is the same "
2022 "expression as the destination; did you mean to "
2023 "remove the addressof?", callee);
2024 else if ((TYPE_PRECISION (TREE_TYPE (type))
2025 == TYPE_PRECISION (char_type_node))
2026 || strop)
2027 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2028 "argument to %<sizeof%> in %qD call is the same "
2029 "expression as the destination; did you mean to "
2030 "provide an explicit length?", callee);
2031 else
2032 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2033 "argument to %<sizeof%> in %qD call is the same "
2034 "expression as the destination; did you mean to "
2035 "dereference it?", callee);
2036 return;
2039 if (POINTER_TYPE_P (TREE_TYPE (dest))
2040 && !strop
2041 && comp_types (TREE_TYPE (dest), type)
2042 && !VOID_TYPE_P (TREE_TYPE (type)))
2044 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2045 "argument to %<sizeof%> in %qD call is the same "
2046 "pointer type %qT as the destination; expected %qT "
2047 "or an explicit length", callee, TREE_TYPE (dest),
2048 TREE_TYPE (TREE_TYPE (dest)));
2049 return;
2053 if (src && !cmp)
2055 if (!TYPE_P (sizeof_arg[idx])
2056 && operand_equal_p (src, sizeof_arg[idx], 0)
2057 && comp_types (TREE_TYPE (src), type))
2059 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2060 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2061 "argument to %<sizeof%> in %qD call is the same "
2062 "expression as the source; did you mean to "
2063 "remove the addressof?", callee);
2064 else if ((TYPE_PRECISION (TREE_TYPE (type))
2065 == TYPE_PRECISION (char_type_node))
2066 || strop)
2067 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2068 "argument to %<sizeof%> in %qD call is the same "
2069 "expression as the source; did you mean to "
2070 "provide an explicit length?", callee);
2071 else
2072 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2073 "argument to %<sizeof%> in %qD call is the same "
2074 "expression as the source; did you mean to "
2075 "dereference it?", callee);
2076 return;
2079 if (POINTER_TYPE_P (TREE_TYPE (src))
2080 && !strop
2081 && comp_types (TREE_TYPE (src), type)
2082 && !VOID_TYPE_P (TREE_TYPE (type)))
2084 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2085 "argument to %<sizeof%> in %qD call is the same "
2086 "pointer type %qT as the source; expected %qT "
2087 "or an explicit length", callee, TREE_TYPE (src),
2088 TREE_TYPE (TREE_TYPE (src)));
2089 return;
2093 if (dest)
2095 if (!TYPE_P (sizeof_arg[idx])
2096 && operand_equal_p (dest, sizeof_arg[idx], 0)
2097 && comp_types (TREE_TYPE (dest), type))
2099 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2100 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2101 "argument to %<sizeof%> in %qD call is the same "
2102 "expression as the first source; did you mean to "
2103 "remove the addressof?", callee);
2104 else if ((TYPE_PRECISION (TREE_TYPE (type))
2105 == TYPE_PRECISION (char_type_node))
2106 || strop)
2107 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2108 "argument to %<sizeof%> in %qD call is the same "
2109 "expression as the first source; did you mean to "
2110 "provide an explicit length?", callee);
2111 else
2112 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2113 "argument to %<sizeof%> in %qD call is the same "
2114 "expression as the first source; did you mean to "
2115 "dereference it?", callee);
2116 return;
2119 if (POINTER_TYPE_P (TREE_TYPE (dest))
2120 && !strop
2121 && comp_types (TREE_TYPE (dest), type)
2122 && !VOID_TYPE_P (TREE_TYPE (type)))
2124 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2125 "argument to %<sizeof%> in %qD call is the same "
2126 "pointer type %qT as the first source; expected %qT "
2127 "or an explicit length", callee, TREE_TYPE (dest),
2128 TREE_TYPE (TREE_TYPE (dest)));
2129 return;
2133 if (src)
2135 if (!TYPE_P (sizeof_arg[idx])
2136 && operand_equal_p (src, sizeof_arg[idx], 0)
2137 && comp_types (TREE_TYPE (src), type))
2139 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2140 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2141 "argument to %<sizeof%> in %qD call is the same "
2142 "expression as the second source; did you mean to "
2143 "remove the addressof?", callee);
2144 else if ((TYPE_PRECISION (TREE_TYPE (type))
2145 == TYPE_PRECISION (char_type_node))
2146 || strop)
2147 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2148 "argument to %<sizeof%> in %qD call is the same "
2149 "expression as the second source; did you mean to "
2150 "provide an explicit length?", callee);
2151 else
2152 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2153 "argument to %<sizeof%> in %qD call is the same "
2154 "expression as the second source; did you mean to "
2155 "dereference it?", callee);
2156 return;
2159 if (POINTER_TYPE_P (TREE_TYPE (src))
2160 && !strop
2161 && comp_types (TREE_TYPE (src), type)
2162 && !VOID_TYPE_P (TREE_TYPE (type)))
2164 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2165 "argument to %<sizeof%> in %qD call is the same "
2166 "pointer type %qT as the second source; expected %qT "
2167 "or an explicit length", callee, TREE_TYPE (src),
2168 TREE_TYPE (TREE_TYPE (src)));
2169 return;
2175 /* Warn for unlikely, improbable, or stupid DECL declarations
2176 of `main'. */
2178 void
2179 check_main_parameter_types (tree decl)
2181 function_args_iterator iter;
2182 tree type;
2183 int argct = 0;
2185 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2187 /* XXX void_type_node belies the abstraction. */
2188 if (type == void_type_node || type == error_mark_node )
2189 break;
2191 ++argct;
2192 switch (argct)
2194 case 1:
2195 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2196 pedwarn (input_location, OPT_Wmain,
2197 "first argument of %q+D should be %<int%>", decl);
2198 break;
2200 case 2:
2201 if (TREE_CODE (type) != POINTER_TYPE
2202 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2203 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2204 != char_type_node))
2205 pedwarn (input_location, OPT_Wmain,
2206 "second argument of %q+D should be %<char **%>", decl);
2207 break;
2209 case 3:
2210 if (TREE_CODE (type) != POINTER_TYPE
2211 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2212 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2213 != char_type_node))
2214 pedwarn (input_location, OPT_Wmain,
2215 "third argument of %q+D should probably be "
2216 "%<char **%>", decl);
2217 break;
2221 /* It is intentional that this message does not mention the third
2222 argument because it's only mentioned in an appendix of the
2223 standard. */
2224 if (argct > 0 && (argct < 2 || argct > 3))
2225 pedwarn (input_location, OPT_Wmain,
2226 "%q+D takes only zero or two arguments", decl);
2229 /* True if pointers to distinct types T1 and T2 can be converted to
2230 each other without an explicit cast. Only returns true for opaque
2231 vector types. */
2232 bool
2233 vector_targets_convertible_p (const_tree t1, const_tree t2)
2235 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2236 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2237 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2238 return true;
2240 return false;
2243 /* True if vector types T1 and T2 can be converted to each other
2244 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2245 can only be converted with -flax-vector-conversions yet that is not
2246 in effect, emit a note telling the user about that option if such
2247 a note has not previously been emitted. */
2248 bool
2249 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2251 static bool emitted_lax_note = false;
2252 bool convertible_lax;
2254 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2255 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2256 return true;
2258 convertible_lax =
2259 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2260 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2261 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2262 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2263 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2265 if (!convertible_lax || flag_lax_vector_conversions)
2266 return convertible_lax;
2268 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2269 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2270 return true;
2272 if (emit_lax_note && !emitted_lax_note)
2274 emitted_lax_note = true;
2275 inform (input_location, "use -flax-vector-conversions to permit "
2276 "conversions between vectors with differing "
2277 "element types or numbers of subparts");
2280 return false;
2283 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2284 and have vector types, V0 has the same type as V1, and the number of
2285 elements of V0, V1, MASK is the same.
2287 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2288 called with two arguments. In this case implementation passes the
2289 first argument twice in order to share the same tree code. This fact
2290 could enable the mask-values being twice the vector length. This is
2291 an implementation accident and this semantics is not guaranteed to
2292 the user. */
2293 tree
2294 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2296 tree ret;
2297 bool wrap = true;
2298 bool maybe_const = false;
2299 bool two_arguments = false;
2301 if (v1 == NULL_TREE)
2303 two_arguments = true;
2304 v1 = v0;
2307 if (v0 == error_mark_node || v1 == error_mark_node
2308 || mask == error_mark_node)
2309 return error_mark_node;
2311 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2312 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2314 error_at (loc, "__builtin_shuffle last argument must "
2315 "be an integer vector");
2316 return error_mark_node;
2319 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2320 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2322 error_at (loc, "__builtin_shuffle arguments must be vectors");
2323 return error_mark_node;
2326 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2328 error_at (loc, "__builtin_shuffle argument vectors must be of "
2329 "the same type");
2330 return error_mark_node;
2333 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2334 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2335 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2336 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2338 error_at (loc, "__builtin_shuffle number of elements of the "
2339 "argument vector(s) and the mask vector should "
2340 "be the same");
2341 return error_mark_node;
2344 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2345 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2347 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2348 "must have the same size as inner type of the mask");
2349 return error_mark_node;
2352 if (!c_dialect_cxx ())
2354 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2355 v0 = c_fully_fold (v0, false, &maybe_const);
2356 wrap &= maybe_const;
2358 if (two_arguments)
2359 v1 = v0 = save_expr (v0);
2360 else
2362 v1 = c_fully_fold (v1, false, &maybe_const);
2363 wrap &= maybe_const;
2366 mask = c_fully_fold (mask, false, &maybe_const);
2367 wrap &= maybe_const;
2370 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2372 if (!c_dialect_cxx () && !wrap)
2373 ret = c_wrap_maybe_const (ret, true);
2375 return ret;
2378 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2379 to integral type. */
2381 static tree
2382 c_common_get_narrower (tree op, int *unsignedp_ptr)
2384 op = get_narrower (op, unsignedp_ptr);
2386 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2387 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2389 /* C++0x scoped enumerations don't implicitly convert to integral
2390 type; if we stripped an explicit conversion to a larger type we
2391 need to replace it so common_type will still work. */
2392 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2393 TYPE_UNSIGNED (TREE_TYPE (op)));
2394 op = fold_convert (type, op);
2396 return op;
2399 /* This is a helper function of build_binary_op.
2401 For certain operations if both args were extended from the same
2402 smaller type, do the arithmetic in that type and then extend.
2404 BITWISE indicates a bitwise operation.
2405 For them, this optimization is safe only if
2406 both args are zero-extended or both are sign-extended.
2407 Otherwise, we might change the result.
2408 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2409 but calculated in (unsigned short) it would be (unsigned short)-1.
2411 tree
2412 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2414 int unsigned0, unsigned1;
2415 tree arg0, arg1;
2416 int uns;
2417 tree type;
2419 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2420 excessive narrowing when we call get_narrower below. For
2421 example, suppose that OP0 is of unsigned int extended
2422 from signed char and that RESULT_TYPE is long long int.
2423 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2424 like
2426 (long long int) (unsigned int) signed_char
2428 which get_narrower would narrow down to
2430 (unsigned int) signed char
2432 If we do not cast OP0 first, get_narrower would return
2433 signed_char, which is inconsistent with the case of the
2434 explicit cast. */
2435 op0 = convert (result_type, op0);
2436 op1 = convert (result_type, op1);
2438 arg0 = c_common_get_narrower (op0, &unsigned0);
2439 arg1 = c_common_get_narrower (op1, &unsigned1);
2441 /* UNS is 1 if the operation to be done is an unsigned one. */
2442 uns = TYPE_UNSIGNED (result_type);
2444 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2445 but it *requires* conversion to FINAL_TYPE. */
2447 if ((TYPE_PRECISION (TREE_TYPE (op0))
2448 == TYPE_PRECISION (TREE_TYPE (arg0)))
2449 && TREE_TYPE (op0) != result_type)
2450 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2451 if ((TYPE_PRECISION (TREE_TYPE (op1))
2452 == TYPE_PRECISION (TREE_TYPE (arg1)))
2453 && TREE_TYPE (op1) != result_type)
2454 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2456 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2458 /* For bitwise operations, signedness of nominal type
2459 does not matter. Consider only how operands were extended. */
2460 if (bitwise)
2461 uns = unsigned0;
2463 /* Note that in all three cases below we refrain from optimizing
2464 an unsigned operation on sign-extended args.
2465 That would not be valid. */
2467 /* Both args variable: if both extended in same way
2468 from same width, do it in that width.
2469 Do it unsigned if args were zero-extended. */
2470 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2471 < TYPE_PRECISION (result_type))
2472 && (TYPE_PRECISION (TREE_TYPE (arg1))
2473 == TYPE_PRECISION (TREE_TYPE (arg0)))
2474 && unsigned0 == unsigned1
2475 && (unsigned0 || !uns))
2476 return c_common_signed_or_unsigned_type
2477 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2479 else if (TREE_CODE (arg0) == INTEGER_CST
2480 && (unsigned1 || !uns)
2481 && (TYPE_PRECISION (TREE_TYPE (arg1))
2482 < TYPE_PRECISION (result_type))
2483 && (type
2484 = c_common_signed_or_unsigned_type (unsigned1,
2485 TREE_TYPE (arg1)))
2486 && !POINTER_TYPE_P (type)
2487 && int_fits_type_p (arg0, type))
2488 return type;
2490 else if (TREE_CODE (arg1) == INTEGER_CST
2491 && (unsigned0 || !uns)
2492 && (TYPE_PRECISION (TREE_TYPE (arg0))
2493 < TYPE_PRECISION (result_type))
2494 && (type
2495 = c_common_signed_or_unsigned_type (unsigned0,
2496 TREE_TYPE (arg0)))
2497 && !POINTER_TYPE_P (type)
2498 && int_fits_type_p (arg1, type))
2499 return type;
2501 return result_type;
2504 /* Checks if expression EXPR of real/integer type cannot be converted
2505 to the real/integer type TYPE. Function returns true when:
2506 * EXPR is a constant which cannot be exactly converted to TYPE
2507 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2508 for EXPR type and TYPE being both integers or both real.
2509 * EXPR is not a constant of real type and TYPE is an integer.
2510 * EXPR is not a constant of integer type which cannot be
2511 exactly converted to real type.
2512 Function allows conversions between types of different signedness and
2513 does not return true in that case. Function can produce signedness
2514 warnings if PRODUCE_WARNS is true. */
2515 bool
2516 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2518 bool give_warning = false;
2519 tree expr_type = TREE_TYPE (expr);
2520 location_t loc = EXPR_LOC_OR_HERE (expr);
2522 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2524 /* Warn for real constant that is not an exact integer converted
2525 to integer type. */
2526 if (TREE_CODE (expr_type) == REAL_TYPE
2527 && TREE_CODE (type) == INTEGER_TYPE)
2529 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2530 give_warning = true;
2532 /* Warn for an integer constant that does not fit into integer type. */
2533 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2534 && TREE_CODE (type) == INTEGER_TYPE
2535 && !int_fits_type_p (expr, type))
2537 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2538 && tree_int_cst_sgn (expr) < 0)
2540 if (produce_warns)
2541 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2542 " implicitly converted to unsigned type");
2544 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2546 if (produce_warns)
2547 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2548 " constant value to negative integer");
2550 else
2551 give_warning = true;
2553 else if (TREE_CODE (type) == REAL_TYPE)
2555 /* Warn for an integer constant that does not fit into real type. */
2556 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2558 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2559 if (!exact_real_truncate (TYPE_MODE (type), &a))
2560 give_warning = true;
2562 /* Warn for a real constant that does not fit into a smaller
2563 real type. */
2564 else if (TREE_CODE (expr_type) == REAL_TYPE
2565 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2567 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2568 if (!exact_real_truncate (TYPE_MODE (type), &a))
2569 give_warning = true;
2573 else
2575 /* Warn for real types converted to integer types. */
2576 if (TREE_CODE (expr_type) == REAL_TYPE
2577 && TREE_CODE (type) == INTEGER_TYPE)
2578 give_warning = true;
2580 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2581 && TREE_CODE (type) == INTEGER_TYPE)
2583 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2584 expr = get_unwidened (expr, 0);
2585 expr_type = TREE_TYPE (expr);
2587 /* Don't warn for short y; short x = ((int)y & 0xff); */
2588 if (TREE_CODE (expr) == BIT_AND_EXPR
2589 || TREE_CODE (expr) == BIT_IOR_EXPR
2590 || TREE_CODE (expr) == BIT_XOR_EXPR)
2592 /* If both args were extended from a shortest type,
2593 use that type if that is safe. */
2594 expr_type = shorten_binary_op (expr_type,
2595 TREE_OPERAND (expr, 0),
2596 TREE_OPERAND (expr, 1),
2597 /* bitwise */1);
2599 if (TREE_CODE (expr) == BIT_AND_EXPR)
2601 tree op0 = TREE_OPERAND (expr, 0);
2602 tree op1 = TREE_OPERAND (expr, 1);
2603 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2604 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2606 /* If one of the operands is a non-negative constant
2607 that fits in the target type, then the type of the
2608 other operand does not matter. */
2609 if ((TREE_CODE (op0) == INTEGER_CST
2610 && int_fits_type_p (op0, c_common_signed_type (type))
2611 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2612 || (TREE_CODE (op1) == INTEGER_CST
2613 && int_fits_type_p (op1, c_common_signed_type (type))
2614 && int_fits_type_p (op1,
2615 c_common_unsigned_type (type))))
2616 return false;
2617 /* If constant is unsigned and fits in the target
2618 type, then the result will also fit. */
2619 else if ((TREE_CODE (op0) == INTEGER_CST
2620 && unsigned0
2621 && int_fits_type_p (op0, type))
2622 || (TREE_CODE (op1) == INTEGER_CST
2623 && unsigned1
2624 && int_fits_type_p (op1, type)))
2625 return false;
2628 /* Warn for integer types converted to smaller integer types. */
2629 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2630 give_warning = true;
2632 /* When they are the same width but different signedness,
2633 then the value may change. */
2634 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2635 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2636 /* Even when converted to a bigger type, if the type is
2637 unsigned but expr is signed, then negative values
2638 will be changed. */
2639 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2640 && produce_warns)
2641 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2642 "may change the sign of the result",
2643 type, expr_type);
2646 /* Warn for integer types converted to real types if and only if
2647 all the range of values of the integer type cannot be
2648 represented by the real type. */
2649 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2650 && TREE_CODE (type) == REAL_TYPE)
2652 tree type_low_bound, type_high_bound;
2653 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2655 /* Don't warn about char y = 0xff; float x = (int) y; */
2656 expr = get_unwidened (expr, 0);
2657 expr_type = TREE_TYPE (expr);
2659 type_low_bound = TYPE_MIN_VALUE (expr_type);
2660 type_high_bound = TYPE_MAX_VALUE (expr_type);
2661 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2662 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2664 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2665 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2666 give_warning = true;
2669 /* Warn for real types converted to smaller real types. */
2670 else if (TREE_CODE (expr_type) == REAL_TYPE
2671 && TREE_CODE (type) == REAL_TYPE
2672 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2673 give_warning = true;
2676 return give_warning;
2679 /* Warns if the conversion of EXPR to TYPE may alter a value.
2680 This is a helper function for warnings_for_convert_and_check. */
2682 static void
2683 conversion_warning (tree type, tree expr)
2685 tree expr_type = TREE_TYPE (expr);
2686 location_t loc = EXPR_LOC_OR_HERE (expr);
2688 if (!warn_conversion && !warn_sign_conversion)
2689 return;
2691 switch (TREE_CODE (expr))
2693 case EQ_EXPR:
2694 case NE_EXPR:
2695 case LE_EXPR:
2696 case GE_EXPR:
2697 case LT_EXPR:
2698 case GT_EXPR:
2699 case TRUTH_ANDIF_EXPR:
2700 case TRUTH_ORIF_EXPR:
2701 case TRUTH_AND_EXPR:
2702 case TRUTH_OR_EXPR:
2703 case TRUTH_XOR_EXPR:
2704 case TRUTH_NOT_EXPR:
2705 /* Conversion from boolean to a signed:1 bit-field (which only
2706 can hold the values 0 and -1) doesn't lose information - but
2707 it does change the value. */
2708 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2709 warning_at (loc, OPT_Wconversion,
2710 "conversion to %qT from boolean expression", type);
2711 return;
2713 case REAL_CST:
2714 case INTEGER_CST:
2715 if (unsafe_conversion_p (type, expr, true))
2716 warning_at (loc, OPT_Wconversion,
2717 "conversion to %qT alters %qT constant value",
2718 type, expr_type);
2719 return;
2721 case COND_EXPR:
2723 /* In case of COND_EXPR, we do not care about the type of
2724 COND_EXPR, only about the conversion of each operand. */
2725 tree op1 = TREE_OPERAND (expr, 1);
2726 tree op2 = TREE_OPERAND (expr, 2);
2728 conversion_warning (type, op1);
2729 conversion_warning (type, op2);
2730 return;
2733 default: /* 'expr' is not a constant. */
2734 if (unsafe_conversion_p (type, expr, true))
2735 warning_at (loc, OPT_Wconversion,
2736 "conversion to %qT from %qT may alter its value",
2737 type, expr_type);
2741 /* Produce warnings after a conversion. RESULT is the result of
2742 converting EXPR to TYPE. This is a helper function for
2743 convert_and_check and cp_convert_and_check. */
2745 void
2746 warnings_for_convert_and_check (tree type, tree expr, tree result)
2748 location_t loc = EXPR_LOC_OR_HERE (expr);
2750 if (TREE_CODE (expr) == INTEGER_CST
2751 && (TREE_CODE (type) == INTEGER_TYPE
2752 || TREE_CODE (type) == ENUMERAL_TYPE)
2753 && !int_fits_type_p (expr, type))
2755 /* Do not diagnose overflow in a constant expression merely
2756 because a conversion overflowed. */
2757 if (TREE_OVERFLOW (result))
2758 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2760 if (TYPE_UNSIGNED (type))
2762 /* This detects cases like converting -129 or 256 to
2763 unsigned char. */
2764 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2765 warning_at (loc, OPT_Woverflow,
2766 "large integer implicitly truncated to unsigned type");
2767 else
2768 conversion_warning (type, expr);
2770 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2771 warning (OPT_Woverflow,
2772 "overflow in implicit constant conversion");
2773 /* No warning for converting 0x80000000 to int. */
2774 else if (pedantic
2775 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2776 || TYPE_PRECISION (TREE_TYPE (expr))
2777 != TYPE_PRECISION (type)))
2778 warning_at (loc, OPT_Woverflow,
2779 "overflow in implicit constant conversion");
2781 else
2782 conversion_warning (type, expr);
2784 else if ((TREE_CODE (result) == INTEGER_CST
2785 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2786 warning_at (loc, OPT_Woverflow,
2787 "overflow in implicit constant conversion");
2788 else
2789 conversion_warning (type, expr);
2793 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2794 Invoke this function on every expression that is converted implicitly,
2795 i.e. because of language rules and not because of an explicit cast. */
2797 tree
2798 convert_and_check (tree type, tree expr)
2800 tree result;
2801 tree expr_for_warning;
2803 /* Convert from a value with possible excess precision rather than
2804 via the semantic type, but do not warn about values not fitting
2805 exactly in the semantic type. */
2806 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2808 tree orig_type = TREE_TYPE (expr);
2809 expr = TREE_OPERAND (expr, 0);
2810 expr_for_warning = convert (orig_type, expr);
2811 if (orig_type == type)
2812 return expr_for_warning;
2814 else
2815 expr_for_warning = expr;
2817 if (TREE_TYPE (expr) == type)
2818 return expr;
2820 result = convert (type, expr);
2822 if (c_inhibit_evaluation_warnings == 0
2823 && !TREE_OVERFLOW_P (expr)
2824 && result != error_mark_node)
2825 warnings_for_convert_and_check (type, expr_for_warning, result);
2827 return result;
2830 /* A node in a list that describes references to variables (EXPR), which are
2831 either read accesses if WRITER is zero, or write accesses, in which case
2832 WRITER is the parent of EXPR. */
2833 struct tlist
2835 struct tlist *next;
2836 tree expr, writer;
2839 /* Used to implement a cache the results of a call to verify_tree. We only
2840 use this for SAVE_EXPRs. */
2841 struct tlist_cache
2843 struct tlist_cache *next;
2844 struct tlist *cache_before_sp;
2845 struct tlist *cache_after_sp;
2846 tree expr;
2849 /* Obstack to use when allocating tlist structures, and corresponding
2850 firstobj. */
2851 static struct obstack tlist_obstack;
2852 static char *tlist_firstobj = 0;
2854 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2855 warnings. */
2856 static struct tlist *warned_ids;
2857 /* SAVE_EXPRs need special treatment. We process them only once and then
2858 cache the results. */
2859 static struct tlist_cache *save_expr_cache;
2861 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2862 static void merge_tlist (struct tlist **, struct tlist *, int);
2863 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2864 static int warning_candidate_p (tree);
2865 static bool candidate_equal_p (const_tree, const_tree);
2866 static void warn_for_collisions (struct tlist *);
2867 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2868 static struct tlist *new_tlist (struct tlist *, tree, tree);
2870 /* Create a new struct tlist and fill in its fields. */
2871 static struct tlist *
2872 new_tlist (struct tlist *next, tree t, tree writer)
2874 struct tlist *l;
2875 l = XOBNEW (&tlist_obstack, struct tlist);
2876 l->next = next;
2877 l->expr = t;
2878 l->writer = writer;
2879 return l;
2882 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2883 is nonnull, we ignore any node we find which has a writer equal to it. */
2885 static void
2886 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2888 while (add)
2890 struct tlist *next = add->next;
2891 if (!copy)
2892 add->next = *to;
2893 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2894 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2895 add = next;
2899 /* Merge the nodes of ADD into TO. This merging process is done so that for
2900 each variable that already exists in TO, no new node is added; however if
2901 there is a write access recorded in ADD, and an occurrence on TO is only
2902 a read access, then the occurrence in TO will be modified to record the
2903 write. */
2905 static void
2906 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2908 struct tlist **end = to;
2910 while (*end)
2911 end = &(*end)->next;
2913 while (add)
2915 int found = 0;
2916 struct tlist *tmp2;
2917 struct tlist *next = add->next;
2919 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2920 if (candidate_equal_p (tmp2->expr, add->expr))
2922 found = 1;
2923 if (!tmp2->writer)
2924 tmp2->writer = add->writer;
2926 if (!found)
2928 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2929 end = &(*end)->next;
2930 *end = 0;
2932 add = next;
2936 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2937 references in list LIST conflict with it, excluding reads if ONLY writers
2938 is nonzero. */
2940 static void
2941 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2942 int only_writes)
2944 struct tlist *tmp;
2946 /* Avoid duplicate warnings. */
2947 for (tmp = warned_ids; tmp; tmp = tmp->next)
2948 if (candidate_equal_p (tmp->expr, written))
2949 return;
2951 while (list)
2953 if (candidate_equal_p (list->expr, written)
2954 && !candidate_equal_p (list->writer, writer)
2955 && (!only_writes || list->writer))
2957 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2958 warning_at (EXPR_LOC_OR_HERE (writer),
2959 OPT_Wsequence_point, "operation on %qE may be undefined",
2960 list->expr);
2962 list = list->next;
2966 /* Given a list LIST of references to variables, find whether any of these
2967 can cause conflicts due to missing sequence points. */
2969 static void
2970 warn_for_collisions (struct tlist *list)
2972 struct tlist *tmp;
2974 for (tmp = list; tmp; tmp = tmp->next)
2976 if (tmp->writer)
2977 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2981 /* Return nonzero if X is a tree that can be verified by the sequence point
2982 warnings. */
2983 static int
2984 warning_candidate_p (tree x)
2986 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2987 return 0;
2989 if (TREE_CODE (x) == BLOCK)
2990 return 0;
2992 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2993 (lvalue_p) crash on TRY/CATCH. */
2994 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2995 return 0;
2997 if (!lvalue_p (x))
2998 return 0;
3000 /* No point to track non-const calls, they will never satisfy
3001 operand_equal_p. */
3002 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3003 return 0;
3005 if (TREE_CODE (x) == STRING_CST)
3006 return 0;
3008 return 1;
3011 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3012 static bool
3013 candidate_equal_p (const_tree x, const_tree y)
3015 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3018 /* Walk the tree X, and record accesses to variables. If X is written by the
3019 parent tree, WRITER is the parent.
3020 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3021 expression or its only operand forces a sequence point, then everything up
3022 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3023 in PNO_SP.
3024 Once we return, we will have emitted warnings if any subexpression before
3025 such a sequence point could be undefined. On a higher level, however, the
3026 sequence point may not be relevant, and we'll merge the two lists.
3028 Example: (b++, a) + b;
3029 The call that processes the COMPOUND_EXPR will store the increment of B
3030 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3031 processes the PLUS_EXPR will need to merge the two lists so that
3032 eventually, all accesses end up on the same list (and we'll warn about the
3033 unordered subexpressions b++ and b.
3035 A note on merging. If we modify the former example so that our expression
3036 becomes
3037 (b++, b) + a
3038 care must be taken not simply to add all three expressions into the final
3039 PNO_SP list. The function merge_tlist takes care of that by merging the
3040 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3041 way, so that no more than one access to B is recorded. */
3043 static void
3044 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3045 tree writer)
3047 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3048 enum tree_code code;
3049 enum tree_code_class cl;
3051 /* X may be NULL if it is the operand of an empty statement expression
3052 ({ }). */
3053 if (x == NULL)
3054 return;
3056 restart:
3057 code = TREE_CODE (x);
3058 cl = TREE_CODE_CLASS (code);
3060 if (warning_candidate_p (x))
3061 *pno_sp = new_tlist (*pno_sp, x, writer);
3063 switch (code)
3065 case CONSTRUCTOR:
3066 return;
3068 case COMPOUND_EXPR:
3069 case TRUTH_ANDIF_EXPR:
3070 case TRUTH_ORIF_EXPR:
3071 tmp_before = tmp_nosp = tmp_list3 = 0;
3072 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3073 warn_for_collisions (tmp_nosp);
3074 merge_tlist (pbefore_sp, tmp_before, 0);
3075 merge_tlist (pbefore_sp, tmp_nosp, 0);
3076 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3077 merge_tlist (pbefore_sp, tmp_list3, 0);
3078 return;
3080 case COND_EXPR:
3081 tmp_before = tmp_list2 = 0;
3082 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3083 warn_for_collisions (tmp_list2);
3084 merge_tlist (pbefore_sp, tmp_before, 0);
3085 merge_tlist (pbefore_sp, tmp_list2, 1);
3087 tmp_list3 = tmp_nosp = 0;
3088 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3089 warn_for_collisions (tmp_nosp);
3090 merge_tlist (pbefore_sp, tmp_list3, 0);
3092 tmp_list3 = tmp_list2 = 0;
3093 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3094 warn_for_collisions (tmp_list2);
3095 merge_tlist (pbefore_sp, tmp_list3, 0);
3096 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3097 two first, to avoid warning for (a ? b++ : b++). */
3098 merge_tlist (&tmp_nosp, tmp_list2, 0);
3099 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3100 return;
3102 case PREDECREMENT_EXPR:
3103 case PREINCREMENT_EXPR:
3104 case POSTDECREMENT_EXPR:
3105 case POSTINCREMENT_EXPR:
3106 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3107 return;
3109 case MODIFY_EXPR:
3110 tmp_before = tmp_nosp = tmp_list3 = 0;
3111 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3112 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3113 /* Expressions inside the LHS are not ordered wrt. the sequence points
3114 in the RHS. Example:
3115 *a = (a++, 2)
3116 Despite the fact that the modification of "a" is in the before_sp
3117 list (tmp_before), it conflicts with the use of "a" in the LHS.
3118 We can handle this by adding the contents of tmp_list3
3119 to those of tmp_before, and redoing the collision warnings for that
3120 list. */
3121 add_tlist (&tmp_before, tmp_list3, x, 1);
3122 warn_for_collisions (tmp_before);
3123 /* Exclude the LHS itself here; we first have to merge it into the
3124 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3125 didn't exclude the LHS, we'd get it twice, once as a read and once
3126 as a write. */
3127 add_tlist (pno_sp, tmp_list3, x, 0);
3128 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3130 merge_tlist (pbefore_sp, tmp_before, 0);
3131 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3132 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3133 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3134 return;
3136 case CALL_EXPR:
3137 /* We need to warn about conflicts among arguments and conflicts between
3138 args and the function address. Side effects of the function address,
3139 however, are not ordered by the sequence point of the call. */
3141 call_expr_arg_iterator iter;
3142 tree arg;
3143 tmp_before = tmp_nosp = 0;
3144 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3145 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3147 tmp_list2 = tmp_list3 = 0;
3148 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3149 merge_tlist (&tmp_list3, tmp_list2, 0);
3150 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3152 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3153 warn_for_collisions (tmp_before);
3154 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3155 return;
3158 case TREE_LIST:
3159 /* Scan all the list, e.g. indices of multi dimensional array. */
3160 while (x)
3162 tmp_before = tmp_nosp = 0;
3163 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3164 merge_tlist (&tmp_nosp, tmp_before, 0);
3165 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3166 x = TREE_CHAIN (x);
3168 return;
3170 case SAVE_EXPR:
3172 struct tlist_cache *t;
3173 for (t = save_expr_cache; t; t = t->next)
3174 if (candidate_equal_p (t->expr, x))
3175 break;
3177 if (!t)
3179 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3180 t->next = save_expr_cache;
3181 t->expr = x;
3182 save_expr_cache = t;
3184 tmp_before = tmp_nosp = 0;
3185 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3186 warn_for_collisions (tmp_nosp);
3188 tmp_list3 = 0;
3189 while (tmp_nosp)
3191 struct tlist *t = tmp_nosp;
3192 tmp_nosp = t->next;
3193 merge_tlist (&tmp_list3, t, 0);
3195 t->cache_before_sp = tmp_before;
3196 t->cache_after_sp = tmp_list3;
3198 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3199 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3200 return;
3203 case ADDR_EXPR:
3204 x = TREE_OPERAND (x, 0);
3205 if (DECL_P (x))
3206 return;
3207 writer = 0;
3208 goto restart;
3210 default:
3211 /* For other expressions, simply recurse on their operands.
3212 Manual tail recursion for unary expressions.
3213 Other non-expressions need not be processed. */
3214 if (cl == tcc_unary)
3216 x = TREE_OPERAND (x, 0);
3217 writer = 0;
3218 goto restart;
3220 else if (IS_EXPR_CODE_CLASS (cl))
3222 int lp;
3223 int max = TREE_OPERAND_LENGTH (x);
3224 for (lp = 0; lp < max; lp++)
3226 tmp_before = tmp_nosp = 0;
3227 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3228 merge_tlist (&tmp_nosp, tmp_before, 0);
3229 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3232 return;
3236 /* Try to warn for undefined behavior in EXPR due to missing sequence
3237 points. */
3239 DEBUG_FUNCTION void
3240 verify_sequence_points (tree expr)
3242 struct tlist *before_sp = 0, *after_sp = 0;
3244 warned_ids = 0;
3245 save_expr_cache = 0;
3246 if (tlist_firstobj == 0)
3248 gcc_obstack_init (&tlist_obstack);
3249 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3252 verify_tree (expr, &before_sp, &after_sp, 0);
3253 warn_for_collisions (after_sp);
3254 obstack_free (&tlist_obstack, tlist_firstobj);
3257 /* Validate the expression after `case' and apply default promotions. */
3259 static tree
3260 check_case_value (tree value)
3262 if (value == NULL_TREE)
3263 return value;
3265 if (TREE_CODE (value) == INTEGER_CST)
3266 /* Promote char or short to int. */
3267 value = perform_integral_promotions (value);
3268 else if (value != error_mark_node)
3270 error ("case label does not reduce to an integer constant");
3271 value = error_mark_node;
3274 constant_expression_warning (value);
3276 return value;
3279 /* See if the case values LOW and HIGH are in the range of the original
3280 type (i.e. before the default conversion to int) of the switch testing
3281 expression.
3282 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3283 the type before promoting it. CASE_LOW_P is a pointer to the lower
3284 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3285 if the case is not a case range.
3286 The caller has to make sure that we are not called with NULL for
3287 CASE_LOW_P (i.e. the default case).
3288 Returns true if the case label is in range of ORIG_TYPE (saturated or
3289 untouched) or false if the label is out of range. */
3291 static bool
3292 check_case_bounds (tree type, tree orig_type,
3293 tree *case_low_p, tree *case_high_p)
3295 tree min_value, max_value;
3296 tree case_low = *case_low_p;
3297 tree case_high = case_high_p ? *case_high_p : case_low;
3299 /* If there was a problem with the original type, do nothing. */
3300 if (orig_type == error_mark_node)
3301 return true;
3303 min_value = TYPE_MIN_VALUE (orig_type);
3304 max_value = TYPE_MAX_VALUE (orig_type);
3306 /* Case label is less than minimum for type. */
3307 if (tree_int_cst_compare (case_low, min_value) < 0
3308 && tree_int_cst_compare (case_high, min_value) < 0)
3310 warning (0, "case label value is less than minimum value for type");
3311 return false;
3314 /* Case value is greater than maximum for type. */
3315 if (tree_int_cst_compare (case_low, max_value) > 0
3316 && tree_int_cst_compare (case_high, max_value) > 0)
3318 warning (0, "case label value exceeds maximum value for type");
3319 return false;
3322 /* Saturate lower case label value to minimum. */
3323 if (tree_int_cst_compare (case_high, min_value) >= 0
3324 && tree_int_cst_compare (case_low, min_value) < 0)
3326 warning (0, "lower value in case label range"
3327 " less than minimum value for type");
3328 case_low = min_value;
3331 /* Saturate upper case label value to maximum. */
3332 if (tree_int_cst_compare (case_low, max_value) <= 0
3333 && tree_int_cst_compare (case_high, max_value) > 0)
3335 warning (0, "upper value in case label range"
3336 " exceeds maximum value for type");
3337 case_high = max_value;
3340 if (*case_low_p != case_low)
3341 *case_low_p = convert (type, case_low);
3342 if (case_high_p && *case_high_p != case_high)
3343 *case_high_p = convert (type, case_high);
3345 return true;
3348 /* Return an integer type with BITS bits of precision,
3349 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3351 tree
3352 c_common_type_for_size (unsigned int bits, int unsignedp)
3354 if (bits == TYPE_PRECISION (integer_type_node))
3355 return unsignedp ? unsigned_type_node : integer_type_node;
3357 if (bits == TYPE_PRECISION (signed_char_type_node))
3358 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3360 if (bits == TYPE_PRECISION (short_integer_type_node))
3361 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3363 if (bits == TYPE_PRECISION (long_integer_type_node))
3364 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3366 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3367 return (unsignedp ? long_long_unsigned_type_node
3368 : long_long_integer_type_node);
3370 if (int128_integer_type_node
3371 && bits == TYPE_PRECISION (int128_integer_type_node))
3372 return (unsignedp ? int128_unsigned_type_node
3373 : int128_integer_type_node);
3375 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3376 return (unsignedp ? widest_unsigned_literal_type_node
3377 : widest_integer_literal_type_node);
3379 if (bits <= TYPE_PRECISION (intQI_type_node))
3380 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3382 if (bits <= TYPE_PRECISION (intHI_type_node))
3383 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3385 if (bits <= TYPE_PRECISION (intSI_type_node))
3386 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3388 if (bits <= TYPE_PRECISION (intDI_type_node))
3389 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3391 return 0;
3394 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3395 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3396 and saturating if SATP is nonzero, otherwise not saturating. */
3398 tree
3399 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3400 int unsignedp, int satp)
3402 enum machine_mode mode;
3403 if (ibit == 0)
3404 mode = unsignedp ? UQQmode : QQmode;
3405 else
3406 mode = unsignedp ? UHAmode : HAmode;
3408 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3409 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3410 break;
3412 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3414 sorry ("GCC cannot support operators with integer types and "
3415 "fixed-point types that have too many integral and "
3416 "fractional bits together");
3417 return 0;
3420 return c_common_type_for_mode (mode, satp);
3423 /* Used for communication between c_common_type_for_mode and
3424 c_register_builtin_type. */
3425 static GTY(()) tree registered_builtin_types;
3427 /* Return a data type that has machine mode MODE.
3428 If the mode is an integer,
3429 then UNSIGNEDP selects between signed and unsigned types.
3430 If the mode is a fixed-point mode,
3431 then UNSIGNEDP selects between saturating and nonsaturating types. */
3433 tree
3434 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3436 tree t;
3438 if (mode == TYPE_MODE (integer_type_node))
3439 return unsignedp ? unsigned_type_node : integer_type_node;
3441 if (mode == TYPE_MODE (signed_char_type_node))
3442 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3444 if (mode == TYPE_MODE (short_integer_type_node))
3445 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3447 if (mode == TYPE_MODE (long_integer_type_node))
3448 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3450 if (mode == TYPE_MODE (long_long_integer_type_node))
3451 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3453 if (int128_integer_type_node
3454 && mode == TYPE_MODE (int128_integer_type_node))
3455 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3457 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3458 return unsignedp ? widest_unsigned_literal_type_node
3459 : widest_integer_literal_type_node;
3461 if (mode == QImode)
3462 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3464 if (mode == HImode)
3465 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3467 if (mode == SImode)
3468 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3470 if (mode == DImode)
3471 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3473 #if HOST_BITS_PER_WIDE_INT >= 64
3474 if (mode == TYPE_MODE (intTI_type_node))
3475 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3476 #endif
3478 if (mode == TYPE_MODE (float_type_node))
3479 return float_type_node;
3481 if (mode == TYPE_MODE (double_type_node))
3482 return double_type_node;
3484 if (mode == TYPE_MODE (long_double_type_node))
3485 return long_double_type_node;
3487 if (mode == TYPE_MODE (void_type_node))
3488 return void_type_node;
3490 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3491 return (unsignedp
3492 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3493 : make_signed_type (GET_MODE_PRECISION (mode)));
3495 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3496 return (unsignedp
3497 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3498 : make_signed_type (GET_MODE_PRECISION (mode)));
3500 if (COMPLEX_MODE_P (mode))
3502 enum machine_mode inner_mode;
3503 tree inner_type;
3505 if (mode == TYPE_MODE (complex_float_type_node))
3506 return complex_float_type_node;
3507 if (mode == TYPE_MODE (complex_double_type_node))
3508 return complex_double_type_node;
3509 if (mode == TYPE_MODE (complex_long_double_type_node))
3510 return complex_long_double_type_node;
3512 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3513 return complex_integer_type_node;
3515 inner_mode = GET_MODE_INNER (mode);
3516 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3517 if (inner_type != NULL_TREE)
3518 return build_complex_type (inner_type);
3520 else if (VECTOR_MODE_P (mode))
3522 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3523 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3524 if (inner_type != NULL_TREE)
3525 return build_vector_type_for_mode (inner_type, mode);
3528 if (mode == TYPE_MODE (dfloat32_type_node))
3529 return dfloat32_type_node;
3530 if (mode == TYPE_MODE (dfloat64_type_node))
3531 return dfloat64_type_node;
3532 if (mode == TYPE_MODE (dfloat128_type_node))
3533 return dfloat128_type_node;
3535 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3537 if (mode == TYPE_MODE (short_fract_type_node))
3538 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3539 if (mode == TYPE_MODE (fract_type_node))
3540 return unsignedp ? sat_fract_type_node : fract_type_node;
3541 if (mode == TYPE_MODE (long_fract_type_node))
3542 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3543 if (mode == TYPE_MODE (long_long_fract_type_node))
3544 return unsignedp ? sat_long_long_fract_type_node
3545 : long_long_fract_type_node;
3547 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3548 return unsignedp ? sat_unsigned_short_fract_type_node
3549 : unsigned_short_fract_type_node;
3550 if (mode == TYPE_MODE (unsigned_fract_type_node))
3551 return unsignedp ? sat_unsigned_fract_type_node
3552 : unsigned_fract_type_node;
3553 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3554 return unsignedp ? sat_unsigned_long_fract_type_node
3555 : unsigned_long_fract_type_node;
3556 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3557 return unsignedp ? sat_unsigned_long_long_fract_type_node
3558 : unsigned_long_long_fract_type_node;
3560 if (mode == TYPE_MODE (short_accum_type_node))
3561 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3562 if (mode == TYPE_MODE (accum_type_node))
3563 return unsignedp ? sat_accum_type_node : accum_type_node;
3564 if (mode == TYPE_MODE (long_accum_type_node))
3565 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3566 if (mode == TYPE_MODE (long_long_accum_type_node))
3567 return unsignedp ? sat_long_long_accum_type_node
3568 : long_long_accum_type_node;
3570 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3571 return unsignedp ? sat_unsigned_short_accum_type_node
3572 : unsigned_short_accum_type_node;
3573 if (mode == TYPE_MODE (unsigned_accum_type_node))
3574 return unsignedp ? sat_unsigned_accum_type_node
3575 : unsigned_accum_type_node;
3576 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3577 return unsignedp ? sat_unsigned_long_accum_type_node
3578 : unsigned_long_accum_type_node;
3579 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3580 return unsignedp ? sat_unsigned_long_long_accum_type_node
3581 : unsigned_long_long_accum_type_node;
3583 if (mode == QQmode)
3584 return unsignedp ? sat_qq_type_node : qq_type_node;
3585 if (mode == HQmode)
3586 return unsignedp ? sat_hq_type_node : hq_type_node;
3587 if (mode == SQmode)
3588 return unsignedp ? sat_sq_type_node : sq_type_node;
3589 if (mode == DQmode)
3590 return unsignedp ? sat_dq_type_node : dq_type_node;
3591 if (mode == TQmode)
3592 return unsignedp ? sat_tq_type_node : tq_type_node;
3594 if (mode == UQQmode)
3595 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3596 if (mode == UHQmode)
3597 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3598 if (mode == USQmode)
3599 return unsignedp ? sat_usq_type_node : usq_type_node;
3600 if (mode == UDQmode)
3601 return unsignedp ? sat_udq_type_node : udq_type_node;
3602 if (mode == UTQmode)
3603 return unsignedp ? sat_utq_type_node : utq_type_node;
3605 if (mode == HAmode)
3606 return unsignedp ? sat_ha_type_node : ha_type_node;
3607 if (mode == SAmode)
3608 return unsignedp ? sat_sa_type_node : sa_type_node;
3609 if (mode == DAmode)
3610 return unsignedp ? sat_da_type_node : da_type_node;
3611 if (mode == TAmode)
3612 return unsignedp ? sat_ta_type_node : ta_type_node;
3614 if (mode == UHAmode)
3615 return unsignedp ? sat_uha_type_node : uha_type_node;
3616 if (mode == USAmode)
3617 return unsignedp ? sat_usa_type_node : usa_type_node;
3618 if (mode == UDAmode)
3619 return unsignedp ? sat_uda_type_node : uda_type_node;
3620 if (mode == UTAmode)
3621 return unsignedp ? sat_uta_type_node : uta_type_node;
3624 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3625 if (TYPE_MODE (TREE_VALUE (t)) == mode
3626 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3627 return TREE_VALUE (t);
3629 return 0;
3632 tree
3633 c_common_unsigned_type (tree type)
3635 return c_common_signed_or_unsigned_type (1, type);
3638 /* Return a signed type the same as TYPE in other respects. */
3640 tree
3641 c_common_signed_type (tree type)
3643 return c_common_signed_or_unsigned_type (0, type);
3646 /* Return a type the same as TYPE except unsigned or
3647 signed according to UNSIGNEDP. */
3649 tree
3650 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3652 tree type1;
3654 /* This block of code emulates the behavior of the old
3655 c_common_unsigned_type. In particular, it returns
3656 long_unsigned_type_node if passed a long, even when a int would
3657 have the same size. This is necessary for warnings to work
3658 correctly in archs where sizeof(int) == sizeof(long) */
3660 type1 = TYPE_MAIN_VARIANT (type);
3661 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3662 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3663 if (type1 == integer_type_node || type1 == unsigned_type_node)
3664 return unsignedp ? unsigned_type_node : integer_type_node;
3665 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3666 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3667 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3668 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3669 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3670 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3671 if (int128_integer_type_node
3672 && (type1 == int128_integer_type_node
3673 || type1 == int128_unsigned_type_node))
3674 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3675 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3676 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3677 #if HOST_BITS_PER_WIDE_INT >= 64
3678 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3679 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3680 #endif
3681 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3682 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3683 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3684 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3685 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3686 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3687 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3688 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3690 #define C_COMMON_FIXED_TYPES(NAME) \
3691 if (type1 == short_ ## NAME ## _type_node \
3692 || type1 == unsigned_short_ ## NAME ## _type_node) \
3693 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3694 : short_ ## NAME ## _type_node; \
3695 if (type1 == NAME ## _type_node \
3696 || type1 == unsigned_ ## NAME ## _type_node) \
3697 return unsignedp ? unsigned_ ## NAME ## _type_node \
3698 : NAME ## _type_node; \
3699 if (type1 == long_ ## NAME ## _type_node \
3700 || type1 == unsigned_long_ ## NAME ## _type_node) \
3701 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3702 : long_ ## NAME ## _type_node; \
3703 if (type1 == long_long_ ## NAME ## _type_node \
3704 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3705 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3706 : long_long_ ## NAME ## _type_node;
3708 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3709 if (type1 == NAME ## _type_node \
3710 || type1 == u ## NAME ## _type_node) \
3711 return unsignedp ? u ## NAME ## _type_node \
3712 : NAME ## _type_node;
3714 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3715 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3716 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3717 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3718 : sat_ ## short_ ## NAME ## _type_node; \
3719 if (type1 == sat_ ## NAME ## _type_node \
3720 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3721 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3722 : sat_ ## NAME ## _type_node; \
3723 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3724 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3725 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3726 : sat_ ## long_ ## NAME ## _type_node; \
3727 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3728 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3729 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3730 : sat_ ## long_long_ ## NAME ## _type_node;
3732 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3733 if (type1 == sat_ ## NAME ## _type_node \
3734 || type1 == sat_ ## u ## NAME ## _type_node) \
3735 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3736 : sat_ ## NAME ## _type_node;
3738 C_COMMON_FIXED_TYPES (fract);
3739 C_COMMON_FIXED_TYPES_SAT (fract);
3740 C_COMMON_FIXED_TYPES (accum);
3741 C_COMMON_FIXED_TYPES_SAT (accum);
3743 C_COMMON_FIXED_MODE_TYPES (qq);
3744 C_COMMON_FIXED_MODE_TYPES (hq);
3745 C_COMMON_FIXED_MODE_TYPES (sq);
3746 C_COMMON_FIXED_MODE_TYPES (dq);
3747 C_COMMON_FIXED_MODE_TYPES (tq);
3748 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3749 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3750 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3751 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3752 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3753 C_COMMON_FIXED_MODE_TYPES (ha);
3754 C_COMMON_FIXED_MODE_TYPES (sa);
3755 C_COMMON_FIXED_MODE_TYPES (da);
3756 C_COMMON_FIXED_MODE_TYPES (ta);
3757 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3758 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3759 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3760 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3762 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3763 the precision; they have precision set to match their range, but
3764 may use a wider mode to match an ABI. If we change modes, we may
3765 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3766 the precision as well, so as to yield correct results for
3767 bit-field types. C++ does not have these separate bit-field
3768 types, and producing a signed or unsigned variant of an
3769 ENUMERAL_TYPE may cause other problems as well. */
3771 if (!INTEGRAL_TYPE_P (type)
3772 || TYPE_UNSIGNED (type) == unsignedp)
3773 return type;
3775 #define TYPE_OK(node) \
3776 (TYPE_MODE (type) == TYPE_MODE (node) \
3777 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3778 if (TYPE_OK (signed_char_type_node))
3779 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3780 if (TYPE_OK (integer_type_node))
3781 return unsignedp ? unsigned_type_node : integer_type_node;
3782 if (TYPE_OK (short_integer_type_node))
3783 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3784 if (TYPE_OK (long_integer_type_node))
3785 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3786 if (TYPE_OK (long_long_integer_type_node))
3787 return (unsignedp ? long_long_unsigned_type_node
3788 : long_long_integer_type_node);
3789 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3790 return (unsignedp ? int128_unsigned_type_node
3791 : int128_integer_type_node);
3792 if (TYPE_OK (widest_integer_literal_type_node))
3793 return (unsignedp ? widest_unsigned_literal_type_node
3794 : widest_integer_literal_type_node);
3796 #if HOST_BITS_PER_WIDE_INT >= 64
3797 if (TYPE_OK (intTI_type_node))
3798 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3799 #endif
3800 if (TYPE_OK (intDI_type_node))
3801 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3802 if (TYPE_OK (intSI_type_node))
3803 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3804 if (TYPE_OK (intHI_type_node))
3805 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3806 if (TYPE_OK (intQI_type_node))
3807 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3808 #undef TYPE_OK
3810 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3813 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3815 tree
3816 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3818 /* Extended integer types of the same width as a standard type have
3819 lesser rank, so those of the same width as int promote to int or
3820 unsigned int and are valid for printf formats expecting int or
3821 unsigned int. To avoid such special cases, avoid creating
3822 extended integer types for bit-fields if a standard integer type
3823 is available. */
3824 if (width == TYPE_PRECISION (integer_type_node))
3825 return unsignedp ? unsigned_type_node : integer_type_node;
3826 if (width == TYPE_PRECISION (signed_char_type_node))
3827 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3828 if (width == TYPE_PRECISION (short_integer_type_node))
3829 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3830 if (width == TYPE_PRECISION (long_integer_type_node))
3831 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3832 if (width == TYPE_PRECISION (long_long_integer_type_node))
3833 return (unsignedp ? long_long_unsigned_type_node
3834 : long_long_integer_type_node);
3835 if (int128_integer_type_node
3836 && width == TYPE_PRECISION (int128_integer_type_node))
3837 return (unsignedp ? int128_unsigned_type_node
3838 : int128_integer_type_node);
3839 return build_nonstandard_integer_type (width, unsignedp);
3842 /* The C version of the register_builtin_type langhook. */
3844 void
3845 c_register_builtin_type (tree type, const char* name)
3847 tree decl;
3849 decl = build_decl (UNKNOWN_LOCATION,
3850 TYPE_DECL, get_identifier (name), type);
3851 DECL_ARTIFICIAL (decl) = 1;
3852 if (!TYPE_NAME (type))
3853 TYPE_NAME (type) = decl;
3854 pushdecl (decl);
3856 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3859 /* Print an error message for invalid operands to arith operation
3860 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3861 LOCATION is the location of the message. */
3863 void
3864 binary_op_error (location_t location, enum tree_code code,
3865 tree type0, tree type1)
3867 const char *opname;
3869 switch (code)
3871 case PLUS_EXPR:
3872 opname = "+"; break;
3873 case MINUS_EXPR:
3874 opname = "-"; break;
3875 case MULT_EXPR:
3876 opname = "*"; break;
3877 case MAX_EXPR:
3878 opname = "max"; break;
3879 case MIN_EXPR:
3880 opname = "min"; break;
3881 case EQ_EXPR:
3882 opname = "=="; break;
3883 case NE_EXPR:
3884 opname = "!="; break;
3885 case LE_EXPR:
3886 opname = "<="; break;
3887 case GE_EXPR:
3888 opname = ">="; break;
3889 case LT_EXPR:
3890 opname = "<"; break;
3891 case GT_EXPR:
3892 opname = ">"; break;
3893 case LSHIFT_EXPR:
3894 opname = "<<"; break;
3895 case RSHIFT_EXPR:
3896 opname = ">>"; break;
3897 case TRUNC_MOD_EXPR:
3898 case FLOOR_MOD_EXPR:
3899 opname = "%"; break;
3900 case TRUNC_DIV_EXPR:
3901 case FLOOR_DIV_EXPR:
3902 opname = "/"; break;
3903 case BIT_AND_EXPR:
3904 opname = "&"; break;
3905 case BIT_IOR_EXPR:
3906 opname = "|"; break;
3907 case TRUTH_ANDIF_EXPR:
3908 opname = "&&"; break;
3909 case TRUTH_ORIF_EXPR:
3910 opname = "||"; break;
3911 case BIT_XOR_EXPR:
3912 opname = "^"; break;
3913 default:
3914 gcc_unreachable ();
3916 error_at (location,
3917 "invalid operands to binary %s (have %qT and %qT)", opname,
3918 type0, type1);
3921 /* Given an expression as a tree, return its original type. Do this
3922 by stripping any conversion that preserves the sign and precision. */
3923 static tree
3924 expr_original_type (tree expr)
3926 STRIP_SIGN_NOPS (expr);
3927 return TREE_TYPE (expr);
3930 /* Subroutine of build_binary_op, used for comparison operations.
3931 See if the operands have both been converted from subword integer types
3932 and, if so, perhaps change them both back to their original type.
3933 This function is also responsible for converting the two operands
3934 to the proper common type for comparison.
3936 The arguments of this function are all pointers to local variables
3937 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3938 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3940 If this function returns nonzero, it means that the comparison has
3941 a constant value. What this function returns is an expression for
3942 that value. */
3944 tree
3945 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3946 enum tree_code *rescode_ptr)
3948 tree type;
3949 tree op0 = *op0_ptr;
3950 tree op1 = *op1_ptr;
3951 int unsignedp0, unsignedp1;
3952 int real1, real2;
3953 tree primop0, primop1;
3954 enum tree_code code = *rescode_ptr;
3955 location_t loc = EXPR_LOC_OR_HERE (op0);
3957 /* Throw away any conversions to wider types
3958 already present in the operands. */
3960 primop0 = c_common_get_narrower (op0, &unsignedp0);
3961 primop1 = c_common_get_narrower (op1, &unsignedp1);
3963 /* If primopN is first sign-extended from primopN's precision to opN's
3964 precision, then zero-extended from opN's precision to
3965 *restype_ptr precision, shortenings might be invalid. */
3966 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3967 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3968 && !unsignedp0
3969 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3970 primop0 = op0;
3971 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3972 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3973 && !unsignedp1
3974 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3975 primop1 = op1;
3977 /* Handle the case that OP0 does not *contain* a conversion
3978 but it *requires* conversion to FINAL_TYPE. */
3980 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3981 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3982 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3983 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3985 /* If one of the operands must be floated, we cannot optimize. */
3986 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3987 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3989 /* If first arg is constant, swap the args (changing operation
3990 so value is preserved), for canonicalization. Don't do this if
3991 the second arg is 0. */
3993 if (TREE_CONSTANT (primop0)
3994 && !integer_zerop (primop1) && !real_zerop (primop1)
3995 && !fixed_zerop (primop1))
3997 tree tem = primop0;
3998 int temi = unsignedp0;
3999 primop0 = primop1;
4000 primop1 = tem;
4001 tem = op0;
4002 op0 = op1;
4003 op1 = tem;
4004 *op0_ptr = op0;
4005 *op1_ptr = op1;
4006 unsignedp0 = unsignedp1;
4007 unsignedp1 = temi;
4008 temi = real1;
4009 real1 = real2;
4010 real2 = temi;
4012 switch (code)
4014 case LT_EXPR:
4015 code = GT_EXPR;
4016 break;
4017 case GT_EXPR:
4018 code = LT_EXPR;
4019 break;
4020 case LE_EXPR:
4021 code = GE_EXPR;
4022 break;
4023 case GE_EXPR:
4024 code = LE_EXPR;
4025 break;
4026 default:
4027 break;
4029 *rescode_ptr = code;
4032 /* If comparing an integer against a constant more bits wide,
4033 maybe we can deduce a value of 1 or 0 independent of the data.
4034 Or else truncate the constant now
4035 rather than extend the variable at run time.
4037 This is only interesting if the constant is the wider arg.
4038 Also, it is not safe if the constant is unsigned and the
4039 variable arg is signed, since in this case the variable
4040 would be sign-extended and then regarded as unsigned.
4041 Our technique fails in this case because the lowest/highest
4042 possible unsigned results don't follow naturally from the
4043 lowest/highest possible values of the variable operand.
4044 For just EQ_EXPR and NE_EXPR there is another technique that
4045 could be used: see if the constant can be faithfully represented
4046 in the other operand's type, by truncating it and reextending it
4047 and see if that preserves the constant's value. */
4049 if (!real1 && !real2
4050 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4051 && TREE_CODE (primop1) == INTEGER_CST
4052 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4054 int min_gt, max_gt, min_lt, max_lt;
4055 tree maxval, minval;
4056 /* 1 if comparison is nominally unsigned. */
4057 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4058 tree val;
4060 type = c_common_signed_or_unsigned_type (unsignedp0,
4061 TREE_TYPE (primop0));
4063 maxval = TYPE_MAX_VALUE (type);
4064 minval = TYPE_MIN_VALUE (type);
4066 if (unsignedp && !unsignedp0)
4067 *restype_ptr = c_common_signed_type (*restype_ptr);
4069 if (TREE_TYPE (primop1) != *restype_ptr)
4071 /* Convert primop1 to target type, but do not introduce
4072 additional overflow. We know primop1 is an int_cst. */
4073 primop1 = force_fit_type_double (*restype_ptr,
4074 tree_to_double_int (primop1),
4075 0, TREE_OVERFLOW (primop1));
4077 if (type != *restype_ptr)
4079 minval = convert (*restype_ptr, minval);
4080 maxval = convert (*restype_ptr, maxval);
4083 if (unsignedp && unsignedp0)
4085 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4086 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4087 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4088 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4090 else
4092 min_gt = INT_CST_LT (primop1, minval);
4093 max_gt = INT_CST_LT (primop1, maxval);
4094 min_lt = INT_CST_LT (minval, primop1);
4095 max_lt = INT_CST_LT (maxval, primop1);
4098 val = 0;
4099 /* This used to be a switch, but Genix compiler can't handle that. */
4100 if (code == NE_EXPR)
4102 if (max_lt || min_gt)
4103 val = truthvalue_true_node;
4105 else if (code == EQ_EXPR)
4107 if (max_lt || min_gt)
4108 val = truthvalue_false_node;
4110 else if (code == LT_EXPR)
4112 if (max_lt)
4113 val = truthvalue_true_node;
4114 if (!min_lt)
4115 val = truthvalue_false_node;
4117 else if (code == GT_EXPR)
4119 if (min_gt)
4120 val = truthvalue_true_node;
4121 if (!max_gt)
4122 val = truthvalue_false_node;
4124 else if (code == LE_EXPR)
4126 if (!max_gt)
4127 val = truthvalue_true_node;
4128 if (min_gt)
4129 val = truthvalue_false_node;
4131 else if (code == GE_EXPR)
4133 if (!min_lt)
4134 val = truthvalue_true_node;
4135 if (max_lt)
4136 val = truthvalue_false_node;
4139 /* If primop0 was sign-extended and unsigned comparison specd,
4140 we did a signed comparison above using the signed type bounds.
4141 But the comparison we output must be unsigned.
4143 Also, for inequalities, VAL is no good; but if the signed
4144 comparison had *any* fixed result, it follows that the
4145 unsigned comparison just tests the sign in reverse
4146 (positive values are LE, negative ones GE).
4147 So we can generate an unsigned comparison
4148 against an extreme value of the signed type. */
4150 if (unsignedp && !unsignedp0)
4152 if (val != 0)
4153 switch (code)
4155 case LT_EXPR:
4156 case GE_EXPR:
4157 primop1 = TYPE_MIN_VALUE (type);
4158 val = 0;
4159 break;
4161 case LE_EXPR:
4162 case GT_EXPR:
4163 primop1 = TYPE_MAX_VALUE (type);
4164 val = 0;
4165 break;
4167 default:
4168 break;
4170 type = c_common_unsigned_type (type);
4173 if (TREE_CODE (primop0) != INTEGER_CST
4174 && c_inhibit_evaluation_warnings == 0)
4176 if (val == truthvalue_false_node)
4177 warning_at (loc, OPT_Wtype_limits,
4178 "comparison is always false due to limited range of data type");
4179 if (val == truthvalue_true_node)
4180 warning_at (loc, OPT_Wtype_limits,
4181 "comparison is always true due to limited range of data type");
4184 if (val != 0)
4186 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4187 if (TREE_SIDE_EFFECTS (primop0))
4188 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4189 return val;
4192 /* Value is not predetermined, but do the comparison
4193 in the type of the operand that is not constant.
4194 TYPE is already properly set. */
4197 /* If either arg is decimal float and the other is float, find the
4198 proper common type to use for comparison. */
4199 else if (real1 && real2
4200 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4201 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4202 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4204 else if (real1 && real2
4205 && (TYPE_PRECISION (TREE_TYPE (primop0))
4206 == TYPE_PRECISION (TREE_TYPE (primop1))))
4207 type = TREE_TYPE (primop0);
4209 /* If args' natural types are both narrower than nominal type
4210 and both extend in the same manner, compare them
4211 in the type of the wider arg.
4212 Otherwise must actually extend both to the nominal
4213 common type lest different ways of extending
4214 alter the result.
4215 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4217 else if (unsignedp0 == unsignedp1 && real1 == real2
4218 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4219 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4221 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4222 type = c_common_signed_or_unsigned_type (unsignedp0
4223 || TYPE_UNSIGNED (*restype_ptr),
4224 type);
4225 /* Make sure shorter operand is extended the right way
4226 to match the longer operand. */
4227 primop0
4228 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4229 TREE_TYPE (primop0)),
4230 primop0);
4231 primop1
4232 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4233 TREE_TYPE (primop1)),
4234 primop1);
4236 else
4238 /* Here we must do the comparison on the nominal type
4239 using the args exactly as we received them. */
4240 type = *restype_ptr;
4241 primop0 = op0;
4242 primop1 = op1;
4244 if (!real1 && !real2 && integer_zerop (primop1)
4245 && TYPE_UNSIGNED (*restype_ptr))
4247 tree value = 0;
4248 /* All unsigned values are >= 0, so we warn. However,
4249 if OP0 is a constant that is >= 0, the signedness of
4250 the comparison isn't an issue, so suppress the
4251 warning. */
4252 bool warn =
4253 warn_type_limits && !in_system_header
4254 && c_inhibit_evaluation_warnings == 0
4255 && !(TREE_CODE (primop0) == INTEGER_CST
4256 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4257 primop0)))
4258 /* Do not warn for enumeration types. */
4259 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4261 switch (code)
4263 case GE_EXPR:
4264 if (warn)
4265 warning_at (loc, OPT_Wtype_limits,
4266 "comparison of unsigned expression >= 0 is always true");
4267 value = truthvalue_true_node;
4268 break;
4270 case LT_EXPR:
4271 if (warn)
4272 warning_at (loc, OPT_Wtype_limits,
4273 "comparison of unsigned expression < 0 is always false");
4274 value = truthvalue_false_node;
4275 break;
4277 default:
4278 break;
4281 if (value != 0)
4283 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4284 if (TREE_SIDE_EFFECTS (primop0))
4285 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4286 primop0, value);
4287 return value;
4292 *op0_ptr = convert (type, primop0);
4293 *op1_ptr = convert (type, primop1);
4295 *restype_ptr = truthvalue_type_node;
4297 return 0;
4300 /* Return a tree for the sum or difference (RESULTCODE says which)
4301 of pointer PTROP and integer INTOP. */
4303 tree
4304 pointer_int_sum (location_t loc, enum tree_code resultcode,
4305 tree ptrop, tree intop)
4307 tree size_exp, ret;
4309 /* The result is a pointer of the same type that is being added. */
4310 tree result_type = TREE_TYPE (ptrop);
4312 /* If the pointer lives in UPC shared memory, then
4313 drop the 'shared' qualifier. */
4314 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4315 result_type = build_upc_unshared_type (result_type);
4317 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4319 pedwarn (loc, OPT_Wpointer_arith,
4320 "pointer of type %<void *%> used in arithmetic");
4321 size_exp = integer_one_node;
4323 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4325 pedwarn (loc, OPT_Wpointer_arith,
4326 "pointer to a function used in arithmetic");
4327 size_exp = integer_one_node;
4329 else
4330 size_exp = size_in_bytes (TREE_TYPE (result_type));
4332 /* We are manipulating pointer values, so we don't need to warn
4333 about relying on undefined signed overflow. We disable the
4334 warning here because we use integer types so fold won't know that
4335 they are really pointers. */
4336 fold_defer_overflow_warnings ();
4338 /* If what we are about to multiply by the size of the elements
4339 contains a constant term, apply distributive law
4340 and multiply that constant term separately.
4341 This helps produce common subexpressions. */
4342 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4343 && !TREE_CONSTANT (intop)
4344 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4345 && TREE_CONSTANT (size_exp)
4346 /* If the constant comes from pointer subtraction,
4347 skip this optimization--it would cause an error. */
4348 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4349 /* If the constant is unsigned, and smaller than the pointer size,
4350 then we must skip this optimization. This is because it could cause
4351 an overflow error if the constant is negative but INTOP is not. */
4352 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4353 || (TYPE_PRECISION (TREE_TYPE (intop))
4354 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4356 enum tree_code subcode = resultcode;
4357 tree int_type = TREE_TYPE (intop);
4358 if (TREE_CODE (intop) == MINUS_EXPR)
4359 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4360 /* Convert both subexpression types to the type of intop,
4361 because weird cases involving pointer arithmetic
4362 can result in a sum or difference with different type args. */
4363 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4364 subcode, ptrop,
4365 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4366 intop = convert (int_type, TREE_OPERAND (intop, 0));
4369 /* Convert the integer argument to a type the same size as sizetype
4370 so the multiply won't overflow spuriously. */
4371 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4372 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4373 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4374 TYPE_UNSIGNED (sizetype)), intop);
4376 /* Replace the integer argument with a suitable product by the object size.
4377 Do this multiplication as signed, then convert to the appropriate type
4378 for the pointer operation and disregard an overflow that occurred only
4379 because of the sign-extension change in the latter conversion. */
4381 tree t = build_binary_op (loc,
4382 MULT_EXPR, intop,
4383 convert (TREE_TYPE (intop), size_exp), 1);
4384 intop = convert (sizetype, t);
4385 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4386 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4387 TREE_INT_CST_HIGH (intop));
4390 /* Create the sum or difference. */
4391 if (resultcode == MINUS_EXPR)
4392 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4394 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4396 fold_undefer_and_ignore_overflow_warnings ();
4398 return ret;
4401 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4402 and if NON_CONST is known not to be permitted in an evaluated part
4403 of a constant expression. */
4405 tree
4406 c_wrap_maybe_const (tree expr, bool non_const)
4408 bool nowarning = TREE_NO_WARNING (expr);
4409 location_t loc = EXPR_LOCATION (expr);
4411 /* This should never be called for C++. */
4412 if (c_dialect_cxx ())
4413 gcc_unreachable ();
4415 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4416 STRIP_TYPE_NOPS (expr);
4417 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4418 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4419 if (nowarning)
4420 TREE_NO_WARNING (expr) = 1;
4421 protected_set_expr_location (expr, loc);
4423 return expr;
4426 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4427 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4428 around the SAVE_EXPR if needed so that c_fully_fold does not need
4429 to look inside SAVE_EXPRs. */
4431 tree
4432 c_save_expr (tree expr)
4434 bool maybe_const = true;
4435 if (c_dialect_cxx ())
4436 return save_expr (expr);
4437 expr = c_fully_fold (expr, false, &maybe_const);
4438 expr = save_expr (expr);
4439 if (!maybe_const)
4440 expr = c_wrap_maybe_const (expr, true);
4441 return expr;
4444 /* Return whether EXPR is a declaration whose address can never be
4445 NULL. */
4447 bool
4448 decl_with_nonnull_addr_p (const_tree expr)
4450 return (DECL_P (expr)
4451 && (TREE_CODE (expr) == PARM_DECL
4452 || TREE_CODE (expr) == LABEL_DECL
4453 || !DECL_WEAK (expr)));
4456 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4457 or for an `if' or `while' statement or ?..: exp. It should already
4458 have been validated to be of suitable type; otherwise, a bad
4459 diagnostic may result.
4461 The EXPR is located at LOCATION.
4463 This preparation consists of taking the ordinary
4464 representation of an expression expr and producing a valid tree
4465 boolean expression describing whether expr is nonzero. We could
4466 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4467 but we optimize comparisons, &&, ||, and !.
4469 The resulting type should always be `truthvalue_type_node'. */
4471 tree
4472 c_common_truthvalue_conversion (location_t location, tree expr)
4474 switch (TREE_CODE (expr))
4476 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4477 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4478 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4479 case ORDERED_EXPR: case UNORDERED_EXPR:
4480 if (TREE_TYPE (expr) == truthvalue_type_node)
4481 return expr;
4482 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4483 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4484 goto ret;
4486 case TRUTH_ANDIF_EXPR:
4487 case TRUTH_ORIF_EXPR:
4488 case TRUTH_AND_EXPR:
4489 case TRUTH_OR_EXPR:
4490 case TRUTH_XOR_EXPR:
4491 if (TREE_TYPE (expr) == truthvalue_type_node)
4492 return expr;
4493 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4494 c_common_truthvalue_conversion (location,
4495 TREE_OPERAND (expr, 0)),
4496 c_common_truthvalue_conversion (location,
4497 TREE_OPERAND (expr, 1)));
4498 goto ret;
4500 case TRUTH_NOT_EXPR:
4501 if (TREE_TYPE (expr) == truthvalue_type_node)
4502 return expr;
4503 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4504 c_common_truthvalue_conversion (location,
4505 TREE_OPERAND (expr, 0)));
4506 goto ret;
4508 case ERROR_MARK:
4509 return expr;
4511 case INTEGER_CST:
4512 return integer_zerop (expr) ? truthvalue_false_node
4513 : truthvalue_true_node;
4515 case REAL_CST:
4516 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4517 ? truthvalue_true_node
4518 : truthvalue_false_node;
4520 case FIXED_CST:
4521 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4522 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4523 ? truthvalue_true_node
4524 : truthvalue_false_node;
4526 case FUNCTION_DECL:
4527 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4528 /* Fall through. */
4530 case ADDR_EXPR:
4532 tree inner = TREE_OPERAND (expr, 0);
4533 if (decl_with_nonnull_addr_p (inner))
4535 /* Common Ada/Pascal programmer's mistake. */
4536 warning_at (location,
4537 OPT_Waddress,
4538 "the address of %qD will always evaluate as %<true%>",
4539 inner);
4540 return truthvalue_true_node;
4542 break;
4545 case COMPLEX_EXPR:
4546 expr = build_binary_op (EXPR_LOCATION (expr),
4547 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4548 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4549 c_common_truthvalue_conversion (location,
4550 TREE_OPERAND (expr, 0)),
4551 c_common_truthvalue_conversion (location,
4552 TREE_OPERAND (expr, 1)),
4554 goto ret;
4556 case NEGATE_EXPR:
4557 case ABS_EXPR:
4558 case FLOAT_EXPR:
4559 case EXCESS_PRECISION_EXPR:
4560 /* These don't change whether an object is nonzero or zero. */
4561 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4563 case LROTATE_EXPR:
4564 case RROTATE_EXPR:
4565 /* These don't change whether an object is zero or nonzero, but
4566 we can't ignore them if their second arg has side-effects. */
4567 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4569 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4570 TREE_OPERAND (expr, 1),
4571 c_common_truthvalue_conversion
4572 (location, TREE_OPERAND (expr, 0)));
4573 goto ret;
4575 else
4576 return c_common_truthvalue_conversion (location,
4577 TREE_OPERAND (expr, 0));
4579 case COND_EXPR:
4580 /* Distribute the conversion into the arms of a COND_EXPR. */
4581 if (c_dialect_cxx ())
4583 tree op1 = TREE_OPERAND (expr, 1);
4584 tree op2 = TREE_OPERAND (expr, 2);
4585 /* In C++ one of the arms might have void type if it is throw. */
4586 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4587 op1 = c_common_truthvalue_conversion (location, op1);
4588 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4589 op2 = c_common_truthvalue_conversion (location, op2);
4590 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4591 TREE_OPERAND (expr, 0), op1, op2);
4592 goto ret;
4594 else
4596 /* Folding will happen later for C. */
4597 expr = build3 (COND_EXPR, truthvalue_type_node,
4598 TREE_OPERAND (expr, 0),
4599 c_common_truthvalue_conversion (location,
4600 TREE_OPERAND (expr, 1)),
4601 c_common_truthvalue_conversion (location,
4602 TREE_OPERAND (expr, 2)));
4603 goto ret;
4606 CASE_CONVERT:
4608 tree totype = TREE_TYPE (expr);
4609 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4611 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4612 since that affects how `default_conversion' will behave. */
4613 if (TREE_CODE (totype) == REFERENCE_TYPE
4614 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4615 break;
4616 /* Don't strip a conversion from C++0x scoped enum, since they
4617 don't implicitly convert to other types. */
4618 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4619 && ENUM_IS_SCOPED (fromtype))
4620 break;
4621 /* If this isn't narrowing the argument, we can ignore it. */
4622 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4623 return c_common_truthvalue_conversion (location,
4624 TREE_OPERAND (expr, 0));
4626 break;
4628 case MODIFY_EXPR:
4629 if (!TREE_NO_WARNING (expr)
4630 && warn_parentheses)
4632 warning (OPT_Wparentheses,
4633 "suggest parentheses around assignment used as truth value");
4634 TREE_NO_WARNING (expr) = 1;
4636 break;
4638 default:
4639 break;
4642 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4644 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4645 expr = (build_binary_op
4646 (EXPR_LOCATION (expr),
4647 (TREE_SIDE_EFFECTS (expr)
4648 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4649 c_common_truthvalue_conversion
4650 (location,
4651 build_unary_op (location, REALPART_EXPR, t, 0)),
4652 c_common_truthvalue_conversion
4653 (location,
4654 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4655 0));
4656 goto ret;
4659 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4661 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4662 FCONST0 (TYPE_MODE
4663 (TREE_TYPE (expr))));
4664 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4666 else
4667 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4669 ret:
4670 protected_set_expr_location (expr, location);
4671 return expr;
4674 static void def_builtin_1 (enum built_in_function fncode,
4675 const char *name,
4676 enum built_in_class fnclass,
4677 tree fntype, tree libtype,
4678 bool both_p, bool fallback_p, bool nonansi_p,
4679 tree fnattrs, bool implicit_p);
4682 /* Apply the TYPE_QUALS to the new DECL. */
4684 void
4685 c_apply_type_quals_to_decl (int type_quals, tree decl)
4687 tree type = TREE_TYPE (decl);
4689 if (type == error_mark_node)
4690 return;
4692 if ((type_quals & TYPE_QUAL_CONST)
4693 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4694 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4695 constructor can produce constant init, so rely on cp_finish_decl to
4696 clear TREE_READONLY if the variable has non-constant init. */
4697 TREE_READONLY (decl) = 1;
4698 if (type_quals & TYPE_QUAL_VOLATILE)
4700 TREE_SIDE_EFFECTS (decl) = 1;
4701 TREE_THIS_VOLATILE (decl) = 1;
4703 if (type_quals & TYPE_QUAL_RESTRICT)
4705 while (type && TREE_CODE (type) == ARRAY_TYPE)
4706 /* Allow 'restrict' on arrays of pointers.
4707 FIXME currently we just ignore it. */
4708 type = TREE_TYPE (type);
4709 if (!type
4710 || !POINTER_TYPE_P (type)
4711 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4712 error ("invalid use of %<restrict%>");
4714 if (type_quals & TYPE_QUAL_SHARED)
4716 TREE_SHARED (decl) = 1;
4717 if (type_quals & TYPE_QUAL_STRICT)
4718 TREE_STRICT(decl) = 1;
4719 else if (type_quals & TYPE_QUAL_RELAXED)
4720 TREE_RELAXED(decl) = 1;
4721 /* The declaration's type should have been previously defined
4722 as a UPC shared type. */
4723 gcc_assert (upc_shared_type_p (type));
4727 /* Hash function for the problem of multiple type definitions in
4728 different files. This must hash all types that will compare
4729 equal via comptypes to the same value. In practice it hashes
4730 on some of the simple stuff and leaves the details to comptypes. */
4732 static hashval_t
4733 c_type_hash (const void *p)
4735 int n_elements;
4736 int shift, size;
4737 const_tree const t = (const_tree) p;
4738 tree t2;
4739 switch (TREE_CODE (t))
4741 /* For pointers, hash on pointee type plus some swizzling. */
4742 case POINTER_TYPE:
4743 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4744 /* Hash on number of elements and total size. */
4745 case ENUMERAL_TYPE:
4746 shift = 3;
4747 t2 = TYPE_VALUES (t);
4748 break;
4749 case RECORD_TYPE:
4750 shift = 0;
4751 t2 = TYPE_FIELDS (t);
4752 break;
4753 case QUAL_UNION_TYPE:
4754 shift = 1;
4755 t2 = TYPE_FIELDS (t);
4756 break;
4757 case UNION_TYPE:
4758 shift = 2;
4759 t2 = TYPE_FIELDS (t);
4760 break;
4761 default:
4762 gcc_unreachable ();
4764 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4765 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4766 n_elements = list_length (t2);
4767 /* We might have a VLA here. */
4768 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4769 size = 0;
4770 else
4771 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4772 return ((size << 24) | (n_elements << shift));
4775 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4777 /* Return the typed-based alias set for T, which may be an expression
4778 or a type. Return -1 if we don't do anything special. */
4780 alias_set_type
4781 c_common_get_alias_set (tree t)
4783 tree u;
4784 PTR *slot;
4786 /* For VLAs, use the alias set of the element type rather than the
4787 default of alias set 0 for types compared structurally. */
4788 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4790 if (TREE_CODE (t) == ARRAY_TYPE)
4791 return get_alias_set (TREE_TYPE (t));
4792 return -1;
4795 /* Permit type-punning when accessing a union, provided the access
4796 is directly through the union. For example, this code does not
4797 permit taking the address of a union member and then storing
4798 through it. Even the type-punning allowed here is a GCC
4799 extension, albeit a common and useful one; the C standard says
4800 that such accesses have implementation-defined behavior. */
4801 for (u = t;
4802 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4803 u = TREE_OPERAND (u, 0))
4804 if (TREE_CODE (u) == COMPONENT_REF
4805 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4806 return 0;
4808 /* That's all the expressions we handle specially. */
4809 if (!TYPE_P (t))
4810 return -1;
4812 /* The C standard guarantees that any object may be accessed via an
4813 lvalue that has character type. */
4814 if (t == char_type_node
4815 || t == signed_char_type_node
4816 || t == unsigned_char_type_node)
4817 return 0;
4819 /* The C standard specifically allows aliasing between signed and
4820 unsigned variants of the same type. We treat the signed
4821 variant as canonical. */
4822 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4824 tree t1 = c_common_signed_type (t);
4826 /* t1 == t can happen for boolean nodes which are always unsigned. */
4827 if (t1 != t)
4828 return get_alias_set (t1);
4831 /* Handle the case of multiple type nodes referring to "the same" type,
4832 which occurs with IMA. These share an alias set. FIXME: Currently only
4833 C90 is handled. (In C99 type compatibility is not transitive, which
4834 complicates things mightily. The alias set splay trees can theoretically
4835 represent this, but insertion is tricky when you consider all the
4836 different orders things might arrive in.) */
4838 if (c_language != clk_c || flag_isoc99)
4839 return -1;
4841 /* Save time if there's only one input file. */
4842 if (num_in_fnames == 1)
4843 return -1;
4845 /* Pointers need special handling if they point to any type that
4846 needs special handling (below). */
4847 if (TREE_CODE (t) == POINTER_TYPE)
4849 tree t2;
4850 /* Find bottom type under any nested POINTERs. */
4851 for (t2 = TREE_TYPE (t);
4852 TREE_CODE (t2) == POINTER_TYPE;
4853 t2 = TREE_TYPE (t2))
4855 if (TREE_CODE (t2) != RECORD_TYPE
4856 && TREE_CODE (t2) != ENUMERAL_TYPE
4857 && TREE_CODE (t2) != QUAL_UNION_TYPE
4858 && TREE_CODE (t2) != UNION_TYPE)
4859 return -1;
4860 if (TYPE_SIZE (t2) == 0)
4861 return -1;
4863 /* These are the only cases that need special handling. */
4864 if (TREE_CODE (t) != RECORD_TYPE
4865 && TREE_CODE (t) != ENUMERAL_TYPE
4866 && TREE_CODE (t) != QUAL_UNION_TYPE
4867 && TREE_CODE (t) != UNION_TYPE
4868 && TREE_CODE (t) != POINTER_TYPE)
4869 return -1;
4870 /* Undefined? */
4871 if (TYPE_SIZE (t) == 0)
4872 return -1;
4874 /* Look up t in hash table. Only one of the compatible types within each
4875 alias set is recorded in the table. */
4876 if (!type_hash_table)
4877 type_hash_table = htab_create_ggc (1021, c_type_hash,
4878 (htab_eq) lang_hooks.types_compatible_p,
4879 NULL);
4880 slot = htab_find_slot (type_hash_table, t, INSERT);
4881 if (*slot != NULL)
4883 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4884 return TYPE_ALIAS_SET ((tree)*slot);
4886 else
4887 /* Our caller will assign and record (in t) a new alias set; all we need
4888 to do is remember t in the hash table. */
4889 *slot = t;
4891 return -1;
4894 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4895 the second parameter indicates which OPERATOR is being applied.
4896 The COMPLAIN flag controls whether we should diagnose possibly
4897 ill-formed constructs or not. LOC is the location of the SIZEOF or
4898 TYPEOF operator. */
4900 tree
4901 c_sizeof_or_alignof_type (location_t loc,
4902 tree type, bool is_sizeof, int complain)
4904 const char *op_name;
4905 tree value = NULL;
4906 enum tree_code type_code = TREE_CODE (type);
4908 op_name = is_sizeof ? "sizeof" : "__alignof__";
4910 if (type_code == FUNCTION_TYPE)
4912 if (is_sizeof)
4914 if (complain && warn_pointer_arith)
4915 pedwarn (loc, OPT_Wpointer_arith,
4916 "invalid application of %<sizeof%> to a function type");
4917 else if (!complain)
4918 return error_mark_node;
4919 value = size_one_node;
4921 else
4923 if (complain)
4925 if (c_dialect_cxx ())
4926 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4927 "%<alignof%> applied to a function type");
4928 else
4929 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4930 "%<_Alignof%> applied to a function type");
4932 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4935 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4937 if (complain)
4939 if (type_code == VOID_TYPE && upc_shared_type_p (type))
4940 error_at (loc, "invalid application of %qs"
4941 " to %<shared void%> type", op_name);
4942 else if (type_code == VOID_TYPE && warn_pointer_arith)
4943 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4944 "invalid application of %qs to a void type", op_name);
4946 else
4947 return error_mark_node;
4948 value = size_one_node;
4950 else if (!COMPLETE_TYPE_P (type)
4951 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4953 if (complain)
4954 error_at (loc, "invalid application of %qs to incomplete type %qT",
4955 op_name, type);
4956 return error_mark_node;
4958 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4959 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4961 if (complain)
4962 error_at (loc, "invalid application of %qs to array type %qT of "
4963 "incomplete element type", op_name, type);
4964 return error_mark_node;
4966 else
4968 if (is_sizeof)
4969 /* Convert in case a char is more than one unit. */
4970 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4971 size_int (TYPE_PRECISION (char_type_node)
4972 / BITS_PER_UNIT));
4973 else
4974 value = size_int (TYPE_ALIGN_UNIT (type));
4977 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
4978 && upc_shared_type_p (type)
4979 && TYPE_HAS_THREADS_FACTOR (type))
4981 const tree n_threads = convert (sizetype, upc_num_threads ());
4982 value = size_binop (MULT_EXPR, value, n_threads);
4985 /* VALUE will have the middle-end integer type sizetype.
4986 However, we should really return a value of type `size_t',
4987 which is just a typedef for an ordinary integer type. */
4988 value = fold_convert_loc (loc, size_type_node, value);
4990 return value;
4993 /* Implement the __alignof keyword: Return the minimum required
4994 alignment of EXPR, measured in bytes. For VAR_DECLs,
4995 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4996 from an "aligned" __attribute__ specification). LOC is the
4997 location of the ALIGNOF operator. */
4999 tree
5000 c_alignof_expr (location_t loc, tree expr)
5002 tree t;
5004 if (VAR_OR_FUNCTION_DECL_P (expr))
5005 t = size_int (DECL_ALIGN_UNIT (expr));
5007 else if (TREE_CODE (expr) == COMPONENT_REF
5008 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5010 error_at (loc, "%<__alignof%> applied to a bit-field");
5011 t = size_one_node;
5013 else if (TREE_CODE (expr) == COMPONENT_REF
5014 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5015 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5017 else if (TREE_CODE (expr) == INDIRECT_REF)
5019 tree t = TREE_OPERAND (expr, 0);
5020 tree best = t;
5021 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5023 while (CONVERT_EXPR_P (t)
5024 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5026 int thisalign;
5028 t = TREE_OPERAND (t, 0);
5029 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5030 if (thisalign > bestalign)
5031 best = t, bestalign = thisalign;
5033 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5035 else
5036 return c_alignof (loc, TREE_TYPE (expr));
5038 return fold_convert_loc (loc, size_type_node, t);
5041 /* Handle C and C++ default attributes. */
5043 enum built_in_attribute
5045 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5046 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5047 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5048 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5049 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5050 #include "builtin-attrs.def"
5051 #undef DEF_ATTR_NULL_TREE
5052 #undef DEF_ATTR_INT
5053 #undef DEF_ATTR_STRING
5054 #undef DEF_ATTR_IDENT
5055 #undef DEF_ATTR_TREE_LIST
5056 ATTR_LAST
5059 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5061 static void c_init_attributes (void);
5063 enum c_builtin_type
5065 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5066 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5067 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5068 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5069 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5070 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5071 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5072 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5073 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5074 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5075 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5076 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5077 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5078 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5080 NAME,
5081 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5082 #include "builtin-types.def"
5083 #undef DEF_PRIMITIVE_TYPE
5084 #undef DEF_FUNCTION_TYPE_0
5085 #undef DEF_FUNCTION_TYPE_1
5086 #undef DEF_FUNCTION_TYPE_2
5087 #undef DEF_FUNCTION_TYPE_3
5088 #undef DEF_FUNCTION_TYPE_4
5089 #undef DEF_FUNCTION_TYPE_5
5090 #undef DEF_FUNCTION_TYPE_6
5091 #undef DEF_FUNCTION_TYPE_7
5092 #undef DEF_FUNCTION_TYPE_VAR_0
5093 #undef DEF_FUNCTION_TYPE_VAR_1
5094 #undef DEF_FUNCTION_TYPE_VAR_2
5095 #undef DEF_FUNCTION_TYPE_VAR_3
5096 #undef DEF_FUNCTION_TYPE_VAR_4
5097 #undef DEF_FUNCTION_TYPE_VAR_5
5098 #undef DEF_POINTER_TYPE
5099 BT_LAST
5102 typedef enum c_builtin_type builtin_type;
5104 /* A temporary array for c_common_nodes_and_builtins. Used in
5105 communication with def_fn_type. */
5106 static tree builtin_types[(int) BT_LAST + 1];
5108 /* A helper function for c_common_nodes_and_builtins. Build function type
5109 for DEF with return type RET and N arguments. If VAR is true, then the
5110 function should be variadic after those N arguments.
5112 Takes special care not to ICE if any of the types involved are
5113 error_mark_node, which indicates that said type is not in fact available
5114 (see builtin_type_for_size). In which case the function type as a whole
5115 should be error_mark_node. */
5117 static void
5118 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5120 tree t;
5121 tree *args = XALLOCAVEC (tree, n);
5122 va_list list;
5123 int i;
5125 va_start (list, n);
5126 for (i = 0; i < n; ++i)
5128 builtin_type a = (builtin_type) va_arg (list, int);
5129 t = builtin_types[a];
5130 if (t == error_mark_node)
5131 goto egress;
5132 args[i] = t;
5135 t = builtin_types[ret];
5136 if (t == error_mark_node)
5137 goto egress;
5138 if (var)
5139 t = build_varargs_function_type_array (t, n, args);
5140 else
5141 t = build_function_type_array (t, n, args);
5143 egress:
5144 builtin_types[def] = t;
5145 va_end (list);
5148 /* Build builtin functions common to both C and C++ language
5149 frontends. */
5151 static void
5152 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5154 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5155 builtin_types[ENUM] = VALUE;
5156 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5157 def_fn_type (ENUM, RETURN, 0, 0);
5158 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5159 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5160 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5161 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5162 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5163 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5164 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5165 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5166 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5167 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5168 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5169 ARG6) \
5170 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5171 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5172 ARG6, ARG7) \
5173 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5174 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5175 def_fn_type (ENUM, RETURN, 1, 0);
5176 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5177 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5178 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5179 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5180 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5181 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5182 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5183 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5184 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5185 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5186 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5187 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5189 #include "builtin-types.def"
5191 #undef DEF_PRIMITIVE_TYPE
5192 #undef DEF_FUNCTION_TYPE_1
5193 #undef DEF_FUNCTION_TYPE_2
5194 #undef DEF_FUNCTION_TYPE_3
5195 #undef DEF_FUNCTION_TYPE_4
5196 #undef DEF_FUNCTION_TYPE_5
5197 #undef DEF_FUNCTION_TYPE_6
5198 #undef DEF_FUNCTION_TYPE_VAR_0
5199 #undef DEF_FUNCTION_TYPE_VAR_1
5200 #undef DEF_FUNCTION_TYPE_VAR_2
5201 #undef DEF_FUNCTION_TYPE_VAR_3
5202 #undef DEF_FUNCTION_TYPE_VAR_4
5203 #undef DEF_FUNCTION_TYPE_VAR_5
5204 #undef DEF_POINTER_TYPE
5205 builtin_types[(int) BT_LAST] = NULL_TREE;
5207 c_init_attributes ();
5209 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5210 NONANSI_P, ATTRS, IMPLICIT, COND) \
5211 if (NAME && COND) \
5212 def_builtin_1 (ENUM, NAME, CLASS, \
5213 builtin_types[(int) TYPE], \
5214 builtin_types[(int) LIBTYPE], \
5215 BOTH_P, FALLBACK_P, NONANSI_P, \
5216 built_in_attributes[(int) ATTRS], IMPLICIT);
5217 #include "builtins.def"
5218 #undef DEF_BUILTIN
5220 targetm.init_builtins ();
5222 build_common_builtin_nodes ();
5224 if (flag_mudflap)
5225 mudflap_init ();
5228 /* Like get_identifier, but avoid warnings about null arguments when
5229 the argument may be NULL for targets where GCC lacks stdint.h type
5230 information. */
5232 static inline tree
5233 c_get_ident (const char *id)
5235 return get_identifier (id);
5238 /* Build tree nodes and builtin functions common to both C and C++ language
5239 frontends. */
5241 void
5242 c_common_nodes_and_builtins (void)
5244 int char16_type_size;
5245 int char32_type_size;
5246 int wchar_type_size;
5247 tree array_domain_type;
5248 tree va_list_ref_type_node;
5249 tree va_list_arg_type_node;
5251 build_common_tree_nodes (flag_signed_char, flag_short_double);
5253 /* Define `int' and `char' first so that dbx will output them first. */
5254 record_builtin_type (RID_INT, NULL, integer_type_node);
5255 record_builtin_type (RID_CHAR, "char", char_type_node);
5257 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5258 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5259 but not C. Are the conditionals here needed? */
5260 if (c_dialect_cxx ())
5261 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5262 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5263 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5264 record_builtin_type (RID_MAX, "long unsigned int",
5265 long_unsigned_type_node);
5266 if (int128_integer_type_node != NULL_TREE)
5268 record_builtin_type (RID_INT128, "__int128",
5269 int128_integer_type_node);
5270 record_builtin_type (RID_MAX, "__int128 unsigned",
5271 int128_unsigned_type_node);
5273 if (c_dialect_cxx ())
5274 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5275 record_builtin_type (RID_MAX, "long long int",
5276 long_long_integer_type_node);
5277 record_builtin_type (RID_MAX, "long long unsigned int",
5278 long_long_unsigned_type_node);
5279 if (c_dialect_cxx ())
5280 record_builtin_type (RID_MAX, "long long unsigned",
5281 long_long_unsigned_type_node);
5282 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5283 record_builtin_type (RID_MAX, "short unsigned int",
5284 short_unsigned_type_node);
5285 if (c_dialect_cxx ())
5286 record_builtin_type (RID_MAX, "unsigned short",
5287 short_unsigned_type_node);
5289 /* Define both `signed char' and `unsigned char'. */
5290 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5291 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5293 /* These are types that c_common_type_for_size and
5294 c_common_type_for_mode use. */
5295 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5296 TYPE_DECL, NULL_TREE,
5297 intQI_type_node));
5298 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5299 TYPE_DECL, NULL_TREE,
5300 intHI_type_node));
5301 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5302 TYPE_DECL, NULL_TREE,
5303 intSI_type_node));
5304 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5305 TYPE_DECL, NULL_TREE,
5306 intDI_type_node));
5307 #if HOST_BITS_PER_WIDE_INT >= 64
5308 if (targetm.scalar_mode_supported_p (TImode))
5309 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5310 TYPE_DECL,
5311 get_identifier ("__int128_t"),
5312 intTI_type_node));
5313 #endif
5314 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5315 TYPE_DECL, NULL_TREE,
5316 unsigned_intQI_type_node));
5317 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5318 TYPE_DECL, NULL_TREE,
5319 unsigned_intHI_type_node));
5320 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5321 TYPE_DECL, NULL_TREE,
5322 unsigned_intSI_type_node));
5323 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5324 TYPE_DECL, NULL_TREE,
5325 unsigned_intDI_type_node));
5326 #if HOST_BITS_PER_WIDE_INT >= 64
5327 if (targetm.scalar_mode_supported_p (TImode))
5328 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5329 TYPE_DECL,
5330 get_identifier ("__uint128_t"),
5331 unsigned_intTI_type_node));
5332 #endif
5334 /* Create the widest literal types. */
5335 widest_integer_literal_type_node
5336 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5337 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5338 TYPE_DECL, NULL_TREE,
5339 widest_integer_literal_type_node));
5341 widest_unsigned_literal_type_node
5342 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5343 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5344 TYPE_DECL, NULL_TREE,
5345 widest_unsigned_literal_type_node));
5347 signed_size_type_node = c_common_signed_type (size_type_node);
5349 pid_type_node =
5350 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5352 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5353 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5354 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5356 /* Only supported decimal floating point extension if the target
5357 actually supports underlying modes. */
5358 if (targetm.scalar_mode_supported_p (SDmode)
5359 && targetm.scalar_mode_supported_p (DDmode)
5360 && targetm.scalar_mode_supported_p (TDmode))
5362 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5363 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5364 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5367 if (targetm.fixed_point_supported_p ())
5369 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5370 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5371 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5372 record_builtin_type (RID_MAX, "long long _Fract",
5373 long_long_fract_type_node);
5374 record_builtin_type (RID_MAX, "unsigned short _Fract",
5375 unsigned_short_fract_type_node);
5376 record_builtin_type (RID_MAX, "unsigned _Fract",
5377 unsigned_fract_type_node);
5378 record_builtin_type (RID_MAX, "unsigned long _Fract",
5379 unsigned_long_fract_type_node);
5380 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5381 unsigned_long_long_fract_type_node);
5382 record_builtin_type (RID_MAX, "_Sat short _Fract",
5383 sat_short_fract_type_node);
5384 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5385 record_builtin_type (RID_MAX, "_Sat long _Fract",
5386 sat_long_fract_type_node);
5387 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5388 sat_long_long_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5390 sat_unsigned_short_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5392 sat_unsigned_fract_type_node);
5393 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5394 sat_unsigned_long_fract_type_node);
5395 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5396 sat_unsigned_long_long_fract_type_node);
5397 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5398 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5399 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5400 record_builtin_type (RID_MAX, "long long _Accum",
5401 long_long_accum_type_node);
5402 record_builtin_type (RID_MAX, "unsigned short _Accum",
5403 unsigned_short_accum_type_node);
5404 record_builtin_type (RID_MAX, "unsigned _Accum",
5405 unsigned_accum_type_node);
5406 record_builtin_type (RID_MAX, "unsigned long _Accum",
5407 unsigned_long_accum_type_node);
5408 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5409 unsigned_long_long_accum_type_node);
5410 record_builtin_type (RID_MAX, "_Sat short _Accum",
5411 sat_short_accum_type_node);
5412 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5413 record_builtin_type (RID_MAX, "_Sat long _Accum",
5414 sat_long_accum_type_node);
5415 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5416 sat_long_long_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5418 sat_unsigned_short_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5420 sat_unsigned_accum_type_node);
5421 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5422 sat_unsigned_long_accum_type_node);
5423 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5424 sat_unsigned_long_long_accum_type_node);
5428 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5429 TYPE_DECL,
5430 get_identifier ("complex int"),
5431 complex_integer_type_node));
5432 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5433 TYPE_DECL,
5434 get_identifier ("complex float"),
5435 complex_float_type_node));
5436 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5437 TYPE_DECL,
5438 get_identifier ("complex double"),
5439 complex_double_type_node));
5440 lang_hooks.decls.pushdecl
5441 (build_decl (UNKNOWN_LOCATION,
5442 TYPE_DECL, get_identifier ("complex long double"),
5443 complex_long_double_type_node));
5445 if (c_dialect_cxx ())
5446 /* For C++, make fileptr_type_node a distinct void * type until
5447 FILE type is defined. */
5448 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5450 record_builtin_type (RID_VOID, NULL, void_type_node);
5452 /* Set the TYPE_NAME for any variants that were built before
5453 record_builtin_type gave names to the built-in types. */
5455 tree void_name = TYPE_NAME (void_type_node);
5456 TYPE_NAME (void_type_node) = NULL_TREE;
5457 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5458 = void_name;
5459 TYPE_NAME (void_type_node) = void_name;
5462 /* This node must not be shared. */
5463 void_zero_node = make_node (INTEGER_CST);
5464 TREE_TYPE (void_zero_node) = void_type_node;
5466 void_list_node = build_void_list_node ();
5468 /* Make a type to be the domain of a few array types
5469 whose domains don't really matter.
5470 200 is small enough that it always fits in size_t
5471 and large enough that it can hold most function names for the
5472 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5473 array_domain_type = build_index_type (size_int (200));
5475 /* Make a type for arrays of characters.
5476 With luck nothing will ever really depend on the length of this
5477 array type. */
5478 char_array_type_node
5479 = build_array_type (char_type_node, array_domain_type);
5481 /* Likewise for arrays of ints. */
5482 int_array_type_node
5483 = build_array_type (integer_type_node, array_domain_type);
5485 string_type_node = build_pointer_type (char_type_node);
5486 const_string_type_node
5487 = build_pointer_type (build_qualified_type
5488 (char_type_node, TYPE_QUAL_CONST));
5490 /* This is special for C++ so functions can be overloaded. */
5491 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5492 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5493 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5494 underlying_wchar_type_node = wchar_type_node;
5495 if (c_dialect_cxx ())
5497 if (TYPE_UNSIGNED (wchar_type_node))
5498 wchar_type_node = make_unsigned_type (wchar_type_size);
5499 else
5500 wchar_type_node = make_signed_type (wchar_type_size);
5501 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5504 /* This is for wide string constants. */
5505 wchar_array_type_node
5506 = build_array_type (wchar_type_node, array_domain_type);
5508 /* Define 'char16_t'. */
5509 char16_type_node = get_identifier (CHAR16_TYPE);
5510 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5511 char16_type_size = TYPE_PRECISION (char16_type_node);
5512 if (c_dialect_cxx ())
5514 char16_type_node = make_unsigned_type (char16_type_size);
5516 if (cxx_dialect >= cxx0x)
5517 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5520 /* This is for UTF-16 string constants. */
5521 char16_array_type_node
5522 = build_array_type (char16_type_node, array_domain_type);
5524 /* Define 'char32_t'. */
5525 char32_type_node = get_identifier (CHAR32_TYPE);
5526 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5527 char32_type_size = TYPE_PRECISION (char32_type_node);
5528 if (c_dialect_cxx ())
5530 char32_type_node = make_unsigned_type (char32_type_size);
5532 if (cxx_dialect >= cxx0x)
5533 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5536 /* This is for UTF-32 string constants. */
5537 char32_array_type_node
5538 = build_array_type (char32_type_node, array_domain_type);
5540 wint_type_node =
5541 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5543 intmax_type_node =
5544 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5545 uintmax_type_node =
5546 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5548 if (SIG_ATOMIC_TYPE)
5549 sig_atomic_type_node =
5550 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5551 if (INT8_TYPE)
5552 int8_type_node =
5553 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5554 if (INT16_TYPE)
5555 int16_type_node =
5556 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5557 if (INT32_TYPE)
5558 int32_type_node =
5559 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5560 if (INT64_TYPE)
5561 int64_type_node =
5562 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5563 if (UINT8_TYPE)
5564 uint8_type_node =
5565 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5566 if (UINT16_TYPE)
5567 c_uint16_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5569 if (UINT32_TYPE)
5570 c_uint32_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5572 if (UINT64_TYPE)
5573 c_uint64_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5575 if (INT_LEAST8_TYPE)
5576 int_least8_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5578 if (INT_LEAST16_TYPE)
5579 int_least16_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5581 if (INT_LEAST32_TYPE)
5582 int_least32_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5584 if (INT_LEAST64_TYPE)
5585 int_least64_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5587 if (UINT_LEAST8_TYPE)
5588 uint_least8_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5590 if (UINT_LEAST16_TYPE)
5591 uint_least16_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5593 if (UINT_LEAST32_TYPE)
5594 uint_least32_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5596 if (UINT_LEAST64_TYPE)
5597 uint_least64_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5599 if (INT_FAST8_TYPE)
5600 int_fast8_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5602 if (INT_FAST16_TYPE)
5603 int_fast16_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5605 if (INT_FAST32_TYPE)
5606 int_fast32_type_node =
5607 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5608 if (INT_FAST64_TYPE)
5609 int_fast64_type_node =
5610 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5611 if (UINT_FAST8_TYPE)
5612 uint_fast8_type_node =
5613 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5614 if (UINT_FAST16_TYPE)
5615 uint_fast16_type_node =
5616 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5617 if (UINT_FAST32_TYPE)
5618 uint_fast32_type_node =
5619 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5620 if (UINT_FAST64_TYPE)
5621 uint_fast64_type_node =
5622 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5623 if (INTPTR_TYPE)
5624 intptr_type_node =
5625 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5626 if (UINTPTR_TYPE)
5627 uintptr_type_node =
5628 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5630 default_function_type
5631 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5632 ptrdiff_type_node
5633 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5634 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5636 lang_hooks.decls.pushdecl
5637 (build_decl (UNKNOWN_LOCATION,
5638 TYPE_DECL, get_identifier ("__builtin_va_list"),
5639 va_list_type_node));
5640 if (targetm.enum_va_list_p)
5642 int l;
5643 const char *pname;
5644 tree ptype;
5646 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5648 lang_hooks.decls.pushdecl
5649 (build_decl (UNKNOWN_LOCATION,
5650 TYPE_DECL, get_identifier (pname),
5651 ptype));
5656 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5658 va_list_arg_type_node = va_list_ref_type_node =
5659 build_pointer_type (TREE_TYPE (va_list_type_node));
5661 else
5663 va_list_arg_type_node = va_list_type_node;
5664 va_list_ref_type_node = build_reference_type (va_list_type_node);
5667 if (!flag_preprocess_only)
5668 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5670 main_identifier_node = get_identifier ("main");
5672 /* Create the built-in __null node. It is important that this is
5673 not shared. */
5674 null_node = make_node (INTEGER_CST);
5675 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5677 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5678 memset (builtin_types, 0, sizeof (builtin_types));
5681 /* The number of named compound-literals generated thus far. */
5682 static GTY(()) int compound_literal_number;
5684 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5686 void
5687 set_compound_literal_name (tree decl)
5689 char *name;
5690 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5691 compound_literal_number);
5692 compound_literal_number++;
5693 DECL_NAME (decl) = get_identifier (name);
5696 tree
5697 build_va_arg (location_t loc, tree expr, tree type)
5699 expr = build1 (VA_ARG_EXPR, type, expr);
5700 SET_EXPR_LOCATION (expr, loc);
5701 return expr;
5705 /* Linked list of disabled built-in functions. */
5707 typedef struct disabled_builtin
5709 const char *name;
5710 struct disabled_builtin *next;
5711 } disabled_builtin;
5712 static disabled_builtin *disabled_builtins = NULL;
5714 static bool builtin_function_disabled_p (const char *);
5716 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5717 begins with "__builtin_", give an error. */
5719 void
5720 disable_builtin_function (const char *name)
5722 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5723 error ("cannot disable built-in function %qs", name);
5724 else
5726 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5727 new_disabled_builtin->name = name;
5728 new_disabled_builtin->next = disabled_builtins;
5729 disabled_builtins = new_disabled_builtin;
5734 /* Return true if the built-in function NAME has been disabled, false
5735 otherwise. */
5737 static bool
5738 builtin_function_disabled_p (const char *name)
5740 disabled_builtin *p;
5741 for (p = disabled_builtins; p != NULL; p = p->next)
5743 if (strcmp (name, p->name) == 0)
5744 return true;
5746 return false;
5750 /* Worker for DEF_BUILTIN.
5751 Possibly define a builtin function with one or two names.
5752 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5753 nonansi_p and flag_no_nonansi_builtin. */
5755 static void
5756 def_builtin_1 (enum built_in_function fncode,
5757 const char *name,
5758 enum built_in_class fnclass,
5759 tree fntype, tree libtype,
5760 bool both_p, bool fallback_p, bool nonansi_p,
5761 tree fnattrs, bool implicit_p)
5763 tree decl;
5764 const char *libname;
5766 if (fntype == error_mark_node)
5767 return;
5769 gcc_assert ((!both_p && !fallback_p)
5770 || !strncmp (name, "__builtin_",
5771 strlen ("__builtin_")));
5773 libname = name + strlen ("__builtin_");
5774 decl = add_builtin_function (name, fntype, fncode, fnclass,
5775 (fallback_p ? libname : NULL),
5776 fnattrs);
5778 set_builtin_decl (fncode, decl, implicit_p);
5780 if (both_p
5781 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5782 && !(nonansi_p && flag_no_nonansi_builtin))
5783 add_builtin_function (libname, libtype, fncode, fnclass,
5784 NULL, fnattrs);
5787 /* Nonzero if the type T promotes to int. This is (nearly) the
5788 integral promotions defined in ISO C99 6.3.1.1/2. */
5790 bool
5791 c_promoting_integer_type_p (const_tree t)
5793 switch (TREE_CODE (t))
5795 case INTEGER_TYPE:
5796 return (TYPE_MAIN_VARIANT (t) == char_type_node
5797 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5798 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5799 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5800 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5801 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5803 case ENUMERAL_TYPE:
5804 /* ??? Technically all enumerations not larger than an int
5805 promote to an int. But this is used along code paths
5806 that only want to notice a size change. */
5807 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5809 case BOOLEAN_TYPE:
5810 return 1;
5812 default:
5813 return 0;
5817 /* Return 1 if PARMS specifies a fixed number of parameters
5818 and none of their types is affected by default promotions. */
5821 self_promoting_args_p (const_tree parms)
5823 const_tree t;
5824 for (t = parms; t; t = TREE_CHAIN (t))
5826 tree type = TREE_VALUE (t);
5828 if (type == error_mark_node)
5829 continue;
5831 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5832 return 0;
5834 if (type == 0)
5835 return 0;
5837 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5838 return 0;
5840 if (c_promoting_integer_type_p (type))
5841 return 0;
5843 return 1;
5846 /* Recursively remove any '*' or '&' operator from TYPE. */
5847 tree
5848 strip_pointer_operator (tree t)
5850 while (POINTER_TYPE_P (t))
5851 t = TREE_TYPE (t);
5852 return t;
5855 /* Recursively remove pointer or array type from TYPE. */
5856 tree
5857 strip_pointer_or_array_types (tree t)
5859 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5860 t = TREE_TYPE (t);
5861 return t;
5864 /* Used to compare case labels. K1 and K2 are actually tree nodes
5865 representing case labels, or NULL_TREE for a `default' label.
5866 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5867 K2, and 0 if K1 and K2 are equal. */
5870 case_compare (splay_tree_key k1, splay_tree_key k2)
5872 /* Consider a NULL key (such as arises with a `default' label) to be
5873 smaller than anything else. */
5874 if (!k1)
5875 return k2 ? -1 : 0;
5876 else if (!k2)
5877 return k1 ? 1 : 0;
5879 return tree_int_cst_compare ((tree) k1, (tree) k2);
5882 /* Process a case label, located at LOC, for the range LOW_VALUE
5883 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5884 then this case label is actually a `default' label. If only
5885 HIGH_VALUE is NULL_TREE, then case label was declared using the
5886 usual C/C++ syntax, rather than the GNU case range extension.
5887 CASES is a tree containing all the case ranges processed so far;
5888 COND is the condition for the switch-statement itself. Returns the
5889 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5890 is created. */
5892 tree
5893 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5894 tree low_value, tree high_value)
5896 tree type;
5897 tree label;
5898 tree case_label;
5899 splay_tree_node node;
5901 /* Create the LABEL_DECL itself. */
5902 label = create_artificial_label (loc);
5904 /* If there was an error processing the switch condition, bail now
5905 before we get more confused. */
5906 if (!cond || cond == error_mark_node)
5907 goto error_out;
5909 if ((low_value && TREE_TYPE (low_value)
5910 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5911 || (high_value && TREE_TYPE (high_value)
5912 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5914 error_at (loc, "pointers are not permitted as case values");
5915 goto error_out;
5918 /* Case ranges are a GNU extension. */
5919 if (high_value)
5920 pedwarn (loc, OPT_Wpedantic,
5921 "range expressions in switch statements are non-standard");
5923 type = TREE_TYPE (cond);
5924 if (low_value)
5926 low_value = check_case_value (low_value);
5927 low_value = convert_and_check (type, low_value);
5928 if (low_value == error_mark_node)
5929 goto error_out;
5931 if (high_value)
5933 high_value = check_case_value (high_value);
5934 high_value = convert_and_check (type, high_value);
5935 if (high_value == error_mark_node)
5936 goto error_out;
5939 if (low_value && high_value)
5941 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5942 really a case range, even though it was written that way.
5943 Remove the HIGH_VALUE to simplify later processing. */
5944 if (tree_int_cst_equal (low_value, high_value))
5945 high_value = NULL_TREE;
5946 else if (!tree_int_cst_lt (low_value, high_value))
5947 warning_at (loc, 0, "empty range specified");
5950 /* See if the case is in range of the type of the original testing
5951 expression. If both low_value and high_value are out of range,
5952 don't insert the case label and return NULL_TREE. */
5953 if (low_value
5954 && !check_case_bounds (type, orig_type,
5955 &low_value, high_value ? &high_value : NULL))
5956 return NULL_TREE;
5958 /* Look up the LOW_VALUE in the table of case labels we already
5959 have. */
5960 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5961 /* If there was not an exact match, check for overlapping ranges.
5962 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5963 that's a `default' label and the only overlap is an exact match. */
5964 if (!node && (low_value || high_value))
5966 splay_tree_node low_bound;
5967 splay_tree_node high_bound;
5969 /* Even though there wasn't an exact match, there might be an
5970 overlap between this case range and another case range.
5971 Since we've (inductively) not allowed any overlapping case
5972 ranges, we simply need to find the greatest low case label
5973 that is smaller that LOW_VALUE, and the smallest low case
5974 label that is greater than LOW_VALUE. If there is an overlap
5975 it will occur in one of these two ranges. */
5976 low_bound = splay_tree_predecessor (cases,
5977 (splay_tree_key) low_value);
5978 high_bound = splay_tree_successor (cases,
5979 (splay_tree_key) low_value);
5981 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5982 the LOW_VALUE, so there is no need to check unless the
5983 LOW_BOUND is in fact itself a case range. */
5984 if (low_bound
5985 && CASE_HIGH ((tree) low_bound->value)
5986 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5987 low_value) >= 0)
5988 node = low_bound;
5989 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5990 range is bigger than the low end of the current range, so we
5991 are only interested if the current range is a real range, and
5992 not an ordinary case label. */
5993 else if (high_bound
5994 && high_value
5995 && (tree_int_cst_compare ((tree) high_bound->key,
5996 high_value)
5997 <= 0))
5998 node = high_bound;
6000 /* If there was an overlap, issue an error. */
6001 if (node)
6003 tree duplicate = CASE_LABEL ((tree) node->value);
6005 if (high_value)
6007 error_at (loc, "duplicate (or overlapping) case value");
6008 error_at (DECL_SOURCE_LOCATION (duplicate),
6009 "this is the first entry overlapping that value");
6011 else if (low_value)
6013 error_at (loc, "duplicate case value") ;
6014 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6016 else
6018 error_at (loc, "multiple default labels in one switch");
6019 error_at (DECL_SOURCE_LOCATION (duplicate),
6020 "this is the first default label");
6022 goto error_out;
6025 /* Add a CASE_LABEL to the statement-tree. */
6026 case_label = add_stmt (build_case_label (low_value, high_value, label));
6027 /* Register this case label in the splay tree. */
6028 splay_tree_insert (cases,
6029 (splay_tree_key) low_value,
6030 (splay_tree_value) case_label);
6032 return case_label;
6034 error_out:
6035 /* Add a label so that the back-end doesn't think that the beginning of
6036 the switch is unreachable. Note that we do not add a case label, as
6037 that just leads to duplicates and thence to failure later on. */
6038 if (!cases->root)
6040 tree t = create_artificial_label (loc);
6041 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6043 return error_mark_node;
6046 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6047 Used to verify that case values match up with enumerator values. */
6049 static void
6050 match_case_to_enum_1 (tree key, tree type, tree label)
6052 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6054 /* ??? Not working too hard to print the double-word value.
6055 Should perhaps be done with %lwd in the diagnostic routines? */
6056 if (TREE_INT_CST_HIGH (key) == 0)
6057 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6058 TREE_INT_CST_LOW (key));
6059 else if (!TYPE_UNSIGNED (type)
6060 && TREE_INT_CST_HIGH (key) == -1
6061 && TREE_INT_CST_LOW (key) != 0)
6062 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6063 -TREE_INT_CST_LOW (key));
6064 else
6065 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6066 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6067 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6069 if (TYPE_NAME (type) == 0)
6070 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6071 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6072 "case value %qs not in enumerated type",
6073 buf);
6074 else
6075 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6076 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6077 "case value %qs not in enumerated type %qT",
6078 buf, type);
6081 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6082 Used to verify that case values match up with enumerator values. */
6084 static int
6085 match_case_to_enum (splay_tree_node node, void *data)
6087 tree label = (tree) node->value;
6088 tree type = (tree) data;
6090 /* Skip default case. */
6091 if (!CASE_LOW (label))
6092 return 0;
6094 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6095 when we did our enum->case scan. Reset our scratch bit after. */
6096 if (!CASE_LOW_SEEN (label))
6097 match_case_to_enum_1 (CASE_LOW (label), type, label);
6098 else
6099 CASE_LOW_SEEN (label) = 0;
6101 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6102 not set, that means that CASE_HIGH did not appear when we did our
6103 enum->case scan. Reset our scratch bit after. */
6104 if (CASE_HIGH (label))
6106 if (!CASE_HIGH_SEEN (label))
6107 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6108 else
6109 CASE_HIGH_SEEN (label) = 0;
6112 return 0;
6115 /* Handle -Wswitch*. Called from the front end after parsing the
6116 switch construct. */
6117 /* ??? Should probably be somewhere generic, since other languages
6118 besides C and C++ would want this. At the moment, however, C/C++
6119 are the only tree-ssa languages that support enumerations at all,
6120 so the point is moot. */
6122 void
6123 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6124 tree type, tree cond)
6126 splay_tree_node default_node;
6127 splay_tree_node node;
6128 tree chain;
6130 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6131 return;
6133 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6134 if (!default_node)
6135 warning_at (switch_location, OPT_Wswitch_default,
6136 "switch missing default case");
6138 /* From here on, we only care about about enumerated types. */
6139 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6140 return;
6142 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6143 if (!warn_switch_enum && !warn_switch)
6144 return;
6146 /* Check the cases. Warn about case values which are not members of
6147 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6148 there is no default case, check that exactly all enumeration
6149 literals are covered by the cases. */
6151 /* Clearing COND if it is not an integer constant simplifies
6152 the tests inside the loop below. */
6153 if (TREE_CODE (cond) != INTEGER_CST)
6154 cond = NULL_TREE;
6156 /* The time complexity here is O(N*lg(N)) worst case, but for the
6157 common case of monotonically increasing enumerators, it is
6158 O(N), since the nature of the splay tree will keep the next
6159 element adjacent to the root at all times. */
6161 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6163 tree value = TREE_VALUE (chain);
6164 if (TREE_CODE (value) == CONST_DECL)
6165 value = DECL_INITIAL (value);
6166 node = splay_tree_lookup (cases, (splay_tree_key) value);
6167 if (node)
6169 /* Mark the CASE_LOW part of the case entry as seen. */
6170 tree label = (tree) node->value;
6171 CASE_LOW_SEEN (label) = 1;
6172 continue;
6175 /* Even though there wasn't an exact match, there might be a
6176 case range which includes the enumerator's value. */
6177 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6178 if (node && CASE_HIGH ((tree) node->value))
6180 tree label = (tree) node->value;
6181 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6182 if (cmp >= 0)
6184 /* If we match the upper bound exactly, mark the CASE_HIGH
6185 part of the case entry as seen. */
6186 if (cmp == 0)
6187 CASE_HIGH_SEEN (label) = 1;
6188 continue;
6192 /* We've now determined that this enumerated literal isn't
6193 handled by the case labels of the switch statement. */
6195 /* If the switch expression is a constant, we only really care
6196 about whether that constant is handled by the switch. */
6197 if (cond && tree_int_cst_compare (cond, value))
6198 continue;
6200 /* If there is a default_node, the only relevant option is
6201 Wswitch-enum. Otherwise, if both are enabled then we prefer
6202 to warn using -Wswitch because -Wswitch is enabled by -Wall
6203 while -Wswitch-enum is explicit. */
6204 warning_at (switch_location,
6205 (default_node || !warn_switch
6206 ? OPT_Wswitch_enum
6207 : OPT_Wswitch),
6208 "enumeration value %qE not handled in switch",
6209 TREE_PURPOSE (chain));
6212 /* Warn if there are case expressions that don't correspond to
6213 enumerators. This can occur since C and C++ don't enforce
6214 type-checking of assignments to enumeration variables.
6216 The time complexity here is now always O(N) worst case, since
6217 we should have marked both the lower bound and upper bound of
6218 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6219 above. This scan also resets those fields. */
6221 splay_tree_foreach (cases, match_case_to_enum, type);
6224 /* Finish an expression taking the address of LABEL (an
6225 IDENTIFIER_NODE). Returns an expression for the address.
6227 LOC is the location for the expression returned. */
6229 tree
6230 finish_label_address_expr (tree label, location_t loc)
6232 tree result;
6234 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6236 if (label == error_mark_node)
6237 return error_mark_node;
6239 label = lookup_label (label);
6240 if (label == NULL_TREE)
6241 result = null_pointer_node;
6242 else
6244 TREE_USED (label) = 1;
6245 result = build1 (ADDR_EXPR, ptr_type_node, label);
6246 /* The current function is not necessarily uninlinable.
6247 Computed gotos are incompatible with inlining, but the value
6248 here could be used only in a diagnostic, for example. */
6249 protected_set_expr_location (result, loc);
6252 return result;
6256 /* Given a boolean expression ARG, return a tree representing an increment
6257 or decrement (as indicated by CODE) of ARG. The front end must check for
6258 invalid cases (e.g., decrement in C++). */
6259 tree
6260 boolean_increment (enum tree_code code, tree arg)
6262 tree val;
6263 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6265 arg = stabilize_reference (arg);
6266 switch (code)
6268 case PREINCREMENT_EXPR:
6269 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6270 break;
6271 case POSTINCREMENT_EXPR:
6272 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6273 arg = save_expr (arg);
6274 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6275 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6276 break;
6277 case PREDECREMENT_EXPR:
6278 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6279 invert_truthvalue_loc (input_location, arg));
6280 break;
6281 case POSTDECREMENT_EXPR:
6282 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6283 invert_truthvalue_loc (input_location, arg));
6284 arg = save_expr (arg);
6285 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6286 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6287 break;
6288 default:
6289 gcc_unreachable ();
6291 TREE_SIDE_EFFECTS (val) = 1;
6292 return val;
6295 /* Built-in macros for stddef.h and stdint.h, that require macros
6296 defined in this file. */
6297 void
6298 c_stddef_cpp_builtins(void)
6300 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6301 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6302 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6303 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6304 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6305 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6306 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6307 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6308 if (SIG_ATOMIC_TYPE)
6309 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6310 if (INT8_TYPE)
6311 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6312 if (INT16_TYPE)
6313 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6314 if (INT32_TYPE)
6315 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6316 if (INT64_TYPE)
6317 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6318 if (UINT8_TYPE)
6319 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6320 if (UINT16_TYPE)
6321 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6322 if (UINT32_TYPE)
6323 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6324 if (UINT64_TYPE)
6325 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6326 if (INT_LEAST8_TYPE)
6327 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6328 if (INT_LEAST16_TYPE)
6329 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6330 if (INT_LEAST32_TYPE)
6331 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6332 if (INT_LEAST64_TYPE)
6333 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6334 if (UINT_LEAST8_TYPE)
6335 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6336 if (UINT_LEAST16_TYPE)
6337 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6338 if (UINT_LEAST32_TYPE)
6339 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6340 if (UINT_LEAST64_TYPE)
6341 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6342 if (INT_FAST8_TYPE)
6343 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6344 if (INT_FAST16_TYPE)
6345 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6346 if (INT_FAST32_TYPE)
6347 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6348 if (INT_FAST64_TYPE)
6349 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6350 if (UINT_FAST8_TYPE)
6351 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6352 if (UINT_FAST16_TYPE)
6353 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6354 if (UINT_FAST32_TYPE)
6355 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6356 if (UINT_FAST64_TYPE)
6357 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6358 if (INTPTR_TYPE)
6359 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6360 if (UINTPTR_TYPE)
6361 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6364 static void
6365 c_init_attributes (void)
6367 /* Fill in the built_in_attributes array. */
6368 #define DEF_ATTR_NULL_TREE(ENUM) \
6369 built_in_attributes[(int) ENUM] = NULL_TREE;
6370 #define DEF_ATTR_INT(ENUM, VALUE) \
6371 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6372 #define DEF_ATTR_STRING(ENUM, VALUE) \
6373 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6374 #define DEF_ATTR_IDENT(ENUM, STRING) \
6375 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6376 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6377 built_in_attributes[(int) ENUM] \
6378 = tree_cons (built_in_attributes[(int) PURPOSE], \
6379 built_in_attributes[(int) VALUE], \
6380 built_in_attributes[(int) CHAIN]);
6381 #include "builtin-attrs.def"
6382 #undef DEF_ATTR_NULL_TREE
6383 #undef DEF_ATTR_INT
6384 #undef DEF_ATTR_IDENT
6385 #undef DEF_ATTR_TREE_LIST
6388 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6389 identifier as an argument, so the front end shouldn't look it up. */
6391 bool
6392 attribute_takes_identifier_p (const_tree attr_id)
6394 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6395 if (spec == NULL)
6396 /* Unknown attribute that we'll end up ignoring, return true so we
6397 don't complain about an identifier argument. */
6398 return true;
6399 else if (!strcmp ("mode", spec->name)
6400 || !strcmp ("format", spec->name)
6401 || !strcmp ("cleanup", spec->name))
6402 return true;
6403 else
6404 return targetm.attribute_takes_identifier_p (attr_id);
6407 /* Attribute handlers common to C front ends. */
6409 /* Handle a "packed" attribute; arguments as in
6410 struct attribute_spec.handler. */
6412 static tree
6413 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6414 int flags, bool *no_add_attrs)
6416 if (TYPE_P (*node))
6418 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6419 *node = build_variant_type_copy (*node);
6420 TYPE_PACKED (*node) = 1;
6422 else if (TREE_CODE (*node) == FIELD_DECL)
6424 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6425 /* Still pack bitfields. */
6426 && ! DECL_INITIAL (*node))
6427 warning (OPT_Wattributes,
6428 "%qE attribute ignored for field of type %qT",
6429 name, TREE_TYPE (*node));
6430 else
6431 DECL_PACKED (*node) = 1;
6433 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6434 used for DECL_REGISTER. It wouldn't mean anything anyway.
6435 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6436 that changes what the typedef is typing. */
6437 else
6439 warning (OPT_Wattributes, "%qE attribute ignored", name);
6440 *no_add_attrs = true;
6443 return NULL_TREE;
6446 /* Handle a "nocommon" attribute; arguments as in
6447 struct attribute_spec.handler. */
6449 static tree
6450 handle_nocommon_attribute (tree *node, tree name,
6451 tree ARG_UNUSED (args),
6452 int ARG_UNUSED (flags), bool *no_add_attrs)
6454 if (TREE_CODE (*node) == VAR_DECL)
6455 DECL_COMMON (*node) = 0;
6456 else
6458 warning (OPT_Wattributes, "%qE attribute ignored", name);
6459 *no_add_attrs = true;
6462 return NULL_TREE;
6465 /* Handle a "common" attribute; arguments as in
6466 struct attribute_spec.handler. */
6468 static tree
6469 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6470 int ARG_UNUSED (flags), bool *no_add_attrs)
6472 if (TREE_CODE (*node) == VAR_DECL)
6473 DECL_COMMON (*node) = 1;
6474 else
6476 warning (OPT_Wattributes, "%qE attribute ignored", name);
6477 *no_add_attrs = true;
6480 return NULL_TREE;
6483 /* Handle a "noreturn" attribute; arguments as in
6484 struct attribute_spec.handler. */
6486 static tree
6487 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6488 int ARG_UNUSED (flags), bool *no_add_attrs)
6490 tree type = TREE_TYPE (*node);
6492 /* See FIXME comment in c_common_attribute_table. */
6493 if (TREE_CODE (*node) == FUNCTION_DECL
6494 || objc_method_decl (TREE_CODE (*node)))
6495 TREE_THIS_VOLATILE (*node) = 1;
6496 else if (TREE_CODE (type) == POINTER_TYPE
6497 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6498 TREE_TYPE (*node)
6499 = build_pointer_type
6500 (build_type_variant (TREE_TYPE (type),
6501 TYPE_READONLY (TREE_TYPE (type)), 1));
6502 else
6504 warning (OPT_Wattributes, "%qE attribute ignored", name);
6505 *no_add_attrs = true;
6508 return NULL_TREE;
6511 /* Handle a "hot" and attribute; arguments as in
6512 struct attribute_spec.handler. */
6514 static tree
6515 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6516 int ARG_UNUSED (flags), bool *no_add_attrs)
6518 if (TREE_CODE (*node) == FUNCTION_DECL
6519 || TREE_CODE (*node) == LABEL_DECL)
6521 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6523 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6524 name, "cold");
6525 *no_add_attrs = true;
6527 /* Most of the rest of the hot processing is done later with
6528 lookup_attribute. */
6530 else
6532 warning (OPT_Wattributes, "%qE attribute ignored", name);
6533 *no_add_attrs = true;
6536 return NULL_TREE;
6539 /* Handle a "cold" and attribute; arguments as in
6540 struct attribute_spec.handler. */
6542 static tree
6543 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6544 int ARG_UNUSED (flags), bool *no_add_attrs)
6546 if (TREE_CODE (*node) == FUNCTION_DECL
6547 || TREE_CODE (*node) == LABEL_DECL)
6549 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6551 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6552 name, "hot");
6553 *no_add_attrs = true;
6555 /* Most of the rest of the cold processing is done later with
6556 lookup_attribute. */
6558 else
6560 warning (OPT_Wattributes, "%qE attribute ignored", name);
6561 *no_add_attrs = true;
6564 return NULL_TREE;
6567 /* Handle a "no_sanitize_address" attribute; arguments as in
6568 struct attribute_spec.handler. */
6570 static tree
6571 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6572 bool *no_add_attrs)
6574 if (TREE_CODE (*node) != FUNCTION_DECL)
6576 warning (OPT_Wattributes, "%qE attribute ignored", name);
6577 *no_add_attrs = true;
6580 return NULL_TREE;
6583 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6584 struct attribute_spec.handler. */
6586 static tree
6587 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6588 bool *no_add_attrs)
6590 if (TREE_CODE (*node) != FUNCTION_DECL)
6591 warning (OPT_Wattributes, "%qE attribute ignored", name);
6592 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6593 DECL_ATTRIBUTES (*node)
6594 = tree_cons (get_identifier ("no_sanitize_address"),
6595 NULL_TREE, DECL_ATTRIBUTES (*node));
6596 *no_add_attrs = true;
6597 return NULL_TREE;
6600 /* Handle a "noinline" attribute; arguments as in
6601 struct attribute_spec.handler. */
6603 static tree
6604 handle_noinline_attribute (tree *node, tree name,
6605 tree ARG_UNUSED (args),
6606 int ARG_UNUSED (flags), bool *no_add_attrs)
6608 if (TREE_CODE (*node) == FUNCTION_DECL)
6609 DECL_UNINLINABLE (*node) = 1;
6610 else
6612 warning (OPT_Wattributes, "%qE attribute ignored", name);
6613 *no_add_attrs = true;
6616 return NULL_TREE;
6619 /* Handle a "noclone" attribute; arguments as in
6620 struct attribute_spec.handler. */
6622 static tree
6623 handle_noclone_attribute (tree *node, tree name,
6624 tree ARG_UNUSED (args),
6625 int ARG_UNUSED (flags), bool *no_add_attrs)
6627 if (TREE_CODE (*node) != FUNCTION_DECL)
6629 warning (OPT_Wattributes, "%qE attribute ignored", name);
6630 *no_add_attrs = true;
6633 return NULL_TREE;
6636 /* Handle a "always_inline" attribute; arguments as in
6637 struct attribute_spec.handler. */
6639 static tree
6640 handle_always_inline_attribute (tree *node, tree name,
6641 tree ARG_UNUSED (args),
6642 int ARG_UNUSED (flags),
6643 bool *no_add_attrs)
6645 if (TREE_CODE (*node) == FUNCTION_DECL)
6647 /* Set the attribute and mark it for disregarding inline
6648 limits. */
6649 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6651 else
6653 warning (OPT_Wattributes, "%qE attribute ignored", name);
6654 *no_add_attrs = true;
6657 return NULL_TREE;
6660 /* Handle a "gnu_inline" attribute; arguments as in
6661 struct attribute_spec.handler. */
6663 static tree
6664 handle_gnu_inline_attribute (tree *node, tree name,
6665 tree ARG_UNUSED (args),
6666 int ARG_UNUSED (flags),
6667 bool *no_add_attrs)
6669 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6671 /* Do nothing else, just set the attribute. We'll get at
6672 it later with lookup_attribute. */
6674 else
6676 warning (OPT_Wattributes, "%qE attribute ignored", name);
6677 *no_add_attrs = true;
6680 return NULL_TREE;
6683 /* Handle a "leaf" attribute; arguments as in
6684 struct attribute_spec.handler. */
6686 static tree
6687 handle_leaf_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)
6693 warning (OPT_Wattributes, "%qE attribute ignored", name);
6694 *no_add_attrs = true;
6696 if (!TREE_PUBLIC (*node))
6698 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6699 *no_add_attrs = true;
6702 return NULL_TREE;
6705 /* Handle an "artificial" attribute; arguments as in
6706 struct attribute_spec.handler. */
6708 static tree
6709 handle_artificial_attribute (tree *node, tree name,
6710 tree ARG_UNUSED (args),
6711 int ARG_UNUSED (flags),
6712 bool *no_add_attrs)
6714 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6716 /* Do nothing else, just set the attribute. We'll get at
6717 it later with lookup_attribute. */
6719 else
6721 warning (OPT_Wattributes, "%qE attribute ignored", name);
6722 *no_add_attrs = true;
6725 return NULL_TREE;
6728 /* Handle a "flatten" attribute; arguments as in
6729 struct attribute_spec.handler. */
6731 static tree
6732 handle_flatten_attribute (tree *node, tree name,
6733 tree args ATTRIBUTE_UNUSED,
6734 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6736 if (TREE_CODE (*node) == FUNCTION_DECL)
6737 /* Do nothing else, just set the attribute. We'll get at
6738 it later with lookup_attribute. */
6740 else
6742 warning (OPT_Wattributes, "%qE attribute ignored", name);
6743 *no_add_attrs = true;
6746 return NULL_TREE;
6749 /* Handle a "warning" or "error" attribute; arguments as in
6750 struct attribute_spec.handler. */
6752 static tree
6753 handle_error_attribute (tree *node, tree name, tree args,
6754 int ARG_UNUSED (flags), bool *no_add_attrs)
6756 if (TREE_CODE (*node) == FUNCTION_DECL
6757 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6758 /* Do nothing else, just set the attribute. We'll get at
6759 it later with lookup_attribute. */
6761 else
6763 warning (OPT_Wattributes, "%qE attribute ignored", name);
6764 *no_add_attrs = true;
6767 return NULL_TREE;
6770 /* Handle a "used" attribute; arguments as in
6771 struct attribute_spec.handler. */
6773 static tree
6774 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6775 int ARG_UNUSED (flags), bool *no_add_attrs)
6777 tree node = *pnode;
6779 if (TREE_CODE (node) == FUNCTION_DECL
6780 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6781 || (TREE_CODE (node) == TYPE_DECL))
6783 TREE_USED (node) = 1;
6784 DECL_PRESERVE_P (node) = 1;
6785 if (TREE_CODE (node) == VAR_DECL)
6786 DECL_READ_P (node) = 1;
6788 else
6790 warning (OPT_Wattributes, "%qE attribute ignored", name);
6791 *no_add_attrs = true;
6794 return NULL_TREE;
6797 /* Handle a "unused" attribute; arguments as in
6798 struct attribute_spec.handler. */
6800 static tree
6801 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6802 int flags, bool *no_add_attrs)
6804 if (DECL_P (*node))
6806 tree decl = *node;
6808 if (TREE_CODE (decl) == PARM_DECL
6809 || TREE_CODE (decl) == VAR_DECL
6810 || TREE_CODE (decl) == FUNCTION_DECL
6811 || TREE_CODE (decl) == LABEL_DECL
6812 || TREE_CODE (decl) == TYPE_DECL)
6814 TREE_USED (decl) = 1;
6815 if (TREE_CODE (decl) == VAR_DECL
6816 || TREE_CODE (decl) == PARM_DECL)
6817 DECL_READ_P (decl) = 1;
6819 else
6821 warning (OPT_Wattributes, "%qE attribute ignored", name);
6822 *no_add_attrs = true;
6825 else
6827 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6828 *node = build_variant_type_copy (*node);
6829 TREE_USED (*node) = 1;
6832 return NULL_TREE;
6835 /* Handle a "externally_visible" attribute; arguments as in
6836 struct attribute_spec.handler. */
6838 static tree
6839 handle_externally_visible_attribute (tree *pnode, tree name,
6840 tree ARG_UNUSED (args),
6841 int ARG_UNUSED (flags),
6842 bool *no_add_attrs)
6844 tree node = *pnode;
6846 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6848 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6849 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6851 warning (OPT_Wattributes,
6852 "%qE attribute have effect only on public objects", name);
6853 *no_add_attrs = true;
6856 else
6858 warning (OPT_Wattributes, "%qE attribute ignored", name);
6859 *no_add_attrs = true;
6862 return NULL_TREE;
6865 /* Handle a "const" attribute; arguments as in
6866 struct attribute_spec.handler. */
6868 static tree
6869 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6870 int ARG_UNUSED (flags), bool *no_add_attrs)
6872 tree type = TREE_TYPE (*node);
6874 /* See FIXME comment on noreturn in c_common_attribute_table. */
6875 if (TREE_CODE (*node) == FUNCTION_DECL)
6876 TREE_READONLY (*node) = 1;
6877 else if (TREE_CODE (type) == POINTER_TYPE
6878 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6879 TREE_TYPE (*node)
6880 = build_pointer_type
6881 (build_type_variant (TREE_TYPE (type), 1,
6882 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6883 else
6885 warning (OPT_Wattributes, "%qE attribute ignored", name);
6886 *no_add_attrs = true;
6889 return NULL_TREE;
6892 /* Handle a "transparent_union" attribute; arguments as in
6893 struct attribute_spec.handler. */
6895 static tree
6896 handle_transparent_union_attribute (tree *node, tree name,
6897 tree ARG_UNUSED (args), int flags,
6898 bool *no_add_attrs)
6900 tree type;
6902 *no_add_attrs = true;
6905 if (TREE_CODE (*node) == TYPE_DECL
6906 && ! (flags & ATTR_FLAG_CXX11))
6907 node = &TREE_TYPE (*node);
6908 type = *node;
6910 if (TREE_CODE (type) == UNION_TYPE)
6912 /* Make sure that the first field will work for a transparent union.
6913 If the type isn't complete yet, leave the check to the code in
6914 finish_struct. */
6915 if (TYPE_SIZE (type))
6917 tree first = first_field (type);
6918 if (first == NULL_TREE
6919 || DECL_ARTIFICIAL (first)
6920 || TYPE_MODE (type) != DECL_MODE (first))
6921 goto ignored;
6924 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6926 /* If the type isn't complete yet, setting the flag
6927 on a variant wouldn't ever be checked. */
6928 if (!TYPE_SIZE (type))
6929 goto ignored;
6931 /* build_duplicate_type doesn't work for C++. */
6932 if (c_dialect_cxx ())
6933 goto ignored;
6935 /* A type variant isn't good enough, since we don't a cast
6936 to such a type removed as a no-op. */
6937 *node = type = build_duplicate_type (type);
6940 TYPE_TRANSPARENT_AGGR (type) = 1;
6941 return NULL_TREE;
6944 ignored:
6945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6946 return NULL_TREE;
6949 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6950 get the requested priority for a constructor or destructor,
6951 possibly issuing diagnostics for invalid or reserved
6952 priorities. */
6954 static priority_type
6955 get_priority (tree args, bool is_destructor)
6957 HOST_WIDE_INT pri;
6958 tree arg;
6960 if (!args)
6961 return DEFAULT_INIT_PRIORITY;
6963 if (!SUPPORTS_INIT_PRIORITY)
6965 if (is_destructor)
6966 error ("destructor priorities are not supported");
6967 else
6968 error ("constructor priorities are not supported");
6969 return DEFAULT_INIT_PRIORITY;
6972 arg = TREE_VALUE (args);
6973 arg = default_conversion (arg);
6974 if (!host_integerp (arg, /*pos=*/0)
6975 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6976 goto invalid;
6978 pri = tree_low_cst (arg, /*pos=*/0);
6979 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6980 goto invalid;
6982 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6984 if (is_destructor)
6985 warning (0,
6986 "destructor priorities from 0 to %d are reserved "
6987 "for the implementation",
6988 MAX_RESERVED_INIT_PRIORITY);
6989 else
6990 warning (0,
6991 "constructor priorities from 0 to %d are reserved "
6992 "for the implementation",
6993 MAX_RESERVED_INIT_PRIORITY);
6995 return pri;
6997 invalid:
6998 if (is_destructor)
6999 error ("destructor priorities must be integers from 0 to %d inclusive",
7000 MAX_INIT_PRIORITY);
7001 else
7002 error ("constructor priorities must be integers from 0 to %d inclusive",
7003 MAX_INIT_PRIORITY);
7004 return DEFAULT_INIT_PRIORITY;
7007 /* Handle a "constructor" attribute; arguments as in
7008 struct attribute_spec.handler. */
7010 static tree
7011 handle_constructor_attribute (tree *node, tree name, tree args,
7012 int ARG_UNUSED (flags),
7013 bool *no_add_attrs)
7015 tree decl = *node;
7016 tree type = TREE_TYPE (decl);
7018 if (TREE_CODE (decl) == FUNCTION_DECL
7019 && TREE_CODE (type) == FUNCTION_TYPE
7020 && decl_function_context (decl) == 0)
7022 priority_type priority;
7023 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7024 priority = get_priority (args, /*is_destructor=*/false);
7025 SET_DECL_INIT_PRIORITY (decl, priority);
7026 TREE_USED (decl) = 1;
7028 else
7030 warning (OPT_Wattributes, "%qE attribute ignored", name);
7031 *no_add_attrs = true;
7034 return NULL_TREE;
7037 /* Handle a "destructor" attribute; arguments as in
7038 struct attribute_spec.handler. */
7040 static tree
7041 handle_destructor_attribute (tree *node, tree name, tree args,
7042 int ARG_UNUSED (flags),
7043 bool *no_add_attrs)
7045 tree decl = *node;
7046 tree type = TREE_TYPE (decl);
7048 if (TREE_CODE (decl) == FUNCTION_DECL
7049 && TREE_CODE (type) == FUNCTION_TYPE
7050 && decl_function_context (decl) == 0)
7052 priority_type priority;
7053 DECL_STATIC_DESTRUCTOR (decl) = 1;
7054 priority = get_priority (args, /*is_destructor=*/true);
7055 SET_DECL_FINI_PRIORITY (decl, priority);
7056 TREE_USED (decl) = 1;
7058 else
7060 warning (OPT_Wattributes, "%qE attribute ignored", name);
7061 *no_add_attrs = true;
7064 return NULL_TREE;
7067 /* Nonzero if the mode is a valid vector mode for this architecture.
7068 This returns nonzero even if there is no hardware support for the
7069 vector mode, but we can emulate with narrower modes. */
7071 static int
7072 vector_mode_valid_p (enum machine_mode mode)
7074 enum mode_class mclass = GET_MODE_CLASS (mode);
7075 enum machine_mode innermode;
7077 /* Doh! What's going on? */
7078 if (mclass != MODE_VECTOR_INT
7079 && mclass != MODE_VECTOR_FLOAT
7080 && mclass != MODE_VECTOR_FRACT
7081 && mclass != MODE_VECTOR_UFRACT
7082 && mclass != MODE_VECTOR_ACCUM
7083 && mclass != MODE_VECTOR_UACCUM)
7084 return 0;
7086 /* Hardware support. Woo hoo! */
7087 if (targetm.vector_mode_supported_p (mode))
7088 return 1;
7090 innermode = GET_MODE_INNER (mode);
7092 /* We should probably return 1 if requesting V4DI and we have no DI,
7093 but we have V2DI, but this is probably very unlikely. */
7095 /* If we have support for the inner mode, we can safely emulate it.
7096 We may not have V2DI, but me can emulate with a pair of DIs. */
7097 return targetm.scalar_mode_supported_p (innermode);
7101 /* Handle a "mode" attribute; arguments as in
7102 struct attribute_spec.handler. */
7104 static tree
7105 handle_mode_attribute (tree *node, tree name, tree args,
7106 int ARG_UNUSED (flags), bool *no_add_attrs)
7108 tree type = *node;
7109 tree ident = TREE_VALUE (args);
7111 *no_add_attrs = true;
7113 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7114 warning (OPT_Wattributes, "%qE attribute ignored", name);
7115 else
7117 int j;
7118 const char *p = IDENTIFIER_POINTER (ident);
7119 int len = strlen (p);
7120 enum machine_mode mode = VOIDmode;
7121 tree typefm;
7122 bool valid_mode;
7124 if (len > 4 && p[0] == '_' && p[1] == '_'
7125 && p[len - 1] == '_' && p[len - 2] == '_')
7127 char *newp = (char *) alloca (len - 1);
7129 strcpy (newp, &p[2]);
7130 newp[len - 4] = '\0';
7131 p = newp;
7134 /* Change this type to have a type with the specified mode.
7135 First check for the special modes. */
7136 if (!strcmp (p, "byte"))
7137 mode = byte_mode;
7138 else if (!strcmp (p, "word"))
7139 mode = word_mode;
7140 else if (!strcmp (p, "pointer"))
7141 mode = ptr_mode;
7142 else if (!strcmp (p, "libgcc_cmp_return"))
7143 mode = targetm.libgcc_cmp_return_mode ();
7144 else if (!strcmp (p, "libgcc_shift_count"))
7145 mode = targetm.libgcc_shift_count_mode ();
7146 else if (!strcmp (p, "unwind_word"))
7147 mode = targetm.unwind_word_mode ();
7148 else
7149 for (j = 0; j < NUM_MACHINE_MODES; j++)
7150 if (!strcmp (p, GET_MODE_NAME (j)))
7152 mode = (enum machine_mode) j;
7153 break;
7156 if (mode == VOIDmode)
7158 error ("unknown machine mode %qE", ident);
7159 return NULL_TREE;
7162 valid_mode = false;
7163 switch (GET_MODE_CLASS (mode))
7165 case MODE_INT:
7166 case MODE_PARTIAL_INT:
7167 case MODE_FLOAT:
7168 case MODE_DECIMAL_FLOAT:
7169 case MODE_FRACT:
7170 case MODE_UFRACT:
7171 case MODE_ACCUM:
7172 case MODE_UACCUM:
7173 valid_mode = targetm.scalar_mode_supported_p (mode);
7174 break;
7176 case MODE_COMPLEX_INT:
7177 case MODE_COMPLEX_FLOAT:
7178 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7179 break;
7181 case MODE_VECTOR_INT:
7182 case MODE_VECTOR_FLOAT:
7183 case MODE_VECTOR_FRACT:
7184 case MODE_VECTOR_UFRACT:
7185 case MODE_VECTOR_ACCUM:
7186 case MODE_VECTOR_UACCUM:
7187 warning (OPT_Wattributes, "specifying vector types with "
7188 "__attribute__ ((mode)) is deprecated");
7189 warning (OPT_Wattributes,
7190 "use __attribute__ ((vector_size)) instead");
7191 valid_mode = vector_mode_valid_p (mode);
7192 break;
7194 default:
7195 break;
7197 if (!valid_mode)
7199 error ("unable to emulate %qs", p);
7200 return NULL_TREE;
7203 if (POINTER_TYPE_P (type))
7205 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7206 tree (*fn)(tree, enum machine_mode, bool);
7208 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7210 error ("invalid pointer mode %qs", p);
7211 return NULL_TREE;
7214 if (TREE_CODE (type) == POINTER_TYPE)
7215 fn = build_pointer_type_for_mode;
7216 else
7217 fn = build_reference_type_for_mode;
7218 typefm = fn (TREE_TYPE (type), mode, false);
7220 else
7222 /* For fixed-point modes, we need to test if the signness of type
7223 and the machine mode are consistent. */
7224 if (ALL_FIXED_POINT_MODE_P (mode)
7225 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7227 error ("signedness of type and machine mode %qs don%'t match", p);
7228 return NULL_TREE;
7230 /* For fixed-point modes, we need to pass saturating info. */
7231 typefm = lang_hooks.types.type_for_mode (mode,
7232 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7233 : TYPE_UNSIGNED (type));
7236 if (typefm == NULL_TREE)
7238 error ("no data type for mode %qs", p);
7239 return NULL_TREE;
7241 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7243 /* For enumeral types, copy the precision from the integer
7244 type returned above. If not an INTEGER_TYPE, we can't use
7245 this mode for this type. */
7246 if (TREE_CODE (typefm) != INTEGER_TYPE)
7248 error ("cannot use mode %qs for enumeral types", p);
7249 return NULL_TREE;
7252 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7254 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7255 typefm = type;
7257 else
7259 /* We cannot build a type variant, as there's code that assumes
7260 that TYPE_MAIN_VARIANT has the same mode. This includes the
7261 debug generators. Instead, create a subrange type. This
7262 results in all of the enumeral values being emitted only once
7263 in the original, and the subtype gets them by reference. */
7264 if (TYPE_UNSIGNED (type))
7265 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7266 else
7267 typefm = make_signed_type (TYPE_PRECISION (typefm));
7268 TREE_TYPE (typefm) = type;
7271 else if (VECTOR_MODE_P (mode)
7272 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7273 : TREE_CODE (type) != TREE_CODE (typefm))
7275 error ("mode %qs applied to inappropriate type", p);
7276 return NULL_TREE;
7279 *node = typefm;
7282 return NULL_TREE;
7285 /* Handle a "section" attribute; arguments as in
7286 struct attribute_spec.handler. */
7288 static tree
7289 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7290 int ARG_UNUSED (flags), bool *no_add_attrs)
7292 tree decl = *node;
7294 if (targetm_common.have_named_sections)
7296 user_defined_section_attribute = true;
7298 if ((TREE_CODE (decl) == FUNCTION_DECL
7299 || TREE_CODE (decl) == VAR_DECL)
7300 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7302 if (TREE_CODE (decl) == VAR_DECL
7303 && current_function_decl != NULL_TREE
7304 && !TREE_STATIC (decl))
7306 error_at (DECL_SOURCE_LOCATION (decl),
7307 "section attribute cannot be specified for "
7308 "local variables");
7309 *no_add_attrs = true;
7312 /* The decl may have already been given a section attribute
7313 from a previous declaration. Ensure they match. */
7314 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7315 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7316 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7318 error ("section of %q+D conflicts with previous declaration",
7319 *node);
7320 *no_add_attrs = true;
7322 else if (TREE_CODE (decl) == VAR_DECL
7323 && !targetm.have_tls && targetm.emutls.tmpl_section
7324 && DECL_THREAD_LOCAL_P (decl))
7326 error ("section of %q+D cannot be overridden", *node);
7327 *no_add_attrs = true;
7329 else
7330 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7332 else
7334 error ("section attribute not allowed for %q+D", *node);
7335 *no_add_attrs = true;
7338 else
7340 error_at (DECL_SOURCE_LOCATION (*node),
7341 "section attributes are not supported for this target");
7342 *no_add_attrs = true;
7345 return NULL_TREE;
7348 /* Check whether ALIGN is a valid user-specified alignment. If so,
7349 return its base-2 log; if not, output an error and return -1. If
7350 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7351 no error. */
7353 check_user_alignment (const_tree align, bool allow_zero)
7355 int i;
7357 if (TREE_CODE (align) != INTEGER_CST
7358 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7360 error ("requested alignment is not an integer constant");
7361 return -1;
7363 else if (allow_zero && integer_zerop (align))
7364 return -1;
7365 else if (tree_int_cst_sgn (align) == -1
7366 || (i = tree_log2 (align)) == -1)
7368 error ("requested alignment is not a positive power of 2");
7369 return -1;
7371 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7373 error ("requested alignment is too large");
7374 return -1;
7376 return i;
7380 If in c++-11, check if the c++-11 alignment constraint with respect
7381 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7382 c++-11 mode, does nothing.
7384 [dcl.align]2/ says:
7386 [* if the constant expression evaluates to a fundamental alignment,
7387 the alignment requirement of the declared entity shall be the
7388 specified fundamental alignment.
7390 * if the constant expression evaluates to an extended alignment
7391 and the implementation supports that alignment in the context
7392 of the declaration, the alignment of the declared entity shall
7393 be that alignment
7395 * if the constant expression evaluates to an extended alignment
7396 and the implementation does not support that alignment in the
7397 context of the declaration, the program is ill-formed]. */
7399 static bool
7400 check_cxx_fundamental_alignment_constraints (tree node,
7401 unsigned align_log,
7402 int flags)
7404 bool alignment_too_large_p = false;
7405 unsigned requested_alignment = 1U << align_log;
7406 unsigned max_align = 0;
7408 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7409 || (node == NULL_TREE || node == error_mark_node))
7410 return true;
7412 if (cxx_fundamental_alignment_p (requested_alignment))
7413 return true;
7415 if (DECL_P (node))
7417 if (TREE_STATIC (node))
7419 /* For file scope variables and static members, the target
7420 supports alignments that are at most
7421 MAX_OFILE_ALIGNMENT. */
7422 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7423 alignment_too_large_p = true;
7425 else
7427 #ifdef BIGGEST_FIELD_ALIGNMENT
7428 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7429 #else
7430 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7431 #endif
7432 /* For non-static members, the target supports either
7433 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7434 if it is defined or BIGGEST_ALIGNMENT. */
7435 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7436 if (TREE_CODE (node) == FIELD_DECL
7437 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7438 alignment_too_large_p = true;
7439 #undef MAX_TARGET_FIELD_ALIGNMENT
7440 /* For stack variables, the target supports at most
7441 MAX_STACK_ALIGNMENT. */
7442 else if (decl_function_context (node) != NULL
7443 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7444 alignment_too_large_p = true;
7447 else if (TYPE_P (node))
7449 /* Let's be liberal for types. */
7450 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7451 alignment_too_large_p = true;
7454 if (alignment_too_large_p)
7455 pedwarn (input_location, OPT_Wattributes,
7456 "requested alignment %d is larger than %d",
7457 requested_alignment, max_align);
7459 return !alignment_too_large_p;
7462 /* Handle a "aligned" attribute; arguments as in
7463 struct attribute_spec.handler. */
7465 static tree
7466 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7467 int flags, bool *no_add_attrs)
7469 tree decl = NULL_TREE;
7470 tree *type = NULL;
7471 int is_type = 0;
7472 tree align_expr = (args ? TREE_VALUE (args)
7473 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7474 int i;
7476 if (DECL_P (*node))
7478 decl = *node;
7479 type = &TREE_TYPE (decl);
7480 is_type = TREE_CODE (*node) == TYPE_DECL;
7482 else if (TYPE_P (*node))
7483 type = node, is_type = 1;
7485 if ((i = check_user_alignment (align_expr, false)) == -1
7486 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7487 *no_add_attrs = true;
7488 else if (is_type)
7490 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7491 /* OK, modify the type in place. */;
7492 /* If we have a TYPE_DECL, then copy the type, so that we
7493 don't accidentally modify a builtin type. See pushdecl. */
7494 else if (decl && TREE_TYPE (decl) != error_mark_node
7495 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7497 tree tt = TREE_TYPE (decl);
7498 *type = build_variant_type_copy (*type);
7499 DECL_ORIGINAL_TYPE (decl) = tt;
7500 TYPE_NAME (*type) = decl;
7501 TREE_USED (*type) = TREE_USED (decl);
7502 TREE_TYPE (decl) = *type;
7504 else
7505 *type = build_variant_type_copy (*type);
7507 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7508 TYPE_USER_ALIGN (*type) = 1;
7510 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7511 && TREE_CODE (decl) != FIELD_DECL)
7513 error ("alignment may not be specified for %q+D", decl);
7514 *no_add_attrs = true;
7516 else if (DECL_USER_ALIGN (decl)
7517 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7518 /* C++-11 [dcl.align/4]:
7520 When multiple alignment-specifiers are specified for an
7521 entity, the alignment requirement shall be set to the
7522 strictest specified alignment.
7524 This formally comes from the c++11 specification but we are
7525 doing it for the GNU attribute syntax as well. */
7526 *no_add_attrs = true;
7527 else if (TREE_CODE (decl) == FUNCTION_DECL
7528 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7530 if (DECL_USER_ALIGN (decl))
7531 error ("alignment for %q+D was previously specified as %d "
7532 "and may not be decreased", decl,
7533 DECL_ALIGN (decl) / BITS_PER_UNIT);
7534 else
7535 error ("alignment for %q+D must be at least %d", decl,
7536 DECL_ALIGN (decl) / BITS_PER_UNIT);
7537 *no_add_attrs = true;
7539 else
7541 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7542 DECL_USER_ALIGN (decl) = 1;
7545 return NULL_TREE;
7548 /* Handle a "weak" attribute; arguments as in
7549 struct attribute_spec.handler. */
7551 static tree
7552 handle_weak_attribute (tree *node, tree name,
7553 tree ARG_UNUSED (args),
7554 int ARG_UNUSED (flags),
7555 bool * ARG_UNUSED (no_add_attrs))
7557 if (TREE_CODE (*node) == FUNCTION_DECL
7558 && DECL_DECLARED_INLINE_P (*node))
7560 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7561 *no_add_attrs = true;
7563 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7565 error ("indirect function %q+D cannot be declared weak", *node);
7566 *no_add_attrs = true;
7567 return NULL_TREE;
7569 else if (TREE_CODE (*node) == FUNCTION_DECL
7570 || TREE_CODE (*node) == VAR_DECL)
7571 declare_weak (*node);
7572 else
7573 warning (OPT_Wattributes, "%qE attribute ignored", name);
7575 return NULL_TREE;
7578 /* Handle an "alias" or "ifunc" attribute; arguments as in
7579 struct attribute_spec.handler, except that IS_ALIAS tells us
7580 whether this is an alias as opposed to ifunc attribute. */
7582 static tree
7583 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7584 bool *no_add_attrs)
7586 tree decl = *node;
7588 if (TREE_CODE (decl) != FUNCTION_DECL
7589 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7591 warning (OPT_Wattributes, "%qE attribute ignored", name);
7592 *no_add_attrs = true;
7594 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7595 || (TREE_CODE (decl) != FUNCTION_DECL
7596 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7597 /* A static variable declaration is always a tentative definition,
7598 but the alias is a non-tentative definition which overrides. */
7599 || (TREE_CODE (decl) != FUNCTION_DECL
7600 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7602 error ("%q+D defined both normally and as %qE attribute", decl, name);
7603 *no_add_attrs = true;
7604 return NULL_TREE;
7606 else if (!is_alias
7607 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7608 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7610 error ("weak %q+D cannot be defined %qE", decl, name);
7611 *no_add_attrs = true;
7612 return NULL_TREE;
7615 /* Note that the very first time we process a nested declaration,
7616 decl_function_context will not be set. Indeed, *would* never
7617 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7618 we do below. After such frobbery, pushdecl would set the context.
7619 In any case, this is never what we want. */
7620 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7622 tree id;
7624 id = TREE_VALUE (args);
7625 if (TREE_CODE (id) != STRING_CST)
7627 error ("attribute %qE argument not a string", name);
7628 *no_add_attrs = true;
7629 return NULL_TREE;
7631 id = get_identifier (TREE_STRING_POINTER (id));
7632 /* This counts as a use of the object pointed to. */
7633 TREE_USED (id) = 1;
7635 if (TREE_CODE (decl) == FUNCTION_DECL)
7636 DECL_INITIAL (decl) = error_mark_node;
7637 else
7639 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7640 DECL_EXTERNAL (decl) = 1;
7641 else
7642 DECL_EXTERNAL (decl) = 0;
7643 TREE_STATIC (decl) = 1;
7646 if (!is_alias)
7647 /* ifuncs are also aliases, so set that attribute too. */
7648 DECL_ATTRIBUTES (decl)
7649 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7651 else
7653 warning (OPT_Wattributes, "%qE attribute ignored", name);
7654 *no_add_attrs = true;
7657 return NULL_TREE;
7660 /* Handle an "alias" or "ifunc" attribute; arguments as in
7661 struct attribute_spec.handler. */
7663 static tree
7664 handle_ifunc_attribute (tree *node, tree name, tree args,
7665 int ARG_UNUSED (flags), bool *no_add_attrs)
7667 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7670 /* Handle an "alias" or "ifunc" attribute; arguments as in
7671 struct attribute_spec.handler. */
7673 static tree
7674 handle_alias_attribute (tree *node, tree name, tree args,
7675 int ARG_UNUSED (flags), bool *no_add_attrs)
7677 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7680 /* Handle a "weakref" attribute; arguments as in struct
7681 attribute_spec.handler. */
7683 static tree
7684 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7685 int flags, bool *no_add_attrs)
7687 tree attr = NULL_TREE;
7689 /* We must ignore the attribute when it is associated with
7690 local-scoped decls, since attribute alias is ignored and many
7691 such symbols do not even have a DECL_WEAK field. */
7692 if (decl_function_context (*node)
7693 || current_function_decl
7694 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7696 warning (OPT_Wattributes, "%qE attribute ignored", name);
7697 *no_add_attrs = true;
7698 return NULL_TREE;
7701 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7703 error ("indirect function %q+D cannot be declared weakref", *node);
7704 *no_add_attrs = true;
7705 return NULL_TREE;
7708 /* The idea here is that `weakref("name")' mutates into `weakref,
7709 alias("name")', and weakref without arguments, in turn,
7710 implicitly adds weak. */
7712 if (args)
7714 attr = tree_cons (get_identifier ("alias"), args, attr);
7715 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7717 *no_add_attrs = true;
7719 decl_attributes (node, attr, flags);
7721 else
7723 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7724 error_at (DECL_SOURCE_LOCATION (*node),
7725 "weakref attribute must appear before alias attribute");
7727 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7728 and that isn't supported; and because it wants to add it to
7729 the list of weak decls, which isn't helpful. */
7730 DECL_WEAK (*node) = 1;
7733 return NULL_TREE;
7736 /* Handle an "visibility" attribute; arguments as in
7737 struct attribute_spec.handler. */
7739 static tree
7740 handle_visibility_attribute (tree *node, tree name, tree args,
7741 int ARG_UNUSED (flags),
7742 bool *ARG_UNUSED (no_add_attrs))
7744 tree decl = *node;
7745 tree id = TREE_VALUE (args);
7746 enum symbol_visibility vis;
7748 if (TYPE_P (*node))
7750 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7751 /* OK */;
7752 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7754 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7755 name);
7756 return NULL_TREE;
7758 else if (TYPE_FIELDS (*node))
7760 error ("%qE attribute ignored because %qT is already defined",
7761 name, *node);
7762 return NULL_TREE;
7765 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7767 warning (OPT_Wattributes, "%qE attribute ignored", name);
7768 return NULL_TREE;
7771 if (TREE_CODE (id) != STRING_CST)
7773 error ("visibility argument not a string");
7774 return NULL_TREE;
7777 /* If this is a type, set the visibility on the type decl. */
7778 if (TYPE_P (decl))
7780 decl = TYPE_NAME (decl);
7781 if (!decl)
7782 return NULL_TREE;
7783 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7785 warning (OPT_Wattributes, "%qE attribute ignored on types",
7786 name);
7787 return NULL_TREE;
7791 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7792 vis = VISIBILITY_DEFAULT;
7793 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7794 vis = VISIBILITY_INTERNAL;
7795 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7796 vis = VISIBILITY_HIDDEN;
7797 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7798 vis = VISIBILITY_PROTECTED;
7799 else
7801 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7802 vis = VISIBILITY_DEFAULT;
7805 if (DECL_VISIBILITY_SPECIFIED (decl)
7806 && vis != DECL_VISIBILITY (decl))
7808 tree attributes = (TYPE_P (*node)
7809 ? TYPE_ATTRIBUTES (*node)
7810 : DECL_ATTRIBUTES (decl));
7811 if (lookup_attribute ("visibility", attributes))
7812 error ("%qD redeclared with different visibility", decl);
7813 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7814 && lookup_attribute ("dllimport", attributes))
7815 error ("%qD was declared %qs which implies default visibility",
7816 decl, "dllimport");
7817 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7818 && lookup_attribute ("dllexport", attributes))
7819 error ("%qD was declared %qs which implies default visibility",
7820 decl, "dllexport");
7823 DECL_VISIBILITY (decl) = vis;
7824 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7826 /* Go ahead and attach the attribute to the node as well. This is needed
7827 so we can determine whether we have VISIBILITY_DEFAULT because the
7828 visibility was not specified, or because it was explicitly overridden
7829 from the containing scope. */
7831 return NULL_TREE;
7834 /* Determine the ELF symbol visibility for DECL, which is either a
7835 variable or a function. It is an error to use this function if a
7836 definition of DECL is not available in this translation unit.
7837 Returns true if the final visibility has been determined by this
7838 function; false if the caller is free to make additional
7839 modifications. */
7841 bool
7842 c_determine_visibility (tree decl)
7844 gcc_assert (TREE_CODE (decl) == VAR_DECL
7845 || TREE_CODE (decl) == FUNCTION_DECL);
7847 /* If the user explicitly specified the visibility with an
7848 attribute, honor that. DECL_VISIBILITY will have been set during
7849 the processing of the attribute. We check for an explicit
7850 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7851 to distinguish the use of an attribute from the use of a "#pragma
7852 GCC visibility push(...)"; in the latter case we still want other
7853 considerations to be able to overrule the #pragma. */
7854 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7855 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7856 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7857 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7858 return true;
7860 /* Set default visibility to whatever the user supplied with
7861 visibility_specified depending on #pragma GCC visibility. */
7862 if (!DECL_VISIBILITY_SPECIFIED (decl))
7864 if (visibility_options.inpragma
7865 || DECL_VISIBILITY (decl) != default_visibility)
7867 DECL_VISIBILITY (decl) = default_visibility;
7868 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7869 /* If visibility changed and DECL already has DECL_RTL, ensure
7870 symbol flags are updated. */
7871 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7872 || TREE_CODE (decl) == FUNCTION_DECL)
7873 && DECL_RTL_SET_P (decl))
7874 make_decl_rtl (decl);
7877 return false;
7880 /* Handle an "tls_model" attribute; arguments as in
7881 struct attribute_spec.handler. */
7883 static tree
7884 handle_tls_model_attribute (tree *node, tree name, tree args,
7885 int ARG_UNUSED (flags), bool *no_add_attrs)
7887 tree id;
7888 tree decl = *node;
7889 enum tls_model kind;
7891 *no_add_attrs = true;
7893 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7895 warning (OPT_Wattributes, "%qE attribute ignored", name);
7896 return NULL_TREE;
7899 kind = DECL_TLS_MODEL (decl);
7900 id = TREE_VALUE (args);
7901 if (TREE_CODE (id) != STRING_CST)
7903 error ("tls_model argument not a string");
7904 return NULL_TREE;
7907 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7908 kind = TLS_MODEL_LOCAL_EXEC;
7909 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7910 kind = TLS_MODEL_INITIAL_EXEC;
7911 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7912 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7913 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7914 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7915 else
7916 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7918 DECL_TLS_MODEL (decl) = kind;
7919 return NULL_TREE;
7922 /* Handle a "no_instrument_function" attribute; arguments as in
7923 struct attribute_spec.handler. */
7925 static tree
7926 handle_no_instrument_function_attribute (tree *node, tree name,
7927 tree ARG_UNUSED (args),
7928 int ARG_UNUSED (flags),
7929 bool *no_add_attrs)
7931 tree decl = *node;
7933 if (TREE_CODE (decl) != FUNCTION_DECL)
7935 error_at (DECL_SOURCE_LOCATION (decl),
7936 "%qE attribute applies only to functions", name);
7937 *no_add_attrs = true;
7939 else if (DECL_INITIAL (decl))
7941 error_at (DECL_SOURCE_LOCATION (decl),
7942 "can%'t set %qE attribute after definition", name);
7943 *no_add_attrs = true;
7945 else
7946 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7948 return NULL_TREE;
7951 /* Handle a "malloc" attribute; arguments as in
7952 struct attribute_spec.handler. */
7954 static tree
7955 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7956 int ARG_UNUSED (flags), bool *no_add_attrs)
7958 if (TREE_CODE (*node) == FUNCTION_DECL
7959 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7960 DECL_IS_MALLOC (*node) = 1;
7961 else
7963 warning (OPT_Wattributes, "%qE attribute ignored", name);
7964 *no_add_attrs = true;
7967 return NULL_TREE;
7970 /* Handle a "alloc_size" attribute; arguments as in
7971 struct attribute_spec.handler. */
7973 static tree
7974 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7975 int ARG_UNUSED (flags), bool *no_add_attrs)
7977 unsigned arg_count = type_num_arguments (*node);
7978 for (; args; args = TREE_CHAIN (args))
7980 tree position = TREE_VALUE (args);
7982 if (TREE_CODE (position) != INTEGER_CST
7983 || TREE_INT_CST_HIGH (position)
7984 || TREE_INT_CST_LOW (position) < 1
7985 || TREE_INT_CST_LOW (position) > arg_count )
7987 warning (OPT_Wattributes,
7988 "alloc_size parameter outside range");
7989 *no_add_attrs = true;
7990 return NULL_TREE;
7993 return NULL_TREE;
7996 /* Handle a "fn spec" attribute; arguments as in
7997 struct attribute_spec.handler. */
7999 static tree
8000 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8001 tree args, int ARG_UNUSED (flags),
8002 bool *no_add_attrs ATTRIBUTE_UNUSED)
8004 gcc_assert (args
8005 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8006 && !TREE_CHAIN (args));
8007 return NULL_TREE;
8010 /* Handle a "returns_twice" attribute; arguments as in
8011 struct attribute_spec.handler. */
8013 static tree
8014 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8015 int ARG_UNUSED (flags), bool *no_add_attrs)
8017 if (TREE_CODE (*node) == FUNCTION_DECL)
8018 DECL_IS_RETURNS_TWICE (*node) = 1;
8019 else
8021 warning (OPT_Wattributes, "%qE attribute ignored", name);
8022 *no_add_attrs = true;
8025 return NULL_TREE;
8028 /* Handle a "no_limit_stack" attribute; arguments as in
8029 struct attribute_spec.handler. */
8031 static tree
8032 handle_no_limit_stack_attribute (tree *node, tree name,
8033 tree ARG_UNUSED (args),
8034 int ARG_UNUSED (flags),
8035 bool *no_add_attrs)
8037 tree decl = *node;
8039 if (TREE_CODE (decl) != FUNCTION_DECL)
8041 error_at (DECL_SOURCE_LOCATION (decl),
8042 "%qE attribute applies only to functions", name);
8043 *no_add_attrs = true;
8045 else if (DECL_INITIAL (decl))
8047 error_at (DECL_SOURCE_LOCATION (decl),
8048 "can%'t set %qE attribute after definition", name);
8049 *no_add_attrs = true;
8051 else
8052 DECL_NO_LIMIT_STACK (decl) = 1;
8054 return NULL_TREE;
8057 /* Handle a "pure" attribute; arguments as in
8058 struct attribute_spec.handler. */
8060 static tree
8061 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8062 int ARG_UNUSED (flags), bool *no_add_attrs)
8064 if (TREE_CODE (*node) == FUNCTION_DECL)
8065 DECL_PURE_P (*node) = 1;
8066 /* ??? TODO: Support types. */
8067 else
8069 warning (OPT_Wattributes, "%qE attribute ignored", name);
8070 *no_add_attrs = true;
8073 return NULL_TREE;
8076 /* Digest an attribute list destined for a transactional memory statement.
8077 ALLOWED is the set of attributes that are allowed for this statement;
8078 return the attribute we parsed. Multiple attributes are never allowed. */
8081 parse_tm_stmt_attr (tree attrs, int allowed)
8083 tree a_seen = NULL;
8084 int m_seen = 0;
8086 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8088 tree a = TREE_PURPOSE (attrs);
8089 int m = 0;
8091 if (is_attribute_p ("outer", a))
8092 m = TM_STMT_ATTR_OUTER;
8094 if ((m & allowed) == 0)
8096 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8097 continue;
8100 if (m_seen == 0)
8102 a_seen = a;
8103 m_seen = m;
8105 else if (m_seen == m)
8106 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8107 else
8108 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8111 return m_seen;
8114 /* Transform a TM attribute name into a maskable integer and back.
8115 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8116 to how the lack of an attribute is treated. */
8119 tm_attr_to_mask (tree attr)
8121 if (attr == NULL)
8122 return 0;
8123 if (is_attribute_p ("transaction_safe", attr))
8124 return TM_ATTR_SAFE;
8125 if (is_attribute_p ("transaction_callable", attr))
8126 return TM_ATTR_CALLABLE;
8127 if (is_attribute_p ("transaction_pure", attr))
8128 return TM_ATTR_PURE;
8129 if (is_attribute_p ("transaction_unsafe", attr))
8130 return TM_ATTR_IRREVOCABLE;
8131 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8132 return TM_ATTR_MAY_CANCEL_OUTER;
8133 return 0;
8136 tree
8137 tm_mask_to_attr (int mask)
8139 const char *str;
8140 switch (mask)
8142 case TM_ATTR_SAFE:
8143 str = "transaction_safe";
8144 break;
8145 case TM_ATTR_CALLABLE:
8146 str = "transaction_callable";
8147 break;
8148 case TM_ATTR_PURE:
8149 str = "transaction_pure";
8150 break;
8151 case TM_ATTR_IRREVOCABLE:
8152 str = "transaction_unsafe";
8153 break;
8154 case TM_ATTR_MAY_CANCEL_OUTER:
8155 str = "transaction_may_cancel_outer";
8156 break;
8157 default:
8158 gcc_unreachable ();
8160 return get_identifier (str);
8163 /* Return the first TM attribute seen in LIST. */
8165 tree
8166 find_tm_attribute (tree list)
8168 for (; list ; list = TREE_CHAIN (list))
8170 tree name = TREE_PURPOSE (list);
8171 if (tm_attr_to_mask (name) != 0)
8172 return name;
8174 return NULL_TREE;
8177 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8178 Here we accept only function types, and verify that none of the other
8179 function TM attributes are also applied. */
8180 /* ??? We need to accept class types for C++, but not C. This greatly
8181 complicates this function, since we can no longer rely on the extra
8182 processing given by function_type_required. */
8184 static tree
8185 handle_tm_attribute (tree *node, tree name, tree args,
8186 int flags, bool *no_add_attrs)
8188 /* Only one path adds the attribute; others don't. */
8189 *no_add_attrs = true;
8191 switch (TREE_CODE (*node))
8193 case RECORD_TYPE:
8194 case UNION_TYPE:
8195 /* Only tm_callable and tm_safe apply to classes. */
8196 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8197 goto ignored;
8198 /* FALLTHRU */
8200 case FUNCTION_TYPE:
8201 case METHOD_TYPE:
8203 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8204 if (old_name == name)
8206 else if (old_name != NULL_TREE)
8207 error ("type was previously declared %qE", old_name);
8208 else
8209 *no_add_attrs = false;
8211 break;
8213 case POINTER_TYPE:
8215 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8216 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8218 tree fn_tmp = TREE_TYPE (*node);
8219 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8220 *node = build_pointer_type (fn_tmp);
8221 break;
8224 /* FALLTHRU */
8226 default:
8227 /* If a function is next, pass it on to be tried next. */
8228 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8229 return tree_cons (name, args, NULL);
8231 ignored:
8232 warning (OPT_Wattributes, "%qE attribute ignored", name);
8233 break;
8236 return NULL_TREE;
8239 /* Handle the TM_WRAP attribute; arguments as in
8240 struct attribute_spec.handler. */
8242 static tree
8243 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8244 int ARG_UNUSED (flags), bool *no_add_attrs)
8246 tree decl = *node;
8248 /* We don't need the attribute even on success, since we
8249 record the entry in an external table. */
8250 *no_add_attrs = true;
8252 if (TREE_CODE (decl) != FUNCTION_DECL)
8253 warning (OPT_Wattributes, "%qE attribute ignored", name);
8254 else
8256 tree wrap_decl = TREE_VALUE (args);
8257 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8258 && TREE_CODE (wrap_decl) != VAR_DECL
8259 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8260 error ("%qE argument not an identifier", name);
8261 else
8263 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8264 wrap_decl = lookup_name (wrap_decl);
8265 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8267 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8268 TREE_TYPE (wrap_decl)))
8269 record_tm_replacement (wrap_decl, decl);
8270 else
8271 error ("%qD is not compatible with %qD", wrap_decl, decl);
8273 else
8274 error ("transaction_wrap argument is not a function");
8278 return NULL_TREE;
8281 /* Ignore the given attribute. Used when this attribute may be usefully
8282 overridden by the target, but is not used generically. */
8284 static tree
8285 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8286 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8287 bool *no_add_attrs)
8289 *no_add_attrs = true;
8290 return NULL_TREE;
8293 /* Handle a "no vops" attribute; arguments as in
8294 struct attribute_spec.handler. */
8296 static tree
8297 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8298 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8299 bool *ARG_UNUSED (no_add_attrs))
8301 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8302 DECL_IS_NOVOPS (*node) = 1;
8303 return NULL_TREE;
8306 /* Handle a "deprecated" attribute; arguments as in
8307 struct attribute_spec.handler. */
8309 static tree
8310 handle_deprecated_attribute (tree *node, tree name,
8311 tree args, int flags,
8312 bool *no_add_attrs)
8314 tree type = NULL_TREE;
8315 int warn = 0;
8316 tree what = NULL_TREE;
8318 if (!args)
8319 *no_add_attrs = true;
8320 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8322 error ("deprecated message is not a string");
8323 *no_add_attrs = true;
8326 if (DECL_P (*node))
8328 tree decl = *node;
8329 type = TREE_TYPE (decl);
8331 if (TREE_CODE (decl) == TYPE_DECL
8332 || TREE_CODE (decl) == PARM_DECL
8333 || TREE_CODE (decl) == VAR_DECL
8334 || TREE_CODE (decl) == FUNCTION_DECL
8335 || TREE_CODE (decl) == FIELD_DECL
8336 || objc_method_decl (TREE_CODE (decl)))
8337 TREE_DEPRECATED (decl) = 1;
8338 else
8339 warn = 1;
8341 else if (TYPE_P (*node))
8343 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8344 *node = build_variant_type_copy (*node);
8345 TREE_DEPRECATED (*node) = 1;
8346 type = *node;
8348 else
8349 warn = 1;
8351 if (warn)
8353 *no_add_attrs = true;
8354 if (type && TYPE_NAME (type))
8356 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8357 what = TYPE_NAME (*node);
8358 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8359 && DECL_NAME (TYPE_NAME (type)))
8360 what = DECL_NAME (TYPE_NAME (type));
8362 if (what)
8363 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8364 else
8365 warning (OPT_Wattributes, "%qE attribute ignored", name);
8368 return NULL_TREE;
8371 /* Handle a "vector_size" attribute; arguments as in
8372 struct attribute_spec.handler. */
8374 static tree
8375 handle_vector_size_attribute (tree *node, tree name, tree args,
8376 int ARG_UNUSED (flags),
8377 bool *no_add_attrs)
8379 unsigned HOST_WIDE_INT vecsize, nunits;
8380 enum machine_mode orig_mode;
8381 tree type = *node, new_type, size;
8383 *no_add_attrs = true;
8385 size = TREE_VALUE (args);
8387 if (!host_integerp (size, 1))
8389 warning (OPT_Wattributes, "%qE attribute ignored", name);
8390 return NULL_TREE;
8393 /* Get the vector size (in bytes). */
8394 vecsize = tree_low_cst (size, 1);
8396 /* We need to provide for vector pointers, vector arrays, and
8397 functions returning vectors. For example:
8399 __attribute__((vector_size(16))) short *foo;
8401 In this case, the mode is SI, but the type being modified is
8402 HI, so we need to look further. */
8404 while (POINTER_TYPE_P (type)
8405 || TREE_CODE (type) == FUNCTION_TYPE
8406 || TREE_CODE (type) == METHOD_TYPE
8407 || TREE_CODE (type) == ARRAY_TYPE
8408 || TREE_CODE (type) == OFFSET_TYPE)
8409 type = TREE_TYPE (type);
8411 /* Get the mode of the type being modified. */
8412 orig_mode = TYPE_MODE (type);
8414 if ((!INTEGRAL_TYPE_P (type)
8415 && !SCALAR_FLOAT_TYPE_P (type)
8416 && !FIXED_POINT_TYPE_P (type))
8417 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8418 && GET_MODE_CLASS (orig_mode) != MODE_INT
8419 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8420 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8421 || TREE_CODE (type) == BOOLEAN_TYPE)
8423 error ("invalid vector type for attribute %qE", name);
8424 return NULL_TREE;
8427 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8429 error ("vector size not an integral multiple of component size");
8430 return NULL;
8433 if (vecsize == 0)
8435 error ("zero vector size");
8436 return NULL;
8439 /* Calculate how many units fit in the vector. */
8440 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8441 if (nunits & (nunits - 1))
8443 error ("number of components of the vector not a power of two");
8444 return NULL_TREE;
8447 new_type = build_vector_type (type, nunits);
8449 /* Build back pointers if needed. */
8450 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8452 return NULL_TREE;
8455 /* Handle the "nonnull" attribute. */
8456 static tree
8457 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8458 tree args, int ARG_UNUSED (flags),
8459 bool *no_add_attrs)
8461 tree type = *node;
8462 unsigned HOST_WIDE_INT attr_arg_num;
8464 /* If no arguments are specified, all pointer arguments should be
8465 non-null. Verify a full prototype is given so that the arguments
8466 will have the correct types when we actually check them later. */
8467 if (!args)
8469 if (!prototype_p (type))
8471 error ("nonnull attribute without arguments on a non-prototype");
8472 *no_add_attrs = true;
8474 return NULL_TREE;
8477 /* Argument list specified. Verify that each argument number references
8478 a pointer argument. */
8479 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8481 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8483 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8485 error ("nonnull argument has invalid operand number (argument %lu)",
8486 (unsigned long) attr_arg_num);
8487 *no_add_attrs = true;
8488 return NULL_TREE;
8491 if (prototype_p (type))
8493 function_args_iterator iter;
8494 tree argument;
8496 function_args_iter_init (&iter, type);
8497 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8499 argument = function_args_iter_cond (&iter);
8500 if (argument == NULL_TREE || ck_num == arg_num)
8501 break;
8504 if (!argument
8505 || TREE_CODE (argument) == VOID_TYPE)
8507 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8508 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8509 *no_add_attrs = true;
8510 return NULL_TREE;
8513 if (TREE_CODE (argument) != POINTER_TYPE)
8515 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8516 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8517 *no_add_attrs = true;
8518 return NULL_TREE;
8523 return NULL_TREE;
8526 /* Check the argument list of a function call for null in argument slots
8527 that are marked as requiring a non-null pointer argument. The NARGS
8528 arguments are passed in the array ARGARRAY.
8531 static void
8532 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8534 tree a;
8535 int i;
8537 attrs = lookup_attribute ("nonnull", attrs);
8538 if (attrs == NULL_TREE)
8539 return;
8541 a = attrs;
8542 /* See if any of the nonnull attributes has no arguments. If so,
8543 then every pointer argument is checked (in which case the check
8544 for pointer type is done in check_nonnull_arg). */
8545 if (TREE_VALUE (a) != NULL_TREE)
8547 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8548 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8550 if (a != NULL_TREE)
8551 for (i = 0; i < nargs; i++)
8552 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8553 i + 1);
8554 else
8556 /* Walk the argument list. If we encounter an argument number we
8557 should check for non-null, do it. */
8558 for (i = 0; i < nargs; i++)
8560 for (a = attrs; ; a = TREE_CHAIN (a))
8562 a = lookup_attribute ("nonnull", a);
8563 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8564 break;
8567 if (a != NULL_TREE)
8568 check_function_arguments_recurse (check_nonnull_arg, NULL,
8569 argarray[i], i + 1);
8574 /* Check that the Nth argument of a function call (counting backwards
8575 from the end) is a (pointer)0. The NARGS arguments are passed in the
8576 array ARGARRAY. */
8578 static void
8579 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8581 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8583 if (attr)
8585 int len = 0;
8586 int pos = 0;
8587 tree sentinel;
8588 function_args_iterator iter;
8589 tree t;
8591 /* Skip over the named arguments. */
8592 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8594 if (len == nargs)
8595 break;
8596 len++;
8599 if (TREE_VALUE (attr))
8601 tree p = TREE_VALUE (TREE_VALUE (attr));
8602 pos = TREE_INT_CST_LOW (p);
8605 /* The sentinel must be one of the varargs, i.e.
8606 in position >= the number of fixed arguments. */
8607 if ((nargs - 1 - pos) < len)
8609 warning (OPT_Wformat_,
8610 "not enough variable arguments to fit a sentinel");
8611 return;
8614 /* Validate the sentinel. */
8615 sentinel = argarray[nargs - 1 - pos];
8616 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8617 || !integer_zerop (sentinel))
8618 /* Although __null (in C++) is only an integer we allow it
8619 nevertheless, as we are guaranteed that it's exactly
8620 as wide as a pointer, and we don't want to force
8621 users to cast the NULL they have written there.
8622 We warn with -Wstrict-null-sentinel, though. */
8623 && (warn_strict_null_sentinel || null_node != sentinel))
8624 warning (OPT_Wformat_, "missing sentinel in function call");
8628 /* Helper for check_function_nonnull; given a list of operands which
8629 must be non-null in ARGS, determine if operand PARAM_NUM should be
8630 checked. */
8632 static bool
8633 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8635 unsigned HOST_WIDE_INT arg_num = 0;
8637 for (; args; args = TREE_CHAIN (args))
8639 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8641 gcc_assert (found);
8643 if (arg_num == param_num)
8644 return true;
8646 return false;
8649 /* Check that the function argument PARAM (which is operand number
8650 PARAM_NUM) is non-null. This is called by check_function_nonnull
8651 via check_function_arguments_recurse. */
8653 static void
8654 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8655 unsigned HOST_WIDE_INT param_num)
8657 /* Just skip checking the argument if it's not a pointer. This can
8658 happen if the "nonnull" attribute was given without an operand
8659 list (which means to check every pointer argument). */
8661 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8662 return;
8664 if (integer_zerop (param))
8665 warning (OPT_Wnonnull, "null argument where non-null required "
8666 "(argument %lu)", (unsigned long) param_num);
8669 /* Helper for nonnull attribute handling; fetch the operand number
8670 from the attribute argument list. */
8672 static bool
8673 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8675 /* Verify the arg number is a constant. */
8676 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8677 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8678 return false;
8680 *valp = TREE_INT_CST_LOW (arg_num_expr);
8681 return true;
8684 /* Handle a "nothrow" attribute; arguments as in
8685 struct attribute_spec.handler. */
8687 static tree
8688 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8689 int ARG_UNUSED (flags), bool *no_add_attrs)
8691 if (TREE_CODE (*node) == FUNCTION_DECL)
8692 TREE_NOTHROW (*node) = 1;
8693 /* ??? TODO: Support types. */
8694 else
8696 warning (OPT_Wattributes, "%qE attribute ignored", name);
8697 *no_add_attrs = true;
8700 return NULL_TREE;
8703 /* Handle a "cleanup" attribute; arguments as in
8704 struct attribute_spec.handler. */
8706 static tree
8707 handle_cleanup_attribute (tree *node, tree name, tree args,
8708 int ARG_UNUSED (flags), bool *no_add_attrs)
8710 tree decl = *node;
8711 tree cleanup_id, cleanup_decl;
8713 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8714 for global destructors in C++. This requires infrastructure that
8715 we don't have generically at the moment. It's also not a feature
8716 we'd be missing too much, since we do have attribute constructor. */
8717 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8719 warning (OPT_Wattributes, "%qE attribute ignored", name);
8720 *no_add_attrs = true;
8721 return NULL_TREE;
8724 /* Verify that the argument is a function in scope. */
8725 /* ??? We could support pointers to functions here as well, if
8726 that was considered desirable. */
8727 cleanup_id = TREE_VALUE (args);
8728 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8730 error ("cleanup argument not an identifier");
8731 *no_add_attrs = true;
8732 return NULL_TREE;
8734 cleanup_decl = lookup_name (cleanup_id);
8735 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8737 error ("cleanup argument not a function");
8738 *no_add_attrs = true;
8739 return NULL_TREE;
8742 /* That the function has proper type is checked with the
8743 eventual call to build_function_call. */
8745 return NULL_TREE;
8748 /* Handle a "warn_unused_result" attribute. No special handling. */
8750 static tree
8751 handle_warn_unused_result_attribute (tree *node, tree name,
8752 tree ARG_UNUSED (args),
8753 int ARG_UNUSED (flags), bool *no_add_attrs)
8755 /* Ignore the attribute for functions not returning any value. */
8756 if (VOID_TYPE_P (TREE_TYPE (*node)))
8758 warning (OPT_Wattributes, "%qE attribute ignored", name);
8759 *no_add_attrs = true;
8762 return NULL_TREE;
8765 /* Handle a "sentinel" attribute. */
8767 static tree
8768 handle_sentinel_attribute (tree *node, tree name, tree args,
8769 int ARG_UNUSED (flags), bool *no_add_attrs)
8771 if (!prototype_p (*node))
8773 warning (OPT_Wattributes,
8774 "%qE attribute requires prototypes with named arguments", name);
8775 *no_add_attrs = true;
8777 else
8779 if (!stdarg_p (*node))
8781 warning (OPT_Wattributes,
8782 "%qE attribute only applies to variadic functions", name);
8783 *no_add_attrs = true;
8787 if (args)
8789 tree position = TREE_VALUE (args);
8791 if (TREE_CODE (position) != INTEGER_CST)
8793 warning (OPT_Wattributes,
8794 "requested position is not an integer constant");
8795 *no_add_attrs = true;
8797 else
8799 if (tree_int_cst_lt (position, integer_zero_node))
8801 warning (OPT_Wattributes,
8802 "requested position is less than zero");
8803 *no_add_attrs = true;
8808 return NULL_TREE;
8811 /* Handle a "type_generic" attribute. */
8813 static tree
8814 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8815 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8816 bool * ARG_UNUSED (no_add_attrs))
8818 /* Ensure we have a function type. */
8819 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8821 /* Ensure we have a variadic function. */
8822 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8824 return NULL_TREE;
8827 /* Handle a "target" attribute. */
8829 static tree
8830 handle_target_attribute (tree *node, tree name, tree args, int flags,
8831 bool *no_add_attrs)
8833 /* Ensure we have a function type. */
8834 if (TREE_CODE (*node) != FUNCTION_DECL)
8836 warning (OPT_Wattributes, "%qE attribute ignored", name);
8837 *no_add_attrs = true;
8839 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8840 flags))
8841 *no_add_attrs = true;
8843 return NULL_TREE;
8846 /* Arguments being collected for optimization. */
8847 typedef const char *const_char_p; /* For DEF_VEC_P. */
8848 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8851 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8852 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8853 false for #pragma GCC optimize. */
8855 bool
8856 parse_optimize_options (tree args, bool attr_p)
8858 bool ret = true;
8859 unsigned opt_argc;
8860 unsigned i;
8861 int saved_flag_strict_aliasing;
8862 const char **opt_argv;
8863 struct cl_decoded_option *decoded_options;
8864 unsigned int decoded_options_count;
8865 tree ap;
8867 /* Build up argv vector. Just in case the string is stored away, use garbage
8868 collected strings. */
8869 vec_safe_truncate (optimize_args, 0);
8870 vec_safe_push (optimize_args, (const char *) NULL);
8872 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8874 tree value = TREE_VALUE (ap);
8876 if (TREE_CODE (value) == INTEGER_CST)
8878 char buffer[20];
8879 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8880 vec_safe_push (optimize_args, ggc_strdup (buffer));
8883 else if (TREE_CODE (value) == STRING_CST)
8885 /* Split string into multiple substrings. */
8886 size_t len = TREE_STRING_LENGTH (value);
8887 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8888 char *end = p + len;
8889 char *comma;
8890 char *next_p = p;
8892 while (next_p != NULL)
8894 size_t len2;
8895 char *q, *r;
8897 p = next_p;
8898 comma = strchr (p, ',');
8899 if (comma)
8901 len2 = comma - p;
8902 *comma = '\0';
8903 next_p = comma+1;
8905 else
8907 len2 = end - p;
8908 next_p = NULL;
8911 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8913 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8914 options. */
8915 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8917 ret = false;
8918 if (attr_p)
8919 warning (OPT_Wattributes,
8920 "bad option %s to optimize attribute", p);
8921 else
8922 warning (OPT_Wpragmas,
8923 "bad option %s to pragma attribute", p);
8924 continue;
8927 if (*p != '-')
8929 *r++ = '-';
8931 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8932 itself is -Os, and any other switch begins with a -f. */
8933 if ((*p >= '0' && *p <= '9')
8934 || (p[0] == 's' && p[1] == '\0'))
8935 *r++ = 'O';
8936 else if (*p != 'O')
8937 *r++ = 'f';
8940 memcpy (r, p, len2);
8941 r[len2] = '\0';
8942 vec_safe_push (optimize_args, (const char *) q);
8948 opt_argc = optimize_args->length ();
8949 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8951 for (i = 1; i < opt_argc; i++)
8952 opt_argv[i] = (*optimize_args)[i];
8954 saved_flag_strict_aliasing = flag_strict_aliasing;
8956 /* Now parse the options. */
8957 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8958 &decoded_options,
8959 &decoded_options_count);
8960 decode_options (&global_options, &global_options_set,
8961 decoded_options, decoded_options_count,
8962 input_location, global_dc);
8964 targetm.override_options_after_change();
8966 /* Don't allow changing -fstrict-aliasing. */
8967 flag_strict_aliasing = saved_flag_strict_aliasing;
8969 optimize_args->truncate (0);
8970 return ret;
8973 /* For handling "optimize" attribute. arguments as in
8974 struct attribute_spec.handler. */
8976 static tree
8977 handle_optimize_attribute (tree *node, tree name, tree args,
8978 int ARG_UNUSED (flags), bool *no_add_attrs)
8980 /* Ensure we have a function type. */
8981 if (TREE_CODE (*node) != FUNCTION_DECL)
8983 warning (OPT_Wattributes, "%qE attribute ignored", name);
8984 *no_add_attrs = true;
8986 else
8988 struct cl_optimization cur_opts;
8989 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8991 /* Save current options. */
8992 cl_optimization_save (&cur_opts, &global_options);
8994 /* If we previously had some optimization options, use them as the
8995 default. */
8996 if (old_opts)
8997 cl_optimization_restore (&global_options,
8998 TREE_OPTIMIZATION (old_opts));
9000 /* Parse options, and update the vector. */
9001 parse_optimize_options (args, true);
9002 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9003 = build_optimization_node ();
9005 /* Restore current options. */
9006 cl_optimization_restore (&global_options, &cur_opts);
9009 return NULL_TREE;
9012 /* Handle a "no_split_stack" attribute. */
9014 static tree
9015 handle_no_split_stack_attribute (tree *node, tree name,
9016 tree ARG_UNUSED (args),
9017 int ARG_UNUSED (flags),
9018 bool *no_add_attrs)
9020 tree decl = *node;
9022 if (TREE_CODE (decl) != FUNCTION_DECL)
9024 error_at (DECL_SOURCE_LOCATION (decl),
9025 "%qE attribute applies only to functions", name);
9026 *no_add_attrs = true;
9028 else if (DECL_INITIAL (decl))
9030 error_at (DECL_SOURCE_LOCATION (decl),
9031 "can%'t set %qE attribute after definition", name);
9032 *no_add_attrs = true;
9035 return NULL_TREE;
9038 /* Check for valid arguments being passed to a function with FNTYPE.
9039 There are NARGS arguments in the array ARGARRAY. */
9040 void
9041 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9043 /* Check for null being passed in a pointer argument that must be
9044 non-null. We also need to do this if format checking is enabled. */
9046 if (warn_nonnull)
9047 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9049 /* Check for errors in format strings. */
9051 if (warn_format || warn_suggest_attribute_format)
9052 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9054 if (warn_format)
9055 check_function_sentinel (fntype, nargs, argarray);
9058 /* Generic argument checking recursion routine. PARAM is the argument to
9059 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9060 once the argument is resolved. CTX is context for the callback. */
9061 void
9062 check_function_arguments_recurse (void (*callback)
9063 (void *, tree, unsigned HOST_WIDE_INT),
9064 void *ctx, tree param,
9065 unsigned HOST_WIDE_INT param_num)
9067 if (CONVERT_EXPR_P (param)
9068 && (TYPE_PRECISION (TREE_TYPE (param))
9069 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9071 /* Strip coercion. */
9072 check_function_arguments_recurse (callback, ctx,
9073 TREE_OPERAND (param, 0), param_num);
9074 return;
9077 if (TREE_CODE (param) == CALL_EXPR)
9079 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9080 tree attrs;
9081 bool found_format_arg = false;
9083 /* See if this is a call to a known internationalization function
9084 that modifies a format arg. Such a function may have multiple
9085 format_arg attributes (for example, ngettext). */
9087 for (attrs = TYPE_ATTRIBUTES (type);
9088 attrs;
9089 attrs = TREE_CHAIN (attrs))
9090 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9092 tree inner_arg;
9093 tree format_num_expr;
9094 int format_num;
9095 int i;
9096 call_expr_arg_iterator iter;
9098 /* Extract the argument number, which was previously checked
9099 to be valid. */
9100 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9102 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9103 && !TREE_INT_CST_HIGH (format_num_expr));
9105 format_num = TREE_INT_CST_LOW (format_num_expr);
9107 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9108 inner_arg != 0;
9109 inner_arg = next_call_expr_arg (&iter), i++)
9110 if (i == format_num)
9112 check_function_arguments_recurse (callback, ctx,
9113 inner_arg, param_num);
9114 found_format_arg = true;
9115 break;
9119 /* If we found a format_arg attribute and did a recursive check,
9120 we are done with checking this argument. Otherwise, we continue
9121 and this will be considered a non-literal. */
9122 if (found_format_arg)
9123 return;
9126 if (TREE_CODE (param) == COND_EXPR)
9128 /* Check both halves of the conditional expression. */
9129 check_function_arguments_recurse (callback, ctx,
9130 TREE_OPERAND (param, 1), param_num);
9131 check_function_arguments_recurse (callback, ctx,
9132 TREE_OPERAND (param, 2), param_num);
9133 return;
9136 (*callback) (ctx, param, param_num);
9139 /* Checks for a builtin function FNDECL that the number of arguments
9140 NARGS against the required number REQUIRED and issues an error if
9141 there is a mismatch. Returns true if the number of arguments is
9142 correct, otherwise false. */
9144 static bool
9145 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9147 if (nargs < required)
9149 error_at (input_location,
9150 "not enough arguments to function %qE", fndecl);
9151 return false;
9153 else if (nargs > required)
9155 error_at (input_location,
9156 "too many arguments to function %qE", fndecl);
9157 return false;
9159 return true;
9162 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9163 Returns false if there was an error, otherwise true. */
9165 bool
9166 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9168 if (!DECL_BUILT_IN (fndecl)
9169 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9170 return true;
9172 switch (DECL_FUNCTION_CODE (fndecl))
9174 case BUILT_IN_CONSTANT_P:
9175 return builtin_function_validate_nargs (fndecl, nargs, 1);
9177 case BUILT_IN_ISFINITE:
9178 case BUILT_IN_ISINF:
9179 case BUILT_IN_ISINF_SIGN:
9180 case BUILT_IN_ISNAN:
9181 case BUILT_IN_ISNORMAL:
9182 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9184 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9186 error ("non-floating-point argument in call to "
9187 "function %qE", fndecl);
9188 return false;
9190 return true;
9192 return false;
9194 case BUILT_IN_ISGREATER:
9195 case BUILT_IN_ISGREATEREQUAL:
9196 case BUILT_IN_ISLESS:
9197 case BUILT_IN_ISLESSEQUAL:
9198 case BUILT_IN_ISLESSGREATER:
9199 case BUILT_IN_ISUNORDERED:
9200 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9202 enum tree_code code0, code1;
9203 code0 = TREE_CODE (TREE_TYPE (args[0]));
9204 code1 = TREE_CODE (TREE_TYPE (args[1]));
9205 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9206 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9207 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9209 error ("non-floating-point arguments in call to "
9210 "function %qE", fndecl);
9211 return false;
9213 return true;
9215 return false;
9217 case BUILT_IN_FPCLASSIFY:
9218 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9220 unsigned i;
9222 for (i=0; i<5; i++)
9223 if (TREE_CODE (args[i]) != INTEGER_CST)
9225 error ("non-const integer argument %u in call to function %qE",
9226 i+1, fndecl);
9227 return false;
9230 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9232 error ("non-floating-point argument in call to function %qE",
9233 fndecl);
9234 return false;
9236 return true;
9238 return false;
9240 case BUILT_IN_ASSUME_ALIGNED:
9241 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9243 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9245 error ("non-integer argument 3 in call to function %qE", fndecl);
9246 return false;
9248 return true;
9250 return false;
9252 default:
9253 return true;
9257 /* Function to help qsort sort FIELD_DECLs by name order. */
9260 field_decl_cmp (const void *x_p, const void *y_p)
9262 const tree *const x = (const tree *const) x_p;
9263 const tree *const y = (const tree *const) y_p;
9265 if (DECL_NAME (*x) == DECL_NAME (*y))
9266 /* A nontype is "greater" than a type. */
9267 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9268 if (DECL_NAME (*x) == NULL_TREE)
9269 return -1;
9270 if (DECL_NAME (*y) == NULL_TREE)
9271 return 1;
9272 if (DECL_NAME (*x) < DECL_NAME (*y))
9273 return -1;
9274 return 1;
9277 static struct {
9278 gt_pointer_operator new_value;
9279 void *cookie;
9280 } resort_data;
9282 /* This routine compares two fields like field_decl_cmp but using the
9283 pointer operator in resort_data. */
9285 static int
9286 resort_field_decl_cmp (const void *x_p, const void *y_p)
9288 const tree *const x = (const tree *const) x_p;
9289 const tree *const y = (const tree *const) y_p;
9291 if (DECL_NAME (*x) == DECL_NAME (*y))
9292 /* A nontype is "greater" than a type. */
9293 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9294 if (DECL_NAME (*x) == NULL_TREE)
9295 return -1;
9296 if (DECL_NAME (*y) == NULL_TREE)
9297 return 1;
9299 tree d1 = DECL_NAME (*x);
9300 tree d2 = DECL_NAME (*y);
9301 resort_data.new_value (&d1, resort_data.cookie);
9302 resort_data.new_value (&d2, resort_data.cookie);
9303 if (d1 < d2)
9304 return -1;
9306 return 1;
9309 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9311 void
9312 resort_sorted_fields (void *obj,
9313 void * ARG_UNUSED (orig_obj),
9314 gt_pointer_operator new_value,
9315 void *cookie)
9317 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9318 resort_data.new_value = new_value;
9319 resort_data.cookie = cookie;
9320 qsort (&sf->elts[0], sf->len, sizeof (tree),
9321 resort_field_decl_cmp);
9324 /* Subroutine of c_parse_error.
9325 Return the result of concatenating LHS and RHS. RHS is really
9326 a string literal, its first character is indicated by RHS_START and
9327 RHS_SIZE is its length (including the terminating NUL character).
9329 The caller is responsible for deleting the returned pointer. */
9331 static char *
9332 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9334 const int lhs_size = strlen (lhs);
9335 char *result = XNEWVEC (char, lhs_size + rhs_size);
9336 strncpy (result, lhs, lhs_size);
9337 strncpy (result + lhs_size, rhs_start, rhs_size);
9338 return result;
9341 /* Issue the error given by GMSGID, indicating that it occurred before
9342 TOKEN, which had the associated VALUE. */
9344 void
9345 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9346 tree value, unsigned char token_flags)
9348 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9350 char *message = NULL;
9352 if (token_type == CPP_EOF)
9353 message = catenate_messages (gmsgid, " at end of input");
9354 else if (token_type == CPP_CHAR
9355 || token_type == CPP_WCHAR
9356 || token_type == CPP_CHAR16
9357 || token_type == CPP_CHAR32)
9359 unsigned int val = TREE_INT_CST_LOW (value);
9360 const char *prefix;
9362 switch (token_type)
9364 default:
9365 prefix = "";
9366 break;
9367 case CPP_WCHAR:
9368 prefix = "L";
9369 break;
9370 case CPP_CHAR16:
9371 prefix = "u";
9372 break;
9373 case CPP_CHAR32:
9374 prefix = "U";
9375 break;
9378 if (val <= UCHAR_MAX && ISGRAPH (val))
9379 message = catenate_messages (gmsgid, " before %s'%c'");
9380 else
9381 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9383 error (message, prefix, val);
9384 free (message);
9385 message = NULL;
9387 else if (token_type == CPP_STRING
9388 || token_type == CPP_WSTRING
9389 || token_type == CPP_STRING16
9390 || token_type == CPP_STRING32
9391 || token_type == CPP_UTF8STRING)
9392 message = catenate_messages (gmsgid, " before string constant");
9393 else if (token_type == CPP_NUMBER)
9394 message = catenate_messages (gmsgid, " before numeric constant");
9395 else if (token_type == CPP_NAME)
9397 message = catenate_messages (gmsgid, " before %qE");
9398 error (message, value);
9399 free (message);
9400 message = NULL;
9402 else if (token_type == CPP_PRAGMA)
9403 message = catenate_messages (gmsgid, " before %<#pragma%>");
9404 else if (token_type == CPP_PRAGMA_EOL)
9405 message = catenate_messages (gmsgid, " before end of line");
9406 else if (token_type == CPP_DECLTYPE)
9407 message = catenate_messages (gmsgid, " before %<decltype%>");
9408 else if (token_type < N_TTYPES)
9410 message = catenate_messages (gmsgid, " before %qs token");
9411 error (message, cpp_type2name (token_type, token_flags));
9412 free (message);
9413 message = NULL;
9415 else
9416 error (gmsgid);
9418 if (message)
9420 error (message);
9421 free (message);
9423 #undef catenate_messages
9426 /* Mapping for cpp message reasons to the options that enable them. */
9428 struct reason_option_codes_t
9430 const int reason; /* cpplib message reason. */
9431 const int option_code; /* gcc option that controls this message. */
9434 static const struct reason_option_codes_t option_codes[] = {
9435 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9436 {CPP_W_COMMENTS, OPT_Wcomment},
9437 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9438 {CPP_W_MULTICHAR, OPT_Wmultichar},
9439 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9440 {CPP_W_LONG_LONG, OPT_Wlong_long},
9441 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9442 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9443 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9444 {CPP_W_UNDEF, OPT_Wundef},
9445 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9446 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9447 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9448 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9449 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9450 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9451 {CPP_W_NONE, 0}
9454 /* Return the gcc option code associated with the reason for a cpp
9455 message, or 0 if none. */
9457 static int
9458 c_option_controlling_cpp_error (int reason)
9460 const struct reason_option_codes_t *entry;
9462 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9464 if (entry->reason == reason)
9465 return entry->option_code;
9467 return 0;
9470 /* Callback from cpp_error for PFILE to print diagnostics from the
9471 preprocessor. The diagnostic is of type LEVEL, with REASON set
9472 to the reason code if LEVEL is represents a warning, at location
9473 LOCATION unless this is after lexing and the compiler's location
9474 should be used instead, with column number possibly overridden by
9475 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9476 the arguments. Returns true if a diagnostic was emitted, false
9477 otherwise. */
9479 bool
9480 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9481 location_t location, unsigned int column_override,
9482 const char *msg, va_list *ap)
9484 diagnostic_info diagnostic;
9485 diagnostic_t dlevel;
9486 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9487 bool ret;
9489 switch (level)
9491 case CPP_DL_WARNING_SYSHDR:
9492 if (flag_no_output)
9493 return false;
9494 global_dc->dc_warn_system_headers = 1;
9495 /* Fall through. */
9496 case CPP_DL_WARNING:
9497 if (flag_no_output)
9498 return false;
9499 dlevel = DK_WARNING;
9500 break;
9501 case CPP_DL_PEDWARN:
9502 if (flag_no_output && !flag_pedantic_errors)
9503 return false;
9504 dlevel = DK_PEDWARN;
9505 break;
9506 case CPP_DL_ERROR:
9507 dlevel = DK_ERROR;
9508 break;
9509 case CPP_DL_ICE:
9510 dlevel = DK_ICE;
9511 break;
9512 case CPP_DL_NOTE:
9513 dlevel = DK_NOTE;
9514 break;
9515 case CPP_DL_FATAL:
9516 dlevel = DK_FATAL;
9517 break;
9518 default:
9519 gcc_unreachable ();
9521 if (done_lexing)
9522 location = input_location;
9523 diagnostic_set_info_translated (&diagnostic, msg, ap,
9524 location, dlevel);
9525 if (column_override)
9526 diagnostic_override_column (&diagnostic, column_override);
9527 diagnostic_override_option_index (&diagnostic,
9528 c_option_controlling_cpp_error (reason));
9529 ret = report_diagnostic (&diagnostic);
9530 if (level == CPP_DL_WARNING_SYSHDR)
9531 global_dc->dc_warn_system_headers = save_warn_system_headers;
9532 return ret;
9535 /* Convert a character from the host to the target execution character
9536 set. cpplib handles this, mostly. */
9538 HOST_WIDE_INT
9539 c_common_to_target_charset (HOST_WIDE_INT c)
9541 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9542 zero-extended under -fno-signed-char. cpplib insists that characters
9543 and character constants are always unsigned. Hence we must convert
9544 back and forth. */
9545 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9547 uc = cpp_host_to_exec_charset (parse_in, uc);
9549 if (flag_signed_char)
9550 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9551 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9552 else
9553 return uc;
9556 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9557 references with an INDIRECT_REF of a constant at the bottom; much like the
9558 traditional rendering of offsetof as a macro. Return the folded result. */
9560 tree
9561 fold_offsetof_1 (tree expr)
9563 tree base, off, t;
9565 switch (TREE_CODE (expr))
9567 case ERROR_MARK:
9568 return expr;
9570 case VAR_DECL:
9571 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9572 return error_mark_node;
9574 case CALL_EXPR:
9575 case TARGET_EXPR:
9576 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9577 return error_mark_node;
9579 case NOP_EXPR:
9580 case INDIRECT_REF:
9581 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9583 error ("cannot apply %<offsetof%> to a non constant address");
9584 return error_mark_node;
9586 return TREE_OPERAND (expr, 0);
9588 case COMPONENT_REF:
9589 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9590 if (base == error_mark_node)
9591 return base;
9593 t = TREE_OPERAND (expr, 1);
9594 if (DECL_C_BIT_FIELD (t))
9596 error ("attempt to take address of bit-field structure "
9597 "member %qD", t);
9598 return error_mark_node;
9600 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9601 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9603 / BITS_PER_UNIT));
9604 break;
9606 case ARRAY_REF:
9607 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9608 if (base == error_mark_node)
9609 return base;
9611 t = TREE_OPERAND (expr, 1);
9613 /* Check if the offset goes beyond the upper bound of the array. */
9614 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9616 tree upbound = array_ref_up_bound (expr);
9617 if (upbound != NULL_TREE
9618 && TREE_CODE (upbound) == INTEGER_CST
9619 && !tree_int_cst_equal (upbound,
9620 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9622 upbound = size_binop (PLUS_EXPR, upbound,
9623 build_int_cst (TREE_TYPE (upbound), 1));
9624 if (tree_int_cst_lt (upbound, t))
9626 tree v;
9628 for (v = TREE_OPERAND (expr, 0);
9629 TREE_CODE (v) == COMPONENT_REF;
9630 v = TREE_OPERAND (v, 0))
9631 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9632 == RECORD_TYPE)
9634 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9635 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9636 if (TREE_CODE (fld_chain) == FIELD_DECL)
9637 break;
9639 if (fld_chain)
9640 break;
9642 /* Don't warn if the array might be considered a poor
9643 man's flexible array member with a very permissive
9644 definition thereof. */
9645 if (TREE_CODE (v) == ARRAY_REF
9646 || TREE_CODE (v) == COMPONENT_REF)
9647 warning (OPT_Warray_bounds,
9648 "index %E denotes an offset "
9649 "greater than size of %qT",
9650 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9655 t = convert (sizetype, t);
9656 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9657 break;
9659 case COMPOUND_EXPR:
9660 /* Handle static members of volatile structs. */
9661 t = TREE_OPERAND (expr, 1);
9662 gcc_assert (TREE_CODE (t) == VAR_DECL);
9663 return fold_offsetof_1 (t);
9665 default:
9666 gcc_unreachable ();
9669 return fold_build_pointer_plus (base, off);
9672 /* Likewise, but convert it to the return type of offsetof. */
9674 tree
9675 fold_offsetof (tree expr)
9677 return convert (size_type_node, fold_offsetof_1 (expr));
9680 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9681 expression, because B will always be true. */
9683 void
9684 warn_for_omitted_condop (location_t location, tree cond)
9686 if (truth_value_p (TREE_CODE (cond)))
9687 warning_at (location, OPT_Wparentheses,
9688 "the omitted middle operand in ?: will always be %<true%>, "
9689 "suggest explicit middle operand");
9692 /* Give an error for storing into ARG, which is 'const'. USE indicates
9693 how ARG was being used. */
9695 void
9696 readonly_error (tree arg, enum lvalue_use use)
9698 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9699 || use == lv_asm);
9700 /* Using this macro rather than (for example) arrays of messages
9701 ensures that all the format strings are checked at compile
9702 time. */
9703 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9704 : (use == lv_increment ? (I) \
9705 : (use == lv_decrement ? (D) : (AS))))
9706 if (TREE_CODE (arg) == COMPONENT_REF)
9708 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9709 error (READONLY_MSG (G_("assignment of member "
9710 "%qD in read-only object"),
9711 G_("increment of member "
9712 "%qD in read-only object"),
9713 G_("decrement of member "
9714 "%qD in read-only object"),
9715 G_("member %qD in read-only object "
9716 "used as %<asm%> output")),
9717 TREE_OPERAND (arg, 1));
9718 else
9719 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9720 G_("increment of read-only member %qD"),
9721 G_("decrement of read-only member %qD"),
9722 G_("read-only member %qD used as %<asm%> output")),
9723 TREE_OPERAND (arg, 1));
9725 else if (TREE_CODE (arg) == VAR_DECL)
9726 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9727 G_("increment of read-only variable %qD"),
9728 G_("decrement of read-only variable %qD"),
9729 G_("read-only variable %qD used as %<asm%> output")),
9730 arg);
9731 else if (TREE_CODE (arg) == PARM_DECL)
9732 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9733 G_("increment of read-only parameter %qD"),
9734 G_("decrement of read-only parameter %qD"),
9735 G_("read-only parameter %qD use as %<asm%> output")),
9736 arg);
9737 else if (TREE_CODE (arg) == RESULT_DECL)
9739 gcc_assert (c_dialect_cxx ());
9740 error (READONLY_MSG (G_("assignment of "
9741 "read-only named return value %qD"),
9742 G_("increment of "
9743 "read-only named return value %qD"),
9744 G_("decrement of "
9745 "read-only named return value %qD"),
9746 G_("read-only named return value %qD "
9747 "used as %<asm%>output")),
9748 arg);
9750 else if (TREE_CODE (arg) == FUNCTION_DECL)
9751 error (READONLY_MSG (G_("assignment of function %qD"),
9752 G_("increment of function %qD"),
9753 G_("decrement of function %qD"),
9754 G_("function %qD used as %<asm%> output")),
9755 arg);
9756 else
9757 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9758 G_("increment of read-only location %qE"),
9759 G_("decrement of read-only location %qE"),
9760 G_("read-only location %qE used as %<asm%> output")),
9761 arg);
9764 /* Print an error message for an invalid lvalue. USE says
9765 how the lvalue is being used and so selects the error message. LOC
9766 is the location for the error. */
9768 void
9769 lvalue_error (location_t loc, enum lvalue_use use)
9771 switch (use)
9773 case lv_assign:
9774 error_at (loc, "lvalue required as left operand of assignment");
9775 break;
9776 case lv_increment:
9777 error_at (loc, "lvalue required as increment operand");
9778 break;
9779 case lv_decrement:
9780 error_at (loc, "lvalue required as decrement operand");
9781 break;
9782 case lv_addressof:
9783 error_at (loc, "lvalue required as unary %<&%> operand");
9784 break;
9785 case lv_asm:
9786 error_at (loc, "lvalue required in asm statement");
9787 break;
9788 default:
9789 gcc_unreachable ();
9793 /* Print an error message for an invalid indirection of type TYPE.
9794 ERRSTRING is the name of the operator for the indirection. */
9796 void
9797 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9799 switch (errstring)
9801 case RO_NULL:
9802 gcc_assert (c_dialect_cxx ());
9803 error_at (loc, "invalid type argument (have %qT)", type);
9804 break;
9805 case RO_ARRAY_INDEXING:
9806 error_at (loc,
9807 "invalid type argument of array indexing (have %qT)",
9808 type);
9809 break;
9810 case RO_UNARY_STAR:
9811 error_at (loc,
9812 "invalid type argument of unary %<*%> (have %qT)",
9813 type);
9814 break;
9815 case RO_ARROW:
9816 error_at (loc,
9817 "invalid type argument of %<->%> (have %qT)",
9818 type);
9819 break;
9820 case RO_IMPLICIT_CONVERSION:
9821 error_at (loc,
9822 "invalid type argument of implicit conversion (have %qT)",
9823 type);
9824 break;
9825 default:
9826 gcc_unreachable ();
9830 /* *PTYPE is an incomplete array. Complete it with a domain based on
9831 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9832 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9833 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9836 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9838 tree maxindex, type, main_type, elt, unqual_elt;
9839 int failure = 0, quals;
9840 hashval_t hashcode = 0;
9842 maxindex = size_zero_node;
9843 if (initial_value)
9845 if (TREE_CODE (initial_value) == STRING_CST)
9847 int eltsize
9848 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9849 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9851 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9853 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9855 if (vec_safe_is_empty (v))
9857 if (pedantic)
9858 failure = 3;
9859 maxindex = ssize_int (-1);
9861 else
9863 tree curindex;
9864 unsigned HOST_WIDE_INT cnt;
9865 constructor_elt *ce;
9866 bool fold_p = false;
9868 if ((*v)[0].index)
9869 maxindex = fold_convert_loc (input_location, sizetype,
9870 (*v)[0].index);
9871 curindex = maxindex;
9873 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
9875 bool curfold_p = false;
9876 if (ce->index)
9877 curindex = ce->index, curfold_p = true;
9878 else
9880 if (fold_p)
9881 curindex = fold_convert (sizetype, curindex);
9882 curindex = size_binop (PLUS_EXPR, curindex,
9883 size_one_node);
9885 if (tree_int_cst_lt (maxindex, curindex))
9886 maxindex = curindex, fold_p = curfold_p;
9888 if (fold_p)
9889 maxindex = fold_convert (sizetype, maxindex);
9892 else
9894 /* Make an error message unless that happened already. */
9895 if (initial_value != error_mark_node)
9896 failure = 1;
9899 else
9901 failure = 2;
9902 if (!do_default)
9903 return failure;
9906 type = *ptype;
9907 /* Force an indefinite layout factor. */
9908 if (upc_shared_type_p (type))
9909 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
9910 elt = TREE_TYPE (type);
9911 quals = TYPE_QUALS (strip_array_types (elt));
9912 if (quals == 0)
9913 unqual_elt = elt;
9914 else
9915 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9917 /* Using build_distinct_type_copy and modifying things afterward instead
9918 of using build_array_type to create a new type preserves all of the
9919 TYPE_LANG_FLAG_? bits that the front end may have set. */
9920 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9921 TREE_TYPE (main_type) = unqual_elt;
9922 TYPE_DOMAIN (main_type)
9923 = build_range_type (TREE_TYPE (maxindex),
9924 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9925 layout_type (main_type);
9927 /* Make sure we have the canonical MAIN_TYPE. */
9928 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9929 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9930 hashcode);
9931 main_type = type_hash_canon (hashcode, main_type);
9933 /* Fix the canonical type. */
9934 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9935 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9936 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9937 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9938 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9939 != TYPE_DOMAIN (main_type)))
9940 TYPE_CANONICAL (main_type)
9941 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9942 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9943 else
9944 TYPE_CANONICAL (main_type) = main_type;
9946 if (quals == 0)
9947 type = main_type;
9948 else
9949 type = c_build_qualified_type (main_type, quals);
9951 if (COMPLETE_TYPE_P (type)
9952 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9953 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9955 error ("size of array is too large");
9956 /* If we proceed with the array type as it is, we'll eventually
9957 crash in tree_low_cst(). */
9958 type = error_mark_node;
9961 *ptype = type;
9962 return failure;
9965 /* Like c_mark_addressable but don't check register qualifier. */
9966 void
9967 c_common_mark_addressable_vec (tree t)
9969 while (handled_component_p (t))
9970 t = TREE_OPERAND (t, 0);
9971 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9972 return;
9973 TREE_ADDRESSABLE (t) = 1;
9978 /* Used to help initialize the builtin-types.def table. When a type of
9979 the correct size doesn't exist, use error_mark_node instead of NULL.
9980 The later results in segfaults even when a decl using the type doesn't
9981 get invoked. */
9983 tree
9984 builtin_type_for_size (int size, bool unsignedp)
9986 tree type = c_common_type_for_size (size, unsignedp);
9987 return type ? type : error_mark_node;
9990 /* A helper function for resolve_overloaded_builtin in resolving the
9991 overloaded __sync_ builtins. Returns a positive power of 2 if the
9992 first operand of PARAMS is a pointer to a supported data type.
9993 Returns 0 if an error is encountered. */
9995 static int
9996 sync_resolve_size (tree function, vec<tree, va_gc> *params)
9998 tree type;
9999 int size;
10001 if (!params)
10003 error ("too few arguments to function %qE", function);
10004 return 0;
10007 type = TREE_TYPE ((*params)[0]);
10008 if (TREE_CODE (type) != POINTER_TYPE)
10009 goto incompatible;
10011 type = TREE_TYPE (type);
10012 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10013 goto incompatible;
10015 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10016 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10017 return size;
10019 incompatible:
10020 error ("incompatible type for argument %d of %qE", 1, function);
10021 return 0;
10024 /* A helper function for resolve_overloaded_builtin. Adds casts to
10025 PARAMS to make arguments match up with those of FUNCTION. Drops
10026 the variadic arguments at the end. Returns false if some error
10027 was encountered; true on success. */
10029 static bool
10030 sync_resolve_params (location_t loc, tree orig_function, tree function,
10031 vec<tree, va_gc> *params, bool orig_format)
10033 function_args_iterator iter;
10034 tree ptype;
10035 unsigned int parmnum;
10037 function_args_iter_init (&iter, TREE_TYPE (function));
10038 /* We've declared the implementation functions to use "volatile void *"
10039 as the pointer parameter, so we shouldn't get any complaints from the
10040 call to check_function_arguments what ever type the user used. */
10041 function_args_iter_next (&iter);
10042 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10044 /* For the rest of the values, we need to cast these to FTYPE, so that we
10045 don't get warnings for passing pointer types, etc. */
10046 parmnum = 0;
10047 while (1)
10049 tree val, arg_type;
10051 arg_type = function_args_iter_cond (&iter);
10052 /* XXX void_type_node belies the abstraction. */
10053 if (arg_type == void_type_node)
10054 break;
10056 ++parmnum;
10057 if (params->length () <= parmnum)
10059 error_at (loc, "too few arguments to function %qE", orig_function);
10060 return false;
10063 /* Only convert parameters if arg_type is unsigned integer type with
10064 new format sync routines, i.e. don't attempt to convert pointer
10065 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10066 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10067 kinds). */
10068 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10070 /* Ideally for the first conversion we'd use convert_for_assignment
10071 so that we get warnings for anything that doesn't match the pointer
10072 type. This isn't portable across the C and C++ front ends atm. */
10073 val = (*params)[parmnum];
10074 val = convert (ptype, val);
10075 val = convert (arg_type, val);
10076 (*params)[parmnum] = val;
10079 function_args_iter_next (&iter);
10082 /* __atomic routines are not variadic. */
10083 if (!orig_format && params->length () != parmnum + 1)
10085 error_at (loc, "too many arguments to function %qE", orig_function);
10086 return false;
10089 /* The definition of these primitives is variadic, with the remaining
10090 being "an optional list of variables protected by the memory barrier".
10091 No clue what that's supposed to mean, precisely, but we consider all
10092 call-clobbered variables to be protected so we're safe. */
10093 params->truncate (parmnum + 1);
10095 return true;
10098 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10099 RESULT to make it match the type of the first pointer argument in
10100 PARAMS. */
10102 static tree
10103 sync_resolve_return (tree first_param, tree result, bool orig_format)
10105 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10106 tree rtype = TREE_TYPE (result);
10107 ptype = TYPE_MAIN_VARIANT (ptype);
10109 /* New format doesn't require casting unless the types are the same size. */
10110 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10111 return convert (ptype, result);
10112 else
10113 return result;
10116 /* This function verifies the PARAMS to generic atomic FUNCTION.
10117 It returns the size if all the parameters are the same size, otherwise
10118 0 is returned if the parameters are invalid. */
10120 static int
10121 get_atomic_generic_size (location_t loc, tree function,
10122 vec<tree, va_gc> *params)
10124 unsigned int n_param;
10125 unsigned int n_model;
10126 unsigned int x;
10127 int size_0;
10128 tree type_0;
10130 /* Determine the parameter makeup. */
10131 switch (DECL_FUNCTION_CODE (function))
10133 case BUILT_IN_ATOMIC_EXCHANGE:
10134 n_param = 4;
10135 n_model = 1;
10136 break;
10137 case BUILT_IN_ATOMIC_LOAD:
10138 case BUILT_IN_ATOMIC_STORE:
10139 n_param = 3;
10140 n_model = 1;
10141 break;
10142 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10143 n_param = 6;
10144 n_model = 2;
10145 break;
10146 default:
10147 gcc_unreachable ();
10150 if (vec_safe_length (params) != n_param)
10152 error_at (loc, "incorrect number of arguments to function %qE", function);
10153 return 0;
10156 /* Get type of first parameter, and determine its size. */
10157 type_0 = TREE_TYPE ((*params)[0]);
10158 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10160 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10161 function);
10162 return 0;
10165 /* Types must be compile time constant sizes. */
10166 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10168 error_at (loc,
10169 "argument 1 of %qE must be a pointer to a constant size type",
10170 function);
10171 return 0;
10174 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10176 /* Zero size objects are not allowed. */
10177 if (size_0 == 0)
10179 error_at (loc,
10180 "argument 1 of %qE must be a pointer to a nonzero size object",
10181 function);
10182 return 0;
10185 /* Check each other parameter is a pointer and the same size. */
10186 for (x = 0; x < n_param - n_model; x++)
10188 int size;
10189 tree type = TREE_TYPE ((*params)[x]);
10190 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
10191 if (n_param == 6 && x == 3)
10192 continue;
10193 if (!POINTER_TYPE_P (type))
10195 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10196 function);
10197 return 0;
10199 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10200 if (size != size_0)
10202 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10203 function);
10204 return 0;
10208 /* Check memory model parameters for validity. */
10209 for (x = n_param - n_model ; x < n_param; x++)
10211 tree p = (*params)[x];
10212 if (TREE_CODE (p) == INTEGER_CST)
10214 int i = tree_low_cst (p, 1);
10215 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10217 warning_at (loc, OPT_Winvalid_memory_model,
10218 "invalid memory model argument %d of %qE", x + 1,
10219 function);
10222 else
10223 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10225 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10226 function);
10227 return 0;
10231 return size_0;
10235 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10236 at the beginning of the parameter list PARAMS representing the size of the
10237 objects. This is to match the library ABI requirement. LOC is the location
10238 of the function call.
10239 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10240 returned to allow the external call to be constructed. */
10242 static tree
10243 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10244 vec<tree, va_gc> *params)
10246 tree size_node;
10248 /* Insert a SIZE_T parameter as the first param. If there isn't
10249 enough space, allocate a new vector and recursively re-build with that. */
10250 if (!params->space (1))
10252 unsigned int z, len;
10253 vec<tree, va_gc> *v;
10254 tree f;
10256 len = params->length ();
10257 vec_alloc (v, len + 1);
10258 for (z = 0; z < len; z++)
10259 v->quick_push ((*params)[z]);
10260 f = build_function_call_vec (loc, function, v, NULL);
10261 vec_free (v);
10262 return f;
10265 /* Add the size parameter and leave as a function call for processing. */
10266 size_node = build_int_cst (size_type_node, n);
10267 params->quick_insert (0, size_node);
10268 return NULL_TREE;
10272 /* This will process an __atomic_exchange function call, determine whether it
10273 needs to be mapped to the _N variation, or turned into a library call.
10274 LOC is the location of the builtin call.
10275 FUNCTION is the DECL that has been invoked;
10276 PARAMS is the argument list for the call. The return value is non-null
10277 TRUE is returned if it is translated into the proper format for a call to the
10278 external library, and NEW_RETURN is set the tree for that function.
10279 FALSE is returned if processing for the _N variation is required, and
10280 NEW_RETURN is set to the the return value the result is copied into. */
10281 static bool
10282 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10283 vec<tree, va_gc> *params, tree *new_return)
10285 tree p0, p1, p2, p3;
10286 tree I_type, I_type_ptr;
10287 int n = get_atomic_generic_size (loc, function, params);
10289 /* Size of 0 is an error condition. */
10290 if (n == 0)
10292 *new_return = error_mark_node;
10293 return true;
10296 /* If not a lock-free size, change to the library generic format. */
10297 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10299 *new_return = add_atomic_size_parameter (n, loc, function, params);
10300 return true;
10303 /* Otherwise there is a lockfree match, transform the call from:
10304 void fn(T* mem, T* desired, T* return, model)
10305 into
10306 *return = (T) (fn (In* mem, (In) *desired, model)) */
10308 p0 = (*params)[0];
10309 p1 = (*params)[1];
10310 p2 = (*params)[2];
10311 p3 = (*params)[3];
10313 /* Create pointer to appropriate size. */
10314 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10315 I_type_ptr = build_pointer_type (I_type);
10317 /* Convert object pointer to required type. */
10318 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10319 (*params)[0] = p0;
10320 /* Convert new value to required type, and dereference it. */
10321 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10322 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10323 (*params)[1] = p1;
10325 /* Move memory model to the 3rd position, and end param list. */
10326 (*params)[2] = p3;
10327 params->truncate (3);
10329 /* Convert return pointer and dereference it for later assignment. */
10330 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10332 return false;
10336 /* This will process an __atomic_compare_exchange function call, determine
10337 whether it needs to be mapped to the _N variation, or turned into a lib call.
10338 LOC is the location of the builtin call.
10339 FUNCTION is the DECL that has been invoked;
10340 PARAMS is the argument list for the call. The return value is non-null
10341 TRUE is returned if it is translated into the proper format for a call to the
10342 external library, and NEW_RETURN is set the tree for that function.
10343 FALSE is returned if processing for the _N variation is required. */
10345 static bool
10346 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10347 vec<tree, va_gc> *params,
10348 tree *new_return)
10350 tree p0, p1, p2;
10351 tree I_type, I_type_ptr;
10352 int n = get_atomic_generic_size (loc, function, params);
10354 /* Size of 0 is an error condition. */
10355 if (n == 0)
10357 *new_return = error_mark_node;
10358 return true;
10361 /* If not a lock-free size, change to the library generic format. */
10362 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10364 /* The library generic format does not have the weak parameter, so
10365 remove it from the param list. Since a parameter has been removed,
10366 we can be sure that there is room for the SIZE_T parameter, meaning
10367 there will not be a recursive rebuilding of the parameter list, so
10368 there is no danger this will be done twice. */
10369 if (n > 0)
10371 (*params)[3] = (*params)[4];
10372 (*params)[4] = (*params)[5];
10373 params->truncate (5);
10375 *new_return = add_atomic_size_parameter (n, loc, function, params);
10376 return true;
10379 /* Otherwise, there is a match, so the call needs to be transformed from:
10380 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10381 into
10382 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10384 p0 = (*params)[0];
10385 p1 = (*params)[1];
10386 p2 = (*params)[2];
10388 /* Create pointer to appropriate size. */
10389 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10390 I_type_ptr = build_pointer_type (I_type);
10392 /* Convert object pointer to required type. */
10393 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10394 (*params)[0] = p0;
10396 /* Convert expected pointer to required type. */
10397 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10398 (*params)[1] = p1;
10400 /* Convert desired value to required type, and dereference it. */
10401 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10402 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10403 (*params)[2] = p2;
10405 /* The rest of the parameters are fine. NULL means no special return value
10406 processing.*/
10407 *new_return = NULL;
10408 return false;
10412 /* This will process an __atomic_load function call, determine whether it
10413 needs to be mapped to the _N variation, or turned into a library call.
10414 LOC is the location of the builtin call.
10415 FUNCTION is the DECL that has been invoked;
10416 PARAMS is the argument list for the call. The return value is non-null
10417 TRUE is returned if it is translated into the proper format for a call to the
10418 external library, and NEW_RETURN is set the tree for that function.
10419 FALSE is returned if processing for the _N variation is required, and
10420 NEW_RETURN is set to the the return value the result is copied into. */
10422 static bool
10423 resolve_overloaded_atomic_load (location_t loc, tree function,
10424 vec<tree, va_gc> *params, tree *new_return)
10426 tree p0, p1, p2;
10427 tree I_type, I_type_ptr;
10428 int n = get_atomic_generic_size (loc, function, params);
10430 /* Size of 0 is an error condition. */
10431 if (n == 0)
10433 *new_return = error_mark_node;
10434 return true;
10437 /* If not a lock-free size, change to the library generic format. */
10438 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10440 *new_return = add_atomic_size_parameter (n, loc, function, params);
10441 return true;
10444 /* Otherwise, there is a match, so the call needs to be transformed from:
10445 void fn(T* mem, T* return, model)
10446 into
10447 *return = (T) (fn ((In *) mem, model)) */
10449 p0 = (*params)[0];
10450 p1 = (*params)[1];
10451 p2 = (*params)[2];
10453 /* Create pointer to appropriate size. */
10454 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10455 I_type_ptr = build_pointer_type (I_type);
10457 /* Convert object pointer to required type. */
10458 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10459 (*params)[0] = p0;
10461 /* Move memory model to the 2nd position, and end param list. */
10462 (*params)[1] = p2;
10463 params->truncate (2);
10465 /* Convert return pointer and dereference it for later assignment. */
10466 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10468 return false;
10472 /* This will process an __atomic_store function call, determine whether it
10473 needs to be mapped to the _N variation, or turned into a library call.
10474 LOC is the location of the builtin call.
10475 FUNCTION is the DECL that has been invoked;
10476 PARAMS is the argument list for the call. The return value is non-null
10477 TRUE is returned if it is translated into the proper format for a call to the
10478 external library, and NEW_RETURN is set the tree for that function.
10479 FALSE is returned if processing for the _N variation is required, and
10480 NEW_RETURN is set to the the return value the result is copied into. */
10482 static bool
10483 resolve_overloaded_atomic_store (location_t loc, tree function,
10484 vec<tree, va_gc> *params, tree *new_return)
10486 tree p0, p1;
10487 tree I_type, I_type_ptr;
10488 int n = get_atomic_generic_size (loc, function, params);
10490 /* Size of 0 is an error condition. */
10491 if (n == 0)
10493 *new_return = error_mark_node;
10494 return true;
10497 /* If not a lock-free size, change to the library generic format. */
10498 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10500 *new_return = add_atomic_size_parameter (n, loc, function, params);
10501 return true;
10504 /* Otherwise, there is a match, so the call needs to be transformed from:
10505 void fn(T* mem, T* value, model)
10506 into
10507 fn ((In *) mem, (In) *value, model) */
10509 p0 = (*params)[0];
10510 p1 = (*params)[1];
10512 /* Create pointer to appropriate size. */
10513 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10514 I_type_ptr = build_pointer_type (I_type);
10516 /* Convert object pointer to required type. */
10517 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10518 (*params)[0] = p0;
10520 /* Convert new value to required type, and dereference it. */
10521 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10522 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10523 (*params)[1] = p1;
10525 /* The memory model is in the right spot already. Return is void. */
10526 *new_return = NULL_TREE;
10528 return false;
10532 /* Some builtin functions are placeholders for other expressions. This
10533 function should be called immediately after parsing the call expression
10534 before surrounding code has committed to the type of the expression.
10536 LOC is the location of the builtin call.
10538 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10539 PARAMS is the argument list for the call. The return value is non-null
10540 when expansion is complete, and null if normal processing should
10541 continue. */
10543 tree
10544 resolve_overloaded_builtin (location_t loc, tree function,
10545 vec<tree, va_gc> *params)
10547 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10548 bool orig_format = true;
10549 tree new_return = NULL_TREE;
10551 switch (DECL_BUILT_IN_CLASS (function))
10553 case BUILT_IN_NORMAL:
10554 break;
10555 case BUILT_IN_MD:
10556 if (targetm.resolve_overloaded_builtin)
10557 return targetm.resolve_overloaded_builtin (loc, function, params);
10558 else
10559 return NULL_TREE;
10560 default:
10561 return NULL_TREE;
10564 /* Handle BUILT_IN_NORMAL here. */
10565 switch (orig_code)
10567 case BUILT_IN_ATOMIC_EXCHANGE:
10568 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10569 case BUILT_IN_ATOMIC_LOAD:
10570 case BUILT_IN_ATOMIC_STORE:
10572 /* Handle these 4 together so that they can fall through to the next
10573 case if the call is transformed to an _N variant. */
10574 switch (orig_code)
10576 case BUILT_IN_ATOMIC_EXCHANGE:
10578 if (resolve_overloaded_atomic_exchange (loc, function, params,
10579 &new_return))
10580 return new_return;
10581 /* Change to the _N variant. */
10582 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10583 break;
10586 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10588 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10589 params,
10590 &new_return))
10591 return new_return;
10592 /* Change to the _N variant. */
10593 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10594 break;
10596 case BUILT_IN_ATOMIC_LOAD:
10598 if (resolve_overloaded_atomic_load (loc, function, params,
10599 &new_return))
10600 return new_return;
10601 /* Change to the _N variant. */
10602 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10603 break;
10605 case BUILT_IN_ATOMIC_STORE:
10607 if (resolve_overloaded_atomic_store (loc, function, params,
10608 &new_return))
10609 return new_return;
10610 /* Change to the _N variant. */
10611 orig_code = BUILT_IN_ATOMIC_STORE_N;
10612 break;
10614 default:
10615 gcc_unreachable ();
10617 /* Fallthrough to the normal processing. */
10619 case BUILT_IN_ATOMIC_EXCHANGE_N:
10620 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10621 case BUILT_IN_ATOMIC_LOAD_N:
10622 case BUILT_IN_ATOMIC_STORE_N:
10623 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10624 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10625 case BUILT_IN_ATOMIC_AND_FETCH_N:
10626 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10627 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10628 case BUILT_IN_ATOMIC_OR_FETCH_N:
10629 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10630 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10631 case BUILT_IN_ATOMIC_FETCH_AND_N:
10632 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10633 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10634 case BUILT_IN_ATOMIC_FETCH_OR_N:
10636 orig_format = false;
10637 /* Fallthru for parameter processing. */
10639 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10640 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10641 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10642 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10643 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10644 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10645 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10646 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10647 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10648 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10649 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10650 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10651 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10652 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10653 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10654 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10656 int n = sync_resolve_size (function, params);
10657 tree new_function, first_param, result;
10658 enum built_in_function fncode;
10660 if (n == 0)
10661 return error_mark_node;
10663 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10664 new_function = builtin_decl_explicit (fncode);
10665 if (!sync_resolve_params (loc, function, new_function, params,
10666 orig_format))
10667 return error_mark_node;
10669 first_param = (*params)[0];
10670 result = build_function_call_vec (loc, new_function, params, NULL);
10671 if (result == error_mark_node)
10672 return result;
10673 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10674 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10675 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10676 result = sync_resolve_return (first_param, result, orig_format);
10678 /* If new_return is set, assign function to that expr and cast the
10679 result to void since the generic interface returned void. */
10680 if (new_return)
10682 /* Cast function result from I{1,2,4,8,16} to the required type. */
10683 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10684 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10685 result);
10686 TREE_SIDE_EFFECTS (result) = 1;
10687 protected_set_expr_location (result, loc);
10688 result = convert (void_type_node, result);
10690 return result;
10693 default:
10694 return NULL_TREE;
10698 /* Ignoring their sign, return true if two scalar types are the same. */
10699 bool
10700 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10702 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10704 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10705 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10706 || c2 == FIXED_POINT_TYPE));
10708 /* Equality works here because c_common_signed_type uses
10709 TYPE_MAIN_VARIANT. */
10710 return c_common_signed_type (t1)
10711 == c_common_signed_type (t2);
10714 /* Check for missing format attributes on function pointers. LTYPE is
10715 the new type or left-hand side type. RTYPE is the old type or
10716 right-hand side type. Returns TRUE if LTYPE is missing the desired
10717 attribute. */
10719 bool
10720 check_missing_format_attribute (tree ltype, tree rtype)
10722 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10723 tree ra;
10725 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10726 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10727 break;
10728 if (ra)
10730 tree la;
10731 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10732 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10733 break;
10734 return !la;
10736 else
10737 return false;
10740 /* Subscripting with type char is likely to lose on a machine where
10741 chars are signed. So warn on any machine, but optionally. Don't
10742 warn for unsigned char since that type is safe. Don't warn for
10743 signed char because anyone who uses that must have done so
10744 deliberately. Furthermore, we reduce the false positive load by
10745 warning only for non-constant value of type char. */
10747 void
10748 warn_array_subscript_with_type_char (tree index)
10750 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10751 && TREE_CODE (index) != INTEGER_CST)
10752 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10755 /* Implement -Wparentheses for the unexpected C precedence rules, to
10756 cover cases like x + y << z which readers are likely to
10757 misinterpret. We have seen an expression in which CODE is a binary
10758 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10759 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10760 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10761 expression was not formed using a binary or unary operator, or it
10762 was enclosed in parentheses. */
10764 void
10765 warn_about_parentheses (location_t loc, enum tree_code code,
10766 enum tree_code code_left, tree arg_left,
10767 enum tree_code code_right, tree arg_right)
10769 if (!warn_parentheses)
10770 return;
10772 /* This macro tests that the expression ARG with original tree code
10773 CODE appears to be a boolean expression. or the result of folding a
10774 boolean expression. */
10775 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10776 (truth_value_p (TREE_CODE (ARG)) \
10777 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10778 /* Folding may create 0 or 1 integers from other expressions. */ \
10779 || ((CODE) != INTEGER_CST \
10780 && (integer_onep (ARG) || integer_zerop (ARG))))
10782 switch (code)
10784 case LSHIFT_EXPR:
10785 if (code_left == PLUS_EXPR)
10786 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10787 "suggest parentheses around %<+%> inside %<<<%>");
10788 else if (code_right == PLUS_EXPR)
10789 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10790 "suggest parentheses around %<+%> inside %<<<%>");
10791 else if (code_left == MINUS_EXPR)
10792 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10793 "suggest parentheses around %<-%> inside %<<<%>");
10794 else if (code_right == MINUS_EXPR)
10795 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10796 "suggest parentheses around %<-%> inside %<<<%>");
10797 return;
10799 case RSHIFT_EXPR:
10800 if (code_left == PLUS_EXPR)
10801 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10802 "suggest parentheses around %<+%> inside %<>>%>");
10803 else if (code_right == PLUS_EXPR)
10804 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10805 "suggest parentheses around %<+%> inside %<>>%>");
10806 else if (code_left == MINUS_EXPR)
10807 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10808 "suggest parentheses around %<-%> inside %<>>%>");
10809 else if (code_right == MINUS_EXPR)
10810 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10811 "suggest parentheses around %<-%> inside %<>>%>");
10812 return;
10814 case TRUTH_ORIF_EXPR:
10815 if (code_left == TRUTH_ANDIF_EXPR)
10816 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10817 "suggest parentheses around %<&&%> within %<||%>");
10818 else if (code_right == TRUTH_ANDIF_EXPR)
10819 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10820 "suggest parentheses around %<&&%> within %<||%>");
10821 return;
10823 case BIT_IOR_EXPR:
10824 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10825 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10826 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10827 "suggest parentheses around arithmetic in operand of %<|%>");
10828 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10829 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10830 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10831 "suggest parentheses around arithmetic in operand of %<|%>");
10832 /* Check cases like x|y==z */
10833 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10834 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10835 "suggest parentheses around comparison in operand of %<|%>");
10836 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10837 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10838 "suggest parentheses around comparison in operand of %<|%>");
10839 /* Check cases like !x | y */
10840 else if (code_left == TRUTH_NOT_EXPR
10841 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10842 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10843 "suggest parentheses around operand of "
10844 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10845 return;
10847 case BIT_XOR_EXPR:
10848 if (code_left == BIT_AND_EXPR
10849 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10850 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10851 "suggest parentheses around arithmetic in operand of %<^%>");
10852 else if (code_right == BIT_AND_EXPR
10853 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10854 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10855 "suggest parentheses around arithmetic in operand of %<^%>");
10856 /* Check cases like x^y==z */
10857 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10858 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10859 "suggest parentheses around comparison in operand of %<^%>");
10860 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10861 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10862 "suggest parentheses around comparison in operand of %<^%>");
10863 return;
10865 case BIT_AND_EXPR:
10866 if (code_left == PLUS_EXPR)
10867 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10868 "suggest parentheses around %<+%> in operand of %<&%>");
10869 else if (code_right == PLUS_EXPR)
10870 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10871 "suggest parentheses around %<+%> in operand of %<&%>");
10872 else if (code_left == MINUS_EXPR)
10873 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10874 "suggest parentheses around %<-%> in operand of %<&%>");
10875 else if (code_right == MINUS_EXPR)
10876 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10877 "suggest parentheses around %<-%> in operand of %<&%>");
10878 /* Check cases like x&y==z */
10879 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10880 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10881 "suggest parentheses around comparison in operand of %<&%>");
10882 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10883 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10884 "suggest parentheses around comparison in operand of %<&%>");
10885 /* Check cases like !x & y */
10886 else if (code_left == TRUTH_NOT_EXPR
10887 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10888 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10889 "suggest parentheses around operand of "
10890 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10891 return;
10893 case EQ_EXPR:
10894 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10895 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10896 "suggest parentheses around comparison in operand of %<==%>");
10897 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10898 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10899 "suggest parentheses around comparison in operand of %<==%>");
10900 return;
10901 case NE_EXPR:
10902 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10903 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10904 "suggest parentheses around comparison in operand of %<!=%>");
10905 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10906 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10907 "suggest parentheses around comparison in operand of %<!=%>");
10908 return;
10910 default:
10911 if (TREE_CODE_CLASS (code) == tcc_comparison)
10913 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10914 && code_left != NE_EXPR && code_left != EQ_EXPR
10915 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10916 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10917 "comparisons like %<X<=Y<=Z%> do not "
10918 "have their mathematical meaning");
10919 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
10920 && code_right != NE_EXPR && code_right != EQ_EXPR
10921 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
10922 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10923 "comparisons like %<X<=Y<=Z%> do not "
10924 "have their mathematical meaning");
10926 return;
10928 #undef NOT_A_BOOLEAN_EXPR_P
10931 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10933 void
10934 warn_for_unused_label (tree label)
10936 if (!TREE_USED (label))
10938 if (DECL_INITIAL (label))
10939 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10940 else
10941 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10945 /* Warn for division by zero according to the value of DIVISOR. LOC
10946 is the location of the division operator. */
10948 void
10949 warn_for_div_by_zero (location_t loc, tree divisor)
10951 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10952 about division by zero. Do not issue a warning if DIVISOR has a
10953 floating-point type, since we consider 0.0/0.0 a valid way of
10954 generating a NaN. */
10955 if (c_inhibit_evaluation_warnings == 0
10956 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10957 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10960 /* Subroutine of build_binary_op. Give warnings for comparisons
10961 between signed and unsigned quantities that may fail. Do the
10962 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10963 so that casts will be considered, but default promotions won't
10966 LOCATION is the location of the comparison operator.
10968 The arguments of this function map directly to local variables
10969 of build_binary_op. */
10971 void
10972 warn_for_sign_compare (location_t location,
10973 tree orig_op0, tree orig_op1,
10974 tree op0, tree op1,
10975 tree result_type, enum tree_code resultcode)
10977 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10978 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10979 int unsignedp0, unsignedp1;
10981 /* In C++, check for comparison of different enum types. */
10982 if (c_dialect_cxx()
10983 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10984 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10985 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10986 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10988 warning_at (location,
10989 OPT_Wsign_compare, "comparison between types %qT and %qT",
10990 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10993 /* Do not warn if the comparison is being done in a signed type,
10994 since the signed type will only be chosen if it can represent
10995 all the values of the unsigned type. */
10996 if (!TYPE_UNSIGNED (result_type))
10997 /* OK */;
10998 /* Do not warn if both operands are unsigned. */
10999 else if (op0_signed == op1_signed)
11000 /* OK */;
11001 else
11003 tree sop, uop, base_type;
11004 bool ovf;
11006 if (op0_signed)
11007 sop = orig_op0, uop = orig_op1;
11008 else
11009 sop = orig_op1, uop = orig_op0;
11011 STRIP_TYPE_NOPS (sop);
11012 STRIP_TYPE_NOPS (uop);
11013 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11014 ? TREE_TYPE (result_type) : result_type);
11016 /* Do not warn if the signed quantity is an unsuffixed integer
11017 literal (or some static constant expression involving such
11018 literals or a conditional expression involving such literals)
11019 and it is non-negative. */
11020 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11021 /* OK */;
11022 /* Do not warn if the comparison is an equality operation, the
11023 unsigned quantity is an integral constant, and it would fit
11024 in the result if the result were signed. */
11025 else if (TREE_CODE (uop) == INTEGER_CST
11026 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11027 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11028 /* OK */;
11029 /* In C, do not warn if the unsigned quantity is an enumeration
11030 constant and its maximum value would fit in the result if the
11031 result were signed. */
11032 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11033 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11034 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11035 c_common_signed_type (base_type)))
11036 /* OK */;
11037 else
11038 warning_at (location,
11039 OPT_Wsign_compare,
11040 "comparison between signed and unsigned integer expressions");
11043 /* Warn if two unsigned values are being compared in a size larger
11044 than their original size, and one (and only one) is the result of
11045 a `~' operator. This comparison will always fail.
11047 Also warn if one operand is a constant, and the constant does not
11048 have all bits set that are set in the ~ operand when it is
11049 extended. */
11051 op0 = c_common_get_narrower (op0, &unsignedp0);
11052 op1 = c_common_get_narrower (op1, &unsignedp1);
11054 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11055 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11057 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11058 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11059 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11060 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11062 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11064 tree primop;
11065 HOST_WIDE_INT constant, mask;
11066 int unsignedp;
11067 unsigned int bits;
11069 if (host_integerp (op0, 0))
11071 primop = op1;
11072 unsignedp = unsignedp1;
11073 constant = tree_low_cst (op0, 0);
11075 else
11077 primop = op0;
11078 unsignedp = unsignedp0;
11079 constant = tree_low_cst (op1, 0);
11082 bits = TYPE_PRECISION (TREE_TYPE (primop));
11083 if (bits < TYPE_PRECISION (result_type)
11084 && bits < HOST_BITS_PER_LONG && unsignedp)
11086 mask = (~ (HOST_WIDE_INT) 0) << bits;
11087 if ((mask & constant) != mask)
11089 if (constant == 0)
11090 warning (OPT_Wsign_compare,
11091 "promoted ~unsigned is always non-zero");
11092 else
11093 warning_at (location, OPT_Wsign_compare,
11094 "comparison of promoted ~unsigned with constant");
11098 else if (unsignedp0 && unsignedp1
11099 && (TYPE_PRECISION (TREE_TYPE (op0))
11100 < TYPE_PRECISION (result_type))
11101 && (TYPE_PRECISION (TREE_TYPE (op1))
11102 < TYPE_PRECISION (result_type)))
11103 warning_at (location, OPT_Wsign_compare,
11104 "comparison of promoted ~unsigned with unsigned");
11108 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11109 type via c_common_type. If -Wdouble-promotion is in use, and the
11110 conditions for warning have been met, issue a warning. GMSGID is
11111 the warning message. It must have two %T specifiers for the type
11112 that was converted (generally "float") and the type to which it was
11113 converted (generally "double), respectively. LOC is the location
11114 to which the awrning should refer. */
11116 void
11117 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11118 const char *gmsgid, location_t loc)
11120 tree source_type;
11122 if (!warn_double_promotion)
11123 return;
11124 /* If the conversion will not occur at run-time, there is no need to
11125 warn about it. */
11126 if (c_inhibit_evaluation_warnings)
11127 return;
11128 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11129 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11130 return;
11131 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11132 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11133 source_type = type1;
11134 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11135 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11136 source_type = type2;
11137 else
11138 return;
11139 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11142 /* Setup a TYPE_DECL node as a typedef representation.
11144 X is a TYPE_DECL for a typedef statement. Create a brand new
11145 ..._TYPE node (which will be just a variant of the existing
11146 ..._TYPE node with identical properties) and then install X
11147 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11149 The whole point here is to end up with a situation where each
11150 and every ..._TYPE node the compiler creates will be uniquely
11151 associated with AT MOST one node representing a typedef name.
11152 This way, even though the compiler substitutes corresponding
11153 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11154 early on, later parts of the compiler can always do the reverse
11155 translation and get back the corresponding typedef name. For
11156 example, given:
11158 typedef struct S MY_TYPE;
11159 MY_TYPE object;
11161 Later parts of the compiler might only know that `object' was of
11162 type `struct S' if it were not for code just below. With this
11163 code however, later parts of the compiler see something like:
11165 struct S' == struct S
11166 typedef struct S' MY_TYPE;
11167 struct S' object;
11169 And they can then deduce (from the node for type struct S') that
11170 the original object declaration was:
11172 MY_TYPE object;
11174 Being able to do this is important for proper support of protoize,
11175 and also for generating precise symbolic debugging information
11176 which takes full account of the programmer's (typedef) vocabulary.
11178 Obviously, we don't want to generate a duplicate ..._TYPE node if
11179 the TYPE_DECL node that we are now processing really represents a
11180 standard built-in type. */
11182 void
11183 set_underlying_type (tree x)
11185 if (x == error_mark_node)
11186 return;
11187 if (DECL_IS_BUILTIN (x))
11189 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11190 TYPE_NAME (TREE_TYPE (x)) = x;
11192 else if (TREE_TYPE (x) != error_mark_node
11193 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11195 tree tt = TREE_TYPE (x);
11196 DECL_ORIGINAL_TYPE (x) = tt;
11197 tt = build_variant_type_copy (tt);
11198 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11199 TYPE_NAME (tt) = x;
11200 TREE_USED (tt) = TREE_USED (x);
11201 TREE_TYPE (x) = tt;
11205 /* Record the types used by the current global variable declaration
11206 being parsed, so that we can decide later to emit their debug info.
11207 Those types are in types_used_by_cur_var_decl, and we are going to
11208 store them in the types_used_by_vars_hash hash table.
11209 DECL is the declaration of the global variable that has been parsed. */
11211 void
11212 record_types_used_by_current_var_decl (tree decl)
11214 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11216 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11218 tree type = types_used_by_cur_var_decl->pop ();
11219 types_used_by_var_decl_insert (type, decl);
11223 /* If DECL is a typedef that is declared in the current function,
11224 record it for the purpose of -Wunused-local-typedefs. */
11226 void
11227 record_locally_defined_typedef (tree decl)
11229 struct c_language_function *l;
11231 if (!warn_unused_local_typedefs
11232 || cfun == NULL
11233 /* if this is not a locally defined typedef then we are not
11234 interested. */
11235 || !is_typedef_decl (decl)
11236 || !decl_function_context (decl))
11237 return;
11239 l = (struct c_language_function *) cfun->language;
11240 vec_safe_push (l->local_typedefs, decl);
11243 /* If T is a TYPE_DECL declared locally, mark it as used. */
11245 void
11246 maybe_record_typedef_use (tree t)
11248 if (!is_typedef_decl (t))
11249 return;
11251 TREE_USED (t) = true;
11254 /* Warn if there are some unused locally defined typedefs in the
11255 current function. */
11257 void
11258 maybe_warn_unused_local_typedefs (void)
11260 int i;
11261 tree decl;
11262 /* The number of times we have emitted -Wunused-local-typedefs
11263 warnings. If this is different from errorcount, that means some
11264 unrelated errors have been issued. In which case, we'll avoid
11265 emitting "unused-local-typedefs" warnings. */
11266 static int unused_local_typedefs_warn_count;
11267 struct c_language_function *l;
11269 if (cfun == NULL)
11270 return;
11272 if ((l = (struct c_language_function *) cfun->language) == NULL)
11273 return;
11275 if (warn_unused_local_typedefs
11276 && errorcount == unused_local_typedefs_warn_count)
11278 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11279 if (!TREE_USED (decl))
11280 warning_at (DECL_SOURCE_LOCATION (decl),
11281 OPT_Wunused_local_typedefs,
11282 "typedef %qD locally defined but not used", decl);
11283 unused_local_typedefs_warn_count = errorcount;
11286 vec_free (l->local_typedefs);
11289 /* The C and C++ parsers both use vectors to hold function arguments.
11290 For efficiency, we keep a cache of unused vectors. This is the
11291 cache. */
11293 typedef vec<tree, va_gc> *tree_gc_vec;
11294 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11296 /* Return a new vector from the cache. If the cache is empty,
11297 allocate a new vector. These vectors are GC'ed, so it is OK if the
11298 pointer is not released.. */
11300 vec<tree, va_gc> *
11301 make_tree_vector (void)
11303 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11304 return tree_vector_cache->pop ();
11305 else
11307 /* Passing 0 to vec::alloc returns NULL, and our callers require
11308 that we always return a non-NULL value. The vector code uses
11309 4 when growing a NULL vector, so we do too. */
11310 vec<tree, va_gc> *v;
11311 vec_alloc (v, 4);
11312 return v;
11316 /* Release a vector of trees back to the cache. */
11318 void
11319 release_tree_vector (vec<tree, va_gc> *vec)
11321 if (vec != NULL)
11323 vec->truncate (0);
11324 vec_safe_push (tree_vector_cache, vec);
11328 /* Get a new tree vector holding a single tree. */
11330 vec<tree, va_gc> *
11331 make_tree_vector_single (tree t)
11333 vec<tree, va_gc> *ret = make_tree_vector ();
11334 ret->quick_push (t);
11335 return ret;
11338 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11340 vec<tree, va_gc> *
11341 make_tree_vector_from_list (tree list)
11343 vec<tree, va_gc> *ret = make_tree_vector ();
11344 for (; list; list = TREE_CHAIN (list))
11345 vec_safe_push (ret, TREE_VALUE (list));
11346 return ret;
11349 /* Get a new tree vector which is a copy of an existing one. */
11351 vec<tree, va_gc> *
11352 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11354 vec<tree, va_gc> *ret;
11355 unsigned int ix;
11356 tree t;
11358 ret = make_tree_vector ();
11359 vec_safe_reserve (ret, vec_safe_length (orig));
11360 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11361 ret->quick_push (t);
11362 return ret;
11365 /* Return true if KEYWORD starts a type specifier. */
11367 bool
11368 keyword_begins_type_specifier (enum rid keyword)
11370 switch (keyword)
11372 case RID_INT:
11373 case RID_CHAR:
11374 case RID_FLOAT:
11375 case RID_DOUBLE:
11376 case RID_VOID:
11377 case RID_INT128:
11378 case RID_UNSIGNED:
11379 case RID_LONG:
11380 case RID_SHORT:
11381 case RID_SIGNED:
11382 case RID_DFLOAT32:
11383 case RID_DFLOAT64:
11384 case RID_DFLOAT128:
11385 case RID_FRACT:
11386 case RID_ACCUM:
11387 case RID_BOOL:
11388 case RID_WCHAR:
11389 case RID_CHAR16:
11390 case RID_CHAR32:
11391 case RID_SAT:
11392 case RID_COMPLEX:
11393 case RID_TYPEOF:
11394 case RID_STRUCT:
11395 case RID_CLASS:
11396 case RID_UNION:
11397 case RID_ENUM:
11398 return true;
11399 default:
11400 return false;
11404 /* Return true if KEYWORD names a type qualifier. */
11406 bool
11407 keyword_is_type_qualifier (enum rid keyword)
11409 switch (keyword)
11411 case RID_CONST:
11412 case RID_VOLATILE:
11413 case RID_RESTRICT:
11414 return true;
11415 default:
11416 return false;
11420 /* Return true if KEYWORD names a storage class specifier.
11422 RID_TYPEDEF is not included in this list despite `typedef' being
11423 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11424 such for syntactic convenience only. */
11426 bool
11427 keyword_is_storage_class_specifier (enum rid keyword)
11429 switch (keyword)
11431 case RID_STATIC:
11432 case RID_EXTERN:
11433 case RID_REGISTER:
11434 case RID_AUTO:
11435 case RID_MUTABLE:
11436 case RID_THREAD:
11437 return true;
11438 default:
11439 return false;
11443 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11445 static bool
11446 keyword_is_function_specifier (enum rid keyword)
11448 switch (keyword)
11450 case RID_INLINE:
11451 case RID_NORETURN:
11452 case RID_VIRTUAL:
11453 case RID_EXPLICIT:
11454 return true;
11455 default:
11456 return false;
11460 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11461 declaration-specifier (C99 6.7). */
11463 bool
11464 keyword_is_decl_specifier (enum rid keyword)
11466 if (keyword_is_storage_class_specifier (keyword)
11467 || keyword_is_type_qualifier (keyword)
11468 || keyword_is_function_specifier (keyword))
11469 return true;
11471 switch (keyword)
11473 case RID_TYPEDEF:
11474 case RID_FRIEND:
11475 case RID_CONSTEXPR:
11476 return true;
11477 default:
11478 return false;
11482 /* Initialize language-specific-bits of tree_contains_struct. */
11484 void
11485 c_common_init_ts (void)
11487 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11488 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11491 /* Build a user-defined numeric literal out of an integer constant type VALUE
11492 with identifier SUFFIX. */
11494 tree
11495 build_userdef_literal (tree suffix_id, tree value,
11496 enum overflow_type overflow, tree num_string)
11498 tree literal = make_node (USERDEF_LITERAL);
11499 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11500 USERDEF_LITERAL_VALUE (literal) = value;
11501 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11502 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11503 return literal;
11506 /* For vector[index], convert the vector to a
11507 pointer of the underlying type. */
11508 void
11509 convert_vector_to_pointer_for_subscript (location_t loc,
11510 tree* vecp, tree index)
11512 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11514 tree type = TREE_TYPE (*vecp);
11515 tree type1;
11517 if (TREE_CODE (index) == INTEGER_CST)
11518 if (!host_integerp (index, 1)
11519 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11520 >= TYPE_VECTOR_SUBPARTS (type)))
11521 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11523 c_common_mark_addressable_vec (*vecp);
11524 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11525 type = build_pointer_type (type);
11526 type1 = build_pointer_type (TREE_TYPE (*vecp));
11527 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11528 *vecp = convert (type, *vecp);
11532 /* Determine which of the operands, if any, is a scalar that needs to be
11533 converted to a vector, for the range of operations. */
11534 enum stv_conv
11535 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11536 bool complain)
11538 tree type0 = TREE_TYPE (op0);
11539 tree type1 = TREE_TYPE (op1);
11540 bool integer_only_op = false;
11541 enum stv_conv ret = stv_firstarg;
11543 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11544 || TREE_CODE (type1) == VECTOR_TYPE);
11545 switch (code)
11547 /* Most GENERIC binary expressions require homogeneous arguments.
11548 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11549 argument that is a vector and a second one that is a scalar, so
11550 we never return stv_secondarg for them. */
11551 case RSHIFT_EXPR:
11552 case LSHIFT_EXPR:
11553 if (TREE_CODE (type0) == INTEGER_TYPE
11554 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11556 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11558 if (complain)
11559 error_at (loc, "conversion of scalar %qT to vector %qT "
11560 "involves truncation", type0, type1);
11561 return stv_error;
11563 else
11564 return stv_firstarg;
11566 break;
11568 case BIT_IOR_EXPR:
11569 case BIT_XOR_EXPR:
11570 case BIT_AND_EXPR:
11571 integer_only_op = true;
11572 /* ... fall through ... */
11574 case VEC_COND_EXPR:
11576 case PLUS_EXPR:
11577 case MINUS_EXPR:
11578 case MULT_EXPR:
11579 case TRUNC_DIV_EXPR:
11580 case CEIL_DIV_EXPR:
11581 case FLOOR_DIV_EXPR:
11582 case ROUND_DIV_EXPR:
11583 case EXACT_DIV_EXPR:
11584 case TRUNC_MOD_EXPR:
11585 case FLOOR_MOD_EXPR:
11586 case RDIV_EXPR:
11587 case EQ_EXPR:
11588 case NE_EXPR:
11589 case LE_EXPR:
11590 case GE_EXPR:
11591 case LT_EXPR:
11592 case GT_EXPR:
11593 /* What about UNLT_EXPR? */
11594 if (TREE_CODE (type0) == VECTOR_TYPE)
11596 tree tmp;
11597 ret = stv_secondarg;
11598 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11599 tmp = type0; type0 = type1; type1 = tmp;
11600 tmp = op0; op0 = op1; op1 = tmp;
11603 if (TREE_CODE (type0) == INTEGER_TYPE
11604 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11606 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11608 if (complain)
11609 error_at (loc, "conversion of scalar %qT to vector %qT "
11610 "involves truncation", type0, type1);
11611 return stv_error;
11613 return ret;
11615 else if (!integer_only_op
11616 /* Allow integer --> real conversion if safe. */
11617 && (TREE_CODE (type0) == REAL_TYPE
11618 || TREE_CODE (type0) == INTEGER_TYPE)
11619 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11621 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11623 if (complain)
11624 error_at (loc, "conversion of scalar %qT to vector %qT "
11625 "involves truncation", type0, type1);
11626 return stv_error;
11628 return ret;
11630 default:
11631 break;
11634 return stv_nothing;
11637 /* Return true iff ALIGN is an integral constant that is a fundamental
11638 alignment, as defined by [basic.align] in the c++-11
11639 specifications.
11641 That is:
11643 [A fundamental alignment is represented by an alignment less than or
11644 equal to the greatest alignment supported by the implementation
11645 in all contexts, which is equal to
11646 alignof(max_align_t)]. */
11648 bool
11649 cxx_fundamental_alignment_p (unsigned align)
11651 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11652 TYPE_ALIGN (long_double_type_node)));
11655 #include "gt-c-family-c-common.h"