Merge trunk version 190524 into gupc branch.
[official-gcc.git] / gcc / c-family / c-common.c
blobfd521e6552bb929a049194c5b7a99187f326a7f0
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 -fupc-pthreads-per-process-N switch tells the UPC compiler
206 and runtime to map N UPC threads per process onto
207 N POSIX threads running inside the process. */
208 int flag_upc_pthreads_per_process;
210 /* The implementation model for UPC threads that
211 are mapped to POSIX threads, specified at compilation
212 time by the -fupc-pthreads-model-* switch. */
213 upc_pthreads_model_kind upc_pthreads_model;
215 /* Warn about #pragma directives that are not recognized. */
217 int warn_unknown_pragmas; /* Tri state variable. */
219 /* Warn about format/argument anomalies in calls to formatted I/O functions
220 (*printf, *scanf, strftime, strfmon, etc.). */
222 int warn_format;
224 /* C/ObjC language option variables. */
227 /* Nonzero means allow type mismatches in conditional expressions;
228 just make their values `void'. */
230 int flag_cond_mismatch;
232 /* Nonzero means enable C89 Amendment 1 features. */
234 int flag_isoc94;
236 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
238 int flag_isoc99;
240 /* Nonzero means use the ISO C11 dialect of C. */
242 int flag_isoc11;
244 /* Nonzero means that we have builtin functions, and main is an int. */
246 int flag_hosted = 1;
249 /* ObjC language option variables. */
252 /* Tells the compiler that this is a special run. Do not perform any
253 compiling, instead we are to test some platform dependent features
254 and output a C header file with appropriate definitions. */
256 int print_struct_values;
258 /* Tells the compiler what is the constant string class for ObjC. */
260 const char *constant_string_class_name;
263 /* C++ language option variables. */
266 /* Nonzero means generate separate instantiation control files and
267 juggle them at link time. */
269 int flag_use_repository;
271 /* The C++ dialect being used. C++98 is the default. */
273 enum cxx_dialect cxx_dialect = cxx98;
275 /* Maximum template instantiation depth. This limit exists to limit the
276 time it takes to notice excessively recursive template instantiations.
278 The default is lower than the 1024 recommended by the C++0x standard
279 because G++ runs out of stack before 1024 with highly recursive template
280 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
282 int max_tinst_depth = 900;
284 /* The elements of `ridpointers' are identifier nodes for the reserved
285 type names and storage classes. It is indexed by a RID_... value. */
286 tree *ridpointers;
288 tree (*make_fname_decl) (location_t, tree, int);
290 /* Nonzero means don't warn about problems that occur when the code is
291 executed. */
292 int c_inhibit_evaluation_warnings;
294 /* Whether we are building a boolean conversion inside
295 convert_for_assignment, or some other late binary operation. If
296 build_binary_op is called for C (from code shared by C and C++) in
297 this case, then the operands have already been folded and the
298 result will not be folded again, so C_MAYBE_CONST_EXPR should not
299 be generated. */
300 bool in_late_binary_op;
302 /* Whether lexing has been completed, so subsequent preprocessor
303 errors should use the compiler's input_location. */
304 bool done_lexing = false;
306 /* Information about how a function name is generated. */
307 struct fname_var_t
309 tree *const decl; /* pointer to the VAR_DECL. */
310 const unsigned rid; /* RID number for the identifier. */
311 const int pretty; /* How pretty is it? */
314 /* The three ways of getting then name of the current function. */
316 const struct fname_var_t fname_vars[] =
318 /* C99 compliant __func__, must be first. */
319 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
320 /* GCC __FUNCTION__ compliant. */
321 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
322 /* GCC __PRETTY_FUNCTION__ compliant. */
323 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
324 {NULL, 0, 0},
327 /* Global visibility options. */
328 struct visibility_flags visibility_options;
330 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
331 static tree check_case_value (tree);
332 static bool check_case_bounds (tree, tree, tree *, tree *);
334 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_always_inline_attribute (tree *, tree, tree, int,
344 bool *);
345 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_transparent_union_attribute (tree *, tree, tree,
355 int, bool *);
356 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
362 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
363 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
366 static tree handle_visibility_attribute (tree *, tree, tree, int,
367 bool *);
368 static tree handle_tls_model_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_no_instrument_function_attribute (tree *, tree,
371 tree, int, bool *);
372 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_deprecated_attribute (tree *, tree, tree, int,
381 bool *);
382 static tree handle_vector_size_attribute (tree *, tree, tree, int,
383 bool *);
384 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
388 bool *);
389 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
394 static tree ignore_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
398 static void check_function_nonnull (tree, int, tree *);
399 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
402 static int resort_field_decl_cmp (const void *, const void *);
404 /* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 UPC is like C except that D_UPC is not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | D_UPC
413 C++ --std=c0x: D_CONLY | D_OBJC | D_UPC
414 ObjC++ is like C++ except that D_OBJC is not set
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
426 const struct c_common_resword c_common_reswords[] =
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
430 { "_Bool", RID_BOOL, D_CONLY },
431 { "_Complex", RID_COMPLEX, 0 },
432 { "_Imaginary", RID_IMAGINARY, D_CONLY },
433 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
434 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
435 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
436 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
437 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
438 { "_Sat", RID_SAT, D_CONLY | D_EXT },
439 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
440 { "_Noreturn", RID_NORETURN, D_CONLY },
441 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
442 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
443 { "__alignof", RID_ALIGNOF, 0 },
444 { "__alignof__", RID_ALIGNOF, 0 },
445 { "__asm", RID_ASM, 0 },
446 { "__asm__", RID_ASM, 0 },
447 { "__attribute", RID_ATTRIBUTE, 0 },
448 { "__attribute__", RID_ATTRIBUTE, 0 },
449 { "__bases", RID_BASES, D_CXXONLY },
450 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
451 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
452 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
453 { "__builtin_offsetof", RID_OFFSETOF, 0 },
454 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
455 { "__builtin_va_arg", RID_VA_ARG, 0 },
456 { "__complex", RID_COMPLEX, 0 },
457 { "__complex__", RID_COMPLEX, 0 },
458 { "__const", RID_CONST, 0 },
459 { "__const__", RID_CONST, 0 },
460 { "__decltype", RID_DECLTYPE, D_CXXONLY },
461 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
462 { "__extension__", RID_EXTENSION, 0 },
463 { "__func__", RID_C99_FUNCTION_NAME, 0 },
464 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
465 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
466 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
467 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
468 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
469 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
470 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
471 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
472 { "__imag", RID_IMAGPART, 0 },
473 { "__imag__", RID_IMAGPART, 0 },
474 { "__inline", RID_INLINE, 0 },
475 { "__inline__", RID_INLINE, 0 },
476 { "__int128", RID_INT128, 0 },
477 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
478 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
479 { "__is_class", RID_IS_CLASS, D_CXXONLY },
480 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
481 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
482 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
483 { "__is_final", RID_IS_FINAL, D_CXXONLY },
484 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
485 { "__is_pod", RID_IS_POD, D_CXXONLY },
486 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
487 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
488 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
489 { "__is_union", RID_IS_UNION, D_CXXONLY },
490 { "__label__", RID_LABEL, 0 },
491 { "__null", RID_NULL, 0 },
492 { "__real", RID_REALPART, 0 },
493 { "__real__", RID_REALPART, 0 },
494 { "__restrict", RID_RESTRICT, 0 },
495 { "__restrict__", RID_RESTRICT, 0 },
496 { "__signed", RID_SIGNED, 0 },
497 { "__signed__", RID_SIGNED, 0 },
498 { "__thread", RID_THREAD, 0 },
499 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
500 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
501 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
502 { "__typeof", RID_TYPEOF, 0 },
503 { "__typeof__", RID_TYPEOF, 0 },
504 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
505 { "__volatile", RID_VOLATILE, 0 },
506 { "__volatile__", RID_VOLATILE, 0 },
507 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "asm", RID_ASM, D_ASM },
509 { "auto", RID_AUTO, 0 },
510 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
511 { "break", RID_BREAK, 0 },
512 { "case", RID_CASE, 0 },
513 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
514 { "char", RID_CHAR, 0 },
515 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
516 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
517 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
518 { "const", RID_CONST, 0 },
519 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
520 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
521 { "continue", RID_CONTINUE, 0 },
522 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
523 { "default", RID_DEFAULT, 0 },
524 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
525 { "do", RID_DO, 0 },
526 { "double", RID_DOUBLE, 0 },
527 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
528 { "else", RID_ELSE, 0 },
529 { "enum", RID_ENUM, 0 },
530 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
531 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
532 { "extern", RID_EXTERN, 0 },
533 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
534 { "float", RID_FLOAT, 0 },
535 { "for", RID_FOR, 0 },
536 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
537 { "goto", RID_GOTO, 0 },
538 { "if", RID_IF, 0 },
539 { "inline", RID_INLINE, D_EXT89 },
540 { "int", RID_INT, 0 },
541 { "long", RID_LONG, 0 },
542 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
543 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
544 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
545 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
546 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
547 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
548 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
549 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
550 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
551 { "register", RID_REGISTER, 0 },
552 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
553 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
554 { "return", RID_RETURN, 0 },
555 { "short", RID_SHORT, 0 },
556 { "signed", RID_SIGNED, 0 },
557 { "sizeof", RID_SIZEOF, 0 },
558 { "static", RID_STATIC, 0 },
559 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
560 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
561 { "struct", RID_STRUCT, 0 },
562 { "switch", RID_SWITCH, 0 },
563 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
564 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
565 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
566 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
567 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
568 { "typedef", RID_TYPEDEF, 0 },
569 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
570 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
571 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
572 { "union", RID_UNION, 0 },
573 { "unsigned", RID_UNSIGNED, 0 },
574 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
575 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
576 { "void", RID_VOID, 0 },
577 { "volatile", RID_VOLATILE, 0 },
578 { "wchar_t", RID_WCHAR, D_CXXONLY },
579 { "while", RID_WHILE, 0 },
580 /* These Objective-C keywords are recognized only immediately after
581 an '@'. */
582 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
583 { "defs", RID_AT_DEFS, D_OBJC },
584 { "encode", RID_AT_ENCODE, D_OBJC },
585 { "end", RID_AT_END, D_OBJC },
586 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
587 { "interface", RID_AT_INTERFACE, D_OBJC },
588 { "protocol", RID_AT_PROTOCOL, D_OBJC },
589 { "selector", RID_AT_SELECTOR, D_OBJC },
590 { "finally", RID_AT_FINALLY, D_OBJC },
591 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
592 { "optional", RID_AT_OPTIONAL, D_OBJC },
593 { "required", RID_AT_REQUIRED, D_OBJC },
594 { "property", RID_AT_PROPERTY, D_OBJC },
595 { "package", RID_AT_PACKAGE, D_OBJC },
596 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
597 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
598 /* These are recognized only in protocol-qualifier context
599 (see above) */
600 { "bycopy", RID_BYCOPY, D_OBJC },
601 { "byref", RID_BYREF, D_OBJC },
602 { "in", RID_IN, D_OBJC },
603 { "inout", RID_INOUT, D_OBJC },
604 { "oneway", RID_ONEWAY, D_OBJC },
605 { "out", RID_OUT, D_OBJC },
607 /* UPC keywords */
608 { "shared", RID_SHARED, D_UPC },
609 { "relaxed", RID_RELAXED, D_UPC },
610 { "strict", RID_STRICT, D_UPC },
611 { "upc_barrier", RID_UPC_BARRIER, D_UPC },
612 { "upc_blocksizeof", RID_UPC_BLOCKSIZEOF, D_UPC },
613 { "upc_elemsizeof", RID_UPC_ELEMSIZEOF, D_UPC },
614 { "upc_forall", RID_UPC_FORALL, D_UPC },
615 { "upc_localsizeof", RID_UPC_LOCALSIZEOF, D_UPC },
616 { "upc_notify", RID_UPC_NOTIFY, D_UPC },
617 { "upc_wait", RID_UPC_WAIT, D_UPC },
619 /* These are recognized inside a property attribute list */
620 { "assign", RID_ASSIGN, D_OBJC },
621 { "copy", RID_COPY, D_OBJC },
622 { "getter", RID_GETTER, D_OBJC },
623 { "nonatomic", RID_NONATOMIC, D_OBJC },
624 { "readonly", RID_READONLY, D_OBJC },
625 { "readwrite", RID_READWRITE, D_OBJC },
626 { "retain", RID_RETAIN, D_OBJC },
627 { "setter", RID_SETTER, D_OBJC },
630 const unsigned int num_c_common_reswords =
631 sizeof c_common_reswords / sizeof (struct c_common_resword);
633 /* Table of machine-independent attributes common to all C-like languages. */
634 const struct attribute_spec c_common_attribute_table[] =
636 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
637 affects_type_identity } */
638 { "packed", 0, 0, false, false, false,
639 handle_packed_attribute , false},
640 { "nocommon", 0, 0, true, false, false,
641 handle_nocommon_attribute, false},
642 { "common", 0, 0, true, false, false,
643 handle_common_attribute, false },
644 /* FIXME: logically, noreturn attributes should be listed as
645 "false, true, true" and apply to function types. But implementing this
646 would require all the places in the compiler that use TREE_THIS_VOLATILE
647 on a decl to identify non-returning functions to be located and fixed
648 to check the function type instead. */
649 { "noreturn", 0, 0, true, false, false,
650 handle_noreturn_attribute, false },
651 { "volatile", 0, 0, true, false, false,
652 handle_noreturn_attribute, false },
653 { "noinline", 0, 0, true, false, false,
654 handle_noinline_attribute, false },
655 { "noclone", 0, 0, true, false, false,
656 handle_noclone_attribute, false },
657 { "leaf", 0, 0, true, false, false,
658 handle_leaf_attribute, false },
659 { "always_inline", 0, 0, true, false, false,
660 handle_always_inline_attribute, false },
661 { "gnu_inline", 0, 0, true, false, false,
662 handle_gnu_inline_attribute, false },
663 { "artificial", 0, 0, true, false, false,
664 handle_artificial_attribute, false },
665 { "flatten", 0, 0, true, false, false,
666 handle_flatten_attribute, false },
667 { "used", 0, 0, true, false, false,
668 handle_used_attribute, false },
669 { "unused", 0, 0, false, false, false,
670 handle_unused_attribute, false },
671 { "externally_visible", 0, 0, true, false, false,
672 handle_externally_visible_attribute, false },
673 /* The same comments as for noreturn attributes apply to const ones. */
674 { "const", 0, 0, true, false, false,
675 handle_const_attribute, false },
676 { "transparent_union", 0, 0, false, false, false,
677 handle_transparent_union_attribute, false },
678 { "constructor", 0, 1, true, false, false,
679 handle_constructor_attribute, false },
680 { "destructor", 0, 1, true, false, false,
681 handle_destructor_attribute, false },
682 { "mode", 1, 1, false, true, false,
683 handle_mode_attribute, false },
684 { "section", 1, 1, true, false, false,
685 handle_section_attribute, false },
686 { "aligned", 0, 1, false, false, false,
687 handle_aligned_attribute, false },
688 { "weak", 0, 0, true, false, false,
689 handle_weak_attribute, false },
690 { "ifunc", 1, 1, true, false, false,
691 handle_ifunc_attribute, false },
692 { "alias", 1, 1, true, false, false,
693 handle_alias_attribute, false },
694 { "weakref", 0, 1, true, false, false,
695 handle_weakref_attribute, false },
696 { "no_instrument_function", 0, 0, true, false, false,
697 handle_no_instrument_function_attribute,
698 false },
699 { "malloc", 0, 0, true, false, false,
700 handle_malloc_attribute, false },
701 { "returns_twice", 0, 0, true, false, false,
702 handle_returns_twice_attribute, false },
703 { "no_stack_limit", 0, 0, true, false, false,
704 handle_no_limit_stack_attribute, false },
705 { "pure", 0, 0, true, false, false,
706 handle_pure_attribute, false },
707 { "transaction_callable", 0, 0, false, true, false,
708 handle_tm_attribute, false },
709 { "transaction_unsafe", 0, 0, false, true, false,
710 handle_tm_attribute, false },
711 { "transaction_safe", 0, 0, false, true, false,
712 handle_tm_attribute, false },
713 { "transaction_may_cancel_outer", 0, 0, false, true, false,
714 handle_tm_attribute, false },
715 /* ??? These two attributes didn't make the transition from the
716 Intel language document to the multi-vendor language document. */
717 { "transaction_pure", 0, 0, false, true, false,
718 handle_tm_attribute, false },
719 { "transaction_wrap", 1, 1, true, false, false,
720 handle_tm_wrap_attribute, false },
721 /* For internal use (marking of builtins) only. The name contains space
722 to prevent its usage in source code. */
723 { "no vops", 0, 0, true, false, false,
724 handle_novops_attribute, false },
725 { "deprecated", 0, 1, false, false, false,
726 handle_deprecated_attribute, false },
727 { "vector_size", 1, 1, false, true, false,
728 handle_vector_size_attribute, false },
729 { "visibility", 1, 1, false, false, false,
730 handle_visibility_attribute, false },
731 { "tls_model", 1, 1, true, false, false,
732 handle_tls_model_attribute, false },
733 { "nonnull", 0, -1, false, true, true,
734 handle_nonnull_attribute, false },
735 { "nothrow", 0, 0, true, false, false,
736 handle_nothrow_attribute, false },
737 { "may_alias", 0, 0, false, true, false, NULL, false },
738 { "cleanup", 1, 1, true, false, false,
739 handle_cleanup_attribute, false },
740 { "warn_unused_result", 0, 0, false, true, true,
741 handle_warn_unused_result_attribute, false },
742 { "sentinel", 0, 1, false, true, true,
743 handle_sentinel_attribute, false },
744 /* For internal use (marking of builtins) only. The name contains space
745 to prevent its usage in source code. */
746 { "type generic", 0, 0, false, true, true,
747 handle_type_generic_attribute, false },
748 { "alloc_size", 1, 2, false, true, true,
749 handle_alloc_size_attribute, false },
750 { "cold", 0, 0, true, false, false,
751 handle_cold_attribute, false },
752 { "hot", 0, 0, true, false, false,
753 handle_hot_attribute, false },
754 { "warning", 1, 1, true, false, false,
755 handle_error_attribute, false },
756 { "error", 1, 1, true, false, false,
757 handle_error_attribute, false },
758 { "target", 1, -1, true, false, false,
759 handle_target_attribute, false },
760 { "optimize", 1, -1, true, false, false,
761 handle_optimize_attribute, false },
762 /* For internal use only. The leading '*' both prevents its usage in
763 source code and signals that it may be overridden by machine tables. */
764 { "*tm regparm", 0, 0, false, true, true,
765 ignore_attribute, false },
766 { "no_split_stack", 0, 0, true, false, false,
767 handle_no_split_stack_attribute, false },
768 /* For internal use (marking of builtins and runtime functions) only.
769 The name contains space to prevent its usage in source code. */
770 { "fn spec", 1, 1, false, true, true,
771 handle_fnspec_attribute, false },
772 { NULL, 0, 0, false, false, false, NULL, false }
775 /* Give the specifications for the format attributes, used by C and all
776 descendants. */
778 const struct attribute_spec c_common_format_attribute_table[] =
780 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
781 affects_type_identity } */
782 { "format", 3, 3, false, true, true,
783 handle_format_attribute, false },
784 { "format_arg", 1, 1, false, true, true,
785 handle_format_arg_attribute, false },
786 { NULL, 0, 0, false, false, false, NULL, false }
789 /* Return identifier for address space AS. */
791 const char *
792 c_addr_space_name (addr_space_t as)
794 int rid = RID_FIRST_ADDR_SPACE + as;
795 gcc_assert (ridpointers [rid]);
796 return IDENTIFIER_POINTER (ridpointers [rid]);
799 /* Push current bindings for the function name VAR_DECLS. */
801 void
802 start_fname_decls (void)
804 unsigned ix;
805 tree saved = NULL_TREE;
807 for (ix = 0; fname_vars[ix].decl; ix++)
809 tree decl = *fname_vars[ix].decl;
811 if (decl)
813 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
814 saved);
815 *fname_vars[ix].decl = NULL_TREE;
818 if (saved || saved_function_name_decls)
819 /* Normally they'll have been NULL, so only push if we've got a
820 stack, or they are non-NULL. */
821 saved_function_name_decls = tree_cons (saved, NULL_TREE,
822 saved_function_name_decls);
825 /* Finish up the current bindings, adding them into the current function's
826 statement tree. This must be done _before_ finish_stmt_tree is called.
827 If there is no current function, we must be at file scope and no statements
828 are involved. Pop the previous bindings. */
830 void
831 finish_fname_decls (void)
833 unsigned ix;
834 tree stmts = NULL_TREE;
835 tree stack = saved_function_name_decls;
837 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
838 append_to_statement_list (TREE_VALUE (stack), &stmts);
840 if (stmts)
842 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
844 if (TREE_CODE (*bodyp) == BIND_EXPR)
845 bodyp = &BIND_EXPR_BODY (*bodyp);
847 append_to_statement_list_force (*bodyp, &stmts);
848 *bodyp = stmts;
851 for (ix = 0; fname_vars[ix].decl; ix++)
852 *fname_vars[ix].decl = NULL_TREE;
854 if (stack)
856 /* We had saved values, restore them. */
857 tree saved;
859 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
861 tree decl = TREE_PURPOSE (saved);
862 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
864 *fname_vars[ix].decl = decl;
866 stack = TREE_CHAIN (stack);
868 saved_function_name_decls = stack;
871 /* Return the text name of the current function, suitably prettified
872 by PRETTY_P. Return string must be freed by caller. */
874 const char *
875 fname_as_string (int pretty_p)
877 const char *name = "top level";
878 char *namep;
879 int vrb = 2, len;
880 cpp_string cstr = { 0, 0 }, strname;
882 if (!pretty_p)
884 name = "";
885 vrb = 0;
888 if (current_function_decl)
889 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
891 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
893 namep = XNEWVEC (char, len);
894 snprintf (namep, len, "\"%s\"", name);
895 strname.text = (unsigned char *) namep;
896 strname.len = len - 1;
898 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
900 XDELETEVEC (namep);
901 return (const char *) cstr.text;
904 return namep;
907 /* Return the VAR_DECL for a const char array naming the current
908 function. If the VAR_DECL has not yet been created, create it
909 now. RID indicates how it should be formatted and IDENTIFIER_NODE
910 ID is its name (unfortunately C and C++ hold the RID values of
911 keywords in different places, so we can't derive RID from ID in
912 this language independent code. LOC is the location of the
913 function. */
915 tree
916 fname_decl (location_t loc, unsigned int rid, tree id)
918 unsigned ix;
919 tree decl = NULL_TREE;
921 for (ix = 0; fname_vars[ix].decl; ix++)
922 if (fname_vars[ix].rid == rid)
923 break;
925 decl = *fname_vars[ix].decl;
926 if (!decl)
928 /* If a tree is built here, it would normally have the lineno of
929 the current statement. Later this tree will be moved to the
930 beginning of the function and this line number will be wrong.
931 To avoid this problem set the lineno to 0 here; that prevents
932 it from appearing in the RTL. */
933 tree stmts;
934 location_t saved_location = input_location;
935 input_location = UNKNOWN_LOCATION;
937 stmts = push_stmt_list ();
938 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
939 stmts = pop_stmt_list (stmts);
940 if (!IS_EMPTY_STMT (stmts))
941 saved_function_name_decls
942 = tree_cons (decl, stmts, saved_function_name_decls);
943 *fname_vars[ix].decl = decl;
944 input_location = saved_location;
946 if (!ix && !current_function_decl)
947 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
949 return decl;
952 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
954 tree
955 fix_string_type (tree value)
957 int length = TREE_STRING_LENGTH (value);
958 int nchars;
959 tree e_type, i_type, a_type;
961 /* Compute the number of elements, for the array type. */
962 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
964 nchars = length;
965 e_type = char_type_node;
967 else if (TREE_TYPE (value) == char16_array_type_node)
969 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
970 e_type = char16_type_node;
972 else if (TREE_TYPE (value) == char32_array_type_node)
974 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
975 e_type = char32_type_node;
977 else
979 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
980 e_type = wchar_type_node;
983 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
984 limit in C++98 Annex B is very large (65536) and is not normative,
985 so we do not diagnose it (warn_overlength_strings is forced off
986 in c_common_post_options). */
987 if (warn_overlength_strings)
989 const int nchars_max = flag_isoc99 ? 4095 : 509;
990 const int relevant_std = flag_isoc99 ? 99 : 90;
991 if (nchars - 1 > nchars_max)
992 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
993 separate the %d from the 'C'. 'ISO' should not be
994 translated, but it may be moved after 'C%d' in languages
995 where modifiers follow nouns. */
996 pedwarn (input_location, OPT_Woverlength_strings,
997 "string length %qd is greater than the length %qd "
998 "ISO C%d compilers are required to support",
999 nchars - 1, nchars_max, relevant_std);
1002 /* Create the array type for the string constant. The ISO C++
1003 standard says that a string literal has type `const char[N]' or
1004 `const wchar_t[N]'. We use the same logic when invoked as a C
1005 front-end with -Wwrite-strings.
1006 ??? We should change the type of an expression depending on the
1007 state of a warning flag. We should just be warning -- see how
1008 this is handled in the C++ front-end for the deprecated implicit
1009 conversion from string literals to `char*' or `wchar_t*'.
1011 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1012 array type being the unqualified version of that type.
1013 Therefore, if we are constructing an array of const char, we must
1014 construct the matching unqualified array type first. The C front
1015 end does not require this, but it does no harm, so we do it
1016 unconditionally. */
1017 i_type = build_index_type (size_int (nchars - 1));
1018 a_type = build_array_type (e_type, i_type);
1019 if (c_dialect_cxx() || warn_write_strings)
1020 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1022 TREE_TYPE (value) = a_type;
1023 TREE_CONSTANT (value) = 1;
1024 TREE_READONLY (value) = 1;
1025 TREE_STATIC (value) = 1;
1026 return value;
1029 /* If DISABLE is true, stop issuing warnings. This is used when
1030 parsing code that we know will not be executed. This function may
1031 be called multiple times, and works as a stack. */
1033 static void
1034 c_disable_warnings (bool disable)
1036 if (disable)
1038 ++c_inhibit_evaluation_warnings;
1039 fold_defer_overflow_warnings ();
1043 /* If ENABLE is true, reenable issuing warnings. */
1045 static void
1046 c_enable_warnings (bool enable)
1048 if (enable)
1050 --c_inhibit_evaluation_warnings;
1051 fold_undefer_and_ignore_overflow_warnings ();
1055 /* Fully fold EXPR, an expression that was not folded (beyond integer
1056 constant expressions and null pointer constants) when being built
1057 up. If IN_INIT, this is in a static initializer and certain
1058 changes are made to the folding done. Clear *MAYBE_CONST if
1059 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1060 expression because it contains an evaluated operator (in C99) or an
1061 operator outside of sizeof returning an integer constant (in C90)
1062 not permitted in constant expressions, or because it contains an
1063 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1064 set to true by callers before calling this function.) Return the
1065 folded expression. Function arguments have already been folded
1066 before calling this function, as have the contents of SAVE_EXPR,
1067 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1068 C_MAYBE_CONST_EXPR. */
1070 tree
1071 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1073 tree ret;
1074 tree eptype = NULL_TREE;
1075 bool dummy = true;
1076 bool maybe_const_itself = true;
1077 location_t loc = EXPR_LOCATION (expr);
1079 /* This function is not relevant to C++ because C++ folds while
1080 parsing, and may need changes to be correct for C++ when C++
1081 stops folding while parsing. */
1082 if (c_dialect_cxx ())
1083 gcc_unreachable ();
1085 if (!maybe_const)
1086 maybe_const = &dummy;
1087 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1089 eptype = TREE_TYPE (expr);
1090 expr = TREE_OPERAND (expr, 0);
1092 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1093 &maybe_const_itself);
1094 if (eptype)
1095 ret = fold_convert_loc (loc, eptype, ret);
1096 *maybe_const &= maybe_const_itself;
1097 return ret;
1100 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1101 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1102 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1103 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1104 both evaluated and unevaluated subexpressions while
1105 *MAYBE_CONST_ITSELF is carried from only evaluated
1106 subexpressions). */
1108 static tree
1109 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1110 bool *maybe_const_itself)
1112 tree ret = expr;
1113 enum tree_code code = TREE_CODE (expr);
1114 enum tree_code_class kind = TREE_CODE_CLASS (code);
1115 location_t loc = EXPR_LOCATION (expr);
1116 tree op0, op1, op2, op3;
1117 tree orig_op0, orig_op1, orig_op2;
1118 bool op0_const = true, op1_const = true, op2_const = true;
1119 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1120 bool nowarning = TREE_NO_WARNING (expr);
1121 bool unused_p;
1123 /* This function is not relevant to C++ because C++ folds while
1124 parsing, and may need changes to be correct for C++ when C++
1125 stops folding while parsing. */
1126 if (c_dialect_cxx ())
1127 gcc_unreachable ();
1129 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1130 anything else not counted as an expression cannot usefully be
1131 folded further at this point. */
1132 if (!IS_EXPR_CODE_CLASS (kind)
1133 || kind == tcc_statement
1134 || code == SAVE_EXPR)
1135 return expr;
1137 /* Operands of variable-length expressions (function calls) have
1138 already been folded, as have __builtin_* function calls, and such
1139 expressions cannot occur in constant expressions. */
1140 if (kind == tcc_vl_exp)
1142 *maybe_const_operands = false;
1143 ret = fold (expr);
1144 goto out;
1147 if (code == C_MAYBE_CONST_EXPR)
1149 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1150 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1151 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1152 *maybe_const_operands = false;
1153 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1154 *maybe_const_itself = false;
1155 if (pre && !in_init)
1156 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1157 else
1158 ret = inner;
1159 goto out;
1162 /* Assignment, increment, decrement, function call and comma
1163 operators, and statement expressions, cannot occur in constant
1164 expressions if evaluated / outside of sizeof. (Function calls
1165 were handled above, though VA_ARG_EXPR is treated like a function
1166 call here, and statement expressions are handled through
1167 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1168 switch (code)
1170 case MODIFY_EXPR:
1171 case PREDECREMENT_EXPR:
1172 case PREINCREMENT_EXPR:
1173 case POSTDECREMENT_EXPR:
1174 case POSTINCREMENT_EXPR:
1175 case COMPOUND_EXPR:
1176 *maybe_const_operands = false;
1177 break;
1179 case VA_ARG_EXPR:
1180 case TARGET_EXPR:
1181 case BIND_EXPR:
1182 case OBJ_TYPE_REF:
1183 *maybe_const_operands = false;
1184 ret = fold (expr);
1185 goto out;
1187 default:
1188 break;
1191 /* Fold individual tree codes as appropriate. */
1192 switch (code)
1194 case COMPOUND_LITERAL_EXPR:
1195 /* Any non-constancy will have been marked in a containing
1196 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1197 goto out;
1199 case COMPONENT_REF:
1200 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1201 op1 = TREE_OPERAND (expr, 1);
1202 op2 = TREE_OPERAND (expr, 2);
1203 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1204 maybe_const_itself);
1205 STRIP_TYPE_NOPS (op0);
1206 if (op0 != orig_op0)
1207 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1208 if (ret != expr)
1210 TREE_READONLY (ret) = TREE_READONLY (expr);
1211 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1213 goto out;
1215 case ARRAY_REF:
1216 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1217 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1218 op2 = TREE_OPERAND (expr, 2);
1219 op3 = TREE_OPERAND (expr, 3);
1220 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1221 maybe_const_itself);
1222 STRIP_TYPE_NOPS (op0);
1223 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1224 maybe_const_itself);
1225 STRIP_TYPE_NOPS (op1);
1226 op1 = decl_constant_value_for_optimization (op1);
1227 if (op0 != orig_op0 || op1 != orig_op1)
1228 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1229 if (ret != expr)
1231 TREE_READONLY (ret) = TREE_READONLY (expr);
1232 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1233 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1235 ret = fold (ret);
1236 goto out;
1238 case COMPOUND_EXPR:
1239 case MODIFY_EXPR:
1240 case PREDECREMENT_EXPR:
1241 case PREINCREMENT_EXPR:
1242 case POSTDECREMENT_EXPR:
1243 case POSTINCREMENT_EXPR:
1244 case PLUS_EXPR:
1245 case MINUS_EXPR:
1246 case MULT_EXPR:
1247 case POINTER_PLUS_EXPR:
1248 case TRUNC_DIV_EXPR:
1249 case CEIL_DIV_EXPR:
1250 case FLOOR_DIV_EXPR:
1251 case TRUNC_MOD_EXPR:
1252 case RDIV_EXPR:
1253 case EXACT_DIV_EXPR:
1254 case LSHIFT_EXPR:
1255 case RSHIFT_EXPR:
1256 case BIT_IOR_EXPR:
1257 case BIT_XOR_EXPR:
1258 case BIT_AND_EXPR:
1259 case LT_EXPR:
1260 case LE_EXPR:
1261 case GT_EXPR:
1262 case GE_EXPR:
1263 case EQ_EXPR:
1264 case NE_EXPR:
1265 case COMPLEX_EXPR:
1266 case TRUTH_AND_EXPR:
1267 case TRUTH_OR_EXPR:
1268 case TRUTH_XOR_EXPR:
1269 case UNORDERED_EXPR:
1270 case ORDERED_EXPR:
1271 case UNLT_EXPR:
1272 case UNLE_EXPR:
1273 case UNGT_EXPR:
1274 case UNGE_EXPR:
1275 case UNEQ_EXPR:
1276 /* Binary operations evaluating both arguments (increment and
1277 decrement are binary internally in GCC). */
1278 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1279 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1280 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1281 maybe_const_itself);
1282 STRIP_TYPE_NOPS (op0);
1283 if (code != MODIFY_EXPR
1284 && code != PREDECREMENT_EXPR
1285 && code != PREINCREMENT_EXPR
1286 && code != POSTDECREMENT_EXPR
1287 && code != POSTINCREMENT_EXPR)
1288 op0 = decl_constant_value_for_optimization (op0);
1289 /* The RHS of a MODIFY_EXPR was fully folded when building that
1290 expression for the sake of conversion warnings. */
1291 if (code != MODIFY_EXPR)
1292 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1293 maybe_const_itself);
1294 STRIP_TYPE_NOPS (op1);
1295 op1 = decl_constant_value_for_optimization (op1);
1296 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1297 ret = in_init
1298 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1299 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1300 else
1301 ret = fold (expr);
1302 if (TREE_OVERFLOW_P (ret)
1303 && !TREE_OVERFLOW_P (op0)
1304 && !TREE_OVERFLOW_P (op1))
1305 overflow_warning (EXPR_LOCATION (expr), ret);
1306 goto out;
1308 case INDIRECT_REF:
1309 case FIX_TRUNC_EXPR:
1310 case FLOAT_EXPR:
1311 CASE_CONVERT:
1312 case VIEW_CONVERT_EXPR:
1313 case NON_LVALUE_EXPR:
1314 case NEGATE_EXPR:
1315 case BIT_NOT_EXPR:
1316 case TRUTH_NOT_EXPR:
1317 case ADDR_EXPR:
1318 case CONJ_EXPR:
1319 case REALPART_EXPR:
1320 case IMAGPART_EXPR:
1321 /* Unary operations. */
1322 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1323 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1324 maybe_const_itself);
1325 STRIP_TYPE_NOPS (op0);
1326 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1327 op0 = decl_constant_value_for_optimization (op0);
1328 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1329 not prepared to deal with them if they occur in initializers.
1330 Avoid attempts to fold references to UPC shared components
1331 due to the complexities of UPC pointer-to-shared arithmetic. */
1332 if (op0 != orig_op0
1333 && code == ADDR_EXPR
1334 && (op1 = get_base_address (op0)) != NULL_TREE
1335 && TREE_CODE (op1) == INDIRECT_REF
1336 && !upc_shared_type_p (TREE_TYPE (op1))
1337 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1338 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1339 else if (op0 != orig_op0 || in_init)
1340 ret = in_init
1341 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1342 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1343 else
1344 ret = fold (expr);
1345 if (code == INDIRECT_REF
1346 && ret != expr
1347 && TREE_CODE (ret) == INDIRECT_REF)
1349 TREE_READONLY (ret) = TREE_READONLY (expr);
1350 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1351 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1353 switch (code)
1355 case FIX_TRUNC_EXPR:
1356 case FLOAT_EXPR:
1357 CASE_CONVERT:
1358 /* Don't warn about explicit conversions. We will already
1359 have warned about suspect implicit conversions. */
1360 break;
1362 default:
1363 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1364 overflow_warning (EXPR_LOCATION (expr), ret);
1365 break;
1367 goto out;
1369 case TRUTH_ANDIF_EXPR:
1370 case TRUTH_ORIF_EXPR:
1371 /* Binary operations not necessarily evaluating both
1372 arguments. */
1373 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1374 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1375 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1376 STRIP_TYPE_NOPS (op0);
1378 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1379 ? truthvalue_false_node
1380 : truthvalue_true_node));
1381 c_disable_warnings (unused_p);
1382 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1383 STRIP_TYPE_NOPS (op1);
1384 c_enable_warnings (unused_p);
1386 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1387 ret = in_init
1388 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1389 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1390 else
1391 ret = fold (expr);
1392 *maybe_const_operands &= op0_const;
1393 *maybe_const_itself &= op0_const_self;
1394 if (!(flag_isoc99
1395 && op0_const
1396 && op0_const_self
1397 && (code == TRUTH_ANDIF_EXPR
1398 ? op0 == truthvalue_false_node
1399 : op0 == truthvalue_true_node)))
1400 *maybe_const_operands &= op1_const;
1401 if (!(op0_const
1402 && op0_const_self
1403 && (code == TRUTH_ANDIF_EXPR
1404 ? op0 == truthvalue_false_node
1405 : op0 == truthvalue_true_node)))
1406 *maybe_const_itself &= op1_const_self;
1407 goto out;
1409 case COND_EXPR:
1410 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1411 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1412 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1413 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1415 STRIP_TYPE_NOPS (op0);
1416 c_disable_warnings (op0 == truthvalue_false_node);
1417 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1418 STRIP_TYPE_NOPS (op1);
1419 c_enable_warnings (op0 == truthvalue_false_node);
1421 c_disable_warnings (op0 == truthvalue_true_node);
1422 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1423 STRIP_TYPE_NOPS (op2);
1424 c_enable_warnings (op0 == truthvalue_true_node);
1426 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1427 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1428 else
1429 ret = fold (expr);
1430 *maybe_const_operands &= op0_const;
1431 *maybe_const_itself &= op0_const_self;
1432 if (!(flag_isoc99
1433 && op0_const
1434 && op0_const_self
1435 && op0 == truthvalue_false_node))
1436 *maybe_const_operands &= op1_const;
1437 if (!(op0_const
1438 && op0_const_self
1439 && op0 == truthvalue_false_node))
1440 *maybe_const_itself &= op1_const_self;
1441 if (!(flag_isoc99
1442 && op0_const
1443 && op0_const_self
1444 && op0 == truthvalue_true_node))
1445 *maybe_const_operands &= op2_const;
1446 if (!(op0_const
1447 && op0_const_self
1448 && op0 == truthvalue_true_node))
1449 *maybe_const_itself &= op2_const_self;
1450 goto out;
1452 case EXCESS_PRECISION_EXPR:
1453 /* Each case where an operand with excess precision may be
1454 encountered must remove the EXCESS_PRECISION_EXPR around
1455 inner operands and possibly put one around the whole
1456 expression or possibly convert to the semantic type (which
1457 c_fully_fold does); we cannot tell at this stage which is
1458 appropriate in any particular case. */
1459 gcc_unreachable ();
1461 default:
1462 /* Various codes may appear through folding built-in functions
1463 and their arguments. */
1464 goto out;
1467 out:
1468 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1469 have been done by this point, so remove them again. */
1470 nowarning |= TREE_NO_WARNING (ret);
1471 STRIP_TYPE_NOPS (ret);
1472 if (nowarning && !TREE_NO_WARNING (ret))
1474 if (!CAN_HAVE_LOCATION_P (ret))
1475 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1476 TREE_NO_WARNING (ret) = 1;
1478 if (ret != expr)
1479 protected_set_expr_location (ret, loc);
1480 return ret;
1483 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1484 return EXP. Otherwise, return either EXP or its known constant
1485 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1486 Is the BLKmode test appropriate? */
1488 tree
1489 decl_constant_value_for_optimization (tree exp)
1491 tree ret;
1493 /* This function is only used by C, for c_fully_fold and other
1494 optimization, and may not be correct for C++. */
1495 if (c_dialect_cxx ())
1496 gcc_unreachable ();
1498 if (!optimize
1499 || TREE_CODE (exp) != VAR_DECL
1500 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1501 || DECL_MODE (exp) == BLKmode)
1502 return exp;
1504 ret = decl_constant_value (exp);
1505 /* Avoid unwanted tree sharing between the initializer and current
1506 function's body where the tree can be modified e.g. by the
1507 gimplifier. */
1508 if (ret != exp && TREE_STATIC (exp))
1509 ret = unshare_expr (ret);
1510 return ret;
1513 /* Print a warning if a constant expression had overflow in folding.
1514 Invoke this function on every expression that the language
1515 requires to be a constant expression.
1516 Note the ANSI C standard says it is erroneous for a
1517 constant expression to overflow. */
1519 void
1520 constant_expression_warning (tree value)
1522 if (warn_overflow && pedantic
1523 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1524 || TREE_CODE (value) == FIXED_CST
1525 || TREE_CODE (value) == VECTOR_CST
1526 || TREE_CODE (value) == COMPLEX_CST)
1527 && TREE_OVERFLOW (value))
1528 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1531 /* The same as above but print an unconditional error. */
1532 void
1533 constant_expression_error (tree value)
1535 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1536 || TREE_CODE (value) == FIXED_CST
1537 || TREE_CODE (value) == VECTOR_CST
1538 || TREE_CODE (value) == COMPLEX_CST)
1539 && TREE_OVERFLOW (value))
1540 error ("overflow in constant expression");
1543 /* Print a warning if an expression had overflow in folding and its
1544 operands hadn't.
1546 Invoke this function on every expression that
1547 (1) appears in the source code, and
1548 (2) is a constant expression that overflowed, and
1549 (3) is not already checked by convert_and_check;
1550 however, do not invoke this function on operands of explicit casts
1551 or when the expression is the result of an operator and any operand
1552 already overflowed. */
1554 void
1555 overflow_warning (location_t loc, tree value)
1557 if (c_inhibit_evaluation_warnings != 0)
1558 return;
1560 switch (TREE_CODE (value))
1562 case INTEGER_CST:
1563 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1564 break;
1566 case REAL_CST:
1567 warning_at (loc, OPT_Woverflow,
1568 "floating point overflow in expression");
1569 break;
1571 case FIXED_CST:
1572 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1573 break;
1575 case VECTOR_CST:
1576 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1577 break;
1579 case COMPLEX_CST:
1580 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1581 warning_at (loc, OPT_Woverflow,
1582 "complex integer overflow in expression");
1583 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1584 warning_at (loc, OPT_Woverflow,
1585 "complex floating point overflow in expression");
1586 break;
1588 default:
1589 break;
1593 /* Warn about uses of logical || / && operator in a context where it
1594 is likely that the bitwise equivalent was intended by the
1595 programmer. We have seen an expression in which CODE is a binary
1596 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1597 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1598 void
1599 warn_logical_operator (location_t location, enum tree_code code, tree type,
1600 enum tree_code code_left, tree op_left,
1601 enum tree_code ARG_UNUSED (code_right), tree op_right)
1603 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1604 int in0_p, in1_p, in_p;
1605 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1606 bool strict_overflow_p = false;
1608 if (code != TRUTH_ANDIF_EXPR
1609 && code != TRUTH_AND_EXPR
1610 && code != TRUTH_ORIF_EXPR
1611 && code != TRUTH_OR_EXPR)
1612 return;
1614 /* Warn if &&/|| are being used in a context where it is
1615 likely that the bitwise equivalent was intended by the
1616 programmer. That is, an expression such as op && MASK
1617 where op should not be any boolean expression, nor a
1618 constant, and mask seems to be a non-boolean integer constant. */
1619 if (!truth_value_p (code_left)
1620 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1621 && !CONSTANT_CLASS_P (op_left)
1622 && !TREE_NO_WARNING (op_left)
1623 && TREE_CODE (op_right) == INTEGER_CST
1624 && !integer_zerop (op_right)
1625 && !integer_onep (op_right))
1627 if (or_op)
1628 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1629 " applied to non-boolean constant");
1630 else
1631 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1632 " applied to non-boolean constant");
1633 TREE_NO_WARNING (op_left) = true;
1634 return;
1637 /* We do not warn for constants because they are typical of macro
1638 expansions that test for features. */
1639 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1640 return;
1642 /* This warning only makes sense with logical operands. */
1643 if (!(truth_value_p (TREE_CODE (op_left))
1644 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1645 || !(truth_value_p (TREE_CODE (op_right))
1646 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1647 return;
1650 /* We first test whether either side separately is trivially true
1651 (with OR) or trivially false (with AND). If so, do not warn.
1652 This is a common idiom for testing ranges of data types in
1653 portable code. */
1654 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1655 if (!lhs)
1656 return;
1657 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1658 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1660 /* If this is an OR operation, invert both sides; now, the result
1661 should be always false to get a warning. */
1662 if (or_op)
1663 in0_p = !in0_p;
1665 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1666 if (tem && integer_zerop (tem))
1667 return;
1669 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1670 if (!rhs)
1671 return;
1672 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1673 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1675 /* If this is an OR operation, invert both sides; now, the result
1676 should be always false to get a warning. */
1677 if (or_op)
1678 in1_p = !in1_p;
1680 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1681 if (tem && integer_zerop (tem))
1682 return;
1684 /* If both expressions have the same operand, if we can merge the
1685 ranges, and if the range test is always false, then warn. */
1686 if (operand_equal_p (lhs, rhs, 0)
1687 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1688 in1_p, low1, high1)
1689 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1690 type, lhs, in_p, low, high))
1691 && integer_zerop (tem))
1693 if (or_op)
1694 warning_at (location, OPT_Wlogical_op,
1695 "logical %<or%> "
1696 "of collectively exhaustive tests is always true");
1697 else
1698 warning_at (location, OPT_Wlogical_op,
1699 "logical %<and%> "
1700 "of mutually exclusive tests is always false");
1705 /* Warn if EXP contains any computations whose results are not used.
1706 Return true if a warning is printed; false otherwise. LOCUS is the
1707 (potential) location of the expression. */
1709 bool
1710 warn_if_unused_value (const_tree exp, location_t locus)
1712 restart:
1713 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1714 return false;
1716 /* Don't warn about void constructs. This includes casting to void,
1717 void function calls, and statement expressions with a final cast
1718 to void. */
1719 if (VOID_TYPE_P (TREE_TYPE (exp)))
1720 return false;
1722 if (EXPR_HAS_LOCATION (exp))
1723 locus = EXPR_LOCATION (exp);
1725 switch (TREE_CODE (exp))
1727 case PREINCREMENT_EXPR:
1728 case POSTINCREMENT_EXPR:
1729 case PREDECREMENT_EXPR:
1730 case POSTDECREMENT_EXPR:
1731 case MODIFY_EXPR:
1732 case INIT_EXPR:
1733 case TARGET_EXPR:
1734 case CALL_EXPR:
1735 case TRY_CATCH_EXPR:
1736 case WITH_CLEANUP_EXPR:
1737 case EXIT_EXPR:
1738 case VA_ARG_EXPR:
1739 return false;
1741 case BIND_EXPR:
1742 /* For a binding, warn if no side effect within it. */
1743 exp = BIND_EXPR_BODY (exp);
1744 goto restart;
1746 case SAVE_EXPR:
1747 case NON_LVALUE_EXPR:
1748 case NOP_EXPR:
1749 exp = TREE_OPERAND (exp, 0);
1750 goto restart;
1752 case TRUTH_ORIF_EXPR:
1753 case TRUTH_ANDIF_EXPR:
1754 /* In && or ||, warn if 2nd operand has no side effect. */
1755 exp = TREE_OPERAND (exp, 1);
1756 goto restart;
1758 case COMPOUND_EXPR:
1759 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1760 return true;
1761 /* Let people do `(foo (), 0)' without a warning. */
1762 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1763 return false;
1764 exp = TREE_OPERAND (exp, 1);
1765 goto restart;
1767 case COND_EXPR:
1768 /* If this is an expression with side effects, don't warn; this
1769 case commonly appears in macro expansions. */
1770 if (TREE_SIDE_EFFECTS (exp))
1771 return false;
1772 goto warn;
1774 case INDIRECT_REF:
1775 /* Don't warn about automatic dereferencing of references, since
1776 the user cannot control it. */
1777 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1779 exp = TREE_OPERAND (exp, 0);
1780 goto restart;
1782 /* Fall through. */
1784 default:
1785 /* Referencing a volatile value is a side effect, so don't warn. */
1786 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1787 && TREE_THIS_VOLATILE (exp))
1788 return false;
1790 /* If this is an expression which has no operands, there is no value
1791 to be unused. There are no such language-independent codes,
1792 but front ends may define such. */
1793 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1794 return false;
1796 warn:
1797 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1802 /* Print a warning about casts that might indicate violation
1803 of strict aliasing rules if -Wstrict-aliasing is used and
1804 strict aliasing mode is in effect. OTYPE is the original
1805 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1807 bool
1808 strict_aliasing_warning (tree otype, tree type, tree expr)
1810 /* Strip pointer conversion chains and get to the correct original type. */
1811 STRIP_NOPS (expr);
1812 otype = TREE_TYPE (expr);
1814 if (!(flag_strict_aliasing
1815 && POINTER_TYPE_P (type)
1816 && POINTER_TYPE_P (otype)
1817 && !VOID_TYPE_P (TREE_TYPE (type)))
1818 /* If the type we are casting to is a ref-all pointer
1819 dereferencing it is always valid. */
1820 || TYPE_REF_CAN_ALIAS_ALL (type))
1821 return false;
1823 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1824 && (DECL_P (TREE_OPERAND (expr, 0))
1825 || handled_component_p (TREE_OPERAND (expr, 0))))
1827 /* Casting the address of an object to non void pointer. Warn
1828 if the cast breaks type based aliasing. */
1829 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1831 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1832 "might break strict-aliasing rules");
1833 return true;
1835 else
1837 /* warn_strict_aliasing >= 3. This includes the default (3).
1838 Only warn if the cast is dereferenced immediately. */
1839 alias_set_type set1 =
1840 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1841 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1843 if (set1 != set2 && set2 != 0
1844 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1846 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1847 "pointer will break strict-aliasing rules");
1848 return true;
1850 else if (warn_strict_aliasing == 2
1851 && !alias_sets_must_conflict_p (set1, set2))
1853 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1854 "pointer might break strict-aliasing rules");
1855 return true;
1859 else
1860 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1862 /* At this level, warn for any conversions, even if an address is
1863 not taken in the same statement. This will likely produce many
1864 false positives, but could be useful to pinpoint problems that
1865 are not revealed at higher levels. */
1866 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1867 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1868 if (!COMPLETE_TYPE_P (type)
1869 || !alias_sets_must_conflict_p (set1, set2))
1871 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1872 "pointer might break strict-aliasing rules");
1873 return true;
1877 return false;
1880 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1881 sizeof as last operand of certain builtins. */
1883 void
1884 sizeof_pointer_memaccess_warning (location_t loc, tree callee,
1885 VEC(tree, gc) *params, tree sizeof_arg,
1886 bool (*comp_types) (tree, tree))
1888 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1889 bool strop = false;
1891 if (TREE_CODE (callee) != FUNCTION_DECL
1892 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1893 || sizeof_arg == error_mark_node
1894 || VEC_length (tree, params) <= 1)
1895 return;
1897 type = TYPE_P (sizeof_arg) ? sizeof_arg : TREE_TYPE (sizeof_arg);
1898 if (!POINTER_TYPE_P (type))
1899 return;
1901 switch (DECL_FUNCTION_CODE (callee))
1903 case BUILT_IN_STRNCMP:
1904 case BUILT_IN_STRNCASECMP:
1905 case BUILT_IN_STRNCPY:
1906 case BUILT_IN_STRNCAT:
1907 strop = true;
1908 /* FALLTHRU */
1909 case BUILT_IN_MEMCPY:
1910 case BUILT_IN_MEMMOVE:
1911 case BUILT_IN_MEMCMP:
1912 if (VEC_length (tree, params) < 3)
1913 return;
1914 src = VEC_index (tree, params, 1);
1915 dest = VEC_index (tree, params, 0);
1916 break;
1917 case BUILT_IN_MEMSET:
1918 if (VEC_length (tree, params) < 3)
1919 return;
1920 dest = VEC_index (tree, params, 0);
1921 break;
1922 case BUILT_IN_STRNDUP:
1923 src = VEC_index (tree, params, 0);
1924 strop = true;
1925 break;
1926 default:
1927 break;
1930 if (dest
1931 && (tem = tree_strip_nop_conversions (dest))
1932 && POINTER_TYPE_P (TREE_TYPE (tem))
1933 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1934 return;
1936 if (src
1937 && (tem = tree_strip_nop_conversions (src))
1938 && POINTER_TYPE_P (TREE_TYPE (tem))
1939 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1940 return;
1942 if (dest)
1944 if (!TYPE_P (sizeof_arg)
1945 && operand_equal_p (dest, sizeof_arg, 0)
1946 && comp_types (TREE_TYPE (dest), type))
1948 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1949 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1950 "argument to %<sizeof%> in %qD call is the same "
1951 "expression as the destination; did you mean to "
1952 "remove the addressof?", callee);
1953 else if ((TYPE_PRECISION (TREE_TYPE (type))
1954 == TYPE_PRECISION (char_type_node))
1955 || strop)
1956 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1957 "argument to %<sizeof%> in %qD call is the same "
1958 "expression as the destination; did you mean to "
1959 "provide an explicit length?", callee);
1960 else
1961 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1962 "argument to %<sizeof%> in %qD call is the same "
1963 "expression as the destination; did you mean to "
1964 "dereference it?", callee);
1965 return;
1968 if (POINTER_TYPE_P (TREE_TYPE (dest))
1969 && !strop
1970 && comp_types (TREE_TYPE (dest), type)
1971 && !VOID_TYPE_P (TREE_TYPE (type)))
1973 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1974 "argument to %<sizeof%> in %qD call is the same "
1975 "pointer type %qT as the destination; expected %qT "
1976 "or an explicit length", callee, TREE_TYPE (dest),
1977 TREE_TYPE (TREE_TYPE (dest)));
1978 return;
1982 if (src)
1984 if (!TYPE_P (sizeof_arg)
1985 && operand_equal_p (src, sizeof_arg, 0)
1986 && comp_types (TREE_TYPE (src), type))
1988 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1989 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1990 "argument to %<sizeof%> in %qD call is the same "
1991 "expression as the source; did you mean to "
1992 "remove the addressof?", callee);
1993 else if ((TYPE_PRECISION (TREE_TYPE (type))
1994 == TYPE_PRECISION (char_type_node))
1995 || strop)
1996 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1997 "argument to %<sizeof%> in %qD call is the same "
1998 "expression as the source; did you mean to "
1999 "provide an explicit length?", callee);
2000 else
2001 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2002 "argument to %<sizeof%> in %qD call is the same "
2003 "expression as the source; did you mean to "
2004 "dereference it?", callee);
2005 return;
2008 if (POINTER_TYPE_P (TREE_TYPE (src))
2009 && !strop
2010 && comp_types (TREE_TYPE (src), type)
2011 && !VOID_TYPE_P (TREE_TYPE (type)))
2013 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2014 "argument to %<sizeof%> in %qD call is the same "
2015 "pointer type %qT as the source; expected %qT "
2016 "or an explicit length", callee, TREE_TYPE (src),
2017 TREE_TYPE (TREE_TYPE (src)));
2018 return;
2023 /* Warn for unlikely, improbable, or stupid DECL declarations
2024 of `main'. */
2026 void
2027 check_main_parameter_types (tree decl)
2029 function_args_iterator iter;
2030 tree type;
2031 int argct = 0;
2033 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2035 /* XXX void_type_node belies the abstraction. */
2036 if (type == void_type_node || type == error_mark_node )
2037 break;
2039 ++argct;
2040 switch (argct)
2042 case 1:
2043 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2044 pedwarn (input_location, OPT_Wmain,
2045 "first argument of %q+D should be %<int%>", decl);
2046 break;
2048 case 2:
2049 if (TREE_CODE (type) != POINTER_TYPE
2050 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2051 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2052 != char_type_node))
2053 pedwarn (input_location, OPT_Wmain,
2054 "second argument of %q+D should be %<char **%>", decl);
2055 break;
2057 case 3:
2058 if (TREE_CODE (type) != POINTER_TYPE
2059 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2060 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2061 != char_type_node))
2062 pedwarn (input_location, OPT_Wmain,
2063 "third argument of %q+D should probably be "
2064 "%<char **%>", decl);
2065 break;
2069 /* It is intentional that this message does not mention the third
2070 argument because it's only mentioned in an appendix of the
2071 standard. */
2072 if (argct > 0 && (argct < 2 || argct > 3))
2073 pedwarn (input_location, OPT_Wmain,
2074 "%q+D takes only zero or two arguments", decl);
2077 /* True if pointers to distinct types T1 and T2 can be converted to
2078 each other without an explicit cast. Only returns true for opaque
2079 vector types. */
2080 bool
2081 vector_targets_convertible_p (const_tree t1, const_tree t2)
2083 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2084 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2085 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2086 return true;
2088 return false;
2091 /* True if vector types T1 and T2 can be converted to each other
2092 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2093 can only be converted with -flax-vector-conversions yet that is not
2094 in effect, emit a note telling the user about that option if such
2095 a note has not previously been emitted. */
2096 bool
2097 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2099 static bool emitted_lax_note = false;
2100 bool convertible_lax;
2102 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2103 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2104 return true;
2106 convertible_lax =
2107 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2108 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2109 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2110 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2111 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2113 if (!convertible_lax || flag_lax_vector_conversions)
2114 return convertible_lax;
2116 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2117 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2118 return true;
2120 if (emit_lax_note && !emitted_lax_note)
2122 emitted_lax_note = true;
2123 inform (input_location, "use -flax-vector-conversions to permit "
2124 "conversions between vectors with differing "
2125 "element types or numbers of subparts");
2128 return false;
2131 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2132 and have vector types, V0 has the same type as V1, and the number of
2133 elements of V0, V1, MASK is the same.
2135 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2136 called with two arguments. In this case implementation passes the
2137 first argument twice in order to share the same tree code. This fact
2138 could enable the mask-values being twice the vector length. This is
2139 an implementation accident and this semantics is not guaranteed to
2140 the user. */
2141 tree
2142 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2144 tree ret;
2145 bool wrap = true;
2146 bool maybe_const = false;
2147 bool two_arguments = false;
2149 if (v1 == NULL_TREE)
2151 two_arguments = true;
2152 v1 = v0;
2155 if (v0 == error_mark_node || v1 == error_mark_node
2156 || mask == error_mark_node)
2157 return error_mark_node;
2159 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2160 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2162 error_at (loc, "__builtin_shuffle last argument must "
2163 "be an integer vector");
2164 return error_mark_node;
2167 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2168 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2170 error_at (loc, "__builtin_shuffle arguments must be vectors");
2171 return error_mark_node;
2174 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2176 error_at (loc, "__builtin_shuffle argument vectors must be of "
2177 "the same type");
2178 return error_mark_node;
2181 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2182 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2183 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2184 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2186 error_at (loc, "__builtin_shuffle number of elements of the "
2187 "argument vector(s) and the mask vector should "
2188 "be the same");
2189 return error_mark_node;
2192 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2193 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2195 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2196 "must have the same size as inner type of the mask");
2197 return error_mark_node;
2200 if (!c_dialect_cxx ())
2202 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2203 v0 = c_fully_fold (v0, false, &maybe_const);
2204 wrap &= maybe_const;
2206 if (two_arguments)
2207 v1 = v0 = save_expr (v0);
2208 else
2210 v1 = c_fully_fold (v1, false, &maybe_const);
2211 wrap &= maybe_const;
2214 mask = c_fully_fold (mask, false, &maybe_const);
2215 wrap &= maybe_const;
2218 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2220 if (!c_dialect_cxx () && !wrap)
2221 ret = c_wrap_maybe_const (ret, true);
2223 return ret;
2226 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2227 to integral type. */
2229 static tree
2230 c_common_get_narrower (tree op, int *unsignedp_ptr)
2232 op = get_narrower (op, unsignedp_ptr);
2234 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2235 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2237 /* C++0x scoped enumerations don't implicitly convert to integral
2238 type; if we stripped an explicit conversion to a larger type we
2239 need to replace it so common_type will still work. */
2240 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2241 TYPE_UNSIGNED (TREE_TYPE (op)));
2242 op = fold_convert (type, op);
2244 return op;
2247 /* This is a helper function of build_binary_op.
2249 For certain operations if both args were extended from the same
2250 smaller type, do the arithmetic in that type and then extend.
2252 BITWISE indicates a bitwise operation.
2253 For them, this optimization is safe only if
2254 both args are zero-extended or both are sign-extended.
2255 Otherwise, we might change the result.
2256 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2257 but calculated in (unsigned short) it would be (unsigned short)-1.
2259 tree
2260 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2262 int unsigned0, unsigned1;
2263 tree arg0, arg1;
2264 int uns;
2265 tree type;
2267 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2268 excessive narrowing when we call get_narrower below. For
2269 example, suppose that OP0 is of unsigned int extended
2270 from signed char and that RESULT_TYPE is long long int.
2271 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2272 like
2274 (long long int) (unsigned int) signed_char
2276 which get_narrower would narrow down to
2278 (unsigned int) signed char
2280 If we do not cast OP0 first, get_narrower would return
2281 signed_char, which is inconsistent with the case of the
2282 explicit cast. */
2283 op0 = convert (result_type, op0);
2284 op1 = convert (result_type, op1);
2286 arg0 = c_common_get_narrower (op0, &unsigned0);
2287 arg1 = c_common_get_narrower (op1, &unsigned1);
2289 /* UNS is 1 if the operation to be done is an unsigned one. */
2290 uns = TYPE_UNSIGNED (result_type);
2292 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2293 but it *requires* conversion to FINAL_TYPE. */
2295 if ((TYPE_PRECISION (TREE_TYPE (op0))
2296 == TYPE_PRECISION (TREE_TYPE (arg0)))
2297 && TREE_TYPE (op0) != result_type)
2298 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2299 if ((TYPE_PRECISION (TREE_TYPE (op1))
2300 == TYPE_PRECISION (TREE_TYPE (arg1)))
2301 && TREE_TYPE (op1) != result_type)
2302 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2304 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2306 /* For bitwise operations, signedness of nominal type
2307 does not matter. Consider only how operands were extended. */
2308 if (bitwise)
2309 uns = unsigned0;
2311 /* Note that in all three cases below we refrain from optimizing
2312 an unsigned operation on sign-extended args.
2313 That would not be valid. */
2315 /* Both args variable: if both extended in same way
2316 from same width, do it in that width.
2317 Do it unsigned if args were zero-extended. */
2318 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2319 < TYPE_PRECISION (result_type))
2320 && (TYPE_PRECISION (TREE_TYPE (arg1))
2321 == TYPE_PRECISION (TREE_TYPE (arg0)))
2322 && unsigned0 == unsigned1
2323 && (unsigned0 || !uns))
2324 return c_common_signed_or_unsigned_type
2325 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2327 else if (TREE_CODE (arg0) == INTEGER_CST
2328 && (unsigned1 || !uns)
2329 && (TYPE_PRECISION (TREE_TYPE (arg1))
2330 < TYPE_PRECISION (result_type))
2331 && (type
2332 = c_common_signed_or_unsigned_type (unsigned1,
2333 TREE_TYPE (arg1)))
2334 && !POINTER_TYPE_P (type)
2335 && int_fits_type_p (arg0, type))
2336 return type;
2338 else if (TREE_CODE (arg1) == INTEGER_CST
2339 && (unsigned0 || !uns)
2340 && (TYPE_PRECISION (TREE_TYPE (arg0))
2341 < TYPE_PRECISION (result_type))
2342 && (type
2343 = c_common_signed_or_unsigned_type (unsigned0,
2344 TREE_TYPE (arg0)))
2345 && !POINTER_TYPE_P (type)
2346 && int_fits_type_p (arg1, type))
2347 return type;
2349 return result_type;
2352 /* Checks if expression EXPR of real/integer type cannot be converted
2353 to the real/integer type TYPE. Function returns true when:
2354 * EXPR is a constant which cannot be exactly converted to TYPE
2355 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2356 for EXPR type and TYPE being both integers or both real.
2357 * EXPR is not a constant of real type and TYPE is an integer.
2358 * EXPR is not a constant of integer type which cannot be
2359 exactly converted to real type.
2360 Function allows conversions between types of different signedness and
2361 does not return true in that case. Function can produce signedness
2362 warnings if PRODUCE_WARNS is true. */
2363 bool
2364 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2366 bool give_warning = false;
2367 tree expr_type = TREE_TYPE (expr);
2368 location_t loc = EXPR_LOC_OR_HERE (expr);
2370 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2372 /* Warn for real constant that is not an exact integer converted
2373 to integer type. */
2374 if (TREE_CODE (expr_type) == REAL_TYPE
2375 && TREE_CODE (type) == INTEGER_TYPE)
2377 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2378 give_warning = true;
2380 /* Warn for an integer constant that does not fit into integer type. */
2381 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2382 && TREE_CODE (type) == INTEGER_TYPE
2383 && !int_fits_type_p (expr, type))
2385 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2386 && tree_int_cst_sgn (expr) < 0)
2388 if (produce_warns)
2389 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2390 " implicitly converted to unsigned type");
2392 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2394 if (produce_warns)
2395 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2396 " constant value to negative integer");
2398 else
2399 give_warning = true;
2401 else if (TREE_CODE (type) == REAL_TYPE)
2403 /* Warn for an integer constant that does not fit into real type. */
2404 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2406 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2407 if (!exact_real_truncate (TYPE_MODE (type), &a))
2408 give_warning = true;
2410 /* Warn for a real constant that does not fit into a smaller
2411 real type. */
2412 else if (TREE_CODE (expr_type) == REAL_TYPE
2413 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2415 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2416 if (!exact_real_truncate (TYPE_MODE (type), &a))
2417 give_warning = true;
2421 else
2423 /* Warn for real types converted to integer types. */
2424 if (TREE_CODE (expr_type) == REAL_TYPE
2425 && TREE_CODE (type) == INTEGER_TYPE)
2426 give_warning = true;
2428 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2429 && TREE_CODE (type) == INTEGER_TYPE)
2431 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2432 expr = get_unwidened (expr, 0);
2433 expr_type = TREE_TYPE (expr);
2435 /* Don't warn for short y; short x = ((int)y & 0xff); */
2436 if (TREE_CODE (expr) == BIT_AND_EXPR
2437 || TREE_CODE (expr) == BIT_IOR_EXPR
2438 || TREE_CODE (expr) == BIT_XOR_EXPR)
2440 /* If both args were extended from a shortest type,
2441 use that type if that is safe. */
2442 expr_type = shorten_binary_op (expr_type,
2443 TREE_OPERAND (expr, 0),
2444 TREE_OPERAND (expr, 1),
2445 /* bitwise */1);
2447 if (TREE_CODE (expr) == BIT_AND_EXPR)
2449 tree op0 = TREE_OPERAND (expr, 0);
2450 tree op1 = TREE_OPERAND (expr, 1);
2451 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2452 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2454 /* If one of the operands is a non-negative constant
2455 that fits in the target type, then the type of the
2456 other operand does not matter. */
2457 if ((TREE_CODE (op0) == INTEGER_CST
2458 && int_fits_type_p (op0, c_common_signed_type (type))
2459 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2460 || (TREE_CODE (op1) == INTEGER_CST
2461 && int_fits_type_p (op1, c_common_signed_type (type))
2462 && int_fits_type_p (op1,
2463 c_common_unsigned_type (type))))
2464 return false;
2465 /* If constant is unsigned and fits in the target
2466 type, then the result will also fit. */
2467 else if ((TREE_CODE (op0) == INTEGER_CST
2468 && unsigned0
2469 && int_fits_type_p (op0, type))
2470 || (TREE_CODE (op1) == INTEGER_CST
2471 && unsigned1
2472 && int_fits_type_p (op1, type)))
2473 return false;
2476 /* Warn for integer types converted to smaller integer types. */
2477 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2478 give_warning = true;
2480 /* When they are the same width but different signedness,
2481 then the value may change. */
2482 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2483 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2484 /* Even when converted to a bigger type, if the type is
2485 unsigned but expr is signed, then negative values
2486 will be changed. */
2487 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2488 && produce_warns)
2489 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2490 "may change the sign of the result",
2491 type, expr_type);
2494 /* Warn for integer types converted to real types if and only if
2495 all the range of values of the integer type cannot be
2496 represented by the real type. */
2497 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2498 && TREE_CODE (type) == REAL_TYPE)
2500 tree type_low_bound, type_high_bound;
2501 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2503 /* Don't warn about char y = 0xff; float x = (int) y; */
2504 expr = get_unwidened (expr, 0);
2505 expr_type = TREE_TYPE (expr);
2507 type_low_bound = TYPE_MIN_VALUE (expr_type);
2508 type_high_bound = TYPE_MAX_VALUE (expr_type);
2509 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2510 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2512 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2513 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2514 give_warning = true;
2517 /* Warn for real types converted to smaller real types. */
2518 else if (TREE_CODE (expr_type) == REAL_TYPE
2519 && TREE_CODE (type) == REAL_TYPE
2520 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2521 give_warning = true;
2524 return give_warning;
2527 /* Warns if the conversion of EXPR to TYPE may alter a value.
2528 This is a helper function for warnings_for_convert_and_check. */
2530 static void
2531 conversion_warning (tree type, tree expr)
2533 tree expr_type = TREE_TYPE (expr);
2534 location_t loc = EXPR_LOC_OR_HERE (expr);
2536 if (!warn_conversion && !warn_sign_conversion)
2537 return;
2539 switch (TREE_CODE (expr))
2541 case EQ_EXPR:
2542 case NE_EXPR:
2543 case LE_EXPR:
2544 case GE_EXPR:
2545 case LT_EXPR:
2546 case GT_EXPR:
2547 case TRUTH_ANDIF_EXPR:
2548 case TRUTH_ORIF_EXPR:
2549 case TRUTH_AND_EXPR:
2550 case TRUTH_OR_EXPR:
2551 case TRUTH_XOR_EXPR:
2552 case TRUTH_NOT_EXPR:
2553 /* Conversion from boolean to a signed:1 bit-field (which only
2554 can hold the values 0 and -1) doesn't lose information - but
2555 it does change the value. */
2556 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2557 warning_at (loc, OPT_Wconversion,
2558 "conversion to %qT from boolean expression", type);
2559 return;
2561 case REAL_CST:
2562 case INTEGER_CST:
2563 if (unsafe_conversion_p (type, expr, true))
2564 warning_at (loc, OPT_Wconversion,
2565 "conversion to %qT alters %qT constant value",
2566 type, expr_type);
2567 return;
2569 case COND_EXPR:
2571 /* In case of COND_EXPR, if both operands are constants or
2572 COND_EXPR, then we do not care about the type of COND_EXPR,
2573 only about the conversion of each operand. */
2574 tree op1 = TREE_OPERAND (expr, 1);
2575 tree op2 = TREE_OPERAND (expr, 2);
2577 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2578 || TREE_CODE (op1) == COND_EXPR)
2579 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2580 || TREE_CODE (op2) == COND_EXPR))
2582 conversion_warning (type, op1);
2583 conversion_warning (type, op2);
2584 return;
2586 /* Fall through. */
2589 default: /* 'expr' is not a constant. */
2590 if (unsafe_conversion_p (type, expr, true))
2591 warning_at (loc, OPT_Wconversion,
2592 "conversion to %qT from %qT may alter its value",
2593 type, expr_type);
2597 /* Produce warnings after a conversion. RESULT is the result of
2598 converting EXPR to TYPE. This is a helper function for
2599 convert_and_check and cp_convert_and_check. */
2601 void
2602 warnings_for_convert_and_check (tree type, tree expr, tree result)
2604 location_t loc = EXPR_LOC_OR_HERE (expr);
2606 if (TREE_CODE (expr) == INTEGER_CST
2607 && (TREE_CODE (type) == INTEGER_TYPE
2608 || TREE_CODE (type) == ENUMERAL_TYPE)
2609 && !int_fits_type_p (expr, type))
2611 /* Do not diagnose overflow in a constant expression merely
2612 because a conversion overflowed. */
2613 if (TREE_OVERFLOW (result))
2614 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2616 if (TYPE_UNSIGNED (type))
2618 /* This detects cases like converting -129 or 256 to
2619 unsigned char. */
2620 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2621 warning_at (loc, OPT_Woverflow,
2622 "large integer implicitly truncated to unsigned type");
2623 else
2624 conversion_warning (type, expr);
2626 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2627 warning (OPT_Woverflow,
2628 "overflow in implicit constant conversion");
2629 /* No warning for converting 0x80000000 to int. */
2630 else if (pedantic
2631 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2632 || TYPE_PRECISION (TREE_TYPE (expr))
2633 != TYPE_PRECISION (type)))
2634 warning_at (loc, OPT_Woverflow,
2635 "overflow in implicit constant conversion");
2637 else
2638 conversion_warning (type, expr);
2640 else if ((TREE_CODE (result) == INTEGER_CST
2641 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2642 warning_at (loc, OPT_Woverflow,
2643 "overflow in implicit constant conversion");
2644 else
2645 conversion_warning (type, expr);
2649 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2650 Invoke this function on every expression that is converted implicitly,
2651 i.e. because of language rules and not because of an explicit cast. */
2653 tree
2654 convert_and_check (tree type, tree expr)
2656 tree result;
2657 tree expr_for_warning;
2659 /* Convert from a value with possible excess precision rather than
2660 via the semantic type, but do not warn about values not fitting
2661 exactly in the semantic type. */
2662 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2664 tree orig_type = TREE_TYPE (expr);
2665 expr = TREE_OPERAND (expr, 0);
2666 expr_for_warning = convert (orig_type, expr);
2667 if (orig_type == type)
2668 return expr_for_warning;
2670 else
2671 expr_for_warning = expr;
2673 if (TREE_TYPE (expr) == type)
2674 return expr;
2676 result = convert (type, expr);
2678 if (c_inhibit_evaluation_warnings == 0
2679 && !TREE_OVERFLOW_P (expr)
2680 && result != error_mark_node)
2681 warnings_for_convert_and_check (type, expr_for_warning, result);
2683 return result;
2686 /* A node in a list that describes references to variables (EXPR), which are
2687 either read accesses if WRITER is zero, or write accesses, in which case
2688 WRITER is the parent of EXPR. */
2689 struct tlist
2691 struct tlist *next;
2692 tree expr, writer;
2695 /* Used to implement a cache the results of a call to verify_tree. We only
2696 use this for SAVE_EXPRs. */
2697 struct tlist_cache
2699 struct tlist_cache *next;
2700 struct tlist *cache_before_sp;
2701 struct tlist *cache_after_sp;
2702 tree expr;
2705 /* Obstack to use when allocating tlist structures, and corresponding
2706 firstobj. */
2707 static struct obstack tlist_obstack;
2708 static char *tlist_firstobj = 0;
2710 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2711 warnings. */
2712 static struct tlist *warned_ids;
2713 /* SAVE_EXPRs need special treatment. We process them only once and then
2714 cache the results. */
2715 static struct tlist_cache *save_expr_cache;
2717 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2718 static void merge_tlist (struct tlist **, struct tlist *, int);
2719 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2720 static int warning_candidate_p (tree);
2721 static bool candidate_equal_p (const_tree, const_tree);
2722 static void warn_for_collisions (struct tlist *);
2723 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2724 static struct tlist *new_tlist (struct tlist *, tree, tree);
2726 /* Create a new struct tlist and fill in its fields. */
2727 static struct tlist *
2728 new_tlist (struct tlist *next, tree t, tree writer)
2730 struct tlist *l;
2731 l = XOBNEW (&tlist_obstack, struct tlist);
2732 l->next = next;
2733 l->expr = t;
2734 l->writer = writer;
2735 return l;
2738 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2739 is nonnull, we ignore any node we find which has a writer equal to it. */
2741 static void
2742 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2744 while (add)
2746 struct tlist *next = add->next;
2747 if (!copy)
2748 add->next = *to;
2749 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2750 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2751 add = next;
2755 /* Merge the nodes of ADD into TO. This merging process is done so that for
2756 each variable that already exists in TO, no new node is added; however if
2757 there is a write access recorded in ADD, and an occurrence on TO is only
2758 a read access, then the occurrence in TO will be modified to record the
2759 write. */
2761 static void
2762 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2764 struct tlist **end = to;
2766 while (*end)
2767 end = &(*end)->next;
2769 while (add)
2771 int found = 0;
2772 struct tlist *tmp2;
2773 struct tlist *next = add->next;
2775 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2776 if (candidate_equal_p (tmp2->expr, add->expr))
2778 found = 1;
2779 if (!tmp2->writer)
2780 tmp2->writer = add->writer;
2782 if (!found)
2784 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2785 end = &(*end)->next;
2786 *end = 0;
2788 add = next;
2792 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2793 references in list LIST conflict with it, excluding reads if ONLY writers
2794 is nonzero. */
2796 static void
2797 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2798 int only_writes)
2800 struct tlist *tmp;
2802 /* Avoid duplicate warnings. */
2803 for (tmp = warned_ids; tmp; tmp = tmp->next)
2804 if (candidate_equal_p (tmp->expr, written))
2805 return;
2807 while (list)
2809 if (candidate_equal_p (list->expr, written)
2810 && !candidate_equal_p (list->writer, writer)
2811 && (!only_writes || list->writer))
2813 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2814 warning_at (EXPR_LOC_OR_HERE (writer),
2815 OPT_Wsequence_point, "operation on %qE may be undefined",
2816 list->expr);
2818 list = list->next;
2822 /* Given a list LIST of references to variables, find whether any of these
2823 can cause conflicts due to missing sequence points. */
2825 static void
2826 warn_for_collisions (struct tlist *list)
2828 struct tlist *tmp;
2830 for (tmp = list; tmp; tmp = tmp->next)
2832 if (tmp->writer)
2833 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2837 /* Return nonzero if X is a tree that can be verified by the sequence point
2838 warnings. */
2839 static int
2840 warning_candidate_p (tree x)
2842 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2843 return 0;
2845 if (TREE_CODE (x) == BLOCK)
2846 return 0;
2848 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2849 (lvalue_p) crash on TRY/CATCH. */
2850 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2851 return 0;
2853 if (!lvalue_p (x))
2854 return 0;
2856 /* No point to track non-const calls, they will never satisfy
2857 operand_equal_p. */
2858 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2859 return 0;
2861 if (TREE_CODE (x) == STRING_CST)
2862 return 0;
2864 return 1;
2867 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2868 static bool
2869 candidate_equal_p (const_tree x, const_tree y)
2871 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2874 /* Walk the tree X, and record accesses to variables. If X is written by the
2875 parent tree, WRITER is the parent.
2876 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2877 expression or its only operand forces a sequence point, then everything up
2878 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2879 in PNO_SP.
2880 Once we return, we will have emitted warnings if any subexpression before
2881 such a sequence point could be undefined. On a higher level, however, the
2882 sequence point may not be relevant, and we'll merge the two lists.
2884 Example: (b++, a) + b;
2885 The call that processes the COMPOUND_EXPR will store the increment of B
2886 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2887 processes the PLUS_EXPR will need to merge the two lists so that
2888 eventually, all accesses end up on the same list (and we'll warn about the
2889 unordered subexpressions b++ and b.
2891 A note on merging. If we modify the former example so that our expression
2892 becomes
2893 (b++, b) + a
2894 care must be taken not simply to add all three expressions into the final
2895 PNO_SP list. The function merge_tlist takes care of that by merging the
2896 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2897 way, so that no more than one access to B is recorded. */
2899 static void
2900 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2901 tree writer)
2903 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2904 enum tree_code code;
2905 enum tree_code_class cl;
2907 /* X may be NULL if it is the operand of an empty statement expression
2908 ({ }). */
2909 if (x == NULL)
2910 return;
2912 restart:
2913 code = TREE_CODE (x);
2914 cl = TREE_CODE_CLASS (code);
2916 if (warning_candidate_p (x))
2917 *pno_sp = new_tlist (*pno_sp, x, writer);
2919 switch (code)
2921 case CONSTRUCTOR:
2922 return;
2924 case COMPOUND_EXPR:
2925 case TRUTH_ANDIF_EXPR:
2926 case TRUTH_ORIF_EXPR:
2927 tmp_before = tmp_nosp = tmp_list3 = 0;
2928 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2929 warn_for_collisions (tmp_nosp);
2930 merge_tlist (pbefore_sp, tmp_before, 0);
2931 merge_tlist (pbefore_sp, tmp_nosp, 0);
2932 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2933 merge_tlist (pbefore_sp, tmp_list3, 0);
2934 return;
2936 case COND_EXPR:
2937 tmp_before = tmp_list2 = 0;
2938 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2939 warn_for_collisions (tmp_list2);
2940 merge_tlist (pbefore_sp, tmp_before, 0);
2941 merge_tlist (pbefore_sp, tmp_list2, 1);
2943 tmp_list3 = tmp_nosp = 0;
2944 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2945 warn_for_collisions (tmp_nosp);
2946 merge_tlist (pbefore_sp, tmp_list3, 0);
2948 tmp_list3 = tmp_list2 = 0;
2949 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2950 warn_for_collisions (tmp_list2);
2951 merge_tlist (pbefore_sp, tmp_list3, 0);
2952 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2953 two first, to avoid warning for (a ? b++ : b++). */
2954 merge_tlist (&tmp_nosp, tmp_list2, 0);
2955 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2956 return;
2958 case PREDECREMENT_EXPR:
2959 case PREINCREMENT_EXPR:
2960 case POSTDECREMENT_EXPR:
2961 case POSTINCREMENT_EXPR:
2962 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2963 return;
2965 case MODIFY_EXPR:
2966 tmp_before = tmp_nosp = tmp_list3 = 0;
2967 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2968 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2969 /* Expressions inside the LHS are not ordered wrt. the sequence points
2970 in the RHS. Example:
2971 *a = (a++, 2)
2972 Despite the fact that the modification of "a" is in the before_sp
2973 list (tmp_before), it conflicts with the use of "a" in the LHS.
2974 We can handle this by adding the contents of tmp_list3
2975 to those of tmp_before, and redoing the collision warnings for that
2976 list. */
2977 add_tlist (&tmp_before, tmp_list3, x, 1);
2978 warn_for_collisions (tmp_before);
2979 /* Exclude the LHS itself here; we first have to merge it into the
2980 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2981 didn't exclude the LHS, we'd get it twice, once as a read and once
2982 as a write. */
2983 add_tlist (pno_sp, tmp_list3, x, 0);
2984 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2986 merge_tlist (pbefore_sp, tmp_before, 0);
2987 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2988 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2989 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2990 return;
2992 case CALL_EXPR:
2993 /* We need to warn about conflicts among arguments and conflicts between
2994 args and the function address. Side effects of the function address,
2995 however, are not ordered by the sequence point of the call. */
2997 call_expr_arg_iterator iter;
2998 tree arg;
2999 tmp_before = tmp_nosp = 0;
3000 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3001 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3003 tmp_list2 = tmp_list3 = 0;
3004 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3005 merge_tlist (&tmp_list3, tmp_list2, 0);
3006 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3008 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3009 warn_for_collisions (tmp_before);
3010 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3011 return;
3014 case TREE_LIST:
3015 /* Scan all the list, e.g. indices of multi dimensional array. */
3016 while (x)
3018 tmp_before = tmp_nosp = 0;
3019 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3020 merge_tlist (&tmp_nosp, tmp_before, 0);
3021 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3022 x = TREE_CHAIN (x);
3024 return;
3026 case SAVE_EXPR:
3028 struct tlist_cache *t;
3029 for (t = save_expr_cache; t; t = t->next)
3030 if (candidate_equal_p (t->expr, x))
3031 break;
3033 if (!t)
3035 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3036 t->next = save_expr_cache;
3037 t->expr = x;
3038 save_expr_cache = t;
3040 tmp_before = tmp_nosp = 0;
3041 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3042 warn_for_collisions (tmp_nosp);
3044 tmp_list3 = 0;
3045 while (tmp_nosp)
3047 struct tlist *t = tmp_nosp;
3048 tmp_nosp = t->next;
3049 merge_tlist (&tmp_list3, t, 0);
3051 t->cache_before_sp = tmp_before;
3052 t->cache_after_sp = tmp_list3;
3054 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3055 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3056 return;
3059 case ADDR_EXPR:
3060 x = TREE_OPERAND (x, 0);
3061 if (DECL_P (x))
3062 return;
3063 writer = 0;
3064 goto restart;
3066 default:
3067 /* For other expressions, simply recurse on their operands.
3068 Manual tail recursion for unary expressions.
3069 Other non-expressions need not be processed. */
3070 if (cl == tcc_unary)
3072 x = TREE_OPERAND (x, 0);
3073 writer = 0;
3074 goto restart;
3076 else if (IS_EXPR_CODE_CLASS (cl))
3078 int lp;
3079 int max = TREE_OPERAND_LENGTH (x);
3080 for (lp = 0; lp < max; lp++)
3082 tmp_before = tmp_nosp = 0;
3083 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3084 merge_tlist (&tmp_nosp, tmp_before, 0);
3085 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3088 return;
3092 /* Try to warn for undefined behavior in EXPR due to missing sequence
3093 points. */
3095 DEBUG_FUNCTION void
3096 verify_sequence_points (tree expr)
3098 struct tlist *before_sp = 0, *after_sp = 0;
3100 warned_ids = 0;
3101 save_expr_cache = 0;
3102 if (tlist_firstobj == 0)
3104 gcc_obstack_init (&tlist_obstack);
3105 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3108 verify_tree (expr, &before_sp, &after_sp, 0);
3109 warn_for_collisions (after_sp);
3110 obstack_free (&tlist_obstack, tlist_firstobj);
3113 /* Validate the expression after `case' and apply default promotions. */
3115 static tree
3116 check_case_value (tree value)
3118 if (value == NULL_TREE)
3119 return value;
3121 if (TREE_CODE (value) == INTEGER_CST)
3122 /* Promote char or short to int. */
3123 value = perform_integral_promotions (value);
3124 else if (value != error_mark_node)
3126 error ("case label does not reduce to an integer constant");
3127 value = error_mark_node;
3130 constant_expression_warning (value);
3132 return value;
3135 /* See if the case values LOW and HIGH are in the range of the original
3136 type (i.e. before the default conversion to int) of the switch testing
3137 expression.
3138 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3139 the type before promoting it. CASE_LOW_P is a pointer to the lower
3140 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3141 if the case is not a case range.
3142 The caller has to make sure that we are not called with NULL for
3143 CASE_LOW_P (i.e. the default case).
3144 Returns true if the case label is in range of ORIG_TYPE (saturated or
3145 untouched) or false if the label is out of range. */
3147 static bool
3148 check_case_bounds (tree type, tree orig_type,
3149 tree *case_low_p, tree *case_high_p)
3151 tree min_value, max_value;
3152 tree case_low = *case_low_p;
3153 tree case_high = case_high_p ? *case_high_p : case_low;
3155 /* If there was a problem with the original type, do nothing. */
3156 if (orig_type == error_mark_node)
3157 return true;
3159 min_value = TYPE_MIN_VALUE (orig_type);
3160 max_value = TYPE_MAX_VALUE (orig_type);
3162 /* Case label is less than minimum for type. */
3163 if (tree_int_cst_compare (case_low, min_value) < 0
3164 && tree_int_cst_compare (case_high, min_value) < 0)
3166 warning (0, "case label value is less than minimum value for type");
3167 return false;
3170 /* Case value is greater than maximum for type. */
3171 if (tree_int_cst_compare (case_low, max_value) > 0
3172 && tree_int_cst_compare (case_high, max_value) > 0)
3174 warning (0, "case label value exceeds maximum value for type");
3175 return false;
3178 /* Saturate lower case label value to minimum. */
3179 if (tree_int_cst_compare (case_high, min_value) >= 0
3180 && tree_int_cst_compare (case_low, min_value) < 0)
3182 warning (0, "lower value in case label range"
3183 " less than minimum value for type");
3184 case_low = min_value;
3187 /* Saturate upper case label value to maximum. */
3188 if (tree_int_cst_compare (case_low, max_value) <= 0
3189 && tree_int_cst_compare (case_high, max_value) > 0)
3191 warning (0, "upper value in case label range"
3192 " exceeds maximum value for type");
3193 case_high = max_value;
3196 if (*case_low_p != case_low)
3197 *case_low_p = convert (type, case_low);
3198 if (case_high_p && *case_high_p != case_high)
3199 *case_high_p = convert (type, case_high);
3201 return true;
3204 /* Return an integer type with BITS bits of precision,
3205 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3207 tree
3208 c_common_type_for_size (unsigned int bits, int unsignedp)
3210 if (bits == TYPE_PRECISION (integer_type_node))
3211 return unsignedp ? unsigned_type_node : integer_type_node;
3213 if (bits == TYPE_PRECISION (signed_char_type_node))
3214 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3216 if (bits == TYPE_PRECISION (short_integer_type_node))
3217 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3219 if (bits == TYPE_PRECISION (long_integer_type_node))
3220 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3222 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3223 return (unsignedp ? long_long_unsigned_type_node
3224 : long_long_integer_type_node);
3226 if (int128_integer_type_node
3227 && bits == TYPE_PRECISION (int128_integer_type_node))
3228 return (unsignedp ? int128_unsigned_type_node
3229 : int128_integer_type_node);
3231 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3232 return (unsignedp ? widest_unsigned_literal_type_node
3233 : widest_integer_literal_type_node);
3235 if (bits <= TYPE_PRECISION (intQI_type_node))
3236 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3238 if (bits <= TYPE_PRECISION (intHI_type_node))
3239 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3241 if (bits <= TYPE_PRECISION (intSI_type_node))
3242 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3244 if (bits <= TYPE_PRECISION (intDI_type_node))
3245 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3247 return 0;
3250 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3251 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3252 and saturating if SATP is nonzero, otherwise not saturating. */
3254 tree
3255 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3256 int unsignedp, int satp)
3258 enum machine_mode mode;
3259 if (ibit == 0)
3260 mode = unsignedp ? UQQmode : QQmode;
3261 else
3262 mode = unsignedp ? UHAmode : HAmode;
3264 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3265 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3266 break;
3268 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3270 sorry ("GCC cannot support operators with integer types and "
3271 "fixed-point types that have too many integral and "
3272 "fractional bits together");
3273 return 0;
3276 return c_common_type_for_mode (mode, satp);
3279 /* Used for communication between c_common_type_for_mode and
3280 c_register_builtin_type. */
3281 static GTY(()) tree registered_builtin_types;
3283 /* Return a data type that has machine mode MODE.
3284 If the mode is an integer,
3285 then UNSIGNEDP selects between signed and unsigned types.
3286 If the mode is a fixed-point mode,
3287 then UNSIGNEDP selects between saturating and nonsaturating types. */
3289 tree
3290 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3292 tree t;
3294 if (mode == TYPE_MODE (integer_type_node))
3295 return unsignedp ? unsigned_type_node : integer_type_node;
3297 if (mode == TYPE_MODE (signed_char_type_node))
3298 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3300 if (mode == TYPE_MODE (short_integer_type_node))
3301 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3303 if (mode == TYPE_MODE (long_integer_type_node))
3304 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3306 if (mode == TYPE_MODE (long_long_integer_type_node))
3307 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3309 if (int128_integer_type_node
3310 && mode == TYPE_MODE (int128_integer_type_node))
3311 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3313 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3314 return unsignedp ? widest_unsigned_literal_type_node
3315 : widest_integer_literal_type_node;
3317 if (mode == QImode)
3318 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3320 if (mode == HImode)
3321 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3323 if (mode == SImode)
3324 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3326 if (mode == DImode)
3327 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3329 #if HOST_BITS_PER_WIDE_INT >= 64
3330 if (mode == TYPE_MODE (intTI_type_node))
3331 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3332 #endif
3334 if (mode == TYPE_MODE (float_type_node))
3335 return float_type_node;
3337 if (mode == TYPE_MODE (double_type_node))
3338 return double_type_node;
3340 if (mode == TYPE_MODE (long_double_type_node))
3341 return long_double_type_node;
3343 if (mode == TYPE_MODE (void_type_node))
3344 return void_type_node;
3346 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3347 return (unsignedp
3348 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3349 : make_signed_type (GET_MODE_PRECISION (mode)));
3351 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3352 return (unsignedp
3353 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3354 : make_signed_type (GET_MODE_PRECISION (mode)));
3356 if (COMPLEX_MODE_P (mode))
3358 enum machine_mode inner_mode;
3359 tree inner_type;
3361 if (mode == TYPE_MODE (complex_float_type_node))
3362 return complex_float_type_node;
3363 if (mode == TYPE_MODE (complex_double_type_node))
3364 return complex_double_type_node;
3365 if (mode == TYPE_MODE (complex_long_double_type_node))
3366 return complex_long_double_type_node;
3368 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3369 return complex_integer_type_node;
3371 inner_mode = GET_MODE_INNER (mode);
3372 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3373 if (inner_type != NULL_TREE)
3374 return build_complex_type (inner_type);
3376 else if (VECTOR_MODE_P (mode))
3378 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3379 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3380 if (inner_type != NULL_TREE)
3381 return build_vector_type_for_mode (inner_type, mode);
3384 if (mode == TYPE_MODE (dfloat32_type_node))
3385 return dfloat32_type_node;
3386 if (mode == TYPE_MODE (dfloat64_type_node))
3387 return dfloat64_type_node;
3388 if (mode == TYPE_MODE (dfloat128_type_node))
3389 return dfloat128_type_node;
3391 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3393 if (mode == TYPE_MODE (short_fract_type_node))
3394 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3395 if (mode == TYPE_MODE (fract_type_node))
3396 return unsignedp ? sat_fract_type_node : fract_type_node;
3397 if (mode == TYPE_MODE (long_fract_type_node))
3398 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3399 if (mode == TYPE_MODE (long_long_fract_type_node))
3400 return unsignedp ? sat_long_long_fract_type_node
3401 : long_long_fract_type_node;
3403 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3404 return unsignedp ? sat_unsigned_short_fract_type_node
3405 : unsigned_short_fract_type_node;
3406 if (mode == TYPE_MODE (unsigned_fract_type_node))
3407 return unsignedp ? sat_unsigned_fract_type_node
3408 : unsigned_fract_type_node;
3409 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3410 return unsignedp ? sat_unsigned_long_fract_type_node
3411 : unsigned_long_fract_type_node;
3412 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3413 return unsignedp ? sat_unsigned_long_long_fract_type_node
3414 : unsigned_long_long_fract_type_node;
3416 if (mode == TYPE_MODE (short_accum_type_node))
3417 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3418 if (mode == TYPE_MODE (accum_type_node))
3419 return unsignedp ? sat_accum_type_node : accum_type_node;
3420 if (mode == TYPE_MODE (long_accum_type_node))
3421 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3422 if (mode == TYPE_MODE (long_long_accum_type_node))
3423 return unsignedp ? sat_long_long_accum_type_node
3424 : long_long_accum_type_node;
3426 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3427 return unsignedp ? sat_unsigned_short_accum_type_node
3428 : unsigned_short_accum_type_node;
3429 if (mode == TYPE_MODE (unsigned_accum_type_node))
3430 return unsignedp ? sat_unsigned_accum_type_node
3431 : unsigned_accum_type_node;
3432 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3433 return unsignedp ? sat_unsigned_long_accum_type_node
3434 : unsigned_long_accum_type_node;
3435 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3436 return unsignedp ? sat_unsigned_long_long_accum_type_node
3437 : unsigned_long_long_accum_type_node;
3439 if (mode == QQmode)
3440 return unsignedp ? sat_qq_type_node : qq_type_node;
3441 if (mode == HQmode)
3442 return unsignedp ? sat_hq_type_node : hq_type_node;
3443 if (mode == SQmode)
3444 return unsignedp ? sat_sq_type_node : sq_type_node;
3445 if (mode == DQmode)
3446 return unsignedp ? sat_dq_type_node : dq_type_node;
3447 if (mode == TQmode)
3448 return unsignedp ? sat_tq_type_node : tq_type_node;
3450 if (mode == UQQmode)
3451 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3452 if (mode == UHQmode)
3453 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3454 if (mode == USQmode)
3455 return unsignedp ? sat_usq_type_node : usq_type_node;
3456 if (mode == UDQmode)
3457 return unsignedp ? sat_udq_type_node : udq_type_node;
3458 if (mode == UTQmode)
3459 return unsignedp ? sat_utq_type_node : utq_type_node;
3461 if (mode == HAmode)
3462 return unsignedp ? sat_ha_type_node : ha_type_node;
3463 if (mode == SAmode)
3464 return unsignedp ? sat_sa_type_node : sa_type_node;
3465 if (mode == DAmode)
3466 return unsignedp ? sat_da_type_node : da_type_node;
3467 if (mode == TAmode)
3468 return unsignedp ? sat_ta_type_node : ta_type_node;
3470 if (mode == UHAmode)
3471 return unsignedp ? sat_uha_type_node : uha_type_node;
3472 if (mode == USAmode)
3473 return unsignedp ? sat_usa_type_node : usa_type_node;
3474 if (mode == UDAmode)
3475 return unsignedp ? sat_uda_type_node : uda_type_node;
3476 if (mode == UTAmode)
3477 return unsignedp ? sat_uta_type_node : uta_type_node;
3480 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3481 if (TYPE_MODE (TREE_VALUE (t)) == mode
3482 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3483 return TREE_VALUE (t);
3485 return 0;
3488 tree
3489 c_common_unsigned_type (tree type)
3491 return c_common_signed_or_unsigned_type (1, type);
3494 /* Return a signed type the same as TYPE in other respects. */
3496 tree
3497 c_common_signed_type (tree type)
3499 return c_common_signed_or_unsigned_type (0, type);
3502 /* Return a type the same as TYPE except unsigned or
3503 signed according to UNSIGNEDP. */
3505 tree
3506 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3508 tree type1;
3510 /* This block of code emulates the behavior of the old
3511 c_common_unsigned_type. In particular, it returns
3512 long_unsigned_type_node if passed a long, even when a int would
3513 have the same size. This is necessary for warnings to work
3514 correctly in archs where sizeof(int) == sizeof(long) */
3516 type1 = TYPE_MAIN_VARIANT (type);
3517 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3518 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3519 if (type1 == integer_type_node || type1 == unsigned_type_node)
3520 return unsignedp ? unsigned_type_node : integer_type_node;
3521 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3522 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3523 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3524 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3525 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3526 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3527 if (int128_integer_type_node
3528 && (type1 == int128_integer_type_node
3529 || type1 == int128_unsigned_type_node))
3530 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3531 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3532 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3533 #if HOST_BITS_PER_WIDE_INT >= 64
3534 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3535 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3536 #endif
3537 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3538 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3539 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3540 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3541 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3542 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3543 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3544 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3546 #define C_COMMON_FIXED_TYPES(NAME) \
3547 if (type1 == short_ ## NAME ## _type_node \
3548 || type1 == unsigned_short_ ## NAME ## _type_node) \
3549 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3550 : short_ ## NAME ## _type_node; \
3551 if (type1 == NAME ## _type_node \
3552 || type1 == unsigned_ ## NAME ## _type_node) \
3553 return unsignedp ? unsigned_ ## NAME ## _type_node \
3554 : NAME ## _type_node; \
3555 if (type1 == long_ ## NAME ## _type_node \
3556 || type1 == unsigned_long_ ## NAME ## _type_node) \
3557 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3558 : long_ ## NAME ## _type_node; \
3559 if (type1 == long_long_ ## NAME ## _type_node \
3560 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3561 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3562 : long_long_ ## NAME ## _type_node;
3564 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3565 if (type1 == NAME ## _type_node \
3566 || type1 == u ## NAME ## _type_node) \
3567 return unsignedp ? u ## NAME ## _type_node \
3568 : NAME ## _type_node;
3570 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3571 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3572 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3573 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3574 : sat_ ## short_ ## NAME ## _type_node; \
3575 if (type1 == sat_ ## NAME ## _type_node \
3576 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3577 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3578 : sat_ ## NAME ## _type_node; \
3579 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3580 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3581 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3582 : sat_ ## long_ ## NAME ## _type_node; \
3583 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3584 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3585 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3586 : sat_ ## long_long_ ## NAME ## _type_node;
3588 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3589 if (type1 == sat_ ## NAME ## _type_node \
3590 || type1 == sat_ ## u ## NAME ## _type_node) \
3591 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3592 : sat_ ## NAME ## _type_node;
3594 C_COMMON_FIXED_TYPES (fract);
3595 C_COMMON_FIXED_TYPES_SAT (fract);
3596 C_COMMON_FIXED_TYPES (accum);
3597 C_COMMON_FIXED_TYPES_SAT (accum);
3599 C_COMMON_FIXED_MODE_TYPES (qq);
3600 C_COMMON_FIXED_MODE_TYPES (hq);
3601 C_COMMON_FIXED_MODE_TYPES (sq);
3602 C_COMMON_FIXED_MODE_TYPES (dq);
3603 C_COMMON_FIXED_MODE_TYPES (tq);
3604 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3605 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3606 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3607 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3608 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3609 C_COMMON_FIXED_MODE_TYPES (ha);
3610 C_COMMON_FIXED_MODE_TYPES (sa);
3611 C_COMMON_FIXED_MODE_TYPES (da);
3612 C_COMMON_FIXED_MODE_TYPES (ta);
3613 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3614 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3615 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3616 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3618 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3619 the precision; they have precision set to match their range, but
3620 may use a wider mode to match an ABI. If we change modes, we may
3621 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3622 the precision as well, so as to yield correct results for
3623 bit-field types. C++ does not have these separate bit-field
3624 types, and producing a signed or unsigned variant of an
3625 ENUMERAL_TYPE may cause other problems as well. */
3627 if (!INTEGRAL_TYPE_P (type)
3628 || TYPE_UNSIGNED (type) == unsignedp)
3629 return type;
3631 #define TYPE_OK(node) \
3632 (TYPE_MODE (type) == TYPE_MODE (node) \
3633 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3634 if (TYPE_OK (signed_char_type_node))
3635 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3636 if (TYPE_OK (integer_type_node))
3637 return unsignedp ? unsigned_type_node : integer_type_node;
3638 if (TYPE_OK (short_integer_type_node))
3639 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3640 if (TYPE_OK (long_integer_type_node))
3641 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3642 if (TYPE_OK (long_long_integer_type_node))
3643 return (unsignedp ? long_long_unsigned_type_node
3644 : long_long_integer_type_node);
3645 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3646 return (unsignedp ? int128_unsigned_type_node
3647 : int128_integer_type_node);
3648 if (TYPE_OK (widest_integer_literal_type_node))
3649 return (unsignedp ? widest_unsigned_literal_type_node
3650 : widest_integer_literal_type_node);
3652 #if HOST_BITS_PER_WIDE_INT >= 64
3653 if (TYPE_OK (intTI_type_node))
3654 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3655 #endif
3656 if (TYPE_OK (intDI_type_node))
3657 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3658 if (TYPE_OK (intSI_type_node))
3659 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3660 if (TYPE_OK (intHI_type_node))
3661 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3662 if (TYPE_OK (intQI_type_node))
3663 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3664 #undef TYPE_OK
3666 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3669 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3671 tree
3672 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3674 /* Extended integer types of the same width as a standard type have
3675 lesser rank, so those of the same width as int promote to int or
3676 unsigned int and are valid for printf formats expecting int or
3677 unsigned int. To avoid such special cases, avoid creating
3678 extended integer types for bit-fields if a standard integer type
3679 is available. */
3680 if (width == TYPE_PRECISION (integer_type_node))
3681 return unsignedp ? unsigned_type_node : integer_type_node;
3682 if (width == TYPE_PRECISION (signed_char_type_node))
3683 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3684 if (width == TYPE_PRECISION (short_integer_type_node))
3685 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3686 if (width == TYPE_PRECISION (long_integer_type_node))
3687 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3688 if (width == TYPE_PRECISION (long_long_integer_type_node))
3689 return (unsignedp ? long_long_unsigned_type_node
3690 : long_long_integer_type_node);
3691 if (int128_integer_type_node
3692 && width == TYPE_PRECISION (int128_integer_type_node))
3693 return (unsignedp ? int128_unsigned_type_node
3694 : int128_integer_type_node);
3695 return build_nonstandard_integer_type (width, unsignedp);
3698 /* The C version of the register_builtin_type langhook. */
3700 void
3701 c_register_builtin_type (tree type, const char* name)
3703 tree decl;
3705 decl = build_decl (UNKNOWN_LOCATION,
3706 TYPE_DECL, get_identifier (name), type);
3707 DECL_ARTIFICIAL (decl) = 1;
3708 if (!TYPE_NAME (type))
3709 TYPE_NAME (type) = decl;
3710 pushdecl (decl);
3712 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3715 /* Print an error message for invalid operands to arith operation
3716 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3717 LOCATION is the location of the message. */
3719 void
3720 binary_op_error (location_t location, enum tree_code code,
3721 tree type0, tree type1)
3723 const char *opname;
3725 switch (code)
3727 case PLUS_EXPR:
3728 opname = "+"; break;
3729 case MINUS_EXPR:
3730 opname = "-"; break;
3731 case MULT_EXPR:
3732 opname = "*"; break;
3733 case MAX_EXPR:
3734 opname = "max"; break;
3735 case MIN_EXPR:
3736 opname = "min"; break;
3737 case EQ_EXPR:
3738 opname = "=="; break;
3739 case NE_EXPR:
3740 opname = "!="; break;
3741 case LE_EXPR:
3742 opname = "<="; break;
3743 case GE_EXPR:
3744 opname = ">="; break;
3745 case LT_EXPR:
3746 opname = "<"; break;
3747 case GT_EXPR:
3748 opname = ">"; break;
3749 case LSHIFT_EXPR:
3750 opname = "<<"; break;
3751 case RSHIFT_EXPR:
3752 opname = ">>"; break;
3753 case TRUNC_MOD_EXPR:
3754 case FLOOR_MOD_EXPR:
3755 opname = "%"; break;
3756 case TRUNC_DIV_EXPR:
3757 case FLOOR_DIV_EXPR:
3758 opname = "/"; break;
3759 case BIT_AND_EXPR:
3760 opname = "&"; break;
3761 case BIT_IOR_EXPR:
3762 opname = "|"; break;
3763 case TRUTH_ANDIF_EXPR:
3764 opname = "&&"; break;
3765 case TRUTH_ORIF_EXPR:
3766 opname = "||"; break;
3767 case BIT_XOR_EXPR:
3768 opname = "^"; break;
3769 default:
3770 gcc_unreachable ();
3772 error_at (location,
3773 "invalid operands to binary %s (have %qT and %qT)", opname,
3774 type0, type1);
3777 /* Given an expression as a tree, return its original type. Do this
3778 by stripping any conversion that preserves the sign and precision. */
3779 static tree
3780 expr_original_type (tree expr)
3782 STRIP_SIGN_NOPS (expr);
3783 return TREE_TYPE (expr);
3786 /* Subroutine of build_binary_op, used for comparison operations.
3787 See if the operands have both been converted from subword integer types
3788 and, if so, perhaps change them both back to their original type.
3789 This function is also responsible for converting the two operands
3790 to the proper common type for comparison.
3792 The arguments of this function are all pointers to local variables
3793 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3794 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3796 If this function returns nonzero, it means that the comparison has
3797 a constant value. What this function returns is an expression for
3798 that value. */
3800 tree
3801 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3802 enum tree_code *rescode_ptr)
3804 tree type;
3805 tree op0 = *op0_ptr;
3806 tree op1 = *op1_ptr;
3807 int unsignedp0, unsignedp1;
3808 int real1, real2;
3809 tree primop0, primop1;
3810 enum tree_code code = *rescode_ptr;
3811 location_t loc = EXPR_LOC_OR_HERE (op0);
3813 /* Throw away any conversions to wider types
3814 already present in the operands. */
3816 primop0 = c_common_get_narrower (op0, &unsignedp0);
3817 primop1 = c_common_get_narrower (op1, &unsignedp1);
3819 /* If primopN is first sign-extended from primopN's precision to opN's
3820 precision, then zero-extended from opN's precision to
3821 *restype_ptr precision, shortenings might be invalid. */
3822 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3823 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3824 && !unsignedp0
3825 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3826 primop0 = op0;
3827 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3828 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3829 && !unsignedp1
3830 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3831 primop1 = op1;
3833 /* Handle the case that OP0 does not *contain* a conversion
3834 but it *requires* conversion to FINAL_TYPE. */
3836 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3837 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3838 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3839 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3841 /* If one of the operands must be floated, we cannot optimize. */
3842 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3843 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3845 /* If first arg is constant, swap the args (changing operation
3846 so value is preserved), for canonicalization. Don't do this if
3847 the second arg is 0. */
3849 if (TREE_CONSTANT (primop0)
3850 && !integer_zerop (primop1) && !real_zerop (primop1)
3851 && !fixed_zerop (primop1))
3853 tree tem = primop0;
3854 int temi = unsignedp0;
3855 primop0 = primop1;
3856 primop1 = tem;
3857 tem = op0;
3858 op0 = op1;
3859 op1 = tem;
3860 *op0_ptr = op0;
3861 *op1_ptr = op1;
3862 unsignedp0 = unsignedp1;
3863 unsignedp1 = temi;
3864 temi = real1;
3865 real1 = real2;
3866 real2 = temi;
3868 switch (code)
3870 case LT_EXPR:
3871 code = GT_EXPR;
3872 break;
3873 case GT_EXPR:
3874 code = LT_EXPR;
3875 break;
3876 case LE_EXPR:
3877 code = GE_EXPR;
3878 break;
3879 case GE_EXPR:
3880 code = LE_EXPR;
3881 break;
3882 default:
3883 break;
3885 *rescode_ptr = code;
3888 /* If comparing an integer against a constant more bits wide,
3889 maybe we can deduce a value of 1 or 0 independent of the data.
3890 Or else truncate the constant now
3891 rather than extend the variable at run time.
3893 This is only interesting if the constant is the wider arg.
3894 Also, it is not safe if the constant is unsigned and the
3895 variable arg is signed, since in this case the variable
3896 would be sign-extended and then regarded as unsigned.
3897 Our technique fails in this case because the lowest/highest
3898 possible unsigned results don't follow naturally from the
3899 lowest/highest possible values of the variable operand.
3900 For just EQ_EXPR and NE_EXPR there is another technique that
3901 could be used: see if the constant can be faithfully represented
3902 in the other operand's type, by truncating it and reextending it
3903 and see if that preserves the constant's value. */
3905 if (!real1 && !real2
3906 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3907 && TREE_CODE (primop1) == INTEGER_CST
3908 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3910 int min_gt, max_gt, min_lt, max_lt;
3911 tree maxval, minval;
3912 /* 1 if comparison is nominally unsigned. */
3913 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3914 tree val;
3916 type = c_common_signed_or_unsigned_type (unsignedp0,
3917 TREE_TYPE (primop0));
3919 maxval = TYPE_MAX_VALUE (type);
3920 minval = TYPE_MIN_VALUE (type);
3922 if (unsignedp && !unsignedp0)
3923 *restype_ptr = c_common_signed_type (*restype_ptr);
3925 if (TREE_TYPE (primop1) != *restype_ptr)
3927 /* Convert primop1 to target type, but do not introduce
3928 additional overflow. We know primop1 is an int_cst. */
3929 primop1 = force_fit_type_double (*restype_ptr,
3930 tree_to_double_int (primop1),
3931 0, TREE_OVERFLOW (primop1));
3933 if (type != *restype_ptr)
3935 minval = convert (*restype_ptr, minval);
3936 maxval = convert (*restype_ptr, maxval);
3939 if (unsignedp && unsignedp0)
3941 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3942 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3943 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3944 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3946 else
3948 min_gt = INT_CST_LT (primop1, minval);
3949 max_gt = INT_CST_LT (primop1, maxval);
3950 min_lt = INT_CST_LT (minval, primop1);
3951 max_lt = INT_CST_LT (maxval, primop1);
3954 val = 0;
3955 /* This used to be a switch, but Genix compiler can't handle that. */
3956 if (code == NE_EXPR)
3958 if (max_lt || min_gt)
3959 val = truthvalue_true_node;
3961 else if (code == EQ_EXPR)
3963 if (max_lt || min_gt)
3964 val = truthvalue_false_node;
3966 else if (code == LT_EXPR)
3968 if (max_lt)
3969 val = truthvalue_true_node;
3970 if (!min_lt)
3971 val = truthvalue_false_node;
3973 else if (code == GT_EXPR)
3975 if (min_gt)
3976 val = truthvalue_true_node;
3977 if (!max_gt)
3978 val = truthvalue_false_node;
3980 else if (code == LE_EXPR)
3982 if (!max_gt)
3983 val = truthvalue_true_node;
3984 if (min_gt)
3985 val = truthvalue_false_node;
3987 else if (code == GE_EXPR)
3989 if (!min_lt)
3990 val = truthvalue_true_node;
3991 if (max_lt)
3992 val = truthvalue_false_node;
3995 /* If primop0 was sign-extended and unsigned comparison specd,
3996 we did a signed comparison above using the signed type bounds.
3997 But the comparison we output must be unsigned.
3999 Also, for inequalities, VAL is no good; but if the signed
4000 comparison had *any* fixed result, it follows that the
4001 unsigned comparison just tests the sign in reverse
4002 (positive values are LE, negative ones GE).
4003 So we can generate an unsigned comparison
4004 against an extreme value of the signed type. */
4006 if (unsignedp && !unsignedp0)
4008 if (val != 0)
4009 switch (code)
4011 case LT_EXPR:
4012 case GE_EXPR:
4013 primop1 = TYPE_MIN_VALUE (type);
4014 val = 0;
4015 break;
4017 case LE_EXPR:
4018 case GT_EXPR:
4019 primop1 = TYPE_MAX_VALUE (type);
4020 val = 0;
4021 break;
4023 default:
4024 break;
4026 type = c_common_unsigned_type (type);
4029 if (TREE_CODE (primop0) != INTEGER_CST
4030 && c_inhibit_evaluation_warnings == 0)
4032 if (val == truthvalue_false_node)
4033 warning_at (loc, OPT_Wtype_limits,
4034 "comparison is always false due to limited range of data type");
4035 if (val == truthvalue_true_node)
4036 warning_at (loc, OPT_Wtype_limits,
4037 "comparison is always true due to limited range of data type");
4040 if (val != 0)
4042 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4043 if (TREE_SIDE_EFFECTS (primop0))
4044 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4045 return val;
4048 /* Value is not predetermined, but do the comparison
4049 in the type of the operand that is not constant.
4050 TYPE is already properly set. */
4053 /* If either arg is decimal float and the other is float, find the
4054 proper common type to use for comparison. */
4055 else if (real1 && real2
4056 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4057 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4058 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4060 else if (real1 && real2
4061 && (TYPE_PRECISION (TREE_TYPE (primop0))
4062 == TYPE_PRECISION (TREE_TYPE (primop1))))
4063 type = TREE_TYPE (primop0);
4065 /* If args' natural types are both narrower than nominal type
4066 and both extend in the same manner, compare them
4067 in the type of the wider arg.
4068 Otherwise must actually extend both to the nominal
4069 common type lest different ways of extending
4070 alter the result.
4071 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4073 else if (unsignedp0 == unsignedp1 && real1 == real2
4074 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4075 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4077 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4078 type = c_common_signed_or_unsigned_type (unsignedp0
4079 || TYPE_UNSIGNED (*restype_ptr),
4080 type);
4081 /* Make sure shorter operand is extended the right way
4082 to match the longer operand. */
4083 primop0
4084 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4085 TREE_TYPE (primop0)),
4086 primop0);
4087 primop1
4088 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4089 TREE_TYPE (primop1)),
4090 primop1);
4092 else
4094 /* Here we must do the comparison on the nominal type
4095 using the args exactly as we received them. */
4096 type = *restype_ptr;
4097 primop0 = op0;
4098 primop1 = op1;
4100 if (!real1 && !real2 && integer_zerop (primop1)
4101 && TYPE_UNSIGNED (*restype_ptr))
4103 tree value = 0;
4104 /* All unsigned values are >= 0, so we warn. However,
4105 if OP0 is a constant that is >= 0, the signedness of
4106 the comparison isn't an issue, so suppress the
4107 warning. */
4108 bool warn =
4109 warn_type_limits && !in_system_header
4110 && c_inhibit_evaluation_warnings == 0
4111 && !(TREE_CODE (primop0) == INTEGER_CST
4112 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4113 primop0)))
4114 /* Do not warn for enumeration types. */
4115 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4117 switch (code)
4119 case GE_EXPR:
4120 if (warn)
4121 warning_at (loc, OPT_Wtype_limits,
4122 "comparison of unsigned expression >= 0 is always true");
4123 value = truthvalue_true_node;
4124 break;
4126 case LT_EXPR:
4127 if (warn)
4128 warning_at (loc, OPT_Wtype_limits,
4129 "comparison of unsigned expression < 0 is always false");
4130 value = truthvalue_false_node;
4131 break;
4133 default:
4134 break;
4137 if (value != 0)
4139 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4140 if (TREE_SIDE_EFFECTS (primop0))
4141 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4142 primop0, value);
4143 return value;
4148 *op0_ptr = convert (type, primop0);
4149 *op1_ptr = convert (type, primop1);
4151 *restype_ptr = truthvalue_type_node;
4153 return 0;
4156 /* Return a tree for the sum or difference (RESULTCODE says which)
4157 of pointer PTROP and integer INTOP. */
4159 tree
4160 pointer_int_sum (location_t loc, enum tree_code resultcode,
4161 tree ptrop, tree intop)
4163 tree size_exp, ret;
4165 /* The result is a pointer of the same type that is being added. */
4166 tree result_type = TREE_TYPE (ptrop);
4168 /* If the pointer lives in UPC shared memory, then
4169 drop the 'shared' qualifier. */
4170 if (TREE_SHARED (ptrop) || upc_shared_type_p (result_type))
4171 result_type = build_upc_unshared_type (result_type);
4173 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4175 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4176 "pointer of type %<void *%> used in arithmetic");
4177 size_exp = integer_one_node;
4179 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4181 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4182 "pointer to a function used in arithmetic");
4183 size_exp = integer_one_node;
4185 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4187 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4188 "pointer to member function used in arithmetic");
4189 size_exp = integer_one_node;
4191 else
4192 size_exp = size_in_bytes (TREE_TYPE (result_type));
4194 /* We are manipulating pointer values, so we don't need to warn
4195 about relying on undefined signed overflow. We disable the
4196 warning here because we use integer types so fold won't know that
4197 they are really pointers. */
4198 fold_defer_overflow_warnings ();
4200 /* If what we are about to multiply by the size of the elements
4201 contains a constant term, apply distributive law
4202 and multiply that constant term separately.
4203 This helps produce common subexpressions. */
4204 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4205 && !TREE_CONSTANT (intop)
4206 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4207 && TREE_CONSTANT (size_exp)
4208 /* If the constant comes from pointer subtraction,
4209 skip this optimization--it would cause an error. */
4210 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4211 /* If the constant is unsigned, and smaller than the pointer size,
4212 then we must skip this optimization. This is because it could cause
4213 an overflow error if the constant is negative but INTOP is not. */
4214 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4215 || (TYPE_PRECISION (TREE_TYPE (intop))
4216 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4218 enum tree_code subcode = resultcode;
4219 tree int_type = TREE_TYPE (intop);
4220 if (TREE_CODE (intop) == MINUS_EXPR)
4221 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4222 /* Convert both subexpression types to the type of intop,
4223 because weird cases involving pointer arithmetic
4224 can result in a sum or difference with different type args. */
4225 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4226 subcode, ptrop,
4227 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4228 intop = convert (int_type, TREE_OPERAND (intop, 0));
4231 /* Convert the integer argument to a type the same size as sizetype
4232 so the multiply won't overflow spuriously. */
4233 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4234 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4235 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4236 TYPE_UNSIGNED (sizetype)), intop);
4238 /* Replace the integer argument with a suitable product by the object size.
4239 Do this multiplication as signed, then convert to the appropriate type
4240 for the pointer operation and disregard an overflow that occurred only
4241 because of the sign-extension change in the latter conversion. */
4243 tree t = build_binary_op (loc,
4244 MULT_EXPR, intop,
4245 convert (TREE_TYPE (intop), size_exp), 1);
4246 intop = convert (sizetype, t);
4247 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4248 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4249 TREE_INT_CST_HIGH (intop));
4252 /* Create the sum or difference. */
4253 if (resultcode == MINUS_EXPR)
4254 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4256 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4258 fold_undefer_and_ignore_overflow_warnings ();
4260 return ret;
4263 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4264 and if NON_CONST is known not to be permitted in an evaluated part
4265 of a constant expression. */
4267 tree
4268 c_wrap_maybe_const (tree expr, bool non_const)
4270 bool nowarning = TREE_NO_WARNING (expr);
4271 location_t loc = EXPR_LOCATION (expr);
4273 /* This should never be called for C++. */
4274 if (c_dialect_cxx ())
4275 gcc_unreachable ();
4277 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4278 STRIP_TYPE_NOPS (expr);
4279 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4280 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4281 if (nowarning)
4282 TREE_NO_WARNING (expr) = 1;
4283 protected_set_expr_location (expr, loc);
4285 return expr;
4288 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4289 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4290 around the SAVE_EXPR if needed so that c_fully_fold does not need
4291 to look inside SAVE_EXPRs. */
4293 tree
4294 c_save_expr (tree expr)
4296 bool maybe_const = true;
4297 if (c_dialect_cxx ())
4298 return save_expr (expr);
4299 expr = c_fully_fold (expr, false, &maybe_const);
4300 expr = save_expr (expr);
4301 if (!maybe_const)
4302 expr = c_wrap_maybe_const (expr, true);
4303 return expr;
4306 /* Return whether EXPR is a declaration whose address can never be
4307 NULL. */
4309 bool
4310 decl_with_nonnull_addr_p (const_tree expr)
4312 return (DECL_P (expr)
4313 && (TREE_CODE (expr) == PARM_DECL
4314 || TREE_CODE (expr) == LABEL_DECL
4315 || !DECL_WEAK (expr)));
4318 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4319 or for an `if' or `while' statement or ?..: exp. It should already
4320 have been validated to be of suitable type; otherwise, a bad
4321 diagnostic may result.
4323 The EXPR is located at LOCATION.
4325 This preparation consists of taking the ordinary
4326 representation of an expression expr and producing a valid tree
4327 boolean expression describing whether expr is nonzero. We could
4328 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4329 but we optimize comparisons, &&, ||, and !.
4331 The resulting type should always be `truthvalue_type_node'. */
4333 tree
4334 c_common_truthvalue_conversion (location_t location, tree expr)
4336 switch (TREE_CODE (expr))
4338 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4339 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4340 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4341 case ORDERED_EXPR: case UNORDERED_EXPR:
4342 if (TREE_TYPE (expr) == truthvalue_type_node)
4343 return expr;
4344 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4345 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4346 goto ret;
4348 case TRUTH_ANDIF_EXPR:
4349 case TRUTH_ORIF_EXPR:
4350 case TRUTH_AND_EXPR:
4351 case TRUTH_OR_EXPR:
4352 case TRUTH_XOR_EXPR:
4353 if (TREE_TYPE (expr) == truthvalue_type_node)
4354 return expr;
4355 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4356 c_common_truthvalue_conversion (location,
4357 TREE_OPERAND (expr, 0)),
4358 c_common_truthvalue_conversion (location,
4359 TREE_OPERAND (expr, 1)));
4360 goto ret;
4362 case TRUTH_NOT_EXPR:
4363 if (TREE_TYPE (expr) == truthvalue_type_node)
4364 return expr;
4365 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4366 c_common_truthvalue_conversion (location,
4367 TREE_OPERAND (expr, 0)));
4368 goto ret;
4370 case ERROR_MARK:
4371 return expr;
4373 case INTEGER_CST:
4374 return integer_zerop (expr) ? truthvalue_false_node
4375 : truthvalue_true_node;
4377 case REAL_CST:
4378 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4379 ? truthvalue_true_node
4380 : truthvalue_false_node;
4382 case FIXED_CST:
4383 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4384 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4385 ? truthvalue_true_node
4386 : truthvalue_false_node;
4388 case FUNCTION_DECL:
4389 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4390 /* Fall through. */
4392 case ADDR_EXPR:
4394 tree inner = TREE_OPERAND (expr, 0);
4395 if (decl_with_nonnull_addr_p (inner))
4397 /* Common Ada/Pascal programmer's mistake. */
4398 warning_at (location,
4399 OPT_Waddress,
4400 "the address of %qD will always evaluate as %<true%>",
4401 inner);
4402 return truthvalue_true_node;
4404 break;
4407 case COMPLEX_EXPR:
4408 expr = build_binary_op (EXPR_LOCATION (expr),
4409 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4410 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4411 c_common_truthvalue_conversion (location,
4412 TREE_OPERAND (expr, 0)),
4413 c_common_truthvalue_conversion (location,
4414 TREE_OPERAND (expr, 1)),
4416 goto ret;
4418 case NEGATE_EXPR:
4419 case ABS_EXPR:
4420 case FLOAT_EXPR:
4421 case EXCESS_PRECISION_EXPR:
4422 /* These don't change whether an object is nonzero or zero. */
4423 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4425 case LROTATE_EXPR:
4426 case RROTATE_EXPR:
4427 /* These don't change whether an object is zero or nonzero, but
4428 we can't ignore them if their second arg has side-effects. */
4429 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4431 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4432 TREE_OPERAND (expr, 1),
4433 c_common_truthvalue_conversion
4434 (location, TREE_OPERAND (expr, 0)));
4435 goto ret;
4437 else
4438 return c_common_truthvalue_conversion (location,
4439 TREE_OPERAND (expr, 0));
4441 case COND_EXPR:
4442 /* Distribute the conversion into the arms of a COND_EXPR. */
4443 if (c_dialect_cxx ())
4445 tree op1 = TREE_OPERAND (expr, 1);
4446 tree op2 = TREE_OPERAND (expr, 2);
4447 /* In C++ one of the arms might have void type if it is throw. */
4448 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4449 op1 = c_common_truthvalue_conversion (location, op1);
4450 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4451 op2 = c_common_truthvalue_conversion (location, op2);
4452 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4453 TREE_OPERAND (expr, 0), op1, op2);
4454 goto ret;
4456 else
4458 /* Folding will happen later for C. */
4459 expr = build3 (COND_EXPR, truthvalue_type_node,
4460 TREE_OPERAND (expr, 0),
4461 c_common_truthvalue_conversion (location,
4462 TREE_OPERAND (expr, 1)),
4463 c_common_truthvalue_conversion (location,
4464 TREE_OPERAND (expr, 2)));
4465 goto ret;
4468 CASE_CONVERT:
4470 tree totype = TREE_TYPE (expr);
4471 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4473 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4474 since that affects how `default_conversion' will behave. */
4475 if (TREE_CODE (totype) == REFERENCE_TYPE
4476 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4477 break;
4478 /* Don't strip a conversion from C++0x scoped enum, since they
4479 don't implicitly convert to other types. */
4480 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4481 && ENUM_IS_SCOPED (fromtype))
4482 break;
4483 /* If this isn't narrowing the argument, we can ignore it. */
4484 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4485 return c_common_truthvalue_conversion (location,
4486 TREE_OPERAND (expr, 0));
4488 break;
4490 case MODIFY_EXPR:
4491 if (!TREE_NO_WARNING (expr)
4492 && warn_parentheses)
4494 warning (OPT_Wparentheses,
4495 "suggest parentheses around assignment used as truth value");
4496 TREE_NO_WARNING (expr) = 1;
4498 break;
4500 default:
4501 break;
4504 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4506 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4507 expr = (build_binary_op
4508 (EXPR_LOCATION (expr),
4509 (TREE_SIDE_EFFECTS (expr)
4510 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4511 c_common_truthvalue_conversion
4512 (location,
4513 build_unary_op (location, REALPART_EXPR, t, 0)),
4514 c_common_truthvalue_conversion
4515 (location,
4516 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4517 0));
4518 goto ret;
4521 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4523 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4524 FCONST0 (TYPE_MODE
4525 (TREE_TYPE (expr))));
4526 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4528 else
4529 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4531 ret:
4532 protected_set_expr_location (expr, location);
4533 return expr;
4536 static void def_builtin_1 (enum built_in_function fncode,
4537 const char *name,
4538 enum built_in_class fnclass,
4539 tree fntype, tree libtype,
4540 bool both_p, bool fallback_p, bool nonansi_p,
4541 tree fnattrs, bool implicit_p);
4544 /* Apply the TYPE_QUALS to the new DECL. */
4546 void
4547 c_apply_type_quals_to_decl (int type_quals, tree decl)
4549 tree type = TREE_TYPE (decl);
4551 if (type == error_mark_node)
4552 return;
4554 if ((type_quals & TYPE_QUAL_CONST)
4555 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4556 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4557 constructor can produce constant init, so rely on cp_finish_decl to
4558 clear TREE_READONLY if the variable has non-constant init. */
4559 TREE_READONLY (decl) = 1;
4560 if (type_quals & TYPE_QUAL_VOLATILE)
4562 TREE_SIDE_EFFECTS (decl) = 1;
4563 TREE_THIS_VOLATILE (decl) = 1;
4565 if (type_quals & TYPE_QUAL_RESTRICT)
4567 while (type && TREE_CODE (type) == ARRAY_TYPE)
4568 /* Allow 'restrict' on arrays of pointers.
4569 FIXME currently we just ignore it. */
4570 type = TREE_TYPE (type);
4571 if (!type
4572 || !POINTER_TYPE_P (type)
4573 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4574 error ("invalid use of %<restrict%>");
4576 if (type_quals & TYPE_QUAL_SHARED)
4578 TREE_SHARED (decl) = 1;
4579 if (type_quals & TYPE_QUAL_STRICT)
4580 TREE_STRICT(decl) = 1;
4581 else if (type_quals & TYPE_QUAL_RELAXED)
4582 TREE_RELAXED(decl) = 1;
4583 /* The declaration's type should have been previously defined
4584 as a UPC shared type. */
4585 gcc_assert (upc_shared_type_p (type));
4589 /* Hash function for the problem of multiple type definitions in
4590 different files. This must hash all types that will compare
4591 equal via comptypes to the same value. In practice it hashes
4592 on some of the simple stuff and leaves the details to comptypes. */
4594 static hashval_t
4595 c_type_hash (const void *p)
4597 int n_elements;
4598 int shift, size;
4599 const_tree const t = (const_tree) p;
4600 tree t2;
4601 switch (TREE_CODE (t))
4603 /* For pointers, hash on pointee type plus some swizzling. */
4604 case POINTER_TYPE:
4605 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4606 /* Hash on number of elements and total size. */
4607 case ENUMERAL_TYPE:
4608 shift = 3;
4609 t2 = TYPE_VALUES (t);
4610 break;
4611 case RECORD_TYPE:
4612 shift = 0;
4613 t2 = TYPE_FIELDS (t);
4614 break;
4615 case QUAL_UNION_TYPE:
4616 shift = 1;
4617 t2 = TYPE_FIELDS (t);
4618 break;
4619 case UNION_TYPE:
4620 shift = 2;
4621 t2 = TYPE_FIELDS (t);
4622 break;
4623 default:
4624 gcc_unreachable ();
4626 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4627 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4628 n_elements = list_length (t2);
4629 /* We might have a VLA here. */
4630 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4631 size = 0;
4632 else
4633 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4634 return ((size << 24) | (n_elements << shift));
4637 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4639 /* Return the typed-based alias set for T, which may be an expression
4640 or a type. Return -1 if we don't do anything special. */
4642 alias_set_type
4643 c_common_get_alias_set (tree t)
4645 tree u;
4646 PTR *slot;
4648 /* For VLAs, use the alias set of the element type rather than the
4649 default of alias set 0 for types compared structurally. */
4650 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4652 if (TREE_CODE (t) == ARRAY_TYPE)
4653 return get_alias_set (TREE_TYPE (t));
4654 return -1;
4657 /* Permit type-punning when accessing a union, provided the access
4658 is directly through the union. For example, this code does not
4659 permit taking the address of a union member and then storing
4660 through it. Even the type-punning allowed here is a GCC
4661 extension, albeit a common and useful one; the C standard says
4662 that such accesses have implementation-defined behavior. */
4663 for (u = t;
4664 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4665 u = TREE_OPERAND (u, 0))
4666 if (TREE_CODE (u) == COMPONENT_REF
4667 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4668 return 0;
4670 /* That's all the expressions we handle specially. */
4671 if (!TYPE_P (t))
4672 return -1;
4674 /* The C standard guarantees that any object may be accessed via an
4675 lvalue that has character type. */
4676 if (t == char_type_node
4677 || t == signed_char_type_node
4678 || t == unsigned_char_type_node)
4679 return 0;
4681 /* The C standard specifically allows aliasing between signed and
4682 unsigned variants of the same type. We treat the signed
4683 variant as canonical. */
4684 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4686 tree t1 = c_common_signed_type (t);
4688 /* t1 == t can happen for boolean nodes which are always unsigned. */
4689 if (t1 != t)
4690 return get_alias_set (t1);
4693 /* Handle the case of multiple type nodes referring to "the same" type,
4694 which occurs with IMA. These share an alias set. FIXME: Currently only
4695 C90 is handled. (In C99 type compatibility is not transitive, which
4696 complicates things mightily. The alias set splay trees can theoretically
4697 represent this, but insertion is tricky when you consider all the
4698 different orders things might arrive in.) */
4700 if (c_language != clk_c || flag_isoc99)
4701 return -1;
4703 /* Save time if there's only one input file. */
4704 if (num_in_fnames == 1)
4705 return -1;
4707 /* Pointers need special handling if they point to any type that
4708 needs special handling (below). */
4709 if (TREE_CODE (t) == POINTER_TYPE)
4711 tree t2;
4712 /* Find bottom type under any nested POINTERs. */
4713 for (t2 = TREE_TYPE (t);
4714 TREE_CODE (t2) == POINTER_TYPE;
4715 t2 = TREE_TYPE (t2))
4717 if (TREE_CODE (t2) != RECORD_TYPE
4718 && TREE_CODE (t2) != ENUMERAL_TYPE
4719 && TREE_CODE (t2) != QUAL_UNION_TYPE
4720 && TREE_CODE (t2) != UNION_TYPE)
4721 return -1;
4722 if (TYPE_SIZE (t2) == 0)
4723 return -1;
4725 /* These are the only cases that need special handling. */
4726 if (TREE_CODE (t) != RECORD_TYPE
4727 && TREE_CODE (t) != ENUMERAL_TYPE
4728 && TREE_CODE (t) != QUAL_UNION_TYPE
4729 && TREE_CODE (t) != UNION_TYPE
4730 && TREE_CODE (t) != POINTER_TYPE)
4731 return -1;
4732 /* Undefined? */
4733 if (TYPE_SIZE (t) == 0)
4734 return -1;
4736 /* Look up t in hash table. Only one of the compatible types within each
4737 alias set is recorded in the table. */
4738 if (!type_hash_table)
4739 type_hash_table = htab_create_ggc (1021, c_type_hash,
4740 (htab_eq) lang_hooks.types_compatible_p,
4741 NULL);
4742 slot = htab_find_slot (type_hash_table, t, INSERT);
4743 if (*slot != NULL)
4745 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4746 return TYPE_ALIAS_SET ((tree)*slot);
4748 else
4749 /* Our caller will assign and record (in t) a new alias set; all we need
4750 to do is remember t in the hash table. */
4751 *slot = t;
4753 return -1;
4756 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4757 the second parameter indicates which OPERATOR is being applied.
4758 The COMPLAIN flag controls whether we should diagnose possibly
4759 ill-formed constructs or not. LOC is the location of the SIZEOF or
4760 TYPEOF operator. */
4762 tree
4763 c_sizeof_or_alignof_type (location_t loc,
4764 tree type, bool is_sizeof, int complain)
4766 const char *op_name;
4767 tree value = NULL;
4768 enum tree_code type_code = TREE_CODE (type);
4770 op_name = is_sizeof ? "sizeof" : "__alignof__";
4772 if (type_code == FUNCTION_TYPE)
4774 if (is_sizeof)
4776 if (complain && (pedantic || warn_pointer_arith))
4777 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4778 "invalid application of %<sizeof%> to a function type");
4779 else if (!complain)
4780 return error_mark_node;
4781 value = size_one_node;
4783 else
4785 if (complain)
4787 if (c_dialect_cxx ())
4788 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4789 "%<alignof%> applied to a function type");
4790 else
4791 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4792 "%<_Alignof%> applied to a function type");
4794 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4797 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4799 if (complain)
4801 if (type_code == VOID_TYPE && upc_shared_type_p (type))
4802 error_at (loc, "invalid application of %qs"
4803 " to %<shared void%> type", op_name);
4804 else if (type_code == VOID_TYPE
4805 && (pedantic || warn_pointer_arith))
4806 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4807 "invalid application of %qs to a void type", op_name);
4809 else
4810 return error_mark_node;
4811 value = size_one_node;
4813 else if (!COMPLETE_TYPE_P (type)
4814 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4816 if (complain)
4817 error_at (loc, "invalid application of %qs to incomplete type %qT",
4818 op_name, type);
4819 return error_mark_node;
4821 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4822 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4824 if (complain)
4825 error_at (loc, "invalid application of %qs to array type %qT of "
4826 "incomplete element type", op_name, type);
4827 return error_mark_node;
4829 else
4831 if (is_sizeof)
4832 /* Convert in case a char is more than one unit. */
4833 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4834 size_int (TYPE_PRECISION (char_type_node)
4835 / BITS_PER_UNIT));
4836 else
4837 value = size_int (TYPE_ALIGN_UNIT (type));
4840 if (is_sizeof && (TREE_CODE (type) == ARRAY_TYPE)
4841 && upc_shared_type_p (type)
4842 && TYPE_HAS_THREADS_FACTOR (type))
4844 const tree n_threads = convert (sizetype, upc_num_threads ());
4845 value = size_binop (MULT_EXPR, value, n_threads);
4848 /* VALUE will have the middle-end integer type sizetype.
4849 However, we should really return a value of type `size_t',
4850 which is just a typedef for an ordinary integer type. */
4851 value = fold_convert_loc (loc, size_type_node, value);
4853 return value;
4856 /* Implement the __alignof keyword: Return the minimum required
4857 alignment of EXPR, measured in bytes. For VAR_DECLs,
4858 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4859 from an "aligned" __attribute__ specification). LOC is the
4860 location of the ALIGNOF operator. */
4862 tree
4863 c_alignof_expr (location_t loc, tree expr)
4865 tree t;
4867 if (VAR_OR_FUNCTION_DECL_P (expr))
4868 t = size_int (DECL_ALIGN_UNIT (expr));
4870 else if (TREE_CODE (expr) == COMPONENT_REF
4871 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4873 error_at (loc, "%<__alignof%> applied to a bit-field");
4874 t = size_one_node;
4876 else if (TREE_CODE (expr) == COMPONENT_REF
4877 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4878 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4880 else if (TREE_CODE (expr) == INDIRECT_REF)
4882 tree t = TREE_OPERAND (expr, 0);
4883 tree best = t;
4884 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4886 while (CONVERT_EXPR_P (t)
4887 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4889 int thisalign;
4891 t = TREE_OPERAND (t, 0);
4892 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4893 if (thisalign > bestalign)
4894 best = t, bestalign = thisalign;
4896 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4898 else
4899 return c_alignof (loc, TREE_TYPE (expr));
4901 return fold_convert_loc (loc, size_type_node, t);
4904 /* Handle C and C++ default attributes. */
4906 enum built_in_attribute
4908 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4909 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4910 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4911 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4912 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4913 #include "builtin-attrs.def"
4914 #undef DEF_ATTR_NULL_TREE
4915 #undef DEF_ATTR_INT
4916 #undef DEF_ATTR_STRING
4917 #undef DEF_ATTR_IDENT
4918 #undef DEF_ATTR_TREE_LIST
4919 ATTR_LAST
4922 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4924 static void c_init_attributes (void);
4926 enum c_builtin_type
4928 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4929 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4930 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4931 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4932 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4933 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4934 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4935 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4936 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4937 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4938 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4939 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4940 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4941 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4942 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4943 NAME,
4944 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4945 #include "builtin-types.def"
4946 #undef DEF_PRIMITIVE_TYPE
4947 #undef DEF_FUNCTION_TYPE_0
4948 #undef DEF_FUNCTION_TYPE_1
4949 #undef DEF_FUNCTION_TYPE_2
4950 #undef DEF_FUNCTION_TYPE_3
4951 #undef DEF_FUNCTION_TYPE_4
4952 #undef DEF_FUNCTION_TYPE_5
4953 #undef DEF_FUNCTION_TYPE_6
4954 #undef DEF_FUNCTION_TYPE_7
4955 #undef DEF_FUNCTION_TYPE_VAR_0
4956 #undef DEF_FUNCTION_TYPE_VAR_1
4957 #undef DEF_FUNCTION_TYPE_VAR_2
4958 #undef DEF_FUNCTION_TYPE_VAR_3
4959 #undef DEF_FUNCTION_TYPE_VAR_4
4960 #undef DEF_FUNCTION_TYPE_VAR_5
4961 #undef DEF_POINTER_TYPE
4962 BT_LAST
4965 typedef enum c_builtin_type builtin_type;
4967 /* A temporary array for c_common_nodes_and_builtins. Used in
4968 communication with def_fn_type. */
4969 static tree builtin_types[(int) BT_LAST + 1];
4971 /* A helper function for c_common_nodes_and_builtins. Build function type
4972 for DEF with return type RET and N arguments. If VAR is true, then the
4973 function should be variadic after those N arguments.
4975 Takes special care not to ICE if any of the types involved are
4976 error_mark_node, which indicates that said type is not in fact available
4977 (see builtin_type_for_size). In which case the function type as a whole
4978 should be error_mark_node. */
4980 static void
4981 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4983 tree t;
4984 tree *args = XALLOCAVEC (tree, n);
4985 va_list list;
4986 int i;
4988 va_start (list, n);
4989 for (i = 0; i < n; ++i)
4991 builtin_type a = (builtin_type) va_arg (list, int);
4992 t = builtin_types[a];
4993 if (t == error_mark_node)
4994 goto egress;
4995 args[i] = t;
4998 t = builtin_types[ret];
4999 if (t == error_mark_node)
5000 goto egress;
5001 if (var)
5002 t = build_varargs_function_type_array (t, n, args);
5003 else
5004 t = build_function_type_array (t, n, args);
5006 egress:
5007 builtin_types[def] = t;
5008 va_end (list);
5011 /* Build builtin functions common to both C and C++ language
5012 frontends. */
5014 static void
5015 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5017 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5018 builtin_types[ENUM] = VALUE;
5019 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5020 def_fn_type (ENUM, RETURN, 0, 0);
5021 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5022 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5023 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5024 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5025 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5026 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5027 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5028 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5029 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5030 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5031 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5032 ARG6) \
5033 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5034 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5035 ARG6, ARG7) \
5036 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5037 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5038 def_fn_type (ENUM, RETURN, 1, 0);
5039 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5040 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5041 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5042 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5043 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5044 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5045 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5046 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5047 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5048 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5049 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5050 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5052 #include "builtin-types.def"
5054 #undef DEF_PRIMITIVE_TYPE
5055 #undef DEF_FUNCTION_TYPE_1
5056 #undef DEF_FUNCTION_TYPE_2
5057 #undef DEF_FUNCTION_TYPE_3
5058 #undef DEF_FUNCTION_TYPE_4
5059 #undef DEF_FUNCTION_TYPE_5
5060 #undef DEF_FUNCTION_TYPE_6
5061 #undef DEF_FUNCTION_TYPE_VAR_0
5062 #undef DEF_FUNCTION_TYPE_VAR_1
5063 #undef DEF_FUNCTION_TYPE_VAR_2
5064 #undef DEF_FUNCTION_TYPE_VAR_3
5065 #undef DEF_FUNCTION_TYPE_VAR_4
5066 #undef DEF_FUNCTION_TYPE_VAR_5
5067 #undef DEF_POINTER_TYPE
5068 builtin_types[(int) BT_LAST] = NULL_TREE;
5070 c_init_attributes ();
5072 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5073 NONANSI_P, ATTRS, IMPLICIT, COND) \
5074 if (NAME && COND) \
5075 def_builtin_1 (ENUM, NAME, CLASS, \
5076 builtin_types[(int) TYPE], \
5077 builtin_types[(int) LIBTYPE], \
5078 BOTH_P, FALLBACK_P, NONANSI_P, \
5079 built_in_attributes[(int) ATTRS], IMPLICIT);
5080 #include "builtins.def"
5081 #undef DEF_BUILTIN
5083 targetm.init_builtins ();
5085 build_common_builtin_nodes ();
5087 if (flag_mudflap)
5088 mudflap_init ();
5091 /* Like get_identifier, but avoid warnings about null arguments when
5092 the argument may be NULL for targets where GCC lacks stdint.h type
5093 information. */
5095 static inline tree
5096 c_get_ident (const char *id)
5098 return get_identifier (id);
5101 /* Build tree nodes and builtin functions common to both C and C++ language
5102 frontends. */
5104 void
5105 c_common_nodes_and_builtins (void)
5107 int char16_type_size;
5108 int char32_type_size;
5109 int wchar_type_size;
5110 tree array_domain_type;
5111 tree va_list_ref_type_node;
5112 tree va_list_arg_type_node;
5114 build_common_tree_nodes (flag_signed_char, flag_short_double);
5116 /* Define `int' and `char' first so that dbx will output them first. */
5117 record_builtin_type (RID_INT, NULL, integer_type_node);
5118 record_builtin_type (RID_CHAR, "char", char_type_node);
5120 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5121 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5122 but not C. Are the conditionals here needed? */
5123 if (c_dialect_cxx ())
5124 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5125 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5126 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5127 record_builtin_type (RID_MAX, "long unsigned int",
5128 long_unsigned_type_node);
5129 if (int128_integer_type_node != NULL_TREE)
5131 record_builtin_type (RID_INT128, "__int128",
5132 int128_integer_type_node);
5133 record_builtin_type (RID_MAX, "__int128 unsigned",
5134 int128_unsigned_type_node);
5136 if (c_dialect_cxx ())
5137 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5138 record_builtin_type (RID_MAX, "long long int",
5139 long_long_integer_type_node);
5140 record_builtin_type (RID_MAX, "long long unsigned int",
5141 long_long_unsigned_type_node);
5142 if (c_dialect_cxx ())
5143 record_builtin_type (RID_MAX, "long long unsigned",
5144 long_long_unsigned_type_node);
5145 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5146 record_builtin_type (RID_MAX, "short unsigned int",
5147 short_unsigned_type_node);
5148 if (c_dialect_cxx ())
5149 record_builtin_type (RID_MAX, "unsigned short",
5150 short_unsigned_type_node);
5152 /* Define both `signed char' and `unsigned char'. */
5153 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5154 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5156 /* These are types that c_common_type_for_size and
5157 c_common_type_for_mode use. */
5158 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5159 TYPE_DECL, NULL_TREE,
5160 intQI_type_node));
5161 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5162 TYPE_DECL, NULL_TREE,
5163 intHI_type_node));
5164 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5165 TYPE_DECL, NULL_TREE,
5166 intSI_type_node));
5167 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5168 TYPE_DECL, NULL_TREE,
5169 intDI_type_node));
5170 #if HOST_BITS_PER_WIDE_INT >= 64
5171 if (targetm.scalar_mode_supported_p (TImode))
5172 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5173 TYPE_DECL,
5174 get_identifier ("__int128_t"),
5175 intTI_type_node));
5176 #endif
5177 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5178 TYPE_DECL, NULL_TREE,
5179 unsigned_intQI_type_node));
5180 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5181 TYPE_DECL, NULL_TREE,
5182 unsigned_intHI_type_node));
5183 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5184 TYPE_DECL, NULL_TREE,
5185 unsigned_intSI_type_node));
5186 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5187 TYPE_DECL, NULL_TREE,
5188 unsigned_intDI_type_node));
5189 #if HOST_BITS_PER_WIDE_INT >= 64
5190 if (targetm.scalar_mode_supported_p (TImode))
5191 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5192 TYPE_DECL,
5193 get_identifier ("__uint128_t"),
5194 unsigned_intTI_type_node));
5195 #endif
5197 /* Create the widest literal types. */
5198 widest_integer_literal_type_node
5199 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5200 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5201 TYPE_DECL, NULL_TREE,
5202 widest_integer_literal_type_node));
5204 widest_unsigned_literal_type_node
5205 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5206 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5207 TYPE_DECL, NULL_TREE,
5208 widest_unsigned_literal_type_node));
5210 signed_size_type_node = c_common_signed_type (size_type_node);
5212 pid_type_node =
5213 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5215 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5216 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5217 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5219 /* Only supported decimal floating point extension if the target
5220 actually supports underlying modes. */
5221 if (targetm.scalar_mode_supported_p (SDmode)
5222 && targetm.scalar_mode_supported_p (DDmode)
5223 && targetm.scalar_mode_supported_p (TDmode))
5225 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5226 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5227 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5230 if (targetm.fixed_point_supported_p ())
5232 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5233 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5234 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5235 record_builtin_type (RID_MAX, "long long _Fract",
5236 long_long_fract_type_node);
5237 record_builtin_type (RID_MAX, "unsigned short _Fract",
5238 unsigned_short_fract_type_node);
5239 record_builtin_type (RID_MAX, "unsigned _Fract",
5240 unsigned_fract_type_node);
5241 record_builtin_type (RID_MAX, "unsigned long _Fract",
5242 unsigned_long_fract_type_node);
5243 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5244 unsigned_long_long_fract_type_node);
5245 record_builtin_type (RID_MAX, "_Sat short _Fract",
5246 sat_short_fract_type_node);
5247 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5248 record_builtin_type (RID_MAX, "_Sat long _Fract",
5249 sat_long_fract_type_node);
5250 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5251 sat_long_long_fract_type_node);
5252 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5253 sat_unsigned_short_fract_type_node);
5254 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5255 sat_unsigned_fract_type_node);
5256 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5257 sat_unsigned_long_fract_type_node);
5258 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5259 sat_unsigned_long_long_fract_type_node);
5260 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5261 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5262 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5263 record_builtin_type (RID_MAX, "long long _Accum",
5264 long_long_accum_type_node);
5265 record_builtin_type (RID_MAX, "unsigned short _Accum",
5266 unsigned_short_accum_type_node);
5267 record_builtin_type (RID_MAX, "unsigned _Accum",
5268 unsigned_accum_type_node);
5269 record_builtin_type (RID_MAX, "unsigned long _Accum",
5270 unsigned_long_accum_type_node);
5271 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5272 unsigned_long_long_accum_type_node);
5273 record_builtin_type (RID_MAX, "_Sat short _Accum",
5274 sat_short_accum_type_node);
5275 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5276 record_builtin_type (RID_MAX, "_Sat long _Accum",
5277 sat_long_accum_type_node);
5278 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5279 sat_long_long_accum_type_node);
5280 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5281 sat_unsigned_short_accum_type_node);
5282 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5283 sat_unsigned_accum_type_node);
5284 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5285 sat_unsigned_long_accum_type_node);
5286 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5287 sat_unsigned_long_long_accum_type_node);
5291 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5292 TYPE_DECL,
5293 get_identifier ("complex int"),
5294 complex_integer_type_node));
5295 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5296 TYPE_DECL,
5297 get_identifier ("complex float"),
5298 complex_float_type_node));
5299 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5300 TYPE_DECL,
5301 get_identifier ("complex double"),
5302 complex_double_type_node));
5303 lang_hooks.decls.pushdecl
5304 (build_decl (UNKNOWN_LOCATION,
5305 TYPE_DECL, get_identifier ("complex long double"),
5306 complex_long_double_type_node));
5308 if (c_dialect_cxx ())
5309 /* For C++, make fileptr_type_node a distinct void * type until
5310 FILE type is defined. */
5311 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5313 record_builtin_type (RID_VOID, NULL, void_type_node);
5315 /* Set the TYPE_NAME for any variants that were built before
5316 record_builtin_type gave names to the built-in types. */
5318 tree void_name = TYPE_NAME (void_type_node);
5319 TYPE_NAME (void_type_node) = NULL_TREE;
5320 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5321 = void_name;
5322 TYPE_NAME (void_type_node) = void_name;
5325 /* This node must not be shared. */
5326 void_zero_node = make_node (INTEGER_CST);
5327 TREE_TYPE (void_zero_node) = void_type_node;
5329 void_list_node = build_void_list_node ();
5331 /* Make a type to be the domain of a few array types
5332 whose domains don't really matter.
5333 200 is small enough that it always fits in size_t
5334 and large enough that it can hold most function names for the
5335 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5336 array_domain_type = build_index_type (size_int (200));
5338 /* Make a type for arrays of characters.
5339 With luck nothing will ever really depend on the length of this
5340 array type. */
5341 char_array_type_node
5342 = build_array_type (char_type_node, array_domain_type);
5344 /* Likewise for arrays of ints. */
5345 int_array_type_node
5346 = build_array_type (integer_type_node, array_domain_type);
5348 string_type_node = build_pointer_type (char_type_node);
5349 const_string_type_node
5350 = build_pointer_type (build_qualified_type
5351 (char_type_node, TYPE_QUAL_CONST));
5353 /* This is special for C++ so functions can be overloaded. */
5354 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5355 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5356 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5357 underlying_wchar_type_node = wchar_type_node;
5358 if (c_dialect_cxx ())
5360 if (TYPE_UNSIGNED (wchar_type_node))
5361 wchar_type_node = make_unsigned_type (wchar_type_size);
5362 else
5363 wchar_type_node = make_signed_type (wchar_type_size);
5364 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5367 /* This is for wide string constants. */
5368 wchar_array_type_node
5369 = build_array_type (wchar_type_node, array_domain_type);
5371 /* Define 'char16_t'. */
5372 char16_type_node = get_identifier (CHAR16_TYPE);
5373 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5374 char16_type_size = TYPE_PRECISION (char16_type_node);
5375 if (c_dialect_cxx ())
5377 char16_type_node = make_unsigned_type (char16_type_size);
5379 if (cxx_dialect >= cxx0x)
5380 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5383 /* This is for UTF-16 string constants. */
5384 char16_array_type_node
5385 = build_array_type (char16_type_node, array_domain_type);
5387 /* Define 'char32_t'. */
5388 char32_type_node = get_identifier (CHAR32_TYPE);
5389 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5390 char32_type_size = TYPE_PRECISION (char32_type_node);
5391 if (c_dialect_cxx ())
5393 char32_type_node = make_unsigned_type (char32_type_size);
5395 if (cxx_dialect >= cxx0x)
5396 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5399 /* This is for UTF-32 string constants. */
5400 char32_array_type_node
5401 = build_array_type (char32_type_node, array_domain_type);
5403 wint_type_node =
5404 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5406 intmax_type_node =
5407 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5408 uintmax_type_node =
5409 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5411 if (SIG_ATOMIC_TYPE)
5412 sig_atomic_type_node =
5413 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5414 if (INT8_TYPE)
5415 int8_type_node =
5416 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5417 if (INT16_TYPE)
5418 int16_type_node =
5419 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5420 if (INT32_TYPE)
5421 int32_type_node =
5422 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5423 if (INT64_TYPE)
5424 int64_type_node =
5425 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5426 if (UINT8_TYPE)
5427 uint8_type_node =
5428 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5429 if (UINT16_TYPE)
5430 c_uint16_type_node =
5431 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5432 if (UINT32_TYPE)
5433 c_uint32_type_node =
5434 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5435 if (UINT64_TYPE)
5436 c_uint64_type_node =
5437 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5438 if (INT_LEAST8_TYPE)
5439 int_least8_type_node =
5440 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5441 if (INT_LEAST16_TYPE)
5442 int_least16_type_node =
5443 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5444 if (INT_LEAST32_TYPE)
5445 int_least32_type_node =
5446 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5447 if (INT_LEAST64_TYPE)
5448 int_least64_type_node =
5449 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5450 if (UINT_LEAST8_TYPE)
5451 uint_least8_type_node =
5452 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5453 if (UINT_LEAST16_TYPE)
5454 uint_least16_type_node =
5455 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5456 if (UINT_LEAST32_TYPE)
5457 uint_least32_type_node =
5458 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5459 if (UINT_LEAST64_TYPE)
5460 uint_least64_type_node =
5461 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5462 if (INT_FAST8_TYPE)
5463 int_fast8_type_node =
5464 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5465 if (INT_FAST16_TYPE)
5466 int_fast16_type_node =
5467 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5468 if (INT_FAST32_TYPE)
5469 int_fast32_type_node =
5470 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5471 if (INT_FAST64_TYPE)
5472 int_fast64_type_node =
5473 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5474 if (UINT_FAST8_TYPE)
5475 uint_fast8_type_node =
5476 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5477 if (UINT_FAST16_TYPE)
5478 uint_fast16_type_node =
5479 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5480 if (UINT_FAST32_TYPE)
5481 uint_fast32_type_node =
5482 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5483 if (UINT_FAST64_TYPE)
5484 uint_fast64_type_node =
5485 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5486 if (INTPTR_TYPE)
5487 intptr_type_node =
5488 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5489 if (UINTPTR_TYPE)
5490 uintptr_type_node =
5491 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5493 default_function_type
5494 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5495 ptrdiff_type_node
5496 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5497 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5499 lang_hooks.decls.pushdecl
5500 (build_decl (UNKNOWN_LOCATION,
5501 TYPE_DECL, get_identifier ("__builtin_va_list"),
5502 va_list_type_node));
5503 if (targetm.enum_va_list_p)
5505 int l;
5506 const char *pname;
5507 tree ptype;
5509 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5511 lang_hooks.decls.pushdecl
5512 (build_decl (UNKNOWN_LOCATION,
5513 TYPE_DECL, get_identifier (pname),
5514 ptype));
5519 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5521 va_list_arg_type_node = va_list_ref_type_node =
5522 build_pointer_type (TREE_TYPE (va_list_type_node));
5524 else
5526 va_list_arg_type_node = va_list_type_node;
5527 va_list_ref_type_node = build_reference_type (va_list_type_node);
5530 if (!flag_preprocess_only)
5531 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5533 main_identifier_node = get_identifier ("main");
5535 /* Create the built-in __null node. It is important that this is
5536 not shared. */
5537 null_node = make_node (INTEGER_CST);
5538 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5540 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5541 memset (builtin_types, 0, sizeof (builtin_types));
5544 /* The number of named compound-literals generated thus far. */
5545 static GTY(()) int compound_literal_number;
5547 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5549 void
5550 set_compound_literal_name (tree decl)
5552 char *name;
5553 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5554 compound_literal_number);
5555 compound_literal_number++;
5556 DECL_NAME (decl) = get_identifier (name);
5559 tree
5560 build_va_arg (location_t loc, tree expr, tree type)
5562 expr = build1 (VA_ARG_EXPR, type, expr);
5563 SET_EXPR_LOCATION (expr, loc);
5564 return expr;
5568 /* Linked list of disabled built-in functions. */
5570 typedef struct disabled_builtin
5572 const char *name;
5573 struct disabled_builtin *next;
5574 } disabled_builtin;
5575 static disabled_builtin *disabled_builtins = NULL;
5577 static bool builtin_function_disabled_p (const char *);
5579 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5580 begins with "__builtin_", give an error. */
5582 void
5583 disable_builtin_function (const char *name)
5585 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5586 error ("cannot disable built-in function %qs", name);
5587 else
5589 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5590 new_disabled_builtin->name = name;
5591 new_disabled_builtin->next = disabled_builtins;
5592 disabled_builtins = new_disabled_builtin;
5597 /* Return true if the built-in function NAME has been disabled, false
5598 otherwise. */
5600 static bool
5601 builtin_function_disabled_p (const char *name)
5603 disabled_builtin *p;
5604 for (p = disabled_builtins; p != NULL; p = p->next)
5606 if (strcmp (name, p->name) == 0)
5607 return true;
5609 return false;
5613 /* Worker for DEF_BUILTIN.
5614 Possibly define a builtin function with one or two names.
5615 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5616 nonansi_p and flag_no_nonansi_builtin. */
5618 static void
5619 def_builtin_1 (enum built_in_function fncode,
5620 const char *name,
5621 enum built_in_class fnclass,
5622 tree fntype, tree libtype,
5623 bool both_p, bool fallback_p, bool nonansi_p,
5624 tree fnattrs, bool implicit_p)
5626 tree decl;
5627 const char *libname;
5629 if (fntype == error_mark_node)
5630 return;
5632 gcc_assert ((!both_p && !fallback_p)
5633 || !strncmp (name, "__builtin_",
5634 strlen ("__builtin_")));
5636 libname = name + strlen ("__builtin_");
5637 decl = add_builtin_function (name, fntype, fncode, fnclass,
5638 (fallback_p ? libname : NULL),
5639 fnattrs);
5641 set_builtin_decl (fncode, decl, implicit_p);
5643 if (both_p
5644 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5645 && !(nonansi_p && flag_no_nonansi_builtin))
5646 add_builtin_function (libname, libtype, fncode, fnclass,
5647 NULL, fnattrs);
5650 /* Nonzero if the type T promotes to int. This is (nearly) the
5651 integral promotions defined in ISO C99 6.3.1.1/2. */
5653 bool
5654 c_promoting_integer_type_p (const_tree t)
5656 switch (TREE_CODE (t))
5658 case INTEGER_TYPE:
5659 return (TYPE_MAIN_VARIANT (t) == char_type_node
5660 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5661 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5662 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5663 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5664 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5666 case ENUMERAL_TYPE:
5667 /* ??? Technically all enumerations not larger than an int
5668 promote to an int. But this is used along code paths
5669 that only want to notice a size change. */
5670 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5672 case BOOLEAN_TYPE:
5673 return 1;
5675 default:
5676 return 0;
5680 /* Return 1 if PARMS specifies a fixed number of parameters
5681 and none of their types is affected by default promotions. */
5684 self_promoting_args_p (const_tree parms)
5686 const_tree t;
5687 for (t = parms; t; t = TREE_CHAIN (t))
5689 tree type = TREE_VALUE (t);
5691 if (type == error_mark_node)
5692 continue;
5694 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5695 return 0;
5697 if (type == 0)
5698 return 0;
5700 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5701 return 0;
5703 if (c_promoting_integer_type_p (type))
5704 return 0;
5706 return 1;
5709 /* Recursively remove any '*' or '&' operator from TYPE. */
5710 tree
5711 strip_pointer_operator (tree t)
5713 while (POINTER_TYPE_P (t))
5714 t = TREE_TYPE (t);
5715 return t;
5718 /* Recursively remove pointer or array type from TYPE. */
5719 tree
5720 strip_pointer_or_array_types (tree t)
5722 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5723 t = TREE_TYPE (t);
5724 return t;
5727 /* Used to compare case labels. K1 and K2 are actually tree nodes
5728 representing case labels, or NULL_TREE for a `default' label.
5729 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5730 K2, and 0 if K1 and K2 are equal. */
5733 case_compare (splay_tree_key k1, splay_tree_key k2)
5735 /* Consider a NULL key (such as arises with a `default' label) to be
5736 smaller than anything else. */
5737 if (!k1)
5738 return k2 ? -1 : 0;
5739 else if (!k2)
5740 return k1 ? 1 : 0;
5742 return tree_int_cst_compare ((tree) k1, (tree) k2);
5745 /* Process a case label, located at LOC, for the range LOW_VALUE
5746 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5747 then this case label is actually a `default' label. If only
5748 HIGH_VALUE is NULL_TREE, then case label was declared using the
5749 usual C/C++ syntax, rather than the GNU case range extension.
5750 CASES is a tree containing all the case ranges processed so far;
5751 COND is the condition for the switch-statement itself. Returns the
5752 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5753 is created. */
5755 tree
5756 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5757 tree low_value, tree high_value)
5759 tree type;
5760 tree label;
5761 tree case_label;
5762 splay_tree_node node;
5764 /* Create the LABEL_DECL itself. */
5765 label = create_artificial_label (loc);
5767 /* If there was an error processing the switch condition, bail now
5768 before we get more confused. */
5769 if (!cond || cond == error_mark_node)
5770 goto error_out;
5772 if ((low_value && TREE_TYPE (low_value)
5773 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5774 || (high_value && TREE_TYPE (high_value)
5775 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5777 error_at (loc, "pointers are not permitted as case values");
5778 goto error_out;
5781 /* Case ranges are a GNU extension. */
5782 if (high_value)
5783 pedwarn (loc, OPT_Wpedantic,
5784 "range expressions in switch statements are non-standard");
5786 type = TREE_TYPE (cond);
5787 if (low_value)
5789 low_value = check_case_value (low_value);
5790 low_value = convert_and_check (type, low_value);
5791 if (low_value == error_mark_node)
5792 goto error_out;
5794 if (high_value)
5796 high_value = check_case_value (high_value);
5797 high_value = convert_and_check (type, high_value);
5798 if (high_value == error_mark_node)
5799 goto error_out;
5802 if (low_value && high_value)
5804 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5805 really a case range, even though it was written that way.
5806 Remove the HIGH_VALUE to simplify later processing. */
5807 if (tree_int_cst_equal (low_value, high_value))
5808 high_value = NULL_TREE;
5809 else if (!tree_int_cst_lt (low_value, high_value))
5810 warning_at (loc, 0, "empty range specified");
5813 /* See if the case is in range of the type of the original testing
5814 expression. If both low_value and high_value are out of range,
5815 don't insert the case label and return NULL_TREE. */
5816 if (low_value
5817 && !check_case_bounds (type, orig_type,
5818 &low_value, high_value ? &high_value : NULL))
5819 return NULL_TREE;
5821 /* Look up the LOW_VALUE in the table of case labels we already
5822 have. */
5823 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5824 /* If there was not an exact match, check for overlapping ranges.
5825 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5826 that's a `default' label and the only overlap is an exact match. */
5827 if (!node && (low_value || high_value))
5829 splay_tree_node low_bound;
5830 splay_tree_node high_bound;
5832 /* Even though there wasn't an exact match, there might be an
5833 overlap between this case range and another case range.
5834 Since we've (inductively) not allowed any overlapping case
5835 ranges, we simply need to find the greatest low case label
5836 that is smaller that LOW_VALUE, and the smallest low case
5837 label that is greater than LOW_VALUE. If there is an overlap
5838 it will occur in one of these two ranges. */
5839 low_bound = splay_tree_predecessor (cases,
5840 (splay_tree_key) low_value);
5841 high_bound = splay_tree_successor (cases,
5842 (splay_tree_key) low_value);
5844 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5845 the LOW_VALUE, so there is no need to check unless the
5846 LOW_BOUND is in fact itself a case range. */
5847 if (low_bound
5848 && CASE_HIGH ((tree) low_bound->value)
5849 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5850 low_value) >= 0)
5851 node = low_bound;
5852 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5853 range is bigger than the low end of the current range, so we
5854 are only interested if the current range is a real range, and
5855 not an ordinary case label. */
5856 else if (high_bound
5857 && high_value
5858 && (tree_int_cst_compare ((tree) high_bound->key,
5859 high_value)
5860 <= 0))
5861 node = high_bound;
5863 /* If there was an overlap, issue an error. */
5864 if (node)
5866 tree duplicate = CASE_LABEL ((tree) node->value);
5868 if (high_value)
5870 error_at (loc, "duplicate (or overlapping) case value");
5871 error_at (DECL_SOURCE_LOCATION (duplicate),
5872 "this is the first entry overlapping that value");
5874 else if (low_value)
5876 error_at (loc, "duplicate case value") ;
5877 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5879 else
5881 error_at (loc, "multiple default labels in one switch");
5882 error_at (DECL_SOURCE_LOCATION (duplicate),
5883 "this is the first default label");
5885 goto error_out;
5888 /* Add a CASE_LABEL to the statement-tree. */
5889 case_label = add_stmt (build_case_label (low_value, high_value, label));
5890 /* Register this case label in the splay tree. */
5891 splay_tree_insert (cases,
5892 (splay_tree_key) low_value,
5893 (splay_tree_value) case_label);
5895 return case_label;
5897 error_out:
5898 /* Add a label so that the back-end doesn't think that the beginning of
5899 the switch is unreachable. Note that we do not add a case label, as
5900 that just leads to duplicates and thence to failure later on. */
5901 if (!cases->root)
5903 tree t = create_artificial_label (loc);
5904 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5906 return error_mark_node;
5909 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5910 Used to verify that case values match up with enumerator values. */
5912 static void
5913 match_case_to_enum_1 (tree key, tree type, tree label)
5915 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5917 /* ??? Not working too hard to print the double-word value.
5918 Should perhaps be done with %lwd in the diagnostic routines? */
5919 if (TREE_INT_CST_HIGH (key) == 0)
5920 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5921 TREE_INT_CST_LOW (key));
5922 else if (!TYPE_UNSIGNED (type)
5923 && TREE_INT_CST_HIGH (key) == -1
5924 && TREE_INT_CST_LOW (key) != 0)
5925 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5926 -TREE_INT_CST_LOW (key));
5927 else
5928 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5929 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5930 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5932 if (TYPE_NAME (type) == 0)
5933 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5934 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5935 "case value %qs not in enumerated type",
5936 buf);
5937 else
5938 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5939 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5940 "case value %qs not in enumerated type %qT",
5941 buf, type);
5944 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5945 Used to verify that case values match up with enumerator values. */
5947 static int
5948 match_case_to_enum (splay_tree_node node, void *data)
5950 tree label = (tree) node->value;
5951 tree type = (tree) data;
5953 /* Skip default case. */
5954 if (!CASE_LOW (label))
5955 return 0;
5957 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5958 when we did our enum->case scan. Reset our scratch bit after. */
5959 if (!CASE_LOW_SEEN (label))
5960 match_case_to_enum_1 (CASE_LOW (label), type, label);
5961 else
5962 CASE_LOW_SEEN (label) = 0;
5964 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5965 not set, that means that CASE_HIGH did not appear when we did our
5966 enum->case scan. Reset our scratch bit after. */
5967 if (CASE_HIGH (label))
5969 if (!CASE_HIGH_SEEN (label))
5970 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5971 else
5972 CASE_HIGH_SEEN (label) = 0;
5975 return 0;
5978 /* Handle -Wswitch*. Called from the front end after parsing the
5979 switch construct. */
5980 /* ??? Should probably be somewhere generic, since other languages
5981 besides C and C++ would want this. At the moment, however, C/C++
5982 are the only tree-ssa languages that support enumerations at all,
5983 so the point is moot. */
5985 void
5986 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5987 tree type, tree cond)
5989 splay_tree_node default_node;
5990 splay_tree_node node;
5991 tree chain;
5993 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5994 return;
5996 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5997 if (!default_node)
5998 warning_at (switch_location, OPT_Wswitch_default,
5999 "switch missing default case");
6001 /* From here on, we only care about about enumerated types. */
6002 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6003 return;
6005 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6006 if (!warn_switch_enum && !warn_switch)
6007 return;
6009 /* Check the cases. Warn about case values which are not members of
6010 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6011 there is no default case, check that exactly all enumeration
6012 literals are covered by the cases. */
6014 /* Clearing COND if it is not an integer constant simplifies
6015 the tests inside the loop below. */
6016 if (TREE_CODE (cond) != INTEGER_CST)
6017 cond = NULL_TREE;
6019 /* The time complexity here is O(N*lg(N)) worst case, but for the
6020 common case of monotonically increasing enumerators, it is
6021 O(N), since the nature of the splay tree will keep the next
6022 element adjacent to the root at all times. */
6024 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6026 tree value = TREE_VALUE (chain);
6027 if (TREE_CODE (value) == CONST_DECL)
6028 value = DECL_INITIAL (value);
6029 node = splay_tree_lookup (cases, (splay_tree_key) value);
6030 if (node)
6032 /* Mark the CASE_LOW part of the case entry as seen. */
6033 tree label = (tree) node->value;
6034 CASE_LOW_SEEN (label) = 1;
6035 continue;
6038 /* Even though there wasn't an exact match, there might be a
6039 case range which includes the enumerator's value. */
6040 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6041 if (node && CASE_HIGH ((tree) node->value))
6043 tree label = (tree) node->value;
6044 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6045 if (cmp >= 0)
6047 /* If we match the upper bound exactly, mark the CASE_HIGH
6048 part of the case entry as seen. */
6049 if (cmp == 0)
6050 CASE_HIGH_SEEN (label) = 1;
6051 continue;
6055 /* We've now determined that this enumerated literal isn't
6056 handled by the case labels of the switch statement. */
6058 /* If the switch expression is a constant, we only really care
6059 about whether that constant is handled by the switch. */
6060 if (cond && tree_int_cst_compare (cond, value))
6061 continue;
6063 /* If there is a default_node, the only relevant option is
6064 Wswitch-enum. Otherwise, if both are enabled then we prefer
6065 to warn using -Wswitch because -Wswitch is enabled by -Wall
6066 while -Wswitch-enum is explicit. */
6067 warning_at (switch_location,
6068 (default_node || !warn_switch
6069 ? OPT_Wswitch_enum
6070 : OPT_Wswitch),
6071 "enumeration value %qE not handled in switch",
6072 TREE_PURPOSE (chain));
6075 /* Warn if there are case expressions that don't correspond to
6076 enumerators. This can occur since C and C++ don't enforce
6077 type-checking of assignments to enumeration variables.
6079 The time complexity here is now always O(N) worst case, since
6080 we should have marked both the lower bound and upper bound of
6081 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6082 above. This scan also resets those fields. */
6084 splay_tree_foreach (cases, match_case_to_enum, type);
6087 /* Finish an expression taking the address of LABEL (an
6088 IDENTIFIER_NODE). Returns an expression for the address.
6090 LOC is the location for the expression returned. */
6092 tree
6093 finish_label_address_expr (tree label, location_t loc)
6095 tree result;
6097 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6099 if (label == error_mark_node)
6100 return error_mark_node;
6102 label = lookup_label (label);
6103 if (label == NULL_TREE)
6104 result = null_pointer_node;
6105 else
6107 TREE_USED (label) = 1;
6108 result = build1 (ADDR_EXPR, ptr_type_node, label);
6109 /* The current function is not necessarily uninlinable.
6110 Computed gotos are incompatible with inlining, but the value
6111 here could be used only in a diagnostic, for example. */
6112 protected_set_expr_location (result, loc);
6115 return result;
6119 /* Given a boolean expression ARG, return a tree representing an increment
6120 or decrement (as indicated by CODE) of ARG. The front end must check for
6121 invalid cases (e.g., decrement in C++). */
6122 tree
6123 boolean_increment (enum tree_code code, tree arg)
6125 tree val;
6126 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6128 arg = stabilize_reference (arg);
6129 switch (code)
6131 case PREINCREMENT_EXPR:
6132 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6133 break;
6134 case POSTINCREMENT_EXPR:
6135 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6136 arg = save_expr (arg);
6137 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6138 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6139 break;
6140 case PREDECREMENT_EXPR:
6141 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6142 invert_truthvalue_loc (input_location, arg));
6143 break;
6144 case POSTDECREMENT_EXPR:
6145 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6146 invert_truthvalue_loc (input_location, arg));
6147 arg = save_expr (arg);
6148 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6149 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6150 break;
6151 default:
6152 gcc_unreachable ();
6154 TREE_SIDE_EFFECTS (val) = 1;
6155 return val;
6158 /* Built-in macros for stddef.h and stdint.h, that require macros
6159 defined in this file. */
6160 void
6161 c_stddef_cpp_builtins(void)
6163 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6164 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6165 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6166 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6167 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6168 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6169 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6170 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6171 if (SIG_ATOMIC_TYPE)
6172 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6173 if (INT8_TYPE)
6174 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6175 if (INT16_TYPE)
6176 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6177 if (INT32_TYPE)
6178 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6179 if (INT64_TYPE)
6180 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6181 if (UINT8_TYPE)
6182 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6183 if (UINT16_TYPE)
6184 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6185 if (UINT32_TYPE)
6186 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6187 if (UINT64_TYPE)
6188 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6189 if (INT_LEAST8_TYPE)
6190 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6191 if (INT_LEAST16_TYPE)
6192 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6193 if (INT_LEAST32_TYPE)
6194 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6195 if (INT_LEAST64_TYPE)
6196 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6197 if (UINT_LEAST8_TYPE)
6198 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6199 if (UINT_LEAST16_TYPE)
6200 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6201 if (UINT_LEAST32_TYPE)
6202 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6203 if (UINT_LEAST64_TYPE)
6204 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6205 if (INT_FAST8_TYPE)
6206 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6207 if (INT_FAST16_TYPE)
6208 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6209 if (INT_FAST32_TYPE)
6210 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6211 if (INT_FAST64_TYPE)
6212 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6213 if (UINT_FAST8_TYPE)
6214 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6215 if (UINT_FAST16_TYPE)
6216 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6217 if (UINT_FAST32_TYPE)
6218 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6219 if (UINT_FAST64_TYPE)
6220 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6221 if (INTPTR_TYPE)
6222 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6223 if (UINTPTR_TYPE)
6224 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6227 static void
6228 c_init_attributes (void)
6230 /* Fill in the built_in_attributes array. */
6231 #define DEF_ATTR_NULL_TREE(ENUM) \
6232 built_in_attributes[(int) ENUM] = NULL_TREE;
6233 #define DEF_ATTR_INT(ENUM, VALUE) \
6234 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6235 #define DEF_ATTR_STRING(ENUM, VALUE) \
6236 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6237 #define DEF_ATTR_IDENT(ENUM, STRING) \
6238 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6239 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6240 built_in_attributes[(int) ENUM] \
6241 = tree_cons (built_in_attributes[(int) PURPOSE], \
6242 built_in_attributes[(int) VALUE], \
6243 built_in_attributes[(int) CHAIN]);
6244 #include "builtin-attrs.def"
6245 #undef DEF_ATTR_NULL_TREE
6246 #undef DEF_ATTR_INT
6247 #undef DEF_ATTR_IDENT
6248 #undef DEF_ATTR_TREE_LIST
6251 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6252 identifier as an argument, so the front end shouldn't look it up. */
6254 bool
6255 attribute_takes_identifier_p (const_tree attr_id)
6257 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6258 if (spec == NULL)
6259 /* Unknown attribute that we'll end up ignoring, return true so we
6260 don't complain about an identifier argument. */
6261 return true;
6262 else if (!strcmp ("mode", spec->name)
6263 || !strcmp ("format", spec->name)
6264 || !strcmp ("cleanup", spec->name))
6265 return true;
6266 else
6267 return targetm.attribute_takes_identifier_p (attr_id);
6270 /* Attribute handlers common to C front ends. */
6272 /* Handle a "packed" attribute; arguments as in
6273 struct attribute_spec.handler. */
6275 static tree
6276 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6277 int flags, bool *no_add_attrs)
6279 if (TYPE_P (*node))
6281 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6282 *node = build_variant_type_copy (*node);
6283 TYPE_PACKED (*node) = 1;
6285 else if (TREE_CODE (*node) == FIELD_DECL)
6287 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6288 /* Still pack bitfields. */
6289 && ! DECL_INITIAL (*node))
6290 warning (OPT_Wattributes,
6291 "%qE attribute ignored for field of type %qT",
6292 name, TREE_TYPE (*node));
6293 else
6294 DECL_PACKED (*node) = 1;
6296 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6297 used for DECL_REGISTER. It wouldn't mean anything anyway.
6298 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6299 that changes what the typedef is typing. */
6300 else
6302 warning (OPT_Wattributes, "%qE attribute ignored", name);
6303 *no_add_attrs = true;
6306 return NULL_TREE;
6309 /* Handle a "nocommon" attribute; arguments as in
6310 struct attribute_spec.handler. */
6312 static tree
6313 handle_nocommon_attribute (tree *node, tree name,
6314 tree ARG_UNUSED (args),
6315 int ARG_UNUSED (flags), bool *no_add_attrs)
6317 if (TREE_CODE (*node) == VAR_DECL)
6318 DECL_COMMON (*node) = 0;
6319 else
6321 warning (OPT_Wattributes, "%qE attribute ignored", name);
6322 *no_add_attrs = true;
6325 return NULL_TREE;
6328 /* Handle a "common" attribute; arguments as in
6329 struct attribute_spec.handler. */
6331 static tree
6332 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6333 int ARG_UNUSED (flags), bool *no_add_attrs)
6335 if (TREE_CODE (*node) == VAR_DECL)
6336 DECL_COMMON (*node) = 1;
6337 else
6339 warning (OPT_Wattributes, "%qE attribute ignored", name);
6340 *no_add_attrs = true;
6343 return NULL_TREE;
6346 /* Handle a "noreturn" attribute; arguments as in
6347 struct attribute_spec.handler. */
6349 static tree
6350 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6351 int ARG_UNUSED (flags), bool *no_add_attrs)
6353 tree type = TREE_TYPE (*node);
6355 /* See FIXME comment in c_common_attribute_table. */
6356 if (TREE_CODE (*node) == FUNCTION_DECL
6357 || objc_method_decl (TREE_CODE (*node)))
6358 TREE_THIS_VOLATILE (*node) = 1;
6359 else if (TREE_CODE (type) == POINTER_TYPE
6360 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6361 TREE_TYPE (*node)
6362 = build_pointer_type
6363 (build_type_variant (TREE_TYPE (type),
6364 TYPE_READONLY (TREE_TYPE (type)), 1));
6365 else
6367 warning (OPT_Wattributes, "%qE attribute ignored", name);
6368 *no_add_attrs = true;
6371 return NULL_TREE;
6374 /* Handle a "hot" and attribute; arguments as in
6375 struct attribute_spec.handler. */
6377 static tree
6378 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6379 int ARG_UNUSED (flags), bool *no_add_attrs)
6381 if (TREE_CODE (*node) == FUNCTION_DECL
6382 || TREE_CODE (*node) == LABEL_DECL)
6384 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6386 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6387 name, "cold");
6388 *no_add_attrs = true;
6390 /* Most of the rest of the hot processing is done later with
6391 lookup_attribute. */
6393 else
6395 warning (OPT_Wattributes, "%qE attribute ignored", name);
6396 *no_add_attrs = true;
6399 return NULL_TREE;
6402 /* Handle a "cold" and attribute; arguments as in
6403 struct attribute_spec.handler. */
6405 static tree
6406 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6407 int ARG_UNUSED (flags), bool *no_add_attrs)
6409 if (TREE_CODE (*node) == FUNCTION_DECL
6410 || TREE_CODE (*node) == LABEL_DECL)
6412 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6414 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6415 name, "hot");
6416 *no_add_attrs = true;
6418 /* Most of the rest of the cold processing is done later with
6419 lookup_attribute. */
6421 else
6423 warning (OPT_Wattributes, "%qE attribute ignored", name);
6424 *no_add_attrs = true;
6427 return NULL_TREE;
6430 /* Handle a "noinline" attribute; arguments as in
6431 struct attribute_spec.handler. */
6433 static tree
6434 handle_noinline_attribute (tree *node, tree name,
6435 tree ARG_UNUSED (args),
6436 int ARG_UNUSED (flags), bool *no_add_attrs)
6438 if (TREE_CODE (*node) == FUNCTION_DECL)
6439 DECL_UNINLINABLE (*node) = 1;
6440 else
6442 warning (OPT_Wattributes, "%qE attribute ignored", name);
6443 *no_add_attrs = true;
6446 return NULL_TREE;
6449 /* Handle a "noclone" attribute; arguments as in
6450 struct attribute_spec.handler. */
6452 static tree
6453 handle_noclone_attribute (tree *node, tree name,
6454 tree ARG_UNUSED (args),
6455 int ARG_UNUSED (flags), bool *no_add_attrs)
6457 if (TREE_CODE (*node) != FUNCTION_DECL)
6459 warning (OPT_Wattributes, "%qE attribute ignored", name);
6460 *no_add_attrs = true;
6463 return NULL_TREE;
6466 /* Handle a "always_inline" attribute; arguments as in
6467 struct attribute_spec.handler. */
6469 static tree
6470 handle_always_inline_attribute (tree *node, tree name,
6471 tree ARG_UNUSED (args),
6472 int ARG_UNUSED (flags),
6473 bool *no_add_attrs)
6475 if (TREE_CODE (*node) == FUNCTION_DECL)
6477 /* Set the attribute and mark it for disregarding inline
6478 limits. */
6479 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6481 else
6483 warning (OPT_Wattributes, "%qE attribute ignored", name);
6484 *no_add_attrs = true;
6487 return NULL_TREE;
6490 /* Handle a "gnu_inline" attribute; arguments as in
6491 struct attribute_spec.handler. */
6493 static tree
6494 handle_gnu_inline_attribute (tree *node, tree name,
6495 tree ARG_UNUSED (args),
6496 int ARG_UNUSED (flags),
6497 bool *no_add_attrs)
6499 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6501 /* Do nothing else, just set the attribute. We'll get at
6502 it later with lookup_attribute. */
6504 else
6506 warning (OPT_Wattributes, "%qE attribute ignored", name);
6507 *no_add_attrs = true;
6510 return NULL_TREE;
6513 /* Handle a "leaf" attribute; arguments as in
6514 struct attribute_spec.handler. */
6516 static tree
6517 handle_leaf_attribute (tree *node, tree name,
6518 tree ARG_UNUSED (args),
6519 int ARG_UNUSED (flags), bool *no_add_attrs)
6521 if (TREE_CODE (*node) != FUNCTION_DECL)
6523 warning (OPT_Wattributes, "%qE attribute ignored", name);
6524 *no_add_attrs = true;
6526 if (!TREE_PUBLIC (*node))
6528 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6529 *no_add_attrs = true;
6532 return NULL_TREE;
6535 /* Handle an "artificial" attribute; arguments as in
6536 struct attribute_spec.handler. */
6538 static tree
6539 handle_artificial_attribute (tree *node, tree name,
6540 tree ARG_UNUSED (args),
6541 int ARG_UNUSED (flags),
6542 bool *no_add_attrs)
6544 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6546 /* Do nothing else, just set the attribute. We'll get at
6547 it later with lookup_attribute. */
6549 else
6551 warning (OPT_Wattributes, "%qE attribute ignored", name);
6552 *no_add_attrs = true;
6555 return NULL_TREE;
6558 /* Handle a "flatten" attribute; arguments as in
6559 struct attribute_spec.handler. */
6561 static tree
6562 handle_flatten_attribute (tree *node, tree name,
6563 tree args ATTRIBUTE_UNUSED,
6564 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6566 if (TREE_CODE (*node) == FUNCTION_DECL)
6567 /* Do nothing else, just set the attribute. We'll get at
6568 it later with lookup_attribute. */
6570 else
6572 warning (OPT_Wattributes, "%qE attribute ignored", name);
6573 *no_add_attrs = true;
6576 return NULL_TREE;
6579 /* Handle a "warning" or "error" attribute; arguments as in
6580 struct attribute_spec.handler. */
6582 static tree
6583 handle_error_attribute (tree *node, tree name, tree args,
6584 int ARG_UNUSED (flags), bool *no_add_attrs)
6586 if (TREE_CODE (*node) == FUNCTION_DECL
6587 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6588 /* Do nothing else, just set the attribute. We'll get at
6589 it later with lookup_attribute. */
6591 else
6593 warning (OPT_Wattributes, "%qE attribute ignored", name);
6594 *no_add_attrs = true;
6597 return NULL_TREE;
6600 /* Handle a "used" attribute; arguments as in
6601 struct attribute_spec.handler. */
6603 static tree
6604 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6605 int ARG_UNUSED (flags), bool *no_add_attrs)
6607 tree node = *pnode;
6609 if (TREE_CODE (node) == FUNCTION_DECL
6610 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6611 || (TREE_CODE (node) == TYPE_DECL))
6613 TREE_USED (node) = 1;
6614 DECL_PRESERVE_P (node) = 1;
6615 if (TREE_CODE (node) == VAR_DECL)
6616 DECL_READ_P (node) = 1;
6618 else
6620 warning (OPT_Wattributes, "%qE attribute ignored", name);
6621 *no_add_attrs = true;
6624 return NULL_TREE;
6627 /* Handle a "unused" attribute; arguments as in
6628 struct attribute_spec.handler. */
6630 static tree
6631 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6632 int flags, bool *no_add_attrs)
6634 if (DECL_P (*node))
6636 tree decl = *node;
6638 if (TREE_CODE (decl) == PARM_DECL
6639 || TREE_CODE (decl) == VAR_DECL
6640 || TREE_CODE (decl) == FUNCTION_DECL
6641 || TREE_CODE (decl) == LABEL_DECL
6642 || TREE_CODE (decl) == TYPE_DECL)
6644 TREE_USED (decl) = 1;
6645 if (TREE_CODE (decl) == VAR_DECL
6646 || TREE_CODE (decl) == PARM_DECL)
6647 DECL_READ_P (decl) = 1;
6649 else
6651 warning (OPT_Wattributes, "%qE attribute ignored", name);
6652 *no_add_attrs = true;
6655 else
6657 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6658 *node = build_variant_type_copy (*node);
6659 TREE_USED (*node) = 1;
6662 return NULL_TREE;
6665 /* Handle a "externally_visible" attribute; arguments as in
6666 struct attribute_spec.handler. */
6668 static tree
6669 handle_externally_visible_attribute (tree *pnode, tree name,
6670 tree ARG_UNUSED (args),
6671 int ARG_UNUSED (flags),
6672 bool *no_add_attrs)
6674 tree node = *pnode;
6676 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6678 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6679 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6681 warning (OPT_Wattributes,
6682 "%qE attribute have effect only on public objects", name);
6683 *no_add_attrs = true;
6686 else
6688 warning (OPT_Wattributes, "%qE attribute ignored", name);
6689 *no_add_attrs = true;
6692 return NULL_TREE;
6695 /* Handle a "const" attribute; arguments as in
6696 struct attribute_spec.handler. */
6698 static tree
6699 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6700 int ARG_UNUSED (flags), bool *no_add_attrs)
6702 tree type = TREE_TYPE (*node);
6704 /* See FIXME comment on noreturn in c_common_attribute_table. */
6705 if (TREE_CODE (*node) == FUNCTION_DECL)
6706 TREE_READONLY (*node) = 1;
6707 else if (TREE_CODE (type) == POINTER_TYPE
6708 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6709 TREE_TYPE (*node)
6710 = build_pointer_type
6711 (build_type_variant (TREE_TYPE (type), 1,
6712 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6713 else
6715 warning (OPT_Wattributes, "%qE attribute ignored", name);
6716 *no_add_attrs = true;
6719 return NULL_TREE;
6722 /* Handle a "transparent_union" attribute; arguments as in
6723 struct attribute_spec.handler. */
6725 static tree
6726 handle_transparent_union_attribute (tree *node, tree name,
6727 tree ARG_UNUSED (args), int flags,
6728 bool *no_add_attrs)
6730 tree type;
6732 *no_add_attrs = true;
6734 if (TREE_CODE (*node) == TYPE_DECL)
6735 node = &TREE_TYPE (*node);
6736 type = *node;
6738 if (TREE_CODE (type) == UNION_TYPE)
6740 /* Make sure that the first field will work for a transparent union.
6741 If the type isn't complete yet, leave the check to the code in
6742 finish_struct. */
6743 if (TYPE_SIZE (type))
6745 tree first = first_field (type);
6746 if (first == NULL_TREE
6747 || DECL_ARTIFICIAL (first)
6748 || TYPE_MODE (type) != DECL_MODE (first))
6749 goto ignored;
6752 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6754 /* If the type isn't complete yet, setting the flag
6755 on a variant wouldn't ever be checked. */
6756 if (!TYPE_SIZE (type))
6757 goto ignored;
6759 /* build_duplicate_type doesn't work for C++. */
6760 if (c_dialect_cxx ())
6761 goto ignored;
6763 /* A type variant isn't good enough, since we don't a cast
6764 to such a type removed as a no-op. */
6765 *node = type = build_duplicate_type (type);
6768 TYPE_TRANSPARENT_AGGR (type) = 1;
6769 return NULL_TREE;
6772 ignored:
6773 warning (OPT_Wattributes, "%qE attribute ignored", name);
6774 return NULL_TREE;
6777 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6778 get the requested priority for a constructor or destructor,
6779 possibly issuing diagnostics for invalid or reserved
6780 priorities. */
6782 static priority_type
6783 get_priority (tree args, bool is_destructor)
6785 HOST_WIDE_INT pri;
6786 tree arg;
6788 if (!args)
6789 return DEFAULT_INIT_PRIORITY;
6791 if (!SUPPORTS_INIT_PRIORITY)
6793 if (is_destructor)
6794 error ("destructor priorities are not supported");
6795 else
6796 error ("constructor priorities are not supported");
6797 return DEFAULT_INIT_PRIORITY;
6800 arg = TREE_VALUE (args);
6801 arg = default_conversion (arg);
6802 if (!host_integerp (arg, /*pos=*/0)
6803 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6804 goto invalid;
6806 pri = tree_low_cst (arg, /*pos=*/0);
6807 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6808 goto invalid;
6810 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6812 if (is_destructor)
6813 warning (0,
6814 "destructor priorities from 0 to %d are reserved "
6815 "for the implementation",
6816 MAX_RESERVED_INIT_PRIORITY);
6817 else
6818 warning (0,
6819 "constructor priorities from 0 to %d are reserved "
6820 "for the implementation",
6821 MAX_RESERVED_INIT_PRIORITY);
6823 return pri;
6825 invalid:
6826 if (is_destructor)
6827 error ("destructor priorities must be integers from 0 to %d inclusive",
6828 MAX_INIT_PRIORITY);
6829 else
6830 error ("constructor priorities must be integers from 0 to %d inclusive",
6831 MAX_INIT_PRIORITY);
6832 return DEFAULT_INIT_PRIORITY;
6835 /* Handle a "constructor" attribute; arguments as in
6836 struct attribute_spec.handler. */
6838 static tree
6839 handle_constructor_attribute (tree *node, tree name, tree args,
6840 int ARG_UNUSED (flags),
6841 bool *no_add_attrs)
6843 tree decl = *node;
6844 tree type = TREE_TYPE (decl);
6846 if (TREE_CODE (decl) == FUNCTION_DECL
6847 && TREE_CODE (type) == FUNCTION_TYPE
6848 && decl_function_context (decl) == 0)
6850 priority_type priority;
6851 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6852 priority = get_priority (args, /*is_destructor=*/false);
6853 SET_DECL_INIT_PRIORITY (decl, priority);
6854 TREE_USED (decl) = 1;
6856 else
6858 warning (OPT_Wattributes, "%qE attribute ignored", name);
6859 *no_add_attrs = true;
6862 return NULL_TREE;
6865 /* Handle a "destructor" attribute; arguments as in
6866 struct attribute_spec.handler. */
6868 static tree
6869 handle_destructor_attribute (tree *node, tree name, tree args,
6870 int ARG_UNUSED (flags),
6871 bool *no_add_attrs)
6873 tree decl = *node;
6874 tree type = TREE_TYPE (decl);
6876 if (TREE_CODE (decl) == FUNCTION_DECL
6877 && TREE_CODE (type) == FUNCTION_TYPE
6878 && decl_function_context (decl) == 0)
6880 priority_type priority;
6881 DECL_STATIC_DESTRUCTOR (decl) = 1;
6882 priority = get_priority (args, /*is_destructor=*/true);
6883 SET_DECL_FINI_PRIORITY (decl, priority);
6884 TREE_USED (decl) = 1;
6886 else
6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
6889 *no_add_attrs = true;
6892 return NULL_TREE;
6895 /* Nonzero if the mode is a valid vector mode for this architecture.
6896 This returns nonzero even if there is no hardware support for the
6897 vector mode, but we can emulate with narrower modes. */
6899 static int
6900 vector_mode_valid_p (enum machine_mode mode)
6902 enum mode_class mclass = GET_MODE_CLASS (mode);
6903 enum machine_mode innermode;
6905 /* Doh! What's going on? */
6906 if (mclass != MODE_VECTOR_INT
6907 && mclass != MODE_VECTOR_FLOAT
6908 && mclass != MODE_VECTOR_FRACT
6909 && mclass != MODE_VECTOR_UFRACT
6910 && mclass != MODE_VECTOR_ACCUM
6911 && mclass != MODE_VECTOR_UACCUM)
6912 return 0;
6914 /* Hardware support. Woo hoo! */
6915 if (targetm.vector_mode_supported_p (mode))
6916 return 1;
6918 innermode = GET_MODE_INNER (mode);
6920 /* We should probably return 1 if requesting V4DI and we have no DI,
6921 but we have V2DI, but this is probably very unlikely. */
6923 /* If we have support for the inner mode, we can safely emulate it.
6924 We may not have V2DI, but me can emulate with a pair of DIs. */
6925 return targetm.scalar_mode_supported_p (innermode);
6929 /* Handle a "mode" attribute; arguments as in
6930 struct attribute_spec.handler. */
6932 static tree
6933 handle_mode_attribute (tree *node, tree name, tree args,
6934 int ARG_UNUSED (flags), bool *no_add_attrs)
6936 tree type = *node;
6937 tree ident = TREE_VALUE (args);
6939 *no_add_attrs = true;
6941 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6942 warning (OPT_Wattributes, "%qE attribute ignored", name);
6943 else
6945 int j;
6946 const char *p = IDENTIFIER_POINTER (ident);
6947 int len = strlen (p);
6948 enum machine_mode mode = VOIDmode;
6949 tree typefm;
6950 bool valid_mode;
6952 if (len > 4 && p[0] == '_' && p[1] == '_'
6953 && p[len - 1] == '_' && p[len - 2] == '_')
6955 char *newp = (char *) alloca (len - 1);
6957 strcpy (newp, &p[2]);
6958 newp[len - 4] = '\0';
6959 p = newp;
6962 /* Change this type to have a type with the specified mode.
6963 First check for the special modes. */
6964 if (!strcmp (p, "byte"))
6965 mode = byte_mode;
6966 else if (!strcmp (p, "word"))
6967 mode = word_mode;
6968 else if (!strcmp (p, "pointer"))
6969 mode = ptr_mode;
6970 else if (!strcmp (p, "libgcc_cmp_return"))
6971 mode = targetm.libgcc_cmp_return_mode ();
6972 else if (!strcmp (p, "libgcc_shift_count"))
6973 mode = targetm.libgcc_shift_count_mode ();
6974 else if (!strcmp (p, "unwind_word"))
6975 mode = targetm.unwind_word_mode ();
6976 else
6977 for (j = 0; j < NUM_MACHINE_MODES; j++)
6978 if (!strcmp (p, GET_MODE_NAME (j)))
6980 mode = (enum machine_mode) j;
6981 break;
6984 if (mode == VOIDmode)
6986 error ("unknown machine mode %qE", ident);
6987 return NULL_TREE;
6990 valid_mode = false;
6991 switch (GET_MODE_CLASS (mode))
6993 case MODE_INT:
6994 case MODE_PARTIAL_INT:
6995 case MODE_FLOAT:
6996 case MODE_DECIMAL_FLOAT:
6997 case MODE_FRACT:
6998 case MODE_UFRACT:
6999 case MODE_ACCUM:
7000 case MODE_UACCUM:
7001 valid_mode = targetm.scalar_mode_supported_p (mode);
7002 break;
7004 case MODE_COMPLEX_INT:
7005 case MODE_COMPLEX_FLOAT:
7006 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7007 break;
7009 case MODE_VECTOR_INT:
7010 case MODE_VECTOR_FLOAT:
7011 case MODE_VECTOR_FRACT:
7012 case MODE_VECTOR_UFRACT:
7013 case MODE_VECTOR_ACCUM:
7014 case MODE_VECTOR_UACCUM:
7015 warning (OPT_Wattributes, "specifying vector types with "
7016 "__attribute__ ((mode)) is deprecated");
7017 warning (OPT_Wattributes,
7018 "use __attribute__ ((vector_size)) instead");
7019 valid_mode = vector_mode_valid_p (mode);
7020 break;
7022 default:
7023 break;
7025 if (!valid_mode)
7027 error ("unable to emulate %qs", p);
7028 return NULL_TREE;
7031 if (POINTER_TYPE_P (type))
7033 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7034 tree (*fn)(tree, enum machine_mode, bool);
7036 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7038 error ("invalid pointer mode %qs", p);
7039 return NULL_TREE;
7042 if (TREE_CODE (type) == POINTER_TYPE)
7043 fn = build_pointer_type_for_mode;
7044 else
7045 fn = build_reference_type_for_mode;
7046 typefm = fn (TREE_TYPE (type), mode, false);
7048 else
7050 /* For fixed-point modes, we need to test if the signness of type
7051 and the machine mode are consistent. */
7052 if (ALL_FIXED_POINT_MODE_P (mode)
7053 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7055 error ("signedness of type and machine mode %qs don%'t match", p);
7056 return NULL_TREE;
7058 /* For fixed-point modes, we need to pass saturating info. */
7059 typefm = lang_hooks.types.type_for_mode (mode,
7060 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7061 : TYPE_UNSIGNED (type));
7064 if (typefm == NULL_TREE)
7066 error ("no data type for mode %qs", p);
7067 return NULL_TREE;
7069 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7071 /* For enumeral types, copy the precision from the integer
7072 type returned above. If not an INTEGER_TYPE, we can't use
7073 this mode for this type. */
7074 if (TREE_CODE (typefm) != INTEGER_TYPE)
7076 error ("cannot use mode %qs for enumeral types", p);
7077 return NULL_TREE;
7080 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7082 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7083 typefm = type;
7085 else
7087 /* We cannot build a type variant, as there's code that assumes
7088 that TYPE_MAIN_VARIANT has the same mode. This includes the
7089 debug generators. Instead, create a subrange type. This
7090 results in all of the enumeral values being emitted only once
7091 in the original, and the subtype gets them by reference. */
7092 if (TYPE_UNSIGNED (type))
7093 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7094 else
7095 typefm = make_signed_type (TYPE_PRECISION (typefm));
7096 TREE_TYPE (typefm) = type;
7099 else if (VECTOR_MODE_P (mode)
7100 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7101 : TREE_CODE (type) != TREE_CODE (typefm))
7103 error ("mode %qs applied to inappropriate type", p);
7104 return NULL_TREE;
7107 *node = typefm;
7110 return NULL_TREE;
7113 /* Handle a "section" attribute; arguments as in
7114 struct attribute_spec.handler. */
7116 static tree
7117 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7118 int ARG_UNUSED (flags), bool *no_add_attrs)
7120 tree decl = *node;
7122 if (targetm_common.have_named_sections)
7124 user_defined_section_attribute = true;
7126 if ((TREE_CODE (decl) == FUNCTION_DECL
7127 || TREE_CODE (decl) == VAR_DECL)
7128 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7130 if (TREE_CODE (decl) == VAR_DECL
7131 && current_function_decl != NULL_TREE
7132 && !TREE_STATIC (decl))
7134 error_at (DECL_SOURCE_LOCATION (decl),
7135 "section attribute cannot be specified for "
7136 "local variables");
7137 *no_add_attrs = true;
7140 /* The decl may have already been given a section attribute
7141 from a previous declaration. Ensure they match. */
7142 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7143 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7144 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7146 error ("section of %q+D conflicts with previous declaration",
7147 *node);
7148 *no_add_attrs = true;
7150 else if (TREE_CODE (decl) == VAR_DECL
7151 && !targetm.have_tls && targetm.emutls.tmpl_section
7152 && DECL_THREAD_LOCAL_P (decl))
7154 error ("section of %q+D cannot be overridden", *node);
7155 *no_add_attrs = true;
7157 else
7158 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7160 else
7162 error ("section attribute not allowed for %q+D", *node);
7163 *no_add_attrs = true;
7166 else
7168 error_at (DECL_SOURCE_LOCATION (*node),
7169 "section attributes are not supported for this target");
7170 *no_add_attrs = true;
7173 return NULL_TREE;
7176 /* Check whether ALIGN is a valid user-specified alignment. If so,
7177 return its base-2 log; if not, output an error and return -1. If
7178 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7179 no error. */
7181 check_user_alignment (const_tree align, bool allow_zero)
7183 int i;
7185 if (!INTEGRAL_TYPE_P (TREE_TYPE (align))
7186 || TREE_CODE (align) != INTEGER_CST)
7188 error ("requested alignment is not an integer constant");
7189 return -1;
7191 else if (allow_zero && integer_zerop (align))
7192 return -1;
7193 else if ((i = tree_log2 (align)) == -1)
7195 error ("requested alignment is not a power of 2");
7196 return -1;
7198 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7200 error ("requested alignment is too large");
7201 return -1;
7203 return i;
7206 /* Handle a "aligned" attribute; arguments as in
7207 struct attribute_spec.handler. */
7209 static tree
7210 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7211 int flags, bool *no_add_attrs)
7213 tree decl = NULL_TREE;
7214 tree *type = NULL;
7215 int is_type = 0;
7216 tree align_expr = (args ? TREE_VALUE (args)
7217 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7218 int i;
7220 if (DECL_P (*node))
7222 decl = *node;
7223 type = &TREE_TYPE (decl);
7224 is_type = TREE_CODE (*node) == TYPE_DECL;
7226 else if (TYPE_P (*node))
7227 type = node, is_type = 1;
7229 if ((i = check_user_alignment (align_expr, false)) == -1)
7230 *no_add_attrs = true;
7231 else if (is_type)
7233 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7234 /* OK, modify the type in place. */;
7235 /* If we have a TYPE_DECL, then copy the type, so that we
7236 don't accidentally modify a builtin type. See pushdecl. */
7237 else if (decl && TREE_TYPE (decl) != error_mark_node
7238 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7240 tree tt = TREE_TYPE (decl);
7241 *type = build_variant_type_copy (*type);
7242 DECL_ORIGINAL_TYPE (decl) = tt;
7243 TYPE_NAME (*type) = decl;
7244 TREE_USED (*type) = TREE_USED (decl);
7245 TREE_TYPE (decl) = *type;
7247 else
7248 *type = build_variant_type_copy (*type);
7250 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7251 TYPE_USER_ALIGN (*type) = 1;
7253 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7254 && TREE_CODE (decl) != FIELD_DECL)
7256 error ("alignment may not be specified for %q+D", decl);
7257 *no_add_attrs = true;
7259 else if (TREE_CODE (decl) == FUNCTION_DECL
7260 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7262 if (DECL_USER_ALIGN (decl))
7263 error ("alignment for %q+D was previously specified as %d "
7264 "and may not be decreased", decl,
7265 DECL_ALIGN (decl) / BITS_PER_UNIT);
7266 else
7267 error ("alignment for %q+D must be at least %d", decl,
7268 DECL_ALIGN (decl) / BITS_PER_UNIT);
7269 *no_add_attrs = true;
7271 else
7273 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7274 DECL_USER_ALIGN (decl) = 1;
7277 return NULL_TREE;
7280 /* Handle a "weak" attribute; arguments as in
7281 struct attribute_spec.handler. */
7283 static tree
7284 handle_weak_attribute (tree *node, tree name,
7285 tree ARG_UNUSED (args),
7286 int ARG_UNUSED (flags),
7287 bool * ARG_UNUSED (no_add_attrs))
7289 if (TREE_CODE (*node) == FUNCTION_DECL
7290 && DECL_DECLARED_INLINE_P (*node))
7292 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7293 *no_add_attrs = true;
7295 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7297 error ("indirect function %q+D cannot be declared weak", *node);
7298 *no_add_attrs = true;
7299 return NULL_TREE;
7301 else if (TREE_CODE (*node) == FUNCTION_DECL
7302 || TREE_CODE (*node) == VAR_DECL)
7303 declare_weak (*node);
7304 else
7305 warning (OPT_Wattributes, "%qE attribute ignored", name);
7307 return NULL_TREE;
7310 /* Handle an "alias" or "ifunc" attribute; arguments as in
7311 struct attribute_spec.handler, except that IS_ALIAS tells us
7312 whether this is an alias as opposed to ifunc attribute. */
7314 static tree
7315 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7316 bool *no_add_attrs)
7318 tree decl = *node;
7320 if (TREE_CODE (decl) != FUNCTION_DECL
7321 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7323 warning (OPT_Wattributes, "%qE attribute ignored", name);
7324 *no_add_attrs = true;
7326 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7327 || (TREE_CODE (decl) != FUNCTION_DECL
7328 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7329 /* A static variable declaration is always a tentative definition,
7330 but the alias is a non-tentative definition which overrides. */
7331 || (TREE_CODE (decl) != FUNCTION_DECL
7332 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7334 error ("%q+D defined both normally and as %qE attribute", decl, name);
7335 *no_add_attrs = true;
7336 return NULL_TREE;
7338 else if (!is_alias
7339 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7340 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7342 error ("weak %q+D cannot be defined %qE", decl, name);
7343 *no_add_attrs = true;
7344 return NULL_TREE;
7347 /* Note that the very first time we process a nested declaration,
7348 decl_function_context will not be set. Indeed, *would* never
7349 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7350 we do below. After such frobbery, pushdecl would set the context.
7351 In any case, this is never what we want. */
7352 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7354 tree id;
7356 id = TREE_VALUE (args);
7357 if (TREE_CODE (id) != STRING_CST)
7359 error ("attribute %qE argument not a string", name);
7360 *no_add_attrs = true;
7361 return NULL_TREE;
7363 id = get_identifier (TREE_STRING_POINTER (id));
7364 /* This counts as a use of the object pointed to. */
7365 TREE_USED (id) = 1;
7367 if (TREE_CODE (decl) == FUNCTION_DECL)
7368 DECL_INITIAL (decl) = error_mark_node;
7369 else
7371 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7372 DECL_EXTERNAL (decl) = 1;
7373 else
7374 DECL_EXTERNAL (decl) = 0;
7375 TREE_STATIC (decl) = 1;
7378 if (!is_alias)
7379 /* ifuncs are also aliases, so set that attribute too. */
7380 DECL_ATTRIBUTES (decl)
7381 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7383 else
7385 warning (OPT_Wattributes, "%qE attribute ignored", name);
7386 *no_add_attrs = true;
7389 return NULL_TREE;
7392 /* Handle an "alias" or "ifunc" attribute; arguments as in
7393 struct attribute_spec.handler. */
7395 static tree
7396 handle_ifunc_attribute (tree *node, tree name, tree args,
7397 int ARG_UNUSED (flags), bool *no_add_attrs)
7399 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7402 /* Handle an "alias" or "ifunc" attribute; arguments as in
7403 struct attribute_spec.handler. */
7405 static tree
7406 handle_alias_attribute (tree *node, tree name, tree args,
7407 int ARG_UNUSED (flags), bool *no_add_attrs)
7409 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7412 /* Handle a "weakref" attribute; arguments as in struct
7413 attribute_spec.handler. */
7415 static tree
7416 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7417 int flags, bool *no_add_attrs)
7419 tree attr = NULL_TREE;
7421 /* We must ignore the attribute when it is associated with
7422 local-scoped decls, since attribute alias is ignored and many
7423 such symbols do not even have a DECL_WEAK field. */
7424 if (decl_function_context (*node)
7425 || current_function_decl
7426 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7428 warning (OPT_Wattributes, "%qE attribute ignored", name);
7429 *no_add_attrs = true;
7430 return NULL_TREE;
7433 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7435 error ("indirect function %q+D cannot be declared weakref", *node);
7436 *no_add_attrs = true;
7437 return NULL_TREE;
7440 /* The idea here is that `weakref("name")' mutates into `weakref,
7441 alias("name")', and weakref without arguments, in turn,
7442 implicitly adds weak. */
7444 if (args)
7446 attr = tree_cons (get_identifier ("alias"), args, attr);
7447 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7449 *no_add_attrs = true;
7451 decl_attributes (node, attr, flags);
7453 else
7455 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7456 error_at (DECL_SOURCE_LOCATION (*node),
7457 "weakref attribute must appear before alias attribute");
7459 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7460 and that isn't supported; and because it wants to add it to
7461 the list of weak decls, which isn't helpful. */
7462 DECL_WEAK (*node) = 1;
7465 return NULL_TREE;
7468 /* Handle an "visibility" attribute; arguments as in
7469 struct attribute_spec.handler. */
7471 static tree
7472 handle_visibility_attribute (tree *node, tree name, tree args,
7473 int ARG_UNUSED (flags),
7474 bool *ARG_UNUSED (no_add_attrs))
7476 tree decl = *node;
7477 tree id = TREE_VALUE (args);
7478 enum symbol_visibility vis;
7480 if (TYPE_P (*node))
7482 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7483 /* OK */;
7484 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7486 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7487 name);
7488 return NULL_TREE;
7490 else if (TYPE_FIELDS (*node))
7492 error ("%qE attribute ignored because %qT is already defined",
7493 name, *node);
7494 return NULL_TREE;
7497 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7499 warning (OPT_Wattributes, "%qE attribute ignored", name);
7500 return NULL_TREE;
7503 if (TREE_CODE (id) != STRING_CST)
7505 error ("visibility argument not a string");
7506 return NULL_TREE;
7509 /* If this is a type, set the visibility on the type decl. */
7510 if (TYPE_P (decl))
7512 decl = TYPE_NAME (decl);
7513 if (!decl)
7514 return NULL_TREE;
7515 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7517 warning (OPT_Wattributes, "%qE attribute ignored on types",
7518 name);
7519 return NULL_TREE;
7523 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7524 vis = VISIBILITY_DEFAULT;
7525 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7526 vis = VISIBILITY_INTERNAL;
7527 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7528 vis = VISIBILITY_HIDDEN;
7529 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7530 vis = VISIBILITY_PROTECTED;
7531 else
7533 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7534 vis = VISIBILITY_DEFAULT;
7537 if (DECL_VISIBILITY_SPECIFIED (decl)
7538 && vis != DECL_VISIBILITY (decl))
7540 tree attributes = (TYPE_P (*node)
7541 ? TYPE_ATTRIBUTES (*node)
7542 : DECL_ATTRIBUTES (decl));
7543 if (lookup_attribute ("visibility", attributes))
7544 error ("%qD redeclared with different visibility", decl);
7545 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7546 && lookup_attribute ("dllimport", attributes))
7547 error ("%qD was declared %qs which implies default visibility",
7548 decl, "dllimport");
7549 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7550 && lookup_attribute ("dllexport", attributes))
7551 error ("%qD was declared %qs which implies default visibility",
7552 decl, "dllexport");
7555 DECL_VISIBILITY (decl) = vis;
7556 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7558 /* Go ahead and attach the attribute to the node as well. This is needed
7559 so we can determine whether we have VISIBILITY_DEFAULT because the
7560 visibility was not specified, or because it was explicitly overridden
7561 from the containing scope. */
7563 return NULL_TREE;
7566 /* Determine the ELF symbol visibility for DECL, which is either a
7567 variable or a function. It is an error to use this function if a
7568 definition of DECL is not available in this translation unit.
7569 Returns true if the final visibility has been determined by this
7570 function; false if the caller is free to make additional
7571 modifications. */
7573 bool
7574 c_determine_visibility (tree decl)
7576 gcc_assert (TREE_CODE (decl) == VAR_DECL
7577 || TREE_CODE (decl) == FUNCTION_DECL);
7579 /* If the user explicitly specified the visibility with an
7580 attribute, honor that. DECL_VISIBILITY will have been set during
7581 the processing of the attribute. We check for an explicit
7582 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7583 to distinguish the use of an attribute from the use of a "#pragma
7584 GCC visibility push(...)"; in the latter case we still want other
7585 considerations to be able to overrule the #pragma. */
7586 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7587 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7588 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7589 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7590 return true;
7592 /* Set default visibility to whatever the user supplied with
7593 visibility_specified depending on #pragma GCC visibility. */
7594 if (!DECL_VISIBILITY_SPECIFIED (decl))
7596 if (visibility_options.inpragma
7597 || DECL_VISIBILITY (decl) != default_visibility)
7599 DECL_VISIBILITY (decl) = default_visibility;
7600 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7601 /* If visibility changed and DECL already has DECL_RTL, ensure
7602 symbol flags are updated. */
7603 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7604 || TREE_CODE (decl) == FUNCTION_DECL)
7605 && DECL_RTL_SET_P (decl))
7606 make_decl_rtl (decl);
7609 return false;
7612 /* Handle an "tls_model" attribute; arguments as in
7613 struct attribute_spec.handler. */
7615 static tree
7616 handle_tls_model_attribute (tree *node, tree name, tree args,
7617 int ARG_UNUSED (flags), bool *no_add_attrs)
7619 tree id;
7620 tree decl = *node;
7621 enum tls_model kind;
7623 *no_add_attrs = true;
7625 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7627 warning (OPT_Wattributes, "%qE attribute ignored", name);
7628 return NULL_TREE;
7631 kind = DECL_TLS_MODEL (decl);
7632 id = TREE_VALUE (args);
7633 if (TREE_CODE (id) != STRING_CST)
7635 error ("tls_model argument not a string");
7636 return NULL_TREE;
7639 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7640 kind = TLS_MODEL_LOCAL_EXEC;
7641 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7642 kind = TLS_MODEL_INITIAL_EXEC;
7643 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7644 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7645 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7646 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7647 else
7648 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7650 DECL_TLS_MODEL (decl) = kind;
7651 return NULL_TREE;
7654 /* Handle a "no_instrument_function" attribute; arguments as in
7655 struct attribute_spec.handler. */
7657 static tree
7658 handle_no_instrument_function_attribute (tree *node, tree name,
7659 tree ARG_UNUSED (args),
7660 int ARG_UNUSED (flags),
7661 bool *no_add_attrs)
7663 tree decl = *node;
7665 if (TREE_CODE (decl) != FUNCTION_DECL)
7667 error_at (DECL_SOURCE_LOCATION (decl),
7668 "%qE attribute applies only to functions", name);
7669 *no_add_attrs = true;
7671 else if (DECL_INITIAL (decl))
7673 error_at (DECL_SOURCE_LOCATION (decl),
7674 "can%'t set %qE attribute after definition", name);
7675 *no_add_attrs = true;
7677 else
7678 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7680 return NULL_TREE;
7683 /* Handle a "malloc" attribute; arguments as in
7684 struct attribute_spec.handler. */
7686 static tree
7687 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7688 int ARG_UNUSED (flags), bool *no_add_attrs)
7690 if (TREE_CODE (*node) == FUNCTION_DECL
7691 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7692 DECL_IS_MALLOC (*node) = 1;
7693 else
7695 warning (OPT_Wattributes, "%qE attribute ignored", name);
7696 *no_add_attrs = true;
7699 return NULL_TREE;
7702 /* Handle a "alloc_size" attribute; arguments as in
7703 struct attribute_spec.handler. */
7705 static tree
7706 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7707 int ARG_UNUSED (flags), bool *no_add_attrs)
7709 unsigned arg_count = type_num_arguments (*node);
7710 for (; args; args = TREE_CHAIN (args))
7712 tree position = TREE_VALUE (args);
7714 if (TREE_CODE (position) != INTEGER_CST
7715 || TREE_INT_CST_HIGH (position)
7716 || TREE_INT_CST_LOW (position) < 1
7717 || TREE_INT_CST_LOW (position) > arg_count )
7719 warning (OPT_Wattributes,
7720 "alloc_size parameter outside range");
7721 *no_add_attrs = true;
7722 return NULL_TREE;
7725 return NULL_TREE;
7728 /* Handle a "fn spec" attribute; arguments as in
7729 struct attribute_spec.handler. */
7731 static tree
7732 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7733 tree args, int ARG_UNUSED (flags),
7734 bool *no_add_attrs ATTRIBUTE_UNUSED)
7736 gcc_assert (args
7737 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7738 && !TREE_CHAIN (args));
7739 return NULL_TREE;
7742 /* Handle a "returns_twice" attribute; arguments as in
7743 struct attribute_spec.handler. */
7745 static tree
7746 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7747 int ARG_UNUSED (flags), bool *no_add_attrs)
7749 if (TREE_CODE (*node) == FUNCTION_DECL)
7750 DECL_IS_RETURNS_TWICE (*node) = 1;
7751 else
7753 warning (OPT_Wattributes, "%qE attribute ignored", name);
7754 *no_add_attrs = true;
7757 return NULL_TREE;
7760 /* Handle a "no_limit_stack" attribute; arguments as in
7761 struct attribute_spec.handler. */
7763 static tree
7764 handle_no_limit_stack_attribute (tree *node, tree name,
7765 tree ARG_UNUSED (args),
7766 int ARG_UNUSED (flags),
7767 bool *no_add_attrs)
7769 tree decl = *node;
7771 if (TREE_CODE (decl) != FUNCTION_DECL)
7773 error_at (DECL_SOURCE_LOCATION (decl),
7774 "%qE attribute applies only to functions", name);
7775 *no_add_attrs = true;
7777 else if (DECL_INITIAL (decl))
7779 error_at (DECL_SOURCE_LOCATION (decl),
7780 "can%'t set %qE attribute after definition", name);
7781 *no_add_attrs = true;
7783 else
7784 DECL_NO_LIMIT_STACK (decl) = 1;
7786 return NULL_TREE;
7789 /* Handle a "pure" attribute; arguments as in
7790 struct attribute_spec.handler. */
7792 static tree
7793 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7794 int ARG_UNUSED (flags), bool *no_add_attrs)
7796 if (TREE_CODE (*node) == FUNCTION_DECL)
7797 DECL_PURE_P (*node) = 1;
7798 /* ??? TODO: Support types. */
7799 else
7801 warning (OPT_Wattributes, "%qE attribute ignored", name);
7802 *no_add_attrs = true;
7805 return NULL_TREE;
7808 /* Digest an attribute list destined for a transactional memory statement.
7809 ALLOWED is the set of attributes that are allowed for this statement;
7810 return the attribute we parsed. Multiple attributes are never allowed. */
7813 parse_tm_stmt_attr (tree attrs, int allowed)
7815 tree a_seen = NULL;
7816 int m_seen = 0;
7818 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
7820 tree a = TREE_PURPOSE (attrs);
7821 int m = 0;
7823 if (is_attribute_p ("outer", a))
7824 m = TM_STMT_ATTR_OUTER;
7826 if ((m & allowed) == 0)
7828 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
7829 continue;
7832 if (m_seen == 0)
7834 a_seen = a;
7835 m_seen = m;
7837 else if (m_seen == m)
7838 warning (OPT_Wattributes, "%qE attribute duplicated", a);
7839 else
7840 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
7843 return m_seen;
7846 /* Transform a TM attribute name into a maskable integer and back.
7847 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
7848 to how the lack of an attribute is treated. */
7851 tm_attr_to_mask (tree attr)
7853 if (attr == NULL)
7854 return 0;
7855 if (is_attribute_p ("transaction_safe", attr))
7856 return TM_ATTR_SAFE;
7857 if (is_attribute_p ("transaction_callable", attr))
7858 return TM_ATTR_CALLABLE;
7859 if (is_attribute_p ("transaction_pure", attr))
7860 return TM_ATTR_PURE;
7861 if (is_attribute_p ("transaction_unsafe", attr))
7862 return TM_ATTR_IRREVOCABLE;
7863 if (is_attribute_p ("transaction_may_cancel_outer", attr))
7864 return TM_ATTR_MAY_CANCEL_OUTER;
7865 return 0;
7868 tree
7869 tm_mask_to_attr (int mask)
7871 const char *str;
7872 switch (mask)
7874 case TM_ATTR_SAFE:
7875 str = "transaction_safe";
7876 break;
7877 case TM_ATTR_CALLABLE:
7878 str = "transaction_callable";
7879 break;
7880 case TM_ATTR_PURE:
7881 str = "transaction_pure";
7882 break;
7883 case TM_ATTR_IRREVOCABLE:
7884 str = "transaction_unsafe";
7885 break;
7886 case TM_ATTR_MAY_CANCEL_OUTER:
7887 str = "transaction_may_cancel_outer";
7888 break;
7889 default:
7890 gcc_unreachable ();
7892 return get_identifier (str);
7895 /* Return the first TM attribute seen in LIST. */
7897 tree
7898 find_tm_attribute (tree list)
7900 for (; list ; list = TREE_CHAIN (list))
7902 tree name = TREE_PURPOSE (list);
7903 if (tm_attr_to_mask (name) != 0)
7904 return name;
7906 return NULL_TREE;
7909 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
7910 Here we accept only function types, and verify that none of the other
7911 function TM attributes are also applied. */
7912 /* ??? We need to accept class types for C++, but not C. This greatly
7913 complicates this function, since we can no longer rely on the extra
7914 processing given by function_type_required. */
7916 static tree
7917 handle_tm_attribute (tree *node, tree name, tree args,
7918 int flags, bool *no_add_attrs)
7920 /* Only one path adds the attribute; others don't. */
7921 *no_add_attrs = true;
7923 switch (TREE_CODE (*node))
7925 case RECORD_TYPE:
7926 case UNION_TYPE:
7927 /* Only tm_callable and tm_safe apply to classes. */
7928 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
7929 goto ignored;
7930 /* FALLTHRU */
7932 case FUNCTION_TYPE:
7933 case METHOD_TYPE:
7935 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
7936 if (old_name == name)
7938 else if (old_name != NULL_TREE)
7939 error ("type was previously declared %qE", old_name);
7940 else
7941 *no_add_attrs = false;
7943 break;
7945 case POINTER_TYPE:
7947 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
7948 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
7950 tree fn_tmp = TREE_TYPE (*node);
7951 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
7952 *node = build_pointer_type (fn_tmp);
7953 break;
7956 /* FALLTHRU */
7958 default:
7959 /* If a function is next, pass it on to be tried next. */
7960 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
7961 return tree_cons (name, args, NULL);
7963 ignored:
7964 warning (OPT_Wattributes, "%qE attribute ignored", name);
7965 break;
7968 return NULL_TREE;
7971 /* Handle the TM_WRAP attribute; arguments as in
7972 struct attribute_spec.handler. */
7974 static tree
7975 handle_tm_wrap_attribute (tree *node, tree name, tree args,
7976 int ARG_UNUSED (flags), bool *no_add_attrs)
7978 tree decl = *node;
7980 /* We don't need the attribute even on success, since we
7981 record the entry in an external table. */
7982 *no_add_attrs = true;
7984 if (TREE_CODE (decl) != FUNCTION_DECL)
7985 warning (OPT_Wattributes, "%qE attribute ignored", name);
7986 else
7988 tree wrap_decl = TREE_VALUE (args);
7989 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
7990 && TREE_CODE (wrap_decl) != VAR_DECL
7991 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
7992 error ("%qE argument not an identifier", name);
7993 else
7995 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
7996 wrap_decl = lookup_name (wrap_decl);
7997 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
7999 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8000 TREE_TYPE (wrap_decl)))
8001 record_tm_replacement (wrap_decl, decl);
8002 else
8003 error ("%qD is not compatible with %qD", wrap_decl, decl);
8005 else
8006 error ("transaction_wrap argument is not a function");
8010 return NULL_TREE;
8013 /* Ignore the given attribute. Used when this attribute may be usefully
8014 overridden by the target, but is not used generically. */
8016 static tree
8017 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8018 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8019 bool *no_add_attrs)
8021 *no_add_attrs = true;
8022 return NULL_TREE;
8025 /* Handle a "no vops" attribute; arguments as in
8026 struct attribute_spec.handler. */
8028 static tree
8029 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8030 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8031 bool *ARG_UNUSED (no_add_attrs))
8033 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8034 DECL_IS_NOVOPS (*node) = 1;
8035 return NULL_TREE;
8038 /* Handle a "deprecated" attribute; arguments as in
8039 struct attribute_spec.handler. */
8041 static tree
8042 handle_deprecated_attribute (tree *node, tree name,
8043 tree args, int flags,
8044 bool *no_add_attrs)
8046 tree type = NULL_TREE;
8047 int warn = 0;
8048 tree what = NULL_TREE;
8050 if (!args)
8051 *no_add_attrs = true;
8052 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8054 error ("deprecated message is not a string");
8055 *no_add_attrs = true;
8058 if (DECL_P (*node))
8060 tree decl = *node;
8061 type = TREE_TYPE (decl);
8063 if (TREE_CODE (decl) == TYPE_DECL
8064 || TREE_CODE (decl) == PARM_DECL
8065 || TREE_CODE (decl) == VAR_DECL
8066 || TREE_CODE (decl) == FUNCTION_DECL
8067 || TREE_CODE (decl) == FIELD_DECL
8068 || objc_method_decl (TREE_CODE (decl)))
8069 TREE_DEPRECATED (decl) = 1;
8070 else
8071 warn = 1;
8073 else if (TYPE_P (*node))
8075 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8076 *node = build_variant_type_copy (*node);
8077 TREE_DEPRECATED (*node) = 1;
8078 type = *node;
8080 else
8081 warn = 1;
8083 if (warn)
8085 *no_add_attrs = true;
8086 if (type && TYPE_NAME (type))
8088 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8089 what = TYPE_NAME (*node);
8090 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8091 && DECL_NAME (TYPE_NAME (type)))
8092 what = DECL_NAME (TYPE_NAME (type));
8094 if (what)
8095 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8096 else
8097 warning (OPT_Wattributes, "%qE attribute ignored", name);
8100 return NULL_TREE;
8103 /* Handle a "vector_size" attribute; arguments as in
8104 struct attribute_spec.handler. */
8106 static tree
8107 handle_vector_size_attribute (tree *node, tree name, tree args,
8108 int ARG_UNUSED (flags),
8109 bool *no_add_attrs)
8111 unsigned HOST_WIDE_INT vecsize, nunits;
8112 enum machine_mode orig_mode;
8113 tree type = *node, new_type, size;
8115 *no_add_attrs = true;
8117 size = TREE_VALUE (args);
8119 if (!host_integerp (size, 1))
8121 warning (OPT_Wattributes, "%qE attribute ignored", name);
8122 return NULL_TREE;
8125 /* Get the vector size (in bytes). */
8126 vecsize = tree_low_cst (size, 1);
8128 /* We need to provide for vector pointers, vector arrays, and
8129 functions returning vectors. For example:
8131 __attribute__((vector_size(16))) short *foo;
8133 In this case, the mode is SI, but the type being modified is
8134 HI, so we need to look further. */
8136 while (POINTER_TYPE_P (type)
8137 || TREE_CODE (type) == FUNCTION_TYPE
8138 || TREE_CODE (type) == METHOD_TYPE
8139 || TREE_CODE (type) == ARRAY_TYPE
8140 || TREE_CODE (type) == OFFSET_TYPE)
8141 type = TREE_TYPE (type);
8143 /* Get the mode of the type being modified. */
8144 orig_mode = TYPE_MODE (type);
8146 if ((!INTEGRAL_TYPE_P (type)
8147 && !SCALAR_FLOAT_TYPE_P (type)
8148 && !FIXED_POINT_TYPE_P (type))
8149 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8150 && GET_MODE_CLASS (orig_mode) != MODE_INT
8151 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8152 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8153 || TREE_CODE (type) == BOOLEAN_TYPE)
8155 error ("invalid vector type for attribute %qE", name);
8156 return NULL_TREE;
8159 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8161 error ("vector size not an integral multiple of component size");
8162 return NULL;
8165 if (vecsize == 0)
8167 error ("zero vector size");
8168 return NULL;
8171 /* Calculate how many units fit in the vector. */
8172 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8173 if (nunits & (nunits - 1))
8175 error ("number of components of the vector not a power of two");
8176 return NULL_TREE;
8179 new_type = build_vector_type (type, nunits);
8181 /* Build back pointers if needed. */
8182 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8184 return NULL_TREE;
8187 /* Handle the "nonnull" attribute. */
8188 static tree
8189 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8190 tree args, int ARG_UNUSED (flags),
8191 bool *no_add_attrs)
8193 tree type = *node;
8194 unsigned HOST_WIDE_INT attr_arg_num;
8196 /* If no arguments are specified, all pointer arguments should be
8197 non-null. Verify a full prototype is given so that the arguments
8198 will have the correct types when we actually check them later. */
8199 if (!args)
8201 if (!prototype_p (type))
8203 error ("nonnull attribute without arguments on a non-prototype");
8204 *no_add_attrs = true;
8206 return NULL_TREE;
8209 /* Argument list specified. Verify that each argument number references
8210 a pointer argument. */
8211 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8213 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8215 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8217 error ("nonnull argument has invalid operand number (argument %lu)",
8218 (unsigned long) attr_arg_num);
8219 *no_add_attrs = true;
8220 return NULL_TREE;
8223 if (prototype_p (type))
8225 function_args_iterator iter;
8226 tree argument;
8228 function_args_iter_init (&iter, type);
8229 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8231 argument = function_args_iter_cond (&iter);
8232 if (argument == NULL_TREE || ck_num == arg_num)
8233 break;
8236 if (!argument
8237 || TREE_CODE (argument) == VOID_TYPE)
8239 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8240 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8241 *no_add_attrs = true;
8242 return NULL_TREE;
8245 if (TREE_CODE (argument) != POINTER_TYPE)
8247 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8248 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8249 *no_add_attrs = true;
8250 return NULL_TREE;
8255 return NULL_TREE;
8258 /* Check the argument list of a function call for null in argument slots
8259 that are marked as requiring a non-null pointer argument. The NARGS
8260 arguments are passed in the array ARGARRAY.
8263 static void
8264 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8266 tree a;
8267 int i;
8269 attrs = lookup_attribute ("nonnull", attrs);
8270 if (attrs == NULL_TREE)
8271 return;
8273 a = attrs;
8274 /* See if any of the nonnull attributes has no arguments. If so,
8275 then every pointer argument is checked (in which case the check
8276 for pointer type is done in check_nonnull_arg). */
8277 if (TREE_VALUE (a) != NULL_TREE)
8279 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8280 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8282 if (a != NULL_TREE)
8283 for (i = 0; i < nargs; i++)
8284 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8285 i + 1);
8286 else
8288 /* Walk the argument list. If we encounter an argument number we
8289 should check for non-null, do it. */
8290 for (i = 0; i < nargs; i++)
8292 for (a = attrs; ; a = TREE_CHAIN (a))
8294 a = lookup_attribute ("nonnull", a);
8295 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8296 break;
8299 if (a != NULL_TREE)
8300 check_function_arguments_recurse (check_nonnull_arg, NULL,
8301 argarray[i], i + 1);
8306 /* Check that the Nth argument of a function call (counting backwards
8307 from the end) is a (pointer)0. The NARGS arguments are passed in the
8308 array ARGARRAY. */
8310 static void
8311 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8313 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8315 if (attr)
8317 int len = 0;
8318 int pos = 0;
8319 tree sentinel;
8320 function_args_iterator iter;
8321 tree t;
8323 /* Skip over the named arguments. */
8324 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8326 if (len == nargs)
8327 break;
8328 len++;
8331 if (TREE_VALUE (attr))
8333 tree p = TREE_VALUE (TREE_VALUE (attr));
8334 pos = TREE_INT_CST_LOW (p);
8337 /* The sentinel must be one of the varargs, i.e.
8338 in position >= the number of fixed arguments. */
8339 if ((nargs - 1 - pos) < len)
8341 warning (OPT_Wformat,
8342 "not enough variable arguments to fit a sentinel");
8343 return;
8346 /* Validate the sentinel. */
8347 sentinel = argarray[nargs - 1 - pos];
8348 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8349 || !integer_zerop (sentinel))
8350 /* Although __null (in C++) is only an integer we allow it
8351 nevertheless, as we are guaranteed that it's exactly
8352 as wide as a pointer, and we don't want to force
8353 users to cast the NULL they have written there.
8354 We warn with -Wstrict-null-sentinel, though. */
8355 && (warn_strict_null_sentinel || null_node != sentinel))
8356 warning (OPT_Wformat, "missing sentinel in function call");
8360 /* Helper for check_function_nonnull; given a list of operands which
8361 must be non-null in ARGS, determine if operand PARAM_NUM should be
8362 checked. */
8364 static bool
8365 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8367 unsigned HOST_WIDE_INT arg_num = 0;
8369 for (; args; args = TREE_CHAIN (args))
8371 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8373 gcc_assert (found);
8375 if (arg_num == param_num)
8376 return true;
8378 return false;
8381 /* Check that the function argument PARAM (which is operand number
8382 PARAM_NUM) is non-null. This is called by check_function_nonnull
8383 via check_function_arguments_recurse. */
8385 static void
8386 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8387 unsigned HOST_WIDE_INT param_num)
8389 /* Just skip checking the argument if it's not a pointer. This can
8390 happen if the "nonnull" attribute was given without an operand
8391 list (which means to check every pointer argument). */
8393 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8394 return;
8396 if (integer_zerop (param))
8397 warning (OPT_Wnonnull, "null argument where non-null required "
8398 "(argument %lu)", (unsigned long) param_num);
8401 /* Helper for nonnull attribute handling; fetch the operand number
8402 from the attribute argument list. */
8404 static bool
8405 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8407 /* Verify the arg number is a constant. */
8408 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8409 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8410 return false;
8412 *valp = TREE_INT_CST_LOW (arg_num_expr);
8413 return true;
8416 /* Handle a "nothrow" attribute; arguments as in
8417 struct attribute_spec.handler. */
8419 static tree
8420 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8421 int ARG_UNUSED (flags), bool *no_add_attrs)
8423 if (TREE_CODE (*node) == FUNCTION_DECL)
8424 TREE_NOTHROW (*node) = 1;
8425 /* ??? TODO: Support types. */
8426 else
8428 warning (OPT_Wattributes, "%qE attribute ignored", name);
8429 *no_add_attrs = true;
8432 return NULL_TREE;
8435 /* Handle a "cleanup" attribute; arguments as in
8436 struct attribute_spec.handler. */
8438 static tree
8439 handle_cleanup_attribute (tree *node, tree name, tree args,
8440 int ARG_UNUSED (flags), bool *no_add_attrs)
8442 tree decl = *node;
8443 tree cleanup_id, cleanup_decl;
8445 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8446 for global destructors in C++. This requires infrastructure that
8447 we don't have generically at the moment. It's also not a feature
8448 we'd be missing too much, since we do have attribute constructor. */
8449 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8451 warning (OPT_Wattributes, "%qE attribute ignored", name);
8452 *no_add_attrs = true;
8453 return NULL_TREE;
8456 /* Verify that the argument is a function in scope. */
8457 /* ??? We could support pointers to functions here as well, if
8458 that was considered desirable. */
8459 cleanup_id = TREE_VALUE (args);
8460 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8462 error ("cleanup argument not an identifier");
8463 *no_add_attrs = true;
8464 return NULL_TREE;
8466 cleanup_decl = lookup_name (cleanup_id);
8467 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8469 error ("cleanup argument not a function");
8470 *no_add_attrs = true;
8471 return NULL_TREE;
8474 /* That the function has proper type is checked with the
8475 eventual call to build_function_call. */
8477 return NULL_TREE;
8480 /* Handle a "warn_unused_result" attribute. No special handling. */
8482 static tree
8483 handle_warn_unused_result_attribute (tree *node, tree name,
8484 tree ARG_UNUSED (args),
8485 int ARG_UNUSED (flags), bool *no_add_attrs)
8487 /* Ignore the attribute for functions not returning any value. */
8488 if (VOID_TYPE_P (TREE_TYPE (*node)))
8490 warning (OPT_Wattributes, "%qE attribute ignored", name);
8491 *no_add_attrs = true;
8494 return NULL_TREE;
8497 /* Handle a "sentinel" attribute. */
8499 static tree
8500 handle_sentinel_attribute (tree *node, tree name, tree args,
8501 int ARG_UNUSED (flags), bool *no_add_attrs)
8503 if (!prototype_p (*node))
8505 warning (OPT_Wattributes,
8506 "%qE attribute requires prototypes with named arguments", name);
8507 *no_add_attrs = true;
8509 else
8511 if (!stdarg_p (*node))
8513 warning (OPT_Wattributes,
8514 "%qE attribute only applies to variadic functions", name);
8515 *no_add_attrs = true;
8519 if (args)
8521 tree position = TREE_VALUE (args);
8523 if (TREE_CODE (position) != INTEGER_CST)
8525 warning (OPT_Wattributes,
8526 "requested position is not an integer constant");
8527 *no_add_attrs = true;
8529 else
8531 if (tree_int_cst_lt (position, integer_zero_node))
8533 warning (OPT_Wattributes,
8534 "requested position is less than zero");
8535 *no_add_attrs = true;
8540 return NULL_TREE;
8543 /* Handle a "type_generic" attribute. */
8545 static tree
8546 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8547 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8548 bool * ARG_UNUSED (no_add_attrs))
8550 /* Ensure we have a function type. */
8551 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8553 /* Ensure we have a variadic function. */
8554 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8556 return NULL_TREE;
8559 /* Handle a "target" attribute. */
8561 static tree
8562 handle_target_attribute (tree *node, tree name, tree args, int flags,
8563 bool *no_add_attrs)
8565 /* Ensure we have a function type. */
8566 if (TREE_CODE (*node) != FUNCTION_DECL)
8568 warning (OPT_Wattributes, "%qE attribute ignored", name);
8569 *no_add_attrs = true;
8571 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8572 flags))
8573 *no_add_attrs = true;
8575 return NULL_TREE;
8578 /* Arguments being collected for optimization. */
8579 typedef const char *const_char_p; /* For DEF_VEC_P. */
8580 DEF_VEC_P(const_char_p);
8581 DEF_VEC_ALLOC_P(const_char_p, gc);
8582 static GTY(()) VEC(const_char_p, gc) *optimize_args;
8585 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8586 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8587 false for #pragma GCC optimize. */
8589 bool
8590 parse_optimize_options (tree args, bool attr_p)
8592 bool ret = true;
8593 unsigned opt_argc;
8594 unsigned i;
8595 int saved_flag_strict_aliasing;
8596 const char **opt_argv;
8597 struct cl_decoded_option *decoded_options;
8598 unsigned int decoded_options_count;
8599 tree ap;
8601 /* Build up argv vector. Just in case the string is stored away, use garbage
8602 collected strings. */
8603 VEC_truncate (const_char_p, optimize_args, 0);
8604 VEC_safe_push (const_char_p, gc, optimize_args, (const_char_p)NULL);
8606 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8608 tree value = TREE_VALUE (ap);
8610 if (TREE_CODE (value) == INTEGER_CST)
8612 char buffer[20];
8613 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8614 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
8617 else if (TREE_CODE (value) == STRING_CST)
8619 /* Split string into multiple substrings. */
8620 size_t len = TREE_STRING_LENGTH (value);
8621 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8622 char *end = p + len;
8623 char *comma;
8624 char *next_p = p;
8626 while (next_p != NULL)
8628 size_t len2;
8629 char *q, *r;
8631 p = next_p;
8632 comma = strchr (p, ',');
8633 if (comma)
8635 len2 = comma - p;
8636 *comma = '\0';
8637 next_p = comma+1;
8639 else
8641 len2 = end - p;
8642 next_p = NULL;
8645 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8647 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8648 options. */
8649 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8651 ret = false;
8652 if (attr_p)
8653 warning (OPT_Wattributes,
8654 "bad option %s to optimize attribute", p);
8655 else
8656 warning (OPT_Wpragmas,
8657 "bad option %s to pragma attribute", p);
8658 continue;
8661 if (*p != '-')
8663 *r++ = '-';
8665 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8666 itself is -Os, and any other switch begins with a -f. */
8667 if ((*p >= '0' && *p <= '9')
8668 || (p[0] == 's' && p[1] == '\0'))
8669 *r++ = 'O';
8670 else if (*p != 'O')
8671 *r++ = 'f';
8674 memcpy (r, p, len2);
8675 r[len2] = '\0';
8676 VEC_safe_push (const_char_p, gc, optimize_args, q);
8682 opt_argc = VEC_length (const_char_p, optimize_args);
8683 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8685 for (i = 1; i < opt_argc; i++)
8686 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
8688 saved_flag_strict_aliasing = flag_strict_aliasing;
8690 /* Now parse the options. */
8691 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8692 &decoded_options,
8693 &decoded_options_count);
8694 decode_options (&global_options, &global_options_set,
8695 decoded_options, decoded_options_count,
8696 input_location, global_dc);
8698 targetm.override_options_after_change();
8700 /* Don't allow changing -fstrict-aliasing. */
8701 flag_strict_aliasing = saved_flag_strict_aliasing;
8703 VEC_truncate (const_char_p, optimize_args, 0);
8704 return ret;
8707 /* For handling "optimize" attribute. arguments as in
8708 struct attribute_spec.handler. */
8710 static tree
8711 handle_optimize_attribute (tree *node, tree name, tree args,
8712 int ARG_UNUSED (flags), bool *no_add_attrs)
8714 /* Ensure we have a function type. */
8715 if (TREE_CODE (*node) != FUNCTION_DECL)
8717 warning (OPT_Wattributes, "%qE attribute ignored", name);
8718 *no_add_attrs = true;
8720 else
8722 struct cl_optimization cur_opts;
8723 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8725 /* Save current options. */
8726 cl_optimization_save (&cur_opts, &global_options);
8728 /* If we previously had some optimization options, use them as the
8729 default. */
8730 if (old_opts)
8731 cl_optimization_restore (&global_options,
8732 TREE_OPTIMIZATION (old_opts));
8734 /* Parse options, and update the vector. */
8735 parse_optimize_options (args, true);
8736 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8737 = build_optimization_node ();
8739 /* Restore current options. */
8740 cl_optimization_restore (&global_options, &cur_opts);
8743 return NULL_TREE;
8746 /* Handle a "no_split_stack" attribute. */
8748 static tree
8749 handle_no_split_stack_attribute (tree *node, tree name,
8750 tree ARG_UNUSED (args),
8751 int ARG_UNUSED (flags),
8752 bool *no_add_attrs)
8754 tree decl = *node;
8756 if (TREE_CODE (decl) != FUNCTION_DECL)
8758 error_at (DECL_SOURCE_LOCATION (decl),
8759 "%qE attribute applies only to functions", name);
8760 *no_add_attrs = true;
8762 else if (DECL_INITIAL (decl))
8764 error_at (DECL_SOURCE_LOCATION (decl),
8765 "can%'t set %qE attribute after definition", name);
8766 *no_add_attrs = true;
8769 return NULL_TREE;
8772 /* Check for valid arguments being passed to a function with FNTYPE.
8773 There are NARGS arguments in the array ARGARRAY. */
8774 void
8775 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8777 /* Check for null being passed in a pointer argument that must be
8778 non-null. We also need to do this if format checking is enabled. */
8780 if (warn_nonnull)
8781 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8783 /* Check for errors in format strings. */
8785 if (warn_format || warn_suggest_attribute_format)
8786 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8788 if (warn_format)
8789 check_function_sentinel (fntype, nargs, argarray);
8792 /* Generic argument checking recursion routine. PARAM is the argument to
8793 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8794 once the argument is resolved. CTX is context for the callback. */
8795 void
8796 check_function_arguments_recurse (void (*callback)
8797 (void *, tree, unsigned HOST_WIDE_INT),
8798 void *ctx, tree param,
8799 unsigned HOST_WIDE_INT param_num)
8801 if (CONVERT_EXPR_P (param)
8802 && (TYPE_PRECISION (TREE_TYPE (param))
8803 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8805 /* Strip coercion. */
8806 check_function_arguments_recurse (callback, ctx,
8807 TREE_OPERAND (param, 0), param_num);
8808 return;
8811 if (TREE_CODE (param) == CALL_EXPR)
8813 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8814 tree attrs;
8815 bool found_format_arg = false;
8817 /* See if this is a call to a known internationalization function
8818 that modifies a format arg. Such a function may have multiple
8819 format_arg attributes (for example, ngettext). */
8821 for (attrs = TYPE_ATTRIBUTES (type);
8822 attrs;
8823 attrs = TREE_CHAIN (attrs))
8824 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8826 tree inner_arg;
8827 tree format_num_expr;
8828 int format_num;
8829 int i;
8830 call_expr_arg_iterator iter;
8832 /* Extract the argument number, which was previously checked
8833 to be valid. */
8834 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8836 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8837 && !TREE_INT_CST_HIGH (format_num_expr));
8839 format_num = TREE_INT_CST_LOW (format_num_expr);
8841 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8842 inner_arg != 0;
8843 inner_arg = next_call_expr_arg (&iter), i++)
8844 if (i == format_num)
8846 check_function_arguments_recurse (callback, ctx,
8847 inner_arg, param_num);
8848 found_format_arg = true;
8849 break;
8853 /* If we found a format_arg attribute and did a recursive check,
8854 we are done with checking this argument. Otherwise, we continue
8855 and this will be considered a non-literal. */
8856 if (found_format_arg)
8857 return;
8860 if (TREE_CODE (param) == COND_EXPR)
8862 /* Check both halves of the conditional expression. */
8863 check_function_arguments_recurse (callback, ctx,
8864 TREE_OPERAND (param, 1), param_num);
8865 check_function_arguments_recurse (callback, ctx,
8866 TREE_OPERAND (param, 2), param_num);
8867 return;
8870 (*callback) (ctx, param, param_num);
8873 /* Checks for a builtin function FNDECL that the number of arguments
8874 NARGS against the required number REQUIRED and issues an error if
8875 there is a mismatch. Returns true if the number of arguments is
8876 correct, otherwise false. */
8878 static bool
8879 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8881 if (nargs < required)
8883 error_at (input_location,
8884 "not enough arguments to function %qE", fndecl);
8885 return false;
8887 else if (nargs > required)
8889 error_at (input_location,
8890 "too many arguments to function %qE", fndecl);
8891 return false;
8893 return true;
8896 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8897 Returns false if there was an error, otherwise true. */
8899 bool
8900 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8902 if (!DECL_BUILT_IN (fndecl)
8903 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8904 return true;
8906 switch (DECL_FUNCTION_CODE (fndecl))
8908 case BUILT_IN_CONSTANT_P:
8909 return builtin_function_validate_nargs (fndecl, nargs, 1);
8911 case BUILT_IN_ISFINITE:
8912 case BUILT_IN_ISINF:
8913 case BUILT_IN_ISINF_SIGN:
8914 case BUILT_IN_ISNAN:
8915 case BUILT_IN_ISNORMAL:
8916 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8918 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8920 error ("non-floating-point argument in call to "
8921 "function %qE", fndecl);
8922 return false;
8924 return true;
8926 return false;
8928 case BUILT_IN_ISGREATER:
8929 case BUILT_IN_ISGREATEREQUAL:
8930 case BUILT_IN_ISLESS:
8931 case BUILT_IN_ISLESSEQUAL:
8932 case BUILT_IN_ISLESSGREATER:
8933 case BUILT_IN_ISUNORDERED:
8934 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8936 enum tree_code code0, code1;
8937 code0 = TREE_CODE (TREE_TYPE (args[0]));
8938 code1 = TREE_CODE (TREE_TYPE (args[1]));
8939 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8940 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8941 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8943 error ("non-floating-point arguments in call to "
8944 "function %qE", fndecl);
8945 return false;
8947 return true;
8949 return false;
8951 case BUILT_IN_FPCLASSIFY:
8952 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8954 unsigned i;
8956 for (i=0; i<5; i++)
8957 if (TREE_CODE (args[i]) != INTEGER_CST)
8959 error ("non-const integer argument %u in call to function %qE",
8960 i+1, fndecl);
8961 return false;
8964 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8966 error ("non-floating-point argument in call to function %qE",
8967 fndecl);
8968 return false;
8970 return true;
8972 return false;
8974 case BUILT_IN_ASSUME_ALIGNED:
8975 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8977 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8979 error ("non-integer argument 3 in call to function %qE", fndecl);
8980 return false;
8982 return true;
8984 return false;
8986 default:
8987 return true;
8991 /* Function to help qsort sort FIELD_DECLs by name order. */
8994 field_decl_cmp (const void *x_p, const void *y_p)
8996 const tree *const x = (const tree *const) x_p;
8997 const tree *const y = (const tree *const) y_p;
8999 if (DECL_NAME (*x) == DECL_NAME (*y))
9000 /* A nontype is "greater" than a type. */
9001 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9002 if (DECL_NAME (*x) == NULL_TREE)
9003 return -1;
9004 if (DECL_NAME (*y) == NULL_TREE)
9005 return 1;
9006 if (DECL_NAME (*x) < DECL_NAME (*y))
9007 return -1;
9008 return 1;
9011 static struct {
9012 gt_pointer_operator new_value;
9013 void *cookie;
9014 } resort_data;
9016 /* This routine compares two fields like field_decl_cmp but using the
9017 pointer operator in resort_data. */
9019 static int
9020 resort_field_decl_cmp (const void *x_p, const void *y_p)
9022 const tree *const x = (const tree *const) x_p;
9023 const tree *const y = (const tree *const) y_p;
9025 if (DECL_NAME (*x) == DECL_NAME (*y))
9026 /* A nontype is "greater" than a type. */
9027 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9028 if (DECL_NAME (*x) == NULL_TREE)
9029 return -1;
9030 if (DECL_NAME (*y) == NULL_TREE)
9031 return 1;
9033 tree d1 = DECL_NAME (*x);
9034 tree d2 = DECL_NAME (*y);
9035 resort_data.new_value (&d1, resort_data.cookie);
9036 resort_data.new_value (&d2, resort_data.cookie);
9037 if (d1 < d2)
9038 return -1;
9040 return 1;
9043 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9045 void
9046 resort_sorted_fields (void *obj,
9047 void * ARG_UNUSED (orig_obj),
9048 gt_pointer_operator new_value,
9049 void *cookie)
9051 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9052 resort_data.new_value = new_value;
9053 resort_data.cookie = cookie;
9054 qsort (&sf->elts[0], sf->len, sizeof (tree),
9055 resort_field_decl_cmp);
9058 /* Subroutine of c_parse_error.
9059 Return the result of concatenating LHS and RHS. RHS is really
9060 a string literal, its first character is indicated by RHS_START and
9061 RHS_SIZE is its length (including the terminating NUL character).
9063 The caller is responsible for deleting the returned pointer. */
9065 static char *
9066 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9068 const int lhs_size = strlen (lhs);
9069 char *result = XNEWVEC (char, lhs_size + rhs_size);
9070 strncpy (result, lhs, lhs_size);
9071 strncpy (result + lhs_size, rhs_start, rhs_size);
9072 return result;
9075 /* Issue the error given by GMSGID, indicating that it occurred before
9076 TOKEN, which had the associated VALUE. */
9078 void
9079 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9080 tree value, unsigned char token_flags)
9082 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9084 char *message = NULL;
9086 if (token_type == CPP_EOF)
9087 message = catenate_messages (gmsgid, " at end of input");
9088 else if (token_type == CPP_CHAR
9089 || token_type == CPP_WCHAR
9090 || token_type == CPP_CHAR16
9091 || token_type == CPP_CHAR32)
9093 unsigned int val = TREE_INT_CST_LOW (value);
9094 const char *prefix;
9096 switch (token_type)
9098 default:
9099 prefix = "";
9100 break;
9101 case CPP_WCHAR:
9102 prefix = "L";
9103 break;
9104 case CPP_CHAR16:
9105 prefix = "u";
9106 break;
9107 case CPP_CHAR32:
9108 prefix = "U";
9109 break;
9112 if (val <= UCHAR_MAX && ISGRAPH (val))
9113 message = catenate_messages (gmsgid, " before %s'%c'");
9114 else
9115 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9117 error (message, prefix, val);
9118 free (message);
9119 message = NULL;
9121 else if (token_type == CPP_STRING
9122 || token_type == CPP_WSTRING
9123 || token_type == CPP_STRING16
9124 || token_type == CPP_STRING32
9125 || token_type == CPP_UTF8STRING)
9126 message = catenate_messages (gmsgid, " before string constant");
9127 else if (token_type == CPP_NUMBER)
9128 message = catenate_messages (gmsgid, " before numeric constant");
9129 else if (token_type == CPP_NAME)
9131 message = catenate_messages (gmsgid, " before %qE");
9132 error (message, value);
9133 free (message);
9134 message = NULL;
9136 else if (token_type == CPP_PRAGMA)
9137 message = catenate_messages (gmsgid, " before %<#pragma%>");
9138 else if (token_type == CPP_PRAGMA_EOL)
9139 message = catenate_messages (gmsgid, " before end of line");
9140 else if (token_type == CPP_DECLTYPE)
9141 message = catenate_messages (gmsgid, " before %<decltype%>");
9142 else if (token_type < N_TTYPES)
9144 message = catenate_messages (gmsgid, " before %qs token");
9145 error (message, cpp_type2name (token_type, token_flags));
9146 free (message);
9147 message = NULL;
9149 else
9150 error (gmsgid);
9152 if (message)
9154 error (message);
9155 free (message);
9157 #undef catenate_messages
9160 /* Mapping for cpp message reasons to the options that enable them. */
9162 struct reason_option_codes_t
9164 const int reason; /* cpplib message reason. */
9165 const int option_code; /* gcc option that controls this message. */
9168 static const struct reason_option_codes_t option_codes[] = {
9169 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9170 {CPP_W_COMMENTS, OPT_Wcomment},
9171 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9172 {CPP_W_MULTICHAR, OPT_Wmultichar},
9173 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9174 {CPP_W_LONG_LONG, OPT_Wlong_long},
9175 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9176 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9177 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9178 {CPP_W_UNDEF, OPT_Wundef},
9179 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9180 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9181 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9182 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9183 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9184 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9185 {CPP_W_NONE, 0}
9188 /* Return the gcc option code associated with the reason for a cpp
9189 message, or 0 if none. */
9191 static int
9192 c_option_controlling_cpp_error (int reason)
9194 const struct reason_option_codes_t *entry;
9196 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9198 if (entry->reason == reason)
9199 return entry->option_code;
9201 return 0;
9204 /* Callback from cpp_error for PFILE to print diagnostics from the
9205 preprocessor. The diagnostic is of type LEVEL, with REASON set
9206 to the reason code if LEVEL is represents a warning, at location
9207 LOCATION unless this is after lexing and the compiler's location
9208 should be used instead, with column number possibly overridden by
9209 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9210 the arguments. Returns true if a diagnostic was emitted, false
9211 otherwise. */
9213 bool
9214 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9215 location_t location, unsigned int column_override,
9216 const char *msg, va_list *ap)
9218 diagnostic_info diagnostic;
9219 diagnostic_t dlevel;
9220 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9221 bool ret;
9223 switch (level)
9225 case CPP_DL_WARNING_SYSHDR:
9226 if (flag_no_output)
9227 return false;
9228 global_dc->dc_warn_system_headers = 1;
9229 /* Fall through. */
9230 case CPP_DL_WARNING:
9231 if (flag_no_output)
9232 return false;
9233 dlevel = DK_WARNING;
9234 break;
9235 case CPP_DL_PEDWARN:
9236 if (flag_no_output && !flag_pedantic_errors)
9237 return false;
9238 dlevel = DK_PEDWARN;
9239 break;
9240 case CPP_DL_ERROR:
9241 dlevel = DK_ERROR;
9242 break;
9243 case CPP_DL_ICE:
9244 dlevel = DK_ICE;
9245 break;
9246 case CPP_DL_NOTE:
9247 dlevel = DK_NOTE;
9248 break;
9249 case CPP_DL_FATAL:
9250 dlevel = DK_FATAL;
9251 break;
9252 default:
9253 gcc_unreachable ();
9255 if (done_lexing)
9256 location = input_location;
9257 diagnostic_set_info_translated (&diagnostic, msg, ap,
9258 location, dlevel);
9259 if (column_override)
9260 diagnostic_override_column (&diagnostic, column_override);
9261 diagnostic_override_option_index (&diagnostic,
9262 c_option_controlling_cpp_error (reason));
9263 ret = report_diagnostic (&diagnostic);
9264 if (level == CPP_DL_WARNING_SYSHDR)
9265 global_dc->dc_warn_system_headers = save_warn_system_headers;
9266 return ret;
9269 /* Convert a character from the host to the target execution character
9270 set. cpplib handles this, mostly. */
9272 HOST_WIDE_INT
9273 c_common_to_target_charset (HOST_WIDE_INT c)
9275 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9276 zero-extended under -fno-signed-char. cpplib insists that characters
9277 and character constants are always unsigned. Hence we must convert
9278 back and forth. */
9279 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9281 uc = cpp_host_to_exec_charset (parse_in, uc);
9283 if (flag_signed_char)
9284 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9285 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9286 else
9287 return uc;
9290 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9291 references with an INDIRECT_REF of a constant at the bottom; much like the
9292 traditional rendering of offsetof as a macro. Return the folded result. */
9294 tree
9295 fold_offsetof_1 (tree expr)
9297 tree base, off, t;
9299 switch (TREE_CODE (expr))
9301 case ERROR_MARK:
9302 return expr;
9304 case VAR_DECL:
9305 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9306 return error_mark_node;
9308 case CALL_EXPR:
9309 case TARGET_EXPR:
9310 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9311 return error_mark_node;
9313 case NOP_EXPR:
9314 case INDIRECT_REF:
9315 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9317 error ("cannot apply %<offsetof%> to a non constant address");
9318 return error_mark_node;
9320 return TREE_OPERAND (expr, 0);
9322 case COMPONENT_REF:
9323 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9324 if (base == error_mark_node)
9325 return base;
9327 t = TREE_OPERAND (expr, 1);
9328 if (DECL_C_BIT_FIELD (t))
9330 error ("attempt to take address of bit-field structure "
9331 "member %qD", t);
9332 return error_mark_node;
9334 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9335 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9337 / BITS_PER_UNIT));
9338 break;
9340 case ARRAY_REF:
9341 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9342 if (base == error_mark_node)
9343 return base;
9345 t = TREE_OPERAND (expr, 1);
9347 /* Check if the offset goes beyond the upper bound of the array. */
9348 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9350 tree upbound = array_ref_up_bound (expr);
9351 if (upbound != NULL_TREE
9352 && TREE_CODE (upbound) == INTEGER_CST
9353 && !tree_int_cst_equal (upbound,
9354 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9356 upbound = size_binop (PLUS_EXPR, upbound,
9357 build_int_cst (TREE_TYPE (upbound), 1));
9358 if (tree_int_cst_lt (upbound, t))
9360 tree v;
9362 for (v = TREE_OPERAND (expr, 0);
9363 TREE_CODE (v) == COMPONENT_REF;
9364 v = TREE_OPERAND (v, 0))
9365 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9366 == RECORD_TYPE)
9368 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9369 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9370 if (TREE_CODE (fld_chain) == FIELD_DECL)
9371 break;
9373 if (fld_chain)
9374 break;
9376 /* Don't warn if the array might be considered a poor
9377 man's flexible array member with a very permissive
9378 definition thereof. */
9379 if (TREE_CODE (v) == ARRAY_REF
9380 || TREE_CODE (v) == COMPONENT_REF)
9381 warning (OPT_Warray_bounds,
9382 "index %E denotes an offset "
9383 "greater than size of %qT",
9384 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9389 t = convert (sizetype, t);
9390 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9391 break;
9393 case COMPOUND_EXPR:
9394 /* Handle static members of volatile structs. */
9395 t = TREE_OPERAND (expr, 1);
9396 gcc_assert (TREE_CODE (t) == VAR_DECL);
9397 return fold_offsetof_1 (t);
9399 default:
9400 gcc_unreachable ();
9403 return fold_build_pointer_plus (base, off);
9406 /* Likewise, but convert it to the return type of offsetof. */
9408 tree
9409 fold_offsetof (tree expr)
9411 return convert (size_type_node, fold_offsetof_1 (expr));
9414 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9415 expression, because B will always be true. */
9417 void
9418 warn_for_omitted_condop (location_t location, tree cond)
9420 if (truth_value_p (TREE_CODE (cond)))
9421 warning_at (location, OPT_Wparentheses,
9422 "the omitted middle operand in ?: will always be %<true%>, "
9423 "suggest explicit middle operand");
9426 /* Give an error for storing into ARG, which is 'const'. USE indicates
9427 how ARG was being used. */
9429 void
9430 readonly_error (tree arg, enum lvalue_use use)
9432 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9433 || use == lv_asm);
9434 /* Using this macro rather than (for example) arrays of messages
9435 ensures that all the format strings are checked at compile
9436 time. */
9437 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9438 : (use == lv_increment ? (I) \
9439 : (use == lv_decrement ? (D) : (AS))))
9440 if (TREE_CODE (arg) == COMPONENT_REF)
9442 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9443 error (READONLY_MSG (G_("assignment of member "
9444 "%qD in read-only object"),
9445 G_("increment of member "
9446 "%qD in read-only object"),
9447 G_("decrement of member "
9448 "%qD in read-only object"),
9449 G_("member %qD in read-only object "
9450 "used as %<asm%> output")),
9451 TREE_OPERAND (arg, 1));
9452 else
9453 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9454 G_("increment of read-only member %qD"),
9455 G_("decrement of read-only member %qD"),
9456 G_("read-only member %qD used as %<asm%> output")),
9457 TREE_OPERAND (arg, 1));
9459 else if (TREE_CODE (arg) == VAR_DECL)
9460 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9461 G_("increment of read-only variable %qD"),
9462 G_("decrement of read-only variable %qD"),
9463 G_("read-only variable %qD used as %<asm%> output")),
9464 arg);
9465 else if (TREE_CODE (arg) == PARM_DECL)
9466 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9467 G_("increment of read-only parameter %qD"),
9468 G_("decrement of read-only parameter %qD"),
9469 G_("read-only parameter %qD use as %<asm%> output")),
9470 arg);
9471 else if (TREE_CODE (arg) == RESULT_DECL)
9473 gcc_assert (c_dialect_cxx ());
9474 error (READONLY_MSG (G_("assignment of "
9475 "read-only named return value %qD"),
9476 G_("increment of "
9477 "read-only named return value %qD"),
9478 G_("decrement of "
9479 "read-only named return value %qD"),
9480 G_("read-only named return value %qD "
9481 "used as %<asm%>output")),
9482 arg);
9484 else if (TREE_CODE (arg) == FUNCTION_DECL)
9485 error (READONLY_MSG (G_("assignment of function %qD"),
9486 G_("increment of function %qD"),
9487 G_("decrement of function %qD"),
9488 G_("function %qD used as %<asm%> output")),
9489 arg);
9490 else
9491 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9492 G_("increment of read-only location %qE"),
9493 G_("decrement of read-only location %qE"),
9494 G_("read-only location %qE used as %<asm%> output")),
9495 arg);
9498 /* Print an error message for an invalid lvalue. USE says
9499 how the lvalue is being used and so selects the error message. LOC
9500 is the location for the error. */
9502 void
9503 lvalue_error (location_t loc, enum lvalue_use use)
9505 switch (use)
9507 case lv_assign:
9508 error_at (loc, "lvalue required as left operand of assignment");
9509 break;
9510 case lv_increment:
9511 error_at (loc, "lvalue required as increment operand");
9512 break;
9513 case lv_decrement:
9514 error_at (loc, "lvalue required as decrement operand");
9515 break;
9516 case lv_addressof:
9517 error_at (loc, "lvalue required as unary %<&%> operand");
9518 break;
9519 case lv_asm:
9520 error_at (loc, "lvalue required in asm statement");
9521 break;
9522 default:
9523 gcc_unreachable ();
9527 /* Print an error message for an invalid indirection of type TYPE.
9528 ERRSTRING is the name of the operator for the indirection. */
9530 void
9531 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9533 switch (errstring)
9535 case RO_NULL:
9536 gcc_assert (c_dialect_cxx ());
9537 error_at (loc, "invalid type argument (have %qT)", type);
9538 break;
9539 case RO_ARRAY_INDEXING:
9540 error_at (loc,
9541 "invalid type argument of array indexing (have %qT)",
9542 type);
9543 break;
9544 case RO_UNARY_STAR:
9545 error_at (loc,
9546 "invalid type argument of unary %<*%> (have %qT)",
9547 type);
9548 break;
9549 case RO_ARROW:
9550 error_at (loc,
9551 "invalid type argument of %<->%> (have %qT)",
9552 type);
9553 break;
9554 case RO_IMPLICIT_CONVERSION:
9555 error_at (loc,
9556 "invalid type argument of implicit conversion (have %qT)",
9557 type);
9558 break;
9559 default:
9560 gcc_unreachable ();
9564 /* *PTYPE is an incomplete array. Complete it with a domain based on
9565 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9566 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9567 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9570 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9572 tree maxindex, type, main_type, elt, unqual_elt;
9573 int failure = 0, quals;
9574 hashval_t hashcode = 0;
9576 maxindex = size_zero_node;
9577 if (initial_value)
9579 if (TREE_CODE (initial_value) == STRING_CST)
9581 int eltsize
9582 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9583 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9585 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9587 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
9589 if (VEC_empty (constructor_elt, v))
9591 if (pedantic)
9592 failure = 3;
9593 maxindex = ssize_int (-1);
9595 else
9597 tree curindex;
9598 unsigned HOST_WIDE_INT cnt;
9599 constructor_elt *ce;
9600 bool fold_p = false;
9602 if (VEC_index (constructor_elt, v, 0).index)
9603 maxindex = fold_convert_loc (input_location, sizetype,
9604 VEC_index (constructor_elt,
9605 v, 0).index);
9606 curindex = maxindex;
9608 for (cnt = 1;
9609 VEC_iterate (constructor_elt, v, cnt, ce);
9610 cnt++)
9612 bool curfold_p = false;
9613 if (ce->index)
9614 curindex = ce->index, curfold_p = true;
9615 else
9617 if (fold_p)
9618 curindex = fold_convert (sizetype, curindex);
9619 curindex = size_binop (PLUS_EXPR, curindex,
9620 size_one_node);
9622 if (tree_int_cst_lt (maxindex, curindex))
9623 maxindex = curindex, fold_p = curfold_p;
9625 if (fold_p)
9626 maxindex = fold_convert (sizetype, maxindex);
9629 else
9631 /* Make an error message unless that happened already. */
9632 if (initial_value != error_mark_node)
9633 failure = 1;
9636 else
9638 failure = 2;
9639 if (!do_default)
9640 return failure;
9643 type = *ptype;
9644 /* Force an indefinite layout factor. */
9645 if (upc_shared_type_p (type))
9646 type = c_build_qualified_type_1 (type, TYPE_QUAL_SHARED, size_zero_node);
9647 elt = TREE_TYPE (type);
9648 quals = TYPE_QUALS (strip_array_types (elt));
9649 if (quals == 0)
9650 unqual_elt = elt;
9651 else
9652 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9654 /* Using build_distinct_type_copy and modifying things afterward instead
9655 of using build_array_type to create a new type preserves all of the
9656 TYPE_LANG_FLAG_? bits that the front end may have set. */
9657 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9658 TREE_TYPE (main_type) = unqual_elt;
9659 TYPE_DOMAIN (main_type)
9660 = build_range_type (TREE_TYPE (maxindex),
9661 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9662 layout_type (main_type);
9664 /* Make sure we have the canonical MAIN_TYPE. */
9665 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9666 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9667 hashcode);
9668 main_type = type_hash_canon (hashcode, main_type);
9670 /* Fix the canonical type. */
9671 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9672 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9673 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9674 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9675 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9676 != TYPE_DOMAIN (main_type)))
9677 TYPE_CANONICAL (main_type)
9678 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9679 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9680 else
9681 TYPE_CANONICAL (main_type) = main_type;
9683 if (quals == 0)
9684 type = main_type;
9685 else
9686 type = c_build_qualified_type (main_type, quals);
9688 if (COMPLETE_TYPE_P (type)
9689 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9690 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9692 error ("size of array is too large");
9693 /* If we proceed with the array type as it is, we'll eventually
9694 crash in tree_low_cst(). */
9695 type = error_mark_node;
9698 *ptype = type;
9699 return failure;
9702 /* Like c_mark_addressable but don't check register qualifier. */
9703 void
9704 c_common_mark_addressable_vec (tree t)
9706 while (handled_component_p (t))
9707 t = TREE_OPERAND (t, 0);
9708 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9709 return;
9710 TREE_ADDRESSABLE (t) = 1;
9715 /* Used to help initialize the builtin-types.def table. When a type of
9716 the correct size doesn't exist, use error_mark_node instead of NULL.
9717 The later results in segfaults even when a decl using the type doesn't
9718 get invoked. */
9720 tree
9721 builtin_type_for_size (int size, bool unsignedp)
9723 tree type = c_common_type_for_size (size, unsignedp);
9724 return type ? type : error_mark_node;
9727 /* A helper function for resolve_overloaded_builtin in resolving the
9728 overloaded __sync_ builtins. Returns a positive power of 2 if the
9729 first operand of PARAMS is a pointer to a supported data type.
9730 Returns 0 if an error is encountered. */
9732 static int
9733 sync_resolve_size (tree function, VEC(tree,gc) *params)
9735 tree type;
9736 int size;
9738 if (VEC_empty (tree, params))
9740 error ("too few arguments to function %qE", function);
9741 return 0;
9744 type = TREE_TYPE (VEC_index (tree, params, 0));
9745 if (TREE_CODE (type) != POINTER_TYPE)
9746 goto incompatible;
9748 type = TREE_TYPE (type);
9749 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9750 goto incompatible;
9752 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9753 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9754 return size;
9756 incompatible:
9757 error ("incompatible type for argument %d of %qE", 1, function);
9758 return 0;
9761 /* A helper function for resolve_overloaded_builtin. Adds casts to
9762 PARAMS to make arguments match up with those of FUNCTION. Drops
9763 the variadic arguments at the end. Returns false if some error
9764 was encountered; true on success. */
9766 static bool
9767 sync_resolve_params (location_t loc, tree orig_function, tree function,
9768 VEC(tree, gc) *params, bool orig_format)
9770 function_args_iterator iter;
9771 tree ptype;
9772 unsigned int parmnum;
9774 function_args_iter_init (&iter, TREE_TYPE (function));
9775 /* We've declared the implementation functions to use "volatile void *"
9776 as the pointer parameter, so we shouldn't get any complaints from the
9777 call to check_function_arguments what ever type the user used. */
9778 function_args_iter_next (&iter);
9779 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9781 /* For the rest of the values, we need to cast these to FTYPE, so that we
9782 don't get warnings for passing pointer types, etc. */
9783 parmnum = 0;
9784 while (1)
9786 tree val, arg_type;
9788 arg_type = function_args_iter_cond (&iter);
9789 /* XXX void_type_node belies the abstraction. */
9790 if (arg_type == void_type_node)
9791 break;
9793 ++parmnum;
9794 if (VEC_length (tree, params) <= parmnum)
9796 error_at (loc, "too few arguments to function %qE", orig_function);
9797 return false;
9800 /* Only convert parameters if arg_type is unsigned integer type with
9801 new format sync routines, i.e. don't attempt to convert pointer
9802 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9803 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9804 kinds). */
9805 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
9807 /* Ideally for the first conversion we'd use convert_for_assignment
9808 so that we get warnings for anything that doesn't match the pointer
9809 type. This isn't portable across the C and C++ front ends atm. */
9810 val = VEC_index (tree, params, parmnum);
9811 val = convert (ptype, val);
9812 val = convert (arg_type, val);
9813 VEC_replace (tree, params, parmnum, val);
9816 function_args_iter_next (&iter);
9819 /* __atomic routines are not variadic. */
9820 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9822 error_at (loc, "too many arguments to function %qE", orig_function);
9823 return false;
9826 /* The definition of these primitives is variadic, with the remaining
9827 being "an optional list of variables protected by the memory barrier".
9828 No clue what that's supposed to mean, precisely, but we consider all
9829 call-clobbered variables to be protected so we're safe. */
9830 VEC_truncate (tree, params, parmnum + 1);
9832 return true;
9835 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9836 RESULT to make it match the type of the first pointer argument in
9837 PARAMS. */
9839 static tree
9840 sync_resolve_return (tree first_param, tree result, bool orig_format)
9842 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9843 tree rtype = TREE_TYPE (result);
9844 ptype = TYPE_MAIN_VARIANT (ptype);
9846 /* New format doesn't require casting unless the types are the same size. */
9847 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9848 return convert (ptype, result);
9849 else
9850 return result;
9853 /* This function verifies the PARAMS to generic atomic FUNCTION.
9854 It returns the size if all the parameters are the same size, otherwise
9855 0 is returned if the parameters are invalid. */
9857 static int
9858 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9860 unsigned int n_param;
9861 unsigned int n_model;
9862 unsigned int x;
9863 int size_0;
9864 tree type_0;
9866 /* Determine the parameter makeup. */
9867 switch (DECL_FUNCTION_CODE (function))
9869 case BUILT_IN_ATOMIC_EXCHANGE:
9870 n_param = 4;
9871 n_model = 1;
9872 break;
9873 case BUILT_IN_ATOMIC_LOAD:
9874 case BUILT_IN_ATOMIC_STORE:
9875 n_param = 3;
9876 n_model = 1;
9877 break;
9878 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9879 n_param = 6;
9880 n_model = 2;
9881 break;
9882 default:
9883 gcc_unreachable ();
9886 if (VEC_length (tree, params) != n_param)
9888 error_at (loc, "incorrect number of arguments to function %qE", function);
9889 return 0;
9892 /* Get type of first parameter, and determine its size. */
9893 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9894 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
9896 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
9897 function);
9898 return 0;
9901 /* Types must be compile time constant sizes. */
9902 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
9904 error_at (loc,
9905 "argument 1 of %qE must be a pointer to a constant size type",
9906 function);
9907 return 0;
9910 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9912 /* Zero size objects are not allowed. */
9913 if (size_0 == 0)
9915 error_at (loc,
9916 "argument 1 of %qE must be a pointer to a nonzero size object",
9917 function);
9918 return 0;
9921 /* Check each other parameter is a pointer and the same size. */
9922 for (x = 0; x < n_param - n_model; x++)
9924 int size;
9925 tree type = TREE_TYPE (VEC_index (tree, params, x));
9926 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9927 if (n_param == 6 && x == 3)
9928 continue;
9929 if (!POINTER_TYPE_P (type))
9931 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9932 function);
9933 return 0;
9935 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9936 if (size != size_0)
9938 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9939 function);
9940 return 0;
9944 /* Check memory model parameters for validity. */
9945 for (x = n_param - n_model ; x < n_param; x++)
9947 tree p = VEC_index (tree, params, x);
9948 if (TREE_CODE (p) == INTEGER_CST)
9950 int i = tree_low_cst (p, 1);
9951 if (i < 0 || i >= MEMMODEL_LAST)
9953 warning_at (loc, OPT_Winvalid_memory_model,
9954 "invalid memory model argument %d of %qE", x + 1,
9955 function);
9958 else
9959 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9961 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9962 function);
9963 return 0;
9967 return size_0;
9971 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
9972 at the beginning of the parameter list PARAMS representing the size of the
9973 objects. This is to match the library ABI requirement. LOC is the location
9974 of the function call.
9975 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
9976 returned to allow the external call to be constructed. */
9978 static tree
9979 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9980 VEC(tree,gc) *params)
9982 tree size_node;
9984 /* Insert a SIZE_T parameter as the first param. If there isn't
9985 enough space, allocate a new vector and recursively re-build with that. */
9986 if (!VEC_space (tree, params, 1))
9988 unsigned int z, len;
9989 VEC(tree,gc) *vec;
9990 tree f;
9992 len = VEC_length (tree, params);
9993 vec = VEC_alloc (tree, gc, len + 1);
9994 for (z = 0; z < len; z++)
9995 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
9996 f = build_function_call_vec (loc, function, vec, NULL);
9997 VEC_free (tree, gc, vec);
9998 return f;
10001 /* Add the size parameter and leave as a function call for processing. */
10002 size_node = build_int_cst (size_type_node, n);
10003 VEC_quick_insert (tree, params, 0, size_node);
10004 return NULL_TREE;
10008 /* This will process an __atomic_exchange function call, determine whether it
10009 needs to be mapped to the _N variation, or turned into a library call.
10010 LOC is the location of the builtin call.
10011 FUNCTION is the DECL that has been invoked;
10012 PARAMS is the argument list for the call. The return value is non-null
10013 TRUE is returned if it is translated into the proper format for a call to the
10014 external library, and NEW_RETURN is set the tree for that function.
10015 FALSE is returned if processing for the _N variation is required, and
10016 NEW_RETURN is set to the the return value the result is copied into. */
10017 static bool
10018 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10019 VEC(tree,gc) *params, tree *new_return)
10021 tree p0, p1, p2, p3;
10022 tree I_type, I_type_ptr;
10023 int n = get_atomic_generic_size (loc, function, params);
10025 /* Size of 0 is an error condition. */
10026 if (n == 0)
10028 *new_return = error_mark_node;
10029 return true;
10032 /* If not a lock-free size, change to the library generic format. */
10033 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10035 *new_return = add_atomic_size_parameter (n, loc, function, params);
10036 return true;
10039 /* Otherwise there is a lockfree match, transform the call from:
10040 void fn(T* mem, T* desired, T* return, model)
10041 into
10042 *return = (T) (fn (In* mem, (In) *desired, model)) */
10044 p0 = VEC_index (tree, params, 0);
10045 p1 = VEC_index (tree, params, 1);
10046 p2 = VEC_index (tree, params, 2);
10047 p3 = VEC_index (tree, params, 3);
10049 /* Create pointer to appropriate size. */
10050 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10051 I_type_ptr = build_pointer_type (I_type);
10053 /* Convert object pointer to required type. */
10054 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10055 VEC_replace (tree, params, 0, p0);
10056 /* Convert new value to required type, and dereference it. */
10057 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10058 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10059 VEC_replace (tree, params, 1, p1);
10061 /* Move memory model to the 3rd position, and end param list. */
10062 VEC_replace (tree, params, 2, p3);
10063 VEC_truncate (tree, params, 3);
10065 /* Convert return pointer and dereference it for later assignment. */
10066 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10068 return false;
10072 /* This will process an __atomic_compare_exchange function call, determine
10073 whether it needs to be mapped to the _N variation, or turned into a lib call.
10074 LOC is the location of the builtin call.
10075 FUNCTION is the DECL that has been invoked;
10076 PARAMS is the argument list for the call. The return value is non-null
10077 TRUE is returned if it is translated into the proper format for a call to the
10078 external library, and NEW_RETURN is set the tree for that function.
10079 FALSE is returned if processing for the _N variation is required. */
10081 static bool
10082 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10083 VEC(tree,gc) *params,
10084 tree *new_return)
10086 tree p0, p1, p2;
10087 tree I_type, I_type_ptr;
10088 int n = get_atomic_generic_size (loc, function, params);
10090 /* Size of 0 is an error condition. */
10091 if (n == 0)
10093 *new_return = error_mark_node;
10094 return true;
10097 /* If not a lock-free size, change to the library generic format. */
10098 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10100 /* The library generic format does not have the weak parameter, so
10101 remove it from the param list. Since a parameter has been removed,
10102 we can be sure that there is room for the SIZE_T parameter, meaning
10103 there will not be a recursive rebuilding of the parameter list, so
10104 there is no danger this will be done twice. */
10105 if (n > 0)
10107 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
10108 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
10109 VEC_truncate (tree, params, 5);
10111 *new_return = add_atomic_size_parameter (n, loc, function, params);
10112 return true;
10115 /* Otherwise, there is a match, so the call needs to be transformed from:
10116 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10117 into
10118 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10120 p0 = VEC_index (tree, params, 0);
10121 p1 = VEC_index (tree, params, 1);
10122 p2 = VEC_index (tree, params, 2);
10124 /* Create pointer to appropriate size. */
10125 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10126 I_type_ptr = build_pointer_type (I_type);
10128 /* Convert object pointer to required type. */
10129 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10130 VEC_replace (tree, params, 0, p0);
10132 /* Convert expected pointer to required type. */
10133 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10134 VEC_replace (tree, params, 1, p1);
10136 /* Convert desired value to required type, and dereference it. */
10137 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10138 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10139 VEC_replace (tree, params, 2, p2);
10141 /* The rest of the parameters are fine. NULL means no special return value
10142 processing.*/
10143 *new_return = NULL;
10144 return false;
10148 /* This will process an __atomic_load function call, determine whether it
10149 needs to be mapped to the _N variation, or turned into a library call.
10150 LOC is the location of the builtin call.
10151 FUNCTION is the DECL that has been invoked;
10152 PARAMS is the argument list for the call. The return value is non-null
10153 TRUE is returned if it is translated into the proper format for a call to the
10154 external library, and NEW_RETURN is set the tree for that function.
10155 FALSE is returned if processing for the _N variation is required, and
10156 NEW_RETURN is set to the the return value the result is copied into. */
10158 static bool
10159 resolve_overloaded_atomic_load (location_t loc, tree function,
10160 VEC(tree,gc) *params, tree *new_return)
10162 tree p0, p1, p2;
10163 tree I_type, I_type_ptr;
10164 int n = get_atomic_generic_size (loc, function, params);
10166 /* Size of 0 is an error condition. */
10167 if (n == 0)
10169 *new_return = error_mark_node;
10170 return true;
10173 /* If not a lock-free size, change to the library generic format. */
10174 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10176 *new_return = add_atomic_size_parameter (n, loc, function, params);
10177 return true;
10180 /* Otherwise, there is a match, so the call needs to be transformed from:
10181 void fn(T* mem, T* return, model)
10182 into
10183 *return = (T) (fn ((In *) mem, model)) */
10185 p0 = VEC_index (tree, params, 0);
10186 p1 = VEC_index (tree, params, 1);
10187 p2 = VEC_index (tree, params, 2);
10189 /* Create pointer to appropriate size. */
10190 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10191 I_type_ptr = build_pointer_type (I_type);
10193 /* Convert object pointer to required type. */
10194 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10195 VEC_replace (tree, params, 0, p0);
10197 /* Move memory model to the 2nd position, and end param list. */
10198 VEC_replace (tree, params, 1, p2);
10199 VEC_truncate (tree, params, 2);
10201 /* Convert return pointer and dereference it for later assignment. */
10202 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10204 return false;
10208 /* This will process an __atomic_store function call, determine whether it
10209 needs to be mapped to the _N variation, or turned into a library call.
10210 LOC is the location of the builtin call.
10211 FUNCTION is the DECL that has been invoked;
10212 PARAMS is the argument list for the call. The return value is non-null
10213 TRUE is returned if it is translated into the proper format for a call to the
10214 external library, and NEW_RETURN is set the tree for that function.
10215 FALSE is returned if processing for the _N variation is required, and
10216 NEW_RETURN is set to the the return value the result is copied into. */
10218 static bool
10219 resolve_overloaded_atomic_store (location_t loc, tree function,
10220 VEC(tree,gc) *params, tree *new_return)
10222 tree p0, p1;
10223 tree I_type, I_type_ptr;
10224 int n = get_atomic_generic_size (loc, function, params);
10226 /* Size of 0 is an error condition. */
10227 if (n == 0)
10229 *new_return = error_mark_node;
10230 return true;
10233 /* If not a lock-free size, change to the library generic format. */
10234 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10236 *new_return = add_atomic_size_parameter (n, loc, function, params);
10237 return true;
10240 /* Otherwise, there is a match, so the call needs to be transformed from:
10241 void fn(T* mem, T* value, model)
10242 into
10243 fn ((In *) mem, (In) *value, model) */
10245 p0 = VEC_index (tree, params, 0);
10246 p1 = VEC_index (tree, params, 1);
10248 /* Create pointer to appropriate size. */
10249 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10250 I_type_ptr = build_pointer_type (I_type);
10252 /* Convert object pointer to required type. */
10253 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10254 VEC_replace (tree, params, 0, p0);
10256 /* Convert new value to required type, and dereference it. */
10257 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10258 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10259 VEC_replace (tree, params, 1, p1);
10261 /* The memory model is in the right spot already. Return is void. */
10262 *new_return = NULL_TREE;
10264 return false;
10268 /* Some builtin functions are placeholders for other expressions. This
10269 function should be called immediately after parsing the call expression
10270 before surrounding code has committed to the type of the expression.
10272 LOC is the location of the builtin call.
10274 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10275 PARAMS is the argument list for the call. The return value is non-null
10276 when expansion is complete, and null if normal processing should
10277 continue. */
10279 tree
10280 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
10282 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10283 bool orig_format = true;
10284 tree new_return = NULL_TREE;
10286 switch (DECL_BUILT_IN_CLASS (function))
10288 case BUILT_IN_NORMAL:
10289 break;
10290 case BUILT_IN_MD:
10291 if (targetm.resolve_overloaded_builtin)
10292 return targetm.resolve_overloaded_builtin (loc, function, params);
10293 else
10294 return NULL_TREE;
10295 default:
10296 return NULL_TREE;
10299 /* Handle BUILT_IN_NORMAL here. */
10300 switch (orig_code)
10302 case BUILT_IN_ATOMIC_EXCHANGE:
10303 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10304 case BUILT_IN_ATOMIC_LOAD:
10305 case BUILT_IN_ATOMIC_STORE:
10307 /* Handle these 4 together so that they can fall through to the next
10308 case if the call is transformed to an _N variant. */
10309 switch (orig_code)
10311 case BUILT_IN_ATOMIC_EXCHANGE:
10313 if (resolve_overloaded_atomic_exchange (loc, function, params,
10314 &new_return))
10315 return new_return;
10316 /* Change to the _N variant. */
10317 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10318 break;
10321 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10323 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10324 params,
10325 &new_return))
10326 return new_return;
10327 /* Change to the _N variant. */
10328 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10329 break;
10331 case BUILT_IN_ATOMIC_LOAD:
10333 if (resolve_overloaded_atomic_load (loc, function, params,
10334 &new_return))
10335 return new_return;
10336 /* Change to the _N variant. */
10337 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10338 break;
10340 case BUILT_IN_ATOMIC_STORE:
10342 if (resolve_overloaded_atomic_store (loc, function, params,
10343 &new_return))
10344 return new_return;
10345 /* Change to the _N variant. */
10346 orig_code = BUILT_IN_ATOMIC_STORE_N;
10347 break;
10349 default:
10350 gcc_unreachable ();
10352 /* Fallthrough to the normal processing. */
10354 case BUILT_IN_ATOMIC_EXCHANGE_N:
10355 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10356 case BUILT_IN_ATOMIC_LOAD_N:
10357 case BUILT_IN_ATOMIC_STORE_N:
10358 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10359 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10360 case BUILT_IN_ATOMIC_AND_FETCH_N:
10361 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10362 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10363 case BUILT_IN_ATOMIC_OR_FETCH_N:
10364 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10365 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10366 case BUILT_IN_ATOMIC_FETCH_AND_N:
10367 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10368 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10369 case BUILT_IN_ATOMIC_FETCH_OR_N:
10371 orig_format = false;
10372 /* Fallthru for parameter processing. */
10374 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10375 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10376 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10377 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10378 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10379 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10380 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10381 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10382 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10383 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10384 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10385 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10386 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10387 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10388 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10389 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10391 int n = sync_resolve_size (function, params);
10392 tree new_function, first_param, result;
10393 enum built_in_function fncode;
10395 if (n == 0)
10396 return error_mark_node;
10398 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10399 new_function = builtin_decl_explicit (fncode);
10400 if (!sync_resolve_params (loc, function, new_function, params,
10401 orig_format))
10402 return error_mark_node;
10404 first_param = VEC_index (tree, params, 0);
10405 result = build_function_call_vec (loc, new_function, params, NULL);
10406 if (result == error_mark_node)
10407 return result;
10408 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10409 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10410 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10411 result = sync_resolve_return (first_param, result, orig_format);
10413 /* If new_return is set, assign function to that expr and cast the
10414 result to void since the generic interface returned void. */
10415 if (new_return)
10417 /* Cast function result from I{1,2,4,8,16} to the required type. */
10418 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10419 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10420 result);
10421 TREE_SIDE_EFFECTS (result) = 1;
10422 protected_set_expr_location (result, loc);
10423 result = convert (void_type_node, result);
10425 return result;
10428 default:
10429 return NULL_TREE;
10433 /* Ignoring their sign, return true if two scalar types are the same. */
10434 bool
10435 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10437 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10439 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10440 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10441 || c2 == FIXED_POINT_TYPE));
10443 /* Equality works here because c_common_signed_type uses
10444 TYPE_MAIN_VARIANT. */
10445 return c_common_signed_type (t1)
10446 == c_common_signed_type (t2);
10449 /* Check for missing format attributes on function pointers. LTYPE is
10450 the new type or left-hand side type. RTYPE is the old type or
10451 right-hand side type. Returns TRUE if LTYPE is missing the desired
10452 attribute. */
10454 bool
10455 check_missing_format_attribute (tree ltype, tree rtype)
10457 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10458 tree ra;
10460 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10461 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10462 break;
10463 if (ra)
10465 tree la;
10466 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10467 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10468 break;
10469 return !la;
10471 else
10472 return false;
10475 /* Subscripting with type char is likely to lose on a machine where
10476 chars are signed. So warn on any machine, but optionally. Don't
10477 warn for unsigned char since that type is safe. Don't warn for
10478 signed char because anyone who uses that must have done so
10479 deliberately. Furthermore, we reduce the false positive load by
10480 warning only for non-constant value of type char. */
10482 void
10483 warn_array_subscript_with_type_char (tree index)
10485 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10486 && TREE_CODE (index) != INTEGER_CST)
10487 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10490 /* Implement -Wparentheses for the unexpected C precedence rules, to
10491 cover cases like x + y << z which readers are likely to
10492 misinterpret. We have seen an expression in which CODE is a binary
10493 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10494 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10495 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10496 expression was not formed using a binary or unary operator, or it
10497 was enclosed in parentheses. */
10499 void
10500 warn_about_parentheses (enum tree_code code,
10501 enum tree_code code_left, tree arg_left,
10502 enum tree_code code_right, tree arg_right)
10504 if (!warn_parentheses)
10505 return;
10507 /* This macro tests that the expression ARG with original tree code
10508 CODE appears to be a boolean expression. or the result of folding a
10509 boolean expression. */
10510 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10511 (truth_value_p (TREE_CODE (ARG)) \
10512 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10513 /* Folding may create 0 or 1 integers from other expressions. */ \
10514 || ((CODE) != INTEGER_CST \
10515 && (integer_onep (ARG) || integer_zerop (ARG))))
10517 switch (code)
10519 case LSHIFT_EXPR:
10520 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10521 warning (OPT_Wparentheses,
10522 "suggest parentheses around %<+%> inside %<<<%>");
10523 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10524 warning (OPT_Wparentheses,
10525 "suggest parentheses around %<-%> inside %<<<%>");
10526 return;
10528 case RSHIFT_EXPR:
10529 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10530 warning (OPT_Wparentheses,
10531 "suggest parentheses around %<+%> inside %<>>%>");
10532 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10533 warning (OPT_Wparentheses,
10534 "suggest parentheses around %<-%> inside %<>>%>");
10535 return;
10537 case TRUTH_ORIF_EXPR:
10538 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
10539 warning (OPT_Wparentheses,
10540 "suggest parentheses around %<&&%> within %<||%>");
10541 return;
10543 case BIT_IOR_EXPR:
10544 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10545 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10546 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10547 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10548 warning (OPT_Wparentheses,
10549 "suggest parentheses around arithmetic in operand of %<|%>");
10550 /* Check cases like x|y==z */
10551 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10552 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10553 warning (OPT_Wparentheses,
10554 "suggest parentheses around comparison in operand of %<|%>");
10555 /* Check cases like !x | y */
10556 else if (code_left == TRUTH_NOT_EXPR
10557 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10558 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10559 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10560 return;
10562 case BIT_XOR_EXPR:
10563 if (code_left == BIT_AND_EXPR
10564 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10565 || code_right == BIT_AND_EXPR
10566 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10567 warning (OPT_Wparentheses,
10568 "suggest parentheses around arithmetic in operand of %<^%>");
10569 /* Check cases like x^y==z */
10570 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10571 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10572 warning (OPT_Wparentheses,
10573 "suggest parentheses around comparison in operand of %<^%>");
10574 return;
10576 case BIT_AND_EXPR:
10577 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10578 warning (OPT_Wparentheses,
10579 "suggest parentheses around %<+%> in operand of %<&%>");
10580 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10581 warning (OPT_Wparentheses,
10582 "suggest parentheses around %<-%> in operand of %<&%>");
10583 /* Check cases like x&y==z */
10584 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10585 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10586 warning (OPT_Wparentheses,
10587 "suggest parentheses around comparison in operand of %<&%>");
10588 /* Check cases like !x & y */
10589 else if (code_left == TRUTH_NOT_EXPR
10590 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10591 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10592 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10593 return;
10595 case EQ_EXPR:
10596 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10597 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10598 warning (OPT_Wparentheses,
10599 "suggest parentheses around comparison in operand of %<==%>");
10600 return;
10601 case NE_EXPR:
10602 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10603 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10604 warning (OPT_Wparentheses,
10605 "suggest parentheses around comparison in operand of %<!=%>");
10606 return;
10608 default:
10609 if (TREE_CODE_CLASS (code) == tcc_comparison
10610 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
10611 && code_left != NE_EXPR && code_left != EQ_EXPR
10612 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10613 || (TREE_CODE_CLASS (code_right) == tcc_comparison
10614 && code_right != NE_EXPR && code_right != EQ_EXPR
10615 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
10616 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
10617 "have their mathematical meaning");
10618 return;
10620 #undef NOT_A_BOOLEAN_EXPR_P
10623 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10625 void
10626 warn_for_unused_label (tree label)
10628 if (!TREE_USED (label))
10630 if (DECL_INITIAL (label))
10631 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10632 else
10633 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10637 /* Warn for division by zero according to the value of DIVISOR. LOC
10638 is the location of the division operator. */
10640 void
10641 warn_for_div_by_zero (location_t loc, tree divisor)
10643 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10644 about division by zero. Do not issue a warning if DIVISOR has a
10645 floating-point type, since we consider 0.0/0.0 a valid way of
10646 generating a NaN. */
10647 if (c_inhibit_evaluation_warnings == 0
10648 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10649 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10652 /* Subroutine of build_binary_op. Give warnings for comparisons
10653 between signed and unsigned quantities that may fail. Do the
10654 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10655 so that casts will be considered, but default promotions won't
10658 LOCATION is the location of the comparison operator.
10660 The arguments of this function map directly to local variables
10661 of build_binary_op. */
10663 void
10664 warn_for_sign_compare (location_t location,
10665 tree orig_op0, tree orig_op1,
10666 tree op0, tree op1,
10667 tree result_type, enum tree_code resultcode)
10669 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10670 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10671 int unsignedp0, unsignedp1;
10673 /* In C++, check for comparison of different enum types. */
10674 if (c_dialect_cxx()
10675 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10676 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10677 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10678 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10680 warning_at (location,
10681 OPT_Wsign_compare, "comparison between types %qT and %qT",
10682 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10685 /* Do not warn if the comparison is being done in a signed type,
10686 since the signed type will only be chosen if it can represent
10687 all the values of the unsigned type. */
10688 if (!TYPE_UNSIGNED (result_type))
10689 /* OK */;
10690 /* Do not warn if both operands are unsigned. */
10691 else if (op0_signed == op1_signed)
10692 /* OK */;
10693 else
10695 tree sop, uop, base_type;
10696 bool ovf;
10698 if (op0_signed)
10699 sop = orig_op0, uop = orig_op1;
10700 else
10701 sop = orig_op1, uop = orig_op0;
10703 STRIP_TYPE_NOPS (sop);
10704 STRIP_TYPE_NOPS (uop);
10705 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10706 ? TREE_TYPE (result_type) : result_type);
10708 /* Do not warn if the signed quantity is an unsuffixed integer
10709 literal (or some static constant expression involving such
10710 literals or a conditional expression involving such literals)
10711 and it is non-negative. */
10712 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10713 /* OK */;
10714 /* Do not warn if the comparison is an equality operation, the
10715 unsigned quantity is an integral constant, and it would fit
10716 in the result if the result were signed. */
10717 else if (TREE_CODE (uop) == INTEGER_CST
10718 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10719 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10720 /* OK */;
10721 /* In C, do not warn if the unsigned quantity is an enumeration
10722 constant and its maximum value would fit in the result if the
10723 result were signed. */
10724 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10725 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10726 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
10727 c_common_signed_type (base_type)))
10728 /* OK */;
10729 else
10730 warning_at (location,
10731 OPT_Wsign_compare,
10732 "comparison between signed and unsigned integer expressions");
10735 /* Warn if two unsigned values are being compared in a size larger
10736 than their original size, and one (and only one) is the result of
10737 a `~' operator. This comparison will always fail.
10739 Also warn if one operand is a constant, and the constant does not
10740 have all bits set that are set in the ~ operand when it is
10741 extended. */
10743 op0 = c_common_get_narrower (op0, &unsignedp0);
10744 op1 = c_common_get_narrower (op1, &unsignedp1);
10746 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10747 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10749 if (TREE_CODE (op0) == BIT_NOT_EXPR)
10750 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
10751 if (TREE_CODE (op1) == BIT_NOT_EXPR)
10752 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
10754 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10756 tree primop;
10757 HOST_WIDE_INT constant, mask;
10758 int unsignedp;
10759 unsigned int bits;
10761 if (host_integerp (op0, 0))
10763 primop = op1;
10764 unsignedp = unsignedp1;
10765 constant = tree_low_cst (op0, 0);
10767 else
10769 primop = op0;
10770 unsignedp = unsignedp0;
10771 constant = tree_low_cst (op1, 0);
10774 bits = TYPE_PRECISION (TREE_TYPE (primop));
10775 if (bits < TYPE_PRECISION (result_type)
10776 && bits < HOST_BITS_PER_LONG && unsignedp)
10778 mask = (~ (HOST_WIDE_INT) 0) << bits;
10779 if ((mask & constant) != mask)
10781 if (constant == 0)
10782 warning (OPT_Wsign_compare,
10783 "promoted ~unsigned is always non-zero");
10784 else
10785 warning_at (location, OPT_Wsign_compare,
10786 "comparison of promoted ~unsigned with constant");
10790 else if (unsignedp0 && unsignedp1
10791 && (TYPE_PRECISION (TREE_TYPE (op0))
10792 < TYPE_PRECISION (result_type))
10793 && (TYPE_PRECISION (TREE_TYPE (op1))
10794 < TYPE_PRECISION (result_type)))
10795 warning_at (location, OPT_Wsign_compare,
10796 "comparison of promoted ~unsigned with unsigned");
10800 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10801 type via c_common_type. If -Wdouble-promotion is in use, and the
10802 conditions for warning have been met, issue a warning. GMSGID is
10803 the warning message. It must have two %T specifiers for the type
10804 that was converted (generally "float") and the type to which it was
10805 converted (generally "double), respectively. LOC is the location
10806 to which the awrning should refer. */
10808 void
10809 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10810 const char *gmsgid, location_t loc)
10812 tree source_type;
10814 if (!warn_double_promotion)
10815 return;
10816 /* If the conversion will not occur at run-time, there is no need to
10817 warn about it. */
10818 if (c_inhibit_evaluation_warnings)
10819 return;
10820 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10821 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10822 return;
10823 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10824 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10825 source_type = type1;
10826 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10827 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10828 source_type = type2;
10829 else
10830 return;
10831 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10834 /* Setup a TYPE_DECL node as a typedef representation.
10836 X is a TYPE_DECL for a typedef statement. Create a brand new
10837 ..._TYPE node (which will be just a variant of the existing
10838 ..._TYPE node with identical properties) and then install X
10839 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10841 The whole point here is to end up with a situation where each
10842 and every ..._TYPE node the compiler creates will be uniquely
10843 associated with AT MOST one node representing a typedef name.
10844 This way, even though the compiler substitutes corresponding
10845 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10846 early on, later parts of the compiler can always do the reverse
10847 translation and get back the corresponding typedef name. For
10848 example, given:
10850 typedef struct S MY_TYPE;
10851 MY_TYPE object;
10853 Later parts of the compiler might only know that `object' was of
10854 type `struct S' if it were not for code just below. With this
10855 code however, later parts of the compiler see something like:
10857 struct S' == struct S
10858 typedef struct S' MY_TYPE;
10859 struct S' object;
10861 And they can then deduce (from the node for type struct S') that
10862 the original object declaration was:
10864 MY_TYPE object;
10866 Being able to do this is important for proper support of protoize,
10867 and also for generating precise symbolic debugging information
10868 which takes full account of the programmer's (typedef) vocabulary.
10870 Obviously, we don't want to generate a duplicate ..._TYPE node if
10871 the TYPE_DECL node that we are now processing really represents a
10872 standard built-in type. */
10874 void
10875 set_underlying_type (tree x)
10877 if (x == error_mark_node)
10878 return;
10879 if (DECL_IS_BUILTIN (x))
10881 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10882 TYPE_NAME (TREE_TYPE (x)) = x;
10884 else if (TREE_TYPE (x) != error_mark_node
10885 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10887 tree tt = TREE_TYPE (x);
10888 DECL_ORIGINAL_TYPE (x) = tt;
10889 tt = build_variant_type_copy (tt);
10890 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10891 TYPE_NAME (tt) = x;
10892 TREE_USED (tt) = TREE_USED (x);
10893 TREE_TYPE (x) = tt;
10897 /* Record the types used by the current global variable declaration
10898 being parsed, so that we can decide later to emit their debug info.
10899 Those types are in types_used_by_cur_var_decl, and we are going to
10900 store them in the types_used_by_vars_hash hash table.
10901 DECL is the declaration of the global variable that has been parsed. */
10903 void
10904 record_types_used_by_current_var_decl (tree decl)
10906 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10908 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10910 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10911 types_used_by_var_decl_insert (type, decl);
10915 /* If DECL is a typedef that is declared in the current function,
10916 record it for the purpose of -Wunused-local-typedefs. */
10918 void
10919 record_locally_defined_typedef (tree decl)
10921 struct c_language_function *l;
10923 if (!warn_unused_local_typedefs
10924 || cfun == NULL
10925 /* if this is not a locally defined typedef then we are not
10926 interested. */
10927 || !is_typedef_decl (decl)
10928 || !decl_function_context (decl))
10929 return;
10931 l = (struct c_language_function *) cfun->language;
10932 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10935 /* If T is a TYPE_DECL declared locally, mark it as used. */
10937 void
10938 maybe_record_typedef_use (tree t)
10940 if (!is_typedef_decl (t))
10941 return;
10943 TREE_USED (t) = true;
10946 /* Warn if there are some unused locally defined typedefs in the
10947 current function. */
10949 void
10950 maybe_warn_unused_local_typedefs (void)
10952 int i;
10953 tree decl;
10954 /* The number of times we have emitted -Wunused-local-typedefs
10955 warnings. If this is different from errorcount, that means some
10956 unrelated errors have been issued. In which case, we'll avoid
10957 emitting "unused-local-typedefs" warnings. */
10958 static int unused_local_typedefs_warn_count;
10959 struct c_language_function *l;
10961 if (cfun == NULL)
10962 return;
10964 if ((l = (struct c_language_function *) cfun->language) == NULL)
10965 return;
10967 if (warn_unused_local_typedefs
10968 && errorcount == unused_local_typedefs_warn_count)
10970 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
10971 if (!TREE_USED (decl))
10972 warning_at (DECL_SOURCE_LOCATION (decl),
10973 OPT_Wunused_local_typedefs,
10974 "typedef %qD locally defined but not used", decl);
10975 unused_local_typedefs_warn_count = errorcount;
10978 if (l->local_typedefs)
10980 VEC_free (tree, gc, l->local_typedefs);
10981 l->local_typedefs = NULL;
10985 /* The C and C++ parsers both use vectors to hold function arguments.
10986 For efficiency, we keep a cache of unused vectors. This is the
10987 cache. */
10989 typedef VEC(tree,gc)* tree_gc_vec;
10990 DEF_VEC_P(tree_gc_vec);
10991 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
10992 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
10994 /* Return a new vector from the cache. If the cache is empty,
10995 allocate a new vector. These vectors are GC'ed, so it is OK if the
10996 pointer is not released.. */
10998 VEC(tree,gc) *
10999 make_tree_vector (void)
11001 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
11002 return VEC_pop (tree_gc_vec, tree_vector_cache);
11003 else
11005 /* Passing 0 to VEC_alloc returns NULL, and our callers require
11006 that we always return a non-NULL value. The vector code uses
11007 4 when growing a NULL vector, so we do too. */
11008 return VEC_alloc (tree, gc, 4);
11012 /* Release a vector of trees back to the cache. */
11014 void
11015 release_tree_vector (VEC(tree,gc) *vec)
11017 if (vec != NULL)
11019 VEC_truncate (tree, vec, 0);
11020 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
11024 /* Get a new tree vector holding a single tree. */
11026 VEC(tree,gc) *
11027 make_tree_vector_single (tree t)
11029 VEC(tree,gc) *ret = make_tree_vector ();
11030 VEC_quick_push (tree, ret, t);
11031 return ret;
11034 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11036 VEC(tree,gc) *
11037 make_tree_vector_from_list (tree list)
11039 VEC(tree,gc) *ret = make_tree_vector ();
11040 for (; list; list = TREE_CHAIN (list))
11041 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
11042 return ret;
11045 /* Get a new tree vector which is a copy of an existing one. */
11047 VEC(tree,gc) *
11048 make_tree_vector_copy (const VEC(tree,gc) *orig)
11050 VEC(tree,gc) *ret;
11051 unsigned int ix;
11052 tree t;
11054 ret = make_tree_vector ();
11055 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
11056 FOR_EACH_VEC_ELT (tree, orig, ix, t)
11057 VEC_quick_push (tree, ret, t);
11058 return ret;
11061 /* Return true if KEYWORD starts a type specifier. */
11063 bool
11064 keyword_begins_type_specifier (enum rid keyword)
11066 switch (keyword)
11068 case RID_INT:
11069 case RID_CHAR:
11070 case RID_FLOAT:
11071 case RID_DOUBLE:
11072 case RID_VOID:
11073 case RID_INT128:
11074 case RID_UNSIGNED:
11075 case RID_LONG:
11076 case RID_SHORT:
11077 case RID_SIGNED:
11078 case RID_DFLOAT32:
11079 case RID_DFLOAT64:
11080 case RID_DFLOAT128:
11081 case RID_FRACT:
11082 case RID_ACCUM:
11083 case RID_BOOL:
11084 case RID_WCHAR:
11085 case RID_CHAR16:
11086 case RID_CHAR32:
11087 case RID_SAT:
11088 case RID_COMPLEX:
11089 case RID_TYPEOF:
11090 case RID_STRUCT:
11091 case RID_CLASS:
11092 case RID_UNION:
11093 case RID_ENUM:
11094 return true;
11095 default:
11096 return false;
11100 /* Return true if KEYWORD names a type qualifier. */
11102 bool
11103 keyword_is_type_qualifier (enum rid keyword)
11105 switch (keyword)
11107 case RID_CONST:
11108 case RID_VOLATILE:
11109 case RID_RESTRICT:
11110 return true;
11111 default:
11112 return false;
11116 /* Return true if KEYWORD names a storage class specifier.
11118 RID_TYPEDEF is not included in this list despite `typedef' being
11119 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11120 such for syntactic convenience only. */
11122 bool
11123 keyword_is_storage_class_specifier (enum rid keyword)
11125 switch (keyword)
11127 case RID_STATIC:
11128 case RID_EXTERN:
11129 case RID_REGISTER:
11130 case RID_AUTO:
11131 case RID_MUTABLE:
11132 case RID_THREAD:
11133 return true;
11134 default:
11135 return false;
11139 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11141 static bool
11142 keyword_is_function_specifier (enum rid keyword)
11144 switch (keyword)
11146 case RID_INLINE:
11147 case RID_NORETURN:
11148 case RID_VIRTUAL:
11149 case RID_EXPLICIT:
11150 return true;
11151 default:
11152 return false;
11156 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11157 declaration-specifier (C99 6.7). */
11159 bool
11160 keyword_is_decl_specifier (enum rid keyword)
11162 if (keyword_is_storage_class_specifier (keyword)
11163 || keyword_is_type_qualifier (keyword)
11164 || keyword_is_function_specifier (keyword))
11165 return true;
11167 switch (keyword)
11169 case RID_TYPEDEF:
11170 case RID_FRIEND:
11171 case RID_CONSTEXPR:
11172 return true;
11173 default:
11174 return false;
11178 /* Initialize language-specific-bits of tree_contains_struct. */
11180 void
11181 c_common_init_ts (void)
11183 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11184 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11187 /* Build a user-defined numeric literal out of an integer constant type VALUE
11188 with identifier SUFFIX. */
11190 tree
11191 build_userdef_literal (tree suffix_id, tree value, tree num_string)
11193 tree literal = make_node (USERDEF_LITERAL);
11194 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11195 USERDEF_LITERAL_VALUE (literal) = value;
11196 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11197 return literal;
11200 /* For vector[index], convert the vector to a
11201 pointer of the underlying type. */
11202 void
11203 convert_vector_to_pointer_for_subscript (location_t loc,
11204 tree* vecp, tree index)
11206 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11208 tree type = TREE_TYPE (*vecp);
11209 tree type1;
11211 if (TREE_CODE (index) == INTEGER_CST)
11212 if (!host_integerp (index, 1)
11213 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11214 >= TYPE_VECTOR_SUBPARTS (type)))
11215 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11217 c_common_mark_addressable_vec (*vecp);
11218 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11219 type = build_pointer_type (type);
11220 type1 = build_pointer_type (TREE_TYPE (*vecp));
11221 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11222 *vecp = convert (type, *vecp);
11226 #include "gt-c-family-c-common.h"