Revert wrong checkin
[official-gcc.git] / gcc / c-family / c-common.c
blobdbef4b314eab6045fd700dd631edc98bd0b0d5f1
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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "intl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "c-common.h"
33 #include "c-objc.h"
34 #include "tm_p.h"
35 #include "obstack.h"
36 #include "cpplib.h"
37 #include "target.h"
38 #include "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"
48 #include "libfuncs.h"
50 cpp_reader *parse_in; /* Declared in c-pragma.h. */
52 /* The following symbols are subsumed in the c_global_trees array, and
53 listed here individually for documentation purposes.
55 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
57 tree short_integer_type_node;
58 tree long_integer_type_node;
59 tree long_long_integer_type_node;
60 tree int128_integer_type_node;
62 tree short_unsigned_type_node;
63 tree long_unsigned_type_node;
64 tree long_long_unsigned_type_node;
65 tree int128_unsigned_type_node;
67 tree truthvalue_type_node;
68 tree truthvalue_false_node;
69 tree truthvalue_true_node;
71 tree ptrdiff_type_node;
73 tree unsigned_char_type_node;
74 tree signed_char_type_node;
75 tree wchar_type_node;
77 tree char16_type_node;
78 tree char32_type_node;
80 tree float_type_node;
81 tree double_type_node;
82 tree long_double_type_node;
84 tree complex_integer_type_node;
85 tree complex_float_type_node;
86 tree complex_double_type_node;
87 tree complex_long_double_type_node;
89 tree dfloat32_type_node;
90 tree dfloat64_type_node;
91 tree_dfloat128_type_node;
93 tree intQI_type_node;
94 tree intHI_type_node;
95 tree intSI_type_node;
96 tree intDI_type_node;
97 tree intTI_type_node;
99 tree unsigned_intQI_type_node;
100 tree unsigned_intHI_type_node;
101 tree unsigned_intSI_type_node;
102 tree unsigned_intDI_type_node;
103 tree unsigned_intTI_type_node;
105 tree widest_integer_literal_type_node;
106 tree widest_unsigned_literal_type_node;
108 Nodes for types `void *' and `const void *'.
110 tree ptr_type_node, const_ptr_type_node;
112 Nodes for types `char *' and `const char *'.
114 tree string_type_node, const_string_type_node;
116 Type `char[SOMENUMBER]'.
117 Used when an array of char is needed and the size is irrelevant.
119 tree char_array_type_node;
121 Type `int[SOMENUMBER]' or something like it.
122 Used when an array of int needed and the size is irrelevant.
124 tree int_array_type_node;
126 Type `wchar_t[SOMENUMBER]' or something like it.
127 Used when a wide string literal is created.
129 tree wchar_array_type_node;
131 Type `char16_t[SOMENUMBER]' or something like it.
132 Used when a UTF-16 string literal is created.
134 tree char16_array_type_node;
136 Type `char32_t[SOMENUMBER]' or something like it.
137 Used when a UTF-32 string literal is created.
139 tree char32_array_type_node;
141 Type `int ()' -- used for implicit declaration of functions.
143 tree default_function_type;
145 A VOID_TYPE node, packaged in a TREE_LIST.
147 tree void_list_node;
149 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
150 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
151 VAR_DECLS, but C++ does.)
153 tree function_name_decl_node;
154 tree pretty_function_name_decl_node;
155 tree c99_function_name_decl_node;
157 Stack of nested function name VAR_DECLs.
159 tree saved_function_name_decls;
163 tree c_global_trees[CTI_MAX];
165 /* Switches common to the C front ends. */
167 /* Nonzero means don't output line number information. */
169 char flag_no_line_commands;
171 /* Nonzero causes -E output not to be done, but directives such as
172 #define that have side effects are still obeyed. */
174 char flag_no_output;
176 /* Nonzero means dump macros in some fashion. */
178 char flag_dump_macros;
180 /* Nonzero means pass #include lines through to the output. */
182 char flag_dump_includes;
184 /* Nonzero means process PCH files while preprocessing. */
186 bool flag_pch_preprocess;
188 /* The file name to which we should write a precompiled header, or
189 NULL if no header will be written in this compile. */
191 const char *pch_file;
193 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 user's namespace. */
195 int flag_iso;
197 /* Warn about #pragma directives that are not recognized. */
199 int warn_unknown_pragmas; /* Tri state variable. */
201 /* Warn about format/argument anomalies in calls to formatted I/O functions
202 (*printf, *scanf, strftime, strfmon, etc.). */
204 int warn_format;
206 /* C/ObjC language option variables. */
209 /* Nonzero means allow type mismatches in conditional expressions;
210 just make their values `void'. */
212 int flag_cond_mismatch;
214 /* Nonzero means enable C89 Amendment 1 features. */
216 int flag_isoc94;
218 /* Nonzero means use the ISO C99 (or C1X) dialect of C. */
220 int flag_isoc99;
222 /* Nonzero means use the ISO C1X dialect of C. */
224 int flag_isoc1x;
226 /* Nonzero means that we have builtin functions, and main is an int. */
228 int flag_hosted = 1;
231 /* ObjC language option variables. */
234 /* Tells the compiler that this is a special run. Do not perform any
235 compiling, instead we are to test some platform dependent features
236 and output a C header file with appropriate definitions. */
238 int print_struct_values;
240 /* Tells the compiler what is the constant string class for ObjC. */
242 const char *constant_string_class_name;
245 /* C++ language option variables. */
248 /* Nonzero means generate separate instantiation control files and
249 juggle them at link time. */
251 int flag_use_repository;
253 /* The C++ dialect being used. C++98 is the default. */
255 enum cxx_dialect cxx_dialect = cxx98;
257 /* Maximum template instantiation depth. This limit exists to limit the
258 time it takes to notice excessively recursive template instantiations;
259 the default value of 1024 is likely to be in the next C++ standard. */
261 int max_tinst_depth = 1024;
263 /* The elements of `ridpointers' are identifier nodes for the reserved
264 type names and storage classes. It is indexed by a RID_... value. */
265 tree *ridpointers;
267 tree (*make_fname_decl) (location_t, tree, int);
269 /* Nonzero means don't warn about problems that occur when the code is
270 executed. */
271 int c_inhibit_evaluation_warnings;
273 /* Whether we are building a boolean conversion inside
274 convert_for_assignment, or some other late binary operation. If
275 build_binary_op is called for C (from code shared by C and C++) in
276 this case, then the operands have already been folded and the
277 result will not be folded again, so C_MAYBE_CONST_EXPR should not
278 be generated. */
279 bool in_late_binary_op;
281 /* Whether lexing has been completed, so subsequent preprocessor
282 errors should use the compiler's input_location. */
283 bool done_lexing = false;
285 /* Information about how a function name is generated. */
286 struct fname_var_t
288 tree *const decl; /* pointer to the VAR_DECL. */
289 const unsigned rid; /* RID number for the identifier. */
290 const int pretty; /* How pretty is it? */
293 /* The three ways of getting then name of the current function. */
295 const struct fname_var_t fname_vars[] =
297 /* C99 compliant __func__, must be first. */
298 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
299 /* GCC __FUNCTION__ compliant. */
300 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
301 /* GCC __PRETTY_FUNCTION__ compliant. */
302 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
303 {NULL, 0, 0},
306 /* Global visibility options. */
307 struct visibility_flags visibility_options;
309 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
310 static tree check_case_value (tree);
311 static bool check_case_bounds (tree, tree, tree *, tree *);
313 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
314 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_always_inline_attribute (tree *, tree, tree, int,
323 bool *);
324 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
331 bool *);
332 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_transparent_union_attribute (tree *, tree, tree,
334 int, bool *);
335 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
341 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
342 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
345 static tree handle_visibility_attribute (tree *, tree, tree, int,
346 bool *);
347 static tree handle_tls_model_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_no_instrument_function_attribute (tree *, tree,
350 tree, int, bool *);
351 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
354 bool *);
355 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_deprecated_attribute (tree *, tree, tree, int,
358 bool *);
359 static tree handle_vector_size_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
374 static void check_function_nonnull (tree, int, tree *);
375 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
376 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
377 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
378 static int resort_field_decl_cmp (const void *, const void *);
380 /* Reserved words. The third field is a mask: keywords are disabled
381 if they match the mask.
383 Masks for languages:
384 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
385 C --std=c99: D_CXXONLY | D_OBJC
386 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
387 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
388 C++ --std=c0x: D_CONLY | D_OBJC
389 ObjC++ is like C++ except that D_OBJC is not set
391 If -fno-asm is used, D_ASM is added to the mask. If
392 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
393 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
394 In C with -Wc++-compat, we warn if D_CXXWARN is set.
396 Note the complication of the D_CXX_OBJC keywords. These are
397 reserved words such as 'class'. In C++, 'class' is a reserved
398 word. In Objective-C++ it is too. In Objective-C, it is a
399 reserved word too, but only if it follows an '@' sign.
401 const struct c_common_resword c_common_reswords[] =
403 { "_Bool", RID_BOOL, D_CONLY },
404 { "_Complex", RID_COMPLEX, 0 },
405 { "_Imaginary", RID_IMAGINARY, D_CONLY },
406 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
407 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
408 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
409 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
410 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
411 { "_Sat", RID_SAT, D_CONLY | D_EXT },
412 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
413 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
414 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
415 { "__alignof", RID_ALIGNOF, 0 },
416 { "__alignof__", RID_ALIGNOF, 0 },
417 { "__asm", RID_ASM, 0 },
418 { "__asm__", RID_ASM, 0 },
419 { "__attribute", RID_ATTRIBUTE, 0 },
420 { "__attribute__", RID_ATTRIBUTE, 0 },
421 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
422 { "__builtin_offsetof", RID_OFFSETOF, 0 },
423 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
424 { "__builtin_va_arg", RID_VA_ARG, 0 },
425 { "__complex", RID_COMPLEX, 0 },
426 { "__complex__", RID_COMPLEX, 0 },
427 { "__const", RID_CONST, 0 },
428 { "__const__", RID_CONST, 0 },
429 { "__decltype", RID_DECLTYPE, D_CXXONLY },
430 { "__extension__", RID_EXTENSION, 0 },
431 { "__func__", RID_C99_FUNCTION_NAME, 0 },
432 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
433 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
434 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
435 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
436 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
437 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
438 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
439 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
440 { "__imag", RID_IMAGPART, 0 },
441 { "__imag__", RID_IMAGPART, 0 },
442 { "__inline", RID_INLINE, 0 },
443 { "__inline__", RID_INLINE, 0 },
444 { "__int128", RID_INT128, 0 },
445 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
446 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
447 { "__is_class", RID_IS_CLASS, D_CXXONLY },
448 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
449 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
450 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
451 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
452 { "__is_pod", RID_IS_POD, D_CXXONLY },
453 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
454 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
455 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
456 { "__is_union", RID_IS_UNION, D_CXXONLY },
457 { "__label__", RID_LABEL, 0 },
458 { "__null", RID_NULL, 0 },
459 { "__real", RID_REALPART, 0 },
460 { "__real__", RID_REALPART, 0 },
461 { "__restrict", RID_RESTRICT, 0 },
462 { "__restrict__", RID_RESTRICT, 0 },
463 { "__signed", RID_SIGNED, 0 },
464 { "__signed__", RID_SIGNED, 0 },
465 { "__thread", RID_THREAD, 0 },
466 { "__typeof", RID_TYPEOF, 0 },
467 { "__typeof__", RID_TYPEOF, 0 },
468 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
469 { "__volatile", RID_VOLATILE, 0 },
470 { "__volatile__", RID_VOLATILE, 0 },
471 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
472 { "asm", RID_ASM, D_ASM },
473 { "auto", RID_AUTO, 0 },
474 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
475 { "break", RID_BREAK, 0 },
476 { "case", RID_CASE, 0 },
477 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
478 { "char", RID_CHAR, 0 },
479 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
480 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
481 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
482 { "const", RID_CONST, 0 },
483 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
484 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
485 { "continue", RID_CONTINUE, 0 },
486 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
487 { "default", RID_DEFAULT, 0 },
488 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
489 { "do", RID_DO, 0 },
490 { "double", RID_DOUBLE, 0 },
491 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
492 { "else", RID_ELSE, 0 },
493 { "enum", RID_ENUM, 0 },
494 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
495 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
496 { "extern", RID_EXTERN, 0 },
497 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
498 { "float", RID_FLOAT, 0 },
499 { "for", RID_FOR, 0 },
500 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
501 { "goto", RID_GOTO, 0 },
502 { "if", RID_IF, 0 },
503 { "inline", RID_INLINE, D_EXT89 },
504 { "int", RID_INT, 0 },
505 { "long", RID_LONG, 0 },
506 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
507 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
508 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
509 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
510 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
511 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
512 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
513 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
514 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
515 { "register", RID_REGISTER, 0 },
516 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
517 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
518 { "return", RID_RETURN, 0 },
519 { "short", RID_SHORT, 0 },
520 { "signed", RID_SIGNED, 0 },
521 { "sizeof", RID_SIZEOF, 0 },
522 { "static", RID_STATIC, 0 },
523 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
524 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
525 { "struct", RID_STRUCT, 0 },
526 { "switch", RID_SWITCH, 0 },
527 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
528 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
529 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
530 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
531 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
532 { "typedef", RID_TYPEDEF, 0 },
533 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
534 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
535 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
536 { "union", RID_UNION, 0 },
537 { "unsigned", RID_UNSIGNED, 0 },
538 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
539 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
540 { "void", RID_VOID, 0 },
541 { "volatile", RID_VOLATILE, 0 },
542 { "wchar_t", RID_WCHAR, D_CXXONLY },
543 { "while", RID_WHILE, 0 },
544 /* These Objective-C keywords are recognized only immediately after
545 an '@'. */
546 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
547 { "defs", RID_AT_DEFS, D_OBJC },
548 { "encode", RID_AT_ENCODE, D_OBJC },
549 { "end", RID_AT_END, D_OBJC },
550 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
551 { "interface", RID_AT_INTERFACE, D_OBJC },
552 { "protocol", RID_AT_PROTOCOL, D_OBJC },
553 { "selector", RID_AT_SELECTOR, D_OBJC },
554 { "finally", RID_AT_FINALLY, D_OBJC },
555 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
556 { "optional", RID_AT_OPTIONAL, D_OBJC },
557 { "required", RID_AT_REQUIRED, D_OBJC },
558 { "property", RID_AT_PROPERTY, D_OBJC },
559 { "package", RID_AT_PACKAGE, D_OBJC },
560 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
561 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
562 /* These are recognized only in protocol-qualifier context
563 (see above) */
564 { "bycopy", RID_BYCOPY, D_OBJC },
565 { "byref", RID_BYREF, D_OBJC },
566 { "in", RID_IN, D_OBJC },
567 { "inout", RID_INOUT, D_OBJC },
568 { "oneway", RID_ONEWAY, D_OBJC },
569 { "out", RID_OUT, D_OBJC },
570 /* These are recognized inside a property attribute list */
571 { "assign", RID_ASSIGN, D_OBJC },
572 { "copy", RID_COPY, D_OBJC },
573 { "getter", RID_GETTER, D_OBJC },
574 { "nonatomic", RID_NONATOMIC, D_OBJC },
575 { "readonly", RID_READONLY, D_OBJC },
576 { "readwrite", RID_READWRITE, D_OBJC },
577 { "retain", RID_RETAIN, D_OBJC },
578 { "setter", RID_SETTER, D_OBJC },
581 const unsigned int num_c_common_reswords =
582 sizeof c_common_reswords / sizeof (struct c_common_resword);
584 /* Table of machine-independent attributes common to all C-like languages. */
585 const struct attribute_spec c_common_attribute_table[] =
587 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
588 affects_type_identity } */
589 { "packed", 0, 0, false, false, false,
590 handle_packed_attribute , false},
591 { "nocommon", 0, 0, true, false, false,
592 handle_nocommon_attribute, false},
593 { "common", 0, 0, true, false, false,
594 handle_common_attribute, false },
595 /* FIXME: logically, noreturn attributes should be listed as
596 "false, true, true" and apply to function types. But implementing this
597 would require all the places in the compiler that use TREE_THIS_VOLATILE
598 on a decl to identify non-returning functions to be located and fixed
599 to check the function type instead. */
600 { "noreturn", 0, 0, true, false, false,
601 handle_noreturn_attribute, false },
602 { "volatile", 0, 0, true, false, false,
603 handle_noreturn_attribute, false },
604 { "noinline", 0, 0, true, false, false,
605 handle_noinline_attribute, false },
606 { "noclone", 0, 0, true, false, false,
607 handle_noclone_attribute, false },
608 { "leaf", 0, 0, true, false, false,
609 handle_leaf_attribute, false },
610 { "always_inline", 0, 0, true, false, false,
611 handle_always_inline_attribute, false },
612 { "gnu_inline", 0, 0, true, false, false,
613 handle_gnu_inline_attribute, false },
614 { "artificial", 0, 0, true, false, false,
615 handle_artificial_attribute, false },
616 { "flatten", 0, 0, true, false, false,
617 handle_flatten_attribute, false },
618 { "used", 0, 0, true, false, false,
619 handle_used_attribute, false },
620 { "unused", 0, 0, false, false, false,
621 handle_unused_attribute, false },
622 { "externally_visible", 0, 0, true, false, false,
623 handle_externally_visible_attribute, false },
624 /* The same comments as for noreturn attributes apply to const ones. */
625 { "const", 0, 0, true, false, false,
626 handle_const_attribute, false },
627 { "transparent_union", 0, 0, false, false, false,
628 handle_transparent_union_attribute, false },
629 { "constructor", 0, 1, true, false, false,
630 handle_constructor_attribute, false },
631 { "destructor", 0, 1, true, false, false,
632 handle_destructor_attribute, false },
633 { "mode", 1, 1, false, true, false,
634 handle_mode_attribute, false },
635 { "section", 1, 1, true, false, false,
636 handle_section_attribute, false },
637 { "aligned", 0, 1, false, false, false,
638 handle_aligned_attribute, false },
639 { "weak", 0, 0, true, false, false,
640 handle_weak_attribute, false },
641 { "ifunc", 1, 1, true, false, false,
642 handle_ifunc_attribute, false },
643 { "alias", 1, 1, true, false, false,
644 handle_alias_attribute, false },
645 { "weakref", 0, 1, true, false, false,
646 handle_weakref_attribute, false },
647 { "no_instrument_function", 0, 0, true, false, false,
648 handle_no_instrument_function_attribute,
649 false },
650 { "malloc", 0, 0, true, false, false,
651 handle_malloc_attribute, false },
652 { "returns_twice", 0, 0, true, false, false,
653 handle_returns_twice_attribute, false },
654 { "no_stack_limit", 0, 0, true, false, false,
655 handle_no_limit_stack_attribute, false },
656 { "pure", 0, 0, true, false, false,
657 handle_pure_attribute, false },
658 /* For internal use (marking of builtins) only. The name contains space
659 to prevent its usage in source code. */
660 { "no vops", 0, 0, true, false, false,
661 handle_novops_attribute, false },
662 { "deprecated", 0, 1, false, false, false,
663 handle_deprecated_attribute, false },
664 { "vector_size", 1, 1, false, true, false,
665 handle_vector_size_attribute, false },
666 { "visibility", 1, 1, false, false, false,
667 handle_visibility_attribute, false },
668 { "tls_model", 1, 1, true, false, false,
669 handle_tls_model_attribute, false },
670 { "nonnull", 0, -1, false, true, true,
671 handle_nonnull_attribute, false },
672 { "nothrow", 0, 0, true, false, false,
673 handle_nothrow_attribute, false },
674 { "may_alias", 0, 0, false, true, false, NULL, false },
675 { "cleanup", 1, 1, true, false, false,
676 handle_cleanup_attribute, false },
677 { "warn_unused_result", 0, 0, false, true, true,
678 handle_warn_unused_result_attribute, false },
679 { "sentinel", 0, 1, false, true, true,
680 handle_sentinel_attribute, false },
681 /* For internal use (marking of builtins) only. The name contains space
682 to prevent its usage in source code. */
683 { "type generic", 0, 0, false, true, true,
684 handle_type_generic_attribute, false },
685 { "alloc_size", 1, 2, false, true, true,
686 handle_alloc_size_attribute, false },
687 { "cold", 0, 0, true, false, false,
688 handle_cold_attribute, false },
689 { "hot", 0, 0, true, false, false,
690 handle_hot_attribute, false },
691 { "warning", 1, 1, true, false, false,
692 handle_error_attribute, false },
693 { "error", 1, 1, true, false, false,
694 handle_error_attribute, false },
695 { "target", 1, -1, true, false, false,
696 handle_target_attribute, false },
697 { "optimize", 1, -1, true, false, false,
698 handle_optimize_attribute, false },
699 { "no_split_stack", 0, 0, true, false, false,
700 handle_no_split_stack_attribute, false },
701 /* For internal use (marking of builtins and runtime functions) only.
702 The name contains space to prevent its usage in source code. */
703 { "fn spec", 1, 1, false, true, true,
704 handle_fnspec_attribute, false },
705 { NULL, 0, 0, false, false, false, NULL, false }
708 /* Give the specifications for the format attributes, used by C and all
709 descendants. */
711 const struct attribute_spec c_common_format_attribute_table[] =
713 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
714 affects_type_identity } */
715 { "format", 3, 3, false, true, true,
716 handle_format_attribute, false },
717 { "format_arg", 1, 1, false, true, true,
718 handle_format_arg_attribute, false },
719 { NULL, 0, 0, false, false, false, NULL, false }
722 /* Return identifier for address space AS. */
724 const char *
725 c_addr_space_name (addr_space_t as)
727 int rid = RID_FIRST_ADDR_SPACE + as;
728 gcc_assert (ridpointers [rid]);
729 return IDENTIFIER_POINTER (ridpointers [rid]);
732 /* Push current bindings for the function name VAR_DECLS. */
734 void
735 start_fname_decls (void)
737 unsigned ix;
738 tree saved = NULL_TREE;
740 for (ix = 0; fname_vars[ix].decl; ix++)
742 tree decl = *fname_vars[ix].decl;
744 if (decl)
746 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
747 saved);
748 *fname_vars[ix].decl = NULL_TREE;
751 if (saved || saved_function_name_decls)
752 /* Normally they'll have been NULL, so only push if we've got a
753 stack, or they are non-NULL. */
754 saved_function_name_decls = tree_cons (saved, NULL_TREE,
755 saved_function_name_decls);
758 /* Finish up the current bindings, adding them into the current function's
759 statement tree. This must be done _before_ finish_stmt_tree is called.
760 If there is no current function, we must be at file scope and no statements
761 are involved. Pop the previous bindings. */
763 void
764 finish_fname_decls (void)
766 unsigned ix;
767 tree stmts = NULL_TREE;
768 tree stack = saved_function_name_decls;
770 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
771 append_to_statement_list (TREE_VALUE (stack), &stmts);
773 if (stmts)
775 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
777 if (TREE_CODE (*bodyp) == BIND_EXPR)
778 bodyp = &BIND_EXPR_BODY (*bodyp);
780 append_to_statement_list_force (*bodyp, &stmts);
781 *bodyp = stmts;
784 for (ix = 0; fname_vars[ix].decl; ix++)
785 *fname_vars[ix].decl = NULL_TREE;
787 if (stack)
789 /* We had saved values, restore them. */
790 tree saved;
792 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
794 tree decl = TREE_PURPOSE (saved);
795 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
797 *fname_vars[ix].decl = decl;
799 stack = TREE_CHAIN (stack);
801 saved_function_name_decls = stack;
804 /* Return the text name of the current function, suitably prettified
805 by PRETTY_P. Return string must be freed by caller. */
807 const char *
808 fname_as_string (int pretty_p)
810 const char *name = "top level";
811 char *namep;
812 int vrb = 2, len;
813 cpp_string cstr = { 0, 0 }, strname;
815 if (!pretty_p)
817 name = "";
818 vrb = 0;
821 if (current_function_decl)
822 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
824 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
826 namep = XNEWVEC (char, len);
827 snprintf (namep, len, "\"%s\"", name);
828 strname.text = (unsigned char *) namep;
829 strname.len = len - 1;
831 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
833 XDELETEVEC (namep);
834 return (const char *) cstr.text;
837 return namep;
840 /* Return the VAR_DECL for a const char array naming the current
841 function. If the VAR_DECL has not yet been created, create it
842 now. RID indicates how it should be formatted and IDENTIFIER_NODE
843 ID is its name (unfortunately C and C++ hold the RID values of
844 keywords in different places, so we can't derive RID from ID in
845 this language independent code. LOC is the location of the
846 function. */
848 tree
849 fname_decl (location_t loc, unsigned int rid, tree id)
851 unsigned ix;
852 tree decl = NULL_TREE;
854 for (ix = 0; fname_vars[ix].decl; ix++)
855 if (fname_vars[ix].rid == rid)
856 break;
858 decl = *fname_vars[ix].decl;
859 if (!decl)
861 /* If a tree is built here, it would normally have the lineno of
862 the current statement. Later this tree will be moved to the
863 beginning of the function and this line number will be wrong.
864 To avoid this problem set the lineno to 0 here; that prevents
865 it from appearing in the RTL. */
866 tree stmts;
867 location_t saved_location = input_location;
868 input_location = UNKNOWN_LOCATION;
870 stmts = push_stmt_list ();
871 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
872 stmts = pop_stmt_list (stmts);
873 if (!IS_EMPTY_STMT (stmts))
874 saved_function_name_decls
875 = tree_cons (decl, stmts, saved_function_name_decls);
876 *fname_vars[ix].decl = decl;
877 input_location = saved_location;
879 if (!ix && !current_function_decl)
880 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
882 return decl;
885 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
887 tree
888 fix_string_type (tree value)
890 int length = TREE_STRING_LENGTH (value);
891 int nchars;
892 tree e_type, i_type, a_type;
894 /* Compute the number of elements, for the array type. */
895 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
897 nchars = length;
898 e_type = char_type_node;
900 else if (TREE_TYPE (value) == char16_array_type_node)
902 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
903 e_type = char16_type_node;
905 else if (TREE_TYPE (value) == char32_array_type_node)
907 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
908 e_type = char32_type_node;
910 else
912 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
913 e_type = wchar_type_node;
916 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
917 limit in C++98 Annex B is very large (65536) and is not normative,
918 so we do not diagnose it (warn_overlength_strings is forced off
919 in c_common_post_options). */
920 if (warn_overlength_strings)
922 const int nchars_max = flag_isoc99 ? 4095 : 509;
923 const int relevant_std = flag_isoc99 ? 99 : 90;
924 if (nchars - 1 > nchars_max)
925 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
926 separate the %d from the 'C'. 'ISO' should not be
927 translated, but it may be moved after 'C%d' in languages
928 where modifiers follow nouns. */
929 pedwarn (input_location, OPT_Woverlength_strings,
930 "string length %qd is greater than the length %qd "
931 "ISO C%d compilers are required to support",
932 nchars - 1, nchars_max, relevant_std);
935 /* Create the array type for the string constant. The ISO C++
936 standard says that a string literal has type `const char[N]' or
937 `const wchar_t[N]'. We use the same logic when invoked as a C
938 front-end with -Wwrite-strings.
939 ??? We should change the type of an expression depending on the
940 state of a warning flag. We should just be warning -- see how
941 this is handled in the C++ front-end for the deprecated implicit
942 conversion from string literals to `char*' or `wchar_t*'.
944 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
945 array type being the unqualified version of that type.
946 Therefore, if we are constructing an array of const char, we must
947 construct the matching unqualified array type first. The C front
948 end does not require this, but it does no harm, so we do it
949 unconditionally. */
950 i_type = build_index_type (size_int (nchars - 1));
951 a_type = build_array_type (e_type, i_type);
952 if (c_dialect_cxx() || warn_write_strings)
953 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
955 TREE_TYPE (value) = a_type;
956 TREE_CONSTANT (value) = 1;
957 TREE_READONLY (value) = 1;
958 TREE_STATIC (value) = 1;
959 return value;
962 /* Fully fold EXPR, an expression that was not folded (beyond integer
963 constant expressions and null pointer constants) when being built
964 up. If IN_INIT, this is in a static initializer and certain
965 changes are made to the folding done. Clear *MAYBE_CONST if
966 MAYBE_CONST is not NULL and EXPR is definitely not a constant
967 expression because it contains an evaluated operator (in C99) or an
968 operator outside of sizeof returning an integer constant (in C90)
969 not permitted in constant expressions, or because it contains an
970 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
971 set to true by callers before calling this function.) Return the
972 folded expression. Function arguments have already been folded
973 before calling this function, as have the contents of SAVE_EXPR,
974 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
975 C_MAYBE_CONST_EXPR. */
977 tree
978 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
980 tree ret;
981 tree eptype = NULL_TREE;
982 bool dummy = true;
983 bool maybe_const_itself = true;
984 location_t loc = EXPR_LOCATION (expr);
986 /* This function is not relevant to C++ because C++ folds while
987 parsing, and may need changes to be correct for C++ when C++
988 stops folding while parsing. */
989 if (c_dialect_cxx ())
990 gcc_unreachable ();
992 if (!maybe_const)
993 maybe_const = &dummy;
994 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
996 eptype = TREE_TYPE (expr);
997 expr = TREE_OPERAND (expr, 0);
999 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1000 &maybe_const_itself);
1001 if (eptype)
1002 ret = fold_convert_loc (loc, eptype, ret);
1003 *maybe_const &= maybe_const_itself;
1004 return ret;
1007 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1008 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1009 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1010 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1011 both evaluated and unevaluated subexpressions while
1012 *MAYBE_CONST_ITSELF is carried from only evaluated
1013 subexpressions). */
1015 static tree
1016 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1017 bool *maybe_const_itself)
1019 tree ret = expr;
1020 enum tree_code code = TREE_CODE (expr);
1021 enum tree_code_class kind = TREE_CODE_CLASS (code);
1022 location_t loc = EXPR_LOCATION (expr);
1023 tree op0, op1, op2, op3;
1024 tree orig_op0, orig_op1, orig_op2;
1025 bool op0_const = true, op1_const = true, op2_const = true;
1026 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1027 bool nowarning = TREE_NO_WARNING (expr);
1028 int unused_p;
1030 /* This function is not relevant to C++ because C++ folds while
1031 parsing, and may need changes to be correct for C++ when C++
1032 stops folding while parsing. */
1033 if (c_dialect_cxx ())
1034 gcc_unreachable ();
1036 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1037 anything else not counted as an expression cannot usefully be
1038 folded further at this point. */
1039 if (!IS_EXPR_CODE_CLASS (kind)
1040 || kind == tcc_statement
1041 || code == SAVE_EXPR)
1042 return expr;
1044 /* Operands of variable-length expressions (function calls) have
1045 already been folded, as have __builtin_* function calls, and such
1046 expressions cannot occur in constant expressions. */
1047 if (kind == tcc_vl_exp)
1049 *maybe_const_operands = false;
1050 ret = fold (expr);
1051 goto out;
1054 if (code == C_MAYBE_CONST_EXPR)
1056 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1057 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1058 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1059 *maybe_const_operands = false;
1060 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1061 *maybe_const_itself = false;
1062 if (pre && !in_init)
1063 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1064 else
1065 ret = inner;
1066 goto out;
1069 /* Assignment, increment, decrement, function call and comma
1070 operators, and statement expressions, cannot occur in constant
1071 expressions if evaluated / outside of sizeof. (Function calls
1072 were handled above, though VA_ARG_EXPR is treated like a function
1073 call here, and statement expressions are handled through
1074 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1075 switch (code)
1077 case MODIFY_EXPR:
1078 case PREDECREMENT_EXPR:
1079 case PREINCREMENT_EXPR:
1080 case POSTDECREMENT_EXPR:
1081 case POSTINCREMENT_EXPR:
1082 case COMPOUND_EXPR:
1083 *maybe_const_operands = false;
1084 break;
1086 case VA_ARG_EXPR:
1087 case TARGET_EXPR:
1088 case BIND_EXPR:
1089 case OBJ_TYPE_REF:
1090 *maybe_const_operands = false;
1091 ret = fold (expr);
1092 goto out;
1094 default:
1095 break;
1098 /* Fold individual tree codes as appropriate. */
1099 switch (code)
1101 case COMPOUND_LITERAL_EXPR:
1102 /* Any non-constancy will have been marked in a containing
1103 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1104 goto out;
1106 case COMPONENT_REF:
1107 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1108 op1 = TREE_OPERAND (expr, 1);
1109 op2 = TREE_OPERAND (expr, 2);
1110 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1111 maybe_const_itself);
1112 STRIP_TYPE_NOPS (op0);
1113 if (op0 != orig_op0)
1114 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1115 if (ret != expr)
1117 TREE_READONLY (ret) = TREE_READONLY (expr);
1118 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1120 goto out;
1122 case ARRAY_REF:
1123 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1124 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1125 op2 = TREE_OPERAND (expr, 2);
1126 op3 = TREE_OPERAND (expr, 3);
1127 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1128 maybe_const_itself);
1129 STRIP_TYPE_NOPS (op0);
1130 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1131 maybe_const_itself);
1132 STRIP_TYPE_NOPS (op1);
1133 op1 = decl_constant_value_for_optimization (op1);
1134 if (op0 != orig_op0 || op1 != orig_op1)
1135 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1136 if (ret != expr)
1138 TREE_READONLY (ret) = TREE_READONLY (expr);
1139 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1140 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1142 ret = fold (ret);
1143 goto out;
1145 case COMPOUND_EXPR:
1146 case MODIFY_EXPR:
1147 case PREDECREMENT_EXPR:
1148 case PREINCREMENT_EXPR:
1149 case POSTDECREMENT_EXPR:
1150 case POSTINCREMENT_EXPR:
1151 case PLUS_EXPR:
1152 case MINUS_EXPR:
1153 case MULT_EXPR:
1154 case POINTER_PLUS_EXPR:
1155 case TRUNC_DIV_EXPR:
1156 case CEIL_DIV_EXPR:
1157 case FLOOR_DIV_EXPR:
1158 case TRUNC_MOD_EXPR:
1159 case RDIV_EXPR:
1160 case EXACT_DIV_EXPR:
1161 case LSHIFT_EXPR:
1162 case RSHIFT_EXPR:
1163 case BIT_IOR_EXPR:
1164 case BIT_XOR_EXPR:
1165 case BIT_AND_EXPR:
1166 case LT_EXPR:
1167 case LE_EXPR:
1168 case GT_EXPR:
1169 case GE_EXPR:
1170 case EQ_EXPR:
1171 case NE_EXPR:
1172 case COMPLEX_EXPR:
1173 case TRUTH_AND_EXPR:
1174 case TRUTH_OR_EXPR:
1175 case TRUTH_XOR_EXPR:
1176 case UNORDERED_EXPR:
1177 case ORDERED_EXPR:
1178 case UNLT_EXPR:
1179 case UNLE_EXPR:
1180 case UNGT_EXPR:
1181 case UNGE_EXPR:
1182 case UNEQ_EXPR:
1183 /* Binary operations evaluating both arguments (increment and
1184 decrement are binary internally in GCC). */
1185 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1186 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1187 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1188 maybe_const_itself);
1189 STRIP_TYPE_NOPS (op0);
1190 if (code != MODIFY_EXPR
1191 && code != PREDECREMENT_EXPR
1192 && code != PREINCREMENT_EXPR
1193 && code != POSTDECREMENT_EXPR
1194 && code != POSTINCREMENT_EXPR)
1195 op0 = decl_constant_value_for_optimization (op0);
1196 /* The RHS of a MODIFY_EXPR was fully folded when building that
1197 expression for the sake of conversion warnings. */
1198 if (code != MODIFY_EXPR)
1199 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1200 maybe_const_itself);
1201 STRIP_TYPE_NOPS (op1);
1202 op1 = decl_constant_value_for_optimization (op1);
1203 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1204 ret = in_init
1205 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1206 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1207 else
1208 ret = fold (expr);
1209 if (TREE_OVERFLOW_P (ret)
1210 && !TREE_OVERFLOW_P (op0)
1211 && !TREE_OVERFLOW_P (op1))
1212 overflow_warning (EXPR_LOCATION (expr), ret);
1213 goto out;
1215 case INDIRECT_REF:
1216 case FIX_TRUNC_EXPR:
1217 case FLOAT_EXPR:
1218 CASE_CONVERT:
1219 case VIEW_CONVERT_EXPR:
1220 case NON_LVALUE_EXPR:
1221 case NEGATE_EXPR:
1222 case BIT_NOT_EXPR:
1223 case TRUTH_NOT_EXPR:
1224 case ADDR_EXPR:
1225 case CONJ_EXPR:
1226 case REALPART_EXPR:
1227 case IMAGPART_EXPR:
1228 /* Unary operations. */
1229 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1230 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1231 maybe_const_itself);
1232 STRIP_TYPE_NOPS (op0);
1233 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1234 op0 = decl_constant_value_for_optimization (op0);
1235 if (op0 != orig_op0 || in_init)
1236 ret = in_init
1237 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1238 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1239 else
1240 ret = fold (expr);
1241 if (code == INDIRECT_REF
1242 && ret != expr
1243 && TREE_CODE (ret) == INDIRECT_REF)
1245 TREE_READONLY (ret) = TREE_READONLY (expr);
1246 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1247 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1249 switch (code)
1251 case FIX_TRUNC_EXPR:
1252 case FLOAT_EXPR:
1253 CASE_CONVERT:
1254 /* Don't warn about explicit conversions. We will already
1255 have warned about suspect implicit conversions. */
1256 break;
1258 default:
1259 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1260 overflow_warning (EXPR_LOCATION (expr), ret);
1261 break;
1263 goto out;
1265 case TRUTH_ANDIF_EXPR:
1266 case TRUTH_ORIF_EXPR:
1267 /* Binary operations not necessarily evaluating both
1268 arguments. */
1269 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1270 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1271 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1272 STRIP_TYPE_NOPS (op0);
1274 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1275 ? truthvalue_false_node
1276 : truthvalue_true_node));
1277 c_inhibit_evaluation_warnings += unused_p;
1278 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1279 STRIP_TYPE_NOPS (op1);
1280 c_inhibit_evaluation_warnings -= unused_p;
1282 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1283 ret = in_init
1284 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1285 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1286 else
1287 ret = fold (expr);
1288 *maybe_const_operands &= op0_const;
1289 *maybe_const_itself &= op0_const_self;
1290 if (!(flag_isoc99
1291 && op0_const
1292 && op0_const_self
1293 && (code == TRUTH_ANDIF_EXPR
1294 ? op0 == truthvalue_false_node
1295 : op0 == truthvalue_true_node)))
1296 *maybe_const_operands &= op1_const;
1297 if (!(op0_const
1298 && op0_const_self
1299 && (code == TRUTH_ANDIF_EXPR
1300 ? op0 == truthvalue_false_node
1301 : op0 == truthvalue_true_node)))
1302 *maybe_const_itself &= op1_const_self;
1303 goto out;
1305 case COND_EXPR:
1306 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1307 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1308 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1309 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1311 STRIP_TYPE_NOPS (op0);
1312 c_inhibit_evaluation_warnings += (op0 == truthvalue_false_node);
1313 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1314 STRIP_TYPE_NOPS (op1);
1315 c_inhibit_evaluation_warnings -= (op0 == truthvalue_false_node);
1317 c_inhibit_evaluation_warnings += (op0 == truthvalue_true_node);
1318 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1319 STRIP_TYPE_NOPS (op2);
1320 c_inhibit_evaluation_warnings -= (op0 == truthvalue_true_node);
1322 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1323 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1324 else
1325 ret = fold (expr);
1326 *maybe_const_operands &= op0_const;
1327 *maybe_const_itself &= op0_const_self;
1328 if (!(flag_isoc99
1329 && op0_const
1330 && op0_const_self
1331 && op0 == truthvalue_false_node))
1332 *maybe_const_operands &= op1_const;
1333 if (!(op0_const
1334 && op0_const_self
1335 && op0 == truthvalue_false_node))
1336 *maybe_const_itself &= op1_const_self;
1337 if (!(flag_isoc99
1338 && op0_const
1339 && op0_const_self
1340 && op0 == truthvalue_true_node))
1341 *maybe_const_operands &= op2_const;
1342 if (!(op0_const
1343 && op0_const_self
1344 && op0 == truthvalue_true_node))
1345 *maybe_const_itself &= op2_const_self;
1346 goto out;
1348 case EXCESS_PRECISION_EXPR:
1349 /* Each case where an operand with excess precision may be
1350 encountered must remove the EXCESS_PRECISION_EXPR around
1351 inner operands and possibly put one around the whole
1352 expression or possibly convert to the semantic type (which
1353 c_fully_fold does); we cannot tell at this stage which is
1354 appropriate in any particular case. */
1355 gcc_unreachable ();
1357 default:
1358 /* Various codes may appear through folding built-in functions
1359 and their arguments. */
1360 goto out;
1363 out:
1364 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1365 have been done by this point, so remove them again. */
1366 nowarning |= TREE_NO_WARNING (ret);
1367 STRIP_TYPE_NOPS (ret);
1368 if (nowarning && !TREE_NO_WARNING (ret))
1370 if (!CAN_HAVE_LOCATION_P (ret))
1371 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1372 TREE_NO_WARNING (ret) = 1;
1374 if (ret != expr)
1375 protected_set_expr_location (ret, loc);
1376 return ret;
1379 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1380 return EXP. Otherwise, return either EXP or its known constant
1381 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1382 Is the BLKmode test appropriate? */
1384 tree
1385 decl_constant_value_for_optimization (tree exp)
1387 tree ret;
1389 /* This function is only used by C, for c_fully_fold and other
1390 optimization, and may not be correct for C++. */
1391 if (c_dialect_cxx ())
1392 gcc_unreachable ();
1394 if (!optimize
1395 || TREE_CODE (exp) != VAR_DECL
1396 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1397 || DECL_MODE (exp) == BLKmode)
1398 return exp;
1400 ret = decl_constant_value (exp);
1401 /* Avoid unwanted tree sharing between the initializer and current
1402 function's body where the tree can be modified e.g. by the
1403 gimplifier. */
1404 if (ret != exp && TREE_STATIC (exp))
1405 ret = unshare_expr (ret);
1406 return ret;
1409 /* Print a warning if a constant expression had overflow in folding.
1410 Invoke this function on every expression that the language
1411 requires to be a constant expression.
1412 Note the ANSI C standard says it is erroneous for a
1413 constant expression to overflow. */
1415 void
1416 constant_expression_warning (tree value)
1418 if (warn_overflow && pedantic
1419 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1420 || TREE_CODE (value) == FIXED_CST
1421 || TREE_CODE (value) == VECTOR_CST
1422 || TREE_CODE (value) == COMPLEX_CST)
1423 && TREE_OVERFLOW (value))
1424 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1427 /* The same as above but print an unconditional error. */
1428 void
1429 constant_expression_error (tree value)
1431 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1432 || TREE_CODE (value) == FIXED_CST
1433 || TREE_CODE (value) == VECTOR_CST
1434 || TREE_CODE (value) == COMPLEX_CST)
1435 && TREE_OVERFLOW (value))
1436 error ("overflow in constant expression");
1439 /* Print a warning if an expression had overflow in folding and its
1440 operands hadn't.
1442 Invoke this function on every expression that
1443 (1) appears in the source code, and
1444 (2) is a constant expression that overflowed, and
1445 (3) is not already checked by convert_and_check;
1446 however, do not invoke this function on operands of explicit casts
1447 or when the expression is the result of an operator and any operand
1448 already overflowed. */
1450 void
1451 overflow_warning (location_t loc, tree value)
1453 if (c_inhibit_evaluation_warnings != 0)
1454 return;
1456 switch (TREE_CODE (value))
1458 case INTEGER_CST:
1459 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1460 break;
1462 case REAL_CST:
1463 warning_at (loc, OPT_Woverflow,
1464 "floating point overflow in expression");
1465 break;
1467 case FIXED_CST:
1468 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1469 break;
1471 case VECTOR_CST:
1472 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1473 break;
1475 case COMPLEX_CST:
1476 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1477 warning_at (loc, OPT_Woverflow,
1478 "complex integer overflow in expression");
1479 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1480 warning_at (loc, OPT_Woverflow,
1481 "complex floating point overflow in expression");
1482 break;
1484 default:
1485 break;
1489 /* Warn about uses of logical || / && operator in a context where it
1490 is likely that the bitwise equivalent was intended by the
1491 programmer. We have seen an expression in which CODE is a binary
1492 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1493 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1494 void
1495 warn_logical_operator (location_t location, enum tree_code code, tree type,
1496 enum tree_code code_left, tree op_left,
1497 enum tree_code ARG_UNUSED (code_right), tree op_right)
1499 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1500 int in0_p, in1_p, in_p;
1501 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1502 bool strict_overflow_p = false;
1504 if (code != TRUTH_ANDIF_EXPR
1505 && code != TRUTH_AND_EXPR
1506 && code != TRUTH_ORIF_EXPR
1507 && code != TRUTH_OR_EXPR)
1508 return;
1510 /* Warn if &&/|| are being used in a context where it is
1511 likely that the bitwise equivalent was intended by the
1512 programmer. That is, an expression such as op && MASK
1513 where op should not be any boolean expression, nor a
1514 constant, and mask seems to be a non-boolean integer constant. */
1515 if (!truth_value_p (code_left)
1516 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1517 && !CONSTANT_CLASS_P (op_left)
1518 && !TREE_NO_WARNING (op_left)
1519 && TREE_CODE (op_right) == INTEGER_CST
1520 && !integer_zerop (op_right)
1521 && !integer_onep (op_right))
1523 if (or_op)
1524 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1525 " applied to non-boolean constant");
1526 else
1527 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1528 " applied to non-boolean constant");
1529 TREE_NO_WARNING (op_left) = true;
1530 return;
1533 /* We do not warn for constants because they are typical of macro
1534 expansions that test for features. */
1535 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1536 return;
1538 /* This warning only makes sense with logical operands. */
1539 if (!(truth_value_p (TREE_CODE (op_left))
1540 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1541 || !(truth_value_p (TREE_CODE (op_right))
1542 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1543 return;
1545 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1546 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1548 if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1549 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1551 if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1552 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1554 /* If this is an OR operation, invert both sides; we will invert
1555 again at the end. */
1556 if (or_op)
1557 in0_p = !in0_p, in1_p = !in1_p;
1559 /* If both expressions are the same, if we can merge the ranges, and we
1560 can build the range test, return it or it inverted. */
1561 if (lhs && rhs && operand_equal_p (lhs, rhs, 0)
1562 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1563 in1_p, low1, high1)
1564 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1565 type, lhs, in_p, low, high)))
1567 if (TREE_CODE (tem) != INTEGER_CST)
1568 return;
1570 if (or_op)
1571 warning_at (location, OPT_Wlogical_op,
1572 "logical %<or%> "
1573 "of collectively exhaustive tests is always true");
1574 else
1575 warning_at (location, OPT_Wlogical_op,
1576 "logical %<and%> "
1577 "of mutually exclusive tests is always false");
1582 /* Print a warning about casts that might indicate violation
1583 of strict aliasing rules if -Wstrict-aliasing is used and
1584 strict aliasing mode is in effect. OTYPE is the original
1585 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1587 bool
1588 strict_aliasing_warning (tree otype, tree type, tree expr)
1590 /* Strip pointer conversion chains and get to the correct original type. */
1591 STRIP_NOPS (expr);
1592 otype = TREE_TYPE (expr);
1594 if (!(flag_strict_aliasing
1595 && POINTER_TYPE_P (type)
1596 && POINTER_TYPE_P (otype)
1597 && !VOID_TYPE_P (TREE_TYPE (type)))
1598 /* If the type we are casting to is a ref-all pointer
1599 dereferencing it is always valid. */
1600 || TYPE_REF_CAN_ALIAS_ALL (type))
1601 return false;
1603 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1604 && (DECL_P (TREE_OPERAND (expr, 0))
1605 || handled_component_p (TREE_OPERAND (expr, 0))))
1607 /* Casting the address of an object to non void pointer. Warn
1608 if the cast breaks type based aliasing. */
1609 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1611 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1612 "might break strict-aliasing rules");
1613 return true;
1615 else
1617 /* warn_strict_aliasing >= 3. This includes the default (3).
1618 Only warn if the cast is dereferenced immediately. */
1619 alias_set_type set1 =
1620 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1621 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1623 if (set1 != set2 && set2 != 0
1624 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1626 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1627 "pointer will break strict-aliasing rules");
1628 return true;
1630 else if (warn_strict_aliasing == 2
1631 && !alias_sets_must_conflict_p (set1, set2))
1633 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1634 "pointer might break strict-aliasing rules");
1635 return true;
1639 else
1640 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1642 /* At this level, warn for any conversions, even if an address is
1643 not taken in the same statement. This will likely produce many
1644 false positives, but could be useful to pinpoint problems that
1645 are not revealed at higher levels. */
1646 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1647 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1648 if (!COMPLETE_TYPE_P (type)
1649 || !alias_sets_must_conflict_p (set1, set2))
1651 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1652 "pointer might break strict-aliasing rules");
1653 return true;
1657 return false;
1660 /* Warn for unlikely, improbable, or stupid DECL declarations
1661 of `main'. */
1663 void
1664 check_main_parameter_types (tree decl)
1666 function_args_iterator iter;
1667 tree type;
1668 int argct = 0;
1670 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1672 /* XXX void_type_node belies the abstraction. */
1673 if (type == void_type_node || type == error_mark_node )
1674 break;
1676 ++argct;
1677 switch (argct)
1679 case 1:
1680 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1681 pedwarn (input_location, OPT_Wmain,
1682 "first argument of %q+D should be %<int%>", decl);
1683 break;
1685 case 2:
1686 if (TREE_CODE (type) != POINTER_TYPE
1687 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1688 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1689 != char_type_node))
1690 pedwarn (input_location, OPT_Wmain,
1691 "second argument of %q+D should be %<char **%>", decl);
1692 break;
1694 case 3:
1695 if (TREE_CODE (type) != POINTER_TYPE
1696 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1697 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1698 != char_type_node))
1699 pedwarn (input_location, OPT_Wmain,
1700 "third argument of %q+D should probably be "
1701 "%<char **%>", decl);
1702 break;
1706 /* It is intentional that this message does not mention the third
1707 argument because it's only mentioned in an appendix of the
1708 standard. */
1709 if (argct > 0 && (argct < 2 || argct > 3))
1710 pedwarn (input_location, OPT_Wmain,
1711 "%q+D takes only zero or two arguments", decl);
1714 /* True if pointers to distinct types T1 and T2 can be converted to
1715 each other without an explicit cast. Only returns true for opaque
1716 vector types. */
1717 bool
1718 vector_targets_convertible_p (const_tree t1, const_tree t2)
1720 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
1721 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1722 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1723 return true;
1725 return false;
1728 /* True if vector types T1 and T2 can be converted to each other
1729 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1730 can only be converted with -flax-vector-conversions yet that is not
1731 in effect, emit a note telling the user about that option if such
1732 a note has not previously been emitted. */
1733 bool
1734 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1736 static bool emitted_lax_note = false;
1737 bool convertible_lax;
1739 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1740 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1741 return true;
1743 convertible_lax =
1744 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1745 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
1746 TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
1747 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1748 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1750 if (!convertible_lax || flag_lax_vector_conversions)
1751 return convertible_lax;
1753 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1754 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1755 return true;
1757 if (emit_lax_note && !emitted_lax_note)
1759 emitted_lax_note = true;
1760 inform (input_location, "use -flax-vector-conversions to permit "
1761 "conversions between vectors with differing "
1762 "element types or numbers of subparts");
1765 return false;
1768 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1769 to integral type. */
1771 static tree
1772 c_common_get_narrower (tree op, int *unsignedp_ptr)
1774 op = get_narrower (op, unsignedp_ptr);
1776 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1777 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1779 /* C++0x scoped enumerations don't implicitly convert to integral
1780 type; if we stripped an explicit conversion to a larger type we
1781 need to replace it so common_type will still work. */
1782 tree type = (lang_hooks.types.type_for_size
1783 (TYPE_PRECISION (TREE_TYPE (op)),
1784 TYPE_UNSIGNED (TREE_TYPE (op))));
1785 op = fold_convert (type, op);
1787 return op;
1790 /* This is a helper function of build_binary_op.
1792 For certain operations if both args were extended from the same
1793 smaller type, do the arithmetic in that type and then extend.
1795 BITWISE indicates a bitwise operation.
1796 For them, this optimization is safe only if
1797 both args are zero-extended or both are sign-extended.
1798 Otherwise, we might change the result.
1799 Eg, (short)-1 | (unsigned short)-1 is (int)-1
1800 but calculated in (unsigned short) it would be (unsigned short)-1.
1802 tree
1803 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1805 int unsigned0, unsigned1;
1806 tree arg0, arg1;
1807 int uns;
1808 tree type;
1810 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1811 excessive narrowing when we call get_narrower below. For
1812 example, suppose that OP0 is of unsigned int extended
1813 from signed char and that RESULT_TYPE is long long int.
1814 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1815 like
1817 (long long int) (unsigned int) signed_char
1819 which get_narrower would narrow down to
1821 (unsigned int) signed char
1823 If we do not cast OP0 first, get_narrower would return
1824 signed_char, which is inconsistent with the case of the
1825 explicit cast. */
1826 op0 = convert (result_type, op0);
1827 op1 = convert (result_type, op1);
1829 arg0 = c_common_get_narrower (op0, &unsigned0);
1830 arg1 = c_common_get_narrower (op1, &unsigned1);
1832 /* UNS is 1 if the operation to be done is an unsigned one. */
1833 uns = TYPE_UNSIGNED (result_type);
1835 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1836 but it *requires* conversion to FINAL_TYPE. */
1838 if ((TYPE_PRECISION (TREE_TYPE (op0))
1839 == TYPE_PRECISION (TREE_TYPE (arg0)))
1840 && TREE_TYPE (op0) != result_type)
1841 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1842 if ((TYPE_PRECISION (TREE_TYPE (op1))
1843 == TYPE_PRECISION (TREE_TYPE (arg1)))
1844 && TREE_TYPE (op1) != result_type)
1845 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1847 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1849 /* For bitwise operations, signedness of nominal type
1850 does not matter. Consider only how operands were extended. */
1851 if (bitwise)
1852 uns = unsigned0;
1854 /* Note that in all three cases below we refrain from optimizing
1855 an unsigned operation on sign-extended args.
1856 That would not be valid. */
1858 /* Both args variable: if both extended in same way
1859 from same width, do it in that width.
1860 Do it unsigned if args were zero-extended. */
1861 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1862 < TYPE_PRECISION (result_type))
1863 && (TYPE_PRECISION (TREE_TYPE (arg1))
1864 == TYPE_PRECISION (TREE_TYPE (arg0)))
1865 && unsigned0 == unsigned1
1866 && (unsigned0 || !uns))
1867 return c_common_signed_or_unsigned_type
1868 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1870 else if (TREE_CODE (arg0) == INTEGER_CST
1871 && (unsigned1 || !uns)
1872 && (TYPE_PRECISION (TREE_TYPE (arg1))
1873 < TYPE_PRECISION (result_type))
1874 && (type
1875 = c_common_signed_or_unsigned_type (unsigned1,
1876 TREE_TYPE (arg1)))
1877 && !POINTER_TYPE_P (type)
1878 && int_fits_type_p (arg0, type))
1879 return type;
1881 else if (TREE_CODE (arg1) == INTEGER_CST
1882 && (unsigned0 || !uns)
1883 && (TYPE_PRECISION (TREE_TYPE (arg0))
1884 < TYPE_PRECISION (result_type))
1885 && (type
1886 = c_common_signed_or_unsigned_type (unsigned0,
1887 TREE_TYPE (arg0)))
1888 && !POINTER_TYPE_P (type)
1889 && int_fits_type_p (arg1, type))
1890 return type;
1892 return result_type;
1895 /* Warns if the conversion of EXPR to TYPE may alter a value.
1896 This is a helper function for warnings_for_convert_and_check. */
1898 static void
1899 conversion_warning (tree type, tree expr)
1901 bool give_warning = false;
1903 int i;
1904 const int expr_num_operands = TREE_OPERAND_LENGTH (expr);
1905 tree expr_type = TREE_TYPE (expr);
1906 location_t loc = EXPR_LOC_OR_HERE (expr);
1908 if (!warn_conversion && !warn_sign_conversion)
1909 return;
1911 /* If any operand is artificial, then this expression was generated
1912 by the compiler and we do not warn. */
1913 for (i = 0; i < expr_num_operands; i++)
1915 tree op = TREE_OPERAND (expr, i);
1916 if (op && DECL_P (op) && DECL_ARTIFICIAL (op))
1917 return;
1920 switch (TREE_CODE (expr))
1922 case EQ_EXPR:
1923 case NE_EXPR:
1924 case LE_EXPR:
1925 case GE_EXPR:
1926 case LT_EXPR:
1927 case GT_EXPR:
1928 case TRUTH_ANDIF_EXPR:
1929 case TRUTH_ORIF_EXPR:
1930 case TRUTH_AND_EXPR:
1931 case TRUTH_OR_EXPR:
1932 case TRUTH_XOR_EXPR:
1933 case TRUTH_NOT_EXPR:
1934 /* Conversion from boolean to a signed:1 bit-field (which only
1935 can hold the values 0 and -1) doesn't lose information - but
1936 it does change the value. */
1937 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
1938 warning_at (loc, OPT_Wconversion,
1939 "conversion to %qT from boolean expression", type);
1940 return;
1942 case REAL_CST:
1943 case INTEGER_CST:
1945 /* Warn for real constant that is not an exact integer converted
1946 to integer type. */
1947 if (TREE_CODE (expr_type) == REAL_TYPE
1948 && TREE_CODE (type) == INTEGER_TYPE)
1950 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1951 give_warning = true;
1953 /* Warn for an integer constant that does not fit into integer type. */
1954 else if (TREE_CODE (expr_type) == INTEGER_TYPE
1955 && TREE_CODE (type) == INTEGER_TYPE
1956 && !int_fits_type_p (expr, type))
1958 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1959 && tree_int_cst_sgn (expr) < 0)
1960 warning_at (loc, OPT_Wsign_conversion, "negative integer"
1961 " implicitly converted to unsigned type");
1962 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1963 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
1964 " constant value to negative integer");
1965 else
1966 give_warning = true;
1968 else if (TREE_CODE (type) == REAL_TYPE)
1970 /* Warn for an integer constant that does not fit into real type. */
1971 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1973 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1974 if (!exact_real_truncate (TYPE_MODE (type), &a))
1975 give_warning = true;
1977 /* Warn for a real constant that does not fit into a smaller
1978 real type. */
1979 else if (TREE_CODE (expr_type) == REAL_TYPE
1980 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1982 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1983 if (!exact_real_truncate (TYPE_MODE (type), &a))
1984 give_warning = true;
1988 if (give_warning)
1989 warning_at (loc, OPT_Wconversion,
1990 "conversion to %qT alters %qT constant value",
1991 type, expr_type);
1993 return;
1995 case COND_EXPR:
1997 /* In case of COND_EXPR, if both operands are constants or
1998 COND_EXPR, then we do not care about the type of COND_EXPR,
1999 only about the conversion of each operand. */
2000 tree op1 = TREE_OPERAND (expr, 1);
2001 tree op2 = TREE_OPERAND (expr, 2);
2003 if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST
2004 || TREE_CODE (op1) == COND_EXPR)
2005 && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST
2006 || TREE_CODE (op2) == COND_EXPR))
2008 conversion_warning (type, op1);
2009 conversion_warning (type, op2);
2010 return;
2012 /* Fall through. */
2015 default: /* 'expr' is not a constant. */
2017 /* Warn for real types converted to integer types. */
2018 if (TREE_CODE (expr_type) == REAL_TYPE
2019 && TREE_CODE (type) == INTEGER_TYPE)
2020 give_warning = true;
2022 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2023 && TREE_CODE (type) == INTEGER_TYPE)
2025 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2026 expr = get_unwidened (expr, 0);
2027 expr_type = TREE_TYPE (expr);
2029 /* Don't warn for short y; short x = ((int)y & 0xff); */
2030 if (TREE_CODE (expr) == BIT_AND_EXPR
2031 || TREE_CODE (expr) == BIT_IOR_EXPR
2032 || TREE_CODE (expr) == BIT_XOR_EXPR)
2034 /* If both args were extended from a shortest type,
2035 use that type if that is safe. */
2036 expr_type = shorten_binary_op (expr_type,
2037 TREE_OPERAND (expr, 0),
2038 TREE_OPERAND (expr, 1),
2039 /* bitwise */1);
2041 if (TREE_CODE (expr) == BIT_AND_EXPR)
2043 tree op0 = TREE_OPERAND (expr, 0);
2044 tree op1 = TREE_OPERAND (expr, 1);
2045 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2046 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2048 /* If one of the operands is a non-negative constant
2049 that fits in the target type, then the type of the
2050 other operand does not matter. */
2051 if ((TREE_CODE (op0) == INTEGER_CST
2052 && int_fits_type_p (op0, c_common_signed_type (type))
2053 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2054 || (TREE_CODE (op1) == INTEGER_CST
2055 && int_fits_type_p (op1, c_common_signed_type (type))
2056 && int_fits_type_p (op1,
2057 c_common_unsigned_type (type))))
2058 return;
2059 /* If constant is unsigned and fits in the target
2060 type, then the result will also fit. */
2061 else if ((TREE_CODE (op0) == INTEGER_CST
2062 && unsigned0
2063 && int_fits_type_p (op0, type))
2064 || (TREE_CODE (op1) == INTEGER_CST
2065 && unsigned1
2066 && int_fits_type_p (op1, type)))
2067 return;
2070 /* Warn for integer types converted to smaller integer types. */
2071 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2072 give_warning = true;
2074 /* When they are the same width but different signedness,
2075 then the value may change. */
2076 else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2077 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2078 /* Even when converted to a bigger type, if the type is
2079 unsigned but expr is signed, then negative values
2080 will be changed. */
2081 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2082 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2083 "may change the sign of the result",
2084 type, expr_type);
2087 /* Warn for integer types converted to real types if and only if
2088 all the range of values of the integer type cannot be
2089 represented by the real type. */
2090 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2091 && TREE_CODE (type) == REAL_TYPE)
2093 tree type_low_bound, type_high_bound;
2094 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2096 /* Don't warn about char y = 0xff; float x = (int) y; */
2097 expr = get_unwidened (expr, 0);
2098 expr_type = TREE_TYPE (expr);
2100 type_low_bound = TYPE_MIN_VALUE (expr_type);
2101 type_high_bound = TYPE_MAX_VALUE (expr_type);
2102 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2103 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2105 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2106 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2107 give_warning = true;
2110 /* Warn for real types converted to smaller real types. */
2111 else if (TREE_CODE (expr_type) == REAL_TYPE
2112 && TREE_CODE (type) == REAL_TYPE
2113 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2114 give_warning = true;
2117 if (give_warning)
2118 warning_at (loc, OPT_Wconversion,
2119 "conversion to %qT from %qT may alter its value",
2120 type, expr_type);
2124 /* Produce warnings after a conversion. RESULT is the result of
2125 converting EXPR to TYPE. This is a helper function for
2126 convert_and_check and cp_convert_and_check. */
2128 void
2129 warnings_for_convert_and_check (tree type, tree expr, tree result)
2131 if (TREE_CODE (expr) == INTEGER_CST
2132 && (TREE_CODE (type) == INTEGER_TYPE
2133 || TREE_CODE (type) == ENUMERAL_TYPE)
2134 && !int_fits_type_p (expr, type))
2136 /* Do not diagnose overflow in a constant expression merely
2137 because a conversion overflowed. */
2138 if (TREE_OVERFLOW (result))
2139 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2141 if (TYPE_UNSIGNED (type))
2143 /* This detects cases like converting -129 or 256 to
2144 unsigned char. */
2145 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2146 warning (OPT_Woverflow,
2147 "large integer implicitly truncated to unsigned type");
2148 else
2149 conversion_warning (type, expr);
2151 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2152 warning (OPT_Woverflow,
2153 "overflow in implicit constant conversion");
2154 /* No warning for converting 0x80000000 to int. */
2155 else if (pedantic
2156 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2157 || TYPE_PRECISION (TREE_TYPE (expr))
2158 != TYPE_PRECISION (type)))
2159 warning (OPT_Woverflow,
2160 "overflow in implicit constant conversion");
2162 else
2163 conversion_warning (type, expr);
2165 else if ((TREE_CODE (result) == INTEGER_CST
2166 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2167 warning (OPT_Woverflow,
2168 "overflow in implicit constant conversion");
2169 else
2170 conversion_warning (type, expr);
2174 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2175 Invoke this function on every expression that is converted implicitly,
2176 i.e. because of language rules and not because of an explicit cast. */
2178 tree
2179 convert_and_check (tree type, tree expr)
2181 tree result;
2182 tree expr_for_warning;
2184 /* Convert from a value with possible excess precision rather than
2185 via the semantic type, but do not warn about values not fitting
2186 exactly in the semantic type. */
2187 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2189 tree orig_type = TREE_TYPE (expr);
2190 expr = TREE_OPERAND (expr, 0);
2191 expr_for_warning = convert (orig_type, expr);
2192 if (orig_type == type)
2193 return expr_for_warning;
2195 else
2196 expr_for_warning = expr;
2198 if (TREE_TYPE (expr) == type)
2199 return expr;
2201 result = convert (type, expr);
2203 if (c_inhibit_evaluation_warnings == 0
2204 && !TREE_OVERFLOW_P (expr)
2205 && result != error_mark_node)
2206 warnings_for_convert_and_check (type, expr_for_warning, result);
2208 return result;
2211 /* A node in a list that describes references to variables (EXPR), which are
2212 either read accesses if WRITER is zero, or write accesses, in which case
2213 WRITER is the parent of EXPR. */
2214 struct tlist
2216 struct tlist *next;
2217 tree expr, writer;
2220 /* Used to implement a cache the results of a call to verify_tree. We only
2221 use this for SAVE_EXPRs. */
2222 struct tlist_cache
2224 struct tlist_cache *next;
2225 struct tlist *cache_before_sp;
2226 struct tlist *cache_after_sp;
2227 tree expr;
2230 /* Obstack to use when allocating tlist structures, and corresponding
2231 firstobj. */
2232 static struct obstack tlist_obstack;
2233 static char *tlist_firstobj = 0;
2235 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2236 warnings. */
2237 static struct tlist *warned_ids;
2238 /* SAVE_EXPRs need special treatment. We process them only once and then
2239 cache the results. */
2240 static struct tlist_cache *save_expr_cache;
2242 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2243 static void merge_tlist (struct tlist **, struct tlist *, int);
2244 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2245 static int warning_candidate_p (tree);
2246 static bool candidate_equal_p (const_tree, const_tree);
2247 static void warn_for_collisions (struct tlist *);
2248 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2249 static struct tlist *new_tlist (struct tlist *, tree, tree);
2251 /* Create a new struct tlist and fill in its fields. */
2252 static struct tlist *
2253 new_tlist (struct tlist *next, tree t, tree writer)
2255 struct tlist *l;
2256 l = XOBNEW (&tlist_obstack, struct tlist);
2257 l->next = next;
2258 l->expr = t;
2259 l->writer = writer;
2260 return l;
2263 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2264 is nonnull, we ignore any node we find which has a writer equal to it. */
2266 static void
2267 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2269 while (add)
2271 struct tlist *next = add->next;
2272 if (!copy)
2273 add->next = *to;
2274 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2275 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2276 add = next;
2280 /* Merge the nodes of ADD into TO. This merging process is done so that for
2281 each variable that already exists in TO, no new node is added; however if
2282 there is a write access recorded in ADD, and an occurrence on TO is only
2283 a read access, then the occurrence in TO will be modified to record the
2284 write. */
2286 static void
2287 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2289 struct tlist **end = to;
2291 while (*end)
2292 end = &(*end)->next;
2294 while (add)
2296 int found = 0;
2297 struct tlist *tmp2;
2298 struct tlist *next = add->next;
2300 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2301 if (candidate_equal_p (tmp2->expr, add->expr))
2303 found = 1;
2304 if (!tmp2->writer)
2305 tmp2->writer = add->writer;
2307 if (!found)
2309 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
2310 end = &(*end)->next;
2311 *end = 0;
2313 add = next;
2317 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2318 references in list LIST conflict with it, excluding reads if ONLY writers
2319 is nonzero. */
2321 static void
2322 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2323 int only_writes)
2325 struct tlist *tmp;
2327 /* Avoid duplicate warnings. */
2328 for (tmp = warned_ids; tmp; tmp = tmp->next)
2329 if (candidate_equal_p (tmp->expr, written))
2330 return;
2332 while (list)
2334 if (candidate_equal_p (list->expr, written)
2335 && !candidate_equal_p (list->writer, writer)
2336 && (!only_writes || list->writer))
2338 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2339 warning_at (EXPR_LOC_OR_HERE (writer),
2340 OPT_Wsequence_point, "operation on %qE may be undefined",
2341 list->expr);
2343 list = list->next;
2347 /* Given a list LIST of references to variables, find whether any of these
2348 can cause conflicts due to missing sequence points. */
2350 static void
2351 warn_for_collisions (struct tlist *list)
2353 struct tlist *tmp;
2355 for (tmp = list; tmp; tmp = tmp->next)
2357 if (tmp->writer)
2358 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2362 /* Return nonzero if X is a tree that can be verified by the sequence point
2363 warnings. */
2364 static int
2365 warning_candidate_p (tree x)
2367 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2368 return 0;
2370 if (TREE_CODE (x) == BLOCK)
2371 return 0;
2373 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2374 (lvalue_p) crash on TRY/CATCH. */
2375 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2376 return 0;
2378 if (!lvalue_p (x))
2379 return 0;
2381 /* No point to track non-const calls, they will never satisfy
2382 operand_equal_p. */
2383 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2384 return 0;
2386 if (TREE_CODE (x) == STRING_CST)
2387 return 0;
2389 return 1;
2392 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2393 static bool
2394 candidate_equal_p (const_tree x, const_tree y)
2396 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2399 /* Walk the tree X, and record accesses to variables. If X is written by the
2400 parent tree, WRITER is the parent.
2401 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2402 expression or its only operand forces a sequence point, then everything up
2403 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2404 in PNO_SP.
2405 Once we return, we will have emitted warnings if any subexpression before
2406 such a sequence point could be undefined. On a higher level, however, the
2407 sequence point may not be relevant, and we'll merge the two lists.
2409 Example: (b++, a) + b;
2410 The call that processes the COMPOUND_EXPR will store the increment of B
2411 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2412 processes the PLUS_EXPR will need to merge the two lists so that
2413 eventually, all accesses end up on the same list (and we'll warn about the
2414 unordered subexpressions b++ and b.
2416 A note on merging. If we modify the former example so that our expression
2417 becomes
2418 (b++, b) + a
2419 care must be taken not simply to add all three expressions into the final
2420 PNO_SP list. The function merge_tlist takes care of that by merging the
2421 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2422 way, so that no more than one access to B is recorded. */
2424 static void
2425 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2426 tree writer)
2428 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2429 enum tree_code code;
2430 enum tree_code_class cl;
2432 /* X may be NULL if it is the operand of an empty statement expression
2433 ({ }). */
2434 if (x == NULL)
2435 return;
2437 restart:
2438 code = TREE_CODE (x);
2439 cl = TREE_CODE_CLASS (code);
2441 if (warning_candidate_p (x))
2442 *pno_sp = new_tlist (*pno_sp, x, writer);
2444 switch (code)
2446 case CONSTRUCTOR:
2447 return;
2449 case COMPOUND_EXPR:
2450 case TRUTH_ANDIF_EXPR:
2451 case TRUTH_ORIF_EXPR:
2452 tmp_before = tmp_nosp = tmp_list3 = 0;
2453 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2454 warn_for_collisions (tmp_nosp);
2455 merge_tlist (pbefore_sp, tmp_before, 0);
2456 merge_tlist (pbefore_sp, tmp_nosp, 0);
2457 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2458 merge_tlist (pbefore_sp, tmp_list3, 0);
2459 return;
2461 case COND_EXPR:
2462 tmp_before = tmp_list2 = 0;
2463 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2464 warn_for_collisions (tmp_list2);
2465 merge_tlist (pbefore_sp, tmp_before, 0);
2466 merge_tlist (pbefore_sp, tmp_list2, 1);
2468 tmp_list3 = tmp_nosp = 0;
2469 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2470 warn_for_collisions (tmp_nosp);
2471 merge_tlist (pbefore_sp, tmp_list3, 0);
2473 tmp_list3 = tmp_list2 = 0;
2474 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2475 warn_for_collisions (tmp_list2);
2476 merge_tlist (pbefore_sp, tmp_list3, 0);
2477 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2478 two first, to avoid warning for (a ? b++ : b++). */
2479 merge_tlist (&tmp_nosp, tmp_list2, 0);
2480 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2481 return;
2483 case PREDECREMENT_EXPR:
2484 case PREINCREMENT_EXPR:
2485 case POSTDECREMENT_EXPR:
2486 case POSTINCREMENT_EXPR:
2487 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2488 return;
2490 case MODIFY_EXPR:
2491 tmp_before = tmp_nosp = tmp_list3 = 0;
2492 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2493 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2494 /* Expressions inside the LHS are not ordered wrt. the sequence points
2495 in the RHS. Example:
2496 *a = (a++, 2)
2497 Despite the fact that the modification of "a" is in the before_sp
2498 list (tmp_before), it conflicts with the use of "a" in the LHS.
2499 We can handle this by adding the contents of tmp_list3
2500 to those of tmp_before, and redoing the collision warnings for that
2501 list. */
2502 add_tlist (&tmp_before, tmp_list3, x, 1);
2503 warn_for_collisions (tmp_before);
2504 /* Exclude the LHS itself here; we first have to merge it into the
2505 tmp_nosp list. This is done to avoid warning for "a = a"; if we
2506 didn't exclude the LHS, we'd get it twice, once as a read and once
2507 as a write. */
2508 add_tlist (pno_sp, tmp_list3, x, 0);
2509 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2511 merge_tlist (pbefore_sp, tmp_before, 0);
2512 if (warning_candidate_p (TREE_OPERAND (x, 0)))
2513 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2514 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2515 return;
2517 case CALL_EXPR:
2518 /* We need to warn about conflicts among arguments and conflicts between
2519 args and the function address. Side effects of the function address,
2520 however, are not ordered by the sequence point of the call. */
2522 call_expr_arg_iterator iter;
2523 tree arg;
2524 tmp_before = tmp_nosp = 0;
2525 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2526 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2528 tmp_list2 = tmp_list3 = 0;
2529 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2530 merge_tlist (&tmp_list3, tmp_list2, 0);
2531 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2533 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2534 warn_for_collisions (tmp_before);
2535 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2536 return;
2539 case TREE_LIST:
2540 /* Scan all the list, e.g. indices of multi dimensional array. */
2541 while (x)
2543 tmp_before = tmp_nosp = 0;
2544 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2545 merge_tlist (&tmp_nosp, tmp_before, 0);
2546 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2547 x = TREE_CHAIN (x);
2549 return;
2551 case SAVE_EXPR:
2553 struct tlist_cache *t;
2554 for (t = save_expr_cache; t; t = t->next)
2555 if (candidate_equal_p (t->expr, x))
2556 break;
2558 if (!t)
2560 t = XOBNEW (&tlist_obstack, struct tlist_cache);
2561 t->next = save_expr_cache;
2562 t->expr = x;
2563 save_expr_cache = t;
2565 tmp_before = tmp_nosp = 0;
2566 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2567 warn_for_collisions (tmp_nosp);
2569 tmp_list3 = 0;
2570 while (tmp_nosp)
2572 struct tlist *t = tmp_nosp;
2573 tmp_nosp = t->next;
2574 merge_tlist (&tmp_list3, t, 0);
2576 t->cache_before_sp = tmp_before;
2577 t->cache_after_sp = tmp_list3;
2579 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2580 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2581 return;
2584 case ADDR_EXPR:
2585 x = TREE_OPERAND (x, 0);
2586 if (DECL_P (x))
2587 return;
2588 writer = 0;
2589 goto restart;
2591 default:
2592 /* For other expressions, simply recurse on their operands.
2593 Manual tail recursion for unary expressions.
2594 Other non-expressions need not be processed. */
2595 if (cl == tcc_unary)
2597 x = TREE_OPERAND (x, 0);
2598 writer = 0;
2599 goto restart;
2601 else if (IS_EXPR_CODE_CLASS (cl))
2603 int lp;
2604 int max = TREE_OPERAND_LENGTH (x);
2605 for (lp = 0; lp < max; lp++)
2607 tmp_before = tmp_nosp = 0;
2608 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2609 merge_tlist (&tmp_nosp, tmp_before, 0);
2610 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2613 return;
2617 /* Try to warn for undefined behavior in EXPR due to missing sequence
2618 points. */
2620 DEBUG_FUNCTION void
2621 verify_sequence_points (tree expr)
2623 struct tlist *before_sp = 0, *after_sp = 0;
2625 warned_ids = 0;
2626 save_expr_cache = 0;
2627 if (tlist_firstobj == 0)
2629 gcc_obstack_init (&tlist_obstack);
2630 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2633 verify_tree (expr, &before_sp, &after_sp, 0);
2634 warn_for_collisions (after_sp);
2635 obstack_free (&tlist_obstack, tlist_firstobj);
2638 /* Validate the expression after `case' and apply default promotions. */
2640 static tree
2641 check_case_value (tree value)
2643 if (value == NULL_TREE)
2644 return value;
2646 if (TREE_CODE (value) == INTEGER_CST)
2647 /* Promote char or short to int. */
2648 value = perform_integral_promotions (value);
2649 else if (value != error_mark_node)
2651 error ("case label does not reduce to an integer constant");
2652 value = error_mark_node;
2655 constant_expression_warning (value);
2657 return value;
2660 /* See if the case values LOW and HIGH are in the range of the original
2661 type (i.e. before the default conversion to int) of the switch testing
2662 expression.
2663 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2664 the type before promoting it. CASE_LOW_P is a pointer to the lower
2665 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2666 if the case is not a case range.
2667 The caller has to make sure that we are not called with NULL for
2668 CASE_LOW_P (i.e. the default case).
2669 Returns true if the case label is in range of ORIG_TYPE (saturated or
2670 untouched) or false if the label is out of range. */
2672 static bool
2673 check_case_bounds (tree type, tree orig_type,
2674 tree *case_low_p, tree *case_high_p)
2676 tree min_value, max_value;
2677 tree case_low = *case_low_p;
2678 tree case_high = case_high_p ? *case_high_p : case_low;
2680 /* If there was a problem with the original type, do nothing. */
2681 if (orig_type == error_mark_node)
2682 return true;
2684 min_value = TYPE_MIN_VALUE (orig_type);
2685 max_value = TYPE_MAX_VALUE (orig_type);
2687 /* Case label is less than minimum for type. */
2688 if (tree_int_cst_compare (case_low, min_value) < 0
2689 && tree_int_cst_compare (case_high, min_value) < 0)
2691 warning (0, "case label value is less than minimum value for type");
2692 return false;
2695 /* Case value is greater than maximum for type. */
2696 if (tree_int_cst_compare (case_low, max_value) > 0
2697 && tree_int_cst_compare (case_high, max_value) > 0)
2699 warning (0, "case label value exceeds maximum value for type");
2700 return false;
2703 /* Saturate lower case label value to minimum. */
2704 if (tree_int_cst_compare (case_high, min_value) >= 0
2705 && tree_int_cst_compare (case_low, min_value) < 0)
2707 warning (0, "lower value in case label range"
2708 " less than minimum value for type");
2709 case_low = min_value;
2712 /* Saturate upper case label value to maximum. */
2713 if (tree_int_cst_compare (case_low, max_value) <= 0
2714 && tree_int_cst_compare (case_high, max_value) > 0)
2716 warning (0, "upper value in case label range"
2717 " exceeds maximum value for type");
2718 case_high = max_value;
2721 if (*case_low_p != case_low)
2722 *case_low_p = convert (type, case_low);
2723 if (case_high_p && *case_high_p != case_high)
2724 *case_high_p = convert (type, case_high);
2726 return true;
2729 /* Return an integer type with BITS bits of precision,
2730 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2732 tree
2733 c_common_type_for_size (unsigned int bits, int unsignedp)
2735 if (bits == TYPE_PRECISION (integer_type_node))
2736 return unsignedp ? unsigned_type_node : integer_type_node;
2738 if (bits == TYPE_PRECISION (signed_char_type_node))
2739 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2741 if (bits == TYPE_PRECISION (short_integer_type_node))
2742 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2744 if (bits == TYPE_PRECISION (long_integer_type_node))
2745 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2747 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2748 return (unsignedp ? long_long_unsigned_type_node
2749 : long_long_integer_type_node);
2751 if (int128_integer_type_node
2752 && bits == TYPE_PRECISION (int128_integer_type_node))
2753 return (unsignedp ? int128_unsigned_type_node
2754 : int128_integer_type_node);
2756 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2757 return (unsignedp ? widest_unsigned_literal_type_node
2758 : widest_integer_literal_type_node);
2760 if (bits <= TYPE_PRECISION (intQI_type_node))
2761 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2763 if (bits <= TYPE_PRECISION (intHI_type_node))
2764 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2766 if (bits <= TYPE_PRECISION (intSI_type_node))
2767 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2769 if (bits <= TYPE_PRECISION (intDI_type_node))
2770 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2772 return 0;
2775 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2776 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2777 and saturating if SATP is nonzero, otherwise not saturating. */
2779 tree
2780 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2781 int unsignedp, int satp)
2783 enum machine_mode mode;
2784 if (ibit == 0)
2785 mode = unsignedp ? UQQmode : QQmode;
2786 else
2787 mode = unsignedp ? UHAmode : HAmode;
2789 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2790 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2791 break;
2793 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
2795 sorry ("GCC cannot support operators with integer types and "
2796 "fixed-point types that have too many integral and "
2797 "fractional bits together");
2798 return 0;
2801 return c_common_type_for_mode (mode, satp);
2804 /* Used for communication between c_common_type_for_mode and
2805 c_register_builtin_type. */
2806 static GTY(()) tree registered_builtin_types;
2808 /* Return a data type that has machine mode MODE.
2809 If the mode is an integer,
2810 then UNSIGNEDP selects between signed and unsigned types.
2811 If the mode is a fixed-point mode,
2812 then UNSIGNEDP selects between saturating and nonsaturating types. */
2814 tree
2815 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
2817 tree t;
2819 if (mode == TYPE_MODE (integer_type_node))
2820 return unsignedp ? unsigned_type_node : integer_type_node;
2822 if (mode == TYPE_MODE (signed_char_type_node))
2823 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2825 if (mode == TYPE_MODE (short_integer_type_node))
2826 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2828 if (mode == TYPE_MODE (long_integer_type_node))
2829 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2831 if (mode == TYPE_MODE (long_long_integer_type_node))
2832 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2834 if (int128_integer_type_node
2835 && mode == TYPE_MODE (int128_integer_type_node))
2836 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
2838 if (mode == TYPE_MODE (widest_integer_literal_type_node))
2839 return unsignedp ? widest_unsigned_literal_type_node
2840 : widest_integer_literal_type_node;
2842 if (mode == QImode)
2843 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2845 if (mode == HImode)
2846 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2848 if (mode == SImode)
2849 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2851 if (mode == DImode)
2852 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2854 #if HOST_BITS_PER_WIDE_INT >= 64
2855 if (mode == TYPE_MODE (intTI_type_node))
2856 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2857 #endif
2859 if (mode == TYPE_MODE (float_type_node))
2860 return float_type_node;
2862 if (mode == TYPE_MODE (double_type_node))
2863 return double_type_node;
2865 if (mode == TYPE_MODE (long_double_type_node))
2866 return long_double_type_node;
2868 if (mode == TYPE_MODE (void_type_node))
2869 return void_type_node;
2871 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2872 return (unsignedp
2873 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2874 : make_signed_type (GET_MODE_PRECISION (mode)));
2876 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2877 return (unsignedp
2878 ? make_unsigned_type (GET_MODE_PRECISION (mode))
2879 : make_signed_type (GET_MODE_PRECISION (mode)));
2881 if (COMPLEX_MODE_P (mode))
2883 enum machine_mode inner_mode;
2884 tree inner_type;
2886 if (mode == TYPE_MODE (complex_float_type_node))
2887 return complex_float_type_node;
2888 if (mode == TYPE_MODE (complex_double_type_node))
2889 return complex_double_type_node;
2890 if (mode == TYPE_MODE (complex_long_double_type_node))
2891 return complex_long_double_type_node;
2893 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2894 return complex_integer_type_node;
2896 inner_mode = GET_MODE_INNER (mode);
2897 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2898 if (inner_type != NULL_TREE)
2899 return build_complex_type (inner_type);
2901 else if (VECTOR_MODE_P (mode))
2903 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2904 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2905 if (inner_type != NULL_TREE)
2906 return build_vector_type_for_mode (inner_type, mode);
2909 if (mode == TYPE_MODE (dfloat32_type_node))
2910 return dfloat32_type_node;
2911 if (mode == TYPE_MODE (dfloat64_type_node))
2912 return dfloat64_type_node;
2913 if (mode == TYPE_MODE (dfloat128_type_node))
2914 return dfloat128_type_node;
2916 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2918 if (mode == TYPE_MODE (short_fract_type_node))
2919 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2920 if (mode == TYPE_MODE (fract_type_node))
2921 return unsignedp ? sat_fract_type_node : fract_type_node;
2922 if (mode == TYPE_MODE (long_fract_type_node))
2923 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2924 if (mode == TYPE_MODE (long_long_fract_type_node))
2925 return unsignedp ? sat_long_long_fract_type_node
2926 : long_long_fract_type_node;
2928 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2929 return unsignedp ? sat_unsigned_short_fract_type_node
2930 : unsigned_short_fract_type_node;
2931 if (mode == TYPE_MODE (unsigned_fract_type_node))
2932 return unsignedp ? sat_unsigned_fract_type_node
2933 : unsigned_fract_type_node;
2934 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2935 return unsignedp ? sat_unsigned_long_fract_type_node
2936 : unsigned_long_fract_type_node;
2937 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2938 return unsignedp ? sat_unsigned_long_long_fract_type_node
2939 : unsigned_long_long_fract_type_node;
2941 if (mode == TYPE_MODE (short_accum_type_node))
2942 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2943 if (mode == TYPE_MODE (accum_type_node))
2944 return unsignedp ? sat_accum_type_node : accum_type_node;
2945 if (mode == TYPE_MODE (long_accum_type_node))
2946 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2947 if (mode == TYPE_MODE (long_long_accum_type_node))
2948 return unsignedp ? sat_long_long_accum_type_node
2949 : long_long_accum_type_node;
2951 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2952 return unsignedp ? sat_unsigned_short_accum_type_node
2953 : unsigned_short_accum_type_node;
2954 if (mode == TYPE_MODE (unsigned_accum_type_node))
2955 return unsignedp ? sat_unsigned_accum_type_node
2956 : unsigned_accum_type_node;
2957 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2958 return unsignedp ? sat_unsigned_long_accum_type_node
2959 : unsigned_long_accum_type_node;
2960 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2961 return unsignedp ? sat_unsigned_long_long_accum_type_node
2962 : unsigned_long_long_accum_type_node;
2964 if (mode == QQmode)
2965 return unsignedp ? sat_qq_type_node : qq_type_node;
2966 if (mode == HQmode)
2967 return unsignedp ? sat_hq_type_node : hq_type_node;
2968 if (mode == SQmode)
2969 return unsignedp ? sat_sq_type_node : sq_type_node;
2970 if (mode == DQmode)
2971 return unsignedp ? sat_dq_type_node : dq_type_node;
2972 if (mode == TQmode)
2973 return unsignedp ? sat_tq_type_node : tq_type_node;
2975 if (mode == UQQmode)
2976 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2977 if (mode == UHQmode)
2978 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2979 if (mode == USQmode)
2980 return unsignedp ? sat_usq_type_node : usq_type_node;
2981 if (mode == UDQmode)
2982 return unsignedp ? sat_udq_type_node : udq_type_node;
2983 if (mode == UTQmode)
2984 return unsignedp ? sat_utq_type_node : utq_type_node;
2986 if (mode == HAmode)
2987 return unsignedp ? sat_ha_type_node : ha_type_node;
2988 if (mode == SAmode)
2989 return unsignedp ? sat_sa_type_node : sa_type_node;
2990 if (mode == DAmode)
2991 return unsignedp ? sat_da_type_node : da_type_node;
2992 if (mode == TAmode)
2993 return unsignedp ? sat_ta_type_node : ta_type_node;
2995 if (mode == UHAmode)
2996 return unsignedp ? sat_uha_type_node : uha_type_node;
2997 if (mode == USAmode)
2998 return unsignedp ? sat_usa_type_node : usa_type_node;
2999 if (mode == UDAmode)
3000 return unsignedp ? sat_uda_type_node : uda_type_node;
3001 if (mode == UTAmode)
3002 return unsignedp ? sat_uta_type_node : uta_type_node;
3005 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3006 if (TYPE_MODE (TREE_VALUE (t)) == mode)
3007 return TREE_VALUE (t);
3009 return 0;
3012 tree
3013 c_common_unsigned_type (tree type)
3015 return c_common_signed_or_unsigned_type (1, type);
3018 /* Return a signed type the same as TYPE in other respects. */
3020 tree
3021 c_common_signed_type (tree type)
3023 return c_common_signed_or_unsigned_type (0, type);
3026 /* Return a type the same as TYPE except unsigned or
3027 signed according to UNSIGNEDP. */
3029 tree
3030 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3032 tree type1;
3034 /* This block of code emulates the behavior of the old
3035 c_common_unsigned_type. In particular, it returns
3036 long_unsigned_type_node if passed a long, even when a int would
3037 have the same size. This is necessary for warnings to work
3038 correctly in archs where sizeof(int) == sizeof(long) */
3040 type1 = TYPE_MAIN_VARIANT (type);
3041 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3042 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3043 if (type1 == integer_type_node || type1 == unsigned_type_node)
3044 return unsignedp ? unsigned_type_node : integer_type_node;
3045 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3046 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3047 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3048 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3049 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3050 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3051 if (int128_integer_type_node
3052 && (type1 == int128_integer_type_node
3053 || type1 == int128_unsigned_type_node))
3054 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3055 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3056 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3057 #if HOST_BITS_PER_WIDE_INT >= 64
3058 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3059 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3060 #endif
3061 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3062 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3063 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3064 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3065 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3066 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3067 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3068 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3070 #define C_COMMON_FIXED_TYPES(NAME) \
3071 if (type1 == short_ ## NAME ## _type_node \
3072 || type1 == unsigned_short_ ## NAME ## _type_node) \
3073 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3074 : short_ ## NAME ## _type_node; \
3075 if (type1 == NAME ## _type_node \
3076 || type1 == unsigned_ ## NAME ## _type_node) \
3077 return unsignedp ? unsigned_ ## NAME ## _type_node \
3078 : NAME ## _type_node; \
3079 if (type1 == long_ ## NAME ## _type_node \
3080 || type1 == unsigned_long_ ## NAME ## _type_node) \
3081 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3082 : long_ ## NAME ## _type_node; \
3083 if (type1 == long_long_ ## NAME ## _type_node \
3084 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3085 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3086 : long_long_ ## NAME ## _type_node;
3088 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3089 if (type1 == NAME ## _type_node \
3090 || type1 == u ## NAME ## _type_node) \
3091 return unsignedp ? u ## NAME ## _type_node \
3092 : NAME ## _type_node;
3094 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3095 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3096 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3097 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3098 : sat_ ## short_ ## NAME ## _type_node; \
3099 if (type1 == sat_ ## NAME ## _type_node \
3100 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3101 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3102 : sat_ ## NAME ## _type_node; \
3103 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3104 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3105 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3106 : sat_ ## long_ ## NAME ## _type_node; \
3107 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3108 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3109 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3110 : sat_ ## long_long_ ## NAME ## _type_node;
3112 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3113 if (type1 == sat_ ## NAME ## _type_node \
3114 || type1 == sat_ ## u ## NAME ## _type_node) \
3115 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3116 : sat_ ## NAME ## _type_node;
3118 C_COMMON_FIXED_TYPES (fract);
3119 C_COMMON_FIXED_TYPES_SAT (fract);
3120 C_COMMON_FIXED_TYPES (accum);
3121 C_COMMON_FIXED_TYPES_SAT (accum);
3123 C_COMMON_FIXED_MODE_TYPES (qq);
3124 C_COMMON_FIXED_MODE_TYPES (hq);
3125 C_COMMON_FIXED_MODE_TYPES (sq);
3126 C_COMMON_FIXED_MODE_TYPES (dq);
3127 C_COMMON_FIXED_MODE_TYPES (tq);
3128 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3129 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3130 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3131 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3132 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3133 C_COMMON_FIXED_MODE_TYPES (ha);
3134 C_COMMON_FIXED_MODE_TYPES (sa);
3135 C_COMMON_FIXED_MODE_TYPES (da);
3136 C_COMMON_FIXED_MODE_TYPES (ta);
3137 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3138 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3139 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3140 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3142 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3143 the precision; they have precision set to match their range, but
3144 may use a wider mode to match an ABI. If we change modes, we may
3145 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3146 the precision as well, so as to yield correct results for
3147 bit-field types. C++ does not have these separate bit-field
3148 types, and producing a signed or unsigned variant of an
3149 ENUMERAL_TYPE may cause other problems as well. */
3151 if (!INTEGRAL_TYPE_P (type)
3152 || TYPE_UNSIGNED (type) == unsignedp)
3153 return type;
3155 #define TYPE_OK(node) \
3156 (TYPE_MODE (type) == TYPE_MODE (node) \
3157 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3158 if (TYPE_OK (signed_char_type_node))
3159 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3160 if (TYPE_OK (integer_type_node))
3161 return unsignedp ? unsigned_type_node : integer_type_node;
3162 if (TYPE_OK (short_integer_type_node))
3163 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3164 if (TYPE_OK (long_integer_type_node))
3165 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3166 if (TYPE_OK (long_long_integer_type_node))
3167 return (unsignedp ? long_long_unsigned_type_node
3168 : long_long_integer_type_node);
3169 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3170 return (unsignedp ? int128_unsigned_type_node
3171 : int128_integer_type_node);
3172 if (TYPE_OK (widest_integer_literal_type_node))
3173 return (unsignedp ? widest_unsigned_literal_type_node
3174 : widest_integer_literal_type_node);
3176 #if HOST_BITS_PER_WIDE_INT >= 64
3177 if (TYPE_OK (intTI_type_node))
3178 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3179 #endif
3180 if (TYPE_OK (intDI_type_node))
3181 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3182 if (TYPE_OK (intSI_type_node))
3183 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3184 if (TYPE_OK (intHI_type_node))
3185 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3186 if (TYPE_OK (intQI_type_node))
3187 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3188 #undef TYPE_OK
3190 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3193 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3195 tree
3196 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3198 /* Extended integer types of the same width as a standard type have
3199 lesser rank, so those of the same width as int promote to int or
3200 unsigned int and are valid for printf formats expecting int or
3201 unsigned int. To avoid such special cases, avoid creating
3202 extended integer types for bit-fields if a standard integer type
3203 is available. */
3204 if (width == TYPE_PRECISION (integer_type_node))
3205 return unsignedp ? unsigned_type_node : integer_type_node;
3206 if (width == TYPE_PRECISION (signed_char_type_node))
3207 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3208 if (width == TYPE_PRECISION (short_integer_type_node))
3209 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3210 if (width == TYPE_PRECISION (long_integer_type_node))
3211 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3212 if (width == TYPE_PRECISION (long_long_integer_type_node))
3213 return (unsignedp ? long_long_unsigned_type_node
3214 : long_long_integer_type_node);
3215 if (int128_integer_type_node
3216 && width == TYPE_PRECISION (int128_integer_type_node))
3217 return (unsignedp ? int128_unsigned_type_node
3218 : int128_integer_type_node);
3219 return build_nonstandard_integer_type (width, unsignedp);
3222 /* The C version of the register_builtin_type langhook. */
3224 void
3225 c_register_builtin_type (tree type, const char* name)
3227 tree decl;
3229 decl = build_decl (UNKNOWN_LOCATION,
3230 TYPE_DECL, get_identifier (name), type);
3231 DECL_ARTIFICIAL (decl) = 1;
3232 if (!TYPE_NAME (type))
3233 TYPE_NAME (type) = decl;
3234 pushdecl (decl);
3236 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3239 /* Print an error message for invalid operands to arith operation
3240 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3241 LOCATION is the location of the message. */
3243 void
3244 binary_op_error (location_t location, enum tree_code code,
3245 tree type0, tree type1)
3247 const char *opname;
3249 switch (code)
3251 case PLUS_EXPR:
3252 opname = "+"; break;
3253 case MINUS_EXPR:
3254 opname = "-"; break;
3255 case MULT_EXPR:
3256 opname = "*"; break;
3257 case MAX_EXPR:
3258 opname = "max"; break;
3259 case MIN_EXPR:
3260 opname = "min"; break;
3261 case EQ_EXPR:
3262 opname = "=="; break;
3263 case NE_EXPR:
3264 opname = "!="; break;
3265 case LE_EXPR:
3266 opname = "<="; break;
3267 case GE_EXPR:
3268 opname = ">="; break;
3269 case LT_EXPR:
3270 opname = "<"; break;
3271 case GT_EXPR:
3272 opname = ">"; break;
3273 case LSHIFT_EXPR:
3274 opname = "<<"; break;
3275 case RSHIFT_EXPR:
3276 opname = ">>"; break;
3277 case TRUNC_MOD_EXPR:
3278 case FLOOR_MOD_EXPR:
3279 opname = "%"; break;
3280 case TRUNC_DIV_EXPR:
3281 case FLOOR_DIV_EXPR:
3282 opname = "/"; break;
3283 case BIT_AND_EXPR:
3284 opname = "&"; break;
3285 case BIT_IOR_EXPR:
3286 opname = "|"; break;
3287 case TRUTH_ANDIF_EXPR:
3288 opname = "&&"; break;
3289 case TRUTH_ORIF_EXPR:
3290 opname = "||"; break;
3291 case BIT_XOR_EXPR:
3292 opname = "^"; break;
3293 default:
3294 gcc_unreachable ();
3296 error_at (location,
3297 "invalid operands to binary %s (have %qT and %qT)", opname,
3298 type0, type1);
3301 /* Subroutine of build_binary_op, used for comparison operations.
3302 See if the operands have both been converted from subword integer types
3303 and, if so, perhaps change them both back to their original type.
3304 This function is also responsible for converting the two operands
3305 to the proper common type for comparison.
3307 The arguments of this function are all pointers to local variables
3308 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3309 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3311 If this function returns nonzero, it means that the comparison has
3312 a constant value. What this function returns is an expression for
3313 that value. */
3315 tree
3316 shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr,
3317 enum tree_code *rescode_ptr)
3319 tree type;
3320 tree op0 = *op0_ptr;
3321 tree op1 = *op1_ptr;
3322 int unsignedp0, unsignedp1;
3323 int real1, real2;
3324 tree primop0, primop1;
3325 enum tree_code code = *rescode_ptr;
3327 /* Throw away any conversions to wider types
3328 already present in the operands. */
3330 primop0 = c_common_get_narrower (op0, &unsignedp0);
3331 primop1 = c_common_get_narrower (op1, &unsignedp1);
3333 /* If primopN is first sign-extended from primopN's precision to opN's
3334 precision, then zero-extended from opN's precision to
3335 *restype_ptr precision, shortenings might be invalid. */
3336 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3337 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3338 && !unsignedp0
3339 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3340 primop0 = op0;
3341 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3342 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3343 && !unsignedp1
3344 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3345 primop1 = op1;
3347 /* Handle the case that OP0 does not *contain* a conversion
3348 but it *requires* conversion to FINAL_TYPE. */
3350 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3351 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3352 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3353 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3355 /* If one of the operands must be floated, we cannot optimize. */
3356 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3357 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3359 /* If first arg is constant, swap the args (changing operation
3360 so value is preserved), for canonicalization. Don't do this if
3361 the second arg is 0. */
3363 if (TREE_CONSTANT (primop0)
3364 && !integer_zerop (primop1) && !real_zerop (primop1)
3365 && !fixed_zerop (primop1))
3367 tree tem = primop0;
3368 int temi = unsignedp0;
3369 primop0 = primop1;
3370 primop1 = tem;
3371 tem = op0;
3372 op0 = op1;
3373 op1 = tem;
3374 *op0_ptr = op0;
3375 *op1_ptr = op1;
3376 unsignedp0 = unsignedp1;
3377 unsignedp1 = temi;
3378 temi = real1;
3379 real1 = real2;
3380 real2 = temi;
3382 switch (code)
3384 case LT_EXPR:
3385 code = GT_EXPR;
3386 break;
3387 case GT_EXPR:
3388 code = LT_EXPR;
3389 break;
3390 case LE_EXPR:
3391 code = GE_EXPR;
3392 break;
3393 case GE_EXPR:
3394 code = LE_EXPR;
3395 break;
3396 default:
3397 break;
3399 *rescode_ptr = code;
3402 /* If comparing an integer against a constant more bits wide,
3403 maybe we can deduce a value of 1 or 0 independent of the data.
3404 Or else truncate the constant now
3405 rather than extend the variable at run time.
3407 This is only interesting if the constant is the wider arg.
3408 Also, it is not safe if the constant is unsigned and the
3409 variable arg is signed, since in this case the variable
3410 would be sign-extended and then regarded as unsigned.
3411 Our technique fails in this case because the lowest/highest
3412 possible unsigned results don't follow naturally from the
3413 lowest/highest possible values of the variable operand.
3414 For just EQ_EXPR and NE_EXPR there is another technique that
3415 could be used: see if the constant can be faithfully represented
3416 in the other operand's type, by truncating it and reextending it
3417 and see if that preserves the constant's value. */
3419 if (!real1 && !real2
3420 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3421 && TREE_CODE (primop1) == INTEGER_CST
3422 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3424 int min_gt, max_gt, min_lt, max_lt;
3425 tree maxval, minval;
3426 /* 1 if comparison is nominally unsigned. */
3427 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3428 tree val;
3430 type = c_common_signed_or_unsigned_type (unsignedp0,
3431 TREE_TYPE (primop0));
3433 maxval = TYPE_MAX_VALUE (type);
3434 minval = TYPE_MIN_VALUE (type);
3436 if (unsignedp && !unsignedp0)
3437 *restype_ptr = c_common_signed_type (*restype_ptr);
3439 if (TREE_TYPE (primop1) != *restype_ptr)
3441 /* Convert primop1 to target type, but do not introduce
3442 additional overflow. We know primop1 is an int_cst. */
3443 primop1 = force_fit_type_double (*restype_ptr,
3444 tree_to_double_int (primop1),
3445 0, TREE_OVERFLOW (primop1));
3447 if (type != *restype_ptr)
3449 minval = convert (*restype_ptr, minval);
3450 maxval = convert (*restype_ptr, maxval);
3453 if (unsignedp && unsignedp0)
3455 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3456 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3457 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3458 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3460 else
3462 min_gt = INT_CST_LT (primop1, minval);
3463 max_gt = INT_CST_LT (primop1, maxval);
3464 min_lt = INT_CST_LT (minval, primop1);
3465 max_lt = INT_CST_LT (maxval, primop1);
3468 val = 0;
3469 /* This used to be a switch, but Genix compiler can't handle that. */
3470 if (code == NE_EXPR)
3472 if (max_lt || min_gt)
3473 val = truthvalue_true_node;
3475 else if (code == EQ_EXPR)
3477 if (max_lt || min_gt)
3478 val = truthvalue_false_node;
3480 else if (code == LT_EXPR)
3482 if (max_lt)
3483 val = truthvalue_true_node;
3484 if (!min_lt)
3485 val = truthvalue_false_node;
3487 else if (code == GT_EXPR)
3489 if (min_gt)
3490 val = truthvalue_true_node;
3491 if (!max_gt)
3492 val = truthvalue_false_node;
3494 else if (code == LE_EXPR)
3496 if (!max_gt)
3497 val = truthvalue_true_node;
3498 if (min_gt)
3499 val = truthvalue_false_node;
3501 else if (code == GE_EXPR)
3503 if (!min_lt)
3504 val = truthvalue_true_node;
3505 if (max_lt)
3506 val = truthvalue_false_node;
3509 /* If primop0 was sign-extended and unsigned comparison specd,
3510 we did a signed comparison above using the signed type bounds.
3511 But the comparison we output must be unsigned.
3513 Also, for inequalities, VAL is no good; but if the signed
3514 comparison had *any* fixed result, it follows that the
3515 unsigned comparison just tests the sign in reverse
3516 (positive values are LE, negative ones GE).
3517 So we can generate an unsigned comparison
3518 against an extreme value of the signed type. */
3520 if (unsignedp && !unsignedp0)
3522 if (val != 0)
3523 switch (code)
3525 case LT_EXPR:
3526 case GE_EXPR:
3527 primop1 = TYPE_MIN_VALUE (type);
3528 val = 0;
3529 break;
3531 case LE_EXPR:
3532 case GT_EXPR:
3533 primop1 = TYPE_MAX_VALUE (type);
3534 val = 0;
3535 break;
3537 default:
3538 break;
3540 type = c_common_unsigned_type (type);
3543 if (TREE_CODE (primop0) != INTEGER_CST)
3545 if (val == truthvalue_false_node)
3546 warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type");
3547 if (val == truthvalue_true_node)
3548 warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type");
3551 if (val != 0)
3553 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3554 if (TREE_SIDE_EFFECTS (primop0))
3555 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3556 return val;
3559 /* Value is not predetermined, but do the comparison
3560 in the type of the operand that is not constant.
3561 TYPE is already properly set. */
3564 /* If either arg is decimal float and the other is float, find the
3565 proper common type to use for comparison. */
3566 else if (real1 && real2
3567 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3568 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3569 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3571 else if (real1 && real2
3572 && (TYPE_PRECISION (TREE_TYPE (primop0))
3573 == TYPE_PRECISION (TREE_TYPE (primop1))))
3574 type = TREE_TYPE (primop0);
3576 /* If args' natural types are both narrower than nominal type
3577 and both extend in the same manner, compare them
3578 in the type of the wider arg.
3579 Otherwise must actually extend both to the nominal
3580 common type lest different ways of extending
3581 alter the result.
3582 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3584 else if (unsignedp0 == unsignedp1 && real1 == real2
3585 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3586 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3588 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3589 type = c_common_signed_or_unsigned_type (unsignedp0
3590 || TYPE_UNSIGNED (*restype_ptr),
3591 type);
3592 /* Make sure shorter operand is extended the right way
3593 to match the longer operand. */
3594 primop0
3595 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3596 TREE_TYPE (primop0)),
3597 primop0);
3598 primop1
3599 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3600 TREE_TYPE (primop1)),
3601 primop1);
3603 else
3605 /* Here we must do the comparison on the nominal type
3606 using the args exactly as we received them. */
3607 type = *restype_ptr;
3608 primop0 = op0;
3609 primop1 = op1;
3611 if (!real1 && !real2 && integer_zerop (primop1)
3612 && TYPE_UNSIGNED (*restype_ptr))
3614 tree value = 0;
3615 switch (code)
3617 case GE_EXPR:
3618 /* All unsigned values are >= 0, so we warn. However,
3619 if OP0 is a constant that is >= 0, the signedness of
3620 the comparison isn't an issue, so suppress the
3621 warning. */
3622 if (warn_type_limits && !in_system_header
3623 && !(TREE_CODE (primop0) == INTEGER_CST
3624 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3625 primop0))))
3626 warning (OPT_Wtype_limits,
3627 "comparison of unsigned expression >= 0 is always true");
3628 value = truthvalue_true_node;
3629 break;
3631 case LT_EXPR:
3632 if (warn_type_limits && !in_system_header
3633 && !(TREE_CODE (primop0) == INTEGER_CST
3634 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3635 primop0))))
3636 warning (OPT_Wtype_limits,
3637 "comparison of unsigned expression < 0 is always false");
3638 value = truthvalue_false_node;
3639 break;
3641 default:
3642 break;
3645 if (value != 0)
3647 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3648 if (TREE_SIDE_EFFECTS (primop0))
3649 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3650 primop0, value);
3651 return value;
3656 *op0_ptr = convert (type, primop0);
3657 *op1_ptr = convert (type, primop1);
3659 *restype_ptr = truthvalue_type_node;
3661 return 0;
3664 /* Return a tree for the sum or difference (RESULTCODE says which)
3665 of pointer PTROP and integer INTOP. */
3667 tree
3668 pointer_int_sum (location_t loc, enum tree_code resultcode,
3669 tree ptrop, tree intop)
3671 tree size_exp, ret;
3673 /* The result is a pointer of the same type that is being added. */
3674 tree result_type = TREE_TYPE (ptrop);
3676 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3678 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3679 "pointer of type %<void *%> used in arithmetic");
3680 size_exp = integer_one_node;
3682 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3684 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3685 "pointer to a function used in arithmetic");
3686 size_exp = integer_one_node;
3688 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3690 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3691 "pointer to member function used in arithmetic");
3692 size_exp = integer_one_node;
3694 else
3695 size_exp = size_in_bytes (TREE_TYPE (result_type));
3697 /* We are manipulating pointer values, so we don't need to warn
3698 about relying on undefined signed overflow. We disable the
3699 warning here because we use integer types so fold won't know that
3700 they are really pointers. */
3701 fold_defer_overflow_warnings ();
3703 /* If what we are about to multiply by the size of the elements
3704 contains a constant term, apply distributive law
3705 and multiply that constant term separately.
3706 This helps produce common subexpressions. */
3707 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3708 && !TREE_CONSTANT (intop)
3709 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3710 && TREE_CONSTANT (size_exp)
3711 /* If the constant comes from pointer subtraction,
3712 skip this optimization--it would cause an error. */
3713 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3714 /* If the constant is unsigned, and smaller than the pointer size,
3715 then we must skip this optimization. This is because it could cause
3716 an overflow error if the constant is negative but INTOP is not. */
3717 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
3718 || (TYPE_PRECISION (TREE_TYPE (intop))
3719 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3721 enum tree_code subcode = resultcode;
3722 tree int_type = TREE_TYPE (intop);
3723 if (TREE_CODE (intop) == MINUS_EXPR)
3724 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3725 /* Convert both subexpression types to the type of intop,
3726 because weird cases involving pointer arithmetic
3727 can result in a sum or difference with different type args. */
3728 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3729 subcode, ptrop,
3730 convert (int_type, TREE_OPERAND (intop, 1)), 1);
3731 intop = convert (int_type, TREE_OPERAND (intop, 0));
3734 /* Convert the integer argument to a type the same size as sizetype
3735 so the multiply won't overflow spuriously. */
3736 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3737 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3738 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3739 TYPE_UNSIGNED (sizetype)), intop);
3741 /* Replace the integer argument with a suitable product by the object size.
3742 Do this multiplication as signed, then convert to the appropriate type
3743 for the pointer operation and disregard an overflow that occured only
3744 because of the sign-extension change in the latter conversion. */
3746 tree t = build_binary_op (loc,
3747 MULT_EXPR, intop,
3748 convert (TREE_TYPE (intop), size_exp), 1);
3749 intop = convert (sizetype, t);
3750 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3751 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
3752 TREE_INT_CST_HIGH (intop));
3755 /* Create the sum or difference. */
3756 if (resultcode == MINUS_EXPR)
3757 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3759 ret = fold_build2_loc (loc, POINTER_PLUS_EXPR, result_type, ptrop, intop);
3761 fold_undefer_and_ignore_overflow_warnings ();
3763 return ret;
3766 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3767 and if NON_CONST is known not to be permitted in an evaluated part
3768 of a constant expression. */
3770 tree
3771 c_wrap_maybe_const (tree expr, bool non_const)
3773 bool nowarning = TREE_NO_WARNING (expr);
3774 location_t loc = EXPR_LOCATION (expr);
3776 /* This should never be called for C++. */
3777 if (c_dialect_cxx ())
3778 gcc_unreachable ();
3780 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3781 STRIP_TYPE_NOPS (expr);
3782 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3783 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3784 if (nowarning)
3785 TREE_NO_WARNING (expr) = 1;
3786 protected_set_expr_location (expr, loc);
3788 return expr;
3791 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
3792 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
3793 around the SAVE_EXPR if needed so that c_fully_fold does not need
3794 to look inside SAVE_EXPRs. */
3796 tree
3797 c_save_expr (tree expr)
3799 bool maybe_const = true;
3800 if (c_dialect_cxx ())
3801 return save_expr (expr);
3802 expr = c_fully_fold (expr, false, &maybe_const);
3803 expr = save_expr (expr);
3804 if (!maybe_const)
3805 expr = c_wrap_maybe_const (expr, true);
3806 return expr;
3809 /* Return whether EXPR is a declaration whose address can never be
3810 NULL. */
3812 bool
3813 decl_with_nonnull_addr_p (const_tree expr)
3815 return (DECL_P (expr)
3816 && (TREE_CODE (expr) == PARM_DECL
3817 || TREE_CODE (expr) == LABEL_DECL
3818 || !DECL_WEAK (expr)));
3821 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3822 or for an `if' or `while' statement or ?..: exp. It should already
3823 have been validated to be of suitable type; otherwise, a bad
3824 diagnostic may result.
3826 The EXPR is located at LOCATION.
3828 This preparation consists of taking the ordinary
3829 representation of an expression expr and producing a valid tree
3830 boolean expression describing whether expr is nonzero. We could
3831 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3832 but we optimize comparisons, &&, ||, and !.
3834 The resulting type should always be `truthvalue_type_node'. */
3836 tree
3837 c_common_truthvalue_conversion (location_t location, tree expr)
3839 switch (TREE_CODE (expr))
3841 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3842 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3843 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3844 case ORDERED_EXPR: case UNORDERED_EXPR:
3845 if (TREE_TYPE (expr) == truthvalue_type_node)
3846 return expr;
3847 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3848 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3849 goto ret;
3851 case TRUTH_ANDIF_EXPR:
3852 case TRUTH_ORIF_EXPR:
3853 case TRUTH_AND_EXPR:
3854 case TRUTH_OR_EXPR:
3855 case TRUTH_XOR_EXPR:
3856 if (TREE_TYPE (expr) == truthvalue_type_node)
3857 return expr;
3858 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3859 c_common_truthvalue_conversion (location,
3860 TREE_OPERAND (expr, 0)),
3861 c_common_truthvalue_conversion (location,
3862 TREE_OPERAND (expr, 1)));
3863 goto ret;
3865 case TRUTH_NOT_EXPR:
3866 if (TREE_TYPE (expr) == truthvalue_type_node)
3867 return expr;
3868 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3869 c_common_truthvalue_conversion (location,
3870 TREE_OPERAND (expr, 0)));
3871 goto ret;
3873 case ERROR_MARK:
3874 return expr;
3876 case INTEGER_CST:
3877 return integer_zerop (expr) ? truthvalue_false_node
3878 : truthvalue_true_node;
3880 case REAL_CST:
3881 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3882 ? truthvalue_true_node
3883 : truthvalue_false_node;
3885 case FIXED_CST:
3886 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3887 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3888 ? truthvalue_true_node
3889 : truthvalue_false_node;
3891 case FUNCTION_DECL:
3892 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
3893 /* Fall through. */
3895 case ADDR_EXPR:
3897 tree inner = TREE_OPERAND (expr, 0);
3898 if (decl_with_nonnull_addr_p (inner))
3900 /* Common Ada/Pascal programmer's mistake. */
3901 warning_at (location,
3902 OPT_Waddress,
3903 "the address of %qD will always evaluate as %<true%>",
3904 inner);
3905 return truthvalue_true_node;
3907 break;
3910 case COMPLEX_EXPR:
3911 expr = build_binary_op (EXPR_LOCATION (expr),
3912 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3913 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3914 c_common_truthvalue_conversion (location,
3915 TREE_OPERAND (expr, 0)),
3916 c_common_truthvalue_conversion (location,
3917 TREE_OPERAND (expr, 1)),
3919 goto ret;
3921 case NEGATE_EXPR:
3922 case ABS_EXPR:
3923 case FLOAT_EXPR:
3924 case EXCESS_PRECISION_EXPR:
3925 /* These don't change whether an object is nonzero or zero. */
3926 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3928 case LROTATE_EXPR:
3929 case RROTATE_EXPR:
3930 /* These don't change whether an object is zero or nonzero, but
3931 we can't ignore them if their second arg has side-effects. */
3932 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3934 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3935 TREE_OPERAND (expr, 1),
3936 c_common_truthvalue_conversion
3937 (location, TREE_OPERAND (expr, 0)));
3938 goto ret;
3940 else
3941 return c_common_truthvalue_conversion (location,
3942 TREE_OPERAND (expr, 0));
3944 case COND_EXPR:
3945 /* Distribute the conversion into the arms of a COND_EXPR. */
3946 if (c_dialect_cxx ())
3948 tree op1 = TREE_OPERAND (expr, 1);
3949 tree op2 = TREE_OPERAND (expr, 2);
3950 /* In C++ one of the arms might have void type if it is throw. */
3951 if (!VOID_TYPE_P (TREE_TYPE (op1)))
3952 op1 = c_common_truthvalue_conversion (location, op1);
3953 if (!VOID_TYPE_P (TREE_TYPE (op2)))
3954 op2 = c_common_truthvalue_conversion (location, op2);
3955 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
3956 TREE_OPERAND (expr, 0), op1, op2);
3957 goto ret;
3959 else
3961 /* Folding will happen later for C. */
3962 expr = build3 (COND_EXPR, truthvalue_type_node,
3963 TREE_OPERAND (expr, 0),
3964 c_common_truthvalue_conversion (location,
3965 TREE_OPERAND (expr, 1)),
3966 c_common_truthvalue_conversion (location,
3967 TREE_OPERAND (expr, 2)));
3968 goto ret;
3971 CASE_CONVERT:
3973 tree totype = TREE_TYPE (expr);
3974 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3976 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3977 since that affects how `default_conversion' will behave. */
3978 if (TREE_CODE (totype) == REFERENCE_TYPE
3979 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3980 break;
3981 /* Don't strip a conversion from C++0x scoped enum, since they
3982 don't implicitly convert to other types. */
3983 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3984 && ENUM_IS_SCOPED (fromtype))
3985 break;
3986 /* If this isn't narrowing the argument, we can ignore it. */
3987 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3988 return c_common_truthvalue_conversion (location,
3989 TREE_OPERAND (expr, 0));
3991 break;
3993 case MODIFY_EXPR:
3994 if (!TREE_NO_WARNING (expr)
3995 && warn_parentheses)
3997 warning (OPT_Wparentheses,
3998 "suggest parentheses around assignment used as truth value");
3999 TREE_NO_WARNING (expr) = 1;
4001 break;
4003 default:
4004 break;
4007 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4009 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4010 expr = (build_binary_op
4011 (EXPR_LOCATION (expr),
4012 (TREE_SIDE_EFFECTS (expr)
4013 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4014 c_common_truthvalue_conversion
4015 (location,
4016 build_unary_op (location, REALPART_EXPR, t, 0)),
4017 c_common_truthvalue_conversion
4018 (location,
4019 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4020 0));
4021 goto ret;
4024 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4026 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4027 FCONST0 (TYPE_MODE
4028 (TREE_TYPE (expr))));
4029 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4031 else
4032 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4034 ret:
4035 protected_set_expr_location (expr, location);
4036 return expr;
4039 static void def_builtin_1 (enum built_in_function fncode,
4040 const char *name,
4041 enum built_in_class fnclass,
4042 tree fntype, tree libtype,
4043 bool both_p, bool fallback_p, bool nonansi_p,
4044 tree fnattrs, bool implicit_p);
4047 /* Apply the TYPE_QUALS to the new DECL. */
4049 void
4050 c_apply_type_quals_to_decl (int type_quals, tree decl)
4052 tree type = TREE_TYPE (decl);
4054 if (type == error_mark_node)
4055 return;
4057 if (((type_quals & TYPE_QUAL_CONST)
4058 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4059 /* An object declared 'const' is only readonly after it is
4060 initialized. We don't have any way of expressing this currently,
4061 so we need to be conservative and unset TREE_READONLY for types
4062 with constructors. Otherwise aliasing code will ignore stores in
4063 an inline constructor. */
4064 && !(type && TYPE_NEEDS_CONSTRUCTING (type)))
4065 TREE_READONLY (decl) = 1;
4066 if (type_quals & TYPE_QUAL_VOLATILE)
4068 TREE_SIDE_EFFECTS (decl) = 1;
4069 TREE_THIS_VOLATILE (decl) = 1;
4071 if (type_quals & TYPE_QUAL_RESTRICT)
4073 while (type && TREE_CODE (type) == ARRAY_TYPE)
4074 /* Allow 'restrict' on arrays of pointers.
4075 FIXME currently we just ignore it. */
4076 type = TREE_TYPE (type);
4077 if (!type
4078 || !POINTER_TYPE_P (type)
4079 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4080 error ("invalid use of %<restrict%>");
4084 /* Hash function for the problem of multiple type definitions in
4085 different files. This must hash all types that will compare
4086 equal via comptypes to the same value. In practice it hashes
4087 on some of the simple stuff and leaves the details to comptypes. */
4089 static hashval_t
4090 c_type_hash (const void *p)
4092 int n_elements;
4093 int shift, size;
4094 const_tree const t = (const_tree) p;
4095 tree t2;
4096 switch (TREE_CODE (t))
4098 /* For pointers, hash on pointee type plus some swizzling. */
4099 case POINTER_TYPE:
4100 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4101 /* Hash on number of elements and total size. */
4102 case ENUMERAL_TYPE:
4103 shift = 3;
4104 t2 = TYPE_VALUES (t);
4105 break;
4106 case RECORD_TYPE:
4107 shift = 0;
4108 t2 = TYPE_FIELDS (t);
4109 break;
4110 case QUAL_UNION_TYPE:
4111 shift = 1;
4112 t2 = TYPE_FIELDS (t);
4113 break;
4114 case UNION_TYPE:
4115 shift = 2;
4116 t2 = TYPE_FIELDS (t);
4117 break;
4118 default:
4119 gcc_unreachable ();
4121 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4122 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4123 n_elements = list_length (t2);
4124 /* We might have a VLA here. */
4125 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4126 size = 0;
4127 else
4128 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4129 return ((size << 24) | (n_elements << shift));
4132 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4134 /* Return the typed-based alias set for T, which may be an expression
4135 or a type. Return -1 if we don't do anything special. */
4137 alias_set_type
4138 c_common_get_alias_set (tree t)
4140 tree u;
4141 PTR *slot;
4143 /* For VLAs, use the alias set of the element type rather than the
4144 default of alias set 0 for types compared structurally. */
4145 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4147 if (TREE_CODE (t) == ARRAY_TYPE)
4148 return get_alias_set (TREE_TYPE (t));
4149 return -1;
4152 /* Permit type-punning when accessing a union, provided the access
4153 is directly through the union. For example, this code does not
4154 permit taking the address of a union member and then storing
4155 through it. Even the type-punning allowed here is a GCC
4156 extension, albeit a common and useful one; the C standard says
4157 that such accesses have implementation-defined behavior. */
4158 for (u = t;
4159 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4160 u = TREE_OPERAND (u, 0))
4161 if (TREE_CODE (u) == COMPONENT_REF
4162 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4163 return 0;
4165 /* That's all the expressions we handle specially. */
4166 if (!TYPE_P (t))
4167 return -1;
4169 /* The C standard guarantees that any object may be accessed via an
4170 lvalue that has character type. */
4171 if (t == char_type_node
4172 || t == signed_char_type_node
4173 || t == unsigned_char_type_node)
4174 return 0;
4176 /* The C standard specifically allows aliasing between signed and
4177 unsigned variants of the same type. We treat the signed
4178 variant as canonical. */
4179 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4181 tree t1 = c_common_signed_type (t);
4183 /* t1 == t can happen for boolean nodes which are always unsigned. */
4184 if (t1 != t)
4185 return get_alias_set (t1);
4188 /* Handle the case of multiple type nodes referring to "the same" type,
4189 which occurs with IMA. These share an alias set. FIXME: Currently only
4190 C90 is handled. (In C99 type compatibility is not transitive, which
4191 complicates things mightily. The alias set splay trees can theoretically
4192 represent this, but insertion is tricky when you consider all the
4193 different orders things might arrive in.) */
4195 if (c_language != clk_c || flag_isoc99)
4196 return -1;
4198 /* Save time if there's only one input file. */
4199 if (num_in_fnames == 1)
4200 return -1;
4202 /* Pointers need special handling if they point to any type that
4203 needs special handling (below). */
4204 if (TREE_CODE (t) == POINTER_TYPE)
4206 tree t2;
4207 /* Find bottom type under any nested POINTERs. */
4208 for (t2 = TREE_TYPE (t);
4209 TREE_CODE (t2) == POINTER_TYPE;
4210 t2 = TREE_TYPE (t2))
4212 if (TREE_CODE (t2) != RECORD_TYPE
4213 && TREE_CODE (t2) != ENUMERAL_TYPE
4214 && TREE_CODE (t2) != QUAL_UNION_TYPE
4215 && TREE_CODE (t2) != UNION_TYPE)
4216 return -1;
4217 if (TYPE_SIZE (t2) == 0)
4218 return -1;
4220 /* These are the only cases that need special handling. */
4221 if (TREE_CODE (t) != RECORD_TYPE
4222 && TREE_CODE (t) != ENUMERAL_TYPE
4223 && TREE_CODE (t) != QUAL_UNION_TYPE
4224 && TREE_CODE (t) != UNION_TYPE
4225 && TREE_CODE (t) != POINTER_TYPE)
4226 return -1;
4227 /* Undefined? */
4228 if (TYPE_SIZE (t) == 0)
4229 return -1;
4231 /* Look up t in hash table. Only one of the compatible types within each
4232 alias set is recorded in the table. */
4233 if (!type_hash_table)
4234 type_hash_table = htab_create_ggc (1021, c_type_hash,
4235 (htab_eq) lang_hooks.types_compatible_p,
4236 NULL);
4237 slot = htab_find_slot (type_hash_table, t, INSERT);
4238 if (*slot != NULL)
4240 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4241 return TYPE_ALIAS_SET ((tree)*slot);
4243 else
4244 /* Our caller will assign and record (in t) a new alias set; all we need
4245 to do is remember t in the hash table. */
4246 *slot = t;
4248 return -1;
4251 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4252 the second parameter indicates which OPERATOR is being applied.
4253 The COMPLAIN flag controls whether we should diagnose possibly
4254 ill-formed constructs or not. LOC is the location of the SIZEOF or
4255 TYPEOF operator. */
4257 tree
4258 c_sizeof_or_alignof_type (location_t loc,
4259 tree type, bool is_sizeof, int complain)
4261 const char *op_name;
4262 tree value = NULL;
4263 enum tree_code type_code = TREE_CODE (type);
4265 op_name = is_sizeof ? "sizeof" : "__alignof__";
4267 if (type_code == FUNCTION_TYPE)
4269 if (is_sizeof)
4271 if (complain && (pedantic || warn_pointer_arith))
4272 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4273 "invalid application of %<sizeof%> to a function type");
4274 else if (!complain)
4275 return error_mark_node;
4276 value = size_one_node;
4278 else
4279 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4281 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4283 if (type_code == VOID_TYPE
4284 && complain && (pedantic || warn_pointer_arith))
4285 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4286 "invalid application of %qs to a void type", op_name);
4287 else if (!complain)
4288 return error_mark_node;
4289 value = size_one_node;
4291 else if (!COMPLETE_TYPE_P (type))
4293 if (complain)
4294 error_at (loc, "invalid application of %qs to incomplete type %qT ",
4295 op_name, type);
4296 return error_mark_node;
4298 else
4300 if (is_sizeof)
4301 /* Convert in case a char is more than one unit. */
4302 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4303 size_int (TYPE_PRECISION (char_type_node)
4304 / BITS_PER_UNIT));
4305 else
4306 value = size_int (TYPE_ALIGN_UNIT (type));
4309 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
4310 TYPE_IS_SIZETYPE means that certain things (like overflow) will
4311 never happen. However, this node should really have type
4312 `size_t', which is just a typedef for an ordinary integer type. */
4313 value = fold_convert_loc (loc, size_type_node, value);
4314 gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)));
4316 return value;
4319 /* Implement the __alignof keyword: Return the minimum required
4320 alignment of EXPR, measured in bytes. For VAR_DECLs,
4321 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4322 from an "aligned" __attribute__ specification). LOC is the
4323 location of the ALIGNOF operator. */
4325 tree
4326 c_alignof_expr (location_t loc, tree expr)
4328 tree t;
4330 if (VAR_OR_FUNCTION_DECL_P (expr))
4331 t = size_int (DECL_ALIGN_UNIT (expr));
4333 else if (TREE_CODE (expr) == COMPONENT_REF
4334 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4336 error_at (loc, "%<__alignof%> applied to a bit-field");
4337 t = size_one_node;
4339 else if (TREE_CODE (expr) == COMPONENT_REF
4340 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4341 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4343 else if (TREE_CODE (expr) == INDIRECT_REF)
4345 tree t = TREE_OPERAND (expr, 0);
4346 tree best = t;
4347 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4349 while (CONVERT_EXPR_P (t)
4350 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4352 int thisalign;
4354 t = TREE_OPERAND (t, 0);
4355 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4356 if (thisalign > bestalign)
4357 best = t, bestalign = thisalign;
4359 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4361 else
4362 return c_alignof (loc, TREE_TYPE (expr));
4364 return fold_convert_loc (loc, size_type_node, t);
4367 /* Handle C and C++ default attributes. */
4369 enum built_in_attribute
4371 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4372 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4373 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4374 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4375 #include "builtin-attrs.def"
4376 #undef DEF_ATTR_NULL_TREE
4377 #undef DEF_ATTR_INT
4378 #undef DEF_ATTR_IDENT
4379 #undef DEF_ATTR_TREE_LIST
4380 ATTR_LAST
4383 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4385 static void c_init_attributes (void);
4387 enum c_builtin_type
4389 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4390 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4391 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4392 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4393 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4394 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4395 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4396 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
4397 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
4398 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4399 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4400 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4401 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4402 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4403 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
4404 NAME,
4405 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4406 #include "builtin-types.def"
4407 #undef DEF_PRIMITIVE_TYPE
4408 #undef DEF_FUNCTION_TYPE_0
4409 #undef DEF_FUNCTION_TYPE_1
4410 #undef DEF_FUNCTION_TYPE_2
4411 #undef DEF_FUNCTION_TYPE_3
4412 #undef DEF_FUNCTION_TYPE_4
4413 #undef DEF_FUNCTION_TYPE_5
4414 #undef DEF_FUNCTION_TYPE_6
4415 #undef DEF_FUNCTION_TYPE_7
4416 #undef DEF_FUNCTION_TYPE_VAR_0
4417 #undef DEF_FUNCTION_TYPE_VAR_1
4418 #undef DEF_FUNCTION_TYPE_VAR_2
4419 #undef DEF_FUNCTION_TYPE_VAR_3
4420 #undef DEF_FUNCTION_TYPE_VAR_4
4421 #undef DEF_FUNCTION_TYPE_VAR_5
4422 #undef DEF_POINTER_TYPE
4423 BT_LAST
4426 typedef enum c_builtin_type builtin_type;
4428 /* A temporary array for c_common_nodes_and_builtins. Used in
4429 communication with def_fn_type. */
4430 static tree builtin_types[(int) BT_LAST + 1];
4432 /* A helper function for c_common_nodes_and_builtins. Build function type
4433 for DEF with return type RET and N arguments. If VAR is true, then the
4434 function should be variadic after those N arguments.
4436 Takes special care not to ICE if any of the types involved are
4437 error_mark_node, which indicates that said type is not in fact available
4438 (see builtin_type_for_size). In which case the function type as a whole
4439 should be error_mark_node. */
4441 static void
4442 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4444 tree t;
4445 tree *args = XALLOCAVEC (tree, n);
4446 va_list list;
4447 int i;
4449 va_start (list, n);
4450 for (i = 0; i < n; ++i)
4452 builtin_type a = (builtin_type) va_arg (list, int);
4453 t = builtin_types[a];
4454 if (t == error_mark_node)
4455 goto egress;
4456 args[i] = t;
4459 t = builtin_types[ret];
4460 if (t == error_mark_node)
4461 goto egress;
4462 if (var)
4463 t = build_varargs_function_type_array (t, n, args);
4464 else
4465 t = build_function_type_array (t, n, args);
4467 egress:
4468 builtin_types[def] = t;
4469 va_end (list);
4472 /* Build builtin functions common to both C and C++ language
4473 frontends. */
4475 static void
4476 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4478 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4479 builtin_types[ENUM] = VALUE;
4480 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4481 def_fn_type (ENUM, RETURN, 0, 0);
4482 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4483 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4484 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4485 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4486 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4487 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4488 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4489 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4490 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4491 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4492 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4493 ARG6) \
4494 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4495 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4496 ARG6, ARG7) \
4497 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4498 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4499 def_fn_type (ENUM, RETURN, 1, 0);
4500 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4501 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4502 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4503 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4504 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4505 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4506 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4507 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4508 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4509 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4510 #define DEF_POINTER_TYPE(ENUM, TYPE) \
4511 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4513 #include "builtin-types.def"
4515 #undef DEF_PRIMITIVE_TYPE
4516 #undef DEF_FUNCTION_TYPE_1
4517 #undef DEF_FUNCTION_TYPE_2
4518 #undef DEF_FUNCTION_TYPE_3
4519 #undef DEF_FUNCTION_TYPE_4
4520 #undef DEF_FUNCTION_TYPE_5
4521 #undef DEF_FUNCTION_TYPE_6
4522 #undef DEF_FUNCTION_TYPE_VAR_0
4523 #undef DEF_FUNCTION_TYPE_VAR_1
4524 #undef DEF_FUNCTION_TYPE_VAR_2
4525 #undef DEF_FUNCTION_TYPE_VAR_3
4526 #undef DEF_FUNCTION_TYPE_VAR_4
4527 #undef DEF_FUNCTION_TYPE_VAR_5
4528 #undef DEF_POINTER_TYPE
4529 builtin_types[(int) BT_LAST] = NULL_TREE;
4531 c_init_attributes ();
4533 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4534 NONANSI_P, ATTRS, IMPLICIT, COND) \
4535 if (NAME && COND) \
4536 def_builtin_1 (ENUM, NAME, CLASS, \
4537 builtin_types[(int) TYPE], \
4538 builtin_types[(int) LIBTYPE], \
4539 BOTH_P, FALLBACK_P, NONANSI_P, \
4540 built_in_attributes[(int) ATTRS], IMPLICIT);
4541 #include "builtins.def"
4542 #undef DEF_BUILTIN
4544 targetm.init_builtins ();
4546 build_common_builtin_nodes ();
4548 if (flag_mudflap)
4549 mudflap_init ();
4552 /* Like get_identifier, but avoid warnings about null arguments when
4553 the argument may be NULL for targets where GCC lacks stdint.h type
4554 information. */
4556 static inline tree
4557 c_get_ident (const char *id)
4559 return get_identifier (id);
4562 /* Build tree nodes and builtin functions common to both C and C++ language
4563 frontends. */
4565 void
4566 c_common_nodes_and_builtins (void)
4568 int char16_type_size;
4569 int char32_type_size;
4570 int wchar_type_size;
4571 tree array_domain_type;
4572 tree va_list_ref_type_node;
4573 tree va_list_arg_type_node;
4575 /* Define `int' and `char' first so that dbx will output them first. */
4576 record_builtin_type (RID_INT, NULL, integer_type_node);
4577 record_builtin_type (RID_CHAR, "char", char_type_node);
4579 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4580 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4581 but not C. Are the conditionals here needed? */
4582 if (c_dialect_cxx ())
4583 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4584 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4585 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4586 record_builtin_type (RID_MAX, "long unsigned int",
4587 long_unsigned_type_node);
4588 if (int128_integer_type_node != NULL_TREE)
4590 record_builtin_type (RID_INT128, "__int128",
4591 int128_integer_type_node);
4592 record_builtin_type (RID_MAX, "__int128 unsigned",
4593 int128_unsigned_type_node);
4595 if (c_dialect_cxx ())
4596 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4597 record_builtin_type (RID_MAX, "long long int",
4598 long_long_integer_type_node);
4599 record_builtin_type (RID_MAX, "long long unsigned int",
4600 long_long_unsigned_type_node);
4601 if (c_dialect_cxx ())
4602 record_builtin_type (RID_MAX, "long long unsigned",
4603 long_long_unsigned_type_node);
4604 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4605 record_builtin_type (RID_MAX, "short unsigned int",
4606 short_unsigned_type_node);
4607 if (c_dialect_cxx ())
4608 record_builtin_type (RID_MAX, "unsigned short",
4609 short_unsigned_type_node);
4611 /* Define both `signed char' and `unsigned char'. */
4612 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4613 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4615 /* These are types that c_common_type_for_size and
4616 c_common_type_for_mode use. */
4617 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4618 TYPE_DECL, NULL_TREE,
4619 intQI_type_node));
4620 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4621 TYPE_DECL, NULL_TREE,
4622 intHI_type_node));
4623 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4624 TYPE_DECL, NULL_TREE,
4625 intSI_type_node));
4626 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4627 TYPE_DECL, NULL_TREE,
4628 intDI_type_node));
4629 #if HOST_BITS_PER_WIDE_INT >= 64
4630 if (targetm.scalar_mode_supported_p (TImode))
4631 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4632 TYPE_DECL,
4633 get_identifier ("__int128_t"),
4634 intTI_type_node));
4635 #endif
4636 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4637 TYPE_DECL, NULL_TREE,
4638 unsigned_intQI_type_node));
4639 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4640 TYPE_DECL, NULL_TREE,
4641 unsigned_intHI_type_node));
4642 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4643 TYPE_DECL, NULL_TREE,
4644 unsigned_intSI_type_node));
4645 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4646 TYPE_DECL, NULL_TREE,
4647 unsigned_intDI_type_node));
4648 #if HOST_BITS_PER_WIDE_INT >= 64
4649 if (targetm.scalar_mode_supported_p (TImode))
4650 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4651 TYPE_DECL,
4652 get_identifier ("__uint128_t"),
4653 unsigned_intTI_type_node));
4654 #endif
4656 /* Create the widest literal types. */
4657 widest_integer_literal_type_node
4658 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
4659 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4660 TYPE_DECL, NULL_TREE,
4661 widest_integer_literal_type_node));
4663 widest_unsigned_literal_type_node
4664 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
4665 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4666 TYPE_DECL, NULL_TREE,
4667 widest_unsigned_literal_type_node));
4669 /* `unsigned long' is the standard type for sizeof.
4670 Note that stddef.h uses `unsigned long',
4671 and this must agree, even if long and int are the same size. */
4672 size_type_node =
4673 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
4674 signed_size_type_node = c_common_signed_type (size_type_node);
4675 set_sizetype (size_type_node);
4677 pid_type_node =
4678 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4680 build_common_tree_nodes_2 (flag_short_double);
4682 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4683 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4684 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4686 /* Only supported decimal floating point extension if the target
4687 actually supports underlying modes. */
4688 if (targetm.scalar_mode_supported_p (SDmode)
4689 && targetm.scalar_mode_supported_p (DDmode)
4690 && targetm.scalar_mode_supported_p (TDmode))
4692 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4693 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4694 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4697 if (targetm.fixed_point_supported_p ())
4699 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4700 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4701 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4702 record_builtin_type (RID_MAX, "long long _Fract",
4703 long_long_fract_type_node);
4704 record_builtin_type (RID_MAX, "unsigned short _Fract",
4705 unsigned_short_fract_type_node);
4706 record_builtin_type (RID_MAX, "unsigned _Fract",
4707 unsigned_fract_type_node);
4708 record_builtin_type (RID_MAX, "unsigned long _Fract",
4709 unsigned_long_fract_type_node);
4710 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4711 unsigned_long_long_fract_type_node);
4712 record_builtin_type (RID_MAX, "_Sat short _Fract",
4713 sat_short_fract_type_node);
4714 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4715 record_builtin_type (RID_MAX, "_Sat long _Fract",
4716 sat_long_fract_type_node);
4717 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4718 sat_long_long_fract_type_node);
4719 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4720 sat_unsigned_short_fract_type_node);
4721 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4722 sat_unsigned_fract_type_node);
4723 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4724 sat_unsigned_long_fract_type_node);
4725 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4726 sat_unsigned_long_long_fract_type_node);
4727 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4728 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4729 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4730 record_builtin_type (RID_MAX, "long long _Accum",
4731 long_long_accum_type_node);
4732 record_builtin_type (RID_MAX, "unsigned short _Accum",
4733 unsigned_short_accum_type_node);
4734 record_builtin_type (RID_MAX, "unsigned _Accum",
4735 unsigned_accum_type_node);
4736 record_builtin_type (RID_MAX, "unsigned long _Accum",
4737 unsigned_long_accum_type_node);
4738 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4739 unsigned_long_long_accum_type_node);
4740 record_builtin_type (RID_MAX, "_Sat short _Accum",
4741 sat_short_accum_type_node);
4742 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4743 record_builtin_type (RID_MAX, "_Sat long _Accum",
4744 sat_long_accum_type_node);
4745 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4746 sat_long_long_accum_type_node);
4747 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4748 sat_unsigned_short_accum_type_node);
4749 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4750 sat_unsigned_accum_type_node);
4751 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4752 sat_unsigned_long_accum_type_node);
4753 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4754 sat_unsigned_long_long_accum_type_node);
4758 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4759 TYPE_DECL,
4760 get_identifier ("complex int"),
4761 complex_integer_type_node));
4762 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4763 TYPE_DECL,
4764 get_identifier ("complex float"),
4765 complex_float_type_node));
4766 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4767 TYPE_DECL,
4768 get_identifier ("complex double"),
4769 complex_double_type_node));
4770 lang_hooks.decls.pushdecl
4771 (build_decl (UNKNOWN_LOCATION,
4772 TYPE_DECL, get_identifier ("complex long double"),
4773 complex_long_double_type_node));
4775 if (c_dialect_cxx ())
4776 /* For C++, make fileptr_type_node a distinct void * type until
4777 FILE type is defined. */
4778 fileptr_type_node = build_variant_type_copy (ptr_type_node);
4780 record_builtin_type (RID_VOID, NULL, void_type_node);
4782 /* Set the TYPE_NAME for any variants that were built before
4783 record_builtin_type gave names to the built-in types. */
4785 tree void_name = TYPE_NAME (void_type_node);
4786 TYPE_NAME (void_type_node) = NULL_TREE;
4787 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4788 = void_name;
4789 TYPE_NAME (void_type_node) = void_name;
4792 /* This node must not be shared. */
4793 void_zero_node = make_node (INTEGER_CST);
4794 TREE_TYPE (void_zero_node) = void_type_node;
4796 void_list_node = build_void_list_node ();
4798 /* Make a type to be the domain of a few array types
4799 whose domains don't really matter.
4800 200 is small enough that it always fits in size_t
4801 and large enough that it can hold most function names for the
4802 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4803 array_domain_type = build_index_type (size_int (200));
4805 /* Make a type for arrays of characters.
4806 With luck nothing will ever really depend on the length of this
4807 array type. */
4808 char_array_type_node
4809 = build_array_type (char_type_node, array_domain_type);
4811 /* Likewise for arrays of ints. */
4812 int_array_type_node
4813 = build_array_type (integer_type_node, array_domain_type);
4815 string_type_node = build_pointer_type (char_type_node);
4816 const_string_type_node
4817 = build_pointer_type (build_qualified_type
4818 (char_type_node, TYPE_QUAL_CONST));
4820 /* This is special for C++ so functions can be overloaded. */
4821 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4822 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4823 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4824 underlying_wchar_type_node = wchar_type_node;
4825 if (c_dialect_cxx ())
4827 if (TYPE_UNSIGNED (wchar_type_node))
4828 wchar_type_node = make_unsigned_type (wchar_type_size);
4829 else
4830 wchar_type_node = make_signed_type (wchar_type_size);
4831 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4834 /* This is for wide string constants. */
4835 wchar_array_type_node
4836 = build_array_type (wchar_type_node, array_domain_type);
4838 /* Define 'char16_t'. */
4839 char16_type_node = get_identifier (CHAR16_TYPE);
4840 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4841 char16_type_size = TYPE_PRECISION (char16_type_node);
4842 if (c_dialect_cxx ())
4844 char16_type_node = make_unsigned_type (char16_type_size);
4846 if (cxx_dialect == cxx0x)
4847 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4850 /* This is for UTF-16 string constants. */
4851 char16_array_type_node
4852 = build_array_type (char16_type_node, array_domain_type);
4854 /* Define 'char32_t'. */
4855 char32_type_node = get_identifier (CHAR32_TYPE);
4856 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4857 char32_type_size = TYPE_PRECISION (char32_type_node);
4858 if (c_dialect_cxx ())
4860 char32_type_node = make_unsigned_type (char32_type_size);
4862 if (cxx_dialect == cxx0x)
4863 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4866 /* This is for UTF-32 string constants. */
4867 char32_array_type_node
4868 = build_array_type (char32_type_node, array_domain_type);
4870 wint_type_node =
4871 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4873 intmax_type_node =
4874 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4875 uintmax_type_node =
4876 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4878 if (SIG_ATOMIC_TYPE)
4879 sig_atomic_type_node =
4880 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4881 if (INT8_TYPE)
4882 int8_type_node =
4883 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4884 if (INT16_TYPE)
4885 int16_type_node =
4886 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4887 if (INT32_TYPE)
4888 int32_type_node =
4889 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4890 if (INT64_TYPE)
4891 int64_type_node =
4892 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4893 if (UINT8_TYPE)
4894 uint8_type_node =
4895 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4896 if (UINT16_TYPE)
4897 uint16_type_node =
4898 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4899 if (UINT32_TYPE)
4900 c_uint32_type_node =
4901 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4902 if (UINT64_TYPE)
4903 c_uint64_type_node =
4904 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4905 if (INT_LEAST8_TYPE)
4906 int_least8_type_node =
4907 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4908 if (INT_LEAST16_TYPE)
4909 int_least16_type_node =
4910 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4911 if (INT_LEAST32_TYPE)
4912 int_least32_type_node =
4913 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4914 if (INT_LEAST64_TYPE)
4915 int_least64_type_node =
4916 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4917 if (UINT_LEAST8_TYPE)
4918 uint_least8_type_node =
4919 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4920 if (UINT_LEAST16_TYPE)
4921 uint_least16_type_node =
4922 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4923 if (UINT_LEAST32_TYPE)
4924 uint_least32_type_node =
4925 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4926 if (UINT_LEAST64_TYPE)
4927 uint_least64_type_node =
4928 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4929 if (INT_FAST8_TYPE)
4930 int_fast8_type_node =
4931 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4932 if (INT_FAST16_TYPE)
4933 int_fast16_type_node =
4934 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4935 if (INT_FAST32_TYPE)
4936 int_fast32_type_node =
4937 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4938 if (INT_FAST64_TYPE)
4939 int_fast64_type_node =
4940 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4941 if (UINT_FAST8_TYPE)
4942 uint_fast8_type_node =
4943 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4944 if (UINT_FAST16_TYPE)
4945 uint_fast16_type_node =
4946 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4947 if (UINT_FAST32_TYPE)
4948 uint_fast32_type_node =
4949 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4950 if (UINT_FAST64_TYPE)
4951 uint_fast64_type_node =
4952 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4953 if (INTPTR_TYPE)
4954 intptr_type_node =
4955 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4956 if (UINTPTR_TYPE)
4957 uintptr_type_node =
4958 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4960 default_function_type
4961 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4962 ptrdiff_type_node
4963 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
4964 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4966 lang_hooks.decls.pushdecl
4967 (build_decl (UNKNOWN_LOCATION,
4968 TYPE_DECL, get_identifier ("__builtin_va_list"),
4969 va_list_type_node));
4970 if (targetm.enum_va_list_p)
4972 int l;
4973 const char *pname;
4974 tree ptype;
4976 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4978 lang_hooks.decls.pushdecl
4979 (build_decl (UNKNOWN_LOCATION,
4980 TYPE_DECL, get_identifier (pname),
4981 ptype));
4986 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4988 va_list_arg_type_node = va_list_ref_type_node =
4989 build_pointer_type (TREE_TYPE (va_list_type_node));
4991 else
4993 va_list_arg_type_node = va_list_type_node;
4994 va_list_ref_type_node = build_reference_type (va_list_type_node);
4997 if (!flag_preprocess_only)
4998 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5000 main_identifier_node = get_identifier ("main");
5002 /* Create the built-in __null node. It is important that this is
5003 not shared. */
5004 null_node = make_node (INTEGER_CST);
5005 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5007 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5008 memset (builtin_types, 0, sizeof (builtin_types));
5011 /* The number of named compound-literals generated thus far. */
5012 static GTY(()) int compound_literal_number;
5014 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5016 void
5017 set_compound_literal_name (tree decl)
5019 char *name;
5020 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5021 compound_literal_number);
5022 compound_literal_number++;
5023 DECL_NAME (decl) = get_identifier (name);
5026 tree
5027 build_va_arg (location_t loc, tree expr, tree type)
5029 expr = build1 (VA_ARG_EXPR, type, expr);
5030 SET_EXPR_LOCATION (expr, loc);
5031 return expr;
5035 /* Linked list of disabled built-in functions. */
5037 typedef struct disabled_builtin
5039 const char *name;
5040 struct disabled_builtin *next;
5041 } disabled_builtin;
5042 static disabled_builtin *disabled_builtins = NULL;
5044 static bool builtin_function_disabled_p (const char *);
5046 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5047 begins with "__builtin_", give an error. */
5049 void
5050 disable_builtin_function (const char *name)
5052 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5053 error ("cannot disable built-in function %qs", name);
5054 else
5056 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5057 new_disabled_builtin->name = name;
5058 new_disabled_builtin->next = disabled_builtins;
5059 disabled_builtins = new_disabled_builtin;
5064 /* Return true if the built-in function NAME has been disabled, false
5065 otherwise. */
5067 static bool
5068 builtin_function_disabled_p (const char *name)
5070 disabled_builtin *p;
5071 for (p = disabled_builtins; p != NULL; p = p->next)
5073 if (strcmp (name, p->name) == 0)
5074 return true;
5076 return false;
5080 /* Worker for DEF_BUILTIN.
5081 Possibly define a builtin function with one or two names.
5082 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5083 nonansi_p and flag_no_nonansi_builtin. */
5085 static void
5086 def_builtin_1 (enum built_in_function fncode,
5087 const char *name,
5088 enum built_in_class fnclass,
5089 tree fntype, tree libtype,
5090 bool both_p, bool fallback_p, bool nonansi_p,
5091 tree fnattrs, bool implicit_p)
5093 tree decl;
5094 const char *libname;
5096 if (fntype == error_mark_node)
5097 return;
5099 gcc_assert ((!both_p && !fallback_p)
5100 || !strncmp (name, "__builtin_",
5101 strlen ("__builtin_")));
5103 libname = name + strlen ("__builtin_");
5104 decl = add_builtin_function (name, fntype, fncode, fnclass,
5105 (fallback_p ? libname : NULL),
5106 fnattrs);
5107 if (both_p
5108 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5109 && !(nonansi_p && flag_no_nonansi_builtin))
5110 add_builtin_function (libname, libtype, fncode, fnclass,
5111 NULL, fnattrs);
5113 built_in_decls[(int) fncode] = decl;
5114 if (implicit_p)
5115 implicit_built_in_decls[(int) fncode] = decl;
5118 /* Nonzero if the type T promotes to int. This is (nearly) the
5119 integral promotions defined in ISO C99 6.3.1.1/2. */
5121 bool
5122 c_promoting_integer_type_p (const_tree t)
5124 switch (TREE_CODE (t))
5126 case INTEGER_TYPE:
5127 return (TYPE_MAIN_VARIANT (t) == char_type_node
5128 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5129 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5130 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5131 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5132 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5134 case ENUMERAL_TYPE:
5135 /* ??? Technically all enumerations not larger than an int
5136 promote to an int. But this is used along code paths
5137 that only want to notice a size change. */
5138 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5140 case BOOLEAN_TYPE:
5141 return 1;
5143 default:
5144 return 0;
5148 /* Return 1 if PARMS specifies a fixed number of parameters
5149 and none of their types is affected by default promotions. */
5152 self_promoting_args_p (const_tree parms)
5154 const_tree t;
5155 for (t = parms; t; t = TREE_CHAIN (t))
5157 tree type = TREE_VALUE (t);
5159 if (type == error_mark_node)
5160 continue;
5162 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5163 return 0;
5165 if (type == 0)
5166 return 0;
5168 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5169 return 0;
5171 if (c_promoting_integer_type_p (type))
5172 return 0;
5174 return 1;
5177 /* Recursively remove any '*' or '&' operator from TYPE. */
5178 tree
5179 strip_pointer_operator (tree t)
5181 while (POINTER_TYPE_P (t))
5182 t = TREE_TYPE (t);
5183 return t;
5186 /* Recursively remove pointer or array type from TYPE. */
5187 tree
5188 strip_pointer_or_array_types (tree t)
5190 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5191 t = TREE_TYPE (t);
5192 return t;
5195 /* Used to compare case labels. K1 and K2 are actually tree nodes
5196 representing case labels, or NULL_TREE for a `default' label.
5197 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5198 K2, and 0 if K1 and K2 are equal. */
5201 case_compare (splay_tree_key k1, splay_tree_key k2)
5203 /* Consider a NULL key (such as arises with a `default' label) to be
5204 smaller than anything else. */
5205 if (!k1)
5206 return k2 ? -1 : 0;
5207 else if (!k2)
5208 return k1 ? 1 : 0;
5210 return tree_int_cst_compare ((tree) k1, (tree) k2);
5213 /* Process a case label, located at LOC, for the range LOW_VALUE
5214 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5215 then this case label is actually a `default' label. If only
5216 HIGH_VALUE is NULL_TREE, then case label was declared using the
5217 usual C/C++ syntax, rather than the GNU case range extension.
5218 CASES is a tree containing all the case ranges processed so far;
5219 COND is the condition for the switch-statement itself. Returns the
5220 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5221 is created. */
5223 tree
5224 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5225 tree low_value, tree high_value)
5227 tree type;
5228 tree label;
5229 tree case_label;
5230 splay_tree_node node;
5232 /* Create the LABEL_DECL itself. */
5233 label = create_artificial_label (loc);
5235 /* If there was an error processing the switch condition, bail now
5236 before we get more confused. */
5237 if (!cond || cond == error_mark_node)
5238 goto error_out;
5240 if ((low_value && TREE_TYPE (low_value)
5241 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5242 || (high_value && TREE_TYPE (high_value)
5243 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5245 error_at (loc, "pointers are not permitted as case values");
5246 goto error_out;
5249 /* Case ranges are a GNU extension. */
5250 if (high_value)
5251 pedwarn (loc, OPT_pedantic,
5252 "range expressions in switch statements are non-standard");
5254 type = TREE_TYPE (cond);
5255 if (low_value)
5257 low_value = check_case_value (low_value);
5258 low_value = convert_and_check (type, low_value);
5259 if (low_value == error_mark_node)
5260 goto error_out;
5262 if (high_value)
5264 high_value = check_case_value (high_value);
5265 high_value = convert_and_check (type, high_value);
5266 if (high_value == error_mark_node)
5267 goto error_out;
5270 if (low_value && high_value)
5272 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5273 really a case range, even though it was written that way.
5274 Remove the HIGH_VALUE to simplify later processing. */
5275 if (tree_int_cst_equal (low_value, high_value))
5276 high_value = NULL_TREE;
5277 else if (!tree_int_cst_lt (low_value, high_value))
5278 warning_at (loc, 0, "empty range specified");
5281 /* See if the case is in range of the type of the original testing
5282 expression. If both low_value and high_value are out of range,
5283 don't insert the case label and return NULL_TREE. */
5284 if (low_value
5285 && !check_case_bounds (type, orig_type,
5286 &low_value, high_value ? &high_value : NULL))
5287 return NULL_TREE;
5289 /* Look up the LOW_VALUE in the table of case labels we already
5290 have. */
5291 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5292 /* If there was not an exact match, check for overlapping ranges.
5293 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5294 that's a `default' label and the only overlap is an exact match. */
5295 if (!node && (low_value || high_value))
5297 splay_tree_node low_bound;
5298 splay_tree_node high_bound;
5300 /* Even though there wasn't an exact match, there might be an
5301 overlap between this case range and another case range.
5302 Since we've (inductively) not allowed any overlapping case
5303 ranges, we simply need to find the greatest low case label
5304 that is smaller that LOW_VALUE, and the smallest low case
5305 label that is greater than LOW_VALUE. If there is an overlap
5306 it will occur in one of these two ranges. */
5307 low_bound = splay_tree_predecessor (cases,
5308 (splay_tree_key) low_value);
5309 high_bound = splay_tree_successor (cases,
5310 (splay_tree_key) low_value);
5312 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5313 the LOW_VALUE, so there is no need to check unless the
5314 LOW_BOUND is in fact itself a case range. */
5315 if (low_bound
5316 && CASE_HIGH ((tree) low_bound->value)
5317 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5318 low_value) >= 0)
5319 node = low_bound;
5320 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5321 range is bigger than the low end of the current range, so we
5322 are only interested if the current range is a real range, and
5323 not an ordinary case label. */
5324 else if (high_bound
5325 && high_value
5326 && (tree_int_cst_compare ((tree) high_bound->key,
5327 high_value)
5328 <= 0))
5329 node = high_bound;
5331 /* If there was an overlap, issue an error. */
5332 if (node)
5334 tree duplicate = CASE_LABEL ((tree) node->value);
5336 if (high_value)
5338 error_at (loc, "duplicate (or overlapping) case value");
5339 error_at (DECL_SOURCE_LOCATION (duplicate),
5340 "this is the first entry overlapping that value");
5342 else if (low_value)
5344 error_at (loc, "duplicate case value") ;
5345 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5347 else
5349 error_at (loc, "multiple default labels in one switch");
5350 error_at (DECL_SOURCE_LOCATION (duplicate),
5351 "this is the first default label");
5353 goto error_out;
5356 /* Add a CASE_LABEL to the statement-tree. */
5357 case_label = add_stmt (build_case_label (low_value, high_value, label));
5358 /* Register this case label in the splay tree. */
5359 splay_tree_insert (cases,
5360 (splay_tree_key) low_value,
5361 (splay_tree_value) case_label);
5363 return case_label;
5365 error_out:
5366 /* Add a label so that the back-end doesn't think that the beginning of
5367 the switch is unreachable. Note that we do not add a case label, as
5368 that just leads to duplicates and thence to failure later on. */
5369 if (!cases->root)
5371 tree t = create_artificial_label (loc);
5372 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5374 return error_mark_node;
5377 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
5378 Used to verify that case values match up with enumerator values. */
5380 static void
5381 match_case_to_enum_1 (tree key, tree type, tree label)
5383 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
5385 /* ??? Not working too hard to print the double-word value.
5386 Should perhaps be done with %lwd in the diagnostic routines? */
5387 if (TREE_INT_CST_HIGH (key) == 0)
5388 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
5389 TREE_INT_CST_LOW (key));
5390 else if (!TYPE_UNSIGNED (type)
5391 && TREE_INT_CST_HIGH (key) == -1
5392 && TREE_INT_CST_LOW (key) != 0)
5393 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
5394 -TREE_INT_CST_LOW (key));
5395 else
5396 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5397 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
5398 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
5400 if (TYPE_NAME (type) == 0)
5401 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5402 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5403 "case value %qs not in enumerated type",
5404 buf);
5405 else
5406 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
5407 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
5408 "case value %qs not in enumerated type %qT",
5409 buf, type);
5412 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
5413 Used to verify that case values match up with enumerator values. */
5415 static int
5416 match_case_to_enum (splay_tree_node node, void *data)
5418 tree label = (tree) node->value;
5419 tree type = (tree) data;
5421 /* Skip default case. */
5422 if (!CASE_LOW (label))
5423 return 0;
5425 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
5426 when we did our enum->case scan. Reset our scratch bit after. */
5427 if (!CASE_LOW_SEEN (label))
5428 match_case_to_enum_1 (CASE_LOW (label), type, label);
5429 else
5430 CASE_LOW_SEEN (label) = 0;
5432 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
5433 not set, that means that CASE_HIGH did not appear when we did our
5434 enum->case scan. Reset our scratch bit after. */
5435 if (CASE_HIGH (label))
5437 if (!CASE_HIGH_SEEN (label))
5438 match_case_to_enum_1 (CASE_HIGH (label), type, label);
5439 else
5440 CASE_HIGH_SEEN (label) = 0;
5443 return 0;
5446 /* Handle -Wswitch*. Called from the front end after parsing the
5447 switch construct. */
5448 /* ??? Should probably be somewhere generic, since other languages
5449 besides C and C++ would want this. At the moment, however, C/C++
5450 are the only tree-ssa languages that support enumerations at all,
5451 so the point is moot. */
5453 void
5454 c_do_switch_warnings (splay_tree cases, location_t switch_location,
5455 tree type, tree cond)
5457 splay_tree_node default_node;
5458 splay_tree_node node;
5459 tree chain;
5461 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
5462 return;
5464 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
5465 if (!default_node)
5466 warning_at (switch_location, OPT_Wswitch_default,
5467 "switch missing default case");
5469 /* From here on, we only care about about enumerated types. */
5470 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
5471 return;
5473 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
5474 if (!warn_switch_enum && !warn_switch)
5475 return;
5477 /* Check the cases. Warn about case values which are not members of
5478 the enumerated type. For -Wswitch-enum, or for -Wswitch when
5479 there is no default case, check that exactly all enumeration
5480 literals are covered by the cases. */
5482 /* Clearing COND if it is not an integer constant simplifies
5483 the tests inside the loop below. */
5484 if (TREE_CODE (cond) != INTEGER_CST)
5485 cond = NULL_TREE;
5487 /* The time complexity here is O(N*lg(N)) worst case, but for the
5488 common case of monotonically increasing enumerators, it is
5489 O(N), since the nature of the splay tree will keep the next
5490 element adjacent to the root at all times. */
5492 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
5494 tree value = TREE_VALUE (chain);
5495 if (TREE_CODE (value) == CONST_DECL)
5496 value = DECL_INITIAL (value);
5497 node = splay_tree_lookup (cases, (splay_tree_key) value);
5498 if (node)
5500 /* Mark the CASE_LOW part of the case entry as seen. */
5501 tree label = (tree) node->value;
5502 CASE_LOW_SEEN (label) = 1;
5503 continue;
5506 /* Even though there wasn't an exact match, there might be a
5507 case range which includes the enumerator's value. */
5508 node = splay_tree_predecessor (cases, (splay_tree_key) value);
5509 if (node && CASE_HIGH ((tree) node->value))
5511 tree label = (tree) node->value;
5512 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
5513 if (cmp >= 0)
5515 /* If we match the upper bound exactly, mark the CASE_HIGH
5516 part of the case entry as seen. */
5517 if (cmp == 0)
5518 CASE_HIGH_SEEN (label) = 1;
5519 continue;
5523 /* We've now determined that this enumerated literal isn't
5524 handled by the case labels of the switch statement. */
5526 /* If the switch expression is a constant, we only really care
5527 about whether that constant is handled by the switch. */
5528 if (cond && tree_int_cst_compare (cond, value))
5529 continue;
5531 /* If there is a default_node, the only relevant option is
5532 Wswitch-enum. Otherwise, if both are enabled then we prefer
5533 to warn using -Wswitch because -Wswitch is enabled by -Wall
5534 while -Wswitch-enum is explicit. */
5535 warning_at (switch_location,
5536 (default_node || !warn_switch
5537 ? OPT_Wswitch_enum
5538 : OPT_Wswitch),
5539 "enumeration value %qE not handled in switch",
5540 TREE_PURPOSE (chain));
5543 /* Warn if there are case expressions that don't correspond to
5544 enumerators. This can occur since C and C++ don't enforce
5545 type-checking of assignments to enumeration variables.
5547 The time complexity here is now always O(N) worst case, since
5548 we should have marked both the lower bound and upper bound of
5549 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
5550 above. This scan also resets those fields. */
5552 splay_tree_foreach (cases, match_case_to_enum, type);
5555 /* Finish an expression taking the address of LABEL (an
5556 IDENTIFIER_NODE). Returns an expression for the address.
5558 LOC is the location for the expression returned. */
5560 tree
5561 finish_label_address_expr (tree label, location_t loc)
5563 tree result;
5565 pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard");
5567 if (label == error_mark_node)
5568 return error_mark_node;
5570 label = lookup_label (label);
5571 if (label == NULL_TREE)
5572 result = null_pointer_node;
5573 else
5575 TREE_USED (label) = 1;
5576 result = build1 (ADDR_EXPR, ptr_type_node, label);
5577 /* The current function is not necessarily uninlinable.
5578 Computed gotos are incompatible with inlining, but the value
5579 here could be used only in a diagnostic, for example. */
5580 protected_set_expr_location (result, loc);
5583 return result;
5587 /* Given a boolean expression ARG, return a tree representing an increment
5588 or decrement (as indicated by CODE) of ARG. The front end must check for
5589 invalid cases (e.g., decrement in C++). */
5590 tree
5591 boolean_increment (enum tree_code code, tree arg)
5593 tree val;
5594 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5596 arg = stabilize_reference (arg);
5597 switch (code)
5599 case PREINCREMENT_EXPR:
5600 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5601 break;
5602 case POSTINCREMENT_EXPR:
5603 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5604 arg = save_expr (arg);
5605 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5606 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5607 break;
5608 case PREDECREMENT_EXPR:
5609 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5610 invert_truthvalue_loc (input_location, arg));
5611 break;
5612 case POSTDECREMENT_EXPR:
5613 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5614 invert_truthvalue_loc (input_location, arg));
5615 arg = save_expr (arg);
5616 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5617 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5618 break;
5619 default:
5620 gcc_unreachable ();
5622 TREE_SIDE_EFFECTS (val) = 1;
5623 return val;
5626 /* Built-in macros for stddef.h and stdint.h, that require macros
5627 defined in this file. */
5628 void
5629 c_stddef_cpp_builtins(void)
5631 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5632 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5633 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5634 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5635 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5636 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5637 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5638 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5639 if (SIG_ATOMIC_TYPE)
5640 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5641 if (INT8_TYPE)
5642 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5643 if (INT16_TYPE)
5644 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5645 if (INT32_TYPE)
5646 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5647 if (INT64_TYPE)
5648 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5649 if (UINT8_TYPE)
5650 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5651 if (UINT16_TYPE)
5652 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5653 if (UINT32_TYPE)
5654 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5655 if (UINT64_TYPE)
5656 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5657 if (INT_LEAST8_TYPE)
5658 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5659 if (INT_LEAST16_TYPE)
5660 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5661 if (INT_LEAST32_TYPE)
5662 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5663 if (INT_LEAST64_TYPE)
5664 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5665 if (UINT_LEAST8_TYPE)
5666 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5667 if (UINT_LEAST16_TYPE)
5668 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5669 if (UINT_LEAST32_TYPE)
5670 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5671 if (UINT_LEAST64_TYPE)
5672 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5673 if (INT_FAST8_TYPE)
5674 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5675 if (INT_FAST16_TYPE)
5676 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5677 if (INT_FAST32_TYPE)
5678 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5679 if (INT_FAST64_TYPE)
5680 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5681 if (UINT_FAST8_TYPE)
5682 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5683 if (UINT_FAST16_TYPE)
5684 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5685 if (UINT_FAST32_TYPE)
5686 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5687 if (UINT_FAST64_TYPE)
5688 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5689 if (INTPTR_TYPE)
5690 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5691 if (UINTPTR_TYPE)
5692 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5695 static void
5696 c_init_attributes (void)
5698 /* Fill in the built_in_attributes array. */
5699 #define DEF_ATTR_NULL_TREE(ENUM) \
5700 built_in_attributes[(int) ENUM] = NULL_TREE;
5701 #define DEF_ATTR_INT(ENUM, VALUE) \
5702 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5703 #define DEF_ATTR_IDENT(ENUM, STRING) \
5704 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5705 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5706 built_in_attributes[(int) ENUM] \
5707 = tree_cons (built_in_attributes[(int) PURPOSE], \
5708 built_in_attributes[(int) VALUE], \
5709 built_in_attributes[(int) CHAIN]);
5710 #include "builtin-attrs.def"
5711 #undef DEF_ATTR_NULL_TREE
5712 #undef DEF_ATTR_INT
5713 #undef DEF_ATTR_IDENT
5714 #undef DEF_ATTR_TREE_LIST
5717 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
5718 identifier as an argument, so the front end shouldn't look it up. */
5720 bool
5721 attribute_takes_identifier_p (const_tree attr_id)
5723 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
5724 if (spec == NULL)
5725 /* Unknown attribute that we'll end up ignoring, return true so we
5726 don't complain about an identifier argument. */
5727 return true;
5728 else if (!strcmp ("mode", spec->name)
5729 || !strcmp ("format", spec->name)
5730 || !strcmp ("cleanup", spec->name))
5731 return true;
5732 else
5733 return targetm.attribute_takes_identifier_p (attr_id);
5736 /* Attribute handlers common to C front ends. */
5738 /* Handle a "packed" attribute; arguments as in
5739 struct attribute_spec.handler. */
5741 static tree
5742 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5743 int flags, bool *no_add_attrs)
5745 if (TYPE_P (*node))
5747 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5748 *node = build_variant_type_copy (*node);
5749 TYPE_PACKED (*node) = 1;
5751 else if (TREE_CODE (*node) == FIELD_DECL)
5753 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
5754 /* Still pack bitfields. */
5755 && ! DECL_INITIAL (*node))
5756 warning (OPT_Wattributes,
5757 "%qE attribute ignored for field of type %qT",
5758 name, TREE_TYPE (*node));
5759 else
5760 DECL_PACKED (*node) = 1;
5762 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5763 used for DECL_REGISTER. It wouldn't mean anything anyway.
5764 We can't set DECL_PACKED on the type of a TYPE_DECL, because
5765 that changes what the typedef is typing. */
5766 else
5768 warning (OPT_Wattributes, "%qE attribute ignored", name);
5769 *no_add_attrs = true;
5772 return NULL_TREE;
5775 /* Handle a "nocommon" attribute; arguments as in
5776 struct attribute_spec.handler. */
5778 static tree
5779 handle_nocommon_attribute (tree *node, tree name,
5780 tree ARG_UNUSED (args),
5781 int ARG_UNUSED (flags), bool *no_add_attrs)
5783 if (TREE_CODE (*node) == VAR_DECL)
5784 DECL_COMMON (*node) = 0;
5785 else
5787 warning (OPT_Wattributes, "%qE attribute ignored", name);
5788 *no_add_attrs = true;
5791 return NULL_TREE;
5794 /* Handle a "common" attribute; arguments as in
5795 struct attribute_spec.handler. */
5797 static tree
5798 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5799 int ARG_UNUSED (flags), bool *no_add_attrs)
5801 if (TREE_CODE (*node) == VAR_DECL)
5802 DECL_COMMON (*node) = 1;
5803 else
5805 warning (OPT_Wattributes, "%qE attribute ignored", name);
5806 *no_add_attrs = true;
5809 return NULL_TREE;
5812 /* Handle a "noreturn" attribute; arguments as in
5813 struct attribute_spec.handler. */
5815 static tree
5816 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5817 int ARG_UNUSED (flags), bool *no_add_attrs)
5819 tree type = TREE_TYPE (*node);
5821 /* See FIXME comment in c_common_attribute_table. */
5822 if (TREE_CODE (*node) == FUNCTION_DECL
5823 || objc_method_decl (TREE_CODE (*node)))
5824 TREE_THIS_VOLATILE (*node) = 1;
5825 else if (TREE_CODE (type) == POINTER_TYPE
5826 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5827 TREE_TYPE (*node)
5828 = build_pointer_type
5829 (build_type_variant (TREE_TYPE (type),
5830 TYPE_READONLY (TREE_TYPE (type)), 1));
5831 else
5833 warning (OPT_Wattributes, "%qE attribute ignored", name);
5834 *no_add_attrs = true;
5837 return NULL_TREE;
5840 /* Handle a "hot" and attribute; arguments as in
5841 struct attribute_spec.handler. */
5843 static tree
5844 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5845 int ARG_UNUSED (flags), bool *no_add_attrs)
5847 if (TREE_CODE (*node) == FUNCTION_DECL)
5849 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
5851 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5852 name, "cold");
5853 *no_add_attrs = true;
5855 /* Most of the rest of the hot processing is done later with
5856 lookup_attribute. */
5858 else
5860 warning (OPT_Wattributes, "%qE attribute ignored", name);
5861 *no_add_attrs = true;
5864 return NULL_TREE;
5866 /* Handle a "cold" and attribute; arguments as in
5867 struct attribute_spec.handler. */
5869 static tree
5870 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5871 int ARG_UNUSED (flags), bool *no_add_attrs)
5873 if (TREE_CODE (*node) == FUNCTION_DECL)
5875 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
5877 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
5878 name, "hot");
5879 *no_add_attrs = true;
5881 /* Most of the rest of the cold processing is done later with
5882 lookup_attribute. */
5884 else
5886 warning (OPT_Wattributes, "%qE attribute ignored", name);
5887 *no_add_attrs = true;
5890 return NULL_TREE;
5893 /* Handle a "noinline" attribute; arguments as in
5894 struct attribute_spec.handler. */
5896 static tree
5897 handle_noinline_attribute (tree *node, tree name,
5898 tree ARG_UNUSED (args),
5899 int ARG_UNUSED (flags), bool *no_add_attrs)
5901 if (TREE_CODE (*node) == FUNCTION_DECL)
5902 DECL_UNINLINABLE (*node) = 1;
5903 else
5905 warning (OPT_Wattributes, "%qE attribute ignored", name);
5906 *no_add_attrs = true;
5909 return NULL_TREE;
5912 /* Handle a "noclone" attribute; arguments as in
5913 struct attribute_spec.handler. */
5915 static tree
5916 handle_noclone_attribute (tree *node, tree name,
5917 tree ARG_UNUSED (args),
5918 int ARG_UNUSED (flags), bool *no_add_attrs)
5920 if (TREE_CODE (*node) != FUNCTION_DECL)
5922 warning (OPT_Wattributes, "%qE attribute ignored", name);
5923 *no_add_attrs = true;
5926 return NULL_TREE;
5929 /* Handle a "always_inline" attribute; arguments as in
5930 struct attribute_spec.handler. */
5932 static tree
5933 handle_always_inline_attribute (tree *node, tree name,
5934 tree ARG_UNUSED (args),
5935 int ARG_UNUSED (flags),
5936 bool *no_add_attrs)
5938 if (TREE_CODE (*node) == FUNCTION_DECL)
5940 /* Set the attribute and mark it for disregarding inline
5941 limits. */
5942 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
5944 else
5946 warning (OPT_Wattributes, "%qE attribute ignored", name);
5947 *no_add_attrs = true;
5950 return NULL_TREE;
5953 /* Handle a "gnu_inline" attribute; arguments as in
5954 struct attribute_spec.handler. */
5956 static tree
5957 handle_gnu_inline_attribute (tree *node, tree name,
5958 tree ARG_UNUSED (args),
5959 int ARG_UNUSED (flags),
5960 bool *no_add_attrs)
5962 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
5964 /* Do nothing else, just set the attribute. We'll get at
5965 it later with lookup_attribute. */
5967 else
5969 warning (OPT_Wattributes, "%qE attribute ignored", name);
5970 *no_add_attrs = true;
5973 return NULL_TREE;
5976 /* Handle a "leaf" attribute; arguments as in
5977 struct attribute_spec.handler. */
5979 static tree
5980 handle_leaf_attribute (tree *node, tree name,
5981 tree ARG_UNUSED (args),
5982 int ARG_UNUSED (flags), bool *no_add_attrs)
5984 if (TREE_CODE (*node) != FUNCTION_DECL)
5986 warning (OPT_Wattributes, "%qE attribute ignored", name);
5987 *no_add_attrs = true;
5989 if (!TREE_PUBLIC (*node))
5991 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
5992 *no_add_attrs = true;
5995 return NULL_TREE;
5998 /* Handle an "artificial" attribute; arguments as in
5999 struct attribute_spec.handler. */
6001 static tree
6002 handle_artificial_attribute (tree *node, tree name,
6003 tree ARG_UNUSED (args),
6004 int ARG_UNUSED (flags),
6005 bool *no_add_attrs)
6007 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6009 /* Do nothing else, just set the attribute. We'll get at
6010 it later with lookup_attribute. */
6012 else
6014 warning (OPT_Wattributes, "%qE attribute ignored", name);
6015 *no_add_attrs = true;
6018 return NULL_TREE;
6021 /* Handle a "flatten" attribute; arguments as in
6022 struct attribute_spec.handler. */
6024 static tree
6025 handle_flatten_attribute (tree *node, tree name,
6026 tree args ATTRIBUTE_UNUSED,
6027 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6029 if (TREE_CODE (*node) == FUNCTION_DECL)
6030 /* Do nothing else, just set the attribute. We'll get at
6031 it later with lookup_attribute. */
6033 else
6035 warning (OPT_Wattributes, "%qE attribute ignored", name);
6036 *no_add_attrs = true;
6039 return NULL_TREE;
6042 /* Handle a "warning" or "error" attribute; arguments as in
6043 struct attribute_spec.handler. */
6045 static tree
6046 handle_error_attribute (tree *node, tree name, tree args,
6047 int ARG_UNUSED (flags), bool *no_add_attrs)
6049 if (TREE_CODE (*node) == FUNCTION_DECL
6050 || TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6051 /* Do nothing else, just set the attribute. We'll get at
6052 it later with lookup_attribute. */
6054 else
6056 warning (OPT_Wattributes, "%qE attribute ignored", name);
6057 *no_add_attrs = true;
6060 return NULL_TREE;
6063 /* Handle a "used" attribute; arguments as in
6064 struct attribute_spec.handler. */
6066 static tree
6067 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6068 int ARG_UNUSED (flags), bool *no_add_attrs)
6070 tree node = *pnode;
6072 if (TREE_CODE (node) == FUNCTION_DECL
6073 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
6075 TREE_USED (node) = 1;
6076 DECL_PRESERVE_P (node) = 1;
6077 if (TREE_CODE (node) == VAR_DECL)
6078 DECL_READ_P (node) = 1;
6080 else
6082 warning (OPT_Wattributes, "%qE attribute ignored", name);
6083 *no_add_attrs = true;
6086 return NULL_TREE;
6089 /* Handle a "unused" attribute; arguments as in
6090 struct attribute_spec.handler. */
6092 static tree
6093 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6094 int flags, bool *no_add_attrs)
6096 if (DECL_P (*node))
6098 tree decl = *node;
6100 if (TREE_CODE (decl) == PARM_DECL
6101 || TREE_CODE (decl) == VAR_DECL
6102 || TREE_CODE (decl) == FUNCTION_DECL
6103 || TREE_CODE (decl) == LABEL_DECL
6104 || TREE_CODE (decl) == TYPE_DECL)
6106 TREE_USED (decl) = 1;
6107 if (TREE_CODE (decl) == VAR_DECL
6108 || TREE_CODE (decl) == PARM_DECL)
6109 DECL_READ_P (decl) = 1;
6111 else
6113 warning (OPT_Wattributes, "%qE attribute ignored", name);
6114 *no_add_attrs = true;
6117 else
6119 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6120 *node = build_variant_type_copy (*node);
6121 TREE_USED (*node) = 1;
6124 return NULL_TREE;
6127 /* Handle a "externally_visible" attribute; arguments as in
6128 struct attribute_spec.handler. */
6130 static tree
6131 handle_externally_visible_attribute (tree *pnode, tree name,
6132 tree ARG_UNUSED (args),
6133 int ARG_UNUSED (flags),
6134 bool *no_add_attrs)
6136 tree node = *pnode;
6138 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6140 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6141 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6143 warning (OPT_Wattributes,
6144 "%qE attribute have effect only on public objects", name);
6145 *no_add_attrs = true;
6148 else
6150 warning (OPT_Wattributes, "%qE attribute ignored", name);
6151 *no_add_attrs = true;
6154 return NULL_TREE;
6157 /* Handle a "const" attribute; arguments as in
6158 struct attribute_spec.handler. */
6160 static tree
6161 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6162 int ARG_UNUSED (flags), bool *no_add_attrs)
6164 tree type = TREE_TYPE (*node);
6166 /* See FIXME comment on noreturn in c_common_attribute_table. */
6167 if (TREE_CODE (*node) == FUNCTION_DECL)
6168 TREE_READONLY (*node) = 1;
6169 else if (TREE_CODE (type) == POINTER_TYPE
6170 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6171 TREE_TYPE (*node)
6172 = build_pointer_type
6173 (build_type_variant (TREE_TYPE (type), 1,
6174 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6175 else
6177 warning (OPT_Wattributes, "%qE attribute ignored", name);
6178 *no_add_attrs = true;
6181 return NULL_TREE;
6184 /* Handle a "transparent_union" attribute; arguments as in
6185 struct attribute_spec.handler. */
6187 static tree
6188 handle_transparent_union_attribute (tree *node, tree name,
6189 tree ARG_UNUSED (args), int flags,
6190 bool *no_add_attrs)
6192 tree type;
6194 *no_add_attrs = true;
6196 if (TREE_CODE (*node) == TYPE_DECL)
6197 node = &TREE_TYPE (*node);
6198 type = *node;
6200 if (TREE_CODE (type) == UNION_TYPE)
6202 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
6203 the code in finish_struct. */
6204 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6206 if (TYPE_FIELDS (type) == NULL_TREE
6207 || c_dialect_cxx ()
6208 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
6209 goto ignored;
6211 /* A type variant isn't good enough, since we don't a cast
6212 to such a type removed as a no-op. */
6213 *node = type = build_duplicate_type (type);
6216 TYPE_TRANSPARENT_AGGR (type) = 1;
6217 return NULL_TREE;
6220 ignored:
6221 warning (OPT_Wattributes, "%qE attribute ignored", name);
6222 return NULL_TREE;
6225 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
6226 get the requested priority for a constructor or destructor,
6227 possibly issuing diagnostics for invalid or reserved
6228 priorities. */
6230 static priority_type
6231 get_priority (tree args, bool is_destructor)
6233 HOST_WIDE_INT pri;
6234 tree arg;
6236 if (!args)
6237 return DEFAULT_INIT_PRIORITY;
6239 if (!SUPPORTS_INIT_PRIORITY)
6241 if (is_destructor)
6242 error ("destructor priorities are not supported");
6243 else
6244 error ("constructor priorities are not supported");
6245 return DEFAULT_INIT_PRIORITY;
6248 arg = TREE_VALUE (args);
6249 if (!host_integerp (arg, /*pos=*/0)
6250 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6251 goto invalid;
6253 pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0);
6254 if (pri < 0 || pri > MAX_INIT_PRIORITY)
6255 goto invalid;
6257 if (pri <= MAX_RESERVED_INIT_PRIORITY)
6259 if (is_destructor)
6260 warning (0,
6261 "destructor priorities from 0 to %d are reserved "
6262 "for the implementation",
6263 MAX_RESERVED_INIT_PRIORITY);
6264 else
6265 warning (0,
6266 "constructor priorities from 0 to %d are reserved "
6267 "for the implementation",
6268 MAX_RESERVED_INIT_PRIORITY);
6270 return pri;
6272 invalid:
6273 if (is_destructor)
6274 error ("destructor priorities must be integers from 0 to %d inclusive",
6275 MAX_INIT_PRIORITY);
6276 else
6277 error ("constructor priorities must be integers from 0 to %d inclusive",
6278 MAX_INIT_PRIORITY);
6279 return DEFAULT_INIT_PRIORITY;
6282 /* Handle a "constructor" attribute; arguments as in
6283 struct attribute_spec.handler. */
6285 static tree
6286 handle_constructor_attribute (tree *node, tree name, tree args,
6287 int ARG_UNUSED (flags),
6288 bool *no_add_attrs)
6290 tree decl = *node;
6291 tree type = TREE_TYPE (decl);
6293 if (TREE_CODE (decl) == FUNCTION_DECL
6294 && TREE_CODE (type) == FUNCTION_TYPE
6295 && decl_function_context (decl) == 0)
6297 priority_type priority;
6298 DECL_STATIC_CONSTRUCTOR (decl) = 1;
6299 priority = get_priority (args, /*is_destructor=*/false);
6300 SET_DECL_INIT_PRIORITY (decl, priority);
6301 TREE_USED (decl) = 1;
6303 else
6305 warning (OPT_Wattributes, "%qE attribute ignored", name);
6306 *no_add_attrs = true;
6309 return NULL_TREE;
6312 /* Handle a "destructor" attribute; arguments as in
6313 struct attribute_spec.handler. */
6315 static tree
6316 handle_destructor_attribute (tree *node, tree name, tree args,
6317 int ARG_UNUSED (flags),
6318 bool *no_add_attrs)
6320 tree decl = *node;
6321 tree type = TREE_TYPE (decl);
6323 if (TREE_CODE (decl) == FUNCTION_DECL
6324 && TREE_CODE (type) == FUNCTION_TYPE
6325 && decl_function_context (decl) == 0)
6327 priority_type priority;
6328 DECL_STATIC_DESTRUCTOR (decl) = 1;
6329 priority = get_priority (args, /*is_destructor=*/true);
6330 SET_DECL_FINI_PRIORITY (decl, priority);
6331 TREE_USED (decl) = 1;
6333 else
6335 warning (OPT_Wattributes, "%qE attribute ignored", name);
6336 *no_add_attrs = true;
6339 return NULL_TREE;
6342 /* Nonzero if the mode is a valid vector mode for this architecture.
6343 This returns nonzero even if there is no hardware support for the
6344 vector mode, but we can emulate with narrower modes. */
6346 static int
6347 vector_mode_valid_p (enum machine_mode mode)
6349 enum mode_class mclass = GET_MODE_CLASS (mode);
6350 enum machine_mode innermode;
6352 /* Doh! What's going on? */
6353 if (mclass != MODE_VECTOR_INT
6354 && mclass != MODE_VECTOR_FLOAT
6355 && mclass != MODE_VECTOR_FRACT
6356 && mclass != MODE_VECTOR_UFRACT
6357 && mclass != MODE_VECTOR_ACCUM
6358 && mclass != MODE_VECTOR_UACCUM)
6359 return 0;
6361 /* Hardware support. Woo hoo! */
6362 if (targetm.vector_mode_supported_p (mode))
6363 return 1;
6365 innermode = GET_MODE_INNER (mode);
6367 /* We should probably return 1 if requesting V4DI and we have no DI,
6368 but we have V2DI, but this is probably very unlikely. */
6370 /* If we have support for the inner mode, we can safely emulate it.
6371 We may not have V2DI, but me can emulate with a pair of DIs. */
6372 return targetm.scalar_mode_supported_p (innermode);
6376 /* Handle a "mode" attribute; arguments as in
6377 struct attribute_spec.handler. */
6379 static tree
6380 handle_mode_attribute (tree *node, tree name, tree args,
6381 int ARG_UNUSED (flags), bool *no_add_attrs)
6383 tree type = *node;
6384 tree ident = TREE_VALUE (args);
6386 *no_add_attrs = true;
6388 if (TREE_CODE (ident) != IDENTIFIER_NODE)
6389 warning (OPT_Wattributes, "%qE attribute ignored", name);
6390 else
6392 int j;
6393 const char *p = IDENTIFIER_POINTER (ident);
6394 int len = strlen (p);
6395 enum machine_mode mode = VOIDmode;
6396 tree typefm;
6397 bool valid_mode;
6399 if (len > 4 && p[0] == '_' && p[1] == '_'
6400 && p[len - 1] == '_' && p[len - 2] == '_')
6402 char *newp = (char *) alloca (len - 1);
6404 strcpy (newp, &p[2]);
6405 newp[len - 4] = '\0';
6406 p = newp;
6409 /* Change this type to have a type with the specified mode.
6410 First check for the special modes. */
6411 if (!strcmp (p, "byte"))
6412 mode = byte_mode;
6413 else if (!strcmp (p, "word"))
6414 mode = word_mode;
6415 else if (!strcmp (p, "pointer"))
6416 mode = ptr_mode;
6417 else if (!strcmp (p, "libgcc_cmp_return"))
6418 mode = targetm.libgcc_cmp_return_mode ();
6419 else if (!strcmp (p, "libgcc_shift_count"))
6420 mode = targetm.libgcc_shift_count_mode ();
6421 else if (!strcmp (p, "unwind_word"))
6422 mode = targetm.unwind_word_mode ();
6423 else
6424 for (j = 0; j < NUM_MACHINE_MODES; j++)
6425 if (!strcmp (p, GET_MODE_NAME (j)))
6427 mode = (enum machine_mode) j;
6428 break;
6431 if (mode == VOIDmode)
6433 error ("unknown machine mode %qE", ident);
6434 return NULL_TREE;
6437 valid_mode = false;
6438 switch (GET_MODE_CLASS (mode))
6440 case MODE_INT:
6441 case MODE_PARTIAL_INT:
6442 case MODE_FLOAT:
6443 case MODE_DECIMAL_FLOAT:
6444 case MODE_FRACT:
6445 case MODE_UFRACT:
6446 case MODE_ACCUM:
6447 case MODE_UACCUM:
6448 valid_mode = targetm.scalar_mode_supported_p (mode);
6449 break;
6451 case MODE_COMPLEX_INT:
6452 case MODE_COMPLEX_FLOAT:
6453 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
6454 break;
6456 case MODE_VECTOR_INT:
6457 case MODE_VECTOR_FLOAT:
6458 case MODE_VECTOR_FRACT:
6459 case MODE_VECTOR_UFRACT:
6460 case MODE_VECTOR_ACCUM:
6461 case MODE_VECTOR_UACCUM:
6462 warning (OPT_Wattributes, "specifying vector types with "
6463 "__attribute__ ((mode)) is deprecated");
6464 warning (OPT_Wattributes,
6465 "use __attribute__ ((vector_size)) instead");
6466 valid_mode = vector_mode_valid_p (mode);
6467 break;
6469 default:
6470 break;
6472 if (!valid_mode)
6474 error ("unable to emulate %qs", p);
6475 return NULL_TREE;
6478 if (POINTER_TYPE_P (type))
6480 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
6481 tree (*fn)(tree, enum machine_mode, bool);
6483 if (!targetm.addr_space.valid_pointer_mode (mode, as))
6485 error ("invalid pointer mode %qs", p);
6486 return NULL_TREE;
6489 if (TREE_CODE (type) == POINTER_TYPE)
6490 fn = build_pointer_type_for_mode;
6491 else
6492 fn = build_reference_type_for_mode;
6493 typefm = fn (TREE_TYPE (type), mode, false);
6495 else
6497 /* For fixed-point modes, we need to test if the signness of type
6498 and the machine mode are consistent. */
6499 if (ALL_FIXED_POINT_MODE_P (mode)
6500 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
6502 error ("signedness of type and machine mode %qs don%'t match", p);
6503 return NULL_TREE;
6505 /* For fixed-point modes, we need to pass saturating info. */
6506 typefm = lang_hooks.types.type_for_mode (mode,
6507 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
6508 : TYPE_UNSIGNED (type));
6511 if (typefm == NULL_TREE)
6513 error ("no data type for mode %qs", p);
6514 return NULL_TREE;
6516 else if (TREE_CODE (type) == ENUMERAL_TYPE)
6518 /* For enumeral types, copy the precision from the integer
6519 type returned above. If not an INTEGER_TYPE, we can't use
6520 this mode for this type. */
6521 if (TREE_CODE (typefm) != INTEGER_TYPE)
6523 error ("cannot use mode %qs for enumeral types", p);
6524 return NULL_TREE;
6527 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
6529 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
6530 typefm = type;
6532 else
6534 /* We cannot build a type variant, as there's code that assumes
6535 that TYPE_MAIN_VARIANT has the same mode. This includes the
6536 debug generators. Instead, create a subrange type. This
6537 results in all of the enumeral values being emitted only once
6538 in the original, and the subtype gets them by reference. */
6539 if (TYPE_UNSIGNED (type))
6540 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
6541 else
6542 typefm = make_signed_type (TYPE_PRECISION (typefm));
6543 TREE_TYPE (typefm) = type;
6546 else if (VECTOR_MODE_P (mode)
6547 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
6548 : TREE_CODE (type) != TREE_CODE (typefm))
6550 error ("mode %qs applied to inappropriate type", p);
6551 return NULL_TREE;
6554 *node = typefm;
6557 return NULL_TREE;
6560 /* Handle a "section" attribute; arguments as in
6561 struct attribute_spec.handler. */
6563 static tree
6564 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6565 int ARG_UNUSED (flags), bool *no_add_attrs)
6567 tree decl = *node;
6569 if (targetm.have_named_sections)
6571 user_defined_section_attribute = true;
6573 if ((TREE_CODE (decl) == FUNCTION_DECL
6574 || TREE_CODE (decl) == VAR_DECL)
6575 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6577 if (TREE_CODE (decl) == VAR_DECL
6578 && current_function_decl != NULL_TREE
6579 && !TREE_STATIC (decl))
6581 error_at (DECL_SOURCE_LOCATION (decl),
6582 "section attribute cannot be specified for "
6583 "local variables");
6584 *no_add_attrs = true;
6587 /* The decl may have already been given a section attribute
6588 from a previous declaration. Ensure they match. */
6589 else if (DECL_SECTION_NAME (decl) != NULL_TREE
6590 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
6591 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
6593 error ("section of %q+D conflicts with previous declaration",
6594 *node);
6595 *no_add_attrs = true;
6597 else if (TREE_CODE (decl) == VAR_DECL
6598 && !targetm.have_tls && targetm.emutls.tmpl_section
6599 && DECL_THREAD_LOCAL_P (decl))
6601 error ("section of %q+D cannot be overridden", *node);
6602 *no_add_attrs = true;
6604 else
6605 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
6607 else
6609 error ("section attribute not allowed for %q+D", *node);
6610 *no_add_attrs = true;
6613 else
6615 error_at (DECL_SOURCE_LOCATION (*node),
6616 "section attributes are not supported for this target");
6617 *no_add_attrs = true;
6620 return NULL_TREE;
6623 /* Handle a "aligned" attribute; arguments as in
6624 struct attribute_spec.handler. */
6626 static tree
6627 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6628 int flags, bool *no_add_attrs)
6630 tree decl = NULL_TREE;
6631 tree *type = NULL;
6632 int is_type = 0;
6633 tree align_expr = (args ? TREE_VALUE (args)
6634 : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
6635 int i;
6637 if (DECL_P (*node))
6639 decl = *node;
6640 type = &TREE_TYPE (decl);
6641 is_type = TREE_CODE (*node) == TYPE_DECL;
6643 else if (TYPE_P (*node))
6644 type = node, is_type = 1;
6646 if (TREE_CODE (align_expr) != INTEGER_CST)
6648 error ("requested alignment is not a constant");
6649 *no_add_attrs = true;
6651 else if ((i = tree_log2 (align_expr)) == -1)
6653 error ("requested alignment is not a power of 2");
6654 *no_add_attrs = true;
6656 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
6658 error ("requested alignment is too large");
6659 *no_add_attrs = true;
6661 else if (is_type)
6663 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6664 /* OK, modify the type in place. */;
6665 /* If we have a TYPE_DECL, then copy the type, so that we
6666 don't accidentally modify a builtin type. See pushdecl. */
6667 else if (decl && TREE_TYPE (decl) != error_mark_node
6668 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
6670 tree tt = TREE_TYPE (decl);
6671 *type = build_variant_type_copy (*type);
6672 DECL_ORIGINAL_TYPE (decl) = tt;
6673 TYPE_NAME (*type) = decl;
6674 TREE_USED (*type) = TREE_USED (decl);
6675 TREE_TYPE (decl) = *type;
6677 else
6678 *type = build_variant_type_copy (*type);
6680 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
6681 TYPE_USER_ALIGN (*type) = 1;
6683 else if (! VAR_OR_FUNCTION_DECL_P (decl)
6684 && TREE_CODE (decl) != FIELD_DECL)
6686 error ("alignment may not be specified for %q+D", decl);
6687 *no_add_attrs = true;
6689 else if (TREE_CODE (decl) == FUNCTION_DECL
6690 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
6692 if (DECL_USER_ALIGN (decl))
6693 error ("alignment for %q+D was previously specified as %d "
6694 "and may not be decreased", decl,
6695 DECL_ALIGN (decl) / BITS_PER_UNIT);
6696 else
6697 error ("alignment for %q+D must be at least %d", decl,
6698 DECL_ALIGN (decl) / BITS_PER_UNIT);
6699 *no_add_attrs = true;
6701 else
6703 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
6704 DECL_USER_ALIGN (decl) = 1;
6707 return NULL_TREE;
6710 /* Handle a "weak" attribute; arguments as in
6711 struct attribute_spec.handler. */
6713 static tree
6714 handle_weak_attribute (tree *node, tree name,
6715 tree ARG_UNUSED (args),
6716 int ARG_UNUSED (flags),
6717 bool * ARG_UNUSED (no_add_attrs))
6719 if (TREE_CODE (*node) == FUNCTION_DECL
6720 && DECL_DECLARED_INLINE_P (*node))
6722 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6723 *no_add_attrs = true;
6725 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6727 error ("indirect function %q+D cannot be declared weak", *node);
6728 *no_add_attrs = true;
6729 return NULL_TREE;
6731 else if (TREE_CODE (*node) == FUNCTION_DECL
6732 || TREE_CODE (*node) == VAR_DECL)
6733 declare_weak (*node);
6734 else
6735 warning (OPT_Wattributes, "%qE attribute ignored", name);
6737 return NULL_TREE;
6740 /* Handle an "alias" or "ifunc" attribute; arguments as in
6741 struct attribute_spec.handler, except that IS_ALIAS tells us
6742 whether this is an alias as opposed to ifunc attribute. */
6744 static tree
6745 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
6746 bool *no_add_attrs)
6748 tree decl = *node;
6750 if (TREE_CODE (decl) != FUNCTION_DECL
6751 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
6753 warning (OPT_Wattributes, "%qE attribute ignored", name);
6754 *no_add_attrs = true;
6756 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
6757 || (TREE_CODE (decl) != FUNCTION_DECL
6758 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
6759 /* A static variable declaration is always a tentative definition,
6760 but the alias is a non-tentative definition which overrides. */
6761 || (TREE_CODE (decl) != FUNCTION_DECL
6762 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
6764 error ("%q+D defined both normally and as %qE attribute", decl, name);
6765 *no_add_attrs = true;
6766 return NULL_TREE;
6768 else if (!is_alias
6769 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
6770 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
6772 error ("weak %q+D cannot be defined %qE", decl, name);
6773 *no_add_attrs = true;
6774 return NULL_TREE;
6777 /* Note that the very first time we process a nested declaration,
6778 decl_function_context will not be set. Indeed, *would* never
6779 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
6780 we do below. After such frobbery, pushdecl would set the context.
6781 In any case, this is never what we want. */
6782 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
6784 tree id;
6786 id = TREE_VALUE (args);
6787 if (TREE_CODE (id) != STRING_CST)
6789 error ("attribute %qE argument not a string", name);
6790 *no_add_attrs = true;
6791 return NULL_TREE;
6793 id = get_identifier (TREE_STRING_POINTER (id));
6794 /* This counts as a use of the object pointed to. */
6795 TREE_USED (id) = 1;
6797 if (TREE_CODE (decl) == FUNCTION_DECL)
6798 DECL_INITIAL (decl) = error_mark_node;
6799 else
6801 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6802 DECL_EXTERNAL (decl) = 1;
6803 else
6804 DECL_EXTERNAL (decl) = 0;
6805 TREE_STATIC (decl) = 1;
6808 if (!is_alias)
6809 /* ifuncs are also aliases, so set that attribute too. */
6810 DECL_ATTRIBUTES (decl)
6811 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
6813 else
6815 warning (OPT_Wattributes, "%qE attribute ignored", name);
6816 *no_add_attrs = true;
6819 return NULL_TREE;
6822 /* Handle an "alias" or "ifunc" attribute; arguments as in
6823 struct attribute_spec.handler. */
6825 static tree
6826 handle_ifunc_attribute (tree *node, tree name, tree args,
6827 int ARG_UNUSED (flags), bool *no_add_attrs)
6829 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
6832 /* Handle an "alias" or "ifunc" attribute; arguments as in
6833 struct attribute_spec.handler. */
6835 static tree
6836 handle_alias_attribute (tree *node, tree name, tree args,
6837 int ARG_UNUSED (flags), bool *no_add_attrs)
6839 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
6842 /* Handle a "weakref" attribute; arguments as in struct
6843 attribute_spec.handler. */
6845 static tree
6846 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
6847 int flags, bool *no_add_attrs)
6849 tree attr = NULL_TREE;
6851 /* We must ignore the attribute when it is associated with
6852 local-scoped decls, since attribute alias is ignored and many
6853 such symbols do not even have a DECL_WEAK field. */
6854 if (decl_function_context (*node)
6855 || current_function_decl
6856 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
6858 warning (OPT_Wattributes, "%qE attribute ignored", name);
6859 *no_add_attrs = true;
6860 return NULL_TREE;
6863 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
6865 error ("indirect function %q+D cannot be declared weakref", *node);
6866 *no_add_attrs = true;
6867 return NULL_TREE;
6870 /* The idea here is that `weakref("name")' mutates into `weakref,
6871 alias("name")', and weakref without arguments, in turn,
6872 implicitly adds weak. */
6874 if (args)
6876 attr = tree_cons (get_identifier ("alias"), args, attr);
6877 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
6879 *no_add_attrs = true;
6881 decl_attributes (node, attr, flags);
6883 else
6885 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
6886 error_at (DECL_SOURCE_LOCATION (*node),
6887 "weakref attribute must appear before alias attribute");
6889 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
6890 and that isn't supported; and because it wants to add it to
6891 the list of weak decls, which isn't helpful. */
6892 DECL_WEAK (*node) = 1;
6895 return NULL_TREE;
6898 /* Handle an "visibility" attribute; arguments as in
6899 struct attribute_spec.handler. */
6901 static tree
6902 handle_visibility_attribute (tree *node, tree name, tree args,
6903 int ARG_UNUSED (flags),
6904 bool *ARG_UNUSED (no_add_attrs))
6906 tree decl = *node;
6907 tree id = TREE_VALUE (args);
6908 enum symbol_visibility vis;
6910 if (TYPE_P (*node))
6912 if (TREE_CODE (*node) == ENUMERAL_TYPE)
6913 /* OK */;
6914 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
6916 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
6917 name);
6918 return NULL_TREE;
6920 else if (TYPE_FIELDS (*node))
6922 error ("%qE attribute ignored because %qT is already defined",
6923 name, *node);
6924 return NULL_TREE;
6927 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
6929 warning (OPT_Wattributes, "%qE attribute ignored", name);
6930 return NULL_TREE;
6933 if (TREE_CODE (id) != STRING_CST)
6935 error ("visibility argument not a string");
6936 return NULL_TREE;
6939 /* If this is a type, set the visibility on the type decl. */
6940 if (TYPE_P (decl))
6942 decl = TYPE_NAME (decl);
6943 if (!decl)
6944 return NULL_TREE;
6945 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6947 warning (OPT_Wattributes, "%qE attribute ignored on types",
6948 name);
6949 return NULL_TREE;
6953 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
6954 vis = VISIBILITY_DEFAULT;
6955 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
6956 vis = VISIBILITY_INTERNAL;
6957 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
6958 vis = VISIBILITY_HIDDEN;
6959 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
6960 vis = VISIBILITY_PROTECTED;
6961 else
6963 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
6964 vis = VISIBILITY_DEFAULT;
6967 if (DECL_VISIBILITY_SPECIFIED (decl)
6968 && vis != DECL_VISIBILITY (decl))
6970 tree attributes = (TYPE_P (*node)
6971 ? TYPE_ATTRIBUTES (*node)
6972 : DECL_ATTRIBUTES (decl));
6973 if (lookup_attribute ("visibility", attributes))
6974 error ("%qD redeclared with different visibility", decl);
6975 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6976 && lookup_attribute ("dllimport", attributes))
6977 error ("%qD was declared %qs which implies default visibility",
6978 decl, "dllimport");
6979 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6980 && lookup_attribute ("dllexport", attributes))
6981 error ("%qD was declared %qs which implies default visibility",
6982 decl, "dllexport");
6985 DECL_VISIBILITY (decl) = vis;
6986 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6988 /* Go ahead and attach the attribute to the node as well. This is needed
6989 so we can determine whether we have VISIBILITY_DEFAULT because the
6990 visibility was not specified, or because it was explicitly overridden
6991 from the containing scope. */
6993 return NULL_TREE;
6996 /* Determine the ELF symbol visibility for DECL, which is either a
6997 variable or a function. It is an error to use this function if a
6998 definition of DECL is not available in this translation unit.
6999 Returns true if the final visibility has been determined by this
7000 function; false if the caller is free to make additional
7001 modifications. */
7003 bool
7004 c_determine_visibility (tree decl)
7006 gcc_assert (TREE_CODE (decl) == VAR_DECL
7007 || TREE_CODE (decl) == FUNCTION_DECL);
7009 /* If the user explicitly specified the visibility with an
7010 attribute, honor that. DECL_VISIBILITY will have been set during
7011 the processing of the attribute. We check for an explicit
7012 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7013 to distinguish the use of an attribute from the use of a "#pragma
7014 GCC visibility push(...)"; in the latter case we still want other
7015 considerations to be able to overrule the #pragma. */
7016 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7017 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7018 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7019 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7020 return true;
7022 /* Set default visibility to whatever the user supplied with
7023 visibility_specified depending on #pragma GCC visibility. */
7024 if (!DECL_VISIBILITY_SPECIFIED (decl))
7026 if (visibility_options.inpragma
7027 || DECL_VISIBILITY (decl) != default_visibility)
7029 DECL_VISIBILITY (decl) = default_visibility;
7030 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7031 /* If visibility changed and DECL already has DECL_RTL, ensure
7032 symbol flags are updated. */
7033 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7034 || TREE_CODE (decl) == FUNCTION_DECL)
7035 && DECL_RTL_SET_P (decl))
7036 make_decl_rtl (decl);
7039 return false;
7042 /* Handle an "tls_model" attribute; arguments as in
7043 struct attribute_spec.handler. */
7045 static tree
7046 handle_tls_model_attribute (tree *node, tree name, tree args,
7047 int ARG_UNUSED (flags), bool *no_add_attrs)
7049 tree id;
7050 tree decl = *node;
7051 enum tls_model kind;
7053 *no_add_attrs = true;
7055 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7057 warning (OPT_Wattributes, "%qE attribute ignored", name);
7058 return NULL_TREE;
7061 kind = DECL_TLS_MODEL (decl);
7062 id = TREE_VALUE (args);
7063 if (TREE_CODE (id) != STRING_CST)
7065 error ("tls_model argument not a string");
7066 return NULL_TREE;
7069 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7070 kind = TLS_MODEL_LOCAL_EXEC;
7071 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7072 kind = TLS_MODEL_INITIAL_EXEC;
7073 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7074 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7075 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7076 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7077 else
7078 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7080 DECL_TLS_MODEL (decl) = kind;
7081 return NULL_TREE;
7084 /* Handle a "no_instrument_function" attribute; arguments as in
7085 struct attribute_spec.handler. */
7087 static tree
7088 handle_no_instrument_function_attribute (tree *node, tree name,
7089 tree ARG_UNUSED (args),
7090 int ARG_UNUSED (flags),
7091 bool *no_add_attrs)
7093 tree decl = *node;
7095 if (TREE_CODE (decl) != FUNCTION_DECL)
7097 error_at (DECL_SOURCE_LOCATION (decl),
7098 "%qE attribute applies only to functions", name);
7099 *no_add_attrs = true;
7101 else if (DECL_INITIAL (decl))
7103 error_at (DECL_SOURCE_LOCATION (decl),
7104 "can%'t set %qE attribute after definition", name);
7105 *no_add_attrs = true;
7107 else
7108 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
7110 return NULL_TREE;
7113 /* Handle a "malloc" attribute; arguments as in
7114 struct attribute_spec.handler. */
7116 static tree
7117 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7118 int ARG_UNUSED (flags), bool *no_add_attrs)
7120 if (TREE_CODE (*node) == FUNCTION_DECL
7121 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
7122 DECL_IS_MALLOC (*node) = 1;
7123 else
7125 warning (OPT_Wattributes, "%qE attribute ignored", name);
7126 *no_add_attrs = true;
7129 return NULL_TREE;
7132 /* Handle a "alloc_size" attribute; arguments as in
7133 struct attribute_spec.handler. */
7135 static tree
7136 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7137 int ARG_UNUSED (flags), bool *no_add_attrs)
7139 unsigned arg_count = type_num_arguments (*node);
7140 for (; args; args = TREE_CHAIN (args))
7142 tree position = TREE_VALUE (args);
7144 if (TREE_CODE (position) != INTEGER_CST
7145 || TREE_INT_CST_HIGH (position)
7146 || TREE_INT_CST_LOW (position) < 1
7147 || TREE_INT_CST_LOW (position) > arg_count )
7149 warning (OPT_Wattributes,
7150 "alloc_size parameter outside range");
7151 *no_add_attrs = true;
7152 return NULL_TREE;
7155 return NULL_TREE;
7158 /* Handle a "fn spec" attribute; arguments as in
7159 struct attribute_spec.handler. */
7161 static tree
7162 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
7163 tree args, int ARG_UNUSED (flags),
7164 bool *no_add_attrs ATTRIBUTE_UNUSED)
7166 gcc_assert (args
7167 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
7168 && !TREE_CHAIN (args));
7169 return NULL_TREE;
7172 /* Handle a "returns_twice" attribute; arguments as in
7173 struct attribute_spec.handler. */
7175 static tree
7176 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7177 int ARG_UNUSED (flags), bool *no_add_attrs)
7179 if (TREE_CODE (*node) == FUNCTION_DECL)
7180 DECL_IS_RETURNS_TWICE (*node) = 1;
7181 else
7183 warning (OPT_Wattributes, "%qE attribute ignored", name);
7184 *no_add_attrs = true;
7187 return NULL_TREE;
7190 /* Handle a "no_limit_stack" attribute; arguments as in
7191 struct attribute_spec.handler. */
7193 static tree
7194 handle_no_limit_stack_attribute (tree *node, tree name,
7195 tree ARG_UNUSED (args),
7196 int ARG_UNUSED (flags),
7197 bool *no_add_attrs)
7199 tree decl = *node;
7201 if (TREE_CODE (decl) != FUNCTION_DECL)
7203 error_at (DECL_SOURCE_LOCATION (decl),
7204 "%qE attribute applies only to functions", name);
7205 *no_add_attrs = true;
7207 else if (DECL_INITIAL (decl))
7209 error_at (DECL_SOURCE_LOCATION (decl),
7210 "can%'t set %qE attribute after definition", name);
7211 *no_add_attrs = true;
7213 else
7214 DECL_NO_LIMIT_STACK (decl) = 1;
7216 return NULL_TREE;
7219 /* Handle a "pure" attribute; arguments as in
7220 struct attribute_spec.handler. */
7222 static tree
7223 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7224 int ARG_UNUSED (flags), bool *no_add_attrs)
7226 if (TREE_CODE (*node) == FUNCTION_DECL)
7227 DECL_PURE_P (*node) = 1;
7228 /* ??? TODO: Support types. */
7229 else
7231 warning (OPT_Wattributes, "%qE attribute ignored", name);
7232 *no_add_attrs = true;
7235 return NULL_TREE;
7238 /* Handle a "no vops" attribute; arguments as in
7239 struct attribute_spec.handler. */
7241 static tree
7242 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
7243 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7244 bool *ARG_UNUSED (no_add_attrs))
7246 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
7247 DECL_IS_NOVOPS (*node) = 1;
7248 return NULL_TREE;
7251 /* Handle a "deprecated" attribute; arguments as in
7252 struct attribute_spec.handler. */
7254 static tree
7255 handle_deprecated_attribute (tree *node, tree name,
7256 tree args, int flags,
7257 bool *no_add_attrs)
7259 tree type = NULL_TREE;
7260 int warn = 0;
7261 tree what = NULL_TREE;
7263 if (!args)
7264 *no_add_attrs = true;
7265 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7267 error ("deprecated message is not a string");
7268 *no_add_attrs = true;
7271 if (DECL_P (*node))
7273 tree decl = *node;
7274 type = TREE_TYPE (decl);
7276 if (TREE_CODE (decl) == TYPE_DECL
7277 || TREE_CODE (decl) == PARM_DECL
7278 || TREE_CODE (decl) == VAR_DECL
7279 || TREE_CODE (decl) == FUNCTION_DECL
7280 || TREE_CODE (decl) == FIELD_DECL
7281 || objc_method_decl (TREE_CODE (decl)))
7282 TREE_DEPRECATED (decl) = 1;
7283 else
7284 warn = 1;
7286 else if (TYPE_P (*node))
7288 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7289 *node = build_variant_type_copy (*node);
7290 TREE_DEPRECATED (*node) = 1;
7291 type = *node;
7293 else
7294 warn = 1;
7296 if (warn)
7298 *no_add_attrs = true;
7299 if (type && TYPE_NAME (type))
7301 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7302 what = TYPE_NAME (*node);
7303 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7304 && DECL_NAME (TYPE_NAME (type)))
7305 what = DECL_NAME (TYPE_NAME (type));
7307 if (what)
7308 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
7309 else
7310 warning (OPT_Wattributes, "%qE attribute ignored", name);
7313 return NULL_TREE;
7316 /* Handle a "vector_size" attribute; arguments as in
7317 struct attribute_spec.handler. */
7319 static tree
7320 handle_vector_size_attribute (tree *node, tree name, tree args,
7321 int ARG_UNUSED (flags),
7322 bool *no_add_attrs)
7324 unsigned HOST_WIDE_INT vecsize, nunits;
7325 enum machine_mode orig_mode;
7326 tree type = *node, new_type, size;
7328 *no_add_attrs = true;
7330 size = TREE_VALUE (args);
7332 if (!host_integerp (size, 1))
7334 warning (OPT_Wattributes, "%qE attribute ignored", name);
7335 return NULL_TREE;
7338 /* Get the vector size (in bytes). */
7339 vecsize = tree_low_cst (size, 1);
7341 /* We need to provide for vector pointers, vector arrays, and
7342 functions returning vectors. For example:
7344 __attribute__((vector_size(16))) short *foo;
7346 In this case, the mode is SI, but the type being modified is
7347 HI, so we need to look further. */
7349 while (POINTER_TYPE_P (type)
7350 || TREE_CODE (type) == FUNCTION_TYPE
7351 || TREE_CODE (type) == METHOD_TYPE
7352 || TREE_CODE (type) == ARRAY_TYPE
7353 || TREE_CODE (type) == OFFSET_TYPE)
7354 type = TREE_TYPE (type);
7356 /* Get the mode of the type being modified. */
7357 orig_mode = TYPE_MODE (type);
7359 if ((!INTEGRAL_TYPE_P (type)
7360 && !SCALAR_FLOAT_TYPE_P (type)
7361 && !FIXED_POINT_TYPE_P (type))
7362 || (!SCALAR_FLOAT_MODE_P (orig_mode)
7363 && GET_MODE_CLASS (orig_mode) != MODE_INT
7364 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
7365 || !host_integerp (TYPE_SIZE_UNIT (type), 1)
7366 || TREE_CODE (type) == BOOLEAN_TYPE)
7368 error ("invalid vector type for attribute %qE", name);
7369 return NULL_TREE;
7372 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
7374 error ("vector size not an integral multiple of component size");
7375 return NULL;
7378 if (vecsize == 0)
7380 error ("zero vector size");
7381 return NULL;
7384 /* Calculate how many units fit in the vector. */
7385 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
7386 if (nunits & (nunits - 1))
7388 error ("number of components of the vector not a power of two");
7389 return NULL_TREE;
7392 new_type = build_vector_type (type, nunits);
7394 /* Build back pointers if needed. */
7395 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
7397 return NULL_TREE;
7400 /* Handle the "nonnull" attribute. */
7401 static tree
7402 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
7403 tree args, int ARG_UNUSED (flags),
7404 bool *no_add_attrs)
7406 tree type = *node;
7407 unsigned HOST_WIDE_INT attr_arg_num;
7409 /* If no arguments are specified, all pointer arguments should be
7410 non-null. Verify a full prototype is given so that the arguments
7411 will have the correct types when we actually check them later. */
7412 if (!args)
7414 if (!prototype_p (type))
7416 error ("nonnull attribute without arguments on a non-prototype");
7417 *no_add_attrs = true;
7419 return NULL_TREE;
7422 /* Argument list specified. Verify that each argument number references
7423 a pointer argument. */
7424 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
7426 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
7428 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
7430 error ("nonnull argument has invalid operand number (argument %lu)",
7431 (unsigned long) attr_arg_num);
7432 *no_add_attrs = true;
7433 return NULL_TREE;
7436 if (prototype_p (type))
7438 function_args_iterator iter;
7439 tree argument;
7441 function_args_iter_init (&iter, type);
7442 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
7444 argument = function_args_iter_cond (&iter);
7445 if (argument == NULL_TREE || ck_num == arg_num)
7446 break;
7449 if (!argument
7450 || TREE_CODE (argument) == VOID_TYPE)
7452 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
7453 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7454 *no_add_attrs = true;
7455 return NULL_TREE;
7458 if (TREE_CODE (argument) != POINTER_TYPE)
7460 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
7461 (unsigned long) attr_arg_num, (unsigned long) arg_num);
7462 *no_add_attrs = true;
7463 return NULL_TREE;
7468 return NULL_TREE;
7471 /* Check the argument list of a function call for null in argument slots
7472 that are marked as requiring a non-null pointer argument. The NARGS
7473 arguments are passed in the array ARGARRAY.
7476 static void
7477 check_function_nonnull (tree attrs, int nargs, tree *argarray)
7479 tree a, args;
7480 int i;
7482 for (a = attrs; a; a = TREE_CHAIN (a))
7484 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
7486 args = TREE_VALUE (a);
7488 /* Walk the argument list. If we encounter an argument number we
7489 should check for non-null, do it. If the attribute has no args,
7490 then every pointer argument is checked (in which case the check
7491 for pointer type is done in check_nonnull_arg). */
7492 for (i = 0; i < nargs; i++)
7494 if (!args || nonnull_check_p (args, i + 1))
7495 check_function_arguments_recurse (check_nonnull_arg, NULL,
7496 argarray[i],
7497 i + 1);
7503 /* Check that the Nth argument of a function call (counting backwards
7504 from the end) is a (pointer)0. The NARGS arguments are passed in the
7505 array ARGARRAY. */
7507 static void
7508 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
7510 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
7512 if (attr)
7514 int len = 0;
7515 int pos = 0;
7516 tree sentinel;
7517 function_args_iterator iter;
7518 tree t;
7520 /* Skip over the named arguments. */
7521 FOREACH_FUNCTION_ARGS (fntype, t, iter)
7523 if (len == nargs)
7524 break;
7525 len++;
7528 if (TREE_VALUE (attr))
7530 tree p = TREE_VALUE (TREE_VALUE (attr));
7531 pos = TREE_INT_CST_LOW (p);
7534 /* The sentinel must be one of the varargs, i.e.
7535 in position >= the number of fixed arguments. */
7536 if ((nargs - 1 - pos) < len)
7538 warning (OPT_Wformat,
7539 "not enough variable arguments to fit a sentinel");
7540 return;
7543 /* Validate the sentinel. */
7544 sentinel = argarray[nargs - 1 - pos];
7545 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
7546 || !integer_zerop (sentinel))
7547 /* Although __null (in C++) is only an integer we allow it
7548 nevertheless, as we are guaranteed that it's exactly
7549 as wide as a pointer, and we don't want to force
7550 users to cast the NULL they have written there.
7551 We warn with -Wstrict-null-sentinel, though. */
7552 && (warn_strict_null_sentinel || null_node != sentinel))
7553 warning (OPT_Wformat, "missing sentinel in function call");
7557 /* Helper for check_function_nonnull; given a list of operands which
7558 must be non-null in ARGS, determine if operand PARAM_NUM should be
7559 checked. */
7561 static bool
7562 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
7564 unsigned HOST_WIDE_INT arg_num = 0;
7566 for (; args; args = TREE_CHAIN (args))
7568 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
7570 gcc_assert (found);
7572 if (arg_num == param_num)
7573 return true;
7575 return false;
7578 /* Check that the function argument PARAM (which is operand number
7579 PARAM_NUM) is non-null. This is called by check_function_nonnull
7580 via check_function_arguments_recurse. */
7582 static void
7583 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
7584 unsigned HOST_WIDE_INT param_num)
7586 /* Just skip checking the argument if it's not a pointer. This can
7587 happen if the "nonnull" attribute was given without an operand
7588 list (which means to check every pointer argument). */
7590 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
7591 return;
7593 if (integer_zerop (param))
7594 warning (OPT_Wnonnull, "null argument where non-null required "
7595 "(argument %lu)", (unsigned long) param_num);
7598 /* Helper for nonnull attribute handling; fetch the operand number
7599 from the attribute argument list. */
7601 static bool
7602 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
7604 /* Verify the arg number is a constant. */
7605 if (TREE_CODE (arg_num_expr) != INTEGER_CST
7606 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
7607 return false;
7609 *valp = TREE_INT_CST_LOW (arg_num_expr);
7610 return true;
7613 /* Handle a "nothrow" attribute; arguments as in
7614 struct attribute_spec.handler. */
7616 static tree
7617 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7618 int ARG_UNUSED (flags), bool *no_add_attrs)
7620 if (TREE_CODE (*node) == FUNCTION_DECL)
7621 TREE_NOTHROW (*node) = 1;
7622 /* ??? TODO: Support types. */
7623 else
7625 warning (OPT_Wattributes, "%qE attribute ignored", name);
7626 *no_add_attrs = true;
7629 return NULL_TREE;
7632 /* Handle a "cleanup" attribute; arguments as in
7633 struct attribute_spec.handler. */
7635 static tree
7636 handle_cleanup_attribute (tree *node, tree name, tree args,
7637 int ARG_UNUSED (flags), bool *no_add_attrs)
7639 tree decl = *node;
7640 tree cleanup_id, cleanup_decl;
7642 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
7643 for global destructors in C++. This requires infrastructure that
7644 we don't have generically at the moment. It's also not a feature
7645 we'd be missing too much, since we do have attribute constructor. */
7646 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
7648 warning (OPT_Wattributes, "%qE attribute ignored", name);
7649 *no_add_attrs = true;
7650 return NULL_TREE;
7653 /* Verify that the argument is a function in scope. */
7654 /* ??? We could support pointers to functions here as well, if
7655 that was considered desirable. */
7656 cleanup_id = TREE_VALUE (args);
7657 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
7659 error ("cleanup argument not an identifier");
7660 *no_add_attrs = true;
7661 return NULL_TREE;
7663 cleanup_decl = lookup_name (cleanup_id);
7664 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
7666 error ("cleanup argument not a function");
7667 *no_add_attrs = true;
7668 return NULL_TREE;
7671 /* That the function has proper type is checked with the
7672 eventual call to build_function_call. */
7674 return NULL_TREE;
7677 /* Handle a "warn_unused_result" attribute. No special handling. */
7679 static tree
7680 handle_warn_unused_result_attribute (tree *node, tree name,
7681 tree ARG_UNUSED (args),
7682 int ARG_UNUSED (flags), bool *no_add_attrs)
7684 /* Ignore the attribute for functions not returning any value. */
7685 if (VOID_TYPE_P (TREE_TYPE (*node)))
7687 warning (OPT_Wattributes, "%qE attribute ignored", name);
7688 *no_add_attrs = true;
7691 return NULL_TREE;
7694 /* Handle a "sentinel" attribute. */
7696 static tree
7697 handle_sentinel_attribute (tree *node, tree name, tree args,
7698 int ARG_UNUSED (flags), bool *no_add_attrs)
7700 if (!prototype_p (*node))
7702 warning (OPT_Wattributes,
7703 "%qE attribute requires prototypes with named arguments", name);
7704 *no_add_attrs = true;
7706 else
7708 if (!stdarg_p (*node))
7710 warning (OPT_Wattributes,
7711 "%qE attribute only applies to variadic functions", name);
7712 *no_add_attrs = true;
7716 if (args)
7718 tree position = TREE_VALUE (args);
7720 if (TREE_CODE (position) != INTEGER_CST)
7722 warning (OPT_Wattributes,
7723 "requested position is not an integer constant");
7724 *no_add_attrs = true;
7726 else
7728 if (tree_int_cst_lt (position, integer_zero_node))
7730 warning (OPT_Wattributes,
7731 "requested position is less than zero");
7732 *no_add_attrs = true;
7737 return NULL_TREE;
7740 /* Handle a "type_generic" attribute. */
7742 static tree
7743 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
7744 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
7745 bool * ARG_UNUSED (no_add_attrs))
7747 /* Ensure we have a function type. */
7748 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
7750 /* Ensure we have a variadic function. */
7751 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
7753 return NULL_TREE;
7756 /* Handle a "target" attribute. */
7758 static tree
7759 handle_target_attribute (tree *node, tree name, tree args, int flags,
7760 bool *no_add_attrs)
7762 /* Ensure we have a function type. */
7763 if (TREE_CODE (*node) != FUNCTION_DECL)
7765 warning (OPT_Wattributes, "%qE attribute ignored", name);
7766 *no_add_attrs = true;
7768 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
7769 flags))
7770 *no_add_attrs = true;
7772 return NULL_TREE;
7775 /* Arguments being collected for optimization. */
7776 typedef const char *const_char_p; /* For DEF_VEC_P. */
7777 DEF_VEC_P(const_char_p);
7778 DEF_VEC_ALLOC_P(const_char_p, gc);
7779 static GTY(()) VEC(const_char_p, gc) *optimize_args;
7782 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
7783 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
7784 false for #pragma GCC optimize. */
7786 bool
7787 parse_optimize_options (tree args, bool attr_p)
7789 bool ret = true;
7790 unsigned opt_argc;
7791 unsigned i;
7792 int saved_flag_strict_aliasing;
7793 const char **opt_argv;
7794 struct cl_decoded_option *decoded_options;
7795 unsigned int decoded_options_count;
7796 tree ap;
7798 /* Build up argv vector. Just in case the string is stored away, use garbage
7799 collected strings. */
7800 VEC_truncate (const_char_p, optimize_args, 0);
7801 VEC_safe_push (const_char_p, gc, optimize_args, NULL);
7803 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
7805 tree value = TREE_VALUE (ap);
7807 if (TREE_CODE (value) == INTEGER_CST)
7809 char buffer[20];
7810 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
7811 VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer));
7814 else if (TREE_CODE (value) == STRING_CST)
7816 /* Split string into multiple substrings. */
7817 size_t len = TREE_STRING_LENGTH (value);
7818 char *p = ASTRDUP (TREE_STRING_POINTER (value));
7819 char *end = p + len;
7820 char *comma;
7821 char *next_p = p;
7823 while (next_p != NULL)
7825 size_t len2;
7826 char *q, *r;
7828 p = next_p;
7829 comma = strchr (p, ',');
7830 if (comma)
7832 len2 = comma - p;
7833 *comma = '\0';
7834 next_p = comma+1;
7836 else
7838 len2 = end - p;
7839 next_p = NULL;
7842 r = q = (char *) ggc_alloc_atomic (len2 + 3);
7844 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
7845 options. */
7846 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
7848 ret = false;
7849 if (attr_p)
7850 warning (OPT_Wattributes,
7851 "bad option %s to optimize attribute", p);
7852 else
7853 warning (OPT_Wpragmas,
7854 "bad option %s to pragma attribute", p);
7855 continue;
7858 if (*p != '-')
7860 *r++ = '-';
7862 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
7863 itself is -Os, and any other switch begins with a -f. */
7864 if ((*p >= '0' && *p <= '9')
7865 || (p[0] == 's' && p[1] == '\0'))
7866 *r++ = 'O';
7867 else if (*p != 'O')
7868 *r++ = 'f';
7871 memcpy (r, p, len2);
7872 r[len2] = '\0';
7873 VEC_safe_push (const_char_p, gc, optimize_args, q);
7879 opt_argc = VEC_length (const_char_p, optimize_args);
7880 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
7882 for (i = 1; i < opt_argc; i++)
7883 opt_argv[i] = VEC_index (const_char_p, optimize_args, i);
7885 saved_flag_strict_aliasing = flag_strict_aliasing;
7887 /* Now parse the options. */
7888 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
7889 &decoded_options,
7890 &decoded_options_count);
7891 decode_options (&global_options, &global_options_set,
7892 decoded_options, decoded_options_count,
7893 input_location, global_dc);
7895 targetm.override_options_after_change();
7897 /* Don't allow changing -fstrict-aliasing. */
7898 flag_strict_aliasing = saved_flag_strict_aliasing;
7900 VEC_truncate (const_char_p, optimize_args, 0);
7901 return ret;
7904 /* For handling "optimize" attribute. arguments as in
7905 struct attribute_spec.handler. */
7907 static tree
7908 handle_optimize_attribute (tree *node, tree name, tree args,
7909 int ARG_UNUSED (flags), bool *no_add_attrs)
7911 /* Ensure we have a function type. */
7912 if (TREE_CODE (*node) != FUNCTION_DECL)
7914 warning (OPT_Wattributes, "%qE attribute ignored", name);
7915 *no_add_attrs = true;
7917 else
7919 struct cl_optimization cur_opts;
7920 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
7922 /* Save current options. */
7923 cl_optimization_save (&cur_opts, &global_options);
7925 /* If we previously had some optimization options, use them as the
7926 default. */
7927 if (old_opts)
7928 cl_optimization_restore (&global_options,
7929 TREE_OPTIMIZATION (old_opts));
7931 /* Parse options, and update the vector. */
7932 parse_optimize_options (args, true);
7933 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
7934 = build_optimization_node ();
7936 /* Restore current options. */
7937 cl_optimization_restore (&global_options, &cur_opts);
7940 return NULL_TREE;
7943 /* Handle a "no_split_stack" attribute. */
7945 static tree
7946 handle_no_split_stack_attribute (tree *node, tree name,
7947 tree ARG_UNUSED (args),
7948 int ARG_UNUSED (flags),
7949 bool *no_add_attrs)
7951 tree decl = *node;
7953 if (TREE_CODE (decl) != FUNCTION_DECL)
7955 error_at (DECL_SOURCE_LOCATION (decl),
7956 "%qE attribute applies only to functions", name);
7957 *no_add_attrs = true;
7959 else if (DECL_INITIAL (decl))
7961 error_at (DECL_SOURCE_LOCATION (decl),
7962 "can%'t set %qE attribute after definition", name);
7963 *no_add_attrs = true;
7966 return NULL_TREE;
7969 /* Check for valid arguments being passed to a function with FNTYPE.
7970 There are NARGS arguments in the array ARGARRAY. */
7971 void
7972 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
7974 /* Check for null being passed in a pointer argument that must be
7975 non-null. We also need to do this if format checking is enabled. */
7977 if (warn_nonnull)
7978 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7980 /* Check for errors in format strings. */
7982 if (warn_format || warn_missing_format_attribute)
7983 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7985 if (warn_format)
7986 check_function_sentinel (fntype, nargs, argarray);
7989 /* Generic argument checking recursion routine. PARAM is the argument to
7990 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
7991 once the argument is resolved. CTX is context for the callback. */
7992 void
7993 check_function_arguments_recurse (void (*callback)
7994 (void *, tree, unsigned HOST_WIDE_INT),
7995 void *ctx, tree param,
7996 unsigned HOST_WIDE_INT param_num)
7998 if (CONVERT_EXPR_P (param)
7999 && (TYPE_PRECISION (TREE_TYPE (param))
8000 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
8002 /* Strip coercion. */
8003 check_function_arguments_recurse (callback, ctx,
8004 TREE_OPERAND (param, 0), param_num);
8005 return;
8008 if (TREE_CODE (param) == CALL_EXPR)
8010 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
8011 tree attrs;
8012 bool found_format_arg = false;
8014 /* See if this is a call to a known internationalization function
8015 that modifies a format arg. Such a function may have multiple
8016 format_arg attributes (for example, ngettext). */
8018 for (attrs = TYPE_ATTRIBUTES (type);
8019 attrs;
8020 attrs = TREE_CHAIN (attrs))
8021 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
8023 tree inner_arg;
8024 tree format_num_expr;
8025 int format_num;
8026 int i;
8027 call_expr_arg_iterator iter;
8029 /* Extract the argument number, which was previously checked
8030 to be valid. */
8031 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
8033 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
8034 && !TREE_INT_CST_HIGH (format_num_expr));
8036 format_num = TREE_INT_CST_LOW (format_num_expr);
8038 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
8039 inner_arg != 0;
8040 inner_arg = next_call_expr_arg (&iter), i++)
8041 if (i == format_num)
8043 check_function_arguments_recurse (callback, ctx,
8044 inner_arg, param_num);
8045 found_format_arg = true;
8046 break;
8050 /* If we found a format_arg attribute and did a recursive check,
8051 we are done with checking this argument. Otherwise, we continue
8052 and this will be considered a non-literal. */
8053 if (found_format_arg)
8054 return;
8057 if (TREE_CODE (param) == COND_EXPR)
8059 /* Check both halves of the conditional expression. */
8060 check_function_arguments_recurse (callback, ctx,
8061 TREE_OPERAND (param, 1), param_num);
8062 check_function_arguments_recurse (callback, ctx,
8063 TREE_OPERAND (param, 2), param_num);
8064 return;
8067 (*callback) (ctx, param, param_num);
8070 /* Checks for a builtin function FNDECL that the number of arguments
8071 NARGS against the required number REQUIRED and issues an error if
8072 there is a mismatch. Returns true if the number of arguments is
8073 correct, otherwise false. */
8075 static bool
8076 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
8078 if (nargs < required)
8080 error_at (input_location,
8081 "not enough arguments to function %qE", fndecl);
8082 return false;
8084 else if (nargs > required)
8086 error_at (input_location,
8087 "too many arguments to function %qE", fndecl);
8088 return false;
8090 return true;
8093 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
8094 Returns false if there was an error, otherwise true. */
8096 bool
8097 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
8099 if (!DECL_BUILT_IN (fndecl)
8100 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
8101 return true;
8103 switch (DECL_FUNCTION_CODE (fndecl))
8105 case BUILT_IN_CONSTANT_P:
8106 return builtin_function_validate_nargs (fndecl, nargs, 1);
8108 case BUILT_IN_ISFINITE:
8109 case BUILT_IN_ISINF:
8110 case BUILT_IN_ISINF_SIGN:
8111 case BUILT_IN_ISNAN:
8112 case BUILT_IN_ISNORMAL:
8113 if (builtin_function_validate_nargs (fndecl, nargs, 1))
8115 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
8117 error ("non-floating-point argument in call to "
8118 "function %qE", fndecl);
8119 return false;
8121 return true;
8123 return false;
8125 case BUILT_IN_ISGREATER:
8126 case BUILT_IN_ISGREATEREQUAL:
8127 case BUILT_IN_ISLESS:
8128 case BUILT_IN_ISLESSEQUAL:
8129 case BUILT_IN_ISLESSGREATER:
8130 case BUILT_IN_ISUNORDERED:
8131 if (builtin_function_validate_nargs (fndecl, nargs, 2))
8133 enum tree_code code0, code1;
8134 code0 = TREE_CODE (TREE_TYPE (args[0]));
8135 code1 = TREE_CODE (TREE_TYPE (args[1]));
8136 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
8137 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8138 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
8140 error ("non-floating-point arguments in call to "
8141 "function %qE", fndecl);
8142 return false;
8144 return true;
8146 return false;
8148 case BUILT_IN_FPCLASSIFY:
8149 if (builtin_function_validate_nargs (fndecl, nargs, 6))
8151 unsigned i;
8153 for (i=0; i<5; i++)
8154 if (TREE_CODE (args[i]) != INTEGER_CST)
8156 error ("non-const integer argument %u in call to function %qE",
8157 i+1, fndecl);
8158 return false;
8161 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
8163 error ("non-floating-point argument in call to function %qE",
8164 fndecl);
8165 return false;
8167 return true;
8169 return false;
8171 default:
8172 return true;
8176 /* Function to help qsort sort FIELD_DECLs by name order. */
8179 field_decl_cmp (const void *x_p, const void *y_p)
8181 const tree *const x = (const tree *const) x_p;
8182 const tree *const y = (const tree *const) y_p;
8184 if (DECL_NAME (*x) == DECL_NAME (*y))
8185 /* A nontype is "greater" than a type. */
8186 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8187 if (DECL_NAME (*x) == NULL_TREE)
8188 return -1;
8189 if (DECL_NAME (*y) == NULL_TREE)
8190 return 1;
8191 if (DECL_NAME (*x) < DECL_NAME (*y))
8192 return -1;
8193 return 1;
8196 static struct {
8197 gt_pointer_operator new_value;
8198 void *cookie;
8199 } resort_data;
8201 /* This routine compares two fields like field_decl_cmp but using the
8202 pointer operator in resort_data. */
8204 static int
8205 resort_field_decl_cmp (const void *x_p, const void *y_p)
8207 const tree *const x = (const tree *const) x_p;
8208 const tree *const y = (const tree *const) y_p;
8210 if (DECL_NAME (*x) == DECL_NAME (*y))
8211 /* A nontype is "greater" than a type. */
8212 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8213 if (DECL_NAME (*x) == NULL_TREE)
8214 return -1;
8215 if (DECL_NAME (*y) == NULL_TREE)
8216 return 1;
8218 tree d1 = DECL_NAME (*x);
8219 tree d2 = DECL_NAME (*y);
8220 resort_data.new_value (&d1, resort_data.cookie);
8221 resort_data.new_value (&d2, resort_data.cookie);
8222 if (d1 < d2)
8223 return -1;
8225 return 1;
8228 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8230 void
8231 resort_sorted_fields (void *obj,
8232 void * ARG_UNUSED (orig_obj),
8233 gt_pointer_operator new_value,
8234 void *cookie)
8236 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8237 resort_data.new_value = new_value;
8238 resort_data.cookie = cookie;
8239 qsort (&sf->elts[0], sf->len, sizeof (tree),
8240 resort_field_decl_cmp);
8243 /* Subroutine of c_parse_error.
8244 Return the result of concatenating LHS and RHS. RHS is really
8245 a string literal, its first character is indicated by RHS_START and
8246 RHS_SIZE is its length (including the terminating NUL character).
8248 The caller is responsible for deleting the returned pointer. */
8250 static char *
8251 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
8253 const int lhs_size = strlen (lhs);
8254 char *result = XNEWVEC (char, lhs_size + rhs_size);
8255 strncpy (result, lhs, lhs_size);
8256 strncpy (result + lhs_size, rhs_start, rhs_size);
8257 return result;
8260 /* Issue the error given by GMSGID, indicating that it occurred before
8261 TOKEN, which had the associated VALUE. */
8263 void
8264 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
8265 tree value, unsigned char token_flags)
8267 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
8269 char *message = NULL;
8271 if (token_type == CPP_EOF)
8272 message = catenate_messages (gmsgid, " at end of input");
8273 else if (token_type == CPP_CHAR
8274 || token_type == CPP_WCHAR
8275 || token_type == CPP_CHAR16
8276 || token_type == CPP_CHAR32)
8278 unsigned int val = TREE_INT_CST_LOW (value);
8279 const char *prefix;
8281 switch (token_type)
8283 default:
8284 prefix = "";
8285 break;
8286 case CPP_WCHAR:
8287 prefix = "L";
8288 break;
8289 case CPP_CHAR16:
8290 prefix = "u";
8291 break;
8292 case CPP_CHAR32:
8293 prefix = "U";
8294 break;
8297 if (val <= UCHAR_MAX && ISGRAPH (val))
8298 message = catenate_messages (gmsgid, " before %s'%c'");
8299 else
8300 message = catenate_messages (gmsgid, " before %s'\\x%x'");
8302 error (message, prefix, val);
8303 free (message);
8304 message = NULL;
8306 else if (token_type == CPP_STRING
8307 || token_type == CPP_WSTRING
8308 || token_type == CPP_STRING16
8309 || token_type == CPP_STRING32
8310 || token_type == CPP_UTF8STRING)
8311 message = catenate_messages (gmsgid, " before string constant");
8312 else if (token_type == CPP_NUMBER)
8313 message = catenate_messages (gmsgid, " before numeric constant");
8314 else if (token_type == CPP_NAME)
8316 message = catenate_messages (gmsgid, " before %qE");
8317 error (message, value);
8318 free (message);
8319 message = NULL;
8321 else if (token_type == CPP_PRAGMA)
8322 message = catenate_messages (gmsgid, " before %<#pragma%>");
8323 else if (token_type == CPP_PRAGMA_EOL)
8324 message = catenate_messages (gmsgid, " before end of line");
8325 else if (token_type < N_TTYPES)
8327 message = catenate_messages (gmsgid, " before %qs token");
8328 error (message, cpp_type2name (token_type, token_flags));
8329 free (message);
8330 message = NULL;
8332 else
8333 error (gmsgid);
8335 if (message)
8337 error (message);
8338 free (message);
8340 #undef catenate_messages
8343 /* Mapping for cpp message reasons to the options that enable them. */
8345 struct reason_option_codes_t
8347 const int reason; /* cpplib message reason. */
8348 const int option_code; /* gcc option that controls this message. */
8351 static const struct reason_option_codes_t option_codes[] = {
8352 {CPP_W_DEPRECATED, OPT_Wdeprecated},
8353 {CPP_W_COMMENTS, OPT_Wcomment},
8354 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
8355 {CPP_W_MULTICHAR, OPT_Wmultichar},
8356 {CPP_W_TRADITIONAL, OPT_Wtraditional},
8357 {CPP_W_LONG_LONG, OPT_Wlong_long},
8358 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
8359 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
8360 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
8361 {CPP_W_UNDEF, OPT_Wundef},
8362 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
8363 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
8364 {CPP_W_NORMALIZE, OPT_Wnormalized_},
8365 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
8366 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
8367 {CPP_W_NONE, 0}
8370 /* Return the gcc option code associated with the reason for a cpp
8371 message, or 0 if none. */
8373 static int
8374 c_option_controlling_cpp_error (int reason)
8376 const struct reason_option_codes_t *entry;
8378 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
8380 if (entry->reason == reason)
8381 return entry->option_code;
8383 return 0;
8386 /* Callback from cpp_error for PFILE to print diagnostics from the
8387 preprocessor. The diagnostic is of type LEVEL, with REASON set
8388 to the reason code if LEVEL is represents a warning, at location
8389 LOCATION unless this is after lexing and the compiler's location
8390 should be used instead, with column number possibly overridden by
8391 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
8392 the arguments. Returns true if a diagnostic was emitted, false
8393 otherwise. */
8395 bool
8396 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8397 location_t location, unsigned int column_override,
8398 const char *msg, va_list *ap)
8400 diagnostic_info diagnostic;
8401 diagnostic_t dlevel;
8402 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
8403 bool ret;
8405 switch (level)
8407 case CPP_DL_WARNING_SYSHDR:
8408 if (flag_no_output)
8409 return false;
8410 global_dc->dc_warn_system_headers = 1;
8411 /* Fall through. */
8412 case CPP_DL_WARNING:
8413 if (flag_no_output)
8414 return false;
8415 dlevel = DK_WARNING;
8416 break;
8417 case CPP_DL_PEDWARN:
8418 if (flag_no_output && !flag_pedantic_errors)
8419 return false;
8420 dlevel = DK_PEDWARN;
8421 break;
8422 case CPP_DL_ERROR:
8423 dlevel = DK_ERROR;
8424 break;
8425 case CPP_DL_ICE:
8426 dlevel = DK_ICE;
8427 break;
8428 case CPP_DL_NOTE:
8429 dlevel = DK_NOTE;
8430 break;
8431 case CPP_DL_FATAL:
8432 dlevel = DK_FATAL;
8433 break;
8434 default:
8435 gcc_unreachable ();
8437 if (done_lexing)
8438 location = input_location;
8439 diagnostic_set_info_translated (&diagnostic, msg, ap,
8440 location, dlevel);
8441 if (column_override)
8442 diagnostic_override_column (&diagnostic, column_override);
8443 diagnostic_override_option_index (&diagnostic,
8444 c_option_controlling_cpp_error (reason));
8445 ret = report_diagnostic (&diagnostic);
8446 if (level == CPP_DL_WARNING_SYSHDR)
8447 global_dc->dc_warn_system_headers = save_warn_system_headers;
8448 return ret;
8451 /* Convert a character from the host to the target execution character
8452 set. cpplib handles this, mostly. */
8454 HOST_WIDE_INT
8455 c_common_to_target_charset (HOST_WIDE_INT c)
8457 /* Character constants in GCC proper are sign-extended under -fsigned-char,
8458 zero-extended under -fno-signed-char. cpplib insists that characters
8459 and character constants are always unsigned. Hence we must convert
8460 back and forth. */
8461 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
8463 uc = cpp_host_to_exec_charset (parse_in, uc);
8465 if (flag_signed_char)
8466 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
8467 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
8468 else
8469 return uc;
8472 /* Build the result of __builtin_offsetof. EXPR is a nested sequence of
8473 component references, with STOP_REF, or alternatively an INDIRECT_REF of
8474 NULL, at the bottom; much like the traditional rendering of offsetof as a
8475 macro. Returns the folded and properly cast result. */
8477 static tree
8478 fold_offsetof_1 (tree expr, tree stop_ref)
8480 enum tree_code code = PLUS_EXPR;
8481 tree base, off, t;
8483 if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK)
8484 return size_zero_node;
8486 switch (TREE_CODE (expr))
8488 case ERROR_MARK:
8489 return expr;
8491 case VAR_DECL:
8492 error ("cannot apply %<offsetof%> to static data member %qD", expr);
8493 return error_mark_node;
8495 case CALL_EXPR:
8496 case TARGET_EXPR:
8497 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
8498 return error_mark_node;
8500 case NOP_EXPR:
8501 case INDIRECT_REF:
8502 if (!integer_zerop (TREE_OPERAND (expr, 0)))
8504 error ("cannot apply %<offsetof%> to a non constant address");
8505 return error_mark_node;
8507 return size_zero_node;
8509 case COMPONENT_REF:
8510 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8511 if (base == error_mark_node)
8512 return base;
8514 t = TREE_OPERAND (expr, 1);
8515 if (DECL_C_BIT_FIELD (t))
8517 error ("attempt to take address of bit-field structure "
8518 "member %qD", t);
8519 return error_mark_node;
8521 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
8522 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t),
8524 / BITS_PER_UNIT));
8525 break;
8527 case ARRAY_REF:
8528 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref);
8529 if (base == error_mark_node)
8530 return base;
8532 t = TREE_OPERAND (expr, 1);
8533 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
8535 code = MINUS_EXPR;
8536 t = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (t), t);
8538 t = convert (sizetype, t);
8539 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
8541 /* Check if the offset goes beyond the upper bound of the array. */
8542 if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST)
8544 tree upbound = array_ref_up_bound (expr);
8545 if (upbound != NULL_TREE
8546 && TREE_CODE (upbound) == INTEGER_CST
8547 && !tree_int_cst_equal (upbound,
8548 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
8550 upbound = size_binop (PLUS_EXPR, upbound,
8551 build_int_cst (TREE_TYPE (upbound), 1));
8552 if (tree_int_cst_lt (upbound, t))
8554 tree v;
8556 for (v = TREE_OPERAND (expr, 0);
8557 TREE_CODE (v) == COMPONENT_REF;
8558 v = TREE_OPERAND (v, 0))
8559 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
8560 == RECORD_TYPE)
8562 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
8563 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
8564 if (TREE_CODE (fld_chain) == FIELD_DECL)
8565 break;
8567 if (fld_chain)
8568 break;
8570 /* Don't warn if the array might be considered a poor
8571 man's flexible array member with a very permissive
8572 definition thereof. */
8573 if (TREE_CODE (v) == ARRAY_REF
8574 || TREE_CODE (v) == COMPONENT_REF)
8575 warning (OPT_Warray_bounds,
8576 "index %E denotes an offset "
8577 "greater than size of %qT",
8578 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
8582 break;
8584 case COMPOUND_EXPR:
8585 /* Handle static members of volatile structs. */
8586 t = TREE_OPERAND (expr, 1);
8587 gcc_assert (TREE_CODE (t) == VAR_DECL);
8588 return fold_offsetof_1 (t, stop_ref);
8590 default:
8591 gcc_unreachable ();
8594 return size_binop (code, base, off);
8597 tree
8598 fold_offsetof (tree expr, tree stop_ref)
8600 /* Convert back from the internal sizetype to size_t. */
8601 return convert (size_type_node, fold_offsetof_1 (expr, stop_ref));
8604 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
8605 expression, because B will always be true. */
8607 void
8608 warn_for_omitted_condop (location_t location, tree cond)
8610 if (truth_value_p (TREE_CODE (cond)))
8611 warning_at (location, OPT_Wparentheses,
8612 "the omitted middle operand in ?: will always be %<true%>, "
8613 "suggest explicit middle operand");
8616 /* Give an error for storing into ARG, which is 'const'. USE indicates
8617 how ARG was being used. */
8619 void
8620 readonly_error (tree arg, enum lvalue_use use)
8622 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
8623 || use == lv_asm);
8624 /* Using this macro rather than (for example) arrays of messages
8625 ensures that all the format strings are checked at compile
8626 time. */
8627 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
8628 : (use == lv_increment ? (I) \
8629 : (use == lv_decrement ? (D) : (AS))))
8630 if (TREE_CODE (arg) == COMPONENT_REF)
8632 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
8633 error (READONLY_MSG (G_("assignment of member "
8634 "%qD in read-only object"),
8635 G_("increment of member "
8636 "%qD in read-only object"),
8637 G_("decrement of member "
8638 "%qD in read-only object"),
8639 G_("member %qD in read-only object "
8640 "used as %<asm%> output")),
8641 TREE_OPERAND (arg, 1));
8642 else
8643 error (READONLY_MSG (G_("assignment of read-only member %qD"),
8644 G_("increment of read-only member %qD"),
8645 G_("decrement of read-only member %qD"),
8646 G_("read-only member %qD used as %<asm%> output")),
8647 TREE_OPERAND (arg, 1));
8649 else if (TREE_CODE (arg) == VAR_DECL)
8650 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
8651 G_("increment of read-only variable %qD"),
8652 G_("decrement of read-only variable %qD"),
8653 G_("read-only variable %qD used as %<asm%> output")),
8654 arg);
8655 else if (TREE_CODE (arg) == PARM_DECL)
8656 error (READONLY_MSG (G_("assignment of read-only parameter %qD"),
8657 G_("increment of read-only parameter %qD"),
8658 G_("decrement of read-only parameter %qD"),
8659 G_("read-only parameter %qD use as %<asm%> output")),
8660 arg);
8661 else if (TREE_CODE (arg) == RESULT_DECL)
8663 gcc_assert (c_dialect_cxx ());
8664 error (READONLY_MSG (G_("assignment of "
8665 "read-only named return value %qD"),
8666 G_("increment of "
8667 "read-only named return value %qD"),
8668 G_("decrement of "
8669 "read-only named return value %qD"),
8670 G_("read-only named return value %qD "
8671 "used as %<asm%>output")),
8672 arg);
8674 else if (TREE_CODE (arg) == FUNCTION_DECL)
8675 error (READONLY_MSG (G_("assignment of function %qD"),
8676 G_("increment of function %qD"),
8677 G_("decrement of function %qD"),
8678 G_("function %qD used as %<asm%> output")),
8679 arg);
8680 else
8681 error (READONLY_MSG (G_("assignment of read-only location %qE"),
8682 G_("increment of read-only location %qE"),
8683 G_("decrement of read-only location %qE"),
8684 G_("read-only location %qE used as %<asm%> output")),
8685 arg);
8688 /* Print an error message for an invalid lvalue. USE says
8689 how the lvalue is being used and so selects the error message. LOC
8690 is the location for the error. */
8692 void
8693 lvalue_error (location_t loc, enum lvalue_use use)
8695 switch (use)
8697 case lv_assign:
8698 error_at (loc, "lvalue required as left operand of assignment");
8699 break;
8700 case lv_increment:
8701 error_at (loc, "lvalue required as increment operand");
8702 break;
8703 case lv_decrement:
8704 error_at (loc, "lvalue required as decrement operand");
8705 break;
8706 case lv_addressof:
8707 error_at (loc, "lvalue required as unary %<&%> operand");
8708 break;
8709 case lv_asm:
8710 error_at (loc, "lvalue required in asm statement");
8711 break;
8712 default:
8713 gcc_unreachable ();
8717 /* Print an error message for an invalid indirection of type TYPE.
8718 ERRSTRING is the name of the operator for the indirection. */
8720 void
8721 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
8723 switch (errstring)
8725 case RO_NULL:
8726 gcc_assert (c_dialect_cxx ());
8727 error_at (loc, "invalid type argument (have %qT)", type);
8728 break;
8729 case RO_ARRAY_INDEXING:
8730 error_at (loc,
8731 "invalid type argument of array indexing (have %qT)",
8732 type);
8733 break;
8734 case RO_UNARY_STAR:
8735 error_at (loc,
8736 "invalid type argument of unary %<*%> (have %qT)",
8737 type);
8738 break;
8739 case RO_ARROW:
8740 error_at (loc,
8741 "invalid type argument of %<->%> (have %qT)",
8742 type);
8743 break;
8744 case RO_IMPLICIT_CONVERSION:
8745 error_at (loc,
8746 "invalid type argument of implicit conversion (have %qT)",
8747 type);
8748 break;
8749 default:
8750 gcc_unreachable ();
8754 /* *PTYPE is an incomplete array. Complete it with a domain based on
8755 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
8756 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8757 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
8760 complete_array_type (tree *ptype, tree initial_value, bool do_default)
8762 tree maxindex, type, main_type, elt, unqual_elt;
8763 int failure = 0, quals;
8764 hashval_t hashcode = 0;
8766 maxindex = size_zero_node;
8767 if (initial_value)
8769 if (TREE_CODE (initial_value) == STRING_CST)
8771 int eltsize
8772 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8773 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
8775 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8777 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
8779 if (VEC_empty (constructor_elt, v))
8781 if (pedantic)
8782 failure = 3;
8783 maxindex = integer_minus_one_node;
8785 else
8787 tree curindex;
8788 unsigned HOST_WIDE_INT cnt;
8789 constructor_elt *ce;
8790 bool fold_p = false;
8792 if (VEC_index (constructor_elt, v, 0)->index)
8793 maxindex = fold_convert_loc (input_location, sizetype,
8794 VEC_index (constructor_elt,
8795 v, 0)->index);
8796 curindex = maxindex;
8798 for (cnt = 1;
8799 VEC_iterate (constructor_elt, v, cnt, ce);
8800 cnt++)
8802 bool curfold_p = false;
8803 if (ce->index)
8804 curindex = ce->index, curfold_p = true;
8805 else
8807 if (fold_p)
8808 curindex = fold_convert (sizetype, curindex);
8809 curindex = size_binop (PLUS_EXPR, curindex,
8810 size_one_node);
8812 if (tree_int_cst_lt (maxindex, curindex))
8813 maxindex = curindex, fold_p = curfold_p;
8815 if (fold_p)
8816 maxindex = fold_convert (sizetype, maxindex);
8819 else
8821 /* Make an error message unless that happened already. */
8822 if (initial_value != error_mark_node)
8823 failure = 1;
8826 else
8828 failure = 2;
8829 if (!do_default)
8830 return failure;
8833 type = *ptype;
8834 elt = TREE_TYPE (type);
8835 quals = TYPE_QUALS (strip_array_types (elt));
8836 if (quals == 0)
8837 unqual_elt = elt;
8838 else
8839 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
8841 /* Using build_distinct_type_copy and modifying things afterward instead
8842 of using build_array_type to create a new type preserves all of the
8843 TYPE_LANG_FLAG_? bits that the front end may have set. */
8844 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8845 TREE_TYPE (main_type) = unqual_elt;
8846 TYPE_DOMAIN (main_type)
8847 = build_range_type (TREE_TYPE (maxindex),
8848 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
8849 layout_type (main_type);
8851 /* Make sure we have the canonical MAIN_TYPE. */
8852 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
8853 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
8854 hashcode);
8855 main_type = type_hash_canon (hashcode, main_type);
8857 /* Fix the canonical type. */
8858 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
8859 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
8860 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
8861 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
8862 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
8863 != TYPE_DOMAIN (main_type)))
8864 TYPE_CANONICAL (main_type)
8865 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8866 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
8867 else
8868 TYPE_CANONICAL (main_type) = main_type;
8870 if (quals == 0)
8871 type = main_type;
8872 else
8873 type = c_build_qualified_type (main_type, quals);
8875 if (COMPLETE_TYPE_P (type)
8876 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8877 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8879 error ("size of array is too large");
8880 /* If we proceed with the array type as it is, we'll eventually
8881 crash in tree_low_cst(). */
8882 type = error_mark_node;
8885 *ptype = type;
8886 return failure;
8889 /* Like c_mark_addressable but don't check register qualifier. */
8890 void
8891 c_common_mark_addressable_vec (tree t)
8893 while (handled_component_p (t))
8894 t = TREE_OPERAND (t, 0);
8895 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
8896 return;
8897 TREE_ADDRESSABLE (t) = 1;
8902 /* Used to help initialize the builtin-types.def table. When a type of
8903 the correct size doesn't exist, use error_mark_node instead of NULL.
8904 The later results in segfaults even when a decl using the type doesn't
8905 get invoked. */
8907 tree
8908 builtin_type_for_size (int size, bool unsignedp)
8910 tree type = lang_hooks.types.type_for_size (size, unsignedp);
8911 return type ? type : error_mark_node;
8914 /* A helper function for resolve_overloaded_builtin in resolving the
8915 overloaded __sync_ builtins. Returns a positive power of 2 if the
8916 first operand of PARAMS is a pointer to a supported data type.
8917 Returns 0 if an error is encountered. */
8919 static int
8920 sync_resolve_size (tree function, VEC(tree,gc) *params)
8922 tree type;
8923 int size;
8925 if (VEC_empty (tree, params))
8927 error ("too few arguments to function %qE", function);
8928 return 0;
8931 type = TREE_TYPE (VEC_index (tree, params, 0));
8932 if (TREE_CODE (type) != POINTER_TYPE)
8933 goto incompatible;
8935 type = TREE_TYPE (type);
8936 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8937 goto incompatible;
8939 size = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
8940 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
8941 return size;
8943 incompatible:
8944 error ("incompatible type for argument %d of %qE", 1, function);
8945 return 0;
8948 /* A helper function for resolve_overloaded_builtin. Adds casts to
8949 PARAMS to make arguments match up with those of FUNCTION. Drops
8950 the variadic arguments at the end. Returns false if some error
8951 was encountered; true on success. */
8953 static bool
8954 sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params)
8956 function_args_iterator iter;
8957 tree ptype;
8958 unsigned int parmnum;
8960 function_args_iter_init (&iter, TREE_TYPE (function));
8961 /* We've declared the implementation functions to use "volatile void *"
8962 as the pointer parameter, so we shouldn't get any complaints from the
8963 call to check_function_arguments what ever type the user used. */
8964 function_args_iter_next (&iter);
8965 ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0)));
8967 /* For the rest of the values, we need to cast these to FTYPE, so that we
8968 don't get warnings for passing pointer types, etc. */
8969 parmnum = 0;
8970 while (1)
8972 tree val, arg_type;
8974 arg_type = function_args_iter_cond (&iter);
8975 /* XXX void_type_node belies the abstraction. */
8976 if (arg_type == void_type_node)
8977 break;
8979 ++parmnum;
8980 if (VEC_length (tree, params) <= parmnum)
8982 error ("too few arguments to function %qE", orig_function);
8983 return false;
8986 /* ??? Ideally for the first conversion we'd use convert_for_assignment
8987 so that we get warnings for anything that doesn't match the pointer
8988 type. This isn't portable across the C and C++ front ends atm. */
8989 val = VEC_index (tree, params, parmnum);
8990 val = convert (ptype, val);
8991 val = convert (arg_type, val);
8992 VEC_replace (tree, params, parmnum, val);
8994 function_args_iter_next (&iter);
8997 /* The definition of these primitives is variadic, with the remaining
8998 being "an optional list of variables protected by the memory barrier".
8999 No clue what that's supposed to mean, precisely, but we consider all
9000 call-clobbered variables to be protected so we're safe. */
9001 VEC_truncate (tree, params, parmnum + 1);
9003 return true;
9006 /* A helper function for resolve_overloaded_builtin. Adds a cast to
9007 RESULT to make it match the type of the first pointer argument in
9008 PARAMS. */
9010 static tree
9011 sync_resolve_return (tree first_param, tree result)
9013 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
9014 ptype = TYPE_MAIN_VARIANT (ptype);
9015 return convert (ptype, result);
9018 /* Some builtin functions are placeholders for other expressions. This
9019 function should be called immediately after parsing the call expression
9020 before surrounding code has committed to the type of the expression.
9022 LOC is the location of the builtin call.
9024 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
9025 PARAMS is the argument list for the call. The return value is non-null
9026 when expansion is complete, and null if normal processing should
9027 continue. */
9029 tree
9030 resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params)
9032 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
9033 switch (DECL_BUILT_IN_CLASS (function))
9035 case BUILT_IN_NORMAL:
9036 break;
9037 case BUILT_IN_MD:
9038 if (targetm.resolve_overloaded_builtin)
9039 return targetm.resolve_overloaded_builtin (loc, function, params);
9040 else
9041 return NULL_TREE;
9042 default:
9043 return NULL_TREE;
9046 /* Handle BUILT_IN_NORMAL here. */
9047 switch (orig_code)
9049 case BUILT_IN_FETCH_AND_ADD_N:
9050 case BUILT_IN_FETCH_AND_SUB_N:
9051 case BUILT_IN_FETCH_AND_OR_N:
9052 case BUILT_IN_FETCH_AND_AND_N:
9053 case BUILT_IN_FETCH_AND_XOR_N:
9054 case BUILT_IN_FETCH_AND_NAND_N:
9055 case BUILT_IN_ADD_AND_FETCH_N:
9056 case BUILT_IN_SUB_AND_FETCH_N:
9057 case BUILT_IN_OR_AND_FETCH_N:
9058 case BUILT_IN_AND_AND_FETCH_N:
9059 case BUILT_IN_XOR_AND_FETCH_N:
9060 case BUILT_IN_NAND_AND_FETCH_N:
9061 case BUILT_IN_BOOL_COMPARE_AND_SWAP_N:
9062 case BUILT_IN_VAL_COMPARE_AND_SWAP_N:
9063 case BUILT_IN_LOCK_TEST_AND_SET_N:
9064 case BUILT_IN_LOCK_RELEASE_N:
9066 int n = sync_resolve_size (function, params);
9067 tree new_function, first_param, result;
9069 if (n == 0)
9070 return error_mark_node;
9072 new_function = built_in_decls[orig_code + exact_log2 (n) + 1];
9073 if (!sync_resolve_params (function, new_function, params))
9074 return error_mark_node;
9076 first_param = VEC_index (tree, params, 0);
9077 result = build_function_call_vec (loc, new_function, params, NULL);
9078 if (orig_code != BUILT_IN_BOOL_COMPARE_AND_SWAP_N
9079 && orig_code != BUILT_IN_LOCK_RELEASE_N)
9080 result = sync_resolve_return (first_param, result);
9082 return result;
9085 default:
9086 return NULL_TREE;
9090 /* Ignoring their sign, return true if two scalar types are the same. */
9091 bool
9092 same_scalar_type_ignoring_signedness (tree t1, tree t2)
9094 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
9096 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
9097 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
9098 || c2 == FIXED_POINT_TYPE));
9100 /* Equality works here because c_common_signed_type uses
9101 TYPE_MAIN_VARIANT. */
9102 return c_common_signed_type (t1)
9103 == c_common_signed_type (t2);
9106 /* Check for missing format attributes on function pointers. LTYPE is
9107 the new type or left-hand side type. RTYPE is the old type or
9108 right-hand side type. Returns TRUE if LTYPE is missing the desired
9109 attribute. */
9111 bool
9112 check_missing_format_attribute (tree ltype, tree rtype)
9114 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
9115 tree ra;
9117 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
9118 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
9119 break;
9120 if (ra)
9122 tree la;
9123 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
9124 if (is_attribute_p ("format", TREE_PURPOSE (la)))
9125 break;
9126 return !la;
9128 else
9129 return false;
9132 /* Subscripting with type char is likely to lose on a machine where
9133 chars are signed. So warn on any machine, but optionally. Don't
9134 warn for unsigned char since that type is safe. Don't warn for
9135 signed char because anyone who uses that must have done so
9136 deliberately. Furthermore, we reduce the false positive load by
9137 warning only for non-constant value of type char. */
9139 void
9140 warn_array_subscript_with_type_char (tree index)
9142 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
9143 && TREE_CODE (index) != INTEGER_CST)
9144 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
9147 /* Implement -Wparentheses for the unexpected C precedence rules, to
9148 cover cases like x + y << z which readers are likely to
9149 misinterpret. We have seen an expression in which CODE is a binary
9150 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
9151 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
9152 CODE_RIGHT may be ERROR_MARK, which means that that side of the
9153 expression was not formed using a binary or unary operator, or it
9154 was enclosed in parentheses. */
9156 void
9157 warn_about_parentheses (enum tree_code code,
9158 enum tree_code code_left, tree arg_left,
9159 enum tree_code code_right, tree arg_right)
9161 if (!warn_parentheses)
9162 return;
9164 /* This macro tests that the expression ARG with original tree code
9165 CODE appears to be a boolean expression. or the result of folding a
9166 boolean expression. */
9167 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
9168 (truth_value_p (TREE_CODE (ARG)) \
9169 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
9170 /* Folding may create 0 or 1 integers from other expressions. */ \
9171 || ((CODE) != INTEGER_CST \
9172 && (integer_onep (ARG) || integer_zerop (ARG))))
9174 switch (code)
9176 case LSHIFT_EXPR:
9177 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9178 warning (OPT_Wparentheses,
9179 "suggest parentheses around %<+%> inside %<<<%>");
9180 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9181 warning (OPT_Wparentheses,
9182 "suggest parentheses around %<-%> inside %<<<%>");
9183 return;
9185 case RSHIFT_EXPR:
9186 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9187 warning (OPT_Wparentheses,
9188 "suggest parentheses around %<+%> inside %<>>%>");
9189 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9190 warning (OPT_Wparentheses,
9191 "suggest parentheses around %<-%> inside %<>>%>");
9192 return;
9194 case TRUTH_ORIF_EXPR:
9195 if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR)
9196 warning (OPT_Wparentheses,
9197 "suggest parentheses around %<&&%> within %<||%>");
9198 return;
9200 case BIT_IOR_EXPR:
9201 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
9202 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9203 || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
9204 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9205 warning (OPT_Wparentheses,
9206 "suggest parentheses around arithmetic in operand of %<|%>");
9207 /* Check cases like x|y==z */
9208 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9209 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9210 warning (OPT_Wparentheses,
9211 "suggest parentheses around comparison in operand of %<|%>");
9212 /* Check cases like !x | y */
9213 else if (code_left == TRUTH_NOT_EXPR
9214 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9215 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9216 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
9217 return;
9219 case BIT_XOR_EXPR:
9220 if (code_left == BIT_AND_EXPR
9221 || code_left == PLUS_EXPR || code_left == MINUS_EXPR
9222 || code_right == BIT_AND_EXPR
9223 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
9224 warning (OPT_Wparentheses,
9225 "suggest parentheses around arithmetic in operand of %<^%>");
9226 /* Check cases like x^y==z */
9227 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9228 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9229 warning (OPT_Wparentheses,
9230 "suggest parentheses around comparison in operand of %<^%>");
9231 return;
9233 case BIT_AND_EXPR:
9234 if (code_left == PLUS_EXPR || code_right == PLUS_EXPR)
9235 warning (OPT_Wparentheses,
9236 "suggest parentheses around %<+%> in operand of %<&%>");
9237 else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR)
9238 warning (OPT_Wparentheses,
9239 "suggest parentheses around %<-%> in operand of %<&%>");
9240 /* Check cases like x&y==z */
9241 else if (TREE_CODE_CLASS (code_left) == tcc_comparison
9242 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9243 warning (OPT_Wparentheses,
9244 "suggest parentheses around comparison in operand of %<&%>");
9245 /* Check cases like !x & y */
9246 else if (code_left == TRUTH_NOT_EXPR
9247 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
9248 warning (OPT_Wparentheses, "suggest parentheses around operand of "
9249 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
9250 return;
9252 case EQ_EXPR:
9253 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9254 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9255 warning (OPT_Wparentheses,
9256 "suggest parentheses around comparison in operand of %<==%>");
9257 return;
9258 case NE_EXPR:
9259 if (TREE_CODE_CLASS (code_left) == tcc_comparison
9260 || TREE_CODE_CLASS (code_right) == tcc_comparison)
9261 warning (OPT_Wparentheses,
9262 "suggest parentheses around comparison in operand of %<!=%>");
9263 return;
9265 default:
9266 if (TREE_CODE_CLASS (code) == tcc_comparison
9267 && ((TREE_CODE_CLASS (code_left) == tcc_comparison
9268 && code_left != NE_EXPR && code_left != EQ_EXPR
9269 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
9270 || (TREE_CODE_CLASS (code_right) == tcc_comparison
9271 && code_right != NE_EXPR && code_right != EQ_EXPR
9272 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
9273 warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
9274 "have their mathematical meaning");
9275 return;
9277 #undef NOT_A_BOOLEAN_EXPR_P
9280 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
9282 void
9283 warn_for_unused_label (tree label)
9285 if (!TREE_USED (label))
9287 if (DECL_INITIAL (label))
9288 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
9289 else
9290 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
9294 /* Warn for division by zero according to the value of DIVISOR. LOC
9295 is the location of the division operator. */
9297 void
9298 warn_for_div_by_zero (location_t loc, tree divisor)
9300 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
9301 about division by zero. Do not issue a warning if DIVISOR has a
9302 floating-point type, since we consider 0.0/0.0 a valid way of
9303 generating a NaN. */
9304 if (c_inhibit_evaluation_warnings == 0
9305 && (integer_zerop (divisor) || fixed_zerop (divisor)))
9306 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
9309 /* Subroutine of build_binary_op. Give warnings for comparisons
9310 between signed and unsigned quantities that may fail. Do the
9311 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
9312 so that casts will be considered, but default promotions won't
9315 LOCATION is the location of the comparison operator.
9317 The arguments of this function map directly to local variables
9318 of build_binary_op. */
9320 void
9321 warn_for_sign_compare (location_t location,
9322 tree orig_op0, tree orig_op1,
9323 tree op0, tree op1,
9324 tree result_type, enum tree_code resultcode)
9326 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
9327 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
9328 int unsignedp0, unsignedp1;
9330 /* In C++, check for comparison of different enum types. */
9331 if (c_dialect_cxx()
9332 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
9333 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
9334 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
9335 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
9337 warning_at (location,
9338 OPT_Wsign_compare, "comparison between types %qT and %qT",
9339 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
9342 /* Do not warn if the comparison is being done in a signed type,
9343 since the signed type will only be chosen if it can represent
9344 all the values of the unsigned type. */
9345 if (!TYPE_UNSIGNED (result_type))
9346 /* OK */;
9347 /* Do not warn if both operands are unsigned. */
9348 else if (op0_signed == op1_signed)
9349 /* OK */;
9350 else
9352 tree sop, uop, base_type;
9353 bool ovf;
9355 if (op0_signed)
9356 sop = orig_op0, uop = orig_op1;
9357 else
9358 sop = orig_op1, uop = orig_op0;
9360 STRIP_TYPE_NOPS (sop);
9361 STRIP_TYPE_NOPS (uop);
9362 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
9363 ? TREE_TYPE (result_type) : result_type);
9365 /* Do not warn if the signed quantity is an unsuffixed integer
9366 literal (or some static constant expression involving such
9367 literals or a conditional expression involving such literals)
9368 and it is non-negative. */
9369 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
9370 /* OK */;
9371 /* Do not warn if the comparison is an equality operation, the
9372 unsigned quantity is an integral constant, and it would fit
9373 in the result if the result were signed. */
9374 else if (TREE_CODE (uop) == INTEGER_CST
9375 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
9376 && int_fits_type_p (uop, c_common_signed_type (base_type)))
9377 /* OK */;
9378 /* In C, do not warn if the unsigned quantity is an enumeration
9379 constant and its maximum value would fit in the result if the
9380 result were signed. */
9381 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
9382 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
9383 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
9384 c_common_signed_type (base_type)))
9385 /* OK */;
9386 else
9387 warning_at (location,
9388 OPT_Wsign_compare,
9389 "comparison between signed and unsigned integer expressions");
9392 /* Warn if two unsigned values are being compared in a size larger
9393 than their original size, and one (and only one) is the result of
9394 a `~' operator. This comparison will always fail.
9396 Also warn if one operand is a constant, and the constant does not
9397 have all bits set that are set in the ~ operand when it is
9398 extended. */
9400 op0 = c_common_get_narrower (op0, &unsignedp0);
9401 op1 = c_common_get_narrower (op1, &unsignedp1);
9403 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
9404 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
9406 if (TREE_CODE (op0) == BIT_NOT_EXPR)
9407 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
9408 if (TREE_CODE (op1) == BIT_NOT_EXPR)
9409 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
9411 if (host_integerp (op0, 0) || host_integerp (op1, 0))
9413 tree primop;
9414 HOST_WIDE_INT constant, mask;
9415 int unsignedp;
9416 unsigned int bits;
9418 if (host_integerp (op0, 0))
9420 primop = op1;
9421 unsignedp = unsignedp1;
9422 constant = tree_low_cst (op0, 0);
9424 else
9426 primop = op0;
9427 unsignedp = unsignedp0;
9428 constant = tree_low_cst (op1, 0);
9431 bits = TYPE_PRECISION (TREE_TYPE (primop));
9432 if (bits < TYPE_PRECISION (result_type)
9433 && bits < HOST_BITS_PER_LONG && unsignedp)
9435 mask = (~ (HOST_WIDE_INT) 0) << bits;
9436 if ((mask & constant) != mask)
9438 if (constant == 0)
9439 warning (OPT_Wsign_compare,
9440 "promoted ~unsigned is always non-zero");
9441 else
9442 warning_at (location, OPT_Wsign_compare,
9443 "comparison of promoted ~unsigned with constant");
9447 else if (unsignedp0 && unsignedp1
9448 && (TYPE_PRECISION (TREE_TYPE (op0))
9449 < TYPE_PRECISION (result_type))
9450 && (TYPE_PRECISION (TREE_TYPE (op1))
9451 < TYPE_PRECISION (result_type)))
9452 warning_at (location, OPT_Wsign_compare,
9453 "comparison of promoted ~unsigned with unsigned");
9457 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
9458 type via c_common_type. If -Wdouble-promotion is in use, and the
9459 conditions for warning have been met, issue a warning. GMSGID is
9460 the warning message. It must have two %T specifiers for the type
9461 that was converted (generally "float") and the type to which it was
9462 converted (generally "double), respectively. LOC is the location
9463 to which the awrning should refer. */
9465 void
9466 do_warn_double_promotion (tree result_type, tree type1, tree type2,
9467 const char *gmsgid, location_t loc)
9469 tree source_type;
9471 if (!warn_double_promotion)
9472 return;
9473 /* If the conversion will not occur at run-time, there is no need to
9474 warn about it. */
9475 if (c_inhibit_evaluation_warnings)
9476 return;
9477 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
9478 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
9479 return;
9480 if (TYPE_MAIN_VARIANT (type1) == float_type_node
9481 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
9482 source_type = type1;
9483 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
9484 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
9485 source_type = type2;
9486 else
9487 return;
9488 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
9491 /* Setup a TYPE_DECL node as a typedef representation.
9493 X is a TYPE_DECL for a typedef statement. Create a brand new
9494 ..._TYPE node (which will be just a variant of the existing
9495 ..._TYPE node with identical properties) and then install X
9496 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
9498 The whole point here is to end up with a situation where each
9499 and every ..._TYPE node the compiler creates will be uniquely
9500 associated with AT MOST one node representing a typedef name.
9501 This way, even though the compiler substitutes corresponding
9502 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
9503 early on, later parts of the compiler can always do the reverse
9504 translation and get back the corresponding typedef name. For
9505 example, given:
9507 typedef struct S MY_TYPE;
9508 MY_TYPE object;
9510 Later parts of the compiler might only know that `object' was of
9511 type `struct S' if it were not for code just below. With this
9512 code however, later parts of the compiler see something like:
9514 struct S' == struct S
9515 typedef struct S' MY_TYPE;
9516 struct S' object;
9518 And they can then deduce (from the node for type struct S') that
9519 the original object declaration was:
9521 MY_TYPE object;
9523 Being able to do this is important for proper support of protoize,
9524 and also for generating precise symbolic debugging information
9525 which takes full account of the programmer's (typedef) vocabulary.
9527 Obviously, we don't want to generate a duplicate ..._TYPE node if
9528 the TYPE_DECL node that we are now processing really represents a
9529 standard built-in type. */
9531 void
9532 set_underlying_type (tree x)
9534 if (x == error_mark_node)
9535 return;
9536 if (DECL_IS_BUILTIN (x))
9538 if (TYPE_NAME (TREE_TYPE (x)) == 0)
9539 TYPE_NAME (TREE_TYPE (x)) = x;
9541 else if (TREE_TYPE (x) != error_mark_node
9542 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
9544 tree tt = TREE_TYPE (x);
9545 DECL_ORIGINAL_TYPE (x) = tt;
9546 tt = build_variant_type_copy (tt);
9547 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
9548 TYPE_NAME (tt) = x;
9549 TREE_USED (tt) = TREE_USED (x);
9550 TREE_TYPE (x) = tt;
9554 /* Record the types used by the current global variable declaration
9555 being parsed, so that we can decide later to emit their debug info.
9556 Those types are in types_used_by_cur_var_decl, and we are going to
9557 store them in the types_used_by_vars_hash hash table.
9558 DECL is the declaration of the global variable that has been parsed. */
9560 void
9561 record_types_used_by_current_var_decl (tree decl)
9563 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9565 while (!VEC_empty (tree, types_used_by_cur_var_decl))
9567 tree type = VEC_pop (tree, types_used_by_cur_var_decl);
9568 types_used_by_var_decl_insert (type, decl);
9572 /* The C and C++ parsers both use vectors to hold function arguments.
9573 For efficiency, we keep a cache of unused vectors. This is the
9574 cache. */
9576 typedef VEC(tree,gc)* tree_gc_vec;
9577 DEF_VEC_P(tree_gc_vec);
9578 DEF_VEC_ALLOC_P(tree_gc_vec,gc);
9579 static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache;
9581 /* Return a new vector from the cache. If the cache is empty,
9582 allocate a new vector. These vectors are GC'ed, so it is OK if the
9583 pointer is not released.. */
9585 VEC(tree,gc) *
9586 make_tree_vector (void)
9588 if (!VEC_empty (tree_gc_vec, tree_vector_cache))
9589 return VEC_pop (tree_gc_vec, tree_vector_cache);
9590 else
9592 /* Passing 0 to VEC_alloc returns NULL, and our callers require
9593 that we always return a non-NULL value. The vector code uses
9594 4 when growing a NULL vector, so we do too. */
9595 return VEC_alloc (tree, gc, 4);
9599 /* Release a vector of trees back to the cache. */
9601 void
9602 release_tree_vector (VEC(tree,gc) *vec)
9604 if (vec != NULL)
9606 VEC_truncate (tree, vec, 0);
9607 VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec);
9611 /* Get a new tree vector holding a single tree. */
9613 VEC(tree,gc) *
9614 make_tree_vector_single (tree t)
9616 VEC(tree,gc) *ret = make_tree_vector ();
9617 VEC_quick_push (tree, ret, t);
9618 return ret;
9621 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
9623 VEC(tree,gc) *
9624 make_tree_vector_from_list (tree list)
9626 VEC(tree,gc) *ret = make_tree_vector ();
9627 for (; list; list = TREE_CHAIN (list))
9628 VEC_safe_push (tree, gc, ret, TREE_VALUE (list));
9629 return ret;
9632 /* Get a new tree vector which is a copy of an existing one. */
9634 VEC(tree,gc) *
9635 make_tree_vector_copy (const VEC(tree,gc) *orig)
9637 VEC(tree,gc) *ret;
9638 unsigned int ix;
9639 tree t;
9641 ret = make_tree_vector ();
9642 VEC_reserve (tree, gc, ret, VEC_length (tree, orig));
9643 FOR_EACH_VEC_ELT (tree, orig, ix, t)
9644 VEC_quick_push (tree, ret, t);
9645 return ret;
9648 /* Return true if KEYWORD starts a type specifier. */
9650 bool
9651 keyword_begins_type_specifier (enum rid keyword)
9653 switch (keyword)
9655 case RID_INT:
9656 case RID_CHAR:
9657 case RID_FLOAT:
9658 case RID_DOUBLE:
9659 case RID_VOID:
9660 case RID_INT128:
9661 case RID_UNSIGNED:
9662 case RID_LONG:
9663 case RID_SHORT:
9664 case RID_SIGNED:
9665 case RID_DFLOAT32:
9666 case RID_DFLOAT64:
9667 case RID_DFLOAT128:
9668 case RID_FRACT:
9669 case RID_ACCUM:
9670 case RID_BOOL:
9671 case RID_WCHAR:
9672 case RID_CHAR16:
9673 case RID_CHAR32:
9674 case RID_SAT:
9675 case RID_COMPLEX:
9676 case RID_TYPEOF:
9677 case RID_STRUCT:
9678 case RID_CLASS:
9679 case RID_UNION:
9680 case RID_ENUM:
9681 return true;
9682 default:
9683 return false;
9687 /* Return true if KEYWORD names a type qualifier. */
9689 bool
9690 keyword_is_type_qualifier (enum rid keyword)
9692 switch (keyword)
9694 case RID_CONST:
9695 case RID_VOLATILE:
9696 case RID_RESTRICT:
9697 return true;
9698 default:
9699 return false;
9703 /* Return true if KEYWORD names a storage class specifier.
9705 RID_TYPEDEF is not included in this list despite `typedef' being
9706 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
9707 such for syntactic convenience only. */
9709 bool
9710 keyword_is_storage_class_specifier (enum rid keyword)
9712 switch (keyword)
9714 case RID_STATIC:
9715 case RID_EXTERN:
9716 case RID_REGISTER:
9717 case RID_AUTO:
9718 case RID_MUTABLE:
9719 case RID_THREAD:
9720 return true;
9721 default:
9722 return false;
9726 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
9728 static bool
9729 keyword_is_function_specifier (enum rid keyword)
9731 switch (keyword)
9733 case RID_INLINE:
9734 case RID_VIRTUAL:
9735 case RID_EXPLICIT:
9736 return true;
9737 default:
9738 return false;
9742 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9743 declaration-specifier (C99 6.7). */
9745 bool
9746 keyword_is_decl_specifier (enum rid keyword)
9748 if (keyword_is_storage_class_specifier (keyword)
9749 || keyword_is_type_qualifier (keyword)
9750 || keyword_is_function_specifier (keyword))
9751 return true;
9753 switch (keyword)
9755 case RID_TYPEDEF:
9756 case RID_FRIEND:
9757 case RID_CONSTEXPR:
9758 return true;
9759 default:
9760 return false;
9764 /* Initialize language-specific-bits of tree_contains_struct. */
9766 void
9767 c_common_init_ts (void)
9769 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
9770 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
9773 #include "gt-c-family-c-common.h"