Merge trunk version 194962 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blobdfd18cd60b31cf742b4808c480ec55602a198eb3
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "c-pragma.h"
30 #include "ggc.h"
31 #include "c-common.h"
32 #include "c-objc.h"
33 #include "c-upc.h"
34 #include "tm_p.h"
35 #include "obstack.h"
36 #include "cpplib.h"
37 #include "target.h"
38 #include "common/common-target.h"
39 #include "langhooks.h"
40 #include "tree-inline.h"
41 #include "toplev.h"
42 #include "diagnostic.h"
43 #include "tree-iterator.h"
44 #include "hashtab.h"
45 #include "tree-mudflap.h"
46 #include "opts.h"
47 #include "cgraph.h"
48 #include "target-def.h"
50 cpp_reader *parse_in; /* Declared in c-pragma.h. */
52 /* The following symbols are subsumed in the c_global_trees array, and
53 listed here individually for documentation purposes.
55 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
57 tree short_integer_type_node;
58 tree long_integer_type_node;
59 tree long_long_integer_type_node;
60 tree int128_integer_type_node;
62 tree short_unsigned_type_node;
63 tree long_unsigned_type_node;
64 tree long_long_unsigned_type_node;
65 tree int128_unsigned_type_node;
67 tree truthvalue_type_node;
68 tree truthvalue_false_node;
69 tree truthvalue_true_node;
71 tree ptrdiff_type_node;
73 tree unsigned_char_type_node;
74 tree signed_char_type_node;
75 tree wchar_type_node;
77 tree char16_type_node;
78 tree char32_type_node;
80 tree float_type_node;
81 tree double_type_node;
82 tree long_double_type_node;
84 tree complex_integer_type_node;
85 tree complex_float_type_node;
86 tree complex_double_type_node;
87 tree complex_long_double_type_node;
89 tree dfloat32_type_node;
90 tree dfloat64_type_node;
91 tree_dfloat128_type_node;
93 tree intQI_type_node;
94 tree intHI_type_node;
95 tree intSI_type_node;
96 tree intDI_type_node;
97 tree intTI_type_node;
99 tree unsigned_intQI_type_node;
100 tree unsigned_intHI_type_node;
101 tree unsigned_intSI_type_node;
102 tree unsigned_intDI_type_node;
103 tree unsigned_intTI_type_node;
105 tree widest_integer_literal_type_node;
106 tree widest_unsigned_literal_type_node;
108 Nodes for types `void *' and `const void *'.
110 tree ptr_type_node, const_ptr_type_node;
112 Nodes for types `char *' and `const char *'.
114 tree string_type_node, const_string_type_node;
116 Type `char[SOMENUMBER]'.
117 Used when an array of char is needed and the size is irrelevant.
119 tree char_array_type_node;
121 Type `int[SOMENUMBER]' or something like it.
122 Used when an array of int needed and the size is irrelevant.
124 tree int_array_type_node;
126 Type `wchar_t[SOMENUMBER]' or something like it.
127 Used when a wide string literal is created.
129 tree wchar_array_type_node;
131 Type `char16_t[SOMENUMBER]' or something like it.
132 Used when a UTF-16 string literal is created.
134 tree char16_array_type_node;
136 Type `char32_t[SOMENUMBER]' or something like it.
137 Used when a UTF-32 string literal is created.
139 tree char32_array_type_node;
141 Type `int ()' -- used for implicit declaration of functions.
143 tree default_function_type;
145 A VOID_TYPE node, packaged in a TREE_LIST.
147 tree void_list_node;
149 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
150 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
151 VAR_DECLS, but C++ does.)
153 tree function_name_decl_node;
154 tree pretty_function_name_decl_node;
155 tree c99_function_name_decl_node;
157 Stack of nested function name VAR_DECLs.
159 tree saved_function_name_decls;
163 tree c_global_trees[CTI_MAX];
165 /* Switches common to the C front ends. */
167 /* Nonzero means don't output line number information. */
169 char flag_no_line_commands;
171 /* Nonzero causes -E output not to be done, but directives such as
172 #define that have side effects are still obeyed. */
174 char flag_no_output;
176 /* Nonzero means dump macros in some fashion. */
178 char flag_dump_macros;
180 /* Nonzero means pass #include lines through to the output. */
182 char flag_dump_includes;
184 /* Nonzero means process PCH files while preprocessing. */
186 bool flag_pch_preprocess;
188 /* The file name to which we should write a precompiled header, or
189 NULL if no header will be written in this compile. */
191 const char *pch_file;
193 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 user's namespace. */
195 int flag_iso;
197 /* Nonzero whenever UPC -fupc-threads-N is asserted.
198 The value N gives the number of UPC threads to be
199 defined at compile-time. */
200 int flag_upc_threads;
202 /* Nonzero whenever UPC -fupc-pthreads-model-* is asserted. */
203 int flag_upc_pthreads;
205 /* The implementation model for UPC threads that
206 are mapped to POSIX threads, specified at compilation
207 time by the -fupc-pthreads-model-* switch. */
208 upc_pthreads_model_kind upc_pthreads_model;
210 /* C/ObjC language option variables. */
213 /* Nonzero means allow type mismatches in conditional expressions;
214 just make their values `void'. */
216 int flag_cond_mismatch;
218 /* Nonzero means enable C89 Amendment 1 features. */
220 int flag_isoc94;
222 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
224 int flag_isoc99;
226 /* Nonzero means use the ISO C11 dialect of C. */
228 int flag_isoc11;
230 /* Nonzero means that we have builtin functions, and main is an int. */
232 int flag_hosted = 1;
235 /* ObjC language option variables. */
238 /* Tells the compiler that this is a special run. Do not perform any
239 compiling, instead we are to test some platform dependent features
240 and output a C header file with appropriate definitions. */
242 int print_struct_values;
244 /* Tells the compiler what is the constant string class for ObjC. */
246 const char *constant_string_class_name;
249 /* C++ language option variables. */
252 /* Nonzero means generate separate instantiation control files and
253 juggle them at link time. */
255 int flag_use_repository;
257 /* The C++ dialect being used. C++98 is the default. */
259 enum cxx_dialect cxx_dialect = cxx98;
261 /* Maximum template instantiation depth. This limit exists to limit the
262 time it takes to notice excessively recursive template instantiations.
264 The default is lower than the 1024 recommended by the C++0x standard
265 because G++ runs out of stack before 1024 with highly recursive template
266 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
268 int max_tinst_depth = 900;
270 /* The elements of `ridpointers' are identifier nodes for the reserved
271 type names and storage classes. It is indexed by a RID_... value. */
272 tree *ridpointers;
274 tree (*make_fname_decl) (location_t, tree, int);
276 /* Nonzero means don't warn about problems that occur when the code is
277 executed. */
278 int c_inhibit_evaluation_warnings;
280 /* Whether we are building a boolean conversion inside
281 convert_for_assignment, or some other late binary operation. If
282 build_binary_op is called for C (from code shared by C and C++) in
283 this case, then the operands have already been folded and the
284 result will not be folded again, so C_MAYBE_CONST_EXPR should not
285 be generated. */
286 bool in_late_binary_op;
288 /* Whether lexing has been completed, so subsequent preprocessor
289 errors should use the compiler's input_location. */
290 bool done_lexing = false;
292 /* Information about how a function name is generated. */
293 struct fname_var_t
295 tree *const decl; /* pointer to the VAR_DECL. */
296 const unsigned rid; /* RID number for the identifier. */
297 const int pretty; /* How pretty is it? */
300 /* The three ways of getting then name of the current function. */
302 const struct fname_var_t fname_vars[] =
304 /* C99 compliant __func__, must be first. */
305 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
306 /* GCC __FUNCTION__ compliant. */
307 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
308 /* GCC __PRETTY_FUNCTION__ compliant. */
309 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
310 {NULL, 0, 0},
313 /* Global visibility options. */
314 struct visibility_flags visibility_options;
316 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
317 static tree check_case_value (tree);
318 static bool check_case_bounds (tree, tree, tree *, tree *);
320 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
327 int, bool *);
328 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_always_inline_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
340 bool *);
341 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_transparent_union_attribute (tree *, tree, tree,
343 int, bool *);
344 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
350 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
351 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_visibility_attribute (tree *, tree, tree, int,
355 bool *);
356 static tree handle_tls_model_attribute (tree *, tree, tree, int,
357 bool *);
358 static tree handle_no_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
360 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
377 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
382 static tree ignore_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
386 static void check_function_nonnull (tree, int, tree *);
387 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
388 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
389 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
390 static int resort_field_decl_cmp (const void *, const void *);
392 /* Reserved words. The third field is a mask: keywords are disabled
393 if they match the mask.
395 Masks for languages:
396 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
397 C --std=c99: D_CXXONLY | D_OBJC
398 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
399 UPC is like C except that D_UPC is not set
400 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
401 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
402 ObjC++ is like C++ except that D_OBJC is not set
404 If -fno-asm is used, D_ASM is added to the mask. If
405 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
406 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
407 In C with -Wc++-compat, we warn if D_CXXWARN is set.
409 Note the complication of the D_CXX_OBJC keywords. These are
410 reserved words such as 'class'. In C++, 'class' is a reserved
411 word. In Objective-C++ it is too. In Objective-C, it is a
412 reserved word too, but only if it follows an '@' sign.
414 const struct c_common_resword c_common_reswords[] =
416 { "_Alignas", RID_ALIGNAS, D_CONLY },
417 { "_Alignof", RID_ALIGNOF, D_CONLY },
418 { "_Bool", RID_BOOL, D_CONLY },
419 { "_Complex", RID_COMPLEX, 0 },
420 { "_Imaginary", RID_IMAGINARY, D_CONLY },
421 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
422 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
423 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
424 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
425 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
426 { "_Sat", RID_SAT, D_CONLY | D_EXT },
427 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
428 { "_Noreturn", RID_NORETURN, D_CONLY },
429 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
430 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
431 { "__alignof", RID_ALIGNOF, 0 },
432 { "__alignof__", RID_ALIGNOF, 0 },
433 { "__asm", RID_ASM, 0 },
434 { "__asm__", RID_ASM, 0 },
435 { "__attribute", RID_ATTRIBUTE, 0 },
436 { "__attribute__", RID_ATTRIBUTE, 0 },
437 { "__bases", RID_BASES, D_CXXONLY },
438 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
439 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
440 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
441 { "__builtin_offsetof", RID_OFFSETOF, 0 },
442 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
443 { "__builtin_va_arg", RID_VA_ARG, 0 },
444 { "__complex", RID_COMPLEX, 0 },
445 { "__complex__", RID_COMPLEX, 0 },
446 { "__const", RID_CONST, 0 },
447 { "__const__", RID_CONST, 0 },
448 { "__decltype", RID_DECLTYPE, D_CXXONLY },
449 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
450 { "__extension__", RID_EXTENSION, 0 },
451 { "__func__", RID_C99_FUNCTION_NAME, 0 },
452 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
453 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
454 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
455 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
456 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
457 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
458 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
459 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
460 { "__imag", RID_IMAGPART, 0 },
461 { "__imag__", RID_IMAGPART, 0 },
462 { "__inline", RID_INLINE, 0 },
463 { "__inline__", RID_INLINE, 0 },
464 { "__int128", RID_INT128, 0 },
465 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
466 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
467 { "__is_class", RID_IS_CLASS, D_CXXONLY },
468 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
469 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
470 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
471 { "__is_final", RID_IS_FINAL, D_CXXONLY },
472 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
473 { "__is_pod", RID_IS_POD, D_CXXONLY },
474 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
475 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
476 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
477 { "__is_union", RID_IS_UNION, D_CXXONLY },
478 { "__label__", RID_LABEL, 0 },
479 { "__null", RID_NULL, 0 },
480 { "__real", RID_REALPART, 0 },
481 { "__real__", RID_REALPART, 0 },
482 { "__restrict", RID_RESTRICT, 0 },
483 { "__restrict__", RID_RESTRICT, 0 },
484 { "__signed", RID_SIGNED, 0 },
485 { "__signed__", RID_SIGNED, 0 },
486 { "__thread", RID_THREAD, 0 },
487 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
488 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
489 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
490 { "__typeof", RID_TYPEOF, 0 },
491 { "__typeof__", RID_TYPEOF, 0 },
492 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
493 { "__volatile", RID_VOLATILE, 0 },
494 { "__volatile__", RID_VOLATILE, 0 },
495 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
496 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
497 { "asm", RID_ASM, D_ASM },
498 { "auto", RID_AUTO, 0 },
499 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
500 { "break", RID_BREAK, 0 },
501 { "case", RID_CASE, 0 },
502 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
503 { "char", RID_CHAR, 0 },
504 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
506 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
507 { "const", RID_CONST, 0 },
508 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
509 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
510 { "continue", RID_CONTINUE, 0 },
511 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
512 { "default", RID_DEFAULT, 0 },
513 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
514 { "do", RID_DO, 0 },
515 { "double", RID_DOUBLE, 0 },
516 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
517 { "else", RID_ELSE, 0 },
518 { "enum", RID_ENUM, 0 },
519 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
520 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
521 { "extern", RID_EXTERN, 0 },
522 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
523 { "float", RID_FLOAT, 0 },
524 { "for", RID_FOR, 0 },
525 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
526 { "goto", RID_GOTO, 0 },
527 { "if", RID_IF, 0 },
528 { "inline", RID_INLINE, D_EXT89 },
529 { "int", RID_INT, 0 },
530 { "long", RID_LONG, 0 },
531 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
532 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
533 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
534 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
535 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
537 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
538 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
539 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
540 { "register", RID_REGISTER, 0 },
541 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
542 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
543 { "return", RID_RETURN, 0 },
544 { "short", RID_SHORT, 0 },
545 { "signed", RID_SIGNED, 0 },
546 { "sizeof", RID_SIZEOF, 0 },
547 { "static", RID_STATIC, 0 },
548 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
549 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
550 { "struct", RID_STRUCT, 0 },
551 { "switch", RID_SWITCH, 0 },
552 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
553 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
554 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
555 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
556 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
557 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
558 { "typedef", RID_TYPEDEF, 0 },
559 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
560 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
561 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
562 { "union", RID_UNION, 0 },
563 { "unsigned", RID_UNSIGNED, 0 },
564 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
565 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
566 { "void", RID_VOID, 0 },
567 { "volatile", RID_VOLATILE, 0 },
568 { "wchar_t", RID_WCHAR, D_CXXONLY },
569 { "while", RID_WHILE, 0 },
570 /* These Objective-C keywords are recognized only immediately after
571 an '@'. */
572 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
573 { "defs", RID_AT_DEFS, D_OBJC },
574 { "encode", RID_AT_ENCODE, D_OBJC },
575 { "end", RID_AT_END, D_OBJC },
576 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
577 { "interface", RID_AT_INTERFACE, D_OBJC },
578 { "protocol", RID_AT_PROTOCOL, D_OBJC },
579 { "selector", RID_AT_SELECTOR, D_OBJC },
580 { "finally", RID_AT_FINALLY, D_OBJC },
581 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
582 { "optional", RID_AT_OPTIONAL, D_OBJC },
583 { "required", RID_AT_REQUIRED, D_OBJC },
584 { "property", RID_AT_PROPERTY, D_OBJC },
585 { "package", RID_AT_PACKAGE, D_OBJC },
586 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
587 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
588 /* These are recognized only in protocol-qualifier context
589 (see above) */
590 { "bycopy", RID_BYCOPY, D_OBJC },
591 { "byref", RID_BYREF, D_OBJC },
592 { "in", RID_IN, D_OBJC },
593 { "inout", RID_INOUT, D_OBJC },
594 { "oneway", RID_ONEWAY, D_OBJC },
595 { "out", RID_OUT, D_OBJC },
597 /* UPC keywords */
598 { "shared", RID_SHARED, D_UPC },
599 { "relaxed", RID_RELAXED, D_UPC },
600 { "strict", RID_STRICT, D_UPC },
601 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
602 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
603 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
604 { "upc_forall", RID_UPC_FORALL, D_UPC },
605 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
606 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
607 { "upc_wait", RID_UPC_WAIT, D_UPC },
609 /* These are recognized inside a property attribute list */
610 { "assign", RID_ASSIGN, D_OBJC },
611 { "copy", RID_COPY, D_OBJC },
612 { "getter", RID_GETTER, D_OBJC },
613 { "nonatomic", RID_NONATOMIC, D_OBJC },
614 { "readonly", RID_READONLY, D_OBJC },
615 { "readwrite", RID_READWRITE, D_OBJC },
616 { "retain", RID_RETAIN, D_OBJC },
617 { "setter", RID_SETTER, D_OBJC },
620 const unsigned int num_c_common_reswords =
621 sizeof c_common_reswords / sizeof (struct c_common_resword);
623 /* Table of machine-independent attributes common to all C-like languages. */
624 const struct attribute_spec c_common_attribute_table[] =
626 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
627 affects_type_identity } */
628 { "packed", 0, 0, false, false, false,
629 handle_packed_attribute , false},
630 { "nocommon", 0, 0, true, false, false,
631 handle_nocommon_attribute, false},
632 { "common", 0, 0, true, false, false,
633 handle_common_attribute, false },
634 /* FIXME: logically, noreturn attributes should be listed as
635 "false, true, true" and apply to function types. But implementing this
636 would require all the places in the compiler that use TREE_THIS_VOLATILE
637 on a decl to identify non-returning functions to be located and fixed
638 to check the function type instead. */
639 { "noreturn", 0, 0, true, false, false,
640 handle_noreturn_attribute, false },
641 { "volatile", 0, 0, true, false, false,
642 handle_noreturn_attribute, false },
643 { "noinline", 0, 0, true, false, false,
644 handle_noinline_attribute, false },
645 { "noclone", 0, 0, true, false, false,
646 handle_noclone_attribute, false },
647 { "leaf", 0, 0, true, false, false,
648 handle_leaf_attribute, false },
649 { "always_inline", 0, 0, true, false, false,
650 handle_always_inline_attribute, false },
651 { "gnu_inline", 0, 0, true, false, false,
652 handle_gnu_inline_attribute, false },
653 { "artificial", 0, 0, true, false, false,
654 handle_artificial_attribute, false },
655 { "flatten", 0, 0, true, false, false,
656 handle_flatten_attribute, false },
657 { "used", 0, 0, true, false, false,
658 handle_used_attribute, false },
659 { "unused", 0, 0, false, false, false,
660 handle_unused_attribute, false },
661 { "externally_visible", 0, 0, true, false, false,
662 handle_externally_visible_attribute, false },
663 /* The same comments as for noreturn attributes apply to const ones. */
664 { "const", 0, 0, true, false, false,
665 handle_const_attribute, false },
666 { "transparent_union", 0, 0, false, false, false,
667 handle_transparent_union_attribute, false },
668 { "constructor", 0, 1, true, false, false,
669 handle_constructor_attribute, false },
670 { "destructor", 0, 1, true, false, false,
671 handle_destructor_attribute, false },
672 { "mode", 1, 1, false, true, false,
673 handle_mode_attribute, false },
674 { "section", 1, 1, true, false, false,
675 handle_section_attribute, false },
676 { "aligned", 0, 1, false, false, false,
677 handle_aligned_attribute, false },
678 { "weak", 0, 0, true, false, false,
679 handle_weak_attribute, false },
680 { "ifunc", 1, 1, true, false, false,
681 handle_ifunc_attribute, false },
682 { "alias", 1, 1, true, false, false,
683 handle_alias_attribute, false },
684 { "weakref", 0, 1, true, false, false,
685 handle_weakref_attribute, false },
686 { "no_instrument_function", 0, 0, true, false, false,
687 handle_no_instrument_function_attribute,
688 false },
689 { "malloc", 0, 0, true, false, false,
690 handle_malloc_attribute, false },
691 { "returns_twice", 0, 0, true, false, false,
692 handle_returns_twice_attribute, false },
693 { "no_stack_limit", 0, 0, true, false, false,
694 handle_no_limit_stack_attribute, false },
695 { "pure", 0, 0, true, false, false,
696 handle_pure_attribute, false },
697 { "transaction_callable", 0, 0, false, true, false,
698 handle_tm_attribute, false },
699 { "transaction_unsafe", 0, 0, false, true, false,
700 handle_tm_attribute, false },
701 { "transaction_safe", 0, 0, false, true, false,
702 handle_tm_attribute, false },
703 { "transaction_may_cancel_outer", 0, 0, false, true, false,
704 handle_tm_attribute, false },
705 /* ??? These two attributes didn't make the transition from the
706 Intel language document to the multi-vendor language document. */
707 { "transaction_pure", 0, 0, false, true, false,
708 handle_tm_attribute, false },
709 { "transaction_wrap", 1, 1, true, false, false,
710 handle_tm_wrap_attribute, false },
711 /* For internal use (marking of builtins) only. The name contains space
712 to prevent its usage in source code. */
713 { "no vops", 0, 0, true, false, false,
714 handle_novops_attribute, false },
715 { "deprecated", 0, 1, false, false, false,
716 handle_deprecated_attribute, false },
717 { "vector_size", 1, 1, false, true, false,
718 handle_vector_size_attribute, false },
719 { "visibility", 1, 1, false, false, false,
720 handle_visibility_attribute, false },
721 { "tls_model", 1, 1, true, false, false,
722 handle_tls_model_attribute, false },
723 { "nonnull", 0, -1, false, true, true,
724 handle_nonnull_attribute, false },
725 { "nothrow", 0, 0, true, false, false,
726 handle_nothrow_attribute, false },
727 { "may_alias", 0, 0, false, true, false, NULL, false },
728 { "cleanup", 1, 1, true, false, false,
729 handle_cleanup_attribute, false },
730 { "warn_unused_result", 0, 0, false, true, true,
731 handle_warn_unused_result_attribute, false },
732 { "sentinel", 0, 1, false, true, true,
733 handle_sentinel_attribute, false },
734 /* For internal use (marking of builtins) only. The name contains space
735 to prevent its usage in source code. */
736 { "type generic", 0, 0, false, true, true,
737 handle_type_generic_attribute, false },
738 { "alloc_size", 1, 2, false, true, true,
739 handle_alloc_size_attribute, false },
740 { "cold", 0, 0, true, false, false,
741 handle_cold_attribute, false },
742 { "hot", 0, 0, true, false, false,
743 handle_hot_attribute, false },
744 { "no_address_safety_analysis",
745 0, 0, true, false, false,
746 handle_no_address_safety_analysis_attribute,
747 false },
748 { "warning", 1, 1, true, false, false,
749 handle_error_attribute, false },
750 { "error", 1, 1, true, false, false,
751 handle_error_attribute, false },
752 { "target", 1, -1, true, false, false,
753 handle_target_attribute, false },
754 { "optimize", 1, -1, true, false, false,
755 handle_optimize_attribute, false },
756 /* For internal use only. The leading '*' both prevents its usage in
757 source code and signals that it may be overridden by machine tables. */
758 { "*tm regparm", 0, 0, false, true, true,
759 ignore_attribute, false },
760 { "no_split_stack", 0, 0, true, false, false,
761 handle_no_split_stack_attribute, false },
762 /* For internal use (marking of builtins and runtime functions) only.
763 The name contains space to prevent its usage in source code. */
764 { "fn spec", 1, 1, false, true, true,
765 handle_fnspec_attribute, false },
766 { NULL, 0, 0, false, false, false, NULL, false }
769 /* Give the specifications for the format attributes, used by C and all
770 descendants. */
772 const struct attribute_spec c_common_format_attribute_table[] =
774 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
775 affects_type_identity } */
776 { "format", 3, 3, false, true, true,
777 handle_format_attribute, false },
778 { "format_arg", 1, 1, false, true, true,
779 handle_format_arg_attribute, false },
780 { NULL, 0, 0, false, false, false, NULL, false }
783 /* Return identifier for address space AS. */
785 const char *
786 c_addr_space_name (addr_space_t as)
788 int rid = RID_FIRST_ADDR_SPACE + as;
789 gcc_assert (ridpointers [rid]);
790 return IDENTIFIER_POINTER (ridpointers [rid]);
793 /* Push current bindings for the function name VAR_DECLS. */
795 void
796 start_fname_decls (void)
798 unsigned ix;
799 tree saved = NULL_TREE;
801 for (ix = 0; fname_vars[ix].decl; ix++)
803 tree decl = *fname_vars[ix].decl;
805 if (decl)
807 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
808 saved);
809 *fname_vars[ix].decl = NULL_TREE;
812 if (saved || saved_function_name_decls)
813 /* Normally they'll have been NULL, so only push if we've got a
814 stack, or they are non-NULL. */
815 saved_function_name_decls = tree_cons (saved, NULL_TREE,
816 saved_function_name_decls);
819 /* Finish up the current bindings, adding them into the current function's
820 statement tree. This must be done _before_ finish_stmt_tree is called.
821 If there is no current function, we must be at file scope and no statements
822 are involved. Pop the previous bindings. */
824 void
825 finish_fname_decls (void)
827 unsigned ix;
828 tree stmts = NULL_TREE;
829 tree stack = saved_function_name_decls;
831 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
832 append_to_statement_list (TREE_VALUE (stack), &stmts);
834 if (stmts)
836 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
838 if (TREE_CODE (*bodyp) == BIND_EXPR)
839 bodyp = &BIND_EXPR_BODY (*bodyp);
841 append_to_statement_list_force (*bodyp, &stmts);
842 *bodyp = stmts;
845 for (ix = 0; fname_vars[ix].decl; ix++)
846 *fname_vars[ix].decl = NULL_TREE;
848 if (stack)
850 /* We had saved values, restore them. */
851 tree saved;
853 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
855 tree decl = TREE_PURPOSE (saved);
856 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
858 *fname_vars[ix].decl = decl;
860 stack = TREE_CHAIN (stack);
862 saved_function_name_decls = stack;
865 /* Return the text name of the current function, suitably prettified
866 by PRETTY_P. Return string must be freed by caller. */
868 const char *
869 fname_as_string (int pretty_p)
871 const char *name = "top level";
872 char *namep;
873 int vrb = 2, len;
874 cpp_string cstr = { 0, 0 }, strname;
876 if (!pretty_p)
878 name = "";
879 vrb = 0;
882 if (current_function_decl)
883 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
885 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
887 namep = XNEWVEC (char, len);
888 snprintf (namep, len, "\"%s\"", name);
889 strname.text = (unsigned char *) namep;
890 strname.len = len - 1;
892 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
894 XDELETEVEC (namep);
895 return (const char *) cstr.text;
898 return namep;
901 /* Return the VAR_DECL for a const char array naming the current
902 function. If the VAR_DECL has not yet been created, create it
903 now. RID indicates how it should be formatted and IDENTIFIER_NODE
904 ID is its name (unfortunately C and C++ hold the RID values of
905 keywords in different places, so we can't derive RID from ID in
906 this language independent code. LOC is the location of the
907 function. */
909 tree
910 fname_decl (location_t loc, unsigned int rid, tree id)
912 unsigned ix;
913 tree decl = NULL_TREE;
915 for (ix = 0; fname_vars[ix].decl; ix++)
916 if (fname_vars[ix].rid == rid)
917 break;
919 decl = *fname_vars[ix].decl;
920 if (!decl)
922 /* If a tree is built here, it would normally have the lineno of
923 the current statement. Later this tree will be moved to the
924 beginning of the function and this line number will be wrong.
925 To avoid this problem set the lineno to 0 here; that prevents
926 it from appearing in the RTL. */
927 tree stmts;
928 location_t saved_location = input_location;
929 input_location = UNKNOWN_LOCATION;
931 stmts = push_stmt_list ();
932 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
933 stmts = pop_stmt_list (stmts);
934 if (!IS_EMPTY_STMT (stmts))
935 saved_function_name_decls
936 = tree_cons (decl, stmts, saved_function_name_decls);
937 *fname_vars[ix].decl = decl;
938 input_location = saved_location;
940 if (!ix && !current_function_decl)
941 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
943 return decl;
946 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
948 tree
949 fix_string_type (tree value)
951 int length = TREE_STRING_LENGTH (value);
952 int nchars;
953 tree e_type, i_type, a_type;
955 /* Compute the number of elements, for the array type. */
956 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
958 nchars = length;
959 e_type = char_type_node;
961 else if (TREE_TYPE (value) == char16_array_type_node)
963 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
964 e_type = char16_type_node;
966 else if (TREE_TYPE (value) == char32_array_type_node)
968 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
969 e_type = char32_type_node;
971 else
973 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
974 e_type = wchar_type_node;
977 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
978 limit in C++98 Annex B is very large (65536) and is not normative,
979 so we do not diagnose it (warn_overlength_strings is forced off
980 in c_common_post_options). */
981 if (warn_overlength_strings)
983 const int nchars_max = flag_isoc99 ? 4095 : 509;
984 const int relevant_std = flag_isoc99 ? 99 : 90;
985 if (nchars - 1 > nchars_max)
986 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
987 separate the %d from the 'C'. 'ISO' should not be
988 translated, but it may be moved after 'C%d' in languages
989 where modifiers follow nouns. */
990 pedwarn (input_location, OPT_Woverlength_strings,
991 "string length %qd is greater than the length %qd "
992 "ISO C%d compilers are required to support",
993 nchars - 1, nchars_max, relevant_std);
996 /* Create the array type for the string constant. The ISO C++
997 standard says that a string literal has type `const char[N]' or
998 `const wchar_t[N]'. We use the same logic when invoked as a C
999 front-end with -Wwrite-strings.
1000 ??? We should change the type of an expression depending on the
1001 state of a warning flag. We should just be warning -- see how
1002 this is handled in the C++ front-end for the deprecated implicit
1003 conversion from string literals to `char*' or `wchar_t*'.
1005 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1006 array type being the unqualified version of that type.
1007 Therefore, if we are constructing an array of const char, we must
1008 construct the matching unqualified array type first. The C front
1009 end does not require this, but it does no harm, so we do it
1010 unconditionally. */
1011 i_type = build_index_type (size_int (nchars - 1));
1012 a_type = build_array_type (e_type, i_type);
1013 if (c_dialect_cxx() || warn_write_strings)
1014 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1016 TREE_TYPE (value) = a_type;
1017 TREE_CONSTANT (value) = 1;
1018 TREE_READONLY (value) = 1;
1019 TREE_STATIC (value) = 1;
1020 return value;
1023 /* If DISABLE is true, stop issuing warnings. This is used when
1024 parsing code that we know will not be executed. This function may
1025 be called multiple times, and works as a stack. */
1027 static void
1028 c_disable_warnings (bool disable)
1030 if (disable)
1032 ++c_inhibit_evaluation_warnings;
1033 fold_defer_overflow_warnings ();
1037 /* If ENABLE is true, reenable issuing warnings. */
1039 static void
1040 c_enable_warnings (bool enable)
1042 if (enable)
1044 --c_inhibit_evaluation_warnings;
1045 fold_undefer_and_ignore_overflow_warnings ();
1049 /* Fully fold EXPR, an expression that was not folded (beyond integer
1050 constant expressions and null pointer constants) when being built
1051 up. If IN_INIT, this is in a static initializer and certain
1052 changes are made to the folding done. Clear *MAYBE_CONST if
1053 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1054 expression because it contains an evaluated operator (in C99) or an
1055 operator outside of sizeof returning an integer constant (in C90)
1056 not permitted in constant expressions, or because it contains an
1057 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1058 set to true by callers before calling this function.) Return the
1059 folded expression. Function arguments have already been folded
1060 before calling this function, as have the contents of SAVE_EXPR,
1061 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1062 C_MAYBE_CONST_EXPR. */
1064 tree
1065 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1067 tree ret;
1068 tree eptype = NULL_TREE;
1069 bool dummy = true;
1070 bool maybe_const_itself = true;
1071 location_t loc = EXPR_LOCATION (expr);
1073 /* This function is not relevant to C++ because C++ folds while
1074 parsing, and may need changes to be correct for C++ when C++
1075 stops folding while parsing. */
1076 if (c_dialect_cxx ())
1077 gcc_unreachable ();
1079 if (!maybe_const)
1080 maybe_const = &dummy;
1081 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1083 eptype = TREE_TYPE (expr);
1084 expr = TREE_OPERAND (expr, 0);
1086 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1087 &maybe_const_itself);
1088 if (eptype)
1089 ret = fold_convert_loc (loc, eptype, ret);
1090 *maybe_const &= maybe_const_itself;
1091 return ret;
1094 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1095 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1096 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1097 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1098 both evaluated and unevaluated subexpressions while
1099 *MAYBE_CONST_ITSELF is carried from only evaluated
1100 subexpressions). */
1102 static tree
1103 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1104 bool *maybe_const_itself)
1106 tree ret = expr;
1107 enum tree_code code = TREE_CODE (expr);
1108 enum tree_code_class kind = TREE_CODE_CLASS (code);
1109 location_t loc = EXPR_LOCATION (expr);
1110 tree op0, op1, op2, op3;
1111 tree orig_op0, orig_op1, orig_op2;
1112 bool op0_const = true, op1_const = true, op2_const = true;
1113 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1114 bool nowarning = TREE_NO_WARNING (expr);
1115 bool unused_p;
1117 /* This function is not relevant to C++ because C++ folds while
1118 parsing, and may need changes to be correct for C++ when C++
1119 stops folding while parsing. */
1120 if (c_dialect_cxx ())
1121 gcc_unreachable ();
1123 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1124 anything else not counted as an expression cannot usefully be
1125 folded further at this point. */
1126 if (!IS_EXPR_CODE_CLASS (kind)
1127 || kind == tcc_statement
1128 || code == SAVE_EXPR)
1129 return expr;
1131 /* Operands of variable-length expressions (function calls) have
1132 already been folded, as have __builtin_* function calls, and such
1133 expressions cannot occur in constant expressions. */
1134 if (kind == tcc_vl_exp)
1136 *maybe_const_operands = false;
1137 ret = fold (expr);
1138 goto out;
1141 if (code == C_MAYBE_CONST_EXPR)
1143 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1144 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1145 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1146 *maybe_const_operands = false;
1147 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1148 *maybe_const_itself = false;
1149 if (pre && !in_init)
1150 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1151 else
1152 ret = inner;
1153 goto out;
1156 /* Assignment, increment, decrement, function call and comma
1157 operators, and statement expressions, cannot occur in constant
1158 expressions if evaluated / outside of sizeof. (Function calls
1159 were handled above, though VA_ARG_EXPR is treated like a function
1160 call here, and statement expressions are handled through
1161 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1162 switch (code)
1164 case MODIFY_EXPR:
1165 case PREDECREMENT_EXPR:
1166 case PREINCREMENT_EXPR:
1167 case POSTDECREMENT_EXPR:
1168 case POSTINCREMENT_EXPR:
1169 case COMPOUND_EXPR:
1170 *maybe_const_operands = false;
1171 break;
1173 case VA_ARG_EXPR:
1174 case TARGET_EXPR:
1175 case BIND_EXPR:
1176 case OBJ_TYPE_REF:
1177 *maybe_const_operands = false;
1178 ret = fold (expr);
1179 goto out;
1181 default:
1182 break;
1185 /* Fold individual tree codes as appropriate. */
1186 switch (code)
1188 case COMPOUND_LITERAL_EXPR:
1189 /* Any non-constancy will have been marked in a containing
1190 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1191 goto out;
1193 case COMPONENT_REF:
1194 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1195 op1 = TREE_OPERAND (expr, 1);
1196 op2 = TREE_OPERAND (expr, 2);
1197 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1198 maybe_const_itself);
1199 STRIP_TYPE_NOPS (op0);
1200 if (op0 != orig_op0)
1201 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1202 if (ret != expr)
1204 TREE_READONLY (ret) = TREE_READONLY (expr);
1205 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1207 goto out;
1209 case ARRAY_REF:
1210 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1211 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1212 op2 = TREE_OPERAND (expr, 2);
1213 op3 = TREE_OPERAND (expr, 3);
1214 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1215 maybe_const_itself);
1216 STRIP_TYPE_NOPS (op0);
1217 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1218 maybe_const_itself);
1219 STRIP_TYPE_NOPS (op1);
1220 op1 = decl_constant_value_for_optimization (op1);
1221 if (op0 != orig_op0 || op1 != orig_op1)
1222 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1223 if (ret != expr)
1225 TREE_READONLY (ret) = TREE_READONLY (expr);
1226 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1227 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1229 ret = fold (ret);
1230 goto out;
1232 case COMPOUND_EXPR:
1233 case MODIFY_EXPR:
1234 case PREDECREMENT_EXPR:
1235 case PREINCREMENT_EXPR:
1236 case POSTDECREMENT_EXPR:
1237 case POSTINCREMENT_EXPR:
1238 case PLUS_EXPR:
1239 case MINUS_EXPR:
1240 case MULT_EXPR:
1241 case POINTER_PLUS_EXPR:
1242 case TRUNC_DIV_EXPR:
1243 case CEIL_DIV_EXPR:
1244 case FLOOR_DIV_EXPR:
1245 case TRUNC_MOD_EXPR:
1246 case RDIV_EXPR:
1247 case EXACT_DIV_EXPR:
1248 case LSHIFT_EXPR:
1249 case RSHIFT_EXPR:
1250 case BIT_IOR_EXPR:
1251 case BIT_XOR_EXPR:
1252 case BIT_AND_EXPR:
1253 case LT_EXPR:
1254 case LE_EXPR:
1255 case GT_EXPR:
1256 case GE_EXPR:
1257 case EQ_EXPR:
1258 case NE_EXPR:
1259 case COMPLEX_EXPR:
1260 case TRUTH_AND_EXPR:
1261 case TRUTH_OR_EXPR:
1262 case TRUTH_XOR_EXPR:
1263 case UNORDERED_EXPR:
1264 case ORDERED_EXPR:
1265 case UNLT_EXPR:
1266 case UNLE_EXPR:
1267 case UNGT_EXPR:
1268 case UNGE_EXPR:
1269 case UNEQ_EXPR:
1270 /* Binary operations evaluating both arguments (increment and
1271 decrement are binary internally in GCC). */
1272 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1273 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1274 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1275 maybe_const_itself);
1276 STRIP_TYPE_NOPS (op0);
1277 if (code != MODIFY_EXPR
1278 && code != PREDECREMENT_EXPR
1279 && code != PREINCREMENT_EXPR
1280 && code != POSTDECREMENT_EXPR
1281 && code != POSTINCREMENT_EXPR)
1282 op0 = decl_constant_value_for_optimization (op0);
1283 /* The RHS of a MODIFY_EXPR was fully folded when building that
1284 expression for the sake of conversion warnings. */
1285 if (code != MODIFY_EXPR)
1286 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1287 maybe_const_itself);
1288 STRIP_TYPE_NOPS (op1);
1289 op1 = decl_constant_value_for_optimization (op1);
1290 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1291 ret = in_init
1292 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1293 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1294 else
1295 ret = fold (expr);
1296 if (TREE_OVERFLOW_P (ret)
1297 && !TREE_OVERFLOW_P (op0)
1298 && !TREE_OVERFLOW_P (op1))
1299 overflow_warning (EXPR_LOCATION (expr), ret);
1300 goto out;
1302 case INDIRECT_REF:
1303 case FIX_TRUNC_EXPR:
1304 case FLOAT_EXPR:
1305 CASE_CONVERT:
1306 case VIEW_CONVERT_EXPR:
1307 case NON_LVALUE_EXPR:
1308 case NEGATE_EXPR:
1309 case BIT_NOT_EXPR:
1310 case TRUTH_NOT_EXPR:
1311 case ADDR_EXPR:
1312 case CONJ_EXPR:
1313 case REALPART_EXPR:
1314 case IMAGPART_EXPR:
1315 /* Unary operations. */
1316 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1317 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1318 maybe_const_itself);
1319 STRIP_TYPE_NOPS (op0);
1320 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1321 op0 = decl_constant_value_for_optimization (op0);
1322 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1323 not prepared to deal with them if they occur in initializers.
1324 Avoid attempts to fold references to UPC shared components
1325 due to the complexities of UPC pointer-to-shared arithmetic. */
1326 if (op0 != orig_op0
1327 && code == ADDR_EXPR
1328 && (op1 = get_base_address (op0)) != NULL_TREE
1329 && TREE_CODE (op1) == INDIRECT_REF
1330 && !upc_shared_type_p (TREE_TYPE (op1))
1331 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1332 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1333 else if (op0 != orig_op0 || in_init)
1334 ret = in_init
1335 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1336 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1337 else
1338 ret = fold (expr);
1339 if (code == INDIRECT_REF
1340 && ret != expr
1341 && TREE_CODE (ret) == INDIRECT_REF)
1343 TREE_READONLY (ret) = TREE_READONLY (expr);
1344 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1345 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1347 switch (code)
1349 case FIX_TRUNC_EXPR:
1350 case FLOAT_EXPR:
1351 CASE_CONVERT:
1352 /* Don't warn about explicit conversions. We will already
1353 have warned about suspect implicit conversions. */
1354 break;
1356 default:
1357 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1358 overflow_warning (EXPR_LOCATION (expr), ret);
1359 break;
1361 goto out;
1363 case TRUTH_ANDIF_EXPR:
1364 case TRUTH_ORIF_EXPR:
1365 /* Binary operations not necessarily evaluating both
1366 arguments. */
1367 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1368 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1369 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1370 STRIP_TYPE_NOPS (op0);
1372 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1373 ? truthvalue_false_node
1374 : truthvalue_true_node));
1375 c_disable_warnings (unused_p);
1376 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1377 STRIP_TYPE_NOPS (op1);
1378 c_enable_warnings (unused_p);
1380 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1381 ret = in_init
1382 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1383 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1384 else
1385 ret = fold (expr);
1386 *maybe_const_operands &= op0_const;
1387 *maybe_const_itself &= op0_const_self;
1388 if (!(flag_isoc99
1389 && op0_const
1390 && op0_const_self
1391 && (code == TRUTH_ANDIF_EXPR
1392 ? op0 == truthvalue_false_node
1393 : op0 == truthvalue_true_node)))
1394 *maybe_const_operands &= op1_const;
1395 if (!(op0_const
1396 && op0_const_self
1397 && (code == TRUTH_ANDIF_EXPR
1398 ? op0 == truthvalue_false_node
1399 : op0 == truthvalue_true_node)))
1400 *maybe_const_itself &= op1_const_self;
1401 goto out;
1403 case COND_EXPR:
1404 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1405 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1406 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1407 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1409 STRIP_TYPE_NOPS (op0);
1410 c_disable_warnings (op0 == truthvalue_false_node);
1411 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1412 STRIP_TYPE_NOPS (op1);
1413 c_enable_warnings (op0 == truthvalue_false_node);
1415 c_disable_warnings (op0 == truthvalue_true_node);
1416 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1417 STRIP_TYPE_NOPS (op2);
1418 c_enable_warnings (op0 == truthvalue_true_node);
1420 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1421 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1422 else
1423 ret = fold (expr);
1424 *maybe_const_operands &= op0_const;
1425 *maybe_const_itself &= op0_const_self;
1426 if (!(flag_isoc99
1427 && op0_const
1428 && op0_const_self
1429 && op0 == truthvalue_false_node))
1430 *maybe_const_operands &= op1_const;
1431 if (!(op0_const
1432 && op0_const_self
1433 && op0 == truthvalue_false_node))
1434 *maybe_const_itself &= op1_const_self;
1435 if (!(flag_isoc99
1436 && op0_const
1437 && op0_const_self
1438 && op0 == truthvalue_true_node))
1439 *maybe_const_operands &= op2_const;
1440 if (!(op0_const
1441 && op0_const_self
1442 && op0 == truthvalue_true_node))
1443 *maybe_const_itself &= op2_const_self;
1444 goto out;
1446 case EXCESS_PRECISION_EXPR:
1447 /* Each case where an operand with excess precision may be
1448 encountered must remove the EXCESS_PRECISION_EXPR around
1449 inner operands and possibly put one around the whole
1450 expression or possibly convert to the semantic type (which
1451 c_fully_fold does); we cannot tell at this stage which is
1452 appropriate in any particular case. */
1453 gcc_unreachable ();
1455 default:
1456 /* Various codes may appear through folding built-in functions
1457 and their arguments. */
1458 goto out;
1461 out:
1462 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1463 have been done by this point, so remove them again. */
1464 nowarning |= TREE_NO_WARNING (ret);
1465 STRIP_TYPE_NOPS (ret);
1466 if (nowarning && !TREE_NO_WARNING (ret))
1468 if (!CAN_HAVE_LOCATION_P (ret))
1469 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1470 TREE_NO_WARNING (ret) = 1;
1472 if (ret != expr)
1473 protected_set_expr_location (ret, loc);
1474 return ret;
1477 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1478 return EXP. Otherwise, return either EXP or its known constant
1479 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1480 Is the BLKmode test appropriate? */
1482 tree
1483 decl_constant_value_for_optimization (tree exp)
1485 tree ret;
1487 /* This function is only used by C, for c_fully_fold and other
1488 optimization, and may not be correct for C++. */
1489 if (c_dialect_cxx ())
1490 gcc_unreachable ();
1492 if (!optimize
1493 || TREE_CODE (exp) != VAR_DECL
1494 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1495 || DECL_MODE (exp) == BLKmode)
1496 return exp;
1498 ret = decl_constant_value (exp);
1499 /* Avoid unwanted tree sharing between the initializer and current
1500 function's body where the tree can be modified e.g. by the
1501 gimplifier. */
1502 if (ret != exp && TREE_STATIC (exp))
1503 ret = unshare_expr (ret);
1504 return ret;
1507 /* Print a warning if a constant expression had overflow in folding.
1508 Invoke this function on every expression that the language
1509 requires to be a constant expression.
1510 Note the ANSI C standard says it is erroneous for a
1511 constant expression to overflow. */
1513 void
1514 constant_expression_warning (tree value)
1516 if (warn_overflow && pedantic
1517 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1518 || TREE_CODE (value) == FIXED_CST
1519 || TREE_CODE (value) == VECTOR_CST
1520 || TREE_CODE (value) == COMPLEX_CST)
1521 && TREE_OVERFLOW (value))
1522 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1525 /* The same as above but print an unconditional error. */
1526 void
1527 constant_expression_error (tree value)
1529 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1530 || TREE_CODE (value) == FIXED_CST
1531 || TREE_CODE (value) == VECTOR_CST
1532 || TREE_CODE (value) == COMPLEX_CST)
1533 && TREE_OVERFLOW (value))
1534 error ("overflow in constant expression");
1537 /* Print a warning if an expression had overflow in folding and its
1538 operands hadn't.
1540 Invoke this function on every expression that
1541 (1) appears in the source code, and
1542 (2) is a constant expression that overflowed, and
1543 (3) is not already checked by convert_and_check;
1544 however, do not invoke this function on operands of explicit casts
1545 or when the expression is the result of an operator and any operand
1546 already overflowed. */
1548 void
1549 overflow_warning (location_t loc, tree value)
1551 if (c_inhibit_evaluation_warnings != 0)
1552 return;
1554 switch (TREE_CODE (value))
1556 case INTEGER_CST:
1557 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1558 break;
1560 case REAL_CST:
1561 warning_at (loc, OPT_Woverflow,
1562 "floating point overflow in expression");
1563 break;
1565 case FIXED_CST:
1566 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1567 break;
1569 case VECTOR_CST:
1570 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1571 break;
1573 case COMPLEX_CST:
1574 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1575 warning_at (loc, OPT_Woverflow,
1576 "complex integer overflow in expression");
1577 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1578 warning_at (loc, OPT_Woverflow,
1579 "complex floating point overflow in expression");
1580 break;
1582 default:
1583 break;
1587 /* Warn about uses of logical || / && operator in a context where it
1588 is likely that the bitwise equivalent was intended by the
1589 programmer. We have seen an expression in which CODE is a binary
1590 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1591 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1592 void
1593 warn_logical_operator (location_t location, enum tree_code code, tree type,
1594 enum tree_code code_left, tree op_left,
1595 enum tree_code ARG_UNUSED (code_right), tree op_right)
1597 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1598 int in0_p, in1_p, in_p;
1599 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1600 bool strict_overflow_p = false;
1602 if (code != TRUTH_ANDIF_EXPR
1603 && code != TRUTH_AND_EXPR
1604 && code != TRUTH_ORIF_EXPR
1605 && code != TRUTH_OR_EXPR)
1606 return;
1608 /* Warn if &&/|| are being used in a context where it is
1609 likely that the bitwise equivalent was intended by the
1610 programmer. That is, an expression such as op && MASK
1611 where op should not be any boolean expression, nor a
1612 constant, and mask seems to be a non-boolean integer constant. */
1613 if (!truth_value_p (code_left)
1614 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1615 && !CONSTANT_CLASS_P (op_left)
1616 && !TREE_NO_WARNING (op_left)
1617 && TREE_CODE (op_right) == INTEGER_CST
1618 && !integer_zerop (op_right)
1619 && !integer_onep (op_right))
1621 if (or_op)
1622 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1623 " applied to non-boolean constant");
1624 else
1625 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1626 " applied to non-boolean constant");
1627 TREE_NO_WARNING (op_left) = true;
1628 return;
1631 /* We do not warn for constants because they are typical of macro
1632 expansions that test for features. */
1633 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1634 return;
1636 /* This warning only makes sense with logical operands. */
1637 if (!(truth_value_p (TREE_CODE (op_left))
1638 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1639 || !(truth_value_p (TREE_CODE (op_right))
1640 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1641 return;
1644 /* We first test whether either side separately is trivially true
1645 (with OR) or trivially false (with AND). If so, do not warn.
1646 This is a common idiom for testing ranges of data types in
1647 portable code. */
1648 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1649 if (!lhs)
1650 return;
1651 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1652 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1654 /* If this is an OR operation, invert both sides; now, the result
1655 should be always false to get a warning. */
1656 if (or_op)
1657 in0_p = !in0_p;
1659 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1660 if (tem && integer_zerop (tem))
1661 return;
1663 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1664 if (!rhs)
1665 return;
1666 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1667 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1669 /* If this is an OR operation, invert both sides; now, the result
1670 should be always false to get a warning. */
1671 if (or_op)
1672 in1_p = !in1_p;
1674 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1675 if (tem && integer_zerop (tem))
1676 return;
1678 /* If both expressions have the same operand, if we can merge the
1679 ranges, and if the range test is always false, then warn. */
1680 if (operand_equal_p (lhs, rhs, 0)
1681 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1682 in1_p, low1, high1)
1683 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1684 type, lhs, in_p, low, high))
1685 && integer_zerop (tem))
1687 if (or_op)
1688 warning_at (location, OPT_Wlogical_op,
1689 "logical %<or%> "
1690 "of collectively exhaustive tests is always true");
1691 else
1692 warning_at (location, OPT_Wlogical_op,
1693 "logical %<and%> "
1694 "of mutually exclusive tests is always false");
1699 /* Warn if EXP contains any computations whose results are not used.
1700 Return true if a warning is printed; false otherwise. LOCUS is the
1701 (potential) location of the expression. */
1703 bool
1704 warn_if_unused_value (const_tree exp, location_t locus)
1706 restart:
1707 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1708 return false;
1710 /* Don't warn about void constructs. This includes casting to void,
1711 void function calls, and statement expressions with a final cast
1712 to void. */
1713 if (VOID_TYPE_P (TREE_TYPE (exp)))
1714 return false;
1716 if (EXPR_HAS_LOCATION (exp))
1717 locus = EXPR_LOCATION (exp);
1719 switch (TREE_CODE (exp))
1721 case PREINCREMENT_EXPR:
1722 case POSTINCREMENT_EXPR:
1723 case PREDECREMENT_EXPR:
1724 case POSTDECREMENT_EXPR:
1725 case MODIFY_EXPR:
1726 case INIT_EXPR:
1727 case TARGET_EXPR:
1728 case CALL_EXPR:
1729 case TRY_CATCH_EXPR:
1730 case WITH_CLEANUP_EXPR:
1731 case EXIT_EXPR:
1732 case VA_ARG_EXPR:
1733 return false;
1735 case BIND_EXPR:
1736 /* For a binding, warn if no side effect within it. */
1737 exp = BIND_EXPR_BODY (exp);
1738 goto restart;
1740 case SAVE_EXPR:
1741 case NON_LVALUE_EXPR:
1742 case NOP_EXPR:
1743 exp = TREE_OPERAND (exp, 0);
1744 goto restart;
1746 case TRUTH_ORIF_EXPR:
1747 case TRUTH_ANDIF_EXPR:
1748 /* In && or ||, warn if 2nd operand has no side effect. */
1749 exp = TREE_OPERAND (exp, 1);
1750 goto restart;
1752 case COMPOUND_EXPR:
1753 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1754 return true;
1755 /* Let people do `(foo (), 0)' without a warning. */
1756 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1757 return false;
1758 exp = TREE_OPERAND (exp, 1);
1759 goto restart;
1761 case COND_EXPR:
1762 /* If this is an expression with side effects, don't warn; this
1763 case commonly appears in macro expansions. */
1764 if (TREE_SIDE_EFFECTS (exp))
1765 return false;
1766 goto warn;
1768 case INDIRECT_REF:
1769 /* Don't warn about automatic dereferencing of references, since
1770 the user cannot control it. */
1771 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1773 exp = TREE_OPERAND (exp, 0);
1774 goto restart;
1776 /* Fall through. */
1778 default:
1779 /* Referencing a volatile value is a side effect, so don't warn. */
1780 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1781 && TREE_THIS_VOLATILE (exp))
1782 return false;
1784 /* If this is an expression which has no operands, there is no value
1785 to be unused. There are no such language-independent codes,
1786 but front ends may define such. */
1787 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1788 return false;
1790 warn:
1791 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1796 /* Print a warning about casts that might indicate violation
1797 of strict aliasing rules if -Wstrict-aliasing is used and
1798 strict aliasing mode is in effect. OTYPE is the original
1799 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1801 bool
1802 strict_aliasing_warning (tree otype, tree type, tree expr)
1804 /* Strip pointer conversion chains and get to the correct original type. */
1805 STRIP_NOPS (expr);
1806 otype = TREE_TYPE (expr);
1808 if (!(flag_strict_aliasing
1809 && POINTER_TYPE_P (type)
1810 && POINTER_TYPE_P (otype)
1811 && !VOID_TYPE_P (TREE_TYPE (type)))
1812 /* If the type we are casting to is a ref-all pointer
1813 dereferencing it is always valid. */
1814 || TYPE_REF_CAN_ALIAS_ALL (type))
1815 return false;
1817 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1818 && (DECL_P (TREE_OPERAND (expr, 0))
1819 || handled_component_p (TREE_OPERAND (expr, 0))))
1821 /* Casting the address of an object to non void pointer. Warn
1822 if the cast breaks type based aliasing. */
1823 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1825 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1826 "might break strict-aliasing rules");
1827 return true;
1829 else
1831 /* warn_strict_aliasing >= 3. This includes the default (3).
1832 Only warn if the cast is dereferenced immediately. */
1833 alias_set_type set1 =
1834 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1835 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1837 if (set1 != set2 && set2 != 0
1838 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1840 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1841 "pointer will break strict-aliasing rules");
1842 return true;
1844 else if (warn_strict_aliasing == 2
1845 && !alias_sets_must_conflict_p (set1, set2))
1847 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1848 "pointer might break strict-aliasing rules");
1849 return true;
1853 else
1854 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1856 /* At this level, warn for any conversions, even if an address is
1857 not taken in the same statement. This will likely produce many
1858 false positives, but could be useful to pinpoint problems that
1859 are not revealed at higher levels. */
1860 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1861 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1862 if (!COMPLETE_TYPE_P (type)
1863 || !alias_sets_must_conflict_p (set1, set2))
1865 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1866 "pointer might break strict-aliasing rules");
1867 return true;
1871 return false;
1874 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1875 sizeof as last operand of certain builtins. */
1877 void
1878 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1879 vec<tree, va_gc> *params, tree *sizeof_arg,
1880 bool (*comp_types) (tree, tree))
1882 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1883 bool strop = false, cmp = false;
1884 unsigned int idx = ~0;
1885 location_t loc;
1887 if (TREE_CODE (callee) != FUNCTION_DECL
1888 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1889 || vec_safe_length (params) <= 1)
1890 return;
1892 switch (DECL_FUNCTION_CODE (callee))
1894 case BUILT_IN_STRNCMP:
1895 case BUILT_IN_STRNCASECMP:
1896 cmp = true;
1897 /* FALLTHRU */
1898 case BUILT_IN_STRNCPY:
1899 case BUILT_IN_STRNCPY_CHK:
1900 case BUILT_IN_STRNCAT:
1901 case BUILT_IN_STRNCAT_CHK:
1902 case BUILT_IN_STPNCPY:
1903 case BUILT_IN_STPNCPY_CHK:
1904 strop = true;
1905 /* FALLTHRU */
1906 case BUILT_IN_MEMCPY:
1907 case BUILT_IN_MEMCPY_CHK:
1908 case BUILT_IN_MEMMOVE:
1909 case BUILT_IN_MEMMOVE_CHK:
1910 if (params->length () < 3)
1911 return;
1912 src = (*params)[1];
1913 dest = (*params)[0];
1914 idx = 2;
1915 break;
1916 case BUILT_IN_BCOPY:
1917 if (params->length () < 3)
1918 return;
1919 src = (*params)[0];
1920 dest = (*params)[1];
1921 idx = 2;
1922 break;
1923 case BUILT_IN_MEMCMP:
1924 case BUILT_IN_BCMP:
1925 if (params->length () < 3)
1926 return;
1927 src = (*params)[1];
1928 dest = (*params)[0];
1929 idx = 2;
1930 cmp = true;
1931 break;
1932 case BUILT_IN_MEMSET:
1933 case BUILT_IN_MEMSET_CHK:
1934 if (params->length () < 3)
1935 return;
1936 dest = (*params)[0];
1937 idx = 2;
1938 break;
1939 case BUILT_IN_BZERO:
1940 dest = (*params)[0];
1941 idx = 1;
1942 break;
1943 case BUILT_IN_STRNDUP:
1944 src = (*params)[0];
1945 strop = true;
1946 idx = 1;
1947 break;
1948 case BUILT_IN_MEMCHR:
1949 if (params->length () < 3)
1950 return;
1951 src = (*params)[0];
1952 idx = 2;
1953 break;
1954 case BUILT_IN_SNPRINTF:
1955 case BUILT_IN_SNPRINTF_CHK:
1956 case BUILT_IN_VSNPRINTF:
1957 case BUILT_IN_VSNPRINTF_CHK:
1958 dest = (*params)[0];
1959 idx = 1;
1960 strop = true;
1961 break;
1962 default:
1963 break;
1966 if (idx >= 3)
1967 return;
1969 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1970 return;
1972 type = TYPE_P (sizeof_arg[idx])
1973 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1974 if (!POINTER_TYPE_P (type))
1975 return;
1977 if (dest
1978 && (tem = tree_strip_nop_conversions (dest))
1979 && POINTER_TYPE_P (TREE_TYPE (tem))
1980 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1981 return;
1983 if (src
1984 && (tem = tree_strip_nop_conversions (src))
1985 && POINTER_TYPE_P (TREE_TYPE (tem))
1986 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1987 return;
1989 loc = sizeof_arg_loc[idx];
1991 if (dest && !cmp)
1993 if (!TYPE_P (sizeof_arg[idx])
1994 && operand_equal_p (dest, sizeof_arg[idx], 0)
1995 && comp_types (TREE_TYPE (dest), type))
1997 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1998 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1999 "argument to %<sizeof%> in %qD call is the same "
2000 "expression as the destination; did you mean to "
2001 "remove the addressof?", callee);
2002 else if ((TYPE_PRECISION (TREE_TYPE (type))
2003 == TYPE_PRECISION (char_type_node))
2004 || strop)
2005 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2006 "argument to %<sizeof%> in %qD call is the same "
2007 "expression as the destination; did you mean to "
2008 "provide an explicit length?", callee);
2009 else
2010 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2011 "argument to %<sizeof%> in %qD call is the same "
2012 "expression as the destination; did you mean to "
2013 "dereference it?", callee);
2014 return;
2017 if (POINTER_TYPE_P (TREE_TYPE (dest))
2018 && !strop
2019 && comp_types (TREE_TYPE (dest), type)
2020 && !VOID_TYPE_P (TREE_TYPE (type)))
2022 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2023 "argument to %<sizeof%> in %qD call is the same "
2024 "pointer type %qT as the destination; expected %qT "
2025 "or an explicit length", callee, TREE_TYPE (dest),
2026 TREE_TYPE (TREE_TYPE (dest)));
2027 return;
2031 if (src && !cmp)
2033 if (!TYPE_P (sizeof_arg[idx])
2034 && operand_equal_p (src, sizeof_arg[idx], 0)
2035 && comp_types (TREE_TYPE (src), type))
2037 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2038 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2039 "argument to %<sizeof%> in %qD call is the same "
2040 "expression as the source; did you mean to "
2041 "remove the addressof?", callee);
2042 else if ((TYPE_PRECISION (TREE_TYPE (type))
2043 == TYPE_PRECISION (char_type_node))
2044 || strop)
2045 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2046 "argument to %<sizeof%> in %qD call is the same "
2047 "expression as the source; did you mean to "
2048 "provide an explicit length?", callee);
2049 else
2050 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2051 "argument to %<sizeof%> in %qD call is the same "
2052 "expression as the source; did you mean to "
2053 "dereference it?", callee);
2054 return;
2057 if (POINTER_TYPE_P (TREE_TYPE (src))
2058 && !strop
2059 && comp_types (TREE_TYPE (src), type)
2060 && !VOID_TYPE_P (TREE_TYPE (type)))
2062 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2063 "argument to %<sizeof%> in %qD call is the same "
2064 "pointer type %qT as the source; expected %qT "
2065 "or an explicit length", callee, TREE_TYPE (src),
2066 TREE_TYPE (TREE_TYPE (src)));
2067 return;
2071 if (dest)
2073 if (!TYPE_P (sizeof_arg[idx])
2074 && operand_equal_p (dest, sizeof_arg[idx], 0)
2075 && comp_types (TREE_TYPE (dest), type))
2077 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2078 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2079 "argument to %<sizeof%> in %qD call is the same "
2080 "expression as the first source; did you mean to "
2081 "remove the addressof?", callee);
2082 else if ((TYPE_PRECISION (TREE_TYPE (type))
2083 == TYPE_PRECISION (char_type_node))
2084 || strop)
2085 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2086 "argument to %<sizeof%> in %qD call is the same "
2087 "expression as the first source; did you mean to "
2088 "provide an explicit length?", callee);
2089 else
2090 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2091 "argument to %<sizeof%> in %qD call is the same "
2092 "expression as the first source; did you mean to "
2093 "dereference it?", callee);
2094 return;
2097 if (POINTER_TYPE_P (TREE_TYPE (dest))
2098 && !strop
2099 && comp_types (TREE_TYPE (dest), type)
2100 && !VOID_TYPE_P (TREE_TYPE (type)))
2102 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2103 "argument to %<sizeof%> in %qD call is the same "
2104 "pointer type %qT as the first source; expected %qT "
2105 "or an explicit length", callee, TREE_TYPE (dest),
2106 TREE_TYPE (TREE_TYPE (dest)));
2107 return;
2111 if (src)
2113 if (!TYPE_P (sizeof_arg[idx])
2114 && operand_equal_p (src, sizeof_arg[idx], 0)
2115 && comp_types (TREE_TYPE (src), type))
2117 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2118 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2119 "argument to %<sizeof%> in %qD call is the same "
2120 "expression as the second source; did you mean to "
2121 "remove the addressof?", callee);
2122 else if ((TYPE_PRECISION (TREE_TYPE (type))
2123 == TYPE_PRECISION (char_type_node))
2124 || strop)
2125 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2126 "argument to %<sizeof%> in %qD call is the same "
2127 "expression as the second source; did you mean to "
2128 "provide an explicit length?", callee);
2129 else
2130 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2131 "argument to %<sizeof%> in %qD call is the same "
2132 "expression as the second source; did you mean to "
2133 "dereference it?", callee);
2134 return;
2137 if (POINTER_TYPE_P (TREE_TYPE (src))
2138 && !strop
2139 && comp_types (TREE_TYPE (src), type)
2140 && !VOID_TYPE_P (TREE_TYPE (type)))
2142 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2143 "argument to %<sizeof%> in %qD call is the same "
2144 "pointer type %qT as the second source; expected %qT "
2145 "or an explicit length", callee, TREE_TYPE (src),
2146 TREE_TYPE (TREE_TYPE (src)));
2147 return;
2153 /* Warn for unlikely, improbable, or stupid DECL declarations
2154 of `main'. */
2156 void
2157 check_main_parameter_types (tree decl)
2159 function_args_iterator iter;
2160 tree type;
2161 int argct = 0;
2163 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2165 /* XXX void_type_node belies the abstraction. */
2166 if (type == void_type_node || type == error_mark_node )
2167 break;
2169 ++argct;
2170 switch (argct)
2172 case 1:
2173 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2174 pedwarn (input_location, OPT_Wmain,
2175 "first argument of %q+D should be %<int%>", decl);
2176 break;
2178 case 2:
2179 if (TREE_CODE (type) != POINTER_TYPE
2180 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2181 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2182 != char_type_node))
2183 pedwarn (input_location, OPT_Wmain,
2184 "second argument of %q+D should be %<char **%>", decl);
2185 break;
2187 case 3:
2188 if (TREE_CODE (type) != POINTER_TYPE
2189 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2190 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2191 != char_type_node))
2192 pedwarn (input_location, OPT_Wmain,
2193 "third argument of %q+D should probably be "
2194 "%<char **%>", decl);
2195 break;
2199 /* It is intentional that this message does not mention the third
2200 argument because it's only mentioned in an appendix of the
2201 standard. */
2202 if (argct > 0 && (argct < 2 || argct > 3))
2203 pedwarn (input_location, OPT_Wmain,
2204 "%q+D takes only zero or two arguments", decl);
2207 /* True if pointers to distinct types T1 and T2 can be converted to
2208 each other without an explicit cast. Only returns true for opaque
2209 vector types. */
2210 bool
2211 vector_targets_convertible_p (const_tree t1, const_tree t2)
2213 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2214 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2215 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2216 return true;
2218 return false;
2221 /* True if vector types T1 and T2 can be converted to each other
2222 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2223 can only be converted with -flax-vector-conversions yet that is not
2224 in effect, emit a note telling the user about that option if such
2225 a note has not previously been emitted. */
2226 bool
2227 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2229 static bool emitted_lax_note = false;
2230 bool convertible_lax;
2232 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2233 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2234 return true;
2236 convertible_lax =
2237 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2238 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2239 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2240 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2241 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2243 if (!convertible_lax || flag_lax_vector_conversions)
2244 return convertible_lax;
2246 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2247 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2248 return true;
2250 if (emit_lax_note && !emitted_lax_note)
2252 emitted_lax_note = true;
2253 inform (input_location, "use -flax-vector-conversions to permit "
2254 "conversions between vectors with differing "
2255 "element types or numbers of subparts");
2258 return false;
2261 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2262 and have vector types, V0 has the same type as V1, and the number of
2263 elements of V0, V1, MASK is the same.
2265 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2266 called with two arguments. In this case implementation passes the
2267 first argument twice in order to share the same tree code. This fact
2268 could enable the mask-values being twice the vector length. This is
2269 an implementation accident and this semantics is not guaranteed to
2270 the user. */
2271 tree
2272 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2274 tree ret;
2275 bool wrap = true;
2276 bool maybe_const = false;
2277 bool two_arguments = false;
2279 if (v1 == NULL_TREE)
2281 two_arguments = true;
2282 v1 = v0;
2285 if (v0 == error_mark_node || v1 == error_mark_node
2286 || mask == error_mark_node)
2287 return error_mark_node;
2289 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2290 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2292 error_at (loc, "__builtin_shuffle last argument must "
2293 "be an integer vector");
2294 return error_mark_node;
2297 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2298 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2300 error_at (loc, "__builtin_shuffle arguments must be vectors");
2301 return error_mark_node;
2304 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2306 error_at (loc, "__builtin_shuffle argument vectors must be of "
2307 "the same type");
2308 return error_mark_node;
2311 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2312 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2313 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2314 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2316 error_at (loc, "__builtin_shuffle number of elements of the "
2317 "argument vector(s) and the mask vector should "
2318 "be the same");
2319 return error_mark_node;
2322 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2323 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2325 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2326 "must have the same size as inner type of the mask");
2327 return error_mark_node;
2330 if (!c_dialect_cxx ())
2332 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2333 v0 = c_fully_fold (v0, false, &maybe_const);
2334 wrap &= maybe_const;
2336 if (two_arguments)
2337 v1 = v0 = save_expr (v0);
2338 else
2340 v1 = c_fully_fold (v1, false, &maybe_const);
2341 wrap &= maybe_const;
2344 mask = c_fully_fold (mask, false, &maybe_const);
2345 wrap &= maybe_const;
2348 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2350 if (!c_dialect_cxx () && !wrap)
2351 ret = c_wrap_maybe_const (ret, true);
2353 return ret;
2356 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2357 to integral type. */
2359 static tree
2360 c_common_get_narrower (tree op, int *unsignedp_ptr)
2362 op = get_narrower (op, unsignedp_ptr);
2364 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2365 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2367 /* C++0x scoped enumerations don't implicitly convert to integral
2368 type; if we stripped an explicit conversion to a larger type we
2369 need to replace it so common_type will still work. */
2370 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2371 TYPE_UNSIGNED (TREE_TYPE (op)));
2372 op = fold_convert (type, op);
2374 return op;
2377 /* This is a helper function of build_binary_op.
2379 For certain operations if both args were extended from the same
2380 smaller type, do the arithmetic in that type and then extend.
2382 BITWISE indicates a bitwise operation.
2383 For them, this optimization is safe only if
2384 both args are zero-extended or both are sign-extended.
2385 Otherwise, we might change the result.
2386 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2387 but calculated in (unsigned short) it would be (unsigned short)-1.
2389 tree
2390 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2392 int unsigned0, unsigned1;
2393 tree arg0, arg1;
2394 int uns;
2395 tree type;
2397 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2398 excessive narrowing when we call get_narrower below. For
2399 example, suppose that OP0 is of unsigned int extended
2400 from signed char and that RESULT_TYPE is long long int.
2401 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2402 like
2404 (long long int) (unsigned int) signed_char
2406 which get_narrower would narrow down to
2408 (unsigned int) signed char
2410 If we do not cast OP0 first, get_narrower would return
2411 signed_char, which is inconsistent with the case of the
2412 explicit cast. */
2413 op0 = convert (result_type, op0);
2414 op1 = convert (result_type, op1);
2416 arg0 = c_common_get_narrower (op0, &unsigned0);
2417 arg1 = c_common_get_narrower (op1, &unsigned1);
2419 /* UNS is 1 if the operation to be done is an unsigned one. */
2420 uns = TYPE_UNSIGNED (result_type);
2422 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2423 but it *requires* conversion to FINAL_TYPE. */
2425 if ((TYPE_PRECISION (TREE_TYPE (op0))
2426 == TYPE_PRECISION (TREE_TYPE (arg0)))
2427 && TREE_TYPE (op0) != result_type)
2428 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2429 if ((TYPE_PRECISION (TREE_TYPE (op1))
2430 == TYPE_PRECISION (TREE_TYPE (arg1)))
2431 && TREE_TYPE (op1) != result_type)
2432 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2434 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2436 /* For bitwise operations, signedness of nominal type
2437 does not matter. Consider only how operands were extended. */
2438 if (bitwise)
2439 uns = unsigned0;
2441 /* Note that in all three cases below we refrain from optimizing
2442 an unsigned operation on sign-extended args.
2443 That would not be valid. */
2445 /* Both args variable: if both extended in same way
2446 from same width, do it in that width.
2447 Do it unsigned if args were zero-extended. */
2448 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2449 < TYPE_PRECISION (result_type))
2450 && (TYPE_PRECISION (TREE_TYPE (arg1))
2451 == TYPE_PRECISION (TREE_TYPE (arg0)))
2452 && unsigned0 == unsigned1
2453 && (unsigned0 || !uns))
2454 return c_common_signed_or_unsigned_type
2455 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2457 else if (TREE_CODE (arg0) == INTEGER_CST
2458 && (unsigned1 || !uns)
2459 && (TYPE_PRECISION (TREE_TYPE (arg1))
2460 < TYPE_PRECISION (result_type))
2461 && (type
2462 = c_common_signed_or_unsigned_type (unsigned1,
2463 TREE_TYPE (arg1)))
2464 && !POINTER_TYPE_P (type)
2465 && int_fits_type_p (arg0, type))
2466 return type;
2468 else if (TREE_CODE (arg1) == INTEGER_CST
2469 && (unsigned0 || !uns)
2470 && (TYPE_PRECISION (TREE_TYPE (arg0))
2471 < TYPE_PRECISION (result_type))
2472 && (type
2473 = c_common_signed_or_unsigned_type (unsigned0,
2474 TREE_TYPE (arg0)))
2475 && !POINTER_TYPE_P (type)
2476 && int_fits_type_p (arg1, type))
2477 return type;
2479 return result_type;
2482 /* Checks if expression EXPR of real/integer type cannot be converted
2483 to the real/integer type TYPE. Function returns true when:
2484 * EXPR is a constant which cannot be exactly converted to TYPE
2485 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2486 for EXPR type and TYPE being both integers or both real.
2487 * EXPR is not a constant of real type and TYPE is an integer.
2488 * EXPR is not a constant of integer type which cannot be
2489 exactly converted to real type.
2490 Function allows conversions between types of different signedness and
2491 does not return true in that case. Function can produce signedness
2492 warnings if PRODUCE_WARNS is true. */
2493 bool
2494 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2496 bool give_warning = false;
2497 tree expr_type = TREE_TYPE (expr);
2498 location_t loc = EXPR_LOC_OR_HERE (expr);
2500 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2502 /* Warn for real constant that is not an exact integer converted
2503 to integer type. */
2504 if (TREE_CODE (expr_type) == REAL_TYPE
2505 && TREE_CODE (type) == INTEGER_TYPE)
2507 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2508 give_warning = true;
2510 /* Warn for an integer constant that does not fit into integer type. */
2511 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2512 && TREE_CODE (type) == INTEGER_TYPE
2513 && !int_fits_type_p (expr, type))
2515 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2516 && tree_int_cst_sgn (expr) < 0)
2518 if (produce_warns)
2519 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2520 " implicitly converted to unsigned type");
2522 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2524 if (produce_warns)
2525 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2526 " constant value to negative integer");
2528 else
2529 give_warning = true;
2531 else if (TREE_CODE (type) == REAL_TYPE)
2533 /* Warn for an integer constant that does not fit into real type. */
2534 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2536 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2537 if (!exact_real_truncate (TYPE_MODE (type), &a))
2538 give_warning = true;
2540 /* Warn for a real constant that does not fit into a smaller
2541 real type. */
2542 else if (TREE_CODE (expr_type) == REAL_TYPE
2543 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2545 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2546 if (!exact_real_truncate (TYPE_MODE (type), &a))
2547 give_warning = true;
2551 else
2553 /* Warn for real types converted to integer types. */
2554 if (TREE_CODE (expr_type) == REAL_TYPE
2555 && TREE_CODE (type) == INTEGER_TYPE)
2556 give_warning = true;
2558 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2559 && TREE_CODE (type) == INTEGER_TYPE)
2561 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2562 expr = get_unwidened (expr, 0);
2563 expr_type = TREE_TYPE (expr);
2565 /* Don't warn for short y; short x = ((int)y & 0xff); */
2566 if (TREE_CODE (expr) == BIT_AND_EXPR
2567 || TREE_CODE (expr) == BIT_IOR_EXPR
2568 || TREE_CODE (expr) == BIT_XOR_EXPR)
2570 /* If both args were extended from a shortest type,
2571 use that type if that is safe. */
2572 expr_type = shorten_binary_op (expr_type,
2573 TREE_OPERAND (expr, 0),
2574 TREE_OPERAND (expr, 1),
2575 /* bitwise */1);
2577 if (TREE_CODE (expr) == BIT_AND_EXPR)
2579 tree op0 = TREE_OPERAND (expr, 0);
2580 tree op1 = TREE_OPERAND (expr, 1);
2581 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2582 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2584 /* If one of the operands is a non-negative constant
2585 that fits in the target type, then the type of the
2586 other operand does not matter. */
2587 if ((TREE_CODE (op0) == INTEGER_CST
2588 && int_fits_type_p (op0, c_common_signed_type (type))
2589 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2590 || (TREE_CODE (op1) == INTEGER_CST
2591 && int_fits_type_p (op1, c_common_signed_type (type))
2592 && int_fits_type_p (op1,
2593 c_common_unsigned_type (type))))
2594 return false;
2595 /* If constant is unsigned and fits in the target
2596 type, then the result will also fit. */
2597 else if ((TREE_CODE (op0) == INTEGER_CST
2598 && unsigned0
2599 && int_fits_type_p (op0, type))
2600 || (TREE_CODE (op1) == INTEGER_CST
2601 && unsigned1
2602 && int_fits_type_p (op1, type)))
2603 return false;
2606 /* Warn for integer types converted to smaller integer types. */
2607 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2608 give_warning = true;
2610 /* When they are the same width but different signedness,
2611 then the value may change. */
2612 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2613 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2614 /* Even when converted to a bigger type, if the type is
2615 unsigned but expr is signed, then negative values
2616 will be changed. */
2617 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2618 && produce_warns)
2619 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2620 "may change the sign of the result",
2621 type, expr_type);
2624 /* Warn for integer types converted to real types if and only if
2625 all the range of values of the integer type cannot be
2626 represented by the real type. */
2627 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2628 && TREE_CODE (type) == REAL_TYPE)
2630 tree type_low_bound, type_high_bound;
2631 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2633 /* Don't warn about char y = 0xff; float x = (int) y; */
2634 expr = get_unwidened (expr, 0);
2635 expr_type = TREE_TYPE (expr);
2637 type_low_bound = TYPE_MIN_VALUE (expr_type);
2638 type_high_bound = TYPE_MAX_VALUE (expr_type);
2639 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2640 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2642 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2643 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2644 give_warning = true;
2647 /* Warn for real types converted to smaller real types. */
2648 else if (TREE_CODE (expr_type) == REAL_TYPE
2649 && TREE_CODE (type) == REAL_TYPE
2650 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2651 give_warning = true;
2654 return give_warning;
2657 /* Warns if the conversion of EXPR to TYPE may alter a value.
2658 This is a helper function for warnings_for_convert_and_check. */
2660 static void
2661 conversion_warning (tree type, tree expr)
2663 tree expr_type = TREE_TYPE (expr);
2664 location_t loc = EXPR_LOC_OR_HERE (expr);
2666 if (!warn_conversion && !warn_sign_conversion)
2667 return;
2669 switch (TREE_CODE (expr))
2671 case EQ_EXPR:
2672 case NE_EXPR:
2673 case LE_EXPR:
2674 case GE_EXPR:
2675 case LT_EXPR:
2676 case GT_EXPR:
2677 case TRUTH_ANDIF_EXPR:
2678 case TRUTH_ORIF_EXPR:
2679 case TRUTH_AND_EXPR:
2680 case TRUTH_OR_EXPR:
2681 case TRUTH_XOR_EXPR:
2682 case TRUTH_NOT_EXPR:
2683 /* Conversion from boolean to a signed:1 bit-field (which only
2684 can hold the values 0 and -1) doesn't lose information - but
2685 it does change the value. */
2686 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2687 warning_at (loc, OPT_Wconversion,
2688 "conversion to %qT from boolean expression", type);
2689 return;
2691 case REAL_CST:
2692 case INTEGER_CST:
2693 if (unsafe_conversion_p (type, expr, true))
2694 warning_at (loc, OPT_Wconversion,
2695 "conversion to %qT alters %qT constant value",
2696 type, expr_type);
2697 return;
2699 case COND_EXPR:
2701 /* In case of COND_EXPR, we do not care about the type of
2702 COND_EXPR, only about the conversion of each operand. */
2703 tree op1 = TREE_OPERAND (expr, 1);
2704 tree op2 = TREE_OPERAND (expr, 2);
2706 conversion_warning (type, op1);
2707 conversion_warning (type, op2);
2708 return;
2711 default: /* 'expr' is not a constant. */
2712 if (unsafe_conversion_p (type, expr, true))
2713 warning_at (loc, OPT_Wconversion,
2714 "conversion to %qT from %qT may alter its value",
2715 type, expr_type);
2719 /* Produce warnings after a conversion. RESULT is the result of
2720 converting EXPR to TYPE. This is a helper function for
2721 convert_and_check and cp_convert_and_check. */
2723 void
2724 warnings_for_convert_and_check (tree type, tree expr, tree result)
2726 location_t loc = EXPR_LOC_OR_HERE (expr);
2728 if (TREE_CODE (expr) == INTEGER_CST
2729 && (TREE_CODE (type) == INTEGER_TYPE
2730 || TREE_CODE (type) == ENUMERAL_TYPE)
2731 && !int_fits_type_p (expr, type))
2733 /* Do not diagnose overflow in a constant expression merely
2734 because a conversion overflowed. */
2735 if (TREE_OVERFLOW (result))
2736 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2738 if (TYPE_UNSIGNED (type))
2740 /* This detects cases like converting -129 or 256 to
2741 unsigned char. */
2742 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2743 warning_at (loc, OPT_Woverflow,
2744 "large integer implicitly truncated to unsigned type");
2745 else
2746 conversion_warning (type, expr);
2748 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2749 warning (OPT_Woverflow,
2750 "overflow in implicit constant conversion");
2751 /* No warning for converting 0x80000000 to int. */
2752 else if (pedantic
2753 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2754 || TYPE_PRECISION (TREE_TYPE (expr))
2755 != TYPE_PRECISION (type)))
2756 warning_at (loc, OPT_Woverflow,
2757 "overflow in implicit constant conversion");
2759 else
2760 conversion_warning (type, expr);
2762 else if ((TREE_CODE (result) == INTEGER_CST
2763 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2764 warning_at (loc, OPT_Woverflow,
2765 "overflow in implicit constant conversion");
2766 else
2767 conversion_warning (type, expr);
2771 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2772 Invoke this function on every expression that is converted implicitly,
2773 i.e. because of language rules and not because of an explicit cast. */
2775 tree
2776 convert_and_check (tree type, tree expr)
2778 tree result;
2779 tree expr_for_warning;
2781 /* Convert from a value with possible excess precision rather than
2782 via the semantic type, but do not warn about values not fitting
2783 exactly in the semantic type. */
2784 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2786 tree orig_type = TREE_TYPE (expr);
2787 expr = TREE_OPERAND (expr, 0);
2788 expr_for_warning = convert (orig_type, expr);
2789 if (orig_type == type)
2790 return expr_for_warning;
2792 else
2793 expr_for_warning = expr;
2795 if (TREE_TYPE (expr) == type)
2796 return expr;
2798 result = convert (type, expr);
2800 if (c_inhibit_evaluation_warnings == 0
2801 && !TREE_OVERFLOW_P (expr)
2802 && result != error_mark_node)
2803 warnings_for_convert_and_check (type, expr_for_warning, result);
2805 return result;
2808 /* A node in a list that describes references to variables (EXPR), which are
2809 either read accesses if WRITER is zero, or write accesses, in which case
2810 WRITER is the parent of EXPR. */
2811 struct tlist
2813 struct tlist *next;
2814 tree expr, writer;
2817 /* Used to implement a cache the results of a call to verify_tree. We only
2818 use this for SAVE_EXPRs. */
2819 struct tlist_cache
2821 struct tlist_cache *next;
2822 struct tlist *cache_before_sp;
2823 struct tlist *cache_after_sp;
2824 tree expr;
2827 /* Obstack to use when allocating tlist structures, and corresponding
2828 firstobj. */
2829 static struct obstack tlist_obstack;
2830 static char *tlist_firstobj = 0;
2832 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2833 warnings. */
2834 static struct tlist *warned_ids;
2835 /* SAVE_EXPRs need special treatment. We process them only once and then
2836 cache the results. */
2837 static struct tlist_cache *save_expr_cache;
2839 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2840 static void merge_tlist (struct tlist **, struct tlist *, int);
2841 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2842 static int warning_candidate_p (tree);
2843 static bool candidate_equal_p (const_tree, const_tree);
2844 static void warn_for_collisions (struct tlist *);
2845 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2846 static struct tlist *new_tlist (struct tlist *, tree, tree);
2848 /* Create a new struct tlist and fill in its fields. */
2849 static struct tlist *
2850 new_tlist (struct tlist *next, tree t, tree writer)
2852 struct tlist *l;
2853 l = XOBNEW (&tlist_obstack, struct tlist);
2854 l->next = next;
2855 l->expr = t;
2856 l->writer = writer;
2857 return l;
2860 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2861 is nonnull, we ignore any node we find which has a writer equal to it. */
2863 static void
2864 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2866 while (add)
2868 struct tlist *next = add->next;
2869 if (!copy)
2870 add->next = *to;
2871 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2872 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2873 add = next;
2877 /* Merge the nodes of ADD into TO. This merging process is done so that for
2878 each variable that already exists in TO, no new node is added; however if
2879 there is a write access recorded in ADD, and an occurrence on TO is only
2880 a read access, then the occurrence in TO will be modified to record the
2881 write. */
2883 static void
2884 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2886 struct tlist **end = to;
2888 while (*end)
2889 end = &(*end)->next;
2891 while (add)
2893 int found = 0;
2894 struct tlist *tmp2;
2895 struct tlist *next = add->next;
2897 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2898 if (candidate_equal_p (tmp2->expr, add->expr))
2900 found = 1;
2901 if (!tmp2->writer)
2902 tmp2->writer = add->writer;
2904 if (!found)
2906 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2907 end = &(*end)->next;
2908 *end = 0;
2910 add = next;
2914 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2915 references in list LIST conflict with it, excluding reads if ONLY writers
2916 is nonzero. */
2918 static void
2919 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2920 int only_writes)
2922 struct tlist *tmp;
2924 /* Avoid duplicate warnings. */
2925 for (tmp = warned_ids; tmp; tmp = tmp->next)
2926 if (candidate_equal_p (tmp->expr, written))
2927 return;
2929 while (list)
2931 if (candidate_equal_p (list->expr, written)
2932 && !candidate_equal_p (list->writer, writer)
2933 && (!only_writes || list->writer))
2935 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2936 warning_at (EXPR_LOC_OR_HERE (writer),
2937 OPT_Wsequence_point, "operation on %qE may be undefined",
2938 list->expr);
2940 list = list->next;
2944 /* Given a list LIST of references to variables, find whether any of these
2945 can cause conflicts due to missing sequence points. */
2947 static void
2948 warn_for_collisions (struct tlist *list)
2950 struct tlist *tmp;
2952 for (tmp = list; tmp; tmp = tmp->next)
2954 if (tmp->writer)
2955 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2959 /* Return nonzero if X is a tree that can be verified by the sequence point
2960 warnings. */
2961 static int
2962 warning_candidate_p (tree x)
2964 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2965 return 0;
2967 if (TREE_CODE (x) == BLOCK)
2968 return 0;
2970 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2971 (lvalue_p) crash on TRY/CATCH. */
2972 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2973 return 0;
2975 if (!lvalue_p (x))
2976 return 0;
2978 /* No point to track non-const calls, they will never satisfy
2979 operand_equal_p. */
2980 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2981 return 0;
2983 if (TREE_CODE (x) == STRING_CST)
2984 return 0;
2986 return 1;
2989 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2990 static bool
2991 candidate_equal_p (const_tree x, const_tree y)
2993 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2996 /* Walk the tree X, and record accesses to variables. If X is written by the
2997 parent tree, WRITER is the parent.
2998 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2999 expression or its only operand forces a sequence point, then everything up
3000 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3001 in PNO_SP.
3002 Once we return, we will have emitted warnings if any subexpression before
3003 such a sequence point could be undefined. On a higher level, however, the
3004 sequence point may not be relevant, and we'll merge the two lists.
3006 Example: (b++, a) + b;
3007 The call that processes the COMPOUND_EXPR will store the increment of B
3008 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3009 processes the PLUS_EXPR will need to merge the two lists so that
3010 eventually, all accesses end up on the same list (and we'll warn about the
3011 unordered subexpressions b++ and b.
3013 A note on merging. If we modify the former example so that our expression
3014 becomes
3015 (b++, b) + a
3016 care must be taken not simply to add all three expressions into the final
3017 PNO_SP list. The function merge_tlist takes care of that by merging the
3018 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3019 way, so that no more than one access to B is recorded. */
3021 static void
3022 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3023 tree writer)
3025 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3026 enum tree_code code;
3027 enum tree_code_class cl;
3029 /* X may be NULL if it is the operand of an empty statement expression
3030 ({ }). */
3031 if (x == NULL)
3032 return;
3034 restart:
3035 code = TREE_CODE (x);
3036 cl = TREE_CODE_CLASS (code);
3038 if (warning_candidate_p (x))
3039 *pno_sp = new_tlist (*pno_sp, x, writer);
3041 switch (code)
3043 case CONSTRUCTOR:
3044 return;
3046 case COMPOUND_EXPR:
3047 case TRUTH_ANDIF_EXPR:
3048 case TRUTH_ORIF_EXPR:
3049 tmp_before = tmp_nosp = tmp_list3 = 0;
3050 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3051 warn_for_collisions (tmp_nosp);
3052 merge_tlist (pbefore_sp, tmp_before, 0);
3053 merge_tlist (pbefore_sp, tmp_nosp, 0);
3054 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3055 merge_tlist (pbefore_sp, tmp_list3, 0);
3056 return;
3058 case COND_EXPR:
3059 tmp_before = tmp_list2 = 0;
3060 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3061 warn_for_collisions (tmp_list2);
3062 merge_tlist (pbefore_sp, tmp_before, 0);
3063 merge_tlist (pbefore_sp, tmp_list2, 1);
3065 tmp_list3 = tmp_nosp = 0;
3066 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3067 warn_for_collisions (tmp_nosp);
3068 merge_tlist (pbefore_sp, tmp_list3, 0);
3070 tmp_list3 = tmp_list2 = 0;
3071 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3072 warn_for_collisions (tmp_list2);
3073 merge_tlist (pbefore_sp, tmp_list3, 0);
3074 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3075 two first, to avoid warning for (a ? b++ : b++). */
3076 merge_tlist (&tmp_nosp, tmp_list2, 0);
3077 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3078 return;
3080 case PREDECREMENT_EXPR:
3081 case PREINCREMENT_EXPR:
3082 case POSTDECREMENT_EXPR:
3083 case POSTINCREMENT_EXPR:
3084 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3085 return;
3087 case MODIFY_EXPR:
3088 tmp_before = tmp_nosp = tmp_list3 = 0;
3089 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3090 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3091 /* Expressions inside the LHS are not ordered wrt. the sequence points
3092 in the RHS. Example:
3093 *a = (a++, 2)
3094 Despite the fact that the modification of "a" is in the before_sp
3095 list (tmp_before), it conflicts with the use of "a" in the LHS.
3096 We can handle this by adding the contents of tmp_list3
3097 to those of tmp_before, and redoing the collision warnings for that
3098 list. */
3099 add_tlist (&tmp_before, tmp_list3, x, 1);
3100 warn_for_collisions (tmp_before);
3101 /* Exclude the LHS itself here; we first have to merge it into the
3102 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3103 didn't exclude the LHS, we'd get it twice, once as a read and once
3104 as a write. */
3105 add_tlist (pno_sp, tmp_list3, x, 0);
3106 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3108 merge_tlist (pbefore_sp, tmp_before, 0);
3109 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3110 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3111 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3112 return;
3114 case CALL_EXPR:
3115 /* We need to warn about conflicts among arguments and conflicts between
3116 args and the function address. Side effects of the function address,
3117 however, are not ordered by the sequence point of the call. */
3119 call_expr_arg_iterator iter;
3120 tree arg;
3121 tmp_before = tmp_nosp = 0;
3122 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3123 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3125 tmp_list2 = tmp_list3 = 0;
3126 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3127 merge_tlist (&tmp_list3, tmp_list2, 0);
3128 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3130 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3131 warn_for_collisions (tmp_before);
3132 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3133 return;
3136 case TREE_LIST:
3137 /* Scan all the list, e.g. indices of multi dimensional array. */
3138 while (x)
3140 tmp_before = tmp_nosp = 0;
3141 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3142 merge_tlist (&tmp_nosp, tmp_before, 0);
3143 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3144 x = TREE_CHAIN (x);
3146 return;
3148 case SAVE_EXPR:
3150 struct tlist_cache *t;
3151 for (t = save_expr_cache; t; t = t->next)
3152 if (candidate_equal_p (t->expr, x))
3153 break;
3155 if (!t)
3157 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3158 t->next = save_expr_cache;
3159 t->expr = x;
3160 save_expr_cache = t;
3162 tmp_before = tmp_nosp = 0;
3163 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3164 warn_for_collisions (tmp_nosp);
3166 tmp_list3 = 0;
3167 while (tmp_nosp)
3169 struct tlist *t = tmp_nosp;
3170 tmp_nosp = t->next;
3171 merge_tlist (&tmp_list3, t, 0);
3173 t->cache_before_sp = tmp_before;
3174 t->cache_after_sp = tmp_list3;
3176 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3177 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3178 return;
3181 case ADDR_EXPR:
3182 x = TREE_OPERAND (x, 0);
3183 if (DECL_P (x))
3184 return;
3185 writer = 0;
3186 goto restart;
3188 default:
3189 /* For other expressions, simply recurse on their operands.
3190 Manual tail recursion for unary expressions.
3191 Other non-expressions need not be processed. */
3192 if (cl == tcc_unary)
3194 x = TREE_OPERAND (x, 0);
3195 writer = 0;
3196 goto restart;
3198 else if (IS_EXPR_CODE_CLASS (cl))
3200 int lp;
3201 int max = TREE_OPERAND_LENGTH (x);
3202 for (lp = 0; lp < max; lp++)
3204 tmp_before = tmp_nosp = 0;
3205 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3206 merge_tlist (&tmp_nosp, tmp_before, 0);
3207 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3210 return;
3214 /* Try to warn for undefined behavior in EXPR due to missing sequence
3215 points. */
3217 DEBUG_FUNCTION void
3218 verify_sequence_points (tree expr)
3220 struct tlist *before_sp = 0, *after_sp = 0;
3222 warned_ids = 0;
3223 save_expr_cache = 0;
3224 if (tlist_firstobj == 0)
3226 gcc_obstack_init (&tlist_obstack);
3227 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3230 verify_tree (expr, &before_sp, &after_sp, 0);
3231 warn_for_collisions (after_sp);
3232 obstack_free (&tlist_obstack, tlist_firstobj);
3235 /* Validate the expression after `case' and apply default promotions. */
3237 static tree
3238 check_case_value (tree value)
3240 if (value == NULL_TREE)
3241 return value;
3243 if (TREE_CODE (value) == INTEGER_CST)
3244 /* Promote char or short to int. */
3245 value = perform_integral_promotions (value);
3246 else if (value != error_mark_node)
3248 error ("case label does not reduce to an integer constant");
3249 value = error_mark_node;
3252 constant_expression_warning (value);
3254 return value;
3257 /* See if the case values LOW and HIGH are in the range of the original
3258 type (i.e. before the default conversion to int) of the switch testing
3259 expression.
3260 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3261 the type before promoting it. CASE_LOW_P is a pointer to the lower
3262 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3263 if the case is not a case range.
3264 The caller has to make sure that we are not called with NULL for
3265 CASE_LOW_P (i.e. the default case).
3266 Returns true if the case label is in range of ORIG_TYPE (saturated or
3267 untouched) or false if the label is out of range. */
3269 static bool
3270 check_case_bounds (tree type, tree orig_type,
3271 tree *case_low_p, tree *case_high_p)
3273 tree min_value, max_value;
3274 tree case_low = *case_low_p;
3275 tree case_high = case_high_p ? *case_high_p : case_low;
3277 /* If there was a problem with the original type, do nothing. */
3278 if (orig_type == error_mark_node)
3279 return true;
3281 min_value = TYPE_MIN_VALUE (orig_type);
3282 max_value = TYPE_MAX_VALUE (orig_type);
3284 /* Case label is less than minimum for type. */
3285 if (tree_int_cst_compare (case_low, min_value) < 0
3286 && tree_int_cst_compare (case_high, min_value) < 0)
3288 warning (0, "case label value is less than minimum value for type");
3289 return false;
3292 /* Case value is greater than maximum for type. */
3293 if (tree_int_cst_compare (case_low, max_value) > 0
3294 && tree_int_cst_compare (case_high, max_value) > 0)
3296 warning (0, "case label value exceeds maximum value for type");
3297 return false;
3300 /* Saturate lower case label value to minimum. */
3301 if (tree_int_cst_compare (case_high, min_value) >= 0
3302 && tree_int_cst_compare (case_low, min_value) < 0)
3304 warning (0, "lower value in case label range"
3305 " less than minimum value for type");
3306 case_low = min_value;
3309 /* Saturate upper case label value to maximum. */
3310 if (tree_int_cst_compare (case_low, max_value) <= 0
3311 && tree_int_cst_compare (case_high, max_value) > 0)
3313 warning (0, "upper value in case label range"
3314 " exceeds maximum value for type");
3315 case_high = max_value;
3318 if (*case_low_p != case_low)
3319 *case_low_p = convert (type, case_low);
3320 if (case_high_p && *case_high_p != case_high)
3321 *case_high_p = convert (type, case_high);
3323 return true;
3326 /* Return an integer type with BITS bits of precision,
3327 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3329 tree
3330 c_common_type_for_size (unsigned int bits, int unsignedp)
3332 if (bits == TYPE_PRECISION (integer_type_node))
3333 return unsignedp ? unsigned_type_node : integer_type_node;
3335 if (bits == TYPE_PRECISION (signed_char_type_node))
3336 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3338 if (bits == TYPE_PRECISION (short_integer_type_node))
3339 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3341 if (bits == TYPE_PRECISION (long_integer_type_node))
3342 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3344 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3345 return (unsignedp ? long_long_unsigned_type_node
3346 : long_long_integer_type_node);
3348 if (int128_integer_type_node
3349 && bits == TYPE_PRECISION (int128_integer_type_node))
3350 return (unsignedp ? int128_unsigned_type_node
3351 : int128_integer_type_node);
3353 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3354 return (unsignedp ? widest_unsigned_literal_type_node
3355 : widest_integer_literal_type_node);
3357 if (bits <= TYPE_PRECISION (intQI_type_node))
3358 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3360 if (bits <= TYPE_PRECISION (intHI_type_node))
3361 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3363 if (bits <= TYPE_PRECISION (intSI_type_node))
3364 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3366 if (bits <= TYPE_PRECISION (intDI_type_node))
3367 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3369 return 0;
3372 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3373 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3374 and saturating if SATP is nonzero, otherwise not saturating. */
3376 tree
3377 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3378 int unsignedp, int satp)
3380 enum machine_mode mode;
3381 if (ibit == 0)
3382 mode = unsignedp ? UQQmode : QQmode;
3383 else
3384 mode = unsignedp ? UHAmode : HAmode;
3386 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3387 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3388 break;
3390 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3392 sorry ("GCC cannot support operators with integer types and "
3393 "fixed-point types that have too many integral and "
3394 "fractional bits together");
3395 return 0;
3398 return c_common_type_for_mode (mode, satp);
3401 /* Used for communication between c_common_type_for_mode and
3402 c_register_builtin_type. */
3403 static GTY(()) tree registered_builtin_types;
3405 /* Return a data type that has machine mode MODE.
3406 If the mode is an integer,
3407 then UNSIGNEDP selects between signed and unsigned types.
3408 If the mode is a fixed-point mode,
3409 then UNSIGNEDP selects between saturating and nonsaturating types. */
3411 tree
3412 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3414 tree t;
3416 if (mode == TYPE_MODE (integer_type_node))
3417 return unsignedp ? unsigned_type_node : integer_type_node;
3419 if (mode == TYPE_MODE (signed_char_type_node))
3420 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3422 if (mode == TYPE_MODE (short_integer_type_node))
3423 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3425 if (mode == TYPE_MODE (long_integer_type_node))
3426 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3428 if (mode == TYPE_MODE (long_long_integer_type_node))
3429 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3431 if (int128_integer_type_node
3432 && mode == TYPE_MODE (int128_integer_type_node))
3433 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3435 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3436 return unsignedp ? widest_unsigned_literal_type_node
3437 : widest_integer_literal_type_node;
3439 if (mode == QImode)
3440 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3442 if (mode == HImode)
3443 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3445 if (mode == SImode)
3446 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3448 if (mode == DImode)
3449 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3451 #if HOST_BITS_PER_WIDE_INT >= 64
3452 if (mode == TYPE_MODE (intTI_type_node))
3453 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3454 #endif
3456 if (mode == TYPE_MODE (float_type_node))
3457 return float_type_node;
3459 if (mode == TYPE_MODE (double_type_node))
3460 return double_type_node;
3462 if (mode == TYPE_MODE (long_double_type_node))
3463 return long_double_type_node;
3465 if (mode == TYPE_MODE (void_type_node))
3466 return void_type_node;
3468 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3469 return (unsignedp
3470 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3471 : make_signed_type (GET_MODE_PRECISION (mode)));
3473 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3474 return (unsignedp
3475 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3476 : make_signed_type (GET_MODE_PRECISION (mode)));
3478 if (COMPLEX_MODE_P (mode))
3480 enum machine_mode inner_mode;
3481 tree inner_type;
3483 if (mode == TYPE_MODE (complex_float_type_node))
3484 return complex_float_type_node;
3485 if (mode == TYPE_MODE (complex_double_type_node))
3486 return complex_double_type_node;
3487 if (mode == TYPE_MODE (complex_long_double_type_node))
3488 return complex_long_double_type_node;
3490 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3491 return complex_integer_type_node;
3493 inner_mode = GET_MODE_INNER (mode);
3494 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3495 if (inner_type != NULL_TREE)
3496 return build_complex_type (inner_type);
3498 else if (VECTOR_MODE_P (mode))
3500 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3501 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3502 if (inner_type != NULL_TREE)
3503 return build_vector_type_for_mode (inner_type, mode);
3506 if (mode == TYPE_MODE (dfloat32_type_node))
3507 return dfloat32_type_node;
3508 if (mode == TYPE_MODE (dfloat64_type_node))
3509 return dfloat64_type_node;
3510 if (mode == TYPE_MODE (dfloat128_type_node))
3511 return dfloat128_type_node;
3513 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3515 if (mode == TYPE_MODE (short_fract_type_node))
3516 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3517 if (mode == TYPE_MODE (fract_type_node))
3518 return unsignedp ? sat_fract_type_node : fract_type_node;
3519 if (mode == TYPE_MODE (long_fract_type_node))
3520 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3521 if (mode == TYPE_MODE (long_long_fract_type_node))
3522 return unsignedp ? sat_long_long_fract_type_node
3523 : long_long_fract_type_node;
3525 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3526 return unsignedp ? sat_unsigned_short_fract_type_node
3527 : unsigned_short_fract_type_node;
3528 if (mode == TYPE_MODE (unsigned_fract_type_node))
3529 return unsignedp ? sat_unsigned_fract_type_node
3530 : unsigned_fract_type_node;
3531 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3532 return unsignedp ? sat_unsigned_long_fract_type_node
3533 : unsigned_long_fract_type_node;
3534 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3535 return unsignedp ? sat_unsigned_long_long_fract_type_node
3536 : unsigned_long_long_fract_type_node;
3538 if (mode == TYPE_MODE (short_accum_type_node))
3539 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3540 if (mode == TYPE_MODE (accum_type_node))
3541 return unsignedp ? sat_accum_type_node : accum_type_node;
3542 if (mode == TYPE_MODE (long_accum_type_node))
3543 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3544 if (mode == TYPE_MODE (long_long_accum_type_node))
3545 return unsignedp ? sat_long_long_accum_type_node
3546 : long_long_accum_type_node;
3548 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3549 return unsignedp ? sat_unsigned_short_accum_type_node
3550 : unsigned_short_accum_type_node;
3551 if (mode == TYPE_MODE (unsigned_accum_type_node))
3552 return unsignedp ? sat_unsigned_accum_type_node
3553 : unsigned_accum_type_node;
3554 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3555 return unsignedp ? sat_unsigned_long_accum_type_node
3556 : unsigned_long_accum_type_node;
3557 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3558 return unsignedp ? sat_unsigned_long_long_accum_type_node
3559 : unsigned_long_long_accum_type_node;
3561 if (mode == QQmode)
3562 return unsignedp ? sat_qq_type_node : qq_type_node;
3563 if (mode == HQmode)
3564 return unsignedp ? sat_hq_type_node : hq_type_node;
3565 if (mode == SQmode)
3566 return unsignedp ? sat_sq_type_node : sq_type_node;
3567 if (mode == DQmode)
3568 return unsignedp ? sat_dq_type_node : dq_type_node;
3569 if (mode == TQmode)
3570 return unsignedp ? sat_tq_type_node : tq_type_node;
3572 if (mode == UQQmode)
3573 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3574 if (mode == UHQmode)
3575 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3576 if (mode == USQmode)
3577 return unsignedp ? sat_usq_type_node : usq_type_node;
3578 if (mode == UDQmode)
3579 return unsignedp ? sat_udq_type_node : udq_type_node;
3580 if (mode == UTQmode)
3581 return unsignedp ? sat_utq_type_node : utq_type_node;
3583 if (mode == HAmode)
3584 return unsignedp ? sat_ha_type_node : ha_type_node;
3585 if (mode == SAmode)
3586 return unsignedp ? sat_sa_type_node : sa_type_node;
3587 if (mode == DAmode)
3588 return unsignedp ? sat_da_type_node : da_type_node;
3589 if (mode == TAmode)
3590 return unsignedp ? sat_ta_type_node : ta_type_node;
3592 if (mode == UHAmode)
3593 return unsignedp ? sat_uha_type_node : uha_type_node;
3594 if (mode == USAmode)
3595 return unsignedp ? sat_usa_type_node : usa_type_node;
3596 if (mode == UDAmode)
3597 return unsignedp ? sat_uda_type_node : uda_type_node;
3598 if (mode == UTAmode)
3599 return unsignedp ? sat_uta_type_node : uta_type_node;
3602 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3603 if (TYPE_MODE (TREE_VALUE (t)) == mode
3604 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3605 return TREE_VALUE (t);
3607 return 0;
3610 tree
3611 c_common_unsigned_type (tree type)
3613 return c_common_signed_or_unsigned_type (1, type);
3616 /* Return a signed type the same as TYPE in other respects. */
3618 tree
3619 c_common_signed_type (tree type)
3621 return c_common_signed_or_unsigned_type (0, type);
3624 /* Return a type the same as TYPE except unsigned or
3625 signed according to UNSIGNEDP. */
3627 tree
3628 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3630 tree type1;
3632 /* This block of code emulates the behavior of the old
3633 c_common_unsigned_type. In particular, it returns
3634 long_unsigned_type_node if passed a long, even when a int would
3635 have the same size. This is necessary for warnings to work
3636 correctly in archs where sizeof(int) == sizeof(long) */
3638 type1 = TYPE_MAIN_VARIANT (type);
3639 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3640 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3641 if (type1 == integer_type_node || type1 == unsigned_type_node)
3642 return unsignedp ? unsigned_type_node : integer_type_node;
3643 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3644 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3645 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3646 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3647 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3648 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3649 if (int128_integer_type_node
3650 && (type1 == int128_integer_type_node
3651 || type1 == int128_unsigned_type_node))
3652 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3653 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3654 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3655 #if HOST_BITS_PER_WIDE_INT >= 64
3656 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3657 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3658 #endif
3659 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3660 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3661 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3662 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3663 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3664 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3665 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3666 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3668 #define C_COMMON_FIXED_TYPES(NAME) \
3669 if (type1 == short_ ## NAME ## _type_node \
3670 || type1 == unsigned_short_ ## NAME ## _type_node) \
3671 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3672 : short_ ## NAME ## _type_node; \
3673 if (type1 == NAME ## _type_node \
3674 || type1 == unsigned_ ## NAME ## _type_node) \
3675 return unsignedp ? unsigned_ ## NAME ## _type_node \
3676 : NAME ## _type_node; \
3677 if (type1 == long_ ## NAME ## _type_node \
3678 || type1 == unsigned_long_ ## NAME ## _type_node) \
3679 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3680 : long_ ## NAME ## _type_node; \
3681 if (type1 == long_long_ ## NAME ## _type_node \
3682 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3683 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3684 : long_long_ ## NAME ## _type_node;
3686 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3687 if (type1 == NAME ## _type_node \
3688 || type1 == u ## NAME ## _type_node) \
3689 return unsignedp ? u ## NAME ## _type_node \
3690 : NAME ## _type_node;
3692 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3693 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3694 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3695 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3696 : sat_ ## short_ ## NAME ## _type_node; \
3697 if (type1 == sat_ ## NAME ## _type_node \
3698 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3699 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3700 : sat_ ## NAME ## _type_node; \
3701 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3702 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3703 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3704 : sat_ ## long_ ## NAME ## _type_node; \
3705 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3706 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3707 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3708 : sat_ ## long_long_ ## NAME ## _type_node;
3710 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3711 if (type1 == sat_ ## NAME ## _type_node \
3712 || type1 == sat_ ## u ## NAME ## _type_node) \
3713 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3714 : sat_ ## NAME ## _type_node;
3716 C_COMMON_FIXED_TYPES (fract);
3717 C_COMMON_FIXED_TYPES_SAT (fract);
3718 C_COMMON_FIXED_TYPES (accum);
3719 C_COMMON_FIXED_TYPES_SAT (accum);
3721 C_COMMON_FIXED_MODE_TYPES (qq);
3722 C_COMMON_FIXED_MODE_TYPES (hq);
3723 C_COMMON_FIXED_MODE_TYPES (sq);
3724 C_COMMON_FIXED_MODE_TYPES (dq);
3725 C_COMMON_FIXED_MODE_TYPES (tq);
3726 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3727 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3728 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3729 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3730 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3731 C_COMMON_FIXED_MODE_TYPES (ha);
3732 C_COMMON_FIXED_MODE_TYPES (sa);
3733 C_COMMON_FIXED_MODE_TYPES (da);
3734 C_COMMON_FIXED_MODE_TYPES (ta);
3735 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3736 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3737 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3738 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3740 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3741 the precision; they have precision set to match their range, but
3742 may use a wider mode to match an ABI. If we change modes, we may
3743 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3744 the precision as well, so as to yield correct results for
3745 bit-field types. C++ does not have these separate bit-field
3746 types, and producing a signed or unsigned variant of an
3747 ENUMERAL_TYPE may cause other problems as well. */
3749 if (!INTEGRAL_TYPE_P (type)
3750 || TYPE_UNSIGNED (type) == unsignedp)
3751 return type;
3753 #define TYPE_OK(node) \
3754 (TYPE_MODE (type) == TYPE_MODE (node) \
3755 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3756 if (TYPE_OK (signed_char_type_node))
3757 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3758 if (TYPE_OK (integer_type_node))
3759 return unsignedp ? unsigned_type_node : integer_type_node;
3760 if (TYPE_OK (short_integer_type_node))
3761 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3762 if (TYPE_OK (long_integer_type_node))
3763 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3764 if (TYPE_OK (long_long_integer_type_node))
3765 return (unsignedp ? long_long_unsigned_type_node
3766 : long_long_integer_type_node);
3767 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3768 return (unsignedp ? int128_unsigned_type_node
3769 : int128_integer_type_node);
3770 if (TYPE_OK (widest_integer_literal_type_node))
3771 return (unsignedp ? widest_unsigned_literal_type_node
3772 : widest_integer_literal_type_node);
3774 #if HOST_BITS_PER_WIDE_INT >= 64
3775 if (TYPE_OK (intTI_type_node))
3776 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3777 #endif
3778 if (TYPE_OK (intDI_type_node))
3779 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3780 if (TYPE_OK (intSI_type_node))
3781 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3782 if (TYPE_OK (intHI_type_node))
3783 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3784 if (TYPE_OK (intQI_type_node))
3785 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3786 #undef TYPE_OK
3788 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3791 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3793 tree
3794 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3796 /* Extended integer types of the same width as a standard type have
3797 lesser rank, so those of the same width as int promote to int or
3798 unsigned int and are valid for printf formats expecting int or
3799 unsigned int. To avoid such special cases, avoid creating
3800 extended integer types for bit-fields if a standard integer type
3801 is available. */
3802 if (width == TYPE_PRECISION (integer_type_node))
3803 return unsignedp ? unsigned_type_node : integer_type_node;
3804 if (width == TYPE_PRECISION (signed_char_type_node))
3805 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3806 if (width == TYPE_PRECISION (short_integer_type_node))
3807 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3808 if (width == TYPE_PRECISION (long_integer_type_node))
3809 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3810 if (width == TYPE_PRECISION (long_long_integer_type_node))
3811 return (unsignedp ? long_long_unsigned_type_node
3812 : long_long_integer_type_node);
3813 if (int128_integer_type_node
3814 && width == TYPE_PRECISION (int128_integer_type_node))
3815 return (unsignedp ? int128_unsigned_type_node
3816 : int128_integer_type_node);
3817 return build_nonstandard_integer_type (width, unsignedp);
3820 /* The C version of the register_builtin_type langhook. */
3822 void
3823 c_register_builtin_type (tree type, const char* name)
3825 tree decl;
3827 decl = build_decl (UNKNOWN_LOCATION,
3828 TYPE_DECL, get_identifier (name), type);
3829 DECL_ARTIFICIAL (decl) = 1;
3830 if (!TYPE_NAME (type))
3831 TYPE_NAME (type) = decl;
3832 pushdecl (decl);
3834 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3837 /* Print an error message for invalid operands to arith operation
3838 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3839 LOCATION is the location of the message. */
3841 void
3842 binary_op_error (location_t location, enum tree_code code,
3843 tree type0, tree type1)
3845 const char *opname;
3847 switch (code)
3849 case PLUS_EXPR:
3850 opname = "+"; break;
3851 case MINUS_EXPR:
3852 opname = "-"; break;
3853 case MULT_EXPR:
3854 opname = "*"; break;
3855 case MAX_EXPR:
3856 opname = "max"; break;
3857 case MIN_EXPR:
3858 opname = "min"; break;
3859 case EQ_EXPR:
3860 opname = "=="; break;
3861 case NE_EXPR:
3862 opname = "!="; break;
3863 case LE_EXPR:
3864 opname = "<="; break;
3865 case GE_EXPR:
3866 opname = ">="; break;
3867 case LT_EXPR:
3868 opname = "<"; break;
3869 case GT_EXPR:
3870 opname = ">"; break;
3871 case LSHIFT_EXPR:
3872 opname = "<<"; break;
3873 case RSHIFT_EXPR:
3874 opname = ">>"; break;
3875 case TRUNC_MOD_EXPR:
3876 case FLOOR_MOD_EXPR:
3877 opname = "%"; break;
3878 case TRUNC_DIV_EXPR:
3879 case FLOOR_DIV_EXPR:
3880 opname = "/"; break;
3881 case BIT_AND_EXPR:
3882 opname = "&"; break;
3883 case BIT_IOR_EXPR:
3884 opname = "|"; break;
3885 case TRUTH_ANDIF_EXPR:
3886 opname = "&&"; break;
3887 case TRUTH_ORIF_EXPR:
3888 opname = "||"; break;
3889 case BIT_XOR_EXPR:
3890 opname = "^"; break;
3891 default:
3892 gcc_unreachable ();
3894 error_at (location,
3895 "invalid operands to binary %s (have %qT and %qT)", opname,
3896 type0, type1);
3899 /* Given an expression as a tree, return its original type. Do this
3900 by stripping any conversion that preserves the sign and precision. */
3901 static tree
3902 expr_original_type (tree expr)
3904 STRIP_SIGN_NOPS (expr);
3905 return TREE_TYPE (expr);
3908 /* Subroutine of build_binary_op, used for comparison operations.
3909 See if the operands have both been converted from subword integer types
3910 and, if so, perhaps change them both back to their original type.
3911 This function is also responsible for converting the two operands
3912 to the proper common type for comparison.
3914 The arguments of this function are all pointers to local variables
3915 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3916 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3918 If this function returns nonzero, it means that the comparison has
3919 a constant value. What this function returns is an expression for
3920 that value. */
3922 tree
3923 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3924 enum tree_code *rescode_ptr)
3926 tree type;
3927 tree op0 = *op0_ptr;
3928 tree op1 = *op1_ptr;
3929 int unsignedp0, unsignedp1;
3930 int real1, real2;
3931 tree primop0, primop1;
3932 enum tree_code code = *rescode_ptr;
3933 location_t loc = EXPR_LOC_OR_HERE (op0);
3935 /* Throw away any conversions to wider types
3936 already present in the operands. */
3938 primop0 = c_common_get_narrower (op0, &unsignedp0);
3939 primop1 = c_common_get_narrower (op1, &unsignedp1);
3941 /* If primopN is first sign-extended from primopN's precision to opN's
3942 precision, then zero-extended from opN's precision to
3943 *restype_ptr precision, shortenings might be invalid. */
3944 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3945 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3946 && !unsignedp0
3947 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3948 primop0 = op0;
3949 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3950 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3951 && !unsignedp1
3952 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3953 primop1 = op1;
3955 /* Handle the case that OP0 does not *contain* a conversion
3956 but it *requires* conversion to FINAL_TYPE. */
3958 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3959 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3960 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3961 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3963 /* If one of the operands must be floated, we cannot optimize. */
3964 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3965 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3967 /* If first arg is constant, swap the args (changing operation
3968 so value is preserved), for canonicalization. Don't do this if
3969 the second arg is 0. */
3971 if (TREE_CONSTANT (primop0)
3972 && !integer_zerop (primop1) && !real_zerop (primop1)
3973 && !fixed_zerop (primop1))
3975 tree tem = primop0;
3976 int temi = unsignedp0;
3977 primop0 = primop1;
3978 primop1 = tem;
3979 tem = op0;
3980 op0 = op1;
3981 op1 = tem;
3982 *op0_ptr = op0;
3983 *op1_ptr = op1;
3984 unsignedp0 = unsignedp1;
3985 unsignedp1 = temi;
3986 temi = real1;
3987 real1 = real2;
3988 real2 = temi;
3990 switch (code)
3992 case LT_EXPR:
3993 code = GT_EXPR;
3994 break;
3995 case GT_EXPR:
3996 code = LT_EXPR;
3997 break;
3998 case LE_EXPR:
3999 code = GE_EXPR;
4000 break;
4001 case GE_EXPR:
4002 code = LE_EXPR;
4003 break;
4004 default:
4005 break;
4007 *rescode_ptr = code;
4010 /* If comparing an integer against a constant more bits wide,
4011 maybe we can deduce a value of 1 or 0 independent of the data.
4012 Or else truncate the constant now
4013 rather than extend the variable at run time.
4015 This is only interesting if the constant is the wider arg.
4016 Also, it is not safe if the constant is unsigned and the
4017 variable arg is signed, since in this case the variable
4018 would be sign-extended and then regarded as unsigned.
4019 Our technique fails in this case because the lowest/highest
4020 possible unsigned results don't follow naturally from the
4021 lowest/highest possible values of the variable operand.
4022 For just EQ_EXPR and NE_EXPR there is another technique that
4023 could be used: see if the constant can be faithfully represented
4024 in the other operand's type, by truncating it and reextending it
4025 and see if that preserves the constant's value. */
4027 if (!real1 && !real2
4028 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4029 && TREE_CODE (primop1) == INTEGER_CST
4030 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4032 int min_gt, max_gt, min_lt, max_lt;
4033 tree maxval, minval;
4034 /* 1 if comparison is nominally unsigned. */
4035 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4036 tree val;
4038 type = c_common_signed_or_unsigned_type (unsignedp0,
4039 TREE_TYPE (primop0));
4041 maxval = TYPE_MAX_VALUE (type);
4042 minval = TYPE_MIN_VALUE (type);
4044 if (unsignedp && !unsignedp0)
4045 *restype_ptr = c_common_signed_type (*restype_ptr);
4047 if (TREE_TYPE (primop1) != *restype_ptr)
4049 /* Convert primop1 to target type, but do not introduce
4050 additional overflow. We know primop1 is an int_cst. */
4051 primop1 = force_fit_type_double (*restype_ptr,
4052 tree_to_double_int (primop1),
4053 0, TREE_OVERFLOW (primop1));
4055 if (type != *restype_ptr)
4057 minval = convert (*restype_ptr, minval);
4058 maxval = convert (*restype_ptr, maxval);
4061 if (unsignedp && unsignedp0)
4063 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4064 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4065 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4066 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4068 else
4070 min_gt = INT_CST_LT (primop1, minval);
4071 max_gt = INT_CST_LT (primop1, maxval);
4072 min_lt = INT_CST_LT (minval, primop1);
4073 max_lt = INT_CST_LT (maxval, primop1);
4076 val = 0;
4077 /* This used to be a switch, but Genix compiler can't handle that. */
4078 if (code == NE_EXPR)
4080 if (max_lt || min_gt)
4081 val = truthvalue_true_node;
4083 else if (code == EQ_EXPR)
4085 if (max_lt || min_gt)
4086 val = truthvalue_false_node;
4088 else if (code == LT_EXPR)
4090 if (max_lt)
4091 val = truthvalue_true_node;
4092 if (!min_lt)
4093 val = truthvalue_false_node;
4095 else if (code == GT_EXPR)
4097 if (min_gt)
4098 val = truthvalue_true_node;
4099 if (!max_gt)
4100 val = truthvalue_false_node;
4102 else if (code == LE_EXPR)
4104 if (!max_gt)
4105 val = truthvalue_true_node;
4106 if (min_gt)
4107 val = truthvalue_false_node;
4109 else if (code == GE_EXPR)
4111 if (!min_lt)
4112 val = truthvalue_true_node;
4113 if (max_lt)
4114 val = truthvalue_false_node;
4117 /* If primop0 was sign-extended and unsigned comparison specd,
4118 we did a signed comparison above using the signed type bounds.
4119 But the comparison we output must be unsigned.
4121 Also, for inequalities, VAL is no good; but if the signed
4122 comparison had *any* fixed result, it follows that the
4123 unsigned comparison just tests the sign in reverse
4124 (positive values are LE, negative ones GE).
4125 So we can generate an unsigned comparison
4126 against an extreme value of the signed type. */
4128 if (unsignedp && !unsignedp0)
4130 if (val != 0)
4131 switch (code)
4133 case LT_EXPR:
4134 case GE_EXPR:
4135 primop1 = TYPE_MIN_VALUE (type);
4136 val = 0;
4137 break;
4139 case LE_EXPR:
4140 case GT_EXPR:
4141 primop1 = TYPE_MAX_VALUE (type);
4142 val = 0;
4143 break;
4145 default:
4146 break;
4148 type = c_common_unsigned_type (type);
4151 if (TREE_CODE (primop0) != INTEGER_CST
4152 && c_inhibit_evaluation_warnings == 0)
4154 if (val == truthvalue_false_node)
4155 warning_at (loc, OPT_Wtype_limits,
4156 "comparison is always false due to limited range of data type");
4157 if (val == truthvalue_true_node)
4158 warning_at (loc, OPT_Wtype_limits,
4159 "comparison is always true due to limited range of data type");
4162 if (val != 0)
4164 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4165 if (TREE_SIDE_EFFECTS (primop0))
4166 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4167 return val;
4170 /* Value is not predetermined, but do the comparison
4171 in the type of the operand that is not constant.
4172 TYPE is already properly set. */
4175 /* If either arg is decimal float and the other is float, find the
4176 proper common type to use for comparison. */
4177 else if (real1 && real2
4178 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4179 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4180 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4182 else if (real1 && real2
4183 && (TYPE_PRECISION (TREE_TYPE (primop0))
4184 == TYPE_PRECISION (TREE_TYPE (primop1))))
4185 type = TREE_TYPE (primop0);
4187 /* If args' natural types are both narrower than nominal type
4188 and both extend in the same manner, compare them
4189 in the type of the wider arg.
4190 Otherwise must actually extend both to the nominal
4191 common type lest different ways of extending
4192 alter the result.
4193 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4195 else if (unsignedp0 == unsignedp1 && real1 == real2
4196 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4197 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4199 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4200 type = c_common_signed_or_unsigned_type (unsignedp0
4201 || TYPE_UNSIGNED (*restype_ptr),
4202 type);
4203 /* Make sure shorter operand is extended the right way
4204 to match the longer operand. */
4205 primop0
4206 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4207 TREE_TYPE (primop0)),
4208 primop0);
4209 primop1
4210 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4211 TREE_TYPE (primop1)),
4212 primop1);
4214 else
4216 /* Here we must do the comparison on the nominal type
4217 using the args exactly as we received them. */
4218 type = *restype_ptr;
4219 primop0 = op0;
4220 primop1 = op1;
4222 if (!real1 && !real2 && integer_zerop (primop1)
4223 && TYPE_UNSIGNED (*restype_ptr))
4225 tree value = 0;
4226 /* All unsigned values are >= 0, so we warn. However,
4227 if OP0 is a constant that is >= 0, the signedness of
4228 the comparison isn't an issue, so suppress the
4229 warning. */
4230 bool warn =
4231 warn_type_limits && !in_system_header
4232 && c_inhibit_evaluation_warnings == 0
4233 && !(TREE_CODE (primop0) == INTEGER_CST
4234 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4235 primop0)))
4236 /* Do not warn for enumeration types. */
4237 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4239 switch (code)
4241 case GE_EXPR:
4242 if (warn)
4243 warning_at (loc, OPT_Wtype_limits,
4244 "comparison of unsigned expression >= 0 is always true");
4245 value = truthvalue_true_node;
4246 break;
4248 case LT_EXPR:
4249 if (warn)
4250 warning_at (loc, OPT_Wtype_limits,
4251 "comparison of unsigned expression < 0 is always false");
4252 value = truthvalue_false_node;
4253 break;
4255 default:
4256 break;
4259 if (value != 0)
4261 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4262 if (TREE_SIDE_EFFECTS (primop0))
4263 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4264 primop0, value);
4265 return value;
4270 *op0_ptr = convert (type, primop0);
4271 *op1_ptr = convert (type, primop1);
4273 *restype_ptr = truthvalue_type_node;
4275 return 0;
4278 /* Return a tree for the sum or difference (RESULTCODE says which)
4279 of pointer PTROP and integer INTOP. */
4281 tree
4282 pointer_int_sum (location_t loc, enum tree_code resultcode,
4283 tree ptrop, tree intop)
4285 tree size_exp, ret;
4287 /* The result is a pointer of the same type that is being added. */
4288 tree result_type = TREE_TYPE (ptrop);
4290 /* If the pointer lives in UPC shared memory, then
4291 drop the 'shared' qualifier. */
4292 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4293 result_type = build_upc_unshared_type (result_type);
4295 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4297 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4298 "pointer of type %<void *%> used in arithmetic");
4299 size_exp = integer_one_node;
4301 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4303 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4304 "pointer to a function used in arithmetic");
4305 size_exp = integer_one_node;
4307 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4309 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4310 "pointer to member function used in arithmetic");
4311 size_exp = integer_one_node;
4313 else
4314 size_exp = size_in_bytes (TREE_TYPE (result_type));
4316 /* We are manipulating pointer values, so we don't need to warn
4317 about relying on undefined signed overflow. We disable the
4318 warning here because we use integer types so fold won't know that
4319 they are really pointers. */
4320 fold_defer_overflow_warnings ();
4322 /* If what we are about to multiply by the size of the elements
4323 contains a constant term, apply distributive law
4324 and multiply that constant term separately.
4325 This helps produce common subexpressions. */
4326 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4327 && !TREE_CONSTANT (intop)
4328 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4329 && TREE_CONSTANT (size_exp)
4330 /* If the constant comes from pointer subtraction,
4331 skip this optimization--it would cause an error. */
4332 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4333 /* If the constant is unsigned, and smaller than the pointer size,
4334 then we must skip this optimization. This is because it could cause
4335 an overflow error if the constant is negative but INTOP is not. */
4336 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4337 || (TYPE_PRECISION (TREE_TYPE (intop))
4338 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4340 enum tree_code subcode = resultcode;
4341 tree int_type = TREE_TYPE (intop);
4342 if (TREE_CODE (intop) == MINUS_EXPR)
4343 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4344 /* Convert both subexpression types to the type of intop,
4345 because weird cases involving pointer arithmetic
4346 can result in a sum or difference with different type args. */
4347 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4348 subcode, ptrop,
4349 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4350 intop = convert (int_type, TREE_OPERAND (intop, 0));
4353 /* Convert the integer argument to a type the same size as sizetype
4354 so the multiply won't overflow spuriously. */
4355 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4356 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4357 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4358 TYPE_UNSIGNED (sizetype)), intop);
4360 /* Replace the integer argument with a suitable product by the object size.
4361 Do this multiplication as signed, then convert to the appropriate type
4362 for the pointer operation and disregard an overflow that occurred only
4363 because of the sign-extension change in the latter conversion. */
4365 tree t = build_binary_op (loc,
4366 MULT_EXPR, intop,
4367 convert (TREE_TYPE (intop), size_exp), 1);
4368 intop = convert (sizetype, t);
4369 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4370 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4371 TREE_INT_CST_HIGH (intop));
4374 /* Create the sum or difference. */
4375 if (resultcode == MINUS_EXPR)
4376 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4378 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4380 fold_undefer_and_ignore_overflow_warnings ();
4382 return ret;
4385 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4386 and if NON_CONST is known not to be permitted in an evaluated part
4387 of a constant expression. */
4389 tree
4390 c_wrap_maybe_const (tree expr, bool non_const)
4392 bool nowarning = TREE_NO_WARNING (expr);
4393 location_t loc = EXPR_LOCATION (expr);
4395 /* This should never be called for C++. */
4396 if (c_dialect_cxx ())
4397 gcc_unreachable ();
4399 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4400 STRIP_TYPE_NOPS (expr);
4401 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4402 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4403 if (nowarning)
4404 TREE_NO_WARNING (expr) = 1;
4405 protected_set_expr_location (expr, loc);
4407 return expr;
4410 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4411 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4412 around the SAVE_EXPR if needed so that c_fully_fold does not need
4413 to look inside SAVE_EXPRs. */
4415 tree
4416 c_save_expr (tree expr)
4418 bool maybe_const = true;
4419 if (c_dialect_cxx ())
4420 return save_expr (expr);
4421 expr = c_fully_fold (expr, false, &maybe_const);
4422 expr = save_expr (expr);
4423 if (!maybe_const)
4424 expr = c_wrap_maybe_const (expr, true);
4425 return expr;
4428 /* Return whether EXPR is a declaration whose address can never be
4429 NULL. */
4431 bool
4432 decl_with_nonnull_addr_p (const_tree expr)
4434 return (DECL_P (expr)
4435 && (TREE_CODE (expr) == PARM_DECL
4436 || TREE_CODE (expr) == LABEL_DECL
4437 || !DECL_WEAK (expr)));
4440 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4441 or for an `if' or `while' statement or ?..: exp. It should already
4442 have been validated to be of suitable type; otherwise, a bad
4443 diagnostic may result.
4445 The EXPR is located at LOCATION.
4447 This preparation consists of taking the ordinary
4448 representation of an expression expr and producing a valid tree
4449 boolean expression describing whether expr is nonzero. We could
4450 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4451 but we optimize comparisons, &&, ||, and !.
4453 The resulting type should always be `truthvalue_type_node'. */
4455 tree
4456 c_common_truthvalue_conversion (location_t location, tree expr)
4458 switch (TREE_CODE (expr))
4460 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4461 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4462 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4463 case ORDERED_EXPR: case UNORDERED_EXPR:
4464 if (TREE_TYPE (expr) == truthvalue_type_node)
4465 return expr;
4466 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4467 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4468 goto ret;
4470 case TRUTH_ANDIF_EXPR:
4471 case TRUTH_ORIF_EXPR:
4472 case TRUTH_AND_EXPR:
4473 case TRUTH_OR_EXPR:
4474 case TRUTH_XOR_EXPR:
4475 if (TREE_TYPE (expr) == truthvalue_type_node)
4476 return expr;
4477 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4478 c_common_truthvalue_conversion (location,
4479 TREE_OPERAND (expr, 0)),
4480 c_common_truthvalue_conversion (location,
4481 TREE_OPERAND (expr, 1)));
4482 goto ret;
4484 case TRUTH_NOT_EXPR:
4485 if (TREE_TYPE (expr) == truthvalue_type_node)
4486 return expr;
4487 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4488 c_common_truthvalue_conversion (location,
4489 TREE_OPERAND (expr, 0)));
4490 goto ret;
4492 case ERROR_MARK:
4493 return expr;
4495 case INTEGER_CST:
4496 return integer_zerop (expr) ? truthvalue_false_node
4497 : truthvalue_true_node;
4499 case REAL_CST:
4500 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4501 ? truthvalue_true_node
4502 : truthvalue_false_node;
4504 case FIXED_CST:
4505 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4506 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4507 ? truthvalue_true_node
4508 : truthvalue_false_node;
4510 case FUNCTION_DECL:
4511 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4512 /* Fall through. */
4514 case ADDR_EXPR:
4516 tree inner = TREE_OPERAND (expr, 0);
4517 if (decl_with_nonnull_addr_p (inner))
4519 /* Common Ada/Pascal programmer's mistake. */
4520 warning_at (location,
4521 OPT_Waddress,
4522 "the address of %qD will always evaluate as %<true%>",
4523 inner);
4524 return truthvalue_true_node;
4526 break;
4529 case COMPLEX_EXPR:
4530 expr = build_binary_op (EXPR_LOCATION (expr),
4531 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4532 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4533 c_common_truthvalue_conversion (location,
4534 TREE_OPERAND (expr, 0)),
4535 c_common_truthvalue_conversion (location,
4536 TREE_OPERAND (expr, 1)),
4538 goto ret;
4540 case NEGATE_EXPR:
4541 case ABS_EXPR:
4542 case FLOAT_EXPR:
4543 case EXCESS_PRECISION_EXPR:
4544 /* These don't change whether an object is nonzero or zero. */
4545 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4547 case LROTATE_EXPR:
4548 case RROTATE_EXPR:
4549 /* These don't change whether an object is zero or nonzero, but
4550 we can't ignore them if their second arg has side-effects. */
4551 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4553 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4554 TREE_OPERAND (expr, 1),
4555 c_common_truthvalue_conversion
4556 (location, TREE_OPERAND (expr, 0)));
4557 goto ret;
4559 else
4560 return c_common_truthvalue_conversion (location,
4561 TREE_OPERAND (expr, 0));
4563 case COND_EXPR:
4564 /* Distribute the conversion into the arms of a COND_EXPR. */
4565 if (c_dialect_cxx ())
4567 tree op1 = TREE_OPERAND (expr, 1);
4568 tree op2 = TREE_OPERAND (expr, 2);
4569 /* In C++ one of the arms might have void type if it is throw. */
4570 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4571 op1 = c_common_truthvalue_conversion (location, op1);
4572 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4573 op2 = c_common_truthvalue_conversion (location, op2);
4574 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4575 TREE_OPERAND (expr, 0), op1, op2);
4576 goto ret;
4578 else
4580 /* Folding will happen later for C. */
4581 expr = build3 (COND_EXPR, truthvalue_type_node,
4582 TREE_OPERAND (expr, 0),
4583 c_common_truthvalue_conversion (location,
4584 TREE_OPERAND (expr, 1)),
4585 c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 2)));
4587 goto ret;
4590 CASE_CONVERT:
4592 tree totype = TREE_TYPE (expr);
4593 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4595 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4596 since that affects how `default_conversion' will behave. */
4597 if (TREE_CODE (totype) == REFERENCE_TYPE
4598 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4599 break;
4600 /* Don't strip a conversion from C++0x scoped enum, since they
4601 don't implicitly convert to other types. */
4602 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4603 && ENUM_IS_SCOPED (fromtype))
4604 break;
4605 /* If this isn't narrowing the argument, we can ignore it. */
4606 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4607 return c_common_truthvalue_conversion (location,
4608 TREE_OPERAND (expr, 0));
4610 break;
4612 case MODIFY_EXPR:
4613 if (!TREE_NO_WARNING (expr)
4614 && warn_parentheses)
4616 warning (OPT_Wparentheses,
4617 "suggest parentheses around assignment used as truth value");
4618 TREE_NO_WARNING (expr) = 1;
4620 break;
4622 default:
4623 break;
4626 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4628 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4629 expr = (build_binary_op
4630 (EXPR_LOCATION (expr),
4631 (TREE_SIDE_EFFECTS (expr)
4632 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4633 c_common_truthvalue_conversion
4634 (location,
4635 build_unary_op (location, REALPART_EXPR, t, 0)),
4636 c_common_truthvalue_conversion
4637 (location,
4638 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4639 0));
4640 goto ret;
4643 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4645 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4646 FCONST0 (TYPE_MODE
4647 (TREE_TYPE (expr))));
4648 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4650 else
4651 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4653 ret:
4654 protected_set_expr_location (expr, location);
4655 return expr;
4658 static void def_builtin_1 (enum built_in_function fncode,
4659 const char *name,
4660 enum built_in_class fnclass,
4661 tree fntype, tree libtype,
4662 bool both_p, bool fallback_p, bool nonansi_p,
4663 tree fnattrs, bool implicit_p);
4666 /* Apply the TYPE_QUALS to the new DECL. */
4668 void
4669 c_apply_type_quals_to_decl (int type_quals, tree decl)
4671 tree type = TREE_TYPE (decl);
4673 if (type == error_mark_node)
4674 return;
4676 if ((type_quals & TYPE_QUAL_CONST)
4677 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4678 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4679 constructor can produce constant init, so rely on cp_finish_decl to
4680 clear TREE_READONLY if the variable has non-constant init. */
4681 TREE_READONLY (decl) = 1;
4682 if (type_quals & TYPE_QUAL_VOLATILE)
4684 TREE_SIDE_EFFECTS (decl) = 1;
4685 TREE_THIS_VOLATILE (decl) = 1;
4687 if (type_quals & TYPE_QUAL_RESTRICT)
4689 while (type && TREE_CODE (type) == ARRAY_TYPE)
4690 /* Allow 'restrict' on arrays of pointers.
4691 FIXME currently we just ignore it. */
4692 type = TREE_TYPE (type);
4693 if (!type
4694 || !POINTER_TYPE_P (type)
4695 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4696 error ("invalid use of %<restrict%>");
4698 if (type_quals & TYPE_QUAL_SHARED)
4700 TREE_SHARED (decl) = 1;
4701 if (type_quals & TYPE_QUAL_STRICT)
4702 TREE_STRICT(decl) = 1;
4703 else if (type_quals & TYPE_QUAL_RELAXED)
4704 TREE_RELAXED(decl) = 1;
4705 /* The declaration's type should have been previously defined
4706 as a UPC shared type. */
4707 gcc_assert (upc_shared_type_p (type));
4711 /* Hash function for the problem of multiple type definitions in
4712 different files. This must hash all types that will compare
4713 equal via comptypes to the same value. In practice it hashes
4714 on some of the simple stuff and leaves the details to comptypes. */
4716 static hashval_t
4717 c_type_hash (const void *p)
4719 int n_elements;
4720 int shift, size;
4721 const_tree const t = (const_tree) p;
4722 tree t2;
4723 switch (TREE_CODE (t))
4725 /* For pointers, hash on pointee type plus some swizzling. */
4726 case POINTER_TYPE:
4727 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4728 /* Hash on number of elements and total size. */
4729 case ENUMERAL_TYPE:
4730 shift = 3;
4731 t2 = TYPE_VALUES (t);
4732 break;
4733 case RECORD_TYPE:
4734 shift = 0;
4735 t2 = TYPE_FIELDS (t);
4736 break;
4737 case QUAL_UNION_TYPE:
4738 shift = 1;
4739 t2 = TYPE_FIELDS (t);
4740 break;
4741 case UNION_TYPE:
4742 shift = 2;
4743 t2 = TYPE_FIELDS (t);
4744 break;
4745 default:
4746 gcc_unreachable ();
4748 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4749 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4750 n_elements = list_length (t2);
4751 /* We might have a VLA here. */
4752 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4753 size = 0;
4754 else
4755 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4756 return ((size << 24) | (n_elements << shift));
4759 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4761 /* Return the typed-based alias set for T, which may be an expression
4762 or a type. Return -1 if we don't do anything special. */
4764 alias_set_type
4765 c_common_get_alias_set (tree t)
4767 tree u;
4768 PTR *slot;
4770 /* For VLAs, use the alias set of the element type rather than the
4771 default of alias set 0 for types compared structurally. */
4772 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4774 if (TREE_CODE (t) == ARRAY_TYPE)
4775 return get_alias_set (TREE_TYPE (t));
4776 return -1;
4779 /* Permit type-punning when accessing a union, provided the access
4780 is directly through the union. For example, this code does not
4781 permit taking the address of a union member and then storing
4782 through it. Even the type-punning allowed here is a GCC
4783 extension, albeit a common and useful one; the C standard says
4784 that such accesses have implementation-defined behavior. */
4785 for (u = t;
4786 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4787 u = TREE_OPERAND (u, 0))
4788 if (TREE_CODE (u) == COMPONENT_REF
4789 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4790 return 0;
4792 /* That's all the expressions we handle specially. */
4793 if (!TYPE_P (t))
4794 return -1;
4796 /* The C standard guarantees that any object may be accessed via an
4797 lvalue that has character type. */
4798 if (t == char_type_node
4799 || t == signed_char_type_node
4800 || t == unsigned_char_type_node)
4801 return 0;
4803 /* The C standard specifically allows aliasing between signed and
4804 unsigned variants of the same type. We treat the signed
4805 variant as canonical. */
4806 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4808 tree t1 = c_common_signed_type (t);
4810 /* t1 == t can happen for boolean nodes which are always unsigned. */
4811 if (t1 != t)
4812 return get_alias_set (t1);
4815 /* Handle the case of multiple type nodes referring to "the same" type,
4816 which occurs with IMA. These share an alias set. FIXME: Currently only
4817 C90 is handled. (In C99 type compatibility is not transitive, which
4818 complicates things mightily. The alias set splay trees can theoretically
4819 represent this, but insertion is tricky when you consider all the
4820 different orders things might arrive in.) */
4822 if (c_language != clk_c || flag_isoc99)
4823 return -1;
4825 /* Save time if there's only one input file. */
4826 if (num_in_fnames == 1)
4827 return -1;
4829 /* Pointers need special handling if they point to any type that
4830 needs special handling (below). */
4831 if (TREE_CODE (t) == POINTER_TYPE)
4833 tree t2;
4834 /* Find bottom type under any nested POINTERs. */
4835 for (t2 = TREE_TYPE (t);
4836 TREE_CODE (t2) == POINTER_TYPE;
4837 t2 = TREE_TYPE (t2))
4839 if (TREE_CODE (t2) != RECORD_TYPE
4840 && TREE_CODE (t2) != ENUMERAL_TYPE
4841 && TREE_CODE (t2) != QUAL_UNION_TYPE
4842 && TREE_CODE (t2) != UNION_TYPE)
4843 return -1;
4844 if (TYPE_SIZE (t2) == 0)
4845 return -1;
4847 /* These are the only cases that need special handling. */
4848 if (TREE_CODE (t) != RECORD_TYPE
4849 && TREE_CODE (t) != ENUMERAL_TYPE
4850 && TREE_CODE (t) != QUAL_UNION_TYPE
4851 && TREE_CODE (t) != UNION_TYPE
4852 && TREE_CODE (t) != POINTER_TYPE)
4853 return -1;
4854 /* Undefined? */
4855 if (TYPE_SIZE (t) == 0)
4856 return -1;
4858 /* Look up t in hash table. Only one of the compatible types within each
4859 alias set is recorded in the table. */
4860 if (!type_hash_table)
4861 type_hash_table = htab_create_ggc (1021, c_type_hash,
4862 (htab_eq) lang_hooks.types_compatible_p,
4863 NULL);
4864 slot = htab_find_slot (type_hash_table, t, INSERT);
4865 if (*slot != NULL)
4867 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4868 return TYPE_ALIAS_SET ((tree)*slot);
4870 else
4871 /* Our caller will assign and record (in t) a new alias set; all we need
4872 to do is remember t in the hash table. */
4873 *slot = t;
4875 return -1;
4878 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4879 the second parameter indicates which OPERATOR is being applied.
4880 The COMPLAIN flag controls whether we should diagnose possibly
4881 ill-formed constructs or not. LOC is the location of the SIZEOF or
4882 TYPEOF operator. */
4884 tree
4885 c_sizeof_or_alignof_type (location_t loc,
4886 tree type, bool is_sizeof, int complain)
4888 const char *op_name;
4889 tree value = NULL;
4890 enum tree_code type_code = TREE_CODE (type);
4892 op_name = is_sizeof ? "sizeof" : "__alignof__";
4894 if (type_code == FUNCTION_TYPE)
4896 if (is_sizeof)
4898 if (complain && (pedantic || warn_pointer_arith))
4899 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4900 "invalid application of %<sizeof%> to a function type");
4901 else if (!complain)
4902 return error_mark_node;
4903 value = size_one_node;
4905 else
4907 if (complain)
4909 if (c_dialect_cxx ())
4910 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4911 "%<alignof%> applied to a function type");
4912 else
4913 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4914 "%<_Alignof%> applied to a function type");
4916 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4919 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4921 if (complain)
4923 if (type_code == VOID_TYPE && upc_shared_type_p (type))
4924 error_at (loc, "invalid application of %qs"
4925 " to %<shared void%> type", op_name);
4926 else if (type_code == VOID_TYPE
4927 && (pedantic || warn_pointer_arith))
4928 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4929 "invalid application of %qs to a void type", op_name);
4931 else
4932 return error_mark_node;
4933 value = size_one_node;
4935 else if (!COMPLETE_TYPE_P (type)
4936 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4938 if (complain)
4939 error_at (loc, "invalid application of %qs to incomplete type %qT",
4940 op_name, type);
4941 return error_mark_node;
4943 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4944 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4946 if (complain)
4947 error_at (loc, "invalid application of %qs to array type %qT of "
4948 "incomplete element type", op_name, type);
4949 return error_mark_node;
4951 else
4953 if (is_sizeof)
4954 /* Convert in case a char is more than one unit. */
4955 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4956 size_int (TYPE_PRECISION (char_type_node)
4957 / BITS_PER_UNIT));
4958 else
4959 value = size_int (TYPE_ALIGN_UNIT (type));
4962 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
4963 && upc_shared_type_p (type)
4964 && TYPE_HAS_THREADS_FACTOR (type))
4966 const tree n_threads = convert (sizetype, upc_num_threads ());
4967 value = size_binop (MULT_EXPR, value, n_threads);
4970 /* VALUE will have the middle-end integer type sizetype.
4971 However, we should really return a value of type `size_t',
4972 which is just a typedef for an ordinary integer type. */
4973 value = fold_convert_loc (loc, size_type_node, value);
4975 return value;
4978 /* Implement the __alignof keyword: Return the minimum required
4979 alignment of EXPR, measured in bytes. For VAR_DECLs,
4980 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4981 from an "aligned" __attribute__ specification). LOC is the
4982 location of the ALIGNOF operator. */
4984 tree
4985 c_alignof_expr (location_t loc, tree expr)
4987 tree t;
4989 if (VAR_OR_FUNCTION_DECL_P (expr))
4990 t = size_int (DECL_ALIGN_UNIT (expr));
4992 else if (TREE_CODE (expr) == COMPONENT_REF
4993 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4995 error_at (loc, "%<__alignof%> applied to a bit-field");
4996 t = size_one_node;
4998 else if (TREE_CODE (expr) == COMPONENT_REF
4999 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5000 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5002 else if (TREE_CODE (expr) == INDIRECT_REF)
5004 tree t = TREE_OPERAND (expr, 0);
5005 tree best = t;
5006 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5008 while (CONVERT_EXPR_P (t)
5009 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5011 int thisalign;
5013 t = TREE_OPERAND (t, 0);
5014 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5015 if (thisalign > bestalign)
5016 best = t, bestalign = thisalign;
5018 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5020 else
5021 return c_alignof (loc, TREE_TYPE (expr));
5023 return fold_convert_loc (loc, size_type_node, t);
5026 /* Handle C and C++ default attributes. */
5028 enum built_in_attribute
5030 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5031 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5032 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5033 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5034 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5035 #include "builtin-attrs.def"
5036 #undef DEF_ATTR_NULL_TREE
5037 #undef DEF_ATTR_INT
5038 #undef DEF_ATTR_STRING
5039 #undef DEF_ATTR_IDENT
5040 #undef DEF_ATTR_TREE_LIST
5041 ATTR_LAST
5044 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5046 static void c_init_attributes (void);
5048 enum c_builtin_type
5050 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5051 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5052 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5053 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5054 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5055 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5056 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5057 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5058 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5059 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5060 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5061 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5062 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5063 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5064 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5065 NAME,
5066 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5067 #include "builtin-types.def"
5068 #undef DEF_PRIMITIVE_TYPE
5069 #undef DEF_FUNCTION_TYPE_0
5070 #undef DEF_FUNCTION_TYPE_1
5071 #undef DEF_FUNCTION_TYPE_2
5072 #undef DEF_FUNCTION_TYPE_3
5073 #undef DEF_FUNCTION_TYPE_4
5074 #undef DEF_FUNCTION_TYPE_5
5075 #undef DEF_FUNCTION_TYPE_6
5076 #undef DEF_FUNCTION_TYPE_7
5077 #undef DEF_FUNCTION_TYPE_VAR_0
5078 #undef DEF_FUNCTION_TYPE_VAR_1
5079 #undef DEF_FUNCTION_TYPE_VAR_2
5080 #undef DEF_FUNCTION_TYPE_VAR_3
5081 #undef DEF_FUNCTION_TYPE_VAR_4
5082 #undef DEF_FUNCTION_TYPE_VAR_5
5083 #undef DEF_POINTER_TYPE
5084 BT_LAST
5087 typedef enum c_builtin_type builtin_type;
5089 /* A temporary array for c_common_nodes_and_builtins. Used in
5090 communication with def_fn_type. */
5091 static tree builtin_types[(int) BT_LAST + 1];
5093 /* A helper function for c_common_nodes_and_builtins. Build function type
5094 for DEF with return type RET and N arguments. If VAR is true, then the
5095 function should be variadic after those N arguments.
5097 Takes special care not to ICE if any of the types involved are
5098 error_mark_node, which indicates that said type is not in fact available
5099 (see builtin_type_for_size). In which case the function type as a whole
5100 should be error_mark_node. */
5102 static void
5103 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5105 tree t;
5106 tree *args = XALLOCAVEC (tree, n);
5107 va_list list;
5108 int i;
5110 va_start (list, n);
5111 for (i = 0; i < n; ++i)
5113 builtin_type a = (builtin_type) va_arg (list, int);
5114 t = builtin_types[a];
5115 if (t == error_mark_node)
5116 goto egress;
5117 args[i] = t;
5120 t = builtin_types[ret];
5121 if (t == error_mark_node)
5122 goto egress;
5123 if (var)
5124 t = build_varargs_function_type_array (t, n, args);
5125 else
5126 t = build_function_type_array (t, n, args);
5128 egress:
5129 builtin_types[def] = t;
5130 va_end (list);
5133 /* Build builtin functions common to both C and C++ language
5134 frontends. */
5136 static void
5137 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5139 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5140 builtin_types[ENUM] = VALUE;
5141 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5142 def_fn_type (ENUM, RETURN, 0, 0);
5143 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5144 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5145 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5146 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5147 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5148 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5149 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5150 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5151 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5152 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5153 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5154 ARG6) \
5155 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5156 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5157 ARG6, ARG7) \
5158 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5159 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5160 def_fn_type (ENUM, RETURN, 1, 0);
5161 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5162 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5163 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5164 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5165 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5166 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5167 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5168 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5169 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5170 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5171 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5172 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5174 #include "builtin-types.def"
5176 #undef DEF_PRIMITIVE_TYPE
5177 #undef DEF_FUNCTION_TYPE_1
5178 #undef DEF_FUNCTION_TYPE_2
5179 #undef DEF_FUNCTION_TYPE_3
5180 #undef DEF_FUNCTION_TYPE_4
5181 #undef DEF_FUNCTION_TYPE_5
5182 #undef DEF_FUNCTION_TYPE_6
5183 #undef DEF_FUNCTION_TYPE_VAR_0
5184 #undef DEF_FUNCTION_TYPE_VAR_1
5185 #undef DEF_FUNCTION_TYPE_VAR_2
5186 #undef DEF_FUNCTION_TYPE_VAR_3
5187 #undef DEF_FUNCTION_TYPE_VAR_4
5188 #undef DEF_FUNCTION_TYPE_VAR_5
5189 #undef DEF_POINTER_TYPE
5190 builtin_types[(int) BT_LAST] = NULL_TREE;
5192 c_init_attributes ();
5194 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5195 NONANSI_P, ATTRS, IMPLICIT, COND) \
5196 if (NAME && COND) \
5197 def_builtin_1 (ENUM, NAME, CLASS, \
5198 builtin_types[(int) TYPE], \
5199 builtin_types[(int) LIBTYPE], \
5200 BOTH_P, FALLBACK_P, NONANSI_P, \
5201 built_in_attributes[(int) ATTRS], IMPLICIT);
5202 #include "builtins.def"
5203 #undef DEF_BUILTIN
5205 targetm.init_builtins ();
5207 build_common_builtin_nodes ();
5209 if (flag_mudflap)
5210 mudflap_init ();
5213 /* Like get_identifier, but avoid warnings about null arguments when
5214 the argument may be NULL for targets where GCC lacks stdint.h type
5215 information. */
5217 static inline tree
5218 c_get_ident (const char *id)
5220 return get_identifier (id);
5223 /* Build tree nodes and builtin functions common to both C and C++ language
5224 frontends. */
5226 void
5227 c_common_nodes_and_builtins (void)
5229 int char16_type_size;
5230 int char32_type_size;
5231 int wchar_type_size;
5232 tree array_domain_type;
5233 tree va_list_ref_type_node;
5234 tree va_list_arg_type_node;
5236 build_common_tree_nodes (flag_signed_char, flag_short_double);
5238 /* Define `int' and `char' first so that dbx will output them first. */
5239 record_builtin_type (RID_INT, NULL, integer_type_node);
5240 record_builtin_type (RID_CHAR, "char", char_type_node);
5242 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5243 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5244 but not C. Are the conditionals here needed? */
5245 if (c_dialect_cxx ())
5246 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5247 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5248 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5249 record_builtin_type (RID_MAX, "long unsigned int",
5250 long_unsigned_type_node);
5251 if (int128_integer_type_node != NULL_TREE)
5253 record_builtin_type (RID_INT128, "__int128",
5254 int128_integer_type_node);
5255 record_builtin_type (RID_MAX, "__int128 unsigned",
5256 int128_unsigned_type_node);
5258 if (c_dialect_cxx ())
5259 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5260 record_builtin_type (RID_MAX, "long long int",
5261 long_long_integer_type_node);
5262 record_builtin_type (RID_MAX, "long long unsigned int",
5263 long_long_unsigned_type_node);
5264 if (c_dialect_cxx ())
5265 record_builtin_type (RID_MAX, "long long unsigned",
5266 long_long_unsigned_type_node);
5267 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5268 record_builtin_type (RID_MAX, "short unsigned int",
5269 short_unsigned_type_node);
5270 if (c_dialect_cxx ())
5271 record_builtin_type (RID_MAX, "unsigned short",
5272 short_unsigned_type_node);
5274 /* Define both `signed char' and `unsigned char'. */
5275 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5276 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5278 /* These are types that c_common_type_for_size and
5279 c_common_type_for_mode use. */
5280 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5281 TYPE_DECL, NULL_TREE,
5282 intQI_type_node));
5283 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5284 TYPE_DECL, NULL_TREE,
5285 intHI_type_node));
5286 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5287 TYPE_DECL, NULL_TREE,
5288 intSI_type_node));
5289 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5290 TYPE_DECL, NULL_TREE,
5291 intDI_type_node));
5292 #if HOST_BITS_PER_WIDE_INT >= 64
5293 if (targetm.scalar_mode_supported_p (TImode))
5294 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5295 TYPE_DECL,
5296 get_identifier ("__int128_t"),
5297 intTI_type_node));
5298 #endif
5299 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5300 TYPE_DECL, NULL_TREE,
5301 unsigned_intQI_type_node));
5302 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5303 TYPE_DECL, NULL_TREE,
5304 unsigned_intHI_type_node));
5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL, NULL_TREE,
5307 unsigned_intSI_type_node));
5308 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5309 TYPE_DECL, NULL_TREE,
5310 unsigned_intDI_type_node));
5311 #if HOST_BITS_PER_WIDE_INT >= 64
5312 if (targetm.scalar_mode_supported_p (TImode))
5313 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5314 TYPE_DECL,
5315 get_identifier ("__uint128_t"),
5316 unsigned_intTI_type_node));
5317 #endif
5319 /* Create the widest literal types. */
5320 widest_integer_literal_type_node
5321 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5322 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5323 TYPE_DECL, NULL_TREE,
5324 widest_integer_literal_type_node));
5326 widest_unsigned_literal_type_node
5327 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5328 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5329 TYPE_DECL, NULL_TREE,
5330 widest_unsigned_literal_type_node));
5332 signed_size_type_node = c_common_signed_type (size_type_node);
5334 pid_type_node =
5335 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5337 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5338 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5339 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5341 /* Only supported decimal floating point extension if the target
5342 actually supports underlying modes. */
5343 if (targetm.scalar_mode_supported_p (SDmode)
5344 && targetm.scalar_mode_supported_p (DDmode)
5345 && targetm.scalar_mode_supported_p (TDmode))
5347 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5348 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5349 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5352 if (targetm.fixed_point_supported_p ())
5354 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5355 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5356 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5357 record_builtin_type (RID_MAX, "long long _Fract",
5358 long_long_fract_type_node);
5359 record_builtin_type (RID_MAX, "unsigned short _Fract",
5360 unsigned_short_fract_type_node);
5361 record_builtin_type (RID_MAX, "unsigned _Fract",
5362 unsigned_fract_type_node);
5363 record_builtin_type (RID_MAX, "unsigned long _Fract",
5364 unsigned_long_fract_type_node);
5365 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5366 unsigned_long_long_fract_type_node);
5367 record_builtin_type (RID_MAX, "_Sat short _Fract",
5368 sat_short_fract_type_node);
5369 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5370 record_builtin_type (RID_MAX, "_Sat long _Fract",
5371 sat_long_fract_type_node);
5372 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5373 sat_long_long_fract_type_node);
5374 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5375 sat_unsigned_short_fract_type_node);
5376 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5377 sat_unsigned_fract_type_node);
5378 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5379 sat_unsigned_long_fract_type_node);
5380 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5381 sat_unsigned_long_long_fract_type_node);
5382 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5383 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5384 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5385 record_builtin_type (RID_MAX, "long long _Accum",
5386 long_long_accum_type_node);
5387 record_builtin_type (RID_MAX, "unsigned short _Accum",
5388 unsigned_short_accum_type_node);
5389 record_builtin_type (RID_MAX, "unsigned _Accum",
5390 unsigned_accum_type_node);
5391 record_builtin_type (RID_MAX, "unsigned long _Accum",
5392 unsigned_long_accum_type_node);
5393 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5394 unsigned_long_long_accum_type_node);
5395 record_builtin_type (RID_MAX, "_Sat short _Accum",
5396 sat_short_accum_type_node);
5397 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5398 record_builtin_type (RID_MAX, "_Sat long _Accum",
5399 sat_long_accum_type_node);
5400 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5401 sat_long_long_accum_type_node);
5402 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5403 sat_unsigned_short_accum_type_node);
5404 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5405 sat_unsigned_accum_type_node);
5406 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5407 sat_unsigned_long_accum_type_node);
5408 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5409 sat_unsigned_long_long_accum_type_node);
5413 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5414 TYPE_DECL,
5415 get_identifier ("complex int"),
5416 complex_integer_type_node));
5417 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5418 TYPE_DECL,
5419 get_identifier ("complex float"),
5420 complex_float_type_node));
5421 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5422 TYPE_DECL,
5423 get_identifier ("complex double"),
5424 complex_double_type_node));
5425 lang_hooks.decls.pushdecl
5426 (build_decl (UNKNOWN_LOCATION,
5427 TYPE_DECL, get_identifier ("complex long double"),
5428 complex_long_double_type_node));
5430 if (c_dialect_cxx ())
5431 /* For C++, make fileptr_type_node a distinct void * type until
5432 FILE type is defined. */
5433 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5435 record_builtin_type (RID_VOID, NULL, void_type_node);
5437 /* Set the TYPE_NAME for any variants that were built before
5438 record_builtin_type gave names to the built-in types. */
5440 tree void_name = TYPE_NAME (void_type_node);
5441 TYPE_NAME (void_type_node) = NULL_TREE;
5442 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5443 = void_name;
5444 TYPE_NAME (void_type_node) = void_name;
5447 /* This node must not be shared. */
5448 void_zero_node = make_node (INTEGER_CST);
5449 TREE_TYPE (void_zero_node) = void_type_node;
5451 void_list_node = build_void_list_node ();
5453 /* Make a type to be the domain of a few array types
5454 whose domains don't really matter.
5455 200 is small enough that it always fits in size_t
5456 and large enough that it can hold most function names for the
5457 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5458 array_domain_type = build_index_type (size_int (200));
5460 /* Make a type for arrays of characters.
5461 With luck nothing will ever really depend on the length of this
5462 array type. */
5463 char_array_type_node
5464 = build_array_type (char_type_node, array_domain_type);
5466 /* Likewise for arrays of ints. */
5467 int_array_type_node
5468 = build_array_type (integer_type_node, array_domain_type);
5470 string_type_node = build_pointer_type (char_type_node);
5471 const_string_type_node
5472 = build_pointer_type (build_qualified_type
5473 (char_type_node, TYPE_QUAL_CONST));
5475 /* This is special for C++ so functions can be overloaded. */
5476 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5477 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5478 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5479 underlying_wchar_type_node = wchar_type_node;
5480 if (c_dialect_cxx ())
5482 if (TYPE_UNSIGNED (wchar_type_node))
5483 wchar_type_node = make_unsigned_type (wchar_type_size);
5484 else
5485 wchar_type_node = make_signed_type (wchar_type_size);
5486 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5489 /* This is for wide string constants. */
5490 wchar_array_type_node
5491 = build_array_type (wchar_type_node, array_domain_type);
5493 /* Define 'char16_t'. */
5494 char16_type_node = get_identifier (CHAR16_TYPE);
5495 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5496 char16_type_size = TYPE_PRECISION (char16_type_node);
5497 if (c_dialect_cxx ())
5499 char16_type_node = make_unsigned_type (char16_type_size);
5501 if (cxx_dialect >= cxx0x)
5502 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5505 /* This is for UTF-16 string constants. */
5506 char16_array_type_node
5507 = build_array_type (char16_type_node, array_domain_type);
5509 /* Define 'char32_t'. */
5510 char32_type_node = get_identifier (CHAR32_TYPE);
5511 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5512 char32_type_size = TYPE_PRECISION (char32_type_node);
5513 if (c_dialect_cxx ())
5515 char32_type_node = make_unsigned_type (char32_type_size);
5517 if (cxx_dialect >= cxx0x)
5518 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5521 /* This is for UTF-32 string constants. */
5522 char32_array_type_node
5523 = build_array_type (char32_type_node, array_domain_type);
5525 wint_type_node =
5526 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5528 intmax_type_node =
5529 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5530 uintmax_type_node =
5531 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5533 if (SIG_ATOMIC_TYPE)
5534 sig_atomic_type_node =
5535 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5536 if (INT8_TYPE)
5537 int8_type_node =
5538 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5539 if (INT16_TYPE)
5540 int16_type_node =
5541 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5542 if (INT32_TYPE)
5543 int32_type_node =
5544 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5545 if (INT64_TYPE)
5546 int64_type_node =
5547 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5548 if (UINT8_TYPE)
5549 uint8_type_node =
5550 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5551 if (UINT16_TYPE)
5552 c_uint16_type_node =
5553 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5554 if (UINT32_TYPE)
5555 c_uint32_type_node =
5556 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5557 if (UINT64_TYPE)
5558 c_uint64_type_node =
5559 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5560 if (INT_LEAST8_TYPE)
5561 int_least8_type_node =
5562 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5563 if (INT_LEAST16_TYPE)
5564 int_least16_type_node =
5565 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5566 if (INT_LEAST32_TYPE)
5567 int_least32_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5569 if (INT_LEAST64_TYPE)
5570 int_least64_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5572 if (UINT_LEAST8_TYPE)
5573 uint_least8_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5575 if (UINT_LEAST16_TYPE)
5576 uint_least16_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5578 if (UINT_LEAST32_TYPE)
5579 uint_least32_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5581 if (UINT_LEAST64_TYPE)
5582 uint_least64_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5584 if (INT_FAST8_TYPE)
5585 int_fast8_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5587 if (INT_FAST16_TYPE)
5588 int_fast16_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5590 if (INT_FAST32_TYPE)
5591 int_fast32_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5593 if (INT_FAST64_TYPE)
5594 int_fast64_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5596 if (UINT_FAST8_TYPE)
5597 uint_fast8_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5599 if (UINT_FAST16_TYPE)
5600 uint_fast16_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5602 if (UINT_FAST32_TYPE)
5603 uint_fast32_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5605 if (UINT_FAST64_TYPE)
5606 uint_fast64_type_node =
5607 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5608 if (INTPTR_TYPE)
5609 intptr_type_node =
5610 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5611 if (UINTPTR_TYPE)
5612 uintptr_type_node =
5613 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5615 default_function_type
5616 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5617 ptrdiff_type_node
5618 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5619 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5621 lang_hooks.decls.pushdecl
5622 (build_decl (UNKNOWN_LOCATION,
5623 TYPE_DECL, get_identifier ("__builtin_va_list"),
5624 va_list_type_node));
5625 if (targetm.enum_va_list_p)
5627 int l;
5628 const char *pname;
5629 tree ptype;
5631 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5633 lang_hooks.decls.pushdecl
5634 (build_decl (UNKNOWN_LOCATION,
5635 TYPE_DECL, get_identifier (pname),
5636 ptype));
5641 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5643 va_list_arg_type_node = va_list_ref_type_node =
5644 build_pointer_type (TREE_TYPE (va_list_type_node));
5646 else
5648 va_list_arg_type_node = va_list_type_node;
5649 va_list_ref_type_node = build_reference_type (va_list_type_node);
5652 if (!flag_preprocess_only)
5653 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5655 main_identifier_node = get_identifier ("main");
5657 /* Create the built-in __null node. It is important that this is
5658 not shared. */
5659 null_node = make_node (INTEGER_CST);
5660 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5662 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5663 memset (builtin_types, 0, sizeof (builtin_types));
5666 /* The number of named compound-literals generated thus far. */
5667 static GTY(()) int compound_literal_number;
5669 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5671 void
5672 set_compound_literal_name (tree decl)
5674 char *name;
5675 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5676 compound_literal_number);
5677 compound_literal_number++;
5678 DECL_NAME (decl) = get_identifier (name);
5681 tree
5682 build_va_arg (location_t loc, tree expr, tree type)
5684 expr = build1 (VA_ARG_EXPR, type, expr);
5685 SET_EXPR_LOCATION (expr, loc);
5686 return expr;
5690 /* Linked list of disabled built-in functions. */
5692 typedef struct disabled_builtin
5694 const char *name;
5695 struct disabled_builtin *next;
5696 } disabled_builtin;
5697 static disabled_builtin *disabled_builtins = NULL;
5699 static bool builtin_function_disabled_p (const char *);
5701 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5702 begins with "__builtin_", give an error. */
5704 void
5705 disable_builtin_function (const char *name)
5707 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5708 error ("cannot disable built-in function %qs", name);
5709 else
5711 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5712 new_disabled_builtin->name = name;
5713 new_disabled_builtin->next = disabled_builtins;
5714 disabled_builtins = new_disabled_builtin;
5719 /* Return true if the built-in function NAME has been disabled, false
5720 otherwise. */
5722 static bool
5723 builtin_function_disabled_p (const char *name)
5725 disabled_builtin *p;
5726 for (p = disabled_builtins; p != NULL; p = p->next)
5728 if (strcmp (name, p->name) == 0)
5729 return true;
5731 return false;
5735 /* Worker for DEF_BUILTIN.
5736 Possibly define a builtin function with one or two names.
5737 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5738 nonansi_p and flag_no_nonansi_builtin. */
5740 static void
5741 def_builtin_1 (enum built_in_function fncode,
5742 const char *name,
5743 enum built_in_class fnclass,
5744 tree fntype, tree libtype,
5745 bool both_p, bool fallback_p, bool nonansi_p,
5746 tree fnattrs, bool implicit_p)
5748 tree decl;
5749 const char *libname;
5751 if (fntype == error_mark_node)
5752 return;
5754 gcc_assert ((!both_p && !fallback_p)
5755 || !strncmp (name, "__builtin_",
5756 strlen ("__builtin_")));
5758 libname = name + strlen ("__builtin_");
5759 decl = add_builtin_function (name, fntype, fncode, fnclass,
5760 (fallback_p ? libname : NULL),
5761 fnattrs);
5763 set_builtin_decl (fncode, decl, implicit_p);
5765 if (both_p
5766 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5767 && !(nonansi_p && flag_no_nonansi_builtin))
5768 add_builtin_function (libname, libtype, fncode, fnclass,
5769 NULL, fnattrs);
5772 /* Nonzero if the type T promotes to int. This is (nearly) the
5773 integral promotions defined in ISO C99 6.3.1.1/2. */
5775 bool
5776 c_promoting_integer_type_p (const_tree t)
5778 switch (TREE_CODE (t))
5780 case INTEGER_TYPE:
5781 return (TYPE_MAIN_VARIANT (t) == char_type_node
5782 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5783 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5784 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5785 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5786 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5788 case ENUMERAL_TYPE:
5789 /* ??? Technically all enumerations not larger than an int
5790 promote to an int. But this is used along code paths
5791 that only want to notice a size change. */
5792 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5794 case BOOLEAN_TYPE:
5795 return 1;
5797 default:
5798 return 0;
5802 /* Return 1 if PARMS specifies a fixed number of parameters
5803 and none of their types is affected by default promotions. */
5806 self_promoting_args_p (const_tree parms)
5808 const_tree t;
5809 for (t = parms; t; t = TREE_CHAIN (t))
5811 tree type = TREE_VALUE (t);
5813 if (type == error_mark_node)
5814 continue;
5816 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5817 return 0;
5819 if (type == 0)
5820 return 0;
5822 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5823 return 0;
5825 if (c_promoting_integer_type_p (type))
5826 return 0;
5828 return 1;
5831 /* Recursively remove any '*' or '&' operator from TYPE. */
5832 tree
5833 strip_pointer_operator (tree t)
5835 while (POINTER_TYPE_P (t))
5836 t = TREE_TYPE (t);
5837 return t;
5840 /* Recursively remove pointer or array type from TYPE. */
5841 tree
5842 strip_pointer_or_array_types (tree t)
5844 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5845 t = TREE_TYPE (t);
5846 return t;
5849 /* Used to compare case labels. K1 and K2 are actually tree nodes
5850 representing case labels, or NULL_TREE for a `default' label.
5851 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5852 K2, and 0 if K1 and K2 are equal. */
5855 case_compare (splay_tree_key k1, splay_tree_key k2)
5857 /* Consider a NULL key (such as arises with a `default' label) to be
5858 smaller than anything else. */
5859 if (!k1)
5860 return k2 ? -1 : 0;
5861 else if (!k2)
5862 return k1 ? 1 : 0;
5864 return tree_int_cst_compare ((tree) k1, (tree) k2);
5867 /* Process a case label, located at LOC, for the range LOW_VALUE
5868 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5869 then this case label is actually a `default' label. If only
5870 HIGH_VALUE is NULL_TREE, then case label was declared using the
5871 usual C/C++ syntax, rather than the GNU case range extension.
5872 CASES is a tree containing all the case ranges processed so far;
5873 COND is the condition for the switch-statement itself. Returns the
5874 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5875 is created. */
5877 tree
5878 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5879 tree low_value, tree high_value)
5881 tree type;
5882 tree label;
5883 tree case_label;
5884 splay_tree_node node;
5886 /* Create the LABEL_DECL itself. */
5887 label = create_artificial_label (loc);
5889 /* If there was an error processing the switch condition, bail now
5890 before we get more confused. */
5891 if (!cond || cond == error_mark_node)
5892 goto error_out;
5894 if ((low_value && TREE_TYPE (low_value)
5895 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5896 || (high_value && TREE_TYPE (high_value)
5897 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5899 error_at (loc, "pointers are not permitted as case values");
5900 goto error_out;
5903 /* Case ranges are a GNU extension. */
5904 if (high_value)
5905 pedwarn (loc, OPT_Wpedantic,
5906 "range expressions in switch statements are non-standard");
5908 type = TREE_TYPE (cond);
5909 if (low_value)
5911 low_value = check_case_value (low_value);
5912 low_value = convert_and_check (type, low_value);
5913 if (low_value == error_mark_node)
5914 goto error_out;
5916 if (high_value)
5918 high_value = check_case_value (high_value);
5919 high_value = convert_and_check (type, high_value);
5920 if (high_value == error_mark_node)
5921 goto error_out;
5924 if (low_value && high_value)
5926 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5927 really a case range, even though it was written that way.
5928 Remove the HIGH_VALUE to simplify later processing. */
5929 if (tree_int_cst_equal (low_value, high_value))
5930 high_value = NULL_TREE;
5931 else if (!tree_int_cst_lt (low_value, high_value))
5932 warning_at (loc, 0, "empty range specified");
5935 /* See if the case is in range of the type of the original testing
5936 expression. If both low_value and high_value are out of range,
5937 don't insert the case label and return NULL_TREE. */
5938 if (low_value
5939 && !check_case_bounds (type, orig_type,
5940 &low_value, high_value ? &high_value : NULL))
5941 return NULL_TREE;
5943 /* Look up the LOW_VALUE in the table of case labels we already
5944 have. */
5945 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5946 /* If there was not an exact match, check for overlapping ranges.
5947 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5948 that's a `default' label and the only overlap is an exact match. */
5949 if (!node && (low_value || high_value))
5951 splay_tree_node low_bound;
5952 splay_tree_node high_bound;
5954 /* Even though there wasn't an exact match, there might be an
5955 overlap between this case range and another case range.
5956 Since we've (inductively) not allowed any overlapping case
5957 ranges, we simply need to find the greatest low case label
5958 that is smaller that LOW_VALUE, and the smallest low case
5959 label that is greater than LOW_VALUE. If there is an overlap
5960 it will occur in one of these two ranges. */
5961 low_bound = splay_tree_predecessor (cases,
5962 (splay_tree_key) low_value);
5963 high_bound = splay_tree_successor (cases,
5964 (splay_tree_key) low_value);
5966 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5967 the LOW_VALUE, so there is no need to check unless the
5968 LOW_BOUND is in fact itself a case range. */
5969 if (low_bound
5970 && CASE_HIGH ((tree) low_bound->value)
5971 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5972 low_value) >= 0)
5973 node = low_bound;
5974 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5975 range is bigger than the low end of the current range, so we
5976 are only interested if the current range is a real range, and
5977 not an ordinary case label. */
5978 else if (high_bound
5979 && high_value
5980 && (tree_int_cst_compare ((tree) high_bound->key,
5981 high_value)
5982 <= 0))
5983 node = high_bound;
5985 /* If there was an overlap, issue an error. */
5986 if (node)
5988 tree duplicate = CASE_LABEL ((tree) node->value);
5990 if (high_value)
5992 error_at (loc, "duplicate (or overlapping) case value");
5993 error_at (DECL_SOURCE_LOCATION (duplicate),
5994 "this is the first entry overlapping that value");
5996 else if (low_value)
5998 error_at (loc, "duplicate case value") ;
5999 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6001 else
6003 error_at (loc, "multiple default labels in one switch");
6004 error_at (DECL_SOURCE_LOCATION (duplicate),
6005 "this is the first default label");
6007 goto error_out;
6010 /* Add a CASE_LABEL to the statement-tree. */
6011 case_label = add_stmt (build_case_label (low_value, high_value, label));
6012 /* Register this case label in the splay tree. */
6013 splay_tree_insert (cases,
6014 (splay_tree_key) low_value,
6015 (splay_tree_value) case_label);
6017 return case_label;
6019 error_out:
6020 /* Add a label so that the back-end doesn't think that the beginning of
6021 the switch is unreachable. Note that we do not add a case label, as
6022 that just leads to duplicates and thence to failure later on. */
6023 if (!cases->root)
6025 tree t = create_artificial_label (loc);
6026 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6028 return error_mark_node;
6031 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6032 Used to verify that case values match up with enumerator values. */
6034 static void
6035 match_case_to_enum_1 (tree key, tree type, tree label)
6037 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6039 /* ??? Not working too hard to print the double-word value.
6040 Should perhaps be done with %lwd in the diagnostic routines? */
6041 if (TREE_INT_CST_HIGH (key) == 0)
6042 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6043 TREE_INT_CST_LOW (key));
6044 else if (!TYPE_UNSIGNED (type)
6045 && TREE_INT_CST_HIGH (key) == -1
6046 && TREE_INT_CST_LOW (key) != 0)
6047 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6048 -TREE_INT_CST_LOW (key));
6049 else
6050 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6051 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6052 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6054 if (TYPE_NAME (type) == 0)
6055 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6056 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6057 "case value %qs not in enumerated type",
6058 buf);
6059 else
6060 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6061 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6062 "case value %qs not in enumerated type %qT",
6063 buf, type);
6066 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6067 Used to verify that case values match up with enumerator values. */
6069 static int
6070 match_case_to_enum (splay_tree_node node, void *data)
6072 tree label = (tree) node->value;
6073 tree type = (tree) data;
6075 /* Skip default case. */
6076 if (!CASE_LOW (label))
6077 return 0;
6079 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6080 when we did our enum->case scan. Reset our scratch bit after. */
6081 if (!CASE_LOW_SEEN (label))
6082 match_case_to_enum_1 (CASE_LOW (label), type, label);
6083 else
6084 CASE_LOW_SEEN (label) = 0;
6086 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6087 not set, that means that CASE_HIGH did not appear when we did our
6088 enum->case scan. Reset our scratch bit after. */
6089 if (CASE_HIGH (label))
6091 if (!CASE_HIGH_SEEN (label))
6092 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6093 else
6094 CASE_HIGH_SEEN (label) = 0;
6097 return 0;
6100 /* Handle -Wswitch*. Called from the front end after parsing the
6101 switch construct. */
6102 /* ??? Should probably be somewhere generic, since other languages
6103 besides C and C++ would want this. At the moment, however, C/C++
6104 are the only tree-ssa languages that support enumerations at all,
6105 so the point is moot. */
6107 void
6108 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6109 tree type, tree cond)
6111 splay_tree_node default_node;
6112 splay_tree_node node;
6113 tree chain;
6115 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6116 return;
6118 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6119 if (!default_node)
6120 warning_at (switch_location, OPT_Wswitch_default,
6121 "switch missing default case");
6123 /* From here on, we only care about about enumerated types. */
6124 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6125 return;
6127 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6128 if (!warn_switch_enum && !warn_switch)
6129 return;
6131 /* Check the cases. Warn about case values which are not members of
6132 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6133 there is no default case, check that exactly all enumeration
6134 literals are covered by the cases. */
6136 /* Clearing COND if it is not an integer constant simplifies
6137 the tests inside the loop below. */
6138 if (TREE_CODE (cond) != INTEGER_CST)
6139 cond = NULL_TREE;
6141 /* The time complexity here is O(N*lg(N)) worst case, but for the
6142 common case of monotonically increasing enumerators, it is
6143 O(N), since the nature of the splay tree will keep the next
6144 element adjacent to the root at all times. */
6146 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6148 tree value = TREE_VALUE (chain);
6149 if (TREE_CODE (value) == CONST_DECL)
6150 value = DECL_INITIAL (value);
6151 node = splay_tree_lookup (cases, (splay_tree_key) value);
6152 if (node)
6154 /* Mark the CASE_LOW part of the case entry as seen. */
6155 tree label = (tree) node->value;
6156 CASE_LOW_SEEN (label) = 1;
6157 continue;
6160 /* Even though there wasn't an exact match, there might be a
6161 case range which includes the enumerator's value. */
6162 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6163 if (node && CASE_HIGH ((tree) node->value))
6165 tree label = (tree) node->value;
6166 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6167 if (cmp >= 0)
6169 /* If we match the upper bound exactly, mark the CASE_HIGH
6170 part of the case entry as seen. */
6171 if (cmp == 0)
6172 CASE_HIGH_SEEN (label) = 1;
6173 continue;
6177 /* We've now determined that this enumerated literal isn't
6178 handled by the case labels of the switch statement. */
6180 /* If the switch expression is a constant, we only really care
6181 about whether that constant is handled by the switch. */
6182 if (cond && tree_int_cst_compare (cond, value))
6183 continue;
6185 /* If there is a default_node, the only relevant option is
6186 Wswitch-enum. Otherwise, if both are enabled then we prefer
6187 to warn using -Wswitch because -Wswitch is enabled by -Wall
6188 while -Wswitch-enum is explicit. */
6189 warning_at (switch_location,
6190 (default_node || !warn_switch
6191 ? OPT_Wswitch_enum
6192 : OPT_Wswitch),
6193 "enumeration value %qE not handled in switch",
6194 TREE_PURPOSE (chain));
6197 /* Warn if there are case expressions that don't correspond to
6198 enumerators. This can occur since C and C++ don't enforce
6199 type-checking of assignments to enumeration variables.
6201 The time complexity here is now always O(N) worst case, since
6202 we should have marked both the lower bound and upper bound of
6203 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6204 above. This scan also resets those fields. */
6206 splay_tree_foreach (cases, match_case_to_enum, type);
6209 /* Finish an expression taking the address of LABEL (an
6210 IDENTIFIER_NODE). Returns an expression for the address.
6212 LOC is the location for the expression returned. */
6214 tree
6215 finish_label_address_expr (tree label, location_t loc)
6217 tree result;
6219 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6221 if (label == error_mark_node)
6222 return error_mark_node;
6224 label = lookup_label (label);
6225 if (label == NULL_TREE)
6226 result = null_pointer_node;
6227 else
6229 TREE_USED (label) = 1;
6230 result = build1 (ADDR_EXPR, ptr_type_node, label);
6231 /* The current function is not necessarily uninlinable.
6232 Computed gotos are incompatible with inlining, but the value
6233 here could be used only in a diagnostic, for example. */
6234 protected_set_expr_location (result, loc);
6237 return result;
6241 /* Given a boolean expression ARG, return a tree representing an increment
6242 or decrement (as indicated by CODE) of ARG. The front end must check for
6243 invalid cases (e.g., decrement in C++). */
6244 tree
6245 boolean_increment (enum tree_code code, tree arg)
6247 tree val;
6248 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6250 arg = stabilize_reference (arg);
6251 switch (code)
6253 case PREINCREMENT_EXPR:
6254 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6255 break;
6256 case POSTINCREMENT_EXPR:
6257 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6258 arg = save_expr (arg);
6259 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6260 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6261 break;
6262 case PREDECREMENT_EXPR:
6263 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6264 invert_truthvalue_loc (input_location, arg));
6265 break;
6266 case POSTDECREMENT_EXPR:
6267 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6268 invert_truthvalue_loc (input_location, arg));
6269 arg = save_expr (arg);
6270 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6271 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6272 break;
6273 default:
6274 gcc_unreachable ();
6276 TREE_SIDE_EFFECTS (val) = 1;
6277 return val;
6280 /* Built-in macros for stddef.h and stdint.h, that require macros
6281 defined in this file. */
6282 void
6283 c_stddef_cpp_builtins(void)
6285 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6286 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6287 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6288 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6289 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6290 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6291 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6292 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6293 if (SIG_ATOMIC_TYPE)
6294 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6295 if (INT8_TYPE)
6296 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6297 if (INT16_TYPE)
6298 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6299 if (INT32_TYPE)
6300 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6301 if (INT64_TYPE)
6302 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6303 if (UINT8_TYPE)
6304 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6305 if (UINT16_TYPE)
6306 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6307 if (UINT32_TYPE)
6308 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6309 if (UINT64_TYPE)
6310 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6311 if (INT_LEAST8_TYPE)
6312 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6313 if (INT_LEAST16_TYPE)
6314 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6315 if (INT_LEAST32_TYPE)
6316 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6317 if (INT_LEAST64_TYPE)
6318 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6319 if (UINT_LEAST8_TYPE)
6320 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6321 if (UINT_LEAST16_TYPE)
6322 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6323 if (UINT_LEAST32_TYPE)
6324 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6325 if (UINT_LEAST64_TYPE)
6326 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6327 if (INT_FAST8_TYPE)
6328 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6329 if (INT_FAST16_TYPE)
6330 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6331 if (INT_FAST32_TYPE)
6332 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6333 if (INT_FAST64_TYPE)
6334 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6335 if (UINT_FAST8_TYPE)
6336 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6337 if (UINT_FAST16_TYPE)
6338 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6339 if (UINT_FAST32_TYPE)
6340 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6341 if (UINT_FAST64_TYPE)
6342 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6343 if (INTPTR_TYPE)
6344 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6345 if (UINTPTR_TYPE)
6346 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6349 static void
6350 c_init_attributes (void)
6352 /* Fill in the built_in_attributes array. */
6353 #define DEF_ATTR_NULL_TREE(ENUM) \
6354 built_in_attributes[(int) ENUM] = NULL_TREE;
6355 #define DEF_ATTR_INT(ENUM, VALUE) \
6356 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6357 #define DEF_ATTR_STRING(ENUM, VALUE) \
6358 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6359 #define DEF_ATTR_IDENT(ENUM, STRING) \
6360 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6361 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6362 built_in_attributes[(int) ENUM] \
6363 = tree_cons (built_in_attributes[(int) PURPOSE], \
6364 built_in_attributes[(int) VALUE], \
6365 built_in_attributes[(int) CHAIN]);
6366 #include "builtin-attrs.def"
6367 #undef DEF_ATTR_NULL_TREE
6368 #undef DEF_ATTR_INT
6369 #undef DEF_ATTR_IDENT
6370 #undef DEF_ATTR_TREE_LIST
6373 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6374 identifier as an argument, so the front end shouldn't look it up. */
6376 bool
6377 attribute_takes_identifier_p (const_tree attr_id)
6379 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6380 if (spec == NULL)
6381 /* Unknown attribute that we'll end up ignoring, return true so we
6382 don't complain about an identifier argument. */
6383 return true;
6384 else if (!strcmp ("mode", spec->name)
6385 || !strcmp ("format", spec->name)
6386 || !strcmp ("cleanup", spec->name))
6387 return true;
6388 else
6389 return targetm.attribute_takes_identifier_p (attr_id);
6392 /* Attribute handlers common to C front ends. */
6394 /* Handle a "packed" attribute; arguments as in
6395 struct attribute_spec.handler. */
6397 static tree
6398 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6399 int flags, bool *no_add_attrs)
6401 if (TYPE_P (*node))
6403 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6404 *node = build_variant_type_copy (*node);
6405 TYPE_PACKED (*node) = 1;
6407 else if (TREE_CODE (*node) == FIELD_DECL)
6409 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6410 /* Still pack bitfields. */
6411 && ! DECL_INITIAL (*node))
6412 warning (OPT_Wattributes,
6413 "%qE attribute ignored for field of type %qT",
6414 name, TREE_TYPE (*node));
6415 else
6416 DECL_PACKED (*node) = 1;
6418 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6419 used for DECL_REGISTER. It wouldn't mean anything anyway.
6420 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6421 that changes what the typedef is typing. */
6422 else
6424 warning (OPT_Wattributes, "%qE attribute ignored", name);
6425 *no_add_attrs = true;
6428 return NULL_TREE;
6431 /* Handle a "nocommon" attribute; arguments as in
6432 struct attribute_spec.handler. */
6434 static tree
6435 handle_nocommon_attribute (tree *node, tree name,
6436 tree ARG_UNUSED (args),
6437 int ARG_UNUSED (flags), bool *no_add_attrs)
6439 if (TREE_CODE (*node) == VAR_DECL)
6440 DECL_COMMON (*node) = 0;
6441 else
6443 warning (OPT_Wattributes, "%qE attribute ignored", name);
6444 *no_add_attrs = true;
6447 return NULL_TREE;
6450 /* Handle a "common" attribute; arguments as in
6451 struct attribute_spec.handler. */
6453 static tree
6454 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6455 int ARG_UNUSED (flags), bool *no_add_attrs)
6457 if (TREE_CODE (*node) == VAR_DECL)
6458 DECL_COMMON (*node) = 1;
6459 else
6461 warning (OPT_Wattributes, "%qE attribute ignored", name);
6462 *no_add_attrs = true;
6465 return NULL_TREE;
6468 /* Handle a "noreturn" attribute; arguments as in
6469 struct attribute_spec.handler. */
6471 static tree
6472 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6473 int ARG_UNUSED (flags), bool *no_add_attrs)
6475 tree type = TREE_TYPE (*node);
6477 /* See FIXME comment in c_common_attribute_table. */
6478 if (TREE_CODE (*node) == FUNCTION_DECL
6479 || objc_method_decl (TREE_CODE (*node)))
6480 TREE_THIS_VOLATILE (*node) = 1;
6481 else if (TREE_CODE (type) == POINTER_TYPE
6482 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6483 TREE_TYPE (*node)
6484 = build_pointer_type
6485 (build_type_variant (TREE_TYPE (type),
6486 TYPE_READONLY (TREE_TYPE (type)), 1));
6487 else
6489 warning (OPT_Wattributes, "%qE attribute ignored", name);
6490 *no_add_attrs = true;
6493 return NULL_TREE;
6496 /* Handle a "hot" and attribute; arguments as in
6497 struct attribute_spec.handler. */
6499 static tree
6500 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6501 int ARG_UNUSED (flags), bool *no_add_attrs)
6503 if (TREE_CODE (*node) == FUNCTION_DECL
6504 || TREE_CODE (*node) == LABEL_DECL)
6506 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6508 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6509 name, "cold");
6510 *no_add_attrs = true;
6512 /* Most of the rest of the hot processing is done later with
6513 lookup_attribute. */
6515 else
6517 warning (OPT_Wattributes, "%qE attribute ignored", name);
6518 *no_add_attrs = true;
6521 return NULL_TREE;
6524 /* Handle a "cold" and attribute; arguments as in
6525 struct attribute_spec.handler. */
6527 static tree
6528 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6529 int ARG_UNUSED (flags), bool *no_add_attrs)
6531 if (TREE_CODE (*node) == FUNCTION_DECL
6532 || TREE_CODE (*node) == LABEL_DECL)
6534 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6536 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6537 name, "hot");
6538 *no_add_attrs = true;
6540 /* Most of the rest of the cold processing is done later with
6541 lookup_attribute. */
6543 else
6545 warning (OPT_Wattributes, "%qE attribute ignored", name);
6546 *no_add_attrs = true;
6549 return NULL_TREE;
6552 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6553 struct attribute_spec.handler. */
6555 static tree
6556 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6557 bool *no_add_attrs)
6559 if (TREE_CODE (*node) != FUNCTION_DECL)
6561 warning (OPT_Wattributes, "%qE attribute ignored", name);
6562 *no_add_attrs = true;
6565 return NULL_TREE;
6568 /* Handle a "noinline" attribute; arguments as in
6569 struct attribute_spec.handler. */
6571 static tree
6572 handle_noinline_attribute (tree *node, tree name,
6573 tree ARG_UNUSED (args),
6574 int ARG_UNUSED (flags), bool *no_add_attrs)
6576 if (TREE_CODE (*node) == FUNCTION_DECL)
6577 DECL_UNINLINABLE (*node) = 1;
6578 else
6580 warning (OPT_Wattributes, "%qE attribute ignored", name);
6581 *no_add_attrs = true;
6584 return NULL_TREE;
6587 /* Handle a "noclone" attribute; arguments as in
6588 struct attribute_spec.handler. */
6590 static tree
6591 handle_noclone_attribute (tree *node, tree name,
6592 tree ARG_UNUSED (args),
6593 int ARG_UNUSED (flags), bool *no_add_attrs)
6595 if (TREE_CODE (*node) != FUNCTION_DECL)
6597 warning (OPT_Wattributes, "%qE attribute ignored", name);
6598 *no_add_attrs = true;
6601 return NULL_TREE;
6604 /* Handle a "always_inline" attribute; arguments as in
6605 struct attribute_spec.handler. */
6607 static tree
6608 handle_always_inline_attribute (tree *node, tree name,
6609 tree ARG_UNUSED (args),
6610 int ARG_UNUSED (flags),
6611 bool *no_add_attrs)
6613 if (TREE_CODE (*node) == FUNCTION_DECL)
6615 /* Set the attribute and mark it for disregarding inline
6616 limits. */
6617 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6619 else
6621 warning (OPT_Wattributes, "%qE attribute ignored", name);
6622 *no_add_attrs = true;
6625 return NULL_TREE;
6628 /* Handle a "gnu_inline" attribute; arguments as in
6629 struct attribute_spec.handler. */
6631 static tree
6632 handle_gnu_inline_attribute (tree *node, tree name,
6633 tree ARG_UNUSED (args),
6634 int ARG_UNUSED (flags),
6635 bool *no_add_attrs)
6637 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6639 /* Do nothing else, just set the attribute. We'll get at
6640 it later with lookup_attribute. */
6642 else
6644 warning (OPT_Wattributes, "%qE attribute ignored", name);
6645 *no_add_attrs = true;
6648 return NULL_TREE;
6651 /* Handle a "leaf" attribute; arguments as in
6652 struct attribute_spec.handler. */
6654 static tree
6655 handle_leaf_attribute (tree *node, tree name,
6656 tree ARG_UNUSED (args),
6657 int ARG_UNUSED (flags), bool *no_add_attrs)
6659 if (TREE_CODE (*node) != FUNCTION_DECL)
6661 warning (OPT_Wattributes, "%qE attribute ignored", name);
6662 *no_add_attrs = true;
6664 if (!TREE_PUBLIC (*node))
6666 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6667 *no_add_attrs = true;
6670 return NULL_TREE;
6673 /* Handle an "artificial" attribute; arguments as in
6674 struct attribute_spec.handler. */
6676 static tree
6677 handle_artificial_attribute (tree *node, tree name,
6678 tree ARG_UNUSED (args),
6679 int ARG_UNUSED (flags),
6680 bool *no_add_attrs)
6682 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6684 /* Do nothing else, just set the attribute. We'll get at
6685 it later with lookup_attribute. */
6687 else
6689 warning (OPT_Wattributes, "%qE attribute ignored", name);
6690 *no_add_attrs = true;
6693 return NULL_TREE;
6696 /* Handle a "flatten" attribute; arguments as in
6697 struct attribute_spec.handler. */
6699 static tree
6700 handle_flatten_attribute (tree *node, tree name,
6701 tree args ATTRIBUTE_UNUSED,
6702 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6704 if (TREE_CODE (*node) == FUNCTION_DECL)
6705 /* Do nothing else, just set the attribute. We'll get at
6706 it later with lookup_attribute. */
6708 else
6710 warning (OPT_Wattributes, "%qE attribute ignored", name);
6711 *no_add_attrs = true;
6714 return NULL_TREE;
6717 /* Handle a "warning" or "error" attribute; arguments as in
6718 struct attribute_spec.handler. */
6720 static tree
6721 handle_error_attribute (tree *node, tree name, tree args,
6722 int ARG_UNUSED (flags), bool *no_add_attrs)
6724 if (TREE_CODE (*node) == FUNCTION_DECL
6725 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6726 /* Do nothing else, just set the attribute. We'll get at
6727 it later with lookup_attribute. */
6729 else
6731 warning (OPT_Wattributes, "%qE attribute ignored", name);
6732 *no_add_attrs = true;
6735 return NULL_TREE;
6738 /* Handle a "used" attribute; arguments as in
6739 struct attribute_spec.handler. */
6741 static tree
6742 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6743 int ARG_UNUSED (flags), bool *no_add_attrs)
6745 tree node = *pnode;
6747 if (TREE_CODE (node) == FUNCTION_DECL
6748 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6749 || (TREE_CODE (node) == TYPE_DECL))
6751 TREE_USED (node) = 1;
6752 DECL_PRESERVE_P (node) = 1;
6753 if (TREE_CODE (node) == VAR_DECL)
6754 DECL_READ_P (node) = 1;
6756 else
6758 warning (OPT_Wattributes, "%qE attribute ignored", name);
6759 *no_add_attrs = true;
6762 return NULL_TREE;
6765 /* Handle a "unused" attribute; arguments as in
6766 struct attribute_spec.handler. */
6768 static tree
6769 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6770 int flags, bool *no_add_attrs)
6772 if (DECL_P (*node))
6774 tree decl = *node;
6776 if (TREE_CODE (decl) == PARM_DECL
6777 || TREE_CODE (decl) == VAR_DECL
6778 || TREE_CODE (decl) == FUNCTION_DECL
6779 || TREE_CODE (decl) == LABEL_DECL
6780 || TREE_CODE (decl) == TYPE_DECL)
6782 TREE_USED (decl) = 1;
6783 if (TREE_CODE (decl) == VAR_DECL
6784 || TREE_CODE (decl) == PARM_DECL)
6785 DECL_READ_P (decl) = 1;
6787 else
6789 warning (OPT_Wattributes, "%qE attribute ignored", name);
6790 *no_add_attrs = true;
6793 else
6795 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6796 *node = build_variant_type_copy (*node);
6797 TREE_USED (*node) = 1;
6800 return NULL_TREE;
6803 /* Handle a "externally_visible" attribute; arguments as in
6804 struct attribute_spec.handler. */
6806 static tree
6807 handle_externally_visible_attribute (tree *pnode, tree name,
6808 tree ARG_UNUSED (args),
6809 int ARG_UNUSED (flags),
6810 bool *no_add_attrs)
6812 tree node = *pnode;
6814 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6816 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6817 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6819 warning (OPT_Wattributes,
6820 "%qE attribute have effect only on public objects", name);
6821 *no_add_attrs = true;
6824 else
6826 warning (OPT_Wattributes, "%qE attribute ignored", name);
6827 *no_add_attrs = true;
6830 return NULL_TREE;
6833 /* Handle a "const" attribute; arguments as in
6834 struct attribute_spec.handler. */
6836 static tree
6837 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6838 int ARG_UNUSED (flags), bool *no_add_attrs)
6840 tree type = TREE_TYPE (*node);
6842 /* See FIXME comment on noreturn in c_common_attribute_table. */
6843 if (TREE_CODE (*node) == FUNCTION_DECL)
6844 TREE_READONLY (*node) = 1;
6845 else if (TREE_CODE (type) == POINTER_TYPE
6846 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6847 TREE_TYPE (*node)
6848 = build_pointer_type
6849 (build_type_variant (TREE_TYPE (type), 1,
6850 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6851 else
6853 warning (OPT_Wattributes, "%qE attribute ignored", name);
6854 *no_add_attrs = true;
6857 return NULL_TREE;
6860 /* Handle a "transparent_union" attribute; arguments as in
6861 struct attribute_spec.handler. */
6863 static tree
6864 handle_transparent_union_attribute (tree *node, tree name,
6865 tree ARG_UNUSED (args), int flags,
6866 bool *no_add_attrs)
6868 tree type;
6870 *no_add_attrs = true;
6873 if (TREE_CODE (*node) == TYPE_DECL
6874 && ! (flags & ATTR_FLAG_CXX11))
6875 node = &TREE_TYPE (*node);
6876 type = *node;
6878 if (TREE_CODE (type) == UNION_TYPE)
6880 /* Make sure that the first field will work for a transparent union.
6881 If the type isn't complete yet, leave the check to the code in
6882 finish_struct. */
6883 if (TYPE_SIZE (type))
6885 tree first = first_field (type);
6886 if (first == NULL_TREE
6887 || DECL_ARTIFICIAL (first)
6888 || TYPE_MODE (type) != DECL_MODE (first))
6889 goto ignored;
6892 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6894 /* If the type isn't complete yet, setting the flag
6895 on a variant wouldn't ever be checked. */
6896 if (!TYPE_SIZE (type))
6897 goto ignored;
6899 /* build_duplicate_type doesn't work for C++. */
6900 if (c_dialect_cxx ())
6901 goto ignored;
6903 /* A type variant isn't good enough, since we don't a cast
6904 to such a type removed as a no-op. */
6905 *node = type = build_duplicate_type (type);
6908 TYPE_TRANSPARENT_AGGR (type) = 1;
6909 return NULL_TREE;
6912 ignored:
6913 warning (OPT_Wattributes, "%qE attribute ignored", name);
6914 return NULL_TREE;
6917 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6918 get the requested priority for a constructor or destructor,
6919 possibly issuing diagnostics for invalid or reserved
6920 priorities. */
6922 static priority_type
6923 get_priority (tree args, bool is_destructor)
6925 HOST_WIDE_INT pri;
6926 tree arg;
6928 if (!args)
6929 return DEFAULT_INIT_PRIORITY;
6931 if (!SUPPORTS_INIT_PRIORITY)
6933 if (is_destructor)
6934 error ("destructor priorities are not supported");
6935 else
6936 error ("constructor priorities are not supported");
6937 return DEFAULT_INIT_PRIORITY;
6940 arg = TREE_VALUE (args);
6941 arg = default_conversion (arg);
6942 if (!host_integerp (arg, /*pos=*/0)
6943 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6944 goto invalid;
6946 pri = tree_low_cst (arg, /*pos=*/0);
6947 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6948 goto invalid;
6950 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6952 if (is_destructor)
6953 warning (0,
6954 "destructor priorities from 0 to %d are reserved "
6955 "for the implementation",
6956 MAX_RESERVED_INIT_PRIORITY);
6957 else
6958 warning (0,
6959 "constructor priorities from 0 to %d are reserved "
6960 "for the implementation",
6961 MAX_RESERVED_INIT_PRIORITY);
6963 return pri;
6965 invalid:
6966 if (is_destructor)
6967 error ("destructor priorities must be integers from 0 to %d inclusive",
6968 MAX_INIT_PRIORITY);
6969 else
6970 error ("constructor priorities must be integers from 0 to %d inclusive",
6971 MAX_INIT_PRIORITY);
6972 return DEFAULT_INIT_PRIORITY;
6975 /* Handle a "constructor" attribute; arguments as in
6976 struct attribute_spec.handler. */
6978 static tree
6979 handle_constructor_attribute (tree *node, tree name, tree args,
6980 int ARG_UNUSED (flags),
6981 bool *no_add_attrs)
6983 tree decl = *node;
6984 tree type = TREE_TYPE (decl);
6986 if (TREE_CODE (decl) == FUNCTION_DECL
6987 && TREE_CODE (type) == FUNCTION_TYPE
6988 && decl_function_context (decl) == 0)
6990 priority_type priority;
6991 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6992 priority = get_priority (args, /*is_destructor=*/false);
6993 SET_DECL_INIT_PRIORITY (decl, priority);
6994 TREE_USED (decl) = 1;
6996 else
6998 warning (OPT_Wattributes, "%qE attribute ignored", name);
6999 *no_add_attrs = true;
7002 return NULL_TREE;
7005 /* Handle a "destructor" attribute; arguments as in
7006 struct attribute_spec.handler. */
7008 static tree
7009 handle_destructor_attribute (tree *node, tree name, tree args,
7010 int ARG_UNUSED (flags),
7011 bool *no_add_attrs)
7013 tree decl = *node;
7014 tree type = TREE_TYPE (decl);
7016 if (TREE_CODE (decl) == FUNCTION_DECL
7017 && TREE_CODE (type) == FUNCTION_TYPE
7018 && decl_function_context (decl) == 0)
7020 priority_type priority;
7021 DECL_STATIC_DESTRUCTOR (decl) = 1;
7022 priority = get_priority (args, /*is_destructor=*/true);
7023 SET_DECL_FINI_PRIORITY (decl, priority);
7024 TREE_USED (decl) = 1;
7026 else
7028 warning (OPT_Wattributes, "%qE attribute ignored", name);
7029 *no_add_attrs = true;
7032 return NULL_TREE;
7035 /* Nonzero if the mode is a valid vector mode for this architecture.
7036 This returns nonzero even if there is no hardware support for the
7037 vector mode, but we can emulate with narrower modes. */
7039 static int
7040 vector_mode_valid_p (enum machine_mode mode)
7042 enum mode_class mclass = GET_MODE_CLASS (mode);
7043 enum machine_mode innermode;
7045 /* Doh! What's going on? */
7046 if (mclass != MODE_VECTOR_INT
7047 && mclass != MODE_VECTOR_FLOAT
7048 && mclass != MODE_VECTOR_FRACT
7049 && mclass != MODE_VECTOR_UFRACT
7050 && mclass != MODE_VECTOR_ACCUM
7051 && mclass != MODE_VECTOR_UACCUM)
7052 return 0;
7054 /* Hardware support. Woo hoo! */
7055 if (targetm.vector_mode_supported_p (mode))
7056 return 1;
7058 innermode = GET_MODE_INNER (mode);
7060 /* We should probably return 1 if requesting V4DI and we have no DI,
7061 but we have V2DI, but this is probably very unlikely. */
7063 /* If we have support for the inner mode, we can safely emulate it.
7064 We may not have V2DI, but me can emulate with a pair of DIs. */
7065 return targetm.scalar_mode_supported_p (innermode);
7069 /* Handle a "mode" attribute; arguments as in
7070 struct attribute_spec.handler. */
7072 static tree
7073 handle_mode_attribute (tree *node, tree name, tree args,
7074 int ARG_UNUSED (flags), bool *no_add_attrs)
7076 tree type = *node;
7077 tree ident = TREE_VALUE (args);
7079 *no_add_attrs = true;
7081 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7082 warning (OPT_Wattributes, "%qE attribute ignored", name);
7083 else
7085 int j;
7086 const char *p = IDENTIFIER_POINTER (ident);
7087 int len = strlen (p);
7088 enum machine_mode mode = VOIDmode;
7089 tree typefm;
7090 bool valid_mode;
7092 if (len > 4 && p[0] == '_' && p[1] == '_'
7093 && p[len - 1] == '_' && p[len - 2] == '_')
7095 char *newp = (char *) alloca (len - 1);
7097 strcpy (newp, &p[2]);
7098 newp[len - 4] = '\0';
7099 p = newp;
7102 /* Change this type to have a type with the specified mode.
7103 First check for the special modes. */
7104 if (!strcmp (p, "byte"))
7105 mode = byte_mode;
7106 else if (!strcmp (p, "word"))
7107 mode = word_mode;
7108 else if (!strcmp (p, "pointer"))
7109 mode = ptr_mode;
7110 else if (!strcmp (p, "libgcc_cmp_return"))
7111 mode = targetm.libgcc_cmp_return_mode ();
7112 else if (!strcmp (p, "libgcc_shift_count"))
7113 mode = targetm.libgcc_shift_count_mode ();
7114 else if (!strcmp (p, "unwind_word"))
7115 mode = targetm.unwind_word_mode ();
7116 else
7117 for (j = 0; j < NUM_MACHINE_MODES; j++)
7118 if (!strcmp (p, GET_MODE_NAME (j)))
7120 mode = (enum machine_mode) j;
7121 break;
7124 if (mode == VOIDmode)
7126 error ("unknown machine mode %qE", ident);
7127 return NULL_TREE;
7130 valid_mode = false;
7131 switch (GET_MODE_CLASS (mode))
7133 case MODE_INT:
7134 case MODE_PARTIAL_INT:
7135 case MODE_FLOAT:
7136 case MODE_DECIMAL_FLOAT:
7137 case MODE_FRACT:
7138 case MODE_UFRACT:
7139 case MODE_ACCUM:
7140 case MODE_UACCUM:
7141 valid_mode = targetm.scalar_mode_supported_p (mode);
7142 break;
7144 case MODE_COMPLEX_INT:
7145 case MODE_COMPLEX_FLOAT:
7146 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7147 break;
7149 case MODE_VECTOR_INT:
7150 case MODE_VECTOR_FLOAT:
7151 case MODE_VECTOR_FRACT:
7152 case MODE_VECTOR_UFRACT:
7153 case MODE_VECTOR_ACCUM:
7154 case MODE_VECTOR_UACCUM:
7155 warning (OPT_Wattributes, "specifying vector types with "
7156 "__attribute__ ((mode)) is deprecated");
7157 warning (OPT_Wattributes,
7158 "use __attribute__ ((vector_size)) instead");
7159 valid_mode = vector_mode_valid_p (mode);
7160 break;
7162 default:
7163 break;
7165 if (!valid_mode)
7167 error ("unable to emulate %qs", p);
7168 return NULL_TREE;
7171 if (POINTER_TYPE_P (type))
7173 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7174 tree (*fn)(tree, enum machine_mode, bool);
7176 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7178 error ("invalid pointer mode %qs", p);
7179 return NULL_TREE;
7182 if (TREE_CODE (type) == POINTER_TYPE)
7183 fn = build_pointer_type_for_mode;
7184 else
7185 fn = build_reference_type_for_mode;
7186 typefm = fn (TREE_TYPE (type), mode, false);
7188 else
7190 /* For fixed-point modes, we need to test if the signness of type
7191 and the machine mode are consistent. */
7192 if (ALL_FIXED_POINT_MODE_P (mode)
7193 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7195 error ("signedness of type and machine mode %qs don%'t match", p);
7196 return NULL_TREE;
7198 /* For fixed-point modes, we need to pass saturating info. */
7199 typefm = lang_hooks.types.type_for_mode (mode,
7200 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7201 : TYPE_UNSIGNED (type));
7204 if (typefm == NULL_TREE)
7206 error ("no data type for mode %qs", p);
7207 return NULL_TREE;
7209 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7211 /* For enumeral types, copy the precision from the integer
7212 type returned above. If not an INTEGER_TYPE, we can't use
7213 this mode for this type. */
7214 if (TREE_CODE (typefm) != INTEGER_TYPE)
7216 error ("cannot use mode %qs for enumeral types", p);
7217 return NULL_TREE;
7220 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7222 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7223 typefm = type;
7225 else
7227 /* We cannot build a type variant, as there's code that assumes
7228 that TYPE_MAIN_VARIANT has the same mode. This includes the
7229 debug generators. Instead, create a subrange type. This
7230 results in all of the enumeral values being emitted only once
7231 in the original, and the subtype gets them by reference. */
7232 if (TYPE_UNSIGNED (type))
7233 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7234 else
7235 typefm = make_signed_type (TYPE_PRECISION (typefm));
7236 TREE_TYPE (typefm) = type;
7239 else if (VECTOR_MODE_P (mode)
7240 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7241 : TREE_CODE (type) != TREE_CODE (typefm))
7243 error ("mode %qs applied to inappropriate type", p);
7244 return NULL_TREE;
7247 *node = typefm;
7250 return NULL_TREE;
7253 /* Handle a "section" attribute; arguments as in
7254 struct attribute_spec.handler. */
7256 static tree
7257 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7258 int ARG_UNUSED (flags), bool *no_add_attrs)
7260 tree decl = *node;
7262 if (targetm_common.have_named_sections)
7264 user_defined_section_attribute = true;
7266 if ((TREE_CODE (decl) == FUNCTION_DECL
7267 || TREE_CODE (decl) == VAR_DECL)
7268 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7270 if (TREE_CODE (decl) == VAR_DECL
7271 && current_function_decl != NULL_TREE
7272 && !TREE_STATIC (decl))
7274 error_at (DECL_SOURCE_LOCATION (decl),
7275 "section attribute cannot be specified for "
7276 "local variables");
7277 *no_add_attrs = true;
7280 /* The decl may have already been given a section attribute
7281 from a previous declaration. Ensure they match. */
7282 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7283 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7284 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7286 error ("section of %q+D conflicts with previous declaration",
7287 *node);
7288 *no_add_attrs = true;
7290 else if (TREE_CODE (decl) == VAR_DECL
7291 && !targetm.have_tls && targetm.emutls.tmpl_section
7292 && DECL_THREAD_LOCAL_P (decl))
7294 error ("section of %q+D cannot be overridden", *node);
7295 *no_add_attrs = true;
7297 else
7298 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7300 else
7302 error ("section attribute not allowed for %q+D", *node);
7303 *no_add_attrs = true;
7306 else
7308 error_at (DECL_SOURCE_LOCATION (*node),
7309 "section attributes are not supported for this target");
7310 *no_add_attrs = true;
7313 return NULL_TREE;
7316 /* Check whether ALIGN is a valid user-specified alignment. If so,
7317 return its base-2 log; if not, output an error and return -1. If
7318 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7319 no error. */
7321 check_user_alignment (const_tree align, bool allow_zero)
7323 int i;
7325 if (TREE_CODE (align) != INTEGER_CST
7326 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7328 error ("requested alignment is not an integer constant");
7329 return -1;
7331 else if (allow_zero && integer_zerop (align))
7332 return -1;
7333 else if ((i = tree_log2 (align)) == -1)
7335 error ("requested alignment is not a power of 2");
7336 return -1;
7338 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7340 error ("requested alignment is too large");
7341 return -1;
7343 return i;
7347 If in c++-11, check if the c++-11 alignment constraint with respect
7348 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7349 c++-11 mode, does nothing.
7351 [dcl.align]2/ says:
7353 [* if the constant expression evaluates to a fundamental alignment,
7354 the alignment requirement of the declared entity shall be the
7355 specified fundamental alignment.
7357 * if the constant expression evaluates to an extended alignment
7358 and the implementation supports that alignment in the context
7359 of the declaration, the alignment of the declared entity shall
7360 be that alignment
7362 * if the constant expression evaluates to an extended alignment
7363 and the implementation does not support that alignment in the
7364 context of the declaration, the program is ill-formed]. */
7366 static bool
7367 check_cxx_fundamental_alignment_constraints (tree node,
7368 unsigned align_log,
7369 int flags)
7371 bool alignment_too_large_p = false;
7372 unsigned requested_alignment = 1U << align_log;
7373 unsigned max_align = 0;
7375 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7376 || (node == NULL_TREE || node == error_mark_node))
7377 return true;
7379 if (cxx_fundamental_alignment_p (requested_alignment))
7380 return true;
7382 if (DECL_P (node))
7384 if (TREE_STATIC (node))
7386 /* For file scope variables and static members, the target
7387 supports alignments that are at most
7388 MAX_OFILE_ALIGNMENT. */
7389 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7390 alignment_too_large_p = true;
7392 else
7394 #ifdef BIGGEST_FIELD_ALIGNMENT
7395 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7396 #else
7397 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7398 #endif
7399 /* For non-static members, the target supports either
7400 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7401 if it is defined or BIGGEST_ALIGNMENT. */
7402 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7403 if (TREE_CODE (node) == FIELD_DECL
7404 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7405 alignment_too_large_p = true;
7406 #undef MAX_TARGET_FIELD_ALIGNMENT
7407 /* For stack variables, the target supports at most
7408 MAX_STACK_ALIGNMENT. */
7409 else if (decl_function_context (node) != NULL
7410 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7411 alignment_too_large_p = true;
7414 else if (TYPE_P (node))
7416 /* Let's be liberal for types. */
7417 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7418 alignment_too_large_p = true;
7421 if (alignment_too_large_p)
7422 pedwarn (input_location, OPT_Wattributes,
7423 "requested alignment %d is larger than %d",
7424 requested_alignment, max_align);
7426 return !alignment_too_large_p;
7429 /* Handle a "aligned" attribute; arguments as in
7430 struct attribute_spec.handler. */
7432 static tree
7433 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7434 int flags, bool *no_add_attrs)
7436 tree decl = NULL_TREE;
7437 tree *type = NULL;
7438 int is_type = 0;
7439 tree align_expr = (args ? TREE_VALUE (args)
7440 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7441 int i;
7443 if (DECL_P (*node))
7445 decl = *node;
7446 type = &TREE_TYPE (decl);
7447 is_type = TREE_CODE (*node) == TYPE_DECL;
7449 else if (TYPE_P (*node))
7450 type = node, is_type = 1;
7452 if ((i = check_user_alignment (align_expr, false)) == -1
7453 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7454 *no_add_attrs = true;
7455 else if (is_type)
7457 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7458 /* OK, modify the type in place. */;
7459 /* If we have a TYPE_DECL, then copy the type, so that we
7460 don't accidentally modify a builtin type. See pushdecl. */
7461 else if (decl && TREE_TYPE (decl) != error_mark_node
7462 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7464 tree tt = TREE_TYPE (decl);
7465 *type = build_variant_type_copy (*type);
7466 DECL_ORIGINAL_TYPE (decl) = tt;
7467 TYPE_NAME (*type) = decl;
7468 TREE_USED (*type) = TREE_USED (decl);
7469 TREE_TYPE (decl) = *type;
7471 else
7472 *type = build_variant_type_copy (*type);
7474 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7475 TYPE_USER_ALIGN (*type) = 1;
7477 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7478 && TREE_CODE (decl) != FIELD_DECL)
7480 error ("alignment may not be specified for %q+D", decl);
7481 *no_add_attrs = true;
7483 else if (DECL_USER_ALIGN (decl)
7484 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7485 /* C++-11 [dcl.align/4]:
7487 When multiple alignment-specifiers are specified for an
7488 entity, the alignment requirement shall be set to the
7489 strictest specified alignment.
7491 This formally comes from the c++11 specification but we are
7492 doing it for the GNU attribute syntax as well. */
7493 *no_add_attrs = true;
7494 else if (TREE_CODE (decl) == FUNCTION_DECL
7495 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7497 if (DECL_USER_ALIGN (decl))
7498 error ("alignment for %q+D was previously specified as %d "
7499 "and may not be decreased", decl,
7500 DECL_ALIGN (decl) / BITS_PER_UNIT);
7501 else
7502 error ("alignment for %q+D must be at least %d", decl,
7503 DECL_ALIGN (decl) / BITS_PER_UNIT);
7504 *no_add_attrs = true;
7506 else
7508 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7509 DECL_USER_ALIGN (decl) = 1;
7512 return NULL_TREE;
7515 /* Handle a "weak" attribute; arguments as in
7516 struct attribute_spec.handler. */
7518 static tree
7519 handle_weak_attribute (tree *node, tree name,
7520 tree ARG_UNUSED (args),
7521 int ARG_UNUSED (flags),
7522 bool * ARG_UNUSED (no_add_attrs))
7524 if (TREE_CODE (*node) == FUNCTION_DECL
7525 && DECL_DECLARED_INLINE_P (*node))
7527 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7528 *no_add_attrs = true;
7530 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7532 error ("indirect function %q+D cannot be declared weak", *node);
7533 *no_add_attrs = true;
7534 return NULL_TREE;
7536 else if (TREE_CODE (*node) == FUNCTION_DECL
7537 || TREE_CODE (*node) == VAR_DECL)
7538 declare_weak (*node);
7539 else
7540 warning (OPT_Wattributes, "%qE attribute ignored", name);
7542 return NULL_TREE;
7545 /* Handle an "alias" or "ifunc" attribute; arguments as in
7546 struct attribute_spec.handler, except that IS_ALIAS tells us
7547 whether this is an alias as opposed to ifunc attribute. */
7549 static tree
7550 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7551 bool *no_add_attrs)
7553 tree decl = *node;
7555 if (TREE_CODE (decl) != FUNCTION_DECL
7556 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7558 warning (OPT_Wattributes, "%qE attribute ignored", name);
7559 *no_add_attrs = true;
7561 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7562 || (TREE_CODE (decl) != FUNCTION_DECL
7563 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7564 /* A static variable declaration is always a tentative definition,
7565 but the alias is a non-tentative definition which overrides. */
7566 || (TREE_CODE (decl) != FUNCTION_DECL
7567 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7569 error ("%q+D defined both normally and as %qE attribute", decl, name);
7570 *no_add_attrs = true;
7571 return NULL_TREE;
7573 else if (!is_alias
7574 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7575 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7577 error ("weak %q+D cannot be defined %qE", decl, name);
7578 *no_add_attrs = true;
7579 return NULL_TREE;
7582 /* Note that the very first time we process a nested declaration,
7583 decl_function_context will not be set. Indeed, *would* never
7584 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7585 we do below. After such frobbery, pushdecl would set the context.
7586 In any case, this is never what we want. */
7587 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7589 tree id;
7591 id = TREE_VALUE (args);
7592 if (TREE_CODE (id) != STRING_CST)
7594 error ("attribute %qE argument not a string", name);
7595 *no_add_attrs = true;
7596 return NULL_TREE;
7598 id = get_identifier (TREE_STRING_POINTER (id));
7599 /* This counts as a use of the object pointed to. */
7600 TREE_USED (id) = 1;
7602 if (TREE_CODE (decl) == FUNCTION_DECL)
7603 DECL_INITIAL (decl) = error_mark_node;
7604 else
7606 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7607 DECL_EXTERNAL (decl) = 1;
7608 else
7609 DECL_EXTERNAL (decl) = 0;
7610 TREE_STATIC (decl) = 1;
7613 if (!is_alias)
7614 /* ifuncs are also aliases, so set that attribute too. */
7615 DECL_ATTRIBUTES (decl)
7616 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7618 else
7620 warning (OPT_Wattributes, "%qE attribute ignored", name);
7621 *no_add_attrs = true;
7624 return NULL_TREE;
7627 /* Handle an "alias" or "ifunc" attribute; arguments as in
7628 struct attribute_spec.handler. */
7630 static tree
7631 handle_ifunc_attribute (tree *node, tree name, tree args,
7632 int ARG_UNUSED (flags), bool *no_add_attrs)
7634 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7637 /* Handle an "alias" or "ifunc" attribute; arguments as in
7638 struct attribute_spec.handler. */
7640 static tree
7641 handle_alias_attribute (tree *node, tree name, tree args,
7642 int ARG_UNUSED (flags), bool *no_add_attrs)
7644 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7647 /* Handle a "weakref" attribute; arguments as in struct
7648 attribute_spec.handler. */
7650 static tree
7651 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7652 int flags, bool *no_add_attrs)
7654 tree attr = NULL_TREE;
7656 /* We must ignore the attribute when it is associated with
7657 local-scoped decls, since attribute alias is ignored and many
7658 such symbols do not even have a DECL_WEAK field. */
7659 if (decl_function_context (*node)
7660 || current_function_decl
7661 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7663 warning (OPT_Wattributes, "%qE attribute ignored", name);
7664 *no_add_attrs = true;
7665 return NULL_TREE;
7668 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7670 error ("indirect function %q+D cannot be declared weakref", *node);
7671 *no_add_attrs = true;
7672 return NULL_TREE;
7675 /* The idea here is that `weakref("name")' mutates into `weakref,
7676 alias("name")', and weakref without arguments, in turn,
7677 implicitly adds weak. */
7679 if (args)
7681 attr = tree_cons (get_identifier ("alias"), args, attr);
7682 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7684 *no_add_attrs = true;
7686 decl_attributes (node, attr, flags);
7688 else
7690 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7691 error_at (DECL_SOURCE_LOCATION (*node),
7692 "weakref attribute must appear before alias attribute");
7694 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7695 and that isn't supported; and because it wants to add it to
7696 the list of weak decls, which isn't helpful. */
7697 DECL_WEAK (*node) = 1;
7700 return NULL_TREE;
7703 /* Handle an "visibility" attribute; arguments as in
7704 struct attribute_spec.handler. */
7706 static tree
7707 handle_visibility_attribute (tree *node, tree name, tree args,
7708 int ARG_UNUSED (flags),
7709 bool *ARG_UNUSED (no_add_attrs))
7711 tree decl = *node;
7712 tree id = TREE_VALUE (args);
7713 enum symbol_visibility vis;
7715 if (TYPE_P (*node))
7717 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7718 /* OK */;
7719 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7721 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7722 name);
7723 return NULL_TREE;
7725 else if (TYPE_FIELDS (*node))
7727 error ("%qE attribute ignored because %qT is already defined",
7728 name, *node);
7729 return NULL_TREE;
7732 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7734 warning (OPT_Wattributes, "%qE attribute ignored", name);
7735 return NULL_TREE;
7738 if (TREE_CODE (id) != STRING_CST)
7740 error ("visibility argument not a string");
7741 return NULL_TREE;
7744 /* If this is a type, set the visibility on the type decl. */
7745 if (TYPE_P (decl))
7747 decl = TYPE_NAME (decl);
7748 if (!decl)
7749 return NULL_TREE;
7750 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7752 warning (OPT_Wattributes, "%qE attribute ignored on types",
7753 name);
7754 return NULL_TREE;
7758 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7759 vis = VISIBILITY_DEFAULT;
7760 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7761 vis = VISIBILITY_INTERNAL;
7762 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7763 vis = VISIBILITY_HIDDEN;
7764 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7765 vis = VISIBILITY_PROTECTED;
7766 else
7768 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7769 vis = VISIBILITY_DEFAULT;
7772 if (DECL_VISIBILITY_SPECIFIED (decl)
7773 && vis != DECL_VISIBILITY (decl))
7775 tree attributes = (TYPE_P (*node)
7776 ? TYPE_ATTRIBUTES (*node)
7777 : DECL_ATTRIBUTES (decl));
7778 if (lookup_attribute ("visibility", attributes))
7779 error ("%qD redeclared with different visibility", decl);
7780 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7781 && lookup_attribute ("dllimport", attributes))
7782 error ("%qD was declared %qs which implies default visibility",
7783 decl, "dllimport");
7784 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7785 && lookup_attribute ("dllexport", attributes))
7786 error ("%qD was declared %qs which implies default visibility",
7787 decl, "dllexport");
7790 DECL_VISIBILITY (decl) = vis;
7791 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7793 /* Go ahead and attach the attribute to the node as well. This is needed
7794 so we can determine whether we have VISIBILITY_DEFAULT because the
7795 visibility was not specified, or because it was explicitly overridden
7796 from the containing scope. */
7798 return NULL_TREE;
7801 /* Determine the ELF symbol visibility for DECL, which is either a
7802 variable or a function. It is an error to use this function if a
7803 definition of DECL is not available in this translation unit.
7804 Returns true if the final visibility has been determined by this
7805 function; false if the caller is free to make additional
7806 modifications. */
7808 bool
7809 c_determine_visibility (tree decl)
7811 gcc_assert (TREE_CODE (decl) == VAR_DECL
7812 || TREE_CODE (decl) == FUNCTION_DECL);
7814 /* If the user explicitly specified the visibility with an
7815 attribute, honor that. DECL_VISIBILITY will have been set during
7816 the processing of the attribute. We check for an explicit
7817 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7818 to distinguish the use of an attribute from the use of a "#pragma
7819 GCC visibility push(...)"; in the latter case we still want other
7820 considerations to be able to overrule the #pragma. */
7821 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7822 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7823 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7824 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7825 return true;
7827 /* Set default visibility to whatever the user supplied with
7828 visibility_specified depending on #pragma GCC visibility. */
7829 if (!DECL_VISIBILITY_SPECIFIED (decl))
7831 if (visibility_options.inpragma
7832 || DECL_VISIBILITY (decl) != default_visibility)
7834 DECL_VISIBILITY (decl) = default_visibility;
7835 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7836 /* If visibility changed and DECL already has DECL_RTL, ensure
7837 symbol flags are updated. */
7838 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7839 || TREE_CODE (decl) == FUNCTION_DECL)
7840 && DECL_RTL_SET_P (decl))
7841 make_decl_rtl (decl);
7844 return false;
7847 /* Handle an "tls_model" attribute; arguments as in
7848 struct attribute_spec.handler. */
7850 static tree
7851 handle_tls_model_attribute (tree *node, tree name, tree args,
7852 int ARG_UNUSED (flags), bool *no_add_attrs)
7854 tree id;
7855 tree decl = *node;
7856 enum tls_model kind;
7858 *no_add_attrs = true;
7860 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7862 warning (OPT_Wattributes, "%qE attribute ignored", name);
7863 return NULL_TREE;
7866 kind = DECL_TLS_MODEL (decl);
7867 id = TREE_VALUE (args);
7868 if (TREE_CODE (id) != STRING_CST)
7870 error ("tls_model argument not a string");
7871 return NULL_TREE;
7874 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7875 kind = TLS_MODEL_LOCAL_EXEC;
7876 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7877 kind = TLS_MODEL_INITIAL_EXEC;
7878 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7879 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7880 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7881 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7882 else
7883 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7885 DECL_TLS_MODEL (decl) = kind;
7886 return NULL_TREE;
7889 /* Handle a "no_instrument_function" attribute; arguments as in
7890 struct attribute_spec.handler. */
7892 static tree
7893 handle_no_instrument_function_attribute (tree *node, tree name,
7894 tree ARG_UNUSED (args),
7895 int ARG_UNUSED (flags),
7896 bool *no_add_attrs)
7898 tree decl = *node;
7900 if (TREE_CODE (decl) != FUNCTION_DECL)
7902 error_at (DECL_SOURCE_LOCATION (decl),
7903 "%qE attribute applies only to functions", name);
7904 *no_add_attrs = true;
7906 else if (DECL_INITIAL (decl))
7908 error_at (DECL_SOURCE_LOCATION (decl),
7909 "can%'t set %qE attribute after definition", name);
7910 *no_add_attrs = true;
7912 else
7913 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7915 return NULL_TREE;
7918 /* Handle a "malloc" attribute; arguments as in
7919 struct attribute_spec.handler. */
7921 static tree
7922 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7923 int ARG_UNUSED (flags), bool *no_add_attrs)
7925 if (TREE_CODE (*node) == FUNCTION_DECL
7926 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7927 DECL_IS_MALLOC (*node) = 1;
7928 else
7930 warning (OPT_Wattributes, "%qE attribute ignored", name);
7931 *no_add_attrs = true;
7934 return NULL_TREE;
7937 /* Handle a "alloc_size" attribute; arguments as in
7938 struct attribute_spec.handler. */
7940 static tree
7941 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7942 int ARG_UNUSED (flags), bool *no_add_attrs)
7944 unsigned arg_count = type_num_arguments (*node);
7945 for (; args; args = TREE_CHAIN (args))
7947 tree position = TREE_VALUE (args);
7949 if (TREE_CODE (position) != INTEGER_CST
7950 || TREE_INT_CST_HIGH (position)
7951 || TREE_INT_CST_LOW (position) < 1
7952 || TREE_INT_CST_LOW (position) > arg_count )
7954 warning (OPT_Wattributes,
7955 "alloc_size parameter outside range");
7956 *no_add_attrs = true;
7957 return NULL_TREE;
7960 return NULL_TREE;
7963 /* Handle a "fn spec" attribute; arguments as in
7964 struct attribute_spec.handler. */
7966 static tree
7967 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7968 tree args, int ARG_UNUSED (flags),
7969 bool *no_add_attrs ATTRIBUTE_UNUSED)
7971 gcc_assert (args
7972 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7973 && !TREE_CHAIN (args));
7974 return NULL_TREE;
7977 /* Handle a "returns_twice" attribute; arguments as in
7978 struct attribute_spec.handler. */
7980 static tree
7981 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7982 int ARG_UNUSED (flags), bool *no_add_attrs)
7984 if (TREE_CODE (*node) == FUNCTION_DECL)
7985 DECL_IS_RETURNS_TWICE (*node) = 1;
7986 else
7988 warning (OPT_Wattributes, "%qE attribute ignored", name);
7989 *no_add_attrs = true;
7992 return NULL_TREE;
7995 /* Handle a "no_limit_stack" attribute; arguments as in
7996 struct attribute_spec.handler. */
7998 static tree
7999 handle_no_limit_stack_attribute (tree *node, tree name,
8000 tree ARG_UNUSED (args),
8001 int ARG_UNUSED (flags),
8002 bool *no_add_attrs)
8004 tree decl = *node;
8006 if (TREE_CODE (decl) != FUNCTION_DECL)
8008 error_at (DECL_SOURCE_LOCATION (decl),
8009 "%qE attribute applies only to functions", name);
8010 *no_add_attrs = true;
8012 else if (DECL_INITIAL (decl))
8014 error_at (DECL_SOURCE_LOCATION (decl),
8015 "can%'t set %qE attribute after definition", name);
8016 *no_add_attrs = true;
8018 else
8019 DECL_NO_LIMIT_STACK (decl) = 1;
8021 return NULL_TREE;
8024 /* Handle a "pure" attribute; arguments as in
8025 struct attribute_spec.handler. */
8027 static tree
8028 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8029 int ARG_UNUSED (flags), bool *no_add_attrs)
8031 if (TREE_CODE (*node) == FUNCTION_DECL)
8032 DECL_PURE_P (*node) = 1;
8033 /* ??? TODO: Support types. */
8034 else
8036 warning (OPT_Wattributes, "%qE attribute ignored", name);
8037 *no_add_attrs = true;
8040 return NULL_TREE;
8043 /* Digest an attribute list destined for a transactional memory statement.
8044 ALLOWED is the set of attributes that are allowed for this statement;
8045 return the attribute we parsed. Multiple attributes are never allowed. */
8048 parse_tm_stmt_attr (tree attrs, int allowed)
8050 tree a_seen = NULL;
8051 int m_seen = 0;
8053 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8055 tree a = TREE_PURPOSE (attrs);
8056 int m = 0;
8058 if (is_attribute_p ("outer", a))
8059 m = TM_STMT_ATTR_OUTER;
8061 if ((m & allowed) == 0)
8063 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8064 continue;
8067 if (m_seen == 0)
8069 a_seen = a;
8070 m_seen = m;
8072 else if (m_seen == m)
8073 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8074 else
8075 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8078 return m_seen;
8081 /* Transform a TM attribute name into a maskable integer and back.
8082 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8083 to how the lack of an attribute is treated. */
8086 tm_attr_to_mask (tree attr)
8088 if (attr == NULL)
8089 return 0;
8090 if (is_attribute_p ("transaction_safe", attr))
8091 return TM_ATTR_SAFE;
8092 if (is_attribute_p ("transaction_callable", attr))
8093 return TM_ATTR_CALLABLE;
8094 if (is_attribute_p ("transaction_pure", attr))
8095 return TM_ATTR_PURE;
8096 if (is_attribute_p ("transaction_unsafe", attr))
8097 return TM_ATTR_IRREVOCABLE;
8098 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8099 return TM_ATTR_MAY_CANCEL_OUTER;
8100 return 0;
8103 tree
8104 tm_mask_to_attr (int mask)
8106 const char *str;
8107 switch (mask)
8109 case TM_ATTR_SAFE:
8110 str = "transaction_safe";
8111 break;
8112 case TM_ATTR_CALLABLE:
8113 str = "transaction_callable";
8114 break;
8115 case TM_ATTR_PURE:
8116 str = "transaction_pure";
8117 break;
8118 case TM_ATTR_IRREVOCABLE:
8119 str = "transaction_unsafe";
8120 break;
8121 case TM_ATTR_MAY_CANCEL_OUTER:
8122 str = "transaction_may_cancel_outer";
8123 break;
8124 default:
8125 gcc_unreachable ();
8127 return get_identifier (str);
8130 /* Return the first TM attribute seen in LIST. */
8132 tree
8133 find_tm_attribute (tree list)
8135 for (; list ; list = TREE_CHAIN (list))
8137 tree name = TREE_PURPOSE (list);
8138 if (tm_attr_to_mask (name) != 0)
8139 return name;
8141 return NULL_TREE;
8144 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8145 Here we accept only function types, and verify that none of the other
8146 function TM attributes are also applied. */
8147 /* ??? We need to accept class types for C++, but not C. This greatly
8148 complicates this function, since we can no longer rely on the extra
8149 processing given by function_type_required. */
8151 static tree
8152 handle_tm_attribute (tree *node, tree name, tree args,
8153 int flags, bool *no_add_attrs)
8155 /* Only one path adds the attribute; others don't. */
8156 *no_add_attrs = true;
8158 switch (TREE_CODE (*node))
8160 case RECORD_TYPE:
8161 case UNION_TYPE:
8162 /* Only tm_callable and tm_safe apply to classes. */
8163 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8164 goto ignored;
8165 /* FALLTHRU */
8167 case FUNCTION_TYPE:
8168 case METHOD_TYPE:
8170 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8171 if (old_name == name)
8173 else if (old_name != NULL_TREE)
8174 error ("type was previously declared %qE", old_name);
8175 else
8176 *no_add_attrs = false;
8178 break;
8180 case POINTER_TYPE:
8182 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8183 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8185 tree fn_tmp = TREE_TYPE (*node);
8186 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8187 *node = build_pointer_type (fn_tmp);
8188 break;
8191 /* FALLTHRU */
8193 default:
8194 /* If a function is next, pass it on to be tried next. */
8195 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8196 return tree_cons (name, args, NULL);
8198 ignored:
8199 warning (OPT_Wattributes, "%qE attribute ignored", name);
8200 break;
8203 return NULL_TREE;
8206 /* Handle the TM_WRAP attribute; arguments as in
8207 struct attribute_spec.handler. */
8209 static tree
8210 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8211 int ARG_UNUSED (flags), bool *no_add_attrs)
8213 tree decl = *node;
8215 /* We don't need the attribute even on success, since we
8216 record the entry in an external table. */
8217 *no_add_attrs = true;
8219 if (TREE_CODE (decl) != FUNCTION_DECL)
8220 warning (OPT_Wattributes, "%qE attribute ignored", name);
8221 else
8223 tree wrap_decl = TREE_VALUE (args);
8224 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8225 && TREE_CODE (wrap_decl) != VAR_DECL
8226 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8227 error ("%qE argument not an identifier", name);
8228 else
8230 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8231 wrap_decl = lookup_name (wrap_decl);
8232 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8234 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8235 TREE_TYPE (wrap_decl)))
8236 record_tm_replacement (wrap_decl, decl);
8237 else
8238 error ("%qD is not compatible with %qD", wrap_decl, decl);
8240 else
8241 error ("transaction_wrap argument is not a function");
8245 return NULL_TREE;
8248 /* Ignore the given attribute. Used when this attribute may be usefully
8249 overridden by the target, but is not used generically. */
8251 static tree
8252 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8253 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8254 bool *no_add_attrs)
8256 *no_add_attrs = true;
8257 return NULL_TREE;
8260 /* Handle a "no vops" attribute; arguments as in
8261 struct attribute_spec.handler. */
8263 static tree
8264 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8265 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8266 bool *ARG_UNUSED (no_add_attrs))
8268 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8269 DECL_IS_NOVOPS (*node) = 1;
8270 return NULL_TREE;
8273 /* Handle a "deprecated" attribute; arguments as in
8274 struct attribute_spec.handler. */
8276 static tree
8277 handle_deprecated_attribute (tree *node, tree name,
8278 tree args, int flags,
8279 bool *no_add_attrs)
8281 tree type = NULL_TREE;
8282 int warn = 0;
8283 tree what = NULL_TREE;
8285 if (!args)
8286 *no_add_attrs = true;
8287 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8289 error ("deprecated message is not a string");
8290 *no_add_attrs = true;
8293 if (DECL_P (*node))
8295 tree decl = *node;
8296 type = TREE_TYPE (decl);
8298 if (TREE_CODE (decl) == TYPE_DECL
8299 || TREE_CODE (decl) == PARM_DECL
8300 || TREE_CODE (decl) == VAR_DECL
8301 || TREE_CODE (decl) == FUNCTION_DECL
8302 || TREE_CODE (decl) == FIELD_DECL
8303 || objc_method_decl (TREE_CODE (decl)))
8304 TREE_DEPRECATED (decl) = 1;
8305 else
8306 warn = 1;
8308 else if (TYPE_P (*node))
8310 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8311 *node = build_variant_type_copy (*node);
8312 TREE_DEPRECATED (*node) = 1;
8313 type = *node;
8315 else
8316 warn = 1;
8318 if (warn)
8320 *no_add_attrs = true;
8321 if (type && TYPE_NAME (type))
8323 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8324 what = TYPE_NAME (*node);
8325 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8326 && DECL_NAME (TYPE_NAME (type)))
8327 what = DECL_NAME (TYPE_NAME (type));
8329 if (what)
8330 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8331 else
8332 warning (OPT_Wattributes, "%qE attribute ignored", name);
8335 return NULL_TREE;
8338 /* Handle a "vector_size" attribute; arguments as in
8339 struct attribute_spec.handler. */
8341 static tree
8342 handle_vector_size_attribute (tree *node, tree name, tree args,
8343 int ARG_UNUSED (flags),
8344 bool *no_add_attrs)
8346 unsigned HOST_WIDE_INT vecsize, nunits;
8347 enum machine_mode orig_mode;
8348 tree type = *node, new_type, size;
8350 *no_add_attrs = true;
8352 size = TREE_VALUE (args);
8354 if (!host_integerp (size, 1))
8356 warning (OPT_Wattributes, "%qE attribute ignored", name);
8357 return NULL_TREE;
8360 /* Get the vector size (in bytes). */
8361 vecsize = tree_low_cst (size, 1);
8363 /* We need to provide for vector pointers, vector arrays, and
8364 functions returning vectors. For example:
8366 __attribute__((vector_size(16))) short *foo;
8368 In this case, the mode is SI, but the type being modified is
8369 HI, so we need to look further. */
8371 while (POINTER_TYPE_P (type)
8372 || TREE_CODE (type) == FUNCTION_TYPE
8373 || TREE_CODE (type) == METHOD_TYPE
8374 || TREE_CODE (type) == ARRAY_TYPE
8375 || TREE_CODE (type) == OFFSET_TYPE)
8376 type = TREE_TYPE (type);
8378 /* Get the mode of the type being modified. */
8379 orig_mode = TYPE_MODE (type);
8381 if ((!INTEGRAL_TYPE_P (type)
8382 && !SCALAR_FLOAT_TYPE_P (type)
8383 && !FIXED_POINT_TYPE_P (type))
8384 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8385 && GET_MODE_CLASS (orig_mode) != MODE_INT
8386 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8387 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8388 || TREE_CODE (type) == BOOLEAN_TYPE)
8390 error ("invalid vector type for attribute %qE", name);
8391 return NULL_TREE;
8394 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8396 error ("vector size not an integral multiple of component size");
8397 return NULL;
8400 if (vecsize == 0)
8402 error ("zero vector size");
8403 return NULL;
8406 /* Calculate how many units fit in the vector. */
8407 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8408 if (nunits & (nunits - 1))
8410 error ("number of components of the vector not a power of two");
8411 return NULL_TREE;
8414 new_type = build_vector_type (type, nunits);
8416 /* Build back pointers if needed. */
8417 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8419 return NULL_TREE;
8422 /* Handle the "nonnull" attribute. */
8423 static tree
8424 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8425 tree args, int ARG_UNUSED (flags),
8426 bool *no_add_attrs)
8428 tree type = *node;
8429 unsigned HOST_WIDE_INT attr_arg_num;
8431 /* If no arguments are specified, all pointer arguments should be
8432 non-null. Verify a full prototype is given so that the arguments
8433 will have the correct types when we actually check them later. */
8434 if (!args)
8436 if (!prototype_p (type))
8438 error ("nonnull attribute without arguments on a non-prototype");
8439 *no_add_attrs = true;
8441 return NULL_TREE;
8444 /* Argument list specified. Verify that each argument number references
8445 a pointer argument. */
8446 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8448 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8450 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8452 error ("nonnull argument has invalid operand number (argument %lu)",
8453 (unsigned long) attr_arg_num);
8454 *no_add_attrs = true;
8455 return NULL_TREE;
8458 if (prototype_p (type))
8460 function_args_iterator iter;
8461 tree argument;
8463 function_args_iter_init (&iter, type);
8464 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8466 argument = function_args_iter_cond (&iter);
8467 if (argument == NULL_TREE || ck_num == arg_num)
8468 break;
8471 if (!argument
8472 || TREE_CODE (argument) == VOID_TYPE)
8474 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8475 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8476 *no_add_attrs = true;
8477 return NULL_TREE;
8480 if (TREE_CODE (argument) != POINTER_TYPE)
8482 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8483 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8484 *no_add_attrs = true;
8485 return NULL_TREE;
8490 return NULL_TREE;
8493 /* Check the argument list of a function call for null in argument slots
8494 that are marked as requiring a non-null pointer argument. The NARGS
8495 arguments are passed in the array ARGARRAY.
8498 static void
8499 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8501 tree a;
8502 int i;
8504 attrs = lookup_attribute ("nonnull", attrs);
8505 if (attrs == NULL_TREE)
8506 return;
8508 a = attrs;
8509 /* See if any of the nonnull attributes has no arguments. If so,
8510 then every pointer argument is checked (in which case the check
8511 for pointer type is done in check_nonnull_arg). */
8512 if (TREE_VALUE (a) != NULL_TREE)
8514 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8515 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8517 if (a != NULL_TREE)
8518 for (i = 0; i < nargs; i++)
8519 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8520 i + 1);
8521 else
8523 /* Walk the argument list. If we encounter an argument number we
8524 should check for non-null, do it. */
8525 for (i = 0; i < nargs; i++)
8527 for (a = attrs; ; a = TREE_CHAIN (a))
8529 a = lookup_attribute ("nonnull", a);
8530 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8531 break;
8534 if (a != NULL_TREE)
8535 check_function_arguments_recurse (check_nonnull_arg, NULL,
8536 argarray[i], i + 1);
8541 /* Check that the Nth argument of a function call (counting backwards
8542 from the end) is a (pointer)0. The NARGS arguments are passed in the
8543 array ARGARRAY. */
8545 static void
8546 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8548 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8550 if (attr)
8552 int len = 0;
8553 int pos = 0;
8554 tree sentinel;
8555 function_args_iterator iter;
8556 tree t;
8558 /* Skip over the named arguments. */
8559 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8561 if (len == nargs)
8562 break;
8563 len++;
8566 if (TREE_VALUE (attr))
8568 tree p = TREE_VALUE (TREE_VALUE (attr));
8569 pos = TREE_INT_CST_LOW (p);
8572 /* The sentinel must be one of the varargs, i.e.
8573 in position >= the number of fixed arguments. */
8574 if ((nargs - 1 - pos) < len)
8576 warning (OPT_Wformat_,
8577 "not enough variable arguments to fit a sentinel");
8578 return;
8581 /* Validate the sentinel. */
8582 sentinel = argarray[nargs - 1 - pos];
8583 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8584 || !integer_zerop (sentinel))
8585 /* Although __null (in C++) is only an integer we allow it
8586 nevertheless, as we are guaranteed that it's exactly
8587 as wide as a pointer, and we don't want to force
8588 users to cast the NULL they have written there.
8589 We warn with -Wstrict-null-sentinel, though. */
8590 && (warn_strict_null_sentinel || null_node != sentinel))
8591 warning (OPT_Wformat_, "missing sentinel in function call");
8595 /* Helper for check_function_nonnull; given a list of operands which
8596 must be non-null in ARGS, determine if operand PARAM_NUM should be
8597 checked. */
8599 static bool
8600 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8602 unsigned HOST_WIDE_INT arg_num = 0;
8604 for (; args; args = TREE_CHAIN (args))
8606 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8608 gcc_assert (found);
8610 if (arg_num == param_num)
8611 return true;
8613 return false;
8616 /* Check that the function argument PARAM (which is operand number
8617 PARAM_NUM) is non-null. This is called by check_function_nonnull
8618 via check_function_arguments_recurse. */
8620 static void
8621 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8622 unsigned HOST_WIDE_INT param_num)
8624 /* Just skip checking the argument if it's not a pointer. This can
8625 happen if the "nonnull" attribute was given without an operand
8626 list (which means to check every pointer argument). */
8628 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8629 return;
8631 if (integer_zerop (param))
8632 warning (OPT_Wnonnull, "null argument where non-null required "
8633 "(argument %lu)", (unsigned long) param_num);
8636 /* Helper for nonnull attribute handling; fetch the operand number
8637 from the attribute argument list. */
8639 static bool
8640 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8642 /* Verify the arg number is a constant. */
8643 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8644 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8645 return false;
8647 *valp = TREE_INT_CST_LOW (arg_num_expr);
8648 return true;
8651 /* Handle a "nothrow" attribute; arguments as in
8652 struct attribute_spec.handler. */
8654 static tree
8655 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8656 int ARG_UNUSED (flags), bool *no_add_attrs)
8658 if (TREE_CODE (*node) == FUNCTION_DECL)
8659 TREE_NOTHROW (*node) = 1;
8660 /* ??? TODO: Support types. */
8661 else
8663 warning (OPT_Wattributes, "%qE attribute ignored", name);
8664 *no_add_attrs = true;
8667 return NULL_TREE;
8670 /* Handle a "cleanup" attribute; arguments as in
8671 struct attribute_spec.handler. */
8673 static tree
8674 handle_cleanup_attribute (tree *node, tree name, tree args,
8675 int ARG_UNUSED (flags), bool *no_add_attrs)
8677 tree decl = *node;
8678 tree cleanup_id, cleanup_decl;
8680 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8681 for global destructors in C++. This requires infrastructure that
8682 we don't have generically at the moment. It's also not a feature
8683 we'd be missing too much, since we do have attribute constructor. */
8684 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8686 warning (OPT_Wattributes, "%qE attribute ignored", name);
8687 *no_add_attrs = true;
8688 return NULL_TREE;
8691 /* Verify that the argument is a function in scope. */
8692 /* ??? We could support pointers to functions here as well, if
8693 that was considered desirable. */
8694 cleanup_id = TREE_VALUE (args);
8695 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8697 error ("cleanup argument not an identifier");
8698 *no_add_attrs = true;
8699 return NULL_TREE;
8701 cleanup_decl = lookup_name (cleanup_id);
8702 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8704 error ("cleanup argument not a function");
8705 *no_add_attrs = true;
8706 return NULL_TREE;
8709 /* That the function has proper type is checked with the
8710 eventual call to build_function_call. */
8712 return NULL_TREE;
8715 /* Handle a "warn_unused_result" attribute. No special handling. */
8717 static tree
8718 handle_warn_unused_result_attribute (tree *node, tree name,
8719 tree ARG_UNUSED (args),
8720 int ARG_UNUSED (flags), bool *no_add_attrs)
8722 /* Ignore the attribute for functions not returning any value. */
8723 if (VOID_TYPE_P (TREE_TYPE (*node)))
8725 warning (OPT_Wattributes, "%qE attribute ignored", name);
8726 *no_add_attrs = true;
8729 return NULL_TREE;
8732 /* Handle a "sentinel" attribute. */
8734 static tree
8735 handle_sentinel_attribute (tree *node, tree name, tree args,
8736 int ARG_UNUSED (flags), bool *no_add_attrs)
8738 if (!prototype_p (*node))
8740 warning (OPT_Wattributes,
8741 "%qE attribute requires prototypes with named arguments", name);
8742 *no_add_attrs = true;
8744 else
8746 if (!stdarg_p (*node))
8748 warning (OPT_Wattributes,
8749 "%qE attribute only applies to variadic functions", name);
8750 *no_add_attrs = true;
8754 if (args)
8756 tree position = TREE_VALUE (args);
8758 if (TREE_CODE (position) != INTEGER_CST)
8760 warning (OPT_Wattributes,
8761 "requested position is not an integer constant");
8762 *no_add_attrs = true;
8764 else
8766 if (tree_int_cst_lt (position, integer_zero_node))
8768 warning (OPT_Wattributes,
8769 "requested position is less than zero");
8770 *no_add_attrs = true;
8775 return NULL_TREE;
8778 /* Handle a "type_generic" attribute. */
8780 static tree
8781 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8782 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8783 bool * ARG_UNUSED (no_add_attrs))
8785 /* Ensure we have a function type. */
8786 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8788 /* Ensure we have a variadic function. */
8789 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8791 return NULL_TREE;
8794 /* Handle a "target" attribute. */
8796 static tree
8797 handle_target_attribute (tree *node, tree name, tree args, int flags,
8798 bool *no_add_attrs)
8800 /* Ensure we have a function type. */
8801 if (TREE_CODE (*node) != FUNCTION_DECL)
8803 warning (OPT_Wattributes, "%qE attribute ignored", name);
8804 *no_add_attrs = true;
8806 /* Do not strip invalid target attributes for targets which support function
8807 multiversioning as the target string is used to determine versioned
8808 functions. */
8809 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8810 flags)
8811 && ! targetm.target_option.supports_function_versions ())
8812 *no_add_attrs = true;
8814 return NULL_TREE;
8817 /* Arguments being collected for optimization. */
8818 typedef const char *const_char_p; /* For DEF_VEC_P. */
8819 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
8822 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8823 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8824 false for #pragma GCC optimize. */
8826 bool
8827 parse_optimize_options (tree args, bool attr_p)
8829 bool ret = true;
8830 unsigned opt_argc;
8831 unsigned i;
8832 int saved_flag_strict_aliasing;
8833 const char **opt_argv;
8834 struct cl_decoded_option *decoded_options;
8835 unsigned int decoded_options_count;
8836 tree ap;
8838 /* Build up argv vector. Just in case the string is stored away, use garbage
8839 collected strings. */
8840 vec_safe_truncate (optimize_args, 0);
8841 vec_safe_push (optimize_args, (const char *) NULL);
8843 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8845 tree value = TREE_VALUE (ap);
8847 if (TREE_CODE (value) == INTEGER_CST)
8849 char buffer[20];
8850 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8851 vec_safe_push (optimize_args, ggc_strdup (buffer));
8854 else if (TREE_CODE (value) == STRING_CST)
8856 /* Split string into multiple substrings. */
8857 size_t len = TREE_STRING_LENGTH (value);
8858 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8859 char *end = p + len;
8860 char *comma;
8861 char *next_p = p;
8863 while (next_p != NULL)
8865 size_t len2;
8866 char *q, *r;
8868 p = next_p;
8869 comma = strchr (p, ',');
8870 if (comma)
8872 len2 = comma - p;
8873 *comma = '\0';
8874 next_p = comma+1;
8876 else
8878 len2 = end - p;
8879 next_p = NULL;
8882 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8884 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8885 options. */
8886 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8888 ret = false;
8889 if (attr_p)
8890 warning (OPT_Wattributes,
8891 "bad option %s to optimize attribute", p);
8892 else
8893 warning (OPT_Wpragmas,
8894 "bad option %s to pragma attribute", p);
8895 continue;
8898 if (*p != '-')
8900 *r++ = '-';
8902 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8903 itself is -Os, and any other switch begins with a -f. */
8904 if ((*p >= '0' && *p <= '9')
8905 || (p[0] == 's' && p[1] == '\0'))
8906 *r++ = 'O';
8907 else if (*p != 'O')
8908 *r++ = 'f';
8911 memcpy (r, p, len2);
8912 r[len2] = '\0';
8913 vec_safe_push (optimize_args, (const char *) q);
8919 opt_argc = optimize_args->length ();
8920 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8922 for (i = 1; i < opt_argc; i++)
8923 opt_argv[i] = (*optimize_args)[i];
8925 saved_flag_strict_aliasing = flag_strict_aliasing;
8927 /* Now parse the options. */
8928 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8929 &decoded_options,
8930 &decoded_options_count);
8931 decode_options (&global_options, &global_options_set,
8932 decoded_options, decoded_options_count,
8933 input_location, global_dc);
8935 targetm.override_options_after_change();
8937 /* Don't allow changing -fstrict-aliasing. */
8938 flag_strict_aliasing = saved_flag_strict_aliasing;
8940 optimize_args->truncate (0);
8941 return ret;
8944 /* For handling "optimize" attribute. arguments as in
8945 struct attribute_spec.handler. */
8947 static tree
8948 handle_optimize_attribute (tree *node, tree name, tree args,
8949 int ARG_UNUSED (flags), bool *no_add_attrs)
8951 /* Ensure we have a function type. */
8952 if (TREE_CODE (*node) != FUNCTION_DECL)
8954 warning (OPT_Wattributes, "%qE attribute ignored", name);
8955 *no_add_attrs = true;
8957 else
8959 struct cl_optimization cur_opts;
8960 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8962 /* Save current options. */
8963 cl_optimization_save (&cur_opts, &global_options);
8965 /* If we previously had some optimization options, use them as the
8966 default. */
8967 if (old_opts)
8968 cl_optimization_restore (&global_options,
8969 TREE_OPTIMIZATION (old_opts));
8971 /* Parse options, and update the vector. */
8972 parse_optimize_options (args, true);
8973 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8974 = build_optimization_node ();
8976 /* Restore current options. */
8977 cl_optimization_restore (&global_options, &cur_opts);
8980 return NULL_TREE;
8983 /* Handle a "no_split_stack" attribute. */
8985 static tree
8986 handle_no_split_stack_attribute (tree *node, tree name,
8987 tree ARG_UNUSED (args),
8988 int ARG_UNUSED (flags),
8989 bool *no_add_attrs)
8991 tree decl = *node;
8993 if (TREE_CODE (decl) != FUNCTION_DECL)
8995 error_at (DECL_SOURCE_LOCATION (decl),
8996 "%qE attribute applies only to functions", name);
8997 *no_add_attrs = true;
8999 else if (DECL_INITIAL (decl))
9001 error_at (DECL_SOURCE_LOCATION (decl),
9002 "can%'t set %qE attribute after definition", name);
9003 *no_add_attrs = true;
9006 return NULL_TREE;
9009 /* Check for valid arguments being passed to a function with FNTYPE.
9010 There are NARGS arguments in the array ARGARRAY. */
9011 void
9012 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9014 /* Check for null being passed in a pointer argument that must be
9015 non-null. We also need to do this if format checking is enabled. */
9017 if (warn_nonnull)
9018 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9020 /* Check for errors in format strings. */
9022 if (warn_format || warn_suggest_attribute_format)
9023 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9025 if (warn_format)
9026 check_function_sentinel (fntype, nargs, argarray);
9029 /* Generic argument checking recursion routine. PARAM is the argument to
9030 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9031 once the argument is resolved. CTX is context for the callback. */
9032 void
9033 check_function_arguments_recurse (void (*callback)
9034 (void *, tree, unsigned HOST_WIDE_INT),
9035 void *ctx, tree param,
9036 unsigned HOST_WIDE_INT param_num)
9038 if (CONVERT_EXPR_P (param)
9039 && (TYPE_PRECISION (TREE_TYPE (param))
9040 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9042 /* Strip coercion. */
9043 check_function_arguments_recurse (callback, ctx,
9044 TREE_OPERAND (param, 0), param_num);
9045 return;
9048 if (TREE_CODE (param) == CALL_EXPR)
9050 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9051 tree attrs;
9052 bool found_format_arg = false;
9054 /* See if this is a call to a known internationalization function
9055 that modifies a format arg. Such a function may have multiple
9056 format_arg attributes (for example, ngettext). */
9058 for (attrs = TYPE_ATTRIBUTES (type);
9059 attrs;
9060 attrs = TREE_CHAIN (attrs))
9061 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9063 tree inner_arg;
9064 tree format_num_expr;
9065 int format_num;
9066 int i;
9067 call_expr_arg_iterator iter;
9069 /* Extract the argument number, which was previously checked
9070 to be valid. */
9071 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9073 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
9074 && !TREE_INT_CST_HIGH (format_num_expr));
9076 format_num = TREE_INT_CST_LOW (format_num_expr);
9078 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9079 inner_arg != 0;
9080 inner_arg = next_call_expr_arg (&iter), i++)
9081 if (i == format_num)
9083 check_function_arguments_recurse (callback, ctx,
9084 inner_arg, param_num);
9085 found_format_arg = true;
9086 break;
9090 /* If we found a format_arg attribute and did a recursive check,
9091 we are done with checking this argument. Otherwise, we continue
9092 and this will be considered a non-literal. */
9093 if (found_format_arg)
9094 return;
9097 if (TREE_CODE (param) == COND_EXPR)
9099 /* Check both halves of the conditional expression. */
9100 check_function_arguments_recurse (callback, ctx,
9101 TREE_OPERAND (param, 1), param_num);
9102 check_function_arguments_recurse (callback, ctx,
9103 TREE_OPERAND (param, 2), param_num);
9104 return;
9107 (*callback) (ctx, param, param_num);
9110 /* Checks for a builtin function FNDECL that the number of arguments
9111 NARGS against the required number REQUIRED and issues an error if
9112 there is a mismatch. Returns true if the number of arguments is
9113 correct, otherwise false. */
9115 static bool
9116 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9118 if (nargs < required)
9120 error_at (input_location,
9121 "not enough arguments to function %qE", fndecl);
9122 return false;
9124 else if (nargs > required)
9126 error_at (input_location,
9127 "too many arguments to function %qE", fndecl);
9128 return false;
9130 return true;
9133 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9134 Returns false if there was an error, otherwise true. */
9136 bool
9137 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9139 if (!DECL_BUILT_IN (fndecl)
9140 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9141 return true;
9143 switch (DECL_FUNCTION_CODE (fndecl))
9145 case BUILT_IN_CONSTANT_P:
9146 return builtin_function_validate_nargs (fndecl, nargs, 1);
9148 case BUILT_IN_ISFINITE:
9149 case BUILT_IN_ISINF:
9150 case BUILT_IN_ISINF_SIGN:
9151 case BUILT_IN_ISNAN:
9152 case BUILT_IN_ISNORMAL:
9153 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9155 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9157 error ("non-floating-point argument in call to "
9158 "function %qE", fndecl);
9159 return false;
9161 return true;
9163 return false;
9165 case BUILT_IN_ISGREATER:
9166 case BUILT_IN_ISGREATEREQUAL:
9167 case BUILT_IN_ISLESS:
9168 case BUILT_IN_ISLESSEQUAL:
9169 case BUILT_IN_ISLESSGREATER:
9170 case BUILT_IN_ISUNORDERED:
9171 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9173 enum tree_code code0, code1;
9174 code0 = TREE_CODE (TREE_TYPE (args[0]));
9175 code1 = TREE_CODE (TREE_TYPE (args[1]));
9176 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9177 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9178 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9180 error ("non-floating-point arguments in call to "
9181 "function %qE", fndecl);
9182 return false;
9184 return true;
9186 return false;
9188 case BUILT_IN_FPCLASSIFY:
9189 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9191 unsigned i;
9193 for (i=0; i<5; i++)
9194 if (TREE_CODE (args[i]) != INTEGER_CST)
9196 error ("non-const integer argument %u in call to function %qE",
9197 i+1, fndecl);
9198 return false;
9201 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9203 error ("non-floating-point argument in call to function %qE",
9204 fndecl);
9205 return false;
9207 return true;
9209 return false;
9211 case BUILT_IN_ASSUME_ALIGNED:
9212 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9214 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9216 error ("non-integer argument 3 in call to function %qE", fndecl);
9217 return false;
9219 return true;
9221 return false;
9223 default:
9224 return true;
9228 /* Function to help qsort sort FIELD_DECLs by name order. */
9231 field_decl_cmp (const void *x_p, const void *y_p)
9233 const tree *const x = (const tree *const) x_p;
9234 const tree *const y = (const tree *const) y_p;
9236 if (DECL_NAME (*x) == DECL_NAME (*y))
9237 /* A nontype is "greater" than a type. */
9238 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9239 if (DECL_NAME (*x) == NULL_TREE)
9240 return -1;
9241 if (DECL_NAME (*y) == NULL_TREE)
9242 return 1;
9243 if (DECL_NAME (*x) < DECL_NAME (*y))
9244 return -1;
9245 return 1;
9248 static struct {
9249 gt_pointer_operator new_value;
9250 void *cookie;
9251 } resort_data;
9253 /* This routine compares two fields like field_decl_cmp but using the
9254 pointer operator in resort_data. */
9256 static int
9257 resort_field_decl_cmp (const void *x_p, const void *y_p)
9259 const tree *const x = (const tree *const) x_p;
9260 const tree *const y = (const tree *const) y_p;
9262 if (DECL_NAME (*x) == DECL_NAME (*y))
9263 /* A nontype is "greater" than a type. */
9264 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9265 if (DECL_NAME (*x) == NULL_TREE)
9266 return -1;
9267 if (DECL_NAME (*y) == NULL_TREE)
9268 return 1;
9270 tree d1 = DECL_NAME (*x);
9271 tree d2 = DECL_NAME (*y);
9272 resort_data.new_value (&d1, resort_data.cookie);
9273 resort_data.new_value (&d2, resort_data.cookie);
9274 if (d1 < d2)
9275 return -1;
9277 return 1;
9280 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9282 void
9283 resort_sorted_fields (void *obj,
9284 void * ARG_UNUSED (orig_obj),
9285 gt_pointer_operator new_value,
9286 void *cookie)
9288 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9289 resort_data.new_value = new_value;
9290 resort_data.cookie = cookie;
9291 qsort (&sf->elts[0], sf->len, sizeof (tree),
9292 resort_field_decl_cmp);
9295 /* Subroutine of c_parse_error.
9296 Return the result of concatenating LHS and RHS. RHS is really
9297 a string literal, its first character is indicated by RHS_START and
9298 RHS_SIZE is its length (including the terminating NUL character).
9300 The caller is responsible for deleting the returned pointer. */
9302 static char *
9303 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9305 const int lhs_size = strlen (lhs);
9306 char *result = XNEWVEC (char, lhs_size + rhs_size);
9307 strncpy (result, lhs, lhs_size);
9308 strncpy (result + lhs_size, rhs_start, rhs_size);
9309 return result;
9312 /* Issue the error given by GMSGID, indicating that it occurred before
9313 TOKEN, which had the associated VALUE. */
9315 void
9316 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9317 tree value, unsigned char token_flags)
9319 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9321 char *message = NULL;
9323 if (token_type == CPP_EOF)
9324 message = catenate_messages (gmsgid, " at end of input");
9325 else if (token_type == CPP_CHAR
9326 || token_type == CPP_WCHAR
9327 || token_type == CPP_CHAR16
9328 || token_type == CPP_CHAR32)
9330 unsigned int val = TREE_INT_CST_LOW (value);
9331 const char *prefix;
9333 switch (token_type)
9335 default:
9336 prefix = "";
9337 break;
9338 case CPP_WCHAR:
9339 prefix = "L";
9340 break;
9341 case CPP_CHAR16:
9342 prefix = "u";
9343 break;
9344 case CPP_CHAR32:
9345 prefix = "U";
9346 break;
9349 if (val <= UCHAR_MAX && ISGRAPH (val))
9350 message = catenate_messages (gmsgid, " before %s'%c'");
9351 else
9352 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9354 error (message, prefix, val);
9355 free (message);
9356 message = NULL;
9358 else if (token_type == CPP_STRING
9359 || token_type == CPP_WSTRING
9360 || token_type == CPP_STRING16
9361 || token_type == CPP_STRING32
9362 || token_type == CPP_UTF8STRING)
9363 message = catenate_messages (gmsgid, " before string constant");
9364 else if (token_type == CPP_NUMBER)
9365 message = catenate_messages (gmsgid, " before numeric constant");
9366 else if (token_type == CPP_NAME)
9368 message = catenate_messages (gmsgid, " before %qE");
9369 error (message, value);
9370 free (message);
9371 message = NULL;
9373 else if (token_type == CPP_PRAGMA)
9374 message = catenate_messages (gmsgid, " before %<#pragma%>");
9375 else if (token_type == CPP_PRAGMA_EOL)
9376 message = catenate_messages (gmsgid, " before end of line");
9377 else if (token_type == CPP_DECLTYPE)
9378 message = catenate_messages (gmsgid, " before %<decltype%>");
9379 else if (token_type < N_TTYPES)
9381 message = catenate_messages (gmsgid, " before %qs token");
9382 error (message, cpp_type2name (token_type, token_flags));
9383 free (message);
9384 message = NULL;
9386 else
9387 error (gmsgid);
9389 if (message)
9391 error (message);
9392 free (message);
9394 #undef catenate_messages
9397 /* Mapping for cpp message reasons to the options that enable them. */
9399 struct reason_option_codes_t
9401 const int reason; /* cpplib message reason. */
9402 const int option_code; /* gcc option that controls this message. */
9405 static const struct reason_option_codes_t option_codes[] = {
9406 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9407 {CPP_W_COMMENTS, OPT_Wcomment},
9408 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9409 {CPP_W_MULTICHAR, OPT_Wmultichar},
9410 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9411 {CPP_W_LONG_LONG, OPT_Wlong_long},
9412 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9413 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9414 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9415 {CPP_W_UNDEF, OPT_Wundef},
9416 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9417 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9418 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9419 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9420 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9421 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9422 {CPP_W_NONE, 0}
9425 /* Return the gcc option code associated with the reason for a cpp
9426 message, or 0 if none. */
9428 static int
9429 c_option_controlling_cpp_error (int reason)
9431 const struct reason_option_codes_t *entry;
9433 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9435 if (entry->reason == reason)
9436 return entry->option_code;
9438 return 0;
9441 /* Callback from cpp_error for PFILE to print diagnostics from the
9442 preprocessor. The diagnostic is of type LEVEL, with REASON set
9443 to the reason code if LEVEL is represents a warning, at location
9444 LOCATION unless this is after lexing and the compiler's location
9445 should be used instead, with column number possibly overridden by
9446 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9447 the arguments. Returns true if a diagnostic was emitted, false
9448 otherwise. */
9450 bool
9451 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9452 location_t location, unsigned int column_override,
9453 const char *msg, va_list *ap)
9455 diagnostic_info diagnostic;
9456 diagnostic_t dlevel;
9457 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9458 bool ret;
9460 switch (level)
9462 case CPP_DL_WARNING_SYSHDR:
9463 if (flag_no_output)
9464 return false;
9465 global_dc->dc_warn_system_headers = 1;
9466 /* Fall through. */
9467 case CPP_DL_WARNING:
9468 if (flag_no_output)
9469 return false;
9470 dlevel = DK_WARNING;
9471 break;
9472 case CPP_DL_PEDWARN:
9473 if (flag_no_output && !flag_pedantic_errors)
9474 return false;
9475 dlevel = DK_PEDWARN;
9476 break;
9477 case CPP_DL_ERROR:
9478 dlevel = DK_ERROR;
9479 break;
9480 case CPP_DL_ICE:
9481 dlevel = DK_ICE;
9482 break;
9483 case CPP_DL_NOTE:
9484 dlevel = DK_NOTE;
9485 break;
9486 case CPP_DL_FATAL:
9487 dlevel = DK_FATAL;
9488 break;
9489 default:
9490 gcc_unreachable ();
9492 if (done_lexing)
9493 location = input_location;
9494 diagnostic_set_info_translated (&diagnostic, msg, ap,
9495 location, dlevel);
9496 if (column_override)
9497 diagnostic_override_column (&diagnostic, column_override);
9498 diagnostic_override_option_index (&diagnostic,
9499 c_option_controlling_cpp_error (reason));
9500 ret = report_diagnostic (&diagnostic);
9501 if (level == CPP_DL_WARNING_SYSHDR)
9502 global_dc->dc_warn_system_headers = save_warn_system_headers;
9503 return ret;
9506 /* Convert a character from the host to the target execution character
9507 set. cpplib handles this, mostly. */
9509 HOST_WIDE_INT
9510 c_common_to_target_charset (HOST_WIDE_INT c)
9512 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9513 zero-extended under -fno-signed-char. cpplib insists that characters
9514 and character constants are always unsigned. Hence we must convert
9515 back and forth. */
9516 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9518 uc = cpp_host_to_exec_charset (parse_in, uc);
9520 if (flag_signed_char)
9521 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9522 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9523 else
9524 return uc;
9527 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9528 references with an INDIRECT_REF of a constant at the bottom; much like the
9529 traditional rendering of offsetof as a macro. Return the folded result. */
9531 tree
9532 fold_offsetof_1 (tree expr)
9534 tree base, off, t;
9536 switch (TREE_CODE (expr))
9538 case ERROR_MARK:
9539 return expr;
9541 case VAR_DECL:
9542 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9543 return error_mark_node;
9545 case CALL_EXPR:
9546 case TARGET_EXPR:
9547 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9548 return error_mark_node;
9550 case NOP_EXPR:
9551 case INDIRECT_REF:
9552 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9554 error ("cannot apply %<offsetof%> to a non constant address");
9555 return error_mark_node;
9557 return TREE_OPERAND (expr, 0);
9559 case COMPONENT_REF:
9560 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9561 if (base == error_mark_node)
9562 return base;
9564 t = TREE_OPERAND (expr, 1);
9565 if (DECL_C_BIT_FIELD (t))
9567 error ("attempt to take address of bit-field structure "
9568 "member %qD", t);
9569 return error_mark_node;
9571 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9572 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9574 / BITS_PER_UNIT));
9575 break;
9577 case ARRAY_REF:
9578 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9579 if (base == error_mark_node)
9580 return base;
9582 t = TREE_OPERAND (expr, 1);
9584 /* Check if the offset goes beyond the upper bound of the array. */
9585 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9587 tree upbound = array_ref_up_bound (expr);
9588 if (upbound != NULL_TREE
9589 && TREE_CODE (upbound) == INTEGER_CST
9590 && !tree_int_cst_equal (upbound,
9591 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9593 upbound = size_binop (PLUS_EXPR, upbound,
9594 build_int_cst (TREE_TYPE (upbound), 1));
9595 if (tree_int_cst_lt (upbound, t))
9597 tree v;
9599 for (v = TREE_OPERAND (expr, 0);
9600 TREE_CODE (v) == COMPONENT_REF;
9601 v = TREE_OPERAND (v, 0))
9602 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9603 == RECORD_TYPE)
9605 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9606 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9607 if (TREE_CODE (fld_chain) == FIELD_DECL)
9608 break;
9610 if (fld_chain)
9611 break;
9613 /* Don't warn if the array might be considered a poor
9614 man's flexible array member with a very permissive
9615 definition thereof. */
9616 if (TREE_CODE (v) == ARRAY_REF
9617 || TREE_CODE (v) == COMPONENT_REF)
9618 warning (OPT_Warray_bounds,
9619 "index %E denotes an offset "
9620 "greater than size of %qT",
9621 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9626 t = convert (sizetype, t);
9627 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9628 break;
9630 case COMPOUND_EXPR:
9631 /* Handle static members of volatile structs. */
9632 t = TREE_OPERAND (expr, 1);
9633 gcc_assert (TREE_CODE (t) == VAR_DECL);
9634 return fold_offsetof_1 (t);
9636 default:
9637 gcc_unreachable ();
9640 return fold_build_pointer_plus (base, off);
9643 /* Likewise, but convert it to the return type of offsetof. */
9645 tree
9646 fold_offsetof (tree expr)
9648 return convert (size_type_node, fold_offsetof_1 (expr));
9651 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9652 expression, because B will always be true. */
9654 void
9655 warn_for_omitted_condop (location_t location, tree cond)
9657 if (truth_value_p (TREE_CODE (cond)))
9658 warning_at (location, OPT_Wparentheses,
9659 "the omitted middle operand in ?: will always be %<true%>, "
9660 "suggest explicit middle operand");
9663 /* Give an error for storing into ARG, which is 'const'. USE indicates
9664 how ARG was being used. */
9666 void
9667 readonly_error (tree arg, enum lvalue_use use)
9669 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9670 || use == lv_asm);
9671 /* Using this macro rather than (for example) arrays of messages
9672 ensures that all the format strings are checked at compile
9673 time. */
9674 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9675 : (use == lv_increment ? (I) \
9676 : (use == lv_decrement ? (D) : (AS))))
9677 if (TREE_CODE (arg) == COMPONENT_REF)
9679 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9680 error (READONLY_MSG (G_("assignment of member "
9681 "%qD in read-only object"),
9682 G_("increment of member "
9683 "%qD in read-only object"),
9684 G_("decrement of member "
9685 "%qD in read-only object"),
9686 G_("member %qD in read-only object "
9687 "used as %<asm%> output")),
9688 TREE_OPERAND (arg, 1));
9689 else
9690 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9691 G_("increment of read-only member %qD"),
9692 G_("decrement of read-only member %qD"),
9693 G_("read-only member %qD used as %<asm%> output")),
9694 TREE_OPERAND (arg, 1));
9696 else if (TREE_CODE (arg) == VAR_DECL)
9697 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9698 G_("increment of read-only variable %qD"),
9699 G_("decrement of read-only variable %qD"),
9700 G_("read-only variable %qD used as %<asm%> output")),
9701 arg);
9702 else if (TREE_CODE (arg) == PARM_DECL)
9703 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9704 G_("increment of read-only parameter %qD"),
9705 G_("decrement of read-only parameter %qD"),
9706 G_("read-only parameter %qD use as %<asm%> output")),
9707 arg);
9708 else if (TREE_CODE (arg) == RESULT_DECL)
9710 gcc_assert (c_dialect_cxx ());
9711 error (READONLY_MSG (G_("assignment of "
9712 "read-only named return value %qD"),
9713 G_("increment of "
9714 "read-only named return value %qD"),
9715 G_("decrement of "
9716 "read-only named return value %qD"),
9717 G_("read-only named return value %qD "
9718 "used as %<asm%>output")),
9719 arg);
9721 else if (TREE_CODE (arg) == FUNCTION_DECL)
9722 error (READONLY_MSG (G_("assignment of function %qD"),
9723 G_("increment of function %qD"),
9724 G_("decrement of function %qD"),
9725 G_("function %qD used as %<asm%> output")),
9726 arg);
9727 else
9728 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9729 G_("increment of read-only location %qE"),
9730 G_("decrement of read-only location %qE"),
9731 G_("read-only location %qE used as %<asm%> output")),
9732 arg);
9735 /* Print an error message for an invalid lvalue. USE says
9736 how the lvalue is being used and so selects the error message. LOC
9737 is the location for the error. */
9739 void
9740 lvalue_error (location_t loc, enum lvalue_use use)
9742 switch (use)
9744 case lv_assign:
9745 error_at (loc, "lvalue required as left operand of assignment");
9746 break;
9747 case lv_increment:
9748 error_at (loc, "lvalue required as increment operand");
9749 break;
9750 case lv_decrement:
9751 error_at (loc, "lvalue required as decrement operand");
9752 break;
9753 case lv_addressof:
9754 error_at (loc, "lvalue required as unary %<&%> operand");
9755 break;
9756 case lv_asm:
9757 error_at (loc, "lvalue required in asm statement");
9758 break;
9759 default:
9760 gcc_unreachable ();
9764 /* Print an error message for an invalid indirection of type TYPE.
9765 ERRSTRING is the name of the operator for the indirection. */
9767 void
9768 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9770 switch (errstring)
9772 case RO_NULL:
9773 gcc_assert (c_dialect_cxx ());
9774 error_at (loc, "invalid type argument (have %qT)", type);
9775 break;
9776 case RO_ARRAY_INDEXING:
9777 error_at (loc,
9778 "invalid type argument of array indexing (have %qT)",
9779 type);
9780 break;
9781 case RO_UNARY_STAR:
9782 error_at (loc,
9783 "invalid type argument of unary %<*%> (have %qT)",
9784 type);
9785 break;
9786 case RO_ARROW:
9787 error_at (loc,
9788 "invalid type argument of %<->%> (have %qT)",
9789 type);
9790 break;
9791 case RO_IMPLICIT_CONVERSION:
9792 error_at (loc,
9793 "invalid type argument of implicit conversion (have %qT)",
9794 type);
9795 break;
9796 default:
9797 gcc_unreachable ();
9801 /* *PTYPE is an incomplete array. Complete it with a domain based on
9802 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9803 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9804 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9807 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9809 tree maxindex, type, main_type, elt, unqual_elt;
9810 int failure = 0, quals;
9811 hashval_t hashcode = 0;
9813 maxindex = size_zero_node;
9814 if (initial_value)
9816 if (TREE_CODE (initial_value) == STRING_CST)
9818 int eltsize
9819 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9820 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9822 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9824 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9826 if (vec_safe_is_empty (v))
9828 if (pedantic)
9829 failure = 3;
9830 maxindex = ssize_int (-1);
9832 else
9834 tree curindex;
9835 unsigned HOST_WIDE_INT cnt;
9836 constructor_elt *ce;
9837 bool fold_p = false;
9839 if ((*v)[0].index)
9840 maxindex = fold_convert_loc (input_location, sizetype,
9841 (*v)[0].index);
9842 curindex = maxindex;
9844 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
9846 bool curfold_p = false;
9847 if (ce->index)
9848 curindex = ce->index, curfold_p = true;
9849 else
9851 if (fold_p)
9852 curindex = fold_convert (sizetype, curindex);
9853 curindex = size_binop (PLUS_EXPR, curindex,
9854 size_one_node);
9856 if (tree_int_cst_lt (maxindex, curindex))
9857 maxindex = curindex, fold_p = curfold_p;
9859 if (fold_p)
9860 maxindex = fold_convert (sizetype, maxindex);
9863 else
9865 /* Make an error message unless that happened already. */
9866 if (initial_value != error_mark_node)
9867 failure = 1;
9870 else
9872 failure = 2;
9873 if (!do_default)
9874 return failure;
9877 type = *ptype;
9878 /* Force an indefinite layout factor. */
9879 if (upc_shared_type_p (type))
9880 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
9881 elt = TREE_TYPE (type);
9882 quals = TYPE_QUALS (strip_array_types (elt));
9883 if (quals == 0)
9884 unqual_elt = elt;
9885 else
9886 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9888 /* Using build_distinct_type_copy and modifying things afterward instead
9889 of using build_array_type to create a new type preserves all of the
9890 TYPE_LANG_FLAG_? bits that the front end may have set. */
9891 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9892 TREE_TYPE (main_type) = unqual_elt;
9893 TYPE_DOMAIN (main_type)
9894 = build_range_type (TREE_TYPE (maxindex),
9895 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9896 layout_type (main_type);
9898 /* Make sure we have the canonical MAIN_TYPE. */
9899 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9900 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9901 hashcode);
9902 main_type = type_hash_canon (hashcode, main_type);
9904 /* Fix the canonical type. */
9905 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9906 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9907 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9908 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9909 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9910 != TYPE_DOMAIN (main_type)))
9911 TYPE_CANONICAL (main_type)
9912 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9913 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9914 else
9915 TYPE_CANONICAL (main_type) = main_type;
9917 if (quals == 0)
9918 type = main_type;
9919 else
9920 type = c_build_qualified_type (main_type, quals);
9922 if (COMPLETE_TYPE_P (type)
9923 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9924 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9926 error ("size of array is too large");
9927 /* If we proceed with the array type as it is, we'll eventually
9928 crash in tree_low_cst(). */
9929 type = error_mark_node;
9932 *ptype = type;
9933 return failure;
9936 /* Like c_mark_addressable but don't check register qualifier. */
9937 void
9938 c_common_mark_addressable_vec (tree t)
9940 while (handled_component_p (t))
9941 t = TREE_OPERAND (t, 0);
9942 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9943 return;
9944 TREE_ADDRESSABLE (t) = 1;
9949 /* Used to help initialize the builtin-types.def table. When a type of
9950 the correct size doesn't exist, use error_mark_node instead of NULL.
9951 The later results in segfaults even when a decl using the type doesn't
9952 get invoked. */
9954 tree
9955 builtin_type_for_size (int size, bool unsignedp)
9957 tree type = c_common_type_for_size (size, unsignedp);
9958 return type ? type : error_mark_node;
9961 /* A helper function for resolve_overloaded_builtin in resolving the
9962 overloaded __sync_ builtins. Returns a positive power of 2 if the
9963 first operand of PARAMS is a pointer to a supported data type.
9964 Returns 0 if an error is encountered. */
9966 static int
9967 sync_resolve_size (tree function, vec<tree, va_gc> *params)
9969 tree type;
9970 int size;
9972 if (!params)
9974 error ("too few arguments to function %qE", function);
9975 return 0;
9978 type = TREE_TYPE ((*params)[0]);
9979 if (TREE_CODE (type) != POINTER_TYPE)
9980 goto incompatible;
9982 type = TREE_TYPE (type);
9983 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9984 goto incompatible;
9986 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9987 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9988 return size;
9990 incompatible:
9991 error ("incompatible type for argument %d of %qE", 1, function);
9992 return 0;
9995 /* A helper function for resolve_overloaded_builtin. Adds casts to
9996 PARAMS to make arguments match up with those of FUNCTION. Drops
9997 the variadic arguments at the end. Returns false if some error
9998 was encountered; true on success. */
10000 static bool
10001 sync_resolve_params (location_t loc, tree orig_function, tree function,
10002 vec<tree, va_gc> *params, bool orig_format)
10004 function_args_iterator iter;
10005 tree ptype;
10006 unsigned int parmnum;
10008 function_args_iter_init (&iter, TREE_TYPE (function));
10009 /* We've declared the implementation functions to use "volatile void *"
10010 as the pointer parameter, so we shouldn't get any complaints from the
10011 call to check_function_arguments what ever type the user used. */
10012 function_args_iter_next (&iter);
10013 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10015 /* For the rest of the values, we need to cast these to FTYPE, so that we
10016 don't get warnings for passing pointer types, etc. */
10017 parmnum = 0;
10018 while (1)
10020 tree val, arg_type;
10022 arg_type = function_args_iter_cond (&iter);
10023 /* XXX void_type_node belies the abstraction. */
10024 if (arg_type == void_type_node)
10025 break;
10027 ++parmnum;
10028 if (params->length () <= parmnum)
10030 error_at (loc, "too few arguments to function %qE", orig_function);
10031 return false;
10034 /* Only convert parameters if arg_type is unsigned integer type with
10035 new format sync routines, i.e. don't attempt to convert pointer
10036 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10037 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10038 kinds). */
10039 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10041 /* Ideally for the first conversion we'd use convert_for_assignment
10042 so that we get warnings for anything that doesn't match the pointer
10043 type. This isn't portable across the C and C++ front ends atm. */
10044 val = (*params)[parmnum];
10045 val = convert (ptype, val);
10046 val = convert (arg_type, val);
10047 (*params)[parmnum] = val;
10050 function_args_iter_next (&iter);
10053 /* __atomic routines are not variadic. */
10054 if (!orig_format && params->length () != parmnum + 1)
10056 error_at (loc, "too many arguments to function %qE", orig_function);
10057 return false;
10060 /* The definition of these primitives is variadic, with the remaining
10061 being "an optional list of variables protected by the memory barrier".
10062 No clue what that's supposed to mean, precisely, but we consider all
10063 call-clobbered variables to be protected so we're safe. */
10064 params->truncate (parmnum + 1);
10066 return true;
10069 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10070 RESULT to make it match the type of the first pointer argument in
10071 PARAMS. */
10073 static tree
10074 sync_resolve_return (tree first_param, tree result, bool orig_format)
10076 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10077 tree rtype = TREE_TYPE (result);
10078 ptype = TYPE_MAIN_VARIANT (ptype);
10080 /* New format doesn't require casting unless the types are the same size. */
10081 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10082 return convert (ptype, result);
10083 else
10084 return result;
10087 /* This function verifies the PARAMS to generic atomic FUNCTION.
10088 It returns the size if all the parameters are the same size, otherwise
10089 0 is returned if the parameters are invalid. */
10091 static int
10092 get_atomic_generic_size (location_t loc, tree function,
10093 vec<tree, va_gc> *params)
10095 unsigned int n_param;
10096 unsigned int n_model;
10097 unsigned int x;
10098 int size_0;
10099 tree type_0;
10101 /* Determine the parameter makeup. */
10102 switch (DECL_FUNCTION_CODE (function))
10104 case BUILT_IN_ATOMIC_EXCHANGE:
10105 n_param = 4;
10106 n_model = 1;
10107 break;
10108 case BUILT_IN_ATOMIC_LOAD:
10109 case BUILT_IN_ATOMIC_STORE:
10110 n_param = 3;
10111 n_model = 1;
10112 break;
10113 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10114 n_param = 6;
10115 n_model = 2;
10116 break;
10117 default:
10118 gcc_unreachable ();
10121 if (vec_safe_length (params) != n_param)
10123 error_at (loc, "incorrect number of arguments to function %qE", function);
10124 return 0;
10127 /* Get type of first parameter, and determine its size. */
10128 type_0 = TREE_TYPE ((*params)[0]);
10129 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10131 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10132 function);
10133 return 0;
10136 /* Types must be compile time constant sizes. */
10137 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10139 error_at (loc,
10140 "argument 1 of %qE must be a pointer to a constant size type",
10141 function);
10142 return 0;
10145 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
10147 /* Zero size objects are not allowed. */
10148 if (size_0 == 0)
10150 error_at (loc,
10151 "argument 1 of %qE must be a pointer to a nonzero size object",
10152 function);
10153 return 0;
10156 /* Check each other parameter is a pointer and the same size. */
10157 for (x = 0; x < n_param - n_model; x++)
10159 int size;
10160 tree type = TREE_TYPE ((*params)[x]);
10161 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
10162 if (n_param == 6 && x == 3)
10163 continue;
10164 if (!POINTER_TYPE_P (type))
10166 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10167 function);
10168 return 0;
10170 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
10171 if (size != size_0)
10173 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10174 function);
10175 return 0;
10179 /* Check memory model parameters for validity. */
10180 for (x = n_param - n_model ; x < n_param; x++)
10182 tree p = (*params)[x];
10183 if (TREE_CODE (p) == INTEGER_CST)
10185 int i = tree_low_cst (p, 1);
10186 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10188 warning_at (loc, OPT_Winvalid_memory_model,
10189 "invalid memory model argument %d of %qE", x + 1,
10190 function);
10193 else
10194 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10196 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10197 function);
10198 return 0;
10202 return size_0;
10206 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10207 at the beginning of the parameter list PARAMS representing the size of the
10208 objects. This is to match the library ABI requirement. LOC is the location
10209 of the function call.
10210 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10211 returned to allow the external call to be constructed. */
10213 static tree
10214 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10215 vec<tree, va_gc> *params)
10217 tree size_node;
10219 /* Insert a SIZE_T parameter as the first param. If there isn't
10220 enough space, allocate a new vector and recursively re-build with that. */
10221 if (!params->space (1))
10223 unsigned int z, len;
10224 vec<tree, va_gc> *v;
10225 tree f;
10227 len = params->length ();
10228 vec_alloc (v, len + 1);
10229 for (z = 0; z < len; z++)
10230 v->quick_push ((*params)[z]);
10231 f = build_function_call_vec (loc, function, v, NULL);
10232 vec_free (v);
10233 return f;
10236 /* Add the size parameter and leave as a function call for processing. */
10237 size_node = build_int_cst (size_type_node, n);
10238 params->quick_insert (0, size_node);
10239 return NULL_TREE;
10243 /* This will process an __atomic_exchange function call, determine whether it
10244 needs to be mapped to the _N variation, or turned into a library call.
10245 LOC is the location of the builtin call.
10246 FUNCTION is the DECL that has been invoked;
10247 PARAMS is the argument list for the call. The return value is non-null
10248 TRUE is returned if it is translated into the proper format for a call to the
10249 external library, and NEW_RETURN is set the tree for that function.
10250 FALSE is returned if processing for the _N variation is required, and
10251 NEW_RETURN is set to the the return value the result is copied into. */
10252 static bool
10253 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10254 vec<tree, va_gc> *params, tree *new_return)
10256 tree p0, p1, p2, p3;
10257 tree I_type, I_type_ptr;
10258 int n = get_atomic_generic_size (loc, function, params);
10260 /* Size of 0 is an error condition. */
10261 if (n == 0)
10263 *new_return = error_mark_node;
10264 return true;
10267 /* If not a lock-free size, change to the library generic format. */
10268 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10270 *new_return = add_atomic_size_parameter (n, loc, function, params);
10271 return true;
10274 /* Otherwise there is a lockfree match, transform the call from:
10275 void fn(T* mem, T* desired, T* return, model)
10276 into
10277 *return = (T) (fn (In* mem, (In) *desired, model)) */
10279 p0 = (*params)[0];
10280 p1 = (*params)[1];
10281 p2 = (*params)[2];
10282 p3 = (*params)[3];
10284 /* Create pointer to appropriate size. */
10285 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10286 I_type_ptr = build_pointer_type (I_type);
10288 /* Convert object pointer to required type. */
10289 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10290 (*params)[0] = p0;
10291 /* Convert new value to required type, and dereference it. */
10292 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10293 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10294 (*params)[1] = p1;
10296 /* Move memory model to the 3rd position, and end param list. */
10297 (*params)[2] = p3;
10298 params->truncate (3);
10300 /* Convert return pointer and dereference it for later assignment. */
10301 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10303 return false;
10307 /* This will process an __atomic_compare_exchange function call, determine
10308 whether it needs to be mapped to the _N variation, or turned into a lib call.
10309 LOC is the location of the builtin call.
10310 FUNCTION is the DECL that has been invoked;
10311 PARAMS is the argument list for the call. The return value is non-null
10312 TRUE is returned if it is translated into the proper format for a call to the
10313 external library, and NEW_RETURN is set the tree for that function.
10314 FALSE is returned if processing for the _N variation is required. */
10316 static bool
10317 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10318 vec<tree, va_gc> *params,
10319 tree *new_return)
10321 tree p0, p1, p2;
10322 tree I_type, I_type_ptr;
10323 int n = get_atomic_generic_size (loc, function, params);
10325 /* Size of 0 is an error condition. */
10326 if (n == 0)
10328 *new_return = error_mark_node;
10329 return true;
10332 /* If not a lock-free size, change to the library generic format. */
10333 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10335 /* The library generic format does not have the weak parameter, so
10336 remove it from the param list. Since a parameter has been removed,
10337 we can be sure that there is room for the SIZE_T parameter, meaning
10338 there will not be a recursive rebuilding of the parameter list, so
10339 there is no danger this will be done twice. */
10340 if (n > 0)
10342 (*params)[3] = (*params)[4];
10343 (*params)[4] = (*params)[5];
10344 params->truncate (5);
10346 *new_return = add_atomic_size_parameter (n, loc, function, params);
10347 return true;
10350 /* Otherwise, there is a match, so the call needs to be transformed from:
10351 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10352 into
10353 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10355 p0 = (*params)[0];
10356 p1 = (*params)[1];
10357 p2 = (*params)[2];
10359 /* Create pointer to appropriate size. */
10360 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10361 I_type_ptr = build_pointer_type (I_type);
10363 /* Convert object pointer to required type. */
10364 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10365 (*params)[0] = p0;
10367 /* Convert expected pointer to required type. */
10368 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10369 (*params)[1] = p1;
10371 /* Convert desired value to required type, and dereference it. */
10372 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10373 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10374 (*params)[2] = p2;
10376 /* The rest of the parameters are fine. NULL means no special return value
10377 processing.*/
10378 *new_return = NULL;
10379 return false;
10383 /* This will process an __atomic_load function call, determine whether it
10384 needs to be mapped to the _N variation, or turned into a library call.
10385 LOC is the location of the builtin call.
10386 FUNCTION is the DECL that has been invoked;
10387 PARAMS is the argument list for the call. The return value is non-null
10388 TRUE is returned if it is translated into the proper format for a call to the
10389 external library, and NEW_RETURN is set the tree for that function.
10390 FALSE is returned if processing for the _N variation is required, and
10391 NEW_RETURN is set to the the return value the result is copied into. */
10393 static bool
10394 resolve_overloaded_atomic_load (location_t loc, tree function,
10395 vec<tree, va_gc> *params, tree *new_return)
10397 tree p0, p1, p2;
10398 tree I_type, I_type_ptr;
10399 int n = get_atomic_generic_size (loc, function, params);
10401 /* Size of 0 is an error condition. */
10402 if (n == 0)
10404 *new_return = error_mark_node;
10405 return true;
10408 /* If not a lock-free size, change to the library generic format. */
10409 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10411 *new_return = add_atomic_size_parameter (n, loc, function, params);
10412 return true;
10415 /* Otherwise, there is a match, so the call needs to be transformed from:
10416 void fn(T* mem, T* return, model)
10417 into
10418 *return = (T) (fn ((In *) mem, model)) */
10420 p0 = (*params)[0];
10421 p1 = (*params)[1];
10422 p2 = (*params)[2];
10424 /* Create pointer to appropriate size. */
10425 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10426 I_type_ptr = build_pointer_type (I_type);
10428 /* Convert object pointer to required type. */
10429 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10430 (*params)[0] = p0;
10432 /* Move memory model to the 2nd position, and end param list. */
10433 (*params)[1] = p2;
10434 params->truncate (2);
10436 /* Convert return pointer and dereference it for later assignment. */
10437 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10439 return false;
10443 /* This will process an __atomic_store function call, determine whether it
10444 needs to be mapped to the _N variation, or turned into a library call.
10445 LOC is the location of the builtin call.
10446 FUNCTION is the DECL that has been invoked;
10447 PARAMS is the argument list for the call. The return value is non-null
10448 TRUE is returned if it is translated into the proper format for a call to the
10449 external library, and NEW_RETURN is set the tree for that function.
10450 FALSE is returned if processing for the _N variation is required, and
10451 NEW_RETURN is set to the the return value the result is copied into. */
10453 static bool
10454 resolve_overloaded_atomic_store (location_t loc, tree function,
10455 vec<tree, va_gc> *params, tree *new_return)
10457 tree p0, p1;
10458 tree I_type, I_type_ptr;
10459 int n = get_atomic_generic_size (loc, function, params);
10461 /* Size of 0 is an error condition. */
10462 if (n == 0)
10464 *new_return = error_mark_node;
10465 return true;
10468 /* If not a lock-free size, change to the library generic format. */
10469 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10471 *new_return = add_atomic_size_parameter (n, loc, function, params);
10472 return true;
10475 /* Otherwise, there is a match, so the call needs to be transformed from:
10476 void fn(T* mem, T* value, model)
10477 into
10478 fn ((In *) mem, (In) *value, model) */
10480 p0 = (*params)[0];
10481 p1 = (*params)[1];
10483 /* Create pointer to appropriate size. */
10484 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10485 I_type_ptr = build_pointer_type (I_type);
10487 /* Convert object pointer to required type. */
10488 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10489 (*params)[0] = p0;
10491 /* Convert new value to required type, and dereference it. */
10492 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10493 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10494 (*params)[1] = p1;
10496 /* The memory model is in the right spot already. Return is void. */
10497 *new_return = NULL_TREE;
10499 return false;
10503 /* Some builtin functions are placeholders for other expressions. This
10504 function should be called immediately after parsing the call expression
10505 before surrounding code has committed to the type of the expression.
10507 LOC is the location of the builtin call.
10509 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10510 PARAMS is the argument list for the call. The return value is non-null
10511 when expansion is complete, and null if normal processing should
10512 continue. */
10514 tree
10515 resolve_overloaded_builtin (location_t loc, tree function,
10516 vec<tree, va_gc> *params)
10518 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10519 bool orig_format = true;
10520 tree new_return = NULL_TREE;
10522 switch (DECL_BUILT_IN_CLASS (function))
10524 case BUILT_IN_NORMAL:
10525 break;
10526 case BUILT_IN_MD:
10527 if (targetm.resolve_overloaded_builtin)
10528 return targetm.resolve_overloaded_builtin (loc, function, params);
10529 else
10530 return NULL_TREE;
10531 default:
10532 return NULL_TREE;
10535 /* Handle BUILT_IN_NORMAL here. */
10536 switch (orig_code)
10538 case BUILT_IN_ATOMIC_EXCHANGE:
10539 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10540 case BUILT_IN_ATOMIC_LOAD:
10541 case BUILT_IN_ATOMIC_STORE:
10543 /* Handle these 4 together so that they can fall through to the next
10544 case if the call is transformed to an _N variant. */
10545 switch (orig_code)
10547 case BUILT_IN_ATOMIC_EXCHANGE:
10549 if (resolve_overloaded_atomic_exchange (loc, function, params,
10550 &new_return))
10551 return new_return;
10552 /* Change to the _N variant. */
10553 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10554 break;
10557 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10559 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10560 params,
10561 &new_return))
10562 return new_return;
10563 /* Change to the _N variant. */
10564 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10565 break;
10567 case BUILT_IN_ATOMIC_LOAD:
10569 if (resolve_overloaded_atomic_load (loc, function, params,
10570 &new_return))
10571 return new_return;
10572 /* Change to the _N variant. */
10573 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10574 break;
10576 case BUILT_IN_ATOMIC_STORE:
10578 if (resolve_overloaded_atomic_store (loc, function, params,
10579 &new_return))
10580 return new_return;
10581 /* Change to the _N variant. */
10582 orig_code = BUILT_IN_ATOMIC_STORE_N;
10583 break;
10585 default:
10586 gcc_unreachable ();
10588 /* Fallthrough to the normal processing. */
10590 case BUILT_IN_ATOMIC_EXCHANGE_N:
10591 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10592 case BUILT_IN_ATOMIC_LOAD_N:
10593 case BUILT_IN_ATOMIC_STORE_N:
10594 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10595 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10596 case BUILT_IN_ATOMIC_AND_FETCH_N:
10597 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10598 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10599 case BUILT_IN_ATOMIC_OR_FETCH_N:
10600 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10601 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10602 case BUILT_IN_ATOMIC_FETCH_AND_N:
10603 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10604 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10605 case BUILT_IN_ATOMIC_FETCH_OR_N:
10607 orig_format = false;
10608 /* Fallthru for parameter processing. */
10610 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10611 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10612 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10613 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10614 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10615 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10616 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10617 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10618 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10619 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10620 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10621 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10622 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10623 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10624 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10625 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10627 int n = sync_resolve_size (function, params);
10628 tree new_function, first_param, result;
10629 enum built_in_function fncode;
10631 if (n == 0)
10632 return error_mark_node;
10634 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10635 new_function = builtin_decl_explicit (fncode);
10636 if (!sync_resolve_params (loc, function, new_function, params,
10637 orig_format))
10638 return error_mark_node;
10640 first_param = (*params)[0];
10641 result = build_function_call_vec (loc, new_function, params, NULL);
10642 if (result == error_mark_node)
10643 return result;
10644 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10645 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10646 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10647 result = sync_resolve_return (first_param, result, orig_format);
10649 /* If new_return is set, assign function to that expr and cast the
10650 result to void since the generic interface returned void. */
10651 if (new_return)
10653 /* Cast function result from I{1,2,4,8,16} to the required type. */
10654 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10655 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10656 result);
10657 TREE_SIDE_EFFECTS (result) = 1;
10658 protected_set_expr_location (result, loc);
10659 result = convert (void_type_node, result);
10661 return result;
10664 default:
10665 return NULL_TREE;
10669 /* Ignoring their sign, return true if two scalar types are the same. */
10670 bool
10671 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10673 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10675 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10676 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10677 || c2 == FIXED_POINT_TYPE));
10679 /* Equality works here because c_common_signed_type uses
10680 TYPE_MAIN_VARIANT. */
10681 return c_common_signed_type (t1)
10682 == c_common_signed_type (t2);
10685 /* Check for missing format attributes on function pointers. LTYPE is
10686 the new type or left-hand side type. RTYPE is the old type or
10687 right-hand side type. Returns TRUE if LTYPE is missing the desired
10688 attribute. */
10690 bool
10691 check_missing_format_attribute (tree ltype, tree rtype)
10693 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10694 tree ra;
10696 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10697 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10698 break;
10699 if (ra)
10701 tree la;
10702 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10703 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10704 break;
10705 return !la;
10707 else
10708 return false;
10711 /* Subscripting with type char is likely to lose on a machine where
10712 chars are signed. So warn on any machine, but optionally. Don't
10713 warn for unsigned char since that type is safe. Don't warn for
10714 signed char because anyone who uses that must have done so
10715 deliberately. Furthermore, we reduce the false positive load by
10716 warning only for non-constant value of type char. */
10718 void
10719 warn_array_subscript_with_type_char (tree index)
10721 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10722 && TREE_CODE (index) != INTEGER_CST)
10723 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10726 /* Implement -Wparentheses for the unexpected C precedence rules, to
10727 cover cases like x + y << z which readers are likely to
10728 misinterpret. We have seen an expression in which CODE is a binary
10729 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10730 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10731 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10732 expression was not formed using a binary or unary operator, or it
10733 was enclosed in parentheses. */
10735 void
10736 warn_about_parentheses (location_t loc, enum tree_code code,
10737 enum tree_code code_left, tree arg_left,
10738 enum tree_code code_right, tree arg_right)
10740 if (!warn_parentheses)
10741 return;
10743 /* This macro tests that the expression ARG with original tree code
10744 CODE appears to be a boolean expression. or the result of folding a
10745 boolean expression. */
10746 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10747 (truth_value_p (TREE_CODE (ARG)) \
10748 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10749 /* Folding may create 0 or 1 integers from other expressions. */ \
10750 || ((CODE) != INTEGER_CST \
10751 && (integer_onep (ARG) || integer_zerop (ARG))))
10753 switch (code)
10755 case LSHIFT_EXPR:
10756 if (code_left == PLUS_EXPR)
10757 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10758 "suggest parentheses around %<+%> inside %<<<%>");
10759 else if (code_right == PLUS_EXPR)
10760 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10761 "suggest parentheses around %<+%> inside %<<<%>");
10762 else if (code_left == MINUS_EXPR)
10763 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10764 "suggest parentheses around %<-%> inside %<<<%>");
10765 else if (code_right == MINUS_EXPR)
10766 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10767 "suggest parentheses around %<-%> inside %<<<%>");
10768 return;
10770 case RSHIFT_EXPR:
10771 if (code_left == PLUS_EXPR)
10772 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10773 "suggest parentheses around %<+%> inside %<>>%>");
10774 else if (code_right == PLUS_EXPR)
10775 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10776 "suggest parentheses around %<+%> inside %<>>%>");
10777 else if (code_left == MINUS_EXPR)
10778 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10779 "suggest parentheses around %<-%> inside %<>>%>");
10780 else if (code_right == MINUS_EXPR)
10781 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10782 "suggest parentheses around %<-%> inside %<>>%>");
10783 return;
10785 case TRUTH_ORIF_EXPR:
10786 if (code_left == TRUTH_ANDIF_EXPR)
10787 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10788 "suggest parentheses around %<&&%> within %<||%>");
10789 else if (code_right == TRUTH_ANDIF_EXPR)
10790 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10791 "suggest parentheses around %<&&%> within %<||%>");
10792 return;
10794 case BIT_IOR_EXPR:
10795 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10796 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10797 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10798 "suggest parentheses around arithmetic in operand of %<|%>");
10799 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10800 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10801 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10802 "suggest parentheses around arithmetic in operand of %<|%>");
10803 /* Check cases like x|y==z */
10804 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10805 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10806 "suggest parentheses around comparison in operand of %<|%>");
10807 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10808 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10809 "suggest parentheses around comparison in operand of %<|%>");
10810 /* Check cases like !x | y */
10811 else if (code_left == TRUTH_NOT_EXPR
10812 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10813 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10814 "suggest parentheses around operand of "
10815 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10816 return;
10818 case BIT_XOR_EXPR:
10819 if (code_left == BIT_AND_EXPR
10820 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
10821 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10822 "suggest parentheses around arithmetic in operand of %<^%>");
10823 else if (code_right == BIT_AND_EXPR
10824 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10825 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10826 "suggest parentheses around arithmetic in operand of %<^%>");
10827 /* Check cases like x^y==z */
10828 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10829 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10830 "suggest parentheses around comparison in operand of %<^%>");
10831 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10832 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10833 "suggest parentheses around comparison in operand of %<^%>");
10834 return;
10836 case BIT_AND_EXPR:
10837 if (code_left == PLUS_EXPR)
10838 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10839 "suggest parentheses around %<+%> in operand of %<&%>");
10840 else if (code_right == PLUS_EXPR)
10841 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10842 "suggest parentheses around %<+%> in operand of %<&%>");
10843 else if (code_left == MINUS_EXPR)
10844 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10845 "suggest parentheses around %<-%> in operand of %<&%>");
10846 else if (code_right == MINUS_EXPR)
10847 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10848 "suggest parentheses around %<-%> in operand of %<&%>");
10849 /* Check cases like x&y==z */
10850 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10851 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10852 "suggest parentheses around comparison in operand of %<&%>");
10853 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10854 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10855 "suggest parentheses around comparison in operand of %<&%>");
10856 /* Check cases like !x & y */
10857 else if (code_left == TRUTH_NOT_EXPR
10858 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10859 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10860 "suggest parentheses around operand of "
10861 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10862 return;
10864 case EQ_EXPR:
10865 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10866 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10867 "suggest parentheses around comparison in operand of %<==%>");
10868 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10869 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10870 "suggest parentheses around comparison in operand of %<==%>");
10871 return;
10872 case NE_EXPR:
10873 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
10874 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10875 "suggest parentheses around comparison in operand of %<!=%>");
10876 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
10877 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10878 "suggest parentheses around comparison in operand of %<!=%>");
10879 return;
10881 default:
10882 if (TREE_CODE_CLASS (code) == tcc_comparison)
10884 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10885 && code_left != NE_EXPR && code_left != EQ_EXPR
10886 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10887 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
10888 "comparisons like %<X<=Y<=Z%> do not "
10889 "have their mathematical meaning");
10890 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
10891 && code_right != NE_EXPR && code_right != EQ_EXPR
10892 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
10893 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
10894 "comparisons like %<X<=Y<=Z%> do not "
10895 "have their mathematical meaning");
10897 return;
10899 #undef NOT_A_BOOLEAN_EXPR_P
10902 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10904 void
10905 warn_for_unused_label (tree label)
10907 if (!TREE_USED (label))
10909 if (DECL_INITIAL (label))
10910 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10911 else
10912 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10916 /* Warn for division by zero according to the value of DIVISOR. LOC
10917 is the location of the division operator. */
10919 void
10920 warn_for_div_by_zero (location_t loc, tree divisor)
10922 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10923 about division by zero. Do not issue a warning if DIVISOR has a
10924 floating-point type, since we consider 0.0/0.0 a valid way of
10925 generating a NaN. */
10926 if (c_inhibit_evaluation_warnings == 0
10927 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10928 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10931 /* Subroutine of build_binary_op. Give warnings for comparisons
10932 between signed and unsigned quantities that may fail. Do the
10933 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10934 so that casts will be considered, but default promotions won't
10937 LOCATION is the location of the comparison operator.
10939 The arguments of this function map directly to local variables
10940 of build_binary_op. */
10942 void
10943 warn_for_sign_compare (location_t location,
10944 tree orig_op0, tree orig_op1,
10945 tree op0, tree op1,
10946 tree result_type, enum tree_code resultcode)
10948 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10949 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10950 int unsignedp0, unsignedp1;
10952 /* In C++, check for comparison of different enum types. */
10953 if (c_dialect_cxx()
10954 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10955 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10956 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10957 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10959 warning_at (location,
10960 OPT_Wsign_compare, "comparison between types %qT and %qT",
10961 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10964 /* Do not warn if the comparison is being done in a signed type,
10965 since the signed type will only be chosen if it can represent
10966 all the values of the unsigned type. */
10967 if (!TYPE_UNSIGNED (result_type))
10968 /* OK */;
10969 /* Do not warn if both operands are unsigned. */
10970 else if (op0_signed == op1_signed)
10971 /* OK */;
10972 else
10974 tree sop, uop, base_type;
10975 bool ovf;
10977 if (op0_signed)
10978 sop = orig_op0, uop = orig_op1;
10979 else
10980 sop = orig_op1, uop = orig_op0;
10982 STRIP_TYPE_NOPS (sop);
10983 STRIP_TYPE_NOPS (uop);
10984 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10985 ? TREE_TYPE (result_type) : result_type);
10987 /* Do not warn if the signed quantity is an unsuffixed integer
10988 literal (or some static constant expression involving such
10989 literals or a conditional expression involving such literals)
10990 and it is non-negative. */
10991 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10992 /* OK */;
10993 /* Do not warn if the comparison is an equality operation, the
10994 unsigned quantity is an integral constant, and it would fit
10995 in the result if the result were signed. */
10996 else if (TREE_CODE (uop) == INTEGER_CST
10997 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10998 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10999 /* OK */;
11000 /* In C, do not warn if the unsigned quantity is an enumeration
11001 constant and its maximum value would fit in the result if the
11002 result were signed. */
11003 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11004 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11005 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11006 c_common_signed_type (base_type)))
11007 /* OK */;
11008 else
11009 warning_at (location,
11010 OPT_Wsign_compare,
11011 "comparison between signed and unsigned integer expressions");
11014 /* Warn if two unsigned values are being compared in a size larger
11015 than their original size, and one (and only one) is the result of
11016 a `~' operator. This comparison will always fail.
11018 Also warn if one operand is a constant, and the constant does not
11019 have all bits set that are set in the ~ operand when it is
11020 extended. */
11022 op0 = c_common_get_narrower (op0, &unsignedp0);
11023 op1 = c_common_get_narrower (op1, &unsignedp1);
11025 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11026 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11028 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11029 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11030 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11031 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11033 if (host_integerp (op0, 0) || host_integerp (op1, 0))
11035 tree primop;
11036 HOST_WIDE_INT constant, mask;
11037 int unsignedp;
11038 unsigned int bits;
11040 if (host_integerp (op0, 0))
11042 primop = op1;
11043 unsignedp = unsignedp1;
11044 constant = tree_low_cst (op0, 0);
11046 else
11048 primop = op0;
11049 unsignedp = unsignedp0;
11050 constant = tree_low_cst (op1, 0);
11053 bits = TYPE_PRECISION (TREE_TYPE (primop));
11054 if (bits < TYPE_PRECISION (result_type)
11055 && bits < HOST_BITS_PER_LONG && unsignedp)
11057 mask = (~ (HOST_WIDE_INT) 0) << bits;
11058 if ((mask & constant) != mask)
11060 if (constant == 0)
11061 warning (OPT_Wsign_compare,
11062 "promoted ~unsigned is always non-zero");
11063 else
11064 warning_at (location, OPT_Wsign_compare,
11065 "comparison of promoted ~unsigned with constant");
11069 else if (unsignedp0 && unsignedp1
11070 && (TYPE_PRECISION (TREE_TYPE (op0))
11071 < TYPE_PRECISION (result_type))
11072 && (TYPE_PRECISION (TREE_TYPE (op1))
11073 < TYPE_PRECISION (result_type)))
11074 warning_at (location, OPT_Wsign_compare,
11075 "comparison of promoted ~unsigned with unsigned");
11079 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11080 type via c_common_type. If -Wdouble-promotion is in use, and the
11081 conditions for warning have been met, issue a warning. GMSGID is
11082 the warning message. It must have two %T specifiers for the type
11083 that was converted (generally "float") and the type to which it was
11084 converted (generally "double), respectively. LOC is the location
11085 to which the awrning should refer. */
11087 void
11088 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11089 const char *gmsgid, location_t loc)
11091 tree source_type;
11093 if (!warn_double_promotion)
11094 return;
11095 /* If the conversion will not occur at run-time, there is no need to
11096 warn about it. */
11097 if (c_inhibit_evaluation_warnings)
11098 return;
11099 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11100 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11101 return;
11102 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11103 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11104 source_type = type1;
11105 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11106 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11107 source_type = type2;
11108 else
11109 return;
11110 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11113 /* Setup a TYPE_DECL node as a typedef representation.
11115 X is a TYPE_DECL for a typedef statement. Create a brand new
11116 ..._TYPE node (which will be just a variant of the existing
11117 ..._TYPE node with identical properties) and then install X
11118 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11120 The whole point here is to end up with a situation where each
11121 and every ..._TYPE node the compiler creates will be uniquely
11122 associated with AT MOST one node representing a typedef name.
11123 This way, even though the compiler substitutes corresponding
11124 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11125 early on, later parts of the compiler can always do the reverse
11126 translation and get back the corresponding typedef name. For
11127 example, given:
11129 typedef struct S MY_TYPE;
11130 MY_TYPE object;
11132 Later parts of the compiler might only know that `object' was of
11133 type `struct S' if it were not for code just below. With this
11134 code however, later parts of the compiler see something like:
11136 struct S' == struct S
11137 typedef struct S' MY_TYPE;
11138 struct S' object;
11140 And they can then deduce (from the node for type struct S') that
11141 the original object declaration was:
11143 MY_TYPE object;
11145 Being able to do this is important for proper support of protoize,
11146 and also for generating precise symbolic debugging information
11147 which takes full account of the programmer's (typedef) vocabulary.
11149 Obviously, we don't want to generate a duplicate ..._TYPE node if
11150 the TYPE_DECL node that we are now processing really represents a
11151 standard built-in type. */
11153 void
11154 set_underlying_type (tree x)
11156 if (x == error_mark_node)
11157 return;
11158 if (DECL_IS_BUILTIN (x))
11160 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11161 TYPE_NAME (TREE_TYPE (x)) = x;
11163 else if (TREE_TYPE (x) != error_mark_node
11164 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11166 tree tt = TREE_TYPE (x);
11167 DECL_ORIGINAL_TYPE (x) = tt;
11168 tt = build_variant_type_copy (tt);
11169 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11170 TYPE_NAME (tt) = x;
11171 TREE_USED (tt) = TREE_USED (x);
11172 TREE_TYPE (x) = tt;
11176 /* Record the types used by the current global variable declaration
11177 being parsed, so that we can decide later to emit their debug info.
11178 Those types are in types_used_by_cur_var_decl, and we are going to
11179 store them in the types_used_by_vars_hash hash table.
11180 DECL is the declaration of the global variable that has been parsed. */
11182 void
11183 record_types_used_by_current_var_decl (tree decl)
11185 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11187 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11189 tree type = types_used_by_cur_var_decl->pop ();
11190 types_used_by_var_decl_insert (type, decl);
11194 /* If DECL is a typedef that is declared in the current function,
11195 record it for the purpose of -Wunused-local-typedefs. */
11197 void
11198 record_locally_defined_typedef (tree decl)
11200 struct c_language_function *l;
11202 if (!warn_unused_local_typedefs
11203 || cfun == NULL
11204 /* if this is not a locally defined typedef then we are not
11205 interested. */
11206 || !is_typedef_decl (decl)
11207 || !decl_function_context (decl))
11208 return;
11210 l = (struct c_language_function *) cfun->language;
11211 vec_safe_push (l->local_typedefs, decl);
11214 /* If T is a TYPE_DECL declared locally, mark it as used. */
11216 void
11217 maybe_record_typedef_use (tree t)
11219 if (!is_typedef_decl (t))
11220 return;
11222 TREE_USED (t) = true;
11225 /* Warn if there are some unused locally defined typedefs in the
11226 current function. */
11228 void
11229 maybe_warn_unused_local_typedefs (void)
11231 int i;
11232 tree decl;
11233 /* The number of times we have emitted -Wunused-local-typedefs
11234 warnings. If this is different from errorcount, that means some
11235 unrelated errors have been issued. In which case, we'll avoid
11236 emitting "unused-local-typedefs" warnings. */
11237 static int unused_local_typedefs_warn_count;
11238 struct c_language_function *l;
11240 if (cfun == NULL)
11241 return;
11243 if ((l = (struct c_language_function *) cfun->language) == NULL)
11244 return;
11246 if (warn_unused_local_typedefs
11247 && errorcount == unused_local_typedefs_warn_count)
11249 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11250 if (!TREE_USED (decl))
11251 warning_at (DECL_SOURCE_LOCATION (decl),
11252 OPT_Wunused_local_typedefs,
11253 "typedef %qD locally defined but not used", decl);
11254 unused_local_typedefs_warn_count = errorcount;
11257 vec_free (l->local_typedefs);
11260 /* The C and C++ parsers both use vectors to hold function arguments.
11261 For efficiency, we keep a cache of unused vectors. This is the
11262 cache. */
11264 typedef vec<tree, va_gc> *tree_gc_vec;
11265 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11267 /* Return a new vector from the cache. If the cache is empty,
11268 allocate a new vector. These vectors are GC'ed, so it is OK if the
11269 pointer is not released.. */
11271 vec<tree, va_gc> *
11272 make_tree_vector (void)
11274 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11275 return tree_vector_cache->pop ();
11276 else
11278 /* Passing 0 to vec::alloc returns NULL, and our callers require
11279 that we always return a non-NULL value. The vector code uses
11280 4 when growing a NULL vector, so we do too. */
11281 vec<tree, va_gc> *v;
11282 vec_alloc (v, 4);
11283 return v;
11287 /* Release a vector of trees back to the cache. */
11289 void
11290 release_tree_vector (vec<tree, va_gc> *vec)
11292 if (vec != NULL)
11294 vec->truncate (0);
11295 vec_safe_push (tree_vector_cache, vec);
11299 /* Get a new tree vector holding a single tree. */
11301 vec<tree, va_gc> *
11302 make_tree_vector_single (tree t)
11304 vec<tree, va_gc> *ret = make_tree_vector ();
11305 ret->quick_push (t);
11306 return ret;
11309 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11311 vec<tree, va_gc> *
11312 make_tree_vector_from_list (tree list)
11314 vec<tree, va_gc> *ret = make_tree_vector ();
11315 for (; list; list = TREE_CHAIN (list))
11316 vec_safe_push (ret, TREE_VALUE (list));
11317 return ret;
11320 /* Get a new tree vector which is a copy of an existing one. */
11322 vec<tree, va_gc> *
11323 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11325 vec<tree, va_gc> *ret;
11326 unsigned int ix;
11327 tree t;
11329 ret = make_tree_vector ();
11330 vec_safe_reserve (ret, vec_safe_length (orig));
11331 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11332 ret->quick_push (t);
11333 return ret;
11336 /* Return true if KEYWORD starts a type specifier. */
11338 bool
11339 keyword_begins_type_specifier (enum rid keyword)
11341 switch (keyword)
11343 case RID_INT:
11344 case RID_CHAR:
11345 case RID_FLOAT:
11346 case RID_DOUBLE:
11347 case RID_VOID:
11348 case RID_INT128:
11349 case RID_UNSIGNED:
11350 case RID_LONG:
11351 case RID_SHORT:
11352 case RID_SIGNED:
11353 case RID_DFLOAT32:
11354 case RID_DFLOAT64:
11355 case RID_DFLOAT128:
11356 case RID_FRACT:
11357 case RID_ACCUM:
11358 case RID_BOOL:
11359 case RID_WCHAR:
11360 case RID_CHAR16:
11361 case RID_CHAR32:
11362 case RID_SAT:
11363 case RID_COMPLEX:
11364 case RID_TYPEOF:
11365 case RID_STRUCT:
11366 case RID_CLASS:
11367 case RID_UNION:
11368 case RID_ENUM:
11369 return true;
11370 default:
11371 return false;
11375 /* Return true if KEYWORD names a type qualifier. */
11377 bool
11378 keyword_is_type_qualifier (enum rid keyword)
11380 switch (keyword)
11382 case RID_CONST:
11383 case RID_VOLATILE:
11384 case RID_RESTRICT:
11385 return true;
11386 default:
11387 return false;
11391 /* Return true if KEYWORD names a storage class specifier.
11393 RID_TYPEDEF is not included in this list despite `typedef' being
11394 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11395 such for syntactic convenience only. */
11397 bool
11398 keyword_is_storage_class_specifier (enum rid keyword)
11400 switch (keyword)
11402 case RID_STATIC:
11403 case RID_EXTERN:
11404 case RID_REGISTER:
11405 case RID_AUTO:
11406 case RID_MUTABLE:
11407 case RID_THREAD:
11408 return true;
11409 default:
11410 return false;
11414 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11416 static bool
11417 keyword_is_function_specifier (enum rid keyword)
11419 switch (keyword)
11421 case RID_INLINE:
11422 case RID_NORETURN:
11423 case RID_VIRTUAL:
11424 case RID_EXPLICIT:
11425 return true;
11426 default:
11427 return false;
11431 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11432 declaration-specifier (C99 6.7). */
11434 bool
11435 keyword_is_decl_specifier (enum rid keyword)
11437 if (keyword_is_storage_class_specifier (keyword)
11438 || keyword_is_type_qualifier (keyword)
11439 || keyword_is_function_specifier (keyword))
11440 return true;
11442 switch (keyword)
11444 case RID_TYPEDEF:
11445 case RID_FRIEND:
11446 case RID_CONSTEXPR:
11447 return true;
11448 default:
11449 return false;
11453 /* Initialize language-specific-bits of tree_contains_struct. */
11455 void
11456 c_common_init_ts (void)
11458 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11459 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11462 /* Build a user-defined numeric literal out of an integer constant type VALUE
11463 with identifier SUFFIX. */
11465 tree
11466 build_userdef_literal (tree suffix_id, tree value,
11467 enum overflow_type overflow, tree num_string)
11469 tree literal = make_node (USERDEF_LITERAL);
11470 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11471 USERDEF_LITERAL_VALUE (literal) = value;
11472 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11473 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11474 return literal;
11477 /* For vector[index], convert the vector to a
11478 pointer of the underlying type. */
11479 void
11480 convert_vector_to_pointer_for_subscript (location_t loc,
11481 tree* vecp, tree index)
11483 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11485 tree type = TREE_TYPE (*vecp);
11486 tree type1;
11488 if (TREE_CODE (index) == INTEGER_CST)
11489 if (!host_integerp (index, 1)
11490 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11491 >= TYPE_VECTOR_SUBPARTS (type)))
11492 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11494 c_common_mark_addressable_vec (*vecp);
11495 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11496 type = build_pointer_type (type);
11497 type1 = build_pointer_type (TREE_TYPE (*vecp));
11498 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11499 *vecp = convert (type, *vecp);
11503 /* Determine which of the operands, if any, is a scalar that needs to be
11504 converted to a vector, for the range of operations. */
11505 enum stv_conv
11506 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11507 bool complain)
11509 tree type0 = TREE_TYPE (op0);
11510 tree type1 = TREE_TYPE (op1);
11511 bool integer_only_op = false;
11512 enum stv_conv ret = stv_firstarg;
11514 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11515 || TREE_CODE (type1) == VECTOR_TYPE);
11516 switch (code)
11518 /* Most GENERIC binary expressions require homogeneous arguments.
11519 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11520 argument that is a vector and a second one that is a scalar, so
11521 we never return stv_secondarg for them. */
11522 case RSHIFT_EXPR:
11523 case LSHIFT_EXPR:
11524 if (TREE_CODE (type0) == INTEGER_TYPE
11525 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11527 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11529 if (complain)
11530 error_at (loc, "conversion of scalar %qT to vector %qT "
11531 "involves truncation", type0, type1);
11532 return stv_error;
11534 else
11535 return stv_firstarg;
11537 break;
11539 case BIT_IOR_EXPR:
11540 case BIT_XOR_EXPR:
11541 case BIT_AND_EXPR:
11542 integer_only_op = true;
11543 /* ... fall through ... */
11545 case VEC_COND_EXPR:
11547 case PLUS_EXPR:
11548 case MINUS_EXPR:
11549 case MULT_EXPR:
11550 case TRUNC_DIV_EXPR:
11551 case CEIL_DIV_EXPR:
11552 case FLOOR_DIV_EXPR:
11553 case ROUND_DIV_EXPR:
11554 case EXACT_DIV_EXPR:
11555 case TRUNC_MOD_EXPR:
11556 case FLOOR_MOD_EXPR:
11557 case RDIV_EXPR:
11558 case EQ_EXPR:
11559 case NE_EXPR:
11560 case LE_EXPR:
11561 case GE_EXPR:
11562 case LT_EXPR:
11563 case GT_EXPR:
11564 /* What about UNLT_EXPR? */
11565 if (TREE_CODE (type0) == VECTOR_TYPE)
11567 tree tmp;
11568 ret = stv_secondarg;
11569 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11570 tmp = type0; type0 = type1; type1 = tmp;
11571 tmp = op0; op0 = op1; op1 = tmp;
11574 if (TREE_CODE (type0) == INTEGER_TYPE
11575 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11577 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11579 if (complain)
11580 error_at (loc, "conversion of scalar %qT to vector %qT "
11581 "involves truncation", type0, type1);
11582 return stv_error;
11584 return ret;
11586 else if (!integer_only_op
11587 /* Allow integer --> real conversion if safe. */
11588 && (TREE_CODE (type0) == REAL_TYPE
11589 || TREE_CODE (type0) == INTEGER_TYPE)
11590 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11592 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
11594 if (complain)
11595 error_at (loc, "conversion of scalar %qT to vector %qT "
11596 "involves truncation", type0, type1);
11597 return stv_error;
11599 return ret;
11601 default:
11602 break;
11605 return stv_nothing;
11608 /* Return true iff ALIGN is an integral constant that is a fundamental
11609 alignment, as defined by [basic.align] in the c++-11
11610 specifications.
11612 That is:
11614 [A fundamental alignment is represented by an alignment less than or
11615 equal to the greatest alignment supported by the implementation
11616 in all contexts, which is equal to
11617 alignof(max_align_t)]. */
11619 bool
11620 cxx_fundamental_alignment_p (unsigned align)
11622 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11623 TYPE_ALIGN (long_double_type_node)));
11626 #include "gt-c-family-c-common.h"