Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blob164aa855bac5f1d594f354668dfbcbdd8f99fc22
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_address_safety_analysis_attribute (tree *, tree, tree,
325 int, bool *);
326 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_always_inline_attribute (tree *, tree, tree, int,
330 bool *);
331 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
338 bool *);
339 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_transparent_union_attribute (tree *, tree, tree,
341 int, bool *);
342 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
348 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
349 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
352 static tree handle_visibility_attribute (tree *, tree, tree, int,
353 bool *);
354 static tree handle_tls_model_attribute (tree *, tree, tree, int,
355 bool *);
356 static tree handle_no_instrument_function_attribute (tree *, tree,
357 tree, int, bool *);
358 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
361 bool *);
362 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_deprecated_attribute (tree *, tree, tree, int,
367 bool *);
368 static tree handle_vector_size_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
374 bool *);
375 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
380 static tree ignore_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
384 static void check_function_nonnull (tree, int, tree *);
385 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
386 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
387 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
388 static int resort_field_decl_cmp (const void *, const void *);
390 /* Reserved words. The third field is a mask: keywords are disabled
391 if they match the mask.
393 Masks for languages:
394 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
395 C --std=c99: D_CXXONLY | D_OBJC
396 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
397 UPC is like C except that D_UPC is not set
398 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
399 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
400 ObjC++ is like C++ except that D_OBJC is not set
402 If -fno-asm is used, D_ASM is added to the mask. If
403 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
404 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
405 In C with -Wc++-compat, we warn if D_CXXWARN is set.
407 Note the complication of the D_CXX_OBJC keywords. These are
408 reserved words such as 'class'. In C++, 'class' is a reserved
409 word. In Objective-C++ it is too. In Objective-C, it is a
410 reserved word too, but only if it follows an '@' sign.
412 const struct c_common_resword c_common_reswords[] =
414 { "_Alignas", RID_ALIGNAS, D_CONLY },
415 { "_Alignof", RID_ALIGNOF, D_CONLY },
416 { "_Bool", RID_BOOL, D_CONLY },
417 { "_Complex", RID_COMPLEX, 0 },
418 { "_Imaginary", RID_IMAGINARY, D_CONLY },
419 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
420 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
421 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
422 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
423 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
424 { "_Sat", RID_SAT, D_CONLY | D_EXT },
425 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
426 { "_Noreturn", RID_NORETURN, D_CONLY },
427 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
428 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
429 { "__alignof", RID_ALIGNOF, 0 },
430 { "__alignof__", RID_ALIGNOF, 0 },
431 { "__asm", RID_ASM, 0 },
432 { "__asm__", RID_ASM, 0 },
433 { "__attribute", RID_ATTRIBUTE, 0 },
434 { "__attribute__", RID_ATTRIBUTE, 0 },
435 { "__bases", RID_BASES, D_CXXONLY },
436 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
437 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
438 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
439 { "__builtin_offsetof", RID_OFFSETOF, 0 },
440 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
441 { "__builtin_va_arg", RID_VA_ARG, 0 },
442 { "__complex", RID_COMPLEX, 0 },
443 { "__complex__", RID_COMPLEX, 0 },
444 { "__const", RID_CONST, 0 },
445 { "__const__", RID_CONST, 0 },
446 { "__decltype", RID_DECLTYPE, D_CXXONLY },
447 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
448 { "__extension__", RID_EXTENSION, 0 },
449 { "__func__", RID_C99_FUNCTION_NAME, 0 },
450 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
451 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
452 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
453 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
454 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
455 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
456 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
457 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
458 { "__imag", RID_IMAGPART, 0 },
459 { "__imag__", RID_IMAGPART, 0 },
460 { "__inline", RID_INLINE, 0 },
461 { "__inline__", RID_INLINE, 0 },
462 { "__int128", RID_INT128, 0 },
463 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
464 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
465 { "__is_class", RID_IS_CLASS, D_CXXONLY },
466 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
467 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
468 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
469 { "__is_final", RID_IS_FINAL, D_CXXONLY },
470 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
471 { "__is_pod", RID_IS_POD, D_CXXONLY },
472 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
473 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
474 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
475 { "__is_union", RID_IS_UNION, D_CXXONLY },
476 { "__label__", RID_LABEL, 0 },
477 { "__null", RID_NULL, 0 },
478 { "__real", RID_REALPART, 0 },
479 { "__real__", RID_REALPART, 0 },
480 { "__restrict", RID_RESTRICT, 0 },
481 { "__restrict__", RID_RESTRICT, 0 },
482 { "__signed", RID_SIGNED, 0 },
483 { "__signed__", RID_SIGNED, 0 },
484 { "__thread", RID_THREAD, 0 },
485 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
486 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
487 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
488 { "__typeof", RID_TYPEOF, 0 },
489 { "__typeof__", RID_TYPEOF, 0 },
490 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
491 { "__volatile", RID_VOLATILE, 0 },
492 { "__volatile__", RID_VOLATILE, 0 },
493 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
494 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
495 { "asm", RID_ASM, D_ASM },
496 { "auto", RID_AUTO, 0 },
497 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
498 { "break", RID_BREAK, 0 },
499 { "case", RID_CASE, 0 },
500 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
501 { "char", RID_CHAR, 0 },
502 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
503 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
504 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
505 { "const", RID_CONST, 0 },
506 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
507 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
508 { "continue", RID_CONTINUE, 0 },
509 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
510 { "default", RID_DEFAULT, 0 },
511 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
512 { "do", RID_DO, 0 },
513 { "double", RID_DOUBLE, 0 },
514 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
515 { "else", RID_ELSE, 0 },
516 { "enum", RID_ENUM, 0 },
517 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
518 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
519 { "extern", RID_EXTERN, 0 },
520 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
521 { "float", RID_FLOAT, 0 },
522 { "for", RID_FOR, 0 },
523 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
524 { "goto", RID_GOTO, 0 },
525 { "if", RID_IF, 0 },
526 { "inline", RID_INLINE, D_EXT89 },
527 { "int", RID_INT, 0 },
528 { "long", RID_LONG, 0 },
529 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
530 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
531 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
532 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
533 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
534 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
535 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
536 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
537 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
538 { "register", RID_REGISTER, 0 },
539 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
540 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
541 { "return", RID_RETURN, 0 },
542 { "short", RID_SHORT, 0 },
543 { "signed", RID_SIGNED, 0 },
544 { "sizeof", RID_SIZEOF, 0 },
545 { "static", RID_STATIC, 0 },
546 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
547 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
548 { "struct", RID_STRUCT, 0 },
549 { "switch", RID_SWITCH, 0 },
550 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
551 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
552 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
553 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
554 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
555 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
556 { "typedef", RID_TYPEDEF, 0 },
557 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
558 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
559 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
560 { "union", RID_UNION, 0 },
561 { "unsigned", RID_UNSIGNED, 0 },
562 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
563 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
564 { "void", RID_VOID, 0 },
565 { "volatile", RID_VOLATILE, 0 },
566 { "wchar_t", RID_WCHAR, D_CXXONLY },
567 { "while", RID_WHILE, 0 },
568 /* These Objective-C keywords are recognized only immediately after
569 an '@'. */
570 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
571 { "defs", RID_AT_DEFS, D_OBJC },
572 { "encode", RID_AT_ENCODE, D_OBJC },
573 { "end", RID_AT_END, D_OBJC },
574 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
575 { "interface", RID_AT_INTERFACE, D_OBJC },
576 { "protocol", RID_AT_PROTOCOL, D_OBJC },
577 { "selector", RID_AT_SELECTOR, D_OBJC },
578 { "finally", RID_AT_FINALLY, D_OBJC },
579 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
580 { "optional", RID_AT_OPTIONAL, D_OBJC },
581 { "required", RID_AT_REQUIRED, D_OBJC },
582 { "property", RID_AT_PROPERTY, D_OBJC },
583 { "package", RID_AT_PACKAGE, D_OBJC },
584 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
585 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
586 /* These are recognized only in protocol-qualifier context
587 (see above) */
588 { "bycopy", RID_BYCOPY, D_OBJC },
589 { "byref", RID_BYREF, D_OBJC },
590 { "in", RID_IN, D_OBJC },
591 { "inout", RID_INOUT, D_OBJC },
592 { "oneway", RID_ONEWAY, D_OBJC },
593 { "out", RID_OUT, D_OBJC },
595 /* UPC keywords */
596 { "shared", RID_SHARED, D_UPC },
597 { "relaxed", RID_RELAXED, D_UPC },
598 { "strict", RID_STRICT, D_UPC },
599 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
600 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
601 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
602 { "upc_forall", RID_UPC_FORALL, D_UPC },
603 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
604 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
605 { "upc_wait", RID_UPC_WAIT, D_UPC },
607 /* These are recognized inside a property attribute list */
608 { "assign", RID_ASSIGN, D_OBJC },
609 { "copy", RID_COPY, D_OBJC },
610 { "getter", RID_GETTER, D_OBJC },
611 { "nonatomic", RID_NONATOMIC, D_OBJC },
612 { "readonly", RID_READONLY, D_OBJC },
613 { "readwrite", RID_READWRITE, D_OBJC },
614 { "retain", RID_RETAIN, D_OBJC },
615 { "setter", RID_SETTER, D_OBJC },
618 const unsigned int num_c_common_reswords =
619 sizeof c_common_reswords / sizeof (struct c_common_resword);
621 /* Table of machine-independent attributes common to all C-like languages. */
622 const struct attribute_spec c_common_attribute_table[] =
624 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
625 affects_type_identity } */
626 { "packed", 0, 0, false, false, false,
627 handle_packed_attribute , false},
628 { "nocommon", 0, 0, true, false, false,
629 handle_nocommon_attribute, false},
630 { "common", 0, 0, true, false, false,
631 handle_common_attribute, false },
632 /* FIXME: logically, noreturn attributes should be listed as
633 "false, true, true" and apply to function types. But implementing this
634 would require all the places in the compiler that use TREE_THIS_VOLATILE
635 on a decl to identify non-returning functions to be located and fixed
636 to check the function type instead. */
637 { "noreturn", 0, 0, true, false, false,
638 handle_noreturn_attribute, false },
639 { "volatile", 0, 0, true, false, false,
640 handle_noreturn_attribute, false },
641 { "noinline", 0, 0, true, false, false,
642 handle_noinline_attribute, false },
643 { "noclone", 0, 0, true, false, false,
644 handle_noclone_attribute, false },
645 { "leaf", 0, 0, true, false, false,
646 handle_leaf_attribute, false },
647 { "always_inline", 0, 0, true, false, false,
648 handle_always_inline_attribute, false },
649 { "gnu_inline", 0, 0, true, false, false,
650 handle_gnu_inline_attribute, false },
651 { "artificial", 0, 0, true, false, false,
652 handle_artificial_attribute, false },
653 { "flatten", 0, 0, true, false, false,
654 handle_flatten_attribute, false },
655 { "used", 0, 0, true, false, false,
656 handle_used_attribute, false },
657 { "unused", 0, 0, false, false, false,
658 handle_unused_attribute, false },
659 { "externally_visible", 0, 0, true, false, false,
660 handle_externally_visible_attribute, false },
661 /* The same comments as for noreturn attributes apply to const ones. */
662 { "const", 0, 0, true, false, false,
663 handle_const_attribute, false },
664 { "transparent_union", 0, 0, false, false, false,
665 handle_transparent_union_attribute, false },
666 { "constructor", 0, 1, true, false, false,
667 handle_constructor_attribute, false },
668 { "destructor", 0, 1, true, false, false,
669 handle_destructor_attribute, false },
670 { "mode", 1, 1, false, true, false,
671 handle_mode_attribute, false },
672 { "section", 1, 1, true, false, false,
673 handle_section_attribute, false },
674 { "aligned", 0, 1, false, false, false,
675 handle_aligned_attribute, false },
676 { "weak", 0, 0, true, false, false,
677 handle_weak_attribute, false },
678 { "ifunc", 1, 1, true, false, false,
679 handle_ifunc_attribute, false },
680 { "alias", 1, 1, true, false, false,
681 handle_alias_attribute, false },
682 { "weakref", 0, 1, true, false, false,
683 handle_weakref_attribute, false },
684 { "no_instrument_function", 0, 0, true, false, false,
685 handle_no_instrument_function_attribute,
686 false },
687 { "malloc", 0, 0, true, false, false,
688 handle_malloc_attribute, false },
689 { "returns_twice", 0, 0, true, false, false,
690 handle_returns_twice_attribute, false },
691 { "no_stack_limit", 0, 0, true, false, false,
692 handle_no_limit_stack_attribute, false },
693 { "pure", 0, 0, true, false, false,
694 handle_pure_attribute, false },
695 { "transaction_callable", 0, 0, false, true, false,
696 handle_tm_attribute, false },
697 { "transaction_unsafe", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_safe", 0, 0, false, true, false,
700 handle_tm_attribute, false },
701 { "transaction_may_cancel_outer", 0, 0, false, true, false,
702 handle_tm_attribute, false },
703 /* ??? These two attributes didn't make the transition from the
704 Intel language document to the multi-vendor language document. */
705 { "transaction_pure", 0, 0, false, true, false,
706 handle_tm_attribute, false },
707 { "transaction_wrap", 1, 1, true, false, false,
708 handle_tm_wrap_attribute, false },
709 /* For internal use (marking of builtins) only. The name contains space
710 to prevent its usage in source code. */
711 { "no vops", 0, 0, true, false, false,
712 handle_novops_attribute, false },
713 { "deprecated", 0, 1, false, false, false,
714 handle_deprecated_attribute, false },
715 { "vector_size", 1, 1, false, true, false,
716 handle_vector_size_attribute, false },
717 { "visibility", 1, 1, false, false, false,
718 handle_visibility_attribute, false },
719 { "tls_model", 1, 1, true, false, false,
720 handle_tls_model_attribute, false },
721 { "nonnull", 0, -1, false, true, true,
722 handle_nonnull_attribute, false },
723 { "nothrow", 0, 0, true, false, false,
724 handle_nothrow_attribute, false },
725 { "may_alias", 0, 0, false, true, false, NULL, false },
726 { "cleanup", 1, 1, true, false, false,
727 handle_cleanup_attribute, false },
728 { "warn_unused_result", 0, 0, false, true, true,
729 handle_warn_unused_result_attribute, false },
730 { "sentinel", 0, 1, false, true, true,
731 handle_sentinel_attribute, false },
732 /* For internal use (marking of builtins) only. The name contains space
733 to prevent its usage in source code. */
734 { "type generic", 0, 0, false, true, true,
735 handle_type_generic_attribute, false },
736 { "alloc_size", 1, 2, false, true, true,
737 handle_alloc_size_attribute, false },
738 { "cold", 0, 0, true, false, false,
739 handle_cold_attribute, false },
740 { "hot", 0, 0, true, false, false,
741 handle_hot_attribute, false },
742 { "no_address_safety_analysis",
743 0, 0, true, false, false,
744 handle_no_address_safety_analysis_attribute,
745 false },
746 { "warning", 1, 1, true, false, false,
747 handle_error_attribute, false },
748 { "error", 1, 1, true, false, false,
749 handle_error_attribute, false },
750 { "target", 1, -1, true, false, false,
751 handle_target_attribute, false },
752 { "optimize", 1, -1, true, false, false,
753 handle_optimize_attribute, false },
754 /* For internal use only. The leading '*' both prevents its usage in
755 source code and signals that it may be overridden by machine tables. */
756 { "*tm regparm", 0, 0, false, true, true,
757 ignore_attribute, false },
758 { "no_split_stack", 0, 0, true, false, false,
759 handle_no_split_stack_attribute, false },
760 /* For internal use (marking of builtins and runtime functions) only.
761 The name contains space to prevent its usage in source code. */
762 { "fn spec", 1, 1, false, true, true,
763 handle_fnspec_attribute, false },
764 { NULL, 0, 0, false, false, false, NULL, false }
767 /* Give the specifications for the format attributes, used by C and all
768 descendants. */
770 const struct attribute_spec c_common_format_attribute_table[] =
772 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
773 affects_type_identity } */
774 { "format", 3, 3, false, true, true,
775 handle_format_attribute, false },
776 { "format_arg", 1, 1, false, true, true,
777 handle_format_arg_attribute, false },
778 { NULL, 0, 0, false, false, false, NULL, false }
781 /* Return identifier for address space AS. */
783 const char *
784 c_addr_space_name (addr_space_t as)
786 int rid = RID_FIRST_ADDR_SPACE + as;
787 gcc_assert (ridpointers [rid]);
788 return IDENTIFIER_POINTER (ridpointers [rid]);
791 /* Push current bindings for the function name VAR_DECLS. */
793 void
794 start_fname_decls (void)
796 unsigned ix;
797 tree saved = NULL_TREE;
799 for (ix = 0; fname_vars[ix].decl; ix++)
801 tree decl = *fname_vars[ix].decl;
803 if (decl)
805 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
806 saved);
807 *fname_vars[ix].decl = NULL_TREE;
810 if (saved || saved_function_name_decls)
811 /* Normally they'll have been NULL, so only push if we've got a
812 stack, or they are non-NULL. */
813 saved_function_name_decls = tree_cons (saved, NULL_TREE,
814 saved_function_name_decls);
817 /* Finish up the current bindings, adding them into the current function's
818 statement tree. This must be done _before_ finish_stmt_tree is called.
819 If there is no current function, we must be at file scope and no statements
820 are involved. Pop the previous bindings. */
822 void
823 finish_fname_decls (void)
825 unsigned ix;
826 tree stmts = NULL_TREE;
827 tree stack = saved_function_name_decls;
829 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
830 append_to_statement_list (TREE_VALUE (stack), &stmts);
832 if (stmts)
834 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
836 if (TREE_CODE (*bodyp) == BIND_EXPR)
837 bodyp = &BIND_EXPR_BODY (*bodyp);
839 append_to_statement_list_force (*bodyp, &stmts);
840 *bodyp = stmts;
843 for (ix = 0; fname_vars[ix].decl; ix++)
844 *fname_vars[ix].decl = NULL_TREE;
846 if (stack)
848 /* We had saved values, restore them. */
849 tree saved;
851 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
853 tree decl = TREE_PURPOSE (saved);
854 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
856 *fname_vars[ix].decl = decl;
858 stack = TREE_CHAIN (stack);
860 saved_function_name_decls = stack;
863 /* Return the text name of the current function, suitably prettified
864 by PRETTY_P. Return string must be freed by caller. */
866 const char *
867 fname_as_string (int pretty_p)
869 const char *name = "top level";
870 char *namep;
871 int vrb = 2, len;
872 cpp_string cstr = { 0, 0 }, strname;
874 if (!pretty_p)
876 name = "";
877 vrb = 0;
880 if (current_function_decl)
881 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
883 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
885 namep = XNEWVEC (char, len);
886 snprintf (namep, len, "\"%s\"", name);
887 strname.text = (unsigned char *) namep;
888 strname.len = len - 1;
890 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
892 XDELETEVEC (namep);
893 return (const char *) cstr.text;
896 return namep;
899 /* Return the VAR_DECL for a const char array naming the current
900 function. If the VAR_DECL has not yet been created, create it
901 now. RID indicates how it should be formatted and IDENTIFIER_NODE
902 ID is its name (unfortunately C and C++ hold the RID values of
903 keywords in different places, so we can't derive RID from ID in
904 this language independent code. LOC is the location of the
905 function. */
907 tree
908 fname_decl (location_t loc, unsigned int rid, tree id)
910 unsigned ix;
911 tree decl = NULL_TREE;
913 for (ix = 0; fname_vars[ix].decl; ix++)
914 if (fname_vars[ix].rid == rid)
915 break;
917 decl = *fname_vars[ix].decl;
918 if (!decl)
920 /* If a tree is built here, it would normally have the lineno of
921 the current statement. Later this tree will be moved to the
922 beginning of the function and this line number will be wrong.
923 To avoid this problem set the lineno to 0 here; that prevents
924 it from appearing in the RTL. */
925 tree stmts;
926 location_t saved_location = input_location;
927 input_location = UNKNOWN_LOCATION;
929 stmts = push_stmt_list ();
930 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
931 stmts = pop_stmt_list (stmts);
932 if (!IS_EMPTY_STMT (stmts))
933 saved_function_name_decls
934 = tree_cons (decl, stmts, saved_function_name_decls);
935 *fname_vars[ix].decl = decl;
936 input_location = saved_location;
938 if (!ix && !current_function_decl)
939 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
941 return decl;
944 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
946 tree
947 fix_string_type (tree value)
949 int length = TREE_STRING_LENGTH (value);
950 int nchars;
951 tree e_type, i_type, a_type;
953 /* Compute the number of elements, for the array type. */
954 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
956 nchars = length;
957 e_type = char_type_node;
959 else if (TREE_TYPE (value) == char16_array_type_node)
961 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
962 e_type = char16_type_node;
964 else if (TREE_TYPE (value) == char32_array_type_node)
966 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
967 e_type = char32_type_node;
969 else
971 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
972 e_type = wchar_type_node;
975 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
976 limit in C++98 Annex B is very large (65536) and is not normative,
977 so we do not diagnose it (warn_overlength_strings is forced off
978 in c_common_post_options). */
979 if (warn_overlength_strings)
981 const int nchars_max = flag_isoc99 ? 4095 : 509;
982 const int relevant_std = flag_isoc99 ? 99 : 90;
983 if (nchars - 1 > nchars_max)
984 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
985 separate the %d from the 'C'. 'ISO' should not be
986 translated, but it may be moved after 'C%d' in languages
987 where modifiers follow nouns. */
988 pedwarn (input_location, OPT_Woverlength_strings,
989 "string length %qd is greater than the length %qd "
990 "ISO C%d compilers are required to support",
991 nchars - 1, nchars_max, relevant_std);
994 /* Create the array type for the string constant. The ISO C++
995 standard says that a string literal has type `const char[N]' or
996 `const wchar_t[N]'. We use the same logic when invoked as a C
997 front-end with -Wwrite-strings.
998 ??? We should change the type of an expression depending on the
999 state of a warning flag. We should just be warning -- see how
1000 this is handled in the C++ front-end for the deprecated implicit
1001 conversion from string literals to `char*' or `wchar_t*'.
1003 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1004 array type being the unqualified version of that type.
1005 Therefore, if we are constructing an array of const char, we must
1006 construct the matching unqualified array type first. The C front
1007 end does not require this, but it does no harm, so we do it
1008 unconditionally. */
1009 i_type = build_index_type (size_int (nchars - 1));
1010 a_type = build_array_type (e_type, i_type);
1011 if (c_dialect_cxx() || warn_write_strings)
1012 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1014 TREE_TYPE (value) = a_type;
1015 TREE_CONSTANT (value) = 1;
1016 TREE_READONLY (value) = 1;
1017 TREE_STATIC (value) = 1;
1018 return value;
1021 /* If DISABLE is true, stop issuing warnings. This is used when
1022 parsing code that we know will not be executed. This function may
1023 be called multiple times, and works as a stack. */
1025 static void
1026 c_disable_warnings (bool disable)
1028 if (disable)
1030 ++c_inhibit_evaluation_warnings;
1031 fold_defer_overflow_warnings ();
1035 /* If ENABLE is true, reenable issuing warnings. */
1037 static void
1038 c_enable_warnings (bool enable)
1040 if (enable)
1042 --c_inhibit_evaluation_warnings;
1043 fold_undefer_and_ignore_overflow_warnings ();
1047 /* Fully fold EXPR, an expression that was not folded (beyond integer
1048 constant expressions and null pointer constants) when being built
1049 up. If IN_INIT, this is in a static initializer and certain
1050 changes are made to the folding done. Clear *MAYBE_CONST if
1051 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1052 expression because it contains an evaluated operator (in C99) or an
1053 operator outside of sizeof returning an integer constant (in C90)
1054 not permitted in constant expressions, or because it contains an
1055 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1056 set to true by callers before calling this function.) Return the
1057 folded expression. Function arguments have already been folded
1058 before calling this function, as have the contents of SAVE_EXPR,
1059 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1060 C_MAYBE_CONST_EXPR. */
1062 tree
1063 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1065 tree ret;
1066 tree eptype = NULL_TREE;
1067 bool dummy = true;
1068 bool maybe_const_itself = true;
1069 location_t loc = EXPR_LOCATION (expr);
1071 /* This function is not relevant to C++ because C++ folds while
1072 parsing, and may need changes to be correct for C++ when C++
1073 stops folding while parsing. */
1074 if (c_dialect_cxx ())
1075 gcc_unreachable ();
1077 if (!maybe_const)
1078 maybe_const = &dummy;
1079 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1081 eptype = TREE_TYPE (expr);
1082 expr = TREE_OPERAND (expr, 0);
1084 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1085 &maybe_const_itself);
1086 if (eptype)
1087 ret = fold_convert_loc (loc, eptype, ret);
1088 *maybe_const &= maybe_const_itself;
1089 return ret;
1092 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1093 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1094 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1095 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1096 both evaluated and unevaluated subexpressions while
1097 *MAYBE_CONST_ITSELF is carried from only evaluated
1098 subexpressions). */
1100 static tree
1101 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1102 bool *maybe_const_itself)
1104 tree ret = expr;
1105 enum tree_code code = TREE_CODE (expr);
1106 enum tree_code_class kind = TREE_CODE_CLASS (code);
1107 location_t loc = EXPR_LOCATION (expr);
1108 tree op0, op1, op2, op3;
1109 tree orig_op0, orig_op1, orig_op2;
1110 bool op0_const = true, op1_const = true, op2_const = true;
1111 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1112 bool nowarning = TREE_NO_WARNING (expr);
1113 bool unused_p;
1115 /* This function is not relevant to C++ because C++ folds while
1116 parsing, and may need changes to be correct for C++ when C++
1117 stops folding while parsing. */
1118 if (c_dialect_cxx ())
1119 gcc_unreachable ();
1121 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1122 anything else not counted as an expression cannot usefully be
1123 folded further at this point. */
1124 if (!IS_EXPR_CODE_CLASS (kind)
1125 || kind == tcc_statement
1126 || code == SAVE_EXPR)
1127 return expr;
1129 /* Operands of variable-length expressions (function calls) have
1130 already been folded, as have __builtin_* function calls, and such
1131 expressions cannot occur in constant expressions. */
1132 if (kind == tcc_vl_exp)
1134 *maybe_const_operands = false;
1135 ret = fold (expr);
1136 goto out;
1139 if (code == C_MAYBE_CONST_EXPR)
1141 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1142 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1143 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1144 *maybe_const_operands = false;
1145 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1146 *maybe_const_itself = false;
1147 if (pre && !in_init)
1148 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1149 else
1150 ret = inner;
1151 goto out;
1154 /* Assignment, increment, decrement, function call and comma
1155 operators, and statement expressions, cannot occur in constant
1156 expressions if evaluated / outside of sizeof. (Function calls
1157 were handled above, though VA_ARG_EXPR is treated like a function
1158 call here, and statement expressions are handled through
1159 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1160 switch (code)
1162 case MODIFY_EXPR:
1163 case PREDECREMENT_EXPR:
1164 case PREINCREMENT_EXPR:
1165 case POSTDECREMENT_EXPR:
1166 case POSTINCREMENT_EXPR:
1167 case COMPOUND_EXPR:
1168 *maybe_const_operands = false;
1169 break;
1171 case VA_ARG_EXPR:
1172 case TARGET_EXPR:
1173 case BIND_EXPR:
1174 case OBJ_TYPE_REF:
1175 *maybe_const_operands = false;
1176 ret = fold (expr);
1177 goto out;
1179 default:
1180 break;
1183 /* Fold individual tree codes as appropriate. */
1184 switch (code)
1186 case COMPOUND_LITERAL_EXPR:
1187 /* Any non-constancy will have been marked in a containing
1188 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1189 goto out;
1191 case COMPONENT_REF:
1192 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1193 op1 = TREE_OPERAND (expr, 1);
1194 op2 = TREE_OPERAND (expr, 2);
1195 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1196 maybe_const_itself);
1197 STRIP_TYPE_NOPS (op0);
1198 if (op0 != orig_op0)
1199 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1200 if (ret != expr)
1202 TREE_READONLY (ret) = TREE_READONLY (expr);
1203 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1205 goto out;
1207 case ARRAY_REF:
1208 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1209 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1210 op2 = TREE_OPERAND (expr, 2);
1211 op3 = TREE_OPERAND (expr, 3);
1212 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1213 maybe_const_itself);
1214 STRIP_TYPE_NOPS (op0);
1215 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1216 maybe_const_itself);
1217 STRIP_TYPE_NOPS (op1);
1218 op1 = decl_constant_value_for_optimization (op1);
1219 if (op0 != orig_op0 || op1 != orig_op1)
1220 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1221 if (ret != expr)
1223 TREE_READONLY (ret) = TREE_READONLY (expr);
1224 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1225 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1227 ret = fold (ret);
1228 goto out;
1230 case COMPOUND_EXPR:
1231 case MODIFY_EXPR:
1232 case PREDECREMENT_EXPR:
1233 case PREINCREMENT_EXPR:
1234 case POSTDECREMENT_EXPR:
1235 case POSTINCREMENT_EXPR:
1236 case PLUS_EXPR:
1237 case MINUS_EXPR:
1238 case MULT_EXPR:
1239 case POINTER_PLUS_EXPR:
1240 case TRUNC_DIV_EXPR:
1241 case CEIL_DIV_EXPR:
1242 case FLOOR_DIV_EXPR:
1243 case TRUNC_MOD_EXPR:
1244 case RDIV_EXPR:
1245 case EXACT_DIV_EXPR:
1246 case LSHIFT_EXPR:
1247 case RSHIFT_EXPR:
1248 case BIT_IOR_EXPR:
1249 case BIT_XOR_EXPR:
1250 case BIT_AND_EXPR:
1251 case LT_EXPR:
1252 case LE_EXPR:
1253 case GT_EXPR:
1254 case GE_EXPR:
1255 case EQ_EXPR:
1256 case NE_EXPR:
1257 case COMPLEX_EXPR:
1258 case TRUTH_AND_EXPR:
1259 case TRUTH_OR_EXPR:
1260 case TRUTH_XOR_EXPR:
1261 case UNORDERED_EXPR:
1262 case ORDERED_EXPR:
1263 case UNLT_EXPR:
1264 case UNLE_EXPR:
1265 case UNGT_EXPR:
1266 case UNGE_EXPR:
1267 case UNEQ_EXPR:
1268 /* Binary operations evaluating both arguments (increment and
1269 decrement are binary internally in GCC). */
1270 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1271 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1272 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1273 maybe_const_itself);
1274 STRIP_TYPE_NOPS (op0);
1275 if (code != MODIFY_EXPR
1276 && code != PREDECREMENT_EXPR
1277 && code != PREINCREMENT_EXPR
1278 && code != POSTDECREMENT_EXPR
1279 && code != POSTINCREMENT_EXPR)
1280 op0 = decl_constant_value_for_optimization (op0);
1281 /* The RHS of a MODIFY_EXPR was fully folded when building that
1282 expression for the sake of conversion warnings. */
1283 if (code != MODIFY_EXPR)
1284 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1285 maybe_const_itself);
1286 STRIP_TYPE_NOPS (op1);
1287 op1 = decl_constant_value_for_optimization (op1);
1288 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1289 ret = in_init
1290 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1291 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1292 else
1293 ret = fold (expr);
1294 if (TREE_OVERFLOW_P (ret)
1295 && !TREE_OVERFLOW_P (op0)
1296 && !TREE_OVERFLOW_P (op1))
1297 overflow_warning (EXPR_LOCATION (expr), ret);
1298 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1299 && TREE_CODE (orig_op1) != INTEGER_CST
1300 && TREE_CODE (op1) == INTEGER_CST
1301 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1302 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1303 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1304 && c_inhibit_evaluation_warnings == 0)
1306 if (tree_int_cst_sgn (op1) < 0)
1307 warning_at (loc, 0, (code == LSHIFT_EXPR
1308 ? G_("left shift count is negative")
1309 : G_("right shift count is negative")));
1310 else if (compare_tree_int (op1,
1311 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1312 >= 0)
1313 warning_at (loc, 0, (code == LSHIFT_EXPR
1314 ? G_("left shift count >= width of type")
1315 : G_("right shift count >= width of type")));
1317 goto out;
1319 case INDIRECT_REF:
1320 case FIX_TRUNC_EXPR:
1321 case FLOAT_EXPR:
1322 CASE_CONVERT:
1323 case VIEW_CONVERT_EXPR:
1324 case NON_LVALUE_EXPR:
1325 case NEGATE_EXPR:
1326 case BIT_NOT_EXPR:
1327 case TRUTH_NOT_EXPR:
1328 case ADDR_EXPR:
1329 case CONJ_EXPR:
1330 case REALPART_EXPR:
1331 case IMAGPART_EXPR:
1332 /* Unary operations. */
1333 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1334 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1335 maybe_const_itself);
1336 STRIP_TYPE_NOPS (op0);
1337 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1338 op0 = decl_constant_value_for_optimization (op0);
1339 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1340 not prepared to deal with them if they occur in initializers.
1341 Avoid attempts to fold references to UPC shared components
1342 due to the complexities of UPC pointer-to-shared arithmetic. */
1343 if (op0 != orig_op0
1344 && code == ADDR_EXPR
1345 && (op1 = get_base_address (op0)) != NULL_TREE
1346 && TREE_CODE (op1) == INDIRECT_REF
1347 && !upc_shared_type_p (TREE_TYPE (op1))
1348 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1349 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1350 else if (op0 != orig_op0 || in_init)
1351 ret = in_init
1352 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1353 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1354 else
1355 ret = fold (expr);
1356 if (code == INDIRECT_REF
1357 && ret != expr
1358 && TREE_CODE (ret) == INDIRECT_REF)
1360 TREE_READONLY (ret) = TREE_READONLY (expr);
1361 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1362 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1364 switch (code)
1366 case FIX_TRUNC_EXPR:
1367 case FLOAT_EXPR:
1368 CASE_CONVERT:
1369 /* Don't warn about explicit conversions. We will already
1370 have warned about suspect implicit conversions. */
1371 break;
1373 default:
1374 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1375 overflow_warning (EXPR_LOCATION (expr), ret);
1376 break;
1378 goto out;
1380 case TRUTH_ANDIF_EXPR:
1381 case TRUTH_ORIF_EXPR:
1382 /* Binary operations not necessarily evaluating both
1383 arguments. */
1384 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1385 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1386 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1387 STRIP_TYPE_NOPS (op0);
1389 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1390 ? truthvalue_false_node
1391 : truthvalue_true_node));
1392 c_disable_warnings (unused_p);
1393 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1394 STRIP_TYPE_NOPS (op1);
1395 c_enable_warnings (unused_p);
1397 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1398 ret = in_init
1399 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1400 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1401 else
1402 ret = fold (expr);
1403 *maybe_const_operands &= op0_const;
1404 *maybe_const_itself &= op0_const_self;
1405 if (!(flag_isoc99
1406 && op0_const
1407 && op0_const_self
1408 && (code == TRUTH_ANDIF_EXPR
1409 ? op0 == truthvalue_false_node
1410 : op0 == truthvalue_true_node)))
1411 *maybe_const_operands &= op1_const;
1412 if (!(op0_const
1413 && op0_const_self
1414 && (code == TRUTH_ANDIF_EXPR
1415 ? op0 == truthvalue_false_node
1416 : op0 == truthvalue_true_node)))
1417 *maybe_const_itself &= op1_const_self;
1418 goto out;
1420 case COND_EXPR:
1421 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1422 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1423 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1424 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1426 STRIP_TYPE_NOPS (op0);
1427 c_disable_warnings (op0 == truthvalue_false_node);
1428 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1429 STRIP_TYPE_NOPS (op1);
1430 c_enable_warnings (op0 == truthvalue_false_node);
1432 c_disable_warnings (op0 == truthvalue_true_node);
1433 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1434 STRIP_TYPE_NOPS (op2);
1435 c_enable_warnings (op0 == truthvalue_true_node);
1437 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1438 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1439 else
1440 ret = fold (expr);
1441 *maybe_const_operands &= op0_const;
1442 *maybe_const_itself &= op0_const_self;
1443 if (!(flag_isoc99
1444 && op0_const
1445 && op0_const_self
1446 && op0 == truthvalue_false_node))
1447 *maybe_const_operands &= op1_const;
1448 if (!(op0_const
1449 && op0_const_self
1450 && op0 == truthvalue_false_node))
1451 *maybe_const_itself &= op1_const_self;
1452 if (!(flag_isoc99
1453 && op0_const
1454 && op0_const_self
1455 && op0 == truthvalue_true_node))
1456 *maybe_const_operands &= op2_const;
1457 if (!(op0_const
1458 && op0_const_self
1459 && op0 == truthvalue_true_node))
1460 *maybe_const_itself &= op2_const_self;
1461 goto out;
1463 case EXCESS_PRECISION_EXPR:
1464 /* Each case where an operand with excess precision may be
1465 encountered must remove the EXCESS_PRECISION_EXPR around
1466 inner operands and possibly put one around the whole
1467 expression or possibly convert to the semantic type (which
1468 c_fully_fold does); we cannot tell at this stage which is
1469 appropriate in any particular case. */
1470 gcc_unreachable ();
1472 default:
1473 /* Various codes may appear through folding built-in functions
1474 and their arguments. */
1475 goto out;
1478 out:
1479 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1480 have been done by this point, so remove them again. */
1481 nowarning |= TREE_NO_WARNING (ret);
1482 STRIP_TYPE_NOPS (ret);
1483 if (nowarning && !TREE_NO_WARNING (ret))
1485 if (!CAN_HAVE_LOCATION_P (ret))
1486 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1487 TREE_NO_WARNING (ret) = 1;
1489 if (ret != expr)
1490 protected_set_expr_location (ret, loc);
1491 return ret;
1494 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1495 return EXP. Otherwise, return either EXP or its known constant
1496 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1497 Is the BLKmode test appropriate? */
1499 tree
1500 decl_constant_value_for_optimization (tree exp)
1502 tree ret;
1504 /* This function is only used by C, for c_fully_fold and other
1505 optimization, and may not be correct for C++. */
1506 if (c_dialect_cxx ())
1507 gcc_unreachable ();
1509 if (!optimize
1510 || TREE_CODE (exp) != VAR_DECL
1511 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1512 || DECL_MODE (exp) == BLKmode)
1513 return exp;
1515 ret = decl_constant_value (exp);
1516 /* Avoid unwanted tree sharing between the initializer and current
1517 function's body where the tree can be modified e.g. by the
1518 gimplifier. */
1519 if (ret != exp && TREE_STATIC (exp))
1520 ret = unshare_expr (ret);
1521 return ret;
1524 /* Print a warning if a constant expression had overflow in folding.
1525 Invoke this function on every expression that the language
1526 requires to be a constant expression.
1527 Note the ANSI C standard says it is erroneous for a
1528 constant expression to overflow. */
1530 void
1531 constant_expression_warning (tree value)
1533 if (warn_overflow && pedantic
1534 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1535 || TREE_CODE (value) == FIXED_CST
1536 || TREE_CODE (value) == VECTOR_CST
1537 || TREE_CODE (value) == COMPLEX_CST)
1538 && TREE_OVERFLOW (value))
1539 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1542 /* The same as above but print an unconditional error. */
1543 void
1544 constant_expression_error (tree value)
1546 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1547 || TREE_CODE (value) == FIXED_CST
1548 || TREE_CODE (value) == VECTOR_CST
1549 || TREE_CODE (value) == COMPLEX_CST)
1550 && TREE_OVERFLOW (value))
1551 error ("overflow in constant expression");
1554 /* Print a warning if an expression had overflow in folding and its
1555 operands hadn't.
1557 Invoke this function on every expression that
1558 (1) appears in the source code, and
1559 (2) is a constant expression that overflowed, and
1560 (3) is not already checked by convert_and_check;
1561 however, do not invoke this function on operands of explicit casts
1562 or when the expression is the result of an operator and any operand
1563 already overflowed. */
1565 void
1566 overflow_warning (location_t loc, tree value)
1568 if (c_inhibit_evaluation_warnings != 0)
1569 return;
1571 switch (TREE_CODE (value))
1573 case INTEGER_CST:
1574 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1575 break;
1577 case REAL_CST:
1578 warning_at (loc, OPT_Woverflow,
1579 "floating point overflow in expression");
1580 break;
1582 case FIXED_CST:
1583 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1584 break;
1586 case VECTOR_CST:
1587 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1588 break;
1590 case COMPLEX_CST:
1591 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1592 warning_at (loc, OPT_Woverflow,
1593 "complex integer overflow in expression");
1594 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1595 warning_at (loc, OPT_Woverflow,
1596 "complex floating point overflow in expression");
1597 break;
1599 default:
1600 break;
1604 /* Warn about uses of logical || / && operator in a context where it
1605 is likely that the bitwise equivalent was intended by the
1606 programmer. We have seen an expression in which CODE is a binary
1607 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1608 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1609 void
1610 warn_logical_operator (location_t location, enum tree_code code, tree type,
1611 enum tree_code code_left, tree op_left,
1612 enum tree_code ARG_UNUSED (code_right), tree op_right)
1614 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1615 int in0_p, in1_p, in_p;
1616 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1617 bool strict_overflow_p = false;
1619 if (code != TRUTH_ANDIF_EXPR
1620 && code != TRUTH_AND_EXPR
1621 && code != TRUTH_ORIF_EXPR
1622 && code != TRUTH_OR_EXPR)
1623 return;
1625 /* Warn if &&/|| are being used in a context where it is
1626 likely that the bitwise equivalent was intended by the
1627 programmer. That is, an expression such as op && MASK
1628 where op should not be any boolean expression, nor a
1629 constant, and mask seems to be a non-boolean integer constant. */
1630 if (!truth_value_p (code_left)
1631 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1632 && !CONSTANT_CLASS_P (op_left)
1633 && !TREE_NO_WARNING (op_left)
1634 && TREE_CODE (op_right) == INTEGER_CST
1635 && !integer_zerop (op_right)
1636 && !integer_onep (op_right))
1638 if (or_op)
1639 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1640 " applied to non-boolean constant");
1641 else
1642 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1643 " applied to non-boolean constant");
1644 TREE_NO_WARNING (op_left) = true;
1645 return;
1648 /* We do not warn for constants because they are typical of macro
1649 expansions that test for features. */
1650 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1651 return;
1653 /* This warning only makes sense with logical operands. */
1654 if (!(truth_value_p (TREE_CODE (op_left))
1655 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1656 || !(truth_value_p (TREE_CODE (op_right))
1657 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1658 return;
1661 /* We first test whether either side separately is trivially true
1662 (with OR) or trivially false (with AND). If so, do not warn.
1663 This is a common idiom for testing ranges of data types in
1664 portable code. */
1665 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1666 if (!lhs)
1667 return;
1668 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1669 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1671 /* If this is an OR operation, invert both sides; now, the result
1672 should be always false to get a warning. */
1673 if (or_op)
1674 in0_p = !in0_p;
1676 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1677 if (tem && integer_zerop (tem))
1678 return;
1680 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1681 if (!rhs)
1682 return;
1683 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1684 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1686 /* If this is an OR operation, invert both sides; now, the result
1687 should be always false to get a warning. */
1688 if (or_op)
1689 in1_p = !in1_p;
1691 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1692 if (tem && integer_zerop (tem))
1693 return;
1695 /* If both expressions have the same operand, if we can merge the
1696 ranges, and if the range test is always false, then warn. */
1697 if (operand_equal_p (lhs, rhs, 0)
1698 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1699 in1_p, low1, high1)
1700 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1701 type, lhs, in_p, low, high))
1702 && integer_zerop (tem))
1704 if (or_op)
1705 warning_at (location, OPT_Wlogical_op,
1706 "logical %<or%> "
1707 "of collectively exhaustive tests is always true");
1708 else
1709 warning_at (location, OPT_Wlogical_op,
1710 "logical %<and%> "
1711 "of mutually exclusive tests is always false");
1716 /* Warn if EXP contains any computations whose results are not used.
1717 Return true if a warning is printed; false otherwise. LOCUS is the
1718 (potential) location of the expression. */
1720 bool
1721 warn_if_unused_value (const_tree exp, location_t locus)
1723 restart:
1724 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1725 return false;
1727 /* Don't warn about void constructs. This includes casting to void,
1728 void function calls, and statement expressions with a final cast
1729 to void. */
1730 if (VOID_TYPE_P (TREE_TYPE (exp)))
1731 return false;
1733 if (EXPR_HAS_LOCATION (exp))
1734 locus = EXPR_LOCATION (exp);
1736 switch (TREE_CODE (exp))
1738 case PREINCREMENT_EXPR:
1739 case POSTINCREMENT_EXPR:
1740 case PREDECREMENT_EXPR:
1741 case POSTDECREMENT_EXPR:
1742 case MODIFY_EXPR:
1743 case INIT_EXPR:
1744 case TARGET_EXPR:
1745 case CALL_EXPR:
1746 case TRY_CATCH_EXPR:
1747 case WITH_CLEANUP_EXPR:
1748 case EXIT_EXPR:
1749 case VA_ARG_EXPR:
1750 return false;
1752 case BIND_EXPR:
1753 /* For a binding, warn if no side effect within it. */
1754 exp = BIND_EXPR_BODY (exp);
1755 goto restart;
1757 case SAVE_EXPR:
1758 case NON_LVALUE_EXPR:
1759 case NOP_EXPR:
1760 exp = TREE_OPERAND (exp, 0);
1761 goto restart;
1763 case TRUTH_ORIF_EXPR:
1764 case TRUTH_ANDIF_EXPR:
1765 /* In && or ||, warn if 2nd operand has no side effect. */
1766 exp = TREE_OPERAND (exp, 1);
1767 goto restart;
1769 case COMPOUND_EXPR:
1770 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1771 return true;
1772 /* Let people do `(foo (), 0)' without a warning. */
1773 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1774 return false;
1775 exp = TREE_OPERAND (exp, 1);
1776 goto restart;
1778 case COND_EXPR:
1779 /* If this is an expression with side effects, don't warn; this
1780 case commonly appears in macro expansions. */
1781 if (TREE_SIDE_EFFECTS (exp))
1782 return false;
1783 goto warn;
1785 case INDIRECT_REF:
1786 /* Don't warn about automatic dereferencing of references, since
1787 the user cannot control it. */
1788 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1790 exp = TREE_OPERAND (exp, 0);
1791 goto restart;
1793 /* Fall through. */
1795 default:
1796 /* Referencing a volatile value is a side effect, so don't warn. */
1797 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1798 && TREE_THIS_VOLATILE (exp))
1799 return false;
1801 /* If this is an expression which has no operands, there is no value
1802 to be unused. There are no such language-independent codes,
1803 but front ends may define such. */
1804 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1805 return false;
1807 warn:
1808 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1813 /* Print a warning about casts that might indicate violation
1814 of strict aliasing rules if -Wstrict-aliasing is used and
1815 strict aliasing mode is in effect. OTYPE is the original
1816 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1818 bool
1819 strict_aliasing_warning (tree otype, tree type, tree expr)
1821 /* Strip pointer conversion chains and get to the correct original type. */
1822 STRIP_NOPS (expr);
1823 otype = TREE_TYPE (expr);
1825 if (!(flag_strict_aliasing
1826 && POINTER_TYPE_P (type)
1827 && POINTER_TYPE_P (otype)
1828 && !VOID_TYPE_P (TREE_TYPE (type)))
1829 /* If the type we are casting to is a ref-all pointer
1830 dereferencing it is always valid. */
1831 || TYPE_REF_CAN_ALIAS_ALL (type))
1832 return false;
1834 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1835 && (DECL_P (TREE_OPERAND (expr, 0))
1836 || handled_component_p (TREE_OPERAND (expr, 0))))
1838 /* Casting the address of an object to non void pointer. Warn
1839 if the cast breaks type based aliasing. */
1840 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1842 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1843 "might break strict-aliasing rules");
1844 return true;
1846 else
1848 /* warn_strict_aliasing >= 3. This includes the default (3).
1849 Only warn if the cast is dereferenced immediately. */
1850 alias_set_type set1 =
1851 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1852 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1854 if (set1 != set2 && set2 != 0
1855 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1857 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1858 "pointer will break strict-aliasing rules");
1859 return true;
1861 else if (warn_strict_aliasing == 2
1862 && !alias_sets_must_conflict_p (set1, set2))
1864 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1865 "pointer might break strict-aliasing rules");
1866 return true;
1870 else
1871 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1873 /* At this level, warn for any conversions, even if an address is
1874 not taken in the same statement. This will likely produce many
1875 false positives, but could be useful to pinpoint problems that
1876 are not revealed at higher levels. */
1877 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1878 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1879 if (!COMPLETE_TYPE_P (type)
1880 || !alias_sets_must_conflict_p (set1, set2))
1882 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1883 "pointer might break strict-aliasing rules");
1884 return true;
1888 return false;
1891 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1892 sizeof as last operand of certain builtins. */
1894 void
1895 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1896 vec<tree, va_gc> *params, tree *sizeof_arg,
1897 bool (*comp_types) (tree, tree))
1899 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1900 bool strop = false, cmp = false;
1901 unsigned int idx = ~0;
1902 location_t loc;
1904 if (TREE_CODE (callee) != FUNCTION_DECL
1905 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1906 || vec_safe_length (params) <= 1)
1907 return;
1909 switch (DECL_FUNCTION_CODE (callee))
1911 case BUILT_IN_STRNCMP:
1912 case BUILT_IN_STRNCASECMP:
1913 cmp = true;
1914 /* FALLTHRU */
1915 case BUILT_IN_STRNCPY:
1916 case BUILT_IN_STRNCPY_CHK:
1917 case BUILT_IN_STRNCAT:
1918 case BUILT_IN_STRNCAT_CHK:
1919 case BUILT_IN_STPNCPY:
1920 case BUILT_IN_STPNCPY_CHK:
1921 strop = true;
1922 /* FALLTHRU */
1923 case BUILT_IN_MEMCPY:
1924 case BUILT_IN_MEMCPY_CHK:
1925 case BUILT_IN_MEMMOVE:
1926 case BUILT_IN_MEMMOVE_CHK:
1927 if (params->length () < 3)
1928 return;
1929 src = (*params)[1];
1930 dest = (*params)[0];
1931 idx = 2;
1932 break;
1933 case BUILT_IN_BCOPY:
1934 if (params->length () < 3)
1935 return;
1936 src = (*params)[0];
1937 dest = (*params)[1];
1938 idx = 2;
1939 break;
1940 case BUILT_IN_MEMCMP:
1941 case BUILT_IN_BCMP:
1942 if (params->length () < 3)
1943 return;
1944 src = (*params)[1];
1945 dest = (*params)[0];
1946 idx = 2;
1947 cmp = true;
1948 break;
1949 case BUILT_IN_MEMSET:
1950 case BUILT_IN_MEMSET_CHK:
1951 if (params->length () < 3)
1952 return;
1953 dest = (*params)[0];
1954 idx = 2;
1955 break;
1956 case BUILT_IN_BZERO:
1957 dest = (*params)[0];
1958 idx = 1;
1959 break;
1960 case BUILT_IN_STRNDUP:
1961 src = (*params)[0];
1962 strop = true;
1963 idx = 1;
1964 break;
1965 case BUILT_IN_MEMCHR:
1966 if (params->length () < 3)
1967 return;
1968 src = (*params)[0];
1969 idx = 2;
1970 break;
1971 case BUILT_IN_SNPRINTF:
1972 case BUILT_IN_SNPRINTF_CHK:
1973 case BUILT_IN_VSNPRINTF:
1974 case BUILT_IN_VSNPRINTF_CHK:
1975 dest = (*params)[0];
1976 idx = 1;
1977 strop = true;
1978 break;
1979 default:
1980 break;
1983 if (idx >= 3)
1984 return;
1986 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1987 return;
1989 type = TYPE_P (sizeof_arg[idx])
1990 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1991 if (!POINTER_TYPE_P (type))
1992 return;
1994 if (dest
1995 && (tem = tree_strip_nop_conversions (dest))
1996 && POINTER_TYPE_P (TREE_TYPE (tem))
1997 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1998 return;
2000 if (src
2001 && (tem = tree_strip_nop_conversions (src))
2002 && POINTER_TYPE_P (TREE_TYPE (tem))
2003 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2004 return;
2006 loc = sizeof_arg_loc[idx];
2008 if (dest && !cmp)
2010 if (!TYPE_P (sizeof_arg[idx])
2011 && operand_equal_p (dest, sizeof_arg[idx], 0)
2012 && comp_types (TREE_TYPE (dest), type))
2014 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2015 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2016 "argument to %<sizeof%> in %qD call is the same "
2017 "expression as the destination; did you mean to "
2018 "remove the addressof?", callee);
2019 else if ((TYPE_PRECISION (TREE_TYPE (type))
2020 == TYPE_PRECISION (char_type_node))
2021 || strop)
2022 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2023 "argument to %<sizeof%> in %qD call is the same "
2024 "expression as the destination; did you mean to "
2025 "provide an explicit length?", callee);
2026 else
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 "dereference it?", callee);
2031 return;
2034 if (POINTER_TYPE_P (TREE_TYPE (dest))
2035 && !strop
2036 && comp_types (TREE_TYPE (dest), type)
2037 && !VOID_TYPE_P (TREE_TYPE (type)))
2039 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2040 "argument to %<sizeof%> in %qD call is the same "
2041 "pointer type %qT as the destination; expected %qT "
2042 "or an explicit length", callee, TREE_TYPE (dest),
2043 TREE_TYPE (TREE_TYPE (dest)));
2044 return;
2048 if (src && !cmp)
2050 if (!TYPE_P (sizeof_arg[idx])
2051 && operand_equal_p (src, sizeof_arg[idx], 0)
2052 && comp_types (TREE_TYPE (src), type))
2054 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2055 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2056 "argument to %<sizeof%> in %qD call is the same "
2057 "expression as the source; did you mean to "
2058 "remove the addressof?", callee);
2059 else if ((TYPE_PRECISION (TREE_TYPE (type))
2060 == TYPE_PRECISION (char_type_node))
2061 || strop)
2062 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2063 "argument to %<sizeof%> in %qD call is the same "
2064 "expression as the source; did you mean to "
2065 "provide an explicit length?", callee);
2066 else
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 "dereference it?", callee);
2071 return;
2074 if (POINTER_TYPE_P (TREE_TYPE (src))
2075 && !strop
2076 && comp_types (TREE_TYPE (src), type)
2077 && !VOID_TYPE_P (TREE_TYPE (type)))
2079 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2080 "argument to %<sizeof%> in %qD call is the same "
2081 "pointer type %qT as the source; expected %qT "
2082 "or an explicit length", callee, TREE_TYPE (src),
2083 TREE_TYPE (TREE_TYPE (src)));
2084 return;
2088 if (dest)
2090 if (!TYPE_P (sizeof_arg[idx])
2091 && operand_equal_p (dest, sizeof_arg[idx], 0)
2092 && comp_types (TREE_TYPE (dest), type))
2094 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2095 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2096 "argument to %<sizeof%> in %qD call is the same "
2097 "expression as the first source; did you mean to "
2098 "remove the addressof?", callee);
2099 else if ((TYPE_PRECISION (TREE_TYPE (type))
2100 == TYPE_PRECISION (char_type_node))
2101 || strop)
2102 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2103 "argument to %<sizeof%> in %qD call is the same "
2104 "expression as the first source; did you mean to "
2105 "provide an explicit length?", callee);
2106 else
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 "dereference it?", callee);
2111 return;
2114 if (POINTER_TYPE_P (TREE_TYPE (dest))
2115 && !strop
2116 && comp_types (TREE_TYPE (dest), type)
2117 && !VOID_TYPE_P (TREE_TYPE (type)))
2119 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2120 "argument to %<sizeof%> in %qD call is the same "
2121 "pointer type %qT as the first source; expected %qT "
2122 "or an explicit length", callee, TREE_TYPE (dest),
2123 TREE_TYPE (TREE_TYPE (dest)));
2124 return;
2128 if (src)
2130 if (!TYPE_P (sizeof_arg[idx])
2131 && operand_equal_p (src, sizeof_arg[idx], 0)
2132 && comp_types (TREE_TYPE (src), type))
2134 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2135 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2136 "argument to %<sizeof%> in %qD call is the same "
2137 "expression as the second source; did you mean to "
2138 "remove the addressof?", callee);
2139 else if ((TYPE_PRECISION (TREE_TYPE (type))
2140 == TYPE_PRECISION (char_type_node))
2141 || strop)
2142 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2143 "argument to %<sizeof%> in %qD call is the same "
2144 "expression as the second source; did you mean to "
2145 "provide an explicit length?", callee);
2146 else
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 "dereference it?", callee);
2151 return;
2154 if (POINTER_TYPE_P (TREE_TYPE (src))
2155 && !strop
2156 && comp_types (TREE_TYPE (src), type)
2157 && !VOID_TYPE_P (TREE_TYPE (type)))
2159 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2160 "argument to %<sizeof%> in %qD call is the same "
2161 "pointer type %qT as the second source; expected %qT "
2162 "or an explicit length", callee, TREE_TYPE (src),
2163 TREE_TYPE (TREE_TYPE (src)));
2164 return;
2170 /* Warn for unlikely, improbable, or stupid DECL declarations
2171 of `main'. */
2173 void
2174 check_main_parameter_types (tree decl)
2176 function_args_iterator iter;
2177 tree type;
2178 int argct = 0;
2180 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2182 /* XXX void_type_node belies the abstraction. */
2183 if (type == void_type_node || type == error_mark_node )
2184 break;
2186 ++argct;
2187 switch (argct)
2189 case 1:
2190 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2191 pedwarn (input_location, OPT_Wmain,
2192 "first argument of %q+D should be %<int%>", decl);
2193 break;
2195 case 2:
2196 if (TREE_CODE (type) != POINTER_TYPE
2197 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2198 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2199 != char_type_node))
2200 pedwarn (input_location, OPT_Wmain,
2201 "second argument of %q+D should be %<char **%>", decl);
2202 break;
2204 case 3:
2205 if (TREE_CODE (type) != POINTER_TYPE
2206 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2207 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2208 != char_type_node))
2209 pedwarn (input_location, OPT_Wmain,
2210 "third argument of %q+D should probably be "
2211 "%<char **%>", decl);
2212 break;
2216 /* It is intentional that this message does not mention the third
2217 argument because it's only mentioned in an appendix of the
2218 standard. */
2219 if (argct > 0 && (argct < 2 || argct > 3))
2220 pedwarn (input_location, OPT_Wmain,
2221 "%q+D takes only zero or two arguments", decl);
2224 /* True if pointers to distinct types T1 and T2 can be converted to
2225 each other without an explicit cast. Only returns true for opaque
2226 vector types. */
2227 bool
2228 vector_targets_convertible_p (const_tree t1, const_tree t2)
2230 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2231 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2232 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2233 return true;
2235 return false;
2238 /* True if vector types T1 and T2 can be converted to each other
2239 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2240 can only be converted with -flax-vector-conversions yet that is not
2241 in effect, emit a note telling the user about that option if such
2242 a note has not previously been emitted. */
2243 bool
2244 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2246 static bool emitted_lax_note = false;
2247 bool convertible_lax;
2249 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2250 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2251 return true;
2253 convertible_lax =
2254 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2255 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2256 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2257 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2258 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2260 if (!convertible_lax || flag_lax_vector_conversions)
2261 return convertible_lax;
2263 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2264 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2265 return true;
2267 if (emit_lax_note && !emitted_lax_note)
2269 emitted_lax_note = true;
2270 inform (input_location, "use -flax-vector-conversions to permit "
2271 "conversions between vectors with differing "
2272 "element types or numbers of subparts");
2275 return false;
2278 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2279 and have vector types, V0 has the same type as V1, and the number of
2280 elements of V0, V1, MASK is the same.
2282 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2283 called with two arguments. In this case implementation passes the
2284 first argument twice in order to share the same tree code. This fact
2285 could enable the mask-values being twice the vector length. This is
2286 an implementation accident and this semantics is not guaranteed to
2287 the user. */
2288 tree
2289 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2291 tree ret;
2292 bool wrap = true;
2293 bool maybe_const = false;
2294 bool two_arguments = false;
2296 if (v1 == NULL_TREE)
2298 two_arguments = true;
2299 v1 = v0;
2302 if (v0 == error_mark_node || v1 == error_mark_node
2303 || mask == error_mark_node)
2304 return error_mark_node;
2306 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2307 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2309 error_at (loc, "__builtin_shuffle last argument must "
2310 "be an integer vector");
2311 return error_mark_node;
2314 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2315 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2317 error_at (loc, "__builtin_shuffle arguments must be vectors");
2318 return error_mark_node;
2321 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2323 error_at (loc, "__builtin_shuffle argument vectors must be of "
2324 "the same type");
2325 return error_mark_node;
2328 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2329 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2330 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2331 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2333 error_at (loc, "__builtin_shuffle number of elements of the "
2334 "argument vector(s) and the mask vector should "
2335 "be the same");
2336 return error_mark_node;
2339 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2340 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2342 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2343 "must have the same size as inner type of the mask");
2344 return error_mark_node;
2347 if (!c_dialect_cxx ())
2349 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2350 v0 = c_fully_fold (v0, false, &maybe_const);
2351 wrap &= maybe_const;
2353 if (two_arguments)
2354 v1 = v0 = save_expr (v0);
2355 else
2357 v1 = c_fully_fold (v1, false, &maybe_const);
2358 wrap &= maybe_const;
2361 mask = c_fully_fold (mask, false, &maybe_const);
2362 wrap &= maybe_const;
2365 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2367 if (!c_dialect_cxx () && !wrap)
2368 ret = c_wrap_maybe_const (ret, true);
2370 return ret;
2373 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2374 to integral type. */
2376 static tree
2377 c_common_get_narrower (tree op, int *unsignedp_ptr)
2379 op = get_narrower (op, unsignedp_ptr);
2381 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2382 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2384 /* C++0x scoped enumerations don't implicitly convert to integral
2385 type; if we stripped an explicit conversion to a larger type we
2386 need to replace it so common_type will still work. */
2387 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2388 TYPE_UNSIGNED (TREE_TYPE (op)));
2389 op = fold_convert (type, op);
2391 return op;
2394 /* This is a helper function of build_binary_op.
2396 For certain operations if both args were extended from the same
2397 smaller type, do the arithmetic in that type and then extend.
2399 BITWISE indicates a bitwise operation.
2400 For them, this optimization is safe only if
2401 both args are zero-extended or both are sign-extended.
2402 Otherwise, we might change the result.
2403 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2404 but calculated in (unsigned short) it would be (unsigned short)-1.
2406 tree
2407 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2409 int unsigned0, unsigned1;
2410 tree arg0, arg1;
2411 int uns;
2412 tree type;
2414 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2415 excessive narrowing when we call get_narrower below. For
2416 example, suppose that OP0 is of unsigned int extended
2417 from signed char and that RESULT_TYPE is long long int.
2418 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2419 like
2421 (long long int) (unsigned int) signed_char
2423 which get_narrower would narrow down to
2425 (unsigned int) signed char
2427 If we do not cast OP0 first, get_narrower would return
2428 signed_char, which is inconsistent with the case of the
2429 explicit cast. */
2430 op0 = convert (result_type, op0);
2431 op1 = convert (result_type, op1);
2433 arg0 = c_common_get_narrower (op0, &unsigned0);
2434 arg1 = c_common_get_narrower (op1, &unsigned1);
2436 /* UNS is 1 if the operation to be done is an unsigned one. */
2437 uns = TYPE_UNSIGNED (result_type);
2439 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2440 but it *requires* conversion to FINAL_TYPE. */
2442 if ((TYPE_PRECISION (TREE_TYPE (op0))
2443 == TYPE_PRECISION (TREE_TYPE (arg0)))
2444 && TREE_TYPE (op0) != result_type)
2445 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2446 if ((TYPE_PRECISION (TREE_TYPE (op1))
2447 == TYPE_PRECISION (TREE_TYPE (arg1)))
2448 && TREE_TYPE (op1) != result_type)
2449 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2451 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2453 /* For bitwise operations, signedness of nominal type
2454 does not matter. Consider only how operands were extended. */
2455 if (bitwise)
2456 uns = unsigned0;
2458 /* Note that in all three cases below we refrain from optimizing
2459 an unsigned operation on sign-extended args.
2460 That would not be valid. */
2462 /* Both args variable: if both extended in same way
2463 from same width, do it in that width.
2464 Do it unsigned if args were zero-extended. */
2465 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2466 < TYPE_PRECISION (result_type))
2467 && (TYPE_PRECISION (TREE_TYPE (arg1))
2468 == TYPE_PRECISION (TREE_TYPE (arg0)))
2469 && unsigned0 == unsigned1
2470 && (unsigned0 || !uns))
2471 return c_common_signed_or_unsigned_type
2472 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2474 else if (TREE_CODE (arg0) == INTEGER_CST
2475 && (unsigned1 || !uns)
2476 && (TYPE_PRECISION (TREE_TYPE (arg1))
2477 < TYPE_PRECISION (result_type))
2478 && (type
2479 = c_common_signed_or_unsigned_type (unsigned1,
2480 TREE_TYPE (arg1)))
2481 && !POINTER_TYPE_P (type)
2482 && int_fits_type_p (arg0, type))
2483 return type;
2485 else if (TREE_CODE (arg1) == INTEGER_CST
2486 && (unsigned0 || !uns)
2487 && (TYPE_PRECISION (TREE_TYPE (arg0))
2488 < TYPE_PRECISION (result_type))
2489 && (type
2490 = c_common_signed_or_unsigned_type (unsigned0,
2491 TREE_TYPE (arg0)))
2492 && !POINTER_TYPE_P (type)
2493 && int_fits_type_p (arg1, type))
2494 return type;
2496 return result_type;
2499 /* Checks if expression EXPR of real/integer type cannot be converted
2500 to the real/integer type TYPE. Function returns true when:
2501 * EXPR is a constant which cannot be exactly converted to TYPE
2502 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2503 for EXPR type and TYPE being both integers or both real.
2504 * EXPR is not a constant of real type and TYPE is an integer.
2505 * EXPR is not a constant of integer type which cannot be
2506 exactly converted to real type.
2507 Function allows conversions between types of different signedness and
2508 does not return true in that case. Function can produce signedness
2509 warnings if PRODUCE_WARNS is true. */
2510 bool
2511 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2513 bool give_warning = false;
2514 tree expr_type = TREE_TYPE (expr);
2515 location_t loc = EXPR_LOC_OR_HERE (expr);
2517 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2519 /* Warn for real constant that is not an exact integer converted
2520 to integer type. */
2521 if (TREE_CODE (expr_type) == REAL_TYPE
2522 && TREE_CODE (type) == INTEGER_TYPE)
2524 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2525 give_warning = true;
2527 /* Warn for an integer constant that does not fit into integer type. */
2528 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2529 && TREE_CODE (type) == INTEGER_TYPE
2530 && !int_fits_type_p (expr, type))
2532 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2533 && tree_int_cst_sgn (expr) < 0)
2535 if (produce_warns)
2536 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2537 " implicitly converted to unsigned type");
2539 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2541 if (produce_warns)
2542 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2543 " constant value to negative integer");
2545 else
2546 give_warning = true;
2548 else if (TREE_CODE (type) == REAL_TYPE)
2550 /* Warn for an integer constant that does not fit into real type. */
2551 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2553 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2554 if (!exact_real_truncate (TYPE_MODE (type), &a))
2555 give_warning = true;
2557 /* Warn for a real constant that does not fit into a smaller
2558 real type. */
2559 else if (TREE_CODE (expr_type) == REAL_TYPE
2560 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2562 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2563 if (!exact_real_truncate (TYPE_MODE (type), &a))
2564 give_warning = true;
2568 else
2570 /* Warn for real types converted to integer types. */
2571 if (TREE_CODE (expr_type) == REAL_TYPE
2572 && TREE_CODE (type) == INTEGER_TYPE)
2573 give_warning = true;
2575 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2576 && TREE_CODE (type) == INTEGER_TYPE)
2578 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2579 expr = get_unwidened (expr, 0);
2580 expr_type = TREE_TYPE (expr);
2582 /* Don't warn for short y; short x = ((int)y & 0xff); */
2583 if (TREE_CODE (expr) == BIT_AND_EXPR
2584 || TREE_CODE (expr) == BIT_IOR_EXPR
2585 || TREE_CODE (expr) == BIT_XOR_EXPR)
2587 /* If both args were extended from a shortest type,
2588 use that type if that is safe. */
2589 expr_type = shorten_binary_op (expr_type,
2590 TREE_OPERAND (expr, 0),
2591 TREE_OPERAND (expr, 1),
2592 /* bitwise */1);
2594 if (TREE_CODE (expr) == BIT_AND_EXPR)
2596 tree op0 = TREE_OPERAND (expr, 0);
2597 tree op1 = TREE_OPERAND (expr, 1);
2598 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2599 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2601 /* If one of the operands is a non-negative constant
2602 that fits in the target type, then the type of the
2603 other operand does not matter. */
2604 if ((TREE_CODE (op0) == INTEGER_CST
2605 && int_fits_type_p (op0, c_common_signed_type (type))
2606 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2607 || (TREE_CODE (op1) == INTEGER_CST
2608 && int_fits_type_p (op1, c_common_signed_type (type))
2609 && int_fits_type_p (op1,
2610 c_common_unsigned_type (type))))
2611 return false;
2612 /* If constant is unsigned and fits in the target
2613 type, then the result will also fit. */
2614 else if ((TREE_CODE (op0) == INTEGER_CST
2615 && unsigned0
2616 && int_fits_type_p (op0, type))
2617 || (TREE_CODE (op1) == INTEGER_CST
2618 && unsigned1
2619 && int_fits_type_p (op1, type)))
2620 return false;
2623 /* Warn for integer types converted to smaller integer types. */
2624 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2625 give_warning = true;
2627 /* When they are the same width but different signedness,
2628 then the value may change. */
2629 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2630 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2631 /* Even when converted to a bigger type, if the type is
2632 unsigned but expr is signed, then negative values
2633 will be changed. */
2634 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2635 && produce_warns)
2636 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2637 "may change the sign of the result",
2638 type, expr_type);
2641 /* Warn for integer types converted to real types if and only if
2642 all the range of values of the integer type cannot be
2643 represented by the real type. */
2644 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2645 && TREE_CODE (type) == REAL_TYPE)
2647 tree type_low_bound, type_high_bound;
2648 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2650 /* Don't warn about char y = 0xff; float x = (int) y; */
2651 expr = get_unwidened (expr, 0);
2652 expr_type = TREE_TYPE (expr);
2654 type_low_bound = TYPE_MIN_VALUE (expr_type);
2655 type_high_bound = TYPE_MAX_VALUE (expr_type);
2656 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2657 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2659 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2660 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2661 give_warning = true;
2664 /* Warn for real types converted to smaller real types. */
2665 else if (TREE_CODE (expr_type) == REAL_TYPE
2666 && TREE_CODE (type) == REAL_TYPE
2667 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2668 give_warning = true;
2671 return give_warning;
2674 /* Warns if the conversion of EXPR to TYPE may alter a value.
2675 This is a helper function for warnings_for_convert_and_check. */
2677 static void
2678 conversion_warning (tree type, tree expr)
2680 tree expr_type = TREE_TYPE (expr);
2681 location_t loc = EXPR_LOC_OR_HERE (expr);
2683 if (!warn_conversion && !warn_sign_conversion)
2684 return;
2686 switch (TREE_CODE (expr))
2688 case EQ_EXPR:
2689 case NE_EXPR:
2690 case LE_EXPR:
2691 case GE_EXPR:
2692 case LT_EXPR:
2693 case GT_EXPR:
2694 case TRUTH_ANDIF_EXPR:
2695 case TRUTH_ORIF_EXPR:
2696 case TRUTH_AND_EXPR:
2697 case TRUTH_OR_EXPR:
2698 case TRUTH_XOR_EXPR:
2699 case TRUTH_NOT_EXPR:
2700 /* Conversion from boolean to a signed:1 bit-field (which only
2701 can hold the values 0 and -1) doesn't lose information - but
2702 it does change the value. */
2703 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2704 warning_at (loc, OPT_Wconversion,
2705 "conversion to %qT from boolean expression", type);
2706 return;
2708 case REAL_CST:
2709 case INTEGER_CST:
2710 if (unsafe_conversion_p (type, expr, true))
2711 warning_at (loc, OPT_Wconversion,
2712 "conversion to %qT alters %qT constant value",
2713 type, expr_type);
2714 return;
2716 case COND_EXPR:
2718 /* In case of COND_EXPR, we do not care about the type of
2719 COND_EXPR, only about the conversion of each operand. */
2720 tree op1 = TREE_OPERAND (expr, 1);
2721 tree op2 = TREE_OPERAND (expr, 2);
2723 conversion_warning (type, op1);
2724 conversion_warning (type, op2);
2725 return;
2728 default: /* 'expr' is not a constant. */
2729 if (unsafe_conversion_p (type, expr, true))
2730 warning_at (loc, OPT_Wconversion,
2731 "conversion to %qT from %qT may alter its value",
2732 type, expr_type);
2736 /* Produce warnings after a conversion. RESULT is the result of
2737 converting EXPR to TYPE. This is a helper function for
2738 convert_and_check and cp_convert_and_check. */
2740 void
2741 warnings_for_convert_and_check (tree type, tree expr, tree result)
2743 location_t loc = EXPR_LOC_OR_HERE (expr);
2745 if (TREE_CODE (expr) == INTEGER_CST
2746 && (TREE_CODE (type) == INTEGER_TYPE
2747 || TREE_CODE (type) == ENUMERAL_TYPE)
2748 && !int_fits_type_p (expr, type))
2750 /* Do not diagnose overflow in a constant expression merely
2751 because a conversion overflowed. */
2752 if (TREE_OVERFLOW (result))
2753 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2755 if (TYPE_UNSIGNED (type))
2757 /* This detects cases like converting -129 or 256 to
2758 unsigned char. */
2759 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2760 warning_at (loc, OPT_Woverflow,
2761 "large integer implicitly truncated to unsigned type");
2762 else
2763 conversion_warning (type, expr);
2765 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2766 warning (OPT_Woverflow,
2767 "overflow in implicit constant conversion");
2768 /* No warning for converting 0x80000000 to int. */
2769 else if (pedantic
2770 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2771 || TYPE_PRECISION (TREE_TYPE (expr))
2772 != TYPE_PRECISION (type)))
2773 warning_at (loc, OPT_Woverflow,
2774 "overflow in implicit constant conversion");
2776 else
2777 conversion_warning (type, expr);
2779 else if ((TREE_CODE (result) == INTEGER_CST
2780 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2781 warning_at (loc, OPT_Woverflow,
2782 "overflow in implicit constant conversion");
2783 else
2784 conversion_warning (type, expr);
2788 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2789 Invoke this function on every expression that is converted implicitly,
2790 i.e. because of language rules and not because of an explicit cast. */
2792 tree
2793 convert_and_check (tree type, tree expr)
2795 tree result;
2796 tree expr_for_warning;
2798 /* Convert from a value with possible excess precision rather than
2799 via the semantic type, but do not warn about values not fitting
2800 exactly in the semantic type. */
2801 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2803 tree orig_type = TREE_TYPE (expr);
2804 expr = TREE_OPERAND (expr, 0);
2805 expr_for_warning = convert (orig_type, expr);
2806 if (orig_type == type)
2807 return expr_for_warning;
2809 else
2810 expr_for_warning = expr;
2812 if (TREE_TYPE (expr) == type)
2813 return expr;
2815 result = convert (type, expr);
2817 if (c_inhibit_evaluation_warnings == 0
2818 && !TREE_OVERFLOW_P (expr)
2819 && result != error_mark_node)
2820 warnings_for_convert_and_check (type, expr_for_warning, result);
2822 return result;
2825 /* A node in a list that describes references to variables (EXPR), which are
2826 either read accesses if WRITER is zero, or write accesses, in which case
2827 WRITER is the parent of EXPR. */
2828 struct tlist
2830 struct tlist *next;
2831 tree expr, writer;
2834 /* Used to implement a cache the results of a call to verify_tree. We only
2835 use this for SAVE_EXPRs. */
2836 struct tlist_cache
2838 struct tlist_cache *next;
2839 struct tlist *cache_before_sp;
2840 struct tlist *cache_after_sp;
2841 tree expr;
2844 /* Obstack to use when allocating tlist structures, and corresponding
2845 firstobj. */
2846 static struct obstack tlist_obstack;
2847 static char *tlist_firstobj = 0;
2849 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2850 warnings. */
2851 static struct tlist *warned_ids;
2852 /* SAVE_EXPRs need special treatment. We process them only once and then
2853 cache the results. */
2854 static struct tlist_cache *save_expr_cache;
2856 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2857 static void merge_tlist (struct tlist **, struct tlist *, int);
2858 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2859 static int warning_candidate_p (tree);
2860 static bool candidate_equal_p (const_tree, const_tree);
2861 static void warn_for_collisions (struct tlist *);
2862 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2863 static struct tlist *new_tlist (struct tlist *, tree, tree);
2865 /* Create a new struct tlist and fill in its fields. */
2866 static struct tlist *
2867 new_tlist (struct tlist *next, tree t, tree writer)
2869 struct tlist *l;
2870 l = XOBNEW (&tlist_obstack, struct tlist);
2871 l->next = next;
2872 l->expr = t;
2873 l->writer = writer;
2874 return l;
2877 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2878 is nonnull, we ignore any node we find which has a writer equal to it. */
2880 static void
2881 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2883 while (add)
2885 struct tlist *next = add->next;
2886 if (!copy)
2887 add->next = *to;
2888 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2889 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2890 add = next;
2894 /* Merge the nodes of ADD into TO. This merging process is done so that for
2895 each variable that already exists in TO, no new node is added; however if
2896 there is a write access recorded in ADD, and an occurrence on TO is only
2897 a read access, then the occurrence in TO will be modified to record the
2898 write. */
2900 static void
2901 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2903 struct tlist **end = to;
2905 while (*end)
2906 end = &(*end)->next;
2908 while (add)
2910 int found = 0;
2911 struct tlist *tmp2;
2912 struct tlist *next = add->next;
2914 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2915 if (candidate_equal_p (tmp2->expr, add->expr))
2917 found = 1;
2918 if (!tmp2->writer)
2919 tmp2->writer = add->writer;
2921 if (!found)
2923 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2924 end = &(*end)->next;
2925 *end = 0;
2927 add = next;
2931 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2932 references in list LIST conflict with it, excluding reads if ONLY writers
2933 is nonzero. */
2935 static void
2936 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2937 int only_writes)
2939 struct tlist *tmp;
2941 /* Avoid duplicate warnings. */
2942 for (tmp = warned_ids; tmp; tmp = tmp->next)
2943 if (candidate_equal_p (tmp->expr, written))
2944 return;
2946 while (list)
2948 if (candidate_equal_p (list->expr, written)
2949 && !candidate_equal_p (list->writer, writer)
2950 && (!only_writes || list->writer))
2952 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2953 warning_at (EXPR_LOC_OR_HERE (writer),
2954 OPT_Wsequence_point, "operation on %qE may be undefined",
2955 list->expr);
2957 list = list->next;
2961 /* Given a list LIST of references to variables, find whether any of these
2962 can cause conflicts due to missing sequence points. */
2964 static void
2965 warn_for_collisions (struct tlist *list)
2967 struct tlist *tmp;
2969 for (tmp = list; tmp; tmp = tmp->next)
2971 if (tmp->writer)
2972 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2976 /* Return nonzero if X is a tree that can be verified by the sequence point
2977 warnings. */
2978 static int
2979 warning_candidate_p (tree x)
2981 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2982 return 0;
2984 if (TREE_CODE (x) == BLOCK)
2985 return 0;
2987 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2988 (lvalue_p) crash on TRY/CATCH. */
2989 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2990 return 0;
2992 if (!lvalue_p (x))
2993 return 0;
2995 /* No point to track non-const calls, they will never satisfy
2996 operand_equal_p. */
2997 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2998 return 0;
3000 if (TREE_CODE (x) == STRING_CST)
3001 return 0;
3003 return 1;
3006 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3007 static bool
3008 candidate_equal_p (const_tree x, const_tree y)
3010 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3013 /* Walk the tree X, and record accesses to variables. If X is written by the
3014 parent tree, WRITER is the parent.
3015 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3016 expression or its only operand forces a sequence point, then everything up
3017 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3018 in PNO_SP.
3019 Once we return, we will have emitted warnings if any subexpression before
3020 such a sequence point could be undefined. On a higher level, however, the
3021 sequence point may not be relevant, and we'll merge the two lists.
3023 Example: (b++, a) + b;
3024 The call that processes the COMPOUND_EXPR will store the increment of B
3025 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3026 processes the PLUS_EXPR will need to merge the two lists so that
3027 eventually, all accesses end up on the same list (and we'll warn about the
3028 unordered subexpressions b++ and b.
3030 A note on merging. If we modify the former example so that our expression
3031 becomes
3032 (b++, b) + a
3033 care must be taken not simply to add all three expressions into the final
3034 PNO_SP list. The function merge_tlist takes care of that by merging the
3035 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3036 way, so that no more than one access to B is recorded. */
3038 static void
3039 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3040 tree writer)
3042 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3043 enum tree_code code;
3044 enum tree_code_class cl;
3046 /* X may be NULL if it is the operand of an empty statement expression
3047 ({ }). */
3048 if (x == NULL)
3049 return;
3051 restart:
3052 code = TREE_CODE (x);
3053 cl = TREE_CODE_CLASS (code);
3055 if (warning_candidate_p (x))
3056 *pno_sp = new_tlist (*pno_sp, x, writer);
3058 switch (code)
3060 case CONSTRUCTOR:
3061 return;
3063 case COMPOUND_EXPR:
3064 case TRUTH_ANDIF_EXPR:
3065 case TRUTH_ORIF_EXPR:
3066 tmp_before = tmp_nosp = tmp_list3 = 0;
3067 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3068 warn_for_collisions (tmp_nosp);
3069 merge_tlist (pbefore_sp, tmp_before, 0);
3070 merge_tlist (pbefore_sp, tmp_nosp, 0);
3071 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3072 merge_tlist (pbefore_sp, tmp_list3, 0);
3073 return;
3075 case COND_EXPR:
3076 tmp_before = tmp_list2 = 0;
3077 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3078 warn_for_collisions (tmp_list2);
3079 merge_tlist (pbefore_sp, tmp_before, 0);
3080 merge_tlist (pbefore_sp, tmp_list2, 1);
3082 tmp_list3 = tmp_nosp = 0;
3083 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3084 warn_for_collisions (tmp_nosp);
3085 merge_tlist (pbefore_sp, tmp_list3, 0);
3087 tmp_list3 = tmp_list2 = 0;
3088 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3089 warn_for_collisions (tmp_list2);
3090 merge_tlist (pbefore_sp, tmp_list3, 0);
3091 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3092 two first, to avoid warning for (a ? b++ : b++). */
3093 merge_tlist (&tmp_nosp, tmp_list2, 0);
3094 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3095 return;
3097 case PREDECREMENT_EXPR:
3098 case PREINCREMENT_EXPR:
3099 case POSTDECREMENT_EXPR:
3100 case POSTINCREMENT_EXPR:
3101 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3102 return;
3104 case MODIFY_EXPR:
3105 tmp_before = tmp_nosp = tmp_list3 = 0;
3106 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3107 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3108 /* Expressions inside the LHS are not ordered wrt. the sequence points
3109 in the RHS. Example:
3110 *a = (a++, 2)
3111 Despite the fact that the modification of "a" is in the before_sp
3112 list (tmp_before), it conflicts with the use of "a" in the LHS.
3113 We can handle this by adding the contents of tmp_list3
3114 to those of tmp_before, and redoing the collision warnings for that
3115 list. */
3116 add_tlist (&tmp_before, tmp_list3, x, 1);
3117 warn_for_collisions (tmp_before);
3118 /* Exclude the LHS itself here; we first have to merge it into the
3119 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3120 didn't exclude the LHS, we'd get it twice, once as a read and once
3121 as a write. */
3122 add_tlist (pno_sp, tmp_list3, x, 0);
3123 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3125 merge_tlist (pbefore_sp, tmp_before, 0);
3126 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3127 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3128 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3129 return;
3131 case CALL_EXPR:
3132 /* We need to warn about conflicts among arguments and conflicts between
3133 args and the function address. Side effects of the function address,
3134 however, are not ordered by the sequence point of the call. */
3136 call_expr_arg_iterator iter;
3137 tree arg;
3138 tmp_before = tmp_nosp = 0;
3139 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3140 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3142 tmp_list2 = tmp_list3 = 0;
3143 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3144 merge_tlist (&tmp_list3, tmp_list2, 0);
3145 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3147 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3148 warn_for_collisions (tmp_before);
3149 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3150 return;
3153 case TREE_LIST:
3154 /* Scan all the list, e.g. indices of multi dimensional array. */
3155 while (x)
3157 tmp_before = tmp_nosp = 0;
3158 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3159 merge_tlist (&tmp_nosp, tmp_before, 0);
3160 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3161 x = TREE_CHAIN (x);
3163 return;
3165 case SAVE_EXPR:
3167 struct tlist_cache *t;
3168 for (t = save_expr_cache; t; t = t->next)
3169 if (candidate_equal_p (t->expr, x))
3170 break;
3172 if (!t)
3174 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3175 t->next = save_expr_cache;
3176 t->expr = x;
3177 save_expr_cache = t;
3179 tmp_before = tmp_nosp = 0;
3180 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3181 warn_for_collisions (tmp_nosp);
3183 tmp_list3 = 0;
3184 while (tmp_nosp)
3186 struct tlist *t = tmp_nosp;
3187 tmp_nosp = t->next;
3188 merge_tlist (&tmp_list3, t, 0);
3190 t->cache_before_sp = tmp_before;
3191 t->cache_after_sp = tmp_list3;
3193 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3194 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3195 return;
3198 case ADDR_EXPR:
3199 x = TREE_OPERAND (x, 0);
3200 if (DECL_P (x))
3201 return;
3202 writer = 0;
3203 goto restart;
3205 default:
3206 /* For other expressions, simply recurse on their operands.
3207 Manual tail recursion for unary expressions.
3208 Other non-expressions need not be processed. */
3209 if (cl == tcc_unary)
3211 x = TREE_OPERAND (x, 0);
3212 writer = 0;
3213 goto restart;
3215 else if (IS_EXPR_CODE_CLASS (cl))
3217 int lp;
3218 int max = TREE_OPERAND_LENGTH (x);
3219 for (lp = 0; lp < max; lp++)
3221 tmp_before = tmp_nosp = 0;
3222 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3223 merge_tlist (&tmp_nosp, tmp_before, 0);
3224 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3227 return;
3231 /* Try to warn for undefined behavior in EXPR due to missing sequence
3232 points. */
3234 DEBUG_FUNCTION void
3235 verify_sequence_points (tree expr)
3237 struct tlist *before_sp = 0, *after_sp = 0;
3239 warned_ids = 0;
3240 save_expr_cache = 0;
3241 if (tlist_firstobj == 0)
3243 gcc_obstack_init (&tlist_obstack);
3244 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3247 verify_tree (expr, &before_sp, &after_sp, 0);
3248 warn_for_collisions (after_sp);
3249 obstack_free (&tlist_obstack, tlist_firstobj);
3252 /* Validate the expression after `case' and apply default promotions. */
3254 static tree
3255 check_case_value (tree value)
3257 if (value == NULL_TREE)
3258 return value;
3260 if (TREE_CODE (value) == INTEGER_CST)
3261 /* Promote char or short to int. */
3262 value = perform_integral_promotions (value);
3263 else if (value != error_mark_node)
3265 error ("case label does not reduce to an integer constant");
3266 value = error_mark_node;
3269 constant_expression_warning (value);
3271 return value;
3274 /* See if the case values LOW and HIGH are in the range of the original
3275 type (i.e. before the default conversion to int) of the switch testing
3276 expression.
3277 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3278 the type before promoting it. CASE_LOW_P is a pointer to the lower
3279 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3280 if the case is not a case range.
3281 The caller has to make sure that we are not called with NULL for
3282 CASE_LOW_P (i.e. the default case).
3283 Returns true if the case label is in range of ORIG_TYPE (saturated or
3284 untouched) or false if the label is out of range. */
3286 static bool
3287 check_case_bounds (tree type, tree orig_type,
3288 tree *case_low_p, tree *case_high_p)
3290 tree min_value, max_value;
3291 tree case_low = *case_low_p;
3292 tree case_high = case_high_p ? *case_high_p : case_low;
3294 /* If there was a problem with the original type, do nothing. */
3295 if (orig_type == error_mark_node)
3296 return true;
3298 min_value = TYPE_MIN_VALUE (orig_type);
3299 max_value = TYPE_MAX_VALUE (orig_type);
3301 /* Case label is less than minimum for type. */
3302 if (tree_int_cst_compare (case_low, min_value) < 0
3303 && tree_int_cst_compare (case_high, min_value) < 0)
3305 warning (0, "case label value is less than minimum value for type");
3306 return false;
3309 /* Case value is greater than maximum for type. */
3310 if (tree_int_cst_compare (case_low, max_value) > 0
3311 && tree_int_cst_compare (case_high, max_value) > 0)
3313 warning (0, "case label value exceeds maximum value for type");
3314 return false;
3317 /* Saturate lower case label value to minimum. */
3318 if (tree_int_cst_compare (case_high, min_value) >= 0
3319 && tree_int_cst_compare (case_low, min_value) < 0)
3321 warning (0, "lower value in case label range"
3322 " less than minimum value for type");
3323 case_low = min_value;
3326 /* Saturate upper case label value to maximum. */
3327 if (tree_int_cst_compare (case_low, max_value) <= 0
3328 && tree_int_cst_compare (case_high, max_value) > 0)
3330 warning (0, "upper value in case label range"
3331 " exceeds maximum value for type");
3332 case_high = max_value;
3335 if (*case_low_p != case_low)
3336 *case_low_p = convert (type, case_low);
3337 if (case_high_p && *case_high_p != case_high)
3338 *case_high_p = convert (type, case_high);
3340 return true;
3343 /* Return an integer type with BITS bits of precision,
3344 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3346 tree
3347 c_common_type_for_size (unsigned int bits, int unsignedp)
3349 if (bits == TYPE_PRECISION (integer_type_node))
3350 return unsignedp ? unsigned_type_node : integer_type_node;
3352 if (bits == TYPE_PRECISION (signed_char_type_node))
3353 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3355 if (bits == TYPE_PRECISION (short_integer_type_node))
3356 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3358 if (bits == TYPE_PRECISION (long_integer_type_node))
3359 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3361 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3362 return (unsignedp ? long_long_unsigned_type_node
3363 : long_long_integer_type_node);
3365 if (int128_integer_type_node
3366 && bits == TYPE_PRECISION (int128_integer_type_node))
3367 return (unsignedp ? int128_unsigned_type_node
3368 : int128_integer_type_node);
3370 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3371 return (unsignedp ? widest_unsigned_literal_type_node
3372 : widest_integer_literal_type_node);
3374 if (bits <= TYPE_PRECISION (intQI_type_node))
3375 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3377 if (bits <= TYPE_PRECISION (intHI_type_node))
3378 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3380 if (bits <= TYPE_PRECISION (intSI_type_node))
3381 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3383 if (bits <= TYPE_PRECISION (intDI_type_node))
3384 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3386 return 0;
3389 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3390 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3391 and saturating if SATP is nonzero, otherwise not saturating. */
3393 tree
3394 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3395 int unsignedp, int satp)
3397 enum machine_mode mode;
3398 if (ibit == 0)
3399 mode = unsignedp ? UQQmode : QQmode;
3400 else
3401 mode = unsignedp ? UHAmode : HAmode;
3403 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3404 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3405 break;
3407 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3409 sorry ("GCC cannot support operators with integer types and "
3410 "fixed-point types that have too many integral and "
3411 "fractional bits together");
3412 return 0;
3415 return c_common_type_for_mode (mode, satp);
3418 /* Used for communication between c_common_type_for_mode and
3419 c_register_builtin_type. */
3420 static GTY(()) tree registered_builtin_types;
3422 /* Return a data type that has machine mode MODE.
3423 If the mode is an integer,
3424 then UNSIGNEDP selects between signed and unsigned types.
3425 If the mode is a fixed-point mode,
3426 then UNSIGNEDP selects between saturating and nonsaturating types. */
3428 tree
3429 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3431 tree t;
3433 if (mode == TYPE_MODE (integer_type_node))
3434 return unsignedp ? unsigned_type_node : integer_type_node;
3436 if (mode == TYPE_MODE (signed_char_type_node))
3437 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3439 if (mode == TYPE_MODE (short_integer_type_node))
3440 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3442 if (mode == TYPE_MODE (long_integer_type_node))
3443 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3445 if (mode == TYPE_MODE (long_long_integer_type_node))
3446 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3448 if (int128_integer_type_node
3449 && mode == TYPE_MODE (int128_integer_type_node))
3450 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3452 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3453 return unsignedp ? widest_unsigned_literal_type_node
3454 : widest_integer_literal_type_node;
3456 if (mode == QImode)
3457 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3459 if (mode == HImode)
3460 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3462 if (mode == SImode)
3463 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3465 if (mode == DImode)
3466 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3468 #if HOST_BITS_PER_WIDE_INT >= 64
3469 if (mode == TYPE_MODE (intTI_type_node))
3470 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3471 #endif
3473 if (mode == TYPE_MODE (float_type_node))
3474 return float_type_node;
3476 if (mode == TYPE_MODE (double_type_node))
3477 return double_type_node;
3479 if (mode == TYPE_MODE (long_double_type_node))
3480 return long_double_type_node;
3482 if (mode == TYPE_MODE (void_type_node))
3483 return void_type_node;
3485 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3486 return (unsignedp
3487 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3488 : make_signed_type (GET_MODE_PRECISION (mode)));
3490 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3491 return (unsignedp
3492 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3493 : make_signed_type (GET_MODE_PRECISION (mode)));
3495 if (COMPLEX_MODE_P (mode))
3497 enum machine_mode inner_mode;
3498 tree inner_type;
3500 if (mode == TYPE_MODE (complex_float_type_node))
3501 return complex_float_type_node;
3502 if (mode == TYPE_MODE (complex_double_type_node))
3503 return complex_double_type_node;
3504 if (mode == TYPE_MODE (complex_long_double_type_node))
3505 return complex_long_double_type_node;
3507 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3508 return complex_integer_type_node;
3510 inner_mode = GET_MODE_INNER (mode);
3511 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3512 if (inner_type != NULL_TREE)
3513 return build_complex_type (inner_type);
3515 else if (VECTOR_MODE_P (mode))
3517 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3518 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3519 if (inner_type != NULL_TREE)
3520 return build_vector_type_for_mode (inner_type, mode);
3523 if (mode == TYPE_MODE (dfloat32_type_node))
3524 return dfloat32_type_node;
3525 if (mode == TYPE_MODE (dfloat64_type_node))
3526 return dfloat64_type_node;
3527 if (mode == TYPE_MODE (dfloat128_type_node))
3528 return dfloat128_type_node;
3530 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3532 if (mode == TYPE_MODE (short_fract_type_node))
3533 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3534 if (mode == TYPE_MODE (fract_type_node))
3535 return unsignedp ? sat_fract_type_node : fract_type_node;
3536 if (mode == TYPE_MODE (long_fract_type_node))
3537 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3538 if (mode == TYPE_MODE (long_long_fract_type_node))
3539 return unsignedp ? sat_long_long_fract_type_node
3540 : long_long_fract_type_node;
3542 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3543 return unsignedp ? sat_unsigned_short_fract_type_node
3544 : unsigned_short_fract_type_node;
3545 if (mode == TYPE_MODE (unsigned_fract_type_node))
3546 return unsignedp ? sat_unsigned_fract_type_node
3547 : unsigned_fract_type_node;
3548 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3549 return unsignedp ? sat_unsigned_long_fract_type_node
3550 : unsigned_long_fract_type_node;
3551 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3552 return unsignedp ? sat_unsigned_long_long_fract_type_node
3553 : unsigned_long_long_fract_type_node;
3555 if (mode == TYPE_MODE (short_accum_type_node))
3556 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3557 if (mode == TYPE_MODE (accum_type_node))
3558 return unsignedp ? sat_accum_type_node : accum_type_node;
3559 if (mode == TYPE_MODE (long_accum_type_node))
3560 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3561 if (mode == TYPE_MODE (long_long_accum_type_node))
3562 return unsignedp ? sat_long_long_accum_type_node
3563 : long_long_accum_type_node;
3565 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3566 return unsignedp ? sat_unsigned_short_accum_type_node
3567 : unsigned_short_accum_type_node;
3568 if (mode == TYPE_MODE (unsigned_accum_type_node))
3569 return unsignedp ? sat_unsigned_accum_type_node
3570 : unsigned_accum_type_node;
3571 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3572 return unsignedp ? sat_unsigned_long_accum_type_node
3573 : unsigned_long_accum_type_node;
3574 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3575 return unsignedp ? sat_unsigned_long_long_accum_type_node
3576 : unsigned_long_long_accum_type_node;
3578 if (mode == QQmode)
3579 return unsignedp ? sat_qq_type_node : qq_type_node;
3580 if (mode == HQmode)
3581 return unsignedp ? sat_hq_type_node : hq_type_node;
3582 if (mode == SQmode)
3583 return unsignedp ? sat_sq_type_node : sq_type_node;
3584 if (mode == DQmode)
3585 return unsignedp ? sat_dq_type_node : dq_type_node;
3586 if (mode == TQmode)
3587 return unsignedp ? sat_tq_type_node : tq_type_node;
3589 if (mode == UQQmode)
3590 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3591 if (mode == UHQmode)
3592 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3593 if (mode == USQmode)
3594 return unsignedp ? sat_usq_type_node : usq_type_node;
3595 if (mode == UDQmode)
3596 return unsignedp ? sat_udq_type_node : udq_type_node;
3597 if (mode == UTQmode)
3598 return unsignedp ? sat_utq_type_node : utq_type_node;
3600 if (mode == HAmode)
3601 return unsignedp ? sat_ha_type_node : ha_type_node;
3602 if (mode == SAmode)
3603 return unsignedp ? sat_sa_type_node : sa_type_node;
3604 if (mode == DAmode)
3605 return unsignedp ? sat_da_type_node : da_type_node;
3606 if (mode == TAmode)
3607 return unsignedp ? sat_ta_type_node : ta_type_node;
3609 if (mode == UHAmode)
3610 return unsignedp ? sat_uha_type_node : uha_type_node;
3611 if (mode == USAmode)
3612 return unsignedp ? sat_usa_type_node : usa_type_node;
3613 if (mode == UDAmode)
3614 return unsignedp ? sat_uda_type_node : uda_type_node;
3615 if (mode == UTAmode)
3616 return unsignedp ? sat_uta_type_node : uta_type_node;
3619 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3620 if (TYPE_MODE (TREE_VALUE (t)) == mode
3621 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3622 return TREE_VALUE (t);
3624 return 0;
3627 tree
3628 c_common_unsigned_type (tree type)
3630 return c_common_signed_or_unsigned_type (1, type);
3633 /* Return a signed type the same as TYPE in other respects. */
3635 tree
3636 c_common_signed_type (tree type)
3638 return c_common_signed_or_unsigned_type (0, type);
3641 /* Return a type the same as TYPE except unsigned or
3642 signed according to UNSIGNEDP. */
3644 tree
3645 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3647 tree type1;
3649 /* This block of code emulates the behavior of the old
3650 c_common_unsigned_type. In particular, it returns
3651 long_unsigned_type_node if passed a long, even when a int would
3652 have the same size. This is necessary for warnings to work
3653 correctly in archs where sizeof(int) == sizeof(long) */
3655 type1 = TYPE_MAIN_VARIANT (type);
3656 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3657 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3658 if (type1 == integer_type_node || type1 == unsigned_type_node)
3659 return unsignedp ? unsigned_type_node : integer_type_node;
3660 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3661 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3662 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3663 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3664 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3665 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3666 if (int128_integer_type_node
3667 && (type1 == int128_integer_type_node
3668 || type1 == int128_unsigned_type_node))
3669 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3670 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3671 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3672 #if HOST_BITS_PER_WIDE_INT >= 64
3673 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3674 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3675 #endif
3676 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3677 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3678 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3679 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3680 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3681 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3682 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3683 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3685 #define C_COMMON_FIXED_TYPES(NAME) \
3686 if (type1 == short_ ## NAME ## _type_node \
3687 || type1 == unsigned_short_ ## NAME ## _type_node) \
3688 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3689 : short_ ## NAME ## _type_node; \
3690 if (type1 == NAME ## _type_node \
3691 || type1 == unsigned_ ## NAME ## _type_node) \
3692 return unsignedp ? unsigned_ ## NAME ## _type_node \
3693 : NAME ## _type_node; \
3694 if (type1 == long_ ## NAME ## _type_node \
3695 || type1 == unsigned_long_ ## NAME ## _type_node) \
3696 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3697 : long_ ## NAME ## _type_node; \
3698 if (type1 == long_long_ ## NAME ## _type_node \
3699 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3700 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3701 : long_long_ ## NAME ## _type_node;
3703 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3704 if (type1 == NAME ## _type_node \
3705 || type1 == u ## NAME ## _type_node) \
3706 return unsignedp ? u ## NAME ## _type_node \
3707 : NAME ## _type_node;
3709 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3710 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3711 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3712 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3713 : sat_ ## short_ ## NAME ## _type_node; \
3714 if (type1 == sat_ ## NAME ## _type_node \
3715 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3716 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3717 : sat_ ## NAME ## _type_node; \
3718 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3719 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3720 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3721 : sat_ ## long_ ## NAME ## _type_node; \
3722 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3723 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3724 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3725 : sat_ ## long_long_ ## NAME ## _type_node;
3727 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3728 if (type1 == sat_ ## NAME ## _type_node \
3729 || type1 == sat_ ## u ## NAME ## _type_node) \
3730 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3731 : sat_ ## NAME ## _type_node;
3733 C_COMMON_FIXED_TYPES (fract);
3734 C_COMMON_FIXED_TYPES_SAT (fract);
3735 C_COMMON_FIXED_TYPES (accum);
3736 C_COMMON_FIXED_TYPES_SAT (accum);
3738 C_COMMON_FIXED_MODE_TYPES (qq);
3739 C_COMMON_FIXED_MODE_TYPES (hq);
3740 C_COMMON_FIXED_MODE_TYPES (sq);
3741 C_COMMON_FIXED_MODE_TYPES (dq);
3742 C_COMMON_FIXED_MODE_TYPES (tq);
3743 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3744 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3745 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3746 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3747 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3748 C_COMMON_FIXED_MODE_TYPES (ha);
3749 C_COMMON_FIXED_MODE_TYPES (sa);
3750 C_COMMON_FIXED_MODE_TYPES (da);
3751 C_COMMON_FIXED_MODE_TYPES (ta);
3752 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3753 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3754 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3755 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3757 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3758 the precision; they have precision set to match their range, but
3759 may use a wider mode to match an ABI. If we change modes, we may
3760 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3761 the precision as well, so as to yield correct results for
3762 bit-field types. C++ does not have these separate bit-field
3763 types, and producing a signed or unsigned variant of an
3764 ENUMERAL_TYPE may cause other problems as well. */
3766 if (!INTEGRAL_TYPE_P (type)
3767 || TYPE_UNSIGNED (type) == unsignedp)
3768 return type;
3770 #define TYPE_OK(node) \
3771 (TYPE_MODE (type) == TYPE_MODE (node) \
3772 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3773 if (TYPE_OK (signed_char_type_node))
3774 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3775 if (TYPE_OK (integer_type_node))
3776 return unsignedp ? unsigned_type_node : integer_type_node;
3777 if (TYPE_OK (short_integer_type_node))
3778 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3779 if (TYPE_OK (long_integer_type_node))
3780 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3781 if (TYPE_OK (long_long_integer_type_node))
3782 return (unsignedp ? long_long_unsigned_type_node
3783 : long_long_integer_type_node);
3784 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3785 return (unsignedp ? int128_unsigned_type_node
3786 : int128_integer_type_node);
3787 if (TYPE_OK (widest_integer_literal_type_node))
3788 return (unsignedp ? widest_unsigned_literal_type_node
3789 : widest_integer_literal_type_node);
3791 #if HOST_BITS_PER_WIDE_INT >= 64
3792 if (TYPE_OK (intTI_type_node))
3793 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3794 #endif
3795 if (TYPE_OK (intDI_type_node))
3796 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3797 if (TYPE_OK (intSI_type_node))
3798 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3799 if (TYPE_OK (intHI_type_node))
3800 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3801 if (TYPE_OK (intQI_type_node))
3802 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3803 #undef TYPE_OK
3805 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3808 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3810 tree
3811 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3813 /* Extended integer types of the same width as a standard type have
3814 lesser rank, so those of the same width as int promote to int or
3815 unsigned int and are valid for printf formats expecting int or
3816 unsigned int. To avoid such special cases, avoid creating
3817 extended integer types for bit-fields if a standard integer type
3818 is available. */
3819 if (width == TYPE_PRECISION (integer_type_node))
3820 return unsignedp ? unsigned_type_node : integer_type_node;
3821 if (width == TYPE_PRECISION (signed_char_type_node))
3822 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3823 if (width == TYPE_PRECISION (short_integer_type_node))
3824 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3825 if (width == TYPE_PRECISION (long_integer_type_node))
3826 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3827 if (width == TYPE_PRECISION (long_long_integer_type_node))
3828 return (unsignedp ? long_long_unsigned_type_node
3829 : long_long_integer_type_node);
3830 if (int128_integer_type_node
3831 && width == TYPE_PRECISION (int128_integer_type_node))
3832 return (unsignedp ? int128_unsigned_type_node
3833 : int128_integer_type_node);
3834 return build_nonstandard_integer_type (width, unsignedp);
3837 /* The C version of the register_builtin_type langhook. */
3839 void
3840 c_register_builtin_type (tree type, const char* name)
3842 tree decl;
3844 decl = build_decl (UNKNOWN_LOCATION,
3845 TYPE_DECL, get_identifier (name), type);
3846 DECL_ARTIFICIAL (decl) = 1;
3847 if (!TYPE_NAME (type))
3848 TYPE_NAME (type) = decl;
3849 pushdecl (decl);
3851 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3854 /* Print an error message for invalid operands to arith operation
3855 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3856 LOCATION is the location of the message. */
3858 void
3859 binary_op_error (location_t location, enum tree_code code,
3860 tree type0, tree type1)
3862 const char *opname;
3864 switch (code)
3866 case PLUS_EXPR:
3867 opname = "+"; break;
3868 case MINUS_EXPR:
3869 opname = "-"; break;
3870 case MULT_EXPR:
3871 opname = "*"; break;
3872 case MAX_EXPR:
3873 opname = "max"; break;
3874 case MIN_EXPR:
3875 opname = "min"; break;
3876 case EQ_EXPR:
3877 opname = "=="; break;
3878 case NE_EXPR:
3879 opname = "!="; break;
3880 case LE_EXPR:
3881 opname = "<="; break;
3882 case GE_EXPR:
3883 opname = ">="; break;
3884 case LT_EXPR:
3885 opname = "<"; break;
3886 case GT_EXPR:
3887 opname = ">"; break;
3888 case LSHIFT_EXPR:
3889 opname = "<<"; break;
3890 case RSHIFT_EXPR:
3891 opname = ">>"; break;
3892 case TRUNC_MOD_EXPR:
3893 case FLOOR_MOD_EXPR:
3894 opname = "%"; break;
3895 case TRUNC_DIV_EXPR:
3896 case FLOOR_DIV_EXPR:
3897 opname = "/"; break;
3898 case BIT_AND_EXPR:
3899 opname = "&"; break;
3900 case BIT_IOR_EXPR:
3901 opname = "|"; break;
3902 case TRUTH_ANDIF_EXPR:
3903 opname = "&&"; break;
3904 case TRUTH_ORIF_EXPR:
3905 opname = "||"; break;
3906 case BIT_XOR_EXPR:
3907 opname = "^"; break;
3908 default:
3909 gcc_unreachable ();
3911 error_at (location,
3912 "invalid operands to binary %s (have %qT and %qT)", opname,
3913 type0, type1);
3916 /* Given an expression as a tree, return its original type. Do this
3917 by stripping any conversion that preserves the sign and precision. */
3918 static tree
3919 expr_original_type (tree expr)
3921 STRIP_SIGN_NOPS (expr);
3922 return TREE_TYPE (expr);
3925 /* Subroutine of build_binary_op, used for comparison operations.
3926 See if the operands have both been converted from subword integer types
3927 and, if so, perhaps change them both back to their original type.
3928 This function is also responsible for converting the two operands
3929 to the proper common type for comparison.
3931 The arguments of this function are all pointers to local variables
3932 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3933 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3935 If this function returns nonzero, it means that the comparison has
3936 a constant value. What this function returns is an expression for
3937 that value. */
3939 tree
3940 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3941 enum tree_code *rescode_ptr)
3943 tree type;
3944 tree op0 = *op0_ptr;
3945 tree op1 = *op1_ptr;
3946 int unsignedp0, unsignedp1;
3947 int real1, real2;
3948 tree primop0, primop1;
3949 enum tree_code code = *rescode_ptr;
3950 location_t loc = EXPR_LOC_OR_HERE (op0);
3952 /* Throw away any conversions to wider types
3953 already present in the operands. */
3955 primop0 = c_common_get_narrower (op0, &unsignedp0);
3956 primop1 = c_common_get_narrower (op1, &unsignedp1);
3958 /* If primopN is first sign-extended from primopN's precision to opN's
3959 precision, then zero-extended from opN's precision to
3960 *restype_ptr precision, shortenings might be invalid. */
3961 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3962 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3963 && !unsignedp0
3964 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3965 primop0 = op0;
3966 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3967 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3968 && !unsignedp1
3969 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3970 primop1 = op1;
3972 /* Handle the case that OP0 does not *contain* a conversion
3973 but it *requires* conversion to FINAL_TYPE. */
3975 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3976 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3977 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3978 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3980 /* If one of the operands must be floated, we cannot optimize. */
3981 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3982 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3984 /* If first arg is constant, swap the args (changing operation
3985 so value is preserved), for canonicalization. Don't do this if
3986 the second arg is 0. */
3988 if (TREE_CONSTANT (primop0)
3989 && !integer_zerop (primop1) && !real_zerop (primop1)
3990 && !fixed_zerop (primop1))
3992 tree tem = primop0;
3993 int temi = unsignedp0;
3994 primop0 = primop1;
3995 primop1 = tem;
3996 tem = op0;
3997 op0 = op1;
3998 op1 = tem;
3999 *op0_ptr = op0;
4000 *op1_ptr = op1;
4001 unsignedp0 = unsignedp1;
4002 unsignedp1 = temi;
4003 temi = real1;
4004 real1 = real2;
4005 real2 = temi;
4007 switch (code)
4009 case LT_EXPR:
4010 code = GT_EXPR;
4011 break;
4012 case GT_EXPR:
4013 code = LT_EXPR;
4014 break;
4015 case LE_EXPR:
4016 code = GE_EXPR;
4017 break;
4018 case GE_EXPR:
4019 code = LE_EXPR;
4020 break;
4021 default:
4022 break;
4024 *rescode_ptr = code;
4027 /* If comparing an integer against a constant more bits wide,
4028 maybe we can deduce a value of 1 or 0 independent of the data.
4029 Or else truncate the constant now
4030 rather than extend the variable at run time.
4032 This is only interesting if the constant is the wider arg.
4033 Also, it is not safe if the constant is unsigned and the
4034 variable arg is signed, since in this case the variable
4035 would be sign-extended and then regarded as unsigned.
4036 Our technique fails in this case because the lowest/highest
4037 possible unsigned results don't follow naturally from the
4038 lowest/highest possible values of the variable operand.
4039 For just EQ_EXPR and NE_EXPR there is another technique that
4040 could be used: see if the constant can be faithfully represented
4041 in the other operand's type, by truncating it and reextending it
4042 and see if that preserves the constant's value. */
4044 if (!real1 && !real2
4045 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4046 && TREE_CODE (primop1) == INTEGER_CST
4047 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4049 int min_gt, max_gt, min_lt, max_lt;
4050 tree maxval, minval;
4051 /* 1 if comparison is nominally unsigned. */
4052 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4053 tree val;
4055 type = c_common_signed_or_unsigned_type (unsignedp0,
4056 TREE_TYPE (primop0));
4058 maxval = TYPE_MAX_VALUE (type);
4059 minval = TYPE_MIN_VALUE (type);
4061 if (unsignedp && !unsignedp0)
4062 *restype_ptr = c_common_signed_type (*restype_ptr);
4064 if (TREE_TYPE (primop1) != *restype_ptr)
4066 /* Convert primop1 to target type, but do not introduce
4067 additional overflow. We know primop1 is an int_cst. */
4068 primop1 = force_fit_type_double (*restype_ptr,
4069 tree_to_double_int (primop1),
4070 0, TREE_OVERFLOW (primop1));
4072 if (type != *restype_ptr)
4074 minval = convert (*restype_ptr, minval);
4075 maxval = convert (*restype_ptr, maxval);
4078 if (unsignedp && unsignedp0)
4080 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4081 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4082 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4083 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4085 else
4087 min_gt = INT_CST_LT (primop1, minval);
4088 max_gt = INT_CST_LT (primop1, maxval);
4089 min_lt = INT_CST_LT (minval, primop1);
4090 max_lt = INT_CST_LT (maxval, primop1);
4093 val = 0;
4094 /* This used to be a switch, but Genix compiler can't handle that. */
4095 if (code == NE_EXPR)
4097 if (max_lt || min_gt)
4098 val = truthvalue_true_node;
4100 else if (code == EQ_EXPR)
4102 if (max_lt || min_gt)
4103 val = truthvalue_false_node;
4105 else if (code == LT_EXPR)
4107 if (max_lt)
4108 val = truthvalue_true_node;
4109 if (!min_lt)
4110 val = truthvalue_false_node;
4112 else if (code == GT_EXPR)
4114 if (min_gt)
4115 val = truthvalue_true_node;
4116 if (!max_gt)
4117 val = truthvalue_false_node;
4119 else if (code == LE_EXPR)
4121 if (!max_gt)
4122 val = truthvalue_true_node;
4123 if (min_gt)
4124 val = truthvalue_false_node;
4126 else if (code == GE_EXPR)
4128 if (!min_lt)
4129 val = truthvalue_true_node;
4130 if (max_lt)
4131 val = truthvalue_false_node;
4134 /* If primop0 was sign-extended and unsigned comparison specd,
4135 we did a signed comparison above using the signed type bounds.
4136 But the comparison we output must be unsigned.
4138 Also, for inequalities, VAL is no good; but if the signed
4139 comparison had *any* fixed result, it follows that the
4140 unsigned comparison just tests the sign in reverse
4141 (positive values are LE, negative ones GE).
4142 So we can generate an unsigned comparison
4143 against an extreme value of the signed type. */
4145 if (unsignedp && !unsignedp0)
4147 if (val != 0)
4148 switch (code)
4150 case LT_EXPR:
4151 case GE_EXPR:
4152 primop1 = TYPE_MIN_VALUE (type);
4153 val = 0;
4154 break;
4156 case LE_EXPR:
4157 case GT_EXPR:
4158 primop1 = TYPE_MAX_VALUE (type);
4159 val = 0;
4160 break;
4162 default:
4163 break;
4165 type = c_common_unsigned_type (type);
4168 if (TREE_CODE (primop0) != INTEGER_CST
4169 && c_inhibit_evaluation_warnings == 0)
4171 if (val == truthvalue_false_node)
4172 warning_at (loc, OPT_Wtype_limits,
4173 "comparison is always false due to limited range of data type");
4174 if (val == truthvalue_true_node)
4175 warning_at (loc, OPT_Wtype_limits,
4176 "comparison is always true due to limited range of data type");
4179 if (val != 0)
4181 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4182 if (TREE_SIDE_EFFECTS (primop0))
4183 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4184 return val;
4187 /* Value is not predetermined, but do the comparison
4188 in the type of the operand that is not constant.
4189 TYPE is already properly set. */
4192 /* If either arg is decimal float and the other is float, find the
4193 proper common type to use for comparison. */
4194 else if (real1 && real2
4195 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4196 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4197 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4199 else if (real1 && real2
4200 && (TYPE_PRECISION (TREE_TYPE (primop0))
4201 == TYPE_PRECISION (TREE_TYPE (primop1))))
4202 type = TREE_TYPE (primop0);
4204 /* If args' natural types are both narrower than nominal type
4205 and both extend in the same manner, compare them
4206 in the type of the wider arg.
4207 Otherwise must actually extend both to the nominal
4208 common type lest different ways of extending
4209 alter the result.
4210 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4212 else if (unsignedp0 == unsignedp1 && real1 == real2
4213 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4214 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4216 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4217 type = c_common_signed_or_unsigned_type (unsignedp0
4218 || TYPE_UNSIGNED (*restype_ptr),
4219 type);
4220 /* Make sure shorter operand is extended the right way
4221 to match the longer operand. */
4222 primop0
4223 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4224 TREE_TYPE (primop0)),
4225 primop0);
4226 primop1
4227 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4228 TREE_TYPE (primop1)),
4229 primop1);
4231 else
4233 /* Here we must do the comparison on the nominal type
4234 using the args exactly as we received them. */
4235 type = *restype_ptr;
4236 primop0 = op0;
4237 primop1 = op1;
4239 if (!real1 && !real2 && integer_zerop (primop1)
4240 && TYPE_UNSIGNED (*restype_ptr))
4242 tree value = 0;
4243 /* All unsigned values are >= 0, so we warn. However,
4244 if OP0 is a constant that is >= 0, the signedness of
4245 the comparison isn't an issue, so suppress the
4246 warning. */
4247 bool warn =
4248 warn_type_limits && !in_system_header
4249 && c_inhibit_evaluation_warnings == 0
4250 && !(TREE_CODE (primop0) == INTEGER_CST
4251 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4252 primop0)))
4253 /* Do not warn for enumeration types. */
4254 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4256 switch (code)
4258 case GE_EXPR:
4259 if (warn)
4260 warning_at (loc, OPT_Wtype_limits,
4261 "comparison of unsigned expression >= 0 is always true");
4262 value = truthvalue_true_node;
4263 break;
4265 case LT_EXPR:
4266 if (warn)
4267 warning_at (loc, OPT_Wtype_limits,
4268 "comparison of unsigned expression < 0 is always false");
4269 value = truthvalue_false_node;
4270 break;
4272 default:
4273 break;
4276 if (value != 0)
4278 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4279 if (TREE_SIDE_EFFECTS (primop0))
4280 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4281 primop0, value);
4282 return value;
4287 *op0_ptr = convert (type, primop0);
4288 *op1_ptr = convert (type, primop1);
4290 *restype_ptr = truthvalue_type_node;
4292 return 0;
4295 /* Return a tree for the sum or difference (RESULTCODE says which)
4296 of pointer PTROP and integer INTOP. */
4298 tree
4299 pointer_int_sum (location_t loc, enum tree_code resultcode,
4300 tree ptrop, tree intop)
4302 tree size_exp, ret;
4304 /* The result is a pointer of the same type that is being added. */
4305 tree result_type = TREE_TYPE (ptrop);
4307 /* If the pointer lives in UPC shared memory, then
4308 drop the 'shared' qualifier. */
4309 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4310 result_type = build_upc_unshared_type (result_type);
4312 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4314 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4315 "pointer of type %<void *%> used in arithmetic");
4316 size_exp = integer_one_node;
4318 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4320 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4321 "pointer to a function used in arithmetic");
4322 size_exp = integer_one_node;
4324 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4326 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4327 "pointer to member function used in arithmetic");
4328 size_exp = integer_one_node;
4330 else
4331 size_exp = size_in_bytes (TREE_TYPE (result_type));
4333 /* We are manipulating pointer values, so we don't need to warn
4334 about relying on undefined signed overflow. We disable the
4335 warning here because we use integer types so fold won't know that
4336 they are really pointers. */
4337 fold_defer_overflow_warnings ();
4339 /* If what we are about to multiply by the size of the elements
4340 contains a constant term, apply distributive law
4341 and multiply that constant term separately.
4342 This helps produce common subexpressions. */
4343 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4344 && !TREE_CONSTANT (intop)
4345 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4346 && TREE_CONSTANT (size_exp)
4347 /* If the constant comes from pointer subtraction,
4348 skip this optimization--it would cause an error. */
4349 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4350 /* If the constant is unsigned, and smaller than the pointer size,
4351 then we must skip this optimization. This is because it could cause
4352 an overflow error if the constant is negative but INTOP is not. */
4353 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4354 || (TYPE_PRECISION (TREE_TYPE (intop))
4355 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4357 enum tree_code subcode = resultcode;
4358 tree int_type = TREE_TYPE (intop);
4359 if (TREE_CODE (intop) == MINUS_EXPR)
4360 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4361 /* Convert both subexpression types to the type of intop,
4362 because weird cases involving pointer arithmetic
4363 can result in a sum or difference with different type args. */
4364 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4365 subcode, ptrop,
4366 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4367 intop = convert (int_type, TREE_OPERAND (intop, 0));
4370 /* Convert the integer argument to a type the same size as sizetype
4371 so the multiply won't overflow spuriously. */
4372 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4373 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4374 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4375 TYPE_UNSIGNED (sizetype)), intop);
4377 /* Replace the integer argument with a suitable product by the object size.
4378 Do this multiplication as signed, then convert to the appropriate type
4379 for the pointer operation and disregard an overflow that occurred only
4380 because of the sign-extension change in the latter conversion. */
4382 tree t = build_binary_op (loc,
4383 MULT_EXPR, intop,
4384 convert (TREE_TYPE (intop), size_exp), 1);
4385 intop = convert (sizetype, t);
4386 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4387 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4388 TREE_INT_CST_HIGH (intop));
4391 /* Create the sum or difference. */
4392 if (resultcode == MINUS_EXPR)
4393 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4395 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4397 fold_undefer_and_ignore_overflow_warnings ();
4399 return ret;
4402 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4403 and if NON_CONST is known not to be permitted in an evaluated part
4404 of a constant expression. */
4406 tree
4407 c_wrap_maybe_const (tree expr, bool non_const)
4409 bool nowarning = TREE_NO_WARNING (expr);
4410 location_t loc = EXPR_LOCATION (expr);
4412 /* This should never be called for C++. */
4413 if (c_dialect_cxx ())
4414 gcc_unreachable ();
4416 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4417 STRIP_TYPE_NOPS (expr);
4418 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4419 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4420 if (nowarning)
4421 TREE_NO_WARNING (expr) = 1;
4422 protected_set_expr_location (expr, loc);
4424 return expr;
4427 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4428 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4429 around the SAVE_EXPR if needed so that c_fully_fold does not need
4430 to look inside SAVE_EXPRs. */
4432 tree
4433 c_save_expr (tree expr)
4435 bool maybe_const = true;
4436 if (c_dialect_cxx ())
4437 return save_expr (expr);
4438 expr = c_fully_fold (expr, false, &maybe_const);
4439 expr = save_expr (expr);
4440 if (!maybe_const)
4441 expr = c_wrap_maybe_const (expr, true);
4442 return expr;
4445 /* Return whether EXPR is a declaration whose address can never be
4446 NULL. */
4448 bool
4449 decl_with_nonnull_addr_p (const_tree expr)
4451 return (DECL_P (expr)
4452 && (TREE_CODE (expr) == PARM_DECL
4453 || TREE_CODE (expr) == LABEL_DECL
4454 || !DECL_WEAK (expr)));
4457 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4458 or for an `if' or `while' statement or ?..: exp. It should already
4459 have been validated to be of suitable type; otherwise, a bad
4460 diagnostic may result.
4462 The EXPR is located at LOCATION.
4464 This preparation consists of taking the ordinary
4465 representation of an expression expr and producing a valid tree
4466 boolean expression describing whether expr is nonzero. We could
4467 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4468 but we optimize comparisons, &&, ||, and !.
4470 The resulting type should always be `truthvalue_type_node'. */
4472 tree
4473 c_common_truthvalue_conversion (location_t location, tree expr)
4475 switch (TREE_CODE (expr))
4477 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4478 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4479 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4480 case ORDERED_EXPR: case UNORDERED_EXPR:
4481 if (TREE_TYPE (expr) == truthvalue_type_node)
4482 return expr;
4483 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4484 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4485 goto ret;
4487 case TRUTH_ANDIF_EXPR:
4488 case TRUTH_ORIF_EXPR:
4489 case TRUTH_AND_EXPR:
4490 case TRUTH_OR_EXPR:
4491 case TRUTH_XOR_EXPR:
4492 if (TREE_TYPE (expr) == truthvalue_type_node)
4493 return expr;
4494 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4495 c_common_truthvalue_conversion (location,
4496 TREE_OPERAND (expr, 0)),
4497 c_common_truthvalue_conversion (location,
4498 TREE_OPERAND (expr, 1)));
4499 goto ret;
4501 case TRUTH_NOT_EXPR:
4502 if (TREE_TYPE (expr) == truthvalue_type_node)
4503 return expr;
4504 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4505 c_common_truthvalue_conversion (location,
4506 TREE_OPERAND (expr, 0)));
4507 goto ret;
4509 case ERROR_MARK:
4510 return expr;
4512 case INTEGER_CST:
4513 return integer_zerop (expr) ? truthvalue_false_node
4514 : truthvalue_true_node;
4516 case REAL_CST:
4517 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4518 ? truthvalue_true_node
4519 : truthvalue_false_node;
4521 case FIXED_CST:
4522 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4523 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4524 ? truthvalue_true_node
4525 : truthvalue_false_node;
4527 case FUNCTION_DECL:
4528 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4529 /* Fall through. */
4531 case ADDR_EXPR:
4533 tree inner = TREE_OPERAND (expr, 0);
4534 if (decl_with_nonnull_addr_p (inner))
4536 /* Common Ada/Pascal programmer's mistake. */
4537 warning_at (location,
4538 OPT_Waddress,
4539 "the address of %qD will always evaluate as %<true%>",
4540 inner);
4541 return truthvalue_true_node;
4543 break;
4546 case COMPLEX_EXPR:
4547 expr = build_binary_op (EXPR_LOCATION (expr),
4548 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4549 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4550 c_common_truthvalue_conversion (location,
4551 TREE_OPERAND (expr, 0)),
4552 c_common_truthvalue_conversion (location,
4553 TREE_OPERAND (expr, 1)),
4555 goto ret;
4557 case NEGATE_EXPR:
4558 case ABS_EXPR:
4559 case FLOAT_EXPR:
4560 case EXCESS_PRECISION_EXPR:
4561 /* These don't change whether an object is nonzero or zero. */
4562 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4564 case LROTATE_EXPR:
4565 case RROTATE_EXPR:
4566 /* These don't change whether an object is zero or nonzero, but
4567 we can't ignore them if their second arg has side-effects. */
4568 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4570 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4571 TREE_OPERAND (expr, 1),
4572 c_common_truthvalue_conversion
4573 (location, TREE_OPERAND (expr, 0)));
4574 goto ret;
4576 else
4577 return c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 0));
4580 case COND_EXPR:
4581 /* Distribute the conversion into the arms of a COND_EXPR. */
4582 if (c_dialect_cxx ())
4584 tree op1 = TREE_OPERAND (expr, 1);
4585 tree op2 = TREE_OPERAND (expr, 2);
4586 /* In C++ one of the arms might have void type if it is throw. */
4587 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4588 op1 = c_common_truthvalue_conversion (location, op1);
4589 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4590 op2 = c_common_truthvalue_conversion (location, op2);
4591 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4592 TREE_OPERAND (expr, 0), op1, op2);
4593 goto ret;
4595 else
4597 /* Folding will happen later for C. */
4598 expr = build3 (COND_EXPR, truthvalue_type_node,
4599 TREE_OPERAND (expr, 0),
4600 c_common_truthvalue_conversion (location,
4601 TREE_OPERAND (expr, 1)),
4602 c_common_truthvalue_conversion (location,
4603 TREE_OPERAND (expr, 2)));
4604 goto ret;
4607 CASE_CONVERT:
4609 tree totype = TREE_TYPE (expr);
4610 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4612 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4613 since that affects how `default_conversion' will behave. */
4614 if (TREE_CODE (totype) == REFERENCE_TYPE
4615 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4616 break;
4617 /* Don't strip a conversion from C++0x scoped enum, since they
4618 don't implicitly convert to other types. */
4619 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4620 && ENUM_IS_SCOPED (fromtype))
4621 break;
4622 /* If this isn't narrowing the argument, we can ignore it. */
4623 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4624 return c_common_truthvalue_conversion (location,
4625 TREE_OPERAND (expr, 0));
4627 break;
4629 case MODIFY_EXPR:
4630 if (!TREE_NO_WARNING (expr)
4631 && warn_parentheses)
4633 warning (OPT_Wparentheses,
4634 "suggest parentheses around assignment used as truth value");
4635 TREE_NO_WARNING (expr) = 1;
4637 break;
4639 default:
4640 break;
4643 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4645 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4646 expr = (build_binary_op
4647 (EXPR_LOCATION (expr),
4648 (TREE_SIDE_EFFECTS (expr)
4649 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4650 c_common_truthvalue_conversion
4651 (location,
4652 build_unary_op (location, REALPART_EXPR, t, 0)),
4653 c_common_truthvalue_conversion
4654 (location,
4655 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4656 0));
4657 goto ret;
4660 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4662 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4663 FCONST0 (TYPE_MODE
4664 (TREE_TYPE (expr))));
4665 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4667 else
4668 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4670 ret:
4671 protected_set_expr_location (expr, location);
4672 return expr;
4675 static void def_builtin_1 (enum built_in_function fncode,
4676 const char *name,
4677 enum built_in_class fnclass,
4678 tree fntype, tree libtype,
4679 bool both_p, bool fallback_p, bool nonansi_p,
4680 tree fnattrs, bool implicit_p);
4683 /* Apply the TYPE_QUALS to the new DECL. */
4685 void
4686 c_apply_type_quals_to_decl (int type_quals, tree decl)
4688 tree type = TREE_TYPE (decl);
4690 if (type == error_mark_node)
4691 return;
4693 if ((type_quals & TYPE_QUAL_CONST)
4694 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4695 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4696 constructor can produce constant init, so rely on cp_finish_decl to
4697 clear TREE_READONLY if the variable has non-constant init. */
4698 TREE_READONLY (decl) = 1;
4699 if (type_quals & TYPE_QUAL_VOLATILE)
4701 TREE_SIDE_EFFECTS (decl) = 1;
4702 TREE_THIS_VOLATILE (decl) = 1;
4704 if (type_quals & TYPE_QUAL_RESTRICT)
4706 while (type && TREE_CODE (type) == ARRAY_TYPE)
4707 /* Allow 'restrict' on arrays of pointers.
4708 FIXME currently we just ignore it. */
4709 type = TREE_TYPE (type);
4710 if (!type
4711 || !POINTER_TYPE_P (type)
4712 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4713 error ("invalid use of %<restrict%>");
4715 if (type_quals & TYPE_QUAL_SHARED)
4717 TREE_SHARED (decl) = 1;
4718 if (type_quals & TYPE_QUAL_STRICT)
4719 TREE_STRICT(decl) = 1;
4720 else if (type_quals & TYPE_QUAL_RELAXED)
4721 TREE_RELAXED(decl) = 1;
4722 /* The declaration's type should have been previously defined
4723 as a UPC shared type. */
4724 gcc_assert (upc_shared_type_p (type));
4728 /* Hash function for the problem of multiple type definitions in
4729 different files. This must hash all types that will compare
4730 equal via comptypes to the same value. In practice it hashes
4731 on some of the simple stuff and leaves the details to comptypes. */
4733 static hashval_t
4734 c_type_hash (const void *p)
4736 int n_elements;
4737 int shift, size;
4738 const_tree const t = (const_tree) p;
4739 tree t2;
4740 switch (TREE_CODE (t))
4742 /* For pointers, hash on pointee type plus some swizzling. */
4743 case POINTER_TYPE:
4744 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4745 /* Hash on number of elements and total size. */
4746 case ENUMERAL_TYPE:
4747 shift = 3;
4748 t2 = TYPE_VALUES (t);
4749 break;
4750 case RECORD_TYPE:
4751 shift = 0;
4752 t2 = TYPE_FIELDS (t);
4753 break;
4754 case QUAL_UNION_TYPE:
4755 shift = 1;
4756 t2 = TYPE_FIELDS (t);
4757 break;
4758 case UNION_TYPE:
4759 shift = 2;
4760 t2 = TYPE_FIELDS (t);
4761 break;
4762 default:
4763 gcc_unreachable ();
4765 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4766 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4767 n_elements = list_length (t2);
4768 /* We might have a VLA here. */
4769 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4770 size = 0;
4771 else
4772 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4773 return ((size << 24) | (n_elements << shift));
4776 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4778 /* Return the typed-based alias set for T, which may be an expression
4779 or a type. Return -1 if we don't do anything special. */
4781 alias_set_type
4782 c_common_get_alias_set (tree t)
4784 tree u;
4785 PTR *slot;
4787 /* For VLAs, use the alias set of the element type rather than the
4788 default of alias set 0 for types compared structurally. */
4789 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4791 if (TREE_CODE (t) == ARRAY_TYPE)
4792 return get_alias_set (TREE_TYPE (t));
4793 return -1;
4796 /* Permit type-punning when accessing a union, provided the access
4797 is directly through the union. For example, this code does not
4798 permit taking the address of a union member and then storing
4799 through it. Even the type-punning allowed here is a GCC
4800 extension, albeit a common and useful one; the C standard says
4801 that such accesses have implementation-defined behavior. */
4802 for (u = t;
4803 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4804 u = TREE_OPERAND (u, 0))
4805 if (TREE_CODE (u) == COMPONENT_REF
4806 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4807 return 0;
4809 /* That's all the expressions we handle specially. */
4810 if (!TYPE_P (t))
4811 return -1;
4813 /* The C standard guarantees that any object may be accessed via an
4814 lvalue that has character type. */
4815 if (t == char_type_node
4816 || t == signed_char_type_node
4817 || t == unsigned_char_type_node)
4818 return 0;
4820 /* The C standard specifically allows aliasing between signed and
4821 unsigned variants of the same type. We treat the signed
4822 variant as canonical. */
4823 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4825 tree t1 = c_common_signed_type (t);
4827 /* t1 == t can happen for boolean nodes which are always unsigned. */
4828 if (t1 != t)
4829 return get_alias_set (t1);
4832 /* Handle the case of multiple type nodes referring to "the same" type,
4833 which occurs with IMA. These share an alias set. FIXME: Currently only
4834 C90 is handled. (In C99 type compatibility is not transitive, which
4835 complicates things mightily. The alias set splay trees can theoretically
4836 represent this, but insertion is tricky when you consider all the
4837 different orders things might arrive in.) */
4839 if (c_language != clk_c || flag_isoc99)
4840 return -1;
4842 /* Save time if there's only one input file. */
4843 if (num_in_fnames == 1)
4844 return -1;
4846 /* Pointers need special handling if they point to any type that
4847 needs special handling (below). */
4848 if (TREE_CODE (t) == POINTER_TYPE)
4850 tree t2;
4851 /* Find bottom type under any nested POINTERs. */
4852 for (t2 = TREE_TYPE (t);
4853 TREE_CODE (t2) == POINTER_TYPE;
4854 t2 = TREE_TYPE (t2))
4856 if (TREE_CODE (t2) != RECORD_TYPE
4857 && TREE_CODE (t2) != ENUMERAL_TYPE
4858 && TREE_CODE (t2) != QUAL_UNION_TYPE
4859 && TREE_CODE (t2) != UNION_TYPE)
4860 return -1;
4861 if (TYPE_SIZE (t2) == 0)
4862 return -1;
4864 /* These are the only cases that need special handling. */
4865 if (TREE_CODE (t) != RECORD_TYPE
4866 && TREE_CODE (t) != ENUMERAL_TYPE
4867 && TREE_CODE (t) != QUAL_UNION_TYPE
4868 && TREE_CODE (t) != UNION_TYPE
4869 && TREE_CODE (t) != POINTER_TYPE)
4870 return -1;
4871 /* Undefined? */
4872 if (TYPE_SIZE (t) == 0)
4873 return -1;
4875 /* Look up t in hash table. Only one of the compatible types within each
4876 alias set is recorded in the table. */
4877 if (!type_hash_table)
4878 type_hash_table = htab_create_ggc (1021, c_type_hash,
4879 (htab_eq) lang_hooks.types_compatible_p,
4880 NULL);
4881 slot = htab_find_slot (type_hash_table, t, INSERT);
4882 if (*slot != NULL)
4884 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4885 return TYPE_ALIAS_SET ((tree)*slot);
4887 else
4888 /* Our caller will assign and record (in t) a new alias set; all we need
4889 to do is remember t in the hash table. */
4890 *slot = t;
4892 return -1;
4895 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4896 the second parameter indicates which OPERATOR is being applied.
4897 The COMPLAIN flag controls whether we should diagnose possibly
4898 ill-formed constructs or not. LOC is the location of the SIZEOF or
4899 TYPEOF operator. */
4901 tree
4902 c_sizeof_or_alignof_type (location_t loc,
4903 tree type, bool is_sizeof, int complain)
4905 const char *op_name;
4906 tree value = NULL;
4907 enum tree_code type_code = TREE_CODE (type);
4909 op_name = is_sizeof ? "sizeof" : "__alignof__";
4911 if (type_code == FUNCTION_TYPE)
4913 if (is_sizeof)
4915 if (complain && (pedantic || warn_pointer_arith))
4916 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4917 "invalid application of %<sizeof%> to a function type");
4918 else if (!complain)
4919 return error_mark_node;
4920 value = size_one_node;
4922 else
4924 if (complain)
4926 if (c_dialect_cxx ())
4927 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4928 "%<alignof%> applied to a function type");
4929 else
4930 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4931 "%<_Alignof%> applied to a function type");
4933 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4936 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4938 if (complain)
4940 if (type_code == VOID_TYPE && upc_shared_type_p (type))
4941 error_at (loc, "invalid application of %qs"
4942 " to %<shared void%> type", op_name);
4943 else if (type_code == VOID_TYPE
4944 && (pedantic || warn_pointer_arith))
4945 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4946 "invalid application of %qs to a void type", op_name);
4948 else
4949 return error_mark_node;
4950 value = size_one_node;
4952 else if (!COMPLETE_TYPE_P (type)
4953 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4955 if (complain)
4956 error_at (loc, "invalid application of %qs to incomplete type %qT",
4957 op_name, type);
4958 return error_mark_node;
4960 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4961 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4963 if (complain)
4964 error_at (loc, "invalid application of %qs to array type %qT of "
4965 "incomplete element type", op_name, type);
4966 return error_mark_node;
4968 else
4970 if (is_sizeof)
4971 /* Convert in case a char is more than one unit. */
4972 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4973 size_int (TYPE_PRECISION (char_type_node)
4974 / BITS_PER_UNIT));
4975 else
4976 value = size_int (TYPE_ALIGN_UNIT (type));
4979 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
4980 && upc_shared_type_p (type)
4981 && TYPE_HAS_THREADS_FACTOR (type))
4983 const tree n_threads = convert (sizetype, upc_num_threads ());
4984 value = size_binop (MULT_EXPR, value, n_threads);
4987 /* VALUE will have the middle-end integer type sizetype.
4988 However, we should really return a value of type `size_t',
4989 which is just a typedef for an ordinary integer type. */
4990 value = fold_convert_loc (loc, size_type_node, value);
4992 return value;
4995 /* Implement the __alignof keyword: Return the minimum required
4996 alignment of EXPR, measured in bytes. For VAR_DECLs,
4997 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4998 from an "aligned" __attribute__ specification). LOC is the
4999 location of the ALIGNOF operator. */
5001 tree
5002 c_alignof_expr (location_t loc, tree expr)
5004 tree t;
5006 if (VAR_OR_FUNCTION_DECL_P (expr))
5007 t = size_int (DECL_ALIGN_UNIT (expr));
5009 else if (TREE_CODE (expr) == COMPONENT_REF
5010 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5012 error_at (loc, "%<__alignof%> applied to a bit-field");
5013 t = size_one_node;
5015 else if (TREE_CODE (expr) == COMPONENT_REF
5016 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5017 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5019 else if (TREE_CODE (expr) == INDIRECT_REF)
5021 tree t = TREE_OPERAND (expr, 0);
5022 tree best = t;
5023 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5025 while (CONVERT_EXPR_P (t)
5026 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5028 int thisalign;
5030 t = TREE_OPERAND (t, 0);
5031 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5032 if (thisalign > bestalign)
5033 best = t, bestalign = thisalign;
5035 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5037 else
5038 return c_alignof (loc, TREE_TYPE (expr));
5040 return fold_convert_loc (loc, size_type_node, t);
5043 /* Handle C and C++ default attributes. */
5045 enum built_in_attribute
5047 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5048 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5049 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5050 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5051 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5052 #include "builtin-attrs.def"
5053 #undef DEF_ATTR_NULL_TREE
5054 #undef DEF_ATTR_INT
5055 #undef DEF_ATTR_STRING
5056 #undef DEF_ATTR_IDENT
5057 #undef DEF_ATTR_TREE_LIST
5058 ATTR_LAST
5061 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5063 static void c_init_attributes (void);
5065 enum c_builtin_type
5067 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5068 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5069 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5070 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5071 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5072 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5073 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5074 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5075 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5076 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5077 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5078 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5082 NAME,
5083 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5084 #include "builtin-types.def"
5085 #undef DEF_PRIMITIVE_TYPE
5086 #undef DEF_FUNCTION_TYPE_0
5087 #undef DEF_FUNCTION_TYPE_1
5088 #undef DEF_FUNCTION_TYPE_2
5089 #undef DEF_FUNCTION_TYPE_3
5090 #undef DEF_FUNCTION_TYPE_4
5091 #undef DEF_FUNCTION_TYPE_5
5092 #undef DEF_FUNCTION_TYPE_6
5093 #undef DEF_FUNCTION_TYPE_7
5094 #undef DEF_FUNCTION_TYPE_VAR_0
5095 #undef DEF_FUNCTION_TYPE_VAR_1
5096 #undef DEF_FUNCTION_TYPE_VAR_2
5097 #undef DEF_FUNCTION_TYPE_VAR_3
5098 #undef DEF_FUNCTION_TYPE_VAR_4
5099 #undef DEF_FUNCTION_TYPE_VAR_5
5100 #undef DEF_POINTER_TYPE
5101 BT_LAST
5104 typedef enum c_builtin_type builtin_type;
5106 /* A temporary array for c_common_nodes_and_builtins. Used in
5107 communication with def_fn_type. */
5108 static tree builtin_types[(int) BT_LAST + 1];
5110 /* A helper function for c_common_nodes_and_builtins. Build function type
5111 for DEF with return type RET and N arguments. If VAR is true, then the
5112 function should be variadic after those N arguments.
5114 Takes special care not to ICE if any of the types involved are
5115 error_mark_node, which indicates that said type is not in fact available
5116 (see builtin_type_for_size). In which case the function type as a whole
5117 should be error_mark_node. */
5119 static void
5120 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5122 tree t;
5123 tree *args = XALLOCAVEC (tree, n);
5124 va_list list;
5125 int i;
5127 va_start (list, n);
5128 for (i = 0; i < n; ++i)
5130 builtin_type a = (builtin_type) va_arg (list, int);
5131 t = builtin_types[a];
5132 if (t == error_mark_node)
5133 goto egress;
5134 args[i] = t;
5137 t = builtin_types[ret];
5138 if (t == error_mark_node)
5139 goto egress;
5140 if (var)
5141 t = build_varargs_function_type_array (t, n, args);
5142 else
5143 t = build_function_type_array (t, n, args);
5145 egress:
5146 builtin_types[def] = t;
5147 va_end (list);
5150 /* Build builtin functions common to both C and C++ language
5151 frontends. */
5153 static void
5154 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5156 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5157 builtin_types[ENUM] = VALUE;
5158 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5159 def_fn_type (ENUM, RETURN, 0, 0);
5160 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5161 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5162 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5163 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5164 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5165 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5166 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5167 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5168 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5169 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5170 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5171 ARG6) \
5172 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5173 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5174 ARG6, ARG7) \
5175 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5176 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5177 def_fn_type (ENUM, RETURN, 1, 0);
5178 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5179 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5180 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5181 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5182 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5183 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5184 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5185 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5186 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5187 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5188 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5189 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5191 #include "builtin-types.def"
5193 #undef DEF_PRIMITIVE_TYPE
5194 #undef DEF_FUNCTION_TYPE_1
5195 #undef DEF_FUNCTION_TYPE_2
5196 #undef DEF_FUNCTION_TYPE_3
5197 #undef DEF_FUNCTION_TYPE_4
5198 #undef DEF_FUNCTION_TYPE_5
5199 #undef DEF_FUNCTION_TYPE_6
5200 #undef DEF_FUNCTION_TYPE_VAR_0
5201 #undef DEF_FUNCTION_TYPE_VAR_1
5202 #undef DEF_FUNCTION_TYPE_VAR_2
5203 #undef DEF_FUNCTION_TYPE_VAR_3
5204 #undef DEF_FUNCTION_TYPE_VAR_4
5205 #undef DEF_FUNCTION_TYPE_VAR_5
5206 #undef DEF_POINTER_TYPE
5207 builtin_types[(int) BT_LAST] = NULL_TREE;
5209 c_init_attributes ();
5211 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5212 NONANSI_P, ATTRS, IMPLICIT, COND) \
5213 if (NAME && COND) \
5214 def_builtin_1 (ENUM, NAME, CLASS, \
5215 builtin_types[(int) TYPE], \
5216 builtin_types[(int) LIBTYPE], \
5217 BOTH_P, FALLBACK_P, NONANSI_P, \
5218 built_in_attributes[(int) ATTRS], IMPLICIT);
5219 #include "builtins.def"
5220 #undef DEF_BUILTIN
5222 targetm.init_builtins ();
5224 build_common_builtin_nodes ();
5226 if (flag_mudflap)
5227 mudflap_init ();
5230 /* Like get_identifier, but avoid warnings about null arguments when
5231 the argument may be NULL for targets where GCC lacks stdint.h type
5232 information. */
5234 static inline tree
5235 c_get_ident (const char *id)
5237 return get_identifier (id);
5240 /* Build tree nodes and builtin functions common to both C and C++ language
5241 frontends. */
5243 void
5244 c_common_nodes_and_builtins (void)
5246 int char16_type_size;
5247 int char32_type_size;
5248 int wchar_type_size;
5249 tree array_domain_type;
5250 tree va_list_ref_type_node;
5251 tree va_list_arg_type_node;
5253 build_common_tree_nodes (flag_signed_char, flag_short_double);
5255 /* Define `int' and `char' first so that dbx will output them first. */
5256 record_builtin_type (RID_INT, NULL, integer_type_node);
5257 record_builtin_type (RID_CHAR, "char", char_type_node);
5259 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5260 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5261 but not C. Are the conditionals here needed? */
5262 if (c_dialect_cxx ())
5263 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5264 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5265 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5266 record_builtin_type (RID_MAX, "long unsigned int",
5267 long_unsigned_type_node);
5268 if (int128_integer_type_node != NULL_TREE)
5270 record_builtin_type (RID_INT128, "__int128",
5271 int128_integer_type_node);
5272 record_builtin_type (RID_MAX, "__int128 unsigned",
5273 int128_unsigned_type_node);
5275 if (c_dialect_cxx ())
5276 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5277 record_builtin_type (RID_MAX, "long long int",
5278 long_long_integer_type_node);
5279 record_builtin_type (RID_MAX, "long long unsigned int",
5280 long_long_unsigned_type_node);
5281 if (c_dialect_cxx ())
5282 record_builtin_type (RID_MAX, "long long unsigned",
5283 long_long_unsigned_type_node);
5284 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5285 record_builtin_type (RID_MAX, "short unsigned int",
5286 short_unsigned_type_node);
5287 if (c_dialect_cxx ())
5288 record_builtin_type (RID_MAX, "unsigned short",
5289 short_unsigned_type_node);
5291 /* Define both `signed char' and `unsigned char'. */
5292 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5293 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5295 /* These are types that c_common_type_for_size and
5296 c_common_type_for_mode use. */
5297 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5298 TYPE_DECL, NULL_TREE,
5299 intQI_type_node));
5300 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5301 TYPE_DECL, NULL_TREE,
5302 intHI_type_node));
5303 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5304 TYPE_DECL, NULL_TREE,
5305 intSI_type_node));
5306 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5307 TYPE_DECL, NULL_TREE,
5308 intDI_type_node));
5309 #if HOST_BITS_PER_WIDE_INT >= 64
5310 if (targetm.scalar_mode_supported_p (TImode))
5311 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5312 TYPE_DECL,
5313 get_identifier ("__int128_t"),
5314 intTI_type_node));
5315 #endif
5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL, NULL_TREE,
5318 unsigned_intQI_type_node));
5319 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5320 TYPE_DECL, NULL_TREE,
5321 unsigned_intHI_type_node));
5322 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5323 TYPE_DECL, NULL_TREE,
5324 unsigned_intSI_type_node));
5325 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5326 TYPE_DECL, NULL_TREE,
5327 unsigned_intDI_type_node));
5328 #if HOST_BITS_PER_WIDE_INT >= 64
5329 if (targetm.scalar_mode_supported_p (TImode))
5330 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5331 TYPE_DECL,
5332 get_identifier ("__uint128_t"),
5333 unsigned_intTI_type_node));
5334 #endif
5336 /* Create the widest literal types. */
5337 widest_integer_literal_type_node
5338 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5339 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5340 TYPE_DECL, NULL_TREE,
5341 widest_integer_literal_type_node));
5343 widest_unsigned_literal_type_node
5344 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5345 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5346 TYPE_DECL, NULL_TREE,
5347 widest_unsigned_literal_type_node));
5349 signed_size_type_node = c_common_signed_type (size_type_node);
5351 pid_type_node =
5352 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5354 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5355 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5356 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5358 /* Only supported decimal floating point extension if the target
5359 actually supports underlying modes. */
5360 if (targetm.scalar_mode_supported_p (SDmode)
5361 && targetm.scalar_mode_supported_p (DDmode)
5362 && targetm.scalar_mode_supported_p (TDmode))
5364 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5365 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5366 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5369 if (targetm.fixed_point_supported_p ())
5371 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5372 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5373 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5374 record_builtin_type (RID_MAX, "long long _Fract",
5375 long_long_fract_type_node);
5376 record_builtin_type (RID_MAX, "unsigned short _Fract",
5377 unsigned_short_fract_type_node);
5378 record_builtin_type (RID_MAX, "unsigned _Fract",
5379 unsigned_fract_type_node);
5380 record_builtin_type (RID_MAX, "unsigned long _Fract",
5381 unsigned_long_fract_type_node);
5382 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5383 unsigned_long_long_fract_type_node);
5384 record_builtin_type (RID_MAX, "_Sat short _Fract",
5385 sat_short_fract_type_node);
5386 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5387 record_builtin_type (RID_MAX, "_Sat long _Fract",
5388 sat_long_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5390 sat_long_long_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5392 sat_unsigned_short_fract_type_node);
5393 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5394 sat_unsigned_fract_type_node);
5395 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5396 sat_unsigned_long_fract_type_node);
5397 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5398 sat_unsigned_long_long_fract_type_node);
5399 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5400 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5401 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5402 record_builtin_type (RID_MAX, "long long _Accum",
5403 long_long_accum_type_node);
5404 record_builtin_type (RID_MAX, "unsigned short _Accum",
5405 unsigned_short_accum_type_node);
5406 record_builtin_type (RID_MAX, "unsigned _Accum",
5407 unsigned_accum_type_node);
5408 record_builtin_type (RID_MAX, "unsigned long _Accum",
5409 unsigned_long_accum_type_node);
5410 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5411 unsigned_long_long_accum_type_node);
5412 record_builtin_type (RID_MAX, "_Sat short _Accum",
5413 sat_short_accum_type_node);
5414 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5415 record_builtin_type (RID_MAX, "_Sat long _Accum",
5416 sat_long_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5418 sat_long_long_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5420 sat_unsigned_short_accum_type_node);
5421 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5422 sat_unsigned_accum_type_node);
5423 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5424 sat_unsigned_long_accum_type_node);
5425 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5426 sat_unsigned_long_long_accum_type_node);
5430 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5431 TYPE_DECL,
5432 get_identifier ("complex int"),
5433 complex_integer_type_node));
5434 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5435 TYPE_DECL,
5436 get_identifier ("complex float"),
5437 complex_float_type_node));
5438 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL,
5440 get_identifier ("complex double"),
5441 complex_double_type_node));
5442 lang_hooks.decls.pushdecl
5443 (build_decl (UNKNOWN_LOCATION,
5444 TYPE_DECL, get_identifier ("complex long double"),
5445 complex_long_double_type_node));
5447 if (c_dialect_cxx ())
5448 /* For C++, make fileptr_type_node a distinct void * type until
5449 FILE type is defined. */
5450 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5452 record_builtin_type (RID_VOID, NULL, void_type_node);
5454 /* Set the TYPE_NAME for any variants that were built before
5455 record_builtin_type gave names to the built-in types. */
5457 tree void_name = TYPE_NAME (void_type_node);
5458 TYPE_NAME (void_type_node) = NULL_TREE;
5459 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5460 = void_name;
5461 TYPE_NAME (void_type_node) = void_name;
5464 /* This node must not be shared. */
5465 void_zero_node = make_node (INTEGER_CST);
5466 TREE_TYPE (void_zero_node) = void_type_node;
5468 void_list_node = build_void_list_node ();
5470 /* Make a type to be the domain of a few array types
5471 whose domains don't really matter.
5472 200 is small enough that it always fits in size_t
5473 and large enough that it can hold most function names for the
5474 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5475 array_domain_type = build_index_type (size_int (200));
5477 /* Make a type for arrays of characters.
5478 With luck nothing will ever really depend on the length of this
5479 array type. */
5480 char_array_type_node
5481 = build_array_type (char_type_node, array_domain_type);
5483 /* Likewise for arrays of ints. */
5484 int_array_type_node
5485 = build_array_type (integer_type_node, array_domain_type);
5487 string_type_node = build_pointer_type (char_type_node);
5488 const_string_type_node
5489 = build_pointer_type (build_qualified_type
5490 (char_type_node, TYPE_QUAL_CONST));
5492 /* This is special for C++ so functions can be overloaded. */
5493 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5494 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5495 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5496 underlying_wchar_type_node = wchar_type_node;
5497 if (c_dialect_cxx ())
5499 if (TYPE_UNSIGNED (wchar_type_node))
5500 wchar_type_node = make_unsigned_type (wchar_type_size);
5501 else
5502 wchar_type_node = make_signed_type (wchar_type_size);
5503 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5506 /* This is for wide string constants. */
5507 wchar_array_type_node
5508 = build_array_type (wchar_type_node, array_domain_type);
5510 /* Define 'char16_t'. */
5511 char16_type_node = get_identifier (CHAR16_TYPE);
5512 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5513 char16_type_size = TYPE_PRECISION (char16_type_node);
5514 if (c_dialect_cxx ())
5516 char16_type_node = make_unsigned_type (char16_type_size);
5518 if (cxx_dialect >= cxx0x)
5519 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5522 /* This is for UTF-16 string constants. */
5523 char16_array_type_node
5524 = build_array_type (char16_type_node, array_domain_type);
5526 /* Define 'char32_t'. */
5527 char32_type_node = get_identifier (CHAR32_TYPE);
5528 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5529 char32_type_size = TYPE_PRECISION (char32_type_node);
5530 if (c_dialect_cxx ())
5532 char32_type_node = make_unsigned_type (char32_type_size);
5534 if (cxx_dialect >= cxx0x)
5535 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5538 /* This is for UTF-32 string constants. */
5539 char32_array_type_node
5540 = build_array_type (char32_type_node, array_domain_type);
5542 wint_type_node =
5543 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5545 intmax_type_node =
5546 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5547 uintmax_type_node =
5548 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5550 if (SIG_ATOMIC_TYPE)
5551 sig_atomic_type_node =
5552 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5553 if (INT8_TYPE)
5554 int8_type_node =
5555 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5556 if (INT16_TYPE)
5557 int16_type_node =
5558 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5559 if (INT32_TYPE)
5560 int32_type_node =
5561 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5562 if (INT64_TYPE)
5563 int64_type_node =
5564 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5565 if (UINT8_TYPE)
5566 uint8_type_node =
5567 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5568 if (UINT16_TYPE)
5569 c_uint16_type_node =
5570 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5571 if (UINT32_TYPE)
5572 c_uint32_type_node =
5573 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5574 if (UINT64_TYPE)
5575 c_uint64_type_node =
5576 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5577 if (INT_LEAST8_TYPE)
5578 int_least8_type_node =
5579 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5580 if (INT_LEAST16_TYPE)
5581 int_least16_type_node =
5582 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5583 if (INT_LEAST32_TYPE)
5584 int_least32_type_node =
5585 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5586 if (INT_LEAST64_TYPE)
5587 int_least64_type_node =
5588 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5589 if (UINT_LEAST8_TYPE)
5590 uint_least8_type_node =
5591 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5592 if (UINT_LEAST16_TYPE)
5593 uint_least16_type_node =
5594 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5595 if (UINT_LEAST32_TYPE)
5596 uint_least32_type_node =
5597 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5598 if (UINT_LEAST64_TYPE)
5599 uint_least64_type_node =
5600 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5601 if (INT_FAST8_TYPE)
5602 int_fast8_type_node =
5603 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5604 if (INT_FAST16_TYPE)
5605 int_fast16_type_node =
5606 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5607 if (INT_FAST32_TYPE)
5608 int_fast32_type_node =
5609 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5610 if (INT_FAST64_TYPE)
5611 int_fast64_type_node =
5612 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5613 if (UINT_FAST8_TYPE)
5614 uint_fast8_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5616 if (UINT_FAST16_TYPE)
5617 uint_fast16_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5619 if (UINT_FAST32_TYPE)
5620 uint_fast32_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5622 if (UINT_FAST64_TYPE)
5623 uint_fast64_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5625 if (INTPTR_TYPE)
5626 intptr_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5628 if (UINTPTR_TYPE)
5629 uintptr_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5632 default_function_type
5633 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5634 ptrdiff_type_node
5635 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5636 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5638 lang_hooks.decls.pushdecl
5639 (build_decl (UNKNOWN_LOCATION,
5640 TYPE_DECL, get_identifier ("__builtin_va_list"),
5641 va_list_type_node));
5642 if (targetm.enum_va_list_p)
5644 int l;
5645 const char *pname;
5646 tree ptype;
5648 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5650 lang_hooks.decls.pushdecl
5651 (build_decl (UNKNOWN_LOCATION,
5652 TYPE_DECL, get_identifier (pname),
5653 ptype));
5658 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5660 va_list_arg_type_node = va_list_ref_type_node =
5661 build_pointer_type (TREE_TYPE (va_list_type_node));
5663 else
5665 va_list_arg_type_node = va_list_type_node;
5666 va_list_ref_type_node = build_reference_type (va_list_type_node);
5669 if (!flag_preprocess_only)
5670 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5672 main_identifier_node = get_identifier ("main");
5674 /* Create the built-in __null node. It is important that this is
5675 not shared. */
5676 null_node = make_node (INTEGER_CST);
5677 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5679 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5680 memset (builtin_types, 0, sizeof (builtin_types));
5683 /* The number of named compound-literals generated thus far. */
5684 static GTY(()) int compound_literal_number;
5686 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5688 void
5689 set_compound_literal_name (tree decl)
5691 char *name;
5692 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5693 compound_literal_number);
5694 compound_literal_number++;
5695 DECL_NAME (decl) = get_identifier (name);
5698 tree
5699 build_va_arg (location_t loc, tree expr, tree type)
5701 expr = build1 (VA_ARG_EXPR, type, expr);
5702 SET_EXPR_LOCATION (expr, loc);
5703 return expr;
5707 /* Linked list of disabled built-in functions. */
5709 typedef struct disabled_builtin
5711 const char *name;
5712 struct disabled_builtin *next;
5713 } disabled_builtin;
5714 static disabled_builtin *disabled_builtins = NULL;
5716 static bool builtin_function_disabled_p (const char *);
5718 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5719 begins with "__builtin_", give an error. */
5721 void
5722 disable_builtin_function (const char *name)
5724 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5725 error ("cannot disable built-in function %qs", name);
5726 else
5728 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5729 new_disabled_builtin->name = name;
5730 new_disabled_builtin->next = disabled_builtins;
5731 disabled_builtins = new_disabled_builtin;
5736 /* Return true if the built-in function NAME has been disabled, false
5737 otherwise. */
5739 static bool
5740 builtin_function_disabled_p (const char *name)
5742 disabled_builtin *p;
5743 for (p = disabled_builtins; p != NULL; p = p->next)
5745 if (strcmp (name, p->name) == 0)
5746 return true;
5748 return false;
5752 /* Worker for DEF_BUILTIN.
5753 Possibly define a builtin function with one or two names.
5754 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5755 nonansi_p and flag_no_nonansi_builtin. */
5757 static void
5758 def_builtin_1 (enum built_in_function fncode,
5759 const char *name,
5760 enum built_in_class fnclass,
5761 tree fntype, tree libtype,
5762 bool both_p, bool fallback_p, bool nonansi_p,
5763 tree fnattrs, bool implicit_p)
5765 tree decl;
5766 const char *libname;
5768 if (fntype == error_mark_node)
5769 return;
5771 gcc_assert ((!both_p && !fallback_p)
5772 || !strncmp (name, "__builtin_",
5773 strlen ("__builtin_")));
5775 libname = name + strlen ("__builtin_");
5776 decl = add_builtin_function (name, fntype, fncode, fnclass,
5777 (fallback_p ? libname : NULL),
5778 fnattrs);
5780 set_builtin_decl (fncode, decl, implicit_p);
5782 if (both_p
5783 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5784 && !(nonansi_p && flag_no_nonansi_builtin))
5785 add_builtin_function (libname, libtype, fncode, fnclass,
5786 NULL, fnattrs);
5789 /* Nonzero if the type T promotes to int. This is (nearly) the
5790 integral promotions defined in ISO C99 6.3.1.1/2. */
5792 bool
5793 c_promoting_integer_type_p (const_tree t)
5795 switch (TREE_CODE (t))
5797 case INTEGER_TYPE:
5798 return (TYPE_MAIN_VARIANT (t) == char_type_node
5799 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5800 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5801 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5802 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5803 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5805 case ENUMERAL_TYPE:
5806 /* ??? Technically all enumerations not larger than an int
5807 promote to an int. But this is used along code paths
5808 that only want to notice a size change. */
5809 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5811 case BOOLEAN_TYPE:
5812 return 1;
5814 default:
5815 return 0;
5819 /* Return 1 if PARMS specifies a fixed number of parameters
5820 and none of their types is affected by default promotions. */
5823 self_promoting_args_p (const_tree parms)
5825 const_tree t;
5826 for (t = parms; t; t = TREE_CHAIN (t))
5828 tree type = TREE_VALUE (t);
5830 if (type == error_mark_node)
5831 continue;
5833 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5834 return 0;
5836 if (type == 0)
5837 return 0;
5839 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5840 return 0;
5842 if (c_promoting_integer_type_p (type))
5843 return 0;
5845 return 1;
5848 /* Recursively remove any '*' or '&' operator from TYPE. */
5849 tree
5850 strip_pointer_operator (tree t)
5852 while (POINTER_TYPE_P (t))
5853 t = TREE_TYPE (t);
5854 return t;
5857 /* Recursively remove pointer or array type from TYPE. */
5858 tree
5859 strip_pointer_or_array_types (tree t)
5861 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5862 t = TREE_TYPE (t);
5863 return t;
5866 /* Used to compare case labels. K1 and K2 are actually tree nodes
5867 representing case labels, or NULL_TREE for a `default' label.
5868 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5869 K2, and 0 if K1 and K2 are equal. */
5872 case_compare (splay_tree_key k1, splay_tree_key k2)
5874 /* Consider a NULL key (such as arises with a `default' label) to be
5875 smaller than anything else. */
5876 if (!k1)
5877 return k2 ? -1 : 0;
5878 else if (!k2)
5879 return k1 ? 1 : 0;
5881 return tree_int_cst_compare ((tree) k1, (tree) k2);
5884 /* Process a case label, located at LOC, for the range LOW_VALUE
5885 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5886 then this case label is actually a `default' label. If only
5887 HIGH_VALUE is NULL_TREE, then case label was declared using the
5888 usual C/C++ syntax, rather than the GNU case range extension.
5889 CASES is a tree containing all the case ranges processed so far;
5890 COND is the condition for the switch-statement itself. Returns the
5891 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5892 is created. */
5894 tree
5895 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5896 tree low_value, tree high_value)
5898 tree type;
5899 tree label;
5900 tree case_label;
5901 splay_tree_node node;
5903 /* Create the LABEL_DECL itself. */
5904 label = create_artificial_label (loc);
5906 /* If there was an error processing the switch condition, bail now
5907 before we get more confused. */
5908 if (!cond || cond == error_mark_node)
5909 goto error_out;
5911 if ((low_value && TREE_TYPE (low_value)
5912 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5913 || (high_value && TREE_TYPE (high_value)
5914 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5916 error_at (loc, "pointers are not permitted as case values");
5917 goto error_out;
5920 /* Case ranges are a GNU extension. */
5921 if (high_value)
5922 pedwarn (loc, OPT_Wpedantic,
5923 "range expressions in switch statements are non-standard");
5925 type = TREE_TYPE (cond);
5926 if (low_value)
5928 low_value = check_case_value (low_value);
5929 low_value = convert_and_check (type, low_value);
5930 if (low_value == error_mark_node)
5931 goto error_out;
5933 if (high_value)
5935 high_value = check_case_value (high_value);
5936 high_value = convert_and_check (type, high_value);
5937 if (high_value == error_mark_node)
5938 goto error_out;
5941 if (low_value && high_value)
5943 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5944 really a case range, even though it was written that way.
5945 Remove the HIGH_VALUE to simplify later processing. */
5946 if (tree_int_cst_equal (low_value, high_value))
5947 high_value = NULL_TREE;
5948 else if (!tree_int_cst_lt (low_value, high_value))
5949 warning_at (loc, 0, "empty range specified");
5952 /* See if the case is in range of the type of the original testing
5953 expression. If both low_value and high_value are out of range,
5954 don't insert the case label and return NULL_TREE. */
5955 if (low_value
5956 && !check_case_bounds (type, orig_type,
5957 &low_value, high_value ? &high_value : NULL))
5958 return NULL_TREE;
5960 /* Look up the LOW_VALUE in the table of case labels we already
5961 have. */
5962 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5963 /* If there was not an exact match, check for overlapping ranges.
5964 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5965 that's a `default' label and the only overlap is an exact match. */
5966 if (!node && (low_value || high_value))
5968 splay_tree_node low_bound;
5969 splay_tree_node high_bound;
5971 /* Even though there wasn't an exact match, there might be an
5972 overlap between this case range and another case range.
5973 Since we've (inductively) not allowed any overlapping case
5974 ranges, we simply need to find the greatest low case label
5975 that is smaller that LOW_VALUE, and the smallest low case
5976 label that is greater than LOW_VALUE. If there is an overlap
5977 it will occur in one of these two ranges. */
5978 low_bound = splay_tree_predecessor (cases,
5979 (splay_tree_key) low_value);
5980 high_bound = splay_tree_successor (cases,
5981 (splay_tree_key) low_value);
5983 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5984 the LOW_VALUE, so there is no need to check unless the
5985 LOW_BOUND is in fact itself a case range. */
5986 if (low_bound
5987 && CASE_HIGH ((tree) low_bound->value)
5988 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5989 low_value) >= 0)
5990 node = low_bound;
5991 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5992 range is bigger than the low end of the current range, so we
5993 are only interested if the current range is a real range, and
5994 not an ordinary case label. */
5995 else if (high_bound
5996 && high_value
5997 && (tree_int_cst_compare ((tree) high_bound->key,
5998 high_value)
5999 <= 0))
6000 node = high_bound;
6002 /* If there was an overlap, issue an error. */
6003 if (node)
6005 tree duplicate = CASE_LABEL ((tree) node->value);
6007 if (high_value)
6009 error_at (loc, "duplicate (or overlapping) case value");
6010 error_at (DECL_SOURCE_LOCATION (duplicate),
6011 "this is the first entry overlapping that value");
6013 else if (low_value)
6015 error_at (loc, "duplicate case value") ;
6016 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6018 else
6020 error_at (loc, "multiple default labels in one switch");
6021 error_at (DECL_SOURCE_LOCATION (duplicate),
6022 "this is the first default label");
6024 goto error_out;
6027 /* Add a CASE_LABEL to the statement-tree. */
6028 case_label = add_stmt (build_case_label (low_value, high_value, label));
6029 /* Register this case label in the splay tree. */
6030 splay_tree_insert (cases,
6031 (splay_tree_key) low_value,
6032 (splay_tree_value) case_label);
6034 return case_label;
6036 error_out:
6037 /* Add a label so that the back-end doesn't think that the beginning of
6038 the switch is unreachable. Note that we do not add a case label, as
6039 that just leads to duplicates and thence to failure later on. */
6040 if (!cases->root)
6042 tree t = create_artificial_label (loc);
6043 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6045 return error_mark_node;
6048 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6049 Used to verify that case values match up with enumerator values. */
6051 static void
6052 match_case_to_enum_1 (tree key, tree type, tree label)
6054 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6056 /* ??? Not working too hard to print the double-word value.
6057 Should perhaps be done with %lwd in the diagnostic routines? */
6058 if (TREE_INT_CST_HIGH (key) == 0)
6059 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6060 TREE_INT_CST_LOW (key));
6061 else if (!TYPE_UNSIGNED (type)
6062 && TREE_INT_CST_HIGH (key) == -1
6063 && TREE_INT_CST_LOW (key) != 0)
6064 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6065 -TREE_INT_CST_LOW (key));
6066 else
6067 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6068 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6069 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6071 if (TYPE_NAME (type) == 0)
6072 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6073 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6074 "case value %qs not in enumerated type",
6075 buf);
6076 else
6077 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6078 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6079 "case value %qs not in enumerated type %qT",
6080 buf, type);
6083 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6084 Used to verify that case values match up with enumerator values. */
6086 static int
6087 match_case_to_enum (splay_tree_node node, void *data)
6089 tree label = (tree) node->value;
6090 tree type = (tree) data;
6092 /* Skip default case. */
6093 if (!CASE_LOW (label))
6094 return 0;
6096 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6097 when we did our enum->case scan. Reset our scratch bit after. */
6098 if (!CASE_LOW_SEEN (label))
6099 match_case_to_enum_1 (CASE_LOW (label), type, label);
6100 else
6101 CASE_LOW_SEEN (label) = 0;
6103 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6104 not set, that means that CASE_HIGH did not appear when we did our
6105 enum->case scan. Reset our scratch bit after. */
6106 if (CASE_HIGH (label))
6108 if (!CASE_HIGH_SEEN (label))
6109 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6110 else
6111 CASE_HIGH_SEEN (label) = 0;
6114 return 0;
6117 /* Handle -Wswitch*. Called from the front end after parsing the
6118 switch construct. */
6119 /* ??? Should probably be somewhere generic, since other languages
6120 besides C and C++ would want this. At the moment, however, C/C++
6121 are the only tree-ssa languages that support enumerations at all,
6122 so the point is moot. */
6124 void
6125 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6126 tree type, tree cond)
6128 splay_tree_node default_node;
6129 splay_tree_node node;
6130 tree chain;
6132 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6133 return;
6135 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6136 if (!default_node)
6137 warning_at (switch_location, OPT_Wswitch_default,
6138 "switch missing default case");
6140 /* From here on, we only care about about enumerated types. */
6141 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6142 return;
6144 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6145 if (!warn_switch_enum && !warn_switch)
6146 return;
6148 /* Check the cases. Warn about case values which are not members of
6149 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6150 there is no default case, check that exactly all enumeration
6151 literals are covered by the cases. */
6153 /* Clearing COND if it is not an integer constant simplifies
6154 the tests inside the loop below. */
6155 if (TREE_CODE (cond) != INTEGER_CST)
6156 cond = NULL_TREE;
6158 /* The time complexity here is O(N*lg(N)) worst case, but for the
6159 common case of monotonically increasing enumerators, it is
6160 O(N), since the nature of the splay tree will keep the next
6161 element adjacent to the root at all times. */
6163 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6165 tree value = TREE_VALUE (chain);
6166 if (TREE_CODE (value) == CONST_DECL)
6167 value = DECL_INITIAL (value);
6168 node = splay_tree_lookup (cases, (splay_tree_key) value);
6169 if (node)
6171 /* Mark the CASE_LOW part of the case entry as seen. */
6172 tree label = (tree) node->value;
6173 CASE_LOW_SEEN (label) = 1;
6174 continue;
6177 /* Even though there wasn't an exact match, there might be a
6178 case range which includes the enumerator's value. */
6179 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6180 if (node && CASE_HIGH ((tree) node->value))
6182 tree label = (tree) node->value;
6183 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6184 if (cmp >= 0)
6186 /* If we match the upper bound exactly, mark the CASE_HIGH
6187 part of the case entry as seen. */
6188 if (cmp == 0)
6189 CASE_HIGH_SEEN (label) = 1;
6190 continue;
6194 /* We've now determined that this enumerated literal isn't
6195 handled by the case labels of the switch statement. */
6197 /* If the switch expression is a constant, we only really care
6198 about whether that constant is handled by the switch. */
6199 if (cond && tree_int_cst_compare (cond, value))
6200 continue;
6202 /* If there is a default_node, the only relevant option is
6203 Wswitch-enum. Otherwise, if both are enabled then we prefer
6204 to warn using -Wswitch because -Wswitch is enabled by -Wall
6205 while -Wswitch-enum is explicit. */
6206 warning_at (switch_location,
6207 (default_node || !warn_switch
6208 ? OPT_Wswitch_enum
6209 : OPT_Wswitch),
6210 "enumeration value %qE not handled in switch",
6211 TREE_PURPOSE (chain));
6214 /* Warn if there are case expressions that don't correspond to
6215 enumerators. This can occur since C and C++ don't enforce
6216 type-checking of assignments to enumeration variables.
6218 The time complexity here is now always O(N) worst case, since
6219 we should have marked both the lower bound and upper bound of
6220 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6221 above. This scan also resets those fields. */
6223 splay_tree_foreach (cases, match_case_to_enum, type);
6226 /* Finish an expression taking the address of LABEL (an
6227 IDENTIFIER_NODE). Returns an expression for the address.
6229 LOC is the location for the expression returned. */
6231 tree
6232 finish_label_address_expr (tree label, location_t loc)
6234 tree result;
6236 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6238 if (label == error_mark_node)
6239 return error_mark_node;
6241 label = lookup_label (label);
6242 if (label == NULL_TREE)
6243 result = null_pointer_node;
6244 else
6246 TREE_USED (label) = 1;
6247 result = build1 (ADDR_EXPR, ptr_type_node, label);
6248 /* The current function is not necessarily uninlinable.
6249 Computed gotos are incompatible with inlining, but the value
6250 here could be used only in a diagnostic, for example. */
6251 protected_set_expr_location (result, loc);
6254 return result;
6258 /* Given a boolean expression ARG, return a tree representing an increment
6259 or decrement (as indicated by CODE) of ARG. The front end must check for
6260 invalid cases (e.g., decrement in C++). */
6261 tree
6262 boolean_increment (enum tree_code code, tree arg)
6264 tree val;
6265 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6267 arg = stabilize_reference (arg);
6268 switch (code)
6270 case PREINCREMENT_EXPR:
6271 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6272 break;
6273 case POSTINCREMENT_EXPR:
6274 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6275 arg = save_expr (arg);
6276 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6277 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6278 break;
6279 case PREDECREMENT_EXPR:
6280 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6281 invert_truthvalue_loc (input_location, arg));
6282 break;
6283 case POSTDECREMENT_EXPR:
6284 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6285 invert_truthvalue_loc (input_location, arg));
6286 arg = save_expr (arg);
6287 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6288 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6289 break;
6290 default:
6291 gcc_unreachable ();
6293 TREE_SIDE_EFFECTS (val) = 1;
6294 return val;
6297 /* Built-in macros for stddef.h and stdint.h, that require macros
6298 defined in this file. */
6299 void
6300 c_stddef_cpp_builtins(void)
6302 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6303 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6304 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6305 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6306 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6307 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6308 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6309 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6310 if (SIG_ATOMIC_TYPE)
6311 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6312 if (INT8_TYPE)
6313 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6314 if (INT16_TYPE)
6315 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6316 if (INT32_TYPE)
6317 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6318 if (INT64_TYPE)
6319 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6320 if (UINT8_TYPE)
6321 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6322 if (UINT16_TYPE)
6323 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6324 if (UINT32_TYPE)
6325 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6326 if (UINT64_TYPE)
6327 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6328 if (INT_LEAST8_TYPE)
6329 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6330 if (INT_LEAST16_TYPE)
6331 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6332 if (INT_LEAST32_TYPE)
6333 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6334 if (INT_LEAST64_TYPE)
6335 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6336 if (UINT_LEAST8_TYPE)
6337 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6338 if (UINT_LEAST16_TYPE)
6339 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6340 if (UINT_LEAST32_TYPE)
6341 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6342 if (UINT_LEAST64_TYPE)
6343 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6344 if (INT_FAST8_TYPE)
6345 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6346 if (INT_FAST16_TYPE)
6347 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6348 if (INT_FAST32_TYPE)
6349 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6350 if (INT_FAST64_TYPE)
6351 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6352 if (UINT_FAST8_TYPE)
6353 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6354 if (UINT_FAST16_TYPE)
6355 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6356 if (UINT_FAST32_TYPE)
6357 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6358 if (UINT_FAST64_TYPE)
6359 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6360 if (INTPTR_TYPE)
6361 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6362 if (UINTPTR_TYPE)
6363 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6366 static void
6367 c_init_attributes (void)
6369 /* Fill in the built_in_attributes array. */
6370 #define DEF_ATTR_NULL_TREE(ENUM) \
6371 built_in_attributes[(int) ENUM] = NULL_TREE;
6372 #define DEF_ATTR_INT(ENUM, VALUE) \
6373 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6374 #define DEF_ATTR_STRING(ENUM, VALUE) \
6375 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6376 #define DEF_ATTR_IDENT(ENUM, STRING) \
6377 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6378 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6379 built_in_attributes[(int) ENUM] \
6380 = tree_cons (built_in_attributes[(int) PURPOSE], \
6381 built_in_attributes[(int) VALUE], \
6382 built_in_attributes[(int) CHAIN]);
6383 #include "builtin-attrs.def"
6384 #undef DEF_ATTR_NULL_TREE
6385 #undef DEF_ATTR_INT
6386 #undef DEF_ATTR_IDENT
6387 #undef DEF_ATTR_TREE_LIST
6390 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6391 identifier as an argument, so the front end shouldn't look it up. */
6393 bool
6394 attribute_takes_identifier_p (const_tree attr_id)
6396 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6397 if (spec == NULL)
6398 /* Unknown attribute that we'll end up ignoring, return true so we
6399 don't complain about an identifier argument. */
6400 return true;
6401 else if (!strcmp ("mode", spec->name)
6402 || !strcmp ("format", spec->name)
6403 || !strcmp ("cleanup", spec->name))
6404 return true;
6405 else
6406 return targetm.attribute_takes_identifier_p (attr_id);
6409 /* Attribute handlers common to C front ends. */
6411 /* Handle a "packed" attribute; arguments as in
6412 struct attribute_spec.handler. */
6414 static tree
6415 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6416 int flags, bool *no_add_attrs)
6418 if (TYPE_P (*node))
6420 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6421 *node = build_variant_type_copy (*node);
6422 TYPE_PACKED (*node) = 1;
6424 else if (TREE_CODE (*node) == FIELD_DECL)
6426 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6427 /* Still pack bitfields. */
6428 && ! DECL_INITIAL (*node))
6429 warning (OPT_Wattributes,
6430 "%qE attribute ignored for field of type %qT",
6431 name, TREE_TYPE (*node));
6432 else
6433 DECL_PACKED (*node) = 1;
6435 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6436 used for DECL_REGISTER. It wouldn't mean anything anyway.
6437 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6438 that changes what the typedef is typing. */
6439 else
6441 warning (OPT_Wattributes, "%qE attribute ignored", name);
6442 *no_add_attrs = true;
6445 return NULL_TREE;
6448 /* Handle a "nocommon" attribute; arguments as in
6449 struct attribute_spec.handler. */
6451 static tree
6452 handle_nocommon_attribute (tree *node, tree name,
6453 tree ARG_UNUSED (args),
6454 int ARG_UNUSED (flags), bool *no_add_attrs)
6456 if (TREE_CODE (*node) == VAR_DECL)
6457 DECL_COMMON (*node) = 0;
6458 else
6460 warning (OPT_Wattributes, "%qE attribute ignored", name);
6461 *no_add_attrs = true;
6464 return NULL_TREE;
6467 /* Handle a "common" attribute; arguments as in
6468 struct attribute_spec.handler. */
6470 static tree
6471 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6472 int ARG_UNUSED (flags), bool *no_add_attrs)
6474 if (TREE_CODE (*node) == VAR_DECL)
6475 DECL_COMMON (*node) = 1;
6476 else
6478 warning (OPT_Wattributes, "%qE attribute ignored", name);
6479 *no_add_attrs = true;
6482 return NULL_TREE;
6485 /* Handle a "noreturn" attribute; arguments as in
6486 struct attribute_spec.handler. */
6488 static tree
6489 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6490 int ARG_UNUSED (flags), bool *no_add_attrs)
6492 tree type = TREE_TYPE (*node);
6494 /* See FIXME comment in c_common_attribute_table. */
6495 if (TREE_CODE (*node) == FUNCTION_DECL
6496 || objc_method_decl (TREE_CODE (*node)))
6497 TREE_THIS_VOLATILE (*node) = 1;
6498 else if (TREE_CODE (type) == POINTER_TYPE
6499 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6500 TREE_TYPE (*node)
6501 = build_pointer_type
6502 (build_type_variant (TREE_TYPE (type),
6503 TYPE_READONLY (TREE_TYPE (type)), 1));
6504 else
6506 warning (OPT_Wattributes, "%qE attribute ignored", name);
6507 *no_add_attrs = true;
6510 return NULL_TREE;
6513 /* Handle a "hot" and attribute; arguments as in
6514 struct attribute_spec.handler. */
6516 static tree
6517 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6518 int ARG_UNUSED (flags), bool *no_add_attrs)
6520 if (TREE_CODE (*node) == FUNCTION_DECL
6521 || TREE_CODE (*node) == LABEL_DECL)
6523 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6525 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6526 name, "cold");
6527 *no_add_attrs = true;
6529 /* Most of the rest of the hot processing is done later with
6530 lookup_attribute. */
6532 else
6534 warning (OPT_Wattributes, "%qE attribute ignored", name);
6535 *no_add_attrs = true;
6538 return NULL_TREE;
6541 /* Handle a "cold" and attribute; arguments as in
6542 struct attribute_spec.handler. */
6544 static tree
6545 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6546 int ARG_UNUSED (flags), bool *no_add_attrs)
6548 if (TREE_CODE (*node) == FUNCTION_DECL
6549 || TREE_CODE (*node) == LABEL_DECL)
6551 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6553 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6554 name, "hot");
6555 *no_add_attrs = true;
6557 /* Most of the rest of the cold processing is done later with
6558 lookup_attribute. */
6560 else
6562 warning (OPT_Wattributes, "%qE attribute ignored", name);
6563 *no_add_attrs = true;
6566 return NULL_TREE;
6569 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6570 struct attribute_spec.handler. */
6572 static tree
6573 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6574 bool *no_add_attrs)
6576 if (TREE_CODE (*node) != FUNCTION_DECL)
6578 warning (OPT_Wattributes, "%qE attribute ignored", name);
6579 *no_add_attrs = true;
6582 return NULL_TREE;
6585 /* Handle a "noinline" attribute; arguments as in
6586 struct attribute_spec.handler. */
6588 static tree
6589 handle_noinline_attribute (tree *node, tree name,
6590 tree ARG_UNUSED (args),
6591 int ARG_UNUSED (flags), bool *no_add_attrs)
6593 if (TREE_CODE (*node) == FUNCTION_DECL)
6594 DECL_UNINLINABLE (*node) = 1;
6595 else
6597 warning (OPT_Wattributes, "%qE attribute ignored", name);
6598 *no_add_attrs = true;
6601 return NULL_TREE;
6604 /* Handle a "noclone" attribute; arguments as in
6605 struct attribute_spec.handler. */
6607 static tree
6608 handle_noclone_attribute (tree *node, tree name,
6609 tree ARG_UNUSED (args),
6610 int ARG_UNUSED (flags), bool *no_add_attrs)
6612 if (TREE_CODE (*node) != FUNCTION_DECL)
6614 warning (OPT_Wattributes, "%qE attribute ignored", name);
6615 *no_add_attrs = true;
6618 return NULL_TREE;
6621 /* Handle a "always_inline" attribute; arguments as in
6622 struct attribute_spec.handler. */
6624 static tree
6625 handle_always_inline_attribute (tree *node, tree name,
6626 tree ARG_UNUSED (args),
6627 int ARG_UNUSED (flags),
6628 bool *no_add_attrs)
6630 if (TREE_CODE (*node) == FUNCTION_DECL)
6632 /* Set the attribute and mark it for disregarding inline
6633 limits. */
6634 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6636 else
6638 warning (OPT_Wattributes, "%qE attribute ignored", name);
6639 *no_add_attrs = true;
6642 return NULL_TREE;
6645 /* Handle a "gnu_inline" attribute; arguments as in
6646 struct attribute_spec.handler. */
6648 static tree
6649 handle_gnu_inline_attribute (tree *node, tree name,
6650 tree ARG_UNUSED (args),
6651 int ARG_UNUSED (flags),
6652 bool *no_add_attrs)
6654 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6656 /* Do nothing else, just set the attribute. We'll get at
6657 it later with lookup_attribute. */
6659 else
6661 warning (OPT_Wattributes, "%qE attribute ignored", name);
6662 *no_add_attrs = true;
6665 return NULL_TREE;
6668 /* Handle a "leaf" attribute; arguments as in
6669 struct attribute_spec.handler. */
6671 static tree
6672 handle_leaf_attribute (tree *node, tree name,
6673 tree ARG_UNUSED (args),
6674 int ARG_UNUSED (flags), bool *no_add_attrs)
6676 if (TREE_CODE (*node) != FUNCTION_DECL)
6678 warning (OPT_Wattributes, "%qE attribute ignored", name);
6679 *no_add_attrs = true;
6681 if (!TREE_PUBLIC (*node))
6683 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6684 *no_add_attrs = true;
6687 return NULL_TREE;
6690 /* Handle an "artificial" attribute; arguments as in
6691 struct attribute_spec.handler. */
6693 static tree
6694 handle_artificial_attribute (tree *node, tree name,
6695 tree ARG_UNUSED (args),
6696 int ARG_UNUSED (flags),
6697 bool *no_add_attrs)
6699 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6701 /* Do nothing else, just set the attribute. We'll get at
6702 it later with lookup_attribute. */
6704 else
6706 warning (OPT_Wattributes, "%qE attribute ignored", name);
6707 *no_add_attrs = true;
6710 return NULL_TREE;
6713 /* Handle a "flatten" attribute; arguments as in
6714 struct attribute_spec.handler. */
6716 static tree
6717 handle_flatten_attribute (tree *node, tree name,
6718 tree args ATTRIBUTE_UNUSED,
6719 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6721 if (TREE_CODE (*node) == FUNCTION_DECL)
6722 /* Do nothing else, just set the attribute. We'll get at
6723 it later with lookup_attribute. */
6725 else
6727 warning (OPT_Wattributes, "%qE attribute ignored", name);
6728 *no_add_attrs = true;
6731 return NULL_TREE;
6734 /* Handle a "warning" or "error" attribute; arguments as in
6735 struct attribute_spec.handler. */
6737 static tree
6738 handle_error_attribute (tree *node, tree name, tree args,
6739 int ARG_UNUSED (flags), bool *no_add_attrs)
6741 if (TREE_CODE (*node) == FUNCTION_DECL
6742 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6743 /* Do nothing else, just set the attribute. We'll get at
6744 it later with lookup_attribute. */
6746 else
6748 warning (OPT_Wattributes, "%qE attribute ignored", name);
6749 *no_add_attrs = true;
6752 return NULL_TREE;
6755 /* Handle a "used" attribute; arguments as in
6756 struct attribute_spec.handler. */
6758 static tree
6759 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6760 int ARG_UNUSED (flags), bool *no_add_attrs)
6762 tree node = *pnode;
6764 if (TREE_CODE (node) == FUNCTION_DECL
6765 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6766 || (TREE_CODE (node) == TYPE_DECL))
6768 TREE_USED (node) = 1;
6769 DECL_PRESERVE_P (node) = 1;
6770 if (TREE_CODE (node) == VAR_DECL)
6771 DECL_READ_P (node) = 1;
6773 else
6775 warning (OPT_Wattributes, "%qE attribute ignored", name);
6776 *no_add_attrs = true;
6779 return NULL_TREE;
6782 /* Handle a "unused" attribute; arguments as in
6783 struct attribute_spec.handler. */
6785 static tree
6786 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6787 int flags, bool *no_add_attrs)
6789 if (DECL_P (*node))
6791 tree decl = *node;
6793 if (TREE_CODE (decl) == PARM_DECL
6794 || TREE_CODE (decl) == VAR_DECL
6795 || TREE_CODE (decl) == FUNCTION_DECL
6796 || TREE_CODE (decl) == LABEL_DECL
6797 || TREE_CODE (decl) == TYPE_DECL)
6799 TREE_USED (decl) = 1;
6800 if (TREE_CODE (decl) == VAR_DECL
6801 || TREE_CODE (decl) == PARM_DECL)
6802 DECL_READ_P (decl) = 1;
6804 else
6806 warning (OPT_Wattributes, "%qE attribute ignored", name);
6807 *no_add_attrs = true;
6810 else
6812 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6813 *node = build_variant_type_copy (*node);
6814 TREE_USED (*node) = 1;
6817 return NULL_TREE;
6820 /* Handle a "externally_visible" attribute; arguments as in
6821 struct attribute_spec.handler. */
6823 static tree
6824 handle_externally_visible_attribute (tree *pnode, tree name,
6825 tree ARG_UNUSED (args),
6826 int ARG_UNUSED (flags),
6827 bool *no_add_attrs)
6829 tree node = *pnode;
6831 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6833 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6834 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6836 warning (OPT_Wattributes,
6837 "%qE attribute have effect only on public objects", name);
6838 *no_add_attrs = true;
6841 else
6843 warning (OPT_Wattributes, "%qE attribute ignored", name);
6844 *no_add_attrs = true;
6847 return NULL_TREE;
6850 /* Handle a "const" attribute; arguments as in
6851 struct attribute_spec.handler. */
6853 static tree
6854 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6855 int ARG_UNUSED (flags), bool *no_add_attrs)
6857 tree type = TREE_TYPE (*node);
6859 /* See FIXME comment on noreturn in c_common_attribute_table. */
6860 if (TREE_CODE (*node) == FUNCTION_DECL)
6861 TREE_READONLY (*node) = 1;
6862 else if (TREE_CODE (type) == POINTER_TYPE
6863 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6864 TREE_TYPE (*node)
6865 = build_pointer_type
6866 (build_type_variant (TREE_TYPE (type), 1,
6867 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6868 else
6870 warning (OPT_Wattributes, "%qE attribute ignored", name);
6871 *no_add_attrs = true;
6874 return NULL_TREE;
6877 /* Handle a "transparent_union" attribute; arguments as in
6878 struct attribute_spec.handler. */
6880 static tree
6881 handle_transparent_union_attribute (tree *node, tree name,
6882 tree ARG_UNUSED (args), int flags,
6883 bool *no_add_attrs)
6885 tree type;
6887 *no_add_attrs = true;
6890 if (TREE_CODE (*node) == TYPE_DECL
6891 && ! (flags & ATTR_FLAG_CXX11))
6892 node = &TREE_TYPE (*node);
6893 type = *node;
6895 if (TREE_CODE (type) == UNION_TYPE)
6897 /* Make sure that the first field will work for a transparent union.
6898 If the type isn't complete yet, leave the check to the code in
6899 finish_struct. */
6900 if (TYPE_SIZE (type))
6902 tree first = first_field (type);
6903 if (first == NULL_TREE
6904 || DECL_ARTIFICIAL (first)
6905 || TYPE_MODE (type) != DECL_MODE (first))
6906 goto ignored;
6909 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6911 /* If the type isn't complete yet, setting the flag
6912 on a variant wouldn't ever be checked. */
6913 if (!TYPE_SIZE (type))
6914 goto ignored;
6916 /* build_duplicate_type doesn't work for C++. */
6917 if (c_dialect_cxx ())
6918 goto ignored;
6920 /* A type variant isn't good enough, since we don't a cast
6921 to such a type removed as a no-op. */
6922 *node = type = build_duplicate_type (type);
6925 TYPE_TRANSPARENT_AGGR (type) = 1;
6926 return NULL_TREE;
6929 ignored:
6930 warning (OPT_Wattributes, "%qE attribute ignored", name);
6931 return NULL_TREE;
6934 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6935 get the requested priority for a constructor or destructor,
6936 possibly issuing diagnostics for invalid or reserved
6937 priorities. */
6939 static priority_type
6940 get_priority (tree args, bool is_destructor)
6942 HOST_WIDE_INT pri;
6943 tree arg;
6945 if (!args)
6946 return DEFAULT_INIT_PRIORITY;
6948 if (!SUPPORTS_INIT_PRIORITY)
6950 if (is_destructor)
6951 error ("destructor priorities are not supported");
6952 else
6953 error ("constructor priorities are not supported");
6954 return DEFAULT_INIT_PRIORITY;
6957 arg = TREE_VALUE (args);
6958 arg = default_conversion (arg);
6959 if (!host_integerp (arg, /*pos=*/0)
6960 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6961 goto invalid;
6963 pri = tree_low_cst (arg, /*pos=*/0);
6964 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6965 goto invalid;
6967 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6969 if (is_destructor)
6970 warning (0,
6971 "destructor priorities from 0 to %d are reserved "
6972 "for the implementation",
6973 MAX_RESERVED_INIT_PRIORITY);
6974 else
6975 warning (0,
6976 "constructor priorities from 0 to %d are reserved "
6977 "for the implementation",
6978 MAX_RESERVED_INIT_PRIORITY);
6980 return pri;
6982 invalid:
6983 if (is_destructor)
6984 error ("destructor priorities must be integers from 0 to %d inclusive",
6985 MAX_INIT_PRIORITY);
6986 else
6987 error ("constructor priorities must be integers from 0 to %d inclusive",
6988 MAX_INIT_PRIORITY);
6989 return DEFAULT_INIT_PRIORITY;
6992 /* Handle a "constructor" attribute; arguments as in
6993 struct attribute_spec.handler. */
6995 static tree
6996 handle_constructor_attribute (tree *node, tree name, tree args,
6997 int ARG_UNUSED (flags),
6998 bool *no_add_attrs)
7000 tree decl = *node;
7001 tree type = TREE_TYPE (decl);
7003 if (TREE_CODE (decl) == FUNCTION_DECL
7004 && TREE_CODE (type) == FUNCTION_TYPE
7005 && decl_function_context (decl) == 0)
7007 priority_type priority;
7008 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7009 priority = get_priority (args, /*is_destructor=*/false);
7010 SET_DECL_INIT_PRIORITY (decl, priority);
7011 TREE_USED (decl) = 1;
7013 else
7015 warning (OPT_Wattributes, "%qE attribute ignored", name);
7016 *no_add_attrs = true;
7019 return NULL_TREE;
7022 /* Handle a "destructor" attribute; arguments as in
7023 struct attribute_spec.handler. */
7025 static tree
7026 handle_destructor_attribute (tree *node, tree name, tree args,
7027 int ARG_UNUSED (flags),
7028 bool *no_add_attrs)
7030 tree decl = *node;
7031 tree type = TREE_TYPE (decl);
7033 if (TREE_CODE (decl) == FUNCTION_DECL
7034 && TREE_CODE (type) == FUNCTION_TYPE
7035 && decl_function_context (decl) == 0)
7037 priority_type priority;
7038 DECL_STATIC_DESTRUCTOR (decl) = 1;
7039 priority = get_priority (args, /*is_destructor=*/true);
7040 SET_DECL_FINI_PRIORITY (decl, priority);
7041 TREE_USED (decl) = 1;
7043 else
7045 warning (OPT_Wattributes, "%qE attribute ignored", name);
7046 *no_add_attrs = true;
7049 return NULL_TREE;
7052 /* Nonzero if the mode is a valid vector mode for this architecture.
7053 This returns nonzero even if there is no hardware support for the
7054 vector mode, but we can emulate with narrower modes. */
7056 static int
7057 vector_mode_valid_p (enum machine_mode mode)
7059 enum mode_class mclass = GET_MODE_CLASS (mode);
7060 enum machine_mode innermode;
7062 /* Doh! What's going on? */
7063 if (mclass != MODE_VECTOR_INT
7064 && mclass != MODE_VECTOR_FLOAT
7065 && mclass != MODE_VECTOR_FRACT
7066 && mclass != MODE_VECTOR_UFRACT
7067 && mclass != MODE_VECTOR_ACCUM
7068 && mclass != MODE_VECTOR_UACCUM)
7069 return 0;
7071 /* Hardware support. Woo hoo! */
7072 if (targetm.vector_mode_supported_p (mode))
7073 return 1;
7075 innermode = GET_MODE_INNER (mode);
7077 /* We should probably return 1 if requesting V4DI and we have no DI,
7078 but we have V2DI, but this is probably very unlikely. */
7080 /* If we have support for the inner mode, we can safely emulate it.
7081 We may not have V2DI, but me can emulate with a pair of DIs. */
7082 return targetm.scalar_mode_supported_p (innermode);
7086 /* Handle a "mode" attribute; arguments as in
7087 struct attribute_spec.handler. */
7089 static tree
7090 handle_mode_attribute (tree *node, tree name, tree args,
7091 int ARG_UNUSED (flags), bool *no_add_attrs)
7093 tree type = *node;
7094 tree ident = TREE_VALUE (args);
7096 *no_add_attrs = true;
7098 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7099 warning (OPT_Wattributes, "%qE attribute ignored", name);
7100 else
7102 int j;
7103 const char *p = IDENTIFIER_POINTER (ident);
7104 int len = strlen (p);
7105 enum machine_mode mode = VOIDmode;
7106 tree typefm;
7107 bool valid_mode;
7109 if (len > 4 && p[0] == '_' && p[1] == '_'
7110 && p[len - 1] == '_' && p[len - 2] == '_')
7112 char *newp = (char *) alloca (len - 1);
7114 strcpy (newp, &p[2]);
7115 newp[len - 4] = '\0';
7116 p = newp;
7119 /* Change this type to have a type with the specified mode.
7120 First check for the special modes. */
7121 if (!strcmp (p, "byte"))
7122 mode = byte_mode;
7123 else if (!strcmp (p, "word"))
7124 mode = word_mode;
7125 else if (!strcmp (p, "pointer"))
7126 mode = ptr_mode;
7127 else if (!strcmp (p, "libgcc_cmp_return"))
7128 mode = targetm.libgcc_cmp_return_mode ();
7129 else if (!strcmp (p, "libgcc_shift_count"))
7130 mode = targetm.libgcc_shift_count_mode ();
7131 else if (!strcmp (p, "unwind_word"))
7132 mode = targetm.unwind_word_mode ();
7133 else
7134 for (j = 0; j < NUM_MACHINE_MODES; j++)
7135 if (!strcmp (p, GET_MODE_NAME (j)))
7137 mode = (enum machine_mode) j;
7138 break;
7141 if (mode == VOIDmode)
7143 error ("unknown machine mode %qE", ident);
7144 return NULL_TREE;
7147 valid_mode = false;
7148 switch (GET_MODE_CLASS (mode))
7150 case MODE_INT:
7151 case MODE_PARTIAL_INT:
7152 case MODE_FLOAT:
7153 case MODE_DECIMAL_FLOAT:
7154 case MODE_FRACT:
7155 case MODE_UFRACT:
7156 case MODE_ACCUM:
7157 case MODE_UACCUM:
7158 valid_mode = targetm.scalar_mode_supported_p (mode);
7159 break;
7161 case MODE_COMPLEX_INT:
7162 case MODE_COMPLEX_FLOAT:
7163 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7164 break;
7166 case MODE_VECTOR_INT:
7167 case MODE_VECTOR_FLOAT:
7168 case MODE_VECTOR_FRACT:
7169 case MODE_VECTOR_UFRACT:
7170 case MODE_VECTOR_ACCUM:
7171 case MODE_VECTOR_UACCUM:
7172 warning (OPT_Wattributes, "specifying vector types with "
7173 "__attribute__ ((mode)) is deprecated");
7174 warning (OPT_Wattributes,
7175 "use __attribute__ ((vector_size)) instead");
7176 valid_mode = vector_mode_valid_p (mode);
7177 break;
7179 default:
7180 break;
7182 if (!valid_mode)
7184 error ("unable to emulate %qs", p);
7185 return NULL_TREE;
7188 if (POINTER_TYPE_P (type))
7190 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7191 tree (*fn)(tree, enum machine_mode, bool);
7193 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7195 error ("invalid pointer mode %qs", p);
7196 return NULL_TREE;
7199 if (TREE_CODE (type) == POINTER_TYPE)
7200 fn = build_pointer_type_for_mode;
7201 else
7202 fn = build_reference_type_for_mode;
7203 typefm = fn (TREE_TYPE (type), mode, false);
7205 else
7207 /* For fixed-point modes, we need to test if the signness of type
7208 and the machine mode are consistent. */
7209 if (ALL_FIXED_POINT_MODE_P (mode)
7210 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7212 error ("signedness of type and machine mode %qs don%'t match", p);
7213 return NULL_TREE;
7215 /* For fixed-point modes, we need to pass saturating info. */
7216 typefm = lang_hooks.types.type_for_mode (mode,
7217 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7218 : TYPE_UNSIGNED (type));
7221 if (typefm == NULL_TREE)
7223 error ("no data type for mode %qs", p);
7224 return NULL_TREE;
7226 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7228 /* For enumeral types, copy the precision from the integer
7229 type returned above. If not an INTEGER_TYPE, we can't use
7230 this mode for this type. */
7231 if (TREE_CODE (typefm) != INTEGER_TYPE)
7233 error ("cannot use mode %qs for enumeral types", p);
7234 return NULL_TREE;
7237 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7239 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7240 typefm = type;
7242 else
7244 /* We cannot build a type variant, as there's code that assumes
7245 that TYPE_MAIN_VARIANT has the same mode. This includes the
7246 debug generators. Instead, create a subrange type. This
7247 results in all of the enumeral values being emitted only once
7248 in the original, and the subtype gets them by reference. */
7249 if (TYPE_UNSIGNED (type))
7250 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7251 else
7252 typefm = make_signed_type (TYPE_PRECISION (typefm));
7253 TREE_TYPE (typefm) = type;
7256 else if (VECTOR_MODE_P (mode)
7257 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7258 : TREE_CODE (type) != TREE_CODE (typefm))
7260 error ("mode %qs applied to inappropriate type", p);
7261 return NULL_TREE;
7264 *node = typefm;
7267 return NULL_TREE;
7270 /* Handle a "section" attribute; arguments as in
7271 struct attribute_spec.handler. */
7273 static tree
7274 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7275 int ARG_UNUSED (flags), bool *no_add_attrs)
7277 tree decl = *node;
7279 if (targetm_common.have_named_sections)
7281 user_defined_section_attribute = true;
7283 if ((TREE_CODE (decl) == FUNCTION_DECL
7284 || TREE_CODE (decl) == VAR_DECL)
7285 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7287 if (TREE_CODE (decl) == VAR_DECL
7288 && current_function_decl != NULL_TREE
7289 && !TREE_STATIC (decl))
7291 error_at (DECL_SOURCE_LOCATION (decl),
7292 "section attribute cannot be specified for "
7293 "local variables");
7294 *no_add_attrs = true;
7297 /* The decl may have already been given a section attribute
7298 from a previous declaration. Ensure they match. */
7299 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7300 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7301 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7303 error ("section of %q+D conflicts with previous declaration",
7304 *node);
7305 *no_add_attrs = true;
7307 else if (TREE_CODE (decl) == VAR_DECL
7308 && !targetm.have_tls && targetm.emutls.tmpl_section
7309 && DECL_THREAD_LOCAL_P (decl))
7311 error ("section of %q+D cannot be overridden", *node);
7312 *no_add_attrs = true;
7314 else
7315 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7317 else
7319 error ("section attribute not allowed for %q+D", *node);
7320 *no_add_attrs = true;
7323 else
7325 error_at (DECL_SOURCE_LOCATION (*node),
7326 "section attributes are not supported for this target");
7327 *no_add_attrs = true;
7330 return NULL_TREE;
7333 /* Check whether ALIGN is a valid user-specified alignment. If so,
7334 return its base-2 log; if not, output an error and return -1. If
7335 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7336 no error. */
7338 check_user_alignment (const_tree align, bool allow_zero)
7340 int i;
7342 if (TREE_CODE (align) != INTEGER_CST
7343 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7345 error ("requested alignment is not an integer constant");
7346 return -1;
7348 else if (allow_zero && integer_zerop (align))
7349 return -1;
7350 else if ((i = tree_log2 (align)) == -1)
7352 error ("requested alignment is not a power of 2");
7353 return -1;
7355 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7357 error ("requested alignment is too large");
7358 return -1;
7360 return i;
7364 If in c++-11, check if the c++-11 alignment constraint with respect
7365 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7366 c++-11 mode, does nothing.
7368 [dcl.align]2/ says:
7370 [* if the constant expression evaluates to a fundamental alignment,
7371 the alignment requirement of the declared entity shall be the
7372 specified fundamental alignment.
7374 * if the constant expression evaluates to an extended alignment
7375 and the implementation supports that alignment in the context
7376 of the declaration, the alignment of the declared entity shall
7377 be that alignment
7379 * if the constant expression evaluates to an extended alignment
7380 and the implementation does not support that alignment in the
7381 context of the declaration, the program is ill-formed]. */
7383 static bool
7384 check_cxx_fundamental_alignment_constraints (tree node,
7385 unsigned align_log,
7386 int flags)
7388 bool alignment_too_large_p = false;
7389 unsigned requested_alignment = 1U << align_log;
7390 unsigned max_align = 0;
7392 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7393 || (node == NULL_TREE || node == error_mark_node))
7394 return true;
7396 if (cxx_fundamental_alignment_p (requested_alignment))
7397 return true;
7399 if (DECL_P (node))
7401 if (TREE_STATIC (node))
7403 /* For file scope variables and static members, the target
7404 supports alignments that are at most
7405 MAX_OFILE_ALIGNMENT. */
7406 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7407 alignment_too_large_p = true;
7409 else
7411 #ifdef BIGGEST_FIELD_ALIGNMENT
7412 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7413 #else
7414 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7415 #endif
7416 /* For non-static members, the target supports either
7417 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7418 if it is defined or BIGGEST_ALIGNMENT. */
7419 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7420 if (TREE_CODE (node) == FIELD_DECL
7421 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7422 alignment_too_large_p = true;
7423 #undef MAX_TARGET_FIELD_ALIGNMENT
7424 /* For stack variables, the target supports at most
7425 MAX_STACK_ALIGNMENT. */
7426 else if (decl_function_context (node) != NULL
7427 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7428 alignment_too_large_p = true;
7431 else if (TYPE_P (node))
7433 /* Let's be liberal for types. */
7434 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7435 alignment_too_large_p = true;
7438 if (alignment_too_large_p)
7439 pedwarn (input_location, OPT_Wattributes,
7440 "requested alignment %d is larger than %d",
7441 requested_alignment, max_align);
7443 return !alignment_too_large_p;
7446 /* Handle a "aligned" attribute; arguments as in
7447 struct attribute_spec.handler. */
7449 static tree
7450 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7451 int flags, bool *no_add_attrs)
7453 tree decl = NULL_TREE;
7454 tree *type = NULL;
7455 int is_type = 0;
7456 tree align_expr = (args ? TREE_VALUE (args)
7457 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7458 int i;
7460 if (DECL_P (*node))
7462 decl = *node;
7463 type = &TREE_TYPE (decl);
7464 is_type = TREE_CODE (*node) == TYPE_DECL;
7466 else if (TYPE_P (*node))
7467 type = node, is_type = 1;
7469 if ((i = check_user_alignment (align_expr, false)) == -1
7470 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7471 *no_add_attrs = true;
7472 else if (is_type)
7474 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7475 /* OK, modify the type in place. */;
7476 /* If we have a TYPE_DECL, then copy the type, so that we
7477 don't accidentally modify a builtin type. See pushdecl. */
7478 else if (decl && TREE_TYPE (decl) != error_mark_node
7479 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7481 tree tt = TREE_TYPE (decl);
7482 *type = build_variant_type_copy (*type);
7483 DECL_ORIGINAL_TYPE (decl) = tt;
7484 TYPE_NAME (*type) = decl;
7485 TREE_USED (*type) = TREE_USED (decl);
7486 TREE_TYPE (decl) = *type;
7488 else
7489 *type = build_variant_type_copy (*type);
7491 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7492 TYPE_USER_ALIGN (*type) = 1;
7494 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7495 && TREE_CODE (decl) != FIELD_DECL)
7497 error ("alignment may not be specified for %q+D", decl);
7498 *no_add_attrs = true;
7500 else if (DECL_USER_ALIGN (decl)
7501 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7502 /* C++-11 [dcl.align/4]:
7504 When multiple alignment-specifiers are specified for an
7505 entity, the alignment requirement shall be set to the
7506 strictest specified alignment.
7508 This formally comes from the c++11 specification but we are
7509 doing it for the GNU attribute syntax as well. */
7510 *no_add_attrs = true;
7511 else if (TREE_CODE (decl) == FUNCTION_DECL
7512 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7514 if (DECL_USER_ALIGN (decl))
7515 error ("alignment for %q+D was previously specified as %d "
7516 "and may not be decreased", decl,
7517 DECL_ALIGN (decl) / BITS_PER_UNIT);
7518 else
7519 error ("alignment for %q+D must be at least %d", decl,
7520 DECL_ALIGN (decl) / BITS_PER_UNIT);
7521 *no_add_attrs = true;
7523 else
7525 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7526 DECL_USER_ALIGN (decl) = 1;
7529 return NULL_TREE;
7532 /* Handle a "weak" attribute; arguments as in
7533 struct attribute_spec.handler. */
7535 static tree
7536 handle_weak_attribute (tree *node, tree name,
7537 tree ARG_UNUSED (args),
7538 int ARG_UNUSED (flags),
7539 bool * ARG_UNUSED (no_add_attrs))
7541 if (TREE_CODE (*node) == FUNCTION_DECL
7542 && DECL_DECLARED_INLINE_P (*node))
7544 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7545 *no_add_attrs = true;
7547 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7549 error ("indirect function %q+D cannot be declared weak", *node);
7550 *no_add_attrs = true;
7551 return NULL_TREE;
7553 else if (TREE_CODE (*node) == FUNCTION_DECL
7554 || TREE_CODE (*node) == VAR_DECL)
7555 declare_weak (*node);
7556 else
7557 warning (OPT_Wattributes, "%qE attribute ignored", name);
7559 return NULL_TREE;
7562 /* Handle an "alias" or "ifunc" attribute; arguments as in
7563 struct attribute_spec.handler, except that IS_ALIAS tells us
7564 whether this is an alias as opposed to ifunc attribute. */
7566 static tree
7567 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7568 bool *no_add_attrs)
7570 tree decl = *node;
7572 if (TREE_CODE (decl) != FUNCTION_DECL
7573 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7575 warning (OPT_Wattributes, "%qE attribute ignored", name);
7576 *no_add_attrs = true;
7578 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7579 || (TREE_CODE (decl) != FUNCTION_DECL
7580 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7581 /* A static variable declaration is always a tentative definition,
7582 but the alias is a non-tentative definition which overrides. */
7583 || (TREE_CODE (decl) != FUNCTION_DECL
7584 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7586 error ("%q+D defined both normally and as %qE attribute", decl, name);
7587 *no_add_attrs = true;
7588 return NULL_TREE;
7590 else if (!is_alias
7591 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7592 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7594 error ("weak %q+D cannot be defined %qE", decl, name);
7595 *no_add_attrs = true;
7596 return NULL_TREE;
7599 /* Note that the very first time we process a nested declaration,
7600 decl_function_context will not be set. Indeed, *would* never
7601 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7602 we do below. After such frobbery, pushdecl would set the context.
7603 In any case, this is never what we want. */
7604 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7606 tree id;
7608 id = TREE_VALUE (args);
7609 if (TREE_CODE (id) != STRING_CST)
7611 error ("attribute %qE argument not a string", name);
7612 *no_add_attrs = true;
7613 return NULL_TREE;
7615 id = get_identifier (TREE_STRING_POINTER (id));
7616 /* This counts as a use of the object pointed to. */
7617 TREE_USED (id) = 1;
7619 if (TREE_CODE (decl) == FUNCTION_DECL)
7620 DECL_INITIAL (decl) = error_mark_node;
7621 else
7623 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7624 DECL_EXTERNAL (decl) = 1;
7625 else
7626 DECL_EXTERNAL (decl) = 0;
7627 TREE_STATIC (decl) = 1;
7630 if (!is_alias)
7631 /* ifuncs are also aliases, so set that attribute too. */
7632 DECL_ATTRIBUTES (decl)
7633 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7635 else
7637 warning (OPT_Wattributes, "%qE attribute ignored", name);
7638 *no_add_attrs = true;
7641 return NULL_TREE;
7644 /* Handle an "alias" or "ifunc" attribute; arguments as in
7645 struct attribute_spec.handler. */
7647 static tree
7648 handle_ifunc_attribute (tree *node, tree name, tree args,
7649 int ARG_UNUSED (flags), bool *no_add_attrs)
7651 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7654 /* Handle an "alias" or "ifunc" attribute; arguments as in
7655 struct attribute_spec.handler. */
7657 static tree
7658 handle_alias_attribute (tree *node, tree name, tree args,
7659 int ARG_UNUSED (flags), bool *no_add_attrs)
7661 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7664 /* Handle a "weakref" attribute; arguments as in struct
7665 attribute_spec.handler. */
7667 static tree
7668 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7669 int flags, bool *no_add_attrs)
7671 tree attr = NULL_TREE;
7673 /* We must ignore the attribute when it is associated with
7674 local-scoped decls, since attribute alias is ignored and many
7675 such symbols do not even have a DECL_WEAK field. */
7676 if (decl_function_context (*node)
7677 || current_function_decl
7678 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7680 warning (OPT_Wattributes, "%qE attribute ignored", name);
7681 *no_add_attrs = true;
7682 return NULL_TREE;
7685 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7687 error ("indirect function %q+D cannot be declared weakref", *node);
7688 *no_add_attrs = true;
7689 return NULL_TREE;
7692 /* The idea here is that `weakref("name")' mutates into `weakref,
7693 alias("name")', and weakref without arguments, in turn,
7694 implicitly adds weak. */
7696 if (args)
7698 attr = tree_cons (get_identifier ("alias"), args, attr);
7699 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7701 *no_add_attrs = true;
7703 decl_attributes (node, attr, flags);
7705 else
7707 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7708 error_at (DECL_SOURCE_LOCATION (*node),
7709 "weakref attribute must appear before alias attribute");
7711 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7712 and that isn't supported; and because it wants to add it to
7713 the list of weak decls, which isn't helpful. */
7714 DECL_WEAK (*node) = 1;
7717 return NULL_TREE;
7720 /* Handle an "visibility" attribute; arguments as in
7721 struct attribute_spec.handler. */
7723 static tree
7724 handle_visibility_attribute (tree *node, tree name, tree args,
7725 int ARG_UNUSED (flags),
7726 bool *ARG_UNUSED (no_add_attrs))
7728 tree decl = *node;
7729 tree id = TREE_VALUE (args);
7730 enum symbol_visibility vis;
7732 if (TYPE_P (*node))
7734 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7735 /* OK */;
7736 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7738 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7739 name);
7740 return NULL_TREE;
7742 else if (TYPE_FIELDS (*node))
7744 error ("%qE attribute ignored because %qT is already defined",
7745 name, *node);
7746 return NULL_TREE;
7749 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7751 warning (OPT_Wattributes, "%qE attribute ignored", name);
7752 return NULL_TREE;
7755 if (TREE_CODE (id) != STRING_CST)
7757 error ("visibility argument not a string");
7758 return NULL_TREE;
7761 /* If this is a type, set the visibility on the type decl. */
7762 if (TYPE_P (decl))
7764 decl = TYPE_NAME (decl);
7765 if (!decl)
7766 return NULL_TREE;
7767 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7769 warning (OPT_Wattributes, "%qE attribute ignored on types",
7770 name);
7771 return NULL_TREE;
7775 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7776 vis = VISIBILITY_DEFAULT;
7777 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7778 vis = VISIBILITY_INTERNAL;
7779 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7780 vis = VISIBILITY_HIDDEN;
7781 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7782 vis = VISIBILITY_PROTECTED;
7783 else
7785 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7786 vis = VISIBILITY_DEFAULT;
7789 if (DECL_VISIBILITY_SPECIFIED (decl)
7790 && vis != DECL_VISIBILITY (decl))
7792 tree attributes = (TYPE_P (*node)
7793 ? TYPE_ATTRIBUTES (*node)
7794 : DECL_ATTRIBUTES (decl));
7795 if (lookup_attribute ("visibility", attributes))
7796 error ("%qD redeclared with different visibility", decl);
7797 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7798 && lookup_attribute ("dllimport", attributes))
7799 error ("%qD was declared %qs which implies default visibility",
7800 decl, "dllimport");
7801 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7802 && lookup_attribute ("dllexport", attributes))
7803 error ("%qD was declared %qs which implies default visibility",
7804 decl, "dllexport");
7807 DECL_VISIBILITY (decl) = vis;
7808 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7810 /* Go ahead and attach the attribute to the node as well. This is needed
7811 so we can determine whether we have VISIBILITY_DEFAULT because the
7812 visibility was not specified, or because it was explicitly overridden
7813 from the containing scope. */
7815 return NULL_TREE;
7818 /* Determine the ELF symbol visibility for DECL, which is either a
7819 variable or a function. It is an error to use this function if a
7820 definition of DECL is not available in this translation unit.
7821 Returns true if the final visibility has been determined by this
7822 function; false if the caller is free to make additional
7823 modifications. */
7825 bool
7826 c_determine_visibility (tree decl)
7828 gcc_assert (TREE_CODE (decl) == VAR_DECL
7829 || TREE_CODE (decl) == FUNCTION_DECL);
7831 /* If the user explicitly specified the visibility with an
7832 attribute, honor that. DECL_VISIBILITY will have been set during
7833 the processing of the attribute. We check for an explicit
7834 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7835 to distinguish the use of an attribute from the use of a "#pragma
7836 GCC visibility push(...)"; in the latter case we still want other
7837 considerations to be able to overrule the #pragma. */
7838 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7839 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7840 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7841 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7842 return true;
7844 /* Set default visibility to whatever the user supplied with
7845 visibility_specified depending on #pragma GCC visibility. */
7846 if (!DECL_VISIBILITY_SPECIFIED (decl))
7848 if (visibility_options.inpragma
7849 || DECL_VISIBILITY (decl) != default_visibility)
7851 DECL_VISIBILITY (decl) = default_visibility;
7852 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7853 /* If visibility changed and DECL already has DECL_RTL, ensure
7854 symbol flags are updated. */
7855 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7856 || TREE_CODE (decl) == FUNCTION_DECL)
7857 && DECL_RTL_SET_P (decl))
7858 make_decl_rtl (decl);
7861 return false;
7864 /* Handle an "tls_model" attribute; arguments as in
7865 struct attribute_spec.handler. */
7867 static tree
7868 handle_tls_model_attribute (tree *node, tree name, tree args,
7869 int ARG_UNUSED (flags), bool *no_add_attrs)
7871 tree id;
7872 tree decl = *node;
7873 enum tls_model kind;
7875 *no_add_attrs = true;
7877 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7879 warning (OPT_Wattributes, "%qE attribute ignored", name);
7880 return NULL_TREE;
7883 kind = DECL_TLS_MODEL (decl);
7884 id = TREE_VALUE (args);
7885 if (TREE_CODE (id) != STRING_CST)
7887 error ("tls_model argument not a string");
7888 return NULL_TREE;
7891 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7892 kind = TLS_MODEL_LOCAL_EXEC;
7893 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7894 kind = TLS_MODEL_INITIAL_EXEC;
7895 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7896 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7897 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7898 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7899 else
7900 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7902 DECL_TLS_MODEL (decl) = kind;
7903 return NULL_TREE;
7906 /* Handle a "no_instrument_function" attribute; arguments as in
7907 struct attribute_spec.handler. */
7909 static tree
7910 handle_no_instrument_function_attribute (tree *node, tree name,
7911 tree ARG_UNUSED (args),
7912 int ARG_UNUSED (flags),
7913 bool *no_add_attrs)
7915 tree decl = *node;
7917 if (TREE_CODE (decl) != FUNCTION_DECL)
7919 error_at (DECL_SOURCE_LOCATION (decl),
7920 "%qE attribute applies only to functions", name);
7921 *no_add_attrs = true;
7923 else if (DECL_INITIAL (decl))
7925 error_at (DECL_SOURCE_LOCATION (decl),
7926 "can%'t set %qE attribute after definition", name);
7927 *no_add_attrs = true;
7929 else
7930 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7932 return NULL_TREE;
7935 /* Handle a "malloc" attribute; arguments as in
7936 struct attribute_spec.handler. */
7938 static tree
7939 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7940 int ARG_UNUSED (flags), bool *no_add_attrs)
7942 if (TREE_CODE (*node) == FUNCTION_DECL
7943 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7944 DECL_IS_MALLOC (*node) = 1;
7945 else
7947 warning (OPT_Wattributes, "%qE attribute ignored", name);
7948 *no_add_attrs = true;
7951 return NULL_TREE;
7954 /* Handle a "alloc_size" attribute; arguments as in
7955 struct attribute_spec.handler. */
7957 static tree
7958 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7959 int ARG_UNUSED (flags), bool *no_add_attrs)
7961 unsigned arg_count = type_num_arguments (*node);
7962 for (; args; args = TREE_CHAIN (args))
7964 tree position = TREE_VALUE (args);
7966 if (TREE_CODE (position) != INTEGER_CST
7967 || TREE_INT_CST_HIGH (position)
7968 || TREE_INT_CST_LOW (position) < 1
7969 || TREE_INT_CST_LOW (position) > arg_count )
7971 warning (OPT_Wattributes,
7972 "alloc_size parameter outside range");
7973 *no_add_attrs = true;
7974 return NULL_TREE;
7977 return NULL_TREE;
7980 /* Handle a "fn spec" attribute; arguments as in
7981 struct attribute_spec.handler. */
7983 static tree
7984 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7985 tree args, int ARG_UNUSED (flags),
7986 bool *no_add_attrs ATTRIBUTE_UNUSED)
7988 gcc_assert (args
7989 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7990 && !TREE_CHAIN (args));
7991 return NULL_TREE;
7994 /* Handle a "returns_twice" attribute; arguments as in
7995 struct attribute_spec.handler. */
7997 static tree
7998 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7999 int ARG_UNUSED (flags), bool *no_add_attrs)
8001 if (TREE_CODE (*node) == FUNCTION_DECL)
8002 DECL_IS_RETURNS_TWICE (*node) = 1;
8003 else
8005 warning (OPT_Wattributes, "%qE attribute ignored", name);
8006 *no_add_attrs = true;
8009 return NULL_TREE;
8012 /* Handle a "no_limit_stack" attribute; arguments as in
8013 struct attribute_spec.handler. */
8015 static tree
8016 handle_no_limit_stack_attribute (tree *node, tree name,
8017 tree ARG_UNUSED (args),
8018 int ARG_UNUSED (flags),
8019 bool *no_add_attrs)
8021 tree decl = *node;
8023 if (TREE_CODE (decl) != FUNCTION_DECL)
8025 error_at (DECL_SOURCE_LOCATION (decl),
8026 "%qE attribute applies only to functions", name);
8027 *no_add_attrs = true;
8029 else if (DECL_INITIAL (decl))
8031 error_at (DECL_SOURCE_LOCATION (decl),
8032 "can%'t set %qE attribute after definition", name);
8033 *no_add_attrs = true;
8035 else
8036 DECL_NO_LIMIT_STACK (decl) = 1;
8038 return NULL_TREE;
8041 /* Handle a "pure" attribute; arguments as in
8042 struct attribute_spec.handler. */
8044 static tree
8045 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8046 int ARG_UNUSED (flags), bool *no_add_attrs)
8048 if (TREE_CODE (*node) == FUNCTION_DECL)
8049 DECL_PURE_P (*node) = 1;
8050 /* ??? TODO: Support types. */
8051 else
8053 warning (OPT_Wattributes, "%qE attribute ignored", name);
8054 *no_add_attrs = true;
8057 return NULL_TREE;
8060 /* Digest an attribute list destined for a transactional memory statement.
8061 ALLOWED is the set of attributes that are allowed for this statement;
8062 return the attribute we parsed. Multiple attributes are never allowed. */
8065 parse_tm_stmt_attr (tree attrs, int allowed)
8067 tree a_seen = NULL;
8068 int m_seen = 0;
8070 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8072 tree a = TREE_PURPOSE (attrs);
8073 int m = 0;
8075 if (is_attribute_p ("outer", a))
8076 m = TM_STMT_ATTR_OUTER;
8078 if ((m & allowed) == 0)
8080 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8081 continue;
8084 if (m_seen == 0)
8086 a_seen = a;
8087 m_seen = m;
8089 else if (m_seen == m)
8090 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8091 else
8092 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8095 return m_seen;
8098 /* Transform a TM attribute name into a maskable integer and back.
8099 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8100 to how the lack of an attribute is treated. */
8103 tm_attr_to_mask (tree attr)
8105 if (attr == NULL)
8106 return 0;
8107 if (is_attribute_p ("transaction_safe", attr))
8108 return TM_ATTR_SAFE;
8109 if (is_attribute_p ("transaction_callable", attr))
8110 return TM_ATTR_CALLABLE;
8111 if (is_attribute_p ("transaction_pure", attr))
8112 return TM_ATTR_PURE;
8113 if (is_attribute_p ("transaction_unsafe", attr))
8114 return TM_ATTR_IRREVOCABLE;
8115 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8116 return TM_ATTR_MAY_CANCEL_OUTER;
8117 return 0;
8120 tree
8121 tm_mask_to_attr (int mask)
8123 const char *str;
8124 switch (mask)
8126 case TM_ATTR_SAFE:
8127 str = "transaction_safe";
8128 break;
8129 case TM_ATTR_CALLABLE:
8130 str = "transaction_callable";
8131 break;
8132 case TM_ATTR_PURE:
8133 str = "transaction_pure";
8134 break;
8135 case TM_ATTR_IRREVOCABLE:
8136 str = "transaction_unsafe";
8137 break;
8138 case TM_ATTR_MAY_CANCEL_OUTER:
8139 str = "transaction_may_cancel_outer";
8140 break;
8141 default:
8142 gcc_unreachable ();
8144 return get_identifier (str);
8147 /* Return the first TM attribute seen in LIST. */
8149 tree
8150 find_tm_attribute (tree list)
8152 for (; list ; list = TREE_CHAIN (list))
8154 tree name = TREE_PURPOSE (list);
8155 if (tm_attr_to_mask (name) != 0)
8156 return name;
8158 return NULL_TREE;
8161 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8162 Here we accept only function types, and verify that none of the other
8163 function TM attributes are also applied. */
8164 /* ??? We need to accept class types for C++, but not C. This greatly
8165 complicates this function, since we can no longer rely on the extra
8166 processing given by function_type_required. */
8168 static tree
8169 handle_tm_attribute (tree *node, tree name, tree args,
8170 int flags, bool *no_add_attrs)
8172 /* Only one path adds the attribute; others don't. */
8173 *no_add_attrs = true;
8175 switch (TREE_CODE (*node))
8177 case RECORD_TYPE:
8178 case UNION_TYPE:
8179 /* Only tm_callable and tm_safe apply to classes. */
8180 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8181 goto ignored;
8182 /* FALLTHRU */
8184 case FUNCTION_TYPE:
8185 case METHOD_TYPE:
8187 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8188 if (old_name == name)
8190 else if (old_name != NULL_TREE)
8191 error ("type was previously declared %qE", old_name);
8192 else
8193 *no_add_attrs = false;
8195 break;
8197 case POINTER_TYPE:
8199 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8200 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8202 tree fn_tmp = TREE_TYPE (*node);
8203 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8204 *node = build_pointer_type (fn_tmp);
8205 break;
8208 /* FALLTHRU */
8210 default:
8211 /* If a function is next, pass it on to be tried next. */
8212 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8213 return tree_cons (name, args, NULL);
8215 ignored:
8216 warning (OPT_Wattributes, "%qE attribute ignored", name);
8217 break;
8220 return NULL_TREE;
8223 /* Handle the TM_WRAP attribute; arguments as in
8224 struct attribute_spec.handler. */
8226 static tree
8227 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8228 int ARG_UNUSED (flags), bool *no_add_attrs)
8230 tree decl = *node;
8232 /* We don't need the attribute even on success, since we
8233 record the entry in an external table. */
8234 *no_add_attrs = true;
8236 if (TREE_CODE (decl) != FUNCTION_DECL)
8237 warning (OPT_Wattributes, "%qE attribute ignored", name);
8238 else
8240 tree wrap_decl = TREE_VALUE (args);
8241 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8242 && TREE_CODE (wrap_decl) != VAR_DECL
8243 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8244 error ("%qE argument not an identifier", name);
8245 else
8247 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8248 wrap_decl = lookup_name (wrap_decl);
8249 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8251 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8252 TREE_TYPE (wrap_decl)))
8253 record_tm_replacement (wrap_decl, decl);
8254 else
8255 error ("%qD is not compatible with %qD", wrap_decl, decl);
8257 else
8258 error ("transaction_wrap argument is not a function");
8262 return NULL_TREE;
8265 /* Ignore the given attribute. Used when this attribute may be usefully
8266 overridden by the target, but is not used generically. */
8268 static tree
8269 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8270 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8271 bool *no_add_attrs)
8273 *no_add_attrs = true;
8274 return NULL_TREE;
8277 /* Handle a "no vops" attribute; arguments as in
8278 struct attribute_spec.handler. */
8280 static tree
8281 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8282 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8283 bool *ARG_UNUSED (no_add_attrs))
8285 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8286 DECL_IS_NOVOPS (*node) = 1;
8287 return NULL_TREE;
8290 /* Handle a "deprecated" attribute; arguments as in
8291 struct attribute_spec.handler. */
8293 static tree
8294 handle_deprecated_attribute (tree *node, tree name,
8295 tree args, int flags,
8296 bool *no_add_attrs)
8298 tree type = NULL_TREE;
8299 int warn = 0;
8300 tree what = NULL_TREE;
8302 if (!args)
8303 *no_add_attrs = true;
8304 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8306 error ("deprecated message is not a string");
8307 *no_add_attrs = true;
8310 if (DECL_P (*node))
8312 tree decl = *node;
8313 type = TREE_TYPE (decl);
8315 if (TREE_CODE (decl) == TYPE_DECL
8316 || TREE_CODE (decl) == PARM_DECL
8317 || TREE_CODE (decl) == VAR_DECL
8318 || TREE_CODE (decl) == FUNCTION_DECL
8319 || TREE_CODE (decl) == FIELD_DECL
8320 || objc_method_decl (TREE_CODE (decl)))
8321 TREE_DEPRECATED (decl) = 1;
8322 else
8323 warn = 1;
8325 else if (TYPE_P (*node))
8327 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8328 *node = build_variant_type_copy (*node);
8329 TREE_DEPRECATED (*node) = 1;
8330 type = *node;
8332 else
8333 warn = 1;
8335 if (warn)
8337 *no_add_attrs = true;
8338 if (type && TYPE_NAME (type))
8340 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8341 what = TYPE_NAME (*node);
8342 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8343 && DECL_NAME (TYPE_NAME (type)))
8344 what = DECL_NAME (TYPE_NAME (type));
8346 if (what)
8347 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8348 else
8349 warning (OPT_Wattributes, "%qE attribute ignored", name);
8352 return NULL_TREE;
8355 /* Handle a "vector_size" attribute; arguments as in
8356 struct attribute_spec.handler. */
8358 static tree
8359 handle_vector_size_attribute (tree *node, tree name, tree args,
8360 int ARG_UNUSED (flags),
8361 bool *no_add_attrs)
8363 unsigned HOST_WIDE_INT vecsize, nunits;
8364 enum machine_mode orig_mode;
8365 tree type = *node, new_type, size;
8367 *no_add_attrs = true;
8369 size = TREE_VALUE (args);
8371 if (!host_integerp (size, 1))
8373 warning (OPT_Wattributes, "%qE attribute ignored", name);
8374 return NULL_TREE;
8377 /* Get the vector size (in bytes). */
8378 vecsize = tree_low_cst (size, 1);
8380 /* We need to provide for vector pointers, vector arrays, and
8381 functions returning vectors. For example:
8383 __attribute__((vector_size(16))) short *foo;
8385 In this case, the mode is SI, but the type being modified is
8386 HI, so we need to look further. */
8388 while (POINTER_TYPE_P (type)
8389 || TREE_CODE (type) == FUNCTION_TYPE
8390 || TREE_CODE (type) == METHOD_TYPE
8391 || TREE_CODE (type) == ARRAY_TYPE
8392 || TREE_CODE (type) == OFFSET_TYPE)
8393 type = TREE_TYPE (type);
8395 /* Get the mode of the type being modified. */
8396 orig_mode = TYPE_MODE (type);
8398 if ((!INTEGRAL_TYPE_P (type)
8399 && !SCALAR_FLOAT_TYPE_P (type)
8400 && !FIXED_POINT_TYPE_P (type))
8401 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8402 && GET_MODE_CLASS (orig_mode) != MODE_INT
8403 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8404 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8405 || TREE_CODE (type) == BOOLEAN_TYPE)
8407 error ("invalid vector type for attribute %qE", name);
8408 return NULL_TREE;
8411 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8413 error ("vector size not an integral multiple of component size");
8414 return NULL;
8417 if (vecsize == 0)
8419 error ("zero vector size");
8420 return NULL;
8423 /* Calculate how many units fit in the vector. */
8424 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8425 if (nunits & (nunits - 1))
8427 error ("number of components of the vector not a power of two");
8428 return NULL_TREE;
8431 new_type = build_vector_type (type, nunits);
8433 /* Build back pointers if needed. */
8434 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8436 return NULL_TREE;
8439 /* Handle the "nonnull" attribute. */
8440 static tree
8441 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8442 tree args, int ARG_UNUSED (flags),
8443 bool *no_add_attrs)
8445 tree type = *node;
8446 unsigned HOST_WIDE_INT attr_arg_num;
8448 /* If no arguments are specified, all pointer arguments should be
8449 non-null. Verify a full prototype is given so that the arguments
8450 will have the correct types when we actually check them later. */
8451 if (!args)
8453 if (!prototype_p (type))
8455 error ("nonnull attribute without arguments on a non-prototype");
8456 *no_add_attrs = true;
8458 return NULL_TREE;
8461 /* Argument list specified. Verify that each argument number references
8462 a pointer argument. */
8463 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8465 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8467 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8469 error ("nonnull argument has invalid operand number (argument %lu)",
8470 (unsigned long) attr_arg_num);
8471 *no_add_attrs = true;
8472 return NULL_TREE;
8475 if (prototype_p (type))
8477 function_args_iterator iter;
8478 tree argument;
8480 function_args_iter_init (&iter, type);
8481 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8483 argument = function_args_iter_cond (&iter);
8484 if (argument == NULL_TREE || ck_num == arg_num)
8485 break;
8488 if (!argument
8489 || TREE_CODE (argument) == VOID_TYPE)
8491 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8492 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8493 *no_add_attrs = true;
8494 return NULL_TREE;
8497 if (TREE_CODE (argument) != POINTER_TYPE)
8499 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8500 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8501 *no_add_attrs = true;
8502 return NULL_TREE;
8507 return NULL_TREE;
8510 /* Check the argument list of a function call for null in argument slots
8511 that are marked as requiring a non-null pointer argument. The NARGS
8512 arguments are passed in the array ARGARRAY.
8515 static void
8516 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8518 tree a;
8519 int i;
8521 attrs = lookup_attribute ("nonnull", attrs);
8522 if (attrs == NULL_TREE)
8523 return;
8525 a = attrs;
8526 /* See if any of the nonnull attributes has no arguments. If so,
8527 then every pointer argument is checked (in which case the check
8528 for pointer type is done in check_nonnull_arg). */
8529 if (TREE_VALUE (a) != NULL_TREE)
8531 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8532 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8534 if (a != NULL_TREE)
8535 for (i = 0; i < nargs; i++)
8536 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8537 i + 1);
8538 else
8540 /* Walk the argument list. If we encounter an argument number we
8541 should check for non-null, do it. */
8542 for (i = 0; i < nargs; i++)
8544 for (a = attrs; ; a = TREE_CHAIN (a))
8546 a = lookup_attribute ("nonnull", a);
8547 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8548 break;
8551 if (a != NULL_TREE)
8552 check_function_arguments_recurse (check_nonnull_arg, NULL,
8553 argarray[i], i + 1);
8558 /* Check that the Nth argument of a function call (counting backwards
8559 from the end) is a (pointer)0. The NARGS arguments are passed in the
8560 array ARGARRAY. */
8562 static void
8563 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8565 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8567 if (attr)
8569 int len = 0;
8570 int pos = 0;
8571 tree sentinel;
8572 function_args_iterator iter;
8573 tree t;
8575 /* Skip over the named arguments. */
8576 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8578 if (len == nargs)
8579 break;
8580 len++;
8583 if (TREE_VALUE (attr))
8585 tree p = TREE_VALUE (TREE_VALUE (attr));
8586 pos = TREE_INT_CST_LOW (p);
8589 /* The sentinel must be one of the varargs, i.e.
8590 in position >= the number of fixed arguments. */
8591 if ((nargs - 1 - pos) < len)
8593 warning (OPT_Wformat_,
8594 "not enough variable arguments to fit a sentinel");
8595 return;
8598 /* Validate the sentinel. */
8599 sentinel = argarray[nargs - 1 - pos];
8600 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8601 || !integer_zerop (sentinel))
8602 /* Although __null (in C++) is only an integer we allow it
8603 nevertheless, as we are guaranteed that it's exactly
8604 as wide as a pointer, and we don't want to force
8605 users to cast the NULL they have written there.
8606 We warn with -Wstrict-null-sentinel, though. */
8607 && (warn_strict_null_sentinel || null_node != sentinel))
8608 warning (OPT_Wformat_, "missing sentinel in function call");
8612 /* Helper for check_function_nonnull; given a list of operands which
8613 must be non-null in ARGS, determine if operand PARAM_NUM should be
8614 checked. */
8616 static bool
8617 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8619 unsigned HOST_WIDE_INT arg_num = 0;
8621 for (; args; args = TREE_CHAIN (args))
8623 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8625 gcc_assert (found);
8627 if (arg_num == param_num)
8628 return true;
8630 return false;
8633 /* Check that the function argument PARAM (which is operand number
8634 PARAM_NUM) is non-null. This is called by check_function_nonnull
8635 via check_function_arguments_recurse. */
8637 static void
8638 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8639 unsigned HOST_WIDE_INT param_num)
8641 /* Just skip checking the argument if it's not a pointer. This can
8642 happen if the "nonnull" attribute was given without an operand
8643 list (which means to check every pointer argument). */
8645 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8646 return;
8648 if (integer_zerop (param))
8649 warning (OPT_Wnonnull, "null argument where non-null required "
8650 "(argument %lu)", (unsigned long) param_num);
8653 /* Helper for nonnull attribute handling; fetch the operand number
8654 from the attribute argument list. */
8656 static bool
8657 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8659 /* Verify the arg number is a constant. */
8660 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8661 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8662 return false;
8664 *valp = TREE_INT_CST_LOW (arg_num_expr);
8665 return true;
8668 /* Handle a "nothrow" attribute; arguments as in
8669 struct attribute_spec.handler. */
8671 static tree
8672 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8673 int ARG_UNUSED (flags), bool *no_add_attrs)
8675 if (TREE_CODE (*node) == FUNCTION_DECL)
8676 TREE_NOTHROW (*node) = 1;
8677 /* ??? TODO: Support types. */
8678 else
8680 warning (OPT_Wattributes, "%qE attribute ignored", name);
8681 *no_add_attrs = true;
8684 return NULL_TREE;
8687 /* Handle a "cleanup" attribute; arguments as in
8688 struct attribute_spec.handler. */
8690 static tree
8691 handle_cleanup_attribute (tree *node, tree name, tree args,
8692 int ARG_UNUSED (flags), bool *no_add_attrs)
8694 tree decl = *node;
8695 tree cleanup_id, cleanup_decl;
8697 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8698 for global destructors in C++. This requires infrastructure that
8699 we don't have generically at the moment. It's also not a feature
8700 we'd be missing too much, since we do have attribute constructor. */
8701 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8703 warning (OPT_Wattributes, "%qE attribute ignored", name);
8704 *no_add_attrs = true;
8705 return NULL_TREE;
8708 /* Verify that the argument is a function in scope. */
8709 /* ??? We could support pointers to functions here as well, if
8710 that was considered desirable. */
8711 cleanup_id = TREE_VALUE (args);
8712 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8714 error ("cleanup argument not an identifier");
8715 *no_add_attrs = true;
8716 return NULL_TREE;
8718 cleanup_decl = lookup_name (cleanup_id);
8719 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8721 error ("cleanup argument not a function");
8722 *no_add_attrs = true;
8723 return NULL_TREE;
8726 /* That the function has proper type is checked with the
8727 eventual call to build_function_call. */
8729 return NULL_TREE;
8732 /* Handle a "warn_unused_result" attribute. No special handling. */
8734 static tree
8735 handle_warn_unused_result_attribute (tree *node, tree name,
8736 tree ARG_UNUSED (args),
8737 int ARG_UNUSED (flags), bool *no_add_attrs)
8739 /* Ignore the attribute for functions not returning any value. */
8740 if (VOID_TYPE_P (TREE_TYPE (*node)))
8742 warning (OPT_Wattributes, "%qE attribute ignored", name);
8743 *no_add_attrs = true;
8746 return NULL_TREE;
8749 /* Handle a "sentinel" attribute. */
8751 static tree
8752 handle_sentinel_attribute (tree *node, tree name, tree args,
8753 int ARG_UNUSED (flags), bool *no_add_attrs)
8755 if (!prototype_p (*node))
8757 warning (OPT_Wattributes,
8758 "%qE attribute requires prototypes with named arguments", name);
8759 *no_add_attrs = true;
8761 else
8763 if (!stdarg_p (*node))
8765 warning (OPT_Wattributes,
8766 "%qE attribute only applies to variadic functions", name);
8767 *no_add_attrs = true;
8771 if (args)
8773 tree position = TREE_VALUE (args);
8775 if (TREE_CODE (position) != INTEGER_CST)
8777 warning (OPT_Wattributes,
8778 "requested position is not an integer constant");
8779 *no_add_attrs = true;
8781 else
8783 if (tree_int_cst_lt (position, integer_zero_node))
8785 warning (OPT_Wattributes,
8786 "requested position is less than zero");
8787 *no_add_attrs = true;
8792 return NULL_TREE;
8795 /* Handle a "type_generic" attribute. */
8797 static tree
8798 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8799 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8800 bool * ARG_UNUSED (no_add_attrs))
8802 /* Ensure we have a function type. */
8803 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8805 /* Ensure we have a variadic function. */
8806 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8808 return NULL_TREE;
8811 /* Handle a "target" attribute. */
8813 static tree
8814 handle_target_attribute (tree *node, tree name, tree args, int flags,
8815 bool *no_add_attrs)
8817 /* Ensure we have a function type. */
8818 if (TREE_CODE (*node) != FUNCTION_DECL)
8820 warning (OPT_Wattributes, "%qE attribute ignored", name);
8821 *no_add_attrs = true;
8823 /* Do not strip invalid target attributes for targets which support function
8824 multiversioning as the target string is used to determine versioned
8825 functions. */
8826 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8827 flags)
8828 && ! targetm.target_option.supports_function_versions ())
8829 *no_add_attrs = true;
8831 return NULL_TREE;
8834 /* Arguments being collected for optimization. */
8835 typedef const char *const_char_p; /* For DEF_VEC_P. */
8836 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8839 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8840 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8841 false for #pragma GCC optimize. */
8843 bool
8844 parse_optimize_options (tree args, bool attr_p)
8846 bool ret = true;
8847 unsigned opt_argc;
8848 unsigned i;
8849 int saved_flag_strict_aliasing;
8850 const char **opt_argv;
8851 struct cl_decoded_option *decoded_options;
8852 unsigned int decoded_options_count;
8853 tree ap;
8855 /* Build up argv vector. Just in case the string is stored away, use garbage
8856 collected strings. */
8857 vec_safe_truncate (optimize_args, 0);
8858 vec_safe_push (optimize_args, (const char *) NULL);
8860 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8862 tree value = TREE_VALUE (ap);
8864 if (TREE_CODE (value) == INTEGER_CST)
8866 char buffer[20];
8867 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8868 vec_safe_push (optimize_args, ggc_strdup (buffer));
8871 else if (TREE_CODE (value) == STRING_CST)
8873 /* Split string into multiple substrings. */
8874 size_t len = TREE_STRING_LENGTH (value);
8875 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8876 char *end = p + len;
8877 char *comma;
8878 char *next_p = p;
8880 while (next_p != NULL)
8882 size_t len2;
8883 char *q, *r;
8885 p = next_p;
8886 comma = strchr (p, ',');
8887 if (comma)
8889 len2 = comma - p;
8890 *comma = '\0';
8891 next_p = comma+1;
8893 else
8895 len2 = end - p;
8896 next_p = NULL;
8899 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8901 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8902 options. */
8903 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8905 ret = false;
8906 if (attr_p)
8907 warning (OPT_Wattributes,
8908 "bad option %s to optimize attribute", p);
8909 else
8910 warning (OPT_Wpragmas,
8911 "bad option %s to pragma attribute", p);
8912 continue;
8915 if (*p != '-')
8917 *r++ = '-';
8919 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8920 itself is -Os, and any other switch begins with a -f. */
8921 if ((*p >= '0' && *p <= '9')
8922 || (p[0] == 's' && p[1] == '\0'))
8923 *r++ = 'O';
8924 else if (*p != 'O')
8925 *r++ = 'f';
8928 memcpy (r, p, len2);
8929 r[len2] = '\0';
8930 vec_safe_push (optimize_args, (const char *) q);
8936 opt_argc = optimize_args->length ();
8937 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8939 for (i = 1; i < opt_argc; i++)
8940 opt_argv[i] = (*optimize_args)[i];
8942 saved_flag_strict_aliasing = flag_strict_aliasing;
8944 /* Now parse the options. */
8945 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8946 &decoded_options,
8947 &decoded_options_count);
8948 decode_options (&global_options, &global_options_set,
8949 decoded_options, decoded_options_count,
8950 input_location, global_dc);
8952 targetm.override_options_after_change();
8954 /* Don't allow changing -fstrict-aliasing. */
8955 flag_strict_aliasing = saved_flag_strict_aliasing;
8957 optimize_args->truncate (0);
8958 return ret;
8961 /* For handling "optimize" attribute. arguments as in
8962 struct attribute_spec.handler. */
8964 static tree
8965 handle_optimize_attribute (tree *node, tree name, tree args,
8966 int ARG_UNUSED (flags), bool *no_add_attrs)
8968 /* Ensure we have a function type. */
8969 if (TREE_CODE (*node) != FUNCTION_DECL)
8971 warning (OPT_Wattributes, "%qE attribute ignored", name);
8972 *no_add_attrs = true;
8974 else
8976 struct cl_optimization cur_opts;
8977 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8979 /* Save current options. */
8980 cl_optimization_save (&cur_opts, &global_options);
8982 /* If we previously had some optimization options, use them as the
8983 default. */
8984 if (old_opts)
8985 cl_optimization_restore (&global_options,
8986 TREE_OPTIMIZATION (old_opts));
8988 /* Parse options, and update the vector. */
8989 parse_optimize_options (args, true);
8990 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8991 = build_optimization_node ();
8993 /* Restore current options. */
8994 cl_optimization_restore (&global_options, &cur_opts);
8997 return NULL_TREE;
9000 /* Handle a "no_split_stack" attribute. */
9002 static tree
9003 handle_no_split_stack_attribute (tree *node, tree name,
9004 tree ARG_UNUSED (args),
9005 int ARG_UNUSED (flags),
9006 bool *no_add_attrs)
9008 tree decl = *node;
9010 if (TREE_CODE (decl) != FUNCTION_DECL)
9012 error_at (DECL_SOURCE_LOCATION (decl),
9013 "%qE attribute applies only to functions", name);
9014 *no_add_attrs = true;
9016 else if (DECL_INITIAL (decl))
9018 error_at (DECL_SOURCE_LOCATION (decl),
9019 "can%'t set %qE attribute after definition", name);
9020 *no_add_attrs = true;
9023 return NULL_TREE;
9026 /* Check for valid arguments being passed to a function with FNTYPE.
9027 There are NARGS arguments in the array ARGARRAY. */
9028 void
9029 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9031 /* Check for null being passed in a pointer argument that must be
9032 non-null. We also need to do this if format checking is enabled. */
9034 if (warn_nonnull)
9035 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9037 /* Check for errors in format strings. */
9039 if (warn_format || warn_suggest_attribute_format)
9040 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9042 if (warn_format)
9043 check_function_sentinel (fntype, nargs, argarray);
9046 /* Generic argument checking recursion routine. PARAM is the argument to
9047 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9048 once the argument is resolved. CTX is context for the callback. */
9049 void
9050 check_function_arguments_recurse (void (*callback)
9051 (void *, tree, unsigned HOST_WIDE_INT),
9052 void *ctx, tree param,
9053 unsigned HOST_WIDE_INT param_num)
9055 if (CONVERT_EXPR_P (param)
9056 && (TYPE_PRECISION (TREE_TYPE (param))
9057 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9059 /* Strip coercion. */
9060 check_function_arguments_recurse (callback, ctx,
9061 TREE_OPERAND (param, 0), param_num);
9062 return;
9065 if (TREE_CODE (param) == CALL_EXPR)
9067 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9068 tree attrs;
9069 bool found_format_arg = false;
9071 /* See if this is a call to a known internationalization function
9072 that modifies a format arg. Such a function may have multiple
9073 format_arg attributes (for example, ngettext). */
9075 for (attrs = TYPE_ATTRIBUTES (type);
9076 attrs;
9077 attrs = TREE_CHAIN (attrs))
9078 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9080 tree inner_arg;
9081 tree format_num_expr;
9082 int format_num;
9083 int i;
9084 call_expr_arg_iterator iter;
9086 /* Extract the argument number, which was previously checked
9087 to be valid. */
9088 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9090 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9091 && !TREE_INT_CST_HIGH (format_num_expr));
9093 format_num = TREE_INT_CST_LOW (format_num_expr);
9095 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9096 inner_arg != 0;
9097 inner_arg = next_call_expr_arg (&iter), i++)
9098 if (i == format_num)
9100 check_function_arguments_recurse (callback, ctx,
9101 inner_arg, param_num);
9102 found_format_arg = true;
9103 break;
9107 /* If we found a format_arg attribute and did a recursive check,
9108 we are done with checking this argument. Otherwise, we continue
9109 and this will be considered a non-literal. */
9110 if (found_format_arg)
9111 return;
9114 if (TREE_CODE (param) == COND_EXPR)
9116 /* Check both halves of the conditional expression. */
9117 check_function_arguments_recurse (callback, ctx,
9118 TREE_OPERAND (param, 1), param_num);
9119 check_function_arguments_recurse (callback, ctx,
9120 TREE_OPERAND (param, 2), param_num);
9121 return;
9124 (*callback) (ctx, param, param_num);
9127 /* Checks for a builtin function FNDECL that the number of arguments
9128 NARGS against the required number REQUIRED and issues an error if
9129 there is a mismatch. Returns true if the number of arguments is
9130 correct, otherwise false. */
9132 static bool
9133 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9135 if (nargs < required)
9137 error_at (input_location,
9138 "not enough arguments to function %qE", fndecl);
9139 return false;
9141 else if (nargs > required)
9143 error_at (input_location,
9144 "too many arguments to function %qE", fndecl);
9145 return false;
9147 return true;
9150 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9151 Returns false if there was an error, otherwise true. */
9153 bool
9154 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9156 if (!DECL_BUILT_IN (fndecl)
9157 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9158 return true;
9160 switch (DECL_FUNCTION_CODE (fndecl))
9162 case BUILT_IN_CONSTANT_P:
9163 return builtin_function_validate_nargs (fndecl, nargs, 1);
9165 case BUILT_IN_ISFINITE:
9166 case BUILT_IN_ISINF:
9167 case BUILT_IN_ISINF_SIGN:
9168 case BUILT_IN_ISNAN:
9169 case BUILT_IN_ISNORMAL:
9170 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9172 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9174 error ("non-floating-point argument in call to "
9175 "function %qE", fndecl);
9176 return false;
9178 return true;
9180 return false;
9182 case BUILT_IN_ISGREATER:
9183 case BUILT_IN_ISGREATEREQUAL:
9184 case BUILT_IN_ISLESS:
9185 case BUILT_IN_ISLESSEQUAL:
9186 case BUILT_IN_ISLESSGREATER:
9187 case BUILT_IN_ISUNORDERED:
9188 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9190 enum tree_code code0, code1;
9191 code0 = TREE_CODE (TREE_TYPE (args[0]));
9192 code1 = TREE_CODE (TREE_TYPE (args[1]));
9193 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9194 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9195 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9197 error ("non-floating-point arguments in call to "
9198 "function %qE", fndecl);
9199 return false;
9201 return true;
9203 return false;
9205 case BUILT_IN_FPCLASSIFY:
9206 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9208 unsigned i;
9210 for (i=0; i<5; i++)
9211 if (TREE_CODE (args[i]) != INTEGER_CST)
9213 error ("non-const integer argument %u in call to function %qE",
9214 i+1, fndecl);
9215 return false;
9218 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9220 error ("non-floating-point argument in call to function %qE",
9221 fndecl);
9222 return false;
9224 return true;
9226 return false;
9228 case BUILT_IN_ASSUME_ALIGNED:
9229 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9231 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9233 error ("non-integer argument 3 in call to function %qE", fndecl);
9234 return false;
9236 return true;
9238 return false;
9240 default:
9241 return true;
9245 /* Function to help qsort sort FIELD_DECLs by name order. */
9248 field_decl_cmp (const void *x_p, const void *y_p)
9250 const tree *const x = (const tree *const) x_p;
9251 const tree *const y = (const tree *const) y_p;
9253 if (DECL_NAME (*x) == DECL_NAME (*y))
9254 /* A nontype is "greater" than a type. */
9255 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9256 if (DECL_NAME (*x) == NULL_TREE)
9257 return -1;
9258 if (DECL_NAME (*y) == NULL_TREE)
9259 return 1;
9260 if (DECL_NAME (*x) < DECL_NAME (*y))
9261 return -1;
9262 return 1;
9265 static struct {
9266 gt_pointer_operator new_value;
9267 void *cookie;
9268 } resort_data;
9270 /* This routine compares two fields like field_decl_cmp but using the
9271 pointer operator in resort_data. */
9273 static int
9274 resort_field_decl_cmp (const void *x_p, const void *y_p)
9276 const tree *const x = (const tree *const) x_p;
9277 const tree *const y = (const tree *const) y_p;
9279 if (DECL_NAME (*x) == DECL_NAME (*y))
9280 /* A nontype is "greater" than a type. */
9281 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9282 if (DECL_NAME (*x) == NULL_TREE)
9283 return -1;
9284 if (DECL_NAME (*y) == NULL_TREE)
9285 return 1;
9287 tree d1 = DECL_NAME (*x);
9288 tree d2 = DECL_NAME (*y);
9289 resort_data.new_value (&d1, resort_data.cookie);
9290 resort_data.new_value (&d2, resort_data.cookie);
9291 if (d1 < d2)
9292 return -1;
9294 return 1;
9297 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9299 void
9300 resort_sorted_fields (void *obj,
9301 void * ARG_UNUSED (orig_obj),
9302 gt_pointer_operator new_value,
9303 void *cookie)
9305 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9306 resort_data.new_value = new_value;
9307 resort_data.cookie = cookie;
9308 qsort (&sf->elts[0], sf->len, sizeof (tree),
9309 resort_field_decl_cmp);
9312 /* Subroutine of c_parse_error.
9313 Return the result of concatenating LHS and RHS. RHS is really
9314 a string literal, its first character is indicated by RHS_START and
9315 RHS_SIZE is its length (including the terminating NUL character).
9317 The caller is responsible for deleting the returned pointer. */
9319 static char *
9320 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9322 const int lhs_size = strlen (lhs);
9323 char *result = XNEWVEC (char, lhs_size + rhs_size);
9324 strncpy (result, lhs, lhs_size);
9325 strncpy (result + lhs_size, rhs_start, rhs_size);
9326 return result;
9329 /* Issue the error given by GMSGID, indicating that it occurred before
9330 TOKEN, which had the associated VALUE. */
9332 void
9333 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9334 tree value, unsigned char token_flags)
9336 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9338 char *message = NULL;
9340 if (token_type == CPP_EOF)
9341 message = catenate_messages (gmsgid, " at end of input");
9342 else if (token_type == CPP_CHAR
9343 || token_type == CPP_WCHAR
9344 || token_type == CPP_CHAR16
9345 || token_type == CPP_CHAR32)
9347 unsigned int val = TREE_INT_CST_LOW (value);
9348 const char *prefix;
9350 switch (token_type)
9352 default:
9353 prefix = "";
9354 break;
9355 case CPP_WCHAR:
9356 prefix = "L";
9357 break;
9358 case CPP_CHAR16:
9359 prefix = "u";
9360 break;
9361 case CPP_CHAR32:
9362 prefix = "U";
9363 break;
9366 if (val <= UCHAR_MAX && ISGRAPH (val))
9367 message = catenate_messages (gmsgid, " before %s'%c'");
9368 else
9369 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9371 error (message, prefix, val);
9372 free (message);
9373 message = NULL;
9375 else if (token_type == CPP_STRING
9376 || token_type == CPP_WSTRING
9377 || token_type == CPP_STRING16
9378 || token_type == CPP_STRING32
9379 || token_type == CPP_UTF8STRING)
9380 message = catenate_messages (gmsgid, " before string constant");
9381 else if (token_type == CPP_NUMBER)
9382 message = catenate_messages (gmsgid, " before numeric constant");
9383 else if (token_type == CPP_NAME)
9385 message = catenate_messages (gmsgid, " before %qE");
9386 error (message, value);
9387 free (message);
9388 message = NULL;
9390 else if (token_type == CPP_PRAGMA)
9391 message = catenate_messages (gmsgid, " before %<#pragma%>");
9392 else if (token_type == CPP_PRAGMA_EOL)
9393 message = catenate_messages (gmsgid, " before end of line");
9394 else if (token_type == CPP_DECLTYPE)
9395 message = catenate_messages (gmsgid, " before %<decltype%>");
9396 else if (token_type < N_TTYPES)
9398 message = catenate_messages (gmsgid, " before %qs token");
9399 error (message, cpp_type2name (token_type, token_flags));
9400 free (message);
9401 message = NULL;
9403 else
9404 error (gmsgid);
9406 if (message)
9408 error (message);
9409 free (message);
9411 #undef catenate_messages
9414 /* Mapping for cpp message reasons to the options that enable them. */
9416 struct reason_option_codes_t
9418 const int reason; /* cpplib message reason. */
9419 const int option_code; /* gcc option that controls this message. */
9422 static const struct reason_option_codes_t option_codes[] = {
9423 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9424 {CPP_W_COMMENTS, OPT_Wcomment},
9425 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9426 {CPP_W_MULTICHAR, OPT_Wmultichar},
9427 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9428 {CPP_W_LONG_LONG, OPT_Wlong_long},
9429 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9430 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9431 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9432 {CPP_W_UNDEF, OPT_Wundef},
9433 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9434 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9435 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9436 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9437 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9438 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9439 {CPP_W_NONE, 0}
9442 /* Return the gcc option code associated with the reason for a cpp
9443 message, or 0 if none. */
9445 static int
9446 c_option_controlling_cpp_error (int reason)
9448 const struct reason_option_codes_t *entry;
9450 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9452 if (entry->reason == reason)
9453 return entry->option_code;
9455 return 0;
9458 /* Callback from cpp_error for PFILE to print diagnostics from the
9459 preprocessor. The diagnostic is of type LEVEL, with REASON set
9460 to the reason code if LEVEL is represents a warning, at location
9461 LOCATION unless this is after lexing and the compiler's location
9462 should be used instead, with column number possibly overridden by
9463 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9464 the arguments. Returns true if a diagnostic was emitted, false
9465 otherwise. */
9467 bool
9468 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9469 location_t location, unsigned int column_override,
9470 const char *msg, va_list *ap)
9472 diagnostic_info diagnostic;
9473 diagnostic_t dlevel;
9474 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9475 bool ret;
9477 switch (level)
9479 case CPP_DL_WARNING_SYSHDR:
9480 if (flag_no_output)
9481 return false;
9482 global_dc->dc_warn_system_headers = 1;
9483 /* Fall through. */
9484 case CPP_DL_WARNING:
9485 if (flag_no_output)
9486 return false;
9487 dlevel = DK_WARNING;
9488 break;
9489 case CPP_DL_PEDWARN:
9490 if (flag_no_output && !flag_pedantic_errors)
9491 return false;
9492 dlevel = DK_PEDWARN;
9493 break;
9494 case CPP_DL_ERROR:
9495 dlevel = DK_ERROR;
9496 break;
9497 case CPP_DL_ICE:
9498 dlevel = DK_ICE;
9499 break;
9500 case CPP_DL_NOTE:
9501 dlevel = DK_NOTE;
9502 break;
9503 case CPP_DL_FATAL:
9504 dlevel = DK_FATAL;
9505 break;
9506 default:
9507 gcc_unreachable ();
9509 if (done_lexing)
9510 location = input_location;
9511 diagnostic_set_info_translated (&diagnostic, msg, ap,
9512 location, dlevel);
9513 if (column_override)
9514 diagnostic_override_column (&diagnostic, column_override);
9515 diagnostic_override_option_index (&diagnostic,
9516 c_option_controlling_cpp_error (reason));
9517 ret = report_diagnostic (&diagnostic);
9518 if (level == CPP_DL_WARNING_SYSHDR)
9519 global_dc->dc_warn_system_headers = save_warn_system_headers;
9520 return ret;
9523 /* Convert a character from the host to the target execution character
9524 set. cpplib handles this, mostly. */
9526 HOST_WIDE_INT
9527 c_common_to_target_charset (HOST_WIDE_INT c)
9529 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9530 zero-extended under -fno-signed-char. cpplib insists that characters
9531 and character constants are always unsigned. Hence we must convert
9532 back and forth. */
9533 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9535 uc = cpp_host_to_exec_charset (parse_in, uc);
9537 if (flag_signed_char)
9538 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9539 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9540 else
9541 return uc;
9544 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9545 references with an INDIRECT_REF of a constant at the bottom; much like the
9546 traditional rendering of offsetof as a macro. Return the folded result. */
9548 tree
9549 fold_offsetof_1 (tree expr)
9551 tree base, off, t;
9553 switch (TREE_CODE (expr))
9555 case ERROR_MARK:
9556 return expr;
9558 case VAR_DECL:
9559 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9560 return error_mark_node;
9562 case CALL_EXPR:
9563 case TARGET_EXPR:
9564 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9565 return error_mark_node;
9567 case NOP_EXPR:
9568 case INDIRECT_REF:
9569 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9571 error ("cannot apply %<offsetof%> to a non constant address");
9572 return error_mark_node;
9574 return TREE_OPERAND (expr, 0);
9576 case COMPONENT_REF:
9577 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9578 if (base == error_mark_node)
9579 return base;
9581 t = TREE_OPERAND (expr, 1);
9582 if (DECL_C_BIT_FIELD (t))
9584 error ("attempt to take address of bit-field structure "
9585 "member %qD", t);
9586 return error_mark_node;
9588 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9589 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9591 / BITS_PER_UNIT));
9592 break;
9594 case ARRAY_REF:
9595 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9596 if (base == error_mark_node)
9597 return base;
9599 t = TREE_OPERAND (expr, 1);
9601 /* Check if the offset goes beyond the upper bound of the array. */
9602 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9604 tree upbound = array_ref_up_bound (expr);
9605 if (upbound != NULL_TREE
9606 && TREE_CODE (upbound) == INTEGER_CST
9607 && !tree_int_cst_equal (upbound,
9608 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9610 upbound = size_binop (PLUS_EXPR, upbound,
9611 build_int_cst (TREE_TYPE (upbound), 1));
9612 if (tree_int_cst_lt (upbound, t))
9614 tree v;
9616 for (v = TREE_OPERAND (expr, 0);
9617 TREE_CODE (v) == COMPONENT_REF;
9618 v = TREE_OPERAND (v, 0))
9619 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9620 == RECORD_TYPE)
9622 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9623 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9624 if (TREE_CODE (fld_chain) == FIELD_DECL)
9625 break;
9627 if (fld_chain)
9628 break;
9630 /* Don't warn if the array might be considered a poor
9631 man's flexible array member with a very permissive
9632 definition thereof. */
9633 if (TREE_CODE (v) == ARRAY_REF
9634 || TREE_CODE (v) == COMPONENT_REF)
9635 warning (OPT_Warray_bounds,
9636 "index %E denotes an offset "
9637 "greater than size of %qT",
9638 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9643 t = convert (sizetype, t);
9644 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9645 break;
9647 case COMPOUND_EXPR:
9648 /* Handle static members of volatile structs. */
9649 t = TREE_OPERAND (expr, 1);
9650 gcc_assert (TREE_CODE (t) == VAR_DECL);
9651 return fold_offsetof_1 (t);
9653 default:
9654 gcc_unreachable ();
9657 return fold_build_pointer_plus (base, off);
9660 /* Likewise, but convert it to the return type of offsetof. */
9662 tree
9663 fold_offsetof (tree expr)
9665 return convert (size_type_node, fold_offsetof_1 (expr));
9668 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9669 expression, because B will always be true. */
9671 void
9672 warn_for_omitted_condop (location_t location, tree cond)
9674 if (truth_value_p (TREE_CODE (cond)))
9675 warning_at (location, OPT_Wparentheses,
9676 "the omitted middle operand in ?: will always be %<true%>, "
9677 "suggest explicit middle operand");
9680 /* Give an error for storing into ARG, which is 'const'. USE indicates
9681 how ARG was being used. */
9683 void
9684 readonly_error (tree arg, enum lvalue_use use)
9686 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9687 || use == lv_asm);
9688 /* Using this macro rather than (for example) arrays of messages
9689 ensures that all the format strings are checked at compile
9690 time. */
9691 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9692 : (use == lv_increment ? (I) \
9693 : (use == lv_decrement ? (D) : (AS))))
9694 if (TREE_CODE (arg) == COMPONENT_REF)
9696 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9697 error (READONLY_MSG (G_("assignment of member "
9698 "%qD in read-only object"),
9699 G_("increment of member "
9700 "%qD in read-only object"),
9701 G_("decrement of member "
9702 "%qD in read-only object"),
9703 G_("member %qD in read-only object "
9704 "used as %<asm%> output")),
9705 TREE_OPERAND (arg, 1));
9706 else
9707 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9708 G_("increment of read-only member %qD"),
9709 G_("decrement of read-only member %qD"),
9710 G_("read-only member %qD used as %<asm%> output")),
9711 TREE_OPERAND (arg, 1));
9713 else if (TREE_CODE (arg) == VAR_DECL)
9714 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9715 G_("increment of read-only variable %qD"),
9716 G_("decrement of read-only variable %qD"),
9717 G_("read-only variable %qD used as %<asm%> output")),
9718 arg);
9719 else if (TREE_CODE (arg) == PARM_DECL)
9720 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9721 G_("increment of read-only parameter %qD"),
9722 G_("decrement of read-only parameter %qD"),
9723 G_("read-only parameter %qD use as %<asm%> output")),
9724 arg);
9725 else if (TREE_CODE (arg) == RESULT_DECL)
9727 gcc_assert (c_dialect_cxx ());
9728 error (READONLY_MSG (G_("assignment of "
9729 "read-only named return value %qD"),
9730 G_("increment of "
9731 "read-only named return value %qD"),
9732 G_("decrement of "
9733 "read-only named return value %qD"),
9734 G_("read-only named return value %qD "
9735 "used as %<asm%>output")),
9736 arg);
9738 else if (TREE_CODE (arg) == FUNCTION_DECL)
9739 error (READONLY_MSG (G_("assignment of function %qD"),
9740 G_("increment of function %qD"),
9741 G_("decrement of function %qD"),
9742 G_("function %qD used as %<asm%> output")),
9743 arg);
9744 else
9745 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9746 G_("increment of read-only location %qE"),
9747 G_("decrement of read-only location %qE"),
9748 G_("read-only location %qE used as %<asm%> output")),
9749 arg);
9752 /* Print an error message for an invalid lvalue. USE says
9753 how the lvalue is being used and so selects the error message. LOC
9754 is the location for the error. */
9756 void
9757 lvalue_error (location_t loc, enum lvalue_use use)
9759 switch (use)
9761 case lv_assign:
9762 error_at (loc, "lvalue required as left operand of assignment");
9763 break;
9764 case lv_increment:
9765 error_at (loc, "lvalue required as increment operand");
9766 break;
9767 case lv_decrement:
9768 error_at (loc, "lvalue required as decrement operand");
9769 break;
9770 case lv_addressof:
9771 error_at (loc, "lvalue required as unary %<&%> operand");
9772 break;
9773 case lv_asm:
9774 error_at (loc, "lvalue required in asm statement");
9775 break;
9776 default:
9777 gcc_unreachable ();
9781 /* Print an error message for an invalid indirection of type TYPE.
9782 ERRSTRING is the name of the operator for the indirection. */
9784 void
9785 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9787 switch (errstring)
9789 case RO_NULL:
9790 gcc_assert (c_dialect_cxx ());
9791 error_at (loc, "invalid type argument (have %qT)", type);
9792 break;
9793 case RO_ARRAY_INDEXING:
9794 error_at (loc,
9795 "invalid type argument of array indexing (have %qT)",
9796 type);
9797 break;
9798 case RO_UNARY_STAR:
9799 error_at (loc,
9800 "invalid type argument of unary %<*%> (have %qT)",
9801 type);
9802 break;
9803 case RO_ARROW:
9804 error_at (loc,
9805 "invalid type argument of %<->%> (have %qT)",
9806 type);
9807 break;
9808 case RO_IMPLICIT_CONVERSION:
9809 error_at (loc,
9810 "invalid type argument of implicit conversion (have %qT)",
9811 type);
9812 break;
9813 default:
9814 gcc_unreachable ();
9818 /* *PTYPE is an incomplete array. Complete it with a domain based on
9819 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9820 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9821 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9824 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9826 tree maxindex, type, main_type, elt, unqual_elt;
9827 int failure = 0, quals;
9828 hashval_t hashcode = 0;
9830 maxindex = size_zero_node;
9831 if (initial_value)
9833 if (TREE_CODE (initial_value) == STRING_CST)
9835 int eltsize
9836 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9837 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9839 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9841 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9843 if (vec_safe_is_empty (v))
9845 if (pedantic)
9846 failure = 3;
9847 maxindex = ssize_int (-1);
9849 else
9851 tree curindex;
9852 unsigned HOST_WIDE_INT cnt;
9853 constructor_elt *ce;
9854 bool fold_p = false;
9856 if ((*v)[0].index)
9857 maxindex = fold_convert_loc (input_location, sizetype,
9858 (*v)[0].index);
9859 curindex = maxindex;
9861 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
9863 bool curfold_p = false;
9864 if (ce->index)
9865 curindex = ce->index, curfold_p = true;
9866 else
9868 if (fold_p)
9869 curindex = fold_convert (sizetype, curindex);
9870 curindex = size_binop (PLUS_EXPR, curindex,
9871 size_one_node);
9873 if (tree_int_cst_lt (maxindex, curindex))
9874 maxindex = curindex, fold_p = curfold_p;
9876 if (fold_p)
9877 maxindex = fold_convert (sizetype, maxindex);
9880 else
9882 /* Make an error message unless that happened already. */
9883 if (initial_value != error_mark_node)
9884 failure = 1;
9887 else
9889 failure = 2;
9890 if (!do_default)
9891 return failure;
9894 type = *ptype;
9895 /* Force an indefinite layout factor. */
9896 if (upc_shared_type_p (type))
9897 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
9898 elt = TREE_TYPE (type);
9899 quals = TYPE_QUALS (strip_array_types (elt));
9900 if (quals == 0)
9901 unqual_elt = elt;
9902 else
9903 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9905 /* Using build_distinct_type_copy and modifying things afterward instead
9906 of using build_array_type to create a new type preserves all of the
9907 TYPE_LANG_FLAG_? bits that the front end may have set. */
9908 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9909 TREE_TYPE (main_type) = unqual_elt;
9910 TYPE_DOMAIN (main_type)
9911 = build_range_type (TREE_TYPE (maxindex),
9912 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9913 layout_type (main_type);
9915 /* Make sure we have the canonical MAIN_TYPE. */
9916 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9917 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9918 hashcode);
9919 main_type = type_hash_canon (hashcode, main_type);
9921 /* Fix the canonical type. */
9922 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9923 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9924 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9925 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9926 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9927 != TYPE_DOMAIN (main_type)))
9928 TYPE_CANONICAL (main_type)
9929 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9930 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9931 else
9932 TYPE_CANONICAL (main_type) = main_type;
9934 if (quals == 0)
9935 type = main_type;
9936 else
9937 type = c_build_qualified_type (main_type, quals);
9939 if (COMPLETE_TYPE_P (type)
9940 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9941 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9943 error ("size of array is too large");
9944 /* If we proceed with the array type as it is, we'll eventually
9945 crash in tree_low_cst(). */
9946 type = error_mark_node;
9949 *ptype = type;
9950 return failure;
9953 /* Like c_mark_addressable but don't check register qualifier. */
9954 void
9955 c_common_mark_addressable_vec (tree t)
9957 while (handled_component_p (t))
9958 t = TREE_OPERAND (t, 0);
9959 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9960 return;
9961 TREE_ADDRESSABLE (t) = 1;
9966 /* Used to help initialize the builtin-types.def table. When a type of
9967 the correct size doesn't exist, use error_mark_node instead of NULL.
9968 The later results in segfaults even when a decl using the type doesn't
9969 get invoked. */
9971 tree
9972 builtin_type_for_size (int size, bool unsignedp)
9974 tree type = c_common_type_for_size (size, unsignedp);
9975 return type ? type : error_mark_node;
9978 /* A helper function for resolve_overloaded_builtin in resolving the
9979 overloaded __sync_ builtins. Returns a positive power of 2 if the
9980 first operand of PARAMS is a pointer to a supported data type.
9981 Returns 0 if an error is encountered. */
9983 static int
9984 sync_resolve_size (tree function, vec<tree, va_gc> *params)
9986 tree type;
9987 int size;
9989 if (!params)
9991 error ("too few arguments to function %qE", function);
9992 return 0;
9995 type = TREE_TYPE ((*params)[0]);
9996 if (TREE_CODE (type) != POINTER_TYPE)
9997 goto incompatible;
9999 type = TREE_TYPE (type);
10000 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10001 goto incompatible;
10003 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
10004 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10005 return size;
10007 incompatible:
10008 error ("incompatible type for argument %d of %qE", 1, function);
10009 return 0;
10012 /* A helper function for resolve_overloaded_builtin. Adds casts to
10013 PARAMS to make arguments match up with those of FUNCTION. Drops
10014 the variadic arguments at the end. Returns false if some error
10015 was encountered; true on success. */
10017 static bool
10018 sync_resolve_params (location_t loc, tree orig_function, tree function,
10019 vec<tree, va_gc> *params, bool orig_format)
10021 function_args_iterator iter;
10022 tree ptype;
10023 unsigned int parmnum;
10025 function_args_iter_init (&iter, TREE_TYPE (function));
10026 /* We've declared the implementation functions to use "volatile void *"
10027 as the pointer parameter, so we shouldn't get any complaints from the
10028 call to check_function_arguments what ever type the user used. */
10029 function_args_iter_next (&iter);
10030 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10032 /* For the rest of the values, we need to cast these to FTYPE, so that we
10033 don't get warnings for passing pointer types, etc. */
10034 parmnum = 0;
10035 while (1)
10037 tree val, arg_type;
10039 arg_type = function_args_iter_cond (&iter);
10040 /* XXX void_type_node belies the abstraction. */
10041 if (arg_type == void_type_node)
10042 break;
10044 ++parmnum;
10045 if (params->length () <= parmnum)
10047 error_at (loc, "too few arguments to function %qE", orig_function);
10048 return false;
10051 /* Only convert parameters if arg_type is unsigned integer type with
10052 new format sync routines, i.e. don't attempt to convert pointer
10053 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10054 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10055 kinds). */
10056 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10058 /* Ideally for the first conversion we'd use convert_for_assignment
10059 so that we get warnings for anything that doesn't match the pointer
10060 type. This isn't portable across the C and C++ front ends atm. */
10061 val = (*params)[parmnum];
10062 val = convert (ptype, val);
10063 val = convert (arg_type, val);
10064 (*params)[parmnum] = val;
10067 function_args_iter_next (&iter);
10070 /* __atomic routines are not variadic. */
10071 if (!orig_format && params->length () != parmnum + 1)
10073 error_at (loc, "too many arguments to function %qE", orig_function);
10074 return false;
10077 /* The definition of these primitives is variadic, with the remaining
10078 being "an optional list of variables protected by the memory barrier".
10079 No clue what that's supposed to mean, precisely, but we consider all
10080 call-clobbered variables to be protected so we're safe. */
10081 params->truncate (parmnum + 1);
10083 return true;
10086 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10087 RESULT to make it match the type of the first pointer argument in
10088 PARAMS. */
10090 static tree
10091 sync_resolve_return (tree first_param, tree result, bool orig_format)
10093 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10094 tree rtype = TREE_TYPE (result);
10095 ptype = TYPE_MAIN_VARIANT (ptype);
10097 /* New format doesn't require casting unless the types are the same size. */
10098 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10099 return convert (ptype, result);
10100 else
10101 return result;
10104 /* This function verifies the PARAMS to generic atomic FUNCTION.
10105 It returns the size if all the parameters are the same size, otherwise
10106 0 is returned if the parameters are invalid. */
10108 static int
10109 get_atomic_generic_size (location_t loc, tree function,
10110 vec<tree, va_gc> *params)
10112 unsigned int n_param;
10113 unsigned int n_model;
10114 unsigned int x;
10115 int size_0;
10116 tree type_0;
10118 /* Determine the parameter makeup. */
10119 switch (DECL_FUNCTION_CODE (function))
10121 case BUILT_IN_ATOMIC_EXCHANGE:
10122 n_param = 4;
10123 n_model = 1;
10124 break;
10125 case BUILT_IN_ATOMIC_LOAD:
10126 case BUILT_IN_ATOMIC_STORE:
10127 n_param = 3;
10128 n_model = 1;
10129 break;
10130 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10131 n_param = 6;
10132 n_model = 2;
10133 break;
10134 default:
10135 gcc_unreachable ();
10138 if (vec_safe_length (params) != n_param)
10140 error_at (loc, "incorrect number of arguments to function %qE", function);
10141 return 0;
10144 /* Get type of first parameter, and determine its size. */
10145 type_0 = TREE_TYPE ((*params)[0]);
10146 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10148 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10149 function);
10150 return 0;
10153 /* Types must be compile time constant sizes. */
10154 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10156 error_at (loc,
10157 "argument 1 of %qE must be a pointer to a constant size type",
10158 function);
10159 return 0;
10162 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10164 /* Zero size objects are not allowed. */
10165 if (size_0 == 0)
10167 error_at (loc,
10168 "argument 1 of %qE must be a pointer to a nonzero size object",
10169 function);
10170 return 0;
10173 /* Check each other parameter is a pointer and the same size. */
10174 for (x = 0; x < n_param - n_model; x++)
10176 int size;
10177 tree type = TREE_TYPE ((*params)[x]);
10178 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
10179 if (n_param == 6 && x == 3)
10180 continue;
10181 if (!POINTER_TYPE_P (type))
10183 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10184 function);
10185 return 0;
10187 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10188 if (size != size_0)
10190 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10191 function);
10192 return 0;
10196 /* Check memory model parameters for validity. */
10197 for (x = n_param - n_model ; x < n_param; x++)
10199 tree p = (*params)[x];
10200 if (TREE_CODE (p) == INTEGER_CST)
10202 int i = tree_low_cst (p, 1);
10203 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10205 warning_at (loc, OPT_Winvalid_memory_model,
10206 "invalid memory model argument %d of %qE", x + 1,
10207 function);
10210 else
10211 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10213 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10214 function);
10215 return 0;
10219 return size_0;
10223 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10224 at the beginning of the parameter list PARAMS representing the size of the
10225 objects. This is to match the library ABI requirement. LOC is the location
10226 of the function call.
10227 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10228 returned to allow the external call to be constructed. */
10230 static tree
10231 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10232 vec<tree, va_gc> *params)
10234 tree size_node;
10236 /* Insert a SIZE_T parameter as the first param. If there isn't
10237 enough space, allocate a new vector and recursively re-build with that. */
10238 if (!params->space (1))
10240 unsigned int z, len;
10241 vec<tree, va_gc> *v;
10242 tree f;
10244 len = params->length ();
10245 vec_alloc (v, len + 1);
10246 for (z = 0; z < len; z++)
10247 v->quick_push ((*params)[z]);
10248 f = build_function_call_vec (loc, function, v, NULL);
10249 vec_free (v);
10250 return f;
10253 /* Add the size parameter and leave as a function call for processing. */
10254 size_node = build_int_cst (size_type_node, n);
10255 params->quick_insert (0, size_node);
10256 return NULL_TREE;
10260 /* This will process an __atomic_exchange function call, determine whether it
10261 needs to be mapped to the _N variation, or turned into a library call.
10262 LOC is the location of the builtin call.
10263 FUNCTION is the DECL that has been invoked;
10264 PARAMS is the argument list for the call. The return value is non-null
10265 TRUE is returned if it is translated into the proper format for a call to the
10266 external library, and NEW_RETURN is set the tree for that function.
10267 FALSE is returned if processing for the _N variation is required, and
10268 NEW_RETURN is set to the the return value the result is copied into. */
10269 static bool
10270 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10271 vec<tree, va_gc> *params, tree *new_return)
10273 tree p0, p1, p2, p3;
10274 tree I_type, I_type_ptr;
10275 int n = get_atomic_generic_size (loc, function, params);
10277 /* Size of 0 is an error condition. */
10278 if (n == 0)
10280 *new_return = error_mark_node;
10281 return true;
10284 /* If not a lock-free size, change to the library generic format. */
10285 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10287 *new_return = add_atomic_size_parameter (n, loc, function, params);
10288 return true;
10291 /* Otherwise there is a lockfree match, transform the call from:
10292 void fn(T* mem, T* desired, T* return, model)
10293 into
10294 *return = (T) (fn (In* mem, (In) *desired, model)) */
10296 p0 = (*params)[0];
10297 p1 = (*params)[1];
10298 p2 = (*params)[2];
10299 p3 = (*params)[3];
10301 /* Create pointer to appropriate size. */
10302 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10303 I_type_ptr = build_pointer_type (I_type);
10305 /* Convert object pointer to required type. */
10306 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10307 (*params)[0] = p0;
10308 /* Convert new value to required type, and dereference it. */
10309 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10310 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10311 (*params)[1] = p1;
10313 /* Move memory model to the 3rd position, and end param list. */
10314 (*params)[2] = p3;
10315 params->truncate (3);
10317 /* Convert return pointer and dereference it for later assignment. */
10318 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10320 return false;
10324 /* This will process an __atomic_compare_exchange function call, determine
10325 whether it needs to be mapped to the _N variation, or turned into a lib call.
10326 LOC is the location of the builtin call.
10327 FUNCTION is the DECL that has been invoked;
10328 PARAMS is the argument list for the call. The return value is non-null
10329 TRUE is returned if it is translated into the proper format for a call to the
10330 external library, and NEW_RETURN is set the tree for that function.
10331 FALSE is returned if processing for the _N variation is required. */
10333 static bool
10334 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10335 vec<tree, va_gc> *params,
10336 tree *new_return)
10338 tree p0, p1, p2;
10339 tree I_type, I_type_ptr;
10340 int n = get_atomic_generic_size (loc, function, params);
10342 /* Size of 0 is an error condition. */
10343 if (n == 0)
10345 *new_return = error_mark_node;
10346 return true;
10349 /* If not a lock-free size, change to the library generic format. */
10350 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10352 /* The library generic format does not have the weak parameter, so
10353 remove it from the param list. Since a parameter has been removed,
10354 we can be sure that there is room for the SIZE_T parameter, meaning
10355 there will not be a recursive rebuilding of the parameter list, so
10356 there is no danger this will be done twice. */
10357 if (n > 0)
10359 (*params)[3] = (*params)[4];
10360 (*params)[4] = (*params)[5];
10361 params->truncate (5);
10363 *new_return = add_atomic_size_parameter (n, loc, function, params);
10364 return true;
10367 /* Otherwise, there is a match, so the call needs to be transformed from:
10368 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10369 into
10370 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10372 p0 = (*params)[0];
10373 p1 = (*params)[1];
10374 p2 = (*params)[2];
10376 /* Create pointer to appropriate size. */
10377 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10378 I_type_ptr = build_pointer_type (I_type);
10380 /* Convert object pointer to required type. */
10381 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10382 (*params)[0] = p0;
10384 /* Convert expected pointer to required type. */
10385 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10386 (*params)[1] = p1;
10388 /* Convert desired value to required type, and dereference it. */
10389 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10390 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10391 (*params)[2] = p2;
10393 /* The rest of the parameters are fine. NULL means no special return value
10394 processing.*/
10395 *new_return = NULL;
10396 return false;
10400 /* This will process an __atomic_load function call, determine whether it
10401 needs to be mapped to the _N variation, or turned into a library call.
10402 LOC is the location of the builtin call.
10403 FUNCTION is the DECL that has been invoked;
10404 PARAMS is the argument list for the call. The return value is non-null
10405 TRUE is returned if it is translated into the proper format for a call to the
10406 external library, and NEW_RETURN is set the tree for that function.
10407 FALSE is returned if processing for the _N variation is required, and
10408 NEW_RETURN is set to the the return value the result is copied into. */
10410 static bool
10411 resolve_overloaded_atomic_load (location_t loc, tree function,
10412 vec<tree, va_gc> *params, tree *new_return)
10414 tree p0, p1, p2;
10415 tree I_type, I_type_ptr;
10416 int n = get_atomic_generic_size (loc, function, params);
10418 /* Size of 0 is an error condition. */
10419 if (n == 0)
10421 *new_return = error_mark_node;
10422 return true;
10425 /* If not a lock-free size, change to the library generic format. */
10426 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10428 *new_return = add_atomic_size_parameter (n, loc, function, params);
10429 return true;
10432 /* Otherwise, there is a match, so the call needs to be transformed from:
10433 void fn(T* mem, T* return, model)
10434 into
10435 *return = (T) (fn ((In *) mem, model)) */
10437 p0 = (*params)[0];
10438 p1 = (*params)[1];
10439 p2 = (*params)[2];
10441 /* Create pointer to appropriate size. */
10442 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10443 I_type_ptr = build_pointer_type (I_type);
10445 /* Convert object pointer to required type. */
10446 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10447 (*params)[0] = p0;
10449 /* Move memory model to the 2nd position, and end param list. */
10450 (*params)[1] = p2;
10451 params->truncate (2);
10453 /* Convert return pointer and dereference it for later assignment. */
10454 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10456 return false;
10460 /* This will process an __atomic_store function call, determine whether it
10461 needs to be mapped to the _N variation, or turned into a library call.
10462 LOC is the location of the builtin call.
10463 FUNCTION is the DECL that has been invoked;
10464 PARAMS is the argument list for the call. The return value is non-null
10465 TRUE is returned if it is translated into the proper format for a call to the
10466 external library, and NEW_RETURN is set the tree for that function.
10467 FALSE is returned if processing for the _N variation is required, and
10468 NEW_RETURN is set to the the return value the result is copied into. */
10470 static bool
10471 resolve_overloaded_atomic_store (location_t loc, tree function,
10472 vec<tree, va_gc> *params, tree *new_return)
10474 tree p0, p1;
10475 tree I_type, I_type_ptr;
10476 int n = get_atomic_generic_size (loc, function, params);
10478 /* Size of 0 is an error condition. */
10479 if (n == 0)
10481 *new_return = error_mark_node;
10482 return true;
10485 /* If not a lock-free size, change to the library generic format. */
10486 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10488 *new_return = add_atomic_size_parameter (n, loc, function, params);
10489 return true;
10492 /* Otherwise, there is a match, so the call needs to be transformed from:
10493 void fn(T* mem, T* value, model)
10494 into
10495 fn ((In *) mem, (In) *value, model) */
10497 p0 = (*params)[0];
10498 p1 = (*params)[1];
10500 /* Create pointer to appropriate size. */
10501 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10502 I_type_ptr = build_pointer_type (I_type);
10504 /* Convert object pointer to required type. */
10505 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10506 (*params)[0] = p0;
10508 /* Convert new value to required type, and dereference it. */
10509 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10510 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10511 (*params)[1] = p1;
10513 /* The memory model is in the right spot already. Return is void. */
10514 *new_return = NULL_TREE;
10516 return false;
10520 /* Some builtin functions are placeholders for other expressions. This
10521 function should be called immediately after parsing the call expression
10522 before surrounding code has committed to the type of the expression.
10524 LOC is the location of the builtin call.
10526 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10527 PARAMS is the argument list for the call. The return value is non-null
10528 when expansion is complete, and null if normal processing should
10529 continue. */
10531 tree
10532 resolve_overloaded_builtin (location_t loc, tree function,
10533 vec<tree, va_gc> *params)
10535 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10536 bool orig_format = true;
10537 tree new_return = NULL_TREE;
10539 switch (DECL_BUILT_IN_CLASS (function))
10541 case BUILT_IN_NORMAL:
10542 break;
10543 case BUILT_IN_MD:
10544 if (targetm.resolve_overloaded_builtin)
10545 return targetm.resolve_overloaded_builtin (loc, function, params);
10546 else
10547 return NULL_TREE;
10548 default:
10549 return NULL_TREE;
10552 /* Handle BUILT_IN_NORMAL here. */
10553 switch (orig_code)
10555 case BUILT_IN_ATOMIC_EXCHANGE:
10556 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10557 case BUILT_IN_ATOMIC_LOAD:
10558 case BUILT_IN_ATOMIC_STORE:
10560 /* Handle these 4 together so that they can fall through to the next
10561 case if the call is transformed to an _N variant. */
10562 switch (orig_code)
10564 case BUILT_IN_ATOMIC_EXCHANGE:
10566 if (resolve_overloaded_atomic_exchange (loc, function, params,
10567 &new_return))
10568 return new_return;
10569 /* Change to the _N variant. */
10570 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10571 break;
10574 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10576 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10577 params,
10578 &new_return))
10579 return new_return;
10580 /* Change to the _N variant. */
10581 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10582 break;
10584 case BUILT_IN_ATOMIC_LOAD:
10586 if (resolve_overloaded_atomic_load (loc, function, params,
10587 &new_return))
10588 return new_return;
10589 /* Change to the _N variant. */
10590 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10591 break;
10593 case BUILT_IN_ATOMIC_STORE:
10595 if (resolve_overloaded_atomic_store (loc, function, params,
10596 &new_return))
10597 return new_return;
10598 /* Change to the _N variant. */
10599 orig_code = BUILT_IN_ATOMIC_STORE_N;
10600 break;
10602 default:
10603 gcc_unreachable ();
10605 /* Fallthrough to the normal processing. */
10607 case BUILT_IN_ATOMIC_EXCHANGE_N:
10608 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10609 case BUILT_IN_ATOMIC_LOAD_N:
10610 case BUILT_IN_ATOMIC_STORE_N:
10611 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10612 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10613 case BUILT_IN_ATOMIC_AND_FETCH_N:
10614 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10615 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10616 case BUILT_IN_ATOMIC_OR_FETCH_N:
10617 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10618 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10619 case BUILT_IN_ATOMIC_FETCH_AND_N:
10620 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10621 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10622 case BUILT_IN_ATOMIC_FETCH_OR_N:
10624 orig_format = false;
10625 /* Fallthru for parameter processing. */
10627 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10628 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10629 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10630 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10631 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10632 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10633 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10634 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10635 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10636 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10637 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10638 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10639 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10640 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10641 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10642 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10644 int n = sync_resolve_size (function, params);
10645 tree new_function, first_param, result;
10646 enum built_in_function fncode;
10648 if (n == 0)
10649 return error_mark_node;
10651 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10652 new_function = builtin_decl_explicit (fncode);
10653 if (!sync_resolve_params (loc, function, new_function, params,
10654 orig_format))
10655 return error_mark_node;
10657 first_param = (*params)[0];
10658 result = build_function_call_vec (loc, new_function, params, NULL);
10659 if (result == error_mark_node)
10660 return result;
10661 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10662 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10663 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10664 result = sync_resolve_return (first_param, result, orig_format);
10666 /* If new_return is set, assign function to that expr and cast the
10667 result to void since the generic interface returned void. */
10668 if (new_return)
10670 /* Cast function result from I{1,2,4,8,16} to the required type. */
10671 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10672 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10673 result);
10674 TREE_SIDE_EFFECTS (result) = 1;
10675 protected_set_expr_location (result, loc);
10676 result = convert (void_type_node, result);
10678 return result;
10681 default:
10682 return NULL_TREE;
10686 /* Ignoring their sign, return true if two scalar types are the same. */
10687 bool
10688 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10690 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10692 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10693 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10694 || c2 == FIXED_POINT_TYPE));
10696 /* Equality works here because c_common_signed_type uses
10697 TYPE_MAIN_VARIANT. */
10698 return c_common_signed_type (t1)
10699 == c_common_signed_type (t2);
10702 /* Check for missing format attributes on function pointers. LTYPE is
10703 the new type or left-hand side type. RTYPE is the old type or
10704 right-hand side type. Returns TRUE if LTYPE is missing the desired
10705 attribute. */
10707 bool
10708 check_missing_format_attribute (tree ltype, tree rtype)
10710 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10711 tree ra;
10713 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10714 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10715 break;
10716 if (ra)
10718 tree la;
10719 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10720 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10721 break;
10722 return !la;
10724 else
10725 return false;
10728 /* Subscripting with type char is likely to lose on a machine where
10729 chars are signed. So warn on any machine, but optionally. Don't
10730 warn for unsigned char since that type is safe. Don't warn for
10731 signed char because anyone who uses that must have done so
10732 deliberately. Furthermore, we reduce the false positive load by
10733 warning only for non-constant value of type char. */
10735 void
10736 warn_array_subscript_with_type_char (tree index)
10738 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10739 && TREE_CODE (index) != INTEGER_CST)
10740 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10743 /* Implement -Wparentheses for the unexpected C precedence rules, to
10744 cover cases like x + y << z which readers are likely to
10745 misinterpret. We have seen an expression in which CODE is a binary
10746 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10747 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10748 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10749 expression was not formed using a binary or unary operator, or it
10750 was enclosed in parentheses. */
10752 void
10753 warn_about_parentheses (location_t loc, enum tree_code code,
10754 enum tree_code code_left, tree arg_left,
10755 enum tree_code code_right, tree arg_right)
10757 if (!warn_parentheses)
10758 return;
10760 /* This macro tests that the expression ARG with original tree code
10761 CODE appears to be a boolean expression. or the result of folding a
10762 boolean expression. */
10763 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10764 (truth_value_p (TREE_CODE (ARG)) \
10765 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10766 /* Folding may create 0 or 1 integers from other expressions. */ \
10767 || ((CODE) != INTEGER_CST \
10768 && (integer_onep (ARG) || integer_zerop (ARG))))
10770 switch (code)
10772 case LSHIFT_EXPR:
10773 if (code_left == PLUS_EXPR)
10774 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10775 "suggest parentheses around %<+%> inside %<<<%>");
10776 else if (code_right == PLUS_EXPR)
10777 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10778 "suggest parentheses around %<+%> inside %<<<%>");
10779 else if (code_left == MINUS_EXPR)
10780 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10781 "suggest parentheses around %<-%> inside %<<<%>");
10782 else if (code_right == MINUS_EXPR)
10783 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10784 "suggest parentheses around %<-%> inside %<<<%>");
10785 return;
10787 case RSHIFT_EXPR:
10788 if (code_left == PLUS_EXPR)
10789 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10790 "suggest parentheses around %<+%> inside %<>>%>");
10791 else if (code_right == PLUS_EXPR)
10792 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10793 "suggest parentheses around %<+%> inside %<>>%>");
10794 else if (code_left == MINUS_EXPR)
10795 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10796 "suggest parentheses around %<-%> inside %<>>%>");
10797 else if (code_right == MINUS_EXPR)
10798 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10799 "suggest parentheses around %<-%> inside %<>>%>");
10800 return;
10802 case TRUTH_ORIF_EXPR:
10803 if (code_left == TRUTH_ANDIF_EXPR)
10804 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10805 "suggest parentheses around %<&&%> within %<||%>");
10806 else if (code_right == TRUTH_ANDIF_EXPR)
10807 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10808 "suggest parentheses around %<&&%> within %<||%>");
10809 return;
10811 case BIT_IOR_EXPR:
10812 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10813 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10814 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10815 "suggest parentheses around arithmetic in operand of %<|%>");
10816 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10817 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10818 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10819 "suggest parentheses around arithmetic in operand of %<|%>");
10820 /* Check cases like x|y==z */
10821 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10822 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10823 "suggest parentheses around comparison in operand of %<|%>");
10824 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10825 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10826 "suggest parentheses around comparison in operand of %<|%>");
10827 /* Check cases like !x | y */
10828 else if (code_left == TRUTH_NOT_EXPR
10829 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10830 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10831 "suggest parentheses around operand of "
10832 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10833 return;
10835 case BIT_XOR_EXPR:
10836 if (code_left == BIT_AND_EXPR
10837 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10838 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10839 "suggest parentheses around arithmetic in operand of %<^%>");
10840 else if (code_right == BIT_AND_EXPR
10841 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10842 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10843 "suggest parentheses around arithmetic in operand of %<^%>");
10844 /* Check cases like x^y==z */
10845 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10846 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10847 "suggest parentheses around comparison in operand of %<^%>");
10848 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10849 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10850 "suggest parentheses around comparison in operand of %<^%>");
10851 return;
10853 case BIT_AND_EXPR:
10854 if (code_left == PLUS_EXPR)
10855 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10856 "suggest parentheses around %<+%> in operand of %<&%>");
10857 else if (code_right == PLUS_EXPR)
10858 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10859 "suggest parentheses around %<+%> in operand of %<&%>");
10860 else if (code_left == MINUS_EXPR)
10861 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10862 "suggest parentheses around %<-%> in operand of %<&%>");
10863 else if (code_right == MINUS_EXPR)
10864 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10865 "suggest parentheses around %<-%> in operand of %<&%>");
10866 /* Check cases like x&y==z */
10867 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10868 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10869 "suggest parentheses around comparison in operand of %<&%>");
10870 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10871 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10872 "suggest parentheses around comparison in operand of %<&%>");
10873 /* Check cases like !x & y */
10874 else if (code_left == TRUTH_NOT_EXPR
10875 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10876 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10877 "suggest parentheses around operand of "
10878 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10879 return;
10881 case EQ_EXPR:
10882 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10883 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10884 "suggest parentheses around comparison in operand of %<==%>");
10885 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10886 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10887 "suggest parentheses around comparison in operand of %<==%>");
10888 return;
10889 case NE_EXPR:
10890 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10891 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10892 "suggest parentheses around comparison in operand of %<!=%>");
10893 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10894 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10895 "suggest parentheses around comparison in operand of %<!=%>");
10896 return;
10898 default:
10899 if (TREE_CODE_CLASS (code) == tcc_comparison)
10901 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10902 && code_left != NE_EXPR && code_left != EQ_EXPR
10903 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10904 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10905 "comparisons like %<X<=Y<=Z%> do not "
10906 "have their mathematical meaning");
10907 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
10908 && code_right != NE_EXPR && code_right != EQ_EXPR
10909 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
10910 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10911 "comparisons like %<X<=Y<=Z%> do not "
10912 "have their mathematical meaning");
10914 return;
10916 #undef NOT_A_BOOLEAN_EXPR_P
10919 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10921 void
10922 warn_for_unused_label (tree label)
10924 if (!TREE_USED (label))
10926 if (DECL_INITIAL (label))
10927 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10928 else
10929 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10933 /* Warn for division by zero according to the value of DIVISOR. LOC
10934 is the location of the division operator. */
10936 void
10937 warn_for_div_by_zero (location_t loc, tree divisor)
10939 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10940 about division by zero. Do not issue a warning if DIVISOR has a
10941 floating-point type, since we consider 0.0/0.0 a valid way of
10942 generating a NaN. */
10943 if (c_inhibit_evaluation_warnings == 0
10944 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10945 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10948 /* Subroutine of build_binary_op. Give warnings for comparisons
10949 between signed and unsigned quantities that may fail. Do the
10950 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10951 so that casts will be considered, but default promotions won't
10954 LOCATION is the location of the comparison operator.
10956 The arguments of this function map directly to local variables
10957 of build_binary_op. */
10959 void
10960 warn_for_sign_compare (location_t location,
10961 tree orig_op0, tree orig_op1,
10962 tree op0, tree op1,
10963 tree result_type, enum tree_code resultcode)
10965 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10966 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10967 int unsignedp0, unsignedp1;
10969 /* In C++, check for comparison of different enum types. */
10970 if (c_dialect_cxx()
10971 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10972 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10973 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10974 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10976 warning_at (location,
10977 OPT_Wsign_compare, "comparison between types %qT and %qT",
10978 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10981 /* Do not warn if the comparison is being done in a signed type,
10982 since the signed type will only be chosen if it can represent
10983 all the values of the unsigned type. */
10984 if (!TYPE_UNSIGNED (result_type))
10985 /* OK */;
10986 /* Do not warn if both operands are unsigned. */
10987 else if (op0_signed == op1_signed)
10988 /* OK */;
10989 else
10991 tree sop, uop, base_type;
10992 bool ovf;
10994 if (op0_signed)
10995 sop = orig_op0, uop = orig_op1;
10996 else
10997 sop = orig_op1, uop = orig_op0;
10999 STRIP_TYPE_NOPS (sop);
11000 STRIP_TYPE_NOPS (uop);
11001 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11002 ? TREE_TYPE (result_type) : result_type);
11004 /* Do not warn if the signed quantity is an unsuffixed integer
11005 literal (or some static constant expression involving such
11006 literals or a conditional expression involving such literals)
11007 and it is non-negative. */
11008 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11009 /* OK */;
11010 /* Do not warn if the comparison is an equality operation, the
11011 unsigned quantity is an integral constant, and it would fit
11012 in the result if the result were signed. */
11013 else if (TREE_CODE (uop) == INTEGER_CST
11014 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11015 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11016 /* OK */;
11017 /* In C, do not warn if the unsigned quantity is an enumeration
11018 constant and its maximum value would fit in the result if the
11019 result were signed. */
11020 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11021 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11022 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11023 c_common_signed_type (base_type)))
11024 /* OK */;
11025 else
11026 warning_at (location,
11027 OPT_Wsign_compare,
11028 "comparison between signed and unsigned integer expressions");
11031 /* Warn if two unsigned values are being compared in a size larger
11032 than their original size, and one (and only one) is the result of
11033 a `~' operator. This comparison will always fail.
11035 Also warn if one operand is a constant, and the constant does not
11036 have all bits set that are set in the ~ operand when it is
11037 extended. */
11039 op0 = c_common_get_narrower (op0, &unsignedp0);
11040 op1 = c_common_get_narrower (op1, &unsignedp1);
11042 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11043 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11045 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11046 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11047 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11048 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11050 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11052 tree primop;
11053 HOST_WIDE_INT constant, mask;
11054 int unsignedp;
11055 unsigned int bits;
11057 if (host_integerp (op0, 0))
11059 primop = op1;
11060 unsignedp = unsignedp1;
11061 constant = tree_low_cst (op0, 0);
11063 else
11065 primop = op0;
11066 unsignedp = unsignedp0;
11067 constant = tree_low_cst (op1, 0);
11070 bits = TYPE_PRECISION (TREE_TYPE (primop));
11071 if (bits < TYPE_PRECISION (result_type)
11072 && bits < HOST_BITS_PER_LONG && unsignedp)
11074 mask = (~ (HOST_WIDE_INT) 0) << bits;
11075 if ((mask & constant) != mask)
11077 if (constant == 0)
11078 warning (OPT_Wsign_compare,
11079 "promoted ~unsigned is always non-zero");
11080 else
11081 warning_at (location, OPT_Wsign_compare,
11082 "comparison of promoted ~unsigned with constant");
11086 else if (unsignedp0 && unsignedp1
11087 && (TYPE_PRECISION (TREE_TYPE (op0))
11088 < TYPE_PRECISION (result_type))
11089 && (TYPE_PRECISION (TREE_TYPE (op1))
11090 < TYPE_PRECISION (result_type)))
11091 warning_at (location, OPT_Wsign_compare,
11092 "comparison of promoted ~unsigned with unsigned");
11096 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11097 type via c_common_type. If -Wdouble-promotion is in use, and the
11098 conditions for warning have been met, issue a warning. GMSGID is
11099 the warning message. It must have two %T specifiers for the type
11100 that was converted (generally "float") and the type to which it was
11101 converted (generally "double), respectively. LOC is the location
11102 to which the awrning should refer. */
11104 void
11105 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11106 const char *gmsgid, location_t loc)
11108 tree source_type;
11110 if (!warn_double_promotion)
11111 return;
11112 /* If the conversion will not occur at run-time, there is no need to
11113 warn about it. */
11114 if (c_inhibit_evaluation_warnings)
11115 return;
11116 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11117 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11118 return;
11119 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11120 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11121 source_type = type1;
11122 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11123 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11124 source_type = type2;
11125 else
11126 return;
11127 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11130 /* Setup a TYPE_DECL node as a typedef representation.
11132 X is a TYPE_DECL for a typedef statement. Create a brand new
11133 ..._TYPE node (which will be just a variant of the existing
11134 ..._TYPE node with identical properties) and then install X
11135 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11137 The whole point here is to end up with a situation where each
11138 and every ..._TYPE node the compiler creates will be uniquely
11139 associated with AT MOST one node representing a typedef name.
11140 This way, even though the compiler substitutes corresponding
11141 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11142 early on, later parts of the compiler can always do the reverse
11143 translation and get back the corresponding typedef name. For
11144 example, given:
11146 typedef struct S MY_TYPE;
11147 MY_TYPE object;
11149 Later parts of the compiler might only know that `object' was of
11150 type `struct S' if it were not for code just below. With this
11151 code however, later parts of the compiler see something like:
11153 struct S' == struct S
11154 typedef struct S' MY_TYPE;
11155 struct S' object;
11157 And they can then deduce (from the node for type struct S') that
11158 the original object declaration was:
11160 MY_TYPE object;
11162 Being able to do this is important for proper support of protoize,
11163 and also for generating precise symbolic debugging information
11164 which takes full account of the programmer's (typedef) vocabulary.
11166 Obviously, we don't want to generate a duplicate ..._TYPE node if
11167 the TYPE_DECL node that we are now processing really represents a
11168 standard built-in type. */
11170 void
11171 set_underlying_type (tree x)
11173 if (x == error_mark_node)
11174 return;
11175 if (DECL_IS_BUILTIN (x))
11177 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11178 TYPE_NAME (TREE_TYPE (x)) = x;
11180 else if (TREE_TYPE (x) != error_mark_node
11181 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11183 tree tt = TREE_TYPE (x);
11184 DECL_ORIGINAL_TYPE (x) = tt;
11185 tt = build_variant_type_copy (tt);
11186 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11187 TYPE_NAME (tt) = x;
11188 TREE_USED (tt) = TREE_USED (x);
11189 TREE_TYPE (x) = tt;
11193 /* Record the types used by the current global variable declaration
11194 being parsed, so that we can decide later to emit their debug info.
11195 Those types are in types_used_by_cur_var_decl, and we are going to
11196 store them in the types_used_by_vars_hash hash table.
11197 DECL is the declaration of the global variable that has been parsed. */
11199 void
11200 record_types_used_by_current_var_decl (tree decl)
11202 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11204 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11206 tree type = types_used_by_cur_var_decl->pop ();
11207 types_used_by_var_decl_insert (type, decl);
11211 /* If DECL is a typedef that is declared in the current function,
11212 record it for the purpose of -Wunused-local-typedefs. */
11214 void
11215 record_locally_defined_typedef (tree decl)
11217 struct c_language_function *l;
11219 if (!warn_unused_local_typedefs
11220 || cfun == NULL
11221 /* if this is not a locally defined typedef then we are not
11222 interested. */
11223 || !is_typedef_decl (decl)
11224 || !decl_function_context (decl))
11225 return;
11227 l = (struct c_language_function *) cfun->language;
11228 vec_safe_push (l->local_typedefs, decl);
11231 /* If T is a TYPE_DECL declared locally, mark it as used. */
11233 void
11234 maybe_record_typedef_use (tree t)
11236 if (!is_typedef_decl (t))
11237 return;
11239 TREE_USED (t) = true;
11242 /* Warn if there are some unused locally defined typedefs in the
11243 current function. */
11245 void
11246 maybe_warn_unused_local_typedefs (void)
11248 int i;
11249 tree decl;
11250 /* The number of times we have emitted -Wunused-local-typedefs
11251 warnings. If this is different from errorcount, that means some
11252 unrelated errors have been issued. In which case, we'll avoid
11253 emitting "unused-local-typedefs" warnings. */
11254 static int unused_local_typedefs_warn_count;
11255 struct c_language_function *l;
11257 if (cfun == NULL)
11258 return;
11260 if ((l = (struct c_language_function *) cfun->language) == NULL)
11261 return;
11263 if (warn_unused_local_typedefs
11264 && errorcount == unused_local_typedefs_warn_count)
11266 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11267 if (!TREE_USED (decl))
11268 warning_at (DECL_SOURCE_LOCATION (decl),
11269 OPT_Wunused_local_typedefs,
11270 "typedef %qD locally defined but not used", decl);
11271 unused_local_typedefs_warn_count = errorcount;
11274 vec_free (l->local_typedefs);
11277 /* The C and C++ parsers both use vectors to hold function arguments.
11278 For efficiency, we keep a cache of unused vectors. This is the
11279 cache. */
11281 typedef vec<tree, va_gc> *tree_gc_vec;
11282 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11284 /* Return a new vector from the cache. If the cache is empty,
11285 allocate a new vector. These vectors are GC'ed, so it is OK if the
11286 pointer is not released.. */
11288 vec<tree, va_gc> *
11289 make_tree_vector (void)
11291 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11292 return tree_vector_cache->pop ();
11293 else
11295 /* Passing 0 to vec::alloc returns NULL, and our callers require
11296 that we always return a non-NULL value. The vector code uses
11297 4 when growing a NULL vector, so we do too. */
11298 vec<tree, va_gc> *v;
11299 vec_alloc (v, 4);
11300 return v;
11304 /* Release a vector of trees back to the cache. */
11306 void
11307 release_tree_vector (vec<tree, va_gc> *vec)
11309 if (vec != NULL)
11311 vec->truncate (0);
11312 vec_safe_push (tree_vector_cache, vec);
11316 /* Get a new tree vector holding a single tree. */
11318 vec<tree, va_gc> *
11319 make_tree_vector_single (tree t)
11321 vec<tree, va_gc> *ret = make_tree_vector ();
11322 ret->quick_push (t);
11323 return ret;
11326 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11328 vec<tree, va_gc> *
11329 make_tree_vector_from_list (tree list)
11331 vec<tree, va_gc> *ret = make_tree_vector ();
11332 for (; list; list = TREE_CHAIN (list))
11333 vec_safe_push (ret, TREE_VALUE (list));
11334 return ret;
11337 /* Get a new tree vector which is a copy of an existing one. */
11339 vec<tree, va_gc> *
11340 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11342 vec<tree, va_gc> *ret;
11343 unsigned int ix;
11344 tree t;
11346 ret = make_tree_vector ();
11347 vec_safe_reserve (ret, vec_safe_length (orig));
11348 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11349 ret->quick_push (t);
11350 return ret;
11353 /* Return true if KEYWORD starts a type specifier. */
11355 bool
11356 keyword_begins_type_specifier (enum rid keyword)
11358 switch (keyword)
11360 case RID_INT:
11361 case RID_CHAR:
11362 case RID_FLOAT:
11363 case RID_DOUBLE:
11364 case RID_VOID:
11365 case RID_INT128:
11366 case RID_UNSIGNED:
11367 case RID_LONG:
11368 case RID_SHORT:
11369 case RID_SIGNED:
11370 case RID_DFLOAT32:
11371 case RID_DFLOAT64:
11372 case RID_DFLOAT128:
11373 case RID_FRACT:
11374 case RID_ACCUM:
11375 case RID_BOOL:
11376 case RID_WCHAR:
11377 case RID_CHAR16:
11378 case RID_CHAR32:
11379 case RID_SAT:
11380 case RID_COMPLEX:
11381 case RID_TYPEOF:
11382 case RID_STRUCT:
11383 case RID_CLASS:
11384 case RID_UNION:
11385 case RID_ENUM:
11386 return true;
11387 default:
11388 return false;
11392 /* Return true if KEYWORD names a type qualifier. */
11394 bool
11395 keyword_is_type_qualifier (enum rid keyword)
11397 switch (keyword)
11399 case RID_CONST:
11400 case RID_VOLATILE:
11401 case RID_RESTRICT:
11402 return true;
11403 default:
11404 return false;
11408 /* Return true if KEYWORD names a storage class specifier.
11410 RID_TYPEDEF is not included in this list despite `typedef' being
11411 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11412 such for syntactic convenience only. */
11414 bool
11415 keyword_is_storage_class_specifier (enum rid keyword)
11417 switch (keyword)
11419 case RID_STATIC:
11420 case RID_EXTERN:
11421 case RID_REGISTER:
11422 case RID_AUTO:
11423 case RID_MUTABLE:
11424 case RID_THREAD:
11425 return true;
11426 default:
11427 return false;
11431 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11433 static bool
11434 keyword_is_function_specifier (enum rid keyword)
11436 switch (keyword)
11438 case RID_INLINE:
11439 case RID_NORETURN:
11440 case RID_VIRTUAL:
11441 case RID_EXPLICIT:
11442 return true;
11443 default:
11444 return false;
11448 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11449 declaration-specifier (C99 6.7). */
11451 bool
11452 keyword_is_decl_specifier (enum rid keyword)
11454 if (keyword_is_storage_class_specifier (keyword)
11455 || keyword_is_type_qualifier (keyword)
11456 || keyword_is_function_specifier (keyword))
11457 return true;
11459 switch (keyword)
11461 case RID_TYPEDEF:
11462 case RID_FRIEND:
11463 case RID_CONSTEXPR:
11464 return true;
11465 default:
11466 return false;
11470 /* Initialize language-specific-bits of tree_contains_struct. */
11472 void
11473 c_common_init_ts (void)
11475 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11476 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11479 /* Build a user-defined numeric literal out of an integer constant type VALUE
11480 with identifier SUFFIX. */
11482 tree
11483 build_userdef_literal (tree suffix_id, tree value,
11484 enum overflow_type overflow, tree num_string)
11486 tree literal = make_node (USERDEF_LITERAL);
11487 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11488 USERDEF_LITERAL_VALUE (literal) = value;
11489 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11490 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11491 return literal;
11494 /* For vector[index], convert the vector to a
11495 pointer of the underlying type. */
11496 void
11497 convert_vector_to_pointer_for_subscript (location_t loc,
11498 tree* vecp, tree index)
11500 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11502 tree type = TREE_TYPE (*vecp);
11503 tree type1;
11505 if (TREE_CODE (index) == INTEGER_CST)
11506 if (!host_integerp (index, 1)
11507 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11508 >= TYPE_VECTOR_SUBPARTS (type)))
11509 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11511 c_common_mark_addressable_vec (*vecp);
11512 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11513 type = build_pointer_type (type);
11514 type1 = build_pointer_type (TREE_TYPE (*vecp));
11515 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11516 *vecp = convert (type, *vecp);
11520 /* Determine which of the operands, if any, is a scalar that needs to be
11521 converted to a vector, for the range of operations. */
11522 enum stv_conv
11523 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11524 bool complain)
11526 tree type0 = TREE_TYPE (op0);
11527 tree type1 = TREE_TYPE (op1);
11528 bool integer_only_op = false;
11529 enum stv_conv ret = stv_firstarg;
11531 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11532 || TREE_CODE (type1) == VECTOR_TYPE);
11533 switch (code)
11535 /* Most GENERIC binary expressions require homogeneous arguments.
11536 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11537 argument that is a vector and a second one that is a scalar, so
11538 we never return stv_secondarg for them. */
11539 case RSHIFT_EXPR:
11540 case LSHIFT_EXPR:
11541 if (TREE_CODE (type0) == INTEGER_TYPE
11542 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11544 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11546 if (complain)
11547 error_at (loc, "conversion of scalar %qT to vector %qT "
11548 "involves truncation", type0, type1);
11549 return stv_error;
11551 else
11552 return stv_firstarg;
11554 break;
11556 case BIT_IOR_EXPR:
11557 case BIT_XOR_EXPR:
11558 case BIT_AND_EXPR:
11559 integer_only_op = true;
11560 /* ... fall through ... */
11562 case VEC_COND_EXPR:
11564 case PLUS_EXPR:
11565 case MINUS_EXPR:
11566 case MULT_EXPR:
11567 case TRUNC_DIV_EXPR:
11568 case CEIL_DIV_EXPR:
11569 case FLOOR_DIV_EXPR:
11570 case ROUND_DIV_EXPR:
11571 case EXACT_DIV_EXPR:
11572 case TRUNC_MOD_EXPR:
11573 case FLOOR_MOD_EXPR:
11574 case RDIV_EXPR:
11575 case EQ_EXPR:
11576 case NE_EXPR:
11577 case LE_EXPR:
11578 case GE_EXPR:
11579 case LT_EXPR:
11580 case GT_EXPR:
11581 /* What about UNLT_EXPR? */
11582 if (TREE_CODE (type0) == VECTOR_TYPE)
11584 tree tmp;
11585 ret = stv_secondarg;
11586 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11587 tmp = type0; type0 = type1; type1 = tmp;
11588 tmp = op0; op0 = op1; op1 = tmp;
11591 if (TREE_CODE (type0) == INTEGER_TYPE
11592 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11594 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11596 if (complain)
11597 error_at (loc, "conversion of scalar %qT to vector %qT "
11598 "involves truncation", type0, type1);
11599 return stv_error;
11601 return ret;
11603 else if (!integer_only_op
11604 /* Allow integer --> real conversion if safe. */
11605 && (TREE_CODE (type0) == REAL_TYPE
11606 || TREE_CODE (type0) == INTEGER_TYPE)
11607 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11609 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11611 if (complain)
11612 error_at (loc, "conversion of scalar %qT to vector %qT "
11613 "involves truncation", type0, type1);
11614 return stv_error;
11616 return ret;
11618 default:
11619 break;
11622 return stv_nothing;
11625 /* Return true iff ALIGN is an integral constant that is a fundamental
11626 alignment, as defined by [basic.align] in the c++-11
11627 specifications.
11629 That is:
11631 [A fundamental alignment is represented by an alignment less than or
11632 equal to the greatest alignment supported by the implementation
11633 in all contexts, which is equal to
11634 alignof(max_align_t)]. */
11636 bool
11637 cxx_fundamental_alignment_p (unsigned align)
11639 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11640 TYPE_ALIGN (long_double_type_node)));
11643 #include "gt-c-family-c-common.h"