PR c++/18747
[official-gcc.git] / gcc / c-family / c-common.c
blob502613ae8df41de7c43647267358431142c0fd75
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "c-pragma.h"
30 #include "ggc.h"
31 #include "c-common.h"
32 #include "c-objc.h"
33 #include "tm_p.h"
34 #include "obstack.h"
35 #include "cpplib.h"
36 #include "target.h"
37 #include "common/common-target.h"
38 #include "langhooks.h"
39 #include "tree-inline.h"
40 #include "toplev.h"
41 #include "diagnostic.h"
42 #include "tree-iterator.h"
43 #include "hashtab.h"
44 #include "tree-mudflap.h"
45 #include "opts.h"
46 #include "cgraph.h"
47 #include "target-def.h"
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
51 /* The following symbols are subsumed in the c_global_trees array, and
52 listed here individually for documentation purposes.
54 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
56 tree short_integer_type_node;
57 tree long_integer_type_node;
58 tree long_long_integer_type_node;
59 tree int128_integer_type_node;
61 tree short_unsigned_type_node;
62 tree long_unsigned_type_node;
63 tree long_long_unsigned_type_node;
64 tree int128_unsigned_type_node;
66 tree truthvalue_type_node;
67 tree truthvalue_false_node;
68 tree truthvalue_true_node;
70 tree ptrdiff_type_node;
72 tree unsigned_char_type_node;
73 tree signed_char_type_node;
74 tree wchar_type_node;
76 tree char16_type_node;
77 tree char32_type_node;
79 tree float_type_node;
80 tree double_type_node;
81 tree long_double_type_node;
83 tree complex_integer_type_node;
84 tree complex_float_type_node;
85 tree complex_double_type_node;
86 tree complex_long_double_type_node;
88 tree dfloat32_type_node;
89 tree dfloat64_type_node;
90 tree_dfloat128_type_node;
92 tree intQI_type_node;
93 tree intHI_type_node;
94 tree intSI_type_node;
95 tree intDI_type_node;
96 tree intTI_type_node;
98 tree unsigned_intQI_type_node;
99 tree unsigned_intHI_type_node;
100 tree unsigned_intSI_type_node;
101 tree unsigned_intDI_type_node;
102 tree unsigned_intTI_type_node;
104 tree widest_integer_literal_type_node;
105 tree widest_unsigned_literal_type_node;
107 Nodes for types `void *' and `const void *'.
109 tree ptr_type_node, const_ptr_type_node;
111 Nodes for types `char *' and `const char *'.
113 tree string_type_node, const_string_type_node;
115 Type `char[SOMENUMBER]'.
116 Used when an array of char is needed and the size is irrelevant.
118 tree char_array_type_node;
120 Type `int[SOMENUMBER]' or something like it.
121 Used when an array of int needed and the size is irrelevant.
123 tree int_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* Warn about #pragma directives that are not recognized. */
198 int warn_unknown_pragmas; /* Tri state variable. */
200 /* Warn about format/argument anomalies in calls to formatted I/O functions
201 (*printf, *scanf, strftime, strfmon, etc.). */
203 int warn_format;
205 /* C/ObjC language option variables. */
208 /* Nonzero means allow type mismatches in conditional expressions;
209 just make their values `void'. */
211 int flag_cond_mismatch;
213 /* Nonzero means enable C89 Amendment 1 features. */
215 int flag_isoc94;
217 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
219 int flag_isoc99;
221 /* Nonzero means use the ISO C11 dialect of C. */
223 int flag_isoc11;
225 /* Nonzero means that we have builtin functions, and main is an int. */
227 int flag_hosted = 1;
230 /* ObjC language option variables. */
233 /* Tells the compiler that this is a special run. Do not perform any
234 compiling, instead we are to test some platform dependent features
235 and output a C header file with appropriate definitions. */
237 int print_struct_values;
239 /* Tells the compiler what is the constant string class for ObjC. */
241 const char *constant_string_class_name;
244 /* C++ language option variables. */
247 /* Nonzero means generate separate instantiation control files and
248 juggle them at link time. */
250 int flag_use_repository;
252 /* The C++ dialect being used. C++98 is the default. */
254 enum cxx_dialect cxx_dialect = cxx98;
256 /* Maximum template instantiation depth. This limit exists to limit the
257 time it takes to notice excessively recursive template instantiations.
259 The default is lower than the 1024 recommended by the C++0x standard
260 because G++ runs out of stack before 1024 with highly recursive template
261 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
263 int max_tinst_depth = 900;
265 /* The elements of `ridpointers' are identifier nodes for the reserved
266 type names and storage classes. It is indexed by a RID_... value. */
267 tree *ridpointers;
269 tree (*make_fname_decl) (location_t, tree, int);
271 /* Nonzero means don't warn about problems that occur when the code is
272 executed. */
273 int c_inhibit_evaluation_warnings;
275 /* Whether we are building a boolean conversion inside
276 convert_for_assignment, or some other late binary operation. If
277 build_binary_op is called for C (from code shared by C and C++) in
278 this case, then the operands have already been folded and the
279 result will not be folded again, so C_MAYBE_CONST_EXPR should not
280 be generated. */
281 bool in_late_binary_op;
283 /* Whether lexing has been completed, so subsequent preprocessor
284 errors should use the compiler's input_location. */
285 bool done_lexing = false;
287 /* Information about how a function name is generated. */
288 struct fname_var_t
290 tree *const decl; /* pointer to the VAR_DECL. */
291 const unsigned rid; /* RID number for the identifier. */
292 const int pretty; /* How pretty is it? */
295 /* The three ways of getting then name of the current function. */
297 const struct fname_var_t fname_vars[] =
299 /* C99 compliant __func__, must be first. */
300 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
301 /* GCC __FUNCTION__ compliant. */
302 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
303 /* GCC __PRETTY_FUNCTION__ compliant. */
304 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
305 {NULL, 0, 0},
308 /* Global visibility options. */
309 struct visibility_flags visibility_options;
311 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
312 static tree check_case_value (tree);
313 static bool check_case_bounds (tree, tree, tree *, tree *);
315 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_always_inline_attribute (tree *, tree, tree, int,
325 bool *);
326 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_transparent_union_attribute (tree *, tree, tree,
336 int, bool *);
337 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
344 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_visibility_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_tls_model_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_no_instrument_function_attribute (tree *, tree,
352 tree, int, bool *);
353 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_deprecated_attribute (tree *, tree, tree, int,
362 bool *);
363 static tree handle_vector_size_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
369 bool *);
370 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
375 static tree ignore_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
379 static void check_function_nonnull (tree, int, tree *);
380 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
381 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
382 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
383 static int resort_field_decl_cmp (const void *, const void *);
385 /* Reserved words. The third field is a mask: keywords are disabled
386 if they match the mask.
388 Masks for languages:
389 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
390 C --std=c99: D_CXXONLY | D_OBJC
391 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
392 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
393 C++ --std=c0x: D_CONLY | D_OBJC
394 ObjC++ is like C++ except that D_OBJC is not set
396 If -fno-asm is used, D_ASM is added to the mask. If
397 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
398 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
399 In C with -Wc++-compat, we warn if D_CXXWARN is set.
401 Note the complication of the D_CXX_OBJC keywords. These are
402 reserved words such as 'class'. In C++, 'class' is a reserved
403 word. In Objective-C++ it is too. In Objective-C, it is a
404 reserved word too, but only if it follows an '@' sign.
406 const struct c_common_resword c_common_reswords[] =
408 { "_Alignas", RID_ALIGNAS, D_CONLY },
409 { "_Alignof", RID_ALIGNOF, D_CONLY },
410 { "_Bool", RID_BOOL, D_CONLY },
411 { "_Complex", RID_COMPLEX, 0 },
412 { "_Imaginary", RID_IMAGINARY, D_CONLY },
413 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
414 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
415 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
416 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
417 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
418 { "_Sat", RID_SAT, D_CONLY | D_EXT },
419 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
420 { "_Noreturn", RID_NORETURN, D_CONLY },
421 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
422 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
423 { "__alignof", RID_ALIGNOF, 0 },
424 { "__alignof__", RID_ALIGNOF, 0 },
425 { "__asm", RID_ASM, 0 },
426 { "__asm__", RID_ASM, 0 },
427 { "__attribute", RID_ATTRIBUTE, 0 },
428 { "__attribute__", RID_ATTRIBUTE, 0 },
429 { "__bases", RID_BASES, D_CXXONLY },
430 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
431 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
432 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
433 { "__builtin_offsetof", RID_OFFSETOF, 0 },
434 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
435 { "__builtin_va_arg", RID_VA_ARG, 0 },
436 { "__complex", RID_COMPLEX, 0 },
437 { "__complex__", RID_COMPLEX, 0 },
438 { "__const", RID_CONST, 0 },
439 { "__const__", RID_CONST, 0 },
440 { "__decltype", RID_DECLTYPE, D_CXXONLY },
441 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
442 { "__extension__", RID_EXTENSION, 0 },
443 { "__func__", RID_C99_FUNCTION_NAME, 0 },
444 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
445 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
446 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
447 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
448 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
449 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
450 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
451 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
452 { "__imag", RID_IMAGPART, 0 },
453 { "__imag__", RID_IMAGPART, 0 },
454 { "__inline", RID_INLINE, 0 },
455 { "__inline__", RID_INLINE, 0 },
456 { "__int128", RID_INT128, 0 },
457 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
458 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
459 { "__is_class", RID_IS_CLASS, D_CXXONLY },
460 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
461 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
462 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
463 { "__is_final", RID_IS_FINAL, D_CXXONLY },
464 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
465 { "__is_pod", RID_IS_POD, D_CXXONLY },
466 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
467 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
468 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
469 { "__is_union", RID_IS_UNION, D_CXXONLY },
470 { "__label__", RID_LABEL, 0 },
471 { "__null", RID_NULL, 0 },
472 { "__real", RID_REALPART, 0 },
473 { "__real__", RID_REALPART, 0 },
474 { "__restrict", RID_RESTRICT, 0 },
475 { "__restrict__", RID_RESTRICT, 0 },
476 { "__signed", RID_SIGNED, 0 },
477 { "__signed__", RID_SIGNED, 0 },
478 { "__thread", RID_THREAD, 0 },
479 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
480 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
481 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
482 { "__typeof", RID_TYPEOF, 0 },
483 { "__typeof__", RID_TYPEOF, 0 },
484 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
485 { "__volatile", RID_VOLATILE, 0 },
486 { "__volatile__", RID_VOLATILE, 0 },
487 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
488 { "asm", RID_ASM, D_ASM },
489 { "auto", RID_AUTO, 0 },
490 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
491 { "break", RID_BREAK, 0 },
492 { "case", RID_CASE, 0 },
493 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
494 { "char", RID_CHAR, 0 },
495 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
496 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
497 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
498 { "const", RID_CONST, 0 },
499 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
500 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
501 { "continue", RID_CONTINUE, 0 },
502 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
503 { "default", RID_DEFAULT, 0 },
504 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
505 { "do", RID_DO, 0 },
506 { "double", RID_DOUBLE, 0 },
507 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
508 { "else", RID_ELSE, 0 },
509 { "enum", RID_ENUM, 0 },
510 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
511 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
512 { "extern", RID_EXTERN, 0 },
513 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
514 { "float", RID_FLOAT, 0 },
515 { "for", RID_FOR, 0 },
516 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
517 { "goto", RID_GOTO, 0 },
518 { "if", RID_IF, 0 },
519 { "inline", RID_INLINE, D_EXT89 },
520 { "int", RID_INT, 0 },
521 { "long", RID_LONG, 0 },
522 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
523 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
524 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
525 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
526 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
528 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
529 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
530 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
531 { "register", RID_REGISTER, 0 },
532 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
533 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
534 { "return", RID_RETURN, 0 },
535 { "short", RID_SHORT, 0 },
536 { "signed", RID_SIGNED, 0 },
537 { "sizeof", RID_SIZEOF, 0 },
538 { "static", RID_STATIC, 0 },
539 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
541 { "struct", RID_STRUCT, 0 },
542 { "switch", RID_SWITCH, 0 },
543 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
544 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
545 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
546 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
547 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
548 { "typedef", RID_TYPEDEF, 0 },
549 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
550 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
551 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
552 { "union", RID_UNION, 0 },
553 { "unsigned", RID_UNSIGNED, 0 },
554 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
555 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
556 { "void", RID_VOID, 0 },
557 { "volatile", RID_VOLATILE, 0 },
558 { "wchar_t", RID_WCHAR, D_CXXONLY },
559 { "while", RID_WHILE, 0 },
560 /* These Objective-C keywords are recognized only immediately after
561 an '@'. */
562 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
563 { "defs", RID_AT_DEFS, D_OBJC },
564 { "encode", RID_AT_ENCODE, D_OBJC },
565 { "end", RID_AT_END, D_OBJC },
566 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
567 { "interface", RID_AT_INTERFACE, D_OBJC },
568 { "protocol", RID_AT_PROTOCOL, D_OBJC },
569 { "selector", RID_AT_SELECTOR, D_OBJC },
570 { "finally", RID_AT_FINALLY, D_OBJC },
571 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
572 { "optional", RID_AT_OPTIONAL, D_OBJC },
573 { "required", RID_AT_REQUIRED, D_OBJC },
574 { "property", RID_AT_PROPERTY, D_OBJC },
575 { "package", RID_AT_PACKAGE, D_OBJC },
576 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
577 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
578 /* These are recognized only in protocol-qualifier context
579 (see above) */
580 { "bycopy", RID_BYCOPY, D_OBJC },
581 { "byref", RID_BYREF, D_OBJC },
582 { "in", RID_IN, D_OBJC },
583 { "inout", RID_INOUT, D_OBJC },
584 { "oneway", RID_ONEWAY, D_OBJC },
585 { "out", RID_OUT, D_OBJC },
586 /* These are recognized inside a property attribute list */
587 { "assign", RID_ASSIGN, D_OBJC },
588 { "copy", RID_COPY, D_OBJC },
589 { "getter", RID_GETTER, D_OBJC },
590 { "nonatomic", RID_NONATOMIC, D_OBJC },
591 { "readonly", RID_READONLY, D_OBJC },
592 { "readwrite", RID_READWRITE, D_OBJC },
593 { "retain", RID_RETAIN, D_OBJC },
594 { "setter", RID_SETTER, D_OBJC },
597 const unsigned int num_c_common_reswords =
598 sizeof c_common_reswords / sizeof (struct c_common_resword);
600 /* Table of machine-independent attributes common to all C-like languages. */
601 const struct attribute_spec c_common_attribute_table[] =
603 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
604 affects_type_identity } */
605 { "packed", 0, 0, false, false, false,
606 handle_packed_attribute , false},
607 { "nocommon", 0, 0, true, false, false,
608 handle_nocommon_attribute, false},
609 { "common", 0, 0, true, false, false,
610 handle_common_attribute, false },
611 /* FIXME: logically, noreturn attributes should be listed as
612 "false, true, true" and apply to function types. But implementing this
613 would require all the places in the compiler that use TREE_THIS_VOLATILE
614 on a decl to identify non-returning functions to be located and fixed
615 to check the function type instead. */
616 { "noreturn", 0, 0, true, false, false,
617 handle_noreturn_attribute, false },
618 { "volatile", 0, 0, true, false, false,
619 handle_noreturn_attribute, false },
620 { "noinline", 0, 0, true, false, false,
621 handle_noinline_attribute, false },
622 { "noclone", 0, 0, true, false, false,
623 handle_noclone_attribute, false },
624 { "leaf", 0, 0, true, false, false,
625 handle_leaf_attribute, false },
626 { "always_inline", 0, 0, true, false, false,
627 handle_always_inline_attribute, false },
628 { "gnu_inline", 0, 0, true, false, false,
629 handle_gnu_inline_attribute, false },
630 { "artificial", 0, 0, true, false, false,
631 handle_artificial_attribute, false },
632 { "flatten", 0, 0, true, false, false,
633 handle_flatten_attribute, false },
634 { "used", 0, 0, true, false, false,
635 handle_used_attribute, false },
636 { "unused", 0, 0, false, false, false,
637 handle_unused_attribute, false },
638 { "externally_visible", 0, 0, true, false, false,
639 handle_externally_visible_attribute, false },
640 /* The same comments as for noreturn attributes apply to const ones. */
641 { "const", 0, 0, true, false, false,
642 handle_const_attribute, false },
643 { "transparent_union", 0, 0, false, false, false,
644 handle_transparent_union_attribute, false },
645 { "constructor", 0, 1, true, false, false,
646 handle_constructor_attribute, false },
647 { "destructor", 0, 1, true, false, false,
648 handle_destructor_attribute, false },
649 { "mode", 1, 1, false, true, false,
650 handle_mode_attribute, false },
651 { "section", 1, 1, true, false, false,
652 handle_section_attribute, false },
653 { "aligned", 0, 1, false, false, false,
654 handle_aligned_attribute, false },
655 { "weak", 0, 0, true, false, false,
656 handle_weak_attribute, false },
657 { "ifunc", 1, 1, true, false, false,
658 handle_ifunc_attribute, false },
659 { "alias", 1, 1, true, false, false,
660 handle_alias_attribute, false },
661 { "weakref", 0, 1, true, false, false,
662 handle_weakref_attribute, false },
663 { "no_instrument_function", 0, 0, true, false, false,
664 handle_no_instrument_function_attribute,
665 false },
666 { "malloc", 0, 0, true, false, false,
667 handle_malloc_attribute, false },
668 { "returns_twice", 0, 0, true, false, false,
669 handle_returns_twice_attribute, false },
670 { "no_stack_limit", 0, 0, true, false, false,
671 handle_no_limit_stack_attribute, false },
672 { "pure", 0, 0, true, false, false,
673 handle_pure_attribute, false },
674 { "transaction_callable", 0, 0, false, true, false,
675 handle_tm_attribute, false },
676 { "transaction_unsafe", 0, 0, false, true, false,
677 handle_tm_attribute, false },
678 { "transaction_safe", 0, 0, false, true, false,
679 handle_tm_attribute, false },
680 { "transaction_may_cancel_outer", 0, 0, false, true, false,
681 handle_tm_attribute, false },
682 /* ??? These two attributes didn't make the transition from the
683 Intel language document to the multi-vendor language document. */
684 { "transaction_pure", 0, 0, false, true, false,
685 handle_tm_attribute, false },
686 { "transaction_wrap", 1, 1, true, false, false,
687 handle_tm_wrap_attribute, false },
688 /* For internal use (marking of builtins) only. The name contains space
689 to prevent its usage in source code. */
690 { "no vops", 0, 0, true, false, false,
691 handle_novops_attribute, false },
692 { "deprecated", 0, 1, false, false, false,
693 handle_deprecated_attribute, false },
694 { "vector_size", 1, 1, false, true, false,
695 handle_vector_size_attribute, false },
696 { "visibility", 1, 1, false, false, false,
697 handle_visibility_attribute, false },
698 { "tls_model", 1, 1, true, false, false,
699 handle_tls_model_attribute, false },
700 { "nonnull", 0, -1, false, true, true,
701 handle_nonnull_attribute, false },
702 { "nothrow", 0, 0, true, false, false,
703 handle_nothrow_attribute, false },
704 { "may_alias", 0, 0, false, true, false, NULL, false },
705 { "cleanup", 1, 1, true, false, false,
706 handle_cleanup_attribute, false },
707 { "warn_unused_result", 0, 0, false, true, true,
708 handle_warn_unused_result_attribute, false },
709 { "sentinel", 0, 1, false, true, true,
710 handle_sentinel_attribute, false },
711 /* For internal use (marking of builtins) only. The name contains space
712 to prevent its usage in source code. */
713 { "type generic", 0, 0, false, true, true,
714 handle_type_generic_attribute, false },
715 { "alloc_size", 1, 2, false, true, true,
716 handle_alloc_size_attribute, false },
717 { "cold", 0, 0, true, false, false,
718 handle_cold_attribute, false },
719 { "hot", 0, 0, true, false, false,
720 handle_hot_attribute, false },
721 { "warning", 1, 1, true, false, false,
722 handle_error_attribute, false },
723 { "error", 1, 1, true, false, false,
724 handle_error_attribute, false },
725 { "target", 1, -1, true, false, false,
726 handle_target_attribute, false },
727 { "optimize", 1, -1, true, false, false,
728 handle_optimize_attribute, false },
729 /* For internal use only. The leading '*' both prevents its usage in
730 source code and signals that it may be overridden by machine tables. */
731 { "*tm regparm", 0, 0, false, true, true,
732 ignore_attribute, false },
733 { "no_split_stack", 0, 0, true, false, false,
734 handle_no_split_stack_attribute, false },
735 /* For internal use (marking of builtins and runtime functions) only.
736 The name contains space to prevent its usage in source code. */
737 { "fn spec", 1, 1, false, true, true,
738 handle_fnspec_attribute, false },
739 { NULL, 0, 0, false, false, false, NULL, false }
742 /* Give the specifications for the format attributes, used by C and all
743 descendants. */
745 const struct attribute_spec c_common_format_attribute_table[] =
747 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
748 affects_type_identity } */
749 { "format", 3, 3, false, true, true,
750 handle_format_attribute, false },
751 { "format_arg", 1, 1, false, true, true,
752 handle_format_arg_attribute, false },
753 { NULL, 0, 0, false, false, false, NULL, false }
756 /* Return identifier for address space AS. */
758 const char *
759 c_addr_space_name (addr_space_t as)
761 int rid = RID_FIRST_ADDR_SPACE + as;
762 gcc_assert (ridpointers [rid]);
763 return IDENTIFIER_POINTER (ridpointers [rid]);
766 /* Push current bindings for the function name VAR_DECLS. */
768 void
769 start_fname_decls (void)
771 unsigned ix;
772 tree saved = NULL_TREE;
774 for (ix = 0; fname_vars[ix].decl; ix++)
776 tree decl = *fname_vars[ix].decl;
778 if (decl)
780 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
781 saved);
782 *fname_vars[ix].decl = NULL_TREE;
785 if (saved || saved_function_name_decls)
786 /* Normally they'll have been NULL, so only push if we've got a
787 stack, or they are non-NULL. */
788 saved_function_name_decls = tree_cons (saved, NULL_TREE,
789 saved_function_name_decls);
792 /* Finish up the current bindings, adding them into the current function's
793 statement tree. This must be done _before_ finish_stmt_tree is called.
794 If there is no current function, we must be at file scope and no statements
795 are involved. Pop the previous bindings. */
797 void
798 finish_fname_decls (void)
800 unsigned ix;
801 tree stmts = NULL_TREE;
802 tree stack = saved_function_name_decls;
804 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
805 append_to_statement_list (TREE_VALUE (stack), &stmts);
807 if (stmts)
809 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
811 if (TREE_CODE (*bodyp) == BIND_EXPR)
812 bodyp = &BIND_EXPR_BODY (*bodyp);
814 append_to_statement_list_force (*bodyp, &stmts);
815 *bodyp = stmts;
818 for (ix = 0; fname_vars[ix].decl; ix++)
819 *fname_vars[ix].decl = NULL_TREE;
821 if (stack)
823 /* We had saved values, restore them. */
824 tree saved;
826 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
828 tree decl = TREE_PURPOSE (saved);
829 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
831 *fname_vars[ix].decl = decl;
833 stack = TREE_CHAIN (stack);
835 saved_function_name_decls = stack;
838 /* Return the text name of the current function, suitably prettified
839 by PRETTY_P. Return string must be freed by caller. */
841 const char *
842 fname_as_string (int pretty_p)
844 const char *name = "top level";
845 char *namep;
846 int vrb = 2, len;
847 cpp_string cstr = { 0, 0 }, strname;
849 if (!pretty_p)
851 name = "";
852 vrb = 0;
855 if (current_function_decl)
856 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
858 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
860 namep = XNEWVEC (char, len);
861 snprintf (namep, len, "\"%s\"", name);
862 strname.text = (unsigned char *) namep;
863 strname.len = len - 1;
865 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
867 XDELETEVEC (namep);
868 return (const char *) cstr.text;
871 return namep;
874 /* Return the VAR_DECL for a const char array naming the current
875 function. If the VAR_DECL has not yet been created, create it
876 now. RID indicates how it should be formatted and IDENTIFIER_NODE
877 ID is its name (unfortunately C and C++ hold the RID values of
878 keywords in different places, so we can't derive RID from ID in
879 this language independent code. LOC is the location of the
880 function. */
882 tree
883 fname_decl (location_t loc, unsigned int rid, tree id)
885 unsigned ix;
886 tree decl = NULL_TREE;
888 for (ix = 0; fname_vars[ix].decl; ix++)
889 if (fname_vars[ix].rid == rid)
890 break;
892 decl = *fname_vars[ix].decl;
893 if (!decl)
895 /* If a tree is built here, it would normally have the lineno of
896 the current statement. Later this tree will be moved to the
897 beginning of the function and this line number will be wrong.
898 To avoid this problem set the lineno to 0 here; that prevents
899 it from appearing in the RTL. */
900 tree stmts;
901 location_t saved_location = input_location;
902 input_location = UNKNOWN_LOCATION;
904 stmts = push_stmt_list ();
905 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
906 stmts = pop_stmt_list (stmts);
907 if (!IS_EMPTY_STMT (stmts))
908 saved_function_name_decls
909 = tree_cons (decl, stmts, saved_function_name_decls);
910 *fname_vars[ix].decl = decl;
911 input_location = saved_location;
913 if (!ix && !current_function_decl)
914 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
916 return decl;
919 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
921 tree
922 fix_string_type (tree value)
924 int length = TREE_STRING_LENGTH (value);
925 int nchars;
926 tree e_type, i_type, a_type;
928 /* Compute the number of elements, for the array type. */
929 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
931 nchars = length;
932 e_type = char_type_node;
934 else if (TREE_TYPE (value) == char16_array_type_node)
936 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
937 e_type = char16_type_node;
939 else if (TREE_TYPE (value) == char32_array_type_node)
941 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
942 e_type = char32_type_node;
944 else
946 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
947 e_type = wchar_type_node;
950 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
951 limit in C++98 Annex B is very large (65536) and is not normative,
952 so we do not diagnose it (warn_overlength_strings is forced off
953 in c_common_post_options). */
954 if (warn_overlength_strings)
956 const int nchars_max = flag_isoc99 ? 4095 : 509;
957 const int relevant_std = flag_isoc99 ? 99 : 90;
958 if (nchars - 1 > nchars_max)
959 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
960 separate the %d from the 'C'. 'ISO' should not be
961 translated, but it may be moved after 'C%d' in languages
962 where modifiers follow nouns. */
963 pedwarn (input_location, OPT_Woverlength_strings,
964 "string length %qd is greater than the length %qd "
965 "ISO C%d compilers are required to support",
966 nchars - 1, nchars_max, relevant_std);
969 /* Create the array type for the string constant. The ISO C++
970 standard says that a string literal has type `const char[N]' or
971 `const wchar_t[N]'. We use the same logic when invoked as a C
972 front-end with -Wwrite-strings.
973 ??? We should change the type of an expression depending on the
974 state of a warning flag. We should just be warning -- see how
975 this is handled in the C++ front-end for the deprecated implicit
976 conversion from string literals to `char*' or `wchar_t*'.
978 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
979 array type being the unqualified version of that type.
980 Therefore, if we are constructing an array of const char, we must
981 construct the matching unqualified array type first. The C front
982 end does not require this, but it does no harm, so we do it
983 unconditionally. */
984 i_type = build_index_type (size_int (nchars - 1));
985 a_type = build_array_type (e_type, i_type);
986 if (c_dialect_cxx() || warn_write_strings)
987 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
989 TREE_TYPE (value) = a_type;
990 TREE_CONSTANT (value) = 1;
991 TREE_READONLY (value) = 1;
992 TREE_STATIC (value) = 1;
993 return value;
996 /* If DISABLE is true, stop issuing warnings. This is used when
997 parsing code that we know will not be executed. This function may
998 be called multiple times, and works as a stack. */
1000 static void
1001 c_disable_warnings (bool disable)
1003 if (disable)
1005 ++c_inhibit_evaluation_warnings;
1006 fold_defer_overflow_warnings ();
1010 /* If ENABLE is true, reenable issuing warnings. */
1012 static void
1013 c_enable_warnings (bool enable)
1015 if (enable)
1017 --c_inhibit_evaluation_warnings;
1018 fold_undefer_and_ignore_overflow_warnings ();
1022 /* Fully fold EXPR, an expression that was not folded (beyond integer
1023 constant expressions and null pointer constants) when being built
1024 up. If IN_INIT, this is in a static initializer and certain
1025 changes are made to the folding done. Clear *MAYBE_CONST if
1026 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1027 expression because it contains an evaluated operator (in C99) or an
1028 operator outside of sizeof returning an integer constant (in C90)
1029 not permitted in constant expressions, or because it contains an
1030 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1031 set to true by callers before calling this function.) Return the
1032 folded expression. Function arguments have already been folded
1033 before calling this function, as have the contents of SAVE_EXPR,
1034 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1035 C_MAYBE_CONST_EXPR. */
1037 tree
1038 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1040 tree ret;
1041 tree eptype = NULL_TREE;
1042 bool dummy = true;
1043 bool maybe_const_itself = true;
1044 location_t loc = EXPR_LOCATION (expr);
1046 /* This function is not relevant to C++ because C++ folds while
1047 parsing, and may need changes to be correct for C++ when C++
1048 stops folding while parsing. */
1049 if (c_dialect_cxx ())
1050 gcc_unreachable ();
1052 if (!maybe_const)
1053 maybe_const = &dummy;
1054 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1056 eptype = TREE_TYPE (expr);
1057 expr = TREE_OPERAND (expr, 0);
1059 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1060 &maybe_const_itself);
1061 if (eptype)
1062 ret = fold_convert_loc (loc, eptype, ret);
1063 *maybe_const &= maybe_const_itself;
1064 return ret;
1067 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1068 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1069 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1070 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1071 both evaluated and unevaluated subexpressions while
1072 *MAYBE_CONST_ITSELF is carried from only evaluated
1073 subexpressions). */
1075 static tree
1076 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1077 bool *maybe_const_itself)
1079 tree ret = expr;
1080 enum tree_code code = TREE_CODE (expr);
1081 enum tree_code_class kind = TREE_CODE_CLASS (code);
1082 location_t loc = EXPR_LOCATION (expr);
1083 tree op0, op1, op2, op3;
1084 tree orig_op0, orig_op1, orig_op2;
1085 bool op0_const = true, op1_const = true, op2_const = true;
1086 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1087 bool nowarning = TREE_NO_WARNING (expr);
1088 bool unused_p;
1090 /* This function is not relevant to C++ because C++ folds while
1091 parsing, and may need changes to be correct for C++ when C++
1092 stops folding while parsing. */
1093 if (c_dialect_cxx ())
1094 gcc_unreachable ();
1096 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1097 anything else not counted as an expression cannot usefully be
1098 folded further at this point. */
1099 if (!IS_EXPR_CODE_CLASS (kind)
1100 || kind == tcc_statement
1101 || code == SAVE_EXPR)
1102 return expr;
1104 /* Operands of variable-length expressions (function calls) have
1105 already been folded, as have __builtin_* function calls, and such
1106 expressions cannot occur in constant expressions. */
1107 if (kind == tcc_vl_exp)
1109 *maybe_const_operands = false;
1110 ret = fold (expr);
1111 goto out;
1114 if (code == C_MAYBE_CONST_EXPR)
1116 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1117 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1118 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1119 *maybe_const_operands = false;
1120 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1121 *maybe_const_itself = false;
1122 if (pre && !in_init)
1123 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1124 else
1125 ret = inner;
1126 goto out;
1129 /* Assignment, increment, decrement, function call and comma
1130 operators, and statement expressions, cannot occur in constant
1131 expressions if evaluated / outside of sizeof. (Function calls
1132 were handled above, though VA_ARG_EXPR is treated like a function
1133 call here, and statement expressions are handled through
1134 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1135 switch (code)
1137 case MODIFY_EXPR:
1138 case PREDECREMENT_EXPR:
1139 case PREINCREMENT_EXPR:
1140 case POSTDECREMENT_EXPR:
1141 case POSTINCREMENT_EXPR:
1142 case COMPOUND_EXPR:
1143 *maybe_const_operands = false;
1144 break;
1146 case VA_ARG_EXPR:
1147 case TARGET_EXPR:
1148 case BIND_EXPR:
1149 case OBJ_TYPE_REF:
1150 *maybe_const_operands = false;
1151 ret = fold (expr);
1152 goto out;
1154 default:
1155 break;
1158 /* Fold individual tree codes as appropriate. */
1159 switch (code)
1161 case COMPOUND_LITERAL_EXPR:
1162 /* Any non-constancy will have been marked in a containing
1163 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1164 goto out;
1166 case COMPONENT_REF:
1167 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1168 op1 = TREE_OPERAND (expr, 1);
1169 op2 = TREE_OPERAND (expr, 2);
1170 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1171 maybe_const_itself);
1172 STRIP_TYPE_NOPS (op0);
1173 if (op0 != orig_op0)
1174 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1175 if (ret != expr)
1177 TREE_READONLY (ret) = TREE_READONLY (expr);
1178 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1180 goto out;
1182 case ARRAY_REF:
1183 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1184 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1185 op2 = TREE_OPERAND (expr, 2);
1186 op3 = TREE_OPERAND (expr, 3);
1187 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1188 maybe_const_itself);
1189 STRIP_TYPE_NOPS (op0);
1190 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1191 maybe_const_itself);
1192 STRIP_TYPE_NOPS (op1);
1193 op1 = decl_constant_value_for_optimization (op1);
1194 if (op0 != orig_op0 || op1 != orig_op1)
1195 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1196 if (ret != expr)
1198 TREE_READONLY (ret) = TREE_READONLY (expr);
1199 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1200 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1202 ret = fold (ret);
1203 goto out;
1205 case COMPOUND_EXPR:
1206 case MODIFY_EXPR:
1207 case PREDECREMENT_EXPR:
1208 case PREINCREMENT_EXPR:
1209 case POSTDECREMENT_EXPR:
1210 case POSTINCREMENT_EXPR:
1211 case PLUS_EXPR:
1212 case MINUS_EXPR:
1213 case MULT_EXPR:
1214 case POINTER_PLUS_EXPR:
1215 case TRUNC_DIV_EXPR:
1216 case CEIL_DIV_EXPR:
1217 case FLOOR_DIV_EXPR:
1218 case TRUNC_MOD_EXPR:
1219 case RDIV_EXPR:
1220 case EXACT_DIV_EXPR:
1221 case LSHIFT_EXPR:
1222 case RSHIFT_EXPR:
1223 case BIT_IOR_EXPR:
1224 case BIT_XOR_EXPR:
1225 case BIT_AND_EXPR:
1226 case LT_EXPR:
1227 case LE_EXPR:
1228 case GT_EXPR:
1229 case GE_EXPR:
1230 case EQ_EXPR:
1231 case NE_EXPR:
1232 case COMPLEX_EXPR:
1233 case TRUTH_AND_EXPR:
1234 case TRUTH_OR_EXPR:
1235 case TRUTH_XOR_EXPR:
1236 case UNORDERED_EXPR:
1237 case ORDERED_EXPR:
1238 case UNLT_EXPR:
1239 case UNLE_EXPR:
1240 case UNGT_EXPR:
1241 case UNGE_EXPR:
1242 case UNEQ_EXPR:
1243 /* Binary operations evaluating both arguments (increment and
1244 decrement are binary internally in GCC). */
1245 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1246 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1247 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1248 maybe_const_itself);
1249 STRIP_TYPE_NOPS (op0);
1250 if (code != MODIFY_EXPR
1251 && code != PREDECREMENT_EXPR
1252 && code != PREINCREMENT_EXPR
1253 && code != POSTDECREMENT_EXPR
1254 && code != POSTINCREMENT_EXPR)
1255 op0 = decl_constant_value_for_optimization (op0);
1256 /* The RHS of a MODIFY_EXPR was fully folded when building that
1257 expression for the sake of conversion warnings. */
1258 if (code != MODIFY_EXPR)
1259 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1260 maybe_const_itself);
1261 STRIP_TYPE_NOPS (op1);
1262 op1 = decl_constant_value_for_optimization (op1);
1263 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1264 ret = in_init
1265 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1266 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1267 else
1268 ret = fold (expr);
1269 if (TREE_OVERFLOW_P (ret)
1270 && !TREE_OVERFLOW_P (op0)
1271 && !TREE_OVERFLOW_P (op1))
1272 overflow_warning (EXPR_LOCATION (expr), ret);
1273 goto out;
1275 case INDIRECT_REF:
1276 case FIX_TRUNC_EXPR:
1277 case FLOAT_EXPR:
1278 CASE_CONVERT:
1279 case VIEW_CONVERT_EXPR:
1280 case NON_LVALUE_EXPR:
1281 case NEGATE_EXPR:
1282 case BIT_NOT_EXPR:
1283 case TRUTH_NOT_EXPR:
1284 case ADDR_EXPR:
1285 case CONJ_EXPR:
1286 case REALPART_EXPR:
1287 case IMAGPART_EXPR:
1288 /* Unary operations. */
1289 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1290 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1291 maybe_const_itself);
1292 STRIP_TYPE_NOPS (op0);
1293 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1294 op0 = decl_constant_value_for_optimization (op0);
1295 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1296 not prepared to deal with them if they occur in initializers. */
1297 if (op0 != orig_op0
1298 && code == ADDR_EXPR
1299 && (op1 = get_base_address (op0)) != NULL_TREE
1300 && TREE_CODE (op1) == INDIRECT_REF
1301 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1302 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1303 else if (op0 != orig_op0 || in_init)
1304 ret = in_init
1305 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1306 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1307 else
1308 ret = fold (expr);
1309 if (code == INDIRECT_REF
1310 && ret != expr
1311 && TREE_CODE (ret) == INDIRECT_REF)
1313 TREE_READONLY (ret) = TREE_READONLY (expr);
1314 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1315 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1317 switch (code)
1319 case FIX_TRUNC_EXPR:
1320 case FLOAT_EXPR:
1321 CASE_CONVERT:
1322 /* Don't warn about explicit conversions. We will already
1323 have warned about suspect implicit conversions. */
1324 break;
1326 default:
1327 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1328 overflow_warning (EXPR_LOCATION (expr), ret);
1329 break;
1331 goto out;
1333 case TRUTH_ANDIF_EXPR:
1334 case TRUTH_ORIF_EXPR:
1335 /* Binary operations not necessarily evaluating both
1336 arguments. */
1337 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1338 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1339 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1340 STRIP_TYPE_NOPS (op0);
1342 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1343 ? truthvalue_false_node
1344 : truthvalue_true_node));
1345 c_disable_warnings (unused_p);
1346 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1347 STRIP_TYPE_NOPS (op1);
1348 c_enable_warnings (unused_p);
1350 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1351 ret = in_init
1352 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1353 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1354 else
1355 ret = fold (expr);
1356 *maybe_const_operands &= op0_const;
1357 *maybe_const_itself &= op0_const_self;
1358 if (!(flag_isoc99
1359 && op0_const
1360 && op0_const_self
1361 && (code == TRUTH_ANDIF_EXPR
1362 ? op0 == truthvalue_false_node
1363 : op0 == truthvalue_true_node)))
1364 *maybe_const_operands &= op1_const;
1365 if (!(op0_const
1366 && op0_const_self
1367 && (code == TRUTH_ANDIF_EXPR
1368 ? op0 == truthvalue_false_node
1369 : op0 == truthvalue_true_node)))
1370 *maybe_const_itself &= op1_const_self;
1371 goto out;
1373 case COND_EXPR:
1374 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1375 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1376 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1377 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1379 STRIP_TYPE_NOPS (op0);
1380 c_disable_warnings (op0 == truthvalue_false_node);
1381 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1382 STRIP_TYPE_NOPS (op1);
1383 c_enable_warnings (op0 == truthvalue_false_node);
1385 c_disable_warnings (op0 == truthvalue_true_node);
1386 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1387 STRIP_TYPE_NOPS (op2);
1388 c_enable_warnings (op0 == truthvalue_true_node);
1390 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1391 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1392 else
1393 ret = fold (expr);
1394 *maybe_const_operands &= op0_const;
1395 *maybe_const_itself &= op0_const_self;
1396 if (!(flag_isoc99
1397 && op0_const
1398 && op0_const_self
1399 && op0 == truthvalue_false_node))
1400 *maybe_const_operands &= op1_const;
1401 if (!(op0_const
1402 && op0_const_self
1403 && op0 == truthvalue_false_node))
1404 *maybe_const_itself &= op1_const_self;
1405 if (!(flag_isoc99
1406 && op0_const
1407 && op0_const_self
1408 && op0 == truthvalue_true_node))
1409 *maybe_const_operands &= op2_const;
1410 if (!(op0_const
1411 && op0_const_self
1412 && op0 == truthvalue_true_node))
1413 *maybe_const_itself &= op2_const_self;
1414 goto out;
1416 case EXCESS_PRECISION_EXPR:
1417 /* Each case where an operand with excess precision may be
1418 encountered must remove the EXCESS_PRECISION_EXPR around
1419 inner operands and possibly put one around the whole
1420 expression or possibly convert to the semantic type (which
1421 c_fully_fold does); we cannot tell at this stage which is
1422 appropriate in any particular case. */
1423 gcc_unreachable ();
1425 default:
1426 /* Various codes may appear through folding built-in functions
1427 and their arguments. */
1428 goto out;
1431 out:
1432 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1433 have been done by this point, so remove them again. */
1434 nowarning |= TREE_NO_WARNING (ret);
1435 STRIP_TYPE_NOPS (ret);
1436 if (nowarning && !TREE_NO_WARNING (ret))
1438 if (!CAN_HAVE_LOCATION_P (ret))
1439 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1440 TREE_NO_WARNING (ret) = 1;
1442 if (ret != expr)
1443 protected_set_expr_location (ret, loc);
1444 return ret;
1447 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1448 return EXP. Otherwise, return either EXP or its known constant
1449 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1450 Is the BLKmode test appropriate? */
1452 tree
1453 decl_constant_value_for_optimization (tree exp)
1455 tree ret;
1457 /* This function is only used by C, for c_fully_fold and other
1458 optimization, and may not be correct for C++. */
1459 if (c_dialect_cxx ())
1460 gcc_unreachable ();
1462 if (!optimize
1463 || TREE_CODE (exp) != VAR_DECL
1464 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1465 || DECL_MODE (exp) == BLKmode)
1466 return exp;
1468 ret = decl_constant_value (exp);
1469 /* Avoid unwanted tree sharing between the initializer and current
1470 function's body where the tree can be modified e.g. by the
1471 gimplifier. */
1472 if (ret != exp && TREE_STATIC (exp))
1473 ret = unshare_expr (ret);
1474 return ret;
1477 /* Print a warning if a constant expression had overflow in folding.
1478 Invoke this function on every expression that the language
1479 requires to be a constant expression.
1480 Note the ANSI C standard says it is erroneous for a
1481 constant expression to overflow. */
1483 void
1484 constant_expression_warning (tree value)
1486 if (warn_overflow && pedantic
1487 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1488 || TREE_CODE (value) == FIXED_CST
1489 || TREE_CODE (value) == VECTOR_CST
1490 || TREE_CODE (value) == COMPLEX_CST)
1491 && TREE_OVERFLOW (value))
1492 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1495 /* The same as above but print an unconditional error. */
1496 void
1497 constant_expression_error (tree value)
1499 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1500 || TREE_CODE (value) == FIXED_CST
1501 || TREE_CODE (value) == VECTOR_CST
1502 || TREE_CODE (value) == COMPLEX_CST)
1503 && TREE_OVERFLOW (value))
1504 error ("overflow in constant expression");
1507 /* Print a warning if an expression had overflow in folding and its
1508 operands hadn't.
1510 Invoke this function on every expression that
1511 (1) appears in the source code, and
1512 (2) is a constant expression that overflowed, and
1513 (3) is not already checked by convert_and_check;
1514 however, do not invoke this function on operands of explicit casts
1515 or when the expression is the result of an operator and any operand
1516 already overflowed. */
1518 void
1519 overflow_warning (location_t loc, tree value)
1521 if (c_inhibit_evaluation_warnings != 0)
1522 return;
1524 switch (TREE_CODE (value))
1526 case INTEGER_CST:
1527 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1528 break;
1530 case REAL_CST:
1531 warning_at (loc, OPT_Woverflow,
1532 "floating point overflow in expression");
1533 break;
1535 case FIXED_CST:
1536 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1537 break;
1539 case VECTOR_CST:
1540 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1541 break;
1543 case COMPLEX_CST:
1544 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1545 warning_at (loc, OPT_Woverflow,
1546 "complex integer overflow in expression");
1547 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1548 warning_at (loc, OPT_Woverflow,
1549 "complex floating point overflow in expression");
1550 break;
1552 default:
1553 break;
1557 /* Warn about uses of logical || / && operator in a context where it
1558 is likely that the bitwise equivalent was intended by the
1559 programmer. We have seen an expression in which CODE is a binary
1560 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1561 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1562 void
1563 warn_logical_operator (location_t location, enum tree_code code, tree type,
1564 enum tree_code code_left, tree op_left,
1565 enum tree_code ARG_UNUSED (code_right), tree op_right)
1567 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1568 int in0_p, in1_p, in_p;
1569 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1570 bool strict_overflow_p = false;
1572 if (code != TRUTH_ANDIF_EXPR
1573 && code != TRUTH_AND_EXPR
1574 && code != TRUTH_ORIF_EXPR
1575 && code != TRUTH_OR_EXPR)
1576 return;
1578 /* Warn if &&/|| are being used in a context where it is
1579 likely that the bitwise equivalent was intended by the
1580 programmer. That is, an expression such as op && MASK
1581 where op should not be any boolean expression, nor a
1582 constant, and mask seems to be a non-boolean integer constant. */
1583 if (!truth_value_p (code_left)
1584 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1585 && !CONSTANT_CLASS_P (op_left)
1586 && !TREE_NO_WARNING (op_left)
1587 && TREE_CODE (op_right) == INTEGER_CST
1588 && !integer_zerop (op_right)
1589 && !integer_onep (op_right))
1591 if (or_op)
1592 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1593 " applied to non-boolean constant");
1594 else
1595 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1596 " applied to non-boolean constant");
1597 TREE_NO_WARNING (op_left) = true;
1598 return;
1601 /* We do not warn for constants because they are typical of macro
1602 expansions that test for features. */
1603 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1604 return;
1606 /* This warning only makes sense with logical operands. */
1607 if (!(truth_value_p (TREE_CODE (op_left))
1608 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1609 || !(truth_value_p (TREE_CODE (op_right))
1610 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1611 return;
1614 /* We first test whether either side separately is trivially true
1615 (with OR) or trivially false (with AND). If so, do not warn.
1616 This is a common idiom for testing ranges of data types in
1617 portable code. */
1618 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1619 if (!lhs)
1620 return;
1621 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1622 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1624 /* If this is an OR operation, invert both sides; now, the result
1625 should be always false to get a warning. */
1626 if (or_op)
1627 in0_p = !in0_p;
1629 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1630 if (tem && integer_zerop (tem))
1631 return;
1633 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1634 if (!rhs)
1635 return;
1636 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1637 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1639 /* If this is an OR operation, invert both sides; now, the result
1640 should be always false to get a warning. */
1641 if (or_op)
1642 in1_p = !in1_p;
1644 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1645 if (tem && integer_zerop (tem))
1646 return;
1648 /* If both expressions have the same operand, if we can merge the
1649 ranges, and if the range test is always false, then warn. */
1650 if (operand_equal_p (lhs, rhs, 0)
1651 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1652 in1_p, low1, high1)
1653 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1654 type, lhs, in_p, low, high))
1655 && integer_zerop (tem))
1657 if (or_op)
1658 warning_at (location, OPT_Wlogical_op,
1659 "logical %<or%> "
1660 "of collectively exhaustive tests is always true");
1661 else
1662 warning_at (location, OPT_Wlogical_op,
1663 "logical %<and%> "
1664 "of mutually exclusive tests is always false");
1669 /* Warn if EXP contains any computations whose results are not used.
1670 Return true if a warning is printed; false otherwise. LOCUS is the
1671 (potential) location of the expression. */
1673 bool
1674 warn_if_unused_value (const_tree exp, location_t locus)
1676 restart:
1677 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1678 return false;
1680 /* Don't warn about void constructs. This includes casting to void,
1681 void function calls, and statement expressions with a final cast
1682 to void. */
1683 if (VOID_TYPE_P (TREE_TYPE (exp)))
1684 return false;
1686 if (EXPR_HAS_LOCATION (exp))
1687 locus = EXPR_LOCATION (exp);
1689 switch (TREE_CODE (exp))
1691 case PREINCREMENT_EXPR:
1692 case POSTINCREMENT_EXPR:
1693 case PREDECREMENT_EXPR:
1694 case POSTDECREMENT_EXPR:
1695 case MODIFY_EXPR:
1696 case INIT_EXPR:
1697 case TARGET_EXPR:
1698 case CALL_EXPR:
1699 case TRY_CATCH_EXPR:
1700 case WITH_CLEANUP_EXPR:
1701 case EXIT_EXPR:
1702 case VA_ARG_EXPR:
1703 return false;
1705 case BIND_EXPR:
1706 /* For a binding, warn if no side effect within it. */
1707 exp = BIND_EXPR_BODY (exp);
1708 goto restart;
1710 case SAVE_EXPR:
1711 case NON_LVALUE_EXPR:
1712 case NOP_EXPR:
1713 exp = TREE_OPERAND (exp, 0);
1714 goto restart;
1716 case TRUTH_ORIF_EXPR:
1717 case TRUTH_ANDIF_EXPR:
1718 /* In && or ||, warn if 2nd operand has no side effect. */
1719 exp = TREE_OPERAND (exp, 1);
1720 goto restart;
1722 case COMPOUND_EXPR:
1723 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1724 return true;
1725 /* Let people do `(foo (), 0)' without a warning. */
1726 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1727 return false;
1728 exp = TREE_OPERAND (exp, 1);
1729 goto restart;
1731 case COND_EXPR:
1732 /* If this is an expression with side effects, don't warn; this
1733 case commonly appears in macro expansions. */
1734 if (TREE_SIDE_EFFECTS (exp))
1735 return false;
1736 goto warn;
1738 case INDIRECT_REF:
1739 /* Don't warn about automatic dereferencing of references, since
1740 the user cannot control it. */
1741 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1743 exp = TREE_OPERAND (exp, 0);
1744 goto restart;
1746 /* Fall through. */
1748 default:
1749 /* Referencing a volatile value is a side effect, so don't warn. */
1750 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1751 && TREE_THIS_VOLATILE (exp))
1752 return false;
1754 /* If this is an expression which has no operands, there is no value
1755 to be unused. There are no such language-independent codes,
1756 but front ends may define such. */
1757 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1758 return false;
1760 warn:
1761 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1766 /* Print a warning about casts that might indicate violation
1767 of strict aliasing rules if -Wstrict-aliasing is used and
1768 strict aliasing mode is in effect. OTYPE is the original
1769 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1771 bool
1772 strict_aliasing_warning (tree otype, tree type, tree expr)
1774 /* Strip pointer conversion chains and get to the correct original type. */
1775 STRIP_NOPS (expr);
1776 otype = TREE_TYPE (expr);
1778 if (!(flag_strict_aliasing
1779 && POINTER_TYPE_P (type)
1780 && POINTER_TYPE_P (otype)
1781 && !VOID_TYPE_P (TREE_TYPE (type)))
1782 /* If the type we are casting to is a ref-all pointer
1783 dereferencing it is always valid. */
1784 || TYPE_REF_CAN_ALIAS_ALL (type))
1785 return false;
1787 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1788 && (DECL_P (TREE_OPERAND (expr, 0))
1789 || handled_component_p (TREE_OPERAND (expr, 0))))
1791 /* Casting the address of an object to non void pointer. Warn
1792 if the cast breaks type based aliasing. */
1793 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1795 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1796 "might break strict-aliasing rules");
1797 return true;
1799 else
1801 /* warn_strict_aliasing >= 3. This includes the default (3).
1802 Only warn if the cast is dereferenced immediately. */
1803 alias_set_type set1 =
1804 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1805 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1807 if (set1 != set2 && set2 != 0
1808 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1810 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1811 "pointer will break strict-aliasing rules");
1812 return true;
1814 else if (warn_strict_aliasing == 2
1815 && !alias_sets_must_conflict_p (set1, set2))
1817 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1818 "pointer might break strict-aliasing rules");
1819 return true;
1823 else
1824 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1826 /* At this level, warn for any conversions, even if an address is
1827 not taken in the same statement. This will likely produce many
1828 false positives, but could be useful to pinpoint problems that
1829 are not revealed at higher levels. */
1830 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1831 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1832 if (!COMPLETE_TYPE_P (type)
1833 || !alias_sets_must_conflict_p (set1, set2))
1835 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1836 "pointer might break strict-aliasing rules");
1837 return true;
1841 return false;
1844 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1845 sizeof as last operand of certain builtins. */
1847 void
1848 sizeof_pointer_memaccess_warning (location_t loc, tree callee,
1849 VEC(tree, gc) *params, tree sizeof_arg,
1850 bool (*comp_types) (tree, tree))
1852 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1853 bool strop = false;
1855 if (TREE_CODE (callee) != FUNCTION_DECL
1856 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1857 || sizeof_arg == error_mark_node
1858 || VEC_length (tree, params) <= 1)
1859 return;
1861 type = TYPE_P (sizeof_arg) ? sizeof_arg : TREE_TYPE (sizeof_arg);
1862 if (!POINTER_TYPE_P (type))
1863 return;
1865 switch (DECL_FUNCTION_CODE (callee))
1867 case BUILT_IN_STRNCMP:
1868 case BUILT_IN_STRNCASECMP:
1869 case BUILT_IN_STRNCPY:
1870 case BUILT_IN_STRNCAT:
1871 strop = true;
1872 /* FALLTHRU */
1873 case BUILT_IN_MEMCPY:
1874 case BUILT_IN_MEMMOVE:
1875 case BUILT_IN_MEMCMP:
1876 if (VEC_length (tree, params) < 3)
1877 return;
1878 src = VEC_index (tree, params, 1);
1879 dest = VEC_index (tree, params, 0);
1880 break;
1881 case BUILT_IN_MEMSET:
1882 if (VEC_length (tree, params) < 3)
1883 return;
1884 dest = VEC_index (tree, params, 0);
1885 break;
1886 case BUILT_IN_STRNDUP:
1887 src = VEC_index (tree, params, 0);
1888 strop = true;
1889 break;
1890 default:
1891 break;
1894 if (dest
1895 && (tem = tree_strip_nop_conversions (dest))
1896 && POINTER_TYPE_P (TREE_TYPE (tem))
1897 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1898 return;
1900 if (src
1901 && (tem = tree_strip_nop_conversions (src))
1902 && POINTER_TYPE_P (TREE_TYPE (tem))
1903 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1904 return;
1906 if (dest)
1908 if (!TYPE_P (sizeof_arg)
1909 && operand_equal_p (dest, sizeof_arg, 0)
1910 && comp_types (TREE_TYPE (dest), type))
1912 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1913 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1914 "argument to %<sizeof%> in %qD call is the same "
1915 "expression as the destination; did you mean to "
1916 "remove the addressof?", callee);
1917 else if ((TYPE_PRECISION (TREE_TYPE (type))
1918 == TYPE_PRECISION (char_type_node))
1919 || strop)
1920 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1921 "argument to %<sizeof%> in %qD call is the same "
1922 "expression as the destination; did you mean to "
1923 "provide an explicit length?", callee);
1924 else
1925 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1926 "argument to %<sizeof%> in %qD call is the same "
1927 "expression as the destination; did you mean to "
1928 "dereference it?", callee);
1929 return;
1932 if (POINTER_TYPE_P (TREE_TYPE (dest))
1933 && !strop
1934 && comp_types (TREE_TYPE (dest), type)
1935 && !VOID_TYPE_P (TREE_TYPE (type)))
1937 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1938 "argument to %<sizeof%> in %qD call is the same "
1939 "pointer type %qT as the destination; expected %qT "
1940 "or an explicit length", callee, TREE_TYPE (dest),
1941 TREE_TYPE (TREE_TYPE (dest)));
1942 return;
1946 if (src)
1948 if (!TYPE_P (sizeof_arg)
1949 && operand_equal_p (src, sizeof_arg, 0)
1950 && comp_types (TREE_TYPE (src), type))
1952 if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop)
1953 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1954 "argument to %<sizeof%> in %qD call is the same "
1955 "expression as the source; did you mean to "
1956 "remove the addressof?", callee);
1957 else if ((TYPE_PRECISION (TREE_TYPE (type))
1958 == TYPE_PRECISION (char_type_node))
1959 || strop)
1960 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1961 "argument to %<sizeof%> in %qD call is the same "
1962 "expression as the source; did you mean to "
1963 "provide an explicit length?", callee);
1964 else
1965 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1966 "argument to %<sizeof%> in %qD call is the same "
1967 "expression as the source; did you mean to "
1968 "dereference it?", callee);
1969 return;
1972 if (POINTER_TYPE_P (TREE_TYPE (src))
1973 && !strop
1974 && comp_types (TREE_TYPE (src), type)
1975 && !VOID_TYPE_P (TREE_TYPE (type)))
1977 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1978 "argument to %<sizeof%> in %qD call is the same "
1979 "pointer type %qT as the source; expected %qT "
1980 "or an explicit length", callee, TREE_TYPE (src),
1981 TREE_TYPE (TREE_TYPE (src)));
1982 return;
1987 /* Warn for unlikely, improbable, or stupid DECL declarations
1988 of `main'. */
1990 void
1991 check_main_parameter_types (tree decl)
1993 function_args_iterator iter;
1994 tree type;
1995 int argct = 0;
1997 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1999 /* XXX void_type_node belies the abstraction. */
2000 if (type == void_type_node || type == error_mark_node )
2001 break;
2003 ++argct;
2004 switch (argct)
2006 case 1:
2007 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2008 pedwarn (input_location, OPT_Wmain,
2009 "first argument of %q+D should be %<int%>", decl);
2010 break;
2012 case 2:
2013 if (TREE_CODE (type) != POINTER_TYPE
2014 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2015 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2016 != char_type_node))
2017 pedwarn (input_location, OPT_Wmain,
2018 "second argument of %q+D should be %<char **%>", decl);
2019 break;
2021 case 3:
2022 if (TREE_CODE (type) != POINTER_TYPE
2023 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2024 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2025 != char_type_node))
2026 pedwarn (input_location, OPT_Wmain,
2027 "third argument of %q+D should probably be "
2028 "%<char **%>", decl);
2029 break;
2033 /* It is intentional that this message does not mention the third
2034 argument because it's only mentioned in an appendix of the
2035 standard. */
2036 if (argct > 0 && (argct < 2 || argct > 3))
2037 pedwarn (input_location, OPT_Wmain,
2038 "%q+D takes only zero or two arguments", decl);
2041 /* True if pointers to distinct types T1 and T2 can be converted to
2042 each other without an explicit cast. Only returns true for opaque
2043 vector types. */
2044 bool
2045 vector_targets_convertible_p (const_tree t1, const_tree t2)
2047 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2048 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2049 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2050 return true;
2052 return false;
2055 /* True if vector types T1 and T2 can be converted to each other
2056 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2057 can only be converted with -flax-vector-conversions yet that is not
2058 in effect, emit a note telling the user about that option if such
2059 a note has not previously been emitted. */
2060 bool
2061 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2063 static bool emitted_lax_note = false;
2064 bool convertible_lax;
2066 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2067 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2068 return true;
2070 convertible_lax =
2071 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2072 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2073 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
2074 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2075 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2077 if (!convertible_lax || flag_lax_vector_conversions)
2078 return convertible_lax;
2080 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2081 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2082 return true;
2084 if (emit_lax_note && !emitted_lax_note)
2086 emitted_lax_note = true;
2087 inform (input_location, "use -flax-vector-conversions to permit "
2088 "conversions between vectors with differing "
2089 "element types or numbers of subparts");
2092 return false;
2095 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2096 and have vector types, V0 has the same type as V1, and the number of
2097 elements of V0, V1, MASK is the same.
2099 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2100 called with two arguments. In this case implementation passes the
2101 first argument twice in order to share the same tree code. This fact
2102 could enable the mask-values being twice the vector length. This is
2103 an implementation accident and this semantics is not guaranteed to
2104 the user. */
2105 tree
2106 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2108 tree ret;
2109 bool wrap = true;
2110 bool maybe_const = false;
2111 bool two_arguments = false;
2113 if (v1 == NULL_TREE)
2115 two_arguments = true;
2116 v1 = v0;
2119 if (v0 == error_mark_node || v1 == error_mark_node
2120 || mask == error_mark_node)
2121 return error_mark_node;
2123 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2124 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2126 error_at (loc, "__builtin_shuffle last argument must "
2127 "be an integer vector");
2128 return error_mark_node;
2131 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2132 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2134 error_at (loc, "__builtin_shuffle arguments must be vectors");
2135 return error_mark_node;
2138 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2140 error_at (loc, "__builtin_shuffle argument vectors must be of "
2141 "the same type");
2142 return error_mark_node;
2145 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2146 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2147 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2148 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2150 error_at (loc, "__builtin_shuffle number of elements of the "
2151 "argument vector(s) and the mask vector should "
2152 "be the same");
2153 return error_mark_node;
2156 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2157 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2159 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2160 "must have the same size as inner type of the mask");
2161 return error_mark_node;
2164 if (!c_dialect_cxx ())
2166 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2167 v0 = c_fully_fold (v0, false, &maybe_const);
2168 wrap &= maybe_const;
2170 if (two_arguments)
2171 v1 = v0 = save_expr (v0);
2172 else
2174 v1 = c_fully_fold (v1, false, &maybe_const);
2175 wrap &= maybe_const;
2178 mask = c_fully_fold (mask, false, &maybe_const);
2179 wrap &= maybe_const;
2182 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2184 if (!c_dialect_cxx () && !wrap)
2185 ret = c_wrap_maybe_const (ret, true);
2187 return ret;
2190 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2191 to integral type. */
2193 static tree
2194 c_common_get_narrower (tree op, int *unsignedp_ptr)
2196 op = get_narrower (op, unsignedp_ptr);
2198 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2199 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2201 /* C++0x scoped enumerations don't implicitly convert to integral
2202 type; if we stripped an explicit conversion to a larger type we
2203 need to replace it so common_type will still work. */
2204 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2205 TYPE_UNSIGNED (TREE_TYPE (op)));
2206 op = fold_convert (type, op);
2208 return op;
2211 /* This is a helper function of build_binary_op.
2213 For certain operations if both args were extended from the same
2214 smaller type, do the arithmetic in that type and then extend.
2216 BITWISE indicates a bitwise operation.
2217 For them, this optimization is safe only if
2218 both args are zero-extended or both are sign-extended.
2219 Otherwise, we might change the result.
2220 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2221 but calculated in (unsigned short) it would be (unsigned short)-1.
2223 tree
2224 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2226 int unsigned0, unsigned1;
2227 tree arg0, arg1;
2228 int uns;
2229 tree type;
2231 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2232 excessive narrowing when we call get_narrower below. For
2233 example, suppose that OP0 is of unsigned int extended
2234 from signed char and that RESULT_TYPE is long long int.
2235 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2236 like
2238 (long long int) (unsigned int) signed_char
2240 which get_narrower would narrow down to
2242 (unsigned int) signed char
2244 If we do not cast OP0 first, get_narrower would return
2245 signed_char, which is inconsistent with the case of the
2246 explicit cast. */
2247 op0 = convert (result_type, op0);
2248 op1 = convert (result_type, op1);
2250 arg0 = c_common_get_narrower (op0, &unsigned0);
2251 arg1 = c_common_get_narrower (op1, &unsigned1);
2253 /* UNS is 1 if the operation to be done is an unsigned one. */
2254 uns = TYPE_UNSIGNED (result_type);
2256 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2257 but it *requires* conversion to FINAL_TYPE. */
2259 if ((TYPE_PRECISION (TREE_TYPE (op0))
2260 == TYPE_PRECISION (TREE_TYPE (arg0)))
2261 && TREE_TYPE (op0) != result_type)
2262 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2263 if ((TYPE_PRECISION (TREE_TYPE (op1))
2264 == TYPE_PRECISION (TREE_TYPE (arg1)))
2265 && TREE_TYPE (op1) != result_type)
2266 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2268 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2270 /* For bitwise operations, signedness of nominal type
2271 does not matter. Consider only how operands were extended. */
2272 if (bitwise)
2273 uns = unsigned0;
2275 /* Note that in all three cases below we refrain from optimizing
2276 an unsigned operation on sign-extended args.
2277 That would not be valid. */
2279 /* Both args variable: if both extended in same way
2280 from same width, do it in that width.
2281 Do it unsigned if args were zero-extended. */
2282 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2283 < TYPE_PRECISION (result_type))
2284 && (TYPE_PRECISION (TREE_TYPE (arg1))
2285 == TYPE_PRECISION (TREE_TYPE (arg0)))
2286 && unsigned0 == unsigned1
2287 && (unsigned0 || !uns))
2288 return c_common_signed_or_unsigned_type
2289 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2291 else if (TREE_CODE (arg0) == INTEGER_CST
2292 && (unsigned1 || !uns)
2293 && (TYPE_PRECISION (TREE_TYPE (arg1))
2294 < TYPE_PRECISION (result_type))
2295 && (type
2296 = c_common_signed_or_unsigned_type (unsigned1,
2297 TREE_TYPE (arg1)))
2298 && !POINTER_TYPE_P (type)
2299 && int_fits_type_p (arg0, type))
2300 return type;
2302 else if (TREE_CODE (arg1) == INTEGER_CST
2303 && (unsigned0 || !uns)
2304 && (TYPE_PRECISION (TREE_TYPE (arg0))
2305 < TYPE_PRECISION (result_type))
2306 && (type
2307 = c_common_signed_or_unsigned_type (unsigned0,
2308 TREE_TYPE (arg0)))
2309 && !POINTER_TYPE_P (type)
2310 && int_fits_type_p (arg1, type))
2311 return type;
2313 return result_type;
2316 /* Checks if expression EXPR of real/integer type cannot be converted
2317 to the real/integer type TYPE. Function returns true when:
2318 * EXPR is a constant which cannot be exactly converted to TYPE
2319 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2320 for EXPR type and TYPE being both integers or both real.
2321 * EXPR is not a constant of real type and TYPE is an integer.
2322 * EXPR is not a constant of integer type which cannot be
2323 exactly converted to real type.
2324 Function allows conversions between types of different signedness and
2325 does not return true in that case. Function can produce signedness
2326 warnings if PRODUCE_WARNS is true. */
2327 bool
2328 unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2330 bool give_warning = false;
2331 tree expr_type = TREE_TYPE (expr);
2332 location_t loc = EXPR_LOC_OR_HERE (expr);
2334 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2336 /* Warn for real constant that is not an exact integer converted
2337 to integer type. */
2338 if (TREE_CODE (expr_type) == REAL_TYPE
2339 && TREE_CODE (type) == INTEGER_TYPE)
2341 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2342 give_warning = true;
2344 /* Warn for an integer constant that does not fit into integer type. */
2345 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2346 && TREE_CODE (type) == INTEGER_TYPE
2347 && !int_fits_type_p (expr, type))
2349 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2350 && tree_int_cst_sgn (expr) < 0)
2352 if (produce_warns)
2353 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2354 " implicitly converted to unsigned type");
2356 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2358 if (produce_warns)
2359 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2360 " constant value to negative integer");
2362 else
2363 give_warning = true;
2365 else if (TREE_CODE (type) == REAL_TYPE)
2367 /* Warn for an integer constant that does not fit into real type. */
2368 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2370 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2371 if (!exact_real_truncate (TYPE_MODE (type), &a))
2372 give_warning = true;
2374 /* Warn for a real constant that does not fit into a smaller
2375 real type. */
2376 else if (TREE_CODE (expr_type) == REAL_TYPE
2377 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2379 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2380 if (!exact_real_truncate (TYPE_MODE (type), &a))
2381 give_warning = true;
2385 else
2387 /* Warn for real types converted to integer types. */
2388 if (TREE_CODE (expr_type) == REAL_TYPE
2389 && TREE_CODE (type) == INTEGER_TYPE)
2390 give_warning = true;
2392 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2393 && TREE_CODE (type) == INTEGER_TYPE)
2395 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2396 expr = get_unwidened (expr, 0);
2397 expr_type = TREE_TYPE (expr);
2399 /* Don't warn for short y; short x = ((int)y & 0xff); */
2400 if (TREE_CODE (expr) == BIT_AND_EXPR
2401 || TREE_CODE (expr) == BIT_IOR_EXPR
2402 || TREE_CODE (expr) == BIT_XOR_EXPR)
2404 /* If both args were extended from a shortest type,
2405 use that type if that is safe. */
2406 expr_type = shorten_binary_op (expr_type,
2407 TREE_OPERAND (expr, 0),
2408 TREE_OPERAND (expr, 1),
2409 /* bitwise */1);
2411 if (TREE_CODE (expr) == BIT_AND_EXPR)
2413 tree op0 = TREE_OPERAND (expr, 0);
2414 tree op1 = TREE_OPERAND (expr, 1);
2415 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2416 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2418 /* If one of the operands is a non-negative constant
2419 that fits in the target type, then the type of the
2420 other operand does not matter. */
2421 if ((TREE_CODE (op0) == INTEGER_CST
2422 && int_fits_type_p (op0, c_common_signed_type (type))
2423 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2424 || (TREE_CODE (op1) == INTEGER_CST
2425 && int_fits_type_p (op1, c_common_signed_type (type))
2426 && int_fits_type_p (op1,
2427 c_common_unsigned_type (type))))
2428 return false;
2429 /* If constant is unsigned and fits in the target
2430 type, then the result will also fit. */
2431 else if ((TREE_CODE (op0) == INTEGER_CST
2432 && unsigned0
2433 && int_fits_type_p (op0, type))
2434 || (TREE_CODE (op1) == INTEGER_CST
2435 && unsigned1
2436 && int_fits_type_p (op1, type)))
2437 return false;
2440 /* Warn for integer types converted to smaller integer types. */
2441 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2442 give_warning = true;
2444 /* When they are the same width but different signedness,
2445 then the value may change. */
2446 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2447 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2448 /* Even when converted to a bigger type, if the type is
2449 unsigned but expr is signed, then negative values
2450 will be changed. */
2451 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2452 && produce_warns)
2453 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2454 "may change the sign of the result",
2455 type, expr_type);
2458 /* Warn for integer types converted to real types if and only if
2459 all the range of values of the integer type cannot be
2460 represented by the real type. */
2461 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2462 && TREE_CODE (type) == REAL_TYPE)
2464 tree type_low_bound, type_high_bound;
2465 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2467 /* Don't warn about char y = 0xff; float x = (int) y; */
2468 expr = get_unwidened (expr, 0);
2469 expr_type = TREE_TYPE (expr);
2471 type_low_bound = TYPE_MIN_VALUE (expr_type);
2472 type_high_bound = TYPE_MAX_VALUE (expr_type);
2473 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2474 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2476 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2477 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2478 give_warning = true;
2481 /* Warn for real types converted to smaller real types. */
2482 else if (TREE_CODE (expr_type) == REAL_TYPE
2483 && TREE_CODE (type) == REAL_TYPE
2484 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2485 give_warning = true;
2488 return give_warning;
2491 /* Warns if the conversion of EXPR to TYPE may alter a value.
2492 This is a helper function for warnings_for_convert_and_check. */
2494 static void
2495 conversion_warning (tree type, tree expr)
2497 tree expr_type = TREE_TYPE (expr);
2498 location_t loc = EXPR_LOC_OR_HERE (expr);
2500 if (!warn_conversion && !warn_sign_conversion)
2501 return;
2503 switch (TREE_CODE (expr))
2505 case EQ_EXPR:
2506 case NE_EXPR:
2507 case LE_EXPR:
2508 case GE_EXPR:
2509 case LT_EXPR:
2510 case GT_EXPR:
2511 case TRUTH_ANDIF_EXPR:
2512 case TRUTH_ORIF_EXPR:
2513 case TRUTH_AND_EXPR:
2514 case TRUTH_OR_EXPR:
2515 case TRUTH_XOR_EXPR:
2516 case TRUTH_NOT_EXPR:
2517 /* Conversion from boolean to a signed:1 bit-field (which only
2518 can hold the values 0 and -1) doesn't lose information - but
2519 it does change the value. */
2520 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2521 warning_at (loc, OPT_Wconversion,
2522 "conversion to %qT from boolean expression", type);
2523 return;
2525 case REAL_CST:
2526 case INTEGER_CST:
2527 if (unsafe_conversion_p (type, expr, true))
2528 warning_at (loc, OPT_Wconversion,
2529 "conversion to %qT alters %qT constant value",
2530 type, expr_type);
2531 return;
2533 case COND_EXPR:
2535 /* In case of COND_EXPR, if both operands are constants or
2536 COND_EXPR, then we do not care about the type of COND_EXPR,
2537 only about the conversion of each operand. */
2538 tree op1 = TREE_OPERAND (expr, 1);
2539 tree op2 = TREE_OPERAND (expr, 2);
2541 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2542 || TREE_CODE (op1) == COND_EXPR)
2543 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2544 || TREE_CODE (op2) == COND_EXPR))
2546 conversion_warning (type, op1);
2547 conversion_warning (type, op2);
2548 return;
2550 /* Fall through. */
2553 default: /* 'expr' is not a constant. */
2554 if (unsafe_conversion_p (type, expr, true))
2555 warning_at (loc, OPT_Wconversion,
2556 "conversion to %qT from %qT may alter its value",
2557 type, expr_type);
2561 /* Produce warnings after a conversion. RESULT is the result of
2562 converting EXPR to TYPE. This is a helper function for
2563 convert_and_check and cp_convert_and_check. */
2565 void
2566 warnings_for_convert_and_check (tree type, tree expr, tree result)
2568 location_t loc = EXPR_LOC_OR_HERE (expr);
2570 if (TREE_CODE (expr) == INTEGER_CST
2571 && (TREE_CODE (type) == INTEGER_TYPE
2572 || TREE_CODE (type) == ENUMERAL_TYPE)
2573 && !int_fits_type_p (expr, type))
2575 /* Do not diagnose overflow in a constant expression merely
2576 because a conversion overflowed. */
2577 if (TREE_OVERFLOW (result))
2578 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2580 if (TYPE_UNSIGNED (type))
2582 /* This detects cases like converting -129 or 256 to
2583 unsigned char. */
2584 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2585 warning_at (loc, OPT_Woverflow,
2586 "large integer implicitly truncated to unsigned type");
2587 else
2588 conversion_warning (type, expr);
2590 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2591 warning (OPT_Woverflow,
2592 "overflow in implicit constant conversion");
2593 /* No warning for converting 0x80000000 to int. */
2594 else if (pedantic
2595 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2596 || TYPE_PRECISION (TREE_TYPE (expr))
2597 != TYPE_PRECISION (type)))
2598 warning_at (loc, OPT_Woverflow,
2599 "overflow in implicit constant conversion");
2601 else
2602 conversion_warning (type, expr);
2604 else if ((TREE_CODE (result) == INTEGER_CST
2605 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2606 warning_at (loc, OPT_Woverflow,
2607 "overflow in implicit constant conversion");
2608 else
2609 conversion_warning (type, expr);
2613 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2614 Invoke this function on every expression that is converted implicitly,
2615 i.e. because of language rules and not because of an explicit cast. */
2617 tree
2618 convert_and_check (tree type, tree expr)
2620 tree result;
2621 tree expr_for_warning;
2623 /* Convert from a value with possible excess precision rather than
2624 via the semantic type, but do not warn about values not fitting
2625 exactly in the semantic type. */
2626 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2628 tree orig_type = TREE_TYPE (expr);
2629 expr = TREE_OPERAND (expr, 0);
2630 expr_for_warning = convert (orig_type, expr);
2631 if (orig_type == type)
2632 return expr_for_warning;
2634 else
2635 expr_for_warning = expr;
2637 if (TREE_TYPE (expr) == type)
2638 return expr;
2640 result = convert (type, expr);
2642 if (c_inhibit_evaluation_warnings == 0
2643 && !TREE_OVERFLOW_P (expr)
2644 && result != error_mark_node)
2645 warnings_for_convert_and_check (type, expr_for_warning, result);
2647 return result;
2650 /* A node in a list that describes references to variables (EXPR), which are
2651 either read accesses if WRITER is zero, or write accesses, in which case
2652 WRITER is the parent of EXPR. */
2653 struct tlist
2655 struct tlist *next;
2656 tree expr, writer;
2659 /* Used to implement a cache the results of a call to verify_tree. We only
2660 use this for SAVE_EXPRs. */
2661 struct tlist_cache
2663 struct tlist_cache *next;
2664 struct tlist *cache_before_sp;
2665 struct tlist *cache_after_sp;
2666 tree expr;
2669 /* Obstack to use when allocating tlist structures, and corresponding
2670 firstobj. */
2671 static struct obstack tlist_obstack;
2672 static char *tlist_firstobj = 0;
2674 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2675 warnings. */
2676 static struct tlist *warned_ids;
2677 /* SAVE_EXPRs need special treatment. We process them only once and then
2678 cache the results. */
2679 static struct tlist_cache *save_expr_cache;
2681 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2682 static void merge_tlist (struct tlist **, struct tlist *, int);
2683 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2684 static int warning_candidate_p (tree);
2685 static bool candidate_equal_p (const_tree, const_tree);
2686 static void warn_for_collisions (struct tlist *);
2687 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2688 static struct tlist *new_tlist (struct tlist *, tree, tree);
2690 /* Create a new struct tlist and fill in its fields. */
2691 static struct tlist *
2692 new_tlist (struct tlist *next, tree t, tree writer)
2694 struct tlist *l;
2695 l = XOBNEW (&tlist_obstack, struct tlist);
2696 l->next = next;
2697 l->expr = t;
2698 l->writer = writer;
2699 return l;
2702 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2703 is nonnull, we ignore any node we find which has a writer equal to it. */
2705 static void
2706 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2708 while (add)
2710 struct tlist *next = add->next;
2711 if (!copy)
2712 add->next = *to;
2713 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2714 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2715 add = next;
2719 /* Merge the nodes of ADD into TO. This merging process is done so that for
2720 each variable that already exists in TO, no new node is added; however if
2721 there is a write access recorded in ADD, and an occurrence on TO is only
2722 a read access, then the occurrence in TO will be modified to record the
2723 write. */
2725 static void
2726 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2728 struct tlist **end = to;
2730 while (*end)
2731 end = &(*end)->next;
2733 while (add)
2735 int found = 0;
2736 struct tlist *tmp2;
2737 struct tlist *next = add->next;
2739 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2740 if (candidate_equal_p (tmp2->expr, add->expr))
2742 found = 1;
2743 if (!tmp2->writer)
2744 tmp2->writer = add->writer;
2746 if (!found)
2748 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2749 end = &(*end)->next;
2750 *end = 0;
2752 add = next;
2756 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2757 references in list LIST conflict with it, excluding reads if ONLY writers
2758 is nonzero. */
2760 static void
2761 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2762 int only_writes)
2764 struct tlist *tmp;
2766 /* Avoid duplicate warnings. */
2767 for (tmp = warned_ids; tmp; tmp = tmp->next)
2768 if (candidate_equal_p (tmp->expr, written))
2769 return;
2771 while (list)
2773 if (candidate_equal_p (list->expr, written)
2774 && !candidate_equal_p (list->writer, writer)
2775 && (!only_writes || list->writer))
2777 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2778 warning_at (EXPR_LOC_OR_HERE (writer),
2779 OPT_Wsequence_point, "operation on %qE may be undefined",
2780 list->expr);
2782 list = list->next;
2786 /* Given a list LIST of references to variables, find whether any of these
2787 can cause conflicts due to missing sequence points. */
2789 static void
2790 warn_for_collisions (struct tlist *list)
2792 struct tlist *tmp;
2794 for (tmp = list; tmp; tmp = tmp->next)
2796 if (tmp->writer)
2797 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2801 /* Return nonzero if X is a tree that can be verified by the sequence point
2802 warnings. */
2803 static int
2804 warning_candidate_p (tree x)
2806 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2807 return 0;
2809 if (TREE_CODE (x) == BLOCK)
2810 return 0;
2812 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2813 (lvalue_p) crash on TRY/CATCH. */
2814 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2815 return 0;
2817 if (!lvalue_p (x))
2818 return 0;
2820 /* No point to track non-const calls, they will never satisfy
2821 operand_equal_p. */
2822 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2823 return 0;
2825 if (TREE_CODE (x) == STRING_CST)
2826 return 0;
2828 return 1;
2831 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2832 static bool
2833 candidate_equal_p (const_tree x, const_tree y)
2835 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2838 /* Walk the tree X, and record accesses to variables. If X is written by the
2839 parent tree, WRITER is the parent.
2840 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2841 expression or its only operand forces a sequence point, then everything up
2842 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2843 in PNO_SP.
2844 Once we return, we will have emitted warnings if any subexpression before
2845 such a sequence point could be undefined. On a higher level, however, the
2846 sequence point may not be relevant, and we'll merge the two lists.
2848 Example: (b++, a) + b;
2849 The call that processes the COMPOUND_EXPR will store the increment of B
2850 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2851 processes the PLUS_EXPR will need to merge the two lists so that
2852 eventually, all accesses end up on the same list (and we'll warn about the
2853 unordered subexpressions b++ and b.
2855 A note on merging. If we modify the former example so that our expression
2856 becomes
2857 (b++, b) + a
2858 care must be taken not simply to add all three expressions into the final
2859 PNO_SP list. The function merge_tlist takes care of that by merging the
2860 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2861 way, so that no more than one access to B is recorded. */
2863 static void
2864 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2865 tree writer)
2867 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2868 enum tree_code code;
2869 enum tree_code_class cl;
2871 /* X may be NULL if it is the operand of an empty statement expression
2872 ({ }). */
2873 if (x == NULL)
2874 return;
2876 restart:
2877 code = TREE_CODE (x);
2878 cl = TREE_CODE_CLASS (code);
2880 if (warning_candidate_p (x))
2881 *pno_sp = new_tlist (*pno_sp, x, writer);
2883 switch (code)
2885 case CONSTRUCTOR:
2886 return;
2888 case COMPOUND_EXPR:
2889 case TRUTH_ANDIF_EXPR:
2890 case TRUTH_ORIF_EXPR:
2891 tmp_before = tmp_nosp = tmp_list3 = 0;
2892 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2893 warn_for_collisions (tmp_nosp);
2894 merge_tlist (pbefore_sp, tmp_before, 0);
2895 merge_tlist (pbefore_sp, tmp_nosp, 0);
2896 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2897 merge_tlist (pbefore_sp, tmp_list3, 0);
2898 return;
2900 case COND_EXPR:
2901 tmp_before = tmp_list2 = 0;
2902 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2903 warn_for_collisions (tmp_list2);
2904 merge_tlist (pbefore_sp, tmp_before, 0);
2905 merge_tlist (pbefore_sp, tmp_list2, 1);
2907 tmp_list3 = tmp_nosp = 0;
2908 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2909 warn_for_collisions (tmp_nosp);
2910 merge_tlist (pbefore_sp, tmp_list3, 0);
2912 tmp_list3 = tmp_list2 = 0;
2913 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2914 warn_for_collisions (tmp_list2);
2915 merge_tlist (pbefore_sp, tmp_list3, 0);
2916 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2917 two first, to avoid warning for (a ? b++ : b++). */
2918 merge_tlist (&tmp_nosp, tmp_list2, 0);
2919 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2920 return;
2922 case PREDECREMENT_EXPR:
2923 case PREINCREMENT_EXPR:
2924 case POSTDECREMENT_EXPR:
2925 case POSTINCREMENT_EXPR:
2926 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2927 return;
2929 case MODIFY_EXPR:
2930 tmp_before = tmp_nosp = tmp_list3 = 0;
2931 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2932 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2933 /* Expressions inside the LHS are not ordered wrt. the sequence points
2934 in the RHS. Example:
2935 *a = (a++, 2)
2936 Despite the fact that the modification of "a" is in the before_sp
2937 list (tmp_before), it conflicts with the use of "a" in the LHS.
2938 We can handle this by adding the contents of tmp_list3
2939 to those of tmp_before, and redoing the collision warnings for that
2940 list. */
2941 add_tlist (&tmp_before, tmp_list3, x, 1);
2942 warn_for_collisions (tmp_before);
2943 /* Exclude the LHS itself here; we first have to merge it into the
2944 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2945 didn't exclude the LHS, we'd get it twice, once as a read and once
2946 as a write. */
2947 add_tlist (pno_sp, tmp_list3, x, 0);
2948 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2950 merge_tlist (pbefore_sp, tmp_before, 0);
2951 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2952 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2953 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2954 return;
2956 case CALL_EXPR:
2957 /* We need to warn about conflicts among arguments and conflicts between
2958 args and the function address. Side effects of the function address,
2959 however, are not ordered by the sequence point of the call. */
2961 call_expr_arg_iterator iter;
2962 tree arg;
2963 tmp_before = tmp_nosp = 0;
2964 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2965 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2967 tmp_list2 = tmp_list3 = 0;
2968 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2969 merge_tlist (&tmp_list3, tmp_list2, 0);
2970 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2972 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2973 warn_for_collisions (tmp_before);
2974 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2975 return;
2978 case TREE_LIST:
2979 /* Scan all the list, e.g. indices of multi dimensional array. */
2980 while (x)
2982 tmp_before = tmp_nosp = 0;
2983 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2984 merge_tlist (&tmp_nosp, tmp_before, 0);
2985 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2986 x = TREE_CHAIN (x);
2988 return;
2990 case SAVE_EXPR:
2992 struct tlist_cache *t;
2993 for (t = save_expr_cache; t; t = t->next)
2994 if (candidate_equal_p (t->expr, x))
2995 break;
2997 if (!t)
2999 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3000 t->next = save_expr_cache;
3001 t->expr = x;
3002 save_expr_cache = t;
3004 tmp_before = tmp_nosp = 0;
3005 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3006 warn_for_collisions (tmp_nosp);
3008 tmp_list3 = 0;
3009 while (tmp_nosp)
3011 struct tlist *t = tmp_nosp;
3012 tmp_nosp = t->next;
3013 merge_tlist (&tmp_list3, t, 0);
3015 t->cache_before_sp = tmp_before;
3016 t->cache_after_sp = tmp_list3;
3018 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3019 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3020 return;
3023 case ADDR_EXPR:
3024 x = TREE_OPERAND (x, 0);
3025 if (DECL_P (x))
3026 return;
3027 writer = 0;
3028 goto restart;
3030 default:
3031 /* For other expressions, simply recurse on their operands.
3032 Manual tail recursion for unary expressions.
3033 Other non-expressions need not be processed. */
3034 if (cl == tcc_unary)
3036 x = TREE_OPERAND (x, 0);
3037 writer = 0;
3038 goto restart;
3040 else if (IS_EXPR_CODE_CLASS (cl))
3042 int lp;
3043 int max = TREE_OPERAND_LENGTH (x);
3044 for (lp = 0; lp < max; lp++)
3046 tmp_before = tmp_nosp = 0;
3047 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3048 merge_tlist (&tmp_nosp, tmp_before, 0);
3049 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3052 return;
3056 /* Try to warn for undefined behavior in EXPR due to missing sequence
3057 points. */
3059 DEBUG_FUNCTION void
3060 verify_sequence_points (tree expr)
3062 struct tlist *before_sp = 0, *after_sp = 0;
3064 warned_ids = 0;
3065 save_expr_cache = 0;
3066 if (tlist_firstobj == 0)
3068 gcc_obstack_init (&tlist_obstack);
3069 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3072 verify_tree (expr, &before_sp, &after_sp, 0);
3073 warn_for_collisions (after_sp);
3074 obstack_free (&tlist_obstack, tlist_firstobj);
3077 /* Validate the expression after `case' and apply default promotions. */
3079 static tree
3080 check_case_value (tree value)
3082 if (value == NULL_TREE)
3083 return value;
3085 if (TREE_CODE (value) == INTEGER_CST)
3086 /* Promote char or short to int. */
3087 value = perform_integral_promotions (value);
3088 else if (value != error_mark_node)
3090 error ("case label does not reduce to an integer constant");
3091 value = error_mark_node;
3094 constant_expression_warning (value);
3096 return value;
3099 /* See if the case values LOW and HIGH are in the range of the original
3100 type (i.e. before the default conversion to int) of the switch testing
3101 expression.
3102 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3103 the type before promoting it. CASE_LOW_P is a pointer to the lower
3104 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3105 if the case is not a case range.
3106 The caller has to make sure that we are not called with NULL for
3107 CASE_LOW_P (i.e. the default case).
3108 Returns true if the case label is in range of ORIG_TYPE (saturated or
3109 untouched) or false if the label is out of range. */
3111 static bool
3112 check_case_bounds (tree type, tree orig_type,
3113 tree *case_low_p, tree *case_high_p)
3115 tree min_value, max_value;
3116 tree case_low = *case_low_p;
3117 tree case_high = case_high_p ? *case_high_p : case_low;
3119 /* If there was a problem with the original type, do nothing. */
3120 if (orig_type == error_mark_node)
3121 return true;
3123 min_value = TYPE_MIN_VALUE (orig_type);
3124 max_value = TYPE_MAX_VALUE (orig_type);
3126 /* Case label is less than minimum for type. */
3127 if (tree_int_cst_compare (case_low, min_value) < 0
3128 && tree_int_cst_compare (case_high, min_value) < 0)
3130 warning (0, "case label value is less than minimum value for type");
3131 return false;
3134 /* Case value is greater than maximum for type. */
3135 if (tree_int_cst_compare (case_low, max_value) > 0
3136 && tree_int_cst_compare (case_high, max_value) > 0)
3138 warning (0, "case label value exceeds maximum value for type");
3139 return false;
3142 /* Saturate lower case label value to minimum. */
3143 if (tree_int_cst_compare (case_high, min_value) >= 0
3144 && tree_int_cst_compare (case_low, min_value) < 0)
3146 warning (0, "lower value in case label range"
3147 " less than minimum value for type");
3148 case_low = min_value;
3151 /* Saturate upper case label value to maximum. */
3152 if (tree_int_cst_compare (case_low, max_value) <= 0
3153 && tree_int_cst_compare (case_high, max_value) > 0)
3155 warning (0, "upper value in case label range"
3156 " exceeds maximum value for type");
3157 case_high = max_value;
3160 if (*case_low_p != case_low)
3161 *case_low_p = convert (type, case_low);
3162 if (case_high_p && *case_high_p != case_high)
3163 *case_high_p = convert (type, case_high);
3165 return true;
3168 /* Return an integer type with BITS bits of precision,
3169 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3171 tree
3172 c_common_type_for_size (unsigned int bits, int unsignedp)
3174 if (bits == TYPE_PRECISION (integer_type_node))
3175 return unsignedp ? unsigned_type_node : integer_type_node;
3177 if (bits == TYPE_PRECISION (signed_char_type_node))
3178 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3180 if (bits == TYPE_PRECISION (short_integer_type_node))
3181 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3183 if (bits == TYPE_PRECISION (long_integer_type_node))
3184 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3186 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3187 return (unsignedp ? long_long_unsigned_type_node
3188 : long_long_integer_type_node);
3190 if (int128_integer_type_node
3191 && bits == TYPE_PRECISION (int128_integer_type_node))
3192 return (unsignedp ? int128_unsigned_type_node
3193 : int128_integer_type_node);
3195 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3196 return (unsignedp ? widest_unsigned_literal_type_node
3197 : widest_integer_literal_type_node);
3199 if (bits <= TYPE_PRECISION (intQI_type_node))
3200 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3202 if (bits <= TYPE_PRECISION (intHI_type_node))
3203 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3205 if (bits <= TYPE_PRECISION (intSI_type_node))
3206 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3208 if (bits <= TYPE_PRECISION (intDI_type_node))
3209 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3211 return 0;
3214 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3215 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3216 and saturating if SATP is nonzero, otherwise not saturating. */
3218 tree
3219 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3220 int unsignedp, int satp)
3222 enum machine_mode mode;
3223 if (ibit == 0)
3224 mode = unsignedp ? UQQmode : QQmode;
3225 else
3226 mode = unsignedp ? UHAmode : HAmode;
3228 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3229 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3230 break;
3232 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3234 sorry ("GCC cannot support operators with integer types and "
3235 "fixed-point types that have too many integral and "
3236 "fractional bits together");
3237 return 0;
3240 return c_common_type_for_mode (mode, satp);
3243 /* Used for communication between c_common_type_for_mode and
3244 c_register_builtin_type. */
3245 static GTY(()) tree registered_builtin_types;
3247 /* Return a data type that has machine mode MODE.
3248 If the mode is an integer,
3249 then UNSIGNEDP selects between signed and unsigned types.
3250 If the mode is a fixed-point mode,
3251 then UNSIGNEDP selects between saturating and nonsaturating types. */
3253 tree
3254 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3256 tree t;
3258 if (mode == TYPE_MODE (integer_type_node))
3259 return unsignedp ? unsigned_type_node : integer_type_node;
3261 if (mode == TYPE_MODE (signed_char_type_node))
3262 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3264 if (mode == TYPE_MODE (short_integer_type_node))
3265 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3267 if (mode == TYPE_MODE (long_integer_type_node))
3268 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3270 if (mode == TYPE_MODE (long_long_integer_type_node))
3271 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3273 if (int128_integer_type_node
3274 && mode == TYPE_MODE (int128_integer_type_node))
3275 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3277 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3278 return unsignedp ? widest_unsigned_literal_type_node
3279 : widest_integer_literal_type_node;
3281 if (mode == QImode)
3282 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3284 if (mode == HImode)
3285 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3287 if (mode == SImode)
3288 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3290 if (mode == DImode)
3291 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3293 #if HOST_BITS_PER_WIDE_INT >= 64
3294 if (mode == TYPE_MODE (intTI_type_node))
3295 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3296 #endif
3298 if (mode == TYPE_MODE (float_type_node))
3299 return float_type_node;
3301 if (mode == TYPE_MODE (double_type_node))
3302 return double_type_node;
3304 if (mode == TYPE_MODE (long_double_type_node))
3305 return long_double_type_node;
3307 if (mode == TYPE_MODE (void_type_node))
3308 return void_type_node;
3310 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3311 return (unsignedp
3312 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3313 : make_signed_type (GET_MODE_PRECISION (mode)));
3315 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3316 return (unsignedp
3317 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3318 : make_signed_type (GET_MODE_PRECISION (mode)));
3320 if (COMPLEX_MODE_P (mode))
3322 enum machine_mode inner_mode;
3323 tree inner_type;
3325 if (mode == TYPE_MODE (complex_float_type_node))
3326 return complex_float_type_node;
3327 if (mode == TYPE_MODE (complex_double_type_node))
3328 return complex_double_type_node;
3329 if (mode == TYPE_MODE (complex_long_double_type_node))
3330 return complex_long_double_type_node;
3332 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3333 return complex_integer_type_node;
3335 inner_mode = GET_MODE_INNER (mode);
3336 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3337 if (inner_type != NULL_TREE)
3338 return build_complex_type (inner_type);
3340 else if (VECTOR_MODE_P (mode))
3342 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3343 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3344 if (inner_type != NULL_TREE)
3345 return build_vector_type_for_mode (inner_type, mode);
3348 if (mode == TYPE_MODE (dfloat32_type_node))
3349 return dfloat32_type_node;
3350 if (mode == TYPE_MODE (dfloat64_type_node))
3351 return dfloat64_type_node;
3352 if (mode == TYPE_MODE (dfloat128_type_node))
3353 return dfloat128_type_node;
3355 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3357 if (mode == TYPE_MODE (short_fract_type_node))
3358 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3359 if (mode == TYPE_MODE (fract_type_node))
3360 return unsignedp ? sat_fract_type_node : fract_type_node;
3361 if (mode == TYPE_MODE (long_fract_type_node))
3362 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3363 if (mode == TYPE_MODE (long_long_fract_type_node))
3364 return unsignedp ? sat_long_long_fract_type_node
3365 : long_long_fract_type_node;
3367 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3368 return unsignedp ? sat_unsigned_short_fract_type_node
3369 : unsigned_short_fract_type_node;
3370 if (mode == TYPE_MODE (unsigned_fract_type_node))
3371 return unsignedp ? sat_unsigned_fract_type_node
3372 : unsigned_fract_type_node;
3373 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3374 return unsignedp ? sat_unsigned_long_fract_type_node
3375 : unsigned_long_fract_type_node;
3376 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3377 return unsignedp ? sat_unsigned_long_long_fract_type_node
3378 : unsigned_long_long_fract_type_node;
3380 if (mode == TYPE_MODE (short_accum_type_node))
3381 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3382 if (mode == TYPE_MODE (accum_type_node))
3383 return unsignedp ? sat_accum_type_node : accum_type_node;
3384 if (mode == TYPE_MODE (long_accum_type_node))
3385 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3386 if (mode == TYPE_MODE (long_long_accum_type_node))
3387 return unsignedp ? sat_long_long_accum_type_node
3388 : long_long_accum_type_node;
3390 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3391 return unsignedp ? sat_unsigned_short_accum_type_node
3392 : unsigned_short_accum_type_node;
3393 if (mode == TYPE_MODE (unsigned_accum_type_node))
3394 return unsignedp ? sat_unsigned_accum_type_node
3395 : unsigned_accum_type_node;
3396 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3397 return unsignedp ? sat_unsigned_long_accum_type_node
3398 : unsigned_long_accum_type_node;
3399 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3400 return unsignedp ? sat_unsigned_long_long_accum_type_node
3401 : unsigned_long_long_accum_type_node;
3403 if (mode == QQmode)
3404 return unsignedp ? sat_qq_type_node : qq_type_node;
3405 if (mode == HQmode)
3406 return unsignedp ? sat_hq_type_node : hq_type_node;
3407 if (mode == SQmode)
3408 return unsignedp ? sat_sq_type_node : sq_type_node;
3409 if (mode == DQmode)
3410 return unsignedp ? sat_dq_type_node : dq_type_node;
3411 if (mode == TQmode)
3412 return unsignedp ? sat_tq_type_node : tq_type_node;
3414 if (mode == UQQmode)
3415 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3416 if (mode == UHQmode)
3417 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3418 if (mode == USQmode)
3419 return unsignedp ? sat_usq_type_node : usq_type_node;
3420 if (mode == UDQmode)
3421 return unsignedp ? sat_udq_type_node : udq_type_node;
3422 if (mode == UTQmode)
3423 return unsignedp ? sat_utq_type_node : utq_type_node;
3425 if (mode == HAmode)
3426 return unsignedp ? sat_ha_type_node : ha_type_node;
3427 if (mode == SAmode)
3428 return unsignedp ? sat_sa_type_node : sa_type_node;
3429 if (mode == DAmode)
3430 return unsignedp ? sat_da_type_node : da_type_node;
3431 if (mode == TAmode)
3432 return unsignedp ? sat_ta_type_node : ta_type_node;
3434 if (mode == UHAmode)
3435 return unsignedp ? sat_uha_type_node : uha_type_node;
3436 if (mode == USAmode)
3437 return unsignedp ? sat_usa_type_node : usa_type_node;
3438 if (mode == UDAmode)
3439 return unsignedp ? sat_uda_type_node : uda_type_node;
3440 if (mode == UTAmode)
3441 return unsignedp ? sat_uta_type_node : uta_type_node;
3444 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3445 if (TYPE_MODE (TREE_VALUE (t)) == mode
3446 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3447 return TREE_VALUE (t);
3449 return 0;
3452 tree
3453 c_common_unsigned_type (tree type)
3455 return c_common_signed_or_unsigned_type (1, type);
3458 /* Return a signed type the same as TYPE in other respects. */
3460 tree
3461 c_common_signed_type (tree type)
3463 return c_common_signed_or_unsigned_type (0, type);
3466 /* Return a type the same as TYPE except unsigned or
3467 signed according to UNSIGNEDP. */
3469 tree
3470 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3472 tree type1;
3474 /* This block of code emulates the behavior of the old
3475 c_common_unsigned_type. In particular, it returns
3476 long_unsigned_type_node if passed a long, even when a int would
3477 have the same size. This is necessary for warnings to work
3478 correctly in archs where sizeof(int) == sizeof(long) */
3480 type1 = TYPE_MAIN_VARIANT (type);
3481 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3482 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3483 if (type1 == integer_type_node || type1 == unsigned_type_node)
3484 return unsignedp ? unsigned_type_node : integer_type_node;
3485 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3486 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3487 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3488 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3489 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3490 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3491 if (int128_integer_type_node
3492 && (type1 == int128_integer_type_node
3493 || type1 == int128_unsigned_type_node))
3494 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3495 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3496 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3497 #if HOST_BITS_PER_WIDE_INT >= 64
3498 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3499 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3500 #endif
3501 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3502 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3503 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3504 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3505 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3506 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3507 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3508 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3510 #define C_COMMON_FIXED_TYPES(NAME) \
3511 if (type1 == short_ ## NAME ## _type_node \
3512 || type1 == unsigned_short_ ## NAME ## _type_node) \
3513 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3514 : short_ ## NAME ## _type_node; \
3515 if (type1 == NAME ## _type_node \
3516 || type1 == unsigned_ ## NAME ## _type_node) \
3517 return unsignedp ? unsigned_ ## NAME ## _type_node \
3518 : NAME ## _type_node; \
3519 if (type1 == long_ ## NAME ## _type_node \
3520 || type1 == unsigned_long_ ## NAME ## _type_node) \
3521 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3522 : long_ ## NAME ## _type_node; \
3523 if (type1 == long_long_ ## NAME ## _type_node \
3524 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3525 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3526 : long_long_ ## NAME ## _type_node;
3528 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3529 if (type1 == NAME ## _type_node \
3530 || type1 == u ## NAME ## _type_node) \
3531 return unsignedp ? u ## NAME ## _type_node \
3532 : NAME ## _type_node;
3534 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3535 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3536 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3537 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3538 : sat_ ## short_ ## NAME ## _type_node; \
3539 if (type1 == sat_ ## NAME ## _type_node \
3540 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3541 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3542 : sat_ ## NAME ## _type_node; \
3543 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3544 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3545 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3546 : sat_ ## long_ ## NAME ## _type_node; \
3547 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3548 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3549 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3550 : sat_ ## long_long_ ## NAME ## _type_node;
3552 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3553 if (type1 == sat_ ## NAME ## _type_node \
3554 || type1 == sat_ ## u ## NAME ## _type_node) \
3555 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3556 : sat_ ## NAME ## _type_node;
3558 C_COMMON_FIXED_TYPES (fract);
3559 C_COMMON_FIXED_TYPES_SAT (fract);
3560 C_COMMON_FIXED_TYPES (accum);
3561 C_COMMON_FIXED_TYPES_SAT (accum);
3563 C_COMMON_FIXED_MODE_TYPES (qq);
3564 C_COMMON_FIXED_MODE_TYPES (hq);
3565 C_COMMON_FIXED_MODE_TYPES (sq);
3566 C_COMMON_FIXED_MODE_TYPES (dq);
3567 C_COMMON_FIXED_MODE_TYPES (tq);
3568 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3569 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3570 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3571 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3572 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3573 C_COMMON_FIXED_MODE_TYPES (ha);
3574 C_COMMON_FIXED_MODE_TYPES (sa);
3575 C_COMMON_FIXED_MODE_TYPES (da);
3576 C_COMMON_FIXED_MODE_TYPES (ta);
3577 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3578 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3579 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3580 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3582 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3583 the precision; they have precision set to match their range, but
3584 may use a wider mode to match an ABI. If we change modes, we may
3585 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3586 the precision as well, so as to yield correct results for
3587 bit-field types. C++ does not have these separate bit-field
3588 types, and producing a signed or unsigned variant of an
3589 ENUMERAL_TYPE may cause other problems as well. */
3591 if (!INTEGRAL_TYPE_P (type)
3592 || TYPE_UNSIGNED (type) == unsignedp)
3593 return type;
3595 #define TYPE_OK(node) \
3596 (TYPE_MODE (type) == TYPE_MODE (node) \
3597 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3598 if (TYPE_OK (signed_char_type_node))
3599 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3600 if (TYPE_OK (integer_type_node))
3601 return unsignedp ? unsigned_type_node : integer_type_node;
3602 if (TYPE_OK (short_integer_type_node))
3603 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3604 if (TYPE_OK (long_integer_type_node))
3605 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3606 if (TYPE_OK (long_long_integer_type_node))
3607 return (unsignedp ? long_long_unsigned_type_node
3608 : long_long_integer_type_node);
3609 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3610 return (unsignedp ? int128_unsigned_type_node
3611 : int128_integer_type_node);
3612 if (TYPE_OK (widest_integer_literal_type_node))
3613 return (unsignedp ? widest_unsigned_literal_type_node
3614 : widest_integer_literal_type_node);
3616 #if HOST_BITS_PER_WIDE_INT >= 64
3617 if (TYPE_OK (intTI_type_node))
3618 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3619 #endif
3620 if (TYPE_OK (intDI_type_node))
3621 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3622 if (TYPE_OK (intSI_type_node))
3623 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3624 if (TYPE_OK (intHI_type_node))
3625 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3626 if (TYPE_OK (intQI_type_node))
3627 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3628 #undef TYPE_OK
3630 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3633 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3635 tree
3636 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3638 /* Extended integer types of the same width as a standard type have
3639 lesser rank, so those of the same width as int promote to int or
3640 unsigned int and are valid for printf formats expecting int or
3641 unsigned int. To avoid such special cases, avoid creating
3642 extended integer types for bit-fields if a standard integer type
3643 is available. */
3644 if (width == TYPE_PRECISION (integer_type_node))
3645 return unsignedp ? unsigned_type_node : integer_type_node;
3646 if (width == TYPE_PRECISION (signed_char_type_node))
3647 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3648 if (width == TYPE_PRECISION (short_integer_type_node))
3649 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3650 if (width == TYPE_PRECISION (long_integer_type_node))
3651 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3652 if (width == TYPE_PRECISION (long_long_integer_type_node))
3653 return (unsignedp ? long_long_unsigned_type_node
3654 : long_long_integer_type_node);
3655 if (int128_integer_type_node
3656 && width == TYPE_PRECISION (int128_integer_type_node))
3657 return (unsignedp ? int128_unsigned_type_node
3658 : int128_integer_type_node);
3659 return build_nonstandard_integer_type (width, unsignedp);
3662 /* The C version of the register_builtin_type langhook. */
3664 void
3665 c_register_builtin_type (tree type, const char* name)
3667 tree decl;
3669 decl = build_decl (UNKNOWN_LOCATION,
3670 TYPE_DECL, get_identifier (name), type);
3671 DECL_ARTIFICIAL (decl) = 1;
3672 if (!TYPE_NAME (type))
3673 TYPE_NAME (type) = decl;
3674 pushdecl (decl);
3676 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3679 /* Print an error message for invalid operands to arith operation
3680 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3681 LOCATION is the location of the message. */
3683 void
3684 binary_op_error (location_t location, enum tree_code code,
3685 tree type0, tree type1)
3687 const char *opname;
3689 switch (code)
3691 case PLUS_EXPR:
3692 opname = "+"; break;
3693 case MINUS_EXPR:
3694 opname = "-"; break;
3695 case MULT_EXPR:
3696 opname = "*"; break;
3697 case MAX_EXPR:
3698 opname = "max"; break;
3699 case MIN_EXPR:
3700 opname = "min"; break;
3701 case EQ_EXPR:
3702 opname = "=="; break;
3703 case NE_EXPR:
3704 opname = "!="; break;
3705 case LE_EXPR:
3706 opname = "<="; break;
3707 case GE_EXPR:
3708 opname = ">="; break;
3709 case LT_EXPR:
3710 opname = "<"; break;
3711 case GT_EXPR:
3712 opname = ">"; break;
3713 case LSHIFT_EXPR:
3714 opname = "<<"; break;
3715 case RSHIFT_EXPR:
3716 opname = ">>"; break;
3717 case TRUNC_MOD_EXPR:
3718 case FLOOR_MOD_EXPR:
3719 opname = "%"; break;
3720 case TRUNC_DIV_EXPR:
3721 case FLOOR_DIV_EXPR:
3722 opname = "/"; break;
3723 case BIT_AND_EXPR:
3724 opname = "&"; break;
3725 case BIT_IOR_EXPR:
3726 opname = "|"; break;
3727 case TRUTH_ANDIF_EXPR:
3728 opname = "&&"; break;
3729 case TRUTH_ORIF_EXPR:
3730 opname = "||"; break;
3731 case BIT_XOR_EXPR:
3732 opname = "^"; break;
3733 default:
3734 gcc_unreachable ();
3736 error_at (location,
3737 "invalid operands to binary %s (have %qT and %qT)", opname,
3738 type0, type1);
3741 /* Given an expression as a tree, return its original type. Do this
3742 by stripping any conversion that preserves the sign and precision. */
3743 static tree
3744 expr_original_type (tree expr)
3746 STRIP_SIGN_NOPS (expr);
3747 return TREE_TYPE (expr);
3750 /* Subroutine of build_binary_op, used for comparison operations.
3751 See if the operands have both been converted from subword integer types
3752 and, if so, perhaps change them both back to their original type.
3753 This function is also responsible for converting the two operands
3754 to the proper common type for comparison.
3756 The arguments of this function are all pointers to local variables
3757 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3758 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3760 If this function returns nonzero, it means that the comparison has
3761 a constant value. What this function returns is an expression for
3762 that value. */
3764 tree
3765 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3766 enum tree_code *rescode_ptr)
3768 tree type;
3769 tree op0 = *op0_ptr;
3770 tree op1 = *op1_ptr;
3771 int unsignedp0, unsignedp1;
3772 int real1, real2;
3773 tree primop0, primop1;
3774 enum tree_code code = *rescode_ptr;
3775 location_t loc = EXPR_LOC_OR_HERE (op0);
3777 /* Throw away any conversions to wider types
3778 already present in the operands. */
3780 primop0 = c_common_get_narrower (op0, &unsignedp0);
3781 primop1 = c_common_get_narrower (op1, &unsignedp1);
3783 /* If primopN is first sign-extended from primopN's precision to opN's
3784 precision, then zero-extended from opN's precision to
3785 *restype_ptr precision, shortenings might be invalid. */
3786 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3787 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3788 && !unsignedp0
3789 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3790 primop0 = op0;
3791 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3792 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3793 && !unsignedp1
3794 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3795 primop1 = op1;
3797 /* Handle the case that OP0 does not *contain* a conversion
3798 but it *requires* conversion to FINAL_TYPE. */
3800 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3801 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3802 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3803 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3805 /* If one of the operands must be floated, we cannot optimize. */
3806 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3807 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3809 /* If first arg is constant, swap the args (changing operation
3810 so value is preserved), for canonicalization. Don't do this if
3811 the second arg is 0. */
3813 if (TREE_CONSTANT (primop0)
3814 && !integer_zerop (primop1) && !real_zerop (primop1)
3815 && !fixed_zerop (primop1))
3817 tree tem = primop0;
3818 int temi = unsignedp0;
3819 primop0 = primop1;
3820 primop1 = tem;
3821 tem = op0;
3822 op0 = op1;
3823 op1 = tem;
3824 *op0_ptr = op0;
3825 *op1_ptr = op1;
3826 unsignedp0 = unsignedp1;
3827 unsignedp1 = temi;
3828 temi = real1;
3829 real1 = real2;
3830 real2 = temi;
3832 switch (code)
3834 case LT_EXPR:
3835 code = GT_EXPR;
3836 break;
3837 case GT_EXPR:
3838 code = LT_EXPR;
3839 break;
3840 case LE_EXPR:
3841 code = GE_EXPR;
3842 break;
3843 case GE_EXPR:
3844 code = LE_EXPR;
3845 break;
3846 default:
3847 break;
3849 *rescode_ptr = code;
3852 /* If comparing an integer against a constant more bits wide,
3853 maybe we can deduce a value of 1 or 0 independent of the data.
3854 Or else truncate the constant now
3855 rather than extend the variable at run time.
3857 This is only interesting if the constant is the wider arg.
3858 Also, it is not safe if the constant is unsigned and the
3859 variable arg is signed, since in this case the variable
3860 would be sign-extended and then regarded as unsigned.
3861 Our technique fails in this case because the lowest/highest
3862 possible unsigned results don't follow naturally from the
3863 lowest/highest possible values of the variable operand.
3864 For just EQ_EXPR and NE_EXPR there is another technique that
3865 could be used: see if the constant can be faithfully represented
3866 in the other operand's type, by truncating it and reextending it
3867 and see if that preserves the constant's value. */
3869 if (!real1 && !real2
3870 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3871 && TREE_CODE (primop1) == INTEGER_CST
3872 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3874 int min_gt, max_gt, min_lt, max_lt;
3875 tree maxval, minval;
3876 /* 1 if comparison is nominally unsigned. */
3877 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3878 tree val;
3880 type = c_common_signed_or_unsigned_type (unsignedp0,
3881 TREE_TYPE (primop0));
3883 maxval = TYPE_MAX_VALUE (type);
3884 minval = TYPE_MIN_VALUE (type);
3886 if (unsignedp && !unsignedp0)
3887 *restype_ptr = c_common_signed_type (*restype_ptr);
3889 if (TREE_TYPE (primop1) != *restype_ptr)
3891 /* Convert primop1 to target type, but do not introduce
3892 additional overflow. We know primop1 is an int_cst. */
3893 primop1 = force_fit_type_double (*restype_ptr,
3894 tree_to_double_int (primop1),
3895 0, TREE_OVERFLOW (primop1));
3897 if (type != *restype_ptr)
3899 minval = convert (*restype_ptr, minval);
3900 maxval = convert (*restype_ptr, maxval);
3903 if (unsignedp && unsignedp0)
3905 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3906 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3907 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3908 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3910 else
3912 min_gt = INT_CST_LT (primop1, minval);
3913 max_gt = INT_CST_LT (primop1, maxval);
3914 min_lt = INT_CST_LT (minval, primop1);
3915 max_lt = INT_CST_LT (maxval, primop1);
3918 val = 0;
3919 /* This used to be a switch, but Genix compiler can't handle that. */
3920 if (code == NE_EXPR)
3922 if (max_lt || min_gt)
3923 val = truthvalue_true_node;
3925 else if (code == EQ_EXPR)
3927 if (max_lt || min_gt)
3928 val = truthvalue_false_node;
3930 else if (code == LT_EXPR)
3932 if (max_lt)
3933 val = truthvalue_true_node;
3934 if (!min_lt)
3935 val = truthvalue_false_node;
3937 else if (code == GT_EXPR)
3939 if (min_gt)
3940 val = truthvalue_true_node;
3941 if (!max_gt)
3942 val = truthvalue_false_node;
3944 else if (code == LE_EXPR)
3946 if (!max_gt)
3947 val = truthvalue_true_node;
3948 if (min_gt)
3949 val = truthvalue_false_node;
3951 else if (code == GE_EXPR)
3953 if (!min_lt)
3954 val = truthvalue_true_node;
3955 if (max_lt)
3956 val = truthvalue_false_node;
3959 /* If primop0 was sign-extended and unsigned comparison specd,
3960 we did a signed comparison above using the signed type bounds.
3961 But the comparison we output must be unsigned.
3963 Also, for inequalities, VAL is no good; but if the signed
3964 comparison had *any* fixed result, it follows that the
3965 unsigned comparison just tests the sign in reverse
3966 (positive values are LE, negative ones GE).
3967 So we can generate an unsigned comparison
3968 against an extreme value of the signed type. */
3970 if (unsignedp && !unsignedp0)
3972 if (val != 0)
3973 switch (code)
3975 case LT_EXPR:
3976 case GE_EXPR:
3977 primop1 = TYPE_MIN_VALUE (type);
3978 val = 0;
3979 break;
3981 case LE_EXPR:
3982 case GT_EXPR:
3983 primop1 = TYPE_MAX_VALUE (type);
3984 val = 0;
3985 break;
3987 default:
3988 break;
3990 type = c_common_unsigned_type (type);
3993 if (TREE_CODE (primop0) != INTEGER_CST
3994 && c_inhibit_evaluation_warnings == 0)
3996 if (val == truthvalue_false_node)
3997 warning_at (loc, OPT_Wtype_limits,
3998 "comparison is always false due to limited range of data type");
3999 if (val == truthvalue_true_node)
4000 warning_at (loc, OPT_Wtype_limits,
4001 "comparison is always true due to limited range of data type");
4004 if (val != 0)
4006 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4007 if (TREE_SIDE_EFFECTS (primop0))
4008 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4009 return val;
4012 /* Value is not predetermined, but do the comparison
4013 in the type of the operand that is not constant.
4014 TYPE is already properly set. */
4017 /* If either arg is decimal float and the other is float, find the
4018 proper common type to use for comparison. */
4019 else if (real1 && real2
4020 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4021 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4022 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4024 else if (real1 && real2
4025 && (TYPE_PRECISION (TREE_TYPE (primop0))
4026 == TYPE_PRECISION (TREE_TYPE (primop1))))
4027 type = TREE_TYPE (primop0);
4029 /* If args' natural types are both narrower than nominal type
4030 and both extend in the same manner, compare them
4031 in the type of the wider arg.
4032 Otherwise must actually extend both to the nominal
4033 common type lest different ways of extending
4034 alter the result.
4035 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4037 else if (unsignedp0 == unsignedp1 && real1 == real2
4038 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4039 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4041 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4042 type = c_common_signed_or_unsigned_type (unsignedp0
4043 || TYPE_UNSIGNED (*restype_ptr),
4044 type);
4045 /* Make sure shorter operand is extended the right way
4046 to match the longer operand. */
4047 primop0
4048 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4049 TREE_TYPE (primop0)),
4050 primop0);
4051 primop1
4052 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4053 TREE_TYPE (primop1)),
4054 primop1);
4056 else
4058 /* Here we must do the comparison on the nominal type
4059 using the args exactly as we received them. */
4060 type = *restype_ptr;
4061 primop0 = op0;
4062 primop1 = op1;
4064 if (!real1 && !real2 && integer_zerop (primop1)
4065 && TYPE_UNSIGNED (*restype_ptr))
4067 tree value = 0;
4068 /* All unsigned values are >= 0, so we warn. However,
4069 if OP0 is a constant that is >= 0, the signedness of
4070 the comparison isn't an issue, so suppress the
4071 warning. */
4072 bool warn =
4073 warn_type_limits && !in_system_header
4074 && c_inhibit_evaluation_warnings == 0
4075 && !(TREE_CODE (primop0) == INTEGER_CST
4076 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4077 primop0)))
4078 /* Do not warn for enumeration types. */
4079 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4081 switch (code)
4083 case GE_EXPR:
4084 if (warn)
4085 warning_at (loc, OPT_Wtype_limits,
4086 "comparison of unsigned expression >= 0 is always true");
4087 value = truthvalue_true_node;
4088 break;
4090 case LT_EXPR:
4091 if (warn)
4092 warning_at (loc, OPT_Wtype_limits,
4093 "comparison of unsigned expression < 0 is always false");
4094 value = truthvalue_false_node;
4095 break;
4097 default:
4098 break;
4101 if (value != 0)
4103 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4104 if (TREE_SIDE_EFFECTS (primop0))
4105 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4106 primop0, value);
4107 return value;
4112 *op0_ptr = convert (type, primop0);
4113 *op1_ptr = convert (type, primop1);
4115 *restype_ptr = truthvalue_type_node;
4117 return 0;
4120 /* Return a tree for the sum or difference (RESULTCODE says which)
4121 of pointer PTROP and integer INTOP. */
4123 tree
4124 pointer_int_sum (location_t loc, enum tree_code resultcode,
4125 tree ptrop, tree intop)
4127 tree size_exp, ret;
4129 /* The result is a pointer of the same type that is being added. */
4130 tree result_type = TREE_TYPE (ptrop);
4132 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4134 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4135 "pointer of type %<void *%> used in arithmetic");
4136 size_exp = integer_one_node;
4138 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4140 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4141 "pointer to a function used in arithmetic");
4142 size_exp = integer_one_node;
4144 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4146 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4147 "pointer to member function used in arithmetic");
4148 size_exp = integer_one_node;
4150 else
4151 size_exp = size_in_bytes (TREE_TYPE (result_type));
4153 /* We are manipulating pointer values, so we don't need to warn
4154 about relying on undefined signed overflow. We disable the
4155 warning here because we use integer types so fold won't know that
4156 they are really pointers. */
4157 fold_defer_overflow_warnings ();
4159 /* If what we are about to multiply by the size of the elements
4160 contains a constant term, apply distributive law
4161 and multiply that constant term separately.
4162 This helps produce common subexpressions. */
4163 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4164 && !TREE_CONSTANT (intop)
4165 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4166 && TREE_CONSTANT (size_exp)
4167 /* If the constant comes from pointer subtraction,
4168 skip this optimization--it would cause an error. */
4169 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4170 /* If the constant is unsigned, and smaller than the pointer size,
4171 then we must skip this optimization. This is because it could cause
4172 an overflow error if the constant is negative but INTOP is not. */
4173 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4174 || (TYPE_PRECISION (TREE_TYPE (intop))
4175 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4177 enum tree_code subcode = resultcode;
4178 tree int_type = TREE_TYPE (intop);
4179 if (TREE_CODE (intop) == MINUS_EXPR)
4180 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4181 /* Convert both subexpression types to the type of intop,
4182 because weird cases involving pointer arithmetic
4183 can result in a sum or difference with different type args. */
4184 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4185 subcode, ptrop,
4186 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4187 intop = convert (int_type, TREE_OPERAND (intop, 0));
4190 /* Convert the integer argument to a type the same size as sizetype
4191 so the multiply won't overflow spuriously. */
4192 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4193 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4194 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4195 TYPE_UNSIGNED (sizetype)), intop);
4197 /* Replace the integer argument with a suitable product by the object size.
4198 Do this multiplication as signed, then convert to the appropriate type
4199 for the pointer operation and disregard an overflow that occurred only
4200 because of the sign-extension change in the latter conversion. */
4202 tree t = build_binary_op (loc,
4203 MULT_EXPR, intop,
4204 convert (TREE_TYPE (intop), size_exp), 1);
4205 intop = convert (sizetype, t);
4206 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4207 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4208 TREE_INT_CST_HIGH (intop));
4211 /* Create the sum or difference. */
4212 if (resultcode == MINUS_EXPR)
4213 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4215 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4217 fold_undefer_and_ignore_overflow_warnings ();
4219 return ret;
4222 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4223 and if NON_CONST is known not to be permitted in an evaluated part
4224 of a constant expression. */
4226 tree
4227 c_wrap_maybe_const (tree expr, bool non_const)
4229 bool nowarning = TREE_NO_WARNING (expr);
4230 location_t loc = EXPR_LOCATION (expr);
4232 /* This should never be called for C++. */
4233 if (c_dialect_cxx ())
4234 gcc_unreachable ();
4236 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4237 STRIP_TYPE_NOPS (expr);
4238 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4239 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4240 if (nowarning)
4241 TREE_NO_WARNING (expr) = 1;
4242 protected_set_expr_location (expr, loc);
4244 return expr;
4247 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4248 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4249 around the SAVE_EXPR if needed so that c_fully_fold does not need
4250 to look inside SAVE_EXPRs. */
4252 tree
4253 c_save_expr (tree expr)
4255 bool maybe_const = true;
4256 if (c_dialect_cxx ())
4257 return save_expr (expr);
4258 expr = c_fully_fold (expr, false, &maybe_const);
4259 expr = save_expr (expr);
4260 if (!maybe_const)
4261 expr = c_wrap_maybe_const (expr, true);
4262 return expr;
4265 /* Return whether EXPR is a declaration whose address can never be
4266 NULL. */
4268 bool
4269 decl_with_nonnull_addr_p (const_tree expr)
4271 return (DECL_P (expr)
4272 && (TREE_CODE (expr) == PARM_DECL
4273 || TREE_CODE (expr) == LABEL_DECL
4274 || !DECL_WEAK (expr)));
4277 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4278 or for an `if' or `while' statement or ?..: exp. It should already
4279 have been validated to be of suitable type; otherwise, a bad
4280 diagnostic may result.
4282 The EXPR is located at LOCATION.
4284 This preparation consists of taking the ordinary
4285 representation of an expression expr and producing a valid tree
4286 boolean expression describing whether expr is nonzero. We could
4287 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4288 but we optimize comparisons, &&, ||, and !.
4290 The resulting type should always be `truthvalue_type_node'. */
4292 tree
4293 c_common_truthvalue_conversion (location_t location, tree expr)
4295 switch (TREE_CODE (expr))
4297 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4298 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4299 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4300 case ORDERED_EXPR: case UNORDERED_EXPR:
4301 if (TREE_TYPE (expr) == truthvalue_type_node)
4302 return expr;
4303 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4304 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4305 goto ret;
4307 case TRUTH_ANDIF_EXPR:
4308 case TRUTH_ORIF_EXPR:
4309 case TRUTH_AND_EXPR:
4310 case TRUTH_OR_EXPR:
4311 case TRUTH_XOR_EXPR:
4312 if (TREE_TYPE (expr) == truthvalue_type_node)
4313 return expr;
4314 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4315 c_common_truthvalue_conversion (location,
4316 TREE_OPERAND (expr, 0)),
4317 c_common_truthvalue_conversion (location,
4318 TREE_OPERAND (expr, 1)));
4319 goto ret;
4321 case TRUTH_NOT_EXPR:
4322 if (TREE_TYPE (expr) == truthvalue_type_node)
4323 return expr;
4324 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4325 c_common_truthvalue_conversion (location,
4326 TREE_OPERAND (expr, 0)));
4327 goto ret;
4329 case ERROR_MARK:
4330 return expr;
4332 case INTEGER_CST:
4333 return integer_zerop (expr) ? truthvalue_false_node
4334 : truthvalue_true_node;
4336 case REAL_CST:
4337 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4338 ? truthvalue_true_node
4339 : truthvalue_false_node;
4341 case FIXED_CST:
4342 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4343 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4344 ? truthvalue_true_node
4345 : truthvalue_false_node;
4347 case FUNCTION_DECL:
4348 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4349 /* Fall through. */
4351 case ADDR_EXPR:
4353 tree inner = TREE_OPERAND (expr, 0);
4354 if (decl_with_nonnull_addr_p (inner))
4356 /* Common Ada/Pascal programmer's mistake. */
4357 warning_at (location,
4358 OPT_Waddress,
4359 "the address of %qD will always evaluate as %<true%>",
4360 inner);
4361 return truthvalue_true_node;
4363 break;
4366 case COMPLEX_EXPR:
4367 expr = build_binary_op (EXPR_LOCATION (expr),
4368 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4369 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4370 c_common_truthvalue_conversion (location,
4371 TREE_OPERAND (expr, 0)),
4372 c_common_truthvalue_conversion (location,
4373 TREE_OPERAND (expr, 1)),
4375 goto ret;
4377 case NEGATE_EXPR:
4378 case ABS_EXPR:
4379 case FLOAT_EXPR:
4380 case EXCESS_PRECISION_EXPR:
4381 /* These don't change whether an object is nonzero or zero. */
4382 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4384 case LROTATE_EXPR:
4385 case RROTATE_EXPR:
4386 /* These don't change whether an object is zero or nonzero, but
4387 we can't ignore them if their second arg has side-effects. */
4388 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4390 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4391 TREE_OPERAND (expr, 1),
4392 c_common_truthvalue_conversion
4393 (location, TREE_OPERAND (expr, 0)));
4394 goto ret;
4396 else
4397 return c_common_truthvalue_conversion (location,
4398 TREE_OPERAND (expr, 0));
4400 case COND_EXPR:
4401 /* Distribute the conversion into the arms of a COND_EXPR. */
4402 if (c_dialect_cxx ())
4404 tree op1 = TREE_OPERAND (expr, 1);
4405 tree op2 = TREE_OPERAND (expr, 2);
4406 /* In C++ one of the arms might have void type if it is throw. */
4407 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4408 op1 = c_common_truthvalue_conversion (location, op1);
4409 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4410 op2 = c_common_truthvalue_conversion (location, op2);
4411 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4412 TREE_OPERAND (expr, 0), op1, op2);
4413 goto ret;
4415 else
4417 /* Folding will happen later for C. */
4418 expr = build3 (COND_EXPR, truthvalue_type_node,
4419 TREE_OPERAND (expr, 0),
4420 c_common_truthvalue_conversion (location,
4421 TREE_OPERAND (expr, 1)),
4422 c_common_truthvalue_conversion (location,
4423 TREE_OPERAND (expr, 2)));
4424 goto ret;
4427 CASE_CONVERT:
4429 tree totype = TREE_TYPE (expr);
4430 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4432 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4433 since that affects how `default_conversion' will behave. */
4434 if (TREE_CODE (totype) == REFERENCE_TYPE
4435 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4436 break;
4437 /* Don't strip a conversion from C++0x scoped enum, since they
4438 don't implicitly convert to other types. */
4439 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4440 && ENUM_IS_SCOPED (fromtype))
4441 break;
4442 /* If this isn't narrowing the argument, we can ignore it. */
4443 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4444 return c_common_truthvalue_conversion (location,
4445 TREE_OPERAND (expr, 0));
4447 break;
4449 case MODIFY_EXPR:
4450 if (!TREE_NO_WARNING (expr)
4451 && warn_parentheses)
4453 warning (OPT_Wparentheses,
4454 "suggest parentheses around assignment used as truth value");
4455 TREE_NO_WARNING (expr) = 1;
4457 break;
4459 default:
4460 break;
4463 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4465 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4466 expr = (build_binary_op
4467 (EXPR_LOCATION (expr),
4468 (TREE_SIDE_EFFECTS (expr)
4469 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4470 c_common_truthvalue_conversion
4471 (location,
4472 build_unary_op (location, REALPART_EXPR, t, 0)),
4473 c_common_truthvalue_conversion
4474 (location,
4475 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4476 0));
4477 goto ret;
4480 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4482 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4483 FCONST0 (TYPE_MODE
4484 (TREE_TYPE (expr))));
4485 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4487 else
4488 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4490 ret:
4491 protected_set_expr_location (expr, location);
4492 return expr;
4495 static void def_builtin_1 (enum built_in_function fncode,
4496 const char *name,
4497 enum built_in_class fnclass,
4498 tree fntype, tree libtype,
4499 bool both_p, bool fallback_p, bool nonansi_p,
4500 tree fnattrs, bool implicit_p);
4503 /* Apply the TYPE_QUALS to the new DECL. */
4505 void
4506 c_apply_type_quals_to_decl (int type_quals, tree decl)
4508 tree type = TREE_TYPE (decl);
4510 if (type == error_mark_node)
4511 return;
4513 if ((type_quals & TYPE_QUAL_CONST)
4514 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4515 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4516 constructor can produce constant init, so rely on cp_finish_decl to
4517 clear TREE_READONLY if the variable has non-constant init. */
4518 TREE_READONLY (decl) = 1;
4519 if (type_quals & TYPE_QUAL_VOLATILE)
4521 TREE_SIDE_EFFECTS (decl) = 1;
4522 TREE_THIS_VOLATILE (decl) = 1;
4524 if (type_quals & TYPE_QUAL_RESTRICT)
4526 while (type && TREE_CODE (type) == ARRAY_TYPE)
4527 /* Allow 'restrict' on arrays of pointers.
4528 FIXME currently we just ignore it. */
4529 type = TREE_TYPE (type);
4530 if (!type
4531 || !POINTER_TYPE_P (type)
4532 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4533 error ("invalid use of %<restrict%>");
4537 /* Hash function for the problem of multiple type definitions in
4538 different files. This must hash all types that will compare
4539 equal via comptypes to the same value. In practice it hashes
4540 on some of the simple stuff and leaves the details to comptypes. */
4542 static hashval_t
4543 c_type_hash (const void *p)
4545 int n_elements;
4546 int shift, size;
4547 const_tree const t = (const_tree) p;
4548 tree t2;
4549 switch (TREE_CODE (t))
4551 /* For pointers, hash on pointee type plus some swizzling. */
4552 case POINTER_TYPE:
4553 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4554 /* Hash on number of elements and total size. */
4555 case ENUMERAL_TYPE:
4556 shift = 3;
4557 t2 = TYPE_VALUES (t);
4558 break;
4559 case RECORD_TYPE:
4560 shift = 0;
4561 t2 = TYPE_FIELDS (t);
4562 break;
4563 case QUAL_UNION_TYPE:
4564 shift = 1;
4565 t2 = TYPE_FIELDS (t);
4566 break;
4567 case UNION_TYPE:
4568 shift = 2;
4569 t2 = TYPE_FIELDS (t);
4570 break;
4571 default:
4572 gcc_unreachable ();
4574 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4575 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4576 n_elements = list_length (t2);
4577 /* We might have a VLA here. */
4578 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4579 size = 0;
4580 else
4581 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4582 return ((size << 24) | (n_elements << shift));
4585 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4587 /* Return the typed-based alias set for T, which may be an expression
4588 or a type. Return -1 if we don't do anything special. */
4590 alias_set_type
4591 c_common_get_alias_set (tree t)
4593 tree u;
4594 PTR *slot;
4596 /* For VLAs, use the alias set of the element type rather than the
4597 default of alias set 0 for types compared structurally. */
4598 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4600 if (TREE_CODE (t) == ARRAY_TYPE)
4601 return get_alias_set (TREE_TYPE (t));
4602 return -1;
4605 /* Permit type-punning when accessing a union, provided the access
4606 is directly through the union. For example, this code does not
4607 permit taking the address of a union member and then storing
4608 through it. Even the type-punning allowed here is a GCC
4609 extension, albeit a common and useful one; the C standard says
4610 that such accesses have implementation-defined behavior. */
4611 for (u = t;
4612 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4613 u = TREE_OPERAND (u, 0))
4614 if (TREE_CODE (u) == COMPONENT_REF
4615 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4616 return 0;
4618 /* That's all the expressions we handle specially. */
4619 if (!TYPE_P (t))
4620 return -1;
4622 /* The C standard guarantees that any object may be accessed via an
4623 lvalue that has character type. */
4624 if (t == char_type_node
4625 || t == signed_char_type_node
4626 || t == unsigned_char_type_node)
4627 return 0;
4629 /* The C standard specifically allows aliasing between signed and
4630 unsigned variants of the same type. We treat the signed
4631 variant as canonical. */
4632 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4634 tree t1 = c_common_signed_type (t);
4636 /* t1 == t can happen for boolean nodes which are always unsigned. */
4637 if (t1 != t)
4638 return get_alias_set (t1);
4641 /* Handle the case of multiple type nodes referring to "the same" type,
4642 which occurs with IMA. These share an alias set. FIXME: Currently only
4643 C90 is handled. (In C99 type compatibility is not transitive, which
4644 complicates things mightily. The alias set splay trees can theoretically
4645 represent this, but insertion is tricky when you consider all the
4646 different orders things might arrive in.) */
4648 if (c_language != clk_c || flag_isoc99)
4649 return -1;
4651 /* Save time if there's only one input file. */
4652 if (num_in_fnames == 1)
4653 return -1;
4655 /* Pointers need special handling if they point to any type that
4656 needs special handling (below). */
4657 if (TREE_CODE (t) == POINTER_TYPE)
4659 tree t2;
4660 /* Find bottom type under any nested POINTERs. */
4661 for (t2 = TREE_TYPE (t);
4662 TREE_CODE (t2) == POINTER_TYPE;
4663 t2 = TREE_TYPE (t2))
4665 if (TREE_CODE (t2) != RECORD_TYPE
4666 && TREE_CODE (t2) != ENUMERAL_TYPE
4667 && TREE_CODE (t2) != QUAL_UNION_TYPE
4668 && TREE_CODE (t2) != UNION_TYPE)
4669 return -1;
4670 if (TYPE_SIZE (t2) == 0)
4671 return -1;
4673 /* These are the only cases that need special handling. */
4674 if (TREE_CODE (t) != RECORD_TYPE
4675 && TREE_CODE (t) != ENUMERAL_TYPE
4676 && TREE_CODE (t) != QUAL_UNION_TYPE
4677 && TREE_CODE (t) != UNION_TYPE
4678 && TREE_CODE (t) != POINTER_TYPE)
4679 return -1;
4680 /* Undefined? */
4681 if (TYPE_SIZE (t) == 0)
4682 return -1;
4684 /* Look up t in hash table. Only one of the compatible types within each
4685 alias set is recorded in the table. */
4686 if (!type_hash_table)
4687 type_hash_table = htab_create_ggc (1021, c_type_hash,
4688 (htab_eq) lang_hooks.types_compatible_p,
4689 NULL);
4690 slot = htab_find_slot (type_hash_table, t, INSERT);
4691 if (*slot != NULL)
4693 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4694 return TYPE_ALIAS_SET ((tree)*slot);
4696 else
4697 /* Our caller will assign and record (in t) a new alias set; all we need
4698 to do is remember t in the hash table. */
4699 *slot = t;
4701 return -1;
4704 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4705 the second parameter indicates which OPERATOR is being applied.
4706 The COMPLAIN flag controls whether we should diagnose possibly
4707 ill-formed constructs or not. LOC is the location of the SIZEOF or
4708 TYPEOF operator. */
4710 tree
4711 c_sizeof_or_alignof_type (location_t loc,
4712 tree type, bool is_sizeof, int complain)
4714 const char *op_name;
4715 tree value = NULL;
4716 enum tree_code type_code = TREE_CODE (type);
4718 op_name = is_sizeof ? "sizeof" : "__alignof__";
4720 if (type_code == FUNCTION_TYPE)
4722 if (is_sizeof)
4724 if (complain && (pedantic || warn_pointer_arith))
4725 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4726 "invalid application of %<sizeof%> to a function type");
4727 else if (!complain)
4728 return error_mark_node;
4729 value = size_one_node;
4731 else
4733 if (complain)
4735 if (c_dialect_cxx ())
4736 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4737 "%<alignof%> applied to a function type");
4738 else
4739 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4740 "%<_Alignof%> applied to a function type");
4742 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4745 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4747 if (type_code == VOID_TYPE
4748 && complain && (pedantic || warn_pointer_arith))
4749 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
4750 "invalid application of %qs to a void type", op_name);
4751 else if (!complain)
4752 return error_mark_node;
4753 value = size_one_node;
4755 else if (!COMPLETE_TYPE_P (type)
4756 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4758 if (complain)
4759 error_at (loc, "invalid application of %qs to incomplete type %qT",
4760 op_name, type);
4761 return error_mark_node;
4763 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4764 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4766 if (complain)
4767 error_at (loc, "invalid application of %qs to array type %qT of "
4768 "incomplete element type", op_name, type);
4769 return error_mark_node;
4771 else
4773 if (is_sizeof)
4774 /* Convert in case a char is more than one unit. */
4775 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4776 size_int (TYPE_PRECISION (char_type_node)
4777 / BITS_PER_UNIT));
4778 else
4779 value = size_int (TYPE_ALIGN_UNIT (type));
4782 /* VALUE will have the middle-end integer type sizetype.
4783 However, we should really return a value of type `size_t',
4784 which is just a typedef for an ordinary integer type. */
4785 value = fold_convert_loc (loc, size_type_node, value);
4787 return value;
4790 /* Implement the __alignof keyword: Return the minimum required
4791 alignment of EXPR, measured in bytes. For VAR_DECLs,
4792 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4793 from an "aligned" __attribute__ specification). LOC is the
4794 location of the ALIGNOF operator. */
4796 tree
4797 c_alignof_expr (location_t loc, tree expr)
4799 tree t;
4801 if (VAR_OR_FUNCTION_DECL_P (expr))
4802 t = size_int (DECL_ALIGN_UNIT (expr));
4804 else if (TREE_CODE (expr) == COMPONENT_REF
4805 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4807 error_at (loc, "%<__alignof%> applied to a bit-field");
4808 t = size_one_node;
4810 else if (TREE_CODE (expr) == COMPONENT_REF
4811 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4812 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4814 else if (TREE_CODE (expr) == INDIRECT_REF)
4816 tree t = TREE_OPERAND (expr, 0);
4817 tree best = t;
4818 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4820 while (CONVERT_EXPR_P (t)
4821 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4823 int thisalign;
4825 t = TREE_OPERAND (t, 0);
4826 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4827 if (thisalign > bestalign)
4828 best = t, bestalign = thisalign;
4830 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4832 else
4833 return c_alignof (loc, TREE_TYPE (expr));
4835 return fold_convert_loc (loc, size_type_node, t);
4838 /* Handle C and C++ default attributes. */
4840 enum built_in_attribute
4842 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4843 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4844 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4845 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4846 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4847 #include "builtin-attrs.def"
4848 #undef DEF_ATTR_NULL_TREE
4849 #undef DEF_ATTR_INT
4850 #undef DEF_ATTR_STRING
4851 #undef DEF_ATTR_IDENT
4852 #undef DEF_ATTR_TREE_LIST
4853 ATTR_LAST
4856 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4858 static void c_init_attributes (void);
4860 enum c_builtin_type
4862 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4863 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4864 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4865 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4866 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4867 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4868 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4869 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4870 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4871 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4872 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4873 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4874 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4875 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4876 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4877 NAME,
4878 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4879 #include "builtin-types.def"
4880 #undef DEF_PRIMITIVE_TYPE
4881 #undef DEF_FUNCTION_TYPE_0
4882 #undef DEF_FUNCTION_TYPE_1
4883 #undef DEF_FUNCTION_TYPE_2
4884 #undef DEF_FUNCTION_TYPE_3
4885 #undef DEF_FUNCTION_TYPE_4
4886 #undef DEF_FUNCTION_TYPE_5
4887 #undef DEF_FUNCTION_TYPE_6
4888 #undef DEF_FUNCTION_TYPE_7
4889 #undef DEF_FUNCTION_TYPE_VAR_0
4890 #undef DEF_FUNCTION_TYPE_VAR_1
4891 #undef DEF_FUNCTION_TYPE_VAR_2
4892 #undef DEF_FUNCTION_TYPE_VAR_3
4893 #undef DEF_FUNCTION_TYPE_VAR_4
4894 #undef DEF_FUNCTION_TYPE_VAR_5
4895 #undef DEF_POINTER_TYPE
4896 BT_LAST
4899 typedef enum c_builtin_type builtin_type;
4901 /* A temporary array for c_common_nodes_and_builtins. Used in
4902 communication with def_fn_type. */
4903 static tree builtin_types[(int) BT_LAST + 1];
4905 /* A helper function for c_common_nodes_and_builtins. Build function type
4906 for DEF with return type RET and N arguments. If VAR is true, then the
4907 function should be variadic after those N arguments.
4909 Takes special care not to ICE if any of the types involved are
4910 error_mark_node, which indicates that said type is not in fact available
4911 (see builtin_type_for_size). In which case the function type as a whole
4912 should be error_mark_node. */
4914 static void
4915 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4917 tree t;
4918 tree *args = XALLOCAVEC (tree, n);
4919 va_list list;
4920 int i;
4922 va_start (list, n);
4923 for (i = 0; i < n; ++i)
4925 builtin_type a = (builtin_type) va_arg (list, int);
4926 t = builtin_types[a];
4927 if (t == error_mark_node)
4928 goto egress;
4929 args[i] = t;
4932 t = builtin_types[ret];
4933 if (t == error_mark_node)
4934 goto egress;
4935 if (var)
4936 t = build_varargs_function_type_array (t, n, args);
4937 else
4938 t = build_function_type_array (t, n, args);
4940 egress:
4941 builtin_types[def] = t;
4942 va_end (list);
4945 /* Build builtin functions common to both C and C++ language
4946 frontends. */
4948 static void
4949 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4951 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4952 builtin_types[ENUM] = VALUE;
4953 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4954 def_fn_type (ENUM, RETURN, 0, 0);
4955 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4956 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4957 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4958 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4959 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4960 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4961 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4962 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4963 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4964 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4965 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4966 ARG6) \
4967 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4968 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4969 ARG6, ARG7) \
4970 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4971 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4972 def_fn_type (ENUM, RETURN, 1, 0);
4973 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4974 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4975 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4976 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4977 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4978 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4979 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4980 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4981 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4982 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4983 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4984 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4986 #include "builtin-types.def"
4988 #undef DEF_PRIMITIVE_TYPE
4989 #undef DEF_FUNCTION_TYPE_1
4990 #undef DEF_FUNCTION_TYPE_2
4991 #undef DEF_FUNCTION_TYPE_3
4992 #undef DEF_FUNCTION_TYPE_4
4993 #undef DEF_FUNCTION_TYPE_5
4994 #undef DEF_FUNCTION_TYPE_6
4995 #undef DEF_FUNCTION_TYPE_VAR_0
4996 #undef DEF_FUNCTION_TYPE_VAR_1
4997 #undef DEF_FUNCTION_TYPE_VAR_2
4998 #undef DEF_FUNCTION_TYPE_VAR_3
4999 #undef DEF_FUNCTION_TYPE_VAR_4
5000 #undef DEF_FUNCTION_TYPE_VAR_5
5001 #undef DEF_POINTER_TYPE
5002 builtin_types[(int) BT_LAST] = NULL_TREE;
5004 c_init_attributes ();
5006 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5007 NONANSI_P, ATTRS, IMPLICIT, COND) \
5008 if (NAME && COND) \
5009 def_builtin_1 (ENUM, NAME, CLASS, \
5010 builtin_types[(int) TYPE], \
5011 builtin_types[(int) LIBTYPE], \
5012 BOTH_P, FALLBACK_P, NONANSI_P, \
5013 built_in_attributes[(int) ATTRS], IMPLICIT);
5014 #include "builtins.def"
5015 #undef DEF_BUILTIN
5017 targetm.init_builtins ();
5019 build_common_builtin_nodes ();
5021 if (flag_mudflap)
5022 mudflap_init ();
5025 /* Like get_identifier, but avoid warnings about null arguments when
5026 the argument may be NULL for targets where GCC lacks stdint.h type
5027 information. */
5029 static inline tree
5030 c_get_ident (const char *id)
5032 return get_identifier (id);
5035 /* Build tree nodes and builtin functions common to both C and C++ language
5036 frontends. */
5038 void
5039 c_common_nodes_and_builtins (void)
5041 int char16_type_size;
5042 int char32_type_size;
5043 int wchar_type_size;
5044 tree array_domain_type;
5045 tree va_list_ref_type_node;
5046 tree va_list_arg_type_node;
5048 build_common_tree_nodes (flag_signed_char, flag_short_double);
5050 /* Define `int' and `char' first so that dbx will output them first. */
5051 record_builtin_type (RID_INT, NULL, integer_type_node);
5052 record_builtin_type (RID_CHAR, "char", char_type_node);
5054 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5055 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5056 but not C. Are the conditionals here needed? */
5057 if (c_dialect_cxx ())
5058 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5059 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5060 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5061 record_builtin_type (RID_MAX, "long unsigned int",
5062 long_unsigned_type_node);
5063 if (int128_integer_type_node != NULL_TREE)
5065 record_builtin_type (RID_INT128, "__int128",
5066 int128_integer_type_node);
5067 record_builtin_type (RID_MAX, "__int128 unsigned",
5068 int128_unsigned_type_node);
5070 if (c_dialect_cxx ())
5071 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5072 record_builtin_type (RID_MAX, "long long int",
5073 long_long_integer_type_node);
5074 record_builtin_type (RID_MAX, "long long unsigned int",
5075 long_long_unsigned_type_node);
5076 if (c_dialect_cxx ())
5077 record_builtin_type (RID_MAX, "long long unsigned",
5078 long_long_unsigned_type_node);
5079 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5080 record_builtin_type (RID_MAX, "short unsigned int",
5081 short_unsigned_type_node);
5082 if (c_dialect_cxx ())
5083 record_builtin_type (RID_MAX, "unsigned short",
5084 short_unsigned_type_node);
5086 /* Define both `signed char' and `unsigned char'. */
5087 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5088 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5090 /* These are types that c_common_type_for_size and
5091 c_common_type_for_mode use. */
5092 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5093 TYPE_DECL, NULL_TREE,
5094 intQI_type_node));
5095 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5096 TYPE_DECL, NULL_TREE,
5097 intHI_type_node));
5098 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5099 TYPE_DECL, NULL_TREE,
5100 intSI_type_node));
5101 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5102 TYPE_DECL, NULL_TREE,
5103 intDI_type_node));
5104 #if HOST_BITS_PER_WIDE_INT >= 64
5105 if (targetm.scalar_mode_supported_p (TImode))
5106 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5107 TYPE_DECL,
5108 get_identifier ("__int128_t"),
5109 intTI_type_node));
5110 #endif
5111 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5112 TYPE_DECL, NULL_TREE,
5113 unsigned_intQI_type_node));
5114 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5115 TYPE_DECL, NULL_TREE,
5116 unsigned_intHI_type_node));
5117 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5118 TYPE_DECL, NULL_TREE,
5119 unsigned_intSI_type_node));
5120 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5121 TYPE_DECL, NULL_TREE,
5122 unsigned_intDI_type_node));
5123 #if HOST_BITS_PER_WIDE_INT >= 64
5124 if (targetm.scalar_mode_supported_p (TImode))
5125 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5126 TYPE_DECL,
5127 get_identifier ("__uint128_t"),
5128 unsigned_intTI_type_node));
5129 #endif
5131 /* Create the widest literal types. */
5132 widest_integer_literal_type_node
5133 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5134 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5135 TYPE_DECL, NULL_TREE,
5136 widest_integer_literal_type_node));
5138 widest_unsigned_literal_type_node
5139 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5140 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5141 TYPE_DECL, NULL_TREE,
5142 widest_unsigned_literal_type_node));
5144 signed_size_type_node = c_common_signed_type (size_type_node);
5146 pid_type_node =
5147 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5149 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5150 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5151 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5153 /* Only supported decimal floating point extension if the target
5154 actually supports underlying modes. */
5155 if (targetm.scalar_mode_supported_p (SDmode)
5156 && targetm.scalar_mode_supported_p (DDmode)
5157 && targetm.scalar_mode_supported_p (TDmode))
5159 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5160 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5161 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5164 if (targetm.fixed_point_supported_p ())
5166 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5167 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5168 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5169 record_builtin_type (RID_MAX, "long long _Fract",
5170 long_long_fract_type_node);
5171 record_builtin_type (RID_MAX, "unsigned short _Fract",
5172 unsigned_short_fract_type_node);
5173 record_builtin_type (RID_MAX, "unsigned _Fract",
5174 unsigned_fract_type_node);
5175 record_builtin_type (RID_MAX, "unsigned long _Fract",
5176 unsigned_long_fract_type_node);
5177 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5178 unsigned_long_long_fract_type_node);
5179 record_builtin_type (RID_MAX, "_Sat short _Fract",
5180 sat_short_fract_type_node);
5181 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5182 record_builtin_type (RID_MAX, "_Sat long _Fract",
5183 sat_long_fract_type_node);
5184 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5185 sat_long_long_fract_type_node);
5186 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5187 sat_unsigned_short_fract_type_node);
5188 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5189 sat_unsigned_fract_type_node);
5190 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5191 sat_unsigned_long_fract_type_node);
5192 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5193 sat_unsigned_long_long_fract_type_node);
5194 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5195 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5196 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5197 record_builtin_type (RID_MAX, "long long _Accum",
5198 long_long_accum_type_node);
5199 record_builtin_type (RID_MAX, "unsigned short _Accum",
5200 unsigned_short_accum_type_node);
5201 record_builtin_type (RID_MAX, "unsigned _Accum",
5202 unsigned_accum_type_node);
5203 record_builtin_type (RID_MAX, "unsigned long _Accum",
5204 unsigned_long_accum_type_node);
5205 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5206 unsigned_long_long_accum_type_node);
5207 record_builtin_type (RID_MAX, "_Sat short _Accum",
5208 sat_short_accum_type_node);
5209 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5210 record_builtin_type (RID_MAX, "_Sat long _Accum",
5211 sat_long_accum_type_node);
5212 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5213 sat_long_long_accum_type_node);
5214 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5215 sat_unsigned_short_accum_type_node);
5216 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5217 sat_unsigned_accum_type_node);
5218 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5219 sat_unsigned_long_accum_type_node);
5220 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5221 sat_unsigned_long_long_accum_type_node);
5225 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5226 TYPE_DECL,
5227 get_identifier ("complex int"),
5228 complex_integer_type_node));
5229 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5230 TYPE_DECL,
5231 get_identifier ("complex float"),
5232 complex_float_type_node));
5233 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5234 TYPE_DECL,
5235 get_identifier ("complex double"),
5236 complex_double_type_node));
5237 lang_hooks.decls.pushdecl
5238 (build_decl (UNKNOWN_LOCATION,
5239 TYPE_DECL, get_identifier ("complex long double"),
5240 complex_long_double_type_node));
5242 if (c_dialect_cxx ())
5243 /* For C++, make fileptr_type_node a distinct void * type until
5244 FILE type is defined. */
5245 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5247 record_builtin_type (RID_VOID, NULL, void_type_node);
5249 /* Set the TYPE_NAME for any variants that were built before
5250 record_builtin_type gave names to the built-in types. */
5252 tree void_name = TYPE_NAME (void_type_node);
5253 TYPE_NAME (void_type_node) = NULL_TREE;
5254 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5255 = void_name;
5256 TYPE_NAME (void_type_node) = void_name;
5259 /* This node must not be shared. */
5260 void_zero_node = make_node (INTEGER_CST);
5261 TREE_TYPE (void_zero_node) = void_type_node;
5263 void_list_node = build_void_list_node ();
5265 /* Make a type to be the domain of a few array types
5266 whose domains don't really matter.
5267 200 is small enough that it always fits in size_t
5268 and large enough that it can hold most function names for the
5269 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5270 array_domain_type = build_index_type (size_int (200));
5272 /* Make a type for arrays of characters.
5273 With luck nothing will ever really depend on the length of this
5274 array type. */
5275 char_array_type_node
5276 = build_array_type (char_type_node, array_domain_type);
5278 /* Likewise for arrays of ints. */
5279 int_array_type_node
5280 = build_array_type (integer_type_node, array_domain_type);
5282 string_type_node = build_pointer_type (char_type_node);
5283 const_string_type_node
5284 = build_pointer_type (build_qualified_type
5285 (char_type_node, TYPE_QUAL_CONST));
5287 /* This is special for C++ so functions can be overloaded. */
5288 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5289 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5290 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5291 underlying_wchar_type_node = wchar_type_node;
5292 if (c_dialect_cxx ())
5294 if (TYPE_UNSIGNED (wchar_type_node))
5295 wchar_type_node = make_unsigned_type (wchar_type_size);
5296 else
5297 wchar_type_node = make_signed_type (wchar_type_size);
5298 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5301 /* This is for wide string constants. */
5302 wchar_array_type_node
5303 = build_array_type (wchar_type_node, array_domain_type);
5305 /* Define 'char16_t'. */
5306 char16_type_node = get_identifier (CHAR16_TYPE);
5307 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5308 char16_type_size = TYPE_PRECISION (char16_type_node);
5309 if (c_dialect_cxx ())
5311 char16_type_node = make_unsigned_type (char16_type_size);
5313 if (cxx_dialect >= cxx0x)
5314 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5317 /* This is for UTF-16 string constants. */
5318 char16_array_type_node
5319 = build_array_type (char16_type_node, array_domain_type);
5321 /* Define 'char32_t'. */
5322 char32_type_node = get_identifier (CHAR32_TYPE);
5323 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5324 char32_type_size = TYPE_PRECISION (char32_type_node);
5325 if (c_dialect_cxx ())
5327 char32_type_node = make_unsigned_type (char32_type_size);
5329 if (cxx_dialect >= cxx0x)
5330 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5333 /* This is for UTF-32 string constants. */
5334 char32_array_type_node
5335 = build_array_type (char32_type_node, array_domain_type);
5337 wint_type_node =
5338 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5340 intmax_type_node =
5341 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5342 uintmax_type_node =
5343 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5345 if (SIG_ATOMIC_TYPE)
5346 sig_atomic_type_node =
5347 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5348 if (INT8_TYPE)
5349 int8_type_node =
5350 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5351 if (INT16_TYPE)
5352 int16_type_node =
5353 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5354 if (INT32_TYPE)
5355 int32_type_node =
5356 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5357 if (INT64_TYPE)
5358 int64_type_node =
5359 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5360 if (UINT8_TYPE)
5361 uint8_type_node =
5362 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5363 if (UINT16_TYPE)
5364 c_uint16_type_node =
5365 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5366 if (UINT32_TYPE)
5367 c_uint32_type_node =
5368 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5369 if (UINT64_TYPE)
5370 c_uint64_type_node =
5371 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5372 if (INT_LEAST8_TYPE)
5373 int_least8_type_node =
5374 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5375 if (INT_LEAST16_TYPE)
5376 int_least16_type_node =
5377 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5378 if (INT_LEAST32_TYPE)
5379 int_least32_type_node =
5380 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5381 if (INT_LEAST64_TYPE)
5382 int_least64_type_node =
5383 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5384 if (UINT_LEAST8_TYPE)
5385 uint_least8_type_node =
5386 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5387 if (UINT_LEAST16_TYPE)
5388 uint_least16_type_node =
5389 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5390 if (UINT_LEAST32_TYPE)
5391 uint_least32_type_node =
5392 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5393 if (UINT_LEAST64_TYPE)
5394 uint_least64_type_node =
5395 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5396 if (INT_FAST8_TYPE)
5397 int_fast8_type_node =
5398 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5399 if (INT_FAST16_TYPE)
5400 int_fast16_type_node =
5401 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5402 if (INT_FAST32_TYPE)
5403 int_fast32_type_node =
5404 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5405 if (INT_FAST64_TYPE)
5406 int_fast64_type_node =
5407 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5408 if (UINT_FAST8_TYPE)
5409 uint_fast8_type_node =
5410 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5411 if (UINT_FAST16_TYPE)
5412 uint_fast16_type_node =
5413 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5414 if (UINT_FAST32_TYPE)
5415 uint_fast32_type_node =
5416 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5417 if (UINT_FAST64_TYPE)
5418 uint_fast64_type_node =
5419 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5420 if (INTPTR_TYPE)
5421 intptr_type_node =
5422 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5423 if (UINTPTR_TYPE)
5424 uintptr_type_node =
5425 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5427 default_function_type
5428 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5429 ptrdiff_type_node
5430 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5431 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5433 lang_hooks.decls.pushdecl
5434 (build_decl (UNKNOWN_LOCATION,
5435 TYPE_DECL, get_identifier ("__builtin_va_list"),
5436 va_list_type_node));
5437 if (targetm.enum_va_list_p)
5439 int l;
5440 const char *pname;
5441 tree ptype;
5443 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5445 lang_hooks.decls.pushdecl
5446 (build_decl (UNKNOWN_LOCATION,
5447 TYPE_DECL, get_identifier (pname),
5448 ptype));
5453 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5455 va_list_arg_type_node = va_list_ref_type_node =
5456 build_pointer_type (TREE_TYPE (va_list_type_node));
5458 else
5460 va_list_arg_type_node = va_list_type_node;
5461 va_list_ref_type_node = build_reference_type (va_list_type_node);
5464 if (!flag_preprocess_only)
5465 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5467 main_identifier_node = get_identifier ("main");
5469 /* Create the built-in __null node. It is important that this is
5470 not shared. */
5471 null_node = make_node (INTEGER_CST);
5472 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5474 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5475 memset (builtin_types, 0, sizeof (builtin_types));
5478 /* The number of named compound-literals generated thus far. */
5479 static GTY(()) int compound_literal_number;
5481 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5483 void
5484 set_compound_literal_name (tree decl)
5486 char *name;
5487 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5488 compound_literal_number);
5489 compound_literal_number++;
5490 DECL_NAME (decl) = get_identifier (name);
5493 tree
5494 build_va_arg (location_t loc, tree expr, tree type)
5496 expr = build1 (VA_ARG_EXPR, type, expr);
5497 SET_EXPR_LOCATION (expr, loc);
5498 return expr;
5502 /* Linked list of disabled built-in functions. */
5504 typedef struct disabled_builtin
5506 const char *name;
5507 struct disabled_builtin *next;
5508 } disabled_builtin;
5509 static disabled_builtin *disabled_builtins = NULL;
5511 static bool builtin_function_disabled_p (const char *);
5513 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5514 begins with "__builtin_", give an error. */
5516 void
5517 disable_builtin_function (const char *name)
5519 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5520 error ("cannot disable built-in function %qs", name);
5521 else
5523 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5524 new_disabled_builtin->name = name;
5525 new_disabled_builtin->next = disabled_builtins;
5526 disabled_builtins = new_disabled_builtin;
5531 /* Return true if the built-in function NAME has been disabled, false
5532 otherwise. */
5534 static bool
5535 builtin_function_disabled_p (const char *name)
5537 disabled_builtin *p;
5538 for (p = disabled_builtins; p != NULL; p = p->next)
5540 if (strcmp (name, p->name) == 0)
5541 return true;
5543 return false;
5547 /* Worker for DEF_BUILTIN.
5548 Possibly define a builtin function with one or two names.
5549 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5550 nonansi_p and flag_no_nonansi_builtin. */
5552 static void
5553 def_builtin_1 (enum built_in_function fncode,
5554 const char *name,
5555 enum built_in_class fnclass,
5556 tree fntype, tree libtype,
5557 bool both_p, bool fallback_p, bool nonansi_p,
5558 tree fnattrs, bool implicit_p)
5560 tree decl;
5561 const char *libname;
5563 if (fntype == error_mark_node)
5564 return;
5566 gcc_assert ((!both_p && !fallback_p)
5567 || !strncmp (name, "__builtin_",
5568 strlen ("__builtin_")));
5570 libname = name + strlen ("__builtin_");
5571 decl = add_builtin_function (name, fntype, fncode, fnclass,
5572 (fallback_p ? libname : NULL),
5573 fnattrs);
5575 set_builtin_decl (fncode, decl, implicit_p);
5577 if (both_p
5578 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5579 && !(nonansi_p && flag_no_nonansi_builtin))
5580 add_builtin_function (libname, libtype, fncode, fnclass,
5581 NULL, fnattrs);
5584 /* Nonzero if the type T promotes to int. This is (nearly) the
5585 integral promotions defined in ISO C99 6.3.1.1/2. */
5587 bool
5588 c_promoting_integer_type_p (const_tree t)
5590 switch (TREE_CODE (t))
5592 case INTEGER_TYPE:
5593 return (TYPE_MAIN_VARIANT (t) == char_type_node
5594 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5595 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5596 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5597 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5598 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5600 case ENUMERAL_TYPE:
5601 /* ??? Technically all enumerations not larger than an int
5602 promote to an int. But this is used along code paths
5603 that only want to notice a size change. */
5604 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5606 case BOOLEAN_TYPE:
5607 return 1;
5609 default:
5610 return 0;
5614 /* Return 1 if PARMS specifies a fixed number of parameters
5615 and none of their types is affected by default promotions. */
5618 self_promoting_args_p (const_tree parms)
5620 const_tree t;
5621 for (t = parms; t; t = TREE_CHAIN (t))
5623 tree type = TREE_VALUE (t);
5625 if (type == error_mark_node)
5626 continue;
5628 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5629 return 0;
5631 if (type == 0)
5632 return 0;
5634 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5635 return 0;
5637 if (c_promoting_integer_type_p (type))
5638 return 0;
5640 return 1;
5643 /* Recursively remove any '*' or '&' operator from TYPE. */
5644 tree
5645 strip_pointer_operator (tree t)
5647 while (POINTER_TYPE_P (t))
5648 t = TREE_TYPE (t);
5649 return t;
5652 /* Recursively remove pointer or array type from TYPE. */
5653 tree
5654 strip_pointer_or_array_types (tree t)
5656 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5657 t = TREE_TYPE (t);
5658 return t;
5661 /* Used to compare case labels. K1 and K2 are actually tree nodes
5662 representing case labels, or NULL_TREE for a `default' label.
5663 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5664 K2, and 0 if K1 and K2 are equal. */
5667 case_compare (splay_tree_key k1, splay_tree_key k2)
5669 /* Consider a NULL key (such as arises with a `default' label) to be
5670 smaller than anything else. */
5671 if (!k1)
5672 return k2 ? -1 : 0;
5673 else if (!k2)
5674 return k1 ? 1 : 0;
5676 return tree_int_cst_compare ((tree) k1, (tree) k2);
5679 /* Process a case label, located at LOC, for the range LOW_VALUE
5680 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5681 then this case label is actually a `default' label. If only
5682 HIGH_VALUE is NULL_TREE, then case label was declared using the
5683 usual C/C++ syntax, rather than the GNU case range extension.
5684 CASES is a tree containing all the case ranges processed so far;
5685 COND is the condition for the switch-statement itself. Returns the
5686 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5687 is created. */
5689 tree
5690 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5691 tree low_value, tree high_value)
5693 tree type;
5694 tree label;
5695 tree case_label;
5696 splay_tree_node node;
5698 /* Create the LABEL_DECL itself. */
5699 label = create_artificial_label (loc);
5701 /* If there was an error processing the switch condition, bail now
5702 before we get more confused. */
5703 if (!cond || cond == error_mark_node)
5704 goto error_out;
5706 if ((low_value && TREE_TYPE (low_value)
5707 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5708 || (high_value && TREE_TYPE (high_value)
5709 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5711 error_at (loc, "pointers are not permitted as case values");
5712 goto error_out;
5715 /* Case ranges are a GNU extension. */
5716 if (high_value)
5717 pedwarn (loc, OPT_Wpedantic,
5718 "range expressions in switch statements are non-standard");
5720 type = TREE_TYPE (cond);
5721 if (low_value)
5723 low_value = check_case_value (low_value);
5724 low_value = convert_and_check (type, low_value);
5725 if (low_value == error_mark_node)
5726 goto error_out;
5728 if (high_value)
5730 high_value = check_case_value (high_value);
5731 high_value = convert_and_check (type, high_value);
5732 if (high_value == error_mark_node)
5733 goto error_out;
5736 if (low_value && high_value)
5738 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5739 really a case range, even though it was written that way.
5740 Remove the HIGH_VALUE to simplify later processing. */
5741 if (tree_int_cst_equal (low_value, high_value))
5742 high_value = NULL_TREE;
5743 else if (!tree_int_cst_lt (low_value, high_value))
5744 warning_at (loc, 0, "empty range specified");
5747 /* See if the case is in range of the type of the original testing
5748 expression. If both low_value and high_value are out of range,
5749 don't insert the case label and return NULL_TREE. */
5750 if (low_value
5751 && !check_case_bounds (type, orig_type,
5752 &low_value, high_value ? &high_value : NULL))
5753 return NULL_TREE;
5755 /* Look up the LOW_VALUE in the table of case labels we already
5756 have. */
5757 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5758 /* If there was not an exact match, check for overlapping ranges.
5759 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5760 that's a `default' label and the only overlap is an exact match. */
5761 if (!node && (low_value || high_value))
5763 splay_tree_node low_bound;
5764 splay_tree_node high_bound;
5766 /* Even though there wasn't an exact match, there might be an
5767 overlap between this case range and another case range.
5768 Since we've (inductively) not allowed any overlapping case
5769 ranges, we simply need to find the greatest low case label
5770 that is smaller that LOW_VALUE, and the smallest low case
5771 label that is greater than LOW_VALUE. If there is an overlap
5772 it will occur in one of these two ranges. */
5773 low_bound = splay_tree_predecessor (cases,
5774 (splay_tree_key) low_value);
5775 high_bound = splay_tree_successor (cases,
5776 (splay_tree_key) low_value);
5778 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5779 the LOW_VALUE, so there is no need to check unless the
5780 LOW_BOUND is in fact itself a case range. */
5781 if (low_bound
5782 && CASE_HIGH ((tree) low_bound->value)
5783 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5784 low_value) >= 0)
5785 node = low_bound;
5786 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5787 range is bigger than the low end of the current range, so we
5788 are only interested if the current range is a real range, and
5789 not an ordinary case label. */
5790 else if (high_bound
5791 && high_value
5792 && (tree_int_cst_compare ((tree) high_bound->key,
5793 high_value)
5794 <= 0))
5795 node = high_bound;
5797 /* If there was an overlap, issue an error. */
5798 if (node)
5800 tree duplicate = CASE_LABEL ((tree) node->value);
5802 if (high_value)
5804 error_at (loc, "duplicate (or overlapping) case value");
5805 error_at (DECL_SOURCE_LOCATION (duplicate),
5806 "this is the first entry overlapping that value");
5808 else if (low_value)
5810 error_at (loc, "duplicate case value") ;
5811 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5813 else
5815 error_at (loc, "multiple default labels in one switch");
5816 error_at (DECL_SOURCE_LOCATION (duplicate),
5817 "this is the first default label");
5819 goto error_out;
5822 /* Add a CASE_LABEL to the statement-tree. */
5823 case_label = add_stmt (build_case_label (low_value, high_value, label));
5824 /* Register this case label in the splay tree. */
5825 splay_tree_insert (cases,
5826 (splay_tree_key) low_value,
5827 (splay_tree_value) case_label);
5829 return case_label;
5831 error_out:
5832 /* Add a label so that the back-end doesn't think that the beginning of
5833 the switch is unreachable. Note that we do not add a case label, as
5834 that just leads to duplicates and thence to failure later on. */
5835 if (!cases->root)
5837 tree t = create_artificial_label (loc);
5838 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5840 return error_mark_node;
5843 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5844 Used to verify that case values match up with enumerator values. */
5846 static void
5847 match_case_to_enum_1 (tree key, tree type, tree label)
5849 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5851 /* ??? Not working too hard to print the double-word value.
5852 Should perhaps be done with %lwd in the diagnostic routines? */
5853 if (TREE_INT_CST_HIGH (key) == 0)
5854 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5855 TREE_INT_CST_LOW (key));
5856 else if (!TYPE_UNSIGNED (type)
5857 && TREE_INT_CST_HIGH (key) == -1
5858 && TREE_INT_CST_LOW (key) != 0)
5859 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5860 -TREE_INT_CST_LOW (key));
5861 else
5862 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5863 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5864 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5866 if (TYPE_NAME (type) == 0)
5867 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5868 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5869 "case value %qs not in enumerated type",
5870 buf);
5871 else
5872 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5873 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5874 "case value %qs not in enumerated type %qT",
5875 buf, type);
5878 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5879 Used to verify that case values match up with enumerator values. */
5881 static int
5882 match_case_to_enum (splay_tree_node node, void *data)
5884 tree label = (tree) node->value;
5885 tree type = (tree) data;
5887 /* Skip default case. */
5888 if (!CASE_LOW (label))
5889 return 0;
5891 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5892 when we did our enum->case scan. Reset our scratch bit after. */
5893 if (!CASE_LOW_SEEN (label))
5894 match_case_to_enum_1 (CASE_LOW (label), type, label);
5895 else
5896 CASE_LOW_SEEN (label) = 0;
5898 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5899 not set, that means that CASE_HIGH did not appear when we did our
5900 enum->case scan. Reset our scratch bit after. */
5901 if (CASE_HIGH (label))
5903 if (!CASE_HIGH_SEEN (label))
5904 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5905 else
5906 CASE_HIGH_SEEN (label) = 0;
5909 return 0;
5912 /* Handle -Wswitch*. Called from the front end after parsing the
5913 switch construct. */
5914 /* ??? Should probably be somewhere generic, since other languages
5915 besides C and C++ would want this. At the moment, however, C/C++
5916 are the only tree-ssa languages that support enumerations at all,
5917 so the point is moot. */
5919 void
5920 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5921 tree type, tree cond)
5923 splay_tree_node default_node;
5924 splay_tree_node node;
5925 tree chain;
5927 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5928 return;
5930 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5931 if (!default_node)
5932 warning_at (switch_location, OPT_Wswitch_default,
5933 "switch missing default case");
5935 /* From here on, we only care about about enumerated types. */
5936 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5937 return;
5939 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5940 if (!warn_switch_enum && !warn_switch)
5941 return;
5943 /* Check the cases. Warn about case values which are not members of
5944 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5945 there is no default case, check that exactly all enumeration
5946 literals are covered by the cases. */
5948 /* Clearing COND if it is not an integer constant simplifies
5949 the tests inside the loop below. */
5950 if (TREE_CODE (cond) != INTEGER_CST)
5951 cond = NULL_TREE;
5953 /* The time complexity here is O(N*lg(N)) worst case, but for the
5954 common case of monotonically increasing enumerators, it is
5955 O(N), since the nature of the splay tree will keep the next
5956 element adjacent to the root at all times. */
5958 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5960 tree value = TREE_VALUE (chain);
5961 if (TREE_CODE (value) == CONST_DECL)
5962 value = DECL_INITIAL (value);
5963 node = splay_tree_lookup (cases, (splay_tree_key) value);
5964 if (node)
5966 /* Mark the CASE_LOW part of the case entry as seen. */
5967 tree label = (tree) node->value;
5968 CASE_LOW_SEEN (label) = 1;
5969 continue;
5972 /* Even though there wasn't an exact match, there might be a
5973 case range which includes the enumerator's value. */
5974 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5975 if (node && CASE_HIGH ((tree) node->value))
5977 tree label = (tree) node->value;
5978 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5979 if (cmp >= 0)
5981 /* If we match the upper bound exactly, mark the CASE_HIGH
5982 part of the case entry as seen. */
5983 if (cmp == 0)
5984 CASE_HIGH_SEEN (label) = 1;
5985 continue;
5989 /* We've now determined that this enumerated literal isn't
5990 handled by the case labels of the switch statement. */
5992 /* If the switch expression is a constant, we only really care
5993 about whether that constant is handled by the switch. */
5994 if (cond && tree_int_cst_compare (cond, value))
5995 continue;
5997 /* If there is a default_node, the only relevant option is
5998 Wswitch-enum. Otherwise, if both are enabled then we prefer
5999 to warn using -Wswitch because -Wswitch is enabled by -Wall
6000 while -Wswitch-enum is explicit. */
6001 warning_at (switch_location,
6002 (default_node || !warn_switch
6003 ? OPT_Wswitch_enum
6004 : OPT_Wswitch),
6005 "enumeration value %qE not handled in switch",
6006 TREE_PURPOSE (chain));
6009 /* Warn if there are case expressions that don't correspond to
6010 enumerators. This can occur since C and C++ don't enforce
6011 type-checking of assignments to enumeration variables.
6013 The time complexity here is now always O(N) worst case, since
6014 we should have marked both the lower bound and upper bound of
6015 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6016 above. This scan also resets those fields. */
6018 splay_tree_foreach (cases, match_case_to_enum, type);
6021 /* Finish an expression taking the address of LABEL (an
6022 IDENTIFIER_NODE). Returns an expression for the address.
6024 LOC is the location for the expression returned. */
6026 tree
6027 finish_label_address_expr (tree label, location_t loc)
6029 tree result;
6031 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6033 if (label == error_mark_node)
6034 return error_mark_node;
6036 label = lookup_label (label);
6037 if (label == NULL_TREE)
6038 result = null_pointer_node;
6039 else
6041 TREE_USED (label) = 1;
6042 result = build1 (ADDR_EXPR, ptr_type_node, label);
6043 /* The current function is not necessarily uninlinable.
6044 Computed gotos are incompatible with inlining, but the value
6045 here could be used only in a diagnostic, for example. */
6046 protected_set_expr_location (result, loc);
6049 return result;
6053 /* Given a boolean expression ARG, return a tree representing an increment
6054 or decrement (as indicated by CODE) of ARG. The front end must check for
6055 invalid cases (e.g., decrement in C++). */
6056 tree
6057 boolean_increment (enum tree_code code, tree arg)
6059 tree val;
6060 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6062 arg = stabilize_reference (arg);
6063 switch (code)
6065 case PREINCREMENT_EXPR:
6066 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6067 break;
6068 case POSTINCREMENT_EXPR:
6069 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6070 arg = save_expr (arg);
6071 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6072 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6073 break;
6074 case PREDECREMENT_EXPR:
6075 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6076 invert_truthvalue_loc (input_location, arg));
6077 break;
6078 case POSTDECREMENT_EXPR:
6079 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6080 invert_truthvalue_loc (input_location, arg));
6081 arg = save_expr (arg);
6082 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6083 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6084 break;
6085 default:
6086 gcc_unreachable ();
6088 TREE_SIDE_EFFECTS (val) = 1;
6089 return val;
6092 /* Built-in macros for stddef.h and stdint.h, that require macros
6093 defined in this file. */
6094 void
6095 c_stddef_cpp_builtins(void)
6097 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6098 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6099 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6100 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6101 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6102 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6103 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6104 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6105 if (SIG_ATOMIC_TYPE)
6106 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6107 if (INT8_TYPE)
6108 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6109 if (INT16_TYPE)
6110 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6111 if (INT32_TYPE)
6112 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6113 if (INT64_TYPE)
6114 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6115 if (UINT8_TYPE)
6116 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6117 if (UINT16_TYPE)
6118 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6119 if (UINT32_TYPE)
6120 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6121 if (UINT64_TYPE)
6122 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6123 if (INT_LEAST8_TYPE)
6124 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6125 if (INT_LEAST16_TYPE)
6126 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6127 if (INT_LEAST32_TYPE)
6128 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6129 if (INT_LEAST64_TYPE)
6130 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6131 if (UINT_LEAST8_TYPE)
6132 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6133 if (UINT_LEAST16_TYPE)
6134 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6135 if (UINT_LEAST32_TYPE)
6136 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6137 if (UINT_LEAST64_TYPE)
6138 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6139 if (INT_FAST8_TYPE)
6140 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6141 if (INT_FAST16_TYPE)
6142 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6143 if (INT_FAST32_TYPE)
6144 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6145 if (INT_FAST64_TYPE)
6146 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6147 if (UINT_FAST8_TYPE)
6148 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6149 if (UINT_FAST16_TYPE)
6150 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6151 if (UINT_FAST32_TYPE)
6152 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6153 if (UINT_FAST64_TYPE)
6154 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6155 if (INTPTR_TYPE)
6156 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6157 if (UINTPTR_TYPE)
6158 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6161 static void
6162 c_init_attributes (void)
6164 /* Fill in the built_in_attributes array. */
6165 #define DEF_ATTR_NULL_TREE(ENUM) \
6166 built_in_attributes[(int) ENUM] = NULL_TREE;
6167 #define DEF_ATTR_INT(ENUM, VALUE) \
6168 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6169 #define DEF_ATTR_STRING(ENUM, VALUE) \
6170 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6171 #define DEF_ATTR_IDENT(ENUM, STRING) \
6172 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6173 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6174 built_in_attributes[(int) ENUM] \
6175 = tree_cons (built_in_attributes[(int) PURPOSE], \
6176 built_in_attributes[(int) VALUE], \
6177 built_in_attributes[(int) CHAIN]);
6178 #include "builtin-attrs.def"
6179 #undef DEF_ATTR_NULL_TREE
6180 #undef DEF_ATTR_INT
6181 #undef DEF_ATTR_IDENT
6182 #undef DEF_ATTR_TREE_LIST
6185 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6186 identifier as an argument, so the front end shouldn't look it up. */
6188 bool
6189 attribute_takes_identifier_p (const_tree attr_id)
6191 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6192 if (spec == NULL)
6193 /* Unknown attribute that we'll end up ignoring, return true so we
6194 don't complain about an identifier argument. */
6195 return true;
6196 else if (!strcmp ("mode", spec->name)
6197 || !strcmp ("format", spec->name)
6198 || !strcmp ("cleanup", spec->name))
6199 return true;
6200 else
6201 return targetm.attribute_takes_identifier_p (attr_id);
6204 /* Attribute handlers common to C front ends. */
6206 /* Handle a "packed" attribute; arguments as in
6207 struct attribute_spec.handler. */
6209 static tree
6210 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6211 int flags, bool *no_add_attrs)
6213 if (TYPE_P (*node))
6215 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6216 *node = build_variant_type_copy (*node);
6217 TYPE_PACKED (*node) = 1;
6219 else if (TREE_CODE (*node) == FIELD_DECL)
6221 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6222 /* Still pack bitfields. */
6223 && ! DECL_INITIAL (*node))
6224 warning (OPT_Wattributes,
6225 "%qE attribute ignored for field of type %qT",
6226 name, TREE_TYPE (*node));
6227 else
6228 DECL_PACKED (*node) = 1;
6230 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6231 used for DECL_REGISTER. It wouldn't mean anything anyway.
6232 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6233 that changes what the typedef is typing. */
6234 else
6236 warning (OPT_Wattributes, "%qE attribute ignored", name);
6237 *no_add_attrs = true;
6240 return NULL_TREE;
6243 /* Handle a "nocommon" attribute; arguments as in
6244 struct attribute_spec.handler. */
6246 static tree
6247 handle_nocommon_attribute (tree *node, tree name,
6248 tree ARG_UNUSED (args),
6249 int ARG_UNUSED (flags), bool *no_add_attrs)
6251 if (TREE_CODE (*node) == VAR_DECL)
6252 DECL_COMMON (*node) = 0;
6253 else
6255 warning (OPT_Wattributes, "%qE attribute ignored", name);
6256 *no_add_attrs = true;
6259 return NULL_TREE;
6262 /* Handle a "common" attribute; arguments as in
6263 struct attribute_spec.handler. */
6265 static tree
6266 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6267 int ARG_UNUSED (flags), bool *no_add_attrs)
6269 if (TREE_CODE (*node) == VAR_DECL)
6270 DECL_COMMON (*node) = 1;
6271 else
6273 warning (OPT_Wattributes, "%qE attribute ignored", name);
6274 *no_add_attrs = true;
6277 return NULL_TREE;
6280 /* Handle a "noreturn" attribute; arguments as in
6281 struct attribute_spec.handler. */
6283 static tree
6284 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6285 int ARG_UNUSED (flags), bool *no_add_attrs)
6287 tree type = TREE_TYPE (*node);
6289 /* See FIXME comment in c_common_attribute_table. */
6290 if (TREE_CODE (*node) == FUNCTION_DECL
6291 || objc_method_decl (TREE_CODE (*node)))
6292 TREE_THIS_VOLATILE (*node) = 1;
6293 else if (TREE_CODE (type) == POINTER_TYPE
6294 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6295 TREE_TYPE (*node)
6296 = build_pointer_type
6297 (build_type_variant (TREE_TYPE (type),
6298 TYPE_READONLY (TREE_TYPE (type)), 1));
6299 else
6301 warning (OPT_Wattributes, "%qE attribute ignored", name);
6302 *no_add_attrs = true;
6305 return NULL_TREE;
6308 /* Handle a "hot" and attribute; arguments as in
6309 struct attribute_spec.handler. */
6311 static tree
6312 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6313 int ARG_UNUSED (flags), bool *no_add_attrs)
6315 if (TREE_CODE (*node) == FUNCTION_DECL
6316 || TREE_CODE (*node) == LABEL_DECL)
6318 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6320 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6321 name, "cold");
6322 *no_add_attrs = true;
6324 /* Most of the rest of the hot processing is done later with
6325 lookup_attribute. */
6327 else
6329 warning (OPT_Wattributes, "%qE attribute ignored", name);
6330 *no_add_attrs = true;
6333 return NULL_TREE;
6336 /* Handle a "cold" and attribute; arguments as in
6337 struct attribute_spec.handler. */
6339 static tree
6340 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6341 int ARG_UNUSED (flags), bool *no_add_attrs)
6343 if (TREE_CODE (*node) == FUNCTION_DECL
6344 || TREE_CODE (*node) == LABEL_DECL)
6346 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6348 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6349 name, "hot");
6350 *no_add_attrs = true;
6352 /* Most of the rest of the cold processing is done later with
6353 lookup_attribute. */
6355 else
6357 warning (OPT_Wattributes, "%qE attribute ignored", name);
6358 *no_add_attrs = true;
6361 return NULL_TREE;
6364 /* Handle a "noinline" attribute; arguments as in
6365 struct attribute_spec.handler. */
6367 static tree
6368 handle_noinline_attribute (tree *node, tree name,
6369 tree ARG_UNUSED (args),
6370 int ARG_UNUSED (flags), bool *no_add_attrs)
6372 if (TREE_CODE (*node) == FUNCTION_DECL)
6373 DECL_UNINLINABLE (*node) = 1;
6374 else
6376 warning (OPT_Wattributes, "%qE attribute ignored", name);
6377 *no_add_attrs = true;
6380 return NULL_TREE;
6383 /* Handle a "noclone" attribute; arguments as in
6384 struct attribute_spec.handler. */
6386 static tree
6387 handle_noclone_attribute (tree *node, tree name,
6388 tree ARG_UNUSED (args),
6389 int ARG_UNUSED (flags), bool *no_add_attrs)
6391 if (TREE_CODE (*node) != FUNCTION_DECL)
6393 warning (OPT_Wattributes, "%qE attribute ignored", name);
6394 *no_add_attrs = true;
6397 return NULL_TREE;
6400 /* Handle a "always_inline" attribute; arguments as in
6401 struct attribute_spec.handler. */
6403 static tree
6404 handle_always_inline_attribute (tree *node, tree name,
6405 tree ARG_UNUSED (args),
6406 int ARG_UNUSED (flags),
6407 bool *no_add_attrs)
6409 if (TREE_CODE (*node) == FUNCTION_DECL)
6411 /* Set the attribute and mark it for disregarding inline
6412 limits. */
6413 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6415 else
6417 warning (OPT_Wattributes, "%qE attribute ignored", name);
6418 *no_add_attrs = true;
6421 return NULL_TREE;
6424 /* Handle a "gnu_inline" attribute; arguments as in
6425 struct attribute_spec.handler. */
6427 static tree
6428 handle_gnu_inline_attribute (tree *node, tree name,
6429 tree ARG_UNUSED (args),
6430 int ARG_UNUSED (flags),
6431 bool *no_add_attrs)
6433 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6435 /* Do nothing else, just set the attribute. We'll get at
6436 it later with lookup_attribute. */
6438 else
6440 warning (OPT_Wattributes, "%qE attribute ignored", name);
6441 *no_add_attrs = true;
6444 return NULL_TREE;
6447 /* Handle a "leaf" attribute; arguments as in
6448 struct attribute_spec.handler. */
6450 static tree
6451 handle_leaf_attribute (tree *node, tree name,
6452 tree ARG_UNUSED (args),
6453 int ARG_UNUSED (flags), bool *no_add_attrs)
6455 if (TREE_CODE (*node) != FUNCTION_DECL)
6457 warning (OPT_Wattributes, "%qE attribute ignored", name);
6458 *no_add_attrs = true;
6460 if (!TREE_PUBLIC (*node))
6462 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6463 *no_add_attrs = true;
6466 return NULL_TREE;
6469 /* Handle an "artificial" attribute; arguments as in
6470 struct attribute_spec.handler. */
6472 static tree
6473 handle_artificial_attribute (tree *node, tree name,
6474 tree ARG_UNUSED (args),
6475 int ARG_UNUSED (flags),
6476 bool *no_add_attrs)
6478 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6480 /* Do nothing else, just set the attribute. We'll get at
6481 it later with lookup_attribute. */
6483 else
6485 warning (OPT_Wattributes, "%qE attribute ignored", name);
6486 *no_add_attrs = true;
6489 return NULL_TREE;
6492 /* Handle a "flatten" attribute; arguments as in
6493 struct attribute_spec.handler. */
6495 static tree
6496 handle_flatten_attribute (tree *node, tree name,
6497 tree args ATTRIBUTE_UNUSED,
6498 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6500 if (TREE_CODE (*node) == FUNCTION_DECL)
6501 /* Do nothing else, just set the attribute. We'll get at
6502 it later with lookup_attribute. */
6504 else
6506 warning (OPT_Wattributes, "%qE attribute ignored", name);
6507 *no_add_attrs = true;
6510 return NULL_TREE;
6513 /* Handle a "warning" or "error" attribute; arguments as in
6514 struct attribute_spec.handler. */
6516 static tree
6517 handle_error_attribute (tree *node, tree name, tree args,
6518 int ARG_UNUSED (flags), bool *no_add_attrs)
6520 if (TREE_CODE (*node) == FUNCTION_DECL
6521 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6522 /* Do nothing else, just set the attribute. We'll get at
6523 it later with lookup_attribute. */
6525 else
6527 warning (OPT_Wattributes, "%qE attribute ignored", name);
6528 *no_add_attrs = true;
6531 return NULL_TREE;
6534 /* Handle a "used" attribute; arguments as in
6535 struct attribute_spec.handler. */
6537 static tree
6538 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6539 int ARG_UNUSED (flags), bool *no_add_attrs)
6541 tree node = *pnode;
6543 if (TREE_CODE (node) == FUNCTION_DECL
6544 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6545 || (TREE_CODE (node) == TYPE_DECL))
6547 TREE_USED (node) = 1;
6548 DECL_PRESERVE_P (node) = 1;
6549 if (TREE_CODE (node) == VAR_DECL)
6550 DECL_READ_P (node) = 1;
6552 else
6554 warning (OPT_Wattributes, "%qE attribute ignored", name);
6555 *no_add_attrs = true;
6558 return NULL_TREE;
6561 /* Handle a "unused" attribute; arguments as in
6562 struct attribute_spec.handler. */
6564 static tree
6565 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6566 int flags, bool *no_add_attrs)
6568 if (DECL_P (*node))
6570 tree decl = *node;
6572 if (TREE_CODE (decl) == PARM_DECL
6573 || TREE_CODE (decl) == VAR_DECL
6574 || TREE_CODE (decl) == FUNCTION_DECL
6575 || TREE_CODE (decl) == LABEL_DECL
6576 || TREE_CODE (decl) == TYPE_DECL)
6578 TREE_USED (decl) = 1;
6579 if (TREE_CODE (decl) == VAR_DECL
6580 || TREE_CODE (decl) == PARM_DECL)
6581 DECL_READ_P (decl) = 1;
6583 else
6585 warning (OPT_Wattributes, "%qE attribute ignored", name);
6586 *no_add_attrs = true;
6589 else
6591 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6592 *node = build_variant_type_copy (*node);
6593 TREE_USED (*node) = 1;
6596 return NULL_TREE;
6599 /* Handle a "externally_visible" attribute; arguments as in
6600 struct attribute_spec.handler. */
6602 static tree
6603 handle_externally_visible_attribute (tree *pnode, tree name,
6604 tree ARG_UNUSED (args),
6605 int ARG_UNUSED (flags),
6606 bool *no_add_attrs)
6608 tree node = *pnode;
6610 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6612 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6613 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6615 warning (OPT_Wattributes,
6616 "%qE attribute have effect only on public objects", name);
6617 *no_add_attrs = true;
6620 else
6622 warning (OPT_Wattributes, "%qE attribute ignored", name);
6623 *no_add_attrs = true;
6626 return NULL_TREE;
6629 /* Handle a "const" attribute; arguments as in
6630 struct attribute_spec.handler. */
6632 static tree
6633 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6634 int ARG_UNUSED (flags), bool *no_add_attrs)
6636 tree type = TREE_TYPE (*node);
6638 /* See FIXME comment on noreturn in c_common_attribute_table. */
6639 if (TREE_CODE (*node) == FUNCTION_DECL)
6640 TREE_READONLY (*node) = 1;
6641 else if (TREE_CODE (type) == POINTER_TYPE
6642 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6643 TREE_TYPE (*node)
6644 = build_pointer_type
6645 (build_type_variant (TREE_TYPE (type), 1,
6646 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6647 else
6649 warning (OPT_Wattributes, "%qE attribute ignored", name);
6650 *no_add_attrs = true;
6653 return NULL_TREE;
6656 /* Handle a "transparent_union" attribute; arguments as in
6657 struct attribute_spec.handler. */
6659 static tree
6660 handle_transparent_union_attribute (tree *node, tree name,
6661 tree ARG_UNUSED (args), int flags,
6662 bool *no_add_attrs)
6664 tree type;
6666 *no_add_attrs = true;
6668 if (TREE_CODE (*node) == TYPE_DECL)
6669 node = &TREE_TYPE (*node);
6670 type = *node;
6672 if (TREE_CODE (type) == UNION_TYPE)
6674 /* Make sure that the first field will work for a transparent union.
6675 If the type isn't complete yet, leave the check to the code in
6676 finish_struct. */
6677 if (TYPE_SIZE (type))
6679 tree first = first_field (type);
6680 if (first == NULL_TREE
6681 || DECL_ARTIFICIAL (first)
6682 || TYPE_MODE (type) != DECL_MODE (first))
6683 goto ignored;
6686 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6688 /* If the type isn't complete yet, setting the flag
6689 on a variant wouldn't ever be checked. */
6690 if (!TYPE_SIZE (type))
6691 goto ignored;
6693 /* build_duplicate_type doesn't work for C++. */
6694 if (c_dialect_cxx ())
6695 goto ignored;
6697 /* A type variant isn't good enough, since we don't a cast
6698 to such a type removed as a no-op. */
6699 *node = type = build_duplicate_type (type);
6702 TYPE_TRANSPARENT_AGGR (type) = 1;
6703 return NULL_TREE;
6706 ignored:
6707 warning (OPT_Wattributes, "%qE attribute ignored", name);
6708 return NULL_TREE;
6711 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6712 get the requested priority for a constructor or destructor,
6713 possibly issuing diagnostics for invalid or reserved
6714 priorities. */
6716 static priority_type
6717 get_priority (tree args, bool is_destructor)
6719 HOST_WIDE_INT pri;
6720 tree arg;
6722 if (!args)
6723 return DEFAULT_INIT_PRIORITY;
6725 if (!SUPPORTS_INIT_PRIORITY)
6727 if (is_destructor)
6728 error ("destructor priorities are not supported");
6729 else
6730 error ("constructor priorities are not supported");
6731 return DEFAULT_INIT_PRIORITY;
6734 arg = TREE_VALUE (args);
6735 arg = default_conversion (arg);
6736 if (!host_integerp (arg, /*pos=*/0)
6737 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6738 goto invalid;
6740 pri = tree_low_cst (arg, /*pos=*/0);
6741 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6742 goto invalid;
6744 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6746 if (is_destructor)
6747 warning (0,
6748 "destructor priorities from 0 to %d are reserved "
6749 "for the implementation",
6750 MAX_RESERVED_INIT_PRIORITY);
6751 else
6752 warning (0,
6753 "constructor priorities from 0 to %d are reserved "
6754 "for the implementation",
6755 MAX_RESERVED_INIT_PRIORITY);
6757 return pri;
6759 invalid:
6760 if (is_destructor)
6761 error ("destructor priorities must be integers from 0 to %d inclusive",
6762 MAX_INIT_PRIORITY);
6763 else
6764 error ("constructor priorities must be integers from 0 to %d inclusive",
6765 MAX_INIT_PRIORITY);
6766 return DEFAULT_INIT_PRIORITY;
6769 /* Handle a "constructor" attribute; arguments as in
6770 struct attribute_spec.handler. */
6772 static tree
6773 handle_constructor_attribute (tree *node, tree name, tree args,
6774 int ARG_UNUSED (flags),
6775 bool *no_add_attrs)
6777 tree decl = *node;
6778 tree type = TREE_TYPE (decl);
6780 if (TREE_CODE (decl) == FUNCTION_DECL
6781 && TREE_CODE (type) == FUNCTION_TYPE
6782 && decl_function_context (decl) == 0)
6784 priority_type priority;
6785 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6786 priority = get_priority (args, /*is_destructor=*/false);
6787 SET_DECL_INIT_PRIORITY (decl, priority);
6788 TREE_USED (decl) = 1;
6790 else
6792 warning (OPT_Wattributes, "%qE attribute ignored", name);
6793 *no_add_attrs = true;
6796 return NULL_TREE;
6799 /* Handle a "destructor" attribute; arguments as in
6800 struct attribute_spec.handler. */
6802 static tree
6803 handle_destructor_attribute (tree *node, tree name, tree args,
6804 int ARG_UNUSED (flags),
6805 bool *no_add_attrs)
6807 tree decl = *node;
6808 tree type = TREE_TYPE (decl);
6810 if (TREE_CODE (decl) == FUNCTION_DECL
6811 && TREE_CODE (type) == FUNCTION_TYPE
6812 && decl_function_context (decl) == 0)
6814 priority_type priority;
6815 DECL_STATIC_DESTRUCTOR (decl) = 1;
6816 priority = get_priority (args, /*is_destructor=*/true);
6817 SET_DECL_FINI_PRIORITY (decl, priority);
6818 TREE_USED (decl) = 1;
6820 else
6822 warning (OPT_Wattributes, "%qE attribute ignored", name);
6823 *no_add_attrs = true;
6826 return NULL_TREE;
6829 /* Nonzero if the mode is a valid vector mode for this architecture.
6830 This returns nonzero even if there is no hardware support for the
6831 vector mode, but we can emulate with narrower modes. */
6833 static int
6834 vector_mode_valid_p (enum machine_mode mode)
6836 enum mode_class mclass = GET_MODE_CLASS (mode);
6837 enum machine_mode innermode;
6839 /* Doh! What's going on? */
6840 if (mclass != MODE_VECTOR_INT
6841 && mclass != MODE_VECTOR_FLOAT
6842 && mclass != MODE_VECTOR_FRACT
6843 && mclass != MODE_VECTOR_UFRACT
6844 && mclass != MODE_VECTOR_ACCUM
6845 && mclass != MODE_VECTOR_UACCUM)
6846 return 0;
6848 /* Hardware support. Woo hoo! */
6849 if (targetm.vector_mode_supported_p (mode))
6850 return 1;
6852 innermode = GET_MODE_INNER (mode);
6854 /* We should probably return 1 if requesting V4DI and we have no DI,
6855 but we have V2DI, but this is probably very unlikely. */
6857 /* If we have support for the inner mode, we can safely emulate it.
6858 We may not have V2DI, but me can emulate with a pair of DIs. */
6859 return targetm.scalar_mode_supported_p (innermode);
6863 /* Handle a "mode" attribute; arguments as in
6864 struct attribute_spec.handler. */
6866 static tree
6867 handle_mode_attribute (tree *node, tree name, tree args,
6868 int ARG_UNUSED (flags), bool *no_add_attrs)
6870 tree type = *node;
6871 tree ident = TREE_VALUE (args);
6873 *no_add_attrs = true;
6875 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6876 warning (OPT_Wattributes, "%qE attribute ignored", name);
6877 else
6879 int j;
6880 const char *p = IDENTIFIER_POINTER (ident);
6881 int len = strlen (p);
6882 enum machine_mode mode = VOIDmode;
6883 tree typefm;
6884 bool valid_mode;
6886 if (len > 4 && p[0] == '_' && p[1] == '_'
6887 && p[len - 1] == '_' && p[len - 2] == '_')
6889 char *newp = (char *) alloca (len - 1);
6891 strcpy (newp, &p[2]);
6892 newp[len - 4] = '\0';
6893 p = newp;
6896 /* Change this type to have a type with the specified mode.
6897 First check for the special modes. */
6898 if (!strcmp (p, "byte"))
6899 mode = byte_mode;
6900 else if (!strcmp (p, "word"))
6901 mode = word_mode;
6902 else if (!strcmp (p, "pointer"))
6903 mode = ptr_mode;
6904 else if (!strcmp (p, "libgcc_cmp_return"))
6905 mode = targetm.libgcc_cmp_return_mode ();
6906 else if (!strcmp (p, "libgcc_shift_count"))
6907 mode = targetm.libgcc_shift_count_mode ();
6908 else if (!strcmp (p, "unwind_word"))
6909 mode = targetm.unwind_word_mode ();
6910 else
6911 for (j = 0; j < NUM_MACHINE_MODES; j++)
6912 if (!strcmp (p, GET_MODE_NAME (j)))
6914 mode = (enum machine_mode) j;
6915 break;
6918 if (mode == VOIDmode)
6920 error ("unknown machine mode %qE", ident);
6921 return NULL_TREE;
6924 valid_mode = false;
6925 switch (GET_MODE_CLASS (mode))
6927 case MODE_INT:
6928 case MODE_PARTIAL_INT:
6929 case MODE_FLOAT:
6930 case MODE_DECIMAL_FLOAT:
6931 case MODE_FRACT:
6932 case MODE_UFRACT:
6933 case MODE_ACCUM:
6934 case MODE_UACCUM:
6935 valid_mode = targetm.scalar_mode_supported_p (mode);
6936 break;
6938 case MODE_COMPLEX_INT:
6939 case MODE_COMPLEX_FLOAT:
6940 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6941 break;
6943 case MODE_VECTOR_INT:
6944 case MODE_VECTOR_FLOAT:
6945 case MODE_VECTOR_FRACT:
6946 case MODE_VECTOR_UFRACT:
6947 case MODE_VECTOR_ACCUM:
6948 case MODE_VECTOR_UACCUM:
6949 warning (OPT_Wattributes, "specifying vector types with "
6950 "__attribute__ ((mode)) is deprecated");
6951 warning (OPT_Wattributes,
6952 "use __attribute__ ((vector_size)) instead");
6953 valid_mode = vector_mode_valid_p (mode);
6954 break;
6956 default:
6957 break;
6959 if (!valid_mode)
6961 error ("unable to emulate %qs", p);
6962 return NULL_TREE;
6965 if (POINTER_TYPE_P (type))
6967 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6968 tree (*fn)(tree, enum machine_mode, bool);
6970 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6972 error ("invalid pointer mode %qs", p);
6973 return NULL_TREE;
6976 if (TREE_CODE (type) == POINTER_TYPE)
6977 fn = build_pointer_type_for_mode;
6978 else
6979 fn = build_reference_type_for_mode;
6980 typefm = fn (TREE_TYPE (type), mode, false);
6982 else
6984 /* For fixed-point modes, we need to test if the signness of type
6985 and the machine mode are consistent. */
6986 if (ALL_FIXED_POINT_MODE_P (mode)
6987 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6989 error ("signedness of type and machine mode %qs don%'t match", p);
6990 return NULL_TREE;
6992 /* For fixed-point modes, we need to pass saturating info. */
6993 typefm = lang_hooks.types.type_for_mode (mode,
6994 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6995 : TYPE_UNSIGNED (type));
6998 if (typefm == NULL_TREE)
7000 error ("no data type for mode %qs", p);
7001 return NULL_TREE;
7003 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7005 /* For enumeral types, copy the precision from the integer
7006 type returned above. If not an INTEGER_TYPE, we can't use
7007 this mode for this type. */
7008 if (TREE_CODE (typefm) != INTEGER_TYPE)
7010 error ("cannot use mode %qs for enumeral types", p);
7011 return NULL_TREE;
7014 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7016 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7017 typefm = type;
7019 else
7021 /* We cannot build a type variant, as there's code that assumes
7022 that TYPE_MAIN_VARIANT has the same mode. This includes the
7023 debug generators. Instead, create a subrange type. This
7024 results in all of the enumeral values being emitted only once
7025 in the original, and the subtype gets them by reference. */
7026 if (TYPE_UNSIGNED (type))
7027 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7028 else
7029 typefm = make_signed_type (TYPE_PRECISION (typefm));
7030 TREE_TYPE (typefm) = type;
7033 else if (VECTOR_MODE_P (mode)
7034 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7035 : TREE_CODE (type) != TREE_CODE (typefm))
7037 error ("mode %qs applied to inappropriate type", p);
7038 return NULL_TREE;
7041 *node = typefm;
7044 return NULL_TREE;
7047 /* Handle a "section" attribute; arguments as in
7048 struct attribute_spec.handler. */
7050 static tree
7051 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7052 int ARG_UNUSED (flags), bool *no_add_attrs)
7054 tree decl = *node;
7056 if (targetm_common.have_named_sections)
7058 user_defined_section_attribute = true;
7060 if ((TREE_CODE (decl) == FUNCTION_DECL
7061 || TREE_CODE (decl) == VAR_DECL)
7062 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7064 if (TREE_CODE (decl) == VAR_DECL
7065 && current_function_decl != NULL_TREE
7066 && !TREE_STATIC (decl))
7068 error_at (DECL_SOURCE_LOCATION (decl),
7069 "section attribute cannot be specified for "
7070 "local variables");
7071 *no_add_attrs = true;
7074 /* The decl may have already been given a section attribute
7075 from a previous declaration. Ensure they match. */
7076 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7077 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7078 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7080 error ("section of %q+D conflicts with previous declaration",
7081 *node);
7082 *no_add_attrs = true;
7084 else if (TREE_CODE (decl) == VAR_DECL
7085 && !targetm.have_tls && targetm.emutls.tmpl_section
7086 && DECL_THREAD_LOCAL_P (decl))
7088 error ("section of %q+D cannot be overridden", *node);
7089 *no_add_attrs = true;
7091 else
7092 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7094 else
7096 error ("section attribute not allowed for %q+D", *node);
7097 *no_add_attrs = true;
7100 else
7102 error_at (DECL_SOURCE_LOCATION (*node),
7103 "section attributes are not supported for this target");
7104 *no_add_attrs = true;
7107 return NULL_TREE;
7110 /* Check whether ALIGN is a valid user-specified alignment. If so,
7111 return its base-2 log; if not, output an error and return -1. If
7112 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7113 no error. */
7115 check_user_alignment (const_tree align, bool allow_zero)
7117 int i;
7119 if (!INTEGRAL_TYPE_P (TREE_TYPE (align))
7120 || TREE_CODE (align) != INTEGER_CST)
7122 error ("requested alignment is not an integer constant");
7123 return -1;
7125 else if (allow_zero && integer_zerop (align))
7126 return -1;
7127 else if ((i = tree_log2 (align)) == -1)
7129 error ("requested alignment is not a power of 2");
7130 return -1;
7132 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7134 error ("requested alignment is too large");
7135 return -1;
7137 return i;
7140 /* Handle a "aligned" attribute; arguments as in
7141 struct attribute_spec.handler. */
7143 static tree
7144 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7145 int flags, bool *no_add_attrs)
7147 tree decl = NULL_TREE;
7148 tree *type = NULL;
7149 int is_type = 0;
7150 tree align_expr = (args ? TREE_VALUE (args)
7151 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
7152 int i;
7154 if (DECL_P (*node))
7156 decl = *node;
7157 type = &TREE_TYPE (decl);
7158 is_type = TREE_CODE (*node) == TYPE_DECL;
7160 else if (TYPE_P (*node))
7161 type = node, is_type = 1;
7163 if ((i = check_user_alignment (align_expr, false)) == -1)
7164 *no_add_attrs = true;
7165 else if (is_type)
7167 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7168 /* OK, modify the type in place. */;
7169 /* If we have a TYPE_DECL, then copy the type, so that we
7170 don't accidentally modify a builtin type. See pushdecl. */
7171 else if (decl && TREE_TYPE (decl) != error_mark_node
7172 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7174 tree tt = TREE_TYPE (decl);
7175 *type = build_variant_type_copy (*type);
7176 DECL_ORIGINAL_TYPE (decl) = tt;
7177 TYPE_NAME (*type) = decl;
7178 TREE_USED (*type) = TREE_USED (decl);
7179 TREE_TYPE (decl) = *type;
7181 else
7182 *type = build_variant_type_copy (*type);
7184 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7185 TYPE_USER_ALIGN (*type) = 1;
7187 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7188 && TREE_CODE (decl) != FIELD_DECL)
7190 error ("alignment may not be specified for %q+D", decl);
7191 *no_add_attrs = true;
7193 else if (TREE_CODE (decl) == FUNCTION_DECL
7194 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7196 if (DECL_USER_ALIGN (decl))
7197 error ("alignment for %q+D was previously specified as %d "
7198 "and may not be decreased", decl,
7199 DECL_ALIGN (decl) / BITS_PER_UNIT);
7200 else
7201 error ("alignment for %q+D must be at least %d", decl,
7202 DECL_ALIGN (decl) / BITS_PER_UNIT);
7203 *no_add_attrs = true;
7205 else
7207 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7208 DECL_USER_ALIGN (decl) = 1;
7211 return NULL_TREE;
7214 /* Handle a "weak" attribute; arguments as in
7215 struct attribute_spec.handler. */
7217 static tree
7218 handle_weak_attribute (tree *node, tree name,
7219 tree ARG_UNUSED (args),
7220 int ARG_UNUSED (flags),
7221 bool * ARG_UNUSED (no_add_attrs))
7223 if (TREE_CODE (*node) == FUNCTION_DECL
7224 && DECL_DECLARED_INLINE_P (*node))
7226 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7227 *no_add_attrs = true;
7229 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7231 error ("indirect function %q+D cannot be declared weak", *node);
7232 *no_add_attrs = true;
7233 return NULL_TREE;
7235 else if (TREE_CODE (*node) == FUNCTION_DECL
7236 || TREE_CODE (*node) == VAR_DECL)
7237 declare_weak (*node);
7238 else
7239 warning (OPT_Wattributes, "%qE attribute ignored", name);
7241 return NULL_TREE;
7244 /* Handle an "alias" or "ifunc" attribute; arguments as in
7245 struct attribute_spec.handler, except that IS_ALIAS tells us
7246 whether this is an alias as opposed to ifunc attribute. */
7248 static tree
7249 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7250 bool *no_add_attrs)
7252 tree decl = *node;
7254 if (TREE_CODE (decl) != FUNCTION_DECL
7255 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7257 warning (OPT_Wattributes, "%qE attribute ignored", name);
7258 *no_add_attrs = true;
7260 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7261 || (TREE_CODE (decl) != FUNCTION_DECL
7262 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7263 /* A static variable declaration is always a tentative definition,
7264 but the alias is a non-tentative definition which overrides. */
7265 || (TREE_CODE (decl) != FUNCTION_DECL
7266 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7268 error ("%q+D defined both normally and as %qE attribute", decl, name);
7269 *no_add_attrs = true;
7270 return NULL_TREE;
7272 else if (!is_alias
7273 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7274 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7276 error ("weak %q+D cannot be defined %qE", decl, name);
7277 *no_add_attrs = true;
7278 return NULL_TREE;
7281 /* Note that the very first time we process a nested declaration,
7282 decl_function_context will not be set. Indeed, *would* never
7283 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7284 we do below. After such frobbery, pushdecl would set the context.
7285 In any case, this is never what we want. */
7286 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7288 tree id;
7290 id = TREE_VALUE (args);
7291 if (TREE_CODE (id) != STRING_CST)
7293 error ("attribute %qE argument not a string", name);
7294 *no_add_attrs = true;
7295 return NULL_TREE;
7297 id = get_identifier (TREE_STRING_POINTER (id));
7298 /* This counts as a use of the object pointed to. */
7299 TREE_USED (id) = 1;
7301 if (TREE_CODE (decl) == FUNCTION_DECL)
7302 DECL_INITIAL (decl) = error_mark_node;
7303 else
7305 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
7306 DECL_EXTERNAL (decl) = 1;
7307 else
7308 DECL_EXTERNAL (decl) = 0;
7309 TREE_STATIC (decl) = 1;
7312 if (!is_alias)
7313 /* ifuncs are also aliases, so set that attribute too. */
7314 DECL_ATTRIBUTES (decl)
7315 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7317 else
7319 warning (OPT_Wattributes, "%qE attribute ignored", name);
7320 *no_add_attrs = true;
7323 return NULL_TREE;
7326 /* Handle an "alias" or "ifunc" attribute; arguments as in
7327 struct attribute_spec.handler. */
7329 static tree
7330 handle_ifunc_attribute (tree *node, tree name, tree args,
7331 int ARG_UNUSED (flags), bool *no_add_attrs)
7333 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7336 /* Handle an "alias" or "ifunc" attribute; arguments as in
7337 struct attribute_spec.handler. */
7339 static tree
7340 handle_alias_attribute (tree *node, tree name, tree args,
7341 int ARG_UNUSED (flags), bool *no_add_attrs)
7343 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7346 /* Handle a "weakref" attribute; arguments as in struct
7347 attribute_spec.handler. */
7349 static tree
7350 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7351 int flags, bool *no_add_attrs)
7353 tree attr = NULL_TREE;
7355 /* We must ignore the attribute when it is associated with
7356 local-scoped decls, since attribute alias is ignored and many
7357 such symbols do not even have a DECL_WEAK field. */
7358 if (decl_function_context (*node)
7359 || current_function_decl
7360 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7362 warning (OPT_Wattributes, "%qE attribute ignored", name);
7363 *no_add_attrs = true;
7364 return NULL_TREE;
7367 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7369 error ("indirect function %q+D cannot be declared weakref", *node);
7370 *no_add_attrs = true;
7371 return NULL_TREE;
7374 /* The idea here is that `weakref("name")' mutates into `weakref,
7375 alias("name")', and weakref without arguments, in turn,
7376 implicitly adds weak. */
7378 if (args)
7380 attr = tree_cons (get_identifier ("alias"), args, attr);
7381 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7383 *no_add_attrs = true;
7385 decl_attributes (node, attr, flags);
7387 else
7389 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7390 error_at (DECL_SOURCE_LOCATION (*node),
7391 "weakref attribute must appear before alias attribute");
7393 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7394 and that isn't supported; and because it wants to add it to
7395 the list of weak decls, which isn't helpful. */
7396 DECL_WEAK (*node) = 1;
7399 return NULL_TREE;
7402 /* Handle an "visibility" attribute; arguments as in
7403 struct attribute_spec.handler. */
7405 static tree
7406 handle_visibility_attribute (tree *node, tree name, tree args,
7407 int ARG_UNUSED (flags),
7408 bool *ARG_UNUSED (no_add_attrs))
7410 tree decl = *node;
7411 tree id = TREE_VALUE (args);
7412 enum symbol_visibility vis;
7414 if (TYPE_P (*node))
7416 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7417 /* OK */;
7418 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7420 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7421 name);
7422 return NULL_TREE;
7424 else if (TYPE_FIELDS (*node))
7426 error ("%qE attribute ignored because %qT is already defined",
7427 name, *node);
7428 return NULL_TREE;
7431 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7433 warning (OPT_Wattributes, "%qE attribute ignored", name);
7434 return NULL_TREE;
7437 if (TREE_CODE (id) != STRING_CST)
7439 error ("visibility argument not a string");
7440 return NULL_TREE;
7443 /* If this is a type, set the visibility on the type decl. */
7444 if (TYPE_P (decl))
7446 decl = TYPE_NAME (decl);
7447 if (!decl)
7448 return NULL_TREE;
7449 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7451 warning (OPT_Wattributes, "%qE attribute ignored on types",
7452 name);
7453 return NULL_TREE;
7457 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7458 vis = VISIBILITY_DEFAULT;
7459 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7460 vis = VISIBILITY_INTERNAL;
7461 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7462 vis = VISIBILITY_HIDDEN;
7463 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7464 vis = VISIBILITY_PROTECTED;
7465 else
7467 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7468 vis = VISIBILITY_DEFAULT;
7471 if (DECL_VISIBILITY_SPECIFIED (decl)
7472 && vis != DECL_VISIBILITY (decl))
7474 tree attributes = (TYPE_P (*node)
7475 ? TYPE_ATTRIBUTES (*node)
7476 : DECL_ATTRIBUTES (decl));
7477 if (lookup_attribute ("visibility", attributes))
7478 error ("%qD redeclared with different visibility", decl);
7479 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7480 && lookup_attribute ("dllimport", attributes))
7481 error ("%qD was declared %qs which implies default visibility",
7482 decl, "dllimport");
7483 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7484 && lookup_attribute ("dllexport", attributes))
7485 error ("%qD was declared %qs which implies default visibility",
7486 decl, "dllexport");
7489 DECL_VISIBILITY (decl) = vis;
7490 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7492 /* Go ahead and attach the attribute to the node as well. This is needed
7493 so we can determine whether we have VISIBILITY_DEFAULT because the
7494 visibility was not specified, or because it was explicitly overridden
7495 from the containing scope. */
7497 return NULL_TREE;
7500 /* Determine the ELF symbol visibility for DECL, which is either a
7501 variable or a function. It is an error to use this function if a
7502 definition of DECL is not available in this translation unit.
7503 Returns true if the final visibility has been determined by this
7504 function; false if the caller is free to make additional
7505 modifications. */
7507 bool
7508 c_determine_visibility (tree decl)
7510 gcc_assert (TREE_CODE (decl) == VAR_DECL
7511 || TREE_CODE (decl) == FUNCTION_DECL);
7513 /* If the user explicitly specified the visibility with an
7514 attribute, honor that. DECL_VISIBILITY will have been set during
7515 the processing of the attribute. We check for an explicit
7516 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7517 to distinguish the use of an attribute from the use of a "#pragma
7518 GCC visibility push(...)"; in the latter case we still want other
7519 considerations to be able to overrule the #pragma. */
7520 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7521 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7522 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7523 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7524 return true;
7526 /* Set default visibility to whatever the user supplied with
7527 visibility_specified depending on #pragma GCC visibility. */
7528 if (!DECL_VISIBILITY_SPECIFIED (decl))
7530 if (visibility_options.inpragma
7531 || DECL_VISIBILITY (decl) != default_visibility)
7533 DECL_VISIBILITY (decl) = default_visibility;
7534 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7535 /* If visibility changed and DECL already has DECL_RTL, ensure
7536 symbol flags are updated. */
7537 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7538 || TREE_CODE (decl) == FUNCTION_DECL)
7539 && DECL_RTL_SET_P (decl))
7540 make_decl_rtl (decl);
7543 return false;
7546 /* Handle an "tls_model" attribute; arguments as in
7547 struct attribute_spec.handler. */
7549 static tree
7550 handle_tls_model_attribute (tree *node, tree name, tree args,
7551 int ARG_UNUSED (flags), bool *no_add_attrs)
7553 tree id;
7554 tree decl = *node;
7555 enum tls_model kind;
7557 *no_add_attrs = true;
7559 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7561 warning (OPT_Wattributes, "%qE attribute ignored", name);
7562 return NULL_TREE;
7565 kind = DECL_TLS_MODEL (decl);
7566 id = TREE_VALUE (args);
7567 if (TREE_CODE (id) != STRING_CST)
7569 error ("tls_model argument not a string");
7570 return NULL_TREE;
7573 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7574 kind = TLS_MODEL_LOCAL_EXEC;
7575 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7576 kind = TLS_MODEL_INITIAL_EXEC;
7577 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7578 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7579 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7580 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7581 else
7582 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7584 DECL_TLS_MODEL (decl) = kind;
7585 return NULL_TREE;
7588 /* Handle a "no_instrument_function" attribute; arguments as in
7589 struct attribute_spec.handler. */
7591 static tree
7592 handle_no_instrument_function_attribute (tree *node, tree name,
7593 tree ARG_UNUSED (args),
7594 int ARG_UNUSED (flags),
7595 bool *no_add_attrs)
7597 tree decl = *node;
7599 if (TREE_CODE (decl) != FUNCTION_DECL)
7601 error_at (DECL_SOURCE_LOCATION (decl),
7602 "%qE attribute applies only to functions", name);
7603 *no_add_attrs = true;
7605 else if (DECL_INITIAL (decl))
7607 error_at (DECL_SOURCE_LOCATION (decl),
7608 "can%'t set %qE attribute after definition", name);
7609 *no_add_attrs = true;
7611 else
7612 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7614 return NULL_TREE;
7617 /* Handle a "malloc" attribute; arguments as in
7618 struct attribute_spec.handler. */
7620 static tree
7621 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7622 int ARG_UNUSED (flags), bool *no_add_attrs)
7624 if (TREE_CODE (*node) == FUNCTION_DECL
7625 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7626 DECL_IS_MALLOC (*node) = 1;
7627 else
7629 warning (OPT_Wattributes, "%qE attribute ignored", name);
7630 *no_add_attrs = true;
7633 return NULL_TREE;
7636 /* Handle a "alloc_size" attribute; arguments as in
7637 struct attribute_spec.handler. */
7639 static tree
7640 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7641 int ARG_UNUSED (flags), bool *no_add_attrs)
7643 unsigned arg_count = type_num_arguments (*node);
7644 for (; args; args = TREE_CHAIN (args))
7646 tree position = TREE_VALUE (args);
7648 if (TREE_CODE (position) != INTEGER_CST
7649 || TREE_INT_CST_HIGH (position)
7650 || TREE_INT_CST_LOW (position) < 1
7651 || TREE_INT_CST_LOW (position) > arg_count )
7653 warning (OPT_Wattributes,
7654 "alloc_size parameter outside range");
7655 *no_add_attrs = true;
7656 return NULL_TREE;
7659 return NULL_TREE;
7662 /* Handle a "fn spec" attribute; arguments as in
7663 struct attribute_spec.handler. */
7665 static tree
7666 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7667 tree args, int ARG_UNUSED (flags),
7668 bool *no_add_attrs ATTRIBUTE_UNUSED)
7670 gcc_assert (args
7671 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7672 && !TREE_CHAIN (args));
7673 return NULL_TREE;
7676 /* Handle a "returns_twice" attribute; arguments as in
7677 struct attribute_spec.handler. */
7679 static tree
7680 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7681 int ARG_UNUSED (flags), bool *no_add_attrs)
7683 if (TREE_CODE (*node) == FUNCTION_DECL)
7684 DECL_IS_RETURNS_TWICE (*node) = 1;
7685 else
7687 warning (OPT_Wattributes, "%qE attribute ignored", name);
7688 *no_add_attrs = true;
7691 return NULL_TREE;
7694 /* Handle a "no_limit_stack" attribute; arguments as in
7695 struct attribute_spec.handler. */
7697 static tree
7698 handle_no_limit_stack_attribute (tree *node, tree name,
7699 tree ARG_UNUSED (args),
7700 int ARG_UNUSED (flags),
7701 bool *no_add_attrs)
7703 tree decl = *node;
7705 if (TREE_CODE (decl) != FUNCTION_DECL)
7707 error_at (DECL_SOURCE_LOCATION (decl),
7708 "%qE attribute applies only to functions", name);
7709 *no_add_attrs = true;
7711 else if (DECL_INITIAL (decl))
7713 error_at (DECL_SOURCE_LOCATION (decl),
7714 "can%'t set %qE attribute after definition", name);
7715 *no_add_attrs = true;
7717 else
7718 DECL_NO_LIMIT_STACK (decl) = 1;
7720 return NULL_TREE;
7723 /* Handle a "pure" attribute; arguments as in
7724 struct attribute_spec.handler. */
7726 static tree
7727 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7728 int ARG_UNUSED (flags), bool *no_add_attrs)
7730 if (TREE_CODE (*node) == FUNCTION_DECL)
7731 DECL_PURE_P (*node) = 1;
7732 /* ??? TODO: Support types. */
7733 else
7735 warning (OPT_Wattributes, "%qE attribute ignored", name);
7736 *no_add_attrs = true;
7739 return NULL_TREE;
7742 /* Digest an attribute list destined for a transactional memory statement.
7743 ALLOWED is the set of attributes that are allowed for this statement;
7744 return the attribute we parsed. Multiple attributes are never allowed. */
7747 parse_tm_stmt_attr (tree attrs, int allowed)
7749 tree a_seen = NULL;
7750 int m_seen = 0;
7752 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
7754 tree a = TREE_PURPOSE (attrs);
7755 int m = 0;
7757 if (is_attribute_p ("outer", a))
7758 m = TM_STMT_ATTR_OUTER;
7760 if ((m & allowed) == 0)
7762 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
7763 continue;
7766 if (m_seen == 0)
7768 a_seen = a;
7769 m_seen = m;
7771 else if (m_seen == m)
7772 warning (OPT_Wattributes, "%qE attribute duplicated", a);
7773 else
7774 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
7777 return m_seen;
7780 /* Transform a TM attribute name into a maskable integer and back.
7781 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
7782 to how the lack of an attribute is treated. */
7785 tm_attr_to_mask (tree attr)
7787 if (attr == NULL)
7788 return 0;
7789 if (is_attribute_p ("transaction_safe", attr))
7790 return TM_ATTR_SAFE;
7791 if (is_attribute_p ("transaction_callable", attr))
7792 return TM_ATTR_CALLABLE;
7793 if (is_attribute_p ("transaction_pure", attr))
7794 return TM_ATTR_PURE;
7795 if (is_attribute_p ("transaction_unsafe", attr))
7796 return TM_ATTR_IRREVOCABLE;
7797 if (is_attribute_p ("transaction_may_cancel_outer", attr))
7798 return TM_ATTR_MAY_CANCEL_OUTER;
7799 return 0;
7802 tree
7803 tm_mask_to_attr (int mask)
7805 const char *str;
7806 switch (mask)
7808 case TM_ATTR_SAFE:
7809 str = "transaction_safe";
7810 break;
7811 case TM_ATTR_CALLABLE:
7812 str = "transaction_callable";
7813 break;
7814 case TM_ATTR_PURE:
7815 str = "transaction_pure";
7816 break;
7817 case TM_ATTR_IRREVOCABLE:
7818 str = "transaction_unsafe";
7819 break;
7820 case TM_ATTR_MAY_CANCEL_OUTER:
7821 str = "transaction_may_cancel_outer";
7822 break;
7823 default:
7824 gcc_unreachable ();
7826 return get_identifier (str);
7829 /* Return the first TM attribute seen in LIST. */
7831 tree
7832 find_tm_attribute (tree list)
7834 for (; list ; list = TREE_CHAIN (list))
7836 tree name = TREE_PURPOSE (list);
7837 if (tm_attr_to_mask (name) != 0)
7838 return name;
7840 return NULL_TREE;
7843 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
7844 Here we accept only function types, and verify that none of the other
7845 function TM attributes are also applied. */
7846 /* ??? We need to accept class types for C++, but not C. This greatly
7847 complicates this function, since we can no longer rely on the extra
7848 processing given by function_type_required. */
7850 static tree
7851 handle_tm_attribute (tree *node, tree name, tree args,
7852 int flags, bool *no_add_attrs)
7854 /* Only one path adds the attribute; others don't. */
7855 *no_add_attrs = true;
7857 switch (TREE_CODE (*node))
7859 case RECORD_TYPE:
7860 case UNION_TYPE:
7861 /* Only tm_callable and tm_safe apply to classes. */
7862 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
7863 goto ignored;
7864 /* FALLTHRU */
7866 case FUNCTION_TYPE:
7867 case METHOD_TYPE:
7869 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
7870 if (old_name == name)
7872 else if (old_name != NULL_TREE)
7873 error ("type was previously declared %qE", old_name);
7874 else
7875 *no_add_attrs = false;
7877 break;
7879 case POINTER_TYPE:
7881 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
7882 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
7884 tree fn_tmp = TREE_TYPE (*node);
7885 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
7886 *node = build_pointer_type (fn_tmp);
7887 break;
7890 /* FALLTHRU */
7892 default:
7893 /* If a function is next, pass it on to be tried next. */
7894 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
7895 return tree_cons (name, args, NULL);
7897 ignored:
7898 warning (OPT_Wattributes, "%qE attribute ignored", name);
7899 break;
7902 return NULL_TREE;
7905 /* Handle the TM_WRAP attribute; arguments as in
7906 struct attribute_spec.handler. */
7908 static tree
7909 handle_tm_wrap_attribute (tree *node, tree name, tree args,
7910 int ARG_UNUSED (flags), bool *no_add_attrs)
7912 tree decl = *node;
7914 /* We don't need the attribute even on success, since we
7915 record the entry in an external table. */
7916 *no_add_attrs = true;
7918 if (TREE_CODE (decl) != FUNCTION_DECL)
7919 warning (OPT_Wattributes, "%qE attribute ignored", name);
7920 else
7922 tree wrap_decl = TREE_VALUE (args);
7923 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
7924 && TREE_CODE (wrap_decl) != VAR_DECL
7925 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
7926 error ("%qE argument not an identifier", name);
7927 else
7929 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
7930 wrap_decl = lookup_name (wrap_decl);
7931 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
7933 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
7934 TREE_TYPE (wrap_decl)))
7935 record_tm_replacement (wrap_decl, decl);
7936 else
7937 error ("%qD is not compatible with %qD", wrap_decl, decl);
7939 else
7940 error ("transaction_wrap argument is not a function");
7944 return NULL_TREE;
7947 /* Ignore the given attribute. Used when this attribute may be usefully
7948 overridden by the target, but is not used generically. */
7950 static tree
7951 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
7952 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7953 bool *no_add_attrs)
7955 *no_add_attrs = true;
7956 return NULL_TREE;
7959 /* Handle a "no vops" attribute; arguments as in
7960 struct attribute_spec.handler. */
7962 static tree
7963 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7964 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7965 bool *ARG_UNUSED (no_add_attrs))
7967 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7968 DECL_IS_NOVOPS (*node) = 1;
7969 return NULL_TREE;
7972 /* Handle a "deprecated" attribute; arguments as in
7973 struct attribute_spec.handler. */
7975 static tree
7976 handle_deprecated_attribute (tree *node, tree name,
7977 tree args, int flags,
7978 bool *no_add_attrs)
7980 tree type = NULL_TREE;
7981 int warn = 0;
7982 tree what = NULL_TREE;
7984 if (!args)
7985 *no_add_attrs = true;
7986 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7988 error ("deprecated message is not a string");
7989 *no_add_attrs = true;
7992 if (DECL_P (*node))
7994 tree decl = *node;
7995 type = TREE_TYPE (decl);
7997 if (TREE_CODE (decl) == TYPE_DECL
7998 || TREE_CODE (decl) == PARM_DECL
7999 || TREE_CODE (decl) == VAR_DECL
8000 || TREE_CODE (decl) == FUNCTION_DECL
8001 || TREE_CODE (decl) == FIELD_DECL
8002 || objc_method_decl (TREE_CODE (decl)))
8003 TREE_DEPRECATED (decl) = 1;
8004 else
8005 warn = 1;
8007 else if (TYPE_P (*node))
8009 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8010 *node = build_variant_type_copy (*node);
8011 TREE_DEPRECATED (*node) = 1;
8012 type = *node;
8014 else
8015 warn = 1;
8017 if (warn)
8019 *no_add_attrs = true;
8020 if (type && TYPE_NAME (type))
8022 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8023 what = TYPE_NAME (*node);
8024 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8025 && DECL_NAME (TYPE_NAME (type)))
8026 what = DECL_NAME (TYPE_NAME (type));
8028 if (what)
8029 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8030 else
8031 warning (OPT_Wattributes, "%qE attribute ignored", name);
8034 return NULL_TREE;
8037 /* Handle a "vector_size" attribute; arguments as in
8038 struct attribute_spec.handler. */
8040 static tree
8041 handle_vector_size_attribute (tree *node, tree name, tree args,
8042 int ARG_UNUSED (flags),
8043 bool *no_add_attrs)
8045 unsigned HOST_WIDE_INT vecsize, nunits;
8046 enum machine_mode orig_mode;
8047 tree type = *node, new_type, size;
8049 *no_add_attrs = true;
8051 size = TREE_VALUE (args);
8053 if (!host_integerp (size, 1))
8055 warning (OPT_Wattributes, "%qE attribute ignored", name);
8056 return NULL_TREE;
8059 /* Get the vector size (in bytes). */
8060 vecsize = tree_low_cst (size, 1);
8062 /* We need to provide for vector pointers, vector arrays, and
8063 functions returning vectors. For example:
8065 __attribute__((vector_size(16))) short *foo;
8067 In this case, the mode is SI, but the type being modified is
8068 HI, so we need to look further. */
8070 while (POINTER_TYPE_P (type)
8071 || TREE_CODE (type) == FUNCTION_TYPE
8072 || TREE_CODE (type) == METHOD_TYPE
8073 || TREE_CODE (type) == ARRAY_TYPE
8074 || TREE_CODE (type) == OFFSET_TYPE)
8075 type = TREE_TYPE (type);
8077 /* Get the mode of the type being modified. */
8078 orig_mode = TYPE_MODE (type);
8080 if ((!INTEGRAL_TYPE_P (type)
8081 && !SCALAR_FLOAT_TYPE_P (type)
8082 && !FIXED_POINT_TYPE_P (type))
8083 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8084 && GET_MODE_CLASS (orig_mode) != MODE_INT
8085 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8086 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
8087 || TREE_CODE (type) == BOOLEAN_TYPE)
8089 error ("invalid vector type for attribute %qE", name);
8090 return NULL_TREE;
8093 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
8095 error ("vector size not an integral multiple of component size");
8096 return NULL;
8099 if (vecsize == 0)
8101 error ("zero vector size");
8102 return NULL;
8105 /* Calculate how many units fit in the vector. */
8106 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8107 if (nunits & (nunits - 1))
8109 error ("number of components of the vector not a power of two");
8110 return NULL_TREE;
8113 new_type = build_vector_type (type, nunits);
8115 /* Build back pointers if needed. */
8116 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8118 return NULL_TREE;
8121 /* Handle the "nonnull" attribute. */
8122 static tree
8123 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8124 tree args, int ARG_UNUSED (flags),
8125 bool *no_add_attrs)
8127 tree type = *node;
8128 unsigned HOST_WIDE_INT attr_arg_num;
8130 /* If no arguments are specified, all pointer arguments should be
8131 non-null. Verify a full prototype is given so that the arguments
8132 will have the correct types when we actually check them later. */
8133 if (!args)
8135 if (!prototype_p (type))
8137 error ("nonnull attribute without arguments on a non-prototype");
8138 *no_add_attrs = true;
8140 return NULL_TREE;
8143 /* Argument list specified. Verify that each argument number references
8144 a pointer argument. */
8145 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
8147 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8149 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
8151 error ("nonnull argument has invalid operand number (argument %lu)",
8152 (unsigned long) attr_arg_num);
8153 *no_add_attrs = true;
8154 return NULL_TREE;
8157 if (prototype_p (type))
8159 function_args_iterator iter;
8160 tree argument;
8162 function_args_iter_init (&iter, type);
8163 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8165 argument = function_args_iter_cond (&iter);
8166 if (argument == NULL_TREE || ck_num == arg_num)
8167 break;
8170 if (!argument
8171 || TREE_CODE (argument) == VOID_TYPE)
8173 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8174 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8175 *no_add_attrs = true;
8176 return NULL_TREE;
8179 if (TREE_CODE (argument) != POINTER_TYPE)
8181 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8182 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8183 *no_add_attrs = true;
8184 return NULL_TREE;
8189 return NULL_TREE;
8192 /* Check the argument list of a function call for null in argument slots
8193 that are marked as requiring a non-null pointer argument. The NARGS
8194 arguments are passed in the array ARGARRAY.
8197 static void
8198 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8200 tree a;
8201 int i;
8203 attrs = lookup_attribute ("nonnull", attrs);
8204 if (attrs == NULL_TREE)
8205 return;
8207 a = attrs;
8208 /* See if any of the nonnull attributes has no arguments. If so,
8209 then every pointer argument is checked (in which case the check
8210 for pointer type is done in check_nonnull_arg). */
8211 if (TREE_VALUE (a) != NULL_TREE)
8213 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8214 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8216 if (a != NULL_TREE)
8217 for (i = 0; i < nargs; i++)
8218 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8219 i + 1);
8220 else
8222 /* Walk the argument list. If we encounter an argument number we
8223 should check for non-null, do it. */
8224 for (i = 0; i < nargs; i++)
8226 for (a = attrs; ; a = TREE_CHAIN (a))
8228 a = lookup_attribute ("nonnull", a);
8229 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8230 break;
8233 if (a != NULL_TREE)
8234 check_function_arguments_recurse (check_nonnull_arg, NULL,
8235 argarray[i], i + 1);
8240 /* Check that the Nth argument of a function call (counting backwards
8241 from the end) is a (pointer)0. The NARGS arguments are passed in the
8242 array ARGARRAY. */
8244 static void
8245 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8247 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8249 if (attr)
8251 int len = 0;
8252 int pos = 0;
8253 tree sentinel;
8254 function_args_iterator iter;
8255 tree t;
8257 /* Skip over the named arguments. */
8258 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8260 if (len == nargs)
8261 break;
8262 len++;
8265 if (TREE_VALUE (attr))
8267 tree p = TREE_VALUE (TREE_VALUE (attr));
8268 pos = TREE_INT_CST_LOW (p);
8271 /* The sentinel must be one of the varargs, i.e.
8272 in position >= the number of fixed arguments. */
8273 if ((nargs - 1 - pos) < len)
8275 warning (OPT_Wformat,
8276 "not enough variable arguments to fit a sentinel");
8277 return;
8280 /* Validate the sentinel. */
8281 sentinel = argarray[nargs - 1 - pos];
8282 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8283 || !integer_zerop (sentinel))
8284 /* Although __null (in C++) is only an integer we allow it
8285 nevertheless, as we are guaranteed that it's exactly
8286 as wide as a pointer, and we don't want to force
8287 users to cast the NULL they have written there.
8288 We warn with -Wstrict-null-sentinel, though. */
8289 && (warn_strict_null_sentinel || null_node != sentinel))
8290 warning (OPT_Wformat, "missing sentinel in function call");
8294 /* Helper for check_function_nonnull; given a list of operands which
8295 must be non-null in ARGS, determine if operand PARAM_NUM should be
8296 checked. */
8298 static bool
8299 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8301 unsigned HOST_WIDE_INT arg_num = 0;
8303 for (; args; args = TREE_CHAIN (args))
8305 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8307 gcc_assert (found);
8309 if (arg_num == param_num)
8310 return true;
8312 return false;
8315 /* Check that the function argument PARAM (which is operand number
8316 PARAM_NUM) is non-null. This is called by check_function_nonnull
8317 via check_function_arguments_recurse. */
8319 static void
8320 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8321 unsigned HOST_WIDE_INT param_num)
8323 /* Just skip checking the argument if it's not a pointer. This can
8324 happen if the "nonnull" attribute was given without an operand
8325 list (which means to check every pointer argument). */
8327 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8328 return;
8330 if (integer_zerop (param))
8331 warning (OPT_Wnonnull, "null argument where non-null required "
8332 "(argument %lu)", (unsigned long) param_num);
8335 /* Helper for nonnull attribute handling; fetch the operand number
8336 from the attribute argument list. */
8338 static bool
8339 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8341 /* Verify the arg number is a constant. */
8342 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8343 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8344 return false;
8346 *valp = TREE_INT_CST_LOW (arg_num_expr);
8347 return true;
8350 /* Handle a "nothrow" attribute; arguments as in
8351 struct attribute_spec.handler. */
8353 static tree
8354 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8355 int ARG_UNUSED (flags), bool *no_add_attrs)
8357 if (TREE_CODE (*node) == FUNCTION_DECL)
8358 TREE_NOTHROW (*node) = 1;
8359 /* ??? TODO: Support types. */
8360 else
8362 warning (OPT_Wattributes, "%qE attribute ignored", name);
8363 *no_add_attrs = true;
8366 return NULL_TREE;
8369 /* Handle a "cleanup" attribute; arguments as in
8370 struct attribute_spec.handler. */
8372 static tree
8373 handle_cleanup_attribute (tree *node, tree name, tree args,
8374 int ARG_UNUSED (flags), bool *no_add_attrs)
8376 tree decl = *node;
8377 tree cleanup_id, cleanup_decl;
8379 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8380 for global destructors in C++. This requires infrastructure that
8381 we don't have generically at the moment. It's also not a feature
8382 we'd be missing too much, since we do have attribute constructor. */
8383 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8385 warning (OPT_Wattributes, "%qE attribute ignored", name);
8386 *no_add_attrs = true;
8387 return NULL_TREE;
8390 /* Verify that the argument is a function in scope. */
8391 /* ??? We could support pointers to functions here as well, if
8392 that was considered desirable. */
8393 cleanup_id = TREE_VALUE (args);
8394 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8396 error ("cleanup argument not an identifier");
8397 *no_add_attrs = true;
8398 return NULL_TREE;
8400 cleanup_decl = lookup_name (cleanup_id);
8401 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8403 error ("cleanup argument not a function");
8404 *no_add_attrs = true;
8405 return NULL_TREE;
8408 /* That the function has proper type is checked with the
8409 eventual call to build_function_call. */
8411 return NULL_TREE;
8414 /* Handle a "warn_unused_result" attribute. No special handling. */
8416 static tree
8417 handle_warn_unused_result_attribute (tree *node, tree name,
8418 tree ARG_UNUSED (args),
8419 int ARG_UNUSED (flags), bool *no_add_attrs)
8421 /* Ignore the attribute for functions not returning any value. */
8422 if (VOID_TYPE_P (TREE_TYPE (*node)))
8424 warning (OPT_Wattributes, "%qE attribute ignored", name);
8425 *no_add_attrs = true;
8428 return NULL_TREE;
8431 /* Handle a "sentinel" attribute. */
8433 static tree
8434 handle_sentinel_attribute (tree *node, tree name, tree args,
8435 int ARG_UNUSED (flags), bool *no_add_attrs)
8437 if (!prototype_p (*node))
8439 warning (OPT_Wattributes,
8440 "%qE attribute requires prototypes with named arguments", name);
8441 *no_add_attrs = true;
8443 else
8445 if (!stdarg_p (*node))
8447 warning (OPT_Wattributes,
8448 "%qE attribute only applies to variadic functions", name);
8449 *no_add_attrs = true;
8453 if (args)
8455 tree position = TREE_VALUE (args);
8457 if (TREE_CODE (position) != INTEGER_CST)
8459 warning (OPT_Wattributes,
8460 "requested position is not an integer constant");
8461 *no_add_attrs = true;
8463 else
8465 if (tree_int_cst_lt (position, integer_zero_node))
8467 warning (OPT_Wattributes,
8468 "requested position is less than zero");
8469 *no_add_attrs = true;
8474 return NULL_TREE;
8477 /* Handle a "type_generic" attribute. */
8479 static tree
8480 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8481 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8482 bool * ARG_UNUSED (no_add_attrs))
8484 /* Ensure we have a function type. */
8485 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8487 /* Ensure we have a variadic function. */
8488 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8490 return NULL_TREE;
8493 /* Handle a "target" attribute. */
8495 static tree
8496 handle_target_attribute (tree *node, tree name, tree args, int flags,
8497 bool *no_add_attrs)
8499 /* Ensure we have a function type. */
8500 if (TREE_CODE (*node) != FUNCTION_DECL)
8502 warning (OPT_Wattributes, "%qE attribute ignored", name);
8503 *no_add_attrs = true;
8505 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
8506 flags))
8507 *no_add_attrs = true;
8509 return NULL_TREE;
8512 /* Arguments being collected for optimization. */
8513 typedef const char *const_char_p; /* For DEF_VEC_P. */
8514 DEF_VEC_P(const_char_p);
8515 DEF_VEC_ALLOC_P(const_char_p, gc);
8516 static GTY(()) VEC(const_char_p, gc) *optimize_args;
8519 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
8520 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
8521 false for #pragma GCC optimize. */
8523 bool
8524 parse_optimize_options (tree args, bool attr_p)
8526 bool ret = true;
8527 unsigned opt_argc;
8528 unsigned i;
8529 int saved_flag_strict_aliasing;
8530 const char **opt_argv;
8531 struct cl_decoded_option *decoded_options;
8532 unsigned int decoded_options_count;
8533 tree ap;
8535 /* Build up argv vector. Just in case the string is stored away, use garbage
8536 collected strings. */
8537 VEC_truncate (const_char_p, optimize_args, 0);
8538 VEC_safe_push (const_char_p, gc, optimize_args, (const_char_p)NULL);
8540 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
8542 tree value = TREE_VALUE (ap);
8544 if (TREE_CODE (value) == INTEGER_CST)
8546 char buffer[20];
8547 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
8548 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
8551 else if (TREE_CODE (value) == STRING_CST)
8553 /* Split string into multiple substrings. */
8554 size_t len = TREE_STRING_LENGTH (value);
8555 char *p = ASTRDUP (TREE_STRING_POINTER (value));
8556 char *end = p + len;
8557 char *comma;
8558 char *next_p = p;
8560 while (next_p != NULL)
8562 size_t len2;
8563 char *q, *r;
8565 p = next_p;
8566 comma = strchr (p, ',');
8567 if (comma)
8569 len2 = comma - p;
8570 *comma = '\0';
8571 next_p = comma+1;
8573 else
8575 len2 = end - p;
8576 next_p = NULL;
8579 r = q = (char *) ggc_alloc_atomic (len2 + 3);
8581 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
8582 options. */
8583 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
8585 ret = false;
8586 if (attr_p)
8587 warning (OPT_Wattributes,
8588 "bad option %s to optimize attribute", p);
8589 else
8590 warning (OPT_Wpragmas,
8591 "bad option %s to pragma attribute", p);
8592 continue;
8595 if (*p != '-')
8597 *r++ = '-';
8599 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
8600 itself is -Os, and any other switch begins with a -f. */
8601 if ((*p >= '0' && *p <= '9')
8602 || (p[0] == 's' && p[1] == '\0'))
8603 *r++ = 'O';
8604 else if (*p != 'O')
8605 *r++ = 'f';
8608 memcpy (r, p, len2);
8609 r[len2] = '\0';
8610 VEC_safe_push (const_char_p, gc, optimize_args, q);
8616 opt_argc = VEC_length (const_char_p, optimize_args);
8617 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
8619 for (i = 1; i < opt_argc; i++)
8620 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
8622 saved_flag_strict_aliasing = flag_strict_aliasing;
8624 /* Now parse the options. */
8625 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
8626 &decoded_options,
8627 &decoded_options_count);
8628 decode_options (&global_options, &global_options_set,
8629 decoded_options, decoded_options_count,
8630 input_location, global_dc);
8632 targetm.override_options_after_change();
8634 /* Don't allow changing -fstrict-aliasing. */
8635 flag_strict_aliasing = saved_flag_strict_aliasing;
8637 VEC_truncate (const_char_p, optimize_args, 0);
8638 return ret;
8641 /* For handling "optimize" attribute. arguments as in
8642 struct attribute_spec.handler. */
8644 static tree
8645 handle_optimize_attribute (tree *node, tree name, tree args,
8646 int ARG_UNUSED (flags), bool *no_add_attrs)
8648 /* Ensure we have a function type. */
8649 if (TREE_CODE (*node) != FUNCTION_DECL)
8651 warning (OPT_Wattributes, "%qE attribute ignored", name);
8652 *no_add_attrs = true;
8654 else
8656 struct cl_optimization cur_opts;
8657 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
8659 /* Save current options. */
8660 cl_optimization_save (&cur_opts, &global_options);
8662 /* If we previously had some optimization options, use them as the
8663 default. */
8664 if (old_opts)
8665 cl_optimization_restore (&global_options,
8666 TREE_OPTIMIZATION (old_opts));
8668 /* Parse options, and update the vector. */
8669 parse_optimize_options (args, true);
8670 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
8671 = build_optimization_node ();
8673 /* Restore current options. */
8674 cl_optimization_restore (&global_options, &cur_opts);
8677 return NULL_TREE;
8680 /* Handle a "no_split_stack" attribute. */
8682 static tree
8683 handle_no_split_stack_attribute (tree *node, tree name,
8684 tree ARG_UNUSED (args),
8685 int ARG_UNUSED (flags),
8686 bool *no_add_attrs)
8688 tree decl = *node;
8690 if (TREE_CODE (decl) != FUNCTION_DECL)
8692 error_at (DECL_SOURCE_LOCATION (decl),
8693 "%qE attribute applies only to functions", name);
8694 *no_add_attrs = true;
8696 else if (DECL_INITIAL (decl))
8698 error_at (DECL_SOURCE_LOCATION (decl),
8699 "can%'t set %qE attribute after definition", name);
8700 *no_add_attrs = true;
8703 return NULL_TREE;
8706 /* Check for valid arguments being passed to a function with FNTYPE.
8707 There are NARGS arguments in the array ARGARRAY. */
8708 void
8709 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
8711 /* Check for null being passed in a pointer argument that must be
8712 non-null. We also need to do this if format checking is enabled. */
8714 if (warn_nonnull)
8715 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8717 /* Check for errors in format strings. */
8719 if (warn_format || warn_suggest_attribute_format)
8720 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
8722 if (warn_format)
8723 check_function_sentinel (fntype, nargs, argarray);
8726 /* Generic argument checking recursion routine. PARAM is the argument to
8727 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
8728 once the argument is resolved. CTX is context for the callback. */
8729 void
8730 check_function_arguments_recurse (void (*callback)
8731 (void *, tree, unsigned HOST_WIDE_INT),
8732 void *ctx, tree param,
8733 unsigned HOST_WIDE_INT param_num)
8735 if (CONVERT_EXPR_P (param)
8736 && (TYPE_PRECISION (TREE_TYPE (param))
8737 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8739 /* Strip coercion. */
8740 check_function_arguments_recurse (callback, ctx,
8741 TREE_OPERAND (param, 0), param_num);
8742 return;
8745 if (TREE_CODE (param) == CALL_EXPR)
8747 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8748 tree attrs;
8749 bool found_format_arg = false;
8751 /* See if this is a call to a known internationalization function
8752 that modifies a format arg. Such a function may have multiple
8753 format_arg attributes (for example, ngettext). */
8755 for (attrs = TYPE_ATTRIBUTES (type);
8756 attrs;
8757 attrs = TREE_CHAIN (attrs))
8758 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8760 tree inner_arg;
8761 tree format_num_expr;
8762 int format_num;
8763 int i;
8764 call_expr_arg_iterator iter;
8766 /* Extract the argument number, which was previously checked
8767 to be valid. */
8768 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8770 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8771 && !TREE_INT_CST_HIGH (format_num_expr));
8773 format_num = TREE_INT_CST_LOW (format_num_expr);
8775 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8776 inner_arg != 0;
8777 inner_arg = next_call_expr_arg (&iter), i++)
8778 if (i == format_num)
8780 check_function_arguments_recurse (callback, ctx,
8781 inner_arg, param_num);
8782 found_format_arg = true;
8783 break;
8787 /* If we found a format_arg attribute and did a recursive check,
8788 we are done with checking this argument. Otherwise, we continue
8789 and this will be considered a non-literal. */
8790 if (found_format_arg)
8791 return;
8794 if (TREE_CODE (param) == COND_EXPR)
8796 /* Check both halves of the conditional expression. */
8797 check_function_arguments_recurse (callback, ctx,
8798 TREE_OPERAND (param, 1), param_num);
8799 check_function_arguments_recurse (callback, ctx,
8800 TREE_OPERAND (param, 2), param_num);
8801 return;
8804 (*callback) (ctx, param, param_num);
8807 /* Checks for a builtin function FNDECL that the number of arguments
8808 NARGS against the required number REQUIRED and issues an error if
8809 there is a mismatch. Returns true if the number of arguments is
8810 correct, otherwise false. */
8812 static bool
8813 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8815 if (nargs < required)
8817 error_at (input_location,
8818 "not enough arguments to function %qE", fndecl);
8819 return false;
8821 else if (nargs > required)
8823 error_at (input_location,
8824 "too many arguments to function %qE", fndecl);
8825 return false;
8827 return true;
8830 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8831 Returns false if there was an error, otherwise true. */
8833 bool
8834 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8836 if (!DECL_BUILT_IN (fndecl)
8837 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8838 return true;
8840 switch (DECL_FUNCTION_CODE (fndecl))
8842 case BUILT_IN_CONSTANT_P:
8843 return builtin_function_validate_nargs (fndecl, nargs, 1);
8845 case BUILT_IN_ISFINITE:
8846 case BUILT_IN_ISINF:
8847 case BUILT_IN_ISINF_SIGN:
8848 case BUILT_IN_ISNAN:
8849 case BUILT_IN_ISNORMAL:
8850 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8852 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8854 error ("non-floating-point argument in call to "
8855 "function %qE", fndecl);
8856 return false;
8858 return true;
8860 return false;
8862 case BUILT_IN_ISGREATER:
8863 case BUILT_IN_ISGREATEREQUAL:
8864 case BUILT_IN_ISLESS:
8865 case BUILT_IN_ISLESSEQUAL:
8866 case BUILT_IN_ISLESSGREATER:
8867 case BUILT_IN_ISUNORDERED:
8868 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8870 enum tree_code code0, code1;
8871 code0 = TREE_CODE (TREE_TYPE (args[0]));
8872 code1 = TREE_CODE (TREE_TYPE (args[1]));
8873 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8874 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8875 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8877 error ("non-floating-point arguments in call to "
8878 "function %qE", fndecl);
8879 return false;
8881 return true;
8883 return false;
8885 case BUILT_IN_FPCLASSIFY:
8886 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8888 unsigned i;
8890 for (i=0; i<5; i++)
8891 if (TREE_CODE (args[i]) != INTEGER_CST)
8893 error ("non-const integer argument %u in call to function %qE",
8894 i+1, fndecl);
8895 return false;
8898 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8900 error ("non-floating-point argument in call to function %qE",
8901 fndecl);
8902 return false;
8904 return true;
8906 return false;
8908 case BUILT_IN_ASSUME_ALIGNED:
8909 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
8911 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
8913 error ("non-integer argument 3 in call to function %qE", fndecl);
8914 return false;
8916 return true;
8918 return false;
8920 default:
8921 return true;
8925 /* Function to help qsort sort FIELD_DECLs by name order. */
8928 field_decl_cmp (const void *x_p, const void *y_p)
8930 const tree *const x = (const tree *const) x_p;
8931 const tree *const y = (const tree *const) y_p;
8933 if (DECL_NAME (*x) == DECL_NAME (*y))
8934 /* A nontype is "greater" than a type. */
8935 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8936 if (DECL_NAME (*x) == NULL_TREE)
8937 return -1;
8938 if (DECL_NAME (*y) == NULL_TREE)
8939 return 1;
8940 if (DECL_NAME (*x) < DECL_NAME (*y))
8941 return -1;
8942 return 1;
8945 static struct {
8946 gt_pointer_operator new_value;
8947 void *cookie;
8948 } resort_data;
8950 /* This routine compares two fields like field_decl_cmp but using the
8951 pointer operator in resort_data. */
8953 static int
8954 resort_field_decl_cmp (const void *x_p, const void *y_p)
8956 const tree *const x = (const tree *const) x_p;
8957 const tree *const y = (const tree *const) y_p;
8959 if (DECL_NAME (*x) == DECL_NAME (*y))
8960 /* A nontype is "greater" than a type. */
8961 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8962 if (DECL_NAME (*x) == NULL_TREE)
8963 return -1;
8964 if (DECL_NAME (*y) == NULL_TREE)
8965 return 1;
8967 tree d1 = DECL_NAME (*x);
8968 tree d2 = DECL_NAME (*y);
8969 resort_data.new_value (&d1, resort_data.cookie);
8970 resort_data.new_value (&d2, resort_data.cookie);
8971 if (d1 < d2)
8972 return -1;
8974 return 1;
8977 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8979 void
8980 resort_sorted_fields (void *obj,
8981 void * ARG_UNUSED (orig_obj),
8982 gt_pointer_operator new_value,
8983 void *cookie)
8985 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8986 resort_data.new_value = new_value;
8987 resort_data.cookie = cookie;
8988 qsort (&sf->elts[0], sf->len, sizeof (tree),
8989 resort_field_decl_cmp);
8992 /* Subroutine of c_parse_error.
8993 Return the result of concatenating LHS and RHS. RHS is really
8994 a string literal, its first character is indicated by RHS_START and
8995 RHS_SIZE is its length (including the terminating NUL character).
8997 The caller is responsible for deleting the returned pointer. */
8999 static char *
9000 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9002 const int lhs_size = strlen (lhs);
9003 char *result = XNEWVEC (char, lhs_size + rhs_size);
9004 strncpy (result, lhs, lhs_size);
9005 strncpy (result + lhs_size, rhs_start, rhs_size);
9006 return result;
9009 /* Issue the error given by GMSGID, indicating that it occurred before
9010 TOKEN, which had the associated VALUE. */
9012 void
9013 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9014 tree value, unsigned char token_flags)
9016 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9018 char *message = NULL;
9020 if (token_type == CPP_EOF)
9021 message = catenate_messages (gmsgid, " at end of input");
9022 else if (token_type == CPP_CHAR
9023 || token_type == CPP_WCHAR
9024 || token_type == CPP_CHAR16
9025 || token_type == CPP_CHAR32)
9027 unsigned int val = TREE_INT_CST_LOW (value);
9028 const char *prefix;
9030 switch (token_type)
9032 default:
9033 prefix = "";
9034 break;
9035 case CPP_WCHAR:
9036 prefix = "L";
9037 break;
9038 case CPP_CHAR16:
9039 prefix = "u";
9040 break;
9041 case CPP_CHAR32:
9042 prefix = "U";
9043 break;
9046 if (val <= UCHAR_MAX && ISGRAPH (val))
9047 message = catenate_messages (gmsgid, " before %s'%c'");
9048 else
9049 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9051 error (message, prefix, val);
9052 free (message);
9053 message = NULL;
9055 else if (token_type == CPP_STRING
9056 || token_type == CPP_WSTRING
9057 || token_type == CPP_STRING16
9058 || token_type == CPP_STRING32
9059 || token_type == CPP_UTF8STRING)
9060 message = catenate_messages (gmsgid, " before string constant");
9061 else if (token_type == CPP_NUMBER)
9062 message = catenate_messages (gmsgid, " before numeric constant");
9063 else if (token_type == CPP_NAME)
9065 message = catenate_messages (gmsgid, " before %qE");
9066 error (message, value);
9067 free (message);
9068 message = NULL;
9070 else if (token_type == CPP_PRAGMA)
9071 message = catenate_messages (gmsgid, " before %<#pragma%>");
9072 else if (token_type == CPP_PRAGMA_EOL)
9073 message = catenate_messages (gmsgid, " before end of line");
9074 else if (token_type == CPP_DECLTYPE)
9075 message = catenate_messages (gmsgid, " before %<decltype%>");
9076 else if (token_type < N_TTYPES)
9078 message = catenate_messages (gmsgid, " before %qs token");
9079 error (message, cpp_type2name (token_type, token_flags));
9080 free (message);
9081 message = NULL;
9083 else
9084 error (gmsgid);
9086 if (message)
9088 error (message);
9089 free (message);
9091 #undef catenate_messages
9094 /* Mapping for cpp message reasons to the options that enable them. */
9096 struct reason_option_codes_t
9098 const int reason; /* cpplib message reason. */
9099 const int option_code; /* gcc option that controls this message. */
9102 static const struct reason_option_codes_t option_codes[] = {
9103 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9104 {CPP_W_COMMENTS, OPT_Wcomment},
9105 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9106 {CPP_W_MULTICHAR, OPT_Wmultichar},
9107 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9108 {CPP_W_LONG_LONG, OPT_Wlong_long},
9109 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9110 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9111 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9112 {CPP_W_UNDEF, OPT_Wundef},
9113 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9114 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9115 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9116 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9117 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9118 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9119 {CPP_W_NONE, 0}
9122 /* Return the gcc option code associated with the reason for a cpp
9123 message, or 0 if none. */
9125 static int
9126 c_option_controlling_cpp_error (int reason)
9128 const struct reason_option_codes_t *entry;
9130 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9132 if (entry->reason == reason)
9133 return entry->option_code;
9135 return 0;
9138 /* Callback from cpp_error for PFILE to print diagnostics from the
9139 preprocessor. The diagnostic is of type LEVEL, with REASON set
9140 to the reason code if LEVEL is represents a warning, at location
9141 LOCATION unless this is after lexing and the compiler's location
9142 should be used instead, with column number possibly overridden by
9143 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9144 the arguments. Returns true if a diagnostic was emitted, false
9145 otherwise. */
9147 bool
9148 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9149 location_t location, unsigned int column_override,
9150 const char *msg, va_list *ap)
9152 diagnostic_info diagnostic;
9153 diagnostic_t dlevel;
9154 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9155 bool ret;
9157 switch (level)
9159 case CPP_DL_WARNING_SYSHDR:
9160 if (flag_no_output)
9161 return false;
9162 global_dc->dc_warn_system_headers = 1;
9163 /* Fall through. */
9164 case CPP_DL_WARNING:
9165 if (flag_no_output)
9166 return false;
9167 dlevel = DK_WARNING;
9168 break;
9169 case CPP_DL_PEDWARN:
9170 if (flag_no_output && !flag_pedantic_errors)
9171 return false;
9172 dlevel = DK_PEDWARN;
9173 break;
9174 case CPP_DL_ERROR:
9175 dlevel = DK_ERROR;
9176 break;
9177 case CPP_DL_ICE:
9178 dlevel = DK_ICE;
9179 break;
9180 case CPP_DL_NOTE:
9181 dlevel = DK_NOTE;
9182 break;
9183 case CPP_DL_FATAL:
9184 dlevel = DK_FATAL;
9185 break;
9186 default:
9187 gcc_unreachable ();
9189 if (done_lexing)
9190 location = input_location;
9191 diagnostic_set_info_translated (&diagnostic, msg, ap,
9192 location, dlevel);
9193 if (column_override)
9194 diagnostic_override_column (&diagnostic, column_override);
9195 diagnostic_override_option_index (&diagnostic,
9196 c_option_controlling_cpp_error (reason));
9197 ret = report_diagnostic (&diagnostic);
9198 if (level == CPP_DL_WARNING_SYSHDR)
9199 global_dc->dc_warn_system_headers = save_warn_system_headers;
9200 return ret;
9203 /* Convert a character from the host to the target execution character
9204 set. cpplib handles this, mostly. */
9206 HOST_WIDE_INT
9207 c_common_to_target_charset (HOST_WIDE_INT c)
9209 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9210 zero-extended under -fno-signed-char. cpplib insists that characters
9211 and character constants are always unsigned. Hence we must convert
9212 back and forth. */
9213 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9215 uc = cpp_host_to_exec_charset (parse_in, uc);
9217 if (flag_signed_char)
9218 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9219 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9220 else
9221 return uc;
9224 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9225 references with an INDIRECT_REF of a constant at the bottom; much like the
9226 traditional rendering of offsetof as a macro. Return the folded result. */
9228 tree
9229 fold_offsetof_1 (tree expr)
9231 tree base, off, t;
9233 switch (TREE_CODE (expr))
9235 case ERROR_MARK:
9236 return expr;
9238 case VAR_DECL:
9239 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9240 return error_mark_node;
9242 case CALL_EXPR:
9243 case TARGET_EXPR:
9244 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9245 return error_mark_node;
9247 case NOP_EXPR:
9248 case INDIRECT_REF:
9249 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9251 error ("cannot apply %<offsetof%> to a non constant address");
9252 return error_mark_node;
9254 return TREE_OPERAND (expr, 0);
9256 case COMPONENT_REF:
9257 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9258 if (base == error_mark_node)
9259 return base;
9261 t = TREE_OPERAND (expr, 1);
9262 if (DECL_C_BIT_FIELD (t))
9264 error ("attempt to take address of bit-field structure "
9265 "member %qD", t);
9266 return error_mark_node;
9268 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9269 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
9271 / BITS_PER_UNIT));
9272 break;
9274 case ARRAY_REF:
9275 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9276 if (base == error_mark_node)
9277 return base;
9279 t = TREE_OPERAND (expr, 1);
9281 /* Check if the offset goes beyond the upper bound of the array. */
9282 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9284 tree upbound = array_ref_up_bound (expr);
9285 if (upbound != NULL_TREE
9286 && TREE_CODE (upbound) == INTEGER_CST
9287 && !tree_int_cst_equal (upbound,
9288 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9290 upbound = size_binop (PLUS_EXPR, upbound,
9291 build_int_cst (TREE_TYPE (upbound), 1));
9292 if (tree_int_cst_lt (upbound, t))
9294 tree v;
9296 for (v = TREE_OPERAND (expr, 0);
9297 TREE_CODE (v) == COMPONENT_REF;
9298 v = TREE_OPERAND (v, 0))
9299 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9300 == RECORD_TYPE)
9302 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9303 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9304 if (TREE_CODE (fld_chain) == FIELD_DECL)
9305 break;
9307 if (fld_chain)
9308 break;
9310 /* Don't warn if the array might be considered a poor
9311 man's flexible array member with a very permissive
9312 definition thereof. */
9313 if (TREE_CODE (v) == ARRAY_REF
9314 || TREE_CODE (v) == COMPONENT_REF)
9315 warning (OPT_Warray_bounds,
9316 "index %E denotes an offset "
9317 "greater than size of %qT",
9318 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9323 t = convert (sizetype, t);
9324 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9325 break;
9327 case COMPOUND_EXPR:
9328 /* Handle static members of volatile structs. */
9329 t = TREE_OPERAND (expr, 1);
9330 gcc_assert (TREE_CODE (t) == VAR_DECL);
9331 return fold_offsetof_1 (t);
9333 default:
9334 gcc_unreachable ();
9337 return fold_build_pointer_plus (base, off);
9340 /* Likewise, but convert it to the return type of offsetof. */
9342 tree
9343 fold_offsetof (tree expr)
9345 return convert (size_type_node, fold_offsetof_1 (expr));
9348 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9349 expression, because B will always be true. */
9351 void
9352 warn_for_omitted_condop (location_t location, tree cond)
9354 if (truth_value_p (TREE_CODE (cond)))
9355 warning_at (location, OPT_Wparentheses,
9356 "the omitted middle operand in ?: will always be %<true%>, "
9357 "suggest explicit middle operand");
9360 /* Give an error for storing into ARG, which is 'const'. USE indicates
9361 how ARG was being used. */
9363 void
9364 readonly_error (tree arg, enum lvalue_use use)
9366 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9367 || use == lv_asm);
9368 /* Using this macro rather than (for example) arrays of messages
9369 ensures that all the format strings are checked at compile
9370 time. */
9371 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9372 : (use == lv_increment ? (I) \
9373 : (use == lv_decrement ? (D) : (AS))))
9374 if (TREE_CODE (arg) == COMPONENT_REF)
9376 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9377 error (READONLY_MSG (G_("assignment of member "
9378 "%qD in read-only object"),
9379 G_("increment of member "
9380 "%qD in read-only object"),
9381 G_("decrement of member "
9382 "%qD in read-only object"),
9383 G_("member %qD in read-only object "
9384 "used as %<asm%> output")),
9385 TREE_OPERAND (arg, 1));
9386 else
9387 error (READONLY_MSG (G_("assignment of read-only member %qD"),
9388 G_("increment of read-only member %qD"),
9389 G_("decrement of read-only member %qD"),
9390 G_("read-only member %qD used as %<asm%> output")),
9391 TREE_OPERAND (arg, 1));
9393 else if (TREE_CODE (arg) == VAR_DECL)
9394 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
9395 G_("increment of read-only variable %qD"),
9396 G_("decrement of read-only variable %qD"),
9397 G_("read-only variable %qD used as %<asm%> output")),
9398 arg);
9399 else if (TREE_CODE (arg) == PARM_DECL)
9400 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
9401 G_("increment of read-only parameter %qD"),
9402 G_("decrement of read-only parameter %qD"),
9403 G_("read-only parameter %qD use as %<asm%> output")),
9404 arg);
9405 else if (TREE_CODE (arg) == RESULT_DECL)
9407 gcc_assert (c_dialect_cxx ());
9408 error (READONLY_MSG (G_("assignment of "
9409 "read-only named return value %qD"),
9410 G_("increment of "
9411 "read-only named return value %qD"),
9412 G_("decrement of "
9413 "read-only named return value %qD"),
9414 G_("read-only named return value %qD "
9415 "used as %<asm%>output")),
9416 arg);
9418 else if (TREE_CODE (arg) == FUNCTION_DECL)
9419 error (READONLY_MSG (G_("assignment of function %qD"),
9420 G_("increment of function %qD"),
9421 G_("decrement of function %qD"),
9422 G_("function %qD used as %<asm%> output")),
9423 arg);
9424 else
9425 error (READONLY_MSG (G_("assignment of read-only location %qE"),
9426 G_("increment of read-only location %qE"),
9427 G_("decrement of read-only location %qE"),
9428 G_("read-only location %qE used as %<asm%> output")),
9429 arg);
9432 /* Print an error message for an invalid lvalue. USE says
9433 how the lvalue is being used and so selects the error message. LOC
9434 is the location for the error. */
9436 void
9437 lvalue_error (location_t loc, enum lvalue_use use)
9439 switch (use)
9441 case lv_assign:
9442 error_at (loc, "lvalue required as left operand of assignment");
9443 break;
9444 case lv_increment:
9445 error_at (loc, "lvalue required as increment operand");
9446 break;
9447 case lv_decrement:
9448 error_at (loc, "lvalue required as decrement operand");
9449 break;
9450 case lv_addressof:
9451 error_at (loc, "lvalue required as unary %<&%> operand");
9452 break;
9453 case lv_asm:
9454 error_at (loc, "lvalue required in asm statement");
9455 break;
9456 default:
9457 gcc_unreachable ();
9461 /* Print an error message for an invalid indirection of type TYPE.
9462 ERRSTRING is the name of the operator for the indirection. */
9464 void
9465 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9467 switch (errstring)
9469 case RO_NULL:
9470 gcc_assert (c_dialect_cxx ());
9471 error_at (loc, "invalid type argument (have %qT)", type);
9472 break;
9473 case RO_ARRAY_INDEXING:
9474 error_at (loc,
9475 "invalid type argument of array indexing (have %qT)",
9476 type);
9477 break;
9478 case RO_UNARY_STAR:
9479 error_at (loc,
9480 "invalid type argument of unary %<*%> (have %qT)",
9481 type);
9482 break;
9483 case RO_ARROW:
9484 error_at (loc,
9485 "invalid type argument of %<->%> (have %qT)",
9486 type);
9487 break;
9488 case RO_IMPLICIT_CONVERSION:
9489 error_at (loc,
9490 "invalid type argument of implicit conversion (have %qT)",
9491 type);
9492 break;
9493 default:
9494 gcc_unreachable ();
9498 /* *PTYPE is an incomplete array. Complete it with a domain based on
9499 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
9500 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9501 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
9504 complete_array_type (tree *ptype, tree initial_value, bool do_default)
9506 tree maxindex, type, main_type, elt, unqual_elt;
9507 int failure = 0, quals;
9508 hashval_t hashcode = 0;
9510 maxindex = size_zero_node;
9511 if (initial_value)
9513 if (TREE_CODE (initial_value) == STRING_CST)
9515 int eltsize
9516 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9517 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
9519 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9521 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
9523 if (VEC_empty (constructor_elt, v))
9525 if (pedantic)
9526 failure = 3;
9527 maxindex = ssize_int (-1);
9529 else
9531 tree curindex;
9532 unsigned HOST_WIDE_INT cnt;
9533 constructor_elt *ce;
9534 bool fold_p = false;
9536 if (VEC_index (constructor_elt, v, 0).index)
9537 maxindex = fold_convert_loc (input_location, sizetype,
9538 VEC_index (constructor_elt,
9539 v, 0).index);
9540 curindex = maxindex;
9542 for (cnt = 1;
9543 VEC_iterate (constructor_elt, v, cnt, ce);
9544 cnt++)
9546 bool curfold_p = false;
9547 if (ce->index)
9548 curindex = ce->index, curfold_p = true;
9549 else
9551 if (fold_p)
9552 curindex = fold_convert (sizetype, curindex);
9553 curindex = size_binop (PLUS_EXPR, curindex,
9554 size_one_node);
9556 if (tree_int_cst_lt (maxindex, curindex))
9557 maxindex = curindex, fold_p = curfold_p;
9559 if (fold_p)
9560 maxindex = fold_convert (sizetype, maxindex);
9563 else
9565 /* Make an error message unless that happened already. */
9566 if (initial_value != error_mark_node)
9567 failure = 1;
9570 else
9572 failure = 2;
9573 if (!do_default)
9574 return failure;
9577 type = *ptype;
9578 elt = TREE_TYPE (type);
9579 quals = TYPE_QUALS (strip_array_types (elt));
9580 if (quals == 0)
9581 unqual_elt = elt;
9582 else
9583 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
9585 /* Using build_distinct_type_copy and modifying things afterward instead
9586 of using build_array_type to create a new type preserves all of the
9587 TYPE_LANG_FLAG_? bits that the front end may have set. */
9588 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9589 TREE_TYPE (main_type) = unqual_elt;
9590 TYPE_DOMAIN (main_type)
9591 = build_range_type (TREE_TYPE (maxindex),
9592 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
9593 layout_type (main_type);
9595 /* Make sure we have the canonical MAIN_TYPE. */
9596 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
9597 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
9598 hashcode);
9599 main_type = type_hash_canon (hashcode, main_type);
9601 /* Fix the canonical type. */
9602 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
9603 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
9604 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
9605 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
9606 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
9607 != TYPE_DOMAIN (main_type)))
9608 TYPE_CANONICAL (main_type)
9609 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
9610 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
9611 else
9612 TYPE_CANONICAL (main_type) = main_type;
9614 if (quals == 0)
9615 type = main_type;
9616 else
9617 type = c_build_qualified_type (main_type, quals);
9619 if (COMPLETE_TYPE_P (type)
9620 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9621 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9623 error ("size of array is too large");
9624 /* If we proceed with the array type as it is, we'll eventually
9625 crash in tree_low_cst(). */
9626 type = error_mark_node;
9629 *ptype = type;
9630 return failure;
9633 /* Like c_mark_addressable but don't check register qualifier. */
9634 void
9635 c_common_mark_addressable_vec (tree t)
9637 while (handled_component_p (t))
9638 t = TREE_OPERAND (t, 0);
9639 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
9640 return;
9641 TREE_ADDRESSABLE (t) = 1;
9646 /* Used to help initialize the builtin-types.def table. When a type of
9647 the correct size doesn't exist, use error_mark_node instead of NULL.
9648 The later results in segfaults even when a decl using the type doesn't
9649 get invoked. */
9651 tree
9652 builtin_type_for_size (int size, bool unsignedp)
9654 tree type = c_common_type_for_size (size, unsignedp);
9655 return type ? type : error_mark_node;
9658 /* A helper function for resolve_overloaded_builtin in resolving the
9659 overloaded __sync_ builtins. Returns a positive power of 2 if the
9660 first operand of PARAMS is a pointer to a supported data type.
9661 Returns 0 if an error is encountered. */
9663 static int
9664 sync_resolve_size (tree function, VEC(tree,gc) *params)
9666 tree type;
9667 int size;
9669 if (VEC_empty (tree, params))
9671 error ("too few arguments to function %qE", function);
9672 return 0;
9675 type = TREE_TYPE (VEC_index (tree, params, 0));
9676 if (TREE_CODE (type) != POINTER_TYPE)
9677 goto incompatible;
9679 type = TREE_TYPE (type);
9680 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9681 goto incompatible;
9683 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
9684 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
9685 return size;
9687 incompatible:
9688 error ("incompatible type for argument %d of %qE", 1, function);
9689 return 0;
9692 /* A helper function for resolve_overloaded_builtin. Adds casts to
9693 PARAMS to make arguments match up with those of FUNCTION. Drops
9694 the variadic arguments at the end. Returns false if some error
9695 was encountered; true on success. */
9697 static bool
9698 sync_resolve_params (location_t loc, tree orig_function, tree function,
9699 VEC(tree, gc) *params, bool orig_format)
9701 function_args_iterator iter;
9702 tree ptype;
9703 unsigned int parmnum;
9705 function_args_iter_init (&iter, TREE_TYPE (function));
9706 /* We've declared the implementation functions to use "volatile void *"
9707 as the pointer parameter, so we shouldn't get any complaints from the
9708 call to check_function_arguments what ever type the user used. */
9709 function_args_iter_next (&iter);
9710 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
9712 /* For the rest of the values, we need to cast these to FTYPE, so that we
9713 don't get warnings for passing pointer types, etc. */
9714 parmnum = 0;
9715 while (1)
9717 tree val, arg_type;
9719 arg_type = function_args_iter_cond (&iter);
9720 /* XXX void_type_node belies the abstraction. */
9721 if (arg_type == void_type_node)
9722 break;
9724 ++parmnum;
9725 if (VEC_length (tree, params) <= parmnum)
9727 error_at (loc, "too few arguments to function %qE", orig_function);
9728 return false;
9731 /* Only convert parameters if arg_type is unsigned integer type with
9732 new format sync routines, i.e. don't attempt to convert pointer
9733 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
9734 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
9735 kinds). */
9736 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
9738 /* Ideally for the first conversion we'd use convert_for_assignment
9739 so that we get warnings for anything that doesn't match the pointer
9740 type. This isn't portable across the C and C++ front ends atm. */
9741 val = VEC_index (tree, params, parmnum);
9742 val = convert (ptype, val);
9743 val = convert (arg_type, val);
9744 VEC_replace (tree, params, parmnum, val);
9747 function_args_iter_next (&iter);
9750 /* __atomic routines are not variadic. */
9751 if (!orig_format && VEC_length (tree, params) != parmnum + 1)
9753 error_at (loc, "too many arguments to function %qE", orig_function);
9754 return false;
9757 /* The definition of these primitives is variadic, with the remaining
9758 being "an optional list of variables protected by the memory barrier".
9759 No clue what that's supposed to mean, precisely, but we consider all
9760 call-clobbered variables to be protected so we're safe. */
9761 VEC_truncate (tree, params, parmnum + 1);
9763 return true;
9766 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9767 RESULT to make it match the type of the first pointer argument in
9768 PARAMS. */
9770 static tree
9771 sync_resolve_return (tree first_param, tree result, bool orig_format)
9773 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9774 tree rtype = TREE_TYPE (result);
9775 ptype = TYPE_MAIN_VARIANT (ptype);
9777 /* New format doesn't require casting unless the types are the same size. */
9778 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
9779 return convert (ptype, result);
9780 else
9781 return result;
9784 /* This function verifies the PARAMS to generic atomic FUNCTION.
9785 It returns the size if all the parameters are the same size, otherwise
9786 0 is returned if the parameters are invalid. */
9788 static int
9789 get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params)
9791 unsigned int n_param;
9792 unsigned int n_model;
9793 unsigned int x;
9794 int size_0;
9795 tree type_0;
9797 /* Determine the parameter makeup. */
9798 switch (DECL_FUNCTION_CODE (function))
9800 case BUILT_IN_ATOMIC_EXCHANGE:
9801 n_param = 4;
9802 n_model = 1;
9803 break;
9804 case BUILT_IN_ATOMIC_LOAD:
9805 case BUILT_IN_ATOMIC_STORE:
9806 n_param = 3;
9807 n_model = 1;
9808 break;
9809 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
9810 n_param = 6;
9811 n_model = 2;
9812 break;
9813 default:
9814 gcc_unreachable ();
9817 if (VEC_length (tree, params) != n_param)
9819 error_at (loc, "incorrect number of arguments to function %qE", function);
9820 return 0;
9823 /* Get type of first parameter, and determine its size. */
9824 type_0 = TREE_TYPE (VEC_index (tree, params, 0));
9825 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
9827 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
9828 function);
9829 return 0;
9832 /* Types must be compile time constant sizes. */
9833 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
9835 error_at (loc,
9836 "argument 1 of %qE must be a pointer to a constant size type",
9837 function);
9838 return 0;
9841 size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1);
9843 /* Zero size objects are not allowed. */
9844 if (size_0 == 0)
9846 error_at (loc,
9847 "argument 1 of %qE must be a pointer to a nonzero size object",
9848 function);
9849 return 0;
9852 /* Check each other parameter is a pointer and the same size. */
9853 for (x = 0; x < n_param - n_model; x++)
9855 int size;
9856 tree type = TREE_TYPE (VEC_index (tree, params, x));
9857 /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */
9858 if (n_param == 6 && x == 3)
9859 continue;
9860 if (!POINTER_TYPE_P (type))
9862 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
9863 function);
9864 return 0;
9866 size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1);
9867 if (size != size_0)
9869 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
9870 function);
9871 return 0;
9875 /* Check memory model parameters for validity. */
9876 for (x = n_param - n_model ; x < n_param; x++)
9878 tree p = VEC_index (tree, params, x);
9879 if (TREE_CODE (p) == INTEGER_CST)
9881 int i = tree_low_cst (p, 1);
9882 if (i < 0 || i >= MEMMODEL_LAST)
9884 warning_at (loc, OPT_Winvalid_memory_model,
9885 "invalid memory model argument %d of %qE", x + 1,
9886 function);
9889 else
9890 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
9892 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
9893 function);
9894 return 0;
9898 return size_0;
9902 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
9903 at the beginning of the parameter list PARAMS representing the size of the
9904 objects. This is to match the library ABI requirement. LOC is the location
9905 of the function call.
9906 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
9907 returned to allow the external call to be constructed. */
9909 static tree
9910 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9911 VEC(tree,gc) *params)
9913 tree size_node;
9915 /* Insert a SIZE_T parameter as the first param. If there isn't
9916 enough space, allocate a new vector and recursively re-build with that. */
9917 if (!VEC_space (tree, params, 1))
9919 unsigned int z, len;
9920 VEC(tree,gc) *vec;
9921 tree f;
9923 len = VEC_length (tree, params);
9924 vec = VEC_alloc (tree, gc, len + 1);
9925 for (z = 0; z < len; z++)
9926 VEC_quick_push (tree, vec, VEC_index (tree, params, z));
9927 f = build_function_call_vec (loc, function, vec, NULL);
9928 VEC_free (tree, gc, vec);
9929 return f;
9932 /* Add the size parameter and leave as a function call for processing. */
9933 size_node = build_int_cst (size_type_node, n);
9934 VEC_quick_insert (tree, params, 0, size_node);
9935 return NULL_TREE;
9939 /* This will process an __atomic_exchange function call, determine whether it
9940 needs to be mapped to the _N variation, or turned into a library call.
9941 LOC is the location of the builtin call.
9942 FUNCTION is the DECL that has been invoked;
9943 PARAMS is the argument list for the call. The return value is non-null
9944 TRUE is returned if it is translated into the proper format for a call to the
9945 external library, and NEW_RETURN is set the tree for that function.
9946 FALSE is returned if processing for the _N variation is required, and
9947 NEW_RETURN is set to the the return value the result is copied into. */
9948 static bool
9949 resolve_overloaded_atomic_exchange (location_t loc, tree function,
9950 VEC(tree,gc) *params, tree *new_return)
9952 tree p0, p1, p2, p3;
9953 tree I_type, I_type_ptr;
9954 int n = get_atomic_generic_size (loc, function, params);
9956 /* Size of 0 is an error condition. */
9957 if (n == 0)
9959 *new_return = error_mark_node;
9960 return true;
9963 /* If not a lock-free size, change to the library generic format. */
9964 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
9966 *new_return = add_atomic_size_parameter (n, loc, function, params);
9967 return true;
9970 /* Otherwise there is a lockfree match, transform the call from:
9971 void fn(T* mem, T* desired, T* return, model)
9972 into
9973 *return = (T) (fn (In* mem, (In) *desired, model)) */
9975 p0 = VEC_index (tree, params, 0);
9976 p1 = VEC_index (tree, params, 1);
9977 p2 = VEC_index (tree, params, 2);
9978 p3 = VEC_index (tree, params, 3);
9980 /* Create pointer to appropriate size. */
9981 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
9982 I_type_ptr = build_pointer_type (I_type);
9984 /* Convert object pointer to required type. */
9985 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9986 VEC_replace (tree, params, 0, p0);
9987 /* Convert new value to required type, and dereference it. */
9988 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
9989 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9990 VEC_replace (tree, params, 1, p1);
9992 /* Move memory model to the 3rd position, and end param list. */
9993 VEC_replace (tree, params, 2, p3);
9994 VEC_truncate (tree, params, 3);
9996 /* Convert return pointer and dereference it for later assignment. */
9997 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
9999 return false;
10003 /* This will process an __atomic_compare_exchange function call, determine
10004 whether it needs to be mapped to the _N variation, or turned into a lib call.
10005 LOC is the location of the builtin call.
10006 FUNCTION is the DECL that has been invoked;
10007 PARAMS is the argument list for the call. The return value is non-null
10008 TRUE is returned if it is translated into the proper format for a call to the
10009 external library, and NEW_RETURN is set the tree for that function.
10010 FALSE is returned if processing for the _N variation is required. */
10012 static bool
10013 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10014 VEC(tree,gc) *params,
10015 tree *new_return)
10017 tree p0, p1, p2;
10018 tree I_type, I_type_ptr;
10019 int n = get_atomic_generic_size (loc, function, params);
10021 /* Size of 0 is an error condition. */
10022 if (n == 0)
10024 *new_return = error_mark_node;
10025 return true;
10028 /* If not a lock-free size, change to the library generic format. */
10029 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10031 /* The library generic format does not have the weak parameter, so
10032 remove it from the param list. Since a parameter has been removed,
10033 we can be sure that there is room for the SIZE_T parameter, meaning
10034 there will not be a recursive rebuilding of the parameter list, so
10035 there is no danger this will be done twice. */
10036 if (n > 0)
10038 VEC_replace (tree, params, 3, VEC_index (tree, params, 4));
10039 VEC_replace (tree, params, 4, VEC_index (tree, params, 5));
10040 VEC_truncate (tree, params, 5);
10042 *new_return = add_atomic_size_parameter (n, loc, function, params);
10043 return true;
10046 /* Otherwise, there is a match, so the call needs to be transformed from:
10047 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10048 into
10049 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10051 p0 = VEC_index (tree, params, 0);
10052 p1 = VEC_index (tree, params, 1);
10053 p2 = VEC_index (tree, params, 2);
10055 /* Create pointer to appropriate size. */
10056 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10057 I_type_ptr = build_pointer_type (I_type);
10059 /* Convert object pointer to required type. */
10060 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10061 VEC_replace (tree, params, 0, p0);
10063 /* Convert expected pointer to required type. */
10064 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10065 VEC_replace (tree, params, 1, p1);
10067 /* Convert desired value to required type, and dereference it. */
10068 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10069 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10070 VEC_replace (tree, params, 2, p2);
10072 /* The rest of the parameters are fine. NULL means no special return value
10073 processing.*/
10074 *new_return = NULL;
10075 return false;
10079 /* This will process an __atomic_load function call, determine whether it
10080 needs to be mapped to the _N variation, or turned into a library call.
10081 LOC is the location of the builtin call.
10082 FUNCTION is the DECL that has been invoked;
10083 PARAMS is the argument list for the call. The return value is non-null
10084 TRUE is returned if it is translated into the proper format for a call to the
10085 external library, and NEW_RETURN is set the tree for that function.
10086 FALSE is returned if processing for the _N variation is required, and
10087 NEW_RETURN is set to the the return value the result is copied into. */
10089 static bool
10090 resolve_overloaded_atomic_load (location_t loc, tree function,
10091 VEC(tree,gc) *params, tree *new_return)
10093 tree p0, p1, p2;
10094 tree I_type, I_type_ptr;
10095 int n = get_atomic_generic_size (loc, function, params);
10097 /* Size of 0 is an error condition. */
10098 if (n == 0)
10100 *new_return = error_mark_node;
10101 return true;
10104 /* If not a lock-free size, change to the library generic format. */
10105 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10107 *new_return = add_atomic_size_parameter (n, loc, function, params);
10108 return true;
10111 /* Otherwise, there is a match, so the call needs to be transformed from:
10112 void fn(T* mem, T* return, model)
10113 into
10114 *return = (T) (fn ((In *) mem, model)) */
10116 p0 = VEC_index (tree, params, 0);
10117 p1 = VEC_index (tree, params, 1);
10118 p2 = VEC_index (tree, params, 2);
10120 /* Create pointer to appropriate size. */
10121 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10122 I_type_ptr = build_pointer_type (I_type);
10124 /* Convert object pointer to required type. */
10125 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10126 VEC_replace (tree, params, 0, p0);
10128 /* Move memory model to the 2nd position, and end param list. */
10129 VEC_replace (tree, params, 1, p2);
10130 VEC_truncate (tree, params, 2);
10132 /* Convert return pointer and dereference it for later assignment. */
10133 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10135 return false;
10139 /* This will process an __atomic_store function call, determine whether it
10140 needs to be mapped to the _N variation, or turned into a library call.
10141 LOC is the location of the builtin call.
10142 FUNCTION is the DECL that has been invoked;
10143 PARAMS is the argument list for the call. The return value is non-null
10144 TRUE is returned if it is translated into the proper format for a call to the
10145 external library, and NEW_RETURN is set the tree for that function.
10146 FALSE is returned if processing for the _N variation is required, and
10147 NEW_RETURN is set to the the return value the result is copied into. */
10149 static bool
10150 resolve_overloaded_atomic_store (location_t loc, tree function,
10151 VEC(tree,gc) *params, tree *new_return)
10153 tree p0, p1;
10154 tree I_type, I_type_ptr;
10155 int n = get_atomic_generic_size (loc, function, params);
10157 /* Size of 0 is an error condition. */
10158 if (n == 0)
10160 *new_return = error_mark_node;
10161 return true;
10164 /* If not a lock-free size, change to the library generic format. */
10165 if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)
10167 *new_return = add_atomic_size_parameter (n, loc, function, params);
10168 return true;
10171 /* Otherwise, there is a match, so the call needs to be transformed from:
10172 void fn(T* mem, T* value, model)
10173 into
10174 fn ((In *) mem, (In) *value, model) */
10176 p0 = VEC_index (tree, params, 0);
10177 p1 = VEC_index (tree, params, 1);
10179 /* Create pointer to appropriate size. */
10180 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10181 I_type_ptr = build_pointer_type (I_type);
10183 /* Convert object pointer to required type. */
10184 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10185 VEC_replace (tree, params, 0, p0);
10187 /* Convert new value to required type, and dereference it. */
10188 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10189 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10190 VEC_replace (tree, params, 1, p1);
10192 /* The memory model is in the right spot already. Return is void. */
10193 *new_return = NULL_TREE;
10195 return false;
10199 /* Some builtin functions are placeholders for other expressions. This
10200 function should be called immediately after parsing the call expression
10201 before surrounding code has committed to the type of the expression.
10203 LOC is the location of the builtin call.
10205 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10206 PARAMS is the argument list for the call. The return value is non-null
10207 when expansion is complete, and null if normal processing should
10208 continue. */
10210 tree
10211 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
10213 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10214 bool orig_format = true;
10215 tree new_return = NULL_TREE;
10217 switch (DECL_BUILT_IN_CLASS (function))
10219 case BUILT_IN_NORMAL:
10220 break;
10221 case BUILT_IN_MD:
10222 if (targetm.resolve_overloaded_builtin)
10223 return targetm.resolve_overloaded_builtin (loc, function, params);
10224 else
10225 return NULL_TREE;
10226 default:
10227 return NULL_TREE;
10230 /* Handle BUILT_IN_NORMAL here. */
10231 switch (orig_code)
10233 case BUILT_IN_ATOMIC_EXCHANGE:
10234 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10235 case BUILT_IN_ATOMIC_LOAD:
10236 case BUILT_IN_ATOMIC_STORE:
10238 /* Handle these 4 together so that they can fall through to the next
10239 case if the call is transformed to an _N variant. */
10240 switch (orig_code)
10242 case BUILT_IN_ATOMIC_EXCHANGE:
10244 if (resolve_overloaded_atomic_exchange (loc, function, params,
10245 &new_return))
10246 return new_return;
10247 /* Change to the _N variant. */
10248 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10249 break;
10252 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10254 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10255 params,
10256 &new_return))
10257 return new_return;
10258 /* Change to the _N variant. */
10259 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10260 break;
10262 case BUILT_IN_ATOMIC_LOAD:
10264 if (resolve_overloaded_atomic_load (loc, function, params,
10265 &new_return))
10266 return new_return;
10267 /* Change to the _N variant. */
10268 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10269 break;
10271 case BUILT_IN_ATOMIC_STORE:
10273 if (resolve_overloaded_atomic_store (loc, function, params,
10274 &new_return))
10275 return new_return;
10276 /* Change to the _N variant. */
10277 orig_code = BUILT_IN_ATOMIC_STORE_N;
10278 break;
10280 default:
10281 gcc_unreachable ();
10283 /* Fallthrough to the normal processing. */
10285 case BUILT_IN_ATOMIC_EXCHANGE_N:
10286 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10287 case BUILT_IN_ATOMIC_LOAD_N:
10288 case BUILT_IN_ATOMIC_STORE_N:
10289 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10290 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10291 case BUILT_IN_ATOMIC_AND_FETCH_N:
10292 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10293 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10294 case BUILT_IN_ATOMIC_OR_FETCH_N:
10295 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10296 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10297 case BUILT_IN_ATOMIC_FETCH_AND_N:
10298 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10299 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10300 case BUILT_IN_ATOMIC_FETCH_OR_N:
10302 orig_format = false;
10303 /* Fallthru for parameter processing. */
10305 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10306 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10307 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10308 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10309 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10310 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10311 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10312 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10313 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10314 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10315 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10316 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10317 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10318 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10319 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10320 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10322 int n = sync_resolve_size (function, params);
10323 tree new_function, first_param, result;
10324 enum built_in_function fncode;
10326 if (n == 0)
10327 return error_mark_node;
10329 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10330 new_function = builtin_decl_explicit (fncode);
10331 if (!sync_resolve_params (loc, function, new_function, params,
10332 orig_format))
10333 return error_mark_node;
10335 first_param = VEC_index (tree, params, 0);
10336 result = build_function_call_vec (loc, new_function, params, NULL);
10337 if (result == error_mark_node)
10338 return result;
10339 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10340 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10341 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10342 result = sync_resolve_return (first_param, result, orig_format);
10344 /* If new_return is set, assign function to that expr and cast the
10345 result to void since the generic interface returned void. */
10346 if (new_return)
10348 /* Cast function result from I{1,2,4,8,16} to the required type. */
10349 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10350 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10351 result);
10352 TREE_SIDE_EFFECTS (result) = 1;
10353 protected_set_expr_location (result, loc);
10354 result = convert (void_type_node, result);
10356 return result;
10359 default:
10360 return NULL_TREE;
10364 /* Ignoring their sign, return true if two scalar types are the same. */
10365 bool
10366 same_scalar_type_ignoring_signedness (tree t1, tree t2)
10368 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10370 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10371 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10372 || c2 == FIXED_POINT_TYPE));
10374 /* Equality works here because c_common_signed_type uses
10375 TYPE_MAIN_VARIANT. */
10376 return c_common_signed_type (t1)
10377 == c_common_signed_type (t2);
10380 /* Check for missing format attributes on function pointers. LTYPE is
10381 the new type or left-hand side type. RTYPE is the old type or
10382 right-hand side type. Returns TRUE if LTYPE is missing the desired
10383 attribute. */
10385 bool
10386 check_missing_format_attribute (tree ltype, tree rtype)
10388 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10389 tree ra;
10391 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10392 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10393 break;
10394 if (ra)
10396 tree la;
10397 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10398 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10399 break;
10400 return !la;
10402 else
10403 return false;
10406 /* Subscripting with type char is likely to lose on a machine where
10407 chars are signed. So warn on any machine, but optionally. Don't
10408 warn for unsigned char since that type is safe. Don't warn for
10409 signed char because anyone who uses that must have done so
10410 deliberately. Furthermore, we reduce the false positive load by
10411 warning only for non-constant value of type char. */
10413 void
10414 warn_array_subscript_with_type_char (tree index)
10416 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10417 && TREE_CODE (index) != INTEGER_CST)
10418 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
10421 /* Implement -Wparentheses for the unexpected C precedence rules, to
10422 cover cases like x + y << z which readers are likely to
10423 misinterpret. We have seen an expression in which CODE is a binary
10424 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
10425 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
10426 CODE_RIGHT may be ERROR_MARK, which means that that side of the
10427 expression was not formed using a binary or unary operator, or it
10428 was enclosed in parentheses. */
10430 void
10431 warn_about_parentheses (enum tree_code code,
10432 enum tree_code code_left, tree arg_left,
10433 enum tree_code code_right, tree arg_right)
10435 if (!warn_parentheses)
10436 return;
10438 /* This macro tests that the expression ARG with original tree code
10439 CODE appears to be a boolean expression. or the result of folding a
10440 boolean expression. */
10441 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
10442 (truth_value_p (TREE_CODE (ARG)) \
10443 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
10444 /* Folding may create 0 or 1 integers from other expressions. */ \
10445 || ((CODE) != INTEGER_CST \
10446 && (integer_onep (ARG) || integer_zerop (ARG))))
10448 switch (code)
10450 case LSHIFT_EXPR:
10451 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10452 warning (OPT_Wparentheses,
10453 "suggest parentheses around %<+%> inside %<<<%>");
10454 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10455 warning (OPT_Wparentheses,
10456 "suggest parentheses around %<-%> inside %<<<%>");
10457 return;
10459 case RSHIFT_EXPR:
10460 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10461 warning (OPT_Wparentheses,
10462 "suggest parentheses around %<+%> inside %<>>%>");
10463 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10464 warning (OPT_Wparentheses,
10465 "suggest parentheses around %<-%> inside %<>>%>");
10466 return;
10468 case TRUTH_ORIF_EXPR:
10469 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
10470 warning (OPT_Wparentheses,
10471 "suggest parentheses around %<&&%> within %<||%>");
10472 return;
10474 case BIT_IOR_EXPR:
10475 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
10476 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10477 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
10478 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10479 warning (OPT_Wparentheses,
10480 "suggest parentheses around arithmetic in operand of %<|%>");
10481 /* Check cases like x|y==z */
10482 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10483 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10484 warning (OPT_Wparentheses,
10485 "suggest parentheses around comparison in operand of %<|%>");
10486 /* Check cases like !x | y */
10487 else if (code_left == TRUTH_NOT_EXPR
10488 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10489 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10490 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
10491 return;
10493 case BIT_XOR_EXPR:
10494 if (code_left == BIT_AND_EXPR
10495 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
10496 || code_right == BIT_AND_EXPR
10497 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
10498 warning (OPT_Wparentheses,
10499 "suggest parentheses around arithmetic in operand of %<^%>");
10500 /* Check cases like x^y==z */
10501 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10502 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10503 warning (OPT_Wparentheses,
10504 "suggest parentheses around comparison in operand of %<^%>");
10505 return;
10507 case BIT_AND_EXPR:
10508 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
10509 warning (OPT_Wparentheses,
10510 "suggest parentheses around %<+%> in operand of %<&%>");
10511 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
10512 warning (OPT_Wparentheses,
10513 "suggest parentheses around %<-%> in operand of %<&%>");
10514 /* Check cases like x&y==z */
10515 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
10516 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10517 warning (OPT_Wparentheses,
10518 "suggest parentheses around comparison in operand of %<&%>");
10519 /* Check cases like !x & y */
10520 else if (code_left == TRUTH_NOT_EXPR
10521 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
10522 warning (OPT_Wparentheses, "suggest parentheses around operand of "
10523 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
10524 return;
10526 case EQ_EXPR:
10527 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10528 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10529 warning (OPT_Wparentheses,
10530 "suggest parentheses around comparison in operand of %<==%>");
10531 return;
10532 case NE_EXPR:
10533 if (TREE_CODE_CLASS (code_left) == tcc_comparison
10534 || TREE_CODE_CLASS (code_right) == tcc_comparison)
10535 warning (OPT_Wparentheses,
10536 "suggest parentheses around comparison in operand of %<!=%>");
10537 return;
10539 default:
10540 if (TREE_CODE_CLASS (code) == tcc_comparison
10541 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
10542 && code_left != NE_EXPR && code_left != EQ_EXPR
10543 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
10544 || (TREE_CODE_CLASS (code_right) == tcc_comparison
10545 && code_right != NE_EXPR && code_right != EQ_EXPR
10546 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
10547 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
10548 "have their mathematical meaning");
10549 return;
10551 #undef NOT_A_BOOLEAN_EXPR_P
10554 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
10556 void
10557 warn_for_unused_label (tree label)
10559 if (!TREE_USED (label))
10561 if (DECL_INITIAL (label))
10562 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
10563 else
10564 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
10568 /* Warn for division by zero according to the value of DIVISOR. LOC
10569 is the location of the division operator. */
10571 void
10572 warn_for_div_by_zero (location_t loc, tree divisor)
10574 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
10575 about division by zero. Do not issue a warning if DIVISOR has a
10576 floating-point type, since we consider 0.0/0.0 a valid way of
10577 generating a NaN. */
10578 if (c_inhibit_evaluation_warnings == 0
10579 && (integer_zerop (divisor) || fixed_zerop (divisor)))
10580 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
10583 /* Subroutine of build_binary_op. Give warnings for comparisons
10584 between signed and unsigned quantities that may fail. Do the
10585 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
10586 so that casts will be considered, but default promotions won't
10589 LOCATION is the location of the comparison operator.
10591 The arguments of this function map directly to local variables
10592 of build_binary_op. */
10594 void
10595 warn_for_sign_compare (location_t location,
10596 tree orig_op0, tree orig_op1,
10597 tree op0, tree op1,
10598 tree result_type, enum tree_code resultcode)
10600 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
10601 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
10602 int unsignedp0, unsignedp1;
10604 /* In C++, check for comparison of different enum types. */
10605 if (c_dialect_cxx()
10606 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
10607 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
10608 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
10609 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
10611 warning_at (location,
10612 OPT_Wsign_compare, "comparison between types %qT and %qT",
10613 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
10616 /* Do not warn if the comparison is being done in a signed type,
10617 since the signed type will only be chosen if it can represent
10618 all the values of the unsigned type. */
10619 if (!TYPE_UNSIGNED (result_type))
10620 /* OK */;
10621 /* Do not warn if both operands are unsigned. */
10622 else if (op0_signed == op1_signed)
10623 /* OK */;
10624 else
10626 tree sop, uop, base_type;
10627 bool ovf;
10629 if (op0_signed)
10630 sop = orig_op0, uop = orig_op1;
10631 else
10632 sop = orig_op1, uop = orig_op0;
10634 STRIP_TYPE_NOPS (sop);
10635 STRIP_TYPE_NOPS (uop);
10636 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
10637 ? TREE_TYPE (result_type) : result_type);
10639 /* Do not warn if the signed quantity is an unsuffixed integer
10640 literal (or some static constant expression involving such
10641 literals or a conditional expression involving such literals)
10642 and it is non-negative. */
10643 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
10644 /* OK */;
10645 /* Do not warn if the comparison is an equality operation, the
10646 unsigned quantity is an integral constant, and it would fit
10647 in the result if the result were signed. */
10648 else if (TREE_CODE (uop) == INTEGER_CST
10649 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
10650 && int_fits_type_p (uop, c_common_signed_type (base_type)))
10651 /* OK */;
10652 /* In C, do not warn if the unsigned quantity is an enumeration
10653 constant and its maximum value would fit in the result if the
10654 result were signed. */
10655 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
10656 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
10657 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
10658 c_common_signed_type (base_type)))
10659 /* OK */;
10660 else
10661 warning_at (location,
10662 OPT_Wsign_compare,
10663 "comparison between signed and unsigned integer expressions");
10666 /* Warn if two unsigned values are being compared in a size larger
10667 than their original size, and one (and only one) is the result of
10668 a `~' operator. This comparison will always fail.
10670 Also warn if one operand is a constant, and the constant does not
10671 have all bits set that are set in the ~ operand when it is
10672 extended. */
10674 op0 = c_common_get_narrower (op0, &unsignedp0);
10675 op1 = c_common_get_narrower (op1, &unsignedp1);
10677 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
10678 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
10680 if (TREE_CODE (op0) == BIT_NOT_EXPR)
10681 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
10682 if (TREE_CODE (op1) == BIT_NOT_EXPR)
10683 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
10685 if (host_integerp (op0, 0) || host_integerp (op1, 0))
10687 tree primop;
10688 HOST_WIDE_INT constant, mask;
10689 int unsignedp;
10690 unsigned int bits;
10692 if (host_integerp (op0, 0))
10694 primop = op1;
10695 unsignedp = unsignedp1;
10696 constant = tree_low_cst (op0, 0);
10698 else
10700 primop = op0;
10701 unsignedp = unsignedp0;
10702 constant = tree_low_cst (op1, 0);
10705 bits = TYPE_PRECISION (TREE_TYPE (primop));
10706 if (bits < TYPE_PRECISION (result_type)
10707 && bits < HOST_BITS_PER_LONG && unsignedp)
10709 mask = (~ (HOST_WIDE_INT) 0) << bits;
10710 if ((mask & constant) != mask)
10712 if (constant == 0)
10713 warning (OPT_Wsign_compare,
10714 "promoted ~unsigned is always non-zero");
10715 else
10716 warning_at (location, OPT_Wsign_compare,
10717 "comparison of promoted ~unsigned with constant");
10721 else if (unsignedp0 && unsignedp1
10722 && (TYPE_PRECISION (TREE_TYPE (op0))
10723 < TYPE_PRECISION (result_type))
10724 && (TYPE_PRECISION (TREE_TYPE (op1))
10725 < TYPE_PRECISION (result_type)))
10726 warning_at (location, OPT_Wsign_compare,
10727 "comparison of promoted ~unsigned with unsigned");
10731 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
10732 type via c_common_type. If -Wdouble-promotion is in use, and the
10733 conditions for warning have been met, issue a warning. GMSGID is
10734 the warning message. It must have two %T specifiers for the type
10735 that was converted (generally "float") and the type to which it was
10736 converted (generally "double), respectively. LOC is the location
10737 to which the awrning should refer. */
10739 void
10740 do_warn_double_promotion (tree result_type, tree type1, tree type2,
10741 const char *gmsgid, location_t loc)
10743 tree source_type;
10745 if (!warn_double_promotion)
10746 return;
10747 /* If the conversion will not occur at run-time, there is no need to
10748 warn about it. */
10749 if (c_inhibit_evaluation_warnings)
10750 return;
10751 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
10752 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
10753 return;
10754 if (TYPE_MAIN_VARIANT (type1) == float_type_node
10755 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
10756 source_type = type1;
10757 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
10758 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
10759 source_type = type2;
10760 else
10761 return;
10762 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
10765 /* Setup a TYPE_DECL node as a typedef representation.
10767 X is a TYPE_DECL for a typedef statement. Create a brand new
10768 ..._TYPE node (which will be just a variant of the existing
10769 ..._TYPE node with identical properties) and then install X
10770 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
10772 The whole point here is to end up with a situation where each
10773 and every ..._TYPE node the compiler creates will be uniquely
10774 associated with AT MOST one node representing a typedef name.
10775 This way, even though the compiler substitutes corresponding
10776 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
10777 early on, later parts of the compiler can always do the reverse
10778 translation and get back the corresponding typedef name. For
10779 example, given:
10781 typedef struct S MY_TYPE;
10782 MY_TYPE object;
10784 Later parts of the compiler might only know that `object' was of
10785 type `struct S' if it were not for code just below. With this
10786 code however, later parts of the compiler see something like:
10788 struct S' == struct S
10789 typedef struct S' MY_TYPE;
10790 struct S' object;
10792 And they can then deduce (from the node for type struct S') that
10793 the original object declaration was:
10795 MY_TYPE object;
10797 Being able to do this is important for proper support of protoize,
10798 and also for generating precise symbolic debugging information
10799 which takes full account of the programmer's (typedef) vocabulary.
10801 Obviously, we don't want to generate a duplicate ..._TYPE node if
10802 the TYPE_DECL node that we are now processing really represents a
10803 standard built-in type. */
10805 void
10806 set_underlying_type (tree x)
10808 if (x == error_mark_node)
10809 return;
10810 if (DECL_IS_BUILTIN (x))
10812 if (TYPE_NAME (TREE_TYPE (x)) == 0)
10813 TYPE_NAME (TREE_TYPE (x)) = x;
10815 else if (TREE_TYPE (x) != error_mark_node
10816 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
10818 tree tt = TREE_TYPE (x);
10819 DECL_ORIGINAL_TYPE (x) = tt;
10820 tt = build_variant_type_copy (tt);
10821 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
10822 TYPE_NAME (tt) = x;
10823 TREE_USED (tt) = TREE_USED (x);
10824 TREE_TYPE (x) = tt;
10828 /* Record the types used by the current global variable declaration
10829 being parsed, so that we can decide later to emit their debug info.
10830 Those types are in types_used_by_cur_var_decl, and we are going to
10831 store them in the types_used_by_vars_hash hash table.
10832 DECL is the declaration of the global variable that has been parsed. */
10834 void
10835 record_types_used_by_current_var_decl (tree decl)
10837 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
10839 while (!VEC_empty (tree, types_used_by_cur_var_decl))
10841 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
10842 types_used_by_var_decl_insert (type, decl);
10846 /* If DECL is a typedef that is declared in the current function,
10847 record it for the purpose of -Wunused-local-typedefs. */
10849 void
10850 record_locally_defined_typedef (tree decl)
10852 struct c_language_function *l;
10854 if (!warn_unused_local_typedefs
10855 || cfun == NULL
10856 /* if this is not a locally defined typedef then we are not
10857 interested. */
10858 || !is_typedef_decl (decl)
10859 || !decl_function_context (decl))
10860 return;
10862 l = (struct c_language_function *) cfun->language;
10863 VEC_safe_push (tree, gc, l->local_typedefs, decl);
10866 /* If T is a TYPE_DECL declared locally, mark it as used. */
10868 void
10869 maybe_record_typedef_use (tree t)
10871 if (!is_typedef_decl (t))
10872 return;
10874 TREE_USED (t) = true;
10877 /* Warn if there are some unused locally defined typedefs in the
10878 current function. */
10880 void
10881 maybe_warn_unused_local_typedefs (void)
10883 int i;
10884 tree decl;
10885 /* The number of times we have emitted -Wunused-local-typedefs
10886 warnings. If this is different from errorcount, that means some
10887 unrelated errors have been issued. In which case, we'll avoid
10888 emitting "unused-local-typedefs" warnings. */
10889 static int unused_local_typedefs_warn_count;
10890 struct c_language_function *l;
10892 if (cfun == NULL)
10893 return;
10895 if ((l = (struct c_language_function *) cfun->language) == NULL)
10896 return;
10898 if (warn_unused_local_typedefs
10899 && errorcount == unused_local_typedefs_warn_count)
10901 FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl)
10902 if (!TREE_USED (decl))
10903 warning_at (DECL_SOURCE_LOCATION (decl),
10904 OPT_Wunused_local_typedefs,
10905 "typedef %qD locally defined but not used", decl);
10906 unused_local_typedefs_warn_count = errorcount;
10909 if (l->local_typedefs)
10911 VEC_free (tree, gc, l->local_typedefs);
10912 l->local_typedefs = NULL;
10916 /* The C and C++ parsers both use vectors to hold function arguments.
10917 For efficiency, we keep a cache of unused vectors. This is the
10918 cache. */
10920 typedef VEC(tree,gc)* tree_gc_vec;
10921 DEF_VEC_P(tree_gc_vec);
10922 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
10923 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
10925 /* Return a new vector from the cache. If the cache is empty,
10926 allocate a new vector. These vectors are GC'ed, so it is OK if the
10927 pointer is not released.. */
10929 VEC(tree,gc) *
10930 make_tree_vector (void)
10932 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
10933 return VEC_pop (tree_gc_vec, tree_vector_cache);
10934 else
10936 /* Passing 0 to VEC_alloc returns NULL, and our callers require
10937 that we always return a non-NULL value. The vector code uses
10938 4 when growing a NULL vector, so we do too. */
10939 return VEC_alloc (tree, gc, 4);
10943 /* Release a vector of trees back to the cache. */
10945 void
10946 release_tree_vector (VEC(tree,gc) *vec)
10948 if (vec != NULL)
10950 VEC_truncate (tree, vec, 0);
10951 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
10955 /* Get a new tree vector holding a single tree. */
10957 VEC(tree,gc) *
10958 make_tree_vector_single (tree t)
10960 VEC(tree,gc) *ret = make_tree_vector ();
10961 VEC_quick_push (tree, ret, t);
10962 return ret;
10965 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
10967 VEC(tree,gc) *
10968 make_tree_vector_from_list (tree list)
10970 VEC(tree,gc) *ret = make_tree_vector ();
10971 for (; list; list = TREE_CHAIN (list))
10972 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
10973 return ret;
10976 /* Get a new tree vector which is a copy of an existing one. */
10978 VEC(tree,gc) *
10979 make_tree_vector_copy (const VEC(tree,gc) *orig)
10981 VEC(tree,gc) *ret;
10982 unsigned int ix;
10983 tree t;
10985 ret = make_tree_vector ();
10986 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
10987 FOR_EACH_VEC_ELT (tree, orig, ix, t)
10988 VEC_quick_push (tree, ret, t);
10989 return ret;
10992 /* Return true if KEYWORD starts a type specifier. */
10994 bool
10995 keyword_begins_type_specifier (enum rid keyword)
10997 switch (keyword)
10999 case RID_INT:
11000 case RID_CHAR:
11001 case RID_FLOAT:
11002 case RID_DOUBLE:
11003 case RID_VOID:
11004 case RID_INT128:
11005 case RID_UNSIGNED:
11006 case RID_LONG:
11007 case RID_SHORT:
11008 case RID_SIGNED:
11009 case RID_DFLOAT32:
11010 case RID_DFLOAT64:
11011 case RID_DFLOAT128:
11012 case RID_FRACT:
11013 case RID_ACCUM:
11014 case RID_BOOL:
11015 case RID_WCHAR:
11016 case RID_CHAR16:
11017 case RID_CHAR32:
11018 case RID_SAT:
11019 case RID_COMPLEX:
11020 case RID_TYPEOF:
11021 case RID_STRUCT:
11022 case RID_CLASS:
11023 case RID_UNION:
11024 case RID_ENUM:
11025 return true;
11026 default:
11027 return false;
11031 /* Return true if KEYWORD names a type qualifier. */
11033 bool
11034 keyword_is_type_qualifier (enum rid keyword)
11036 switch (keyword)
11038 case RID_CONST:
11039 case RID_VOLATILE:
11040 case RID_RESTRICT:
11041 return true;
11042 default:
11043 return false;
11047 /* Return true if KEYWORD names a storage class specifier.
11049 RID_TYPEDEF is not included in this list despite `typedef' being
11050 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11051 such for syntactic convenience only. */
11053 bool
11054 keyword_is_storage_class_specifier (enum rid keyword)
11056 switch (keyword)
11058 case RID_STATIC:
11059 case RID_EXTERN:
11060 case RID_REGISTER:
11061 case RID_AUTO:
11062 case RID_MUTABLE:
11063 case RID_THREAD:
11064 return true;
11065 default:
11066 return false;
11070 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11072 static bool
11073 keyword_is_function_specifier (enum rid keyword)
11075 switch (keyword)
11077 case RID_INLINE:
11078 case RID_NORETURN:
11079 case RID_VIRTUAL:
11080 case RID_EXPLICIT:
11081 return true;
11082 default:
11083 return false;
11087 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11088 declaration-specifier (C99 6.7). */
11090 bool
11091 keyword_is_decl_specifier (enum rid keyword)
11093 if (keyword_is_storage_class_specifier (keyword)
11094 || keyword_is_type_qualifier (keyword)
11095 || keyword_is_function_specifier (keyword))
11096 return true;
11098 switch (keyword)
11100 case RID_TYPEDEF:
11101 case RID_FRIEND:
11102 case RID_CONSTEXPR:
11103 return true;
11104 default:
11105 return false;
11109 /* Initialize language-specific-bits of tree_contains_struct. */
11111 void
11112 c_common_init_ts (void)
11114 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11115 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11118 /* Build a user-defined numeric literal out of an integer constant type VALUE
11119 with identifier SUFFIX. */
11121 tree
11122 build_userdef_literal (tree suffix_id, tree value, tree num_string)
11124 tree literal = make_node (USERDEF_LITERAL);
11125 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11126 USERDEF_LITERAL_VALUE (literal) = value;
11127 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11128 return literal;
11131 /* For vector[index], convert the vector to a
11132 pointer of the underlying type. */
11133 void
11134 convert_vector_to_pointer_for_subscript (location_t loc,
11135 tree* vecp, tree index)
11137 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11139 tree type = TREE_TYPE (*vecp);
11140 tree type1;
11142 if (TREE_CODE (index) == INTEGER_CST)
11143 if (!host_integerp (index, 1)
11144 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
11145 >= TYPE_VECTOR_SUBPARTS (type)))
11146 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11148 c_common_mark_addressable_vec (*vecp);
11149 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11150 type = build_pointer_type (type);
11151 type1 = build_pointer_type (TREE_TYPE (*vecp));
11152 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11153 *vecp = convert (type, *vecp);
11157 #include "gt-c-family-c-common.h"