* c-common.c: Fix typo.
[official-gcc.git] / gcc / c-family / c-common.c
blob3a1bbd253473407f241a199be5234000b910b6b6
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 "output.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "c-common.h"
33 #include "c-objc.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"
49 #include "libfuncs.h"
51 cpp_reader *parse_in; /* Declared in c-pragma.h. */
53 /* The following symbols are subsumed in the c_global_trees array, and
54 listed here individually for documentation purposes.
56 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
58 tree short_integer_type_node;
59 tree long_integer_type_node;
60 tree long_long_integer_type_node;
61 tree int128_integer_type_node;
63 tree short_unsigned_type_node;
64 tree long_unsigned_type_node;
65 tree long_long_unsigned_type_node;
66 tree int128_unsigned_type_node;
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
72 tree ptrdiff_type_node;
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
78 tree char16_type_node;
79 tree char32_type_node;
81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
109 Nodes for types `void *' and `const void *'.
111 tree ptr_type_node, const_ptr_type_node;
113 Nodes for types `char *' and `const char *'.
115 tree string_type_node, const_string_type_node;
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
120 tree char_array_type_node;
122 Type `int[SOMENUMBER]' or something like it.
123 Used when an array of int needed and the size is irrelevant.
125 tree int_array_type_node;
127 Type `wchar_t[SOMENUMBER]' or something like it.
128 Used when a wide string literal is created.
130 tree wchar_array_type_node;
132 Type `char16_t[SOMENUMBER]' or something like it.
133 Used when a UTF-16 string literal is created.
135 tree char16_array_type_node;
137 Type `char32_t[SOMENUMBER]' or something like it.
138 Used when a UTF-32 string literal is created.
140 tree char32_array_type_node;
142 Type `int ()' -- used for implicit declaration of functions.
144 tree default_function_type;
146 A VOID_TYPE node, packaged in a TREE_LIST.
148 tree void_list_node;
150 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
151 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
152 VAR_DECLS, but C++ does.)
154 tree function_name_decl_node;
155 tree pretty_function_name_decl_node;
156 tree c99_function_name_decl_node;
158 Stack of nested function name VAR_DECLs.
160 tree saved_function_name_decls;
164 tree c_global_trees[CTI_MAX];
166 /* Switches common to the C front ends. */
168 /* Nonzero means don't output line number information. */
170 char flag_no_line_commands;
172 /* Nonzero causes -E output not to be done, but directives such as
173 #define that have side effects are still obeyed. */
175 char flag_no_output;
177 /* Nonzero means dump macros in some fashion. */
179 char flag_dump_macros;
181 /* Nonzero means pass #include lines through to the output. */
183 char flag_dump_includes;
185 /* Nonzero means process PCH files while preprocessing. */
187 bool flag_pch_preprocess;
189 /* The file name to which we should write a precompiled header, or
190 NULL if no header will be written in this compile. */
192 const char *pch_file;
194 /* Nonzero if an ISO standard was selected. It rejects macros in the
195 user's namespace. */
196 int flag_iso;
198 /* Warn about #pragma directives that are not recognized. */
200 int warn_unknown_pragmas; /* Tri state variable. */
202 /* Warn about format/argument anomalies in calls to formatted I/O functions
203 (*printf, *scanf, strftime, strfmon, etc.). */
205 int warn_format;
207 /* C/ObjC language option variables. */
210 /* Nonzero means allow type mismatches in conditional expressions;
211 just make their values `void'. */
213 int flag_cond_mismatch;
215 /* Nonzero means enable C89 Amendment 1 features. */
217 int flag_isoc94;
219 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
221 int flag_isoc99;
223 /* Nonzero means use the ISO C11 dialect of C. */
225 int flag_isoc11;
227 /* Nonzero means that we have builtin functions, and main is an int. */
229 int flag_hosted = 1;
232 /* ObjC language option variables. */
235 /* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
239 int print_struct_values;
241 /* Tells the compiler what is the constant string class for ObjC. */
243 const char *constant_string_class_name;
246 /* C++ language option variables. */
249 /* Nonzero means generate separate instantiation control files and
250 juggle them at link time. */
252 int flag_use_repository;
254 /* The C++ dialect being used. C++98 is the default. */
256 enum cxx_dialect cxx_dialect = cxx98;
258 /* Maximum template instantiation depth. This limit exists to limit the
259 time it takes to notice excessively recursive template instantiations.
261 The default is lower than the 1024 recommended by the C++0x standard
262 because G++ runs out of stack before 1024 with highly recursive template
263 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
265 int max_tinst_depth = 900;
267 /* The elements of `ridpointers' are identifier nodes for the reserved
268 type names and storage classes. It is indexed by a RID_... value. */
269 tree *ridpointers;
271 tree (*make_fname_decl) (location_t, tree, int);
273 /* Nonzero means don't warn about problems that occur when the code is
274 executed. */
275 int c_inhibit_evaluation_warnings;
277 /* Whether we are building a boolean conversion inside
278 convert_for_assignment, or some other late binary operation. If
279 build_binary_op is called for C (from code shared by C and C++) in
280 this case, then the operands have already been folded and the
281 result will not be folded again, so C_MAYBE_CONST_EXPR should not
282 be generated. */
283 bool in_late_binary_op;
285 /* Whether lexing has been completed, so subsequent preprocessor
286 errors should use the compiler's input_location. */
287 bool done_lexing = false;
289 /* Information about how a function name is generated. */
290 struct fname_var_t
292 tree *const decl; /* pointer to the VAR_DECL. */
293 const unsigned rid; /* RID number for the identifier. */
294 const int pretty; /* How pretty is it? */
297 /* The three ways of getting then name of the current function. */
299 const struct fname_var_t fname_vars[] =
301 /* C99 compliant __func__, must be first. */
302 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
303 /* GCC __FUNCTION__ compliant. */
304 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
305 /* GCC __PRETTY_FUNCTION__ compliant. */
306 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
307 {NULL, 0, 0},
310 /* Global visibility options. */
311 struct visibility_flags visibility_options;
313 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
314 static tree check_case_value (tree);
315 static bool check_case_bounds (tree, tree, tree *, tree *);
317 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_always_inline_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
335 bool *);
336 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
339 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
345 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
346 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
349 static tree handle_visibility_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_tls_model_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_no_instrument_function_attribute (tree *, tree,
354 tree, int, bool *);
355 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_deprecated_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_vector_size_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
371 bool *);
372 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
377 static tree ignore_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
381 static void check_function_nonnull (tree, int, tree *);
382 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
383 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
384 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
385 static int resort_field_decl_cmp (const void *, const void *);
387 /* Reserved words. The third field is a mask: keywords are disabled
388 if they match the mask.
390 Masks for languages:
391 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
392 C --std=c99: D_CXXONLY | D_OBJC
393 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
394 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
395 C++ --std=c0x: D_CONLY | D_OBJC
396 ObjC++ is like C++ except that D_OBJC is not set
398 If -fno-asm is used, D_ASM is added to the mask. If
399 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
400 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
401 In C with -Wc++-compat, we warn if D_CXXWARN is set.
403 Note the complication of the D_CXX_OBJC keywords. These are
404 reserved words such as 'class'. In C++, 'class' is a reserved
405 word. In Objective-C++ it is too. In Objective-C, it is a
406 reserved word too, but only if it follows an '@' sign.
408 const struct c_common_resword c_common_reswords[] =
410 { "_Alignas", RID_ALIGNAS, D_CONLY },
411 { "_Alignof", RID_ALIGNOF, D_CONLY },
412 { "_Bool", RID_BOOL, D_CONLY },
413 { "_Complex", RID_COMPLEX, 0 },
414 { "_Imaginary", RID_IMAGINARY, D_CONLY },
415 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
416 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
417 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
418 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
419 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
420 { "_Sat", RID_SAT, D_CONLY | D_EXT },
421 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
422 { "_Noreturn", RID_NORETURN, D_CONLY },
423 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
424 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
425 { "__alignof", RID_ALIGNOF, 0 },
426 { "__alignof__", RID_ALIGNOF, 0 },
427 { "__asm", RID_ASM, 0 },
428 { "__asm__", RID_ASM, 0 },
429 { "__attribute", RID_ATTRIBUTE, 0 },
430 { "__attribute__", RID_ATTRIBUTE, 0 },
431 { "__bases", RID_BASES, D_CXXONLY },
432 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
433 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
434 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, D_CONLY },
435 { "__builtin_offsetof", RID_OFFSETOF, 0 },
436 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
437 { "__builtin_va_arg", RID_VA_ARG, 0 },
438 { "__complex", RID_COMPLEX, 0 },
439 { "__complex__", RID_COMPLEX, 0 },
440 { "__const", RID_CONST, 0 },
441 { "__const__", RID_CONST, 0 },
442 { "__decltype", RID_DECLTYPE, D_CXXONLY },
443 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
444 { "__extension__", RID_EXTENSION, 0 },
445 { "__func__", RID_C99_FUNCTION_NAME, 0 },
446 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
447 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
448 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
449 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
450 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
451 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
452 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
453 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
454 { "__imag", RID_IMAGPART, 0 },
455 { "__imag__", RID_IMAGPART, 0 },
456 { "__inline", RID_INLINE, 0 },
457 { "__inline__", RID_INLINE, 0 },
458 { "__int128", RID_INT128, 0 },
459 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
460 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
461 { "__is_class", RID_IS_CLASS, D_CXXONLY },
462 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
463 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
464 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
465 { "__is_final", RID_IS_FINAL, D_CXXONLY },
466 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
467 { "__is_pod", RID_IS_POD, D_CXXONLY },
468 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
469 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
470 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
471 { "__is_union", RID_IS_UNION, D_CXXONLY },
472 { "__label__", RID_LABEL, 0 },
473 { "__null", RID_NULL, 0 },
474 { "__real", RID_REALPART, 0 },
475 { "__real__", RID_REALPART, 0 },
476 { "__restrict", RID_RESTRICT, 0 },
477 { "__restrict__", RID_RESTRICT, 0 },
478 { "__signed", RID_SIGNED, 0 },
479 { "__signed__", RID_SIGNED, 0 },
480 { "__thread", RID_THREAD, 0 },
481 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
482 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
483 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
484 { "__typeof", RID_TYPEOF, 0 },
485 { "__typeof__", RID_TYPEOF, 0 },
486 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
487 { "__volatile", RID_VOLATILE, 0 },
488 { "__volatile__", RID_VOLATILE, 0 },
489 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
490 { "asm", RID_ASM, D_ASM },
491 { "auto", RID_AUTO, 0 },
492 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
493 { "break", RID_BREAK, 0 },
494 { "case", RID_CASE, 0 },
495 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
496 { "char", RID_CHAR, 0 },
497 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
500 { "const", RID_CONST, 0 },
501 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
502 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
503 { "continue", RID_CONTINUE, 0 },
504 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
505 { "default", RID_DEFAULT, 0 },
506 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
507 { "do", RID_DO, 0 },
508 { "double", RID_DOUBLE, 0 },
509 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
510 { "else", RID_ELSE, 0 },
511 { "enum", RID_ENUM, 0 },
512 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
513 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
514 { "extern", RID_EXTERN, 0 },
515 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
516 { "float", RID_FLOAT, 0 },
517 { "for", RID_FOR, 0 },
518 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
519 { "goto", RID_GOTO, 0 },
520 { "if", RID_IF, 0 },
521 { "inline", RID_INLINE, D_EXT89 },
522 { "int", RID_INT, 0 },
523 { "long", RID_LONG, 0 },
524 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
525 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
526 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
527 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
530 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
531 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
532 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
533 { "register", RID_REGISTER, 0 },
534 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
535 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
536 { "return", RID_RETURN, 0 },
537 { "short", RID_SHORT, 0 },
538 { "signed", RID_SIGNED, 0 },
539 { "sizeof", RID_SIZEOF, 0 },
540 { "static", RID_STATIC, 0 },
541 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
542 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
543 { "struct", RID_STRUCT, 0 },
544 { "switch", RID_SWITCH, 0 },
545 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
546 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
547 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
548 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
549 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
550 { "typedef", RID_TYPEDEF, 0 },
551 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
552 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
553 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
554 { "union", RID_UNION, 0 },
555 { "unsigned", RID_UNSIGNED, 0 },
556 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
557 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
558 { "void", RID_VOID, 0 },
559 { "volatile", RID_VOLATILE, 0 },
560 { "wchar_t", RID_WCHAR, D_CXXONLY },
561 { "while", RID_WHILE, 0 },
562 /* These Objective-C keywords are recognized only immediately after
563 an '@'. */
564 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
565 { "defs", RID_AT_DEFS, D_OBJC },
566 { "encode", RID_AT_ENCODE, D_OBJC },
567 { "end", RID_AT_END, D_OBJC },
568 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
569 { "interface", RID_AT_INTERFACE, D_OBJC },
570 { "protocol", RID_AT_PROTOCOL, D_OBJC },
571 { "selector", RID_AT_SELECTOR, D_OBJC },
572 { "finally", RID_AT_FINALLY, D_OBJC },
573 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
574 { "optional", RID_AT_OPTIONAL, D_OBJC },
575 { "required", RID_AT_REQUIRED, D_OBJC },
576 { "property", RID_AT_PROPERTY, D_OBJC },
577 { "package", RID_AT_PACKAGE, D_OBJC },
578 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
579 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
580 /* These are recognized only in protocol-qualifier context
581 (see above) */
582 { "bycopy", RID_BYCOPY, D_OBJC },
583 { "byref", RID_BYREF, D_OBJC },
584 { "in", RID_IN, D_OBJC },
585 { "inout", RID_INOUT, D_OBJC },
586 { "oneway", RID_ONEWAY, D_OBJC },
587 { "out", RID_OUT, D_OBJC },
588 /* These are recognized inside a property attribute list */
589 { "assign", RID_ASSIGN, D_OBJC },
590 { "copy", RID_COPY, D_OBJC },
591 { "getter", RID_GETTER, D_OBJC },
592 { "nonatomic", RID_NONATOMIC, D_OBJC },
593 { "readonly", RID_READONLY, D_OBJC },
594 { "readwrite", RID_READWRITE, D_OBJC },
595 { "retain", RID_RETAIN, D_OBJC },
596 { "setter", RID_SETTER, D_OBJC },
599 const unsigned int num_c_common_reswords =
600 sizeof c_common_reswords / sizeof (struct c_common_resword);
602 /* Table of machine-independent attributes common to all C-like languages. */
603 const struct attribute_spec c_common_attribute_table[] =
605 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
606 affects_type_identity } */
607 { "packed", 0, 0, false, false, false,
608 handle_packed_attribute , false},
609 { "nocommon", 0, 0, true, false, false,
610 handle_nocommon_attribute, false},
611 { "common", 0, 0, true, false, false,
612 handle_common_attribute, false },
613 /* FIXME: logically, noreturn attributes should be listed as
614 "false, true, true" and apply to function types. But implementing this
615 would require all the places in the compiler that use TREE_THIS_VOLATILE
616 on a decl to identify non-returning functions to be located and fixed
617 to check the function type instead. */
618 { "noreturn", 0, 0, true, false, false,
619 handle_noreturn_attribute, false },
620 { "volatile", 0, 0, true, false, false,
621 handle_noreturn_attribute, false },
622 { "noinline", 0, 0, true, false, false,
623 handle_noinline_attribute, false },
624 { "noclone", 0, 0, true, false, false,
625 handle_noclone_attribute, false },
626 { "leaf", 0, 0, true, false, false,
627 handle_leaf_attribute, false },
628 { "always_inline", 0, 0, true, false, false,
629 handle_always_inline_attribute, false },
630 { "gnu_inline", 0, 0, true, false, false,
631 handle_gnu_inline_attribute, false },
632 { "artificial", 0, 0, true, false, false,
633 handle_artificial_attribute, false },
634 { "flatten", 0, 0, true, false, false,
635 handle_flatten_attribute, false },
636 { "used", 0, 0, true, false, false,
637 handle_used_attribute, false },
638 { "unused", 0, 0, false, false, false,
639 handle_unused_attribute, false },
640 { "externally_visible", 0, 0, true, false, false,
641 handle_externally_visible_attribute, false },
642 /* The same comments as for noreturn attributes apply to const ones. */
643 { "const", 0, 0, true, false, false,
644 handle_const_attribute, false },
645 { "transparent_union", 0, 0, false, false, false,
646 handle_transparent_union_attribute, false },
647 { "constructor", 0, 1, true, false, false,
648 handle_constructor_attribute, false },
649 { "destructor", 0, 1, true, false, false,
650 handle_destructor_attribute, false },
651 { "mode", 1, 1, false, true, false,
652 handle_mode_attribute, false },
653 { "section", 1, 1, true, false, false,
654 handle_section_attribute, false },
655 { "aligned", 0, 1, false, false, false,
656 handle_aligned_attribute, false },
657 { "weak", 0, 0, true, false, false,
658 handle_weak_attribute, false },
659 { "ifunc", 1, 1, true, false, false,
660 handle_ifunc_attribute, false },
661 { "alias", 1, 1, true, false, false,
662 handle_alias_attribute, false },
663 { "weakref", 0, 1, true, false, false,
664 handle_weakref_attribute, false },
665 { "no_instrument_function", 0, 0, true, false, false,
666 handle_no_instrument_function_attribute,
667 false },
668 { "malloc", 0, 0, true, false, false,
669 handle_malloc_attribute, false },
670 { "returns_twice", 0, 0, true, false, false,
671 handle_returns_twice_attribute, false },
672 { "no_stack_limit", 0, 0, true, false, false,
673 handle_no_limit_stack_attribute, false },
674 { "pure", 0, 0, true, false, false,
675 handle_pure_attribute, false },
676 { "transaction_callable", 0, 0, false, true, false,
677 handle_tm_attribute, false },
678 { "transaction_unsafe", 0, 0, false, true, false,
679 handle_tm_attribute, false },
680 { "transaction_safe", 0, 0, false, true, false,
681 handle_tm_attribute, false },
682 { "transaction_may_cancel_outer", 0, 0, false, true, false,
683 handle_tm_attribute, false },
684 /* ??? These two attributes didn't make the transition from the
685 Intel language document to the multi-vendor language document. */
686 { "transaction_pure", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_wrap", 1, 1, true, false, false,
689 handle_tm_wrap_attribute, false },
690 /* For internal use (marking of builtins) only. The name contains space
691 to prevent its usage in source code. */
692 { "no vops", 0, 0, true, false, false,
693 handle_novops_attribute, false },
694 { "deprecated", 0, 1, false, false, false,
695 handle_deprecated_attribute, false },
696 { "vector_size", 1, 1, false, true, false,
697 handle_vector_size_attribute, false },
698 { "visibility", 1, 1, false, false, false,
699 handle_visibility_attribute, false },
700 { "tls_model", 1, 1, true, false, false,
701 handle_tls_model_attribute, false },
702 { "nonnull", 0, -1, false, true, true,
703 handle_nonnull_attribute, false },
704 { "nothrow", 0, 0, true, false, false,
705 handle_nothrow_attribute, false },
706 { "may_alias", 0, 0, false, true, false, NULL, false },
707 { "cleanup", 1, 1, true, false, false,
708 handle_cleanup_attribute, false },
709 { "warn_unused_result", 0, 0, false, true, true,
710 handle_warn_unused_result_attribute, false },
711 { "sentinel", 0, 1, false, true, true,
712 handle_sentinel_attribute, false },
713 /* For internal use (marking of builtins) only. The name contains space
714 to prevent its usage in source code. */
715 { "type generic", 0, 0, false, true, true,
716 handle_type_generic_attribute, false },
717 { "alloc_size", 1, 2, false, true, true,
718 handle_alloc_size_attribute, false },
719 { "cold", 0, 0, true, false, false,
720 handle_cold_attribute, false },
721 { "hot", 0, 0, true, false, false,
722 handle_hot_attribute, false },
723 { "warning", 1, 1, true, false, false,
724 handle_error_attribute, false },
725 { "error", 1, 1, true, false, false,
726 handle_error_attribute, false },
727 { "target", 1, -1, true, false, false,
728 handle_target_attribute, false },
729 { "optimize", 1, -1, true, false, false,
730 handle_optimize_attribute, false },
731 /* For internal use only. The leading '*' both prevents its usage in
732 source code and signals that it may be overridden by machine tables. */
733 { "*tm regparm", 0, 0, false, true, true,
734 ignore_attribute, false },
735 { "no_split_stack", 0, 0, true, false, false,
736 handle_no_split_stack_attribute, false },
737 /* For internal use (marking of builtins and runtime functions) only.
738 The name contains space to prevent its usage in source code. */
739 { "fn spec", 1, 1, false, true, true,
740 handle_fnspec_attribute, false },
741 { NULL, 0, 0, false, false, false, NULL, false }
744 /* Give the specifications for the format attributes, used by C and all
745 descendants. */
747 const struct attribute_spec c_common_format_attribute_table[] =
749 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
750 affects_type_identity } */
751 { "format", 3, 3, false, true, true,
752 handle_format_attribute, false },
753 { "format_arg", 1, 1, false, true, true,
754 handle_format_arg_attribute, false },
755 { NULL, 0, 0, false, false, false, NULL, false }
758 /* Return identifier for address space AS. */
760 const char *
761 c_addr_space_name (addr_space_t as)
763 int rid = RID_FIRST_ADDR_SPACE + as;
764 gcc_assert (ridpointers [rid]);
765 return IDENTIFIER_POINTER (ridpointers [rid]);
768 /* Push current bindings for the function name VAR_DECLS. */
770 void
771 start_fname_decls (void)
773 unsigned ix;
774 tree saved = NULL_TREE;
776 for (ix = 0; fname_vars[ix].decl; ix++)
778 tree decl = *fname_vars[ix].decl;
780 if (decl)
782 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
783 saved);
784 *fname_vars[ix].decl = NULL_TREE;
787 if (saved || saved_function_name_decls)
788 /* Normally they'll have been NULL, so only push if we've got a
789 stack, or they are non-NULL. */
790 saved_function_name_decls = tree_cons (saved, NULL_TREE,
791 saved_function_name_decls);
794 /* Finish up the current bindings, adding them into the current function's
795 statement tree. This must be done _before_ finish_stmt_tree is called.
796 If there is no current function, we must be at file scope and no statements
797 are involved. Pop the previous bindings. */
799 void
800 finish_fname_decls (void)
802 unsigned ix;
803 tree stmts = NULL_TREE;
804 tree stack = saved_function_name_decls;
806 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
807 append_to_statement_list (TREE_VALUE (stack), &stmts);
809 if (stmts)
811 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
813 if (TREE_CODE (*bodyp) == BIND_EXPR)
814 bodyp = &BIND_EXPR_BODY (*bodyp);
816 append_to_statement_list_force (*bodyp, &stmts);
817 *bodyp = stmts;
820 for (ix = 0; fname_vars[ix].decl; ix++)
821 *fname_vars[ix].decl = NULL_TREE;
823 if (stack)
825 /* We had saved values, restore them. */
826 tree saved;
828 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
830 tree decl = TREE_PURPOSE (saved);
831 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
833 *fname_vars[ix].decl = decl;
835 stack = TREE_CHAIN (stack);
837 saved_function_name_decls = stack;
840 /* Return the text name of the current function, suitably prettified
841 by PRETTY_P. Return string must be freed by caller. */
843 const char *
844 fname_as_string (int pretty_p)
846 const char *name = "top level";
847 char *namep;
848 int vrb = 2, len;
849 cpp_string cstr = { 0, 0 }, strname;
851 if (!pretty_p)
853 name = "";
854 vrb = 0;
857 if (current_function_decl)
858 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
860 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
862 namep = XNEWVEC (char, len);
863 snprintf (namep, len, "\"%s\"", name);
864 strname.text = (unsigned char *) namep;
865 strname.len = len - 1;
867 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
869 XDELETEVEC (namep);
870 return (const char *) cstr.text;
873 return namep;
876 /* Return the VAR_DECL for a const char array naming the current
877 function. If the VAR_DECL has not yet been created, create it
878 now. RID indicates how it should be formatted and IDENTIFIER_NODE
879 ID is its name (unfortunately C and C++ hold the RID values of
880 keywords in different places, so we can't derive RID from ID in
881 this language independent code. LOC is the location of the
882 function. */
884 tree
885 fname_decl (location_t loc, unsigned int rid, tree id)
887 unsigned ix;
888 tree decl = NULL_TREE;
890 for (ix = 0; fname_vars[ix].decl; ix++)
891 if (fname_vars[ix].rid == rid)
892 break;
894 decl = *fname_vars[ix].decl;
895 if (!decl)
897 /* If a tree is built here, it would normally have the lineno of
898 the current statement. Later this tree will be moved to the
899 beginning of the function and this line number will be wrong.
900 To avoid this problem set the lineno to 0 here; that prevents
901 it from appearing in the RTL. */
902 tree stmts;
903 location_t saved_location = input_location;
904 input_location = UNKNOWN_LOCATION;
906 stmts = push_stmt_list ();
907 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
908 stmts = pop_stmt_list (stmts);
909 if (!IS_EMPTY_STMT (stmts))
910 saved_function_name_decls
911 = tree_cons (decl, stmts, saved_function_name_decls);
912 *fname_vars[ix].decl = decl;
913 input_location = saved_location;
915 if (!ix && !current_function_decl)
916 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
918 return decl;
921 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
923 tree
924 fix_string_type (tree value)
926 int length = TREE_STRING_LENGTH (value);
927 int nchars;
928 tree e_type, i_type, a_type;
930 /* Compute the number of elements, for the array type. */
931 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
933 nchars = length;
934 e_type = char_type_node;
936 else if (TREE_TYPE (value) == char16_array_type_node)
938 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
939 e_type = char16_type_node;
941 else if (TREE_TYPE (value) == char32_array_type_node)
943 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
944 e_type = char32_type_node;
946 else
948 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
949 e_type = wchar_type_node;
952 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
953 limit in C++98 Annex B is very large (65536) and is not normative,
954 so we do not diagnose it (warn_overlength_strings is forced off
955 in c_common_post_options). */
956 if (warn_overlength_strings)
958 const int nchars_max = flag_isoc99 ? 4095 : 509;
959 const int relevant_std = flag_isoc99 ? 99 : 90;
960 if (nchars - 1 > nchars_max)
961 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
962 separate the %d from the 'C'. 'ISO' should not be
963 translated, but it may be moved after 'C%d' in languages
964 where modifiers follow nouns. */
965 pedwarn (input_location, OPT_Woverlength_strings,
966 "string length %qd is greater than the length %qd "
967 "ISO C%d compilers are required to support",
968 nchars - 1, nchars_max, relevant_std);
971 /* Create the array type for the string constant. The ISO C++
972 standard says that a string literal has type `const char[N]' or
973 `const wchar_t[N]'. We use the same logic when invoked as a C
974 front-end with -Wwrite-strings.
975 ??? We should change the type of an expression depending on the
976 state of a warning flag. We should just be warning -- see how
977 this is handled in the C++ front-end for the deprecated implicit
978 conversion from string literals to `char*' or `wchar_t*'.
980 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
981 array type being the unqualified version of that type.
982 Therefore, if we are constructing an array of const char, we must
983 construct the matching unqualified array type first. The C front
984 end does not require this, but it does no harm, so we do it
985 unconditionally. */
986 i_type = build_index_type (size_int (nchars - 1));
987 a_type = build_array_type (e_type, i_type);
988 if (c_dialect_cxx() || warn_write_strings)
989 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
991 TREE_TYPE (value) = a_type;
992 TREE_CONSTANT (value) = 1;
993 TREE_READONLY (value) = 1;
994 TREE_STATIC (value) = 1;
995 return value;
998 /* If DISABLE is true, stop issuing warnings. This is used when
999 parsing code that we know will not be executed. This function may
1000 be called multiple times, and works as a stack. */
1002 static void
1003 c_disable_warnings (bool disable)
1005 if (disable)
1007 ++c_inhibit_evaluation_warnings;
1008 fold_defer_overflow_warnings ();
1012 /* If ENABLE is true, reenable issuing warnings. */
1014 static void
1015 c_enable_warnings (bool enable)
1017 if (enable)
1019 --c_inhibit_evaluation_warnings;
1020 fold_undefer_and_ignore_overflow_warnings ();
1024 /* Fully fold EXPR, an expression that was not folded (beyond integer
1025 constant expressions and null pointer constants) when being built
1026 up. If IN_INIT, this is in a static initializer and certain
1027 changes are made to the folding done. Clear *MAYBE_CONST if
1028 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1029 expression because it contains an evaluated operator (in C99) or an
1030 operator outside of sizeof returning an integer constant (in C90)
1031 not permitted in constant expressions, or because it contains an
1032 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1033 set to true by callers before calling this function.) Return the
1034 folded expression. Function arguments have already been folded
1035 before calling this function, as have the contents of SAVE_EXPR,
1036 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1037 C_MAYBE_CONST_EXPR. */
1039 tree
1040 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1042 tree ret;
1043 tree eptype = NULL_TREE;
1044 bool dummy = true;
1045 bool maybe_const_itself = true;
1046 location_t loc = EXPR_LOCATION (expr);
1048 /* This function is not relevant to C++ because C++ folds while
1049 parsing, and may need changes to be correct for C++ when C++
1050 stops folding while parsing. */
1051 if (c_dialect_cxx ())
1052 gcc_unreachable ();
1054 if (!maybe_const)
1055 maybe_const = &dummy;
1056 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1058 eptype = TREE_TYPE (expr);
1059 expr = TREE_OPERAND (expr, 0);
1061 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1062 &maybe_const_itself);
1063 if (eptype)
1064 ret = fold_convert_loc (loc, eptype, ret);
1065 *maybe_const &= maybe_const_itself;
1066 return ret;
1069 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1070 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1071 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1072 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1073 both evaluated and unevaluated subexpressions while
1074 *MAYBE_CONST_ITSELF is carried from only evaluated
1075 subexpressions). */
1077 static tree
1078 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1079 bool *maybe_const_itself)
1081 tree ret = expr;
1082 enum tree_code code = TREE_CODE (expr);
1083 enum tree_code_class kind = TREE_CODE_CLASS (code);
1084 location_t loc = EXPR_LOCATION (expr);
1085 tree op0, op1, op2, op3;
1086 tree orig_op0, orig_op1, orig_op2;
1087 bool op0_const = true, op1_const = true, op2_const = true;
1088 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1089 bool nowarning = TREE_NO_WARNING (expr);
1090 bool unused_p;
1092 /* This function is not relevant to C++ because C++ folds while
1093 parsing, and may need changes to be correct for C++ when C++
1094 stops folding while parsing. */
1095 if (c_dialect_cxx ())
1096 gcc_unreachable ();
1098 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1099 anything else not counted as an expression cannot usefully be
1100 folded further at this point. */
1101 if (!IS_EXPR_CODE_CLASS (kind)
1102 || kind == tcc_statement
1103 || code == SAVE_EXPR)
1104 return expr;
1106 /* Operands of variable-length expressions (function calls) have
1107 already been folded, as have __builtin_* function calls, and such
1108 expressions cannot occur in constant expressions. */
1109 if (kind == tcc_vl_exp)
1111 *maybe_const_operands = false;
1112 ret = fold (expr);
1113 goto out;
1116 if (code == C_MAYBE_CONST_EXPR)
1118 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1119 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1120 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1121 *maybe_const_operands = false;
1122 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1123 *maybe_const_itself = false;
1124 if (pre && !in_init)
1125 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1126 else
1127 ret = inner;
1128 goto out;
1131 /* Assignment, increment, decrement, function call and comma
1132 operators, and statement expressions, cannot occur in constant
1133 expressions if evaluated / outside of sizeof. (Function calls
1134 were handled above, though VA_ARG_EXPR is treated like a function
1135 call here, and statement expressions are handled through
1136 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1137 switch (code)
1139 case MODIFY_EXPR:
1140 case PREDECREMENT_EXPR:
1141 case PREINCREMENT_EXPR:
1142 case POSTDECREMENT_EXPR:
1143 case POSTINCREMENT_EXPR:
1144 case COMPOUND_EXPR:
1145 *maybe_const_operands = false;
1146 break;
1148 case VA_ARG_EXPR:
1149 case TARGET_EXPR:
1150 case BIND_EXPR:
1151 case OBJ_TYPE_REF:
1152 *maybe_const_operands = false;
1153 ret = fold (expr);
1154 goto out;
1156 default:
1157 break;
1160 /* Fold individual tree codes as appropriate. */
1161 switch (code)
1163 case COMPOUND_LITERAL_EXPR:
1164 /* Any non-constancy will have been marked in a containing
1165 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1166 goto out;
1168 case COMPONENT_REF:
1169 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1170 op1 = TREE_OPERAND (expr, 1);
1171 op2 = TREE_OPERAND (expr, 2);
1172 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1173 maybe_const_itself);
1174 STRIP_TYPE_NOPS (op0);
1175 if (op0 != orig_op0)
1176 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1177 if (ret != expr)
1179 TREE_READONLY (ret) = TREE_READONLY (expr);
1180 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1182 goto out;
1184 case ARRAY_REF:
1185 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1186 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1187 op2 = TREE_OPERAND (expr, 2);
1188 op3 = TREE_OPERAND (expr, 3);
1189 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1190 maybe_const_itself);
1191 STRIP_TYPE_NOPS (op0);
1192 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1193 maybe_const_itself);
1194 STRIP_TYPE_NOPS (op1);
1195 op1 = decl_constant_value_for_optimization (op1);
1196 if (op0 != orig_op0 || op1 != orig_op1)
1197 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1198 if (ret != expr)
1200 TREE_READONLY (ret) = TREE_READONLY (expr);
1201 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1202 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1204 ret = fold (ret);
1205 goto out;
1207 case COMPOUND_EXPR:
1208 case MODIFY_EXPR:
1209 case PREDECREMENT_EXPR:
1210 case PREINCREMENT_EXPR:
1211 case POSTDECREMENT_EXPR:
1212 case POSTINCREMENT_EXPR:
1213 case PLUS_EXPR:
1214 case MINUS_EXPR:
1215 case MULT_EXPR:
1216 case POINTER_PLUS_EXPR:
1217 case TRUNC_DIV_EXPR:
1218 case CEIL_DIV_EXPR:
1219 case FLOOR_DIV_EXPR:
1220 case TRUNC_MOD_EXPR:
1221 case RDIV_EXPR:
1222 case EXACT_DIV_EXPR:
1223 case LSHIFT_EXPR:
1224 case RSHIFT_EXPR:
1225 case BIT_IOR_EXPR:
1226 case BIT_XOR_EXPR:
1227 case BIT_AND_EXPR:
1228 case LT_EXPR:
1229 case LE_EXPR:
1230 case GT_EXPR:
1231 case GE_EXPR:
1232 case EQ_EXPR:
1233 case NE_EXPR:
1234 case COMPLEX_EXPR:
1235 case TRUTH_AND_EXPR:
1236 case TRUTH_OR_EXPR:
1237 case TRUTH_XOR_EXPR:
1238 case UNORDERED_EXPR:
1239 case ORDERED_EXPR:
1240 case UNLT_EXPR:
1241 case UNLE_EXPR:
1242 case UNGT_EXPR:
1243 case UNGE_EXPR:
1244 case UNEQ_EXPR:
1245 /* Binary operations evaluating both arguments (increment and
1246 decrement are binary internally in GCC). */
1247 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1248 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1249 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1250 maybe_const_itself);
1251 STRIP_TYPE_NOPS (op0);
1252 if (code != MODIFY_EXPR
1253 && code != PREDECREMENT_EXPR
1254 && code != PREINCREMENT_EXPR
1255 && code != POSTDECREMENT_EXPR
1256 && code != POSTINCREMENT_EXPR)
1257 op0 = decl_constant_value_for_optimization (op0);
1258 /* The RHS of a MODIFY_EXPR was fully folded when building that
1259 expression for the sake of conversion warnings. */
1260 if (code != MODIFY_EXPR)
1261 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1262 maybe_const_itself);
1263 STRIP_TYPE_NOPS (op1);
1264 op1 = decl_constant_value_for_optimization (op1);
1265 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1266 ret = in_init
1267 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1268 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1269 else
1270 ret = fold (expr);
1271 if (TREE_OVERFLOW_P (ret)
1272 && !TREE_OVERFLOW_P (op0)
1273 && !TREE_OVERFLOW_P (op1))
1274 overflow_warning (EXPR_LOCATION (expr), ret);
1275 goto out;
1277 case INDIRECT_REF:
1278 case FIX_TRUNC_EXPR:
1279 case FLOAT_EXPR:
1280 CASE_CONVERT:
1281 case VIEW_CONVERT_EXPR:
1282 case NON_LVALUE_EXPR:
1283 case NEGATE_EXPR:
1284 case BIT_NOT_EXPR:
1285 case TRUTH_NOT_EXPR:
1286 case ADDR_EXPR:
1287 case CONJ_EXPR:
1288 case REALPART_EXPR:
1289 case IMAGPART_EXPR:
1290 /* Unary operations. */
1291 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1292 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1293 maybe_const_itself);
1294 STRIP_TYPE_NOPS (op0);
1295 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1296 op0 = decl_constant_value_for_optimization (op0);
1297 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1298 not prepared to deal with them if they occur in initializers. */
1299 if (op0 != orig_op0
1300 && code == ADDR_EXPR
1301 && (op1 = get_base_address (op0)) != NULL_TREE
1302 && TREE_CODE (op1) == INDIRECT_REF
1303 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1304 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1305 else if (op0 != orig_op0 || in_init)
1306 ret = in_init
1307 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1308 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1309 else
1310 ret = fold (expr);
1311 if (code == INDIRECT_REF
1312 && ret != expr
1313 && TREE_CODE (ret) == INDIRECT_REF)
1315 TREE_READONLY (ret) = TREE_READONLY (expr);
1316 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1317 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1319 switch (code)
1321 case FIX_TRUNC_EXPR:
1322 case FLOAT_EXPR:
1323 CASE_CONVERT:
1324 /* Don't warn about explicit conversions. We will already
1325 have warned about suspect implicit conversions. */
1326 break;
1328 default:
1329 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1330 overflow_warning (EXPR_LOCATION (expr), ret);
1331 break;
1333 goto out;
1335 case TRUTH_ANDIF_EXPR:
1336 case TRUTH_ORIF_EXPR:
1337 /* Binary operations not necessarily evaluating both
1338 arguments. */
1339 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1340 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1341 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1342 STRIP_TYPE_NOPS (op0);
1344 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1345 ? truthvalue_false_node
1346 : truthvalue_true_node));
1347 c_disable_warnings (unused_p);
1348 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1349 STRIP_TYPE_NOPS (op1);
1350 c_enable_warnings (unused_p);
1352 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1353 ret = in_init
1354 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1355 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1356 else
1357 ret = fold (expr);
1358 *maybe_const_operands &= op0_const;
1359 *maybe_const_itself &= op0_const_self;
1360 if (!(flag_isoc99
1361 && op0_const
1362 && op0_const_self
1363 && (code == TRUTH_ANDIF_EXPR
1364 ? op0 == truthvalue_false_node
1365 : op0 == truthvalue_true_node)))
1366 *maybe_const_operands &= op1_const;
1367 if (!(op0_const
1368 && op0_const_self
1369 && (code == TRUTH_ANDIF_EXPR
1370 ? op0 == truthvalue_false_node
1371 : op0 == truthvalue_true_node)))
1372 *maybe_const_itself &= op1_const_self;
1373 goto out;
1375 case COND_EXPR:
1376 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1377 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1378 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1379 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1381 STRIP_TYPE_NOPS (op0);
1382 c_disable_warnings (op0 == truthvalue_false_node);
1383 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1384 STRIP_TYPE_NOPS (op1);
1385 c_enable_warnings (op0 == truthvalue_false_node);
1387 c_disable_warnings (op0 == truthvalue_true_node);
1388 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1389 STRIP_TYPE_NOPS (op2);
1390 c_enable_warnings (op0 == truthvalue_true_node);
1392 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1393 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1394 else
1395 ret = fold (expr);
1396 *maybe_const_operands &= op0_const;
1397 *maybe_const_itself &= op0_const_self;
1398 if (!(flag_isoc99
1399 && op0_const
1400 && op0_const_self
1401 && op0 == truthvalue_false_node))
1402 *maybe_const_operands &= op1_const;
1403 if (!(op0_const
1404 && op0_const_self
1405 && op0 == truthvalue_false_node))
1406 *maybe_const_itself &= op1_const_self;
1407 if (!(flag_isoc99
1408 && op0_const
1409 && op0_const_self
1410 && op0 == truthvalue_true_node))
1411 *maybe_const_operands &= op2_const;
1412 if (!(op0_const
1413 && op0_const_self
1414 && op0 == truthvalue_true_node))
1415 *maybe_const_itself &= op2_const_self;
1416 goto out;
1418 case EXCESS_PRECISION_EXPR:
1419 /* Each case where an operand with excess precision may be
1420 encountered must remove the EXCESS_PRECISION_EXPR around
1421 inner operands and possibly put one around the whole
1422 expression or possibly convert to the semantic type (which
1423 c_fully_fold does); we cannot tell at this stage which is
1424 appropriate in any particular case. */
1425 gcc_unreachable ();
1427 default:
1428 /* Various codes may appear through folding built-in functions
1429 and their arguments. */
1430 goto out;
1433 out:
1434 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1435 have been done by this point, so remove them again. */
1436 nowarning |= TREE_NO_WARNING (ret);
1437 STRIP_TYPE_NOPS (ret);
1438 if (nowarning && !TREE_NO_WARNING (ret))
1440 if (!CAN_HAVE_LOCATION_P (ret))
1441 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1442 TREE_NO_WARNING (ret) = 1;
1444 if (ret != expr)
1445 protected_set_expr_location (ret, loc);
1446 return ret;
1449 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1450 return EXP. Otherwise, return either EXP or its known constant
1451 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1452 Is the BLKmode test appropriate? */
1454 tree
1455 decl_constant_value_for_optimization (tree exp)
1457 tree ret;
1459 /* This function is only used by C, for c_fully_fold and other
1460 optimization, and may not be correct for C++. */
1461 if (c_dialect_cxx ())
1462 gcc_unreachable ();
1464 if (!optimize
1465 || TREE_CODE (exp) != VAR_DECL
1466 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1467 || DECL_MODE (exp) == BLKmode)
1468 return exp;
1470 ret = decl_constant_value (exp);
1471 /* Avoid unwanted tree sharing between the initializer and current
1472 function's body where the tree can be modified e.g. by the
1473 gimplifier. */
1474 if (ret != exp && TREE_STATIC (exp))
1475 ret = unshare_expr (ret);
1476 return ret;
1479 /* Print a warning if a constant expression had overflow in folding.
1480 Invoke this function on every expression that the language
1481 requires to be a constant expression.
1482 Note the ANSI C standard says it is erroneous for a
1483 constant expression to overflow. */
1485 void
1486 constant_expression_warning (tree value)
1488 if (warn_overflow && pedantic
1489 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1490 || TREE_CODE (value) == FIXED_CST
1491 || TREE_CODE (value) == VECTOR_CST
1492 || TREE_CODE (value) == COMPLEX_CST)
1493 && TREE_OVERFLOW (value))
1494 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1497 /* The same as above but print an unconditional error. */
1498 void
1499 constant_expression_error (tree value)
1501 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1502 || TREE_CODE (value) == FIXED_CST
1503 || TREE_CODE (value) == VECTOR_CST
1504 || TREE_CODE (value) == COMPLEX_CST)
1505 && TREE_OVERFLOW (value))
1506 error ("overflow in constant expression");
1509 /* Print a warning if an expression had overflow in folding and its
1510 operands hadn't.
1512 Invoke this function on every expression that
1513 (1) appears in the source code, and
1514 (2) is a constant expression that overflowed, and
1515 (3) is not already checked by convert_and_check;
1516 however, do not invoke this function on operands of explicit casts
1517 or when the expression is the result of an operator and any operand
1518 already overflowed. */
1520 void
1521 overflow_warning (location_t loc, tree value)
1523 if (c_inhibit_evaluation_warnings != 0)
1524 return;
1526 switch (TREE_CODE (value))
1528 case INTEGER_CST:
1529 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1530 break;
1532 case REAL_CST:
1533 warning_at (loc, OPT_Woverflow,
1534 "floating point overflow in expression");
1535 break;
1537 case FIXED_CST:
1538 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1539 break;
1541 case VECTOR_CST:
1542 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1543 break;
1545 case COMPLEX_CST:
1546 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1547 warning_at (loc, OPT_Woverflow,
1548 "complex integer overflow in expression");
1549 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1550 warning_at (loc, OPT_Woverflow,
1551 "complex floating point overflow in expression");
1552 break;
1554 default:
1555 break;
1559 /* Warn about uses of logical || / && operator in a context where it
1560 is likely that the bitwise equivalent was intended by the
1561 programmer. We have seen an expression in which CODE is a binary
1562 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1563 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1564 void
1565 warn_logical_operator (location_t location, enum tree_code code, tree type,
1566 enum tree_code code_left, tree op_left,
1567 enum tree_code ARG_UNUSED (code_right), tree op_right)
1569 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1570 int in0_p, in1_p, in_p;
1571 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1572 bool strict_overflow_p = false;
1574 if (code != TRUTH_ANDIF_EXPR
1575 && code != TRUTH_AND_EXPR
1576 && code != TRUTH_ORIF_EXPR
1577 && code != TRUTH_OR_EXPR)
1578 return;
1580 /* Warn if &&/|| are being used in a context where it is
1581 likely that the bitwise equivalent was intended by the
1582 programmer. That is, an expression such as op && MASK
1583 where op should not be any boolean expression, nor a
1584 constant, and mask seems to be a non-boolean integer constant. */
1585 if (!truth_value_p (code_left)
1586 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1587 && !CONSTANT_CLASS_P (op_left)
1588 && !TREE_NO_WARNING (op_left)
1589 && TREE_CODE (op_right) == INTEGER_CST
1590 && !integer_zerop (op_right)
1591 && !integer_onep (op_right))
1593 if (or_op)
1594 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1595 " applied to non-boolean constant");
1596 else
1597 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1598 " applied to non-boolean constant");
1599 TREE_NO_WARNING (op_left) = true;
1600 return;
1603 /* We do not warn for constants because they are typical of macro
1604 expansions that test for features. */
1605 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1606 return;
1608 /* This warning only makes sense with logical operands. */
1609 if (!(truth_value_p (TREE_CODE (op_left))
1610 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1611 || !(truth_value_p (TREE_CODE (op_right))
1612 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1613 return;
1616 /* We first test whether either side separately is trivially true
1617 (with OR) or trivially false (with AND). If so, do not warn.
1618 This is a common idiom for testing ranges of data types in
1619 portable code. */
1620 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1621 if (!lhs)
1622 return;
1623 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1624 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1626 /* If this is an OR operation, invert both sides; now, the result
1627 should be always false to get a warning. */
1628 if (or_op)
1629 in0_p = !in0_p;
1631 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1632 if (tem && integer_zerop (tem))
1633 return;
1635 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1636 if (!rhs)
1637 return;
1638 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1639 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1641 /* If this is an OR operation, invert both sides; now, the result
1642 should be always false to get a warning. */
1643 if (or_op)
1644 in1_p = !in1_p;
1646 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1647 if (tem && integer_zerop (tem))
1648 return;
1650 /* If both expressions have the same operand, if we can merge the
1651 ranges, and if the range test is always false, then warn. */
1652 if (operand_equal_p (lhs, rhs, 0)
1653 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1654 in1_p, low1, high1)
1655 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1656 type, lhs, in_p, low, high))
1657 && integer_zerop (tem))
1659 if (or_op)
1660 warning_at (location, OPT_Wlogical_op,
1661 "logical %<or%> "
1662 "of collectively exhaustive tests is always true");
1663 else
1664 warning_at (location, OPT_Wlogical_op,
1665 "logical %<and%> "
1666 "of mutually exclusive tests is always false");
1671 /* Warn if EXP contains any computations whose results are not used.
1672 Return true if a warning is printed; false otherwise. LOCUS is the
1673 (potential) location of the expression. */
1675 bool
1676 warn_if_unused_value (const_tree exp, location_t locus)
1678 restart:
1679 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1680 return false;
1682 /* Don't warn about void constructs. This includes casting to void,
1683 void function calls, and statement expressions with a final cast
1684 to void. */
1685 if (VOID_TYPE_P (TREE_TYPE (exp)))
1686 return false;
1688 if (EXPR_HAS_LOCATION (exp))
1689 locus = EXPR_LOCATION (exp);
1691 switch (TREE_CODE (exp))
1693 case PREINCREMENT_EXPR:
1694 case POSTINCREMENT_EXPR:
1695 case PREDECREMENT_EXPR:
1696 case POSTDECREMENT_EXPR:
1697 case MODIFY_EXPR:
1698 case INIT_EXPR:
1699 case TARGET_EXPR:
1700 case CALL_EXPR:
1701 case TRY_CATCH_EXPR:
1702 case WITH_CLEANUP_EXPR:
1703 case EXIT_EXPR:
1704 case VA_ARG_EXPR:
1705 return false;
1707 case BIND_EXPR:
1708 /* For a binding, warn if no side effect within it. */
1709 exp = BIND_EXPR_BODY (exp);
1710 goto restart;
1712 case SAVE_EXPR:
1713 case NON_LVALUE_EXPR:
1714 case NOP_EXPR:
1715 exp = TREE_OPERAND (exp, 0);
1716 goto restart;
1718 case TRUTH_ORIF_EXPR:
1719 case TRUTH_ANDIF_EXPR:
1720 /* In && or ||, warn if 2nd operand has no side effect. */
1721 exp = TREE_OPERAND (exp, 1);
1722 goto restart;
1724 case COMPOUND_EXPR:
1725 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1726 return true;
1727 /* Let people do `(foo (), 0)' without a warning. */
1728 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1729 return false;
1730 exp = TREE_OPERAND (exp, 1);
1731 goto restart;
1733 case COND_EXPR:
1734 /* If this is an expression with side effects, don't warn; this
1735 case commonly appears in macro expansions. */
1736 if (TREE_SIDE_EFFECTS (exp))
1737 return false;
1738 goto warn;
1740 case INDIRECT_REF:
1741 /* Don't warn about automatic dereferencing of references, since
1742 the user cannot control it. */
1743 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1745 exp = TREE_OPERAND (exp, 0);
1746 goto restart;
1748 /* Fall through. */
1750 default:
1751 /* Referencing a volatile value is a side effect, so don't warn. */
1752 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1753 && TREE_THIS_VOLATILE (exp))
1754 return false;
1756 /* If this is an expression which has no operands, there is no value
1757 to be unused. There are no such language-independent codes,
1758 but front ends may define such. */
1759 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1760 return false;
1762 warn:
1763 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1768 /* Print a warning about casts that might indicate violation
1769 of strict aliasing rules if -Wstrict-aliasing is used and
1770 strict aliasing mode is in effect. OTYPE is the original
1771 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1773 bool
1774 strict_aliasing_warning (tree otype, tree type, tree expr)
1776 /* Strip pointer conversion chains and get to the correct original type. */
1777 STRIP_NOPS (expr);
1778 otype = TREE_TYPE (expr);
1780 if (!(flag_strict_aliasing
1781 && POINTER_TYPE_P (type)
1782 && POINTER_TYPE_P (otype)
1783 && !VOID_TYPE_P (TREE_TYPE (type)))
1784 /* If the type we are casting to is a ref-all pointer
1785 dereferencing it is always valid. */
1786 || TYPE_REF_CAN_ALIAS_ALL (type))
1787 return false;
1789 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1790 && (DECL_P (TREE_OPERAND (expr, 0))
1791 || handled_component_p (TREE_OPERAND (expr, 0))))
1793 /* Casting the address of an object to non void pointer. Warn
1794 if the cast breaks type based aliasing. */
1795 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1797 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1798 "might break strict-aliasing rules");
1799 return true;
1801 else
1803 /* warn_strict_aliasing >= 3. This includes the default (3).
1804 Only warn if the cast is dereferenced immediately. */
1805 alias_set_type set1 =
1806 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1807 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1809 if (set1 != set2 && set2 != 0
1810 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1812 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1813 "pointer will break strict-aliasing rules");
1814 return true;
1816 else if (warn_strict_aliasing == 2
1817 && !alias_sets_must_conflict_p (set1, set2))
1819 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1820 "pointer might break strict-aliasing rules");
1821 return true;
1825 else
1826 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1828 /* At this level, warn for any conversions, even if an address is
1829 not taken in the same statement. This will likely produce many
1830 false positives, but could be useful to pinpoint problems that
1831 are not revealed at higher levels. */
1832 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1833 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1834 if (!COMPLETE_TYPE_P (type)
1835 || !alias_sets_must_conflict_p (set1, set2))
1837 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1838 "pointer might break strict-aliasing rules");
1839 return true;
1843 return false;
1846 /* Warn for unlikely, improbable, or stupid DECL declarations
1847 of `main'. */
1849 void
1850 check_main_parameter_types (tree decl)
1852 function_args_iterator iter;
1853 tree type;
1854 int argct = 0;
1856 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1858 /* XXX void_type_node belies the abstraction. */
1859 if (type == void_type_node || type == error_mark_node )
1860 break;
1862 ++argct;
1863 switch (argct)
1865 case 1:
1866 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1867 pedwarn (input_location, OPT_Wmain,
1868 "first argument of %q+D should be %<int%>", decl);
1869 break;
1871 case 2:
1872 if (TREE_CODE (type) != POINTER_TYPE
1873 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1874 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1875 != char_type_node))
1876 pedwarn (input_location, OPT_Wmain,
1877 "second argument of %q+D should be %<char **%>", decl);
1878 break;
1880 case 3:
1881 if (TREE_CODE (type) != POINTER_TYPE
1882 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1883 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1884 != char_type_node))
1885 pedwarn (input_location, OPT_Wmain,
1886 "third argument of %q+D should probably be "
1887 "%<char **%>", decl);
1888 break;
1892 /* It is intentional that this message does not mention the third
1893 argument because it's only mentioned in an appendix of the
1894 standard. */
1895 if (argct > 0 && (argct < 2 || argct > 3))
1896 pedwarn (input_location, OPT_Wmain,
1897 "%q+D takes only zero or two arguments", decl);
1900 /* True if pointers to distinct types T1 and T2 can be converted to
1901 each other without an explicit cast. Only returns true for opaque
1902 vector types. */
1903 bool
1904 vector_targets_convertible_p (const_tree t1, const_tree t2)
1906 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1907 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1908 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1909 return true;
1911 return false;
1914 /* True if vector types T1 and T2 can be converted to each other
1915 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1916 can only be converted with -flax-vector-conversions yet that is not
1917 in effect, emit a note telling the user about that option if such
1918 a note has not previously been emitted. */
1919 bool
1920 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1922 static bool emitted_lax_note = false;
1923 bool convertible_lax;
1925 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1926 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1927 return true;
1929 convertible_lax =
1930 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1931 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1932 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1933 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1934 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1936 if (!convertible_lax || flag_lax_vector_conversions)
1937 return convertible_lax;
1939 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1940 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1941 return true;
1943 if (emit_lax_note && !emitted_lax_note)
1945 emitted_lax_note = true;
1946 inform (input_location, "use -flax-vector-conversions to permit "
1947 "conversions between vectors with differing "
1948 "element types or numbers of subparts");
1951 return false;
1954 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1955 to integral type. */
1957 static tree
1958 c_common_get_narrower (tree op, int *unsignedp_ptr)
1960 op = get_narrower (op, unsignedp_ptr);
1962 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1963 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1965 /* C++0x scoped enumerations don't implicitly convert to integral
1966 type; if we stripped an explicit conversion to a larger type we
1967 need to replace it so common_type will still work. */
1968 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1969 TYPE_UNSIGNED (TREE_TYPE (op)));
1970 op = fold_convert (type, op);
1972 return op;
1975 /* This is a helper function of build_binary_op.
1977 For certain operations if both args were extended from the same
1978 smaller type, do the arithmetic in that type and then extend.
1980 BITWISE indicates a bitwise operation.
1981 For them, this optimization is safe only if
1982 both args are zero-extended or both are sign-extended.
1983 Otherwise, we might change the result.
1984 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1985 but calculated in (unsigned short) it would be (unsigned short)-1.
1987 tree
1988 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1990 int unsigned0, unsigned1;
1991 tree arg0, arg1;
1992 int uns;
1993 tree type;
1995 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1996 excessive narrowing when we call get_narrower below. For
1997 example, suppose that OP0 is of unsigned int extended
1998 from signed char and that RESULT_TYPE is long long int.
1999 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2000 like
2002 (long long int) (unsigned int) signed_char
2004 which get_narrower would narrow down to
2006 (unsigned int) signed char
2008 If we do not cast OP0 first, get_narrower would return
2009 signed_char, which is inconsistent with the case of the
2010 explicit cast. */
2011 op0 = convert (result_type, op0);
2012 op1 = convert (result_type, op1);
2014 arg0 = c_common_get_narrower (op0, &unsigned0);
2015 arg1 = c_common_get_narrower (op1, &unsigned1);
2017 /* UNS is 1 if the operation to be done is an unsigned one. */
2018 uns = TYPE_UNSIGNED (result_type);
2020 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2021 but it *requires* conversion to FINAL_TYPE. */
2023 if ((TYPE_PRECISION (TREE_TYPE (op0))
2024 == TYPE_PRECISION (TREE_TYPE (arg0)))
2025 && TREE_TYPE (op0) != result_type)
2026 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2027 if ((TYPE_PRECISION (TREE_TYPE (op1))
2028 == TYPE_PRECISION (TREE_TYPE (arg1)))
2029 && TREE_TYPE (op1) != result_type)
2030 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2032 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2034 /* For bitwise operations, signedness of nominal type
2035 does not matter. Consider only how operands were extended. */
2036 if (bitwise)
2037 uns = unsigned0;
2039 /* Note that in all three cases below we refrain from optimizing
2040 an unsigned operation on sign-extended args.
2041 That would not be valid. */
2043 /* Both args variable: if both extended in same way
2044 from same width, do it in that width.
2045 Do it unsigned if args were zero-extended. */
2046 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2047 < TYPE_PRECISION (result_type))
2048 && (TYPE_PRECISION (TREE_TYPE (arg1))
2049 == TYPE_PRECISION (TREE_TYPE (arg0)))
2050 && unsigned0 == unsigned1
2051 && (unsigned0 || !uns))
2052 return c_common_signed_or_unsigned_type
2053 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2055 else if (TREE_CODE (arg0) == INTEGER_CST
2056 && (unsigned1 || !uns)
2057 && (TYPE_PRECISION (TREE_TYPE (arg1))
2058 < TYPE_PRECISION (result_type))
2059 && (type
2060 = c_common_signed_or_unsigned_type (unsigned1,
2061 TREE_TYPE (arg1)))
2062 && !POINTER_TYPE_P (type)
2063 && int_fits_type_p (arg0, type))
2064 return type;
2066 else if (TREE_CODE (arg1) == INTEGER_CST
2067 && (unsigned0 || !uns)
2068 && (TYPE_PRECISION (TREE_TYPE (arg0))
2069 < TYPE_PRECISION (result_type))
2070 && (type
2071 = c_common_signed_or_unsigned_type (unsigned0,
2072 TREE_TYPE (arg0)))
2073 && !POINTER_TYPE_P (type)
2074 && int_fits_type_p (arg1, type))
2075 return type;
2077 return result_type;
2080 /* Checks if expression EXPR of real/integer type cannot be converted
2081 to the real/integer type TYPE. Function returns true when:
2082 * EXPR is a constant which cannot be exactly converted to TYPE
2083 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2084 for EXPR type and TYPE being both integers or both real.
2085 * EXPR is not a constant of real type and TYPE is an integer.
2086 * EXPR is not a constant of integer type which cannot be
2087 exactly converted to real type.
2088 Function allows conversions between types of different signedness and
2089 does not return true in that case. Function can produce signedness
2090 warnings if PRODUCE_WARNS is true. */
2091 bool
2092 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2094 bool give_warning = false;
2095 tree expr_type = TREE_TYPE (expr);
2096 location_t loc = EXPR_LOC_OR_HERE (expr);
2098 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2100 /* Warn for real constant that is not an exact integer converted
2101 to integer type. */
2102 if (TREE_CODE (expr_type) == REAL_TYPE
2103 && TREE_CODE (type) == INTEGER_TYPE)
2105 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2106 give_warning = true;
2108 /* Warn for an integer constant that does not fit into integer type. */
2109 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2110 && TREE_CODE (type) == INTEGER_TYPE
2111 && !int_fits_type_p (expr, type))
2113 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2114 && tree_int_cst_sgn (expr) < 0)
2116 if (produce_warns)
2117 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2118 " implicitly converted to unsigned type");
2120 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2122 if (produce_warns)
2123 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2124 " constant value to negative integer");
2126 else
2127 give_warning = true;
2129 else if (TREE_CODE (type) == REAL_TYPE)
2131 /* Warn for an integer constant that does not fit into real type. */
2132 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2134 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2135 if (!exact_real_truncate (TYPE_MODE (type), &a))
2136 give_warning = true;
2138 /* Warn for a real constant that does not fit into a smaller
2139 real type. */
2140 else if (TREE_CODE (expr_type) == REAL_TYPE
2141 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2143 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2144 if (!exact_real_truncate (TYPE_MODE (type), &a))
2145 give_warning = true;
2149 else
2151 /* Warn for real types converted to integer types. */
2152 if (TREE_CODE (expr_type) == REAL_TYPE
2153 && TREE_CODE (type) == INTEGER_TYPE)
2154 give_warning = true;
2156 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2157 && TREE_CODE (type) == INTEGER_TYPE)
2159 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2160 expr = get_unwidened (expr, 0);
2161 expr_type = TREE_TYPE (expr);
2163 /* Don't warn for short y; short x = ((int)y & 0xff); */
2164 if (TREE_CODE (expr) == BIT_AND_EXPR
2165 || TREE_CODE (expr) == BIT_IOR_EXPR
2166 || TREE_CODE (expr) == BIT_XOR_EXPR)
2168 /* If both args were extended from a shortest type,
2169 use that type if that is safe. */
2170 expr_type = shorten_binary_op (expr_type,
2171 TREE_OPERAND (expr, 0),
2172 TREE_OPERAND (expr, 1),
2173 /* bitwise */1);
2175 if (TREE_CODE (expr) == BIT_AND_EXPR)
2177 tree op0 = TREE_OPERAND (expr, 0);
2178 tree op1 = TREE_OPERAND (expr, 1);
2179 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2180 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2182 /* If one of the operands is a non-negative constant
2183 that fits in the target type, then the type of the
2184 other operand does not matter. */
2185 if ((TREE_CODE (op0) == INTEGER_CST
2186 && int_fits_type_p (op0, c_common_signed_type (type))
2187 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2188 || (TREE_CODE (op1) == INTEGER_CST
2189 && int_fits_type_p (op1, c_common_signed_type (type))
2190 && int_fits_type_p (op1,
2191 c_common_unsigned_type (type))))
2192 return false;
2193 /* If constant is unsigned and fits in the target
2194 type, then the result will also fit. */
2195 else if ((TREE_CODE (op0) == INTEGER_CST
2196 && unsigned0
2197 && int_fits_type_p (op0, type))
2198 || (TREE_CODE (op1) == INTEGER_CST
2199 && unsigned1
2200 && int_fits_type_p (op1, type)))
2201 return false;
2204 /* Warn for integer types converted to smaller integer types. */
2205 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2206 give_warning = true;
2208 /* When they are the same width but different signedness,
2209 then the value may change. */
2210 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2211 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2212 /* Even when converted to a bigger type, if the type is
2213 unsigned but expr is signed, then negative values
2214 will be changed. */
2215 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2216 && produce_warns)
2217 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2218 "may change the sign of the result",
2219 type, expr_type);
2222 /* Warn for integer types converted to real types if and only if
2223 all the range of values of the integer type cannot be
2224 represented by the real type. */
2225 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2226 && TREE_CODE (type) == REAL_TYPE)
2228 tree type_low_bound, type_high_bound;
2229 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2231 /* Don't warn about char y = 0xff; float x = (int) y; */
2232 expr = get_unwidened (expr, 0);
2233 expr_type = TREE_TYPE (expr);
2235 type_low_bound = TYPE_MIN_VALUE (expr_type);
2236 type_high_bound = TYPE_MAX_VALUE (expr_type);
2237 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2238 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2240 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2241 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2242 give_warning = true;
2245 /* Warn for real types converted to smaller real types. */
2246 else if (TREE_CODE (expr_type) == REAL_TYPE
2247 && TREE_CODE (type) == REAL_TYPE
2248 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2249 give_warning = true;
2252 return give_warning;
2255 /* Warns if the conversion of EXPR to TYPE may alter a value.
2256 This is a helper function for warnings_for_convert_and_check. */
2258 static void
2259 conversion_warning (tree type, tree expr)
2261 tree expr_type = TREE_TYPE (expr);
2262 location_t loc = EXPR_LOC_OR_HERE (expr);
2264 if (!warn_conversion && !warn_sign_conversion)
2265 return;
2267 switch (TREE_CODE (expr))
2269 case EQ_EXPR:
2270 case NE_EXPR:
2271 case LE_EXPR:
2272 case GE_EXPR:
2273 case LT_EXPR:
2274 case GT_EXPR:
2275 case TRUTH_ANDIF_EXPR:
2276 case TRUTH_ORIF_EXPR:
2277 case TRUTH_AND_EXPR:
2278 case TRUTH_OR_EXPR:
2279 case TRUTH_XOR_EXPR:
2280 case TRUTH_NOT_EXPR:
2281 /* Conversion from boolean to a signed:1 bit-field (which only
2282 can hold the values 0 and -1) doesn't lose information - but
2283 it does change the value. */
2284 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2285 warning_at (loc, OPT_Wconversion,
2286 "conversion to %qT from boolean expression", type);
2287 return;
2289 case REAL_CST:
2290 case INTEGER_CST:
2291 if (unsafe_conversion_p (type, expr, true))
2292 warning_at (loc, OPT_Wconversion,
2293 "conversion to %qT alters %qT constant value",
2294 type, expr_type);
2295 return;
2297 case COND_EXPR:
2299 /* In case of COND_EXPR, if both operands are constants or
2300 COND_EXPR, then we do not care about the type of COND_EXPR,
2301 only about the conversion of each operand. */
2302 tree op1 = TREE_OPERAND (expr, 1);
2303 tree op2 = TREE_OPERAND (expr, 2);
2305 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2306 || TREE_CODE (op1) == COND_EXPR)
2307 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2308 || TREE_CODE (op2) == COND_EXPR))
2310 conversion_warning (type, op1);
2311 conversion_warning (type, op2);
2312 return;
2314 /* Fall through. */
2317 default: /* 'expr' is not a constant. */
2318 if (unsafe_conversion_p (type, expr, true))
2319 warning_at (loc, OPT_Wconversion,
2320 "conversion to %qT from %qT may alter its value",
2321 type, expr_type);
2325 /* Produce warnings after a conversion. RESULT is the result of
2326 converting EXPR to TYPE. This is a helper function for
2327 convert_and_check and cp_convert_and_check. */
2329 void
2330 warnings_for_convert_and_check (tree type, tree expr, tree result)
2332 location_t loc = EXPR_LOC_OR_HERE (expr);
2334 if (TREE_CODE (expr) == INTEGER_CST
2335 && (TREE_CODE (type) == INTEGER_TYPE
2336 || TREE_CODE (type) == ENUMERAL_TYPE)
2337 && !int_fits_type_p (expr, type))
2339 /* Do not diagnose overflow in a constant expression merely
2340 because a conversion overflowed. */
2341 if (TREE_OVERFLOW (result))
2342 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2344 if (TYPE_UNSIGNED (type))
2346 /* This detects cases like converting -129 or 256 to
2347 unsigned char. */
2348 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2349 warning_at (loc, OPT_Woverflow,
2350 "large integer implicitly truncated to unsigned type");
2351 else
2352 conversion_warning (type, expr);
2354 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2355 warning (OPT_Woverflow,
2356 "overflow in implicit constant conversion");
2357 /* No warning for converting 0x80000000 to int. */
2358 else if (pedantic
2359 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2360 || TYPE_PRECISION (TREE_TYPE (expr))
2361 != TYPE_PRECISION (type)))
2362 warning_at (loc, OPT_Woverflow,
2363 "overflow in implicit constant conversion");
2365 else
2366 conversion_warning (type, expr);
2368 else if ((TREE_CODE (result) == INTEGER_CST
2369 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2370 warning_at (loc, OPT_Woverflow,
2371 "overflow in implicit constant conversion");
2372 else
2373 conversion_warning (type, expr);
2377 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2378 Invoke this function on every expression that is converted implicitly,
2379 i.e. because of language rules and not because of an explicit cast. */
2381 tree
2382 convert_and_check (tree type, tree expr)
2384 tree result;
2385 tree expr_for_warning;
2387 /* Convert from a value with possible excess precision rather than
2388 via the semantic type, but do not warn about values not fitting
2389 exactly in the semantic type. */
2390 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2392 tree orig_type = TREE_TYPE (expr);
2393 expr = TREE_OPERAND (expr, 0);
2394 expr_for_warning = convert (orig_type, expr);
2395 if (orig_type == type)
2396 return expr_for_warning;
2398 else
2399 expr_for_warning = expr;
2401 if (TREE_TYPE (expr) == type)
2402 return expr;
2404 result = convert (type, expr);
2406 if (c_inhibit_evaluation_warnings == 0
2407 && !TREE_OVERFLOW_P (expr)
2408 && result != error_mark_node)
2409 warnings_for_convert_and_check (type, expr_for_warning, result);
2411 return result;
2414 /* A node in a list that describes references to variables (EXPR), which are
2415 either read accesses if WRITER is zero, or write accesses, in which case
2416 WRITER is the parent of EXPR. */
2417 struct tlist
2419 struct tlist *next;
2420 tree expr, writer;
2423 /* Used to implement a cache the results of a call to verify_tree. We only
2424 use this for SAVE_EXPRs. */
2425 struct tlist_cache
2427 struct tlist_cache *next;
2428 struct tlist *cache_before_sp;
2429 struct tlist *cache_after_sp;
2430 tree expr;
2433 /* Obstack to use when allocating tlist structures, and corresponding
2434 firstobj. */
2435 static struct obstack tlist_obstack;
2436 static char *tlist_firstobj = 0;
2438 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2439 warnings. */
2440 static struct tlist *warned_ids;
2441 /* SAVE_EXPRs need special treatment. We process them only once and then
2442 cache the results. */
2443 static struct tlist_cache *save_expr_cache;
2445 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2446 static void merge_tlist (struct tlist **, struct tlist *, int);
2447 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2448 static int warning_candidate_p (tree);
2449 static bool candidate_equal_p (const_tree, const_tree);
2450 static void warn_for_collisions (struct tlist *);
2451 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2452 static struct tlist *new_tlist (struct tlist *, tree, tree);
2454 /* Create a new struct tlist and fill in its fields. */
2455 static struct tlist *
2456 new_tlist (struct tlist *next, tree t, tree writer)
2458 struct tlist *l;
2459 l = XOBNEW (&tlist_obstack, struct tlist);
2460 l->next = next;
2461 l->expr = t;
2462 l->writer = writer;
2463 return l;
2466 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2467 is nonnull, we ignore any node we find which has a writer equal to it. */
2469 static void
2470 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2472 while (add)
2474 struct tlist *next = add->next;
2475 if (!copy)
2476 add->next = *to;
2477 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2478 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2479 add = next;
2483 /* Merge the nodes of ADD into TO. This merging process is done so that for
2484 each variable that already exists in TO, no new node is added; however if
2485 there is a write access recorded in ADD, and an occurrence on TO is only
2486 a read access, then the occurrence in TO will be modified to record the
2487 write. */
2489 static void
2490 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2492 struct tlist **end = to;
2494 while (*end)
2495 end = &(*end)->next;
2497 while (add)
2499 int found = 0;
2500 struct tlist *tmp2;
2501 struct tlist *next = add->next;
2503 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2504 if (candidate_equal_p (tmp2->expr, add->expr))
2506 found = 1;
2507 if (!tmp2->writer)
2508 tmp2->writer = add->writer;
2510 if (!found)
2512 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2513 end = &(*end)->next;
2514 *end = 0;
2516 add = next;
2520 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2521 references in list LIST conflict with it, excluding reads if ONLY writers
2522 is nonzero. */
2524 static void
2525 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2526 int only_writes)
2528 struct tlist *tmp;
2530 /* Avoid duplicate warnings. */
2531 for (tmp = warned_ids; tmp; tmp = tmp->next)
2532 if (candidate_equal_p (tmp->expr, written))
2533 return;
2535 while (list)
2537 if (candidate_equal_p (list->expr, written)
2538 && !candidate_equal_p (list->writer, writer)
2539 && (!only_writes || list->writer))
2541 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2542 warning_at (EXPR_LOC_OR_HERE (writer),
2543 OPT_Wsequence_point, "operation on %qE may be undefined",
2544 list->expr);
2546 list = list->next;
2550 /* Given a list LIST of references to variables, find whether any of these
2551 can cause conflicts due to missing sequence points. */
2553 static void
2554 warn_for_collisions (struct tlist *list)
2556 struct tlist *tmp;
2558 for (tmp = list; tmp; tmp = tmp->next)
2560 if (tmp->writer)
2561 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2565 /* Return nonzero if X is a tree that can be verified by the sequence point
2566 warnings. */
2567 static int
2568 warning_candidate_p (tree x)
2570 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2571 return 0;
2573 if (TREE_CODE (x) == BLOCK)
2574 return 0;
2576 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2577 (lvalue_p) crash on TRY/CATCH. */
2578 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2579 return 0;
2581 if (!lvalue_p (x))
2582 return 0;
2584 /* No point to track non-const calls, they will never satisfy
2585 operand_equal_p. */
2586 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2587 return 0;
2589 if (TREE_CODE (x) == STRING_CST)
2590 return 0;
2592 return 1;
2595 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2596 static bool
2597 candidate_equal_p (const_tree x, const_tree y)
2599 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2602 /* Walk the tree X, and record accesses to variables. If X is written by the
2603 parent tree, WRITER is the parent.
2604 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2605 expression or its only operand forces a sequence point, then everything up
2606 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2607 in PNO_SP.
2608 Once we return, we will have emitted warnings if any subexpression before
2609 such a sequence point could be undefined. On a higher level, however, the
2610 sequence point may not be relevant, and we'll merge the two lists.
2612 Example: (b++, a) + b;
2613 The call that processes the COMPOUND_EXPR will store the increment of B
2614 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2615 processes the PLUS_EXPR will need to merge the two lists so that
2616 eventually, all accesses end up on the same list (and we'll warn about the
2617 unordered subexpressions b++ and b.
2619 A note on merging. If we modify the former example so that our expression
2620 becomes
2621 (b++, b) + a
2622 care must be taken not simply to add all three expressions into the final
2623 PNO_SP list. The function merge_tlist takes care of that by merging the
2624 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2625 way, so that no more than one access to B is recorded. */
2627 static void
2628 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2629 tree writer)
2631 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2632 enum tree_code code;
2633 enum tree_code_class cl;
2635 /* X may be NULL if it is the operand of an empty statement expression
2636 ({ }). */
2637 if (x == NULL)
2638 return;
2640 restart:
2641 code = TREE_CODE (x);
2642 cl = TREE_CODE_CLASS (code);
2644 if (warning_candidate_p (x))
2645 *pno_sp = new_tlist (*pno_sp, x, writer);
2647 switch (code)
2649 case CONSTRUCTOR:
2650 return;
2652 case COMPOUND_EXPR:
2653 case TRUTH_ANDIF_EXPR:
2654 case TRUTH_ORIF_EXPR:
2655 tmp_before = tmp_nosp = tmp_list3 = 0;
2656 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2657 warn_for_collisions (tmp_nosp);
2658 merge_tlist (pbefore_sp, tmp_before, 0);
2659 merge_tlist (pbefore_sp, tmp_nosp, 0);
2660 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2661 merge_tlist (pbefore_sp, tmp_list3, 0);
2662 return;
2664 case COND_EXPR:
2665 tmp_before = tmp_list2 = 0;
2666 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2667 warn_for_collisions (tmp_list2);
2668 merge_tlist (pbefore_sp, tmp_before, 0);
2669 merge_tlist (pbefore_sp, tmp_list2, 1);
2671 tmp_list3 = tmp_nosp = 0;
2672 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2673 warn_for_collisions (tmp_nosp);
2674 merge_tlist (pbefore_sp, tmp_list3, 0);
2676 tmp_list3 = tmp_list2 = 0;
2677 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2678 warn_for_collisions (tmp_list2);
2679 merge_tlist (pbefore_sp, tmp_list3, 0);
2680 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2681 two first, to avoid warning for (a ? b++ : b++). */
2682 merge_tlist (&tmp_nosp, tmp_list2, 0);
2683 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2684 return;
2686 case PREDECREMENT_EXPR:
2687 case PREINCREMENT_EXPR:
2688 case POSTDECREMENT_EXPR:
2689 case POSTINCREMENT_EXPR:
2690 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2691 return;
2693 case MODIFY_EXPR:
2694 tmp_before = tmp_nosp = tmp_list3 = 0;
2695 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2696 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2697 /* Expressions inside the LHS are not ordered wrt. the sequence points
2698 in the RHS. Example:
2699 *a = (a++, 2)
2700 Despite the fact that the modification of "a" is in the before_sp
2701 list (tmp_before), it conflicts with the use of "a" in the LHS.
2702 We can handle this by adding the contents of tmp_list3
2703 to those of tmp_before, and redoing the collision warnings for that
2704 list. */
2705 add_tlist (&tmp_before, tmp_list3, x, 1);
2706 warn_for_collisions (tmp_before);
2707 /* Exclude the LHS itself here; we first have to merge it into the
2708 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2709 didn't exclude the LHS, we'd get it twice, once as a read and once
2710 as a write. */
2711 add_tlist (pno_sp, tmp_list3, x, 0);
2712 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2714 merge_tlist (pbefore_sp, tmp_before, 0);
2715 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2716 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2717 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2718 return;
2720 case CALL_EXPR:
2721 /* We need to warn about conflicts among arguments and conflicts between
2722 args and the function address. Side effects of the function address,
2723 however, are not ordered by the sequence point of the call. */
2725 call_expr_arg_iterator iter;
2726 tree arg;
2727 tmp_before = tmp_nosp = 0;
2728 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2729 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2731 tmp_list2 = tmp_list3 = 0;
2732 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2733 merge_tlist (&tmp_list3, tmp_list2, 0);
2734 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2736 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2737 warn_for_collisions (tmp_before);
2738 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2739 return;
2742 case TREE_LIST:
2743 /* Scan all the list, e.g. indices of multi dimensional array. */
2744 while (x)
2746 tmp_before = tmp_nosp = 0;
2747 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2748 merge_tlist (&tmp_nosp, tmp_before, 0);
2749 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2750 x = TREE_CHAIN (x);
2752 return;
2754 case SAVE_EXPR:
2756 struct tlist_cache *t;
2757 for (t = save_expr_cache; t; t = t->next)
2758 if (candidate_equal_p (t->expr, x))
2759 break;
2761 if (!t)
2763 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2764 t->next = save_expr_cache;
2765 t->expr = x;
2766 save_expr_cache = t;
2768 tmp_before = tmp_nosp = 0;
2769 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2770 warn_for_collisions (tmp_nosp);
2772 tmp_list3 = 0;
2773 while (tmp_nosp)
2775 struct tlist *t = tmp_nosp;
2776 tmp_nosp = t->next;
2777 merge_tlist (&tmp_list3, t, 0);
2779 t->cache_before_sp = tmp_before;
2780 t->cache_after_sp = tmp_list3;
2782 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2783 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2784 return;
2787 case ADDR_EXPR:
2788 x = TREE_OPERAND (x, 0);
2789 if (DECL_P (x))
2790 return;
2791 writer = 0;
2792 goto restart;
2794 default:
2795 /* For other expressions, simply recurse on their operands.
2796 Manual tail recursion for unary expressions.
2797 Other non-expressions need not be processed. */
2798 if (cl == tcc_unary)
2800 x = TREE_OPERAND (x, 0);
2801 writer = 0;
2802 goto restart;
2804 else if (IS_EXPR_CODE_CLASS (cl))
2806 int lp;
2807 int max = TREE_OPERAND_LENGTH (x);
2808 for (lp = 0; lp < max; lp++)
2810 tmp_before = tmp_nosp = 0;
2811 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2812 merge_tlist (&tmp_nosp, tmp_before, 0);
2813 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2816 return;
2820 /* Try to warn for undefined behavior in EXPR due to missing sequence
2821 points. */
2823 DEBUG_FUNCTION void
2824 verify_sequence_points (tree expr)
2826 struct tlist *before_sp = 0, *after_sp = 0;
2828 warned_ids = 0;
2829 save_expr_cache = 0;
2830 if (tlist_firstobj == 0)
2832 gcc_obstack_init (&tlist_obstack);
2833 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2836 verify_tree (expr, &before_sp, &after_sp, 0);
2837 warn_for_collisions (after_sp);
2838 obstack_free (&tlist_obstack, tlist_firstobj);
2841 /* Validate the expression after `case' and apply default promotions. */
2843 static tree
2844 check_case_value (tree value)
2846 if (value == NULL_TREE)
2847 return value;
2849 if (TREE_CODE (value) == INTEGER_CST)
2850 /* Promote char or short to int. */
2851 value = perform_integral_promotions (value);
2852 else if (value != error_mark_node)
2854 error ("case label does not reduce to an integer constant");
2855 value = error_mark_node;
2858 constant_expression_warning (value);
2860 return value;
2863 /* See if the case values LOW and HIGH are in the range of the original
2864 type (i.e. before the default conversion to int) of the switch testing
2865 expression.
2866 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2867 the type before promoting it. CASE_LOW_P is a pointer to the lower
2868 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2869 if the case is not a case range.
2870 The caller has to make sure that we are not called with NULL for
2871 CASE_LOW_P (i.e. the default case).
2872 Returns true if the case label is in range of ORIG_TYPE (saturated or
2873 untouched) or false if the label is out of range. */
2875 static bool
2876 check_case_bounds (tree type, tree orig_type,
2877 tree *case_low_p, tree *case_high_p)
2879 tree min_value, max_value;
2880 tree case_low = *case_low_p;
2881 tree case_high = case_high_p ? *case_high_p : case_low;
2883 /* If there was a problem with the original type, do nothing. */
2884 if (orig_type == error_mark_node)
2885 return true;
2887 min_value = TYPE_MIN_VALUE (orig_type);
2888 max_value = TYPE_MAX_VALUE (orig_type);
2890 /* Case label is less than minimum for type. */
2891 if (tree_int_cst_compare (case_low, min_value) < 0
2892 && tree_int_cst_compare (case_high, min_value) < 0)
2894 warning (0, "case label value is less than minimum value for type");
2895 return false;
2898 /* Case value is greater than maximum for type. */
2899 if (tree_int_cst_compare (case_low, max_value) > 0
2900 && tree_int_cst_compare (case_high, max_value) > 0)
2902 warning (0, "case label value exceeds maximum value for type");
2903 return false;
2906 /* Saturate lower case label value to minimum. */
2907 if (tree_int_cst_compare (case_high, min_value) >= 0
2908 && tree_int_cst_compare (case_low, min_value) < 0)
2910 warning (0, "lower value in case label range"
2911 " less than minimum value for type");
2912 case_low = min_value;
2915 /* Saturate upper case label value to maximum. */
2916 if (tree_int_cst_compare (case_low, max_value) <= 0
2917 && tree_int_cst_compare (case_high, max_value) > 0)
2919 warning (0, "upper value in case label range"
2920 " exceeds maximum value for type");
2921 case_high = max_value;
2924 if (*case_low_p != case_low)
2925 *case_low_p = convert (type, case_low);
2926 if (case_high_p && *case_high_p != case_high)
2927 *case_high_p = convert (type, case_high);
2929 return true;
2932 /* Return an integer type with BITS bits of precision,
2933 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2935 tree
2936 c_common_type_for_size (unsigned int bits, int unsignedp)
2938 if (bits == TYPE_PRECISION (integer_type_node))
2939 return unsignedp ? unsigned_type_node : integer_type_node;
2941 if (bits == TYPE_PRECISION (signed_char_type_node))
2942 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2944 if (bits == TYPE_PRECISION (short_integer_type_node))
2945 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2947 if (bits == TYPE_PRECISION (long_integer_type_node))
2948 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2950 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2951 return (unsignedp ? long_long_unsigned_type_node
2952 : long_long_integer_type_node);
2954 if (int128_integer_type_node
2955 && bits == TYPE_PRECISION (int128_integer_type_node))
2956 return (unsignedp ? int128_unsigned_type_node
2957 : int128_integer_type_node);
2959 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2960 return (unsignedp ? widest_unsigned_literal_type_node
2961 : widest_integer_literal_type_node);
2963 if (bits <= TYPE_PRECISION (intQI_type_node))
2964 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2966 if (bits <= TYPE_PRECISION (intHI_type_node))
2967 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2969 if (bits <= TYPE_PRECISION (intSI_type_node))
2970 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2972 if (bits <= TYPE_PRECISION (intDI_type_node))
2973 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2975 return 0;
2978 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2979 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2980 and saturating if SATP is nonzero, otherwise not saturating. */
2982 tree
2983 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2984 int unsignedp, int satp)
2986 enum machine_mode mode;
2987 if (ibit == 0)
2988 mode = unsignedp ? UQQmode : QQmode;
2989 else
2990 mode = unsignedp ? UHAmode : HAmode;
2992 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2993 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2994 break;
2996 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2998 sorry ("GCC cannot support operators with integer types and "
2999 "fixed-point types that have too many integral and "
3000 "fractional bits together");
3001 return 0;
3004 return c_common_type_for_mode (mode, satp);
3007 /* Used for communication between c_common_type_for_mode and
3008 c_register_builtin_type. */
3009 static GTY(()) tree registered_builtin_types;
3011 /* Return a data type that has machine mode MODE.
3012 If the mode is an integer,
3013 then UNSIGNEDP selects between signed and unsigned types.
3014 If the mode is a fixed-point mode,
3015 then UNSIGNEDP selects between saturating and nonsaturating types. */
3017 tree
3018 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3020 tree t;
3022 if (mode == TYPE_MODE (integer_type_node))
3023 return unsignedp ? unsigned_type_node : integer_type_node;
3025 if (mode == TYPE_MODE (signed_char_type_node))
3026 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3028 if (mode == TYPE_MODE (short_integer_type_node))
3029 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3031 if (mode == TYPE_MODE (long_integer_type_node))
3032 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3034 if (mode == TYPE_MODE (long_long_integer_type_node))
3035 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3037 if (int128_integer_type_node
3038 && mode == TYPE_MODE (int128_integer_type_node))
3039 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3041 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3042 return unsignedp ? widest_unsigned_literal_type_node
3043 : widest_integer_literal_type_node;
3045 if (mode == QImode)
3046 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3048 if (mode == HImode)
3049 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3051 if (mode == SImode)
3052 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3054 if (mode == DImode)
3055 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3057 #if HOST_BITS_PER_WIDE_INT >= 64
3058 if (mode == TYPE_MODE (intTI_type_node))
3059 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3060 #endif
3062 if (mode == TYPE_MODE (float_type_node))
3063 return float_type_node;
3065 if (mode == TYPE_MODE (double_type_node))
3066 return double_type_node;
3068 if (mode == TYPE_MODE (long_double_type_node))
3069 return long_double_type_node;
3071 if (mode == TYPE_MODE (void_type_node))
3072 return void_type_node;
3074 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3075 return (unsignedp
3076 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3077 : make_signed_type (GET_MODE_PRECISION (mode)));
3079 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3080 return (unsignedp
3081 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3082 : make_signed_type (GET_MODE_PRECISION (mode)));
3084 if (COMPLEX_MODE_P (mode))
3086 enum machine_mode inner_mode;
3087 tree inner_type;
3089 if (mode == TYPE_MODE (complex_float_type_node))
3090 return complex_float_type_node;
3091 if (mode == TYPE_MODE (complex_double_type_node))
3092 return complex_double_type_node;
3093 if (mode == TYPE_MODE (complex_long_double_type_node))
3094 return complex_long_double_type_node;
3096 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3097 return complex_integer_type_node;
3099 inner_mode = GET_MODE_INNER (mode);
3100 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3101 if (inner_type != NULL_TREE)
3102 return build_complex_type (inner_type);
3104 else if (VECTOR_MODE_P (mode))
3106 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3107 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3108 if (inner_type != NULL_TREE)
3109 return build_vector_type_for_mode (inner_type, mode);
3112 if (mode == TYPE_MODE (dfloat32_type_node))
3113 return dfloat32_type_node;
3114 if (mode == TYPE_MODE (dfloat64_type_node))
3115 return dfloat64_type_node;
3116 if (mode == TYPE_MODE (dfloat128_type_node))
3117 return dfloat128_type_node;
3119 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3121 if (mode == TYPE_MODE (short_fract_type_node))
3122 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3123 if (mode == TYPE_MODE (fract_type_node))
3124 return unsignedp ? sat_fract_type_node : fract_type_node;
3125 if (mode == TYPE_MODE (long_fract_type_node))
3126 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3127 if (mode == TYPE_MODE (long_long_fract_type_node))
3128 return unsignedp ? sat_long_long_fract_type_node
3129 : long_long_fract_type_node;
3131 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3132 return unsignedp ? sat_unsigned_short_fract_type_node
3133 : unsigned_short_fract_type_node;
3134 if (mode == TYPE_MODE (unsigned_fract_type_node))
3135 return unsignedp ? sat_unsigned_fract_type_node
3136 : unsigned_fract_type_node;
3137 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3138 return unsignedp ? sat_unsigned_long_fract_type_node
3139 : unsigned_long_fract_type_node;
3140 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3141 return unsignedp ? sat_unsigned_long_long_fract_type_node
3142 : unsigned_long_long_fract_type_node;
3144 if (mode == TYPE_MODE (short_accum_type_node))
3145 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3146 if (mode == TYPE_MODE (accum_type_node))
3147 return unsignedp ? sat_accum_type_node : accum_type_node;
3148 if (mode == TYPE_MODE (long_accum_type_node))
3149 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3150 if (mode == TYPE_MODE (long_long_accum_type_node))
3151 return unsignedp ? sat_long_long_accum_type_node
3152 : long_long_accum_type_node;
3154 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3155 return unsignedp ? sat_unsigned_short_accum_type_node
3156 : unsigned_short_accum_type_node;
3157 if (mode == TYPE_MODE (unsigned_accum_type_node))
3158 return unsignedp ? sat_unsigned_accum_type_node
3159 : unsigned_accum_type_node;
3160 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3161 return unsignedp ? sat_unsigned_long_accum_type_node
3162 : unsigned_long_accum_type_node;
3163 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3164 return unsignedp ? sat_unsigned_long_long_accum_type_node
3165 : unsigned_long_long_accum_type_node;
3167 if (mode == QQmode)
3168 return unsignedp ? sat_qq_type_node : qq_type_node;
3169 if (mode == HQmode)
3170 return unsignedp ? sat_hq_type_node : hq_type_node;
3171 if (mode == SQmode)
3172 return unsignedp ? sat_sq_type_node : sq_type_node;
3173 if (mode == DQmode)
3174 return unsignedp ? sat_dq_type_node : dq_type_node;
3175 if (mode == TQmode)
3176 return unsignedp ? sat_tq_type_node : tq_type_node;
3178 if (mode == UQQmode)
3179 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3180 if (mode == UHQmode)
3181 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3182 if (mode == USQmode)
3183 return unsignedp ? sat_usq_type_node : usq_type_node;
3184 if (mode == UDQmode)
3185 return unsignedp ? sat_udq_type_node : udq_type_node;
3186 if (mode == UTQmode)
3187 return unsignedp ? sat_utq_type_node : utq_type_node;
3189 if (mode == HAmode)
3190 return unsignedp ? sat_ha_type_node : ha_type_node;
3191 if (mode == SAmode)
3192 return unsignedp ? sat_sa_type_node : sa_type_node;
3193 if (mode == DAmode)
3194 return unsignedp ? sat_da_type_node : da_type_node;
3195 if (mode == TAmode)
3196 return unsignedp ? sat_ta_type_node : ta_type_node;
3198 if (mode == UHAmode)
3199 return unsignedp ? sat_uha_type_node : uha_type_node;
3200 if (mode == USAmode)
3201 return unsignedp ? sat_usa_type_node : usa_type_node;
3202 if (mode == UDAmode)
3203 return unsignedp ? sat_uda_type_node : uda_type_node;
3204 if (mode == UTAmode)
3205 return unsignedp ? sat_uta_type_node : uta_type_node;
3208 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3209 if (TYPE_MODE (TREE_VALUE (t)) == mode
3210 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3211 return TREE_VALUE (t);
3213 return 0;
3216 tree
3217 c_common_unsigned_type (tree type)
3219 return c_common_signed_or_unsigned_type (1, type);
3222 /* Return a signed type the same as TYPE in other respects. */
3224 tree
3225 c_common_signed_type (tree type)
3227 return c_common_signed_or_unsigned_type (0, type);
3230 /* Return a type the same as TYPE except unsigned or
3231 signed according to UNSIGNEDP. */
3233 tree
3234 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3236 tree type1;
3238 /* This block of code emulates the behavior of the old
3239 c_common_unsigned_type. In particular, it returns
3240 long_unsigned_type_node if passed a long, even when a int would
3241 have the same size. This is necessary for warnings to work
3242 correctly in archs where sizeof(int) == sizeof(long) */
3244 type1 = TYPE_MAIN_VARIANT (type);
3245 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3246 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3247 if (type1 == integer_type_node || type1 == unsigned_type_node)
3248 return unsignedp ? unsigned_type_node : integer_type_node;
3249 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3250 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3251 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3252 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3253 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3254 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3255 if (int128_integer_type_node
3256 && (type1 == int128_integer_type_node
3257 || type1 == int128_unsigned_type_node))
3258 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3259 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3260 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3261 #if HOST_BITS_PER_WIDE_INT >= 64
3262 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3263 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3264 #endif
3265 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3266 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3267 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3268 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3269 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3270 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3271 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3272 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3274 #define C_COMMON_FIXED_TYPES(NAME) \
3275 if (type1 == short_ ## NAME ## _type_node \
3276 || type1 == unsigned_short_ ## NAME ## _type_node) \
3277 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3278 : short_ ## NAME ## _type_node; \
3279 if (type1 == NAME ## _type_node \
3280 || type1 == unsigned_ ## NAME ## _type_node) \
3281 return unsignedp ? unsigned_ ## NAME ## _type_node \
3282 : NAME ## _type_node; \
3283 if (type1 == long_ ## NAME ## _type_node \
3284 || type1 == unsigned_long_ ## NAME ## _type_node) \
3285 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3286 : long_ ## NAME ## _type_node; \
3287 if (type1 == long_long_ ## NAME ## _type_node \
3288 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3289 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3290 : long_long_ ## NAME ## _type_node;
3292 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3293 if (type1 == NAME ## _type_node \
3294 || type1 == u ## NAME ## _type_node) \
3295 return unsignedp ? u ## NAME ## _type_node \
3296 : NAME ## _type_node;
3298 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3299 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3300 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3301 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3302 : sat_ ## short_ ## NAME ## _type_node; \
3303 if (type1 == sat_ ## NAME ## _type_node \
3304 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3305 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3306 : sat_ ## NAME ## _type_node; \
3307 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3308 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3309 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3310 : sat_ ## long_ ## NAME ## _type_node; \
3311 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3312 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3313 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3314 : sat_ ## long_long_ ## NAME ## _type_node;
3316 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3317 if (type1 == sat_ ## NAME ## _type_node \
3318 || type1 == sat_ ## u ## NAME ## _type_node) \
3319 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3320 : sat_ ## NAME ## _type_node;
3322 C_COMMON_FIXED_TYPES (fract);
3323 C_COMMON_FIXED_TYPES_SAT (fract);
3324 C_COMMON_FIXED_TYPES (accum);
3325 C_COMMON_FIXED_TYPES_SAT (accum);
3327 C_COMMON_FIXED_MODE_TYPES (qq);
3328 C_COMMON_FIXED_MODE_TYPES (hq);
3329 C_COMMON_FIXED_MODE_TYPES (sq);
3330 C_COMMON_FIXED_MODE_TYPES (dq);
3331 C_COMMON_FIXED_MODE_TYPES (tq);
3332 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3333 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3334 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3335 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3336 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3337 C_COMMON_FIXED_MODE_TYPES (ha);
3338 C_COMMON_FIXED_MODE_TYPES (sa);
3339 C_COMMON_FIXED_MODE_TYPES (da);
3340 C_COMMON_FIXED_MODE_TYPES (ta);
3341 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3342 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3343 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3344 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3346 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3347 the precision; they have precision set to match their range, but
3348 may use a wider mode to match an ABI. If we change modes, we may
3349 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3350 the precision as well, so as to yield correct results for
3351 bit-field types. C++ does not have these separate bit-field
3352 types, and producing a signed or unsigned variant of an
3353 ENUMERAL_TYPE may cause other problems as well. */
3355 if (!INTEGRAL_TYPE_P (type)
3356 || TYPE_UNSIGNED (type) == unsignedp)
3357 return type;
3359 #define TYPE_OK(node) \
3360 (TYPE_MODE (type) == TYPE_MODE (node) \
3361 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3362 if (TYPE_OK (signed_char_type_node))
3363 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3364 if (TYPE_OK (integer_type_node))
3365 return unsignedp ? unsigned_type_node : integer_type_node;
3366 if (TYPE_OK (short_integer_type_node))
3367 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3368 if (TYPE_OK (long_integer_type_node))
3369 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3370 if (TYPE_OK (long_long_integer_type_node))
3371 return (unsignedp ? long_long_unsigned_type_node
3372 : long_long_integer_type_node);
3373 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3374 return (unsignedp ? int128_unsigned_type_node
3375 : int128_integer_type_node);
3376 if (TYPE_OK (widest_integer_literal_type_node))
3377 return (unsignedp ? widest_unsigned_literal_type_node
3378 : widest_integer_literal_type_node);
3380 #if HOST_BITS_PER_WIDE_INT >= 64
3381 if (TYPE_OK (intTI_type_node))
3382 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3383 #endif
3384 if (TYPE_OK (intDI_type_node))
3385 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3386 if (TYPE_OK (intSI_type_node))
3387 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3388 if (TYPE_OK (intHI_type_node))
3389 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3390 if (TYPE_OK (intQI_type_node))
3391 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3392 #undef TYPE_OK
3394 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3397 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3399 tree
3400 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3402 /* Extended integer types of the same width as a standard type have
3403 lesser rank, so those of the same width as int promote to int or
3404 unsigned int and are valid for printf formats expecting int or
3405 unsigned int. To avoid such special cases, avoid creating
3406 extended integer types for bit-fields if a standard integer type
3407 is available. */
3408 if (width == TYPE_PRECISION (integer_type_node))
3409 return unsignedp ? unsigned_type_node : integer_type_node;
3410 if (width == TYPE_PRECISION (signed_char_type_node))
3411 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3412 if (width == TYPE_PRECISION (short_integer_type_node))
3413 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3414 if (width == TYPE_PRECISION (long_integer_type_node))
3415 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3416 if (width == TYPE_PRECISION (long_long_integer_type_node))
3417 return (unsignedp ? long_long_unsigned_type_node
3418 : long_long_integer_type_node);
3419 if (int128_integer_type_node
3420 && width == TYPE_PRECISION (int128_integer_type_node))
3421 return (unsignedp ? int128_unsigned_type_node
3422 : int128_integer_type_node);
3423 return build_nonstandard_integer_type (width, unsignedp);
3426 /* The C version of the register_builtin_type langhook. */
3428 void
3429 c_register_builtin_type (tree type, const char* name)
3431 tree decl;
3433 decl = build_decl (UNKNOWN_LOCATION,
3434 TYPE_DECL, get_identifier (name), type);
3435 DECL_ARTIFICIAL (decl) = 1;
3436 if (!TYPE_NAME (type))
3437 TYPE_NAME (type) = decl;
3438 pushdecl (decl);
3440 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3443 /* Print an error message for invalid operands to arith operation
3444 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3445 LOCATION is the location of the message. */
3447 void
3448 binary_op_error (location_t location, enum tree_code code,
3449 tree type0, tree type1)
3451 const char *opname;
3453 switch (code)
3455 case PLUS_EXPR:
3456 opname = "+"; break;
3457 case MINUS_EXPR:
3458 opname = "-"; break;
3459 case MULT_EXPR:
3460 opname = "*"; break;
3461 case MAX_EXPR:
3462 opname = "max"; break;
3463 case MIN_EXPR:
3464 opname = "min"; break;
3465 case EQ_EXPR:
3466 opname = "=="; break;
3467 case NE_EXPR:
3468 opname = "!="; break;
3469 case LE_EXPR:
3470 opname = "<="; break;
3471 case GE_EXPR:
3472 opname = ">="; break;
3473 case LT_EXPR:
3474 opname = "<"; break;
3475 case GT_EXPR:
3476 opname = ">"; break;
3477 case LSHIFT_EXPR:
3478 opname = "<<"; break;
3479 case RSHIFT_EXPR:
3480 opname = ">>"; break;
3481 case TRUNC_MOD_EXPR:
3482 case FLOOR_MOD_EXPR:
3483 opname = "%"; break;
3484 case TRUNC_DIV_EXPR:
3485 case FLOOR_DIV_EXPR:
3486 opname = "/"; break;
3487 case BIT_AND_EXPR:
3488 opname = "&"; break;
3489 case BIT_IOR_EXPR:
3490 opname = "|"; break;
3491 case TRUTH_ANDIF_EXPR:
3492 opname = "&&"; break;
3493 case TRUTH_ORIF_EXPR:
3494 opname = "||"; break;
3495 case BIT_XOR_EXPR:
3496 opname = "^"; break;
3497 default:
3498 gcc_unreachable ();
3500 error_at (location,
3501 "invalid operands to binary %s (have %qT and %qT)", opname,
3502 type0, type1);
3505 /* Given an expression as a tree, return its original type. Do this
3506 by stripping any conversion that preserves the sign and precision. */
3507 static tree
3508 expr_original_type (tree expr)
3510 STRIP_SIGN_NOPS (expr);
3511 return TREE_TYPE (expr);
3514 /* Subroutine of build_binary_op, used for comparison operations.
3515 See if the operands have both been converted from subword integer types
3516 and, if so, perhaps change them both back to their original type.
3517 This function is also responsible for converting the two operands
3518 to the proper common type for comparison.
3520 The arguments of this function are all pointers to local variables
3521 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3522 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3524 If this function returns nonzero, it means that the comparison has
3525 a constant value. What this function returns is an expression for
3526 that value. */
3528 tree
3529 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3530 enum tree_code *rescode_ptr)
3532 tree type;
3533 tree op0 = *op0_ptr;
3534 tree op1 = *op1_ptr;
3535 int unsignedp0, unsignedp1;
3536 int real1, real2;
3537 tree primop0, primop1;
3538 enum tree_code code = *rescode_ptr;
3539 location_t loc = EXPR_LOC_OR_HERE (op0);
3541 /* Throw away any conversions to wider types
3542 already present in the operands. */
3544 primop0 = c_common_get_narrower (op0, &unsignedp0);
3545 primop1 = c_common_get_narrower (op1, &unsignedp1);
3547 /* If primopN is first sign-extended from primopN's precision to opN's
3548 precision, then zero-extended from opN's precision to
3549 *restype_ptr precision, shortenings might be invalid. */
3550 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3551 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3552 && !unsignedp0
3553 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3554 primop0 = op0;
3555 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3556 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3557 && !unsignedp1
3558 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3559 primop1 = op1;
3561 /* Handle the case that OP0 does not *contain* a conversion
3562 but it *requires* conversion to FINAL_TYPE. */
3564 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3565 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3566 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3567 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3569 /* If one of the operands must be floated, we cannot optimize. */
3570 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3571 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3573 /* If first arg is constant, swap the args (changing operation
3574 so value is preserved), for canonicalization. Don't do this if
3575 the second arg is 0. */
3577 if (TREE_CONSTANT (primop0)
3578 && !integer_zerop (primop1) && !real_zerop (primop1)
3579 && !fixed_zerop (primop1))
3581 tree tem = primop0;
3582 int temi = unsignedp0;
3583 primop0 = primop1;
3584 primop1 = tem;
3585 tem = op0;
3586 op0 = op1;
3587 op1 = tem;
3588 *op0_ptr = op0;
3589 *op1_ptr = op1;
3590 unsignedp0 = unsignedp1;
3591 unsignedp1 = temi;
3592 temi = real1;
3593 real1 = real2;
3594 real2 = temi;
3596 switch (code)
3598 case LT_EXPR:
3599 code = GT_EXPR;
3600 break;
3601 case GT_EXPR:
3602 code = LT_EXPR;
3603 break;
3604 case LE_EXPR:
3605 code = GE_EXPR;
3606 break;
3607 case GE_EXPR:
3608 code = LE_EXPR;
3609 break;
3610 default:
3611 break;
3613 *rescode_ptr = code;
3616 /* If comparing an integer against a constant more bits wide,
3617 maybe we can deduce a value of 1 or 0 independent of the data.
3618 Or else truncate the constant now
3619 rather than extend the variable at run time.
3621 This is only interesting if the constant is the wider arg.
3622 Also, it is not safe if the constant is unsigned and the
3623 variable arg is signed, since in this case the variable
3624 would be sign-extended and then regarded as unsigned.
3625 Our technique fails in this case because the lowest/highest
3626 possible unsigned results don't follow naturally from the
3627 lowest/highest possible values of the variable operand.
3628 For just EQ_EXPR and NE_EXPR there is another technique that
3629 could be used: see if the constant can be faithfully represented
3630 in the other operand's type, by truncating it and reextending it
3631 and see if that preserves the constant's value. */
3633 if (!real1 && !real2
3634 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3635 && TREE_CODE (primop1) == INTEGER_CST
3636 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3638 int min_gt, max_gt, min_lt, max_lt;
3639 tree maxval, minval;
3640 /* 1 if comparison is nominally unsigned. */
3641 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3642 tree val;
3644 type = c_common_signed_or_unsigned_type (unsignedp0,
3645 TREE_TYPE (primop0));
3647 maxval = TYPE_MAX_VALUE (type);
3648 minval = TYPE_MIN_VALUE (type);
3650 if (unsignedp && !unsignedp0)
3651 *restype_ptr = c_common_signed_type (*restype_ptr);
3653 if (TREE_TYPE (primop1) != *restype_ptr)
3655 /* Convert primop1 to target type, but do not introduce
3656 additional overflow. We know primop1 is an int_cst. */
3657 primop1 = force_fit_type_double (*restype_ptr,
3658 tree_to_double_int (primop1),
3659 0, TREE_OVERFLOW (primop1));
3661 if (type != *restype_ptr)
3663 minval = convert (*restype_ptr, minval);
3664 maxval = convert (*restype_ptr, maxval);
3667 if (unsignedp && unsignedp0)
3669 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3670 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3671 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3672 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3674 else
3676 min_gt = INT_CST_LT (primop1, minval);
3677 max_gt = INT_CST_LT (primop1, maxval);
3678 min_lt = INT_CST_LT (minval, primop1);
3679 max_lt = INT_CST_LT (maxval, primop1);
3682 val = 0;
3683 /* This used to be a switch, but Genix compiler can't handle that. */
3684 if (code == NE_EXPR)
3686 if (max_lt || min_gt)
3687 val = truthvalue_true_node;
3689 else if (code == EQ_EXPR)
3691 if (max_lt || min_gt)
3692 val = truthvalue_false_node;
3694 else if (code == LT_EXPR)
3696 if (max_lt)
3697 val = truthvalue_true_node;
3698 if (!min_lt)
3699 val = truthvalue_false_node;
3701 else if (code == GT_EXPR)
3703 if (min_gt)
3704 val = truthvalue_true_node;
3705 if (!max_gt)
3706 val = truthvalue_false_node;
3708 else if (code == LE_EXPR)
3710 if (!max_gt)
3711 val = truthvalue_true_node;
3712 if (min_gt)
3713 val = truthvalue_false_node;
3715 else if (code == GE_EXPR)
3717 if (!min_lt)
3718 val = truthvalue_true_node;
3719 if (max_lt)
3720 val = truthvalue_false_node;
3723 /* If primop0 was sign-extended and unsigned comparison specd,
3724 we did a signed comparison above using the signed type bounds.
3725 But the comparison we output must be unsigned.
3727 Also, for inequalities, VAL is no good; but if the signed
3728 comparison had *any* fixed result, it follows that the
3729 unsigned comparison just tests the sign in reverse
3730 (positive values are LE, negative ones GE).
3731 So we can generate an unsigned comparison
3732 against an extreme value of the signed type. */
3734 if (unsignedp && !unsignedp0)
3736 if (val != 0)
3737 switch (code)
3739 case LT_EXPR:
3740 case GE_EXPR:
3741 primop1 = TYPE_MIN_VALUE (type);
3742 val = 0;
3743 break;
3745 case LE_EXPR:
3746 case GT_EXPR:
3747 primop1 = TYPE_MAX_VALUE (type);
3748 val = 0;
3749 break;
3751 default:
3752 break;
3754 type = c_common_unsigned_type (type);
3757 if (TREE_CODE (primop0) != INTEGER_CST
3758 && c_inhibit_evaluation_warnings == 0)
3760 if (val == truthvalue_false_node)
3761 warning_at (loc, OPT_Wtype_limits,
3762 "comparison is always false due to limited range of data type");
3763 if (val == truthvalue_true_node)
3764 warning_at (loc, OPT_Wtype_limits,
3765 "comparison is always true due to limited range of data type");
3768 if (val != 0)
3770 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3771 if (TREE_SIDE_EFFECTS (primop0))
3772 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3773 return val;
3776 /* Value is not predetermined, but do the comparison
3777 in the type of the operand that is not constant.
3778 TYPE is already properly set. */
3781 /* If either arg is decimal float and the other is float, find the
3782 proper common type to use for comparison. */
3783 else if (real1 && real2
3784 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3785 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3786 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3788 else if (real1 && real2
3789 && (TYPE_PRECISION (TREE_TYPE (primop0))
3790 == TYPE_PRECISION (TREE_TYPE (primop1))))
3791 type = TREE_TYPE (primop0);
3793 /* If args' natural types are both narrower than nominal type
3794 and both extend in the same manner, compare them
3795 in the type of the wider arg.
3796 Otherwise must actually extend both to the nominal
3797 common type lest different ways of extending
3798 alter the result.
3799 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3801 else if (unsignedp0 == unsignedp1 && real1 == real2
3802 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3803 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3805 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3806 type = c_common_signed_or_unsigned_type (unsignedp0
3807 || TYPE_UNSIGNED (*restype_ptr),
3808 type);
3809 /* Make sure shorter operand is extended the right way
3810 to match the longer operand. */
3811 primop0
3812 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3813 TREE_TYPE (primop0)),
3814 primop0);
3815 primop1
3816 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3817 TREE_TYPE (primop1)),
3818 primop1);
3820 else
3822 /* Here we must do the comparison on the nominal type
3823 using the args exactly as we received them. */
3824 type = *restype_ptr;
3825 primop0 = op0;
3826 primop1 = op1;
3828 if (!real1 && !real2 && integer_zerop (primop1)
3829 && TYPE_UNSIGNED (*restype_ptr))
3831 tree value = 0;
3832 /* All unsigned values are >= 0, so we warn. However,
3833 if OP0 is a constant that is >= 0, the signedness of
3834 the comparison isn't an issue, so suppress the
3835 warning. */
3836 bool warn =
3837 warn_type_limits && !in_system_header
3838 && c_inhibit_evaluation_warnings == 0
3839 && !(TREE_CODE (primop0) == INTEGER_CST
3840 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3841 primop0)))
3842 /* Do not warn for enumeration types. */
3843 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
3845 switch (code)
3847 case GE_EXPR:
3848 if (warn)
3849 warning_at (loc, OPT_Wtype_limits,
3850 "comparison of unsigned expression >= 0 is always true");
3851 value = truthvalue_true_node;
3852 break;
3854 case LT_EXPR:
3855 if (warn)
3856 warning_at (loc, OPT_Wtype_limits,
3857 "comparison of unsigned expression < 0 is always false");
3858 value = truthvalue_false_node;
3859 break;
3861 default:
3862 break;
3865 if (value != 0)
3867 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3868 if (TREE_SIDE_EFFECTS (primop0))
3869 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3870 primop0, value);
3871 return value;
3876 *op0_ptr = convert (type, primop0);
3877 *op1_ptr = convert (type, primop1);
3879 *restype_ptr = truthvalue_type_node;
3881 return 0;
3884 /* Return a tree for the sum or difference (RESULTCODE says which)
3885 of pointer PTROP and integer INTOP. */
3887 tree
3888 pointer_int_sum (location_t loc, enum tree_code resultcode,
3889 tree ptrop, tree intop)
3891 tree size_exp, ret;
3893 /* The result is a pointer of the same type that is being added. */
3894 tree result_type = TREE_TYPE (ptrop);
3896 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3898 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3899 "pointer of type %<void *%> used in arithmetic");
3900 size_exp = integer_one_node;
3902 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3904 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3905 "pointer to a function used in arithmetic");
3906 size_exp = integer_one_node;
3908 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3910 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3911 "pointer to member function used in arithmetic");
3912 size_exp = integer_one_node;
3914 else
3915 size_exp = size_in_bytes (TREE_TYPE (result_type));
3917 /* We are manipulating pointer values, so we don't need to warn
3918 about relying on undefined signed overflow. We disable the
3919 warning here because we use integer types so fold won't know that
3920 they are really pointers. */
3921 fold_defer_overflow_warnings ();
3923 /* If what we are about to multiply by the size of the elements
3924 contains a constant term, apply distributive law
3925 and multiply that constant term separately.
3926 This helps produce common subexpressions. */
3927 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3928 && !TREE_CONSTANT (intop)
3929 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3930 && TREE_CONSTANT (size_exp)
3931 /* If the constant comes from pointer subtraction,
3932 skip this optimization--it would cause an error. */
3933 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3934 /* If the constant is unsigned, and smaller than the pointer size,
3935 then we must skip this optimization. This is because it could cause
3936 an overflow error if the constant is negative but INTOP is not. */
3937 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3938 || (TYPE_PRECISION (TREE_TYPE (intop))
3939 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3941 enum tree_code subcode = resultcode;
3942 tree int_type = TREE_TYPE (intop);
3943 if (TREE_CODE (intop) == MINUS_EXPR)
3944 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3945 /* Convert both subexpression types to the type of intop,
3946 because weird cases involving pointer arithmetic
3947 can result in a sum or difference with different type args. */
3948 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3949 subcode, ptrop,
3950 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3951 intop = convert (int_type, TREE_OPERAND (intop, 0));
3954 /* Convert the integer argument to a type the same size as sizetype
3955 so the multiply won't overflow spuriously. */
3956 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3957 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3958 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3959 TYPE_UNSIGNED (sizetype)), intop);
3961 /* Replace the integer argument with a suitable product by the object size.
3962 Do this multiplication as signed, then convert to the appropriate type
3963 for the pointer operation and disregard an overflow that occurred only
3964 because of the sign-extension change in the latter conversion. */
3966 tree t = build_binary_op (loc,
3967 MULT_EXPR, intop,
3968 convert (TREE_TYPE (intop), size_exp), 1);
3969 intop = convert (sizetype, t);
3970 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3971 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3972 TREE_INT_CST_HIGH (intop));
3975 /* Create the sum or difference. */
3976 if (resultcode == MINUS_EXPR)
3977 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3979 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3981 fold_undefer_and_ignore_overflow_warnings ();
3983 return ret;
3986 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3987 and if NON_CONST is known not to be permitted in an evaluated part
3988 of a constant expression. */
3990 tree
3991 c_wrap_maybe_const (tree expr, bool non_const)
3993 bool nowarning = TREE_NO_WARNING (expr);
3994 location_t loc = EXPR_LOCATION (expr);
3996 /* This should never be called for C++. */
3997 if (c_dialect_cxx ())
3998 gcc_unreachable ();
4000 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4001 STRIP_TYPE_NOPS (expr);
4002 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4003 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4004 if (nowarning)
4005 TREE_NO_WARNING (expr) = 1;
4006 protected_set_expr_location (expr, loc);
4008 return expr;
4011 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4012 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4013 around the SAVE_EXPR if needed so that c_fully_fold does not need
4014 to look inside SAVE_EXPRs. */
4016 tree
4017 c_save_expr (tree expr)
4019 bool maybe_const = true;
4020 if (c_dialect_cxx ())
4021 return save_expr (expr);
4022 expr = c_fully_fold (expr, false, &maybe_const);
4023 expr = save_expr (expr);
4024 if (!maybe_const)
4025 expr = c_wrap_maybe_const (expr, true);
4026 return expr;
4029 /* Return whether EXPR is a declaration whose address can never be
4030 NULL. */
4032 bool
4033 decl_with_nonnull_addr_p (const_tree expr)
4035 return (DECL_P (expr)
4036 && (TREE_CODE (expr) == PARM_DECL
4037 || TREE_CODE (expr) == LABEL_DECL
4038 || !DECL_WEAK (expr)));
4041 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4042 or for an `if' or `while' statement or ?..: exp. It should already
4043 have been validated to be of suitable type; otherwise, a bad
4044 diagnostic may result.
4046 The EXPR is located at LOCATION.
4048 This preparation consists of taking the ordinary
4049 representation of an expression expr and producing a valid tree
4050 boolean expression describing whether expr is nonzero. We could
4051 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4052 but we optimize comparisons, &&, ||, and !.
4054 The resulting type should always be `truthvalue_type_node'. */
4056 tree
4057 c_common_truthvalue_conversion (location_t location, tree expr)
4059 switch (TREE_CODE (expr))
4061 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4062 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4063 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4064 case ORDERED_EXPR: case UNORDERED_EXPR:
4065 if (TREE_TYPE (expr) == truthvalue_type_node)
4066 return expr;
4067 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4068 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4069 goto ret;
4071 case TRUTH_ANDIF_EXPR:
4072 case TRUTH_ORIF_EXPR:
4073 case TRUTH_AND_EXPR:
4074 case TRUTH_OR_EXPR:
4075 case TRUTH_XOR_EXPR:
4076 if (TREE_TYPE (expr) == truthvalue_type_node)
4077 return expr;
4078 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4079 c_common_truthvalue_conversion (location,
4080 TREE_OPERAND (expr, 0)),
4081 c_common_truthvalue_conversion (location,
4082 TREE_OPERAND (expr, 1)));
4083 goto ret;
4085 case TRUTH_NOT_EXPR:
4086 if (TREE_TYPE (expr) == truthvalue_type_node)
4087 return expr;
4088 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4089 c_common_truthvalue_conversion (location,
4090 TREE_OPERAND (expr, 0)));
4091 goto ret;
4093 case ERROR_MARK:
4094 return expr;
4096 case INTEGER_CST:
4097 return integer_zerop (expr) ? truthvalue_false_node
4098 : truthvalue_true_node;
4100 case REAL_CST:
4101 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4102 ? truthvalue_true_node
4103 : truthvalue_false_node;
4105 case FIXED_CST:
4106 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4107 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4108 ? truthvalue_true_node
4109 : truthvalue_false_node;
4111 case FUNCTION_DECL:
4112 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4113 /* Fall through. */
4115 case ADDR_EXPR:
4117 tree inner = TREE_OPERAND (expr, 0);
4118 if (decl_with_nonnull_addr_p (inner))
4120 /* Common Ada/Pascal programmer's mistake. */
4121 warning_at (location,
4122 OPT_Waddress,
4123 "the address of %qD will always evaluate as %<true%>",
4124 inner);
4125 return truthvalue_true_node;
4127 break;
4130 case COMPLEX_EXPR:
4131 expr = build_binary_op (EXPR_LOCATION (expr),
4132 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4133 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4134 c_common_truthvalue_conversion (location,
4135 TREE_OPERAND (expr, 0)),
4136 c_common_truthvalue_conversion (location,
4137 TREE_OPERAND (expr, 1)),
4139 goto ret;
4141 case NEGATE_EXPR:
4142 case ABS_EXPR:
4143 case FLOAT_EXPR:
4144 case EXCESS_PRECISION_EXPR:
4145 /* These don't change whether an object is nonzero or zero. */
4146 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4148 case LROTATE_EXPR:
4149 case RROTATE_EXPR:
4150 /* These don't change whether an object is zero or nonzero, but
4151 we can't ignore them if their second arg has side-effects. */
4152 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4154 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4155 TREE_OPERAND (expr, 1),
4156 c_common_truthvalue_conversion
4157 (location, TREE_OPERAND (expr, 0)));
4158 goto ret;
4160 else
4161 return c_common_truthvalue_conversion (location,
4162 TREE_OPERAND (expr, 0));
4164 case COND_EXPR:
4165 /* Distribute the conversion into the arms of a COND_EXPR. */
4166 if (c_dialect_cxx ())
4168 tree op1 = TREE_OPERAND (expr, 1);
4169 tree op2 = TREE_OPERAND (expr, 2);
4170 /* In C++ one of the arms might have void type if it is throw. */
4171 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4172 op1 = c_common_truthvalue_conversion (location, op1);
4173 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4174 op2 = c_common_truthvalue_conversion (location, op2);
4175 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4176 TREE_OPERAND (expr, 0), op1, op2);
4177 goto ret;
4179 else
4181 /* Folding will happen later for C. */
4182 expr = build3 (COND_EXPR, truthvalue_type_node,
4183 TREE_OPERAND (expr, 0),
4184 c_common_truthvalue_conversion (location,
4185 TREE_OPERAND (expr, 1)),
4186 c_common_truthvalue_conversion (location,
4187 TREE_OPERAND (expr, 2)));
4188 goto ret;
4191 CASE_CONVERT:
4193 tree totype = TREE_TYPE (expr);
4194 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4196 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4197 since that affects how `default_conversion' will behave. */
4198 if (TREE_CODE (totype) == REFERENCE_TYPE
4199 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4200 break;
4201 /* Don't strip a conversion from C++0x scoped enum, since they
4202 don't implicitly convert to other types. */
4203 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4204 && ENUM_IS_SCOPED (fromtype))
4205 break;
4206 /* If this isn't narrowing the argument, we can ignore it. */
4207 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4208 return c_common_truthvalue_conversion (location,
4209 TREE_OPERAND (expr, 0));
4211 break;
4213 case MODIFY_EXPR:
4214 if (!TREE_NO_WARNING (expr)
4215 && warn_parentheses)
4217 warning (OPT_Wparentheses,
4218 "suggest parentheses around assignment used as truth value");
4219 TREE_NO_WARNING (expr) = 1;
4221 break;
4223 default:
4224 break;
4227 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4229 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4230 expr = (build_binary_op
4231 (EXPR_LOCATION (expr),
4232 (TREE_SIDE_EFFECTS (expr)
4233 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4234 c_common_truthvalue_conversion
4235 (location,
4236 build_unary_op (location, REALPART_EXPR, t, 0)),
4237 c_common_truthvalue_conversion
4238 (location,
4239 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4240 0));
4241 goto ret;
4244 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4246 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4247 FCONST0 (TYPE_MODE
4248 (TREE_TYPE (expr))));
4249 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4251 else
4252 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4254 ret:
4255 protected_set_expr_location (expr, location);
4256 return expr;
4259 static void def_builtin_1 (enum built_in_function fncode,
4260 const char *name,
4261 enum built_in_class fnclass,
4262 tree fntype, tree libtype,
4263 bool both_p, bool fallback_p, bool nonansi_p,
4264 tree fnattrs, bool implicit_p);
4267 /* Apply the TYPE_QUALS to the new DECL. */
4269 void
4270 c_apply_type_quals_to_decl (int type_quals, tree decl)
4272 tree type = TREE_TYPE (decl);
4274 if (type == error_mark_node)
4275 return;
4277 if ((type_quals & TYPE_QUAL_CONST)
4278 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4279 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4280 constructor can produce constant init, so rely on cp_finish_decl to
4281 clear TREE_READONLY if the variable has non-constant init. */
4282 TREE_READONLY (decl) = 1;
4283 if (type_quals & TYPE_QUAL_VOLATILE)
4285 TREE_SIDE_EFFECTS (decl) = 1;
4286 TREE_THIS_VOLATILE (decl) = 1;
4288 if (type_quals & TYPE_QUAL_RESTRICT)
4290 while (type && TREE_CODE (type) == ARRAY_TYPE)
4291 /* Allow 'restrict' on arrays of pointers.
4292 FIXME currently we just ignore it. */
4293 type = TREE_TYPE (type);
4294 if (!type
4295 || !POINTER_TYPE_P (type)
4296 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4297 error ("invalid use of %<restrict%>");
4301 /* Hash function for the problem of multiple type definitions in
4302 different files. This must hash all types that will compare
4303 equal via comptypes to the same value. In practice it hashes
4304 on some of the simple stuff and leaves the details to comptypes. */
4306 static hashval_t
4307 c_type_hash (const void *p)
4309 int n_elements;
4310 int shift, size;
4311 const_tree const t = (const_tree) p;
4312 tree t2;
4313 switch (TREE_CODE (t))
4315 /* For pointers, hash on pointee type plus some swizzling. */
4316 case POINTER_TYPE:
4317 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4318 /* Hash on number of elements and total size. */
4319 case ENUMERAL_TYPE:
4320 shift = 3;
4321 t2 = TYPE_VALUES (t);
4322 break;
4323 case RECORD_TYPE:
4324 shift = 0;
4325 t2 = TYPE_FIELDS (t);
4326 break;
4327 case QUAL_UNION_TYPE:
4328 shift = 1;
4329 t2 = TYPE_FIELDS (t);
4330 break;
4331 case UNION_TYPE:
4332 shift = 2;
4333 t2 = TYPE_FIELDS (t);
4334 break;
4335 default:
4336 gcc_unreachable ();
4338 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4339 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4340 n_elements = list_length (t2);
4341 /* We might have a VLA here. */
4342 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4343 size = 0;
4344 else
4345 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4346 return ((size << 24) | (n_elements << shift));
4349 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4351 /* Return the typed-based alias set for T, which may be an expression
4352 or a type. Return -1 if we don't do anything special. */
4354 alias_set_type
4355 c_common_get_alias_set (tree t)
4357 tree u;
4358 PTR *slot;
4360 /* For VLAs, use the alias set of the element type rather than the
4361 default of alias set 0 for types compared structurally. */
4362 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4364 if (TREE_CODE (t) == ARRAY_TYPE)
4365 return get_alias_set (TREE_TYPE (t));
4366 return -1;
4369 /* Permit type-punning when accessing a union, provided the access
4370 is directly through the union. For example, this code does not
4371 permit taking the address of a union member and then storing
4372 through it. Even the type-punning allowed here is a GCC
4373 extension, albeit a common and useful one; the C standard says
4374 that such accesses have implementation-defined behavior. */
4375 for (u = t;
4376 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4377 u = TREE_OPERAND (u, 0))
4378 if (TREE_CODE (u) == COMPONENT_REF
4379 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4380 return 0;
4382 /* That's all the expressions we handle specially. */
4383 if (!TYPE_P (t))
4384 return -1;
4386 /* The C standard guarantees that any object may be accessed via an
4387 lvalue that has character type. */
4388 if (t == char_type_node
4389 || t == signed_char_type_node
4390 || t == unsigned_char_type_node)
4391 return 0;
4393 /* The C standard specifically allows aliasing between signed and
4394 unsigned variants of the same type. We treat the signed
4395 variant as canonical. */
4396 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4398 tree t1 = c_common_signed_type (t);
4400 /* t1 == t can happen for boolean nodes which are always unsigned. */
4401 if (t1 != t)
4402 return get_alias_set (t1);
4405 /* Handle the case of multiple type nodes referring to "the same" type,
4406 which occurs with IMA. These share an alias set. FIXME: Currently only
4407 C90 is handled. (In C99 type compatibility is not transitive, which
4408 complicates things mightily. The alias set splay trees can theoretically
4409 represent this, but insertion is tricky when you consider all the
4410 different orders things might arrive in.) */
4412 if (c_language != clk_c || flag_isoc99)
4413 return -1;
4415 /* Save time if there's only one input file. */
4416 if (num_in_fnames == 1)
4417 return -1;
4419 /* Pointers need special handling if they point to any type that
4420 needs special handling (below). */
4421 if (TREE_CODE (t) == POINTER_TYPE)
4423 tree t2;
4424 /* Find bottom type under any nested POINTERs. */
4425 for (t2 = TREE_TYPE (t);
4426 TREE_CODE (t2) == POINTER_TYPE;
4427 t2 = TREE_TYPE (t2))
4429 if (TREE_CODE (t2) != RECORD_TYPE
4430 && TREE_CODE (t2) != ENUMERAL_TYPE
4431 && TREE_CODE (t2) != QUAL_UNION_TYPE
4432 && TREE_CODE (t2) != UNION_TYPE)
4433 return -1;
4434 if (TYPE_SIZE (t2) == 0)
4435 return -1;
4437 /* These are the only cases that need special handling. */
4438 if (TREE_CODE (t) != RECORD_TYPE
4439 && TREE_CODE (t) != ENUMERAL_TYPE
4440 && TREE_CODE (t) != QUAL_UNION_TYPE
4441 && TREE_CODE (t) != UNION_TYPE
4442 && TREE_CODE (t) != POINTER_TYPE)
4443 return -1;
4444 /* Undefined? */
4445 if (TYPE_SIZE (t) == 0)
4446 return -1;
4448 /* Look up t in hash table. Only one of the compatible types within each
4449 alias set is recorded in the table. */
4450 if (!type_hash_table)
4451 type_hash_table = htab_create_ggc (1021, c_type_hash,
4452 (htab_eq) lang_hooks.types_compatible_p,
4453 NULL);
4454 slot = htab_find_slot (type_hash_table, t, INSERT);
4455 if (*slot != NULL)
4457 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4458 return TYPE_ALIAS_SET ((tree)*slot);
4460 else
4461 /* Our caller will assign and record (in t) a new alias set; all we need
4462 to do is remember t in the hash table. */
4463 *slot = t;
4465 return -1;
4468 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4469 the second parameter indicates which OPERATOR is being applied.
4470 The COMPLAIN flag controls whether we should diagnose possibly
4471 ill-formed constructs or not. LOC is the location of the SIZEOF or
4472 TYPEOF operator. */
4474 tree
4475 c_sizeof_or_alignof_type (location_t loc,
4476 tree type, bool is_sizeof, int complain)
4478 const char *op_name;
4479 tree value = NULL;
4480 enum tree_code type_code = TREE_CODE (type);
4482 op_name = is_sizeof ? "sizeof" : "__alignof__";
4484 if (type_code == FUNCTION_TYPE)
4486 if (is_sizeof)
4488 if (complain && (pedantic || warn_pointer_arith))
4489 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4490 "invalid application of %<sizeof%> to a function type");
4491 else if (!complain)
4492 return error_mark_node;
4493 value = size_one_node;
4495 else
4497 if (complain)
4499 if (c_dialect_cxx ())
4500 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4501 "%<alignof%> applied to a function type");
4502 else
4503 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4504 "%<_Alignof%> applied to a function type");
4506 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4509 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4511 if (type_code == VOID_TYPE
4512 && complain && (pedantic || warn_pointer_arith))
4513 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4514 "invalid application of %qs to a void type", op_name);
4515 else if (!complain)
4516 return error_mark_node;
4517 value = size_one_node;
4519 else if (!COMPLETE_TYPE_P (type)
4520 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4522 if (complain)
4523 error_at (loc, "invalid application of %qs to incomplete type %qT",
4524 op_name, type);
4525 return error_mark_node;
4527 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4528 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4530 if (complain)
4531 error_at (loc, "invalid application of %qs to array type %qT of "
4532 "incomplete element type", op_name, type);
4533 return error_mark_node;
4535 else
4537 if (is_sizeof)
4538 /* Convert in case a char is more than one unit. */
4539 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4540 size_int (TYPE_PRECISION (char_type_node)
4541 / BITS_PER_UNIT));
4542 else
4543 value = size_int (TYPE_ALIGN_UNIT (type));
4546 /* VALUE will have the middle-end integer type sizetype.
4547 However, we should really return a value of type `size_t',
4548 which is just a typedef for an ordinary integer type. */
4549 value = fold_convert_loc (loc, size_type_node, value);
4551 return value;
4554 /* Implement the __alignof keyword: Return the minimum required
4555 alignment of EXPR, measured in bytes. For VAR_DECLs,
4556 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4557 from an "aligned" __attribute__ specification). LOC is the
4558 location of the ALIGNOF operator. */
4560 tree
4561 c_alignof_expr (location_t loc, tree expr)
4563 tree t;
4565 if (VAR_OR_FUNCTION_DECL_P (expr))
4566 t = size_int (DECL_ALIGN_UNIT (expr));
4568 else if (TREE_CODE (expr) == COMPONENT_REF
4569 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4571 error_at (loc, "%<__alignof%> applied to a bit-field");
4572 t = size_one_node;
4574 else if (TREE_CODE (expr) == COMPONENT_REF
4575 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4576 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4578 else if (TREE_CODE (expr) == INDIRECT_REF)
4580 tree t = TREE_OPERAND (expr, 0);
4581 tree best = t;
4582 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4584 while (CONVERT_EXPR_P (t)
4585 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4587 int thisalign;
4589 t = TREE_OPERAND (t, 0);
4590 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4591 if (thisalign > bestalign)
4592 best = t, bestalign = thisalign;
4594 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4596 else
4597 return c_alignof (loc, TREE_TYPE (expr));
4599 return fold_convert_loc (loc, size_type_node, t);
4602 /* Handle C and C++ default attributes. */
4604 enum built_in_attribute
4606 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4607 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4608 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4609 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4610 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4611 #include "builtin-attrs.def"
4612 #undef DEF_ATTR_NULL_TREE
4613 #undef DEF_ATTR_INT
4614 #undef DEF_ATTR_STRING
4615 #undef DEF_ATTR_IDENT
4616 #undef DEF_ATTR_TREE_LIST
4617 ATTR_LAST
4620 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4622 static void c_init_attributes (void);
4624 enum c_builtin_type
4626 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4627 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4628 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4629 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4630 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4631 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4632 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4633 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4634 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4635 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4636 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4637 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4638 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4639 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4640 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4641 NAME,
4642 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4643 #include "builtin-types.def"
4644 #undef DEF_PRIMITIVE_TYPE
4645 #undef DEF_FUNCTION_TYPE_0
4646 #undef DEF_FUNCTION_TYPE_1
4647 #undef DEF_FUNCTION_TYPE_2
4648 #undef DEF_FUNCTION_TYPE_3
4649 #undef DEF_FUNCTION_TYPE_4
4650 #undef DEF_FUNCTION_TYPE_5
4651 #undef DEF_FUNCTION_TYPE_6
4652 #undef DEF_FUNCTION_TYPE_7
4653 #undef DEF_FUNCTION_TYPE_VAR_0
4654 #undef DEF_FUNCTION_TYPE_VAR_1
4655 #undef DEF_FUNCTION_TYPE_VAR_2
4656 #undef DEF_FUNCTION_TYPE_VAR_3
4657 #undef DEF_FUNCTION_TYPE_VAR_4
4658 #undef DEF_FUNCTION_TYPE_VAR_5
4659 #undef DEF_POINTER_TYPE
4660 BT_LAST
4663 typedef enum c_builtin_type builtin_type;
4665 /* A temporary array for c_common_nodes_and_builtins. Used in
4666 communication with def_fn_type. */
4667 static tree builtin_types[(int) BT_LAST + 1];
4669 /* A helper function for c_common_nodes_and_builtins. Build function type
4670 for DEF with return type RET and N arguments. If VAR is true, then the
4671 function should be variadic after those N arguments.
4673 Takes special care not to ICE if any of the types involved are
4674 error_mark_node, which indicates that said type is not in fact available
4675 (see builtin_type_for_size). In which case the function type as a whole
4676 should be error_mark_node. */
4678 static void
4679 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4681 tree t;
4682 tree *args = XALLOCAVEC (tree, n);
4683 va_list list;
4684 int i;
4686 va_start (list, n);
4687 for (i = 0; i < n; ++i)
4689 builtin_type a = (builtin_type) va_arg (list, int);
4690 t = builtin_types[a];
4691 if (t == error_mark_node)
4692 goto egress;
4693 args[i] = t;
4696 t = builtin_types[ret];
4697 if (t == error_mark_node)
4698 goto egress;
4699 if (var)
4700 t = build_varargs_function_type_array (t, n, args);
4701 else
4702 t = build_function_type_array (t, n, args);
4704 egress:
4705 builtin_types[def] = t;
4706 va_end (list);
4709 /* Build builtin functions common to both C and C++ language
4710 frontends. */
4712 static void
4713 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4715 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4716 builtin_types[ENUM] = VALUE;
4717 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4718 def_fn_type (ENUM, RETURN, 0, 0);
4719 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4720 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4721 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4722 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4723 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4724 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4725 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4726 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4727 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4728 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4729 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4730 ARG6) \
4731 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4732 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4733 ARG6, ARG7) \
4734 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4735 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4736 def_fn_type (ENUM, RETURN, 1, 0);
4737 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4738 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4739 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4740 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4741 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4742 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4743 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4744 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4745 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4746 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4747 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4748 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4750 #include "builtin-types.def"
4752 #undef DEF_PRIMITIVE_TYPE
4753 #undef DEF_FUNCTION_TYPE_1
4754 #undef DEF_FUNCTION_TYPE_2
4755 #undef DEF_FUNCTION_TYPE_3
4756 #undef DEF_FUNCTION_TYPE_4
4757 #undef DEF_FUNCTION_TYPE_5
4758 #undef DEF_FUNCTION_TYPE_6
4759 #undef DEF_FUNCTION_TYPE_VAR_0
4760 #undef DEF_FUNCTION_TYPE_VAR_1
4761 #undef DEF_FUNCTION_TYPE_VAR_2
4762 #undef DEF_FUNCTION_TYPE_VAR_3
4763 #undef DEF_FUNCTION_TYPE_VAR_4
4764 #undef DEF_FUNCTION_TYPE_VAR_5
4765 #undef DEF_POINTER_TYPE
4766 builtin_types[(int) BT_LAST] = NULL_TREE;
4768 c_init_attributes ();
4770 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4771 NONANSI_P, ATTRS, IMPLICIT, COND) \
4772 if (NAME && COND) \
4773 def_builtin_1 (ENUM, NAME, CLASS, \
4774 builtin_types[(int) TYPE], \
4775 builtin_types[(int) LIBTYPE], \
4776 BOTH_P, FALLBACK_P, NONANSI_P, \
4777 built_in_attributes[(int) ATTRS], IMPLICIT);
4778 #include "builtins.def"
4779 #undef DEF_BUILTIN
4781 targetm.init_builtins ();
4783 build_common_builtin_nodes ();
4785 if (flag_mudflap)
4786 mudflap_init ();
4789 /* Like get_identifier, but avoid warnings about null arguments when
4790 the argument may be NULL for targets where GCC lacks stdint.h type
4791 information. */
4793 static inline tree
4794 c_get_ident (const char *id)
4796 return get_identifier (id);
4799 /* Build tree nodes and builtin functions common to both C and C++ language
4800 frontends. */
4802 void
4803 c_common_nodes_and_builtins (void)
4805 int char16_type_size;
4806 int char32_type_size;
4807 int wchar_type_size;
4808 tree array_domain_type;
4809 tree va_list_ref_type_node;
4810 tree va_list_arg_type_node;
4812 build_common_tree_nodes (flag_signed_char, flag_short_double);
4814 /* Define `int' and `char' first so that dbx will output them first. */
4815 record_builtin_type (RID_INT, NULL, integer_type_node);
4816 record_builtin_type (RID_CHAR, "char", char_type_node);
4818 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4819 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4820 but not C. Are the conditionals here needed? */
4821 if (c_dialect_cxx ())
4822 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4823 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4824 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4825 record_builtin_type (RID_MAX, "long unsigned int",
4826 long_unsigned_type_node);
4827 if (int128_integer_type_node != NULL_TREE)
4829 record_builtin_type (RID_INT128, "__int128",
4830 int128_integer_type_node);
4831 record_builtin_type (RID_MAX, "__int128 unsigned",
4832 int128_unsigned_type_node);
4834 if (c_dialect_cxx ())
4835 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4836 record_builtin_type (RID_MAX, "long long int",
4837 long_long_integer_type_node);
4838 record_builtin_type (RID_MAX, "long long unsigned int",
4839 long_long_unsigned_type_node);
4840 if (c_dialect_cxx ())
4841 record_builtin_type (RID_MAX, "long long unsigned",
4842 long_long_unsigned_type_node);
4843 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4844 record_builtin_type (RID_MAX, "short unsigned int",
4845 short_unsigned_type_node);
4846 if (c_dialect_cxx ())
4847 record_builtin_type (RID_MAX, "unsigned short",
4848 short_unsigned_type_node);
4850 /* Define both `signed char' and `unsigned char'. */
4851 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4852 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4854 /* These are types that c_common_type_for_size and
4855 c_common_type_for_mode use. */
4856 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4857 TYPE_DECL, NULL_TREE,
4858 intQI_type_node));
4859 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4860 TYPE_DECL, NULL_TREE,
4861 intHI_type_node));
4862 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4863 TYPE_DECL, NULL_TREE,
4864 intSI_type_node));
4865 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4866 TYPE_DECL, NULL_TREE,
4867 intDI_type_node));
4868 #if HOST_BITS_PER_WIDE_INT >= 64
4869 if (targetm.scalar_mode_supported_p (TImode))
4870 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4871 TYPE_DECL,
4872 get_identifier ("__int128_t"),
4873 intTI_type_node));
4874 #endif
4875 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4876 TYPE_DECL, NULL_TREE,
4877 unsigned_intQI_type_node));
4878 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4879 TYPE_DECL, NULL_TREE,
4880 unsigned_intHI_type_node));
4881 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4882 TYPE_DECL, NULL_TREE,
4883 unsigned_intSI_type_node));
4884 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4885 TYPE_DECL, NULL_TREE,
4886 unsigned_intDI_type_node));
4887 #if HOST_BITS_PER_WIDE_INT >= 64
4888 if (targetm.scalar_mode_supported_p (TImode))
4889 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4890 TYPE_DECL,
4891 get_identifier ("__uint128_t"),
4892 unsigned_intTI_type_node));
4893 #endif
4895 /* Create the widest literal types. */
4896 widest_integer_literal_type_node
4897 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4898 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4899 TYPE_DECL, NULL_TREE,
4900 widest_integer_literal_type_node));
4902 widest_unsigned_literal_type_node
4903 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4904 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4905 TYPE_DECL, NULL_TREE,
4906 widest_unsigned_literal_type_node));
4908 signed_size_type_node = c_common_signed_type (size_type_node);
4910 pid_type_node =
4911 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4913 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4914 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4915 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4917 /* Only supported decimal floating point extension if the target
4918 actually supports underlying modes. */
4919 if (targetm.scalar_mode_supported_p (SDmode)
4920 && targetm.scalar_mode_supported_p (DDmode)
4921 && targetm.scalar_mode_supported_p (TDmode))
4923 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4924 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4925 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4928 if (targetm.fixed_point_supported_p ())
4930 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4931 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4932 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4933 record_builtin_type (RID_MAX, "long long _Fract",
4934 long_long_fract_type_node);
4935 record_builtin_type (RID_MAX, "unsigned short _Fract",
4936 unsigned_short_fract_type_node);
4937 record_builtin_type (RID_MAX, "unsigned _Fract",
4938 unsigned_fract_type_node);
4939 record_builtin_type (RID_MAX, "unsigned long _Fract",
4940 unsigned_long_fract_type_node);
4941 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4942 unsigned_long_long_fract_type_node);
4943 record_builtin_type (RID_MAX, "_Sat short _Fract",
4944 sat_short_fract_type_node);
4945 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4946 record_builtin_type (RID_MAX, "_Sat long _Fract",
4947 sat_long_fract_type_node);
4948 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4949 sat_long_long_fract_type_node);
4950 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4951 sat_unsigned_short_fract_type_node);
4952 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4953 sat_unsigned_fract_type_node);
4954 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4955 sat_unsigned_long_fract_type_node);
4956 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4957 sat_unsigned_long_long_fract_type_node);
4958 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4959 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4960 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4961 record_builtin_type (RID_MAX, "long long _Accum",
4962 long_long_accum_type_node);
4963 record_builtin_type (RID_MAX, "unsigned short _Accum",
4964 unsigned_short_accum_type_node);
4965 record_builtin_type (RID_MAX, "unsigned _Accum",
4966 unsigned_accum_type_node);
4967 record_builtin_type (RID_MAX, "unsigned long _Accum",
4968 unsigned_long_accum_type_node);
4969 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4970 unsigned_long_long_accum_type_node);
4971 record_builtin_type (RID_MAX, "_Sat short _Accum",
4972 sat_short_accum_type_node);
4973 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4974 record_builtin_type (RID_MAX, "_Sat long _Accum",
4975 sat_long_accum_type_node);
4976 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4977 sat_long_long_accum_type_node);
4978 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4979 sat_unsigned_short_accum_type_node);
4980 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4981 sat_unsigned_accum_type_node);
4982 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4983 sat_unsigned_long_accum_type_node);
4984 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4985 sat_unsigned_long_long_accum_type_node);
4989 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4990 TYPE_DECL,
4991 get_identifier ("complex int"),
4992 complex_integer_type_node));
4993 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4994 TYPE_DECL,
4995 get_identifier ("complex float"),
4996 complex_float_type_node));
4997 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4998 TYPE_DECL,
4999 get_identifier ("complex double"),
5000 complex_double_type_node));
5001 lang_hooks.decls.pushdecl
5002 (build_decl (UNKNOWN_LOCATION,
5003 TYPE_DECL, get_identifier ("complex long double"),
5004 complex_long_double_type_node));
5006 if (c_dialect_cxx ())
5007 /* For C++, make fileptr_type_node a distinct void * type until
5008 FILE type is defined. */
5009 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5011 record_builtin_type (RID_VOID, NULL, void_type_node);
5013 /* Set the TYPE_NAME for any variants that were built before
5014 record_builtin_type gave names to the built-in types. */
5016 tree void_name = TYPE_NAME (void_type_node);
5017 TYPE_NAME (void_type_node) = NULL_TREE;
5018 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5019 = void_name;
5020 TYPE_NAME (void_type_node) = void_name;
5023 /* This node must not be shared. */
5024 void_zero_node = make_node (INTEGER_CST);
5025 TREE_TYPE (void_zero_node) = void_type_node;
5027 void_list_node = build_void_list_node ();
5029 /* Make a type to be the domain of a few array types
5030 whose domains don't really matter.
5031 200 is small enough that it always fits in size_t
5032 and large enough that it can hold most function names for the
5033 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5034 array_domain_type = build_index_type (size_int (200));
5036 /* Make a type for arrays of characters.
5037 With luck nothing will ever really depend on the length of this
5038 array type. */
5039 char_array_type_node
5040 = build_array_type (char_type_node, array_domain_type);
5042 /* Likewise for arrays of ints. */
5043 int_array_type_node
5044 = build_array_type (integer_type_node, array_domain_type);
5046 string_type_node = build_pointer_type (char_type_node);
5047 const_string_type_node
5048 = build_pointer_type (build_qualified_type
5049 (char_type_node, TYPE_QUAL_CONST));
5051 /* This is special for C++ so functions can be overloaded. */
5052 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5053 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5054 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5055 underlying_wchar_type_node = wchar_type_node;
5056 if (c_dialect_cxx ())
5058 if (TYPE_UNSIGNED (wchar_type_node))
5059 wchar_type_node = make_unsigned_type (wchar_type_size);
5060 else
5061 wchar_type_node = make_signed_type (wchar_type_size);
5062 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5065 /* This is for wide string constants. */
5066 wchar_array_type_node
5067 = build_array_type (wchar_type_node, array_domain_type);
5069 /* Define 'char16_t'. */
5070 char16_type_node = get_identifier (CHAR16_TYPE);
5071 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5072 char16_type_size = TYPE_PRECISION (char16_type_node);
5073 if (c_dialect_cxx ())
5075 char16_type_node = make_unsigned_type (char16_type_size);
5077 if (cxx_dialect >= cxx0x)
5078 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5081 /* This is for UTF-16 string constants. */
5082 char16_array_type_node
5083 = build_array_type (char16_type_node, array_domain_type);
5085 /* Define 'char32_t'. */
5086 char32_type_node = get_identifier (CHAR32_TYPE);
5087 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5088 char32_type_size = TYPE_PRECISION (char32_type_node);
5089 if (c_dialect_cxx ())
5091 char32_type_node = make_unsigned_type (char32_type_size);
5093 if (cxx_dialect >= cxx0x)
5094 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5097 /* This is for UTF-32 string constants. */
5098 char32_array_type_node
5099 = build_array_type (char32_type_node, array_domain_type);
5101 wint_type_node =
5102 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5104 intmax_type_node =
5105 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5106 uintmax_type_node =
5107 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5109 if (SIG_ATOMIC_TYPE)
5110 sig_atomic_type_node =
5111 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5112 if (INT8_TYPE)
5113 int8_type_node =
5114 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5115 if (INT16_TYPE)
5116 int16_type_node =
5117 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5118 if (INT32_TYPE)
5119 int32_type_node =
5120 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5121 if (INT64_TYPE)
5122 int64_type_node =
5123 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5124 if (UINT8_TYPE)
5125 uint8_type_node =
5126 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5127 if (UINT16_TYPE)
5128 c_uint16_type_node =
5129 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5130 if (UINT32_TYPE)
5131 c_uint32_type_node =
5132 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5133 if (UINT64_TYPE)
5134 c_uint64_type_node =
5135 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5136 if (INT_LEAST8_TYPE)
5137 int_least8_type_node =
5138 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5139 if (INT_LEAST16_TYPE)
5140 int_least16_type_node =
5141 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5142 if (INT_LEAST32_TYPE)
5143 int_least32_type_node =
5144 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5145 if (INT_LEAST64_TYPE)
5146 int_least64_type_node =
5147 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5148 if (UINT_LEAST8_TYPE)
5149 uint_least8_type_node =
5150 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5151 if (UINT_LEAST16_TYPE)
5152 uint_least16_type_node =
5153 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5154 if (UINT_LEAST32_TYPE)
5155 uint_least32_type_node =
5156 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5157 if (UINT_LEAST64_TYPE)
5158 uint_least64_type_node =
5159 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5160 if (INT_FAST8_TYPE)
5161 int_fast8_type_node =
5162 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5163 if (INT_FAST16_TYPE)
5164 int_fast16_type_node =
5165 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5166 if (INT_FAST32_TYPE)
5167 int_fast32_type_node =
5168 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5169 if (INT_FAST64_TYPE)
5170 int_fast64_type_node =
5171 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5172 if (UINT_FAST8_TYPE)
5173 uint_fast8_type_node =
5174 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5175 if (UINT_FAST16_TYPE)
5176 uint_fast16_type_node =
5177 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5178 if (UINT_FAST32_TYPE)
5179 uint_fast32_type_node =
5180 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5181 if (UINT_FAST64_TYPE)
5182 uint_fast64_type_node =
5183 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5184 if (INTPTR_TYPE)
5185 intptr_type_node =
5186 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5187 if (UINTPTR_TYPE)
5188 uintptr_type_node =
5189 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5191 default_function_type
5192 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5193 ptrdiff_type_node
5194 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5195 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5197 lang_hooks.decls.pushdecl
5198 (build_decl (UNKNOWN_LOCATION,
5199 TYPE_DECL, get_identifier ("__builtin_va_list"),
5200 va_list_type_node));
5201 if (targetm.enum_va_list_p)
5203 int l;
5204 const char *pname;
5205 tree ptype;
5207 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5209 lang_hooks.decls.pushdecl
5210 (build_decl (UNKNOWN_LOCATION,
5211 TYPE_DECL, get_identifier (pname),
5212 ptype));
5217 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5219 va_list_arg_type_node = va_list_ref_type_node =
5220 build_pointer_type (TREE_TYPE (va_list_type_node));
5222 else
5224 va_list_arg_type_node = va_list_type_node;
5225 va_list_ref_type_node = build_reference_type (va_list_type_node);
5228 if (!flag_preprocess_only)
5229 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5231 main_identifier_node = get_identifier ("main");
5233 /* Create the built-in __null node. It is important that this is
5234 not shared. */
5235 null_node = make_node (INTEGER_CST);
5236 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5238 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5239 memset (builtin_types, 0, sizeof (builtin_types));
5242 /* The number of named compound-literals generated thus far. */
5243 static GTY(()) int compound_literal_number;
5245 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5247 void
5248 set_compound_literal_name (tree decl)
5250 char *name;
5251 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5252 compound_literal_number);
5253 compound_literal_number++;
5254 DECL_NAME (decl) = get_identifier (name);
5257 tree
5258 build_va_arg (location_t loc, tree expr, tree type)
5260 expr = build1 (VA_ARG_EXPR, type, expr);
5261 SET_EXPR_LOCATION (expr, loc);
5262 return expr;
5266 /* Linked list of disabled built-in functions. */
5268 typedef struct disabled_builtin
5270 const char *name;
5271 struct disabled_builtin *next;
5272 } disabled_builtin;
5273 static disabled_builtin *disabled_builtins = NULL;
5275 static bool builtin_function_disabled_p (const char *);
5277 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5278 begins with "__builtin_", give an error. */
5280 void
5281 disable_builtin_function (const char *name)
5283 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5284 error ("cannot disable built-in function %qs", name);
5285 else
5287 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5288 new_disabled_builtin->name = name;
5289 new_disabled_builtin->next = disabled_builtins;
5290 disabled_builtins = new_disabled_builtin;
5295 /* Return true if the built-in function NAME has been disabled, false
5296 otherwise. */
5298 static bool
5299 builtin_function_disabled_p (const char *name)
5301 disabled_builtin *p;
5302 for (p = disabled_builtins; p != NULL; p = p->next)
5304 if (strcmp (name, p->name) == 0)
5305 return true;
5307 return false;
5311 /* Worker for DEF_BUILTIN.
5312 Possibly define a builtin function with one or two names.
5313 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5314 nonansi_p and flag_no_nonansi_builtin. */
5316 static void
5317 def_builtin_1 (enum built_in_function fncode,
5318 const char *name,
5319 enum built_in_class fnclass,
5320 tree fntype, tree libtype,
5321 bool both_p, bool fallback_p, bool nonansi_p,
5322 tree fnattrs, bool implicit_p)
5324 tree decl;
5325 const char *libname;
5327 if (fntype == error_mark_node)
5328 return;
5330 gcc_assert ((!both_p && !fallback_p)
5331 || !strncmp (name, "__builtin_",
5332 strlen ("__builtin_")));
5334 libname = name + strlen ("__builtin_");
5335 decl = add_builtin_function (name, fntype, fncode, fnclass,
5336 (fallback_p ? libname : NULL),
5337 fnattrs);
5339 set_builtin_decl (fncode, decl, implicit_p);
5341 if (both_p
5342 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5343 && !(nonansi_p && flag_no_nonansi_builtin))
5344 add_builtin_function (libname, libtype, fncode, fnclass,
5345 NULL, fnattrs);
5348 /* Nonzero if the type T promotes to int. This is (nearly) the
5349 integral promotions defined in ISO C99 6.3.1.1/2. */
5351 bool
5352 c_promoting_integer_type_p (const_tree t)
5354 switch (TREE_CODE (t))
5356 case INTEGER_TYPE:
5357 return (TYPE_MAIN_VARIANT (t) == char_type_node
5358 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5359 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5360 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5361 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5362 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5364 case ENUMERAL_TYPE:
5365 /* ??? Technically all enumerations not larger than an int
5366 promote to an int. But this is used along code paths
5367 that only want to notice a size change. */
5368 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5370 case BOOLEAN_TYPE:
5371 return 1;
5373 default:
5374 return 0;
5378 /* Return 1 if PARMS specifies a fixed number of parameters
5379 and none of their types is affected by default promotions. */
5382 self_promoting_args_p (const_tree parms)
5384 const_tree t;
5385 for (t = parms; t; t = TREE_CHAIN (t))
5387 tree type = TREE_VALUE (t);
5389 if (type == error_mark_node)
5390 continue;
5392 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5393 return 0;
5395 if (type == 0)
5396 return 0;
5398 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5399 return 0;
5401 if (c_promoting_integer_type_p (type))
5402 return 0;
5404 return 1;
5407 /* Recursively remove any '*' or '&' operator from TYPE. */
5408 tree
5409 strip_pointer_operator (tree t)
5411 while (POINTER_TYPE_P (t))
5412 t = TREE_TYPE (t);
5413 return t;
5416 /* Recursively remove pointer or array type from TYPE. */
5417 tree
5418 strip_pointer_or_array_types (tree t)
5420 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5421 t = TREE_TYPE (t);
5422 return t;
5425 /* Used to compare case labels. K1 and K2 are actually tree nodes
5426 representing case labels, or NULL_TREE for a `default' label.
5427 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5428 K2, and 0 if K1 and K2 are equal. */
5431 case_compare (splay_tree_key k1, splay_tree_key k2)
5433 /* Consider a NULL key (such as arises with a `default' label) to be
5434 smaller than anything else. */
5435 if (!k1)
5436 return k2 ? -1 : 0;
5437 else if (!k2)
5438 return k1 ? 1 : 0;
5440 return tree_int_cst_compare ((tree) k1, (tree) k2);
5443 /* Process a case label, located at LOC, for the range LOW_VALUE
5444 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5445 then this case label is actually a `default' label. If only
5446 HIGH_VALUE is NULL_TREE, then case label was declared using the
5447 usual C/C++ syntax, rather than the GNU case range extension.
5448 CASES is a tree containing all the case ranges processed so far;
5449 COND is the condition for the switch-statement itself. Returns the
5450 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5451 is created. */
5453 tree
5454 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5455 tree low_value, tree high_value)
5457 tree type;
5458 tree label;
5459 tree case_label;
5460 splay_tree_node node;
5462 /* Create the LABEL_DECL itself. */
5463 label = create_artificial_label (loc);
5465 /* If there was an error processing the switch condition, bail now
5466 before we get more confused. */
5467 if (!cond || cond == error_mark_node)
5468 goto error_out;
5470 if ((low_value && TREE_TYPE (low_value)
5471 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5472 || (high_value && TREE_TYPE (high_value)
5473 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5475 error_at (loc, "pointers are not permitted as case values");
5476 goto error_out;
5479 /* Case ranges are a GNU extension. */
5480 if (high_value)
5481 pedwarn (loc, OPT_Wpedantic,
5482 "range expressions in switch statements are non-standard");
5484 type = TREE_TYPE (cond);
5485 if (low_value)
5487 low_value = check_case_value (low_value);
5488 low_value = convert_and_check (type, low_value);
5489 if (low_value == error_mark_node)
5490 goto error_out;
5492 if (high_value)
5494 high_value = check_case_value (high_value);
5495 high_value = convert_and_check (type, high_value);
5496 if (high_value == error_mark_node)
5497 goto error_out;
5500 if (low_value && high_value)
5502 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5503 really a case range, even though it was written that way.
5504 Remove the HIGH_VALUE to simplify later processing. */
5505 if (tree_int_cst_equal (low_value, high_value))
5506 high_value = NULL_TREE;
5507 else if (!tree_int_cst_lt (low_value, high_value))
5508 warning_at (loc, 0, "empty range specified");
5511 /* See if the case is in range of the type of the original testing
5512 expression. If both low_value and high_value are out of range,
5513 don't insert the case label and return NULL_TREE. */
5514 if (low_value
5515 && !check_case_bounds (type, orig_type,
5516 &low_value, high_value ? &high_value : NULL))
5517 return NULL_TREE;
5519 /* Look up the LOW_VALUE in the table of case labels we already
5520 have. */
5521 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5522 /* If there was not an exact match, check for overlapping ranges.
5523 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5524 that's a `default' label and the only overlap is an exact match. */
5525 if (!node && (low_value || high_value))
5527 splay_tree_node low_bound;
5528 splay_tree_node high_bound;
5530 /* Even though there wasn't an exact match, there might be an
5531 overlap between this case range and another case range.
5532 Since we've (inductively) not allowed any overlapping case
5533 ranges, we simply need to find the greatest low case label
5534 that is smaller that LOW_VALUE, and the smallest low case
5535 label that is greater than LOW_VALUE. If there is an overlap
5536 it will occur in one of these two ranges. */
5537 low_bound = splay_tree_predecessor (cases,
5538 (splay_tree_key) low_value);
5539 high_bound = splay_tree_successor (cases,
5540 (splay_tree_key) low_value);
5542 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5543 the LOW_VALUE, so there is no need to check unless the
5544 LOW_BOUND is in fact itself a case range. */
5545 if (low_bound
5546 && CASE_HIGH ((tree) low_bound->value)
5547 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5548 low_value) >= 0)
5549 node = low_bound;
5550 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5551 range is bigger than the low end of the current range, so we
5552 are only interested if the current range is a real range, and
5553 not an ordinary case label. */
5554 else if (high_bound
5555 && high_value
5556 && (tree_int_cst_compare ((tree) high_bound->key,
5557 high_value)
5558 <= 0))
5559 node = high_bound;
5561 /* If there was an overlap, issue an error. */
5562 if (node)
5564 tree duplicate = CASE_LABEL ((tree) node->value);
5566 if (high_value)
5568 error_at (loc, "duplicate (or overlapping) case value");
5569 error_at (DECL_SOURCE_LOCATION (duplicate),
5570 "this is the first entry overlapping that value");
5572 else if (low_value)
5574 error_at (loc, "duplicate case value") ;
5575 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5577 else
5579 error_at (loc, "multiple default labels in one switch");
5580 error_at (DECL_SOURCE_LOCATION (duplicate),
5581 "this is the first default label");
5583 goto error_out;
5586 /* Add a CASE_LABEL to the statement-tree. */
5587 case_label = add_stmt (build_case_label (low_value, high_value, label));
5588 /* Register this case label in the splay tree. */
5589 splay_tree_insert (cases,
5590 (splay_tree_key) low_value,
5591 (splay_tree_value) case_label);
5593 return case_label;
5595 error_out:
5596 /* Add a label so that the back-end doesn't think that the beginning of
5597 the switch is unreachable. Note that we do not add a case label, as
5598 that just leads to duplicates and thence to failure later on. */
5599 if (!cases->root)
5601 tree t = create_artificial_label (loc);
5602 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5604 return error_mark_node;
5607 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5608 Used to verify that case values match up with enumerator values. */
5610 static void
5611 match_case_to_enum_1 (tree key, tree type, tree label)
5613 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5615 /* ??? Not working too hard to print the double-word value.
5616 Should perhaps be done with %lwd in the diagnostic routines? */
5617 if (TREE_INT_CST_HIGH (key) == 0)
5618 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5619 TREE_INT_CST_LOW (key));
5620 else if (!TYPE_UNSIGNED (type)
5621 && TREE_INT_CST_HIGH (key) == -1
5622 && TREE_INT_CST_LOW (key) != 0)
5623 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5624 -TREE_INT_CST_LOW (key));
5625 else
5626 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5627 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5628 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5630 if (TYPE_NAME (type) == 0)
5631 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5632 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5633 "case value %qs not in enumerated type",
5634 buf);
5635 else
5636 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5637 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5638 "case value %qs not in enumerated type %qT",
5639 buf, type);
5642 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5643 Used to verify that case values match up with enumerator values. */
5645 static int
5646 match_case_to_enum (splay_tree_node node, void *data)
5648 tree label = (tree) node->value;
5649 tree type = (tree) data;
5651 /* Skip default case. */
5652 if (!CASE_LOW (label))
5653 return 0;
5655 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5656 when we did our enum->case scan. Reset our scratch bit after. */
5657 if (!CASE_LOW_SEEN (label))
5658 match_case_to_enum_1 (CASE_LOW (label), type, label);
5659 else
5660 CASE_LOW_SEEN (label) = 0;
5662 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5663 not set, that means that CASE_HIGH did not appear when we did our
5664 enum->case scan. Reset our scratch bit after. */
5665 if (CASE_HIGH (label))
5667 if (!CASE_HIGH_SEEN (label))
5668 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5669 else
5670 CASE_HIGH_SEEN (label) = 0;
5673 return 0;
5676 /* Handle -Wswitch*. Called from the front end after parsing the
5677 switch construct. */
5678 /* ??? Should probably be somewhere generic, since other languages
5679 besides C and C++ would want this. At the moment, however, C/C++
5680 are the only tree-ssa languages that support enumerations at all,
5681 so the point is moot. */
5683 void
5684 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5685 tree type, tree cond)
5687 splay_tree_node default_node;
5688 splay_tree_node node;
5689 tree chain;
5691 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5692 return;
5694 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5695 if (!default_node)
5696 warning_at (switch_location, OPT_Wswitch_default,
5697 "switch missing default case");
5699 /* From here on, we only care about about enumerated types. */
5700 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5701 return;
5703 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5704 if (!warn_switch_enum && !warn_switch)
5705 return;
5707 /* Check the cases. Warn about case values which are not members of
5708 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5709 there is no default case, check that exactly all enumeration
5710 literals are covered by the cases. */
5712 /* Clearing COND if it is not an integer constant simplifies
5713 the tests inside the loop below. */
5714 if (TREE_CODE (cond) != INTEGER_CST)
5715 cond = NULL_TREE;
5717 /* The time complexity here is O(N*lg(N)) worst case, but for the
5718 common case of monotonically increasing enumerators, it is
5719 O(N), since the nature of the splay tree will keep the next
5720 element adjacent to the root at all times. */
5722 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5724 tree value = TREE_VALUE (chain);
5725 if (TREE_CODE (value) == CONST_DECL)
5726 value = DECL_INITIAL (value);
5727 node = splay_tree_lookup (cases, (splay_tree_key) value);
5728 if (node)
5730 /* Mark the CASE_LOW part of the case entry as seen. */
5731 tree label = (tree) node->value;
5732 CASE_LOW_SEEN (label) = 1;
5733 continue;
5736 /* Even though there wasn't an exact match, there might be a
5737 case range which includes the enumerator's value. */
5738 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5739 if (node && CASE_HIGH ((tree) node->value))
5741 tree label = (tree) node->value;
5742 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5743 if (cmp >= 0)
5745 /* If we match the upper bound exactly, mark the CASE_HIGH
5746 part of the case entry as seen. */
5747 if (cmp == 0)
5748 CASE_HIGH_SEEN (label) = 1;
5749 continue;
5753 /* We've now determined that this enumerated literal isn't
5754 handled by the case labels of the switch statement. */
5756 /* If the switch expression is a constant, we only really care
5757 about whether that constant is handled by the switch. */
5758 if (cond && tree_int_cst_compare (cond, value))
5759 continue;
5761 /* If there is a default_node, the only relevant option is
5762 Wswitch-enum. Otherwise, if both are enabled then we prefer
5763 to warn using -Wswitch because -Wswitch is enabled by -Wall
5764 while -Wswitch-enum is explicit. */
5765 warning_at (switch_location,
5766 (default_node || !warn_switch
5767 ? OPT_Wswitch_enum
5768 : OPT_Wswitch),
5769 "enumeration value %qE not handled in switch",
5770 TREE_PURPOSE (chain));
5773 /* Warn if there are case expressions that don't correspond to
5774 enumerators. This can occur since C and C++ don't enforce
5775 type-checking of assignments to enumeration variables.
5777 The time complexity here is now always O(N) worst case, since
5778 we should have marked both the lower bound and upper bound of
5779 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5780 above. This scan also resets those fields. */
5782 splay_tree_foreach (cases, match_case_to_enum, type);
5785 /* Finish an expression taking the address of LABEL (an
5786 IDENTIFIER_NODE). Returns an expression for the address.
5788 LOC is the location for the expression returned. */
5790 tree
5791 finish_label_address_expr (tree label, location_t loc)
5793 tree result;
5795 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5797 if (label == error_mark_node)
5798 return error_mark_node;
5800 label = lookup_label (label);
5801 if (label == NULL_TREE)
5802 result = null_pointer_node;
5803 else
5805 TREE_USED (label) = 1;
5806 result = build1 (ADDR_EXPR, ptr_type_node, label);
5807 /* The current function is not necessarily uninlinable.
5808 Computed gotos are incompatible with inlining, but the value
5809 here could be used only in a diagnostic, for example. */
5810 protected_set_expr_location (result, loc);
5813 return result;
5817 /* Given a boolean expression ARG, return a tree representing an increment
5818 or decrement (as indicated by CODE) of ARG. The front end must check for
5819 invalid cases (e.g., decrement in C++). */
5820 tree
5821 boolean_increment (enum tree_code code, tree arg)
5823 tree val;
5824 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5826 arg = stabilize_reference (arg);
5827 switch (code)
5829 case PREINCREMENT_EXPR:
5830 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5831 break;
5832 case POSTINCREMENT_EXPR:
5833 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5834 arg = save_expr (arg);
5835 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5836 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5837 break;
5838 case PREDECREMENT_EXPR:
5839 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5840 invert_truthvalue_loc (input_location, arg));
5841 break;
5842 case POSTDECREMENT_EXPR:
5843 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5844 invert_truthvalue_loc (input_location, arg));
5845 arg = save_expr (arg);
5846 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5847 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5848 break;
5849 default:
5850 gcc_unreachable ();
5852 TREE_SIDE_EFFECTS (val) = 1;
5853 return val;
5856 /* Built-in macros for stddef.h and stdint.h, that require macros
5857 defined in this file. */
5858 void
5859 c_stddef_cpp_builtins(void)
5861 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5862 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5863 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5864 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5865 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5866 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5867 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5868 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5869 if (SIG_ATOMIC_TYPE)
5870 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5871 if (INT8_TYPE)
5872 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5873 if (INT16_TYPE)
5874 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5875 if (INT32_TYPE)
5876 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5877 if (INT64_TYPE)
5878 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5879 if (UINT8_TYPE)
5880 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5881 if (UINT16_TYPE)
5882 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5883 if (UINT32_TYPE)
5884 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5885 if (UINT64_TYPE)
5886 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5887 if (INT_LEAST8_TYPE)
5888 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5889 if (INT_LEAST16_TYPE)
5890 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5891 if (INT_LEAST32_TYPE)
5892 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5893 if (INT_LEAST64_TYPE)
5894 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5895 if (UINT_LEAST8_TYPE)
5896 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5897 if (UINT_LEAST16_TYPE)
5898 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5899 if (UINT_LEAST32_TYPE)
5900 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5901 if (UINT_LEAST64_TYPE)
5902 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5903 if (INT_FAST8_TYPE)
5904 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5905 if (INT_FAST16_TYPE)
5906 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5907 if (INT_FAST32_TYPE)
5908 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5909 if (INT_FAST64_TYPE)
5910 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5911 if (UINT_FAST8_TYPE)
5912 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5913 if (UINT_FAST16_TYPE)
5914 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5915 if (UINT_FAST32_TYPE)
5916 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5917 if (UINT_FAST64_TYPE)
5918 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5919 if (INTPTR_TYPE)
5920 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5921 if (UINTPTR_TYPE)
5922 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5925 static void
5926 c_init_attributes (void)
5928 /* Fill in the built_in_attributes array. */
5929 #define DEF_ATTR_NULL_TREE(ENUM) \
5930 built_in_attributes[(int) ENUM] = NULL_TREE;
5931 #define DEF_ATTR_INT(ENUM, VALUE) \
5932 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5933 #define DEF_ATTR_STRING(ENUM, VALUE) \
5934 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5935 #define DEF_ATTR_IDENT(ENUM, STRING) \
5936 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5937 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5938 built_in_attributes[(int) ENUM] \
5939 = tree_cons (built_in_attributes[(int) PURPOSE], \
5940 built_in_attributes[(int) VALUE], \
5941 built_in_attributes[(int) CHAIN]);
5942 #include "builtin-attrs.def"
5943 #undef DEF_ATTR_NULL_TREE
5944 #undef DEF_ATTR_INT
5945 #undef DEF_ATTR_IDENT
5946 #undef DEF_ATTR_TREE_LIST
5949 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5950 identifier as an argument, so the front end shouldn't look it up. */
5952 bool
5953 attribute_takes_identifier_p (const_tree attr_id)
5955 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
5956 if (spec == NULL)
5957 /* Unknown attribute that we'll end up ignoring, return true so we
5958 don't complain about an identifier argument. */
5959 return true;
5960 else if (!strcmp ("mode", spec->name)
5961 || !strcmp ("format", spec->name)
5962 || !strcmp ("cleanup", spec->name))
5963 return true;
5964 else
5965 return targetm.attribute_takes_identifier_p (attr_id);
5968 /* Attribute handlers common to C front ends. */
5970 /* Handle a "packed" attribute; arguments as in
5971 struct attribute_spec.handler. */
5973 static tree
5974 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5975 int flags, bool *no_add_attrs)
5977 if (TYPE_P (*node))
5979 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5980 *node = build_variant_type_copy (*node);
5981 TYPE_PACKED (*node) = 1;
5983 else if (TREE_CODE (*node) == FIELD_DECL)
5985 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5986 /* Still pack bitfields. */
5987 && ! DECL_INITIAL (*node))
5988 warning (OPT_Wattributes,
5989 "%qE attribute ignored for field of type %qT",
5990 name, TREE_TYPE (*node));
5991 else
5992 DECL_PACKED (*node) = 1;
5994 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5995 used for DECL_REGISTER. It wouldn't mean anything anyway.
5996 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5997 that changes what the typedef is typing. */
5998 else
6000 warning (OPT_Wattributes, "%qE attribute ignored", name);
6001 *no_add_attrs = true;
6004 return NULL_TREE;
6007 /* Handle a "nocommon" attribute; arguments as in
6008 struct attribute_spec.handler. */
6010 static tree
6011 handle_nocommon_attribute (tree *node, tree name,
6012 tree ARG_UNUSED (args),
6013 int ARG_UNUSED (flags), bool *no_add_attrs)
6015 if (TREE_CODE (*node) == VAR_DECL)
6016 DECL_COMMON (*node) = 0;
6017 else
6019 warning (OPT_Wattributes, "%qE attribute ignored", name);
6020 *no_add_attrs = true;
6023 return NULL_TREE;
6026 /* Handle a "common" attribute; arguments as in
6027 struct attribute_spec.handler. */
6029 static tree
6030 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6031 int ARG_UNUSED (flags), bool *no_add_attrs)
6033 if (TREE_CODE (*node) == VAR_DECL)
6034 DECL_COMMON (*node) = 1;
6035 else
6037 warning (OPT_Wattributes, "%qE attribute ignored", name);
6038 *no_add_attrs = true;
6041 return NULL_TREE;
6044 /* Handle a "noreturn" attribute; arguments as in
6045 struct attribute_spec.handler. */
6047 static tree
6048 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6049 int ARG_UNUSED (flags), bool *no_add_attrs)
6051 tree type = TREE_TYPE (*node);
6053 /* See FIXME comment in c_common_attribute_table. */
6054 if (TREE_CODE (*node) == FUNCTION_DECL
6055 || objc_method_decl (TREE_CODE (*node)))
6056 TREE_THIS_VOLATILE (*node) = 1;
6057 else if (TREE_CODE (type) == POINTER_TYPE
6058 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6059 TREE_TYPE (*node)
6060 = build_pointer_type
6061 (build_type_variant (TREE_TYPE (type),
6062 TYPE_READONLY (TREE_TYPE (type)), 1));
6063 else
6065 warning (OPT_Wattributes, "%qE attribute ignored", name);
6066 *no_add_attrs = true;
6069 return NULL_TREE;
6072 /* Handle a "hot" and attribute; arguments as in
6073 struct attribute_spec.handler. */
6075 static tree
6076 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6077 int ARG_UNUSED (flags), bool *no_add_attrs)
6079 if (TREE_CODE (*node) == FUNCTION_DECL)
6081 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6083 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6084 name, "cold");
6085 *no_add_attrs = true;
6087 /* Most of the rest of the hot processing is done later with
6088 lookup_attribute. */
6090 else
6092 warning (OPT_Wattributes, "%qE attribute ignored", name);
6093 *no_add_attrs = true;
6096 return NULL_TREE;
6098 /* Handle a "cold" and attribute; arguments as in
6099 struct attribute_spec.handler. */
6101 static tree
6102 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6103 int ARG_UNUSED (flags), bool *no_add_attrs)
6105 if (TREE_CODE (*node) == FUNCTION_DECL)
6107 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6109 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6110 name, "hot");
6111 *no_add_attrs = true;
6113 /* Most of the rest of the cold processing is done later with
6114 lookup_attribute. */
6116 else
6118 warning (OPT_Wattributes, "%qE attribute ignored", name);
6119 *no_add_attrs = true;
6122 return NULL_TREE;
6125 /* Handle a "noinline" attribute; arguments as in
6126 struct attribute_spec.handler. */
6128 static tree
6129 handle_noinline_attribute (tree *node, tree name,
6130 tree ARG_UNUSED (args),
6131 int ARG_UNUSED (flags), bool *no_add_attrs)
6133 if (TREE_CODE (*node) == FUNCTION_DECL)
6134 DECL_UNINLINABLE (*node) = 1;
6135 else
6137 warning (OPT_Wattributes, "%qE attribute ignored", name);
6138 *no_add_attrs = true;
6141 return NULL_TREE;
6144 /* Handle a "noclone" attribute; arguments as in
6145 struct attribute_spec.handler. */
6147 static tree
6148 handle_noclone_attribute (tree *node, tree name,
6149 tree ARG_UNUSED (args),
6150 int ARG_UNUSED (flags), bool *no_add_attrs)
6152 if (TREE_CODE (*node) != FUNCTION_DECL)
6154 warning (OPT_Wattributes, "%qE attribute ignored", name);
6155 *no_add_attrs = true;
6158 return NULL_TREE;
6161 /* Handle a "always_inline" attribute; arguments as in
6162 struct attribute_spec.handler. */
6164 static tree
6165 handle_always_inline_attribute (tree *node, tree name,
6166 tree ARG_UNUSED (args),
6167 int ARG_UNUSED (flags),
6168 bool *no_add_attrs)
6170 if (TREE_CODE (*node) == FUNCTION_DECL)
6172 /* Set the attribute and mark it for disregarding inline
6173 limits. */
6174 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6176 else
6178 warning (OPT_Wattributes, "%qE attribute ignored", name);
6179 *no_add_attrs = true;
6182 return NULL_TREE;
6185 /* Handle a "gnu_inline" attribute; arguments as in
6186 struct attribute_spec.handler. */
6188 static tree
6189 handle_gnu_inline_attribute (tree *node, tree name,
6190 tree ARG_UNUSED (args),
6191 int ARG_UNUSED (flags),
6192 bool *no_add_attrs)
6194 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6196 /* Do nothing else, just set the attribute. We'll get at
6197 it later with lookup_attribute. */
6199 else
6201 warning (OPT_Wattributes, "%qE attribute ignored", name);
6202 *no_add_attrs = true;
6205 return NULL_TREE;
6208 /* Handle a "leaf" attribute; arguments as in
6209 struct attribute_spec.handler. */
6211 static tree
6212 handle_leaf_attribute (tree *node, tree name,
6213 tree ARG_UNUSED (args),
6214 int ARG_UNUSED (flags), bool *no_add_attrs)
6216 if (TREE_CODE (*node) != FUNCTION_DECL)
6218 warning (OPT_Wattributes, "%qE attribute ignored", name);
6219 *no_add_attrs = true;
6221 if (!TREE_PUBLIC (*node))
6223 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6224 *no_add_attrs = true;
6227 return NULL_TREE;
6230 /* Handle an "artificial" attribute; arguments as in
6231 struct attribute_spec.handler. */
6233 static tree
6234 handle_artificial_attribute (tree *node, tree name,
6235 tree ARG_UNUSED (args),
6236 int ARG_UNUSED (flags),
6237 bool *no_add_attrs)
6239 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6241 /* Do nothing else, just set the attribute. We'll get at
6242 it later with lookup_attribute. */
6244 else
6246 warning (OPT_Wattributes, "%qE attribute ignored", name);
6247 *no_add_attrs = true;
6250 return NULL_TREE;
6253 /* Handle a "flatten" attribute; arguments as in
6254 struct attribute_spec.handler. */
6256 static tree
6257 handle_flatten_attribute (tree *node, tree name,
6258 tree args ATTRIBUTE_UNUSED,
6259 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6261 if (TREE_CODE (*node) == FUNCTION_DECL)
6262 /* Do nothing else, just set the attribute. We'll get at
6263 it later with lookup_attribute. */
6265 else
6267 warning (OPT_Wattributes, "%qE attribute ignored", name);
6268 *no_add_attrs = true;
6271 return NULL_TREE;
6274 /* Handle a "warning" or "error" attribute; arguments as in
6275 struct attribute_spec.handler. */
6277 static tree
6278 handle_error_attribute (tree *node, tree name, tree args,
6279 int ARG_UNUSED (flags), bool *no_add_attrs)
6281 if (TREE_CODE (*node) == FUNCTION_DECL
6282 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6283 /* Do nothing else, just set the attribute. We'll get at
6284 it later with lookup_attribute. */
6286 else
6288 warning (OPT_Wattributes, "%qE attribute ignored", name);
6289 *no_add_attrs = true;
6292 return NULL_TREE;
6295 /* Handle a "used" attribute; arguments as in
6296 struct attribute_spec.handler. */
6298 static tree
6299 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6300 int ARG_UNUSED (flags), bool *no_add_attrs)
6302 tree node = *pnode;
6304 if (TREE_CODE (node) == FUNCTION_DECL
6305 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6306 || (TREE_CODE (node) == TYPE_DECL))
6308 TREE_USED (node) = 1;
6309 DECL_PRESERVE_P (node) = 1;
6310 if (TREE_CODE (node) == VAR_DECL)
6311 DECL_READ_P (node) = 1;
6313 else
6315 warning (OPT_Wattributes, "%qE attribute ignored", name);
6316 *no_add_attrs = true;
6319 return NULL_TREE;
6322 /* Handle a "unused" attribute; arguments as in
6323 struct attribute_spec.handler. */
6325 static tree
6326 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6327 int flags, bool *no_add_attrs)
6329 if (DECL_P (*node))
6331 tree decl = *node;
6333 if (TREE_CODE (decl) == PARM_DECL
6334 || TREE_CODE (decl) == VAR_DECL
6335 || TREE_CODE (decl) == FUNCTION_DECL
6336 || TREE_CODE (decl) == LABEL_DECL
6337 || TREE_CODE (decl) == TYPE_DECL)
6339 TREE_USED (decl) = 1;
6340 if (TREE_CODE (decl) == VAR_DECL
6341 || TREE_CODE (decl) == PARM_DECL)
6342 DECL_READ_P (decl) = 1;
6344 else
6346 warning (OPT_Wattributes, "%qE attribute ignored", name);
6347 *no_add_attrs = true;
6350 else
6352 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6353 *node = build_variant_type_copy (*node);
6354 TREE_USED (*node) = 1;
6357 return NULL_TREE;
6360 /* Handle a "externally_visible" attribute; arguments as in
6361 struct attribute_spec.handler. */
6363 static tree
6364 handle_externally_visible_attribute (tree *pnode, tree name,
6365 tree ARG_UNUSED (args),
6366 int ARG_UNUSED (flags),
6367 bool *no_add_attrs)
6369 tree node = *pnode;
6371 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6373 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6374 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6376 warning (OPT_Wattributes,
6377 "%qE attribute have effect only on public objects", name);
6378 *no_add_attrs = true;
6381 else
6383 warning (OPT_Wattributes, "%qE attribute ignored", name);
6384 *no_add_attrs = true;
6387 return NULL_TREE;
6390 /* Handle a "const" attribute; arguments as in
6391 struct attribute_spec.handler. */
6393 static tree
6394 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6395 int ARG_UNUSED (flags), bool *no_add_attrs)
6397 tree type = TREE_TYPE (*node);
6399 /* See FIXME comment on noreturn in c_common_attribute_table. */
6400 if (TREE_CODE (*node) == FUNCTION_DECL)
6401 TREE_READONLY (*node) = 1;
6402 else if (TREE_CODE (type) == POINTER_TYPE
6403 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6404 TREE_TYPE (*node)
6405 = build_pointer_type
6406 (build_type_variant (TREE_TYPE (type), 1,
6407 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6408 else
6410 warning (OPT_Wattributes, "%qE attribute ignored", name);
6411 *no_add_attrs = true;
6414 return NULL_TREE;
6417 /* Handle a "transparent_union" attribute; arguments as in
6418 struct attribute_spec.handler. */
6420 static tree
6421 handle_transparent_union_attribute (tree *node, tree name,
6422 tree ARG_UNUSED (args), int flags,
6423 bool *no_add_attrs)
6425 tree type;
6427 *no_add_attrs = true;
6429 if (TREE_CODE (*node) == TYPE_DECL)
6430 node = &TREE_TYPE (*node);
6431 type = *node;
6433 if (TREE_CODE (type) == UNION_TYPE)
6435 /* Make sure that the first field will work for a transparent union.
6436 If the type isn't complete yet, leave the check to the code in
6437 finish_struct. */
6438 if (TYPE_SIZE (type))
6440 tree first = first_field (type);
6441 if (first == NULL_TREE
6442 || DECL_ARTIFICIAL (first)
6443 || TYPE_MODE (type) != DECL_MODE (first))
6444 goto ignored;
6447 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6449 /* If the type isn't complete yet, setting the flag
6450 on a variant wouldn't ever be checked. */
6451 if (!TYPE_SIZE (type))
6452 goto ignored;
6454 /* build_duplicate_type doesn't work for C++. */
6455 if (c_dialect_cxx ())
6456 goto ignored;
6458 /* A type variant isn't good enough, since we don't a cast
6459 to such a type removed as a no-op. */
6460 *node = type = build_duplicate_type (type);
6463 TYPE_TRANSPARENT_AGGR (type) = 1;
6464 return NULL_TREE;
6467 ignored:
6468 warning (OPT_Wattributes, "%qE attribute ignored", name);
6469 return NULL_TREE;
6472 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6473 get the requested priority for a constructor or destructor,
6474 possibly issuing diagnostics for invalid or reserved
6475 priorities. */
6477 static priority_type
6478 get_priority (tree args, bool is_destructor)
6480 HOST_WIDE_INT pri;
6481 tree arg;
6483 if (!args)
6484 return DEFAULT_INIT_PRIORITY;
6486 if (!SUPPORTS_INIT_PRIORITY)
6488 if (is_destructor)
6489 error ("destructor priorities are not supported");
6490 else
6491 error ("constructor priorities are not supported");
6492 return DEFAULT_INIT_PRIORITY;
6495 arg = TREE_VALUE (args);
6496 if (!host_integerp (arg, /*pos=*/0)
6497 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6498 goto invalid;
6500 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6501 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6502 goto invalid;
6504 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6506 if (is_destructor)
6507 warning (0,
6508 "destructor priorities from 0 to %d are reserved "
6509 "for the implementation",
6510 MAX_RESERVED_INIT_PRIORITY);
6511 else
6512 warning (0,
6513 "constructor priorities from 0 to %d are reserved "
6514 "for the implementation",
6515 MAX_RESERVED_INIT_PRIORITY);
6517 return pri;
6519 invalid:
6520 if (is_destructor)
6521 error ("destructor priorities must be integers from 0 to %d inclusive",
6522 MAX_INIT_PRIORITY);
6523 else
6524 error ("constructor priorities must be integers from 0 to %d inclusive",
6525 MAX_INIT_PRIORITY);
6526 return DEFAULT_INIT_PRIORITY;
6529 /* Handle a "constructor" attribute; arguments as in
6530 struct attribute_spec.handler. */
6532 static tree
6533 handle_constructor_attribute (tree *node, tree name, tree args,
6534 int ARG_UNUSED (flags),
6535 bool *no_add_attrs)
6537 tree decl = *node;
6538 tree type = TREE_TYPE (decl);
6540 if (TREE_CODE (decl) == FUNCTION_DECL
6541 && TREE_CODE (type) == FUNCTION_TYPE
6542 && decl_function_context (decl) == 0)
6544 priority_type priority;
6545 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6546 priority = get_priority (args, /*is_destructor=*/false);
6547 SET_DECL_INIT_PRIORITY (decl, priority);
6548 TREE_USED (decl) = 1;
6550 else
6552 warning (OPT_Wattributes, "%qE attribute ignored", name);
6553 *no_add_attrs = true;
6556 return NULL_TREE;
6559 /* Handle a "destructor" attribute; arguments as in
6560 struct attribute_spec.handler. */
6562 static tree
6563 handle_destructor_attribute (tree *node, tree name, tree args,
6564 int ARG_UNUSED (flags),
6565 bool *no_add_attrs)
6567 tree decl = *node;
6568 tree type = TREE_TYPE (decl);
6570 if (TREE_CODE (decl) == FUNCTION_DECL
6571 && TREE_CODE (type) == FUNCTION_TYPE
6572 && decl_function_context (decl) == 0)
6574 priority_type priority;
6575 DECL_STATIC_DESTRUCTOR (decl) = 1;
6576 priority = get_priority (args, /*is_destructor=*/true);
6577 SET_DECL_FINI_PRIORITY (decl, priority);
6578 TREE_USED (decl) = 1;
6580 else
6582 warning (OPT_Wattributes, "%qE attribute ignored", name);
6583 *no_add_attrs = true;
6586 return NULL_TREE;
6589 /* Nonzero if the mode is a valid vector mode for this architecture.
6590 This returns nonzero even if there is no hardware support for the
6591 vector mode, but we can emulate with narrower modes. */
6593 static int
6594 vector_mode_valid_p (enum machine_mode mode)
6596 enum mode_class mclass = GET_MODE_CLASS (mode);
6597 enum machine_mode innermode;
6599 /* Doh! What's going on? */
6600 if (mclass != MODE_VECTOR_INT
6601 && mclass != MODE_VECTOR_FLOAT
6602 && mclass != MODE_VECTOR_FRACT
6603 && mclass != MODE_VECTOR_UFRACT
6604 && mclass != MODE_VECTOR_ACCUM
6605 && mclass != MODE_VECTOR_UACCUM)
6606 return 0;
6608 /* Hardware support. Woo hoo! */
6609 if (targetm.vector_mode_supported_p (mode))
6610 return 1;
6612 innermode = GET_MODE_INNER (mode);
6614 /* We should probably return 1 if requesting V4DI and we have no DI,
6615 but we have V2DI, but this is probably very unlikely. */
6617 /* If we have support for the inner mode, we can safely emulate it.
6618 We may not have V2DI, but me can emulate with a pair of DIs. */
6619 return targetm.scalar_mode_supported_p (innermode);
6623 /* Handle a "mode" attribute; arguments as in
6624 struct attribute_spec.handler. */
6626 static tree
6627 handle_mode_attribute (tree *node, tree name, tree args,
6628 int ARG_UNUSED (flags), bool *no_add_attrs)
6630 tree type = *node;
6631 tree ident = TREE_VALUE (args);
6633 *no_add_attrs = true;
6635 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6636 warning (OPT_Wattributes, "%qE attribute ignored", name);
6637 else
6639 int j;
6640 const char *p = IDENTIFIER_POINTER (ident);
6641 int len = strlen (p);
6642 enum machine_mode mode = VOIDmode;
6643 tree typefm;
6644 bool valid_mode;
6646 if (len > 4 && p[0] == '_' && p[1] == '_'
6647 && p[len - 1] == '_' && p[len - 2] == '_')
6649 char *newp = (char *) alloca (len - 1);
6651 strcpy (newp, &p[2]);
6652 newp[len - 4] = '\0';
6653 p = newp;
6656 /* Change this type to have a type with the specified mode.
6657 First check for the special modes. */
6658 if (!strcmp (p, "byte"))
6659 mode = byte_mode;
6660 else if (!strcmp (p, "word"))
6661 mode = word_mode;
6662 else if (!strcmp (p, "pointer"))
6663 mode = ptr_mode;
6664 else if (!strcmp (p, "libgcc_cmp_return"))
6665 mode = targetm.libgcc_cmp_return_mode ();
6666 else if (!strcmp (p, "libgcc_shift_count"))
6667 mode = targetm.libgcc_shift_count_mode ();
6668 else if (!strcmp (p, "unwind_word"))
6669 mode = targetm.unwind_word_mode ();
6670 else
6671 for (j = 0; j < NUM_MACHINE_MODES; j++)
6672 if (!strcmp (p, GET_MODE_NAME (j)))
6674 mode = (enum machine_mode) j;
6675 break;
6678 if (mode == VOIDmode)
6680 error ("unknown machine mode %qE", ident);
6681 return NULL_TREE;
6684 valid_mode = false;
6685 switch (GET_MODE_CLASS (mode))
6687 case MODE_INT:
6688 case MODE_PARTIAL_INT:
6689 case MODE_FLOAT:
6690 case MODE_DECIMAL_FLOAT:
6691 case MODE_FRACT:
6692 case MODE_UFRACT:
6693 case MODE_ACCUM:
6694 case MODE_UACCUM:
6695 valid_mode = targetm.scalar_mode_supported_p (mode);
6696 break;
6698 case MODE_COMPLEX_INT:
6699 case MODE_COMPLEX_FLOAT:
6700 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6701 break;
6703 case MODE_VECTOR_INT:
6704 case MODE_VECTOR_FLOAT:
6705 case MODE_VECTOR_FRACT:
6706 case MODE_VECTOR_UFRACT:
6707 case MODE_VECTOR_ACCUM:
6708 case MODE_VECTOR_UACCUM:
6709 warning (OPT_Wattributes, "specifying vector types with "
6710 "__attribute__ ((mode)) is deprecated");
6711 warning (OPT_Wattributes,
6712 "use __attribute__ ((vector_size)) instead");
6713 valid_mode = vector_mode_valid_p (mode);
6714 break;
6716 default:
6717 break;
6719 if (!valid_mode)
6721 error ("unable to emulate %qs", p);
6722 return NULL_TREE;
6725 if (POINTER_TYPE_P (type))
6727 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6728 tree (*fn)(tree, enum machine_mode, bool);
6730 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6732 error ("invalid pointer mode %qs", p);
6733 return NULL_TREE;
6736 if (TREE_CODE (type) == POINTER_TYPE)
6737 fn = build_pointer_type_for_mode;
6738 else
6739 fn = build_reference_type_for_mode;
6740 typefm = fn (TREE_TYPE (type), mode, false);
6742 else
6744 /* For fixed-point modes, we need to test if the signness of type
6745 and the machine mode are consistent. */
6746 if (ALL_FIXED_POINT_MODE_P (mode)
6747 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6749 error ("signedness of type and machine mode %qs don%'t match", p);
6750 return NULL_TREE;
6752 /* For fixed-point modes, we need to pass saturating info. */
6753 typefm = lang_hooks.types.type_for_mode (mode,
6754 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6755 : TYPE_UNSIGNED (type));
6758 if (typefm == NULL_TREE)
6760 error ("no data type for mode %qs", p);
6761 return NULL_TREE;
6763 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6765 /* For enumeral types, copy the precision from the integer
6766 type returned above. If not an INTEGER_TYPE, we can't use
6767 this mode for this type. */
6768 if (TREE_CODE (typefm) != INTEGER_TYPE)
6770 error ("cannot use mode %qs for enumeral types", p);
6771 return NULL_TREE;
6774 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6776 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6777 typefm = type;
6779 else
6781 /* We cannot build a type variant, as there's code that assumes
6782 that TYPE_MAIN_VARIANT has the same mode. This includes the
6783 debug generators. Instead, create a subrange type. This
6784 results in all of the enumeral values being emitted only once
6785 in the original, and the subtype gets them by reference. */
6786 if (TYPE_UNSIGNED (type))
6787 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6788 else
6789 typefm = make_signed_type (TYPE_PRECISION (typefm));
6790 TREE_TYPE (typefm) = type;
6793 else if (VECTOR_MODE_P (mode)
6794 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6795 : TREE_CODE (type) != TREE_CODE (typefm))
6797 error ("mode %qs applied to inappropriate type", p);
6798 return NULL_TREE;
6801 *node = typefm;
6804 return NULL_TREE;
6807 /* Handle a "section" attribute; arguments as in
6808 struct attribute_spec.handler. */
6810 static tree
6811 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6812 int ARG_UNUSED (flags), bool *no_add_attrs)
6814 tree decl = *node;
6816 if (targetm_common.have_named_sections)
6818 user_defined_section_attribute = true;
6820 if ((TREE_CODE (decl) == FUNCTION_DECL
6821 || TREE_CODE (decl) == VAR_DECL)
6822 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6824 if (TREE_CODE (decl) == VAR_DECL
6825 && current_function_decl != NULL_TREE
6826 && !TREE_STATIC (decl))
6828 error_at (DECL_SOURCE_LOCATION (decl),
6829 "section attribute cannot be specified for "
6830 "local variables");
6831 *no_add_attrs = true;
6834 /* The decl may have already been given a section attribute
6835 from a previous declaration. Ensure they match. */
6836 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6837 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6838 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6840 error ("section of %q+D conflicts with previous declaration",
6841 *node);
6842 *no_add_attrs = true;
6844 else if (TREE_CODE (decl) == VAR_DECL
6845 && !targetm.have_tls && targetm.emutls.tmpl_section
6846 && DECL_THREAD_LOCAL_P (decl))
6848 error ("section of %q+D cannot be overridden", *node);
6849 *no_add_attrs = true;
6851 else
6852 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6854 else
6856 error ("section attribute not allowed for %q+D", *node);
6857 *no_add_attrs = true;
6860 else
6862 error_at (DECL_SOURCE_LOCATION (*node),
6863 "section attributes are not supported for this target");
6864 *no_add_attrs = true;
6867 return NULL_TREE;
6870 /* Check whether ALIGN is a valid user-specified alignment. If so,
6871 return its base-2 log; if not, output an error and return -1. If
6872 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
6873 no error. */
6875 check_user_alignment (const_tree align, bool allow_zero)
6877 int i;
6879 if (!INTEGRAL_TYPE_P (TREE_TYPE (align))
6880 || TREE_CODE (align) != INTEGER_CST)
6882 error ("requested alignment is not an integer constant");
6883 return -1;
6885 else if (allow_zero && integer_zerop (align))
6886 return -1;
6887 else if ((i = tree_log2 (align)) == -1)
6889 error ("requested alignment is not a power of 2");
6890 return -1;
6892 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6894 error ("requested alignment is too large");
6895 return -1;
6897 return i;
6900 /* Handle a "aligned" attribute; arguments as in
6901 struct attribute_spec.handler. */
6903 static tree
6904 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6905 int flags, bool *no_add_attrs)
6907 tree decl = NULL_TREE;
6908 tree *type = NULL;
6909 int is_type = 0;
6910 tree align_expr = (args ? TREE_VALUE (args)
6911 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6912 int i;
6914 if (DECL_P (*node))
6916 decl = *node;
6917 type = &TREE_TYPE (decl);
6918 is_type = TREE_CODE (*node) == TYPE_DECL;
6920 else if (TYPE_P (*node))
6921 type = node, is_type = 1;
6923 if ((i = check_user_alignment (align_expr, false)) == -1)
6924 *no_add_attrs = true;
6925 else if (is_type)
6927 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6928 /* OK, modify the type in place. */;
6929 /* If we have a TYPE_DECL, then copy the type, so that we
6930 don't accidentally modify a builtin type. See pushdecl. */
6931 else if (decl && TREE_TYPE (decl) != error_mark_node
6932 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6934 tree tt = TREE_TYPE (decl);
6935 *type = build_variant_type_copy (*type);
6936 DECL_ORIGINAL_TYPE (decl) = tt;
6937 TYPE_NAME (*type) = decl;
6938 TREE_USED (*type) = TREE_USED (decl);
6939 TREE_TYPE (decl) = *type;
6941 else
6942 *type = build_variant_type_copy (*type);
6944 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6945 TYPE_USER_ALIGN (*type) = 1;
6947 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6948 && TREE_CODE (decl) != FIELD_DECL)
6950 error ("alignment may not be specified for %q+D", decl);
6951 *no_add_attrs = true;
6953 else if (TREE_CODE (decl) == FUNCTION_DECL
6954 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6956 if (DECL_USER_ALIGN (decl))
6957 error ("alignment for %q+D was previously specified as %d "
6958 "and may not be decreased", decl,
6959 DECL_ALIGN (decl) / BITS_PER_UNIT);
6960 else
6961 error ("alignment for %q+D must be at least %d", decl,
6962 DECL_ALIGN (decl) / BITS_PER_UNIT);
6963 *no_add_attrs = true;
6965 else
6967 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6968 DECL_USER_ALIGN (decl) = 1;
6971 return NULL_TREE;
6974 /* Handle a "weak" attribute; arguments as in
6975 struct attribute_spec.handler. */
6977 static tree
6978 handle_weak_attribute (tree *node, tree name,
6979 tree ARG_UNUSED (args),
6980 int ARG_UNUSED (flags),
6981 bool * ARG_UNUSED (no_add_attrs))
6983 if (TREE_CODE (*node) == FUNCTION_DECL
6984 && DECL_DECLARED_INLINE_P (*node))
6986 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6987 *no_add_attrs = true;
6989 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6991 error ("indirect function %q+D cannot be declared weak", *node);
6992 *no_add_attrs = true;
6993 return NULL_TREE;
6995 else if (TREE_CODE (*node) == FUNCTION_DECL
6996 || TREE_CODE (*node) == VAR_DECL)
6997 declare_weak (*node);
6998 else
6999 warning (OPT_Wattributes, "%qE attribute ignored", name);
7001 return NULL_TREE;
7004 /* Handle an "alias" or "ifunc" attribute; arguments as in
7005 struct attribute_spec.handler, except that IS_ALIAS tells us
7006 whether this is an alias as opposed to ifunc attribute. */
7008 static tree
7009 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7010 bool *no_add_attrs)
7012 tree decl = *node;
7014 if (TREE_CODE (decl) != FUNCTION_DECL
7015 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7017 warning (OPT_Wattributes, "%qE attribute ignored", name);
7018 *no_add_attrs = true;
7020 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7021 || (TREE_CODE (decl) != FUNCTION_DECL
7022 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7023 /* A static variable declaration is always a tentative definition,
7024 but the alias is a non-tentative definition which overrides. */
7025 || (TREE_CODE (decl) != FUNCTION_DECL
7026 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7028 error ("%q+D defined both normally and as %qE attribute", decl, name);
7029 *no_add_attrs = true;
7030 return NULL_TREE;
7032 else if (!is_alias
7033 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7034 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7036 error ("weak %q+D cannot be defined %qE", decl, name);
7037 *no_add_attrs = true;
7038 return NULL_TREE;
7041 /* Note that the very first time we process a nested declaration,
7042 decl_function_context will not be set. Indeed, *would* never
7043 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7044 we do below. After such frobbery, pushdecl would set the context.
7045 In any case, this is never what we want. */
7046 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7048 tree id;
7050 id = TREE_VALUE (args);
7051 if (TREE_CODE (id) != STRING_CST)
7053 error ("attribute %qE argument not a string", name);
7054 *no_add_attrs = true;
7055 return NULL_TREE;
7057 id = get_identifier (TREE_STRING_POINTER (id));
7058 /* This counts as a use of the object pointed to. */
7059 TREE_USED (id) = 1;
7061 if (TREE_CODE (decl) == FUNCTION_DECL)
7062 DECL_INITIAL (decl) = error_mark_node;
7063 else
7065 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7066 DECL_EXTERNAL (decl) = 1;
7067 else
7068 DECL_EXTERNAL (decl) = 0;
7069 TREE_STATIC (decl) = 1;
7072 if (!is_alias)
7073 /* ifuncs are also aliases, so set that attribute too. */
7074 DECL_ATTRIBUTES (decl)
7075 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7077 else
7079 warning (OPT_Wattributes, "%qE attribute ignored", name);
7080 *no_add_attrs = true;
7083 return NULL_TREE;
7086 /* Handle an "alias" or "ifunc" attribute; arguments as in
7087 struct attribute_spec.handler. */
7089 static tree
7090 handle_ifunc_attribute (tree *node, tree name, tree args,
7091 int ARG_UNUSED (flags), bool *no_add_attrs)
7093 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7096 /* Handle an "alias" or "ifunc" attribute; arguments as in
7097 struct attribute_spec.handler. */
7099 static tree
7100 handle_alias_attribute (tree *node, tree name, tree args,
7101 int ARG_UNUSED (flags), bool *no_add_attrs)
7103 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7106 /* Handle a "weakref" attribute; arguments as in struct
7107 attribute_spec.handler. */
7109 static tree
7110 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7111 int flags, bool *no_add_attrs)
7113 tree attr = NULL_TREE;
7115 /* We must ignore the attribute when it is associated with
7116 local-scoped decls, since attribute alias is ignored and many
7117 such symbols do not even have a DECL_WEAK field. */
7118 if (decl_function_context (*node)
7119 || current_function_decl
7120 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7122 warning (OPT_Wattributes, "%qE attribute ignored", name);
7123 *no_add_attrs = true;
7124 return NULL_TREE;
7127 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7129 error ("indirect function %q+D cannot be declared weakref", *node);
7130 *no_add_attrs = true;
7131 return NULL_TREE;
7134 /* The idea here is that `weakref("name")' mutates into `weakref,
7135 alias("name")', and weakref without arguments, in turn,
7136 implicitly adds weak. */
7138 if (args)
7140 attr = tree_cons (get_identifier ("alias"), args, attr);
7141 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7143 *no_add_attrs = true;
7145 decl_attributes (node, attr, flags);
7147 else
7149 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7150 error_at (DECL_SOURCE_LOCATION (*node),
7151 "weakref attribute must appear before alias attribute");
7153 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7154 and that isn't supported; and because it wants to add it to
7155 the list of weak decls, which isn't helpful. */
7156 DECL_WEAK (*node) = 1;
7159 return NULL_TREE;
7162 /* Handle an "visibility" attribute; arguments as in
7163 struct attribute_spec.handler. */
7165 static tree
7166 handle_visibility_attribute (tree *node, tree name, tree args,
7167 int ARG_UNUSED (flags),
7168 bool *ARG_UNUSED (no_add_attrs))
7170 tree decl = *node;
7171 tree id = TREE_VALUE (args);
7172 enum symbol_visibility vis;
7174 if (TYPE_P (*node))
7176 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7177 /* OK */;
7178 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7180 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7181 name);
7182 return NULL_TREE;
7184 else if (TYPE_FIELDS (*node))
7186 error ("%qE attribute ignored because %qT is already defined",
7187 name, *node);
7188 return NULL_TREE;
7191 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7193 warning (OPT_Wattributes, "%qE attribute ignored", name);
7194 return NULL_TREE;
7197 if (TREE_CODE (id) != STRING_CST)
7199 error ("visibility argument not a string");
7200 return NULL_TREE;
7203 /* If this is a type, set the visibility on the type decl. */
7204 if (TYPE_P (decl))
7206 decl = TYPE_NAME (decl);
7207 if (!decl)
7208 return NULL_TREE;
7209 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7211 warning (OPT_Wattributes, "%qE attribute ignored on types",
7212 name);
7213 return NULL_TREE;
7217 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7218 vis = VISIBILITY_DEFAULT;
7219 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7220 vis = VISIBILITY_INTERNAL;
7221 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7222 vis = VISIBILITY_HIDDEN;
7223 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7224 vis = VISIBILITY_PROTECTED;
7225 else
7227 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7228 vis = VISIBILITY_DEFAULT;
7231 if (DECL_VISIBILITY_SPECIFIED (decl)
7232 && vis != DECL_VISIBILITY (decl))
7234 tree attributes = (TYPE_P (*node)
7235 ? TYPE_ATTRIBUTES (*node)
7236 : DECL_ATTRIBUTES (decl));
7237 if (lookup_attribute ("visibility", attributes))
7238 error ("%qD redeclared with different visibility", decl);
7239 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7240 && lookup_attribute ("dllimport", attributes))
7241 error ("%qD was declared %qs which implies default visibility",
7242 decl, "dllimport");
7243 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7244 && lookup_attribute ("dllexport", attributes))
7245 error ("%qD was declared %qs which implies default visibility",
7246 decl, "dllexport");
7249 DECL_VISIBILITY (decl) = vis;
7250 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7252 /* Go ahead and attach the attribute to the node as well. This is needed
7253 so we can determine whether we have VISIBILITY_DEFAULT because the
7254 visibility was not specified, or because it was explicitly overridden
7255 from the containing scope. */
7257 return NULL_TREE;
7260 /* Determine the ELF symbol visibility for DECL, which is either a
7261 variable or a function. It is an error to use this function if a
7262 definition of DECL is not available in this translation unit.
7263 Returns true if the final visibility has been determined by this
7264 function; false if the caller is free to make additional
7265 modifications. */
7267 bool
7268 c_determine_visibility (tree decl)
7270 gcc_assert (TREE_CODE (decl) == VAR_DECL
7271 || TREE_CODE (decl) == FUNCTION_DECL);
7273 /* If the user explicitly specified the visibility with an
7274 attribute, honor that. DECL_VISIBILITY will have been set during
7275 the processing of the attribute. We check for an explicit
7276 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7277 to distinguish the use of an attribute from the use of a "#pragma
7278 GCC visibility push(...)"; in the latter case we still want other
7279 considerations to be able to overrule the #pragma. */
7280 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7281 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7282 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7283 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7284 return true;
7286 /* Set default visibility to whatever the user supplied with
7287 visibility_specified depending on #pragma GCC visibility. */
7288 if (!DECL_VISIBILITY_SPECIFIED (decl))
7290 if (visibility_options.inpragma
7291 || DECL_VISIBILITY (decl) != default_visibility)
7293 DECL_VISIBILITY (decl) = default_visibility;
7294 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7295 /* If visibility changed and DECL already has DECL_RTL, ensure
7296 symbol flags are updated. */
7297 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7298 || TREE_CODE (decl) == FUNCTION_DECL)
7299 && DECL_RTL_SET_P (decl))
7300 make_decl_rtl (decl);
7303 return false;
7306 /* Handle an "tls_model" attribute; arguments as in
7307 struct attribute_spec.handler. */
7309 static tree
7310 handle_tls_model_attribute (tree *node, tree name, tree args,
7311 int ARG_UNUSED (flags), bool *no_add_attrs)
7313 tree id;
7314 tree decl = *node;
7315 enum tls_model kind;
7317 *no_add_attrs = true;
7319 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7321 warning (OPT_Wattributes, "%qE attribute ignored", name);
7322 return NULL_TREE;
7325 kind = DECL_TLS_MODEL (decl);
7326 id = TREE_VALUE (args);
7327 if (TREE_CODE (id) != STRING_CST)
7329 error ("tls_model argument not a string");
7330 return NULL_TREE;
7333 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7334 kind = TLS_MODEL_LOCAL_EXEC;
7335 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7336 kind = TLS_MODEL_INITIAL_EXEC;
7337 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7338 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7339 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7340 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7341 else
7342 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7344 DECL_TLS_MODEL (decl) = kind;
7345 return NULL_TREE;
7348 /* Handle a "no_instrument_function" attribute; arguments as in
7349 struct attribute_spec.handler. */
7351 static tree
7352 handle_no_instrument_function_attribute (tree *node, tree name,
7353 tree ARG_UNUSED (args),
7354 int ARG_UNUSED (flags),
7355 bool *no_add_attrs)
7357 tree decl = *node;
7359 if (TREE_CODE (decl) != FUNCTION_DECL)
7361 error_at (DECL_SOURCE_LOCATION (decl),
7362 "%qE attribute applies only to functions", name);
7363 *no_add_attrs = true;
7365 else if (DECL_INITIAL (decl))
7367 error_at (DECL_SOURCE_LOCATION (decl),
7368 "can%'t set %qE attribute after definition", name);
7369 *no_add_attrs = true;
7371 else
7372 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7374 return NULL_TREE;
7377 /* Handle a "malloc" attribute; arguments as in
7378 struct attribute_spec.handler. */
7380 static tree
7381 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7382 int ARG_UNUSED (flags), bool *no_add_attrs)
7384 if (TREE_CODE (*node) == FUNCTION_DECL
7385 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7386 DECL_IS_MALLOC (*node) = 1;
7387 else
7389 warning (OPT_Wattributes, "%qE attribute ignored", name);
7390 *no_add_attrs = true;
7393 return NULL_TREE;
7396 /* Handle a "alloc_size" attribute; arguments as in
7397 struct attribute_spec.handler. */
7399 static tree
7400 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7401 int ARG_UNUSED (flags), bool *no_add_attrs)
7403 unsigned arg_count = type_num_arguments (*node);
7404 for (; args; args = TREE_CHAIN (args))
7406 tree position = TREE_VALUE (args);
7408 if (TREE_CODE (position) != INTEGER_CST
7409 || TREE_INT_CST_HIGH (position)
7410 || TREE_INT_CST_LOW (position) < 1
7411 || TREE_INT_CST_LOW (position) > arg_count )
7413 warning (OPT_Wattributes,
7414 "alloc_size parameter outside range");
7415 *no_add_attrs = true;
7416 return NULL_TREE;
7419 return NULL_TREE;
7422 /* Handle a "fn spec" attribute; arguments as in
7423 struct attribute_spec.handler. */
7425 static tree
7426 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7427 tree args, int ARG_UNUSED (flags),
7428 bool *no_add_attrs ATTRIBUTE_UNUSED)
7430 gcc_assert (args
7431 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7432 && !TREE_CHAIN (args));
7433 return NULL_TREE;
7436 /* Handle a "returns_twice" attribute; arguments as in
7437 struct attribute_spec.handler. */
7439 static tree
7440 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7441 int ARG_UNUSED (flags), bool *no_add_attrs)
7443 if (TREE_CODE (*node) == FUNCTION_DECL)
7444 DECL_IS_RETURNS_TWICE (*node) = 1;
7445 else
7447 warning (OPT_Wattributes, "%qE attribute ignored", name);
7448 *no_add_attrs = true;
7451 return NULL_TREE;
7454 /* Handle a "no_limit_stack" attribute; arguments as in
7455 struct attribute_spec.handler. */
7457 static tree
7458 handle_no_limit_stack_attribute (tree *node, tree name,
7459 tree ARG_UNUSED (args),
7460 int ARG_UNUSED (flags),
7461 bool *no_add_attrs)
7463 tree decl = *node;
7465 if (TREE_CODE (decl) != FUNCTION_DECL)
7467 error_at (DECL_SOURCE_LOCATION (decl),
7468 "%qE attribute applies only to functions", name);
7469 *no_add_attrs = true;
7471 else if (DECL_INITIAL (decl))
7473 error_at (DECL_SOURCE_LOCATION (decl),
7474 "can%'t set %qE attribute after definition", name);
7475 *no_add_attrs = true;
7477 else
7478 DECL_NO_LIMIT_STACK (decl) = 1;
7480 return NULL_TREE;
7483 /* Handle a "pure" attribute; arguments as in
7484 struct attribute_spec.handler. */
7486 static tree
7487 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7488 int ARG_UNUSED (flags), bool *no_add_attrs)
7490 if (TREE_CODE (*node) == FUNCTION_DECL)
7491 DECL_PURE_P (*node) = 1;
7492 /* ??? TODO: Support types. */
7493 else
7495 warning (OPT_Wattributes, "%qE attribute ignored", name);
7496 *no_add_attrs = true;
7499 return NULL_TREE;
7502 /* Digest an attribute list destined for a transactional memory statement.
7503 ALLOWED is the set of attributes that are allowed for this statement;
7504 return the attribute we parsed. Multiple attributes are never allowed. */
7507 parse_tm_stmt_attr (tree attrs, int allowed)
7509 tree a_seen = NULL;
7510 int m_seen = 0;
7512 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
7514 tree a = TREE_PURPOSE (attrs);
7515 int m = 0;
7517 if (is_attribute_p ("outer", a))
7518 m = TM_STMT_ATTR_OUTER;
7520 if ((m & allowed) == 0)
7522 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
7523 continue;
7526 if (m_seen == 0)
7528 a_seen = a;
7529 m_seen = m;
7531 else if (m_seen == m)
7532 warning (OPT_Wattributes, "%qE attribute duplicated", a);
7533 else
7534 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
7537 return m_seen;
7540 /* Transform a TM attribute name into a maskable integer and back.
7541 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
7542 to how the lack of an attribute is treated. */
7545 tm_attr_to_mask (tree attr)
7547 if (attr == NULL)
7548 return 0;
7549 if (is_attribute_p ("transaction_safe", attr))
7550 return TM_ATTR_SAFE;
7551 if (is_attribute_p ("transaction_callable", attr))
7552 return TM_ATTR_CALLABLE;
7553 if (is_attribute_p ("transaction_pure", attr))
7554 return TM_ATTR_PURE;
7555 if (is_attribute_p ("transaction_unsafe", attr))
7556 return TM_ATTR_IRREVOCABLE;
7557 if (is_attribute_p ("transaction_may_cancel_outer", attr))
7558 return TM_ATTR_MAY_CANCEL_OUTER;
7559 return 0;
7562 tree
7563 tm_mask_to_attr (int mask)
7565 const char *str;
7566 switch (mask)
7568 case TM_ATTR_SAFE:
7569 str = "transaction_safe";
7570 break;
7571 case TM_ATTR_CALLABLE:
7572 str = "transaction_callable";
7573 break;
7574 case TM_ATTR_PURE:
7575 str = "transaction_pure";
7576 break;
7577 case TM_ATTR_IRREVOCABLE:
7578 str = "transaction_unsafe";
7579 break;
7580 case TM_ATTR_MAY_CANCEL_OUTER:
7581 str = "transaction_may_cancel_outer";
7582 break;
7583 default:
7584 gcc_unreachable ();
7586 return get_identifier (str);
7589 /* Return the first TM attribute seen in LIST. */
7591 tree
7592 find_tm_attribute (tree list)
7594 for (; list ; list = TREE_CHAIN (list))
7596 tree name = TREE_PURPOSE (list);
7597 if (tm_attr_to_mask (name) != 0)
7598 return name;
7600 return NULL_TREE;
7603 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
7604 Here we accept only function types, and verify that none of the other
7605 function TM attributes are also applied. */
7606 /* ??? We need to accept class types for C++, but not C. This greatly
7607 complicates this function, since we can no longer rely on the extra
7608 processing given by function_type_required. */
7610 static tree
7611 handle_tm_attribute (tree *node, tree name, tree args,
7612 int flags, bool *no_add_attrs)
7614 /* Only one path adds the attribute; others don't. */
7615 *no_add_attrs = true;
7617 switch (TREE_CODE (*node))
7619 case RECORD_TYPE:
7620 case UNION_TYPE:
7621 /* Only tm_callable and tm_safe apply to classes. */
7622 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
7623 goto ignored;
7624 /* FALLTHRU */
7626 case FUNCTION_TYPE:
7627 case METHOD_TYPE:
7629 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
7630 if (old_name == name)
7632 else if (old_name != NULL_TREE)
7633 error ("type was previously declared %qE", old_name);
7634 else
7635 *no_add_attrs = false;
7637 break;
7639 case POINTER_TYPE:
7641 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
7642 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
7644 tree fn_tmp = TREE_TYPE (*node);
7645 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
7646 *node = build_pointer_type (fn_tmp);
7647 break;
7650 /* FALLTHRU */
7652 default:
7653 /* If a function is next, pass it on to be tried next. */
7654 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
7655 return tree_cons (name, args, NULL);
7657 ignored:
7658 warning (OPT_Wattributes, "%qE attribute ignored", name);
7659 break;
7662 return NULL_TREE;
7665 /* Handle the TM_WRAP attribute; arguments as in
7666 struct attribute_spec.handler. */
7668 static tree
7669 handle_tm_wrap_attribute (tree *node, tree name, tree args,
7670 int ARG_UNUSED (flags), bool *no_add_attrs)
7672 tree decl = *node;
7674 /* We don't need the attribute even on success, since we
7675 record the entry in an external table. */
7676 *no_add_attrs = true;
7678 if (TREE_CODE (decl) != FUNCTION_DECL)
7679 warning (OPT_Wattributes, "%qE attribute ignored", name);
7680 else
7682 tree wrap_decl = TREE_VALUE (args);
7683 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
7684 && TREE_CODE (wrap_decl) != VAR_DECL
7685 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
7686 error ("%qE argument not an identifier", name);
7687 else
7689 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
7690 wrap_decl = lookup_name (wrap_decl);
7691 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
7693 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
7694 TREE_TYPE (wrap_decl)))
7695 record_tm_replacement (wrap_decl, decl);
7696 else
7697 error ("%qD is not compatible with %qD", wrap_decl, decl);
7699 else
7700 error ("transaction_wrap argument is not a function");
7704 return NULL_TREE;
7707 /* Ignore the given attribute. Used when this attribute may be usefully
7708 overridden by the target, but is not used generically. */
7710 static tree
7711 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
7712 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7713 bool *no_add_attrs)
7715 *no_add_attrs = true;
7716 return NULL_TREE;
7719 /* Handle a "no vops" attribute; arguments as in
7720 struct attribute_spec.handler. */
7722 static tree
7723 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7724 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7725 bool *ARG_UNUSED (no_add_attrs))
7727 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7728 DECL_IS_NOVOPS (*node) = 1;
7729 return NULL_TREE;
7732 /* Handle a "deprecated" attribute; arguments as in
7733 struct attribute_spec.handler. */
7735 static tree
7736 handle_deprecated_attribute (tree *node, tree name,
7737 tree args, int flags,
7738 bool *no_add_attrs)
7740 tree type = NULL_TREE;
7741 int warn = 0;
7742 tree what = NULL_TREE;
7744 if (!args)
7745 *no_add_attrs = true;
7746 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7748 error ("deprecated message is not a string");
7749 *no_add_attrs = true;
7752 if (DECL_P (*node))
7754 tree decl = *node;
7755 type = TREE_TYPE (decl);
7757 if (TREE_CODE (decl) == TYPE_DECL
7758 || TREE_CODE (decl) == PARM_DECL
7759 || TREE_CODE (decl) == VAR_DECL
7760 || TREE_CODE (decl) == FUNCTION_DECL
7761 || TREE_CODE (decl) == FIELD_DECL
7762 || objc_method_decl (TREE_CODE (decl)))
7763 TREE_DEPRECATED (decl) = 1;
7764 else
7765 warn = 1;
7767 else if (TYPE_P (*node))
7769 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7770 *node = build_variant_type_copy (*node);
7771 TREE_DEPRECATED (*node) = 1;
7772 type = *node;
7774 else
7775 warn = 1;
7777 if (warn)
7779 *no_add_attrs = true;
7780 if (type && TYPE_NAME (type))
7782 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7783 what = TYPE_NAME (*node);
7784 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7785 && DECL_NAME (TYPE_NAME (type)))
7786 what = DECL_NAME (TYPE_NAME (type));
7788 if (what)
7789 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7790 else
7791 warning (OPT_Wattributes, "%qE attribute ignored", name);
7794 return NULL_TREE;
7797 /* Handle a "vector_size" attribute; arguments as in
7798 struct attribute_spec.handler. */
7800 static tree
7801 handle_vector_size_attribute (tree *node, tree name, tree args,
7802 int ARG_UNUSED (flags),
7803 bool *no_add_attrs)
7805 unsigned HOST_WIDE_INT vecsize, nunits;
7806 enum machine_mode orig_mode;
7807 tree type = *node, new_type, size;
7809 *no_add_attrs = true;
7811 size = TREE_VALUE (args);
7813 if (!host_integerp (size, 1))
7815 warning (OPT_Wattributes, "%qE attribute ignored", name);
7816 return NULL_TREE;
7819 /* Get the vector size (in bytes). */
7820 vecsize = tree_low_cst (size, 1);
7822 /* We need to provide for vector pointers, vector arrays, and
7823 functions returning vectors. For example:
7825 __attribute__((vector_size(16))) short *foo;
7827 In this case, the mode is SI, but the type being modified is
7828 HI, so we need to look further. */
7830 while (POINTER_TYPE_P (type)
7831 || TREE_CODE (type) == FUNCTION_TYPE
7832 || TREE_CODE (type) == METHOD_TYPE
7833 || TREE_CODE (type) == ARRAY_TYPE
7834 || TREE_CODE (type) == OFFSET_TYPE)
7835 type = TREE_TYPE (type);
7837 /* Get the mode of the type being modified. */
7838 orig_mode = TYPE_MODE (type);
7840 if ((!INTEGRAL_TYPE_P (type)
7841 && !SCALAR_FLOAT_TYPE_P (type)
7842 && !FIXED_POINT_TYPE_P (type))
7843 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7844 && GET_MODE_CLASS (orig_mode) != MODE_INT
7845 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7846 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7847 || TREE_CODE (type) == BOOLEAN_TYPE)
7849 error ("invalid vector type for attribute %qE", name);
7850 return NULL_TREE;
7853 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7855 error ("vector size not an integral multiple of component size");
7856 return NULL;
7859 if (vecsize == 0)
7861 error ("zero vector size");
7862 return NULL;
7865 /* Calculate how many units fit in the vector. */
7866 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7867 if (nunits & (nunits - 1))
7869 error ("number of components of the vector not a power of two");
7870 return NULL_TREE;
7873 new_type = build_vector_type (type, nunits);
7875 /* Build back pointers if needed. */
7876 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7878 return NULL_TREE;
7881 /* Handle the "nonnull" attribute. */
7882 static tree
7883 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7884 tree args, int ARG_UNUSED (flags),
7885 bool *no_add_attrs)
7887 tree type = *node;
7888 unsigned HOST_WIDE_INT attr_arg_num;
7890 /* If no arguments are specified, all pointer arguments should be
7891 non-null. Verify a full prototype is given so that the arguments
7892 will have the correct types when we actually check them later. */
7893 if (!args)
7895 if (!prototype_p (type))
7897 error ("nonnull attribute without arguments on a non-prototype");
7898 *no_add_attrs = true;
7900 return NULL_TREE;
7903 /* Argument list specified. Verify that each argument number references
7904 a pointer argument. */
7905 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7907 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7909 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7911 error ("nonnull argument has invalid operand number (argument %lu)",
7912 (unsigned long) attr_arg_num);
7913 *no_add_attrs = true;
7914 return NULL_TREE;
7917 if (prototype_p (type))
7919 function_args_iterator iter;
7920 tree argument;
7922 function_args_iter_init (&iter, type);
7923 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
7925 argument = function_args_iter_cond (&iter);
7926 if (argument == NULL_TREE || ck_num == arg_num)
7927 break;
7930 if (!argument
7931 || TREE_CODE (argument) == VOID_TYPE)
7933 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7934 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7935 *no_add_attrs = true;
7936 return NULL_TREE;
7939 if (TREE_CODE (argument) != POINTER_TYPE)
7941 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7942 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7943 *no_add_attrs = true;
7944 return NULL_TREE;
7949 return NULL_TREE;
7952 /* Check the argument list of a function call for null in argument slots
7953 that are marked as requiring a non-null pointer argument. The NARGS
7954 arguments are passed in the array ARGARRAY.
7957 static void
7958 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7960 tree a, args;
7961 int i;
7963 for (a = attrs; a; a = TREE_CHAIN (a))
7965 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7967 args = TREE_VALUE (a);
7969 /* Walk the argument list. If we encounter an argument number we
7970 should check for non-null, do it. If the attribute has no args,
7971 then every pointer argument is checked (in which case the check
7972 for pointer type is done in check_nonnull_arg). */
7973 for (i = 0; i < nargs; i++)
7975 if (!args || nonnull_check_p (args, i + 1))
7976 check_function_arguments_recurse (check_nonnull_arg, NULL,
7977 argarray[i],
7978 i + 1);
7984 /* Check that the Nth argument of a function call (counting backwards
7985 from the end) is a (pointer)0. The NARGS arguments are passed in the
7986 array ARGARRAY. */
7988 static void
7989 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
7991 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
7993 if (attr)
7995 int len = 0;
7996 int pos = 0;
7997 tree sentinel;
7998 function_args_iterator iter;
7999 tree t;
8001 /* Skip over the named arguments. */
8002 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8004 if (len == nargs)
8005 break;
8006 len++;
8009 if (TREE_VALUE (attr))
8011 tree p = TREE_VALUE (TREE_VALUE (attr));
8012 pos = TREE_INT_CST_LOW (p);
8015 /* The sentinel must be one of the varargs, i.e.
8016 in position >= the number of fixed arguments. */
8017 if ((nargs - 1 - pos) < len)
8019 warning (OPT_Wformat,
8020 "not enough variable arguments to fit a sentinel");
8021 return;
8024 /* Validate the sentinel. */
8025 sentinel = argarray[nargs - 1 - pos];
8026 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8027 || !integer_zerop (sentinel))
8028 /* Although __null (in C++) is only an integer we allow it
8029 nevertheless, as we are guaranteed that it's exactly
8030 as wide as a pointer, and we don't want to force
8031 users to cast the NULL they have written there.
8032 We warn with -Wstrict-null-sentinel, though. */
8033 && (warn_strict_null_sentinel || null_node != sentinel))
8034 warning (OPT_Wformat, "missing sentinel in function call");
8038 /* Helper for check_function_nonnull; given a list of operands which
8039 must be non-null in ARGS, determine if operand PARAM_NUM should be
8040 checked. */
8042 static bool
8043 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8045 unsigned HOST_WIDE_INT arg_num = 0;
8047 for (; args; args = TREE_CHAIN (args))
8049 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8051 gcc_assert (found);
8053 if (arg_num == param_num)
8054 return true;
8056 return false;
8059 /* Check that the function argument PARAM (which is operand number
8060 PARAM_NUM) is non-null. This is called by check_function_nonnull
8061 via check_function_arguments_recurse. */
8063 static void
8064 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8065 unsigned HOST_WIDE_INT param_num)
8067 /* Just skip checking the argument if it's not a pointer. This can
8068 happen if the "nonnull" attribute was given without an operand
8069 list (which means to check every pointer argument). */
8071 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8072 return;
8074 if (integer_zerop (param))
8075 warning (OPT_Wnonnull, "null argument where non-null required "
8076 "(argument %lu)", (unsigned long) param_num);
8079 /* Helper for nonnull attribute handling; fetch the operand number
8080 from the attribute argument list. */
8082 static bool
8083 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8085 /* Verify the arg number is a constant. */
8086 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8087 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8088 return false;
8090 *valp = TREE_INT_CST_LOW (arg_num_expr);
8091 return true;
8094 /* Handle a "nothrow" attribute; arguments as in
8095 struct attribute_spec.handler. */
8097 static tree
8098 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8099 int ARG_UNUSED (flags), bool *no_add_attrs)
8101 if (TREE_CODE (*node) == FUNCTION_DECL)
8102 TREE_NOTHROW (*node) = 1;
8103 /* ??? TODO: Support types. */
8104 else
8106 warning (OPT_Wattributes, "%qE attribute ignored", name);
8107 *no_add_attrs = true;
8110 return NULL_TREE;
8113 /* Handle a "cleanup" attribute; arguments as in
8114 struct attribute_spec.handler. */
8116 static tree
8117 handle_cleanup_attribute (tree *node, tree name, tree args,
8118 int ARG_UNUSED (flags), bool *no_add_attrs)
8120 tree decl = *node;
8121 tree cleanup_id, cleanup_decl;
8123 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8124 for global destructors in C++. This requires infrastructure that
8125 we don't have generically at the moment. It's also not a feature
8126 we'd be missing too much, since we do have attribute constructor. */
8127 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8129 warning (OPT_Wattributes, "%qE attribute ignored", name);
8130 *no_add_attrs = true;
8131 return NULL_TREE;
8134 /* Verify that the argument is a function in scope. */
8135 /* ??? We could support pointers to functions here as well, if
8136 that was considered desirable. */
8137 cleanup_id = TREE_VALUE (args);
8138 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8140 error ("cleanup argument not an identifier");
8141 *no_add_attrs = true;
8142 return NULL_TREE;
8144 cleanup_decl = lookup_name (cleanup_id);
8145 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8147 error ("cleanup argument not a function");
8148 *no_add_attrs = true;
8149 return NULL_TREE;
8152 /* That the function has proper type is checked with the
8153 eventual call to build_function_call. */
8155 return NULL_TREE;
8158 /* Handle a "warn_unused_result" attribute. No special handling. */
8160 static tree
8161 handle_warn_unused_result_attribute (tree *node, tree name,
8162 tree ARG_UNUSED (args),
8163 int ARG_UNUSED (flags), bool *no_add_attrs)
8165 /* Ignore the attribute for functions not returning any value. */
8166 if (VOID_TYPE_P (TREE_TYPE (*node)))
8168 warning (OPT_Wattributes, "%qE attribute ignored", name);
8169 *no_add_attrs = true;
8172 return NULL_TREE;
8175 /* Handle a "sentinel" attribute. */
8177 static tree
8178 handle_sentinel_attribute (tree *node, tree name, tree args,
8179 int ARG_UNUSED (flags), bool *no_add_attrs)
8181 if (!prototype_p (*node))
8183 warning (OPT_Wattributes,
8184 "%qE attribute requires prototypes with named arguments", name);
8185 *no_add_attrs = true;
8187 else
8189 if (!stdarg_p (*node))
8191 warning (OPT_Wattributes,
8192 "%qE attribute only applies to variadic functions", name);
8193 *no_add_attrs = true;
8197 if (args)
8199 tree position = TREE_VALUE (args);
8201 if (TREE_CODE (position) != INTEGER_CST)
8203 warning (OPT_Wattributes,
8204 "requested position is not an integer constant");
8205 *no_add_attrs = true;
8207 else
8209 if (tree_int_cst_lt (position, integer_zero_node))
8211 warning (OPT_Wattributes,
8212 "requested position is less than zero");
8213 *no_add_attrs = true;
8218 return NULL_TREE;
8221 /* Handle a "type_generic" attribute. */
8223 static tree
8224 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8225 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8226 bool * ARG_UNUSED (no_add_attrs))
8228 /* Ensure we have a function type. */
8229 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8231 /* Ensure we have a variadic function. */
8232 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8234 return NULL_TREE;
8237 /* Handle a "target" attribute. */
8239 static tree
8240 handle_target_attribute (tree *node, tree name, tree args, int flags,
8241 bool *no_add_attrs)
8243 /* Ensure we have a function type. */
8244 if (TREE_CODE (*node) != FUNCTION_DECL)
8246 warning (OPT_Wattributes, "%qE attribute ignored", name);
8247 *no_add_attrs = true;
8249 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8250 flags))
8251 *no_add_attrs = true;
8253 return NULL_TREE;
8256 /* Arguments being collected for optimization. */
8257 typedef const char *const_char_p; /* For DEF_VEC_P. */
8258 DEF_VEC_P(const_char_p);
8259 DEF_VEC_ALLOC_P(const_char_p, gc);
8260 static GTY(()) VEC(const_char_p, gc) *optimize_args;
8263 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8264 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8265 false for #pragma GCC optimize. */
8267 bool
8268 parse_optimize_options (tree args, bool attr_p)
8270 bool ret = true;
8271 unsigned opt_argc;
8272 unsigned i;
8273 int saved_flag_strict_aliasing;
8274 const char **opt_argv;
8275 struct cl_decoded_option *decoded_options;
8276 unsigned int decoded_options_count;
8277 tree ap;
8279 /* Build up argv vector. Just in case the string is stored away, use garbage
8280 collected strings. */
8281 VEC_truncate (const_char_p, optimize_args, 0);
8282 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
8284 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8286 tree value = TREE_VALUE (ap);
8288 if (TREE_CODE (value) == INTEGER_CST)
8290 char buffer[20];
8291 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8292 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
8295 else if (TREE_CODE (value) == STRING_CST)
8297 /* Split string into multiple substrings. */
8298 size_t len = TREE_STRING_LENGTH (value);
8299 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8300 char *end = p + len;
8301 char *comma;
8302 char *next_p = p;
8304 while (next_p != NULL)
8306 size_t len2;
8307 char *q, *r;
8309 p = next_p;
8310 comma = strchr (p, ',');
8311 if (comma)
8313 len2 = comma - p;
8314 *comma = '\0';
8315 next_p = comma+1;
8317 else
8319 len2 = end - p;
8320 next_p = NULL;
8323 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8325 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8326 options. */
8327 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8329 ret = false;
8330 if (attr_p)
8331 warning (OPT_Wattributes,
8332 "bad option %s to optimize attribute", p);
8333 else
8334 warning (OPT_Wpragmas,
8335 "bad option %s to pragma attribute", p);
8336 continue;
8339 if (*p != '-')
8341 *r++ = '-';
8343 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8344 itself is -Os, and any other switch begins with a -f. */
8345 if ((*p >= '0' && *p <= '9')
8346 || (p[0] == 's' && p[1] == '\0'))
8347 *r++ = 'O';
8348 else if (*p != 'O')
8349 *r++ = 'f';
8352 memcpy (r, p, len2);
8353 r[len2] = '\0';
8354 VEC_safe_push (const_char_p, gc, optimize_args, q);
8360 opt_argc = VEC_length (const_char_p, optimize_args);
8361 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8363 for (i = 1; i < opt_argc; i++)
8364 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
8366 saved_flag_strict_aliasing = flag_strict_aliasing;
8368 /* Now parse the options. */
8369 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8370 &decoded_options,
8371 &decoded_options_count);
8372 decode_options (&global_options, &global_options_set,
8373 decoded_options, decoded_options_count,
8374 input_location, global_dc);
8376 targetm.override_options_after_change();
8378 /* Don't allow changing -fstrict-aliasing. */
8379 flag_strict_aliasing = saved_flag_strict_aliasing;
8381 VEC_truncate (const_char_p, optimize_args, 0);
8382 return ret;
8385 /* For handling "optimize" attribute. arguments as in
8386 struct attribute_spec.handler. */
8388 static tree
8389 handle_optimize_attribute (tree *node, tree name, tree args,
8390 int ARG_UNUSED (flags), bool *no_add_attrs)
8392 /* Ensure we have a function type. */
8393 if (TREE_CODE (*node) != FUNCTION_DECL)
8395 warning (OPT_Wattributes, "%qE attribute ignored", name);
8396 *no_add_attrs = true;
8398 else
8400 struct cl_optimization cur_opts;
8401 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8403 /* Save current options. */
8404 cl_optimization_save (&cur_opts, &global_options);
8406 /* If we previously had some optimization options, use them as the
8407 default. */
8408 if (old_opts)
8409 cl_optimization_restore (&global_options,
8410 TREE_OPTIMIZATION (old_opts));
8412 /* Parse options, and update the vector. */
8413 parse_optimize_options (args, true);
8414 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8415 = build_optimization_node ();
8417 /* Restore current options. */
8418 cl_optimization_restore (&global_options, &cur_opts);
8421 return NULL_TREE;
8424 /* Handle a "no_split_stack" attribute. */
8426 static tree
8427 handle_no_split_stack_attribute (tree *node, tree name,
8428 tree ARG_UNUSED (args),
8429 int ARG_UNUSED (flags),
8430 bool *no_add_attrs)
8432 tree decl = *node;
8434 if (TREE_CODE (decl) != FUNCTION_DECL)
8436 error_at (DECL_SOURCE_LOCATION (decl),
8437 "%qE attribute applies only to functions", name);
8438 *no_add_attrs = true;
8440 else if (DECL_INITIAL (decl))
8442 error_at (DECL_SOURCE_LOCATION (decl),
8443 "can%'t set %qE attribute after definition", name);
8444 *no_add_attrs = true;
8447 return NULL_TREE;
8450 /* Check for valid arguments being passed to a function with FNTYPE.
8451 There are NARGS arguments in the array ARGARRAY. */
8452 void
8453 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8455 /* Check for null being passed in a pointer argument that must be
8456 non-null. We also need to do this if format checking is enabled. */
8458 if (warn_nonnull)
8459 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8461 /* Check for errors in format strings. */
8463 if (warn_format || warn_suggest_attribute_format)
8464 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8466 if (warn_format)
8467 check_function_sentinel (fntype, nargs, argarray);
8470 /* Generic argument checking recursion routine. PARAM is the argument to
8471 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8472 once the argument is resolved. CTX is context for the callback. */
8473 void
8474 check_function_arguments_recurse (void (*callback)
8475 (void *, tree, unsigned HOST_WIDE_INT),
8476 void *ctx, tree param,
8477 unsigned HOST_WIDE_INT param_num)
8479 if (CONVERT_EXPR_P (param)
8480 && (TYPE_PRECISION (TREE_TYPE (param))
8481 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8483 /* Strip coercion. */
8484 check_function_arguments_recurse (callback, ctx,
8485 TREE_OPERAND (param, 0), param_num);
8486 return;
8489 if (TREE_CODE (param) == CALL_EXPR)
8491 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8492 tree attrs;
8493 bool found_format_arg = false;
8495 /* See if this is a call to a known internationalization function
8496 that modifies a format arg. Such a function may have multiple
8497 format_arg attributes (for example, ngettext). */
8499 for (attrs = TYPE_ATTRIBUTES (type);
8500 attrs;
8501 attrs = TREE_CHAIN (attrs))
8502 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8504 tree inner_arg;
8505 tree format_num_expr;
8506 int format_num;
8507 int i;
8508 call_expr_arg_iterator iter;
8510 /* Extract the argument number, which was previously checked
8511 to be valid. */
8512 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8514 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8515 && !TREE_INT_CST_HIGH (format_num_expr));
8517 format_num = TREE_INT_CST_LOW (format_num_expr);
8519 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8520 inner_arg != 0;
8521 inner_arg = next_call_expr_arg (&iter), i++)
8522 if (i == format_num)
8524 check_function_arguments_recurse (callback, ctx,
8525 inner_arg, param_num);
8526 found_format_arg = true;
8527 break;
8531 /* If we found a format_arg attribute and did a recursive check,
8532 we are done with checking this argument. Otherwise, we continue
8533 and this will be considered a non-literal. */
8534 if (found_format_arg)
8535 return;
8538 if (TREE_CODE (param) == COND_EXPR)
8540 /* Check both halves of the conditional expression. */
8541 check_function_arguments_recurse (callback, ctx,
8542 TREE_OPERAND (param, 1), param_num);
8543 check_function_arguments_recurse (callback, ctx,
8544 TREE_OPERAND (param, 2), param_num);
8545 return;
8548 (*callback) (ctx, param, param_num);
8551 /* Checks for a builtin function FNDECL that the number of arguments
8552 NARGS against the required number REQUIRED and issues an error if
8553 there is a mismatch. Returns true if the number of arguments is
8554 correct, otherwise false. */
8556 static bool
8557 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8559 if (nargs < required)
8561 error_at (input_location,
8562 "not enough arguments to function %qE", fndecl);
8563 return false;
8565 else if (nargs > required)
8567 error_at (input_location,
8568 "too many arguments to function %qE", fndecl);
8569 return false;
8571 return true;
8574 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8575 Returns false if there was an error, otherwise true. */
8577 bool
8578 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8580 if (!DECL_BUILT_IN (fndecl)
8581 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8582 return true;
8584 switch (DECL_FUNCTION_CODE (fndecl))
8586 case BUILT_IN_CONSTANT_P:
8587 return builtin_function_validate_nargs (fndecl, nargs, 1);
8589 case BUILT_IN_ISFINITE:
8590 case BUILT_IN_ISINF:
8591 case BUILT_IN_ISINF_SIGN:
8592 case BUILT_IN_ISNAN:
8593 case BUILT_IN_ISNORMAL:
8594 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8596 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8598 error ("non-floating-point argument in call to "
8599 "function %qE", fndecl);
8600 return false;
8602 return true;
8604 return false;
8606 case BUILT_IN_ISGREATER:
8607 case BUILT_IN_ISGREATEREQUAL:
8608 case BUILT_IN_ISLESS:
8609 case BUILT_IN_ISLESSEQUAL:
8610 case BUILT_IN_ISLESSGREATER:
8611 case BUILT_IN_ISUNORDERED:
8612 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8614 enum tree_code code0, code1;
8615 code0 = TREE_CODE (TREE_TYPE (args[0]));
8616 code1 = TREE_CODE (TREE_TYPE (args[1]));
8617 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8618 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8619 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8621 error ("non-floating-point arguments in call to "
8622 "function %qE", fndecl);
8623 return false;
8625 return true;
8627 return false;
8629 case BUILT_IN_FPCLASSIFY:
8630 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8632 unsigned i;
8634 for (i=0; i<5; i++)
8635 if (TREE_CODE (args[i]) != INTEGER_CST)
8637 error ("non-const integer argument %u in call to function %qE",
8638 i+1, fndecl);
8639 return false;
8642 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8644 error ("non-floating-point argument in call to function %qE",
8645 fndecl);
8646 return false;
8648 return true;
8650 return false;
8652 case BUILT_IN_ASSUME_ALIGNED:
8653 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8655 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8657 error ("non-integer argument 3 in call to function %qE", fndecl);
8658 return false;
8660 return true;
8662 return false;
8664 default:
8665 return true;
8669 /* Function to help qsort sort FIELD_DECLs by name order. */
8672 field_decl_cmp (const void *x_p, const void *y_p)
8674 const tree *const x = (const tree *const) x_p;
8675 const tree *const y = (const tree *const) y_p;
8677 if (DECL_NAME (*x) == DECL_NAME (*y))
8678 /* A nontype is "greater" than a type. */
8679 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8680 if (DECL_NAME (*x) == NULL_TREE)
8681 return -1;
8682 if (DECL_NAME (*y) == NULL_TREE)
8683 return 1;
8684 if (DECL_NAME (*x) < DECL_NAME (*y))
8685 return -1;
8686 return 1;
8689 static struct {
8690 gt_pointer_operator new_value;
8691 void *cookie;
8692 } resort_data;
8694 /* This routine compares two fields like field_decl_cmp but using the
8695 pointer operator in resort_data. */
8697 static int
8698 resort_field_decl_cmp (const void *x_p, const void *y_p)
8700 const tree *const x = (const tree *const) x_p;
8701 const tree *const y = (const tree *const) y_p;
8703 if (DECL_NAME (*x) == DECL_NAME (*y))
8704 /* A nontype is "greater" than a type. */
8705 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8706 if (DECL_NAME (*x) == NULL_TREE)
8707 return -1;
8708 if (DECL_NAME (*y) == NULL_TREE)
8709 return 1;
8711 tree d1 = DECL_NAME (*x);
8712 tree d2 = DECL_NAME (*y);
8713 resort_data.new_value (&d1, resort_data.cookie);
8714 resort_data.new_value (&d2, resort_data.cookie);
8715 if (d1 < d2)
8716 return -1;
8718 return 1;
8721 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8723 void
8724 resort_sorted_fields (void *obj,
8725 void * ARG_UNUSED (orig_obj),
8726 gt_pointer_operator new_value,
8727 void *cookie)
8729 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8730 resort_data.new_value = new_value;
8731 resort_data.cookie = cookie;
8732 qsort (&sf->elts[0], sf->len, sizeof (tree),
8733 resort_field_decl_cmp);
8736 /* Subroutine of c_parse_error.
8737 Return the result of concatenating LHS and RHS. RHS is really
8738 a string literal, its first character is indicated by RHS_START and
8739 RHS_SIZE is its length (including the terminating NUL character).
8741 The caller is responsible for deleting the returned pointer. */
8743 static char *
8744 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8746 const int lhs_size = strlen (lhs);
8747 char *result = XNEWVEC (char, lhs_size + rhs_size);
8748 strncpy (result, lhs, lhs_size);
8749 strncpy (result + lhs_size, rhs_start, rhs_size);
8750 return result;
8753 /* Issue the error given by GMSGID, indicating that it occurred before
8754 TOKEN, which had the associated VALUE. */
8756 void
8757 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8758 tree value, unsigned char token_flags)
8760 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8762 char *message = NULL;
8764 if (token_type == CPP_EOF)
8765 message = catenate_messages (gmsgid, " at end of input");
8766 else if (token_type == CPP_CHAR
8767 || token_type == CPP_WCHAR
8768 || token_type == CPP_CHAR16
8769 || token_type == CPP_CHAR32)
8771 unsigned int val = TREE_INT_CST_LOW (value);
8772 const char *prefix;
8774 switch (token_type)
8776 default:
8777 prefix = "";
8778 break;
8779 case CPP_WCHAR:
8780 prefix = "L";
8781 break;
8782 case CPP_CHAR16:
8783 prefix = "u";
8784 break;
8785 case CPP_CHAR32:
8786 prefix = "U";
8787 break;
8790 if (val <= UCHAR_MAX && ISGRAPH (val))
8791 message = catenate_messages (gmsgid, " before %s'%c'");
8792 else
8793 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8795 error (message, prefix, val);
8796 free (message);
8797 message = NULL;
8799 else if (token_type == CPP_STRING
8800 || token_type == CPP_WSTRING
8801 || token_type == CPP_STRING16
8802 || token_type == CPP_STRING32
8803 || token_type == CPP_UTF8STRING)
8804 message = catenate_messages (gmsgid, " before string constant");
8805 else if (token_type == CPP_NUMBER)
8806 message = catenate_messages (gmsgid, " before numeric constant");
8807 else if (token_type == CPP_NAME)
8809 message = catenate_messages (gmsgid, " before %qE");
8810 error (message, value);
8811 free (message);
8812 message = NULL;
8814 else if (token_type == CPP_PRAGMA)
8815 message = catenate_messages (gmsgid, " before %<#pragma%>");
8816 else if (token_type == CPP_PRAGMA_EOL)
8817 message = catenate_messages (gmsgid, " before end of line");
8818 else if (token_type == CPP_DECLTYPE)
8819 message = catenate_messages (gmsgid, " before %<decltype%>");
8820 else if (token_type < N_TTYPES)
8822 message = catenate_messages (gmsgid, " before %qs token");
8823 error (message, cpp_type2name (token_type, token_flags));
8824 free (message);
8825 message = NULL;
8827 else
8828 error (gmsgid);
8830 if (message)
8832 error (message);
8833 free (message);
8835 #undef catenate_messages
8838 /* Mapping for cpp message reasons to the options that enable them. */
8840 struct reason_option_codes_t
8842 const int reason; /* cpplib message reason. */
8843 const int option_code; /* gcc option that controls this message. */
8846 static const struct reason_option_codes_t option_codes[] = {
8847 {CPP_W_DEPRECATED, OPT_Wdeprecated},
8848 {CPP_W_COMMENTS, OPT_Wcomment},
8849 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
8850 {CPP_W_MULTICHAR, OPT_Wmultichar},
8851 {CPP_W_TRADITIONAL, OPT_Wtraditional},
8852 {CPP_W_LONG_LONG, OPT_Wlong_long},
8853 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
8854 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
8855 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
8856 {CPP_W_UNDEF, OPT_Wundef},
8857 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
8858 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
8859 {CPP_W_NORMALIZE, OPT_Wnormalized_},
8860 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
8861 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
8862 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
8863 {CPP_W_NONE, 0}
8866 /* Return the gcc option code associated with the reason for a cpp
8867 message, or 0 if none. */
8869 static int
8870 c_option_controlling_cpp_error (int reason)
8872 const struct reason_option_codes_t *entry;
8874 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8876 if (entry->reason == reason)
8877 return entry->option_code;
8879 return 0;
8882 /* Callback from cpp_error for PFILE to print diagnostics from the
8883 preprocessor. The diagnostic is of type LEVEL, with REASON set
8884 to the reason code if LEVEL is represents a warning, at location
8885 LOCATION unless this is after lexing and the compiler's location
8886 should be used instead, with column number possibly overridden by
8887 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8888 the arguments. Returns true if a diagnostic was emitted, false
8889 otherwise. */
8891 bool
8892 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8893 location_t location, unsigned int column_override,
8894 const char *msg, va_list *ap)
8896 diagnostic_info diagnostic;
8897 diagnostic_t dlevel;
8898 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8899 bool ret;
8901 switch (level)
8903 case CPP_DL_WARNING_SYSHDR:
8904 if (flag_no_output)
8905 return false;
8906 global_dc->dc_warn_system_headers = 1;
8907 /* Fall through. */
8908 case CPP_DL_WARNING:
8909 if (flag_no_output)
8910 return false;
8911 dlevel = DK_WARNING;
8912 break;
8913 case CPP_DL_PEDWARN:
8914 if (flag_no_output && !flag_pedantic_errors)
8915 return false;
8916 dlevel = DK_PEDWARN;
8917 break;
8918 case CPP_DL_ERROR:
8919 dlevel = DK_ERROR;
8920 break;
8921 case CPP_DL_ICE:
8922 dlevel = DK_ICE;
8923 break;
8924 case CPP_DL_NOTE:
8925 dlevel = DK_NOTE;
8926 break;
8927 case CPP_DL_FATAL:
8928 dlevel = DK_FATAL;
8929 break;
8930 default:
8931 gcc_unreachable ();
8933 if (done_lexing)
8934 location = input_location;
8935 diagnostic_set_info_translated (&diagnostic, msg, ap,
8936 location, dlevel);
8937 if (column_override)
8938 diagnostic_override_column (&diagnostic, column_override);
8939 diagnostic_override_option_index (&diagnostic,
8940 c_option_controlling_cpp_error (reason));
8941 ret = report_diagnostic (&diagnostic);
8942 if (level == CPP_DL_WARNING_SYSHDR)
8943 global_dc->dc_warn_system_headers = save_warn_system_headers;
8944 return ret;
8947 /* Convert a character from the host to the target execution character
8948 set. cpplib handles this, mostly. */
8950 HOST_WIDE_INT
8951 c_common_to_target_charset (HOST_WIDE_INT c)
8953 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8954 zero-extended under -fno-signed-char. cpplib insists that characters
8955 and character constants are always unsigned. Hence we must convert
8956 back and forth. */
8957 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8959 uc = cpp_host_to_exec_charset (parse_in, uc);
8961 if (flag_signed_char)
8962 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8963 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8964 else
8965 return uc;
8968 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
8969 references with an INDIRECT_REF of a constant at the bottom; much like the
8970 traditional rendering of offsetof as a macro. Return the folded result. */
8972 tree
8973 fold_offsetof_1 (tree expr)
8975 tree base, off, t;
8977 switch (TREE_CODE (expr))
8979 case ERROR_MARK:
8980 return expr;
8982 case VAR_DECL:
8983 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8984 return error_mark_node;
8986 case CALL_EXPR:
8987 case TARGET_EXPR:
8988 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8989 return error_mark_node;
8991 case NOP_EXPR:
8992 case INDIRECT_REF:
8993 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
8995 error ("cannot apply %<offsetof%> to a non constant address");
8996 return error_mark_node;
8998 return TREE_OPERAND (expr, 0);
9000 case COMPONENT_REF:
9001 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9002 if (base == error_mark_node)
9003 return base;
9005 t = TREE_OPERAND (expr, 1);
9006 if (DECL_C_BIT_FIELD (t))
9008 error ("attempt to take address of bit-field structure "
9009 "member %qD", t);
9010 return error_mark_node;
9012 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9013 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9015 / BITS_PER_UNIT));
9016 break;
9018 case ARRAY_REF:
9019 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9020 if (base == error_mark_node)
9021 return base;
9023 t = TREE_OPERAND (expr, 1);
9025 /* Check if the offset goes beyond the upper bound of the array. */
9026 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9028 tree upbound = array_ref_up_bound (expr);
9029 if (upbound != NULL_TREE
9030 && TREE_CODE (upbound) == INTEGER_CST
9031 && !tree_int_cst_equal (upbound,
9032 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9034 upbound = size_binop (PLUS_EXPR, upbound,
9035 build_int_cst (TREE_TYPE (upbound), 1));
9036 if (tree_int_cst_lt (upbound, t))
9038 tree v;
9040 for (v = TREE_OPERAND (expr, 0);
9041 TREE_CODE (v) == COMPONENT_REF;
9042 v = TREE_OPERAND (v, 0))
9043 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9044 == RECORD_TYPE)
9046 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9047 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9048 if (TREE_CODE (fld_chain) == FIELD_DECL)
9049 break;
9051 if (fld_chain)
9052 break;
9054 /* Don't warn if the array might be considered a poor
9055 man's flexible array member with a very permissive
9056 definition thereof. */
9057 if (TREE_CODE (v) == ARRAY_REF
9058 || TREE_CODE (v) == COMPONENT_REF)
9059 warning (OPT_Warray_bounds,
9060 "index %E denotes an offset "
9061 "greater than size of %qT",
9062 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9067 t = convert (sizetype, t);
9068 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9069 break;
9071 case COMPOUND_EXPR:
9072 /* Handle static members of volatile structs. */
9073 t = TREE_OPERAND (expr, 1);
9074 gcc_assert (TREE_CODE (t) == VAR_DECL);
9075 return fold_offsetof_1 (t);
9077 default:
9078 gcc_unreachable ();
9081 return fold_build_pointer_plus (base, off);
9084 /* Likewise, but convert it to the return type of offsetof. */
9086 tree
9087 fold_offsetof (tree expr)
9089 return convert (size_type_node, fold_offsetof_1 (expr));
9092 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9093 expression, because B will always be true. */
9095 void
9096 warn_for_omitted_condop (location_t location, tree cond)
9098 if (truth_value_p (TREE_CODE (cond)))
9099 warning_at (location, OPT_Wparentheses,
9100 "the omitted middle operand in ?: will always be %<true%>, "
9101 "suggest explicit middle operand");
9104 /* Give an error for storing into ARG, which is 'const'. USE indicates
9105 how ARG was being used. */
9107 void
9108 readonly_error (tree arg, enum lvalue_use use)
9110 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9111 || use == lv_asm);
9112 /* Using this macro rather than (for example) arrays of messages
9113 ensures that all the format strings are checked at compile
9114 time. */
9115 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9116 : (use == lv_increment ? (I) \
9117 : (use == lv_decrement ? (D) : (AS))))
9118 if (TREE_CODE (arg) == COMPONENT_REF)
9120 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9121 error (READONLY_MSG (G_("assignment of member "
9122 "%qD in read-only object"),
9123 G_("increment of member "
9124 "%qD in read-only object"),
9125 G_("decrement of member "
9126 "%qD in read-only object"),
9127 G_("member %qD in read-only object "
9128 "used as %<asm%> output")),
9129 TREE_OPERAND (arg, 1));
9130 else
9131 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9132 G_("increment of read-only member %qD"),
9133 G_("decrement of read-only member %qD"),
9134 G_("read-only member %qD used as %<asm%> output")),
9135 TREE_OPERAND (arg, 1));
9137 else if (TREE_CODE (arg) == VAR_DECL)
9138 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9139 G_("increment of read-only variable %qD"),
9140 G_("decrement of read-only variable %qD"),
9141 G_("read-only variable %qD used as %<asm%> output")),
9142 arg);
9143 else if (TREE_CODE (arg) == PARM_DECL)
9144 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9145 G_("increment of read-only parameter %qD"),
9146 G_("decrement of read-only parameter %qD"),
9147 G_("read-only parameter %qD use as %<asm%> output")),
9148 arg);
9149 else if (TREE_CODE (arg) == RESULT_DECL)
9151 gcc_assert (c_dialect_cxx ());
9152 error (READONLY_MSG (G_("assignment of "
9153 "read-only named return value %qD"),
9154 G_("increment of "
9155 "read-only named return value %qD"),
9156 G_("decrement of "
9157 "read-only named return value %qD"),
9158 G_("read-only named return value %qD "
9159 "used as %<asm%>output")),
9160 arg);
9162 else if (TREE_CODE (arg) == FUNCTION_DECL)
9163 error (READONLY_MSG (G_("assignment of function %qD"),
9164 G_("increment of function %qD"),
9165 G_("decrement of function %qD"),
9166 G_("function %qD used as %<asm%> output")),
9167 arg);
9168 else
9169 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9170 G_("increment of read-only location %qE"),
9171 G_("decrement of read-only location %qE"),
9172 G_("read-only location %qE used as %<asm%> output")),
9173 arg);
9176 /* Print an error message for an invalid lvalue. USE says
9177 how the lvalue is being used and so selects the error message. LOC
9178 is the location for the error. */
9180 void
9181 lvalue_error (location_t loc, enum lvalue_use use)
9183 switch (use)
9185 case lv_assign:
9186 error_at (loc, "lvalue required as left operand of assignment");
9187 break;
9188 case lv_increment:
9189 error_at (loc, "lvalue required as increment operand");
9190 break;
9191 case lv_decrement:
9192 error_at (loc, "lvalue required as decrement operand");
9193 break;
9194 case lv_addressof:
9195 error_at (loc, "lvalue required as unary %<&%> operand");
9196 break;
9197 case lv_asm:
9198 error_at (loc, "lvalue required in asm statement");
9199 break;
9200 default:
9201 gcc_unreachable ();
9205 /* Print an error message for an invalid indirection of type TYPE.
9206 ERRSTRING is the name of the operator for the indirection. */
9208 void
9209 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9211 switch (errstring)
9213 case RO_NULL:
9214 gcc_assert (c_dialect_cxx ());
9215 error_at (loc, "invalid type argument (have %qT)", type);
9216 break;
9217 case RO_ARRAY_INDEXING:
9218 error_at (loc,
9219 "invalid type argument of array indexing (have %qT)",
9220 type);
9221 break;
9222 case RO_UNARY_STAR:
9223 error_at (loc,
9224 "invalid type argument of unary %<*%> (have %qT)",
9225 type);
9226 break;
9227 case RO_ARROW:
9228 error_at (loc,
9229 "invalid type argument of %<->%> (have %qT)",
9230 type);
9231 break;
9232 case RO_IMPLICIT_CONVERSION:
9233 error_at (loc,
9234 "invalid type argument of implicit conversion (have %qT)",
9235 type);
9236 break;
9237 default:
9238 gcc_unreachable ();
9242 /* *PTYPE is an incomplete array. Complete it with a domain based on
9243 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9244 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9245 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9248 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9250 tree maxindex, type, main_type, elt, unqual_elt;
9251 int failure = 0, quals;
9252 hashval_t hashcode = 0;
9254 maxindex = size_zero_node;
9255 if (initial_value)
9257 if (TREE_CODE (initial_value) == STRING_CST)
9259 int eltsize
9260 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9261 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9263 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9265 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
9267 if (VEC_empty (constructor_elt, v))
9269 if (pedantic)
9270 failure = 3;
9271 maxindex = ssize_int (-1);
9273 else
9275 tree curindex;
9276 unsigned HOST_WIDE_INT cnt;
9277 constructor_elt *ce;
9278 bool fold_p = false;
9280 if (VEC_index (constructor_elt, v, 0)->index)
9281 maxindex = fold_convert_loc (input_location, sizetype,
9282 VEC_index (constructor_elt,
9283 v, 0)->index);
9284 curindex = maxindex;
9286 for (cnt = 1;
9287 VEC_iterate (constructor_elt, v, cnt, ce);
9288 cnt++)
9290 bool curfold_p = false;
9291 if (ce->index)
9292 curindex = ce->index, curfold_p = true;
9293 else
9295 if (fold_p)
9296 curindex = fold_convert (sizetype, curindex);
9297 curindex = size_binop (PLUS_EXPR, curindex,
9298 size_one_node);
9300 if (tree_int_cst_lt (maxindex, curindex))
9301 maxindex = curindex, fold_p = curfold_p;
9303 if (fold_p)
9304 maxindex = fold_convert (sizetype, maxindex);
9307 else
9309 /* Make an error message unless that happened already. */
9310 if (initial_value != error_mark_node)
9311 failure = 1;
9314 else
9316 failure = 2;
9317 if (!do_default)
9318 return failure;
9321 type = *ptype;
9322 elt = TREE_TYPE (type);
9323 quals = TYPE_QUALS (strip_array_types (elt));
9324 if (quals == 0)
9325 unqual_elt = elt;
9326 else
9327 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9329 /* Using build_distinct_type_copy and modifying things afterward instead
9330 of using build_array_type to create a new type preserves all of the
9331 TYPE_LANG_FLAG_? bits that the front end may have set. */
9332 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9333 TREE_TYPE (main_type) = unqual_elt;
9334 TYPE_DOMAIN (main_type)
9335 = build_range_type (TREE_TYPE (maxindex),
9336 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9337 layout_type (main_type);
9339 /* Make sure we have the canonical MAIN_TYPE. */
9340 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9341 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9342 hashcode);
9343 main_type = type_hash_canon (hashcode, main_type);
9345 /* Fix the canonical type. */
9346 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9347 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9348 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9349 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9350 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9351 != TYPE_DOMAIN (main_type)))
9352 TYPE_CANONICAL (main_type)
9353 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9354 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9355 else
9356 TYPE_CANONICAL (main_type) = main_type;
9358 if (quals == 0)
9359 type = main_type;
9360 else
9361 type = c_build_qualified_type (main_type, quals);
9363 if (COMPLETE_TYPE_P (type)
9364 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9365 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9367 error ("size of array is too large");
9368 /* If we proceed with the array type as it is, we'll eventually
9369 crash in tree_low_cst(). */
9370 type = error_mark_node;
9373 *ptype = type;
9374 return failure;
9377 /* Like c_mark_addressable but don't check register qualifier. */
9378 void
9379 c_common_mark_addressable_vec (tree t)
9381 while (handled_component_p (t))
9382 t = TREE_OPERAND (t, 0);
9383 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9384 return;
9385 TREE_ADDRESSABLE (t) = 1;
9390 /* Used to help initialize the builtin-types.def table. When a type of
9391 the correct size doesn't exist, use error_mark_node instead of NULL.
9392 The later results in segfaults even when a decl using the type doesn't
9393 get invoked. */
9395 tree
9396 builtin_type_for_size (int size, bool unsignedp)
9398 tree type = c_common_type_for_size (size, unsignedp);
9399 return type ? type : error_mark_node;
9402 /* A helper function for resolve_overloaded_builtin in resolving the
9403 overloaded __sync_ builtins. Returns a positive power of 2 if the
9404 first operand of PARAMS is a pointer to a supported data type.
9405 Returns 0 if an error is encountered. */
9407 static int
9408 sync_resolve_size (tree function, VEC(tree,gc) *params)
9410 tree type;
9411 int size;
9413 if (VEC_empty (tree, params))
9415 error ("too few arguments to function %qE", function);
9416 return 0;
9419 type = TREE_TYPE (VEC_index (tree, params, 0));
9420 if (TREE_CODE (type) != POINTER_TYPE)
9421 goto incompatible;
9423 type = TREE_TYPE (type);
9424 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9425 goto incompatible;
9427 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9428 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9429 return size;
9431 incompatible:
9432 error ("incompatible type for argument %d of %qE", 1, function);
9433 return 0;
9436 /* A helper function for resolve_overloaded_builtin. Adds casts to
9437 PARAMS to make arguments match up with those of FUNCTION. Drops
9438 the variadic arguments at the end. Returns false if some error
9439 was encountered; true on success. */
9441 static bool
9442 sync_resolve_params (location_t loc, tree orig_function, tree function,
9443 VEC(tree, gc) *params, bool orig_format)
9445 function_args_iterator iter;
9446 tree ptype;
9447 unsigned int parmnum;
9449 function_args_iter_init (&iter, TREE_TYPE (function));
9450 /* We've declared the implementation functions to use "volatile void *"
9451 as the pointer parameter, so we shouldn't get any complaints from the
9452 call to check_function_arguments what ever type the user used. */
9453 function_args_iter_next (&iter);
9454 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9456 /* For the rest of the values, we need to cast these to FTYPE, so that we
9457 don't get warnings for passing pointer types, etc. */
9458 parmnum = 0;
9459 while (1)
9461 tree val, arg_type;
9463 arg_type = function_args_iter_cond (&iter);
9464 /* XXX void_type_node belies the abstraction. */
9465 if (arg_type == void_type_node)
9466 break;
9468 ++parmnum;
9469 if (VEC_length (tree, params) <= parmnum)
9471 error_at (loc, "too few arguments to function %qE", orig_function);
9472 return false;
9475 /* Only convert parameters if arg_type is unsigned integer type with
9476 new format sync routines, i.e. don't attempt to convert pointer
9477 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9478 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9479 kinds). */
9480 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
9482 /* Ideally for the first conversion we'd use convert_for_assignment
9483 so that we get warnings for anything that doesn't match the pointer
9484 type. This isn't portable across the C and C++ front ends atm. */
9485 val = VEC_index (tree, params, parmnum);
9486 val = convert (ptype, val);
9487 val = convert (arg_type, val);
9488 VEC_replace (tree, params, parmnum, val);
9491 function_args_iter_next (&iter);
9494 /* __atomic routines are not variadic. */
9495 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9497 error_at (loc, "too many arguments to function %qE", orig_function);
9498 return false;
9501 /* The definition of these primitives is variadic, with the remaining
9502 being "an optional list of variables protected by the memory barrier".
9503 No clue what that's supposed to mean, precisely, but we consider all
9504 call-clobbered variables to be protected so we're safe. */
9505 VEC_truncate (tree, params, parmnum + 1);
9507 return true;
9510 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9511 RESULT to make it match the type of the first pointer argument in
9512 PARAMS. */
9514 static tree
9515 sync_resolve_return (tree first_param, tree result, bool orig_format)
9517 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9518 tree rtype = TREE_TYPE (result);
9519 ptype = TYPE_MAIN_VARIANT (ptype);
9521 /* New format doesn't require casting unless the types are the same size. */
9522 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9523 return convert (ptype, result);
9524 else
9525 return result;
9528 /* This function verifies the PARAMS to generic atomic FUNCTION.
9529 It returns the size if all the parameters are the same size, otherwise
9530 0 is returned if the parameters are invalid. */
9532 static int
9533 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9535 unsigned int n_param;
9536 unsigned int n_model;
9537 unsigned int x;
9538 int size_0;
9539 tree type_0;
9541 /* Determine the parameter makeup. */
9542 switch (DECL_FUNCTION_CODE (function))
9544 case BUILT_IN_ATOMIC_EXCHANGE:
9545 n_param = 4;
9546 n_model = 1;
9547 break;
9548 case BUILT_IN_ATOMIC_LOAD:
9549 case BUILT_IN_ATOMIC_STORE:
9550 n_param = 3;
9551 n_model = 1;
9552 break;
9553 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9554 n_param = 6;
9555 n_model = 2;
9556 break;
9557 default:
9558 gcc_unreachable ();
9561 if (VEC_length (tree, params) != n_param)
9563 error_at (loc, "incorrect number of arguments to function %qE", function);
9564 return 0;
9567 /* Get type of first parameter, and determine its size. */
9568 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9569 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
9571 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
9572 function);
9573 return 0;
9576 /* Types must be compile time constant sizes. */
9577 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
9579 error_at (loc,
9580 "argument 1 of %qE must be a pointer to a constant size type",
9581 function);
9582 return 0;
9585 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9587 /* Zero size objects are not allowed. */
9588 if (size_0 == 0)
9590 error_at (loc,
9591 "argument 1 of %qE must be a pointer to a nonzero size object",
9592 function);
9593 return 0;
9596 /* Check each other parameter is a pointer and the same size. */
9597 for (x = 0; x < n_param - n_model; x++)
9599 int size;
9600 tree type = TREE_TYPE (VEC_index (tree, params, x));
9601 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9602 if (n_param == 6 && x == 3)
9603 continue;
9604 if (!POINTER_TYPE_P (type))
9606 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9607 function);
9608 return 0;
9610 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9611 if (size != size_0)
9613 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9614 function);
9615 return 0;
9619 /* Check memory model parameters for validity. */
9620 for (x = n_param - n_model ; x < n_param; x++)
9622 tree p = VEC_index (tree, params, x);
9623 if (TREE_CODE (p) == INTEGER_CST)
9625 int i = tree_low_cst (p, 1);
9626 if (i < 0 || i >= MEMMODEL_LAST)
9628 warning_at (loc, OPT_Winvalid_memory_model,
9629 "invalid memory model argument %d of %qE", x + 1,
9630 function);
9633 else
9634 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9636 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9637 function);
9638 return 0;
9642 return size_0;
9646 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
9647 at the beginning of the parameter list PARAMS representing the size of the
9648 objects. This is to match the library ABI requirement. LOC is the location
9649 of the function call.
9650 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
9651 returned to allow the external call to be constructed. */
9653 static tree
9654 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9655 VEC(tree,gc) *params)
9657 tree size_node;
9659 /* Insert a SIZE_T parameter as the first param. If there isn't
9660 enough space, allocate a new vector and recursively re-build with that. */
9661 if (!VEC_space (tree, params, 1))
9663 unsigned int z, len;
9664 VEC(tree,gc) *vec;
9665 tree f;
9667 len = VEC_length (tree, params);
9668 vec = VEC_alloc (tree, gc, len + 1);
9669 for (z = 0; z < len; z++)
9670 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
9671 f = build_function_call_vec (loc, function, vec, NULL);
9672 VEC_free (tree, gc, vec);
9673 return f;
9676 /* Add the size parameter and leave as a function call for processing. */
9677 size_node = build_int_cst (size_type_node, n);
9678 VEC_quick_insert (tree, params, 0, size_node);
9679 return NULL_TREE;
9683 /* This will process an __atomic_exchange function call, determine whether it
9684 needs to be mapped to the _N variation, or turned into a library call.
9685 LOC is the location of the builtin call.
9686 FUNCTION is the DECL that has been invoked;
9687 PARAMS is the argument list for the call. The return value is non-null
9688 TRUE is returned if it is translated into the proper format for a call to the
9689 external library, and NEW_RETURN is set the tree for that function.
9690 FALSE is returned if processing for the _N variation is required, and
9691 NEW_RETURN is set to the the return value the result is copied into. */
9692 static bool
9693 resolve_overloaded_atomic_exchange (location_t loc, tree function,
9694 VEC(tree,gc) *params, tree *new_return)
9696 tree p0, p1, p2, p3;
9697 tree I_type, I_type_ptr;
9698 int n = get_atomic_generic_size (loc, function, params);
9700 /* Size of 0 is an error condition. */
9701 if (n == 0)
9703 *new_return = error_mark_node;
9704 return true;
9707 /* If not a lock-free size, change to the library generic format. */
9708 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9710 *new_return = add_atomic_size_parameter (n, loc, function, params);
9711 return true;
9714 /* Otherwise there is a lockfree match, transform the call from:
9715 void fn(T* mem, T* desired, T* return, model)
9716 into
9717 *return = (T) (fn (In* mem, (In) *desired, model)) */
9719 p0 = VEC_index (tree, params, 0);
9720 p1 = VEC_index (tree, params, 1);
9721 p2 = VEC_index (tree, params, 2);
9722 p3 = VEC_index (tree, params, 3);
9724 /* Create pointer to appropriate size. */
9725 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9726 I_type_ptr = build_pointer_type (I_type);
9728 /* Convert object pointer to required type. */
9729 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9730 VEC_replace (tree, params, 0, p0);
9731 /* Convert new value to required type, and dereference it. */
9732 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9733 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9734 VEC_replace (tree, params, 1, p1);
9736 /* Move memory model to the 3rd position, and end param list. */
9737 VEC_replace (tree, params, 2, p3);
9738 VEC_truncate (tree, params, 3);
9740 /* Convert return pointer and dereference it for later assignment. */
9741 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9743 return false;
9747 /* This will process an __atomic_compare_exchange function call, determine
9748 whether it needs to be mapped to the _N variation, or turned into a lib call.
9749 LOC is the location of the builtin call.
9750 FUNCTION is the DECL that has been invoked;
9751 PARAMS is the argument list for the call. The return value is non-null
9752 TRUE is returned if it is translated into the proper format for a call to the
9753 external library, and NEW_RETURN is set the tree for that function.
9754 FALSE is returned if processing for the _N variation is required. */
9756 static bool
9757 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9758 VEC(tree,gc) *params,
9759 tree *new_return)
9761 tree p0, p1, p2;
9762 tree I_type, I_type_ptr;
9763 int n = get_atomic_generic_size (loc, function, params);
9765 /* Size of 0 is an error condition. */
9766 if (n == 0)
9768 *new_return = error_mark_node;
9769 return true;
9772 /* If not a lock-free size, change to the library generic format. */
9773 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9775 /* The library generic format does not have the weak parameter, so
9776 remove it from the param list. Since a parameter has been removed,
9777 we can be sure that there is room for the SIZE_T parameter, meaning
9778 there will not be a recursive rebuilding of the parameter list, so
9779 there is no danger this will be done twice. */
9780 if (n > 0)
9782 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
9783 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
9784 VEC_truncate (tree, params, 5);
9786 *new_return = add_atomic_size_parameter (n, loc, function, params);
9787 return true;
9790 /* Otherwise, there is a match, so the call needs to be transformed from:
9791 bool fn(T* mem, T* desired, T* return, weak, success, failure)
9792 into
9793 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
9795 p0 = VEC_index (tree, params, 0);
9796 p1 = VEC_index (tree, params, 1);
9797 p2 = VEC_index (tree, params, 2);
9799 /* Create pointer to appropriate size. */
9800 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9801 I_type_ptr = build_pointer_type (I_type);
9803 /* Convert object pointer to required type. */
9804 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9805 VEC_replace (tree, params, 0, p0);
9807 /* Convert expected pointer to required type. */
9808 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9809 VEC_replace (tree, params, 1, p1);
9811 /* Convert desired value to required type, and dereference it. */
9812 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9813 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9814 VEC_replace (tree, params, 2, p2);
9816 /* The rest of the parameters are fine. NULL means no special return value
9817 processing.*/
9818 *new_return = NULL;
9819 return false;
9823 /* This will process an __atomic_load function call, determine whether it
9824 needs to be mapped to the _N variation, or turned into a library call.
9825 LOC is the location of the builtin call.
9826 FUNCTION is the DECL that has been invoked;
9827 PARAMS is the argument list for the call. The return value is non-null
9828 TRUE is returned if it is translated into the proper format for a call to the
9829 external library, and NEW_RETURN is set the tree for that function.
9830 FALSE is returned if processing for the _N variation is required, and
9831 NEW_RETURN is set to the the return value the result is copied into. */
9833 static bool
9834 resolve_overloaded_atomic_load (location_t loc, tree function,
9835 VEC(tree,gc) *params, tree *new_return)
9837 tree p0, p1, p2;
9838 tree I_type, I_type_ptr;
9839 int n = get_atomic_generic_size (loc, function, params);
9841 /* Size of 0 is an error condition. */
9842 if (n == 0)
9844 *new_return = error_mark_node;
9845 return true;
9848 /* If not a lock-free size, change to the library generic format. */
9849 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9851 *new_return = add_atomic_size_parameter (n, loc, function, params);
9852 return true;
9855 /* Otherwise, there is a match, so the call needs to be transformed from:
9856 void fn(T* mem, T* return, model)
9857 into
9858 *return = (T) (fn ((In *) mem, model)) */
9860 p0 = VEC_index (tree, params, 0);
9861 p1 = VEC_index (tree, params, 1);
9862 p2 = VEC_index (tree, params, 2);
9864 /* Create pointer to appropriate size. */
9865 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9866 I_type_ptr = build_pointer_type (I_type);
9868 /* Convert object pointer to required type. */
9869 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9870 VEC_replace (tree, params, 0, p0);
9872 /* Move memory model to the 2nd position, and end param list. */
9873 VEC_replace (tree, params, 1, p2);
9874 VEC_truncate (tree, params, 2);
9876 /* Convert return pointer and dereference it for later assignment. */
9877 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9879 return false;
9883 /* This will process an __atomic_store function call, determine whether it
9884 needs to be mapped to the _N variation, or turned into a library call.
9885 LOC is the location of the builtin call.
9886 FUNCTION is the DECL that has been invoked;
9887 PARAMS is the argument list for the call. The return value is non-null
9888 TRUE is returned if it is translated into the proper format for a call to the
9889 external library, and NEW_RETURN is set the tree for that function.
9890 FALSE is returned if processing for the _N variation is required, and
9891 NEW_RETURN is set to the the return value the result is copied into. */
9893 static bool
9894 resolve_overloaded_atomic_store (location_t loc, tree function,
9895 VEC(tree,gc) *params, tree *new_return)
9897 tree p0, p1;
9898 tree I_type, I_type_ptr;
9899 int n = get_atomic_generic_size (loc, function, params);
9901 /* Size of 0 is an error condition. */
9902 if (n == 0)
9904 *new_return = error_mark_node;
9905 return true;
9908 /* If not a lock-free size, change to the library generic format. */
9909 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9911 *new_return = add_atomic_size_parameter (n, loc, function, params);
9912 return true;
9915 /* Otherwise, there is a match, so the call needs to be transformed from:
9916 void fn(T* mem, T* value, model)
9917 into
9918 fn ((In *) mem, (In) *value, model) */
9920 p0 = VEC_index (tree, params, 0);
9921 p1 = VEC_index (tree, params, 1);
9923 /* Create pointer to appropriate size. */
9924 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9925 I_type_ptr = build_pointer_type (I_type);
9927 /* Convert object pointer to required type. */
9928 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9929 VEC_replace (tree, params, 0, p0);
9931 /* Convert new value to required type, and dereference it. */
9932 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9933 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9934 VEC_replace (tree, params, 1, p1);
9936 /* The memory model is in the right spot already. Return is void. */
9937 *new_return = NULL_TREE;
9939 return false;
9943 /* Some builtin functions are placeholders for other expressions. This
9944 function should be called immediately after parsing the call expression
9945 before surrounding code has committed to the type of the expression.
9947 LOC is the location of the builtin call.
9949 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
9950 PARAMS is the argument list for the call. The return value is non-null
9951 when expansion is complete, and null if normal processing should
9952 continue. */
9954 tree
9955 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
9957 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
9958 bool orig_format = true;
9959 tree new_return = NULL_TREE;
9961 switch (DECL_BUILT_IN_CLASS (function))
9963 case BUILT_IN_NORMAL:
9964 break;
9965 case BUILT_IN_MD:
9966 if (targetm.resolve_overloaded_builtin)
9967 return targetm.resolve_overloaded_builtin (loc, function, params);
9968 else
9969 return NULL_TREE;
9970 default:
9971 return NULL_TREE;
9974 /* Handle BUILT_IN_NORMAL here. */
9975 switch (orig_code)
9977 case BUILT_IN_ATOMIC_EXCHANGE:
9978 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9979 case BUILT_IN_ATOMIC_LOAD:
9980 case BUILT_IN_ATOMIC_STORE:
9982 /* Handle these 4 together so that they can fall through to the next
9983 case if the call is transformed to an _N variant. */
9984 switch (orig_code)
9986 case BUILT_IN_ATOMIC_EXCHANGE:
9988 if (resolve_overloaded_atomic_exchange (loc, function, params,
9989 &new_return))
9990 return new_return;
9991 /* Change to the _N variant. */
9992 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
9993 break;
9996 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9998 if (resolve_overloaded_atomic_compare_exchange (loc, function,
9999 params,
10000 &new_return))
10001 return new_return;
10002 /* Change to the _N variant. */
10003 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10004 break;
10006 case BUILT_IN_ATOMIC_LOAD:
10008 if (resolve_overloaded_atomic_load (loc, function, params,
10009 &new_return))
10010 return new_return;
10011 /* Change to the _N variant. */
10012 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10013 break;
10015 case BUILT_IN_ATOMIC_STORE:
10017 if (resolve_overloaded_atomic_store (loc, function, params,
10018 &new_return))
10019 return new_return;
10020 /* Change to the _N variant. */
10021 orig_code = BUILT_IN_ATOMIC_STORE_N;
10022 break;
10024 default:
10025 gcc_unreachable ();
10027 /* Fallthrough to the normal processing. */
10029 case BUILT_IN_ATOMIC_EXCHANGE_N:
10030 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10031 case BUILT_IN_ATOMIC_LOAD_N:
10032 case BUILT_IN_ATOMIC_STORE_N:
10033 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10034 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10035 case BUILT_IN_ATOMIC_AND_FETCH_N:
10036 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10037 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10038 case BUILT_IN_ATOMIC_OR_FETCH_N:
10039 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10040 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10041 case BUILT_IN_ATOMIC_FETCH_AND_N:
10042 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10043 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10044 case BUILT_IN_ATOMIC_FETCH_OR_N:
10046 orig_format = false;
10047 /* Fallthru for parameter processing. */
10049 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10050 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10051 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10052 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10053 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10054 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10055 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10056 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10057 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10058 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10059 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10060 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10061 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10062 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10063 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10064 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10066 int n = sync_resolve_size (function, params);
10067 tree new_function, first_param, result;
10068 enum built_in_function fncode;
10070 if (n == 0)
10071 return error_mark_node;
10073 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10074 new_function = builtin_decl_explicit (fncode);
10075 if (!sync_resolve_params (loc, function, new_function, params,
10076 orig_format))
10077 return error_mark_node;
10079 first_param = VEC_index (tree, params, 0);
10080 result = build_function_call_vec (loc, new_function, params, NULL);
10081 if (result == error_mark_node)
10082 return result;
10083 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10084 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10085 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10086 result = sync_resolve_return (first_param, result, orig_format);
10088 /* If new_return is set, assign function to that expr and cast the
10089 result to void since the generic interface returned void. */
10090 if (new_return)
10092 /* Cast function result from I{1,2,4,8,16} to the required type. */
10093 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10094 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10095 result);
10096 TREE_SIDE_EFFECTS (result) = 1;
10097 protected_set_expr_location (result, loc);
10098 result = convert (void_type_node, result);
10100 return result;
10103 default:
10104 return NULL_TREE;
10108 /* Ignoring their sign, return true if two scalar types are the same. */
10109 bool
10110 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10112 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10114 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10115 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10116 || c2 == FIXED_POINT_TYPE));
10118 /* Equality works here because c_common_signed_type uses
10119 TYPE_MAIN_VARIANT. */
10120 return c_common_signed_type (t1)
10121 == c_common_signed_type (t2);
10124 /* Check for missing format attributes on function pointers. LTYPE is
10125 the new type or left-hand side type. RTYPE is the old type or
10126 right-hand side type. Returns TRUE if LTYPE is missing the desired
10127 attribute. */
10129 bool
10130 check_missing_format_attribute (tree ltype, tree rtype)
10132 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10133 tree ra;
10135 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10136 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10137 break;
10138 if (ra)
10140 tree la;
10141 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10142 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10143 break;
10144 return !la;
10146 else
10147 return false;
10150 /* Subscripting with type char is likely to lose on a machine where
10151 chars are signed. So warn on any machine, but optionally. Don't
10152 warn for unsigned char since that type is safe. Don't warn for
10153 signed char because anyone who uses that must have done so
10154 deliberately. Furthermore, we reduce the false positive load by
10155 warning only for non-constant value of type char. */
10157 void
10158 warn_array_subscript_with_type_char (tree index)
10160 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10161 && TREE_CODE (index) != INTEGER_CST)
10162 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10165 /* Implement -Wparentheses for the unexpected C precedence rules, to
10166 cover cases like x + y << z which readers are likely to
10167 misinterpret. We have seen an expression in which CODE is a binary
10168 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10169 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10170 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10171 expression was not formed using a binary or unary operator, or it
10172 was enclosed in parentheses. */
10174 void
10175 warn_about_parentheses (enum tree_code code,
10176 enum tree_code code_left, tree arg_left,
10177 enum tree_code code_right, tree arg_right)
10179 if (!warn_parentheses)
10180 return;
10182 /* This macro tests that the expression ARG with original tree code
10183 CODE appears to be a boolean expression. or the result of folding a
10184 boolean expression. */
10185 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10186 (truth_value_p (TREE_CODE (ARG)) \
10187 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10188 /* Folding may create 0 or 1 integers from other expressions. */ \
10189 || ((CODE) != INTEGER_CST \
10190 && (integer_onep (ARG) || integer_zerop (ARG))))
10192 switch (code)
10194 case LSHIFT_EXPR:
10195 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10196 warning (OPT_Wparentheses,
10197 "suggest parentheses around %<+%> inside %<<<%>");
10198 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10199 warning (OPT_Wparentheses,
10200 "suggest parentheses around %<-%> inside %<<<%>");
10201 return;
10203 case RSHIFT_EXPR:
10204 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10205 warning (OPT_Wparentheses,
10206 "suggest parentheses around %<+%> inside %<>>%>");
10207 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10208 warning (OPT_Wparentheses,
10209 "suggest parentheses around %<-%> inside %<>>%>");
10210 return;
10212 case TRUTH_ORIF_EXPR:
10213 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
10214 warning (OPT_Wparentheses,
10215 "suggest parentheses around %<&&%> within %<||%>");
10216 return;
10218 case BIT_IOR_EXPR:
10219 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10220 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10221 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10222 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10223 warning (OPT_Wparentheses,
10224 "suggest parentheses around arithmetic in operand of %<|%>");
10225 /* Check cases like x|y==z */
10226 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10227 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10228 warning (OPT_Wparentheses,
10229 "suggest parentheses around comparison in operand of %<|%>");
10230 /* Check cases like !x | y */
10231 else if (code_left == TRUTH_NOT_EXPR
10232 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10233 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10234 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10235 return;
10237 case BIT_XOR_EXPR:
10238 if (code_left == BIT_AND_EXPR
10239 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10240 || code_right == BIT_AND_EXPR
10241 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10242 warning (OPT_Wparentheses,
10243 "suggest parentheses around arithmetic in operand of %<^%>");
10244 /* Check cases like x^y==z */
10245 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10246 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10247 warning (OPT_Wparentheses,
10248 "suggest parentheses around comparison in operand of %<^%>");
10249 return;
10251 case BIT_AND_EXPR:
10252 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10253 warning (OPT_Wparentheses,
10254 "suggest parentheses around %<+%> in operand of %<&%>");
10255 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10256 warning (OPT_Wparentheses,
10257 "suggest parentheses around %<-%> in operand of %<&%>");
10258 /* Check cases like x&y==z */
10259 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10260 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10261 warning (OPT_Wparentheses,
10262 "suggest parentheses around comparison in operand of %<&%>");
10263 /* Check cases like !x & y */
10264 else if (code_left == TRUTH_NOT_EXPR
10265 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10266 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10267 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10268 return;
10270 case EQ_EXPR:
10271 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10272 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10273 warning (OPT_Wparentheses,
10274 "suggest parentheses around comparison in operand of %<==%>");
10275 return;
10276 case NE_EXPR:
10277 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10278 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10279 warning (OPT_Wparentheses,
10280 "suggest parentheses around comparison in operand of %<!=%>");
10281 return;
10283 default:
10284 if (TREE_CODE_CLASS (code) == tcc_comparison
10285 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
10286 && code_left != NE_EXPR && code_left != EQ_EXPR
10287 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10288 || (TREE_CODE_CLASS (code_right) == tcc_comparison
10289 && code_right != NE_EXPR && code_right != EQ_EXPR
10290 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
10291 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
10292 "have their mathematical meaning");
10293 return;
10295 #undef NOT_A_BOOLEAN_EXPR_P
10298 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10300 void
10301 warn_for_unused_label (tree label)
10303 if (!TREE_USED (label))
10305 if (DECL_INITIAL (label))
10306 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10307 else
10308 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10312 /* Warn for division by zero according to the value of DIVISOR. LOC
10313 is the location of the division operator. */
10315 void
10316 warn_for_div_by_zero (location_t loc, tree divisor)
10318 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10319 about division by zero. Do not issue a warning if DIVISOR has a
10320 floating-point type, since we consider 0.0/0.0 a valid way of
10321 generating a NaN. */
10322 if (c_inhibit_evaluation_warnings == 0
10323 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10324 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10327 /* Subroutine of build_binary_op. Give warnings for comparisons
10328 between signed and unsigned quantities that may fail. Do the
10329 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10330 so that casts will be considered, but default promotions won't
10333 LOCATION is the location of the comparison operator.
10335 The arguments of this function map directly to local variables
10336 of build_binary_op. */
10338 void
10339 warn_for_sign_compare (location_t location,
10340 tree orig_op0, tree orig_op1,
10341 tree op0, tree op1,
10342 tree result_type, enum tree_code resultcode)
10344 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10345 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10346 int unsignedp0, unsignedp1;
10348 /* In C++, check for comparison of different enum types. */
10349 if (c_dialect_cxx()
10350 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10351 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10352 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10353 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10355 warning_at (location,
10356 OPT_Wsign_compare, "comparison between types %qT and %qT",
10357 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10360 /* Do not warn if the comparison is being done in a signed type,
10361 since the signed type will only be chosen if it can represent
10362 all the values of the unsigned type. */
10363 if (!TYPE_UNSIGNED (result_type))
10364 /* OK */;
10365 /* Do not warn if both operands are unsigned. */
10366 else if (op0_signed == op1_signed)
10367 /* OK */;
10368 else
10370 tree sop, uop, base_type;
10371 bool ovf;
10373 if (op0_signed)
10374 sop = orig_op0, uop = orig_op1;
10375 else
10376 sop = orig_op1, uop = orig_op0;
10378 STRIP_TYPE_NOPS (sop);
10379 STRIP_TYPE_NOPS (uop);
10380 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10381 ? TREE_TYPE (result_type) : result_type);
10383 /* Do not warn if the signed quantity is an unsuffixed integer
10384 literal (or some static constant expression involving such
10385 literals or a conditional expression involving such literals)
10386 and it is non-negative. */
10387 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10388 /* OK */;
10389 /* Do not warn if the comparison is an equality operation, the
10390 unsigned quantity is an integral constant, and it would fit
10391 in the result if the result were signed. */
10392 else if (TREE_CODE (uop) == INTEGER_CST
10393 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10394 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10395 /* OK */;
10396 /* In C, do not warn if the unsigned quantity is an enumeration
10397 constant and its maximum value would fit in the result if the
10398 result were signed. */
10399 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10400 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10401 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
10402 c_common_signed_type (base_type)))
10403 /* OK */;
10404 else
10405 warning_at (location,
10406 OPT_Wsign_compare,
10407 "comparison between signed and unsigned integer expressions");
10410 /* Warn if two unsigned values are being compared in a size larger
10411 than their original size, and one (and only one) is the result of
10412 a `~' operator. This comparison will always fail.
10414 Also warn if one operand is a constant, and the constant does not
10415 have all bits set that are set in the ~ operand when it is
10416 extended. */
10418 op0 = c_common_get_narrower (op0, &unsignedp0);
10419 op1 = c_common_get_narrower (op1, &unsignedp1);
10421 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10422 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10424 if (TREE_CODE (op0) == BIT_NOT_EXPR)
10425 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
10426 if (TREE_CODE (op1) == BIT_NOT_EXPR)
10427 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
10429 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10431 tree primop;
10432 HOST_WIDE_INT constant, mask;
10433 int unsignedp;
10434 unsigned int bits;
10436 if (host_integerp (op0, 0))
10438 primop = op1;
10439 unsignedp = unsignedp1;
10440 constant = tree_low_cst (op0, 0);
10442 else
10444 primop = op0;
10445 unsignedp = unsignedp0;
10446 constant = tree_low_cst (op1, 0);
10449 bits = TYPE_PRECISION (TREE_TYPE (primop));
10450 if (bits < TYPE_PRECISION (result_type)
10451 && bits < HOST_BITS_PER_LONG && unsignedp)
10453 mask = (~ (HOST_WIDE_INT) 0) << bits;
10454 if ((mask & constant) != mask)
10456 if (constant == 0)
10457 warning (OPT_Wsign_compare,
10458 "promoted ~unsigned is always non-zero");
10459 else
10460 warning_at (location, OPT_Wsign_compare,
10461 "comparison of promoted ~unsigned with constant");
10465 else if (unsignedp0 && unsignedp1
10466 && (TYPE_PRECISION (TREE_TYPE (op0))
10467 < TYPE_PRECISION (result_type))
10468 && (TYPE_PRECISION (TREE_TYPE (op1))
10469 < TYPE_PRECISION (result_type)))
10470 warning_at (location, OPT_Wsign_compare,
10471 "comparison of promoted ~unsigned with unsigned");
10475 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10476 type via c_common_type. If -Wdouble-promotion is in use, and the
10477 conditions for warning have been met, issue a warning. GMSGID is
10478 the warning message. It must have two %T specifiers for the type
10479 that was converted (generally "float") and the type to which it was
10480 converted (generally "double), respectively. LOC is the location
10481 to which the awrning should refer. */
10483 void
10484 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10485 const char *gmsgid, location_t loc)
10487 tree source_type;
10489 if (!warn_double_promotion)
10490 return;
10491 /* If the conversion will not occur at run-time, there is no need to
10492 warn about it. */
10493 if (c_inhibit_evaluation_warnings)
10494 return;
10495 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10496 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10497 return;
10498 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10499 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10500 source_type = type1;
10501 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10502 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10503 source_type = type2;
10504 else
10505 return;
10506 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10509 /* Setup a TYPE_DECL node as a typedef representation.
10511 X is a TYPE_DECL for a typedef statement. Create a brand new
10512 ..._TYPE node (which will be just a variant of the existing
10513 ..._TYPE node with identical properties) and then install X
10514 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10516 The whole point here is to end up with a situation where each
10517 and every ..._TYPE node the compiler creates will be uniquely
10518 associated with AT MOST one node representing a typedef name.
10519 This way, even though the compiler substitutes corresponding
10520 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10521 early on, later parts of the compiler can always do the reverse
10522 translation and get back the corresponding typedef name. For
10523 example, given:
10525 typedef struct S MY_TYPE;
10526 MY_TYPE object;
10528 Later parts of the compiler might only know that `object' was of
10529 type `struct S' if it were not for code just below. With this
10530 code however, later parts of the compiler see something like:
10532 struct S' == struct S
10533 typedef struct S' MY_TYPE;
10534 struct S' object;
10536 And they can then deduce (from the node for type struct S') that
10537 the original object declaration was:
10539 MY_TYPE object;
10541 Being able to do this is important for proper support of protoize,
10542 and also for generating precise symbolic debugging information
10543 which takes full account of the programmer's (typedef) vocabulary.
10545 Obviously, we don't want to generate a duplicate ..._TYPE node if
10546 the TYPE_DECL node that we are now processing really represents a
10547 standard built-in type. */
10549 void
10550 set_underlying_type (tree x)
10552 if (x == error_mark_node)
10553 return;
10554 if (DECL_IS_BUILTIN (x))
10556 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10557 TYPE_NAME (TREE_TYPE (x)) = x;
10559 else if (TREE_TYPE (x) != error_mark_node
10560 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10562 tree tt = TREE_TYPE (x);
10563 DECL_ORIGINAL_TYPE (x) = tt;
10564 tt = build_variant_type_copy (tt);
10565 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10566 TYPE_NAME (tt) = x;
10567 TREE_USED (tt) = TREE_USED (x);
10568 TREE_TYPE (x) = tt;
10572 /* Record the types used by the current global variable declaration
10573 being parsed, so that we can decide later to emit their debug info.
10574 Those types are in types_used_by_cur_var_decl, and we are going to
10575 store them in the types_used_by_vars_hash hash table.
10576 DECL is the declaration of the global variable that has been parsed. */
10578 void
10579 record_types_used_by_current_var_decl (tree decl)
10581 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10583 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10585 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10586 types_used_by_var_decl_insert (type, decl);
10590 /* If DECL is a typedef that is declared in the current function,
10591 record it for the purpose of -Wunused-local-typedefs. */
10593 void
10594 record_locally_defined_typedef (tree decl)
10596 struct c_language_function *l;
10598 if (!warn_unused_local_typedefs
10599 || cfun == NULL
10600 /* if this is not a locally defined typedef then we are not
10601 interested. */
10602 || !is_typedef_decl (decl)
10603 || !decl_function_context (decl))
10604 return;
10606 l = (struct c_language_function *) cfun->language;
10607 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10610 /* If T is a TYPE_DECL declared locally, mark it as used. */
10612 void
10613 maybe_record_typedef_use (tree t)
10615 if (!is_typedef_decl (t))
10616 return;
10618 TREE_USED (t) = true;
10621 /* Warn if there are some unused locally defined typedefs in the
10622 current function. */
10624 void
10625 maybe_warn_unused_local_typedefs (void)
10627 int i;
10628 tree decl;
10629 /* The number of times we have emitted -Wunused-local-typedefs
10630 warnings. If this is different from errorcount, that means some
10631 unrelated errors have been issued. In which case, we'll avoid
10632 emitting "unused-local-typedefs" warnings. */
10633 static int unused_local_typedefs_warn_count;
10634 struct c_language_function *l;
10636 if (cfun == NULL)
10637 return;
10639 if ((l = (struct c_language_function *) cfun->language) == NULL)
10640 return;
10642 if (warn_unused_local_typedefs
10643 && errorcount == unused_local_typedefs_warn_count)
10645 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
10646 if (!TREE_USED (decl))
10647 warning_at (DECL_SOURCE_LOCATION (decl),
10648 OPT_Wunused_local_typedefs,
10649 "typedef %qD locally defined but not used", decl);
10650 unused_local_typedefs_warn_count = errorcount;
10653 if (l->local_typedefs)
10655 VEC_free (tree, gc, l->local_typedefs);
10656 l->local_typedefs = NULL;
10660 /* The C and C++ parsers both use vectors to hold function arguments.
10661 For efficiency, we keep a cache of unused vectors. This is the
10662 cache. */
10664 typedef VEC(tree,gc)* tree_gc_vec;
10665 DEF_VEC_P(tree_gc_vec);
10666 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
10667 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
10669 /* Return a new vector from the cache. If the cache is empty,
10670 allocate a new vector. These vectors are GC'ed, so it is OK if the
10671 pointer is not released.. */
10673 VEC(tree,gc) *
10674 make_tree_vector (void)
10676 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
10677 return VEC_pop (tree_gc_vec, tree_vector_cache);
10678 else
10680 /* Passing 0 to VEC_alloc returns NULL, and our callers require
10681 that we always return a non-NULL value. The vector code uses
10682 4 when growing a NULL vector, so we do too. */
10683 return VEC_alloc (tree, gc, 4);
10687 /* Release a vector of trees back to the cache. */
10689 void
10690 release_tree_vector (VEC(tree,gc) *vec)
10692 if (vec != NULL)
10694 VEC_truncate (tree, vec, 0);
10695 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
10699 /* Get a new tree vector holding a single tree. */
10701 VEC(tree,gc) *
10702 make_tree_vector_single (tree t)
10704 VEC(tree,gc) *ret = make_tree_vector ();
10705 VEC_quick_push (tree, ret, t);
10706 return ret;
10709 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
10711 VEC(tree,gc) *
10712 make_tree_vector_from_list (tree list)
10714 VEC(tree,gc) *ret = make_tree_vector ();
10715 for (; list; list = TREE_CHAIN (list))
10716 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
10717 return ret;
10720 /* Get a new tree vector which is a copy of an existing one. */
10722 VEC(tree,gc) *
10723 make_tree_vector_copy (const VEC(tree,gc) *orig)
10725 VEC(tree,gc) *ret;
10726 unsigned int ix;
10727 tree t;
10729 ret = make_tree_vector ();
10730 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
10731 FOR_EACH_VEC_ELT (tree, orig, ix, t)
10732 VEC_quick_push (tree, ret, t);
10733 return ret;
10736 /* Return true if KEYWORD starts a type specifier. */
10738 bool
10739 keyword_begins_type_specifier (enum rid keyword)
10741 switch (keyword)
10743 case RID_INT:
10744 case RID_CHAR:
10745 case RID_FLOAT:
10746 case RID_DOUBLE:
10747 case RID_VOID:
10748 case RID_INT128:
10749 case RID_UNSIGNED:
10750 case RID_LONG:
10751 case RID_SHORT:
10752 case RID_SIGNED:
10753 case RID_DFLOAT32:
10754 case RID_DFLOAT64:
10755 case RID_DFLOAT128:
10756 case RID_FRACT:
10757 case RID_ACCUM:
10758 case RID_BOOL:
10759 case RID_WCHAR:
10760 case RID_CHAR16:
10761 case RID_CHAR32:
10762 case RID_SAT:
10763 case RID_COMPLEX:
10764 case RID_TYPEOF:
10765 case RID_STRUCT:
10766 case RID_CLASS:
10767 case RID_UNION:
10768 case RID_ENUM:
10769 return true;
10770 default:
10771 return false;
10775 /* Return true if KEYWORD names a type qualifier. */
10777 bool
10778 keyword_is_type_qualifier (enum rid keyword)
10780 switch (keyword)
10782 case RID_CONST:
10783 case RID_VOLATILE:
10784 case RID_RESTRICT:
10785 return true;
10786 default:
10787 return false;
10791 /* Return true if KEYWORD names a storage class specifier.
10793 RID_TYPEDEF is not included in this list despite `typedef' being
10794 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
10795 such for syntactic convenience only. */
10797 bool
10798 keyword_is_storage_class_specifier (enum rid keyword)
10800 switch (keyword)
10802 case RID_STATIC:
10803 case RID_EXTERN:
10804 case RID_REGISTER:
10805 case RID_AUTO:
10806 case RID_MUTABLE:
10807 case RID_THREAD:
10808 return true;
10809 default:
10810 return false;
10814 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
10816 static bool
10817 keyword_is_function_specifier (enum rid keyword)
10819 switch (keyword)
10821 case RID_INLINE:
10822 case RID_NORETURN:
10823 case RID_VIRTUAL:
10824 case RID_EXPLICIT:
10825 return true;
10826 default:
10827 return false;
10831 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
10832 declaration-specifier (C99 6.7). */
10834 bool
10835 keyword_is_decl_specifier (enum rid keyword)
10837 if (keyword_is_storage_class_specifier (keyword)
10838 || keyword_is_type_qualifier (keyword)
10839 || keyword_is_function_specifier (keyword))
10840 return true;
10842 switch (keyword)
10844 case RID_TYPEDEF:
10845 case RID_FRIEND:
10846 case RID_CONSTEXPR:
10847 return true;
10848 default:
10849 return false;
10853 /* Initialize language-specific-bits of tree_contains_struct. */
10855 void
10856 c_common_init_ts (void)
10858 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
10859 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
10862 /* Build a user-defined numeric literal out of an integer constant type VALUE
10863 with identifier SUFFIX. */
10865 tree
10866 build_userdef_literal (tree suffix_id, tree value, tree num_string)
10868 tree literal = make_node (USERDEF_LITERAL);
10869 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
10870 USERDEF_LITERAL_VALUE (literal) = value;
10871 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
10872 return literal;
10875 /* For vector[index], convert the vector to a
10876 pointer of the underlying type. */
10877 void
10878 convert_vector_to_pointer_for_subscript (location_t loc,
10879 tree* vecp, tree index)
10881 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
10883 tree type = TREE_TYPE (*vecp);
10884 tree type1;
10886 if (TREE_CODE (index) == INTEGER_CST)
10887 if (!host_integerp (index, 1)
10888 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
10889 >= TYPE_VECTOR_SUBPARTS (type)))
10890 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
10892 c_common_mark_addressable_vec (*vecp);
10893 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10894 type = build_pointer_type (type);
10895 type1 = build_pointer_type (TREE_TYPE (*vecp));
10896 *vecp = build1 (ADDR_EXPR, type1, *vecp);
10897 *vecp = convert (type, *vecp);
10901 #include "gt-c-family-c-common.h"